summaryrefslogtreecommitdiffabout
path: root/mfd/savclt.c
blob: 447f685766115313b7502245589b0b362f730a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This file is part of Mailfromd.
   Copyright (C) 2005, 2006, 2007, 2008, 2009 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#define MF_SOURCE_NAME  MF_SOURCE_SAVCLT
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "mailfromd.h"

struct sockaddr *callout_server_sa;
socklen_t callout_server_sa_len;
time_t savclt_timeout = 3;

/* Ideally, I'd want to use smtp_io functions here. But they are
   based on Mailutils' TCP streams, which so far do not support
   UNIX sockets. So, for the time being, I use the straightforward
   I/O approach. */
int
savclt_getline(int fd, char **pbuf, size_t *pbufsize)
{
	char *buf = *pbuf;
	size_t bufsize = *pbufsize;
	int off = 0;
#define DELTA 128
	time_t start = time(NULL);
	
	if (buf == NULL) {
		bufsize = DELTA;
		buf = xmalloc(bufsize);
	}
	
	do {
		struct timeval tv;
		int rc, flags;
		time_t diff = time(NULL) - start;

		if (diff > savclt_timeout) {
			errno = ETIMEDOUT;
			return -1;
		}
		
		tv.tv_sec = savclt_timeout - diff;
		tv.tv_usec = 0;
		flags = MU_STREAM_READY_RD;
		rc = mu_fd_wait(fd, &flags, &tv);
		if (rc) {
			debug1(1, "mu_fd_wait: %s", mu_strerror(rc));
			errno = rc;
			return -1;
		}
		if (!(flags & MU_STREAM_READY_RD)) {
			errno = ETIMEDOUT;
			return -1;
		}
      
		if (off + 1 == bufsize) {
		       bufsize += DELTA;
		       buf = xrealloc(buf, bufsize);
		}

		rc = read(fd, buf + off, 1);
		if (rc == -1)
			return -1;
		if (rc == 0)
			break;
		off++;
	} while (buf[off - 1] != '\n');
	
	if (off + 1 == bufsize) {
		bufsize++;
		buf = xrealloc(buf, bufsize);
	}
	buf[off] = 0;
	*pbuf = buf;
	*pbufsize = bufsize;
	transcript("savclt", "RECV:", buf);
	return off;
}

static int
send_line(int fd, const char *txt, size_t len)
{
	time_t start = time(NULL);

	while (len) {
		struct timeval tv;
		int rc, flags;
		time_t diff = time(NULL) - start;

		if (diff > savclt_timeout) {
			errno = ETIMEDOUT;
			return -1;
		}
		
		tv.tv_sec = savclt_timeout - diff;
		tv.tv_usec = 0;
		flags = MU_STREAM_READY_WR;
		rc = mu_fd_wait(fd, &flags, &tv);
		if (rc) {
			debug1(1, "mu_fd_wait: %s", mu_strerror(rc));
			errno = rc;
			return -1;
		}
		if (!(flags & MU_STREAM_READY_WR)) {
			errno = ETIMEDOUT;
			return -1;
		}

		rc = write(fd, txt, len);
		if (rc <= 0) {
			if (errno == EAGAIN)
				continue;
			return -1;
		}
		txt += rc;
		len -= rc;
	}
	return 0;
}	

int
savclt_wrtline(int fd, char **pbuf, size_t *pbufsize,
	       const char *fmt, ...)
{
	int rc;
	va_list ap;

	va_start(ap, fmt);
	rc = mu_vasnprintf(pbuf, pbufsize, fmt, ap);
	va_end(ap);
	if (rc == 0) {
		transcript("savclt", "SEND:", *pbuf);
		rc = send_line(fd, *pbuf, strlen(*pbuf));
	}
	return rc;
}


static int
connect_callout_server()
{
	int fd;
	int flags;
	int rc;
	
	if (!callout_server_sa)
		return -1;
	fd = socket(callout_server_sa->sa_family, SOCK_STREAM, 0);
	if (fd == -1) {
		mu_error("schedule_callout: socket: %s",
			 mu_strerror(errno));
		return -1;
	}

	flags = fcntl(fd, F_GETFL);
	flags |= O_NONBLOCK;
	fcntl(fd, F_SETFL, flags);

	rc = connect(fd, callout_server_sa, callout_server_sa_len); 
	if (rc == -1) {
		if (errno == EINPROGRESS) {
			struct timeval tv;

			tv.tv_sec = savclt_timeout;
			tv.tv_usec = 0;
			flags = MU_STREAM_READY_WR;
			rc = mu_fd_wait(fd, &flags, &tv);
			if (rc) {
				debug1(1, "mu_fd_wait: %s", mu_strerror(rc));
				close(fd);
				return -1;
			}

			if (flags & MU_STREAM_READY_WR) {
				socklen_t len = sizeof(flags);
				rc = getsockopt(fd, SOL_SOCKET, SO_ERROR,
						&flags, &len);
				if (rc) {
					mu_error("getsockopt: %s",
						 mu_strerror(rc));
					close(fd);
					return -1;
				}
			} else
				flags = ETIMEDOUT;
		} else
			flags = errno;

		if (flags) {
			mu_error("connect: %s", mu_strerror(flags));
			close(fd);
			return -1;
		}
	}
	
	if (rc) {
		close(fd);
		return -1;
	}
	
	return fd;
}

enum callout_server_state {
	css_init,
	css_send,
	css_quit,
};

void
schedule_callout(const char *email, const char *ehlo, const char *mailfrom,
		 const char *client_addr)
{
	size_t size = 0;
	char *buf = NULL;
	int fd = connect_callout_server();
	enum callout_server_state state = css_init;
	int rc;
	
	while (state != css_quit) {
		if (savclt_getline(fd, &buf, &size) < 0)
			break;
		trimcrlf(buf);
		if (!(strlen(buf) > 2 && memcmp(buf, "OK", 2) == 0
		      && isspace(buf[2])))
			break;

		switch (state) {
		case css_init:
			if (client_addr)
				rc = savclt_wrtline(fd, &buf, &size,
				     "SVRFY \"%s\" \"%s\" \"%s\" \"%s\"\r\n",
				     email, ehlo, mailfrom, client_addr);
			else
				rc = savclt_wrtline(fd, &buf, &size,
				      "VRFY \"%s\" \"%s\" \"%s\"\r\n",
				      email, ehlo, mailfrom);
			state = (rc < 0) ? css_quit : css_send;
			break;
			
		case css_send:
			savclt_wrtline(fd, &buf, &size, "QUIT\r\n");
			break;
			
		case css_quit:
			break;
		}
	}
	close(fd);
	free(buf);
}

Return to:

Send suggestions and report system problems to the System administrator.