1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
3 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
16 * Program: Simple Mail Transfer Protocol (SMTP) routines
18 * Author: Mark Crispin
19 * Networks and Distributed Computing
20 * Computing & Communications
21 * University of Washington
22 * Administration Building, AG-44
24 * Internet: MRC@CAC.Washington.EDU
27 * Last Edited: 28 January 2008
29 * This original version of this file is
30 * Copyright 1988 Stanford University
31 * and was developed in the Symbolic Systems Resources Group of the Knowledge
32 * Systems Laboratory at Stanford University in 1987-88, and was funded by the
33 * Biomedical Research Technology Program of the National Institutes of Health
34 * under grant number RR-00785.
44 #define SMTPSSLPORT (long) 465 /* former assigned SSL TCP contact port */
45 #define SMTPGREET (long) 220 /* SMTP successful greeting */
46 #define SMTPAUTHED (long) 235 /* SMTP successful authentication */
47 #define SMTPOK (long) 250 /* SMTP OK code */
48 #define SMTPAUTHREADY (long) 334/* SMTP ready for authentication */
49 #define SMTPREADY (long) 354 /* SMTP ready for data */
50 #define SMTPSOFTFATAL (long) 421/* SMTP soft fatal code */
51 #define SMTPWANTAUTH (long) 505 /* SMTP authentication needed */
52 #define SMTPWANTAUTH2 (long) 530/* SMTP authentication needed */
53 #define SMTPUNAVAIL (long) 550 /* SMTP mailbox unavailable */
54 #define SMTPHARDERROR (long) 554/* SMTP miscellaneous hard failure */
57 /* Convenient access to protocol-specific data */
59 #define ESMTP stream->protocol.esmtp
62 /* Function prototypes */
64 void *smtp_challenge (void *s,unsigned long *len);
65 long smtp_response (void *s,char *response,unsigned long size);
66 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp);
67 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error);
68 long smtp_send (SENDSTREAM *stream,char *command,char *args);
69 long smtp_reply (SENDSTREAM *stream);
70 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb);
71 long smtp_fake (SENDSTREAM *stream,char *text);
72 static long smtp_seterror (SENDSTREAM *stream,long code,char *text);
73 long smtp_soutr (void *stream,char *s);
75 /* Mailer parameters */
77 static unsigned long smtp_maxlogintrials = MAXLOGINTRIALS;
78 static long smtp_port = 0; /* default port override */
79 static long smtp_sslport = 0;
83 #define RFC2821 /* RFC 2821 compliance */
86 /* SMTP limits, current as of RFC 2821 */
88 #define SMTPMAXLOCALPART 64
89 #define SMTPMAXDOMAIN 255
90 #define SMTPMAXPATH 256
93 /* I have seen local parts of more than 64 octets, in spite of the SMTP
94 * limits. So, we'll have a more generous limit that's still guaranteed
95 * not to pop the buffer, and let the server worry about it. As of this
96 * writing, it comes out to 240. Anyone with a mailbox name larger than
97 * that is in serious need of a life or at least a new ISP! 23 June 1998
100 #define MAXLOCALPART ((MAILTMPLEN - (SMTPMAXDOMAIN + SMTPMAXPATH + 32)) / 2)
102 /* Mail Transfer Protocol manipulate driver parameters
103 * Accepts: function code
104 * function-dependent value
105 * Returns: function-dependent return value
108 void *smtp_parameters (long function,void *value)
110 switch ((int) function) {
111 case SET_MAXLOGINTRIALS:
112 smtp_maxlogintrials = (unsigned long) value;
114 case GET_MAXLOGINTRIALS:
115 value = (void *) smtp_maxlogintrials;
118 smtp_port = (long) value;
121 value = (void *) smtp_port;
123 case SET_SSLSMTPPORT:
124 smtp_sslport = (long) value;
126 case GET_SSLSMTPPORT:
127 value = (void *) smtp_sslport;
130 value = NIL; /* error case */
136 /* Mail Transfer Protocol open connection
137 * Accepts: network driver
142 * Returns: SEND stream on success, NIL on failure
145 SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
146 unsigned long port,long options)
148 SENDSTREAM *stream = NIL;
150 char *s,tmp[MAILTMPLEN];
151 NETSTREAM *netstream;
153 if (!(hostlist && *hostlist)) mm_log ("Missing SMTP service host",ERROR);
154 /* maximum domain name is 64 characters */
155 else do if (strlen (*hostlist) < SMTPMAXDOMAIN) {
156 sprintf (tmp,"{%.1000s}",*hostlist);
157 if (!mail_valid_net_parse_work (tmp,&mb,service ? service : "smtp") ||
158 mb.anoflag || mb.readonlyflag) {
159 sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
162 else { /* light tryssl flag if requested */
163 mb.trysslflag = (options & SOP_TRYSSL) ? T : NIL;
164 /* explicit port overrides all */
165 if (mb.port) port = mb.port;
166 /* else /submit overrides port argument */
167 else if (!compare_cstring (mb.service,"submit")) {
168 port = SUBMITTCPPORT; /* override port, use IANA name */
169 strcpy (mb.service,"submission");
171 /* else port argument overrides SMTP port */
172 else if (!port) port = smtp_port ? smtp_port : SMTPTCPPORT;
173 if (netstream = /* try to open ordinary connection */
174 net_open (&mb,dv,port,
175 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
176 "*smtps",smtp_sslport ? smtp_sslport : SMTPSSLPORT)) {
177 stream = (SENDSTREAM *) memset (fs_get (sizeof (SENDSTREAM)),0,
178 sizeof (SENDSTREAM));
179 stream->netstream = netstream;
180 stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
181 net_host (netstream) : mb.host);
182 stream->debug = (mb.dbgflag || (options & OP_DEBUG)) ? T : NIL;
183 if (options & SOP_SECURE) mb.secflag = T;
184 /* get name of local host to use */
185 s = compare_cstring ("localhost",mb.host) ?
186 net_localhost (netstream) : "localhost";
189 do reply = smtp_reply (stream);
190 while ((reply < 100) || (stream->reply[3] == '-'));
191 if (reply != SMTPGREET){/* get SMTP greeting */
192 sprintf (tmp,"SMTP greeting failure: %.80s",stream->reply);
194 stream = smtp_close (stream);
196 /* try EHLO first, then HELO */
197 else if (((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) &&
198 ((reply = smtp_send (stream,"HELO",s)) != SMTPOK)) {
199 sprintf (tmp,"SMTP hello failure: %.80s",stream->reply);
201 stream = smtp_close (stream);
204 NETDRIVER *ssld =(NETDRIVER *)mail_parameters(NIL,GET_SSLDRIVER,NIL);
205 sslstart_t stls = (sslstart_t) mail_parameters(NIL,GET_SSLSTART,NIL);
206 ESMTP.ok = T; /* ESMTP server, start TLS if present */
207 if (!dv && stls && ESMTP.service.starttls &&
208 !mb.sslflag && !mb.notlsflag &&
209 (smtp_send (stream,"STARTTLS",NIL) == SMTPGREET)) {
210 mb.tlsflag = T; /* TLS OK, get into TLS at this end */
211 stream->netstream->dtb = ssld;
212 /* TLS started, negotiate it */
213 if (!(stream->netstream->stream = (*stls)
214 (stream->netstream->stream,mb.host,
215 (mb.tlssslv23 ? NIL : NET_TLSCLIENT) |
216 (mb.novalidate ? NET_NOVALIDATECERT:NIL)))){
217 /* TLS negotiation failed after STARTTLS */
218 sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",
221 /* close without doing QUIT */
222 if (stream->netstream) net_close (stream->netstream);
223 stream->netstream = NIL;
224 stream = smtp_close (stream);
226 /* TLS OK, re-negotiate EHLO */
227 else if ((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) {
228 sprintf (tmp,"SMTP EHLO failure after STARTTLS: %.80s",
231 stream = smtp_close (stream);
233 else ESMTP.ok = T; /* TLS OK and EHLO successful */
235 else if (mb.tlsflag) {/* user specified /tls but can't do it */
236 sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
238 stream = smtp_close (stream);
241 /* remote name for authentication */
242 if (stream && ((mb.secflag || mb.user[0]))) {
243 if (ESMTP.auth) { /* use authenticator? */
244 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
245 /* remote name for authentication */
247 (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
248 net_remotehost (netstream) : net_host (netstream),
250 mb.host[NETMAXHOST-1] = '\0';
252 if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
254 else { /* no available authenticators? */
255 sprintf (tmp,"%sSMTP authentication not available: %.80s", mb.secflag ? "Secure " : "",mb.host);
257 stream = smtp_close (stream);
263 } while (!stream && *++hostlist);
264 if (stream) { /* set stream options if have a stream */
265 if (options &(SOP_DSN | SOP_DSN_NOTIFY_FAILURE | SOP_DSN_NOTIFY_DELAY |
266 SOP_DSN_NOTIFY_SUCCESS | SOP_DSN_RETURN_FULL)) {
268 if (options & SOP_DSN_NOTIFY_FAILURE) ESMTP.dsn.notify.failure = T;
269 if (options & SOP_DSN_NOTIFY_DELAY) ESMTP.dsn.notify.delay = T;
270 if (options & SOP_DSN_NOTIFY_SUCCESS) ESMTP.dsn.notify.success = T;
271 if (options & SOP_DSN_RETURN_FULL) ESMTP.dsn.full = T;
273 if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
279 * Accepts: stream to login
280 * parsed network mailbox structure
282 * place to return user name
283 * Returns: T on success, NIL on failure
286 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
288 unsigned long trial,auths;
290 char usr[MAILTMPLEN];
294 for (auths = ESMTP.auth, stream->saslcancel = NIL;
295 !ret && stream->netstream && auths &&
296 (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
297 if (lsterr) { /* previous authenticator failed? */
298 sprintf (tmp,"Retrying using %s authentication after %.80s", at->name,lsterr);
300 fs_give ((void **) &lsterr);
302 trial = 0; /* initial trial count */
303 tmp[0] = '\0'; /* empty buffer */
304 if (stream->netstream) do {
306 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
308 fs_give ((void **) &lsterr);
310 stream->saslcancel = NIL;
311 if (smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
312 /* hide client authentication responses */
313 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
314 if ((*at->client) (smtp_challenge,smtp_response,"smtp",mb,stream,
316 if (stream->replycode == SMTPAUTHED) {
317 ESMTP.auth = NIL; /* disable authenticators */
320 /* if main program requested cancellation */
321 else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
323 stream->sensitive = NIL;/* unhide */
325 #if 1 // for smtp.web.de
326 else if (!strcmp(at->name, "PLAIN")) {
328 char pwd[MAILTMPLEN];
331 mm_login(mb, user, pwd, trial);
333 unsigned long rlen = strlen(mb->authuser) + strlen(user) + strlen(pwd) + 2;
334 char* response = (char*) fs_get(rlen);
339 for (u =user; *u; *t++ = *u++);
343 for (u = (mb->authuser[0] ? mb->authuser : user); *u; *t++ = *u++);
347 for (u = pwd; *u; *t++ = *u++);
351 for (t = (char*) rfc822_binary(response, rlen, &i), u = t, j = 0; j < i; j++) {
352 if (t[j] > ' ') *u++ = t[j];
357 i = smtp_send(stream, "AUTH PLAIN", t);
361 memset(response, 0, rlen);
362 fs_give((void**)&response);
364 if (i == SMTPAUTHED) {
368 else if (!trial) mm_log("SMTP Authentication cancelled", ERROR);
371 /* remember response if error and no cancel */
372 if (!ret && trial) lsterr = cpystr (stream->reply);
373 } while (!ret && stream->netstream && trial &&
374 (trial < smtp_maxlogintrials));
376 if (lsterr) { /* previous authenticator failed? */
377 if (!stream->saslcancel) { /* don't do this if a cancel */
378 sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
381 fs_give ((void **) &lsterr);
383 return ret; /* authentication failed */
386 /* Get challenge to authenticator in binary
388 * pointer to returned size
389 * Returns: challenge or NIL if not challenge
392 void *smtp_challenge (void *s,unsigned long *len)
394 char tmp[MAILTMPLEN];
396 SENDSTREAM *stream = (SENDSTREAM *) s;
397 // for smtp.mail.yahoo.co.kr
398 if (!strcmp(stream->reply+4, "ok, go on")) {
399 sprintf (tmp,"smtp_challenge : Server bug: non-empty initial PLAIN challenge 3: %.80s",stream->reply+4);
403 return cpystr("ok, go on");
405 // for smtp.naver.com or other servers that reply only "THREE" response digit code whose string length is 3
406 if ( (stream->replycode == SMTPAUTHREADY) &&
407 strlen(stream->reply) <= 3 ) // only response digit code exists in the reply buffer
412 if ((stream->replycode == SMTPAUTHREADY) &&
413 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
414 strlen (stream->reply + 4),len))) {
415 sprintf (tmp,"SMTP SERVER BUG (invalid challenge): %.80s",stream->reply+4);
422 /* Send authenticator response in BASE64
423 * Accepts: MAIL stream
429 long smtp_response (void *s,char *response,unsigned long size)
431 SENDSTREAM *stream = (SENDSTREAM *) s;
435 if (response) { /* make CRLFless BASE64 string */
437 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
438 j < i; j++) if (t[j] > ' ') *u++ = t[j];
439 *u = '\0'; /* tie off string */
441 i = smtp_send (stream,t,NIL);
442 fs_give ((void **) &t);
446 i = smtp_send (stream,"",NIL);
449 else { /* abort requested */
450 i = smtp_send (stream,"*",NIL);
451 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
456 /* Mail Transfer Protocol close connection
457 * Accepts: SEND stream
458 * Returns: NIL always
461 SENDSTREAM *smtp_close (SENDSTREAM *stream)
463 if (stream) { /* send "QUIT" */
464 if (stream->netstream) { /* do close actions if have netstream */
465 smtp_send (stream,"QUIT",NIL);
466 if (stream->netstream) /* could have been closed during "QUIT" */
467 net_close (stream->netstream);
470 if (stream->host) fs_give ((void **) &stream->host);
471 if (stream->reply) fs_give ((void **) &stream->reply);
472 if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
473 if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
474 fs_give ((void **) &stream);/* flush the stream */
479 /* Mail Transfer Protocol deliver mail
480 * Accepts: SEND stream
481 * delivery option (MAIL, SEND, SAML, SOML)
484 * Returns: T on success, NIL on failure
487 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
490 char tmp[SENDBUFLEN+1];
493 buf.f = smtp_soutr; /* initialize buffer */
494 buf.s = stream->netstream;
495 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
496 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
497 if (!(env->to || env->cc || env->bcc)) {
498 /* no recipients in request */
499 smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
502 do { /* make sure stream is in good shape */
503 smtp_send (stream,"RSET",NIL);
504 if (retry) { /* need to retry with authentication? */
506 /* yes, build remote name for authentication */
507 sprintf (tmp,"{%.200s/smtp%s}<none>",
508 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
509 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
510 net_remotehost (stream->netstream) :
511 net_host (stream->netstream)) :
513 (stream->netstream->dtb ==
514 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
516 mail_valid_net_parse (tmp,&mb);
517 if (!smtp_auth (stream,&mb,tmp)) return NIL;
518 retry = NIL; /* no retry at this point */
521 strcpy (tmp,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
523 if (env->return_path && env->return_path->host &&
524 !((strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
525 (strlen (env->return_path->host) > SMTPMAXDOMAIN))) {
526 rfc822_cat (tmp,env->return_path->mailbox,NIL);
527 sprintf (tmp + strlen (tmp),"@%s",env->return_path->host);
529 #else /* old code with A-D-L support */
530 if (env->return_path && env->return_path->host &&
531 !((env->return_path->adl &&
532 (strlen (env->return_path->adl) > SMTPMAXPATH)) ||
533 (strlen (env->return_path->mailbox) > SMTPMAXLOCALPART) ||
534 (strlen (env->return_path->host) > SMTPMAXDOMAIN)))
535 rfc822_address (tmp,env->return_path);
539 if (ESMTP.eightbit.ok && ESMTP.eightbit.want)
540 strcat (tmp," BODY=8BITMIME");
541 if (ESMTP.dsn.ok && ESMTP.dsn.want) {
542 strcat (tmp,ESMTP.dsn.full ? " RET=FULL" : " RET=HDRS");
544 sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
547 /* send "MAIL FROM" command */
548 switch (smtp_send (stream,type,tmp)) {
549 case SMTPUNAVAIL: /* mailbox unavailable? */
550 case SMTPWANTAUTH: /* wants authentication? */
552 if (ESMTP.auth) retry = T;/* yes, retry with authentication */
553 case SMTPOK: /* looks good */
555 default: /* other failure */
558 /* negotiate the recipients */
559 if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
560 if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
561 if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
562 if (!retry && error) { /* any recipients failed? */
563 smtp_send (stream,"RSET",NIL);
564 smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
568 /* negotiate data command */
569 if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) return NIL;
570 /* send message data */
571 if (!rfc822_output_full (&buf,env,body,
572 ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
573 smtp_fake (stream,"SMTP connection broken (message data)");
574 return NIL; /* can't do much else here */
576 /* send trailing dot */
577 return (smtp_send (stream,".",NIL) == SMTPOK) ? LONGT : NIL;
580 /* Simple Mail Transfer Protocol send VERBose
581 * Accepts: SMTP stream
582 * Returns: T if successful, else NIL
584 * Descriptive text formerly in [al]pine sources:
585 * At worst, this command may cause the SMTP connection to get nuked. Modern
586 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
587 * arg is for PMDF). What's more, if it works, the reply code and accompanying
588 * text may vary from server to server.
591 long smtp_verbose (SENDSTREAM *stream)
593 /* accept any 2xx reply code */
594 return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
597 /* Internal routines */
600 /* Simple Mail Transfer Protocol send recipient
601 * Accepts: SMTP stream
603 * pointer to error flag
604 * Returns: T if should retry, else NIL
607 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
609 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
610 while (adr) { /* for each address on the list */
611 /* clear any former error */
612 if (adr->error) fs_give ((void **) &adr->error);
613 if (adr->host) { /* ignore group syntax */
614 /* enforce SMTP limits to protect the buffer */
615 if (strlen (adr->mailbox) > MAXLOCALPART) {
616 adr->error = cpystr ("501 Recipient name too long");
619 else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
620 adr->error = cpystr ("501 Recipient domain too long");
623 #ifndef RFC2821 /* old code with A-D-L support */
624 else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
625 adr->error = cpystr ("501 Path too long");
631 strcpy (tmp,"TO:<"); /* compose "RCPT TO:<return-path>" */
633 rfc822_cat (tmp,adr->mailbox,NIL);
634 sprintf (tmp + strlen (tmp),"@%s>",adr->host);
635 #else /* old code with A-D-L support */
636 rfc822_address (tmp,adr);
639 /* want notifications */
640 if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
641 /* yes, start with prefix */
642 strcat (tmp," NOTIFY=");
643 s = tmp + strlen (tmp);
644 if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
645 if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
646 if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
647 /* tie off last comma */
648 if (*s) s[strlen (s) - 1] = '\0';
649 else strcat (tmp,"NEVER");
650 if (adr->orcpt.addr) {
651 sprintf (orcpt,"%.498s;%.498s",
652 adr->orcpt.type ? adr->orcpt.type : "rfc822",
654 sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
657 switch (smtp_send (stream,"RCPT",tmp)) {
658 case SMTPOK: /* looks good */
660 case SMTPUNAVAIL: /* mailbox unavailable? */
661 case SMTPWANTAUTH: /* wants authentication? */
663 if (ESMTP.auth) return T;
664 default: /* other failure */
665 *error = T; /* note that an error occurred */
666 adr->error = cpystr (stream->reply);
670 adr = adr->next; /* do any subsequent recipients */
672 return NIL; /* no retry called for */
675 /* Simple Mail Transfer Protocol send command
676 * Accepts: SEND stream
678 * Returns: reply code
681 long smtp_send (SENDSTREAM *stream,char *command,char *args)
684 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
686 /* build the complete command */
687 if (args) sprintf (s,"%s %s",command,args);
688 else strcpy (s,command);
690 if (stream->debug) mail_dlog (s,stream->sensitive);
691 strcat (s,"\015\012");
692 /* send the command */
693 if (stream->netstream && net_soutr (stream->netstream,s)) {
694 do stream->replycode = smtp_reply (stream);
695 while ((stream->replycode < 100) || (stream->reply[3] == '-'));
696 ret = stream->replycode;
698 else ret = smtp_fake (stream,"SMTP connection broken (command)");
699 fs_give ((void **) &s);
704 /* Simple Mail Transfer Protocol get reply
705 * Accepts: SMTP stream
706 * Returns: reply code
709 long smtp_reply (SENDSTREAM *stream)
711 smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
713 /* flush old reply */
714 if (stream->reply) fs_give ((void **) &stream->reply);
716 if (stream->netstream && (stream->reply = net_getline (stream->netstream))) {
717 if (stream->debug) mm_dlog (stream->reply);
718 /* return response code */
719 reply = atol (stream->reply);
720 if (pv && (reply < 100)) (*pv) (stream->reply);
722 else reply = smtp_fake (stream,"SMTP connection broken (reply)");
726 /* Simple Mail Transfer Protocol send EHLO
727 * Accepts: SMTP stream
728 * host name to use in EHLO
730 * Returns: reply code
733 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
736 long flags = (mb->secflag ? AU_SECURE : NIL) |
737 (mb->authuser[0] ? AU_AUTHUSER : NIL);
738 char *s,*t,*r,tmp[MAILTMPLEN];
739 /* clear ESMTP data */
740 memset (&ESMTP,0,sizeof (ESMTP));
741 if (mb->loser) return 500; /* never do EHLO if a loser */
742 sprintf (tmp,"EHLO %s",host); /* build the complete command */
743 if (stream->debug) mm_dlog (tmp);
744 strcat (tmp,"\015\012");
745 /* send the command */
746 if (!net_soutr (stream->netstream,tmp))
747 return smtp_fake (stream,"SMTP connection broken (EHLO)");
748 /* got an OK reply? */
749 do if ((i = smtp_reply (stream)) == SMTPOK) {
751 if (stream->reply[4] && stream->reply[5] && stream->reply[6] &&
752 stream->reply[7] && (stream->reply[8] == '=')) stream->reply[8] = ' ';
753 /* get option code */
754 if (!(s = strtok_r (stream->reply+4," ",&r)));
755 /* have option, does it have a value */
756 else if ((t = strtok_r (NIL," ",&r)) && *t) {
757 /* EHLO options which take arguments */
758 if (!compare_cstring (s,"SIZE")) {
759 if (isdigit (*t)) ESMTP.size.limit = strtoul (t,&t,10);
762 else if (!compare_cstring (s,"DELIVERBY")) {
763 if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
764 ESMTP.deliverby.ok = T;
766 else if (!compare_cstring (s,"ATRN")) {
767 ESMTP.atrn.domains = cpystr (t);
770 else if (!compare_cstring (s,"AUTH"))
771 //do if ((j = mail_lookup_auth_name (t,flags)) &&
772 do if ((j = mail_lookup_auth_name_smtp (t,flags)) && // 22-Mar-2010 Fix for SMTP Authorization issue - avoid race condition. change from mail_lookup_auth_name to mail_lookup_auth_name_smtp
773 (--j < MAXAUTHENTICATORS)) ESMTP.auth |= (1 << j);
774 while ((t = strtok_r (NIL," ",&r)) && *t);
776 /* EHLO options which do not take arguments */
777 else if (!compare_cstring (s,"SIZE")) ESMTP.size.ok = T;
778 else if (!compare_cstring (s,"8BITMIME")) ESMTP.eightbit.ok = T;
779 else if (!compare_cstring (s,"DSN")) ESMTP.dsn.ok = T;
780 else if (!compare_cstring (s,"ATRN")) ESMTP.atrn.ok = T;
781 else if (!compare_cstring (s,"SEND")) ESMTP.service.send = T;
782 else if (!compare_cstring (s,"SOML")) ESMTP.service.soml = T;
783 else if (!compare_cstring (s,"SAML")) ESMTP.service.saml = T;
784 else if (!compare_cstring (s,"EXPN")) ESMTP.service.expn = T;
785 else if (!compare_cstring (s,"HELP")) ESMTP.service.help = T;
786 else if (!compare_cstring (s,"TURN")) ESMTP.service.turn = T;
787 else if (!compare_cstring (s,"ETRN")) ESMTP.service.etrn = T;
788 else if (!compare_cstring (s,"STARTTLS")) ESMTP.service.starttls = T;
789 else if (!compare_cstring (s,"RELAY")) ESMTP.service.relay = T;
790 else if (!compare_cstring (s,"PIPELINING")) ESMTP.service.pipe = T;
791 else if (!compare_cstring (s,"ENHANCEDSTATUSCODES"))
792 ESMTP.service.ensc = T;
793 else if (!compare_cstring (s,"BINARYMIME")) ESMTP.service.bmime = T;
794 else if (!compare_cstring (s,"CHUNKING")) ESMTP.service.chunk = T;
796 while ((i < 100) || (stream->reply[3] == '-'));
797 /* disable LOGIN if PLAIN also advertised */
798 // 22-Mar-2010 change from mail_lookup_auth_name to mail_lookup_auth_name_smtp
800 if ((j = mail_lookup_auth_name ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
801 (ESMTP.auth & (1 << j)) &&
802 (j = mail_lookup_auth_name ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
803 ESMTP.auth &= ~(1 << j);
805 if ((j = mail_lookup_auth_name_smtp ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
806 (ESMTP.auth & (1 << j)) &&
807 (j = mail_lookup_auth_name_smtp ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
808 ESMTP.auth &= ~(1 << j);
809 return i; /* return the response code */
812 /* Simple Mail Transfer Protocol set fake error and abort
813 * Accepts: SMTP stream
815 * Returns: SMTPSOFTFATAL, always
818 long smtp_fake (SENDSTREAM *stream,char *text)
820 if (stream->netstream) { /* close net connection if still open */
821 net_close (stream->netstream);
822 stream->netstream = NIL;
825 return smtp_seterror (stream,SMTPSOFTFATAL,text);
829 /* Simple Mail Transfer Protocol set error
830 * Accepts: SMTP stream
833 * Returns: error code
836 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
838 /* flush any old reply */
839 if (stream->reply ) fs_give ((void **) &stream->reply);
840 /* set up pseudo-reply string */
841 stream->reply = (char *) fs_get (20+strlen (text));
842 sprintf (stream->reply,"%ld %s",code,text);
843 return code; /* return error code */
847 #ifdef __FEATURE_SEND_OPTMIZATION__
848 /* Simple Mail Transfer Protocol filter mail
851 * Returns: T on success, NIL on failure
853 __attribute__((visibility("default"))) long smtp_soutr_test (void *stream, char *s)
857 /* "." on first line */
860 net_sout (stream,".",1); /* find lines beginning with a "." */
864 while (t = strstr (c,"\015\012."))
866 t += 2; /* Increment the end point */
868 /* Pump minimum of 8K data */
871 if (!net_sout (stream,s,t-s)) /* output prefix */
873 c = s = t; /* Move search as well as data pointer ahead */
877 c = t; /* Move the serach pointer ahead "." */
880 /* output remainder of text */
881 return *s ? net_soutr (stream,s) : T;
886 /* Simple Mail Transfer Protocol filter mail
889 * Returns: T on success, NIL on failure
892 long smtp_soutr (void *stream,char *s)
895 /* "." on first line */
896 if (s[0] == '.') net_sout (stream,".",1);
897 /* find lines beginning with a "." */
898 while (t = strstr (s,"\015\012.")) {
899 c = *(t += 3); /* remember next character after "." */
900 *t = '\0'; /* tie off string */
902 if (!net_sout (stream,s,t-s)) return NIL;
903 *t = c; /* restore delimiter */
904 s = t - 1; /* push pointer up to the "." */
906 /* output remainder of text */
907 return *s ? net_soutr (stream,s) : T;