xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)conf.c	8.1 (Berkeley) 06/07/93";
11 #endif /* not lint */
12 
13 # include <sys/ioctl.h>
14 # include <sys/param.h>
15 # include <signal.h>
16 # include <pwd.h>
17 # include "sendmail.h"
18 # include "pathnames.h"
19 
20 /*
21 **  CONF.C -- Sendmail Configuration Tables.
22 **
23 **	Defines the configuration of this installation.
24 **
25 **	Configuration Variables:
26 **		HdrInfo -- a table describing well-known header fields.
27 **			Each entry has the field name and some flags,
28 **			which are described in sendmail.h.
29 **
30 **	Notes:
31 **		I have tried to put almost all the reasonable
32 **		configuration information into the configuration
33 **		file read at runtime.  My intent is that anything
34 **		here is a function of the version of UNIX you
35 **		are running, or is really static -- for example
36 **		the headers are a superset of widely used
37 **		protocols.  If you find yourself playing with
38 **		this file too much, you may be making a mistake!
39 */
40 
41 
42 
43 
44 /*
45 **  Header info table
46 **	Final (null) entry contains the flags used for any other field.
47 **
48 **	Not all of these are actually handled specially by sendmail
49 **	at this time.  They are included as placeholders, to let
50 **	you know that "someday" I intend to have sendmail do
51 **	something with them.
52 */
53 
54 struct hdrinfo	HdrInfo[] =
55 {
56 		/* originator fields, most to least significant  */
57 	"resent-sender",	H_FROM|H_RESENT,
58 	"resent-from",		H_FROM|H_RESENT,
59 	"resent-reply-to",	H_FROM|H_RESENT,
60 	"sender",		H_FROM,
61 	"from",			H_FROM,
62 	"reply-to",		H_FROM,
63 	"full-name",		H_ACHECK,
64 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
65 	"errors-to",		H_FROM|H_ERRORSTO,
66 
67 		/* destination fields */
68 	"to",			H_RCPT,
69 	"resent-to",		H_RCPT|H_RESENT,
70 	"cc",			H_RCPT,
71 	"resent-cc",		H_RCPT|H_RESENT,
72 	"bcc",			H_RCPT|H_ACHECK,
73 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
74 	"apparently-to",	H_RCPT,
75 
76 		/* message identification and control */
77 	"message-id",		0,
78 	"resent-message-id",	H_RESENT,
79 	"message",		H_EOH,
80 	"text",			H_EOH,
81 
82 		/* date fields */
83 	"date",			0,
84 	"resent-date",		H_RESENT,
85 
86 		/* trace fields */
87 	"received",		H_TRACE|H_FORCE,
88 	"x400-received",	H_TRACE|H_FORCE,
89 	"via",			H_TRACE|H_FORCE,
90 	"mail-from",		H_TRACE|H_FORCE,
91 
92 		/* miscellaneous fields */
93 	"comments",		H_FORCE,
94 	"return-path",		H_ACHECK,
95 
96 	NULL,			0,
97 };
98 
99 
100 
101 /*
102 **  Location of system files/databases/etc.
103 */
104 
105 char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
106 char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
107 char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
108 
109 
110 
111 /*
112 **  Privacy values
113 */
114 
115 struct prival PrivacyValues[] =
116 {
117 	"public",		PRIV_PUBLIC,
118 	"needmailhelo",		PRIV_NEEDMAILHELO,
119 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
120 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
121 	"noexpn",		PRIV_NOEXPN,
122 	"novrfy",		PRIV_NOVRFY,
123 	"restrictmailq",	PRIV_RESTRMAILQ,
124 	"authwarnings",		PRIV_AUTHWARNINGS,
125 	"goaway",		PRIV_GOAWAY,
126 	NULL,			0,
127 };
128 
129 
130 
131 /*
132 **  Miscellaneous stuff.
133 */
134 
135 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
136 /*
137 **  SETDEFAULTS -- set default values
138 **
139 **	Because of the way freezing is done, these must be initialized
140 **	using direct code.
141 **
142 **	Parameters:
143 **		e -- the default envelope.
144 **
145 **	Returns:
146 **		none.
147 **
148 **	Side Effects:
149 **		Initializes a bunch of global variables to their
150 **		default values.
151 */
152 
153 #define DAYS		* 24 * 60 * 60
154 
155 setdefaults(e)
156 	register ENVELOPE *e;
157 {
158 	SpaceSub = ' ';				/* option B */
159 	QueueLA = 8;				/* option x */
160 	RefuseLA = 12;				/* option X */
161 	WkRecipFact = 30000L;			/* option y */
162 	WkClassFact = 1800L;			/* option z */
163 	WkTimeFact = 90000L;			/* option Z */
164 	QueueFactor = WkRecipFact * 20;		/* option q */
165 	FileMode = (getuid() != geteuid()) ? 0644 : 0600;
166 						/* option F */
167 	DefUid = 1;				/* option u */
168 	DefGid = 1;				/* option g */
169 	CheckpointInterval = 10;		/* option C */
170 	MaxHopCount = 25;			/* option h */
171 	e->e_sendmode = SM_FORK;		/* option d */
172 	e->e_errormode = EM_PRINT;		/* option e */
173 	SevenBit = FALSE;			/* option 7 */
174 	MaxMciCache = 1;			/* option k */
175 	MciCacheTimeout = 300;			/* option K */
176 	LogLevel = 9;				/* option L */
177 	settimeouts(NULL);			/* option r */
178 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
179 	TimeOuts.to_q_warning = 0;		/* option T */
180 	PrivacyFlags = 0;			/* option p */
181 	setdefuser();
182 	setupmaps();
183 	setupmailers();
184 }
185 
186 
187 /*
188 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
189 */
190 
191 setdefuser()
192 {
193 	struct passwd *defpwent;
194 	static char defuserbuf[40];
195 
196 	DefUser = defuserbuf;
197 	if ((defpwent = getpwuid(DefUid)) != NULL)
198 		strcpy(defuserbuf, defpwent->pw_name);
199 	else
200 		strcpy(defuserbuf, "nobody");
201 }
202 /*
203 **  HOST_MAP_INIT -- initialize host class structures
204 */
205 
206 bool
207 host_map_init(map, args)
208 	MAP *map;
209 	char *args;
210 {
211 	register char *p = args;
212 
213 	for (;;)
214 	{
215 		while (isascii(*p) && isspace(*p))
216 			p++;
217 		if (*p != '-')
218 			break;
219 		switch (*++p)
220 		{
221 		  case 'a':
222 			map->map_app = ++p;
223 			break;
224 		}
225 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
226 			p++;
227 		if (*p != '\0')
228 			*p++ = '\0';
229 	}
230 	if (map->map_app != NULL)
231 		map->map_app = newstr(map->map_app);
232 	return TRUE;
233 }
234 /*
235 **  SETUPMAILERS -- initialize default mailers
236 */
237 
238 setupmailers()
239 {
240 	char buf[100];
241 
242 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
243 	makemailer(buf);
244 
245 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
246 	makemailer(buf);
247 
248 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
249 	makemailer(buf);
250 }
251 /*
252 **  SETUPMAPS -- set up map classes
253 */
254 
255 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
256 	{ \
257 		extern bool parse __P((MAP *, char *)); \
258 		extern bool open __P((MAP *, int)); \
259 		extern void close __P((MAP *)); \
260 		extern char *lookup __P((MAP *, char *, char **, int *)); \
261 		extern void store __P((MAP *, char *, char *)); \
262 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
263 		s->s_mapclass.map_cname = name; \
264 		s->s_mapclass.map_ext = ext; \
265 		s->s_mapclass.map_cflags = flags; \
266 		s->s_mapclass.map_parse = parse; \
267 		s->s_mapclass.map_open = open; \
268 		s->s_mapclass.map_close = close; \
269 		s->s_mapclass.map_lookup = lookup; \
270 		s->s_mapclass.map_store = store; \
271 	}
272 
273 setupmaps()
274 {
275 	register STAB *s;
276 
277 #ifdef NEWDB
278 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
279 		map_parseargs, hash_map_open, db_map_close,
280 		db_map_lookup, db_map_store);
281 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
282 		map_parseargs, bt_map_open, db_map_close,
283 		db_map_lookup, db_map_store);
284 #endif
285 
286 #ifdef NDBM
287 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
288 		map_parseargs, ndbm_map_open, ndbm_map_close,
289 		ndbm_map_lookup, ndbm_map_store);
290 #endif
291 
292 #ifdef NIS
293 	MAPDEF("nis", NULL, MCF_ALIASOK,
294 		map_parseargs, nis_map_open, nis_map_close,
295 		nis_map_lookup, nis_map_store);
296 #endif
297 
298 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
299 		map_parseargs, stab_map_open, stab_map_close,
300 		stab_map_lookup, stab_map_store);
301 
302 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
303 		map_parseargs, impl_map_open, impl_map_close,
304 		impl_map_lookup, impl_map_store);
305 
306 	/* host DNS lookup */
307 	MAPDEF("host", NULL, 0,
308 		host_map_init, null_map_open, null_map_close,
309 		host_map_lookup, null_map_store);
310 
311 	/* dequote map */
312 	MAPDEF("dequote", NULL, 0,
313 		dequote_init, null_map_open, null_map_close,
314 		dequote_map, null_map_store);
315 
316 #if 0
317 # ifdef USERDB
318 	/* user database */
319 	MAPDEF("udb", ".db", 0,
320 		udb_map_parse, null_map_open, null_map_close,
321 		udb_map_lookup, null_map_store);
322 # endif
323 #endif
324 }
325 
326 #undef MAPDEF
327 /*
328 **  GETRUID -- get real user id (V7)
329 */
330 
331 getruid()
332 {
333 	if (OpMode == MD_DAEMON)
334 		return (RealUid);
335 	else
336 		return (getuid());
337 }
338 
339 
340 /*
341 **  GETRGID -- get real group id (V7).
342 */
343 
344 getrgid()
345 {
346 	if (OpMode == MD_DAEMON)
347 		return (RealGid);
348 	else
349 		return (getgid());
350 }
351 /*
352 **  USERNAME -- return the user id of the logged in user.
353 **
354 **	Parameters:
355 **		none.
356 **
357 **	Returns:
358 **		The login name of the logged in user.
359 **
360 **	Side Effects:
361 **		none.
362 **
363 **	Notes:
364 **		The return value is statically allocated.
365 */
366 
367 char *
368 username()
369 {
370 	static char *myname = NULL;
371 	extern char *getlogin();
372 	register struct passwd *pw;
373 
374 	/* cache the result */
375 	if (myname == NULL)
376 	{
377 		myname = getlogin();
378 		if (myname == NULL || myname[0] == '\0')
379 		{
380 			pw = getpwuid(getruid());
381 			if (pw != NULL)
382 				myname = newstr(pw->pw_name);
383 		}
384 		else
385 		{
386 			uid_t uid = getuid();
387 
388 			myname = newstr(myname);
389 			if ((pw = getpwnam(myname)) == NULL ||
390 			      (uid != 0 && uid != pw->pw_uid))
391 			{
392 				pw = getpwuid(uid);
393 				if (pw != NULL)
394 					myname = newstr(pw->pw_name);
395 			}
396 		}
397 		if (myname == NULL || myname[0] == '\0')
398 		{
399 			syserr("554 Who are you?");
400 			myname = "postmaster";
401 		}
402 	}
403 
404 	return (myname);
405 }
406 /*
407 **  TTYPATH -- Get the path of the user's tty
408 **
409 **	Returns the pathname of the user's tty.  Returns NULL if
410 **	the user is not logged in or if s/he has write permission
411 **	denied.
412 **
413 **	Parameters:
414 **		none
415 **
416 **	Returns:
417 **		pathname of the user's tty.
418 **		NULL if not logged in or write permission denied.
419 **
420 **	Side Effects:
421 **		none.
422 **
423 **	WARNING:
424 **		Return value is in a local buffer.
425 **
426 **	Called By:
427 **		savemail
428 */
429 
430 char *
431 ttypath()
432 {
433 	struct stat stbuf;
434 	register char *pathn;
435 	extern char *ttyname();
436 	extern char *getlogin();
437 
438 	/* compute the pathname of the controlling tty */
439 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
440 	    (pathn = ttyname(0)) == NULL)
441 	{
442 		errno = 0;
443 		return (NULL);
444 	}
445 
446 	/* see if we have write permission */
447 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
448 	{
449 		errno = 0;
450 		return (NULL);
451 	}
452 
453 	/* see if the user is logged in */
454 	if (getlogin() == NULL)
455 		return (NULL);
456 
457 	/* looks good */
458 	return (pathn);
459 }
460 /*
461 **  CHECKCOMPAT -- check for From and To person compatible.
462 **
463 **	This routine can be supplied on a per-installation basis
464 **	to determine whether a person is allowed to send a message.
465 **	This allows restriction of certain types of internet
466 **	forwarding or registration of users.
467 **
468 **	If the hosts are found to be incompatible, an error
469 **	message should be given using "usrerr" and 0 should
470 **	be returned.
471 **
472 **	'NoReturn' can be set to suppress the return-to-sender
473 **	function; this should be done on huge messages.
474 **
475 **	Parameters:
476 **		to -- the person being sent to.
477 **
478 **	Returns:
479 **		an exit status
480 **
481 **	Side Effects:
482 **		none (unless you include the usrerr stuff)
483 */
484 
485 checkcompat(to, e)
486 	register ADDRESS *to;
487 	register ENVELOPE *e;
488 {
489 # ifdef lint
490 	if (to == NULL)
491 		to++;
492 # endif lint
493 # ifdef EXAMPLE_CODE
494 	/* this code is intended as an example only */
495 	register STAB *s;
496 
497 	s = stab("arpa", ST_MAILER, ST_FIND);
498 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
499 	    to->q_mailer == s->s_mailer)
500 	{
501 		usrerr("553 No ARPA mail through this machine: see your system administration");
502 		/* NoReturn = TRUE; to supress return copy */
503 		return (EX_UNAVAILABLE);
504 	}
505 # endif /* EXAMPLE_CODE */
506 	return (EX_OK);
507 }
508 /*
509 **  HOLDSIGS -- arrange to hold all signals
510 **
511 **	Parameters:
512 **		none.
513 **
514 **	Returns:
515 **		none.
516 **
517 **	Side Effects:
518 **		Arranges that signals are held.
519 */
520 
521 holdsigs()
522 {
523 }
524 /*
525 **  RLSESIGS -- arrange to release all signals
526 **
527 **	This undoes the effect of holdsigs.
528 **
529 **	Parameters:
530 **		none.
531 **
532 **	Returns:
533 **		none.
534 **
535 **	Side Effects:
536 **		Arranges that signals are released.
537 */
538 
539 rlsesigs()
540 {
541 }
542 /*
543 **  GETLA -- get the current load average
544 **
545 **	This code stolen from la.c.
546 **
547 **	Parameters:
548 **		none.
549 **
550 **	Returns:
551 **		The current load average as an integer.
552 **
553 **	Side Effects:
554 **		none.
555 */
556 
557 /* try to guess what style of load average we have */
558 #define LA_ZERO		1	/* always return load average as zero */
559 #define LA_INT		2	/* read kmem for avenrun; interpret as int */
560 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
561 #define LA_SUBR		4	/* call getloadavg */
562 
563 #ifndef LA_TYPE
564 #  if defined(sun) && !defined(BSD)
565 #    define LA_TYPE		LA_INT
566 #  endif
567 #  if defined(mips) || defined(__alpha)
568      /* Ultrix or OSF/1 or RISC/os */
569 #    define LA_TYPE		LA_INT
570 #    define LA_AVENRUN		"avenrun"
571 #  endif
572 #  if defined(__hpux)
573 #    define LA_TYPE		LA_FLOAT
574 #    define LA_AVENRUN		"avenrun"
575 #  endif
576 
577 /* now do the guesses based on general OS type */
578 #  ifndef LA_TYPE
579 #   if defined(SYSTEM5)
580 #    define LA_TYPE		LA_INT
581 #    define LA_AVENRUN		"avenrun"
582 #   else
583 #    if defined(BSD)
584 #     define LA_TYPE		LA_SUBR
585 #    else
586 #     define LA_TYPE		LA_ZERO
587 #    endif
588 #   endif
589 #  endif
590 #endif
591 
592 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
593 
594 #include <nlist.h>
595 
596 #ifndef LA_AVENRUN
597 #define LA_AVENRUN	"_avenrun"
598 #endif
599 
600 /* _PATH_UNIX should be defined in <paths.h> */
601 #ifndef _PATH_UNIX
602 #  if defined(__hpux)
603 #    define _PATH_UNIX		"/hp-ux"
604 #  endif
605 #  if defined(mips) && !defined(ultrix)
606      /* powerful RISC/os */
607 #    define _PATH_UNIX		"/unix"
608 #  endif
609 #  if defined(SYSTEM5)
610 #    ifndef _PATH_UNIX
611 #      define _PATH_UNIX	"/unix"
612 #    endif
613 #  endif
614 #  ifndef _PATH_UNIX
615 #    define _PATH_UNIX		"/vmunix"
616 #  endif
617 #endif
618 
619 struct	nlist Nl[] =
620 {
621 	{ LA_AVENRUN },
622 #define	X_AVENRUN	0
623 	{ 0 },
624 };
625 
626 #ifndef FSHIFT
627 # if defined(unixpc)
628 #  define FSHIFT	5
629 # endif
630 
631 # if defined(__alpha)
632 #  define FSHIFT	10
633 # endif
634 
635 # if (LA_TYPE == LA_INT)
636 #  define FSHIFT	8
637 # endif
638 #endif
639 
640 #if (LA_TYPE == LA_INT) && !defined(FSCALE)
641 #  define FSCALE	(1 << FSHIFT)
642 #endif
643 
644 getla()
645 {
646 	static int kmem = -1;
647 #if LA_TYPE == LA_INT
648 	long avenrun[3];
649 #else
650 	double avenrun[3];
651 #endif
652 	extern off_t lseek();
653 	extern int errno;
654 
655 	if (kmem < 0)
656 	{
657 		kmem = open("/dev/kmem", 0, 0);
658 		if (kmem < 0)
659 		{
660 			if (tTd(3, 1))
661 				printf("getla: open(/dev/kmem): %s\n",
662 					errstring(errno));
663 			return (-1);
664 		}
665 		(void) fcntl(kmem, F_SETFD, 1);
666 		if (nlist(_PATH_UNIX, Nl) < 0)
667 		{
668 			if (tTd(3, 1))
669 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
670 					errstring(errno));
671 			return (-1);
672 		}
673 		if (Nl[X_AVENRUN].n_value == 0)
674 		{
675 			if (tTd(3, 1))
676 				printf("getla: nlist(%s, %s) ==> 0\n",
677 					_PATH_UNIX, LA_AVENRUN);
678 			return (-1);
679 		}
680 	}
681 	if (tTd(3, 20))
682 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
683 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
684 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
685 	{
686 		/* thank you Ian */
687 		if (tTd(3, 1))
688 			printf("getla: lseek or read: %s\n", errstring(errno));
689 		return (-1);
690 	}
691 #if LA_TYPE == LA_INT
692 	if (tTd(3, 5))
693 	{
694 		printf("getla: avenrun = %d", avenrun[0]);
695 		if (tTd(3, 15))
696 			printf(", %d, %d", avenrun[1], avenrun[2]);
697 		printf("\n");
698 	}
699 	if (tTd(3, 1))
700 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
701 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
702 #else
703 	if (tTd(3, 5))
704 	{
705 		printf("getla: avenrun = %g", avenrun[0]);
706 		if (tTd(3, 15))
707 			printf(", %g, %g", avenrun[1], avenrun[2]);
708 		printf("\n");
709 	}
710 	if (tTd(3, 1))
711 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
712 	return ((int) (avenrun[0] + 0.5));
713 #endif
714 }
715 
716 #else
717 #if LA_TYPE == LA_SUBR
718 
719 getla()
720 {
721 	double avenrun[3];
722 
723 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
724 	{
725 		if (tTd(3, 1))
726 			perror("getla: getloadavg failed:");
727 		return (-1);
728 	}
729 	if (tTd(3, 1))
730 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
731 	return ((int) (avenrun[0] + 0.5));
732 }
733 
734 #else
735 
736 getla()
737 {
738 	if (tTd(3, 1))
739 		printf("getla: ZERO\n");
740 	return (0);
741 }
742 
743 #endif
744 #endif
745 /*
746 **  SHOULDQUEUE -- should this message be queued or sent?
747 **
748 **	Compares the message cost to the load average to decide.
749 **
750 **	Parameters:
751 **		pri -- the priority of the message in question.
752 **		ctime -- the message creation time.
753 **
754 **	Returns:
755 **		TRUE -- if this message should be queued up for the
756 **			time being.
757 **		FALSE -- if the load is low enough to send this message.
758 **
759 **	Side Effects:
760 **		none.
761 */
762 
763 bool
764 shouldqueue(pri, ctime)
765 	long pri;
766 	time_t ctime;
767 {
768 	if (CurrentLA < QueueLA)
769 		return (FALSE);
770 	if (CurrentLA >= RefuseLA)
771 		return (TRUE);
772 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
773 }
774 /*
775 **  REFUSECONNECTIONS -- decide if connections should be refused
776 **
777 **	Parameters:
778 **		none.
779 **
780 **	Returns:
781 **		TRUE if incoming SMTP connections should be refused
782 **			(for now).
783 **		FALSE if we should accept new work.
784 **
785 **	Side Effects:
786 **		none.
787 */
788 
789 bool
790 refuseconnections()
791 {
792 #ifdef XLA
793 	if (!xla_smtp_ok())
794 		return TRUE;
795 #endif
796 
797 	/* this is probably too simplistic */
798 	return (CurrentLA >= RefuseLA);
799 }
800 /*
801 **  SETPROCTITLE -- set process title for ps
802 **
803 **	Parameters:
804 **		fmt -- a printf style format string.
805 **		a, b, c -- possible parameters to fmt.
806 **
807 **	Returns:
808 **		none.
809 **
810 **	Side Effects:
811 **		Clobbers argv of our main procedure so ps(1) will
812 **		display the title.
813 */
814 
815 #ifdef SETPROCTITLE
816 # ifdef __hpux
817 #  include <sys/pstat.h>
818 # endif
819 # ifdef BSD4_4
820 #  include <machine/vmparam.h>
821 #  include <sys/exec.h>
822 #  ifdef PS_STRINGS
823 #   define SETPROC_STATIC static
824 #  endif
825 # endif
826 # ifndef SETPROC_STATIC
827 #  define SETPROC_STATIC
828 # endif
829 #endif
830 
831 /*VARARGS1*/
832 #ifdef __STDC__
833 setproctitle(char *fmt, ...)
834 #else
835 setproctitle(fmt, va_alist)
836 	char *fmt;
837 	va_dcl
838 #endif
839 {
840 # ifdef SETPROCTITLE
841 	register char *p;
842 	register int i;
843 	SETPROC_STATIC char buf[MAXLINE];
844 	VA_LOCAL_DECL
845 #  ifdef __hpux
846 	union pstun pst;
847 #  endif
848 	extern char **Argv;
849 	extern char *LastArgv;
850 
851 	p = buf;
852 
853 	/* print sendmail: heading for grep */
854 	(void) strcpy(p, "sendmail: ");
855 	p += strlen(p);
856 
857 	/* print the argument string */
858 	VA_START(fmt);
859 	(void) vsprintf(p, fmt, ap);
860 	VA_END;
861 
862 	i = strlen(buf);
863 
864 #  ifdef __hpux
865 	pst.pst_command = buf;
866 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
867 #  else
868 #   ifdef PS_STRINGS
869 	PS_STRINGS->ps_nargvstr = 1;
870 	PS_STRINGS->ps_argvstr = buf;
871 #   else
872 	if (i > LastArgv - Argv[0] - 2)
873 	{
874 		i = LastArgv - Argv[0] - 2;
875 		buf[i] = '\0';
876 	}
877 	(void) strcpy(Argv[0], buf);
878 	p = &Argv[0][i];
879 	while (p < LastArgv)
880 		*p++ = ' ';
881 #   endif
882 #  endif
883 # endif /* SETPROCTITLE */
884 }
885 /*
886 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
887 **
888 **	Parameters:
889 **		none.
890 **
891 **	Returns:
892 **		none.
893 **
894 **	Side Effects:
895 **		Picks up extant zombies.
896 */
897 
898 # include <sys/wait.h>
899 
900 void
901 reapchild()
902 {
903 # ifdef WIFEXITED
904 	auto int status;
905 	int count;
906 	int pid;
907 
908 	count = 0;
909 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
910 	{
911 		if (count++ > 1000)
912 		{
913 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
914 				pid, status);
915 			break;
916 		}
917 	}
918 # else
919 # ifdef WNOHANG
920 	union wait status;
921 
922 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
923 		continue;
924 # else /* WNOHANG */
925 	auto int status;
926 
927 	while (wait(&status) > 0)
928 		continue;
929 # endif /* WNOHANG */
930 # endif
931 # ifdef SYSTEM5
932 	(void) signal(SIGCHLD, reapchild);
933 # endif
934 }
935 /*
936 **  UNSETENV -- remove a variable from the environment
937 **
938 **	Not needed on newer systems.
939 **
940 **	Parameters:
941 **		name -- the string name of the environment variable to be
942 **			deleted from the current environment.
943 **
944 **	Returns:
945 **		none.
946 **
947 **	Globals:
948 **		environ -- a pointer to the current environment.
949 **
950 **	Side Effects:
951 **		Modifies environ.
952 */
953 
954 #ifdef UNSETENV
955 
956 void
957 unsetenv(name)
958 	char *name;
959 {
960 	extern char **environ;
961 	register char **pp;
962 	int len = strlen(name);
963 
964 	for (pp = environ; *pp != NULL; pp++)
965 	{
966 		if (strncmp(name, *pp, len) == 0 &&
967 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
968 			break;
969 	}
970 
971 	for (; *pp != NULL; pp++)
972 		*pp = pp[1];
973 }
974 
975 #endif /* UNSETENV */
976 /*
977 **  GETDTABLESIZE -- return number of file descriptors
978 **
979 **	Only on non-BSD systems
980 **
981 **	Parameters:
982 **		none
983 **
984 **	Returns:
985 **		size of file descriptor table
986 **
987 **	Side Effects:
988 **		none
989 */
990 
991 #ifdef SYSTEM5
992 
993 int
994 getdtablesize()
995 {
996 # ifdef _SC_OPEN_MAX
997 	return sysconf(_SC_OPEN_MAX);
998 # else
999 	return NOFILE;
1000 # endif
1001 }
1002 
1003 #endif
1004 /*
1005 **  UNAME -- get the UUCP name of this system.
1006 */
1007 
1008 #ifndef HASUNAME
1009 
1010 int
1011 uname(name)
1012 	struct utsname *name;
1013 {
1014 	FILE *file;
1015 	char *n;
1016 
1017 	name->nodename[0] = '\0';
1018 
1019 	/* try /etc/whoami -- one line with the node name */
1020 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1021 	{
1022 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1023 		(void) fclose(file);
1024 		n = strchr(name->nodename, '\n');
1025 		if (n != NULL)
1026 			*n = '\0';
1027 		if (name->nodename[0] != '\0')
1028 			return (0);
1029 	}
1030 
1031 	/* try /usr/include/whoami.h -- has a #define somewhere */
1032 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1033 	{
1034 		char buf[MAXLINE];
1035 
1036 		while (fgets(buf, MAXLINE, file) != NULL)
1037 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1038 					NODE_LENGTH, name->nodename) > 0)
1039 				break;
1040 		(void) fclose(file);
1041 		if (name->nodename[0] != '\0')
1042 			return (0);
1043 	}
1044 
1045 #ifdef TRUST_POPEN
1046 	/*
1047 	**  Popen is known to have security holes.
1048 	*/
1049 
1050 	/* try uuname -l to return local name */
1051 	if ((file = popen("uuname -l", "r")) != NULL)
1052 	{
1053 		(void) fgets(name, NODE_LENGTH + 1, file);
1054 		(void) pclose(file);
1055 		n = strchr(name, '\n');
1056 		if (n != NULL)
1057 			*n = '\0';
1058 		if (name->nodename[0] != '\0')
1059 			return (0);
1060 	}
1061 #endif
1062 
1063 	return (-1);
1064 }
1065 #endif /* HASUNAME */
1066 /*
1067 **  INITGROUPS -- initialize groups
1068 **
1069 **	Stub implementation for System V style systems
1070 */
1071 
1072 #ifndef HASINITGROUPS
1073 # if !defined(SYSTEM5) || defined(__hpux)
1074 #  define HASINITGROUPS
1075 # endif
1076 #endif
1077 
1078 #ifndef HASINITGROUPS
1079 
1080 initgroups(name, basegid)
1081 	char *name;
1082 	int basegid;
1083 {
1084 	return 0;
1085 }
1086 
1087 #endif
1088 /*
1089 **  SETSID -- set session id (for non-POSIX systems)
1090 */
1091 
1092 #ifndef HASSETSID
1093 
1094 setsid()
1095 {
1096 # ifdef SYSTEM5
1097 	setpgrp();
1098 # endif
1099 }
1100 
1101 #endif
1102 /*
1103 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1104 **
1105 **	Only implemented if you have statfs.
1106 **
1107 **	Parameters:
1108 **		msize -- the size to check against.  If zero, we don't yet
1109 **			know how big the message will be, so just check for
1110 **			a "reasonable" amount.
1111 **
1112 **	Returns:
1113 **		TRUE if there is enough space.
1114 **		FALSE otherwise.
1115 */
1116 
1117 #ifndef HASSTATFS
1118 # if defined(BSD4_4) || defined(__osf__)
1119 #  define HASSTATFS
1120 # endif
1121 #endif
1122 
1123 #ifdef HASSTATFS
1124 # undef HASUSTAT
1125 #endif
1126 
1127 #if defined(HASUSTAT)
1128 # include <ustat.h>
1129 #endif
1130 
1131 #ifdef HASSTATFS
1132 # if defined(sgi) || defined(apollo)
1133 #  include <sys/statfs.h>
1134 # else
1135 #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
1136 #   include <sys/vfs.h>
1137 #  else
1138 #   include <sys/mount.h>
1139 #  endif
1140 # endif
1141 #endif
1142 
1143 bool
1144 enoughspace(msize)
1145 	long msize;
1146 {
1147 #if defined(HASSTATFS) || defined(HASUSTAT)
1148 # if defined(HASUSTAT)
1149 	struct ustat fs;
1150 	struct stat statbuf;
1151 #  define FSBLOCKSIZE	DEV_BSIZE
1152 #  define f_bavail	f_tfree
1153 # else
1154 #  if defined(ultrix)
1155 	struct fs_data fs;
1156 #   define f_bavail	fd_bfreen
1157 #   define FSBLOCKSIZE	fs.fd_bsize
1158 #  else
1159 	struct statfs fs;
1160 #   define FSBLOCKSIZE	fs.f_bsize
1161 #  endif
1162 # endif
1163 	extern int errno;
1164 
1165 	if (MinBlocksFree <= 0 && msize <= 0)
1166 	{
1167 		if (tTd(4, 80))
1168 			printf("enoughspace: no threshold\n");
1169 		return TRUE;
1170 	}
1171 
1172 # if defined(HASUSTAT)
1173 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1174 # else
1175 #  if defined(sgi) || defined(apollo)
1176 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
1177 #  else
1178 #   if defined(ultrix)
1179 	if (statfs(QueueDir, &fs) > 0)
1180 #   else
1181 	if (statfs(QueueDir, &fs) == 0)
1182 #   endif
1183 #  endif
1184 # endif
1185 	{
1186 		if (tTd(4, 80))
1187 			printf("enoughspace: bavail=%ld, need=%ld\n",
1188 				fs.f_bavail, msize);
1189 
1190 		/* convert msize to block count */
1191 		msize = msize / FSBLOCKSIZE + 1;
1192 		if (MinBlocksFree >= 0)
1193 			msize += MinBlocksFree;
1194 
1195 		if (fs.f_bavail < msize)
1196 		{
1197 #ifdef LOG
1198 			if (LogLevel > 0)
1199 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
1200 					QueueDir, fs.f_bavail, msize);
1201 #endif
1202 			return FALSE;
1203 		}
1204 	}
1205 	else if (tTd(4, 80))
1206 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1207 			MinBlocksFree, msize, errstring(errno));
1208 #endif
1209 	return TRUE;
1210 }
1211 /*
1212 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1213 **
1214 **	This looks at an errno value and tells if this is likely to
1215 **	go away if retried later.
1216 **
1217 **	Parameters:
1218 **		err -- the errno code to classify.
1219 **
1220 **	Returns:
1221 **		TRUE if this is probably transient.
1222 **		FALSE otherwise.
1223 */
1224 
1225 bool
1226 transienterror(err)
1227 	int err;
1228 {
1229 	switch (err)
1230 	{
1231 	  case EIO:			/* I/O error */
1232 	  case ENXIO:			/* Device not configured */
1233 	  case EAGAIN:			/* Resource temporarily unavailable */
1234 	  case ENOMEM:			/* Cannot allocate memory */
1235 	  case ENODEV:			/* Operation not supported by device */
1236 	  case ENFILE:			/* Too many open files in system */
1237 	  case EMFILE:			/* Too many open files */
1238 	  case ENOSPC:			/* No space left on device */
1239 #ifdef ETIMEDOUT
1240 	  case ETIMEDOUT:		/* Connection timed out */
1241 #endif
1242 #ifdef ESTALE
1243 	  case ESTALE:			/* Stale NFS file handle */
1244 #endif
1245 #ifdef ENETDOWN
1246 	  case ENETDOWN:		/* Network is down */
1247 #endif
1248 #ifdef ENETUNREACH
1249 	  case ENETUNREACH:		/* Network is unreachable */
1250 #endif
1251 #ifdef ENETRESET
1252 	  case ENETRESET:		/* Network dropped connection on reset */
1253 #endif
1254 #ifdef ECONNABORTED
1255 	  case ECONNABORTED:		/* Software caused connection abort */
1256 #endif
1257 #ifdef ECONNRESET
1258 	  case ECONNRESET:		/* Connection reset by peer */
1259 #endif
1260 #ifdef ENOBUFS
1261 	  case ENOBUFS:			/* No buffer space available */
1262 #endif
1263 #ifdef ESHUTDOWN
1264 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1265 #endif
1266 #ifdef ECONNREFUSED
1267 	  case ECONNREFUSED:		/* Connection refused */
1268 #endif
1269 #ifdef EHOSTDOWN
1270 	  case EHOSTDOWN:		/* Host is down */
1271 #endif
1272 #ifdef EHOSTUNREACH
1273 	  case EHOSTUNREACH:		/* No route to host */
1274 #endif
1275 #ifdef EDQUOT
1276 	  case EDQUOT:			/* Disc quota exceeded */
1277 #endif
1278 #ifdef EPROCLIM
1279 	  case EPROCLIM:		/* Too many processes */
1280 #endif
1281 #ifdef EUSERS
1282 	  case EUSERS:			/* Too many users */
1283 #endif
1284 #ifdef EDEADLK
1285 	  case EDEADLK:			/* Resource deadlock avoided */
1286 #endif
1287 #ifdef EISCONN
1288 	  case EISCONN:			/* Socket already connected */
1289 #endif
1290 #ifdef EINPROGRESS
1291 	  case EINPROGRESS:		/* Operation now in progress */
1292 #endif
1293 #ifdef EALREADY
1294 	  case EALREADY:		/* Operation already in progress */
1295 #endif
1296 #ifdef EADDRINUSE
1297 	  case EADDRINUSE:		/* Address already in use */
1298 #endif
1299 #ifdef EADDRNOTAVAIL
1300 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1301 #endif
1302 #ifdef ENOSR
1303 	  case ENOSR:			/* Out of streams resources */
1304 #endif
1305 		return TRUE;
1306 	}
1307 
1308 	/* nope, must be permanent */
1309 	return FALSE;
1310 }
1311 /*
1312 **  LOCKFILE -- lock a file using flock or (shudder) lockf
1313 **
1314 **	Parameters:
1315 **		fd -- the file descriptor of the file.
1316 **		filename -- the file name (for error messages).
1317 **		type -- type of the lock.  Bits can be:
1318 **			LOCK_EX -- exclusive lock.
1319 **			LOCK_NB -- non-blocking.
1320 **
1321 **	Returns:
1322 **		TRUE if the lock was acquired.
1323 **		FALSE otherwise.
1324 */
1325 
1326 bool
1327 lockfile(fd, filename, type)
1328 	int fd;
1329 	char *filename;
1330 	int type;
1331 {
1332 # ifdef LOCKF
1333 	int action;
1334 	struct flock lfd;
1335 
1336 	if (bitset(LOCK_UN, type))
1337 		lfd.l_type = F_UNLCK;
1338 	else if (bitset(LOCK_EX, type))
1339 		lfd.l_type = F_WRLCK;
1340 	else
1341 		lfd.l_type = F_RDLCK;
1342 
1343 	if (bitset(LOCK_NB, type))
1344 		action = F_SETLK;
1345 	else
1346 		action = F_SETLKW;
1347 
1348 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1349 
1350 	if (fcntl(fd, action, &lfd) >= 0)
1351 		return TRUE;
1352 
1353 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1354 		syserr("cannot lockf(%s, %o)", filename, type);
1355 # else
1356 	if (flock(fd, type) >= 0)
1357 		return TRUE;
1358 
1359 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1360 		syserr("cannot flock(%s, %o)", filename, type);
1361 # endif
1362 	return FALSE;
1363 }
1364