* Add support for the OAUTHBEARER authentication method in Gmail. Thanks to
[alpine.git] / imap / src / c-client / smtp.c
blob96abd5faa599d3743d53d677fdd1a3f69337ecf3
1 /* ========================================================================
2 * Copyright 2015-2020 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 *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;
285 char *lsterr = NIL;
286 char usr[MAILTMPLEN];
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 fs_give ((void **) &lsterr);
306 trial = 0; /* initial trial count */
307 tmp[0] = '\0'; /* empty buffer */
308 if (stream->netstream) do {
309 if (lsterr) {
310 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
311 mm_log (tmp,WARN);
312 fs_give ((void **) &lsterr);
314 stream->saslcancel = NIL;
315 if (smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
316 /* hide client authentication responses */
317 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
318 if ((*at->client) (smtp_challenge,smtp_response,"smtp",mb,stream,
319 net_port(stream->netstream),&trial,usr)) {
320 if (stream->replycode == SMTPAUTHED) {
321 ESMTP.auth = NIL; /* disable authenticators */
322 ret = LONGT;
324 /* if main program requested cancellation */
325 else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
327 stream->sensitive = NIL;/* unhide */
329 /* remember response if error and no cancel */
330 if (!ret && trial) lsterr = cpystr (stream->reply);
331 } while (!ret && stream->netstream && trial &&
332 (trial < smtp_maxlogintrials));
334 if (lsterr) { /* previous authenticator failed? */
335 if (!stream->saslcancel) { /* don't do this if a cancel */
336 sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
337 mm_log (tmp,ERROR);
339 fs_give ((void **) &lsterr);
341 if(mb && *mb->auth){
342 if(!authsaved) sprintf (tmp, "Client does not support AUTH=%.80s authenticator",mb->auth);
343 else if (!atsaved) sprintf (tmp,"SMTP server does not support AUTH=%.80s authenticator",mb->auth);
344 if (!authsaved || !atsaved) mm_log (tmp,ERROR);
346 return ret; /* authentication failed */
349 /* Get challenge to authenticator in binary
350 * Accepts: stream
351 * pointer to returned size
352 * Returns: challenge or NIL if not challenge
355 void *smtp_challenge (void *s,unsigned long *len)
357 char tmp[MAILTMPLEN];
358 void *ret = NIL;
359 SENDSTREAM *stream = (SENDSTREAM *) s;
360 if ((stream->replycode == SMTPAUTHREADY) &&
361 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
362 strlen (stream->reply + 4),len))) {
363 sprintf (tmp,"SMTP SERVER BUG (invalid challenge, continuing): %.80s",stream->reply+4);
364 mm_log (tmp,ERROR);
365 ret = cpystr(""); /* This is silly: fake a reply, it will be ignored */
367 return ret;
371 /* Send authenticator response in BASE64
372 * Accepts: MAIL stream
373 * string to send
374 * length of string
375 * Returns: T, always
378 long smtp_response (void *s,char *response,unsigned long size)
380 SENDSTREAM *stream = (SENDSTREAM *) s;
381 unsigned long i,j;
382 char *t,*u;
383 if (response) { /* make CRLFless BASE64 string */
384 if (size) {
385 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
386 j < i; j++) if (t[j] > ' ') *u++ = t[j];
387 *u = '\0'; /* tie off string */
388 i = smtp_send (stream,t,NIL);
389 fs_give ((void **) &t);
391 else i = smtp_send (stream,"",NIL);
393 else { /* abort requested */
394 i = smtp_send (stream,"*",NIL);
395 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
397 return LONGT;
400 /* Mail Transfer Protocol close connection
401 * Accepts: SEND stream
402 * Returns: NIL always
405 SENDSTREAM *smtp_close (SENDSTREAM *stream)
407 if (stream) { /* send "QUIT" */
408 if (stream->netstream) { /* do close actions if have netstream */
409 smtp_send (stream,"QUIT",NIL);
410 if (stream->netstream) net_close (stream->netstream);
412 /* clean up */
413 if (stream->host) fs_give ((void **) &stream->host);
414 if (stream->reply) fs_give ((void **) &stream->reply);
415 if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
416 if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
417 fs_give ((void **) &stream);/* flush the stream */
419 return NIL;
422 /* Mail Transfer Protocol deliver mail
423 * Accepts: SEND stream
424 * delivery option (MAIL, SEND, SAML, SOML)
425 * message envelope
426 * message body
427 * Returns: T on success, NIL on failure
430 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
432 RFC822BUFFER buf;
433 char tmp[SENDBUFLEN+1], smtpserver[SENDBUFLEN+1], *error_string;
434 long error = NIL, i;
435 long retry = NIL;
436 buf.f = smtp_soutr; /* initialize buffer */
437 buf.s = stream->netstream;
438 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
439 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
440 smtpserver[SENDBUFLEN] = '\0';
441 if (!(env->to || env->cc || env->bcc)) {
442 /* no recipients in request */
443 smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
444 return NIL;
446 /* get this now in case the rug is pulled from under us */
447 sprintf (smtpserver,"{%.200s/smtp%s}<none>",
448 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
449 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
450 net_remotehost (stream->netstream) :
451 net_host (stream->netstream)) :
452 stream->host,
453 (stream->netstream->dtb ==
454 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
455 "/ssl" : "");
456 do {
457 if (retry) { /* need to retry with authentication? */
458 NETMBX mb;
459 /* make sure stream is in good shape */
460 smtp_send (stream,"RSET",NIL);
461 /* yes, build remote name for authentication */
462 mail_valid_net_parse (smtpserver,&mb);
463 if (!smtp_auth (stream,&mb,smtpserver)) return NIL;
464 retry = NIL; /* no retry at this point */
467 strcpy (tmp,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
468 #ifdef RFC2821
469 if (env->return_path && env->return_path->host &&
470 !((strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
471 (strlen (env->return_path->host) > SMTPMAXDOMAIN))) {
472 rfc822_cat (tmp,env->return_path->mailbox,NIL);
473 sprintf (tmp + strlen (tmp),"@%s",env->return_path->host);
475 #else /* old code with A-D-L support */
476 if (env->return_path && env->return_path->host &&
477 !((env->return_path->adl &&
478 (strlen (env->return_path->adl) > SMTPMAXPATH)) ||
479 (strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
480 (strlen (env->return_path->host) > SMTPMAXDOMAIN)))
481 rfc822_address (tmp,env->return_path);
482 #endif
483 strcat (tmp,">");
484 if (ESMTP.ok) {
485 if (ESMTP.eightbit.ok && ESMTP.eightbit.want)
486 strcat (tmp," BODY=8BITMIME");
487 if (ESMTP.dsn.ok && ESMTP.dsn.want) {
488 strcat (tmp,ESMTP.dsn.full ? " RET=FULL" : " RET=HDRS");
489 if (ESMTP.dsn.envid)
490 sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
493 /* send "MAIL FROM" command */
494 switch (i = smtp_send (stream,type,tmp)) {
495 case SMTPUNAVAIL: /* mailbox unavailable? */
496 case SMTPWANTAUTH: /* wants authentication? */
497 case SMTPWANTAUTH2:
498 if (ESMTP.auth) retry = T;/* yes, retry with authentication */
499 case SMTPOK: /* looks good */
500 break;
501 default: /* other failure */
502 error_string = stream && stream->reply ? cpystr(stream->reply) : NIL;
503 smtp_send (stream,"RSET",NIL);
504 if(error_string){ /* report it */
505 smtp_seterror(stream, i, error_string);
506 fs_give((void **) &error_string);
508 return NIL;
510 /* negotiate the recipients */
511 if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
512 if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
513 if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
514 if (!retry && error) { /* any recipients failed? */
515 smtp_send (stream,"RSET",NIL);
516 smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
517 return NIL;
519 } while (retry);
520 /* negotiate data command */
521 if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) {
522 smtp_send (stream,"RSET",NIL);
523 return NIL;
525 /* send message data */
526 if (!rfc822_output_full (&buf,env,body,
527 ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
528 smtp_fake (stream,"SMTP connection broken (message data)");
529 return NIL; /* can't do much else here */
531 /* send trailing dot */
532 if (smtp_send (stream,".",NIL) != SMTPOK) {
533 smtp_send (stream,"RSET",NIL);
534 return NIL;
536 return LONGT;
539 /* Simple Mail Transfer Protocol send VERBose
540 * Accepts: SMTP stream
541 * Returns: T if successful, else NIL
543 * Descriptive text formerly in [al]pine sources:
544 * At worst, this command may cause the SMTP connection to get nuked. Modern
545 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
546 * arg is for PMDF). What's more, if it works, the reply code and accompanying
547 * text may vary from server to server.
550 long smtp_verbose (SENDSTREAM *stream)
552 /* accept any 2xx reply code */
553 return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
556 /* Internal routines */
559 /* Simple Mail Transfer Protocol send recipient
560 * Accepts: SMTP stream
561 * address list
562 * pointer to error flag
563 * Returns: T if should retry, else NIL
566 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
568 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
569 while (adr) { /* for each address on the list */
570 /* clear any former error */
571 if (adr->error) fs_give ((void **) &adr->error);
572 if (adr->host) { /* ignore group syntax */
573 /* enforce SMTP limits to protect the buffer */
574 if (strlen (adr->mailbox) > MAXLOCALPART) {
575 adr->error = cpystr ("501 Recipient name too long");
576 *error = T;
578 else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
579 adr->error = cpystr ("501 Recipient domain too long");
580 *error = T;
582 #ifndef RFC2821 /* old code with A-D-L support */
583 else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
584 adr->error = cpystr ("501 Path too long");
585 *error = T;
587 #endif
589 else {
590 strcpy (tmp,"TO:<"); /* compose "RCPT TO:<return-path>" */
591 #ifdef RFC2821
592 rfc822_cat (tmp,adr->mailbox,NIL);
593 sprintf (tmp + strlen (tmp),"@%s>",adr->host);
594 #else /* old code with A-D-L support */
595 rfc822_address (tmp,adr);
596 strcat (tmp,">");
597 #endif
598 /* want notifications */
599 if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
600 /* yes, start with prefix */
601 strcat (tmp," NOTIFY=");
602 s = tmp + strlen (tmp);
603 if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
604 if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
605 if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
606 /* tie off last comma */
607 if (*s) s[strlen (s) - 1] = '\0';
608 else strcat (tmp,"NEVER");
609 if (adr->orcpt.addr) {
610 sprintf (orcpt,"%.498s;%.498s",
611 adr->orcpt.type ? adr->orcpt.type : "rfc822",
612 adr->orcpt.addr);
613 sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
616 switch (smtp_send (stream,"RCPT",tmp)) {
617 case SMTPOK: /* looks good */
618 break;
619 case SMTPUNAVAIL: /* mailbox unavailable? */
620 case SMTPWANTAUTH: /* wants authentication? */
621 case SMTPWANTAUTH2:
622 if (ESMTP.auth) return T;
623 default: /* other failure */
624 *error = T; /* note that an error occurred */
625 adr->error = cpystr (stream->reply);
629 adr = adr->next; /* do any subsequent recipients */
631 return NIL; /* no retry called for */
634 /* Simple Mail Transfer Protocol send command
635 * Accepts: SEND stream
636 * text
637 * Returns: reply code
640 long smtp_send (SENDSTREAM *stream,char *command,char *args)
642 long ret;
643 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
644 + 3);
645 /* build the complete command */
646 if (args) sprintf (s,"%s %s",command,args);
647 else strcpy (s,command);
648 if (stream->debug) mail_dlog (s,stream->sensitive);
649 strcat (s,"\015\012");
650 /* send the command */
651 if (stream->netstream && net_soutr (stream->netstream,s)) {
652 do stream->replycode = smtp_reply (stream);
653 while ((stream->replycode < 100) || (stream->reply[3] == '-'));
654 ret = stream->replycode;
656 else ret = smtp_fake (stream,"SMTP connection broken (command)");
657 fs_give ((void **) &s);
658 return ret;
662 /* Simple Mail Transfer Protocol get reply
663 * Accepts: SMTP stream
664 * Returns: reply code
667 long smtp_reply (SENDSTREAM *stream)
669 smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
670 long reply;
671 /* flush old reply */
672 if (stream->reply) fs_give ((void **) &stream->reply);
673 /* get reply */
674 if (stream->netstream && (stream->reply = net_getline (stream->netstream))) {
675 if (stream->debug) mm_dlog (stream->reply);
676 /* return response code */
677 reply = atol (stream->reply);
678 if (pv && (reply < 100)) (*pv) (stream->reply);
680 else reply = smtp_fake (stream,"SMTP connection broken (reply)");
681 return reply;
684 /* Simple Mail Transfer Protocol send EHLO
685 * Accepts: SMTP stream
686 * host name to use in EHLO
687 * NETMBX structure
688 * Returns: reply code
691 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
693 unsigned long i,j;
694 long flags = (mb->secflag ? AU_SECURE : NIL) |
695 (mb->authuser[0] ? AU_AUTHUSER : NIL);
696 char *s,*t,*r,tmp[MAILTMPLEN];
697 /* clear ESMTP data */
698 memset (&ESMTP,0,sizeof (ESMTP));
699 if (mb->loser) return 500; /* never do EHLO if a loser */
700 sprintf (tmp,"EHLO %s",host); /* build the complete command */
701 if (stream->debug) mm_dlog (tmp);
702 strcat (tmp,"\015\012");
703 /* send the command */
704 if (!net_soutr (stream->netstream,tmp))
705 return smtp_fake (stream,"SMTP connection broken (EHLO)");
706 /* got an OK reply? */
707 do if ((i = smtp_reply (stream)) == SMTPOK) {
708 /* hack for AUTH= */
709 if (stream->reply[4] && stream->reply[5] && stream->reply[6] &&
710 stream->reply[7] && (stream->reply[8] == '=')) stream->reply[8] = ' ';
711 /* get option code */
712 if (!(s = strtok_r (stream->reply+4," ",&r)));
713 /* have option, does it have a value */
714 else if ((t = strtok_r (NIL," ",&r)) && *t) {
715 /* EHLO options which take arguments */
716 if (!compare_cstring (s,"SIZE")) {
717 if (isdigit (*t)) ESMTP.size.limit = strtoul (t,&t,10);
718 ESMTP.size.ok = T;
720 else if (!compare_cstring (s,"DELIVERBY")) {
721 if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
722 ESMTP.deliverby.ok = T;
724 else if (!compare_cstring (s,"ATRN")) {
725 ESMTP.atrn.domains = cpystr (t);
726 ESMTP.atrn.ok = T;
728 else if (!compare_cstring (s,"AUTH"))
729 do if ((j = mail_lookup_auth_name (t,flags)) &&
730 (--j < MAXAUTHENTICATORS)) ESMTP.auth |= (1 << j);
731 while ((t = strtok_r (NIL," ",&r)) && *t);
733 /* EHLO options which do not take arguments */
734 else if (!compare_cstring (s,"SIZE")) ESMTP.size.ok = T;
735 else if (!compare_cstring (s,"8BITMIME")) ESMTP.eightbit.ok = T;
736 else if (!compare_cstring (s,"DSN")) ESMTP.dsn.ok = T;
737 else if (!compare_cstring (s,"ATRN")) ESMTP.atrn.ok = T;
738 else if (!compare_cstring (s,"SEND")) ESMTP.service.send = T;
739 else if (!compare_cstring (s,"SOML")) ESMTP.service.soml = T;
740 else if (!compare_cstring (s,"SAML")) ESMTP.service.saml = T;
741 else if (!compare_cstring (s,"EXPN")) ESMTP.service.expn = T;
742 else if (!compare_cstring (s,"HELP")) ESMTP.service.help = T;
743 else if (!compare_cstring (s,"TURN")) ESMTP.service.turn = T;
744 else if (!compare_cstring (s,"ETRN")) ESMTP.service.etrn = T;
745 else if (!compare_cstring (s,"STARTTLS")) ESMTP.service.starttls = T;
746 else if (!compare_cstring (s,"RELAY")) ESMTP.service.relay = T;
747 else if (!compare_cstring (s,"PIPELINING")) ESMTP.service.pipe = T;
748 else if (!compare_cstring (s,"ENHANCEDSTATUSCODES"))
749 ESMTP.service.ensc = T;
750 else if (!compare_cstring (s,"BINARYMIME")) ESMTP.service.bmime = T;
751 else if (!compare_cstring (s,"CHUNKING")) ESMTP.service.chunk = T;
753 while ((i < 100) || (stream->reply[3] == '-'));
754 /* disable LOGIN if PLAIN also advertised */
755 if ((j = mail_lookup_auth_name ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
756 (ESMTP.auth & (1 << j)) &&
757 (j = mail_lookup_auth_name ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
758 ESMTP.auth &= ~(1 << j);
759 return i; /* return the response code */
762 /* Simple Mail Transfer Protocol set fake error and abort
763 * Accepts: SMTP stream
764 * error text
765 * Returns: SMTPSOFTFATAL, always
768 long smtp_fake (SENDSTREAM *stream,char *text)
770 if (stream->netstream) { /* close net connection if still open */
771 net_close (stream->netstream);
772 stream->netstream = NIL;
774 /* set last error */
775 return smtp_seterror (stream,SMTPSOFTFATAL,text);
779 /* Simple Mail Transfer Protocol set error
780 * Accepts: SMTP stream
781 * SMTP error code
782 * error text
783 * Returns: error code
786 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
788 /* flush any old reply */
789 if (stream->reply ) fs_give ((void **) &stream->reply);
790 /* set up pseudo-reply string */
791 stream->reply = (char *) fs_get (20+strlen (text));
792 sprintf (stream->reply,"%ld %s",code,text);
793 return code; /* return error code */
797 /* Simple Mail Transfer Protocol filter mail
798 * Accepts: stream
799 * string
800 * Returns: T on success, NIL on failure
803 long smtp_soutr (void *stream,char *s)
805 char c,*t;
806 /* "." on first line */
807 if (s[0] == '.') net_sout (stream,".",1);
808 /* find lines beginning with a "." */
809 while ((t = strstr (s,"\015\012.")) != NULL) {
810 c = *(t += 3); /* remember next character after "." */
811 *t = '\0'; /* tie off string */
812 /* output prefix */
813 if (!net_sout (stream,s,t-s)) return NIL;
814 *t = c; /* restore delimiter */
815 s = t - 1; /* push pointer up to the "." */
817 /* output remainder of text */
818 return *s ? net_soutr (stream,s) : T;