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