xref: /illumos-gate/usr/src/cmd/sendmail/src/srvrsmtp.c (revision e9af4bc0b1cc30cea75d6ad4aa2fde97d985e9be)
17c478bd9Sstevel@tonic-gate /*
2d4660949Sjbeck  * Copyright (c) 1998-2008 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sendmail.h>
157c478bd9Sstevel@tonic-gate #if MILTER
167c478bd9Sstevel@tonic-gate # include <libmilter/mfapi.h>
177c478bd9Sstevel@tonic-gate # include <libmilter/mfdef.h>
187c478bd9Sstevel@tonic-gate #endif /* MILTER */
197c478bd9Sstevel@tonic-gate 
20*e9af4bc0SJohn Beck SM_RCSID("@(#)$Id: srvrsmtp.c,v 8.989 2009/12/18 17:08:01 ca Exp $")
217c478bd9Sstevel@tonic-gate 
2249218d4fSjbeck #include <sm/time.h>
237c478bd9Sstevel@tonic-gate #include <sm/fdset.h>
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #if SASL || STARTTLS
267c478bd9Sstevel@tonic-gate # include "sfsasl.h"
277c478bd9Sstevel@tonic-gate #endif /* SASL || STARTTLS */
287c478bd9Sstevel@tonic-gate #if SASL
297c478bd9Sstevel@tonic-gate # define ENC64LEN(l)	(((l) + 2) * 4 / 3 + 1)
307c478bd9Sstevel@tonic-gate static int saslmechs __P((sasl_conn_t *, char **));
317c478bd9Sstevel@tonic-gate #endif /* SASL */
327c478bd9Sstevel@tonic-gate #if STARTTLS
337c478bd9Sstevel@tonic-gate # include <sysexits.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static SSL_CTX	*srv_ctx = NULL;	/* TLS server context */
367c478bd9Sstevel@tonic-gate static SSL	*srv_ssl = NULL;	/* per connection context */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static bool	tls_ok_srv = false;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate # define TLS_VERIFY_CLIENT() tls_set_verify(srv_ctx, srv_ssl, \
417c478bd9Sstevel@tonic-gate 				bitset(SRV_VRFY_CLT, features))
427c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
437c478bd9Sstevel@tonic-gate 
44058561cbSjbeck #if _FFR_DM_ONE
45058561cbSjbeck static bool	NotFirstDelivery = false;
46058561cbSjbeck #endif /* _FFR_DM_ONE */
47058561cbSjbeck 
487c478bd9Sstevel@tonic-gate /* server features */
497c478bd9Sstevel@tonic-gate #define SRV_NONE	0x0000	/* none... */
507c478bd9Sstevel@tonic-gate #define SRV_OFFER_TLS	0x0001	/* offer STARTTLS */
517c478bd9Sstevel@tonic-gate #define SRV_VRFY_CLT	0x0002	/* request a cert */
527c478bd9Sstevel@tonic-gate #define SRV_OFFER_AUTH	0x0004	/* offer AUTH */
537c478bd9Sstevel@tonic-gate #define SRV_OFFER_ETRN	0x0008	/* offer ETRN */
547c478bd9Sstevel@tonic-gate #define SRV_OFFER_VRFY	0x0010	/* offer VRFY (not yet used) */
557c478bd9Sstevel@tonic-gate #define SRV_OFFER_EXPN	0x0020	/* offer EXPN */
567c478bd9Sstevel@tonic-gate #define SRV_OFFER_VERB	0x0040	/* offer VERB */
577c478bd9Sstevel@tonic-gate #define SRV_OFFER_DSN	0x0080	/* offer DSN */
587c478bd9Sstevel@tonic-gate #if PIPELINING
597c478bd9Sstevel@tonic-gate # define SRV_OFFER_PIPE	0x0100	/* offer PIPELINING */
607c478bd9Sstevel@tonic-gate # if _FFR_NO_PIPE
617c478bd9Sstevel@tonic-gate #  define SRV_NO_PIPE	0x0200	/* disable PIPELINING, sleep if used */
627c478bd9Sstevel@tonic-gate # endif /* _FFR_NO_PIPE */
637c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
647c478bd9Sstevel@tonic-gate #define SRV_REQ_AUTH	0x0400	/* require AUTH */
657c478bd9Sstevel@tonic-gate #define SRV_REQ_SEC	0x0800	/* require security - equiv to AuthOptions=p */
667c478bd9Sstevel@tonic-gate #define SRV_TMP_FAIL	0x1000	/* ruleset caused a temporary failure */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static unsigned int	srvfeatures __P((ENVELOPE *, char *, unsigned int));
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	STOP_ATTACK	((time_t) -1)
717c478bd9Sstevel@tonic-gate static time_t	checksmtpattack __P((volatile unsigned int *, unsigned int,
727c478bd9Sstevel@tonic-gate 				     bool, char *, ENVELOPE *));
737c478bd9Sstevel@tonic-gate static void	printvrfyaddr __P((ADDRESS *, bool, bool));
747c478bd9Sstevel@tonic-gate static char	*skipword __P((char *volatile, char *));
757c478bd9Sstevel@tonic-gate static void	setup_smtpd_io __P((void));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #if SASL
787c478bd9Sstevel@tonic-gate # if SASL >= 20000
797c478bd9Sstevel@tonic-gate static int reset_saslconn __P((sasl_conn_t **_conn, char *_hostname,
807c478bd9Sstevel@tonic-gate 				char *_remoteip, char *_localip,
817c478bd9Sstevel@tonic-gate 				char *_auth_id, sasl_ssf_t *_ext_ssf));
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate # define RESET_SASLCONN	\
847c478bd9Sstevel@tonic-gate 	do							\
857c478bd9Sstevel@tonic-gate 	{							\
867c478bd9Sstevel@tonic-gate 		result = reset_saslconn(&conn, AuthRealm, remoteip, \
877c478bd9Sstevel@tonic-gate 					localip, auth_id, &ext_ssf); \
887c478bd9Sstevel@tonic-gate 		if (result != SASL_OK)				\
897c478bd9Sstevel@tonic-gate 			sasl_ok = false;			\
907c478bd9Sstevel@tonic-gate 	} while (0)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
937c478bd9Sstevel@tonic-gate static int reset_saslconn __P((sasl_conn_t **_conn, char *_hostname,
947c478bd9Sstevel@tonic-gate 				struct sockaddr_in *_saddr_r,
957c478bd9Sstevel@tonic-gate 				struct sockaddr_in *_saddr_l,
967c478bd9Sstevel@tonic-gate 				sasl_external_properties_t *_ext_ssf));
977c478bd9Sstevel@tonic-gate # define RESET_SASLCONN	\
987c478bd9Sstevel@tonic-gate 	do							\
997c478bd9Sstevel@tonic-gate 	{							\
1007c478bd9Sstevel@tonic-gate 		result = reset_saslconn(&conn, AuthRealm, &saddr_r, \
1017c478bd9Sstevel@tonic-gate 					&saddr_l, &ext_ssf);	\
1027c478bd9Sstevel@tonic-gate 		if (result != SASL_OK)				\
1037c478bd9Sstevel@tonic-gate 			sasl_ok = false;			\
1047c478bd9Sstevel@tonic-gate 	} while (0)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
1077c478bd9Sstevel@tonic-gate #endif /* SASL */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate extern ENVELOPE	BlankEnvelope;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define NBADRCPTS						\
1127c478bd9Sstevel@tonic-gate 	do							\
1137c478bd9Sstevel@tonic-gate 	{							\
1147c478bd9Sstevel@tonic-gate 		char buf[16];					\
115058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf), "%d",	\
1167c478bd9Sstevel@tonic-gate 			BadRcptThrottle > 0 && n_badrcpts > BadRcptThrottle \
1177c478bd9Sstevel@tonic-gate 				? n_badrcpts - 1 : n_badrcpts);	\
1187c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, macid("{nbadrcpts}"), buf); \
1197c478bd9Sstevel@tonic-gate 	} while (0)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define SKIP_SPACE(s)	while (isascii(*s) && isspace(*s))	\
1227c478bd9Sstevel@tonic-gate 				(s)++
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
125058561cbSjbeck **  PARSE_ESMTP_ARGS -- parse EMSTP arguments (for MAIL, RCPT)
126058561cbSjbeck **
127058561cbSjbeck **	Parameters:
128058561cbSjbeck **		e -- the envelope
129058561cbSjbeck **		addr_st -- address (RCPT only)
130058561cbSjbeck **		p -- read buffer
131058561cbSjbeck **		delimptr -- current position in read buffer
132058561cbSjbeck **		which -- MAIL/RCPT
133058561cbSjbeck **		args -- arguments (output)
134058561cbSjbeck **		esmtp_args -- function to process a single ESMTP argument
135058561cbSjbeck **
136058561cbSjbeck **	Returns:
137058561cbSjbeck **		none
138058561cbSjbeck */
139058561cbSjbeck 
140058561cbSjbeck void
parse_esmtp_args(e,addr_st,p,delimptr,which,args,esmtp_args)141058561cbSjbeck parse_esmtp_args(e, addr_st, p, delimptr, which, args, esmtp_args)
142058561cbSjbeck 	ENVELOPE *e;
143058561cbSjbeck 	ADDRESS *addr_st;
144058561cbSjbeck 	char *p;
145058561cbSjbeck 	char *delimptr;
146058561cbSjbeck 	char *which;
147058561cbSjbeck 	char *args[];
148058561cbSjbeck 	esmtp_args_F esmtp_args;
149058561cbSjbeck {
150058561cbSjbeck 	int argno;
151058561cbSjbeck 
152058561cbSjbeck 	argno = 0;
153058561cbSjbeck 	if (args != NULL)
154058561cbSjbeck 		args[argno++] = p;
155058561cbSjbeck 	p = delimptr;
156058561cbSjbeck 	while (p != NULL && *p != '\0')
157058561cbSjbeck 	{
158058561cbSjbeck 		char *kp;
159058561cbSjbeck 		char *vp = NULL;
160058561cbSjbeck 		char *equal = NULL;
161058561cbSjbeck 
162058561cbSjbeck 		/* locate the beginning of the keyword */
163058561cbSjbeck 		SKIP_SPACE(p);
164058561cbSjbeck 		if (*p == '\0')
165058561cbSjbeck 			break;
166058561cbSjbeck 		kp = p;
167058561cbSjbeck 
168058561cbSjbeck 		/* skip to the value portion */
169058561cbSjbeck 		while ((isascii(*p) && isalnum(*p)) || *p == '-')
170058561cbSjbeck 			p++;
171058561cbSjbeck 		if (*p == '=')
172058561cbSjbeck 		{
173058561cbSjbeck 			equal = p;
174058561cbSjbeck 			*p++ = '\0';
175058561cbSjbeck 			vp = p;
176058561cbSjbeck 
177058561cbSjbeck 			/* skip to the end of the value */
178058561cbSjbeck 			while (*p != '\0' && *p != ' ' &&
179058561cbSjbeck 			       !(isascii(*p) && iscntrl(*p)) &&
180058561cbSjbeck 			       *p != '=')
181058561cbSjbeck 				p++;
182058561cbSjbeck 		}
183058561cbSjbeck 
184058561cbSjbeck 		if (*p != '\0')
185058561cbSjbeck 			*p++ = '\0';
186058561cbSjbeck 
187058561cbSjbeck 		if (tTd(19, 1))
188058561cbSjbeck 			sm_dprintf("%s: got arg %s=\"%s\"\n", which, kp,
189058561cbSjbeck 				vp == NULL ? "<null>" : vp);
190058561cbSjbeck 
191058561cbSjbeck 		esmtp_args(addr_st, kp, vp, e);
192058561cbSjbeck 		if (equal != NULL)
193058561cbSjbeck 			*equal = '=';
194058561cbSjbeck 		if (args != NULL)
195058561cbSjbeck 			args[argno] = kp;
196058561cbSjbeck 		argno++;
197058561cbSjbeck 		if (argno >= MAXSMTPARGS - 1)
198058561cbSjbeck 			usrerr("501 5.5.4 Too many parameters");
199058561cbSjbeck 		if (Errors > 0)
200058561cbSjbeck 			break;
201058561cbSjbeck 	}
202058561cbSjbeck 	if (args != NULL)
203058561cbSjbeck 		args[argno] = NULL;
204058561cbSjbeck }
205058561cbSjbeck 
206058561cbSjbeck /*
2077c478bd9Sstevel@tonic-gate **  SMTP -- run the SMTP protocol.
2087c478bd9Sstevel@tonic-gate **
2097c478bd9Sstevel@tonic-gate **	Parameters:
2107c478bd9Sstevel@tonic-gate **		nullserver -- if non-NULL, rejection message for
2117c478bd9Sstevel@tonic-gate **			(almost) all SMTP commands.
2127c478bd9Sstevel@tonic-gate **		d_flags -- daemon flags
2137c478bd9Sstevel@tonic-gate **		e -- the envelope.
2147c478bd9Sstevel@tonic-gate **
2157c478bd9Sstevel@tonic-gate **	Returns:
2167c478bd9Sstevel@tonic-gate **		never.
2177c478bd9Sstevel@tonic-gate **
2187c478bd9Sstevel@tonic-gate **	Side Effects:
2197c478bd9Sstevel@tonic-gate **		Reads commands from the input channel and processes them.
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate **  Notice: The smtp server doesn't have a session context like the client
2247c478bd9Sstevel@tonic-gate **	side has (mci). Therefore some data (session oriented) is allocated
2257c478bd9Sstevel@tonic-gate **	or assigned to the "wrong" structure (esp. STARTTLS, AUTH).
2267c478bd9Sstevel@tonic-gate **	This should be fixed in a successor version.
2277c478bd9Sstevel@tonic-gate */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate struct cmd
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	char	*cmd_name;	/* command name */
2327c478bd9Sstevel@tonic-gate 	int	cmd_code;	/* internal code, see below */
2337c478bd9Sstevel@tonic-gate };
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /* values for cmd_code */
2367c478bd9Sstevel@tonic-gate #define CMDERROR	0	/* bad command */
2377c478bd9Sstevel@tonic-gate #define CMDMAIL	1	/* mail -- designate sender */
2387c478bd9Sstevel@tonic-gate #define CMDRCPT	2	/* rcpt -- designate recipient */
2397c478bd9Sstevel@tonic-gate #define CMDDATA	3	/* data -- send message text */
2407c478bd9Sstevel@tonic-gate #define CMDRSET	4	/* rset -- reset state */
2417c478bd9Sstevel@tonic-gate #define CMDVRFY	5	/* vrfy -- verify address */
2427c478bd9Sstevel@tonic-gate #define CMDEXPN	6	/* expn -- expand address */
2437c478bd9Sstevel@tonic-gate #define CMDNOOP	7	/* noop -- do nothing */
2447c478bd9Sstevel@tonic-gate #define CMDQUIT	8	/* quit -- close connection and die */
2457c478bd9Sstevel@tonic-gate #define CMDHELO	9	/* helo -- be polite */
2467c478bd9Sstevel@tonic-gate #define CMDHELP	10	/* help -- give usage info */
2477c478bd9Sstevel@tonic-gate #define CMDEHLO	11	/* ehlo -- extended helo (RFC 1425) */
2487c478bd9Sstevel@tonic-gate #define CMDETRN	12	/* etrn -- flush queue */
2497c478bd9Sstevel@tonic-gate #if SASL
2507c478bd9Sstevel@tonic-gate # define CMDAUTH	13	/* auth -- SASL authenticate */
2517c478bd9Sstevel@tonic-gate #endif /* SASL */
2527c478bd9Sstevel@tonic-gate #if STARTTLS
2537c478bd9Sstevel@tonic-gate # define CMDSTLS	14	/* STARTTLS -- start TLS session */
2547c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
2557c478bd9Sstevel@tonic-gate /* non-standard commands */
2567c478bd9Sstevel@tonic-gate #define CMDVERB	17	/* verb -- go into verbose mode */
2577c478bd9Sstevel@tonic-gate /* unimplemented commands from RFC 821 */
2587c478bd9Sstevel@tonic-gate #define CMDUNIMPL	19	/* unimplemented rfc821 commands */
2597c478bd9Sstevel@tonic-gate /* use this to catch and log "door handle" attempts on your system */
2607c478bd9Sstevel@tonic-gate #define CMDLOGBOGUS	23	/* bogus command that should be logged */
2617c478bd9Sstevel@tonic-gate /* debugging-only commands, only enabled if SMTPDEBUG is defined */
2627c478bd9Sstevel@tonic-gate #define CMDDBGQSHOW	24	/* showq -- show send queue */
2637c478bd9Sstevel@tonic-gate #define CMDDBGDEBUG	25	/* debug -- set debug mode */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate **  Note: If you change this list, remember to update 'helpfile'
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static struct cmd	CmdTab[] =
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	{ "mail",	CMDMAIL		},
2727c478bd9Sstevel@tonic-gate 	{ "rcpt",	CMDRCPT		},
2737c478bd9Sstevel@tonic-gate 	{ "data",	CMDDATA		},
2747c478bd9Sstevel@tonic-gate 	{ "rset",	CMDRSET		},
2757c478bd9Sstevel@tonic-gate 	{ "vrfy",	CMDVRFY		},
2767c478bd9Sstevel@tonic-gate 	{ "expn",	CMDEXPN		},
2777c478bd9Sstevel@tonic-gate 	{ "help",	CMDHELP		},
2787c478bd9Sstevel@tonic-gate 	{ "noop",	CMDNOOP		},
2797c478bd9Sstevel@tonic-gate 	{ "quit",	CMDQUIT		},
2807c478bd9Sstevel@tonic-gate 	{ "helo",	CMDHELO		},
2817c478bd9Sstevel@tonic-gate 	{ "ehlo",	CMDEHLO		},
2827c478bd9Sstevel@tonic-gate 	{ "etrn",	CMDETRN		},
2837c478bd9Sstevel@tonic-gate 	{ "verb",	CMDVERB		},
2847c478bd9Sstevel@tonic-gate 	{ "send",	CMDUNIMPL	},
2857c478bd9Sstevel@tonic-gate 	{ "saml",	CMDUNIMPL	},
2867c478bd9Sstevel@tonic-gate 	{ "soml",	CMDUNIMPL	},
2877c478bd9Sstevel@tonic-gate 	{ "turn",	CMDUNIMPL	},
2887c478bd9Sstevel@tonic-gate #if SASL
2897c478bd9Sstevel@tonic-gate 	{ "auth",	CMDAUTH,	},
2907c478bd9Sstevel@tonic-gate #endif /* SASL */
2917c478bd9Sstevel@tonic-gate #if STARTTLS
2927c478bd9Sstevel@tonic-gate 	{ "starttls",	CMDSTLS,	},
2937c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
2947c478bd9Sstevel@tonic-gate     /* remaining commands are here only to trap and log attempts to use them */
2957c478bd9Sstevel@tonic-gate 	{ "showq",	CMDDBGQSHOW	},
2967c478bd9Sstevel@tonic-gate 	{ "debug",	CMDDBGDEBUG	},
2977c478bd9Sstevel@tonic-gate 	{ "wiz",	CMDLOGBOGUS	},
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	{ NULL,		CMDERROR	}
3007c478bd9Sstevel@tonic-gate };
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static char	*CurSmtpClient;		/* who's at the other end of channel */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #ifndef MAXBADCOMMANDS
3057c478bd9Sstevel@tonic-gate # define MAXBADCOMMANDS 25	/* maximum number of bad commands */
3067c478bd9Sstevel@tonic-gate #endif /* ! MAXBADCOMMANDS */
3077c478bd9Sstevel@tonic-gate #ifndef MAXHELOCOMMANDS
3087c478bd9Sstevel@tonic-gate # define MAXHELOCOMMANDS 3	/* max HELO/EHLO commands before slowdown */
3097c478bd9Sstevel@tonic-gate #endif /* ! MAXHELOCOMMANDS */
3107c478bd9Sstevel@tonic-gate #ifndef MAXVRFYCOMMANDS
3117c478bd9Sstevel@tonic-gate # define MAXVRFYCOMMANDS 6	/* max VRFY/EXPN commands before slowdown */
3127c478bd9Sstevel@tonic-gate #endif /* ! MAXVRFYCOMMANDS */
3137c478bd9Sstevel@tonic-gate #ifndef MAXETRNCOMMANDS
3147c478bd9Sstevel@tonic-gate # define MAXETRNCOMMANDS 8	/* max ETRN commands before slowdown */
3157c478bd9Sstevel@tonic-gate #endif /* ! MAXETRNCOMMANDS */
3167c478bd9Sstevel@tonic-gate #ifndef MAXTIMEOUT
3177c478bd9Sstevel@tonic-gate # define MAXTIMEOUT (4 * 60)	/* max timeout for bad commands */
3187c478bd9Sstevel@tonic-gate #endif /* ! MAXTIMEOUT */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate **  Maximum shift value to compute timeout for bad commands.
3227c478bd9Sstevel@tonic-gate **  This introduces an upper limit of 2^MAXSHIFT for the timeout.
3237c478bd9Sstevel@tonic-gate */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate #ifndef MAXSHIFT
3267c478bd9Sstevel@tonic-gate # define MAXSHIFT 8
3277c478bd9Sstevel@tonic-gate #endif /* ! MAXSHIFT */
3287c478bd9Sstevel@tonic-gate #if MAXSHIFT > 31
3297c478bd9Sstevel@tonic-gate  ERROR _MAXSHIFT > 31 is invalid
3307c478bd9Sstevel@tonic-gate #endif /* MAXSHIFT */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate #if MAXBADCOMMANDS > 0
3347c478bd9Sstevel@tonic-gate # define STOP_IF_ATTACK(r)	do		\
3357c478bd9Sstevel@tonic-gate 	{					\
3367c478bd9Sstevel@tonic-gate 		if ((r) == STOP_ATTACK)		\
3377c478bd9Sstevel@tonic-gate 			goto stopattack;	\
3387c478bd9Sstevel@tonic-gate 	} while (0)
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate #else /* MAXBADCOMMANDS > 0 */
3417c478bd9Sstevel@tonic-gate # define STOP_IF_ATTACK(r)	r
3427c478bd9Sstevel@tonic-gate #endif /* MAXBADCOMMANDS > 0 */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
3467c478bd9Sstevel@tonic-gate static SM_DEBUG_T DebugLeakSmtp = SM_DEBUG_INITIALIZER("leak_smtp",
3477c478bd9Sstevel@tonic-gate 	"@(#)$Debug: leak_smtp - trace memory leaks during SMTP processing $");
3487c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate typedef struct
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	bool		sm_gotmail;	/* mail command received */
3537c478bd9Sstevel@tonic-gate 	unsigned int	sm_nrcpts;	/* number of successful RCPT commands */
3547c478bd9Sstevel@tonic-gate 	bool		sm_discard;
3557c478bd9Sstevel@tonic-gate #if MILTER
3567c478bd9Sstevel@tonic-gate 	bool		sm_milterize;
3577c478bd9Sstevel@tonic-gate 	bool		sm_milterlist;	/* any filters in the list? */
3587800901eSjbeck 	milters_T	sm_milters;
3597800901eSjbeck 
3607800901eSjbeck 	/* e_nrcpts from envelope before recipient() call */
3617800901eSjbeck 	unsigned int	sm_e_nrcpts_orig;
3627c478bd9Sstevel@tonic-gate #endif /* MILTER */
3637c478bd9Sstevel@tonic-gate 	char		*sm_quarmsg;	/* carry quarantining across messages */
3647c478bd9Sstevel@tonic-gate } SMTP_T;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate static bool	smtp_data __P((SMTP_T *, ENVELOPE *));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define MSG_TEMPFAIL "451 4.3.2 Please try again later"
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #if MILTER
3717c478bd9Sstevel@tonic-gate # define MILTER_ABORT(e)	milter_abort((e))
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate # define MILTER_REPLY(str)						\
3747c478bd9Sstevel@tonic-gate 	{								\
3757c478bd9Sstevel@tonic-gate 		int savelogusrerrs = LogUsrErrs;			\
3767c478bd9Sstevel@tonic-gate 									\
3774aac33d3Sjbeck 		milter_cmd_fail = true;					\
3787c478bd9Sstevel@tonic-gate 		switch (state)						\
3797c478bd9Sstevel@tonic-gate 		{							\
380445f2479Sjbeck 		  case SMFIR_SHUTDOWN:					\
381445f2479Sjbeck 			if (MilterLogLevel > 3)				\
382445f2479Sjbeck 			{						\
383445f2479Sjbeck 				sm_syslog(LOG_INFO, e->e_id,		\
384445f2479Sjbeck 					  "Milter: %s=%s, reject=421, errormode=4",	\
385445f2479Sjbeck 					  str, addr);			\
386445f2479Sjbeck 				LogUsrErrs = false;			\
387445f2479Sjbeck 			}						\
388445f2479Sjbeck 			{						\
389445f2479Sjbeck 				bool tsave = QuickAbort;		\
390445f2479Sjbeck 									\
391445f2479Sjbeck 				QuickAbort = false;			\
392445f2479Sjbeck 				usrerr("421 4.3.0 closing connection");	\
393445f2479Sjbeck 				QuickAbort = tsave;			\
394445f2479Sjbeck 				e->e_sendqueue = NULL;			\
395445f2479Sjbeck 				goto doquit;				\
396445f2479Sjbeck 			}						\
397445f2479Sjbeck 			break;						\
3987c478bd9Sstevel@tonic-gate 		  case SMFIR_REPLYCODE:					\
3997c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)				\
4007c478bd9Sstevel@tonic-gate 			{						\
4017c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,		\
4027c478bd9Sstevel@tonic-gate 					  "Milter: %s=%s, reject=%s",	\
4037c478bd9Sstevel@tonic-gate 					  str, addr, response);		\
4047c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;			\
4057c478bd9Sstevel@tonic-gate 			}						\
40649218d4fSjbeck 			if (strncmp(response, "421 ", 4) == 0		\
40749218d4fSjbeck 			    || strncmp(response, "421-", 4) == 0)	\
4087c478bd9Sstevel@tonic-gate 			{						\
4097c478bd9Sstevel@tonic-gate 				bool tsave = QuickAbort;		\
4107c478bd9Sstevel@tonic-gate 									\
4117c478bd9Sstevel@tonic-gate 				QuickAbort = false;			\
4127c478bd9Sstevel@tonic-gate 				usrerr(response);			\
4137c478bd9Sstevel@tonic-gate 				QuickAbort = tsave;			\
4147c478bd9Sstevel@tonic-gate 				e->e_sendqueue = NULL;			\
4157c478bd9Sstevel@tonic-gate 				goto doquit;				\
4167c478bd9Sstevel@tonic-gate 			}						\
4177c478bd9Sstevel@tonic-gate 			else						\
4187c478bd9Sstevel@tonic-gate 				usrerr(response);			\
4197c478bd9Sstevel@tonic-gate 			break;						\
4207c478bd9Sstevel@tonic-gate 									\
4217c478bd9Sstevel@tonic-gate 		  case SMFIR_REJECT:					\
4227c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)				\
4237c478bd9Sstevel@tonic-gate 			{						\
4247c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,		\
4257c478bd9Sstevel@tonic-gate 					  "Milter: %s=%s, reject=550 5.7.1 Command rejected", \
4267c478bd9Sstevel@tonic-gate 					  str, addr);			\
4277c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;			\
4287c478bd9Sstevel@tonic-gate 			}						\
4297c478bd9Sstevel@tonic-gate 			usrerr("550 5.7.1 Command rejected");		\
4307c478bd9Sstevel@tonic-gate 			break;						\
4317c478bd9Sstevel@tonic-gate 									\
4327c478bd9Sstevel@tonic-gate 		  case SMFIR_DISCARD:					\
4337c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)				\
4347c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,		\
4357c478bd9Sstevel@tonic-gate 					  "Milter: %s=%s, discard",	\
4367c478bd9Sstevel@tonic-gate 					  str, addr);			\
4377c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_DISCARD;			\
4384aac33d3Sjbeck 			milter_cmd_fail = false;			\
4397c478bd9Sstevel@tonic-gate 			break;						\
4407c478bd9Sstevel@tonic-gate 									\
4417c478bd9Sstevel@tonic-gate 		  case SMFIR_TEMPFAIL:					\
4427c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)				\
4437c478bd9Sstevel@tonic-gate 			{						\
4447c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,		\
4457c478bd9Sstevel@tonic-gate 					  "Milter: %s=%s, reject=%s",	\
4467c478bd9Sstevel@tonic-gate 					  str, addr, MSG_TEMPFAIL);	\
4477c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;			\
4487c478bd9Sstevel@tonic-gate 			}						\
4497c478bd9Sstevel@tonic-gate 			usrerr(MSG_TEMPFAIL);				\
4507c478bd9Sstevel@tonic-gate 			break;						\
4514aac33d3Sjbeck 		  default:						\
4524aac33d3Sjbeck 			milter_cmd_fail = false;			\
4534aac33d3Sjbeck 			break;						\
4547c478bd9Sstevel@tonic-gate 		}							\
4557c478bd9Sstevel@tonic-gate 		LogUsrErrs = savelogusrerrs;				\
4567c478bd9Sstevel@tonic-gate 		if (response != NULL)					\
4577c478bd9Sstevel@tonic-gate 			sm_free(response); /* XXX */			\
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate #else /* MILTER */
4617c478bd9Sstevel@tonic-gate # define MILTER_ABORT(e)
4627c478bd9Sstevel@tonic-gate #endif /* MILTER */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate /* clear all SMTP state (for HELO/EHLO/RSET) */
4657c478bd9Sstevel@tonic-gate #define CLEAR_STATE(cmd)					\
4667c478bd9Sstevel@tonic-gate do								\
4677c478bd9Sstevel@tonic-gate {								\
4687c478bd9Sstevel@tonic-gate 	/* abort milter filters */				\
4697c478bd9Sstevel@tonic-gate 	MILTER_ABORT(e);					\
4707c478bd9Sstevel@tonic-gate 								\
4717c478bd9Sstevel@tonic-gate 	if (smtp.sm_nrcpts > 0)					\
4727c478bd9Sstevel@tonic-gate 	{							\
4737c478bd9Sstevel@tonic-gate 		logundelrcpts(e, cmd, 10, false);		\
4747c478bd9Sstevel@tonic-gate 		smtp.sm_nrcpts = 0;				\
4757c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM,			\
4767c478bd9Sstevel@tonic-gate 			  macid("{nrcpts}"), "0");		\
4777c478bd9Sstevel@tonic-gate 	}							\
4787c478bd9Sstevel@tonic-gate 								\
4797c478bd9Sstevel@tonic-gate 	e->e_sendqueue = NULL;					\
4807c478bd9Sstevel@tonic-gate 	e->e_flags |= EF_CLRQUEUE;				\
4817c478bd9Sstevel@tonic-gate 								\
482*e9af4bc0SJohn Beck 	if (tTd(92, 2))						\
483*e9af4bc0SJohn Beck 		sm_dprintf("CLEAR_STATE: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",\
484*e9af4bc0SJohn Beck 			e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);\
4857c478bd9Sstevel@tonic-gate 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))	\
4867c478bd9Sstevel@tonic-gate 		logsender(e, NULL);				\
4877c478bd9Sstevel@tonic-gate 	e->e_flags &= ~EF_LOGSENDER;				\
4887c478bd9Sstevel@tonic-gate 								\
4897c478bd9Sstevel@tonic-gate 	/* clean up a bit */					\
4907c478bd9Sstevel@tonic-gate 	smtp.sm_gotmail = false;				\
4917c478bd9Sstevel@tonic-gate 	SuprErrs = true;					\
492*e9af4bc0SJohn Beck 	(void) dropenvelope(e, true, false);			\
4937c478bd9Sstevel@tonic-gate 	sm_rpool_free(e->e_rpool);				\
4947c478bd9Sstevel@tonic-gate 	e = newenvelope(e, CurEnv, sm_rpool_new_x(NULL));	\
4957c478bd9Sstevel@tonic-gate 	CurEnv = e;						\
496058561cbSjbeck 	e->e_features = features;				\
4977c478bd9Sstevel@tonic-gate 								\
4987c478bd9Sstevel@tonic-gate 	/* put back discard bit */				\
4997c478bd9Sstevel@tonic-gate 	if (smtp.sm_discard)					\
5007c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_DISCARD;			\
5017c478bd9Sstevel@tonic-gate 								\
5027c478bd9Sstevel@tonic-gate 	/* restore connection quarantining */			\
5037c478bd9Sstevel@tonic-gate 	if (smtp.sm_quarmsg == NULL)				\
5047c478bd9Sstevel@tonic-gate 	{							\
5057c478bd9Sstevel@tonic-gate 		e->e_quarmsg = NULL;				\
5067c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM,			\
5077c478bd9Sstevel@tonic-gate 			macid("{quarantine}"), "");		\
5087c478bd9Sstevel@tonic-gate 	}							\
5097c478bd9Sstevel@tonic-gate 	else							\
5107c478bd9Sstevel@tonic-gate 	{							\
5117c478bd9Sstevel@tonic-gate 		e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool,	\
5127c478bd9Sstevel@tonic-gate 						smtp.sm_quarmsg);	\
5137c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{quarantine}"),	\
5147c478bd9Sstevel@tonic-gate 			  e->e_quarmsg);			\
5157c478bd9Sstevel@tonic-gate 	}							\
5167c478bd9Sstevel@tonic-gate } while (0)
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate /* sleep to flatten out connection load */
5197c478bd9Sstevel@tonic-gate #define MIN_DELAY_LOG	15	/* wait before logging this again */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /* is it worth setting the process title for 1s? */
5227c478bd9Sstevel@tonic-gate #define DELAY_CONN(cmd)						\
5237c478bd9Sstevel@tonic-gate 	if (DelayLA > 0 && (CurrentLA = getla()) >= DelayLA)	\
5247c478bd9Sstevel@tonic-gate 	{							\
5257c478bd9Sstevel@tonic-gate 		time_t dnow;					\
5267c478bd9Sstevel@tonic-gate 								\
5277c478bd9Sstevel@tonic-gate 		sm_setproctitle(true, e,			\
5287c478bd9Sstevel@tonic-gate 				"%s: %s: delaying %s: load average: %d", \
5297c478bd9Sstevel@tonic-gate 				qid_printname(e), CurSmtpClient,	\
5307c478bd9Sstevel@tonic-gate 				cmd, DelayLA);	\
5317c478bd9Sstevel@tonic-gate 		if (LogLevel > 8 && (dnow = curtime()) > log_delay)	\
5327c478bd9Sstevel@tonic-gate 		{						\
5337c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id,		\
5347c478bd9Sstevel@tonic-gate 				  "delaying=%s, load average=%d >= %d",	\
5357c478bd9Sstevel@tonic-gate 				  cmd, CurrentLA, DelayLA);		\
5367c478bd9Sstevel@tonic-gate 			log_delay = dnow + MIN_DELAY_LOG;	\
5377c478bd9Sstevel@tonic-gate 		}						\
5387c478bd9Sstevel@tonic-gate 		(void) sleep(1);				\
5397c478bd9Sstevel@tonic-gate 		sm_setproctitle(true, e, "%s %s: %.80s",	\
5407c478bd9Sstevel@tonic-gate 				qid_printname(e), CurSmtpClient, inp);	\
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
543058561cbSjbeck static bool SevenBitInput_Saved;	/* saved version of SevenBitInput */
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate void
smtp(nullserver,d_flags,e)5467c478bd9Sstevel@tonic-gate smtp(nullserver, d_flags, e)
5477c478bd9Sstevel@tonic-gate 	char *volatile nullserver;
5487c478bd9Sstevel@tonic-gate 	BITMAP256 d_flags;
5497c478bd9Sstevel@tonic-gate 	register ENVELOPE *volatile e;
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	register char *volatile p;
5527c478bd9Sstevel@tonic-gate 	register struct cmd *volatile c = NULL;
5537c478bd9Sstevel@tonic-gate 	char *cmd;
5547c478bd9Sstevel@tonic-gate 	auto ADDRESS *vrfyqueue;
5557c478bd9Sstevel@tonic-gate 	ADDRESS *a;
5567c478bd9Sstevel@tonic-gate 	volatile bool gothello;		/* helo command received */
5577c478bd9Sstevel@tonic-gate 	bool vrfy;			/* set if this is a vrfy command */
5587c478bd9Sstevel@tonic-gate 	char *volatile protocol;	/* sending protocol */
5597c478bd9Sstevel@tonic-gate 	char *volatile sendinghost;	/* sending hostname */
5607c478bd9Sstevel@tonic-gate 	char *volatile peerhostname;	/* name of SMTP peer or "localhost" */
5617c478bd9Sstevel@tonic-gate 	auto char *delimptr;
5627c478bd9Sstevel@tonic-gate 	char *id;
5637c478bd9Sstevel@tonic-gate 	volatile unsigned int n_badcmds = 0;	/* count of bad commands */
5647c478bd9Sstevel@tonic-gate 	volatile unsigned int n_badrcpts = 0;	/* number of rejected RCPT */
5657c478bd9Sstevel@tonic-gate 	volatile unsigned int n_verifies = 0;	/* count of VRFY/EXPN */
5667c478bd9Sstevel@tonic-gate 	volatile unsigned int n_etrn = 0;	/* count of ETRN */
5677c478bd9Sstevel@tonic-gate 	volatile unsigned int n_noop = 0;	/* count of NOOP/VERB/etc */
5687c478bd9Sstevel@tonic-gate 	volatile unsigned int n_helo = 0;	/* count of HELO/EHLO */
5697c478bd9Sstevel@tonic-gate 	bool ok;
5707c478bd9Sstevel@tonic-gate 	volatile bool first;
5717c478bd9Sstevel@tonic-gate 	volatile bool tempfail = false;
5727c478bd9Sstevel@tonic-gate 	volatile time_t wt;		/* timeout after too many commands */
5737c478bd9Sstevel@tonic-gate 	volatile time_t previous;	/* time after checksmtpattack() */
5747c478bd9Sstevel@tonic-gate 	volatile bool lognullconnection = true;
5757c478bd9Sstevel@tonic-gate 	register char *q;
5767c478bd9Sstevel@tonic-gate 	SMTP_T smtp;
5777c478bd9Sstevel@tonic-gate 	char *addr;
5787c478bd9Sstevel@tonic-gate 	char *greetcode = "220";
5797c478bd9Sstevel@tonic-gate 	char *hostname;			/* my hostname ($j) */
5807c478bd9Sstevel@tonic-gate 	QUEUE_CHAR *new;
5817c478bd9Sstevel@tonic-gate 	char *args[MAXSMTPARGS];
582058561cbSjbeck 	char inp[MAXINPLINE];
583058561cbSjbeck #if MAXINPLINE < MAXLINE
584058561cbSjbeck  ERROR _MAXINPLINE must NOT be less than _MAXLINE: MAXINPLINE < MAXLINE
585058561cbSjbeck #endif /* MAXINPLINE < MAXLINE */
5867c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXLINE];
5877c478bd9Sstevel@tonic-gate #if SASL
5887c478bd9Sstevel@tonic-gate 	sasl_conn_t *conn;
5897c478bd9Sstevel@tonic-gate 	volatile bool sasl_ok;
5907c478bd9Sstevel@tonic-gate 	volatile unsigned int n_auth = 0;	/* count of AUTH commands */
5917c478bd9Sstevel@tonic-gate 	bool ismore;
5927c478bd9Sstevel@tonic-gate 	int result;
5937c478bd9Sstevel@tonic-gate 	volatile int authenticating;
5947c478bd9Sstevel@tonic-gate 	char *user;
5957c478bd9Sstevel@tonic-gate 	char *in, *out2;
5967c478bd9Sstevel@tonic-gate # if SASL >= 20000
597058561cbSjbeck 	char *auth_id = NULL;
5987c478bd9Sstevel@tonic-gate 	const char *out;
5997c478bd9Sstevel@tonic-gate 	sasl_ssf_t ext_ssf;
6007c478bd9Sstevel@tonic-gate 	char localip[60], remoteip[60];
6017c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
6027c478bd9Sstevel@tonic-gate 	char *out;
6037c478bd9Sstevel@tonic-gate 	const char *errstr;
6047c478bd9Sstevel@tonic-gate 	sasl_external_properties_t ext_ssf;
6057c478bd9Sstevel@tonic-gate 	struct sockaddr_in saddr_l;
6067c478bd9Sstevel@tonic-gate 	struct sockaddr_in saddr_r;
6077c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
6087c478bd9Sstevel@tonic-gate 	sasl_security_properties_t ssp;
6097c478bd9Sstevel@tonic-gate 	sasl_ssf_t *ssf;
6107c478bd9Sstevel@tonic-gate 	unsigned int inlen, out2len;
6117c478bd9Sstevel@tonic-gate 	unsigned int outlen;
6127c478bd9Sstevel@tonic-gate 	char *volatile auth_type;
6137c478bd9Sstevel@tonic-gate 	char *mechlist;
6147c478bd9Sstevel@tonic-gate 	volatile unsigned int n_mechs;
6157c478bd9Sstevel@tonic-gate 	unsigned int len;
616058561cbSjbeck #else /* SASL */
6177c478bd9Sstevel@tonic-gate #endif /* SASL */
6187c478bd9Sstevel@tonic-gate 	int r;
6197c478bd9Sstevel@tonic-gate #if STARTTLS
6207c478bd9Sstevel@tonic-gate 	int rfd, wfd;
6217c478bd9Sstevel@tonic-gate 	volatile bool tls_active = false;
6227c478bd9Sstevel@tonic-gate 	volatile bool smtps = bitnset(D_SMTPS, d_flags);
6237c478bd9Sstevel@tonic-gate 	bool saveQuickAbort;
6247c478bd9Sstevel@tonic-gate 	bool saveSuprErrs;
6257c478bd9Sstevel@tonic-gate 	time_t tlsstart;
6267c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
6277c478bd9Sstevel@tonic-gate 	volatile unsigned int features;
6287c478bd9Sstevel@tonic-gate #if PIPELINING
6297c478bd9Sstevel@tonic-gate # if _FFR_NO_PIPE
6307c478bd9Sstevel@tonic-gate 	int np_log = 0;
6317c478bd9Sstevel@tonic-gate # endif /* _FFR_NO_PIPE */
6327c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
6337c478bd9Sstevel@tonic-gate 	volatile time_t log_delay = (time_t) 0;
634058561cbSjbeck #if MILTER
635058561cbSjbeck 	volatile bool milter_cmd_done, milter_cmd_safe;
6364aac33d3Sjbeck 	volatile bool milter_rcpt_added, milter_cmd_fail;
637058561cbSjbeck 	ADDRESS addr_st;
638058561cbSjbeck # define p_addr_st	&addr_st
639058561cbSjbeck #else /* MILTER */
640058561cbSjbeck # define p_addr_st	NULL
641058561cbSjbeck #endif /* MILTER */
642058561cbSjbeck 	size_t inplen;
643d4660949Sjbeck #if _FFR_BADRCPT_SHUTDOWN
644d4660949Sjbeck 	int n_badrcpts_adj;
645d4660949Sjbeck #endif /* _FFR_BADRCPT_SHUTDOWN */
6467c478bd9Sstevel@tonic-gate 
647058561cbSjbeck 	SevenBitInput_Saved = SevenBitInput;
6487c478bd9Sstevel@tonic-gate 	smtp.sm_nrcpts = 0;
6497c478bd9Sstevel@tonic-gate #if MILTER
6507c478bd9Sstevel@tonic-gate 	smtp.sm_milterize = (nullserver == NULL);
6517c478bd9Sstevel@tonic-gate 	smtp.sm_milterlist = false;
652058561cbSjbeck 	addr = NULL;
6537c478bd9Sstevel@tonic-gate #endif /* MILTER */
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/* setup I/O fd correctly for the SMTP server */
6567c478bd9Sstevel@tonic-gate 	setup_smtpd_io();
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
6597c478bd9Sstevel@tonic-gate 	if (sm_debug_active(&DebugLeakSmtp, 1))
6607c478bd9Sstevel@tonic-gate 	{
6617c478bd9Sstevel@tonic-gate 		sm_heap_newgroup();
6627c478bd9Sstevel@tonic-gate 		sm_dprintf("smtp() heap group #%d\n", sm_heap_group());
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/* XXX the rpool should be set when e is initialized in main() */
6677c478bd9Sstevel@tonic-gate 	e->e_rpool = sm_rpool_new_x(NULL);
6687c478bd9Sstevel@tonic-gate 	e->e_macro.mac_rpool = e->e_rpool;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	settime(e);
6717c478bd9Sstevel@tonic-gate 	sm_getla();
6727c478bd9Sstevel@tonic-gate 	peerhostname = RealHostName;
6737c478bd9Sstevel@tonic-gate 	if (peerhostname == NULL)
6747c478bd9Sstevel@tonic-gate 		peerhostname = "localhost";
6757c478bd9Sstevel@tonic-gate 	CurHostName = peerhostname;
6767c478bd9Sstevel@tonic-gate 	CurSmtpClient = macvalue('_', e);
6777c478bd9Sstevel@tonic-gate 	if (CurSmtpClient == NULL)
6787c478bd9Sstevel@tonic-gate 		CurSmtpClient = CurHostName;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/* check_relay may have set discard bit, save for later */
6817c478bd9Sstevel@tonic-gate 	smtp.sm_discard = bitset(EF_DISCARD, e->e_flags);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate #if PIPELINING
6847c478bd9Sstevel@tonic-gate 	/* auto-flush output when reading input */
6857c478bd9Sstevel@tonic-gate 	(void) sm_io_autoflush(InChannel, OutChannel);
6867c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	sm_setproctitle(true, e, "server %s startup", CurSmtpClient);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/* Set default features for server. */
6917c478bd9Sstevel@tonic-gate 	features = ((bitset(PRIV_NOETRN, PrivacyFlags) ||
6927c478bd9Sstevel@tonic-gate 		     bitnset(D_NOETRN, d_flags)) ? SRV_NONE : SRV_OFFER_ETRN)
6937c478bd9Sstevel@tonic-gate 		| (bitnset(D_AUTHREQ, d_flags) ? SRV_REQ_AUTH : SRV_NONE)
6947c478bd9Sstevel@tonic-gate 		| (bitset(PRIV_NOEXPN, PrivacyFlags) ? SRV_NONE
6957c478bd9Sstevel@tonic-gate 			: (SRV_OFFER_EXPN
6967c478bd9Sstevel@tonic-gate 			  | (bitset(PRIV_NOVERB, PrivacyFlags)
6977c478bd9Sstevel@tonic-gate 			     ? SRV_NONE : SRV_OFFER_VERB)))
6983ee0e492Sjbeck 		| ((bitset(PRIV_NORECEIPTS, PrivacyFlags) || !SendMIMEErrors)
6993ee0e492Sjbeck 			 ? SRV_NONE : SRV_OFFER_DSN)
7007c478bd9Sstevel@tonic-gate #if SASL
7017c478bd9Sstevel@tonic-gate 		| (bitnset(D_NOAUTH, d_flags) ? SRV_NONE : SRV_OFFER_AUTH)
7027c478bd9Sstevel@tonic-gate 		| (bitset(SASL_SEC_NOPLAINTEXT, SASLOpts) ? SRV_REQ_SEC
7037c478bd9Sstevel@tonic-gate 							  : SRV_NONE)
7047c478bd9Sstevel@tonic-gate #endif /* SASL */
7057c478bd9Sstevel@tonic-gate #if PIPELINING
7067c478bd9Sstevel@tonic-gate 		| SRV_OFFER_PIPE
7077c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
7087c478bd9Sstevel@tonic-gate #if STARTTLS
7097c478bd9Sstevel@tonic-gate 		| (bitnset(D_NOTLS, d_flags) ? SRV_NONE : SRV_OFFER_TLS)
7107c478bd9Sstevel@tonic-gate 		| (bitset(TLS_I_NO_VRFY, TLS_Srv_Opts) ? SRV_NONE
7117c478bd9Sstevel@tonic-gate 						       : SRV_VRFY_CLT)
7127c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
7137c478bd9Sstevel@tonic-gate 		;
7147c478bd9Sstevel@tonic-gate 	if (nullserver == NULL)
7157c478bd9Sstevel@tonic-gate 	{
7167c478bd9Sstevel@tonic-gate 		features = srvfeatures(e, CurSmtpClient, features);
7177c478bd9Sstevel@tonic-gate 		if (bitset(SRV_TMP_FAIL, features))
7187c478bd9Sstevel@tonic-gate 		{
7197c478bd9Sstevel@tonic-gate 			if (LogLevel > 4)
7207c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_ERR, NOQID,
7217c478bd9Sstevel@tonic-gate 					  "ERROR: srv_features=tempfail, relay=%.100s, access temporarily disabled",
7227c478bd9Sstevel@tonic-gate 					  CurSmtpClient);
7237c478bd9Sstevel@tonic-gate 			nullserver = "450 4.3.0 Please try again later.";
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 		else
7267c478bd9Sstevel@tonic-gate 		{
7277c478bd9Sstevel@tonic-gate #if PIPELINING
7287c478bd9Sstevel@tonic-gate # if _FFR_NO_PIPE
7297c478bd9Sstevel@tonic-gate 			if (bitset(SRV_NO_PIPE, features))
7307c478bd9Sstevel@tonic-gate 			{
7317c478bd9Sstevel@tonic-gate 				/* for consistency */
7327c478bd9Sstevel@tonic-gate 				features &= ~SRV_OFFER_PIPE;
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate # endif /* _FFR_NO_PIPE */
7357c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
7367c478bd9Sstevel@tonic-gate #if SASL
7377c478bd9Sstevel@tonic-gate 			if (bitset(SRV_REQ_SEC, features))
7387c478bd9Sstevel@tonic-gate 				SASLOpts |= SASL_SEC_NOPLAINTEXT;
7397c478bd9Sstevel@tonic-gate 			else
7407c478bd9Sstevel@tonic-gate 				SASLOpts &= ~SASL_SEC_NOPLAINTEXT;
7417c478bd9Sstevel@tonic-gate #endif /* SASL */
7427c478bd9Sstevel@tonic-gate 		}
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	else if (strncmp(nullserver, "421 ", 4) == 0)
7457c478bd9Sstevel@tonic-gate 	{
7467c478bd9Sstevel@tonic-gate 		message(nullserver);
7477c478bd9Sstevel@tonic-gate 		goto doquit;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
750058561cbSjbeck 	e->e_features = features;
7517c478bd9Sstevel@tonic-gate 	hostname = macvalue('j', e);
7527c478bd9Sstevel@tonic-gate #if SASL
7537c478bd9Sstevel@tonic-gate 	if (AuthRealm == NULL)
7547c478bd9Sstevel@tonic-gate 		AuthRealm = hostname;
7557c478bd9Sstevel@tonic-gate 	sasl_ok = bitset(SRV_OFFER_AUTH, features);
7567c478bd9Sstevel@tonic-gate 	n_mechs = 0;
7577c478bd9Sstevel@tonic-gate 	authenticating = SASL_NOT_AUTH;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/* SASL server new connection */
7607c478bd9Sstevel@tonic-gate 	if (sasl_ok)
7617c478bd9Sstevel@tonic-gate 	{
7627c478bd9Sstevel@tonic-gate # if SASL >= 20000
7637c478bd9Sstevel@tonic-gate 		result = sasl_server_new("smtp", AuthRealm, NULL, NULL, NULL,
7647c478bd9Sstevel@tonic-gate 					 NULL, 0, &conn);
7657c478bd9Sstevel@tonic-gate # elif SASL > 10505
7667c478bd9Sstevel@tonic-gate 		/* use empty realm: only works in SASL > 1.5.5 */
7677c478bd9Sstevel@tonic-gate 		result = sasl_server_new("smtp", AuthRealm, "", NULL, 0, &conn);
7687c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
7697c478bd9Sstevel@tonic-gate 		/* use no realm -> realm is set to hostname by SASL lib */
7707c478bd9Sstevel@tonic-gate 		result = sasl_server_new("smtp", AuthRealm, NULL, NULL, 0,
7717c478bd9Sstevel@tonic-gate 					 &conn);
7727c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
7737c478bd9Sstevel@tonic-gate 		sasl_ok = result == SASL_OK;
7747c478bd9Sstevel@tonic-gate 		if (!sasl_ok)
7757c478bd9Sstevel@tonic-gate 		{
7767c478bd9Sstevel@tonic-gate 			if (LogLevel > 9)
7777c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
7787c478bd9Sstevel@tonic-gate 					  "AUTH error: sasl_server_new failed=%d",
7797c478bd9Sstevel@tonic-gate 					  result);
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	if (sasl_ok)
7837c478bd9Sstevel@tonic-gate 	{
7847c478bd9Sstevel@tonic-gate 		/*
7857c478bd9Sstevel@tonic-gate 		**  SASL set properties for sasl
7867c478bd9Sstevel@tonic-gate 		**  set local/remote IP
7877c478bd9Sstevel@tonic-gate 		**  XXX Cyrus SASL v1 only supports IPv4
7887c478bd9Sstevel@tonic-gate 		**
7897c478bd9Sstevel@tonic-gate 		**  XXX where exactly are these used/required?
7907c478bd9Sstevel@tonic-gate 		**  Kerberos_v4
7917c478bd9Sstevel@tonic-gate 		*/
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate # if SASL >= 20000
7947c478bd9Sstevel@tonic-gate 		localip[0] = remoteip[0] = '\0';
7957c478bd9Sstevel@tonic-gate #  if NETINET || NETINET6
7967c478bd9Sstevel@tonic-gate 		in = macvalue(macid("{daemon_family}"), e);
7977c478bd9Sstevel@tonic-gate 		if (in != NULL && (
7987c478bd9Sstevel@tonic-gate #   if NETINET6
7997c478bd9Sstevel@tonic-gate 		    strcmp(in, "inet6") == 0 ||
8007c478bd9Sstevel@tonic-gate #   endif /* NETINET6 */
8017c478bd9Sstevel@tonic-gate 		    strcmp(in, "inet") == 0))
8027c478bd9Sstevel@tonic-gate 		{
8037c478bd9Sstevel@tonic-gate 			SOCKADDR_LEN_T addrsize;
8047c478bd9Sstevel@tonic-gate 			SOCKADDR saddr_l;
8057c478bd9Sstevel@tonic-gate 			SOCKADDR saddr_r;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			addrsize = sizeof(saddr_r);
8087c478bd9Sstevel@tonic-gate 			if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
8097c478bd9Sstevel@tonic-gate 						      NULL),
8107c478bd9Sstevel@tonic-gate 					(struct sockaddr *) &saddr_r,
8117c478bd9Sstevel@tonic-gate 					&addrsize) == 0)
8127c478bd9Sstevel@tonic-gate 			{
8137c478bd9Sstevel@tonic-gate 				if (iptostring(&saddr_r, addrsize,
814058561cbSjbeck 					       remoteip, sizeof(remoteip)))
8157c478bd9Sstevel@tonic-gate 				{
8167c478bd9Sstevel@tonic-gate 					sasl_setprop(conn, SASL_IPREMOTEPORT,
8177c478bd9Sstevel@tonic-gate 						     remoteip);
8187c478bd9Sstevel@tonic-gate 				}
8197c478bd9Sstevel@tonic-gate 				addrsize = sizeof(saddr_l);
8207c478bd9Sstevel@tonic-gate 				if (getsockname(sm_io_getinfo(InChannel,
8217c478bd9Sstevel@tonic-gate 							      SM_IO_WHAT_FD,
8227c478bd9Sstevel@tonic-gate 							      NULL),
8237c478bd9Sstevel@tonic-gate 						(struct sockaddr *) &saddr_l,
8247c478bd9Sstevel@tonic-gate 						&addrsize) == 0)
8257c478bd9Sstevel@tonic-gate 				{
8267c478bd9Sstevel@tonic-gate 					if (iptostring(&saddr_l, addrsize,
8277c478bd9Sstevel@tonic-gate 						       localip,
828058561cbSjbeck 						       sizeof(localip)))
8297c478bd9Sstevel@tonic-gate 					{
8307c478bd9Sstevel@tonic-gate 						sasl_setprop(conn,
8317c478bd9Sstevel@tonic-gate 							     SASL_IPLOCALPORT,
8327c478bd9Sstevel@tonic-gate 							     localip);
8337c478bd9Sstevel@tonic-gate 					}
8347c478bd9Sstevel@tonic-gate 				}
8357c478bd9Sstevel@tonic-gate 			}
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate #  endif /* NETINET || NETINET6 */
8387c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
8397c478bd9Sstevel@tonic-gate #  if NETINET
8407c478bd9Sstevel@tonic-gate 		in = macvalue(macid("{daemon_family}"), e);
8417c478bd9Sstevel@tonic-gate 		if (in != NULL && strcmp(in, "inet") == 0)
8427c478bd9Sstevel@tonic-gate 		{
8437c478bd9Sstevel@tonic-gate 			SOCKADDR_LEN_T addrsize;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			addrsize = sizeof(struct sockaddr_in);
8467c478bd9Sstevel@tonic-gate 			if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
8477c478bd9Sstevel@tonic-gate 						      NULL),
8487c478bd9Sstevel@tonic-gate 					(struct sockaddr *)&saddr_r,
8497c478bd9Sstevel@tonic-gate 					&addrsize) == 0)
8507c478bd9Sstevel@tonic-gate 			{
8517c478bd9Sstevel@tonic-gate 				sasl_setprop(conn, SASL_IP_REMOTE, &saddr_r);
8527c478bd9Sstevel@tonic-gate 				addrsize = sizeof(struct sockaddr_in);
8537c478bd9Sstevel@tonic-gate 				if (getsockname(sm_io_getinfo(InChannel,
8547c478bd9Sstevel@tonic-gate 							      SM_IO_WHAT_FD,
8557c478bd9Sstevel@tonic-gate 							      NULL),
8567c478bd9Sstevel@tonic-gate 						(struct sockaddr *)&saddr_l,
8577c478bd9Sstevel@tonic-gate 						&addrsize) == 0)
8587c478bd9Sstevel@tonic-gate 					sasl_setprop(conn, SASL_IP_LOCAL,
8597c478bd9Sstevel@tonic-gate 						     &saddr_l);
8607c478bd9Sstevel@tonic-gate 			}
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate #  endif /* NETINET */
8637c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		auth_type = NULL;
8667c478bd9Sstevel@tonic-gate 		mechlist = NULL;
8677c478bd9Sstevel@tonic-gate 		user = NULL;
8687c478bd9Sstevel@tonic-gate # if 0
8697c478bd9Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_PERM,
8707c478bd9Sstevel@tonic-gate 			macid("{auth_author}"), NULL);
8717c478bd9Sstevel@tonic-gate # endif /* 0 */
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/* set properties */
874058561cbSjbeck 		(void) memset(&ssp, '\0', sizeof(ssp));
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		/* XXX should these be options settable via .cf ? */
8777c478bd9Sstevel@tonic-gate 		/* ssp.min_ssf = 0; is default due to memset() */
8787c478bd9Sstevel@tonic-gate 		{
8797c478bd9Sstevel@tonic-gate 			ssp.max_ssf = MaxSLBits;
8807c478bd9Sstevel@tonic-gate 			ssp.maxbufsize = MAXOUTLEN;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		ssp.security_flags = SASLOpts & SASL_SEC_MASK;
8837c478bd9Sstevel@tonic-gate 		sasl_ok = sasl_setprop(conn, SASL_SEC_PROPS, &ssp) == SASL_OK;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if (sasl_ok)
8867c478bd9Sstevel@tonic-gate 		{
8877c478bd9Sstevel@tonic-gate 			/*
8887c478bd9Sstevel@tonic-gate 			**  external security strength factor;
8897c478bd9Sstevel@tonic-gate 			**	currently we have none so zero
8907c478bd9Sstevel@tonic-gate 			*/
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate # if SASL >= 20000
8937c478bd9Sstevel@tonic-gate 			ext_ssf = 0;
8947c478bd9Sstevel@tonic-gate 			auth_id = NULL;
8957c478bd9Sstevel@tonic-gate 			sasl_ok = ((sasl_setprop(conn, SASL_SSF_EXTERNAL,
8967c478bd9Sstevel@tonic-gate 						 &ext_ssf) == SASL_OK) &&
8977c478bd9Sstevel@tonic-gate 				   (sasl_setprop(conn, SASL_AUTH_EXTERNAL,
8987c478bd9Sstevel@tonic-gate 						 auth_id) == SASL_OK));
8997c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
9007c478bd9Sstevel@tonic-gate 			ext_ssf.ssf = 0;
9017c478bd9Sstevel@tonic-gate 			ext_ssf.auth_id = NULL;
9027c478bd9Sstevel@tonic-gate 			sasl_ok = sasl_setprop(conn, SASL_SSF_EXTERNAL,
9037c478bd9Sstevel@tonic-gate 					       &ext_ssf) == SASL_OK;
9047c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
9057c478bd9Sstevel@tonic-gate 		}
9067c478bd9Sstevel@tonic-gate 		if (sasl_ok)
9077c478bd9Sstevel@tonic-gate 			n_mechs = saslmechs(conn, &mechlist);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate #endif /* SASL */
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate #if STARTTLS
912*e9af4bc0SJohn Beck # if USE_OPENSSL_ENGINE
913*e9af4bc0SJohn Beck 	if (tls_ok_srv && bitset(SRV_OFFER_TLS, features) &&
914*e9af4bc0SJohn Beck 	    !SSL_set_engine(NULL))
915*e9af4bc0SJohn Beck 	{
916*e9af4bc0SJohn Beck 		sm_syslog(LOG_ERR, NOQID,
917*e9af4bc0SJohn Beck 			  "STARTTLS=server, SSL_set_engine=failed");
918*e9af4bc0SJohn Beck 		tls_ok_srv = false;
919*e9af4bc0SJohn Beck 	}
920*e9af4bc0SJohn Beck # endif /* USE_OPENSSL_ENGINE */
921*e9af4bc0SJohn Beck 
922d4660949Sjbeck 
923d4660949Sjbeck 	set_tls_rd_tmo(TimeOuts.to_nextcommand);
9247c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate #if MILTER
9277c478bd9Sstevel@tonic-gate 	if (smtp.sm_milterize)
9287c478bd9Sstevel@tonic-gate 	{
9297c478bd9Sstevel@tonic-gate 		char state;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/* initialize mail filter connection */
9327800901eSjbeck 		smtp.sm_milterlist = milter_init(e, &state, &smtp.sm_milters);
9337c478bd9Sstevel@tonic-gate 		switch (state)
9347c478bd9Sstevel@tonic-gate 		{
9357c478bd9Sstevel@tonic-gate 		  case SMFIR_REJECT:
9367c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
9377c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
9387c478bd9Sstevel@tonic-gate 					  "Milter: initialization failed, rejecting commands");
9397c478bd9Sstevel@tonic-gate 			greetcode = "554";
9407c478bd9Sstevel@tonic-gate 			nullserver = "Command rejected";
9417c478bd9Sstevel@tonic-gate 			smtp.sm_milterize = false;
9427c478bd9Sstevel@tonic-gate 			break;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		  case SMFIR_TEMPFAIL:
9457c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
9467c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
9477c478bd9Sstevel@tonic-gate 					  "Milter: initialization failed, temp failing commands");
9487c478bd9Sstevel@tonic-gate 			tempfail = true;
9497c478bd9Sstevel@tonic-gate 			smtp.sm_milterize = false;
9507c478bd9Sstevel@tonic-gate 			break;
951445f2479Sjbeck 
952445f2479Sjbeck 		  case SMFIR_SHUTDOWN:
953445f2479Sjbeck 			if (MilterLogLevel > 3)
954445f2479Sjbeck 				sm_syslog(LOG_INFO, e->e_id,
955445f2479Sjbeck 					  "Milter: initialization failed, closing connection");
956445f2479Sjbeck 			tempfail = true;
957445f2479Sjbeck 			smtp.sm_milterize = false;
958445f2479Sjbeck 			message("421 4.7.0 %s closing connection",
959445f2479Sjbeck 					MyHostName);
960445f2479Sjbeck 
961445f2479Sjbeck 			/* arrange to ignore send list */
962445f2479Sjbeck 			e->e_sendqueue = NULL;
963d4660949Sjbeck 			lognullconnection = false;
964445f2479Sjbeck 			goto doquit;
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (smtp.sm_milterlist && smtp.sm_milterize &&
9697c478bd9Sstevel@tonic-gate 	    !bitset(EF_DISCARD, e->e_flags))
9707c478bd9Sstevel@tonic-gate 	{
9717c478bd9Sstevel@tonic-gate 		char state;
9727c478bd9Sstevel@tonic-gate 		char *response;
9737c478bd9Sstevel@tonic-gate 
9741daa5768Sjbeck 		q = macvalue(macid("{client_name}"), e);
975058561cbSjbeck 		SM_ASSERT(q != NULL || OpMode == MD_SMTP);
976058561cbSjbeck 		if (q == NULL)
977058561cbSjbeck 			q = "localhost";
9781daa5768Sjbeck 		response = milter_connect(q, RealHostAddr, e, &state);
9797c478bd9Sstevel@tonic-gate 		switch (state)
9807c478bd9Sstevel@tonic-gate 		{
9817c478bd9Sstevel@tonic-gate 		  case SMFIR_REPLYCODE:	/* REPLYCODE shouldn't happen */
9827c478bd9Sstevel@tonic-gate 		  case SMFIR_REJECT:
9837c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
9847c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
9857c478bd9Sstevel@tonic-gate 					  "Milter: connect: host=%s, addr=%s, rejecting commands",
9867c478bd9Sstevel@tonic-gate 					  peerhostname,
9877c478bd9Sstevel@tonic-gate 					  anynet_ntoa(&RealHostAddr));
9887c478bd9Sstevel@tonic-gate 			greetcode = "554";
9897c478bd9Sstevel@tonic-gate 			nullserver = "Command rejected";
9907c478bd9Sstevel@tonic-gate 			smtp.sm_milterize = false;
9917c478bd9Sstevel@tonic-gate 			break;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		  case SMFIR_TEMPFAIL:
9947c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
9957c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
9967c478bd9Sstevel@tonic-gate 					  "Milter: connect: host=%s, addr=%s, temp failing commands",
9977c478bd9Sstevel@tonic-gate 					  peerhostname,
9987c478bd9Sstevel@tonic-gate 					  anynet_ntoa(&RealHostAddr));
9997c478bd9Sstevel@tonic-gate 			tempfail = true;
10007c478bd9Sstevel@tonic-gate 			smtp.sm_milterize = false;
10017c478bd9Sstevel@tonic-gate 			break;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		  case SMFIR_SHUTDOWN:
10047c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
10057c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
10067c478bd9Sstevel@tonic-gate 					  "Milter: connect: host=%s, addr=%s, shutdown",
10077c478bd9Sstevel@tonic-gate 					  peerhostname,
10087c478bd9Sstevel@tonic-gate 					  anynet_ntoa(&RealHostAddr));
10097c478bd9Sstevel@tonic-gate 			tempfail = true;
10107c478bd9Sstevel@tonic-gate 			smtp.sm_milterize = false;
10117c478bd9Sstevel@tonic-gate 			message("421 4.7.0 %s closing connection",
10127c478bd9Sstevel@tonic-gate 					MyHostName);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 			/* arrange to ignore send list */
10157c478bd9Sstevel@tonic-gate 			e->e_sendqueue = NULL;
10167c478bd9Sstevel@tonic-gate 			goto doquit;
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		if (response != NULL)
10197c478bd9Sstevel@tonic-gate 			sm_free(response); /* XXX */
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate #endif /* MILTER */
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	**  Broken proxies and SMTP slammers
10257c478bd9Sstevel@tonic-gate 	**  push data without waiting, catch them
10267c478bd9Sstevel@tonic-gate 	*/
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (
10297c478bd9Sstevel@tonic-gate #if STARTTLS
10307c478bd9Sstevel@tonic-gate 	    !smtps &&
10317c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
1032058561cbSjbeck 	    *greetcode == '2' && nullserver == NULL)
10337c478bd9Sstevel@tonic-gate 	{
10347c478bd9Sstevel@tonic-gate 		time_t msecs = 0;
10357c478bd9Sstevel@tonic-gate 		char **pvp;
10367c478bd9Sstevel@tonic-gate 		char pvpbuf[PSBUFSIZE];
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		/* Ask the rulesets how long to pause */
10397c478bd9Sstevel@tonic-gate 		pvp = NULL;
10407c478bd9Sstevel@tonic-gate 		r = rscap("greet_pause", peerhostname,
10417c478bd9Sstevel@tonic-gate 			  anynet_ntoa(&RealHostAddr), e,
10427c478bd9Sstevel@tonic-gate 			  &pvp, pvpbuf, sizeof(pvpbuf));
10437c478bd9Sstevel@tonic-gate 		if (r == EX_OK && pvp != NULL && pvp[0] != NULL &&
10447c478bd9Sstevel@tonic-gate 		    (pvp[0][0] & 0377) == CANONNET && pvp[1] != NULL)
10457c478bd9Sstevel@tonic-gate 		{
10467c478bd9Sstevel@tonic-gate 			msecs = strtol(pvp[1], NULL, 10);
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		if (msecs > 0)
10507c478bd9Sstevel@tonic-gate 		{
10517c478bd9Sstevel@tonic-gate 			int fd;
10527c478bd9Sstevel@tonic-gate 			fd_set readfds;
10537c478bd9Sstevel@tonic-gate 			struct timeval timeout;
105449218d4fSjbeck 			struct timeval bp, ep, tp; /* {begin,end,total}pause */
1055058561cbSjbeck 			int eoftest;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 			/* pause for a moment */
10587c478bd9Sstevel@tonic-gate 			timeout.tv_sec = msecs / 1000;
10597c478bd9Sstevel@tonic-gate 			timeout.tv_usec = (msecs % 1000) * 1000;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 			/* Obey RFC 2821: 4.3.5.2: 220 timeout of 5 minutes */
10627c478bd9Sstevel@tonic-gate 			if (timeout.tv_sec >= 300)
10637c478bd9Sstevel@tonic-gate 			{
10647c478bd9Sstevel@tonic-gate 				timeout.tv_sec = 300;
10657c478bd9Sstevel@tonic-gate 				timeout.tv_usec = 0;
10667c478bd9Sstevel@tonic-gate 			}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 			/* check if data is on the socket during the pause */
10697c478bd9Sstevel@tonic-gate 			fd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
10707c478bd9Sstevel@tonic-gate 			FD_ZERO(&readfds);
10717c478bd9Sstevel@tonic-gate 			SM_FD_SET(fd, &readfds);
107249218d4fSjbeck 			gettimeofday(&bp, NULL);
10737c478bd9Sstevel@tonic-gate 			if (select(fd + 1, FDSET_CAST &readfds,
10747c478bd9Sstevel@tonic-gate 			    NULL, NULL, &timeout) > 0 &&
1075058561cbSjbeck 			    FD_ISSET(fd, &readfds) &&
1076058561cbSjbeck 			    (eoftest = sm_io_getc(InChannel, SM_TIME_DEFAULT))
1077058561cbSjbeck 			    != SM_IO_EOF)
10787c478bd9Sstevel@tonic-gate 			{
1079058561cbSjbeck 				sm_io_ungetc(InChannel, SM_TIME_DEFAULT,
1080058561cbSjbeck 					     eoftest);
108149218d4fSjbeck 				gettimeofday(&ep, NULL);
108249218d4fSjbeck 				timersub(&ep, &bp, &tp);
10837c478bd9Sstevel@tonic-gate 				greetcode = "554";
10847c478bd9Sstevel@tonic-gate 				nullserver = "Command rejected";
10857c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
1086058561cbSjbeck 					  "rejecting commands from %s [%s] due to pre-greeting traffic after %d seconds",
10877c478bd9Sstevel@tonic-gate 					  peerhostname,
1088058561cbSjbeck 					  anynet_ntoa(&RealHostAddr),
1089058561cbSjbeck 					  (int) tp.tv_sec +
109049218d4fSjbeck 						(tp.tv_usec >= 500000 ? 1 : 0)
109149218d4fSjbeck 					 );
10927c478bd9Sstevel@tonic-gate 			}
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate #if STARTTLS
10977c478bd9Sstevel@tonic-gate 	/* If this an smtps connection, start TLS now */
10987c478bd9Sstevel@tonic-gate 	if (smtps)
10997c478bd9Sstevel@tonic-gate 	{
11007c478bd9Sstevel@tonic-gate 		Errors = 0;
11017c478bd9Sstevel@tonic-gate 		goto starttls;
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate   greeting:
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* output the first line, inserting "ESMTP" as second word */
11097c478bd9Sstevel@tonic-gate 	if (*greetcode == '5')
1110058561cbSjbeck 		(void) sm_snprintf(inp, sizeof(inp),
1111058561cbSjbeck 				"%s not accepting messages", hostname);
11127c478bd9Sstevel@tonic-gate 	else
1113058561cbSjbeck 		expand(SmtpGreeting, inp, sizeof(inp), e);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	p = strchr(inp, '\n');
11167c478bd9Sstevel@tonic-gate 	if (p != NULL)
11177c478bd9Sstevel@tonic-gate 		*p++ = '\0';
11187c478bd9Sstevel@tonic-gate 	id = strchr(inp, ' ');
11197c478bd9Sstevel@tonic-gate 	if (id == NULL)
11207c478bd9Sstevel@tonic-gate 		id = &inp[strlen(inp)];
11217c478bd9Sstevel@tonic-gate 	if (p == NULL)
1122058561cbSjbeck 		(void) sm_snprintf(cmdbuf, sizeof(cmdbuf),
11237c478bd9Sstevel@tonic-gate 			 "%s %%.*s ESMTP%%s", greetcode);
11247c478bd9Sstevel@tonic-gate 	else
1125058561cbSjbeck 		(void) sm_snprintf(cmdbuf, sizeof(cmdbuf),
11267c478bd9Sstevel@tonic-gate 			 "%s-%%.*s ESMTP%%s", greetcode);
11277c478bd9Sstevel@tonic-gate 	message(cmdbuf, (int) (id - inp), inp, id);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/* output remaining lines */
11307c478bd9Sstevel@tonic-gate 	while ((id = p) != NULL && (p = strchr(id, '\n')) != NULL)
11317c478bd9Sstevel@tonic-gate 	{
11327c478bd9Sstevel@tonic-gate 		*p++ = '\0';
11337c478bd9Sstevel@tonic-gate 		if (isascii(*id) && isspace(*id))
11347c478bd9Sstevel@tonic-gate 			id++;
1135058561cbSjbeck 		(void) sm_strlcpyn(cmdbuf, sizeof(cmdbuf), 2, greetcode, "-%s");
11367c478bd9Sstevel@tonic-gate 		message(cmdbuf, id);
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	if (id != NULL)
11397c478bd9Sstevel@tonic-gate 	{
11407c478bd9Sstevel@tonic-gate 		if (isascii(*id) && isspace(*id))
11417c478bd9Sstevel@tonic-gate 			id++;
1142058561cbSjbeck 		(void) sm_strlcpyn(cmdbuf, sizeof(cmdbuf), 2, greetcode, " %s");
11437c478bd9Sstevel@tonic-gate 		message(cmdbuf, id);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	protocol = NULL;
11477c478bd9Sstevel@tonic-gate 	sendinghost = macvalue('s', e);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/* If quarantining by a connect/ehlo action, save between messages */
11507c478bd9Sstevel@tonic-gate 	if (e->e_quarmsg == NULL)
11517c478bd9Sstevel@tonic-gate 		smtp.sm_quarmsg = NULL;
11527c478bd9Sstevel@tonic-gate 	else
11537c478bd9Sstevel@tonic-gate 		smtp.sm_quarmsg = newstr(e->e_quarmsg);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/* sendinghost's storage must outlive the current envelope */
11567c478bd9Sstevel@tonic-gate 	if (sendinghost != NULL)
11577c478bd9Sstevel@tonic-gate 		sendinghost = sm_strdup_x(sendinghost);
11587c478bd9Sstevel@tonic-gate 	first = true;
11597c478bd9Sstevel@tonic-gate 	gothello = false;
11607c478bd9Sstevel@tonic-gate 	smtp.sm_gotmail = false;
11617c478bd9Sstevel@tonic-gate 	for (;;)
11627c478bd9Sstevel@tonic-gate 	{
11637c478bd9Sstevel@tonic-gate 	    SM_TRY
11647c478bd9Sstevel@tonic-gate 	    {
11657c478bd9Sstevel@tonic-gate 		QuickAbort = false;
11667c478bd9Sstevel@tonic-gate 		HoldErrs = false;
11677c478bd9Sstevel@tonic-gate 		SuprErrs = false;
11687c478bd9Sstevel@tonic-gate 		LogUsrErrs = false;
11697c478bd9Sstevel@tonic-gate 		OnlyOneError = true;
11707c478bd9Sstevel@tonic-gate 		e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS);
11714aac33d3Sjbeck #if MILTER
11724aac33d3Sjbeck 		milter_cmd_fail = false;
11734aac33d3Sjbeck #endif /* MILTER */
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		/* setup for the read */
11767c478bd9Sstevel@tonic-gate 		e->e_to = NULL;
11777c478bd9Sstevel@tonic-gate 		Errors = 0;
11787c478bd9Sstevel@tonic-gate 		FileName = NULL;
11797c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		/* read the input line */
11827c478bd9Sstevel@tonic-gate 		SmtpPhase = "server cmd read";
11837c478bd9Sstevel@tonic-gate 		sm_setproctitle(true, e, "server %s cmd read", CurSmtpClient);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		/* handle errors */
11867c478bd9Sstevel@tonic-gate 		if (sm_io_error(OutChannel) ||
1187058561cbSjbeck 		    (p = sfgets(inp, sizeof(inp), InChannel,
11887c478bd9Sstevel@tonic-gate 				TimeOuts.to_nextcommand, SmtpPhase)) == NULL)
11897c478bd9Sstevel@tonic-gate 		{
11907c478bd9Sstevel@tonic-gate 			char *d;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			d = macvalue(macid("{daemon_name}"), e);
11937c478bd9Sstevel@tonic-gate 			if (d == NULL)
11947c478bd9Sstevel@tonic-gate 				d = "stdin";
11957c478bd9Sstevel@tonic-gate 			/* end of file, just die */
11967c478bd9Sstevel@tonic-gate 			disconnect(1, e);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate #if MILTER
11997c478bd9Sstevel@tonic-gate 			/* close out milter filters */
12007c478bd9Sstevel@tonic-gate 			milter_quit(e);
12017c478bd9Sstevel@tonic-gate #endif /* MILTER */
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 			message("421 4.4.1 %s Lost input channel from %s",
12047c478bd9Sstevel@tonic-gate 				MyHostName, CurSmtpClient);
12057c478bd9Sstevel@tonic-gate 			if (LogLevel > (smtp.sm_gotmail ? 1 : 19))
12067c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_NOTICE, e->e_id,
12077c478bd9Sstevel@tonic-gate 					  "lost input channel from %s to %s after %s",
12087c478bd9Sstevel@tonic-gate 					  CurSmtpClient, d,
12097c478bd9Sstevel@tonic-gate 					  (c == NULL || c->cmd_name == NULL) ? "startup" : c->cmd_name);
12107c478bd9Sstevel@tonic-gate 			/*
12117c478bd9Sstevel@tonic-gate 			**  If have not accepted mail (DATA), do not bounce
12127c478bd9Sstevel@tonic-gate 			**  bad addresses back to sender.
12137c478bd9Sstevel@tonic-gate 			*/
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 			if (bitset(EF_CLRQUEUE, e->e_flags))
12167c478bd9Sstevel@tonic-gate 				e->e_sendqueue = NULL;
12177c478bd9Sstevel@tonic-gate 			goto doquit;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 
1220058561cbSjbeck 		/* also used by "proxy" check below */
1221058561cbSjbeck 		inplen = strlen(inp);
1222058561cbSjbeck #if SASL
1223058561cbSjbeck 		/*
1224058561cbSjbeck 		**  SMTP AUTH requires accepting any length,
1225058561cbSjbeck 		**  at least for challenge/response. However, not imposing
1226058561cbSjbeck 		**  a limit is a bad idea (denial of service).
1227058561cbSjbeck 		*/
1228058561cbSjbeck 
1229058561cbSjbeck 		if (authenticating != SASL_PROC_AUTH
1230058561cbSjbeck 		    && sm_strncasecmp(inp, "AUTH ", 5) != 0
1231058561cbSjbeck 		    && inplen > MAXLINE)
1232058561cbSjbeck 		{
1233058561cbSjbeck 			message("421 4.7.0 %s Command too long, possible attack %s",
1234058561cbSjbeck 				MyHostName, CurSmtpClient);
1235058561cbSjbeck 			sm_syslog(LOG_INFO, e->e_id,
1236058561cbSjbeck 				  "%s: SMTP violation, input too long: %lu",
1237058561cbSjbeck 				  CurSmtpClient, (unsigned long) inplen);
1238058561cbSjbeck 			goto doquit;
1239058561cbSjbeck 		}
1240058561cbSjbeck #endif /* SASL */
1241058561cbSjbeck 
12427c478bd9Sstevel@tonic-gate 		if (first)
12437c478bd9Sstevel@tonic-gate 		{
1244058561cbSjbeck 			size_t cmdlen;
12457c478bd9Sstevel@tonic-gate 			int idx;
12467c478bd9Sstevel@tonic-gate 			char *http_cmd;
12477c478bd9Sstevel@tonic-gate 			static char *http_cmds[] = { "GET", "POST",
12487c478bd9Sstevel@tonic-gate 						     "CONNECT", "USER", NULL };
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			for (idx = 0; (http_cmd = http_cmds[idx]) != NULL;
12517c478bd9Sstevel@tonic-gate 			     idx++)
12527c478bd9Sstevel@tonic-gate 			{
12537c478bd9Sstevel@tonic-gate 				cmdlen = strlen(http_cmd);
12547c478bd9Sstevel@tonic-gate 				if (cmdlen < inplen &&
12557c478bd9Sstevel@tonic-gate 				    sm_strncasecmp(inp, http_cmd, cmdlen) == 0 &&
12567c478bd9Sstevel@tonic-gate 				    isascii(inp[cmdlen]) && isspace(inp[cmdlen]))
12577c478bd9Sstevel@tonic-gate 				{
12587c478bd9Sstevel@tonic-gate 					/* Open proxy, drop it */
12597c478bd9Sstevel@tonic-gate 					message("421 4.7.0 %s Rejecting open proxy %s",
12607c478bd9Sstevel@tonic-gate 						MyHostName, CurSmtpClient);
12617c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
12627c478bd9Sstevel@tonic-gate 						  "%s: probable open proxy: command=%.40s",
12637c478bd9Sstevel@tonic-gate 						  CurSmtpClient, inp);
12647c478bd9Sstevel@tonic-gate 					goto doquit;
12657c478bd9Sstevel@tonic-gate 				}
12667c478bd9Sstevel@tonic-gate 			}
12677c478bd9Sstevel@tonic-gate 			first = false;
12687c478bd9Sstevel@tonic-gate 		}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 		/* clean up end of line */
12717c478bd9Sstevel@tonic-gate 		fixcrlf(inp, true);
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate #if PIPELINING
12747c478bd9Sstevel@tonic-gate # if _FFR_NO_PIPE
12757c478bd9Sstevel@tonic-gate 		/*
12767c478bd9Sstevel@tonic-gate 		**  if there is more input and pipelining is disabled:
12777c478bd9Sstevel@tonic-gate 		**	delay ... (and maybe discard the input?)
12787c478bd9Sstevel@tonic-gate 		**  XXX this doesn't really work, at least in tests using
12797c478bd9Sstevel@tonic-gate 		**  telnet SM_IO_IS_READABLE only returns 1 if there were
12807c478bd9Sstevel@tonic-gate 		**  more than 2 input lines available.
12817c478bd9Sstevel@tonic-gate 		*/
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 		if (bitset(SRV_NO_PIPE, features) &&
12847c478bd9Sstevel@tonic-gate 		    sm_io_getinfo(InChannel, SM_IO_IS_READABLE, NULL) > 0)
12857c478bd9Sstevel@tonic-gate 		{
12867c478bd9Sstevel@tonic-gate 			if (++np_log < 3)
12877c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, NOQID,
1288*e9af4bc0SJohn Beck 					  "unauthorized PIPELINING, sleeping, relay=%.100s",
1289*e9af4bc0SJohn Beck 					   CurSmtpClient);
12907c478bd9Sstevel@tonic-gate 			sleep(1);
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate # endif /* _FFR_NO_PIPE */
12947c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate #if SASL
12977c478bd9Sstevel@tonic-gate 		if (authenticating == SASL_PROC_AUTH)
12987c478bd9Sstevel@tonic-gate 		{
12997c478bd9Sstevel@tonic-gate # if 0
13007c478bd9Sstevel@tonic-gate 			if (*inp == '\0')
13017c478bd9Sstevel@tonic-gate 			{
13027c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
13037c478bd9Sstevel@tonic-gate 				message("501 5.5.2 missing input");
13047c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
13057c478bd9Sstevel@tonic-gate 				continue;
13067c478bd9Sstevel@tonic-gate 			}
13077c478bd9Sstevel@tonic-gate # endif /* 0 */
13087c478bd9Sstevel@tonic-gate 			if (*inp == '*' && *(inp + 1) == '\0')
13097c478bd9Sstevel@tonic-gate 			{
13107c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
13117c478bd9Sstevel@tonic-gate 
13127800901eSjbeck 				/* RFC 2554 4. */
13137c478bd9Sstevel@tonic-gate 				message("501 5.0.0 AUTH aborted");
13147c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
13157c478bd9Sstevel@tonic-gate 				continue;
13167c478bd9Sstevel@tonic-gate 			}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 			/* could this be shorter? XXX */
13197c478bd9Sstevel@tonic-gate # if SASL >= 20000
13207c478bd9Sstevel@tonic-gate 			in = xalloc(strlen(inp) + 1);
13217c478bd9Sstevel@tonic-gate 			result = sasl_decode64(inp, strlen(inp), in,
13227c478bd9Sstevel@tonic-gate 					       strlen(inp), &inlen);
13237c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
13247c478bd9Sstevel@tonic-gate 			out = xalloc(strlen(inp));
13257c478bd9Sstevel@tonic-gate 			result = sasl_decode64(inp, strlen(inp), out, &outlen);
13267c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
13277c478bd9Sstevel@tonic-gate 			if (result != SASL_OK)
13287c478bd9Sstevel@tonic-gate 			{
13297c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
13307c478bd9Sstevel@tonic-gate 
13317800901eSjbeck 				/* RFC 2554 4. */
13327c478bd9Sstevel@tonic-gate 				message("501 5.5.4 cannot decode AUTH parameter %s",
13337c478bd9Sstevel@tonic-gate 					inp);
13347c478bd9Sstevel@tonic-gate # if SASL >= 20000
13357c478bd9Sstevel@tonic-gate 				sm_free(in);
13367c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
13377c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
13387c478bd9Sstevel@tonic-gate 				continue;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate # if SASL >= 20000
13427c478bd9Sstevel@tonic-gate 			result = sasl_server_step(conn,	in, inlen,
13437c478bd9Sstevel@tonic-gate 						  &out, &outlen);
13447c478bd9Sstevel@tonic-gate 			sm_free(in);
13457c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
13467c478bd9Sstevel@tonic-gate 			result = sasl_server_step(conn,	out, outlen,
13477c478bd9Sstevel@tonic-gate 						  &out, &outlen, &errstr);
13487c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 			/* get an OK if we're done */
13517c478bd9Sstevel@tonic-gate 			if (result == SASL_OK)
13527c478bd9Sstevel@tonic-gate 			{
13537c478bd9Sstevel@tonic-gate   authenticated:
13547c478bd9Sstevel@tonic-gate 				message("235 2.0.0 OK Authenticated");
13557c478bd9Sstevel@tonic-gate 				authenticating = SASL_IS_AUTH;
13567c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_TEMP,
13577c478bd9Sstevel@tonic-gate 					macid("{auth_type}"), auth_type);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate # if SASL >= 20000
13607c478bd9Sstevel@tonic-gate 				user = macvalue(macid("{auth_authen}"), e);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 				/* get security strength (features) */
13637c478bd9Sstevel@tonic-gate 				result = sasl_getprop(conn, SASL_SSF,
13647c478bd9Sstevel@tonic-gate 						      (const void **) &ssf);
13657c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
13667c478bd9Sstevel@tonic-gate 				result = sasl_getprop(conn, SASL_USERNAME,
13677c478bd9Sstevel@tonic-gate 						      (void **)&user);
13687c478bd9Sstevel@tonic-gate 				if (result != SASL_OK)
13697c478bd9Sstevel@tonic-gate 				{
13707c478bd9Sstevel@tonic-gate 					user = "";
13717c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
13727c478bd9Sstevel@tonic-gate 						  A_PERM,
13737c478bd9Sstevel@tonic-gate 						  macid("{auth_authen}"), NULL);
13747c478bd9Sstevel@tonic-gate 				}
13757c478bd9Sstevel@tonic-gate 				else
13767c478bd9Sstevel@tonic-gate 				{
13777c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
13787c478bd9Sstevel@tonic-gate 						  A_TEMP,
13797c478bd9Sstevel@tonic-gate 						  macid("{auth_authen}"),
13807c478bd9Sstevel@tonic-gate 						  xtextify(user, "<>\")"));
13817c478bd9Sstevel@tonic-gate 				}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate # if 0
13847c478bd9Sstevel@tonic-gate 				/* get realm? */
13857c478bd9Sstevel@tonic-gate 				sasl_getprop(conn, SASL_REALM, (void **) &data);
13867c478bd9Sstevel@tonic-gate # endif /* 0 */
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 				/* get security strength (features) */
13897c478bd9Sstevel@tonic-gate 				result = sasl_getprop(conn, SASL_SSF,
13907c478bd9Sstevel@tonic-gate 						      (void **) &ssf);
13917c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
13927c478bd9Sstevel@tonic-gate 				if (result != SASL_OK)
13937c478bd9Sstevel@tonic-gate 				{
13947c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
13957c478bd9Sstevel@tonic-gate 						  A_PERM,
13967c478bd9Sstevel@tonic-gate 						  macid("{auth_ssf}"), "0");
13977c478bd9Sstevel@tonic-gate 					ssf = NULL;
13987c478bd9Sstevel@tonic-gate 				}
13997c478bd9Sstevel@tonic-gate 				else
14007c478bd9Sstevel@tonic-gate 				{
14017c478bd9Sstevel@tonic-gate 					char pbuf[8];
14027c478bd9Sstevel@tonic-gate 
1403058561cbSjbeck 					(void) sm_snprintf(pbuf, sizeof(pbuf),
14047c478bd9Sstevel@tonic-gate 							   "%u", *ssf);
14057c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
14067c478bd9Sstevel@tonic-gate 						  A_TEMP,
14077c478bd9Sstevel@tonic-gate 						  macid("{auth_ssf}"), pbuf);
14087c478bd9Sstevel@tonic-gate 					if (tTd(95, 8))
14097c478bd9Sstevel@tonic-gate 						sm_dprintf("AUTH auth_ssf: %u\n",
14107c478bd9Sstevel@tonic-gate 							   *ssf);
14117c478bd9Sstevel@tonic-gate 				}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 				/*
14147c478bd9Sstevel@tonic-gate 				**  Only switch to encrypted connection
14157c478bd9Sstevel@tonic-gate 				**  if a security layer has been negotiated
14167c478bd9Sstevel@tonic-gate 				*/
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 				if (ssf != NULL && *ssf > 0)
14197c478bd9Sstevel@tonic-gate 				{
14203ee0e492Sjbeck 					int tmo;
14213ee0e492Sjbeck 
14227c478bd9Sstevel@tonic-gate 					/*
14237c478bd9Sstevel@tonic-gate 					**  Convert I/O layer to use SASL.
14247c478bd9Sstevel@tonic-gate 					**  If the call fails, the connection
14257c478bd9Sstevel@tonic-gate 					**  is aborted.
14267c478bd9Sstevel@tonic-gate 					*/
14277c478bd9Sstevel@tonic-gate 
14283ee0e492Sjbeck 					tmo = TimeOuts.to_datablock * 1000;
14297c478bd9Sstevel@tonic-gate 					if (sfdcsasl(&InChannel, &OutChannel,
14303ee0e492Sjbeck 						     conn, tmo) == 0)
14317c478bd9Sstevel@tonic-gate 					{
14327c478bd9Sstevel@tonic-gate 						/* restart dialogue */
14337c478bd9Sstevel@tonic-gate 						n_helo = 0;
14347c478bd9Sstevel@tonic-gate # if PIPELINING
14357c478bd9Sstevel@tonic-gate 						(void) sm_io_autoflush(InChannel,
14367c478bd9Sstevel@tonic-gate 								       OutChannel);
14377c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
14387c478bd9Sstevel@tonic-gate 					}
14397c478bd9Sstevel@tonic-gate 					else
14407c478bd9Sstevel@tonic-gate 						syserr("503 5.3.3 SASL TLS failed");
14417c478bd9Sstevel@tonic-gate 				}
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 				/* NULL pointer ok since it's our function */
14447c478bd9Sstevel@tonic-gate 				if (LogLevel > 8)
14457c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, NOQID,
14467c478bd9Sstevel@tonic-gate 						  "AUTH=server, relay=%s, authid=%.128s, mech=%.16s, bits=%d",
14477c478bd9Sstevel@tonic-gate 						  CurSmtpClient,
14487c478bd9Sstevel@tonic-gate 						  shortenstring(user, 128),
14497c478bd9Sstevel@tonic-gate 						  auth_type, *ssf);
14507c478bd9Sstevel@tonic-gate 			}
14517c478bd9Sstevel@tonic-gate 			else if (result == SASL_CONTINUE)
14527c478bd9Sstevel@tonic-gate 			{
14537c478bd9Sstevel@tonic-gate 				len = ENC64LEN(outlen);
14547c478bd9Sstevel@tonic-gate 				out2 = xalloc(len);
14557c478bd9Sstevel@tonic-gate 				result = sasl_encode64(out, outlen, out2, len,
14567c478bd9Sstevel@tonic-gate 						       &out2len);
14577c478bd9Sstevel@tonic-gate 				if (result != SASL_OK)
14587c478bd9Sstevel@tonic-gate 				{
14597c478bd9Sstevel@tonic-gate 					/* correct code? XXX */
14607c478bd9Sstevel@tonic-gate 					/* 454 Temp. authentication failure */
14617c478bd9Sstevel@tonic-gate 					message("454 4.5.4 Internal error: unable to encode64");
14627c478bd9Sstevel@tonic-gate 					if (LogLevel > 5)
14637c478bd9Sstevel@tonic-gate 						sm_syslog(LOG_WARNING, e->e_id,
1464*e9af4bc0SJohn Beck 							  "AUTH encode64 error [%d for \"%s\"], relay=%.100s",
1465*e9af4bc0SJohn Beck 							  result, out,
1466*e9af4bc0SJohn Beck 							  CurSmtpClient);
14677c478bd9Sstevel@tonic-gate 					/* start over? */
14687c478bd9Sstevel@tonic-gate 					authenticating = SASL_NOT_AUTH;
14697c478bd9Sstevel@tonic-gate 				}
14707c478bd9Sstevel@tonic-gate 				else
14717c478bd9Sstevel@tonic-gate 				{
14727c478bd9Sstevel@tonic-gate 					message("334 %s", out2);
14737c478bd9Sstevel@tonic-gate 					if (tTd(95, 2))
14747c478bd9Sstevel@tonic-gate 						sm_dprintf("AUTH continue: msg='%s' len=%u\n",
14757c478bd9Sstevel@tonic-gate 							   out2, out2len);
14767c478bd9Sstevel@tonic-gate 				}
14777c478bd9Sstevel@tonic-gate # if SASL >= 20000
14787c478bd9Sstevel@tonic-gate 				sm_free(out2);
14797c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 			else
14827c478bd9Sstevel@tonic-gate 			{
14837c478bd9Sstevel@tonic-gate 				/* not SASL_OK or SASL_CONT */
14847c478bd9Sstevel@tonic-gate 				message("535 5.7.0 authentication failed");
14857c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
14867c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, e->e_id,
1487*e9af4bc0SJohn Beck 						  "AUTH failure (%s): %s (%d) %s, relay=%.100s",
14887c478bd9Sstevel@tonic-gate 						  auth_type,
14897c478bd9Sstevel@tonic-gate 						  sasl_errstring(result, NULL,
14907c478bd9Sstevel@tonic-gate 								 NULL),
14917c478bd9Sstevel@tonic-gate 						  result,
14927c478bd9Sstevel@tonic-gate # if SASL >= 20000
1493*e9af4bc0SJohn Beck 						  sasl_errdetail(conn),
14947c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
1495*e9af4bc0SJohn Beck 						  errstr == NULL ? "" : errstr,
14967c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
1497*e9af4bc0SJohn Beck 						  CurSmtpClient);
14987c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
14997c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
15007c478bd9Sstevel@tonic-gate 			}
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 		else
15037c478bd9Sstevel@tonic-gate 		{
15047c478bd9Sstevel@tonic-gate 			/* don't want to do any of this if authenticating */
15057c478bd9Sstevel@tonic-gate #endif /* SASL */
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 		/* echo command to transcript */
15087c478bd9Sstevel@tonic-gate 		if (e->e_xfp != NULL)
15097c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
15107c478bd9Sstevel@tonic-gate 					     "<<< %s\n", inp);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		if (LogLevel > 14)
15137c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id, "<-- %s", inp);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		/* break off command */
15167c478bd9Sstevel@tonic-gate 		for (p = inp; isascii(*p) && isspace(*p); p++)
15177c478bd9Sstevel@tonic-gate 			continue;
15187c478bd9Sstevel@tonic-gate 		cmd = cmdbuf;
15197c478bd9Sstevel@tonic-gate 		while (*p != '\0' &&
15207c478bd9Sstevel@tonic-gate 		       !(isascii(*p) && isspace(*p)) &&
1521058561cbSjbeck 		       cmd < &cmdbuf[sizeof(cmdbuf) - 2])
15227c478bd9Sstevel@tonic-gate 			*cmd++ = *p++;
15237c478bd9Sstevel@tonic-gate 		*cmd = '\0';
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		/* throw away leading whitespace */
15267c478bd9Sstevel@tonic-gate 		SKIP_SPACE(p);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		/* decode command */
15297c478bd9Sstevel@tonic-gate 		for (c = CmdTab; c->cmd_name != NULL; c++)
15307c478bd9Sstevel@tonic-gate 		{
15317c478bd9Sstevel@tonic-gate 			if (sm_strcasecmp(c->cmd_name, cmdbuf) == 0)
15327c478bd9Sstevel@tonic-gate 				break;
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		/* reset errors */
15367c478bd9Sstevel@tonic-gate 		errno = 0;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 		/* check whether a "non-null" command has been used */
15397c478bd9Sstevel@tonic-gate 		switch (c->cmd_code)
15407c478bd9Sstevel@tonic-gate 		{
15417c478bd9Sstevel@tonic-gate #if SASL
15427c478bd9Sstevel@tonic-gate 		  case CMDAUTH:
15437c478bd9Sstevel@tonic-gate 			/* avoid information leak; take first two words? */
15447c478bd9Sstevel@tonic-gate 			q = "AUTH";
15457c478bd9Sstevel@tonic-gate 			break;
15467c478bd9Sstevel@tonic-gate #endif /* SASL */
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 		  case CMDMAIL:
15497c478bd9Sstevel@tonic-gate 		  case CMDEXPN:
15507c478bd9Sstevel@tonic-gate 		  case CMDVRFY:
15517c478bd9Sstevel@tonic-gate 		  case CMDETRN:
15527c478bd9Sstevel@tonic-gate 			lognullconnection = false;
15537c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
15547c478bd9Sstevel@tonic-gate 		  default:
15557c478bd9Sstevel@tonic-gate 			q = inp;
15567c478bd9Sstevel@tonic-gate 			break;
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 		if (e->e_id == NULL)
15607c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "%s: %.80s",
15617c478bd9Sstevel@tonic-gate 					CurSmtpClient, q);
15627c478bd9Sstevel@tonic-gate 		else
15637c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "%s %s: %.80s",
15647c478bd9Sstevel@tonic-gate 					qid_printname(e),
15657c478bd9Sstevel@tonic-gate 					CurSmtpClient, q);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		/*
15687c478bd9Sstevel@tonic-gate 		**  Process command.
15697c478bd9Sstevel@tonic-gate 		**
15707c478bd9Sstevel@tonic-gate 		**	If we are running as a null server, return 550
15717c478bd9Sstevel@tonic-gate 		**	to almost everything.
15727c478bd9Sstevel@tonic-gate 		*/
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		if (nullserver != NULL || bitnset(D_ETRNONLY, d_flags))
15757c478bd9Sstevel@tonic-gate 		{
15767c478bd9Sstevel@tonic-gate 			switch (c->cmd_code)
15777c478bd9Sstevel@tonic-gate 			{
15787c478bd9Sstevel@tonic-gate 			  case CMDQUIT:
15797c478bd9Sstevel@tonic-gate 			  case CMDHELO:
15807c478bd9Sstevel@tonic-gate 			  case CMDEHLO:
15817c478bd9Sstevel@tonic-gate 			  case CMDNOOP:
15827c478bd9Sstevel@tonic-gate 			  case CMDRSET:
15837c478bd9Sstevel@tonic-gate 			  case CMDERROR:
15847c478bd9Sstevel@tonic-gate 				/* process normally */
15857c478bd9Sstevel@tonic-gate 				break;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 			  case CMDETRN:
15887c478bd9Sstevel@tonic-gate 				if (bitnset(D_ETRNONLY, d_flags) &&
15897c478bd9Sstevel@tonic-gate 				    nullserver == NULL)
15907c478bd9Sstevel@tonic-gate 					break;
15917c478bd9Sstevel@tonic-gate 				DELAY_CONN("ETRN");
15927c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 			  default:
15957c478bd9Sstevel@tonic-gate #if MAXBADCOMMANDS > 0
15967c478bd9Sstevel@tonic-gate 				/* theoretically this could overflow */
15977c478bd9Sstevel@tonic-gate 				if (nullserver != NULL &&
15987c478bd9Sstevel@tonic-gate 				    ++n_badcmds > MAXBADCOMMANDS)
15997c478bd9Sstevel@tonic-gate 				{
16007c478bd9Sstevel@tonic-gate 					message("421 4.7.0 %s Too many bad commands; closing connection",
16017c478bd9Sstevel@tonic-gate 						MyHostName);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 					/* arrange to ignore send list */
16047c478bd9Sstevel@tonic-gate 					e->e_sendqueue = NULL;
16057c478bd9Sstevel@tonic-gate 					goto doquit;
16067c478bd9Sstevel@tonic-gate 				}
16077c478bd9Sstevel@tonic-gate #endif /* MAXBADCOMMANDS > 0 */
16087c478bd9Sstevel@tonic-gate 				if (nullserver != NULL)
16097c478bd9Sstevel@tonic-gate 				{
16107c478bd9Sstevel@tonic-gate 					if (ISSMTPREPLY(nullserver))
16117c478bd9Sstevel@tonic-gate 						usrerr(nullserver);
16127c478bd9Sstevel@tonic-gate 					else
16137c478bd9Sstevel@tonic-gate 						usrerr("550 5.0.0 %s",
16147c478bd9Sstevel@tonic-gate 						       nullserver);
16157c478bd9Sstevel@tonic-gate 				}
16167c478bd9Sstevel@tonic-gate 				else
16177c478bd9Sstevel@tonic-gate 					usrerr("452 4.4.5 Insufficient disk space; try again later");
16187c478bd9Sstevel@tonic-gate 				continue;
16197c478bd9Sstevel@tonic-gate 			}
16207c478bd9Sstevel@tonic-gate 		}
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 		switch (c->cmd_code)
16237c478bd9Sstevel@tonic-gate 		{
16247c478bd9Sstevel@tonic-gate #if SASL
16257c478bd9Sstevel@tonic-gate 		  case CMDAUTH: /* sasl */
16267c478bd9Sstevel@tonic-gate 			DELAY_CONN("AUTH");
16277c478bd9Sstevel@tonic-gate 			if (!sasl_ok || n_mechs <= 0)
16287c478bd9Sstevel@tonic-gate 			{
16297c478bd9Sstevel@tonic-gate 				message("503 5.3.3 AUTH not available");
16307c478bd9Sstevel@tonic-gate 				break;
16317c478bd9Sstevel@tonic-gate 			}
16327c478bd9Sstevel@tonic-gate 			if (authenticating == SASL_IS_AUTH)
16337c478bd9Sstevel@tonic-gate 			{
16347c478bd9Sstevel@tonic-gate 				message("503 5.5.0 Already Authenticated");
16357c478bd9Sstevel@tonic-gate 				break;
16367c478bd9Sstevel@tonic-gate 			}
16377c478bd9Sstevel@tonic-gate 			if (smtp.sm_gotmail)
16387c478bd9Sstevel@tonic-gate 			{
16397c478bd9Sstevel@tonic-gate 				message("503 5.5.0 AUTH not permitted during a mail transaction");
16407c478bd9Sstevel@tonic-gate 				break;
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 			if (tempfail)
16437c478bd9Sstevel@tonic-gate 			{
16447c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
16457c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
16467c478bd9Sstevel@tonic-gate 						  "SMTP AUTH command (%.100s) from %s tempfailed (due to previous checks)",
16477c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
16487c478bd9Sstevel@tonic-gate 				usrerr("454 4.3.0 Please try again later");
16497c478bd9Sstevel@tonic-gate 				break;
16507c478bd9Sstevel@tonic-gate 			}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 			ismore = false;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 			/* crude way to avoid crack attempts */
16557c478bd9Sstevel@tonic-gate 			STOP_IF_ATTACK(checksmtpattack(&n_auth, n_mechs + 1,
16567c478bd9Sstevel@tonic-gate 							true, "AUTH", e));
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 			/* make sure mechanism (p) is a valid string */
16597c478bd9Sstevel@tonic-gate 			for (q = p; *q != '\0' && isascii(*q); q++)
16607c478bd9Sstevel@tonic-gate 			{
16617c478bd9Sstevel@tonic-gate 				if (isspace(*q))
16627c478bd9Sstevel@tonic-gate 				{
16637c478bd9Sstevel@tonic-gate 					*q = '\0';
16647c478bd9Sstevel@tonic-gate 					while (*++q != '\0' &&
16657c478bd9Sstevel@tonic-gate 					       isascii(*q) && isspace(*q))
16667c478bd9Sstevel@tonic-gate 						continue;
16677c478bd9Sstevel@tonic-gate 					*(q - 1) = '\0';
16687c478bd9Sstevel@tonic-gate 					ismore = (*q != '\0');
16697c478bd9Sstevel@tonic-gate 					break;
16707c478bd9Sstevel@tonic-gate 				}
16717c478bd9Sstevel@tonic-gate 			}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 			if (*p == '\0')
16747c478bd9Sstevel@tonic-gate 			{
16757c478bd9Sstevel@tonic-gate 				message("501 5.5.2 AUTH mechanism must be specified");
16767c478bd9Sstevel@tonic-gate 				break;
16777c478bd9Sstevel@tonic-gate 			}
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 			/* check whether mechanism is available */
16807c478bd9Sstevel@tonic-gate 			if (iteminlist(p, mechlist, " ") == NULL)
16817c478bd9Sstevel@tonic-gate 			{
16827c478bd9Sstevel@tonic-gate 				message("504 5.3.3 AUTH mechanism %.32s not available",
16837c478bd9Sstevel@tonic-gate 					p);
16847c478bd9Sstevel@tonic-gate 				break;
16857c478bd9Sstevel@tonic-gate 			}
16867c478bd9Sstevel@tonic-gate 
16877800901eSjbeck 			/*
16887800901eSjbeck 			**  RFC 2554 4.
16897800901eSjbeck 			**  Unlike a zero-length client answer to a
16907800901eSjbeck 			**  334 reply, a zero- length initial response
16917800901eSjbeck 			**  is sent as a single equals sign ("=").
16927800901eSjbeck 			*/
16937800901eSjbeck 
16947800901eSjbeck 			if (ismore && *q == '=' && *(q + 1) == '\0')
16957800901eSjbeck 			{
16967800901eSjbeck 				/* will be free()d, don't use in=""; */
16977800901eSjbeck 				in = xalloc(1);
16987800901eSjbeck 				*in = '\0';
16997800901eSjbeck 				inlen = 0;
17007800901eSjbeck 			}
17017800901eSjbeck 			else if (ismore)
17027c478bd9Sstevel@tonic-gate 			{
17037c478bd9Sstevel@tonic-gate 				/* could this be shorter? XXX */
17047c478bd9Sstevel@tonic-gate # if SASL >= 20000
17057c478bd9Sstevel@tonic-gate 				in = xalloc(strlen(q) + 1);
17067c478bd9Sstevel@tonic-gate 				result = sasl_decode64(q, strlen(q), in,
17077c478bd9Sstevel@tonic-gate 						       strlen(q), &inlen);
17087c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
17097c478bd9Sstevel@tonic-gate 				in = sm_rpool_malloc(e->e_rpool, strlen(q));
17107c478bd9Sstevel@tonic-gate 				result = sasl_decode64(q, strlen(q), in,
17117c478bd9Sstevel@tonic-gate 						       &inlen);
17127c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
17137c478bd9Sstevel@tonic-gate 				if (result != SASL_OK)
17147c478bd9Sstevel@tonic-gate 				{
17157c478bd9Sstevel@tonic-gate 					message("501 5.5.4 cannot BASE64 decode '%s'",
17167c478bd9Sstevel@tonic-gate 						q);
17177c478bd9Sstevel@tonic-gate 					if (LogLevel > 5)
17187c478bd9Sstevel@tonic-gate 						sm_syslog(LOG_WARNING, e->e_id,
1719*e9af4bc0SJohn Beck 							  "AUTH decode64 error [%d for \"%s\"], relay=%.100s",
1720*e9af4bc0SJohn Beck 							  result, q,
1721*e9af4bc0SJohn Beck 							  CurSmtpClient);
17227c478bd9Sstevel@tonic-gate 					/* start over? */
17237c478bd9Sstevel@tonic-gate 					authenticating = SASL_NOT_AUTH;
17247c478bd9Sstevel@tonic-gate # if SASL >= 20000
17257c478bd9Sstevel@tonic-gate 					sm_free(in);
17267c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
17277c478bd9Sstevel@tonic-gate 					in = NULL;
17287c478bd9Sstevel@tonic-gate 					inlen = 0;
17297c478bd9Sstevel@tonic-gate 					break;
17307c478bd9Sstevel@tonic-gate 				}
17317c478bd9Sstevel@tonic-gate 			}
17327c478bd9Sstevel@tonic-gate 			else
17337c478bd9Sstevel@tonic-gate 			{
17347c478bd9Sstevel@tonic-gate 				in = NULL;
17357c478bd9Sstevel@tonic-gate 				inlen = 0;
17367c478bd9Sstevel@tonic-gate 			}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 			/* see if that auth type exists */
17397c478bd9Sstevel@tonic-gate # if SASL >= 20000
17407c478bd9Sstevel@tonic-gate 			result = sasl_server_start(conn, p, in, inlen,
17417c478bd9Sstevel@tonic-gate 						   &out, &outlen);
17427c478bd9Sstevel@tonic-gate 			if (in != NULL)
17437c478bd9Sstevel@tonic-gate 				sm_free(in);
17447c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
17457c478bd9Sstevel@tonic-gate 			result = sasl_server_start(conn, p, in, inlen,
17467c478bd9Sstevel@tonic-gate 						   &out, &outlen, &errstr);
17477c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 			if (result != SASL_OK && result != SASL_CONTINUE)
17507c478bd9Sstevel@tonic-gate 			{
17517c478bd9Sstevel@tonic-gate 				message("535 5.7.0 authentication failed");
17527c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
17537c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_ERR, e->e_id,
1754*e9af4bc0SJohn Beck 						  "AUTH failure (%s): %s (%d) %s, relay=%.100s",
17557c478bd9Sstevel@tonic-gate 						  p,
17567c478bd9Sstevel@tonic-gate 						  sasl_errstring(result, NULL,
17577c478bd9Sstevel@tonic-gate 								 NULL),
17587c478bd9Sstevel@tonic-gate 						  result,
17597c478bd9Sstevel@tonic-gate # if SASL >= 20000
1760*e9af4bc0SJohn Beck 						  sasl_errdetail(conn),
17617c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
1762*e9af4bc0SJohn Beck 						  errstr,
17637c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
1764*e9af4bc0SJohn Beck 						  CurSmtpClient);
17657c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
17667c478bd9Sstevel@tonic-gate 				break;
17677c478bd9Sstevel@tonic-gate 			}
17687c478bd9Sstevel@tonic-gate 			auth_type = newstr(p);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 			if (result == SASL_OK)
17717c478bd9Sstevel@tonic-gate 			{
17727c478bd9Sstevel@tonic-gate 				/* ugly, but same code */
17737c478bd9Sstevel@tonic-gate 				goto authenticated;
17747c478bd9Sstevel@tonic-gate 				/* authenticated by the initial response */
17757c478bd9Sstevel@tonic-gate 			}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			/* len is at least 2 */
17787c478bd9Sstevel@tonic-gate 			len = ENC64LEN(outlen);
17797c478bd9Sstevel@tonic-gate 			out2 = xalloc(len);
17807c478bd9Sstevel@tonic-gate 			result = sasl_encode64(out, outlen, out2, len,
17817c478bd9Sstevel@tonic-gate 					       &out2len);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 			if (result != SASL_OK)
17847c478bd9Sstevel@tonic-gate 			{
17857c478bd9Sstevel@tonic-gate 				message("454 4.5.4 Temporary authentication failure");
17867c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
17877c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, e->e_id,
17887c478bd9Sstevel@tonic-gate 						  "AUTH encode64 error [%d for \"%s\"]",
17897c478bd9Sstevel@tonic-gate 						  result, out);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 				/* start over? */
17927c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
17937c478bd9Sstevel@tonic-gate 				RESET_SASLCONN;
17947c478bd9Sstevel@tonic-gate 			}
17957c478bd9Sstevel@tonic-gate 			else
17967c478bd9Sstevel@tonic-gate 			{
17977c478bd9Sstevel@tonic-gate 				message("334 %s", out2);
17987c478bd9Sstevel@tonic-gate 				authenticating = SASL_PROC_AUTH;
17997c478bd9Sstevel@tonic-gate 			}
18007c478bd9Sstevel@tonic-gate # if SASL >= 20000
18017c478bd9Sstevel@tonic-gate 			sm_free(out2);
18027c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
18037c478bd9Sstevel@tonic-gate 			break;
18047c478bd9Sstevel@tonic-gate #endif /* SASL */
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate #if STARTTLS
18077c478bd9Sstevel@tonic-gate 		  case CMDSTLS: /* starttls */
18087c478bd9Sstevel@tonic-gate 			DELAY_CONN("STARTTLS");
18097c478bd9Sstevel@tonic-gate 			if (*p != '\0')
18107c478bd9Sstevel@tonic-gate 			{
18117c478bd9Sstevel@tonic-gate 				message("501 5.5.2 Syntax error (no parameters allowed)");
18127c478bd9Sstevel@tonic-gate 				break;
18137c478bd9Sstevel@tonic-gate 			}
18147c478bd9Sstevel@tonic-gate 			if (!bitset(SRV_OFFER_TLS, features))
18157c478bd9Sstevel@tonic-gate 			{
18167c478bd9Sstevel@tonic-gate 				message("503 5.5.0 TLS not available");
18177c478bd9Sstevel@tonic-gate 				break;
18187c478bd9Sstevel@tonic-gate 			}
18197c478bd9Sstevel@tonic-gate 			if (!tls_ok_srv)
18207c478bd9Sstevel@tonic-gate 			{
18217c478bd9Sstevel@tonic-gate 				message("454 4.3.3 TLS not available after start");
18227c478bd9Sstevel@tonic-gate 				break;
18237c478bd9Sstevel@tonic-gate 			}
18247c478bd9Sstevel@tonic-gate 			if (smtp.sm_gotmail)
18257c478bd9Sstevel@tonic-gate 			{
18267c478bd9Sstevel@tonic-gate 				message("503 5.5.0 TLS not permitted during a mail transaction");
18277c478bd9Sstevel@tonic-gate 				break;
18287c478bd9Sstevel@tonic-gate 			}
18297c478bd9Sstevel@tonic-gate 			if (tempfail)
18307c478bd9Sstevel@tonic-gate 			{
18317c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
18327c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
18337c478bd9Sstevel@tonic-gate 						  "SMTP STARTTLS command (%.100s) from %s tempfailed (due to previous checks)",
18347c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
18357c478bd9Sstevel@tonic-gate 				usrerr("454 4.7.0 Please try again later");
18367c478bd9Sstevel@tonic-gate 				break;
18377c478bd9Sstevel@tonic-gate 			}
18387c478bd9Sstevel@tonic-gate   starttls:
18397c478bd9Sstevel@tonic-gate # if TLS_NO_RSA
18407c478bd9Sstevel@tonic-gate 			/*
18417c478bd9Sstevel@tonic-gate 			**  XXX do we need a temp key ?
18427c478bd9Sstevel@tonic-gate 			*/
18437c478bd9Sstevel@tonic-gate # else /* TLS_NO_RSA */
18447c478bd9Sstevel@tonic-gate # endif /* TLS_NO_RSA */
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate # if TLS_VRFY_PER_CTX
18477c478bd9Sstevel@tonic-gate 			/*
18487c478bd9Sstevel@tonic-gate 			**  Note: this sets the verification globally
18497c478bd9Sstevel@tonic-gate 			**  (per SSL_CTX)
18507c478bd9Sstevel@tonic-gate 			**  it's ok since it applies only to one transaction
18517c478bd9Sstevel@tonic-gate 			*/
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 			TLS_VERIFY_CLIENT();
18547c478bd9Sstevel@tonic-gate # endif /* TLS_VRFY_PER_CTX */
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 			if (srv_ssl != NULL)
18577c478bd9Sstevel@tonic-gate 				SSL_clear(srv_ssl);
18587c478bd9Sstevel@tonic-gate 			else if ((srv_ssl = SSL_new(srv_ctx)) == NULL)
18597c478bd9Sstevel@tonic-gate 			{
18607c478bd9Sstevel@tonic-gate 				message("454 4.3.3 TLS not available: error generating SSL handle");
18617c478bd9Sstevel@tonic-gate 				if (LogLevel > 8)
18627c478bd9Sstevel@tonic-gate 					tlslogerr("server");
18637c478bd9Sstevel@tonic-gate 				goto tls_done;
18647c478bd9Sstevel@tonic-gate 			}
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate # if !TLS_VRFY_PER_CTX
18677c478bd9Sstevel@tonic-gate 			/*
18687c478bd9Sstevel@tonic-gate 			**  this could be used if it were possible to set
18697c478bd9Sstevel@tonic-gate 			**  verification per SSL (connection)
18707c478bd9Sstevel@tonic-gate 			**  not just per SSL_CTX (global)
18717c478bd9Sstevel@tonic-gate 			*/
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 			TLS_VERIFY_CLIENT();
18747c478bd9Sstevel@tonic-gate # endif /* !TLS_VRFY_PER_CTX */
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 			rfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
18777c478bd9Sstevel@tonic-gate 			wfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 			if (rfd < 0 || wfd < 0 ||
18807c478bd9Sstevel@tonic-gate 			    SSL_set_rfd(srv_ssl, rfd) <= 0 ||
18817c478bd9Sstevel@tonic-gate 			    SSL_set_wfd(srv_ssl, wfd) <= 0)
18827c478bd9Sstevel@tonic-gate 			{
18837c478bd9Sstevel@tonic-gate 				message("454 4.3.3 TLS not available: error set fd");
18847c478bd9Sstevel@tonic-gate 				SSL_free(srv_ssl);
18857c478bd9Sstevel@tonic-gate 				srv_ssl = NULL;
18867c478bd9Sstevel@tonic-gate 				goto tls_done;
18877c478bd9Sstevel@tonic-gate 			}
18887c478bd9Sstevel@tonic-gate 			if (!smtps)
18897c478bd9Sstevel@tonic-gate 				message("220 2.0.0 Ready to start TLS");
18907c478bd9Sstevel@tonic-gate # if PIPELINING
18917c478bd9Sstevel@tonic-gate 			(void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
18927c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 			SSL_set_accept_state(srv_ssl);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate #  define SSL_ACC(s)	SSL_accept(s)
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 			tlsstart = curtime();
18997c478bd9Sstevel@tonic-gate   ssl_retry:
19007c478bd9Sstevel@tonic-gate 			if ((r = SSL_ACC(srv_ssl)) <= 0)
19017c478bd9Sstevel@tonic-gate 			{
1902445f2479Sjbeck 				int i, ssl_err;
19037c478bd9Sstevel@tonic-gate 
1904445f2479Sjbeck 				ssl_err = SSL_get_error(srv_ssl, r);
1905445f2479Sjbeck 				i = tls_retry(srv_ssl, rfd, wfd, tlsstart,
1906445f2479Sjbeck 						TimeOuts.to_starttls, ssl_err,
1907445f2479Sjbeck 						"server");
1908445f2479Sjbeck 				if (i > 0)
19097c478bd9Sstevel@tonic-gate 					goto ssl_retry;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
19127c478bd9Sstevel@tonic-gate 				{
19137c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
1914*e9af4bc0SJohn Beck 						  "STARTTLS=server, error: accept failed=%d, SSL_error=%d, errno=%d, retry=%d, relay=%.100s",
1915*e9af4bc0SJohn Beck 						  r, ssl_err, errno, i,
1916*e9af4bc0SJohn Beck 						  CurSmtpClient);
19177c478bd9Sstevel@tonic-gate 					if (LogLevel > 8)
19187c478bd9Sstevel@tonic-gate 						tlslogerr("server");
19197c478bd9Sstevel@tonic-gate 				}
19207c478bd9Sstevel@tonic-gate 				tls_ok_srv = false;
19217c478bd9Sstevel@tonic-gate 				SSL_free(srv_ssl);
19227c478bd9Sstevel@tonic-gate 				srv_ssl = NULL;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 				/*
19257c478bd9Sstevel@tonic-gate 				**  according to the next draft of
19267c478bd9Sstevel@tonic-gate 				**  RFC 2487 the connection should be dropped
19277c478bd9Sstevel@tonic-gate 				*/
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 				/* arrange to ignore any current send list */
19307c478bd9Sstevel@tonic-gate 				e->e_sendqueue = NULL;
19317c478bd9Sstevel@tonic-gate 				goto doquit;
19327c478bd9Sstevel@tonic-gate 			}
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 			/* ignore return code for now, it's in {verify} */
19357c478bd9Sstevel@tonic-gate 			(void) tls_get_info(srv_ssl, true,
19367c478bd9Sstevel@tonic-gate 					    CurSmtpClient,
19377c478bd9Sstevel@tonic-gate 					    &BlankEnvelope.e_macro,
19387c478bd9Sstevel@tonic-gate 					    bitset(SRV_VRFY_CLT, features));
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 			/*
19417c478bd9Sstevel@tonic-gate 			**  call Stls_client to find out whether
19427c478bd9Sstevel@tonic-gate 			**  to accept the connection from the client
19437c478bd9Sstevel@tonic-gate 			*/
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 			saveQuickAbort = QuickAbort;
19467c478bd9Sstevel@tonic-gate 			saveSuprErrs = SuprErrs;
19477c478bd9Sstevel@tonic-gate 			SuprErrs = true;
19487c478bd9Sstevel@tonic-gate 			QuickAbort = false;
19497c478bd9Sstevel@tonic-gate 			if (rscheck("tls_client",
19507c478bd9Sstevel@tonic-gate 				     macvalue(macid("{verify}"), e),
19517c478bd9Sstevel@tonic-gate 				     "STARTTLS", e,
19527c478bd9Sstevel@tonic-gate 				     RSF_RMCOMM|RSF_COUNT,
1953058561cbSjbeck 				     5, NULL, NOQID, NULL) != EX_OK ||
19547c478bd9Sstevel@tonic-gate 			    Errors > 0)
19557c478bd9Sstevel@tonic-gate 			{
19567c478bd9Sstevel@tonic-gate 				extern char MsgBuf[];
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 				if (MsgBuf[0] != '\0' && ISSMTPREPLY(MsgBuf))
19597c478bd9Sstevel@tonic-gate 					nullserver = newstr(MsgBuf);
19607c478bd9Sstevel@tonic-gate 				else
19617c478bd9Sstevel@tonic-gate 					nullserver = "503 5.7.0 Authentication required.";
19627c478bd9Sstevel@tonic-gate 			}
19637c478bd9Sstevel@tonic-gate 			QuickAbort = saveQuickAbort;
19647c478bd9Sstevel@tonic-gate 			SuprErrs = saveSuprErrs;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 			tls_ok_srv = false;	/* don't offer STARTTLS again */
19677c478bd9Sstevel@tonic-gate 			n_helo = 0;
19687c478bd9Sstevel@tonic-gate # if SASL
19697c478bd9Sstevel@tonic-gate 			if (sasl_ok)
19707c478bd9Sstevel@tonic-gate 			{
19717c478bd9Sstevel@tonic-gate 				int cipher_bits;
19727c478bd9Sstevel@tonic-gate 				bool verified;
19737c478bd9Sstevel@tonic-gate 				char *s, *v, *c;
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 				s = macvalue(macid("{cipher_bits}"), e);
19767c478bd9Sstevel@tonic-gate 				v = macvalue(macid("{verify}"), e);
19777c478bd9Sstevel@tonic-gate 				c = macvalue(macid("{cert_subject}"), e);
19787c478bd9Sstevel@tonic-gate 				verified = (v != NULL && strcmp(v, "OK") == 0);
19797c478bd9Sstevel@tonic-gate 				if (s != NULL && (cipher_bits = atoi(s)) > 0)
19807c478bd9Sstevel@tonic-gate 				{
19817c478bd9Sstevel@tonic-gate #  if SASL >= 20000
19827c478bd9Sstevel@tonic-gate 					ext_ssf = cipher_bits;
19837c478bd9Sstevel@tonic-gate 					auth_id = verified ? c : NULL;
19847c478bd9Sstevel@tonic-gate 					sasl_ok = ((sasl_setprop(conn,
19857c478bd9Sstevel@tonic-gate 							SASL_SSF_EXTERNAL,
19867c478bd9Sstevel@tonic-gate 							&ext_ssf) == SASL_OK) &&
19877c478bd9Sstevel@tonic-gate 						   (sasl_setprop(conn,
19887c478bd9Sstevel@tonic-gate 							SASL_AUTH_EXTERNAL,
19897c478bd9Sstevel@tonic-gate 							auth_id) == SASL_OK));
19907c478bd9Sstevel@tonic-gate #  else /* SASL >= 20000 */
19917c478bd9Sstevel@tonic-gate 					ext_ssf.ssf = cipher_bits;
19927c478bd9Sstevel@tonic-gate 					ext_ssf.auth_id = verified ? c : NULL;
19937c478bd9Sstevel@tonic-gate 					sasl_ok = sasl_setprop(conn,
19947c478bd9Sstevel@tonic-gate 							SASL_SSF_EXTERNAL,
19957c478bd9Sstevel@tonic-gate 							&ext_ssf) == SASL_OK;
19967c478bd9Sstevel@tonic-gate #  endif /* SASL >= 20000 */
19977c478bd9Sstevel@tonic-gate 					mechlist = NULL;
19987c478bd9Sstevel@tonic-gate 					if (sasl_ok)
19997c478bd9Sstevel@tonic-gate 						n_mechs = saslmechs(conn,
20007c478bd9Sstevel@tonic-gate 								    &mechlist);
20017c478bd9Sstevel@tonic-gate 				}
20027c478bd9Sstevel@tonic-gate 			}
20037c478bd9Sstevel@tonic-gate # endif /* SASL */
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 			/* switch to secure connection */
20067c478bd9Sstevel@tonic-gate 			if (sfdctls(&InChannel, &OutChannel, srv_ssl) == 0)
20077c478bd9Sstevel@tonic-gate 			{
20087c478bd9Sstevel@tonic-gate 				tls_active = true;
20097c478bd9Sstevel@tonic-gate # if PIPELINING
20107c478bd9Sstevel@tonic-gate 				(void) sm_io_autoflush(InChannel, OutChannel);
20117c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
20127c478bd9Sstevel@tonic-gate 			}
20137c478bd9Sstevel@tonic-gate 			else
20147c478bd9Sstevel@tonic-gate 			{
20157c478bd9Sstevel@tonic-gate 				/*
20167c478bd9Sstevel@tonic-gate 				**  XXX this is an internal error
20177c478bd9Sstevel@tonic-gate 				**  how to deal with it?
20187c478bd9Sstevel@tonic-gate 				**  we can't generate an error message
20197c478bd9Sstevel@tonic-gate 				**  since the other side switched to an
20207c478bd9Sstevel@tonic-gate 				**  encrypted layer, but we could not...
20217c478bd9Sstevel@tonic-gate 				**  just "hang up"?
20227c478bd9Sstevel@tonic-gate 				*/
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 				nullserver = "454 4.3.3 TLS not available: can't switch to encrypted layer";
20257c478bd9Sstevel@tonic-gate 				syserr("STARTTLS: can't switch to encrypted layer");
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 		  tls_done:
20287c478bd9Sstevel@tonic-gate 			if (smtps)
20297c478bd9Sstevel@tonic-gate 			{
20307c478bd9Sstevel@tonic-gate 				if (tls_active)
20317c478bd9Sstevel@tonic-gate 					goto greeting;
20327c478bd9Sstevel@tonic-gate 				else
20337c478bd9Sstevel@tonic-gate 					goto doquit;
20347c478bd9Sstevel@tonic-gate 			}
20357c478bd9Sstevel@tonic-gate 			break;
20367c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 		  case CMDHELO:		/* hello -- introduce yourself */
20397c478bd9Sstevel@tonic-gate 		  case CMDEHLO:		/* extended hello */
20407c478bd9Sstevel@tonic-gate 			DELAY_CONN("EHLO");
20417c478bd9Sstevel@tonic-gate 			if (c->cmd_code == CMDEHLO)
20427c478bd9Sstevel@tonic-gate 			{
20437c478bd9Sstevel@tonic-gate 				protocol = "ESMTP";
20447c478bd9Sstevel@tonic-gate 				SmtpPhase = "server EHLO";
20457c478bd9Sstevel@tonic-gate 			}
20467c478bd9Sstevel@tonic-gate 			else
20477c478bd9Sstevel@tonic-gate 			{
20487c478bd9Sstevel@tonic-gate 				protocol = "SMTP";
20497c478bd9Sstevel@tonic-gate 				SmtpPhase = "server HELO";
20507c478bd9Sstevel@tonic-gate 			}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 			/* avoid denial-of-service */
20537c478bd9Sstevel@tonic-gate 			STOP_IF_ATTACK(checksmtpattack(&n_helo, MAXHELOCOMMANDS,
20547c478bd9Sstevel@tonic-gate 							true, "HELO/EHLO", e));
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate #if 0
20577c478bd9Sstevel@tonic-gate 			/* RFC2821 4.1.4 allows duplicate HELO/EHLO */
20587c478bd9Sstevel@tonic-gate 			/* check for duplicate HELO/EHLO per RFC 1651 4.2 */
20597c478bd9Sstevel@tonic-gate 			if (gothello)
20607c478bd9Sstevel@tonic-gate 			{
20617c478bd9Sstevel@tonic-gate 				usrerr("503 %s Duplicate HELO/EHLO",
20627c478bd9Sstevel@tonic-gate 				       MyHostName);
20637c478bd9Sstevel@tonic-gate 				break;
20647c478bd9Sstevel@tonic-gate 			}
20657c478bd9Sstevel@tonic-gate #endif /* 0 */
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 			/* check for valid domain name (re 1123 5.2.5) */
20687c478bd9Sstevel@tonic-gate 			if (*p == '\0' && !AllowBogusHELO)
20697c478bd9Sstevel@tonic-gate 			{
20707c478bd9Sstevel@tonic-gate 				usrerr("501 %s requires domain address",
20717c478bd9Sstevel@tonic-gate 					cmdbuf);
20727c478bd9Sstevel@tonic-gate 				break;
20737c478bd9Sstevel@tonic-gate 			}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 			/* check for long domain name (hides Received: info) */
20767c478bd9Sstevel@tonic-gate 			if (strlen(p) > MAXNAME)
20777c478bd9Sstevel@tonic-gate 			{
20787c478bd9Sstevel@tonic-gate 				usrerr("501 Invalid domain name");
20797c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
20807c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, CurEnv->e_id,
20817c478bd9Sstevel@tonic-gate 						  "invalid domain name (too long) from %s",
20827c478bd9Sstevel@tonic-gate 						  CurSmtpClient);
20837c478bd9Sstevel@tonic-gate 				break;
20847c478bd9Sstevel@tonic-gate 			}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 			ok = true;
20877c478bd9Sstevel@tonic-gate 			for (q = p; *q != '\0'; q++)
20887c478bd9Sstevel@tonic-gate 			{
20897c478bd9Sstevel@tonic-gate 				if (!isascii(*q))
20907c478bd9Sstevel@tonic-gate 					break;
20917c478bd9Sstevel@tonic-gate 				if (isalnum(*q))
20927c478bd9Sstevel@tonic-gate 					continue;
20937c478bd9Sstevel@tonic-gate 				if (isspace(*q))
20947c478bd9Sstevel@tonic-gate 				{
20957c478bd9Sstevel@tonic-gate 					*q = '\0';
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 					/* only complain if strict check */
20987c478bd9Sstevel@tonic-gate 					ok = AllowBogusHELO;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 					/* allow trailing whitespace */
21017c478bd9Sstevel@tonic-gate 					while (!ok && *++q != '\0' &&
21027c478bd9Sstevel@tonic-gate 					       isspace(*q))
21037c478bd9Sstevel@tonic-gate 						;
21047c478bd9Sstevel@tonic-gate 					if (*q == '\0')
21057c478bd9Sstevel@tonic-gate 						ok = true;
21067c478bd9Sstevel@tonic-gate 					break;
21077c478bd9Sstevel@tonic-gate 				}
21087c478bd9Sstevel@tonic-gate 				if (strchr("[].-_#:", *q) == NULL)
21097c478bd9Sstevel@tonic-gate 					break;
21107c478bd9Sstevel@tonic-gate 			}
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 			if (*q == '\0' && ok)
21137c478bd9Sstevel@tonic-gate 			{
21147c478bd9Sstevel@tonic-gate 				q = "pleased to meet you";
21157c478bd9Sstevel@tonic-gate 				sendinghost = sm_strdup_x(p);
21167c478bd9Sstevel@tonic-gate 			}
21177c478bd9Sstevel@tonic-gate 			else if (!AllowBogusHELO)
21187c478bd9Sstevel@tonic-gate 			{
21197c478bd9Sstevel@tonic-gate 				usrerr("501 Invalid domain name");
21207c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
21217c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, CurEnv->e_id,
21227c478bd9Sstevel@tonic-gate 						  "invalid domain name (%s) from %.100s",
21237c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
21247c478bd9Sstevel@tonic-gate 				break;
21257c478bd9Sstevel@tonic-gate 			}
21267c478bd9Sstevel@tonic-gate 			else
21277c478bd9Sstevel@tonic-gate 			{
21287c478bd9Sstevel@tonic-gate 				q = "accepting invalid domain name";
21297c478bd9Sstevel@tonic-gate 			}
21307c478bd9Sstevel@tonic-gate 
2131445f2479Sjbeck 			if (gothello || smtp.sm_gotmail)
21327c478bd9Sstevel@tonic-gate 				CLEAR_STATE(cmdbuf);
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate #if MILTER
21357c478bd9Sstevel@tonic-gate 			if (smtp.sm_milterlist && smtp.sm_milterize &&
21367c478bd9Sstevel@tonic-gate 			    !bitset(EF_DISCARD, e->e_flags))
21377c478bd9Sstevel@tonic-gate 			{
21387c478bd9Sstevel@tonic-gate 				char state;
21397c478bd9Sstevel@tonic-gate 				char *response;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 				response = milter_helo(p, e, &state);
21427c478bd9Sstevel@tonic-gate 				switch (state)
21437c478bd9Sstevel@tonic-gate 				{
21447c478bd9Sstevel@tonic-gate 				  case SMFIR_REJECT:
21457c478bd9Sstevel@tonic-gate 					if (MilterLogLevel > 3)
21467c478bd9Sstevel@tonic-gate 						sm_syslog(LOG_INFO, e->e_id,
21477c478bd9Sstevel@tonic-gate 							  "Milter: helo=%s, reject=Command rejected",
21487c478bd9Sstevel@tonic-gate 							  p);
21497c478bd9Sstevel@tonic-gate 					nullserver = "Command rejected";
21507c478bd9Sstevel@tonic-gate 					smtp.sm_milterize = false;
21517c478bd9Sstevel@tonic-gate 					break;
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 				  case SMFIR_TEMPFAIL:
21547c478bd9Sstevel@tonic-gate 					if (MilterLogLevel > 3)
21557c478bd9Sstevel@tonic-gate 						sm_syslog(LOG_INFO, e->e_id,
21567c478bd9Sstevel@tonic-gate 							  "Milter: helo=%s, reject=%s",
21577c478bd9Sstevel@tonic-gate 							  p, MSG_TEMPFAIL);
21587c478bd9Sstevel@tonic-gate 					tempfail = true;
21597c478bd9Sstevel@tonic-gate 					smtp.sm_milterize = false;
21607c478bd9Sstevel@tonic-gate 					break;
2161445f2479Sjbeck 
2162058561cbSjbeck 				  case SMFIR_REPLYCODE:
2163445f2479Sjbeck 					if (MilterLogLevel > 3)
2164445f2479Sjbeck 						sm_syslog(LOG_INFO, e->e_id,
2165058561cbSjbeck 							  "Milter: helo=%s, reject=%s",
2166058561cbSjbeck 							  p, response);
2167058561cbSjbeck 					if (strncmp(response, "421 ", 4) != 0
2168058561cbSjbeck 					    && strncmp(response, "421-", 4) != 0)
2169058561cbSjbeck 					{
2170058561cbSjbeck 						nullserver = newstr(response);
2171058561cbSjbeck 						smtp.sm_milterize = false;
2172058561cbSjbeck 						break;
2173058561cbSjbeck 					}
2174058561cbSjbeck 					/* FALLTHROUGH */
2175058561cbSjbeck 
2176058561cbSjbeck 				  case SMFIR_SHUTDOWN:
2177058561cbSjbeck 					if (MilterLogLevel > 3 &&
2178058561cbSjbeck 					    response == NULL)
2179058561cbSjbeck 						sm_syslog(LOG_INFO, e->e_id,
21803ee0e492Sjbeck 							  "Milter: helo=%s, reject=421 4.7.0 %s closing connection",
2181445f2479Sjbeck 							  p, MyHostName);
2182445f2479Sjbeck 					tempfail = true;
2183445f2479Sjbeck 					smtp.sm_milterize = false;
2184058561cbSjbeck 					if (response != NULL)
2185058561cbSjbeck 						usrerr(response);
2186058561cbSjbeck 					else
2187445f2479Sjbeck 						message("421 4.7.0 %s closing connection",
2188445f2479Sjbeck 							MyHostName);
2189445f2479Sjbeck 					/* arrange to ignore send list */
2190445f2479Sjbeck 					e->e_sendqueue = NULL;
2191058561cbSjbeck 					lognullconnection = false;
2192445f2479Sjbeck 					goto doquit;
21937c478bd9Sstevel@tonic-gate 				}
21947c478bd9Sstevel@tonic-gate 				if (response != NULL)
21957c478bd9Sstevel@tonic-gate 					sm_free(response);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 				/*
21987c478bd9Sstevel@tonic-gate 				**  If quarantining by a connect/ehlo action,
21997c478bd9Sstevel@tonic-gate 				**  save between messages
22007c478bd9Sstevel@tonic-gate 				*/
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 				if (smtp.sm_quarmsg == NULL &&
22037c478bd9Sstevel@tonic-gate 				    e->e_quarmsg != NULL)
22047c478bd9Sstevel@tonic-gate 					smtp.sm_quarmsg = newstr(e->e_quarmsg);
22057c478bd9Sstevel@tonic-gate 			}
22067c478bd9Sstevel@tonic-gate #endif /* MILTER */
22077c478bd9Sstevel@tonic-gate 			gothello = true;
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 			/* print HELO response message */
22107c478bd9Sstevel@tonic-gate 			if (c->cmd_code != CMDEHLO)
22117c478bd9Sstevel@tonic-gate 			{
22127c478bd9Sstevel@tonic-gate 				message("250 %s Hello %s, %s",
22137c478bd9Sstevel@tonic-gate 					MyHostName, CurSmtpClient, q);
22147c478bd9Sstevel@tonic-gate 				break;
22157c478bd9Sstevel@tonic-gate 			}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 			message("250-%s Hello %s, %s",
22187c478bd9Sstevel@tonic-gate 				MyHostName, CurSmtpClient, q);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 			/* offer ENHSC even for nullserver */
22217c478bd9Sstevel@tonic-gate 			if (nullserver != NULL)
22227c478bd9Sstevel@tonic-gate 			{
22237c478bd9Sstevel@tonic-gate 				message("250 ENHANCEDSTATUSCODES");
22247c478bd9Sstevel@tonic-gate 				break;
22257c478bd9Sstevel@tonic-gate 			}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 			/*
22287c478bd9Sstevel@tonic-gate 			**  print EHLO features list
22297c478bd9Sstevel@tonic-gate 			**
22307c478bd9Sstevel@tonic-gate 			**  Note: If you change this list,
22317c478bd9Sstevel@tonic-gate 			**	  remember to update 'helpfile'
22327c478bd9Sstevel@tonic-gate 			*/
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 			message("250-ENHANCEDSTATUSCODES");
22357c478bd9Sstevel@tonic-gate #if PIPELINING
22367c478bd9Sstevel@tonic-gate 			if (bitset(SRV_OFFER_PIPE, features))
22377c478bd9Sstevel@tonic-gate 				message("250-PIPELINING");
22387c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
22397c478bd9Sstevel@tonic-gate 			if (bitset(SRV_OFFER_EXPN, features))
22407c478bd9Sstevel@tonic-gate 			{
22417c478bd9Sstevel@tonic-gate 				message("250-EXPN");
22427c478bd9Sstevel@tonic-gate 				if (bitset(SRV_OFFER_VERB, features))
22437c478bd9Sstevel@tonic-gate 					message("250-VERB");
22447c478bd9Sstevel@tonic-gate 			}
22457c478bd9Sstevel@tonic-gate #if MIME8TO7
22467c478bd9Sstevel@tonic-gate 			message("250-8BITMIME");
22477c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */
22487c478bd9Sstevel@tonic-gate 			if (MaxMessageSize > 0)
22497c478bd9Sstevel@tonic-gate 				message("250-SIZE %ld", MaxMessageSize);
22507c478bd9Sstevel@tonic-gate 			else
22517c478bd9Sstevel@tonic-gate 				message("250-SIZE");
22527c478bd9Sstevel@tonic-gate #if DSN
22537c478bd9Sstevel@tonic-gate 			if (SendMIMEErrors && bitset(SRV_OFFER_DSN, features))
22547c478bd9Sstevel@tonic-gate 				message("250-DSN");
22557c478bd9Sstevel@tonic-gate #endif /* DSN */
22567c478bd9Sstevel@tonic-gate 			if (bitset(SRV_OFFER_ETRN, features))
22577c478bd9Sstevel@tonic-gate 				message("250-ETRN");
22587c478bd9Sstevel@tonic-gate #if SASL
22597c478bd9Sstevel@tonic-gate 			if (sasl_ok && mechlist != NULL && *mechlist != '\0')
22607c478bd9Sstevel@tonic-gate 				message("250-AUTH %s", mechlist);
22617c478bd9Sstevel@tonic-gate #endif /* SASL */
22627c478bd9Sstevel@tonic-gate #if STARTTLS
22637c478bd9Sstevel@tonic-gate 			if (tls_ok_srv &&
22647c478bd9Sstevel@tonic-gate 			    bitset(SRV_OFFER_TLS, features))
22657c478bd9Sstevel@tonic-gate 				message("250-STARTTLS");
22667c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
22677c478bd9Sstevel@tonic-gate 			if (DeliverByMin > 0)
22687c478bd9Sstevel@tonic-gate 				message("250-DELIVERBY %ld",
22697c478bd9Sstevel@tonic-gate 					(long) DeliverByMin);
22707c478bd9Sstevel@tonic-gate 			else if (DeliverByMin == 0)
22717c478bd9Sstevel@tonic-gate 				message("250-DELIVERBY");
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 			/* < 0: no deliver-by */
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 			message("250 HELP");
22767c478bd9Sstevel@tonic-gate 			break;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		  case CMDMAIL:		/* mail -- designate sender */
22797c478bd9Sstevel@tonic-gate 			SmtpPhase = "server MAIL";
22807c478bd9Sstevel@tonic-gate 			DELAY_CONN("MAIL");
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 			/* check for validity of this command */
22837c478bd9Sstevel@tonic-gate 			if (!gothello && bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
22847c478bd9Sstevel@tonic-gate 			{
22857c478bd9Sstevel@tonic-gate 				usrerr("503 5.0.0 Polite people say HELO first");
22867c478bd9Sstevel@tonic-gate 				break;
22877c478bd9Sstevel@tonic-gate 			}
22887c478bd9Sstevel@tonic-gate 			if (smtp.sm_gotmail)
22897c478bd9Sstevel@tonic-gate 			{
22907c478bd9Sstevel@tonic-gate 				usrerr("503 5.5.0 Sender already specified");
22917c478bd9Sstevel@tonic-gate 				break;
22927c478bd9Sstevel@tonic-gate 			}
22937c478bd9Sstevel@tonic-gate #if SASL
22947c478bd9Sstevel@tonic-gate 			if (bitset(SRV_REQ_AUTH, features) &&
22957c478bd9Sstevel@tonic-gate 			    authenticating != SASL_IS_AUTH)
22967c478bd9Sstevel@tonic-gate 			{
22977c478bd9Sstevel@tonic-gate 				usrerr("530 5.7.0 Authentication required");
22987c478bd9Sstevel@tonic-gate 				break;
22997c478bd9Sstevel@tonic-gate 			}
23007c478bd9Sstevel@tonic-gate #endif /* SASL */
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 			p = skipword(p, "from");
23037c478bd9Sstevel@tonic-gate 			if (p == NULL)
23047c478bd9Sstevel@tonic-gate 				break;
23057c478bd9Sstevel@tonic-gate 			if (tempfail)
23067c478bd9Sstevel@tonic-gate 			{
23077c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
23087c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
23097c478bd9Sstevel@tonic-gate 						  "SMTP MAIL command (%.100s) from %s tempfailed (due to previous checks)",
23107c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
23117c478bd9Sstevel@tonic-gate 				usrerr(MSG_TEMPFAIL);
23127c478bd9Sstevel@tonic-gate 				break;
23137c478bd9Sstevel@tonic-gate 			}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 			/* make sure we know who the sending host is */
23167c478bd9Sstevel@tonic-gate 			if (sendinghost == NULL)
23177c478bd9Sstevel@tonic-gate 				sendinghost = peerhostname;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
23217c478bd9Sstevel@tonic-gate 			if (sm_debug_active(&DebugLeakSmtp, 1))
23227c478bd9Sstevel@tonic-gate 			{
23237c478bd9Sstevel@tonic-gate 				sm_heap_newgroup();
23247c478bd9Sstevel@tonic-gate 				sm_dprintf("smtp() heap group #%d\n",
23257c478bd9Sstevel@tonic-gate 					sm_heap_group());
23267c478bd9Sstevel@tonic-gate 			}
23277c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 			if (Errors > 0)
23307c478bd9Sstevel@tonic-gate 				goto undo_no_pm;
23317c478bd9Sstevel@tonic-gate 			if (!gothello)
23327c478bd9Sstevel@tonic-gate 			{
23337c478bd9Sstevel@tonic-gate 				auth_warning(e, "%s didn't use HELO protocol",
23347c478bd9Sstevel@tonic-gate 					     CurSmtpClient);
23357c478bd9Sstevel@tonic-gate 			}
23367c478bd9Sstevel@tonic-gate #ifdef PICKY_HELO_CHECK
23377c478bd9Sstevel@tonic-gate 			if (sm_strcasecmp(sendinghost, peerhostname) != 0 &&
23387c478bd9Sstevel@tonic-gate 			    (sm_strcasecmp(peerhostname, "localhost") != 0 ||
23397c478bd9Sstevel@tonic-gate 			     sm_strcasecmp(sendinghost, MyHostName) != 0))
23407c478bd9Sstevel@tonic-gate 			{
23417c478bd9Sstevel@tonic-gate 				auth_warning(e, "Host %s claimed to be %s",
23427c478bd9Sstevel@tonic-gate 					     CurSmtpClient, sendinghost);
23437c478bd9Sstevel@tonic-gate 			}
23447c478bd9Sstevel@tonic-gate #endif /* PICKY_HELO_CHECK */
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 			if (protocol == NULL)
23477c478bd9Sstevel@tonic-gate 				protocol = "SMTP";
23487c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, 'r', protocol);
23497c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, 's', sendinghost);
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 			if (Errors > 0)
23527c478bd9Sstevel@tonic-gate 				goto undo_no_pm;
23537c478bd9Sstevel@tonic-gate 			smtp.sm_nrcpts = 0;
23547c478bd9Sstevel@tonic-gate 			n_badrcpts = 0;
23557c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, macid("{ntries}"), "0");
23567c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, macid("{nrcpts}"), "0");
23577c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, macid("{nbadrcpts}"),
23587c478bd9Sstevel@tonic-gate 				"0");
23597c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_CLRQUEUE;
23607c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "%s %s: %.80s",
23617c478bd9Sstevel@tonic-gate 					qid_printname(e),
23627c478bd9Sstevel@tonic-gate 					CurSmtpClient, inp);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 			/* do the processing */
23657c478bd9Sstevel@tonic-gate 		    SM_TRY
23667c478bd9Sstevel@tonic-gate 		    {
23677c478bd9Sstevel@tonic-gate 			extern char *FullName;
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 			QuickAbort = true;
23707c478bd9Sstevel@tonic-gate 			SM_FREE_CLR(FullName);
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 			/* must parse sender first */
23737c478bd9Sstevel@tonic-gate 			delimptr = NULL;
23747c478bd9Sstevel@tonic-gate 			setsender(p, e, &delimptr, ' ', false);
23757c478bd9Sstevel@tonic-gate 			if (delimptr != NULL && *delimptr != '\0')
23767c478bd9Sstevel@tonic-gate 				*delimptr++ = '\0';
23777c478bd9Sstevel@tonic-gate 			if (Errors > 0)
23787c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 			/* Successfully set e_from, allow logging */
23817c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_LOGSENDER;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 			/* put resulting triple from parseaddr() into macros */
23847c478bd9Sstevel@tonic-gate 			if (e->e_from.q_mailer != NULL)
23857c478bd9Sstevel@tonic-gate 				 macdefine(&e->e_macro, A_PERM,
23867c478bd9Sstevel@tonic-gate 					macid("{mail_mailer}"),
23877c478bd9Sstevel@tonic-gate 					e->e_from.q_mailer->m_name);
23887c478bd9Sstevel@tonic-gate 			else
23897c478bd9Sstevel@tonic-gate 				 macdefine(&e->e_macro, A_PERM,
23907c478bd9Sstevel@tonic-gate 					macid("{mail_mailer}"), NULL);
23917c478bd9Sstevel@tonic-gate 			if (e->e_from.q_host != NULL)
23927c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
23937c478bd9Sstevel@tonic-gate 					macid("{mail_host}"),
23947c478bd9Sstevel@tonic-gate 					e->e_from.q_host);
23957c478bd9Sstevel@tonic-gate 			else
23967c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
23977c478bd9Sstevel@tonic-gate 					macid("{mail_host}"), "localhost");
23987c478bd9Sstevel@tonic-gate 			if (e->e_from.q_user != NULL)
23997c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
24007c478bd9Sstevel@tonic-gate 					macid("{mail_addr}"),
24017c478bd9Sstevel@tonic-gate 					e->e_from.q_user);
24027c478bd9Sstevel@tonic-gate 			else
24037c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
24047c478bd9Sstevel@tonic-gate 					macid("{mail_addr}"), NULL);
24057c478bd9Sstevel@tonic-gate 			if (Errors > 0)
24067c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 			/* check for possible spoofing */
24097c478bd9Sstevel@tonic-gate 			if (RealUid != 0 && OpMode == MD_SMTP &&
24107c478bd9Sstevel@tonic-gate 			    !wordinclass(RealUserName, 't') &&
24117c478bd9Sstevel@tonic-gate 			    (!bitnset(M_LOCALMAILER,
24127c478bd9Sstevel@tonic-gate 				      e->e_from.q_mailer->m_flags) ||
24137c478bd9Sstevel@tonic-gate 			     strcmp(e->e_from.q_user, RealUserName) != 0))
24147c478bd9Sstevel@tonic-gate 			{
24157c478bd9Sstevel@tonic-gate 				auth_warning(e, "%s owned process doing -bs",
24167c478bd9Sstevel@tonic-gate 					RealUserName);
24177c478bd9Sstevel@tonic-gate 			}
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 			/* reset to default value */
2420058561cbSjbeck 			SevenBitInput = SevenBitInput_Saved;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 			/* now parse ESMTP arguments */
24237c478bd9Sstevel@tonic-gate 			e->e_msgsize = 0;
24247c478bd9Sstevel@tonic-gate 			addr = p;
2425058561cbSjbeck 			parse_esmtp_args(e, NULL, p, delimptr, "MAIL", args,
2426058561cbSjbeck 					mail_esmtp_args);
24277c478bd9Sstevel@tonic-gate 			if (Errors > 0)
24287c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate #if SASL
24317c478bd9Sstevel@tonic-gate # if _FFR_AUTH_PASSING
24327c478bd9Sstevel@tonic-gate 			/* set the default AUTH= if the sender didn't */
24337c478bd9Sstevel@tonic-gate 			if (e->e_auth_param == NULL)
24347c478bd9Sstevel@tonic-gate 			{
24357c478bd9Sstevel@tonic-gate 				/* XXX only do this for an MSA? */
24367c478bd9Sstevel@tonic-gate 				e->e_auth_param = macvalue(macid("{auth_authen}"),
24377c478bd9Sstevel@tonic-gate 							   e);
24387c478bd9Sstevel@tonic-gate 				if (e->e_auth_param == NULL)
24397c478bd9Sstevel@tonic-gate 					e->e_auth_param = "<>";
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 				/*
24427c478bd9Sstevel@tonic-gate 				**  XXX should we invoke Strust_auth now?
24437c478bd9Sstevel@tonic-gate 				**  authorizing as the client that just
24447c478bd9Sstevel@tonic-gate 				**  authenticated, so we'll trust implicitly
24457c478bd9Sstevel@tonic-gate 				*/
24467c478bd9Sstevel@tonic-gate 			}
24477c478bd9Sstevel@tonic-gate # endif /* _FFR_AUTH_PASSING */
24487c478bd9Sstevel@tonic-gate #endif /* SASL */
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 			/* do config file checking of the sender */
24517c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
24527c478bd9Sstevel@tonic-gate 				macid("{addr_type}"), "e s");
24537c478bd9Sstevel@tonic-gate #if _FFR_MAIL_MACRO
24547c478bd9Sstevel@tonic-gate 			/* make the "real" sender address available */
24557c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_TEMP, macid("{mail_from}"),
24567c478bd9Sstevel@tonic-gate 				  e->e_from.q_paddr);
24577c478bd9Sstevel@tonic-gate #endif /* _FFR_MAIL_MACRO */
24587c478bd9Sstevel@tonic-gate 			if (rscheck("check_mail", addr,
24597c478bd9Sstevel@tonic-gate 				    NULL, e, RSF_RMCOMM|RSF_COUNT, 3,
2460058561cbSjbeck 				    NULL, e->e_id, NULL) != EX_OK ||
24617c478bd9Sstevel@tonic-gate 			    Errors > 0)
24627c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
24637c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
24647c478bd9Sstevel@tonic-gate 				  macid("{addr_type}"), NULL);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 			if (MaxMessageSize > 0 &&
24677c478bd9Sstevel@tonic-gate 			    (e->e_msgsize > MaxMessageSize ||
24687c478bd9Sstevel@tonic-gate 			     e->e_msgsize < 0))
24697c478bd9Sstevel@tonic-gate 			{
24707c478bd9Sstevel@tonic-gate 				usrerr("552 5.2.3 Message size exceeds fixed maximum message size (%ld)",
24717c478bd9Sstevel@tonic-gate 					MaxMessageSize);
24727c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
24737c478bd9Sstevel@tonic-gate 			}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 			/*
24767c478bd9Sstevel@tonic-gate 			**  XXX always check whether there is at least one fs
24777c478bd9Sstevel@tonic-gate 			**  with enough space?
24787c478bd9Sstevel@tonic-gate 			**  However, this may not help much: the queue group
24797c478bd9Sstevel@tonic-gate 			**  selection may later on select a FS that hasn't
24807c478bd9Sstevel@tonic-gate 			**  enough space.
24817c478bd9Sstevel@tonic-gate 			*/
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 			if ((NumFileSys == 1 || NumQueue == 1) &&
24847c478bd9Sstevel@tonic-gate 			    !enoughdiskspace(e->e_msgsize, e)
24857c478bd9Sstevel@tonic-gate #if _FFR_ANY_FREE_FS
24867c478bd9Sstevel@tonic-gate 			    && !filesys_free(e->e_msgsize)
24877c478bd9Sstevel@tonic-gate #endif /* _FFR_ANY_FREE_FS */
24887c478bd9Sstevel@tonic-gate 			   )
24897c478bd9Sstevel@tonic-gate 			{
24907c478bd9Sstevel@tonic-gate 				/*
24917c478bd9Sstevel@tonic-gate 				**  We perform this test again when the
24927c478bd9Sstevel@tonic-gate 				**  queue directory is selected, in collect.
24937c478bd9Sstevel@tonic-gate 				*/
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 				usrerr("452 4.4.5 Insufficient disk space; try again later");
24967c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
24977c478bd9Sstevel@tonic-gate 			}
24987c478bd9Sstevel@tonic-gate 			if (Errors > 0)
24997c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 			LogUsrErrs = true;
25027c478bd9Sstevel@tonic-gate #if MILTER
25037c478bd9Sstevel@tonic-gate 			if (smtp.sm_milterlist && smtp.sm_milterize &&
25047c478bd9Sstevel@tonic-gate 			    !bitset(EF_DISCARD, e->e_flags))
25057c478bd9Sstevel@tonic-gate 			{
25067c478bd9Sstevel@tonic-gate 				char state;
25077c478bd9Sstevel@tonic-gate 				char *response;
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 				response = milter_envfrom(args, e, &state);
25107c478bd9Sstevel@tonic-gate 				MILTER_REPLY("from");
25117c478bd9Sstevel@tonic-gate 			}
25127c478bd9Sstevel@tonic-gate #endif /* MILTER */
25137c478bd9Sstevel@tonic-gate 			if (Errors > 0)
25147c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 			message("250 2.1.0 Sender ok");
25177c478bd9Sstevel@tonic-gate 			smtp.sm_gotmail = true;
25187c478bd9Sstevel@tonic-gate 		    }
25197c478bd9Sstevel@tonic-gate 		    SM_EXCEPT(exc, "[!F]*")
25207c478bd9Sstevel@tonic-gate 		    {
25217c478bd9Sstevel@tonic-gate 			/*
25227c478bd9Sstevel@tonic-gate 			**  An error occurred while processing a MAIL command.
25237c478bd9Sstevel@tonic-gate 			**  Jump to the common error handling code.
25247c478bd9Sstevel@tonic-gate 			*/
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 			sm_exc_free(exc);
25277c478bd9Sstevel@tonic-gate 			goto undo_no_pm;
25287c478bd9Sstevel@tonic-gate 		    }
25297c478bd9Sstevel@tonic-gate 		    SM_END_TRY
25307c478bd9Sstevel@tonic-gate 			break;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 		  undo_no_pm:
25337c478bd9Sstevel@tonic-gate 			e->e_flags &= ~EF_PM_NOTIFY;
25347c478bd9Sstevel@tonic-gate 		  undo:
25357c478bd9Sstevel@tonic-gate 			break;
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 		  case CMDRCPT:		/* rcpt -- designate recipient */
25387c478bd9Sstevel@tonic-gate 			DELAY_CONN("RCPT");
2539058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2540058561cbSjbeck 				macid("{rcpt_mailer}"), NULL);
2541058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2542058561cbSjbeck 				macid("{rcpt_host}"), NULL);
2543058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2544058561cbSjbeck 				macid("{rcpt_addr}"), NULL);
2545058561cbSjbeck #if MILTER
2546058561cbSjbeck 			(void) memset(&addr_st, '\0', sizeof(addr_st));
2547058561cbSjbeck 			a = NULL;
25484aac33d3Sjbeck 			milter_rcpt_added = false;
25497800901eSjbeck 			smtp.sm_e_nrcpts_orig = e->e_nrcpts;
2550058561cbSjbeck #endif
2551d4660949Sjbeck #if _FFR_BADRCPT_SHUTDOWN
2552d4660949Sjbeck 			/*
2553d4660949Sjbeck 			**  hack to deal with hack, see below:
2554*e9af4bc0SJohn Beck 			**  n_badrcpts is increased if limit is reached.
2555d4660949Sjbeck 			*/
2556d4660949Sjbeck 
2557d4660949Sjbeck 			n_badrcpts_adj = (BadRcptThrottle > 0 &&
2558d4660949Sjbeck 					  n_badrcpts > BadRcptThrottle &&
2559d4660949Sjbeck 					  LogLevel > 5)
2560d4660949Sjbeck 					  ? n_badrcpts - 1 : n_badrcpts;
2561d4660949Sjbeck 			if (BadRcptShutdown > 0 &&
2562d4660949Sjbeck 			    n_badrcpts_adj >= BadRcptShutdown &&
2563d4660949Sjbeck 			    (BadRcptShutdownGood == 0 ||
2564d4660949Sjbeck 			     smtp.sm_nrcpts == 0 ||
2565d4660949Sjbeck 			     (n_badrcpts_adj * 100 /
2566d4660949Sjbeck 			      (smtp.sm_nrcpts + n_badrcpts) >=
2567d4660949Sjbeck 			      BadRcptShutdownGood)))
2568d4660949Sjbeck 			{
2569d4660949Sjbeck 				if (LogLevel > 5)
2570d4660949Sjbeck 					sm_syslog(LOG_INFO, e->e_id,
2571d4660949Sjbeck 						  "%s: Possible SMTP RCPT flood, shutting down connection.",
2572d4660949Sjbeck 						  CurSmtpClient);
2573d4660949Sjbeck 				message("421 4.7.0 %s Too many bad recipients; closing connection",
2574d4660949Sjbeck 				MyHostName);
2575d4660949Sjbeck 
2576d4660949Sjbeck 				/* arrange to ignore any current send list */
2577d4660949Sjbeck 				e->e_sendqueue = NULL;
2578d4660949Sjbeck 				goto doquit;
2579d4660949Sjbeck 			}
2580d4660949Sjbeck #endif /* _FFR_BADRCPT_SHUTDOWN */
25817c478bd9Sstevel@tonic-gate 			if (BadRcptThrottle > 0 &&
25827c478bd9Sstevel@tonic-gate 			    n_badrcpts >= BadRcptThrottle)
25837c478bd9Sstevel@tonic-gate 			{
25847c478bd9Sstevel@tonic-gate 				if (LogLevel > 5 &&
25857c478bd9Sstevel@tonic-gate 				    n_badrcpts == BadRcptThrottle)
25867c478bd9Sstevel@tonic-gate 				{
25877c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
25887c478bd9Sstevel@tonic-gate 						  "%s: Possible SMTP RCPT flood, throttling.",
25897c478bd9Sstevel@tonic-gate 						  CurSmtpClient);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 					/* To avoid duplicated message */
25927c478bd9Sstevel@tonic-gate 					n_badrcpts++;
25937c478bd9Sstevel@tonic-gate 				}
25947c478bd9Sstevel@tonic-gate 				NBADRCPTS;
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 				/*
25977c478bd9Sstevel@tonic-gate 				**  Don't use exponential backoff for now.
2598*e9af4bc0SJohn Beck 				**  Some systems will open more connections
25997c478bd9Sstevel@tonic-gate 				**  and actually overload the receiver even
26007c478bd9Sstevel@tonic-gate 				**  more.
26017c478bd9Sstevel@tonic-gate 				*/
26027c478bd9Sstevel@tonic-gate 
2603*e9af4bc0SJohn Beck 				(void) sleep(BadRcptThrottleDelay);
26047c478bd9Sstevel@tonic-gate 			}
26057c478bd9Sstevel@tonic-gate 			if (!smtp.sm_gotmail)
26067c478bd9Sstevel@tonic-gate 			{
26077c478bd9Sstevel@tonic-gate 				usrerr("503 5.0.0 Need MAIL before RCPT");
26087c478bd9Sstevel@tonic-gate 				break;
26097c478bd9Sstevel@tonic-gate 			}
26107c478bd9Sstevel@tonic-gate 			SmtpPhase = "server RCPT";
26117c478bd9Sstevel@tonic-gate 		    SM_TRY
26127c478bd9Sstevel@tonic-gate 		    {
26137c478bd9Sstevel@tonic-gate 			QuickAbort = true;
26147c478bd9Sstevel@tonic-gate 			LogUsrErrs = true;
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 			/* limit flooding of our machine */
26177c478bd9Sstevel@tonic-gate 			if (MaxRcptPerMsg > 0 &&
26187c478bd9Sstevel@tonic-gate 			    smtp.sm_nrcpts >= MaxRcptPerMsg)
26197c478bd9Sstevel@tonic-gate 			{
26207c478bd9Sstevel@tonic-gate 				/* sleep(1); / * slow down? */
26217c478bd9Sstevel@tonic-gate 				usrerr("452 4.5.3 Too many recipients");
26227c478bd9Sstevel@tonic-gate 				goto rcpt_done;
26237c478bd9Sstevel@tonic-gate 			}
26247c478bd9Sstevel@tonic-gate 
2625445f2479Sjbeck 			if (e->e_sendmode != SM_DELIVER
2626445f2479Sjbeck #if _FFR_DM_ONE
2627445f2479Sjbeck 			    && (NotFirstDelivery || SM_DM_ONE != e->e_sendmode)
2628445f2479Sjbeck #endif /* _FFR_DM_ONE */
2629445f2479Sjbeck 			   )
26307c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_VRFYONLY;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate #if MILTER
26337c478bd9Sstevel@tonic-gate 			/*
26344aac33d3Sjbeck 			**  Do not expand recipients at RCPT time (in the call
26357800901eSjbeck 			**  to recipient()) if a milter can delete or reject
26367800901eSjbeck 			**  a RCPT.  If they are expanded, it is impossible
26377800901eSjbeck 			**  for removefromlist() to figure out the expanded
26387800901eSjbeck 			**  members of the original recipient and mark them
26397800901eSjbeck 			**  as QS_DONTSEND.
26407c478bd9Sstevel@tonic-gate 			*/
26417c478bd9Sstevel@tonic-gate 
26427800901eSjbeck 			if (!(smtp.sm_milterlist && smtp.sm_milterize &&
26437800901eSjbeck 			      !bitset(EF_DISCARD, e->e_flags)) &&
26447800901eSjbeck 			    (smtp.sm_milters.mis_flags &
26457800901eSjbeck 			     (MIS_FL_DEL_RCPT|MIS_FL_REJ_RCPT)) != 0)
26467c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_VRFYONLY;
2647058561cbSjbeck 			milter_cmd_done = false;
2648058561cbSjbeck 			milter_cmd_safe = false;
26497c478bd9Sstevel@tonic-gate #endif /* MILTER */
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 			p = skipword(p, "to");
26527c478bd9Sstevel@tonic-gate 			if (p == NULL)
26537c478bd9Sstevel@tonic-gate 				goto rcpt_done;
26547c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
26557c478bd9Sstevel@tonic-gate 				macid("{addr_type}"), "e r");
26567c478bd9Sstevel@tonic-gate 			a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr,
26577c478bd9Sstevel@tonic-gate 				      e, true);
26587c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
26597c478bd9Sstevel@tonic-gate 				macid("{addr_type}"), NULL);
26607c478bd9Sstevel@tonic-gate 			if (Errors > 0)
26617c478bd9Sstevel@tonic-gate 				goto rcpt_done;
26627c478bd9Sstevel@tonic-gate 			if (a == NULL)
26637c478bd9Sstevel@tonic-gate 			{
26647c478bd9Sstevel@tonic-gate 				usrerr("501 5.0.0 Missing recipient");
26657c478bd9Sstevel@tonic-gate 				goto rcpt_done;
26667c478bd9Sstevel@tonic-gate 			}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 			if (delimptr != NULL && *delimptr != '\0')
26697c478bd9Sstevel@tonic-gate 				*delimptr++ = '\0';
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 			/* put resulting triple from parseaddr() into macros */
26727c478bd9Sstevel@tonic-gate 			if (a->q_mailer != NULL)
26737c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26747c478bd9Sstevel@tonic-gate 					macid("{rcpt_mailer}"),
26757c478bd9Sstevel@tonic-gate 					a->q_mailer->m_name);
26767c478bd9Sstevel@tonic-gate 			else
26777c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26787c478bd9Sstevel@tonic-gate 					macid("{rcpt_mailer}"), NULL);
26797c478bd9Sstevel@tonic-gate 			if (a->q_host != NULL)
26807c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26817c478bd9Sstevel@tonic-gate 					macid("{rcpt_host}"), a->q_host);
26827c478bd9Sstevel@tonic-gate 			else
26837c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26847c478bd9Sstevel@tonic-gate 					macid("{rcpt_host}"), "localhost");
26857c478bd9Sstevel@tonic-gate 			if (a->q_user != NULL)
26867c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26877c478bd9Sstevel@tonic-gate 					macid("{rcpt_addr}"), a->q_user);
26887c478bd9Sstevel@tonic-gate 			else
26897c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
26907c478bd9Sstevel@tonic-gate 					macid("{rcpt_addr}"), NULL);
26917c478bd9Sstevel@tonic-gate 			if (Errors > 0)
26927c478bd9Sstevel@tonic-gate 				goto rcpt_done;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 			/* now parse ESMTP arguments */
26957c478bd9Sstevel@tonic-gate 			addr = p;
2696058561cbSjbeck 			parse_esmtp_args(e, a, p, delimptr, "RCPT", args,
2697058561cbSjbeck 					rcpt_esmtp_args);
26987c478bd9Sstevel@tonic-gate 			if (Errors > 0)
26997c478bd9Sstevel@tonic-gate 				goto rcpt_done;
27007c478bd9Sstevel@tonic-gate 
27014aac33d3Sjbeck #if MILTER
27024aac33d3Sjbeck 			/*
27034aac33d3Sjbeck 			**  rscheck() can trigger an "exception"
27044aac33d3Sjbeck 			**  in which case the execution continues at
27054aac33d3Sjbeck 			**  SM_EXCEPT(exc, "[!F]*")
27064aac33d3Sjbeck 			**  This means milter_cmd_safe is not set
27074aac33d3Sjbeck 			**  and hence milter is not invoked.
27084aac33d3Sjbeck 			**  Would it be "safe" to change that, i.e., use
27094aac33d3Sjbeck 			**  milter_cmd_safe = true;
27104aac33d3Sjbeck 			**  here so a milter is informed (if requested)
27114aac33d3Sjbeck 			**  about RCPTs that are rejected by check_rcpt?
27124aac33d3Sjbeck 			*/
27134aac33d3Sjbeck # if _FFR_MILTER_CHECK_REJECTIONS_TOO
27144aac33d3Sjbeck 			milter_cmd_safe = true;
27154aac33d3Sjbeck # endif
27164aac33d3Sjbeck #endif
27174aac33d3Sjbeck 
27187c478bd9Sstevel@tonic-gate 			/* do config file checking of the recipient */
27197c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
27207c478bd9Sstevel@tonic-gate 				macid("{addr_type}"), "e r");
27217c478bd9Sstevel@tonic-gate 			if (rscheck("check_rcpt", addr,
27227c478bd9Sstevel@tonic-gate 				    NULL, e, RSF_RMCOMM|RSF_COUNT, 3,
2723058561cbSjbeck 				    NULL, e->e_id, p_addr_st) != EX_OK ||
27247c478bd9Sstevel@tonic-gate 			    Errors > 0)
27257c478bd9Sstevel@tonic-gate 				goto rcpt_done;
27267c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
27277c478bd9Sstevel@tonic-gate 				macid("{addr_type}"), NULL);
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 			/* If discarding, don't bother to verify user */
27307c478bd9Sstevel@tonic-gate 			if (bitset(EF_DISCARD, e->e_flags))
27317c478bd9Sstevel@tonic-gate 				a->q_state = QS_VERIFIED;
2732058561cbSjbeck #if MILTER
2733058561cbSjbeck 			milter_cmd_safe = true;
2734058561cbSjbeck #endif
2735058561cbSjbeck 
2736058561cbSjbeck 			/* save in recipient list after ESMTP mods */
2737058561cbSjbeck 			a = recipient(a, &e->e_sendqueue, 0, e);
2738058561cbSjbeck 			/* may trigger exception... */
2739058561cbSjbeck 
27404aac33d3Sjbeck #if MILTER
27414aac33d3Sjbeck 			milter_rcpt_added = true;
27424aac33d3Sjbeck #endif
27434aac33d3Sjbeck 
2744058561cbSjbeck 			if(!(Errors > 0) && QS_IS_BADADDR(a->q_state))
2745058561cbSjbeck 			{
2746058561cbSjbeck 				/* punt -- should keep message in ADDRESS.... */
2747058561cbSjbeck 				usrerr("550 5.1.1 Addressee unknown");
2748058561cbSjbeck 			}
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate #if MILTER
2751058561cbSjbeck 		rcpt_done:
27527c478bd9Sstevel@tonic-gate 			if (smtp.sm_milterlist && smtp.sm_milterize &&
27537c478bd9Sstevel@tonic-gate 			    !bitset(EF_DISCARD, e->e_flags))
27547c478bd9Sstevel@tonic-gate 			{
27557c478bd9Sstevel@tonic-gate 				char state;
27567c478bd9Sstevel@tonic-gate 				char *response;
27577c478bd9Sstevel@tonic-gate 
2758058561cbSjbeck 				/* how to get the error codes? */
2759058561cbSjbeck 				if (Errors > 0)
2760058561cbSjbeck 				{
2761058561cbSjbeck 					macdefine(&e->e_macro, A_PERM,
2762058561cbSjbeck 						macid("{rcpt_mailer}"),
2763058561cbSjbeck 						"error");
2764058561cbSjbeck 					if (a != NULL &&
2765058561cbSjbeck 					    a->q_status != NULL &&
2766058561cbSjbeck 					    a->q_rstatus != NULL)
2767058561cbSjbeck 					{
2768058561cbSjbeck 						macdefine(&e->e_macro, A_PERM,
2769058561cbSjbeck 							macid("{rcpt_host}"),
2770058561cbSjbeck 							a->q_status);
2771058561cbSjbeck 						macdefine(&e->e_macro, A_PERM,
2772058561cbSjbeck 							macid("{rcpt_addr}"),
2773058561cbSjbeck 							a->q_rstatus);
2774058561cbSjbeck 					}
2775058561cbSjbeck 					else
2776058561cbSjbeck 					{
2777058561cbSjbeck 						if (addr_st.q_host != NULL)
2778058561cbSjbeck 							macdefine(&e->e_macro,
2779058561cbSjbeck 								A_PERM,
2780058561cbSjbeck 								macid("{rcpt_host}"),
2781058561cbSjbeck 								addr_st.q_host);
2782058561cbSjbeck 						if (addr_st.q_user != NULL)
2783058561cbSjbeck 							macdefine(&e->e_macro,
2784058561cbSjbeck 								A_PERM,
2785058561cbSjbeck 								macid("{rcpt_addr}"),
2786058561cbSjbeck 								addr_st.q_user);
2787058561cbSjbeck 					}
2788058561cbSjbeck 				}
2789058561cbSjbeck 
2790058561cbSjbeck 				response = milter_envrcpt(args, e, &state,
2791058561cbSjbeck 							Errors > 0);
2792058561cbSjbeck 				milter_cmd_done = true;
27937c478bd9Sstevel@tonic-gate 				MILTER_REPLY("to");
27947c478bd9Sstevel@tonic-gate 			}
27957c478bd9Sstevel@tonic-gate #endif /* MILTER */
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 			/* no errors during parsing, but might be a duplicate */
27987c478bd9Sstevel@tonic-gate 			e->e_to = a->q_paddr;
2799058561cbSjbeck 			if (!(Errors > 0) && !QS_IS_BADADDR(a->q_state))
28007c478bd9Sstevel@tonic-gate 			{
28017c478bd9Sstevel@tonic-gate 				if (smtp.sm_nrcpts == 0)
28027c478bd9Sstevel@tonic-gate 					initsys(e);
28037c478bd9Sstevel@tonic-gate 				message("250 2.1.5 Recipient ok%s",
28047c478bd9Sstevel@tonic-gate 					QS_IS_QUEUEUP(a->q_state) ?
28057c478bd9Sstevel@tonic-gate 						" (will queue)" : "");
28067c478bd9Sstevel@tonic-gate 				smtp.sm_nrcpts++;
28077c478bd9Sstevel@tonic-gate 			}
2808058561cbSjbeck 
2809058561cbSjbeck 			/* Is this needed? */
2810058561cbSjbeck #if !MILTER
28117c478bd9Sstevel@tonic-gate 		rcpt_done:
2812058561cbSjbeck #endif /* !MILTER */
2813058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2814058561cbSjbeck 				macid("{rcpt_mailer}"), NULL);
2815058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2816058561cbSjbeck 				macid("{rcpt_host}"), NULL);
2817058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2818058561cbSjbeck 				macid("{rcpt_addr}"), NULL);
2819058561cbSjbeck 			macdefine(&e->e_macro, A_PERM,
2820058561cbSjbeck 				macid("{dsn_notify}"), NULL);
2821058561cbSjbeck 
28227c478bd9Sstevel@tonic-gate 			if (Errors > 0)
28237c478bd9Sstevel@tonic-gate 			{
28247c478bd9Sstevel@tonic-gate 				++n_badrcpts;
28257c478bd9Sstevel@tonic-gate 				NBADRCPTS;
28267c478bd9Sstevel@tonic-gate 			}
28277c478bd9Sstevel@tonic-gate 		    }
28287c478bd9Sstevel@tonic-gate 		    SM_EXCEPT(exc, "[!F]*")
28297c478bd9Sstevel@tonic-gate 		    {
28307c478bd9Sstevel@tonic-gate 			/* An exception occurred while processing RCPT */
28317c478bd9Sstevel@tonic-gate 			e->e_flags &= ~(EF_FATALERRS|EF_PM_NOTIFY);
28327c478bd9Sstevel@tonic-gate 			++n_badrcpts;
28337c478bd9Sstevel@tonic-gate 			NBADRCPTS;
2834058561cbSjbeck #if MILTER
2835058561cbSjbeck 			if (smtp.sm_milterlist && smtp.sm_milterize &&
2836058561cbSjbeck 			    !bitset(EF_DISCARD, e->e_flags) &&
2837058561cbSjbeck 			    !milter_cmd_done && milter_cmd_safe)
2838058561cbSjbeck 			{
2839058561cbSjbeck 				char state;
2840058561cbSjbeck 				char *response;
2841058561cbSjbeck 
2842058561cbSjbeck 				macdefine(&e->e_macro, A_PERM,
2843058561cbSjbeck 					macid("{rcpt_mailer}"), "error");
2844058561cbSjbeck 
2845058561cbSjbeck 				/* how to get the error codes? */
2846058561cbSjbeck 				if (addr_st.q_host != NULL)
2847058561cbSjbeck 					macdefine(&e->e_macro, A_PERM,
2848058561cbSjbeck 						macid("{rcpt_host}"),
2849058561cbSjbeck 						addr_st.q_host);
2850058561cbSjbeck 				else if (a != NULL && a->q_status != NULL)
2851058561cbSjbeck 					macdefine(&e->e_macro, A_PERM,
2852058561cbSjbeck 						macid("{rcpt_host}"),
2853058561cbSjbeck 						a->q_status);
2854058561cbSjbeck 
2855058561cbSjbeck 				if (addr_st.q_user != NULL)
2856058561cbSjbeck 					macdefine(&e->e_macro, A_PERM,
2857058561cbSjbeck 						macid("{rcpt_addr}"),
2858058561cbSjbeck 						addr_st.q_user);
2859058561cbSjbeck 				else if (a != NULL && a->q_rstatus != NULL)
2860058561cbSjbeck 					macdefine(&e->e_macro, A_PERM,
2861058561cbSjbeck 						macid("{rcpt_addr}"),
2862058561cbSjbeck 						a->q_rstatus);
2863058561cbSjbeck 
2864058561cbSjbeck 				response = milter_envrcpt(args, e, &state,
2865058561cbSjbeck 							true);
2866058561cbSjbeck 				milter_cmd_done = true;
2867058561cbSjbeck 				MILTER_REPLY("to");
2868058561cbSjbeck 				macdefine(&e->e_macro, A_PERM,
2869058561cbSjbeck 					macid("{rcpt_mailer}"), NULL);
2870058561cbSjbeck 				macdefine(&e->e_macro, A_PERM,
2871058561cbSjbeck 					macid("{rcpt_host}"), NULL);
2872058561cbSjbeck 				macdefine(&e->e_macro, A_PERM,
2873058561cbSjbeck 					macid("{rcpt_addr}"), NULL);
2874058561cbSjbeck 			}
28754aac33d3Sjbeck 			if (smtp.sm_milterlist && smtp.sm_milterize &&
28764aac33d3Sjbeck 			    milter_rcpt_added && milter_cmd_done &&
28774aac33d3Sjbeck 			    milter_cmd_fail)
28784aac33d3Sjbeck 			{
28794aac33d3Sjbeck 				(void) removefromlist(addr, &e->e_sendqueue, e);
28804aac33d3Sjbeck 				milter_cmd_fail = false;
28817800901eSjbeck 				if (smtp.sm_e_nrcpts_orig < e->e_nrcpts)
28827800901eSjbeck 					e->e_nrcpts = smtp.sm_e_nrcpts_orig;
28834aac33d3Sjbeck 			}
2884058561cbSjbeck #endif /* MILTER */
28857c478bd9Sstevel@tonic-gate 		    }
28867c478bd9Sstevel@tonic-gate 		    SM_END_TRY
28877c478bd9Sstevel@tonic-gate 			break;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 		  case CMDDATA:		/* data -- text of mail */
28907c478bd9Sstevel@tonic-gate 			DELAY_CONN("DATA");
28917c478bd9Sstevel@tonic-gate 			if (!smtp_data(&smtp, e))
28927c478bd9Sstevel@tonic-gate 				goto doquit;
28937c478bd9Sstevel@tonic-gate 			break;
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 		  case CMDRSET:		/* rset -- reset state */
28967c478bd9Sstevel@tonic-gate 			if (tTd(94, 100))
28977c478bd9Sstevel@tonic-gate 				message("451 4.0.0 Test failure");
28987c478bd9Sstevel@tonic-gate 			else
28997c478bd9Sstevel@tonic-gate 				message("250 2.0.0 Reset state");
29007c478bd9Sstevel@tonic-gate 			CLEAR_STATE(cmdbuf);
29017c478bd9Sstevel@tonic-gate 			break;
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 		  case CMDVRFY:		/* vrfy -- verify address */
29047c478bd9Sstevel@tonic-gate 		  case CMDEXPN:		/* expn -- expand address */
29057c478bd9Sstevel@tonic-gate 			vrfy = c->cmd_code == CMDVRFY;
29067c478bd9Sstevel@tonic-gate 			DELAY_CONN(vrfy ? "VRFY" : "EXPN");
29077c478bd9Sstevel@tonic-gate 			if (tempfail)
29087c478bd9Sstevel@tonic-gate 			{
29097c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
29107c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
29117c478bd9Sstevel@tonic-gate 						  "SMTP %s command (%.100s) from %s tempfailed (due to previous checks)",
29127c478bd9Sstevel@tonic-gate 						  vrfy ? "VRFY" : "EXPN",
29137c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 				/* RFC 821 doesn't allow 4xy reply code */
29167c478bd9Sstevel@tonic-gate 				usrerr("550 5.7.1 Please try again later");
29177c478bd9Sstevel@tonic-gate 				break;
29187c478bd9Sstevel@tonic-gate 			}
29197c478bd9Sstevel@tonic-gate 			wt = checksmtpattack(&n_verifies, MAXVRFYCOMMANDS,
29207c478bd9Sstevel@tonic-gate 					     false, vrfy ? "VRFY" : "EXPN", e);
29217c478bd9Sstevel@tonic-gate 			STOP_IF_ATTACK(wt);
29227c478bd9Sstevel@tonic-gate 			previous = curtime();
29237c478bd9Sstevel@tonic-gate 			if ((vrfy && bitset(PRIV_NOVRFY, PrivacyFlags)) ||
29247c478bd9Sstevel@tonic-gate 			    (!vrfy && !bitset(SRV_OFFER_EXPN, features)))
29257c478bd9Sstevel@tonic-gate 			{
29267c478bd9Sstevel@tonic-gate 				if (vrfy)
29277c478bd9Sstevel@tonic-gate 					message("252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)");
29287c478bd9Sstevel@tonic-gate 				else
29297c478bd9Sstevel@tonic-gate 					message("502 5.7.0 Sorry, we do not allow this operation");
29307c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
29317c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
29327c478bd9Sstevel@tonic-gate 						  "%s: %s [rejected]",
29337c478bd9Sstevel@tonic-gate 						  CurSmtpClient,
29347c478bd9Sstevel@tonic-gate 						  shortenstring(inp, MAXSHORTSTR));
29357c478bd9Sstevel@tonic-gate 				break;
29367c478bd9Sstevel@tonic-gate 			}
29377c478bd9Sstevel@tonic-gate 			else if (!gothello &&
29387c478bd9Sstevel@tonic-gate 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
29397c478bd9Sstevel@tonic-gate 						PrivacyFlags))
29407c478bd9Sstevel@tonic-gate 			{
29417c478bd9Sstevel@tonic-gate 				usrerr("503 5.0.0 I demand that you introduce yourself first");
29427c478bd9Sstevel@tonic-gate 				break;
29437c478bd9Sstevel@tonic-gate 			}
29447c478bd9Sstevel@tonic-gate 			if (Errors > 0)
29457c478bd9Sstevel@tonic-gate 				break;
29467c478bd9Sstevel@tonic-gate 			if (LogLevel > 5)
29477c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id, "%s: %s",
29487c478bd9Sstevel@tonic-gate 					  CurSmtpClient,
29497c478bd9Sstevel@tonic-gate 					  shortenstring(inp, MAXSHORTSTR));
29507c478bd9Sstevel@tonic-gate 		    SM_TRY
29517c478bd9Sstevel@tonic-gate 		    {
29527c478bd9Sstevel@tonic-gate 			QuickAbort = true;
29537c478bd9Sstevel@tonic-gate 			vrfyqueue = NULL;
29547c478bd9Sstevel@tonic-gate 			if (vrfy)
29557c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_VRFYONLY;
29567c478bd9Sstevel@tonic-gate 			while (*p != '\0' && isascii(*p) && isspace(*p))
29577c478bd9Sstevel@tonic-gate 				p++;
29587c478bd9Sstevel@tonic-gate 			if (*p == '\0')
29597c478bd9Sstevel@tonic-gate 			{
29607c478bd9Sstevel@tonic-gate 				usrerr("501 5.5.2 Argument required");
29617c478bd9Sstevel@tonic-gate 			}
29627c478bd9Sstevel@tonic-gate 			else
29637c478bd9Sstevel@tonic-gate 			{
29647c478bd9Sstevel@tonic-gate 				/* do config file checking of the address */
29657c478bd9Sstevel@tonic-gate 				if (rscheck(vrfy ? "check_vrfy" : "check_expn",
29667c478bd9Sstevel@tonic-gate 					    p, NULL, e, RSF_RMCOMM,
2967058561cbSjbeck 					    3, NULL, NOQID, NULL) != EX_OK ||
29687c478bd9Sstevel@tonic-gate 				    Errors > 0)
29697c478bd9Sstevel@tonic-gate 					sm_exc_raisenew_x(&EtypeQuickAbort, 1);
29707c478bd9Sstevel@tonic-gate 				(void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e);
29717c478bd9Sstevel@tonic-gate 			}
29727c478bd9Sstevel@tonic-gate 			if (wt > 0)
29737c478bd9Sstevel@tonic-gate 			{
29747c478bd9Sstevel@tonic-gate 				time_t t;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 				t = wt - (curtime() - previous);
29777c478bd9Sstevel@tonic-gate 				if (t > 0)
29787c478bd9Sstevel@tonic-gate 					(void) sleep(t);
29797c478bd9Sstevel@tonic-gate 			}
29807c478bd9Sstevel@tonic-gate 			if (Errors > 0)
29817c478bd9Sstevel@tonic-gate 				sm_exc_raisenew_x(&EtypeQuickAbort, 1);
29827c478bd9Sstevel@tonic-gate 			if (vrfyqueue == NULL)
29837c478bd9Sstevel@tonic-gate 			{
29847c478bd9Sstevel@tonic-gate 				usrerr("554 5.5.2 Nothing to %s", vrfy ? "VRFY" : "EXPN");
29857c478bd9Sstevel@tonic-gate 			}
29867c478bd9Sstevel@tonic-gate 			while (vrfyqueue != NULL)
29877c478bd9Sstevel@tonic-gate 			{
29887c478bd9Sstevel@tonic-gate 				if (!QS_IS_UNDELIVERED(vrfyqueue->q_state))
29897c478bd9Sstevel@tonic-gate 				{
29907c478bd9Sstevel@tonic-gate 					vrfyqueue = vrfyqueue->q_next;
29917c478bd9Sstevel@tonic-gate 					continue;
29927c478bd9Sstevel@tonic-gate 				}
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 				/* see if there is more in the vrfy list */
29957c478bd9Sstevel@tonic-gate 				a = vrfyqueue;
29967c478bd9Sstevel@tonic-gate 				while ((a = a->q_next) != NULL &&
29977c478bd9Sstevel@tonic-gate 				       (!QS_IS_UNDELIVERED(a->q_state)))
29987c478bd9Sstevel@tonic-gate 					continue;
29997c478bd9Sstevel@tonic-gate 				printvrfyaddr(vrfyqueue, a == NULL, vrfy);
30007c478bd9Sstevel@tonic-gate 				vrfyqueue = a;
30017c478bd9Sstevel@tonic-gate 			}
30027c478bd9Sstevel@tonic-gate 		    }
30037c478bd9Sstevel@tonic-gate 		    SM_EXCEPT(exc, "[!F]*")
30047c478bd9Sstevel@tonic-gate 		    {
30057c478bd9Sstevel@tonic-gate 			/*
30067c478bd9Sstevel@tonic-gate 			**  An exception occurred while processing VRFY/EXPN
30077c478bd9Sstevel@tonic-gate 			*/
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 			sm_exc_free(exc);
30107c478bd9Sstevel@tonic-gate 			goto undo;
30117c478bd9Sstevel@tonic-gate 		    }
30127c478bd9Sstevel@tonic-gate 		    SM_END_TRY
30137c478bd9Sstevel@tonic-gate 			break;
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 		  case CMDETRN:		/* etrn -- force queue flush */
30167c478bd9Sstevel@tonic-gate 			DELAY_CONN("ETRN");
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 			/* Don't leak queue information via debug flags */
30197c478bd9Sstevel@tonic-gate 			if (!bitset(SRV_OFFER_ETRN, features) || UseMSP ||
30207c478bd9Sstevel@tonic-gate 			    (RealUid != 0 && RealUid != TrustedUid &&
30217c478bd9Sstevel@tonic-gate 			     OpMode == MD_SMTP))
30227c478bd9Sstevel@tonic-gate 			{
30237c478bd9Sstevel@tonic-gate 				/* different message for MSA ? */
30247c478bd9Sstevel@tonic-gate 				message("502 5.7.0 Sorry, we do not allow this operation");
30257c478bd9Sstevel@tonic-gate 				if (LogLevel > 5)
30267c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
30277c478bd9Sstevel@tonic-gate 						  "%s: %s [rejected]",
30287c478bd9Sstevel@tonic-gate 						  CurSmtpClient,
30297c478bd9Sstevel@tonic-gate 						  shortenstring(inp, MAXSHORTSTR));
30307c478bd9Sstevel@tonic-gate 				break;
30317c478bd9Sstevel@tonic-gate 			}
30327c478bd9Sstevel@tonic-gate 			if (tempfail)
30337c478bd9Sstevel@tonic-gate 			{
30347c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
30357c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
30367c478bd9Sstevel@tonic-gate 						  "SMTP ETRN command (%.100s) from %s tempfailed (due to previous checks)",
30377c478bd9Sstevel@tonic-gate 						  p, CurSmtpClient);
30387c478bd9Sstevel@tonic-gate 				usrerr(MSG_TEMPFAIL);
30397c478bd9Sstevel@tonic-gate 				break;
30407c478bd9Sstevel@tonic-gate 			}
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 			if (strlen(p) <= 0)
30437c478bd9Sstevel@tonic-gate 			{
30447c478bd9Sstevel@tonic-gate 				usrerr("500 5.5.2 Parameter required");
30457c478bd9Sstevel@tonic-gate 				break;
30467c478bd9Sstevel@tonic-gate 			}
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 			/* crude way to avoid denial-of-service attacks */
30497c478bd9Sstevel@tonic-gate 			STOP_IF_ATTACK(checksmtpattack(&n_etrn, MAXETRNCOMMANDS,
30507c478bd9Sstevel@tonic-gate 							true, "ETRN", e));
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 			/*
30537c478bd9Sstevel@tonic-gate 			**  Do config file checking of the parameter.
30547c478bd9Sstevel@tonic-gate 			**  Even though we have srv_features now, we still
30557c478bd9Sstevel@tonic-gate 			**  need this ruleset because the former is called
30567c478bd9Sstevel@tonic-gate 			**  when the connection has been established, while
30577c478bd9Sstevel@tonic-gate 			**  this ruleset is called when the command is
30587c478bd9Sstevel@tonic-gate 			**  actually issued and therefore has all information
30597c478bd9Sstevel@tonic-gate 			**  available to make a decision.
30607c478bd9Sstevel@tonic-gate 			*/
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 			if (rscheck("check_etrn", p, NULL, e,
3063058561cbSjbeck 				    RSF_RMCOMM, 3, NULL, NOQID, NULL)
3064058561cbSjbeck 								!= EX_OK ||
30657c478bd9Sstevel@tonic-gate 			    Errors > 0)
30667c478bd9Sstevel@tonic-gate 				break;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 			if (LogLevel > 5)
30697c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
30707c478bd9Sstevel@tonic-gate 					  "%s: ETRN %s", CurSmtpClient,
30717c478bd9Sstevel@tonic-gate 					  shortenstring(p, MAXSHORTSTR));
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 			id = p;
30747c478bd9Sstevel@tonic-gate 			if (*id == '#')
30757c478bd9Sstevel@tonic-gate 			{
30767c478bd9Sstevel@tonic-gate 				int i, qgrp;
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 				id++;
30797c478bd9Sstevel@tonic-gate 				qgrp = name2qid(id);
30807c478bd9Sstevel@tonic-gate 				if (!ISVALIDQGRP(qgrp))
30817c478bd9Sstevel@tonic-gate 				{
30827c478bd9Sstevel@tonic-gate 					usrerr("459 4.5.4 Queue %s unknown",
30837c478bd9Sstevel@tonic-gate 					       id);
30847c478bd9Sstevel@tonic-gate 					break;
30857c478bd9Sstevel@tonic-gate 				}
30867c478bd9Sstevel@tonic-gate 				for (i = 0; i < NumQueue && Queue[i] != NULL;
30877c478bd9Sstevel@tonic-gate 				     i++)
30887c478bd9Sstevel@tonic-gate 					Queue[i]->qg_nextrun = (time_t) -1;
30897c478bd9Sstevel@tonic-gate 				Queue[qgrp]->qg_nextrun = 0;
30907c478bd9Sstevel@tonic-gate 				ok = run_work_group(Queue[qgrp]->qg_wgrp,
30917c478bd9Sstevel@tonic-gate 						    RWG_FORK|RWG_FORCE);
30927c478bd9Sstevel@tonic-gate 				if (ok && Errors == 0)
30937c478bd9Sstevel@tonic-gate 					message("250 2.0.0 Queuing for queue group %s started", id);
30947c478bd9Sstevel@tonic-gate 				break;
30957c478bd9Sstevel@tonic-gate 			}
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 			if (*id == '@')
30987c478bd9Sstevel@tonic-gate 				id++;
30997c478bd9Sstevel@tonic-gate 			else
31007c478bd9Sstevel@tonic-gate 				*--id = '@';
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 			new = (QUEUE_CHAR *) sm_malloc(sizeof(QUEUE_CHAR));
31037c478bd9Sstevel@tonic-gate 			if (new == NULL)
31047c478bd9Sstevel@tonic-gate 			{
31057c478bd9Sstevel@tonic-gate 				syserr("500 5.5.0 ETRN out of memory");
31067c478bd9Sstevel@tonic-gate 				break;
31077c478bd9Sstevel@tonic-gate 			}
31087c478bd9Sstevel@tonic-gate 			new->queue_match = id;
31097c478bd9Sstevel@tonic-gate 			new->queue_negate = false;
31107c478bd9Sstevel@tonic-gate 			new->queue_next = NULL;
31117c478bd9Sstevel@tonic-gate 			QueueLimitRecipient = new;
31127c478bd9Sstevel@tonic-gate 			ok = runqueue(true, false, false, true);
31137c478bd9Sstevel@tonic-gate 			sm_free(QueueLimitRecipient); /* XXX */
31147c478bd9Sstevel@tonic-gate 			QueueLimitRecipient = NULL;
31157c478bd9Sstevel@tonic-gate 			if (ok && Errors == 0)
31167c478bd9Sstevel@tonic-gate 				message("250 2.0.0 Queuing for node %s started", p);
31177c478bd9Sstevel@tonic-gate 			break;
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 		  case CMDHELP:		/* help -- give user info */
31207c478bd9Sstevel@tonic-gate 			DELAY_CONN("HELP");
31217c478bd9Sstevel@tonic-gate 			help(p, e);
31227c478bd9Sstevel@tonic-gate 			break;
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 		  case CMDNOOP:		/* noop -- do nothing */
31257c478bd9Sstevel@tonic-gate 			DELAY_CONN("NOOP");
3126445f2479Sjbeck 			STOP_IF_ATTACK(checksmtpattack(&n_noop, MaxNOOPCommands,
31277c478bd9Sstevel@tonic-gate 							true, "NOOP", e));
31287c478bd9Sstevel@tonic-gate 			message("250 2.0.0 OK");
31297c478bd9Sstevel@tonic-gate 			break;
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 		  case CMDQUIT:		/* quit -- leave mail */
31327c478bd9Sstevel@tonic-gate 			message("221 2.0.0 %s closing connection", MyHostName);
31337c478bd9Sstevel@tonic-gate #if PIPELINING
31347c478bd9Sstevel@tonic-gate 			(void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
31357c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 			if (smtp.sm_nrcpts > 0)
31387c478bd9Sstevel@tonic-gate 				logundelrcpts(e, "aborted by sender", 9, false);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 			/* arrange to ignore any current send list */
31417c478bd9Sstevel@tonic-gate 			e->e_sendqueue = NULL;
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate #if STARTTLS
31447c478bd9Sstevel@tonic-gate 			/* shutdown TLS connection */
31457c478bd9Sstevel@tonic-gate 			if (tls_active)
31467c478bd9Sstevel@tonic-gate 			{
31477c478bd9Sstevel@tonic-gate 				(void) endtls(srv_ssl, "server");
31487c478bd9Sstevel@tonic-gate 				tls_active = false;
31497c478bd9Sstevel@tonic-gate 			}
31507c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
31517c478bd9Sstevel@tonic-gate #if SASL
31527c478bd9Sstevel@tonic-gate 			if (authenticating == SASL_IS_AUTH)
31537c478bd9Sstevel@tonic-gate 			{
31547c478bd9Sstevel@tonic-gate 				sasl_dispose(&conn);
31557c478bd9Sstevel@tonic-gate 				authenticating = SASL_NOT_AUTH;
31567c478bd9Sstevel@tonic-gate 				/* XXX sasl_done(); this is a child */
31577c478bd9Sstevel@tonic-gate 			}
31587c478bd9Sstevel@tonic-gate #endif /* SASL */
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate doquit:
31617c478bd9Sstevel@tonic-gate 			/* avoid future 050 messages */
31627c478bd9Sstevel@tonic-gate 			disconnect(1, e);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate #if MILTER
31657c478bd9Sstevel@tonic-gate 			/* close out milter filters */
31667c478bd9Sstevel@tonic-gate 			milter_quit(e);
31677c478bd9Sstevel@tonic-gate #endif /* MILTER */
31687c478bd9Sstevel@tonic-gate 
3169*e9af4bc0SJohn Beck 			if (tTd(92, 2))
3170*e9af4bc0SJohn Beck 				sm_dprintf("QUIT: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
3171*e9af4bc0SJohn Beck 					e->e_id,
3172*e9af4bc0SJohn Beck 					bitset(EF_LOGSENDER, e->e_flags),
3173*e9af4bc0SJohn Beck 					LogLevel);
31747c478bd9Sstevel@tonic-gate 			if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
31757c478bd9Sstevel@tonic-gate 				logsender(e, NULL);
31767c478bd9Sstevel@tonic-gate 			e->e_flags &= ~EF_LOGSENDER;
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 			if (lognullconnection && LogLevel > 5 &&
31797c478bd9Sstevel@tonic-gate 			    nullserver == NULL)
31807c478bd9Sstevel@tonic-gate 			{
31817c478bd9Sstevel@tonic-gate 				char *d;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 				d = macvalue(macid("{daemon_name}"), e);
31847c478bd9Sstevel@tonic-gate 				if (d == NULL)
31857c478bd9Sstevel@tonic-gate 					d = "stdin";
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 				/*
31887c478bd9Sstevel@tonic-gate 				**  even though this id is "bogus", it makes
31897c478bd9Sstevel@tonic-gate 				**  it simpler to "grep" related events, e.g.,
31907c478bd9Sstevel@tonic-gate 				**  timeouts for the same connection.
31917c478bd9Sstevel@tonic-gate 				*/
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
31947c478bd9Sstevel@tonic-gate 					  "%s did not issue MAIL/EXPN/VRFY/ETRN during connection to %s",
31957c478bd9Sstevel@tonic-gate 					  CurSmtpClient, d);
31967c478bd9Sstevel@tonic-gate 			}
31977c478bd9Sstevel@tonic-gate 			if (tTd(93, 100))
31987c478bd9Sstevel@tonic-gate 			{
31997c478bd9Sstevel@tonic-gate 				/* return to handle next connection */
32007c478bd9Sstevel@tonic-gate 				return;
32017c478bd9Sstevel@tonic-gate 			}
32027c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
32037c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
32047c478bd9Sstevel@tonic-gate 
3205445f2479Sjbeck 			/* just to avoid bogus warning from some compilers */
3206445f2479Sjbeck 			exit(EX_OSERR);
3207445f2479Sjbeck 
32087c478bd9Sstevel@tonic-gate 		  case CMDVERB:		/* set verbose mode */
32097c478bd9Sstevel@tonic-gate 			DELAY_CONN("VERB");
32107c478bd9Sstevel@tonic-gate 			if (!bitset(SRV_OFFER_EXPN, features) ||
32117c478bd9Sstevel@tonic-gate 			    !bitset(SRV_OFFER_VERB, features))
32127c478bd9Sstevel@tonic-gate 			{
32137c478bd9Sstevel@tonic-gate 				/* this would give out the same info */
32147c478bd9Sstevel@tonic-gate 				message("502 5.7.0 Verbose unavailable");
32157c478bd9Sstevel@tonic-gate 				break;
32167c478bd9Sstevel@tonic-gate 			}
3217445f2479Sjbeck 			STOP_IF_ATTACK(checksmtpattack(&n_noop, MaxNOOPCommands,
32187c478bd9Sstevel@tonic-gate 							true, "VERB", e));
32197c478bd9Sstevel@tonic-gate 			Verbose = 1;
32207c478bd9Sstevel@tonic-gate 			set_delivery_mode(SM_DELIVER, e);
32217c478bd9Sstevel@tonic-gate 			message("250 2.0.0 Verbose mode");
32227c478bd9Sstevel@tonic-gate 			break;
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate #if SMTPDEBUG
32257c478bd9Sstevel@tonic-gate 		  case CMDDBGQSHOW:	/* show queues */
32267c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
32277c478bd9Sstevel@tonic-gate 					     "Send Queue=");
32287c478bd9Sstevel@tonic-gate 			printaddr(smioout, e->e_sendqueue, true);
32297c478bd9Sstevel@tonic-gate 			break;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 		  case CMDDBGDEBUG:	/* set debug mode */
3232058561cbSjbeck 			tTsetup(tTdvect, sizeof(tTdvect), "0-99.1");
32337c478bd9Sstevel@tonic-gate 			tTflag(p);
32347c478bd9Sstevel@tonic-gate 			message("200 2.0.0 Debug set");
32357c478bd9Sstevel@tonic-gate 			break;
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate #else /* SMTPDEBUG */
32387c478bd9Sstevel@tonic-gate 		  case CMDDBGQSHOW:	/* show queues */
32397c478bd9Sstevel@tonic-gate 		  case CMDDBGDEBUG:	/* set debug mode */
32407c478bd9Sstevel@tonic-gate #endif /* SMTPDEBUG */
32417c478bd9Sstevel@tonic-gate 		  case CMDLOGBOGUS:	/* bogus command */
32427c478bd9Sstevel@tonic-gate 			DELAY_CONN("Bogus");
32437c478bd9Sstevel@tonic-gate 			if (LogLevel > 0)
32447c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_CRIT, e->e_id,
32457c478bd9Sstevel@tonic-gate 					  "\"%s\" command from %s (%.100s)",
32467c478bd9Sstevel@tonic-gate 					  c->cmd_name, CurSmtpClient,
32477c478bd9Sstevel@tonic-gate 					  anynet_ntoa(&RealHostAddr));
32487c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 		  case CMDERROR:	/* unknown command */
32517c478bd9Sstevel@tonic-gate #if MAXBADCOMMANDS > 0
32527c478bd9Sstevel@tonic-gate 			if (++n_badcmds > MAXBADCOMMANDS)
32537c478bd9Sstevel@tonic-gate 			{
32547c478bd9Sstevel@tonic-gate   stopattack:
32557c478bd9Sstevel@tonic-gate 				message("421 4.7.0 %s Too many bad commands; closing connection",
32567c478bd9Sstevel@tonic-gate 					MyHostName);
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 				/* arrange to ignore any current send list */
32597c478bd9Sstevel@tonic-gate 				e->e_sendqueue = NULL;
32607c478bd9Sstevel@tonic-gate 				goto doquit;
32617c478bd9Sstevel@tonic-gate 			}
32627c478bd9Sstevel@tonic-gate #endif /* MAXBADCOMMANDS > 0 */
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate #if MILTER && SMFI_VERSION > 2
32657c478bd9Sstevel@tonic-gate 			if (smtp.sm_milterlist && smtp.sm_milterize &&
32667c478bd9Sstevel@tonic-gate 			    !bitset(EF_DISCARD, e->e_flags))
32677c478bd9Sstevel@tonic-gate 			{
32687c478bd9Sstevel@tonic-gate 				char state;
32697c478bd9Sstevel@tonic-gate 				char *response;
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 				if (MilterLogLevel > 9)
32727c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
32737c478bd9Sstevel@tonic-gate 						"Sending \"%s\" to Milter", inp);
32747c478bd9Sstevel@tonic-gate 				response = milter_unknown(inp, e, &state);
32757c478bd9Sstevel@tonic-gate 				MILTER_REPLY("unknown");
32767c478bd9Sstevel@tonic-gate 				if (state == SMFIR_REPLYCODE ||
32777c478bd9Sstevel@tonic-gate 				    state == SMFIR_REJECT ||
3278445f2479Sjbeck 				    state == SMFIR_TEMPFAIL ||
3279445f2479Sjbeck 				    state == SMFIR_SHUTDOWN)
32807c478bd9Sstevel@tonic-gate 				{
32817c478bd9Sstevel@tonic-gate 					/* MILTER_REPLY already gave an error */
32827c478bd9Sstevel@tonic-gate 					break;
32837c478bd9Sstevel@tonic-gate 				}
32847c478bd9Sstevel@tonic-gate 			}
32857c478bd9Sstevel@tonic-gate #endif /* MILTER && SMFI_VERSION > 2 */
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 			usrerr("500 5.5.1 Command unrecognized: \"%s\"",
32887c478bd9Sstevel@tonic-gate 			       shortenstring(inp, MAXSHORTSTR));
32897c478bd9Sstevel@tonic-gate 			break;
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 		  case CMDUNIMPL:
32927c478bd9Sstevel@tonic-gate 			DELAY_CONN("Unimpl");
32937c478bd9Sstevel@tonic-gate 			usrerr("502 5.5.1 Command not implemented: \"%s\"",
32947c478bd9Sstevel@tonic-gate 			       shortenstring(inp, MAXSHORTSTR));
32957c478bd9Sstevel@tonic-gate 			break;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 		  default:
32987c478bd9Sstevel@tonic-gate 			DELAY_CONN("default");
32997c478bd9Sstevel@tonic-gate 			errno = 0;
33007c478bd9Sstevel@tonic-gate 			syserr("500 5.5.0 smtp: unknown code %d", c->cmd_code);
33017c478bd9Sstevel@tonic-gate 			break;
33027c478bd9Sstevel@tonic-gate 		}
33037c478bd9Sstevel@tonic-gate #if SASL
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate #endif /* SASL */
33067c478bd9Sstevel@tonic-gate 	    }
33077c478bd9Sstevel@tonic-gate 	    SM_EXCEPT(exc, "[!F]*")
33087c478bd9Sstevel@tonic-gate 	    {
33097c478bd9Sstevel@tonic-gate 		/*
33107c478bd9Sstevel@tonic-gate 		**  The only possible exception is "E:mta.quickabort".
33117c478bd9Sstevel@tonic-gate 		**  There is nothing to do except fall through and loop.
33127c478bd9Sstevel@tonic-gate 		*/
33137c478bd9Sstevel@tonic-gate 	    }
33147c478bd9Sstevel@tonic-gate 	    SM_END_TRY
33157c478bd9Sstevel@tonic-gate 	}
33167c478bd9Sstevel@tonic-gate }
33177c478bd9Sstevel@tonic-gate /*
33187c478bd9Sstevel@tonic-gate **  SMTP_DATA -- implement the SMTP DATA command.
33197c478bd9Sstevel@tonic-gate **
33207c478bd9Sstevel@tonic-gate **	Parameters:
33217c478bd9Sstevel@tonic-gate **		smtp -- status of SMTP connection.
33227c478bd9Sstevel@tonic-gate **		e -- envelope.
33237c478bd9Sstevel@tonic-gate **
33247c478bd9Sstevel@tonic-gate **	Returns:
33257c478bd9Sstevel@tonic-gate **		true iff SMTP session can continue.
33267c478bd9Sstevel@tonic-gate **
33277c478bd9Sstevel@tonic-gate **	Side Effects:
33287c478bd9Sstevel@tonic-gate **		possibly sends message.
33297c478bd9Sstevel@tonic-gate */
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate static bool
smtp_data(smtp,e)33327c478bd9Sstevel@tonic-gate smtp_data(smtp, e)
33337c478bd9Sstevel@tonic-gate 	SMTP_T *smtp;
33347c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
33357c478bd9Sstevel@tonic-gate {
33367c478bd9Sstevel@tonic-gate #if MILTER
33377c478bd9Sstevel@tonic-gate 	bool milteraccept;
33387c478bd9Sstevel@tonic-gate #endif /* MILTER */
33397c478bd9Sstevel@tonic-gate 	bool aborting;
33407c478bd9Sstevel@tonic-gate 	bool doublequeue;
3341058561cbSjbeck 	bool rv = true;
33427c478bd9Sstevel@tonic-gate 	ADDRESS *a;
33437c478bd9Sstevel@tonic-gate 	ENVELOPE *ee;
33447c478bd9Sstevel@tonic-gate 	char *id;
33457c478bd9Sstevel@tonic-gate 	char *oldid;
3346058561cbSjbeck 	unsigned int features;
33477c478bd9Sstevel@tonic-gate 	char buf[32];
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate 	SmtpPhase = "server DATA";
33507c478bd9Sstevel@tonic-gate 	if (!smtp->sm_gotmail)
33517c478bd9Sstevel@tonic-gate 	{
33527c478bd9Sstevel@tonic-gate 		usrerr("503 5.0.0 Need MAIL command");
33537c478bd9Sstevel@tonic-gate 		return true;
33547c478bd9Sstevel@tonic-gate 	}
33557c478bd9Sstevel@tonic-gate 	else if (smtp->sm_nrcpts <= 0)
33567c478bd9Sstevel@tonic-gate 	{
33577c478bd9Sstevel@tonic-gate 		usrerr("503 5.0.0 Need RCPT (recipient)");
33587c478bd9Sstevel@tonic-gate 		return true;
33597c478bd9Sstevel@tonic-gate 	}
3360058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf), "%u", smtp->sm_nrcpts);
33617c478bd9Sstevel@tonic-gate 	if (rscheck("check_data", buf, NULL, e,
33627c478bd9Sstevel@tonic-gate 		    RSF_RMCOMM|RSF_UNSTRUCTURED|RSF_COUNT, 3, NULL,
3363058561cbSjbeck 		    e->e_id, NULL) != EX_OK)
33647c478bd9Sstevel@tonic-gate 		return true;
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate #if MILTER && SMFI_VERSION > 3
33677c478bd9Sstevel@tonic-gate 	if (smtp->sm_milterlist && smtp->sm_milterize &&
33687c478bd9Sstevel@tonic-gate 	    !bitset(EF_DISCARD, e->e_flags))
33697c478bd9Sstevel@tonic-gate 	{
33707c478bd9Sstevel@tonic-gate 		char state;
33717c478bd9Sstevel@tonic-gate 		char *response;
33727c478bd9Sstevel@tonic-gate 		int savelogusrerrs = LogUsrErrs;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 		response = milter_data_cmd(e, &state);
33757c478bd9Sstevel@tonic-gate 		switch (state)
33767c478bd9Sstevel@tonic-gate 		{
33777c478bd9Sstevel@tonic-gate 		  case SMFIR_REPLYCODE:
33787c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
33797c478bd9Sstevel@tonic-gate 			{
33807c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
33817c478bd9Sstevel@tonic-gate 					  "Milter: cmd=data, reject=%s",
33827c478bd9Sstevel@tonic-gate 					  response);
33837c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;
33847c478bd9Sstevel@tonic-gate 			}
3385*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3386*e9af4bc0SJohn Beck 			if (ISSMTPCODE(response))
3387*e9af4bc0SJohn Beck 				(void) extenhsc(response + 4, ' ', e->e_enhsc);
3388*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
3389*e9af4bc0SJohn Beck 
33907c478bd9Sstevel@tonic-gate 			usrerr(response);
339149218d4fSjbeck 			if (strncmp(response, "421 ", 4) == 0
339249218d4fSjbeck 			    || strncmp(response, "421-", 4) == 0)
33937c478bd9Sstevel@tonic-gate 			{
33947c478bd9Sstevel@tonic-gate 				e->e_sendqueue = NULL;
33957c478bd9Sstevel@tonic-gate 				return false;
33967c478bd9Sstevel@tonic-gate 			}
33977c478bd9Sstevel@tonic-gate 			return true;
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 		  case SMFIR_REJECT:
34007c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
34017c478bd9Sstevel@tonic-gate 			{
34027c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
34037c478bd9Sstevel@tonic-gate 					  "Milter: cmd=data, reject=550 5.7.1 Command rejected");
34047c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;
34057c478bd9Sstevel@tonic-gate 			}
3406*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3407*e9af4bc0SJohn Beck 			(void) sm_strlcpy(e->e_enhsc, "5.7.1",
3408*e9af4bc0SJohn Beck 					 sizeof(e->e_enhsc));
3409*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
34107c478bd9Sstevel@tonic-gate 			usrerr("550 5.7.1 Command rejected");
34117c478bd9Sstevel@tonic-gate 			return true;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 		  case SMFIR_DISCARD:
34147c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
34157c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
34167c478bd9Sstevel@tonic-gate 					  "Milter: cmd=data, discard");
34177c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_DISCARD;
34187c478bd9Sstevel@tonic-gate 			break;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 		  case SMFIR_TEMPFAIL:
34217c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
34227c478bd9Sstevel@tonic-gate 			{
34237c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
34247c478bd9Sstevel@tonic-gate 					  "Milter: cmd=data, reject=%s",
34257c478bd9Sstevel@tonic-gate 					  MSG_TEMPFAIL);
34267c478bd9Sstevel@tonic-gate 				LogUsrErrs = false;
34277c478bd9Sstevel@tonic-gate 			}
3428*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3429*e9af4bc0SJohn Beck 			(void) extenhsc(MSG_TEMPFAIL + 4, ' ', e->e_enhsc);
3430*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
34317c478bd9Sstevel@tonic-gate 			usrerr(MSG_TEMPFAIL);
34327c478bd9Sstevel@tonic-gate 			return true;
3433445f2479Sjbeck 
3434445f2479Sjbeck 		  case SMFIR_SHUTDOWN:
3435445f2479Sjbeck 			if (MilterLogLevel > 3)
3436445f2479Sjbeck 			{
3437445f2479Sjbeck 				sm_syslog(LOG_INFO, e->e_id,
3438445f2479Sjbeck 					  "Milter: cmd=data, reject=421 4.7.0 %s closing connection",
3439445f2479Sjbeck 					  MyHostName);
3440445f2479Sjbeck 				LogUsrErrs = false;
3441445f2479Sjbeck 			}
3442445f2479Sjbeck 			usrerr("421 4.7.0 %s closing connection", MyHostName);
3443445f2479Sjbeck 			e->e_sendqueue = NULL;
3444445f2479Sjbeck 			return false;
34457c478bd9Sstevel@tonic-gate 		}
34467c478bd9Sstevel@tonic-gate 		LogUsrErrs = savelogusrerrs;
34477c478bd9Sstevel@tonic-gate 		if (response != NULL)
34487c478bd9Sstevel@tonic-gate 			sm_free(response); /* XXX */
34497c478bd9Sstevel@tonic-gate 	}
34507c478bd9Sstevel@tonic-gate #endif /* MILTER && SMFI_VERSION > 3 */
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	/* put back discard bit */
34537c478bd9Sstevel@tonic-gate 	if (smtp->sm_discard)
34547c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_DISCARD;
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	/* check to see if we need to re-expand aliases */
34577c478bd9Sstevel@tonic-gate 	/* also reset QS_BADADDR on already-diagnosted addrs */
34587c478bd9Sstevel@tonic-gate 	doublequeue = false;
34597c478bd9Sstevel@tonic-gate 	for (a = e->e_sendqueue; a != NULL; a = a->q_next)
34607c478bd9Sstevel@tonic-gate 	{
34617c478bd9Sstevel@tonic-gate 		if (QS_IS_VERIFIED(a->q_state) &&
34627c478bd9Sstevel@tonic-gate 		    !bitset(EF_DISCARD, e->e_flags))
34637c478bd9Sstevel@tonic-gate 		{
34647c478bd9Sstevel@tonic-gate 			/* need to re-expand aliases */
34657c478bd9Sstevel@tonic-gate 			doublequeue = true;
34667c478bd9Sstevel@tonic-gate 		}
34677c478bd9Sstevel@tonic-gate 		if (QS_IS_BADADDR(a->q_state))
34687c478bd9Sstevel@tonic-gate 		{
34697c478bd9Sstevel@tonic-gate 			/* make this "go away" */
34707c478bd9Sstevel@tonic-gate 			a->q_state = QS_DONTSEND;
34717c478bd9Sstevel@tonic-gate 		}
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	/* collect the text of the message */
34757c478bd9Sstevel@tonic-gate 	SmtpPhase = "collect";
34767c478bd9Sstevel@tonic-gate 	buffer_errors();
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	collect(InChannel, true, NULL, e, true);
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	/* redefine message size */
3481058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf), "%ld", e->e_msgsize);
34827c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf);
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	/* rscheck() will set Errors or EF_DISCARD if it trips */
34857c478bd9Sstevel@tonic-gate 	(void) rscheck("check_eom", buf, NULL, e, RSF_UNSTRUCTURED|RSF_COUNT,
3486058561cbSjbeck 		       3, NULL, e->e_id, NULL);
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate #if MILTER
34897c478bd9Sstevel@tonic-gate 	milteraccept = true;
34907c478bd9Sstevel@tonic-gate 	if (smtp->sm_milterlist && smtp->sm_milterize &&
34917c478bd9Sstevel@tonic-gate 	    Errors <= 0 &&
34927c478bd9Sstevel@tonic-gate 	    !bitset(EF_DISCARD, e->e_flags))
34937c478bd9Sstevel@tonic-gate 	{
34947c478bd9Sstevel@tonic-gate 		char state;
34957c478bd9Sstevel@tonic-gate 		char *response;
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 		response = milter_data(e, &state);
34987c478bd9Sstevel@tonic-gate 		switch (state)
34997c478bd9Sstevel@tonic-gate 		{
35007c478bd9Sstevel@tonic-gate 		  case SMFIR_REPLYCODE:
35017c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
35027c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
35037c478bd9Sstevel@tonic-gate 					  "Milter: data, reject=%s",
35047c478bd9Sstevel@tonic-gate 					  response);
35057c478bd9Sstevel@tonic-gate 			milteraccept = false;
3506*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3507*e9af4bc0SJohn Beck 			if (ISSMTPCODE(response))
3508*e9af4bc0SJohn Beck 				(void) extenhsc(response + 4, ' ', e->e_enhsc);
3509*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
35107c478bd9Sstevel@tonic-gate 			usrerr(response);
3511*e9af4bc0SJohn Beck 			if (strncmp(response, "421 ", 4) == 0
3512*e9af4bc0SJohn Beck 			    || strncmp(response, "421-", 4) == 0)
3513*e9af4bc0SJohn Beck 				rv = false;
35147c478bd9Sstevel@tonic-gate 			break;
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 		  case SMFIR_REJECT:
35177c478bd9Sstevel@tonic-gate 			milteraccept = false;
35187c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
35197c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
35207c478bd9Sstevel@tonic-gate 					  "Milter: data, reject=554 5.7.1 Command rejected");
35217c478bd9Sstevel@tonic-gate 			usrerr("554 5.7.1 Command rejected");
35227c478bd9Sstevel@tonic-gate 			break;
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 		  case SMFIR_DISCARD:
35257c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
35267c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
35277c478bd9Sstevel@tonic-gate 					  "Milter: data, discard");
35287c478bd9Sstevel@tonic-gate 			milteraccept = false;
35297c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_DISCARD;
35307c478bd9Sstevel@tonic-gate 			break;
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 		  case SMFIR_TEMPFAIL:
35337c478bd9Sstevel@tonic-gate 			if (MilterLogLevel > 3)
35347c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
35357c478bd9Sstevel@tonic-gate 					  "Milter: data, reject=%s",
35367c478bd9Sstevel@tonic-gate 					  MSG_TEMPFAIL);
35377c478bd9Sstevel@tonic-gate 			milteraccept = false;
3538*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3539*e9af4bc0SJohn Beck 			(void) extenhsc(MSG_TEMPFAIL + 4, ' ', e->e_enhsc);
3540*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
35417c478bd9Sstevel@tonic-gate 			usrerr(MSG_TEMPFAIL);
35427c478bd9Sstevel@tonic-gate 			break;
3543445f2479Sjbeck 
3544445f2479Sjbeck 		  case SMFIR_SHUTDOWN:
3545445f2479Sjbeck 			if (MilterLogLevel > 3)
3546445f2479Sjbeck 				sm_syslog(LOG_INFO, e->e_id,
3547445f2479Sjbeck 					  "Milter: data, reject=421 4.7.0 %s closing connection",
3548445f2479Sjbeck 					  MyHostName);
3549445f2479Sjbeck 			milteraccept = false;
3550445f2479Sjbeck 			usrerr("421 4.7.0 %s closing connection", MyHostName);
3551445f2479Sjbeck 			rv = false;
3552445f2479Sjbeck 			break;
35537c478bd9Sstevel@tonic-gate 		}
35547c478bd9Sstevel@tonic-gate 		if (response != NULL)
35557c478bd9Sstevel@tonic-gate 			sm_free(response);
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	/* Milter may have changed message size */
3559058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf), "%ld", e->e_msgsize);
35607c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf);
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 	/* abort message filters that didn't get the body & log msg is OK */
35637c478bd9Sstevel@tonic-gate 	if (smtp->sm_milterlist && smtp->sm_milterize)
35647c478bd9Sstevel@tonic-gate 	{
35657c478bd9Sstevel@tonic-gate 		milter_abort(e);
35667c478bd9Sstevel@tonic-gate 		if (milteraccept && MilterLogLevel > 9)
35677c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id, "Milter accept: message");
35687c478bd9Sstevel@tonic-gate 	}
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 	/*
35717c478bd9Sstevel@tonic-gate 	**  If SuperSafe is SAFE_REALLY_POSTMILTER, and we don't have milter or
35727c478bd9Sstevel@tonic-gate 	**  milter accepted message, sync it now
35737c478bd9Sstevel@tonic-gate 	**
35747c478bd9Sstevel@tonic-gate 	**  XXX This is almost a copy of the code in collect(): put it into
35757c478bd9Sstevel@tonic-gate 	**	a function that is called from both places?
35767c478bd9Sstevel@tonic-gate 	*/
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 	if (milteraccept && SuperSafe == SAFE_REALLY_POSTMILTER)
35797c478bd9Sstevel@tonic-gate 	{
35807c478bd9Sstevel@tonic-gate 		int afd;
35817c478bd9Sstevel@tonic-gate 		SM_FILE_T *volatile df;
35827c478bd9Sstevel@tonic-gate 		char *dfname;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 		df = e->e_dfp;
35857c478bd9Sstevel@tonic-gate 		dfname = queuename(e, DATAFL_LETTER);
35867c478bd9Sstevel@tonic-gate 		if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0
35877c478bd9Sstevel@tonic-gate 		    && errno != EINVAL)
35887c478bd9Sstevel@tonic-gate 		{
35897c478bd9Sstevel@tonic-gate 			int save_errno;
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 			save_errno = errno;
35927c478bd9Sstevel@tonic-gate 			if (save_errno == EEXIST)
35937c478bd9Sstevel@tonic-gate 			{
35947c478bd9Sstevel@tonic-gate 				struct stat st;
35957c478bd9Sstevel@tonic-gate 				int dfd;
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 				if (stat(dfname, &st) < 0)
35987c478bd9Sstevel@tonic-gate 					st.st_size = -1;
35997c478bd9Sstevel@tonic-gate 				errno = EEXIST;
36007c478bd9Sstevel@tonic-gate 				syserr("@collect: bfcommit(%s): already on disk, size=%ld",
36017c478bd9Sstevel@tonic-gate 				       dfname, (long) st.st_size);
36027c478bd9Sstevel@tonic-gate 				dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
36037c478bd9Sstevel@tonic-gate 				if (dfd >= 0)
36047c478bd9Sstevel@tonic-gate 					dumpfd(dfd, true, true);
36057c478bd9Sstevel@tonic-gate 			}
36067c478bd9Sstevel@tonic-gate 			errno = save_errno;
36077c478bd9Sstevel@tonic-gate 			dferror(df, "bfcommit", e);
36087c478bd9Sstevel@tonic-gate 			flush_errors(true);
36097c478bd9Sstevel@tonic-gate 			finis(save_errno != EEXIST, true, ExitStat);
36107c478bd9Sstevel@tonic-gate 		}
36117c478bd9Sstevel@tonic-gate 		else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
36127c478bd9Sstevel@tonic-gate 		{
36137c478bd9Sstevel@tonic-gate 			dferror(df, "sm_io_getinfo", e);
36147c478bd9Sstevel@tonic-gate 			flush_errors(true);
36157c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
36167c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
36177c478bd9Sstevel@tonic-gate 		}
36187c478bd9Sstevel@tonic-gate 		else if (fsync(afd) < 0)
36197c478bd9Sstevel@tonic-gate 		{
36207c478bd9Sstevel@tonic-gate 			dferror(df, "fsync", e);
36217c478bd9Sstevel@tonic-gate 			flush_errors(true);
36227c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
36237c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
36247c478bd9Sstevel@tonic-gate 		}
36257c478bd9Sstevel@tonic-gate 		else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
36267c478bd9Sstevel@tonic-gate 		{
36277c478bd9Sstevel@tonic-gate 			dferror(df, "sm_io_close", e);
36287c478bd9Sstevel@tonic-gate 			flush_errors(true);
36297c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
36307c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
36317c478bd9Sstevel@tonic-gate 		}
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 		/* Now reopen the df file */
36347c478bd9Sstevel@tonic-gate 		e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
36357c478bd9Sstevel@tonic-gate 					SM_IO_RDONLY, NULL);
36367c478bd9Sstevel@tonic-gate 		if (e->e_dfp == NULL)
36377c478bd9Sstevel@tonic-gate 		{
36387c478bd9Sstevel@tonic-gate 			/* we haven't acked receipt yet, so just chuck this */
36397c478bd9Sstevel@tonic-gate 			syserr("@Cannot reopen %s", dfname);
36407c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
36417c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
36427c478bd9Sstevel@tonic-gate 		}
36437c478bd9Sstevel@tonic-gate 	}
36447c478bd9Sstevel@tonic-gate #endif /* MILTER */
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 	/* Check if quarantining stats should be updated */
36477c478bd9Sstevel@tonic-gate 	if (e->e_quarmsg != NULL)
36487c478bd9Sstevel@tonic-gate 		markstats(e, NULL, STATS_QUARANTINE);
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 	/*
36517c478bd9Sstevel@tonic-gate 	**  If a header/body check (header checks or milter)
36527c478bd9Sstevel@tonic-gate 	**  set EF_DISCARD, don't queueup the message --
36537c478bd9Sstevel@tonic-gate 	**  that would lose the EF_DISCARD bit and deliver
36547c478bd9Sstevel@tonic-gate 	**  the message.
36557c478bd9Sstevel@tonic-gate 	*/
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 	if (bitset(EF_DISCARD, e->e_flags))
36587c478bd9Sstevel@tonic-gate 		doublequeue = false;
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 	aborting = Errors > 0;
36617c478bd9Sstevel@tonic-gate 	if (!(aborting || bitset(EF_DISCARD, e->e_flags)) &&
36627c478bd9Sstevel@tonic-gate 	    (QueueMode == QM_QUARANTINE || e->e_quarmsg == NULL) &&
36637c478bd9Sstevel@tonic-gate 	    !split_by_recipient(e))
36647c478bd9Sstevel@tonic-gate 		aborting = bitset(EF_FATALERRS, e->e_flags);
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	if (aborting)
36677c478bd9Sstevel@tonic-gate 	{
36687800901eSjbeck 		ADDRESS *q;
36697800901eSjbeck 
36707c478bd9Sstevel@tonic-gate 		/* Log who the mail would have gone to */
36717c478bd9Sstevel@tonic-gate 		logundelrcpts(e, e->e_message, 8, false);
36727800901eSjbeck 
36737800901eSjbeck 		/*
36747800901eSjbeck 		**  If something above refused the message, we still haven't
36757800901eSjbeck 		**  accepted responsibility for it.  Don't send DSNs.
36767800901eSjbeck 		*/
36777800901eSjbeck 
36787800901eSjbeck 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
36797800901eSjbeck 			q->q_flags &= ~Q_PINGFLAGS;
36807800901eSjbeck 
36817c478bd9Sstevel@tonic-gate 		flush_errors(true);
36827c478bd9Sstevel@tonic-gate 		buffer_errors();
36837c478bd9Sstevel@tonic-gate 		goto abortmessage;
36847c478bd9Sstevel@tonic-gate 	}
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 	/* from now on, we have to operate silently */
36877c478bd9Sstevel@tonic-gate 	buffer_errors();
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate #if 0
36907c478bd9Sstevel@tonic-gate 	/*
36917c478bd9Sstevel@tonic-gate 	**  Clear message, it may contain an error from the SMTP dialogue.
36927c478bd9Sstevel@tonic-gate 	**  This error must not show up in the queue.
36937c478bd9Sstevel@tonic-gate 	**	Some error message should show up, e.g., alias database
36947c478bd9Sstevel@tonic-gate 	**	not available, but others shouldn't, e.g., from check_rcpt.
36957c478bd9Sstevel@tonic-gate 	*/
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	e->e_message = NULL;
36987c478bd9Sstevel@tonic-gate #endif /* 0 */
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 	/*
37017c478bd9Sstevel@tonic-gate 	**  Arrange to send to everyone.
37027c478bd9Sstevel@tonic-gate 	**	If sending to multiple people, mail back
37037c478bd9Sstevel@tonic-gate 	**		errors rather than reporting directly.
37047c478bd9Sstevel@tonic-gate 	**	In any case, don't mail back errors for
37057c478bd9Sstevel@tonic-gate 	**		anything that has happened up to
37067c478bd9Sstevel@tonic-gate 	**		now (the other end will do this).
37077c478bd9Sstevel@tonic-gate 	**	Truncate our transcript -- the mail has gotten
37087c478bd9Sstevel@tonic-gate 	**		to us successfully, and if we have
37097c478bd9Sstevel@tonic-gate 	**		to mail this back, it will be easier
37107c478bd9Sstevel@tonic-gate 	**		on the reader.
37117c478bd9Sstevel@tonic-gate 	**	Then send to everyone.
37127c478bd9Sstevel@tonic-gate 	**	Finally give a reply code.  If an error has
37137c478bd9Sstevel@tonic-gate 	**		already been given, don't mail a
37147c478bd9Sstevel@tonic-gate 	**		message back.
37157c478bd9Sstevel@tonic-gate 	**	We goose error returns by clearing error bit.
37167c478bd9Sstevel@tonic-gate 	*/
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate 	SmtpPhase = "delivery";
37197c478bd9Sstevel@tonic-gate 	(void) sm_io_setinfo(e->e_xfp, SM_BF_TRUNCATE, NULL);
37207c478bd9Sstevel@tonic-gate 	id = e->e_id;
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate #if NAMED_BIND
37237c478bd9Sstevel@tonic-gate 	_res.retry = TimeOuts.res_retry[RES_TO_FIRST];
37247c478bd9Sstevel@tonic-gate 	_res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
37257c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	for (ee = e; ee != NULL; ee = ee->e_sibling)
37287c478bd9Sstevel@tonic-gate 	{
37297c478bd9Sstevel@tonic-gate 		/* make sure we actually do delivery */
37307c478bd9Sstevel@tonic-gate 		ee->e_flags &= ~EF_CLRQUEUE;
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 		/* from now on, operate silently */
37337c478bd9Sstevel@tonic-gate 		ee->e_errormode = EM_MAIL;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 		if (doublequeue)
37367c478bd9Sstevel@tonic-gate 		{
37377c478bd9Sstevel@tonic-gate 			/* make sure it is in the queue */
37387c478bd9Sstevel@tonic-gate 			queueup(ee, false, true);
37397c478bd9Sstevel@tonic-gate 		}
37407c478bd9Sstevel@tonic-gate 		else
37417c478bd9Sstevel@tonic-gate 		{
3742445f2479Sjbeck 			int mode;
3743445f2479Sjbeck 
37447c478bd9Sstevel@tonic-gate 			/* send to all recipients */
3745445f2479Sjbeck 			mode = SM_DEFAULT;
3746445f2479Sjbeck #if _FFR_DM_ONE
3747445f2479Sjbeck 			if (SM_DM_ONE == e->e_sendmode)
3748445f2479Sjbeck 			{
3749445f2479Sjbeck 				if (NotFirstDelivery)
3750445f2479Sjbeck 				{
3751445f2479Sjbeck 					mode = SM_QUEUE;
3752445f2479Sjbeck 					e->e_sendmode = SM_QUEUE;
3753445f2479Sjbeck 				}
3754445f2479Sjbeck 				else
3755445f2479Sjbeck 				{
3756445f2479Sjbeck 					mode = SM_FORK;
3757445f2479Sjbeck 					NotFirstDelivery = true;
3758445f2479Sjbeck 				}
3759445f2479Sjbeck 			}
3760445f2479Sjbeck #endif /* _FFR_DM_ONE */
3761445f2479Sjbeck 			sendall(ee, mode);
37627c478bd9Sstevel@tonic-gate 		}
37637c478bd9Sstevel@tonic-gate 		ee->e_to = NULL;
37647c478bd9Sstevel@tonic-gate 	}
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	/* put back id for SMTP logging in putoutmsg() */
37677c478bd9Sstevel@tonic-gate 	oldid = CurEnv->e_id;
37687c478bd9Sstevel@tonic-gate 	CurEnv->e_id = id;
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 	/* issue success message */
3771445f2479Sjbeck #if _FFR_MSG_ACCEPT
3772445f2479Sjbeck 	if (MessageAccept != NULL && *MessageAccept != '\0')
3773445f2479Sjbeck 	{
3774445f2479Sjbeck 		char msg[MAXLINE];
3775445f2479Sjbeck 
3776058561cbSjbeck 		expand(MessageAccept, msg, sizeof(msg), e);
3777445f2479Sjbeck 		message("250 2.0.0 %s", msg);
3778445f2479Sjbeck 	}
3779445f2479Sjbeck 	else
3780445f2479Sjbeck #endif /* _FFR_MSG_ACCEPT */
37817c478bd9Sstevel@tonic-gate 	message("250 2.0.0 %s Message accepted for delivery", id);
37827c478bd9Sstevel@tonic-gate 	CurEnv->e_id = oldid;
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	/* if we just queued, poke it */
37857c478bd9Sstevel@tonic-gate 	if (doublequeue)
37867c478bd9Sstevel@tonic-gate 	{
37877c478bd9Sstevel@tonic-gate 		bool anything_to_send = false;
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 		sm_getla();
37907c478bd9Sstevel@tonic-gate 		for (ee = e; ee != NULL; ee = ee->e_sibling)
37917c478bd9Sstevel@tonic-gate 		{
37927c478bd9Sstevel@tonic-gate 			if (WILL_BE_QUEUED(ee->e_sendmode))
37937c478bd9Sstevel@tonic-gate 				continue;
37947c478bd9Sstevel@tonic-gate 			if (shouldqueue(ee->e_msgpriority, ee->e_ctime))
37957c478bd9Sstevel@tonic-gate 			{
37967c478bd9Sstevel@tonic-gate 				ee->e_sendmode = SM_QUEUE;
37977c478bd9Sstevel@tonic-gate 				continue;
37987c478bd9Sstevel@tonic-gate 			}
37997c478bd9Sstevel@tonic-gate 			else if (QueueMode != QM_QUARANTINE &&
38007c478bd9Sstevel@tonic-gate 				 ee->e_quarmsg != NULL)
38017c478bd9Sstevel@tonic-gate 			{
38027c478bd9Sstevel@tonic-gate 				ee->e_sendmode = SM_QUEUE;
38037c478bd9Sstevel@tonic-gate 				continue;
38047c478bd9Sstevel@tonic-gate 			}
38057c478bd9Sstevel@tonic-gate 			anything_to_send = true;
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 			/* close all the queue files */
38087c478bd9Sstevel@tonic-gate 			closexscript(ee);
38097c478bd9Sstevel@tonic-gate 			if (ee->e_dfp != NULL)
38107c478bd9Sstevel@tonic-gate 			{
38117c478bd9Sstevel@tonic-gate 				(void) sm_io_close(ee->e_dfp, SM_TIME_DEFAULT);
38127c478bd9Sstevel@tonic-gate 				ee->e_dfp = NULL;
38137c478bd9Sstevel@tonic-gate 			}
38147c478bd9Sstevel@tonic-gate 			unlockqueue(ee);
38157c478bd9Sstevel@tonic-gate 		}
38167c478bd9Sstevel@tonic-gate 		if (anything_to_send)
38177c478bd9Sstevel@tonic-gate 		{
38187c478bd9Sstevel@tonic-gate #if PIPELINING
38197c478bd9Sstevel@tonic-gate 			/*
38207c478bd9Sstevel@tonic-gate 			**  XXX if we don't do this, we get 250 twice
38217c478bd9Sstevel@tonic-gate 			**	because it is also flushed in the child.
38227c478bd9Sstevel@tonic-gate 			*/
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 			(void) sm_io_flush(OutChannel, SM_TIME_DEFAULT);
38257c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
38267c478bd9Sstevel@tonic-gate 			(void) doworklist(e, true, true);
38277c478bd9Sstevel@tonic-gate 		}
38287c478bd9Sstevel@tonic-gate 	}
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate   abortmessage:
3831*e9af4bc0SJohn Beck 	if (tTd(92, 2))
3832*e9af4bc0SJohn Beck 		sm_dprintf("abortmessage: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
3833*e9af4bc0SJohn Beck 			e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
38347c478bd9Sstevel@tonic-gate 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
38357c478bd9Sstevel@tonic-gate 		logsender(e, NULL);
38367c478bd9Sstevel@tonic-gate 	e->e_flags &= ~EF_LOGSENDER;
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	/* clean up a bit */
38397c478bd9Sstevel@tonic-gate 	smtp->sm_gotmail = false;
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 	/*
38427c478bd9Sstevel@tonic-gate 	**  Call dropenvelope if and only if the envelope is *not*
38437c478bd9Sstevel@tonic-gate 	**  being processed by the child process forked by doworklist().
38447c478bd9Sstevel@tonic-gate 	*/
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate 	if (aborting || bitset(EF_DISCARD, e->e_flags))
3847*e9af4bc0SJohn Beck 		(void) dropenvelope(e, true, false);
38487c478bd9Sstevel@tonic-gate 	else
38497c478bd9Sstevel@tonic-gate 	{
38507c478bd9Sstevel@tonic-gate 		for (ee = e; ee != NULL; ee = ee->e_sibling)
38517c478bd9Sstevel@tonic-gate 		{
38527c478bd9Sstevel@tonic-gate 			if (!doublequeue &&
38537c478bd9Sstevel@tonic-gate 			    QueueMode != QM_QUARANTINE &&
38547c478bd9Sstevel@tonic-gate 			    ee->e_quarmsg != NULL)
38557c478bd9Sstevel@tonic-gate 			{
3856*e9af4bc0SJohn Beck 				(void) dropenvelope(ee, true, false);
38577c478bd9Sstevel@tonic-gate 				continue;
38587c478bd9Sstevel@tonic-gate 			}
38597c478bd9Sstevel@tonic-gate 			if (WILL_BE_QUEUED(ee->e_sendmode))
3860*e9af4bc0SJohn Beck 				(void) dropenvelope(ee, true, false);
38617c478bd9Sstevel@tonic-gate 		}
38627c478bd9Sstevel@tonic-gate 	}
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	CurEnv = e;
3865058561cbSjbeck 	features = e->e_features;
3866d4660949Sjbeck 	sm_rpool_free(e->e_rpool);
38677c478bd9Sstevel@tonic-gate 	newenvelope(e, e, sm_rpool_new_x(NULL));
38687c478bd9Sstevel@tonic-gate 	e->e_flags = BlankEnvelope.e_flags;
3869058561cbSjbeck 	e->e_features = features;
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	/* restore connection quarantining */
38727c478bd9Sstevel@tonic-gate 	if (smtp->sm_quarmsg == NULL)
38737c478bd9Sstevel@tonic-gate 	{
38747c478bd9Sstevel@tonic-gate 		e->e_quarmsg = NULL;
38757c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), "");
38767c478bd9Sstevel@tonic-gate 	}
38777c478bd9Sstevel@tonic-gate 	else
38787c478bd9Sstevel@tonic-gate 	{
38797c478bd9Sstevel@tonic-gate 		e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, smtp->sm_quarmsg);
38807c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM,
38817c478bd9Sstevel@tonic-gate 			  macid("{quarantine}"), e->e_quarmsg);
38827c478bd9Sstevel@tonic-gate 	}
3883445f2479Sjbeck 	return rv;
38847c478bd9Sstevel@tonic-gate }
38857c478bd9Sstevel@tonic-gate /*
38867c478bd9Sstevel@tonic-gate **  LOGUNDELRCPTS -- log undelivered (or all) recipients.
38877c478bd9Sstevel@tonic-gate **
38887c478bd9Sstevel@tonic-gate **	Parameters:
38897c478bd9Sstevel@tonic-gate **		e -- envelope.
38907c478bd9Sstevel@tonic-gate **		msg -- message for Stat=
38917c478bd9Sstevel@tonic-gate **		level -- log level.
38927c478bd9Sstevel@tonic-gate **		all -- log all recipients.
38937c478bd9Sstevel@tonic-gate **
38947c478bd9Sstevel@tonic-gate **	Returns:
38957c478bd9Sstevel@tonic-gate **		none.
38967c478bd9Sstevel@tonic-gate **
38977c478bd9Sstevel@tonic-gate **	Side Effects:
38987c478bd9Sstevel@tonic-gate **		logs undelivered (or all) recipients
38997c478bd9Sstevel@tonic-gate */
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate void
logundelrcpts(e,msg,level,all)39027c478bd9Sstevel@tonic-gate logundelrcpts(e, msg, level, all)
39037c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
39047c478bd9Sstevel@tonic-gate 	char *msg;
39057c478bd9Sstevel@tonic-gate 	int level;
39067c478bd9Sstevel@tonic-gate 	bool all;
39077c478bd9Sstevel@tonic-gate {
39087c478bd9Sstevel@tonic-gate 	ADDRESS *a;
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	if (LogLevel <= level || msg == NULL || *msg == '\0')
39117c478bd9Sstevel@tonic-gate 		return;
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	/* Clear $h so relay= doesn't get mislogged by logdelivery() */
39147c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_PERM, 'h', NULL);
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	/* Log who the mail would have gone to */
39177c478bd9Sstevel@tonic-gate 	for (a = e->e_sendqueue; a != NULL; a = a->q_next)
39187c478bd9Sstevel@tonic-gate 	{
39197c478bd9Sstevel@tonic-gate 		if (!QS_IS_UNDELIVERED(a->q_state) && !all)
39207c478bd9Sstevel@tonic-gate 			continue;
39217c478bd9Sstevel@tonic-gate 		e->e_to = a->q_paddr;
3922*e9af4bc0SJohn Beck 		logdelivery(NULL, NULL,
3923*e9af4bc0SJohn Beck #if _FFR_MILTER_ENHSC
3924*e9af4bc0SJohn Beck 			    (a->q_status == NULL && e->e_enhsc[0] != '\0')
3925*e9af4bc0SJohn Beck 			    ? e->e_enhsc :
3926*e9af4bc0SJohn Beck #endif /* _FFR_MILTER_ENHSC */
3927*e9af4bc0SJohn Beck 			    a->q_status,
3928*e9af4bc0SJohn Beck 			    msg, NULL, (time_t) 0, e);
39297c478bd9Sstevel@tonic-gate 	}
39307c478bd9Sstevel@tonic-gate 	e->e_to = NULL;
39317c478bd9Sstevel@tonic-gate }
39327c478bd9Sstevel@tonic-gate /*
39337c478bd9Sstevel@tonic-gate **  CHECKSMTPATTACK -- check for denial-of-service attack by repetition
39347c478bd9Sstevel@tonic-gate **
39357c478bd9Sstevel@tonic-gate **	Parameters:
39367c478bd9Sstevel@tonic-gate **		pcounter -- pointer to a counter for this command.
39377c478bd9Sstevel@tonic-gate **		maxcount -- maximum value for this counter before we
39387c478bd9Sstevel@tonic-gate **			slow down.
39397c478bd9Sstevel@tonic-gate **		waitnow -- sleep now (in this routine)?
39407c478bd9Sstevel@tonic-gate **		cname -- command name for logging.
39417c478bd9Sstevel@tonic-gate **		e -- the current envelope.
39427c478bd9Sstevel@tonic-gate **
39437c478bd9Sstevel@tonic-gate **	Returns:
39447c478bd9Sstevel@tonic-gate **		time to wait,
39457c478bd9Sstevel@tonic-gate **		STOP_ATTACK if twice as many commands as allowed and
39467c478bd9Sstevel@tonic-gate **			MaxChildren > 0.
39477c478bd9Sstevel@tonic-gate **
39487c478bd9Sstevel@tonic-gate **	Side Effects:
39497c478bd9Sstevel@tonic-gate **		Slows down if we seem to be under attack.
39507c478bd9Sstevel@tonic-gate */
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate static time_t
checksmtpattack(pcounter,maxcount,waitnow,cname,e)39537c478bd9Sstevel@tonic-gate checksmtpattack(pcounter, maxcount, waitnow, cname, e)
39547c478bd9Sstevel@tonic-gate 	volatile unsigned int *pcounter;
39557c478bd9Sstevel@tonic-gate 	unsigned int maxcount;
39567c478bd9Sstevel@tonic-gate 	bool waitnow;
39577c478bd9Sstevel@tonic-gate 	char *cname;
39587c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
39597c478bd9Sstevel@tonic-gate {
39607c478bd9Sstevel@tonic-gate 	if (maxcount <= 0)	/* no limit */
39617c478bd9Sstevel@tonic-gate 		return (time_t) 0;
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	if (++(*pcounter) >= maxcount)
39647c478bd9Sstevel@tonic-gate 	{
39657c478bd9Sstevel@tonic-gate 		unsigned int shift;
39667c478bd9Sstevel@tonic-gate 		time_t s;
39677c478bd9Sstevel@tonic-gate 
39687c478bd9Sstevel@tonic-gate 		if (*pcounter == maxcount && LogLevel > 5)
39697c478bd9Sstevel@tonic-gate 		{
39707c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id,
39717c478bd9Sstevel@tonic-gate 				  "%s: possible SMTP attack: command=%.40s, count=%u",
39727c478bd9Sstevel@tonic-gate 				  CurSmtpClient, cname, *pcounter);
39737c478bd9Sstevel@tonic-gate 		}
39747c478bd9Sstevel@tonic-gate 		shift = *pcounter - maxcount;
39757c478bd9Sstevel@tonic-gate 		s = 1 << shift;
39767c478bd9Sstevel@tonic-gate 		if (shift > MAXSHIFT || s >= MAXTIMEOUT || s <= 0)
39777c478bd9Sstevel@tonic-gate 			s = MAXTIMEOUT;
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate #define IS_ATTACK(s)	((MaxChildren > 0 && *pcounter >= maxcount * 2)	\
39807c478bd9Sstevel@tonic-gate 				? STOP_ATTACK : (time_t) s)
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 		/* sleep at least 1 second before returning */
39837c478bd9Sstevel@tonic-gate 		(void) sleep(*pcounter / maxcount);
39847c478bd9Sstevel@tonic-gate 		s -= *pcounter / maxcount;
39857c478bd9Sstevel@tonic-gate 		if (s >= MAXTIMEOUT || s < 0)
39867c478bd9Sstevel@tonic-gate 			s = MAXTIMEOUT;
39877c478bd9Sstevel@tonic-gate 		if (waitnow && s > 0)
39887c478bd9Sstevel@tonic-gate 		{
39897c478bd9Sstevel@tonic-gate 			(void) sleep(s);
39907c478bd9Sstevel@tonic-gate 			return IS_ATTACK(0);
39917c478bd9Sstevel@tonic-gate 		}
39927c478bd9Sstevel@tonic-gate 		return IS_ATTACK(s);
39937c478bd9Sstevel@tonic-gate 	}
39947c478bd9Sstevel@tonic-gate 	return (time_t) 0;
39957c478bd9Sstevel@tonic-gate }
39967c478bd9Sstevel@tonic-gate /*
39977c478bd9Sstevel@tonic-gate **  SETUP_SMTPD_IO -- setup I/O fd correctly for the SMTP server
39987c478bd9Sstevel@tonic-gate **
39997c478bd9Sstevel@tonic-gate **	Parameters:
40007c478bd9Sstevel@tonic-gate **		none.
40017c478bd9Sstevel@tonic-gate **
40027c478bd9Sstevel@tonic-gate **	Returns:
40037c478bd9Sstevel@tonic-gate **		nothing.
40047c478bd9Sstevel@tonic-gate **
40057c478bd9Sstevel@tonic-gate **	Side Effects:
40067c478bd9Sstevel@tonic-gate **		may change I/O fd.
40077c478bd9Sstevel@tonic-gate */
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate static void
setup_smtpd_io()40107c478bd9Sstevel@tonic-gate setup_smtpd_io()
40117c478bd9Sstevel@tonic-gate {
40127c478bd9Sstevel@tonic-gate 	int inchfd, outchfd, outfd;
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	inchfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL);
40157c478bd9Sstevel@tonic-gate 	outchfd  = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL);
40167c478bd9Sstevel@tonic-gate 	outfd = sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL);
40177c478bd9Sstevel@tonic-gate 	if (outchfd != outfd)
40187c478bd9Sstevel@tonic-gate 	{
40197c478bd9Sstevel@tonic-gate 		/* arrange for debugging output to go to remote host */
40207c478bd9Sstevel@tonic-gate 		(void) dup2(outchfd, outfd);
40217c478bd9Sstevel@tonic-gate 	}
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	/*
40247c478bd9Sstevel@tonic-gate 	**  if InChannel and OutChannel are stdin/stdout
40257c478bd9Sstevel@tonic-gate 	**  and connected to ttys
40267c478bd9Sstevel@tonic-gate 	**  and fcntl(STDIN, F_SETFL, O_NONBLOCKING) also changes STDOUT,
40277c478bd9Sstevel@tonic-gate 	**  then "chain" them together.
40287c478bd9Sstevel@tonic-gate 	*/
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	if (inchfd == STDIN_FILENO && outchfd == STDOUT_FILENO &&
40317c478bd9Sstevel@tonic-gate 	    isatty(inchfd) && isatty(outchfd))
40327c478bd9Sstevel@tonic-gate 	{
40337c478bd9Sstevel@tonic-gate 		int inmode, outmode;
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 		inmode = fcntl(inchfd, F_GETFL, 0);
40367c478bd9Sstevel@tonic-gate 		if (inmode == -1)
40377c478bd9Sstevel@tonic-gate 		{
40387c478bd9Sstevel@tonic-gate 			if (LogLevel > 11)
40397c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, NOQID,
40407c478bd9Sstevel@tonic-gate 					"fcntl(inchfd, F_GETFL) failed: %s",
40417c478bd9Sstevel@tonic-gate 					sm_errstring(errno));
40427c478bd9Sstevel@tonic-gate 			return;
40437c478bd9Sstevel@tonic-gate 		}
40447c478bd9Sstevel@tonic-gate 		outmode = fcntl(outchfd, F_GETFL, 0);
40457c478bd9Sstevel@tonic-gate 		if (outmode == -1)
40467c478bd9Sstevel@tonic-gate 		{
40477c478bd9Sstevel@tonic-gate 			if (LogLevel > 11)
40487c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, NOQID,
40497c478bd9Sstevel@tonic-gate 					"fcntl(outchfd, F_GETFL) failed: %s",
40507c478bd9Sstevel@tonic-gate 					sm_errstring(errno));
40517c478bd9Sstevel@tonic-gate 			return;
40527c478bd9Sstevel@tonic-gate 		}
40537c478bd9Sstevel@tonic-gate 		if (bitset(O_NONBLOCK, inmode) ||
40547c478bd9Sstevel@tonic-gate 		    bitset(O_NONBLOCK, outmode) ||
40557c478bd9Sstevel@tonic-gate 		    fcntl(inchfd, F_SETFL, inmode | O_NONBLOCK) == -1)
40567c478bd9Sstevel@tonic-gate 			return;
40577c478bd9Sstevel@tonic-gate 		outmode = fcntl(outchfd, F_GETFL, 0);
40587c478bd9Sstevel@tonic-gate 		if (outmode != -1 && bitset(O_NONBLOCK, outmode))
40597c478bd9Sstevel@tonic-gate 		{
40607c478bd9Sstevel@tonic-gate 			/* changing InChannel also changes OutChannel */
40617c478bd9Sstevel@tonic-gate 			sm_io_automode(OutChannel, InChannel);
40627c478bd9Sstevel@tonic-gate 			if (tTd(97, 4) && LogLevel > 9)
40637c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, NOQID,
40647c478bd9Sstevel@tonic-gate 					  "set automode for I (%d)/O (%d) in SMTP server",
40657c478bd9Sstevel@tonic-gate 					  inchfd, outchfd);
40667c478bd9Sstevel@tonic-gate 		}
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate 		/* undo change of inchfd */
40697c478bd9Sstevel@tonic-gate 		(void) fcntl(inchfd, F_SETFL, inmode);
40707c478bd9Sstevel@tonic-gate 	}
40717c478bd9Sstevel@tonic-gate }
40727c478bd9Sstevel@tonic-gate /*
40737c478bd9Sstevel@tonic-gate **  SKIPWORD -- skip a fixed word.
40747c478bd9Sstevel@tonic-gate **
40757c478bd9Sstevel@tonic-gate **	Parameters:
40767c478bd9Sstevel@tonic-gate **		p -- place to start looking.
40777c478bd9Sstevel@tonic-gate **		w -- word to skip.
40787c478bd9Sstevel@tonic-gate **
40797c478bd9Sstevel@tonic-gate **	Returns:
40807c478bd9Sstevel@tonic-gate **		p following w.
40817c478bd9Sstevel@tonic-gate **		NULL on error.
40827c478bd9Sstevel@tonic-gate **
40837c478bd9Sstevel@tonic-gate **	Side Effects:
40847c478bd9Sstevel@tonic-gate **		clobbers the p data area.
40857c478bd9Sstevel@tonic-gate */
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate static char *
skipword(p,w)40887c478bd9Sstevel@tonic-gate skipword(p, w)
40897c478bd9Sstevel@tonic-gate 	register char *volatile p;
40907c478bd9Sstevel@tonic-gate 	char *w;
40917c478bd9Sstevel@tonic-gate {
40927c478bd9Sstevel@tonic-gate 	register char *q;
40937c478bd9Sstevel@tonic-gate 	char *firstp = p;
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	/* find beginning of word */
40967c478bd9Sstevel@tonic-gate 	SKIP_SPACE(p);
40977c478bd9Sstevel@tonic-gate 	q = p;
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	/* find end of word */
41007c478bd9Sstevel@tonic-gate 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
41017c478bd9Sstevel@tonic-gate 		p++;
41027c478bd9Sstevel@tonic-gate 	while (isascii(*p) && isspace(*p))
41037c478bd9Sstevel@tonic-gate 		*p++ = '\0';
41047c478bd9Sstevel@tonic-gate 	if (*p != ':')
41057c478bd9Sstevel@tonic-gate 	{
41067c478bd9Sstevel@tonic-gate 	  syntax:
41077c478bd9Sstevel@tonic-gate 		usrerr("501 5.5.2 Syntax error in parameters scanning \"%s\"",
41087c478bd9Sstevel@tonic-gate 			shortenstring(firstp, MAXSHORTSTR));
41097c478bd9Sstevel@tonic-gate 		return NULL;
41107c478bd9Sstevel@tonic-gate 	}
41117c478bd9Sstevel@tonic-gate 	*p++ = '\0';
41127c478bd9Sstevel@tonic-gate 	SKIP_SPACE(p);
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 	if (*p == '\0')
41157c478bd9Sstevel@tonic-gate 		goto syntax;
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	/* see if the input word matches desired word */
41187c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(q, w))
41197c478bd9Sstevel@tonic-gate 		goto syntax;
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	return p;
41227c478bd9Sstevel@tonic-gate }
41233ee0e492Sjbeck 
41247c478bd9Sstevel@tonic-gate /*
4125058561cbSjbeck **  RESET_MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
41267c478bd9Sstevel@tonic-gate **
41277c478bd9Sstevel@tonic-gate **	Parameters:
41287c478bd9Sstevel@tonic-gate **		e -- the envelope.
41297c478bd9Sstevel@tonic-gate **
41307c478bd9Sstevel@tonic-gate **	Returns:
41317c478bd9Sstevel@tonic-gate **		none.
41327c478bd9Sstevel@tonic-gate */
41337c478bd9Sstevel@tonic-gate 
4134058561cbSjbeck void
reset_mail_esmtp_args(e)4135058561cbSjbeck reset_mail_esmtp_args(e)
4136058561cbSjbeck 	ENVELOPE *e;
4137058561cbSjbeck {
4138058561cbSjbeck 	/* "size": no reset */
4139058561cbSjbeck 
4140058561cbSjbeck 	/* "body" */
4141058561cbSjbeck 	SevenBitInput = SevenBitInput_Saved;
4142058561cbSjbeck 	e->e_bodytype = NULL;
4143058561cbSjbeck 
4144058561cbSjbeck 	/* "envid" */
4145058561cbSjbeck 	e->e_envid = NULL;
4146058561cbSjbeck 	macdefine(&e->e_macro, A_PERM, macid("{dsn_envid}"), NULL);
4147058561cbSjbeck 
4148058561cbSjbeck 	/* "ret" */
41497800901eSjbeck 	e->e_flags &= ~(EF_RET_PARAM|EF_NO_BODY_RETN);
4150058561cbSjbeck 	macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), NULL);
4151058561cbSjbeck 
4152058561cbSjbeck #if SASL
4153058561cbSjbeck 	/* "auth" */
4154058561cbSjbeck 	macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"), NULL);
4155058561cbSjbeck 	e->e_auth_param = "";
4156058561cbSjbeck # if _FFR_AUTH_PASSING
4157058561cbSjbeck 	macdefine(&BlankEnvelope.e_macro, A_PERM,
4158058561cbSjbeck 				  macid("{auth_author}"), NULL);
4159058561cbSjbeck # endif /* _FFR_AUTH_PASSING */
4160058561cbSjbeck #endif /* SASL */
4161058561cbSjbeck 
4162058561cbSjbeck 	/* "by" */
4163058561cbSjbeck 	e->e_deliver_by = 0;
4164058561cbSjbeck 	e->e_dlvr_flag = 0;
4165058561cbSjbeck }
4166058561cbSjbeck 
4167058561cbSjbeck /*
4168058561cbSjbeck **  MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
4169058561cbSjbeck **
4170058561cbSjbeck **	Parameters:
4171058561cbSjbeck **		a -- address (unused, for compatibility with rcpt_esmtp_args)
4172058561cbSjbeck **		kp -- the parameter key.
4173058561cbSjbeck **		vp -- the value of that parameter.
4174058561cbSjbeck **		e -- the envelope.
4175058561cbSjbeck **
4176058561cbSjbeck **	Returns:
4177058561cbSjbeck **		none.
4178058561cbSjbeck */
4179058561cbSjbeck 
4180058561cbSjbeck void
mail_esmtp_args(a,kp,vp,e)4181058561cbSjbeck mail_esmtp_args(a, kp, vp, e)
4182058561cbSjbeck 	ADDRESS *a;
41837c478bd9Sstevel@tonic-gate 	char *kp;
41847c478bd9Sstevel@tonic-gate 	char *vp;
41857c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
41867c478bd9Sstevel@tonic-gate {
41877c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(kp, "size") == 0)
41887c478bd9Sstevel@tonic-gate 	{
41897c478bd9Sstevel@tonic-gate 		if (vp == NULL)
41907c478bd9Sstevel@tonic-gate 		{
41917c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 SIZE requires a value");
41927c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
41937c478bd9Sstevel@tonic-gate 		}
41947c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), vp);
41957c478bd9Sstevel@tonic-gate 		errno = 0;
41967c478bd9Sstevel@tonic-gate 		e->e_msgsize = strtol(vp, (char **) NULL, 10);
41977c478bd9Sstevel@tonic-gate 		if (e->e_msgsize == LONG_MAX && errno == ERANGE)
41987c478bd9Sstevel@tonic-gate 		{
41997c478bd9Sstevel@tonic-gate 			usrerr("552 5.2.3 Message size exceeds maximum value");
42007c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42017c478bd9Sstevel@tonic-gate 		}
42027c478bd9Sstevel@tonic-gate 		if (e->e_msgsize < 0)
42037c478bd9Sstevel@tonic-gate 		{
42047c478bd9Sstevel@tonic-gate 			usrerr("552 5.2.3 Message size invalid");
42057c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42067c478bd9Sstevel@tonic-gate 		}
42077c478bd9Sstevel@tonic-gate 	}
42087c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "body") == 0)
42097c478bd9Sstevel@tonic-gate 	{
42107c478bd9Sstevel@tonic-gate 		if (vp == NULL)
42117c478bd9Sstevel@tonic-gate 		{
42127c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 BODY requires a value");
42137c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42147c478bd9Sstevel@tonic-gate 		}
42157c478bd9Sstevel@tonic-gate 		else if (sm_strcasecmp(vp, "8bitmime") == 0)
42167c478bd9Sstevel@tonic-gate 		{
42177c478bd9Sstevel@tonic-gate 			SevenBitInput = false;
42187c478bd9Sstevel@tonic-gate 		}
42197c478bd9Sstevel@tonic-gate 		else if (sm_strcasecmp(vp, "7bit") == 0)
42207c478bd9Sstevel@tonic-gate 		{
42217c478bd9Sstevel@tonic-gate 			SevenBitInput = true;
42227c478bd9Sstevel@tonic-gate 		}
42237c478bd9Sstevel@tonic-gate 		else
42247c478bd9Sstevel@tonic-gate 		{
42257c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.4 Unknown BODY type %s", vp);
42267c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42277c478bd9Sstevel@tonic-gate 		}
42287c478bd9Sstevel@tonic-gate 		e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, vp);
42297c478bd9Sstevel@tonic-gate 	}
42307c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "envid") == 0)
42317c478bd9Sstevel@tonic-gate 	{
4232058561cbSjbeck 		if (!bitset(SRV_OFFER_DSN, e->e_features))
42337c478bd9Sstevel@tonic-gate 		{
42347c478bd9Sstevel@tonic-gate 			usrerr("504 5.7.0 Sorry, ENVID not supported, we do not allow DSN");
42357c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42367c478bd9Sstevel@tonic-gate 		}
42377c478bd9Sstevel@tonic-gate 		if (vp == NULL)
42387c478bd9Sstevel@tonic-gate 		{
42397c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 ENVID requires a value");
42407c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42417c478bd9Sstevel@tonic-gate 		}
42427c478bd9Sstevel@tonic-gate 		if (!xtextok(vp))
42437c478bd9Sstevel@tonic-gate 		{
42447c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.4 Syntax error in ENVID parameter value");
42457c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42467c478bd9Sstevel@tonic-gate 		}
42477c478bd9Sstevel@tonic-gate 		if (e->e_envid != NULL)
42487c478bd9Sstevel@tonic-gate 		{
42497c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.0 Duplicate ENVID parameter");
42507c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42517c478bd9Sstevel@tonic-gate 		}
42527c478bd9Sstevel@tonic-gate 		e->e_envid = sm_rpool_strdup_x(e->e_rpool, vp);
42537c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM,
42547c478bd9Sstevel@tonic-gate 			macid("{dsn_envid}"), e->e_envid);
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "ret") == 0)
42577c478bd9Sstevel@tonic-gate 	{
4258058561cbSjbeck 		if (!bitset(SRV_OFFER_DSN, e->e_features))
42597c478bd9Sstevel@tonic-gate 		{
42607c478bd9Sstevel@tonic-gate 			usrerr("504 5.7.0 Sorry, RET not supported, we do not allow DSN");
42617c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42627c478bd9Sstevel@tonic-gate 		}
42637c478bd9Sstevel@tonic-gate 		if (vp == NULL)
42647c478bd9Sstevel@tonic-gate 		{
42657c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 RET requires a value");
42667c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 		if (bitset(EF_RET_PARAM, e->e_flags))
42697c478bd9Sstevel@tonic-gate 		{
42707c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.0 Duplicate RET parameter");
42717c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42727c478bd9Sstevel@tonic-gate 		}
42737c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_RET_PARAM;
42747c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(vp, "hdrs") == 0)
42757c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_NO_BODY_RETN;
42767c478bd9Sstevel@tonic-gate 		else if (sm_strcasecmp(vp, "full") != 0)
42777c478bd9Sstevel@tonic-gate 		{
42787c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 Bad argument \"%s\" to RET", vp);
42797c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42807c478bd9Sstevel@tonic-gate 		}
42817c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), vp);
42827c478bd9Sstevel@tonic-gate 	}
42837c478bd9Sstevel@tonic-gate #if SASL
42847c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "auth") == 0)
42857c478bd9Sstevel@tonic-gate 	{
42867c478bd9Sstevel@tonic-gate 		int len;
42877c478bd9Sstevel@tonic-gate 		char *q;
42887c478bd9Sstevel@tonic-gate 		char *auth_param;	/* the value of the AUTH=x */
42897c478bd9Sstevel@tonic-gate 		bool saveQuickAbort = QuickAbort;
42907c478bd9Sstevel@tonic-gate 		bool saveSuprErrs = SuprErrs;
42917c478bd9Sstevel@tonic-gate 		bool saveExitStat = ExitStat;
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 		if (vp == NULL)
42947c478bd9Sstevel@tonic-gate 		{
42957c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 AUTH= requires a value");
42967c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
42977c478bd9Sstevel@tonic-gate 		}
42987c478bd9Sstevel@tonic-gate 		if (e->e_auth_param != NULL)
42997c478bd9Sstevel@tonic-gate 		{
43007c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.0 Duplicate AUTH parameter");
43017c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
43027c478bd9Sstevel@tonic-gate 		}
43037c478bd9Sstevel@tonic-gate 		if ((q = strchr(vp, ' ')) != NULL)
43047c478bd9Sstevel@tonic-gate 			len = q - vp + 1;
43057c478bd9Sstevel@tonic-gate 		else
43067c478bd9Sstevel@tonic-gate 			len = strlen(vp) + 1;
43077c478bd9Sstevel@tonic-gate 		auth_param = xalloc(len);
43087c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(auth_param, vp, len);
43097c478bd9Sstevel@tonic-gate 		if (!xtextok(auth_param))
43107c478bd9Sstevel@tonic-gate 		{
43117c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.4 Syntax error in AUTH parameter value");
43127c478bd9Sstevel@tonic-gate 			/* just a warning? */
43137c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
43147c478bd9Sstevel@tonic-gate 		}
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 		/* XXX define this always or only if trusted? */
43177c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"),
43187c478bd9Sstevel@tonic-gate 			  auth_param);
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate 		/*
43217c478bd9Sstevel@tonic-gate 		**  call Strust_auth to find out whether
43227c478bd9Sstevel@tonic-gate 		**  auth_param is acceptable (trusted)
43237c478bd9Sstevel@tonic-gate 		**  we shouldn't trust it if not authenticated
43247c478bd9Sstevel@tonic-gate 		**  (required by RFC, leave it to ruleset?)
43257c478bd9Sstevel@tonic-gate 		*/
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 		SuprErrs = true;
43287c478bd9Sstevel@tonic-gate 		QuickAbort = false;
43297c478bd9Sstevel@tonic-gate 		if (strcmp(auth_param, "<>") != 0 &&
43307c478bd9Sstevel@tonic-gate 		     (rscheck("trust_auth", auth_param, NULL, e, RSF_RMCOMM,
4331058561cbSjbeck 			      9, NULL, NOQID, NULL) != EX_OK || Errors > 0))
43327c478bd9Sstevel@tonic-gate 		{
43337c478bd9Sstevel@tonic-gate 			if (tTd(95, 8))
43347c478bd9Sstevel@tonic-gate 			{
43357c478bd9Sstevel@tonic-gate 				q = e->e_auth_param;
43367c478bd9Sstevel@tonic-gate 				sm_dprintf("auth=\"%.100s\" not trusted user=\"%.100s\"\n",
43377c478bd9Sstevel@tonic-gate 					auth_param, (q == NULL) ? "" : q);
43387c478bd9Sstevel@tonic-gate 			}
43397c478bd9Sstevel@tonic-gate 
43407c478bd9Sstevel@tonic-gate 			/* not trusted */
43417c478bd9Sstevel@tonic-gate 			e->e_auth_param = "<>";
43427c478bd9Sstevel@tonic-gate # if _FFR_AUTH_PASSING
43437c478bd9Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_PERM,
43447c478bd9Sstevel@tonic-gate 				  macid("{auth_author}"), NULL);
43457c478bd9Sstevel@tonic-gate # endif /* _FFR_AUTH_PASSING */
43467c478bd9Sstevel@tonic-gate 		}
43477c478bd9Sstevel@tonic-gate 		else
43487c478bd9Sstevel@tonic-gate 		{
43497c478bd9Sstevel@tonic-gate 			if (tTd(95, 8))
43507c478bd9Sstevel@tonic-gate 				sm_dprintf("auth=\"%.100s\" trusted\n", auth_param);
43517c478bd9Sstevel@tonic-gate 			e->e_auth_param = sm_rpool_strdup_x(e->e_rpool,
43527c478bd9Sstevel@tonic-gate 							    auth_param);
43537c478bd9Sstevel@tonic-gate 		}
43547c478bd9Sstevel@tonic-gate 		sm_free(auth_param); /* XXX */
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 		/* reset values */
43577c478bd9Sstevel@tonic-gate 		Errors = 0;
43587c478bd9Sstevel@tonic-gate 		QuickAbort = saveQuickAbort;
43597c478bd9Sstevel@tonic-gate 		SuprErrs = saveSuprErrs;
43607c478bd9Sstevel@tonic-gate 		ExitStat = saveExitStat;
43617c478bd9Sstevel@tonic-gate 	}
43627c478bd9Sstevel@tonic-gate #endif /* SASL */
43637c478bd9Sstevel@tonic-gate #define PRTCHAR(c)	((isascii(c) && isprint(c)) ? (c) : '?')
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	/*
43667c478bd9Sstevel@tonic-gate 	**  "by" is only accepted if DeliverByMin >= 0.
43677c478bd9Sstevel@tonic-gate 	**  We maybe could add this to the list of server_features.
43687c478bd9Sstevel@tonic-gate 	*/
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "by") == 0 && DeliverByMin >= 0)
43717c478bd9Sstevel@tonic-gate 	{
43727c478bd9Sstevel@tonic-gate 		char *s;
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 		if (vp == NULL)
43757c478bd9Sstevel@tonic-gate 		{
43767c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 BY= requires a value");
43777c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
43787c478bd9Sstevel@tonic-gate 		}
43797c478bd9Sstevel@tonic-gate 		errno = 0;
43807c478bd9Sstevel@tonic-gate 		e->e_deliver_by = strtol(vp, &s, 10);
43817c478bd9Sstevel@tonic-gate 		if (e->e_deliver_by == LONG_MIN ||
43827c478bd9Sstevel@tonic-gate 		    e->e_deliver_by == LONG_MAX ||
43837c478bd9Sstevel@tonic-gate 		    e->e_deliver_by > 999999999l ||
43847c478bd9Sstevel@tonic-gate 		    e->e_deliver_by < -999999999l)
43857c478bd9Sstevel@tonic-gate 		{
43867c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 BY=%s out of range", vp);
43877c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
43887c478bd9Sstevel@tonic-gate 		}
43897c478bd9Sstevel@tonic-gate 		if (s == NULL || *s != ';')
43907c478bd9Sstevel@tonic-gate 		{
43917c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 BY= missing ';'");
43927c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
43937c478bd9Sstevel@tonic-gate 		}
43947c478bd9Sstevel@tonic-gate 		e->e_dlvr_flag = 0;
43957c478bd9Sstevel@tonic-gate 		++s;	/* XXX: spaces allowed? */
43967c478bd9Sstevel@tonic-gate 		SKIP_SPACE(s);
43977c478bd9Sstevel@tonic-gate 		switch (tolower(*s))
43987c478bd9Sstevel@tonic-gate 		{
43997c478bd9Sstevel@tonic-gate 		  case 'n':
44007c478bd9Sstevel@tonic-gate 			e->e_dlvr_flag = DLVR_NOTIFY;
44017c478bd9Sstevel@tonic-gate 			break;
44027c478bd9Sstevel@tonic-gate 		  case 'r':
44037c478bd9Sstevel@tonic-gate 			e->e_dlvr_flag = DLVR_RETURN;
44047c478bd9Sstevel@tonic-gate 			if (e->e_deliver_by <= 0)
44057c478bd9Sstevel@tonic-gate 			{
44067c478bd9Sstevel@tonic-gate 				usrerr("501 5.5.4 mode R requires BY time > 0");
44077c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
44087c478bd9Sstevel@tonic-gate 			}
44097c478bd9Sstevel@tonic-gate 			if (DeliverByMin > 0 && e->e_deliver_by > 0 &&
44107c478bd9Sstevel@tonic-gate 			    e->e_deliver_by < DeliverByMin)
44117c478bd9Sstevel@tonic-gate 			{
44127c478bd9Sstevel@tonic-gate 				usrerr("555 5.5.2 time %ld less than %ld",
44137c478bd9Sstevel@tonic-gate 					e->e_deliver_by, (long) DeliverByMin);
44147c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
44157c478bd9Sstevel@tonic-gate 			}
44167c478bd9Sstevel@tonic-gate 			break;
44177c478bd9Sstevel@tonic-gate 		  default:
44187c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 illegal by-mode '%c'", PRTCHAR(*s));
44197c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
44207c478bd9Sstevel@tonic-gate 		}
44217c478bd9Sstevel@tonic-gate 		++s;	/* XXX: spaces allowed? */
44227c478bd9Sstevel@tonic-gate 		SKIP_SPACE(s);
44237c478bd9Sstevel@tonic-gate 		switch (tolower(*s))
44247c478bd9Sstevel@tonic-gate 		{
44257c478bd9Sstevel@tonic-gate 		  case 't':
44267c478bd9Sstevel@tonic-gate 			e->e_dlvr_flag |= DLVR_TRACE;
44277c478bd9Sstevel@tonic-gate 			break;
44287c478bd9Sstevel@tonic-gate 		  case '\0':
44297c478bd9Sstevel@tonic-gate 			break;
44307c478bd9Sstevel@tonic-gate 		  default:
44317c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 illegal by-trace '%c'", PRTCHAR(*s));
44327c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
44337c478bd9Sstevel@tonic-gate 		}
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 		/* XXX: check whether more characters follow? */
44367c478bd9Sstevel@tonic-gate 	}
44377c478bd9Sstevel@tonic-gate 	else
44387c478bd9Sstevel@tonic-gate 	{
44397c478bd9Sstevel@tonic-gate 		usrerr("555 5.5.4 %s parameter unrecognized", kp);
44407c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
44417c478bd9Sstevel@tonic-gate 	}
44427c478bd9Sstevel@tonic-gate }
4443058561cbSjbeck 
44447c478bd9Sstevel@tonic-gate /*
44457c478bd9Sstevel@tonic-gate **  RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line
44467c478bd9Sstevel@tonic-gate **
44477c478bd9Sstevel@tonic-gate **	Parameters:
44487c478bd9Sstevel@tonic-gate **		a -- the address corresponding to the To: parameter.
44497c478bd9Sstevel@tonic-gate **		kp -- the parameter key.
44507c478bd9Sstevel@tonic-gate **		vp -- the value of that parameter.
44517c478bd9Sstevel@tonic-gate **		e -- the envelope.
44527c478bd9Sstevel@tonic-gate **
44537c478bd9Sstevel@tonic-gate **	Returns:
44547c478bd9Sstevel@tonic-gate **		none.
44557c478bd9Sstevel@tonic-gate */
44567c478bd9Sstevel@tonic-gate 
4457058561cbSjbeck void
rcpt_esmtp_args(a,kp,vp,e)4458058561cbSjbeck rcpt_esmtp_args(a, kp, vp, e)
44597c478bd9Sstevel@tonic-gate 	ADDRESS *a;
44607c478bd9Sstevel@tonic-gate 	char *kp;
44617c478bd9Sstevel@tonic-gate 	char *vp;
44627c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
44637c478bd9Sstevel@tonic-gate {
44647c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(kp, "notify") == 0)
44657c478bd9Sstevel@tonic-gate 	{
44667c478bd9Sstevel@tonic-gate 		char *p;
44677c478bd9Sstevel@tonic-gate 
4468058561cbSjbeck 		if (!bitset(SRV_OFFER_DSN, e->e_features))
44697c478bd9Sstevel@tonic-gate 		{
44707c478bd9Sstevel@tonic-gate 			usrerr("504 5.7.0 Sorry, NOTIFY not supported, we do not allow DSN");
44717c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
44727c478bd9Sstevel@tonic-gate 		}
44737c478bd9Sstevel@tonic-gate 		if (vp == NULL)
44747c478bd9Sstevel@tonic-gate 		{
44757c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 NOTIFY requires a value");
44767c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
44777c478bd9Sstevel@tonic-gate 		}
44787c478bd9Sstevel@tonic-gate 		a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY);
44797c478bd9Sstevel@tonic-gate 		a->q_flags |= QHASNOTIFY;
44807c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, macid("{dsn_notify}"), vp);
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(vp, "never") == 0)
44837c478bd9Sstevel@tonic-gate 			return;
44847c478bd9Sstevel@tonic-gate 		for (p = vp; p != NULL; vp = p)
44857c478bd9Sstevel@tonic-gate 		{
44867c478bd9Sstevel@tonic-gate 			char *s;
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 			s = p = strchr(p, ',');
44897c478bd9Sstevel@tonic-gate 			if (p != NULL)
44907c478bd9Sstevel@tonic-gate 				*p++ = '\0';
44917c478bd9Sstevel@tonic-gate 			if (sm_strcasecmp(vp, "success") == 0)
44927c478bd9Sstevel@tonic-gate 				a->q_flags |= QPINGONSUCCESS;
44937c478bd9Sstevel@tonic-gate 			else if (sm_strcasecmp(vp, "failure") == 0)
44947c478bd9Sstevel@tonic-gate 				a->q_flags |= QPINGONFAILURE;
44957c478bd9Sstevel@tonic-gate 			else if (sm_strcasecmp(vp, "delay") == 0)
44967c478bd9Sstevel@tonic-gate 				a->q_flags |= QPINGONDELAY;
44977c478bd9Sstevel@tonic-gate 			else
44987c478bd9Sstevel@tonic-gate 			{
44997c478bd9Sstevel@tonic-gate 				usrerr("501 5.5.4 Bad argument \"%s\"  to NOTIFY",
45007c478bd9Sstevel@tonic-gate 					vp);
45017c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
45027c478bd9Sstevel@tonic-gate 			}
45037c478bd9Sstevel@tonic-gate 			if (s != NULL)
45047c478bd9Sstevel@tonic-gate 				*s = ',';
45057c478bd9Sstevel@tonic-gate 		}
45067c478bd9Sstevel@tonic-gate 	}
45077c478bd9Sstevel@tonic-gate 	else if (sm_strcasecmp(kp, "orcpt") == 0)
45087c478bd9Sstevel@tonic-gate 	{
4509058561cbSjbeck 		if (!bitset(SRV_OFFER_DSN, e->e_features))
45107c478bd9Sstevel@tonic-gate 		{
45117c478bd9Sstevel@tonic-gate 			usrerr("504 5.7.0 Sorry, ORCPT not supported, we do not allow DSN");
45127c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
45137c478bd9Sstevel@tonic-gate 		}
45147c478bd9Sstevel@tonic-gate 		if (vp == NULL)
45157c478bd9Sstevel@tonic-gate 		{
45167c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.2 ORCPT requires a value");
45177c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
45187c478bd9Sstevel@tonic-gate 		}
45197c478bd9Sstevel@tonic-gate 		if (strchr(vp, ';') == NULL || !xtextok(vp))
45207c478bd9Sstevel@tonic-gate 		{
45217c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.4 Syntax error in ORCPT parameter value");
45227c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
45237c478bd9Sstevel@tonic-gate 		}
45247c478bd9Sstevel@tonic-gate 		if (a->q_orcpt != NULL)
45257c478bd9Sstevel@tonic-gate 		{
45267c478bd9Sstevel@tonic-gate 			usrerr("501 5.5.0 Duplicate ORCPT parameter");
45277c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
45287c478bd9Sstevel@tonic-gate 		}
45297c478bd9Sstevel@tonic-gate 		a->q_orcpt = sm_rpool_strdup_x(e->e_rpool, vp);
45307c478bd9Sstevel@tonic-gate 	}
45317c478bd9Sstevel@tonic-gate 	else
45327c478bd9Sstevel@tonic-gate 	{
45337c478bd9Sstevel@tonic-gate 		usrerr("555 5.5.4 %s parameter unrecognized", kp);
45347c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
45357c478bd9Sstevel@tonic-gate 	}
45367c478bd9Sstevel@tonic-gate }
45377c478bd9Sstevel@tonic-gate /*
45387c478bd9Sstevel@tonic-gate **  PRINTVRFYADDR -- print an entry in the verify queue
45397c478bd9Sstevel@tonic-gate **
45407c478bd9Sstevel@tonic-gate **	Parameters:
45417c478bd9Sstevel@tonic-gate **		a -- the address to print.
45427c478bd9Sstevel@tonic-gate **		last -- set if this is the last one.
45437c478bd9Sstevel@tonic-gate **		vrfy -- set if this is a VRFY command.
45447c478bd9Sstevel@tonic-gate **
45457c478bd9Sstevel@tonic-gate **	Returns:
45467c478bd9Sstevel@tonic-gate **		none.
45477c478bd9Sstevel@tonic-gate **
45487c478bd9Sstevel@tonic-gate **	Side Effects:
45497c478bd9Sstevel@tonic-gate **		Prints the appropriate 250 codes.
45507c478bd9Sstevel@tonic-gate */
45517c478bd9Sstevel@tonic-gate #define OFFF	(3 + 1 + 5 + 1)	/* offset in fmt: SMTP reply + enh. code */
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate static void
printvrfyaddr(a,last,vrfy)45547c478bd9Sstevel@tonic-gate printvrfyaddr(a, last, vrfy)
45557c478bd9Sstevel@tonic-gate 	register ADDRESS *a;
45567c478bd9Sstevel@tonic-gate 	bool last;
45577c478bd9Sstevel@tonic-gate 	bool vrfy;
45587c478bd9Sstevel@tonic-gate {
45597c478bd9Sstevel@tonic-gate 	char fmtbuf[30];
45607c478bd9Sstevel@tonic-gate 
45617c478bd9Sstevel@tonic-gate 	if (vrfy && a->q_mailer != NULL &&
45627c478bd9Sstevel@tonic-gate 	    !bitnset(M_VRFY250, a->q_mailer->m_flags))
4563058561cbSjbeck 		(void) sm_strlcpy(fmtbuf, "252", sizeof(fmtbuf));
45647c478bd9Sstevel@tonic-gate 	else
4565058561cbSjbeck 		(void) sm_strlcpy(fmtbuf, "250", sizeof(fmtbuf));
45667c478bd9Sstevel@tonic-gate 	fmtbuf[3] = last ? ' ' : '-';
4567058561cbSjbeck 	(void) sm_strlcpy(&fmtbuf[4], "2.1.5 ", sizeof(fmtbuf) - 4);
45687c478bd9Sstevel@tonic-gate 	if (a->q_fullname == NULL)
45697c478bd9Sstevel@tonic-gate 	{
45707c478bd9Sstevel@tonic-gate 		if ((a->q_mailer == NULL ||
45717c478bd9Sstevel@tonic-gate 		     a->q_mailer->m_addrtype == NULL ||
45727c478bd9Sstevel@tonic-gate 		     sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) &&
45737c478bd9Sstevel@tonic-gate 		    strchr(a->q_user, '@') == NULL)
45747c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(&fmtbuf[OFFF], "<%s@%s>",
4575058561cbSjbeck 				       sizeof(fmtbuf) - OFFF);
45767c478bd9Sstevel@tonic-gate 		else
45777c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(&fmtbuf[OFFF], "<%s>",
4578058561cbSjbeck 				       sizeof(fmtbuf) - OFFF);
45797c478bd9Sstevel@tonic-gate 		message(fmtbuf, a->q_user, MyHostName);
45807c478bd9Sstevel@tonic-gate 	}
45817c478bd9Sstevel@tonic-gate 	else
45827c478bd9Sstevel@tonic-gate 	{
45837c478bd9Sstevel@tonic-gate 		if ((a->q_mailer == NULL ||
45847c478bd9Sstevel@tonic-gate 		     a->q_mailer->m_addrtype == NULL ||
45857c478bd9Sstevel@tonic-gate 		     sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) &&
45867c478bd9Sstevel@tonic-gate 		    strchr(a->q_user, '@') == NULL)
45877c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s@%s>",
4588058561cbSjbeck 				       sizeof(fmtbuf) - OFFF);
45897c478bd9Sstevel@tonic-gate 		else
45907c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s>",
4591058561cbSjbeck 				       sizeof(fmtbuf) - OFFF);
45927c478bd9Sstevel@tonic-gate 		message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate }
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate #if SASL
45977c478bd9Sstevel@tonic-gate /*
45987c478bd9Sstevel@tonic-gate **  SASLMECHS -- get list of possible AUTH mechanisms
45997c478bd9Sstevel@tonic-gate **
46007c478bd9Sstevel@tonic-gate **	Parameters:
46017c478bd9Sstevel@tonic-gate **		conn -- SASL connection info.
46027c478bd9Sstevel@tonic-gate **		mechlist -- output parameter for list of mechanisms.
46037c478bd9Sstevel@tonic-gate **
46047c478bd9Sstevel@tonic-gate **	Returns:
46057c478bd9Sstevel@tonic-gate **		number of mechs.
46067c478bd9Sstevel@tonic-gate */
46077c478bd9Sstevel@tonic-gate 
46087c478bd9Sstevel@tonic-gate static int
saslmechs(conn,mechlist)46097c478bd9Sstevel@tonic-gate saslmechs(conn, mechlist)
46107c478bd9Sstevel@tonic-gate 	sasl_conn_t *conn;
46117c478bd9Sstevel@tonic-gate 	char **mechlist;
46127c478bd9Sstevel@tonic-gate {
46137c478bd9Sstevel@tonic-gate 	int len, num, result;
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	/* "user" is currently unused */
46167c478bd9Sstevel@tonic-gate # if SASL >= 20000
46177c478bd9Sstevel@tonic-gate 	result = sasl_listmech(conn, NULL,
46187c478bd9Sstevel@tonic-gate 			       "", " ", "", (const char **) mechlist,
46197c478bd9Sstevel@tonic-gate 			       (unsigned int *)&len, &num);
46207c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
46217c478bd9Sstevel@tonic-gate 	result = sasl_listmech(conn, "user", /* XXX */
46227c478bd9Sstevel@tonic-gate 			       "", " ", "", mechlist,
46237c478bd9Sstevel@tonic-gate 			       (unsigned int *)&len, (unsigned int *)&num);
46247c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
46257c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
46267c478bd9Sstevel@tonic-gate 	{
46277c478bd9Sstevel@tonic-gate 		if (LogLevel > 9)
46287c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
46297c478bd9Sstevel@tonic-gate 				  "AUTH error: listmech=%d, num=%d",
46307c478bd9Sstevel@tonic-gate 				  result, num);
46317c478bd9Sstevel@tonic-gate 		num = 0;
46327c478bd9Sstevel@tonic-gate 	}
46337c478bd9Sstevel@tonic-gate 	if (num > 0)
46347c478bd9Sstevel@tonic-gate 	{
46357c478bd9Sstevel@tonic-gate 		if (LogLevel > 11)
46367c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, NOQID,
46377c478bd9Sstevel@tonic-gate 				  "AUTH: available mech=%s, allowed mech=%s",
46387c478bd9Sstevel@tonic-gate 				  *mechlist, AuthMechanisms);
46397c478bd9Sstevel@tonic-gate 		*mechlist = intersect(AuthMechanisms, *mechlist, NULL);
46407c478bd9Sstevel@tonic-gate 	}
46417c478bd9Sstevel@tonic-gate 	else
46427c478bd9Sstevel@tonic-gate 	{
46437c478bd9Sstevel@tonic-gate 		*mechlist = NULL;	/* be paranoid... */
46447c478bd9Sstevel@tonic-gate 		if (result == SASL_OK && LogLevel > 9)
46457c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
46467c478bd9Sstevel@tonic-gate 				  "AUTH warning: no mechanisms");
46477c478bd9Sstevel@tonic-gate 	}
46487c478bd9Sstevel@tonic-gate 	return num;
46497c478bd9Sstevel@tonic-gate }
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate # if SASL >= 20000
46527c478bd9Sstevel@tonic-gate /*
46537c478bd9Sstevel@tonic-gate **  PROXY_POLICY -- define proxy policy for AUTH
46547c478bd9Sstevel@tonic-gate **
46557c478bd9Sstevel@tonic-gate **	Parameters:
46567c478bd9Sstevel@tonic-gate **		conn -- unused.
46577c478bd9Sstevel@tonic-gate **		context -- unused.
46587c478bd9Sstevel@tonic-gate **		requested_user -- authorization identity.
46597c478bd9Sstevel@tonic-gate **		rlen -- authorization identity length.
46607c478bd9Sstevel@tonic-gate **		auth_identity -- authentication identity.
46617c478bd9Sstevel@tonic-gate **		alen -- authentication identity length.
46627c478bd9Sstevel@tonic-gate **		def_realm -- default user realm.
46637c478bd9Sstevel@tonic-gate **		urlen -- user realm length.
46647c478bd9Sstevel@tonic-gate **		propctx -- unused.
46657c478bd9Sstevel@tonic-gate **
46667c478bd9Sstevel@tonic-gate **	Returns:
46677c478bd9Sstevel@tonic-gate **		ok?
46687c478bd9Sstevel@tonic-gate **
46697c478bd9Sstevel@tonic-gate **	Side Effects:
46707c478bd9Sstevel@tonic-gate **		sets {auth_authen} macro.
46717c478bd9Sstevel@tonic-gate */
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate int
proxy_policy(conn,context,requested_user,rlen,auth_identity,alen,def_realm,urlen,propctx)46747c478bd9Sstevel@tonic-gate proxy_policy(conn, context, requested_user, rlen, auth_identity, alen,
46757c478bd9Sstevel@tonic-gate 	     def_realm, urlen, propctx)
46767c478bd9Sstevel@tonic-gate 	sasl_conn_t *conn;
46777c478bd9Sstevel@tonic-gate 	void *context;
46787c478bd9Sstevel@tonic-gate 	const char *requested_user;
46797c478bd9Sstevel@tonic-gate 	unsigned rlen;
46807c478bd9Sstevel@tonic-gate 	const char *auth_identity;
46817c478bd9Sstevel@tonic-gate 	unsigned alen;
46827c478bd9Sstevel@tonic-gate 	const char *def_realm;
46837c478bd9Sstevel@tonic-gate 	unsigned urlen;
46847c478bd9Sstevel@tonic-gate 	struct propctx *propctx;
46857c478bd9Sstevel@tonic-gate {
46867c478bd9Sstevel@tonic-gate 	if (auth_identity == NULL)
46877c478bd9Sstevel@tonic-gate 		return SASL_FAIL;
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate 	macdefine(&BlankEnvelope.e_macro, A_TEMP,
46907c478bd9Sstevel@tonic-gate 		  macid("{auth_authen}"), (char *) auth_identity);
46917c478bd9Sstevel@tonic-gate 
46927c478bd9Sstevel@tonic-gate 	return SASL_OK;
46937c478bd9Sstevel@tonic-gate }
46947c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
46957c478bd9Sstevel@tonic-gate 
46967c478bd9Sstevel@tonic-gate /*
46977c478bd9Sstevel@tonic-gate **  PROXY_POLICY -- define proxy policy for AUTH
46987c478bd9Sstevel@tonic-gate **
46997c478bd9Sstevel@tonic-gate **	Parameters:
47007c478bd9Sstevel@tonic-gate **		context -- unused.
47017c478bd9Sstevel@tonic-gate **		auth_identity -- authentication identity.
47027c478bd9Sstevel@tonic-gate **		requested_user -- authorization identity.
47037c478bd9Sstevel@tonic-gate **		user -- allowed user (output).
47047c478bd9Sstevel@tonic-gate **		errstr -- possible error string (output).
47057c478bd9Sstevel@tonic-gate **
47067c478bd9Sstevel@tonic-gate **	Returns:
47077c478bd9Sstevel@tonic-gate **		ok?
47087c478bd9Sstevel@tonic-gate */
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate int
proxy_policy(context,auth_identity,requested_user,user,errstr)47117c478bd9Sstevel@tonic-gate proxy_policy(context, auth_identity, requested_user, user, errstr)
47127c478bd9Sstevel@tonic-gate 	void *context;
47137c478bd9Sstevel@tonic-gate 	const char *auth_identity;
47147c478bd9Sstevel@tonic-gate 	const char *requested_user;
47157c478bd9Sstevel@tonic-gate 	const char **user;
47167c478bd9Sstevel@tonic-gate 	const char **errstr;
47177c478bd9Sstevel@tonic-gate {
47187c478bd9Sstevel@tonic-gate 	if (user == NULL || auth_identity == NULL)
47197c478bd9Sstevel@tonic-gate 		return SASL_FAIL;
47207c478bd9Sstevel@tonic-gate 	*user = newstr(auth_identity);
47217c478bd9Sstevel@tonic-gate 	return SASL_OK;
47227c478bd9Sstevel@tonic-gate }
47237c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
47247c478bd9Sstevel@tonic-gate #endif /* SASL */
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate #if STARTTLS
47277c478bd9Sstevel@tonic-gate /*
47287c478bd9Sstevel@tonic-gate **  INITSRVTLS -- initialize server side TLS
47297c478bd9Sstevel@tonic-gate **
47307c478bd9Sstevel@tonic-gate **	Parameters:
47317c478bd9Sstevel@tonic-gate **		tls_ok -- should tls initialization be done?
47327c478bd9Sstevel@tonic-gate **
47337c478bd9Sstevel@tonic-gate **	Returns:
47347c478bd9Sstevel@tonic-gate **		succeeded?
47357c478bd9Sstevel@tonic-gate **
47367c478bd9Sstevel@tonic-gate **	Side Effects:
47377c478bd9Sstevel@tonic-gate **		sets tls_ok_srv which is a static variable in this module.
47387c478bd9Sstevel@tonic-gate **		Do NOT remove assignments to it!
47397c478bd9Sstevel@tonic-gate */
47407c478bd9Sstevel@tonic-gate 
47417c478bd9Sstevel@tonic-gate bool
initsrvtls(tls_ok)47427c478bd9Sstevel@tonic-gate initsrvtls(tls_ok)
47437c478bd9Sstevel@tonic-gate 	bool tls_ok;
47447c478bd9Sstevel@tonic-gate {
47457c478bd9Sstevel@tonic-gate 	if (!tls_ok)
47467c478bd9Sstevel@tonic-gate 		return false;
47477c478bd9Sstevel@tonic-gate 
47487c478bd9Sstevel@tonic-gate 	/* do NOT remove assignment */
4749*e9af4bc0SJohn Beck 	tls_ok_srv = inittls(&srv_ctx, TLS_Srv_Opts, Srv_SSL_Options, true,
4750*e9af4bc0SJohn Beck 			     SrvCertFile, SrvKeyFile,
4751*e9af4bc0SJohn Beck 			     CACertPath, CACertFile, DHParams);
47527c478bd9Sstevel@tonic-gate 	return tls_ok_srv;
47537c478bd9Sstevel@tonic-gate }
47547c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
47557c478bd9Sstevel@tonic-gate /*
47567c478bd9Sstevel@tonic-gate **  SRVFEATURES -- get features for SMTP server
47577c478bd9Sstevel@tonic-gate **
47587c478bd9Sstevel@tonic-gate **	Parameters:
47597c478bd9Sstevel@tonic-gate **		e -- envelope (should be session context).
47607c478bd9Sstevel@tonic-gate **		clientname -- name of client.
47617c478bd9Sstevel@tonic-gate **		features -- default features for this invocation.
47627c478bd9Sstevel@tonic-gate **
47637c478bd9Sstevel@tonic-gate **	Returns:
47647c478bd9Sstevel@tonic-gate **		server features.
47657c478bd9Sstevel@tonic-gate */
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate /* table with options: it uses just one character, how about strings? */
47687c478bd9Sstevel@tonic-gate static struct
47697c478bd9Sstevel@tonic-gate {
47707c478bd9Sstevel@tonic-gate 	char		srvf_opt;
47717c478bd9Sstevel@tonic-gate 	unsigned int	srvf_flag;
47727c478bd9Sstevel@tonic-gate } srv_feat_table[] =
47737c478bd9Sstevel@tonic-gate {
47747c478bd9Sstevel@tonic-gate 	{ 'A',	SRV_OFFER_AUTH	},
47757c478bd9Sstevel@tonic-gate 	{ 'B',	SRV_OFFER_VERB	},
47767c478bd9Sstevel@tonic-gate 	{ 'C',	SRV_REQ_SEC	},
47777c478bd9Sstevel@tonic-gate 	{ 'D',	SRV_OFFER_DSN	},
47787c478bd9Sstevel@tonic-gate 	{ 'E',	SRV_OFFER_ETRN	},
47797c478bd9Sstevel@tonic-gate 	{ 'L',	SRV_REQ_AUTH	},
47807c478bd9Sstevel@tonic-gate #if PIPELINING
47817c478bd9Sstevel@tonic-gate # if _FFR_NO_PIPE
47827c478bd9Sstevel@tonic-gate 	{ 'N',	SRV_NO_PIPE	},
47837c478bd9Sstevel@tonic-gate # endif /* _FFR_NO_PIPE */
47847c478bd9Sstevel@tonic-gate 	{ 'P',	SRV_OFFER_PIPE	},
47857c478bd9Sstevel@tonic-gate #endif /* PIPELINING */
47867c478bd9Sstevel@tonic-gate 	{ 'R',	SRV_VRFY_CLT	},	/* same as V; not documented */
47877c478bd9Sstevel@tonic-gate 	{ 'S',	SRV_OFFER_TLS	},
47887c478bd9Sstevel@tonic-gate /*	{ 'T',	SRV_TMP_FAIL	},	*/
47897c478bd9Sstevel@tonic-gate 	{ 'V',	SRV_VRFY_CLT	},
47907c478bd9Sstevel@tonic-gate 	{ 'X',	SRV_OFFER_EXPN	},
47917c478bd9Sstevel@tonic-gate /*	{ 'Y',	SRV_OFFER_VRFY	},	*/
47927c478bd9Sstevel@tonic-gate 	{ '\0',	SRV_NONE	}
47937c478bd9Sstevel@tonic-gate };
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate static unsigned int
srvfeatures(e,clientname,features)47967c478bd9Sstevel@tonic-gate srvfeatures(e, clientname, features)
47977c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
47987c478bd9Sstevel@tonic-gate 	char *clientname;
47997c478bd9Sstevel@tonic-gate 	unsigned int features;
48007c478bd9Sstevel@tonic-gate {
48017c478bd9Sstevel@tonic-gate 	int r, i, j;
48027c478bd9Sstevel@tonic-gate 	char **pvp, c, opt;
48037c478bd9Sstevel@tonic-gate 	char pvpbuf[PSBUFSIZE];
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate 	pvp = NULL;
48067c478bd9Sstevel@tonic-gate 	r = rscap("srv_features", clientname, "", e, &pvp, pvpbuf,
48077c478bd9Sstevel@tonic-gate 		  sizeof(pvpbuf));
48087c478bd9Sstevel@tonic-gate 	if (r != EX_OK)
48097c478bd9Sstevel@tonic-gate 		return features;
48107c478bd9Sstevel@tonic-gate 	if (pvp == NULL || pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
48117c478bd9Sstevel@tonic-gate 		return features;
48127c478bd9Sstevel@tonic-gate 	if (pvp[1] != NULL && sm_strncasecmp(pvp[1], "temp", 4) == 0)
48137c478bd9Sstevel@tonic-gate 		return SRV_TMP_FAIL;
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	/*
48167c478bd9Sstevel@tonic-gate 	**  General rule (see sendmail.h, d_flags):
48177c478bd9Sstevel@tonic-gate 	**  lower case: required/offered, upper case: Not required/available
48187c478bd9Sstevel@tonic-gate 	**
48197c478bd9Sstevel@tonic-gate 	**  Since we can change some features per daemon, we have both
48207c478bd9Sstevel@tonic-gate 	**  cases here: turn on/off a feature.
48217c478bd9Sstevel@tonic-gate 	*/
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 	for (i = 1; pvp[i] != NULL; i++)
48247c478bd9Sstevel@tonic-gate 	{
48257c478bd9Sstevel@tonic-gate 		c = pvp[i][0];
48267c478bd9Sstevel@tonic-gate 		j = 0;
48277c478bd9Sstevel@tonic-gate 		for (;;)
48287c478bd9Sstevel@tonic-gate 		{
48297c478bd9Sstevel@tonic-gate 			if ((opt = srv_feat_table[j].srvf_opt) == '\0')
48307c478bd9Sstevel@tonic-gate 			{
48317c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
48327c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, e->e_id,
48337c478bd9Sstevel@tonic-gate 						  "srvfeatures: unknown feature %s",
48347c478bd9Sstevel@tonic-gate 						  pvp[i]);
48357c478bd9Sstevel@tonic-gate 				break;
48367c478bd9Sstevel@tonic-gate 			}
48377c478bd9Sstevel@tonic-gate 			if (c == opt)
48387c478bd9Sstevel@tonic-gate 			{
48397c478bd9Sstevel@tonic-gate 				features &= ~(srv_feat_table[j].srvf_flag);
48407c478bd9Sstevel@tonic-gate 				break;
48417c478bd9Sstevel@tonic-gate 			}
48427c478bd9Sstevel@tonic-gate 			if (c == tolower(opt))
48437c478bd9Sstevel@tonic-gate 			{
48447c478bd9Sstevel@tonic-gate 				features |= srv_feat_table[j].srvf_flag;
48457c478bd9Sstevel@tonic-gate 				break;
48467c478bd9Sstevel@tonic-gate 			}
48477c478bd9Sstevel@tonic-gate 			++j;
48487c478bd9Sstevel@tonic-gate 		}
48497c478bd9Sstevel@tonic-gate 	}
48507c478bd9Sstevel@tonic-gate 	return features;
48517c478bd9Sstevel@tonic-gate }
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate /*
48547c478bd9Sstevel@tonic-gate **  HELP -- implement the HELP command.
48557c478bd9Sstevel@tonic-gate **
48567c478bd9Sstevel@tonic-gate **	Parameters:
48577c478bd9Sstevel@tonic-gate **		topic -- the topic we want help for.
48587c478bd9Sstevel@tonic-gate **		e -- envelope.
48597c478bd9Sstevel@tonic-gate **
48607c478bd9Sstevel@tonic-gate **	Returns:
48617c478bd9Sstevel@tonic-gate **		none.
48627c478bd9Sstevel@tonic-gate **
48637c478bd9Sstevel@tonic-gate **	Side Effects:
48647c478bd9Sstevel@tonic-gate **		outputs the help file to message output.
48657c478bd9Sstevel@tonic-gate */
48667c478bd9Sstevel@tonic-gate #define HELPVSTR	"#vers	"
48677c478bd9Sstevel@tonic-gate #define HELPVERSION	2
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate void
help(topic,e)48707c478bd9Sstevel@tonic-gate help(topic, e)
48717c478bd9Sstevel@tonic-gate 	char *topic;
48727c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
48737c478bd9Sstevel@tonic-gate {
48747c478bd9Sstevel@tonic-gate 	register SM_FILE_T *hf;
48757c478bd9Sstevel@tonic-gate 	register char *p;
48767c478bd9Sstevel@tonic-gate 	int len;
48777c478bd9Sstevel@tonic-gate 	bool noinfo;
48787c478bd9Sstevel@tonic-gate 	bool first = true;
48797c478bd9Sstevel@tonic-gate 	long sff = SFF_OPENASROOT|SFF_REGONLY;
48807c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
48817c478bd9Sstevel@tonic-gate 	char inp[MAXLINE];
48827c478bd9Sstevel@tonic-gate 	static int foundvers = -1;
48837c478bd9Sstevel@tonic-gate 	extern char Version[];
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 	if (DontLockReadFiles)
48867c478bd9Sstevel@tonic-gate 		sff |= SFF_NOLOCK;
48877c478bd9Sstevel@tonic-gate 	if (!bitnset(DBS_HELPFILEINUNSAFEDIRPATH, DontBlameSendmail))
48887c478bd9Sstevel@tonic-gate 		sff |= SFF_SAFEDIRPATH;
48897c478bd9Sstevel@tonic-gate 
48907c478bd9Sstevel@tonic-gate 	if (HelpFile == NULL ||
48917c478bd9Sstevel@tonic-gate 	    (hf = safefopen(HelpFile, O_RDONLY, 0444, sff)) == NULL)
48927c478bd9Sstevel@tonic-gate 	{
48937c478bd9Sstevel@tonic-gate 		/* no help */
48947c478bd9Sstevel@tonic-gate 		errno = 0;
48957c478bd9Sstevel@tonic-gate 		message("502 5.3.0 Sendmail %s -- HELP not implemented",
48967c478bd9Sstevel@tonic-gate 			Version);
48977c478bd9Sstevel@tonic-gate 		return;
48987c478bd9Sstevel@tonic-gate 	}
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 	if (topic == NULL || *topic == '\0')
49017c478bd9Sstevel@tonic-gate 	{
49027c478bd9Sstevel@tonic-gate 		topic = "smtp";
49037c478bd9Sstevel@tonic-gate 		noinfo = false;
49047c478bd9Sstevel@tonic-gate 	}
49057c478bd9Sstevel@tonic-gate 	else
49067c478bd9Sstevel@tonic-gate 	{
49077c478bd9Sstevel@tonic-gate 		makelower(topic);
49087c478bd9Sstevel@tonic-gate 		noinfo = true;
49097c478bd9Sstevel@tonic-gate 	}
49107c478bd9Sstevel@tonic-gate 
49117c478bd9Sstevel@tonic-gate 	len = strlen(topic);
49127c478bd9Sstevel@tonic-gate 
4913058561cbSjbeck 	while (sm_io_fgets(hf, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
49147c478bd9Sstevel@tonic-gate 	{
49157c478bd9Sstevel@tonic-gate 		if (buf[0] == '#')
49167c478bd9Sstevel@tonic-gate 		{
49177c478bd9Sstevel@tonic-gate 			if (foundvers < 0 &&
49187c478bd9Sstevel@tonic-gate 			    strncmp(buf, HELPVSTR, strlen(HELPVSTR)) == 0)
49197c478bd9Sstevel@tonic-gate 			{
49207c478bd9Sstevel@tonic-gate 				int h;
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 				if (sm_io_sscanf(buf + strlen(HELPVSTR), "%d",
49237c478bd9Sstevel@tonic-gate 						 &h) == 1)
49247c478bd9Sstevel@tonic-gate 					foundvers = h;
49257c478bd9Sstevel@tonic-gate 			}
49267c478bd9Sstevel@tonic-gate 			continue;
49277c478bd9Sstevel@tonic-gate 		}
49287c478bd9Sstevel@tonic-gate 		if (strncmp(buf, topic, len) == 0)
49297c478bd9Sstevel@tonic-gate 		{
49307c478bd9Sstevel@tonic-gate 			if (first)
49317c478bd9Sstevel@tonic-gate 			{
49327c478bd9Sstevel@tonic-gate 				first = false;
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate 				/* print version if no/old vers# in file */
49357c478bd9Sstevel@tonic-gate 				if (foundvers < 2 && !noinfo)
49367c478bd9Sstevel@tonic-gate 					message("214-2.0.0 This is Sendmail version %s", Version);
49377c478bd9Sstevel@tonic-gate 			}
49387c478bd9Sstevel@tonic-gate 			p = strpbrk(buf, " \t");
49397c478bd9Sstevel@tonic-gate 			if (p == NULL)
49407c478bd9Sstevel@tonic-gate 				p = buf + strlen(buf) - 1;
49417c478bd9Sstevel@tonic-gate 			else
49427c478bd9Sstevel@tonic-gate 				p++;
49437c478bd9Sstevel@tonic-gate 			fixcrlf(p, true);
49447c478bd9Sstevel@tonic-gate 			if (foundvers >= 2)
49457c478bd9Sstevel@tonic-gate 			{
4946058561cbSjbeck 				char *lbp;
4947058561cbSjbeck 				int lbs = sizeof(buf) - (p - buf);
4948058561cbSjbeck 
4949058561cbSjbeck 				lbp = translate_dollars(p, p, &lbs);
4950058561cbSjbeck 				expand(lbp, inp, sizeof(inp), e);
4951058561cbSjbeck 				if (p != lbp)
4952058561cbSjbeck 					sm_free(lbp);
49537c478bd9Sstevel@tonic-gate 				p = inp;
49547c478bd9Sstevel@tonic-gate 			}
49557c478bd9Sstevel@tonic-gate 			message("214-2.0.0 %s", p);
49567c478bd9Sstevel@tonic-gate 			noinfo = false;
49577c478bd9Sstevel@tonic-gate 		}
49587c478bd9Sstevel@tonic-gate 	}
49597c478bd9Sstevel@tonic-gate 
49607c478bd9Sstevel@tonic-gate 	if (noinfo)
49617c478bd9Sstevel@tonic-gate 		message("504 5.3.0 HELP topic \"%.10s\" unknown", topic);
49627c478bd9Sstevel@tonic-gate 	else
49637c478bd9Sstevel@tonic-gate 		message("214 2.0.0 End of HELP info");
49647c478bd9Sstevel@tonic-gate 
49657c478bd9Sstevel@tonic-gate 	if (foundvers != 0 && foundvers < HELPVERSION)
49667c478bd9Sstevel@tonic-gate 	{
49677c478bd9Sstevel@tonic-gate 		if (LogLevel > 1)
49687c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, e->e_id,
49697c478bd9Sstevel@tonic-gate 				  "%s too old (require version %d)",
49707c478bd9Sstevel@tonic-gate 				  HelpFile, HELPVERSION);
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 		/* avoid log next time */
49737c478bd9Sstevel@tonic-gate 		foundvers = 0;
49747c478bd9Sstevel@tonic-gate 	}
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate 	(void) sm_io_close(hf, SM_TIME_DEFAULT);
49777c478bd9Sstevel@tonic-gate }
49787c478bd9Sstevel@tonic-gate 
49797c478bd9Sstevel@tonic-gate #if SASL
49807c478bd9Sstevel@tonic-gate /*
49817c478bd9Sstevel@tonic-gate **  RESET_SASLCONN -- reset SASL connection data
49827c478bd9Sstevel@tonic-gate **
49837c478bd9Sstevel@tonic-gate **	Parameters:
49847c478bd9Sstevel@tonic-gate **		conn -- SASL connection context
49857c478bd9Sstevel@tonic-gate **		hostname -- host name
49867c478bd9Sstevel@tonic-gate **		various connection data
49877c478bd9Sstevel@tonic-gate **
49887c478bd9Sstevel@tonic-gate **	Returns:
49897c478bd9Sstevel@tonic-gate **		SASL result
49907c478bd9Sstevel@tonic-gate */
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate static int
reset_saslconn(sasl_conn_t ** conn,char * hostname,char * remoteip,char * localip,char * auth_id,sasl_ssf_t * ext_ssf)49937c478bd9Sstevel@tonic-gate reset_saslconn(sasl_conn_t **conn, char *hostname,
49947c478bd9Sstevel@tonic-gate # if SASL >= 20000
49957c478bd9Sstevel@tonic-gate 	       char *remoteip, char *localip,
49967c478bd9Sstevel@tonic-gate 	       char *auth_id, sasl_ssf_t * ext_ssf)
49977c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
49987c478bd9Sstevel@tonic-gate 	       struct sockaddr_in *saddr_r, struct sockaddr_in *saddr_l,
49997c478bd9Sstevel@tonic-gate 	       sasl_external_properties_t * ext_ssf)
50007c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
50017c478bd9Sstevel@tonic-gate {
50027c478bd9Sstevel@tonic-gate 	int result;
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate 	sasl_dispose(conn);
50057c478bd9Sstevel@tonic-gate # if SASL >= 20000
50067c478bd9Sstevel@tonic-gate 	result = sasl_server_new("smtp", hostname, NULL, NULL, NULL,
50077c478bd9Sstevel@tonic-gate 				 NULL, 0, conn);
50087c478bd9Sstevel@tonic-gate # elif SASL > 10505
50097c478bd9Sstevel@tonic-gate 	/* use empty realm: only works in SASL > 1.5.5 */
50107c478bd9Sstevel@tonic-gate 	result = sasl_server_new("smtp", hostname, "", NULL, 0, conn);
50117c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
50127c478bd9Sstevel@tonic-gate 	/* use no realm -> realm is set to hostname by SASL lib */
50137c478bd9Sstevel@tonic-gate 	result = sasl_server_new("smtp", hostname, NULL, NULL, 0,
50147c478bd9Sstevel@tonic-gate 				 conn);
50157c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
50167c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50177c478bd9Sstevel@tonic-gate 		return result;
50187c478bd9Sstevel@tonic-gate 
50197c478bd9Sstevel@tonic-gate # if SASL >= 20000
50207c478bd9Sstevel@tonic-gate #  if NETINET || NETINET6
50217c478bd9Sstevel@tonic-gate 	if (remoteip != NULL && *remoteip != '\0')
50227c478bd9Sstevel@tonic-gate 		result = sasl_setprop(*conn, SASL_IPREMOTEPORT, remoteip);
50237c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50247c478bd9Sstevel@tonic-gate 		return result;
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 	if (localip != NULL && *localip != '\0')
50277c478bd9Sstevel@tonic-gate 		result = sasl_setprop(*conn, SASL_IPLOCALPORT, localip);
50287c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50297c478bd9Sstevel@tonic-gate 		return result;
50307c478bd9Sstevel@tonic-gate #  endif /* NETINET || NETINET6 */
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	result = sasl_setprop(*conn, SASL_SSF_EXTERNAL, ext_ssf);
50337c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50347c478bd9Sstevel@tonic-gate 		return result;
50357c478bd9Sstevel@tonic-gate 
50367c478bd9Sstevel@tonic-gate 	result = sasl_setprop(*conn, SASL_AUTH_EXTERNAL, auth_id);
50377c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50387c478bd9Sstevel@tonic-gate 		return result;
50397c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
50407c478bd9Sstevel@tonic-gate #  if NETINET
50417c478bd9Sstevel@tonic-gate 	if (saddr_r != NULL)
50427c478bd9Sstevel@tonic-gate 		result = sasl_setprop(*conn, SASL_IP_REMOTE, saddr_r);
50437c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50447c478bd9Sstevel@tonic-gate 		return result;
50457c478bd9Sstevel@tonic-gate 
50467c478bd9Sstevel@tonic-gate 	if (saddr_l != NULL)
50477c478bd9Sstevel@tonic-gate 		result = sasl_setprop(*conn, SASL_IP_LOCAL, saddr_l);
50487c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50497c478bd9Sstevel@tonic-gate 		return result;
50507c478bd9Sstevel@tonic-gate #  endif /* NETINET */
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 	result = sasl_setprop(*conn, SASL_SSF_EXTERNAL, ext_ssf);
50537c478bd9Sstevel@tonic-gate 	if (result != SASL_OK)
50547c478bd9Sstevel@tonic-gate 		return result;
50557c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
50567c478bd9Sstevel@tonic-gate 	return SASL_OK;
50577c478bd9Sstevel@tonic-gate }
50587c478bd9Sstevel@tonic-gate #endif /* SASL */
5059