Add n_idec_{buf,cp}()
[s-mailx.git] / smtp.c
blob84fc9af2ee4939753d6f4a1da3e8b4d21751975a
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ SMTP client.
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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 */
9 /*
10 * Copyright (c) 2000
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
15 * are met:
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
39 * SUCH DAMAGE.
41 #undef n_FILE
42 #define n_FILE smtp
44 #ifndef HAVE_AMALGAMATION
45 # include "nail.h"
46 #endif
48 EMPTY_FILE()
49 #ifdef HAVE_SMTP
50 #include <sys/socket.h>
52 struct smtp_line {
53 char *dat; /* Actual data */
54 size_t datlen;
55 char *buf; /* Memory buffer */
56 size_t bufsize;
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);
70 #ifdef HAVE_GSSAPI
71 static bool_t _smtp_gssapi(struct sock *sp, struct sendbundle *sbp,
72 struct smtp_line *slp);
73 #endif
75 static void
76 _smtp_onterm(int signo)
78 NYD_X; /* Signal handler */
79 n_UNUSED(signo);
80 siglongjmp(_smtp_jmp, 1);
83 static int
84 _smtp_read(struct sock *sp, struct smtp_line *slp, int val,
85 bool_t ign_eof, bool_t want_dat)
87 int rv, len;
88 char *cp;
89 NYD_ENTER;
91 do {
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"));
95 rv = -1;
96 goto jleave;
98 if (n_poption & n_PO_VERBVERB)
99 n_err(slp->buf);
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;
107 if (val != rv)
108 n_err(_("smtp-server: %s"), slp->buf);
109 } while (slp->buf[3] == '-');
111 if (want_dat) {
112 for (cp = slp->buf; digitchar(*cp); --len, ++cp)
114 for (; blankchar(*cp); --len, ++cp)
116 slp->dat = cp;
117 assert(len >= 2);
118 len -= 2;
119 cp[slp->datlen = (size_t)len] = '\0';
121 jleave:
122 NYD_LEAVE;
123 return rv;
126 /* Indirect SMTP I/O */
127 #define _ANSWER(X, IGNEOF, WANTDAT) \
128 do if (!(n_poption & n_PO_DEBUG)) {\
129 int y;\
130 if ((y = _smtp_read(sp, slp, X, IGNEOF, WANTDAT)) != (X) &&\
131 (!(IGNEOF) || y != -1))\
132 goto jleave;\
133 } while (0)
134 #define _OUT(X) \
135 do {\
136 if (n_poption & n_PO_VERBVERB)\
137 n_err(">>> %s", X);\
138 if (!(n_poption & n_PO_DEBUG))\
139 swrite(sp, X);\
140 } while (0)
142 static bool_t
143 _smtp_talk(struct sock *sp, struct sendbundle *sbp) /* TODO n_string etc. */
145 char o[LINESIZE], *hostname;
146 struct smtp_line _sl, *slp = &_sl;
147 struct str b64;
148 struct name *n;
149 size_t blen, cnt;
150 bool_t inhdr = TRU1, inbcc = FAL0, rv = FAL0;
151 NYD_ENTER;
153 hostname = nodename(TRU1);
154 slp->buf = NULL;
155 slp->bufsize = 0;
157 /* Read greeting */
158 _ANSWER(2, FAL0, FAL0);
160 #ifdef HAVE_SSL
161 if (!sp->s_use_ssl && xok_blook(smtp_use_starttls, &sbp->sb_url, OXM_ALL)) {
162 snprintf(o, sizeof o, NETLINE("EHLO %s"), hostname);
163 _OUT(o);
164 _ANSWER(2, FAL0, FAL0);
166 _OUT(NETLINE("STARTTLS"));
167 _ANSWER(2, FAL0, FAL0);
169 if (!(n_poption & n_PO_DEBUG) && ssl_open(&sbp->sb_url, sp) != OKAY)
170 goto jleave;
172 #else
173 if (xok_blook(smtp_use_starttls, &sbp->sb_url, OXM_ALL)) {
174 n_err(_("No SSL support compiled in\n"));
175 goto jleave;
177 #endif
179 /* Shorthand: no authentication, plain HELO? */
180 if (sbp->sb_ccred.cc_authtype == AUTHTYPE_NONE) {
181 snprintf(o, sizeof o, NETLINE("HELO %s"), hostname);
182 _OUT(o);
183 _ANSWER(2, FAL0, FAL0);
184 goto jsend;
187 /* We'll have to deal with authentication */
188 snprintf(o, sizeof o, NETLINE("EHLO %s"), hostname);
189 _OUT(o);
190 _ANSWER(2, FAL0, FAL0);
192 switch (sbp->sb_ccred.cc_authtype) {
193 default:
194 /* FALLTHRU (doesn't happen) */
195 case AUTHTYPE_PLAIN:
196 cnt = sbp->sb_ccred.cc_user.l;
197 if(sbp->sb_ccred.cc_pass.l >= UIZ_MAX - 2 ||
198 cnt >= UIZ_MAX - 2 - sbp->sb_ccred.cc_pass.l){
199 jerr_cred:
200 n_err(_("Credentials overflow buffer sizes\n"));
201 goto jleave;
203 cnt += sbp->sb_ccred.cc_pass.l;
205 if(cnt >= sizeof(o) - 2)
206 goto jerr_cred;
207 cnt += 2;
208 if(b64_encode_calc_size(cnt) == UIZ_MAX)
209 goto jerr_cred;
211 _OUT(NETLINE("AUTH PLAIN"));
212 _ANSWER(3, FAL0, FAL0);
214 snprintf(o, sizeof o, "%c%s%c%s",
215 '\0', sbp->sb_ccred.cc_user.s, '\0', sbp->sb_ccred.cc_pass.s);
216 if(b64_encode_buf(&b64, o, cnt, B64_SALLOC | B64_CRLF) == NULL)
217 goto jleave;
218 _OUT(b64.s);
219 _ANSWER(2, FAL0, FAL0);
220 break;
221 case AUTHTYPE_LOGIN:
222 if(b64_encode_calc_size(sbp->sb_ccred.cc_user.l) == UIZ_MAX ||
223 b64_encode_calc_size(sbp->sb_ccred.cc_pass.l) == UIZ_MAX)
224 goto jerr_cred;
226 _OUT(NETLINE("AUTH LOGIN"));
227 _ANSWER(3, FAL0, FAL0);
229 if(b64_encode_buf(&b64, sbp->sb_ccred.cc_user.s, sbp->sb_ccred.cc_user.l,
230 B64_SALLOC | B64_CRLF) == NULL)
231 goto jleave;
232 _OUT(b64.s);
233 _ANSWER(3, FAL0, FAL0);
235 if(b64_encode_buf(&b64, sbp->sb_ccred.cc_pass.s, sbp->sb_ccred.cc_pass.l,
236 B64_SALLOC | B64_CRLF) == NULL)
237 goto jleave;
238 _OUT(b64.s);
239 _ANSWER(2, FAL0, FAL0);
240 break;
241 #ifdef HAVE_MD5
242 case AUTHTYPE_CRAM_MD5:{
243 char *cp;
245 if((cp = cram_md5_string(&sbp->sb_ccred.cc_user, &sbp->sb_ccred.cc_pass,
246 slp->dat)) == NULL)
247 goto jerr_cred;
249 _OUT(NETLINE("AUTH CRAM-MD5"));
250 _ANSWER(3, FAL0, TRU1);
252 _OUT(cp);
253 _ANSWER(2, FAL0, FAL0);
254 } break;
255 #endif
256 #ifdef HAVE_GSSAPI
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))
261 goto jleave;
262 break;
263 #endif
266 jsend:
267 snprintf(o, sizeof o, NETLINE("MAIL FROM:<%s>"), sbp->sb_url.url_u_h.s);
268 _OUT(o);
269 _ANSWER(2, FAL0, FAL0);
271 for (n = sbp->sb_to; n != NULL; n = n->n_flink) {
272 if (!(n->n_type & GDEL)) {
273 snprintf(o, sizeof o, NETLINE("RCPT TO:<%s>"), skinned_name(n));
274 _OUT(o);
275 _ANSWER(2, FAL0, FAL0);
279 _OUT(NETLINE("DATA"));
280 _ANSWER(3, FAL0, FAL0);
282 fflush_rewind(sbp->sb_input);
283 cnt = fsize(sbp->sb_input);
284 while (fgetline(&slp->buf, &slp->bufsize, &cnt, &blen, sbp->sb_input, 1)
285 != NULL) {
286 if (inhdr) {
287 if (*slp->buf == '\n')
288 inhdr = inbcc = FAL0;
289 else if (inbcc && blankchar(*slp->buf))
290 continue;
291 /* We know what we have generated first, so do not look for whitespace
292 * before the ':' */
293 else if (!ascncasecmp(slp->buf, "bcc: ", 5)) {
294 inbcc = TRU1;
295 continue;
296 } else
297 inbcc = FAL0;
300 if (n_poption & n_PO_DEBUG) {
301 slp->buf[blen - 1] = '\0';
302 n_err(">>> %s%s\n", (*slp->buf == '.' ? "." : n_empty), slp->buf);
303 continue;
305 if (*slp->buf == '.')
306 swrite1(sp, ".", 1, 1); /* TODO I/O rewrite.. */
307 slp->buf[blen - 1] = NETNL[0];
308 slp->buf[blen] = NETNL[1];
309 swrite1(sp, slp->buf, blen + 1, 1);
311 _OUT(NETLINE("."));
312 _ANSWER(2, FAL0, FAL0);
314 _OUT(NETLINE("QUIT"));
315 _ANSWER(2, TRU1, FAL0);
316 rv = TRU1;
317 jleave:
318 if (slp->buf != NULL)
319 free(slp->buf);
320 NYD_LEAVE;
321 return rv;
324 #ifdef HAVE_GSSAPI
325 # include "smtp_gssapi.h"
326 #endif
328 #undef _OUT
329 #undef _ANSWER
331 FL bool_t
332 smtp_mta(struct sendbundle *sbp)
334 struct sock so;
335 sighandler_type volatile saveterm;
336 bool_t volatile rv = FAL0;
337 NYD_ENTER;
339 saveterm = safe_signal(SIGTERM, SIG_IGN);
340 if (sigsetjmp(_smtp_jmp, 1))
341 goto jleave;
342 if (saveterm != SIG_IGN)
343 safe_signal(SIGTERM, &_smtp_onterm);
345 memset(&so, 0, sizeof so);
346 if (!(n_poption & n_PO_DEBUG) && !sopen(&so, &sbp->sb_url))
347 goto jleave;
349 so.s_desc = "SMTP";
350 rv = _smtp_talk(&so, sbp);
352 if (!(n_poption & n_PO_DEBUG))
353 sclose(&so);
354 jleave:
355 safe_signal(SIGTERM, saveterm);
356 NYD_LEAVE;
357 return rv;
359 #endif /* HAVE_SMTP */
361 /* s-it-mode */