1 /*
2 **  SENDMAIL.H -- Global definitions for sendmail.
3 */
4 
5 
6 
7 # ifdef _DEFINE
8 # define EXTERN
9 # ifndef lint
10 static char SmailSccsId[] =	"@(#)sendmail.h	4.11		12/05/84";
11 # endif lint
12 # else  _DEFINE
13 # define EXTERN extern
14 # endif _DEFINE
15 
16 # include <stdio.h>
17 # include <ctype.h>
18 # include <setjmp.h>
19 # include "conf.h"
20 # include "useful.h"
21 
22 # ifdef LOG
23 # include <syslog.h>
24 # endif LOG
25 
26 
27 # define PSBUFSIZE	(MAXNAME + MAXATOM)	/* size of prescan buffer */
28 
29 
30 /*
31 **  Data structure for bit maps.
32 **
33 **	Each bit in this map can be referenced by an ascii character.
34 **	This is 128 possible bits, or 12 8-bit bytes.
35 */
36 
37 #define BITMAPBYTES	16	/* number of bytes in a bit map */
38 #define BYTEBITS	8	/* number of bits in a byte */
39 
40 /* internal macros */
41 #define _BITWORD(bit)	(bit / (BYTEBITS * sizeof (int)))
42 #define _BITBIT(bit)	(1 << (bit % (BYTEBITS * sizeof (int))))
43 
44 typedef int	BITMAP[BITMAPBYTES / sizeof (int)];
45 
46 /* test bit number N */
47 #define bitnset(bit, map)	((map)[_BITWORD(bit)] & _BITBIT(bit))
48 
49 /* set bit number N */
50 #define setbitn(bit, map)	(map)[_BITWORD(bit)] |= _BITBIT(bit)
51 
52 /* clear bit number N */
53 #define clrbitn(bit, map)	(map)[_BITWORD(bit)] &= ~_BITBIT(bit)
54 
55 /* clear an entire bit map */
56 #define clrbitmap(map)		bzero((char *) map, BITMAPBYTES)
57 /*
58 **  Address structure.
59 **	Addresses are stored internally in this structure.
60 */
61 
62 struct address
63 {
64 	char		*q_paddr;	/* the printname for the address */
65 	char		*q_user;	/* user name */
66 	char		*q_host;	/* host name */
67 	struct mailer	*q_mailer;	/* mailer to use */
68 	u_short		q_flags;	/* status flags, see below */
69 	short		q_uid;		/* user-id of receiver (if known) */
70 	short		q_gid;		/* group-id of receiver (if known) */
71 	char		*q_home;	/* home dir (local mailer only) */
72 	char		*q_fullname;	/* full name if known */
73 	struct address	*q_next;	/* chain */
74 	struct address	*q_alias;	/* address this results from */
75 	struct address	*q_tchain;	/* temporary use chain */
76 	time_t		q_timeout;	/* timeout for this address */
77 };
78 
79 typedef struct address ADDRESS;
80 
81 # define QDONTSEND	000001	/* don't send to this address */
82 # define QBADADDR	000002	/* this address is verified bad */
83 # define QGOODUID	000004	/* the q_uid q_gid fields are good */
84 # define QPRIMARY	000010	/* set from argv */
85 # define QQUEUEUP	000020	/* queue for later transmission */
86 /*
87 **  Mailer definition structure.
88 **	Every mailer known to the system is declared in this
89 **	structure.  It defines the pathname of the mailer, some
90 **	flags associated with it, and the argument vector to
91 **	pass to it.  The flags are defined in conf.c
92 **
93 **	The argument vector is expanded before actual use.  All
94 **	words except the first are passed through the macro
95 **	processor.
96 */
97 
98 struct mailer
99 {
100 	char	*m_name;	/* symbolic name of this mailer */
101 	char	*m_mailer;	/* pathname of the mailer to use */
102 	BITMAP	m_flags;	/* status flags, see below */
103 	short	m_mno;		/* mailer number internally */
104 	char	**m_argv;	/* template argument vector */
105 	short	m_s_rwset;	/* rewriting set for sender addresses */
106 	short	m_r_rwset;	/* rewriting set for recipient addresses */
107 	char	*m_eol;		/* end of line string */
108 	long	m_maxsize;	/* size limit on message to this mailer */
109 };
110 
111 typedef struct mailer	MAILER;
112 
113 /* bits for m_flags */
114 # define M_CANONICAL	'C'	/* make addresses canonical "u@dom" */
115 # define M_EXPENSIVE	'e'	/* it costs to use this mailer.... */
116 # define M_ESCFROM	'E'	/* escape From lines to >From */
117 # define M_FOPT		'f'	/* mailer takes picky -f flag */
118 # define M_HST_UPPER	'h'	/* preserve host case distinction */
119 # define M_INTERNAL	'I'	/* SMTP to another sendmail site */
120 # define M_LOCAL	'l'	/* delivery is to this host */
121 # define M_LIMITS	'L'	/* must enforce SMTP line limits */
122 # define M_MUSER	'm'	/* can handle multiple users at once */
123 # define M_NHDR		'n'	/* don't insert From line */
124 # define M_FROMPATH	'p'	/* use reverse-path in MAIL FROM: */
125 # define M_ROPT		'r'	/* mailer takes picky -r flag */
126 # define M_SECURE_PORT	'R'	/* try to send on a reserved TCP port */
127 # define M_STRIPQ	's'	/* strip quote chars from user/host */
128 # define M_RESTR	'S'	/* must be daemon to execute */
129 # define M_USR_UPPER	'u'	/* preserve user case distinction */
130 # define M_UGLYUUCP	'U'	/* this wants an ugly UUCP from line */
131 # define M_XDOT		'X'	/* use hidden-dot algorithm */
132 
133 EXTERN MAILER	*Mailer[MAXMAILERS+1];
134 
135 EXTERN MAILER	*LocalMailer;		/* ptr to local mailer */
136 EXTERN MAILER	*ProgMailer;		/* ptr to program mailer */
137 /*
138 **  Header structure.
139 **	This structure is used internally to store header items.
140 */
141 
142 struct header
143 {
144 	char		*h_field;	/* the name of the field */
145 	char		*h_value;	/* the value of that field */
146 	struct header	*h_link;	/* the next header */
147 	u_short		h_flags;	/* status bits, see below */
148 	BITMAP		h_mflags;	/* m_flags bits needed */
149 };
150 
151 typedef struct header	HDR;
152 
153 /*
154 **  Header information structure.
155 **	Defined in conf.c, this struct declares the header fields
156 **	that have some magic meaning.
157 */
158 
159 struct hdrinfo
160 {
161 	char	*hi_field;	/* the name of the field */
162 	u_short	hi_flags;	/* status bits, see below */
163 };
164 
165 extern struct hdrinfo	HdrInfo[];
166 
167 /* bits for h_flags and hi_flags */
168 # define H_EOH		00001	/* this field terminates header */
169 # define H_RCPT		00002	/* contains recipient addresses */
170 # define H_DEFAULT	00004	/* if another value is found, drop this */
171 # define H_RESENT	00010	/* this address is a "Resent-..." address */
172 # define H_CHECK	00020	/* check h_mflags against m_flags */
173 # define H_ACHECK	00040	/* ditto, but always (not just default) */
174 # define H_FORCE	00100	/* force this field, even if default */
175 # define H_TRACE	00200	/* this field contains trace information */
176 # define H_FROM		00400	/* this is a from-type field */
177 /*
178 **  Envelope structure.
179 **	This structure defines the message itself.  There is usually
180 **	only one of these -- for the message that we originally read
181 **	and which is our primary interest -- but other envelopes can
182 **	be generated during processing.  For example, error messages
183 **	will have their own envelope.
184 */
185 
186 struct envelope
187 {
188 	HDR		*e_header;	/* head of header list */
189 	long		e_msgpriority;	/* adjusted priority of this message */
190 	time_t		e_ctime;	/* time message appeared in the queue */
191 	char		*e_to;		/* the target person */
192 	char		*e_receiptto;	/* return receipt address */
193 	ADDRESS		e_from;		/* the person it is from */
194 	char		**e_fromdomain;	/* the domain part of the sender */
195 	ADDRESS		*e_sendqueue;	/* list of message recipients */
196 	ADDRESS		*e_errorqueue;	/* the queue for error responses */
197 	long		e_msgsize;	/* size of the message in bytes */
198 	short		e_class;	/* msg class (priority, junk, etc.) */
199 	short		e_flags;	/* flags, see below */
200 	short		e_hopcount;	/* number of times processed */
201 	int		(*e_puthdr)();	/* function to put header of message */
202 	int		(*e_putbody)();	/* function to put body of message */
203 	struct envelope	*e_parent;	/* the message this one encloses */
204 	struct envelope *e_sibling;	/* the next envelope of interest */
205 	char		*e_df;		/* location of temp file */
206 	FILE		*e_dfp;		/* temporary file */
207 	char		*e_id;		/* code for this entry in queue */
208 	FILE		*e_xfp;		/* transcript file */
209 	FILE		*e_qfp;		/* queue control file */
210 	char		*e_message;	/* error message */
211 	char		*e_macro[128];	/* macro definitions */
212 };
213 
214 typedef struct envelope	ENVELOPE;
215 
216 /* values for e_flags */
217 #define EF_OLDSTYLE	000001		/* use spaces (not commas) in hdrs */
218 #define EF_INQUEUE	000002		/* this message is fully queued */
219 #define EF_TIMEOUT	000004		/* this message is too old */
220 #define EF_CLRQUEUE	000010		/* disk copy is no longer needed */
221 #define EF_SENDRECEIPT	000020		/* send a return receipt */
222 #define EF_FATALERRS	000040		/* fatal errors occured */
223 #define EF_KEEPQUEUE	000100		/* keep queue files always */
224 #define EF_RESPONSE	000200		/* this is an error or return receipt */
225 #define EF_RESENT	000400		/* this message is being forwarded */
226 
227 EXTERN ENVELOPE	*CurEnv;	/* envelope currently being processed */
228 /*
229 **  Message priorities.
230 **	Priorities > 0 should be preemptive.
231 **
232 **	CurEnv->e_msgpriority is the number of bytes in the message adjusted
233 **	by the message priority and the amount of time the message
234 **	has been sitting around.  Each priority point is worth
235 **	WKPRIFACT bytes of message, and each time we reprocess a
236 **	message the size gets reduced by WKTIMEFACT.
237 **
238 **	WKTIMEFACT is negative since jobs that fail once have a high
239 **	probability of failing again.  Making it negative tends to force
240 **	them to the back rather than the front of the queue, where they
241 **	only clog things.  Thanks go to Jay Lepreau at Utah for pointing
242 **	out the error in my thinking.
243 **
244 **	The "class" is this number, unadjusted by the age or size of
245 **	this message.  Classes with negative representations will have
246 **	error messages thrown away if they are not local.
247 */
248 
249 struct priority
250 {
251 	char	*pri_name;	/* external name of priority */
252 	int	pri_val;	/* internal value for same */
253 };
254 
255 EXTERN struct priority	Priorities[MAXPRIORITIES];
256 EXTERN int		NumPriorities;	/* pointer into Priorities */
257 
258 # define WKPRIFACT	1800		/* bytes each pri point is worth */
259 # define WKTIMEFACT	(-600)		/* bytes each reprocessing is worth */
260 /*
261 **  Rewrite rules.
262 */
263 
264 struct rewrite
265 {
266 	char	**r_lhs;	/* pattern match */
267 	char	**r_rhs;	/* substitution value */
268 	struct rewrite	*r_next;/* next in chain */
269 };
270 
271 EXTERN struct rewrite	*RewriteRules[MAXRWSETS];
272 
273 /*
274 **  Special characters in rewriting rules.
275 **	These are used internally only.
276 **	The COND* rules are actually used in macros rather than in
277 **		rewriting rules, but are given here because they
278 **		cannot conflict.
279 */
280 
281 /* left hand side items */
282 # define MATCHZANY	'\020'	/* match zero or more tokens */
283 # define MATCHANY	'\021'	/* match one or more tokens */
284 # define MATCHONE	'\022'	/* match exactly one token */
285 # define MATCHCLASS	'\023'	/* match one token in a class */
286 # define MATCHNCLASS	'\024'	/* match anything not in class */
287 # define MATCHREPL	'\025'	/* replacement on RHS for above */
288 
289 /* right hand side items */
290 # define CANONNET	'\026'	/* canonical net, next token */
291 # define CANONHOST	'\027'	/* canonical host, next token */
292 # define CANONUSER	'\030'	/* canonical user, next N tokens */
293 # define CALLSUBR	'\031'	/* call another rewriting set */
294 
295 /* conditionals in macros */
296 # define CONDIF		'\032'	/* conditional if-then */
297 # define CONDELSE	'\033'	/* conditional else */
298 # define CONDFI		'\034'	/* conditional fi */
299 
300 /* bracket characters for host name lookup */
301 # define HOSTBEGIN	'\035'	/* hostname lookup begin */
302 # define HOSTEND	'\036'	/* hostname lookup end */
303 
304 /* \001 is also reserved as the macro expansion character */
305 /*
306 **  Symbol table definitions
307 */
308 
309 struct symtab
310 {
311 	char		*s_name;	/* name to be entered */
312 	char		s_type;		/* general type (see below) */
313 	struct symtab	*s_next;	/* pointer to next in chain */
314 	union
315 	{
316 		BITMAP	sv_class;	/* bit-map of word classes */
317 		ADDRESS	*sv_addr;	/* pointer to address header */
318 		MAILER	*sv_mailer;	/* pointer to mailer */
319 		char	*sv_alias;	/* alias */
320 	}	s_value;
321 };
322 
323 typedef struct symtab	STAB;
324 
325 /* symbol types */
326 # define ST_UNDEF	0	/* undefined type */
327 # define ST_CLASS	1	/* class map */
328 # define ST_ADDRESS	2	/* an address in parsed format */
329 # define ST_MAILER	3	/* a mailer header */
330 # define ST_ALIAS	4	/* an alias */
331 
332 # define s_class	s_value.sv_class
333 # define s_address	s_value.sv_addr
334 # define s_mailer	s_value.sv_mailer
335 # define s_alias	s_value.sv_alias
336 
337 extern STAB	*stab();
338 
339 /* opcodes to stab */
340 # define ST_FIND	0	/* find entry */
341 # define ST_ENTER	1	/* enter if not there */
342 /*
343 **  STRUCT EVENT -- event queue.
344 **
345 **	Maintained in sorted order.
346 **
347 **	We store the pid of the process that set this event to insure
348 **	that when we fork we will not take events intended for the parent.
349 */
350 
351 struct event
352 {
353 	time_t		ev_time;	/* time of the function call */
354 	int		(*ev_func)();	/* function to call */
355 	int		ev_arg;		/* argument to ev_func */
356 	int		ev_pid;		/* pid that set this event */
357 	struct event	*ev_link;	/* link to next item */
358 };
359 
360 typedef struct event	EVENT;
361 
362 EXTERN EVENT	*EventQueue;		/* head of event queue */
363 /*
364 **  Operation, send, and error modes
365 **
366 **	The operation mode describes the basic operation of sendmail.
367 **	This can be set from the command line, and is "send mail" by
368 **	default.
369 **
370 **	The send mode tells how to send mail.  It can be set in the
371 **	configuration file.  It's setting determines how quickly the
372 **	mail will be delivered versus the load on your system.  If the
373 **	-v (verbose) flag is given, it will be forced to SM_DELIVER
374 **	mode.
375 **
376 **	The error mode tells how to return errors.
377 */
378 
379 EXTERN char	OpMode;		/* operation mode, see below */
380 
381 #define MD_DELIVER	'm'		/* be a mail sender */
382 #define MD_ARPAFTP	'a'		/* old-style arpanet protocols */
383 #define MD_SMTP		's'		/* run SMTP on standard input */
384 #define MD_DAEMON	'd'		/* run as a daemon */
385 #define MD_VERIFY	'v'		/* verify: don't collect or deliver */
386 #define MD_TEST		't'		/* test mode: resolve addrs only */
387 #define MD_INITALIAS	'i'		/* initialize alias database */
388 #define MD_PRINT	'p'		/* print the queue */
389 #define MD_FREEZE	'z'		/* freeze the configuration file */
390 
391 
392 EXTERN char	SendMode;	/* send mode, see below */
393 
394 #define SM_DELIVER	'i'		/* interactive delivery */
395 #define SM_QUICKD	'j'		/* deliver w/o queueing */
396 #define SM_FORK		'b'		/* deliver in background */
397 #define SM_QUEUE	'q'		/* queue, don't deliver */
398 #define SM_VERIFY	'v'		/* verify only (used internally) */
399 
400 /* used only as a parameter to sendall */
401 #define SM_DEFAULT	'\0'		/* unspecified, use SendMode */
402 
403 
404 EXTERN char	ErrorMode;	/* error mode, see below */
405 
406 #define EM_PRINT	'p'		/* print errors */
407 #define EM_MAIL		'm'		/* mail back errors */
408 #define EM_WRITE	'w'		/* write back errors */
409 #define EM_BERKNET	'e'		/* special berknet processing */
410 #define EM_QUIET	'q'		/* don't print messages (stat only) */
411 /*
412 **  Global variables.
413 */
414 
415 EXTERN bool	FromFlag;	/* if set, "From" person is explicit */
416 EXTERN bool	NoAlias;	/* if set, don't do any aliasing */
417 EXTERN bool	ForceMail;	/* if set, mail even if already got a copy */
418 EXTERN bool	MeToo;		/* send to the sender also */
419 EXTERN bool	IgnrDot;	/* don't let dot end messages */
420 EXTERN bool	SaveFrom;	/* save leading "From" lines */
421 EXTERN bool	Verbose;	/* set if blow-by-blow desired */
422 EXTERN bool	GrabTo;		/* if set, get recipients from msg */
423 EXTERN bool	NoReturn;	/* don't return letter to sender */
424 EXTERN bool	SuprErrs;	/* set if we are suppressing errors */
425 EXTERN bool	QueueRun;	/* currently running message from the queue */
426 EXTERN bool	HoldErrs;	/* only output errors to transcript */
427 EXTERN bool	NoConnect;	/* don't connect to non-local mailers */
428 EXTERN bool	SuperSafe;	/* be extra careful, even if expensive */
429 EXTERN bool	AutoRebuild;	/* auto-rebuild the alias database as needed */
430 EXTERN int	SafeAlias;	/* minutes to wait until @:@ in alias file */
431 EXTERN time_t	TimeOut;	/* time until timeout */
432 EXTERN FILE	*InChannel;	/* input connection */
433 EXTERN FILE	*OutChannel;	/* output connection */
434 EXTERN int	RealUid;	/* when Daemon, real uid of caller */
435 EXTERN int	RealGid;	/* when Daemon, real gid of caller */
436 EXTERN int	DefUid;		/* default uid to run as */
437 EXTERN int	DefGid;		/* default gid to run as */
438 EXTERN int	OldUmask;	/* umask when sendmail starts up */
439 EXTERN int	Errors;		/* set if errors (local to single pass) */
440 EXTERN int	ExitStat;	/* exit status code */
441 EXTERN int	AliasLevel;	/* depth of aliasing */
442 EXTERN int	MotherPid;	/* proc id of parent process */
443 EXTERN int	LineNumber;	/* line number in current input */
444 EXTERN time_t	ReadTimeout;	/* timeout on reads */
445 EXTERN int	LogLevel;	/* level of logging to perform */
446 EXTERN int	FileMode;	/* mode on files */
447 EXTERN time_t	QueueIntvl;	/* intervals between running the queue */
448 EXTERN char	*HostName;	/* name of this host for SMTP messages */
449 EXTERN char	*AliasFile;	/* location of alias file */
450 EXTERN char	*HelpFile;	/* location of SMTP help file */
451 EXTERN char	*StatFile;	/* location of statistics summary */
452 EXTERN char	*QueueDir;	/* location of queue directory */
453 EXTERN char	*FileName;	/* name to print on error messages */
454 EXTERN char	*TrustedUsers[MAXTRUST+1];	/* list of trusted users */
455 EXTERN jmp_buf	TopFrame;	/* branch-to-top-of-loop-on-error frame */
456 EXTERN bool	QuickAbort;	/*  .... but only if we want a quick abort */
457 extern char	*ConfFile;	/* location of configuration file [conf.c] */
458 extern char	*FreezeFile;	/* location of frozen memory image [conf.c] */
459 extern char	Arpa_Info[];	/* the reply code for Arpanet info [conf.c] */
460 extern char	SpaceSub;	/* substitution for <lwsp> [conf.c] */
461 /*
462 **  Trace information
463 */
464 
465 /* trace vector and macros for debugging flags */
466 EXTERN u_char	tTdvect[100];
467 # define tTd(flag, level)	(tTdvect[flag] >= level)
468 # define tTdlevel(flag)		(tTdvect[flag])
469 /*
470 **  Miscellaneous information.
471 */
472 
473 # include	<sysexits.h>
474 
475 
476 /*
477 **  Some in-line functions
478 */
479 
480 /* set exit status */
481 #define setstat(s)	{ \
482 				if (ExitStat == EX_OK || ExitStat == EX_TEMPFAIL) \
483 					ExitStat = s; \
484 			}
485 
486 /* make a copy of a string */
487 #define newstr(s)	strcpy(xalloc(strlen(s) + 1), s)
488 
489 
490 /*
491 **  Declarations of useful functions
492 */
493 
494 extern ADDRESS	*parseaddr();
495 extern char	*xalloc();
496 extern bool	sameaddr();
497 extern FILE	*dfopen();
498 extern EVENT	*setevent();
499 extern char	*sfgets();
500 extern char	*queuename();
501 extern time_t	curtime();
502