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