xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68482)
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*68482Seric static char sccsid[] = "@(#)conf.c	8.137 (Berkeley) 03/05/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*68482Seric 	"resent-sender",		H_FROM|H_RESENT,
58*68482Seric 	"resent-from",			H_FROM|H_RESENT,
59*68482Seric 	"resent-reply-to",		H_FROM|H_RESENT,
60*68482Seric 	"sender",			H_FROM,
61*68482Seric 	"from",				H_FROM,
62*68482Seric 	"reply-to",			H_FROM,
63*68482Seric 	"full-name",			H_ACHECK,
64*68482Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
65*68482Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
68*68482Seric 	"to",				H_RCPT,
69*68482Seric 	"resent-to",			H_RCPT|H_RESENT,
70*68482Seric 	"cc",				H_RCPT,
71*68482Seric 	"resent-cc",			H_RCPT|H_RESENT,
72*68482Seric 	"bcc",				H_RCPT|H_STRIPVAL,
73*68482Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
74*68482Seric 	"apparently-to",		H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
77*68482Seric 	"message-id",			0,
78*68482Seric 	"resent-message-id",		H_RESENT,
79*68482Seric 	"message",			H_EOH,
80*68482Seric 	"text",				H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
83*68482Seric 	"date",				0,
84*68482Seric 	"resent-date",			H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
87*68482Seric 	"received",			H_TRACE|H_FORCE,
88*68482Seric 	"x400-received",		H_TRACE|H_FORCE,
89*68482Seric 	"via",				H_TRACE|H_FORCE,
90*68482Seric 	"mail-from",			H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
93*68482Seric 	"comments",			H_FORCE,
94*68482Seric 	"return-path",			H_FORCE|H_ACHECK,
95*68482Seric 	"content-transfer-encoding",	H_CTE,
96*68482Seric 	"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*68482Seric void
15858734Seric setdefaults(e)
15958734Seric 	register ENVELOPE *e;
16024943Seric {
161*68482Seric 	int i;
162*68482Seric 	extern void inittimeouts();
163*68482Seric 	extern void setdefuser();
164*68482Seric 	extern void setupmaps();
165*68482Seric 	extern void setupmailers();
166*68482Seric 
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*68482Seric 	SevenBitInput = FALSE;			/* option 7 */
18357438Seric 	MaxMciCache = 1;			/* option k */
18457438Seric 	MciCacheTimeout = 300;			/* option K */
18557438Seric 	LogLevel = 9;				/* option L */
186*68482Seric 	inittimeouts(NULL);			/* option r */
18758853Seric 	PrivacyFlags = 0;			/* option p */
188*68482Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
189*68482Seric 	for (i = 0; i < MAXTOCLASS; i++)
190*68482Seric 	{
191*68482Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
192*68482Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
193*68482Seric 	}
194*68482Seric 	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*68482Seric 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*68482Seric bool	host_map_init __P((MAP *map, char *args));
222*68482Seric 
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*68482Seric void
25657402Seric setupmailers()
25757402Seric {
25857402Seric 	char buf[100];
259*68482Seric 	extern void makemailer();
26057402Seric 
261*68482Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u");
26257403Seric 	makemailer(buf);
26357403Seric 
264*68482Seric 	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*68482Seric 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*68482Seric 
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*68482Seric 		map_parseargs, nis_map_open, null_map_close,
316*68482Seric 		nis_map_lookup, null_map_store);
31760207Seric #endif
31860207Seric 
319*68482Seric #ifdef NISPLUS
320*68482Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
321*68482Seric 		map_parseargs, nisplus_map_open, null_map_close,
322*68482Seric 		nisplus_map_lookup, null_map_store);
323*68482Seric #endif
324*68482Seric 
325*68482Seric #ifdef HESIOD
326*68482Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
327*68482Seric 		map_parseargs, null_map_open, null_map_close,
328*68482Seric 		hes_map_lookup, null_map_store);
329*68482Seric #endif
330*68482Seric 
331*68482Seric #ifdef NETINFO
332*68482Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
333*68482Seric 		map_parseargs, ni_map_open, null_map_close,
334*68482Seric 		ni_map_lookup, null_map_store);
335*68482Seric #endif
336*68482Seric 
337*68482Seric #if 0
338*68482Seric 	MAPDEF("dns", NULL, 0,
339*68482Seric 		dns_map_init, null_map_open, null_map_close,
340*68482Seric 		dns_map_lookup, null_map_store);
341*68482Seric #endif
342*68482Seric 
343*68482Seric #if NAMED_BIND
344*68482Seric 	/* best MX DNS lookup */
345*68482Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
346*68482Seric 		map_parseargs, null_map_open, null_map_close,
347*68482Seric 		bestmx_map_lookup, null_map_store);
348*68482Seric #endif
349*68482Seric 
350*68482Seric 	MAPDEF("host", NULL, 0,
351*68482Seric 		host_map_init, null_map_open, null_map_close,
352*68482Seric 		host_map_lookup, null_map_store);
353*68482Seric 
354*68482Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
355*68482Seric 		map_parseargs, text_map_open, null_map_close,
356*68482Seric 		text_map_lookup, null_map_store);
357*68482Seric 
35860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
359*68482Seric 		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*68482Seric 	/* access to system passwd file */
367*68482Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
368*68482Seric 		map_parseargs, user_map_open, null_map_close,
369*68482Seric 		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*68482Seric 
385*68482Seric 	/* sequenced maps */
386*68482Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
387*68482Seric 		seq_map_parse, null_map_open, null_map_close,
388*68482Seric 		seq_map_lookup, seq_map_store);
389*68482Seric 
390*68482Seric 	/* switched interface to sequenced maps */
391*68482Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
392*68482Seric 		map_parseargs, switch_map_open, null_map_close,
393*68482Seric 		seq_map_lookup, seq_map_store);
39460207Seric }
39560207Seric 
39660207Seric #undef MAPDEF
39760207Seric /*
398*68482Seric **  INITHOSTMAPS -- initial host-dependent maps
399*68482Seric **
400*68482Seric **	This should act as an interface to any local service switch
401*68482Seric **	provided by the host operating system.
402*68482Seric **
403*68482Seric **	Parameters:
404*68482Seric **		none
405*68482Seric **
406*68482Seric **	Returns:
407*68482Seric **		none
408*68482Seric **
409*68482Seric **	Side Effects:
410*68482Seric **		Should define maps "host" and "users" as necessary
411*68482Seric **		for this OS.  If they are not defined, they will get
412*68482Seric **		a default value later.  It should check to make sure
413*68482Seric **		they are not defined first, since it's possible that
414*68482Seric **		the config file has provided an override.
415*68482Seric */
416*68482Seric 
417*68482Seric void
418*68482Seric inithostmaps()
419*68482Seric {
420*68482Seric 	register int i;
421*68482Seric 	int nmaps;
422*68482Seric 	char *maptype[MAXMAPSTACK];
423*68482Seric 	short mapreturn[MAXMAPACTIONS];
424*68482Seric 	char buf[MAXLINE];
425*68482Seric 
426*68482Seric 	/*
427*68482Seric 	**  Set up default hosts maps.
428*68482Seric 	*/
429*68482Seric 
430*68482Seric #if 0
431*68482Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
432*68482Seric 	for (i = 0; i < nmaps; i++)
433*68482Seric 	{
434*68482Seric 		if (strcmp(maptype[i], "files") == 0 &&
435*68482Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
436*68482Seric 		{
437*68482Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
438*68482Seric 			makemapentry(buf);
439*68482Seric 		}
440*68482Seric #if NAMED_BIND
441*68482Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
442*68482Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
443*68482Seric 		{
444*68482Seric 			strcpy(buf, "hosts.dns dns A");
445*68482Seric 			makemapentry(buf);
446*68482Seric 		}
447*68482Seric #endif
448*68482Seric #ifdef NISPLUS
449*68482Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
450*68482Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
451*68482Seric 		{
452*68482Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
453*68482Seric 			makemapentry(buf);
454*68482Seric 		}
455*68482Seric #endif
456*68482Seric #ifdef NIS
457*68482Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
458*68482Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
459*68482Seric 		{
460*68482Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
461*68482Seric 			makemapentry(buf);
462*68482Seric 		}
463*68482Seric #endif
464*68482Seric 	}
465*68482Seric #endif
466*68482Seric 
467*68482Seric 	/*
468*68482Seric 	**  Make sure we have a host map.
469*68482Seric 	*/
470*68482Seric 
471*68482Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
472*68482Seric 	{
473*68482Seric 		/* user didn't initialize: set up host map */
474*68482Seric 		strcpy(buf, "host host");
475*68482Seric #if NAMED_BIND
476*68482Seric 		if (ConfigLevel >= 2)
477*68482Seric 			strcat(buf, " -a.");
478*68482Seric #endif
479*68482Seric 		makemapentry(buf);
480*68482Seric 	}
481*68482Seric 
482*68482Seric 	/*
483*68482Seric 	**  Set up default aliases maps
484*68482Seric 	*/
485*68482Seric 
486*68482Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
487*68482Seric 	for (i = 0; i < nmaps; i++)
488*68482Seric 	{
489*68482Seric 		if (strcmp(maptype[i], "files") == 0 &&
490*68482Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
491*68482Seric 		{
492*68482Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
493*68482Seric 			makemapentry(buf);
494*68482Seric 		}
495*68482Seric #ifdef NISPLUS
496*68482Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
497*68482Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
498*68482Seric 		{
499*68482Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
500*68482Seric 			makemapentry(buf);
501*68482Seric 		}
502*68482Seric #endif
503*68482Seric #ifdef NIS
504*68482Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
505*68482Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
506*68482Seric 		{
507*68482Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
508*68482Seric 			makemapentry(buf);
509*68482Seric 		}
510*68482Seric #endif
511*68482Seric 	}
512*68482Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
513*68482Seric 	{
514*68482Seric 		strcpy(buf, "aliases switch aliases");
515*68482Seric 		makemapentry(buf);
516*68482Seric 	}
517*68482Seric 	strcpy(buf, "switch:aliases");
518*68482Seric 	setalias(buf);
519*68482Seric 
520*68482Seric #if 0		/* "user" map class is a better choice */
521*68482Seric 	/*
522*68482Seric 	**  Set up default users maps.
523*68482Seric 	*/
524*68482Seric 
525*68482Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
526*68482Seric 	for (i = 0; i < nmaps; i++)
527*68482Seric 	{
528*68482Seric 		if (strcmp(maptype[i], "files") == 0 &&
529*68482Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
530*68482Seric 		{
531*68482Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
532*68482Seric 			makemapentry(buf);
533*68482Seric 		}
534*68482Seric #ifdef NISPLUS
535*68482Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
536*68482Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
537*68482Seric 		{
538*68482Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
539*68482Seric 			makemapentry(buf);
540*68482Seric 		}
541*68482Seric #endif
542*68482Seric #ifdef NIS
543*68482Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
544*68482Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
545*68482Seric 		{
546*68482Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
547*68482Seric 			makemapentry(buf);
548*68482Seric 		}
549*68482Seric #endif
550*68482Seric #ifdef HESIOD
551*68482Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
552*68482Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
553*68482Seric 		{
554*68482Seric 			strcpy(buf, "users.hesiod hesiod");
555*68482Seric 			makemapentry(buf);
556*68482Seric 		}
557*68482Seric #endif
558*68482Seric 	}
559*68482Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
560*68482Seric 	{
561*68482Seric 		strcpy(buf, "users switch -m passwd");
562*68482Seric 		makemapentry(buf);
563*68482Seric 	}
564*68482Seric #endif
565*68482Seric }
566*68482Seric /*
567*68482Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
568*68482Seric **
569*68482Seric **	This is the system-dependent interface to the service switch.
570*68482Seric **
571*68482Seric **	Parameters:
572*68482Seric **		service -- the name of the service of interest.
573*68482Seric **		maptype -- an out-array of strings containing the types
574*68482Seric **			of access to use for this service.  There can
575*68482Seric **			be at most MAXMAPSTACK types for a single service.
576*68482Seric **		mapreturn -- an out-array of return information bitmaps
577*68482Seric **			for the map.
578*68482Seric **
579*68482Seric **	Returns:
580*68482Seric **		The number of map types filled in, or -1 for failure.
581*68482Seric */
582*68482Seric 
583*68482Seric #ifdef SOLARIS
584*68482Seric # include <nsswitch.h>
585*68482Seric #endif
586*68482Seric 
587*68482Seric #if defined(ultrix) || defined(__osf__)
588*68482Seric # include <sys/svcinfo.h>
589*68482Seric #endif
590*68482Seric 
591*68482Seric int
592*68482Seric switch_map_find(service, maptype, mapreturn)
593*68482Seric 	char *service;
594*68482Seric 	char *maptype[MAXMAPSTACK];
595*68482Seric 	short mapreturn[MAXMAPACTIONS];
596*68482Seric {
597*68482Seric 	register FILE *fp;
598*68482Seric 	int svcno;
599*68482Seric 	static char buf[MAXLINE];
600*68482Seric 
601*68482Seric #ifdef SOLARIS
602*68482Seric 	struct __nsw_switchconfig *nsw_conf;
603*68482Seric 	enum __nsw_parse_err pserr;
604*68482Seric 	struct __nsw_lookup *lk;
605*68482Seric 	int nsw_rc;
606*68482Seric 	static struct __nsw_lookup lkp0 =
607*68482Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
608*68482Seric 	static struct __nsw_switchconfig lkp_default =
609*68482Seric 		{ 0, "sendmail", 3, &lkp0 };
610*68482Seric 
611*68482Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
612*68482Seric 		lk = lkp_default.lookups;
613*68482Seric 	else
614*68482Seric 		lk = nsw_conf->lookups;
615*68482Seric 	svcno = 0;
616*68482Seric 	while (lk != NULL)
617*68482Seric 	{
618*68482Seric 		maptype[svcno] = lk->service_name;
619*68482Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
620*68482Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
621*68482Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
622*68482Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
623*68482Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
624*68482Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
625*68482Seric 		svcno++;
626*68482Seric 		lk = lk->next;
627*68482Seric 	}
628*68482Seric 	return svcno;
629*68482Seric #endif
630*68482Seric 
631*68482Seric #if defined(ultrix) || defined(__osf__)
632*68482Seric 	struct svcinfo *svcinfo;
633*68482Seric 	int svc;
634*68482Seric 
635*68482Seric 	svcinfo = getsvc();
636*68482Seric 	if (svcinfo == NULL)
637*68482Seric 		goto punt;
638*68482Seric 	if (strcmp(service, "hosts") == 0)
639*68482Seric 		svc = SVC_HOSTS;
640*68482Seric 	else if (strcmp(service, "aliases") == 0)
641*68482Seric 		svc = SVC_ALIASES;
642*68482Seric 	else if (strcmp(service, "passwd") == 0)
643*68482Seric 		svc = SVC_PASSWD;
644*68482Seric 	else
645*68482Seric 		return -1;
646*68482Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
647*68482Seric 	{
648*68482Seric 		switch (svcinfo->svcpath[svc][svcno])
649*68482Seric 		{
650*68482Seric 		  case SVC_LOCAL:
651*68482Seric 			maptype[svcno] = "files";
652*68482Seric 			break;
653*68482Seric 
654*68482Seric 		  case SVC_YP:
655*68482Seric 			maptype[svcno] = "nis";
656*68482Seric 			break;
657*68482Seric 
658*68482Seric 		  case SVC_BIND:
659*68482Seric 			maptype[svcno] = "dns";
660*68482Seric 			break;
661*68482Seric 
662*68482Seric #ifdef SVC_HESIOD
663*68482Seric 		  case SVC_HESIOD:
664*68482Seric 			maptype[svcno] = "hesiod";
665*68482Seric 			break;
666*68482Seric #endif
667*68482Seric 
668*68482Seric 		  case SVC_LAST:
669*68482Seric 			return svcno;
670*68482Seric 		}
671*68482Seric 	}
672*68482Seric 	return svcno;
673*68482Seric #endif
674*68482Seric 
675*68482Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
676*68482Seric 	/*
677*68482Seric 	**  Fall-back mechanism.
678*68482Seric 	*/
679*68482Seric 
680*68482Seric 	svcno = 0;
681*68482Seric 	fp = fopen(ServiceSwitchFile, "r");
682*68482Seric 	if (fp != NULL)
683*68482Seric 	{
684*68482Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
685*68482Seric 		{
686*68482Seric 			register char *p;
687*68482Seric 
688*68482Seric 			p = strpbrk(buf, "#\n");
689*68482Seric 			if (p != NULL)
690*68482Seric 				*p = '\0';
691*68482Seric 			p = strpbrk(buf, " \t");
692*68482Seric 			if (p != NULL)
693*68482Seric 				*p++ = '\0';
694*68482Seric 			if (strcmp(buf, service) != 0)
695*68482Seric 				continue;
696*68482Seric 
697*68482Seric 			/* got the right service -- extract data */
698*68482Seric 			do
699*68482Seric 			{
700*68482Seric 				while (isspace(*p))
701*68482Seric 					p++;
702*68482Seric 				if (*p == '\0')
703*68482Seric 					break;
704*68482Seric 				maptype[svcno++] = p;
705*68482Seric 				p = strpbrk(p, " \t");
706*68482Seric 				if (p != NULL)
707*68482Seric 					*p++ = '\0';
708*68482Seric 			} while (p != NULL);
709*68482Seric 			break;
710*68482Seric 		}
711*68482Seric 		fclose(fp);
712*68482Seric 		return svcno;
713*68482Seric 	}
714*68482Seric #endif
715*68482Seric 
716*68482Seric 	/* if the service file doesn't work, use an absolute fallback */
717*68482Seric   punt:
718*68482Seric 	if (strcmp(service, "aliases") == 0)
719*68482Seric 	{
720*68482Seric 		maptype[0] = "files";
721*68482Seric 		return 1;
722*68482Seric 	}
723*68482Seric 	if (strcmp(service, "hosts") == 0)
724*68482Seric 	{
725*68482Seric # if NAMED_BIND
726*68482Seric 		maptype[svcno++] = "dns";
727*68482Seric # else
728*68482Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
729*68482Seric 		/* SunOS */
730*68482Seric 		maptype[svcno++] = "nis";
731*68482Seric #  endif
732*68482Seric # endif
733*68482Seric 		maptype[svcno++] = "files";
734*68482Seric 		return svcno;
735*68482Seric 	}
736*68482Seric 	return -1;
737*68482Seric }
738*68482Seric /*
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*68482Seric **	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*68482Seric 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*68482Seric 	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*68482Seric 		/* 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*68482Seric # ifdef SA_RESTART
920*68482Seric 	n.sa_flags = SA_RESTART;
921*68482Seric # 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*68482Seric 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*68482Seric 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*68482Seric 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*68482Seric 
983*68482Seric #ifdef VENDOR_DEFAULT
984*68482Seric 	VendorCode = VENDOR_DEFAULT;
985*68482Seric #else
986*68482Seric 	VendorCode = VENDOR_BERKELEY;
987*68482Seric #endif
98864705Seric }
98964705Seric /*
990*68482Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
991*68482Seric **
992*68482Seric **	Called once, on startup.
993*68482Seric **
994*68482Seric **	Parameters:
995*68482Seric **		e -- the global envelope.
996*68482Seric **
997*68482Seric **	Returns:
998*68482Seric **		none.
999*68482Seric **
1000*68482Seric **	Side Effects:
1001*68482Seric **		vendor-dependent.
1002*68482Seric */
1003*68482Seric 
1004*68482Seric void
1005*68482Seric init_vendor_macros(e)
1006*68482Seric 	register ENVELOPE *e;
1007*68482Seric {
1008*68482Seric }
1009*68482Seric /*
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*68482Seric #ifdef IRIX64
1043*68482Seric # define nlist		nlist64
1044*68482Seric #endif
1045*68482Seric 
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*68482Seric 
1079*68482Seric # if defined(_AIX3)
1080*68482Seric #  define FSHIFT	16
1081*68482Seric # 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*68482Seric #ifdef _AIX3
1119*68482Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
1120*68482Seric #else
112157736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
1122*68482Seric #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*68482Seric #ifdef NAMELISTMASK
1137*68482Seric 		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*68482Seric 	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*68482Seric int
1183*68482Seric 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*68482Seric         if (tTd(3, 1))
1191*68482Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1192*68482Seric 
119364718Seric 	return((int) (load_info.one_minute + 0.5));
119464718Seric }
119564718Seric 
119664718Seric #else
1197*68482Seric # ifdef __hpux
119864718Seric 
1199*68482Seric #  include <sys/param.h>
1200*68482Seric #  include <sys/pstat.h>
1201*68482Seric 
1202*68482Seric int
120351773Seric getla()
120451773Seric {
1205*68482Seric 	struct pst_dynamic pstd;
1206*68482Seric 
1207*68482Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1208*68482Seric 			     (size_t) 1 ,0) == -1)
1209*68482Seric 		return 0;
1210*68482Seric 
1211*68482Seric         if (tTd(3, 1))
1212*68482Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1213*68482Seric 
1214*68482Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
1215*68482Seric }
1216*68482Seric 
1217*68482Seric # else
1218*68482Seric 
1219*68482Seric int
1220*68482Seric getla()
1221*68482Seric {
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*68482Seric # 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*68482Seric 	bool rval;
1402*68482Seric 
1403*68482Seric 	if (tTd(3, 30))
1404*68482Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
140551920Seric 	if (CurrentLA < QueueLA)
1406*68482Seric 	{
1407*68482Seric 		if (tTd(3, 30))
1408*68482Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
140924943Seric 		return (FALSE);
1410*68482Seric 	}
141158132Seric 	if (CurrentLA >= RefuseLA)
1412*68482Seric 	{
1413*68482Seric 		if (tTd(3, 30))
1414*68482Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
141558132Seric 		return (TRUE);
1416*68482Seric 	}
1417*68482Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
1418*68482Seric 	if (tTd(3, 30))
1419*68482Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
1420*68482Seric 	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*68482Seric 	extern bool enoughspace();
1441*68482Seric 
144259156Seric #ifdef XLA
144359156Seric 	if (!xla_smtp_ok())
144459156Seric 		return TRUE;
144559156Seric #endif
144659156Seric 
144753037Seric 	/* this is probably too simplistic */
1448*68482Seric 	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*68482Seric #define SPT_NONE	0	/* don't use it at all */
1466*68482Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
1467*68482Seric #define SPT_BUILTIN	2	/* use libc builtin */
1468*68482Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
1469*68482Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
1470*68482Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
1471*68482Seric 
1472*68482Seric #ifndef SPT_TYPE
1473*68482Seric # define SPT_TYPE	SPT_REUSEARGV
1474*68482Seric #endif
1475*68482Seric 
1476*68482Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
1477*68482Seric 
1478*68482Seric # if SPT_TYPE == SPT_PSTAT
147958689Seric #  include <sys/pstat.h>
148058689Seric # endif
1481*68482Seric # if SPT_TYPE == SPT_PSSTRINGS
148259732Seric #  include <machine/vmparam.h>
148359732Seric #  include <sys/exec.h>
1484*68482Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
1485*68482Seric #   undef SPT_TYPE
1486*68482Seric #   define SPT_TYPE	SPT_REUSEARGV
148764247Seric #  endif
148859732Seric # endif
1489*68482Seric 
1490*68482Seric # if SPT_TYPE == SPT_PSSTRINGS
1491*68482Seric #  define SETPROC_STATIC	static
1492*68482Seric # else
149359732Seric #  define SETPROC_STATIC
149459732Seric # endif
149567626Seric 
1496*68482Seric # ifndef SPT_PADCHAR
1497*68482Seric #  define SPT_PADCHAR	' '
1498*68482Seric # endif
149958689Seric 
1500*68482Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
150164843Seric 
1502*68482Seric #if SPT_TYPE != SPT_BUILTIN
1503*68482Seric 
150424943Seric /*VARARGS1*/
1505*68482Seric void
1506*68482Seric # ifdef __STDC__
150757642Seric setproctitle(char *fmt, ...)
1508*68482Seric # else
150957642Seric setproctitle(fmt, va_alist)
151024943Seric 	char *fmt;
151157642Seric 	va_dcl
1512*68482Seric # endif
151324943Seric {
1514*68482Seric # if SPT_TYPE != SPT_NONE
151524943Seric 	register char *p;
151625049Seric 	register int i;
151759732Seric 	SETPROC_STATIC char buf[MAXLINE];
151856852Seric 	VA_LOCAL_DECL
1519*68482Seric #  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*68482Seric #  if SPT_TYPE == SPT_PSTAT
153958689Seric 	pst.pst_command = buf;
154058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
154158689Seric #  else
1542*68482Seric #   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*68482Seric 		*p++ = SPT_PADCHAR;
1555*68482Seric 	Argv[1] = NULL;
1556*68482Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
1557*68482Seric #  endif /* SPT_TYPE == SPT_PSTAT */
1558*68482Seric # endif /* SPT_TYPE != SPT_NONE */
155924943Seric }
156065982Seric 
1561*68482Seric #endif /* SPT_TYPE != SPT_BUILTIN */
156225698Seric /*
156325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
156425698Seric **
156525698Seric **	Parameters:
1566*68482Seric **		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*68482Seric reapchild(sig)
1577*68482Seric 	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*68482Seric **	instead of a long.  This patches things.  Only needed on versions
1823*68482Seric **	prior to 5.4.3.
182464718Seric */
182564718Seric 
1826*68482Seric #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
186965352Seric extern int	optind, opterr;
187065352Seric #else
187165352Seric int	opterr = 1;		/* if error message should be printed */
187265352Seric int	optind = 1;		/* index into parent argv vector */
187365352Seric #endif
187468470Seric int	optopt = 0;		/* character checked for validity */
187568470Seric char	*optarg = NULL;		/* argument associated with option */
187663969Seric 
187763969Seric #define BADCH	(int)'?'
187863969Seric #define EMSG	""
187963969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
188063969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
188163969Seric 
188263969Seric getopt(nargc,nargv,ostr)
188365160Seric 	int		nargc;
188465160Seric 	char *const	*nargv;
188565160Seric 	const char	*ostr;
188663969Seric {
188763969Seric 	static char	*place = EMSG;	/* option letter processing */
188863969Seric 	static char	atend = 0;
188963969Seric 	register char	*oli;		/* option letter list index */
189063969Seric 
189163969Seric 	if (atend) {
189263969Seric 		atend = 0;
189363969Seric 		place = EMSG;
189463969Seric 	}
189563969Seric 	if(!*place) {			/* update scanning pointer */
189664136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
189763969Seric 			atend++;
189863969Seric 			return(EOF);
189963969Seric 		}
190063969Seric 		if (*place == '-') {	/* found "--" */
190163969Seric 			++optind;
190263969Seric 			atend++;
190363969Seric 			return(EOF);
190463969Seric 		}
190563969Seric 	}				/* option letter okay? */
190664125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
190764136Seric 		if (!*place) ++optind;
190863969Seric 		tell(": illegal option -- ");
190963969Seric 	}
191063969Seric 	if (*++oli != ':') {		/* don't need argument */
191163969Seric 		optarg = NULL;
191263969Seric 		if (!*place) ++optind;
191363969Seric 	}
191463969Seric 	else {				/* need an argument */
191563969Seric 		if (*place) optarg = place;	/* no white space */
191663969Seric 		else if (nargc <= ++optind) {	/* no arg */
191763969Seric 			place = EMSG;
191863969Seric 			tell(": option requires an argument -- ");
191963969Seric 		}
192063969Seric 	 	else optarg = nargv[optind];	/* white space */
192163969Seric 		place = EMSG;
192263969Seric 		++optind;
192363969Seric 	}
192463969Seric 	return(optopt);			/* dump back option letter */
192563969Seric }
192663969Seric 
192763969Seric #endif
192863969Seric /*
192963969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
193063969Seric */
193163969Seric 
193263969Seric #ifdef NEEDVPRINTF
193363969Seric 
193463969Seric #define MAXARG	16
193563969Seric 
193663969Seric vfprintf(fp, fmt, ap)
193763969Seric 	FILE *	fp;
193863969Seric 	char *	fmt;
193963969Seric 	char **	ap;
194063969Seric {
194163969Seric 	char *	bp[MAXARG];
194263969Seric 	int	i = 0;
194363969Seric 
194463969Seric 	while (*ap && i < MAXARG)
194563969Seric 		bp[i++] = *ap++;
194663969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
194763969Seric 			 bp[4], bp[5], bp[6], bp[7],
194863969Seric 			 bp[8], bp[9], bp[10], bp[11],
194963969Seric 			 bp[12], bp[13], bp[14], bp[15]);
195063969Seric }
195163969Seric 
195263969Seric vsprintf(s, fmt, ap)
195363969Seric 	char *	s;
195463969Seric 	char *	fmt;
195563969Seric 	char **	ap;
195663969Seric {
195763969Seric 	char *	bp[MAXARG];
195863969Seric 	int	i = 0;
195963969Seric 
196063969Seric 	while (*ap && i < MAXARG)
196163969Seric 		bp[i++] = *ap++;
196263969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
196363969Seric 			bp[4], bp[5], bp[6], bp[7],
196463969Seric 			bp[8], bp[9], bp[10], bp[11],
196563969Seric 			bp[12], bp[13], bp[14], bp[15]);
196663969Seric }
196763969Seric 
196863969Seric #endif
196963969Seric /*
197065220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
197165208Seric **
197265211Seric **	Parameters:
197365211Seric **		shell -- the user's shell from /etc/passwd
197465211Seric **
197565211Seric **	Returns:
197665211Seric **		TRUE -- if it is ok to use this for unrestricted access.
197765211Seric **		FALSE -- if the shell is restricted.
197865208Seric */
197965208Seric 
198065587Seric #if !HASGETUSERSHELL
198165587Seric 
198265587Seric # ifndef _PATH_SHELLS
198365587Seric #  define _PATH_SHELLS	"/etc/shells"
198465587Seric # endif
198565587Seric 
198665587Seric char	*DefaultUserShells[] =
198765587Seric {
1988*68482Seric 	"/bin/sh",		/* standard shell */
198965587Seric 	"/usr/bin/sh",
1990*68482Seric 	"/bin/csh",		/* C shell */
199165587Seric 	"/usr/bin/csh",
199265587Seric #ifdef __hpux
1993*68482Seric 	"/bin/rsh",		/* restricted Bourne shell */
1994*68482Seric 	"/bin/ksh",		/* Korn shell */
1995*68482Seric 	"/bin/rksh",		/* restricted Korn shell */
199665587Seric 	"/bin/pam",
1997*68482Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
199865587Seric 	"/bin/posix/sh",
199965211Seric #endif
2000*68482Seric #ifdef _AIX3
2001*68482Seric 	"/bin/ksh",		/* Korn shell */
2002*68482Seric 	"/usr/bin/ksh",
2003*68482Seric 	"/bin/tsh",		/* trusted shell */
2004*68482Seric 	"/usr/bin/tsh",
2005*68482Seric 	"/bin/bsh",		/* Bourne shell */
2006*68482Seric 	"/usr/bin/bsh",
2007*68482Seric #endif
200865587Seric 	NULL
200965587Seric };
201065587Seric 
201165208Seric #endif
201265208Seric 
201365747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
201465747Seric 
201565211Seric bool
201665211Seric usershellok(shell)
201765211Seric 	char *shell;
201865208Seric {
201965211Seric #if HASGETUSERSHELL
202065211Seric 	register char *p;
202165211Seric 	extern char *getusershell();
202265208Seric 
202368457Seric 	if (shell == NULL || shell[0] == '\0')
202468457Seric 		return TRUE;
202568457Seric 
202665211Seric 	setusershell();
202765211Seric 	while ((p = getusershell()) != NULL)
202865747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
202965211Seric 			break;
203065211Seric 	endusershell();
203165211Seric 	return p != NULL;
203265211Seric #else
203365211Seric 	register FILE *shellf;
203465211Seric 	char buf[MAXLINE];
203565208Seric 
203668457Seric 	if (shell == NULL || shell[0] == '\0')
203768457Seric 		return TRUE;
203868457Seric 
203965211Seric 	shellf = fopen(_PATH_SHELLS, "r");
204065211Seric 	if (shellf == NULL)
204165211Seric 	{
204265211Seric 		/* no /etc/shells; see if it is one of the std shells */
204365587Seric 		char **d;
204465587Seric 
204565587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
204665587Seric 		{
204765587Seric 			if (strcmp(shell, *d) == 0)
204865587Seric 				return TRUE;
204965587Seric 		}
205065587Seric 		return FALSE;
205165211Seric 	}
205265208Seric 
205365211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
205465211Seric 	{
205565211Seric 		register char *p, *q;
205665208Seric 
205765211Seric 		p = buf;
205865211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
205965211Seric 			p++;
206065211Seric 		if (*p == '#' || *p == '\0')
206165208Seric 			continue;
206265211Seric 		q = p;
206365211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
206465211Seric 			p++;
206565211Seric 		*p = '\0';
206665747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
206765211Seric 		{
206865211Seric 			fclose(shellf);
206965211Seric 			return TRUE;
207065211Seric 		}
207165208Seric 	}
207265211Seric 	fclose(shellf);
207365211Seric 	return FALSE;
207465211Seric #endif
207565208Seric }
207665208Seric /*
207764311Seric **  FREESPACE -- see how much free space is on the queue filesystem
207858082Seric **
207958082Seric **	Only implemented if you have statfs.
208058082Seric **
208158082Seric **	Parameters:
208264311Seric **		dir -- the directory in question.
208364311Seric **		bsize -- a variable into which the filesystem
208464311Seric **			block size is stored.
208558082Seric **
208658082Seric **	Returns:
208764311Seric **		The number of bytes free on the queue filesystem.
208864311Seric **		-1 if the statfs call fails.
208964311Seric **
209064311Seric **	Side effects:
209164311Seric **		Puts the filesystem block size into bsize.
209258082Seric */
209358082Seric 
209465749Seric /* statfs types */
209565749Seric #define SFS_NONE	0	/* no statfs implementation */
209665749Seric #define SFS_USTAT	1	/* use ustat */
209765749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
209865749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
209965749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
210065749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
210166752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
210265749Seric 
210365749Seric #ifndef SFS_TYPE
210465749Seric # define SFS_TYPE	SFS_NONE
210558157Seric #endif
210658157Seric 
210765749Seric #if SFS_TYPE == SFS_USTAT
210858157Seric # include <ustat.h>
210958157Seric #endif
211065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
211165749Seric # include <sys/statfs.h>
211258082Seric #endif
211365749Seric #if SFS_TYPE == SFS_VFS
211465749Seric # include <sys/vfs.h>
211565749Seric #endif
211665749Seric #if SFS_TYPE == SFS_MOUNT
211765749Seric # include <sys/mount.h>
211865749Seric #endif
211966752Seric #if SFS_TYPE == SFS_STATVFS
212066752Seric # include <sys/statvfs.h>
212166752Seric #endif
212258082Seric 
212364311Seric long
212464311Seric freespace(dir, bsize)
212564311Seric 	char *dir;
212664311Seric 	long *bsize;
212758082Seric {
212865749Seric #if SFS_TYPE != SFS_NONE
212965749Seric # if SFS_TYPE == SFS_USTAT
213058153Seric 	struct ustat fs;
213158153Seric 	struct stat statbuf;
213258366Seric #  define FSBLOCKSIZE	DEV_BSIZE
2133*68482Seric #  define FSF_BAVAIL	f_tfree
213458157Seric # else
213558157Seric #  if defined(ultrix)
213658157Seric 	struct fs_data fs;
2137*68482Seric #   define FSF_BAVAIL	fd_bfreen
2138*68482Seric #   define FSBLOCKSIZE	1024L
213958153Seric #  else
214066752Seric #   if SFS_TYPE == SFS_STATVFS
214166752Seric 	struct statvfs fs;
2142*68482Seric #    define FSBLOCKSIZE	fs.f_frsize
214366752Seric #   else
214458082Seric 	struct statfs fs;
214566752Seric #    define FSBLOCKSIZE	fs.f_bsize
214663987Seric #   endif
214758153Seric #  endif
214858133Seric # endif
2149*68482Seric # ifndef FSF_BAVAIL
2150*68482Seric #  define FSF_BAVAIL f_bavail
2151*68482Seric # endif
215258082Seric 
215365749Seric # if SFS_TYPE == SFS_USTAT
215464311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
215558157Seric # else
215665749Seric #  if SFS_TYPE == SFS_4ARGS
215764311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
215858157Seric #  else
2159*68482Seric #   if SFS_TYPE == SFS_STATVFS
2160*68482Seric 	if (statvfs(dir, &fs) == 0)
2161*68482Seric #   else
2162*68482Seric #    if defined(ultrix)
216368471Seric 	if (statfs(dir, &fs) > 0)
2164*68482Seric #    else
216564311Seric 	if (statfs(dir, &fs) == 0)
2166*68482Seric #    endif
216758153Seric #   endif
216858133Seric #  endif
216958133Seric # endif
217058133Seric 	{
217164311Seric 		if (bsize != NULL)
217264311Seric 			*bsize = FSBLOCKSIZE;
2173*68482Seric 		return (fs.FSF_BAVAIL);
217464311Seric 	}
217564337Seric #endif
217664311Seric 	return (-1);
217764311Seric }
217864311Seric /*
217964311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
218064311Seric **
218164311Seric **	Only implemented if you have statfs.
218264311Seric **
218364311Seric **	Parameters:
218464311Seric **		msize -- the size to check against.  If zero, we don't yet
218564311Seric **		know how big the message will be, so just check for
218664311Seric **		a "reasonable" amount.
218764311Seric **
218864311Seric **	Returns:
218964311Seric **		TRUE if there is enough space.
219064311Seric **		FALSE otherwise.
219164311Seric */
219264311Seric 
219364311Seric bool
219464311Seric enoughspace(msize)
219564311Seric 	long msize;
219664311Seric {
219764311Seric 	long bfree, bsize;
219864311Seric 
219964311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
220064311Seric 	{
220158133Seric 		if (tTd(4, 80))
220264311Seric 			printf("enoughspace: no threshold\n");
220364311Seric 		return TRUE;
220464311Seric 	}
220564311Seric 
220664311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
220764311Seric 	{
220864311Seric 		if (tTd(4, 80))
220958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
221064311Seric 				bfree, msize);
221158333Seric 
221258333Seric 		/* convert msize to block count */
221364311Seric 		msize = msize / bsize + 1;
221458333Seric 		if (MinBlocksFree >= 0)
221558333Seric 			msize += MinBlocksFree;
221658333Seric 
221764311Seric 		if (bfree < msize)
221858090Seric 		{
221958090Seric #ifdef LOG
222058090Seric 			if (LogLevel > 0)
222164254Seric 				syslog(LOG_ALERT,
222264254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
2223*68482Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
2224*68482Seric 					bfree,
2225*68482Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
2226*68482Seric 					msize, QueueDir);
222758090Seric #endif
222858082Seric 			return FALSE;
222958090Seric 		}
223058082Seric 	}
223158082Seric 	else if (tTd(4, 80))
223258333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
223358333Seric 			MinBlocksFree, msize, errstring(errno));
223458082Seric 	return TRUE;
223558082Seric }
223658542Seric /*
223758542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
223858542Seric **
223958542Seric **	This looks at an errno value and tells if this is likely to
224058542Seric **	go away if retried later.
224158542Seric **
224258542Seric **	Parameters:
224358542Seric **		err -- the errno code to classify.
224458542Seric **
224558542Seric **	Returns:
224658542Seric **		TRUE if this is probably transient.
224758542Seric **		FALSE otherwise.
224858542Seric */
224958542Seric 
225058542Seric bool
225158542Seric transienterror(err)
225258542Seric 	int err;
225358542Seric {
225458542Seric 	switch (err)
225558542Seric 	{
225658542Seric 	  case EIO:			/* I/O error */
225758542Seric 	  case ENXIO:			/* Device not configured */
225858542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
225958542Seric 	  case ENOMEM:			/* Cannot allocate memory */
226058542Seric 	  case ENODEV:			/* Operation not supported by device */
226158542Seric 	  case ENFILE:			/* Too many open files in system */
226258542Seric 	  case EMFILE:			/* Too many open files */
226358542Seric 	  case ENOSPC:			/* No space left on device */
226458542Seric #ifdef ETIMEDOUT
226558542Seric 	  case ETIMEDOUT:		/* Connection timed out */
226658542Seric #endif
226758542Seric #ifdef ESTALE
226858542Seric 	  case ESTALE:			/* Stale NFS file handle */
226958542Seric #endif
227058542Seric #ifdef ENETDOWN
227158542Seric 	  case ENETDOWN:		/* Network is down */
227258542Seric #endif
227358542Seric #ifdef ENETUNREACH
227458542Seric 	  case ENETUNREACH:		/* Network is unreachable */
227558542Seric #endif
227658542Seric #ifdef ENETRESET
227758542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
227858542Seric #endif
227958542Seric #ifdef ECONNABORTED
228058542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
228158542Seric #endif
228258542Seric #ifdef ECONNRESET
228358542Seric 	  case ECONNRESET:		/* Connection reset by peer */
228458542Seric #endif
228558542Seric #ifdef ENOBUFS
228658542Seric 	  case ENOBUFS:			/* No buffer space available */
228758542Seric #endif
228858542Seric #ifdef ESHUTDOWN
228958542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
229058542Seric #endif
229158542Seric #ifdef ECONNREFUSED
229258542Seric 	  case ECONNREFUSED:		/* Connection refused */
229358542Seric #endif
229458542Seric #ifdef EHOSTDOWN
229558542Seric 	  case EHOSTDOWN:		/* Host is down */
229658542Seric #endif
229758542Seric #ifdef EHOSTUNREACH
229858542Seric 	  case EHOSTUNREACH:		/* No route to host */
229958542Seric #endif
230058542Seric #ifdef EDQUOT
230158542Seric 	  case EDQUOT:			/* Disc quota exceeded */
230258542Seric #endif
230358542Seric #ifdef EPROCLIM
230458542Seric 	  case EPROCLIM:		/* Too many processes */
230558542Seric #endif
230658542Seric #ifdef EUSERS
230758542Seric 	  case EUSERS:			/* Too many users */
230858542Seric #endif
230958542Seric #ifdef EDEADLK
231058542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
231158542Seric #endif
231258542Seric #ifdef EISCONN
231358542Seric 	  case EISCONN:			/* Socket already connected */
231458542Seric #endif
231558542Seric #ifdef EINPROGRESS
231658542Seric 	  case EINPROGRESS:		/* Operation now in progress */
231758542Seric #endif
231858542Seric #ifdef EALREADY
231958542Seric 	  case EALREADY:		/* Operation already in progress */
232058542Seric #endif
232158542Seric #ifdef EADDRINUSE
232258542Seric 	  case EADDRINUSE:		/* Address already in use */
232358542Seric #endif
232458542Seric #ifdef EADDRNOTAVAIL
232558542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
232658542Seric #endif
232766018Seric #ifdef ETXTBSY
232866017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
232966017Seric #endif
233063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
233158542Seric 	  case ENOSR:			/* Out of streams resources */
233258542Seric #endif
233358542Seric 		return TRUE;
233458542Seric 	}
233558542Seric 
233658542Seric 	/* nope, must be permanent */
233758542Seric 	return FALSE;
233858542Seric }
233958689Seric /*
234064035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
234158689Seric **
234258689Seric **	Parameters:
234358689Seric **		fd -- the file descriptor of the file.
234458689Seric **		filename -- the file name (for error messages).
234564335Seric **		ext -- the filename extension.
234658689Seric **		type -- type of the lock.  Bits can be:
234758689Seric **			LOCK_EX -- exclusive lock.
234858689Seric **			LOCK_NB -- non-blocking.
234958689Seric **
235058689Seric **	Returns:
235158689Seric **		TRUE if the lock was acquired.
235258689Seric **		FALSE otherwise.
235358689Seric */
235458689Seric 
235558689Seric bool
235664335Seric lockfile(fd, filename, ext, type)
235758689Seric 	int fd;
235858689Seric 	char *filename;
235964335Seric 	char *ext;
236058689Seric 	int type;
236158689Seric {
236265830Seric # if !HASFLOCK
236358689Seric 	int action;
236458689Seric 	struct flock lfd;
236564335Seric 
236664335Seric 	if (ext == NULL)
236764335Seric 		ext = "";
236864035Seric 
236963983Seric 	bzero(&lfd, sizeof lfd);
237059447Seric 	if (bitset(LOCK_UN, type))
237159447Seric 		lfd.l_type = F_UNLCK;
237259447Seric 	else if (bitset(LOCK_EX, type))
237358689Seric 		lfd.l_type = F_WRLCK;
237458689Seric 	else
237558689Seric 		lfd.l_type = F_RDLCK;
237658689Seric 
237758689Seric 	if (bitset(LOCK_NB, type))
237858689Seric 		action = F_SETLK;
237958689Seric 	else
238058689Seric 		action = F_SETLKW;
238158689Seric 
238264264Seric 	if (tTd(55, 60))
238364335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
238464335Seric 			filename, ext, action, lfd.l_type);
238564264Seric 
238658689Seric 	if (fcntl(fd, action, &lfd) >= 0)
238764264Seric 	{
238864264Seric 		if (tTd(55, 60))
238964264Seric 			printf("SUCCESS\n");
239058689Seric 		return TRUE;
239164264Seric 	}
239258689Seric 
239364264Seric 	if (tTd(55, 60))
239464264Seric 		printf("(%s) ", errstring(errno));
239564264Seric 
239664136Seric 	/*
239764136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
239864136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
239964136Seric 	**  as type "tmp" (that is, served from swap space), the
240064136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
240164136Seric 	**  Since this is fairly common during testing, we will assume
240264136Seric 	**  that this indicates that the lock is successfully grabbed.
240364136Seric 	*/
240464136Seric 
240564136Seric 	if (errno == EINVAL)
240664264Seric 	{
240764264Seric 		if (tTd(55, 60))
240864264Seric 			printf("SUCCESS\n");
240964136Seric 		return TRUE;
241064264Seric 	}
241164136Seric 
241258689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
241364378Seric 	{
241464378Seric 		int omode = -1;
241564378Seric #  ifdef F_GETFL
241664378Seric 		int oerrno = errno;
241764378Seric 
241864378Seric 		(void) fcntl(fd, F_GETFL, &omode);
241964378Seric 		errno = oerrno;
242064378Seric #  endif
242164378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
242264378Seric 			filename, ext, fd, type, omode, geteuid());
242364378Seric 	}
242458689Seric # else
242564335Seric 	if (ext == NULL)
242664335Seric 		ext = "";
242764335Seric 
242864264Seric 	if (tTd(55, 60))
242964335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
243064264Seric 
243158689Seric 	if (flock(fd, type) >= 0)
243264264Seric 	{
243364264Seric 		if (tTd(55, 60))
243464264Seric 			printf("SUCCESS\n");
243558689Seric 		return TRUE;
243664264Seric 	}
243758689Seric 
243864264Seric 	if (tTd(55, 60))
243964264Seric 		printf("(%s) ", errstring(errno));
244064264Seric 
244158689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
244264378Seric 	{
244364378Seric 		int omode = -1;
244464378Seric #  ifdef F_GETFL
244564378Seric 		int oerrno = errno;
244664378Seric 
244764378Seric 		(void) fcntl(fd, F_GETFL, &omode);
244864378Seric 		errno = oerrno;
244964378Seric #  endif
245064378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
245164378Seric 			filename, ext, fd, type, omode, geteuid());
245264386Seric 	}
245358689Seric # endif
245464264Seric 	if (tTd(55, 60))
245564264Seric 		printf("FAIL\n");
245658689Seric 	return FALSE;
245758689Seric }
245864556Seric /*
245965948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
246065948Seric **
246165948Seric **	Parameters:
246265948Seric **		fd -- the file descriptor to check.
246365948Seric **
246465948Seric **	Returns:
246565948Seric **		TRUE -- if only root can chown the file to an arbitrary
246665948Seric **			user.
246765948Seric **		FALSE -- if an arbitrary user can give away a file.
246865948Seric */
246965948Seric 
247065948Seric bool
247165948Seric chownsafe(fd)
247265948Seric 	int fd;
247365948Seric {
247465950Seric #ifdef __hpux
247565948Seric 	char *s;
247665948Seric 	int tfd;
247765948Seric 	uid_t o_uid, o_euid;
247865948Seric 	gid_t o_gid, o_egid;
247965948Seric 	bool rval;
248065948Seric 	struct stat stbuf;
248165948Seric 
248265948Seric 	o_uid = getuid();
248365948Seric 	o_euid = geteuid();
248465948Seric 	o_gid = getgid();
248565948Seric 	o_egid = getegid();
248665959Seric 	fstat(fd, &stbuf);
248765948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
248865948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
248965948Seric 	s = tmpnam(NULL);
249065948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
249165974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
249265948Seric 	close(tfd);
249365948Seric 	unlink(s);
249465948Seric 	setreuid(o_uid, o_euid);
249565948Seric 	setresgid(o_gid, o_egid, -1);
249665948Seric 	return rval;
249765948Seric #else
249866088Seric # ifdef _POSIX_CHOWN_RESTRICTED
249966088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
250066088Seric 	return FALSE;
250166088Seric #  else
250266088Seric 	return TRUE;
250366088Seric #  endif
250466088Seric # else
250566088Seric #  ifdef _PC_CHOWN_RESTRICTED
2506*68482Seric 	int rval;
2507*68482Seric 
2508*68482Seric 	/*
2509*68482Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
2510*68482Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
2511*68482Seric 	**  the call.  This heuristic checks for that.
2512*68482Seric 	*/
2513*68482Seric 
2514*68482Seric 	errno = 0;
2515*68482Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
2516*68482Seric 	if (errno == 0)
2517*68482Seric 		return rval > 0;
2518*68482Seric #  endif
2519*68482Seric #  ifdef BSD
2520*68482Seric 	return TRUE;
252168471Seric #  else
252265948Seric 	return FALSE;
252365948Seric #  endif
252465948Seric # endif
252565948Seric #endif
252665948Seric }
252765948Seric /*
2528*68482Seric **  RESETLIMITS -- reset system controlled resource limits
2529*68482Seric **
2530*68482Seric **	This is to avoid denial-of-service attacks
2531*68482Seric **
2532*68482Seric **	Parameters:
2533*68482Seric **		none
2534*68482Seric **
2535*68482Seric **	Returns:
2536*68482Seric **		none
2537*68482Seric */
2538*68482Seric 
2539*68482Seric #if HASSETRLIMIT
2540*68482Seric # include <sys/resource.h>
2541*68482Seric #endif
2542*68482Seric 
2543*68482Seric void
2544*68482Seric resetlimits()
2545*68482Seric {
2546*68482Seric #if HASSETRLIMIT
2547*68482Seric 	struct rlimit lim;
2548*68482Seric 
2549*68482Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
2550*68482Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
2551*68482Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
2552*68482Seric #else
2553*68482Seric # if HASULIMIT
2554*68482Seric 	(void) ulimit(2, 0x3fffff);
2555*68482Seric # endif
2556*68482Seric #endif
2557*68482Seric }
2558*68482Seric /*
255964556Seric **  GETCFNAME -- return the name of the .cf file.
256064556Seric **
256164556Seric **	Some systems (e.g., NeXT) determine this dynamically.
256264556Seric */
256364556Seric 
256464556Seric char *
256564556Seric getcfname()
256664556Seric {
256764556Seric 	if (ConfFile != NULL)
256864556Seric 		return ConfFile;
256965214Seric #ifdef NETINFO
257065214Seric 	{
257165214Seric 		extern char *ni_propval();
257265214Seric 		char *cflocation;
257365214Seric 
2574*68482Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
2575*68482Seric 					"sendmail.cf", '\0');
257665214Seric 		if (cflocation != NULL)
257765214Seric 			return cflocation;
257865214Seric 	}
257965214Seric #endif
258064556Seric 	return _PATH_SENDMAILCF;
258164556Seric }
258264718Seric /*
258364718Seric **  SETVENDOR -- process vendor code from V configuration line
258464718Seric **
258564718Seric **	Parameters:
258664718Seric **		vendor -- string representation of vendor.
258764718Seric **
258864718Seric **	Returns:
258964718Seric **		TRUE -- if ok.
259064718Seric **		FALSE -- if vendor code could not be processed.
259164926Seric **
259264926Seric **	Side Effects:
259364926Seric **		It is reasonable to set mode flags here to tweak
259464926Seric **		processing in other parts of the code if necessary.
259564926Seric **		For example, if you are a vendor that uses $%y to
259664926Seric **		indicate YP lookups, you could enable that here.
259764718Seric */
259864718Seric 
259964718Seric bool
260064718Seric setvendor(vendor)
260164718Seric 	char *vendor;
260264718Seric {
260364926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
2604*68482Seric 	{
2605*68482Seric 		VendorCode = VENDOR_BERKELEY;
260664926Seric 		return TRUE;
2607*68482Seric 	}
260864926Seric 
260964926Seric 	/* add vendor extensions here */
261064926Seric 
2611*68482Seric #ifdef SUN_EXTENSIONS
2612*68482Seric 	if (strcasecmp(vendor, "Sun") == 0)
2613*68482Seric 	{
2614*68482Seric 		VendorCode = VENDOR_SUN;
2615*68482Seric 		return TRUE;
2616*68482Seric 	}
2617*68482Seric #endif
2618*68482Seric 
261964926Seric 	return FALSE;
262064718Seric }
262164816Seric /*
262264816Seric **  STRTOL -- convert string to long integer
262364816Seric **
262464816Seric **	For systems that don't have it in the C library.
262566161Seric **
262666161Seric **	This is taken verbatim from the 4.4-Lite C library.
262764816Seric */
262864816Seric 
262964816Seric #ifdef NEEDSTRTOL
263064816Seric 
263166161Seric #if defined(LIBC_SCCS) && !defined(lint)
263266161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
263366161Seric #endif /* LIBC_SCCS and not lint */
263466161Seric 
263566161Seric #include <limits.h>
263666161Seric 
263766161Seric /*
263866161Seric  * Convert a string to a long integer.
263966161Seric  *
264066161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
264166161Seric  * alphabets and digits are each contiguous.
264266161Seric  */
264366161Seric 
264464816Seric long
264566161Seric strtol(nptr, endptr, base)
264666161Seric 	const char *nptr;
264766161Seric 	char **endptr;
264866161Seric 	register int base;
264964816Seric {
265066161Seric 	register const char *s = nptr;
265166161Seric 	register unsigned long acc;
265266161Seric 	register int c;
265366161Seric 	register unsigned long cutoff;
265466161Seric 	register int neg = 0, any, cutlim;
265564816Seric 
265666161Seric 	/*
265766161Seric 	 * Skip white space and pick up leading +/- sign if any.
265866161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
265966161Seric 	 * assume decimal; if base is already 16, allow 0x.
266066161Seric 	 */
266166161Seric 	do {
266266161Seric 		c = *s++;
266366161Seric 	} while (isspace(c));
266466161Seric 	if (c == '-') {
266566161Seric 		neg = 1;
266666161Seric 		c = *s++;
266766161Seric 	} else if (c == '+')
266866161Seric 		c = *s++;
266966161Seric 	if ((base == 0 || base == 16) &&
267066161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
267166161Seric 		c = s[1];
267266161Seric 		s += 2;
267366161Seric 		base = 16;
267466161Seric 	}
267566161Seric 	if (base == 0)
267666161Seric 		base = c == '0' ? 8 : 10;
267764816Seric 
267866161Seric 	/*
267966161Seric 	 * Compute the cutoff value between legal numbers and illegal
268066161Seric 	 * numbers.  That is the largest legal value, divided by the
268166161Seric 	 * base.  An input number that is greater than this value, if
268266161Seric 	 * followed by a legal input character, is too big.  One that
268366161Seric 	 * is equal to this value may be valid or not; the limit
268466161Seric 	 * between valid and invalid numbers is then based on the last
268566161Seric 	 * digit.  For instance, if the range for longs is
268666161Seric 	 * [-2147483648..2147483647] and the input base is 10,
268766161Seric 	 * cutoff will be set to 214748364 and cutlim to either
268866161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
268966161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
269066161Seric 	 * the number is too big, and we will return a range error.
269166161Seric 	 *
269266161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
269366161Seric 	 * overflow.
269466161Seric 	 */
269566161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
269666161Seric 	cutlim = cutoff % (unsigned long)base;
269766161Seric 	cutoff /= (unsigned long)base;
269866161Seric 	for (acc = 0, any = 0;; c = *s++) {
269966161Seric 		if (isdigit(c))
270066161Seric 			c -= '0';
270166161Seric 		else if (isalpha(c))
270266161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
270366161Seric 		else
270466161Seric 			break;
270566161Seric 		if (c >= base)
270666161Seric 			break;
270766161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
270866161Seric 			any = -1;
270966161Seric 		else {
271066161Seric 			any = 1;
271166161Seric 			acc *= base;
271266161Seric 			acc += c;
271364816Seric 		}
271464816Seric 	}
271566161Seric 	if (any < 0) {
271666161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
271766161Seric 		errno = ERANGE;
271866161Seric 	} else if (neg)
271966161Seric 		acc = -acc;
272066161Seric 	if (endptr != 0)
272166161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
272266161Seric 	return (acc);
272364816Seric }
272464816Seric 
272564816Seric #endif
272664841Seric /*
272764841Seric **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
272864841Seric **
2729*68482Seric **	Solaris versions at least through 2.3 don't properly deliver a
273064841Seric **	canonical h_name field.  This tries to work around it.
273164841Seric */
273264841Seric 
273364841Seric #ifdef SOLARIS
273464841Seric 
2735*68482Seric extern int	h_errno;
2736*68482Seric 
273764841Seric struct hostent *
273864841Seric solaris_gethostbyname(name)
273965094Seric 	const char *name;
274064841Seric {
274164841Seric # ifdef SOLARIS_2_3
274264841Seric 	static struct hostent hp;
274364841Seric 	static char buf[1000];
274464841Seric 	extern struct hostent *_switch_gethostbyname_r();
274564841Seric 
274664841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
274764841Seric # else
274865009Seric 	extern struct hostent *__switch_gethostbyname();
274965009Seric 
275064841Seric 	return __switch_gethostbyname(name);
275164841Seric # endif
275264841Seric }
275364841Seric 
275464841Seric struct hostent *
275564841Seric solaris_gethostbyaddr(addr, len, type)
275665094Seric 	const char *addr;
275764841Seric 	int len;
275864841Seric 	int type;
275964841Seric {
276064841Seric # ifdef SOLARIS_2_3
276164841Seric 	static struct hostent hp;
276264841Seric 	static char buf[1000];
276364841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
276464841Seric 
276564841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
276664841Seric # else
276765009Seric 	extern struct hostent *__switch_gethostbyaddr();
276865009Seric 
276964841Seric 	return __switch_gethostbyaddr(addr, len, type);
277064841Seric # endif
277164841Seric }
277264841Seric 
277364841Seric #endif
277465214Seric /*
277565214Seric **  NI_PROPVAL -- netinfo property value lookup routine
277665214Seric **
277765214Seric **	Parameters:
2778*68482Seric **		keydir -- the Netinfo directory name in which to search
2779*68482Seric **			for the key.
2780*68482Seric **		keyprop -- the name of the property in which to find the
2781*68482Seric **			property we are interested.  Defaults to "name".
2782*68482Seric **		keyval -- the value for which we are really searching.
2783*68482Seric **		valprop -- the property name for the value in which we
2784*68482Seric **			are interested.
2785*68482Seric **		sepchar -- if non-nil, this can be multiple-valued, and
2786*68482Seric **			we should return a string separated by this
2787*68482Seric **			character.
278865214Seric **
278965214Seric **	Returns:
279065214Seric **		NULL -- if:
279165214Seric **			1. the directory is not found
279265214Seric **			2. the property name is not found
279365214Seric **			3. the property contains multiple values
279465214Seric **			4. some error occured
279565214Seric **		else -- the location of the config file.
279665214Seric **
2797*68482Seric **	Example:
2798*68482Seric **		To search for an alias value, use:
2799*68482Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
2800*68482Seric **
280165214Seric **	Notes:
280265214Seric **      	Caller should free the return value of ni_proval
280365214Seric */
280465214Seric 
280565214Seric #ifdef NETINFO
280665214Seric 
280765214Seric # include <netinfo/ni.h>
280865214Seric 
280965214Seric # define LOCAL_NETINFO_DOMAIN    "."
281065214Seric # define PARENT_NETINFO_DOMAIN   ".."
281165214Seric # define MAX_NI_LEVELS           256
281265214Seric 
281365214Seric char *
2814*68482Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
2815*68482Seric 	char *keydir;
2816*68482Seric 	char *keyprop;
2817*68482Seric 	char *keyval;
2818*68482Seric 	char *valprop;
2819*68482Seric 	char sepchar;
282065214Seric {
282165820Seric 	char *propval = NULL;
282265214Seric 	int i;
2823*68482Seric 	int j, alen;
282465214Seric 	void *ni = NULL;
282565214Seric 	void *lastni = NULL;
282665214Seric 	ni_status nis;
282765214Seric 	ni_id nid;
282865214Seric 	ni_namelist ninl;
2829*68482Seric 	register char *p;
2830*68482Seric 	char keybuf[1024];
283165214Seric 
283265214Seric 	/*
2833*68482Seric 	**  Create the full key from the two parts.
2834*68482Seric 	**
2835*68482Seric 	**	Note that directory can end with, e.g., "name=" to specify
2836*68482Seric 	**	an alternate search property.
2837*68482Seric 	*/
2838*68482Seric 
2839*68482Seric 	i = strlen(keydir) + strlen(keyval) + 2;
2840*68482Seric 	if (keyprop != NULL)
2841*68482Seric 		i += strlen(keyprop) + 1;
2842*68482Seric 	if (i > sizeof keybuf)
2843*68482Seric 		return NULL;
2844*68482Seric 	strcpy(keybuf, keydir);
2845*68482Seric 	strcat(keybuf, "/");
2846*68482Seric 	if (keyprop != NULL)
2847*68482Seric 	{
2848*68482Seric 		strcat(keybuf, keyprop);
2849*68482Seric 		strcat(keybuf, "=");
2850*68482Seric 	}
2851*68482Seric 	strcat(keybuf, keyval);
2852*68482Seric 
2853*68482Seric 	/*
285465214Seric 	**  If the passed directory and property name are found
285565214Seric 	**  in one of netinfo domains we need to search (starting
285665214Seric 	**  from the local domain moving all the way back to the
285765214Seric 	**  root domain) set propval to the property's value
285865214Seric 	**  and return it.
285965214Seric 	*/
286065214Seric 
286165214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
286265214Seric 	{
286365214Seric 		if (i == 0)
286465214Seric 		{
286565214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
286665214Seric 		}
286765214Seric 		else
286865214Seric 		{
286965214Seric 			if (lastni != NULL)
287065214Seric 				ni_free(lastni);
287165214Seric 			lastni = ni;
287265214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
287365214Seric 		}
287465214Seric 
287565214Seric 		/*
287665214Seric 		**  Don't bother if we didn't get a handle on a
287765214Seric 		**  proper domain.  This is not necessarily an error.
287865214Seric 		**  We would get a positive ni_status if, for instance
287965214Seric 		**  we never found the directory or property and tried
288065214Seric 		**  to open the parent of the root domain!
288165214Seric 		*/
288265214Seric 
288365214Seric 		if (nis != 0)
288465214Seric 			break;
288565214Seric 
288665214Seric 		/*
288765214Seric 		**  Find the path to the server information.
288865214Seric 		*/
288965214Seric 
2890*68482Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
289165214Seric 			continue;
289265214Seric 
289365214Seric 		/*
2894*68482Seric 		**  Find associated value information.
289565214Seric 		*/
289665214Seric 
2897*68482Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
289865214Seric 			continue;
289965214Seric 
290065214Seric 		/*
2901*68482Seric 		**  See if we have an acceptable number of values.
290265214Seric 		*/
290365214Seric 
2904*68482Seric 		if (ninl.ni_namelist_len <= 0)
2905*68482Seric 			continue;
2906*68482Seric 
2907*68482Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
290865214Seric 		{
2909*68482Seric 			ni_namelist_free(&ninl);
2910*68482Seric 			continue;
291165214Seric 		}
2912*68482Seric 
2913*68482Seric 		/*
2914*68482Seric 		**  Calculate number of bytes needed and build result
2915*68482Seric 		*/
2916*68482Seric 
2917*68482Seric 		alen = 1;
2918*68482Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
2919*68482Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
2920*68482Seric 		propval = p = xalloc(alen);
2921*68482Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
2922*68482Seric 		{
2923*68482Seric 			strcpy(p, ninl.ni_namelist_val[j]);
2924*68482Seric 			p += strlen(p);
2925*68482Seric 			*p++ = sepchar;
2926*68482Seric 		}
2927*68482Seric 		*--p = '\0';
2928*68482Seric 
2929*68482Seric 		ni_namelist_free(&ninl);
293065214Seric 	}
293165214Seric 
293265214Seric 	/*
293365214Seric 	**  Clean up.
293465214Seric 	*/
293565214Seric 
293665214Seric 	if (ni != NULL)
293765214Seric 		ni_free(ni);
293865214Seric 	if (lastni != NULL && ni != lastni)
293965214Seric 		ni_free(lastni);
294065214Seric 
294165214Seric 	return propval;
294265214Seric }
294365214Seric 
294465214Seric #endif /* NETINFO */
294565504Seric /*
294665504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
294765504Seric **
294865504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
294965504Seric **	syslog succeeds during interrupt handlers.
295065504Seric */
295165504Seric 
295265504Seric #ifdef __hpux
295365504Seric 
295465504Seric # define MAXSYSLOGTRIES	100
295565504Seric # undef syslog
295665504Seric 
295765504Seric # ifdef __STDC__
295865504Seric hard_syslog(int pri, char *msg, ...)
295965504Seric # else
296065504Seric hard_syslog(pri, msg, va_alist)
296165504Seric 	int pri;
296265504Seric 	char *msg;
296365504Seric 	va_dcl
296465504Seric # endif
296565504Seric {
296665504Seric 	int i;
296765504Seric 	char buf[SYSLOG_BUFSIZE * 2];
296865504Seric 	VA_LOCAL_DECL;
296965504Seric 
297065504Seric 	VA_START(msg);
297165504Seric 	vsprintf(buf, msg, ap);
297265504Seric 	VA_END;
297365504Seric 
297465504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
297565504Seric 		continue;
297665504Seric }
297765504Seric 
297865504Seric #endif
2979