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