xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision 333da485)
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.66 (Berkeley) 01/22/94";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include "pathnames.h"
15 # include <sys/ioctl.h>
16 # include <sys/param.h>
17 # include <netdb.h>
18 # include <pwd.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_FORCE|H_ACHECK,
95 
96 	NULL,			0,
97 };
98 
99 
100 
101 /*
102 **  Location of system files/databases/etc.
103 */
104 
105 char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
106 
107 
108 
109 /*
110 **  Privacy values
111 */
112 
113 struct prival PrivacyValues[] =
114 {
115 	"public",		PRIV_PUBLIC,
116 	"needmailhelo",		PRIV_NEEDMAILHELO,
117 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
118 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
119 	"noexpn",		PRIV_NOEXPN,
120 	"novrfy",		PRIV_NOVRFY,
121 	"restrictmailq",	PRIV_RESTRICTMAILQ,
122 	"restrictqrun",		PRIV_RESTRICTQRUN,
123 	"authwarnings",		PRIV_AUTHWARNINGS,
124 	"goaway",		PRIV_GOAWAY,
125 	NULL,			0,
126 };
127 
128 
129 
130 /*
131 **  Miscellaneous stuff.
132 */
133 
134 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
135 
136 
137 /*
138 **  Following should be config parameters (and probably will be in
139 **  future releases).  In the meantime, setting these is considered
140 **  unsupported, and is intentionally undocumented.
141 */
142 
143 #ifdef BROKENSMTPPEERS
144 bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
145 #else
146 bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
147 #endif
148 #ifdef NOLOOPBACKCHECK
149 bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
150 #else
151 bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
152 #endif
153 
154 /*
155 **  SETDEFAULTS -- set default values
156 **
157 **	Because of the way freezing is done, these must be initialized
158 **	using direct code.
159 **
160 **	Parameters:
161 **		e -- the default envelope.
162 **
163 **	Returns:
164 **		none.
165 **
166 **	Side Effects:
167 **		Initializes a bunch of global variables to their
168 **		default values.
169 */
170 
171 #define DAYS		* 24 * 60 * 60
172 
173 setdefaults(e)
174 	register ENVELOPE *e;
175 {
176 	SpaceSub = ' ';				/* option B */
177 	QueueLA = 8;				/* option x */
178 	RefuseLA = 12;				/* option X */
179 	WkRecipFact = 30000L;			/* option y */
180 	WkClassFact = 1800L;			/* option z */
181 	WkTimeFact = 90000L;			/* option Z */
182 	QueueFactor = WkRecipFact * 20;		/* option q */
183 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
184 						/* option F */
185 	DefUid = 1;				/* option u */
186 	DefGid = 1;				/* option g */
187 	CheckpointInterval = 10;		/* option C */
188 	MaxHopCount = 25;			/* option h */
189 	e->e_sendmode = SM_FORK;		/* option d */
190 	e->e_errormode = EM_PRINT;		/* option e */
191 	SevenBit = FALSE;			/* option 7 */
192 	MaxMciCache = 1;			/* option k */
193 	MciCacheTimeout = 300;			/* option K */
194 	LogLevel = 9;				/* option L */
195 	settimeouts(NULL);			/* option r */
196 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
197 	TimeOuts.to_q_warning = 0;		/* option T */
198 	PrivacyFlags = 0;			/* option p */
199 	setdefuser();
200 	setupmaps();
201 	setupmailers();
202 }
203 
204 
205 /*
206 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
207 */
208 
209 setdefuser()
210 {
211 	struct passwd *defpwent;
212 	static char defuserbuf[40];
213 
214 	DefUser = defuserbuf;
215 	if ((defpwent = getpwuid(DefUid)) != NULL)
216 		strcpy(defuserbuf, defpwent->pw_name);
217 	else
218 		strcpy(defuserbuf, "nobody");
219 }
220 /*
221 **  HOST_MAP_INIT -- initialize host class structures
222 */
223 
224 bool
225 host_map_init(map, args)
226 	MAP *map;
227 	char *args;
228 {
229 	register char *p = args;
230 
231 	for (;;)
232 	{
233 		while (isascii(*p) && isspace(*p))
234 			p++;
235 		if (*p != '-')
236 			break;
237 		switch (*++p)
238 		{
239 		  case 'a':
240 			map->map_app = ++p;
241 			break;
242 		}
243 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
244 			p++;
245 		if (*p != '\0')
246 			*p++ = '\0';
247 	}
248 	if (map->map_app != NULL)
249 		map->map_app = newstr(map->map_app);
250 	return TRUE;
251 }
252 /*
253 **  SETUPMAILERS -- initialize default mailers
254 */
255 
256 setupmailers()
257 {
258 	char buf[100];
259 
260 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
261 	makemailer(buf);
262 
263 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
264 	makemailer(buf);
265 
266 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
267 	makemailer(buf);
268 }
269 /*
270 **  SETUPMAPS -- set up map classes
271 */
272 
273 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
274 	{ \
275 		extern bool parse __P((MAP *, char *)); \
276 		extern bool open __P((MAP *, int)); \
277 		extern void close __P((MAP *)); \
278 		extern char *lookup __P((MAP *, char *, char **, int *)); \
279 		extern void store __P((MAP *, char *, char *)); \
280 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
281 		s->s_mapclass.map_cname = name; \
282 		s->s_mapclass.map_ext = ext; \
283 		s->s_mapclass.map_cflags = flags; \
284 		s->s_mapclass.map_parse = parse; \
285 		s->s_mapclass.map_open = open; \
286 		s->s_mapclass.map_close = close; \
287 		s->s_mapclass.map_lookup = lookup; \
288 		s->s_mapclass.map_store = store; \
289 	}
290 
291 setupmaps()
292 {
293 	register STAB *s;
294 
295 #ifdef NEWDB
296 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
297 		map_parseargs, hash_map_open, db_map_close,
298 		db_map_lookup, db_map_store);
299 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
300 		map_parseargs, bt_map_open, db_map_close,
301 		db_map_lookup, db_map_store);
302 #endif
303 
304 #ifdef NDBM
305 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
306 		map_parseargs, ndbm_map_open, ndbm_map_close,
307 		ndbm_map_lookup, ndbm_map_store);
308 #endif
309 
310 #ifdef NIS
311 	MAPDEF("nis", NULL, MCF_ALIASOK,
312 		map_parseargs, nis_map_open, nis_map_close,
313 		nis_map_lookup, nis_map_store);
314 #endif
315 
316 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
317 		map_parseargs, stab_map_open, stab_map_close,
318 		stab_map_lookup, stab_map_store);
319 
320 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
321 		map_parseargs, impl_map_open, impl_map_close,
322 		impl_map_lookup, impl_map_store);
323 
324 	/* host DNS lookup */
325 	MAPDEF("host", NULL, 0,
326 		host_map_init, null_map_open, null_map_close,
327 		host_map_lookup, null_map_store);
328 
329 	/* dequote map */
330 	MAPDEF("dequote", NULL, 0,
331 		dequote_init, null_map_open, null_map_close,
332 		dequote_map, null_map_store);
333 
334 #if 0
335 # ifdef USERDB
336 	/* user database */
337 	MAPDEF("udb", ".db", 0,
338 		udb_map_parse, null_map_open, null_map_close,
339 		udb_map_lookup, null_map_store);
340 # endif
341 #endif
342 }
343 
344 #undef MAPDEF
345 /*
346 **  USERNAME -- return the user id of the logged in user.
347 **
348 **	Parameters:
349 **		none.
350 **
351 **	Returns:
352 **		The login name of the logged in user.
353 **
354 **	Side Effects:
355 **		none.
356 **
357 **	Notes:
358 **		The return value is statically allocated.
359 */
360 
361 char *
362 username()
363 {
364 	static char *myname = NULL;
365 	extern char *getlogin();
366 	register struct passwd *pw;
367 
368 	/* cache the result */
369 	if (myname == NULL)
370 	{
371 		myname = getlogin();
372 		if (myname == NULL || myname[0] == '\0')
373 		{
374 			pw = getpwuid(RealUid);
375 			if (pw != NULL)
376 				myname = newstr(pw->pw_name);
377 		}
378 		else
379 		{
380 			uid_t uid = RealUid;
381 
382 			myname = newstr(myname);
383 			if ((pw = getpwnam(myname)) == NULL ||
384 			      (uid != 0 && uid != pw->pw_uid))
385 			{
386 				pw = getpwuid(uid);
387 				if (pw != NULL)
388 					myname = newstr(pw->pw_name);
389 			}
390 		}
391 		if (myname == NULL || myname[0] == '\0')
392 		{
393 			syserr("554 Who are you?");
394 			myname = "postmaster";
395 		}
396 	}
397 
398 	return (myname);
399 }
400 /*
401 **  TTYPATH -- Get the path of the user's tty
402 **
403 **	Returns the pathname of the user's tty.  Returns NULL if
404 **	the user is not logged in or if s/he has write permission
405 **	denied.
406 **
407 **	Parameters:
408 **		none
409 **
410 **	Returns:
411 **		pathname of the user's tty.
412 **		NULL if not logged in or write permission denied.
413 **
414 **	Side Effects:
415 **		none.
416 **
417 **	WARNING:
418 **		Return value is in a local buffer.
419 **
420 **	Called By:
421 **		savemail
422 */
423 
424 char *
425 ttypath()
426 {
427 	struct stat stbuf;
428 	register char *pathn;
429 	extern char *ttyname();
430 	extern char *getlogin();
431 
432 	/* compute the pathname of the controlling tty */
433 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
434 	    (pathn = ttyname(0)) == NULL)
435 	{
436 		errno = 0;
437 		return (NULL);
438 	}
439 
440 	/* see if we have write permission */
441 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
442 	{
443 		errno = 0;
444 		return (NULL);
445 	}
446 
447 	/* see if the user is logged in */
448 	if (getlogin() == NULL)
449 		return (NULL);
450 
451 	/* looks good */
452 	return (pathn);
453 }
454 /*
455 **  CHECKCOMPAT -- check for From and To person compatible.
456 **
457 **	This routine can be supplied on a per-installation basis
458 **	to determine whether a person is allowed to send a message.
459 **	This allows restriction of certain types of internet
460 **	forwarding or registration of users.
461 **
462 **	If the hosts are found to be incompatible, an error
463 **	message should be given using "usrerr" and 0 should
464 **	be returned.
465 **
466 **	'NoReturn' can be set to suppress the return-to-sender
467 **	function; this should be done on huge messages.
468 **
469 **	Parameters:
470 **		to -- the person being sent to.
471 **
472 **	Returns:
473 **		an exit status
474 **
475 **	Side Effects:
476 **		none (unless you include the usrerr stuff)
477 */
478 
479 checkcompat(to, e)
480 	register ADDRESS *to;
481 	register ENVELOPE *e;
482 {
483 # ifdef lint
484 	if (to == NULL)
485 		to++;
486 # endif /* lint */
487 
488 	if (tTd(49, 1))
489 		printf("checkcompat(to=%s, from=%s)\n",
490 			to->q_paddr, e->e_from.q_paddr);
491 
492 # ifdef EXAMPLE_CODE
493 	/* this code is intended as an example only */
494 	register STAB *s;
495 
496 	s = stab("arpa", ST_MAILER, ST_FIND);
497 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
498 	    to->q_mailer == s->s_mailer)
499 	{
500 		usrerr("553 No ARPA mail through this machine: see your system administration");
501 		/* NoReturn = TRUE; to supress return copy */
502 		return (EX_UNAVAILABLE);
503 	}
504 # endif /* EXAMPLE_CODE */
505 	return (EX_OK);
506 }
507 /*
508 **  SETSIGNAL -- set a signal handler
509 **
510 **	This is essentially old BSD "signal(3)".
511 */
512 
513 sigfunc_t
514 setsignal(sig, handler)
515 	int sig;
516 	sigfunc_t handler;
517 {
518 #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
519 	return signal(sig, handler);
520 #else
521 	struct sigaction n, o;
522 
523 	bzero(&n, sizeof n);
524 	n.sa_handler = handler;
525 	if (sigaction(sig, &n, &o) < 0)
526 		return SIG_ERR;
527 	return o.sa_handler;
528 #endif
529 }
530 /*
531 **  HOLDSIGS -- arrange to hold all signals
532 **
533 **	Parameters:
534 **		none.
535 **
536 **	Returns:
537 **		none.
538 **
539 **	Side Effects:
540 **		Arranges that signals are held.
541 */
542 
543 holdsigs()
544 {
545 }
546 /*
547 **  RLSESIGS -- arrange to release all signals
548 **
549 **	This undoes the effect of holdsigs.
550 **
551 **	Parameters:
552 **		none.
553 **
554 **	Returns:
555 **		none.
556 **
557 **	Side Effects:
558 **		Arranges that signals are released.
559 */
560 
561 rlsesigs()
562 {
563 }
564 /*
565 **  INIT_MD -- do machine dependent initializations
566 **
567 **	Systems that have global modes that should be set should do
568 **	them here rather than in main.
569 */
570 
571 #ifdef _AUX_SOURCE
572 # include	<compat.h>
573 #endif
574 
575 init_md(argc, argv)
576 	int argc;
577 	char **argv;
578 {
579 #ifdef _AUX_SOURCE
580 	setcompat(getcompat() | COMPAT_BSDPROT);
581 #endif
582 }
583 /*
584 **  GETLA -- get the current load average
585 **
586 **	This code stolen from la.c.
587 **
588 **	Parameters:
589 **		none.
590 **
591 **	Returns:
592 **		The current load average as an integer.
593 **
594 **	Side Effects:
595 **		none.
596 */
597 
598 /* try to guess what style of load average we have */
599 #define LA_ZERO		1	/* always return load average as zero */
600 #define LA_INT		2	/* read kmem for avenrun; interpret as long */
601 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
602 #define LA_SUBR		4	/* call getloadavg */
603 #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
604 #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
605 
606 /* do guesses based on general OS type */
607 #ifndef LA_TYPE
608 # define LA_TYPE	LA_ZERO
609 #endif
610 
611 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
612 
613 #include <nlist.h>
614 
615 #ifndef LA_AVENRUN
616 # ifdef SYSTEM5
617 #  define LA_AVENRUN	"avenrun"
618 # else
619 #  define LA_AVENRUN	"_avenrun"
620 # endif
621 #endif
622 
623 /* _PATH_UNIX should be defined in <paths.h> */
624 #ifndef _PATH_UNIX
625 # if defined(SYSTEM5)
626 #  define _PATH_UNIX	"/unix"
627 # else
628 #  define _PATH_UNIX	"/vmunix"
629 # endif
630 #endif
631 
632 struct	nlist Nl[] =
633 {
634 	{ LA_AVENRUN },
635 #define	X_AVENRUN	0
636 	{ 0 },
637 };
638 
639 #ifndef FSHIFT
640 # if defined(unixpc)
641 #  define FSHIFT	5
642 # endif
643 
644 # if defined(__alpha)
645 #  define FSHIFT	10
646 # endif
647 
648 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
649 #  define FSHIFT	8
650 # endif
651 #endif
652 
653 #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
654 #  define FSCALE	(1 << FSHIFT)
655 #endif
656 
657 getla()
658 {
659 	static int kmem = -1;
660 #if LA_TYPE == LA_INT
661 	long avenrun[3];
662 #else
663 # if LA_TYPE == LA_SHORT
664 	short avenrun[3];
665 # else
666 	double avenrun[3];
667 # endif
668 #endif
669 	extern off_t lseek();
670 	extern int errno;
671 
672 	if (kmem < 0)
673 	{
674 		kmem = open("/dev/kmem", 0, 0);
675 		if (kmem < 0)
676 		{
677 			if (tTd(3, 1))
678 				printf("getla: open(/dev/kmem): %s\n",
679 					errstring(errno));
680 			return (-1);
681 		}
682 		(void) fcntl(kmem, F_SETFD, 1);
683 		if (nlist(_PATH_UNIX, Nl) < 0)
684 		{
685 			if (tTd(3, 1))
686 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
687 					errstring(errno));
688 			return (-1);
689 		}
690 		if (Nl[X_AVENRUN].n_value == 0)
691 		{
692 			if (tTd(3, 1))
693 				printf("getla: nlist(%s, %s) ==> 0\n",
694 					_PATH_UNIX, LA_AVENRUN);
695 			return (-1);
696 		}
697 	}
698 	if (tTd(3, 20))
699 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
700 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
701 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
702 	{
703 		/* thank you Ian */
704 		if (tTd(3, 1))
705 			printf("getla: lseek or read: %s\n", errstring(errno));
706 		return (-1);
707 	}
708 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
709 	if (tTd(3, 5))
710 	{
711 		printf("getla: avenrun = %d", avenrun[0]);
712 		if (tTd(3, 15))
713 			printf(", %d, %d", avenrun[1], avenrun[2]);
714 		printf("\n");
715 	}
716 	if (tTd(3, 1))
717 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
718 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
719 #else
720 	if (tTd(3, 5))
721 	{
722 		printf("getla: avenrun = %g", avenrun[0]);
723 		if (tTd(3, 15))
724 			printf(", %g, %g", avenrun[1], avenrun[2]);
725 		printf("\n");
726 	}
727 	if (tTd(3, 1))
728 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
729 	return ((int) (avenrun[0] + 0.5));
730 #endif
731 }
732 
733 #else
734 #if LA_TYPE == LA_SUBR
735 
736 #ifdef DGUX
737 
738 #include <sys/dg_sys_info.h>
739 
740 int getla()
741 {
742 	struct dg_sys_info_load_info load_info;
743 
744 	dg_sys_info((long *)&load_info,
745 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
746 
747 	return((int) (load_info.one_minute + 0.5));
748 }
749 
750 #else
751 
752 getla()
753 {
754 	double avenrun[3];
755 
756 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
757 	{
758 		if (tTd(3, 1))
759 			perror("getla: getloadavg failed:");
760 		return (-1);
761 	}
762 	if (tTd(3, 1))
763 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
764 	return ((int) (avenrun[0] + 0.5));
765 }
766 
767 #endif /* DGUX */
768 #else
769 #if LA_TYPE == LA_MACH
770 
771 /*
772 **  This has been tested on NEXTSTEP release 2.1/3.X.
773 */
774 
775 #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
776 # include <mach/mach.h>
777 #else
778 # include <mach.h>
779 #endif
780 
781 getla()
782 {
783 	processor_set_t default_set;
784 	kern_return_t error;
785 	unsigned int info_count;
786 	struct processor_set_basic_info info;
787 	host_t host;
788 
789 	error = processor_set_default(host_self(), &default_set);
790 	if (error != KERN_SUCCESS)
791 		return -1;
792 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
793 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
794 			       &host, (processor_set_info_t)&info,
795 			       &info_count) != KERN_SUCCESS)
796 	{
797 		return -1;
798 	}
799 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
800 }
801 
802 
803 #else
804 
805 getla()
806 {
807 	if (tTd(3, 1))
808 		printf("getla: ZERO\n");
809 	return (0);
810 }
811 
812 #endif
813 #endif
814 #endif
815 /*
816 **  SHOULDQUEUE -- should this message be queued or sent?
817 **
818 **	Compares the message cost to the load average to decide.
819 **
820 **	Parameters:
821 **		pri -- the priority of the message in question.
822 **		ctime -- the message creation time.
823 **
824 **	Returns:
825 **		TRUE -- if this message should be queued up for the
826 **			time being.
827 **		FALSE -- if the load is low enough to send this message.
828 **
829 **	Side Effects:
830 **		none.
831 */
832 
833 bool
834 shouldqueue(pri, ctime)
835 	long pri;
836 	time_t ctime;
837 {
838 	if (CurrentLA < QueueLA)
839 		return (FALSE);
840 	if (CurrentLA >= RefuseLA)
841 		return (TRUE);
842 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
843 }
844 /*
845 **  REFUSECONNECTIONS -- decide if connections should be refused
846 **
847 **	Parameters:
848 **		none.
849 **
850 **	Returns:
851 **		TRUE if incoming SMTP connections should be refused
852 **			(for now).
853 **		FALSE if we should accept new work.
854 **
855 **	Side Effects:
856 **		none.
857 */
858 
859 bool
860 refuseconnections()
861 {
862 #ifdef XLA
863 	if (!xla_smtp_ok())
864 		return TRUE;
865 #endif
866 
867 	/* this is probably too simplistic */
868 	return (CurrentLA >= RefuseLA);
869 }
870 /*
871 **  SETPROCTITLE -- set process title for ps
872 **
873 **	Parameters:
874 **		fmt -- a printf style format string.
875 **		a, b, c -- possible parameters to fmt.
876 **
877 **	Returns:
878 **		none.
879 **
880 **	Side Effects:
881 **		Clobbers argv of our main procedure so ps(1) will
882 **		display the title.
883 */
884 
885 #ifdef SETPROCTITLE
886 # ifdef __hpux
887 #  include <sys/pstat.h>
888 # endif
889 # ifdef BSD4_4
890 #  include <machine/vmparam.h>
891 #  include <sys/exec.h>
892 #  ifdef __bsdi__
893 #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
894 #   define PROCTITLEPAD	'\0'
895 #  endif
896 #  ifdef PS_STRINGS
897 #   define SETPROC_STATIC static
898 #  endif
899 # endif
900 # ifndef SETPROC_STATIC
901 #  define SETPROC_STATIC
902 # endif
903 #endif
904 
905 #ifndef PROCTITLEPAD
906 # define PROCTITLEPAD	' '
907 #endif
908 
909 /*VARARGS1*/
910 #ifdef __STDC__
911 setproctitle(char *fmt, ...)
912 #else
913 setproctitle(fmt, va_alist)
914 	char *fmt;
915 	va_dcl
916 #endif
917 {
918 # ifdef SETPROCTITLE
919 	register char *p;
920 	register int i;
921 	SETPROC_STATIC char buf[MAXLINE];
922 	VA_LOCAL_DECL
923 #  ifdef __hpux
924 	union pstun pst;
925 #  endif
926 	extern char **Argv;
927 	extern char *LastArgv;
928 
929 	p = buf;
930 
931 	/* print sendmail: heading for grep */
932 	(void) strcpy(p, "sendmail: ");
933 	p += strlen(p);
934 
935 	/* print the argument string */
936 	VA_START(fmt);
937 	(void) vsprintf(p, fmt, ap);
938 	VA_END;
939 
940 	i = strlen(buf);
941 
942 #  ifdef __hpux
943 	pst.pst_command = buf;
944 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
945 #  else
946 #   ifdef PS_STRINGS
947 	PS_STRINGS->ps_nargvstr = 1;
948 	PS_STRINGS->ps_argvstr = buf;
949 #   else
950 	if (i > LastArgv - Argv[0] - 2)
951 	{
952 		i = LastArgv - Argv[0] - 2;
953 		buf[i] = '\0';
954 	}
955 	(void) strcpy(Argv[0], buf);
956 	p = &Argv[0][i];
957 	while (p < LastArgv)
958 		*p++ = PROCTITLEPAD;
959 #   endif
960 #  endif
961 # endif /* SETPROCTITLE */
962 }
963 /*
964 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
965 **
966 **	Parameters:
967 **		none.
968 **
969 **	Returns:
970 **		none.
971 **
972 **	Side Effects:
973 **		Picks up extant zombies.
974 */
975 
976 void
977 reapchild()
978 {
979 	int olderrno = errno;
980 # ifdef HASWAITPID
981 	auto int status;
982 	int count;
983 	int pid;
984 
985 	count = 0;
986 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
987 	{
988 		if (count++ > 1000)
989 		{
990 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
991 				pid, status);
992 			break;
993 		}
994 	}
995 # else
996 # ifdef WNOHANG
997 	union wait status;
998 
999 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
1000 		continue;
1001 # else /* WNOHANG */
1002 	auto int status;
1003 
1004 	while (wait(&status) > 0)
1005 		continue;
1006 # endif /* WNOHANG */
1007 # endif
1008 # ifdef SYS5SIGNALS
1009 	(void) setsignal(SIGCHLD, reapchild);
1010 # endif
1011 	errno = olderrno;
1012 }
1013 /*
1014 **  UNSETENV -- remove a variable from the environment
1015 **
1016 **	Not needed on newer systems.
1017 **
1018 **	Parameters:
1019 **		name -- the string name of the environment variable to be
1020 **			deleted from the current environment.
1021 **
1022 **	Returns:
1023 **		none.
1024 **
1025 **	Globals:
1026 **		environ -- a pointer to the current environment.
1027 **
1028 **	Side Effects:
1029 **		Modifies environ.
1030 */
1031 
1032 #ifndef HASUNSETENV
1033 
1034 void
1035 unsetenv(name)
1036 	char *name;
1037 {
1038 	extern char **environ;
1039 	register char **pp;
1040 	int len = strlen(name);
1041 
1042 	for (pp = environ; *pp != NULL; pp++)
1043 	{
1044 		if (strncmp(name, *pp, len) == 0 &&
1045 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1046 			break;
1047 	}
1048 
1049 	for (; *pp != NULL; pp++)
1050 		*pp = pp[1];
1051 }
1052 
1053 #endif
1054 /*
1055 **  GETDTABLESIZE -- return number of file descriptors
1056 **
1057 **	Only on non-BSD systems
1058 **
1059 **	Parameters:
1060 **		none
1061 **
1062 **	Returns:
1063 **		size of file descriptor table
1064 **
1065 **	Side Effects:
1066 **		none
1067 */
1068 
1069 #ifdef SOLARIS
1070 # include <sys/resource.h>
1071 #endif
1072 
1073 int
1074 getdtsize()
1075 {
1076 #ifdef RLIMIT_NOFILE
1077 	struct rlimit rl;
1078 
1079 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1080 		return rl.rlim_cur;
1081 #endif
1082 
1083 # ifdef HASGETDTABLESIZE
1084 	return getdtablesize();
1085 # else
1086 #  ifdef _SC_OPEN_MAX
1087 	return sysconf(_SC_OPEN_MAX);
1088 #  else
1089 	return NOFILE;
1090 #  endif
1091 # endif
1092 }
1093 /*
1094 **  UNAME -- get the UUCP name of this system.
1095 */
1096 
1097 #ifndef HASUNAME
1098 
1099 int
1100 uname(name)
1101 	struct utsname *name;
1102 {
1103 	FILE *file;
1104 	char *n;
1105 
1106 	name->nodename[0] = '\0';
1107 
1108 	/* try /etc/whoami -- one line with the node name */
1109 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1110 	{
1111 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1112 		(void) fclose(file);
1113 		n = strchr(name->nodename, '\n');
1114 		if (n != NULL)
1115 			*n = '\0';
1116 		if (name->nodename[0] != '\0')
1117 			return (0);
1118 	}
1119 
1120 	/* try /usr/include/whoami.h -- has a #define somewhere */
1121 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1122 	{
1123 		char buf[MAXLINE];
1124 
1125 		while (fgets(buf, MAXLINE, file) != NULL)
1126 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1127 					NODE_LENGTH, name->nodename) > 0)
1128 				break;
1129 		(void) fclose(file);
1130 		if (name->nodename[0] != '\0')
1131 			return (0);
1132 	}
1133 
1134 #ifdef TRUST_POPEN
1135 	/*
1136 	**  Popen is known to have security holes.
1137 	*/
1138 
1139 	/* try uuname -l to return local name */
1140 	if ((file = popen("uuname -l", "r")) != NULL)
1141 	{
1142 		(void) fgets(name, NODE_LENGTH + 1, file);
1143 		(void) pclose(file);
1144 		n = strchr(name, '\n');
1145 		if (n != NULL)
1146 			*n = '\0';
1147 		if (name->nodename[0] != '\0')
1148 			return (0);
1149 	}
1150 #endif
1151 
1152 	return (-1);
1153 }
1154 #endif /* HASUNAME */
1155 /*
1156 **  INITGROUPS -- initialize groups
1157 **
1158 **	Stub implementation for System V style systems
1159 */
1160 
1161 #ifndef HASINITGROUPS
1162 
1163 initgroups(name, basegid)
1164 	char *name;
1165 	int basegid;
1166 {
1167 	return 0;
1168 }
1169 
1170 #endif
1171 /*
1172 **  SETSID -- set session id (for non-POSIX systems)
1173 */
1174 
1175 #ifndef HASSETSID
1176 
1177 pid_t
1178 setsid __P ((void))
1179 {
1180 #ifdef TIOCNOTTY
1181 	int fd;
1182 
1183 	fd = open("/dev/tty", 2);
1184 	if (fd >= 0)
1185 	{
1186 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1187 		(void) close(fd);
1188 	}
1189 #endif /* TIOCNOTTY */
1190 # ifdef SYS5SETPGRP
1191 	return setpgrp();
1192 # else
1193 	return setpgid(0, getpid());
1194 # endif
1195 }
1196 
1197 #endif
1198 /*
1199 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1200 **
1201 **	Data General DG/UX version of inet_addr returns a struct in_addr
1202 **	instead of a long.  This patches things.
1203 */
1204 
1205 #ifdef DGUX
1206 
1207 #undef inet_addr
1208 
1209 long
1210 dgux_inet_addr(host)
1211 	char *host;
1212 {
1213 	struct in_addr haddr;
1214 
1215 	haddr = inet_addr(host);
1216 	return haddr.s_addr;
1217 }
1218 
1219 #endif
1220 /*
1221 **  GETOPT -- for old systems or systems with bogus implementations
1222 */
1223 
1224 #ifdef NEEDGETOPT
1225 
1226 /*
1227  * Copyright (c) 1985 Regents of the University of California.
1228  * All rights reserved.  The Berkeley software License Agreement
1229  * specifies the terms and conditions for redistribution.
1230  */
1231 
1232 
1233 /*
1234 ** this version hacked to add `atend' flag to allow state machine
1235 ** to reset if invoked by the program to scan args for a 2nd time
1236 */
1237 
1238 #if defined(LIBC_SCCS) && !defined(lint)
1239 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1240 #endif /* LIBC_SCCS and not lint */
1241 
1242 #include <stdio.h>
1243 
1244 /*
1245  * get option letter from argument vector
1246  */
1247 #ifdef _CONVEX_SOURCE
1248 extern int	optind, opterr;
1249 #else
1250 int	opterr = 1;		/* if error message should be printed */
1251 int	optind = 1;		/* index into parent argv vector */
1252 #endif
1253 int	optopt;			/* character checked for validity */
1254 char	*optarg;		/* argument associated with option */
1255 
1256 #define BADCH	(int)'?'
1257 #define EMSG	""
1258 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1259 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1260 
1261 getopt(nargc,nargv,ostr)
1262 	int		nargc;
1263 	char *const	*nargv;
1264 	const char	*ostr;
1265 {
1266 	static char	*place = EMSG;	/* option letter processing */
1267 	static char	atend = 0;
1268 	register char	*oli;		/* option letter list index */
1269 
1270 	if (atend) {
1271 		atend = 0;
1272 		place = EMSG;
1273 	}
1274 	if(!*place) {			/* update scanning pointer */
1275 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1276 			atend++;
1277 			return(EOF);
1278 		}
1279 		if (*place == '-') {	/* found "--" */
1280 			++optind;
1281 			atend++;
1282 			return(EOF);
1283 		}
1284 	}				/* option letter okay? */
1285 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1286 		if (!*place) ++optind;
1287 		tell(": illegal option -- ");
1288 	}
1289 	if (*++oli != ':') {		/* don't need argument */
1290 		optarg = NULL;
1291 		if (!*place) ++optind;
1292 	}
1293 	else {				/* need an argument */
1294 		if (*place) optarg = place;	/* no white space */
1295 		else if (nargc <= ++optind) {	/* no arg */
1296 			place = EMSG;
1297 			tell(": option requires an argument -- ");
1298 		}
1299 	 	else optarg = nargv[optind];	/* white space */
1300 		place = EMSG;
1301 		++optind;
1302 	}
1303 	return(optopt);			/* dump back option letter */
1304 }
1305 
1306 #endif
1307 /*
1308 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1309 */
1310 
1311 #ifdef NEEDVPRINTF
1312 
1313 #define MAXARG	16
1314 
1315 vfprintf(fp, fmt, ap)
1316 	FILE *	fp;
1317 	char *	fmt;
1318 	char **	ap;
1319 {
1320 	char *	bp[MAXARG];
1321 	int	i = 0;
1322 
1323 	while (*ap && i < MAXARG)
1324 		bp[i++] = *ap++;
1325 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1326 			 bp[4], bp[5], bp[6], bp[7],
1327 			 bp[8], bp[9], bp[10], bp[11],
1328 			 bp[12], bp[13], bp[14], bp[15]);
1329 }
1330 
1331 vsprintf(s, fmt, ap)
1332 	char *	s;
1333 	char *	fmt;
1334 	char **	ap;
1335 {
1336 	char *	bp[MAXARG];
1337 	int	i = 0;
1338 
1339 	while (*ap && i < MAXARG)
1340 		bp[i++] = *ap++;
1341 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1342 			bp[4], bp[5], bp[6], bp[7],
1343 			bp[8], bp[9], bp[10], bp[11],
1344 			bp[12], bp[13], bp[14], bp[15]);
1345 }
1346 
1347 #endif
1348 /*
1349 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
1350 **
1351 **	Parameters:
1352 **		shell -- the user's shell from /etc/passwd
1353 **
1354 **	Returns:
1355 **		TRUE -- if it is ok to use this for unrestricted access.
1356 **		FALSE -- if the shell is restricted.
1357 */
1358 
1359 #if !HASGETUSERSHELL
1360 
1361 # ifndef _PATH_SHELLS
1362 #  define _PATH_SHELLS	"/etc/shells"
1363 # endif
1364 
1365 char	*DefaultUserShells[] =
1366 {
1367 	"/bin/sh",
1368 	"/usr/bin/sh",
1369 	"/bin/csh",
1370 	"/usr/bin/csh",
1371 #ifdef __hpux
1372 	"/bin/rsh",
1373 	"/bin/ksh",
1374 	"/bin/rksh",
1375 	"/bin/pam",
1376 	"/usr/bin/keysh",
1377 	"/bin/posix/sh",
1378 #endif
1379 	NULL
1380 };
1381 
1382 #endif
1383 
1384 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
1385 
1386 bool
1387 usershellok(shell)
1388 	char *shell;
1389 {
1390 #if HASGETUSERSHELL
1391 	register char *p;
1392 	extern char *getusershell();
1393 
1394 	setusershell();
1395 	while ((p = getusershell()) != NULL)
1396 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
1397 			break;
1398 	endusershell();
1399 	return p != NULL;
1400 #else
1401 	register FILE *shellf;
1402 	char buf[MAXLINE];
1403 
1404 	shellf = fopen(_PATH_SHELLS, "r");
1405 	if (shellf == NULL)
1406 	{
1407 		/* no /etc/shells; see if it is one of the std shells */
1408 		char **d;
1409 
1410 		for (d = DefaultUserShells; *d != NULL; d++)
1411 		{
1412 			if (strcmp(shell, *d) == 0)
1413 				return TRUE;
1414 		}
1415 		return FALSE;
1416 	}
1417 
1418 	while (fgets(buf, sizeof buf, shellf) != NULL)
1419 	{
1420 		register char *p, *q;
1421 
1422 		p = buf;
1423 		while (*p != '\0' && *p != '#' && *p != '/')
1424 			p++;
1425 		if (*p == '#' || *p == '\0')
1426 			continue;
1427 		q = p;
1428 		while (*p != '\0' && *p != '#' && !isspace(*p))
1429 			p++;
1430 		*p = '\0';
1431 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
1432 		{
1433 			fclose(shellf);
1434 			return TRUE;
1435 		}
1436 	}
1437 	fclose(shellf);
1438 	return FALSE;
1439 #endif
1440 }
1441 /*
1442 **  FREESPACE -- see how much free space is on the queue filesystem
1443 **
1444 **	Only implemented if you have statfs.
1445 **
1446 **	Parameters:
1447 **		dir -- the directory in question.
1448 **		bsize -- a variable into which the filesystem
1449 **			block size is stored.
1450 **
1451 **	Returns:
1452 **		The number of bytes free on the queue filesystem.
1453 **		-1 if the statfs call fails.
1454 **
1455 **	Side effects:
1456 **		Puts the filesystem block size into bsize.
1457 */
1458 
1459 /* statfs types */
1460 #define SFS_NONE	0	/* no statfs implementation */
1461 #define SFS_USTAT	1	/* use ustat */
1462 #define SFS_4ARGS	2	/* use four-argument statfs call */
1463 #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
1464 #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
1465 #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
1466 
1467 #ifndef SFS_TYPE
1468 # define SFS_TYPE	SFS_NONE
1469 #endif
1470 
1471 #if SFS_TYPE == SFS_USTAT
1472 # include <ustat.h>
1473 #endif
1474 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
1475 # include <sys/statfs.h>
1476 #endif
1477 #if SFS_TYPE == SFS_VFS
1478 # include <sys/vfs.h>
1479 #endif
1480 #if SFS_TYPE == SFS_MOUNT
1481 # include <sys/mount.h>
1482 #endif
1483 
1484 long
1485 freespace(dir, bsize)
1486 	char *dir;
1487 	long *bsize;
1488 {
1489 #if SFS_TYPE != SFS_NONE
1490 # if SFS_TYPE == SFS_USTAT
1491 	struct ustat fs;
1492 	struct stat statbuf;
1493 #  define FSBLOCKSIZE	DEV_BSIZE
1494 #  define f_bavail	f_tfree
1495 # else
1496 #  if defined(ultrix)
1497 	struct fs_data fs;
1498 #   define f_bavail	fd_bfreen
1499 #   define FSBLOCKSIZE	fs.fd_bsize
1500 #  else
1501 	struct statfs fs;
1502 #   define FSBLOCKSIZE	fs.f_bsize
1503 #   if defined(_SCO_unix_) || defined(IRIX) || defined(apollo)
1504 #    define f_bavail f_bfree
1505 #   endif
1506 #  endif
1507 # endif
1508 	extern int errno;
1509 
1510 # if SFS_TYPE == SFS_USTAT
1511 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1512 # else
1513 #  if SFS_TYPE == SFS_4ARGS
1514 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
1515 #  else
1516 #   if defined(ultrix)
1517 	if (statfs(dir, &fs) > 0)
1518 #   else
1519 	if (statfs(dir, &fs) == 0)
1520 #   endif
1521 #  endif
1522 # endif
1523 	{
1524 		if (bsize != NULL)
1525 			*bsize = FSBLOCKSIZE;
1526 		return (fs.f_bavail);
1527 	}
1528 #endif
1529 	return (-1);
1530 }
1531 /*
1532 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1533 **
1534 **	Only implemented if you have statfs.
1535 **
1536 **	Parameters:
1537 **		msize -- the size to check against.  If zero, we don't yet
1538 **		know how big the message will be, so just check for
1539 **		a "reasonable" amount.
1540 **
1541 **	Returns:
1542 **		TRUE if there is enough space.
1543 **		FALSE otherwise.
1544 */
1545 
1546 bool
1547 enoughspace(msize)
1548 	long msize;
1549 {
1550 	long bfree, bsize;
1551 
1552 	if (MinBlocksFree <= 0 && msize <= 0)
1553 	{
1554 		if (tTd(4, 80))
1555 			printf("enoughspace: no threshold\n");
1556 		return TRUE;
1557 	}
1558 
1559 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
1560 	{
1561 		if (tTd(4, 80))
1562 			printf("enoughspace: bavail=%ld, need=%ld\n",
1563 				bfree, msize);
1564 
1565 		/* convert msize to block count */
1566 		msize = msize / bsize + 1;
1567 		if (MinBlocksFree >= 0)
1568 			msize += MinBlocksFree;
1569 
1570 		if (bfree < msize)
1571 		{
1572 #ifdef LOG
1573 			if (LogLevel > 0)
1574 				syslog(LOG_ALERT,
1575 					"%s: low on space (have %ld, %s needs %ld in %s)",
1576 					CurEnv->e_id, bfree,
1577 					CurHostName, msize, QueueDir);
1578 #endif
1579 			return FALSE;
1580 		}
1581 	}
1582 	else if (tTd(4, 80))
1583 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1584 			MinBlocksFree, msize, errstring(errno));
1585 	return TRUE;
1586 }
1587 /*
1588 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1589 **
1590 **	This looks at an errno value and tells if this is likely to
1591 **	go away if retried later.
1592 **
1593 **	Parameters:
1594 **		err -- the errno code to classify.
1595 **
1596 **	Returns:
1597 **		TRUE if this is probably transient.
1598 **		FALSE otherwise.
1599 */
1600 
1601 bool
1602 transienterror(err)
1603 	int err;
1604 {
1605 	switch (err)
1606 	{
1607 	  case EIO:			/* I/O error */
1608 	  case ENXIO:			/* Device not configured */
1609 	  case EAGAIN:			/* Resource temporarily unavailable */
1610 	  case ENOMEM:			/* Cannot allocate memory */
1611 	  case ENODEV:			/* Operation not supported by device */
1612 	  case ENFILE:			/* Too many open files in system */
1613 	  case EMFILE:			/* Too many open files */
1614 	  case ENOSPC:			/* No space left on device */
1615 #ifdef ETIMEDOUT
1616 	  case ETIMEDOUT:		/* Connection timed out */
1617 #endif
1618 #ifdef ESTALE
1619 	  case ESTALE:			/* Stale NFS file handle */
1620 #endif
1621 #ifdef ENETDOWN
1622 	  case ENETDOWN:		/* Network is down */
1623 #endif
1624 #ifdef ENETUNREACH
1625 	  case ENETUNREACH:		/* Network is unreachable */
1626 #endif
1627 #ifdef ENETRESET
1628 	  case ENETRESET:		/* Network dropped connection on reset */
1629 #endif
1630 #ifdef ECONNABORTED
1631 	  case ECONNABORTED:		/* Software caused connection abort */
1632 #endif
1633 #ifdef ECONNRESET
1634 	  case ECONNRESET:		/* Connection reset by peer */
1635 #endif
1636 #ifdef ENOBUFS
1637 	  case ENOBUFS:			/* No buffer space available */
1638 #endif
1639 #ifdef ESHUTDOWN
1640 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1641 #endif
1642 #ifdef ECONNREFUSED
1643 	  case ECONNREFUSED:		/* Connection refused */
1644 #endif
1645 #ifdef EHOSTDOWN
1646 	  case EHOSTDOWN:		/* Host is down */
1647 #endif
1648 #ifdef EHOSTUNREACH
1649 	  case EHOSTUNREACH:		/* No route to host */
1650 #endif
1651 #ifdef EDQUOT
1652 	  case EDQUOT:			/* Disc quota exceeded */
1653 #endif
1654 #ifdef EPROCLIM
1655 	  case EPROCLIM:		/* Too many processes */
1656 #endif
1657 #ifdef EUSERS
1658 	  case EUSERS:			/* Too many users */
1659 #endif
1660 #ifdef EDEADLK
1661 	  case EDEADLK:			/* Resource deadlock avoided */
1662 #endif
1663 #ifdef EISCONN
1664 	  case EISCONN:			/* Socket already connected */
1665 #endif
1666 #ifdef EINPROGRESS
1667 	  case EINPROGRESS:		/* Operation now in progress */
1668 #endif
1669 #ifdef EALREADY
1670 	  case EALREADY:		/* Operation already in progress */
1671 #endif
1672 #ifdef EADDRINUSE
1673 	  case EADDRINUSE:		/* Address already in use */
1674 #endif
1675 #ifdef EADDRNOTAVAIL
1676 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1677 #endif
1678 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1679 	  case ENOSR:			/* Out of streams resources */
1680 #endif
1681 		return TRUE;
1682 	}
1683 
1684 	/* nope, must be permanent */
1685 	return FALSE;
1686 }
1687 /*
1688 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1689 **
1690 **	Parameters:
1691 **		fd -- the file descriptor of the file.
1692 **		filename -- the file name (for error messages).
1693 **		ext -- the filename extension.
1694 **		type -- type of the lock.  Bits can be:
1695 **			LOCK_EX -- exclusive lock.
1696 **			LOCK_NB -- non-blocking.
1697 **
1698 **	Returns:
1699 **		TRUE if the lock was acquired.
1700 **		FALSE otherwise.
1701 */
1702 
1703 bool
1704 lockfile(fd, filename, ext, type)
1705 	int fd;
1706 	char *filename;
1707 	char *ext;
1708 	int type;
1709 {
1710 # if !HASFLOCK
1711 	int action;
1712 	struct flock lfd;
1713 
1714 	if (ext == NULL)
1715 		ext = "";
1716 
1717 	bzero(&lfd, sizeof lfd);
1718 	if (bitset(LOCK_UN, type))
1719 		lfd.l_type = F_UNLCK;
1720 	else if (bitset(LOCK_EX, type))
1721 		lfd.l_type = F_WRLCK;
1722 	else
1723 		lfd.l_type = F_RDLCK;
1724 
1725 	if (bitset(LOCK_NB, type))
1726 		action = F_SETLK;
1727 	else
1728 		action = F_SETLKW;
1729 
1730 	if (tTd(55, 60))
1731 		printf("lockfile(%s%s, action=%d, type=%d): ",
1732 			filename, ext, action, lfd.l_type);
1733 
1734 	if (fcntl(fd, action, &lfd) >= 0)
1735 	{
1736 		if (tTd(55, 60))
1737 			printf("SUCCESS\n");
1738 		return TRUE;
1739 	}
1740 
1741 	if (tTd(55, 60))
1742 		printf("(%s) ", errstring(errno));
1743 
1744 	/*
1745 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
1746 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
1747 	**  as type "tmp" (that is, served from swap space), the
1748 	**  previous fcntl will fail with "Invalid argument" errors.
1749 	**  Since this is fairly common during testing, we will assume
1750 	**  that this indicates that the lock is successfully grabbed.
1751 	*/
1752 
1753 	if (errno == EINVAL)
1754 	{
1755 		if (tTd(55, 60))
1756 			printf("SUCCESS\n");
1757 		return TRUE;
1758 	}
1759 
1760 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1761 	{
1762 		int omode = -1;
1763 #  ifdef F_GETFL
1764 		int oerrno = errno;
1765 
1766 		(void) fcntl(fd, F_GETFL, &omode);
1767 		errno = oerrno;
1768 #  endif
1769 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1770 			filename, ext, fd, type, omode, geteuid());
1771 	}
1772 # else
1773 	if (ext == NULL)
1774 		ext = "";
1775 
1776 	if (tTd(55, 60))
1777 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
1778 
1779 	if (flock(fd, type) >= 0)
1780 	{
1781 		if (tTd(55, 60))
1782 			printf("SUCCESS\n");
1783 		return TRUE;
1784 	}
1785 
1786 	if (tTd(55, 60))
1787 		printf("(%s) ", errstring(errno));
1788 
1789 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1790 	{
1791 		int omode = -1;
1792 #  ifdef F_GETFL
1793 		int oerrno = errno;
1794 
1795 		(void) fcntl(fd, F_GETFL, &omode);
1796 		errno = oerrno;
1797 #  endif
1798 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1799 			filename, ext, fd, type, omode, geteuid());
1800 	}
1801 # endif
1802 	if (tTd(55, 60))
1803 		printf("FAIL\n");
1804 	return FALSE;
1805 }
1806 /*
1807 **  GETCFNAME -- return the name of the .cf file.
1808 **
1809 **	Some systems (e.g., NeXT) determine this dynamically.
1810 */
1811 
1812 char *
1813 getcfname()
1814 {
1815 	if (ConfFile != NULL)
1816 		return ConfFile;
1817 #ifdef NETINFO
1818 	{
1819 		extern char *ni_propval();
1820 		char *cflocation;
1821 
1822 		cflocation = ni_propval("/locations/sendmail", "sendmail.cf");
1823 		if (cflocation != NULL)
1824 			return cflocation;
1825 	}
1826 #endif
1827 	return _PATH_SENDMAILCF;
1828 }
1829 /*
1830 **  SETVENDOR -- process vendor code from V configuration line
1831 **
1832 **	Parameters:
1833 **		vendor -- string representation of vendor.
1834 **
1835 **	Returns:
1836 **		TRUE -- if ok.
1837 **		FALSE -- if vendor code could not be processed.
1838 **
1839 **	Side Effects:
1840 **		It is reasonable to set mode flags here to tweak
1841 **		processing in other parts of the code if necessary.
1842 **		For example, if you are a vendor that uses $%y to
1843 **		indicate YP lookups, you could enable that here.
1844 */
1845 
1846 bool
1847 setvendor(vendor)
1848 	char *vendor;
1849 {
1850 	if (strcasecmp(vendor, "Berkeley") == 0)
1851 		return TRUE;
1852 
1853 	/* add vendor extensions here */
1854 
1855 	return FALSE;
1856 }
1857 /*
1858 **  STRTOL -- convert string to long integer
1859 **
1860 **	For systems that don't have it in the C library.
1861 */
1862 
1863 #ifdef NEEDSTRTOL
1864 
1865 long
1866 strtol(p, ep, b)
1867 	char *p;
1868 	char **ep;
1869 	int b;
1870 {
1871 	long l = 0;
1872 	char c;
1873 	char maxd;
1874 	int neg = 1;
1875 
1876 	maxd = (b > 10) ? '9' : b + '0';
1877 
1878 	if (p && *p == '-') {
1879 		neg = -1;
1880 		p++;
1881 	}
1882 	while (p && (c = *p)) {
1883 		if (c >= '0' && c <= maxd) {
1884 			l = l*b + *p++ - '0';
1885 			continue;
1886 		}
1887 		if (c >= 'A' && c <= 'Z')
1888 			c -= 'A' + 'a';
1889 		c = c - 'a' + 10;
1890 		if (b > c) {
1891 			l = l*b + c;
1892 			p++;
1893 			continue;
1894 		}
1895 		break;
1896 	}
1897 	l *= neg;
1898 	if (ep)
1899 		*ep = p;
1900 	return l;
1901 }
1902 
1903 #endif
1904 /*
1905 **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
1906 **
1907 **	Solaris versions prior through 2.3 don't properly deliver a
1908 **	canonical h_name field.  This tries to work around it.
1909 */
1910 
1911 #ifdef SOLARIS
1912 
1913 struct hostent *
1914 solaris_gethostbyname(name)
1915 	const char *name;
1916 {
1917 # ifdef SOLARIS_2_3
1918 	static struct hostent hp;
1919 	static char buf[1000];
1920 	extern struct hostent *_switch_gethostbyname_r();
1921 
1922 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
1923 # else
1924 	extern struct hostent *__switch_gethostbyname();
1925 
1926 	return __switch_gethostbyname(name);
1927 # endif
1928 }
1929 
1930 struct hostent *
1931 solaris_gethostbyaddr(addr, len, type)
1932 	const char *addr;
1933 	int len;
1934 	int type;
1935 {
1936 # ifdef SOLARIS_2_3
1937 	static struct hostent hp;
1938 	static char buf[1000];
1939 	extern struct hostent *_switch_gethostbyaddr_r();
1940 
1941 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
1942 # else
1943 	extern struct hostent *__switch_gethostbyaddr();
1944 
1945 	return __switch_gethostbyaddr(addr, len, type);
1946 # endif
1947 }
1948 
1949 #endif
1950 /*
1951 **  NI_PROPVAL -- netinfo property value lookup routine
1952 **
1953 **	Parameters:
1954 **		directory -- the Netinfo directory name.
1955 **		propname -- the Netinfo property name.
1956 **
1957 **	Returns:
1958 **		NULL -- if:
1959 **			1. the directory is not found
1960 **			2. the property name is not found
1961 **			3. the property contains multiple values
1962 **			4. some error occured
1963 **		else -- the location of the config file.
1964 **
1965 **	Notes:
1966 **      	Caller should free the return value of ni_proval
1967 */
1968 
1969 #ifdef NETINFO
1970 
1971 # include <netinfo/ni.h>
1972 
1973 # define LOCAL_NETINFO_DOMAIN    "."
1974 # define PARENT_NETINFO_DOMAIN   ".."
1975 # define MAX_NI_LEVELS           256
1976 
1977 char *
1978 ni_propval(directory, propname)
1979 	char *directory;
1980 	char *propname;
1981 {
1982 	char *propval = NULL;
1983 	int i;
1984 	void *ni = NULL;
1985 	void *lastni = NULL;
1986 	ni_status nis;
1987 	ni_id nid;
1988 	ni_namelist ninl;
1989 
1990 	/*
1991 	**  If the passed directory and property name are found
1992 	**  in one of netinfo domains we need to search (starting
1993 	**  from the local domain moving all the way back to the
1994 	**  root domain) set propval to the property's value
1995 	**  and return it.
1996 	*/
1997 
1998 	for (i = 0; i < MAX_NI_LEVELS; ++i)
1999 	{
2000 		if (i == 0)
2001 		{
2002 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
2003 		}
2004 		else
2005 		{
2006 			if (lastni != NULL)
2007 				ni_free(lastni);
2008 			lastni = ni;
2009 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
2010 		}
2011 
2012 		/*
2013 		**  Don't bother if we didn't get a handle on a
2014 		**  proper domain.  This is not necessarily an error.
2015 		**  We would get a positive ni_status if, for instance
2016 		**  we never found the directory or property and tried
2017 		**  to open the parent of the root domain!
2018 		*/
2019 
2020 		if (nis != 0)
2021 			break;
2022 
2023 		/*
2024 		**  Find the path to the server information.
2025 		*/
2026 
2027 		if (ni_pathsearch(ni, &nid, directory) != 0)
2028 			continue;
2029 
2030 		/*
2031 		**  Find "host" information.
2032 		*/
2033 
2034 		if (ni_lookupprop(ni, &nid, propname, &ninl) != 0)
2035 			continue;
2036 
2037 		/*
2038 		**  If there's only one name in
2039 		**  the list, assume we've got
2040 		**  what we want.
2041 		*/
2042 
2043 		if (ninl.ni_namelist_len == 1)
2044 		{
2045 			propval = ni_name_dup(ninl.ni_namelist_val[0]);
2046 			break;
2047 		}
2048 	}
2049 
2050 	/*
2051 	**  Clean up.
2052 	*/
2053 
2054 	if (ni != NULL)
2055 		ni_free(ni);
2056 	if (lastni != NULL && ni != lastni)
2057 		ni_free(lastni);
2058 
2059 	return propval;
2060 }
2061 
2062 #endif /* NETINFO */
2063 /*
2064 **  HARD_SYSLOG -- call syslog repeatedly until it works
2065 **
2066 **	Needed on HP-UX, which apparently doesn't guarantee that
2067 **	syslog succeeds during interrupt handlers.
2068 */
2069 
2070 #ifdef __hpux
2071 
2072 # define MAXSYSLOGTRIES	100
2073 # undef syslog
2074 
2075 # ifdef __STDC__
2076 hard_syslog(int pri, char *msg, ...)
2077 # else
2078 hard_syslog(pri, msg, va_alist)
2079 	int pri;
2080 	char *msg;
2081 	va_dcl
2082 # endif
2083 {
2084 	int i;
2085 	char buf[SYSLOG_BUFSIZE * 2];
2086 	VA_LOCAL_DECL;
2087 
2088 	VA_START(msg);
2089 	vsprintf(buf, msg, ap);
2090 	VA_END;
2091 
2092 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
2093 		continue;
2094 }
2095 
2096 #endif
2097