xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68508)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*68508Seric static char sccsid[] = "@(#)conf.c	8.139 (Berkeley) 03/10/95";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1765018Seric # include <netdb.h>
1836928Sbostic # include <pwd.h>
19404Seric 
20294Seric /*
213309Seric **  CONF.C -- Sendmail Configuration Tables.
22294Seric **
23294Seric **	Defines the configuration of this installation.
24294Seric **
251388Seric **	Configuration Variables:
262897Seric **		HdrInfo -- a table describing well-known header fields.
272897Seric **			Each entry has the field name and some flags,
284147Seric **			which are described in sendmail.h.
294093Seric **
304093Seric **	Notes:
314093Seric **		I have tried to put almost all the reasonable
324093Seric **		configuration information into the configuration
334093Seric **		file read at runtime.  My intent is that anything
344093Seric **		here is a function of the version of UNIX you
354093Seric **		are running, or is really static -- for example
364093Seric **		the headers are a superset of widely used
374093Seric **		protocols.  If you find yourself playing with
384093Seric **		this file too much, you may be making a mistake!
39294Seric */
40294Seric 
41294Seric 
42294Seric 
43294Seric 
444437Seric /*
452897Seric **  Header info table
463057Seric **	Final (null) entry contains the flags used for any other field.
474147Seric **
484147Seric **	Not all of these are actually handled specially by sendmail
494147Seric **	at this time.  They are included as placeholders, to let
504147Seric **	you know that "someday" I intend to have sendmail do
514147Seric **	something with them.
522897Seric */
532897Seric 
542897Seric struct hdrinfo	HdrInfo[] =
552897Seric {
568060Seric 		/* originator fields, most to least significant  */
57*68508Seric 	"resent-sender",		H_FROM|H_RESENT,
58*68508Seric 	"resent-from",			H_FROM|H_RESENT,
59*68508Seric 	"resent-reply-to",		H_FROM|H_RESENT,
60*68508Seric 	"sender",			H_FROM,
61*68508Seric 	"from",				H_FROM,
62*68508Seric 	"reply-to",			H_FROM,
63*68508Seric 	"full-name",			H_ACHECK,
64*68508Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
65*68508Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
68*68508Seric 	"to",				H_RCPT,
69*68508Seric 	"resent-to",			H_RCPT|H_RESENT,
70*68508Seric 	"cc",				H_RCPT,
71*68508Seric 	"resent-cc",			H_RCPT|H_RESENT,
72*68508Seric 	"bcc",				H_RCPT|H_STRIPVAL,
73*68508Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
74*68508Seric 	"apparently-to",		H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
77*68508Seric 	"message-id",			0,
78*68508Seric 	"resent-message-id",		H_RESENT,
79*68508Seric 	"message",			H_EOH,
80*68508Seric 	"text",				H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
83*68508Seric 	"date",				0,
84*68508Seric 	"resent-date",			H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
87*68508Seric 	"received",			H_TRACE|H_FORCE,
88*68508Seric 	"x400-received",		H_TRACE|H_FORCE,
89*68508Seric 	"via",				H_TRACE|H_FORCE,
90*68508Seric 	"mail-from",			H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
93*68508Seric 	"comments",			H_FORCE,
94*68508Seric 	"return-path",			H_FORCE|H_ACHECK,
95*68508Seric 	"content-transfer-encoding",	H_CTE,
96*68508Seric 	"content-type",			H_CTYPE,
9758796Seric 
989055Seric 	NULL,			0,
992897Seric };
1004166Seric 
1014166Seric 
1024166Seric 
1034282Seric /*
1044282Seric **  Location of system files/databases/etc.
1054282Seric */
1064282Seric 
10758082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1089039Seric 
1099064Seric 
1109064Seric 
1119039Seric /*
11258082Seric **  Privacy values
11358082Seric */
11458082Seric 
11558082Seric struct prival PrivacyValues[] =
11658082Seric {
11758082Seric 	"public",		PRIV_PUBLIC,
11858082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11958114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
12058082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12158082Seric 	"noexpn",		PRIV_NOEXPN,
12258082Seric 	"novrfy",		PRIV_NOVRFY,
12364333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12464333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12558789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12666783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12758082Seric 	"goaway",		PRIV_GOAWAY,
12858789Seric 	NULL,			0,
12958082Seric };
13058082Seric 
13158082Seric 
13258082Seric 
13358082Seric /*
13424943Seric **  Miscellaneous stuff.
1359039Seric */
1369039Seric 
13724943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13824943Seric /*
13924943Seric **  SETDEFAULTS -- set default values
14024943Seric **
14124943Seric **	Because of the way freezing is done, these must be initialized
14224943Seric **	using direct code.
14324943Seric **
14424943Seric **	Parameters:
14558734Seric **		e -- the default envelope.
14624943Seric **
14724943Seric **	Returns:
14824943Seric **		none.
14924943Seric **
15024943Seric **	Side Effects:
15124943Seric **		Initializes a bunch of global variables to their
15224943Seric **		default values.
15324943Seric */
15424943Seric 
15558737Seric #define DAYS		* 24 * 60 * 60
15658737Seric 
157*68508Seric void
15858734Seric setdefaults(e)
15958734Seric 	register ENVELOPE *e;
16024943Seric {
161*68508Seric 	int i;
162*68508Seric 	extern void inittimeouts();
163*68508Seric 	extern void setdefuser();
164*68508Seric 	extern void setupmaps();
165*68508Seric 	extern void setupmailers();
166*68508Seric 
16757438Seric 	SpaceSub = ' ';				/* option B */
16857438Seric 	QueueLA = 8;				/* option x */
16957438Seric 	RefuseLA = 12;				/* option X */
17057438Seric 	WkRecipFact = 30000L;			/* option y */
17157438Seric 	WkClassFact = 1800L;			/* option z */
17257438Seric 	WkTimeFact = 90000L;			/* option Z */
17357438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17463787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17557438Seric 						/* option F */
17657438Seric 	DefUid = 1;				/* option u */
17757438Seric 	DefGid = 1;				/* option g */
17857438Seric 	CheckpointInterval = 10;		/* option C */
17957438Seric 	MaxHopCount = 25;			/* option h */
18058734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18158734Seric 	e->e_errormode = EM_PRINT;		/* option e */
182*68508Seric 	SevenBitInput = FALSE;			/* option 7 */
18357438Seric 	MaxMciCache = 1;			/* option k */
18457438Seric 	MciCacheTimeout = 300;			/* option K */
18557438Seric 	LogLevel = 9;				/* option L */
186*68508Seric 	inittimeouts(NULL);			/* option r */
18758853Seric 	PrivacyFlags = 0;			/* option p */
188*68508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
189*68508Seric 	for (i = 0; i < MAXTOCLASS; i++)
190*68508Seric 	{
191*68508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
192*68508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
193*68508Seric 	}
194*68508Seric 	ServiceSwitchFile = "/etc/service.switch";
19540973Sbostic 	setdefuser();
19653654Seric 	setupmaps();
19757402Seric 	setupmailers();
19824943Seric }
199294Seric 
20040973Sbostic 
2014326Seric /*
20240973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20340973Sbostic */
20440973Sbostic 
205*68508Seric void
20640973Sbostic setdefuser()
20740973Sbostic {
20840973Sbostic 	struct passwd *defpwent;
20957386Seric 	static char defuserbuf[40];
21040973Sbostic 
21157386Seric 	DefUser = defuserbuf;
21240973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
21357386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21440973Sbostic 	else
21557386Seric 		strcpy(defuserbuf, "nobody");
21640973Sbostic }
21753654Seric /*
21856836Seric **  HOST_MAP_INIT -- initialize host class structures
21956836Seric */
22056836Seric 
221*68508Seric bool	host_map_init __P((MAP *map, char *args));
222*68508Seric 
22356836Seric bool
22460219Seric host_map_init(map, args)
22556836Seric 	MAP *map;
22656836Seric 	char *args;
22756836Seric {
22856836Seric 	register char *p = args;
22956836Seric 
23056836Seric 	for (;;)
23156836Seric 	{
23258050Seric 		while (isascii(*p) && isspace(*p))
23356836Seric 			p++;
23456836Seric 		if (*p != '-')
23556836Seric 			break;
23656836Seric 		switch (*++p)
23756836Seric 		{
23856836Seric 		  case 'a':
23956836Seric 			map->map_app = ++p;
24056836Seric 			break;
24156836Seric 		}
24258050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24356836Seric 			p++;
24456836Seric 		if (*p != '\0')
24556836Seric 			*p++ = '\0';
24656836Seric 	}
24756836Seric 	if (map->map_app != NULL)
24856836Seric 		map->map_app = newstr(map->map_app);
24956836Seric 	return TRUE;
25056836Seric }
25157402Seric /*
25257402Seric **  SETUPMAILERS -- initialize default mailers
25357402Seric */
25456836Seric 
255*68508Seric void
25657402Seric setupmailers()
25757402Seric {
25857402Seric 	char buf[100];
259*68508Seric 	extern void makemailer();
26057402Seric 
261*68508Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u");
26257403Seric 	makemailer(buf);
26357403Seric 
264*68508Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE");
26557402Seric 	makemailer(buf);
26657402Seric 
26757402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26857402Seric 	makemailer(buf);
26957402Seric }
27056836Seric /*
27160207Seric **  SETUPMAPS -- set up map classes
27260207Seric */
27360207Seric 
27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27560207Seric 	{ \
27660207Seric 		extern bool parse __P((MAP *, char *)); \
27760207Seric 		extern bool open __P((MAP *, int)); \
27860207Seric 		extern void close __P((MAP *)); \
27960207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
28060207Seric 		extern void store __P((MAP *, char *, char *)); \
28160207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28260207Seric 		s->s_mapclass.map_cname = name; \
28360207Seric 		s->s_mapclass.map_ext = ext; \
28460207Seric 		s->s_mapclass.map_cflags = flags; \
28560207Seric 		s->s_mapclass.map_parse = parse; \
28660207Seric 		s->s_mapclass.map_open = open; \
28760207Seric 		s->s_mapclass.map_close = close; \
28860207Seric 		s->s_mapclass.map_lookup = lookup; \
28960207Seric 		s->s_mapclass.map_store = store; \
29060207Seric 	}
29160207Seric 
292*68508Seric void
29360207Seric setupmaps()
29460207Seric {
29560207Seric 	register STAB *s;
29660207Seric 
29760207Seric #ifdef NEWDB
29860207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29960207Seric 		map_parseargs, hash_map_open, db_map_close,
30060207Seric 		db_map_lookup, db_map_store);
301*68508Seric 
30260207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, bt_map_open, db_map_close,
30460207Seric 		db_map_lookup, db_map_store);
30560207Seric #endif
30660207Seric 
30760207Seric #ifdef NDBM
30860207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30960207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
31060207Seric 		ndbm_map_lookup, ndbm_map_store);
31160207Seric #endif
31260207Seric 
31360207Seric #ifdef NIS
31460207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
315*68508Seric 		map_parseargs, nis_map_open, null_map_close,
316*68508Seric 		nis_map_lookup, null_map_store);
31760207Seric #endif
31860207Seric 
319*68508Seric #ifdef NISPLUS
320*68508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
321*68508Seric 		map_parseargs, nisplus_map_open, null_map_close,
322*68508Seric 		nisplus_map_lookup, null_map_store);
323*68508Seric #endif
324*68508Seric 
325*68508Seric #ifdef HESIOD
326*68508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
327*68508Seric 		map_parseargs, null_map_open, null_map_close,
328*68508Seric 		hes_map_lookup, null_map_store);
329*68508Seric #endif
330*68508Seric 
331*68508Seric #ifdef NETINFO
332*68508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
333*68508Seric 		map_parseargs, ni_map_open, null_map_close,
334*68508Seric 		ni_map_lookup, null_map_store);
335*68508Seric #endif
336*68508Seric 
337*68508Seric #if 0
338*68508Seric 	MAPDEF("dns", NULL, 0,
339*68508Seric 		dns_map_init, null_map_open, null_map_close,
340*68508Seric 		dns_map_lookup, null_map_store);
341*68508Seric #endif
342*68508Seric 
343*68508Seric #if NAMED_BIND
344*68508Seric 	/* best MX DNS lookup */
345*68508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
346*68508Seric 		map_parseargs, null_map_open, null_map_close,
347*68508Seric 		bestmx_map_lookup, null_map_store);
348*68508Seric #endif
349*68508Seric 
350*68508Seric 	MAPDEF("host", NULL, 0,
351*68508Seric 		host_map_init, null_map_open, null_map_close,
352*68508Seric 		host_map_lookup, null_map_store);
353*68508Seric 
354*68508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
355*68508Seric 		map_parseargs, text_map_open, null_map_close,
356*68508Seric 		text_map_lookup, null_map_store);
357*68508Seric 
35860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
359*68508Seric 		map_parseargs, stab_map_open, null_map_close,
36060207Seric 		stab_map_lookup, stab_map_store);
36160207Seric 
36260207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36360207Seric 		map_parseargs, impl_map_open, impl_map_close,
36460207Seric 		impl_map_lookup, impl_map_store);
36560207Seric 
366*68508Seric 	/* access to system passwd file */
367*68508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
368*68508Seric 		map_parseargs, user_map_open, null_map_close,
369*68508Seric 		user_map_lookup, null_map_store);
37060207Seric 
37160207Seric 	/* dequote map */
37260207Seric 	MAPDEF("dequote", NULL, 0,
37360207Seric 		dequote_init, null_map_open, null_map_close,
37460207Seric 		dequote_map, null_map_store);
37560207Seric 
37660207Seric #if 0
37760207Seric # ifdef USERDB
37860207Seric 	/* user database */
37960207Seric 	MAPDEF("udb", ".db", 0,
38060207Seric 		udb_map_parse, null_map_open, null_map_close,
38160207Seric 		udb_map_lookup, null_map_store);
38260207Seric # endif
38360207Seric #endif
384*68508Seric 
385*68508Seric 	/* sequenced maps */
386*68508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
387*68508Seric 		seq_map_parse, null_map_open, null_map_close,
388*68508Seric 		seq_map_lookup, seq_map_store);
389*68508Seric 
390*68508Seric 	/* switched interface to sequenced maps */
391*68508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
392*68508Seric 		map_parseargs, switch_map_open, null_map_close,
393*68508Seric 		seq_map_lookup, seq_map_store);
39460207Seric }
39560207Seric 
39660207Seric #undef MAPDEF
39760207Seric /*
398*68508Seric **  INITHOSTMAPS -- initial host-dependent maps
399*68508Seric **
400*68508Seric **	This should act as an interface to any local service switch
401*68508Seric **	provided by the host operating system.
402*68508Seric **
403*68508Seric **	Parameters:
404*68508Seric **		none
405*68508Seric **
406*68508Seric **	Returns:
407*68508Seric **		none
408*68508Seric **
409*68508Seric **	Side Effects:
410*68508Seric **		Should define maps "host" and "users" as necessary
411*68508Seric **		for this OS.  If they are not defined, they will get
412*68508Seric **		a default value later.  It should check to make sure
413*68508Seric **		they are not defined first, since it's possible that
414*68508Seric **		the config file has provided an override.
415*68508Seric */
416*68508Seric 
417*68508Seric void
418*68508Seric inithostmaps()
419*68508Seric {
420*68508Seric 	register int i;
421*68508Seric 	int nmaps;
422*68508Seric 	char *maptype[MAXMAPSTACK];
423*68508Seric 	short mapreturn[MAXMAPACTIONS];
424*68508Seric 	char buf[MAXLINE];
425*68508Seric 
426*68508Seric 	/*
427*68508Seric 	**  Set up default hosts maps.
428*68508Seric 	*/
429*68508Seric 
430*68508Seric #if 0
431*68508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
432*68508Seric 	for (i = 0; i < nmaps; i++)
433*68508Seric 	{
434*68508Seric 		if (strcmp(maptype[i], "files") == 0 &&
435*68508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
436*68508Seric 		{
437*68508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
438*68508Seric 			makemapentry(buf);
439*68508Seric 		}
440*68508Seric #if NAMED_BIND
441*68508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
442*68508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
443*68508Seric 		{
444*68508Seric 			strcpy(buf, "hosts.dns dns A");
445*68508Seric 			makemapentry(buf);
446*68508Seric 		}
447*68508Seric #endif
448*68508Seric #ifdef NISPLUS
449*68508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
450*68508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
451*68508Seric 		{
452*68508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
453*68508Seric 			makemapentry(buf);
454*68508Seric 		}
455*68508Seric #endif
456*68508Seric #ifdef NIS
457*68508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
458*68508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
459*68508Seric 		{
460*68508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
461*68508Seric 			makemapentry(buf);
462*68508Seric 		}
463*68508Seric #endif
464*68508Seric 	}
465*68508Seric #endif
466*68508Seric 
467*68508Seric 	/*
468*68508Seric 	**  Make sure we have a host map.
469*68508Seric 	*/
470*68508Seric 
471*68508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
472*68508Seric 	{
473*68508Seric 		/* user didn't initialize: set up host map */
474*68508Seric 		strcpy(buf, "host host");
475*68508Seric #if NAMED_BIND
476*68508Seric 		if (ConfigLevel >= 2)
477*68508Seric 			strcat(buf, " -a.");
478*68508Seric #endif
479*68508Seric 		makemapentry(buf);
480*68508Seric 	}
481*68508Seric 
482*68508Seric 	/*
483*68508Seric 	**  Set up default aliases maps
484*68508Seric 	*/
485*68508Seric 
486*68508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
487*68508Seric 	for (i = 0; i < nmaps; i++)
488*68508Seric 	{
489*68508Seric 		if (strcmp(maptype[i], "files") == 0 &&
490*68508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
491*68508Seric 		{
492*68508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
493*68508Seric 			makemapentry(buf);
494*68508Seric 		}
495*68508Seric #ifdef NISPLUS
496*68508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
497*68508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
498*68508Seric 		{
499*68508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
500*68508Seric 			makemapentry(buf);
501*68508Seric 		}
502*68508Seric #endif
503*68508Seric #ifdef NIS
504*68508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
505*68508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
506*68508Seric 		{
507*68508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
508*68508Seric 			makemapentry(buf);
509*68508Seric 		}
510*68508Seric #endif
511*68508Seric 	}
512*68508Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
513*68508Seric 	{
514*68508Seric 		strcpy(buf, "aliases switch aliases");
515*68508Seric 		makemapentry(buf);
516*68508Seric 	}
517*68508Seric 	strcpy(buf, "switch:aliases");
518*68508Seric 	setalias(buf);
519*68508Seric 
520*68508Seric #if 0		/* "user" map class is a better choice */
521*68508Seric 	/*
522*68508Seric 	**  Set up default users maps.
523*68508Seric 	*/
524*68508Seric 
525*68508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
526*68508Seric 	for (i = 0; i < nmaps; i++)
527*68508Seric 	{
528*68508Seric 		if (strcmp(maptype[i], "files") == 0 &&
529*68508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
530*68508Seric 		{
531*68508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
532*68508Seric 			makemapentry(buf);
533*68508Seric 		}
534*68508Seric #ifdef NISPLUS
535*68508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
536*68508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
537*68508Seric 		{
538*68508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
539*68508Seric 			makemapentry(buf);
540*68508Seric 		}
541*68508Seric #endif
542*68508Seric #ifdef NIS
543*68508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
544*68508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
545*68508Seric 		{
546*68508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
547*68508Seric 			makemapentry(buf);
548*68508Seric 		}
549*68508Seric #endif
550*68508Seric #ifdef HESIOD
551*68508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
552*68508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
553*68508Seric 		{
554*68508Seric 			strcpy(buf, "users.hesiod hesiod");
555*68508Seric 			makemapentry(buf);
556*68508Seric 		}
557*68508Seric #endif
558*68508Seric 	}
559*68508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
560*68508Seric 	{
561*68508Seric 		strcpy(buf, "users switch -m passwd");
562*68508Seric 		makemapentry(buf);
563*68508Seric 	}
564*68508Seric #endif
565*68508Seric }
566*68508Seric /*
567*68508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
568*68508Seric **
569*68508Seric **	This is the system-dependent interface to the service switch.
570*68508Seric **
571*68508Seric **	Parameters:
572*68508Seric **		service -- the name of the service of interest.
573*68508Seric **		maptype -- an out-array of strings containing the types
574*68508Seric **			of access to use for this service.  There can
575*68508Seric **			be at most MAXMAPSTACK types for a single service.
576*68508Seric **		mapreturn -- an out-array of return information bitmaps
577*68508Seric **			for the map.
578*68508Seric **
579*68508Seric **	Returns:
580*68508Seric **		The number of map types filled in, or -1 for failure.
581*68508Seric */
582*68508Seric 
583*68508Seric #ifdef SOLARIS
584*68508Seric # include <nsswitch.h>
585*68508Seric #endif
586*68508Seric 
587*68508Seric #if defined(ultrix) || defined(__osf__)
588*68508Seric # include <sys/svcinfo.h>
589*68508Seric #endif
590*68508Seric 
591*68508Seric int
592*68508Seric switch_map_find(service, maptype, mapreturn)
593*68508Seric 	char *service;
594*68508Seric 	char *maptype[MAXMAPSTACK];
595*68508Seric 	short mapreturn[MAXMAPACTIONS];
596*68508Seric {
597*68508Seric 	register FILE *fp;
598*68508Seric 	int svcno;
599*68508Seric 	static char buf[MAXLINE];
600*68508Seric 
601*68508Seric #ifdef SOLARIS
602*68508Seric 	struct __nsw_switchconfig *nsw_conf;
603*68508Seric 	enum __nsw_parse_err pserr;
604*68508Seric 	struct __nsw_lookup *lk;
605*68508Seric 	int nsw_rc;
606*68508Seric 	static struct __nsw_lookup lkp0 =
607*68508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
608*68508Seric 	static struct __nsw_switchconfig lkp_default =
609*68508Seric 		{ 0, "sendmail", 3, &lkp0 };
610*68508Seric 
611*68508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
612*68508Seric 		lk = lkp_default.lookups;
613*68508Seric 	else
614*68508Seric 		lk = nsw_conf->lookups;
615*68508Seric 	svcno = 0;
616*68508Seric 	while (lk != NULL)
617*68508Seric 	{
618*68508Seric 		maptype[svcno] = lk->service_name;
619*68508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
620*68508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
621*68508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
622*68508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
623*68508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
624*68508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
625*68508Seric 		svcno++;
626*68508Seric 		lk = lk->next;
627*68508Seric 	}
628*68508Seric 	return svcno;
629*68508Seric #endif
630*68508Seric 
631*68508Seric #if defined(ultrix) || defined(__osf__)
632*68508Seric 	struct svcinfo *svcinfo;
633*68508Seric 	int svc;
634*68508Seric 
635*68508Seric 	svcinfo = getsvc();
636*68508Seric 	if (svcinfo == NULL)
637*68508Seric 		goto punt;
638*68508Seric 	if (strcmp(service, "hosts") == 0)
639*68508Seric 		svc = SVC_HOSTS;
640*68508Seric 	else if (strcmp(service, "aliases") == 0)
641*68508Seric 		svc = SVC_ALIASES;
642*68508Seric 	else if (strcmp(service, "passwd") == 0)
643*68508Seric 		svc = SVC_PASSWD;
644*68508Seric 	else
645*68508Seric 		return -1;
646*68508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
647*68508Seric 	{
648*68508Seric 		switch (svcinfo->svcpath[svc][svcno])
649*68508Seric 		{
650*68508Seric 		  case SVC_LOCAL:
651*68508Seric 			maptype[svcno] = "files";
652*68508Seric 			break;
653*68508Seric 
654*68508Seric 		  case SVC_YP:
655*68508Seric 			maptype[svcno] = "nis";
656*68508Seric 			break;
657*68508Seric 
658*68508Seric 		  case SVC_BIND:
659*68508Seric 			maptype[svcno] = "dns";
660*68508Seric 			break;
661*68508Seric 
662*68508Seric #ifdef SVC_HESIOD
663*68508Seric 		  case SVC_HESIOD:
664*68508Seric 			maptype[svcno] = "hesiod";
665*68508Seric 			break;
666*68508Seric #endif
667*68508Seric 
668*68508Seric 		  case SVC_LAST:
669*68508Seric 			return svcno;
670*68508Seric 		}
671*68508Seric 	}
672*68508Seric 	return svcno;
673*68508Seric #endif
674*68508Seric 
675*68508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
676*68508Seric 	/*
677*68508Seric 	**  Fall-back mechanism.
678*68508Seric 	*/
679*68508Seric 
680*68508Seric 	svcno = 0;
681*68508Seric 	fp = fopen(ServiceSwitchFile, "r");
682*68508Seric 	if (fp != NULL)
683*68508Seric 	{
684*68508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
685*68508Seric 		{
686*68508Seric 			register char *p;
687*68508Seric 
688*68508Seric 			p = strpbrk(buf, "#\n");
689*68508Seric 			if (p != NULL)
690*68508Seric 				*p = '\0';
691*68508Seric 			p = strpbrk(buf, " \t");
692*68508Seric 			if (p != NULL)
693*68508Seric 				*p++ = '\0';
694*68508Seric 			if (strcmp(buf, service) != 0)
695*68508Seric 				continue;
696*68508Seric 
697*68508Seric 			/* got the right service -- extract data */
698*68508Seric 			do
699*68508Seric 			{
700*68508Seric 				while (isspace(*p))
701*68508Seric 					p++;
702*68508Seric 				if (*p == '\0')
703*68508Seric 					break;
704*68508Seric 				maptype[svcno++] = p;
705*68508Seric 				p = strpbrk(p, " \t");
706*68508Seric 				if (p != NULL)
707*68508Seric 					*p++ = '\0';
708*68508Seric 			} while (p != NULL);
709*68508Seric 			break;
710*68508Seric 		}
711*68508Seric 		fclose(fp);
712*68508Seric 		return svcno;
713*68508Seric 	}
714*68508Seric #endif
715*68508Seric 
716*68508Seric 	/* if the service file doesn't work, use an absolute fallback */
717*68508Seric   punt:
718*68508Seric 	if (strcmp(service, "aliases") == 0)
719*68508Seric 	{
720*68508Seric 		maptype[0] = "files";
721*68508Seric 		return 1;
722*68508Seric 	}
723*68508Seric 	if (strcmp(service, "hosts") == 0)
724*68508Seric 	{
725*68508Seric # if NAMED_BIND
726*68508Seric 		maptype[svcno++] = "dns";
727*68508Seric # else
728*68508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
729*68508Seric 		/* SunOS */
730*68508Seric 		maptype[svcno++] = "nis";
731*68508Seric #  endif
732*68508Seric # endif
733*68508Seric 		maptype[svcno++] = "files";
734*68508Seric 		return svcno;
735*68508Seric 	}
736*68508Seric 	return -1;
737*68508Seric }
738*68508Seric /*
7399369Seric **  USERNAME -- return the user id of the logged in user.
7409369Seric **
7419369Seric **	Parameters:
7429369Seric **		none.
7439369Seric **
7449369Seric **	Returns:
7459369Seric **		The login name of the logged in user.
7469369Seric **
7479369Seric **	Side Effects:
7489369Seric **		none.
7499369Seric **
7509369Seric **	Notes:
7519369Seric **		The return value is statically allocated.
7529369Seric */
7539369Seric 
7549369Seric char *
7559369Seric username()
7569369Seric {
75717469Seric 	static char *myname = NULL;
7589369Seric 	extern char *getlogin();
75919904Smiriam 	register struct passwd *pw;
7609369Seric 
76117469Seric 	/* cache the result */
76217469Seric 	if (myname == NULL)
76317469Seric 	{
76417469Seric 		myname = getlogin();
76517469Seric 		if (myname == NULL || myname[0] == '\0')
76617469Seric 		{
76763787Seric 			pw = getpwuid(RealUid);
76817469Seric 			if (pw != NULL)
76940993Sbostic 				myname = newstr(pw->pw_name);
77017469Seric 		}
77119904Smiriam 		else
77219904Smiriam 		{
77363787Seric 			uid_t uid = RealUid;
77419873Smiriam 
77540993Sbostic 			myname = newstr(myname);
77640993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
77758736Seric 			      (uid != 0 && uid != pw->pw_uid))
77819904Smiriam 			{
77958736Seric 				pw = getpwuid(uid);
78024945Seric 				if (pw != NULL)
78140993Sbostic 					myname = newstr(pw->pw_name);
78219873Smiriam 			}
78319873Smiriam 		}
78417469Seric 		if (myname == NULL || myname[0] == '\0')
78517469Seric 		{
78658151Seric 			syserr("554 Who are you?");
78717469Seric 			myname = "postmaster";
78817469Seric 		}
78917469Seric 	}
79017469Seric 
79117469Seric 	return (myname);
7929369Seric }
7939369Seric /*
7944190Seric **  TTYPATH -- Get the path of the user's tty
795294Seric **
796294Seric **	Returns the pathname of the user's tty.  Returns NULL if
797294Seric **	the user is not logged in or if s/he has write permission
798294Seric **	denied.
799294Seric **
800294Seric **	Parameters:
801294Seric **		none
802294Seric **
803294Seric **	Returns:
804294Seric **		pathname of the user's tty.
805294Seric **		NULL if not logged in or write permission denied.
806294Seric **
807294Seric **	Side Effects:
808294Seric **		none.
809294Seric **
810294Seric **	WARNING:
811294Seric **		Return value is in a local buffer.
812294Seric **
813294Seric **	Called By:
814294Seric **		savemail
815294Seric */
816294Seric 
817294Seric char *
818294Seric ttypath()
819294Seric {
820294Seric 	struct stat stbuf;
821294Seric 	register char *pathn;
822294Seric 	extern char *ttyname();
8234081Seric 	extern char *getlogin();
824294Seric 
825294Seric 	/* compute the pathname of the controlling tty */
8269369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8279369Seric 	    (pathn = ttyname(0)) == NULL)
828294Seric 	{
829294Seric 		errno = 0;
830294Seric 		return (NULL);
831294Seric 	}
832294Seric 
833294Seric 	/* see if we have write permission */
8342967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
835294Seric 	{
836294Seric 		errno = 0;
837294Seric 		return (NULL);
838294Seric 	}
839294Seric 
840294Seric 	/* see if the user is logged in */
841294Seric 	if (getlogin() == NULL)
842294Seric 		return (NULL);
843294Seric 
844294Seric 	/* looks good */
845294Seric 	return (pathn);
846294Seric }
8472967Seric /*
8482967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8492967Seric **
8502967Seric **	This routine can be supplied on a per-installation basis
8512967Seric **	to determine whether a person is allowed to send a message.
8522967Seric **	This allows restriction of certain types of internet
8532967Seric **	forwarding or registration of users.
8542967Seric **
8552967Seric **	If the hosts are found to be incompatible, an error
85657454Seric **	message should be given using "usrerr" and 0 should
8572967Seric **	be returned.
8582967Seric **
859*68508Seric **	EF_NORETURN can be set in e->e_flags to suppress the return-to-sender
8604288Seric **	function; this should be done on huge messages.
8614288Seric **
8622967Seric **	Parameters:
8632967Seric **		to -- the person being sent to.
8642967Seric **
8652967Seric **	Returns:
86657459Seric **		an exit status
8672967Seric **
8682967Seric **	Side Effects:
8692967Seric **		none (unless you include the usrerr stuff)
8702967Seric */
8712967Seric 
872*68508Seric int
87355012Seric checkcompat(to, e)
8742967Seric 	register ADDRESS *to;
87555012Seric 	register ENVELOPE *e;
8762967Seric {
87712133Seric # ifdef lint
87812133Seric 	if (to == NULL)
87912133Seric 		to++;
88063834Seric # endif /* lint */
88164402Seric 
88264402Seric 	if (tTd(49, 1))
88364402Seric 		printf("checkcompat(to=%s, from=%s)\n",
88464402Seric 			to->q_paddr, e->e_from.q_paddr);
88564402Seric 
88610698Seric # ifdef EXAMPLE_CODE
88710698Seric 	/* this code is intended as an example only */
8884437Seric 	register STAB *s;
8894437Seric 
8904437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
891*68508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
8929369Seric 	    to->q_mailer == s->s_mailer)
8934437Seric 	{
89458151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
895*68508Seric 		/* e->e_flags |= EF_NORETURN; to supress return copy */
89657459Seric 		return (EX_UNAVAILABLE);
8974437Seric 	}
89856795Seric # endif /* EXAMPLE_CODE */
89957459Seric 	return (EX_OK);
9002967Seric }
9019369Seric /*
90264035Seric **  SETSIGNAL -- set a signal handler
90364035Seric **
90464035Seric **	This is essentially old BSD "signal(3)".
90564035Seric */
90664035Seric 
90764561Seric sigfunc_t
90864035Seric setsignal(sig, handler)
90964035Seric 	int sig;
91064561Seric 	sigfunc_t handler;
91164035Seric {
91264380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
91364035Seric 	return signal(sig, handler);
91464035Seric #else
91564035Seric 	struct sigaction n, o;
91664035Seric 
91764035Seric 	bzero(&n, sizeof n);
91864035Seric 	n.sa_handler = handler;
919*68508Seric # ifdef SA_RESTART
920*68508Seric 	n.sa_flags = SA_RESTART;
921*68508Seric # endif
92264035Seric 	if (sigaction(sig, &n, &o) < 0)
92364035Seric 		return SIG_ERR;
92464035Seric 	return o.sa_handler;
92564035Seric #endif
92664035Seric }
92764035Seric /*
9289369Seric **  HOLDSIGS -- arrange to hold all signals
9299369Seric **
9309369Seric **	Parameters:
9319369Seric **		none.
9329369Seric **
9339369Seric **	Returns:
9349369Seric **		none.
9359369Seric **
9369369Seric **	Side Effects:
9379369Seric **		Arranges that signals are held.
9389369Seric */
9399369Seric 
940*68508Seric void
9419369Seric holdsigs()
9429369Seric {
9439369Seric }
9449369Seric /*
9459369Seric **  RLSESIGS -- arrange to release all signals
9469369Seric **
9479369Seric **	This undoes the effect of holdsigs.
9489369Seric **
9499369Seric **	Parameters:
9509369Seric **		none.
9519369Seric **
9529369Seric **	Returns:
9539369Seric **		none.
9549369Seric **
9559369Seric **	Side Effects:
9569369Seric **		Arranges that signals are released.
9579369Seric */
9589369Seric 
959*68508Seric void
9609369Seric rlsesigs()
9619369Seric {
9629369Seric }
96314872Seric /*
96464705Seric **  INIT_MD -- do machine dependent initializations
96564705Seric **
96664705Seric **	Systems that have global modes that should be set should do
96764705Seric **	them here rather than in main.
96864705Seric */
96964705Seric 
97064705Seric #ifdef _AUX_SOURCE
97164705Seric # include	<compat.h>
97264705Seric #endif
97364705Seric 
974*68508Seric void
97564825Seric init_md(argc, argv)
97664825Seric 	int argc;
97764825Seric 	char **argv;
97864705Seric {
97964705Seric #ifdef _AUX_SOURCE
98064705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
98164705Seric #endif
982*68508Seric 
983*68508Seric #ifdef VENDOR_DEFAULT
984*68508Seric 	VendorCode = VENDOR_DEFAULT;
985*68508Seric #else
986*68508Seric 	VendorCode = VENDOR_BERKELEY;
987*68508Seric #endif
98864705Seric }
98964705Seric /*
990*68508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
991*68508Seric **
992*68508Seric **	Called once, on startup.
993*68508Seric **
994*68508Seric **	Parameters:
995*68508Seric **		e -- the global envelope.
996*68508Seric **
997*68508Seric **	Returns:
998*68508Seric **		none.
999*68508Seric **
1000*68508Seric **	Side Effects:
1001*68508Seric **		vendor-dependent.
1002*68508Seric */
1003*68508Seric 
1004*68508Seric void
1005*68508Seric init_vendor_macros(e)
1006*68508Seric 	register ENVELOPE *e;
1007*68508Seric {
1008*68508Seric }
1009*68508Seric /*
101014872Seric **  GETLA -- get the current load average
101114872Seric **
101214881Seric **	This code stolen from la.c.
101314881Seric **
101414872Seric **	Parameters:
101514872Seric **		none.
101614872Seric **
101714872Seric **	Returns:
101814872Seric **		The current load average as an integer.
101914872Seric **
102014872Seric **	Side Effects:
102114872Seric **		none.
102214872Seric */
102314872Seric 
102451920Seric /* try to guess what style of load average we have */
102551920Seric #define LA_ZERO		1	/* always return load average as zero */
102664360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
102751920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
102851920Seric #define LA_SUBR		4	/* call getloadavg */
102964295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
103064360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
103166301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
103214872Seric 
103363962Seric /* do guesses based on general OS type */
103451920Seric #ifndef LA_TYPE
103563962Seric # define LA_TYPE	LA_ZERO
103651920Seric #endif
103751920Seric 
103864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
103951920Seric 
104014872Seric #include <nlist.h>
104114872Seric 
1042*68508Seric #ifdef IRIX64
1043*68508Seric # define nlist		nlist64
1044*68508Seric #endif
1045*68508Seric 
104651920Seric #ifndef LA_AVENRUN
104763962Seric # ifdef SYSTEM5
104863962Seric #  define LA_AVENRUN	"avenrun"
104963962Seric # else
105063962Seric #  define LA_AVENRUN	"_avenrun"
105163962Seric # endif
105251920Seric #endif
105351920Seric 
105451920Seric /* _PATH_UNIX should be defined in <paths.h> */
105551920Seric #ifndef _PATH_UNIX
105663962Seric # if defined(SYSTEM5)
105763962Seric #  define _PATH_UNIX	"/unix"
105863962Seric # else
105963962Seric #  define _PATH_UNIX	"/vmunix"
106063962Seric # endif
106151920Seric #endif
106251920Seric 
106314872Seric struct	nlist Nl[] =
106414872Seric {
106551920Seric 	{ LA_AVENRUN },
106614872Seric #define	X_AVENRUN	0
106714872Seric 	{ 0 },
106814872Seric };
106914872Seric 
107060583Seric #ifndef FSHIFT
107160583Seric # if defined(unixpc)
107260583Seric #  define FSHIFT	5
107360583Seric # endif
107457736Seric 
107566763Seric # if defined(__alpha) || defined(IRIX)
107660583Seric #  define FSHIFT	10
107760583Seric # endif
1078*68508Seric 
1079*68508Seric # if defined(_AIX3)
1080*68508Seric #  define FSHIFT	16
1081*68508Seric # endif
108266788Seric #endif
108357977Seric 
108466788Seric #ifndef FSHIFT
108566788Seric # define FSHIFT		8
108657736Seric #endif
108760583Seric 
108866788Seric #ifndef FSCALE
108966788Seric # define FSCALE		(1 << FSHIFT)
109051920Seric #endif
109140930Srick 
109214872Seric getla()
109314872Seric {
109414872Seric 	static int kmem = -1;
109551920Seric #if LA_TYPE == LA_INT
109624943Seric 	long avenrun[3];
109751920Seric #else
109864360Seric # if LA_TYPE == LA_SHORT
109964360Seric 	short avenrun[3];
110064360Seric # else
110151920Seric 	double avenrun[3];
110264360Seric # endif
110351920Seric #endif
110425615Seric 	extern off_t lseek();
110557736Seric 	extern int errno;
110614872Seric 
110714872Seric 	if (kmem < 0)
110814872Seric 	{
110924945Seric 		kmem = open("/dev/kmem", 0, 0);
111014872Seric 		if (kmem < 0)
111157736Seric 		{
111257736Seric 			if (tTd(3, 1))
111357736Seric 				printf("getla: open(/dev/kmem): %s\n",
111457736Seric 					errstring(errno));
111514872Seric 			return (-1);
111657736Seric 		}
111751920Seric 		(void) fcntl(kmem, F_SETFD, 1);
1118*68508Seric #ifdef _AIX3
1119*68508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
1120*68508Seric #else
112157736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
1122*68508Seric #endif
112357736Seric 		{
112457736Seric 			if (tTd(3, 1))
112557736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
112657736Seric 					errstring(errno));
112714872Seric 			return (-1);
112857736Seric 		}
112959253Seric 		if (Nl[X_AVENRUN].n_value == 0)
113059253Seric 		{
113159253Seric 			if (tTd(3, 1))
113259253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
113359253Seric 					_PATH_UNIX, LA_AVENRUN);
113459253Seric 			return (-1);
113559253Seric 		}
1136*68508Seric #ifdef NAMELISTMASK
1137*68508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
113866763Seric #endif
113914872Seric 	}
114057736Seric 	if (tTd(3, 20))
114157736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
1142*68508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
114323118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
114419967Seric 	{
114519967Seric 		/* thank you Ian */
114657736Seric 		if (tTd(3, 1))
114757736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
114819967Seric 		return (-1);
114919967Seric 	}
115064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
115157736Seric 	if (tTd(3, 5))
115257736Seric 	{
115357736Seric 		printf("getla: avenrun = %d", avenrun[0]);
115457736Seric 		if (tTd(3, 15))
115557736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
115657736Seric 		printf("\n");
115757736Seric 	}
115857736Seric 	if (tTd(3, 1))
115957736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116024943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116151920Seric #else
116257736Seric 	if (tTd(3, 5))
116357736Seric 	{
116457736Seric 		printf("getla: avenrun = %g", avenrun[0]);
116557736Seric 		if (tTd(3, 15))
116657736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
116757736Seric 		printf("\n");
116857736Seric 	}
116957736Seric 	if (tTd(3, 1))
117057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
117151920Seric 	return ((int) (avenrun[0] + 0.5));
117251920Seric #endif
117314872Seric }
117414872Seric 
117551773Seric #else
117651920Seric #if LA_TYPE == LA_SUBR
117751773Seric 
117864718Seric #ifdef DGUX
117964718Seric 
118064718Seric #include <sys/dg_sys_info.h>
118164718Seric 
1182*68508Seric int
1183*68508Seric getla()
118464718Seric {
118564718Seric 	struct dg_sys_info_load_info load_info;
118664718Seric 
118764718Seric 	dg_sys_info((long *)&load_info,
118864718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
118964718Seric 
1190*68508Seric         if (tTd(3, 1))
1191*68508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1192*68508Seric 
119364718Seric 	return((int) (load_info.one_minute + 0.5));
119464718Seric }
119564718Seric 
119664718Seric #else
1197*68508Seric # ifdef __hpux
119864718Seric 
1199*68508Seric #  include <sys/param.h>
1200*68508Seric #  include <sys/pstat.h>
1201*68508Seric 
1202*68508Seric int
120351773Seric getla()
120451773Seric {
1205*68508Seric 	struct pst_dynamic pstd;
1206*68508Seric 
1207*68508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1208*68508Seric 			     (size_t) 1 ,0) == -1)
1209*68508Seric 		return 0;
1210*68508Seric 
1211*68508Seric         if (tTd(3, 1))
1212*68508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1213*68508Seric 
1214*68508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
1215*68508Seric }
1216*68508Seric 
1217*68508Seric # else
1218*68508Seric 
1219*68508Seric int
1220*68508Seric getla()
1221*68508Seric {
122251920Seric 	double avenrun[3];
122351920Seric 
122451920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
122557736Seric 	{
122657736Seric 		if (tTd(3, 1))
122757736Seric 			perror("getla: getloadavg failed:");
122851920Seric 		return (-1);
122957736Seric 	}
123057736Seric 	if (tTd(3, 1))
123157736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
123251920Seric 	return ((int) (avenrun[0] + 0.5));
123351773Seric }
123451773Seric 
1235*68508Seric # endif /* __hpux */
123664718Seric #endif /* DGUX */
123751773Seric #else
123864295Seric #if LA_TYPE == LA_MACH
123951773Seric 
124064295Seric /*
124165173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
124264295Seric */
124364295Seric 
124465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
124565173Seric # include <mach/mach.h>
124665173Seric #else
124765173Seric # include <mach.h>
124865173Seric #endif
124964295Seric 
125051773Seric getla()
125151773Seric {
125264295Seric 	processor_set_t default_set;
125364295Seric 	kern_return_t error;
125464295Seric 	unsigned int info_count;
125564295Seric 	struct processor_set_basic_info info;
125664295Seric 	host_t host;
125764295Seric 
125864295Seric 	error = processor_set_default(host_self(), &default_set);
125964295Seric 	if (error != KERN_SUCCESS)
126064295Seric 		return -1;
126164295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
126264295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
126364295Seric 			       &host, (processor_set_info_t)&info,
126464295Seric 			       &info_count) != KERN_SUCCESS)
126564295Seric 	{
126664295Seric 		return -1;
126764295Seric 	}
126864295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
126964295Seric }
127064295Seric 
127164295Seric 
127264295Seric #else
127366313Seric #if LA_TYPE == LA_PROCSTR
127464295Seric 
127566301Seric /*
127666301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
127766301Seric **  in a format like "0.15 0.12 0.06".
127866301Seric **
127966301Seric **	Initially intended for Linux.  This has been in the kernel
128066301Seric **	since at least 0.99.15.
128166301Seric */
128266301Seric 
128366301Seric # ifndef _PATH_LOADAVG
128466301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
128566301Seric # endif
128666301Seric 
128766301Seric int
128864295Seric getla()
128964295Seric {
129066319Seric 	double avenrun;
129166301Seric 	register int result;
129266319Seric 	FILE *fp;
129366301Seric 
129466319Seric 	fp = fopen(_PATH_LOADAVG, "r");
129566319Seric 	if (fp == NULL)
129666301Seric 	{
129766319Seric 		if (tTd(3, 1))
129866319Seric 			printf("getla: fopen(%s): %s\n",
129966319Seric 				_PATH_LOADAVG, errstring(errno));
130066319Seric 		return -1;
130166301Seric 	}
130266301Seric 	result = fscanf(fp, "%lf", &avenrun);
130366319Seric 	fclose(fp);
130466301Seric 	if (result != 1)
130566301Seric 	{
130666310Seric 		if (tTd(3, 1))
130766301Seric 			printf("getla: fscanf() = %d: %s\n",
130866301Seric 				result, errstring(errno));
130966301Seric 		return -1;
131066301Seric 	}
131166301Seric 
131257736Seric 	if (tTd(3, 1))
131366301Seric 		printf("getla(): %.2f\n", avenrun);
131466301Seric 
131566301Seric 	return ((int) (avenrun + 0.5));
131666301Seric }
131766301Seric 
131866301Seric #else
131966301Seric 
132066301Seric getla()
132166301Seric {
132266301Seric 	if (tTd(3, 1))
132357736Seric 		printf("getla: ZERO\n");
132451920Seric 	return (0);
132551773Seric }
132651773Seric 
132751773Seric #endif
132851773Seric #endif
132964295Seric #endif
133066301Seric #endif
133166045Seric 
133266045Seric 
133366045Seric /*
133466045Seric  * Copyright 1989 Massachusetts Institute of Technology
133566045Seric  *
133666045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
133766045Seric  * documentation for any purpose is hereby granted without fee, provided that
133866045Seric  * the above copyright notice appear in all copies and that both that
133966045Seric  * copyright notice and this permission notice appear in supporting
134066045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
134166045Seric  * publicity pertaining to distribution of the software without specific,
134266045Seric  * written prior permission.  M.I.T. makes no representations about the
134366045Seric  * suitability of this software for any purpose.  It is provided "as is"
134466045Seric  * without express or implied warranty.
134566045Seric  *
134666045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
134766045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
134866045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134966045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
135066045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
135166045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
135266045Seric  *
135366045Seric  * Authors:  Many and varied...
135466045Seric  */
135566045Seric 
135666045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
135766045Seric #ifndef lint
135866045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
135966045Seric #endif /* !lint */
136066045Seric 
136166179Seric #ifdef apollo
136266045Seric # undef volatile
136366045Seric #    include <apollo/base.h>
136466045Seric 
136566045Seric /* ARGSUSED */
136666045Seric int getloadavg( call_data )
136766045Seric      caddr_t	call_data;	/* pointer to (double) return value */
136866045Seric {
136966045Seric      double *avenrun = (double *) call_data;
137066045Seric      int i;
137166045Seric      status_$t      st;
137266045Seric      long loadav[3];
137366045Seric      proc1_$get_loadav(loadav, &st);
137466045Seric      *avenrun = loadav[0] / (double) (1 << 16);
137566045Seric      return(0);
137666045Seric }
137766045Seric #   endif /* apollo */
137824943Seric /*
137924943Seric **  SHOULDQUEUE -- should this message be queued or sent?
138024943Seric **
138124943Seric **	Compares the message cost to the load average to decide.
138224943Seric **
138324943Seric **	Parameters:
138424943Seric **		pri -- the priority of the message in question.
138557438Seric **		ctime -- the message creation time.
138624943Seric **
138724943Seric **	Returns:
138824943Seric **		TRUE -- if this message should be queued up for the
138924943Seric **			time being.
139024943Seric **		FALSE -- if the load is low enough to send this message.
139124943Seric **
139224943Seric **	Side Effects:
139324943Seric **		none.
139424943Seric */
139524943Seric 
139624943Seric bool
139757438Seric shouldqueue(pri, ctime)
139824943Seric 	long pri;
139957438Seric 	time_t ctime;
140024943Seric {
1401*68508Seric 	bool rval;
1402*68508Seric 
1403*68508Seric 	if (tTd(3, 30))
1404*68508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
140551920Seric 	if (CurrentLA < QueueLA)
1406*68508Seric 	{
1407*68508Seric 		if (tTd(3, 30))
1408*68508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
140924943Seric 		return (FALSE);
1410*68508Seric 	}
141158132Seric 	if (CurrentLA >= RefuseLA)
1412*68508Seric 	{
1413*68508Seric 		if (tTd(3, 30))
1414*68508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
141558132Seric 		return (TRUE);
1416*68508Seric 	}
1417*68508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
1418*68508Seric 	if (tTd(3, 30))
1419*68508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
1420*68508Seric 	return rval;
142124943Seric }
142224943Seric /*
142353037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
142453037Seric **
142553037Seric **	Parameters:
142653037Seric **		none.
142753037Seric **
142853037Seric **	Returns:
142953037Seric **		TRUE if incoming SMTP connections should be refused
143053037Seric **			(for now).
143153037Seric **		FALSE if we should accept new work.
143253037Seric **
143353037Seric **	Side Effects:
143453037Seric **		none.
143553037Seric */
143653037Seric 
143753037Seric bool
143853037Seric refuseconnections()
143953037Seric {
1440*68508Seric 	extern bool enoughspace();
1441*68508Seric 
144259156Seric #ifdef XLA
144359156Seric 	if (!xla_smtp_ok())
144459156Seric 		return TRUE;
144559156Seric #endif
144659156Seric 
144753037Seric 	/* this is probably too simplistic */
1448*68508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
144953037Seric }
145053037Seric /*
145124943Seric **  SETPROCTITLE -- set process title for ps
145224943Seric **
145324943Seric **	Parameters:
145458674Seric **		fmt -- a printf style format string.
145558674Seric **		a, b, c -- possible parameters to fmt.
145624943Seric **
145724943Seric **	Returns:
145824943Seric **		none.
145924943Seric **
146024943Seric **	Side Effects:
146124943Seric **		Clobbers argv of our main procedure so ps(1) will
146224943Seric **		display the title.
146324943Seric */
146424943Seric 
1465*68508Seric #define SPT_NONE	0	/* don't use it at all */
1466*68508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
1467*68508Seric #define SPT_BUILTIN	2	/* use libc builtin */
1468*68508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
1469*68508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
1470*68508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
1471*68508Seric 
1472*68508Seric #ifndef SPT_TYPE
1473*68508Seric # define SPT_TYPE	SPT_REUSEARGV
1474*68508Seric #endif
1475*68508Seric 
1476*68508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
1477*68508Seric 
1478*68508Seric # if SPT_TYPE == SPT_PSTAT
147958689Seric #  include <sys/pstat.h>
148058689Seric # endif
1481*68508Seric # if SPT_TYPE == SPT_PSSTRINGS
148259732Seric #  include <machine/vmparam.h>
148359732Seric #  include <sys/exec.h>
1484*68508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
1485*68508Seric #   undef SPT_TYPE
1486*68508Seric #   define SPT_TYPE	SPT_REUSEARGV
148764247Seric #  endif
148859732Seric # endif
1489*68508Seric 
1490*68508Seric # if SPT_TYPE == SPT_PSSTRINGS
1491*68508Seric #  define SETPROC_STATIC	static
1492*68508Seric # else
149359732Seric #  define SETPROC_STATIC
149459732Seric # endif
149567626Seric 
1496*68508Seric # ifndef SPT_PADCHAR
1497*68508Seric #  define SPT_PADCHAR	' '
1498*68508Seric # endif
149958689Seric 
1500*68508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
150164843Seric 
1502*68508Seric #if SPT_TYPE != SPT_BUILTIN
1503*68508Seric 
150424943Seric /*VARARGS1*/
1505*68508Seric void
1506*68508Seric # ifdef __STDC__
150757642Seric setproctitle(char *fmt, ...)
1508*68508Seric # else
150957642Seric setproctitle(fmt, va_alist)
151024943Seric 	char *fmt;
151157642Seric 	va_dcl
1512*68508Seric # endif
151324943Seric {
1514*68508Seric # if SPT_TYPE != SPT_NONE
151524943Seric 	register char *p;
151625049Seric 	register int i;
151759732Seric 	SETPROC_STATIC char buf[MAXLINE];
151856852Seric 	VA_LOCAL_DECL
1519*68508Seric #  if SPT_TYPE == SPT_PSTAT
152058689Seric 	union pstun pst;
152158689Seric #  endif
152224943Seric 	extern char **Argv;
152324943Seric 	extern char *LastArgv;
152424943Seric 
152558674Seric 	p = buf;
152624943Seric 
152758674Seric 	/* print sendmail: heading for grep */
152858674Seric 	(void) strcpy(p, "sendmail: ");
152958674Seric 	p += strlen(p);
153024943Seric 
153158674Seric 	/* print the argument string */
153258674Seric 	VA_START(fmt);
153358674Seric 	(void) vsprintf(p, fmt, ap);
153456852Seric 	VA_END;
153554996Seric 
153658674Seric 	i = strlen(buf);
153758689Seric 
1538*68508Seric #  if SPT_TYPE == SPT_PSTAT
153958689Seric 	pst.pst_command = buf;
154058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
154158689Seric #  else
1542*68508Seric #   if SPT_TYPE == SPT_PSSTRINGS
154359732Seric 	PS_STRINGS->ps_nargvstr = 1;
154459732Seric 	PS_STRINGS->ps_argvstr = buf;
154559732Seric #   else
154654996Seric 	if (i > LastArgv - Argv[0] - 2)
154725049Seric 	{
154854996Seric 		i = LastArgv - Argv[0] - 2;
154958674Seric 		buf[i] = '\0';
155025049Seric 	}
155158674Seric 	(void) strcpy(Argv[0], buf);
155254997Seric 	p = &Argv[0][i];
155324943Seric 	while (p < LastArgv)
1554*68508Seric 		*p++ = SPT_PADCHAR;
1555*68508Seric 	Argv[1] = NULL;
1556*68508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
1557*68508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
1558*68508Seric # endif /* SPT_TYPE != SPT_NONE */
155924943Seric }
156065982Seric 
1561*68508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
156225698Seric /*
156325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
156425698Seric **
156525698Seric **	Parameters:
1566*68508Seric **		sig -- the signal that got us here (unused).
156725698Seric **
156825698Seric **	Returns:
156925698Seric **		none.
157025698Seric **
157125698Seric **	Side Effects:
157225698Seric **		Picks up extant zombies.
157325698Seric */
157425698Seric 
157546928Sbostic void
1576*68508Seric reapchild(sig)
1577*68508Seric 	int sig;
157825698Seric {
157964738Seric 	int olderrno = errno;
158063966Seric # ifdef HASWAITPID
158160219Seric 	auto int status;
158260560Seric 	int count;
158360560Seric 	int pid;
158460219Seric 
158560560Seric 	count = 0;
158660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
158760560Seric 	{
158860560Seric 		if (count++ > 1000)
158960560Seric 		{
159066748Seric #ifdef LOG
159160560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
159260560Seric 				pid, status);
159366748Seric #endif
159460560Seric 			break;
159560560Seric 		}
159660560Seric 	}
159760219Seric # else
159825698Seric # ifdef WNOHANG
159925698Seric 	union wait status;
160025698Seric 
160163753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
160225698Seric 		continue;
160356795Seric # else /* WNOHANG */
160425698Seric 	auto int status;
160525698Seric 
160660219Seric 	while (wait(&status) > 0)
160725698Seric 		continue;
160856795Seric # endif /* WNOHANG */
160960219Seric # endif
161064035Seric # ifdef SYS5SIGNALS
161164035Seric 	(void) setsignal(SIGCHLD, reapchild);
161258061Seric # endif
161364738Seric 	errno = olderrno;
161425698Seric }
161555418Seric /*
161655418Seric **  UNSETENV -- remove a variable from the environment
161755418Seric **
161855418Seric **	Not needed on newer systems.
161955418Seric **
162055418Seric **	Parameters:
162155418Seric **		name -- the string name of the environment variable to be
162255418Seric **			deleted from the current environment.
162355418Seric **
162455418Seric **	Returns:
162555418Seric **		none.
162655418Seric **
162755418Seric **	Globals:
162855418Seric **		environ -- a pointer to the current environment.
162955418Seric **
163055418Seric **	Side Effects:
163155418Seric **		Modifies environ.
163255418Seric */
163355418Seric 
163463962Seric #ifndef HASUNSETENV
163555418Seric 
163655418Seric void
163755418Seric unsetenv(name)
163855418Seric 	char *name;
163955418Seric {
164055418Seric 	extern char **environ;
164155418Seric 	register char **pp;
164255418Seric 	int len = strlen(name);
164355418Seric 
164455418Seric 	for (pp = environ; *pp != NULL; pp++)
164555418Seric 	{
164655418Seric 		if (strncmp(name, *pp, len) == 0 &&
164755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
164855418Seric 			break;
164955418Seric 	}
165055418Seric 
165155418Seric 	for (; *pp != NULL; pp++)
165255418Seric 		*pp = pp[1];
165355418Seric }
165455418Seric 
165563962Seric #endif
165656215Seric /*
165756215Seric **  GETDTABLESIZE -- return number of file descriptors
165856215Seric **
165956215Seric **	Only on non-BSD systems
166056215Seric **
166156215Seric **	Parameters:
166256215Seric **		none
166356215Seric **
166456215Seric **	Returns:
166556215Seric **		size of file descriptor table
166656215Seric **
166756215Seric **	Side Effects:
166856215Seric **		none
166956215Seric */
167056215Seric 
167163787Seric #ifdef SOLARIS
167263787Seric # include <sys/resource.h>
167363787Seric #endif
167456215Seric 
167556215Seric int
167663787Seric getdtsize()
167756215Seric {
167863787Seric #ifdef RLIMIT_NOFILE
167963787Seric 	struct rlimit rl;
168063787Seric 
168163787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
168263787Seric 		return rl.rlim_cur;
168363787Seric #endif
168463787Seric 
168564031Seric # ifdef HASGETDTABLESIZE
168664031Seric 	return getdtablesize();
168764031Seric # else
168864031Seric #  ifdef _SC_OPEN_MAX
168958689Seric 	return sysconf(_SC_OPEN_MAX);
169063787Seric #  else
169156215Seric 	return NOFILE;
169263787Seric #  endif
169358689Seric # endif
169456215Seric }
169557631Seric /*
169657631Seric **  UNAME -- get the UUCP name of this system.
169757631Seric */
169857631Seric 
169957943Seric #ifndef HASUNAME
170057631Seric 
170157631Seric int
170257631Seric uname(name)
170357631Seric 	struct utsname *name;
170457631Seric {
170557631Seric 	FILE *file;
170657631Seric 	char *n;
170757631Seric 
170857631Seric 	name->nodename[0] = '\0';
170957631Seric 
171057661Seric 	/* try /etc/whoami -- one line with the node name */
171157631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
171257631Seric 	{
171357661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
171457631Seric 		(void) fclose(file);
171557661Seric 		n = strchr(name->nodename, '\n');
171657631Seric 		if (n != NULL)
171757631Seric 			*n = '\0';
171857631Seric 		if (name->nodename[0] != '\0')
171957631Seric 			return (0);
172057631Seric 	}
172157631Seric 
172257661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
172357631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
172457631Seric 	{
172557631Seric 		char buf[MAXLINE];
172657631Seric 
172757631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
172857631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
172957631Seric 					NODE_LENGTH, name->nodename) > 0)
173057631Seric 				break;
173157631Seric 		(void) fclose(file);
173257631Seric 		if (name->nodename[0] != '\0')
173357631Seric 			return (0);
173457631Seric 	}
173557631Seric 
173657631Seric #ifdef TRUST_POPEN
173757631Seric 	/*
173857631Seric 	**  Popen is known to have security holes.
173957631Seric 	*/
174057631Seric 
174157661Seric 	/* try uuname -l to return local name */
174257631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
174357631Seric 	{
174457661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
174557631Seric 		(void) pclose(file);
174657661Seric 		n = strchr(name, '\n');
174757631Seric 		if (n != NULL)
174857631Seric 			*n = '\0';
174957661Seric 		if (name->nodename[0] != '\0')
175057631Seric 			return (0);
175157631Seric 	}
175257631Seric #endif
175357631Seric 
175457631Seric 	return (-1);
175557631Seric }
175657943Seric #endif /* HASUNAME */
175758068Seric /*
175858068Seric **  INITGROUPS -- initialize groups
175958068Seric **
176058068Seric **	Stub implementation for System V style systems
176158068Seric */
176258068Seric 
176358068Seric #ifndef HASINITGROUPS
176458068Seric 
176558068Seric initgroups(name, basegid)
176658068Seric 	char *name;
176758068Seric 	int basegid;
176858068Seric {
176958068Seric 	return 0;
177058068Seric }
177158068Seric 
177258068Seric #endif
177358082Seric /*
177459289Seric **  SETSID -- set session id (for non-POSIX systems)
177559289Seric */
177659289Seric 
177759289Seric #ifndef HASSETSID
177859289Seric 
177963753Seric pid_t
178063753Seric setsid __P ((void))
178159289Seric {
178264155Seric #ifdef TIOCNOTTY
178364155Seric 	int fd;
178464155Seric 
178566743Seric 	fd = open("/dev/tty", O_RDWR, 0);
178664155Seric 	if (fd >= 0)
178764155Seric 	{
178864155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
178964155Seric 		(void) close(fd);
179064155Seric 	}
179164155Seric #endif /* TIOCNOTTY */
179264705Seric # ifdef SYS5SETPGRP
179363753Seric 	return setpgrp();
179463753Seric # else
179564155Seric 	return setpgid(0, getpid());
179659289Seric # endif
179759289Seric }
179859289Seric 
179959289Seric #endif
180059289Seric /*
180166748Seric **  FSYNC -- dummy fsync
180266748Seric */
180366748Seric 
180466748Seric #ifdef NEEDFSYNC
180566748Seric 
180666748Seric fsync(fd)
180766748Seric 	int fd;
180866748Seric {
180966748Seric # ifdef O_SYNC
181066748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
181166748Seric # else
181266748Seric 	/* nothing we can do */
181366748Seric 	return 0;
181466748Seric # endif
181566748Seric }
181666748Seric 
181766748Seric #endif
181866748Seric /*
181964718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
182064718Seric **
182164718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
1822*68508Seric **	instead of a long.  This patches things.  Only needed on versions
1823*68508Seric **	prior to 5.4.3.
182464718Seric */
182564718Seric 
1826*68508Seric #ifdef DGUX_5_4_2
182764718Seric 
182864718Seric #undef inet_addr
182964718Seric 
183064718Seric long
183164718Seric dgux_inet_addr(host)
183264718Seric 	char *host;
183364718Seric {
183464718Seric 	struct in_addr haddr;
183564718Seric 
183664718Seric 	haddr = inet_addr(host);
183764718Seric 	return haddr.s_addr;
183864718Seric }
183964718Seric 
184064718Seric #endif
184164718Seric /*
184263969Seric **  GETOPT -- for old systems or systems with bogus implementations
184363969Seric */
184463969Seric 
184563969Seric #ifdef NEEDGETOPT
184663969Seric 
184763969Seric /*
184863969Seric  * Copyright (c) 1985 Regents of the University of California.
184963969Seric  * All rights reserved.  The Berkeley software License Agreement
185063969Seric  * specifies the terms and conditions for redistribution.
185163969Seric  */
185263969Seric 
185363969Seric 
185463969Seric /*
185563969Seric ** this version hacked to add `atend' flag to allow state machine
185663969Seric ** to reset if invoked by the program to scan args for a 2nd time
185763969Seric */
185863969Seric 
185963969Seric #if defined(LIBC_SCCS) && !defined(lint)
186063969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
186164072Seric #endif /* LIBC_SCCS and not lint */
186263969Seric 
186363969Seric #include <stdio.h>
186463969Seric 
186563969Seric /*
186663969Seric  * get option letter from argument vector
186763969Seric  */
186865352Seric #ifdef _CONVEX_SOURCE
1869*68508Seric extern int	optind, opterr, optopt;
1870*68508Seric extern char	*optarg;
187165352Seric #else
187265352Seric int	opterr = 1;		/* if error message should be printed */
187365352Seric int	optind = 1;		/* index into parent argv vector */
187468470Seric int	optopt = 0;		/* character checked for validity */
187568470Seric char	*optarg = NULL;		/* argument associated with option */
1876*68508Seric #endif
187763969Seric 
187863969Seric #define BADCH	(int)'?'
187963969Seric #define EMSG	""
188063969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
188163969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
188263969Seric 
188363969Seric getopt(nargc,nargv,ostr)
188465160Seric 	int		nargc;
188565160Seric 	char *const	*nargv;
188665160Seric 	const char	*ostr;
188763969Seric {
188863969Seric 	static char	*place = EMSG;	/* option letter processing */
188963969Seric 	static char	atend = 0;
189063969Seric 	register char	*oli;		/* option letter list index */
189163969Seric 
189263969Seric 	if (atend) {
189363969Seric 		atend = 0;
189463969Seric 		place = EMSG;
189563969Seric 	}
189663969Seric 	if(!*place) {			/* update scanning pointer */
189764136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
189863969Seric 			atend++;
189963969Seric 			return(EOF);
190063969Seric 		}
190163969Seric 		if (*place == '-') {	/* found "--" */
190263969Seric 			++optind;
190363969Seric 			atend++;
190463969Seric 			return(EOF);
190563969Seric 		}
190663969Seric 	}				/* option letter okay? */
190764125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
190864136Seric 		if (!*place) ++optind;
190963969Seric 		tell(": illegal option -- ");
191063969Seric 	}
191163969Seric 	if (*++oli != ':') {		/* don't need argument */
191263969Seric 		optarg = NULL;
191363969Seric 		if (!*place) ++optind;
191463969Seric 	}
191563969Seric 	else {				/* need an argument */
191663969Seric 		if (*place) optarg = place;	/* no white space */
191763969Seric 		else if (nargc <= ++optind) {	/* no arg */
191863969Seric 			place = EMSG;
191963969Seric 			tell(": option requires an argument -- ");
192063969Seric 		}
192163969Seric 	 	else optarg = nargv[optind];	/* white space */
192263969Seric 		place = EMSG;
192363969Seric 		++optind;
192463969Seric 	}
192563969Seric 	return(optopt);			/* dump back option letter */
192663969Seric }
192763969Seric 
192863969Seric #endif
192963969Seric /*
193063969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
193163969Seric */
193263969Seric 
193363969Seric #ifdef NEEDVPRINTF
193463969Seric 
193563969Seric #define MAXARG	16
193663969Seric 
193763969Seric vfprintf(fp, fmt, ap)
193863969Seric 	FILE *	fp;
193963969Seric 	char *	fmt;
194063969Seric 	char **	ap;
194163969Seric {
194263969Seric 	char *	bp[MAXARG];
194363969Seric 	int	i = 0;
194463969Seric 
194563969Seric 	while (*ap && i < MAXARG)
194663969Seric 		bp[i++] = *ap++;
194763969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
194863969Seric 			 bp[4], bp[5], bp[6], bp[7],
194963969Seric 			 bp[8], bp[9], bp[10], bp[11],
195063969Seric 			 bp[12], bp[13], bp[14], bp[15]);
195163969Seric }
195263969Seric 
195363969Seric vsprintf(s, fmt, ap)
195463969Seric 	char *	s;
195563969Seric 	char *	fmt;
195663969Seric 	char **	ap;
195763969Seric {
195863969Seric 	char *	bp[MAXARG];
195963969Seric 	int	i = 0;
196063969Seric 
196163969Seric 	while (*ap && i < MAXARG)
196263969Seric 		bp[i++] = *ap++;
196363969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
196463969Seric 			bp[4], bp[5], bp[6], bp[7],
196563969Seric 			bp[8], bp[9], bp[10], bp[11],
196663969Seric 			bp[12], bp[13], bp[14], bp[15]);
196763969Seric }
196863969Seric 
196963969Seric #endif
197063969Seric /*
197165220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
197265208Seric **
197365211Seric **	Parameters:
197465211Seric **		shell -- the user's shell from /etc/passwd
197565211Seric **
197665211Seric **	Returns:
197765211Seric **		TRUE -- if it is ok to use this for unrestricted access.
197865211Seric **		FALSE -- if the shell is restricted.
197965208Seric */
198065208Seric 
198165587Seric #if !HASGETUSERSHELL
198265587Seric 
198365587Seric # ifndef _PATH_SHELLS
198465587Seric #  define _PATH_SHELLS	"/etc/shells"
198565587Seric # endif
198665587Seric 
198765587Seric char	*DefaultUserShells[] =
198865587Seric {
1989*68508Seric 	"/bin/sh",		/* standard shell */
199065587Seric 	"/usr/bin/sh",
1991*68508Seric 	"/bin/csh",		/* C shell */
199265587Seric 	"/usr/bin/csh",
199365587Seric #ifdef __hpux
1994*68508Seric 	"/bin/rsh",		/* restricted Bourne shell */
1995*68508Seric 	"/bin/ksh",		/* Korn shell */
1996*68508Seric 	"/bin/rksh",		/* restricted Korn shell */
199765587Seric 	"/bin/pam",
1998*68508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
199965587Seric 	"/bin/posix/sh",
200065211Seric #endif
2001*68508Seric #ifdef _AIX3
2002*68508Seric 	"/bin/ksh",		/* Korn shell */
2003*68508Seric 	"/usr/bin/ksh",
2004*68508Seric 	"/bin/tsh",		/* trusted shell */
2005*68508Seric 	"/usr/bin/tsh",
2006*68508Seric 	"/bin/bsh",		/* Bourne shell */
2007*68508Seric 	"/usr/bin/bsh",
2008*68508Seric #endif
200965587Seric 	NULL
201065587Seric };
201165587Seric 
201265208Seric #endif
201365208Seric 
201465747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
201565747Seric 
201665211Seric bool
201765211Seric usershellok(shell)
201865211Seric 	char *shell;
201965208Seric {
202065211Seric #if HASGETUSERSHELL
202165211Seric 	register char *p;
202265211Seric 	extern char *getusershell();
202365208Seric 
202468457Seric 	if (shell == NULL || shell[0] == '\0')
202568457Seric 		return TRUE;
202668457Seric 
202765211Seric 	setusershell();
202865211Seric 	while ((p = getusershell()) != NULL)
202965747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
203065211Seric 			break;
203165211Seric 	endusershell();
203265211Seric 	return p != NULL;
203365211Seric #else
203465211Seric 	register FILE *shellf;
203565211Seric 	char buf[MAXLINE];
203665208Seric 
203768457Seric 	if (shell == NULL || shell[0] == '\0')
203868457Seric 		return TRUE;
203968457Seric 
204065211Seric 	shellf = fopen(_PATH_SHELLS, "r");
204165211Seric 	if (shellf == NULL)
204265211Seric 	{
204365211Seric 		/* no /etc/shells; see if it is one of the std shells */
204465587Seric 		char **d;
204565587Seric 
204665587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
204765587Seric 		{
204865587Seric 			if (strcmp(shell, *d) == 0)
204965587Seric 				return TRUE;
205065587Seric 		}
205165587Seric 		return FALSE;
205265211Seric 	}
205365208Seric 
205465211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
205565211Seric 	{
205665211Seric 		register char *p, *q;
205765208Seric 
205865211Seric 		p = buf;
205965211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
206065211Seric 			p++;
206165211Seric 		if (*p == '#' || *p == '\0')
206265208Seric 			continue;
206365211Seric 		q = p;
206465211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
206565211Seric 			p++;
206665211Seric 		*p = '\0';
206765747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
206865211Seric 		{
206965211Seric 			fclose(shellf);
207065211Seric 			return TRUE;
207165211Seric 		}
207265208Seric 	}
207365211Seric 	fclose(shellf);
207465211Seric 	return FALSE;
207565211Seric #endif
207665208Seric }
207765208Seric /*
207864311Seric **  FREESPACE -- see how much free space is on the queue filesystem
207958082Seric **
208058082Seric **	Only implemented if you have statfs.
208158082Seric **
208258082Seric **	Parameters:
208364311Seric **		dir -- the directory in question.
208464311Seric **		bsize -- a variable into which the filesystem
208564311Seric **			block size is stored.
208658082Seric **
208758082Seric **	Returns:
208864311Seric **		The number of bytes free on the queue filesystem.
208964311Seric **		-1 if the statfs call fails.
209064311Seric **
209164311Seric **	Side effects:
209264311Seric **		Puts the filesystem block size into bsize.
209358082Seric */
209458082Seric 
209565749Seric /* statfs types */
209665749Seric #define SFS_NONE	0	/* no statfs implementation */
209765749Seric #define SFS_USTAT	1	/* use ustat */
209865749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
209965749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
210065749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
210165749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
210266752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
210365749Seric 
210465749Seric #ifndef SFS_TYPE
210565749Seric # define SFS_TYPE	SFS_NONE
210658157Seric #endif
210758157Seric 
210865749Seric #if SFS_TYPE == SFS_USTAT
210958157Seric # include <ustat.h>
211058157Seric #endif
211165749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
211265749Seric # include <sys/statfs.h>
211358082Seric #endif
211465749Seric #if SFS_TYPE == SFS_VFS
211565749Seric # include <sys/vfs.h>
211665749Seric #endif
211765749Seric #if SFS_TYPE == SFS_MOUNT
211865749Seric # include <sys/mount.h>
211965749Seric #endif
212066752Seric #if SFS_TYPE == SFS_STATVFS
212166752Seric # include <sys/statvfs.h>
212266752Seric #endif
212358082Seric 
212464311Seric long
212564311Seric freespace(dir, bsize)
212664311Seric 	char *dir;
212764311Seric 	long *bsize;
212858082Seric {
212965749Seric #if SFS_TYPE != SFS_NONE
213065749Seric # if SFS_TYPE == SFS_USTAT
213158153Seric 	struct ustat fs;
213258153Seric 	struct stat statbuf;
213358366Seric #  define FSBLOCKSIZE	DEV_BSIZE
2134*68508Seric #  define FSF_BAVAIL	f_tfree
213558157Seric # else
213658157Seric #  if defined(ultrix)
213758157Seric 	struct fs_data fs;
2138*68508Seric #   define FSF_BAVAIL	fd_bfreen
2139*68508Seric #   define FSBLOCKSIZE	1024L
214058153Seric #  else
214166752Seric #   if SFS_TYPE == SFS_STATVFS
214266752Seric 	struct statvfs fs;
2143*68508Seric #    define FSBLOCKSIZE	fs.f_frsize
214466752Seric #   else
214558082Seric 	struct statfs fs;
214666752Seric #    define FSBLOCKSIZE	fs.f_bsize
214763987Seric #   endif
214858153Seric #  endif
214958133Seric # endif
2150*68508Seric # ifndef FSF_BAVAIL
2151*68508Seric #  define FSF_BAVAIL f_bavail
2152*68508Seric # endif
215358082Seric 
215465749Seric # if SFS_TYPE == SFS_USTAT
215564311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
215658157Seric # else
215765749Seric #  if SFS_TYPE == SFS_4ARGS
215864311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
215958157Seric #  else
2160*68508Seric #   if SFS_TYPE == SFS_STATVFS
2161*68508Seric 	if (statvfs(dir, &fs) == 0)
2162*68508Seric #   else
2163*68508Seric #    if defined(ultrix)
216468499Seric 	if (statfs(dir, &fs) > 0)
2165*68508Seric #    else
216664311Seric 	if (statfs(dir, &fs) == 0)
2167*68508Seric #    endif
216858153Seric #   endif
216958133Seric #  endif
217058133Seric # endif
217158133Seric 	{
217264311Seric 		if (bsize != NULL)
217364311Seric 			*bsize = FSBLOCKSIZE;
2174*68508Seric 		return (fs.FSF_BAVAIL);
217564311Seric 	}
217664337Seric #endif
217764311Seric 	return (-1);
217864311Seric }
217964311Seric /*
218064311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
218164311Seric **
218264311Seric **	Only implemented if you have statfs.
218364311Seric **
218464311Seric **	Parameters:
218564311Seric **		msize -- the size to check against.  If zero, we don't yet
218664311Seric **		know how big the message will be, so just check for
218764311Seric **		a "reasonable" amount.
218864311Seric **
218964311Seric **	Returns:
219064311Seric **		TRUE if there is enough space.
219164311Seric **		FALSE otherwise.
219264311Seric */
219364311Seric 
219464311Seric bool
219564311Seric enoughspace(msize)
219664311Seric 	long msize;
219764311Seric {
219864311Seric 	long bfree, bsize;
219964311Seric 
220064311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
220164311Seric 	{
220258133Seric 		if (tTd(4, 80))
220364311Seric 			printf("enoughspace: no threshold\n");
220464311Seric 		return TRUE;
220564311Seric 	}
220664311Seric 
220764311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
220864311Seric 	{
220964311Seric 		if (tTd(4, 80))
221058333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
221164311Seric 				bfree, msize);
221258333Seric 
221358333Seric 		/* convert msize to block count */
221464311Seric 		msize = msize / bsize + 1;
221558333Seric 		if (MinBlocksFree >= 0)
221658333Seric 			msize += MinBlocksFree;
221758333Seric 
221864311Seric 		if (bfree < msize)
221958090Seric 		{
222058090Seric #ifdef LOG
222158090Seric 			if (LogLevel > 0)
222264254Seric 				syslog(LOG_ALERT,
222364254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
2224*68508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
2225*68508Seric 					bfree,
2226*68508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
2227*68508Seric 					msize, QueueDir);
222858090Seric #endif
222958082Seric 			return FALSE;
223058090Seric 		}
223158082Seric 	}
223258082Seric 	else if (tTd(4, 80))
223358333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
223458333Seric 			MinBlocksFree, msize, errstring(errno));
223558082Seric 	return TRUE;
223658082Seric }
223758542Seric /*
223858542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
223958542Seric **
224058542Seric **	This looks at an errno value and tells if this is likely to
224158542Seric **	go away if retried later.
224258542Seric **
224358542Seric **	Parameters:
224458542Seric **		err -- the errno code to classify.
224558542Seric **
224658542Seric **	Returns:
224758542Seric **		TRUE if this is probably transient.
224858542Seric **		FALSE otherwise.
224958542Seric */
225058542Seric 
225158542Seric bool
225258542Seric transienterror(err)
225358542Seric 	int err;
225458542Seric {
225558542Seric 	switch (err)
225658542Seric 	{
225758542Seric 	  case EIO:			/* I/O error */
225858542Seric 	  case ENXIO:			/* Device not configured */
225958542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
226058542Seric 	  case ENOMEM:			/* Cannot allocate memory */
226158542Seric 	  case ENODEV:			/* Operation not supported by device */
226258542Seric 	  case ENFILE:			/* Too many open files in system */
226358542Seric 	  case EMFILE:			/* Too many open files */
226458542Seric 	  case ENOSPC:			/* No space left on device */
226558542Seric #ifdef ETIMEDOUT
226658542Seric 	  case ETIMEDOUT:		/* Connection timed out */
226758542Seric #endif
226858542Seric #ifdef ESTALE
226958542Seric 	  case ESTALE:			/* Stale NFS file handle */
227058542Seric #endif
227158542Seric #ifdef ENETDOWN
227258542Seric 	  case ENETDOWN:		/* Network is down */
227358542Seric #endif
227458542Seric #ifdef ENETUNREACH
227558542Seric 	  case ENETUNREACH:		/* Network is unreachable */
227658542Seric #endif
227758542Seric #ifdef ENETRESET
227858542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
227958542Seric #endif
228058542Seric #ifdef ECONNABORTED
228158542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
228258542Seric #endif
228358542Seric #ifdef ECONNRESET
228458542Seric 	  case ECONNRESET:		/* Connection reset by peer */
228558542Seric #endif
228658542Seric #ifdef ENOBUFS
228758542Seric 	  case ENOBUFS:			/* No buffer space available */
228858542Seric #endif
228958542Seric #ifdef ESHUTDOWN
229058542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
229158542Seric #endif
229258542Seric #ifdef ECONNREFUSED
229358542Seric 	  case ECONNREFUSED:		/* Connection refused */
229458542Seric #endif
229558542Seric #ifdef EHOSTDOWN
229658542Seric 	  case EHOSTDOWN:		/* Host is down */
229758542Seric #endif
229858542Seric #ifdef EHOSTUNREACH
229958542Seric 	  case EHOSTUNREACH:		/* No route to host */
230058542Seric #endif
230158542Seric #ifdef EDQUOT
230258542Seric 	  case EDQUOT:			/* Disc quota exceeded */
230358542Seric #endif
230458542Seric #ifdef EPROCLIM
230558542Seric 	  case EPROCLIM:		/* Too many processes */
230658542Seric #endif
230758542Seric #ifdef EUSERS
230858542Seric 	  case EUSERS:			/* Too many users */
230958542Seric #endif
231058542Seric #ifdef EDEADLK
231158542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
231258542Seric #endif
231358542Seric #ifdef EISCONN
231458542Seric 	  case EISCONN:			/* Socket already connected */
231558542Seric #endif
231658542Seric #ifdef EINPROGRESS
231758542Seric 	  case EINPROGRESS:		/* Operation now in progress */
231858542Seric #endif
231958542Seric #ifdef EALREADY
232058542Seric 	  case EALREADY:		/* Operation already in progress */
232158542Seric #endif
232258542Seric #ifdef EADDRINUSE
232358542Seric 	  case EADDRINUSE:		/* Address already in use */
232458542Seric #endif
232558542Seric #ifdef EADDRNOTAVAIL
232658542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
232758542Seric #endif
232866018Seric #ifdef ETXTBSY
232966017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
233066017Seric #endif
233163834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
233258542Seric 	  case ENOSR:			/* Out of streams resources */
233358542Seric #endif
233458542Seric 		return TRUE;
233558542Seric 	}
233658542Seric 
233758542Seric 	/* nope, must be permanent */
233858542Seric 	return FALSE;
233958542Seric }
234058689Seric /*
234164035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
234258689Seric **
234358689Seric **	Parameters:
234458689Seric **		fd -- the file descriptor of the file.
234558689Seric **		filename -- the file name (for error messages).
234664335Seric **		ext -- the filename extension.
234758689Seric **		type -- type of the lock.  Bits can be:
234858689Seric **			LOCK_EX -- exclusive lock.
234958689Seric **			LOCK_NB -- non-blocking.
235058689Seric **
235158689Seric **	Returns:
235258689Seric **		TRUE if the lock was acquired.
235358689Seric **		FALSE otherwise.
235458689Seric */
235558689Seric 
235658689Seric bool
235764335Seric lockfile(fd, filename, ext, type)
235858689Seric 	int fd;
235958689Seric 	char *filename;
236064335Seric 	char *ext;
236158689Seric 	int type;
236258689Seric {
236365830Seric # if !HASFLOCK
236458689Seric 	int action;
236558689Seric 	struct flock lfd;
236664335Seric 
236764335Seric 	if (ext == NULL)
236864335Seric 		ext = "";
236964035Seric 
237063983Seric 	bzero(&lfd, sizeof lfd);
237159447Seric 	if (bitset(LOCK_UN, type))
237259447Seric 		lfd.l_type = F_UNLCK;
237359447Seric 	else if (bitset(LOCK_EX, type))
237458689Seric 		lfd.l_type = F_WRLCK;
237558689Seric 	else
237658689Seric 		lfd.l_type = F_RDLCK;
237758689Seric 
237858689Seric 	if (bitset(LOCK_NB, type))
237958689Seric 		action = F_SETLK;
238058689Seric 	else
238158689Seric 		action = F_SETLKW;
238258689Seric 
238364264Seric 	if (tTd(55, 60))
238464335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
238564335Seric 			filename, ext, action, lfd.l_type);
238664264Seric 
238758689Seric 	if (fcntl(fd, action, &lfd) >= 0)
238864264Seric 	{
238964264Seric 		if (tTd(55, 60))
239064264Seric 			printf("SUCCESS\n");
239158689Seric 		return TRUE;
239264264Seric 	}
239358689Seric 
239464264Seric 	if (tTd(55, 60))
239564264Seric 		printf("(%s) ", errstring(errno));
239664264Seric 
239764136Seric 	/*
239864136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
239964136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
240064136Seric 	**  as type "tmp" (that is, served from swap space), the
240164136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
240264136Seric 	**  Since this is fairly common during testing, we will assume
240364136Seric 	**  that this indicates that the lock is successfully grabbed.
240464136Seric 	*/
240564136Seric 
240664136Seric 	if (errno == EINVAL)
240764264Seric 	{
240864264Seric 		if (tTd(55, 60))
240964264Seric 			printf("SUCCESS\n");
241064136Seric 		return TRUE;
241164264Seric 	}
241264136Seric 
241358689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
241464378Seric 	{
241564378Seric 		int omode = -1;
241664378Seric #  ifdef F_GETFL
241764378Seric 		int oerrno = errno;
241864378Seric 
241964378Seric 		(void) fcntl(fd, F_GETFL, &omode);
242064378Seric 		errno = oerrno;
242164378Seric #  endif
242264378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
242364378Seric 			filename, ext, fd, type, omode, geteuid());
242464378Seric 	}
242558689Seric # else
242664335Seric 	if (ext == NULL)
242764335Seric 		ext = "";
242864335Seric 
242964264Seric 	if (tTd(55, 60))
243064335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
243164264Seric 
243258689Seric 	if (flock(fd, type) >= 0)
243364264Seric 	{
243464264Seric 		if (tTd(55, 60))
243564264Seric 			printf("SUCCESS\n");
243658689Seric 		return TRUE;
243764264Seric 	}
243858689Seric 
243964264Seric 	if (tTd(55, 60))
244064264Seric 		printf("(%s) ", errstring(errno));
244164264Seric 
244258689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
244364378Seric 	{
244464378Seric 		int omode = -1;
244564378Seric #  ifdef F_GETFL
244664378Seric 		int oerrno = errno;
244764378Seric 
244864378Seric 		(void) fcntl(fd, F_GETFL, &omode);
244964378Seric 		errno = oerrno;
245064378Seric #  endif
245164378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
245264378Seric 			filename, ext, fd, type, omode, geteuid());
245364386Seric 	}
245458689Seric # endif
245564264Seric 	if (tTd(55, 60))
245664264Seric 		printf("FAIL\n");
245758689Seric 	return FALSE;
245858689Seric }
245964556Seric /*
246065948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
246165948Seric **
246265948Seric **	Parameters:
246365948Seric **		fd -- the file descriptor to check.
246465948Seric **
246565948Seric **	Returns:
246665948Seric **		TRUE -- if only root can chown the file to an arbitrary
246765948Seric **			user.
246865948Seric **		FALSE -- if an arbitrary user can give away a file.
246965948Seric */
247065948Seric 
247165948Seric bool
247265948Seric chownsafe(fd)
247365948Seric 	int fd;
247465948Seric {
247565950Seric #ifdef __hpux
247665948Seric 	char *s;
247765948Seric 	int tfd;
247865948Seric 	uid_t o_uid, o_euid;
247965948Seric 	gid_t o_gid, o_egid;
248065948Seric 	bool rval;
248165948Seric 	struct stat stbuf;
248265948Seric 
248365948Seric 	o_uid = getuid();
248465948Seric 	o_euid = geteuid();
248565948Seric 	o_gid = getgid();
248665948Seric 	o_egid = getegid();
248765959Seric 	fstat(fd, &stbuf);
248865948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
248965948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
249065948Seric 	s = tmpnam(NULL);
249165948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
249265974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
249365948Seric 	close(tfd);
249465948Seric 	unlink(s);
249565948Seric 	setreuid(o_uid, o_euid);
249665948Seric 	setresgid(o_gid, o_egid, -1);
249765948Seric 	return rval;
249865948Seric #else
249966088Seric # ifdef _POSIX_CHOWN_RESTRICTED
250066088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
250166088Seric 	return FALSE;
250266088Seric #  else
250366088Seric 	return TRUE;
250466088Seric #  endif
250566088Seric # else
250666088Seric #  ifdef _PC_CHOWN_RESTRICTED
2507*68508Seric 	int rval;
2508*68508Seric 
2509*68508Seric 	/*
2510*68508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
2511*68508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
2512*68508Seric 	**  the call.  This heuristic checks for that.
2513*68508Seric 	*/
2514*68508Seric 
2515*68508Seric 	errno = 0;
2516*68508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
2517*68508Seric 	if (errno == 0)
2518*68508Seric 		return rval > 0;
2519*68508Seric #  endif
2520*68508Seric #  ifdef BSD
2521*68508Seric 	return TRUE;
252268499Seric #  else
252365948Seric 	return FALSE;
252465948Seric #  endif
252565948Seric # endif
252665948Seric #endif
252765948Seric }
252865948Seric /*
2529*68508Seric **  RESETLIMITS -- reset system controlled resource limits
2530*68508Seric **
2531*68508Seric **	This is to avoid denial-of-service attacks
2532*68508Seric **
2533*68508Seric **	Parameters:
2534*68508Seric **		none
2535*68508Seric **
2536*68508Seric **	Returns:
2537*68508Seric **		none
2538*68508Seric */
2539*68508Seric 
2540*68508Seric #if HASSETRLIMIT
2541*68508Seric # include <sys/resource.h>
2542*68508Seric #endif
2543*68508Seric 
2544*68508Seric void
2545*68508Seric resetlimits()
2546*68508Seric {
2547*68508Seric #if HASSETRLIMIT
2548*68508Seric 	struct rlimit lim;
2549*68508Seric 
2550*68508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
2551*68508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
2552*68508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
2553*68508Seric #else
2554*68508Seric # if HASULIMIT
2555*68508Seric 	(void) ulimit(2, 0x3fffff);
2556*68508Seric # endif
2557*68508Seric #endif
2558*68508Seric }
2559*68508Seric /*
256064556Seric **  GETCFNAME -- return the name of the .cf file.
256164556Seric **
256264556Seric **	Some systems (e.g., NeXT) determine this dynamically.
256364556Seric */
256464556Seric 
256564556Seric char *
256664556Seric getcfname()
256764556Seric {
256864556Seric 	if (ConfFile != NULL)
256964556Seric 		return ConfFile;
257065214Seric #ifdef NETINFO
257165214Seric 	{
257265214Seric 		extern char *ni_propval();
257365214Seric 		char *cflocation;
257465214Seric 
2575*68508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
2576*68508Seric 					"sendmail.cf", '\0');
257765214Seric 		if (cflocation != NULL)
257865214Seric 			return cflocation;
257965214Seric 	}
258065214Seric #endif
258164556Seric 	return _PATH_SENDMAILCF;
258264556Seric }
258364718Seric /*
258464718Seric **  SETVENDOR -- process vendor code from V configuration line
258564718Seric **
258664718Seric **	Parameters:
258764718Seric **		vendor -- string representation of vendor.
258864718Seric **
258964718Seric **	Returns:
259064718Seric **		TRUE -- if ok.
259164718Seric **		FALSE -- if vendor code could not be processed.
259264926Seric **
259364926Seric **	Side Effects:
259464926Seric **		It is reasonable to set mode flags here to tweak
259564926Seric **		processing in other parts of the code if necessary.
259664926Seric **		For example, if you are a vendor that uses $%y to
259764926Seric **		indicate YP lookups, you could enable that here.
259864718Seric */
259964718Seric 
260064718Seric bool
260164718Seric setvendor(vendor)
260264718Seric 	char *vendor;
260364718Seric {
260464926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
2605*68508Seric 	{
2606*68508Seric 		VendorCode = VENDOR_BERKELEY;
260764926Seric 		return TRUE;
2608*68508Seric 	}
260964926Seric 
261064926Seric 	/* add vendor extensions here */
261164926Seric 
2612*68508Seric #ifdef SUN_EXTENSIONS
2613*68508Seric 	if (strcasecmp(vendor, "Sun") == 0)
2614*68508Seric 	{
2615*68508Seric 		VendorCode = VENDOR_SUN;
2616*68508Seric 		return TRUE;
2617*68508Seric 	}
2618*68508Seric #endif
2619*68508Seric 
262064926Seric 	return FALSE;
262164718Seric }
262264816Seric /*
262364816Seric **  STRTOL -- convert string to long integer
262464816Seric **
262564816Seric **	For systems that don't have it in the C library.
262666161Seric **
262766161Seric **	This is taken verbatim from the 4.4-Lite C library.
262864816Seric */
262964816Seric 
263064816Seric #ifdef NEEDSTRTOL
263164816Seric 
263266161Seric #if defined(LIBC_SCCS) && !defined(lint)
263366161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
263466161Seric #endif /* LIBC_SCCS and not lint */
263566161Seric 
263666161Seric #include <limits.h>
263766161Seric 
263866161Seric /*
263966161Seric  * Convert a string to a long integer.
264066161Seric  *
264166161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
264266161Seric  * alphabets and digits are each contiguous.
264366161Seric  */
264466161Seric 
264564816Seric long
264666161Seric strtol(nptr, endptr, base)
264766161Seric 	const char *nptr;
264866161Seric 	char **endptr;
264966161Seric 	register int base;
265064816Seric {
265166161Seric 	register const char *s = nptr;
265266161Seric 	register unsigned long acc;
265366161Seric 	register int c;
265466161Seric 	register unsigned long cutoff;
265566161Seric 	register int neg = 0, any, cutlim;
265664816Seric 
265766161Seric 	/*
265866161Seric 	 * Skip white space and pick up leading +/- sign if any.
265966161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
266066161Seric 	 * assume decimal; if base is already 16, allow 0x.
266166161Seric 	 */
266266161Seric 	do {
266366161Seric 		c = *s++;
266466161Seric 	} while (isspace(c));
266566161Seric 	if (c == '-') {
266666161Seric 		neg = 1;
266766161Seric 		c = *s++;
266866161Seric 	} else if (c == '+')
266966161Seric 		c = *s++;
267066161Seric 	if ((base == 0 || base == 16) &&
267166161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
267266161Seric 		c = s[1];
267366161Seric 		s += 2;
267466161Seric 		base = 16;
267566161Seric 	}
267666161Seric 	if (base == 0)
267766161Seric 		base = c == '0' ? 8 : 10;
267864816Seric 
267966161Seric 	/*
268066161Seric 	 * Compute the cutoff value between legal numbers and illegal
268166161Seric 	 * numbers.  That is the largest legal value, divided by the
268266161Seric 	 * base.  An input number that is greater than this value, if
268366161Seric 	 * followed by a legal input character, is too big.  One that
268466161Seric 	 * is equal to this value may be valid or not; the limit
268566161Seric 	 * between valid and invalid numbers is then based on the last
268666161Seric 	 * digit.  For instance, if the range for longs is
268766161Seric 	 * [-2147483648..2147483647] and the input base is 10,
268866161Seric 	 * cutoff will be set to 214748364 and cutlim to either
268966161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
269066161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
269166161Seric 	 * the number is too big, and we will return a range error.
269266161Seric 	 *
269366161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
269466161Seric 	 * overflow.
269566161Seric 	 */
269666161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
269766161Seric 	cutlim = cutoff % (unsigned long)base;
269866161Seric 	cutoff /= (unsigned long)base;
269966161Seric 	for (acc = 0, any = 0;; c = *s++) {
270066161Seric 		if (isdigit(c))
270166161Seric 			c -= '0';
270266161Seric 		else if (isalpha(c))
270366161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
270466161Seric 		else
270566161Seric 			break;
270666161Seric 		if (c >= base)
270766161Seric 			break;
270866161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
270966161Seric 			any = -1;
271066161Seric 		else {
271166161Seric 			any = 1;
271266161Seric 			acc *= base;
271366161Seric 			acc += c;
271464816Seric 		}
271564816Seric 	}
271666161Seric 	if (any < 0) {
271766161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
271866161Seric 		errno = ERANGE;
271966161Seric 	} else if (neg)
272066161Seric 		acc = -acc;
272166161Seric 	if (endptr != 0)
272266161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
272366161Seric 	return (acc);
272464816Seric }
272564816Seric 
272664816Seric #endif
272764841Seric /*
272864841Seric **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
272964841Seric **
2730*68508Seric **	Solaris versions at least through 2.3 don't properly deliver a
273164841Seric **	canonical h_name field.  This tries to work around it.
273264841Seric */
273364841Seric 
2734*68508Seric #if defined(SOLARIS) && SOLARIS < 204
273564841Seric 
273668482Seric extern int	h_errno;
273768482Seric 
273864841Seric struct hostent *
273964841Seric solaris_gethostbyname(name)
274065094Seric 	const char *name;
274164841Seric {
2742*68508Seric # if SOLARIS == 203
274364841Seric 	static struct hostent hp;
274464841Seric 	static char buf[1000];
274564841Seric 	extern struct hostent *_switch_gethostbyname_r();
274664841Seric 
274764841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
274864841Seric # else
274965009Seric 	extern struct hostent *__switch_gethostbyname();
275065009Seric 
275164841Seric 	return __switch_gethostbyname(name);
275264841Seric # endif
275364841Seric }
275464841Seric 
275564841Seric struct hostent *
275664841Seric solaris_gethostbyaddr(addr, len, type)
275765094Seric 	const char *addr;
275864841Seric 	int len;
275964841Seric 	int type;
276064841Seric {
2761*68508Seric # if SOLARIS == 203
276264841Seric 	static struct hostent hp;
276364841Seric 	static char buf[1000];
276464841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
276564841Seric 
276664841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
276764841Seric # else
276865009Seric 	extern struct hostent *__switch_gethostbyaddr();
276965009Seric 
277064841Seric 	return __switch_gethostbyaddr(addr, len, type);
277164841Seric # endif
277264841Seric }
277364841Seric 
277464841Seric #endif
277565214Seric /*
277665214Seric **  NI_PROPVAL -- netinfo property value lookup routine
277765214Seric **
277865214Seric **	Parameters:
2779*68508Seric **		keydir -- the Netinfo directory name in which to search
2780*68508Seric **			for the key.
2781*68508Seric **		keyprop -- the name of the property in which to find the
2782*68508Seric **			property we are interested.  Defaults to "name".
2783*68508Seric **		keyval -- the value for which we are really searching.
2784*68508Seric **		valprop -- the property name for the value in which we
2785*68508Seric **			are interested.
2786*68508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
2787*68508Seric **			we should return a string separated by this
2788*68508Seric **			character.
278965214Seric **
279065214Seric **	Returns:
279165214Seric **		NULL -- if:
279265214Seric **			1. the directory is not found
279365214Seric **			2. the property name is not found
279465214Seric **			3. the property contains multiple values
279565214Seric **			4. some error occured
279665214Seric **		else -- the location of the config file.
279765214Seric **
2798*68508Seric **	Example:
2799*68508Seric **		To search for an alias value, use:
2800*68508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
2801*68508Seric **
280265214Seric **	Notes:
280365214Seric **      	Caller should free the return value of ni_proval
280465214Seric */
280565214Seric 
280665214Seric #ifdef NETINFO
280765214Seric 
280865214Seric # include <netinfo/ni.h>
280965214Seric 
281065214Seric # define LOCAL_NETINFO_DOMAIN    "."
281165214Seric # define PARENT_NETINFO_DOMAIN   ".."
281265214Seric # define MAX_NI_LEVELS           256
281365214Seric 
281465214Seric char *
2815*68508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
2816*68508Seric 	char *keydir;
2817*68508Seric 	char *keyprop;
2818*68508Seric 	char *keyval;
2819*68508Seric 	char *valprop;
2820*68508Seric 	char sepchar;
282165214Seric {
282265820Seric 	char *propval = NULL;
282365214Seric 	int i;
2824*68508Seric 	int j, alen;
282565214Seric 	void *ni = NULL;
282665214Seric 	void *lastni = NULL;
282765214Seric 	ni_status nis;
282865214Seric 	ni_id nid;
282965214Seric 	ni_namelist ninl;
2830*68508Seric 	register char *p;
2831*68508Seric 	char keybuf[1024];
283265214Seric 
283365214Seric 	/*
2834*68508Seric 	**  Create the full key from the two parts.
2835*68508Seric 	**
2836*68508Seric 	**	Note that directory can end with, e.g., "name=" to specify
2837*68508Seric 	**	an alternate search property.
2838*68508Seric 	*/
2839*68508Seric 
2840*68508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
2841*68508Seric 	if (keyprop != NULL)
2842*68508Seric 		i += strlen(keyprop) + 1;
2843*68508Seric 	if (i > sizeof keybuf)
2844*68508Seric 		return NULL;
2845*68508Seric 	strcpy(keybuf, keydir);
2846*68508Seric 	strcat(keybuf, "/");
2847*68508Seric 	if (keyprop != NULL)
2848*68508Seric 	{
2849*68508Seric 		strcat(keybuf, keyprop);
2850*68508Seric 		strcat(keybuf, "=");
2851*68508Seric 	}
2852*68508Seric 	strcat(keybuf, keyval);
2853*68508Seric 
2854*68508Seric 	/*
285565214Seric 	**  If the passed directory and property name are found
285665214Seric 	**  in one of netinfo domains we need to search (starting
285765214Seric 	**  from the local domain moving all the way back to the
285865214Seric 	**  root domain) set propval to the property's value
285965214Seric 	**  and return it.
286065214Seric 	*/
286165214Seric 
286265214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
286365214Seric 	{
286465214Seric 		if (i == 0)
286565214Seric 		{
286665214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
286765214Seric 		}
286865214Seric 		else
286965214Seric 		{
287065214Seric 			if (lastni != NULL)
287165214Seric 				ni_free(lastni);
287265214Seric 			lastni = ni;
287365214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
287465214Seric 		}
287565214Seric 
287665214Seric 		/*
287765214Seric 		**  Don't bother if we didn't get a handle on a
287865214Seric 		**  proper domain.  This is not necessarily an error.
287965214Seric 		**  We would get a positive ni_status if, for instance
288065214Seric 		**  we never found the directory or property and tried
288165214Seric 		**  to open the parent of the root domain!
288265214Seric 		*/
288365214Seric 
288465214Seric 		if (nis != 0)
288565214Seric 			break;
288665214Seric 
288765214Seric 		/*
288865214Seric 		**  Find the path to the server information.
288965214Seric 		*/
289065214Seric 
2891*68508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
289265214Seric 			continue;
289365214Seric 
289465214Seric 		/*
2895*68508Seric 		**  Find associated value information.
289665214Seric 		*/
289765214Seric 
2898*68508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
289965214Seric 			continue;
290065214Seric 
290165214Seric 		/*
2902*68508Seric 		**  See if we have an acceptable number of values.
290365214Seric 		*/
290465214Seric 
2905*68508Seric 		if (ninl.ni_namelist_len <= 0)
2906*68508Seric 			continue;
2907*68508Seric 
2908*68508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
290965214Seric 		{
2910*68508Seric 			ni_namelist_free(&ninl);
2911*68508Seric 			continue;
291265214Seric 		}
2913*68508Seric 
2914*68508Seric 		/*
2915*68508Seric 		**  Calculate number of bytes needed and build result
2916*68508Seric 		*/
2917*68508Seric 
2918*68508Seric 		alen = 1;
2919*68508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
2920*68508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
2921*68508Seric 		propval = p = xalloc(alen);
2922*68508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
2923*68508Seric 		{
2924*68508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
2925*68508Seric 			p += strlen(p);
2926*68508Seric 			*p++ = sepchar;
2927*68508Seric 		}
2928*68508Seric 		*--p = '\0';
2929*68508Seric 
2930*68508Seric 		ni_namelist_free(&ninl);
293165214Seric 	}
293265214Seric 
293365214Seric 	/*
293465214Seric 	**  Clean up.
293565214Seric 	*/
293665214Seric 
293765214Seric 	if (ni != NULL)
293865214Seric 		ni_free(ni);
293965214Seric 	if (lastni != NULL && ni != lastni)
294065214Seric 		ni_free(lastni);
294165214Seric 
294265214Seric 	return propval;
294365214Seric }
294465214Seric 
294565214Seric #endif /* NETINFO */
294665504Seric /*
294765504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
294865504Seric **
294965504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
295065504Seric **	syslog succeeds during interrupt handlers.
295165504Seric */
295265504Seric 
295365504Seric #ifdef __hpux
295465504Seric 
295565504Seric # define MAXSYSLOGTRIES	100
295665504Seric # undef syslog
295765504Seric 
295865504Seric # ifdef __STDC__
295965504Seric hard_syslog(int pri, char *msg, ...)
296065504Seric # else
296165504Seric hard_syslog(pri, msg, va_alist)
296265504Seric 	int pri;
296365504Seric 	char *msg;
296465504Seric 	va_dcl
296565504Seric # endif
296665504Seric {
296765504Seric 	int i;
296865504Seric 	char buf[SYSLOG_BUFSIZE * 2];
296965504Seric 	VA_LOCAL_DECL;
297065504Seric 
297165504Seric 	VA_START(msg);
297265504Seric 	vsprintf(buf, msg, ap);
297365504Seric 	VA_END;
297465504Seric 
297565504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
297665504Seric 		continue;
297765504Seric }
297865504Seric 
297965504Seric #endif
2980