File: smtp.c

package info (click to toggle)
heirloom-mailx 12.4-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,524 kB
  • ctags: 1,960
  • sloc: ansic: 29,443; sh: 333; makefile: 126
file content (474 lines) | stat: -rw-r--r-- 11,748 bytes parent folder | download | duplicates (5)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
 *
 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
 */
/*
 * Copyright (c) 2000
 *	Gunnar Ritter.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Gunnar Ritter
 *	and his contributors.
 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
#ifdef	DOSCCS
static char sccsid[] = "@(#)smtp.c	2.43 (gritter) 8/4/07";
#endif
#endif /* not lint */

#include "rcv.h"

#include <sys/utsname.h>
#ifdef	HAVE_SOCKETS
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#ifdef	HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif	/* HAVE_ARPA_INET_H */
#endif	/* HAVE_SOCKETS */
#include <unistd.h>
#include <setjmp.h>

#include "extern.h"
#include "md5.h"

/*
 * Mail -- a mail program
 *
 * SMTP client and other internet related functions.
 */

#ifdef	HAVE_SOCKETS
static int verbose;
static int _debug;
#endif

/*
 * Return our hostname.
 */
char *
nodename(int mayoverride)
{
	static char *hostname;
	char *hn;
        struct utsname ut;
#ifdef	HAVE_SOCKETS
#ifdef	HAVE_IPv6_FUNCS
	struct addrinfo hints, *res;
#else	/* !HAVE_IPv6_FUNCS */
        struct hostent *hent;
#endif	/* !HAVE_IPv6_FUNCS */
#endif	/* HAVE_SOCKETS */

	if (mayoverride && (hn = value("hostname")) != NULL && *hn) {
		free(hostname);
		hostname = sstrdup(hn);
	}
	if (hostname == NULL) {
		uname(&ut);
		hn = ut.nodename;
#ifdef	HAVE_SOCKETS
#ifdef	HAVE_IPv6_FUNCS
		memset(&hints, 0, sizeof hints);
		hints.ai_socktype = SOCK_DGRAM;	/* dummy */
		hints.ai_flags = AI_CANONNAME;
		if (getaddrinfo(hn, "0", &hints, &res) == 0) {
			if (res->ai_canonname) {
				hn = salloc(strlen(res->ai_canonname) + 1);
				strcpy(hn, res->ai_canonname);
			}
			freeaddrinfo(res);
		}
#else	/* !HAVE_IPv6_FUNCS */
		hent = gethostbyname(hn);
		if (hent != NULL) {
			hn = hent->h_name;
		}
#endif	/* !HAVE_IPv6_FUNCS */
#endif	/* HAVE_SOCKETS */
		hostname = smalloc(strlen(hn) + 1);
		strcpy(hostname, hn);
	}
	return hostname;
}

/*
 * Return the user's From: address(es).
 */
char *
myaddrs(struct header *hp)
{
	char *cp, *hn;
	static char *addr;
	size_t sz;

	if (hp != NULL && hp->h_from != NULL) {
		if (hp->h_from->n_fullname)
			return savestr(hp->h_from->n_fullname);
		if (hp->h_from->n_name)
			return savestr(hp->h_from->n_name);
	}
	if ((cp = value("from")) != NULL)
		return cp;
	/*
	 * When invoking sendmail directly, it's its task
	 * to generate a From: address.
	 */
	if (value("smtp") == NULL)
		return NULL;
	if (addr == NULL) {
		hn = nodename(1);
		sz = strlen(myname) + strlen(hn) + 2;
		addr = smalloc(sz);
		snprintf(addr, sz, "%s@%s", myname, hn);
	}
	return addr;
}

char *
myorigin(struct header *hp)
{
	char	*cp;
	struct name	*np;

	if ((cp = myaddrs(hp)) == NULL ||
			(np = sextract(cp, GEXTRA|GFULL)) == NULL)
		return NULL;
	return np->n_flink != NULL ? value("sender") : cp;
}

#ifdef	HAVE_SOCKETS

static int read_smtp(struct sock *sp, int value, int ign_eof);
static int talk_smtp(struct name *to, FILE *fi, struct sock *sp,
		char *server, char *uhp, struct header *hp,
		const char *user, const char *password, const char *skinned);

char *
smtp_auth_var(const char *type, const char *addr)
{
	char	*var, *cp;
	int	len;

	var = ac_alloc(len = strlen(type) + strlen(addr) + 7);
	snprintf(var, len, "smtp-auth%s-%s", type, addr);
	if ((cp = value(var)) != NULL)
		cp = savestr(cp);
	else {
		snprintf(var, len, "smtp-auth%s", type);
		if ((cp = value(var)) != NULL)
			cp = savestr(cp);
	}
	ac_free(var);
	return cp;
}

