xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68470)
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*68470Seric static char sccsid[] = "@(#)conf.c	8.136 (Berkeley) 03/01/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  */
5768461Seric 	"resent-sender",		H_FROM|H_RESENT,
5868461Seric 	"resent-from",			H_FROM|H_RESENT,
5968461Seric 	"resent-reply-to",		H_FROM|H_RESENT,
6068461Seric 	"sender",			H_FROM,
6168461Seric 	"from",				H_FROM,
6268461Seric 	"reply-to",			H_FROM,
6368461Seric 	"full-name",			H_ACHECK,
6468461Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
6568461Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
6868461Seric 	"to",				H_RCPT,
6968461Seric 	"resent-to",			H_RCPT|H_RESENT,
7068461Seric 	"cc",				H_RCPT,
7168461Seric 	"resent-cc",			H_RCPT|H_RESENT,
7268461Seric 	"bcc",				H_RCPT|H_STRIPVAL,
7368461Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
7468461Seric 	"apparently-to",		H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
7768461Seric 	"message-id",			0,
7868461Seric 	"resent-message-id",		H_RESENT,
7968461Seric 	"message",			H_EOH,
8068461Seric 	"text",				H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
8368461Seric 	"date",				0,
8468461Seric 	"resent-date",			H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
8768461Seric 	"received",			H_TRACE|H_FORCE,
8868461Seric 	"x400-received",		H_TRACE|H_FORCE,
8968461Seric 	"via",				H_TRACE|H_FORCE,
9068461Seric 	"mail-from",			H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
9368461Seric 	"comments",			H_FORCE,
9468461Seric 	"return-path",			H_FORCE|H_ACHECK,
9568461Seric 	"content-transfer-encoding",	H_CTE,
9668461Seric 	"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 
15768461Seric void
15858734Seric setdefaults(e)
15958734Seric 	register ENVELOPE *e;
16024943Seric {
16168461Seric 	int i;
16268461Seric 	extern void inittimeouts();
16368461Seric 	extern void setdefuser();
16468461Seric 	extern void setupmaps();
16568461Seric 	extern void setupmailers();
16668461Seric 
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 */
18268461Seric 	SevenBitInput = FALSE;			/* option 7 */
18357438Seric 	MaxMciCache = 1;			/* option k */
18457438Seric 	MciCacheTimeout = 300;			/* option K */
18557438Seric 	LogLevel = 9;				/* option L */
18668461Seric 	inittimeouts(NULL);			/* option r */
18758853Seric 	PrivacyFlags = 0;			/* option p */
18868461Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18968461Seric 	for (i = 0; i < MAXTOCLASS; i++)
19068461Seric 	{
19168461Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19268461Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19368461Seric 	}
19468461Seric 	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 
20568461Seric 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 
22168461Seric bool	host_map_init __P((MAP *map, char *args));
22268461Seric 
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 
25568461Seric void
25657402Seric setupmailers()
25757402Seric {
25857402Seric 	char buf[100];
25968461Seric 	extern void makemailer();
26057402Seric 
26168461Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u");
26257403Seric 	makemailer(buf);
26357403Seric 
26468461Seric 	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 
29268461Seric 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);
30168461Seric 
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,
31568461Seric 		map_parseargs, nis_map_open, null_map_close,
31668461Seric 		nis_map_lookup, null_map_store);
31760207Seric #endif
31860207Seric 
31968461Seric #ifdef NISPLUS
32068461Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32168461Seric 		map_parseargs, nisplus_map_open, null_map_close,
32268461Seric 		nisplus_map_lookup, null_map_store);
32368461Seric #endif
32468461Seric 
32568461Seric #ifdef HESIOD
32668461Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
32768461Seric 		map_parseargs, null_map_open, null_map_close,
32868461Seric 		hes_map_lookup, null_map_store);
32968461Seric #endif
33068461Seric 
33168461Seric #ifdef NETINFO
33268461Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33368461Seric 		map_parseargs, ni_map_open, null_map_close,
33468461Seric 		ni_map_lookup, null_map_store);
33568461Seric #endif
33668461Seric 
33768461Seric #if 0
33868461Seric 	MAPDEF("dns", NULL, 0,
33968461Seric 		dns_map_init, null_map_open, null_map_close,
34068461Seric 		dns_map_lookup, null_map_store);
34168461Seric #endif
34268461Seric 
34368461Seric #if NAMED_BIND
34468461Seric 	/* best MX DNS lookup */
34568461Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
34668461Seric 		map_parseargs, null_map_open, null_map_close,
34768461Seric 		bestmx_map_lookup, null_map_store);
34868461Seric #endif
34968461Seric 
35068461Seric 	MAPDEF("host", NULL, 0,
35168461Seric 		host_map_init, null_map_open, null_map_close,
35268461Seric 		host_map_lookup, null_map_store);
35368461Seric 
35468461Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35568461Seric 		map_parseargs, text_map_open, null_map_close,
35668461Seric 		text_map_lookup, null_map_store);
35768461Seric 
35860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
35968461Seric 		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 
36668461Seric 	/* access to system passwd file */
36768461Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
36868461Seric 		map_parseargs, user_map_open, null_map_close,
36968461Seric 		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
38468461Seric 
38568461Seric 	/* sequenced maps */
38668461Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
38768461Seric 		seq_map_parse, null_map_open, null_map_close,
38868461Seric 		seq_map_lookup, seq_map_store);
38968461Seric 
39068461Seric 	/* switched interface to sequenced maps */
39168461Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
39268461Seric 		map_parseargs, switch_map_open, null_map_close,
39368461Seric 		seq_map_lookup, seq_map_store);
39460207Seric }
39560207Seric 
39660207Seric #undef MAPDEF
39760207Seric /*
39868461Seric **  INITHOSTMAPS -- initial host-dependent maps
39968461Seric **
40068461Seric **	This should act as an interface to any local service switch
40168461Seric **	provided by the host operating system.
40268461Seric **
40368461Seric **	Parameters:
40468461Seric **		none
40568461Seric **
40668461Seric **	Returns:
40768461Seric **		none
40868461Seric **
40968461Seric **	Side Effects:
41068461Seric **		Should define maps "host" and "users" as necessary
41168461Seric **		for this OS.  If they are not defined, they will get
41268461Seric **		a default value later.  It should check to make sure
41368461Seric **		they are not defined first, since it's possible that
41468461Seric **		the config file has provided an override.
41568461Seric */
41668461Seric 
41768461Seric void
41868461Seric inithostmaps()
41968461Seric {
42068461Seric 	register int i;
42168461Seric 	int nmaps;
42268461Seric 	char *maptype[MAXMAPSTACK];
42368461Seric 	short mapreturn[MAXMAPACTIONS];
42468461Seric 	char buf[MAXLINE];
42568461Seric 
42668461Seric 	/*
42768461Seric 	**  Set up default hosts maps.
42868461Seric 	*/
42968461Seric 
43068461Seric #if 0
43168461Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
43268461Seric 	for (i = 0; i < nmaps; i++)
43368461Seric 	{
43468461Seric 		if (strcmp(maptype[i], "files") == 0 &&
43568461Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
43668461Seric 		{
43768461Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
43868461Seric 			makemapentry(buf);
43968461Seric 		}
44068461Seric #if NAMED_BIND
44168461Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
44268461Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
44368461Seric 		{
44468461Seric 			strcpy(buf, "hosts.dns dns A");
44568461Seric 			makemapentry(buf);
44668461Seric 		}
44768461Seric #endif
44868461Seric #ifdef NISPLUS
44968461Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
45068461Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
45168461Seric 		{
45268461Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
45368461Seric 			makemapentry(buf);
45468461Seric 		}
45568461Seric #endif
45668461Seric #ifdef NIS
45768461Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
45868461Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
45968461Seric 		{
46068461Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
46168461Seric 			makemapentry(buf);
46268461Seric 		}
46368461Seric #endif
46468461Seric 	}
46568461Seric #endif
46668461Seric 
46768461Seric 	/*
46868461Seric 	**  Make sure we have a host map.
46968461Seric 	*/
47068461Seric 
47168461Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
47268461Seric 	{
47368461Seric 		/* user didn't initialize: set up host map */
47468461Seric 		strcpy(buf, "host host");
47568461Seric #if NAMED_BIND
47668461Seric 		if (ConfigLevel >= 2)
47768461Seric 			strcat(buf, " -a.");
47868461Seric #endif
47968461Seric 		makemapentry(buf);
48068461Seric 	}
48168461Seric 
48268461Seric 	/*
48368461Seric 	**  Set up default aliases maps
48468461Seric 	*/
48568461Seric 
48668461Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
48768461Seric 	for (i = 0; i < nmaps; i++)
48868461Seric 	{
48968461Seric 		if (strcmp(maptype[i], "files") == 0 &&
49068461Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
49168461Seric 		{
49268461Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
49368461Seric 			makemapentry(buf);
49468461Seric 		}
49568461Seric #ifdef NISPLUS
49668461Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
49768461Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
49868461Seric 		{
49968461Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
50068461Seric 			makemapentry(buf);
50168461Seric 		}
50268461Seric #endif
50368461Seric #ifdef NIS
50468461Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
50568461Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
50668461Seric 		{
50768461Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
50868461Seric 			makemapentry(buf);
50968461Seric 		}
51068461Seric #endif
51168461Seric 	}
51268461Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
51368461Seric 	{
51468461Seric 		strcpy(buf, "aliases switch aliases");
51568461Seric 		makemapentry(buf);
51668461Seric 	}
51768461Seric 	strcpy(buf, "switch:aliases");
51868461Seric 	setalias(buf);
51968461Seric 
52068461Seric #if 0		/* "user" map class is a better choice */
52168461Seric 	/*
52268461Seric 	**  Set up default users maps.
52368461Seric 	*/
52468461Seric 
52568461Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
52668461Seric 	for (i = 0; i < nmaps; i++)
52768461Seric 	{
52868461Seric 		if (strcmp(maptype[i], "files") == 0 &&
52968461Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
53068461Seric 		{
53168461Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
53268461Seric 			makemapentry(buf);
53368461Seric 		}
53468461Seric #ifdef NISPLUS
53568461Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
53668461Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
53768461Seric 		{
53868461Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
53968461Seric 			makemapentry(buf);
54068461Seric 		}
54168461Seric #endif
54268461Seric #ifdef NIS
54368461Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
54468461Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
54568461Seric 		{
54668461Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
54768461Seric 			makemapentry(buf);
54868461Seric 		}
54968461Seric #endif
55068461Seric #ifdef HESIOD
55168461Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
55268461Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
55368461Seric 		{
55468461Seric 			strcpy(buf, "users.hesiod hesiod");
55568461Seric 			makemapentry(buf);
55668461Seric 		}
55768461Seric #endif
55868461Seric 	}
55968461Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
56068461Seric 	{
56168461Seric 		strcpy(buf, "users switch -m passwd");
56268461Seric 		makemapentry(buf);
56368461Seric 	}
56468461Seric #endif
56568461Seric }
56668461Seric /*
56768461Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
56868461Seric **
56968461Seric **	This is the system-dependent interface to the service switch.
57068461Seric **
57168461Seric **	Parameters:
57268461Seric **		service -- the name of the service of interest.
57368461Seric **		maptype -- an out-array of strings containing the types
57468461Seric **			of access to use for this service.  There can
57568461Seric **			be at most MAXMAPSTACK types for a single service.
57668461Seric **		mapreturn -- an out-array of return information bitmaps
57768461Seric **			for the map.
57868461Seric **
57968461Seric **	Returns:
58068461Seric **		The number of map types filled in, or -1 for failure.
58168461Seric */
58268461Seric 
58368461Seric #ifdef SOLARIS
58468461Seric # include <nsswitch.h>
58568461Seric #endif
58668461Seric 
58768461Seric #if defined(ultrix) || defined(__osf__)
58868461Seric # include <sys/svcinfo.h>
58968461Seric #endif
59068461Seric 
59168461Seric int
59268461Seric switch_map_find(service, maptype, mapreturn)
59368461Seric 	char *service;
59468461Seric 	char *maptype[MAXMAPSTACK];
59568461Seric 	short mapreturn[MAXMAPACTIONS];
59668461Seric {
59768461Seric 	register FILE *fp;
59868461Seric 	int svcno;
59968461Seric 	static char buf[MAXLINE];
60068461Seric 
60168461Seric #ifdef SOLARIS
60268461Seric 	struct __nsw_switchconfig *nsw_conf;
60368461Seric 	enum __nsw_parse_err pserr;
60468461Seric 	struct __nsw_lookup *lk;
60568461Seric 	int nsw_rc;
60668461Seric 	static struct __nsw_lookup lkp0 =
60768461Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
60868461Seric 	static struct __nsw_switchconfig lkp_default =
60968461Seric 		{ 0, "sendmail", 3, &lkp0 };
61068461Seric 
61168461Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
61268461Seric 		lk = lkp_default.lookups;
61368461Seric 	else
61468461Seric 		lk = nsw_conf->lookups;
61568461Seric 	svcno = 0;
61668461Seric 	while (lk != NULL)
61768461Seric 	{
61868461Seric 		maptype[svcno] = lk->service_name;
61968461Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
62068461Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
62168461Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
62268461Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62368461Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
62468461Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62568461Seric 		svcno++;
62668461Seric 		lk = lk->next;
62768461Seric 	}
62868461Seric 	return svcno;
62968461Seric #endif
63068461Seric 
63168461Seric #if defined(ultrix) || defined(__osf__)
63268461Seric 	struct svcinfo *svcinfo;
63368461Seric 	int svc;
63468461Seric 
63568461Seric 	svcinfo = getsvc();
63668461Seric 	if (svcinfo == NULL)
63768461Seric 		goto punt;
63868461Seric 	if (strcmp(service, "hosts") == 0)
63968461Seric 		svc = SVC_HOSTS;
64068461Seric 	else if (strcmp(service, "aliases") == 0)
64168461Seric 		svc = SVC_ALIASES;
64268461Seric 	else if (strcmp(service, "passwd") == 0)
64368461Seric 		svc = SVC_PASSWD;
64468461Seric 	else
64568461Seric 		return -1;
64668461Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
64768461Seric 	{
64868461Seric 		switch (svcinfo->svcpath[svc][svcno])
64968461Seric 		{
65068461Seric 		  case SVC_LOCAL:
65168461Seric 			maptype[svcno] = "files";
65268461Seric 			break;
65368461Seric 
65468461Seric 		  case SVC_YP:
65568461Seric 			maptype[svcno] = "nis";
65668461Seric 			break;
65768461Seric 
65868461Seric 		  case SVC_BIND:
65968461Seric 			maptype[svcno] = "dns";
66068461Seric 			break;
66168461Seric 
66268461Seric #ifdef SVC_HESIOD
66368461Seric 		  case SVC_HESIOD:
66468461Seric 			maptype[svcno] = "hesiod";
66568461Seric 			break;
66668461Seric #endif
66768461Seric 
66868461Seric 		  case SVC_LAST:
66968461Seric 			return svcno;
67068461Seric 		}
67168461Seric 	}
67268461Seric 	return svcno;
67368461Seric #endif
67468461Seric 
67568461Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
67668461Seric 	/*
67768461Seric 	**  Fall-back mechanism.
67868461Seric 	*/
67968461Seric 
68068461Seric 	svcno = 0;
68168461Seric 	fp = fopen(ServiceSwitchFile, "r");
68268461Seric 	if (fp != NULL)
68368461Seric 	{
68468461Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
68568461Seric 		{
68668461Seric 			register char *p;
68768461Seric 
68868461Seric 			p = strpbrk(buf, "#\n");
68968461Seric 			if (p != NULL)
69068461Seric 				*p = '\0';
69168461Seric 			p = strpbrk(buf, " \t");
69268461Seric 			if (p != NULL)
69368461Seric 				*p++ = '\0';
69468461Seric 			if (strcmp(buf, service) != 0)
69568461Seric 				continue;
69668461Seric 
69768461Seric 			/* got the right service -- extract data */
69868461Seric 			do
69968461Seric 			{
70068461Seric 				while (isspace(*p))
70168461Seric 					p++;
70268461Seric 				if (*p == '\0')
70368461Seric 					break;
70468461Seric 				maptype[svcno++] = p;
70568461Seric 				p = strpbrk(p, " \t");
70668461Seric 				if (p != NULL)
70768461Seric 					*p++ = '\0';
70868461Seric 			} while (p != NULL);
70968461Seric 			break;
71068461Seric 		}
71168461Seric 		fclose(fp);
71268461Seric 		return svcno;
71368461Seric 	}
71468461Seric #endif
71568461Seric 
71668461Seric 	/* if the service file doesn't work, use an absolute fallback */
71768461Seric   punt:
71868461Seric 	if (strcmp(service, "aliases") == 0)
71968461Seric 	{
72068461Seric 		maptype[0] = "files";
72168461Seric 		return 1;
72268461Seric 	}
72368461Seric 	if (strcmp(service, "hosts") == 0)
72468461Seric 	{
72568461Seric # if NAMED_BIND
72668461Seric 		maptype[svcno++] = "dns";
72768461Seric # else
72868461Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
72968461Seric 		/* SunOS */
73068461Seric 		maptype[svcno++] = "nis";
73168461Seric #  endif
73268461Seric # endif
73368461Seric 		maptype[svcno++] = "files";
73468461Seric 		return svcno;
73568461Seric 	}
73668461Seric 	return -1;
73768461Seric }
73868461Seric /*
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 **
85968461Seric **	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 
87268461Seric 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);
89168461Seric 	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");
89568461Seric 		/* 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;
91968461Seric # ifdef SA_RESTART
92068461Seric 	n.sa_flags = SA_RESTART;
92168461Seric # 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 
94068461Seric 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 
95968461Seric 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 
97468461Seric void
97564825Seric init_md(argc, argv)
97664825Seric 	int argc;
97764825Seric 	char **argv;
97864705Seric {
97964705Seric #ifdef _AUX_SOURCE
98064705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
98164705Seric #endif
98268461Seric 
98368461Seric #ifdef VENDOR_DEFAULT
98468461Seric 	VendorCode = VENDOR_DEFAULT;
98568461Seric #else
98668461Seric 	VendorCode = VENDOR_BERKELEY;
98768461Seric #endif
98864705Seric }
98964705Seric /*
99014872Seric **  GETLA -- get the current load average
99114872Seric **
99214881Seric **	This code stolen from la.c.
99314881Seric **
99414872Seric **	Parameters:
99514872Seric **		none.
99614872Seric **
99714872Seric **	Returns:
99814872Seric **		The current load average as an integer.
99914872Seric **
100014872Seric **	Side Effects:
100114872Seric **		none.
100214872Seric */
100314872Seric 
100451920Seric /* try to guess what style of load average we have */
100551920Seric #define LA_ZERO		1	/* always return load average as zero */
100664360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
100751920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
100851920Seric #define LA_SUBR		4	/* call getloadavg */
100964295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
101064360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
101166301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
101214872Seric 
101363962Seric /* do guesses based on general OS type */
101451920Seric #ifndef LA_TYPE
101563962Seric # define LA_TYPE	LA_ZERO
101651920Seric #endif
101751920Seric 
101864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
101951920Seric 
102014872Seric #include <nlist.h>
102114872Seric 
102268461Seric #ifdef IRIX64
102368461Seric # define nlist		nlist64
102468461Seric #endif
102568461Seric 
102651920Seric #ifndef LA_AVENRUN
102763962Seric # ifdef SYSTEM5
102863962Seric #  define LA_AVENRUN	"avenrun"
102963962Seric # else
103063962Seric #  define LA_AVENRUN	"_avenrun"
103163962Seric # endif
103251920Seric #endif
103351920Seric 
103451920Seric /* _PATH_UNIX should be defined in <paths.h> */
103551920Seric #ifndef _PATH_UNIX
103663962Seric # if defined(SYSTEM5)
103763962Seric #  define _PATH_UNIX	"/unix"
103863962Seric # else
103963962Seric #  define _PATH_UNIX	"/vmunix"
104063962Seric # endif
104151920Seric #endif
104251920Seric 
104314872Seric struct	nlist Nl[] =
104414872Seric {
104551920Seric 	{ LA_AVENRUN },
104614872Seric #define	X_AVENRUN	0
104714872Seric 	{ 0 },
104814872Seric };
104914872Seric 
105060583Seric #ifndef FSHIFT
105160583Seric # if defined(unixpc)
105260583Seric #  define FSHIFT	5
105360583Seric # endif
105457736Seric 
105566763Seric # if defined(__alpha) || defined(IRIX)
105660583Seric #  define FSHIFT	10
105760583Seric # endif
105868461Seric 
105968461Seric # if defined(_AIX3)
106068461Seric #  define FSHIFT	16
106168461Seric # endif
106266788Seric #endif
106357977Seric 
106466788Seric #ifndef FSHIFT
106566788Seric # define FSHIFT		8
106657736Seric #endif
106760583Seric 
106866788Seric #ifndef FSCALE
106966788Seric # define FSCALE		(1 << FSHIFT)
107051920Seric #endif
107140930Srick 
107214872Seric getla()
107314872Seric {
107414872Seric 	static int kmem = -1;
107551920Seric #if LA_TYPE == LA_INT
107624943Seric 	long avenrun[3];
107751920Seric #else
107864360Seric # if LA_TYPE == LA_SHORT
107964360Seric 	short avenrun[3];
108064360Seric # else
108151920Seric 	double avenrun[3];
108264360Seric # endif
108351920Seric #endif
108425615Seric 	extern off_t lseek();
108557736Seric 	extern int errno;
108614872Seric 
108714872Seric 	if (kmem < 0)
108814872Seric 	{
108924945Seric 		kmem = open("/dev/kmem", 0, 0);
109014872Seric 		if (kmem < 0)
109157736Seric 		{
109257736Seric 			if (tTd(3, 1))
109357736Seric 				printf("getla: open(/dev/kmem): %s\n",
109457736Seric 					errstring(errno));
109514872Seric 			return (-1);
109657736Seric 		}
109751920Seric 		(void) fcntl(kmem, F_SETFD, 1);
109868461Seric #ifdef _AIX3
109968461Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
110068461Seric #else
110157736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
110268461Seric #endif
110357736Seric 		{
110457736Seric 			if (tTd(3, 1))
110557736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
110657736Seric 					errstring(errno));
110714872Seric 			return (-1);
110857736Seric 		}
110959253Seric 		if (Nl[X_AVENRUN].n_value == 0)
111059253Seric 		{
111159253Seric 			if (tTd(3, 1))
111259253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
111359253Seric 					_PATH_UNIX, LA_AVENRUN);
111459253Seric 			return (-1);
111559253Seric 		}
111668461Seric #ifdef NAMELISTMASK
111768461Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
111866763Seric #endif
111914872Seric 	}
112057736Seric 	if (tTd(3, 20))
112157736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
112268461Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
112323118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
112419967Seric 	{
112519967Seric 		/* thank you Ian */
112657736Seric 		if (tTd(3, 1))
112757736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
112819967Seric 		return (-1);
112919967Seric 	}
113064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
113157736Seric 	if (tTd(3, 5))
113257736Seric 	{
113357736Seric 		printf("getla: avenrun = %d", avenrun[0]);
113457736Seric 		if (tTd(3, 15))
113557736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
113657736Seric 		printf("\n");
113757736Seric 	}
113857736Seric 	if (tTd(3, 1))
113957736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
114024943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
114151920Seric #else
114257736Seric 	if (tTd(3, 5))
114357736Seric 	{
114457736Seric 		printf("getla: avenrun = %g", avenrun[0]);
114557736Seric 		if (tTd(3, 15))
114657736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
114757736Seric 		printf("\n");
114857736Seric 	}
114957736Seric 	if (tTd(3, 1))
115057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
115151920Seric 	return ((int) (avenrun[0] + 0.5));
115251920Seric #endif
115314872Seric }
115414872Seric 
115551773Seric #else
115651920Seric #if LA_TYPE == LA_SUBR
115751773Seric 
115864718Seric #ifdef DGUX
115964718Seric 
116064718Seric #include <sys/dg_sys_info.h>
116164718Seric 
116268461Seric int
116368461Seric getla()
116464718Seric {
116564718Seric 	struct dg_sys_info_load_info load_info;
116664718Seric 
116764718Seric 	dg_sys_info((long *)&load_info,
116864718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
116964718Seric 
117068461Seric         if (tTd(3, 1))
117168461Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
117268461Seric 
117364718Seric 	return((int) (load_info.one_minute + 0.5));
117464718Seric }
117564718Seric 
117664718Seric #else
117768461Seric # ifdef __hpux
117864718Seric 
117968461Seric #  include <sys/param.h>
118068461Seric #  include <sys/pstat.h>
118168461Seric 
118268461Seric int
118351773Seric getla()
118451773Seric {
118568461Seric 	struct pst_dynamic pstd;
118668461Seric 
118768461Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
118868461Seric 			     (size_t) 1 ,0) == -1)
118968461Seric 		return 0;
119068461Seric 
119168461Seric         if (tTd(3, 1))
119268461Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
119368461Seric 
119468461Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
119568461Seric }
119668461Seric 
119768461Seric # else
119868461Seric 
119968461Seric int
120068461Seric getla()
120168461Seric {
120251920Seric 	double avenrun[3];
120351920Seric 
120451920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
120557736Seric 	{
120657736Seric 		if (tTd(3, 1))
120757736Seric 			perror("getla: getloadavg failed:");
120851920Seric 		return (-1);
120957736Seric 	}
121057736Seric 	if (tTd(3, 1))
121157736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
121251920Seric 	return ((int) (avenrun[0] + 0.5));
121351773Seric }
121451773Seric 
121568461Seric # endif /* __hpux */
121664718Seric #endif /* DGUX */
121751773Seric #else
121864295Seric #if LA_TYPE == LA_MACH
121951773Seric 
122064295Seric /*
122165173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
122264295Seric */
122364295Seric 
122465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
122565173Seric # include <mach/mach.h>
122665173Seric #else
122765173Seric # include <mach.h>
122865173Seric #endif
122964295Seric 
123051773Seric getla()
123151773Seric {
123264295Seric 	processor_set_t default_set;
123364295Seric 	kern_return_t error;
123464295Seric 	unsigned int info_count;
123564295Seric 	struct processor_set_basic_info info;
123664295Seric 	host_t host;
123764295Seric 
123864295Seric 	error = processor_set_default(host_self(), &default_set);
123964295Seric 	if (error != KERN_SUCCESS)
124064295Seric 		return -1;
124164295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
124264295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
124364295Seric 			       &host, (processor_set_info_t)&info,
124464295Seric 			       &info_count) != KERN_SUCCESS)
124564295Seric 	{
124664295Seric 		return -1;
124764295Seric 	}
124864295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
124964295Seric }
125064295Seric 
125164295Seric 
125264295Seric #else
125366313Seric #if LA_TYPE == LA_PROCSTR
125464295Seric 
125566301Seric /*
125666301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
125766301Seric **  in a format like "0.15 0.12 0.06".
125866301Seric **
125966301Seric **	Initially intended for Linux.  This has been in the kernel
126066301Seric **	since at least 0.99.15.
126166301Seric */
126266301Seric 
126366301Seric # ifndef _PATH_LOADAVG
126466301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
126566301Seric # endif
126666301Seric 
126766301Seric int
126864295Seric getla()
126964295Seric {
127066319Seric 	double avenrun;
127166301Seric 	register int result;
127266319Seric 	FILE *fp;
127366301Seric 
127466319Seric 	fp = fopen(_PATH_LOADAVG, "r");
127566319Seric 	if (fp == NULL)
127666301Seric 	{
127766319Seric 		if (tTd(3, 1))
127866319Seric 			printf("getla: fopen(%s): %s\n",
127966319Seric 				_PATH_LOADAVG, errstring(errno));
128066319Seric 		return -1;
128166301Seric 	}
128266301Seric 	result = fscanf(fp, "%lf", &avenrun);
128366319Seric 	fclose(fp);
128466301Seric 	if (result != 1)
128566301Seric 	{
128666310Seric 		if (tTd(3, 1))
128766301Seric 			printf("getla: fscanf() = %d: %s\n",
128866301Seric 				result, errstring(errno));
128966301Seric 		return -1;
129066301Seric 	}
129166301Seric 
129257736Seric 	if (tTd(3, 1))
129366301Seric 		printf("getla(): %.2f\n", avenrun);
129466301Seric 
129566301Seric 	return ((int) (avenrun + 0.5));
129666301Seric }
129766301Seric 
129866301Seric #else
129966301Seric 
130066301Seric getla()
130166301Seric {
130266301Seric 	if (tTd(3, 1))
130357736Seric 		printf("getla: ZERO\n");
130451920Seric 	return (0);
130551773Seric }
130651773Seric 
130751773Seric #endif
130851773Seric #endif
130964295Seric #endif
131066301Seric #endif
131166045Seric 
131266045Seric 
131366045Seric /*
131466045Seric  * Copyright 1989 Massachusetts Institute of Technology
131566045Seric  *
131666045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
131766045Seric  * documentation for any purpose is hereby granted without fee, provided that
131866045Seric  * the above copyright notice appear in all copies and that both that
131966045Seric  * copyright notice and this permission notice appear in supporting
132066045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
132166045Seric  * publicity pertaining to distribution of the software without specific,
132266045Seric  * written prior permission.  M.I.T. makes no representations about the
132366045Seric  * suitability of this software for any purpose.  It is provided "as is"
132466045Seric  * without express or implied warranty.
132566045Seric  *
132666045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
132766045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
132866045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
132966045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
133066045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
133166045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
133266045Seric  *
133366045Seric  * Authors:  Many and varied...
133466045Seric  */
133566045Seric 
133666045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
133766045Seric #ifndef lint
133866045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
133966045Seric #endif /* !lint */
134066045Seric 
134166179Seric #ifdef apollo
134266045Seric # undef volatile
134366045Seric #    include <apollo/base.h>
134466045Seric 
134566045Seric /* ARGSUSED */
134666045Seric int getloadavg( call_data )
134766045Seric      caddr_t	call_data;	/* pointer to (double) return value */
134866045Seric {
134966045Seric      double *avenrun = (double *) call_data;
135066045Seric      int i;
135166045Seric      status_$t      st;
135266045Seric      long loadav[3];
135366045Seric      proc1_$get_loadav(loadav, &st);
135466045Seric      *avenrun = loadav[0] / (double) (1 << 16);
135566045Seric      return(0);
135666045Seric }
135766045Seric #   endif /* apollo */
135824943Seric /*
135924943Seric **  SHOULDQUEUE -- should this message be queued or sent?
136024943Seric **
136124943Seric **	Compares the message cost to the load average to decide.
136224943Seric **
136324943Seric **	Parameters:
136424943Seric **		pri -- the priority of the message in question.
136557438Seric **		ctime -- the message creation time.
136624943Seric **
136724943Seric **	Returns:
136824943Seric **		TRUE -- if this message should be queued up for the
136924943Seric **			time being.
137024943Seric **		FALSE -- if the load is low enough to send this message.
137124943Seric **
137224943Seric **	Side Effects:
137324943Seric **		none.
137424943Seric */
137524943Seric 
137624943Seric bool
137757438Seric shouldqueue(pri, ctime)
137824943Seric 	long pri;
137957438Seric 	time_t ctime;
138024943Seric {
138168461Seric 	bool rval;
138268461Seric 
138368461Seric 	if (tTd(3, 30))
138468461Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
138551920Seric 	if (CurrentLA < QueueLA)
138668461Seric 	{
138768461Seric 		if (tTd(3, 30))
138868461Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
138924943Seric 		return (FALSE);
139068461Seric 	}
139158132Seric 	if (CurrentLA >= RefuseLA)
139268461Seric 	{
139368461Seric 		if (tTd(3, 30))
139468461Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
139558132Seric 		return (TRUE);
139668461Seric 	}
139768461Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
139868461Seric 	if (tTd(3, 30))
139968461Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
140068461Seric 	return rval;
140124943Seric }
140224943Seric /*
140353037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
140453037Seric **
140553037Seric **	Parameters:
140653037Seric **		none.
140753037Seric **
140853037Seric **	Returns:
140953037Seric **		TRUE if incoming SMTP connections should be refused
141053037Seric **			(for now).
141153037Seric **		FALSE if we should accept new work.
141253037Seric **
141353037Seric **	Side Effects:
141453037Seric **		none.
141553037Seric */
141653037Seric 
141753037Seric bool
141853037Seric refuseconnections()
141953037Seric {
142068461Seric 	extern bool enoughspace();
142168461Seric 
142259156Seric #ifdef XLA
142359156Seric 	if (!xla_smtp_ok())
142459156Seric 		return TRUE;
142559156Seric #endif
142659156Seric 
142753037Seric 	/* this is probably too simplistic */
142868461Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
142953037Seric }
143053037Seric /*
143124943Seric **  SETPROCTITLE -- set process title for ps
143224943Seric **
143324943Seric **	Parameters:
143458674Seric **		fmt -- a printf style format string.
143558674Seric **		a, b, c -- possible parameters to fmt.
143624943Seric **
143724943Seric **	Returns:
143824943Seric **		none.
143924943Seric **
144024943Seric **	Side Effects:
144124943Seric **		Clobbers argv of our main procedure so ps(1) will
144224943Seric **		display the title.
144324943Seric */
144424943Seric 
144568461Seric #define SPT_NONE	0	/* don't use it at all */
144668461Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
144768461Seric #define SPT_BUILTIN	2	/* use libc builtin */
144868461Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
144968461Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
145068461Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
145168461Seric 
145268461Seric #ifndef SPT_TYPE
145368461Seric # define SPT_TYPE	SPT_REUSEARGV
145468461Seric #endif
145568461Seric 
145668461Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
145768461Seric 
145868461Seric # if SPT_TYPE == SPT_PSTAT
145958689Seric #  include <sys/pstat.h>
146058689Seric # endif
146168461Seric # if SPT_TYPE == SPT_PSSTRINGS
146259732Seric #  include <machine/vmparam.h>
146359732Seric #  include <sys/exec.h>
146468461Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
146568461Seric #   undef SPT_TYPE
146668461Seric #   define SPT_TYPE	SPT_REUSEARGV
146764247Seric #  endif
146859732Seric # endif
146968461Seric 
147068461Seric # if SPT_TYPE == SPT_PSSTRINGS
147168461Seric #  define SETPROC_STATIC	static
147268461Seric # else
147359732Seric #  define SETPROC_STATIC
147459732Seric # endif
147567626Seric 
147668461Seric # ifndef SPT_PADCHAR
147768461Seric #  define SPT_PADCHAR	' '
147868461Seric # endif
147958689Seric 
148068461Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
148164843Seric 
148268461Seric #if SPT_TYPE != SPT_BUILTIN
148368461Seric 
148424943Seric /*VARARGS1*/
148568461Seric void
148668461Seric # ifdef __STDC__
148757642Seric setproctitle(char *fmt, ...)
148868461Seric # else
148957642Seric setproctitle(fmt, va_alist)
149024943Seric 	char *fmt;
149157642Seric 	va_dcl
149268461Seric # endif
149324943Seric {
149468461Seric # if SPT_TYPE != SPT_NONE
149524943Seric 	register char *p;
149625049Seric 	register int i;
149759732Seric 	SETPROC_STATIC char buf[MAXLINE];
149856852Seric 	VA_LOCAL_DECL
149968461Seric #  if SPT_TYPE == SPT_PSTAT
150058689Seric 	union pstun pst;
150158689Seric #  endif
150224943Seric 	extern char **Argv;
150324943Seric 	extern char *LastArgv;
150424943Seric 
150558674Seric 	p = buf;
150624943Seric 
150758674Seric 	/* print sendmail: heading for grep */
150858674Seric 	(void) strcpy(p, "sendmail: ");
150958674Seric 	p += strlen(p);
151024943Seric 
151158674Seric 	/* print the argument string */
151258674Seric 	VA_START(fmt);
151358674Seric 	(void) vsprintf(p, fmt, ap);
151456852Seric 	VA_END;
151554996Seric 
151658674Seric 	i = strlen(buf);
151758689Seric 
151868461Seric #  if SPT_TYPE == SPT_PSTAT
151958689Seric 	pst.pst_command = buf;
152058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
152158689Seric #  else
152268461Seric #   if SPT_TYPE == SPT_PSSTRINGS
152359732Seric 	PS_STRINGS->ps_nargvstr = 1;
152459732Seric 	PS_STRINGS->ps_argvstr = buf;
152559732Seric #   else
152654996Seric 	if (i > LastArgv - Argv[0] - 2)
152725049Seric 	{
152854996Seric 		i = LastArgv - Argv[0] - 2;
152958674Seric 		buf[i] = '\0';
153025049Seric 	}
153158674Seric 	(void) strcpy(Argv[0], buf);
153254997Seric 	p = &Argv[0][i];
153324943Seric 	while (p < LastArgv)
153468461Seric 		*p++ = SPT_PADCHAR;
153568461Seric 	Argv[1] = NULL;
153668461Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
153768461Seric #  endif /* SPT_TYPE == SPT_PSTAT */
153868461Seric # endif /* SPT_TYPE != SPT_NONE */
153924943Seric }
154065982Seric 
154168461Seric #endif /* SPT_TYPE != SPT_BUILTIN */
154225698Seric /*
154325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
154425698Seric **
154525698Seric **	Parameters:
154668461Seric **		sig -- the signal that got us here (unused).
154725698Seric **
154825698Seric **	Returns:
154925698Seric **		none.
155025698Seric **
155125698Seric **	Side Effects:
155225698Seric **		Picks up extant zombies.
155325698Seric */
155425698Seric 
155546928Sbostic void
155668461Seric reapchild(sig)
155768461Seric 	int sig;
155825698Seric {
155964738Seric 	int olderrno = errno;
156063966Seric # ifdef HASWAITPID
156160219Seric 	auto int status;
156260560Seric 	int count;
156360560Seric 	int pid;
156460219Seric 
156560560Seric 	count = 0;
156660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
156760560Seric 	{
156860560Seric 		if (count++ > 1000)
156960560Seric 		{
157066748Seric #ifdef LOG
157160560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
157260560Seric 				pid, status);
157366748Seric #endif
157460560Seric 			break;
157560560Seric 		}
157660560Seric 	}
157760219Seric # else
157825698Seric # ifdef WNOHANG
157925698Seric 	union wait status;
158025698Seric 
158163753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
158225698Seric 		continue;
158356795Seric # else /* WNOHANG */
158425698Seric 	auto int status;
158525698Seric 
158660219Seric 	while (wait(&status) > 0)
158725698Seric 		continue;
158856795Seric # endif /* WNOHANG */
158960219Seric # endif
159064035Seric # ifdef SYS5SIGNALS
159164035Seric 	(void) setsignal(SIGCHLD, reapchild);
159258061Seric # endif
159364738Seric 	errno = olderrno;
159425698Seric }
159555418Seric /*
159655418Seric **  UNSETENV -- remove a variable from the environment
159755418Seric **
159855418Seric **	Not needed on newer systems.
159955418Seric **
160055418Seric **	Parameters:
160155418Seric **		name -- the string name of the environment variable to be
160255418Seric **			deleted from the current environment.
160355418Seric **
160455418Seric **	Returns:
160555418Seric **		none.
160655418Seric **
160755418Seric **	Globals:
160855418Seric **		environ -- a pointer to the current environment.
160955418Seric **
161055418Seric **	Side Effects:
161155418Seric **		Modifies environ.
161255418Seric */
161355418Seric 
161463962Seric #ifndef HASUNSETENV
161555418Seric 
161655418Seric void
161755418Seric unsetenv(name)
161855418Seric 	char *name;
161955418Seric {
162055418Seric 	extern char **environ;
162155418Seric 	register char **pp;
162255418Seric 	int len = strlen(name);
162355418Seric 
162455418Seric 	for (pp = environ; *pp != NULL; pp++)
162555418Seric 	{
162655418Seric 		if (strncmp(name, *pp, len) == 0 &&
162755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
162855418Seric 			break;
162955418Seric 	}
163055418Seric 
163155418Seric 	for (; *pp != NULL; pp++)
163255418Seric 		*pp = pp[1];
163355418Seric }
163455418Seric 
163563962Seric #endif
163656215Seric /*
163756215Seric **  GETDTABLESIZE -- return number of file descriptors
163856215Seric **
163956215Seric **	Only on non-BSD systems
164056215Seric **
164156215Seric **	Parameters:
164256215Seric **		none
164356215Seric **
164456215Seric **	Returns:
164556215Seric **		size of file descriptor table
164656215Seric **
164756215Seric **	Side Effects:
164856215Seric **		none
164956215Seric */
165056215Seric 
165163787Seric #ifdef SOLARIS
165263787Seric # include <sys/resource.h>
165363787Seric #endif
165456215Seric 
165556215Seric int
165663787Seric getdtsize()
165756215Seric {
165863787Seric #ifdef RLIMIT_NOFILE
165963787Seric 	struct rlimit rl;
166063787Seric 
166163787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
166263787Seric 		return rl.rlim_cur;
166363787Seric #endif
166463787Seric 
166564031Seric # ifdef HASGETDTABLESIZE
166664031Seric 	return getdtablesize();
166764031Seric # else
166864031Seric #  ifdef _SC_OPEN_MAX
166958689Seric 	return sysconf(_SC_OPEN_MAX);
167063787Seric #  else
167156215Seric 	return NOFILE;
167263787Seric #  endif
167358689Seric # endif
167456215Seric }
167557631Seric /*
167657631Seric **  UNAME -- get the UUCP name of this system.
167757631Seric */
167857631Seric 
167957943Seric #ifndef HASUNAME
168057631Seric 
168157631Seric int
168257631Seric uname(name)
168357631Seric 	struct utsname *name;
168457631Seric {
168557631Seric 	FILE *file;
168657631Seric 	char *n;
168757631Seric 
168857631Seric 	name->nodename[0] = '\0';
168957631Seric 
169057661Seric 	/* try /etc/whoami -- one line with the node name */
169157631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
169257631Seric 	{
169357661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
169457631Seric 		(void) fclose(file);
169557661Seric 		n = strchr(name->nodename, '\n');
169657631Seric 		if (n != NULL)
169757631Seric 			*n = '\0';
169857631Seric 		if (name->nodename[0] != '\0')
169957631Seric 			return (0);
170057631Seric 	}
170157631Seric 
170257661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
170357631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
170457631Seric 	{
170557631Seric 		char buf[MAXLINE];
170657631Seric 
170757631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
170857631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
170957631Seric 					NODE_LENGTH, name->nodename) > 0)
171057631Seric 				break;
171157631Seric 		(void) fclose(file);
171257631Seric 		if (name->nodename[0] != '\0')
171357631Seric 			return (0);
171457631Seric 	}
171557631Seric 
171657631Seric #ifdef TRUST_POPEN
171757631Seric 	/*
171857631Seric 	**  Popen is known to have security holes.
171957631Seric 	*/
172057631Seric 
172157661Seric 	/* try uuname -l to return local name */
172257631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
172357631Seric 	{
172457661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
172557631Seric 		(void) pclose(file);
172657661Seric 		n = strchr(name, '\n');
172757631Seric 		if (n != NULL)
172857631Seric 			*n = '\0';
172957661Seric 		if (name->nodename[0] != '\0')
173057631Seric 			return (0);
173157631Seric 	}
173257631Seric #endif
173357631Seric 
173457631Seric 	return (-1);
173557631Seric }
173657943Seric #endif /* HASUNAME */
173758068Seric /*
173858068Seric **  INITGROUPS -- initialize groups
173958068Seric **
174058068Seric **	Stub implementation for System V style systems
174158068Seric */
174258068Seric 
174358068Seric #ifndef HASINITGROUPS
174458068Seric 
174558068Seric initgroups(name, basegid)
174658068Seric 	char *name;
174758068Seric 	int basegid;
174858068Seric {
174958068Seric 	return 0;
175058068Seric }
175158068Seric 
175258068Seric #endif
175358082Seric /*
175459289Seric **  SETSID -- set session id (for non-POSIX systems)
175559289Seric */
175659289Seric 
175759289Seric #ifndef HASSETSID
175859289Seric 
175963753Seric pid_t
176063753Seric setsid __P ((void))
176159289Seric {
176264155Seric #ifdef TIOCNOTTY
176364155Seric 	int fd;
176464155Seric 
176566743Seric 	fd = open("/dev/tty", O_RDWR, 0);
176664155Seric 	if (fd >= 0)
176764155Seric 	{
176864155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
176964155Seric 		(void) close(fd);
177064155Seric 	}
177164155Seric #endif /* TIOCNOTTY */
177264705Seric # ifdef SYS5SETPGRP
177363753Seric 	return setpgrp();
177463753Seric # else
177564155Seric 	return setpgid(0, getpid());
177659289Seric # endif
177759289Seric }
177859289Seric 
177959289Seric #endif
178059289Seric /*
178166748Seric **  FSYNC -- dummy fsync
178266748Seric */
178366748Seric 
178466748Seric #ifdef NEEDFSYNC
178566748Seric 
178666748Seric fsync(fd)
178766748Seric 	int fd;
178866748Seric {
178966748Seric # ifdef O_SYNC
179066748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
179166748Seric # else
179266748Seric 	/* nothing we can do */
179366748Seric 	return 0;
179466748Seric # endif
179566748Seric }
179666748Seric 
179766748Seric #endif
179866748Seric /*
179964718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
180064718Seric **
180164718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
180268461Seric **	instead of a long.  This patches things.  Only needed on versions
180368461Seric **	prior to 5.4.3.
180464718Seric */
180564718Seric 
180668461Seric #ifdef DGUX_5_4_2
180764718Seric 
180864718Seric #undef inet_addr
180964718Seric 
181064718Seric long
181164718Seric dgux_inet_addr(host)
181264718Seric 	char *host;
181364718Seric {
181464718Seric 	struct in_addr haddr;
181564718Seric 
181664718Seric 	haddr = inet_addr(host);
181764718Seric 	return haddr.s_addr;
181864718Seric }
181964718Seric 
182064718Seric #endif
182164718Seric /*
182263969Seric **  GETOPT -- for old systems or systems with bogus implementations
182363969Seric */
182463969Seric 
182563969Seric #ifdef NEEDGETOPT
182663969Seric 
182763969Seric /*
182863969Seric  * Copyright (c) 1985 Regents of the University of California.
182963969Seric  * All rights reserved.  The Berkeley software License Agreement
183063969Seric  * specifies the terms and conditions for redistribution.
183163969Seric  */
183263969Seric 
183363969Seric 
183463969Seric /*
183563969Seric ** this version hacked to add `atend' flag to allow state machine
183663969Seric ** to reset if invoked by the program to scan args for a 2nd time
183763969Seric */
183863969Seric 
183963969Seric #if defined(LIBC_SCCS) && !defined(lint)
184063969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
184164072Seric #endif /* LIBC_SCCS and not lint */
184263969Seric 
184363969Seric #include <stdio.h>
184463969Seric 
184563969Seric /*
184663969Seric  * get option letter from argument vector
184763969Seric  */
184865352Seric #ifdef _CONVEX_SOURCE
184965352Seric extern int	optind, opterr;
185065352Seric #else
185165352Seric int	opterr = 1;		/* if error message should be printed */
185265352Seric int	optind = 1;		/* index into parent argv vector */
185365352Seric #endif
1854*68470Seric int	optopt = 0;		/* character checked for validity */
1855*68470Seric char	*optarg = NULL;		/* argument associated with option */
185663969Seric 
185763969Seric #define BADCH	(int)'?'
185863969Seric #define EMSG	""
185963969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
186063969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
186163969Seric 
186263969Seric getopt(nargc,nargv,ostr)
186365160Seric 	int		nargc;
186465160Seric 	char *const	*nargv;
186565160Seric 	const char	*ostr;
186663969Seric {
186763969Seric 	static char	*place = EMSG;	/* option letter processing */
186863969Seric 	static char	atend = 0;
186963969Seric 	register char	*oli;		/* option letter list index */
187063969Seric 
187163969Seric 	if (atend) {
187263969Seric 		atend = 0;
187363969Seric 		place = EMSG;
187463969Seric 	}
187563969Seric 	if(!*place) {			/* update scanning pointer */
187664136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
187763969Seric 			atend++;
187863969Seric 			return(EOF);
187963969Seric 		}
188063969Seric 		if (*place == '-') {	/* found "--" */
188163969Seric 			++optind;
188263969Seric 			atend++;
188363969Seric 			return(EOF);
188463969Seric 		}
188563969Seric 	}				/* option letter okay? */
188664125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
188764136Seric 		if (!*place) ++optind;
188863969Seric 		tell(": illegal option -- ");
188963969Seric 	}
189063969Seric 	if (*++oli != ':') {		/* don't need argument */
189163969Seric 		optarg = NULL;
189263969Seric 		if (!*place) ++optind;
189363969Seric 	}
189463969Seric 	else {				/* need an argument */
189563969Seric 		if (*place) optarg = place;	/* no white space */
189663969Seric 		else if (nargc <= ++optind) {	/* no arg */
189763969Seric 			place = EMSG;
189863969Seric 			tell(": option requires an argument -- ");
189963969Seric 		}
190063969Seric 	 	else optarg = nargv[optind];	/* white space */
190163969Seric 		place = EMSG;
190263969Seric 		++optind;
190363969Seric 	}
190463969Seric 	return(optopt);			/* dump back option letter */
190563969Seric }
190663969Seric 
190763969Seric #endif
190863969Seric /*
190963969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
191063969Seric */
191163969Seric 
191263969Seric #ifdef NEEDVPRINTF
191363969Seric 
191463969Seric #define MAXARG	16
191563969Seric 
191663969Seric vfprintf(fp, fmt, ap)
191763969Seric 	FILE *	fp;
191863969Seric 	char *	fmt;
191963969Seric 	char **	ap;
192063969Seric {
192163969Seric 	char *	bp[MAXARG];
192263969Seric 	int	i = 0;
192363969Seric 
192463969Seric 	while (*ap && i < MAXARG)
192563969Seric 		bp[i++] = *ap++;
192663969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
192763969Seric 			 bp[4], bp[5], bp[6], bp[7],
192863969Seric 			 bp[8], bp[9], bp[10], bp[11],
192963969Seric 			 bp[12], bp[13], bp[14], bp[15]);
193063969Seric }
193163969Seric 
193263969Seric vsprintf(s, fmt, ap)
193363969Seric 	char *	s;
193463969Seric 	char *	fmt;
193563969Seric 	char **	ap;
193663969Seric {
193763969Seric 	char *	bp[MAXARG];
193863969Seric 	int	i = 0;
193963969Seric 
194063969Seric 	while (*ap && i < MAXARG)
194163969Seric 		bp[i++] = *ap++;
194263969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
194363969Seric 			bp[4], bp[5], bp[6], bp[7],
194463969Seric 			bp[8], bp[9], bp[10], bp[11],
194563969Seric 			bp[12], bp[13], bp[14], bp[15]);
194663969Seric }
194763969Seric 
194863969Seric #endif
194963969Seric /*
195065220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
195165208Seric **
195265211Seric **	Parameters:
195365211Seric **		shell -- the user's shell from /etc/passwd
195465211Seric **
195565211Seric **	Returns:
195665211Seric **		TRUE -- if it is ok to use this for unrestricted access.
195765211Seric **		FALSE -- if the shell is restricted.
195865208Seric */
195965208Seric 
196065587Seric #if !HASGETUSERSHELL
196165587Seric 
196265587Seric # ifndef _PATH_SHELLS
196365587Seric #  define _PATH_SHELLS	"/etc/shells"
196465587Seric # endif
196565587Seric 
196665587Seric char	*DefaultUserShells[] =
196765587Seric {
196868461Seric 	"/bin/sh",		/* standard shell */
196965587Seric 	"/usr/bin/sh",
197068461Seric 	"/bin/csh",		/* C shell */
197165587Seric 	"/usr/bin/csh",
197265587Seric #ifdef __hpux
197368461Seric 	"/bin/rsh",		/* restricted Bourne shell */
197468461Seric 	"/bin/ksh",		/* Korn shell */
197568461Seric 	"/bin/rksh",		/* restricted Korn shell */
197665587Seric 	"/bin/pam",
197768461Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
197865587Seric 	"/bin/posix/sh",
197965211Seric #endif
198068461Seric #ifdef _AIX3
198168461Seric 	"/bin/ksh",		/* Korn shell */
198268461Seric 	"/usr/bin/ksh",
198368461Seric 	"/bin/tsh",		/* trusted shell */
198468461Seric 	"/usr/bin/tsh",
198568461Seric 	"/bin/bsh",		/* Bourne shell */
198668461Seric 	"/usr/bin/bsh",
198768461Seric #endif
198865587Seric 	NULL
198965587Seric };
199065587Seric 
199165208Seric #endif
199265208Seric 
199365747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
199465747Seric 
199565211Seric bool
199665211Seric usershellok(shell)
199765211Seric 	char *shell;
199865208Seric {
199965211Seric #if HASGETUSERSHELL
200065211Seric 	register char *p;
200165211Seric 	extern char *getusershell();
200265208Seric 
200368457Seric 	if (shell == NULL || shell[0] == '\0')
200468457Seric 		return TRUE;
200568457Seric 
200665211Seric 	setusershell();
200765211Seric 	while ((p = getusershell()) != NULL)
200865747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
200965211Seric 			break;
201065211Seric 	endusershell();
201165211Seric 	return p != NULL;
201265211Seric #else
201365211Seric 	register FILE *shellf;
201465211Seric 	char buf[MAXLINE];
201565208Seric 
201668457Seric 	if (shell == NULL || shell[0] == '\0')
201768457Seric 		return TRUE;
201868457Seric 
201965211Seric 	shellf = fopen(_PATH_SHELLS, "r");
202065211Seric 	if (shellf == NULL)
202165211Seric 	{
202265211Seric 		/* no /etc/shells; see if it is one of the std shells */
202365587Seric 		char **d;
202465587Seric 
202565587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
202665587Seric 		{
202765587Seric 			if (strcmp(shell, *d) == 0)
202865587Seric 				return TRUE;
202965587Seric 		}
203065587Seric 		return FALSE;
203165211Seric 	}
203265208Seric 
203365211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
203465211Seric 	{
203565211Seric 		register char *p, *q;
203665208Seric 
203765211Seric 		p = buf;
203865211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
203965211Seric 			p++;
204065211Seric 		if (*p == '#' || *p == '\0')
204165208Seric 			continue;
204265211Seric 		q = p;
204365211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
204465211Seric 			p++;
204565211Seric 		*p = '\0';
204665747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
204765211Seric 		{
204865211Seric 			fclose(shellf);
204965211Seric 			return TRUE;
205065211Seric 		}
205165208Seric 	}
205265211Seric 	fclose(shellf);
205365211Seric 	return FALSE;
205465211Seric #endif
205565208Seric }
205665208Seric /*
205764311Seric **  FREESPACE -- see how much free space is on the queue filesystem
205858082Seric **
205958082Seric **	Only implemented if you have statfs.
206058082Seric **
206158082Seric **	Parameters:
206264311Seric **		dir -- the directory in question.
206364311Seric **		bsize -- a variable into which the filesystem
206464311Seric **			block size is stored.
206558082Seric **
206658082Seric **	Returns:
206764311Seric **		The number of bytes free on the queue filesystem.
206864311Seric **		-1 if the statfs call fails.
206964311Seric **
207064311Seric **	Side effects:
207164311Seric **		Puts the filesystem block size into bsize.
207258082Seric */
207358082Seric 
207465749Seric /* statfs types */
207565749Seric #define SFS_NONE	0	/* no statfs implementation */
207665749Seric #define SFS_USTAT	1	/* use ustat */
207765749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
207865749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
207965749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
208065749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
208166752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
208265749Seric 
208365749Seric #ifndef SFS_TYPE
208465749Seric # define SFS_TYPE	SFS_NONE
208558157Seric #endif
208658157Seric 
208765749Seric #if SFS_TYPE == SFS_USTAT
208858157Seric # include <ustat.h>
208958157Seric #endif
209065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
209165749Seric # include <sys/statfs.h>
209258082Seric #endif
209365749Seric #if SFS_TYPE == SFS_VFS
209465749Seric # include <sys/vfs.h>
209565749Seric #endif
209665749Seric #if SFS_TYPE == SFS_MOUNT
209765749Seric # include <sys/mount.h>
209865749Seric #endif
209966752Seric #if SFS_TYPE == SFS_STATVFS
210066752Seric # include <sys/statvfs.h>
210166752Seric #endif
210258082Seric 
210364311Seric long
210464311Seric freespace(dir, bsize)
210564311Seric 	char *dir;
210664311Seric 	long *bsize;
210758082Seric {
210865749Seric #if SFS_TYPE != SFS_NONE
210965749Seric # if SFS_TYPE == SFS_USTAT
211058153Seric 	struct ustat fs;
211158153Seric 	struct stat statbuf;
211258366Seric #  define FSBLOCKSIZE	DEV_BSIZE
211368461Seric #  define FSF_BAVAIL	f_tfree
211458157Seric # else
211558157Seric #  if defined(ultrix)
211658157Seric 	struct fs_data fs;
211768461Seric #   define FSF_BAVAIL	fd_bfreen
211868461Seric #   define FSBLOCKSIZE	1024L
211958153Seric #  else
212066752Seric #   if SFS_TYPE == SFS_STATVFS
212166752Seric 	struct statvfs fs;
212268461Seric #    define FSBLOCKSIZE	fs.f_frsize
212366752Seric #   else
212458082Seric 	struct statfs fs;
212566752Seric #    define FSBLOCKSIZE	fs.f_bsize
212663987Seric #   endif
212758153Seric #  endif
212858133Seric # endif
212968461Seric # ifndef FSF_BAVAIL
213068461Seric #  define FSF_BAVAIL f_bavail
213168461Seric # endif
213258082Seric 
213365749Seric # if SFS_TYPE == SFS_USTAT
213464311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
213558157Seric # else
213665749Seric #  if SFS_TYPE == SFS_4ARGS
213764311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
213858157Seric #  else
213968461Seric #   if SFS_TYPE == SFS_STATVFS
214068461Seric 	if (statvfs(dir, &fs) == 0)
214168461Seric #   else
214268461Seric #    if defined(ultrix)
214368457Seric 	if (statfs(dir, &fs) > 0)
214468461Seric #    else
214564311Seric 	if (statfs(dir, &fs) == 0)
214668461Seric #    endif
214758153Seric #   endif
214858133Seric #  endif
214958133Seric # endif
215058133Seric 	{
215164311Seric 		if (bsize != NULL)
215264311Seric 			*bsize = FSBLOCKSIZE;
215368461Seric 		return (fs.FSF_BAVAIL);
215464311Seric 	}
215564337Seric #endif
215664311Seric 	return (-1);
215764311Seric }
215864311Seric /*
215964311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
216064311Seric **
216164311Seric **	Only implemented if you have statfs.
216264311Seric **
216364311Seric **	Parameters:
216464311Seric **		msize -- the size to check against.  If zero, we don't yet
216564311Seric **		know how big the message will be, so just check for
216664311Seric **		a "reasonable" amount.
216764311Seric **
216864311Seric **	Returns:
216964311Seric **		TRUE if there is enough space.
217064311Seric **		FALSE otherwise.
217164311Seric */
217264311Seric 
217364311Seric bool
217464311Seric enoughspace(msize)
217564311Seric 	long msize;
217664311Seric {
217764311Seric 	long bfree, bsize;
217864311Seric 
217964311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
218064311Seric 	{
218158133Seric 		if (tTd(4, 80))
218264311Seric 			printf("enoughspace: no threshold\n");
218364311Seric 		return TRUE;
218464311Seric 	}
218564311Seric 
218664311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
218764311Seric 	{
218864311Seric 		if (tTd(4, 80))
218958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
219064311Seric 				bfree, msize);
219158333Seric 
219258333Seric 		/* convert msize to block count */
219364311Seric 		msize = msize / bsize + 1;
219458333Seric 		if (MinBlocksFree >= 0)
219558333Seric 			msize += MinBlocksFree;
219658333Seric 
219764311Seric 		if (bfree < msize)
219858090Seric 		{
219958090Seric #ifdef LOG
220058090Seric 			if (LogLevel > 0)
220164254Seric 				syslog(LOG_ALERT,
220264254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
220368461Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
220468461Seric 					bfree,
220568461Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
220668461Seric 					msize, QueueDir);
220758090Seric #endif
220858082Seric 			return FALSE;
220958090Seric 		}
221058082Seric 	}
221158082Seric 	else if (tTd(4, 80))
221258333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
221358333Seric 			MinBlocksFree, msize, errstring(errno));
221458082Seric 	return TRUE;
221558082Seric }
221658542Seric /*
221758542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
221858542Seric **
221958542Seric **	This looks at an errno value and tells if this is likely to
222058542Seric **	go away if retried later.
222158542Seric **
222258542Seric **	Parameters:
222358542Seric **		err -- the errno code to classify.
222458542Seric **
222558542Seric **	Returns:
222658542Seric **		TRUE if this is probably transient.
222758542Seric **		FALSE otherwise.
222858542Seric */
222958542Seric 
223058542Seric bool
223158542Seric transienterror(err)
223258542Seric 	int err;
223358542Seric {
223458542Seric 	switch (err)
223558542Seric 	{
223658542Seric 	  case EIO:			/* I/O error */
223758542Seric 	  case ENXIO:			/* Device not configured */
223858542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
223958542Seric 	  case ENOMEM:			/* Cannot allocate memory */
224058542Seric 	  case ENODEV:			/* Operation not supported by device */
224158542Seric 	  case ENFILE:			/* Too many open files in system */
224258542Seric 	  case EMFILE:			/* Too many open files */
224358542Seric 	  case ENOSPC:			/* No space left on device */
224458542Seric #ifdef ETIMEDOUT
224558542Seric 	  case ETIMEDOUT:		/* Connection timed out */
224658542Seric #endif
224758542Seric #ifdef ESTALE
224858542Seric 	  case ESTALE:			/* Stale NFS file handle */
224958542Seric #endif
225058542Seric #ifdef ENETDOWN
225158542Seric 	  case ENETDOWN:		/* Network is down */
225258542Seric #endif
225358542Seric #ifdef ENETUNREACH
225458542Seric 	  case ENETUNREACH:		/* Network is unreachable */
225558542Seric #endif
225658542Seric #ifdef ENETRESET
225758542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
225858542Seric #endif
225958542Seric #ifdef ECONNABORTED
226058542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
226158542Seric #endif
226258542Seric #ifdef ECONNRESET
226358542Seric 	  case ECONNRESET:		/* Connection reset by peer */
226458542Seric #endif
226558542Seric #ifdef ENOBUFS
226658542Seric 	  case ENOBUFS:			/* No buffer space available */
226758542Seric #endif
226858542Seric #ifdef ESHUTDOWN
226958542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
227058542Seric #endif
227158542Seric #ifdef ECONNREFUSED
227258542Seric 	  case ECONNREFUSED:		/* Connection refused */
227358542Seric #endif
227458542Seric #ifdef EHOSTDOWN
227558542Seric 	  case EHOSTDOWN:		/* Host is down */
227658542Seric #endif
227758542Seric #ifdef EHOSTUNREACH
227858542Seric 	  case EHOSTUNREACH:		/* No route to host */
227958542Seric #endif
228058542Seric #ifdef EDQUOT
228158542Seric 	  case EDQUOT:			/* Disc quota exceeded */
228258542Seric #endif
228358542Seric #ifdef EPROCLIM
228458542Seric 	  case EPROCLIM:		/* Too many processes */
228558542Seric #endif
228658542Seric #ifdef EUSERS
228758542Seric 	  case EUSERS:			/* Too many users */
228858542Seric #endif
228958542Seric #ifdef EDEADLK
229058542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
229158542Seric #endif
229258542Seric #ifdef EISCONN
229358542Seric 	  case EISCONN:			/* Socket already connected */
229458542Seric #endif
229558542Seric #ifdef EINPROGRESS
229658542Seric 	  case EINPROGRESS:		/* Operation now in progress */
229758542Seric #endif
229858542Seric #ifdef EALREADY
229958542Seric 	  case EALREADY:		/* Operation already in progress */
230058542Seric #endif
230158542Seric #ifdef EADDRINUSE
230258542Seric 	  case EADDRINUSE:		/* Address already in use */
230358542Seric #endif
230458542Seric #ifdef EADDRNOTAVAIL
230558542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
230658542Seric #endif
230766018Seric #ifdef ETXTBSY
230866017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
230966017Seric #endif
231063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
231158542Seric 	  case ENOSR:			/* Out of streams resources */
231258542Seric #endif
231358542Seric 		return TRUE;
231458542Seric 	}
231558542Seric 
231658542Seric 	/* nope, must be permanent */
231758542Seric 	return FALSE;
231858542Seric }
231958689Seric /*
232064035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
232158689Seric **
232258689Seric **	Parameters:
232358689Seric **		fd -- the file descriptor of the file.
232458689Seric **		filename -- the file name (for error messages).
232564335Seric **		ext -- the filename extension.
232658689Seric **		type -- type of the lock.  Bits can be:
232758689Seric **			LOCK_EX -- exclusive lock.
232858689Seric **			LOCK_NB -- non-blocking.
232958689Seric **
233058689Seric **	Returns:
233158689Seric **		TRUE if the lock was acquired.
233258689Seric **		FALSE otherwise.
233358689Seric */
233458689Seric 
233558689Seric bool
233664335Seric lockfile(fd, filename, ext, type)
233758689Seric 	int fd;
233858689Seric 	char *filename;
233964335Seric 	char *ext;
234058689Seric 	int type;
234158689Seric {
234265830Seric # if !HASFLOCK
234358689Seric 	int action;
234458689Seric 	struct flock lfd;
234564335Seric 
234664335Seric 	if (ext == NULL)
234764335Seric 		ext = "";
234864035Seric 
234963983Seric 	bzero(&lfd, sizeof lfd);
235059447Seric 	if (bitset(LOCK_UN, type))
235159447Seric 		lfd.l_type = F_UNLCK;
235259447Seric 	else if (bitset(LOCK_EX, type))
235358689Seric 		lfd.l_type = F_WRLCK;
235458689Seric 	else
235558689Seric 		lfd.l_type = F_RDLCK;
235658689Seric 
235758689Seric 	if (bitset(LOCK_NB, type))
235858689Seric 		action = F_SETLK;
235958689Seric 	else
236058689Seric 		action = F_SETLKW;
236158689Seric 
236264264Seric 	if (tTd(55, 60))
236364335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
236464335Seric 			filename, ext, action, lfd.l_type);
236564264Seric 
236658689Seric 	if (fcntl(fd, action, &lfd) >= 0)
236764264Seric 	{
236864264Seric 		if (tTd(55, 60))
236964264Seric 			printf("SUCCESS\n");
237058689Seric 		return TRUE;
237164264Seric 	}
237258689Seric 
237364264Seric 	if (tTd(55, 60))
237464264Seric 		printf("(%s) ", errstring(errno));
237564264Seric 
237664136Seric 	/*
237764136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
237864136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
237964136Seric 	**  as type "tmp" (that is, served from swap space), the
238064136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
238164136Seric 	**  Since this is fairly common during testing, we will assume
238264136Seric 	**  that this indicates that the lock is successfully grabbed.
238364136Seric 	*/
238464136Seric 
238564136Seric 	if (errno == EINVAL)
238664264Seric 	{
238764264Seric 		if (tTd(55, 60))
238864264Seric 			printf("SUCCESS\n");
238964136Seric 		return TRUE;
239064264Seric 	}
239164136Seric 
239258689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
239364378Seric 	{
239464378Seric 		int omode = -1;
239564378Seric #  ifdef F_GETFL
239664378Seric 		int oerrno = errno;
239764378Seric 
239864378Seric 		(void) fcntl(fd, F_GETFL, &omode);
239964378Seric 		errno = oerrno;
240064378Seric #  endif
240164378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
240264378Seric 			filename, ext, fd, type, omode, geteuid());
240364378Seric 	}
240458689Seric # else
240564335Seric 	if (ext == NULL)
240664335Seric 		ext = "";
240764335Seric 
240864264Seric 	if (tTd(55, 60))
240964335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
241064264Seric 
241158689Seric 	if (flock(fd, type) >= 0)
241264264Seric 	{
241364264Seric 		if (tTd(55, 60))
241464264Seric 			printf("SUCCESS\n");
241558689Seric 		return TRUE;
241664264Seric 	}
241758689Seric 
241864264Seric 	if (tTd(55, 60))
241964264Seric 		printf("(%s) ", errstring(errno));
242064264Seric 
242158689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
242264378Seric 	{
242364378Seric 		int omode = -1;
242464378Seric #  ifdef F_GETFL
242564378Seric 		int oerrno = errno;
242664378Seric 
242764378Seric 		(void) fcntl(fd, F_GETFL, &omode);
242864378Seric 		errno = oerrno;
242964378Seric #  endif
243064378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
243164378Seric 			filename, ext, fd, type, omode, geteuid());
243264386Seric 	}
243358689Seric # endif
243464264Seric 	if (tTd(55, 60))
243564264Seric 		printf("FAIL\n");
243658689Seric 	return FALSE;
243758689Seric }
243864556Seric /*
243965948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
244065948Seric **
244165948Seric **	Parameters:
244265948Seric **		fd -- the file descriptor to check.
244365948Seric **
244465948Seric **	Returns:
244565948Seric **		TRUE -- if only root can chown the file to an arbitrary
244665948Seric **			user.
244765948Seric **		FALSE -- if an arbitrary user can give away a file.
244865948Seric */
244965948Seric 
245065948Seric bool
245165948Seric chownsafe(fd)
245265948Seric 	int fd;
245365948Seric {
245465950Seric #ifdef __hpux
245565948Seric 	char *s;
245665948Seric 	int tfd;
245765948Seric 	uid_t o_uid, o_euid;
245865948Seric 	gid_t o_gid, o_egid;
245965948Seric 	bool rval;
246065948Seric 	struct stat stbuf;
246165948Seric 
246265948Seric 	o_uid = getuid();
246365948Seric 	o_euid = geteuid();
246465948Seric 	o_gid = getgid();
246565948Seric 	o_egid = getegid();
246665959Seric 	fstat(fd, &stbuf);
246765948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
246865948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
246965948Seric 	s = tmpnam(NULL);
247065948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
247165974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
247265948Seric 	close(tfd);
247365948Seric 	unlink(s);
247465948Seric 	setreuid(o_uid, o_euid);
247565948Seric 	setresgid(o_gid, o_egid, -1);
247665948Seric 	return rval;
247765948Seric #else
247866088Seric # ifdef _POSIX_CHOWN_RESTRICTED
247966088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
248066088Seric 	return FALSE;
248166088Seric #  else
248266088Seric 	return TRUE;
248366088Seric #  endif
248466088Seric # else
248566088Seric #  ifdef _PC_CHOWN_RESTRICTED
248668461Seric 	int rval;
248768461Seric 
248868461Seric 	/*
248968461Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
249068461Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
249168461Seric 	**  the call.  This heuristic checks for that.
249268461Seric 	*/
249368461Seric 
249468461Seric 	errno = 0;
249568461Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
249668461Seric 	if (errno == 0)
249768461Seric 		return rval > 0;
249868461Seric #  endif
249968461Seric #  ifdef BSD
250068461Seric 	return TRUE;
250168457Seric #  else
250265948Seric 	return FALSE;
250365948Seric #  endif
250465948Seric # endif
250565948Seric #endif
250665948Seric }
250765948Seric /*
250868461Seric **  RESETLIMITS -- reset system controlled resource limits
250968461Seric **
251068461Seric **	This is to avoid denial-of-service attacks
251168461Seric **
251268461Seric **	Parameters:
251368461Seric **		none
251468461Seric **
251568461Seric **	Returns:
251668461Seric **		none
251768461Seric */
251868461Seric 
251968461Seric #if HASSETRLIMIT
252068461Seric # include <sys/resource.h>
252168461Seric #endif
252268461Seric 
252368461Seric void
252468461Seric resetlimits()
252568461Seric {
252668461Seric #if HASSETRLIMIT
252768461Seric 	struct rlimit lim;
252868461Seric 
252968461Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
253068461Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
253168461Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
253268461Seric #else
253368461Seric # if HASULIMIT
253468461Seric 	(void) ulimit(2, 0x3fffff);
253568461Seric # endif
253668461Seric #endif
253768461Seric }
253868461Seric /*
253964556Seric **  GETCFNAME -- return the name of the .cf file.
254064556Seric **
254164556Seric **	Some systems (e.g., NeXT) determine this dynamically.
254264556Seric */
254364556Seric 
254464556Seric char *
254564556Seric getcfname()
254664556Seric {
254764556Seric 	if (ConfFile != NULL)
254864556Seric 		return ConfFile;
254965214Seric #ifdef NETINFO
255065214Seric 	{
255165214Seric 		extern char *ni_propval();
255265214Seric 		char *cflocation;
255365214Seric 
255468461Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
255568461Seric 					"sendmail.cf", '\0');
255665214Seric 		if (cflocation != NULL)
255765214Seric 			return cflocation;
255865214Seric 	}
255965214Seric #endif
256064556Seric 	return _PATH_SENDMAILCF;
256164556Seric }
256264718Seric /*
256364718Seric **  SETVENDOR -- process vendor code from V configuration line
256464718Seric **
256564718Seric **	Parameters:
256664718Seric **		vendor -- string representation of vendor.
256764718Seric **
256864718Seric **	Returns:
256964718Seric **		TRUE -- if ok.
257064718Seric **		FALSE -- if vendor code could not be processed.
257164926Seric **
257264926Seric **	Side Effects:
257364926Seric **		It is reasonable to set mode flags here to tweak
257464926Seric **		processing in other parts of the code if necessary.
257564926Seric **		For example, if you are a vendor that uses $%y to
257664926Seric **		indicate YP lookups, you could enable that here.
257764718Seric */
257864718Seric 
257964718Seric bool
258064718Seric setvendor(vendor)
258164718Seric 	char *vendor;
258264718Seric {
258364926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
258468461Seric 	{
258568461Seric 		VendorCode = VENDOR_BERKELEY;
258664926Seric 		return TRUE;
258768461Seric 	}
258864926Seric 
258964926Seric 	/* add vendor extensions here */
259064926Seric 
259168461Seric #ifdef SUN_EXTENSIONS
259268461Seric 	if (strcasecmp(vendor, "Sun") == 0)
259368461Seric 	{
259468461Seric 		VendorCode = VENDOR_SUN;
259568461Seric 		return TRUE;
259668461Seric 	}
259768461Seric #endif
259868461Seric 
259964926Seric 	return FALSE;
260064718Seric }
260164816Seric /*
260264816Seric **  STRTOL -- convert string to long integer
260364816Seric **
260464816Seric **	For systems that don't have it in the C library.
260566161Seric **
260666161Seric **	This is taken verbatim from the 4.4-Lite C library.
260764816Seric */
260864816Seric 
260964816Seric #ifdef NEEDSTRTOL
261064816Seric 
261166161Seric #if defined(LIBC_SCCS) && !defined(lint)
261266161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
261366161Seric #endif /* LIBC_SCCS and not lint */
261466161Seric 
261566161Seric #include <limits.h>
261666161Seric 
261766161Seric /*
261866161Seric  * Convert a string to a long integer.
261966161Seric  *
262066161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
262166161Seric  * alphabets and digits are each contiguous.
262266161Seric  */
262366161Seric 
262464816Seric long
262566161Seric strtol(nptr, endptr, base)
262666161Seric 	const char *nptr;
262766161Seric 	char **endptr;
262866161Seric 	register int base;
262964816Seric {
263066161Seric 	register const char *s = nptr;
263166161Seric 	register unsigned long acc;
263266161Seric 	register int c;
263366161Seric 	register unsigned long cutoff;
263466161Seric 	register int neg = 0, any, cutlim;
263564816Seric 
263666161Seric 	/*
263766161Seric 	 * Skip white space and pick up leading +/- sign if any.
263866161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
263966161Seric 	 * assume decimal; if base is already 16, allow 0x.
264066161Seric 	 */
264166161Seric 	do {
264266161Seric 		c = *s++;
264366161Seric 	} while (isspace(c));
264466161Seric 	if (c == '-') {
264566161Seric 		neg = 1;
264666161Seric 		c = *s++;
264766161Seric 	} else if (c == '+')
264866161Seric 		c = *s++;
264966161Seric 	if ((base == 0 || base == 16) &&
265066161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
265166161Seric 		c = s[1];
265266161Seric 		s += 2;
265366161Seric 		base = 16;
265466161Seric 	}
265566161Seric 	if (base == 0)
265666161Seric 		base = c == '0' ? 8 : 10;
265764816Seric 
265866161Seric 	/*
265966161Seric 	 * Compute the cutoff value between legal numbers and illegal
266066161Seric 	 * numbers.  That is the largest legal value, divided by the
266166161Seric 	 * base.  An input number that is greater than this value, if
266266161Seric 	 * followed by a legal input character, is too big.  One that
266366161Seric 	 * is equal to this value may be valid or not; the limit
266466161Seric 	 * between valid and invalid numbers is then based on the last
266566161Seric 	 * digit.  For instance, if the range for longs is
266666161Seric 	 * [-2147483648..2147483647] and the input base is 10,
266766161Seric 	 * cutoff will be set to 214748364 and cutlim to either
266866161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
266966161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
267066161Seric 	 * the number is too big, and we will return a range error.
267166161Seric 	 *
267266161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
267366161Seric 	 * overflow.
267466161Seric 	 */
267566161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
267666161Seric 	cutlim = cutoff % (unsigned long)base;
267766161Seric 	cutoff /= (unsigned long)base;
267866161Seric 	for (acc = 0, any = 0;; c = *s++) {
267966161Seric 		if (isdigit(c))
268066161Seric 			c -= '0';
268166161Seric 		else if (isalpha(c))
268266161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
268366161Seric 		else
268466161Seric 			break;
268566161Seric 		if (c >= base)
268666161Seric 			break;
268766161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
268866161Seric 			any = -1;
268966161Seric 		else {
269066161Seric 			any = 1;
269166161Seric 			acc *= base;
269266161Seric 			acc += c;
269364816Seric 		}
269464816Seric 	}
269566161Seric 	if (any < 0) {
269666161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
269766161Seric 		errno = ERANGE;
269866161Seric 	} else if (neg)
269966161Seric 		acc = -acc;
270066161Seric 	if (endptr != 0)
270166161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
270266161Seric 	return (acc);
270364816Seric }
270464816Seric 
270564816Seric #endif
270664841Seric /*
270764841Seric **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
270864841Seric **
270968461Seric **	Solaris versions at least through 2.3 don't properly deliver a
271064841Seric **	canonical h_name field.  This tries to work around it.
271164841Seric */
271264841Seric 
271364841Seric #ifdef SOLARIS
271464841Seric 
271568461Seric extern int	h_errno;
271668461Seric 
271764841Seric struct hostent *
271864841Seric solaris_gethostbyname(name)
271965094Seric 	const char *name;
272064841Seric {
272164841Seric # ifdef SOLARIS_2_3
272264841Seric 	static struct hostent hp;
272364841Seric 	static char buf[1000];
272464841Seric 	extern struct hostent *_switch_gethostbyname_r();
272564841Seric 
272664841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
272764841Seric # else
272865009Seric 	extern struct hostent *__switch_gethostbyname();
272965009Seric 
273064841Seric 	return __switch_gethostbyname(name);
273164841Seric # endif
273264841Seric }
273364841Seric 
273464841Seric struct hostent *
273564841Seric solaris_gethostbyaddr(addr, len, type)
273665094Seric 	const char *addr;
273764841Seric 	int len;
273864841Seric 	int type;
273964841Seric {
274064841Seric # ifdef SOLARIS_2_3
274164841Seric 	static struct hostent hp;
274264841Seric 	static char buf[1000];
274364841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
274464841Seric 
274564841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
274664841Seric # else
274765009Seric 	extern struct hostent *__switch_gethostbyaddr();
274865009Seric 
274964841Seric 	return __switch_gethostbyaddr(addr, len, type);
275064841Seric # endif
275164841Seric }
275264841Seric 
275364841Seric #endif
275465214Seric /*
275565214Seric **  NI_PROPVAL -- netinfo property value lookup routine
275665214Seric **
275765214Seric **	Parameters:
275868461Seric **		keydir -- the Netinfo directory name in which to search
275968461Seric **			for the key.
276068461Seric **		keyprop -- the name of the property in which to find the
276168461Seric **			property we are interested.  Defaults to "name".
276268461Seric **		keyval -- the value for which we are really searching.
276368461Seric **		valprop -- the property name for the value in which we
276468461Seric **			are interested.
276568461Seric **		sepchar -- if non-nil, this can be multiple-valued, and
276668461Seric **			we should return a string separated by this
276768461Seric **			character.
276865214Seric **
276965214Seric **	Returns:
277065214Seric **		NULL -- if:
277165214Seric **			1. the directory is not found
277265214Seric **			2. the property name is not found
277365214Seric **			3. the property contains multiple values
277465214Seric **			4. some error occured
277565214Seric **		else -- the location of the config file.
277665214Seric **
277768461Seric **	Example:
277868461Seric **		To search for an alias value, use:
277968461Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
278068461Seric **
278165214Seric **	Notes:
278265214Seric **      	Caller should free the return value of ni_proval
278365214Seric */
278465214Seric 
278565214Seric #ifdef NETINFO
278665214Seric 
278765214Seric # include <netinfo/ni.h>
278865214Seric 
278965214Seric # define LOCAL_NETINFO_DOMAIN    "."
279065214Seric # define PARENT_NETINFO_DOMAIN   ".."
279165214Seric # define MAX_NI_LEVELS           256
279265214Seric 
279365214Seric char *
279468461Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
279568461Seric 	char *keydir;
279668461Seric 	char *keyprop;
279768461Seric 	char *keyval;
279868461Seric 	char *valprop;
279968461Seric 	char sepchar;
280065214Seric {
280165820Seric 	char *propval = NULL;
280265214Seric 	int i;
280368461Seric 	int j, alen;
280465214Seric 	void *ni = NULL;
280565214Seric 	void *lastni = NULL;
280665214Seric 	ni_status nis;
280765214Seric 	ni_id nid;
280865214Seric 	ni_namelist ninl;
280968461Seric 	register char *p;
281068461Seric 	char keybuf[1024];
281165214Seric 
281265214Seric 	/*
281368461Seric 	**  Create the full key from the two parts.
281468461Seric 	**
281568461Seric 	**	Note that directory can end with, e.g., "name=" to specify
281668461Seric 	**	an alternate search property.
281768461Seric 	*/
281868461Seric 
281968461Seric 	i = strlen(keydir) + strlen(keyval) + 2;
282068461Seric 	if (keyprop != NULL)
282168461Seric 		i += strlen(keyprop) + 1;
282268461Seric 	if (i > sizeof keybuf)
282368461Seric 		return NULL;
282468461Seric 	strcpy(keybuf, keydir);
282568461Seric 	strcat(keybuf, "/");
282668461Seric 	if (keyprop != NULL)
282768461Seric 	{
282868461Seric 		strcat(keybuf, keyprop);
282968461Seric 		strcat(keybuf, "=");
283068461Seric 	}
283168461Seric 	strcat(keybuf, keyval);
283268461Seric 
283368461Seric 	/*
283465214Seric 	**  If the passed directory and property name are found
283565214Seric 	**  in one of netinfo domains we need to search (starting
283665214Seric 	**  from the local domain moving all the way back to the
283765214Seric 	**  root domain) set propval to the property's value
283865214Seric 	**  and return it.
283965214Seric 	*/
284065214Seric 
284165214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
284265214Seric 	{
284365214Seric 		if (i == 0)
284465214Seric 		{
284565214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
284665214Seric 		}
284765214Seric 		else
284865214Seric 		{
284965214Seric 			if (lastni != NULL)
285065214Seric 				ni_free(lastni);
285165214Seric 			lastni = ni;
285265214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
285365214Seric 		}
285465214Seric 
285565214Seric 		/*
285665214Seric 		**  Don't bother if we didn't get a handle on a
285765214Seric 		**  proper domain.  This is not necessarily an error.
285865214Seric 		**  We would get a positive ni_status if, for instance
285965214Seric 		**  we never found the directory or property and tried
286065214Seric 		**  to open the parent of the root domain!
286165214Seric 		*/
286265214Seric 
286365214Seric 		if (nis != 0)
286465214Seric 			break;
286565214Seric 
286665214Seric 		/*
286765214Seric 		**  Find the path to the server information.
286865214Seric 		*/
286965214Seric 
287068461Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
287165214Seric 			continue;
287265214Seric 
287365214Seric 		/*
287468461Seric 		**  Find associated value information.
287565214Seric 		*/
287665214Seric 
287768461Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
287865214Seric 			continue;
287965214Seric 
288065214Seric 		/*
288168461Seric 		**  See if we have an acceptable number of values.
288265214Seric 		*/
288365214Seric 
288468461Seric 		if (ninl.ni_namelist_len <= 0)
288568461Seric 			continue;
288668461Seric 
288768461Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
288865214Seric 		{
288968461Seric 			ni_namelist_free(&ninl);
289068461Seric 			continue;
289165214Seric 		}
289268461Seric 
289368461Seric 		/*
289468461Seric 		**  Calculate number of bytes needed and build result
289568461Seric 		*/
289668461Seric 
289768461Seric 		alen = 1;
289868461Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
289968461Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
290068461Seric 		propval = p = xalloc(alen);
290168461Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
290268461Seric 		{
290368461Seric 			strcpy(p, ninl.ni_namelist_val[j]);
290468461Seric 			p += strlen(p);
290568461Seric 			*p++ = sepchar;
290668461Seric 		}
290768461Seric 		*--p = '\0';
290868461Seric 
290968461Seric 		ni_namelist_free(&ninl);
291065214Seric 	}
291165214Seric 
291265214Seric 	/*
291365214Seric 	**  Clean up.
291465214Seric 	*/
291565214Seric 
291665214Seric 	if (ni != NULL)
291765214Seric 		ni_free(ni);
291865214Seric 	if (lastni != NULL && ni != lastni)
291965214Seric 		ni_free(lastni);
292065214Seric 
292165214Seric 	return propval;
292265214Seric }
292365214Seric 
292465214Seric #endif /* NETINFO */
292565504Seric /*
292665504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
292765504Seric **
292865504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
292965504Seric **	syslog succeeds during interrupt handlers.
293065504Seric */
293165504Seric 
293265504Seric #ifdef __hpux
293365504Seric 
293465504Seric # define MAXSYSLOGTRIES	100
293565504Seric # undef syslog
293665504Seric 
293765504Seric # ifdef __STDC__
293865504Seric hard_syslog(int pri, char *msg, ...)
293965504Seric # else
294065504Seric hard_syslog(pri, msg, va_alist)
294165504Seric 	int pri;
294265504Seric 	char *msg;
294365504Seric 	va_dcl
294465504Seric # endif
294565504Seric {
294665504Seric 	int i;
294765504Seric 	char buf[SYSLOG_BUFSIZE * 2];
294865504Seric 	VA_LOCAL_DECL;
294965504Seric 
295065504Seric 	VA_START(msg);
295165504Seric 	vsprintf(buf, msg, ap);
295265504Seric 	VA_END;
295365504Seric 
295465504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
295565504Seric 		continue;
295665504Seric }
295765504Seric 
295865504Seric #endif
2959