outof(): Mail NULL fix..
[s-mailx.git] / smtp.c
blob81e96e1760d8837251531732e45f85476172a3ae
1 /*
2 * S-nail - 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 #include "rcv.h"
42 #include <sys/utsname.h>
43 #ifdef HAVE_SOCKETS
44 # include <sys/socket.h>
45 # include <netdb.h>
46 # include <netinet/in.h>
47 # ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
49 # endif
50 #endif
51 #include <unistd.h>
52 #include <setjmp.h>
54 #include "extern.h"
56 #ifdef USE_MD5
57 # include "md5.h"
58 #endif
61 * Mail -- a mail program
63 * SMTP client and other internet related functions.
66 #ifdef USE_SMTP
67 static int verbose;
68 static int _debug;
69 #endif
72 * Return our hostname.
74 char *
75 nodename(int mayoverride)
77 static char *hostname;
78 char *hn;
79 struct utsname ut;
80 #ifdef HAVE_SOCKETS
81 # ifdef USE_IPV6
82 struct addrinfo hints, *res;
83 # else
84 struct hostent *hent;
85 # endif
86 #endif
88 if (mayoverride && (hn = value("hostname")) != NULL && *hn) {
89 free(hostname);
90 hostname = sstrdup(hn);
92 if (hostname == NULL) {
93 uname(&ut);
94 hn = ut.nodename;
95 #ifdef HAVE_SOCKETS
96 # ifdef USE_IPV6
97 memset(&hints, 0, sizeof hints);
98 hints.ai_socktype = SOCK_DGRAM; /* dummy */
99 hints.ai_flags = AI_CANONNAME;
100 if (getaddrinfo(hn, "0", &hints, &res) == 0) {
101 if (res->ai_canonname) {
102 hn = salloc(strlen(res->ai_canonname) + 1);
103 strcpy(hn, res->ai_canonname);
105 freeaddrinfo(res);
107 # else
108 hent = gethostbyname(hn);
109 if (hent != NULL) {
110 hn = hent->h_name;
112 # endif
113 #endif
114 hostname = smalloc(strlen(hn) + 1);
115 strcpy(hostname, hn);
117 return hostname;
121 * Return the user's From: address(es).
123 char *
124 myaddrs(struct header *hp)
126 char *cp, *hn;
127 static char *addr;
128 size_t sz;
130 if (hp != NULL && hp->h_from != NULL) {
131 if (hp->h_from->n_fullname)
132 return savestr(hp->h_from->n_fullname);
133 if (hp->h_from->n_name)
134 return savestr(hp->h_from->n_name);
136 if ((cp = value("from")) != NULL)
137 return cp;
139 * When invoking sendmail directly, it's its task
140 * to generate a From: address.
142 if (value("smtp") == NULL)
143 return NULL;
144 if (addr == NULL) {
145 hn = nodename(1);
146 sz = strlen(myname) + strlen(hn) + 2;
147 addr = smalloc(sz);
148 snprintf(addr, sz, "%s@%s", myname, hn);
150 return addr;
153 char *
154 myorigin(struct header *hp)
156 char *cp;
157 struct name *np;
159 if ((cp = myaddrs(hp)) == NULL ||
160 (np = sextract(cp, GEXTRA|GFULL)) == NULL)
161 return NULL;
162 return np->n_flink != NULL ? value("sender") : cp;
165 #ifdef USE_SMTP
167 static int read_smtp(struct sock *sp, int value, int ign_eof);
168 static int talk_smtp(struct name *to, FILE *fi, struct sock *sp,
169 char *server, char *uhp, struct header *hp,
170 const char *user, const char *password, const char *skinned);
172 char *
173 smtp_auth_var(const char *type, const char *addr)
175 char *var, *cp;
176 int len;
178 var = ac_alloc(len = strlen(type) + strlen(addr) + 7);
179 snprintf(var, len, "smtp-auth%s-%s", type, addr);
180 if ((cp = value(var)) != NULL)
181 cp = savestr(cp);
182 else {
183 snprintf(var, len, "smtp-auth%s", type);
184 if ((cp = value(var)) != NULL)
185 cp = savestr(cp);
187 ac_free(var);
188 return cp;
191 static char *smtpbuf;
192 static size_t smtpbufsize;
195 * Get the SMTP server's answer, expecting value.
197 static int
198 read_smtp(struct sock *sp, int value, int ign_eof)
200 int ret;
201 int len;
203 do {
204 if ((len = sgetline(&smtpbuf, &smtpbufsize, NULL, sp)) < 6) {
205 if (len >= 0 && !ign_eof)
206 fprintf(stderr, catgets(catd, CATSET, 241,
207 "Unexpected EOF on SMTP connection\n"));
208 return -1;
210 if (verbose || debug || _debug)
211 fputs(smtpbuf, stderr);
212 switch (*smtpbuf) {
213 case '1': ret = 1; break;
214 case '2': ret = 2; break;
215 case '3': ret = 3; break;
216 case '4': ret = 4; break;
217 default: ret = 5;
219 if (value != ret)
220 fprintf(stderr, catgets(catd, CATSET, 191,
221 "smtp-server: %s"), smtpbuf);
222 } while (smtpbuf[3] == '-');
223 return ret;
227 * Macros for talk_smtp.
229 #define _SMTP_ANSWER(x, ign_eof) \
230 if (!debug && !_debug) { \
231 int y; \
232 if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
233 (!(ign_eof) || y != -1)) { \
234 if (b != NULL) \
235 free(b); \
236 return 1; \
240 #define SMTP_ANSWER(x) _SMTP_ANSWER(x, 0)
242 #define SMTP_OUT(x) if (verbose || debug || _debug) \
243 fprintf(stderr, ">>> %s", x); \
244 if (!debug && !_debug) \
245 swrite(sp, x);
248 * Talk to a SMTP server.
250 static int
251 talk_smtp(struct name *to, FILE *fi, struct sock *sp,
252 char *xserver, char *uhp, struct header *hp,
253 const char *user, const char *password, const char *skinned)
255 struct name *n;
256 char *b = NULL, o[LINESIZE];
257 size_t blen, bsize = 0, count;
258 char *b64, *authstr, *cp;
259 enum { AUTH_NONE, AUTH_PLAIN, AUTH_LOGIN, AUTH_CRAM_MD5 } auth;
260 int inhdr = 1, inbcc = 0;
261 (void)hp;
263 if ((authstr = smtp_auth_var("", skinned)) == NULL)
264 auth = user && password ? AUTH_LOGIN : AUTH_NONE;
265 else if (strcmp(authstr, "plain") == 0)
266 auth = AUTH_PLAIN;
267 else if (strcmp(authstr, "login") == 0)
268 auth = AUTH_LOGIN;
269 else if (strcmp(authstr, "cram-md5") == 0) {
270 #ifdef USE_MD5
271 auth = AUTH_CRAM_MD5;
272 #else
273 fprintf(stderr, tr(277, "No CRAM-MD5 support compiled in.\n"));
274 return (1);
275 #endif
276 } else {
277 fprintf(stderr, tr(274,
278 "Unknown SMTP authentication method: %s\n"), authstr);
279 return 1;
281 if (auth != AUTH_NONE && (user == NULL || password == NULL)) {
282 fprintf(stderr, tr(275,
283 "User and password are necessary "
284 "for SMTP authentication.\n"));
285 return 1;
287 SMTP_ANSWER(2);
288 #ifdef USE_SSL
289 if (value("smtp-use-starttls") ||
290 value("smtp-use-tls") /* v11.0 compatibility */) {
291 char *server;
292 if ((cp = strchr(xserver, ':')) != NULL) {
293 server = salloc(cp - xserver + 1);
294 memcpy(server, xserver, cp - xserver);
295 server[cp - xserver] = '\0';
296 } else
297 server = xserver;
298 snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
299 SMTP_OUT(o);
300 SMTP_ANSWER(2);
301 SMTP_OUT("STARTTLS\r\n");
302 SMTP_ANSWER(2);
303 if (!debug && !_debug && ssl_open(server, sp, uhp) != OKAY)
304 return 1;
306 #else /* !USE_SSL */
307 if (value("smtp-use-starttls") || value("smtp-use-tls")) {
308 fprintf(stderr, tr(225, "No SSL support compiled in.\n"));
309 return 1;
311 #endif /* !USE_SSL */
312 if (auth != AUTH_NONE) {
313 snprintf(o, sizeof o, "EHLO %s\r\n", nodename(1));
314 SMTP_OUT(o);
315 SMTP_ANSWER(2);
316 switch (auth) {
317 default:
318 case AUTH_LOGIN:
319 SMTP_OUT("AUTH LOGIN\r\n");
320 SMTP_ANSWER(3);
321 b64 = strtob64(user);
322 snprintf(o, sizeof o, "%s\r\n", b64);
323 free(b64);
324 SMTP_OUT(o);
325 SMTP_ANSWER(3);
326 b64 = strtob64(password);
327 snprintf(o, sizeof o, "%s\r\n", b64);
328 free(b64);
329 SMTP_OUT(o);
330 SMTP_ANSWER(2);
331 break;
332 case AUTH_PLAIN:
333 SMTP_OUT("AUTH PLAIN\r\n");
334 SMTP_ANSWER(3);
335 snprintf(o, sizeof o, "%c%s%c%s", '\0', user, '\0',
336 password);
337 b64 = memtob64(o, strlen(user)+strlen(password)+2);
338 snprintf(o, sizeof o, "%s\r\n", b64);
339 SMTP_OUT(o);
340 SMTP_ANSWER(2);
341 break;
342 #ifdef USE_MD5
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;
352 #endif
354 } else {
355 snprintf(o, sizeof o, "HELO %s\r\n", nodename(1));
356 SMTP_OUT(o);
357 SMTP_ANSWER(2);
359 snprintf(o, sizeof o, "MAIL FROM:<%s>\r\n", skinned);
360 SMTP_OUT(o);
361 SMTP_ANSWER(2);
362 for (n = to; n != NULL; n = n->n_flink) {
363 if ((n->n_type & GDEL) == 0) {
364 snprintf(o, sizeof o, "RCPT TO:<%s>\r\n",
365 skinned_name(n));
366 SMTP_OUT(o);
367 SMTP_ANSWER(2);
370 SMTP_OUT("DATA\r\n");
371 SMTP_ANSWER(3);
372 fflush(fi);
373 rewind(fi);
374 count = fsize(fi);
375 while (fgetline(&b, &bsize, &count, &blen, fi, 1) != NULL) {
376 if (inhdr) {
377 if (*b == '\n') {
378 inhdr = 0;
379 inbcc = 0;
380 } else if (inbcc && blankchar(*b & 0377))
381 continue;
383 * We know what we have generated first, so
384 * do not look for whitespace before the ':'.
386 else if (ascncasecmp(b, "bcc: ", 5) == 0) {
387 inbcc = 1;
388 continue;
389 } else
390 inbcc = 0;
392 if (*b == '.') {
393 if (debug || _debug)
394 putc('.', stderr);
395 else
396 swrite1(sp, ".", 1, 1);
398 if (debug || _debug) {
399 fprintf(stderr, ">>> %s", b);
400 continue;
402 b[blen-1] = '\r';
403 b[blen] = '\n';
404 swrite1(sp, b, blen+1, 1);
406 SMTP_OUT(".\r\n");
407 SMTP_ANSWER(2);
408 SMTP_OUT("QUIT\r\n");
409 _SMTP_ANSWER(2, 1);
410 if (b != NULL)
411 free(b);
412 return 0;
415 static sigjmp_buf smtpjmp;
417 static void
418 onterm(int signo)
420 (void)signo;
421 siglongjmp(smtpjmp, 1);
425 * Connect to a SMTP server.
428 smtp_mta(char *volatile server, struct name *volatile to, FILE *fi,
429 struct header *hp, const char *user, const char *password,
430 const char *skinned)
432 struct sock so;
433 int use_ssl, ret;
434 sighandler_type saveterm;
436 memset(&so, 0, sizeof so);
437 verbose = value("verbose") != NULL;
438 _debug = value("debug") != NULL;
439 saveterm = safe_signal(SIGTERM, SIG_IGN);
440 if (sigsetjmp(smtpjmp, 1)) {
441 safe_signal(SIGTERM, saveterm);
442 return 1;
444 if (saveterm != SIG_IGN)
445 safe_signal(SIGTERM, onterm);
446 if (strncmp(server, "smtp://", 7) == 0) {
447 use_ssl = 0;
448 server += 7;
449 #ifdef USE_SSL
450 } else if (strncmp(server, "smtps://", 8) == 0) {
451 use_ssl = 1;
452 server += 8;
453 #endif
454 } else
455 use_ssl = 0;
456 if (!debug && !_debug && sopen(server, &so, use_ssl, server,
457 use_ssl ? "smtps" : "smtp", verbose) != OKAY) {
458 safe_signal(SIGTERM, saveterm);
459 return 1;
461 so.s_desc = "SMTP";
462 ret = talk_smtp(to, fi, &so, server, server, hp,
463 user, password, skinned);
464 if (!debug && !_debug)
465 sclose(&so);
466 if (smtpbuf) {
467 free(smtpbuf);
468 smtpbuf = NULL;
469 smtpbufsize = 0;
471 safe_signal(SIGTERM, saveterm);
472 return ret;
474 #else /* !USE_SMTP */
476 smtp_mta(char *server, struct name *to, FILE *fi, struct header *hp,
477 const char *user, const char *password, const char *skinned)
479 (void)server;
480 (void)to;
481 (void)fi;
482 (void)hp;
483 (void)user;
484 (void)password;
485 (void)skinned;
486 fputs(catgets(catd, CATSET, 194,
487 "No SMTP support compiled in.\n"), stderr);
488 return 1;
490 #endif /* USE_SMTP */