xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision a9b964fe)
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.78 (Berkeley) 02/21/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 
817 /*
818  * Copyright 1989 Massachusetts Institute of Technology
819  *
820  * Permission to use, copy, modify, distribute, and sell this software and its
821  * documentation for any purpose is hereby granted without fee, provided that
822  * the above copyright notice appear in all copies and that both that
823  * copyright notice and this permission notice appear in supporting
824  * documentation, and that the name of M.I.T. not be used in advertising or
825  * publicity pertaining to distribution of the software without specific,
826  * written prior permission.  M.I.T. makes no representations about the
827  * suitability of this software for any purpose.  It is provided "as is"
828  * without express or implied warranty.
829  *
830  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
831  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
832  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
833  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
834  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
835  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
836  *
837  * Authors:  Many and varied...
838  */
839 
840 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
841 #ifndef lint
842 static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
843 #endif /* !lint */
844 
845 #ifdef apollo
846 # undef volatile
847 #    include <apollo/base.h>
848 
849 /* ARGSUSED */
850 int getloadavg( call_data )
851      caddr_t	call_data;	/* pointer to (double) return value */
852 {
853      double *avenrun = (double *) call_data;
854      int i;
855      status_$t      st;
856      long loadav[3];
857      proc1_$get_loadav(loadav, &st);
858      *avenrun = loadav[0] / (double) (1 << 16);
859      return(0);
860 }
861 #   endif /* apollo */
862 /*
863 **  SHOULDQUEUE -- should this message be queued or sent?
864 **
865 **	Compares the message cost to the load average to decide.
866 **
867 **	Parameters:
868 **		pri -- the priority of the message in question.
869 **		ctime -- the message creation time.
870 **
871 **	Returns:
872 **		TRUE -- if this message should be queued up for the
873 **			time being.
874 **		FALSE -- if the load is low enough to send this message.
875 **
876 **	Side Effects:
877 **		none.
878 */
879 
880 bool
881 shouldqueue(pri, ctime)
882 	long pri;
883 	time_t ctime;
884 {
885 	if (CurrentLA < QueueLA)
886 		return (FALSE);
887 	if (CurrentLA >= RefuseLA)
888 		return (TRUE);
889 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
890 }
891 /*
892 **  REFUSECONNECTIONS -- decide if connections should be refused
893 **
894 **	Parameters:
895 **		none.
896 **
897 **	Returns:
898 **		TRUE if incoming SMTP connections should be refused
899 **			(for now).
900 **		FALSE if we should accept new work.
901 **
902 **	Side Effects:
903 **		none.
904 */
905 
906 bool
907 refuseconnections()
908 {
909 #ifdef XLA
910 	if (!xla_smtp_ok())
911 		return TRUE;
912 #endif
913 
914 	/* this is probably too simplistic */
915 	return (CurrentLA >= RefuseLA);
916 }
917 /*
918 **  SETPROCTITLE -- set process title for ps
919 **
920 **	Parameters:
921 **		fmt -- a printf style format string.
922 **		a, b, c -- possible parameters to fmt.
923 **
924 **	Returns:
925 **		none.
926 **
927 **	Side Effects:
928 **		Clobbers argv of our main procedure so ps(1) will
929 **		display the title.
930 */
931 
932 #ifdef SETPROCTITLE
933 # ifdef HASSETPROCTITLE
934    *** ERROR ***  Cannot have both SETPROCTITLE and HASSETPROCTITLE defined
935 # endif
936 # ifdef __hpux
937 #  include <sys/pstat.h>
938 # endif
939 # ifdef BSD4_4
940 #  include <machine/vmparam.h>
941 #  include <sys/exec.h>
942 #  ifdef __bsdi__
943 #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
944 #   define PROCTITLEPAD	'\0'
945 #  endif
946 #  ifdef PS_STRINGS
947 #   define SETPROC_STATIC static
948 #  endif
949 # endif
950 # ifndef SETPROC_STATIC
951 #  define SETPROC_STATIC
952 # endif
953 #endif
954 
955 #ifndef PROCTITLEPAD
956 # define PROCTITLEPAD	' '
957 #endif
958 
959 #ifndef HASSETPROCTITLE
960 
961 /*VARARGS1*/
962 #ifdef __STDC__
963 setproctitle(char *fmt, ...)
964 #else
965 setproctitle(fmt, va_alist)
966 	char *fmt;
967 	va_dcl
968 #endif
969 {
970 # ifdef SETPROCTITLE
971 	register char *p;
972 	register int i;
973 	SETPROC_STATIC char buf[MAXLINE];
974 	VA_LOCAL_DECL
975 #  ifdef __hpux
976 	union pstun pst;
977 #  endif
978 	extern char **Argv;
979 	extern char *LastArgv;
980 
981 	p = buf;
982 
983 	/* print sendmail: heading for grep */
984 	(void) strcpy(p, "sendmail: ");
985 	p += strlen(p);
986 
987 	/* print the argument string */
988 	VA_START(fmt);
989 	(void) vsprintf(p, fmt, ap);
990 	VA_END;
991 
992 	i = strlen(buf);
993 
994 #  ifdef __hpux
995 	pst.pst_command = buf;
996 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
997 #  else
998 #   ifdef PS_STRINGS
999 	PS_STRINGS->ps_nargvstr = 1;
1000 	PS_STRINGS->ps_argvstr = buf;
1001 #   else
1002 	if (i > LastArgv - Argv[0] - 2)
1003 	{
1004 		i = LastArgv - Argv[0] - 2;
1005 		buf[i] = '\0';
1006 	}
1007 	(void) strcpy(Argv[0], buf);
1008 	p = &Argv[0][i];
1009 	while (p < LastArgv)
1010 		*p++ = PROCTITLEPAD;
1011 #   endif
1012 #  endif
1013 # endif /* SETPROCTITLE */
1014 }
1015 
1016 #endif
1017 /*
1018 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
1019 **
1020 **	Parameters:
1021 **		none.
1022 **
1023 **	Returns:
1024 **		none.
1025 **
1026 **	Side Effects:
1027 **		Picks up extant zombies.
1028 */
1029 
1030 void
1031 reapchild()
1032 {
1033 	int olderrno = errno;
1034 # ifdef HASWAITPID
1035 	auto int status;
1036 	int count;
1037 	int pid;
1038 
1039 	count = 0;
1040 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1041 	{
1042 		if (count++ > 1000)
1043 		{
1044 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
1045 				pid, status);
1046 			break;
1047 		}
1048 	}
1049 # else
1050 # ifdef WNOHANG
1051 	union wait status;
1052 
1053 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
1054 		continue;
1055 # else /* WNOHANG */
1056 	auto int status;
1057 
1058 	while (wait(&status) > 0)
1059 		continue;
1060 # endif /* WNOHANG */
1061 # endif
1062 # ifdef SYS5SIGNALS
1063 	(void) setsignal(SIGCHLD, reapchild);
1064 # endif
1065 	errno = olderrno;
1066 }
1067 /*
1068 **  UNSETENV -- remove a variable from the environment
1069 **
1070 **	Not needed on newer systems.
1071 **
1072 **	Parameters:
1073 **		name -- the string name of the environment variable to be
1074 **			deleted from the current environment.
1075 **
1076 **	Returns:
1077 **		none.
1078 **
1079 **	Globals:
1080 **		environ -- a pointer to the current environment.
1081 **
1082 **	Side Effects:
1083 **		Modifies environ.
1084 */
1085 
1086 #ifndef HASUNSETENV
1087 
1088 void
1089 unsetenv(name)
1090 	char *name;
1091 {
1092 	extern char **environ;
1093 	register char **pp;
1094 	int len = strlen(name);
1095 
1096 	for (pp = environ; *pp != NULL; pp++)
1097 	{
1098 		if (strncmp(name, *pp, len) == 0 &&
1099 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1100 			break;
1101 	}
1102 
1103 	for (; *pp != NULL; pp++)
1104 		*pp = pp[1];
1105 }
1106 
1107 #endif
1108 /*
1109 **  GETDTABLESIZE -- return number of file descriptors
1110 **
1111 **	Only on non-BSD systems
1112 **
1113 **	Parameters:
1114 **		none
1115 **
1116 **	Returns:
1117 **		size of file descriptor table
1118 **
1119 **	Side Effects:
1120 **		none
1121 */
1122 
1123 #ifdef SOLARIS
1124 # include <sys/resource.h>
1125 #endif
1126 
1127 int
1128 getdtsize()
1129 {
1130 #ifdef RLIMIT_NOFILE
1131 	struct rlimit rl;
1132 
1133 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1134 		return rl.rlim_cur;
1135 #endif
1136 
1137 # ifdef HASGETDTABLESIZE
1138 	return getdtablesize();
1139 # else
1140 #  ifdef _SC_OPEN_MAX
1141 	return sysconf(_SC_OPEN_MAX);
1142 #  else
1143 	return NOFILE;
1144 #  endif
1145 # endif
1146 }
1147 /*
1148 **  UNAME -- get the UUCP name of this system.
1149 */
1150 
1151 #ifndef HASUNAME
1152 
1153 int
1154 uname(name)
1155 	struct utsname *name;
1156 {
1157 	FILE *file;
1158 	char *n;
1159 
1160 	name->nodename[0] = '\0';
1161 
1162 	/* try /etc/whoami -- one line with the node name */
1163 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1164 	{
1165 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1166 		(void) fclose(file);
1167 		n = strchr(name->nodename, '\n');
1168 		if (n != NULL)
1169 			*n = '\0';
1170 		if (name->nodename[0] != '\0')
1171 			return (0);
1172 	}
1173 
1174 	/* try /usr/include/whoami.h -- has a #define somewhere */
1175 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1176 	{
1177 		char buf[MAXLINE];
1178 
1179 		while (fgets(buf, MAXLINE, file) != NULL)
1180 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1181 					NODE_LENGTH, name->nodename) > 0)
1182 				break;
1183 		(void) fclose(file);
1184 		if (name->nodename[0] != '\0')
1185 			return (0);
1186 	}
1187 
1188 #ifdef TRUST_POPEN
1189 	/*
1190 	**  Popen is known to have security holes.
1191 	*/
1192 
1193 	/* try uuname -l to return local name */
1194 	if ((file = popen("uuname -l", "r")) != NULL)
1195 	{
1196 		(void) fgets(name, NODE_LENGTH + 1, file);
1197 		(void) pclose(file);
1198 		n = strchr(name, '\n');
1199 		if (n != NULL)
1200 			*n = '\0';
1201 		if (name->nodename[0] != '\0')
1202 			return (0);
1203 	}
1204 #endif
1205 
1206 	return (-1);
1207 }
1208 #endif /* HASUNAME */
1209 /*
1210 **  INITGROUPS -- initialize groups
1211 **
1212 **	Stub implementation for System V style systems
1213 */
1214 
1215 #ifndef HASINITGROUPS
1216 
1217 initgroups(name, basegid)
1218 	char *name;
1219 	int basegid;
1220 {
1221 	return 0;
1222 }
1223 
1224 #endif
1225 /*
1226 **  SETSID -- set session id (for non-POSIX systems)
1227 */
1228 
1229 #ifndef HASSETSID
1230 
1231 pid_t
1232 setsid __P ((void))
1233 {
1234 #ifdef TIOCNOTTY
1235 	int fd;
1236 
1237 	fd = open("/dev/tty", 2);
1238 	if (fd >= 0)
1239 	{
1240 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1241 		(void) close(fd);
1242 	}
1243 #endif /* TIOCNOTTY */
1244 # ifdef SYS5SETPGRP
1245 	return setpgrp();
1246 # else
1247 	return setpgid(0, getpid());
1248 # endif
1249 }
1250 
1251 #endif
1252 /*
1253 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1254 **
1255 **	Data General DG/UX version of inet_addr returns a struct in_addr
1256 **	instead of a long.  This patches things.
1257 */
1258 
1259 #ifdef DGUX
1260 
1261 #undef inet_addr
1262 
1263 long
1264 dgux_inet_addr(host)
1265 	char *host;
1266 {
1267 	struct in_addr haddr;
1268 
1269 	haddr = inet_addr(host);
1270 	return haddr.s_addr;
1271 }
1272 
1273 #endif
1274 /*
1275 **  GETOPT -- for old systems or systems with bogus implementations
1276 */
1277 
1278 #ifdef NEEDGETOPT
1279 
1280 /*
1281  * Copyright (c) 1985 Regents of the University of California.
1282  * All rights reserved.  The Berkeley software License Agreement
1283  * specifies the terms and conditions for redistribution.
1284  */
1285 
1286 
1287 /*
1288 ** this version hacked to add `atend' flag to allow state machine
1289 ** to reset if invoked by the program to scan args for a 2nd time
1290 */
1291 
1292 #if defined(LIBC_SCCS) && !defined(lint)
1293 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1294 #endif /* LIBC_SCCS and not lint */
1295 
1296 #include <stdio.h>
1297 
1298 /*
1299  * get option letter from argument vector
1300  */
1301 #ifdef _CONVEX_SOURCE
1302 extern int	optind, opterr;
1303 #else
1304 int	opterr = 1;		/* if error message should be printed */
1305 int	optind = 1;		/* index into parent argv vector */
1306 #endif
1307 int	optopt;			/* character checked for validity */
1308 char	*optarg;		/* argument associated with option */
1309 
1310 #define BADCH	(int)'?'
1311 #define EMSG	""
1312 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1313 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1314 
1315 getopt(nargc,nargv,ostr)
1316 	int		nargc;
1317 	char *const	*nargv;
1318 	const char	*ostr;
1319 {
1320 	static char	*place = EMSG;	/* option letter processing */
1321 	static char	atend = 0;
1322 	register char	*oli;		/* option letter list index */
1323 
1324 	if (atend) {
1325 		atend = 0;
1326 		place = EMSG;
1327 	}
1328 	if(!*place) {			/* update scanning pointer */
1329 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1330 			atend++;
1331 			return(EOF);
1332 		}
1333 		if (*place == '-') {	/* found "--" */
1334 			++optind;
1335 			atend++;
1336 			return(EOF);
1337 		}
1338 	}				/* option letter okay? */
1339 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1340 		if (!*place) ++optind;
1341 		tell(": illegal option -- ");
1342 	}
1343 	if (*++oli != ':') {		/* don't need argument */
1344 		optarg = NULL;
1345 		if (!*place) ++optind;
1346 	}
1347 	else {				/* need an argument */
1348 		if (*place) optarg = place;	/* no white space */
1349 		else if (nargc <= ++optind) {	/* no arg */
1350 			place = EMSG;
1351 			tell(": option requires an argument -- ");
1352 		}
1353 	 	else optarg = nargv[optind];	/* white space */
1354 		place = EMSG;
1355 		++optind;
1356 	}
1357 	return(optopt);			/* dump back option letter */
1358 }
1359 
1360 #endif
1361 /*
1362 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1363 */
1364 
1365 #ifdef NEEDVPRINTF
1366 
1367 #define MAXARG	16
1368 
1369 vfprintf(fp, fmt, ap)
1370 	FILE *	fp;
1371 	char *	fmt;
1372 	char **	ap;
1373 {
1374 	char *	bp[MAXARG];
1375 	int	i = 0;
1376 
1377 	while (*ap && i < MAXARG)
1378 		bp[i++] = *ap++;
1379 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1380 			 bp[4], bp[5], bp[6], bp[7],
1381 			 bp[8], bp[9], bp[10], bp[11],
1382 			 bp[12], bp[13], bp[14], bp[15]);
1383 }
1384 
1385 vsprintf(s, fmt, ap)
1386 	char *	s;
1387 	char *	fmt;
1388 	char **	ap;
1389 {
1390 	char *	bp[MAXARG];
1391 	int	i = 0;
1392 
1393 	while (*ap && i < MAXARG)
1394 		bp[i++] = *ap++;
1395 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1396 			bp[4], bp[5], bp[6], bp[7],
1397 			bp[8], bp[9], bp[10], bp[11],
1398 			bp[12], bp[13], bp[14], bp[15]);
1399 }
1400 
1401 #endif
1402 /*
1403 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
1404 **
1405 **	Parameters:
1406 **		shell -- the user's shell from /etc/passwd
1407 **
1408 **	Returns:
1409 **		TRUE -- if it is ok to use this for unrestricted access.
1410 **		FALSE -- if the shell is restricted.
1411 */
1412 
1413 #if !HASGETUSERSHELL
1414 
1415 # ifndef _PATH_SHELLS
1416 #  define _PATH_SHELLS	"/etc/shells"
1417 # endif
1418 
1419 char	*DefaultUserShells[] =
1420 {
1421 	"/bin/sh",
1422 	"/usr/bin/sh",
1423 	"/bin/csh",
1424 	"/usr/bin/csh",
1425 #ifdef __hpux
1426 	"/bin/rsh",
1427 	"/bin/ksh",
1428 	"/bin/rksh",
1429 	"/bin/pam",
1430 	"/usr/bin/keysh",
1431 	"/bin/posix/sh",
1432 #endif
1433 	NULL
1434 };
1435 
1436 #endif
1437 
1438 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
1439 
1440 bool
1441 usershellok(shell)
1442 	char *shell;
1443 {
1444 #if HASGETUSERSHELL
1445 	register char *p;
1446 	extern char *getusershell();
1447 
1448 	setusershell();
1449 	while ((p = getusershell()) != NULL)
1450 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
1451 			break;
1452 	endusershell();
1453 	return p != NULL;
1454 #else
1455 	register FILE *shellf;
1456 	char buf[MAXLINE];
1457 
1458 	shellf = fopen(_PATH_SHELLS, "r");
1459 	if (shellf == NULL)
1460 	{
1461 		/* no /etc/shells; see if it is one of the std shells */
1462 		char **d;
1463 
1464 		for (d = DefaultUserShells; *d != NULL; d++)
1465 		{
1466 			if (strcmp(shell, *d) == 0)
1467 				return TRUE;
1468 		}
1469 		return FALSE;
1470 	}
1471 
1472 	while (fgets(buf, sizeof buf, shellf) != NULL)
1473 	{
1474 		register char *p, *q;
1475 
1476 		p = buf;
1477 		while (*p != '\0' && *p != '#' && *p != '/')
1478 			p++;
1479 		if (*p == '#' || *p == '\0')
1480 			continue;
1481 		q = p;
1482 		while (*p != '\0' && *p != '#' && !isspace(*p))
1483 			p++;
1484 		*p = '\0';
1485 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
1486 		{
1487 			fclose(shellf);
1488 			return TRUE;
1489 		}
1490 	}
1491 	fclose(shellf);
1492 	return FALSE;
1493 #endif
1494 }
1495 /*
1496 **  FREESPACE -- see how much free space is on the queue filesystem
1497 **
1498 **	Only implemented if you have statfs.
1499 **
1500 **	Parameters:
1501 **		dir -- the directory in question.
1502 **		bsize -- a variable into which the filesystem
1503 **			block size is stored.
1504 **
1505 **	Returns:
1506 **		The number of bytes free on the queue filesystem.
1507 **		-1 if the statfs call fails.
1508 **
1509 **	Side effects:
1510 **		Puts the filesystem block size into bsize.
1511 */
1512 
1513 /* statfs types */
1514 #define SFS_NONE	0	/* no statfs implementation */
1515 #define SFS_USTAT	1	/* use ustat */
1516 #define SFS_4ARGS	2	/* use four-argument statfs call */
1517 #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
1518 #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
1519 #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
1520 
1521 #ifndef SFS_TYPE
1522 # define SFS_TYPE	SFS_NONE
1523 #endif
1524 
1525 #if SFS_TYPE == SFS_USTAT
1526 # include <ustat.h>
1527 #endif
1528 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
1529 # include <sys/statfs.h>
1530 #endif
1531 #if SFS_TYPE == SFS_VFS
1532 # include <sys/vfs.h>
1533 #endif
1534 #if SFS_TYPE == SFS_MOUNT
1535 # include <sys/mount.h>
1536 #endif
1537 
1538 long
1539 freespace(dir, bsize)
1540 	char *dir;
1541 	long *bsize;
1542 {
1543 #if SFS_TYPE != SFS_NONE
1544 # if SFS_TYPE == SFS_USTAT
1545 	struct ustat fs;
1546 	struct stat statbuf;
1547 #  define FSBLOCKSIZE	DEV_BSIZE
1548 #  define f_bavail	f_tfree
1549 # else
1550 #  if defined(ultrix)
1551 	struct fs_data fs;
1552 #   define f_bavail	fd_bfreen
1553 #   define FSBLOCKSIZE	fs.fd_bsize
1554 #  else
1555 	struct statfs fs;
1556 #   define FSBLOCKSIZE	fs.f_bsize
1557 #   if defined(_SCO_unix_) || defined(IRIX) || defined(apollo)
1558 #    define f_bavail f_bfree
1559 #   endif
1560 #  endif
1561 # endif
1562 	extern int errno;
1563 
1564 # if SFS_TYPE == SFS_USTAT
1565 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1566 # else
1567 #  if SFS_TYPE == SFS_4ARGS
1568 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
1569 #  else
1570 #   if defined(ultrix)
1571 	if (statfs(dir, &fs) > 0)
1572 #   else
1573 	if (statfs(dir, &fs) == 0)
1574 #   endif
1575 #  endif
1576 # endif
1577 	{
1578 		if (bsize != NULL)
1579 			*bsize = FSBLOCKSIZE;
1580 		return (fs.f_bavail);
1581 	}
1582 #endif
1583 	return (-1);
1584 }
1585 /*
1586 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1587 **
1588 **	Only implemented if you have statfs.
1589 **
1590 **	Parameters:
1591 **		msize -- the size to check against.  If zero, we don't yet
1592 **		know how big the message will be, so just check for
1593 **		a "reasonable" amount.
1594 **
1595 **	Returns:
1596 **		TRUE if there is enough space.
1597 **		FALSE otherwise.
1598 */
1599 
1600 bool
1601 enoughspace(msize)
1602 	long msize;
1603 {
1604 	long bfree, bsize;
1605 
1606 	if (MinBlocksFree <= 0 && msize <= 0)
1607 	{
1608 		if (tTd(4, 80))
1609 			printf("enoughspace: no threshold\n");
1610 		return TRUE;
1611 	}
1612 
1613 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
1614 	{
1615 		if (tTd(4, 80))
1616 			printf("enoughspace: bavail=%ld, need=%ld\n",
1617 				bfree, msize);
1618 
1619 		/* convert msize to block count */
1620 		msize = msize / bsize + 1;
1621 		if (MinBlocksFree >= 0)
1622 			msize += MinBlocksFree;
1623 
1624 		if (bfree < msize)
1625 		{
1626 #ifdef LOG
1627 			if (LogLevel > 0)
1628 				syslog(LOG_ALERT,
1629 					"%s: low on space (have %ld, %s needs %ld in %s)",
1630 					CurEnv->e_id, bfree,
1631 					CurHostName, msize, QueueDir);
1632 #endif
1633 			return FALSE;
1634 		}
1635 	}
1636 	else if (tTd(4, 80))
1637 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1638 			MinBlocksFree, msize, errstring(errno));
1639 	return TRUE;
1640 }
1641 /*
1642 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1643 **
1644 **	This looks at an errno value and tells if this is likely to
1645 **	go away if retried later.
1646 **
1647 **	Parameters:
1648 **		err -- the errno code to classify.
1649 **
1650 **	Returns:
1651 **		TRUE if this is probably transient.
1652 **		FALSE otherwise.
1653 */
1654 
1655 bool
1656 transienterror(err)
1657 	int err;
1658 {
1659 	switch (err)
1660 	{
1661 	  case EIO:			/* I/O error */
1662 	  case ENXIO:			/* Device not configured */
1663 	  case EAGAIN:			/* Resource temporarily unavailable */
1664 	  case ENOMEM:			/* Cannot allocate memory */
1665 	  case ENODEV:			/* Operation not supported by device */
1666 	  case ENFILE:			/* Too many open files in system */
1667 	  case EMFILE:			/* Too many open files */
1668 	  case ENOSPC:			/* No space left on device */
1669 #ifdef ETIMEDOUT
1670 	  case ETIMEDOUT:		/* Connection timed out */
1671 #endif
1672 #ifdef ESTALE
1673 	  case ESTALE:			/* Stale NFS file handle */
1674 #endif
1675 #ifdef ENETDOWN
1676 	  case ENETDOWN:		/* Network is down */
1677 #endif
1678 #ifdef ENETUNREACH
1679 	  case ENETUNREACH:		/* Network is unreachable */
1680 #endif
1681 #ifdef ENETRESET
1682 	  case ENETRESET:		/* Network dropped connection on reset */
1683 #endif
1684 #ifdef ECONNABORTED
1685 	  case ECONNABORTED:		/* Software caused connection abort */
1686 #endif
1687 #ifdef ECONNRESET
1688 	  case ECONNRESET:		/* Connection reset by peer */
1689 #endif
1690 #ifdef ENOBUFS
1691 	  case ENOBUFS:			/* No buffer space available */
1692 #endif
1693 #ifdef ESHUTDOWN
1694 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1695 #endif
1696 #ifdef ECONNREFUSED
1697 	  case ECONNREFUSED:		/* Connection refused */
1698 #endif
1699 #ifdef EHOSTDOWN
1700 	  case EHOSTDOWN:		/* Host is down */
1701 #endif
1702 #ifdef EHOSTUNREACH
1703 	  case EHOSTUNREACH:		/* No route to host */
1704 #endif
1705 #ifdef EDQUOT
1706 	  case EDQUOT:			/* Disc quota exceeded */
1707 #endif
1708 #ifdef EPROCLIM
1709 	  case EPROCLIM:		/* Too many processes */
1710 #endif
1711 #ifdef EUSERS
1712 	  case EUSERS:			/* Too many users */
1713 #endif
1714 #ifdef EDEADLK
1715 	  case EDEADLK:			/* Resource deadlock avoided */
1716 #endif
1717 #ifdef EISCONN
1718 	  case EISCONN:			/* Socket already connected */
1719 #endif
1720 #ifdef EINPROGRESS
1721 	  case EINPROGRESS:		/* Operation now in progress */
1722 #endif
1723 #ifdef EALREADY
1724 	  case EALREADY:		/* Operation already in progress */
1725 #endif
1726 #ifdef EADDRINUSE
1727 	  case EADDRINUSE:		/* Address already in use */
1728 #endif
1729 #ifdef EADDRNOTAVAIL
1730 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1731 #endif
1732 #ifdef ETXTBSY
1733 	  case ETXTBSY:			/* (Apollo) file locked */
1734 #endif
1735 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1736 	  case ENOSR:			/* Out of streams resources */
1737 #endif
1738 		return TRUE;
1739 	}
1740 
1741 	/* nope, must be permanent */
1742 	return FALSE;
1743 }
1744 /*
1745 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1746 **
1747 **	Parameters:
1748 **		fd -- the file descriptor of the file.
1749 **		filename -- the file name (for error messages).
1750 **		ext -- the filename extension.
1751 **		type -- type of the lock.  Bits can be:
1752 **			LOCK_EX -- exclusive lock.
1753 **			LOCK_NB -- non-blocking.
1754 **
1755 **	Returns:
1756 **		TRUE if the lock was acquired.
1757 **		FALSE otherwise.
1758 */
1759 
1760 bool
1761 lockfile(fd, filename, ext, type)
1762 	int fd;
1763 	char *filename;
1764 	char *ext;
1765 	int type;
1766 {
1767 # if !HASFLOCK
1768 	int action;
1769 	struct flock lfd;
1770 
1771 	if (ext == NULL)
1772 		ext = "";
1773 
1774 	bzero(&lfd, sizeof lfd);
1775 	if (bitset(LOCK_UN, type))
1776 		lfd.l_type = F_UNLCK;
1777 	else if (bitset(LOCK_EX, type))
1778 		lfd.l_type = F_WRLCK;
1779 	else
1780 		lfd.l_type = F_RDLCK;
1781 
1782 	if (bitset(LOCK_NB, type))
1783 		action = F_SETLK;
1784 	else
1785 		action = F_SETLKW;
1786 
1787 	if (tTd(55, 60))
1788 		printf("lockfile(%s%s, action=%d, type=%d): ",
1789 			filename, ext, action, lfd.l_type);
1790 
1791 	if (fcntl(fd, action, &lfd) >= 0)
1792 	{
1793 		if (tTd(55, 60))
1794 			printf("SUCCESS\n");
1795 		return TRUE;
1796 	}
1797 
1798 	if (tTd(55, 60))
1799 		printf("(%s) ", errstring(errno));
1800 
1801 	/*
1802 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
1803 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
1804 	**  as type "tmp" (that is, served from swap space), the
1805 	**  previous fcntl will fail with "Invalid argument" errors.
1806 	**  Since this is fairly common during testing, we will assume
1807 	**  that this indicates that the lock is successfully grabbed.
1808 	*/
1809 
1810 	if (errno == EINVAL)
1811 	{
1812 		if (tTd(55, 60))
1813 			printf("SUCCESS\n");
1814 		return TRUE;
1815 	}
1816 
1817 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1818 	{
1819 		int omode = -1;
1820 #  ifdef F_GETFL
1821 		int oerrno = errno;
1822 
1823 		(void) fcntl(fd, F_GETFL, &omode);
1824 		errno = oerrno;
1825 #  endif
1826 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1827 			filename, ext, fd, type, omode, geteuid());
1828 	}
1829 # else
1830 	if (ext == NULL)
1831 		ext = "";
1832 
1833 	if (tTd(55, 60))
1834 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
1835 
1836 	if (flock(fd, type) >= 0)
1837 	{
1838 		if (tTd(55, 60))
1839 			printf("SUCCESS\n");
1840 		return TRUE;
1841 	}
1842 
1843 	if (tTd(55, 60))
1844 		printf("(%s) ", errstring(errno));
1845 
1846 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1847 	{
1848 		int omode = -1;
1849 #  ifdef F_GETFL
1850 		int oerrno = errno;
1851 
1852 		(void) fcntl(fd, F_GETFL, &omode);
1853 		errno = oerrno;
1854 #  endif
1855 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1856 			filename, ext, fd, type, omode, geteuid());
1857 	}
1858 # endif
1859 	if (tTd(55, 60))
1860 		printf("FAIL\n");
1861 	return FALSE;
1862 }
1863 /*
1864 **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
1865 **
1866 **	Parameters:
1867 **		fd -- the file descriptor to check.
1868 **
1869 **	Returns:
1870 **		TRUE -- if only root can chown the file to an arbitrary
1871 **			user.
1872 **		FALSE -- if an arbitrary user can give away a file.
1873 */
1874 
1875 bool
1876 chownsafe(fd)
1877 	int fd;
1878 {
1879 #ifdef __hpux
1880 	char *s;
1881 	int tfd;
1882 	uid_t o_uid, o_euid;
1883 	gid_t o_gid, o_egid;
1884 	bool rval;
1885 	struct stat stbuf;
1886 
1887 	o_uid = getuid();
1888 	o_euid = geteuid();
1889 	o_gid = getgid();
1890 	o_egid = getegid();
1891 	fstat(fd, &stbuf);
1892 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
1893 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
1894 	s = tmpnam(NULL);
1895 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
1896 	rval = fchown(tfd, DefUid, DefGid) != 0;
1897 	close(tfd);
1898 	unlink(s);
1899 	setreuid(o_uid, o_euid);
1900 	setresgid(o_gid, o_egid, -1);
1901 	return rval;
1902 #else
1903 # ifdef _POSIX_CHOWN_RESTRICTED
1904 #  if _POSIX_CHOWN_RESTRICTED == -1
1905 	return FALSE;
1906 #  else
1907 	return TRUE;
1908 #  endif
1909 # else
1910 #  ifdef _PC_CHOWN_RESTRICTED
1911 	return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0;
1912 #  else
1913 #   ifdef BSD
1914 	return TRUE;
1915 #   else
1916 	return FALSE;
1917 #   endif
1918 #  endif
1919 # endif
1920 #endif
1921 }
1922 /*
1923 **  GETCFNAME -- return the name of the .cf file.
1924 **
1925 **	Some systems (e.g., NeXT) determine this dynamically.
1926 */
1927 
1928 char *
1929 getcfname()
1930 {
1931 	if (ConfFile != NULL)
1932 		return ConfFile;
1933 #ifdef NETINFO
1934 	{
1935 		extern char *ni_propval();
1936 		char *cflocation;
1937 
1938 		cflocation = ni_propval("/locations/sendmail", "sendmail.cf");
1939 		if (cflocation != NULL)
1940 			return cflocation;
1941 	}
1942 #endif
1943 	return _PATH_SENDMAILCF;
1944 }
1945 /*
1946 **  SETVENDOR -- process vendor code from V configuration line
1947 **
1948 **	Parameters:
1949 **		vendor -- string representation of vendor.
1950 **
1951 **	Returns:
1952 **		TRUE -- if ok.
1953 **		FALSE -- if vendor code could not be processed.
1954 **
1955 **	Side Effects:
1956 **		It is reasonable to set mode flags here to tweak
1957 **		processing in other parts of the code if necessary.
1958 **		For example, if you are a vendor that uses $%y to
1959 **		indicate YP lookups, you could enable that here.
1960 */
1961 
1962 bool
1963 setvendor(vendor)
1964 	char *vendor;
1965 {
1966 	if (strcasecmp(vendor, "Berkeley") == 0)
1967 		return TRUE;
1968 
1969 	/* add vendor extensions here */
1970 
1971 	return FALSE;
1972 }
1973 /*
1974 **  STRTOL -- convert string to long integer
1975 **
1976 **	For systems that don't have it in the C library.
1977 **
1978 **	This is taken verbatim from the 4.4-Lite C library.
1979 */
1980 
1981 #ifdef NEEDSTRTOL
1982 
1983 #if defined(LIBC_SCCS) && !defined(lint)
1984 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
1985 #endif /* LIBC_SCCS and not lint */
1986 
1987 #include <limits.h>
1988 
1989 /*
1990  * Convert a string to a long integer.
1991  *
1992  * Ignores `locale' stuff.  Assumes that the upper and lower case
1993  * alphabets and digits are each contiguous.
1994  */
1995 
1996 long
1997 strtol(nptr, endptr, base)
1998 	const char *nptr;
1999 	char **endptr;
2000 	register int base;
2001 {
2002 	register const char *s = nptr;
2003 	register unsigned long acc;
2004 	register int c;
2005 	register unsigned long cutoff;
2006 	register int neg = 0, any, cutlim;
2007 
2008 	/*
2009 	 * Skip white space and pick up leading +/- sign if any.
2010 	 * If base is 0, allow 0x for hex and 0 for octal, else
2011 	 * assume decimal; if base is already 16, allow 0x.
2012 	 */
2013 	do {
2014 		c = *s++;
2015 	} while (isspace(c));
2016 	if (c == '-') {
2017 		neg = 1;
2018 		c = *s++;
2019 	} else if (c == '+')
2020 		c = *s++;
2021 	if ((base == 0 || base == 16) &&
2022 	    c == '0' && (*s == 'x' || *s == 'X')) {
2023 		c = s[1];
2024 		s += 2;
2025 		base = 16;
2026 	}
2027 	if (base == 0)
2028 		base = c == '0' ? 8 : 10;
2029 
2030 	/*
2031 	 * Compute the cutoff value between legal numbers and illegal
2032 	 * numbers.  That is the largest legal value, divided by the
2033 	 * base.  An input number that is greater than this value, if
2034 	 * followed by a legal input character, is too big.  One that
2035 	 * is equal to this value may be valid or not; the limit
2036 	 * between valid and invalid numbers is then based on the last
2037 	 * digit.  For instance, if the range for longs is
2038 	 * [-2147483648..2147483647] and the input base is 10,
2039 	 * cutoff will be set to 214748364 and cutlim to either
2040 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
2041 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
2042 	 * the number is too big, and we will return a range error.
2043 	 *
2044 	 * Set any if any `digits' consumed; make it negative to indicate
2045 	 * overflow.
2046 	 */
2047 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
2048 	cutlim = cutoff % (unsigned long)base;
2049 	cutoff /= (unsigned long)base;
2050 	for (acc = 0, any = 0;; c = *s++) {
2051 		if (isdigit(c))
2052 			c -= '0';
2053 		else if (isalpha(c))
2054 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
2055 		else
2056 			break;
2057 		if (c >= base)
2058 			break;
2059 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
2060 			any = -1;
2061 		else {
2062 			any = 1;
2063 			acc *= base;
2064 			acc += c;
2065 		}
2066 	}
2067 	if (any < 0) {
2068 		acc = neg ? LONG_MIN : LONG_MAX;
2069 		errno = ERANGE;
2070 	} else if (neg)
2071 		acc = -acc;
2072 	if (endptr != 0)
2073 		*endptr = (char *)(any ? s - 1 : nptr);
2074 	return (acc);
2075 }
2076 
2077 #endif
2078 /*
2079 **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
2080 **
2081 **	Solaris versions prior through 2.3 don't properly deliver a
2082 **	canonical h_name field.  This tries to work around it.
2083 */
2084 
2085 #ifdef SOLARIS
2086 
2087 struct hostent *
2088 solaris_gethostbyname(name)
2089 	const char *name;
2090 {
2091 # ifdef SOLARIS_2_3
2092 	static struct hostent hp;
2093 	static char buf[1000];
2094 	extern struct hostent *_switch_gethostbyname_r();
2095 
2096 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
2097 # else
2098 	extern struct hostent *__switch_gethostbyname();
2099 
2100 	return __switch_gethostbyname(name);
2101 # endif
2102 }
2103 
2104 struct hostent *
2105 solaris_gethostbyaddr(addr, len, type)
2106 	const char *addr;
2107 	int len;
2108 	int type;
2109 {
2110 # ifdef SOLARIS_2_3
2111 	static struct hostent hp;
2112 	static char buf[1000];
2113 	extern struct hostent *_switch_gethostbyaddr_r();
2114 
2115 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
2116 # else
2117 	extern struct hostent *__switch_gethostbyaddr();
2118 
2119 	return __switch_gethostbyaddr(addr, len, type);
2120 # endif
2121 }
2122 
2123 #endif
2124 /*
2125 **  NI_PROPVAL -- netinfo property value lookup routine
2126 **
2127 **	Parameters:
2128 **		directory -- the Netinfo directory name.
2129 **		propname -- the Netinfo property name.
2130 **
2131 **	Returns:
2132 **		NULL -- if:
2133 **			1. the directory is not found
2134 **			2. the property name is not found
2135 **			3. the property contains multiple values
2136 **			4. some error occured
2137 **		else -- the location of the config file.
2138 **
2139 **	Notes:
2140 **      	Caller should free the return value of ni_proval
2141 */
2142 
2143 #ifdef NETINFO
2144 
2145 # include <netinfo/ni.h>
2146 
2147 # define LOCAL_NETINFO_DOMAIN    "."
2148 # define PARENT_NETINFO_DOMAIN   ".."
2149 # define MAX_NI_LEVELS           256
2150 
2151 char *
2152 ni_propval(directory, propname)
2153 	char *directory;
2154 	char *propname;
2155 {
2156 	char *propval = NULL;
2157 	int i;
2158 	void *ni = NULL;
2159 	void *lastni = NULL;
2160 	ni_status nis;
2161 	ni_id nid;
2162 	ni_namelist ninl;
2163 
2164 	/*
2165 	**  If the passed directory and property name are found
2166 	**  in one of netinfo domains we need to search (starting
2167 	**  from the local domain moving all the way back to the
2168 	**  root domain) set propval to the property's value
2169 	**  and return it.
2170 	*/
2171 
2172 	for (i = 0; i < MAX_NI_LEVELS; ++i)
2173 	{
2174 		if (i == 0)
2175 		{
2176 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
2177 		}
2178 		else
2179 		{
2180 			if (lastni != NULL)
2181 				ni_free(lastni);
2182 			lastni = ni;
2183 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
2184 		}
2185 
2186 		/*
2187 		**  Don't bother if we didn't get a handle on a
2188 		**  proper domain.  This is not necessarily an error.
2189 		**  We would get a positive ni_status if, for instance
2190 		**  we never found the directory or property and tried
2191 		**  to open the parent of the root domain!
2192 		*/
2193 
2194 		if (nis != 0)
2195 			break;
2196 
2197 		/*
2198 		**  Find the path to the server information.
2199 		*/
2200 
2201 		if (ni_pathsearch(ni, &nid, directory) != 0)
2202 			continue;
2203 
2204 		/*
2205 		**  Find "host" information.
2206 		*/
2207 
2208 		if (ni_lookupprop(ni, &nid, propname, &ninl) != 0)
2209 			continue;
2210 
2211 		/*
2212 		**  If there's only one name in
2213 		**  the list, assume we've got
2214 		**  what we want.
2215 		*/
2216 
2217 		if (ninl.ni_namelist_len == 1)
2218 		{
2219 			propval = ni_name_dup(ninl.ni_namelist_val[0]);
2220 			break;
2221 		}
2222 	}
2223 
2224 	/*
2225 	**  Clean up.
2226 	*/
2227 
2228 	if (ni != NULL)
2229 		ni_free(ni);
2230 	if (lastni != NULL && ni != lastni)
2231 		ni_free(lastni);
2232 
2233 	return propval;
2234 }
2235 
2236 #endif /* NETINFO */
2237 /*
2238 **  HARD_SYSLOG -- call syslog repeatedly until it works
2239 **
2240 **	Needed on HP-UX, which apparently doesn't guarantee that
2241 **	syslog succeeds during interrupt handlers.
2242 */
2243 
2244 #ifdef __hpux
2245 
2246 # define MAXSYSLOGTRIES	100
2247 # undef syslog
2248 
2249 # ifdef __STDC__
2250 hard_syslog(int pri, char *msg, ...)
2251 # else
2252 hard_syslog(pri, msg, va_alist)
2253 	int pri;
2254 	char *msg;
2255 	va_dcl
2256 # endif
2257 {
2258 	int i;
2259 	char buf[SYSLOG_BUFSIZE * 2];
2260 	VA_LOCAL_DECL;
2261 
2262 	VA_START(msg);
2263 	vsprintf(buf, msg, ap);
2264 	VA_END;
2265 
2266 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
2267 		continue;
2268 }
2269 
2270 #endif
2271