static char	*smtpbuf;
static size_t	smtpbufsize;

/*
 * Get the SMTP server's answer, expecting value.
 */
static int 
read_smtp(struct sock *sp, int value, int ign_eof)
{
	int ret;
	int len;

	do {
		if ((len = sgetline(&smtpbuf, &smtpbufsize, NULL, sp)) < 6) {
			if (len >= 0 && !ign_eof)
				fprintf(stderr, catgets(catd, CATSET, 241,
					"Unexpected EOF on SMTP connection\n"));
			return -1;
		}
		if (verbose || debug || _debug)
			fputs(smtpbuf, stderr);
		switch (*smtpbuf) {
		case '1': ret = 1; break;
		case '2': ret = 2; break;
		case '3': ret = 3; break;
		case '4': ret = 4; break;
		default: ret = 5;
		}
		if (value != ret)
			fprintf(stderr, catgets(catd, CATSET, 191,
					"smtp-server: %s"), smtpbuf);
	} while (smtpbuf[3] == '-');
	return ret;
}

/*
 * Macros for talk_smtp.
 */
#define	_SMTP_ANSWER(x, ign_eof)	\
			if (!debug && !_debug) { \
				int	y; \
				if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
					(!(ign_eof) || y != -1)) { \
					if (b != NULL) \
						free(b); \
					return 1; \
				} \
			}

#define	SMTP_ANSWER(x)	_SMTP_ANSWER(x, 0)

#define	SMTP_OUT(x)	if (verbose || debug || _debug) \
				fprintf(stderr, ">>> %s", x); \
			if (!debug && !_debug) \
				swrite(sp, x);

/*
 * Talk to a SMTP server.
 */
static int
talk_smtp(struct name *to, FILE *fi, struct sock *sp,
		char *xserver, char *uhp, struct header *hp,
		const char *user, const char *password, const char *skinned)
{
	struct name *n;
	char *b = NULL, o[LINESIZE];
	size_t blen, bsize = 0, count;
	char	*b64, *authstr, *cp;
	enum	{ AUTH_NONE, AUTH_PLAIN, AUTH_LOGIN, AUTH_CRAM_MD5 } auth;
	int	inhdr = 1, inbcc = 0;

	if ((authstr = smtp_auth_var("", skinned)) == NULL)
		auth = user && password ? AUTH_LOGIN : AUTH_NONE;
	else if (strcmp(authstr, "plain") == 0)
		auth = AUTH_PLAIN;
	else if (strcmp(authstr, "login") == 0)
		auth = AUTH_LOGIN;
	else if (strcmp(authstr, "cram-md5") == 0)
		auth = AUTH_CRAM_MD5;
	else {
		fprintf(stderr, "Unknown SMTP authentication "
				"method: \"%s\"\n", authstr);
		return 1;
	}
	if (auth != AUTH_NONE && (user == NULL || password == NULL)) {
		fprintf(stderr, "User and password are necessary "
				"for SMTP authentication.\n");
		return 1;
	}
	SMTP_ANSWER(2);
#ifdef	USE_SSL
	if (value("smtp-use-starttls") ||
			value("smtp-use-tls") /* v11.0 compatibility */) {
		char	*server;
		if ((cp = strchr(xserver, ':')) != NULL) {
			server = salloc(cp - xserver + 1);
			memcpy(server, xserver, cp - xserver);
			server[cp - xserver] = '\0';
		} else
			server = xserver;
		snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
		SMTP_OUT(o);
		SMTP_ANSWER(2);
		SMTP_OUT("STARTTLS\r\n");
		SMTP_ANSWER(2);
		if (!debug && !_debug && ssl_open(server, sp, uhp) != OKAY)
			return 1;
	}
#else	/* !USE_SSL */
	if (value("smtp-use-starttls") || value("smtp-use-tls")) {
		fprintf(stderr, "No SSL support compiled in.\n");
		return 1;
	}
#endif	/* !USE_SSL */
	if (auth != AUTH_NONE) {
		snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
		SMTP_OUT(o);
		SMTP_ANSWER(2);
		switch (auth) {
		default:
		case AUTH_LOGIN:
			SMTP_OUT("AUTH LOGIN\r\n");
			SMTP_ANSWER(3);
			b64 = strtob64(user);
			snprintf(o, sizeof o, "%s\r\n", b64);
			free(b64);
			SMTP_OUT(o);
			SMTP_ANSWER(3);
			b64 = strtob64(password);
			snprintf(o, sizeof o, "%s\r\n", b64);
			free(b64);
			SMTP_OUT(o);
			SMTP_ANSWER(2);
			break;
		case AUTH_PLAIN:
			SMTP_OUT("AUTH PLAIN\r\n");
			SMTP_ANSWER(3);
			snprintf(o, sizeof o, "%c%s%c%s", '\0', user, '\0',
							  password);
			b64 = memtob64(o, strlen(user)+strlen(password)+2);
			snprintf(o, sizeof o, "%s\r\n", b64);
			SMTP_OUT(o);
			SMTP_ANSWER(2);
			break;
		case AUTH_CRAM_MD5:
			SMTP_OUT("AUTH CRAM-MD5\r\n");
			SMTP_ANSWER(3);
			for (cp = smtpbuf; digitchar(*cp&0377); cp++);
			while (blankchar(*cp&0377)) cp++;
			cp = cram_md5_string(user, password, cp);
			SMTP_OUT(cp);
			SMTP_ANSWER(2);
			break;
		}
	} else {
		snprintf(o, sizeof o, "HELO %s\r\n", nodename(1));
		SMTP_OUT(o);
		SMTP_ANSWER(2);
	}
	snprintf(o, sizeof o, "MAIL FROM:<%s>\r\n", skinned);
	SMTP_OUT(o);
	SMTP_ANSWER(2);
	for (n = to; n != NULL; n = n->n_flink) {
		if ((n->n_type & GDEL) == 0) {
			snprintf(o, sizeof o, "RCPT TO:<%s>\r\n",
					skin(n->n_name));
			SMTP_OUT(o);
			SMTP_ANSWER(2);
		}
	}
	SMTP_OUT("DATA\r\n");
	SMTP_ANSWER(3);
	fflush(fi);
	rewind(fi);
	count = fsize(fi);
	while (fgetline(&b, &bsize, &count, &blen, fi, 1) != NULL) {
		if (inhdr) {
			if (*b == '\n') {
				inhdr = 0;
				inbcc = 0;
			} else if (inbcc && blankchar(*b & 0377))
				continue;
			/*
			 * We know what we have generated first, so
			 * do not look for whitespace before the ':'.
			 */
			else if (ascncasecmp(b, "bcc: ", 5) == 0) {
				inbcc = 1;
				continue;
			} else
				inbcc = 0;
		}
		if (*b == '.') {
			if (debug || _debug)
				putc('.', stderr);
			else
				swrite1(sp, ".", 1, 1);
		}
		if (debug || _debug) {
			fprintf(stderr, ">>> %s", b);
			continue;
		}
		b[blen-1] = '\r';
		b[blen] = '\n';
		swrite1(sp, b, blen+1, 1);
	}
	SMTP_OUT(".\r\n");
	SMTP_ANSWER(2);
	SMTP_OUT("QUIT\r\n");
	_SMTP_ANSWER(2, 1);
	if (b != NULL)
		free(b);
	return 0;
}

