1295Seric /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  *
8*61088Seric  *	@(#)sendmail.h	6.73 (Berkeley) 06/03/93
933731Sbostic  */
1022727Sdist 
1122727Sdist /*
123313Seric **  SENDMAIL.H -- Global definitions for sendmail.
13295Seric */
14295Seric 
154371Seric # ifdef _DEFINE
164371Seric # define EXTERN
175194Seric # ifndef lint
18*61088Seric static char SmailSccsId[] =	"@(#)sendmail.h	6.73		06/03/93";
1958889Seric # endif
2056795Seric # else /*  _DEFINE */
214371Seric # define EXTERN extern
2256795Seric # endif /* _DEFINE */
23295Seric 
2458331Seric # include <unistd.h>
2556795Seric # include <stddef.h>
2656795Seric # include <stdlib.h>
274183Seric # include <stdio.h>
284183Seric # include <ctype.h>
297355Seric # include <setjmp.h>
3051961Seric # include <sysexits.h>
3156795Seric # include <string.h>
3256795Seric # include <time.h>
3356795Seric # include <errno.h>
3456795Seric 
359144Seric # include "conf.h"
361390Seric # include "useful.h"
371390Seric 
387674Seric # ifdef LOG
3956215Seric # include <syslog.h>
4056795Seric # endif /* LOG */
4110679Seric 
4224943Seric # ifdef DAEMON
4324943Seric # include <sys/socket.h>
4458778Seric # endif
4558778Seric # ifdef NETINET
4624943Seric # include <netinet/in.h>
4758778Seric # endif
4858778Seric # ifdef NETISO
4958778Seric # include <netiso/iso.h>
5058778Seric # endif
5158871Seric # ifdef NETNS
5258871Seric # include <netns/ns.h>
5358871Seric # endif
5458871Seric # ifdef NETX25
5558871Seric # include <netccitt/x25.h>
5658871Seric # endif
5710679Seric 
5824943Seric 
5916907Seric 
6016907Seric 
6110679Seric /*
6210679Seric **  Data structure for bit maps.
6310679Seric **
6410679Seric **	Each bit in this map can be referenced by an ascii character.
6510679Seric **	This is 128 possible bits, or 12 8-bit bytes.
6610679Seric */
6710679Seric 
6810679Seric #define BITMAPBYTES	16	/* number of bytes in a bit map */
6910679Seric #define BYTEBITS	8	/* number of bits in a byte */
7010679Seric 
7110679Seric /* internal macros */
7210679Seric #define _BITWORD(bit)	(bit / (BYTEBITS * sizeof (int)))
7310679Seric #define _BITBIT(bit)	(1 << (bit % (BYTEBITS * sizeof (int))))
7410679Seric 
7510679Seric typedef int	BITMAP[BITMAPBYTES / sizeof (int)];
7610679Seric 
7710679Seric /* test bit number N */
7810679Seric #define bitnset(bit, map)	((map)[_BITWORD(bit)] & _BITBIT(bit))
7910679Seric 
8010679Seric /* set bit number N */
8110679Seric #define setbitn(bit, map)	(map)[_BITWORD(bit)] |= _BITBIT(bit)
8210679Seric 
8310679Seric /* clear bit number N */
8410679Seric #define clrbitn(bit, map)	(map)[_BITWORD(bit)] &= ~_BITBIT(bit)
8510679Seric 
8610679Seric /* clear an entire bit map */
8711053Seric #define clrbitmap(map)		bzero((char *) map, BITMAPBYTES)
887799Seric /*
893190Seric **  Address structure.
903190Seric **	Addresses are stored internally in this structure.
913190Seric */
923190Seric 
933190Seric struct address
943190Seric {
953190Seric 	char		*q_paddr;	/* the printname for the address */
963190Seric 	char		*q_user;	/* user name */
9740973Sbostic 	char		*q_ruser;	/* real user name, or NULL if q_user */
983190Seric 	char		*q_host;	/* host name */
994597Seric 	struct mailer	*q_mailer;	/* mailer to use */
1004149Seric 	u_short		q_flags;	/* status flags, see below */
10155369Seric 	uid_t		q_uid;		/* user-id of receiver (if known) */
10255369Seric 	gid_t		q_gid;		/* group-id of receiver (if known) */
1034079Seric 	char		*q_home;	/* home dir (local mailer only) */
1044995Seric 	char		*q_fullname;	/* full name if known */
1054995Seric 	struct address	*q_next;	/* chain */
1064995Seric 	struct address	*q_alias;	/* address this results from */
10758170Seric 	char		*q_owner;	/* owner of q_alias */
1085034Seric 	struct address	*q_tchain;	/* temporary use chain */
1094987Seric 	time_t		q_timeout;	/* timeout for this address */
1103190Seric };
1113190Seric 
1123190Seric typedef struct address ADDRESS;
1133190Seric 
1143190Seric # define QDONTSEND	000001	/* don't send to this address */
1154068Seric # define QBADADDR	000002	/* this address is verified bad */
1164403Seric # define QGOODUID	000004	/* the q_uid q_gid fields are good */
1174422Seric # define QPRIMARY	000010	/* set from argv */
1184624Seric # define QQUEUEUP	000020	/* queue for later transmission */
11947285Seric # define QSENT		000040	/* has been successfully delivered */
12051316Seric # define QNOTREMOTE	000100	/* not an address for remote forwarding */
12158060Seric # define QSELFREF	000200	/* this address references itself */
12258154Seric # define QVERIFIED	000400	/* verified, but not expanded */
1236902Seric /*
124295Seric **  Mailer definition structure.
125295Seric **	Every mailer known to the system is declared in this
126295Seric **	structure.  It defines the pathname of the mailer, some
127295Seric **	flags associated with it, and the argument vector to
1281390Seric **	pass to it.  The flags are defined in conf.c
129295Seric **
1304171Seric **	The argument vector is expanded before actual use.  All
1314171Seric **	words except the first are passed through the macro
1324171Seric **	processor.
133295Seric */
134295Seric 
135295Seric struct mailer
136295Seric {
1373190Seric 	char	*m_name;	/* symbolic name of this mailer */
138295Seric 	char	*m_mailer;	/* pathname of the mailer to use */
13910679Seric 	BITMAP	m_flags;	/* status flags, see below */
1404438Seric 	short	m_mno;		/* mailer number internally */
1413049Seric 	char	**m_argv;	/* template argument vector */
14258013Seric 	short	m_sh_rwset;	/* rewrite set: sender header addresses */
14358013Seric 	short	m_se_rwset;	/* rewrite set: sender envelope addresses */
14458013Seric 	short	m_rh_rwset;	/* rewrite set: recipient header addresses */
14558013Seric 	short	m_re_rwset;	/* rewrite set: recipient envelope addresses */
14610323Seric 	char	*m_eol;		/* end of line string */
14710679Seric 	long	m_maxsize;	/* size limit on message to this mailer */
14852106Seric 	int	m_linelimit;	/* max # characters per line */
14958932Seric 	char	*m_execdir;	/* directory to chdir to before execv */
150295Seric };
151295Seric 
1524100Seric typedef struct mailer	MAILER;
1534100Seric 
1545601Seric /* bits for m_flags */
15559280Seric # define M_ESMTP	'a'	/* run Extended SMTP protocol */
15659532Seric # define M_BLANKEND	'b'	/* ensure blank line at end of message */
15758035Seric # define M_NOCOMMENT	'c'	/* don't include comment part of address */
15816871Seric # define M_CANONICAL	'C'	/* make addresses canonical "u@dom" */
15958035Seric 		/*	'D'	/* CF: include Date: */
16016871Seric # define M_EXPENSIVE	'e'	/* it costs to use this mailer.... */
16116871Seric # define M_ESCFROM	'E'	/* escape From lines to >From */
16210679Seric # define M_FOPT		'f'	/* mailer takes picky -f flag */
16358035Seric 		/*	'F'	/* CF: include From: or Resent-From: */
16459537Seric # define M_NO_NULL_FROM	'g'	/* sender of errors should be $g */
16516871Seric # define M_HST_UPPER	'h'	/* preserve host case distinction */
16658035Seric 		/*	'H'	/* UIUC: MAIL11V3: preview headers */
16716871Seric # define M_INTERNAL	'I'	/* SMTP to another sendmail site */
16858139Seric # define M_LOCALMAILER	'l'	/* delivery is to this host */
16916871Seric # define M_LIMITS	'L'	/* must enforce SMTP line limits */
17016871Seric # define M_MUSER	'm'	/* can handle multiple users at once */
17158035Seric 		/*	'M'	/* CF: include Message-Id: */
17216871Seric # define M_NHDR		'n'	/* don't insert From line */
17358035Seric 		/*	'N'	/* UIUC: MAIL11V3: DATA returns multi-status */
17416871Seric # define M_FROMPATH	'p'	/* use reverse-path in MAIL FROM: */
17558035Seric 		/*	'P'	/* CF: include Return-Path: */
17610679Seric # define M_ROPT		'r'	/* mailer takes picky -r flag */
17717466Seric # define M_SECURE_PORT	'R'	/* try to send on a reserved TCP port */
17816871Seric # define M_STRIPQ	's'	/* strip quote chars from user/host */
17910679Seric # define M_RESTR	'S'	/* must be daemon to execute */
18010679Seric # define M_USR_UPPER	'u'	/* preserve user case distinction */
18110679Seric # define M_UGLYUUCP	'U'	/* this wants an ugly UUCP from line */
18258035Seric 		/*	'V'	/* UIUC: !-relativize all addresses */
18359280Seric 		/*	'x'	/* CF: include Full-Name: */
18410679Seric # define M_XDOT		'X'	/* use hidden-dot algorithm */
18552106Seric # define M_7BITS	'7'	/* use 7-bit path */
186295Seric 
1874597Seric EXTERN MAILER	*Mailer[MAXMAILERS+1];
188295Seric 
1894597Seric EXTERN MAILER	*LocalMailer;		/* ptr to local mailer */
1904597Seric EXTERN MAILER	*ProgMailer;		/* ptr to program mailer */
19157393Seric EXTERN MAILER	*FileMailer;		/* ptr to *file* mailer */
19257393Seric EXTERN MAILER	*InclMailer;		/* ptr to *include* mailer */
1936902Seric /*
1942899Seric **  Header structure.
1952899Seric **	This structure is used internally to store header items.
1962899Seric */
197295Seric 
1982899Seric struct header
1992899Seric {
2002899Seric 	char		*h_field;	/* the name of the field */
2012899Seric 	char		*h_value;	/* the value of that field */
2022899Seric 	struct header	*h_link;	/* the next header */
2034149Seric 	u_short		h_flags;	/* status bits, see below */
20410679Seric 	BITMAP		h_mflags;	/* m_flags bits needed */
2052899Seric };
206295Seric 
2072899Seric typedef struct header	HDR;
2082899Seric 
209295Seric /*
2102899Seric **  Header information structure.
2112899Seric **	Defined in conf.c, this struct declares the header fields
2122899Seric **	that have some magic meaning.
2132899Seric */
2142899Seric 
2152899Seric struct hdrinfo
2162899Seric {
2172899Seric 	char	*hi_field;	/* the name of the field */
2184149Seric 	u_short	hi_flags;	/* status bits, see below */
2192899Seric };
2202899Seric 
2212899Seric extern struct hdrinfo	HdrInfo[];
2222899Seric 
2232899Seric /* bits for h_flags and hi_flags */
2243060Seric # define H_EOH		00001	/* this field terminates header */
2255918Seric # define H_RCPT		00002	/* contains recipient addresses */
2262899Seric # define H_DEFAULT	00004	/* if another value is found, drop this */
22711416Seric # define H_RESENT	00010	/* this address is a "Resent-..." address */
2283386Seric # define H_CHECK	00020	/* check h_mflags against m_flags */
2293390Seric # define H_ACHECK	00040	/* ditto, but always (not just default) */
2304149Seric # define H_FORCE	00100	/* force this field, even if default */
2318061Seric # define H_TRACE	00200	/* this field contains trace information */
2327761Seric # define H_FROM		00400	/* this is a from-type field */
23324945Seric # define H_VALID	01000	/* this field has a validated value */
23457359Seric # define H_RECEIPTTO	02000	/* this field has return receipt info */
23557359Seric # define H_ERRORSTO	04000	/* this field has error address info */
2366902Seric /*
2376902Seric **  Envelope structure.
2386902Seric **	This structure defines the message itself.  There is usually
2396902Seric **	only one of these -- for the message that we originally read
2406902Seric **	and which is our primary interest -- but other envelopes can
2416902Seric **	be generated during processing.  For example, error messages
2426902Seric **	will have their own envelope.
2436902Seric */
2442899Seric 
24559153Seric # define ENVELOPE	struct envelope
24659153Seric 
24759153Seric ENVELOPE
2486902Seric {
2496987Seric 	HDR		*e_header;	/* head of header list */
2506987Seric 	long		e_msgpriority;	/* adjusted priority of this message */
2517859Seric 	time_t		e_ctime;	/* time message appeared in the queue */
2526987Seric 	char		*e_to;		/* the target person */
2538061Seric 	char		*e_receiptto;	/* return receipt address */
2546987Seric 	ADDRESS		e_from;		/* the person it is from */
25558703Seric 	char		*e_sender;	/* e_from.q_paddr w comments stripped */
2568180Seric 	char		**e_fromdomain;	/* the domain part of the sender */
2576987Seric 	ADDRESS		*e_sendqueue;	/* list of message recipients */
2587044Seric 	ADDRESS		*e_errorqueue;	/* the queue for error responses */
2596987Seric 	long		e_msgsize;	/* size of the message in bytes */
26024943Seric 	int		e_nrcpts;	/* number of recipients */
2616987Seric 	short		e_class;	/* msg class (priority, junk, etc.) */
2629336Seric 	short		e_flags;	/* flags, see below */
2639373Seric 	short		e_hopcount;	/* number of times processed */
26457585Seric 	short		e_nsent;	/* number of sends since checkpoint */
26558734Seric 	short		e_sendmode;	/* message send mode */
26658734Seric 	short		e_errormode;	/* error return mode */
26759153Seric 	int		(*e_puthdr)__P((FILE *, MAILER *, ENVELOPE *));
26859153Seric 					/* function to put header of message */
26959722Seric 	int		(*e_putbody)__P((FILE *, MAILER *, ENVELOPE *, char *));
27059153Seric 					/* function to put body of message */
2716987Seric 	struct envelope	*e_parent;	/* the message this one encloses */
2729336Seric 	struct envelope *e_sibling;	/* the next envelope of interest */
27359090Seric 	char		*e_bodytype;	/* type of message body */
2746987Seric 	char		*e_df;		/* location of temp file */
2759541Seric 	FILE		*e_dfp;		/* temporary file */
2767810Seric 	char		*e_id;		/* code for this entry in queue */
2779541Seric 	FILE		*e_xfp;		/* transcript file */
27851920Seric 	FILE		*e_lockfp;	/* the lock file for this message */
27910102Seric 	char		*e_message;	/* error message */
28058913Seric 	char		*e_statmsg;	/* stat msg (changes per delivery) */
28159710Seric 	char		*e_msgboundary;	/* MIME-style message part boundary */
2826987Seric 	char		*e_macro[128];	/* macro definitions */
2836902Seric };
2842899Seric 
2859336Seric /* values for e_flags */
2869336Seric #define EF_OLDSTYLE	000001		/* use spaces (not commas) in hdrs */
2879336Seric #define EF_INQUEUE	000002		/* this message is fully queued */
2889336Seric #define EF_TIMEOUT	000004		/* this message is too old */
2899336Seric #define EF_CLRQUEUE	000010		/* disk copy is no longer needed */
2909336Seric #define EF_SENDRECEIPT	000020		/* send a return receipt */
2919336Seric #define EF_FATALERRS	000040		/* fatal errors occured */
2929336Seric #define EF_KEEPQUEUE	000100		/* keep queue files always */
2939373Seric #define EF_RESPONSE	000200		/* this is an error or return receipt */
29411416Seric #define EF_RESENT	000400		/* this message is being forwarded */
29558091Seric #define EF_VRFYONLY	001000		/* verify only (don't expand aliases) */
29658737Seric #define EF_WARNING	002000		/* warning message has been sent */
29758737Seric #define EF_QUEUERUN	004000		/* this envelope is from queue */
2989336Seric 
2996902Seric EXTERN ENVELOPE	*CurEnv;	/* envelope currently being processed */
3006902Seric /*
30124980Seric **  Message priority classes.
3025034Seric **
30324980Seric **	The message class is read directly from the Priority: header
30424980Seric **	field in the message.
3056910Seric **
30624980Seric **	CurEnv->e_msgpriority is the number of bytes in the message plus
30724980Seric **	the creation time (so that jobs ``tend'' to be ordered correctly),
30824980Seric **	adjusted by the message class, the number of recipients, and the
30924980Seric **	amount of time the message has been sitting around.  This number
31024980Seric **	is used to order the queue.  Higher values mean LOWER priority.
31112516Seric **
31224980Seric **	Each priority class point is worth WkClassFact priority points;
31324980Seric **	each recipient is worth WkRecipFact priority points.  Each time
31424980Seric **	we reprocess a message the priority is adjusted by WkTimeFact.
31524980Seric **	WkTimeFact should normally decrease the priority so that jobs
31624980Seric **	that have historically failed will be run later; thanks go to
31724980Seric **	Jay Lepreau at Utah for pointing out the error in my thinking.
31824980Seric **
3196910Seric **	The "class" is this number, unadjusted by the age or size of
3206910Seric **	this message.  Classes with negative representations will have
3216910Seric **	error messages thrown away if they are not local.
3224624Seric */
3234624Seric 
3248249Seric struct priority
3258249Seric {
3268249Seric 	char	*pri_name;	/* external name of priority */
3278249Seric 	int	pri_val;	/* internal value for same */
3288249Seric };
3294624Seric 
3308249Seric EXTERN struct priority	Priorities[MAXPRIORITIES];
3318249Seric EXTERN int		NumPriorities;	/* pointer into Priorities */
3326902Seric /*
3333153Seric **  Rewrite rules.
3343153Seric */
3352899Seric 
3363153Seric struct rewrite
3373153Seric {
3383153Seric 	char	**r_lhs;	/* pattern match */
3393153Seric 	char	**r_rhs;	/* substitution value */
3403153Seric 	struct rewrite	*r_next;/* next in chain */
3413153Seric };
3422899Seric 
3438057Seric EXTERN struct rewrite	*RewriteRules[MAXRWSETS];
3443153Seric 
3458057Seric /*
3468057Seric **  Special characters in rewriting rules.
3478057Seric **	These are used internally only.
3488057Seric **	The COND* rules are actually used in macros rather than in
3498057Seric **		rewriting rules, but are given here because they
3508057Seric **		cannot conflict.
3518057Seric */
3523153Seric 
3538057Seric /* left hand side items */
35458050Seric # define MATCHZANY	0220	/* match zero or more tokens */
35558050Seric # define MATCHANY	0221	/* match one or more tokens */
35658050Seric # define MATCHONE	0222	/* match exactly one token */
35758050Seric # define MATCHCLASS	0223	/* match one token in a class */
35858050Seric # define MATCHNCLASS	0224	/* match anything not in class */
35958050Seric # define MATCHREPL	0225	/* replacement on RHS for above */
3608057Seric 
3618057Seric /* right hand side items */
36258050Seric # define CANONNET	0226	/* canonical net, next token */
36358050Seric # define CANONHOST	0227	/* canonical host, next token */
36458050Seric # define CANONUSER	0230	/* canonical user, next N tokens */
36558050Seric # define CALLSUBR	0231	/* call another rewriting set */
3663153Seric 
3678057Seric /* conditionals in macros */
36858050Seric # define CONDIF		0232	/* conditional if-then */
36958050Seric # define CONDELSE	0233	/* conditional else */
37058050Seric # define CONDFI		0234	/* conditional fi */
37116151Seric 
37216905Seric /* bracket characters for host name lookup */
37358050Seric # define HOSTBEGIN	0235	/* hostname lookup begin */
37458050Seric # define HOSTEND	0236	/* hostname lookup end */
37516905Seric 
37653751Seric /* bracket characters for generalized lookup */
37758050Seric # define LOOKUPBEGIN	0205	/* generalized lookup begin */
37858050Seric # define LOOKUPEND	0206	/* generalized lookup end */
37953751Seric 
38058050Seric /* macro substitution character */
38158050Seric # define MACROEXPAND	0201	/* macro expansion */
38259024Seric # define MACRODEXPAND	0202	/* deferred macro expansion */
38351782Seric 
38458829Seric /* to make the code clearer */
38558829Seric # define MATCHZERO	CANONHOST
38658829Seric 
38751782Seric /* external <==> internal mapping table */
38851782Seric struct metamac
38951782Seric {
39051782Seric 	char	metaname;	/* external code (after $) */
39151782Seric 	char	metaval;	/* internal code (as above) */
39251782Seric };
3936902Seric /*
39453741Seric **  Information about currently open connections to mailers, or to
39553741Seric **  hosts that we have looked up recently.
39624943Seric */
39724943Seric 
39854967Seric # define MCI	struct mailer_con_info
39924943Seric 
40054967Seric MCI
40124943Seric {
40253741Seric 	short		mci_flags;	/* flag bits, see below */
40353741Seric 	short		mci_errno;	/* error number on last connection */
40453741Seric 	short		mci_exitstat;	/* exit status from last connection */
40555467Seric 	short		mci_state;	/* SMTP state */
40659262Seric 	long		mci_maxsize;	/* max size this server will accept */
40753741Seric 	FILE		*mci_in;	/* input side of connection */
40853741Seric 	FILE		*mci_out;	/* output side of connection */
40953741Seric 	int		mci_pid;	/* process id of subordinate proc */
41053751Seric 	char		*mci_phase;	/* SMTP phase string */
41153751Seric 	struct mailer	*mci_mailer;	/* ptr to the mailer for this conn */
41255467Seric 	char		*mci_host;	/* host name */
41353751Seric 	time_t		mci_lastuse;	/* last usage time */
41424943Seric };
41524943Seric 
41624943Seric 
41724943Seric /* flag bits */
41859281Seric #define MCIF_VALID	000001		/* this entry is valid */
41959281Seric #define MCIF_TEMP	000002		/* don't cache this connection */
42059281Seric #define MCIF_CACHED	000004		/* currently in open cache */
42159281Seric #define MCIF_ESMTP	000010		/* this host speaks ESMTP */
42259281Seric #define MCIF_EXPN	000020		/* EXPN command supported */
42359281Seric #define MCIF_SIZE	000040		/* SIZE option supported */
42459281Seric #define MCIF_8BITMIME	000100		/* BODY=8BITMIME supported */
42553654Seric 
42653741Seric /* states */
42753741Seric #define MCIS_CLOSED	0		/* no traffic on this connection */
42853751Seric #define MCIS_OPENING	1		/* sending initial protocol */
42953751Seric #define MCIS_OPEN	2		/* open, initial protocol sent */
43053751Seric #define MCIS_ACTIVE	3		/* message being sent */
43154967Seric #define MCIS_QUITING	4		/* running quit protocol */
43254967Seric #define MCIS_SSD	5		/* service shutting down */
43354967Seric #define MCIS_ERROR	6		/* I/O error on connection */
43424943Seric /*
43559652Seric **  Name canonification short circuit.
43659652Seric **
43759652Seric **	If the name server for a host is down, the process of trying to
43859652Seric **	canonify the name can hang.  This is similar to (but alas, not
43959652Seric **	identical to) looking up the name for delivery.  This stab type
44059652Seric **	caches the result of the name server lookup so we don't hang
44159652Seric **	multiple times.
44259652Seric */
44359652Seric 
44459652Seric #define NAMECANON	struct _namecanon
44559652Seric 
44659652Seric NAMECANON
44759652Seric {
44859652Seric 	short		nc_errno;	/* cached errno */
44959652Seric 	short		nc_herrno;	/* cached h_errno */
45059652Seric 	short		nc_stat;	/* cached exit status code */
45159653Seric 	short		nc_flags;	/* flag bits */
45259652Seric 	char		*nc_cname;	/* the canonical name */
45359652Seric };
45459653Seric 
45559653Seric /* values for nc_flags */
45659653Seric #define NCF_VALID	0x0001	/* entry valid */
45759652Seric /*
45853751Seric **  Mapping functions
45953751Seric **
46053751Seric **	These allow arbitrary mappings in the config file.  The idea
46153751Seric **	(albeit not the implementation) comes from IDA sendmail.
46253751Seric */
46353751Seric 
46453751Seric # define MAPCLASS	struct _mapclass
46559153Seric # define MAP		struct _map
46653751Seric 
46753751Seric 
46853751Seric /*
46953751Seric **  An actual map.
47053751Seric */
47153751Seric 
47253751Seric MAP
47353751Seric {
47453751Seric 	MAPCLASS	*map_class;	/* the class of this map */
47560089Seric 	char		*map_mname;	/* name of this map */
47660207Seric 	int		map_mflags;	/* flags, see below */
47753751Seric 	char		*map_file;	/* the (nominal) filename */
47860089Seric 	void		*map_db1;	/* the open database ptr */
47960089Seric 	void		*map_db2;	/* an "extra" database pointer */
48056836Seric 	char		*map_app;	/* to append to successful matches */
48157208Seric 	char		*map_domain;	/* the (nominal) NIS domain */
48258962Seric 	char		*map_rebuild;	/* program to run to do auto-rebuild */
48353751Seric };
48453751Seric 
48553751Seric /* bit values for map_flags */
48660089Seric # define MF_VALID	0x0001		/* this entry is valid */
48760089Seric # define MF_INCLNULL	0x0002		/* include null byte in key */
48860089Seric # define MF_OPTIONAL	0x0004		/* don't complain if map not found */
48960089Seric # define MF_NOFOLDCASE	0x0008		/* don't fold case in keys */
49060089Seric # define MF_MATCHONLY	0x0010		/* don't use the map value */
49160089Seric # define MF_OPEN	0x0020		/* this entry is open */
49260089Seric # define MF_WRITABLE	0x0040		/* open for writing */
49360207Seric # define MF_ALIAS	0x0080		/* this is an alias file */
49460089Seric # define MF_IMPL_HASH	0x1000		/* implicit: underlying hash database */
49560089Seric # define MF_IMPL_NDBM	0x2000		/* implicit: underlying NDBM database */
49659153Seric 
49759153Seric 
49859153Seric /*
49959153Seric **  The class of a map -- essentially the functions to call
50059153Seric */
50159153Seric 
50259153Seric MAPCLASS
50359153Seric {
50460089Seric 	char	*map_cname;		/* name of this map class */
50560089Seric 	char	*map_ext;		/* extension for database file */
50660207Seric 	short	map_cflags;		/* flag bits, see below */
50760089Seric 	bool	(*map_parse)__P((MAP *, char *));
50860089Seric 					/* argument parsing function */
50960089Seric 	char	*(*map_lookup)__P((MAP *, char *, char **, int *));
51059153Seric 					/* lookup function */
51160089Seric 	void	(*map_store)__P((MAP *, char *, char *));
51260089Seric 					/* store function */
51360089Seric 	bool	(*map_open)__P((MAP *, int));
51460089Seric 					/* open function */
51560089Seric 	void	(*map_close)__P((MAP *));
51660089Seric 					/* close function */
51759153Seric };
51860207Seric 
51960207Seric /* bit values for map_cflags */
52060207Seric #define MCF_ALIASOK	0x0001		/* can be used for aliases */
52160207Seric #define MCF_ALIASONLY	0x0002		/* usable only for aliases */
52260207Seric #define MCF_REBUILDABLE	0x0004		/* can rebuild alias files */
52353751Seric /*
5244056Seric **  Symbol table definitions
5254056Seric */
5263153Seric 
5274056Seric struct symtab
5284056Seric {
5294056Seric 	char		*s_name;	/* name to be entered */
5304100Seric 	char		s_type;		/* general type (see below) */
5314056Seric 	struct symtab	*s_next;	/* pointer to next in chain */
5324100Seric 	union
5334100Seric 	{
53424943Seric 		BITMAP		sv_class;	/* bit-map of word classes */
53524943Seric 		ADDRESS		*sv_addr;	/* pointer to address header */
53624943Seric 		MAILER		*sv_mailer;	/* pointer to mailer */
53724943Seric 		char		*sv_alias;	/* alias */
53860207Seric 		MAPCLASS	sv_mapclass;	/* mapping function class */
53953751Seric 		MAP		sv_map;		/* mapping function */
54057443Seric 		char		*sv_hostsig;	/* host signature */
54154967Seric 		MCI		sv_mci;		/* mailer connection info */
54259652Seric 		NAMECANON	sv_namecanon;	/* canonical name cache */
5434100Seric 	}	s_value;
5444056Seric };
5454056Seric 
5464056Seric typedef struct symtab	STAB;
5474056Seric 
5484100Seric /* symbol types */
5494100Seric # define ST_UNDEF	0	/* undefined type */
5504100Seric # define ST_CLASS	1	/* class map */
5514100Seric # define ST_ADDRESS	2	/* an address in parsed format */
5524100Seric # define ST_MAILER	3	/* a mailer header */
5534100Seric # define ST_ALIAS	4	/* an alias */
55453751Seric # define ST_MAPCLASS	5	/* mapping function class */
55553751Seric # define ST_MAP		6	/* mapping function */
55657443Seric # define ST_HOSTSIG	7	/* host signature */
55759652Seric # define ST_NAMECANON	8	/* cached canonical name */
55854967Seric # define ST_MCI		16	/* mailer connection info (offset) */
5594100Seric 
5604100Seric # define s_class	s_value.sv_class
5615976Seric # define s_address	s_value.sv_addr
5624100Seric # define s_mailer	s_value.sv_mailer
5634100Seric # define s_alias	s_value.sv_alias
56453741Seric # define s_mci		s_value.sv_mci
56553751Seric # define s_mapclass	s_value.sv_mapclass
56657443Seric # define s_hostsig	s_value.sv_hostsig
56753751Seric # define s_map		s_value.sv_map
56859652Seric # define s_namecanon	s_value.sv_namecanon
5694100Seric 
57060494Seric extern STAB		*stab __P((char *, int, int));
57160523Seric extern void		stabapply __P((void (*)(STAB *, int), int));
5724056Seric 
5734056Seric /* opcodes to stab */
5744056Seric # define ST_FIND	0	/* find entry */
5754056Seric # define ST_ENTER	1	/* enter if not there */
5766902Seric /*
5777684Seric **  STRUCT EVENT -- event queue.
5787684Seric **
5797684Seric **	Maintained in sorted order.
5807931Seric **
5817931Seric **	We store the pid of the process that set this event to insure
5827931Seric **	that when we fork we will not take events intended for the parent.
5837684Seric */
5847684Seric 
5857684Seric struct event
5867684Seric {
5877684Seric 	time_t		ev_time;	/* time of the function call */
58859153Seric 	int		(*ev_func)__P((int));
58959153Seric 					/* function to call */
5907684Seric 	int		ev_arg;		/* argument to ev_func */
5917931Seric 	int		ev_pid;		/* pid that set this event */
5927684Seric 	struct event	*ev_link;	/* link to next item */
5937684Seric };
5947684Seric 
5957684Seric typedef struct event	EVENT;
5967684Seric 
5977684Seric EXTERN EVENT	*EventQueue;		/* head of event queue */
5987684Seric /*
5999373Seric **  Operation, send, and error modes
6009278Seric **
6019278Seric **	The operation mode describes the basic operation of sendmail.
6029278Seric **	This can be set from the command line, and is "send mail" by
6039278Seric **	default.
6049278Seric **
6059278Seric **	The send mode tells how to send mail.  It can be set in the
6069278Seric **	configuration file.  It's setting determines how quickly the
6079278Seric **	mail will be delivered versus the load on your system.  If the
6089278Seric **	-v (verbose) flag is given, it will be forced to SM_DELIVER
6099278Seric **	mode.
6109278Seric **
6119373Seric **	The error mode tells how to return errors.
6126997Seric */
6136997Seric 
6149278Seric EXTERN char	OpMode;		/* operation mode, see below */
6156997Seric 
6169278Seric #define MD_DELIVER	'm'		/* be a mail sender */
6179278Seric #define MD_SMTP		's'		/* run SMTP on standard input */
6186997Seric #define MD_DAEMON	'd'		/* run as a daemon */
6196997Seric #define MD_VERIFY	'v'		/* verify: don't collect or deliver */
6208334Seric #define MD_TEST		't'		/* test mode: resolve addrs only */
6219144Seric #define MD_INITALIAS	'i'		/* initialize alias database */
6229144Seric #define MD_PRINT	'p'		/* print the queue */
6239144Seric #define MD_FREEZE	'z'		/* freeze the configuration file */
6246997Seric 
6259278Seric 
62658734Seric /* values for e_sendmode -- send modes */
6279278Seric #define SM_DELIVER	'i'		/* interactive delivery */
6289278Seric #define SM_QUICKD	'j'		/* deliver w/o queueing */
6299278Seric #define SM_FORK		'b'		/* deliver in background */
6309278Seric #define SM_QUEUE	'q'		/* queue, don't deliver */
6319278Seric #define SM_VERIFY	'v'		/* verify only (used internally) */
6329373Seric 
63314871Seric /* used only as a parameter to sendall */
63414871Seric #define SM_DEFAULT	'\0'		/* unspecified, use SendMode */
6359373Seric 
63614871Seric 
63758734Seric /* values for e_errormode -- error handling modes */
6389373Seric #define EM_PRINT	'p'		/* print errors */
6399373Seric #define EM_MAIL		'm'		/* mail back errors */
6409373Seric #define EM_WRITE	'w'		/* write back errors */
6419373Seric #define EM_BERKNET	'e'		/* special berknet processing */
6429373Seric #define EM_QUIET	'q'		/* don't print messages (stat only) */
64359162Seric /*
64459162Seric **  Additional definitions
64559162Seric */
64625525Smiriam 
64759162Seric 
64858082Seric /* Offset used to ensure that name server error * codes are unique */
64925525Smiriam #define	MAX_ERRNO	100
65058082Seric 
65159162Seric 
65259162Seric /*
65359162Seric **  Privacy flags
65459162Seric **	These are bit values for the PrivacyFlags word.
65559162Seric */
65659162Seric 
65758082Seric #define PRIV_PUBLIC		0	/* what have I got to hide? */
65858082Seric #define PRIV_NEEDMAILHELO	00001	/* insist on HELO for MAIL, at least */
65958082Seric #define PRIV_NEEDEXPNHELO	00002	/* insist on HELO for EXPN */
66058082Seric #define PRIV_NEEDVRFYHELO	00004	/* insist on HELO for VRFY */
66158082Seric #define PRIV_NOEXPN		00010	/* disallow EXPN command entirely */
66258082Seric #define PRIV_NOVRFY		00020	/* disallow VRFY command entirely */
66358789Seric #define PRIV_AUTHWARNINGS	00040	/* flag possible authorization probs */
66458249Seric #define PRIV_RESTRMAILQ		01000	/* restrict mailq command */
66558249Seric #define PRIV_GOAWAY		00777	/* don't give no info, anyway, anyhow */
66658082Seric 
66758082Seric /* struct defining such things */
66858082Seric struct prival
66958082Seric {
67058082Seric 	char	*pv_name;	/* name of privacy flag */
67158082Seric 	int	pv_flag;	/* numeric level */
67258082Seric };
67358755Seric 
67459162Seric 
67558755Seric /*
67659162Seric **  Flags passed to remotename
67759162Seric */
67859162Seric 
67959162Seric #define RF_SENDERADDR		0001	/* this is a sender address */
68059162Seric #define RF_HEADERADDR		0002	/* this is a header address */
68159162Seric #define RF_CANONICAL		0004	/* strip comment information */
68259162Seric #define RF_ADDDOMAIN		0010	/* OK to do domain extension */
68359162Seric 
68459162Seric 
68559162Seric /*
68658755Seric **  Regular UNIX sockaddrs are too small to handle ISO addresses, so
68758755Seric **  we are forced to declare a supertype here.
68858755Seric */
68958755Seric 
69058778Seric union bigsockaddr
69158755Seric {
69258778Seric 	struct sockaddr		sa;	/* general version */
69358778Seric #ifdef NETINET
69458778Seric 	struct sockaddr_in	sin;	/* INET family */
69558778Seric #endif
69658778Seric #ifdef NETISO
69758778Seric 	struct sockaddr_iso	siso;	/* ISO family */
69858778Seric #endif
69958871Seric #ifdef NETNS
70058871Seric 	struct sockaddr_ns	sns;	/* XNS family */
70158871Seric #endif
70258871Seric #ifdef NETX25
70358871Seric 	struct sockaddr_x25	sx25;	/* X.25 family */
70458871Seric #endif
70558755Seric };
70658755Seric 
70758778Seric #define SOCKADDR	union bigsockaddr
70858755Seric 
7096997Seric /*
710295Seric **  Global variables.
711295Seric */
712295Seric 
7134371Seric EXTERN bool	FromFlag;	/* if set, "From" person is explicit */
7144371Seric EXTERN bool	MeToo;		/* send to the sender also */
7154371Seric EXTERN bool	IgnrDot;	/* don't let dot end messages */
7164371Seric EXTERN bool	SaveFrom;	/* save leading "From" lines */
7174371Seric EXTERN bool	Verbose;	/* set if blow-by-blow desired */
7184371Seric EXTERN bool	GrabTo;		/* if set, get recipients from msg */
7194371Seric EXTERN bool	NoReturn;	/* don't return letter to sender */
7204553Seric EXTERN bool	SuprErrs;	/* set if we are suppressing errors */
7214711Seric EXTERN bool	HoldErrs;	/* only output errors to transcript */
7225904Seric EXTERN bool	NoConnect;	/* don't connect to non-local mailers */
7238268Seric EXTERN bool	SuperSafe;	/* be extra careful, even if expensive */
72424943Seric EXTERN bool	ForkQueueRuns;	/* fork for each job when running the queue */
7259144Seric EXTERN bool	AutoRebuild;	/* auto-rebuild the alias database as needed */
72625818Seric EXTERN bool	CheckAliases;	/* parse addresses during newaliases */
72759668Seric EXTERN bool	NoAlias;	/* suppress aliasing */
72835651Seric EXTERN bool	UseNameServer;	/* use internet domain name server */
72959708Seric EXTERN bool	SevenBit;	/* force 7-bit data */
73017465Seric EXTERN int	SafeAlias;	/* minutes to wait until @:@ in alias file */
7314553Seric EXTERN FILE	*InChannel;	/* input connection */
7324553Seric EXTERN FILE	*OutChannel;	/* output connection */
73355370Seric EXTERN uid_t	RealUid;	/* when Daemon, real uid of caller */
73455370Seric EXTERN gid_t	RealGid;	/* when Daemon, real gid of caller */
73555370Seric EXTERN uid_t	DefUid;		/* default uid to run as */
73655370Seric EXTERN gid_t	DefGid;		/* default gid to run as */
73740973Sbostic EXTERN char	*DefUser;	/* default user to run as (from DefUid) */
7384371Seric EXTERN int	OldUmask;	/* umask when sendmail starts up */
7396049Seric EXTERN int	Errors;		/* set if errors (local to single pass) */
7404371Seric EXTERN int	ExitStat;	/* exit status code */
7414553Seric EXTERN int	AliasLevel;	/* depth of aliasing */
7428057Seric EXTERN int	LineNumber;	/* line number in current input */
7438268Seric EXTERN int	LogLevel;	/* level of logging to perform */
7449045Seric EXTERN int	FileMode;	/* mode on files */
74524943Seric EXTERN int	QueueLA;	/* load average starting forced queueing */
74624943Seric EXTERN int	RefuseLA;	/* load average refusing connections are */
74751920Seric EXTERN int	CurrentLA;	/* current load average */
74857434Seric EXTERN long	QueueFactor;	/* slope of queue function */
7494624Seric EXTERN time_t	QueueIntvl;	/* intervals between running the queue */
7508268Seric EXTERN char	*HelpFile;	/* location of SMTP help file */
75155370Seric EXTERN char	*ErrMsgFile;	/* file to prepend to all error messages */
7528268Seric EXTERN char	*StatFile;	/* location of statistics summary */
7538268Seric EXTERN char	*QueueDir;	/* location of queue directory */
7549373Seric EXTERN char	*FileName;	/* name to print on error messages */
75524943Seric EXTERN char	*SmtpPhase;	/* current phase in SMTP processing */
75625050Seric EXTERN char	*MyHostName;	/* name of this host for SMTP messages */
75724943Seric EXTERN char	*RealHostName;	/* name of host we are talking to */
75858755Seric EXTERN SOCKADDR RealHostAddr;	/* address of host we are talking to */
75925050Seric EXTERN char	*CurHostName;	/* current host we are dealing with */
7608268Seric EXTERN jmp_buf	TopFrame;	/* branch-to-top-of-loop-on-error frame */
7618268Seric EXTERN bool	QuickAbort;	/*  .... but only if we want a quick abort */
76251951Seric EXTERN bool	LogUsrErrs;	/* syslog user errors (e.g., SMTP RCPT cmd) */
76359715Seric EXTERN bool	SendMIMEErrors;	/* send error messages in MIME format */
76459715Seric EXTERN bool	MatchGecos;	/* look for user names in gecos field */
76561086Seric EXTERN bool	UseErrorsTo;	/* use Errors-To: header (back compat) */
76659715Seric EXTERN char	SpaceSub;	/* substitution for <lwsp> */
76758082Seric EXTERN int	PrivacyFlags;	/* privacy flags */
7687859Seric extern char	*ConfFile;	/* location of configuration file [conf.c] */
7699065Seric extern char	*FreezeFile;	/* location of frozen memory image [conf.c] */
77058082Seric extern char	*PidFile;	/* location of proc id file [conf.c] */
77124943Seric extern ADDRESS	NullAddress;	/* a null (template) address [main.c] */
77257434Seric EXTERN long	WkClassFact;	/* multiplier for message class -> priority */
77357434Seric EXTERN long	WkRecipFact;	/* multiplier for # of recipients -> priority */
77457434Seric EXTERN long	WkTimeFact;	/* priority offset each time this job is run */
77559715Seric EXTERN char	*UdbSpec;	/* user database source spec */
77659715Seric EXTERN int	MaxHopCount;	/* max # of hops until bounce */
77759715Seric EXTERN int	ConfigLevel;	/* config file level */
77859715Seric EXTERN char	*TimeZoneSpec;	/* override time zone specification */
77959715Seric EXTERN char	*ForwardPath;	/* path to search for .forward files */
78059715Seric EXTERN long	MinBlocksFree;	/* min # of blocks free on queue fs */
78159715Seric EXTERN char	*FallBackMX;	/* fall back MX host */
78259715Seric EXTERN long	MaxMessageSize;	/* advertised max size we will accept */
78324943Seric EXTERN char	*PostMasterCopy;	/* address to get errs cc's */
78447285Seric EXTERN int	CheckpointInterval;	/* queue file checkpoint interval */
78558144Seric EXTERN bool	DontPruneRoutes;	/* don't prune source routes */
78659262Seric EXTERN int	MaxMciCache;		/* maximum entries in MCI cache */
78754967Seric EXTERN time_t	MciCacheTimeout;	/* maximum idle time on connections */
78858318Seric EXTERN char	*QueueLimitRecipient;	/* limit queue runs to this recipient */
78958318Seric EXTERN char	*QueueLimitSender;	/* limit queue runs to this sender */
79058318Seric EXTERN char	*QueueLimitId;		/* limit queue runs to this id */
79158112Seric 
79258112Seric 
79358112Seric /*
79458112Seric **  Timeouts
79558112Seric **
79658112Seric **	Indicated values are the MINIMUM per RFC 1123 section 5.3.2.
79758112Seric */
79858112Seric 
79958112Seric EXTERN struct
80058112Seric {
80158112Seric 	time_t	to_initial;	/* initial greeting timeout [5m] */
80258112Seric 	time_t	to_mail;	/* MAIL command [5m] */
80358112Seric 	time_t	to_rcpt;	/* RCPT command [5m] */
80458112Seric 	time_t	to_datainit;	/* DATA initiation [2m] */
80558112Seric 	time_t	to_datablock;	/* DATA block [3m] */
80658112Seric 	time_t	to_datafinal;	/* DATA completion [10m] */
80758112Seric 	time_t	to_nextcommand;	/* next command [5m] */
80858112Seric 			/* following timeouts are not mentioned in RFC 1123 */
80958112Seric 	time_t	to_rset;	/* RSET command */
81058112Seric 	time_t	to_helo;	/* HELO command */
81158112Seric 	time_t	to_quit;	/* QUIT command */
81258112Seric 	time_t	to_miscshort;	/* misc short commands (NOOP, VERB, etc) */
81358737Seric 			/* following are per message */
81458737Seric 	time_t	to_q_return;	/* queue return timeout */
81558737Seric 	time_t	to_q_warning;	/* queue warning timeout */
81658112Seric } TimeOuts;
81758112Seric 
81858112Seric 
81958112Seric /*
8207674Seric **  Trace information
8217674Seric */
822295Seric 
8237674Seric /* trace vector and macros for debugging flags */
8247674Seric EXTERN u_char	tTdvect[100];
8257674Seric # define tTd(flag, level)	(tTdvect[flag] >= level)
8267674Seric # define tTdlevel(flag)		(tTdvect[flag])
8277674Seric /*
8287674Seric **  Miscellaneous information.
8297674Seric */
830295Seric 
831295Seric 
83210213Seric 
83310213Seric /*
83410213Seric **  Some in-line functions
83510213Seric */
83610213Seric 
83710213Seric /* set exit status */
83811426Seric #define setstat(s)	{ \
83911426Seric 				if (ExitStat == EX_OK || ExitStat == EX_TEMPFAIL) \
84011426Seric 					ExitStat = s; \
84111426Seric 			}
8424085Seric 
84310213Seric /* make a copy of a string */
84411426Seric #define newstr(s)	strcpy(xalloc(strlen(s) + 1), s)
8454085Seric 
84624943Seric #define STRUCTCOPY(s, d)	d = s
84710213Seric 
84824943Seric 
84910213Seric /*
85010213Seric **  Declarations of useful functions
85110213Seric */
85210213Seric 
85360089Seric extern ADDRESS		*parseaddr __P((char *, ADDRESS *, int, int, char **, ENVELOPE *));
85460089Seric extern char		*xalloc __P((int));
85560089Seric extern bool		sameaddr __P((ADDRESS *, ADDRESS *));
85660089Seric extern FILE		*dfopen __P((char *, int, int));
85760089Seric extern EVENT		*setevent __P((time_t, int(*)(), int));
858*61088Seric extern char		*sfgets __P((char *, int, FILE *, time_t, char *));
85960089Seric extern char		*queuename __P((ENVELOPE *, int));
86060089Seric extern time_t		curtime __P(());
86160089Seric extern bool		transienterror __P((int));
86260089Seric extern const char	*errstring __P((int));
86360494Seric extern void		expand __P((char *, char *, char *, ENVELOPE *));
86460574Seric extern void		define __P((int, char *, ENVELOPE *));
86560574Seric extern char		*macvalue __P((int, ENVELOPE *));
86660574Seric extern char		**prescan __P((char *, int, char[], char **));
86760494Seric extern char		*fgetfolded __P((char *, int, FILE *));
86860494Seric extern ADDRESS		*recipient __P((ADDRESS *, ADDRESS **, ENVELOPE *));
86960494Seric extern ENVELOPE		*newenvelope __P((ENVELOPE *, ENVELOPE *));
87060494Seric extern void		dropenvelope __P((ENVELOPE *));
87160574Seric extern void		clearenvelope __P((ENVELOPE *, int));
87260494Seric extern char		*username __P(());
87360494Seric extern MCI		*mci_get __P((char *, MAILER *));
87460574Seric extern char		*pintvl __P((time_t, int));
87560494Seric extern char		*map_rewrite __P((MAP *, char *, int, char **));
87660494Seric extern ADDRESS		*getctladdr __P((ADDRESS *));
87760494Seric extern char		*anynet_ntoa __P((SOCKADDR *));
87860494Seric extern char		*remotename __P((char *, MAILER *, int, int *, ENVELOPE *));
87960494Seric extern bool		shouldqueue __P((long, time_t));
88060494Seric extern bool		lockfile __P((int, char *, int));
88160494Seric extern char		*hostsignature __P((MAILER *, char *, ENVELOPE *));
88260494Seric extern void		openxscript __P((ENVELOPE *));
88360494Seric extern void		closexscript __P((ENVELOPE *));
88458789Seric 
88558823Seric /* ellipsis is a different case though */
88658823Seric #ifdef __STDC__
88760093Seric extern void		auth_warning(ENVELOPE *, const char *, ...);
88860093Seric extern void		syserr(const char *, ...);
88960093Seric extern void		usrerr(const char *, ...);
89060093Seric extern void		message(const char *, ...);
89160093Seric extern void		nmessage(const char *, ...);
89258823Seric #else
89360089Seric extern void		auth_warning();
89460089Seric extern void		syserr();
89560089Seric extern void		usrerr();
89660089Seric extern void		message();
89760089Seric extern void		nmessage();
89858823Seric #endif
899