2 * Copyright (c) 1998-2006, 2008-2010, 2014 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: usersmtp.c,v 8.488 2013-11-22 20:51:57 ca Exp $")
21 static void esmtp_check __P((char *, bool, MAILER *, MCI *, ENVELOPE *));
22 static void helo_options __P((char *, bool, MAILER *, MCI *, ENVELOPE *));
23 static int smtprcptstat __P((ADDRESS *, MAILER *, MCI *, ENVELOPE *));
26 extern void *sm_sasl_malloc __P((unsigned long));
27 extern void sm_sasl_free __P((void *));
31 ** USERSMTP -- run SMTP protocol from the user end.
33 ** This protocol is described in RFC821.
36 #define SMTPCLOSING 421 /* "Service Shutting Down" */
38 #define ENHSCN(e, d) ((e) == NULL ? (d) : (e))
40 #define ENHSCN_RPOOL(e, d, rpool) \
41 ((e) == NULL ? (d) : sm_rpool_strdup_x(rpool, e))
43 static char SmtpMsgBuffer[MAXLINE]; /* buffer for commands */
44 static char SmtpReplyBuffer[MAXLINE]; /* buffer for replies */
45 static bool SmtpNeedIntro; /* need "while talking" in transcript */
47 ** SMTPINIT -- initialize SMTP.
49 ** Opens the connection and sends the initial protocol.
52 ** m -- mailer to create connection to.
53 ** mci -- the mailer connection info.
55 ** onlyhelo -- send only helo command?
61 ** creates connection and sends initial protocol.
65 smtpinit(m, mci, e, onlyhelo)
80 sm_dprintf("smtpinit ");
81 mci_dump(sm_debug_file(), mci, false);
85 ** Open the connection to the mailer.
89 SmtpMsgBuffer[0] = '\0';
90 CurHostName = mci->mci_host; /* XXX UGLY XXX */
91 if (CurHostName == NULL)
92 CurHostName = MyHostName;
94 state = mci->mci_state;
97 e->e_renhsc[0] = '\0';
99 #endif /* _FFR_ERRCODE */
105 /* need to clear old information */
117 /* shouldn't happen */
122 syserr("451 4.4.0 smtpinit: state CLOSED (was %d)", state);
131 mci->mci_state = MCIS_OPENING;
135 ** Get the greeting message.
136 ** This should appear spontaneously. Give it five minutes to
140 SmtpPhase = mci->mci_phase = "client greeting";
141 sm_setproctitle(true, e, "%s %s: %s",
142 qid_printname(e), CurHostName, mci->mci_phase);
143 r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check, NULL, XS_GREET);
146 if (REPLYTYPE(r) == 4)
148 if (REPLYTYPE(r) != 2)
152 ** Send the HELO command.
153 ** My mother taught me to always introduce myself.
157 if (bitnset(M_ESMTP, m->m_flags) || bitnset(M_LMTP, m->m_flags))
158 mci->mci_flags |= MCIF_ESMTP;
159 hn = mci->mci_heloname ? mci->mci_heloname : MyHostName;
162 #if _FFR_IGNORE_EXT_ON_HELO
163 mci->mci_flags &= ~MCIF_HELO;
164 #endif /* _FFR_IGNORE_EXT_ON_HELO */
165 if (bitnset(M_LMTP, m->m_flags))
167 smtpmessage("LHLO %s", m, mci, hn);
168 SmtpPhase = mci->mci_phase = "client LHLO";
170 else if (bitset(MCIF_ESMTP, mci->mci_flags) &&
171 !bitnset(M_FSMTP, m->m_flags))
173 smtpmessage("EHLO %s", m, mci, hn);
174 SmtpPhase = mci->mci_phase = "client EHLO";
178 smtpmessage("HELO %s", m, mci, hn);
179 SmtpPhase = mci->mci_phase = "client HELO";
180 #if _FFR_IGNORE_EXT_ON_HELO
181 mci->mci_flags |= MCIF_HELO;
182 #endif /* _FFR_IGNORE_EXT_ON_HELO */
184 sm_setproctitle(true, e, "%s %s: %s", qid_printname(e),
185 CurHostName, mci->mci_phase);
187 bitnset(M_LMTP, m->m_flags) ? TimeOuts.to_lhlo
189 helo_options, NULL, XS_EHLO);
192 else if (REPLYTYPE(r) == 5)
194 if (bitset(MCIF_ESMTP, mci->mci_flags) &&
195 !bitnset(M_LMTP, m->m_flags))
197 /* try old SMTP instead */
198 mci->mci_flags &= ~MCIF_ESMTP;
203 else if (REPLYTYPE(r) != 2)
207 ** Check to see if we actually ended up talking to ourself.
208 ** This means we didn't know about an alias or MX, or we managed
209 ** to connect to an echo server.
212 p = strchr(&SmtpReplyBuffer[4], ' ');
215 if (!bitnset(M_NOLOOPCHECK, m->m_flags) &&
216 !bitnset(M_LMTP, m->m_flags) &&
217 sm_strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0)
219 syserr("553 5.3.5 %s config error: mail loops back to me (MX problem?)",
221 mci_setstat(mci, EX_CONFIG, "5.3.5",
222 "553 5.3.5 system config error");
229 ** If this is expected to be another sendmail, send some internal
231 ** If we're running as MSP, "propagate" -v flag if possible.
234 if ((UseMSP && Verbose && bitset(MCIF_VERB, mci->mci_flags))
235 || bitnset(M_INTERNAL, m->m_flags))
237 /* tell it to be verbose */
238 smtpmessage("VERB", m, mci);
239 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL, &enhsc,
245 if (mci->mci_state != MCIS_CLOSED)
247 mci->mci_state = MCIS_OPEN;
251 /* got a 421 error code during startup */
254 mci_setstat(mci, EX_TEMPFAIL, ENHSCN(enhsc, "4.4.2"), NULL);
255 if (mci->mci_state != MCIS_CLOSED)
260 /* XXX should use code from other end iff ENHANCEDSTATUSCODES */
261 mci_setstat(mci, EX_TEMPFAIL, ENHSCN(enhsc, "4.5.0"),
263 if (mci->mci_state != MCIS_CLOSED)
268 mci_setstat(mci, EX_UNAVAILABLE, "5.5.0", SmtpReplyBuffer);
273 ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol
276 ** line -- the response line.
277 ** firstline -- set if this is the first line of the reply.
279 ** mci -- the mailer connection info.
280 ** e -- the envelope.
287 esmtp_check(line, firstline, m, mci, e)
294 if (strstr(line, "ESMTP") != NULL)
295 mci->mci_flags |= MCIF_ESMTP;
298 ** Dirty hack below. Quoting the author:
299 ** This was a response to people who wanted SMTP transmission to be
300 ** just-send-8 by default. Essentially, you could put this tag into
301 ** your greeting message to behave as though the F=8 flag was set on
305 if (strstr(line, "8BIT-OK") != NULL)
306 mci->mci_flags |= MCIF_8BITOK;
310 /* specify prototype so compiler can check calls */
311 static char *str_union __P((char *, char *, SM_RPOOL_T *));
314 ** STR_UNION -- create the union of two lists
317 ** s1, s2 -- lists of items (separated by single blanks).
318 ** rpool -- resource pool from which result is allocated.
321 ** the union of both lists.
325 str_union(s1, s2, rpool)
329 char *hr, *h1, *h, *res;
332 if (s1 == NULL || *s1 == '\0')
334 if (s2 == NULL || *s2 == '\0')
341 sm_syslog(LOG_WARNING, NOQID,
342 "str_union: stringlen1=%d, stringlen2=%d, sum=%d, status=overflow",
347 res = (char *) sm_rpool_malloc(rpool, rl + 2);
354 (void) sm_strlcpy(res, s1, rl);
359 /* walk through s2 */
360 while (h != NULL && *h1 != '\0')
362 /* is there something after the current word? */
363 if ((h = strchr(h1, ' ')) != NULL)
367 /* does the current word appear in s1 ? */
368 if (iteminlist(h1, s1, " ") == NULL)
370 /* add space as delimiter */
376 /* advance pointer in result list */
382 /* there are more items */
392 ** HELO_OPTIONS -- process the options on a HELO line.
395 ** line -- the response line.
396 ** firstline -- set if this is the first line of the reply.
398 ** mci -- the mailer connection info.
399 ** e -- the envelope (unused).
406 helo_options(line, firstline, m, mci, e)
414 #if _FFR_IGNORE_EXT_ON_HELO
415 static bool logged = false;
416 #endif /* _FFR_IGNORE_EXT_ON_HELO */
420 mci_clr_extensions(mci);
421 #if _FFR_IGNORE_EXT_ON_HELO
423 #endif /* _FFR_IGNORE_EXT_ON_HELO */
426 #if _FFR_IGNORE_EXT_ON_HELO
427 else if (bitset(MCIF_HELO, mci->mci_flags))
429 if (LogLevel > 8 && !logged)
431 sm_syslog(LOG_WARNING, NOQID,
432 "server=%s [%s] returned extensions despite HELO command",
433 macvalue(macid("{server_name}"), e),
434 macvalue(macid("{server_addr}"), e));
439 #endif /* _FFR_IGNORE_EXT_ON_HELO */
441 if (strlen(line) < 5)
444 p = strpbrk(line, " =");
447 if (sm_strcasecmp(line, "size") == 0)
449 mci->mci_flags |= MCIF_SIZE;
451 mci->mci_maxsize = atol(p);
453 else if (sm_strcasecmp(line, "8bitmime") == 0)
455 mci->mci_flags |= MCIF_8BITMIME;
456 mci->mci_flags &= ~MCIF_7BIT;
458 else if (sm_strcasecmp(line, "expn") == 0)
459 mci->mci_flags |= MCIF_EXPN;
460 else if (sm_strcasecmp(line, "dsn") == 0)
461 mci->mci_flags |= MCIF_DSN;
462 else if (sm_strcasecmp(line, "enhancedstatuscodes") == 0)
463 mci->mci_flags |= MCIF_ENHSTAT;
464 else if (sm_strcasecmp(line, "pipelining") == 0)
465 mci->mci_flags |= MCIF_PIPELINED;
466 else if (sm_strcasecmp(line, "verb") == 0)
467 mci->mci_flags |= MCIF_VERB;
469 else if (sm_strcasecmp(line, "starttls") == 0)
470 mci->mci_flags |= MCIF_TLS;
471 #endif /* STARTTLS */
472 else if (sm_strcasecmp(line, "deliverby") == 0)
474 mci->mci_flags |= MCIF_DLVR_BY;
476 mci->mci_min_by = atol(p);
479 else if (sm_strcasecmp(line, "auth") == 0)
481 if (p != NULL && *p != '\0' &&
482 !bitset(MCIF_AUTH2, mci->mci_flags))
484 if (mci->mci_saslcap != NULL)
487 ** Create the union with previous auth
488 ** offerings because we recognize "auth "
489 ** and "auth=" (old format).
492 mci->mci_saslcap = str_union(mci->mci_saslcap,
494 mci->mci_flags |= MCIF_AUTH2;
501 mci->mci_saslcap = (char *)
502 sm_rpool_malloc(mci->mci_rpool, l);
503 if (mci->mci_saslcap != NULL)
505 (void) sm_strlcpy(mci->mci_saslcap, p,
507 mci->mci_flags |= MCIF_AUTH;
512 sm_syslog(LOG_DEBUG, NOQID, "AUTH flags=%lx, mechs=%s",
513 mci->mci_flags, mci->mci_saslcap);
519 static int getsimple __P((void *, int, const char **, unsigned *));
520 static int getsecret __P((sasl_conn_t *, void *, int, sasl_secret_t **));
521 static int saslgetrealm __P((void *, int, const char **, const char **));
522 static int readauth __P((char *, bool, SASL_AI_T *m, SM_RPOOL_T *));
523 static int getauth __P((MCI *, ENVELOPE *, SASL_AI_T *));
524 static char *removemech __P((char *, char *, SM_RPOOL_T *));
525 static int attemptauth __P((MAILER *, MCI *, ENVELOPE *, SASL_AI_T *));
527 static sasl_callback_t callbacks[] =
529 { SASL_CB_GETREALM, (sasl_callback_ft)&saslgetrealm, NULL },
530 #define CB_GETREALM_IDX 0
531 { SASL_CB_PASS, (sasl_callback_ft)&getsecret, NULL },
532 #define CB_PASS_IDX 1
533 { SASL_CB_USER, (sasl_callback_ft)&getsimple, NULL },
534 #define CB_USER_IDX 2
535 { SASL_CB_AUTHNAME, (sasl_callback_ft)&getsimple, NULL },
536 #define CB_AUTHNAME_IDX 3
537 { SASL_CB_VERIFYFILE, (sasl_callback_ft)&safesaslfile, NULL },
538 #define CB_SAFESASL_IDX 4
539 { SASL_CB_LIST_END, NULL, NULL }
543 ** INIT_SASL_CLIENT -- initialize client side of Cyrus-SASL
549 ** SASL_OK -- if successful.
550 ** SASL error code -- otherwise.
553 ** checks/sets sasl_clt_init.
556 ** Callbacks are ignored if sasl_client_init() has
557 ** been called before (by a library such as libnss_ldap)
560 static bool sasl_clt_init = false;
569 result = sasl_client_init(callbacks);
571 /* should we retry later again or just remember that it failed? */
572 if (result == SASL_OK)
573 sasl_clt_init = true;
577 ** STOP_SASL_CLIENT -- shutdown client side of Cyrus-SASL
586 ** checks/sets sasl_clt_init.
594 sasl_clt_init = false;
598 ** GETSASLDATA -- process the challenges from the SASL protocol
600 ** This gets the relevant sasl response data out of the reply
604 ** line -- the response line.
605 ** firstline -- set if this is the first line of the reply.
607 ** mci -- the mailer connection info.
608 ** e -- the envelope (unused).
614 static void getsasldata __P((char *, bool, MAILER *, MCI *, ENVELOPE *));
617 getsasldata(line, firstline, m, mci, e)
628 # endif /* SASL < 20000 */
630 /* if not a continue we don't care about it */
634 !isascii(line[1]) || !isdigit(line[1]) ||
635 !isascii(line[2]) || !isdigit(line[2]))
637 SM_FREE_CLR(mci->mci_sasl_string);
641 /* forget about "334 " */
645 /* XXX put this into a macro/function? It's duplicated below */
646 if (mci->mci_sasl_string != NULL)
648 if (mci->mci_sasl_string_len <= len)
650 sm_free(mci->mci_sasl_string); /* XXX */
651 mci->mci_sasl_string = xalloc(len + 1);
655 mci->mci_sasl_string = xalloc(len + 1);
657 result = sasl_decode64(line, len, mci->mci_sasl_string, len + 1,
658 (unsigned int *) &mci->mci_sasl_string_len);
659 if (result != SASL_OK)
661 mci->mci_sasl_string_len = 0;
662 *mci->mci_sasl_string = '\0';
664 # else /* SASL >= 20000 */
665 out = (char *) sm_rpool_malloc_x(mci->mci_rpool, len + 1);
666 result = sasl_decode64(line, len, out, (unsigned int *) &len);
667 if (result != SASL_OK)
674 ** mci_sasl_string is "shared" with Cyrus-SASL library; hence
675 ** it can't be in an rpool unless we use the same memory
676 ** management mechanism (with same rpool!) for Cyrus SASL.
679 if (mci->mci_sasl_string != NULL)
681 if (mci->mci_sasl_string_len <= len)
683 sm_free(mci->mci_sasl_string); /* XXX */
684 mci->mci_sasl_string = xalloc(len + 1);
688 mci->mci_sasl_string = xalloc(len + 1);
690 memcpy(mci->mci_sasl_string, out, len);
691 mci->mci_sasl_string[len] = '\0';
692 mci->mci_sasl_string_len = len;
693 # endif /* SASL >= 20000 */
697 ** READAUTH -- read auth values from a file
700 ** filename -- name of file to read.
701 ** safe -- if set, this is a safe read.
702 ** sai -- where to store auth_info.
703 ** rpool -- resource pool for sai.
706 ** EX_OK -- data succesfully read.
707 ** EX_UNAVAILABLE -- no valid filename.
708 ** EX_TEMPFAIL -- temporary failure.
711 static char *sasl_info_name[] =
720 readauth(filename, safe, sai, rpool)
733 if (filename == NULL || filename[0] == '\0')
734 return EX_UNAVAILABLE;
736 #if !_FFR_ALLOW_SASLINFO
738 ** make sure we don't use a program that is not
739 ** accesible to the user who specified a different authinfo file.
740 ** However, currently we don't pass this info (authinfo file
741 ** specified by user) around, so we just turn off program access.
744 if (filename[0] == '|')
749 char *argv[MAXPV + 1];
752 for (p = strtok(&filename[1], " \t"); p != NULL;
753 p = strtok(NULL, " \t"))
760 pid = prog_open(argv, &fd, CurEnv);
764 f = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
765 (void *) &fd, SM_IO_RDONLY, NULL);
768 #endif /* !_FFR_ALLOW_SASLINFO */
771 sff = SFF_REGONLY|SFF_SAFEDIRPATH|SFF_NOWLINK
772 |SFF_NOGWFILES|SFF_NOWWFILES|SFF_NOWRFILES;
773 if (!bitnset(DBS_GROUPREADABLEAUTHINFOFILE, DontBlameSendmail))
774 sff |= SFF_NOGRFILES;
775 if (DontLockReadFiles)
778 #if _FFR_ALLOW_SASLINFO
780 ** XXX: make sure we don't read or open files that are not
781 ** accesible to the user who specified a different authinfo
786 #else /* _FFR_ALLOW_SASLINFO */
788 sff |= SFF_OPENASROOT;
789 #endif /* _FFR_ALLOW_SASLINFO */
791 f = safefopen(filename, O_RDONLY, 0, sff);
796 sm_syslog(LOG_ERR, NOQID,
797 "AUTH=client, error: can't open %s: %s",
798 filename, sm_errstring(errno));
803 while (lc <= SASL_MECHLIST &&
804 sm_io_fgets(f, SM_TIME_DEFAULT, buf, sizeof(buf)) >= 0)
808 (*sai)[lc] = sm_rpool_strdup_x(rpool, buf);
809 if ((s = strchr((*sai)[lc], '\n')) != NULL)
815 (void) sm_io_close(f, SM_TIME_DEFAULT);
818 if (lc < SASL_PASSWORD)
821 sm_syslog(LOG_ERR, NOQID,
822 "AUTH=client, error: can't read %s from %s",
823 sasl_info_name[lc + 1], filename);
830 ** GETAUTH -- get authinfo from ruleset call
832 ** {server_name}, {server_addr} must be set
835 ** mci -- the mailer connection structure.
836 ** e -- the envelope (including the sender to specify).
837 ** sai -- pointer to authinfo (result).
840 ** EX_OK -- ruleset was succesfully called, data may not
841 ** be available, sai must be checked.
842 ** EX_UNAVAILABLE -- ruleset unavailable (or failed).
843 ** EX_TEMPFAIL -- temporary failure (from ruleset).
846 ** Fills in sai if successful.
855 int i, r, l, got, ret;
857 char pvpbuf[PSBUFSIZE];
859 r = rscap("authinfo", macvalue(macid("{server_name}"), e),
860 macvalue(macid("{server_addr}"), e), e,
861 &pvp, pvpbuf, sizeof(pvpbuf));
864 return EX_UNAVAILABLE;
866 /* other than expected return value: ok (i.e., no auth) */
867 if (pvp == NULL || pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
869 if (pvp[1] != NULL && sm_strncasecmp(pvp[1], "temp", 4) == 0)
873 ** parse the data, put it into sai
874 ** format: "TDstring" (including the '"' !)
875 ** where T is a tag: 'U', ...
876 ** D is a delimiter: ':' or '='
879 ret = EX_OK; /* default return value */
882 while (i < SASL_ENTRIES)
884 if (pvp[i + 1] == NULL)
886 if (pvp[i + 1][0] != '"')
888 switch (pvp[i + 1][1])
913 l = strlen(pvp[i + 1]);
916 if (l <= 3 || pvp[i + 1][l - 1] != '"')
919 /* remove closing quote */
920 pvp[i + 1][l - 1] = '\0';
922 /* remove "TD and " */
924 (*sai)[r] = (char *) sm_rpool_malloc(mci->mci_rpool, l + 1);
925 if ((*sai)[r] == NULL)
927 if (pvp[i + 1][2] == ':')
929 /* ':text' (just copy) */
930 (void) sm_strlcpy((*sai)[r], pvp[i + 1] + 3, l + 1);
933 else if (pvp[i + 1][2] == '=')
937 /* '=base64' (decode) */
939 ret = sasl_decode64(pvp[i + 1] + 3,
940 (unsigned int) l, (*sai)[r],
941 (unsigned int) l + 1, &len);
942 # else /* SASL >= 20000 */
943 ret = sasl_decode64(pvp[i + 1] + 3,
944 (unsigned int) l, (*sai)[r], &len);
945 # endif /* SASL >= 20000 */
953 sm_syslog(LOG_DEBUG, NOQID, "getauth %s=%s",
954 sasl_info_name[r], (*sai)[r]);
958 /* did we get the expected data? */
959 /* XXX: EXTERNAL mechanism only requires (and only uses) SASL_USER */
960 if (!(bitset(SASL_USER_BIT|SASL_AUTHID_BIT, got) &&
961 bitset(SASL_PASSWORD_BIT, got)))
964 /* no authid? copy uid */
965 if (!bitset(SASL_AUTHID_BIT, got))
967 l = strlen((*sai)[SASL_USER]) + 1;
968 (*sai)[SASL_AUTHID] = (char *) sm_rpool_malloc(mci->mci_rpool,
970 if ((*sai)[SASL_AUTHID] == NULL)
972 (void) sm_strlcpy((*sai)[SASL_AUTHID], (*sai)[SASL_USER], l);
975 /* no uid? copy authid */
976 if (!bitset(SASL_USER_BIT, got))
978 l = strlen((*sai)[SASL_AUTHID]) + 1;
979 (*sai)[SASL_USER] = (char *) sm_rpool_malloc(mci->mci_rpool,
981 if ((*sai)[SASL_USER] == NULL)
983 (void) sm_strlcpy((*sai)[SASL_USER], (*sai)[SASL_AUTHID], l);
991 sm_syslog(LOG_WARNING, NOQID,
992 "AUTH=client, relay=%.64s [%.16s], authinfo %sfailed",
993 macvalue(macid("{server_name}"), e),
994 macvalue(macid("{server_addr}"), e),
995 ret == EX_TEMPFAIL ? "temp" : "");
996 for (i = 0; i <= SASL_MECHLIST; i++)
997 (*sai)[i] = NULL; /* just clear; rpool */
1003 ** GETSIMPLE -- callback to get userid or authid
1008 ** result -- (pointer to) result
1009 ** len -- (pointer to) length of result
1012 ** OK/failure values
1016 getsimple(context, id, result, len)
1019 const char **result;
1024 if (result == NULL || context == NULL)
1025 return SASL_BADPARAM;
1026 sai = (SASL_AI_T *) context;
1031 *result = (*sai)[SASL_USER];
1033 sm_syslog(LOG_DEBUG, NOQID, "AUTH username '%s'",
1036 *len = *result != NULL ? strlen(*result) : 0;
1039 case SASL_CB_AUTHNAME:
1040 *result = (*sai)[SASL_AUTHID];
1042 sm_syslog(LOG_DEBUG, NOQID, "AUTH authid '%s'",
1045 *len = *result != NULL ? strlen(*result) : 0;
1048 case SASL_CB_LANGUAGE:
1055 return SASL_BADPARAM;
1060 ** GETSECRET -- callback to get password
1063 ** conn -- connection information
1066 ** psecret -- (pointer to) result
1069 ** OK/failure values
1073 getsecret(conn, context, id, psecret)
1075 SM_UNUSED(void *context);
1077 sasl_secret_t **psecret;
1083 if (conn == NULL || psecret == NULL || id != SASL_CB_PASS)
1084 return SASL_BADPARAM;
1086 mci = (MCI *) context;
1087 authpass = mci->mci_sai[SASL_PASSWORD];
1088 len = strlen(authpass);
1091 ** use an rpool because we are responsible for free()ing the secret,
1092 ** but we can't free() it until after the auth completes
1095 *psecret = (sasl_secret_t *) sm_rpool_malloc(mci->mci_rpool,
1096 sizeof(sasl_secret_t) +
1098 if (*psecret == NULL)
1100 (void) sm_strlcpy((char *) (*psecret)->data, authpass, len + 1);
1101 (*psecret)->len = (unsigned long) len;
1104 # else /* SASL >= 20000 */
1106 ** GETSIMPLE -- callback to get userid or authid
1111 ** result -- (pointer to) result
1112 ** len -- (pointer to) length of result
1115 ** OK/failure values
1119 getsimple(context, id, result, len)
1122 const char **result;
1128 # endif /* SASL > 10509 */
1131 char *authid = NULL;
1133 if (result == NULL || context == NULL)
1134 return SASL_BADPARAM;
1135 sai = (SASL_AI_T *) context;
1138 ** Unfortunately it is not clear whether this routine should
1139 ** return a copy of a string or just a pointer to a string.
1140 ** The Cyrus-SASL plugins treat these return values differently, e.g.,
1141 ** plugins/cram.c free()s authid, plugings/digestmd5.c does not.
1142 ** The best solution to this problem is to fix Cyrus-SASL, but it
1143 ** seems there is nobody who creates patches... Hello CMU!?
1144 ** The second best solution is to have flags that tell this routine
1145 ** whether to return an malloc()ed copy.
1146 ** The next best solution is to always return an malloc()ed copy,
1147 ** and suffer from some memory leak, which is ugly for persistent
1149 ** For now we go with the last solution...
1150 ** We can't use rpools (which would avoid this particular problem)
1151 ** as explained in sasl.c.
1157 l = strlen((*sai)[SASL_USER]) + 1;
1158 s = sm_sasl_malloc(l);
1166 (void) sm_strlcpy(s, (*sai)[SASL_USER], l);
1169 sm_syslog(LOG_DEBUG, NOQID, "AUTH username '%s'",
1172 *len = *result != NULL ? strlen(*result) : 0;
1175 case SASL_CB_AUTHNAME:
1176 h = (*sai)[SASL_AUTHID];
1178 /* XXX maybe other mechanisms too?! */
1179 addrealm = (*sai)[SASL_MECH] != NULL &&
1180 sm_strcasecmp((*sai)[SASL_MECH], "CRAM-MD5") == 0;
1183 ** Add realm to authentication id unless authid contains
1184 ** '@' (i.e., a realm) or the default realm is empty.
1187 if (addrealm && h != NULL && strchr(h, '@') == NULL)
1189 /* has this been done before? */
1190 if ((*sai)[SASL_ID_REALM] == NULL)
1194 realm = (*sai)[SASL_DEFREALM];
1196 /* do not add an empty realm */
1200 (*sai)[SASL_ID_REALM] = NULL;
1204 l = strlen(h) + strlen(realm) + 2;
1206 /* should use rpool, but from where? */
1207 authid = sm_sasl_malloc(l);
1210 (void) sm_snprintf(authid, l,
1213 (*sai)[SASL_ID_REALM] = authid;
1218 (*sai)[SASL_ID_REALM] = NULL;
1223 authid = (*sai)[SASL_ID_REALM];
1226 # endif /* SASL > 10509 */
1228 l = strlen(authid) + 1;
1229 s = sm_sasl_malloc(l);
1237 (void) sm_strlcpy(s, authid, l);
1240 sm_syslog(LOG_DEBUG, NOQID, "AUTH authid '%s'",
1243 *len = authid ? strlen(authid) : 0;
1246 case SASL_CB_LANGUAGE:
1253 return SASL_BADPARAM;
1258 ** GETSECRET -- callback to get password
1261 ** conn -- connection information
1264 ** psecret -- (pointer to) result
1267 ** OK/failure values
1271 getsecret(conn, context, id, psecret)
1273 SM_UNUSED(void *context);
1275 sasl_secret_t **psecret;
1281 if (conn == NULL || psecret == NULL || id != SASL_CB_PASS)
1282 return SASL_BADPARAM;
1284 sai = (SASL_AI_T *) context;
1285 authpass = (*sai)[SASL_PASSWORD];
1286 len = strlen(authpass);
1287 *psecret = (sasl_secret_t *) sm_sasl_malloc(sizeof(sasl_secret_t) +
1289 if (*psecret == NULL)
1291 (void) sm_strlcpy((*psecret)->data, authpass, len + 1);
1292 (*psecret)->len = (unsigned long) len;
1295 # endif /* SASL >= 20000 */
1298 ** SAFESASLFILE -- callback for sasl: is file safe?
1301 ** context -- pointer to context between invocations (unused)
1302 ** file -- name of file to check
1303 ** type -- type of file to check
1306 ** SASL_OK -- file can be used
1307 ** SASL_CONTINUE -- don't use file
1308 ** SASL_FAIL -- failure (not used here)
1314 safesaslfile(context, file, type)
1315 #else /* SASL > 10515 */
1316 safesaslfile(context, file)
1317 #endif /* SASL > 10515 */
1321 # else /* SASL >= 20000 */
1323 # endif /* SASL >= 20000 */
1326 sasl_verify_type_t type;
1327 # else /* SASL >= 20000 */
1329 # endif /* SASL >= 20000 */
1330 #endif /* SASL > 10515 */
1336 #endif /* SASL <= 10515 */
1339 if (file == NULL || *file == '\0')
1341 sff = SFF_SAFEDIRPATH|SFF_NOWLINK|SFF_NOWWFILES|SFF_ROOTOK;
1343 if ((p = strrchr(file, '/')) == NULL)
1348 /* everything beside libs and .conf files must not be readable */
1350 if ((len <= 3 || strncmp(p, "lib", 3) != 0) &&
1351 (len <= 5 || strncmp(p + len - 5, ".conf", 5) != 0))
1353 if (!bitnset(DBS_GROUPREADABLESASLDBFILE, DontBlameSendmail))
1354 sff |= SFF_NORFILES;
1355 if (!bitnset(DBS_GROUPWRITABLESASLDBFILE, DontBlameSendmail))
1356 sff |= SFF_NOGWFILES;
1358 #else /* SASL <= 10515 */
1359 /* files containing passwords should be not readable */
1360 if (type == SASL_VRFY_PASSWD)
1362 if (bitnset(DBS_GROUPREADABLESASLDBFILE, DontBlameSendmail))
1363 sff |= SFF_NOWRFILES;
1365 sff |= SFF_NORFILES;
1366 if (!bitnset(DBS_GROUPWRITABLESASLDBFILE, DontBlameSendmail))
1367 sff |= SFF_NOGWFILES;
1369 #endif /* SASL <= 10515 */
1372 if ((r = safefile(p, RunAsUid, RunAsGid, RunAsUserName, sff,
1373 S_IRUSR, NULL)) == 0)
1375 if (LogLevel > (r != ENOENT ? 8 : 10))
1376 sm_syslog(LOG_WARNING, NOQID, "error: safesasl(%s) failed: %s",
1377 p, sm_errstring(r));
1378 return SASL_CONTINUE;
1382 ** SASLGETREALM -- return the realm for SASL
1384 ** return the realm for the client
1387 ** context -- context shared between invocations
1388 ** availrealms -- list of available realms
1389 ** {realm, realm, ...}
1390 ** result -- pointer to result
1397 saslgetrealm(context, id, availrealms, result)
1400 const char **availrealms;
1401 const char **result;
1406 sai = (SASL_AI_T *) context;
1409 r = (*sai)[SASL_DEFREALM];
1412 sm_syslog(LOG_INFO, NOQID,
1413 "AUTH=client, realm=%s, available realms=%s",
1414 r == NULL ? "<No Realm>" : r,
1415 (availrealms == NULL || *availrealms == NULL)
1416 ? "<No Realms>" : *availrealms);
1418 /* check whether context is in list */
1419 if (availrealms != NULL && *availrealms != NULL)
1421 if (iteminlist(context, (char *)(*availrealms + 1), " ,}") ==
1425 sm_syslog(LOG_ERR, NOQID,
1426 "AUTH=client, realm=%s not in list=%s",
1435 ** ITEMINLIST -- does item appear in list?
1437 ** Check whether item appears in list (which must be separated by a
1438 ** character in delim) as a "word", i.e. it must appear at the begin
1439 ** of the list or after a space, and it must end with a space or the
1443 ** item -- item to search.
1444 ** list -- list of items.
1445 ** delim -- list of delimiters.
1448 ** pointer to occurrence (NULL if not found).
1452 iteminlist(item, list, delim)
1460 if (list == NULL || *list == '\0')
1462 if (item == NULL || *item == '\0')
1466 while (s != NULL && *s != '\0')
1468 if (sm_strncasecmp(s, item, len) == 0 &&
1469 (s[len] == '\0' || strchr(delim, s[len]) != NULL))
1471 s = strpbrk(s, delim);
1479 ** REMOVEMECH -- remove item [rem] from list [list]
1482 ** rem -- item to remove
1483 ** list -- list of items
1484 ** rpool -- resource pool from which result is allocated.
1487 ** pointer to new list (NULL in case of error).
1491 removemech(rem, list, rpool)
1502 if (rem == NULL || *rem == '\0')
1504 /* take out what? */
1508 /* find the item in the list */
1509 if ((needle = iteminlist(rem, list, " ")) == NULL)
1511 /* not in there: return original */
1515 /* length of string without rem */
1516 len = strlen(list) - strlen(rem);
1519 ret = (char *) sm_rpool_malloc_x(rpool, 1);
1523 ret = (char *) sm_rpool_malloc_x(rpool, len);
1524 memset(ret, '\0', len);
1526 /* copy from start to removed item */
1527 memcpy(ret, list, needle - list);
1529 /* length of rest of string past removed item */
1530 len = strlen(needle) - strlen(rem) - 1;
1533 /* not last item -- copy into string */
1534 memcpy(ret + (needle - list),
1535 list + (needle - list) + strlen(rem) + 1,
1539 ret[(needle - list) - 1] = '\0';
1543 ** ATTEMPTAUTH -- try to AUTHenticate using one mechanism
1547 ** mci -- the mailer connection structure.
1548 ** e -- the envelope (including the sender to specify).
1549 ** sai - sasl authinfo
1552 ** EX_OK -- authentication was successful.
1553 ** EX_NOPERM -- authentication failed.
1554 ** EX_IOERR -- authentication dialogue failed (I/O problem?).
1555 ** EX_TEMPFAIL -- temporary failure.
1560 attemptauth(m, mci, e, sai)
1566 int saslresult, smtpresult;
1569 const char *auth_id;
1571 # else /* SASL >= 20000 */
1572 sasl_external_properties_t ssf;
1574 # endif /* SASL >= 20000 */
1575 unsigned int outlen;
1576 sasl_interact_t *client_interact = NULL;
1578 sasl_security_properties_t ssp;
1580 /* MUST NOT be a multiple of 4: bug in some sasl_encode64() versions */
1581 char in64[MAXOUTLEN + 1];
1582 #if NETINET || (NETINET6 && SASL >= 20000)
1583 extern SOCKADDR CurHostAddr;
1584 #endif /* NETINET || (NETINET6 && SASL >= 20000) */
1586 /* no mechanism selected (yet) */
1587 (*sai)[SASL_MECH] = NULL;
1589 /* dispose old connection */
1590 if (mci->mci_conn != NULL)
1591 sasl_dispose(&(mci->mci_conn));
1593 /* make a new client sasl connection */
1596 ** We provide the callbacks again because global callbacks in
1597 ** sasl_client_init() are ignored if SASL has been initialized
1598 ** before, for example, by a library such as libnss-ldap.
1601 saslresult = sasl_client_new(bitnset(M_LMTP, m->m_flags) ? "lmtp"
1603 CurHostName, NULL, NULL, callbacks, 0,
1605 # else /* SASL >= 20000 */
1606 saslresult = sasl_client_new(bitnset(M_LMTP, m->m_flags) ? "lmtp"
1608 CurHostName, NULL, 0, &mci->mci_conn);
1609 # endif /* SASL >= 20000 */
1610 if (saslresult != SASL_OK)
1613 /* set properties */
1614 (void) memset(&ssp, '\0', sizeof(ssp));
1616 /* XXX should these be options settable via .cf ? */
1617 ssp.max_ssf = MaxSLBits;
1618 ssp.maxbufsize = MAXOUTLEN;
1620 ssp.security_flags = SASL_SEC_NOPLAINTEXT;
1622 saslresult = sasl_setprop(mci->mci_conn, SASL_SEC_PROPS, &ssp);
1623 if (saslresult != SASL_OK)
1627 /* external security strength factor, authentication id */
1631 out = macvalue(macid("{cert_subject}"), e);
1632 if (out != NULL && *out != '\0')
1634 out = macvalue(macid("{cipher_bits}"), e);
1635 if (out != NULL && *out != '\0')
1637 # endif /* STARTTLS */
1638 saslresult = sasl_setprop(mci->mci_conn, SASL_SSF_EXTERNAL, &ssf);
1639 if (saslresult != SASL_OK)
1641 saslresult = sasl_setprop(mci->mci_conn, SASL_AUTH_EXTERNAL, auth_id);
1642 if (saslresult != SASL_OK)
1645 # if NETINET || NETINET6
1646 /* set local/remote ipv4 addresses */
1647 if (mci->mci_out != NULL && (
1649 CurHostAddr.sa.sa_family == AF_INET6 ||
1650 # endif /* NETINET6 */
1651 CurHostAddr.sa.sa_family == AF_INET))
1653 SOCKADDR_LEN_T addrsize;
1655 char localip[60], remoteip[60];
1657 switch (CurHostAddr.sa.sa_family)
1660 addrsize = sizeof(struct sockaddr_in);
1664 addrsize = sizeof(struct sockaddr_in6);
1666 # endif /* NETINET6 */
1670 if (iptostring(&CurHostAddr, addrsize,
1671 remoteip, sizeof(remoteip)))
1673 if (sasl_setprop(mci->mci_conn, SASL_IPREMOTEPORT,
1674 remoteip) != SASL_OK)
1677 addrsize = sizeof(saddr_l);
1678 if (getsockname(sm_io_getinfo(mci->mci_out, SM_IO_WHAT_FD,
1680 (struct sockaddr *) &saddr_l, &addrsize) == 0)
1682 if (iptostring(&saddr_l, addrsize,
1683 localip, sizeof(localip)))
1685 if (sasl_setprop(mci->mci_conn,
1687 localip) != SASL_OK)
1692 # endif /* NETINET || NETINET6 */
1694 /* start client side of sasl */
1695 saslresult = sasl_client_start(mci->mci_conn, mci->mci_saslcap,
1698 (const char **) &mechusing);
1699 # else /* SASL >= 20000 */
1700 /* external security strength factor, authentication id */
1704 out = macvalue(macid("{cert_subject}"), e);
1705 if (out != NULL && *out != '\0')
1707 out = macvalue(macid("{cipher_bits}"), e);
1708 if (out != NULL && *out != '\0')
1709 ssf.ssf = atoi(out);
1710 # endif /* STARTTLS */
1711 saslresult = sasl_setprop(mci->mci_conn, SASL_SSF_EXTERNAL, &ssf);
1712 if (saslresult != SASL_OK)
1716 /* set local/remote ipv4 addresses */
1717 if (mci->mci_out != NULL && CurHostAddr.sa.sa_family == AF_INET)
1719 SOCKADDR_LEN_T addrsize;
1720 struct sockaddr_in saddr_l;
1722 if (sasl_setprop(mci->mci_conn, SASL_IP_REMOTE,
1723 (struct sockaddr_in *) &CurHostAddr)
1726 addrsize = sizeof(struct sockaddr_in);
1727 if (getsockname(sm_io_getinfo(mci->mci_out, SM_IO_WHAT_FD,
1729 (struct sockaddr *) &saddr_l, &addrsize) == 0)
1731 if (sasl_setprop(mci->mci_conn, SASL_IP_LOCAL,
1732 &saddr_l) != SASL_OK)
1736 # endif /* NETINET */
1738 /* start client side of sasl */
1739 saslresult = sasl_client_start(mci->mci_conn, mci->mci_saslcap,
1740 NULL, &client_interact,
1742 (const char **) &mechusing);
1743 # endif /* SASL >= 20000 */
1745 if (saslresult != SASL_OK && saslresult != SASL_CONTINUE)
1747 if (saslresult == SASL_NOMECH && LogLevel > 8)
1749 sm_syslog(LOG_NOTICE, e->e_id,
1750 "AUTH=client, available mechanisms do not fulfill requirements");
1755 /* just point current mechanism to the data in the sasl library */
1756 (*sai)[SASL_MECH] = mechusing;
1758 /* send the info across the wire */
1760 /* login and digest-md5 up to 1.5.28 set out="" */
1762 (sm_strcasecmp(mechusing, "LOGIN") == 0 ||
1763 sm_strcasecmp(mechusing, "DIGEST-MD5") == 0))
1766 /* no initial response */
1767 smtpmessage("AUTH %s", m, mci, mechusing);
1769 else if (outlen == 0)
1772 ** zero-length initial response, per RFC 2554 4.:
1773 ** "Unlike a zero-length client answer to a 334 reply, a zero-
1774 ** length initial response is sent as a single equals sign"
1777 smtpmessage("AUTH %s =", m, mci, mechusing);
1781 saslresult = sasl_encode64(out, outlen, in64, sizeof(in64),
1783 if (saslresult != SASL_OK) /* internal error */
1786 sm_syslog(LOG_ERR, e->e_id,
1787 "encode64 for AUTH failed");
1790 smtpmessage("AUTH %s %s", m, mci, mechusing, in64);
1793 sm_sasl_free(out); /* XXX only if no rpool is used */
1794 # endif /* SASL < 20000 */
1797 smtpresult = reply(m, mci, e, TimeOuts.to_auth, getsasldata, NULL,
1802 /* check return code from server */
1803 if (smtpresult == 235)
1805 macdefine(&mci->mci_macro, A_TEMP, macid("{auth_type}"),
1809 if (smtpresult == -1)
1811 if (REPLYTYPE(smtpresult) == 5)
1812 return EX_NOPERM; /* ugly, but ... */
1813 if (REPLYTYPE(smtpresult) != 3)
1815 /* should we fail deliberately, see RFC 2554 4. ? */
1816 /* smtpmessage("*", m, mci); */
1820 saslresult = sasl_client_step(mci->mci_conn,
1821 mci->mci_sasl_string,
1822 mci->mci_sasl_string_len,
1826 if (saslresult != SASL_OK && saslresult != SASL_CONTINUE)
1829 sm_dprintf("AUTH FAIL=%s (%d)\n",
1830 sasl_errstring(saslresult, NULL, NULL),
1833 /* fail deliberately, see RFC 2554 4. */
1834 smtpmessage("*", m, mci);
1837 ** but we should only fail for this authentication
1838 ** mechanism; how to do that?
1841 smtpresult = reply(m, mci, e, TimeOuts.to_auth,
1842 getsasldata, NULL, XS_AUTH);
1848 saslresult = sasl_encode64(out, outlen, in64,
1849 sizeof(in64), NULL);
1850 if (saslresult != SASL_OK)
1852 /* give an error reply to the other side! */
1853 smtpmessage("*", m, mci);
1860 sm_sasl_free(out); /* XXX only if no rpool is used */
1861 # endif /* SASL < 20000 */
1862 smtpmessage("%s", m, mci, in64);
1863 smtpresult = reply(m, mci, e, TimeOuts.to_auth,
1864 getsasldata, NULL, XS_AUTH);
1869 ** SMTPAUTH -- try to AUTHenticate
1871 ** This will try mechanisms in the order the sasl library decided until:
1872 ** - there are no more mechanisms
1873 ** - a mechanism succeeds
1874 ** - the sasl library fails initializing
1878 ** mci -- the mailer connection info.
1879 ** e -- the envelope.
1882 ** EX_OK -- authentication was successful
1883 ** EX_UNAVAILABLE -- authentication not possible, e.g.,
1884 ** no data available.
1885 ** EX_NOPERM -- authentication failed.
1886 ** EX_TEMPFAIL -- temporary failure.
1888 ** Notice: AuthInfo is used for all connections, hence we must
1889 ** return EX_TEMPFAIL only if we really want to retry, i.e.,
1890 ** iff getauth() tempfailed or getauth() was used and
1891 ** authentication tempfailed.
1904 mci->mci_sasl_auth = false;
1905 for (i = 0; i < SASL_MECH ; i++)
1906 mci->mci_sai[i] = NULL;
1908 result = getauth(mci, e, &(mci->mci_sai));
1909 if (result == EX_TEMPFAIL)
1913 /* no data available: don't try to authenticate */
1914 if (result == EX_OK && mci->mci_sai[SASL_AUTHID] == NULL)
1916 if (result != EX_OK)
1918 if (SASLInfo == NULL)
1919 return EX_UNAVAILABLE;
1921 /* read authinfo from file */
1922 result = readauth(SASLInfo, true, &(mci->mci_sai),
1924 if (result != EX_OK)
1926 usedgetauth = false;
1929 /* check whether sufficient data is available */
1930 if (mci->mci_sai[SASL_PASSWORD] == NULL ||
1931 *(mci->mci_sai)[SASL_PASSWORD] == '\0')
1932 return EX_UNAVAILABLE;
1933 if ((mci->mci_sai[SASL_AUTHID] == NULL ||
1934 *(mci->mci_sai)[SASL_AUTHID] == '\0') &&
1935 (mci->mci_sai[SASL_USER] == NULL ||
1936 *(mci->mci_sai)[SASL_USER] == '\0'))
1937 return EX_UNAVAILABLE;
1939 /* set the context for the callback function to sai */
1941 callbacks[CB_PASS_IDX].context = (void *) mci;
1942 # else /* SASL >= 20000 */
1943 callbacks[CB_PASS_IDX].context = (void *) &mci->mci_sai;
1944 # endif /* SASL >= 20000 */
1945 callbacks[CB_USER_IDX].context = (void *) &mci->mci_sai;
1946 callbacks[CB_AUTHNAME_IDX].context = (void *) &mci->mci_sai;
1947 callbacks[CB_GETREALM_IDX].context = (void *) &mci->mci_sai;
1949 callbacks[CB_SAFESASL_IDX].context = (void *) &mci->mci_sai;
1952 /* set default value for realm */
1953 if ((mci->mci_sai)[SASL_DEFREALM] == NULL)
1954 (mci->mci_sai)[SASL_DEFREALM] = sm_rpool_strdup_x(e->e_rpool,
1955 macvalue('j', CurEnv));
1957 /* set default value for list of mechanism to use */
1958 if ((mci->mci_sai)[SASL_MECHLIST] == NULL ||
1959 *(mci->mci_sai)[SASL_MECHLIST] == '\0')
1960 (mci->mci_sai)[SASL_MECHLIST] = AuthMechanisms;
1962 /* create list of mechanisms to try */
1963 mci->mci_saslcap = intersect((mci->mci_sai)[SASL_MECHLIST],
1964 mci->mci_saslcap, mci->mci_rpool);
1966 /* initialize sasl client library */
1967 result = init_sasl_client();
1968 if (result != SASL_OK)
1969 return usedgetauth ? EX_TEMPFAIL : EX_UNAVAILABLE;
1972 result = attemptauth(m, mci, e, &(mci->mci_sai));
1973 if (result == EX_OK)
1974 mci->mci_sasl_auth = true;
1975 else if (result == EX_TEMPFAIL || result == EX_NOPERM)
1977 mci->mci_saslcap = removemech((mci->mci_sai)[SASL_MECH],
1980 if (mci->mci_saslcap == NULL ||
1981 *(mci->mci_saslcap) == '\0')
1982 return usedgetauth ? result
1987 } while (result != EX_OK);
1993 ** SMTPMAILFROM -- send MAIL command
1997 ** mci -- the mailer connection structure.
1998 ** e -- the envelope (including the sender to specify).
2002 smtpmailfrom(m, mci, e)
2011 char buf[MAXNAME + 1];
2012 char optbuf[MAXLINE];
2015 sm_dprintf("smtpmailfrom: CurHost=%s\n", CurHostName);
2019 ** Check if connection is gone, if so
2020 ** it's a tempfail and we use mci_errno
2024 if (mci->mci_state == MCIS_CLOSED)
2026 errno = mci->mci_errno;
2030 /* set up appropriate options to include */
2031 if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0)
2033 (void) sm_snprintf(optbuf, sizeof(optbuf), " SIZE=%ld",
2035 bufp = &optbuf[strlen(optbuf)];
2043 bodytype = e->e_bodytype;
2044 if (bitset(MCIF_8BITMIME, mci->mci_flags))
2046 if (bodytype == NULL &&
2047 bitset(MM_MIME8BIT, MimeMode) &&
2048 bitset(EF_HAS8BIT, e->e_flags) &&
2049 !bitset(EF_DONT_MIME, e->e_flags) &&
2050 !bitnset(M_8BITS, m->m_flags))
2051 bodytype = "8BITMIME";
2052 if (bodytype != NULL &&
2053 SPACELEFT(optbuf, bufp) > strlen(bodytype) + 7)
2055 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2056 " BODY=%s", bodytype);
2057 bufp += strlen(bufp);
2060 else if (bitnset(M_8BITS, m->m_flags) ||
2061 !bitset(EF_HAS8BIT, e->e_flags) ||
2062 bitset(MCIF_8BITOK, mci->mci_flags))
2065 /* just pass it through */
2068 else if (bitset(MM_CVTMIME, MimeMode) &&
2069 !bitset(EF_DONT_MIME, e->e_flags) &&
2070 (!bitset(MM_PASS8BIT, MimeMode) ||
2071 bitset(EF_IS_MIME, e->e_flags)))
2073 /* must convert from 8bit MIME format to 7bit encoded */
2074 mci->mci_flags |= MCIF_CVT8TO7;
2076 #endif /* MIME8TO7 */
2077 else if (!bitset(MM_PASS8BIT, MimeMode))
2079 /* cannot just send a 8-bit version */
2080 extern char MsgBuf[];
2082 usrerrenh("5.6.3", "%s does not support 8BITMIME", CurHostName);
2083 mci_setstat(mci, EX_NOTSTICKY, "5.6.3", MsgBuf);
2087 if (bitset(MCIF_DSN, mci->mci_flags))
2089 if (e->e_envid != NULL &&
2090 SPACELEFT(optbuf, bufp) > strlen(e->e_envid) + 7)
2092 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2093 " ENVID=%s", e->e_envid);
2094 bufp += strlen(bufp);
2097 /* RET= parameter */
2098 if (bitset(EF_RET_PARAM, e->e_flags) &&
2099 SPACELEFT(optbuf, bufp) > 9)
2101 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2103 bitset(EF_NO_BODY_RETN, e->e_flags) ?
2105 bufp += strlen(bufp);
2109 if (bitset(MCIF_AUTH, mci->mci_flags) && e->e_auth_param != NULL &&
2110 SPACELEFT(optbuf, bufp) > strlen(e->e_auth_param) + 7
2112 && (!bitset(SASL_AUTH_AUTH, SASLOpts) || mci->mci_sasl_auth)
2116 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2117 " AUTH=%s", e->e_auth_param);
2118 bufp += strlen(bufp);
2122 ** 17 is the max length required, we could use log() to compute
2123 ** the exact length (and check IS_DLVR_TRACE())
2126 if (bitset(MCIF_DLVR_BY, mci->mci_flags) &&
2127 IS_DLVR_BY(e) && SPACELEFT(optbuf, bufp) > 17)
2132 ** Avoid problems with delays (for R) since the check
2133 ** in deliver() whether min-deliver-time is sufficient.
2134 ** Alternatively we could pass the computed time to this
2138 dby = e->e_deliver_by - (curtime() - e->e_ctime);
2139 if (dby <= 0 && IS_DLVR_RETURN(e))
2140 dby = mci->mci_min_by <= 0 ? 1 : mci->mci_min_by;
2141 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2144 IS_DLVR_RETURN(e) ? 'R' : 'N',
2145 IS_DLVR_TRACE(e) ? "T" : "");
2146 bufp += strlen(bufp);
2150 ** Send the MAIL command.
2151 ** Designates the sender.
2154 mci->mci_state = MCIS_MAIL;
2156 if (bitset(EF_RESPONSE, e->e_flags) &&
2157 !bitnset(M_NO_NULL_FROM, m->m_flags))
2160 expand("\201g", buf, sizeof(buf), e);
2163 /* strip off <angle brackets> (put back on below) */
2164 bufp = &buf[strlen(buf) - 1];
2171 if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) ||
2172 !bitnset(M_FROMPATH, m->m_flags))
2174 smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf);
2178 smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
2179 *bufp == '@' ? ',' : ':', bufp, optbuf);
2181 SmtpPhase = mci->mci_phase = "client MAIL";
2182 sm_setproctitle(true, e, "%s %s: %s", qid_printname(e),
2183 CurHostName, mci->mci_phase);
2184 r = reply(m, mci, e, TimeOuts.to_mail, NULL, &enhsc, XS_MAIL);
2187 /* communications failure */
2188 mci_setstat(mci, EX_TEMPFAIL, "4.4.2", NULL);
2191 else if (r == SMTPCLOSING)
2193 /* service shutting down: handled by reply() */
2196 else if (REPLYTYPE(r) == 4)
2198 mci_setstat(mci, EX_NOTSTICKY, ENHSCN(enhsc, smtptodsn(r)),
2202 else if (REPLYTYPE(r) == 2)
2208 /* syntax error in arguments */
2209 mci_setstat(mci, EX_NOTSTICKY, ENHSCN(enhsc, "5.5.2"),
2215 /* mailbox name not allowed */
2216 mci_setstat(mci, EX_NOTSTICKY, ENHSCN(enhsc, "5.1.3"),
2222 /* exceeded storage allocation */
2223 mci_setstat(mci, EX_NOTSTICKY, ENHSCN(enhsc, "5.3.4"),
2225 if (bitset(MCIF_SIZE, mci->mci_flags))
2226 e->e_flags |= EF_NO_BODY_RETN;
2227 return EX_UNAVAILABLE;
2229 else if (REPLYTYPE(r) == 5)
2232 mci_setstat(mci, EX_NOTSTICKY, ENHSCN(enhsc, "5.0.0"),
2234 return EX_UNAVAILABLE;
2239 sm_syslog(LOG_CRIT, e->e_id,
2240 "%.100s: SMTP MAIL protocol error: %s",
2242 shortenstring(SmtpReplyBuffer, 403));
2245 /* protocol error -- close up */
2246 mci_setstat(mci, EX_PROTOCOL, ENHSCN(enhsc, "5.5.1"),
2248 smtpquit(m, mci, e);
2252 ** SMTPRCPT -- designate recipient.
2255 ** to -- address of recipient.
2256 ** m -- the mailer we are sending to.
2257 ** mci -- the connection info for this transaction.
2258 ** e -- the envelope for this transaction.
2261 ** exit status corresponding to recipient status.
2264 ** Sends the mail via SMTP.
2268 smtprcpt(to, m, mci, e, ctladdr, xstart)
2277 char optbuf[MAXLINE];
2281 ** If there is status waiting from the other end, read it.
2282 ** This should normally happen because of SMTP pipelining.
2285 while (mci->mci_nextaddr != NULL &&
2286 sm_io_getinfo(mci->mci_in, SM_IO_IS_READABLE, NULL) > 0)
2290 r = smtprcptstat(mci->mci_nextaddr, m, mci, e);
2293 markfailure(e, mci->mci_nextaddr, mci, r, false);
2294 giveresponse(r, mci->mci_nextaddr->q_status, m, mci,
2295 ctladdr, xstart, e, to);
2297 mci->mci_nextaddr = mci->mci_nextaddr->q_pchain;
2299 #endif /* PIPELINING */
2302 ** Check if connection is gone, if so
2303 ** it's a tempfail and we use mci_errno
2307 if (mci->mci_state == MCIS_CLOSED)
2309 errno = mci->mci_errno;
2317 ** Warning: in the following it is assumed that the free space
2318 ** in bufp is sizeof(optbuf)
2321 if (bitset(MCIF_DSN, mci->mci_flags))
2323 if (IS_DLVR_NOTIFY(e) &&
2324 !bitset(MCIF_DLVR_BY, mci->mci_flags))
2326 /* RFC 2852: 4.1.4.2 */
2327 if (!bitset(QHASNOTIFY, to->q_flags))
2328 to->q_flags |= QPINGONFAILURE|QPINGONDELAY|QHASNOTIFY;
2329 else if (bitset(QPINGONSUCCESS, to->q_flags) ||
2330 bitset(QPINGONFAILURE, to->q_flags) ||
2331 bitset(QPINGONDELAY, to->q_flags))
2332 to->q_flags |= QPINGONDELAY;
2335 /* NOTIFY= parameter */
2336 if (bitset(QHASNOTIFY, to->q_flags) &&
2337 bitset(QPRIMARY, to->q_flags) &&
2338 !bitnset(M_LOCALMAILER, m->m_flags))
2340 bool firstone = true;
2342 (void) sm_strlcat(bufp, " NOTIFY=", sizeof(optbuf));
2343 if (bitset(QPINGONSUCCESS, to->q_flags))
2345 (void) sm_strlcat(bufp, "SUCCESS", sizeof(optbuf));
2348 if (bitset(QPINGONFAILURE, to->q_flags))
2351 (void) sm_strlcat(bufp, ",",
2353 (void) sm_strlcat(bufp, "FAILURE", sizeof(optbuf));
2356 if (bitset(QPINGONDELAY, to->q_flags))
2359 (void) sm_strlcat(bufp, ",",
2361 (void) sm_strlcat(bufp, "DELAY", sizeof(optbuf));
2365 (void) sm_strlcat(bufp, "NEVER", sizeof(optbuf));
2366 bufp += strlen(bufp);
2369 /* ORCPT= parameter */
2370 if (to->q_orcpt != NULL &&
2371 SPACELEFT(optbuf, bufp) > strlen(to->q_orcpt) + 7)
2373 (void) sm_snprintf(bufp, SPACELEFT(optbuf, bufp),
2374 " ORCPT=%s", to->q_orcpt);
2375 bufp += strlen(bufp);
2379 smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
2380 mci->mci_state = MCIS_RCPT;
2382 SmtpPhase = mci->mci_phase = "client RCPT";
2383 sm_setproctitle(true, e, "%s %s: %s", qid_printname(e),
2384 CurHostName, mci->mci_phase);
2388 ** If running SMTP pipelining, we will pick up status later
2391 if (bitset(MCIF_PIPELINED, mci->mci_flags))
2393 #endif /* PIPELINING */
2395 return smtprcptstat(to, m, mci, e);
2398 ** SMTPRCPTSTAT -- get recipient status
2400 ** This is only called during SMTP pipelining
2403 ** to -- address of recipient.
2404 ** m -- mailer being sent to.
2405 ** mci -- the mailer connection information.
2406 ** e -- the envelope for this message.
2409 ** EX_* -- protocol status
2413 smtprcptstat(to, m, mci, e)
2417 register ENVELOPE *e;
2424 ** Check if connection is gone, if so
2425 ** it's a tempfail and we use mci_errno
2429 if (mci->mci_state == MCIS_CLOSED)
2431 errno = mci->mci_errno;
2436 r = reply(m, mci, e, TimeOuts.to_rcpt, NULL, &enhsc, XS_RCPT);
2438 to->q_rstatus = sm_rpool_strdup_x(e->e_rpool, SmtpReplyBuffer);
2439 to->q_status = ENHSCN_RPOOL(enhsc, smtptodsn(r), e->e_rpool);
2440 if (!bitnset(M_LMTP, m->m_flags))
2441 to->q_statmta = mci->mci_host;
2442 if (r < 0 || REPLYTYPE(r) == 4)
2444 mci->mci_retryrcpt = true;
2448 else if (REPLYTYPE(r) == 2)
2452 if ((t = mci->mci_tolist) != NULL)
2457 for (p = to->q_paddr; *p != '\0'; *t++ = *p++)
2460 mci->mci_tolist = t;
2464 #endif /* PIPELINING */
2469 to->q_status = ENHSCN_RPOOL(enhsc, "5.1.1", e->e_rpool);
2474 to->q_status = ENHSCN_RPOOL(enhsc, "5.1.6", e->e_rpool);
2479 to->q_status = ENHSCN_RPOOL(enhsc, "5.1.3", e->e_rpool);
2482 else if (REPLYTYPE(r) == 5)
2484 return EX_UNAVAILABLE;
2489 sm_syslog(LOG_CRIT, e->e_id,
2490 "%.100s: SMTP RCPT protocol error: %s",
2492 shortenstring(SmtpReplyBuffer, 403));
2495 mci_setstat(mci, EX_PROTOCOL, ENHSCN(enhsc, "5.5.1"),
2500 ** SMTPDATA -- send the data and clean up the transaction.
2503 ** m -- mailer being sent to.
2504 ** mci -- the mailer connection information.
2505 ** e -- the envelope for this message.
2508 ** exit status corresponding to DATA command.
2512 smtpdata(m, mci, e, ctladdr, xstart)
2515 register ENVELOPE *e;
2526 ** Check if connection is gone, if so
2527 ** it's a tempfail and we use mci_errno
2531 if (mci->mci_state == MCIS_CLOSED)
2533 errno = mci->mci_errno;
2541 ** First send the command and check that it is ok.
2542 ** Then send the data (if there are valid recipients).
2543 ** Follow it up with a dot to terminate.
2544 ** Finally get the results of the transaction.
2547 /* send the command and check ok to proceed */
2548 smtpmessage("DATA", m, mci);
2551 if (mci->mci_nextaddr != NULL)
2553 char *oldto = e->e_to;
2555 /* pick up any pending RCPT responses for SMTP pipelining */
2556 while (mci->mci_nextaddr != NULL)
2560 e->e_to = mci->mci_nextaddr->q_paddr;
2561 r = smtprcptstat(mci->mci_nextaddr, m, mci, e);
2564 markfailure(e, mci->mci_nextaddr, mci, r,
2566 giveresponse(r, mci->mci_nextaddr->q_status, m,
2567 mci, ctladdr, xstart, e,
2569 if (r == EX_TEMPFAIL)
2570 mci->mci_nextaddr->q_state = QS_RETRY;
2572 mci->mci_nextaddr = mci->mci_nextaddr->q_pchain;
2577 ** Connection might be closed in response to a RCPT command,
2578 ** i.e., the server responded with 421. In that case (at
2579 ** least) one RCPT has a temporary failure, hence we don't
2580 ** need to check mci_okrcpts (as it is done below) to figure
2581 ** out which error to return.
2584 if (mci->mci_state == MCIS_CLOSED)
2586 errno = mci->mci_errno;
2590 #endif /* PIPELINING */
2592 /* now proceed with DATA phase */
2593 SmtpPhase = mci->mci_phase = "client DATA 354";
2594 mci->mci_state = MCIS_DATA;
2595 sm_setproctitle(true, e, "%s %s: %s",
2596 qid_printname(e), CurHostName, mci->mci_phase);
2597 r = reply(m, mci, e, TimeOuts.to_datainit, NULL, &enhsc, XS_DATA);
2598 if (r < 0 || REPLYTYPE(r) == 4)
2601 smtpquit(m, mci, e);
2602 errno = mci->mci_errno;
2605 else if (REPLYTYPE(r) == 5)
2607 smtprset(m, mci, e);
2609 if (mci->mci_okrcpts <= 0)
2610 return mci->mci_retryrcpt ? EX_TEMPFAIL
2612 #endif /* PIPELINING */
2613 return EX_UNAVAILABLE;
2615 else if (REPLYTYPE(r) != 3)
2619 sm_syslog(LOG_CRIT, e->e_id,
2620 "%.100s: SMTP DATA-1 protocol error: %s",
2622 shortenstring(SmtpReplyBuffer, 403));
2624 smtprset(m, mci, e);
2625 mci_setstat(mci, EX_PROTOCOL, ENHSCN(enhsc, "5.5.1"),
2628 if (mci->mci_okrcpts <= 0)
2629 return mci->mci_retryrcpt ? EX_TEMPFAIL
2631 #endif /* PIPELINING */
2636 if (mci->mci_okrcpts > 0)
2638 #endif /* PIPELINING */
2641 ** Set timeout around data writes. Make it at least large
2642 ** enough for DNS timeouts on all recipients plus some fudge
2643 ** factor. The main thing is that it should not be infinite.
2648 /* simulate a DATA timeout */
2652 timeout = DATA_PROGRESS_TIMEOUT * 1000;
2653 sm_io_setinfo(mci->mci_out, SM_IO_WHAT_TIMEOUT, &timeout);
2657 ** Output the actual message.
2660 if (!(*e->e_puthdr)(mci, e->e_header, e, M87F_OUTER))
2665 /* simulate a DATA timeout */
2669 if (!(*e->e_putbody)(mci, e, NULL))
2673 ** Cleanup after sending message.
2679 #endif /* PIPELINING */
2681 #if _FFR_CATCH_BROKEN_MTAS
2682 if (sm_io_getinfo(mci->mci_in, SM_IO_IS_READABLE, NULL) > 0)
2684 /* terminate the message */
2685 (void) sm_io_fprintf(mci->mci_out, SM_TIME_DEFAULT, ".%s",
2687 if (TrafficLogFile != NULL)
2688 (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
2689 "%05d >>> .\n", (int) CurrentPid);
2693 sm_syslog(LOG_CRIT, e->e_id,
2694 "%.100s: SMTP DATA-1 protocol error: remote server returned response before final dot",
2696 mci->mci_errno = EIO;
2697 mci->mci_state = MCIS_ERROR;
2698 mci_setstat(mci, EX_PROTOCOL, "5.5.0", NULL);
2699 smtpquit(m, mci, e);
2702 #endif /* _FFR_CATCH_BROKEN_MTAS */
2704 if (sm_io_error(mci->mci_out))
2706 /* error during processing -- don't send the dot */
2707 mci->mci_errno = EIO;
2708 mci->mci_state = MCIS_ERROR;
2709 mci_setstat(mci, EX_IOERR, "4.4.2", NULL);
2710 smtpquit(m, mci, e);
2714 /* terminate the message */
2715 if (sm_io_fprintf(mci->mci_out, SM_TIME_DEFAULT, "%s.%s",
2716 bitset(MCIF_INLONGLINE, mci->mci_flags) ? m->m_eol : "",
2717 m->m_eol) == SM_IO_EOF)
2719 if (TrafficLogFile != NULL)
2720 (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
2721 "%05d >>> .\n", (int) CurrentPid);
2725 /* check for the results of the transaction */
2726 SmtpPhase = mci->mci_phase = "client DATA status";
2727 sm_setproctitle(true, e, "%s %s: %s", qid_printname(e),
2728 CurHostName, mci->mci_phase);
2729 if (bitnset(M_LMTP, m->m_flags))
2731 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL, &enhsc, XS_EOM);
2734 if (mci->mci_state == MCIS_DATA)
2735 mci->mci_state = MCIS_OPEN;
2736 xstat = EX_NOTSTICKY;
2738 rstat = EX_TEMPFAIL;
2739 else if (REPLYTYPE(r) == 4)
2740 rstat = xstat = EX_TEMPFAIL;
2741 else if (REPLYTYPE(r) == 2)
2742 rstat = xstat = EX_OK;
2743 else if (REPLYCLASS(r) != 5)
2744 rstat = xstat = EX_PROTOCOL;
2745 else if (REPLYTYPE(r) == 5)
2746 rstat = EX_UNAVAILABLE;
2748 rstat = EX_PROTOCOL;
2749 mci_setstat(mci, xstat, ENHSCN(enhsc, smtptodsn(r)),
2751 if (bitset(MCIF_ENHSTAT, mci->mci_flags) &&
2752 (r = isenhsc(SmtpReplyBuffer + 4, ' ')) > 0)
2756 e->e_statmsg = sm_rpool_strdup_x(e->e_rpool, &SmtpReplyBuffer[r]);
2757 SmtpPhase = mci->mci_phase = "idle";
2758 sm_setproctitle(true, e, "%s: %s", CurHostName, mci->mci_phase);
2759 if (rstat != EX_PROTOCOL)
2763 sm_syslog(LOG_CRIT, e->e_id,
2764 "%.100s: SMTP DATA-2 protocol error: %s",
2766 shortenstring(SmtpReplyBuffer, 403));
2771 mci->mci_errno = errno;
2772 mci->mci_state = MCIS_ERROR;
2773 mci_setstat(mci, bitset(MCIF_NOTSTICKY, mci->mci_flags)
2774 ? EX_NOTSTICKY: EX_TEMPFAIL,
2776 mci->mci_flags &= ~MCIF_NOTSTICKY;
2779 ** If putbody() couldn't finish due to a timeout,
2780 ** rewind it here in the timeout handler. See
2781 ** comments at the end of putbody() for reasoning.
2784 if (e->e_dfp != NULL)
2785 (void) bfrewind(e->e_dfp);
2787 errno = mci->mci_errno;
2788 syserr("+451 4.4.1 timeout writing message to %s", CurHostName);
2789 smtpquit(m, mci, e);
2794 ** SMTPGETSTAT -- get status code from DATA in LMTP
2797 ** m -- the mailer to which we are sending the message.
2798 ** mci -- the mailer connection structure.
2799 ** e -- the current envelope.
2802 ** The exit status corresponding to the reply code.
2806 smtpgetstat(m, mci, e)
2818 /* check for the results of the transaction */
2819 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL, &enhsc, XS_DATA2);
2822 xstat = EX_NOTSTICKY;
2823 if (REPLYTYPE(r) == 4)
2824 status = EX_TEMPFAIL;
2825 else if (REPLYTYPE(r) == 2)
2826 status = xstat = EX_OK;
2827 else if (REPLYCLASS(r) != 5)
2828 status = xstat = EX_PROTOCOL;
2829 else if (REPLYTYPE(r) == 5)
2830 status = EX_UNAVAILABLE;
2832 status = EX_PROTOCOL;
2833 if (bitset(MCIF_ENHSTAT, mci->mci_flags) &&
2834 (off = isenhsc(SmtpReplyBuffer + 4, ' ')) > 0)
2838 e->e_statmsg = sm_rpool_strdup_x(e->e_rpool, &SmtpReplyBuffer[off]);
2839 mci_setstat(mci, xstat, ENHSCN(enhsc, smtptodsn(r)), SmtpReplyBuffer);
2840 if (LogLevel > 1 && status == EX_PROTOCOL)
2842 sm_syslog(LOG_CRIT, e->e_id,
2843 "%.100s: SMTP DATA-3 protocol error: %s",
2845 shortenstring(SmtpReplyBuffer, 403));
2850 ** SMTPQUIT -- close the SMTP connection.
2853 ** m -- a pointer to the mailer.
2854 ** mci -- the mailer connection information.
2855 ** e -- the current envelope.
2861 ** sends the final protocol and closes the connection.
2870 bool oldSuprErrs = SuprErrs;
2874 if (mci->mci_state == MCIS_CLOSED)
2876 mci_close(mci, "smtpquit:1");
2880 oldcurhost = CurHostName;
2881 CurHostName = mci->mci_host; /* XXX UGLY XXX */
2882 if (CurHostName == NULL)
2883 CurHostName = MyHostName;
2886 mci->mci_okrcpts = 0;
2887 #endif /* PIPELINING */
2890 ** Suppress errors here -- we may be processing a different
2891 ** job when we do the quit connection, and we don't want the
2892 ** new job to be penalized for something that isn't it's
2898 /* send the quit message if we haven't gotten I/O error */
2899 if (mci->mci_state != MCIS_ERROR &&
2900 mci->mci_state != MCIS_QUITING)
2902 SmtpPhase = "client QUIT";
2903 mci->mci_state = MCIS_QUITING;
2904 smtpmessage("QUIT", m, mci);
2905 (void) reply(m, mci, e, TimeOuts.to_quit, NULL, NULL, XS_QUIT);
2906 SuprErrs = oldSuprErrs;
2907 if (mci->mci_state == MCIS_CLOSED)
2911 /* now actually close the connection and pick up the zombie */
2912 rcode = endmailer(mci, e, NULL);
2915 char *mailer = NULL;
2917 if (mci->mci_mailer != NULL &&
2918 mci->mci_mailer->m_name != NULL)
2919 mailer = mci->mci_mailer->m_name;
2921 /* look for naughty mailers */
2922 sm_syslog(LOG_ERR, e->e_id,
2923 "smtpquit: mailer%s%s exited with exit value %d",
2924 mailer == NULL ? "" : " ",
2925 mailer == NULL ? "" : mailer,
2929 SuprErrs = oldSuprErrs;
2932 CurHostName = oldcurhost;
2936 ** SMTPRSET -- send a RSET (reset) command
2939 ** m -- a pointer to the mailer.
2940 ** mci -- the mailer connection information.
2941 ** e -- the current envelope.
2947 ** closes the connection if there is no reply to RSET.
2958 CurHostName = mci->mci_host; /* XXX UGLY XXX */
2959 if (CurHostName == NULL)
2960 CurHostName = MyHostName;
2963 mci->mci_okrcpts = 0;
2964 #endif /* PIPELINING */
2967 ** Check if connection is gone, if so
2968 ** it's a tempfail and we use mci_errno
2972 if (mci->mci_state == MCIS_CLOSED)
2974 errno = mci->mci_errno;
2978 SmtpPhase = "client RSET";
2979 smtpmessage("RSET", m, mci);
2980 r = reply(m, mci, e, TimeOuts.to_rset, NULL, NULL, XS_DEFAULT);
2985 ** Any response is deemed to be acceptable.
2986 ** The standard does not state the proper action
2987 ** to take when a value other than 250 is received.
2989 ** However, if 421 is returned for the RSET, leave
2990 ** mci_state alone (MCIS_SSD can be set in reply()
2991 ** and MCIS_CLOSED can be set in smtpquit() if
2992 ** reply() gets a 421 and calls smtpquit()).
2995 if (mci->mci_state != MCIS_SSD && mci->mci_state != MCIS_CLOSED)
2996 mci->mci_state = MCIS_OPEN;
2997 else if (mci->mci_exitstat == EX_OK)
2998 mci_setstat(mci, EX_TEMPFAIL, "4.5.0", NULL);
3001 ** SMTPPROBE -- check the connection state
3004 ** mci -- the mailer connection information.
3010 ** closes the connection if there is no reply to RSET.
3018 MAILER *m = mci->mci_mailer;
3020 extern ENVELOPE BlankEnvelope;
3022 CurHostName = mci->mci_host; /* XXX UGLY XXX */
3023 if (CurHostName == NULL)
3024 CurHostName = MyHostName;
3027 SmtpPhase = "client probe";
3028 smtpmessage("RSET", m, mci);
3029 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL, NULL, XS_DEFAULT);
3030 if (REPLYTYPE(r) != 2)
3031 smtpquit(m, mci, e);
3035 ** REPLY -- read arpanet reply
3038 ** m -- the mailer we are reading the reply from.
3039 ** mci -- the mailer connection info structure.
3040 ** e -- the current envelope.
3041 ** timeout -- the timeout for reads.
3042 ** pfunc -- processing function called on each line of response.
3043 ** If null, no special processing is done.
3044 ** enhstat -- optional, returns enhanced error code string (if set)
3045 ** rtype -- type of SmtpMsgBuffer: does it contains secret data?
3048 ** reply code it reads.
3051 ** flushes the mail file.
3055 reply(m, mci, e, timeout, pfunc, enhstat, rtype)
3060 void (*pfunc) __P((char *, bool, MAILER *, MCI *, ENVELOPE *));
3064 register char *bufp;
3066 bool firstline = true;
3067 char junkbuf[MAXLINE];
3068 static char enhstatcode[ENHSCLEN];
3072 ** Flush the output before reading response.
3074 ** For SMTP pipelining, it would be better if we didn't do
3075 ** this if there was already data waiting to be read. But
3076 ** to do it properly means pushing it to the I/O library,
3077 ** since it really needs to be done below the buffer layer.
3080 if (mci->mci_out != NULL)
3081 (void) sm_io_flush(mci->mci_out, SM_TIME_DEFAULT);
3084 sm_dprintf("reply\n");
3087 ** Read the input line, being careful not to hang.
3090 bufp = SmtpReplyBuffer;
3091 (void) set_tls_rd_tmo(timeout);
3096 /* actually do the read */
3097 if (e->e_xfp != NULL) /* for debugging */
3098 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
3100 /* if we are in the process of closing just give the code */
3101 if (mci->mci_state == MCIS_CLOSED)
3104 /* don't try to read from a non-existent fd */
3105 if (mci->mci_in == NULL)
3107 if (mci->mci_errno == 0)
3108 mci->mci_errno = EBADF;
3110 /* errors on QUIT should be ignored */
3111 if (strncmp(SmtpMsgBuffer, "QUIT", 4) == 0)
3113 errno = mci->mci_errno;
3114 mci_close(mci, "reply:1");
3117 mci->mci_state = MCIS_ERROR;
3118 smtpquit(m, mci, e);
3119 errno = mci->mci_errno;
3123 if (mci->mci_out != NULL)
3124 (void) sm_io_flush(mci->mci_out, SM_TIME_DEFAULT);
3126 /* get the line from the other side */
3127 p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
3129 mci->mci_lastuse = curtime();
3134 extern char MsgBuf[];
3136 /* errors on QUIT should be ignored */
3137 if (strncmp(SmtpMsgBuffer, "QUIT", 4) == 0)
3139 mci_close(mci, "reply:2");
3143 /* if the remote end closed early, fake an error */
3147 (void) sm_snprintf(SmtpReplyBuffer,
3148 sizeof(SmtpReplyBuffer),
3149 "421 4.4.1 Connection reset by %s",
3153 #else /* ECONNRESET */
3155 #endif /* ECONNRESET */
3158 mci->mci_errno = errno;
3159 oldholderrs = HoldErrs;
3161 usrerr("451 4.4.1 reply: read error from %s",
3163 mci_setstat(mci, EX_TEMPFAIL, "4.4.2", MsgBuf);
3165 /* if debugging, pause so we can see state */
3168 mci->mci_state = MCIS_ERROR;
3169 smtpquit(m, mci, e);
3175 if (e->e_to != NULL)
3177 (void) sm_snprintf(p,
3180 shortenstring(e->e_to, MAXSHORTSTR));
3183 (void) sm_snprintf(p, SPACELEFT(wbuf, p),
3184 "reply(%.100s) during %s",
3185 CURHOSTNAME, SmtpPhase);
3189 HoldErrs = oldholderrs;
3193 fixcrlf(bufp, true);
3195 /* EHLO failure is not a real error */
3196 if (e->e_xfp != NULL && (bufp[0] == '4' ||
3197 (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
3199 /* serious error -- log the previous command */
3202 /* inform user who we are chatting with */
3203 (void) sm_io_fprintf(CurEnv->e_xfp,
3205 "... while talking to %s:\n",
3207 SmtpNeedIntro = false;
3209 if (SmtpMsgBuffer[0] != '\0')
3211 (void) sm_io_fprintf(e->e_xfp,
3214 (rtype == XS_STARTTLS)
3215 ? "STARTTLS dialogue"
3216 : ((rtype == XS_AUTH)
3219 SmtpMsgBuffer[0] = '\0';
3222 /* now log the message as from the other side */
3223 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
3227 /* display the input for verbose mode */
3229 nmessage("050 %s", bufp);
3231 /* ignore improperly formatted input */
3232 if (!ISSMTPREPLY(bufp))
3235 if (bitset(MCIF_ENHSTAT, mci->mci_flags) &&
3237 extenhsc(bufp + 4, ' ', enhstatcode) > 0)
3238 *enhstat = enhstatcode;
3240 /* process the line */
3242 (*pfunc)(bufp, firstline, m, mci, e);
3244 /* decode the reply code */
3247 /* extra semantics: 0xx codes are "informational" */
3255 if ((e->e_rcode == 0 || REPLYTYPE(e->e_rcode) < 5)
3256 && REPLYTYPE(r) > 3 && firstline)
3259 if (REPLYTYPE(r) > 3 && firstline)
3265 ** ignore error iff: DATA, 5xy error, but we had
3266 ** "retryable" recipients. XREF: smtpdata()
3269 if (!(rtype == XS_DATA && REPLYTYPE(r) == 5 &&
3270 mci->mci_okrcpts <= 0 && mci->mci_retryrcpt))
3271 # endif /* PIPELINING */
3273 o = extenhsc(bufp + 4, ' ', enhstatcode);
3276 sm_strlcpy(e->e_renhsc, enhstatcode,
3277 sizeof(e->e_renhsc));
3279 /* skip SMTP reply code, delimiters */
3285 e->e_text = sm_rpool_strdup_x(e->e_rpool,
3290 sm_dprintf("user: offset=%d, bufp=%s, rcode=%d, enhstat=%s, text=%s\n",
3291 o, bufp, r, e->e_renhsc, e->e_text);
3294 #endif /* _FFR_ERRCODE */
3298 /* if no continuation lines, return this line */
3302 /* first line of real reply -- ignore rest */
3307 ** Now look at SmtpReplyBuffer -- only care about the first
3308 ** line of the response from here on out.
3311 /* save temporary failure messages for posterity */
3312 if (SmtpReplyBuffer[0] == '4')
3313 (void) sm_strlcpy(SmtpError, SmtpReplyBuffer, sizeof(SmtpError));
3315 /* reply code 421 is "Service Shutting Down" */
3316 if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD &&
3317 mci->mci_state != MCIS_QUITING)
3319 /* send the quit protocol */
3320 mci->mci_state = MCIS_SSD;
3321 smtpquit(m, mci, e);
3327 ** SMTPMESSAGE -- send message to server
3331 ** m -- the mailer to control formatting.
3332 ** a, b, c -- parameters
3338 ** writes message to mci->mci_out.
3344 smtpmessage(char *f, MAILER *m, MCI *mci, ...)
3345 #else /* __STDC__ */
3346 smtpmessage(f, m, mci, va_alist)
3351 #endif /* __STDC__ */
3355 SM_VA_START(ap, mci);
3356 (void) sm_vsnprintf(SmtpMsgBuffer, sizeof(SmtpMsgBuffer), f, ap);
3359 if (tTd(18, 1) || Verbose)
3360 nmessage(">>> %s", SmtpMsgBuffer);
3361 if (TrafficLogFile != NULL)
3362 (void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
3363 "%05d >>> %s\n", (int) CurrentPid,
3365 if (mci->mci_out != NULL)
3367 (void) sm_io_fprintf(mci->mci_out, SM_TIME_DEFAULT, "%s%s",
3368 SmtpMsgBuffer, m == NULL ? "\r\n"
3371 else if (tTd(18, 1))
3373 sm_dprintf("smtpmessage: NULL mci_out\n");