1 /* ========================================================================
2 * Copyright 2015 Eduardo Chappa
3 * Copyright 2008 Mark Crispin
4 * ========================================================================
8 * Program: Simple Mail Transfer Protocol (SMTP) routines
10 * Author: Mark Crispin
13 * Last Edited: 19 November 2008 (Crispin)
14 * Last Edited: 16 January 2015 (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.
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;
80 #define RFC2821 /* RFC 2821 compliance */
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
;
111 case GET_MAXLOGINTRIALS
:
112 value
= (void *) smtp_maxlogintrials
;
115 smtp_port
= (long) value
;
118 value
= (void *) smtp_port
;
120 case SET_SSLSMTPPORT
:
121 smtp_sslport
= (long) value
;
123 case GET_SSLSMTPPORT
:
124 value
= (void *) smtp_sslport
;
127 value
= NIL
; /* error case */
133 /* Mail Transfer Protocol open connection
134 * Accepts: network driver
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
;
147 char *s
,tmp
[MAILTMPLEN
];
148 NETSTREAM
*netstream
;
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
);
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
)) {
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
);
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
);
197 stream
= smtp_close (stream
);
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",
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",
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
);
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 */
242 (long) mail_parameters (NIL
,GET_SASLUSESPTRNAME
,NIL
) ?
243 net_remotehost (netstream
) : net_host (netstream
),
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
);
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
)) {
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
;
275 * Accepts: stream to login
276 * parsed network mailbox structure
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
;
286 char usr
[MAILTMPLEN
];
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 (lsterr
) { /* previous authenticator failed? */
293 sprintf (tmp
,"Retrying using %s authentication after %.80s",
296 fs_give ((void **) &lsterr
);
298 trial
= 0; /* initial trial count */
299 tmp
[0] = '\0'; /* empty buffer */
300 if (stream
->netstream
) do {
302 sprintf (tmp
,"Retrying %s authentication after %.80s",at
->name
,lsterr
);
304 fs_give ((void **) &lsterr
);
306 stream
->saslcancel
= NIL
;
307 if (smtp_send (stream
,"AUTH",at
->name
) == SMTPAUTHREADY
) {
308 /* hide client authentication responses */
309 if (!(at
->flags
& AU_SECURE
)) stream
->sensitive
= T
;
310 if ((*at
->client
) (smtp_challenge
,smtp_response
,"smtp",mb
,stream
,
312 if (stream
->replycode
== SMTPAUTHED
) {
313 ESMTP
.auth
= NIL
; /* disable authenticators */
316 /* if main program requested cancellation */
317 else if (!trial
) mm_log ("SMTP Authentication cancelled",ERROR
);
319 stream
->sensitive
= NIL
;/* unhide */
321 /* remember response if error and no cancel */
322 if (!ret
&& trial
) lsterr
= cpystr (stream
->reply
);
323 } while (!ret
&& stream
->netstream
&& trial
&&
324 (trial
< smtp_maxlogintrials
));
326 if (lsterr
) { /* previous authenticator failed? */
327 if (!stream
->saslcancel
) { /* don't do this if a cancel */
328 sprintf (tmp
,"Can not authenticate to SMTP server: %.80s",lsterr
);
331 fs_give ((void **) &lsterr
);
333 return ret
; /* authentication failed */
336 /* Get challenge to authenticator in binary
338 * pointer to returned size
339 * Returns: challenge or NIL if not challenge
342 void *smtp_challenge (void *s
,unsigned long *len
)
344 char tmp
[MAILTMPLEN
];
346 SENDSTREAM
*stream
= (SENDSTREAM
*) s
;
347 if ((stream
->replycode
== SMTPAUTHREADY
) &&
348 !(ret
= rfc822_base64 ((unsigned char *) stream
->reply
+ 4,
349 strlen (stream
->reply
+ 4),len
))) {
350 sprintf (tmp
,"SMTP SERVER BUG (invalid challenge, continuing): %.80s",stream
->reply
+4);
352 ret
= cpystr(""); /* This is silly: fake a reply, it will be ignored */
358 /* Send authenticator response in BASE64
359 * Accepts: MAIL stream
365 long smtp_response (void *s
,char *response
,unsigned long size
)
367 SENDSTREAM
*stream
= (SENDSTREAM
*) s
;
370 if (response
) { /* make CRLFless BASE64 string */
372 for (t
= (char *) rfc822_binary ((void *) response
,size
,&i
),u
= t
,j
= 0;
373 j
< i
; j
++) if (t
[j
] > ' ') *u
++ = t
[j
];
374 *u
= '\0'; /* tie off string */
375 i
= smtp_send (stream
,t
,NIL
);
376 fs_give ((void **) &t
);
378 else i
= smtp_send (stream
,"",NIL
);
380 else { /* abort requested */
381 i
= smtp_send (stream
,"*",NIL
);
382 stream
->saslcancel
= T
; /* mark protocol-requested SASL cancel */
387 /* Mail Transfer Protocol close connection
388 * Accepts: SEND stream
389 * Returns: NIL always
392 SENDSTREAM
*smtp_close (SENDSTREAM
*stream
)
394 if (stream
) { /* send "QUIT" */
395 if (stream
->netstream
) { /* do close actions if have netstream */
396 smtp_send (stream
,"QUIT",NIL
);
397 if (stream
->netstream
) net_close (stream
->netstream
);
400 if (stream
->host
) fs_give ((void **) &stream
->host
);
401 if (stream
->reply
) fs_give ((void **) &stream
->reply
);
402 if (ESMTP
.dsn
.envid
) fs_give ((void **) &ESMTP
.dsn
.envid
);
403 if (ESMTP
.atrn
.domains
) fs_give ((void **) &ESMTP
.atrn
.domains
);
404 fs_give ((void **) &stream
);/* flush the stream */
409 /* Mail Transfer Protocol deliver mail
410 * Accepts: SEND stream
411 * delivery option (MAIL, SEND, SAML, SOML)
414 * Returns: T on success, NIL on failure
417 long smtp_mail (SENDSTREAM
*stream
,char *type
,ENVELOPE
*env
,BODY
*body
)
420 char tmp
[SENDBUFLEN
+1], smtpserver
[SENDBUFLEN
+1];
423 buf
.f
= smtp_soutr
; /* initialize buffer */
424 buf
.s
= stream
->netstream
;
425 buf
.end
= (buf
.beg
= buf
.cur
= tmp
) + SENDBUFLEN
;
426 tmp
[SENDBUFLEN
] = '\0'; /* must have additional null guard byte */
427 smtpserver
[SENDBUFLEN
] = '\0';
428 if (!(env
->to
|| env
->cc
|| env
->bcc
)) {
429 /* no recipients in request */
430 smtp_seterror (stream
,SMTPHARDERROR
,"No recipients specified");
433 /* get this now in case the rug is pulled from under us */
434 sprintf (smtpserver
,"{%.200s/smtp%s}<none>",
435 (long) mail_parameters (NIL
,GET_TRUSTDNS
,NIL
) ?
436 ((long) mail_parameters (NIL
,GET_SASLUSESPTRNAME
,NIL
) ?
437 net_remotehost (stream
->netstream
) :
438 net_host (stream
->netstream
)) :
440 (stream
->netstream
->dtb
==
441 (NETDRIVER
*) mail_parameters (NIL
,GET_SSLDRIVER
,NIL
)) ?
444 if (retry
) { /* need to retry with authentication? */
446 /* make sure stream is in good shape */
447 smtp_send (stream
,"RSET",NIL
);
448 /* yes, build remote name for authentication */
449 mail_valid_net_parse (smtpserver
,&mb
);
450 if (!smtp_auth (stream
,&mb
,smtpserver
)) return NIL
;
451 retry
= NIL
; /* no retry at this point */
454 strcpy (tmp
,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
456 if (env
->return_path
&& env
->return_path
->host
&&
457 !((strlen (env
->return_path
->mailbox
) > SMTPMAXLOCALPART
) ||
458 (strlen (env
->return_path
->host
) > SMTPMAXDOMAIN
))) {
459 rfc822_cat (tmp
,env
->return_path
->mailbox
,NIL
);
460 sprintf (tmp
+ strlen (tmp
),"@%s",env
->return_path
->host
);
462 #else /* old code with A-D-L support */
463 if (env
->return_path
&& env
->return_path
->host
&&
464 !((env
->return_path
->adl
&&
465 (strlen (env
->return_path
->adl
) > SMTPMAXPATH
)) ||
466 (strlen (env
->return_path
->mailbox
) > SMTPMAXLOCALPART
) ||
467 (strlen (env
->return_path
->host
) > SMTPMAXDOMAIN
)))
468 rfc822_address (tmp
,env
->return_path
);
472 if (ESMTP
.eightbit
.ok
&& ESMTP
.eightbit
.want
)
473 strcat (tmp
," BODY=8BITMIME");
474 if (ESMTP
.dsn
.ok
&& ESMTP
.dsn
.want
) {
475 strcat (tmp
,ESMTP
.dsn
.full
? " RET=FULL" : " RET=HDRS");
477 sprintf (tmp
+ strlen (tmp
)," ENVID=%.100s",ESMTP
.dsn
.envid
);
480 /* send "MAIL FROM" command */
481 switch (smtp_send (stream
,type
,tmp
)) {
482 case SMTPUNAVAIL
: /* mailbox unavailable? */
483 case SMTPWANTAUTH
: /* wants authentication? */
485 if (ESMTP
.auth
) retry
= T
;/* yes, retry with authentication */
486 case SMTPOK
: /* looks good */
488 default: /* other failure */
489 smtp_send (stream
,"RSET",NIL
);
492 /* negotiate the recipients */
493 if (!retry
&& env
->to
) retry
= smtp_rcpt (stream
,env
->to
,&error
);
494 if (!retry
&& env
->cc
) retry
= smtp_rcpt (stream
,env
->cc
,&error
);
495 if (!retry
&& env
->bcc
) retry
= smtp_rcpt (stream
,env
->bcc
,&error
);
496 if (!retry
&& error
) { /* any recipients failed? */
497 smtp_send (stream
,"RSET",NIL
);
498 smtp_seterror (stream
,SMTPHARDERROR
,"One or more recipients failed");
502 /* negotiate data command */
503 if (!(smtp_send (stream
,"DATA",NIL
) == SMTPREADY
)) {
504 smtp_send (stream
,"RSET",NIL
);
507 /* send message data */
508 if (!rfc822_output_full (&buf
,env
,body
,
509 ESMTP
.eightbit
.ok
&& ESMTP
.eightbit
.want
)) {
510 smtp_fake (stream
,"SMTP connection broken (message data)");
511 return NIL
; /* can't do much else here */
513 /* send trailing dot */
514 if (smtp_send (stream
,".",NIL
) != SMTPOK
) {
515 smtp_send (stream
,"RSET",NIL
);
521 /* Simple Mail Transfer Protocol send VERBose
522 * Accepts: SMTP stream
523 * Returns: T if successful, else NIL
525 * Descriptive text formerly in [al]pine sources:
526 * At worst, this command may cause the SMTP connection to get nuked. Modern
527 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
528 * arg is for PMDF). What's more, if it works, the reply code and accompanying
529 * text may vary from server to server.
532 long smtp_verbose (SENDSTREAM
*stream
)
534 /* accept any 2xx reply code */
535 return ((smtp_send (stream
,"VERB","ON") / (long) 100) == 2) ? LONGT
: NIL
;
538 /* Internal routines */
541 /* Simple Mail Transfer Protocol send recipient
542 * Accepts: SMTP stream
544 * pointer to error flag
545 * Returns: T if should retry, else NIL
548 long smtp_rcpt (SENDSTREAM
*stream
,ADDRESS
*adr
,long *error
)
550 char *s
,tmp
[2*MAILTMPLEN
],orcpt
[MAILTMPLEN
];
551 while (adr
) { /* for each address on the list */
552 /* clear any former error */
553 if (adr
->error
) fs_give ((void **) &adr
->error
);
554 if (adr
->host
) { /* ignore group syntax */
555 /* enforce SMTP limits to protect the buffer */
556 if (strlen (adr
->mailbox
) > MAXLOCALPART
) {
557 adr
->error
= cpystr ("501 Recipient name too long");
560 else if ((strlen (adr
->host
) > SMTPMAXDOMAIN
)) {
561 adr
->error
= cpystr ("501 Recipient domain too long");
564 #ifndef RFC2821 /* old code with A-D-L support */
565 else if (adr
->adl
&& (strlen (adr
->adl
) > SMTPMAXPATH
)) {
566 adr
->error
= cpystr ("501 Path too long");
572 strcpy (tmp
,"TO:<"); /* compose "RCPT TO:<return-path>" */
574 rfc822_cat (tmp
,adr
->mailbox
,NIL
);
575 sprintf (tmp
+ strlen (tmp
),"@%s>",adr
->host
);
576 #else /* old code with A-D-L support */
577 rfc822_address (tmp
,adr
);
580 /* want notifications */
581 if (ESMTP
.ok
&& ESMTP
.dsn
.ok
&& ESMTP
.dsn
.want
) {
582 /* yes, start with prefix */
583 strcat (tmp
," NOTIFY=");
584 s
= tmp
+ strlen (tmp
);
585 if (ESMTP
.dsn
.notify
.failure
) strcat (s
,"FAILURE,");
586 if (ESMTP
.dsn
.notify
.delay
) strcat (s
,"DELAY,");
587 if (ESMTP
.dsn
.notify
.success
) strcat (s
,"SUCCESS,");
588 /* tie off last comma */
589 if (*s
) s
[strlen (s
) - 1] = '\0';
590 else strcat (tmp
,"NEVER");
591 if (adr
->orcpt
.addr
) {
592 sprintf (orcpt
,"%.498s;%.498s",
593 adr
->orcpt
.type
? adr
->orcpt
.type
: "rfc822",
595 sprintf (tmp
+ strlen (tmp
)," ORCPT=%.500s",orcpt
);
598 switch (smtp_send (stream
,"RCPT",tmp
)) {
599 case SMTPOK
: /* looks good */
601 case SMTPUNAVAIL
: /* mailbox unavailable? */
602 case SMTPWANTAUTH
: /* wants authentication? */
604 if (ESMTP
.auth
) return T
;
605 default: /* other failure */
606 *error
= T
; /* note that an error occurred */
607 adr
->error
= cpystr (stream
->reply
);
611 adr
= adr
->next
; /* do any subsequent recipients */
613 return NIL
; /* no retry called for */
616 /* Simple Mail Transfer Protocol send command
617 * Accepts: SEND stream
619 * Returns: reply code
622 long smtp_send (SENDSTREAM
*stream
,char *command
,char *args
)
625 char *s
= (char *) fs_get (strlen (command
) + (args
? strlen (args
) + 1 : 0)
627 /* build the complete command */
628 if (args
) sprintf (s
,"%s %s",command
,args
);
629 else strcpy (s
,command
);
630 if (stream
->debug
) mail_dlog (s
,stream
->sensitive
);
631 strcat (s
,"\015\012");
632 /* send the command */
633 if (stream
->netstream
&& net_soutr (stream
->netstream
,s
)) {
634 do stream
->replycode
= smtp_reply (stream
);
635 while ((stream
->replycode
< 100) || (stream
->reply
[3] == '-'));
636 ret
= stream
->replycode
;
638 else ret
= smtp_fake (stream
,"SMTP connection broken (command)");
639 fs_give ((void **) &s
);
644 /* Simple Mail Transfer Protocol get reply
645 * Accepts: SMTP stream
646 * Returns: reply code
649 long smtp_reply (SENDSTREAM
*stream
)
651 smtpverbose_t pv
= (smtpverbose_t
) mail_parameters (NIL
,GET_SMTPVERBOSE
,NIL
);
653 /* flush old reply */
654 if (stream
->reply
) fs_give ((void **) &stream
->reply
);
656 if (stream
->netstream
&& (stream
->reply
= net_getline (stream
->netstream
))) {
657 if (stream
->debug
) mm_dlog (stream
->reply
);
658 /* return response code */
659 reply
= atol (stream
->reply
);
660 if (pv
&& (reply
< 100)) (*pv
) (stream
->reply
);
662 else reply
= smtp_fake (stream
,"SMTP connection broken (reply)");
666 /* Simple Mail Transfer Protocol send EHLO
667 * Accepts: SMTP stream
668 * host name to use in EHLO
670 * Returns: reply code
673 long smtp_ehlo (SENDSTREAM
*stream
,char *host
,NETMBX
*mb
)
676 long flags
= (mb
->secflag
? AU_SECURE
: NIL
) |
677 (mb
->authuser
[0] ? AU_AUTHUSER
: NIL
);
678 char *s
,*t
,*r
,tmp
[MAILTMPLEN
];
679 /* clear ESMTP data */
680 memset (&ESMTP
,0,sizeof (ESMTP
));
681 if (mb
->loser
) return 500; /* never do EHLO if a loser */
682 sprintf (tmp
,"EHLO %s",host
); /* build the complete command */
683 if (stream
->debug
) mm_dlog (tmp
);
684 strcat (tmp
,"\015\012");
685 /* send the command */
686 if (!net_soutr (stream
->netstream
,tmp
))
687 return smtp_fake (stream
,"SMTP connection broken (EHLO)");
688 /* got an OK reply? */
689 do if ((i
= smtp_reply (stream
)) == SMTPOK
) {
691 if (stream
->reply
[4] && stream
->reply
[5] && stream
->reply
[6] &&
692 stream
->reply
[7] && (stream
->reply
[8] == '=')) stream
->reply
[8] = ' ';
693 /* get option code */
694 if (!(s
= strtok_r (stream
->reply
+4," ",&r
)));
695 /* have option, does it have a value */
696 else if ((t
= strtok_r (NIL
," ",&r
)) && *t
) {
697 /* EHLO options which take arguments */
698 if (!compare_cstring (s
,"SIZE")) {
699 if (isdigit (*t
)) ESMTP
.size
.limit
= strtoul (t
,&t
,10);
702 else if (!compare_cstring (s
,"DELIVERBY")) {
703 if (isdigit (*t
)) ESMTP
.deliverby
.minby
= strtoul (t
,&t
,10);
704 ESMTP
.deliverby
.ok
= T
;
706 else if (!compare_cstring (s
,"ATRN")) {
707 ESMTP
.atrn
.domains
= cpystr (t
);
710 else if (!compare_cstring (s
,"AUTH"))
711 do if ((j
= mail_lookup_auth_name (t
,flags
)) &&
712 (--j
< MAXAUTHENTICATORS
)) ESMTP
.auth
|= (1 << j
);
713 while ((t
= strtok_r (NIL
," ",&r
)) && *t
);
715 /* EHLO options which do not take arguments */
716 else if (!compare_cstring (s
,"SIZE")) ESMTP
.size
.ok
= T
;
717 else if (!compare_cstring (s
,"8BITMIME")) ESMTP
.eightbit
.ok
= T
;
718 else if (!compare_cstring (s
,"DSN")) ESMTP
.dsn
.ok
= T
;
719 else if (!compare_cstring (s
,"ATRN")) ESMTP
.atrn
.ok
= T
;
720 else if (!compare_cstring (s
,"SEND")) ESMTP
.service
.send
= T
;
721 else if (!compare_cstring (s
,"SOML")) ESMTP
.service
.soml
= T
;
722 else if (!compare_cstring (s
,"SAML")) ESMTP
.service
.saml
= T
;
723 else if (!compare_cstring (s
,"EXPN")) ESMTP
.service
.expn
= T
;
724 else if (!compare_cstring (s
,"HELP")) ESMTP
.service
.help
= T
;
725 else if (!compare_cstring (s
,"TURN")) ESMTP
.service
.turn
= T
;
726 else if (!compare_cstring (s
,"ETRN")) ESMTP
.service
.etrn
= T
;
727 else if (!compare_cstring (s
,"STARTTLS")) ESMTP
.service
.starttls
= T
;
728 else if (!compare_cstring (s
,"RELAY")) ESMTP
.service
.relay
= T
;
729 else if (!compare_cstring (s
,"PIPELINING")) ESMTP
.service
.pipe
= T
;
730 else if (!compare_cstring (s
,"ENHANCEDSTATUSCODES"))
731 ESMTP
.service
.ensc
= T
;
732 else if (!compare_cstring (s
,"BINARYMIME")) ESMTP
.service
.bmime
= T
;
733 else if (!compare_cstring (s
,"CHUNKING")) ESMTP
.service
.chunk
= T
;
735 while ((i
< 100) || (stream
->reply
[3] == '-'));
736 /* disable LOGIN if PLAIN also advertised */
737 if ((j
= mail_lookup_auth_name ("PLAIN",NIL
)) && (--j
< MAXAUTHENTICATORS
) &&
738 (ESMTP
.auth
& (1 << j
)) &&
739 (j
= mail_lookup_auth_name ("LOGIN",NIL
)) && (--j
< MAXAUTHENTICATORS
))
740 ESMTP
.auth
&= ~(1 << j
);
741 return i
; /* return the response code */
744 /* Simple Mail Transfer Protocol set fake error and abort
745 * Accepts: SMTP stream
747 * Returns: SMTPSOFTFATAL, always
750 long smtp_fake (SENDSTREAM
*stream
,char *text
)
752 if (stream
->netstream
) { /* close net connection if still open */
753 net_close (stream
->netstream
);
754 stream
->netstream
= NIL
;
757 return smtp_seterror (stream
,SMTPSOFTFATAL
,text
);
761 /* Simple Mail Transfer Protocol set error
762 * Accepts: SMTP stream
765 * Returns: error code
768 static long smtp_seterror (SENDSTREAM
*stream
,long code
,char *text
)
770 /* flush any old reply */
771 if (stream
->reply
) fs_give ((void **) &stream
->reply
);
772 /* set up pseudo-reply string */
773 stream
->reply
= (char *) fs_get (20+strlen (text
));
774 sprintf (stream
->reply
,"%ld %s",code
,text
);
775 return code
; /* return error code */
779 /* Simple Mail Transfer Protocol filter mail
782 * Returns: T on success, NIL on failure
785 long smtp_soutr (void *stream
,char *s
)
788 /* "." on first line */
789 if (s
[0] == '.') net_sout (stream
,".",1);
790 /* find lines beginning with a "." */
791 while (t
= strstr (s
,"\015\012.")) {
792 c
= *(t
+= 3); /* remember next character after "." */
793 *t
= '\0'; /* tie off string */
795 if (!net_sout (stream
,s
,t
-s
)) return NIL
;
796 *t
= c
; /* restore delimiter */
797 s
= t
- 1; /* push pointer up to the "." */
799 /* output remainder of text */
800 return *s
? net_soutr (stream
,s
) : T
;