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.
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
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
42 static char sccsid
[] = "@(#)smtp.c 2.43 (gritter) 8/4/07";
48 #include <sys/utsname.h>
50 # include <sys/socket.h>
52 # include <netinet/in.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.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! */
79 * Return our hostname.
82 nodename(int mayoverride
)
84 static char *hostname
;
88 #ifdef HAVE_IPv6_FUNCS
89 struct addrinfo hints
, *res
;
90 #else /* !HAVE_IPv6_FUNCS */
92 #endif /* !HAVE_IPv6_FUNCS */
93 #endif /* HAVE_SOCKETS */
95 if (mayoverride
&& (hn
= value("hostname")) != NULL
&& *hn
) {
97 hostname
= sstrdup(hn
);
99 if (hostname
== NULL
) {
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
);
114 #else /* !HAVE_IPv6_FUNCS */
115 hent
= gethostbyname(hn
);
119 #endif /* !HAVE_IPv6_FUNCS */
120 #endif /* HAVE_SOCKETS */
121 hostname
= smalloc(strlen(hn
) + 1);
122 strcpy(hostname
, hn
);
128 * Return the user's From: address(es).
131 myaddrs(struct header
*hp
)
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
)
146 * When invoking sendmail directly, it's its task
147 * to generate a From: address.
149 if (value("smtp") == NULL
)
153 sz
= strlen(myname
) + strlen(hn
) + 2;
155 snprintf(addr
, sz
, "%s@%s", myname
, hn
);
161 myorigin(struct header
*hp
)
166 if ((cp
= myaddrs(hp
)) == NULL
||
167 (np
= sextract(cp
, GEXTRA
|GFULL
)) == NULL
)
169 return np
->n_flink
!= NULL
? value("sender") : cp
;
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
);
180 smtp_auth_var(const char *type
, const char *addr
)
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
)
190 snprintf(var
, len
, "smtp-auth%s", type
);
191 if ((cp
= value(var
)) != NULL
)
198 static char *smtpbuf
;
199 static size_t smtpbufsize
;
202 * Get the SMTP server's answer, expecting value.
205 read_smtp(struct sock
*sp
, int value
, int ign_eof
)
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"));
217 if (verbose
|| debug
|| _debug
)
218 fputs(smtpbuf
, stderr
);
220 case '1': ret
= 1; break;
221 case '2': ret
= 2; break;
222 case '3': ret
= 3; break;
223 case '4': ret
= 4; break;
227 fprintf(stderr
, catgets(catd
, CATSET
, 191,
228 "smtp-server: %s"), smtpbuf
);
229 } while (smtpbuf
[3] == '-');
234 * Macros for talk_smtp.
236 #define _SMTP_ANSWER(x, ign_eof) \
237 if (!debug && !_debug) { \
239 if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
240 (!(ign_eof) || y != -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) \
255 * Talk to a SMTP server.
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
)
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;
270 if ((authstr
= smtp_auth_var("", skinned
)) == NULL
)
271 auth
= user
&& password
? AUTH_LOGIN
: AUTH_NONE
;
272 else if (strcmp(authstr
, "plain") == 0)
274 else if (strcmp(authstr
, "login") == 0)
276 else if (strcmp(authstr
, "cram-md5") == 0)
277 auth
= AUTH_CRAM_MD5
;
279 fprintf(stderr
, "Unknown SMTP authentication "
280 "method: \"%s\"\n", authstr
);
283 if (auth
!= AUTH_NONE
&& (user
== NULL
|| password
== NULL
)) {
284 fprintf(stderr
, "User and password are necessary "
285 "for SMTP authentication.\n");
290 if (value("smtp-use-starttls") ||
291 value("smtp-use-tls") /* v11.0 compatibility */) {
293 if ((cp
= strchr(xserver
, ':')) != NULL
) {
294 server
= salloc(cp
- xserver
+ 1);
295 memcpy(server
, xserver
, cp
- xserver
);
296 server
[cp
- xserver
] = '\0';
299 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
302 SMTP_OUT("STARTTLS\r\n");
304 if (!debug
&& !_debug
&& ssl_open(server
, sp
, uhp
) != OKAY
)
308 if (value("smtp-use-starttls") || value("smtp-use-tls")) {
309 fprintf(stderr
, "No SSL support compiled in.\n");
312 #endif /* !USE_SSL */
313 if (auth
!= AUTH_NONE
) {
314 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
320 SMTP_OUT("AUTH LOGIN\r\n");
322 b64
= strtob64(user
);
323 snprintf(o
, sizeof o
, "%s\r\n", b64
);
327 b64
= strtob64(password
);
328 snprintf(o
, sizeof o
, "%s\r\n", b64
);
334 SMTP_OUT("AUTH PLAIN\r\n");
336 snprintf(o
, sizeof o
, "%c%s%c%s", '\0', user
, '\0',
338 b64
= memtob64(o
, strlen(user
)+strlen(password
)+2);
339 snprintf(o
, sizeof o
, "%s\r\n", b64
);
344 SMTP_OUT("AUTH CRAM-MD5\r\n");
346 for (cp
= smtpbuf
; digitchar(*cp
&0377); cp
++);
347 while (blankchar(*cp
&0377)) cp
++;
348 cp
= cram_md5_string(user
, password
, cp
);
354 snprintf(o
, sizeof o
, "HELO %s\r\n", nodename(1));
358 snprintf(o
, sizeof o
, "MAIL FROM:<%s>\r\n", skinned
);
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",
369 SMTP_OUT("DATA\r\n");
374 while (fgetline(&b
, &bsize
, &count
, &blen
, fi
, 1) != NULL
) {
379 } else if (inbcc
&& blankchar(*b
& 0377))
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) {
395 swrite1(sp
, ".", 1, 1);
397 if (debug
|| _debug
) {
398 fprintf(stderr
, ">>> %s", b
);
403 swrite1(sp
, b
, blen
+1, 1);
407 SMTP_OUT("QUIT\r\n");
414 static sigjmp_buf smtpjmp
;
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
)
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
);
442 if (saveterm
!= SIG_IGN
)
443 safe_signal(SIGTERM
, onterm
);
444 if (strncmp(server
, "smtp://", 7) == 0) {
448 } else if (strncmp(server
, "smtps://", 8) == 0) {
454 if (!debug
&& !_debug
&& sopen(server
, &so
, use_ssl
, server
,
455 use_ssl
? "smtps" : "smtp", verbose
) != OKAY
) {
456 safe_signal(SIGTERM
, saveterm
);
460 ret
= talk_smtp(to
, fi
, &so
, server
, server
, hp
,
461 user
, password
, skinned
);
462 if (!debug
&& !_debug
)
469 safe_signal(SIGTERM
, saveterm
);
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
)
484 fputs(catgets(catd
, CATSET
, 194,
485 "No SMTP support compiled in.\n"), stderr
);
488 #endif /* USE_SMTP */