1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
3 *@ TODO - use initial responses to save a round-trip (RFC 4954)
4 *@ TODO - more (verbose) understanding+rection upon STATUS CODES
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
11 * Gunnar Ritter. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Gunnar Ritter
24 * and his contributors.
25 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #ifndef HAVE_AMALGAMATION
50 #include <sys/socket.h>
53 char *dat
; /* Actual data */
55 char *buf
; /* Memory buffer */
59 static sigjmp_buf _smtp_jmp
;
61 static void _smtp_onterm(int signo
);
63 /* Get the SMTP server's answer, expecting val */
64 static int _smtp_read(struct sock
*sp
, struct smtp_line
*slp
, int val
,
65 bool_t ign_eof
, bool_t want_dat
);
67 /* Talk to a SMTP server */
68 static bool_t
_smtp_talk(struct sock
*sp
, struct sendbundle
*sbp
);
71 static bool_t
_smtp_gssapi(struct sock
*sp
, struct sendbundle
*sbp
,
72 struct smtp_line
*slp
);
76 _smtp_onterm(int signo
)
78 NYD_X
; /* Signal handler */
80 siglongjmp(_smtp_jmp
, 1);
84 _smtp_read(struct sock
*sp
, struct smtp_line
*slp
, int val
,
85 bool_t ign_eof
, bool_t want_dat
)
92 if ((len
= sgetline(&slp
->buf
, &slp
->bufsize
, NULL
, sp
)) < 6) {
93 if (len
>= 0 && !ign_eof
)
94 n_err(_("Unexpected EOF on SMTP connection\n"));
98 if (n_poption
& n_PO_VERBVERB
)
100 switch (slp
->buf
[0]) {
101 case '1': rv
= 1; break;
102 case '2': rv
= 2; break;
103 case '3': rv
= 3; break;
104 case '4': rv
= 4; break;
105 default: rv
= 5; break;
108 n_err(_("smtp-server: %s"), slp
->buf
);
109 } while (slp
->buf
[3] == '-');
112 for (cp
= slp
->buf
; digitchar(*cp
); --len
, ++cp
)
114 for (; blankchar(*cp
); --len
, ++cp
)
119 cp
[slp
->datlen
= (size_t)len
] = '\0';
126 /* Indirect SMTP I/O */
127 #define _ANSWER(X, IGNEOF, WANTDAT) \
128 do if (!(n_poption & n_PO_DEBUG)) {\
130 if ((y = _smtp_read(sp, slp, X, IGNEOF, WANTDAT)) != (X) &&\
131 (!(IGNEOF) || y != -1))\
136 if (n_poption & n_PO_D_VV)\
138 if (!(n_poption & n_PO_DEBUG))\
143 _smtp_talk(struct sock
*sp
, struct sendbundle
*sbp
) /* TODO n_string etc. */
146 char const *hostname
;
147 struct smtp_line _sl
, *slp
= &_sl
;
151 bool_t inhdr
= TRU1
, inbcc
= FAL0
, rv
= FAL0
;
154 hostname
= n_nodename(TRU1
);
159 _ANSWER(2, FAL0
, FAL0
);
162 if (!sp
->s_use_ssl
&& xok_blook(smtp_use_starttls
, &sbp
->sb_url
, OXM_ALL
)) {
163 snprintf(o
, sizeof o
, NETLINE("EHLO %s"), hostname
);
165 _ANSWER(2, FAL0
, FAL0
);
167 _OUT(NETLINE("STARTTLS"));
168 _ANSWER(2, FAL0
, FAL0
);
170 if (!(n_poption
& n_PO_DEBUG
) && ssl_open(&sbp
->sb_url
, sp
) != OKAY
)
174 if (xok_blook(smtp_use_starttls
, &sbp
->sb_url
, OXM_ALL
)) {
175 n_err(_("No SSL support compiled in\n"));
180 /* Shorthand: no authentication, plain HELO? */
181 if (sbp
->sb_ccred
.cc_authtype
== AUTHTYPE_NONE
) {
182 snprintf(o
, sizeof o
, NETLINE("HELO %s"), hostname
);
184 _ANSWER(2, FAL0
, FAL0
);
188 /* We'll have to deal with authentication */
189 snprintf(o
, sizeof o
, NETLINE("EHLO %s"), hostname
);
191 _ANSWER(2, FAL0
, FAL0
);
193 switch (sbp
->sb_ccred
.cc_authtype
) {
195 /* FALLTHRU (doesn't happen) */
197 cnt
= sbp
->sb_ccred
.cc_user
.l
;
198 if(sbp
->sb_ccred
.cc_pass
.l
>= UIZ_MAX
- 2 ||
199 cnt
>= UIZ_MAX
- 2 - sbp
->sb_ccred
.cc_pass
.l
){
201 n_err(_("Credentials overflow buffer sizes\n"));
204 cnt
+= sbp
->sb_ccred
.cc_pass
.l
;
206 if(cnt
>= sizeof(o
) - 2)
209 if(b64_encode_calc_size(cnt
) == UIZ_MAX
)
212 _OUT(NETLINE("AUTH PLAIN"));
213 _ANSWER(3, FAL0
, FAL0
);
215 snprintf(o
, sizeof o
, "%c%s%c%s",
216 '\0', sbp
->sb_ccred
.cc_user
.s
, '\0', sbp
->sb_ccred
.cc_pass
.s
);
217 if(b64_encode_buf(&b64
, o
, cnt
, B64_SALLOC
| B64_CRLF
) == NULL
)
220 _ANSWER(2, FAL0
, FAL0
);
223 if(b64_encode_calc_size(sbp
->sb_ccred
.cc_user
.l
) == UIZ_MAX
||
224 b64_encode_calc_size(sbp
->sb_ccred
.cc_pass
.l
) == UIZ_MAX
)
227 _OUT(NETLINE("AUTH LOGIN"));
228 _ANSWER(3, FAL0
, FAL0
);
230 if(b64_encode_buf(&b64
, sbp
->sb_ccred
.cc_user
.s
, sbp
->sb_ccred
.cc_user
.l
,
231 B64_SALLOC
| B64_CRLF
) == NULL
)
234 _ANSWER(3, FAL0
, FAL0
);
236 if(b64_encode_buf(&b64
, sbp
->sb_ccred
.cc_pass
.s
, sbp
->sb_ccred
.cc_pass
.l
,
237 B64_SALLOC
| B64_CRLF
) == NULL
)
240 _ANSWER(2, FAL0
, FAL0
);
243 case AUTHTYPE_CRAM_MD5
:{
246 _OUT(NETLINE("AUTH CRAM-MD5"));
247 _ANSWER(3, FAL0
, TRU1
);
249 if((cp
= cram_md5_string(&sbp
->sb_ccred
.cc_user
, &sbp
->sb_ccred
.cc_pass
,
253 _ANSWER(2, FAL0
, FAL0
);
257 case AUTHTYPE_GSSAPI
:
258 if (n_poption
& n_PO_DEBUG
)
259 n_err(_(">>> We would perform GSS-API authentication now\n"));
260 else if (!_smtp_gssapi(sp
, sbp
, slp
))
267 snprintf(o
, sizeof o
, NETLINE("MAIL FROM:<%s>"), sbp
->sb_url
.url_u_h
.s
);
269 _ANSWER(2, FAL0
, FAL0
);
271 for(n
= sbp
->sb_to
; n
!= NULL
; n
= n
->n_flink
){
272 if (!(n
->n_type
& GDEL
)) { /* TODO should not happen!?! */
273 if(n
->n_flags
& NAME_ADDRSPEC_WITHOUT_DOMAIN
)
274 snprintf(o
, sizeof o
, NETLINE("RCPT TO:<%s@%s>"),
275 skinned_name(n
), hostname
);
277 snprintf(o
, sizeof o
, NETLINE("RCPT TO:<%s>"), skinned_name(n
));
279 _ANSWER(2, FAL0
, FAL0
);
283 _OUT(NETLINE("DATA"));
284 _ANSWER(3, FAL0
, FAL0
);
286 fflush_rewind(sbp
->sb_input
);
287 cnt
= fsize(sbp
->sb_input
);
288 while (fgetline(&slp
->buf
, &slp
->bufsize
, &cnt
, &blen
, sbp
->sb_input
, 1)
291 if (*slp
->buf
== '\n')
292 inhdr
= inbcc
= FAL0
;
293 else if (inbcc
&& blankchar(*slp
->buf
))
295 /* We know what we have generated first, so do not look for whitespace
297 else if (!ascncasecmp(slp
->buf
, "bcc: ", 5)) {
304 if (n_poption
& n_PO_DEBUG
) {
305 slp
->buf
[blen
- 1] = '\0';
306 n_err(">>> %s%s\n", (*slp
->buf
== '.' ? "." : n_empty
), slp
->buf
);
309 if (*slp
->buf
== '.')
310 swrite1(sp
, ".", 1, 1); /* TODO I/O rewrite.. */
311 slp
->buf
[blen
- 1] = NETNL
[0];
312 slp
->buf
[blen
] = NETNL
[1];
313 swrite1(sp
, slp
->buf
, blen
+ 1, 1);
316 _ANSWER(2, FAL0
, FAL0
);
318 _OUT(NETLINE("QUIT"));
319 _ANSWER(2, TRU1
, FAL0
);
322 if (slp
->buf
!= NULL
)
329 # include "smtp-gssapi.h"
336 smtp_mta(struct sendbundle
*sbp
)
339 sighandler_type
volatile saveterm
;
340 bool_t
volatile rv
= FAL0
;
343 saveterm
= safe_signal(SIGTERM
, SIG_IGN
);
344 if (sigsetjmp(_smtp_jmp
, 1))
346 if (saveterm
!= SIG_IGN
)
347 safe_signal(SIGTERM
, &_smtp_onterm
);
349 memset(&so
, 0, sizeof so
);
350 if (!(n_poption
& n_PO_DEBUG
) && !sopen(&so
, &sbp
->sb_url
))
354 rv
= _smtp_talk(&so
, sbp
);
356 if (!(n_poption
& n_PO_DEBUG
))
359 safe_signal(SIGTERM
, saveterm
);
363 #endif /* HAVE_SMTP */