* Version 2.11.6
[alpine.git] / imap / src / c-client / smtp.c
blob1cba335716221e78eac3198ab68e6fdc95aa7d91
1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Program: Simple Mail Transfer Protocol (SMTP) routines
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
25 * Date: 27 July 1988
26 * Last Edited: 28 January 2008
28 * This original version of this file is
29 * Copyright 1988 Stanford University
30 * and was developed in the Symbolic Systems Resources Group of the Knowledge
31 * Systems Laboratory at Stanford University in 1987-88, and was funded by the
32 * Biomedical Research Technology Program of the National Institutes of Health
33 * under grant number RR-00785.
37 #include <ctype.h>
38 #include <stdio.h>
39 #include "c-client.h"
41 /* Constants */
43 #define SMTPSSLPORT (long) 465 /* former assigned SSL TCP contact port */
44 #define SMTPGREET (long) 220 /* SMTP successful greeting */
45 #define SMTPAUTHED (long) 235 /* SMTP successful authentication */
46 #define SMTPOK (long) 250 /* SMTP OK code */
47 #define SMTPAUTHREADY (long) 334/* SMTP ready for authentication */
48 #define SMTPREADY (long) 354 /* SMTP ready for data */
49 #define SMTPSOFTFATAL (long) 421/* SMTP soft fatal code */
50 #define SMTPWANTAUTH (long) 505 /* SMTP authentication needed */
51 #define SMTPWANTAUTH2 (long) 530/* SMTP authentication needed */
52 #define SMTPUNAVAIL (long) 550 /* SMTP mailbox unavailable */
53 #define SMTPHARDERROR (long) 554/* SMTP miscellaneous hard failure */
56 /* Convenient access to protocol-specific data */
58 #define ESMTP stream->protocol.esmtp
61 /* Function prototypes */
63 void *smtp_challenge (void *s,unsigned long *len);
64 long smtp_response (void *s,char *response,unsigned long size);
65 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp);
66 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error);
67 long smtp_send (SENDSTREAM *stream,char *command,char *args);
68 long smtp_reply (SENDSTREAM *stream);
69 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb);
70 long smtp_fake (SENDSTREAM *stream,char *text);
71 static long smtp_seterror (SENDSTREAM *stream,long code,char *text);
72 long smtp_soutr (void *stream,char *s);
74 /* Mailer parameters */
76 static unsigned long smtp_maxlogintrials = MAXLOGINTRIALS;
77 static long smtp_port = 0; /* default port override */
78 static long smtp_sslport = 0;
81 #ifndef RFC2821
82 #define RFC2821 /* RFC 2821 compliance */
83 #endif
85 /* SMTP limits, current as of RFC 2821 */
87 #define SMTPMAXLOCALPART 64
88 #define SMTPMAXDOMAIN 255
89 #define SMTPMAXPATH 256
92 /* I have seen local parts of more than 64 octets, in spite of the SMTP
93 * limits. So, we'll have a more generous limit that's still guaranteed
94 * not to pop the buffer, and let the server worry about it. As of this
95 * writing, it comes out to 240. Anyone with a mailbox name larger than
96 * that is in serious need of a life or at least a new ISP! 23 June 1998
99 #define MAXLOCALPART ((MAILTMPLEN - (SMTPMAXDOMAIN + SMTPMAXPATH + 32)) / 2)
101 /* Mail Transfer Protocol manipulate driver parameters
102 * Accepts: function code
103 * function-dependent value
104 * Returns: function-dependent return value
107 void *smtp_parameters (long function,void *value)
109 switch ((int) function) {
110 case SET_MAXLOGINTRIALS:
111 smtp_maxlogintrials = (unsigned long) value;
112 break;
113 case GET_MAXLOGINTRIALS:
114 value = (void *) smtp_maxlogintrials;
115 break;
116 case SET_SMTPPORT:
117 smtp_port = (long) value;
118 break;
119 case GET_SMTPPORT:
120 value = (void *) smtp_port;
121 break;
122 case SET_SSLSMTPPORT:
123 smtp_sslport = (long) value;
124 break;
125 case GET_SSLSMTPPORT:
126 value = (void *) smtp_sslport;
127 break;
128 default:
129 value = NIL; /* error case */
130 break;
132 return value;
135 /* Mail Transfer Protocol open connection
136 * Accepts: network driver
137 * service host list
138 * port number
139 * service name
140 * SMTP open options
141 * Returns: SEND stream on success, NIL on failure
144 SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
145 unsigned long port,long options)
147 SENDSTREAM *stream = NIL;
148 long reply;
149 char *s,tmp[MAILTMPLEN];
150 NETSTREAM *netstream;
151 NETMBX mb;
152 if (!(hostlist && *hostlist)) mm_log ("Missing SMTP service host",ERROR);
153 /* maximum domain name is 64 characters */
154 else do if (strlen (*hostlist) < SMTPMAXDOMAIN) {
155 sprintf (tmp,"{%.1000s}",*hostlist);
156 if (!mail_valid_net_parse_work (tmp,&mb,service ? service : "smtp") ||
157 mb.anoflag || mb.readonlyflag) {
158 sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
159 mm_log (tmp,ERROR);
161 else { /* light tryssl flag if requested */
162 mb.trysslflag = (options & SOP_TRYSSL) ? T : NIL;
163 /* explicit port overrides all */
164 if (mb.port) port = mb.port;
165 /* else /submit overrides port argument */
166 else if (!compare_cstring (mb.service,"submit")) {
167 port = SUBMITTCPPORT; /* override port, use IANA name */
168 strcpy (mb.service,"submission");
170 /* else port argument overrides SMTP port */
171 else if (!port) port = smtp_port ? smtp_port : SMTPTCPPORT;
172 if (netstream = /* try to open ordinary connection */
173 net_open (&mb,dv,port,
174 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
175 "*smtps",smtp_sslport ? smtp_sslport : SMTPSSLPORT)) {
176 stream = (SENDSTREAM *) memset (fs_get (sizeof (SENDSTREAM)),0,
177 sizeof (SENDSTREAM));
178 stream->netstream = netstream;
179 stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
180 net_host (netstream) : mb.host);
181 stream->debug = (mb.dbgflag || (options & OP_DEBUG)) ? T : NIL;
182 if (options & SOP_SECURE) mb.secflag = T;
183 /* get name of local host to use */
184 s = compare_cstring ("localhost",mb.host) ?
185 net_localhost (netstream) : "localhost";
187 do reply = smtp_reply (stream);
188 while ((reply < 100) || (stream->reply[3] == '-'));
189 if (reply != SMTPGREET){/* get SMTP greeting */
190 sprintf (tmp,"SMTP greeting failure: %.80s",stream->reply);
191 mm_log (tmp,ERROR);
192 stream = smtp_close (stream);
194 /* try EHLO first, then HELO */
195 else if (((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) &&
196 ((reply = smtp_send (stream,"HELO",s)) != SMTPOK)) {
197 sprintf (tmp,"SMTP hello failure: %.80s",stream->reply);
198 mm_log (tmp,ERROR);
199 stream = smtp_close (stream);
201 else {
202 NETDRIVER *ssld =(NETDRIVER *)mail_parameters(NIL,GET_SSLDRIVER,NIL);
203 sslstart_t stls = (sslstart_t) mail_parameters(NIL,GET_SSLSTART,NIL);
204 ESMTP.ok = T; /* ESMTP server, start TLS if present */
205 if (!dv && stls && ESMTP.service.starttls &&
206 !mb.sslflag && !mb.notlsflag &&
207 (smtp_send (stream,"STARTTLS",NIL) == SMTPGREET)) {
208 mb.tlsflag = T; /* TLS OK, get into TLS at this end */
209 stream->netstream->dtb = ssld;
210 /* TLS started, negotiate it */
211 if (!(stream->netstream->stream = (*stls)
212 (stream->netstream->stream,mb.host,
213 SSL_METHOD(mb) | (mb.novalidate ? NET_NOVALIDATECERT:NIL)))){
214 /* TLS negotiation failed after STARTTLS */
215 sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",
216 mb.host);
217 mm_log (tmp,ERROR);
218 /* close without doing QUIT */
219 if (stream->netstream) net_close (stream->netstream);
220 stream->netstream = NIL;
221 stream = smtp_close (stream);
223 /* TLS OK, re-negotiate EHLO */
224 else if ((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) {
225 sprintf (tmp,"SMTP EHLO failure after STARTTLS: %.80s",
226 stream->reply);
227 mm_log (tmp,ERROR);
228 stream = smtp_close (stream);
230 else ESMTP.ok = T; /* TLS OK and EHLO successful */
232 else if (mb.tlsflag) {/* user specified /tls but can't do it */
233 sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
234 mm_log (tmp,ERROR);
235 stream = smtp_close (stream);
238 /* remote name for authentication */
239 if (stream && ((mb.secflag || mb.user[0]))) {
240 if (ESMTP.auth) { /* use authenticator? */
241 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
242 /* remote name for authentication */
243 strncpy (mb.host,
244 (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
245 net_remotehost (netstream) : net_host (netstream),
246 NETMAXHOST-1);
247 mb.host[NETMAXHOST-1] = '\0';
249 if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
251 else { /* no available authenticators? */
252 sprintf (tmp,"%sSMTP authentication not available: %.80s",
253 mb.secflag ? "Secure " : "",mb.host);
254 mm_log (tmp,ERROR);
255 stream = smtp_close (stream);
261 } while (!stream && *++hostlist);
262 if (stream) { /* set stream options if have a stream */
263 if (options &(SOP_DSN | SOP_DSN_NOTIFY_FAILURE | SOP_DSN_NOTIFY_DELAY |
264 SOP_DSN_NOTIFY_SUCCESS | SOP_DSN_RETURN_FULL)) {
265 ESMTP.dsn.want = T;
266 if (options & SOP_DSN_NOTIFY_FAILURE) ESMTP.dsn.notify.failure = T;
267 if (options & SOP_DSN_NOTIFY_DELAY) ESMTP.dsn.notify.delay = T;
268 if (options & SOP_DSN_NOTIFY_SUCCESS) ESMTP.dsn.notify.success = T;
269 if (options & SOP_DSN_RETURN_FULL) ESMTP.dsn.full = T;
271 if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
273 return stream;
276 /* SMTP authenticate
277 * Accepts: stream to login
278 * parsed network mailbox structure
279 * scratch buffer
280 * place to return user name
281 * Returns: T on success, NIL on failure
284 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
286 unsigned long trial,auths;
287 char *lsterr = NIL;
288 char usr[MAILTMPLEN];
289 AUTHENTICATOR *at;
290 long ret = NIL;
291 for (auths = ESMTP.auth, stream->saslcancel = NIL;
292 !ret && stream->netstream && auths &&
293 (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
294 if (lsterr) { /* previous authenticator failed? */
295 sprintf (tmp,"Retrying using %s authentication after %.80s",
296 at->name,lsterr);
297 mm_log (tmp,NIL);
298 fs_give ((void **) &lsterr);
300 trial = 0; /* initial trial count */
301 tmp[0] = '\0'; /* empty buffer */
302 if (stream->netstream) do {
303 if (lsterr) {
304 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
305 mm_log (tmp,WARN);
306 fs_give ((void **) &lsterr);
308 stream->saslcancel = NIL;
309 if (smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
310 /* hide client authentication responses */
311 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
312 if ((*at->client) (smtp_challenge,smtp_response,"smtp",mb,stream,
313 &trial,usr)) {
314 if (stream->replycode == SMTPAUTHED) {
315 ESMTP.auth = NIL; /* disable authenticators */
316 ret = LONGT;
318 /* if main program requested cancellation */
319 else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
321 stream->sensitive = NIL;/* unhide */
323 /* remember response if error and no cancel */
324 if (!ret && trial) lsterr = cpystr (stream->reply);
325 } while (!ret && stream->netstream && trial &&
326 (trial < smtp_maxlogintrials));
328 if (lsterr) { /* previous authenticator failed? */
329 if (!stream->saslcancel) { /* don't do this if a cancel */
330 sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
331 mm_log (tmp,ERROR);
333 fs_give ((void **) &lsterr);
335 return ret; /* authentication failed */
338 /* Get challenge to authenticator in binary
339 * Accepts: stream
340 * pointer to returned size
341 * Returns: challenge or NIL if not challenge
344 void *smtp_challenge (void *s,unsigned long *len)
346 char tmp[MAILTMPLEN];
347 void *ret = NIL;
348 SENDSTREAM *stream = (SENDSTREAM *) s;
349 if ((stream->replycode == SMTPAUTHREADY) &&
350 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
351 strlen (stream->reply + 4),len))) {
352 sprintf (tmp,"SMTP SERVER BUG (invalid challenge): %.80s",stream->reply+4);
353 mm_log (tmp,ERROR);
355 return ret;
359 /* Send authenticator response in BASE64
360 * Accepts: MAIL stream
361 * string to send
362 * length of string
363 * Returns: T, always
366 long smtp_response (void *s,char *response,unsigned long size)
368 SENDSTREAM *stream = (SENDSTREAM *) s;
369 unsigned long i,j;
370 char *t,*u;
371 if (response) { /* make CRLFless BASE64 string */
372 if (size) {
373 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
374 j < i; j++) if (t[j] > ' ') *u++ = t[j];
375 *u = '\0'; /* tie off string */
376 i = smtp_send (stream,t,NIL);
377 fs_give ((void **) &t);
379 else i = smtp_send (stream,"",NIL);
381 else { /* abort requested */
382 i = smtp_send (stream,"*",NIL);
383 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
385 return LONGT;
388 /* Mail Transfer Protocol close connection
389 * Accepts: SEND stream
390 * Returns: NIL always
393 SENDSTREAM *smtp_close (SENDSTREAM *stream)
395 if (stream) { /* send "QUIT" */
396 if (stream->netstream) { /* do close actions if have netstream */
397 smtp_send (stream,"QUIT",NIL);
398 if (stream->netstream) /* could have been closed during "QUIT" */
399 net_close (stream->netstream);
401 /* clean up */
402 if (stream->host) fs_give ((void **) &stream->host);
403 if (stream->reply) fs_give ((void **) &stream->reply);
404 if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
405 if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
406 fs_give ((void **) &stream);/* flush the stream */
408 return NIL;
411 /* Mail Transfer Protocol deliver mail
412 * Accepts: SEND stream
413 * delivery option (MAIL, SEND, SAML, SOML)
414 * message envelope
415 * message body
416 * Returns: T on success, NIL on failure
419 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
421 RFC822BUFFER buf;
422 char tmp[SENDBUFLEN+1], smtpserver[SENDBUFLEN+1];
423 long error = NIL;
424 long retry = NIL;
425 buf.f = smtp_soutr; /* initialize buffer */
426 buf.s = stream->netstream;
427 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
428 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
429 smtpserver[SENDBUFLEN] = '\0';
430 if (!(env->to || env->cc || env->bcc)) {
431 /* no recipients in request */
432 smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
433 return NIL;
435 /* get this now in case the rug is pulled from under us */
436 sprintf (smtpserver,"{%.200s/smtp%s}<none>",
437 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
438 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
439 net_remotehost (stream->netstream) :
440 net_host (stream->netstream)) :
441 stream->host,
442 (stream->netstream->dtb ==
443 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
444 "/ssl" : "");
445 do {
446 smtp_send (stream,"RSET",NIL);
447 if (retry) { /* need to retry with authentication? */
448 NETMBX mb;
449 /* yes, build remote name for authentication */
450 mail_valid_net_parse (smtpserver,&mb);
451 if (!smtp_auth (stream,&mb,smtpserver)) return NIL;
452 retry = NIL; /* no retry at this point */
455 strcpy (tmp,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
456 #ifdef RFC2821
457 if (env->return_path && env->return_path->host &&
458 !((strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
459 (strlen (env->return_path->host) > SMTPMAXDOMAIN))) {
460 rfc822_cat (tmp,env->return_path->mailbox,NIL);
461 sprintf (tmp + strlen (tmp),"@%s",env->return_path->host);
463 #else /* old code with A-D-L support */
464 if (env->return_path && env->return_path->host &&
465 !((env->return_path->adl &&
466 (strlen (env->return_path->adl) > SMTPMAXPATH)) ||
467 (strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
468 (strlen (env->return_path->host) > SMTPMAXDOMAIN)))
469 rfc822_address (tmp,env->return_path);
470 #endif
471 strcat (tmp,">");
472 if (ESMTP.ok) {
473 if (ESMTP.eightbit.ok && ESMTP.eightbit.want)
474 strcat (tmp," BODY=8BITMIME");
475 if (ESMTP.dsn.ok && ESMTP.dsn.want) {
476 strcat (tmp,ESMTP.dsn.full ? " RET=FULL" : " RET=HDRS");
477 if (ESMTP.dsn.envid)
478 sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
481 /* send "MAIL FROM" command */
482 switch (smtp_send (stream,type,tmp)) {
483 case SMTPUNAVAIL: /* mailbox unavailable? */
484 case SMTPWANTAUTH: /* wants authentication? */
485 case SMTPWANTAUTH2:
486 if (ESMTP.auth) retry = T;/* yes, retry with authentication */
487 case SMTPOK: /* looks good */
488 break;
489 default: /* other failure */
490 return NIL;
492 /* negotiate the recipients */
493 if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
494 if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
495 if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
496 if (!retry && error) { /* any recipients failed? */
497 smtp_send (stream,"RSET",NIL);
498 smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
499 return NIL;
501 } while (retry);
502 /* negotiate data command */
503 if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) return NIL;
504 /* send message data */
505 if (!rfc822_output_full (&buf,env,body,
506 ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
507 smtp_fake (stream,"SMTP connection broken (message data)");
508 return NIL; /* can't do much else here */
510 /* send trailing dot */
511 return (smtp_send (stream,".",NIL) == SMTPOK) ? LONGT : NIL;
514 /* Simple Mail Transfer Protocol send VERBose
515 * Accepts: SMTP stream
516 * Returns: T if successful, else NIL
518 * Descriptive text formerly in [al]pine sources:
519 * At worst, this command may cause the SMTP connection to get nuked. Modern
520 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
521 * arg is for PMDF). What's more, if it works, the reply code and accompanying
522 * text may vary from server to server.
525 long smtp_verbose (SENDSTREAM *stream)
527 /* accept any 2xx reply code */
528 return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
531 /* Internal routines */
534 /* Simple Mail Transfer Protocol send recipient
535 * Accepts: SMTP stream
536 * address list
537 * pointer to error flag
538 * Returns: T if should retry, else NIL
541 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
543 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
544 while (adr) { /* for each address on the list */
545 /* clear any former error */
546 if (adr->error) fs_give ((void **) &adr->error);
547 if (adr->host) { /* ignore group syntax */
548 /* enforce SMTP limits to protect the buffer */
549 if (strlen (adr->mailbox) > MAXLOCALPART) {
550 adr->error = cpystr ("501 Recipient name too long");
551 *error = T;
553 else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
554 adr->error = cpystr ("501 Recipient domain too long");
555 *error = T;
557 #ifndef RFC2821 /* old code with A-D-L support */
558 else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
559 adr->error = cpystr ("501 Path too long");
560 *error = T;
562 #endif
564 else {
565 strcpy (tmp,"TO:<"); /* compose "RCPT TO:<return-path>" */
566 #ifdef RFC2821
567 rfc822_cat (tmp,adr->mailbox,NIL);
568 sprintf (tmp + strlen (tmp),"@%s>",adr->host);
569 #else /* old code with A-D-L support */
570 rfc822_address (tmp,adr);
571 strcat (tmp,">");
572 #endif
573 /* want notifications */
574 if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
575 /* yes, start with prefix */
576 strcat (tmp," NOTIFY=");
577 s = tmp + strlen (tmp);
578 if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
579 if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
580 if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
581 /* tie off last comma */
582 if (*s) s[strlen (s) - 1] = '\0';
583 else strcat (tmp,"NEVER");
584 if (adr->orcpt.addr) {
585 sprintf (orcpt,"%.498s;%.498s",
586 adr->orcpt.type ? adr->orcpt.type : "rfc822",
587 adr->orcpt.addr);
588 sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
591 switch (smtp_send (stream,"RCPT",tmp)) {
592 case SMTPOK: /* looks good */
593 break;
594 case SMTPUNAVAIL: /* mailbox unavailable? */
595 case SMTPWANTAUTH: /* wants authentication? */
596 case SMTPWANTAUTH2:
597 if (ESMTP.auth) return T;
598 default: /* other failure */
599 *error = T; /* note that an error occurred */
600 adr->error = cpystr (stream->reply);
604 adr = adr->next; /* do any subsequent recipients */
606 return NIL; /* no retry called for */
609 /* Simple Mail Transfer Protocol send command
610 * Accepts: SEND stream
611 * text
612 * Returns: reply code
615 long smtp_send (SENDSTREAM *stream,char *command,char *args)
617 long ret;
618 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
619 + 3);
620 /* build the complete command */
621 if (args) sprintf (s,"%s %s",command,args);
622 else strcpy (s,command);
623 if (stream->debug) mail_dlog (s,stream->sensitive);
624 strcat (s,"\015\012");
625 /* send the command */
626 if (stream->netstream && net_soutr (stream->netstream,s)) {
627 do stream->replycode = smtp_reply (stream);
628 while ((stream->replycode < 100) || (stream->reply[3] == '-'));
629 ret = stream->replycode;
631 else ret = smtp_fake (stream,"SMTP connection broken (command)");
632 fs_give ((void **) &s);
633 return ret;
637 /* Simple Mail Transfer Protocol get reply
638 * Accepts: SMTP stream
639 * Returns: reply code
642 long smtp_reply (SENDSTREAM *stream)
644 smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
645 long reply;
646 /* flush old reply */
647 if (stream->reply) fs_give ((void **) &stream->reply);
648 /* get reply */
649 if (stream->netstream && (stream->reply = net_getline (stream->netstream))) {
650 if (stream->debug) mm_dlog (stream->reply);
651 /* return response code */
652 reply = atol (stream->reply);
653 if (pv && (reply < 100)) (*pv) (stream->reply);
655 else reply = smtp_fake (stream,"SMTP connection broken (reply)");
656 return reply;
659 /* Simple Mail Transfer Protocol send EHLO
660 * Accepts: SMTP stream
661 * host name to use in EHLO
662 * NETMBX structure
663 * Returns: reply code
666 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
668 unsigned long i,j;
669 long flags = (mb->secflag ? AU_SECURE : NIL) |
670 (mb->authuser[0] ? AU_AUTHUSER : NIL);
671 char *s,*t,*r,tmp[MAILTMPLEN];
672 /* clear ESMTP data */
673 memset (&ESMTP,0,sizeof (ESMTP));
674 if (mb->loser) return 500; /* never do EHLO if a loser */
675 sprintf (tmp,"EHLO %s",host); /* build the complete command */
676 if (stream->debug) mm_dlog (tmp);
677 strcat (tmp,"\015\012");
678 /* send the command */
679 if (!net_soutr (stream->netstream,tmp))
680 return smtp_fake (stream,"SMTP connection broken (EHLO)");
681 /* got an OK reply? */
682 do if ((i = smtp_reply (stream)) == SMTPOK) {
683 /* hack for AUTH= */
684 if (stream->reply[4] && stream->reply[5] && stream->reply[6] &&
685 stream->reply[7] && (stream->reply[8] == '=')) stream->reply[8] = ' ';
686 /* get option code */
687 if (!(s = strtok_r (stream->reply+4," ",&r)));
688 /* have option, does it have a value */
689 else if ((t = strtok_r (NIL," ",&r)) && *t) {
690 /* EHLO options which take arguments */
691 if (!compare_cstring (s,"SIZE")) {
692 if (isdigit (*t)) ESMTP.size.limit = strtoul (t,&t,10);
693 ESMTP.size.ok = T;
695 else if (!compare_cstring (s,"DELIVERBY")) {
696 if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
697 ESMTP.deliverby.ok = T;
699 else if (!compare_cstring (s,"ATRN")) {
700 ESMTP.atrn.domains = cpystr (t);
701 ESMTP.atrn.ok = T;
703 else if (!compare_cstring (s,"AUTH"))
704 do if ((j = mail_lookup_auth_name (t,flags)) &&
705 (--j < MAXAUTHENTICATORS)) ESMTP.auth |= (1 << j);
706 while ((t = strtok_r (NIL," ",&r)) && *t);
708 /* EHLO options which do not take arguments */
709 else if (!compare_cstring (s,"SIZE")) ESMTP.size.ok = T;
710 else if (!compare_cstring (s,"8BITMIME")) ESMTP.eightbit.ok = T;
711 else if (!compare_cstring (s,"DSN")) ESMTP.dsn.ok = T;
712 else if (!compare_cstring (s,"ATRN")) ESMTP.atrn.ok = T;
713 else if (!compare_cstring (s,"SEND")) ESMTP.service.send = T;
714 else if (!compare_cstring (s,"SOML")) ESMTP.service.soml = T;
715 else if (!compare_cstring (s,"SAML")) ESMTP.service.saml = T;
716 else if (!compare_cstring (s,"EXPN")) ESMTP.service.expn = T;
717 else if (!compare_cstring (s,"HELP")) ESMTP.service.help = T;
718 else if (!compare_cstring (s,"TURN")) ESMTP.service.turn = T;
719 else if (!compare_cstring (s,"ETRN")) ESMTP.service.etrn = T;
720 else if (!compare_cstring (s,"STARTTLS")) ESMTP.service.starttls = T;
721 else if (!compare_cstring (s,"RELAY")) ESMTP.service.relay = T;
722 else if (!compare_cstring (s,"PIPELINING")) ESMTP.service.pipe = T;
723 else if (!compare_cstring (s,"ENHANCEDSTATUSCODES"))
724 ESMTP.service.ensc = T;
725 else if (!compare_cstring (s,"BINARYMIME")) ESMTP.service.bmime = T;
726 else if (!compare_cstring (s,"CHUNKING")) ESMTP.service.chunk = T;
728 while ((i < 100) || (stream->reply[3] == '-'));
729 /* disable LOGIN if PLAIN also advertised */
730 if ((j = mail_lookup_auth_name ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
731 (ESMTP.auth & (1 << j)) &&
732 (j = mail_lookup_auth_name ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
733 ESMTP.auth &= ~(1 << j);
734 return i; /* return the response code */
737 /* Simple Mail Transfer Protocol set fake error and abort
738 * Accepts: SMTP stream
739 * error text
740 * Returns: SMTPSOFTFATAL, always
743 long smtp_fake (SENDSTREAM *stream,char *text)
745 if (stream->netstream) { /* close net connection if still open */
746 net_close (stream->netstream);
747 stream->netstream = NIL;
749 /* set last error */
750 return smtp_seterror (stream,SMTPSOFTFATAL,text);
754 /* Simple Mail Transfer Protocol set error
755 * Accepts: SMTP stream
756 * SMTP error code
757 * error text
758 * Returns: error code
761 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
763 /* flush any old reply */
764 if (stream->reply ) fs_give ((void **) &stream->reply);
765 /* set up pseudo-reply string */
766 stream->reply = (char *) fs_get (20+strlen (text));
767 sprintf (stream->reply,"%ld %s",code,text);
768 return code; /* return error code */
772 /* Simple Mail Transfer Protocol filter mail
773 * Accepts: stream
774 * string
775 * Returns: T on success, NIL on failure
778 long smtp_soutr (void *stream,char *s)
780 char c,*t;
781 /* "." on first line */
782 if (s[0] == '.') net_sout (stream,".",1);
783 /* find lines beginning with a "." */
784 while (t = strstr (s,"\015\012.")) {
785 c = *(t += 3); /* remember next character after "." */
786 *t = '\0'; /* tie off string */
787 /* output prefix */
788 if (!net_sout (stream,s,t-s)) return NIL;
789 *t = c; /* restore delimiter */
790 s = t - 1; /* push pointer up to the "." */
792 /* output remainder of text */
793 return *s ? net_soutr (stream,s) : T;