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.1 (Berkeley) 06/07/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.1		06/07/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 /*
190 **  Envelope structure.
191 **	This structure defines the message itself.  There is usually
192 **	only one of these -- for the message that we originally read
193 **	and which is our primary interest -- but other envelopes can
194 **	be generated during processing.  For example, error messages
195 **	will have their own envelope.
196 */
197 
198 struct envelope
199 {
200 	HDR		*e_header;	/* head of header list */
201 	long		e_msgpriority;	/* adjusted priority of this message */
202 	time_t		e_ctime;	/* time message appeared in the queue */
203 	char		*e_to;		/* the target person */
204 	char		*e_receiptto;	/* return receipt address */
205 	ADDRESS		e_from;		/* the person it is from */
206 	char		**e_fromdomain;	/* the domain part of the sender */
207 	ADDRESS		*e_sendqueue;	/* list of message recipients */
208 	ADDRESS		*e_errorqueue;	/* the queue for error responses */
209 	long		e_msgsize;	/* size of the message in bytes */
210 	short		e_class;	/* msg class (priority, junk, etc.) */
211 	short		e_flags;	/* flags, see below */
212 	short		e_hopcount;	/* number of times processed */
213 	int		(*e_puthdr)();	/* function to put header of message */
214 	int		(*e_putbody)();	/* function to put body of message */
215 	struct envelope	*e_parent;	/* the message this one encloses */
216 	struct envelope *e_sibling;	/* the next envelope of interest */
217 	char		*e_df;		/* location of temp file */
218 	FILE		*e_dfp;		/* temporary file */
219 	char		*e_id;		/* code for this entry in queue */
220 	FILE		*e_xfp;		/* transcript file */
221 	char		*e_message;	/* error message */
222 	char		*e_macro[128];	/* macro definitions */
223 };
224 
225 typedef struct envelope	ENVELOPE;
226 
227 /* values for e_flags */
228 #define EF_OLDSTYLE	000001		/* use spaces (not commas) in hdrs */
229 #define EF_INQUEUE	000002		/* this message is fully queued */
230 #define EF_TIMEOUT	000004		/* this message is too old */
231 #define EF_CLRQUEUE	000010		/* disk copy is no longer needed */
232 #define EF_SENDRECEIPT	000020		/* send a return receipt */
233 #define EF_FATALERRS	000040		/* fatal errors occured */
234 #define EF_KEEPQUEUE	000100		/* keep queue files always */
235 #define EF_RESPONSE	000200		/* this is an error or return receipt */
236 #define EF_RESENT	000400		/* this message is being forwarded */
237 
238 EXTERN ENVELOPE	*CurEnv;	/* envelope currently being processed */
239 /*
240 **  Message priorities.
241 **	Priorities > 0 should be preemptive.
242 **
243 **	CurEnv->e_msgpriority is the number of bytes in the message adjusted
244 **	by the message priority and the amount of time the message
245 **	has been sitting around.  Each priority point is worth
246 **	WKPRIFACT bytes of message, and each time we reprocess a
247 **	message the size gets reduced by WKTIMEFACT.
248 **
249 **	WKTIMEFACT is negative since jobs that fail once have a high
250 **	probability of failing again.  Making it negative tends to force
251 **	them to the back rather than the front of the queue, where they
252 **	only clog things.  Thanks go to Jay Lepreau at Utah for pointing
253 **	out the error in my thinking.
254 **
255 **	The "class" is this number, unadjusted by the age or size of
256 **	this message.  Classes with negative representations will have
257 **	error messages thrown away if they are not local.
258 */
259 
260 struct priority
261 {
262 	char	*pri_name;	/* external name of priority */
263 	int	pri_val;	/* internal value for same */
264 };
265 
266 EXTERN struct priority	Priorities[MAXPRIORITIES];
267 EXTERN int		NumPriorities;	/* pointer into Priorities */
268 
269 # define WKPRIFACT	1800		/* bytes each pri point is worth */
270 # define WKTIMEFACT	(-600)		/* bytes each reprocessing is worth */
271 /*
272 **  Rewrite rules.
273 */
274 
275 struct rewrite
276 {
277 	char	**r_lhs;	/* pattern match */
278 	char	**r_rhs;	/* substitution value */
279 	struct rewrite	*r_next;/* next in chain */
280 };
281 
282 EXTERN struct rewrite	*RewriteRules[MAXRWSETS];
283 
284 /*
285 **  Special characters in rewriting rules.
286 **	These are used internally only.
287 **	The COND* rules are actually used in macros rather than in
288 **		rewriting rules, but are given here because they
289 **		cannot conflict.
290 */
291 
292 /* left hand side items */
293 # define MATCHZANY	'\020'	/* match zero or more tokens */
294 # define MATCHANY	'\021'	/* match one or more tokens */
295 # define MATCHONE	'\022'	/* match exactly one token */
296 # define MATCHCLASS	'\023'	/* match one token in a class */
297 # define MATCHNCLASS	'\024'	/* match anything not in class */
298 # define MATCHREPL	'\025'	/* replacement on RHS for above */
299 
300 /* right hand side items */
301 # define CANONNET	'\026'	/* canonical net, next token */
302 # define CANONHOST	'\027'	/* canonical host, next token */
303 # define CANONUSER	'\030'	/* canonical user, next N tokens */
304 # define CALLSUBR	'\031'	/* call another rewriting set */
305 
306 /* conditionals in macros */
307 # define CONDIF		'\032'	/* conditional if-then */
308 # define CONDELSE	'\033'	/* conditional else */
309 # define CONDFI		'\034'	/* conditional fi */
310 
311 /* bracket characters for host name lookup */
312 # define HOSTBEGIN	'\035'	/* hostname lookup begin */
313 # define HOSTEND	'\036'	/* hostname lookup end */
314 
315 /* \001 is also reserved as the macro expansion character */
316 /*
317 **  Symbol table definitions
318 */
319 
320 struct symtab
321 {
322 	char		*s_name;	/* name to be entered */
323 	char		s_type;		/* general type (see below) */
324 	struct symtab	*s_next;	/* pointer to next in chain */
325 	union
326 	{
327 		BITMAP	sv_class;	/* bit-map of word classes */
328 		ADDRESS	*sv_addr;	/* pointer to address header */
329 		MAILER	*sv_mailer;	/* pointer to mailer */
330 		char	*sv_alias;	/* alias */
331 	}	s_value;
332 };
333 
334 typedef struct symtab	STAB;
335 
336 /* symbol types */
337 # define ST_UNDEF	0	/* undefined type */
338 # define ST_CLASS	1	/* class map */
339 # define ST_ADDRESS	2	/* an address in parsed format */
340 # define ST_MAILER	3	/* a mailer header */
341 # define ST_ALIAS	4	/* an alias */
342 
343 # define s_class	s_value.sv_class
344 # define s_address	s_value.sv_addr
345 # define s_mailer	s_value.sv_mailer
346 # define s_alias	s_value.sv_alias
347 
348 extern STAB	*stab();
349 
350 /* opcodes to stab */
351 # define ST_FIND	0	/* find entry */
352 # define ST_ENTER	1	/* enter if not there */
353 /*
354 **  STRUCT EVENT -- event queue.
355 **
356 **	Maintained in sorted order.
357 **
358 **	We store the pid of the process that set this event to insure
359 **	that when we fork we will not take events intended for the parent.
360 */
361 
362 struct event
363 {
364 	time_t		ev_time;	/* time of the function call */
365 	int		(*ev_func)();	/* function to call */
366 	int		ev_arg;		/* argument to ev_func */
367 	int		ev_pid;		/* pid that set this event */
368 	struct event	*ev_link;	/* link to next item */
369 };
370 
371 typedef struct event	EVENT;
372 
373 EXTERN EVENT	*EventQueue;		/* head of event queue */
374 /*
375 **  Operation, send, and error modes
376 **
377 **	The operation mode describes the basic operation of sendmail.
378 **	This can be set from the command line, and is "send mail" by
379 **	default.
380 **
381 **	The send mode tells how to send mail.  It can be set in the
382 **	configuration file.  It's setting determines how quickly the
383 **	mail will be delivered versus the load on your system.  If the
384 **	-v (verbose) flag is given, it will be forced to SM_DELIVER
385 **	mode.
386 **
387 **	The error mode tells how to return errors.
388 */
389 
390 EXTERN char	OpMode;		/* operation mode, see below */
391 
392 #define MD_DELIVER	'm'		/* be a mail sender */
393 #define MD_ARPAFTP	'a'		/* old-style arpanet protocols */
394 #define MD_SMTP		's'		/* run SMTP on standard input */
395 #define MD_DAEMON	'd'		/* run as a daemon */
396 #define MD_VERIFY	'v'		/* verify: don't collect or deliver */
397 #define MD_TEST		't'		/* test mode: resolve addrs only */
398 #define MD_INITALIAS	'i'		/* initialize alias database */
399 #define MD_PRINT	'p'		/* print the queue */
400 #define MD_FREEZE	'z'		/* freeze the configuration file */
401 
402 
403 EXTERN char	SendMode;	/* send mode, see below */
404 
405 #define SM_DELIVER	'i'		/* interactive delivery */
406 #define SM_QUICKD	'j'		/* deliver w/o queueing */
407 #define SM_FORK		'b'		/* deliver in background */
408 #define SM_QUEUE	'q'		/* queue, don't deliver */
409 #define SM_VERIFY	'v'		/* verify only (used internally) */
410 
411 /* used only as a parameter to sendall */
412 #define SM_DEFAULT	'\0'		/* unspecified, use SendMode */
413 
414 
415 EXTERN char	ErrorMode;	/* error mode, see below */
416 
417 #define EM_PRINT	'p'		/* print errors */
418 #define EM_MAIL		'm'		/* mail back errors */
419 #define EM_WRITE	'w'		/* write back errors */
420 #define EM_BERKNET	'e'		/* special berknet processing */
421 #define EM_QUIET	'q'		/* don't print messages (stat only) */
422 /*
423 **  Global variables.
424 */
425 
426 EXTERN bool	FromFlag;	/* if set, "From" person is explicit */
427 EXTERN bool	NoAlias;	/* if set, don't do any aliasing */
428 EXTERN bool	ForceMail;	/* if set, mail even if already got a copy */
429 EXTERN bool	MeToo;		/* send to the sender also */
430 EXTERN bool	IgnrDot;	/* don't let dot end messages */
431 EXTERN bool	SaveFrom;	/* save leading "From" lines */
432 EXTERN bool	Verbose;	/* set if blow-by-blow desired */
433 EXTERN bool	GrabTo;		/* if set, get recipients from msg */
434 EXTERN bool	NoReturn;	/* don't return letter to sender */
435 EXTERN bool	SuprErrs;	/* set if we are suppressing errors */
436 EXTERN bool	QueueRun;	/* currently running message from the queue */
437 EXTERN bool	HoldErrs;	/* only output errors to transcript */
438 EXTERN bool	NoConnect;	/* don't connect to non-local mailers */
439 EXTERN bool	SuperSafe;	/* be extra careful, even if expensive */
440 EXTERN bool	AutoRebuild;	/* auto-rebuild the alias database as needed */
441 EXTERN int	SafeAlias;	/* minutes to wait until @:@ in alias file */
442 EXTERN time_t	TimeOut;	/* time until timeout */
443 EXTERN FILE	*InChannel;	/* input connection */
444 EXTERN FILE	*OutChannel;	/* output connection */
445 EXTERN int	RealUid;	/* when Daemon, real uid of caller */
446 EXTERN int	RealGid;	/* when Daemon, real gid of caller */
447 EXTERN int	DefUid;		/* default uid to run as */
448 EXTERN int	DefGid;		/* default gid to run as */
449 EXTERN int	OldUmask;	/* umask when sendmail starts up */
450 EXTERN int	Errors;		/* set if errors (local to single pass) */
451 EXTERN int	ExitStat;	/* exit status code */
452 EXTERN int	AliasLevel;	/* depth of aliasing */
453 EXTERN int	MotherPid;	/* proc id of parent process */
454 EXTERN int	LineNumber;	/* line number in current input */
455 EXTERN time_t	ReadTimeout;	/* timeout on reads */
456 EXTERN int	LogLevel;	/* level of logging to perform */
457 EXTERN int	FileMode;	/* mode on files */
458 EXTERN time_t	QueueIntvl;	/* intervals between running the queue */
459 EXTERN char	*HostName;	/* name of this host for SMTP messages */
460 EXTERN char	*AliasFile;	/* location of alias file */
461 EXTERN char	*HelpFile;	/* location of SMTP help file */
462 EXTERN char	*StatFile;	/* location of statistics summary */
463 EXTERN char	*QueueDir;	/* location of queue directory */
464 EXTERN char	*FileName;	/* name to print on error messages */
465 EXTERN char	*TrustedUsers[MAXTRUST+1];	/* list of trusted users */
466 EXTERN jmp_buf	TopFrame;	/* branch-to-top-of-loop-on-error frame */
467 EXTERN bool	QuickAbort;	/*  .... but only if we want a quick abort */
468 extern char	*ConfFile;	/* location of configuration file [conf.c] */
469 extern char	*FreezeFile;	/* location of frozen memory image [conf.c] */
470 extern char	Arpa_Info[];	/* the reply code for Arpanet info [conf.c] */
471 extern char	SpaceSub;	/* substitution for <lwsp> [conf.c] */
472 /*
473 **  Trace information
474 */
475 
476 /* trace vector and macros for debugging flags */
477 EXTERN u_char	tTdvect[100];
478 # define tTd(flag, level)	(tTdvect[flag] >= level)
479 # define tTdlevel(flag)		(tTdvect[flag])
480 /*
481 **  Miscellaneous information.
482 */
483 
484 # include	<sysexits.h>
485 
486 
487 /*
488 **  Some in-line functions
489 */
490 
491 /* set exit status */
492 #define setstat(s)	{ \
493 				if (ExitStat == EX_OK || ExitStat == EX_TEMPFAIL) \
494 					ExitStat = s; \
495 			}
496 
497 /* make a copy of a string */
498 #define newstr(s)	strcpy(xalloc(strlen(s) + 1), s)
499 
500 
501 /*
502 **  Declarations of useful functions
503 */
504 
505 extern ADDRESS	*parseaddr();
506 extern char	*xalloc();
507 extern bool	sameaddr();
508 extern FILE	*dfopen();
509 extern EVENT	*setevent();
510 extern char	*sfgets();
511 extern char	*queuename();
512 extern time_t	curtime();
513