xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69954)
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*69954Seric static char sccsid[] = "@(#)conf.c	8.203 (Berkeley) 06/20/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
setdefaults(e)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 */
18769480Seric #if MIME8TO7
18868508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18969480Seric #else
19069480Seric 	MimeMode = MM_PASS8BIT;
19169480Seric #endif
19268508Seric 	for (i = 0; i < MAXTOCLASS; i++)
19368508Seric 	{
19468508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19568508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19668508Seric 	}
19768508Seric 	ServiceSwitchFile = "/etc/service.switch";
19869401Seric 	HostsFile = _PATH_HOSTS;
19940973Sbostic 	setdefuser();
20053654Seric 	setupmaps();
20157402Seric 	setupmailers();
20224943Seric }
203294Seric 
20440973Sbostic 
2054326Seric /*
20640973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20740973Sbostic */
20840973Sbostic 
20968508Seric void
setdefuser()21040973Sbostic setdefuser()
21140973Sbostic {
21240973Sbostic 	struct passwd *defpwent;
21357386Seric 	static char defuserbuf[40];
21440973Sbostic 
21557386Seric 	DefUser = defuserbuf;
21668693Seric 	if ((defpwent = sm_getpwuid(DefUid)) != NULL)
21757386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21840973Sbostic 	else
21957386Seric 		strcpy(defuserbuf, "nobody");
22040973Sbostic }
22153654Seric /*
22256836Seric **  HOST_MAP_INIT -- initialize host class structures
22356836Seric */
22456836Seric 
22568508Seric bool	host_map_init __P((MAP *map, char *args));
22668508Seric 
22756836Seric bool
host_map_init(map,args)22860219Seric host_map_init(map, args)
22956836Seric 	MAP *map;
23056836Seric 	char *args;
23156836Seric {
23256836Seric 	register char *p = args;
23356836Seric 
23456836Seric 	for (;;)
23556836Seric 	{
23658050Seric 		while (isascii(*p) && isspace(*p))
23756836Seric 			p++;
23856836Seric 		if (*p != '-')
23956836Seric 			break;
24056836Seric 		switch (*++p)
24156836Seric 		{
24256836Seric 		  case 'a':
24356836Seric 			map->map_app = ++p;
24456836Seric 			break;
24556836Seric 		}
24658050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24756836Seric 			p++;
24856836Seric 		if (*p != '\0')
24956836Seric 			*p++ = '\0';
25056836Seric 	}
25156836Seric 	if (map->map_app != NULL)
25256836Seric 		map->map_app = newstr(map->map_app);
25356836Seric 	return TRUE;
25456836Seric }
25557402Seric /*
25657402Seric **  SETUPMAILERS -- initialize default mailers
25757402Seric */
25856836Seric 
25968508Seric void
setupmailers()26057402Seric setupmailers()
26157402Seric {
26257402Seric 	char buf[100];
26368508Seric 	extern void makemailer();
26457402Seric 
26569793Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=DNS/RFC822/X-Unix, A=sh -c \201u");
26657403Seric 	makemailer(buf);
26757403Seric 
26869721Seric 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=DNS/RFC822/X-Unix, A=FILE");
26957402Seric 	makemailer(buf);
27057402Seric 
27157402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
27257402Seric 	makemailer(buf);
27357402Seric }
27456836Seric /*
27560207Seric **  SETUPMAPS -- set up map classes
27660207Seric */
27760207Seric 
27860207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27960207Seric 	{ \
28060207Seric 		extern bool parse __P((MAP *, char *)); \
28160207Seric 		extern bool open __P((MAP *, int)); \
28260207Seric 		extern void close __P((MAP *)); \
28360207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
28460207Seric 		extern void store __P((MAP *, char *, char *)); \
28560207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28660207Seric 		s->s_mapclass.map_cname = name; \
28760207Seric 		s->s_mapclass.map_ext = ext; \
28860207Seric 		s->s_mapclass.map_cflags = flags; \
28960207Seric 		s->s_mapclass.map_parse = parse; \
29060207Seric 		s->s_mapclass.map_open = open; \
29160207Seric 		s->s_mapclass.map_close = close; \
29260207Seric 		s->s_mapclass.map_lookup = lookup; \
29360207Seric 		s->s_mapclass.map_store = store; \
29460207Seric 	}
29560207Seric 
29668508Seric void
setupmaps()29760207Seric setupmaps()
29860207Seric {
29960207Seric 	register STAB *s;
30060207Seric 
30160207Seric #ifdef NEWDB
30260207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, hash_map_open, db_map_close,
30460207Seric 		db_map_lookup, db_map_store);
30568508Seric 
30660207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30760207Seric 		map_parseargs, bt_map_open, db_map_close,
30860207Seric 		db_map_lookup, db_map_store);
30960207Seric #endif
31060207Seric 
31160207Seric #ifdef NDBM
31260207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
31360207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
31460207Seric 		ndbm_map_lookup, ndbm_map_store);
31560207Seric #endif
31660207Seric 
31760207Seric #ifdef NIS
31860207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31968508Seric 		map_parseargs, nis_map_open, null_map_close,
32068508Seric 		nis_map_lookup, null_map_store);
32160207Seric #endif
32260207Seric 
32368508Seric #ifdef NISPLUS
32468508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32568508Seric 		map_parseargs, nisplus_map_open, null_map_close,
32668508Seric 		nisplus_map_lookup, null_map_store);
32768508Seric #endif
32868508Seric 
32968508Seric #ifdef HESIOD
33068508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
33168508Seric 		map_parseargs, null_map_open, null_map_close,
33268508Seric 		hes_map_lookup, null_map_store);
33368508Seric #endif
33468508Seric 
33569881Seric #if NETINFO
33668508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33768508Seric 		map_parseargs, ni_map_open, null_map_close,
33868508Seric 		ni_map_lookup, null_map_store);
33968508Seric #endif
34068508Seric 
34168508Seric #if 0
34268508Seric 	MAPDEF("dns", NULL, 0,
34368508Seric 		dns_map_init, null_map_open, null_map_close,
34468508Seric 		dns_map_lookup, null_map_store);
34568508Seric #endif
34668508Seric 
34768508Seric #if NAMED_BIND
34868508Seric 	/* best MX DNS lookup */
34968508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
35068508Seric 		map_parseargs, null_map_open, null_map_close,
35168508Seric 		bestmx_map_lookup, null_map_store);
35268508Seric #endif
35368508Seric 
35468508Seric 	MAPDEF("host", NULL, 0,
35568508Seric 		host_map_init, null_map_open, null_map_close,
35668508Seric 		host_map_lookup, null_map_store);
35768508Seric 
35868508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35968508Seric 		map_parseargs, text_map_open, null_map_close,
36068508Seric 		text_map_lookup, null_map_store);
36168508Seric 
36260207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
36368508Seric 		map_parseargs, stab_map_open, null_map_close,
36460207Seric 		stab_map_lookup, stab_map_store);
36560207Seric 
36660207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36760207Seric 		map_parseargs, impl_map_open, impl_map_close,
36860207Seric 		impl_map_lookup, impl_map_store);
36960207Seric 
37068508Seric 	/* access to system passwd file */
37168508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
37268508Seric 		map_parseargs, user_map_open, null_map_close,
37368508Seric 		user_map_lookup, null_map_store);
37460207Seric 
37560207Seric 	/* dequote map */
37660207Seric 	MAPDEF("dequote", NULL, 0,
37760207Seric 		dequote_init, null_map_open, null_map_close,
37860207Seric 		dequote_map, null_map_store);
37960207Seric 
38069717Seric #if USERDB
38160207Seric 	/* user database */
38268521Seric 	MAPDEF("userdb", ".db", 0,
38368521Seric 		map_parseargs, null_map_open, null_map_close,
38460207Seric 		udb_map_lookup, null_map_store);
38560207Seric #endif
38668508Seric 
38769453Seric 	/* arbitrary programs */
38869499Seric 	MAPDEF("program", NULL, MCF_ALIASOK,
38969453Seric 		map_parseargs, null_map_open, null_map_close,
39069453Seric 		prog_map_lookup, null_map_store);
39169453Seric 
39268508Seric 	/* sequenced maps */
39368508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
39468508Seric 		seq_map_parse, null_map_open, null_map_close,
39568508Seric 		seq_map_lookup, seq_map_store);
39668508Seric 
39768508Seric 	/* switched interface to sequenced maps */
39868508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
39968508Seric 		map_parseargs, switch_map_open, null_map_close,
40068508Seric 		seq_map_lookup, seq_map_store);
40160207Seric }
40260207Seric 
40360207Seric #undef MAPDEF
40460207Seric /*
40568508Seric **  INITHOSTMAPS -- initial host-dependent maps
40668508Seric **
40768508Seric **	This should act as an interface to any local service switch
40868508Seric **	provided by the host operating system.
40968508Seric **
41068508Seric **	Parameters:
41168508Seric **		none
41268508Seric **
41368508Seric **	Returns:
41468508Seric **		none
41568508Seric **
41668508Seric **	Side Effects:
41768508Seric **		Should define maps "host" and "users" as necessary
41868508Seric **		for this OS.  If they are not defined, they will get
41968508Seric **		a default value later.  It should check to make sure
42068508Seric **		they are not defined first, since it's possible that
42168508Seric **		the config file has provided an override.
42268508Seric */
42368508Seric 
42468508Seric void
inithostmaps()42568508Seric inithostmaps()
42668508Seric {
42768508Seric 	register int i;
42868508Seric 	int nmaps;
42968508Seric 	char *maptype[MAXMAPSTACK];
43068508Seric 	short mapreturn[MAXMAPACTIONS];
43168508Seric 	char buf[MAXLINE];
43268508Seric 
43368508Seric 	/*
43468508Seric 	**  Set up default hosts maps.
43568508Seric 	*/
43668508Seric 
43768508Seric #if 0
43868508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
43968508Seric 	for (i = 0; i < nmaps; i++)
44068508Seric 	{
44168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
44268508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
44368508Seric 		{
44468508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
44569774Seric 			(void) makemapentry(buf);
44668508Seric 		}
44768508Seric #if NAMED_BIND
44868508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
44968508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
45068508Seric 		{
45168508Seric 			strcpy(buf, "hosts.dns dns A");
45269774Seric 			(void) makemapentry(buf);
45368508Seric 		}
45468508Seric #endif
45568508Seric #ifdef NISPLUS
45668508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
45768508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
45868508Seric 		{
45968508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
46069774Seric 			(void) makemapentry(buf);
46168508Seric 		}
46268508Seric #endif
46368508Seric #ifdef NIS
46468508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
46568508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
46668508Seric 		{
46768508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
46869774Seric 			(void) makemapentry(buf);
46968508Seric 		}
47068508Seric #endif
47168508Seric 	}
47268508Seric #endif
47368508Seric 
47468508Seric 	/*
47568508Seric 	**  Make sure we have a host map.
47668508Seric 	*/
47768508Seric 
47868508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
47968508Seric 	{
48068508Seric 		/* user didn't initialize: set up host map */
48168508Seric 		strcpy(buf, "host host");
48268508Seric #if NAMED_BIND
48368508Seric 		if (ConfigLevel >= 2)
48468508Seric 			strcat(buf, " -a.");
48568508Seric #endif
48669774Seric 		(void) makemapentry(buf);
48768508Seric 	}
48868508Seric 
48968508Seric 	/*
49068508Seric 	**  Set up default aliases maps
49168508Seric 	*/
49268508Seric 
49368508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
49468508Seric 	for (i = 0; i < nmaps; i++)
49568508Seric 	{
49668508Seric 		if (strcmp(maptype[i], "files") == 0 &&
49768508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
49868508Seric 		{
49968508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
50069774Seric 			(void) makemapentry(buf);
50168508Seric 		}
50268508Seric #ifdef NISPLUS
50368508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
50468508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
50568508Seric 		{
50668508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
50769774Seric 			(void) makemapentry(buf);
50868508Seric 		}
50968508Seric #endif
51068508Seric #ifdef NIS
51168508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
51268508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
51368508Seric 		{
51468508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
51569774Seric 			(void) makemapentry(buf);
51668508Seric 		}
51768508Seric #endif
51869774Seric #ifdef HESIOD
51969774Seric 		else if (strcmp(maptype[i], "hesiod") == 0 &&
52069774Seric 		    stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL)
52169774Seric 		{
52269774Seric 			strcpy(buf, "aliases.hesiod hesiod aliases");
52369774Seric 			(void) makemapentry(buf);
52469774Seric 		}
52569774Seric #endif
52668508Seric 	}
52769774Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
52868508Seric 	{
52968508Seric 		strcpy(buf, "aliases switch aliases");
53069774Seric 		(void) makemapentry(buf);
53168508Seric 	}
53268508Seric 
53368508Seric #if 0		/* "user" map class is a better choice */
53468508Seric 	/*
53568508Seric 	**  Set up default users maps.
53668508Seric 	*/
53768508Seric 
53868508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
53968508Seric 	for (i = 0; i < nmaps; i++)
54068508Seric 	{
54168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
54268508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
54368508Seric 		{
54468508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
54569774Seric 			(void) makemapentry(buf);
54668508Seric 		}
54768508Seric #ifdef NISPLUS
54868508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
54968508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
55068508Seric 		{
55168508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
55269774Seric 			(void) makemapentry(buf);
55368508Seric 		}
55468508Seric #endif
55568508Seric #ifdef NIS
55668508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
55768508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
55868508Seric 		{
55968508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
56069774Seric 			(void) makemapentry(buf);
56168508Seric 		}
56268508Seric #endif
56368508Seric #ifdef HESIOD
56468508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
56568508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
56668508Seric 		{
56768508Seric 			strcpy(buf, "users.hesiod hesiod");
56869774Seric 			(void) makemapentry(buf);
56968508Seric 		}
57068508Seric #endif
57168508Seric 	}
57268508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
57368508Seric 	{
57468508Seric 		strcpy(buf, "users switch -m passwd");
57569774Seric 		(void) makemapentry(buf);
57668508Seric 	}
57768508Seric #endif
57868508Seric }
57968508Seric /*
58068508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
58168508Seric **
58268508Seric **	This is the system-dependent interface to the service switch.
58368508Seric **
58468508Seric **	Parameters:
58568508Seric **		service -- the name of the service of interest.
58668508Seric **		maptype -- an out-array of strings containing the types
58768508Seric **			of access to use for this service.  There can
58868508Seric **			be at most MAXMAPSTACK types for a single service.
58968508Seric **		mapreturn -- an out-array of return information bitmaps
59068508Seric **			for the map.
59168508Seric **
59268508Seric **	Returns:
59368508Seric **		The number of map types filled in, or -1 for failure.
59468508Seric */
59568508Seric 
59668508Seric #ifdef SOLARIS
59768508Seric # include <nsswitch.h>
59868508Seric #endif
59968508Seric 
60069815Seric #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
60169815Seric # define _USE_DEC_SVC_CONF_
60269815Seric #endif
60369815Seric 
60469815Seric #ifdef _USE_DEC_SVC_CONF_
60568508Seric # include <sys/svcinfo.h>
60668508Seric #endif
60768508Seric 
60868508Seric int
switch_map_find(service,maptype,mapreturn)60968508Seric switch_map_find(service, maptype, mapreturn)
61068508Seric 	char *service;
61168508Seric 	char *maptype[MAXMAPSTACK];
61268508Seric 	short mapreturn[MAXMAPACTIONS];
61368508Seric {
61468508Seric 	register FILE *fp;
61568508Seric 	int svcno;
61668508Seric 	static char buf[MAXLINE];
61768508Seric 
61868508Seric #ifdef SOLARIS
61968508Seric 	struct __nsw_switchconfig *nsw_conf;
62068508Seric 	enum __nsw_parse_err pserr;
62168508Seric 	struct __nsw_lookup *lk;
62268508Seric 	int nsw_rc;
62368508Seric 	static struct __nsw_lookup lkp0 =
62468508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
62568508Seric 	static struct __nsw_switchconfig lkp_default =
62668508Seric 		{ 0, "sendmail", 3, &lkp0 };
62768508Seric 
62868508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
62968508Seric 		lk = lkp_default.lookups;
63068508Seric 	else
63168508Seric 		lk = nsw_conf->lookups;
63268508Seric 	svcno = 0;
63368508Seric 	while (lk != NULL)
63468508Seric 	{
63568508Seric 		maptype[svcno] = lk->service_name;
63668508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
63768508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
63868508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
63968508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
64068508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
64168508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
64268508Seric 		svcno++;
64368508Seric 		lk = lk->next;
64468508Seric 	}
64568508Seric 	return svcno;
64668508Seric #endif
64768508Seric 
64869815Seric #ifdef _USE_DEC_SVC_CONF_
64968508Seric 	struct svcinfo *svcinfo;
65068508Seric 	int svc;
65168508Seric 
65269401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
65369401Seric 		mapreturn[svcno] = 0;
65469401Seric 
65568508Seric 	svcinfo = getsvc();
65668508Seric 	if (svcinfo == NULL)
65768508Seric 		goto punt;
65868508Seric 	if (strcmp(service, "hosts") == 0)
65968508Seric 		svc = SVC_HOSTS;
66068508Seric 	else if (strcmp(service, "aliases") == 0)
66168508Seric 		svc = SVC_ALIASES;
66268508Seric 	else if (strcmp(service, "passwd") == 0)
66368508Seric 		svc = SVC_PASSWD;
66468508Seric 	else
66568508Seric 		return -1;
66668508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
66768508Seric 	{
66868508Seric 		switch (svcinfo->svcpath[svc][svcno])
66968508Seric 		{
67068508Seric 		  case SVC_LOCAL:
67168508Seric 			maptype[svcno] = "files";
67268508Seric 			break;
67368508Seric 
67468508Seric 		  case SVC_YP:
67568508Seric 			maptype[svcno] = "nis";
67668508Seric 			break;
67768508Seric 
67868508Seric 		  case SVC_BIND:
67968508Seric 			maptype[svcno] = "dns";
68068508Seric 			break;
68168508Seric 
68268508Seric #ifdef SVC_HESIOD
68368508Seric 		  case SVC_HESIOD:
68468508Seric 			maptype[svcno] = "hesiod";
68568508Seric 			break;
68668508Seric #endif
68768508Seric 
68868508Seric 		  case SVC_LAST:
68968508Seric 			return svcno;
69068508Seric 		}
69168508Seric 	}
69268508Seric 	return svcno;
69368508Seric #endif
69468508Seric 
69569815Seric #if !defined(SOLARIS) && !defined(_USE_DEC_SVC_CONF_)
69668508Seric 	/*
69768508Seric 	**  Fall-back mechanism.
69868508Seric 	*/
69968508Seric 
70069401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
70169401Seric 		mapreturn[svcno] = 0;
70269401Seric 
70368508Seric 	svcno = 0;
70468508Seric 	fp = fopen(ServiceSwitchFile, "r");
70568508Seric 	if (fp != NULL)
70668508Seric 	{
70768508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
70868508Seric 		{
70968508Seric 			register char *p;
71068508Seric 
71168508Seric 			p = strpbrk(buf, "#\n");
71268508Seric 			if (p != NULL)
71368508Seric 				*p = '\0';
71468508Seric 			p = strpbrk(buf, " \t");
71568508Seric 			if (p != NULL)
71668508Seric 				*p++ = '\0';
71768508Seric 			if (strcmp(buf, service) != 0)
71868508Seric 				continue;
71968508Seric 
72068508Seric 			/* got the right service -- extract data */
72168508Seric 			do
72268508Seric 			{
72368508Seric 				while (isspace(*p))
72468508Seric 					p++;
72568508Seric 				if (*p == '\0')
72668508Seric 					break;
72768508Seric 				maptype[svcno++] = p;
72868508Seric 				p = strpbrk(p, " \t");
72968508Seric 				if (p != NULL)
73068508Seric 					*p++ = '\0';
73168508Seric 			} while (p != NULL);
73269830Seric 			fclose(fp);
73369830Seric 			return svcno;
73468508Seric 		}
73569830Seric 
73669830Seric 		/* service was not found -- use compiled in default */
73768508Seric 		fclose(fp);
73868508Seric 	}
73968508Seric #endif
74068508Seric 
74168508Seric 	/* if the service file doesn't work, use an absolute fallback */
74268508Seric   punt:
74369401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
74469401Seric 		mapreturn[svcno] = 0;
74569401Seric 	svcno = 0;
74668508Seric 	if (strcmp(service, "aliases") == 0)
74768508Seric 	{
74868628Seric 		maptype[svcno++] = "files";
74968628Seric #ifdef AUTO_NIS_ALIASES
75068628Seric # ifdef NISPLUS
75168628Seric 		maptype[svcno++] = "nisplus";
75268628Seric # endif
75368628Seric # ifdef NIS
75468628Seric 		maptype[svcno++] = "nis";
75568628Seric # endif
75668628Seric #endif
75768628Seric 		return svcno;
75868508Seric 	}
75968508Seric 	if (strcmp(service, "hosts") == 0)
76068508Seric 	{
76168508Seric # if NAMED_BIND
76268508Seric 		maptype[svcno++] = "dns";
76368508Seric # else
76468508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
76568508Seric 		/* SunOS */
76668508Seric 		maptype[svcno++] = "nis";
76768508Seric #  endif
76868508Seric # endif
76968508Seric 		maptype[svcno++] = "files";
77068508Seric 		return svcno;
77168508Seric 	}
77268508Seric 	return -1;
77368508Seric }
77468508Seric /*
7759369Seric **  USERNAME -- return the user id of the logged in user.
7769369Seric **
7779369Seric **	Parameters:
7789369Seric **		none.
7799369Seric **
7809369Seric **	Returns:
7819369Seric **		The login name of the logged in user.
7829369Seric **
7839369Seric **	Side Effects:
7849369Seric **		none.
7859369Seric **
7869369Seric **	Notes:
7879369Seric **		The return value is statically allocated.
7889369Seric */
7899369Seric 
7909369Seric char *
username()7919369Seric username()
7929369Seric {
79317469Seric 	static char *myname = NULL;
7949369Seric 	extern char *getlogin();
79519904Smiriam 	register struct passwd *pw;
7969369Seric 
79717469Seric 	/* cache the result */
79817469Seric 	if (myname == NULL)
79917469Seric 	{
80017469Seric 		myname = getlogin();
80117469Seric 		if (myname == NULL || myname[0] == '\0')
80217469Seric 		{
80368693Seric 			pw = sm_getpwuid(RealUid);
80417469Seric 			if (pw != NULL)
80540993Sbostic 				myname = newstr(pw->pw_name);
80617469Seric 		}
80719904Smiriam 		else
80819904Smiriam 		{
80963787Seric 			uid_t uid = RealUid;
81019873Smiriam 
81140993Sbostic 			myname = newstr(myname);
81268693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
81358736Seric 			      (uid != 0 && uid != pw->pw_uid))
81419904Smiriam 			{
81568693Seric 				pw = sm_getpwuid(uid);
81624945Seric 				if (pw != NULL)
81740993Sbostic 					myname = newstr(pw->pw_name);
81819873Smiriam 			}
81919873Smiriam 		}
82017469Seric 		if (myname == NULL || myname[0] == '\0')
82117469Seric 		{
82258151Seric 			syserr("554 Who are you?");
82317469Seric 			myname = "postmaster";
82417469Seric 		}
82517469Seric 	}
82617469Seric 
82717469Seric 	return (myname);
8289369Seric }
8299369Seric /*
8304190Seric **  TTYPATH -- Get the path of the user's tty
831294Seric **
832294Seric **	Returns the pathname of the user's tty.  Returns NULL if
833294Seric **	the user is not logged in or if s/he has write permission
834294Seric **	denied.
835294Seric **
836294Seric **	Parameters:
837294Seric **		none
838294Seric **
839294Seric **	Returns:
840294Seric **		pathname of the user's tty.
841294Seric **		NULL if not logged in or write permission denied.
842294Seric **
843294Seric **	Side Effects:
844294Seric **		none.
845294Seric **
846294Seric **	WARNING:
847294Seric **		Return value is in a local buffer.
848294Seric **
849294Seric **	Called By:
850294Seric **		savemail
851294Seric */
852294Seric 
853294Seric char *
ttypath()854294Seric ttypath()
855294Seric {
856294Seric 	struct stat stbuf;
857294Seric 	register char *pathn;
858294Seric 	extern char *ttyname();
8594081Seric 	extern char *getlogin();
860294Seric 
861294Seric 	/* compute the pathname of the controlling tty */
8629369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8639369Seric 	    (pathn = ttyname(0)) == NULL)
864294Seric 	{
865294Seric 		errno = 0;
866294Seric 		return (NULL);
867294Seric 	}
868294Seric 
869294Seric 	/* see if we have write permission */
8702967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
871294Seric 	{
872294Seric 		errno = 0;
873294Seric 		return (NULL);
874294Seric 	}
875294Seric 
876294Seric 	/* see if the user is logged in */
877294Seric 	if (getlogin() == NULL)
878294Seric 		return (NULL);
879294Seric 
880294Seric 	/* looks good */
881294Seric 	return (pathn);
882294Seric }
8832967Seric /*
8842967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8852967Seric **
8862967Seric **	This routine can be supplied on a per-installation basis
8872967Seric **	to determine whether a person is allowed to send a message.
8882967Seric **	This allows restriction of certain types of internet
8892967Seric **	forwarding or registration of users.
8902967Seric **
8912967Seric **	If the hosts are found to be incompatible, an error
89268857Seric **	message should be given using "usrerr" and an EX_ code
89368857Seric **	should be returned.  You can also set to->q_status to
89468857Seric **	a DSN-style status code.
8952967Seric **
89668559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
89768559Seric **	body during the return-to-sender function; this should be done
89868559Seric **	on huge messages.  This bit may already be set by the ESMTP
89968559Seric **	protocol.
9004288Seric **
9012967Seric **	Parameters:
9022967Seric **		to -- the person being sent to.
9032967Seric **
9042967Seric **	Returns:
90557459Seric **		an exit status
9062967Seric **
9072967Seric **	Side Effects:
9082967Seric **		none (unless you include the usrerr stuff)
9092967Seric */
9102967Seric 
91168508Seric int
checkcompat(to,e)91255012Seric checkcompat(to, e)
9132967Seric 	register ADDRESS *to;
91455012Seric 	register ENVELOPE *e;
9152967Seric {
91612133Seric # ifdef lint
91712133Seric 	if (to == NULL)
91812133Seric 		to++;
91963834Seric # endif /* lint */
92064402Seric 
92164402Seric 	if (tTd(49, 1))
92264402Seric 		printf("checkcompat(to=%s, from=%s)\n",
92364402Seric 			to->q_paddr, e->e_from.q_paddr);
92464402Seric 
92510698Seric # ifdef EXAMPLE_CODE
92610698Seric 	/* this code is intended as an example only */
9274437Seric 	register STAB *s;
9284437Seric 
9294437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
93068508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9319369Seric 	    to->q_mailer == s->s_mailer)
9324437Seric 	{
93358151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
93468559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
93568857Seric 		to->q_status = "5.7.1";
93657459Seric 		return (EX_UNAVAILABLE);
9374437Seric 	}
93856795Seric # endif /* EXAMPLE_CODE */
93957459Seric 	return (EX_OK);
9402967Seric }
9419369Seric /*
94264035Seric **  SETSIGNAL -- set a signal handler
94364035Seric **
94464035Seric **	This is essentially old BSD "signal(3)".
94564035Seric */
94664035Seric 
94764561Seric sigfunc_t
setsignal(sig,handler)94864035Seric setsignal(sig, handler)
94964035Seric 	int sig;
95064561Seric 	sigfunc_t handler;
95164035Seric {
95269892Seric #if defined(SYS5SIGNALS) || defined(BSD4_3)
95364035Seric 	return signal(sig, handler);
95464035Seric #else
95564035Seric 	struct sigaction n, o;
95664035Seric 
95764035Seric 	bzero(&n, sizeof n);
95864035Seric 	n.sa_handler = handler;
95968508Seric # ifdef SA_RESTART
96068508Seric 	n.sa_flags = SA_RESTART;
96168508Seric # endif
96264035Seric 	if (sigaction(sig, &n, &o) < 0)
96364035Seric 		return SIG_ERR;
96464035Seric 	return o.sa_handler;
96564035Seric #endif
96664035Seric }
96764035Seric /*
96869927Seric **  RELEASESIGNAL -- release a held signal
96969927Seric **
97069927Seric **	Parameters:
97169927Seric **		sig -- the signal to release.
97269927Seric **
97369927Seric **	Returns:
97469927Seric **		0 on success.
97569927Seric **		-1 on failure.
97669927Seric */
97769927Seric 
97869927Seric int
releasesignal(sig)97969927Seric releasesignal(sig)
98069927Seric 	int sig;
98169927Seric {
98269927Seric #ifdef BSD4_3
98369927Seric 	return sigsetmask(sigblock(0) & ~(1 << sig));
98469927Seric #else
98569927Seric 	sigset_t sset;
98669927Seric 
98769927Seric 	sigemptyset(&sset);
98869927Seric 	sigaddset(&sset, sig);
98969927Seric 	return sigprocmask(SIG_UNBLOCK, &sset, NULL);
99069927Seric #endif
99169927Seric }
99269927Seric /*
9939369Seric **  HOLDSIGS -- arrange to hold all signals
9949369Seric **
9959369Seric **	Parameters:
9969369Seric **		none.
9979369Seric **
9989369Seric **	Returns:
9999369Seric **		none.
10009369Seric **
10019369Seric **	Side Effects:
10029369Seric **		Arranges that signals are held.
10039369Seric */
10049369Seric 
100568508Seric void
holdsigs()10069369Seric holdsigs()
10079369Seric {
10089369Seric }
10099369Seric /*
10109369Seric **  RLSESIGS -- arrange to release all signals
10119369Seric **
10129369Seric **	This undoes the effect of holdsigs.
10139369Seric **
10149369Seric **	Parameters:
10159369Seric **		none.
10169369Seric **
10179369Seric **	Returns:
10189369Seric **		none.
10199369Seric **
10209369Seric **	Side Effects:
10219369Seric **		Arranges that signals are released.
10229369Seric */
10239369Seric 
102468508Seric void
rlsesigs()10259369Seric rlsesigs()
10269369Seric {
10279369Seric }
102814872Seric /*
102964705Seric **  INIT_MD -- do machine dependent initializations
103064705Seric **
103164705Seric **	Systems that have global modes that should be set should do
103264705Seric **	them here rather than in main.
103364705Seric */
103464705Seric 
103564705Seric #ifdef _AUX_SOURCE
103664705Seric # include	<compat.h>
103764705Seric #endif
103864705Seric 
103968508Seric void
init_md(argc,argv)104064825Seric init_md(argc, argv)
104164825Seric 	int argc;
104264825Seric 	char **argv;
104364705Seric {
104464705Seric #ifdef _AUX_SOURCE
104564705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
104664705Seric #endif
104768508Seric 
104868508Seric #ifdef VENDOR_DEFAULT
104968508Seric 	VendorCode = VENDOR_DEFAULT;
105068508Seric #else
105168508Seric 	VendorCode = VENDOR_BERKELEY;
105268508Seric #endif
105364705Seric }
105464705Seric /*
105568508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
105668508Seric **
105768508Seric **	Called once, on startup.
105868508Seric **
105968508Seric **	Parameters:
106068508Seric **		e -- the global envelope.
106168508Seric **
106268508Seric **	Returns:
106368508Seric **		none.
106468508Seric **
106568508Seric **	Side Effects:
106668508Seric **		vendor-dependent.
106768508Seric */
106868508Seric 
106968508Seric void
init_vendor_macros(e)107068508Seric init_vendor_macros(e)
107168508Seric 	register ENVELOPE *e;
107268508Seric {
107368508Seric }
107468508Seric /*
107514872Seric **  GETLA -- get the current load average
107614872Seric **
107714881Seric **	This code stolen from la.c.
107814881Seric **
107914872Seric **	Parameters:
108014872Seric **		none.
108114872Seric **
108214872Seric **	Returns:
108314872Seric **		The current load average as an integer.
108414872Seric **
108514872Seric **	Side Effects:
108614872Seric **		none.
108714872Seric */
108814872Seric 
108951920Seric /* try to guess what style of load average we have */
109051920Seric #define LA_ZERO		1	/* always return load average as zero */
109164360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
109251920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
109351920Seric #define LA_SUBR		4	/* call getloadavg */
109464295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
109564360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
109666301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
109769462Seric #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
109869462Seric #define LA_DGUX		9	/* special DGUX implementation */
109969462Seric #define LA_HPUX		10	/* special HPUX implementation */
110014872Seric 
110163962Seric /* do guesses based on general OS type */
110251920Seric #ifndef LA_TYPE
110363962Seric # define LA_TYPE	LA_ZERO
110451920Seric #endif
110551920Seric 
110669462Seric #ifndef FSHIFT
110769462Seric # if defined(unixpc)
110869462Seric #  define FSHIFT	5
110969462Seric # endif
111069462Seric 
111169462Seric # if defined(__alpha) || defined(IRIX)
111269462Seric #  define FSHIFT	10
111369462Seric # endif
111469462Seric 
111569462Seric # if defined(_AIX3)
111669462Seric #  define FSHIFT	16
111769462Seric # endif
111869462Seric #endif
111969462Seric 
112069462Seric #ifndef FSHIFT
112169462Seric # define FSHIFT		8
112269462Seric #endif
112369462Seric 
112469462Seric #ifndef FSCALE
112569462Seric # define FSCALE		(1 << FSHIFT)
112669462Seric #endif
112769462Seric 
112869654Seric #ifndef LA_AVENRUN
112969654Seric # ifdef SYSTEM5
113069654Seric #  define LA_AVENRUN	"avenrun"
113169654Seric # else
113269654Seric #  define LA_AVENRUN	"_avenrun"
113369654Seric # endif
113469654Seric #endif
113569654Seric 
113664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
113751920Seric 
113814872Seric #include <nlist.h>
113914872Seric 
114068508Seric #ifdef IRIX64
114168508Seric # define nlist		nlist64
114268508Seric #endif
114368508Seric 
114451920Seric /* _PATH_UNIX should be defined in <paths.h> */
114551920Seric #ifndef _PATH_UNIX
114663962Seric # if defined(SYSTEM5)
114763962Seric #  define _PATH_UNIX	"/unix"
114863962Seric # else
114963962Seric #  define _PATH_UNIX	"/vmunix"
115063962Seric # endif
115151920Seric #endif
115251920Seric 
115369892Seric #ifdef _AUX_SOURCE
115469892Seric struct nlist	Nl[2];
115569892Seric #else
115669462Seric struct nlist	Nl[] =
115714872Seric {
115851920Seric 	{ LA_AVENRUN },
115914872Seric 	{ 0 },
116014872Seric };
116169892Seric #endif
116269892Seric #define	X_AVENRUN	0
116314872Seric 
getla()116414872Seric getla()
116514872Seric {
116614872Seric 	static int kmem = -1;
116751920Seric #if LA_TYPE == LA_INT
116824943Seric 	long avenrun[3];
116951920Seric #else
117064360Seric # if LA_TYPE == LA_SHORT
117164360Seric 	short avenrun[3];
117264360Seric # else
117351920Seric 	double avenrun[3];
117464360Seric # endif
117551920Seric #endif
117669462Seric 	extern int errno;
117725615Seric 	extern off_t lseek();
117814872Seric 
117914872Seric 	if (kmem < 0)
118014872Seric 	{
118124945Seric 		kmem = open("/dev/kmem", 0, 0);
118214872Seric 		if (kmem < 0)
118357736Seric 		{
118457736Seric 			if (tTd(3, 1))
118557736Seric 				printf("getla: open(/dev/kmem): %s\n",
118657736Seric 					errstring(errno));
118714872Seric 			return (-1);
118857736Seric 		}
118951920Seric 		(void) fcntl(kmem, F_SETFD, 1);
119069462Seric 
119169892Seric #ifdef _AUX_SOURCE
119269892Seric 		strcpy(Nl[X_AVENRUN].n_name, LA_AVENRUN);
119369892Seric #endif
119469892Seric 
119568508Seric #ifdef _AIX3
119668508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
119768508Seric #else
119857736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
119968508Seric #endif
120057736Seric 		{
120157736Seric 			if (tTd(3, 1))
120257736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
120357736Seric 					errstring(errno));
120414872Seric 			return (-1);
120557736Seric 		}
120659253Seric 		if (Nl[X_AVENRUN].n_value == 0)
120759253Seric 		{
120859253Seric 			if (tTd(3, 1))
120959253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
121059253Seric 					_PATH_UNIX, LA_AVENRUN);
121159253Seric 			return (-1);
121259253Seric 		}
121368508Seric #ifdef NAMELISTMASK
121468508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
121566763Seric #endif
121614872Seric 	}
121757736Seric 	if (tTd(3, 20))
121857736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
121968508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
122023118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
122119967Seric 	{
122219967Seric 		/* thank you Ian */
122357736Seric 		if (tTd(3, 1))
122457736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
122519967Seric 		return (-1);
122619967Seric 	}
122769462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
122857736Seric 	if (tTd(3, 5))
122957736Seric 	{
123057736Seric 		printf("getla: avenrun = %d", avenrun[0]);
123157736Seric 		if (tTd(3, 15))
123257736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
123357736Seric 		printf("\n");
123457736Seric 	}
123557736Seric 	if (tTd(3, 1))
123657736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
123724943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
123869462Seric # else /* LA_TYPE == LA_FLOAT */
123957736Seric 	if (tTd(3, 5))
124057736Seric 	{
124157736Seric 		printf("getla: avenrun = %g", avenrun[0]);
124257736Seric 		if (tTd(3, 15))
124357736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
124457736Seric 		printf("\n");
124557736Seric 	}
124657736Seric 	if (tTd(3, 1))
124757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
124851920Seric 	return ((int) (avenrun[0] + 0.5));
124969462Seric # endif
125014872Seric }
125114872Seric 
125269462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
125351773Seric 
125469462Seric #if LA_TYPE == LA_READKSYM
125564718Seric 
125669473Seric # include <sys/ksym.h>
125769473Seric 
getla()125869462Seric getla()
125969462Seric {
126069462Seric 	static int kmem = -1;
126169462Seric 	long avenrun[3];
126269462Seric 	extern int errno;
126369462Seric 	struct mioc_rksym mirk;
126464718Seric 
126569462Seric 	if (kmem < 0)
126669462Seric 	{
126769462Seric 		kmem = open("/dev/kmem", 0, 0);
126869462Seric 		if (kmem < 0)
126969462Seric 		{
127069462Seric 			if (tTd(3, 1))
127169462Seric 				printf("getla: open(/dev/kmem): %s\n",
127269462Seric 					errstring(errno));
127369462Seric 			return (-1);
127469462Seric 		}
127569462Seric 		(void) fcntl(kmem, F_SETFD, 1);
127669462Seric 	}
127769462Seric 	mirk.mirk_symname = LA_AVENRUN;
127869462Seric 	mirk.mirk_buf = avenrun;
127969462Seric 	mirk.mirk_buflen = sizeof(avenrun);
128069462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
128169462Seric 	{
128269462Seric 		if (tTd(3, 1))
128369462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
128469462Seric 				errstring(errno));
128569462Seric 		return -1;
128669462Seric 	}
128769462Seric 	if (tTd(3, 5))
128869462Seric 	{
128969462Seric 		printf("getla: avenrun = %d", avenrun[0]);
129069462Seric 		if (tTd(3, 15))
129169462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
129269462Seric 		printf("\n");
129369462Seric 	}
129469462Seric 	if (tTd(3, 1))
129569462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
129669462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
129769462Seric }
129869462Seric 
129969462Seric #endif /* LA_TYPE == LA_READKSYM */
130069462Seric 
130169462Seric #if LA_TYPE == LA_DGUX
130269462Seric 
130369462Seric # include <sys/dg_sys_info.h>
130469462Seric 
130568508Seric int
getla()130668508Seric getla()
130764718Seric {
130864718Seric 	struct dg_sys_info_load_info load_info;
130964718Seric 
131064718Seric 	dg_sys_info((long *)&load_info,
131164718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
131264718Seric 
131368508Seric         if (tTd(3, 1))
131468508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
131568508Seric 
131664718Seric 	return((int) (load_info.one_minute + 0.5));
131764718Seric }
131864718Seric 
131969462Seric #endif /* LA_TYPE == LA_DGUX */
132064718Seric 
132169462Seric #if LA_TYPE == LA_HPUX
132269462Seric 
132369563Seric /* forward declarations to keep gcc from complaining */
132468869Seric struct pst_dynamic;
132569563Seric struct pst_status;
132669563Seric struct pst_static;
132769563Seric struct pst_vminfo;
132869563Seric struct pst_diskinfo;
132969563Seric struct pst_processor;
133069563Seric struct pst_lv;
133169563Seric struct pst_swapinfo;
133268869Seric 
133369462Seric # include <sys/param.h>
133469462Seric # include <sys/pstat.h>
133568508Seric 
133668508Seric int
getla()133751773Seric getla()
133851773Seric {
133968508Seric 	struct pst_dynamic pstd;
134068508Seric 
134168508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
134268869Seric 			     (size_t) 1, 0) == -1)
134368508Seric 		return 0;
134468508Seric 
134568508Seric         if (tTd(3, 1))
134668508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
134768508Seric 
134868508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
134968508Seric }
135068508Seric 
135169462Seric #endif /* LA_TYPE == LA_HPUX */
135268508Seric 
135369462Seric #if LA_TYPE == LA_SUBR
135469462Seric 
135568508Seric int
getla()135668508Seric getla()
135768508Seric {
135851920Seric 	double avenrun[3];
135951920Seric 
136051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
136157736Seric 	{
136257736Seric 		if (tTd(3, 1))
136357736Seric 			perror("getla: getloadavg failed:");
136451920Seric 		return (-1);
136557736Seric 	}
136657736Seric 	if (tTd(3, 1))
136757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
136851920Seric 	return ((int) (avenrun[0] + 0.5));
136951773Seric }
137051773Seric 
137169462Seric #endif /* LA_TYPE == LA_SUBR */
137269462Seric 
137364295Seric #if LA_TYPE == LA_MACH
137451773Seric 
137564295Seric /*
137665173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
137764295Seric */
137864295Seric 
137965173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
138065173Seric # include <mach/mach.h>
138165173Seric #else
138265173Seric # include <mach.h>
138365173Seric #endif
138464295Seric 
getla()138551773Seric getla()
138651773Seric {
138764295Seric 	processor_set_t default_set;
138864295Seric 	kern_return_t error;
138964295Seric 	unsigned int info_count;
139064295Seric 	struct processor_set_basic_info info;
139164295Seric 	host_t host;
139264295Seric 
139364295Seric 	error = processor_set_default(host_self(), &default_set);
139464295Seric 	if (error != KERN_SUCCESS)
139564295Seric 		return -1;
139664295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
139764295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
139864295Seric 			       &host, (processor_set_info_t)&info,
139964295Seric 			       &info_count) != KERN_SUCCESS)
140064295Seric 	{
140164295Seric 		return -1;
140264295Seric 	}
140364295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
140464295Seric }
140564295Seric 
140669462Seric #endif /* LA_TYPE == LA_MACH */
140764295Seric 
140866313Seric #if LA_TYPE == LA_PROCSTR
140964295Seric 
141066301Seric /*
141166301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
141266301Seric **  in a format like "0.15 0.12 0.06".
141366301Seric **
141466301Seric **	Initially intended for Linux.  This has been in the kernel
141566301Seric **	since at least 0.99.15.
141666301Seric */
141766301Seric 
141866301Seric # ifndef _PATH_LOADAVG
141966301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
142066301Seric # endif
142166301Seric 
142266301Seric int
getla()142364295Seric getla()
142464295Seric {
142566319Seric 	double avenrun;
142666301Seric 	register int result;
142766319Seric 	FILE *fp;
142866301Seric 
142966319Seric 	fp = fopen(_PATH_LOADAVG, "r");
143069472Seric 	if (fp == NULL)
143166301Seric 	{
143266319Seric 		if (tTd(3, 1))
143366319Seric 			printf("getla: fopen(%s): %s\n",
143466319Seric 				_PATH_LOADAVG, errstring(errno));
143566319Seric 		return -1;
143666301Seric 	}
143766301Seric 	result = fscanf(fp, "%lf", &avenrun);
143866319Seric 	fclose(fp);
143966301Seric 	if (result != 1)
144066301Seric 	{
144166310Seric 		if (tTd(3, 1))
144266301Seric 			printf("getla: fscanf() = %d: %s\n",
144366301Seric 				result, errstring(errno));
144466301Seric 		return -1;
144566301Seric 	}
144666301Seric 
144757736Seric 	if (tTd(3, 1))
144866301Seric 		printf("getla(): %.2f\n", avenrun);
144966301Seric 
145066301Seric 	return ((int) (avenrun + 0.5));
145166301Seric }
145266301Seric 
145369462Seric #endif /* LA_TYPE == LA_PROCSTR */
145466301Seric 
145569462Seric #if LA_TYPE == LA_ZERO
145669462Seric 
getla()145766301Seric getla()
145866301Seric {
145966301Seric 	if (tTd(3, 1))
146057736Seric 		printf("getla: ZERO\n");
146151920Seric 	return (0);
146251773Seric }
146351773Seric 
146469462Seric #endif /* LA_TYPE == LA_ZERO */
146566045Seric 
146666045Seric 
146766045Seric /*
146866045Seric  * Copyright 1989 Massachusetts Institute of Technology
146966045Seric  *
147066045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
147166045Seric  * documentation for any purpose is hereby granted without fee, provided that
147266045Seric  * the above copyright notice appear in all copies and that both that
147366045Seric  * copyright notice and this permission notice appear in supporting
147466045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
147566045Seric  * publicity pertaining to distribution of the software without specific,
147666045Seric  * written prior permission.  M.I.T. makes no representations about the
147766045Seric  * suitability of this software for any purpose.  It is provided "as is"
147866045Seric  * without express or implied warranty.
147966045Seric  *
148066045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
148166045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
148266045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
148366045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
148466045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
148566045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
148666045Seric  *
148766045Seric  * Authors:  Many and varied...
148866045Seric  */
148966045Seric 
149066045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
149166045Seric #ifndef lint
149266045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
149366045Seric #endif /* !lint */
149466045Seric 
149566179Seric #ifdef apollo
149666045Seric # undef volatile
149766045Seric #    include <apollo/base.h>
149866045Seric 
149966045Seric /* ARGSUSED */
getloadavg(call_data)150066045Seric int getloadavg( call_data )
150166045Seric      caddr_t	call_data;	/* pointer to (double) return value */
150266045Seric {
150366045Seric      double *avenrun = (double *) call_data;
150466045Seric      int i;
150566045Seric      status_$t      st;
150666045Seric      long loadav[3];
150766045Seric      proc1_$get_loadav(loadav, &st);
150866045Seric      *avenrun = loadav[0] / (double) (1 << 16);
150966045Seric      return(0);
151066045Seric }
151166045Seric #   endif /* apollo */
151224943Seric /*
151324943Seric **  SHOULDQUEUE -- should this message be queued or sent?
151424943Seric **
151524943Seric **	Compares the message cost to the load average to decide.
151624943Seric **
151724943Seric **	Parameters:
151824943Seric **		pri -- the priority of the message in question.
151957438Seric **		ctime -- the message creation time.
152024943Seric **
152124943Seric **	Returns:
152224943Seric **		TRUE -- if this message should be queued up for the
152324943Seric **			time being.
152424943Seric **		FALSE -- if the load is low enough to send this message.
152524943Seric **
152624943Seric **	Side Effects:
152724943Seric **		none.
152824943Seric */
152924943Seric 
153024943Seric bool
shouldqueue(pri,ctime)153157438Seric shouldqueue(pri, ctime)
153224943Seric 	long pri;
153357438Seric 	time_t ctime;
153424943Seric {
153568508Seric 	bool rval;
153668508Seric 
153768508Seric 	if (tTd(3, 30))
153868508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
153951920Seric 	if (CurrentLA < QueueLA)
154068508Seric 	{
154168508Seric 		if (tTd(3, 30))
154268508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
154324943Seric 		return (FALSE);
154468508Seric 	}
154558132Seric 	if (CurrentLA >= RefuseLA)
154668508Seric 	{
154768508Seric 		if (tTd(3, 30))
154868508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
154958132Seric 		return (TRUE);
155068508Seric 	}
155168508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
155268508Seric 	if (tTd(3, 30))
155368508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
155468508Seric 	return rval;
155524943Seric }
155624943Seric /*
155753037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
155853037Seric **
155953037Seric **	Parameters:
156053037Seric **		none.
156153037Seric **
156253037Seric **	Returns:
156353037Seric **		TRUE if incoming SMTP connections should be refused
156453037Seric **			(for now).
156553037Seric **		FALSE if we should accept new work.
156653037Seric **
156753037Seric **	Side Effects:
156869832Seric **		Sets process title when it is rejecting connections.
156953037Seric */
157053037Seric 
157153037Seric bool
refuseconnections()157253037Seric refuseconnections()
157353037Seric {
157469801Seric 	extern bool enoughdiskspace();
157569847Seric 	extern void setproctitle __P((char *, ...));
157668508Seric 
157759156Seric #ifdef XLA
157859156Seric 	if (!xla_smtp_ok())
157959156Seric 		return TRUE;
158059156Seric #endif
158159156Seric 
158269832Seric 	if (CurrentLA >= RefuseLA)
158369832Seric 	{
158469832Seric 		setproctitle("rejecting connections: load average: %d",
158569832Seric 			CurrentLA);
158669832Seric 	}
158769832Seric 	else if (!enoughdiskspace(MinBlocksFree + 1))
158869832Seric 	{
158969832Seric 		setproctitle("rejecting connections: min free: %d",
159069832Seric 			MinBlocksFree);
159169832Seric 	}
159269832Seric 	else if (MaxChildren > 0 && CurChildren >= MaxChildren)
159369832Seric 	{
159469832Seric 		setproctitle("rejecting connections: maximum children: %d",
159569832Seric 			CurChildren);
159669832Seric 	}
159769832Seric 	else
159869832Seric 		return FALSE;
159969832Seric 	return TRUE;
160053037Seric }
160153037Seric /*
160224943Seric **  SETPROCTITLE -- set process title for ps
160324943Seric **
160424943Seric **	Parameters:
160558674Seric **		fmt -- a printf style format string.
160658674Seric **		a, b, c -- possible parameters to fmt.
160724943Seric **
160824943Seric **	Returns:
160924943Seric **		none.
161024943Seric **
161124943Seric **	Side Effects:
161224943Seric **		Clobbers argv of our main procedure so ps(1) will
161324943Seric **		display the title.
161424943Seric */
161524943Seric 
161668508Seric #define SPT_NONE	0	/* don't use it at all */
161768508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
161868508Seric #define SPT_BUILTIN	2	/* use libc builtin */
161968508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
162068508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
162169654Seric #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
162268508Seric 
162368508Seric #ifndef SPT_TYPE
162468508Seric # define SPT_TYPE	SPT_REUSEARGV
162568508Seric #endif
162668508Seric 
162768508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
162868508Seric 
162968508Seric # if SPT_TYPE == SPT_PSTAT
163058689Seric #  include <sys/pstat.h>
163158689Seric # endif
163268508Seric # if SPT_TYPE == SPT_PSSTRINGS
163359732Seric #  include <machine/vmparam.h>
163459732Seric #  include <sys/exec.h>
163568508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
163668508Seric #   undef SPT_TYPE
163768508Seric #   define SPT_TYPE	SPT_REUSEARGV
163868734Seric #  else
163968734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
164068734Seric #    define NKPDE 63
164168734Seric typedef unsigned int	*pt_entry_t;
164268734Seric #   endif
164364247Seric #  endif
164459732Seric # endif
164568508Seric 
164668508Seric # if SPT_TYPE == SPT_PSSTRINGS
164768508Seric #  define SETPROC_STATIC	static
164868508Seric # else
164959732Seric #  define SETPROC_STATIC
165059732Seric # endif
165167626Seric 
165269654Seric # if SPT_TYPE == SPT_SYSMIPS
165369654Seric #  include <sys/sysmips.h>
165469654Seric #  include <sys/sysnews.h>
165569654Seric # endif
165669654Seric 
165768508Seric # ifndef SPT_PADCHAR
165868508Seric #  define SPT_PADCHAR	' '
165968508Seric # endif
166058689Seric 
166168508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
166264843Seric 
166368508Seric #if SPT_TYPE != SPT_BUILTIN
166468508Seric 
166524943Seric /*VARARGS1*/
166668508Seric void
166768508Seric # ifdef __STDC__
setproctitle(char * fmt,...)166857642Seric setproctitle(char *fmt, ...)
166968508Seric # else
167057642Seric setproctitle(fmt, va_alist)
167124943Seric 	char *fmt;
167257642Seric 	va_dcl
167368508Seric # endif
167424943Seric {
167568508Seric # if SPT_TYPE != SPT_NONE
167624943Seric 	register char *p;
167725049Seric 	register int i;
167859732Seric 	SETPROC_STATIC char buf[MAXLINE];
167956852Seric 	VA_LOCAL_DECL
168068508Seric #  if SPT_TYPE == SPT_PSTAT
168158689Seric 	union pstun pst;
168258689Seric #  endif
168369748Seric #  if SPT_TYPE == SPT_REUSEARGV
168424943Seric 	extern char **Argv;
168524943Seric 	extern char *LastArgv;
168669748Seric #  endif
168724943Seric 
168858674Seric 	p = buf;
168924943Seric 
169058674Seric 	/* print sendmail: heading for grep */
169158674Seric 	(void) strcpy(p, "sendmail: ");
169258674Seric 	p += strlen(p);
169324943Seric 
169458674Seric 	/* print the argument string */
169558674Seric 	VA_START(fmt);
169658674Seric 	(void) vsprintf(p, fmt, ap);
169756852Seric 	VA_END;
169854996Seric 
169958674Seric 	i = strlen(buf);
170058689Seric 
170168508Seric #  if SPT_TYPE == SPT_PSTAT
170258689Seric 	pst.pst_command = buf;
170358689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
170469654Seric #  endif
170569654Seric #  if SPT_TYPE == SPT_PSSTRINGS
170659732Seric 	PS_STRINGS->ps_nargvstr = 1;
170759732Seric 	PS_STRINGS->ps_argvstr = buf;
170869654Seric #  endif
170969654Seric #  if SPT_TYPE == SPT_SYSMIPS
171069654Seric 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
171169654Seric #  endif
171269654Seric #  if SPT_TYPE == SPT_REUSEARGV
171354996Seric 	if (i > LastArgv - Argv[0] - 2)
171425049Seric 	{
171554996Seric 		i = LastArgv - Argv[0] - 2;
171658674Seric 		buf[i] = '\0';
171725049Seric 	}
171858674Seric 	(void) strcpy(Argv[0], buf);
171954997Seric 	p = &Argv[0][i];
172024943Seric 	while (p < LastArgv)
172168508Seric 		*p++ = SPT_PADCHAR;
172268508Seric 	Argv[1] = NULL;
172369654Seric #  endif
172468508Seric # endif /* SPT_TYPE != SPT_NONE */
172524943Seric }
172665982Seric 
172768508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
172825698Seric /*
172925698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
173025698Seric **
173125698Seric **	Parameters:
173268508Seric **		sig -- the signal that got us here (unused).
173325698Seric **
173425698Seric **	Returns:
173525698Seric **		none.
173625698Seric **
173725698Seric **	Side Effects:
173825698Seric **		Picks up extant zombies.
173925698Seric */
174025698Seric 
174146928Sbostic void
reapchild(sig)174268508Seric reapchild(sig)
174368508Seric 	int sig;
174425698Seric {
174564738Seric 	int olderrno = errno;
174663966Seric # ifdef HASWAITPID
174760219Seric 	auto int status;
174860560Seric 	int count;
174960560Seric 	int pid;
175060219Seric 
175160560Seric 	count = 0;
175260560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
175360560Seric 	{
175460560Seric 		if (count++ > 1000)
175560560Seric 		{
175666748Seric #ifdef LOG
175760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
175860560Seric 				pid, status);
175966748Seric #endif
176060560Seric 			break;
176160560Seric 		}
176269832Seric 		CurChildren--;
176360560Seric 	}
176460219Seric # else
176525698Seric # ifdef WNOHANG
176625698Seric 	union wait status;
176725698Seric 
176863753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
176969832Seric 		CurChildren--;
177056795Seric # else /* WNOHANG */
177125698Seric 	auto int status;
177225698Seric 
177360219Seric 	while (wait(&status) > 0)
177469832Seric 		CurChildren--;
177556795Seric # endif /* WNOHANG */
177660219Seric # endif
177764035Seric # ifdef SYS5SIGNALS
177864035Seric 	(void) setsignal(SIGCHLD, reapchild);
177958061Seric # endif
178064738Seric 	errno = olderrno;
178125698Seric }
178255418Seric /*
178369654Seric **  PUTENV -- emulation of putenv() in terms of setenv()
178469654Seric **
178569654Seric **	Not needed on Posix-compliant systems.
178669654Seric **	This doesn't have full Posix semantics, but it's good enough
178769654Seric **		for sendmail.
178869654Seric **
178969654Seric **	Parameter:
179069654Seric **		env -- the environment to put.
179169654Seric **
179269654Seric **	Returns:
179369654Seric **		none.
179469654Seric */
179569654Seric 
179669654Seric #ifdef NEEDPUTENV
179769654Seric 
179869717Seric # if NEEDPUTENV == 2		/* no setenv(3) call available */
179969717Seric 
180069717Seric int
putenv(str)180169717Seric putenv(str)
180269717Seric 	char *str;
180369717Seric {
180469717Seric 	char **current;
180569717Seric 	int matchlen, envlen=0;
180669717Seric 	char *tmp;
180769717Seric 	char **newenv;
180869717Seric 	static int first=1;
180969717Seric 	extern char **environ;
181069717Seric 
181169717Seric 	/*
181269717Seric 	 * find out how much of str to match when searching
181369717Seric 	 * for a string to replace.
181469717Seric 	 */
181569717Seric 	if ((tmp = index(str, '=')) == NULL || tmp == str)
181669717Seric 		matchlen = strlen(str);
181769717Seric 	else
181869717Seric 		matchlen = (int) (tmp - str);
181969717Seric 	++matchlen;
182069717Seric 
182169717Seric 	/*
182269717Seric 	 * Search for an existing string in the environment and find the
182369717Seric 	 * length of environ.  If found, replace and exit.
182469717Seric 	 */
182569717Seric 	for (current=environ; *current; current++) {
182669717Seric 		++envlen;
182769717Seric 
182869717Seric 		if (strncmp(str, *current, matchlen) == 0) {
182969717Seric 			/* found it, now insert the new version */
183069717Seric 			*current = (char *)str;
183169717Seric 			return(0);
183269717Seric 		}
183369717Seric 	}
183469717Seric 
183569717Seric 	/*
183669717Seric 	 * There wasn't already a slot so add space for a new slot.
183769717Seric 	 * If this is our first time through, use malloc(), else realloc().
183869717Seric 	 */
183969717Seric 	if (first) {
184069717Seric 		newenv = (char **) malloc(sizeof(char *) * (envlen + 2));
184169717Seric 		if (newenv == NULL)
184269717Seric 			return(-1);
184369717Seric 
184469717Seric 		first=0;
184569717Seric 		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
184669717Seric 	} else {
184769717Seric 		newenv = (char **) realloc((char *)environ, sizeof(char *) * (envlen + 2));
184869717Seric 		if (newenv == NULL)
184969717Seric 			return(-1);
185069717Seric 	}
185169717Seric 
185269717Seric 	/* actually add in the new entry */
185369717Seric 	environ = newenv;
185469717Seric 	environ[envlen] = (char *)str;
185569717Seric 	environ[envlen+1] = NULL;
185669717Seric 
185769717Seric 	return(0);
185869717Seric }
185969717Seric 
186069717Seric #else			/* implement putenv() in terms of setenv() */
186169717Seric 
186269717Seric int
putenv(env)186369654Seric putenv(env)
186469654Seric 	char *env;
186569654Seric {
186669654Seric 	char *p;
186769654Seric 	int l;
186869654Seric 	char nbuf[100];
186969654Seric 
187069654Seric 	p = strchr(env, '=');
187169654Seric 	if (p == NULL)
187269717Seric 		return 0;
187369654Seric 	l = p - env;
187469654Seric 	if (l > sizeof nbuf - 1)
187569654Seric 		l = sizeof nbuf - 1;
187669654Seric 	bcopy(env, nbuf, l);
187769654Seric 	nbuf[l] = '\0';
187869717Seric 	return setenv(nbuf, ++p, 1);
187969654Seric }
188069654Seric 
188169717Seric # endif
188269654Seric #endif
188369654Seric /*
188455418Seric **  UNSETENV -- remove a variable from the environment
188555418Seric **
188655418Seric **	Not needed on newer systems.
188755418Seric **
188855418Seric **	Parameters:
188955418Seric **		name -- the string name of the environment variable to be
189055418Seric **			deleted from the current environment.
189155418Seric **
189255418Seric **	Returns:
189355418Seric **		none.
189455418Seric **
189555418Seric **	Globals:
189655418Seric **		environ -- a pointer to the current environment.
189755418Seric **
189855418Seric **	Side Effects:
189955418Seric **		Modifies environ.
190055418Seric */
190155418Seric 
190263962Seric #ifndef HASUNSETENV
190355418Seric 
190455418Seric void
unsetenv(name)190555418Seric unsetenv(name)
190655418Seric 	char *name;
190755418Seric {
190855418Seric 	extern char **environ;
190955418Seric 	register char **pp;
191055418Seric 	int len = strlen(name);
191155418Seric 
191255418Seric 	for (pp = environ; *pp != NULL; pp++)
191355418Seric 	{
191455418Seric 		if (strncmp(name, *pp, len) == 0 &&
191555418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
191655418Seric 			break;
191755418Seric 	}
191855418Seric 
191955418Seric 	for (; *pp != NULL; pp++)
192055418Seric 		*pp = pp[1];
192155418Seric }
192255418Seric 
192363962Seric #endif
192456215Seric /*
192556215Seric **  GETDTABLESIZE -- return number of file descriptors
192656215Seric **
192756215Seric **	Only on non-BSD systems
192856215Seric **
192956215Seric **	Parameters:
193056215Seric **		none
193156215Seric **
193256215Seric **	Returns:
193356215Seric **		size of file descriptor table
193456215Seric **
193556215Seric **	Side Effects:
193656215Seric **		none
193756215Seric */
193856215Seric 
193963787Seric #ifdef SOLARIS
194063787Seric # include <sys/resource.h>
194163787Seric #endif
194256215Seric 
194356215Seric int
getdtsize()194463787Seric getdtsize()
194556215Seric {
194663787Seric #ifdef RLIMIT_NOFILE
194763787Seric 	struct rlimit rl;
194863787Seric 
194963787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
195063787Seric 		return rl.rlim_cur;
195163787Seric #endif
195263787Seric 
195364031Seric # ifdef HASGETDTABLESIZE
195464031Seric 	return getdtablesize();
195564031Seric # else
195664031Seric #  ifdef _SC_OPEN_MAX
195758689Seric 	return sysconf(_SC_OPEN_MAX);
195863787Seric #  else
195956215Seric 	return NOFILE;
196063787Seric #  endif
196158689Seric # endif
196256215Seric }
196357631Seric /*
196457631Seric **  UNAME -- get the UUCP name of this system.
196557631Seric */
196657631Seric 
196757943Seric #ifndef HASUNAME
196857631Seric 
196957631Seric int
uname(name)197057631Seric uname(name)
197157631Seric 	struct utsname *name;
197257631Seric {
197357631Seric 	FILE *file;
197457631Seric 	char *n;
197557631Seric 
197657631Seric 	name->nodename[0] = '\0';
197757631Seric 
197857661Seric 	/* try /etc/whoami -- one line with the node name */
197957631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
198057631Seric 	{
198157661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
198257631Seric 		(void) fclose(file);
198357661Seric 		n = strchr(name->nodename, '\n');
198457631Seric 		if (n != NULL)
198557631Seric 			*n = '\0';
198657631Seric 		if (name->nodename[0] != '\0')
198757631Seric 			return (0);
198857631Seric 	}
198957631Seric 
199057661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
199157631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
199257631Seric 	{
199357631Seric 		char buf[MAXLINE];
199457631Seric 
199557631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
199657631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
199757631Seric 					NODE_LENGTH, name->nodename) > 0)
199857631Seric 				break;
199957631Seric 		(void) fclose(file);
200057631Seric 		if (name->nodename[0] != '\0')
200157631Seric 			return (0);
200257631Seric 	}
200357631Seric 
200457631Seric #ifdef TRUST_POPEN
200557631Seric 	/*
200657631Seric 	**  Popen is known to have security holes.
200757631Seric 	*/
200857631Seric 
200957661Seric 	/* try uuname -l to return local name */
201057631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
201157631Seric 	{
201257661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
201357631Seric 		(void) pclose(file);
201457661Seric 		n = strchr(name, '\n');
201557631Seric 		if (n != NULL)
201657631Seric 			*n = '\0';
201757661Seric 		if (name->nodename[0] != '\0')
201857631Seric 			return (0);
201957631Seric 	}
202057631Seric #endif
202169472Seric 
202257631Seric 	return (-1);
202357631Seric }
202457943Seric #endif /* HASUNAME */
202558068Seric /*
202658068Seric **  INITGROUPS -- initialize groups
202758068Seric **
202858068Seric **	Stub implementation for System V style systems
202958068Seric */
203058068Seric 
203158068Seric #ifndef HASINITGROUPS
203258068Seric 
initgroups(name,basegid)203358068Seric initgroups(name, basegid)
203458068Seric 	char *name;
203558068Seric 	int basegid;
203658068Seric {
203758068Seric 	return 0;
203858068Seric }
203958068Seric 
204058068Seric #endif
204158082Seric /*
204259289Seric **  SETSID -- set session id (for non-POSIX systems)
204359289Seric */
204459289Seric 
204559289Seric #ifndef HASSETSID
204659289Seric 
204763753Seric pid_t
setsid(void)204863753Seric setsid __P ((void))
204959289Seric {
205064155Seric #ifdef TIOCNOTTY
205164155Seric 	int fd;
205264155Seric 
205366743Seric 	fd = open("/dev/tty", O_RDWR, 0);
205464155Seric 	if (fd >= 0)
205564155Seric 	{
205664155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
205764155Seric 		(void) close(fd);
205864155Seric 	}
205964155Seric #endif /* TIOCNOTTY */
206064705Seric # ifdef SYS5SETPGRP
206163753Seric 	return setpgrp();
206263753Seric # else
206364155Seric 	return setpgid(0, getpid());
206459289Seric # endif
206559289Seric }
206659289Seric 
206759289Seric #endif
206859289Seric /*
206966748Seric **  FSYNC -- dummy fsync
207066748Seric */
207166748Seric 
207266748Seric #ifdef NEEDFSYNC
207366748Seric 
fsync(fd)207466748Seric fsync(fd)
207566748Seric 	int fd;
207666748Seric {
207766748Seric # ifdef O_SYNC
207866748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
207966748Seric # else
208066748Seric 	/* nothing we can do */
208166748Seric 	return 0;
208266748Seric # endif
208366748Seric }
208466748Seric 
208566748Seric #endif
208666748Seric /*
208764718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
208864718Seric **
208964718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
209068508Seric **	instead of a long.  This patches things.  Only needed on versions
209168508Seric **	prior to 5.4.3.
209264718Seric */
209364718Seric 
209468508Seric #ifdef DGUX_5_4_2
209564718Seric 
209664718Seric #undef inet_addr
209764718Seric 
209864718Seric long
dgux_inet_addr(host)209964718Seric dgux_inet_addr(host)
210064718Seric 	char *host;
210164718Seric {
210264718Seric 	struct in_addr haddr;
210364718Seric 
210464718Seric 	haddr = inet_addr(host);
210564718Seric 	return haddr.s_addr;
210664718Seric }
210764718Seric 
210864718Seric #endif
210964718Seric /*
211063969Seric **  GETOPT -- for old systems or systems with bogus implementations
211163969Seric */
211263969Seric 
211363969Seric #ifdef NEEDGETOPT
211463969Seric 
211563969Seric /*
211663969Seric  * Copyright (c) 1985 Regents of the University of California.
211763969Seric  * All rights reserved.  The Berkeley software License Agreement
211863969Seric  * specifies the terms and conditions for redistribution.
211963969Seric  */
212063969Seric 
212163969Seric 
212263969Seric /*
212363969Seric ** this version hacked to add `atend' flag to allow state machine
212463969Seric ** to reset if invoked by the program to scan args for a 2nd time
212563969Seric */
212663969Seric 
212763969Seric #if defined(LIBC_SCCS) && !defined(lint)
212863969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
212964072Seric #endif /* LIBC_SCCS and not lint */
213063969Seric 
213163969Seric #include <stdio.h>
213263969Seric 
213363969Seric /*
213463969Seric  * get option letter from argument vector
213563969Seric  */
213665352Seric #ifdef _CONVEX_SOURCE
213768508Seric extern int	optind, opterr, optopt;
213868508Seric extern char	*optarg;
213965352Seric #else
214065352Seric int	opterr = 1;		/* if error message should be printed */
214165352Seric int	optind = 1;		/* index into parent argv vector */
214268470Seric int	optopt = 0;		/* character checked for validity */
214368470Seric char	*optarg = NULL;		/* argument associated with option */
214468508Seric #endif
214563969Seric 
214663969Seric #define BADCH	(int)'?'
214763969Seric #define EMSG	""
214863969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
214963969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
215063969Seric 
getopt(nargc,nargv,ostr)215163969Seric getopt(nargc,nargv,ostr)
215265160Seric 	int		nargc;
215365160Seric 	char *const	*nargv;
215465160Seric 	const char	*ostr;
215563969Seric {
215663969Seric 	static char	*place = EMSG;	/* option letter processing */
215763969Seric 	static char	atend = 0;
215863969Seric 	register char	*oli;		/* option letter list index */
215963969Seric 
216063969Seric 	if (atend) {
216163969Seric 		atend = 0;
216263969Seric 		place = EMSG;
216363969Seric 	}
216463969Seric 	if(!*place) {			/* update scanning pointer */
216564136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
216663969Seric 			atend++;
216763969Seric 			return(EOF);
216863969Seric 		}
216963969Seric 		if (*place == '-') {	/* found "--" */
217063969Seric 			++optind;
217163969Seric 			atend++;
217263969Seric 			return(EOF);
217363969Seric 		}
217463969Seric 	}				/* option letter okay? */
217564125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
217664136Seric 		if (!*place) ++optind;
217763969Seric 		tell(": illegal option -- ");
217863969Seric 	}
217963969Seric 	if (*++oli != ':') {		/* don't need argument */
218063969Seric 		optarg = NULL;
218163969Seric 		if (!*place) ++optind;
218263969Seric 	}
218363969Seric 	else {				/* need an argument */
218463969Seric 		if (*place) optarg = place;	/* no white space */
218563969Seric 		else if (nargc <= ++optind) {	/* no arg */
218663969Seric 			place = EMSG;
218763969Seric 			tell(": option requires an argument -- ");
218863969Seric 		}
218963969Seric 	 	else optarg = nargv[optind];	/* white space */
219063969Seric 		place = EMSG;
219163969Seric 		++optind;
219263969Seric 	}
219363969Seric 	return(optopt);			/* dump back option letter */
219463969Seric }
219563969Seric 
219663969Seric #endif
219763969Seric /*
219863969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
219963969Seric */
220063969Seric 
220163969Seric #ifdef NEEDVPRINTF
220263969Seric 
220363969Seric #define MAXARG	16
220463969Seric 
vfprintf(fp,fmt,ap)220563969Seric vfprintf(fp, fmt, ap)
220663969Seric 	FILE *	fp;
220763969Seric 	char *	fmt;
220863969Seric 	char **	ap;
220963969Seric {
221063969Seric 	char *	bp[MAXARG];
221163969Seric 	int	i = 0;
221263969Seric 
221363969Seric 	while (*ap && i < MAXARG)
221463969Seric 		bp[i++] = *ap++;
221563969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
221663969Seric 			 bp[4], bp[5], bp[6], bp[7],
221763969Seric 			 bp[8], bp[9], bp[10], bp[11],
221863969Seric 			 bp[12], bp[13], bp[14], bp[15]);
221963969Seric }
222063969Seric 
vsprintf(s,fmt,ap)222163969Seric vsprintf(s, fmt, ap)
222263969Seric 	char *	s;
222363969Seric 	char *	fmt;
222463969Seric 	char **	ap;
222563969Seric {
222663969Seric 	char *	bp[MAXARG];
222763969Seric 	int	i = 0;
222863969Seric 
222963969Seric 	while (*ap && i < MAXARG)
223063969Seric 		bp[i++] = *ap++;
223163969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
223263969Seric 			bp[4], bp[5], bp[6], bp[7],
223363969Seric 			bp[8], bp[9], bp[10], bp[11],
223463969Seric 			bp[12], bp[13], bp[14], bp[15]);
223563969Seric }
223663969Seric 
223763969Seric #endif
223863969Seric /*
223965220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
224065208Seric **
224165211Seric **	Parameters:
224269894Seric **		user -- the name of the user we are checking.
224365211Seric **		shell -- the user's shell from /etc/passwd
224465211Seric **
224565211Seric **	Returns:
224665211Seric **		TRUE -- if it is ok to use this for unrestricted access.
224765211Seric **		FALSE -- if the shell is restricted.
224865208Seric */
224965208Seric 
225065587Seric #if !HASGETUSERSHELL
225165587Seric 
225265587Seric # ifndef _PATH_SHELLS
225365587Seric #  define _PATH_SHELLS	"/etc/shells"
225465587Seric # endif
225565587Seric 
225669762Seric # ifdef _AIX3
225769762Seric #  include <userconf.h>
225869762Seric #  include <usersec.h>
225969762Seric # endif
226069762Seric 
226165587Seric char	*DefaultUserShells[] =
226265587Seric {
226368508Seric 	"/bin/sh",		/* standard shell */
226465587Seric 	"/usr/bin/sh",
226568508Seric 	"/bin/csh",		/* C shell */
226665587Seric 	"/usr/bin/csh",
226765587Seric #ifdef __hpux
226868889Seric # ifdef V4FS
226968889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
227068889Seric 	"/usr/bin/ksh",		/* Korn shell */
227168889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
227268889Seric 	"/usr/bin/pam",
227368889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
227468889Seric 	"/usr/bin/posix/sh",
227568889Seric # else
227668508Seric 	"/bin/rsh",		/* restricted Bourne shell */
227768508Seric 	"/bin/ksh",		/* Korn shell */
227868508Seric 	"/bin/rksh",		/* restricted Korn shell */
227965587Seric 	"/bin/pam",
228068508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
228165587Seric 	"/bin/posix/sh",
228268889Seric # endif
228365211Seric #endif
228468508Seric #ifdef _AIX3
228568508Seric 	"/bin/ksh",		/* Korn shell */
228668508Seric 	"/usr/bin/ksh",
228768508Seric 	"/bin/tsh",		/* trusted shell */
228868508Seric 	"/usr/bin/tsh",
228968508Seric 	"/bin/bsh",		/* Bourne shell */
229068508Seric 	"/usr/bin/bsh",
229168508Seric #endif
229265587Seric 	NULL
229365587Seric };
229465587Seric 
229565208Seric #endif
229665208Seric 
229765747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
229865747Seric 
229965211Seric bool
usershellok(user,shell)230069894Seric usershellok(user, shell)
230169894Seric 	char *user;
230265211Seric 	char *shell;
230365208Seric {
230465211Seric #if HASGETUSERSHELL
230565211Seric 	register char *p;
230665211Seric 	extern char *getusershell();
230765208Seric 
230869894Seric 	if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
230969894Seric 	    ConfigLevel <= 1)
231068457Seric 		return TRUE;
231168457Seric 
231265211Seric 	setusershell();
231365211Seric 	while ((p = getusershell()) != NULL)
231465747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
231565211Seric 			break;
231665211Seric 	endusershell();
231765211Seric 	return p != NULL;
231865211Seric #else
231969769Seric # if USEGETCONFATTR
232069762Seric 	auto char *v;
232169762Seric # endif
232265211Seric 	register FILE *shellf;
232365211Seric 	char buf[MAXLINE];
232465208Seric 
232569894Seric 	if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't'))
232668457Seric 		return TRUE;
232768457Seric 
232869769Seric # if USEGETCONFATTR
232969769Seric 	/*
233069769Seric 	**  Naturally IBM has a "better" idea.....
233169769Seric 	**
233269769Seric 	**	What a crock.  This interface isn't documented, it is
233369769Seric 	**	considered part of the security library (-ls), and it
233469769Seric 	**	only works if you are running as root (since the list
233569769Seric 	**	of valid shells is obviously a source of great concern).
233669769Seric 	**	I recommend that you do NOT define USEGETCONFATTR,
233769769Seric 	**	especially since you are going to have to set up an
233869769Seric 	**	/etc/shells anyhow to handle the cases where getconfattr
233969769Seric 	**	fails.
234069769Seric 	*/
234169769Seric 
234269769Seric 	if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
234369762Seric 	{
234469769Seric 		while (*v != '\0')
234569762Seric 		{
234669762Seric 			if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
234769762Seric 				return TRUE;
234869762Seric 			v += strlen(v) + 1;
234969762Seric 		}
235069769Seric 		return FALSE;
235169762Seric 	}
235269762Seric # endif
235369762Seric 
235465211Seric 	shellf = fopen(_PATH_SHELLS, "r");
235565211Seric 	if (shellf == NULL)
235665211Seric 	{
235765211Seric 		/* no /etc/shells; see if it is one of the std shells */
235865587Seric 		char **d;
235965587Seric 
236065587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
236165587Seric 		{
236265587Seric 			if (strcmp(shell, *d) == 0)
236365587Seric 				return TRUE;
236465587Seric 		}
236565587Seric 		return FALSE;
236665211Seric 	}
236765208Seric 
236865211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
236965211Seric 	{
237065211Seric 		register char *p, *q;
237165208Seric 
237265211Seric 		p = buf;
237365211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
237465211Seric 			p++;
237565211Seric 		if (*p == '#' || *p == '\0')
237665208Seric 			continue;
237765211Seric 		q = p;
237865211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
237965211Seric 			p++;
238065211Seric 		*p = '\0';
238165747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
238265211Seric 		{
238365211Seric 			fclose(shellf);
238465211Seric 			return TRUE;
238565211Seric 		}
238665208Seric 	}
238765211Seric 	fclose(shellf);
238865211Seric 	return FALSE;
238965211Seric #endif
239065208Seric }
239165208Seric /*
239269801Seric **  FREEDISKSPACE -- see how much free space is on the queue filesystem
239358082Seric **
239458082Seric **	Only implemented if you have statfs.
239558082Seric **
239658082Seric **	Parameters:
239764311Seric **		dir -- the directory in question.
239864311Seric **		bsize -- a variable into which the filesystem
239964311Seric **			block size is stored.
240058082Seric **
240158082Seric **	Returns:
240264311Seric **		The number of bytes free on the queue filesystem.
240364311Seric **		-1 if the statfs call fails.
240464311Seric **
240564311Seric **	Side effects:
240664311Seric **		Puts the filesystem block size into bsize.
240758082Seric */
240858082Seric 
240965749Seric /* statfs types */
241065749Seric #define SFS_NONE	0	/* no statfs implementation */
241165749Seric #define SFS_USTAT	1	/* use ustat */
241265749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
241365749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
241465749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
241565749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
241666752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
241765749Seric 
241865749Seric #ifndef SFS_TYPE
241965749Seric # define SFS_TYPE	SFS_NONE
242058157Seric #endif
242158157Seric 
242265749Seric #if SFS_TYPE == SFS_USTAT
242358157Seric # include <ustat.h>
242458157Seric #endif
242565749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
242665749Seric # include <sys/statfs.h>
242758082Seric #endif
242865749Seric #if SFS_TYPE == SFS_VFS
242965749Seric # include <sys/vfs.h>
243065749Seric #endif
243165749Seric #if SFS_TYPE == SFS_MOUNT
243265749Seric # include <sys/mount.h>
243365749Seric #endif
243466752Seric #if SFS_TYPE == SFS_STATVFS
243566752Seric # include <sys/statvfs.h>
243666752Seric #endif
243758082Seric 
243864311Seric long
freediskspace(dir,bsize)243969801Seric freediskspace(dir, bsize)
244064311Seric 	char *dir;
244164311Seric 	long *bsize;
244258082Seric {
244365749Seric #if SFS_TYPE != SFS_NONE
244465749Seric # if SFS_TYPE == SFS_USTAT
244558153Seric 	struct ustat fs;
244658153Seric 	struct stat statbuf;
244758366Seric #  define FSBLOCKSIZE	DEV_BSIZE
244868695Seric #  define SFS_BAVAIL	f_tfree
244958157Seric # else
245058157Seric #  if defined(ultrix)
245158157Seric 	struct fs_data fs;
245268695Seric #   define SFS_BAVAIL	fd_bfreen
245368508Seric #   define FSBLOCKSIZE	1024L
245458153Seric #  else
245566752Seric #   if SFS_TYPE == SFS_STATVFS
245666752Seric 	struct statvfs fs;
245768508Seric #    define FSBLOCKSIZE	fs.f_frsize
245866752Seric #   else
245958082Seric 	struct statfs fs;
246066752Seric #    define FSBLOCKSIZE	fs.f_bsize
246163987Seric #   endif
246258153Seric #  endif
246358133Seric # endif
246468695Seric # ifndef SFS_BAVAIL
246568695Seric #  define SFS_BAVAIL f_bavail
246668508Seric # endif
246758082Seric 
246865749Seric # if SFS_TYPE == SFS_USTAT
246964311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
247058157Seric # else
247165749Seric #  if SFS_TYPE == SFS_4ARGS
247264311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
247358157Seric #  else
247468508Seric #   if SFS_TYPE == SFS_STATVFS
247568508Seric 	if (statvfs(dir, &fs) == 0)
247668508Seric #   else
247768508Seric #    if defined(ultrix)
247868499Seric 	if (statfs(dir, &fs) > 0)
247968508Seric #    else
248064311Seric 	if (statfs(dir, &fs) == 0)
248168508Seric #    endif
248258153Seric #   endif
248358133Seric #  endif
248458133Seric # endif
248558133Seric 	{
248664311Seric 		if (bsize != NULL)
248764311Seric 			*bsize = FSBLOCKSIZE;
248868695Seric 		return (fs.SFS_BAVAIL);
248964311Seric 	}
249064337Seric #endif
249164311Seric 	return (-1);
249264311Seric }
249364311Seric /*
249469801Seric **  ENOUGHDISKSPACE -- is there enough free space on the queue fs?
249564311Seric **
249664311Seric **	Only implemented if you have statfs.
249764311Seric **
249864311Seric **	Parameters:
249964311Seric **		msize -- the size to check against.  If zero, we don't yet
250064311Seric **		know how big the message will be, so just check for
250164311Seric **		a "reasonable" amount.
250264311Seric **
250364311Seric **	Returns:
250464311Seric **		TRUE if there is enough space.
250564311Seric **		FALSE otherwise.
250664311Seric */
250764311Seric 
250864311Seric bool
enoughdiskspace(msize)250969801Seric enoughdiskspace(msize)
251064311Seric 	long msize;
251164311Seric {
251264311Seric 	long bfree, bsize;
251364311Seric 
251464311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
251564311Seric 	{
251658133Seric 		if (tTd(4, 80))
251769801Seric 			printf("enoughdiskspace: no threshold\n");
251864311Seric 		return TRUE;
251964311Seric 	}
252064311Seric 
252169801Seric 	if ((bfree = freediskspace(QueueDir, &bsize)) >= 0)
252264311Seric 	{
252364311Seric 		if (tTd(4, 80))
252469801Seric 			printf("enoughdiskspace: bavail=%ld, need=%ld\n",
252564311Seric 				bfree, msize);
252658333Seric 
252758333Seric 		/* convert msize to block count */
252864311Seric 		msize = msize / bsize + 1;
252958333Seric 		if (MinBlocksFree >= 0)
253058333Seric 			msize += MinBlocksFree;
253158333Seric 
253264311Seric 		if (bfree < msize)
253358090Seric 		{
253458090Seric #ifdef LOG
253558090Seric 			if (LogLevel > 0)
253664254Seric 				syslog(LOG_ALERT,
253764254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
253868508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
253968508Seric 					bfree,
254068508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
254168508Seric 					msize, QueueDir);
254258090Seric #endif
254358082Seric 			return FALSE;
254458090Seric 		}
254558082Seric 	}
254658082Seric 	else if (tTd(4, 80))
254769801Seric 		printf("enoughdiskspace failure: min=%ld, need=%ld: %s\n",
254858333Seric 			MinBlocksFree, msize, errstring(errno));
254958082Seric 	return TRUE;
255058082Seric }
255158542Seric /*
255258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
255358542Seric **
255458542Seric **	This looks at an errno value and tells if this is likely to
255558542Seric **	go away if retried later.
255658542Seric **
255758542Seric **	Parameters:
255858542Seric **		err -- the errno code to classify.
255958542Seric **
256058542Seric **	Returns:
256158542Seric **		TRUE if this is probably transient.
256258542Seric **		FALSE otherwise.
256358542Seric */
256458542Seric 
256558542Seric bool
transienterror(err)256658542Seric transienterror(err)
256758542Seric 	int err;
256858542Seric {
256958542Seric 	switch (err)
257058542Seric 	{
257158542Seric 	  case EIO:			/* I/O error */
257258542Seric 	  case ENXIO:			/* Device not configured */
257358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
257458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
257558542Seric 	  case ENODEV:			/* Operation not supported by device */
257658542Seric 	  case ENFILE:			/* Too many open files in system */
257758542Seric 	  case EMFILE:			/* Too many open files */
257858542Seric 	  case ENOSPC:			/* No space left on device */
257958542Seric #ifdef ETIMEDOUT
258058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
258158542Seric #endif
258258542Seric #ifdef ESTALE
258358542Seric 	  case ESTALE:			/* Stale NFS file handle */
258458542Seric #endif
258558542Seric #ifdef ENETDOWN
258658542Seric 	  case ENETDOWN:		/* Network is down */
258758542Seric #endif
258858542Seric #ifdef ENETUNREACH
258958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
259058542Seric #endif
259158542Seric #ifdef ENETRESET
259258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
259358542Seric #endif
259458542Seric #ifdef ECONNABORTED
259558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
259658542Seric #endif
259758542Seric #ifdef ECONNRESET
259858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
259958542Seric #endif
260058542Seric #ifdef ENOBUFS
260158542Seric 	  case ENOBUFS:			/* No buffer space available */
260258542Seric #endif
260358542Seric #ifdef ESHUTDOWN
260458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
260558542Seric #endif
260658542Seric #ifdef ECONNREFUSED
260758542Seric 	  case ECONNREFUSED:		/* Connection refused */
260858542Seric #endif
260958542Seric #ifdef EHOSTDOWN
261058542Seric 	  case EHOSTDOWN:		/* Host is down */
261158542Seric #endif
261258542Seric #ifdef EHOSTUNREACH
261358542Seric 	  case EHOSTUNREACH:		/* No route to host */
261458542Seric #endif
261558542Seric #ifdef EDQUOT
261658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
261758542Seric #endif
261858542Seric #ifdef EPROCLIM
261958542Seric 	  case EPROCLIM:		/* Too many processes */
262058542Seric #endif
262158542Seric #ifdef EUSERS
262258542Seric 	  case EUSERS:			/* Too many users */
262358542Seric #endif
262458542Seric #ifdef EDEADLK
262558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
262658542Seric #endif
262758542Seric #ifdef EISCONN
262858542Seric 	  case EISCONN:			/* Socket already connected */
262958542Seric #endif
263058542Seric #ifdef EINPROGRESS
263158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
263258542Seric #endif
263358542Seric #ifdef EALREADY
263458542Seric 	  case EALREADY:		/* Operation already in progress */
263558542Seric #endif
263658542Seric #ifdef EADDRINUSE
263758542Seric 	  case EADDRINUSE:		/* Address already in use */
263858542Seric #endif
263958542Seric #ifdef EADDRNOTAVAIL
264058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
264158542Seric #endif
264266018Seric #ifdef ETXTBSY
264366017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
264466017Seric #endif
264563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
264658542Seric 	  case ENOSR:			/* Out of streams resources */
264758542Seric #endif
264858542Seric 		return TRUE;
264958542Seric 	}
265058542Seric 
265158542Seric 	/* nope, must be permanent */
265258542Seric 	return FALSE;
265358542Seric }
265458689Seric /*
265564035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
265658689Seric **
265758689Seric **	Parameters:
265858689Seric **		fd -- the file descriptor of the file.
265958689Seric **		filename -- the file name (for error messages).
266064335Seric **		ext -- the filename extension.
266158689Seric **		type -- type of the lock.  Bits can be:
266258689Seric **			LOCK_EX -- exclusive lock.
266358689Seric **			LOCK_NB -- non-blocking.
266458689Seric **
266558689Seric **	Returns:
266658689Seric **		TRUE if the lock was acquired.
266758689Seric **		FALSE otherwise.
266858689Seric */
266958689Seric 
267058689Seric bool
lockfile(fd,filename,ext,type)267164335Seric lockfile(fd, filename, ext, type)
267258689Seric 	int fd;
267358689Seric 	char *filename;
267464335Seric 	char *ext;
267558689Seric 	int type;
267658689Seric {
267765830Seric # if !HASFLOCK
267858689Seric 	int action;
267958689Seric 	struct flock lfd;
268064335Seric 
268164335Seric 	if (ext == NULL)
268264335Seric 		ext = "";
268369472Seric 
268463983Seric 	bzero(&lfd, sizeof lfd);
268559447Seric 	if (bitset(LOCK_UN, type))
268659447Seric 		lfd.l_type = F_UNLCK;
268759447Seric 	else if (bitset(LOCK_EX, type))
268858689Seric 		lfd.l_type = F_WRLCK;
268958689Seric 	else
269058689Seric 		lfd.l_type = F_RDLCK;
269158689Seric 
269258689Seric 	if (bitset(LOCK_NB, type))
269358689Seric 		action = F_SETLK;
269458689Seric 	else
269558689Seric 		action = F_SETLKW;
269658689Seric 
269764264Seric 	if (tTd(55, 60))
269864335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
269964335Seric 			filename, ext, action, lfd.l_type);
270064264Seric 
270158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
270264264Seric 	{
270364264Seric 		if (tTd(55, 60))
270464264Seric 			printf("SUCCESS\n");
270558689Seric 		return TRUE;
270664264Seric 	}
270758689Seric 
270864264Seric 	if (tTd(55, 60))
270964264Seric 		printf("(%s) ", errstring(errno));
271064264Seric 
271164136Seric 	/*
271264136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
271364136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
271464136Seric 	**  as type "tmp" (that is, served from swap space), the
271564136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
271664136Seric 	**  Since this is fairly common during testing, we will assume
271764136Seric 	**  that this indicates that the lock is successfully grabbed.
271864136Seric 	*/
271964136Seric 
272064136Seric 	if (errno == EINVAL)
272164264Seric 	{
272264264Seric 		if (tTd(55, 60))
272364264Seric 			printf("SUCCESS\n");
272464136Seric 		return TRUE;
272564264Seric 	}
272664136Seric 
272758689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
272864378Seric 	{
272964378Seric 		int omode = -1;
273064378Seric #  ifdef F_GETFL
273164378Seric 		int oerrno = errno;
273264378Seric 
273364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
273464378Seric 		errno = oerrno;
273564378Seric #  endif
273664378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
273764378Seric 			filename, ext, fd, type, omode, geteuid());
273864378Seric 	}
273958689Seric # else
274064335Seric 	if (ext == NULL)
274164335Seric 		ext = "";
274264335Seric 
274364264Seric 	if (tTd(55, 60))
274464335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
274564264Seric 
274658689Seric 	if (flock(fd, type) >= 0)
274764264Seric 	{
274864264Seric 		if (tTd(55, 60))
274964264Seric 			printf("SUCCESS\n");
275058689Seric 		return TRUE;
275164264Seric 	}
275258689Seric 
275364264Seric 	if (tTd(55, 60))
275464264Seric 		printf("(%s) ", errstring(errno));
275564264Seric 
275658689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
275764378Seric 	{
275864378Seric 		int omode = -1;
275964378Seric #  ifdef F_GETFL
276064378Seric 		int oerrno = errno;
276164378Seric 
276264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
276364378Seric 		errno = oerrno;
276464378Seric #  endif
276564378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
276664378Seric 			filename, ext, fd, type, omode, geteuid());
276764386Seric 	}
276858689Seric # endif
276964264Seric 	if (tTd(55, 60))
277064264Seric 		printf("FAIL\n");
277158689Seric 	return FALSE;
277258689Seric }
277364556Seric /*
277465948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
277565948Seric **
277665948Seric **	Parameters:
277765948Seric **		fd -- the file descriptor to check.
277865948Seric **
277965948Seric **	Returns:
278065948Seric **		TRUE -- if only root can chown the file to an arbitrary
278165948Seric **			user.
278265948Seric **		FALSE -- if an arbitrary user can give away a file.
278365948Seric */
278465948Seric 
278565948Seric bool
chownsafe(fd)278665948Seric chownsafe(fd)
278765948Seric 	int fd;
278865948Seric {
278965950Seric #ifdef __hpux
279065948Seric 	char *s;
279165948Seric 	int tfd;
279265948Seric 	uid_t o_uid, o_euid;
279365948Seric 	gid_t o_gid, o_egid;
279465948Seric 	bool rval;
279565948Seric 	struct stat stbuf;
279665948Seric 
279765948Seric 	o_uid = getuid();
279865948Seric 	o_euid = geteuid();
279965948Seric 	o_gid = getgid();
280065948Seric 	o_egid = getegid();
280165959Seric 	fstat(fd, &stbuf);
280265948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
280365948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
280465948Seric 	s = tmpnam(NULL);
280565948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
280665974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
280765948Seric 	close(tfd);
280865948Seric 	unlink(s);
280969638Seric 	setresuid(o_uid, o_euid, -1);
281065948Seric 	setresgid(o_gid, o_egid, -1);
281165948Seric 	return rval;
281265948Seric #else
281366088Seric # ifdef _POSIX_CHOWN_RESTRICTED
281466088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
281566088Seric 	return FALSE;
281666088Seric #  else
281766088Seric 	return TRUE;
281866088Seric #  endif
281966088Seric # else
282066088Seric #  ifdef _PC_CHOWN_RESTRICTED
282168508Seric 	int rval;
282268508Seric 
282368508Seric 	/*
282468508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
282568508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
282668508Seric 	**  the call.  This heuristic checks for that.
282768508Seric 	*/
282868508Seric 
282968508Seric 	errno = 0;
283068508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
283168508Seric 	if (errno == 0)
283268508Seric 		return rval > 0;
283368508Seric #  endif
283468508Seric #  ifdef BSD
283568508Seric 	return TRUE;
283668499Seric #  else
283765948Seric 	return FALSE;
283865948Seric #  endif
283965948Seric # endif
284065948Seric #endif
284165948Seric }
284265948Seric /*
284368508Seric **  RESETLIMITS -- reset system controlled resource limits
284468508Seric **
284568508Seric **	This is to avoid denial-of-service attacks
284668508Seric **
284768508Seric **	Parameters:
284868508Seric **		none
284968508Seric **
285068508Seric **	Returns:
285168508Seric **		none
285268508Seric */
285368508Seric 
285468508Seric #if HASSETRLIMIT
285568508Seric # include <sys/resource.h>
285668508Seric #endif
285768508Seric 
285868508Seric void
resetlimits()285968508Seric resetlimits()
286068508Seric {
286168508Seric #if HASSETRLIMIT
286268508Seric 	struct rlimit lim;
286368508Seric 
286468508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
286568508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
286668508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
286768508Seric #else
286868508Seric # if HASULIMIT
286968508Seric 	(void) ulimit(2, 0x3fffff);
287068508Seric # endif
287168508Seric #endif
287268508Seric }
287368508Seric /*
287464556Seric **  GETCFNAME -- return the name of the .cf file.
287564556Seric **
287664556Seric **	Some systems (e.g., NeXT) determine this dynamically.
287764556Seric */
287864556Seric 
287964556Seric char *
getcfname()288064556Seric getcfname()
288164556Seric {
288268842Seric 
288364556Seric 	if (ConfFile != NULL)
288464556Seric 		return ConfFile;
288569881Seric #if NETINFO
288665214Seric 	{
288765214Seric 		extern char *ni_propval();
288865214Seric 		char *cflocation;
288965214Seric 
289068508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
289168508Seric 					"sendmail.cf", '\0');
289265214Seric 		if (cflocation != NULL)
289365214Seric 			return cflocation;
289465214Seric 	}
289565214Seric #endif
289668842Seric 
289764556Seric 	return _PATH_SENDMAILCF;
289864556Seric }
289964718Seric /*
290064718Seric **  SETVENDOR -- process vendor code from V configuration line
290164718Seric **
290264718Seric **	Parameters:
290364718Seric **		vendor -- string representation of vendor.
290464718Seric **
290564718Seric **	Returns:
290664718Seric **		TRUE -- if ok.
290764718Seric **		FALSE -- if vendor code could not be processed.
290864926Seric **
290964926Seric **	Side Effects:
291064926Seric **		It is reasonable to set mode flags here to tweak
291164926Seric **		processing in other parts of the code if necessary.
291264926Seric **		For example, if you are a vendor that uses $%y to
291364926Seric **		indicate YP lookups, you could enable that here.
291464718Seric */
291564718Seric 
291664718Seric bool
setvendor(vendor)291764718Seric setvendor(vendor)
291864718Seric 	char *vendor;
291964718Seric {
292064926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
292168508Seric 	{
292268508Seric 		VendorCode = VENDOR_BERKELEY;
292364926Seric 		return TRUE;
292468508Seric 	}
292564926Seric 
292664926Seric 	/* add vendor extensions here */
292764926Seric 
292868508Seric #ifdef SUN_EXTENSIONS
292968508Seric 	if (strcasecmp(vendor, "Sun") == 0)
293068508Seric 	{
293168508Seric 		VendorCode = VENDOR_SUN;
293268508Seric 		return TRUE;
293368508Seric 	}
293468508Seric #endif
293568508Seric 
293664926Seric 	return FALSE;
293764718Seric }
293864816Seric /*
293969510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
294069510Seric **
294169510Seric **	Vendor_pre_defaults is called before reading the configuration
294269510Seric **	file; vendor_post_defaults is called immediately after.
294369510Seric **
294469510Seric **	Parameters:
294569510Seric **		e -- the global environment to initialize.
294669510Seric **
294769510Seric **	Returns:
294869510Seric **		none.
294969510Seric */
295069510Seric 
295169510Seric void
vendor_pre_defaults(e)295269510Seric vendor_pre_defaults(e)
295369510Seric 	ENVELOPE *e;
295469510Seric {
295569510Seric }
295669510Seric 
295769510Seric 
295869510Seric void
vendor_post_defaults(e)295969510Seric vendor_post_defaults(e)
296069510Seric 	ENVELOPE *e;
296169510Seric {
296269510Seric }
296369510Seric /*
296464816Seric **  STRTOL -- convert string to long integer
296564816Seric **
296664816Seric **	For systems that don't have it in the C library.
296766161Seric **
296866161Seric **	This is taken verbatim from the 4.4-Lite C library.
296964816Seric */
297064816Seric 
297164816Seric #ifdef NEEDSTRTOL
297264816Seric 
297366161Seric #if defined(LIBC_SCCS) && !defined(lint)
297466161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
297566161Seric #endif /* LIBC_SCCS and not lint */
297666161Seric 
297766161Seric #include <limits.h>
297866161Seric 
297966161Seric /*
298066161Seric  * Convert a string to a long integer.
298166161Seric  *
298266161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
298366161Seric  * alphabets and digits are each contiguous.
298466161Seric  */
298566161Seric 
298664816Seric long
strtol(nptr,endptr,base)298766161Seric strtol(nptr, endptr, base)
298866161Seric 	const char *nptr;
298966161Seric 	char **endptr;
299066161Seric 	register int base;
299164816Seric {
299266161Seric 	register const char *s = nptr;
299366161Seric 	register unsigned long acc;
299466161Seric 	register int c;
299566161Seric 	register unsigned long cutoff;
299666161Seric 	register int neg = 0, any, cutlim;
299764816Seric 
299866161Seric 	/*
299966161Seric 	 * Skip white space and pick up leading +/- sign if any.
300066161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
300166161Seric 	 * assume decimal; if base is already 16, allow 0x.
300266161Seric 	 */
300366161Seric 	do {
300466161Seric 		c = *s++;
300566161Seric 	} while (isspace(c));
300666161Seric 	if (c == '-') {
300766161Seric 		neg = 1;
300866161Seric 		c = *s++;
300966161Seric 	} else if (c == '+')
301066161Seric 		c = *s++;
301166161Seric 	if ((base == 0 || base == 16) &&
301266161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
301366161Seric 		c = s[1];
301466161Seric 		s += 2;
301566161Seric 		base = 16;
301666161Seric 	}
301766161Seric 	if (base == 0)
301866161Seric 		base = c == '0' ? 8 : 10;
301964816Seric 
302066161Seric 	/*
302166161Seric 	 * Compute the cutoff value between legal numbers and illegal
302266161Seric 	 * numbers.  That is the largest legal value, divided by the
302366161Seric 	 * base.  An input number that is greater than this value, if
302466161Seric 	 * followed by a legal input character, is too big.  One that
302566161Seric 	 * is equal to this value may be valid or not; the limit
302666161Seric 	 * between valid and invalid numbers is then based on the last
302766161Seric 	 * digit.  For instance, if the range for longs is
302866161Seric 	 * [-2147483648..2147483647] and the input base is 10,
302966161Seric 	 * cutoff will be set to 214748364 and cutlim to either
303066161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
303166161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
303266161Seric 	 * the number is too big, and we will return a range error.
303366161Seric 	 *
303466161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
303566161Seric 	 * overflow.
303666161Seric 	 */
303766161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
303866161Seric 	cutlim = cutoff % (unsigned long)base;
303966161Seric 	cutoff /= (unsigned long)base;
304066161Seric 	for (acc = 0, any = 0;; c = *s++) {
304166161Seric 		if (isdigit(c))
304266161Seric 			c -= '0';
304366161Seric 		else if (isalpha(c))
304466161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
304566161Seric 		else
304666161Seric 			break;
304766161Seric 		if (c >= base)
304866161Seric 			break;
304966161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
305066161Seric 			any = -1;
305166161Seric 		else {
305266161Seric 			any = 1;
305366161Seric 			acc *= base;
305466161Seric 			acc += c;
305564816Seric 		}
305664816Seric 	}
305766161Seric 	if (any < 0) {
305866161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
305966161Seric 		errno = ERANGE;
306066161Seric 	} else if (neg)
306166161Seric 		acc = -acc;
306266161Seric 	if (endptr != 0)
306366161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
306466161Seric 	return (acc);
306564816Seric }
306664816Seric 
306764816Seric #endif
306864841Seric /*
306969679Seric **  STRSTR -- find first substring in string
307069679Seric **
307169679Seric **	Parameters:
307269679Seric **		big -- the big (full) string.
307369679Seric **		little -- the little (sub) string.
307469679Seric **
307569679Seric **	Returns:
307669679Seric **		A pointer to the first instance of little in big.
307769679Seric **		big if little is the null string.
307869679Seric **		NULL if little is not contained in big.
307969679Seric */
308069679Seric 
308169679Seric #ifdef NEEDSTRSTR
308269679Seric 
308369679Seric char *
strstr(big,little)308469679Seric strstr(big, little)
308569679Seric 	char *big;
308669679Seric 	char *little;
308769679Seric {
308869679Seric 	register char *p = big;
308969679Seric 	int l;
309069679Seric 
309169679Seric 	if (*little == '\0')
309269679Seric 		return big;
309369679Seric 	l = strlen(little);
309469679Seric 
309569679Seric 	while ((p = strchr(p, *little)) != NULL)
309669679Seric 	{
309769679Seric 		if (strncmp(p, little, l) == 0)
309869679Seric 			return p;
309969679Seric 		p++;
310069679Seric 	}
310169679Seric 	return NULL;
310269679Seric }
310369679Seric 
310469679Seric #endif
310569679Seric /*
310668693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
310764841Seric **
310868693Seric **	Some operating systems have wierd problems with the gethostbyXXX
310968693Seric **	routines.  For example, Solaris versions at least through 2.3
311068693Seric **	don't properly deliver a canonical h_name field.  This tries to
311168693Seric **	work around these problems.
311264841Seric */
311364841Seric 
311464841Seric struct hostent *
sm_gethostbyname(name)311568693Seric sm_gethostbyname(name)
311668707Seric 	char *name;
311764841Seric {
311869777Seric 	struct hostent *h;
311969654Seric #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4)
312068508Seric # if SOLARIS == 203
312164841Seric 	static struct hostent hp;
312264841Seric 	static char buf[1000];
312364841Seric 	extern struct hostent *_switch_gethostbyname_r();
312464841Seric 
312569777Seric 	if (tTd(61, 10))
312669777Seric 		printf("_switch_gethostbyname_r(%s)... ", name);
312769777Seric 	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
312864841Seric # else
312965009Seric 	extern struct hostent *__switch_gethostbyname();
313065009Seric 
313169777Seric 	if (tTd(61, 10))
313269777Seric 		printf("__switch_gethostbyname(%s)... ", name);
313369777Seric 	h = __switch_gethostbyname(name);
313464841Seric # endif
313568693Seric #else
313669687Seric 	int nmaps;
313769687Seric 	char *maptype[MAXMAPSTACK];
313869687Seric 	short mapreturn[MAXMAPACTIONS];
313969687Seric 	char hbuf[MAXNAME];
314069687Seric 
314169777Seric 	if (tTd(61, 10))
314269777Seric 		printf("gethostbyname(%s)... ", name);
314369687Seric 	h = gethostbyname(name);
314469777Seric 	if (h == NULL)
314569687Seric 	{
314669777Seric 		if (tTd(61, 10))
314769777Seric 			printf("failure\n");
314869687Seric 
314969777Seric 		nmaps = switch_map_find("hosts", maptype, mapreturn);
315069777Seric 		while (--nmaps >= 0)
315169919Seric 			if (strcmp(maptype[nmaps], "nis") == 0 ||
315269919Seric 			    strcmp(maptype[nmaps], "files") == 0)
315369777Seric 				break;
315469777Seric 		if (nmaps >= 0)
315569777Seric 		{
315669777Seric 			/* try short name */
315769777Seric 			if (strlen(name) > sizeof hbuf - 1)
315869777Seric 				return NULL;
315969777Seric 			strcpy(hbuf, name);
316069777Seric 			shorten_hostname(hbuf);
316169777Seric 
316269777Seric 			/* if it hasn't been shortened, there's no point */
316369777Seric 			if (strcmp(hbuf, name) != 0)
316469777Seric 			{
316569777Seric 				if (tTd(61, 10))
316669777Seric 					printf("gethostbyname(%s)... ", hbuf);
316769777Seric 				h = gethostbyname(hbuf);
316869777Seric 			}
316969777Seric 		}
317069687Seric 	}
317169777Seric #endif
317269777Seric 	if (tTd(61, 10))
317369777Seric 	{
317469777Seric 		if (h == NULL)
317569777Seric 			printf("failure\n");
317669777Seric 		else
317769777Seric 			printf("%s\n", h->h_name);
317869777Seric 	}
317969687Seric 	return h;
318064841Seric }
318164841Seric 
318264841Seric struct hostent *
sm_gethostbyaddr(addr,len,type)318368693Seric sm_gethostbyaddr(addr, len, type)
318468707Seric 	char *addr;
318564841Seric 	int len;
318664841Seric 	int type;
318764841Seric {
318868693Seric #if defined(SOLARIS) && SOLARIS < 204
318968508Seric # if SOLARIS == 203
319064841Seric 	static struct hostent hp;
319164841Seric 	static char buf[1000];
319264841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
319364841Seric 
319464841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
319564841Seric # else
319665009Seric 	extern struct hostent *__switch_gethostbyaddr();
319765009Seric 
319864841Seric 	return __switch_gethostbyaddr(addr, len, type);
319964841Seric # endif
320068693Seric #else
320168693Seric 	return gethostbyaddr(addr, len, type);
320268693Seric #endif
320364841Seric }
320468693Seric /*
320568693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
320668693Seric */
320764841Seric 
320868693Seric struct passwd *
sm_getpwnam(user)320968693Seric sm_getpwnam(user)
321068704Seric 	char *user;
321168693Seric {
321268693Seric 	return getpwnam(user);
321368693Seric }
321468693Seric 
321568693Seric struct passwd *
sm_getpwuid(uid)321668693Seric sm_getpwuid(uid)
321769865Seric 	UID_T uid;
321868693Seric {
321968693Seric 	return getpwuid(uid);
322068693Seric }
322165214Seric /*
322269472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
322369472Seric **
322469472Seric **	Parameters:
322569472Seric **		none.
322669472Seric **
322769472Seric **	Returns:
322869472Seric **		none.
322969472Seric **
323069472Seric **	Side Effects:
323169472Seric **		Loads $=w with the names of all the interfaces.
323269472Seric */
323369472Seric 
323469472Seric #ifdef SIOCGIFCONF
323569565Seric struct rtentry;
323669565Seric struct mbuf;
323769472Seric # include <arpa/inet.h>
323869562Seric # include <sys/time.h>
323969472Seric # include <net/if.h>
324069472Seric #endif
324169472Seric 
324269472Seric void
load_if_names()324369472Seric load_if_names()
324469472Seric {
324569472Seric #ifdef SIOCGIFCONF
324669472Seric 	int s;
324769516Seric 	int i;
324869472Seric         struct ifconf ifc;
324969472Seric 	char interfacebuf[1024];
325069472Seric 
325169472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
325269472Seric 	if (s == -1)
325369472Seric 		return;
325469472Seric 
325569472Seric 	/* get the list of known IP address from the kernel */
325669472Seric         ifc.ifc_buf = interfacebuf;
325769516Seric         ifc.ifc_len = sizeof interfacebuf;
325869472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
325969472Seric 	{
326069472Seric 		if (tTd(0, 4))
326169472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
326269516Seric 		close(s);
326369472Seric 		return;
326469472Seric 	}
326569516Seric 	close(s);
326669472Seric 
326769472Seric 	/* scan the list of IP address */
326869865Seric 	if (tTd(0, 40))
326969472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
327069472Seric 			ifc.ifc_len);
327169472Seric 
327269516Seric 	for (i = 0; i < ifc.ifc_len; )
327369516Seric 	{
327469516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
327569516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
327669472Seric 		struct in_addr ia;
327769516Seric 		struct hostent *hp;
327869472Seric 		char ip_addr[256];
327969516Seric 		extern char *inet_ntoa();
328069516Seric 		extern struct hostent *gethostbyaddr();
328169472Seric 
328269516Seric #ifdef BSD4_4_SOCKADDR
328369516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
328469516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
328569516Seric 		else
328669516Seric #endif
328769516Seric 			i += sizeof *ifr;
328869516Seric 
328969472Seric 		if (tTd(0, 20))
329069516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
329169472Seric 
329269472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
329369472Seric 			continue;
329469472Seric 
329569472Seric 		/* extract IP address from the list*/
329669516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
329769472Seric 
329869472Seric 		/* save IP address in text from */
329969472Seric 		(void) sprintf(ip_addr, "[%s]",
330069516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
330169472Seric 		if (!wordinclass(ip_addr, 'w'))
330269472Seric 		{
330369472Seric 			setclass('w', ip_addr);
330469472Seric 			if (tTd(0, 4))
330569472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
330669472Seric 		}
330769472Seric 
330869472Seric 		/* skip "loopback" interface "lo" */
330969472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
331069472Seric 			continue;
331169472Seric 
331269472Seric 		/* lookup name with IP address */
331369472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
331469472Seric 		if (hp == NULL)
331569472Seric 		{
331669472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
331769472Seric 				inet_ntoa(ia));
331869472Seric 			continue;
331969472Seric 		}
332069472Seric 
332169472Seric 		/* save its cname */
332269472Seric 		if (!wordinclass(hp->h_name, 'w'))
332369472Seric 		{
332469472Seric 			setclass('w', hp->h_name);
332569472Seric 			if (tTd(0, 4))
332669472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
332769472Seric 		}
332869472Seric 
332969472Seric 		/* save all it aliases name */
333069472Seric 		while (*hp->h_aliases)
333169472Seric 		{
333269472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
333369472Seric 			{
333469472Seric 				setclass('w', *hp->h_aliases);
333569472Seric 				if (tTd(0, 4))
333669472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
333769472Seric 			}
333869472Seric 			hp->h_aliases++;
333969472Seric 		}
334069472Seric 	}
334169472Seric #endif
334269472Seric }
334369472Seric /*
334465214Seric **  NI_PROPVAL -- netinfo property value lookup routine
334565214Seric **
334665214Seric **	Parameters:
334768508Seric **		keydir -- the Netinfo directory name in which to search
334868508Seric **			for the key.
334968508Seric **		keyprop -- the name of the property in which to find the
335068508Seric **			property we are interested.  Defaults to "name".
335168508Seric **		keyval -- the value for which we are really searching.
335268508Seric **		valprop -- the property name for the value in which we
335368508Seric **			are interested.
335468508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
335568508Seric **			we should return a string separated by this
335668508Seric **			character.
335765214Seric **
335865214Seric **	Returns:
335965214Seric **		NULL -- if:
336065214Seric **			1. the directory is not found
336165214Seric **			2. the property name is not found
336265214Seric **			3. the property contains multiple values
336365214Seric **			4. some error occured
336465214Seric **		else -- the location of the config file.
336565214Seric **
336668508Seric **	Example:
336768508Seric **		To search for an alias value, use:
336868508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
336968508Seric **
337065214Seric **	Notes:
337165214Seric **      	Caller should free the return value of ni_proval
337265214Seric */
337365214Seric 
337469881Seric #if NETINFO
337565214Seric 
337665214Seric # include <netinfo/ni.h>
337765214Seric 
337865214Seric # define LOCAL_NETINFO_DOMAIN    "."
337965214Seric # define PARENT_NETINFO_DOMAIN   ".."
338065214Seric # define MAX_NI_LEVELS           256
338165214Seric 
338265214Seric char *
ni_propval(keydir,keyprop,keyval,valprop,sepchar)338368508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
338468508Seric 	char *keydir;
338568508Seric 	char *keyprop;
338668508Seric 	char *keyval;
338768508Seric 	char *valprop;
338868508Seric 	char sepchar;
338965214Seric {
339065820Seric 	char *propval = NULL;
339165214Seric 	int i;
339268508Seric 	int j, alen;
339365214Seric 	void *ni = NULL;
339465214Seric 	void *lastni = NULL;
339565214Seric 	ni_status nis;
339665214Seric 	ni_id nid;
339765214Seric 	ni_namelist ninl;
339868508Seric 	register char *p;
339968508Seric 	char keybuf[1024];
340065214Seric 
340165214Seric 	/*
340268508Seric 	**  Create the full key from the two parts.
340368508Seric 	**
340468508Seric 	**	Note that directory can end with, e.g., "name=" to specify
340568508Seric 	**	an alternate search property.
340668508Seric 	*/
340768508Seric 
340868508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
340968508Seric 	if (keyprop != NULL)
341068508Seric 		i += strlen(keyprop) + 1;
341168508Seric 	if (i > sizeof keybuf)
341268508Seric 		return NULL;
341368508Seric 	strcpy(keybuf, keydir);
341468508Seric 	strcat(keybuf, "/");
341568508Seric 	if (keyprop != NULL)
341668508Seric 	{
341768508Seric 		strcat(keybuf, keyprop);
341868508Seric 		strcat(keybuf, "=");
341968508Seric 	}
342068508Seric 	strcat(keybuf, keyval);
342168508Seric 
342268508Seric 	/*
342365214Seric 	**  If the passed directory and property name are found
342465214Seric 	**  in one of netinfo domains we need to search (starting
342565214Seric 	**  from the local domain moving all the way back to the
342665214Seric 	**  root domain) set propval to the property's value
342765214Seric 	**  and return it.
342865214Seric 	*/
342965214Seric 
343065214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
343165214Seric 	{
343265214Seric 		if (i == 0)
343365214Seric 		{
343465214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
343565214Seric 		}
343665214Seric 		else
343765214Seric 		{
343865214Seric 			if (lastni != NULL)
343965214Seric 				ni_free(lastni);
344065214Seric 			lastni = ni;
344165214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
344265214Seric 		}
344365214Seric 
344465214Seric 		/*
344565214Seric 		**  Don't bother if we didn't get a handle on a
344665214Seric 		**  proper domain.  This is not necessarily an error.
344765214Seric 		**  We would get a positive ni_status if, for instance
344865214Seric 		**  we never found the directory or property and tried
344965214Seric 		**  to open the parent of the root domain!
345065214Seric 		*/
345165214Seric 
345265214Seric 		if (nis != 0)
345365214Seric 			break;
345465214Seric 
345565214Seric 		/*
345665214Seric 		**  Find the path to the server information.
345765214Seric 		*/
345865214Seric 
345968508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
346065214Seric 			continue;
346165214Seric 
346265214Seric 		/*
346368508Seric 		**  Find associated value information.
346465214Seric 		*/
346565214Seric 
346668508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
346765214Seric 			continue;
346865214Seric 
346965214Seric 		/*
347068508Seric 		**  See if we have an acceptable number of values.
347165214Seric 		*/
347265214Seric 
347368508Seric 		if (ninl.ni_namelist_len <= 0)
347468508Seric 			continue;
347568508Seric 
347668508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
347765214Seric 		{
347868508Seric 			ni_namelist_free(&ninl);
347968508Seric 			continue;
348065214Seric 		}
348168508Seric 
348269472Seric 		/*
348368508Seric 		**  Calculate number of bytes needed and build result
348468508Seric 		*/
348568508Seric 
348668508Seric 		alen = 1;
348768508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
348868508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
348968508Seric 		propval = p = xalloc(alen);
349068508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
349168508Seric 		{
349268508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
349368508Seric 			p += strlen(p);
349468508Seric 			*p++ = sepchar;
349569472Seric 		}
349668508Seric 		*--p = '\0';
349768508Seric 
349868508Seric 		ni_namelist_free(&ninl);
349965214Seric 	}
350065214Seric 
350165214Seric 	/*
350265214Seric 	**  Clean up.
350365214Seric 	*/
350465214Seric 
350565214Seric 	if (ni != NULL)
350665214Seric 		ni_free(ni);
350765214Seric 	if (lastni != NULL && ni != lastni)
350865214Seric 		ni_free(lastni);
350965214Seric 
351065214Seric 	return propval;
351165214Seric }
351265214Seric 
351365214Seric #endif /* NETINFO */
351465504Seric /*
351565504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
351665504Seric **
351765504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
351865504Seric **	syslog succeeds during interrupt handlers.
351965504Seric */
352065504Seric 
352165504Seric #ifdef __hpux
352265504Seric 
352365504Seric # define MAXSYSLOGTRIES	100
352465504Seric # undef syslog
352565504Seric 
352669758Seric # ifdef __STDC__
352769748Seric void
hard_syslog(int pri,char * msg,...)352865504Seric hard_syslog(int pri, char *msg, ...)
352965504Seric # else
353065504Seric hard_syslog(pri, msg, va_alist)
353165504Seric 	int pri;
353265504Seric 	char *msg;
353365504Seric 	va_dcl
353465504Seric # endif
353565504Seric {
353665504Seric 	int i;
353765504Seric 	char buf[SYSLOG_BUFSIZE * 2];
353865504Seric 	VA_LOCAL_DECL;
353965504Seric 
354065504Seric 	VA_START(msg);
354165504Seric 	vsprintf(buf, msg, ap);
354265504Seric 	VA_END;
354365504Seric 
354465504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
354565504Seric 		continue;
354665504Seric }
354765504Seric 
354865504Seric #endif
354968696Seric /*
355068696Seric **  LOCAL_HOSTNAME_LENGTH
355168696Seric **
355268696Seric **	This is required to get sendmail to compile against BIND 4.9.x
355368696Seric **	on Ultrix.
355468696Seric */
355568696Seric 
355668696Seric #if defined(ultrix) && NAMED_BIND
355768696Seric 
355868696Seric # include <resolv.h>
355968696Seric # if __RES >= 19931104
356068696Seric 
356168696Seric int
local_hostname_length(hostname)356268696Seric local_hostname_length(hostname)
356368696Seric 	char *hostname;
356468696Seric {
356568696Seric 	int len_host, len_domain;
356668696Seric 
356768696Seric 	if (!*_res.defdname)
356868696Seric 		res_init();
356968696Seric 	len_host = strlen(hostname);
357068696Seric 	len_domain = strlen(_res.defdname);
357168696Seric 	if (len_host > len_domain &&
357268696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
357368696Seric 	    hostname[len_host - len_domain - 1] == '.')
357468696Seric 		return len_host - len_domain - 1;
357568696Seric 	else
357668696Seric 		return 0;
357768696Seric }
357868696Seric 
357968696Seric # endif
358068696Seric #endif
358168741Seric /*
358268741Seric **  Compile-Time options
358368741Seric */
358468741Seric 
358568741Seric char	*CompileOptions[] =
358668741Seric {
358768741Seric #if HESIOD
358868741Seric 	"HESIOD",
358968741Seric #endif
359069883Seric #ifdef LOG
359168741Seric 	"LOG",
359268741Seric #endif
359368741Seric #if MATCHGECOS
359468741Seric 	"MATCHGECOS",
359568741Seric #endif
359668741Seric #if NAMED_BIND
359768741Seric 	"NAMED_BIND",
359868741Seric #endif
359968741Seric #if NDBM
360068741Seric 	"NDBM",
360168741Seric #endif
360268741Seric #if NETINET
360368741Seric 	"NETINET",
360468741Seric #endif
360568741Seric #if NETINFO
360668741Seric 	"NETINFO",
360768741Seric #endif
360868741Seric #if NETISO
360968741Seric 	"NETISO",
361068741Seric #endif
361168741Seric #if NETNS
361268741Seric 	"NETNS",
361368741Seric #endif
361468741Seric #if NETUNIX
361568741Seric 	"NETUNIX",
361668741Seric #endif
361768741Seric #if NETX25
361868741Seric 	"NETX25",
361968741Seric #endif
362068741Seric #if NEWDB
362168741Seric 	"NEWDB",
362268741Seric #endif
362368741Seric #if NIS
362468741Seric 	"NIS",
362568741Seric #endif
362669518Seric #if NISPLUS
362769518Seric 	"NISPLUS",
362869518Seric #endif
362968741Seric #if SCANF
363068741Seric 	"SCANF",
363168741Seric #endif
363268741Seric #if SUID_ROOT_FILES_OK
363368741Seric 	"SUID_ROOT_FILES_OK",
363468741Seric #endif
363568741Seric #if USERDB
363668741Seric 	"USERDB",
363768741Seric #endif
363868741Seric #if XDEBUG
363968741Seric 	"XDEBUG",
364068741Seric #endif
364168741Seric #if XLA
364268741Seric 	"XLA",
364368741Seric #endif
364468741Seric 	NULL
364568741Seric };
364669817Seric 
364769817Seric 
364869817Seric /*
364969817Seric **  OS compile options.
365069817Seric */
365169817Seric 
365269817Seric char	*OsCompileOptions[] =
365369817Seric {
365469817Seric #if HASFLOCK
365569817Seric 	"HASFLOCK",
365669817Seric #endif
365769817Seric #if HASGETUSERSHELL
365869817Seric 	"HASGETUSERSHELL",
365969817Seric #endif
366069817Seric #if HASINITGROUPS
366169817Seric 	"HASINITGROUPS",
366269817Seric #endif
366369817Seric #if HASLSTAT
366469817Seric 	"HASLSTAT",
366569817Seric #endif
366669817Seric #if HASSETREUID
366769817Seric 	"HASSETREUID",
366869817Seric #endif
366969817Seric #if HASSETSID
367069817Seric 	"HASSETSID",
367169817Seric #endif
367269817Seric #if HASSETVBUF
367369817Seric 	"HASSETVBUF",
367469817Seric #endif
367569817Seric #if HASUNAME
367669817Seric 	"HASUNAME",
367769817Seric #endif
367869817Seric #if IDENTPROTO
367969817Seric 	"IDENTPROTO",
368069817Seric #endif
368169817Seric #if IP_SRCROUTE
368269817Seric 	"IP_SRCROUTE",
368369817Seric #endif
368469817Seric #if SYS5SETPGRP
368569817Seric 	"SYS5SETPGRP",
368669817Seric #endif
368769817Seric #if SYSTEM5
368869817Seric 	"SYSTEM5",
368969817Seric #endif
369069817Seric #if USESETEUID
369169817Seric 	"USESETEUID",
369269817Seric #endif
369369817Seric 	NULL
369469817Seric };
3695