static sigjmp_buf	smtpjmp;

static void
onterm(int signo)
{
	siglongjmp(smtpjmp, 1);
}

/*
 * Connect to a SMTP server.
 */
int
smtp_mta(char *server, struct name *to, FILE *fi, struct header *hp,
		const char *user, const char *password, const char *skinned)
{
	struct sock	so;
	int	use_ssl, ret;
	sighandler_type	saveterm;

	memset(&so, 0, sizeof so);
	verbose = value("verbose") != NULL;
	_debug = value("debug") != NULL;
	saveterm = safe_signal(SIGTERM, SIG_IGN);
	if (sigsetjmp(smtpjmp, 1)) {
		safe_signal(SIGTERM, saveterm);
		return 1;
	}
	if (saveterm != SIG_IGN)
		safe_signal(SIGTERM, onterm);
	if (strncmp(server, "smtp://", 7) == 0) {
		use_ssl = 0;
		server += 7;
#ifdef	USE_SSL
	} else if (strncmp(server, "smtps://", 8) == 0) {
		use_ssl = 1;
		server += 8;
#endif
	} else
		use_ssl = 0;
	if (!debug && !_debug && sopen(server, &so, use_ssl, server,
				use_ssl ? "smtps" : "smtp", verbose) != OKAY) {
		safe_signal(SIGTERM, saveterm);
		return 1;
	}
	so.s_desc = "SMTP";
	ret = talk_smtp(to, fi, &so, server, server, hp,
			user, password, skinned);
	if (!debug && !_debug)
		sclose(&so);
	if (smtpbuf) {
		free(smtpbuf);
		smtpbuf = NULL;
		smtpbufsize = 0;
	}
	safe_signal(SIGTERM, saveterm);
	return ret;
}
#else	/* !HAVE_SOCKETS */
int
smtp_mta(char *server, struct name *to, FILE *fi, struct header *hp,
		const char *user, const char *password, const char *skinned)
{
	fputs(catgets(catd, CATSET, 194,
			"No SMTP support compiled in.\n"), stderr);
	return 1;
}
#endif	/* !HAVE_SOCKETS */