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.
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 #include <sys/utsname.h>
44 # include <sys/socket.h>
46 # include <netinet/in.h>
47 # ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
61 * Mail -- a mail program
63 * SMTP client and other internet related functions.
72 * Return our hostname.
75 nodename(int mayoverride
)
77 static char *hostname
;
82 struct addrinfo hints
, *res
;
88 if (mayoverride
&& (hn
= value("hostname")) != NULL
&& *hn
) {
90 hostname
= sstrdup(hn
);
92 if (hostname
== NULL
) {
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
);
108 hent
= gethostbyname(hn
);
114 hostname
= smalloc(strlen(hn
) + 1);
115 strcpy(hostname
, hn
);
121 * Return the user's From: address(es).
124 myaddrs(struct header
*hp
)
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
)
139 * When invoking sendmail directly, it's its task
140 * to generate a From: address.
142 if (value("smtp") == NULL
)
146 sz
= strlen(myname
) + strlen(hn
) + 2;
148 snprintf(addr
, sz
, "%s@%s", myname
, hn
);
154 myorigin(struct header
*hp
)
159 if ((cp
= myaddrs(hp
)) == NULL
||
160 (np
= sextract(cp
, GEXTRA
|GFULL
)) == NULL
)
162 return np
->n_flink
!= NULL
? value("sender") : cp
;
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
);
173 smtp_auth_var(const char *type
, const char *addr
)
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
)
183 snprintf(var
, len
, "smtp-auth%s", type
);
184 if ((cp
= value(var
)) != NULL
)
191 static char *smtpbuf
;
192 static size_t smtpbufsize
;
195 * Get the SMTP server's answer, expecting value.
198 read_smtp(struct sock
*sp
, int value
, int ign_eof
)
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"));
210 if (verbose
|| debug
|| _debug
)
211 fputs(smtpbuf
, stderr
);
213 case '1': ret
= 1; break;
214 case '2': ret
= 2; break;
215 case '3': ret
= 3; break;
216 case '4': ret
= 4; break;
220 fprintf(stderr
, catgets(catd
, CATSET
, 191,
221 "smtp-server: %s"), smtpbuf
);
222 } while (smtpbuf
[3] == '-');
227 * Macros for talk_smtp.
229 #define _SMTP_ANSWER(x, ign_eof) \
230 if (!debug && !_debug) { \
232 if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
233 (!(ign_eof) || y != -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) \
248 * Talk to a SMTP server.
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
)
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;
263 if ((authstr
= smtp_auth_var("", skinned
)) == NULL
)
264 auth
= user
&& password
? AUTH_LOGIN
: AUTH_NONE
;
265 else if (strcmp(authstr
, "plain") == 0)
267 else if (strcmp(authstr
, "login") == 0)
269 else if (strcmp(authstr
, "cram-md5") == 0) {
271 auth
= AUTH_CRAM_MD5
;
273 fprintf(stderr
, tr(277, "No CRAM-MD5 support compiled in.\n"));
277 fprintf(stderr
, tr(274,
278 "Unknown SMTP authentication method: %s\n"), authstr
);
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"));
289 if (value("smtp-use-starttls") ||
290 value("smtp-use-tls") /* v11.0 compatibility */) {
292 if ((cp
= strchr(xserver
, ':')) != NULL
) {
293 server
= salloc(cp
- xserver
+ 1);
294 memcpy(server
, xserver
, cp
- xserver
);
295 server
[cp
- xserver
] = '\0';
298 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
301 SMTP_OUT("STARTTLS\r\n");
303 if (!debug
&& !_debug
&& ssl_open(server
, sp
, uhp
) != OKAY
)
307 if (value("smtp-use-starttls") || value("smtp-use-tls")) {
308 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
311 #endif /* !USE_SSL */
312 if (auth
!= AUTH_NONE
) {
313 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
319 SMTP_OUT("AUTH LOGIN\r\n");
321 b64
= strtob64(user
);
322 snprintf(o
, sizeof o
, "%s\r\n", b64
);
326 b64
= strtob64(password
);
327 snprintf(o
, sizeof o
, "%s\r\n", b64
);
333 SMTP_OUT("AUTH PLAIN\r\n");
335 snprintf(o
, sizeof o
, "%c%s%c%s", '\0', user
, '\0',
337 b64
= memtob64(o
, strlen(user
)+strlen(password
)+2);
338 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
);
355 snprintf(o
, sizeof o
, "HELO %s\r\n", nodename(1));
359 snprintf(o
, sizeof o
, "MAIL FROM:<%s>\r\n", skinned
);
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",
370 SMTP_OUT("DATA\r\n");
375 while (fgetline(&b
, &bsize
, &count
, &blen
, fi
, 1) != NULL
) {
380 } else if (inbcc
&& blankchar(*b
& 0377))
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) {
396 swrite1(sp
, ".", 1, 1);
398 if (debug
|| _debug
) {
399 fprintf(stderr
, ">>> %s", b
);
404 swrite1(sp
, b
, blen
+1, 1);
408 SMTP_OUT("QUIT\r\n");
415 static sigjmp_buf smtpjmp
;
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
,
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
);
444 if (saveterm
!= SIG_IGN
)
445 safe_signal(SIGTERM
, onterm
);
446 if (strncmp(server
, "smtp://", 7) == 0) {
450 } else if (strncmp(server
, "smtps://", 8) == 0) {
456 if (!debug
&& !_debug
&& sopen(server
, &so
, use_ssl
, server
,
457 use_ssl
? "smtps" : "smtp", verbose
) != OKAY
) {
458 safe_signal(SIGTERM
, saveterm
);
462 ret
= talk_smtp(to
, fi
, &so
, server
, server
, hp
,
463 user
, password
, skinned
);
464 if (!debug
&& !_debug
)
471 safe_signal(SIGTERM
, saveterm
);
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
)
486 fputs(catgets(catd
, CATSET
, 194,
487 "No SMTP support compiled in.\n"), stderr
);
490 #endif /* USE_SMTP */