* Implement a different way to delete a password from the cache.
[alpine.git] / imap / src / c-client / smtp.c
blob3abe13f28b67ba948feb9af21cd99413579bce5b
1 /* ========================================================================
2 * Copyright 2015-2022 Eduardo Chappa
3 * Copyright 2008 Mark Crispin
4 * ========================================================================
5 */
7 /*
8 * Program: Simple Mail Transfer Protocol (SMTP) routines
10 * Author: Mark Crispin
12 * Date: 27 July 1988
13 * Last Edited: 19 November 2008 (Crispin)
14 * Last Edited: 8 December 2018 (Chappa)
16 * Previous versions of this file were
18 * Copyright 1988-2008 University of Washington
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
24 * http://www.apache.org/licenses/LICENSE-2.0
26 * This original version of this file is
27 * Copyright 1988 Stanford University
28 * and was developed in the Symbolic Systems Resources Group of the Knowledge
29 * Systems Laboratory at Stanford University in 1987-88, and was funded by the
30 * Biomedical Research Technology Program of the National Institutes of Health
31 * under grant number RR-00785.
35 #include <ctype.h>
36 #include <stdio.h>
37 #include "c-client.h"
39 /* Constants */
41 #define SMTPSSLPORT (long) 465 /* former assigned SSL TCP contact port */
42 #define SMTPGREET (long) 220 /* SMTP successful greeting */
43 #define SMTPAUTHED (long) 235 /* SMTP successful authentication */
44 #define SMTPOK (long) 250 /* SMTP OK code */
45 #define SMTPAUTHREADY (long) 334/* SMTP ready for authentication */
46 #define SMTPREADY (long) 354 /* SMTP ready for data */
47 #define SMTPSOFTFATAL (long) 421/* SMTP soft fatal code */
48 #define SMTPWANTAUTH (long) 505 /* SMTP authentication needed */
49 #define SMTPWANTAUTH2 (long) 530/* SMTP authentication needed */
50 #define SMTPUNAVAIL (long) 550 /* SMTP mailbox unavailable */
51 #define SMTPHARDERROR (long) 554/* SMTP miscellaneous hard failure */
54 /* Convenient access to protocol-specific data */
56 #define ESMTP stream->protocol.esmtp
59 /* Function prototypes */
61 void *smtp_challenge (void *s,unsigned long *len);
62 long smtp_response (void *s,char *base,char *response,unsigned long size);
63 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp);
64 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error);
65 long smtp_send (SENDSTREAM *stream,char *command,char *args);
66 long smtp_reply (SENDSTREAM *stream);
67 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb);
68 long smtp_fake (SENDSTREAM *stream,char *text);
69 static long smtp_seterror (SENDSTREAM *stream,long code,char *text);
70 long smtp_soutr (void *stream,char *s);
72 /* Mailer parameters */
74 static unsigned long smtp_maxlogintrials = MAXLOGINTRIALS;
75 static long smtp_port = 0; /* default port override */
76 static long smtp_sslport = 0;
79 #ifndef RFC2821
80 #define RFC2821 /* RFC 2821 compliance */
81 #endif
83 /* SMTP limits, current as of RFC 2821 */
85 #define SMTPMAXLOCALPART 64
86 #define SMTPMAXDOMAIN 255
87 #define SMTPMAXPATH 256
90 /* I have seen local parts of more than 64 octets, in spite of the SMTP
91 * limits. So, we'll have a more generous limit that's still guaranteed
92 * not to pop the buffer, and let the server worry about it. As of this
93 * writing, it comes out to 240. Anyone with a mailbox name larger than
94 * that is in serious need of a life or at least a new ISP! 23 June 1998
97 #define MAXLOCALPART ((MAILTMPLEN - (SMTPMAXDOMAIN + SMTPMAXPATH + 32)) / 2)
99 /* Mail Transfer Protocol manipulate driver parameters
100 * Accepts: function code
101 * function-dependent value
102 * Returns: function-dependent return value
105 void *smtp_parameters (long function,void *value)
107 switch ((int) function) {
108 case SET_MAXLOGINTRIALS:
109 smtp_maxlogintrials = (unsigned long) value;
110 break;
111 case GET_MAXLOGINTRIALS:
112 value = (void *) smtp_maxlogintrials;
113 break;
114 case SET_SMTPPORT:
115 smtp_port = (long) value;
116 break;
117 case GET_SMTPPORT:
118 value = (void *) smtp_port;
119 break;
120 case SET_SSLSMTPPORT:
121 smtp_sslport = (long) value;
122 break;
123 case GET_SSLSMTPPORT:
124 value = (void *) smtp_sslport;
125 break;
126 default:
127 value = NIL; /* error case */
128 break;
130 return value;
133 /* Mail Transfer Protocol open connection
134 * Accepts: network driver
135 * service host list
136 * port number
137 * service name
138 * SMTP open options
139 * Returns: SEND stream on success, NIL on failure
142 SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
143 unsigned long port,long options)
145 SENDSTREAM *stream = NIL;
146 long reply;
147 char *s,tmp[MAILTMPLEN];
148 NETSTREAM *netstream;
149 NETMBX mb;
150 if (!(hostlist && *hostlist)) mm_log ("Missing SMTP service host",ERROR);
151 /* maximum domain name is 64 characters */
152 else do if (strlen (*hostlist) < SMTPMAXDOMAIN) {
153 sprintf (tmp,"{%.1000s}",*hostlist);
154 if (!mail_valid_net_parse_work (tmp,&mb,service ? service : "smtp") ||
155 mb.anoflag || mb.readonlyflag) {
156 sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
157 mm_log (tmp,ERROR);
159 else { /* light tryssl flag if requested */
160 mb.trysslflag = (options & SOP_TRYSSL) ? T : NIL;
161 /* explicit port overrides all */
162 if (mb.port) port = mb.port;
163 /* else /submit overrides port argument */
164 else if (!compare_cstring (mb.service,"submit")) {
165 port = SUBMITTCPPORT; /* override port, use IANA name */
166 strcpy (mb.service,"submission");
168 /* else port argument overrides SMTP port */
169 else if (!port) port = smtp_port ? smtp_port : SMTPTCPPORT;
170 if ((netstream = /* try to open ordinary connection */
171 net_open (&mb,dv,port,
172 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
173 "*smtps",smtp_sslport ? smtp_sslport : SMTPSSLPORT)) != NULL) {
174 stream = (SENDSTREAM *) memset (fs_get (sizeof (SENDSTREAM)),0,
175 sizeof (SENDSTREAM));
176 stream->netstream = netstream;
177 stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
178 net_host (netstream) : mb.host);
179 stream->debug = (mb.dbgflag || (options & OP_DEBUG)) ? T : NIL;
180 if (options & SOP_SECURE) mb.secflag = T;
181 /* get name of local host to use */
182 s = compare_cstring ("localhost",mb.host) ?
183 net_localhost (netstream) : "localhost";
185 do reply = smtp_reply (stream);
186 while ((reply < 100) || (stream->reply[3] == '-'));
187 if (reply != SMTPGREET){/* get SMTP greeting */
188 sprintf (tmp,"SMTP greeting failure: %.80s",stream->reply);
189 mm_log (tmp,ERROR);
190 stream = smtp_close (stream);
192 /* try EHLO first, then HELO */
193 else if (((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) &&
194 ((reply = smtp_send (stream,"HELO",s)) != SMTPOK)) {
195 sprintf (tmp,"SMTP hello failure: %.80s",stream->reply);
196 mm_log (tmp,ERROR);
197 stream = smtp_close (stream);
199 else {
200 NETDRIVER *ssld =(NETDRIVER *)mail_parameters(NIL,GET_SSLDRIVER,NIL);
201 sslstart_t stls = (sslstart_t) mail_parameters(NIL,GET_SSLSTART,NIL);
202 ESMTP.ok = T; /* ESMTP server, start TLS if present */
203 if (!dv && stls && ESMTP.service.starttls &&
204 !mb.sslflag && !mb.notlsflag &&
205 (smtp_send (stream,"STARTTLS",NIL) == SMTPGREET)) {
206 mb.tlsflag = T; /* TLS OK, get into TLS at this end */
207 stream->netstream->dtb = ssld;
208 /* TLS started, negotiate it */
209 if (!(stream->netstream->stream = (*stls)
210 (stream->netstream->stream,mb.host,
211 SSL_MTHD(mb) | (mb.novalidate ? NET_NOVALIDATECERT:NIL)))){
212 /* TLS negotiation failed after STARTTLS */
213 sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",
214 mb.host);
215 mm_log (tmp,ERROR);
216 /* close without doing QUIT */
217 if (stream->netstream) net_close (stream->netstream);
218 stream->netstream = NIL;
219 stream = smtp_close (stream);
221 /* TLS OK, re-negotiate EHLO */
222 else if ((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) {
223 sprintf (tmp,"SMTP EHLO failure after STARTTLS: %.80s",
224 stream->reply);
225 mm_log (tmp,ERROR);
226 stream = smtp_close (stream);
228 else ESMTP.ok = T; /* TLS OK and EHLO successful */
230 else if (mb.tlsflag) {/* user specified /tls but can't do it */
231 sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
232 mm_log (tmp,ERROR);
233 stream = smtp_close (stream);
236 /* remote name for authentication */
237 if (stream && ((mb.secflag || mb.user[0]))) {
238 if (ESMTP.auth) { /* use authenticator? */
239 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
240 /* remote name for authentication */
241 strncpy (mb.host,
242 (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
243 net_remotehost (netstream) : net_host (netstream),
244 NETMAXHOST-1);
245 mb.host[NETMAXHOST-1] = '\0';
247 if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
249 else { /* no available authenticators? */
250 sprintf (tmp,"%sSMTP authentication not available: %.80s",
251 mb.secflag ? "Secure " : "",mb.host);
252 mm_log (tmp,ERROR);
253 stream = smtp_close (stream);
259 } while (!stream && *++hostlist);
260 if (stream) { /* set stream options if have a stream */
261 if (options &(SOP_DSN | SOP_DSN_NOTIFY_FAILURE | SOP_DSN_NOTIFY_DELAY |
262 SOP_DSN_NOTIFY_SUCCESS | SOP_DSN_RETURN_FULL)) {
263 ESMTP.dsn.want = T;
264 if (options & SOP_DSN_NOTIFY_FAILURE) ESMTP.dsn.notify.failure = T;
265 if (options & SOP_DSN_NOTIFY_DELAY) ESMTP.dsn.notify.delay = T;
266 if (options & SOP_DSN_NOTIFY_SUCCESS) ESMTP.dsn.notify.success = T;
267 if (options & SOP_DSN_RETURN_FULL) ESMTP.dsn.full = T;
269 if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
271 return stream;
274 /* SMTP authenticate
275 * Accepts: stream to login
276 * parsed network mailbox structure
277 * scratch buffer
278 * place to return user name
279 * Returns: T on success, NIL on failure
282 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
284 unsigned long trial,auths, authsaved = NIL;
285 char *lsterr = NIL;
286 char usr[MAILTMPLEN], *base;
287 AUTHENTICATOR *at, *atsaved;
288 long ret = NIL;
289 for (auths = ESMTP.auth, stream->saslcancel = NIL;
290 !ret && stream->netstream && auths &&
291 (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
292 if(mb && *mb->auth){
293 if(!compare_cstring(at->name, mb->auth))
294 atsaved = at;
295 else{
296 authsaved = auths;
297 continue;
300 if (lsterr) { /* previous authenticator failed? */
301 sprintf (tmp,"Retrying using %s authentication after %.80s",
302 at->name,lsterr);
303 mm_log (tmp,NIL);
304 delete_password(mb, usr);
305 fs_give ((void **) &lsterr);
307 trial = 0; /* initial trial count */
308 tmp[0] = '\0'; /* empty buffer */
309 if (stream->netstream) do {
310 if (lsterr) {
311 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
312 mm_log (tmp,WARN);
313 fs_give ((void **) &lsterr);
314 delete_password(mb, usr);
316 if(at->flags & AU_SINGLE){
317 sprintf(tmp, "AUTH %s", at->name);
318 base = (char *) tmp;
320 else base = NIL;
321 stream->saslcancel = NIL;
322 if ((at->flags & AU_SINGLE) || smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
323 /* log what type of authentication we are about to try */
324 if ((at->flags & AU_SINGLE) && stream && stream->debug) mm_dlog(base);
325 /* hide client authentication responses */
326 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
327 if ((*at->client) (smtp_challenge,smtp_response,base,"smtp",mb,stream,
328 net_port(stream->netstream),&trial,usr)) {
329 if (stream->replycode == SMTPAUTHED) {
330 ESMTP.auth = NIL; /* disable authenticators */
331 ret = LONGT;
333 /* if main program requested cancellation */
334 else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
336 stream->sensitive = NIL;/* unhide */
338 /* remember response if error and no cancel */
339 if (!ret && trial) lsterr = cpystr (stream->reply);
340 } while (!ret && stream->netstream && trial &&
341 (trial < smtp_maxlogintrials));
343 if (lsterr) { /* previous authenticator failed? */
344 if (!stream->saslcancel) { /* don't do this if a cancel */
345 sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
346 mm_log (tmp,ERROR);
348 if(!ret && stream->netstream)
349 delete_password(mb, usr);
350 fs_give ((void **) &lsterr);
352 if(mb && *mb->auth){
353 if(!authsaved) sprintf (tmp, "Client does not support AUTH=%.80s authenticator",mb->auth);
354 else if (!atsaved) sprintf (tmp,"SMTP server does not support AUTH=%.80s authenticator",mb->auth);
355 if (!authsaved || !atsaved) mm_log (tmp,ERROR);
357 return ret; /* authentication failed */
360 /* Get challenge to authenticator in binary
361 * Accepts: stream
362 * pointer to returned size
363 * Returns: challenge or NIL if not challenge
366 void *smtp_challenge (void *s,unsigned long *len)
368 char tmp[MAILTMPLEN];
369 void *ret = NIL;
370 SENDSTREAM *stream = (SENDSTREAM *) s;
371 if ((stream->replycode == SMTPAUTHREADY) &&
372 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
373 strlen (stream->reply + 4),len))) {
374 sprintf (tmp,"SMTP SERVER BUG (invalid challenge, continuing): %.80s",stream->reply+4);
375 mm_log (tmp,ERROR);
376 ret = cpystr(""); /* This is silly: fake a reply, it will be ignored */
378 return ret;
382 /* Send authenticator response in BASE64
383 * Accepts: MAIL stream
384 * string to send
385 * length of string
386 * Returns: T, always
389 long smtp_response (void *s,char* base,char *response,unsigned long size)
391 SENDSTREAM *stream = (SENDSTREAM *) s;
392 unsigned long i,j;
393 char *t,*u;
394 if (response) { /* make CRLFless BASE64 string */
395 if (size) {
396 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
397 j < i; j++) if (t[j] > ' ') *u++ = t[j];
398 *u = '\0'; /* tie off string */
399 i = base ? smtp_send(stream, base, t) : smtp_send (stream,t,NIL);
400 fs_give ((void **) &t);
402 else i = smtp_send (stream,"",NIL);
404 else { /* abort requested */
405 i = base ? 0L : smtp_send (stream,"*",NIL);
406 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
408 return LONGT;
411 /* Mail Transfer Protocol close connection
412 * Accepts: SEND stream
413 * Returns: NIL always
416 SENDSTREAM *smtp_close (SENDSTREAM *stream)
418 if (stream) { /* send "QUIT" */
419 if (stream->netstream) { /* do close actions if have netstream */
420 smtp_send (stream,"QUIT",NIL);
421 if (stream->netstream) net_close (stream->netstream);
423 /* clean up */
424 if (stream->host) fs_give ((void **) &stream->host);
425 if (stream->reply) fs_give ((void **) &stream->reply);
426 if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
427 if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
428 fs_give ((void **) &stream);/* flush the stream */
430 return NIL;
433 /* Mail Transfer Protocol deliver mail
434 * Accepts: SEND stream
435 * delivery option (MAIL, SEND, SAML, SOML)
436 * message envelope
437 * message body
438 * Returns: T on success, NIL on failure
441 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
443 RFC822BUFFER buf;
444 char tmp[SENDBUFLEN+1], smtpserver[SENDBUFLEN+1], *error_string;
445 long error = NIL, i;
446 long retry = NIL;
447 buf.f = smtp_soutr; /* initialize buffer */
448 buf.s = stream->netstream;
449 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
450 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
451 smtpserver[SENDBUFLEN] = '\0';
452 if (!(env->to || env->cc || env->bcc)) {
453 /* no recipients in request */
454 smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
455 return NIL;
457 /* get this now in case the rug is pulled from under us */
458 sprintf (smtpserver,"{%.200s/smtp%s}<none>",
459 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
460 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
461 net_remotehost (stream->netstream) :
462 net_host (stream->netstream)) :
463 stream->host,
464 (stream->netstream->dtb ==
465 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
466 "/ssl" : "");
467 do {
468 if (retry) { /* need to retry with authentication? */
469 NETMBX mb;
470 /* make sure stream is in good shape */
471 smtp_send (stream,"RSET",NIL);
472 /* yes, build remote name for authentication */
473 mail_valid_net_parse (smtpserver,&mb);
474 if (!smtp_auth (stream,&mb,smtpserver)) return NIL;
475 retry = NIL; /* no retry at this point */
478 strcpy (tmp,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
479 #ifdef RFC2821
480 if (env->return_path && env->return_path->host &&
481 !((strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
482 (strlen (env->return_path->host) > SMTPMAXDOMAIN))) {
483 rfc822_cat (tmp,env->return_path->mailbox,NIL);
484 sprintf (tmp + strlen (tmp),"@%s",env->return_path->host);
486 #else /* old code with A-D-L support */
487 if (env->return_path && env->return_path->host &&
488 !((env->return_path->adl &&
489 (strlen (env->return_path->adl) > SMTPMAXPATH)) ||
490 (strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
491 (strlen (env->return_path->host) > SMTPMAXDOMAIN)))
492 rfc822_address (tmp,env->return_path);
493 #endif
494 strcat (tmp,">");
495 if (ESMTP.ok) {
496 if (ESMTP.eightbit.ok && ESMTP.eightbit.want)
497 strcat (tmp," BODY=8BITMIME");
498 if (ESMTP.dsn.ok && ESMTP.dsn.want) {
499 strcat (tmp,ESMTP.dsn.full ? " RET=FULL" : " RET=HDRS");
500 if (ESMTP.dsn.envid)
501 sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
504 /* send "MAIL FROM" command */
505 switch (i = smtp_send (stream,type,tmp)) {
506 case SMTPUNAVAIL: /* mailbox unavailable? */
507 case SMTPWANTAUTH: /* wants authentication? */
508 case SMTPWANTAUTH2:
509 if (ESMTP.auth) retry = T;/* yes, retry with authentication */
510 case SMTPOK: /* looks good */
511 break;
512 default: /* other failure */
513 error_string = stream && stream->reply ? cpystr(stream->reply) : NIL;
514 smtp_send (stream,"RSET",NIL);
515 if(error_string){ /* report it */
516 smtp_seterror(stream, i, error_string);
517 fs_give((void **) &error_string);
519 return NIL;
521 /* negotiate the recipients */
522 if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
523 if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
524 if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
525 if (!retry && error) { /* any recipients failed? */
526 smtp_send (stream,"RSET",NIL);
527 smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
528 return NIL;
530 } while (retry);
531 /* negotiate data command */
532 if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) {
533 smtp_send (stream,"RSET",NIL);
534 return NIL;
536 /* send message data */
537 if (!rfc822_output_full (&buf,env,body,
538 ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
539 smtp_fake (stream,"SMTP connection broken (message data)");
540 return NIL; /* can't do much else here */
542 /* send trailing dot */
543 if (smtp_send (stream,".",NIL) != SMTPOK) {
544 smtp_send (stream,"RSET",NIL);
545 return NIL;
547 return LONGT;
550 /* Simple Mail Transfer Protocol send VERBose
551 * Accepts: SMTP stream
552 * Returns: T if successful, else NIL
554 * Descriptive text formerly in [al]pine sources:
555 * At worst, this command may cause the SMTP connection to get nuked. Modern
556 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
557 * arg is for PMDF). What's more, if it works, the reply code and accompanying
558 * text may vary from server to server.
561 long smtp_verbose (SENDSTREAM *stream)
563 /* accept any 2xx reply code */
564 return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
567 /* Internal routines */
570 /* Simple Mail Transfer Protocol send recipient
571 * Accepts: SMTP stream
572 * address list
573 * pointer to error flag
574 * Returns: T if should retry, else NIL
577 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
579 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
580 while (adr) { /* for each address on the list */
581 /* clear any former error */
582 if (adr->error) fs_give ((void **) &adr->error);
583 if (adr->host) { /* ignore group syntax */
584 /* enforce SMTP limits to protect the buffer */
585 if (strlen (adr->mailbox) > MAXLOCALPART) {
586 adr->error = cpystr ("501 Recipient name too long");
587 *error = T;
589 else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
590 adr->error = cpystr ("501 Recipient domain too long");
591 *error = T;
593 #ifndef RFC2821 /* old code with A-D-L support */
594 else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
595 adr->error = cpystr ("501 Path too long");
596 *error = T;
598 #endif
600 else {
601 strcpy (tmp,"TO:<"); /* compose "RCPT TO:<return-path>" */
602 #ifdef RFC2821
603 rfc822_cat (tmp,adr->mailbox,NIL);
604 sprintf (tmp + strlen (tmp),"@%s>",adr->host);
605 #else /* old code with A-D-L support */
606 rfc822_address (tmp,adr);
607 strcat (tmp,">");
608 #endif
609 /* want notifications */
610 if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
611 /* yes, start with prefix */
612 strcat (tmp," NOTIFY=");
613 s = tmp + strlen (tmp);
614 if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
615 if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
616 if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
617 /* tie off last comma */
618 if (*s) s[strlen (s) - 1] = '\0';
619 else strcat (tmp,"NEVER");
620 if (adr->orcpt.addr) {
621 sprintf (orcpt,"%.498s;%.498s",
622 adr->orcpt.type ? adr->orcpt.type : "rfc822",
623 adr->orcpt.addr);
624 sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
627 switch (smtp_send (stream,"RCPT",tmp)) {
628 case SMTPOK: /* looks good */
629 break;
630 case SMTPUNAVAIL: /* mailbox unavailable? */
631 case SMTPWANTAUTH: /* wants authentication? */
632 case SMTPWANTAUTH2:
633 if (ESMTP.auth) return T;
634 default: /* other failure */
635 *error = T; /* note that an error occurred */
636 adr->error = cpystr (stream->reply);
640 adr = adr->next; /* do any subsequent recipients */
642 return NIL; /* no retry called for */
645 /* Simple Mail Transfer Protocol send command
646 * Accepts: SEND stream
647 * text
648 * Returns: reply code
651 long smtp_send (SENDSTREAM *stream,char *command,char *args)
653 long ret;
654 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
655 + 3);
656 /* build the complete command */
657 if (args) sprintf (s,"%s %s",command,args);
658 else strcpy (s,command);
659 if (stream->debug) mail_dlog (s,stream->sensitive);
660 strcat (s,"\015\012");
661 /* send the command */
662 if (stream->netstream && net_soutr (stream->netstream,s)) {
663 do stream->replycode = smtp_reply (stream);
664 while ((stream->replycode < 100) || (stream->reply[3] == '-'));
665 ret = stream->replycode;
667 else ret = smtp_fake (stream,"SMTP connection broken (command)");
668 fs_give ((void **) &s);
669 return ret;
673 /* Simple Mail Transfer Protocol get reply
674 * Accepts: SMTP stream
675 * Returns: reply code
678 long smtp_reply (SENDSTREAM *stream)
680 smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
681 long reply;
682 /* flush old reply */
683 if (stream->reply) fs_give ((void **) &stream->reply);
684 /* get reply */
685 if (stream->netstream && (stream->reply = net_getline (stream->netstream))) {
686 if (stream->debug) mm_dlog (stream->reply);
687 /* return response code */
688 reply = atol (stream->reply);
689 if (pv && (reply < 100)) (*pv) (stream->reply);
691 else reply = smtp_fake (stream,"SMTP connection broken (reply)");
692 return reply;
695 /* Simple Mail Transfer Protocol send EHLO
696 * Accepts: SMTP stream
697 * host name to use in EHLO
698 * NETMBX structure
699 * Returns: reply code
702 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
704 unsigned long i,j;
705 long flags = (mb->secflag ? AU_SECURE : NIL) |
706 (mb->authuser[0] ? AU_AUTHUSER : NIL);
707 char *s,*t,*r,tmp[MAILTMPLEN];
708 /* clear ESMTP data */
709 memset (&ESMTP,0,sizeof (ESMTP));
710 if (mb->loser) return 500; /* never do EHLO if a loser */
711 sprintf (tmp,"EHLO %s",host); /* build the complete command */
712 if (stream->debug) mm_dlog (tmp);
713 strcat (tmp,"\015\012");
714 /* send the command */
715 if (!net_soutr (stream->netstream,tmp))
716 return smtp_fake (stream,"SMTP connection broken (EHLO)");
717 /* got an OK reply? */
718 do if ((i = smtp_reply (stream)) == SMTPOK) {
719 /* hack for AUTH= */
720 if (stream->reply[4] && stream->reply[5] && stream->reply[6] &&
721 stream->reply[7] && (stream->reply[8] == '=')) stream->reply[8] = ' ';
722 /* get option code */
723 if (!(s = strtok_r (stream->reply+4," ",&r)));
724 /* have option, does it have a value */
725 else if ((t = strtok_r (NIL," ",&r)) && *t) {
726 /* EHLO options which take arguments */
727 if (!compare_cstring (s,"SIZE")) {
728 if (isdigit (*t)) ESMTP.size.limit = strtoul (t,&t,10);
729 ESMTP.size.ok = T;
731 else if (!compare_cstring (s,"DELIVERBY")) {
732 if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
733 ESMTP.deliverby.ok = T;
735 else if (!compare_cstring (s,"ATRN")) {
736 ESMTP.atrn.domains = cpystr (t);
737 ESMTP.atrn.ok = T;
739 else if (!compare_cstring (s,"AUTH"))
740 do if ((j = mail_lookup_auth_name (t,flags)) &&
741 (--j < MAXAUTHENTICATORS)) ESMTP.auth |= (1 << j);
742 while ((t = strtok_r (NIL," ",&r)) && *t);
744 /* EHLO options which do not take arguments */
745 else if (!compare_cstring (s,"SIZE")) ESMTP.size.ok = T;
746 else if (!compare_cstring (s,"8BITMIME")) ESMTP.eightbit.ok = T;
747 else if (!compare_cstring (s,"DSN")) ESMTP.dsn.ok = T;
748 else if (!compare_cstring (s,"ATRN")) ESMTP.atrn.ok = T;
749 else if (!compare_cstring (s,"SEND")) ESMTP.service.send = T;
750 else if (!compare_cstring (s,"SOML")) ESMTP.service.soml = T;
751 else if (!compare_cstring (s,"SAML")) ESMTP.service.saml = T;
752 else if (!compare_cstring (s,"EXPN")) ESMTP.service.expn = T;
753 else if (!compare_cstring (s,"HELP")) ESMTP.service.help = T;
754 else if (!compare_cstring (s,"TURN")) ESMTP.service.turn = T;
755 else if (!compare_cstring (s,"ETRN")) ESMTP.service.etrn = T;
756 else if (!compare_cstring (s,"STARTTLS")) ESMTP.service.starttls = T;
757 else if (!compare_cstring (s,"RELAY")) ESMTP.service.relay = T;
758 else if (!compare_cstring (s,"PIPELINING")) ESMTP.service.pipe = T;
759 else if (!compare_cstring (s,"ENHANCEDSTATUSCODES"))
760 ESMTP.service.ensc = T;
761 else if (!compare_cstring (s,"BINARYMIME")) ESMTP.service.bmime = T;
762 else if (!compare_cstring (s,"CHUNKING")) ESMTP.service.chunk = T;
764 while ((i < 100) || (stream->reply[3] == '-'));
765 /* disable LOGIN if PLAIN also advertised */
766 if ((j = mail_lookup_auth_name ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
767 (ESMTP.auth & (1 << j)) &&
768 (j = mail_lookup_auth_name ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
769 ESMTP.auth &= ~(1 << j);
770 return i; /* return the response code */
773 /* Simple Mail Transfer Protocol set fake error and abort
774 * Accepts: SMTP stream
775 * error text
776 * Returns: SMTPSOFTFATAL, always
779 long smtp_fake (SENDSTREAM *stream,char *text)
781 if (stream->netstream) { /* close net connection if still open */
782 net_close (stream->netstream);
783 stream->netstream = NIL;
785 /* set last error */
786 return smtp_seterror (stream,SMTPSOFTFATAL,text);
790 /* Simple Mail Transfer Protocol set error
791 * Accepts: SMTP stream
792 * SMTP error code
793 * error text
794 * Returns: error code
797 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
799 /* flush any old reply */
800 if (stream->reply ) fs_give ((void **) &stream->reply);
801 /* set up pseudo-reply string */
802 stream->reply = (char *) fs_get (20+strlen (text));
803 sprintf (stream->reply,"%ld %s",code,text);
804 return code; /* return error code */
808 /* Simple Mail Transfer Protocol filter mail
809 * Accepts: stream
810 * string
811 * Returns: T on success, NIL on failure
814 long smtp_soutr (void *stream,char *s)
816 char c,*t;
817 /* "." on first line */
818 if (s[0] == '.') net_sout (stream,".",1);
819 /* find lines beginning with a "." */
820 while ((t = strstr (s,"\015\012.")) != NULL) {
821 c = *(t += 3); /* remember next character after "." */
822 *t = '\0'; /* tie off string */
823 /* output prefix */
824 if (!net_sout (stream,s,t-s)) return NIL;
825 *t = c; /* restore delimiter */
826 s = t - 1; /* push pointer up to the "." */
828 /* output remainder of text */
829 return *s ? net_soutr (stream,s) : T;