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