xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision cdfb06ef)
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.58 (Berkeley) 12/27/93";
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 int	opterr = 1,		/* if error message should be printed */
1248 	optind = 1,		/* index into parent argv vector */
1249 	optopt;			/* character checked for validity */
1250 char	*optarg;		/* argument associated with option */
1251 
1252 #define BADCH	(int)'?'
1253 #define EMSG	""
1254 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1255 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1256 
1257 getopt(nargc,nargv,ostr)
1258 	int		nargc;
1259 	char *const	*nargv;
1260 	const char	*ostr;
1261 {
1262 	static char	*place = EMSG;	/* option letter processing */
1263 	static char	atend = 0;
1264 	register char	*oli;		/* option letter list index */
1265 
1266 	if (atend) {
1267 		atend = 0;
1268 		place = EMSG;
1269 	}
1270 	if(!*place) {			/* update scanning pointer */
1271 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1272 			atend++;
1273 			return(EOF);
1274 		}
1275 		if (*place == '-') {	/* found "--" */
1276 			++optind;
1277 			atend++;
1278 			return(EOF);
1279 		}
1280 	}				/* option letter okay? */
1281 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1282 		if (!*place) ++optind;
1283 		tell(": illegal option -- ");
1284 	}
1285 	if (*++oli != ':') {		/* don't need argument */
1286 		optarg = NULL;
1287 		if (!*place) ++optind;
1288 	}
1289 	else {				/* need an argument */
1290 		if (*place) optarg = place;	/* no white space */
1291 		else if (nargc <= ++optind) {	/* no arg */
1292 			place = EMSG;
1293 			tell(": option requires an argument -- ");
1294 		}
1295 	 	else optarg = nargv[optind];	/* white space */
1296 		place = EMSG;
1297 		++optind;
1298 	}
1299 	return(optopt);			/* dump back option letter */
1300 }
1301 
1302 #endif
1303 /*
1304 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1305 */
1306 
1307 #ifdef NEEDVPRINTF
1308 
1309 #define MAXARG	16
1310 
1311 vfprintf(fp, fmt, ap)
1312 	FILE *	fp;
1313 	char *	fmt;
1314 	char **	ap;
1315 {
1316 	char *	bp[MAXARG];
1317 	int	i = 0;
1318 
1319 	while (*ap && i < MAXARG)
1320 		bp[i++] = *ap++;
1321 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1322 			 bp[4], bp[5], bp[6], bp[7],
1323 			 bp[8], bp[9], bp[10], bp[11],
1324 			 bp[12], bp[13], bp[14], bp[15]);
1325 }
1326 
1327 vsprintf(s, fmt, ap)
1328 	char *	s;
1329 	char *	fmt;
1330 	char **	ap;
1331 {
1332 	char *	bp[MAXARG];
1333 	int	i = 0;
1334 
1335 	while (*ap && i < MAXARG)
1336 		bp[i++] = *ap++;
1337 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1338 			bp[4], bp[5], bp[6], bp[7],
1339 			bp[8], bp[9], bp[10], bp[11],
1340 			bp[12], bp[13], bp[14], bp[15]);
1341 }
1342 
1343 #endif
1344 /*
1345 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
1346 **
1347 **	Parameters:
1348 **		shell -- the user's shell from /etc/passwd
1349 **
1350 **	Returns:
1351 **		TRUE -- if it is ok to use this for unrestricted access.
1352 **		FALSE -- if the shell is restricted.
1353 */
1354 
1355 #ifndef _PATH_SHELLS
1356 # define _PATH_SHELLS	"/etc/shells"
1357 #endif
1358 #ifndef _PATH_BSHELL
1359 # define _PATH_BSHELL	"/bin/sh"
1360 #endif
1361 #ifndef _PATH_CSHELL
1362 # define _PATH_CSHELL	"/bin/csh"
1363 #endif
1364 
1365 bool
1366 usershellok(shell)
1367 	char *shell;
1368 {
1369 #if HASGETUSERSHELL
1370 	register char *p;
1371 	extern char *getusershell();
1372 
1373 	setusershell();
1374 	while ((p = getusershell()) != NULL)
1375 		if (strcmp(p, shell) == 0)
1376 			break;
1377 	endusershell();
1378 	return p != NULL;
1379 #else
1380 	register FILE *shellf;
1381 	char buf[MAXLINE];
1382 
1383 	shellf = fopen(_PATH_SHELLS, "r");
1384 	if (shellf == NULL)
1385 	{
1386 		/* no /etc/shells; see if it is one of the std shells */
1387 		return (strcmp(shell, _PATH_CSHELL) == 0 ||
1388 			strcmp(shell, _PATH_BSHELL) == 0);
1389 	}
1390 
1391 	while (fgets(buf, sizeof buf, shellf) != NULL)
1392 	{
1393 		register char *p, *q;
1394 
1395 		p = buf;
1396 		while (*p != '\0' && *p != '#' && *p != '/')
1397 			p++;
1398 		if (*p == '#' || *p == '\0')
1399 			continue;
1400 		q = p;
1401 		while (*p != '\0' && *p != '#' && !isspace(*p))
1402 			p++;
1403 		*p = '\0';
1404 		if (strcmp(shell, q) == 0)
1405 		{
1406 			fclose(shellf);
1407 			return TRUE;
1408 		}
1409 	}
1410 	fclose(shellf);
1411 	return FALSE;
1412 #endif
1413 }
1414 /*
1415 **  FREESPACE -- see how much free space is on the queue filesystem
1416 **
1417 **	Only implemented if you have statfs.
1418 **
1419 **	Parameters:
1420 **		dir -- the directory in question.
1421 **		bsize -- a variable into which the filesystem
1422 **			block size is stored.
1423 **
1424 **	Returns:
1425 **		The number of bytes free on the queue filesystem.
1426 **		-1 if the statfs call fails.
1427 **
1428 **	Side effects:
1429 **		Puts the filesystem block size into bsize.
1430 */
1431 
1432 #ifdef HASSTATFS
1433 # undef HASUSTAT
1434 #endif
1435 
1436 #if defined(HASUSTAT)
1437 # include <ustat.h>
1438 #endif
1439 
1440 #ifdef HASSTATFS
1441 # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX) || defined(_AIX3)
1442 #  include <sys/statfs.h>
1443 # else
1444 #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) || defined(MACH386)
1445 #   include <sys/vfs.h>
1446 #  else
1447 #   include <sys/mount.h>
1448 #  endif
1449 # endif
1450 #endif
1451 
1452 long
1453 freespace(dir, bsize)
1454 	char *dir;
1455 	long *bsize;
1456 {
1457 #if defined(HASSTATFS) || defined(HASUSTAT)
1458 # if defined(HASUSTAT)
1459 	struct ustat fs;
1460 	struct stat statbuf;
1461 #  define FSBLOCKSIZE	DEV_BSIZE
1462 #  define f_bavail	f_tfree
1463 # else
1464 #  if defined(ultrix)
1465 	struct fs_data fs;
1466 #   define f_bavail	fd_bfreen
1467 #   define FSBLOCKSIZE	fs.fd_bsize
1468 #  else
1469 	struct statfs fs;
1470 #   define FSBLOCKSIZE	fs.f_bsize
1471 #   if defined(_SCO_unix_) || defined(IRIX)
1472 #    define f_bavail f_bfree
1473 #   endif
1474 #  endif
1475 # endif
1476 	extern int errno;
1477 
1478 # if defined(HASUSTAT)
1479 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1480 # else
1481 #  if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX)
1482 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
1483 #  else
1484 #   if defined(ultrix)
1485 	if (statfs(dir, &fs) > 0)
1486 #   else
1487 	if (statfs(dir, &fs) == 0)
1488 #   endif
1489 #  endif
1490 # endif
1491 	{
1492 		if (bsize != NULL)
1493 			*bsize = FSBLOCKSIZE;
1494 		return (fs.f_bavail);
1495 	}
1496 #endif
1497 	return (-1);
1498 }
1499 /*
1500 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1501 **
1502 **	Only implemented if you have statfs.
1503 **
1504 **	Parameters:
1505 **		msize -- the size to check against.  If zero, we don't yet
1506 **		know how big the message will be, so just check for
1507 **		a "reasonable" amount.
1508 **
1509 **	Returns:
1510 **		TRUE if there is enough space.
1511 **		FALSE otherwise.
1512 */
1513 
1514 bool
1515 enoughspace(msize)
1516 	long msize;
1517 {
1518 	long bfree, bsize;
1519 
1520 	if (MinBlocksFree <= 0 && msize <= 0)
1521 	{
1522 		if (tTd(4, 80))
1523 			printf("enoughspace: no threshold\n");
1524 		return TRUE;
1525 	}
1526 
1527 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
1528 	{
1529 		if (tTd(4, 80))
1530 			printf("enoughspace: bavail=%ld, need=%ld\n",
1531 				bfree, msize);
1532 
1533 		/* convert msize to block count */
1534 		msize = msize / bsize + 1;
1535 		if (MinBlocksFree >= 0)
1536 			msize += MinBlocksFree;
1537 
1538 		if (bfree < msize)
1539 		{
1540 #ifdef LOG
1541 			if (LogLevel > 0)
1542 				syslog(LOG_ALERT,
1543 					"%s: low on space (have %ld, %s needs %ld in %s)",
1544 					CurEnv->e_id, bfree,
1545 					CurHostName, msize, QueueDir);
1546 #endif
1547 			return FALSE;
1548 		}
1549 	}
1550 	else if (tTd(4, 80))
1551 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1552 			MinBlocksFree, msize, errstring(errno));
1553 	return TRUE;
1554 }
1555 /*
1556 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1557 **
1558 **	This looks at an errno value and tells if this is likely to
1559 **	go away if retried later.
1560 **
1561 **	Parameters:
1562 **		err -- the errno code to classify.
1563 **
1564 **	Returns:
1565 **		TRUE if this is probably transient.
1566 **		FALSE otherwise.
1567 */
1568 
1569 bool
1570 transienterror(err)
1571 	int err;
1572 {
1573 	switch (err)
1574 	{
1575 	  case EIO:			/* I/O error */
1576 	  case ENXIO:			/* Device not configured */
1577 	  case EAGAIN:			/* Resource temporarily unavailable */
1578 	  case ENOMEM:			/* Cannot allocate memory */
1579 	  case ENODEV:			/* Operation not supported by device */
1580 	  case ENFILE:			/* Too many open files in system */
1581 	  case EMFILE:			/* Too many open files */
1582 	  case ENOSPC:			/* No space left on device */
1583 #ifdef ETIMEDOUT
1584 	  case ETIMEDOUT:		/* Connection timed out */
1585 #endif
1586 #ifdef ESTALE
1587 	  case ESTALE:			/* Stale NFS file handle */
1588 #endif
1589 #ifdef ENETDOWN
1590 	  case ENETDOWN:		/* Network is down */
1591 #endif
1592 #ifdef ENETUNREACH
1593 	  case ENETUNREACH:		/* Network is unreachable */
1594 #endif
1595 #ifdef ENETRESET
1596 	  case ENETRESET:		/* Network dropped connection on reset */
1597 #endif
1598 #ifdef ECONNABORTED
1599 	  case ECONNABORTED:		/* Software caused connection abort */
1600 #endif
1601 #ifdef ECONNRESET
1602 	  case ECONNRESET:		/* Connection reset by peer */
1603 #endif
1604 #ifdef ENOBUFS
1605 	  case ENOBUFS:			/* No buffer space available */
1606 #endif
1607 #ifdef ESHUTDOWN
1608 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1609 #endif
1610 #ifdef ECONNREFUSED
1611 	  case ECONNREFUSED:		/* Connection refused */
1612 #endif
1613 #ifdef EHOSTDOWN
1614 	  case EHOSTDOWN:		/* Host is down */
1615 #endif
1616 #ifdef EHOSTUNREACH
1617 	  case EHOSTUNREACH:		/* No route to host */
1618 #endif
1619 #ifdef EDQUOT
1620 	  case EDQUOT:			/* Disc quota exceeded */
1621 #endif
1622 #ifdef EPROCLIM
1623 	  case EPROCLIM:		/* Too many processes */
1624 #endif
1625 #ifdef EUSERS
1626 	  case EUSERS:			/* Too many users */
1627 #endif
1628 #ifdef EDEADLK
1629 	  case EDEADLK:			/* Resource deadlock avoided */
1630 #endif
1631 #ifdef EISCONN
1632 	  case EISCONN:			/* Socket already connected */
1633 #endif
1634 #ifdef EINPROGRESS
1635 	  case EINPROGRESS:		/* Operation now in progress */
1636 #endif
1637 #ifdef EALREADY
1638 	  case EALREADY:		/* Operation already in progress */
1639 #endif
1640 #ifdef EADDRINUSE
1641 	  case EADDRINUSE:		/* Address already in use */
1642 #endif
1643 #ifdef EADDRNOTAVAIL
1644 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1645 #endif
1646 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1647 	  case ENOSR:			/* Out of streams resources */
1648 #endif
1649 		return TRUE;
1650 	}
1651 
1652 	/* nope, must be permanent */
1653 	return FALSE;
1654 }
1655 /*
1656 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1657 **
1658 **	Parameters:
1659 **		fd -- the file descriptor of the file.
1660 **		filename -- the file name (for error messages).
1661 **		ext -- the filename extension.
1662 **		type -- type of the lock.  Bits can be:
1663 **			LOCK_EX -- exclusive lock.
1664 **			LOCK_NB -- non-blocking.
1665 **
1666 **	Returns:
1667 **		TRUE if the lock was acquired.
1668 **		FALSE otherwise.
1669 */
1670 
1671 bool
1672 lockfile(fd, filename, ext, type)
1673 	int fd;
1674 	char *filename;
1675 	char *ext;
1676 	int type;
1677 {
1678 # ifndef HASFLOCK
1679 	int action;
1680 	struct flock lfd;
1681 
1682 	if (ext == NULL)
1683 		ext = "";
1684 
1685 	bzero(&lfd, sizeof lfd);
1686 	if (bitset(LOCK_UN, type))
1687 		lfd.l_type = F_UNLCK;
1688 	else if (bitset(LOCK_EX, type))
1689 		lfd.l_type = F_WRLCK;
1690 	else
1691 		lfd.l_type = F_RDLCK;
1692 
1693 	if (bitset(LOCK_NB, type))
1694 		action = F_SETLK;
1695 	else
1696 		action = F_SETLKW;
1697 
1698 	if (tTd(55, 60))
1699 		printf("lockfile(%s%s, action=%d, type=%d): ",
1700 			filename, ext, action, lfd.l_type);
1701 
1702 	if (fcntl(fd, action, &lfd) >= 0)
1703 	{
1704 		if (tTd(55, 60))
1705 			printf("SUCCESS\n");
1706 		return TRUE;
1707 	}
1708 
1709 	if (tTd(55, 60))
1710 		printf("(%s) ", errstring(errno));
1711 
1712 	/*
1713 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
1714 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
1715 	**  as type "tmp" (that is, served from swap space), the
1716 	**  previous fcntl will fail with "Invalid argument" errors.
1717 	**  Since this is fairly common during testing, we will assume
1718 	**  that this indicates that the lock is successfully grabbed.
1719 	*/
1720 
1721 	if (errno == EINVAL)
1722 	{
1723 		if (tTd(55, 60))
1724 			printf("SUCCESS\n");
1725 		return TRUE;
1726 	}
1727 
1728 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1729 	{
1730 		int omode = -1;
1731 #  ifdef F_GETFL
1732 		int oerrno = errno;
1733 
1734 		(void) fcntl(fd, F_GETFL, &omode);
1735 		errno = oerrno;
1736 #  endif
1737 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1738 			filename, ext, fd, type, omode, geteuid());
1739 	}
1740 # else
1741 	if (ext == NULL)
1742 		ext = "";
1743 
1744 	if (tTd(55, 60))
1745 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
1746 
1747 	if (flock(fd, type) >= 0)
1748 	{
1749 		if (tTd(55, 60))
1750 			printf("SUCCESS\n");
1751 		return TRUE;
1752 	}
1753 
1754 	if (tTd(55, 60))
1755 		printf("(%s) ", errstring(errno));
1756 
1757 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1758 	{
1759 		int omode = -1;
1760 #  ifdef F_GETFL
1761 		int oerrno = errno;
1762 
1763 		(void) fcntl(fd, F_GETFL, &omode);
1764 		errno = oerrno;
1765 #  endif
1766 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1767 			filename, ext, fd, type, omode, geteuid());
1768 	}
1769 # endif
1770 	if (tTd(55, 60))
1771 		printf("FAIL\n");
1772 	return FALSE;
1773 }
1774 /*
1775 **  GETCFNAME -- return the name of the .cf file.
1776 **
1777 **	Some systems (e.g., NeXT) determine this dynamically.
1778 */
1779 
1780 char *
1781 getcfname()
1782 {
1783 	if (ConfFile != NULL)
1784 		return ConfFile;
1785 #ifdef NETINFO
1786 	{
1787 		extern char *ni_propval();
1788 		char *cflocation;
1789 
1790 		cflocation = ni_propval("/locations/sendmail", "sendmail.cf");
1791 		if (cflocation != NULL)
1792 			return cflocation;
1793 	}
1794 #endif
1795 	return _PATH_SENDMAILCF;
1796 }
1797 /*
1798 **  SETVENDOR -- process vendor code from V configuration line
1799 **
1800 **	Parameters:
1801 **		vendor -- string representation of vendor.
1802 **
1803 **	Returns:
1804 **		TRUE -- if ok.
1805 **		FALSE -- if vendor code could not be processed.
1806 **
1807 **	Side Effects:
1808 **		It is reasonable to set mode flags here to tweak
1809 **		processing in other parts of the code if necessary.
1810 **		For example, if you are a vendor that uses $%y to
1811 **		indicate YP lookups, you could enable that here.
1812 */
1813 
1814 bool
1815 setvendor(vendor)
1816 	char *vendor;
1817 {
1818 	if (strcasecmp(vendor, "Berkeley") == 0)
1819 		return TRUE;
1820 
1821 	/* add vendor extensions here */
1822 
1823 	return FALSE;
1824 }
1825 /*
1826 **  STRTOL -- convert string to long integer
1827 **
1828 **	For systems that don't have it in the C library.
1829 */
1830 
1831 #ifdef NEEDSTRTOL
1832 
1833 long
1834 strtol(p, ep, b)
1835 	char *p;
1836 	char **ep;
1837 	int b;
1838 {
1839 	long l = 0;
1840 	char c;
1841 	char maxd;
1842 	int neg = 1;
1843 
1844 	maxd = (b > 10) ? '9' : b + '0';
1845 
1846 	if (p && *p == '-') {
1847 		neg = -1;
1848 		p++;
1849 	}
1850 	while (p && (c = *p)) {
1851 		if (c >= '0' && c <= maxd) {
1852 			l = l*b + *p++ - '0';
1853 			continue;
1854 		}
1855 		if (c >= 'A' && c <= 'Z')
1856 			c -= 'A' + 'a';
1857 		c = c - 'a' + 10;
1858 		if (b > c) {
1859 			l = l*b + c;
1860 			p++;
1861 			continue;
1862 		}
1863 		break;
1864 	}
1865 	l *= neg;
1866 	if (ep)
1867 		*ep = p;
1868 	return l;
1869 }
1870 
1871 #endif
1872 /*
1873 **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
1874 **
1875 **	Solaris versions prior through 2.3 don't properly deliver a
1876 **	canonical h_name field.  This tries to work around it.
1877 */
1878 
1879 #ifdef SOLARIS
1880 
1881 struct hostent *
1882 solaris_gethostbyname(name)
1883 	const char *name;
1884 {
1885 # ifdef SOLARIS_2_3
1886 	static struct hostent hp;
1887 	static char buf[1000];
1888 	extern struct hostent *_switch_gethostbyname_r();
1889 
1890 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
1891 # else
1892 	extern struct hostent *__switch_gethostbyname();
1893 
1894 	return __switch_gethostbyname(name);
1895 # endif
1896 }
1897 
1898 struct hostent *
1899 solaris_gethostbyaddr(addr, len, type)
1900 	const char *addr;
1901 	int len;
1902 	int type;
1903 {
1904 # ifdef SOLARIS_2_3
1905 	static struct hostent hp;
1906 	static char buf[1000];
1907 	extern struct hostent *_switch_gethostbyaddr_r();
1908 
1909 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
1910 # else
1911 	extern struct hostent *__switch_gethostbyaddr();
1912 
1913 	return __switch_gethostbyaddr(addr, len, type);
1914 # endif
1915 }
1916 
1917 #endif
1918 /*
1919 **  NI_PROPVAL -- netinfo property value lookup routine
1920 **
1921 **	Parameters:
1922 **		directory -- the Netinfo directory name.
1923 **		propname -- the Netinfo property name.
1924 **
1925 **	Returns:
1926 **		NULL -- if:
1927 **			1. the directory is not found
1928 **			2. the property name is not found
1929 **			3. the property contains multiple values
1930 **			4. some error occured
1931 **		else -- the location of the config file.
1932 **
1933 **	Notes:
1934 **      	Caller should free the return value of ni_proval
1935 */
1936 
1937 #ifdef NETINFO
1938 
1939 # include <netinfo/ni.h>
1940 
1941 # define LOCAL_NETINFO_DOMAIN    "."
1942 # define PARENT_NETINFO_DOMAIN   ".."
1943 # define MAX_NI_LEVELS           256
1944 
1945 char *
1946 ni_propval(directory, propname)
1947 	char *directory;
1948 	char *propname;
1949 {
1950 	char *propval;
1951 	int i;
1952 	void *ni = NULL;
1953 	void *lastni = NULL;
1954 	ni_status nis;
1955 	ni_id nid;
1956 	ni_namelist ninl;
1957 
1958 	/*
1959 	**  If the passed directory and property name are found
1960 	**  in one of netinfo domains we need to search (starting
1961 	**  from the local domain moving all the way back to the
1962 	**  root domain) set propval to the property's value
1963 	**  and return it.
1964 	*/
1965 
1966 	for (i = 0; i < MAX_NI_LEVELS; ++i)
1967 	{
1968 		if (i == 0)
1969 		{
1970 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
1971 		}
1972 		else
1973 		{
1974 			if (lastni != NULL)
1975 				ni_free(lastni);
1976 			lastni = ni;
1977 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
1978 		}
1979 
1980 		/*
1981 		**  Don't bother if we didn't get a handle on a
1982 		**  proper domain.  This is not necessarily an error.
1983 		**  We would get a positive ni_status if, for instance
1984 		**  we never found the directory or property and tried
1985 		**  to open the parent of the root domain!
1986 		*/
1987 
1988 		if (nis != 0)
1989 			break;
1990 
1991 		/*
1992 		**  Find the path to the server information.
1993 		*/
1994 
1995 		if (ni_pathsearch(ni, &nid, directory) != 0)
1996 			continue;
1997 
1998 		/*
1999 		**  Find "host" information.
2000 		*/
2001 
2002 		if (ni_lookupprop(ni, &nid, propname, &ninl) != 0)
2003 			continue;
2004 
2005 		/*
2006 		**  If there's only one name in
2007 		**  the list, assume we've got
2008 		**  what we want.
2009 		*/
2010 
2011 		if (ninl.ni_namelist_len == 1)
2012 		{
2013 			propval = ni_name_dup(ninl.ni_namelist_val[0]);
2014 			break;
2015 		}
2016 	}
2017 
2018 	/*
2019 	**  Clean up.
2020 	*/
2021 
2022 	if (ni != NULL)
2023 		ni_free(ni);
2024 	if (lastni != NULL && ni != lastni)
2025 		ni_free(lastni);
2026 
2027 	return propval;
2028 }
2029 
2030 #endif /* NETINFO */
2031