xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68889)
122698Sdist /*
268839Seric  * Copyright (c) 1983, 1995 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*68889Seric static char sccsid[] = "@(#)conf.c	8.157 (Berkeley) 04/25/95";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
17404Seric 
18294Seric /*
193309Seric **  CONF.C -- Sendmail Configuration Tables.
20294Seric **
21294Seric **	Defines the configuration of this installation.
22294Seric **
231388Seric **	Configuration Variables:
242897Seric **		HdrInfo -- a table describing well-known header fields.
252897Seric **			Each entry has the field name and some flags,
264147Seric **			which are described in sendmail.h.
274093Seric **
284093Seric **	Notes:
294093Seric **		I have tried to put almost all the reasonable
304093Seric **		configuration information into the configuration
314093Seric **		file read at runtime.  My intent is that anything
324093Seric **		here is a function of the version of UNIX you
334093Seric **		are running, or is really static -- for example
344093Seric **		the headers are a superset of widely used
354093Seric **		protocols.  If you find yourself playing with
364093Seric **		this file too much, you may be making a mistake!
37294Seric */
38294Seric 
39294Seric 
40294Seric 
41294Seric 
424437Seric /*
432897Seric **  Header info table
443057Seric **	Final (null) entry contains the flags used for any other field.
454147Seric **
464147Seric **	Not all of these are actually handled specially by sendmail
474147Seric **	at this time.  They are included as placeholders, to let
484147Seric **	you know that "someday" I intend to have sendmail do
494147Seric **	something with them.
502897Seric */
512897Seric 
522897Seric struct hdrinfo	HdrInfo[] =
532897Seric {
548060Seric 		/* originator fields, most to least significant  */
5568508Seric 	"resent-sender",		H_FROM|H_RESENT,
5668508Seric 	"resent-from",			H_FROM|H_RESENT,
5768508Seric 	"resent-reply-to",		H_FROM|H_RESENT,
5868508Seric 	"sender",			H_FROM,
5968508Seric 	"from",				H_FROM,
6068508Seric 	"reply-to",			H_FROM,
6168508Seric 	"full-name",			H_ACHECK,
6268508Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
6368508Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6458796Seric 
658060Seric 		/* destination fields */
6668508Seric 	"to",				H_RCPT,
6768508Seric 	"resent-to",			H_RCPT|H_RESENT,
6868508Seric 	"cc",				H_RCPT,
6968508Seric 	"resent-cc",			H_RCPT|H_RESENT,
7068508Seric 	"bcc",				H_RCPT|H_STRIPVAL,
7168508Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
7268508Seric 	"apparently-to",		H_RCPT,
7358796Seric 
748060Seric 		/* message identification and control */
7568508Seric 	"message-id",			0,
7668508Seric 	"resent-message-id",		H_RESENT,
7768508Seric 	"message",			H_EOH,
7868508Seric 	"text",				H_EOH,
7958796Seric 
8011417Seric 		/* date fields */
8168508Seric 	"date",				0,
8268508Seric 	"resent-date",			H_RESENT,
8358796Seric 
848060Seric 		/* trace fields */
8568508Seric 	"received",			H_TRACE|H_FORCE,
8668508Seric 	"x400-received",		H_TRACE|H_FORCE,
8768508Seric 	"via",				H_TRACE|H_FORCE,
8868508Seric 	"mail-from",			H_TRACE|H_FORCE,
898060Seric 
9058796Seric 		/* miscellaneous fields */
9168508Seric 	"comments",			H_FORCE,
9268508Seric 	"return-path",			H_FORCE|H_ACHECK,
9368508Seric 	"content-transfer-encoding",	H_CTE,
9468508Seric 	"content-type",			H_CTYPE,
9568706Seric 	"content-length",		H_ACHECK,
9658796Seric 
979055Seric 	NULL,			0,
982897Seric };
994166Seric 
1004166Seric 
1014166Seric 
1024282Seric /*
1034282Seric **  Location of system files/databases/etc.
1044282Seric */
1054282Seric 
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12264333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12364333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12566783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12658082Seric 	"goaway",		PRIV_GOAWAY,
12758789Seric 	NULL,			0,
12858082Seric };
12958082Seric 
13058082Seric 
13158082Seric 
13258082Seric /*
13324943Seric **  Miscellaneous stuff.
1349039Seric */
1359039Seric 
13624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13724943Seric /*
13824943Seric **  SETDEFAULTS -- set default values
13924943Seric **
14024943Seric **	Because of the way freezing is done, these must be initialized
14124943Seric **	using direct code.
14224943Seric **
14324943Seric **	Parameters:
14458734Seric **		e -- the default envelope.
14524943Seric **
14624943Seric **	Returns:
14724943Seric **		none.
14824943Seric **
14924943Seric **	Side Effects:
15024943Seric **		Initializes a bunch of global variables to their
15124943Seric **		default values.
15224943Seric */
15324943Seric 
15458737Seric #define DAYS		* 24 * 60 * 60
15558737Seric 
15668508Seric void
15758734Seric setdefaults(e)
15858734Seric 	register ENVELOPE *e;
15924943Seric {
16068508Seric 	int i;
16168508Seric 	extern void inittimeouts();
16268508Seric 	extern void setdefuser();
16368508Seric 	extern void setupmaps();
16468508Seric 	extern void setupmailers();
16568508Seric 
16657438Seric 	SpaceSub = ' ';				/* option B */
16757438Seric 	QueueLA = 8;				/* option x */
16857438Seric 	RefuseLA = 12;				/* option X */
16957438Seric 	WkRecipFact = 30000L;			/* option y */
17057438Seric 	WkClassFact = 1800L;			/* option z */
17157438Seric 	WkTimeFact = 90000L;			/* option Z */
17257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17457438Seric 						/* option F */
17557438Seric 	DefUid = 1;				/* option u */
17657438Seric 	DefGid = 1;				/* option g */
17757438Seric 	CheckpointInterval = 10;		/* option C */
17857438Seric 	MaxHopCount = 25;			/* option h */
17958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
18168508Seric 	SevenBitInput = FALSE;			/* option 7 */
18257438Seric 	MaxMciCache = 1;			/* option k */
18357438Seric 	MciCacheTimeout = 300;			/* option K */
18457438Seric 	LogLevel = 9;				/* option L */
18568508Seric 	inittimeouts(NULL);			/* option r */
18658853Seric 	PrivacyFlags = 0;			/* option p */
18768508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18868508Seric 	for (i = 0; i < MAXTOCLASS; i++)
18968508Seric 	{
19068508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19168508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19268508Seric 	}
19368508Seric 	ServiceSwitchFile = "/etc/service.switch";
19440973Sbostic 	setdefuser();
19553654Seric 	setupmaps();
19657402Seric 	setupmailers();
19724943Seric }
198294Seric 
19940973Sbostic 
2004326Seric /*
20140973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20240973Sbostic */
20340973Sbostic 
20468508Seric void
20540973Sbostic setdefuser()
20640973Sbostic {
20740973Sbostic 	struct passwd *defpwent;
20857386Seric 	static char defuserbuf[40];
20940973Sbostic 
21057386Seric 	DefUser = defuserbuf;
21168693Seric 	if ((defpwent = sm_getpwuid(DefUid)) != NULL)
21257386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21340973Sbostic 	else
21457386Seric 		strcpy(defuserbuf, "nobody");
21540973Sbostic }
21653654Seric /*
21756836Seric **  HOST_MAP_INIT -- initialize host class structures
21856836Seric */
21956836Seric 
22068508Seric bool	host_map_init __P((MAP *map, char *args));
22168508Seric 
22256836Seric bool
22360219Seric host_map_init(map, args)
22456836Seric 	MAP *map;
22556836Seric 	char *args;
22656836Seric {
22756836Seric 	register char *p = args;
22856836Seric 
22956836Seric 	for (;;)
23056836Seric 	{
23158050Seric 		while (isascii(*p) && isspace(*p))
23256836Seric 			p++;
23356836Seric 		if (*p != '-')
23456836Seric 			break;
23556836Seric 		switch (*++p)
23656836Seric 		{
23756836Seric 		  case 'a':
23856836Seric 			map->map_app = ++p;
23956836Seric 			break;
24056836Seric 		}
24158050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24256836Seric 			p++;
24356836Seric 		if (*p != '\0')
24456836Seric 			*p++ = '\0';
24556836Seric 	}
24656836Seric 	if (map->map_app != NULL)
24756836Seric 		map->map_app = newstr(map->map_app);
24856836Seric 	return TRUE;
24956836Seric }
25057402Seric /*
25157402Seric **  SETUPMAILERS -- initialize default mailers
25257402Seric */
25356836Seric 
25468508Seric void
25557402Seric setupmailers()
25657402Seric {
25757402Seric 	char buf[100];
25868508Seric 	extern void makemailer();
25957402Seric 
26068868Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=X-Unix, A=sh -c $u");
26157403Seric 	makemailer(buf);
26257403Seric 
26368880Seric 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=X-Unix, A=FILE");
26457402Seric 	makemailer(buf);
26557402Seric 
26657402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26757402Seric 	makemailer(buf);
26857402Seric }
26956836Seric /*
27060207Seric **  SETUPMAPS -- set up map classes
27160207Seric */
27260207Seric 
27360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27460207Seric 	{ \
27560207Seric 		extern bool parse __P((MAP *, char *)); \
27660207Seric 		extern bool open __P((MAP *, int)); \
27760207Seric 		extern void close __P((MAP *)); \
27860207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
27960207Seric 		extern void store __P((MAP *, char *, char *)); \
28060207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28160207Seric 		s->s_mapclass.map_cname = name; \
28260207Seric 		s->s_mapclass.map_ext = ext; \
28360207Seric 		s->s_mapclass.map_cflags = flags; \
28460207Seric 		s->s_mapclass.map_parse = parse; \
28560207Seric 		s->s_mapclass.map_open = open; \
28660207Seric 		s->s_mapclass.map_close = close; \
28760207Seric 		s->s_mapclass.map_lookup = lookup; \
28860207Seric 		s->s_mapclass.map_store = store; \
28960207Seric 	}
29060207Seric 
29168508Seric void
29260207Seric setupmaps()
29360207Seric {
29460207Seric 	register STAB *s;
29560207Seric 
29660207Seric #ifdef NEWDB
29760207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29860207Seric 		map_parseargs, hash_map_open, db_map_close,
29960207Seric 		db_map_lookup, db_map_store);
30068508Seric 
30160207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30260207Seric 		map_parseargs, bt_map_open, db_map_close,
30360207Seric 		db_map_lookup, db_map_store);
30460207Seric #endif
30560207Seric 
30660207Seric #ifdef NDBM
30760207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30860207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30960207Seric 		ndbm_map_lookup, ndbm_map_store);
31060207Seric #endif
31160207Seric 
31260207Seric #ifdef NIS
31360207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31468508Seric 		map_parseargs, nis_map_open, null_map_close,
31568508Seric 		nis_map_lookup, null_map_store);
31660207Seric #endif
31760207Seric 
31868508Seric #ifdef NISPLUS
31968508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32068508Seric 		map_parseargs, nisplus_map_open, null_map_close,
32168508Seric 		nisplus_map_lookup, null_map_store);
32268508Seric #endif
32368508Seric 
32468508Seric #ifdef HESIOD
32568508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
32668508Seric 		map_parseargs, null_map_open, null_map_close,
32768508Seric 		hes_map_lookup, null_map_store);
32868508Seric #endif
32968508Seric 
33068508Seric #ifdef NETINFO
33168508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33268508Seric 		map_parseargs, ni_map_open, null_map_close,
33368508Seric 		ni_map_lookup, null_map_store);
33468508Seric #endif
33568508Seric 
33668508Seric #if 0
33768508Seric 	MAPDEF("dns", NULL, 0,
33868508Seric 		dns_map_init, null_map_open, null_map_close,
33968508Seric 		dns_map_lookup, null_map_store);
34068508Seric #endif
34168508Seric 
34268508Seric #if NAMED_BIND
34368508Seric 	/* best MX DNS lookup */
34468508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
34568508Seric 		map_parseargs, null_map_open, null_map_close,
34668508Seric 		bestmx_map_lookup, null_map_store);
34768508Seric #endif
34868508Seric 
34968508Seric 	MAPDEF("host", NULL, 0,
35068508Seric 		host_map_init, null_map_open, null_map_close,
35168508Seric 		host_map_lookup, null_map_store);
35268508Seric 
35368508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35468508Seric 		map_parseargs, text_map_open, null_map_close,
35568508Seric 		text_map_lookup, null_map_store);
35668508Seric 
35760207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
35868508Seric 		map_parseargs, stab_map_open, null_map_close,
35960207Seric 		stab_map_lookup, stab_map_store);
36060207Seric 
36160207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36260207Seric 		map_parseargs, impl_map_open, impl_map_close,
36360207Seric 		impl_map_lookup, impl_map_store);
36460207Seric 
36568508Seric 	/* access to system passwd file */
36668508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
36768508Seric 		map_parseargs, user_map_open, null_map_close,
36868508Seric 		user_map_lookup, null_map_store);
36960207Seric 
37060207Seric 	/* dequote map */
37160207Seric 	MAPDEF("dequote", NULL, 0,
37260207Seric 		dequote_init, null_map_open, null_map_close,
37360207Seric 		dequote_map, null_map_store);
37460207Seric 
37568521Seric #ifdef USERDB
37660207Seric 	/* user database */
37768521Seric 	MAPDEF("userdb", ".db", 0,
37868521Seric 		map_parseargs, null_map_open, null_map_close,
37960207Seric 		udb_map_lookup, null_map_store);
38060207Seric #endif
38168508Seric 
38268508Seric 	/* sequenced maps */
38368508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
38468508Seric 		seq_map_parse, null_map_open, null_map_close,
38568508Seric 		seq_map_lookup, seq_map_store);
38668508Seric 
38768508Seric 	/* switched interface to sequenced maps */
38868508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
38968508Seric 		map_parseargs, switch_map_open, null_map_close,
39068508Seric 		seq_map_lookup, seq_map_store);
39160207Seric }
39260207Seric 
39360207Seric #undef MAPDEF
39460207Seric /*
39568508Seric **  INITHOSTMAPS -- initial host-dependent maps
39668508Seric **
39768508Seric **	This should act as an interface to any local service switch
39868508Seric **	provided by the host operating system.
39968508Seric **
40068508Seric **	Parameters:
40168508Seric **		none
40268508Seric **
40368508Seric **	Returns:
40468508Seric **		none
40568508Seric **
40668508Seric **	Side Effects:
40768508Seric **		Should define maps "host" and "users" as necessary
40868508Seric **		for this OS.  If they are not defined, they will get
40968508Seric **		a default value later.  It should check to make sure
41068508Seric **		they are not defined first, since it's possible that
41168508Seric **		the config file has provided an override.
41268508Seric */
41368508Seric 
41468508Seric void
41568508Seric inithostmaps()
41668508Seric {
41768508Seric 	register int i;
41868508Seric 	int nmaps;
41968508Seric 	char *maptype[MAXMAPSTACK];
42068508Seric 	short mapreturn[MAXMAPACTIONS];
42168508Seric 	char buf[MAXLINE];
42268508Seric 
42368508Seric 	/*
42468508Seric 	**  Set up default hosts maps.
42568508Seric 	*/
42668508Seric 
42768508Seric #if 0
42868508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
42968508Seric 	for (i = 0; i < nmaps; i++)
43068508Seric 	{
43168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
43268508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
43368508Seric 		{
43468508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
43568508Seric 			makemapentry(buf);
43668508Seric 		}
43768508Seric #if NAMED_BIND
43868508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
43968508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
44068508Seric 		{
44168508Seric 			strcpy(buf, "hosts.dns dns A");
44268508Seric 			makemapentry(buf);
44368508Seric 		}
44468508Seric #endif
44568508Seric #ifdef NISPLUS
44668508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
44768508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
44868508Seric 		{
44968508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
45068508Seric 			makemapentry(buf);
45168508Seric 		}
45268508Seric #endif
45368508Seric #ifdef NIS
45468508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
45568508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
45668508Seric 		{
45768508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
45868508Seric 			makemapentry(buf);
45968508Seric 		}
46068508Seric #endif
46168508Seric 	}
46268508Seric #endif
46368508Seric 
46468508Seric 	/*
46568508Seric 	**  Make sure we have a host map.
46668508Seric 	*/
46768508Seric 
46868508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
46968508Seric 	{
47068508Seric 		/* user didn't initialize: set up host map */
47168508Seric 		strcpy(buf, "host host");
47268508Seric #if NAMED_BIND
47368508Seric 		if (ConfigLevel >= 2)
47468508Seric 			strcat(buf, " -a.");
47568508Seric #endif
47668508Seric 		makemapentry(buf);
47768508Seric 	}
47868508Seric 
47968508Seric 	/*
48068508Seric 	**  Set up default aliases maps
48168508Seric 	*/
48268508Seric 
48368508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
48468508Seric 	for (i = 0; i < nmaps; i++)
48568508Seric 	{
48668508Seric 		if (strcmp(maptype[i], "files") == 0 &&
48768508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
48868508Seric 		{
48968508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
49068508Seric 			makemapentry(buf);
49168508Seric 		}
49268508Seric #ifdef NISPLUS
49368508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
49468508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
49568508Seric 		{
49668508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
49768508Seric 			makemapentry(buf);
49868508Seric 		}
49968508Seric #endif
50068508Seric #ifdef NIS
50168508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
50268508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
50368508Seric 		{
50468508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
50568508Seric 			makemapentry(buf);
50668508Seric 		}
50768508Seric #endif
50868508Seric 	}
50968508Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
51068508Seric 	{
51168508Seric 		strcpy(buf, "aliases switch aliases");
51268508Seric 		makemapentry(buf);
51368508Seric 	}
51468508Seric 	strcpy(buf, "switch:aliases");
51568508Seric 	setalias(buf);
51668508Seric 
51768508Seric #if 0		/* "user" map class is a better choice */
51868508Seric 	/*
51968508Seric 	**  Set up default users maps.
52068508Seric 	*/
52168508Seric 
52268508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
52368508Seric 	for (i = 0; i < nmaps; i++)
52468508Seric 	{
52568508Seric 		if (strcmp(maptype[i], "files") == 0 &&
52668508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
52768508Seric 		{
52868508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
52968508Seric 			makemapentry(buf);
53068508Seric 		}
53168508Seric #ifdef NISPLUS
53268508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
53368508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
53468508Seric 		{
53568508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
53668508Seric 			makemapentry(buf);
53768508Seric 		}
53868508Seric #endif
53968508Seric #ifdef NIS
54068508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
54168508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
54268508Seric 		{
54368508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
54468508Seric 			makemapentry(buf);
54568508Seric 		}
54668508Seric #endif
54768508Seric #ifdef HESIOD
54868508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
54968508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
55068508Seric 		{
55168508Seric 			strcpy(buf, "users.hesiod hesiod");
55268508Seric 			makemapentry(buf);
55368508Seric 		}
55468508Seric #endif
55568508Seric 	}
55668508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
55768508Seric 	{
55868508Seric 		strcpy(buf, "users switch -m passwd");
55968508Seric 		makemapentry(buf);
56068508Seric 	}
56168508Seric #endif
56268508Seric }
56368508Seric /*
56468508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
56568508Seric **
56668508Seric **	This is the system-dependent interface to the service switch.
56768508Seric **
56868508Seric **	Parameters:
56968508Seric **		service -- the name of the service of interest.
57068508Seric **		maptype -- an out-array of strings containing the types
57168508Seric **			of access to use for this service.  There can
57268508Seric **			be at most MAXMAPSTACK types for a single service.
57368508Seric **		mapreturn -- an out-array of return information bitmaps
57468508Seric **			for the map.
57568508Seric **
57668508Seric **	Returns:
57768508Seric **		The number of map types filled in, or -1 for failure.
57868508Seric */
57968508Seric 
58068508Seric #ifdef SOLARIS
58168508Seric # include <nsswitch.h>
58268508Seric #endif
58368508Seric 
58468508Seric #if defined(ultrix) || defined(__osf__)
58568508Seric # include <sys/svcinfo.h>
58668508Seric #endif
58768508Seric 
58868508Seric int
58968508Seric switch_map_find(service, maptype, mapreturn)
59068508Seric 	char *service;
59168508Seric 	char *maptype[MAXMAPSTACK];
59268508Seric 	short mapreturn[MAXMAPACTIONS];
59368508Seric {
59468508Seric 	register FILE *fp;
59568508Seric 	int svcno;
59668508Seric 	static char buf[MAXLINE];
59768508Seric 
59868508Seric #ifdef SOLARIS
59968508Seric 	struct __nsw_switchconfig *nsw_conf;
60068508Seric 	enum __nsw_parse_err pserr;
60168508Seric 	struct __nsw_lookup *lk;
60268508Seric 	int nsw_rc;
60368508Seric 	static struct __nsw_lookup lkp0 =
60468508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
60568508Seric 	static struct __nsw_switchconfig lkp_default =
60668508Seric 		{ 0, "sendmail", 3, &lkp0 };
60768508Seric 
60868508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
60968508Seric 		lk = lkp_default.lookups;
61068508Seric 	else
61168508Seric 		lk = nsw_conf->lookups;
61268508Seric 	svcno = 0;
61368508Seric 	while (lk != NULL)
61468508Seric 	{
61568508Seric 		maptype[svcno] = lk->service_name;
61668508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
61768508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
61868508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
61968508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62068508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
62168508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62268508Seric 		svcno++;
62368508Seric 		lk = lk->next;
62468508Seric 	}
62568508Seric 	return svcno;
62668508Seric #endif
62768508Seric 
62868508Seric #if defined(ultrix) || defined(__osf__)
62968508Seric 	struct svcinfo *svcinfo;
63068508Seric 	int svc;
63168508Seric 
63268508Seric 	svcinfo = getsvc();
63368508Seric 	if (svcinfo == NULL)
63468508Seric 		goto punt;
63568508Seric 	if (strcmp(service, "hosts") == 0)
63668508Seric 		svc = SVC_HOSTS;
63768508Seric 	else if (strcmp(service, "aliases") == 0)
63868508Seric 		svc = SVC_ALIASES;
63968508Seric 	else if (strcmp(service, "passwd") == 0)
64068508Seric 		svc = SVC_PASSWD;
64168508Seric 	else
64268508Seric 		return -1;
64368508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
64468508Seric 	{
64568508Seric 		switch (svcinfo->svcpath[svc][svcno])
64668508Seric 		{
64768508Seric 		  case SVC_LOCAL:
64868508Seric 			maptype[svcno] = "files";
64968508Seric 			break;
65068508Seric 
65168508Seric 		  case SVC_YP:
65268508Seric 			maptype[svcno] = "nis";
65368508Seric 			break;
65468508Seric 
65568508Seric 		  case SVC_BIND:
65668508Seric 			maptype[svcno] = "dns";
65768508Seric 			break;
65868508Seric 
65968508Seric #ifdef SVC_HESIOD
66068508Seric 		  case SVC_HESIOD:
66168508Seric 			maptype[svcno] = "hesiod";
66268508Seric 			break;
66368508Seric #endif
66468508Seric 
66568508Seric 		  case SVC_LAST:
66668508Seric 			return svcno;
66768508Seric 		}
66868508Seric 	}
66968508Seric 	return svcno;
67068508Seric #endif
67168508Seric 
67268508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
67368508Seric 	/*
67468508Seric 	**  Fall-back mechanism.
67568508Seric 	*/
67668508Seric 
67768508Seric 	svcno = 0;
67868508Seric 	fp = fopen(ServiceSwitchFile, "r");
67968508Seric 	if (fp != NULL)
68068508Seric 	{
68168508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
68268508Seric 		{
68368508Seric 			register char *p;
68468508Seric 
68568508Seric 			p = strpbrk(buf, "#\n");
68668508Seric 			if (p != NULL)
68768508Seric 				*p = '\0';
68868508Seric 			p = strpbrk(buf, " \t");
68968508Seric 			if (p != NULL)
69068508Seric 				*p++ = '\0';
69168508Seric 			if (strcmp(buf, service) != 0)
69268508Seric 				continue;
69368508Seric 
69468508Seric 			/* got the right service -- extract data */
69568508Seric 			do
69668508Seric 			{
69768508Seric 				while (isspace(*p))
69868508Seric 					p++;
69968508Seric 				if (*p == '\0')
70068508Seric 					break;
70168508Seric 				maptype[svcno++] = p;
70268508Seric 				p = strpbrk(p, " \t");
70368508Seric 				if (p != NULL)
70468508Seric 					*p++ = '\0';
70568508Seric 			} while (p != NULL);
70668508Seric 			break;
70768508Seric 		}
70868508Seric 		fclose(fp);
70968508Seric 		return svcno;
71068508Seric 	}
71168508Seric #endif
71268508Seric 
71368508Seric 	/* if the service file doesn't work, use an absolute fallback */
71468508Seric   punt:
71568508Seric 	if (strcmp(service, "aliases") == 0)
71668508Seric 	{
71768628Seric 		maptype[svcno++] = "files";
71868628Seric #ifdef AUTO_NIS_ALIASES
71968628Seric # ifdef NISPLUS
72068628Seric 		maptype[svcno++] = "nisplus";
72168628Seric # endif
72268628Seric # ifdef NIS
72368628Seric 		maptype[svcno++] = "nis";
72468628Seric # endif
72568628Seric #endif
72668628Seric 		return svcno;
72768508Seric 	}
72868508Seric 	if (strcmp(service, "hosts") == 0)
72968508Seric 	{
73068508Seric # if NAMED_BIND
73168508Seric 		maptype[svcno++] = "dns";
73268508Seric # else
73368508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
73468508Seric 		/* SunOS */
73568508Seric 		maptype[svcno++] = "nis";
73668508Seric #  endif
73768508Seric # endif
73868508Seric 		maptype[svcno++] = "files";
73968508Seric 		return svcno;
74068508Seric 	}
74168508Seric 	return -1;
74268508Seric }
74368508Seric /*
7449369Seric **  USERNAME -- return the user id of the logged in user.
7459369Seric **
7469369Seric **	Parameters:
7479369Seric **		none.
7489369Seric **
7499369Seric **	Returns:
7509369Seric **		The login name of the logged in user.
7519369Seric **
7529369Seric **	Side Effects:
7539369Seric **		none.
7549369Seric **
7559369Seric **	Notes:
7569369Seric **		The return value is statically allocated.
7579369Seric */
7589369Seric 
7599369Seric char *
7609369Seric username()
7619369Seric {
76217469Seric 	static char *myname = NULL;
7639369Seric 	extern char *getlogin();
76419904Smiriam 	register struct passwd *pw;
7659369Seric 
76617469Seric 	/* cache the result */
76717469Seric 	if (myname == NULL)
76817469Seric 	{
76917469Seric 		myname = getlogin();
77017469Seric 		if (myname == NULL || myname[0] == '\0')
77117469Seric 		{
77268693Seric 			pw = sm_getpwuid(RealUid);
77317469Seric 			if (pw != NULL)
77440993Sbostic 				myname = newstr(pw->pw_name);
77517469Seric 		}
77619904Smiriam 		else
77719904Smiriam 		{
77863787Seric 			uid_t uid = RealUid;
77919873Smiriam 
78040993Sbostic 			myname = newstr(myname);
78168693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
78258736Seric 			      (uid != 0 && uid != pw->pw_uid))
78319904Smiriam 			{
78468693Seric 				pw = sm_getpwuid(uid);
78524945Seric 				if (pw != NULL)
78640993Sbostic 					myname = newstr(pw->pw_name);
78719873Smiriam 			}
78819873Smiriam 		}
78917469Seric 		if (myname == NULL || myname[0] == '\0')
79017469Seric 		{
79158151Seric 			syserr("554 Who are you?");
79217469Seric 			myname = "postmaster";
79317469Seric 		}
79417469Seric 	}
79517469Seric 
79617469Seric 	return (myname);
7979369Seric }
7989369Seric /*
7994190Seric **  TTYPATH -- Get the path of the user's tty
800294Seric **
801294Seric **	Returns the pathname of the user's tty.  Returns NULL if
802294Seric **	the user is not logged in or if s/he has write permission
803294Seric **	denied.
804294Seric **
805294Seric **	Parameters:
806294Seric **		none
807294Seric **
808294Seric **	Returns:
809294Seric **		pathname of the user's tty.
810294Seric **		NULL if not logged in or write permission denied.
811294Seric **
812294Seric **	Side Effects:
813294Seric **		none.
814294Seric **
815294Seric **	WARNING:
816294Seric **		Return value is in a local buffer.
817294Seric **
818294Seric **	Called By:
819294Seric **		savemail
820294Seric */
821294Seric 
822294Seric char *
823294Seric ttypath()
824294Seric {
825294Seric 	struct stat stbuf;
826294Seric 	register char *pathn;
827294Seric 	extern char *ttyname();
8284081Seric 	extern char *getlogin();
829294Seric 
830294Seric 	/* compute the pathname of the controlling tty */
8319369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8329369Seric 	    (pathn = ttyname(0)) == NULL)
833294Seric 	{
834294Seric 		errno = 0;
835294Seric 		return (NULL);
836294Seric 	}
837294Seric 
838294Seric 	/* see if we have write permission */
8392967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
840294Seric 	{
841294Seric 		errno = 0;
842294Seric 		return (NULL);
843294Seric 	}
844294Seric 
845294Seric 	/* see if the user is logged in */
846294Seric 	if (getlogin() == NULL)
847294Seric 		return (NULL);
848294Seric 
849294Seric 	/* looks good */
850294Seric 	return (pathn);
851294Seric }
8522967Seric /*
8532967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8542967Seric **
8552967Seric **	This routine can be supplied on a per-installation basis
8562967Seric **	to determine whether a person is allowed to send a message.
8572967Seric **	This allows restriction of certain types of internet
8582967Seric **	forwarding or registration of users.
8592967Seric **
8602967Seric **	If the hosts are found to be incompatible, an error
86168857Seric **	message should be given using "usrerr" and an EX_ code
86268857Seric **	should be returned.  You can also set to->q_status to
86368857Seric **	a DSN-style status code.
8642967Seric **
86568559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
86668559Seric **	body during the return-to-sender function; this should be done
86768559Seric **	on huge messages.  This bit may already be set by the ESMTP
86868559Seric **	protocol.
8694288Seric **
8702967Seric **	Parameters:
8712967Seric **		to -- the person being sent to.
8722967Seric **
8732967Seric **	Returns:
87457459Seric **		an exit status
8752967Seric **
8762967Seric **	Side Effects:
8772967Seric **		none (unless you include the usrerr stuff)
8782967Seric */
8792967Seric 
88068508Seric int
88155012Seric checkcompat(to, e)
8822967Seric 	register ADDRESS *to;
88355012Seric 	register ENVELOPE *e;
8842967Seric {
88512133Seric # ifdef lint
88612133Seric 	if (to == NULL)
88712133Seric 		to++;
88863834Seric # endif /* lint */
88964402Seric 
89064402Seric 	if (tTd(49, 1))
89164402Seric 		printf("checkcompat(to=%s, from=%s)\n",
89264402Seric 			to->q_paddr, e->e_from.q_paddr);
89364402Seric 
89410698Seric # ifdef EXAMPLE_CODE
89510698Seric 	/* this code is intended as an example only */
8964437Seric 	register STAB *s;
8974437Seric 
8984437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
89968508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9009369Seric 	    to->q_mailer == s->s_mailer)
9014437Seric 	{
90258151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
90368559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
90468857Seric 		to->q_status = "5.7.1";
90557459Seric 		return (EX_UNAVAILABLE);
9064437Seric 	}
90756795Seric # endif /* EXAMPLE_CODE */
90857459Seric 	return (EX_OK);
9092967Seric }
9109369Seric /*
91164035Seric **  SETSIGNAL -- set a signal handler
91264035Seric **
91364035Seric **	This is essentially old BSD "signal(3)".
91464035Seric */
91564035Seric 
91664561Seric sigfunc_t
91764035Seric setsignal(sig, handler)
91864035Seric 	int sig;
91964561Seric 	sigfunc_t handler;
92064035Seric {
92164380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
92264035Seric 	return signal(sig, handler);
92364035Seric #else
92464035Seric 	struct sigaction n, o;
92564035Seric 
92664035Seric 	bzero(&n, sizeof n);
92764035Seric 	n.sa_handler = handler;
92868508Seric # ifdef SA_RESTART
92968508Seric 	n.sa_flags = SA_RESTART;
93068508Seric # endif
93164035Seric 	if (sigaction(sig, &n, &o) < 0)
93264035Seric 		return SIG_ERR;
93364035Seric 	return o.sa_handler;
93464035Seric #endif
93564035Seric }
93664035Seric /*
9379369Seric **  HOLDSIGS -- arrange to hold all signals
9389369Seric **
9399369Seric **	Parameters:
9409369Seric **		none.
9419369Seric **
9429369Seric **	Returns:
9439369Seric **		none.
9449369Seric **
9459369Seric **	Side Effects:
9469369Seric **		Arranges that signals are held.
9479369Seric */
9489369Seric 
94968508Seric void
9509369Seric holdsigs()
9519369Seric {
9529369Seric }
9539369Seric /*
9549369Seric **  RLSESIGS -- arrange to release all signals
9559369Seric **
9569369Seric **	This undoes the effect of holdsigs.
9579369Seric **
9589369Seric **	Parameters:
9599369Seric **		none.
9609369Seric **
9619369Seric **	Returns:
9629369Seric **		none.
9639369Seric **
9649369Seric **	Side Effects:
9659369Seric **		Arranges that signals are released.
9669369Seric */
9679369Seric 
96868508Seric void
9699369Seric rlsesigs()
9709369Seric {
9719369Seric }
97214872Seric /*
97364705Seric **  INIT_MD -- do machine dependent initializations
97464705Seric **
97564705Seric **	Systems that have global modes that should be set should do
97664705Seric **	them here rather than in main.
97764705Seric */
97864705Seric 
97964705Seric #ifdef _AUX_SOURCE
98064705Seric # include	<compat.h>
98164705Seric #endif
98264705Seric 
98368508Seric void
98464825Seric init_md(argc, argv)
98564825Seric 	int argc;
98664825Seric 	char **argv;
98764705Seric {
98864705Seric #ifdef _AUX_SOURCE
98964705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
99064705Seric #endif
99168508Seric 
99268508Seric #ifdef VENDOR_DEFAULT
99368508Seric 	VendorCode = VENDOR_DEFAULT;
99468508Seric #else
99568508Seric 	VendorCode = VENDOR_BERKELEY;
99668508Seric #endif
99764705Seric }
99864705Seric /*
99968508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
100068508Seric **
100168508Seric **	Called once, on startup.
100268508Seric **
100368508Seric **	Parameters:
100468508Seric **		e -- the global envelope.
100568508Seric **
100668508Seric **	Returns:
100768508Seric **		none.
100868508Seric **
100968508Seric **	Side Effects:
101068508Seric **		vendor-dependent.
101168508Seric */
101268508Seric 
101368508Seric void
101468508Seric init_vendor_macros(e)
101568508Seric 	register ENVELOPE *e;
101668508Seric {
101768508Seric }
101868508Seric /*
101914872Seric **  GETLA -- get the current load average
102014872Seric **
102114881Seric **	This code stolen from la.c.
102214881Seric **
102314872Seric **	Parameters:
102414872Seric **		none.
102514872Seric **
102614872Seric **	Returns:
102714872Seric **		The current load average as an integer.
102814872Seric **
102914872Seric **	Side Effects:
103014872Seric **		none.
103114872Seric */
103214872Seric 
103351920Seric /* try to guess what style of load average we have */
103451920Seric #define LA_ZERO		1	/* always return load average as zero */
103564360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
103651920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
103751920Seric #define LA_SUBR		4	/* call getloadavg */
103864295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
103964360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
104066301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
104114872Seric 
104263962Seric /* do guesses based on general OS type */
104351920Seric #ifndef LA_TYPE
104463962Seric # define LA_TYPE	LA_ZERO
104551920Seric #endif
104651920Seric 
104764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
104851920Seric 
104914872Seric #include <nlist.h>
105014872Seric 
105168508Seric #ifdef IRIX64
105268508Seric # define nlist		nlist64
105368508Seric #endif
105468508Seric 
105551920Seric #ifndef LA_AVENRUN
105663962Seric # ifdef SYSTEM5
105763962Seric #  define LA_AVENRUN	"avenrun"
105863962Seric # else
105963962Seric #  define LA_AVENRUN	"_avenrun"
106063962Seric # endif
106151920Seric #endif
106251920Seric 
106351920Seric /* _PATH_UNIX should be defined in <paths.h> */
106451920Seric #ifndef _PATH_UNIX
106563962Seric # if defined(SYSTEM5)
106663962Seric #  define _PATH_UNIX	"/unix"
106763962Seric # else
106863962Seric #  define _PATH_UNIX	"/vmunix"
106963962Seric # endif
107051920Seric #endif
107151920Seric 
107214872Seric struct	nlist Nl[] =
107314872Seric {
107451920Seric 	{ LA_AVENRUN },
107514872Seric #define	X_AVENRUN	0
107614872Seric 	{ 0 },
107714872Seric };
107814872Seric 
107960583Seric #ifndef FSHIFT
108060583Seric # if defined(unixpc)
108160583Seric #  define FSHIFT	5
108260583Seric # endif
108357736Seric 
108466763Seric # if defined(__alpha) || defined(IRIX)
108560583Seric #  define FSHIFT	10
108660583Seric # endif
108768508Seric 
108868508Seric # if defined(_AIX3)
108968508Seric #  define FSHIFT	16
109068508Seric # endif
109166788Seric #endif
109257977Seric 
109366788Seric #ifndef FSHIFT
109466788Seric # define FSHIFT		8
109557736Seric #endif
109660583Seric 
109766788Seric #ifndef FSCALE
109866788Seric # define FSCALE		(1 << FSHIFT)
109951920Seric #endif
110040930Srick 
110114872Seric getla()
110214872Seric {
110314872Seric 	static int kmem = -1;
110451920Seric #if LA_TYPE == LA_INT
110524943Seric 	long avenrun[3];
110651920Seric #else
110764360Seric # if LA_TYPE == LA_SHORT
110864360Seric 	short avenrun[3];
110964360Seric # else
111051920Seric 	double avenrun[3];
111164360Seric # endif
111251920Seric #endif
111325615Seric 	extern off_t lseek();
111457736Seric 	extern int errno;
111514872Seric 
111614872Seric 	if (kmem < 0)
111714872Seric 	{
111824945Seric 		kmem = open("/dev/kmem", 0, 0);
111914872Seric 		if (kmem < 0)
112057736Seric 		{
112157736Seric 			if (tTd(3, 1))
112257736Seric 				printf("getla: open(/dev/kmem): %s\n",
112357736Seric 					errstring(errno));
112414872Seric 			return (-1);
112557736Seric 		}
112651920Seric 		(void) fcntl(kmem, F_SETFD, 1);
112768508Seric #ifdef _AIX3
112868508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
112968508Seric #else
113057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
113168508Seric #endif
113257736Seric 		{
113357736Seric 			if (tTd(3, 1))
113457736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
113557736Seric 					errstring(errno));
113614872Seric 			return (-1);
113757736Seric 		}
113859253Seric 		if (Nl[X_AVENRUN].n_value == 0)
113959253Seric 		{
114059253Seric 			if (tTd(3, 1))
114159253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
114259253Seric 					_PATH_UNIX, LA_AVENRUN);
114359253Seric 			return (-1);
114459253Seric 		}
114568508Seric #ifdef NAMELISTMASK
114668508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
114766763Seric #endif
114814872Seric 	}
114957736Seric 	if (tTd(3, 20))
115057736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
115168508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
115223118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
115319967Seric 	{
115419967Seric 		/* thank you Ian */
115557736Seric 		if (tTd(3, 1))
115657736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
115719967Seric 		return (-1);
115819967Seric 	}
115964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
116057736Seric 	if (tTd(3, 5))
116157736Seric 	{
116257736Seric 		printf("getla: avenrun = %d", avenrun[0]);
116357736Seric 		if (tTd(3, 15))
116457736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
116557736Seric 		printf("\n");
116657736Seric 	}
116757736Seric 	if (tTd(3, 1))
116857736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116924943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
117051920Seric #else
117157736Seric 	if (tTd(3, 5))
117257736Seric 	{
117357736Seric 		printf("getla: avenrun = %g", avenrun[0]);
117457736Seric 		if (tTd(3, 15))
117557736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
117657736Seric 		printf("\n");
117757736Seric 	}
117857736Seric 	if (tTd(3, 1))
117957736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
118051920Seric 	return ((int) (avenrun[0] + 0.5));
118151920Seric #endif
118214872Seric }
118314872Seric 
118451773Seric #else
118551920Seric #if LA_TYPE == LA_SUBR
118651773Seric 
118764718Seric #ifdef DGUX
118864718Seric 
118964718Seric #include <sys/dg_sys_info.h>
119064718Seric 
119168508Seric int
119268508Seric getla()
119364718Seric {
119464718Seric 	struct dg_sys_info_load_info load_info;
119564718Seric 
119664718Seric 	dg_sys_info((long *)&load_info,
119764718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
119864718Seric 
119968508Seric         if (tTd(3, 1))
120068508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
120168508Seric 
120264718Seric 	return((int) (load_info.one_minute + 0.5));
120364718Seric }
120464718Seric 
120564718Seric #else
120668508Seric # ifdef __hpux
120764718Seric 
120868869Seric struct pst_dynamic;
120968869Seric 
121068508Seric #  include <sys/param.h>
121168508Seric #  include <sys/pstat.h>
121268508Seric 
121368508Seric int
121451773Seric getla()
121551773Seric {
121668508Seric 	struct pst_dynamic pstd;
121768508Seric 
121868508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
121968869Seric 			     (size_t) 1, 0) == -1)
122068508Seric 		return 0;
122168508Seric 
122268508Seric         if (tTd(3, 1))
122368508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
122468508Seric 
122568508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
122668508Seric }
122768508Seric 
122868508Seric # else
122968508Seric 
123068508Seric int
123168508Seric getla()
123268508Seric {
123351920Seric 	double avenrun[3];
123451920Seric 
123551920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
123657736Seric 	{
123757736Seric 		if (tTd(3, 1))
123857736Seric 			perror("getla: getloadavg failed:");
123951920Seric 		return (-1);
124057736Seric 	}
124157736Seric 	if (tTd(3, 1))
124257736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
124351920Seric 	return ((int) (avenrun[0] + 0.5));
124451773Seric }
124551773Seric 
124668508Seric # endif /* __hpux */
124764718Seric #endif /* DGUX */
124851773Seric #else
124964295Seric #if LA_TYPE == LA_MACH
125051773Seric 
125164295Seric /*
125265173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
125364295Seric */
125464295Seric 
125565173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
125665173Seric # include <mach/mach.h>
125765173Seric #else
125865173Seric # include <mach.h>
125965173Seric #endif
126064295Seric 
126151773Seric getla()
126251773Seric {
126364295Seric 	processor_set_t default_set;
126464295Seric 	kern_return_t error;
126564295Seric 	unsigned int info_count;
126664295Seric 	struct processor_set_basic_info info;
126764295Seric 	host_t host;
126864295Seric 
126964295Seric 	error = processor_set_default(host_self(), &default_set);
127064295Seric 	if (error != KERN_SUCCESS)
127164295Seric 		return -1;
127264295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
127364295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
127464295Seric 			       &host, (processor_set_info_t)&info,
127564295Seric 			       &info_count) != KERN_SUCCESS)
127664295Seric 	{
127764295Seric 		return -1;
127864295Seric 	}
127964295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
128064295Seric }
128164295Seric 
128264295Seric 
128364295Seric #else
128466313Seric #if LA_TYPE == LA_PROCSTR
128564295Seric 
128666301Seric /*
128766301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
128866301Seric **  in a format like "0.15 0.12 0.06".
128966301Seric **
129066301Seric **	Initially intended for Linux.  This has been in the kernel
129166301Seric **	since at least 0.99.15.
129266301Seric */
129366301Seric 
129466301Seric # ifndef _PATH_LOADAVG
129566301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
129666301Seric # endif
129766301Seric 
129866301Seric int
129964295Seric getla()
130064295Seric {
130166319Seric 	double avenrun;
130266301Seric 	register int result;
130366319Seric 	FILE *fp;
130466301Seric 
130566319Seric 	fp = fopen(_PATH_LOADAVG, "r");
130666319Seric 	if (fp == NULL)
130766301Seric 	{
130866319Seric 		if (tTd(3, 1))
130966319Seric 			printf("getla: fopen(%s): %s\n",
131066319Seric 				_PATH_LOADAVG, errstring(errno));
131166319Seric 		return -1;
131266301Seric 	}
131366301Seric 	result = fscanf(fp, "%lf", &avenrun);
131466319Seric 	fclose(fp);
131566301Seric 	if (result != 1)
131666301Seric 	{
131766310Seric 		if (tTd(3, 1))
131866301Seric 			printf("getla: fscanf() = %d: %s\n",
131966301Seric 				result, errstring(errno));
132066301Seric 		return -1;
132166301Seric 	}
132266301Seric 
132357736Seric 	if (tTd(3, 1))
132466301Seric 		printf("getla(): %.2f\n", avenrun);
132566301Seric 
132666301Seric 	return ((int) (avenrun + 0.5));
132766301Seric }
132866301Seric 
132966301Seric #else
133066301Seric 
133166301Seric getla()
133266301Seric {
133366301Seric 	if (tTd(3, 1))
133457736Seric 		printf("getla: ZERO\n");
133551920Seric 	return (0);
133651773Seric }
133751773Seric 
133851773Seric #endif
133951773Seric #endif
134064295Seric #endif
134166301Seric #endif
134266045Seric 
134366045Seric 
134466045Seric /*
134566045Seric  * Copyright 1989 Massachusetts Institute of Technology
134666045Seric  *
134766045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
134866045Seric  * documentation for any purpose is hereby granted without fee, provided that
134966045Seric  * the above copyright notice appear in all copies and that both that
135066045Seric  * copyright notice and this permission notice appear in supporting
135166045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
135266045Seric  * publicity pertaining to distribution of the software without specific,
135366045Seric  * written prior permission.  M.I.T. makes no representations about the
135466045Seric  * suitability of this software for any purpose.  It is provided "as is"
135566045Seric  * without express or implied warranty.
135666045Seric  *
135766045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
135866045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
135966045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
136066045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
136166045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
136266045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
136366045Seric  *
136466045Seric  * Authors:  Many and varied...
136566045Seric  */
136666045Seric 
136766045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
136866045Seric #ifndef lint
136966045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
137066045Seric #endif /* !lint */
137166045Seric 
137266179Seric #ifdef apollo
137366045Seric # undef volatile
137466045Seric #    include <apollo/base.h>
137566045Seric 
137666045Seric /* ARGSUSED */
137766045Seric int getloadavg( call_data )
137866045Seric      caddr_t	call_data;	/* pointer to (double) return value */
137966045Seric {
138066045Seric      double *avenrun = (double *) call_data;
138166045Seric      int i;
138266045Seric      status_$t      st;
138366045Seric      long loadav[3];
138466045Seric      proc1_$get_loadav(loadav, &st);
138566045Seric      *avenrun = loadav[0] / (double) (1 << 16);
138666045Seric      return(0);
138766045Seric }
138866045Seric #   endif /* apollo */
138924943Seric /*
139024943Seric **  SHOULDQUEUE -- should this message be queued or sent?
139124943Seric **
139224943Seric **	Compares the message cost to the load average to decide.
139324943Seric **
139424943Seric **	Parameters:
139524943Seric **		pri -- the priority of the message in question.
139657438Seric **		ctime -- the message creation time.
139724943Seric **
139824943Seric **	Returns:
139924943Seric **		TRUE -- if this message should be queued up for the
140024943Seric **			time being.
140124943Seric **		FALSE -- if the load is low enough to send this message.
140224943Seric **
140324943Seric **	Side Effects:
140424943Seric **		none.
140524943Seric */
140624943Seric 
140724943Seric bool
140857438Seric shouldqueue(pri, ctime)
140924943Seric 	long pri;
141057438Seric 	time_t ctime;
141124943Seric {
141268508Seric 	bool rval;
141368508Seric 
141468508Seric 	if (tTd(3, 30))
141568508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
141651920Seric 	if (CurrentLA < QueueLA)
141768508Seric 	{
141868508Seric 		if (tTd(3, 30))
141968508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
142024943Seric 		return (FALSE);
142168508Seric 	}
142258132Seric 	if (CurrentLA >= RefuseLA)
142368508Seric 	{
142468508Seric 		if (tTd(3, 30))
142568508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
142658132Seric 		return (TRUE);
142768508Seric 	}
142868508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
142968508Seric 	if (tTd(3, 30))
143068508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
143168508Seric 	return rval;
143224943Seric }
143324943Seric /*
143453037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
143553037Seric **
143653037Seric **	Parameters:
143753037Seric **		none.
143853037Seric **
143953037Seric **	Returns:
144053037Seric **		TRUE if incoming SMTP connections should be refused
144153037Seric **			(for now).
144253037Seric **		FALSE if we should accept new work.
144353037Seric **
144453037Seric **	Side Effects:
144553037Seric **		none.
144653037Seric */
144753037Seric 
144853037Seric bool
144953037Seric refuseconnections()
145053037Seric {
145168508Seric 	extern bool enoughspace();
145268508Seric 
145359156Seric #ifdef XLA
145459156Seric 	if (!xla_smtp_ok())
145559156Seric 		return TRUE;
145659156Seric #endif
145759156Seric 
145853037Seric 	/* this is probably too simplistic */
145968508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
146053037Seric }
146153037Seric /*
146224943Seric **  SETPROCTITLE -- set process title for ps
146324943Seric **
146424943Seric **	Parameters:
146558674Seric **		fmt -- a printf style format string.
146658674Seric **		a, b, c -- possible parameters to fmt.
146724943Seric **
146824943Seric **	Returns:
146924943Seric **		none.
147024943Seric **
147124943Seric **	Side Effects:
147224943Seric **		Clobbers argv of our main procedure so ps(1) will
147324943Seric **		display the title.
147424943Seric */
147524943Seric 
147668508Seric #define SPT_NONE	0	/* don't use it at all */
147768508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
147868508Seric #define SPT_BUILTIN	2	/* use libc builtin */
147968508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
148068508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
148168508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
148268508Seric 
148368508Seric #ifndef SPT_TYPE
148468508Seric # define SPT_TYPE	SPT_REUSEARGV
148568508Seric #endif
148668508Seric 
148768508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
148868508Seric 
148968508Seric # if SPT_TYPE == SPT_PSTAT
149058689Seric #  include <sys/pstat.h>
149158689Seric # endif
149268508Seric # if SPT_TYPE == SPT_PSSTRINGS
149359732Seric #  include <machine/vmparam.h>
149459732Seric #  include <sys/exec.h>
149568508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
149668508Seric #   undef SPT_TYPE
149768508Seric #   define SPT_TYPE	SPT_REUSEARGV
149868734Seric #  else
149968734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
150068734Seric #    define NKPDE 63
150168734Seric typedef unsigned int	*pt_entry_t;
150268734Seric #   endif
150364247Seric #  endif
150459732Seric # endif
150568508Seric 
150668508Seric # if SPT_TYPE == SPT_PSSTRINGS
150768508Seric #  define SETPROC_STATIC	static
150868508Seric # else
150959732Seric #  define SETPROC_STATIC
151059732Seric # endif
151167626Seric 
151268508Seric # ifndef SPT_PADCHAR
151368508Seric #  define SPT_PADCHAR	' '
151468508Seric # endif
151558689Seric 
151668508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
151764843Seric 
151868508Seric #if SPT_TYPE != SPT_BUILTIN
151968508Seric 
152024943Seric /*VARARGS1*/
152168508Seric void
152268508Seric # ifdef __STDC__
152357642Seric setproctitle(char *fmt, ...)
152468508Seric # else
152557642Seric setproctitle(fmt, va_alist)
152624943Seric 	char *fmt;
152757642Seric 	va_dcl
152868508Seric # endif
152924943Seric {
153068508Seric # if SPT_TYPE != SPT_NONE
153124943Seric 	register char *p;
153225049Seric 	register int i;
153359732Seric 	SETPROC_STATIC char buf[MAXLINE];
153456852Seric 	VA_LOCAL_DECL
153568508Seric #  if SPT_TYPE == SPT_PSTAT
153658689Seric 	union pstun pst;
153758689Seric #  endif
153824943Seric 	extern char **Argv;
153924943Seric 	extern char *LastArgv;
154024943Seric 
154158674Seric 	p = buf;
154224943Seric 
154358674Seric 	/* print sendmail: heading for grep */
154458674Seric 	(void) strcpy(p, "sendmail: ");
154558674Seric 	p += strlen(p);
154624943Seric 
154758674Seric 	/* print the argument string */
154858674Seric 	VA_START(fmt);
154958674Seric 	(void) vsprintf(p, fmt, ap);
155056852Seric 	VA_END;
155154996Seric 
155258674Seric 	i = strlen(buf);
155358689Seric 
155468508Seric #  if SPT_TYPE == SPT_PSTAT
155558689Seric 	pst.pst_command = buf;
155658689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
155758689Seric #  else
155868508Seric #   if SPT_TYPE == SPT_PSSTRINGS
155959732Seric 	PS_STRINGS->ps_nargvstr = 1;
156059732Seric 	PS_STRINGS->ps_argvstr = buf;
156159732Seric #   else
156254996Seric 	if (i > LastArgv - Argv[0] - 2)
156325049Seric 	{
156454996Seric 		i = LastArgv - Argv[0] - 2;
156558674Seric 		buf[i] = '\0';
156625049Seric 	}
156758674Seric 	(void) strcpy(Argv[0], buf);
156854997Seric 	p = &Argv[0][i];
156924943Seric 	while (p < LastArgv)
157068508Seric 		*p++ = SPT_PADCHAR;
157168508Seric 	Argv[1] = NULL;
157268508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
157368508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
157468508Seric # endif /* SPT_TYPE != SPT_NONE */
157524943Seric }
157665982Seric 
157768508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
157825698Seric /*
157925698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
158025698Seric **
158125698Seric **	Parameters:
158268508Seric **		sig -- the signal that got us here (unused).
158325698Seric **
158425698Seric **	Returns:
158525698Seric **		none.
158625698Seric **
158725698Seric **	Side Effects:
158825698Seric **		Picks up extant zombies.
158925698Seric */
159025698Seric 
159146928Sbostic void
159268508Seric reapchild(sig)
159368508Seric 	int sig;
159425698Seric {
159564738Seric 	int olderrno = errno;
159663966Seric # ifdef HASWAITPID
159760219Seric 	auto int status;
159860560Seric 	int count;
159960560Seric 	int pid;
160060219Seric 
160160560Seric 	count = 0;
160260560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
160360560Seric 	{
160460560Seric 		if (count++ > 1000)
160560560Seric 		{
160666748Seric #ifdef LOG
160760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
160860560Seric 				pid, status);
160966748Seric #endif
161060560Seric 			break;
161160560Seric 		}
161260560Seric 	}
161360219Seric # else
161425698Seric # ifdef WNOHANG
161525698Seric 	union wait status;
161625698Seric 
161763753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
161825698Seric 		continue;
161956795Seric # else /* WNOHANG */
162025698Seric 	auto int status;
162125698Seric 
162260219Seric 	while (wait(&status) > 0)
162325698Seric 		continue;
162456795Seric # endif /* WNOHANG */
162560219Seric # endif
162664035Seric # ifdef SYS5SIGNALS
162764035Seric 	(void) setsignal(SIGCHLD, reapchild);
162858061Seric # endif
162964738Seric 	errno = olderrno;
163025698Seric }
163155418Seric /*
163255418Seric **  UNSETENV -- remove a variable from the environment
163355418Seric **
163455418Seric **	Not needed on newer systems.
163555418Seric **
163655418Seric **	Parameters:
163755418Seric **		name -- the string name of the environment variable to be
163855418Seric **			deleted from the current environment.
163955418Seric **
164055418Seric **	Returns:
164155418Seric **		none.
164255418Seric **
164355418Seric **	Globals:
164455418Seric **		environ -- a pointer to the current environment.
164555418Seric **
164655418Seric **	Side Effects:
164755418Seric **		Modifies environ.
164855418Seric */
164955418Seric 
165063962Seric #ifndef HASUNSETENV
165155418Seric 
165255418Seric void
165355418Seric unsetenv(name)
165455418Seric 	char *name;
165555418Seric {
165655418Seric 	extern char **environ;
165755418Seric 	register char **pp;
165855418Seric 	int len = strlen(name);
165955418Seric 
166055418Seric 	for (pp = environ; *pp != NULL; pp++)
166155418Seric 	{
166255418Seric 		if (strncmp(name, *pp, len) == 0 &&
166355418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
166455418Seric 			break;
166555418Seric 	}
166655418Seric 
166755418Seric 	for (; *pp != NULL; pp++)
166855418Seric 		*pp = pp[1];
166955418Seric }
167055418Seric 
167163962Seric #endif
167256215Seric /*
167356215Seric **  GETDTABLESIZE -- return number of file descriptors
167456215Seric **
167556215Seric **	Only on non-BSD systems
167656215Seric **
167756215Seric **	Parameters:
167856215Seric **		none
167956215Seric **
168056215Seric **	Returns:
168156215Seric **		size of file descriptor table
168256215Seric **
168356215Seric **	Side Effects:
168456215Seric **		none
168556215Seric */
168656215Seric 
168763787Seric #ifdef SOLARIS
168863787Seric # include <sys/resource.h>
168963787Seric #endif
169056215Seric 
169156215Seric int
169263787Seric getdtsize()
169356215Seric {
169463787Seric #ifdef RLIMIT_NOFILE
169563787Seric 	struct rlimit rl;
169663787Seric 
169763787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
169863787Seric 		return rl.rlim_cur;
169963787Seric #endif
170063787Seric 
170164031Seric # ifdef HASGETDTABLESIZE
170264031Seric 	return getdtablesize();
170364031Seric # else
170464031Seric #  ifdef _SC_OPEN_MAX
170558689Seric 	return sysconf(_SC_OPEN_MAX);
170663787Seric #  else
170756215Seric 	return NOFILE;
170863787Seric #  endif
170958689Seric # endif
171056215Seric }
171157631Seric /*
171257631Seric **  UNAME -- get the UUCP name of this system.
171357631Seric */
171457631Seric 
171557943Seric #ifndef HASUNAME
171657631Seric 
171757631Seric int
171857631Seric uname(name)
171957631Seric 	struct utsname *name;
172057631Seric {
172157631Seric 	FILE *file;
172257631Seric 	char *n;
172357631Seric 
172457631Seric 	name->nodename[0] = '\0';
172557631Seric 
172657661Seric 	/* try /etc/whoami -- one line with the node name */
172757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
172857631Seric 	{
172957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
173057631Seric 		(void) fclose(file);
173157661Seric 		n = strchr(name->nodename, '\n');
173257631Seric 		if (n != NULL)
173357631Seric 			*n = '\0';
173457631Seric 		if (name->nodename[0] != '\0')
173557631Seric 			return (0);
173657631Seric 	}
173757631Seric 
173857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
173957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
174057631Seric 	{
174157631Seric 		char buf[MAXLINE];
174257631Seric 
174357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
174457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
174557631Seric 					NODE_LENGTH, name->nodename) > 0)
174657631Seric 				break;
174757631Seric 		(void) fclose(file);
174857631Seric 		if (name->nodename[0] != '\0')
174957631Seric 			return (0);
175057631Seric 	}
175157631Seric 
175257631Seric #ifdef TRUST_POPEN
175357631Seric 	/*
175457631Seric 	**  Popen is known to have security holes.
175557631Seric 	*/
175657631Seric 
175757661Seric 	/* try uuname -l to return local name */
175857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
175957631Seric 	{
176057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
176157631Seric 		(void) pclose(file);
176257661Seric 		n = strchr(name, '\n');
176357631Seric 		if (n != NULL)
176457631Seric 			*n = '\0';
176557661Seric 		if (name->nodename[0] != '\0')
176657631Seric 			return (0);
176757631Seric 	}
176857631Seric #endif
176957631Seric 
177057631Seric 	return (-1);
177157631Seric }
177257943Seric #endif /* HASUNAME */
177358068Seric /*
177458068Seric **  INITGROUPS -- initialize groups
177558068Seric **
177658068Seric **	Stub implementation for System V style systems
177758068Seric */
177858068Seric 
177958068Seric #ifndef HASINITGROUPS
178058068Seric 
178158068Seric initgroups(name, basegid)
178258068Seric 	char *name;
178358068Seric 	int basegid;
178458068Seric {
178558068Seric 	return 0;
178658068Seric }
178758068Seric 
178858068Seric #endif
178958082Seric /*
179059289Seric **  SETSID -- set session id (for non-POSIX systems)
179159289Seric */
179259289Seric 
179359289Seric #ifndef HASSETSID
179459289Seric 
179563753Seric pid_t
179663753Seric setsid __P ((void))
179759289Seric {
179864155Seric #ifdef TIOCNOTTY
179964155Seric 	int fd;
180064155Seric 
180166743Seric 	fd = open("/dev/tty", O_RDWR, 0);
180264155Seric 	if (fd >= 0)
180364155Seric 	{
180464155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
180564155Seric 		(void) close(fd);
180664155Seric 	}
180764155Seric #endif /* TIOCNOTTY */
180864705Seric # ifdef SYS5SETPGRP
180963753Seric 	return setpgrp();
181063753Seric # else
181164155Seric 	return setpgid(0, getpid());
181259289Seric # endif
181359289Seric }
181459289Seric 
181559289Seric #endif
181659289Seric /*
181766748Seric **  FSYNC -- dummy fsync
181866748Seric */
181966748Seric 
182066748Seric #ifdef NEEDFSYNC
182166748Seric 
182266748Seric fsync(fd)
182366748Seric 	int fd;
182466748Seric {
182566748Seric # ifdef O_SYNC
182666748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
182766748Seric # else
182866748Seric 	/* nothing we can do */
182966748Seric 	return 0;
183066748Seric # endif
183166748Seric }
183266748Seric 
183366748Seric #endif
183466748Seric /*
183564718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
183664718Seric **
183764718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
183868508Seric **	instead of a long.  This patches things.  Only needed on versions
183968508Seric **	prior to 5.4.3.
184064718Seric */
184164718Seric 
184268508Seric #ifdef DGUX_5_4_2
184364718Seric 
184464718Seric #undef inet_addr
184564718Seric 
184664718Seric long
184764718Seric dgux_inet_addr(host)
184864718Seric 	char *host;
184964718Seric {
185064718Seric 	struct in_addr haddr;
185164718Seric 
185264718Seric 	haddr = inet_addr(host);
185364718Seric 	return haddr.s_addr;
185464718Seric }
185564718Seric 
185664718Seric #endif
185764718Seric /*
185863969Seric **  GETOPT -- for old systems or systems with bogus implementations
185963969Seric */
186063969Seric 
186163969Seric #ifdef NEEDGETOPT
186263969Seric 
186363969Seric /*
186463969Seric  * Copyright (c) 1985 Regents of the University of California.
186563969Seric  * All rights reserved.  The Berkeley software License Agreement
186663969Seric  * specifies the terms and conditions for redistribution.
186763969Seric  */
186863969Seric 
186963969Seric 
187063969Seric /*
187163969Seric ** this version hacked to add `atend' flag to allow state machine
187263969Seric ** to reset if invoked by the program to scan args for a 2nd time
187363969Seric */
187463969Seric 
187563969Seric #if defined(LIBC_SCCS) && !defined(lint)
187663969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
187764072Seric #endif /* LIBC_SCCS and not lint */
187863969Seric 
187963969Seric #include <stdio.h>
188063969Seric 
188163969Seric /*
188263969Seric  * get option letter from argument vector
188363969Seric  */
188465352Seric #ifdef _CONVEX_SOURCE
188568508Seric extern int	optind, opterr, optopt;
188668508Seric extern char	*optarg;
188765352Seric #else
188865352Seric int	opterr = 1;		/* if error message should be printed */
188965352Seric int	optind = 1;		/* index into parent argv vector */
189068470Seric int	optopt = 0;		/* character checked for validity */
189168470Seric char	*optarg = NULL;		/* argument associated with option */
189268508Seric #endif
189363969Seric 
189463969Seric #define BADCH	(int)'?'
189563969Seric #define EMSG	""
189663969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
189763969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
189863969Seric 
189963969Seric getopt(nargc,nargv,ostr)
190065160Seric 	int		nargc;
190165160Seric 	char *const	*nargv;
190265160Seric 	const char	*ostr;
190363969Seric {
190463969Seric 	static char	*place = EMSG;	/* option letter processing */
190563969Seric 	static char	atend = 0;
190663969Seric 	register char	*oli;		/* option letter list index */
190763969Seric 
190863969Seric 	if (atend) {
190963969Seric 		atend = 0;
191063969Seric 		place = EMSG;
191163969Seric 	}
191263969Seric 	if(!*place) {			/* update scanning pointer */
191364136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
191463969Seric 			atend++;
191563969Seric 			return(EOF);
191663969Seric 		}
191763969Seric 		if (*place == '-') {	/* found "--" */
191863969Seric 			++optind;
191963969Seric 			atend++;
192063969Seric 			return(EOF);
192163969Seric 		}
192263969Seric 	}				/* option letter okay? */
192364125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
192464136Seric 		if (!*place) ++optind;
192563969Seric 		tell(": illegal option -- ");
192663969Seric 	}
192763969Seric 	if (*++oli != ':') {		/* don't need argument */
192863969Seric 		optarg = NULL;
192963969Seric 		if (!*place) ++optind;
193063969Seric 	}
193163969Seric 	else {				/* need an argument */
193263969Seric 		if (*place) optarg = place;	/* no white space */
193363969Seric 		else if (nargc <= ++optind) {	/* no arg */
193463969Seric 			place = EMSG;
193563969Seric 			tell(": option requires an argument -- ");
193663969Seric 		}
193763969Seric 	 	else optarg = nargv[optind];	/* white space */
193863969Seric 		place = EMSG;
193963969Seric 		++optind;
194063969Seric 	}
194163969Seric 	return(optopt);			/* dump back option letter */
194263969Seric }
194363969Seric 
194463969Seric #endif
194563969Seric /*
194663969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
194763969Seric */
194863969Seric 
194963969Seric #ifdef NEEDVPRINTF
195063969Seric 
195163969Seric #define MAXARG	16
195263969Seric 
195363969Seric vfprintf(fp, fmt, ap)
195463969Seric 	FILE *	fp;
195563969Seric 	char *	fmt;
195663969Seric 	char **	ap;
195763969Seric {
195863969Seric 	char *	bp[MAXARG];
195963969Seric 	int	i = 0;
196063969Seric 
196163969Seric 	while (*ap && i < MAXARG)
196263969Seric 		bp[i++] = *ap++;
196363969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
196463969Seric 			 bp[4], bp[5], bp[6], bp[7],
196563969Seric 			 bp[8], bp[9], bp[10], bp[11],
196663969Seric 			 bp[12], bp[13], bp[14], bp[15]);
196763969Seric }
196863969Seric 
196963969Seric vsprintf(s, fmt, ap)
197063969Seric 	char *	s;
197163969Seric 	char *	fmt;
197263969Seric 	char **	ap;
197363969Seric {
197463969Seric 	char *	bp[MAXARG];
197563969Seric 	int	i = 0;
197663969Seric 
197763969Seric 	while (*ap && i < MAXARG)
197863969Seric 		bp[i++] = *ap++;
197963969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
198063969Seric 			bp[4], bp[5], bp[6], bp[7],
198163969Seric 			bp[8], bp[9], bp[10], bp[11],
198263969Seric 			bp[12], bp[13], bp[14], bp[15]);
198363969Seric }
198463969Seric 
198563969Seric #endif
198663969Seric /*
198765220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
198865208Seric **
198965211Seric **	Parameters:
199065211Seric **		shell -- the user's shell from /etc/passwd
199165211Seric **
199265211Seric **	Returns:
199365211Seric **		TRUE -- if it is ok to use this for unrestricted access.
199465211Seric **		FALSE -- if the shell is restricted.
199565208Seric */
199665208Seric 
199765587Seric #if !HASGETUSERSHELL
199865587Seric 
199965587Seric # ifndef _PATH_SHELLS
200065587Seric #  define _PATH_SHELLS	"/etc/shells"
200165587Seric # endif
200265587Seric 
200365587Seric char	*DefaultUserShells[] =
200465587Seric {
200568508Seric 	"/bin/sh",		/* standard shell */
200665587Seric 	"/usr/bin/sh",
200768508Seric 	"/bin/csh",		/* C shell */
200865587Seric 	"/usr/bin/csh",
200965587Seric #ifdef __hpux
2010*68889Seric # ifdef V4FS
2011*68889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
2012*68889Seric 	"/usr/bin/ksh",		/* Korn shell */
2013*68889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
2014*68889Seric 	"/usr/bin/pam",
2015*68889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
2016*68889Seric 	"/usr/bin/posix/sh",
2017*68889Seric # else
201868508Seric 	"/bin/rsh",		/* restricted Bourne shell */
201968508Seric 	"/bin/ksh",		/* Korn shell */
202068508Seric 	"/bin/rksh",		/* restricted Korn shell */
202165587Seric 	"/bin/pam",
202268508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
202365587Seric 	"/bin/posix/sh",
2024*68889Seric # endif
202565211Seric #endif
202668508Seric #ifdef _AIX3
202768508Seric 	"/bin/ksh",		/* Korn shell */
202868508Seric 	"/usr/bin/ksh",
202968508Seric 	"/bin/tsh",		/* trusted shell */
203068508Seric 	"/usr/bin/tsh",
203168508Seric 	"/bin/bsh",		/* Bourne shell */
203268508Seric 	"/usr/bin/bsh",
203368508Seric #endif
203465587Seric 	NULL
203565587Seric };
203665587Seric 
203765208Seric #endif
203865208Seric 
203965747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
204065747Seric 
204165211Seric bool
204265211Seric usershellok(shell)
204365211Seric 	char *shell;
204465208Seric {
204565211Seric #if HASGETUSERSHELL
204665211Seric 	register char *p;
204765211Seric 	extern char *getusershell();
204865208Seric 
204968457Seric 	if (shell == NULL || shell[0] == '\0')
205068457Seric 		return TRUE;
205168457Seric 
205265211Seric 	setusershell();
205365211Seric 	while ((p = getusershell()) != NULL)
205465747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
205565211Seric 			break;
205665211Seric 	endusershell();
205765211Seric 	return p != NULL;
205865211Seric #else
205965211Seric 	register FILE *shellf;
206065211Seric 	char buf[MAXLINE];
206165208Seric 
206268457Seric 	if (shell == NULL || shell[0] == '\0')
206368457Seric 		return TRUE;
206468457Seric 
206565211Seric 	shellf = fopen(_PATH_SHELLS, "r");
206665211Seric 	if (shellf == NULL)
206765211Seric 	{
206865211Seric 		/* no /etc/shells; see if it is one of the std shells */
206965587Seric 		char **d;
207065587Seric 
207165587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
207265587Seric 		{
207365587Seric 			if (strcmp(shell, *d) == 0)
207465587Seric 				return TRUE;
207565587Seric 		}
207665587Seric 		return FALSE;
207765211Seric 	}
207865208Seric 
207965211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
208065211Seric 	{
208165211Seric 		register char *p, *q;
208265208Seric 
208365211Seric 		p = buf;
208465211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
208565211Seric 			p++;
208665211Seric 		if (*p == '#' || *p == '\0')
208765208Seric 			continue;
208865211Seric 		q = p;
208965211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
209065211Seric 			p++;
209165211Seric 		*p = '\0';
209265747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
209365211Seric 		{
209465211Seric 			fclose(shellf);
209565211Seric 			return TRUE;
209665211Seric 		}
209765208Seric 	}
209865211Seric 	fclose(shellf);
209965211Seric 	return FALSE;
210065211Seric #endif
210165208Seric }
210265208Seric /*
210364311Seric **  FREESPACE -- see how much free space is on the queue filesystem
210458082Seric **
210558082Seric **	Only implemented if you have statfs.
210658082Seric **
210758082Seric **	Parameters:
210864311Seric **		dir -- the directory in question.
210964311Seric **		bsize -- a variable into which the filesystem
211064311Seric **			block size is stored.
211158082Seric **
211258082Seric **	Returns:
211364311Seric **		The number of bytes free on the queue filesystem.
211464311Seric **		-1 if the statfs call fails.
211564311Seric **
211664311Seric **	Side effects:
211764311Seric **		Puts the filesystem block size into bsize.
211858082Seric */
211958082Seric 
212065749Seric /* statfs types */
212165749Seric #define SFS_NONE	0	/* no statfs implementation */
212265749Seric #define SFS_USTAT	1	/* use ustat */
212365749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
212465749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
212565749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
212665749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
212766752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
212865749Seric 
212965749Seric #ifndef SFS_TYPE
213065749Seric # define SFS_TYPE	SFS_NONE
213158157Seric #endif
213258157Seric 
213365749Seric #if SFS_TYPE == SFS_USTAT
213458157Seric # include <ustat.h>
213558157Seric #endif
213665749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
213765749Seric # include <sys/statfs.h>
213858082Seric #endif
213965749Seric #if SFS_TYPE == SFS_VFS
214065749Seric # include <sys/vfs.h>
214165749Seric #endif
214265749Seric #if SFS_TYPE == SFS_MOUNT
214365749Seric # include <sys/mount.h>
214465749Seric #endif
214566752Seric #if SFS_TYPE == SFS_STATVFS
214666752Seric # include <sys/statvfs.h>
214766752Seric #endif
214858082Seric 
214964311Seric long
215064311Seric freespace(dir, bsize)
215164311Seric 	char *dir;
215264311Seric 	long *bsize;
215358082Seric {
215465749Seric #if SFS_TYPE != SFS_NONE
215565749Seric # if SFS_TYPE == SFS_USTAT
215658153Seric 	struct ustat fs;
215758153Seric 	struct stat statbuf;
215858366Seric #  define FSBLOCKSIZE	DEV_BSIZE
215968695Seric #  define SFS_BAVAIL	f_tfree
216058157Seric # else
216158157Seric #  if defined(ultrix)
216258157Seric 	struct fs_data fs;
216368695Seric #   define SFS_BAVAIL	fd_bfreen
216468508Seric #   define FSBLOCKSIZE	1024L
216558153Seric #  else
216666752Seric #   if SFS_TYPE == SFS_STATVFS
216766752Seric 	struct statvfs fs;
216868508Seric #    define FSBLOCKSIZE	fs.f_frsize
216966752Seric #   else
217058082Seric 	struct statfs fs;
217166752Seric #    define FSBLOCKSIZE	fs.f_bsize
217263987Seric #   endif
217358153Seric #  endif
217458133Seric # endif
217568695Seric # ifndef SFS_BAVAIL
217668695Seric #  define SFS_BAVAIL f_bavail
217768508Seric # endif
217858082Seric 
217965749Seric # if SFS_TYPE == SFS_USTAT
218064311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
218158157Seric # else
218265749Seric #  if SFS_TYPE == SFS_4ARGS
218364311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
218458157Seric #  else
218568508Seric #   if SFS_TYPE == SFS_STATVFS
218668508Seric 	if (statvfs(dir, &fs) == 0)
218768508Seric #   else
218868508Seric #    if defined(ultrix)
218968499Seric 	if (statfs(dir, &fs) > 0)
219068508Seric #    else
219164311Seric 	if (statfs(dir, &fs) == 0)
219268508Seric #    endif
219358153Seric #   endif
219458133Seric #  endif
219558133Seric # endif
219658133Seric 	{
219764311Seric 		if (bsize != NULL)
219864311Seric 			*bsize = FSBLOCKSIZE;
219968695Seric 		return (fs.SFS_BAVAIL);
220064311Seric 	}
220164337Seric #endif
220264311Seric 	return (-1);
220364311Seric }
220464311Seric /*
220564311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
220664311Seric **
220764311Seric **	Only implemented if you have statfs.
220864311Seric **
220964311Seric **	Parameters:
221064311Seric **		msize -- the size to check against.  If zero, we don't yet
221164311Seric **		know how big the message will be, so just check for
221264311Seric **		a "reasonable" amount.
221364311Seric **
221464311Seric **	Returns:
221564311Seric **		TRUE if there is enough space.
221664311Seric **		FALSE otherwise.
221764311Seric */
221864311Seric 
221964311Seric bool
222064311Seric enoughspace(msize)
222164311Seric 	long msize;
222264311Seric {
222364311Seric 	long bfree, bsize;
222464311Seric 
222564311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
222664311Seric 	{
222758133Seric 		if (tTd(4, 80))
222864311Seric 			printf("enoughspace: no threshold\n");
222964311Seric 		return TRUE;
223064311Seric 	}
223164311Seric 
223264311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
223364311Seric 	{
223464311Seric 		if (tTd(4, 80))
223558333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
223664311Seric 				bfree, msize);
223758333Seric 
223858333Seric 		/* convert msize to block count */
223964311Seric 		msize = msize / bsize + 1;
224058333Seric 		if (MinBlocksFree >= 0)
224158333Seric 			msize += MinBlocksFree;
224258333Seric 
224364311Seric 		if (bfree < msize)
224458090Seric 		{
224558090Seric #ifdef LOG
224658090Seric 			if (LogLevel > 0)
224764254Seric 				syslog(LOG_ALERT,
224864254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
224968508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
225068508Seric 					bfree,
225168508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
225268508Seric 					msize, QueueDir);
225358090Seric #endif
225458082Seric 			return FALSE;
225558090Seric 		}
225658082Seric 	}
225758082Seric 	else if (tTd(4, 80))
225858333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
225958333Seric 			MinBlocksFree, msize, errstring(errno));
226058082Seric 	return TRUE;
226158082Seric }
226258542Seric /*
226358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
226458542Seric **
226558542Seric **	This looks at an errno value and tells if this is likely to
226658542Seric **	go away if retried later.
226758542Seric **
226858542Seric **	Parameters:
226958542Seric **		err -- the errno code to classify.
227058542Seric **
227158542Seric **	Returns:
227258542Seric **		TRUE if this is probably transient.
227358542Seric **		FALSE otherwise.
227458542Seric */
227558542Seric 
227658542Seric bool
227758542Seric transienterror(err)
227858542Seric 	int err;
227958542Seric {
228058542Seric 	switch (err)
228158542Seric 	{
228258542Seric 	  case EIO:			/* I/O error */
228358542Seric 	  case ENXIO:			/* Device not configured */
228458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
228558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
228658542Seric 	  case ENODEV:			/* Operation not supported by device */
228758542Seric 	  case ENFILE:			/* Too many open files in system */
228858542Seric 	  case EMFILE:			/* Too many open files */
228958542Seric 	  case ENOSPC:			/* No space left on device */
229058542Seric #ifdef ETIMEDOUT
229158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
229258542Seric #endif
229358542Seric #ifdef ESTALE
229458542Seric 	  case ESTALE:			/* Stale NFS file handle */
229558542Seric #endif
229658542Seric #ifdef ENETDOWN
229758542Seric 	  case ENETDOWN:		/* Network is down */
229858542Seric #endif
229958542Seric #ifdef ENETUNREACH
230058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
230158542Seric #endif
230258542Seric #ifdef ENETRESET
230358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
230458542Seric #endif
230558542Seric #ifdef ECONNABORTED
230658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
230758542Seric #endif
230858542Seric #ifdef ECONNRESET
230958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
231058542Seric #endif
231158542Seric #ifdef ENOBUFS
231258542Seric 	  case ENOBUFS:			/* No buffer space available */
231358542Seric #endif
231458542Seric #ifdef ESHUTDOWN
231558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
231658542Seric #endif
231758542Seric #ifdef ECONNREFUSED
231858542Seric 	  case ECONNREFUSED:		/* Connection refused */
231958542Seric #endif
232058542Seric #ifdef EHOSTDOWN
232158542Seric 	  case EHOSTDOWN:		/* Host is down */
232258542Seric #endif
232358542Seric #ifdef EHOSTUNREACH
232458542Seric 	  case EHOSTUNREACH:		/* No route to host */
232558542Seric #endif
232658542Seric #ifdef EDQUOT
232758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
232858542Seric #endif
232958542Seric #ifdef EPROCLIM
233058542Seric 	  case EPROCLIM:		/* Too many processes */
233158542Seric #endif
233258542Seric #ifdef EUSERS
233358542Seric 	  case EUSERS:			/* Too many users */
233458542Seric #endif
233558542Seric #ifdef EDEADLK
233658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
233758542Seric #endif
233858542Seric #ifdef EISCONN
233958542Seric 	  case EISCONN:			/* Socket already connected */
234058542Seric #endif
234158542Seric #ifdef EINPROGRESS
234258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
234358542Seric #endif
234458542Seric #ifdef EALREADY
234558542Seric 	  case EALREADY:		/* Operation already in progress */
234658542Seric #endif
234758542Seric #ifdef EADDRINUSE
234858542Seric 	  case EADDRINUSE:		/* Address already in use */
234958542Seric #endif
235058542Seric #ifdef EADDRNOTAVAIL
235158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
235258542Seric #endif
235366018Seric #ifdef ETXTBSY
235466017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
235566017Seric #endif
235663834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
235758542Seric 	  case ENOSR:			/* Out of streams resources */
235858542Seric #endif
235958542Seric 		return TRUE;
236058542Seric 	}
236158542Seric 
236258542Seric 	/* nope, must be permanent */
236358542Seric 	return FALSE;
236458542Seric }
236558689Seric /*
236664035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
236758689Seric **
236858689Seric **	Parameters:
236958689Seric **		fd -- the file descriptor of the file.
237058689Seric **		filename -- the file name (for error messages).
237164335Seric **		ext -- the filename extension.
237258689Seric **		type -- type of the lock.  Bits can be:
237358689Seric **			LOCK_EX -- exclusive lock.
237458689Seric **			LOCK_NB -- non-blocking.
237558689Seric **
237658689Seric **	Returns:
237758689Seric **		TRUE if the lock was acquired.
237858689Seric **		FALSE otherwise.
237958689Seric */
238058689Seric 
238158689Seric bool
238264335Seric lockfile(fd, filename, ext, type)
238358689Seric 	int fd;
238458689Seric 	char *filename;
238564335Seric 	char *ext;
238658689Seric 	int type;
238758689Seric {
238865830Seric # if !HASFLOCK
238958689Seric 	int action;
239058689Seric 	struct flock lfd;
239164335Seric 
239264335Seric 	if (ext == NULL)
239364335Seric 		ext = "";
239464035Seric 
239563983Seric 	bzero(&lfd, sizeof lfd);
239659447Seric 	if (bitset(LOCK_UN, type))
239759447Seric 		lfd.l_type = F_UNLCK;
239859447Seric 	else if (bitset(LOCK_EX, type))
239958689Seric 		lfd.l_type = F_WRLCK;
240058689Seric 	else
240158689Seric 		lfd.l_type = F_RDLCK;
240258689Seric 
240358689Seric 	if (bitset(LOCK_NB, type))
240458689Seric 		action = F_SETLK;
240558689Seric 	else
240658689Seric 		action = F_SETLKW;
240758689Seric 
240864264Seric 	if (tTd(55, 60))
240964335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
241064335Seric 			filename, ext, action, lfd.l_type);
241164264Seric 
241258689Seric 	if (fcntl(fd, action, &lfd) >= 0)
241364264Seric 	{
241464264Seric 		if (tTd(55, 60))
241564264Seric 			printf("SUCCESS\n");
241658689Seric 		return TRUE;
241764264Seric 	}
241858689Seric 
241964264Seric 	if (tTd(55, 60))
242064264Seric 		printf("(%s) ", errstring(errno));
242164264Seric 
242264136Seric 	/*
242364136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
242464136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
242564136Seric 	**  as type "tmp" (that is, served from swap space), the
242664136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
242764136Seric 	**  Since this is fairly common during testing, we will assume
242864136Seric 	**  that this indicates that the lock is successfully grabbed.
242964136Seric 	*/
243064136Seric 
243164136Seric 	if (errno == EINVAL)
243264264Seric 	{
243364264Seric 		if (tTd(55, 60))
243464264Seric 			printf("SUCCESS\n");
243564136Seric 		return TRUE;
243664264Seric 	}
243764136Seric 
243858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
243964378Seric 	{
244064378Seric 		int omode = -1;
244164378Seric #  ifdef F_GETFL
244264378Seric 		int oerrno = errno;
244364378Seric 
244464378Seric 		(void) fcntl(fd, F_GETFL, &omode);
244564378Seric 		errno = oerrno;
244664378Seric #  endif
244764378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
244864378Seric 			filename, ext, fd, type, omode, geteuid());
244964378Seric 	}
245058689Seric # else
245164335Seric 	if (ext == NULL)
245264335Seric 		ext = "";
245364335Seric 
245464264Seric 	if (tTd(55, 60))
245564335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
245664264Seric 
245758689Seric 	if (flock(fd, type) >= 0)
245864264Seric 	{
245964264Seric 		if (tTd(55, 60))
246064264Seric 			printf("SUCCESS\n");
246158689Seric 		return TRUE;
246264264Seric 	}
246358689Seric 
246464264Seric 	if (tTd(55, 60))
246564264Seric 		printf("(%s) ", errstring(errno));
246664264Seric 
246758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
246864378Seric 	{
246964378Seric 		int omode = -1;
247064378Seric #  ifdef F_GETFL
247164378Seric 		int oerrno = errno;
247264378Seric 
247364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
247464378Seric 		errno = oerrno;
247564378Seric #  endif
247664378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
247764378Seric 			filename, ext, fd, type, omode, geteuid());
247864386Seric 	}
247958689Seric # endif
248064264Seric 	if (tTd(55, 60))
248164264Seric 		printf("FAIL\n");
248258689Seric 	return FALSE;
248358689Seric }
248464556Seric /*
248565948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
248665948Seric **
248765948Seric **	Parameters:
248865948Seric **		fd -- the file descriptor to check.
248965948Seric **
249065948Seric **	Returns:
249165948Seric **		TRUE -- if only root can chown the file to an arbitrary
249265948Seric **			user.
249365948Seric **		FALSE -- if an arbitrary user can give away a file.
249465948Seric */
249565948Seric 
249665948Seric bool
249765948Seric chownsafe(fd)
249865948Seric 	int fd;
249965948Seric {
250065950Seric #ifdef __hpux
250165948Seric 	char *s;
250265948Seric 	int tfd;
250365948Seric 	uid_t o_uid, o_euid;
250465948Seric 	gid_t o_gid, o_egid;
250565948Seric 	bool rval;
250665948Seric 	struct stat stbuf;
250765948Seric 
250865948Seric 	o_uid = getuid();
250965948Seric 	o_euid = geteuid();
251065948Seric 	o_gid = getgid();
251165948Seric 	o_egid = getegid();
251265959Seric 	fstat(fd, &stbuf);
251365948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
251465948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
251565948Seric 	s = tmpnam(NULL);
251665948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
251765974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
251865948Seric 	close(tfd);
251965948Seric 	unlink(s);
252065948Seric 	setreuid(o_uid, o_euid);
252165948Seric 	setresgid(o_gid, o_egid, -1);
252265948Seric 	return rval;
252365948Seric #else
252466088Seric # ifdef _POSIX_CHOWN_RESTRICTED
252566088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
252666088Seric 	return FALSE;
252766088Seric #  else
252866088Seric 	return TRUE;
252966088Seric #  endif
253066088Seric # else
253166088Seric #  ifdef _PC_CHOWN_RESTRICTED
253268508Seric 	int rval;
253368508Seric 
253468508Seric 	/*
253568508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
253668508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
253768508Seric 	**  the call.  This heuristic checks for that.
253868508Seric 	*/
253968508Seric 
254068508Seric 	errno = 0;
254168508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
254268508Seric 	if (errno == 0)
254368508Seric 		return rval > 0;
254468508Seric #  endif
254568508Seric #  ifdef BSD
254668508Seric 	return TRUE;
254768499Seric #  else
254865948Seric 	return FALSE;
254965948Seric #  endif
255065948Seric # endif
255165948Seric #endif
255265948Seric }
255365948Seric /*
255468508Seric **  RESETLIMITS -- reset system controlled resource limits
255568508Seric **
255668508Seric **	This is to avoid denial-of-service attacks
255768508Seric **
255868508Seric **	Parameters:
255968508Seric **		none
256068508Seric **
256168508Seric **	Returns:
256268508Seric **		none
256368508Seric */
256468508Seric 
256568508Seric #if HASSETRLIMIT
256668508Seric # include <sys/resource.h>
256768508Seric #endif
256868508Seric 
256968508Seric void
257068508Seric resetlimits()
257168508Seric {
257268508Seric #if HASSETRLIMIT
257368508Seric 	struct rlimit lim;
257468508Seric 
257568508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
257668508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
257768508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
257868508Seric #else
257968508Seric # if HASULIMIT
258068508Seric 	(void) ulimit(2, 0x3fffff);
258168508Seric # endif
258268508Seric #endif
258368508Seric }
258468508Seric /*
258564556Seric **  GETCFNAME -- return the name of the .cf file.
258664556Seric **
258764556Seric **	Some systems (e.g., NeXT) determine this dynamically.
258864556Seric */
258964556Seric 
259064556Seric char *
259164556Seric getcfname()
259264556Seric {
259368842Seric 	int i;
259468842Seric 	static char cbuf[200];
259568842Seric 
259664556Seric 	if (ConfFile != NULL)
259764556Seric 		return ConfFile;
259865214Seric #ifdef NETINFO
259965214Seric 	{
260065214Seric 		extern char *ni_propval();
260165214Seric 		char *cflocation;
260265214Seric 
260368508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
260468508Seric 					"sendmail.cf", '\0');
260565214Seric 		if (cflocation != NULL)
260665214Seric 			return cflocation;
260765214Seric 	}
260865214Seric #endif
260968842Seric 
261068842Seric 	/*
261168842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
261268842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
261368842Seric 	**
261468842Seric 	**	I suppose it should really try a search path here --
261568842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
261668842Seric 	**	/usr/lib/sendmail.cf, and so forth.
261768842Seric 	*/
261868842Seric 
261968842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
262068842Seric 	i = strlen(cbuf);
262168842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
262268842Seric 	{
262368842Seric 		char *p;
262468842Seric 		extern char Version[];
262568842Seric 
262668842Seric 		strcpy(&cbuf[i - 2], Version);
262768842Seric 		p = strchr(&cbuf[i - 2], '/');
262868842Seric 		if (p != NULL)
262968842Seric 			*p = '\0';
263068842Seric 		p = &cbuf[strlen(cbuf)];
263168842Seric 		do
263268842Seric 		{
263368842Seric 			int fd;
263468842Seric 
263568842Seric 			strcpy(p, ".cf");
263668842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
263768842Seric 			{
263868842Seric 				close(fd);
263968842Seric 				return cbuf;
264068842Seric 			}
264168842Seric 			*p = '\0';
264268842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
264368842Seric 	}
264464556Seric 	return _PATH_SENDMAILCF;
264564556Seric }
264664718Seric /*
264764718Seric **  SETVENDOR -- process vendor code from V configuration line
264864718Seric **
264964718Seric **	Parameters:
265064718Seric **		vendor -- string representation of vendor.
265164718Seric **
265264718Seric **	Returns:
265364718Seric **		TRUE -- if ok.
265464718Seric **		FALSE -- if vendor code could not be processed.
265564926Seric **
265664926Seric **	Side Effects:
265764926Seric **		It is reasonable to set mode flags here to tweak
265864926Seric **		processing in other parts of the code if necessary.
265964926Seric **		For example, if you are a vendor that uses $%y to
266064926Seric **		indicate YP lookups, you could enable that here.
266164718Seric */
266264718Seric 
266364718Seric bool
266464718Seric setvendor(vendor)
266564718Seric 	char *vendor;
266664718Seric {
266764926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
266868508Seric 	{
266968508Seric 		VendorCode = VENDOR_BERKELEY;
267064926Seric 		return TRUE;
267168508Seric 	}
267264926Seric 
267364926Seric 	/* add vendor extensions here */
267464926Seric 
267568508Seric #ifdef SUN_EXTENSIONS
267668508Seric 	if (strcasecmp(vendor, "Sun") == 0)
267768508Seric 	{
267868508Seric 		VendorCode = VENDOR_SUN;
267968508Seric 		return TRUE;
268068508Seric 	}
268168508Seric #endif
268268508Seric 
268364926Seric 	return FALSE;
268464718Seric }
268564816Seric /*
268664816Seric **  STRTOL -- convert string to long integer
268764816Seric **
268864816Seric **	For systems that don't have it in the C library.
268966161Seric **
269066161Seric **	This is taken verbatim from the 4.4-Lite C library.
269164816Seric */
269264816Seric 
269364816Seric #ifdef NEEDSTRTOL
269464816Seric 
269566161Seric #if defined(LIBC_SCCS) && !defined(lint)
269666161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
269766161Seric #endif /* LIBC_SCCS and not lint */
269866161Seric 
269966161Seric #include <limits.h>
270066161Seric 
270166161Seric /*
270266161Seric  * Convert a string to a long integer.
270366161Seric  *
270466161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
270566161Seric  * alphabets and digits are each contiguous.
270666161Seric  */
270766161Seric 
270864816Seric long
270966161Seric strtol(nptr, endptr, base)
271066161Seric 	const char *nptr;
271166161Seric 	char **endptr;
271266161Seric 	register int base;
271364816Seric {
271466161Seric 	register const char *s = nptr;
271566161Seric 	register unsigned long acc;
271666161Seric 	register int c;
271766161Seric 	register unsigned long cutoff;
271866161Seric 	register int neg = 0, any, cutlim;
271964816Seric 
272066161Seric 	/*
272166161Seric 	 * Skip white space and pick up leading +/- sign if any.
272266161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
272366161Seric 	 * assume decimal; if base is already 16, allow 0x.
272466161Seric 	 */
272566161Seric 	do {
272666161Seric 		c = *s++;
272766161Seric 	} while (isspace(c));
272866161Seric 	if (c == '-') {
272966161Seric 		neg = 1;
273066161Seric 		c = *s++;
273166161Seric 	} else if (c == '+')
273266161Seric 		c = *s++;
273366161Seric 	if ((base == 0 || base == 16) &&
273466161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
273566161Seric 		c = s[1];
273666161Seric 		s += 2;
273766161Seric 		base = 16;
273866161Seric 	}
273966161Seric 	if (base == 0)
274066161Seric 		base = c == '0' ? 8 : 10;
274164816Seric 
274266161Seric 	/*
274366161Seric 	 * Compute the cutoff value between legal numbers and illegal
274466161Seric 	 * numbers.  That is the largest legal value, divided by the
274566161Seric 	 * base.  An input number that is greater than this value, if
274666161Seric 	 * followed by a legal input character, is too big.  One that
274766161Seric 	 * is equal to this value may be valid or not; the limit
274866161Seric 	 * between valid and invalid numbers is then based on the last
274966161Seric 	 * digit.  For instance, if the range for longs is
275066161Seric 	 * [-2147483648..2147483647] and the input base is 10,
275166161Seric 	 * cutoff will be set to 214748364 and cutlim to either
275266161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
275366161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
275466161Seric 	 * the number is too big, and we will return a range error.
275566161Seric 	 *
275666161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
275766161Seric 	 * overflow.
275866161Seric 	 */
275966161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
276066161Seric 	cutlim = cutoff % (unsigned long)base;
276166161Seric 	cutoff /= (unsigned long)base;
276266161Seric 	for (acc = 0, any = 0;; c = *s++) {
276366161Seric 		if (isdigit(c))
276466161Seric 			c -= '0';
276566161Seric 		else if (isalpha(c))
276666161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
276766161Seric 		else
276866161Seric 			break;
276966161Seric 		if (c >= base)
277066161Seric 			break;
277166161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
277266161Seric 			any = -1;
277366161Seric 		else {
277466161Seric 			any = 1;
277566161Seric 			acc *= base;
277666161Seric 			acc += c;
277764816Seric 		}
277864816Seric 	}
277966161Seric 	if (any < 0) {
278066161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
278166161Seric 		errno = ERANGE;
278266161Seric 	} else if (neg)
278366161Seric 		acc = -acc;
278466161Seric 	if (endptr != 0)
278566161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
278666161Seric 	return (acc);
278764816Seric }
278864816Seric 
278964816Seric #endif
279064841Seric /*
279168693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
279264841Seric **
279368693Seric **	Some operating systems have wierd problems with the gethostbyXXX
279468693Seric **	routines.  For example, Solaris versions at least through 2.3
279568693Seric **	don't properly deliver a canonical h_name field.  This tries to
279668693Seric **	work around these problems.
279764841Seric */
279864841Seric 
279964841Seric struct hostent *
280068693Seric sm_gethostbyname(name)
280168707Seric 	char *name;
280264841Seric {
280368693Seric #if defined(SOLARIS) && SOLARIS < 204
280468693Seric 	extern int h_errno;
280568693Seric 
280668508Seric # if SOLARIS == 203
280764841Seric 	static struct hostent hp;
280864841Seric 	static char buf[1000];
280964841Seric 	extern struct hostent *_switch_gethostbyname_r();
281064841Seric 
281164841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
281264841Seric # else
281365009Seric 	extern struct hostent *__switch_gethostbyname();
281465009Seric 
281564841Seric 	return __switch_gethostbyname(name);
281664841Seric # endif
281768693Seric #else
281868693Seric 	return gethostbyname(name);
281968693Seric #endif
282064841Seric }
282164841Seric 
282264841Seric struct hostent *
282368693Seric sm_gethostbyaddr(addr, len, type)
282468707Seric 	char *addr;
282564841Seric 	int len;
282664841Seric 	int type;
282764841Seric {
282868693Seric #if defined(SOLARIS) && SOLARIS < 204
282968693Seric 	extern int h_errno;
283068693Seric 
283168508Seric # if SOLARIS == 203
283264841Seric 	static struct hostent hp;
283364841Seric 	static char buf[1000];
283464841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
283564841Seric 
283664841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
283764841Seric # else
283865009Seric 	extern struct hostent *__switch_gethostbyaddr();
283965009Seric 
284064841Seric 	return __switch_gethostbyaddr(addr, len, type);
284164841Seric # endif
284268693Seric #else
284368693Seric 	return gethostbyaddr(addr, len, type);
284468693Seric #endif
284564841Seric }
284668693Seric /*
284768693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
284868693Seric */
284964841Seric 
285068693Seric struct passwd *
285168693Seric sm_getpwnam(user)
285268704Seric 	char *user;
285368693Seric {
285468693Seric 	return getpwnam(user);
285568693Seric }
285668693Seric 
285768693Seric struct passwd *
285868693Seric sm_getpwuid(uid)
285968693Seric 	uid_t uid;
286068693Seric {
286168693Seric 	return getpwuid(uid);
286268693Seric }
286365214Seric /*
286465214Seric **  NI_PROPVAL -- netinfo property value lookup routine
286565214Seric **
286665214Seric **	Parameters:
286768508Seric **		keydir -- the Netinfo directory name in which to search
286868508Seric **			for the key.
286968508Seric **		keyprop -- the name of the property in which to find the
287068508Seric **			property we are interested.  Defaults to "name".
287168508Seric **		keyval -- the value for which we are really searching.
287268508Seric **		valprop -- the property name for the value in which we
287368508Seric **			are interested.
287468508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
287568508Seric **			we should return a string separated by this
287668508Seric **			character.
287765214Seric **
287865214Seric **	Returns:
287965214Seric **		NULL -- if:
288065214Seric **			1. the directory is not found
288165214Seric **			2. the property name is not found
288265214Seric **			3. the property contains multiple values
288365214Seric **			4. some error occured
288465214Seric **		else -- the location of the config file.
288565214Seric **
288668508Seric **	Example:
288768508Seric **		To search for an alias value, use:
288868508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
288968508Seric **
289065214Seric **	Notes:
289165214Seric **      	Caller should free the return value of ni_proval
289265214Seric */
289365214Seric 
289465214Seric #ifdef NETINFO
289565214Seric 
289665214Seric # include <netinfo/ni.h>
289765214Seric 
289865214Seric # define LOCAL_NETINFO_DOMAIN    "."
289965214Seric # define PARENT_NETINFO_DOMAIN   ".."
290065214Seric # define MAX_NI_LEVELS           256
290165214Seric 
290265214Seric char *
290368508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
290468508Seric 	char *keydir;
290568508Seric 	char *keyprop;
290668508Seric 	char *keyval;
290768508Seric 	char *valprop;
290868508Seric 	char sepchar;
290965214Seric {
291065820Seric 	char *propval = NULL;
291165214Seric 	int i;
291268508Seric 	int j, alen;
291365214Seric 	void *ni = NULL;
291465214Seric 	void *lastni = NULL;
291565214Seric 	ni_status nis;
291665214Seric 	ni_id nid;
291765214Seric 	ni_namelist ninl;
291868508Seric 	register char *p;
291968508Seric 	char keybuf[1024];
292065214Seric 
292165214Seric 	/*
292268508Seric 	**  Create the full key from the two parts.
292368508Seric 	**
292468508Seric 	**	Note that directory can end with, e.g., "name=" to specify
292568508Seric 	**	an alternate search property.
292668508Seric 	*/
292768508Seric 
292868508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
292968508Seric 	if (keyprop != NULL)
293068508Seric 		i += strlen(keyprop) + 1;
293168508Seric 	if (i > sizeof keybuf)
293268508Seric 		return NULL;
293368508Seric 	strcpy(keybuf, keydir);
293468508Seric 	strcat(keybuf, "/");
293568508Seric 	if (keyprop != NULL)
293668508Seric 	{
293768508Seric 		strcat(keybuf, keyprop);
293868508Seric 		strcat(keybuf, "=");
293968508Seric 	}
294068508Seric 	strcat(keybuf, keyval);
294168508Seric 
294268508Seric 	/*
294365214Seric 	**  If the passed directory and property name are found
294465214Seric 	**  in one of netinfo domains we need to search (starting
294565214Seric 	**  from the local domain moving all the way back to the
294665214Seric 	**  root domain) set propval to the property's value
294765214Seric 	**  and return it.
294865214Seric 	*/
294965214Seric 
295065214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
295165214Seric 	{
295265214Seric 		if (i == 0)
295365214Seric 		{
295465214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
295565214Seric 		}
295665214Seric 		else
295765214Seric 		{
295865214Seric 			if (lastni != NULL)
295965214Seric 				ni_free(lastni);
296065214Seric 			lastni = ni;
296165214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
296265214Seric 		}
296365214Seric 
296465214Seric 		/*
296565214Seric 		**  Don't bother if we didn't get a handle on a
296665214Seric 		**  proper domain.  This is not necessarily an error.
296765214Seric 		**  We would get a positive ni_status if, for instance
296865214Seric 		**  we never found the directory or property and tried
296965214Seric 		**  to open the parent of the root domain!
297065214Seric 		*/
297165214Seric 
297265214Seric 		if (nis != 0)
297365214Seric 			break;
297465214Seric 
297565214Seric 		/*
297665214Seric 		**  Find the path to the server information.
297765214Seric 		*/
297865214Seric 
297968508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
298065214Seric 			continue;
298165214Seric 
298265214Seric 		/*
298368508Seric 		**  Find associated value information.
298465214Seric 		*/
298565214Seric 
298668508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
298765214Seric 			continue;
298865214Seric 
298965214Seric 		/*
299068508Seric 		**  See if we have an acceptable number of values.
299165214Seric 		*/
299265214Seric 
299368508Seric 		if (ninl.ni_namelist_len <= 0)
299468508Seric 			continue;
299568508Seric 
299668508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
299765214Seric 		{
299868508Seric 			ni_namelist_free(&ninl);
299968508Seric 			continue;
300065214Seric 		}
300168508Seric 
300268508Seric 		/*
300368508Seric 		**  Calculate number of bytes needed and build result
300468508Seric 		*/
300568508Seric 
300668508Seric 		alen = 1;
300768508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
300868508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
300968508Seric 		propval = p = xalloc(alen);
301068508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
301168508Seric 		{
301268508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
301368508Seric 			p += strlen(p);
301468508Seric 			*p++ = sepchar;
301568508Seric 		}
301668508Seric 		*--p = '\0';
301768508Seric 
301868508Seric 		ni_namelist_free(&ninl);
301965214Seric 	}
302065214Seric 
302165214Seric 	/*
302265214Seric 	**  Clean up.
302365214Seric 	*/
302465214Seric 
302565214Seric 	if (ni != NULL)
302665214Seric 		ni_free(ni);
302765214Seric 	if (lastni != NULL && ni != lastni)
302865214Seric 		ni_free(lastni);
302965214Seric 
303065214Seric 	return propval;
303165214Seric }
303265214Seric 
303365214Seric #endif /* NETINFO */
303465504Seric /*
303565504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
303665504Seric **
303765504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
303865504Seric **	syslog succeeds during interrupt handlers.
303965504Seric */
304065504Seric 
304165504Seric #ifdef __hpux
304265504Seric 
304365504Seric # define MAXSYSLOGTRIES	100
304465504Seric # undef syslog
304565504Seric 
304665504Seric # ifdef __STDC__
304765504Seric hard_syslog(int pri, char *msg, ...)
304865504Seric # else
304965504Seric hard_syslog(pri, msg, va_alist)
305065504Seric 	int pri;
305165504Seric 	char *msg;
305265504Seric 	va_dcl
305365504Seric # endif
305465504Seric {
305565504Seric 	int i;
305665504Seric 	char buf[SYSLOG_BUFSIZE * 2];
305765504Seric 	VA_LOCAL_DECL;
305865504Seric 
305965504Seric 	VA_START(msg);
306065504Seric 	vsprintf(buf, msg, ap);
306165504Seric 	VA_END;
306265504Seric 
306365504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
306465504Seric 		continue;
306565504Seric }
306665504Seric 
306765504Seric #endif
306868696Seric /*
306968696Seric **  LOCAL_HOSTNAME_LENGTH
307068696Seric **
307168696Seric **	This is required to get sendmail to compile against BIND 4.9.x
307268696Seric **	on Ultrix.
307368696Seric */
307468696Seric 
307568696Seric #if defined(ultrix) && NAMED_BIND
307668696Seric 
307768696Seric # include <resolv.h>
307868696Seric # if __RES >= 19931104
307968696Seric 
308068696Seric int
308168696Seric local_hostname_length(hostname)
308268696Seric 	char *hostname;
308368696Seric {
308468696Seric 	int len_host, len_domain;
308568696Seric 
308668696Seric 	if (!*_res.defdname)
308768696Seric 		res_init();
308868696Seric 	len_host = strlen(hostname);
308968696Seric 	len_domain = strlen(_res.defdname);
309068696Seric 	if (len_host > len_domain &&
309168696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
309268696Seric 	    hostname[len_host - len_domain - 1] == '.')
309368696Seric 		return len_host - len_domain - 1;
309468696Seric 	else
309568696Seric 		return 0;
309668696Seric }
309768696Seric 
309868696Seric # endif
309968696Seric #endif
310068741Seric /*
310168741Seric **  Compile-Time options
310268741Seric */
310368741Seric 
310468741Seric char	*CompileOptions[] =
310568741Seric {
310668741Seric #if HESIOD
310768741Seric 	"HESIOD",
310868741Seric #endif
310968741Seric #if LOG
311068741Seric 	"LOG",
311168741Seric #endif
311268741Seric #if MATCHGECOS
311368741Seric 	"MATCHGECOS",
311468741Seric #endif
311568741Seric #if NAMED_BIND
311668741Seric 	"NAMED_BIND",
311768741Seric #endif
311868741Seric #if NDBM
311968741Seric 	"NDBM",
312068741Seric #endif
312168741Seric #if NETINET
312268741Seric 	"NETINET",
312368741Seric #endif
312468741Seric #if NETINFO
312568741Seric 	"NETINFO",
312668741Seric #endif
312768741Seric #if NETISO
312868741Seric 	"NETISO",
312968741Seric #endif
313068741Seric #if NETNS
313168741Seric 	"NETNS",
313268741Seric #endif
313368741Seric #if NETUNIX
313468741Seric 	"NETUNIX",
313568741Seric #endif
313668741Seric #if NETX25
313768741Seric 	"NETX25",
313868741Seric #endif
313968741Seric #if NEWDB
314068741Seric 	"NEWDB",
314168741Seric #endif
314268741Seric #if NIS
314368741Seric 	"NIS",
314468741Seric #endif
314568741Seric #if SCANF
314668741Seric 	"SCANF",
314768741Seric #endif
314868741Seric #if SUID_ROOT_FILES_OK
314968741Seric 	"SUID_ROOT_FILES_OK",
315068741Seric #endif
315168741Seric #if USERDB
315268741Seric 	"USERDB",
315368741Seric #endif
315468741Seric #if XDEBUG
315568741Seric 	"XDEBUG",
315668741Seric #endif
315768741Seric #if XLA
315868741Seric 	"XLA",
315968741Seric #endif
316068741Seric 	NULL
316168741Seric };
3162