imapext-2007

view src/c-client/smtp.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2008 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Simple Mail Transfer Protocol (SMTP) routines
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 27 July 1988
26 * Last Edited: 28 January 2008
27 *
28 * This original version of this file is
29 * Copyright 1988 Stanford University
30 * and was developed in the Symbolic Systems Resources Group of the Knowledge
31 * Systems Laboratory at Stanford University in 1987-88, and was funded by the
32 * Biomedical Research Technology Program of the National Institutes of Health
33 * under grant number RR-00785.
34 */
37 #include <ctype.h>
38 #include <stdio.h>
39 #include "c-client.h"
41 /* Constants */
43 #define SMTPSSLPORT (long) 465 /* former assigned SSL TCP contact port */
44 #define SMTPGREET (long) 220 /* SMTP successful greeting */
45 #define SMTPAUTHED (long) 235 /* SMTP successful authentication */
46 #define SMTPOK (long) 250 /* SMTP OK code */
47 #define SMTPAUTHREADY (long) 334/* SMTP ready for authentication */
48 #define SMTPREADY (long) 354 /* SMTP ready for data */
49 #define SMTPSOFTFATAL (long) 421/* SMTP soft fatal code */
50 #define SMTPWANTAUTH (long) 505 /* SMTP authentication needed */
51 #define SMTPWANTAUTH2 (long) 530/* SMTP authentication needed */
52 #define SMTPUNAVAIL (long) 550 /* SMTP mailbox unavailable */
53 #define SMTPHARDERROR (long) 554/* SMTP miscellaneous hard failure */
56 /* Convenient access to protocol-specific data */
58 #define ESMTP stream->protocol.esmtp
61 /* Function prototypes */
63 void *smtp_challenge (void *s,unsigned long *len);
64 long smtp_response (void *s,char *response,unsigned long size);
65 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp);
66 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error);
67 long smtp_send (SENDSTREAM *stream,char *command,char *args);
68 long smtp_reply (SENDSTREAM *stream);
69 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb);
70 long smtp_fake (SENDSTREAM *stream,char *text);
71 static long smtp_seterror (SENDSTREAM *stream,long code,char *text);
72 long smtp_soutr (void *stream,char *s);
74 /* Mailer parameters */
76 static unsigned long smtp_maxlogintrials = MAXLOGINTRIALS;
77 static long smtp_port = 0; /* default port override */
78 static long smtp_sslport = 0;
81 #ifndef RFC2821
82 #define RFC2821 /* RFC 2821 compliance */
83 #endif
85 /* SMTP limits, current as of RFC 2821 */
87 #define SMTPMAXLOCALPART 64
88 #define SMTPMAXDOMAIN 255
89 #define SMTPMAXPATH 256
92 /* I have seen local parts of more than 64 octets, in spite of the SMTP
93 * limits. So, we'll have a more generous limit that's still guaranteed
94 * not to pop the buffer, and let the server worry about it. As of this
95 * writing, it comes out to 240. Anyone with a mailbox name larger than
96 * that is in serious need of a life or at least a new ISP! 23 June 1998
97 */
99 #define MAXLOCALPART ((MAILTMPLEN - (SMTPMAXDOMAIN + SMTPMAXPATH + 32)) / 2)
101 /* Mail Transfer Protocol manipulate driver parameters
102 * Accepts: function code
103 * function-dependent value
104 * Returns: function-dependent return value
105 */
107 void *smtp_parameters (long function,void *value)
108 {
109 switch ((int) function) {
110 case SET_MAXLOGINTRIALS:
111 smtp_maxlogintrials = (unsigned long) value;
112 break;
113 case GET_MAXLOGINTRIALS:
114 value = (void *) smtp_maxlogintrials;
115 break;
116 case SET_SMTPPORT:
117 smtp_port = (long) value;
118 break;
119 case GET_SMTPPORT:
120 value = (void *) smtp_port;
121 break;
122 case SET_SSLSMTPPORT:
123 smtp_sslport = (long) value;
124 break;
125 case GET_SSLSMTPPORT:
126 value = (void *) smtp_sslport;
127 break;
128 default:
129 value = NIL; /* error case */
130 break;
131 }
132 return value;
133 }
135 /* Mail Transfer Protocol open connection
136 * Accepts: network driver
137 * service host list
138 * port number
139 * service name
140 * SMTP open options
141 * Returns: SEND stream on success, NIL on failure
142 */
144 SENDSTREAM *smtp_open_full (NETDRIVER *dv,char **hostlist,char *service,
145 unsigned long port,long options)
146 {
147 SENDSTREAM *stream = NIL;
148 long reply;
149 char *s,tmp[MAILTMPLEN];
150 NETSTREAM *netstream;
151 NETMBX mb;
152 if (!(hostlist && *hostlist)) mm_log ("Missing SMTP service host",ERROR);
153 /* maximum domain name is 64 characters */
154 else do if (strlen (*hostlist) < SMTPMAXDOMAIN) {
155 sprintf (tmp,"{%.1000s}",*hostlist);
156 if (!mail_valid_net_parse_work (tmp,&mb,service ? service : "smtp") ||
157 mb.anoflag || mb.readonlyflag) {
158 sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
159 mm_log (tmp,ERROR);
160 }
161 else { /* light tryssl flag if requested */
162 mb.trysslflag = (options & SOP_TRYSSL) ? T : NIL;
163 /* explicit port overrides all */
164 if (mb.port) port = mb.port;
165 /* else /submit overrides port argument */
166 else if (!compare_cstring (mb.service,"submit")) {
167 port = SUBMITTCPPORT; /* override port, use IANA name */
168 strcpy (mb.service,"submission");
169 }
170 /* else port argument overrides SMTP port */
171 else if (!port) port = smtp_port ? smtp_port : SMTPTCPPORT;
172 if (netstream = /* try to open ordinary connection */
173 net_open (&mb,dv,port,
174 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
175 "*smtps",smtp_sslport ? smtp_sslport : SMTPSSLPORT)) {
176 stream = (SENDSTREAM *) memset (fs_get (sizeof (SENDSTREAM)),0,
177 sizeof (SENDSTREAM));
178 stream->netstream = netstream;
179 stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
180 net_host (netstream) : mb.host);
181 stream->debug = (mb.dbgflag || (options & OP_DEBUG)) ? T : NIL;
182 if (options & SOP_SECURE) mb.secflag = T;
183 /* get name of local host to use */
184 s = compare_cstring ("localhost",mb.host) ?
185 net_localhost (netstream) : "localhost";
187 do reply = smtp_reply (stream);
188 while ((reply < 100) || (stream->reply[3] == '-'));
189 if (reply != SMTPGREET){/* get SMTP greeting */
190 sprintf (tmp,"SMTP greeting failure: %.80s",stream->reply);
191 mm_log (tmp,ERROR);
192 stream = smtp_close (stream);
193 }
194 /* try EHLO first, then HELO */
195 else if (((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) &&
196 ((reply = smtp_send (stream,"HELO",s)) != SMTPOK)) {
197 sprintf (tmp,"SMTP hello failure: %.80s",stream->reply);
198 mm_log (tmp,ERROR);
199 stream = smtp_close (stream);
200 }
201 else {
202 NETDRIVER *ssld =(NETDRIVER *)mail_parameters(NIL,GET_SSLDRIVER,NIL);
203 sslstart_t stls = (sslstart_t) mail_parameters(NIL,GET_SSLSTART,NIL);
204 ESMTP.ok = T; /* ESMTP server, start TLS if present */
205 if (!dv && stls && ESMTP.service.starttls &&
206 !mb.sslflag && !mb.notlsflag &&
207 (smtp_send (stream,"STARTTLS",NIL) == SMTPGREET)) {
208 mb.tlsflag = T; /* TLS OK, get into TLS at this end */
209 stream->netstream->dtb = ssld;
210 /* TLS started, negotiate it */
211 if (!(stream->netstream->stream = (*stls)
212 (stream->netstream->stream,mb.host,
213 (mb.tlssslv23 ? NIL : NET_TLSCLIENT) |
214 (mb.novalidate ? NET_NOVALIDATECERT:NIL)))){
215 /* TLS negotiation failed after STARTTLS */
216 sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",
217 mb.host);
218 mm_log (tmp,ERROR);
219 /* close without doing QUIT */
220 if (stream->netstream) net_close (stream->netstream);
221 stream->netstream = NIL;
222 stream = smtp_close (stream);
223 }
224 /* TLS OK, re-negotiate EHLO */
225 else if ((reply = smtp_ehlo (stream,s,&mb)) != SMTPOK) {
226 sprintf (tmp,"SMTP EHLO failure after STARTTLS: %.80s",
227 stream->reply);
228 mm_log (tmp,ERROR);
229 stream = smtp_close (stream);
230 }
231 else ESMTP.ok = T; /* TLS OK and EHLO successful */
232 }
233 else if (mb.tlsflag) {/* user specified /tls but can't do it */
234 sprintf (tmp,"TLS unavailable with this server: %.80s",mb.host);
235 mm_log (tmp,ERROR);
236 stream = smtp_close (stream);
237 }
239 /* remote name for authentication */
240 if (stream && ((mb.secflag || mb.user[0]))) {
241 if (ESMTP.auth) { /* use authenticator? */
242 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
243 /* remote name for authentication */
244 strncpy (mb.host,
245 (long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
246 net_remotehost (netstream) : net_host (netstream),
247 NETMAXHOST-1);
248 mb.host[NETMAXHOST-1] = '\0';
249 }
250 if (!smtp_auth (stream,&mb,tmp)) stream = smtp_close (stream);
251 }
252 else { /* no available authenticators? */
253 sprintf (tmp,"%sSMTP authentication not available: %.80s",
254 mb.secflag ? "Secure " : "",mb.host);
255 mm_log (tmp,ERROR);
256 stream = smtp_close (stream);
257 }
258 }
259 }
260 }
261 }
262 } while (!stream && *++hostlist);
263 if (stream) { /* set stream options if have a stream */
264 if (options &(SOP_DSN | SOP_DSN_NOTIFY_FAILURE | SOP_DSN_NOTIFY_DELAY |
265 SOP_DSN_NOTIFY_SUCCESS | SOP_DSN_RETURN_FULL)) {
266 ESMTP.dsn.want = T;
267 if (options & SOP_DSN_NOTIFY_FAILURE) ESMTP.dsn.notify.failure = T;
268 if (options & SOP_DSN_NOTIFY_DELAY) ESMTP.dsn.notify.delay = T;
269 if (options & SOP_DSN_NOTIFY_SUCCESS) ESMTP.dsn.notify.success = T;
270 if (options & SOP_DSN_RETURN_FULL) ESMTP.dsn.full = T;
271 }
272 if (options & SOP_8BITMIME) ESMTP.eightbit.want = T;
273 }
274 return stream;
275 }
277 /* SMTP authenticate
278 * Accepts: stream to login
279 * parsed network mailbox structure
280 * scratch buffer
281 * place to return user name
282 * Returns: T on success, NIL on failure
283 */
285 long smtp_auth (SENDSTREAM *stream,NETMBX *mb,char *tmp)
286 {
287 unsigned long trial,auths;
288 char *lsterr = NIL;
289 char usr[MAILTMPLEN];
290 AUTHENTICATOR *at;
291 long ret = NIL;
292 for (auths = ESMTP.auth, stream->saslcancel = NIL;
293 !ret && stream->netstream && auths &&
294 (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
295 if (lsterr) { /* previous authenticator failed? */
296 sprintf (tmp,"Retrying using %s authentication after %.80s",
297 at->name,lsterr);
298 mm_log (tmp,NIL);
299 fs_give ((void **) &lsterr);
300 }
301 trial = 0; /* initial trial count */
302 tmp[0] = '\0'; /* empty buffer */
303 if (stream->netstream) do {
304 if (lsterr) {
305 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
306 mm_log (tmp,WARN);
307 fs_give ((void **) &lsterr);
308 }
309 stream->saslcancel = NIL;
310 if (smtp_send (stream,"AUTH",at->name) == SMTPAUTHREADY) {
311 /* hide client authentication responses */
312 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
313 if ((*at->client) (smtp_challenge,smtp_response,"smtp",mb,stream,
314 &trial,usr)) {
315 if (stream->replycode == SMTPAUTHED) {
316 ESMTP.auth = NIL; /* disable authenticators */
317 ret = LONGT;
318 }
319 /* if main program requested cancellation */
320 else if (!trial) mm_log ("SMTP Authentication cancelled",ERROR);
321 }
322 stream->sensitive = NIL;/* unhide */
323 }
324 /* remember response if error and no cancel */
325 if (!ret && trial) lsterr = cpystr (stream->reply);
326 } while (!ret && stream->netstream && trial &&
327 (trial < smtp_maxlogintrials));
328 }
329 if (lsterr) { /* previous authenticator failed? */
330 if (!stream->saslcancel) { /* don't do this if a cancel */
331 sprintf (tmp,"Can not authenticate to SMTP server: %.80s",lsterr);
332 mm_log (tmp,ERROR);
333 }
334 fs_give ((void **) &lsterr);
335 }
336 return ret; /* authentication failed */
337 }
339 /* Get challenge to authenticator in binary
340 * Accepts: stream
341 * pointer to returned size
342 * Returns: challenge or NIL if not challenge
343 */
345 void *smtp_challenge (void *s,unsigned long *len)
346 {
347 char tmp[MAILTMPLEN];
348 void *ret = NIL;
349 SENDSTREAM *stream = (SENDSTREAM *) s;
350 if ((stream->replycode == SMTPAUTHREADY) &&
351 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
352 strlen (stream->reply + 4),len))) {
353 sprintf (tmp,"SMTP SERVER BUG (invalid challenge): %.80s",stream->reply+4);
354 mm_log (tmp,ERROR);
355 }
356 return ret;
357 }
360 /* Send authenticator response in BASE64
361 * Accepts: MAIL stream
362 * string to send
363 * length of string
364 * Returns: T, always
365 */
367 long smtp_response (void *s,char *response,unsigned long size)
368 {
369 SENDSTREAM *stream = (SENDSTREAM *) s;
370 unsigned long i,j;
371 char *t,*u;
372 if (response) { /* make CRLFless BASE64 string */
373 if (size) {
374 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
375 j < i; j++) if (t[j] > ' ') *u++ = t[j];
376 *u = '\0'; /* tie off string */
377 i = smtp_send (stream,t,NIL);
378 fs_give ((void **) &t);
379 }
380 else i = smtp_send (stream,"",NIL);
381 }
382 else { /* abort requested */
383 i = smtp_send (stream,"*",NIL);
384 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
385 }
386 return LONGT;
387 }
389 /* Mail Transfer Protocol close connection
390 * Accepts: SEND stream
391 * Returns: NIL always
392 */
394 SENDSTREAM *smtp_close (SENDSTREAM *stream)
395 {
396 if (stream) { /* send "QUIT" */
397 if (stream->netstream) { /* do close actions if have netstream */
398 smtp_send (stream,"QUIT",NIL);
399 if (stream->netstream) /* could have been closed during "QUIT" */
400 net_close (stream->netstream);
401 }
402 /* clean up */
403 if (stream->host) fs_give ((void **) &stream->host);
404 if (stream->reply) fs_give ((void **) &stream->reply);
405 if (ESMTP.dsn.envid) fs_give ((void **) &ESMTP.dsn.envid);
406 if (ESMTP.atrn.domains) fs_give ((void **) &ESMTP.atrn.domains);
407 fs_give ((void **) &stream);/* flush the stream */
408 }
409 return NIL;
410 }
412 /* Mail Transfer Protocol deliver mail
413 * Accepts: SEND stream
414 * delivery option (MAIL, SEND, SAML, SOML)
415 * message envelope
416 * message body
417 * Returns: T on success, NIL on failure
418 */
420 long smtp_mail (SENDSTREAM *stream,char *type,ENVELOPE *env,BODY *body)
421 {
422 RFC822BUFFER buf;
423 char tmp[SENDBUFLEN+1];
424 long error = NIL;
425 long retry = NIL;
426 buf.f = smtp_soutr; /* initialize buffer */
427 buf.s = stream->netstream;
428 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
429 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
430 if (!(env->to || env->cc || env->bcc)) {
431 /* no recipients in request */
432 smtp_seterror (stream,SMTPHARDERROR,"No recipients specified");
433 return NIL;
434 }
435 do { /* make sure stream is in good shape */
436 smtp_send (stream,"RSET",NIL);
437 if (retry) { /* need to retry with authentication? */
438 NETMBX mb;
439 /* yes, build remote name for authentication */
440 sprintf (tmp,"{%.200s/smtp%s}<none>",
441 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
442 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
443 net_remotehost (stream->netstream) :
444 net_host (stream->netstream)) :
445 stream->host,
446 (stream->netstream->dtb ==
447 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL)) ?
448 "/ssl" : "");
449 mail_valid_net_parse (tmp,&mb);
450 if (!smtp_auth (stream,&mb,tmp)) return NIL;
451 retry = NIL; /* no retry at this point */
452 }
454 strcpy (tmp,"FROM:<"); /* compose "MAIL FROM:<return-path>" */
455 #ifdef RFC2821
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);
461 }
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);
469 #endif
470 strcat (tmp,">");
471 if (ESMTP.ok) {
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");
476 if (ESMTP.dsn.envid)
477 sprintf (tmp + strlen (tmp)," ENVID=%.100s",ESMTP.dsn.envid);
478 }
479 }
480 /* send "MAIL FROM" command */
481 switch (smtp_send (stream,type,tmp)) {
482 case SMTPUNAVAIL: /* mailbox unavailable? */
483 case SMTPWANTAUTH: /* wants authentication? */
484 case SMTPWANTAUTH2:
485 if (ESMTP.auth) retry = T;/* yes, retry with authentication */
486 case SMTPOK: /* looks good */
487 break;
488 default: /* other failure */
489 return NIL;
490 }
491 /* negotiate the recipients */
492 if (!retry && env->to) retry = smtp_rcpt (stream,env->to,&error);
493 if (!retry && env->cc) retry = smtp_rcpt (stream,env->cc,&error);
494 if (!retry && env->bcc) retry = smtp_rcpt (stream,env->bcc,&error);
495 if (!retry && error) { /* any recipients failed? */
496 smtp_send (stream,"RSET",NIL);
497 smtp_seterror (stream,SMTPHARDERROR,"One or more recipients failed");
498 return NIL;
499 }
500 } while (retry);
501 /* negotiate data command */
502 if (!(smtp_send (stream,"DATA",NIL) == SMTPREADY)) return NIL;
503 /* send message data */
504 if (!rfc822_output_full (&buf,env,body,
505 ESMTP.eightbit.ok && ESMTP.eightbit.want)) {
506 smtp_fake (stream,"SMTP connection broken (message data)");
507 return NIL; /* can't do much else here */
508 }
509 /* send trailing dot */
510 return (smtp_send (stream,".",NIL) == SMTPOK) ? LONGT : NIL;
511 }
513 /* Simple Mail Transfer Protocol send VERBose
514 * Accepts: SMTP stream
515 * Returns: T if successful, else NIL
516 *
517 * Descriptive text formerly in [al]pine sources:
518 * At worst, this command may cause the SMTP connection to get nuked. Modern
519 * sendmail's recognize it, and possibly other SMTP implementations (the "ON"
520 * arg is for PMDF). What's more, if it works, the reply code and accompanying
521 * text may vary from server to server.
522 */
524 long smtp_verbose (SENDSTREAM *stream)
525 {
526 /* accept any 2xx reply code */
527 return ((smtp_send (stream,"VERB","ON") / (long) 100) == 2) ? LONGT : NIL;
528 }
530 /* Internal routines */
533 /* Simple Mail Transfer Protocol send recipient
534 * Accepts: SMTP stream
535 * address list
536 * pointer to error flag
537 * Returns: T if should retry, else NIL
538 */
540 long smtp_rcpt (SENDSTREAM *stream,ADDRESS *adr,long *error)
541 {
542 char *s,tmp[2*MAILTMPLEN],orcpt[MAILTMPLEN];
543 while (adr) { /* for each address on the list */
544 /* clear any former error */
545 if (adr->error) fs_give ((void **) &adr->error);
546 if (adr->host) { /* ignore group syntax */
547 /* enforce SMTP limits to protect the buffer */
548 if (strlen (adr->mailbox) > MAXLOCALPART) {
549 adr->error = cpystr ("501 Recipient name too long");
550 *error = T;
551 }
552 else if ((strlen (adr->host) > SMTPMAXDOMAIN)) {
553 adr->error = cpystr ("501 Recipient domain too long");
554 *error = T;
555 }
556 #ifndef RFC2821 /* old code with A-D-L support */
557 else if (adr->adl && (strlen (adr->adl) > SMTPMAXPATH)) {
558 adr->error = cpystr ("501 Path too long");
559 *error = T;
560 }
561 #endif
563 else {
564 strcpy (tmp,"TO:<"); /* compose "RCPT TO:<return-path>" */
565 #ifdef RFC2821
566 rfc822_cat (tmp,adr->mailbox,NIL);
567 sprintf (tmp + strlen (tmp),"@%s>",adr->host);
568 #else /* old code with A-D-L support */
569 rfc822_address (tmp,adr);
570 strcat (tmp,">");
571 #endif
572 /* want notifications */
573 if (ESMTP.ok && ESMTP.dsn.ok && ESMTP.dsn.want) {
574 /* yes, start with prefix */
575 strcat (tmp," NOTIFY=");
576 s = tmp + strlen (tmp);
577 if (ESMTP.dsn.notify.failure) strcat (s,"FAILURE,");
578 if (ESMTP.dsn.notify.delay) strcat (s,"DELAY,");
579 if (ESMTP.dsn.notify.success) strcat (s,"SUCCESS,");
580 /* tie off last comma */
581 if (*s) s[strlen (s) - 1] = '\0';
582 else strcat (tmp,"NEVER");
583 if (adr->orcpt.addr) {
584 sprintf (orcpt,"%.498s;%.498s",
585 adr->orcpt.type ? adr->orcpt.type : "rfc822",
586 adr->orcpt.addr);
587 sprintf (tmp + strlen (tmp)," ORCPT=%.500s",orcpt);
588 }
589 }
590 switch (smtp_send (stream,"RCPT",tmp)) {
591 case SMTPOK: /* looks good */
592 break;
593 case SMTPUNAVAIL: /* mailbox unavailable? */
594 case SMTPWANTAUTH: /* wants authentication? */
595 case SMTPWANTAUTH2:
596 if (ESMTP.auth) return T;
597 default: /* other failure */
598 *error = T; /* note that an error occurred */
599 adr->error = cpystr (stream->reply);
600 }
601 }
602 }
603 adr = adr->next; /* do any subsequent recipients */
604 }
605 return NIL; /* no retry called for */
606 }
608 /* Simple Mail Transfer Protocol send command
609 * Accepts: SEND stream
610 * text
611 * Returns: reply code
612 */
614 long smtp_send (SENDSTREAM *stream,char *command,char *args)
615 {
616 long ret;
617 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
618 + 3);
619 /* build the complete command */
620 if (args) sprintf (s,"%s %s",command,args);
621 else strcpy (s,command);
622 if (stream->debug) mail_dlog (s,stream->sensitive);
623 strcat (s,"\015\012");
624 /* send the command */
625 if (stream->netstream && net_soutr (stream->netstream,s)) {
626 do stream->replycode = smtp_reply (stream);
627 while ((stream->replycode < 100) || (stream->reply[3] == '-'));
628 ret = stream->replycode;
629 }
630 else ret = smtp_fake (stream,"SMTP connection broken (command)");
631 fs_give ((void **) &s);
632 return ret;
633 }
636 /* Simple Mail Transfer Protocol get reply
637 * Accepts: SMTP stream
638 * Returns: reply code
639 */
641 long smtp_reply (SENDSTREAM *stream)
642 {
643 smtpverbose_t pv = (smtpverbose_t) mail_parameters (NIL,GET_SMTPVERBOSE,NIL);
644 long reply;
645 /* flush old reply */
646 if (stream->reply) fs_give ((void **) &stream->reply);
647 /* get reply */
648 if (stream->netstream && (stream->reply = net_getline (stream->netstream))) {
649 if (stream->debug) mm_dlog (stream->reply);
650 /* return response code */
651 reply = atol (stream->reply);
652 if (pv && (reply < 100)) (*pv) (stream->reply);
653 }
654 else reply = smtp_fake (stream,"SMTP connection broken (reply)");
655 return reply;
656 }
658 /* Simple Mail Transfer Protocol send EHLO
659 * Accepts: SMTP stream
660 * host name to use in EHLO
661 * NETMBX structure
662 * Returns: reply code
663 */
665 long smtp_ehlo (SENDSTREAM *stream,char *host,NETMBX *mb)
666 {
667 unsigned long i,j;
668 long flags = (mb->secflag ? AU_SECURE : NIL) |
669 (mb->authuser[0] ? AU_AUTHUSER : NIL);
670 char *s,*t,*r,tmp[MAILTMPLEN];
671 /* clear ESMTP data */
672 memset (&ESMTP,0,sizeof (ESMTP));
673 if (mb->loser) return 500; /* never do EHLO if a loser */
674 sprintf (tmp,"EHLO %s",host); /* build the complete command */
675 if (stream->debug) mm_dlog (tmp);
676 strcat (tmp,"\015\012");
677 /* send the command */
678 if (!net_soutr (stream->netstream,tmp))
679 return smtp_fake (stream,"SMTP connection broken (EHLO)");
680 /* got an OK reply? */
681 do if ((i = smtp_reply (stream)) == SMTPOK) {
682 /* hack for AUTH= */
683 if (stream->reply[4] && stream->reply[5] && stream->reply[6] &&
684 stream->reply[7] && (stream->reply[8] == '=')) stream->reply[8] = ' ';
685 /* get option code */
686 if (!(s = strtok_r (stream->reply+4," ",&r)));
687 /* have option, does it have a value */
688 else if ((t = strtok_r (NIL," ",&r)) && *t) {
689 /* EHLO options which take arguments */
690 if (!compare_cstring (s,"SIZE")) {
691 if (isdigit (*t)) ESMTP.size.limit = strtoul (t,&t,10);
692 ESMTP.size.ok = T;
693 }
694 else if (!compare_cstring (s,"DELIVERBY")) {
695 if (isdigit (*t)) ESMTP.deliverby.minby = strtoul (t,&t,10);
696 ESMTP.deliverby.ok = T;
697 }
698 else if (!compare_cstring (s,"ATRN")) {
699 ESMTP.atrn.domains = cpystr (t);
700 ESMTP.atrn.ok = T;
701 }
702 else if (!compare_cstring (s,"AUTH"))
703 do if ((j = mail_lookup_auth_name (t,flags)) &&
704 (--j < MAXAUTHENTICATORS)) ESMTP.auth |= (1 << j);
705 while ((t = strtok_r (NIL," ",&r)) && *t);
706 }
707 /* EHLO options which do not take arguments */
708 else if (!compare_cstring (s,"SIZE")) ESMTP.size.ok = T;
709 else if (!compare_cstring (s,"8BITMIME")) ESMTP.eightbit.ok = T;
710 else if (!compare_cstring (s,"DSN")) ESMTP.dsn.ok = T;
711 else if (!compare_cstring (s,"ATRN")) ESMTP.atrn.ok = T;
712 else if (!compare_cstring (s,"SEND")) ESMTP.service.send = T;
713 else if (!compare_cstring (s,"SOML")) ESMTP.service.soml = T;
714 else if (!compare_cstring (s,"SAML")) ESMTP.service.saml = T;
715 else if (!compare_cstring (s,"EXPN")) ESMTP.service.expn = T;
716 else if (!compare_cstring (s,"HELP")) ESMTP.service.help = T;
717 else if (!compare_cstring (s,"TURN")) ESMTP.service.turn = T;
718 else if (!compare_cstring (s,"ETRN")) ESMTP.service.etrn = T;
719 else if (!compare_cstring (s,"STARTTLS")) ESMTP.service.starttls = T;
720 else if (!compare_cstring (s,"RELAY")) ESMTP.service.relay = T;
721 else if (!compare_cstring (s,"PIPELINING")) ESMTP.service.pipe = T;
722 else if (!compare_cstring (s,"ENHANCEDSTATUSCODES"))
723 ESMTP.service.ensc = T;
724 else if (!compare_cstring (s,"BINARYMIME")) ESMTP.service.bmime = T;
725 else if (!compare_cstring (s,"CHUNKING")) ESMTP.service.chunk = T;
726 }
727 while ((i < 100) || (stream->reply[3] == '-'));
728 /* disable LOGIN if PLAIN also advertised */
729 if ((j = mail_lookup_auth_name ("PLAIN",NIL)) && (--j < MAXAUTHENTICATORS) &&
730 (ESMTP.auth & (1 << j)) &&
731 (j = mail_lookup_auth_name ("LOGIN",NIL)) && (--j < MAXAUTHENTICATORS))
732 ESMTP.auth &= ~(1 << j);
733 return i; /* return the response code */
734 }
736 /* Simple Mail Transfer Protocol set fake error and abort
737 * Accepts: SMTP stream
738 * error text
739 * Returns: SMTPSOFTFATAL, always
740 */
742 long smtp_fake (SENDSTREAM *stream,char *text)
743 {
744 if (stream->netstream) { /* close net connection if still open */
745 net_close (stream->netstream);
746 stream->netstream = NIL;
747 }
748 /* set last error */
749 return smtp_seterror (stream,SMTPSOFTFATAL,text);
750 }
753 /* Simple Mail Transfer Protocol set error
754 * Accepts: SMTP stream
755 * SMTP error code
756 * error text
757 * Returns: error code
758 */
760 static long smtp_seterror (SENDSTREAM *stream,long code,char *text)
761 {
762 /* flush any old reply */
763 if (stream->reply ) fs_give ((void **) &stream->reply);
764 /* set up pseudo-reply string */
765 stream->reply = (char *) fs_get (20+strlen (text));
766 sprintf (stream->reply,"%ld %s",code,text);
767 return code; /* return error code */
768 }
771 /* Simple Mail Transfer Protocol filter mail
772 * Accepts: stream
773 * string
774 * Returns: T on success, NIL on failure
775 */
777 long smtp_soutr (void *stream,char *s)
778 {
779 char c,*t;
780 /* "." on first line */
781 if (s[0] == '.') net_sout (stream,".",1);
782 /* find lines beginning with a "." */
783 while (t = strstr (s,"\015\012.")) {
784 c = *(t += 3); /* remember next character after "." */
785 *t = '\0'; /* tie off string */
786 /* output prefix */
787 if (!net_sout (stream,s,t-s)) return NIL;
788 *t = c; /* restore delimiter */
789 s = t - 1; /* push pointer up to the "." */
790 }
791 /* output remainder of text */
792 return *s ? net_soutr (stream,s) : T;
793 }

UW-IMAP'd extensions by yuuji