Add enum nameflags and struct name.n_flags field
[s-mailx.git] / smtp.c
blobcbb14ab6d9ea172003c401517ef9ce2752826baf
1 /*
2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 2000
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef lint
41 #ifdef DOSCCS
42 static char sccsid[] = "@(#)smtp.c 2.43 (gritter) 8/4/07";
43 #endif
44 #endif /* not lint */
46 #include "rcv.h"
48 #include <sys/utsname.h>
49 #ifdef HAVE_SOCKETS
50 # include <sys/socket.h>
51 # include <netdb.h>
52 # include <netinet/in.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
55 # endif
56 #endif
57 #include <unistd.h>
58 #include <setjmp.h>
60 #include "extern.h"
61 #include "md5.h"
64 * Mail -- a mail program
66 * SMTP client and other internet related functions.
69 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
70 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
71 * TODO may leave child running if fdopen() fails! */
73 #ifdef HAVE_SOCKETS
74 static int verbose;
75 static int _debug;
76 #endif
79 * Return our hostname.
81 char *
82 nodename(int mayoverride)
84 static char *hostname;
85 char *hn;
86 struct utsname ut;
87 #ifdef HAVE_SOCKETS
88 #ifdef HAVE_IPv6_FUNCS
89 struct addrinfo hints, *res;
90 #else /* !HAVE_IPv6_FUNCS */
91 struct hostent *hent;
92 #endif /* !HAVE_IPv6_FUNCS */
93 #endif /* HAVE_SOCKETS */
95 if (mayoverride && (hn = value("hostname")) != NULL && *hn) {
96 free(hostname);
97 hostname = sstrdup(hn);
99 if (hostname == NULL) {
100 uname(&ut);
101 hn = ut.nodename;
102 #ifdef HAVE_SOCKETS
103 #ifdef HAVE_IPv6_FUNCS
104 memset(&hints, 0, sizeof hints);
105 hints.ai_socktype = SOCK_DGRAM; /* dummy */
106 hints.ai_flags = AI_CANONNAME;
107 if (getaddrinfo(hn, "0", &hints, &res) == 0) {
108 if (res->ai_canonname) {
109 hn = salloc(strlen(res->ai_canonname) + 1);
110 strcpy(hn, res->ai_canonname);
112 freeaddrinfo(res);
114 #else /* !HAVE_IPv6_FUNCS */
115 hent = gethostbyname(hn);
116 if (hent != NULL) {
117 hn = hent->h_name;
119 #endif /* !HAVE_IPv6_FUNCS */
120 #endif /* HAVE_SOCKETS */
121 hostname = smalloc(strlen(hn) + 1);
122 strcpy(hostname, hn);
124 return hostname;
128 * Return the user's From: address(es).
130 char *
131 myaddrs(struct header *hp)
133 char *cp, *hn;
134 static char *addr;
135 size_t sz;
137 if (hp != NULL && hp->h_from != NULL) {
138 if (hp->h_from->n_fullname)
139 return savestr(hp->h_from->n_fullname);
140 if (hp->h_from->n_name)
141 return savestr(hp->h_from->n_name);
143 if ((cp = value("from")) != NULL)
144 return cp;
146 * When invoking sendmail directly, it's its task
147 * to generate a From: address.
149 if (value("smtp") == NULL)
150 return NULL;
151 if (addr == NULL) {
152 hn = nodename(1);
153 sz = strlen(myname) + strlen(hn) + 2;
154 addr = smalloc(sz);
155 snprintf(addr, sz, "%s@%s", myname, hn);
157 return addr;
160 char *
161 myorigin(struct header *hp)
163 char *cp;
164 struct name *np;
166 if ((cp = myaddrs(hp)) == NULL ||
167 (np = sextract(cp, GEXTRA|GFULL)) == NULL)
168 return NULL;
169 return np->n_flink != NULL ? value("sender") : cp;
172 #ifdef USE_SMTP
174 static int read_smtp(struct sock *sp, int value, int ign_eof);
175 static int talk_smtp(struct name *to, FILE *fi, struct sock *sp,
176 char *server, char *uhp, struct header *hp,
177 const char *user, const char *password, const char *skinned);
179 char *
180 smtp_auth_var(const char *type, const char *addr)
182 char *var, *cp;
183 int len;
185 var = ac_alloc(len = strlen(type) + strlen(addr) + 7);
186 snprintf(var, len, "smtp-auth%s-%s", type, addr);
187 if ((cp = value(var)) != NULL)
188 cp = savestr(cp);
189 else {
190 snprintf(var, len, "smtp-auth%s", type);
191 if ((cp = value(var)) != NULL)
192 cp = savestr(cp);
194 ac_free(var);
195 return cp;
198 static char *smtpbuf;
199 static size_t smtpbufsize;
202 * Get the SMTP server's answer, expecting value.
204 static int
205 read_smtp(struct sock *sp, int value, int ign_eof)
207 int ret;
208 int len;
210 do {
211 if ((len = sgetline(&smtpbuf, &smtpbufsize, NULL, sp)) < 6) {
212 if (len >= 0 && !ign_eof)
213 fprintf(stderr, catgets(catd, CATSET, 241,
214 "Unexpected EOF on SMTP connection\n"));
215 return -1;
217 if (verbose || debug || _debug)
218 fputs(smtpbuf, stderr);
219 switch (*smtpbuf) {
220 case '1': ret = 1; break;
221 case '2': ret = 2; break;
222 case '3': ret = 3; break;
223 case '4': ret = 4; break;
224 default: ret = 5;
226 if (value != ret)
227 fprintf(stderr, catgets(catd, CATSET, 191,
228 "smtp-server: %s"), smtpbuf);
229 } while (smtpbuf[3] == '-');
230 return ret;
234 * Macros for talk_smtp.
236 #define _SMTP_ANSWER(x, ign_eof) \
237 if (!debug && !_debug) { \
238 int y; \
239 if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
240 (!(ign_eof) || y != -1)) { \
241 if (b != NULL) \
242 free(b); \
243 return 1; \
247 #define SMTP_ANSWER(x) _SMTP_ANSWER(x, 0)
249 #define SMTP_OUT(x) if (verbose || debug || _debug) \
250 fprintf(stderr, ">>> %s", x); \
251 if (!debug && !_debug) \
252 swrite(sp, x);
255 * Talk to a SMTP server.
257 static int
258 talk_smtp(struct name *to, FILE *fi, struct sock *sp,
259 char *xserver, char *uhp, struct header *hp,
260 const char *user, const char *password, const char *skinned)
262 struct name *n;
263 char *b = NULL, o[LINESIZE];
264 size_t blen, bsize = 0, count;
265 char *b64, *authstr, *cp;
266 enum { AUTH_NONE, AUTH_PLAIN, AUTH_LOGIN, AUTH_CRAM_MD5 } auth;
267 int inhdr = 1, inbcc = 0;
268 (void)hp;
270 if ((authstr = smtp_auth_var("", skinned)) == NULL)
271 auth = user && password ? AUTH_LOGIN : AUTH_NONE;
272 else if (strcmp(authstr, "plain") == 0)
273 auth = AUTH_PLAIN;
274 else if (strcmp(authstr, "login") == 0)
275 auth = AUTH_LOGIN;
276 else if (strcmp(authstr, "cram-md5") == 0)
277 auth = AUTH_CRAM_MD5;
278 else {
279 fprintf(stderr, "Unknown SMTP authentication "
280 "method: \"%s\"\n", authstr);
281 return 1;
283 if (auth != AUTH_NONE && (user == NULL || password == NULL)) {
284 fprintf(stderr, "User and password are necessary "
285 "for SMTP authentication.\n");
286 return 1;
288 SMTP_ANSWER(2);
289 #ifdef USE_SSL
290 if (value("smtp-use-starttls") ||
291 value("smtp-use-tls") /* v11.0 compatibility */) {
292 char *server;
293 if ((cp = strchr(xserver, ':')) != NULL) {
294 server = salloc(cp - xserver + 1);
295 memcpy(server, xserver, cp - xserver);
296 server[cp - xserver] = '\0';
297 } else
298 server = xserver;
299 snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
300 SMTP_OUT(o);
301 SMTP_ANSWER(2);
302 SMTP_OUT("STARTTLS\r\n");
303 SMTP_ANSWER(2);
304 if (!debug && !_debug && ssl_open(server, sp, uhp) != OKAY)
305 return 1;
307 #else /* !USE_SSL */
308 if (value("smtp-use-starttls") || value("smtp-use-tls")) {
309 fprintf(stderr, "No SSL support compiled in.\n");
310 return 1;
312 #endif /* !USE_SSL */
313 if (auth != AUTH_NONE) {
314 snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
315 SMTP_OUT(o);
316 SMTP_ANSWER(2);
317 switch (auth) {
318 default:
319 case AUTH_LOGIN:
320 SMTP_OUT("AUTH LOGIN\r\n");
321 SMTP_ANSWER(3);
322 b64 = strtob64(user);
323 snprintf(o, sizeof o, "%s\r\n", b64);
324 free(b64);
325 SMTP_OUT(o);
326 SMTP_ANSWER(3);
327 b64 = strtob64(password);
328 snprintf(o, sizeof o, "%s\r\n", b64);
329 free(b64);
330 SMTP_OUT(o);
331 SMTP_ANSWER(2);
332 break;
333 case AUTH_PLAIN:
334 SMTP_OUT("AUTH PLAIN\r\n");
335 SMTP_ANSWER(3);
336 snprintf(o, sizeof o, "%c%s%c%s", '\0', user, '\0',
337 password);
338 b64 = memtob64(o, strlen(user)+strlen(password)+2);
339 snprintf(o, sizeof o, "%s\r\n", b64);
340 SMTP_OUT(o);
341 SMTP_ANSWER(2);
342 break;
343 case AUTH_CRAM_MD5:
344 SMTP_OUT("AUTH CRAM-MD5\r\n");
345 SMTP_ANSWER(3);
346 for (cp = smtpbuf; digitchar(*cp&0377); cp++);
347 while (blankchar(*cp&0377)) cp++;
348 cp = cram_md5_string(user, password, cp);
349 SMTP_OUT(cp);
350 SMTP_ANSWER(2);
351 break;
353 } else {
354 snprintf(o, sizeof o, "HELO %s\r\n", nodename(1));
355 SMTP_OUT(o);
356 SMTP_ANSWER(2);
358 snprintf(o, sizeof o, "MAIL FROM:<%s>\r\n", skinned);
359 SMTP_OUT(o);
360 SMTP_ANSWER(2);
361 for (n = to; n != NULL; n = n->n_flink) {
362 if ((n->n_type & GDEL) == 0) {
363 snprintf(o, sizeof o, "RCPT TO:<%s>\r\n",
364 skin(n->n_name));
365 SMTP_OUT(o);
366 SMTP_ANSWER(2);
369 SMTP_OUT("DATA\r\n");
370 SMTP_ANSWER(3);
371 fflush(fi);
372 rewind(fi);
373 count = fsize(fi);
374 while (fgetline(&b, &bsize, &count, &blen, fi, 1) != NULL) {
375 if (inhdr) {
376 if (*b == '\n') {
377 inhdr = 0;
378 inbcc = 0;
379 } else if (inbcc && blankchar(*b & 0377))
380 continue;
382 * We know what we have generated first, so
383 * do not look for whitespace before the ':'.
385 else if (ascncasecmp(b, "bcc: ", 5) == 0) {
386 inbcc = 1;
387 continue;
388 } else
389 inbcc = 0;
391 if (*b == '.') {
392 if (debug || _debug)
393 putc('.', stderr);
394 else
395 swrite1(sp, ".", 1, 1);
397 if (debug || _debug) {
398 fprintf(stderr, ">>> %s", b);
399 continue;
401 b[blen-1] = '\r';
402 b[blen] = '\n';
403 swrite1(sp, b, blen+1, 1);
405 SMTP_OUT(".\r\n");
406 SMTP_ANSWER(2);
407 SMTP_OUT("QUIT\r\n");
408 _SMTP_ANSWER(2, 1);
409 if (b != NULL)
410 free(b);
411 return 0;
414 static sigjmp_buf smtpjmp;
416 static void
417 onterm(int signo)
419 (void)signo;
420 siglongjmp(smtpjmp, 1);
424 * Connect to a SMTP server.
427 smtp_mta(char *server, struct name *to, FILE *fi, struct header *hp,
428 const char *user, const char *password, const char *skinned)
430 struct sock so;
431 int use_ssl, ret;
432 sighandler_type saveterm;
434 memset(&so, 0, sizeof so);
435 verbose = value("verbose") != NULL;
436 _debug = value("debug") != NULL;
437 saveterm = safe_signal(SIGTERM, SIG_IGN);
438 if (sigsetjmp(smtpjmp, 1)) {
439 safe_signal(SIGTERM, saveterm);
440 return 1;
442 if (saveterm != SIG_IGN)
443 safe_signal(SIGTERM, onterm);
444 if (strncmp(server, "smtp://", 7) == 0) {
445 use_ssl = 0;
446 server += 7;
447 #ifdef USE_SSL
448 } else if (strncmp(server, "smtps://", 8) == 0) {
449 use_ssl = 1;
450 server += 8;
451 #endif
452 } else
453 use_ssl = 0;
454 if (!debug && !_debug && sopen(server, &so, use_ssl, server,
455 use_ssl ? "smtps" : "smtp", verbose) != OKAY) {
456 safe_signal(SIGTERM, saveterm);
457 return 1;
459 so.s_desc = "SMTP";
460 ret = talk_smtp(to, fi, &so, server, server, hp,
461 user, password, skinned);
462 if (!debug && !_debug)
463 sclose(&so);
464 if (smtpbuf) {
465 free(smtpbuf);
466 smtpbuf = NULL;
467 smtpbufsize = 0;
469 safe_signal(SIGTERM, saveterm);
470 return ret;
472 #else /* !USE_SMTP */
474 smtp_mta(char *server, struct name *to, FILE *fi, struct header *hp,
475 const char *user, const char *password, const char *skinned)
477 (void)server;
478 (void)to;
479 (void)fi;
480 (void)hp;
481 (void)user;
482 (void)password;
483 (void)skinned;
484 fputs(catgets(catd, CATSET, 194,
485 "No SMTP support compiled in.\n"), stderr);
486 return 1;
488 #endif /* USE_SMTP */