Init RSA repo.
[external/uw-imap-toolkit.git] / imap-2007e / c-client / smtp.c
1 /* ========================================================================
2  * Copyright 1988-2008 University of Washington
3  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
4
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * 
12  * ========================================================================
13  */
14
15 /*
16  * Program:     Simple Mail Transfer Protocol (SMTP) routines
17  *
18  * Author:      Mark Crispin
19  *              Networks and Distributed Computing
20  *              Computing & Communications
21  *              University of Washington
22  *              Administration Building, AG-44
23  *              Seattle, WA  98195
24  *              Internet: MRC@CAC.Washington.EDU
25  *
26  * Date:        27 July 1988
27  * Last Edited: 28 January 2008
28  *
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.
35  */
36
37
38 #include <ctype.h>
39 #include <stdio.h>
40 #include "c-client.h"
41 \f
42 /* Constants */
43
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 */
55
56
57 /* Convenient access to protocol-specific data */
58
59 #define ESMTP stream->protocol.esmtp
60
61
62 /* Function prototypes */
63
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);
74 \f
75 /* Mailer parameters */
76
77 static unsigned long smtp_maxlogintrials = MAXLOGINTRIALS;
78 static long smtp_port = 0;      /* default port override */
79 static long smtp_sslport = 0;
80
81
82 #ifndef RFC2821
83 #define RFC2821                 /* RFC 2821 compliance */
84 #endif
85
86 /* SMTP limits, current as of RFC 2821 */
87
88 #define SMTPMAXLOCALPART 64
89 #define SMTPMAXDOMAIN 255
90 #define SMTPMAXPATH 256
91
92
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
98  */
99
100 #define MAXLOCALPART ((MAILTMPLEN - (SMTPMAXDOMAIN + SMTPMAXPATH + 32)) / 2)
101 \f
102 /* Mail Transfer Protocol manipulate driver parameters
103  * Accepts: function code
104  *          function-dependent value
105  * Returns: function-dependent return value
106  */
107
108 void *smtp_parameters (long function,void *value)
109 {
110   switch ((int) function) {
111   case SET_MAXLOGINTRIALS:
112     smtp_maxlogintrials = (unsigned long) value;
113     break;
114   case GET_MAXLOGINTRIALS:
115     value = (void *) smtp_maxlogintrials;
116     break;
117   case SET_SMTPPORT:
118     smtp_port = (long) value;
119     break;
120   case GET_SMTPPORT:
121     value = (void *) smtp_port;
122     break;
123   case SET_SSLSMTPPORT:
124     smtp_sslport = (long) value;
125     break;
126   case GET_SSLSMTPPORT:
127     value = (void *) smtp_sslport;
128     break;
129   default:
130     value = NIL;                /* error case */
131     break;
132   }
133   return value;
134 }
135 \f
136 /* Mail Transfer Protocol open connection
137  * Accepts: network driver
138  *          service host list
139  *          port number
140  *          service name
141  *          SMTP open options
142  * Returns: SEND stream on success, NIL on failure
143  */
144
145 SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
146                             unsigned long port,long options)
147 {
148   SENDSTREAM *stream = NIL;
149   long reply;
150   char *s,tmp[MAILTMPLEN];
151   NETSTREAM *netstream;
152   NETMBX mb;
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);
160       mm_log (tmp,ERROR);
161     }
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");
170       }
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";
187
188         // SMTP GREETING
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);
193           mm_log (tmp,ERROR);
194           stream = smtp_close (stream);
195         }
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);
200           mm_log (tmp,ERROR);
201           stream = smtp_close (stream);
202         }
203         else {
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",
219                        mb.host);
220               mm_log (tmp,ERROR);
221                                 /* close without doing QUIT */
222               if (stream->netstream) net_close (stream->netstream);
223               stream->netstream = NIL;
224               stream = smtp_close (stream);
225             }
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",
229                        stream->reply);
230               mm_log (tmp,ERROR);
231               stream = smtp_close (stream);
232             }
233             else ESMTP.ok = T;  /* TLS OK and EHLO successful */
234           }
235           else if (mb.tlsflag) {/* user specified /tls but can't do it */
236             sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
237             mm_log (tmp,ERROR);
238             stream = smtp_close (stream);
239           }
240 \f
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 */
246                 strncpy (mb.host,
247                          (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
248                          net_remotehost (netstream) : net_host (netstream),
249                          NETMAXHOST-1);
250                 mb.host[NETMAXHOST-1] = '\0';
251               }
252               if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
253             }
254             else {              /* no available authenticators? */
255               sprintf (tmp,"%sSMTP authentication not available: %.80s", mb.secflag ? "Secure " : "",mb.host);
256               mm_log (tmp,ERROR);
257               stream = smtp_close (stream);
258             }
259           }
260         }
261       }
262     }
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)) {
267       ESMTP.dsn.want = T;
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;
272     }
273     if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
274   }
275   return stream;
276 }
277 \f
278 /* SMTP authenticate
279  * Accepts: stream to login
280  *          parsed network mailbox structure
281  *          scratch buffer
282  *          place to return user name
283  * Returns: T on success, NIL on failure
284  */
285
286 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
287 {
288   unsigned long trial,auths;
289   char *lsterr = NIL;
290   char usr[MAILTMPLEN];
291   AUTHENTICATOR *at;
292   long ret = NIL;
293
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);
299       mm_log (tmp,NIL);
300       fs_give ((void **) &lsterr);
301     }
302     trial = 0;                  /* initial trial count */
303     tmp[0] = '\0';              /* empty buffer */
304     if (stream->netstream) do {
305       if (lsterr) {
306         sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
307         mm_log (tmp,WARN);
308         fs_give ((void **) &lsterr);
309       }
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,
315                            &trial,usr)) {
316           if (stream->replycode == SMTPAUTHED) {
317             ESMTP.auth = NIL;   /* disable authenticators */
318             ret = LONGT;
319           }
320                                 /* if main program requested cancellation */
321           else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
322         }
323         stream->sensitive = NIL;/* unhide */
324       }
325 #if 1           // for smtp.web.de
326                 else if (!strcmp(at->name, "PLAIN")) {
327                         char* user = usr;
328                         char pwd[MAILTMPLEN];
329                         
330                         pwd[0] = NIL;
331                         mm_login(mb, user, pwd, trial);
332                         
333                         unsigned long rlen = strlen(mb->authuser) + strlen(user) + strlen(pwd) + 2;
334                         char* response = (char*) fs_get(rlen);
335                         char* t = response;
336                         char* u;
337                         
338                         if (mb->authuser[0])
339                                 for (u =user; *u; *t++ = *u++);
340                         
341                         *t++ = '\0';
342                         
343                         for (u = (mb->authuser[0] ? mb->authuser : user); *u; *t++ = *u++);
344                         
345                         *t++ = '\0';
346                         
347                         for (u = pwd; *u; *t++ = *u++);
348                         
349                         unsigned long i, j;
350                         
351                         for (t = (char*) rfc822_binary(response, rlen, &i), u = t, j = 0; j < i; j++) {
352                                 if (t[j] > ' ') *u++ = t[j];
353                         }
354                         
355                         *u = '\0';
356                         
357                         i = smtp_send(stream, "AUTH PLAIN", t);
358                         
359                         fs_give((void**)&t);
360                         
361                         memset(response, 0, rlen);
362                         fs_give((void**)&response);
363                         
364                         if (i == SMTPAUTHED) {
365                                 ESMTP.auth = NIL;
366                                 ret = LONGT;
367                         }
368                         else if (!trial) mm_log("SMTP Authentication cancelled", ERROR);
369                 }
370 #endif
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));
375   }
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);
379       mm_log (tmp,ERROR);
380     }
381     fs_give ((void **) &lsterr);
382   }
383   return ret;                   /* authentication failed */
384 }
385 \f
386 /* Get challenge to authenticator in binary
387  * Accepts: stream
388  *          pointer to returned size
389  * Returns: challenge or NIL if not challenge
390  */
391
392 void *smtp_challenge (void *s,unsigned long *len)
393 {
394   char tmp[MAILTMPLEN];
395   void *ret = NIL;
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);
400                 mm_log (tmp,WARN);
401                 
402                 *len = 0;               // MUST BE
403                 return cpystr("ok, go on");
404         }
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
408         {
409                 *len = 0;
410                 return cpystr("");
411         }
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);
416     mm_log (tmp,ERROR);
417   }
418   return ret;
419 }
420
421
422 /* Send authenticator response in BASE64
423  * Accepts: MAIL stream
424  *          string to send
425  *          length of string
426  * Returns: T, always
427  */
428
429 long smtp_response (void *s,char *response,unsigned long size)
430 {
431   SENDSTREAM *stream = (SENDSTREAM *) s;
432   unsigned long i,j;
433   char *t,*u;
434
435   if (response) {               /* make CRLFless BASE64 string */
436     if (size) {
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 */
440
441       i = smtp_send (stream,t,NIL);
442       fs_give ((void **) &t);
443     }
444     else
445     {
446         i = smtp_send (stream,"",NIL);
447     }
448   }
449   else {                        /* abort requested */
450     i = smtp_send (stream,"*",NIL);
451     stream->saslcancel = T;     /* mark protocol-requested SASL cancel */
452   }
453   return LONGT;
454 }
455 \f
456 /* Mail Transfer Protocol close connection
457  * Accepts: SEND stream
458  * Returns: NIL always
459  */
460
461 SENDSTREAM *smtp_close (SENDSTREAM *stream)
462 {
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);
468     }
469                                 /* clean up */
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 */
475   }
476   return NIL;
477 }
478 \f
479 /* Mail Transfer Protocol deliver mail
480  * Accepts: SEND stream
481  *          delivery option (MAIL, SEND, SAML, SOML)
482  *          message envelope
483  *          message body
484  * Returns: T on success, NIL on failure
485  */
486
487 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
488 {
489   RFC822BUFFER buf;
490   char tmp[SENDBUFLEN+1];
491   long error = NIL;
492   long retry = NIL;
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");
500     return NIL;
501   }
502   do {                          /* make sure stream is in good shape */
503     smtp_send (stream,"RSET",NIL);
504     if (retry) {                /* need to retry with authentication? */
505       NETMBX mb;
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)) :
512                stream->host,
513                (stream->netstream->dtb ==
514                 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
515                "/ssl" : "");
516       mail_valid_net_parse (tmp,&mb);
517       if (!smtp_auth (stream,&mb,tmp)) return NIL;
518       retry = NIL;              /* no retry at this point */
519     }
520 \f
521     strcpy (tmp,"FROM:<");      /* compose "MAIL FROM:<return-path>" */
522 #ifdef RFC2821
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);
528     }
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);
536 #endif
537     strcat (tmp,">");
538     if (ESMTP.ok) {
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");
543         if (ESMTP.dsn.envid)
544           sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
545       }
546     }
547                                 /* send "MAIL FROM" command */
548     switch (smtp_send (stream,type,tmp)) {
549     case SMTPUNAVAIL:           /* mailbox unavailable? */
550     case SMTPWANTAUTH:          /* wants authentication? */
551     case SMTPWANTAUTH2:
552       if (ESMTP.auth) retry = T;/* yes, retry with authentication */
553     case SMTPOK:                /* looks good */
554       break;
555     default:                    /* other failure */
556       return NIL;
557     }
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");
565       return NIL;
566     }
567   } while (retry);
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 */
575   }
576                                 /* send trailing dot */
577   return (smtp_send (stream,".",NIL) == SMTPOK) ? LONGT : NIL;
578 }
579 \f
580 /* Simple Mail Transfer Protocol send VERBose
581  * Accepts: SMTP stream
582  * Returns: T if successful, else NIL
583  *
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.
589  */
590
591 long smtp_verbose (SENDSTREAM *stream)
592 {
593                                 /* accept any 2xx reply code */
594   return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
595 }
596 \f
597 /* Internal routines */
598
599
600 /* Simple Mail Transfer Protocol send recipient
601  * Accepts: SMTP stream
602  *          address list
603  *          pointer to error flag
604  * Returns: T if should retry, else NIL
605  */
606
607 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
608 {
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");
617         *error = T;
618       }
619       else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
620         adr->error = cpystr ("501 Recipient domain too long");
621         *error = T;
622       }
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");
626         *error = T;
627       }
628 #endif
629 \f
630       else {
631         strcpy (tmp,"TO:<");    /* compose "RCPT TO:<return-path>" */
632 #ifdef RFC2821
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);
637         strcat (tmp,">");
638 #endif
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",
653                      adr->orcpt.addr);
654             sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
655           }
656         }
657         switch (smtp_send (stream,"RCPT",tmp)) {
658         case SMTPOK:            /* looks good */
659           break;
660         case SMTPUNAVAIL:       /* mailbox unavailable? */
661         case SMTPWANTAUTH:      /* wants authentication? */
662         case SMTPWANTAUTH2:
663           if (ESMTP.auth) return T;
664         default:                /* other failure */
665           *error = T;           /* note that an error occurred */
666           adr->error = cpystr (stream->reply);
667         }
668       }
669     }
670     adr = adr->next;            /* do any subsequent recipients */
671   }
672   return NIL;                   /* no retry called for */
673 }
674 \f
675 /* Simple Mail Transfer Protocol send command
676  * Accepts: SEND stream
677  *          text
678  * Returns: reply code
679  */
680
681 long smtp_send (SENDSTREAM *stream,char *command,char *args)
682 {
683   long ret;
684   char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
685                              + 3);
686                                 /* build the complete command */
687   if (args) sprintf (s,"%s %s",command,args);
688   else strcpy (s,command);
689
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;
697   }
698   else ret = smtp_fake (stream,"SMTP connection broken (command)");
699   fs_give ((void **) &s);
700   return ret;
701 }
702
703
704 /* Simple Mail Transfer Protocol get reply
705  * Accepts: SMTP stream
706  * Returns: reply code
707  */
708
709 long smtp_reply (SENDSTREAM *stream)
710 {
711   smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
712   long reply;
713                                 /* flush old reply */
714   if (stream->reply) fs_give ((void **) &stream->reply);
715                                 /* get 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);
721   }
722   else reply = smtp_fake (stream,"SMTP connection broken (reply)");
723   return reply;
724 }
725 \f
726 /* Simple Mail Transfer Protocol send EHLO
727  * Accepts: SMTP stream
728  *          host name to use in EHLO
729  *          NETMBX structure
730  * Returns: reply code
731  */
732
733 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
734 {
735   unsigned long i,j;
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) {
750                                 /* hack for AUTH= */
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);
760         ESMTP.size.ok = T;
761       }
762       else if (!compare_cstring (s,"DELIVERBY")) {
763         if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
764         ESMTP.deliverby.ok = T;
765       }
766       else if (!compare_cstring (s,"ATRN")) {
767         ESMTP.atrn.domains = cpystr (t);
768         ESMTP.atrn.ok = T;
769       }
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);
775     }
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;
795   }
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
799   /*
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);
804     */
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 */
810 }
811 \f
812 /* Simple Mail Transfer Protocol set fake error and abort
813  * Accepts: SMTP stream
814  *          error text
815  * Returns: SMTPSOFTFATAL, always
816  */
817
818 long smtp_fake (SENDSTREAM *stream,char *text)
819 {
820   if (stream->netstream) {      /* close net connection if still open */
821     net_close (stream->netstream);
822     stream->netstream = NIL;
823   }
824                                 /* set last error */
825   return smtp_seterror (stream,SMTPSOFTFATAL,text);
826 }
827
828
829 /* Simple Mail Transfer Protocol set error
830  * Accepts: SMTP stream
831  *          SMTP error code
832  *          error text
833  * Returns: error code
834  */
835
836 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
837 {
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 */
844 }
845
846
847 #ifdef __FEATURE_SEND_OPTMIZATION__
848 /* Simple Mail Transfer Protocol filter mail
849  * Accepts: stream
850  *          string
851  * Returns: T on success, NIL on failure
852  */
853 __attribute__((visibility("default"))) long smtp_soutr_test (void *stream, char *s)
854 {
855         char *c,*t;
856
857         /* "." on first line */
858
859         if (s[0] == '.') 
860                 net_sout (stream,".",1);        /* find lines beginning with a "." */
861
862         c = s;
863
864         while (t = strstr (c,"\015\012.")) 
865         {
866                 t += 2;         /* Increment the end point */   
867                 
868                 /* Pump minimum of 8K data */
869                 if((t-s) > 8192)                
870                 {
871                         if (!net_sout (stream,s,t-s)) /* output prefix */
872                                 return NIL;
873                         c = s = t; /* Move search as well as data pointer ahead */
874                 }
875                 else
876                 {       
877                         c = t;  /* Move the serach pointer ahead "." */
878                 }       
879         }               
880                 /* output remainder of text */
881         return *s ? net_soutr (stream,s) : T;
882
883 }
884 #endif
885
886 /* Simple Mail Transfer Protocol filter mail
887  * Accepts: stream
888  *          string
889  * Returns: T on success, NIL on failure
890  */
891
892 long smtp_soutr (void *stream,char *s)
893 {
894   char c,*t;
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 */
901                                 /* output prefix */
902     if (!net_sout (stream,s,t-s)) return NIL;
903     *t = c;                     /* restore delimiter */
904     s = t - 1;                  /* push pointer up to the "." */
905   }
906                                 /* output remainder of text */
907   return *s ? net_soutr (stream,s) : T;
908 }