2 * Copyright (c) 1998-2008 Sendmail, 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 # include <libmilter/mfapi.h>
17 # include <libmilter/mfdef.h>
20 SM_RCSID("@(#)$Id: srvrsmtp.c,v 8.975 2008/03/31 16:32:13 ca Exp $")
27 #endif /* SASL || STARTTLS */
29 # define ENC64LEN(l) (((l) + 2) * 4 / 3 + 1)
30 static int saslmechs __P((sasl_conn_t *, char **));
33 # include <sysexits.h>
35 static SSL_CTX *srv_ctx = NULL; /* TLS server context */
36 static SSL *srv_ssl = NULL; /* per connection context */
38 static bool tls_ok_srv = false;
40 # define TLS_VERIFY_CLIENT() tls_set_verify(srv_ctx, srv_ssl, \
41 bitset(SRV_VRFY_CLT, features))
45 static bool NotFirstDelivery = false;
46 #endif /* _FFR_DM_ONE */
49 #define SRV_NONE 0x0000 /* none... */
50 #define SRV_OFFER_TLS 0x0001 /* offer STARTTLS */
51 #define SRV_VRFY_CLT 0x0002 /* request a cert */
52 #define SRV_OFFER_AUTH 0x0004 /* offer AUTH */
53 #define SRV_OFFER_ETRN 0x0008 /* offer ETRN */
54 #define SRV_OFFER_VRFY 0x0010 /* offer VRFY (not yet used) */
55 #define SRV_OFFER_EXPN 0x0020 /* offer EXPN */
56 #define SRV_OFFER_VERB 0x0040 /* offer VERB */
57 #define SRV_OFFER_DSN 0x0080 /* offer DSN */
59 # define SRV_OFFER_PIPE 0x0100 /* offer PIPELINING */
61 # define SRV_NO_PIPE 0x0200 /* disable PIPELINING, sleep if used */
62 # endif /* _FFR_NO_PIPE */
63 #endif /* PIPELINING */
64 #define SRV_REQ_AUTH 0x0400 /* require AUTH */
65 #define SRV_REQ_SEC 0x0800 /* require security - equiv to AuthOptions=p */
66 #define SRV_TMP_FAIL 0x1000 /* ruleset caused a temporary failure */
68 static unsigned int srvfeatures __P((ENVELOPE *, char *, unsigned int));
70 #define STOP_ATTACK ((time_t) -1)
71 static time_t checksmtpattack __P((volatile unsigned int *, unsigned int,
72 bool, char *, ENVELOPE *));
73 static void printvrfyaddr __P((ADDRESS *, bool, bool));
74 static char *skipword __P((char *volatile, char *));
75 static void setup_smtpd_io __P((void));
79 static int reset_saslconn __P((sasl_conn_t **_conn, char *_hostname,
80 char *_remoteip, char *_localip,
81 char *_auth_id, sasl_ssf_t *_ext_ssf));
83 # define RESET_SASLCONN \
86 result = reset_saslconn(&conn, AuthRealm, remoteip, \
87 localip, auth_id, &ext_ssf); \
88 if (result != SASL_OK) \
92 # else /* SASL >= 20000 */
93 static int reset_saslconn __P((sasl_conn_t **_conn, char *_hostname,
94 struct sockaddr_in *_saddr_r,
95 struct sockaddr_in *_saddr_l,
96 sasl_external_properties_t *_ext_ssf));
97 # define RESET_SASLCONN \
100 result = reset_saslconn(&conn, AuthRealm, &saddr_r, \
101 &saddr_l, &ext_ssf); \
102 if (result != SASL_OK) \
106 # endif /* SASL >= 20000 */
109 extern ENVELOPE BlankEnvelope;
115 (void) sm_snprintf(buf, sizeof(buf), "%d", \
116 BadRcptThrottle > 0 && n_badrcpts > BadRcptThrottle \
117 ? n_badrcpts - 1 : n_badrcpts); \
118 macdefine(&e->e_macro, A_TEMP, macid("{nbadrcpts}"), buf); \
121 #define SKIP_SPACE(s) while (isascii(*s) && isspace(*s)) \
125 ** PARSE_ESMTP_ARGS -- parse EMSTP arguments (for MAIL, RCPT)
129 ** addr_st -- address (RCPT only)
131 ** delimptr -- current position in read buffer
132 ** which -- MAIL/RCPT
133 ** args -- arguments (output)
134 ** esmtp_args -- function to process a single ESMTP argument
141 parse_esmtp_args(e, addr_st, p, delimptr, which, args, esmtp_args)
148 esmtp_args_F esmtp_args;
156 while (p != NULL && *p != '\0')
162 /* locate the beginning of the keyword */
168 /* skip to the value portion */
169 while ((isascii(*p) && isalnum(*p)) || *p == '-')
177 /* skip to the end of the value */
178 while (*p != '\0' && *p != ' ' &&
179 !(isascii(*p) && iscntrl(*p)) &&
188 sm_dprintf("%s: got arg %s=\"%s\"\n", which, kp,
189 vp == NULL ? "<null>" : vp);
191 esmtp_args(addr_st, kp, vp, e);
197 if (argno >= MAXSMTPARGS - 1)
198 usrerr("501 5.5.4 Too many parameters");
207 ** SMTP -- run the SMTP protocol.
210 ** nullserver -- if non-NULL, rejection message for
211 ** (almost) all SMTP commands.
212 ** d_flags -- daemon flags
213 ** e -- the envelope.
219 ** Reads commands from the input channel and processes them.
223 ** Notice: The smtp server doesn't have a session context like the client
224 ** side has (mci). Therefore some data (session oriented) is allocated
225 ** or assigned to the "wrong" structure (esp. STARTTLS, AUTH).
226 ** This should be fixed in a successor version.
231 char *cmd_name; /* command name */
232 int cmd_code; /* internal code, see below */
235 /* values for cmd_code */
236 #define CMDERROR 0 /* bad command */
237 #define CMDMAIL 1 /* mail -- designate sender */
238 #define CMDRCPT 2 /* rcpt -- designate recipient */
239 #define CMDDATA 3 /* data -- send message text */
240 #define CMDRSET 4 /* rset -- reset state */
241 #define CMDVRFY 5 /* vrfy -- verify address */
242 #define CMDEXPN 6 /* expn -- expand address */
243 #define CMDNOOP 7 /* noop -- do nothing */
244 #define CMDQUIT 8 /* quit -- close connection and die */
245 #define CMDHELO 9 /* helo -- be polite */
246 #define CMDHELP 10 /* help -- give usage info */
247 #define CMDEHLO 11 /* ehlo -- extended helo (RFC 1425) */
248 #define CMDETRN 12 /* etrn -- flush queue */
250 # define CMDAUTH 13 /* auth -- SASL authenticate */
253 # define CMDSTLS 14 /* STARTTLS -- start TLS session */
254 #endif /* STARTTLS */
255 /* non-standard commands */
256 #define CMDVERB 17 /* verb -- go into verbose mode */
257 /* unimplemented commands from RFC 821 */
258 #define CMDUNIMPL 19 /* unimplemented rfc821 commands */
259 /* use this to catch and log "door handle" attempts on your system */
260 #define CMDLOGBOGUS 23 /* bogus command that should be logged */
261 /* debugging-only commands, only enabled if SMTPDEBUG is defined */
262 #define CMDDBGQSHOW 24 /* showq -- show send queue */
263 #define CMDDBGDEBUG 25 /* debug -- set debug mode */
266 ** Note: If you change this list, remember to update 'helpfile'
269 static struct cmd CmdTab[] =
284 { "send", CMDUNIMPL },
285 { "saml", CMDUNIMPL },
286 { "soml", CMDUNIMPL },
287 { "turn", CMDUNIMPL },
289 { "auth", CMDAUTH, },
292 { "starttls", CMDSTLS, },
293 #endif /* STARTTLS */
294 /* remaining commands are here only to trap and log attempts to use them */
295 { "showq", CMDDBGQSHOW },
296 { "debug", CMDDBGDEBUG },
297 { "wiz", CMDLOGBOGUS },
302 static char *CurSmtpClient; /* who's at the other end of channel */
304 #ifndef MAXBADCOMMANDS
305 # define MAXBADCOMMANDS 25 /* maximum number of bad commands */
306 #endif /* ! MAXBADCOMMANDS */
307 #ifndef MAXHELOCOMMANDS
308 # define MAXHELOCOMMANDS 3 /* max HELO/EHLO commands before slowdown */
309 #endif /* ! MAXHELOCOMMANDS */
310 #ifndef MAXVRFYCOMMANDS
311 # define MAXVRFYCOMMANDS 6 /* max VRFY/EXPN commands before slowdown */
312 #endif /* ! MAXVRFYCOMMANDS */
313 #ifndef MAXETRNCOMMANDS
314 # define MAXETRNCOMMANDS 8 /* max ETRN commands before slowdown */
315 #endif /* ! MAXETRNCOMMANDS */
317 # define MAXTIMEOUT (4 * 60) /* max timeout for bad commands */
318 #endif /* ! MAXTIMEOUT */
321 ** Maximum shift value to compute timeout for bad commands.
322 ** This introduces an upper limit of 2^MAXSHIFT for the timeout.
327 #endif /* ! MAXSHIFT */
329 ERROR _MAXSHIFT > 31 is invalid
330 #endif /* MAXSHIFT */
333 #if MAXBADCOMMANDS > 0
334 # define STOP_IF_ATTACK(r) do \
336 if ((r) == STOP_ATTACK) \
340 #else /* MAXBADCOMMANDS > 0 */
341 # define STOP_IF_ATTACK(r) r
342 #endif /* MAXBADCOMMANDS > 0 */
346 static SM_DEBUG_T DebugLeakSmtp = SM_DEBUG_INITIALIZER("leak_smtp",
347 "@(#)$Debug: leak_smtp - trace memory leaks during SMTP processing $");
348 #endif /* SM_HEAP_CHECK */
352 bool sm_gotmail; /* mail command received */
353 unsigned int sm_nrcpts; /* number of successful RCPT commands */
357 bool sm_milterlist; /* any filters in the list? */
358 milters_T sm_milters;
360 /* e_nrcpts from envelope before recipient() call */
361 unsigned int sm_e_nrcpts_orig;
363 char *sm_quarmsg; /* carry quarantining across messages */
366 static bool smtp_data __P((SMTP_T *, ENVELOPE *));
368 #define MSG_TEMPFAIL "451 4.3.2 Please try again later"
371 # define MILTER_ABORT(e) milter_abort((e))
373 # define MILTER_REPLY(str) \
375 int savelogusrerrs = LogUsrErrs; \
377 milter_cmd_fail = true; \
380 case SMFIR_SHUTDOWN: \
381 if (MilterLogLevel > 3) \
383 sm_syslog(LOG_INFO, e->e_id, \
384 "Milter: %s=%s, reject=421, errormode=4", \
386 LogUsrErrs = false; \
389 bool tsave = QuickAbort; \
391 QuickAbort = false; \
392 usrerr("421 4.3.0 closing connection"); \
393 QuickAbort = tsave; \
394 e->e_sendqueue = NULL; \
398 case SMFIR_REPLYCODE: \
399 if (MilterLogLevel > 3) \
401 sm_syslog(LOG_INFO, e->e_id, \
402 "Milter: %s=%s, reject=%s", \
403 str, addr, response); \
404 LogUsrErrs = false; \
406 if (strncmp(response, "421 ", 4) == 0 \
407 || strncmp(response, "421-", 4) == 0) \
409 bool tsave = QuickAbort; \
411 QuickAbort = false; \
413 QuickAbort = tsave; \
414 e->e_sendqueue = NULL; \
422 if (MilterLogLevel > 3) \
424 sm_syslog(LOG_INFO, e->e_id, \
425 "Milter: %s=%s, reject=550 5.7.1 Command rejected", \
427 LogUsrErrs = false; \
429 usrerr("550 5.7.1 Command rejected"); \
432 case SMFIR_DISCARD: \
433 if (MilterLogLevel > 3) \
434 sm_syslog(LOG_INFO, e->e_id, \
435 "Milter: %s=%s, discard", \
437 e->e_flags |= EF_DISCARD; \
438 milter_cmd_fail = false; \
441 case SMFIR_TEMPFAIL: \
442 if (MilterLogLevel > 3) \
444 sm_syslog(LOG_INFO, e->e_id, \
445 "Milter: %s=%s, reject=%s", \
446 str, addr, MSG_TEMPFAIL); \
447 LogUsrErrs = false; \
449 usrerr(MSG_TEMPFAIL); \
452 milter_cmd_fail = false; \
455 LogUsrErrs = savelogusrerrs; \
456 if (response != NULL) \
457 sm_free(response); /* XXX */ \
461 # define MILTER_ABORT(e)
464 /* clear all SMTP state (for HELO/EHLO/RSET) */
465 #define CLEAR_STATE(cmd) \
468 /* abort milter filters */ \
471 if (smtp.sm_nrcpts > 0) \
473 logundelrcpts(e, cmd, 10, false); \
474 smtp.sm_nrcpts = 0; \
475 macdefine(&e->e_macro, A_PERM, \
476 macid("{nrcpts}"), "0"); \
479 e->e_sendqueue = NULL; \
480 e->e_flags |= EF_CLRQUEUE; \
482 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) \
483 logsender(e, NULL); \
484 e->e_flags &= ~EF_LOGSENDER; \
486 /* clean up a bit */ \
487 smtp.sm_gotmail = false; \
489 dropenvelope(e, true, false); \
490 sm_rpool_free(e->e_rpool); \
491 e = newenvelope(e, CurEnv, sm_rpool_new_x(NULL)); \
493 e->e_features = features; \
495 /* put back discard bit */ \
496 if (smtp.sm_discard) \
497 e->e_flags |= EF_DISCARD; \
499 /* restore connection quarantining */ \
500 if (smtp.sm_quarmsg == NULL) \
502 e->e_quarmsg = NULL; \
503 macdefine(&e->e_macro, A_PERM, \
504 macid("{quarantine}"), ""); \
508 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, \
510 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), \
515 /* sleep to flatten out connection load */
516 #define MIN_DELAY_LOG 15 /* wait before logging this again */
518 /* is it worth setting the process title for 1s? */
519 #define DELAY_CONN(cmd) \
520 if (DelayLA > 0 && (CurrentLA = getla()) >= DelayLA) \
524 sm_setproctitle(true, e, \
525 "%s: %s: delaying %s: load average: %d", \
526 qid_printname(e), CurSmtpClient, \
528 if (LogLevel > 8 && (dnow = curtime()) > log_delay) \
530 sm_syslog(LOG_INFO, e->e_id, \
531 "delaying=%s, load average=%d >= %d", \
532 cmd, CurrentLA, DelayLA); \
533 log_delay = dnow + MIN_DELAY_LOG; \
536 sm_setproctitle(true, e, "%s %s: %.80s", \
537 qid_printname(e), CurSmtpClient, inp); \
540 static bool SevenBitInput_Saved; /* saved version of SevenBitInput */
543 smtp(nullserver, d_flags, e)
544 char *volatile nullserver;
546 register ENVELOPE *volatile e;
548 register char *volatile p;
549 register struct cmd *volatile c = NULL;
551 auto ADDRESS *vrfyqueue;
553 volatile bool gothello; /* helo command received */
554 bool vrfy; /* set if this is a vrfy command */
555 char *volatile protocol; /* sending protocol */
556 char *volatile sendinghost; /* sending hostname */
557 char *volatile peerhostname; /* name of SMTP peer or "localhost" */
560 volatile unsigned int n_badcmds = 0; /* count of bad commands */
561 volatile unsigned int n_badrcpts = 0; /* number of rejected RCPT */
562 volatile unsigned int n_verifies = 0; /* count of VRFY/EXPN */
563 volatile unsigned int n_etrn = 0; /* count of ETRN */
564 volatile unsigned int n_noop = 0; /* count of NOOP/VERB/etc */
565 volatile unsigned int n_helo = 0; /* count of HELO/EHLO */
568 volatile bool tempfail = false;
569 volatile time_t wt; /* timeout after too many commands */
570 volatile time_t previous; /* time after checksmtpattack() */
571 volatile bool lognullconnection = true;
575 char *greetcode = "220";
576 char *hostname; /* my hostname ($j) */
578 char *args[MAXSMTPARGS];
579 char inp[MAXINPLINE];
580 #if MAXINPLINE < MAXLINE
581 ERROR _MAXINPLINE must NOT be less than _MAXLINE: MAXINPLINE < MAXLINE
582 #endif /* MAXINPLINE < MAXLINE */
583 char cmdbuf[MAXLINE];
586 volatile bool sasl_ok;
587 volatile unsigned int n_auth = 0; /* count of AUTH commands */
590 volatile int authenticating;
594 char *auth_id = NULL;
597 char localip[60], remoteip[60];
598 # else /* SASL >= 20000 */
601 sasl_external_properties_t ext_ssf;
602 struct sockaddr_in saddr_l;
603 struct sockaddr_in saddr_r;
604 # endif /* SASL >= 20000 */
605 sasl_security_properties_t ssp;
607 unsigned int inlen, out2len;
609 char *volatile auth_type;
611 volatile unsigned int n_mechs;
618 volatile bool tls_active = false;
619 volatile bool smtps = bitnset(D_SMTPS, d_flags);
623 #endif /* STARTTLS */
624 volatile unsigned int features;
628 # endif /* _FFR_NO_PIPE */
629 #endif /* PIPELINING */
630 volatile time_t log_delay = (time_t) 0;
632 volatile bool milter_cmd_done, milter_cmd_safe;
633 volatile bool milter_rcpt_added, milter_cmd_fail;
635 # define p_addr_st &addr_st
637 # define p_addr_st NULL
640 #if _FFR_BADRCPT_SHUTDOWN
642 #endif /* _FFR_BADRCPT_SHUTDOWN */
644 SevenBitInput_Saved = SevenBitInput;
647 smtp.sm_milterize = (nullserver == NULL);
648 smtp.sm_milterlist = false;
652 /* setup I/O fd correctly for the SMTP server */
656 if (sm_debug_active(&DebugLeakSmtp, 1))
659 sm_dprintf("smtp() heap group #%d\n", sm_heap_group());
661 #endif /* SM_HEAP_CHECK */
663 /* XXX the rpool should be set when e is initialized in main() */
664 e->e_rpool = sm_rpool_new_x(NULL);
665 e->e_macro.mac_rpool = e->e_rpool;
669 peerhostname = RealHostName;
670 if (peerhostname == NULL)
671 peerhostname = "localhost";
672 CurHostName = peerhostname;
673 CurSmtpClient = macvalue('_', e);
674 if (CurSmtpClient == NULL)
675 CurSmtpClient = CurHostName;
677 /* check_relay may have set discard bit, save for later */
678 smtp.sm_discard = bitset(EF_DISCARD, e->e_flags);
681 /* auto-flush output when reading input */
682 (void) sm_io_autoflush(InChannel, OutChannel);
683 #endif /* PIPELINING */
685 sm_setproctitle(true, e, "server %s startup", CurSmtpClient);
687 /* Set default features for server. */
688 features = ((bitset(PRIV_NOETRN, PrivacyFlags) ||
689 bitnset(D_NOETRN, d_flags)) ? SRV_NONE : SRV_OFFER_ETRN)
690 | (bitnset(D_AUTHREQ, d_flags) ? SRV_REQ_AUTH : SRV_NONE)
691 | (bitset(PRIV_NOEXPN, PrivacyFlags) ? SRV_NONE
693 | (bitset(PRIV_NOVERB, PrivacyFlags)
694 ? SRV_NONE : SRV_OFFER_VERB)))
695 | ((bitset(PRIV_NORECEIPTS, PrivacyFlags) || !SendMIMEErrors)
696 ? SRV_NONE : SRV_OFFER_DSN)
698 | (bitnset(D_NOAUTH, d_flags) ? SRV_NONE : SRV_OFFER_AUTH)
699 | (bitset(SASL_SEC_NOPLAINTEXT, SASLOpts) ? SRV_REQ_SEC
704 #endif /* PIPELINING */
706 | (bitnset(D_NOTLS, d_flags) ? SRV_NONE : SRV_OFFER_TLS)
707 | (bitset(TLS_I_NO_VRFY, TLS_Srv_Opts) ? SRV_NONE
709 #endif /* STARTTLS */
711 if (nullserver == NULL)
713 features = srvfeatures(e, CurSmtpClient, features);
714 if (bitset(SRV_TMP_FAIL, features))
717 sm_syslog(LOG_ERR, NOQID,
718 "ERROR: srv_features=tempfail, relay=%.100s, access temporarily disabled",
720 nullserver = "450 4.3.0 Please try again later.";
726 if (bitset(SRV_NO_PIPE, features))
728 /* for consistency */
729 features &= ~SRV_OFFER_PIPE;
731 # endif /* _FFR_NO_PIPE */
732 #endif /* PIPELINING */
734 if (bitset(SRV_REQ_SEC, features))
735 SASLOpts |= SASL_SEC_NOPLAINTEXT;
737 SASLOpts &= ~SASL_SEC_NOPLAINTEXT;
741 else if (strncmp(nullserver, "421 ", 4) == 0)
747 e->e_features = features;
748 hostname = macvalue('j', e);
750 if (AuthRealm == NULL)
751 AuthRealm = hostname;
752 sasl_ok = bitset(SRV_OFFER_AUTH, features);
754 authenticating = SASL_NOT_AUTH;
756 /* SASL server new connection */
760 result = sasl_server_new("smtp", AuthRealm, NULL, NULL, NULL,
763 /* use empty realm: only works in SASL > 1.5.5 */
764 result = sasl_server_new("smtp", AuthRealm, "", NULL, 0, &conn);
765 # else /* SASL >= 20000 */
766 /* use no realm -> realm is set to hostname by SASL lib */
767 result = sasl_server_new("smtp", AuthRealm, NULL, NULL, 0,
769 # endif /* SASL >= 20000 */
770 sasl_ok = result == SASL_OK;
774 sm_syslog(LOG_WARNING, NOQID,
775 "AUTH error: sasl_server_new failed=%d",
782 ** SASL set properties for sasl
783 ** set local/remote IP
784 ** XXX Cyrus SASL v1 only supports IPv4
786 ** XXX where exactly are these used/required?
791 localip[0] = remoteip[0] = '\0';
792 # if NETINET || NETINET6
793 in = macvalue(macid("{daemon_family}"), e);
796 strcmp(in, "inet6") == 0 ||
797 # endif /* NETINET6 */
798 strcmp(in, "inet") == 0))
800 SOCKADDR_LEN_T addrsize;
804 addrsize = sizeof(saddr_r);
805 if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
807 (struct sockaddr *) &saddr_r,
810 if (iptostring(&saddr_r, addrsize,
811 remoteip, sizeof(remoteip)))
813 sasl_setprop(conn, SASL_IPREMOTEPORT,
816 addrsize = sizeof(saddr_l);
817 if (getsockname(sm_io_getinfo(InChannel,
820 (struct sockaddr *) &saddr_l,
823 if (iptostring(&saddr_l, addrsize,
834 # endif /* NETINET || NETINET6 */
835 # else /* SASL >= 20000 */
837 in = macvalue(macid("{daemon_family}"), e);
838 if (in != NULL && strcmp(in, "inet") == 0)
840 SOCKADDR_LEN_T addrsize;
842 addrsize = sizeof(struct sockaddr_in);
843 if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
845 (struct sockaddr *)&saddr_r,
848 sasl_setprop(conn, SASL_IP_REMOTE, &saddr_r);
849 addrsize = sizeof(struct sockaddr_in);
850 if (getsockname(sm_io_getinfo(InChannel,
853 (struct sockaddr *)&saddr_l,
855 sasl_setprop(conn, SASL_IP_LOCAL,
859 # endif /* NETINET */
860 # endif /* SASL >= 20000 */
866 macdefine(&BlankEnvelope.e_macro, A_PERM,
867 macid("{auth_author}"), NULL);
871 (void) memset(&ssp, '\0', sizeof(ssp));
873 /* XXX should these be options settable via .cf ? */
874 /* ssp.min_ssf = 0; is default due to memset() */
876 ssp.max_ssf = MaxSLBits;
877 ssp.maxbufsize = MAXOUTLEN;
879 ssp.security_flags = SASLOpts & SASL_SEC_MASK;
880 sasl_ok = sasl_setprop(conn, SASL_SEC_PROPS, &ssp) == SASL_OK;
885 ** external security strength factor;
886 ** currently we have none so zero
892 sasl_ok = ((sasl_setprop(conn, SASL_SSF_EXTERNAL,
893 &ext_ssf) == SASL_OK) &&
894 (sasl_setprop(conn, SASL_AUTH_EXTERNAL,
895 auth_id) == SASL_OK));
896 # else /* SASL >= 20000 */
898 ext_ssf.auth_id = NULL;
899 sasl_ok = sasl_setprop(conn, SASL_SSF_EXTERNAL,
900 &ext_ssf) == SASL_OK;
901 # endif /* SASL >= 20000 */
904 n_mechs = saslmechs(conn, &mechlist);
910 set_tls_rd_tmo(TimeOuts.to_nextcommand);
911 #endif /* STARTTLS */
914 if (smtp.sm_milterize)
918 /* initialize mail filter connection */
919 smtp.sm_milterlist = milter_init(e, &state, &smtp.sm_milters);
923 if (MilterLogLevel > 3)
924 sm_syslog(LOG_INFO, e->e_id,
925 "Milter: initialization failed, rejecting commands");
927 nullserver = "Command rejected";
928 smtp.sm_milterize = false;
932 if (MilterLogLevel > 3)
933 sm_syslog(LOG_INFO, e->e_id,
934 "Milter: initialization failed, temp failing commands");
936 smtp.sm_milterize = false;
940 if (MilterLogLevel > 3)
941 sm_syslog(LOG_INFO, e->e_id,
942 "Milter: initialization failed, closing connection");
944 smtp.sm_milterize = false;
945 message("421 4.7.0 %s closing connection",
948 /* arrange to ignore send list */
949 e->e_sendqueue = NULL;
950 lognullconnection = false;
955 if (smtp.sm_milterlist && smtp.sm_milterize &&
956 !bitset(EF_DISCARD, e->e_flags))
961 q = macvalue(macid("{client_name}"), e);
962 SM_ASSERT(q != NULL || OpMode == MD_SMTP);
965 response = milter_connect(q, RealHostAddr, e, &state);
968 case SMFIR_REPLYCODE: /* REPLYCODE shouldn't happen */
970 if (MilterLogLevel > 3)
971 sm_syslog(LOG_INFO, e->e_id,
972 "Milter: connect: host=%s, addr=%s, rejecting commands",
974 anynet_ntoa(&RealHostAddr));
976 nullserver = "Command rejected";
977 smtp.sm_milterize = false;
981 if (MilterLogLevel > 3)
982 sm_syslog(LOG_INFO, e->e_id,
983 "Milter: connect: host=%s, addr=%s, temp failing commands",
985 anynet_ntoa(&RealHostAddr));
987 smtp.sm_milterize = false;
991 if (MilterLogLevel > 3)
992 sm_syslog(LOG_INFO, e->e_id,
993 "Milter: connect: host=%s, addr=%s, shutdown",
995 anynet_ntoa(&RealHostAddr));
997 smtp.sm_milterize = false;
998 message("421 4.7.0 %s closing connection",
1001 /* arrange to ignore send list */
1002 e->e_sendqueue = NULL;
1005 if (response != NULL)
1006 sm_free(response); /* XXX */
1011 ** Broken proxies and SMTP slammers
1012 ** push data without waiting, catch them
1018 #endif /* STARTTLS */
1019 *greetcode == '2' && nullserver == NULL)
1023 char pvpbuf[PSBUFSIZE];
1025 /* Ask the rulesets how long to pause */
1027 r = rscap("greet_pause", peerhostname,
1028 anynet_ntoa(&RealHostAddr), e,
1029 &pvp, pvpbuf, sizeof(pvpbuf));
1030 if (r == EX_OK && pvp != NULL && pvp[0] != NULL &&
1031 (pvp[0][0] & 0377) == CANONNET && pvp[1] != NULL)
1033 msecs = strtol(pvp[1], NULL, 10);
1040 struct timeval timeout;
1041 struct timeval bp, ep, tp; /* {begin,end,total}pause */
1044 /* pause for a moment */
1045 timeout.tv_sec = msecs / 1000;
1046 timeout.tv_usec = (msecs % 1000) * 1000;
1048 /* Obey RFC 2821: 4.3.5.2: 220 timeout of 5 minutes */
1049 if (timeout.tv_sec >= 300)
1051 timeout.tv_sec = 300;
1052 timeout.tv_usec = 0;
1055 /* check if data is on the socket during the pause */
1056 fd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
1058 SM_FD_SET(fd, &readfds);
1059 gettimeofday(&bp, NULL);
1060 if (select(fd + 1, FDSET_CAST &readfds,
1061 NULL, NULL, &timeout) > 0 &&
1062 FD_ISSET(fd, &readfds) &&
1063 (eoftest = sm_io_getc(InChannel, SM_TIME_DEFAULT))
1066 sm_io_ungetc(InChannel, SM_TIME_DEFAULT,
1068 gettimeofday(&ep, NULL);
1069 timersub(&ep, &bp, &tp);
1071 nullserver = "Command rejected";
1072 sm_syslog(LOG_INFO, e->e_id,
1073 "rejecting commands from %s [%s] due to pre-greeting traffic after %d seconds",
1075 anynet_ntoa(&RealHostAddr),
1077 (tp.tv_usec >= 500000 ? 1 : 0)
1084 /* If this an smtps connection, start TLS now */
1093 #endif /* STARTTLS */
1095 /* output the first line, inserting "ESMTP" as second word */
1096 if (*greetcode == '5')
1097 (void) sm_snprintf(inp, sizeof(inp),
1098 "%s not accepting messages", hostname);
1100 expand(SmtpGreeting, inp, sizeof(inp), e);
1102 p = strchr(inp, '\n');
1105 id = strchr(inp, ' ');
1107 id = &inp[strlen(inp)];
1109 (void) sm_snprintf(cmdbuf, sizeof(cmdbuf),
1110 "%s %%.*s ESMTP%%s", greetcode);
1112 (void) sm_snprintf(cmdbuf, sizeof(cmdbuf),
1113 "%s-%%.*s ESMTP%%s", greetcode);
1114 message(cmdbuf, (int) (id - inp), inp, id);
1116 /* output remaining lines */
1117 while ((id = p) != NULL && (p = strchr(id, '\n')) != NULL)
1120 if (isascii(*id) && isspace(*id))
1122 (void) sm_strlcpyn(cmdbuf, sizeof(cmdbuf), 2, greetcode, "-%s");
1123 message(cmdbuf, id);
1127 if (isascii(*id) && isspace(*id))
1129 (void) sm_strlcpyn(cmdbuf, sizeof(cmdbuf), 2, greetcode, " %s");
1130 message(cmdbuf, id);
1134 sendinghost = macvalue('s', e);
1136 /* If quarantining by a connect/ehlo action, save between messages */
1137 if (e->e_quarmsg == NULL)
1138 smtp.sm_quarmsg = NULL;
1140 smtp.sm_quarmsg = newstr(e->e_quarmsg);
1142 /* sendinghost's storage must outlive the current envelope */
1143 if (sendinghost != NULL)
1144 sendinghost = sm_strdup_x(sendinghost);
1147 smtp.sm_gotmail = false;
1156 OnlyOneError = true;
1157 e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS);
1159 milter_cmd_fail = false;
1162 /* setup for the read */
1166 (void) sm_io_flush(smioout, SM_TIME_DEFAULT);
1168 /* read the input line */
1169 SmtpPhase = "server cmd read";
1170 sm_setproctitle(true, e, "server %s cmd read", CurSmtpClient);
1173 if (sm_io_error(OutChannel) ||
1174 (p = sfgets(inp, sizeof(inp), InChannel,
1175 TimeOuts.to_nextcommand, SmtpPhase)) == NULL)
1179 d = macvalue(macid("{daemon_name}"), e);
1182 /* end of file, just die */
1186 /* close out milter filters */
1190 message("421 4.4.1 %s Lost input channel from %s",
1191 MyHostName, CurSmtpClient);
1192 if (LogLevel > (smtp.sm_gotmail ? 1 : 19))
1193 sm_syslog(LOG_NOTICE, e->e_id,
1194 "lost input channel from %s to %s after %s",
1196 (c == NULL || c->cmd_name == NULL) ? "startup" : c->cmd_name);
1198 ** If have not accepted mail (DATA), do not bounce
1199 ** bad addresses back to sender.
1202 if (bitset(EF_CLRQUEUE, e->e_flags))
1203 e->e_sendqueue = NULL;
1207 /* also used by "proxy" check below */
1208 inplen = strlen(inp);
1211 ** SMTP AUTH requires accepting any length,
1212 ** at least for challenge/response. However, not imposing
1213 ** a limit is a bad idea (denial of service).
1216 if (authenticating != SASL_PROC_AUTH
1217 && sm_strncasecmp(inp, "AUTH ", 5) != 0
1218 && inplen > MAXLINE)
1220 message("421 4.7.0 %s Command too long, possible attack %s",
1221 MyHostName, CurSmtpClient);
1222 sm_syslog(LOG_INFO, e->e_id,
1223 "%s: SMTP violation, input too long: %lu",
1224 CurSmtpClient, (unsigned long) inplen);
1234 static char *http_cmds[] = { "GET", "POST",
1235 "CONNECT", "USER", NULL };
1237 for (idx = 0; (http_cmd = http_cmds[idx]) != NULL;
1240 cmdlen = strlen(http_cmd);
1241 if (cmdlen < inplen &&
1242 sm_strncasecmp(inp, http_cmd, cmdlen) == 0 &&
1243 isascii(inp[cmdlen]) && isspace(inp[cmdlen]))
1245 /* Open proxy, drop it */
1246 message("421 4.7.0 %s Rejecting open proxy %s",
1247 MyHostName, CurSmtpClient);
1248 sm_syslog(LOG_INFO, e->e_id,
1249 "%s: probable open proxy: command=%.40s",
1250 CurSmtpClient, inp);
1257 /* clean up end of line */
1263 ** if there is more input and pipelining is disabled:
1264 ** delay ... (and maybe discard the input?)
1265 ** XXX this doesn't really work, at least in tests using
1266 ** telnet SM_IO_IS_READABLE only returns 1 if there were
1267 ** more than 2 input lines available.
1270 if (bitset(SRV_NO_PIPE, features) &&
1271 sm_io_getinfo(InChannel, SM_IO_IS_READABLE, NULL) > 0)
1274 sm_syslog(LOG_INFO, NOQID,
1275 "unauthorized PIPELINING, sleeping");
1279 # endif /* _FFR_NO_PIPE */
1280 #endif /* PIPELINING */
1283 if (authenticating == SASL_PROC_AUTH)
1288 authenticating = SASL_NOT_AUTH;
1289 message("501 5.5.2 missing input");
1294 if (*inp == '*' && *(inp + 1) == '\0')
1296 authenticating = SASL_NOT_AUTH;
1299 message("501 5.0.0 AUTH aborted");
1304 /* could this be shorter? XXX */
1306 in = xalloc(strlen(inp) + 1);
1307 result = sasl_decode64(inp, strlen(inp), in,
1308 strlen(inp), &inlen);
1309 # else /* SASL >= 20000 */
1310 out = xalloc(strlen(inp));
1311 result = sasl_decode64(inp, strlen(inp), out, &outlen);
1312 # endif /* SASL >= 20000 */
1313 if (result != SASL_OK)
1315 authenticating = SASL_NOT_AUTH;
1318 message("501 5.5.4 cannot decode AUTH parameter %s",
1322 # endif /* SASL >= 20000 */
1328 result = sasl_server_step(conn, in, inlen,
1331 # else /* SASL >= 20000 */
1332 result = sasl_server_step(conn, out, outlen,
1333 &out, &outlen, &errstr);
1334 # endif /* SASL >= 20000 */
1336 /* get an OK if we're done */
1337 if (result == SASL_OK)
1340 message("235 2.0.0 OK Authenticated");
1341 authenticating = SASL_IS_AUTH;
1342 macdefine(&BlankEnvelope.e_macro, A_TEMP,
1343 macid("{auth_type}"), auth_type);
1346 user = macvalue(macid("{auth_authen}"), e);
1348 /* get security strength (features) */
1349 result = sasl_getprop(conn, SASL_SSF,
1350 (const void **) &ssf);
1351 # else /* SASL >= 20000 */
1352 result = sasl_getprop(conn, SASL_USERNAME,
1354 if (result != SASL_OK)
1357 macdefine(&BlankEnvelope.e_macro,
1359 macid("{auth_authen}"), NULL);
1363 macdefine(&BlankEnvelope.e_macro,
1365 macid("{auth_authen}"),
1366 xtextify(user, "<>\")"));
1371 sasl_getprop(conn, SASL_REALM, (void **) &data);
1374 /* get security strength (features) */
1375 result = sasl_getprop(conn, SASL_SSF,
1377 # endif /* SASL >= 20000 */
1378 if (result != SASL_OK)
1380 macdefine(&BlankEnvelope.e_macro,
1382 macid("{auth_ssf}"), "0");
1389 (void) sm_snprintf(pbuf, sizeof(pbuf),
1391 macdefine(&BlankEnvelope.e_macro,
1393 macid("{auth_ssf}"), pbuf);
1395 sm_dprintf("AUTH auth_ssf: %u\n",
1400 ** Only switch to encrypted connection
1401 ** if a security layer has been negotiated
1404 if (ssf != NULL && *ssf > 0)
1409 ** Convert I/O layer to use SASL.
1410 ** If the call fails, the connection
1414 tmo = TimeOuts.to_datablock * 1000;
1415 if (sfdcsasl(&InChannel, &OutChannel,
1418 /* restart dialogue */
1421 (void) sm_io_autoflush(InChannel,
1423 # endif /* PIPELINING */
1426 syserr("503 5.3.3 SASL TLS failed");
1429 /* NULL pointer ok since it's our function */
1431 sm_syslog(LOG_INFO, NOQID,
1432 "AUTH=server, relay=%s, authid=%.128s, mech=%.16s, bits=%d",
1434 shortenstring(user, 128),
1437 else if (result == SASL_CONTINUE)
1439 len = ENC64LEN(outlen);
1441 result = sasl_encode64(out, outlen, out2, len,
1443 if (result != SASL_OK)
1445 /* correct code? XXX */
1446 /* 454 Temp. authentication failure */
1447 message("454 4.5.4 Internal error: unable to encode64");
1449 sm_syslog(LOG_WARNING, e->e_id,
1450 "AUTH encode64 error [%d for \"%s\"]",
1453 authenticating = SASL_NOT_AUTH;
1457 message("334 %s", out2);
1459 sm_dprintf("AUTH continue: msg='%s' len=%u\n",
1464 # endif /* SASL >= 20000 */
1468 /* not SASL_OK or SASL_CONT */
1469 message("535 5.7.0 authentication failed");
1471 sm_syslog(LOG_WARNING, e->e_id,
1472 "AUTH failure (%s): %s (%d) %s",
1474 sasl_errstring(result, NULL,
1478 sasl_errdetail(conn));
1479 # else /* SASL >= 20000 */
1480 errstr == NULL ? "" : errstr);
1481 # endif /* SASL >= 20000 */
1483 authenticating = SASL_NOT_AUTH;
1488 /* don't want to do any of this if authenticating */
1491 /* echo command to transcript */
1492 if (e->e_xfp != NULL)
1493 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
1497 sm_syslog(LOG_INFO, e->e_id, "<-- %s", inp);
1499 /* break off command */
1500 for (p = inp; isascii(*p) && isspace(*p); p++)
1503 while (*p != '\0' &&
1504 !(isascii(*p) && isspace(*p)) &&
1505 cmd < &cmdbuf[sizeof(cmdbuf) - 2])
1509 /* throw away leading whitespace */
1512 /* decode command */
1513 for (c = CmdTab; c->cmd_name != NULL; c++)
1515 if (sm_strcasecmp(c->cmd_name, cmdbuf) == 0)
1522 /* check whether a "non-null" command has been used */
1523 switch (c->cmd_code)
1527 /* avoid information leak; take first two words? */
1536 lognullconnection = false;
1543 if (e->e_id == NULL)
1544 sm_setproctitle(true, e, "%s: %.80s",
1547 sm_setproctitle(true, e, "%s %s: %.80s",
1554 ** If we are running as a null server, return 550
1555 ** to almost everything.
1558 if (nullserver != NULL || bitnset(D_ETRNONLY, d_flags))
1560 switch (c->cmd_code)
1568 /* process normally */
1572 if (bitnset(D_ETRNONLY, d_flags) &&
1579 #if MAXBADCOMMANDS > 0
1580 /* theoretically this could overflow */
1581 if (nullserver != NULL &&
1582 ++n_badcmds > MAXBADCOMMANDS)
1584 message("421 4.7.0 %s Too many bad commands; closing connection",
1587 /* arrange to ignore send list */
1588 e->e_sendqueue = NULL;
1591 #endif /* MAXBADCOMMANDS > 0 */
1592 if (nullserver != NULL)
1594 if (ISSMTPREPLY(nullserver))
1597 usrerr("550 5.0.0 %s",
1601 usrerr("452 4.4.5 Insufficient disk space; try again later");
1606 switch (c->cmd_code)
1609 case CMDAUTH: /* sasl */
1611 if (!sasl_ok || n_mechs <= 0)
1613 message("503 5.3.3 AUTH not available");
1616 if (authenticating == SASL_IS_AUTH)
1618 message("503 5.5.0 Already Authenticated");
1621 if (smtp.sm_gotmail)
1623 message("503 5.5.0 AUTH not permitted during a mail transaction");
1629 sm_syslog(LOG_INFO, e->e_id,
1630 "SMTP AUTH command (%.100s) from %s tempfailed (due to previous checks)",
1632 usrerr("454 4.3.0 Please try again later");
1638 /* crude way to avoid crack attempts */
1639 STOP_IF_ATTACK(checksmtpattack(&n_auth, n_mechs + 1,
1642 /* make sure mechanism (p) is a valid string */
1643 for (q = p; *q != '\0' && isascii(*q); q++)
1648 while (*++q != '\0' &&
1649 isascii(*q) && isspace(*q))
1652 ismore = (*q != '\0');
1659 message("501 5.5.2 AUTH mechanism must be specified");
1663 /* check whether mechanism is available */
1664 if (iteminlist(p, mechlist, " ") == NULL)
1666 message("504 5.3.3 AUTH mechanism %.32s not available",
1673 ** Unlike a zero-length client answer to a
1674 ** 334 reply, a zero- length initial response
1675 ** is sent as a single equals sign ("=").
1678 if (ismore && *q == '=' && *(q + 1) == '\0')
1680 /* will be free()d, don't use in=""; */
1687 /* could this be shorter? XXX */
1689 in = xalloc(strlen(q) + 1);
1690 result = sasl_decode64(q, strlen(q), in,
1692 # else /* SASL >= 20000 */
1693 in = sm_rpool_malloc(e->e_rpool, strlen(q));
1694 result = sasl_decode64(q, strlen(q), in,
1696 # endif /* SASL >= 20000 */
1697 if (result != SASL_OK)
1699 message("501 5.5.4 cannot BASE64 decode '%s'",
1702 sm_syslog(LOG_WARNING, e->e_id,
1703 "AUTH decode64 error [%d for \"%s\"]",
1706 authenticating = SASL_NOT_AUTH;
1709 # endif /* SASL >= 20000 */
1721 /* see if that auth type exists */
1723 result = sasl_server_start(conn, p, in, inlen,
1727 # else /* SASL >= 20000 */
1728 result = sasl_server_start(conn, p, in, inlen,
1729 &out, &outlen, &errstr);
1730 # endif /* SASL >= 20000 */
1732 if (result != SASL_OK && result != SASL_CONTINUE)
1734 message("535 5.7.0 authentication failed");
1736 sm_syslog(LOG_ERR, e->e_id,
1737 "AUTH failure (%s): %s (%d) %s",
1739 sasl_errstring(result, NULL,
1743 sasl_errdetail(conn));
1744 # else /* SASL >= 20000 */
1746 # endif /* SASL >= 20000 */
1750 auth_type = newstr(p);
1752 if (result == SASL_OK)
1754 /* ugly, but same code */
1756 /* authenticated by the initial response */
1759 /* len is at least 2 */
1760 len = ENC64LEN(outlen);
1762 result = sasl_encode64(out, outlen, out2, len,
1765 if (result != SASL_OK)
1767 message("454 4.5.4 Temporary authentication failure");
1769 sm_syslog(LOG_WARNING, e->e_id,
1770 "AUTH encode64 error [%d for \"%s\"]",
1774 authenticating = SASL_NOT_AUTH;
1779 message("334 %s", out2);
1780 authenticating = SASL_PROC_AUTH;
1784 # endif /* SASL >= 20000 */
1789 case CMDSTLS: /* starttls */
1790 DELAY_CONN("STARTTLS");
1793 message("501 5.5.2 Syntax error (no parameters allowed)");
1796 if (!bitset(SRV_OFFER_TLS, features))
1798 message("503 5.5.0 TLS not available");
1803 message("454 4.3.3 TLS not available after start");
1806 if (smtp.sm_gotmail)
1808 message("503 5.5.0 TLS not permitted during a mail transaction");
1814 sm_syslog(LOG_INFO, e->e_id,
1815 "SMTP STARTTLS command (%.100s) from %s tempfailed (due to previous checks)",
1817 usrerr("454 4.7.0 Please try again later");
1823 ** XXX do we need a temp key ?
1825 # else /* TLS_NO_RSA */
1826 # endif /* TLS_NO_RSA */
1828 # if TLS_VRFY_PER_CTX
1830 ** Note: this sets the verification globally
1832 ** it's ok since it applies only to one transaction
1835 TLS_VERIFY_CLIENT();
1836 # endif /* TLS_VRFY_PER_CTX */
1838 if (srv_ssl != NULL)
1840 else if ((srv_ssl = SSL_new(srv_ctx)) == NULL)
1842 message("454 4.3.3 TLS not available: error generating SSL handle");
1844 tlslogerr("server");
1848 # if !TLS_VRFY_PER_CTX
1850 ** this could be used if it were possible to set
1851 ** verification per SSL (connection)
1852 ** not just per SSL_CTX (global)
1855 TLS_VERIFY_CLIENT();
1856 # endif /* !TLS_VRFY_PER_CTX */
1858 rfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
1859 wfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL);
1861 if (rfd < 0 || wfd < 0 ||
1862 SSL_set_rfd(srv_ssl, rfd) <= 0 ||
1863 SSL_set_wfd(srv_ssl, wfd) <= 0)
1865 message("454 4.3.3 TLS not available: error set fd");
1871 message("220 2.0.0 Ready to start TLS");
1873 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
1874 # endif /* PIPELINING */
1876 SSL_set_accept_state(srv_ssl);
1878 # define SSL_ACC(s) SSL_accept(s)
1880 tlsstart = curtime();
1882 if ((r = SSL_ACC(srv_ssl)) <= 0)
1886 ssl_err = SSL_get_error(srv_ssl, r);
1887 i = tls_retry(srv_ssl, rfd, wfd, tlsstart,
1888 TimeOuts.to_starttls, ssl_err,
1895 sm_syslog(LOG_WARNING, NOQID,
1896 "STARTTLS=server, error: accept failed=%d, SSL_error=%d, errno=%d, retry=%d",
1897 r, ssl_err, errno, i);
1899 tlslogerr("server");
1906 ** according to the next draft of
1907 ** RFC 2487 the connection should be dropped
1910 /* arrange to ignore any current send list */
1911 e->e_sendqueue = NULL;
1915 /* ignore return code for now, it's in {verify} */
1916 (void) tls_get_info(srv_ssl, true,
1918 &BlankEnvelope.e_macro,
1919 bitset(SRV_VRFY_CLT, features));
1922 ** call Stls_client to find out whether
1923 ** to accept the connection from the client
1926 saveQuickAbort = QuickAbort;
1927 saveSuprErrs = SuprErrs;
1930 if (rscheck("tls_client",
1931 macvalue(macid("{verify}"), e),
1933 RSF_RMCOMM|RSF_COUNT,
1934 5, NULL, NOQID, NULL) != EX_OK ||
1937 extern char MsgBuf[];
1939 if (MsgBuf[0] != '\0' && ISSMTPREPLY(MsgBuf))
1940 nullserver = newstr(MsgBuf);
1942 nullserver = "503 5.7.0 Authentication required.";
1944 QuickAbort = saveQuickAbort;
1945 SuprErrs = saveSuprErrs;
1947 tls_ok_srv = false; /* don't offer STARTTLS again */
1956 s = macvalue(macid("{cipher_bits}"), e);
1957 v = macvalue(macid("{verify}"), e);
1958 c = macvalue(macid("{cert_subject}"), e);
1959 verified = (v != NULL && strcmp(v, "OK") == 0);
1960 if (s != NULL && (cipher_bits = atoi(s)) > 0)
1963 ext_ssf = cipher_bits;
1964 auth_id = verified ? c : NULL;
1965 sasl_ok = ((sasl_setprop(conn,
1967 &ext_ssf) == SASL_OK) &&
1970 auth_id) == SASL_OK));
1971 # else /* SASL >= 20000 */
1972 ext_ssf.ssf = cipher_bits;
1973 ext_ssf.auth_id = verified ? c : NULL;
1974 sasl_ok = sasl_setprop(conn,
1976 &ext_ssf) == SASL_OK;
1977 # endif /* SASL >= 20000 */
1980 n_mechs = saslmechs(conn,
1986 /* switch to secure connection */
1987 if (sfdctls(&InChannel, &OutChannel, srv_ssl) == 0)
1991 (void) sm_io_autoflush(InChannel, OutChannel);
1992 # endif /* PIPELINING */
1997 ** XXX this is an internal error
1998 ** how to deal with it?
1999 ** we can't generate an error message
2000 ** since the other side switched to an
2001 ** encrypted layer, but we could not...
2005 nullserver = "454 4.3.3 TLS not available: can't switch to encrypted layer";
2006 syserr("STARTTLS: can't switch to encrypted layer");
2017 #endif /* STARTTLS */
2019 case CMDHELO: /* hello -- introduce yourself */
2020 case CMDEHLO: /* extended hello */
2022 if (c->cmd_code == CMDEHLO)
2025 SmtpPhase = "server EHLO";
2030 SmtpPhase = "server HELO";
2033 /* avoid denial-of-service */
2034 STOP_IF_ATTACK(checksmtpattack(&n_helo, MAXHELOCOMMANDS,
2035 true, "HELO/EHLO", e));
2038 /* RFC2821 4.1.4 allows duplicate HELO/EHLO */
2039 /* check for duplicate HELO/EHLO per RFC 1651 4.2 */
2042 usrerr("503 %s Duplicate HELO/EHLO",
2048 /* check for valid domain name (re 1123 5.2.5) */
2049 if (*p == '\0' && !AllowBogusHELO)
2051 usrerr("501 %s requires domain address",
2056 /* check for long domain name (hides Received: info) */
2057 if (strlen(p) > MAXNAME)
2059 usrerr("501 Invalid domain name");
2061 sm_syslog(LOG_INFO, CurEnv->e_id,
2062 "invalid domain name (too long) from %s",
2068 for (q = p; *q != '\0'; q++)
2078 /* only complain if strict check */
2079 ok = AllowBogusHELO;
2081 /* allow trailing whitespace */
2082 while (!ok && *++q != '\0' &&
2089 if (strchr("[].-_#:", *q) == NULL)
2093 if (*q == '\0' && ok)
2095 q = "pleased to meet you";
2096 sendinghost = sm_strdup_x(p);
2098 else if (!AllowBogusHELO)
2100 usrerr("501 Invalid domain name");
2102 sm_syslog(LOG_INFO, CurEnv->e_id,
2103 "invalid domain name (%s) from %.100s",
2109 q = "accepting invalid domain name";
2112 if (gothello || smtp.sm_gotmail)
2113 CLEAR_STATE(cmdbuf);
2116 if (smtp.sm_milterlist && smtp.sm_milterize &&
2117 !bitset(EF_DISCARD, e->e_flags))
2122 response = milter_helo(p, e, &state);
2126 if (MilterLogLevel > 3)
2127 sm_syslog(LOG_INFO, e->e_id,
2128 "Milter: helo=%s, reject=Command rejected",
2130 nullserver = "Command rejected";
2131 smtp.sm_milterize = false;
2134 case SMFIR_TEMPFAIL:
2135 if (MilterLogLevel > 3)
2136 sm_syslog(LOG_INFO, e->e_id,
2137 "Milter: helo=%s, reject=%s",
2140 smtp.sm_milterize = false;
2143 case SMFIR_REPLYCODE:
2144 if (MilterLogLevel > 3)
2145 sm_syslog(LOG_INFO, e->e_id,
2146 "Milter: helo=%s, reject=%s",
2148 if (strncmp(response, "421 ", 4) != 0
2149 && strncmp(response, "421-", 4) != 0)
2151 nullserver = newstr(response);
2152 smtp.sm_milterize = false;
2157 case SMFIR_SHUTDOWN:
2158 if (MilterLogLevel > 3 &&
2160 sm_syslog(LOG_INFO, e->e_id,
2161 "Milter: helo=%s, reject=421 4.7.0 %s closing connection",
2164 smtp.sm_milterize = false;
2165 if (response != NULL)
2168 message("421 4.7.0 %s closing connection",
2170 /* arrange to ignore send list */
2171 e->e_sendqueue = NULL;
2172 lognullconnection = false;
2175 if (response != NULL)
2179 ** If quarantining by a connect/ehlo action,
2180 ** save between messages
2183 if (smtp.sm_quarmsg == NULL &&
2184 e->e_quarmsg != NULL)
2185 smtp.sm_quarmsg = newstr(e->e_quarmsg);
2190 /* print HELO response message */
2191 if (c->cmd_code != CMDEHLO)
2193 message("250 %s Hello %s, %s",
2194 MyHostName, CurSmtpClient, q);
2198 message("250-%s Hello %s, %s",
2199 MyHostName, CurSmtpClient, q);
2201 /* offer ENHSC even for nullserver */
2202 if (nullserver != NULL)
2204 message("250 ENHANCEDSTATUSCODES");
2209 ** print EHLO features list
2211 ** Note: If you change this list,
2212 ** remember to update 'helpfile'
2215 message("250-ENHANCEDSTATUSCODES");
2217 if (bitset(SRV_OFFER_PIPE, features))
2218 message("250-PIPELINING");
2219 #endif /* PIPELINING */
2220 if (bitset(SRV_OFFER_EXPN, features))
2222 message("250-EXPN");
2223 if (bitset(SRV_OFFER_VERB, features))
2224 message("250-VERB");
2227 message("250-8BITMIME");
2228 #endif /* MIME8TO7 */
2229 if (MaxMessageSize > 0)
2230 message("250-SIZE %ld", MaxMessageSize);
2232 message("250-SIZE");
2234 if (SendMIMEErrors && bitset(SRV_OFFER_DSN, features))
2237 if (bitset(SRV_OFFER_ETRN, features))
2238 message("250-ETRN");
2240 if (sasl_ok && mechlist != NULL && *mechlist != '\0')
2241 message("250-AUTH %s", mechlist);
2245 bitset(SRV_OFFER_TLS, features))
2246 message("250-STARTTLS");
2247 #endif /* STARTTLS */
2248 if (DeliverByMin > 0)
2249 message("250-DELIVERBY %ld",
2250 (long) DeliverByMin);
2251 else if (DeliverByMin == 0)
2252 message("250-DELIVERBY");
2254 /* < 0: no deliver-by */
2256 message("250 HELP");
2259 case CMDMAIL: /* mail -- designate sender */
2260 SmtpPhase = "server MAIL";
2263 /* check for validity of this command */
2264 if (!gothello && bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
2266 usrerr("503 5.0.0 Polite people say HELO first");
2269 if (smtp.sm_gotmail)
2271 usrerr("503 5.5.0 Sender already specified");
2275 if (bitset(SRV_REQ_AUTH, features) &&
2276 authenticating != SASL_IS_AUTH)
2278 usrerr("530 5.7.0 Authentication required");
2283 p = skipword(p, "from");
2289 sm_syslog(LOG_INFO, e->e_id,
2290 "SMTP MAIL command (%.100s) from %s tempfailed (due to previous checks)",
2292 usrerr(MSG_TEMPFAIL);
2296 /* make sure we know who the sending host is */
2297 if (sendinghost == NULL)
2298 sendinghost = peerhostname;
2302 if (sm_debug_active(&DebugLeakSmtp, 1))
2305 sm_dprintf("smtp() heap group #%d\n",
2308 #endif /* SM_HEAP_CHECK */
2314 auth_warning(e, "%s didn't use HELO protocol",
2317 #ifdef PICKY_HELO_CHECK
2318 if (sm_strcasecmp(sendinghost, peerhostname) != 0 &&
2319 (sm_strcasecmp(peerhostname, "localhost") != 0 ||
2320 sm_strcasecmp(sendinghost, MyHostName) != 0))
2322 auth_warning(e, "Host %s claimed to be %s",
2323 CurSmtpClient, sendinghost);
2325 #endif /* PICKY_HELO_CHECK */
2327 if (protocol == NULL)
2329 macdefine(&e->e_macro, A_PERM, 'r', protocol);
2330 macdefine(&e->e_macro, A_PERM, 's', sendinghost);
2336 macdefine(&e->e_macro, A_PERM, macid("{ntries}"), "0");
2337 macdefine(&e->e_macro, A_PERM, macid("{nrcpts}"), "0");
2338 macdefine(&e->e_macro, A_PERM, macid("{nbadrcpts}"),
2340 e->e_flags |= EF_CLRQUEUE;
2341 sm_setproctitle(true, e, "%s %s: %.80s",
2343 CurSmtpClient, inp);
2345 /* do the processing */
2348 extern char *FullName;
2351 SM_FREE_CLR(FullName);
2353 /* must parse sender first */
2355 setsender(p, e, &delimptr, ' ', false);
2356 if (delimptr != NULL && *delimptr != '\0')
2359 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2361 /* Successfully set e_from, allow logging */
2362 e->e_flags |= EF_LOGSENDER;
2364 /* put resulting triple from parseaddr() into macros */
2365 if (e->e_from.q_mailer != NULL)
2366 macdefine(&e->e_macro, A_PERM,
2367 macid("{mail_mailer}"),
2368 e->e_from.q_mailer->m_name);
2370 macdefine(&e->e_macro, A_PERM,
2371 macid("{mail_mailer}"), NULL);
2372 if (e->e_from.q_host != NULL)
2373 macdefine(&e->e_macro, A_PERM,
2374 macid("{mail_host}"),
2377 macdefine(&e->e_macro, A_PERM,
2378 macid("{mail_host}"), "localhost");
2379 if (e->e_from.q_user != NULL)
2380 macdefine(&e->e_macro, A_PERM,
2381 macid("{mail_addr}"),
2384 macdefine(&e->e_macro, A_PERM,
2385 macid("{mail_addr}"), NULL);
2387 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2389 /* check for possible spoofing */
2390 if (RealUid != 0 && OpMode == MD_SMTP &&
2391 !wordinclass(RealUserName, 't') &&
2392 (!bitnset(M_LOCALMAILER,
2393 e->e_from.q_mailer->m_flags) ||
2394 strcmp(e->e_from.q_user, RealUserName) != 0))
2396 auth_warning(e, "%s owned process doing -bs",
2400 /* reset to default value */
2401 SevenBitInput = SevenBitInput_Saved;
2403 /* now parse ESMTP arguments */
2406 parse_esmtp_args(e, NULL, p, delimptr, "MAIL", args,
2409 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2412 # if _FFR_AUTH_PASSING
2413 /* set the default AUTH= if the sender didn't */
2414 if (e->e_auth_param == NULL)
2416 /* XXX only do this for an MSA? */
2417 e->e_auth_param = macvalue(macid("{auth_authen}"),
2419 if (e->e_auth_param == NULL)
2420 e->e_auth_param = "<>";
2423 ** XXX should we invoke Strust_auth now?
2424 ** authorizing as the client that just
2425 ** authenticated, so we'll trust implicitly
2428 # endif /* _FFR_AUTH_PASSING */
2431 /* do config file checking of the sender */
2432 macdefine(&e->e_macro, A_PERM,
2433 macid("{addr_type}"), "e s");
2435 /* make the "real" sender address available */
2436 macdefine(&e->e_macro, A_TEMP, macid("{mail_from}"),
2438 #endif /* _FFR_MAIL_MACRO */
2439 if (rscheck("check_mail", addr,
2440 NULL, e, RSF_RMCOMM|RSF_COUNT, 3,
2441 NULL, e->e_id, NULL) != EX_OK ||
2443 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2444 macdefine(&e->e_macro, A_PERM,
2445 macid("{addr_type}"), NULL);
2447 if (MaxMessageSize > 0 &&
2448 (e->e_msgsize > MaxMessageSize ||
2451 usrerr("552 5.2.3 Message size exceeds fixed maximum message size (%ld)",
2453 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2457 ** XXX always check whether there is at least one fs
2458 ** with enough space?
2459 ** However, this may not help much: the queue group
2460 ** selection may later on select a FS that hasn't
2464 if ((NumFileSys == 1 || NumQueue == 1) &&
2465 !enoughdiskspace(e->e_msgsize, e)
2466 #if _FFR_ANY_FREE_FS
2467 && !filesys_free(e->e_msgsize)
2468 #endif /* _FFR_ANY_FREE_FS */
2472 ** We perform this test again when the
2473 ** queue directory is selected, in collect.
2476 usrerr("452 4.4.5 Insufficient disk space; try again later");
2477 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2480 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2484 if (smtp.sm_milterlist && smtp.sm_milterize &&
2485 !bitset(EF_DISCARD, e->e_flags))
2490 response = milter_envfrom(args, e, &state);
2491 MILTER_REPLY("from");
2495 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2497 message("250 2.1.0 Sender ok");
2498 smtp.sm_gotmail = true;
2500 SM_EXCEPT(exc, "[!F]*")
2503 ** An error occurred while processing a MAIL command.
2504 ** Jump to the common error handling code.
2514 e->e_flags &= ~EF_PM_NOTIFY;
2518 case CMDRCPT: /* rcpt -- designate recipient */
2520 macdefine(&e->e_macro, A_PERM,
2521 macid("{rcpt_mailer}"), NULL);
2522 macdefine(&e->e_macro, A_PERM,
2523 macid("{rcpt_host}"), NULL);
2524 macdefine(&e->e_macro, A_PERM,
2525 macid("{rcpt_addr}"), NULL);
2527 (void) memset(&addr_st, '\0', sizeof(addr_st));
2529 milter_rcpt_added = false;
2530 smtp.sm_e_nrcpts_orig = e->e_nrcpts;
2532 #if _FFR_BADRCPT_SHUTDOWN
2534 ** hack to deal with hack, see below:
2535 ** n_badrcpts is increased is limit is reached.
2538 n_badrcpts_adj = (BadRcptThrottle > 0 &&
2539 n_badrcpts > BadRcptThrottle &&
2541 ? n_badrcpts - 1 : n_badrcpts;
2542 if (BadRcptShutdown > 0 &&
2543 n_badrcpts_adj >= BadRcptShutdown &&
2544 (BadRcptShutdownGood == 0 ||
2545 smtp.sm_nrcpts == 0 ||
2546 (n_badrcpts_adj * 100 /
2547 (smtp.sm_nrcpts + n_badrcpts) >=
2548 BadRcptShutdownGood)))
2551 sm_syslog(LOG_INFO, e->e_id,
2552 "%s: Possible SMTP RCPT flood, shutting down connection.",
2554 message("421 4.7.0 %s Too many bad recipients; closing connection",
2557 /* arrange to ignore any current send list */
2558 e->e_sendqueue = NULL;
2561 #endif /* _FFR_BADRCPT_SHUTDOWN */
2562 if (BadRcptThrottle > 0 &&
2563 n_badrcpts >= BadRcptThrottle)
2566 n_badrcpts == BadRcptThrottle)
2568 sm_syslog(LOG_INFO, e->e_id,
2569 "%s: Possible SMTP RCPT flood, throttling.",
2572 /* To avoid duplicated message */
2578 ** Don't use exponential backoff for now.
2579 ** Some servers will open more connections
2580 ** and actually overload the receiver even
2586 if (!smtp.sm_gotmail)
2588 usrerr("503 5.0.0 Need MAIL before RCPT");
2591 SmtpPhase = "server RCPT";
2597 /* limit flooding of our machine */
2598 if (MaxRcptPerMsg > 0 &&
2599 smtp.sm_nrcpts >= MaxRcptPerMsg)
2601 /* sleep(1); / * slow down? */
2602 usrerr("452 4.5.3 Too many recipients");
2606 if (e->e_sendmode != SM_DELIVER
2608 && (NotFirstDelivery || SM_DM_ONE != e->e_sendmode)
2609 #endif /* _FFR_DM_ONE */
2611 e->e_flags |= EF_VRFYONLY;
2615 ** Do not expand recipients at RCPT time (in the call
2616 ** to recipient()) if a milter can delete or reject
2617 ** a RCPT. If they are expanded, it is impossible
2618 ** for removefromlist() to figure out the expanded
2619 ** members of the original recipient and mark them
2623 if (!(smtp.sm_milterlist && smtp.sm_milterize &&
2624 !bitset(EF_DISCARD, e->e_flags)) &&
2625 (smtp.sm_milters.mis_flags &
2626 (MIS_FL_DEL_RCPT|MIS_FL_REJ_RCPT)) != 0)
2627 e->e_flags |= EF_VRFYONLY;
2628 milter_cmd_done = false;
2629 milter_cmd_safe = false;
2632 p = skipword(p, "to");
2635 macdefine(&e->e_macro, A_PERM,
2636 macid("{addr_type}"), "e r");
2637 a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr,
2639 macdefine(&e->e_macro, A_PERM,
2640 macid("{addr_type}"), NULL);
2645 usrerr("501 5.0.0 Missing recipient");
2649 if (delimptr != NULL && *delimptr != '\0')
2652 /* put resulting triple from parseaddr() into macros */
2653 if (a->q_mailer != NULL)
2654 macdefine(&e->e_macro, A_PERM,
2655 macid("{rcpt_mailer}"),
2656 a->q_mailer->m_name);
2658 macdefine(&e->e_macro, A_PERM,
2659 macid("{rcpt_mailer}"), NULL);
2660 if (a->q_host != NULL)
2661 macdefine(&e->e_macro, A_PERM,
2662 macid("{rcpt_host}"), a->q_host);
2664 macdefine(&e->e_macro, A_PERM,
2665 macid("{rcpt_host}"), "localhost");
2666 if (a->q_user != NULL)
2667 macdefine(&e->e_macro, A_PERM,
2668 macid("{rcpt_addr}"), a->q_user);
2670 macdefine(&e->e_macro, A_PERM,
2671 macid("{rcpt_addr}"), NULL);
2675 /* now parse ESMTP arguments */
2677 parse_esmtp_args(e, a, p, delimptr, "RCPT", args,
2684 ** rscheck() can trigger an "exception"
2685 ** in which case the execution continues at
2686 ** SM_EXCEPT(exc, "[!F]*")
2687 ** This means milter_cmd_safe is not set
2688 ** and hence milter is not invoked.
2689 ** Would it be "safe" to change that, i.e., use
2690 ** milter_cmd_safe = true;
2691 ** here so a milter is informed (if requested)
2692 ** about RCPTs that are rejected by check_rcpt?
2694 # if _FFR_MILTER_CHECK_REJECTIONS_TOO
2695 milter_cmd_safe = true;
2699 /* do config file checking of the recipient */
2700 macdefine(&e->e_macro, A_PERM,
2701 macid("{addr_type}"), "e r");
2702 if (rscheck("check_rcpt", addr,
2703 NULL, e, RSF_RMCOMM|RSF_COUNT, 3,
2704 NULL, e->e_id, p_addr_st) != EX_OK ||
2707 macdefine(&e->e_macro, A_PERM,
2708 macid("{addr_type}"), NULL);
2710 /* If discarding, don't bother to verify user */
2711 if (bitset(EF_DISCARD, e->e_flags))
2712 a->q_state = QS_VERIFIED;
2714 milter_cmd_safe = true;
2717 /* save in recipient list after ESMTP mods */
2718 a = recipient(a, &e->e_sendqueue, 0, e);
2719 /* may trigger exception... */
2722 milter_rcpt_added = true;
2725 if(!(Errors > 0) && QS_IS_BADADDR(a->q_state))
2727 /* punt -- should keep message in ADDRESS.... */
2728 usrerr("550 5.1.1 Addressee unknown");
2733 if (smtp.sm_milterlist && smtp.sm_milterize &&
2734 !bitset(EF_DISCARD, e->e_flags))
2739 /* how to get the error codes? */
2742 macdefine(&e->e_macro, A_PERM,
2743 macid("{rcpt_mailer}"),
2746 a->q_status != NULL &&
2747 a->q_rstatus != NULL)
2749 macdefine(&e->e_macro, A_PERM,
2750 macid("{rcpt_host}"),
2752 macdefine(&e->e_macro, A_PERM,
2753 macid("{rcpt_addr}"),
2758 if (addr_st.q_host != NULL)
2759 macdefine(&e->e_macro,
2761 macid("{rcpt_host}"),
2763 if (addr_st.q_user != NULL)
2764 macdefine(&e->e_macro,
2766 macid("{rcpt_addr}"),
2771 response = milter_envrcpt(args, e, &state,
2773 milter_cmd_done = true;
2778 /* no errors during parsing, but might be a duplicate */
2779 e->e_to = a->q_paddr;
2780 if (!(Errors > 0) && !QS_IS_BADADDR(a->q_state))
2782 if (smtp.sm_nrcpts == 0)
2784 message("250 2.1.5 Recipient ok%s",
2785 QS_IS_QUEUEUP(a->q_state) ?
2786 " (will queue)" : "");
2790 /* Is this needed? */
2793 #endif /* !MILTER */
2794 macdefine(&e->e_macro, A_PERM,
2795 macid("{rcpt_mailer}"), NULL);
2796 macdefine(&e->e_macro, A_PERM,
2797 macid("{rcpt_host}"), NULL);
2798 macdefine(&e->e_macro, A_PERM,
2799 macid("{rcpt_addr}"), NULL);
2800 macdefine(&e->e_macro, A_PERM,
2801 macid("{dsn_notify}"), NULL);
2809 SM_EXCEPT(exc, "[!F]*")
2811 /* An exception occurred while processing RCPT */
2812 e->e_flags &= ~(EF_FATALERRS|EF_PM_NOTIFY);
2816 if (smtp.sm_milterlist && smtp.sm_milterize &&
2817 !bitset(EF_DISCARD, e->e_flags) &&
2818 !milter_cmd_done && milter_cmd_safe)
2823 macdefine(&e->e_macro, A_PERM,
2824 macid("{rcpt_mailer}"), "error");
2826 /* how to get the error codes? */
2827 if (addr_st.q_host != NULL)
2828 macdefine(&e->e_macro, A_PERM,
2829 macid("{rcpt_host}"),
2831 else if (a != NULL && a->q_status != NULL)
2832 macdefine(&e->e_macro, A_PERM,
2833 macid("{rcpt_host}"),
2836 if (addr_st.q_user != NULL)
2837 macdefine(&e->e_macro, A_PERM,
2838 macid("{rcpt_addr}"),
2840 else if (a != NULL && a->q_rstatus != NULL)
2841 macdefine(&e->e_macro, A_PERM,
2842 macid("{rcpt_addr}"),
2845 response = milter_envrcpt(args, e, &state,
2847 milter_cmd_done = true;
2849 macdefine(&e->e_macro, A_PERM,
2850 macid("{rcpt_mailer}"), NULL);
2851 macdefine(&e->e_macro, A_PERM,
2852 macid("{rcpt_host}"), NULL);
2853 macdefine(&e->e_macro, A_PERM,
2854 macid("{rcpt_addr}"), NULL);
2856 if (smtp.sm_milterlist && smtp.sm_milterize &&
2857 milter_rcpt_added && milter_cmd_done &&
2860 (void) removefromlist(addr, &e->e_sendqueue, e);
2861 milter_cmd_fail = false;
2862 if (smtp.sm_e_nrcpts_orig < e->e_nrcpts)
2863 e->e_nrcpts = smtp.sm_e_nrcpts_orig;
2870 case CMDDATA: /* data -- text of mail */
2872 if (!smtp_data(&smtp, e))
2876 case CMDRSET: /* rset -- reset state */
2878 message("451 4.0.0 Test failure");
2880 message("250 2.0.0 Reset state");
2881 CLEAR_STATE(cmdbuf);
2884 case CMDVRFY: /* vrfy -- verify address */
2885 case CMDEXPN: /* expn -- expand address */
2886 vrfy = c->cmd_code == CMDVRFY;
2887 DELAY_CONN(vrfy ? "VRFY" : "EXPN");
2891 sm_syslog(LOG_INFO, e->e_id,
2892 "SMTP %s command (%.100s) from %s tempfailed (due to previous checks)",
2893 vrfy ? "VRFY" : "EXPN",
2896 /* RFC 821 doesn't allow 4xy reply code */
2897 usrerr("550 5.7.1 Please try again later");
2900 wt = checksmtpattack(&n_verifies, MAXVRFYCOMMANDS,
2901 false, vrfy ? "VRFY" : "EXPN", e);
2903 previous = curtime();
2904 if ((vrfy && bitset(PRIV_NOVRFY, PrivacyFlags)) ||
2905 (!vrfy && !bitset(SRV_OFFER_EXPN, features)))
2908 message("252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)");
2910 message("502 5.7.0 Sorry, we do not allow this operation");
2912 sm_syslog(LOG_INFO, e->e_id,
2913 "%s: %s [rejected]",
2915 shortenstring(inp, MAXSHORTSTR));
2918 else if (!gothello &&
2919 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
2922 usrerr("503 5.0.0 I demand that you introduce yourself first");
2928 sm_syslog(LOG_INFO, e->e_id, "%s: %s",
2930 shortenstring(inp, MAXSHORTSTR));
2936 e->e_flags |= EF_VRFYONLY;
2937 while (*p != '\0' && isascii(*p) && isspace(*p))
2941 usrerr("501 5.5.2 Argument required");
2945 /* do config file checking of the address */
2946 if (rscheck(vrfy ? "check_vrfy" : "check_expn",
2947 p, NULL, e, RSF_RMCOMM,
2948 3, NULL, NOQID, NULL) != EX_OK ||
2950 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2951 (void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e);
2957 t = wt - (curtime() - previous);
2962 sm_exc_raisenew_x(&EtypeQuickAbort, 1);
2963 if (vrfyqueue == NULL)
2965 usrerr("554 5.5.2 Nothing to %s", vrfy ? "VRFY" : "EXPN");
2967 while (vrfyqueue != NULL)
2969 if (!QS_IS_UNDELIVERED(vrfyqueue->q_state))
2971 vrfyqueue = vrfyqueue->q_next;
2975 /* see if there is more in the vrfy list */
2977 while ((a = a->q_next) != NULL &&
2978 (!QS_IS_UNDELIVERED(a->q_state)))
2980 printvrfyaddr(vrfyqueue, a == NULL, vrfy);
2984 SM_EXCEPT(exc, "[!F]*")
2987 ** An exception occurred while processing VRFY/EXPN
2996 case CMDETRN: /* etrn -- force queue flush */
2999 /* Don't leak queue information via debug flags */
3000 if (!bitset(SRV_OFFER_ETRN, features) || UseMSP ||
3001 (RealUid != 0 && RealUid != TrustedUid &&
3004 /* different message for MSA ? */
3005 message("502 5.7.0 Sorry, we do not allow this operation");
3007 sm_syslog(LOG_INFO, e->e_id,
3008 "%s: %s [rejected]",
3010 shortenstring(inp, MAXSHORTSTR));
3016 sm_syslog(LOG_INFO, e->e_id,
3017 "SMTP ETRN command (%.100s) from %s tempfailed (due to previous checks)",
3019 usrerr(MSG_TEMPFAIL);
3025 usrerr("500 5.5.2 Parameter required");
3029 /* crude way to avoid denial-of-service attacks */
3030 STOP_IF_ATTACK(checksmtpattack(&n_etrn, MAXETRNCOMMANDS,
3034 ** Do config file checking of the parameter.
3035 ** Even though we have srv_features now, we still
3036 ** need this ruleset because the former is called
3037 ** when the connection has been established, while
3038 ** this ruleset is called when the command is
3039 ** actually issued and therefore has all information
3040 ** available to make a decision.
3043 if (rscheck("check_etrn", p, NULL, e,
3044 RSF_RMCOMM, 3, NULL, NOQID, NULL)
3050 sm_syslog(LOG_INFO, e->e_id,
3051 "%s: ETRN %s", CurSmtpClient,
3052 shortenstring(p, MAXSHORTSTR));
3060 qgrp = name2qid(id);
3061 if (!ISVALIDQGRP(qgrp))
3063 usrerr("459 4.5.4 Queue %s unknown",
3067 for (i = 0; i < NumQueue && Queue[i] != NULL;
3069 Queue[i]->qg_nextrun = (time_t) -1;
3070 Queue[qgrp]->qg_nextrun = 0;
3071 ok = run_work_group(Queue[qgrp]->qg_wgrp,
3072 RWG_FORK|RWG_FORCE);
3073 if (ok && Errors == 0)
3074 message("250 2.0.0 Queuing for queue group %s started", id);
3083 new = (QUEUE_CHAR *) sm_malloc(sizeof(QUEUE_CHAR));
3086 syserr("500 5.5.0 ETRN out of memory");
3089 new->queue_match = id;
3090 new->queue_negate = false;
3091 new->queue_next = NULL;
3092 QueueLimitRecipient = new;
3093 ok = runqueue(true, false, false, true);
3094 sm_free(QueueLimitRecipient); /* XXX */
3095 QueueLimitRecipient = NULL;
3096 if (ok && Errors == 0)
3097 message("250 2.0.0 Queuing for node %s started", p);
3100 case CMDHELP: /* help -- give user info */
3105 case CMDNOOP: /* noop -- do nothing */
3107 STOP_IF_ATTACK(checksmtpattack(&n_noop, MaxNOOPCommands,
3109 message("250 2.0.0 OK");
3112 case CMDQUIT: /* quit -- leave mail */
3113 message("221 2.0.0 %s closing connection", MyHostName);
3115 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
3116 #endif /* PIPELINING */
3118 if (smtp.sm_nrcpts > 0)
3119 logundelrcpts(e, "aborted by sender", 9, false);
3121 /* arrange to ignore any current send list */
3122 e->e_sendqueue = NULL;
3125 /* shutdown TLS connection */
3128 (void) endtls(srv_ssl, "server");
3131 #endif /* STARTTLS */
3133 if (authenticating == SASL_IS_AUTH)
3135 sasl_dispose(&conn);
3136 authenticating = SASL_NOT_AUTH;
3137 /* XXX sasl_done(); this is a child */
3142 /* avoid future 050 messages */
3146 /* close out milter filters */
3150 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
3152 e->e_flags &= ~EF_LOGSENDER;
3154 if (lognullconnection && LogLevel > 5 &&
3159 d = macvalue(macid("{daemon_name}"), e);
3164 ** even though this id is "bogus", it makes
3165 ** it simpler to "grep" related events, e.g.,
3166 ** timeouts for the same connection.
3169 sm_syslog(LOG_INFO, e->e_id,
3170 "%s did not issue MAIL/EXPN/VRFY/ETRN during connection to %s",
3175 /* return to handle next connection */
3178 finis(true, true, ExitStat);
3181 /* just to avoid bogus warning from some compilers */
3184 case CMDVERB: /* set verbose mode */
3186 if (!bitset(SRV_OFFER_EXPN, features) ||
3187 !bitset(SRV_OFFER_VERB, features))
3189 /* this would give out the same info */
3190 message("502 5.7.0 Verbose unavailable");
3193 STOP_IF_ATTACK(checksmtpattack(&n_noop, MaxNOOPCommands,
3196 set_delivery_mode(SM_DELIVER, e);
3197 message("250 2.0.0 Verbose mode");
3201 case CMDDBGQSHOW: /* show queues */
3202 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
3204 printaddr(smioout, e->e_sendqueue, true);
3207 case CMDDBGDEBUG: /* set debug mode */
3208 tTsetup(tTdvect, sizeof(tTdvect), "0-99.1");
3210 message("200 2.0.0 Debug set");
3213 #else /* SMTPDEBUG */
3214 case CMDDBGQSHOW: /* show queues */
3215 case CMDDBGDEBUG: /* set debug mode */
3216 #endif /* SMTPDEBUG */
3217 case CMDLOGBOGUS: /* bogus command */
3218 DELAY_CONN("Bogus");
3220 sm_syslog(LOG_CRIT, e->e_id,
3221 "\"%s\" command from %s (%.100s)",
3222 c->cmd_name, CurSmtpClient,
3223 anynet_ntoa(&RealHostAddr));
3226 case CMDERROR: /* unknown command */
3227 #if MAXBADCOMMANDS > 0
3228 if (++n_badcmds > MAXBADCOMMANDS)
3231 message("421 4.7.0 %s Too many bad commands; closing connection",
3234 /* arrange to ignore any current send list */
3235 e->e_sendqueue = NULL;
3238 #endif /* MAXBADCOMMANDS > 0 */
3240 #if MILTER && SMFI_VERSION > 2
3241 if (smtp.sm_milterlist && smtp.sm_milterize &&
3242 !bitset(EF_DISCARD, e->e_flags))
3247 if (MilterLogLevel > 9)
3248 sm_syslog(LOG_INFO, e->e_id,
3249 "Sending \"%s\" to Milter", inp);
3250 response = milter_unknown(inp, e, &state);
3251 MILTER_REPLY("unknown");
3252 if (state == SMFIR_REPLYCODE ||
3253 state == SMFIR_REJECT ||
3254 state == SMFIR_TEMPFAIL ||
3255 state == SMFIR_SHUTDOWN)
3257 /* MILTER_REPLY already gave an error */
3261 #endif /* MILTER && SMFI_VERSION > 2 */
3263 usrerr("500 5.5.1 Command unrecognized: \"%s\"",
3264 shortenstring(inp, MAXSHORTSTR));
3268 DELAY_CONN("Unimpl");
3269 usrerr("502 5.5.1 Command not implemented: \"%s\"",
3270 shortenstring(inp, MAXSHORTSTR));
3274 DELAY_CONN("default");
3276 syserr("500 5.5.0 smtp: unknown code %d", c->cmd_code);
3283 SM_EXCEPT(exc, "[!F]*")
3286 ** The only possible exception is "E:mta.quickabort".
3287 ** There is nothing to do except fall through and loop.
3294 ** SMTP_DATA -- implement the SMTP DATA command.
3297 ** smtp -- status of SMTP connection.
3301 ** true iff SMTP session can continue.
3304 ** possibly sends message.
3322 unsigned int features;
3325 SmtpPhase = "server DATA";
3326 if (!smtp->sm_gotmail)
3328 usrerr("503 5.0.0 Need MAIL command");
3331 else if (smtp->sm_nrcpts <= 0)
3333 usrerr("503 5.0.0 Need RCPT (recipient)");
3336 (void) sm_snprintf(buf, sizeof(buf), "%u", smtp->sm_nrcpts);
3337 if (rscheck("check_data", buf, NULL, e,
3338 RSF_RMCOMM|RSF_UNSTRUCTURED|RSF_COUNT, 3, NULL,
3339 e->e_id, NULL) != EX_OK)
3342 #if MILTER && SMFI_VERSION > 3
3343 if (smtp->sm_milterlist && smtp->sm_milterize &&
3344 !bitset(EF_DISCARD, e->e_flags))
3348 int savelogusrerrs = LogUsrErrs;
3350 response = milter_data_cmd(e, &state);
3353 case SMFIR_REPLYCODE:
3354 if (MilterLogLevel > 3)
3356 sm_syslog(LOG_INFO, e->e_id,
3357 "Milter: cmd=data, reject=%s",
3362 if (strncmp(response, "421 ", 4) == 0
3363 || strncmp(response, "421-", 4) == 0)
3365 e->e_sendqueue = NULL;
3371 if (MilterLogLevel > 3)
3373 sm_syslog(LOG_INFO, e->e_id,
3374 "Milter: cmd=data, reject=550 5.7.1 Command rejected");
3377 usrerr("550 5.7.1 Command rejected");
3381 if (MilterLogLevel > 3)
3382 sm_syslog(LOG_INFO, e->e_id,
3383 "Milter: cmd=data, discard");
3384 e->e_flags |= EF_DISCARD;
3387 case SMFIR_TEMPFAIL:
3388 if (MilterLogLevel > 3)
3390 sm_syslog(LOG_INFO, e->e_id,
3391 "Milter: cmd=data, reject=%s",
3395 usrerr(MSG_TEMPFAIL);
3398 case SMFIR_SHUTDOWN:
3399 if (MilterLogLevel > 3)
3401 sm_syslog(LOG_INFO, e->e_id,
3402 "Milter: cmd=data, reject=421 4.7.0 %s closing connection",
3406 usrerr("421 4.7.0 %s closing connection", MyHostName);
3407 e->e_sendqueue = NULL;
3410 LogUsrErrs = savelogusrerrs;
3411 if (response != NULL)
3412 sm_free(response); /* XXX */
3414 #endif /* MILTER && SMFI_VERSION > 3 */
3416 /* put back discard bit */
3417 if (smtp->sm_discard)
3418 e->e_flags |= EF_DISCARD;
3420 /* check to see if we need to re-expand aliases */
3421 /* also reset QS_BADADDR on already-diagnosted addrs */
3422 doublequeue = false;
3423 for (a = e->e_sendqueue; a != NULL; a = a->q_next)
3425 if (QS_IS_VERIFIED(a->q_state) &&
3426 !bitset(EF_DISCARD, e->e_flags))
3428 /* need to re-expand aliases */
3431 if (QS_IS_BADADDR(a->q_state))
3433 /* make this "go away" */
3434 a->q_state = QS_DONTSEND;
3438 /* collect the text of the message */
3439 SmtpPhase = "collect";
3442 collect(InChannel, true, NULL, e, true);
3444 /* redefine message size */
3445 (void) sm_snprintf(buf, sizeof(buf), "%ld", e->e_msgsize);
3446 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf);
3448 /* rscheck() will set Errors or EF_DISCARD if it trips */
3449 (void) rscheck("check_eom", buf, NULL, e, RSF_UNSTRUCTURED|RSF_COUNT,
3450 3, NULL, e->e_id, NULL);
3453 milteraccept = true;
3454 if (smtp->sm_milterlist && smtp->sm_milterize &&
3456 !bitset(EF_DISCARD, e->e_flags))
3461 response = milter_data(e, &state);
3464 case SMFIR_REPLYCODE:
3465 if (MilterLogLevel > 3)
3466 sm_syslog(LOG_INFO, e->e_id,
3467 "Milter: data, reject=%s",
3469 milteraccept = false;
3474 milteraccept = false;
3475 if (MilterLogLevel > 3)
3476 sm_syslog(LOG_INFO, e->e_id,
3477 "Milter: data, reject=554 5.7.1 Command rejected");
3478 usrerr("554 5.7.1 Command rejected");
3482 if (MilterLogLevel > 3)
3483 sm_syslog(LOG_INFO, e->e_id,
3484 "Milter: data, discard");
3485 milteraccept = false;
3486 e->e_flags |= EF_DISCARD;
3489 case SMFIR_TEMPFAIL:
3490 if (MilterLogLevel > 3)
3491 sm_syslog(LOG_INFO, e->e_id,
3492 "Milter: data, reject=%s",
3494 milteraccept = false;
3495 usrerr(MSG_TEMPFAIL);
3498 case SMFIR_SHUTDOWN:
3499 if (MilterLogLevel > 3)
3500 sm_syslog(LOG_INFO, e->e_id,
3501 "Milter: data, reject=421 4.7.0 %s closing connection",
3503 milteraccept = false;
3504 usrerr("421 4.7.0 %s closing connection", MyHostName);
3508 if (response != NULL)
3512 /* Milter may have changed message size */
3513 (void) sm_snprintf(buf, sizeof(buf), "%ld", e->e_msgsize);
3514 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf);
3516 /* abort message filters that didn't get the body & log msg is OK */
3517 if (smtp->sm_milterlist && smtp->sm_milterize)
3520 if (milteraccept && MilterLogLevel > 9)
3521 sm_syslog(LOG_INFO, e->e_id, "Milter accept: message");
3525 ** If SuperSafe is SAFE_REALLY_POSTMILTER, and we don't have milter or
3526 ** milter accepted message, sync it now
3528 ** XXX This is almost a copy of the code in collect(): put it into
3529 ** a function that is called from both places?
3532 if (milteraccept && SuperSafe == SAFE_REALLY_POSTMILTER)
3535 SM_FILE_T *volatile df;
3539 dfname = queuename(e, DATAFL_LETTER);
3540 if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0
3546 if (save_errno == EEXIST)
3551 if (stat(dfname, &st) < 0)
3554 syserr("@collect: bfcommit(%s): already on disk, size=%ld",
3555 dfname, (long) st.st_size);
3556 dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
3558 dumpfd(dfd, true, true);
3561 dferror(df, "bfcommit", e);
3563 finis(save_errno != EEXIST, true, ExitStat);
3565 else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
3567 dferror(df, "sm_io_getinfo", e);
3569 finis(true, true, ExitStat);
3572 else if (fsync(afd) < 0)
3574 dferror(df, "fsync", e);
3576 finis(true, true, ExitStat);
3579 else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
3581 dferror(df, "sm_io_close", e);
3583 finis(true, true, ExitStat);
3587 /* Now reopen the df file */
3588 e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
3589 SM_IO_RDONLY, NULL);
3590 if (e->e_dfp == NULL)
3592 /* we haven't acked receipt yet, so just chuck this */
3593 syserr("@Cannot reopen %s", dfname);
3594 finis(true, true, ExitStat);
3600 /* Check if quarantining stats should be updated */
3601 if (e->e_quarmsg != NULL)
3602 markstats(e, NULL, STATS_QUARANTINE);
3605 ** If a header/body check (header checks or milter)
3606 ** set EF_DISCARD, don't queueup the message --
3607 ** that would lose the EF_DISCARD bit and deliver
3611 if (bitset(EF_DISCARD, e->e_flags))
3612 doublequeue = false;
3614 aborting = Errors > 0;
3615 if (!(aborting || bitset(EF_DISCARD, e->e_flags)) &&
3616 (QueueMode == QM_QUARANTINE || e->e_quarmsg == NULL) &&
3617 !split_by_recipient(e))
3618 aborting = bitset(EF_FATALERRS, e->e_flags);
3624 /* Log who the mail would have gone to */
3625 logundelrcpts(e, e->e_message, 8, false);
3628 ** If something above refused the message, we still haven't
3629 ** accepted responsibility for it. Don't send DSNs.
3632 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3633 q->q_flags &= ~Q_PINGFLAGS;
3640 /* from now on, we have to operate silently */
3645 ** Clear message, it may contain an error from the SMTP dialogue.
3646 ** This error must not show up in the queue.
3647 ** Some error message should show up, e.g., alias database
3648 ** not available, but others shouldn't, e.g., from check_rcpt.
3651 e->e_message = NULL;
3655 ** Arrange to send to everyone.
3656 ** If sending to multiple people, mail back
3657 ** errors rather than reporting directly.
3658 ** In any case, don't mail back errors for
3659 ** anything that has happened up to
3660 ** now (the other end will do this).
3661 ** Truncate our transcript -- the mail has gotten
3662 ** to us successfully, and if we have
3663 ** to mail this back, it will be easier
3665 ** Then send to everyone.
3666 ** Finally give a reply code. If an error has
3667 ** already been given, don't mail a
3669 ** We goose error returns by clearing error bit.
3672 SmtpPhase = "delivery";
3673 (void) sm_io_setinfo(e->e_xfp, SM_BF_TRUNCATE, NULL);
3677 _res.retry = TimeOuts.res_retry[RES_TO_FIRST];
3678 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
3679 #endif /* NAMED_BIND */
3681 for (ee = e; ee != NULL; ee = ee->e_sibling)
3683 /* make sure we actually do delivery */
3684 ee->e_flags &= ~EF_CLRQUEUE;
3686 /* from now on, operate silently */
3687 ee->e_errormode = EM_MAIL;
3691 /* make sure it is in the queue */
3692 queueup(ee, false, true);
3698 /* send to all recipients */
3701 if (SM_DM_ONE == e->e_sendmode)
3703 if (NotFirstDelivery)
3706 e->e_sendmode = SM_QUEUE;
3711 NotFirstDelivery = true;
3714 #endif /* _FFR_DM_ONE */
3720 /* put back id for SMTP logging in putoutmsg() */
3721 oldid = CurEnv->e_id;
3724 /* issue success message */
3726 if (MessageAccept != NULL && *MessageAccept != '\0')
3730 expand(MessageAccept, msg, sizeof(msg), e);
3731 message("250 2.0.0 %s", msg);
3734 #endif /* _FFR_MSG_ACCEPT */
3735 message("250 2.0.0 %s Message accepted for delivery", id);
3736 CurEnv->e_id = oldid;
3738 /* if we just queued, poke it */
3741 bool anything_to_send = false;
3744 for (ee = e; ee != NULL; ee = ee->e_sibling)
3746 if (WILL_BE_QUEUED(ee->e_sendmode))
3748 if (shouldqueue(ee->e_msgpriority, ee->e_ctime))
3750 ee->e_sendmode = SM_QUEUE;
3753 else if (QueueMode != QM_QUARANTINE &&
3754 ee->e_quarmsg != NULL)
3756 ee->e_sendmode = SM_QUEUE;
3759 anything_to_send = true;
3761 /* close all the queue files */
3763 if (ee->e_dfp != NULL)
3765 (void) sm_io_close(ee->e_dfp, SM_TIME_DEFAULT);
3770 if (anything_to_send)
3774 ** XXX if we don't do this, we get 250 twice
3775 ** because it is also flushed in the child.
3778 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
3779 #endif /* PIPELINING */
3780 (void) doworklist(e, true, true);
3785 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
3787 e->e_flags &= ~EF_LOGSENDER;
3789 /* clean up a bit */
3790 smtp->sm_gotmail = false;
3793 ** Call dropenvelope if and only if the envelope is *not*
3794 ** being processed by the child process forked by doworklist().
3797 if (aborting || bitset(EF_DISCARD, e->e_flags))
3798 dropenvelope(e, true, false);
3801 for (ee = e; ee != NULL; ee = ee->e_sibling)
3804 QueueMode != QM_QUARANTINE &&
3805 ee->e_quarmsg != NULL)
3807 dropenvelope(ee, true, false);
3810 if (WILL_BE_QUEUED(ee->e_sendmode))
3811 dropenvelope(ee, true, false);
3816 features = e->e_features;
3817 sm_rpool_free(e->e_rpool);
3818 newenvelope(e, e, sm_rpool_new_x(NULL));
3819 e->e_flags = BlankEnvelope.e_flags;
3820 e->e_features = features;
3822 /* restore connection quarantining */
3823 if (smtp->sm_quarmsg == NULL)
3825 e->e_quarmsg = NULL;
3826 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
3830 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, smtp->sm_quarmsg);
3831 macdefine(&e->e_macro, A_PERM,
3832 macid("{quarantine}"), e->e_quarmsg);
3837 ** LOGUNDELRCPTS -- log undelivered (or all) recipients.
3841 ** msg -- message for Stat=
3842 ** level -- log level.
3843 ** all -- log all recipients.
3849 ** logs undelivered (or all) recipients
3853 logundelrcpts(e, msg, level, all)
3861 if (LogLevel <= level || msg == NULL || *msg == '\0')
3864 /* Clear $h so relay= doesn't get mislogged by logdelivery() */
3865 macdefine(&e->e_macro, A_PERM, 'h', NULL);
3867 /* Log who the mail would have gone to */
3868 for (a = e->e_sendqueue; a != NULL; a = a->q_next)
3870 if (!QS_IS_UNDELIVERED(a->q_state) && !all)
3872 e->e_to = a->q_paddr;
3873 logdelivery(NULL, NULL, a->q_status, msg, NULL,
3879 ** CHECKSMTPATTACK -- check for denial-of-service attack by repetition
3882 ** pcounter -- pointer to a counter for this command.
3883 ** maxcount -- maximum value for this counter before we
3885 ** waitnow -- sleep now (in this routine)?
3886 ** cname -- command name for logging.
3887 ** e -- the current envelope.
3891 ** STOP_ATTACK if twice as many commands as allowed and
3895 ** Slows down if we seem to be under attack.
3899 checksmtpattack(pcounter, maxcount, waitnow, cname, e)
3900 volatile unsigned int *pcounter;
3901 unsigned int maxcount;
3906 if (maxcount <= 0) /* no limit */
3909 if (++(*pcounter) >= maxcount)
3914 if (*pcounter == maxcount && LogLevel > 5)
3916 sm_syslog(LOG_INFO, e->e_id,
3917 "%s: possible SMTP attack: command=%.40s, count=%u",
3918 CurSmtpClient, cname, *pcounter);
3920 shift = *pcounter - maxcount;
3922 if (shift > MAXSHIFT || s >= MAXTIMEOUT || s <= 0)
3925 #define IS_ATTACK(s) ((MaxChildren > 0 && *pcounter >= maxcount * 2) \
3926 ? STOP_ATTACK : (time_t) s)
3928 /* sleep at least 1 second before returning */
3929 (void) sleep(*pcounter / maxcount);
3930 s -= *pcounter / maxcount;
3931 if (s >= MAXTIMEOUT || s < 0)
3933 if (waitnow && s > 0)
3936 return IS_ATTACK(0);
3938 return IS_ATTACK(s);
3943 ** SETUP_SMTPD_IO -- setup I/O fd correctly for the SMTP server
3952 ** may change I/O fd.
3958 int inchfd, outchfd, outfd;
3960 inchfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
3961 outchfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL);
3962 outfd = sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL);
3963 if (outchfd != outfd)
3965 /* arrange for debugging output to go to remote host */
3966 (void) dup2(outchfd, outfd);
3970 ** if InChannel and OutChannel are stdin/stdout
3971 ** and connected to ttys
3972 ** and fcntl(STDIN, F_SETFL, O_NONBLOCKING) also changes STDOUT,
3973 ** then "chain" them together.
3976 if (inchfd == STDIN_FILENO && outchfd == STDOUT_FILENO &&
3977 isatty(inchfd) && isatty(outchfd))
3979 int inmode, outmode;
3981 inmode = fcntl(inchfd, F_GETFL, 0);
3985 sm_syslog(LOG_INFO, NOQID,
3986 "fcntl(inchfd, F_GETFL) failed: %s",
3987 sm_errstring(errno));
3990 outmode = fcntl(outchfd, F_GETFL, 0);
3994 sm_syslog(LOG_INFO, NOQID,
3995 "fcntl(outchfd, F_GETFL) failed: %s",
3996 sm_errstring(errno));
3999 if (bitset(O_NONBLOCK, inmode) ||
4000 bitset(O_NONBLOCK, outmode) ||
4001 fcntl(inchfd, F_SETFL, inmode | O_NONBLOCK) == -1)
4003 outmode = fcntl(outchfd, F_GETFL, 0);
4004 if (outmode != -1 && bitset(O_NONBLOCK, outmode))
4006 /* changing InChannel also changes OutChannel */
4007 sm_io_automode(OutChannel, InChannel);
4008 if (tTd(97, 4) && LogLevel > 9)
4009 sm_syslog(LOG_INFO, NOQID,
4010 "set automode for I (%d)/O (%d) in SMTP server",
4014 /* undo change of inchfd */
4015 (void) fcntl(inchfd, F_SETFL, inmode);
4019 ** SKIPWORD -- skip a fixed word.
4022 ** p -- place to start looking.
4023 ** w -- word to skip.
4030 ** clobbers the p data area.
4035 register char *volatile p;
4041 /* find beginning of word */
4045 /* find end of word */
4046 while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
4048 while (isascii(*p) && isspace(*p))
4053 usrerr("501 5.5.2 Syntax error in parameters scanning \"%s\"",
4054 shortenstring(firstp, MAXSHORTSTR));
4063 /* see if the input word matches desired word */
4064 if (sm_strcasecmp(q, w))
4071 ** RESET_MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
4074 ** e -- the envelope.
4081 reset_mail_esmtp_args(e)
4084 /* "size": no reset */
4087 SevenBitInput = SevenBitInput_Saved;
4088 e->e_bodytype = NULL;
4092 macdefine(&e->e_macro, A_PERM, macid("{dsn_envid}"), NULL);
4095 e->e_flags &= ~(EF_RET_PARAM|EF_NO_BODY_RETN);
4096 macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), NULL);
4100 macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"), NULL);
4101 e->e_auth_param = "";
4102 # if _FFR_AUTH_PASSING
4103 macdefine(&BlankEnvelope.e_macro, A_PERM,
4104 macid("{auth_author}"), NULL);
4105 # endif /* _FFR_AUTH_PASSING */
4109 e->e_deliver_by = 0;
4114 ** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
4117 ** a -- address (unused, for compatibility with rcpt_esmtp_args)
4118 ** kp -- the parameter key.
4119 ** vp -- the value of that parameter.
4120 ** e -- the envelope.
4127 mail_esmtp_args(a, kp, vp, e)
4133 if (sm_strcasecmp(kp, "size") == 0)
4137 usrerr("501 5.5.2 SIZE requires a value");
4140 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), vp);
4142 e->e_msgsize = strtol(vp, (char **) NULL, 10);
4143 if (e->e_msgsize == LONG_MAX && errno == ERANGE)
4145 usrerr("552 5.2.3 Message size exceeds maximum value");
4148 if (e->e_msgsize < 0)
4150 usrerr("552 5.2.3 Message size invalid");
4154 else if (sm_strcasecmp(kp, "body") == 0)
4158 usrerr("501 5.5.2 BODY requires a value");
4161 else if (sm_strcasecmp(vp, "8bitmime") == 0)
4163 SevenBitInput = false;
4165 else if (sm_strcasecmp(vp, "7bit") == 0)
4167 SevenBitInput = true;
4171 usrerr("501 5.5.4 Unknown BODY type %s", vp);
4174 e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, vp);
4176 else if (sm_strcasecmp(kp, "envid") == 0)
4178 if (!bitset(SRV_OFFER_DSN, e->e_features))
4180 usrerr("504 5.7.0 Sorry, ENVID not supported, we do not allow DSN");
4185 usrerr("501 5.5.2 ENVID requires a value");
4190 usrerr("501 5.5.4 Syntax error in ENVID parameter value");
4193 if (e->e_envid != NULL)
4195 usrerr("501 5.5.0 Duplicate ENVID parameter");
4198 e->e_envid = sm_rpool_strdup_x(e->e_rpool, vp);
4199 macdefine(&e->e_macro, A_PERM,
4200 macid("{dsn_envid}"), e->e_envid);
4202 else if (sm_strcasecmp(kp, "ret") == 0)
4204 if (!bitset(SRV_OFFER_DSN, e->e_features))
4206 usrerr("504 5.7.0 Sorry, RET not supported, we do not allow DSN");
4211 usrerr("501 5.5.2 RET requires a value");
4214 if (bitset(EF_RET_PARAM, e->e_flags))
4216 usrerr("501 5.5.0 Duplicate RET parameter");
4219 e->e_flags |= EF_RET_PARAM;
4220 if (sm_strcasecmp(vp, "hdrs") == 0)
4221 e->e_flags |= EF_NO_BODY_RETN;
4222 else if (sm_strcasecmp(vp, "full") != 0)
4224 usrerr("501 5.5.2 Bad argument \"%s\" to RET", vp);
4227 macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), vp);
4230 else if (sm_strcasecmp(kp, "auth") == 0)
4234 char *auth_param; /* the value of the AUTH=x */
4235 bool saveQuickAbort = QuickAbort;
4236 bool saveSuprErrs = SuprErrs;
4237 bool saveExitStat = ExitStat;
4241 usrerr("501 5.5.2 AUTH= requires a value");
4244 if (e->e_auth_param != NULL)
4246 usrerr("501 5.5.0 Duplicate AUTH parameter");
4249 if ((q = strchr(vp, ' ')) != NULL)
4252 len = strlen(vp) + 1;
4253 auth_param = xalloc(len);
4254 (void) sm_strlcpy(auth_param, vp, len);
4255 if (!xtextok(auth_param))
4257 usrerr("501 5.5.4 Syntax error in AUTH parameter value");
4258 /* just a warning? */
4262 /* XXX define this always or only if trusted? */
4263 macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"),
4267 ** call Strust_auth to find out whether
4268 ** auth_param is acceptable (trusted)
4269 ** we shouldn't trust it if not authenticated
4270 ** (required by RFC, leave it to ruleset?)
4275 if (strcmp(auth_param, "<>") != 0 &&
4276 (rscheck("trust_auth", auth_param, NULL, e, RSF_RMCOMM,
4277 9, NULL, NOQID, NULL) != EX_OK || Errors > 0))
4281 q = e->e_auth_param;
4282 sm_dprintf("auth=\"%.100s\" not trusted user=\"%.100s\"\n",
4283 auth_param, (q == NULL) ? "" : q);
4287 e->e_auth_param = "<>";
4288 # if _FFR_AUTH_PASSING
4289 macdefine(&BlankEnvelope.e_macro, A_PERM,
4290 macid("{auth_author}"), NULL);
4291 # endif /* _FFR_AUTH_PASSING */
4296 sm_dprintf("auth=\"%.100s\" trusted\n", auth_param);
4297 e->e_auth_param = sm_rpool_strdup_x(e->e_rpool,
4300 sm_free(auth_param); /* XXX */
4304 QuickAbort = saveQuickAbort;
4305 SuprErrs = saveSuprErrs;
4306 ExitStat = saveExitStat;
4309 #define PRTCHAR(c) ((isascii(c) && isprint(c)) ? (c) : '?')
4312 ** "by" is only accepted if DeliverByMin >= 0.
4313 ** We maybe could add this to the list of server_features.
4316 else if (sm_strcasecmp(kp, "by") == 0 && DeliverByMin >= 0)
4322 usrerr("501 5.5.2 BY= requires a value");
4326 e->e_deliver_by = strtol(vp, &s, 10);
4327 if (e->e_deliver_by == LONG_MIN ||
4328 e->e_deliver_by == LONG_MAX ||
4329 e->e_deliver_by > 999999999l ||
4330 e->e_deliver_by < -999999999l)
4332 usrerr("501 5.5.2 BY=%s out of range", vp);
4335 if (s == NULL || *s != ';')
4337 usrerr("501 5.5.2 BY= missing ';'");
4341 ++s; /* XXX: spaces allowed? */
4343 switch (tolower(*s))
4346 e->e_dlvr_flag = DLVR_NOTIFY;
4349 e->e_dlvr_flag = DLVR_RETURN;
4350 if (e->e_deliver_by <= 0)
4352 usrerr("501 5.5.4 mode R requires BY time > 0");
4355 if (DeliverByMin > 0 && e->e_deliver_by > 0 &&
4356 e->e_deliver_by < DeliverByMin)
4358 usrerr("555 5.5.2 time %ld less than %ld",
4359 e->e_deliver_by, (long) DeliverByMin);
4364 usrerr("501 5.5.2 illegal by-mode '%c'", PRTCHAR(*s));
4367 ++s; /* XXX: spaces allowed? */
4369 switch (tolower(*s))
4372 e->e_dlvr_flag |= DLVR_TRACE;
4377 usrerr("501 5.5.2 illegal by-trace '%c'", PRTCHAR(*s));
4381 /* XXX: check whether more characters follow? */
4385 usrerr("555 5.5.4 %s parameter unrecognized", kp);
4391 ** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line
4394 ** a -- the address corresponding to the To: parameter.
4395 ** kp -- the parameter key.
4396 ** vp -- the value of that parameter.
4397 ** e -- the envelope.
4404 rcpt_esmtp_args(a, kp, vp, e)
4410 if (sm_strcasecmp(kp, "notify") == 0)
4414 if (!bitset(SRV_OFFER_DSN, e->e_features))
4416 usrerr("504 5.7.0 Sorry, NOTIFY not supported, we do not allow DSN");
4421 usrerr("501 5.5.2 NOTIFY requires a value");
4424 a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY);
4425 a->q_flags |= QHASNOTIFY;
4426 macdefine(&e->e_macro, A_TEMP, macid("{dsn_notify}"), vp);
4428 if (sm_strcasecmp(vp, "never") == 0)
4430 for (p = vp; p != NULL; vp = p)
4434 s = p = strchr(p, ',');
4437 if (sm_strcasecmp(vp, "success") == 0)
4438 a->q_flags |= QPINGONSUCCESS;
4439 else if (sm_strcasecmp(vp, "failure") == 0)
4440 a->q_flags |= QPINGONFAILURE;
4441 else if (sm_strcasecmp(vp, "delay") == 0)
4442 a->q_flags |= QPINGONDELAY;
4445 usrerr("501 5.5.4 Bad argument \"%s\" to NOTIFY",
4453 else if (sm_strcasecmp(kp, "orcpt") == 0)
4455 if (!bitset(SRV_OFFER_DSN, e->e_features))
4457 usrerr("504 5.7.0 Sorry, ORCPT not supported, we do not allow DSN");
4462 usrerr("501 5.5.2 ORCPT requires a value");
4465 if (strchr(vp, ';') == NULL || !xtextok(vp))
4467 usrerr("501 5.5.4 Syntax error in ORCPT parameter value");
4470 if (a->q_orcpt != NULL)
4472 usrerr("501 5.5.0 Duplicate ORCPT parameter");
4475 a->q_orcpt = sm_rpool_strdup_x(e->e_rpool, vp);
4479 usrerr("555 5.5.4 %s parameter unrecognized", kp);
4484 ** PRINTVRFYADDR -- print an entry in the verify queue
4487 ** a -- the address to print.
4488 ** last -- set if this is the last one.
4489 ** vrfy -- set if this is a VRFY command.
4495 ** Prints the appropriate 250 codes.
4497 #define OFFF (3 + 1 + 5 + 1) /* offset in fmt: SMTP reply + enh. code */
4500 printvrfyaddr(a, last, vrfy)
4501 register ADDRESS *a;
4507 if (vrfy && a->q_mailer != NULL &&
4508 !bitnset(M_VRFY250, a->q_mailer->m_flags))
4509 (void) sm_strlcpy(fmtbuf, "252", sizeof(fmtbuf));
4511 (void) sm_strlcpy(fmtbuf, "250", sizeof(fmtbuf));
4512 fmtbuf[3] = last ? ' ' : '-';
4513 (void) sm_strlcpy(&fmtbuf[4], "2.1.5 ", sizeof(fmtbuf) - 4);
4514 if (a->q_fullname == NULL)
4516 if ((a->q_mailer == NULL ||
4517 a->q_mailer->m_addrtype == NULL ||
4518 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) &&
4519 strchr(a->q_user, '@') == NULL)
4520 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s@%s>",
4521 sizeof(fmtbuf) - OFFF);
4523 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s>",
4524 sizeof(fmtbuf) - OFFF);
4525 message(fmtbuf, a->q_user, MyHostName);
4529 if ((a->q_mailer == NULL ||
4530 a->q_mailer->m_addrtype == NULL ||
4531 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) &&
4532 strchr(a->q_user, '@') == NULL)
4533 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s@%s>",
4534 sizeof(fmtbuf) - OFFF);
4536 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s>",
4537 sizeof(fmtbuf) - OFFF);
4538 message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
4544 ** SASLMECHS -- get list of possible AUTH mechanisms
4547 ** conn -- SASL connection info.
4548 ** mechlist -- output parameter for list of mechanisms.
4555 saslmechs(conn, mechlist)
4559 int len, num, result;
4561 /* "user" is currently unused */
4563 result = sasl_listmech(conn, NULL,
4564 "", " ", "", (const char **) mechlist,
4565 (unsigned int *)&len, &num);
4566 # else /* SASL >= 20000 */
4567 result = sasl_listmech(conn, "user", /* XXX */
4568 "", " ", "", mechlist,
4569 (unsigned int *)&len, (unsigned int *)&num);
4570 # endif /* SASL >= 20000 */
4571 if (result != SASL_OK)
4574 sm_syslog(LOG_WARNING, NOQID,
4575 "AUTH error: listmech=%d, num=%d",
4582 sm_syslog(LOG_INFO, NOQID,
4583 "AUTH: available mech=%s, allowed mech=%s",
4584 *mechlist, AuthMechanisms);
4585 *mechlist = intersect(AuthMechanisms, *mechlist, NULL);
4589 *mechlist = NULL; /* be paranoid... */
4590 if (result == SASL_OK && LogLevel > 9)
4591 sm_syslog(LOG_WARNING, NOQID,
4592 "AUTH warning: no mechanisms");
4599 ** PROXY_POLICY -- define proxy policy for AUTH
4603 ** context -- unused.
4604 ** requested_user -- authorization identity.
4605 ** rlen -- authorization identity length.
4606 ** auth_identity -- authentication identity.
4607 ** alen -- authentication identity length.
4608 ** def_realm -- default user realm.
4609 ** urlen -- user realm length.
4610 ** propctx -- unused.
4616 ** sets {auth_authen} macro.
4620 proxy_policy(conn, context, requested_user, rlen, auth_identity, alen,
4621 def_realm, urlen, propctx)
4624 const char *requested_user;
4626 const char *auth_identity;
4628 const char *def_realm;
4630 struct propctx *propctx;
4632 if (auth_identity == NULL)
4635 macdefine(&BlankEnvelope.e_macro, A_TEMP,
4636 macid("{auth_authen}"), (char *) auth_identity);
4640 # else /* SASL >= 20000 */
4643 ** PROXY_POLICY -- define proxy policy for AUTH
4646 ** context -- unused.
4647 ** auth_identity -- authentication identity.
4648 ** requested_user -- authorization identity.
4649 ** user -- allowed user (output).
4650 ** errstr -- possible error string (output).
4657 proxy_policy(context, auth_identity, requested_user, user, errstr)
4659 const char *auth_identity;
4660 const char *requested_user;
4662 const char **errstr;
4664 if (user == NULL || auth_identity == NULL)
4666 *user = newstr(auth_identity);
4669 # endif /* SASL >= 20000 */
4674 ** INITSRVTLS -- initialize server side TLS
4677 ** tls_ok -- should tls initialization be done?
4683 ** sets tls_ok_srv which is a static variable in this module.
4684 ** Do NOT remove assignments to it!
4694 /* do NOT remove assignment */
4695 tls_ok_srv = inittls(&srv_ctx, TLS_Srv_Opts, true, SrvCertFile,
4696 SrvKeyFile, CACertPath, CACertFile, DHParams);
4699 #endif /* STARTTLS */
4701 ** SRVFEATURES -- get features for SMTP server
4704 ** e -- envelope (should be session context).
4705 ** clientname -- name of client.
4706 ** features -- default features for this invocation.
4712 /* table with options: it uses just one character, how about strings? */
4716 unsigned int srvf_flag;
4717 } srv_feat_table[] =
4719 { 'A', SRV_OFFER_AUTH },
4720 { 'B', SRV_OFFER_VERB },
4721 { 'C', SRV_REQ_SEC },
4722 { 'D', SRV_OFFER_DSN },
4723 { 'E', SRV_OFFER_ETRN },
4724 { 'L', SRV_REQ_AUTH },
4727 { 'N', SRV_NO_PIPE },
4728 # endif /* _FFR_NO_PIPE */
4729 { 'P', SRV_OFFER_PIPE },
4730 #endif /* PIPELINING */
4731 { 'R', SRV_VRFY_CLT }, /* same as V; not documented */
4732 { 'S', SRV_OFFER_TLS },
4733 /* { 'T', SRV_TMP_FAIL }, */
4734 { 'V', SRV_VRFY_CLT },
4735 { 'X', SRV_OFFER_EXPN },
4736 /* { 'Y', SRV_OFFER_VRFY }, */
4741 srvfeatures(e, clientname, features)
4744 unsigned int features;
4748 char pvpbuf[PSBUFSIZE];
4751 r = rscap("srv_features", clientname, "", e, &pvp, pvpbuf,
4755 if (pvp == NULL || pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
4757 if (pvp[1] != NULL && sm_strncasecmp(pvp[1], "temp", 4) == 0)
4758 return SRV_TMP_FAIL;
4761 ** General rule (see sendmail.h, d_flags):
4762 ** lower case: required/offered, upper case: Not required/available
4764 ** Since we can change some features per daemon, we have both
4765 ** cases here: turn on/off a feature.
4768 for (i = 1; pvp[i] != NULL; i++)
4774 if ((opt = srv_feat_table[j].srvf_opt) == '\0')
4777 sm_syslog(LOG_WARNING, e->e_id,
4778 "srvfeatures: unknown feature %s",
4784 features &= ~(srv_feat_table[j].srvf_flag);
4787 if (c == tolower(opt))
4789 features |= srv_feat_table[j].srvf_flag;
4799 ** HELP -- implement the HELP command.
4802 ** topic -- the topic we want help for.
4809 ** outputs the help file to message output.
4811 #define HELPVSTR "#vers "
4812 #define HELPVERSION 2
4819 register SM_FILE_T *hf;
4824 long sff = SFF_OPENASROOT|SFF_REGONLY;
4827 static int foundvers = -1;
4828 extern char Version[];
4830 if (DontLockReadFiles)
4832 if (!bitnset(DBS_HELPFILEINUNSAFEDIRPATH, DontBlameSendmail))
4833 sff |= SFF_SAFEDIRPATH;
4835 if (HelpFile == NULL ||
4836 (hf = safefopen(HelpFile, O_RDONLY, 0444, sff)) == NULL)
4840 message("502 5.3.0 Sendmail %s -- HELP not implemented",
4845 if (topic == NULL || *topic == '\0')
4856 len = strlen(topic);
4858 while (sm_io_fgets(hf, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
4862 if (foundvers < 0 &&
4863 strncmp(buf, HELPVSTR, strlen(HELPVSTR)) == 0)
4867 if (sm_io_sscanf(buf + strlen(HELPVSTR), "%d",
4873 if (strncmp(buf, topic, len) == 0)
4879 /* print version if no/old vers# in file */
4880 if (foundvers < 2 && !noinfo)
4881 message("214-2.0.0 This is Sendmail version %s", Version);
4883 p = strpbrk(buf, " \t");
4885 p = buf + strlen(buf) - 1;
4892 int lbs = sizeof(buf) - (p - buf);
4894 lbp = translate_dollars(p, p, &lbs);
4895 expand(lbp, inp, sizeof(inp), e);
4900 message("214-2.0.0 %s", p);
4906 message("504 5.3.0 HELP topic \"%.10s\" unknown", topic);
4908 message("214 2.0.0 End of HELP info");
4910 if (foundvers != 0 && foundvers < HELPVERSION)
4913 sm_syslog(LOG_WARNING, e->e_id,
4914 "%s too old (require version %d)",
4915 HelpFile, HELPVERSION);
4917 /* avoid log next time */
4921 (void) sm_io_close(hf, SM_TIME_DEFAULT);
4926 ** RESET_SASLCONN -- reset SASL connection data
4929 ** conn -- SASL connection context
4930 ** hostname -- host name
4931 ** various connection data
4938 reset_saslconn(sasl_conn_t **conn, char *hostname,
4940 char *remoteip, char *localip,
4941 char *auth_id, sasl_ssf_t * ext_ssf)
4942 # else /* SASL >= 20000 */
4943 struct sockaddr_in *saddr_r, struct sockaddr_in *saddr_l,
4944 sasl_external_properties_t * ext_ssf)
4945 # endif /* SASL >= 20000 */
4951 result = sasl_server_new("smtp", hostname, NULL, NULL, NULL,
4954 /* use empty realm: only works in SASL > 1.5.5 */
4955 result = sasl_server_new("smtp", hostname, "", NULL, 0, conn);
4956 # else /* SASL >= 20000 */
4957 /* use no realm -> realm is set to hostname by SASL lib */
4958 result = sasl_server_new("smtp", hostname, NULL, NULL, 0,
4960 # endif /* SASL >= 20000 */
4961 if (result != SASL_OK)
4965 # if NETINET || NETINET6
4966 if (remoteip != NULL && *remoteip != '\0')
4967 result = sasl_setprop(*conn, SASL_IPREMOTEPORT, remoteip);
4968 if (result != SASL_OK)
4971 if (localip != NULL && *localip != '\0')
4972 result = sasl_setprop(*conn, SASL_IPLOCALPORT, localip);
4973 if (result != SASL_OK)
4975 # endif /* NETINET || NETINET6 */
4977 result = sasl_setprop(*conn, SASL_SSF_EXTERNAL, ext_ssf);
4978 if (result != SASL_OK)
4981 result = sasl_setprop(*conn, SASL_AUTH_EXTERNAL, auth_id);
4982 if (result != SASL_OK)
4984 # else /* SASL >= 20000 */
4986 if (saddr_r != NULL)
4987 result = sasl_setprop(*conn, SASL_IP_REMOTE, saddr_r);
4988 if (result != SASL_OK)
4991 if (saddr_l != NULL)
4992 result = sasl_setprop(*conn, SASL_IP_LOCAL, saddr_l);
4993 if (result != SASL_OK)
4995 # endif /* NETINET */
4997 result = sasl_setprop(*conn, SASL_SSF_EXTERNAL, ext_ssf);
4998 if (result != SASL_OK)
5000 # endif /* SASL >= 20000 */