2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
8 * Gunnar Ritter. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Gunnar Ritter
21 * and his contributors.
22 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid
[] = "@(#)smtp.c 2.43 (gritter) 8/4/07";
47 #include <sys/utsname.h>
49 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #ifdef HAVE_ARPA_INET_H
53 #include <arpa/inet.h>
54 #endif /* HAVE_ARPA_INET_H */
63 * Mail -- a mail program
65 * SMTP client and other internet related functions.
74 * Return our hostname.
77 nodename(int mayoverride
)
79 static char *hostname
;
83 #ifdef HAVE_IPv6_FUNCS
84 struct addrinfo hints
, *res
;
85 #else /* !HAVE_IPv6_FUNCS */
87 #endif /* !HAVE_IPv6_FUNCS */
88 #endif /* HAVE_SOCKETS */
90 if (mayoverride
&& (hn
= value("hostname")) != NULL
&& *hn
) {
92 hostname
= sstrdup(hn
);
94 if (hostname
== NULL
) {
98 #ifdef HAVE_IPv6_FUNCS
99 memset(&hints
, 0, sizeof hints
);
100 hints
.ai_socktype
= SOCK_DGRAM
; /* dummy */
101 hints
.ai_flags
= AI_CANONNAME
;
102 if (getaddrinfo(hn
, "0", &hints
, &res
) == 0) {
103 if (res
->ai_canonname
) {
104 hn
= salloc(strlen(res
->ai_canonname
) + 1);
105 strcpy(hn
, res
->ai_canonname
);
109 #else /* !HAVE_IPv6_FUNCS */
110 hent
= gethostbyname(hn
);
114 #endif /* !HAVE_IPv6_FUNCS */
115 #endif /* HAVE_SOCKETS */
116 hostname
= smalloc(strlen(hn
) + 1);
117 strcpy(hostname
, hn
);
123 * Return the user's From: address(es).
126 myaddrs(struct header
*hp
)
132 if (hp
!= NULL
&& hp
->h_from
!= NULL
) {
133 if (hp
->h_from
->n_fullname
)
134 return savestr(hp
->h_from
->n_fullname
);
135 if (hp
->h_from
->n_name
)
136 return savestr(hp
->h_from
->n_name
);
138 if ((cp
= value("from")) != NULL
)
141 * When invoking sendmail directly, it's its task
142 * to generate a From: address.
144 if (value("smtp") == NULL
)
148 sz
= strlen(myname
) + strlen(hn
) + 2;
150 snprintf(addr
, sz
, "%s@%s", myname
, hn
);
156 myorigin(struct header
*hp
)
161 if ((cp
= myaddrs(hp
)) == NULL
||
162 (np
= sextract(cp
, GEXTRA
|GFULL
)) == NULL
)
164 return np
->n_flink
!= NULL
? value("sender") : cp
;
169 static int read_smtp(struct sock
*sp
, int value
, int ign_eof
);
170 static int talk_smtp(struct name
*to
, FILE *fi
, struct sock
*sp
,
171 char *server
, char *uhp
, struct header
*hp
,
172 const char *user
, const char *password
, const char *skinned
);
175 smtp_auth_var(const char *type
, const char *addr
)
180 var
= ac_alloc(len
= strlen(type
) + strlen(addr
) + 7);
181 snprintf(var
, len
, "smtp-auth%s-%s", type
, addr
);
182 if ((cp
= value(var
)) != NULL
)
185 snprintf(var
, len
, "smtp-auth%s", type
);
186 if ((cp
= value(var
)) != NULL
)
193 static char *smtpbuf
;
194 static size_t smtpbufsize
;
197 * Get the SMTP server's answer, expecting value.
200 read_smtp(struct sock
*sp
, int value
, int ign_eof
)
206 if ((len
= sgetline(&smtpbuf
, &smtpbufsize
, NULL
, sp
)) < 6) {
207 if (len
>= 0 && !ign_eof
)
208 fprintf(stderr
, catgets(catd
, CATSET
, 241,
209 "Unexpected EOF on SMTP connection\n"));
212 if (verbose
|| debug
|| _debug
)
213 fputs(smtpbuf
, stderr
);
215 case '1': ret
= 1; break;
216 case '2': ret
= 2; break;
217 case '3': ret
= 3; break;
218 case '4': ret
= 4; break;
222 fprintf(stderr
, catgets(catd
, CATSET
, 191,
223 "smtp-server: %s"), smtpbuf
);
224 } while (smtpbuf
[3] == '-');
229 * Macros for talk_smtp.
231 #define _SMTP_ANSWER(x, ign_eof) \
232 if (!debug && !_debug) { \
234 if ((y = read_smtp(sp, x, ign_eof)) != (x) && \
235 (!(ign_eof) || y != -1)) { \
242 #define SMTP_ANSWER(x) _SMTP_ANSWER(x, 0)
244 #define SMTP_OUT(x) if (verbose || debug || _debug) \
245 fprintf(stderr, ">>> %s", x); \
246 if (!debug && !_debug) \
250 * Talk to a SMTP server.
253 talk_smtp(struct name
*to
, FILE *fi
, struct sock
*sp
,
254 char *xserver
, char *uhp
, struct header
*hp
,
255 const char *user
, const char *password
, const char *skinned
)
258 char *b
= NULL
, o
[LINESIZE
];
259 size_t blen
, bsize
= 0, count
;
260 char *b64
, *authstr
, *cp
;
261 enum { AUTH_NONE
, AUTH_PLAIN
, AUTH_LOGIN
, AUTH_CRAM_MD5
} auth
;
262 int inhdr
= 1, inbcc
= 0;
264 if ((authstr
= smtp_auth_var("", skinned
)) == NULL
)
265 auth
= user
&& password
? AUTH_LOGIN
: AUTH_NONE
;
266 else if (strcmp(authstr
, "plain") == 0)
268 else if (strcmp(authstr
, "login") == 0)
270 else if (strcmp(authstr
, "cram-md5") == 0)
271 auth
= AUTH_CRAM_MD5
;
273 fprintf(stderr
, "Unknown SMTP authentication "
274 "method: \"%s\"\n", authstr
);
277 if (auth
!= AUTH_NONE
&& (user
== NULL
|| password
== NULL
)) {
278 fprintf(stderr
, "User and password are necessary "
279 "for SMTP authentication.\n");
284 if (value("smtp-use-starttls") ||
285 value("smtp-use-tls") /* v11.0 compatibility */) {
287 if ((cp
= strchr(xserver
, ':')) != NULL
) {
288 server
= salloc(cp
- xserver
+ 1);
289 memcpy(server
, xserver
, cp
- xserver
);
290 server
[cp
- xserver
] = '\0';
293 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
296 SMTP_OUT("STARTTLS\r\n");
298 if (!debug
&& !_debug
&& ssl_open(server
, sp
, uhp
) != OKAY
)
302 if (value("smtp-use-starttls") || value("smtp-use-tls")) {
303 fprintf(stderr
, "No SSL support compiled in.\n");
306 #endif /* !USE_SSL */
307 if (auth
!= AUTH_NONE
) {
308 snprintf(o
, sizeof o
, "EHLO %s\r\n", nodename(1));
314 SMTP_OUT("AUTH LOGIN\r\n");
316 b64
= strtob64(user
);
317 snprintf(o
, sizeof o
, "%s\r\n", b64
);
321 b64
= strtob64(password
);
322 snprintf(o
, sizeof o
, "%s\r\n", b64
);
328 SMTP_OUT("AUTH PLAIN\r\n");
330 snprintf(o
, sizeof o
, "%c%s%c%s", '\0', user
, '\0',
332 b64
= memtob64(o
, strlen(user
)+strlen(password
)+2);
333 snprintf(o
, sizeof o
, "%s\r\n", b64
);
338 SMTP_OUT("AUTH CRAM-MD5\r\n");
340 for (cp
= smtpbuf
; digitchar(*cp
&0377); cp
++);
341 while (blankchar(*cp
&0377)) cp
++;
342 cp
= cram_md5_string(user
, password
, cp
);
348 snprintf(o
, sizeof o
, "HELO %s\r\n", nodename(1));
352 snprintf(o
, sizeof o
, "MAIL FROM:<%s>\r\n", skinned
);
355 for (n
= to
; n
!= NULL
; n
= n
->n_flink
) {
356 if ((n
->n_type
& GDEL
) == 0) {
357 snprintf(o
, sizeof o
, "RCPT TO:<%s>\r\n",
363 SMTP_OUT("DATA\r\n");
368 while (fgetline(&b
, &bsize
, &count
, &blen
, fi
, 1) != NULL
) {
373 } else if (inbcc
&& blankchar(*b
& 0377))
376 * We know what we have generated first, so
377 * do not look for whitespace before the ':'.
379 else if (ascncasecmp(b
, "bcc: ", 5) == 0) {
389 swrite1(sp
, ".", 1, 1);
391 if (debug
|| _debug
) {
392 fprintf(stderr
, ">>> %s", b
);
397 swrite1(sp
, b
, blen
+1, 1);
401 SMTP_OUT("QUIT\r\n");
408 static sigjmp_buf smtpjmp
;
413 siglongjmp(smtpjmp
, 1);
417 * Connect to a SMTP server.
420 smtp_mta(char *server
, struct name
*to
, FILE *fi
, struct header
*hp
,
421 const char *user
, const char *password
, const char *skinned
)
425 sighandler_type saveterm
;
427 memset(&so
, 0, sizeof so
);
428 verbose
= value("verbose") != NULL
;
429 _debug
= value("debug") != NULL
;
430 saveterm
= safe_signal(SIGTERM
, SIG_IGN
);
431 if (sigsetjmp(smtpjmp
, 1)) {
432 safe_signal(SIGTERM
, saveterm
);
435 if (saveterm
!= SIG_IGN
)
436 safe_signal(SIGTERM
, onterm
);
437 if (strncmp(server
, "smtp://", 7) == 0) {
441 } else if (strncmp(server
, "smtps://", 8) == 0) {
447 if (!debug
&& !_debug
&& sopen(server
, &so
, use_ssl
, server
,
448 use_ssl
? "smtps" : "smtp", verbose
) != OKAY
) {
449 safe_signal(SIGTERM
, saveterm
);
453 ret
= talk_smtp(to
, fi
, &so
, server
, server
, hp
,
454 user
, password
, skinned
);
455 if (!debug
&& !_debug
)
462 safe_signal(SIGTERM
, saveterm
);
465 #else /* !USE_SMTP */
467 smtp_mta(char *server
, struct name
*to
, FILE *fi
, struct header
*hp
,
468 const char *user
, const char *password
, const char *skinned
)
470 fputs(catgets(catd
, CATSET
, 194,
471 "No SMTP support compiled in.\n"), stderr
);
474 #endif /* USE_SMTP */