xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69769)
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*69769Seric static char sccsid[] = "@(#)conf.c	8.182 (Berkeley) 05/29/95";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
17404Seric 
18294Seric /*
193309Seric **  CONF.C -- Sendmail Configuration Tables.
20294Seric **
21294Seric **	Defines the configuration of this installation.
22294Seric **
231388Seric **	Configuration Variables:
242897Seric **		HdrInfo -- a table describing well-known header fields.
252897Seric **			Each entry has the field name and some flags,
264147Seric **			which are described in sendmail.h.
274093Seric **
284093Seric **	Notes:
294093Seric **		I have tried to put almost all the reasonable
304093Seric **		configuration information into the configuration
314093Seric **		file read at runtime.  My intent is that anything
324093Seric **		here is a function of the version of UNIX you
334093Seric **		are running, or is really static -- for example
344093Seric **		the headers are a superset of widely used
354093Seric **		protocols.  If you find yourself playing with
364093Seric **		this file too much, you may be making a mistake!
37294Seric */
38294Seric 
39294Seric 
40294Seric 
41294Seric 
424437Seric /*
432897Seric **  Header info table
443057Seric **	Final (null) entry contains the flags used for any other field.
454147Seric **
464147Seric **	Not all of these are actually handled specially by sendmail
474147Seric **	at this time.  They are included as placeholders, to let
484147Seric **	you know that "someday" I intend to have sendmail do
494147Seric **	something with them.
502897Seric */
512897Seric 
522897Seric struct hdrinfo	HdrInfo[] =
532897Seric {
548060Seric 		/* originator fields, most to least significant  */
5568508Seric 	"resent-sender",		H_FROM|H_RESENT,
5668508Seric 	"resent-from",			H_FROM|H_RESENT,
5768508Seric 	"resent-reply-to",		H_FROM|H_RESENT,
5868508Seric 	"sender",			H_FROM,
5968508Seric 	"from",				H_FROM,
6068508Seric 	"reply-to",			H_FROM,
6168508Seric 	"full-name",			H_ACHECK,
6268508Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
6368508Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6458796Seric 
658060Seric 		/* destination fields */
6668508Seric 	"to",				H_RCPT,
6768508Seric 	"resent-to",			H_RCPT|H_RESENT,
6868508Seric 	"cc",				H_RCPT,
6968508Seric 	"resent-cc",			H_RCPT|H_RESENT,
7068508Seric 	"bcc",				H_RCPT|H_STRIPVAL,
7168508Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
7268508Seric 	"apparently-to",		H_RCPT,
7358796Seric 
748060Seric 		/* message identification and control */
7568508Seric 	"message-id",			0,
7668508Seric 	"resent-message-id",		H_RESENT,
7768508Seric 	"message",			H_EOH,
7868508Seric 	"text",				H_EOH,
7958796Seric 
8011417Seric 		/* date fields */
8168508Seric 	"date",				0,
8268508Seric 	"resent-date",			H_RESENT,
8358796Seric 
848060Seric 		/* trace fields */
8568508Seric 	"received",			H_TRACE|H_FORCE,
8668508Seric 	"x400-received",		H_TRACE|H_FORCE,
8768508Seric 	"via",				H_TRACE|H_FORCE,
8868508Seric 	"mail-from",			H_TRACE|H_FORCE,
898060Seric 
9058796Seric 		/* miscellaneous fields */
9168508Seric 	"comments",			H_FORCE,
9268508Seric 	"return-path",			H_FORCE|H_ACHECK,
9368508Seric 	"content-transfer-encoding",	H_CTE,
9468508Seric 	"content-type",			H_CTYPE,
9568706Seric 	"content-length",		H_ACHECK,
9658796Seric 
979055Seric 	NULL,			0,
982897Seric };
994166Seric 
1004166Seric 
1014166Seric 
1024282Seric /*
1034282Seric **  Location of system files/databases/etc.
1044282Seric */
1054282Seric 
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12264333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12364333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12566783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12658082Seric 	"goaway",		PRIV_GOAWAY,
12758789Seric 	NULL,			0,
12858082Seric };
12958082Seric 
13058082Seric 
13158082Seric 
13258082Seric /*
13324943Seric **  Miscellaneous stuff.
1349039Seric */
1359039Seric 
13624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13724943Seric /*
13824943Seric **  SETDEFAULTS -- set default values
13924943Seric **
14024943Seric **	Because of the way freezing is done, these must be initialized
14124943Seric **	using direct code.
14224943Seric **
14324943Seric **	Parameters:
14458734Seric **		e -- the default envelope.
14524943Seric **
14624943Seric **	Returns:
14724943Seric **		none.
14824943Seric **
14924943Seric **	Side Effects:
15024943Seric **		Initializes a bunch of global variables to their
15124943Seric **		default values.
15224943Seric */
15324943Seric 
15458737Seric #define DAYS		* 24 * 60 * 60
15558737Seric 
15668508Seric void
15758734Seric setdefaults(e)
15858734Seric 	register ENVELOPE *e;
15924943Seric {
16068508Seric 	int i;
16168508Seric 	extern void inittimeouts();
16268508Seric 	extern void setdefuser();
16368508Seric 	extern void setupmaps();
16468508Seric 	extern void setupmailers();
16568508Seric 
16657438Seric 	SpaceSub = ' ';				/* option B */
16757438Seric 	QueueLA = 8;				/* option x */
16857438Seric 	RefuseLA = 12;				/* option X */
16957438Seric 	WkRecipFact = 30000L;			/* option y */
17057438Seric 	WkClassFact = 1800L;			/* option z */
17157438Seric 	WkTimeFact = 90000L;			/* option Z */
17257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17457438Seric 						/* option F */
17557438Seric 	DefUid = 1;				/* option u */
17657438Seric 	DefGid = 1;				/* option g */
17757438Seric 	CheckpointInterval = 10;		/* option C */
17857438Seric 	MaxHopCount = 25;			/* option h */
17958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
18168508Seric 	SevenBitInput = FALSE;			/* option 7 */
18257438Seric 	MaxMciCache = 1;			/* option k */
18357438Seric 	MciCacheTimeout = 300;			/* option K */
18457438Seric 	LogLevel = 9;				/* option L */
18568508Seric 	inittimeouts(NULL);			/* option r */
18658853Seric 	PrivacyFlags = 0;			/* option p */
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
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
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
26057402Seric setupmailers()
26157402Seric {
26257402Seric 	char buf[100];
26368508Seric 	extern void makemailer();
26457402Seric 
26569721Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=DNS/RFC822/X-Unix, A=sh -c $u");
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
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 
33568508Seric #ifdef 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
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");
44568508Seric 			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");
45268508Seric 			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");
46068508Seric 			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");
46868508Seric 			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
48668508Seric 		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");
50068508Seric 			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");
50768508Seric 			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");
51568508Seric 			makemapentry(buf);
51668508Seric 		}
51768508Seric #endif
51868508Seric 	}
51968508Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
52068508Seric 	{
52168508Seric 		strcpy(buf, "aliases switch aliases");
52268508Seric 		makemapentry(buf);
52368508Seric 	}
52468508Seric 	strcpy(buf, "switch:aliases");
52568508Seric 	setalias(buf);
52668508Seric 
52768508Seric #if 0		/* "user" map class is a better choice */
52868508Seric 	/*
52968508Seric 	**  Set up default users maps.
53068508Seric 	*/
53168508Seric 
53268508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
53368508Seric 	for (i = 0; i < nmaps; i++)
53468508Seric 	{
53568508Seric 		if (strcmp(maptype[i], "files") == 0 &&
53668508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
53768508Seric 		{
53868508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
53968508Seric 			makemapentry(buf);
54068508Seric 		}
54168508Seric #ifdef NISPLUS
54268508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
54368508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
54468508Seric 		{
54568508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
54668508Seric 			makemapentry(buf);
54768508Seric 		}
54868508Seric #endif
54968508Seric #ifdef NIS
55068508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
55168508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
55268508Seric 		{
55368508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
55468508Seric 			makemapentry(buf);
55568508Seric 		}
55668508Seric #endif
55768508Seric #ifdef HESIOD
55868508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
55968508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
56068508Seric 		{
56168508Seric 			strcpy(buf, "users.hesiod hesiod");
56268508Seric 			makemapentry(buf);
56368508Seric 		}
56468508Seric #endif
56568508Seric 	}
56668508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
56768508Seric 	{
56868508Seric 		strcpy(buf, "users switch -m passwd");
56968508Seric 		makemapentry(buf);
57068508Seric 	}
57168508Seric #endif
57268508Seric }
57368508Seric /*
57468508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
57568508Seric **
57668508Seric **	This is the system-dependent interface to the service switch.
57768508Seric **
57868508Seric **	Parameters:
57968508Seric **		service -- the name of the service of interest.
58068508Seric **		maptype -- an out-array of strings containing the types
58168508Seric **			of access to use for this service.  There can
58268508Seric **			be at most MAXMAPSTACK types for a single service.
58368508Seric **		mapreturn -- an out-array of return information bitmaps
58468508Seric **			for the map.
58568508Seric **
58668508Seric **	Returns:
58768508Seric **		The number of map types filled in, or -1 for failure.
58868508Seric */
58968508Seric 
59068508Seric #ifdef SOLARIS
59168508Seric # include <nsswitch.h>
59268508Seric #endif
59368508Seric 
59468508Seric #if defined(ultrix) || defined(__osf__)
59568508Seric # include <sys/svcinfo.h>
59668508Seric #endif
59768508Seric 
59868508Seric int
59968508Seric switch_map_find(service, maptype, mapreturn)
60068508Seric 	char *service;
60168508Seric 	char *maptype[MAXMAPSTACK];
60268508Seric 	short mapreturn[MAXMAPACTIONS];
60368508Seric {
60468508Seric 	register FILE *fp;
60568508Seric 	int svcno;
60668508Seric 	static char buf[MAXLINE];
60768508Seric 
60868508Seric #ifdef SOLARIS
60968508Seric 	struct __nsw_switchconfig *nsw_conf;
61068508Seric 	enum __nsw_parse_err pserr;
61168508Seric 	struct __nsw_lookup *lk;
61268508Seric 	int nsw_rc;
61368508Seric 	static struct __nsw_lookup lkp0 =
61468508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
61568508Seric 	static struct __nsw_switchconfig lkp_default =
61668508Seric 		{ 0, "sendmail", 3, &lkp0 };
61768508Seric 
61868508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
61968508Seric 		lk = lkp_default.lookups;
62068508Seric 	else
62168508Seric 		lk = nsw_conf->lookups;
62268508Seric 	svcno = 0;
62368508Seric 	while (lk != NULL)
62468508Seric 	{
62568508Seric 		maptype[svcno] = lk->service_name;
62668508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
62768508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
62868508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
62968508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
63068508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
63168508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
63268508Seric 		svcno++;
63368508Seric 		lk = lk->next;
63468508Seric 	}
63568508Seric 	return svcno;
63668508Seric #endif
63768508Seric 
63868508Seric #if defined(ultrix) || defined(__osf__)
63968508Seric 	struct svcinfo *svcinfo;
64068508Seric 	int svc;
64168508Seric 
64269401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
64369401Seric 		mapreturn[svcno] = 0;
64469401Seric 
64568508Seric 	svcinfo = getsvc();
64668508Seric 	if (svcinfo == NULL)
64768508Seric 		goto punt;
64868508Seric 	if (strcmp(service, "hosts") == 0)
64968508Seric 		svc = SVC_HOSTS;
65068508Seric 	else if (strcmp(service, "aliases") == 0)
65168508Seric 		svc = SVC_ALIASES;
65268508Seric 	else if (strcmp(service, "passwd") == 0)
65368508Seric 		svc = SVC_PASSWD;
65468508Seric 	else
65568508Seric 		return -1;
65668508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
65768508Seric 	{
65868508Seric 		switch (svcinfo->svcpath[svc][svcno])
65968508Seric 		{
66068508Seric 		  case SVC_LOCAL:
66168508Seric 			maptype[svcno] = "files";
66268508Seric 			break;
66368508Seric 
66468508Seric 		  case SVC_YP:
66568508Seric 			maptype[svcno] = "nis";
66668508Seric 			break;
66768508Seric 
66868508Seric 		  case SVC_BIND:
66968508Seric 			maptype[svcno] = "dns";
67068508Seric 			break;
67168508Seric 
67268508Seric #ifdef SVC_HESIOD
67368508Seric 		  case SVC_HESIOD:
67468508Seric 			maptype[svcno] = "hesiod";
67568508Seric 			break;
67668508Seric #endif
67768508Seric 
67868508Seric 		  case SVC_LAST:
67968508Seric 			return svcno;
68068508Seric 		}
68168508Seric 	}
68268508Seric 	return svcno;
68368508Seric #endif
68468508Seric 
68568508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
68668508Seric 	/*
68768508Seric 	**  Fall-back mechanism.
68868508Seric 	*/
68968508Seric 
69069401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
69169401Seric 		mapreturn[svcno] = 0;
69269401Seric 
69368508Seric 	svcno = 0;
69468508Seric 	fp = fopen(ServiceSwitchFile, "r");
69568508Seric 	if (fp != NULL)
69668508Seric 	{
69768508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
69868508Seric 		{
69968508Seric 			register char *p;
70068508Seric 
70168508Seric 			p = strpbrk(buf, "#\n");
70268508Seric 			if (p != NULL)
70368508Seric 				*p = '\0';
70468508Seric 			p = strpbrk(buf, " \t");
70568508Seric 			if (p != NULL)
70668508Seric 				*p++ = '\0';
70768508Seric 			if (strcmp(buf, service) != 0)
70868508Seric 				continue;
70968508Seric 
71068508Seric 			/* got the right service -- extract data */
71168508Seric 			do
71268508Seric 			{
71368508Seric 				while (isspace(*p))
71468508Seric 					p++;
71568508Seric 				if (*p == '\0')
71668508Seric 					break;
71768508Seric 				maptype[svcno++] = p;
71868508Seric 				p = strpbrk(p, " \t");
71968508Seric 				if (p != NULL)
72068508Seric 					*p++ = '\0';
72168508Seric 			} while (p != NULL);
72268508Seric 			break;
72368508Seric 		}
72468508Seric 		fclose(fp);
72568508Seric 		return svcno;
72668508Seric 	}
72768508Seric #endif
72868508Seric 
72968508Seric 	/* if the service file doesn't work, use an absolute fallback */
73068508Seric   punt:
73169401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
73269401Seric 		mapreturn[svcno] = 0;
73369401Seric 	svcno = 0;
73468508Seric 	if (strcmp(service, "aliases") == 0)
73568508Seric 	{
73668628Seric 		maptype[svcno++] = "files";
73768628Seric #ifdef AUTO_NIS_ALIASES
73868628Seric # ifdef NISPLUS
73968628Seric 		maptype[svcno++] = "nisplus";
74068628Seric # endif
74168628Seric # ifdef NIS
74268628Seric 		maptype[svcno++] = "nis";
74368628Seric # endif
74468628Seric #endif
74568628Seric 		return svcno;
74668508Seric 	}
74768508Seric 	if (strcmp(service, "hosts") == 0)
74868508Seric 	{
74968508Seric # if NAMED_BIND
75068508Seric 		maptype[svcno++] = "dns";
75168508Seric # else
75268508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
75368508Seric 		/* SunOS */
75468508Seric 		maptype[svcno++] = "nis";
75568508Seric #  endif
75668508Seric # endif
75768508Seric 		maptype[svcno++] = "files";
75868508Seric 		return svcno;
75968508Seric 	}
76068508Seric 	return -1;
76168508Seric }
76268508Seric /*
7639369Seric **  USERNAME -- return the user id of the logged in user.
7649369Seric **
7659369Seric **	Parameters:
7669369Seric **		none.
7679369Seric **
7689369Seric **	Returns:
7699369Seric **		The login name of the logged in user.
7709369Seric **
7719369Seric **	Side Effects:
7729369Seric **		none.
7739369Seric **
7749369Seric **	Notes:
7759369Seric **		The return value is statically allocated.
7769369Seric */
7779369Seric 
7789369Seric char *
7799369Seric username()
7809369Seric {
78117469Seric 	static char *myname = NULL;
7829369Seric 	extern char *getlogin();
78319904Smiriam 	register struct passwd *pw;
7849369Seric 
78517469Seric 	/* cache the result */
78617469Seric 	if (myname == NULL)
78717469Seric 	{
78817469Seric 		myname = getlogin();
78917469Seric 		if (myname == NULL || myname[0] == '\0')
79017469Seric 		{
79168693Seric 			pw = sm_getpwuid(RealUid);
79217469Seric 			if (pw != NULL)
79340993Sbostic 				myname = newstr(pw->pw_name);
79417469Seric 		}
79519904Smiriam 		else
79619904Smiriam 		{
79763787Seric 			uid_t uid = RealUid;
79819873Smiriam 
79940993Sbostic 			myname = newstr(myname);
80068693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
80158736Seric 			      (uid != 0 && uid != pw->pw_uid))
80219904Smiriam 			{
80368693Seric 				pw = sm_getpwuid(uid);
80424945Seric 				if (pw != NULL)
80540993Sbostic 					myname = newstr(pw->pw_name);
80619873Smiriam 			}
80719873Smiriam 		}
80817469Seric 		if (myname == NULL || myname[0] == '\0')
80917469Seric 		{
81058151Seric 			syserr("554 Who are you?");
81117469Seric 			myname = "postmaster";
81217469Seric 		}
81317469Seric 	}
81417469Seric 
81517469Seric 	return (myname);
8169369Seric }
8179369Seric /*
8184190Seric **  TTYPATH -- Get the path of the user's tty
819294Seric **
820294Seric **	Returns the pathname of the user's tty.  Returns NULL if
821294Seric **	the user is not logged in or if s/he has write permission
822294Seric **	denied.
823294Seric **
824294Seric **	Parameters:
825294Seric **		none
826294Seric **
827294Seric **	Returns:
828294Seric **		pathname of the user's tty.
829294Seric **		NULL if not logged in or write permission denied.
830294Seric **
831294Seric **	Side Effects:
832294Seric **		none.
833294Seric **
834294Seric **	WARNING:
835294Seric **		Return value is in a local buffer.
836294Seric **
837294Seric **	Called By:
838294Seric **		savemail
839294Seric */
840294Seric 
841294Seric char *
842294Seric ttypath()
843294Seric {
844294Seric 	struct stat stbuf;
845294Seric 	register char *pathn;
846294Seric 	extern char *ttyname();
8474081Seric 	extern char *getlogin();
848294Seric 
849294Seric 	/* compute the pathname of the controlling tty */
8509369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8519369Seric 	    (pathn = ttyname(0)) == NULL)
852294Seric 	{
853294Seric 		errno = 0;
854294Seric 		return (NULL);
855294Seric 	}
856294Seric 
857294Seric 	/* see if we have write permission */
8582967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
859294Seric 	{
860294Seric 		errno = 0;
861294Seric 		return (NULL);
862294Seric 	}
863294Seric 
864294Seric 	/* see if the user is logged in */
865294Seric 	if (getlogin() == NULL)
866294Seric 		return (NULL);
867294Seric 
868294Seric 	/* looks good */
869294Seric 	return (pathn);
870294Seric }
8712967Seric /*
8722967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8732967Seric **
8742967Seric **	This routine can be supplied on a per-installation basis
8752967Seric **	to determine whether a person is allowed to send a message.
8762967Seric **	This allows restriction of certain types of internet
8772967Seric **	forwarding or registration of users.
8782967Seric **
8792967Seric **	If the hosts are found to be incompatible, an error
88068857Seric **	message should be given using "usrerr" and an EX_ code
88168857Seric **	should be returned.  You can also set to->q_status to
88268857Seric **	a DSN-style status code.
8832967Seric **
88468559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
88568559Seric **	body during the return-to-sender function; this should be done
88668559Seric **	on huge messages.  This bit may already be set by the ESMTP
88768559Seric **	protocol.
8884288Seric **
8892967Seric **	Parameters:
8902967Seric **		to -- the person being sent to.
8912967Seric **
8922967Seric **	Returns:
89357459Seric **		an exit status
8942967Seric **
8952967Seric **	Side Effects:
8962967Seric **		none (unless you include the usrerr stuff)
8972967Seric */
8982967Seric 
89968508Seric int
90055012Seric checkcompat(to, e)
9012967Seric 	register ADDRESS *to;
90255012Seric 	register ENVELOPE *e;
9032967Seric {
90412133Seric # ifdef lint
90512133Seric 	if (to == NULL)
90612133Seric 		to++;
90763834Seric # endif /* lint */
90864402Seric 
90964402Seric 	if (tTd(49, 1))
91064402Seric 		printf("checkcompat(to=%s, from=%s)\n",
91164402Seric 			to->q_paddr, e->e_from.q_paddr);
91264402Seric 
91310698Seric # ifdef EXAMPLE_CODE
91410698Seric 	/* this code is intended as an example only */
9154437Seric 	register STAB *s;
9164437Seric 
9174437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
91868508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9199369Seric 	    to->q_mailer == s->s_mailer)
9204437Seric 	{
92158151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
92268559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
92368857Seric 		to->q_status = "5.7.1";
92457459Seric 		return (EX_UNAVAILABLE);
9254437Seric 	}
92656795Seric # endif /* EXAMPLE_CODE */
92757459Seric 	return (EX_OK);
9282967Seric }
9299369Seric /*
93064035Seric **  SETSIGNAL -- set a signal handler
93164035Seric **
93264035Seric **	This is essentially old BSD "signal(3)".
93364035Seric */
93464035Seric 
93564561Seric sigfunc_t
93664035Seric setsignal(sig, handler)
93764035Seric 	int sig;
93864561Seric 	sigfunc_t handler;
93964035Seric {
94064380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
94164035Seric 	return signal(sig, handler);
94264035Seric #else
94364035Seric 	struct sigaction n, o;
94464035Seric 
94564035Seric 	bzero(&n, sizeof n);
94664035Seric 	n.sa_handler = handler;
94768508Seric # ifdef SA_RESTART
94868508Seric 	n.sa_flags = SA_RESTART;
94968508Seric # endif
95064035Seric 	if (sigaction(sig, &n, &o) < 0)
95164035Seric 		return SIG_ERR;
95264035Seric 	return o.sa_handler;
95364035Seric #endif
95464035Seric }
95564035Seric /*
9569369Seric **  HOLDSIGS -- arrange to hold all signals
9579369Seric **
9589369Seric **	Parameters:
9599369Seric **		none.
9609369Seric **
9619369Seric **	Returns:
9629369Seric **		none.
9639369Seric **
9649369Seric **	Side Effects:
9659369Seric **		Arranges that signals are held.
9669369Seric */
9679369Seric 
96868508Seric void
9699369Seric holdsigs()
9709369Seric {
9719369Seric }
9729369Seric /*
9739369Seric **  RLSESIGS -- arrange to release all signals
9749369Seric **
9759369Seric **	This undoes the effect of holdsigs.
9769369Seric **
9779369Seric **	Parameters:
9789369Seric **		none.
9799369Seric **
9809369Seric **	Returns:
9819369Seric **		none.
9829369Seric **
9839369Seric **	Side Effects:
9849369Seric **		Arranges that signals are released.
9859369Seric */
9869369Seric 
98768508Seric void
9889369Seric rlsesigs()
9899369Seric {
9909369Seric }
99114872Seric /*
99264705Seric **  INIT_MD -- do machine dependent initializations
99364705Seric **
99464705Seric **	Systems that have global modes that should be set should do
99564705Seric **	them here rather than in main.
99664705Seric */
99764705Seric 
99864705Seric #ifdef _AUX_SOURCE
99964705Seric # include	<compat.h>
100064705Seric #endif
100164705Seric 
100268508Seric void
100364825Seric init_md(argc, argv)
100464825Seric 	int argc;
100564825Seric 	char **argv;
100664705Seric {
100764705Seric #ifdef _AUX_SOURCE
100864705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
100964705Seric #endif
101068508Seric 
101168508Seric #ifdef VENDOR_DEFAULT
101268508Seric 	VendorCode = VENDOR_DEFAULT;
101368508Seric #else
101468508Seric 	VendorCode = VENDOR_BERKELEY;
101568508Seric #endif
101664705Seric }
101764705Seric /*
101868508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
101968508Seric **
102068508Seric **	Called once, on startup.
102168508Seric **
102268508Seric **	Parameters:
102368508Seric **		e -- the global envelope.
102468508Seric **
102568508Seric **	Returns:
102668508Seric **		none.
102768508Seric **
102868508Seric **	Side Effects:
102968508Seric **		vendor-dependent.
103068508Seric */
103168508Seric 
103268508Seric void
103368508Seric init_vendor_macros(e)
103468508Seric 	register ENVELOPE *e;
103568508Seric {
103668508Seric }
103768508Seric /*
103814872Seric **  GETLA -- get the current load average
103914872Seric **
104014881Seric **	This code stolen from la.c.
104114881Seric **
104214872Seric **	Parameters:
104314872Seric **		none.
104414872Seric **
104514872Seric **	Returns:
104614872Seric **		The current load average as an integer.
104714872Seric **
104814872Seric **	Side Effects:
104914872Seric **		none.
105014872Seric */
105114872Seric 
105251920Seric /* try to guess what style of load average we have */
105351920Seric #define LA_ZERO		1	/* always return load average as zero */
105464360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
105551920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
105651920Seric #define LA_SUBR		4	/* call getloadavg */
105764295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
105864360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
105966301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
106069462Seric #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
106169462Seric #define LA_DGUX		9	/* special DGUX implementation */
106269462Seric #define LA_HPUX		10	/* special HPUX implementation */
106314872Seric 
106463962Seric /* do guesses based on general OS type */
106551920Seric #ifndef LA_TYPE
106663962Seric # define LA_TYPE	LA_ZERO
106751920Seric #endif
106851920Seric 
106969462Seric #ifndef FSHIFT
107069462Seric # if defined(unixpc)
107169462Seric #  define FSHIFT	5
107269462Seric # endif
107369462Seric 
107469462Seric # if defined(__alpha) || defined(IRIX)
107569462Seric #  define FSHIFT	10
107669462Seric # endif
107769462Seric 
107869462Seric # if defined(_AIX3)
107969462Seric #  define FSHIFT	16
108069462Seric # endif
108169462Seric #endif
108269462Seric 
108369462Seric #ifndef FSHIFT
108469462Seric # define FSHIFT		8
108569462Seric #endif
108669462Seric 
108769462Seric #ifndef FSCALE
108869462Seric # define FSCALE		(1 << FSHIFT)
108969462Seric #endif
109069462Seric 
109169654Seric #ifndef LA_AVENRUN
109269654Seric # ifdef SYSTEM5
109369654Seric #  define LA_AVENRUN	"avenrun"
109469654Seric # else
109569654Seric #  define LA_AVENRUN	"_avenrun"
109669654Seric # endif
109769654Seric #endif
109869654Seric 
109964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
110051920Seric 
110114872Seric #include <nlist.h>
110214872Seric 
110368508Seric #ifdef IRIX64
110468508Seric # define nlist		nlist64
110568508Seric #endif
110668508Seric 
110751920Seric /* _PATH_UNIX should be defined in <paths.h> */
110851920Seric #ifndef _PATH_UNIX
110963962Seric # if defined(SYSTEM5)
111063962Seric #  define _PATH_UNIX	"/unix"
111163962Seric # else
111263962Seric #  define _PATH_UNIX	"/vmunix"
111363962Seric # endif
111451920Seric #endif
111551920Seric 
111669462Seric struct nlist	Nl[] =
111714872Seric {
111851920Seric 	{ LA_AVENRUN },
111914872Seric #define	X_AVENRUN	0
112014872Seric 	{ 0 },
112114872Seric };
112214872Seric 
112314872Seric getla()
112414872Seric {
112514872Seric 	static int kmem = -1;
112651920Seric #if LA_TYPE == LA_INT
112724943Seric 	long avenrun[3];
112851920Seric #else
112964360Seric # if LA_TYPE == LA_SHORT
113064360Seric 	short avenrun[3];
113164360Seric # else
113251920Seric 	double avenrun[3];
113364360Seric # endif
113451920Seric #endif
113569462Seric 	extern int errno;
113625615Seric 	extern off_t lseek();
113714872Seric 
113814872Seric 	if (kmem < 0)
113914872Seric 	{
114024945Seric 		kmem = open("/dev/kmem", 0, 0);
114114872Seric 		if (kmem < 0)
114257736Seric 		{
114357736Seric 			if (tTd(3, 1))
114457736Seric 				printf("getla: open(/dev/kmem): %s\n",
114557736Seric 					errstring(errno));
114614872Seric 			return (-1);
114757736Seric 		}
114851920Seric 		(void) fcntl(kmem, F_SETFD, 1);
114969462Seric 
115068508Seric #ifdef _AIX3
115168508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
115268508Seric #else
115357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
115468508Seric #endif
115557736Seric 		{
115657736Seric 			if (tTd(3, 1))
115757736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
115857736Seric 					errstring(errno));
115914872Seric 			return (-1);
116057736Seric 		}
116159253Seric 		if (Nl[X_AVENRUN].n_value == 0)
116259253Seric 		{
116359253Seric 			if (tTd(3, 1))
116459253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
116559253Seric 					_PATH_UNIX, LA_AVENRUN);
116659253Seric 			return (-1);
116759253Seric 		}
116868508Seric #ifdef NAMELISTMASK
116968508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
117066763Seric #endif
117114872Seric 	}
117257736Seric 	if (tTd(3, 20))
117357736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
117468508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
117523118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
117619967Seric 	{
117719967Seric 		/* thank you Ian */
117857736Seric 		if (tTd(3, 1))
117957736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
118019967Seric 		return (-1);
118119967Seric 	}
118269462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
118357736Seric 	if (tTd(3, 5))
118457736Seric 	{
118557736Seric 		printf("getla: avenrun = %d", avenrun[0]);
118657736Seric 		if (tTd(3, 15))
118757736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
118857736Seric 		printf("\n");
118957736Seric 	}
119057736Seric 	if (tTd(3, 1))
119157736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119224943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119369462Seric # else /* LA_TYPE == LA_FLOAT */
119457736Seric 	if (tTd(3, 5))
119557736Seric 	{
119657736Seric 		printf("getla: avenrun = %g", avenrun[0]);
119757736Seric 		if (tTd(3, 15))
119857736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
119957736Seric 		printf("\n");
120057736Seric 	}
120157736Seric 	if (tTd(3, 1))
120257736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
120351920Seric 	return ((int) (avenrun[0] + 0.5));
120469462Seric # endif
120514872Seric }
120614872Seric 
120769462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
120851773Seric 
120969462Seric #if LA_TYPE == LA_READKSYM
121064718Seric 
121169473Seric # include <sys/ksym.h>
121269473Seric 
121369462Seric getla()
121469462Seric {
121569462Seric 	static int kmem = -1;
121669462Seric 	long avenrun[3];
121769462Seric 	extern int errno;
121869462Seric 	struct mioc_rksym mirk;
121964718Seric 
122069462Seric 	if (kmem < 0)
122169462Seric 	{
122269462Seric 		kmem = open("/dev/kmem", 0, 0);
122369462Seric 		if (kmem < 0)
122469462Seric 		{
122569462Seric 			if (tTd(3, 1))
122669462Seric 				printf("getla: open(/dev/kmem): %s\n",
122769462Seric 					errstring(errno));
122869462Seric 			return (-1);
122969462Seric 		}
123069462Seric 		(void) fcntl(kmem, F_SETFD, 1);
123169462Seric 	}
123269462Seric 	mirk.mirk_symname = LA_AVENRUN;
123369462Seric 	mirk.mirk_buf = avenrun;
123469462Seric 	mirk.mirk_buflen = sizeof(avenrun);
123569462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
123669462Seric 	{
123769462Seric 		if (tTd(3, 1))
123869462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
123969462Seric 				errstring(errno));
124069462Seric 		return -1;
124169462Seric 	}
124269462Seric 	if (tTd(3, 5))
124369462Seric 	{
124469462Seric 		printf("getla: avenrun = %d", avenrun[0]);
124569462Seric 		if (tTd(3, 15))
124669462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
124769462Seric 		printf("\n");
124869462Seric 	}
124969462Seric 	if (tTd(3, 1))
125069462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125169462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125269462Seric }
125369462Seric 
125469462Seric #endif /* LA_TYPE == LA_READKSYM */
125569462Seric 
125669462Seric #if LA_TYPE == LA_DGUX
125769462Seric 
125869462Seric # include <sys/dg_sys_info.h>
125969462Seric 
126068508Seric int
126168508Seric getla()
126264718Seric {
126364718Seric 	struct dg_sys_info_load_info load_info;
126464718Seric 
126564718Seric 	dg_sys_info((long *)&load_info,
126664718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
126764718Seric 
126868508Seric         if (tTd(3, 1))
126968508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
127068508Seric 
127164718Seric 	return((int) (load_info.one_minute + 0.5));
127264718Seric }
127364718Seric 
127469462Seric #endif /* LA_TYPE == LA_DGUX */
127564718Seric 
127669462Seric #if LA_TYPE == LA_HPUX
127769462Seric 
127869563Seric /* forward declarations to keep gcc from complaining */
127968869Seric struct pst_dynamic;
128069563Seric struct pst_status;
128169563Seric struct pst_static;
128269563Seric struct pst_vminfo;
128369563Seric struct pst_diskinfo;
128469563Seric struct pst_processor;
128569563Seric struct pst_lv;
128669563Seric struct pst_swapinfo;
128768869Seric 
128869462Seric # include <sys/param.h>
128969462Seric # include <sys/pstat.h>
129068508Seric 
129168508Seric int
129251773Seric getla()
129351773Seric {
129468508Seric 	struct pst_dynamic pstd;
129568508Seric 
129668508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
129768869Seric 			     (size_t) 1, 0) == -1)
129868508Seric 		return 0;
129968508Seric 
130068508Seric         if (tTd(3, 1))
130168508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
130268508Seric 
130368508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
130468508Seric }
130568508Seric 
130669462Seric #endif /* LA_TYPE == LA_HPUX */
130768508Seric 
130869462Seric #if LA_TYPE == LA_SUBR
130969462Seric 
131068508Seric int
131168508Seric getla()
131268508Seric {
131351920Seric 	double avenrun[3];
131451920Seric 
131551920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
131657736Seric 	{
131757736Seric 		if (tTd(3, 1))
131857736Seric 			perror("getla: getloadavg failed:");
131951920Seric 		return (-1);
132057736Seric 	}
132157736Seric 	if (tTd(3, 1))
132257736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
132351920Seric 	return ((int) (avenrun[0] + 0.5));
132451773Seric }
132551773Seric 
132669462Seric #endif /* LA_TYPE == LA_SUBR */
132769462Seric 
132864295Seric #if LA_TYPE == LA_MACH
132951773Seric 
133064295Seric /*
133165173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
133264295Seric */
133364295Seric 
133465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
133565173Seric # include <mach/mach.h>
133665173Seric #else
133765173Seric # include <mach.h>
133865173Seric #endif
133964295Seric 
134051773Seric getla()
134151773Seric {
134264295Seric 	processor_set_t default_set;
134364295Seric 	kern_return_t error;
134464295Seric 	unsigned int info_count;
134564295Seric 	struct processor_set_basic_info info;
134664295Seric 	host_t host;
134764295Seric 
134864295Seric 	error = processor_set_default(host_self(), &default_set);
134964295Seric 	if (error != KERN_SUCCESS)
135064295Seric 		return -1;
135164295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
135264295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
135364295Seric 			       &host, (processor_set_info_t)&info,
135464295Seric 			       &info_count) != KERN_SUCCESS)
135564295Seric 	{
135664295Seric 		return -1;
135764295Seric 	}
135864295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
135964295Seric }
136064295Seric 
136169462Seric #endif /* LA_TYPE == LA_MACH */
136264295Seric 
136366313Seric #if LA_TYPE == LA_PROCSTR
136464295Seric 
136566301Seric /*
136666301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
136766301Seric **  in a format like "0.15 0.12 0.06".
136866301Seric **
136966301Seric **	Initially intended for Linux.  This has been in the kernel
137066301Seric **	since at least 0.99.15.
137166301Seric */
137266301Seric 
137366301Seric # ifndef _PATH_LOADAVG
137466301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
137566301Seric # endif
137666301Seric 
137766301Seric int
137864295Seric getla()
137964295Seric {
138066319Seric 	double avenrun;
138166301Seric 	register int result;
138266319Seric 	FILE *fp;
138366301Seric 
138466319Seric 	fp = fopen(_PATH_LOADAVG, "r");
138569472Seric 	if (fp == NULL)
138666301Seric 	{
138766319Seric 		if (tTd(3, 1))
138866319Seric 			printf("getla: fopen(%s): %s\n",
138966319Seric 				_PATH_LOADAVG, errstring(errno));
139066319Seric 		return -1;
139166301Seric 	}
139266301Seric 	result = fscanf(fp, "%lf", &avenrun);
139366319Seric 	fclose(fp);
139466301Seric 	if (result != 1)
139566301Seric 	{
139666310Seric 		if (tTd(3, 1))
139766301Seric 			printf("getla: fscanf() = %d: %s\n",
139866301Seric 				result, errstring(errno));
139966301Seric 		return -1;
140066301Seric 	}
140166301Seric 
140257736Seric 	if (tTd(3, 1))
140366301Seric 		printf("getla(): %.2f\n", avenrun);
140466301Seric 
140566301Seric 	return ((int) (avenrun + 0.5));
140666301Seric }
140766301Seric 
140869462Seric #endif /* LA_TYPE == LA_PROCSTR */
140966301Seric 
141069462Seric #if LA_TYPE == LA_ZERO
141169462Seric 
141266301Seric getla()
141366301Seric {
141466301Seric 	if (tTd(3, 1))
141557736Seric 		printf("getla: ZERO\n");
141651920Seric 	return (0);
141751773Seric }
141851773Seric 
141969462Seric #endif /* LA_TYPE == LA_ZERO */
142066045Seric 
142166045Seric 
142266045Seric /*
142366045Seric  * Copyright 1989 Massachusetts Institute of Technology
142466045Seric  *
142566045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
142666045Seric  * documentation for any purpose is hereby granted without fee, provided that
142766045Seric  * the above copyright notice appear in all copies and that both that
142866045Seric  * copyright notice and this permission notice appear in supporting
142966045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
143066045Seric  * publicity pertaining to distribution of the software without specific,
143166045Seric  * written prior permission.  M.I.T. makes no representations about the
143266045Seric  * suitability of this software for any purpose.  It is provided "as is"
143366045Seric  * without express or implied warranty.
143466045Seric  *
143566045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
143666045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
143766045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143866045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
143966045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
144066045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
144166045Seric  *
144266045Seric  * Authors:  Many and varied...
144366045Seric  */
144466045Seric 
144566045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
144666045Seric #ifndef lint
144766045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
144866045Seric #endif /* !lint */
144966045Seric 
145066179Seric #ifdef apollo
145166045Seric # undef volatile
145266045Seric #    include <apollo/base.h>
145366045Seric 
145466045Seric /* ARGSUSED */
145566045Seric int getloadavg( call_data )
145666045Seric      caddr_t	call_data;	/* pointer to (double) return value */
145766045Seric {
145866045Seric      double *avenrun = (double *) call_data;
145966045Seric      int i;
146066045Seric      status_$t      st;
146166045Seric      long loadav[3];
146266045Seric      proc1_$get_loadav(loadav, &st);
146366045Seric      *avenrun = loadav[0] / (double) (1 << 16);
146466045Seric      return(0);
146566045Seric }
146666045Seric #   endif /* apollo */
146724943Seric /*
146824943Seric **  SHOULDQUEUE -- should this message be queued or sent?
146924943Seric **
147024943Seric **	Compares the message cost to the load average to decide.
147124943Seric **
147224943Seric **	Parameters:
147324943Seric **		pri -- the priority of the message in question.
147457438Seric **		ctime -- the message creation time.
147524943Seric **
147624943Seric **	Returns:
147724943Seric **		TRUE -- if this message should be queued up for the
147824943Seric **			time being.
147924943Seric **		FALSE -- if the load is low enough to send this message.
148024943Seric **
148124943Seric **	Side Effects:
148224943Seric **		none.
148324943Seric */
148424943Seric 
148524943Seric bool
148657438Seric shouldqueue(pri, ctime)
148724943Seric 	long pri;
148857438Seric 	time_t ctime;
148924943Seric {
149068508Seric 	bool rval;
149168508Seric 
149268508Seric 	if (tTd(3, 30))
149368508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
149451920Seric 	if (CurrentLA < QueueLA)
149568508Seric 	{
149668508Seric 		if (tTd(3, 30))
149768508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
149824943Seric 		return (FALSE);
149968508Seric 	}
150058132Seric 	if (CurrentLA >= RefuseLA)
150168508Seric 	{
150268508Seric 		if (tTd(3, 30))
150368508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
150458132Seric 		return (TRUE);
150568508Seric 	}
150668508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
150768508Seric 	if (tTd(3, 30))
150868508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
150968508Seric 	return rval;
151024943Seric }
151124943Seric /*
151253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
151353037Seric **
151453037Seric **	Parameters:
151553037Seric **		none.
151653037Seric **
151753037Seric **	Returns:
151853037Seric **		TRUE if incoming SMTP connections should be refused
151953037Seric **			(for now).
152053037Seric **		FALSE if we should accept new work.
152153037Seric **
152253037Seric **	Side Effects:
152353037Seric **		none.
152453037Seric */
152553037Seric 
152653037Seric bool
152753037Seric refuseconnections()
152853037Seric {
152968508Seric 	extern bool enoughspace();
153068508Seric 
153159156Seric #ifdef XLA
153259156Seric 	if (!xla_smtp_ok())
153359156Seric 		return TRUE;
153459156Seric #endif
153559156Seric 
153653037Seric 	/* this is probably too simplistic */
153768508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
153853037Seric }
153953037Seric /*
154024943Seric **  SETPROCTITLE -- set process title for ps
154124943Seric **
154224943Seric **	Parameters:
154358674Seric **		fmt -- a printf style format string.
154458674Seric **		a, b, c -- possible parameters to fmt.
154524943Seric **
154624943Seric **	Returns:
154724943Seric **		none.
154824943Seric **
154924943Seric **	Side Effects:
155024943Seric **		Clobbers argv of our main procedure so ps(1) will
155124943Seric **		display the title.
155224943Seric */
155324943Seric 
155468508Seric #define SPT_NONE	0	/* don't use it at all */
155568508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
155668508Seric #define SPT_BUILTIN	2	/* use libc builtin */
155768508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
155868508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
155969654Seric #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
156068508Seric 
156168508Seric #ifndef SPT_TYPE
156268508Seric # define SPT_TYPE	SPT_REUSEARGV
156368508Seric #endif
156468508Seric 
156568508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
156668508Seric 
156768508Seric # if SPT_TYPE == SPT_PSTAT
156858689Seric #  include <sys/pstat.h>
156958689Seric # endif
157068508Seric # if SPT_TYPE == SPT_PSSTRINGS
157159732Seric #  include <machine/vmparam.h>
157259732Seric #  include <sys/exec.h>
157368508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
157468508Seric #   undef SPT_TYPE
157568508Seric #   define SPT_TYPE	SPT_REUSEARGV
157668734Seric #  else
157768734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
157868734Seric #    define NKPDE 63
157968734Seric typedef unsigned int	*pt_entry_t;
158068734Seric #   endif
158164247Seric #  endif
158259732Seric # endif
158368508Seric 
158468508Seric # if SPT_TYPE == SPT_PSSTRINGS
158568508Seric #  define SETPROC_STATIC	static
158668508Seric # else
158759732Seric #  define SETPROC_STATIC
158859732Seric # endif
158967626Seric 
159069654Seric # if SPT_TYPE == SPT_SYSMIPS
159169654Seric #  include <sys/sysmips.h>
159269654Seric #  include <sys/sysnews.h>
159369654Seric # endif
159469654Seric 
159568508Seric # ifndef SPT_PADCHAR
159668508Seric #  define SPT_PADCHAR	' '
159768508Seric # endif
159858689Seric 
159968508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
160064843Seric 
160168508Seric #if SPT_TYPE != SPT_BUILTIN
160268508Seric 
160324943Seric /*VARARGS1*/
160468508Seric void
160568508Seric # ifdef __STDC__
160657642Seric setproctitle(char *fmt, ...)
160768508Seric # else
160857642Seric setproctitle(fmt, va_alist)
160924943Seric 	char *fmt;
161057642Seric 	va_dcl
161168508Seric # endif
161224943Seric {
161368508Seric # if SPT_TYPE != SPT_NONE
161424943Seric 	register char *p;
161525049Seric 	register int i;
161659732Seric 	SETPROC_STATIC char buf[MAXLINE];
161756852Seric 	VA_LOCAL_DECL
161868508Seric #  if SPT_TYPE == SPT_PSTAT
161958689Seric 	union pstun pst;
162058689Seric #  endif
162169748Seric #  if SPT_TYPE == SPT_REUSEARGV
162224943Seric 	extern char **Argv;
162324943Seric 	extern char *LastArgv;
162469748Seric #  endif
162524943Seric 
162658674Seric 	p = buf;
162724943Seric 
162858674Seric 	/* print sendmail: heading for grep */
162958674Seric 	(void) strcpy(p, "sendmail: ");
163058674Seric 	p += strlen(p);
163124943Seric 
163258674Seric 	/* print the argument string */
163358674Seric 	VA_START(fmt);
163458674Seric 	(void) vsprintf(p, fmt, ap);
163556852Seric 	VA_END;
163654996Seric 
163758674Seric 	i = strlen(buf);
163858689Seric 
163968508Seric #  if SPT_TYPE == SPT_PSTAT
164058689Seric 	pst.pst_command = buf;
164158689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
164269654Seric #  endif
164369654Seric #  if SPT_TYPE == SPT_PSSTRINGS
164459732Seric 	PS_STRINGS->ps_nargvstr = 1;
164559732Seric 	PS_STRINGS->ps_argvstr = buf;
164669654Seric #  endif
164769654Seric #  if SPT_TYPE == SPT_SYSMIPS
164869654Seric 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
164969654Seric #  endif
165069654Seric #  if SPT_TYPE == SPT_REUSEARGV
165154996Seric 	if (i > LastArgv - Argv[0] - 2)
165225049Seric 	{
165354996Seric 		i = LastArgv - Argv[0] - 2;
165458674Seric 		buf[i] = '\0';
165525049Seric 	}
165658674Seric 	(void) strcpy(Argv[0], buf);
165754997Seric 	p = &Argv[0][i];
165824943Seric 	while (p < LastArgv)
165968508Seric 		*p++ = SPT_PADCHAR;
166068508Seric 	Argv[1] = NULL;
166169654Seric #  endif
166268508Seric # endif /* SPT_TYPE != SPT_NONE */
166324943Seric }
166465982Seric 
166568508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
166625698Seric /*
166725698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
166825698Seric **
166925698Seric **	Parameters:
167068508Seric **		sig -- the signal that got us here (unused).
167125698Seric **
167225698Seric **	Returns:
167325698Seric **		none.
167425698Seric **
167525698Seric **	Side Effects:
167625698Seric **		Picks up extant zombies.
167725698Seric */
167825698Seric 
167946928Sbostic void
168068508Seric reapchild(sig)
168168508Seric 	int sig;
168225698Seric {
168364738Seric 	int olderrno = errno;
168463966Seric # ifdef HASWAITPID
168560219Seric 	auto int status;
168660560Seric 	int count;
168760560Seric 	int pid;
168860219Seric 
168960560Seric 	count = 0;
169060560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
169160560Seric 	{
169260560Seric 		if (count++ > 1000)
169360560Seric 		{
169466748Seric #ifdef LOG
169560560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
169660560Seric 				pid, status);
169766748Seric #endif
169860560Seric 			break;
169960560Seric 		}
170060560Seric 	}
170160219Seric # else
170225698Seric # ifdef WNOHANG
170325698Seric 	union wait status;
170425698Seric 
170563753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
170625698Seric 		continue;
170756795Seric # else /* WNOHANG */
170825698Seric 	auto int status;
170925698Seric 
171060219Seric 	while (wait(&status) > 0)
171125698Seric 		continue;
171256795Seric # endif /* WNOHANG */
171360219Seric # endif
171464035Seric # ifdef SYS5SIGNALS
171564035Seric 	(void) setsignal(SIGCHLD, reapchild);
171658061Seric # endif
171764738Seric 	errno = olderrno;
171825698Seric }
171955418Seric /*
172069654Seric **  PUTENV -- emulation of putenv() in terms of setenv()
172169654Seric **
172269654Seric **	Not needed on Posix-compliant systems.
172369654Seric **	This doesn't have full Posix semantics, but it's good enough
172469654Seric **		for sendmail.
172569654Seric **
172669654Seric **	Parameter:
172769654Seric **		env -- the environment to put.
172869654Seric **
172969654Seric **	Returns:
173069654Seric **		none.
173169654Seric */
173269654Seric 
173369654Seric #ifdef NEEDPUTENV
173469654Seric 
173569717Seric # if NEEDPUTENV == 2		/* no setenv(3) call available */
173669717Seric 
173769717Seric int
173869717Seric putenv(str)
173969717Seric 	char *str;
174069717Seric {
174169717Seric 	char **current;
174269717Seric 	int matchlen, envlen=0;
174369717Seric 	char *tmp;
174469717Seric 	char **newenv;
174569717Seric 	static int first=1;
174669717Seric 	extern char **environ;
174769717Seric 
174869717Seric 	/*
174969717Seric 	 * find out how much of str to match when searching
175069717Seric 	 * for a string to replace.
175169717Seric 	 */
175269717Seric 	if ((tmp = index(str, '=')) == NULL || tmp == str)
175369717Seric 		matchlen = strlen(str);
175469717Seric 	else
175569717Seric 		matchlen = (int) (tmp - str);
175669717Seric 	++matchlen;
175769717Seric 
175869717Seric 	/*
175969717Seric 	 * Search for an existing string in the environment and find the
176069717Seric 	 * length of environ.  If found, replace and exit.
176169717Seric 	 */
176269717Seric 	for (current=environ; *current; current++) {
176369717Seric 		++envlen;
176469717Seric 
176569717Seric 		if (strncmp(str, *current, matchlen) == 0) {
176669717Seric 			/* found it, now insert the new version */
176769717Seric 			*current = (char *)str;
176869717Seric 			return(0);
176969717Seric 		}
177069717Seric 	}
177169717Seric 
177269717Seric 	/*
177369717Seric 	 * There wasn't already a slot so add space for a new slot.
177469717Seric 	 * If this is our first time through, use malloc(), else realloc().
177569717Seric 	 */
177669717Seric 	if (first) {
177769717Seric 		newenv = (char **) malloc(sizeof(char *) * (envlen + 2));
177869717Seric 		if (newenv == NULL)
177969717Seric 			return(-1);
178069717Seric 
178169717Seric 		first=0;
178269717Seric 		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
178369717Seric 	} else {
178469717Seric 		newenv = (char **) realloc((char *)environ, sizeof(char *) * (envlen + 2));
178569717Seric 		if (newenv == NULL)
178669717Seric 			return(-1);
178769717Seric 	}
178869717Seric 
178969717Seric 	/* actually add in the new entry */
179069717Seric 	environ = newenv;
179169717Seric 	environ[envlen] = (char *)str;
179269717Seric 	environ[envlen+1] = NULL;
179369717Seric 
179469717Seric 	return(0);
179569717Seric }
179669717Seric 
179769717Seric #else			/* implement putenv() in terms of setenv() */
179869717Seric 
179969717Seric int
180069654Seric putenv(env)
180169654Seric 	char *env;
180269654Seric {
180369654Seric 	char *p;
180469654Seric 	int l;
180569654Seric 	char nbuf[100];
180669654Seric 
180769654Seric 	p = strchr(env, '=');
180869654Seric 	if (p == NULL)
180969717Seric 		return 0;
181069654Seric 	l = p - env;
181169654Seric 	if (l > sizeof nbuf - 1)
181269654Seric 		l = sizeof nbuf - 1;
181369654Seric 	bcopy(env, nbuf, l);
181469654Seric 	nbuf[l] = '\0';
181569717Seric 	return setenv(nbuf, ++p, 1);
181669654Seric }
181769654Seric 
181869717Seric # endif
181969654Seric #endif
182069654Seric /*
182155418Seric **  UNSETENV -- remove a variable from the environment
182255418Seric **
182355418Seric **	Not needed on newer systems.
182455418Seric **
182555418Seric **	Parameters:
182655418Seric **		name -- the string name of the environment variable to be
182755418Seric **			deleted from the current environment.
182855418Seric **
182955418Seric **	Returns:
183055418Seric **		none.
183155418Seric **
183255418Seric **	Globals:
183355418Seric **		environ -- a pointer to the current environment.
183455418Seric **
183555418Seric **	Side Effects:
183655418Seric **		Modifies environ.
183755418Seric */
183855418Seric 
183963962Seric #ifndef HASUNSETENV
184055418Seric 
184155418Seric void
184255418Seric unsetenv(name)
184355418Seric 	char *name;
184455418Seric {
184555418Seric 	extern char **environ;
184655418Seric 	register char **pp;
184755418Seric 	int len = strlen(name);
184855418Seric 
184955418Seric 	for (pp = environ; *pp != NULL; pp++)
185055418Seric 	{
185155418Seric 		if (strncmp(name, *pp, len) == 0 &&
185255418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
185355418Seric 			break;
185455418Seric 	}
185555418Seric 
185655418Seric 	for (; *pp != NULL; pp++)
185755418Seric 		*pp = pp[1];
185855418Seric }
185955418Seric 
186063962Seric #endif
186156215Seric /*
186256215Seric **  GETDTABLESIZE -- return number of file descriptors
186356215Seric **
186456215Seric **	Only on non-BSD systems
186556215Seric **
186656215Seric **	Parameters:
186756215Seric **		none
186856215Seric **
186956215Seric **	Returns:
187056215Seric **		size of file descriptor table
187156215Seric **
187256215Seric **	Side Effects:
187356215Seric **		none
187456215Seric */
187556215Seric 
187663787Seric #ifdef SOLARIS
187763787Seric # include <sys/resource.h>
187863787Seric #endif
187956215Seric 
188056215Seric int
188163787Seric getdtsize()
188256215Seric {
188363787Seric #ifdef RLIMIT_NOFILE
188463787Seric 	struct rlimit rl;
188563787Seric 
188663787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
188763787Seric 		return rl.rlim_cur;
188863787Seric #endif
188963787Seric 
189064031Seric # ifdef HASGETDTABLESIZE
189164031Seric 	return getdtablesize();
189264031Seric # else
189364031Seric #  ifdef _SC_OPEN_MAX
189458689Seric 	return sysconf(_SC_OPEN_MAX);
189563787Seric #  else
189656215Seric 	return NOFILE;
189763787Seric #  endif
189858689Seric # endif
189956215Seric }
190057631Seric /*
190157631Seric **  UNAME -- get the UUCP name of this system.
190257631Seric */
190357631Seric 
190457943Seric #ifndef HASUNAME
190557631Seric 
190657631Seric int
190757631Seric uname(name)
190857631Seric 	struct utsname *name;
190957631Seric {
191057631Seric 	FILE *file;
191157631Seric 	char *n;
191257631Seric 
191357631Seric 	name->nodename[0] = '\0';
191457631Seric 
191557661Seric 	/* try /etc/whoami -- one line with the node name */
191657631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
191757631Seric 	{
191857661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
191957631Seric 		(void) fclose(file);
192057661Seric 		n = strchr(name->nodename, '\n');
192157631Seric 		if (n != NULL)
192257631Seric 			*n = '\0';
192357631Seric 		if (name->nodename[0] != '\0')
192457631Seric 			return (0);
192557631Seric 	}
192657631Seric 
192757661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
192857631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
192957631Seric 	{
193057631Seric 		char buf[MAXLINE];
193157631Seric 
193257631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
193357631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
193457631Seric 					NODE_LENGTH, name->nodename) > 0)
193557631Seric 				break;
193657631Seric 		(void) fclose(file);
193757631Seric 		if (name->nodename[0] != '\0')
193857631Seric 			return (0);
193957631Seric 	}
194057631Seric 
194157631Seric #ifdef TRUST_POPEN
194257631Seric 	/*
194357631Seric 	**  Popen is known to have security holes.
194457631Seric 	*/
194557631Seric 
194657661Seric 	/* try uuname -l to return local name */
194757631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
194857631Seric 	{
194957661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
195057631Seric 		(void) pclose(file);
195157661Seric 		n = strchr(name, '\n');
195257631Seric 		if (n != NULL)
195357631Seric 			*n = '\0';
195457661Seric 		if (name->nodename[0] != '\0')
195557631Seric 			return (0);
195657631Seric 	}
195757631Seric #endif
195869472Seric 
195957631Seric 	return (-1);
196057631Seric }
196157943Seric #endif /* HASUNAME */
196258068Seric /*
196358068Seric **  INITGROUPS -- initialize groups
196458068Seric **
196558068Seric **	Stub implementation for System V style systems
196658068Seric */
196758068Seric 
196858068Seric #ifndef HASINITGROUPS
196958068Seric 
197058068Seric initgroups(name, basegid)
197158068Seric 	char *name;
197258068Seric 	int basegid;
197358068Seric {
197458068Seric 	return 0;
197558068Seric }
197658068Seric 
197758068Seric #endif
197858082Seric /*
197959289Seric **  SETSID -- set session id (for non-POSIX systems)
198059289Seric */
198159289Seric 
198259289Seric #ifndef HASSETSID
198359289Seric 
198463753Seric pid_t
198563753Seric setsid __P ((void))
198659289Seric {
198764155Seric #ifdef TIOCNOTTY
198864155Seric 	int fd;
198964155Seric 
199066743Seric 	fd = open("/dev/tty", O_RDWR, 0);
199164155Seric 	if (fd >= 0)
199264155Seric 	{
199364155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
199464155Seric 		(void) close(fd);
199564155Seric 	}
199664155Seric #endif /* TIOCNOTTY */
199764705Seric # ifdef SYS5SETPGRP
199863753Seric 	return setpgrp();
199963753Seric # else
200064155Seric 	return setpgid(0, getpid());
200159289Seric # endif
200259289Seric }
200359289Seric 
200459289Seric #endif
200559289Seric /*
200666748Seric **  FSYNC -- dummy fsync
200766748Seric */
200866748Seric 
200966748Seric #ifdef NEEDFSYNC
201066748Seric 
201166748Seric fsync(fd)
201266748Seric 	int fd;
201366748Seric {
201466748Seric # ifdef O_SYNC
201566748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
201666748Seric # else
201766748Seric 	/* nothing we can do */
201866748Seric 	return 0;
201966748Seric # endif
202066748Seric }
202166748Seric 
202266748Seric #endif
202366748Seric /*
202464718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
202564718Seric **
202664718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
202768508Seric **	instead of a long.  This patches things.  Only needed on versions
202868508Seric **	prior to 5.4.3.
202964718Seric */
203064718Seric 
203168508Seric #ifdef DGUX_5_4_2
203264718Seric 
203364718Seric #undef inet_addr
203464718Seric 
203564718Seric long
203664718Seric dgux_inet_addr(host)
203764718Seric 	char *host;
203864718Seric {
203964718Seric 	struct in_addr haddr;
204064718Seric 
204164718Seric 	haddr = inet_addr(host);
204264718Seric 	return haddr.s_addr;
204364718Seric }
204464718Seric 
204564718Seric #endif
204664718Seric /*
204763969Seric **  GETOPT -- for old systems or systems with bogus implementations
204863969Seric */
204963969Seric 
205063969Seric #ifdef NEEDGETOPT
205163969Seric 
205263969Seric /*
205363969Seric  * Copyright (c) 1985 Regents of the University of California.
205463969Seric  * All rights reserved.  The Berkeley software License Agreement
205563969Seric  * specifies the terms and conditions for redistribution.
205663969Seric  */
205763969Seric 
205863969Seric 
205963969Seric /*
206063969Seric ** this version hacked to add `atend' flag to allow state machine
206163969Seric ** to reset if invoked by the program to scan args for a 2nd time
206263969Seric */
206363969Seric 
206463969Seric #if defined(LIBC_SCCS) && !defined(lint)
206563969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
206664072Seric #endif /* LIBC_SCCS and not lint */
206763969Seric 
206863969Seric #include <stdio.h>
206963969Seric 
207063969Seric /*
207163969Seric  * get option letter from argument vector
207263969Seric  */
207365352Seric #ifdef _CONVEX_SOURCE
207468508Seric extern int	optind, opterr, optopt;
207568508Seric extern char	*optarg;
207665352Seric #else
207765352Seric int	opterr = 1;		/* if error message should be printed */
207865352Seric int	optind = 1;		/* index into parent argv vector */
207968470Seric int	optopt = 0;		/* character checked for validity */
208068470Seric char	*optarg = NULL;		/* argument associated with option */
208168508Seric #endif
208263969Seric 
208363969Seric #define BADCH	(int)'?'
208463969Seric #define EMSG	""
208563969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
208663969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
208763969Seric 
208863969Seric getopt(nargc,nargv,ostr)
208965160Seric 	int		nargc;
209065160Seric 	char *const	*nargv;
209165160Seric 	const char	*ostr;
209263969Seric {
209363969Seric 	static char	*place = EMSG;	/* option letter processing */
209463969Seric 	static char	atend = 0;
209563969Seric 	register char	*oli;		/* option letter list index */
209663969Seric 
209763969Seric 	if (atend) {
209863969Seric 		atend = 0;
209963969Seric 		place = EMSG;
210063969Seric 	}
210163969Seric 	if(!*place) {			/* update scanning pointer */
210264136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
210363969Seric 			atend++;
210463969Seric 			return(EOF);
210563969Seric 		}
210663969Seric 		if (*place == '-') {	/* found "--" */
210763969Seric 			++optind;
210863969Seric 			atend++;
210963969Seric 			return(EOF);
211063969Seric 		}
211163969Seric 	}				/* option letter okay? */
211264125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
211364136Seric 		if (!*place) ++optind;
211463969Seric 		tell(": illegal option -- ");
211563969Seric 	}
211663969Seric 	if (*++oli != ':') {		/* don't need argument */
211763969Seric 		optarg = NULL;
211863969Seric 		if (!*place) ++optind;
211963969Seric 	}
212063969Seric 	else {				/* need an argument */
212163969Seric 		if (*place) optarg = place;	/* no white space */
212263969Seric 		else if (nargc <= ++optind) {	/* no arg */
212363969Seric 			place = EMSG;
212463969Seric 			tell(": option requires an argument -- ");
212563969Seric 		}
212663969Seric 	 	else optarg = nargv[optind];	/* white space */
212763969Seric 		place = EMSG;
212863969Seric 		++optind;
212963969Seric 	}
213063969Seric 	return(optopt);			/* dump back option letter */
213163969Seric }
213263969Seric 
213363969Seric #endif
213463969Seric /*
213563969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
213663969Seric */
213763969Seric 
213863969Seric #ifdef NEEDVPRINTF
213963969Seric 
214063969Seric #define MAXARG	16
214163969Seric 
214263969Seric vfprintf(fp, fmt, ap)
214363969Seric 	FILE *	fp;
214463969Seric 	char *	fmt;
214563969Seric 	char **	ap;
214663969Seric {
214763969Seric 	char *	bp[MAXARG];
214863969Seric 	int	i = 0;
214963969Seric 
215063969Seric 	while (*ap && i < MAXARG)
215163969Seric 		bp[i++] = *ap++;
215263969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
215363969Seric 			 bp[4], bp[5], bp[6], bp[7],
215463969Seric 			 bp[8], bp[9], bp[10], bp[11],
215563969Seric 			 bp[12], bp[13], bp[14], bp[15]);
215663969Seric }
215763969Seric 
215863969Seric vsprintf(s, fmt, ap)
215963969Seric 	char *	s;
216063969Seric 	char *	fmt;
216163969Seric 	char **	ap;
216263969Seric {
216363969Seric 	char *	bp[MAXARG];
216463969Seric 	int	i = 0;
216563969Seric 
216663969Seric 	while (*ap && i < MAXARG)
216763969Seric 		bp[i++] = *ap++;
216863969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
216963969Seric 			bp[4], bp[5], bp[6], bp[7],
217063969Seric 			bp[8], bp[9], bp[10], bp[11],
217163969Seric 			bp[12], bp[13], bp[14], bp[15]);
217263969Seric }
217363969Seric 
217463969Seric #endif
217563969Seric /*
217665220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
217765208Seric **
217865211Seric **	Parameters:
217965211Seric **		shell -- the user's shell from /etc/passwd
218065211Seric **
218165211Seric **	Returns:
218265211Seric **		TRUE -- if it is ok to use this for unrestricted access.
218365211Seric **		FALSE -- if the shell is restricted.
218465208Seric */
218565208Seric 
218665587Seric #if !HASGETUSERSHELL
218765587Seric 
218865587Seric # ifndef _PATH_SHELLS
218965587Seric #  define _PATH_SHELLS	"/etc/shells"
219065587Seric # endif
219165587Seric 
219269762Seric # ifdef _AIX3
219369762Seric #  include <userconf.h>
219469762Seric #  include <usersec.h>
219569762Seric # endif
219669762Seric 
219765587Seric char	*DefaultUserShells[] =
219865587Seric {
219968508Seric 	"/bin/sh",		/* standard shell */
220065587Seric 	"/usr/bin/sh",
220168508Seric 	"/bin/csh",		/* C shell */
220265587Seric 	"/usr/bin/csh",
220365587Seric #ifdef __hpux
220468889Seric # ifdef V4FS
220568889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
220668889Seric 	"/usr/bin/ksh",		/* Korn shell */
220768889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
220868889Seric 	"/usr/bin/pam",
220968889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
221068889Seric 	"/usr/bin/posix/sh",
221168889Seric # else
221268508Seric 	"/bin/rsh",		/* restricted Bourne shell */
221368508Seric 	"/bin/ksh",		/* Korn shell */
221468508Seric 	"/bin/rksh",		/* restricted Korn shell */
221565587Seric 	"/bin/pam",
221668508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
221765587Seric 	"/bin/posix/sh",
221868889Seric # endif
221965211Seric #endif
222068508Seric #ifdef _AIX3
222168508Seric 	"/bin/ksh",		/* Korn shell */
222268508Seric 	"/usr/bin/ksh",
222368508Seric 	"/bin/tsh",		/* trusted shell */
222468508Seric 	"/usr/bin/tsh",
222568508Seric 	"/bin/bsh",		/* Bourne shell */
222668508Seric 	"/usr/bin/bsh",
222768508Seric #endif
222865587Seric 	NULL
222965587Seric };
223065587Seric 
223165208Seric #endif
223265208Seric 
223365747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
223465747Seric 
223565211Seric bool
223665211Seric usershellok(shell)
223765211Seric 	char *shell;
223865208Seric {
223965211Seric #if HASGETUSERSHELL
224065211Seric 	register char *p;
224165211Seric 	extern char *getusershell();
224265208Seric 
224369505Seric 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
224468457Seric 		return TRUE;
224568457Seric 
224665211Seric 	setusershell();
224765211Seric 	while ((p = getusershell()) != NULL)
224865747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
224965211Seric 			break;
225065211Seric 	endusershell();
225165211Seric 	return p != NULL;
225265211Seric #else
2253*69769Seric # if USEGETCONFATTR
225469762Seric 	auto char *v;
225569762Seric # endif
225665211Seric 	register FILE *shellf;
225765211Seric 	char buf[MAXLINE];
225865208Seric 
225968457Seric 	if (shell == NULL || shell[0] == '\0')
226068457Seric 		return TRUE;
226168457Seric 
2262*69769Seric # if USEGETCONFATTR
2263*69769Seric 	/*
2264*69769Seric 	**  Naturally IBM has a "better" idea.....
2265*69769Seric 	**
2266*69769Seric 	**	What a crock.  This interface isn't documented, it is
2267*69769Seric 	**	considered part of the security library (-ls), and it
2268*69769Seric 	**	only works if you are running as root (since the list
2269*69769Seric 	**	of valid shells is obviously a source of great concern).
2270*69769Seric 	**	I recommend that you do NOT define USEGETCONFATTR,
2271*69769Seric 	**	especially since you are going to have to set up an
2272*69769Seric 	**	/etc/shells anyhow to handle the cases where getconfattr
2273*69769Seric 	**	fails.
2274*69769Seric 	*/
2275*69769Seric 
2276*69769Seric 	if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
227769762Seric 	{
2278*69769Seric 		while (*v != '\0')
227969762Seric 		{
228069762Seric 			if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
228169762Seric 				return TRUE;
228269762Seric 			v += strlen(v) + 1;
228369762Seric 		}
2284*69769Seric 		return FALSE;
228569762Seric 	}
228669762Seric # endif
228769762Seric 
228865211Seric 	shellf = fopen(_PATH_SHELLS, "r");
228965211Seric 	if (shellf == NULL)
229065211Seric 	{
229165211Seric 		/* no /etc/shells; see if it is one of the std shells */
229265587Seric 		char **d;
229365587Seric 
229465587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
229565587Seric 		{
229665587Seric 			if (strcmp(shell, *d) == 0)
229765587Seric 				return TRUE;
229865587Seric 		}
229965587Seric 		return FALSE;
230065211Seric 	}
230165208Seric 
230265211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
230365211Seric 	{
230465211Seric 		register char *p, *q;
230565208Seric 
230665211Seric 		p = buf;
230765211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
230865211Seric 			p++;
230965211Seric 		if (*p == '#' || *p == '\0')
231065208Seric 			continue;
231165211Seric 		q = p;
231265211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
231365211Seric 			p++;
231465211Seric 		*p = '\0';
231565747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
231665211Seric 		{
231765211Seric 			fclose(shellf);
231865211Seric 			return TRUE;
231965211Seric 		}
232065208Seric 	}
232165211Seric 	fclose(shellf);
232265211Seric 	return FALSE;
232365211Seric #endif
232465208Seric }
232565208Seric /*
232664311Seric **  FREESPACE -- see how much free space is on the queue filesystem
232758082Seric **
232858082Seric **	Only implemented if you have statfs.
232958082Seric **
233058082Seric **	Parameters:
233164311Seric **		dir -- the directory in question.
233264311Seric **		bsize -- a variable into which the filesystem
233364311Seric **			block size is stored.
233458082Seric **
233558082Seric **	Returns:
233664311Seric **		The number of bytes free on the queue filesystem.
233764311Seric **		-1 if the statfs call fails.
233864311Seric **
233964311Seric **	Side effects:
234064311Seric **		Puts the filesystem block size into bsize.
234158082Seric */
234258082Seric 
234365749Seric /* statfs types */
234465749Seric #define SFS_NONE	0	/* no statfs implementation */
234565749Seric #define SFS_USTAT	1	/* use ustat */
234665749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
234765749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
234865749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
234965749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
235066752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
235165749Seric 
235265749Seric #ifndef SFS_TYPE
235365749Seric # define SFS_TYPE	SFS_NONE
235458157Seric #endif
235558157Seric 
235665749Seric #if SFS_TYPE == SFS_USTAT
235758157Seric # include <ustat.h>
235858157Seric #endif
235965749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
236065749Seric # include <sys/statfs.h>
236158082Seric #endif
236265749Seric #if SFS_TYPE == SFS_VFS
236365749Seric # include <sys/vfs.h>
236465749Seric #endif
236565749Seric #if SFS_TYPE == SFS_MOUNT
236665749Seric # include <sys/mount.h>
236765749Seric #endif
236866752Seric #if SFS_TYPE == SFS_STATVFS
236966752Seric # include <sys/statvfs.h>
237066752Seric #endif
237158082Seric 
237264311Seric long
237364311Seric freespace(dir, bsize)
237464311Seric 	char *dir;
237564311Seric 	long *bsize;
237658082Seric {
237765749Seric #if SFS_TYPE != SFS_NONE
237865749Seric # if SFS_TYPE == SFS_USTAT
237958153Seric 	struct ustat fs;
238058153Seric 	struct stat statbuf;
238158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
238268695Seric #  define SFS_BAVAIL	f_tfree
238358157Seric # else
238458157Seric #  if defined(ultrix)
238558157Seric 	struct fs_data fs;
238668695Seric #   define SFS_BAVAIL	fd_bfreen
238768508Seric #   define FSBLOCKSIZE	1024L
238858153Seric #  else
238966752Seric #   if SFS_TYPE == SFS_STATVFS
239066752Seric 	struct statvfs fs;
239168508Seric #    define FSBLOCKSIZE	fs.f_frsize
239266752Seric #   else
239358082Seric 	struct statfs fs;
239466752Seric #    define FSBLOCKSIZE	fs.f_bsize
239563987Seric #   endif
239658153Seric #  endif
239758133Seric # endif
239868695Seric # ifndef SFS_BAVAIL
239968695Seric #  define SFS_BAVAIL f_bavail
240068508Seric # endif
240158082Seric 
240265749Seric # if SFS_TYPE == SFS_USTAT
240364311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
240458157Seric # else
240565749Seric #  if SFS_TYPE == SFS_4ARGS
240664311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
240758157Seric #  else
240868508Seric #   if SFS_TYPE == SFS_STATVFS
240968508Seric 	if (statvfs(dir, &fs) == 0)
241068508Seric #   else
241168508Seric #    if defined(ultrix)
241268499Seric 	if (statfs(dir, &fs) > 0)
241368508Seric #    else
241464311Seric 	if (statfs(dir, &fs) == 0)
241568508Seric #    endif
241658153Seric #   endif
241758133Seric #  endif
241858133Seric # endif
241958133Seric 	{
242064311Seric 		if (bsize != NULL)
242164311Seric 			*bsize = FSBLOCKSIZE;
242268695Seric 		return (fs.SFS_BAVAIL);
242364311Seric 	}
242464337Seric #endif
242564311Seric 	return (-1);
242664311Seric }
242764311Seric /*
242864311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
242964311Seric **
243064311Seric **	Only implemented if you have statfs.
243164311Seric **
243264311Seric **	Parameters:
243364311Seric **		msize -- the size to check against.  If zero, we don't yet
243464311Seric **		know how big the message will be, so just check for
243564311Seric **		a "reasonable" amount.
243664311Seric **
243764311Seric **	Returns:
243864311Seric **		TRUE if there is enough space.
243964311Seric **		FALSE otherwise.
244064311Seric */
244164311Seric 
244264311Seric bool
244364311Seric enoughspace(msize)
244464311Seric 	long msize;
244564311Seric {
244664311Seric 	long bfree, bsize;
244764311Seric 
244864311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
244964311Seric 	{
245058133Seric 		if (tTd(4, 80))
245164311Seric 			printf("enoughspace: no threshold\n");
245264311Seric 		return TRUE;
245364311Seric 	}
245464311Seric 
245564311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
245664311Seric 	{
245764311Seric 		if (tTd(4, 80))
245858333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
245964311Seric 				bfree, msize);
246058333Seric 
246158333Seric 		/* convert msize to block count */
246264311Seric 		msize = msize / bsize + 1;
246358333Seric 		if (MinBlocksFree >= 0)
246458333Seric 			msize += MinBlocksFree;
246558333Seric 
246664311Seric 		if (bfree < msize)
246758090Seric 		{
246858090Seric #ifdef LOG
246958090Seric 			if (LogLevel > 0)
247064254Seric 				syslog(LOG_ALERT,
247164254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
247268508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
247368508Seric 					bfree,
247468508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
247568508Seric 					msize, QueueDir);
247658090Seric #endif
247758082Seric 			return FALSE;
247858090Seric 		}
247958082Seric 	}
248058082Seric 	else if (tTd(4, 80))
248158333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
248258333Seric 			MinBlocksFree, msize, errstring(errno));
248358082Seric 	return TRUE;
248458082Seric }
248558542Seric /*
248658542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
248758542Seric **
248858542Seric **	This looks at an errno value and tells if this is likely to
248958542Seric **	go away if retried later.
249058542Seric **
249158542Seric **	Parameters:
249258542Seric **		err -- the errno code to classify.
249358542Seric **
249458542Seric **	Returns:
249558542Seric **		TRUE if this is probably transient.
249658542Seric **		FALSE otherwise.
249758542Seric */
249858542Seric 
249958542Seric bool
250058542Seric transienterror(err)
250158542Seric 	int err;
250258542Seric {
250358542Seric 	switch (err)
250458542Seric 	{
250558542Seric 	  case EIO:			/* I/O error */
250658542Seric 	  case ENXIO:			/* Device not configured */
250758542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
250858542Seric 	  case ENOMEM:			/* Cannot allocate memory */
250958542Seric 	  case ENODEV:			/* Operation not supported by device */
251058542Seric 	  case ENFILE:			/* Too many open files in system */
251158542Seric 	  case EMFILE:			/* Too many open files */
251258542Seric 	  case ENOSPC:			/* No space left on device */
251358542Seric #ifdef ETIMEDOUT
251458542Seric 	  case ETIMEDOUT:		/* Connection timed out */
251558542Seric #endif
251658542Seric #ifdef ESTALE
251758542Seric 	  case ESTALE:			/* Stale NFS file handle */
251858542Seric #endif
251958542Seric #ifdef ENETDOWN
252058542Seric 	  case ENETDOWN:		/* Network is down */
252158542Seric #endif
252258542Seric #ifdef ENETUNREACH
252358542Seric 	  case ENETUNREACH:		/* Network is unreachable */
252458542Seric #endif
252558542Seric #ifdef ENETRESET
252658542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
252758542Seric #endif
252858542Seric #ifdef ECONNABORTED
252958542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
253058542Seric #endif
253158542Seric #ifdef ECONNRESET
253258542Seric 	  case ECONNRESET:		/* Connection reset by peer */
253358542Seric #endif
253458542Seric #ifdef ENOBUFS
253558542Seric 	  case ENOBUFS:			/* No buffer space available */
253658542Seric #endif
253758542Seric #ifdef ESHUTDOWN
253858542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
253958542Seric #endif
254058542Seric #ifdef ECONNREFUSED
254158542Seric 	  case ECONNREFUSED:		/* Connection refused */
254258542Seric #endif
254358542Seric #ifdef EHOSTDOWN
254458542Seric 	  case EHOSTDOWN:		/* Host is down */
254558542Seric #endif
254658542Seric #ifdef EHOSTUNREACH
254758542Seric 	  case EHOSTUNREACH:		/* No route to host */
254858542Seric #endif
254958542Seric #ifdef EDQUOT
255058542Seric 	  case EDQUOT:			/* Disc quota exceeded */
255158542Seric #endif
255258542Seric #ifdef EPROCLIM
255358542Seric 	  case EPROCLIM:		/* Too many processes */
255458542Seric #endif
255558542Seric #ifdef EUSERS
255658542Seric 	  case EUSERS:			/* Too many users */
255758542Seric #endif
255858542Seric #ifdef EDEADLK
255958542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
256058542Seric #endif
256158542Seric #ifdef EISCONN
256258542Seric 	  case EISCONN:			/* Socket already connected */
256358542Seric #endif
256458542Seric #ifdef EINPROGRESS
256558542Seric 	  case EINPROGRESS:		/* Operation now in progress */
256658542Seric #endif
256758542Seric #ifdef EALREADY
256858542Seric 	  case EALREADY:		/* Operation already in progress */
256958542Seric #endif
257058542Seric #ifdef EADDRINUSE
257158542Seric 	  case EADDRINUSE:		/* Address already in use */
257258542Seric #endif
257358542Seric #ifdef EADDRNOTAVAIL
257458542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
257558542Seric #endif
257666018Seric #ifdef ETXTBSY
257766017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
257866017Seric #endif
257963834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
258058542Seric 	  case ENOSR:			/* Out of streams resources */
258158542Seric #endif
258258542Seric 		return TRUE;
258358542Seric 	}
258458542Seric 
258558542Seric 	/* nope, must be permanent */
258658542Seric 	return FALSE;
258758542Seric }
258858689Seric /*
258964035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
259058689Seric **
259158689Seric **	Parameters:
259258689Seric **		fd -- the file descriptor of the file.
259358689Seric **		filename -- the file name (for error messages).
259464335Seric **		ext -- the filename extension.
259558689Seric **		type -- type of the lock.  Bits can be:
259658689Seric **			LOCK_EX -- exclusive lock.
259758689Seric **			LOCK_NB -- non-blocking.
259858689Seric **
259958689Seric **	Returns:
260058689Seric **		TRUE if the lock was acquired.
260158689Seric **		FALSE otherwise.
260258689Seric */
260358689Seric 
260458689Seric bool
260564335Seric lockfile(fd, filename, ext, type)
260658689Seric 	int fd;
260758689Seric 	char *filename;
260864335Seric 	char *ext;
260958689Seric 	int type;
261058689Seric {
261165830Seric # if !HASFLOCK
261258689Seric 	int action;
261358689Seric 	struct flock lfd;
261464335Seric 
261564335Seric 	if (ext == NULL)
261664335Seric 		ext = "";
261769472Seric 
261863983Seric 	bzero(&lfd, sizeof lfd);
261959447Seric 	if (bitset(LOCK_UN, type))
262059447Seric 		lfd.l_type = F_UNLCK;
262159447Seric 	else if (bitset(LOCK_EX, type))
262258689Seric 		lfd.l_type = F_WRLCK;
262358689Seric 	else
262458689Seric 		lfd.l_type = F_RDLCK;
262558689Seric 
262658689Seric 	if (bitset(LOCK_NB, type))
262758689Seric 		action = F_SETLK;
262858689Seric 	else
262958689Seric 		action = F_SETLKW;
263058689Seric 
263164264Seric 	if (tTd(55, 60))
263264335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
263364335Seric 			filename, ext, action, lfd.l_type);
263464264Seric 
263558689Seric 	if (fcntl(fd, action, &lfd) >= 0)
263664264Seric 	{
263764264Seric 		if (tTd(55, 60))
263864264Seric 			printf("SUCCESS\n");
263958689Seric 		return TRUE;
264064264Seric 	}
264158689Seric 
264264264Seric 	if (tTd(55, 60))
264364264Seric 		printf("(%s) ", errstring(errno));
264464264Seric 
264564136Seric 	/*
264664136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
264764136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
264864136Seric 	**  as type "tmp" (that is, served from swap space), the
264964136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
265064136Seric 	**  Since this is fairly common during testing, we will assume
265164136Seric 	**  that this indicates that the lock is successfully grabbed.
265264136Seric 	*/
265364136Seric 
265464136Seric 	if (errno == EINVAL)
265564264Seric 	{
265664264Seric 		if (tTd(55, 60))
265764264Seric 			printf("SUCCESS\n");
265864136Seric 		return TRUE;
265964264Seric 	}
266064136Seric 
266158689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
266264378Seric 	{
266364378Seric 		int omode = -1;
266464378Seric #  ifdef F_GETFL
266564378Seric 		int oerrno = errno;
266664378Seric 
266764378Seric 		(void) fcntl(fd, F_GETFL, &omode);
266864378Seric 		errno = oerrno;
266964378Seric #  endif
267064378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
267164378Seric 			filename, ext, fd, type, omode, geteuid());
267264378Seric 	}
267358689Seric # else
267464335Seric 	if (ext == NULL)
267564335Seric 		ext = "";
267664335Seric 
267764264Seric 	if (tTd(55, 60))
267864335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
267964264Seric 
268058689Seric 	if (flock(fd, type) >= 0)
268164264Seric 	{
268264264Seric 		if (tTd(55, 60))
268364264Seric 			printf("SUCCESS\n");
268458689Seric 		return TRUE;
268564264Seric 	}
268658689Seric 
268764264Seric 	if (tTd(55, 60))
268864264Seric 		printf("(%s) ", errstring(errno));
268964264Seric 
269058689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
269164378Seric 	{
269264378Seric 		int omode = -1;
269364378Seric #  ifdef F_GETFL
269464378Seric 		int oerrno = errno;
269564378Seric 
269664378Seric 		(void) fcntl(fd, F_GETFL, &omode);
269764378Seric 		errno = oerrno;
269864378Seric #  endif
269964378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
270064378Seric 			filename, ext, fd, type, omode, geteuid());
270164386Seric 	}
270258689Seric # endif
270364264Seric 	if (tTd(55, 60))
270464264Seric 		printf("FAIL\n");
270558689Seric 	return FALSE;
270658689Seric }
270764556Seric /*
270865948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
270965948Seric **
271065948Seric **	Parameters:
271165948Seric **		fd -- the file descriptor to check.
271265948Seric **
271365948Seric **	Returns:
271465948Seric **		TRUE -- if only root can chown the file to an arbitrary
271565948Seric **			user.
271665948Seric **		FALSE -- if an arbitrary user can give away a file.
271765948Seric */
271865948Seric 
271965948Seric bool
272065948Seric chownsafe(fd)
272165948Seric 	int fd;
272265948Seric {
272365950Seric #ifdef __hpux
272465948Seric 	char *s;
272565948Seric 	int tfd;
272665948Seric 	uid_t o_uid, o_euid;
272765948Seric 	gid_t o_gid, o_egid;
272865948Seric 	bool rval;
272965948Seric 	struct stat stbuf;
273065948Seric 
273165948Seric 	o_uid = getuid();
273265948Seric 	o_euid = geteuid();
273365948Seric 	o_gid = getgid();
273465948Seric 	o_egid = getegid();
273565959Seric 	fstat(fd, &stbuf);
273665948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
273765948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
273865948Seric 	s = tmpnam(NULL);
273965948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
274065974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
274165948Seric 	close(tfd);
274265948Seric 	unlink(s);
274369638Seric 	setresuid(o_uid, o_euid, -1);
274465948Seric 	setresgid(o_gid, o_egid, -1);
274565948Seric 	return rval;
274665948Seric #else
274766088Seric # ifdef _POSIX_CHOWN_RESTRICTED
274866088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
274966088Seric 	return FALSE;
275066088Seric #  else
275166088Seric 	return TRUE;
275266088Seric #  endif
275366088Seric # else
275466088Seric #  ifdef _PC_CHOWN_RESTRICTED
275568508Seric 	int rval;
275668508Seric 
275768508Seric 	/*
275868508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
275968508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
276068508Seric 	**  the call.  This heuristic checks for that.
276168508Seric 	*/
276268508Seric 
276368508Seric 	errno = 0;
276468508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
276568508Seric 	if (errno == 0)
276668508Seric 		return rval > 0;
276768508Seric #  endif
276868508Seric #  ifdef BSD
276968508Seric 	return TRUE;
277068499Seric #  else
277165948Seric 	return FALSE;
277265948Seric #  endif
277365948Seric # endif
277465948Seric #endif
277565948Seric }
277665948Seric /*
277768508Seric **  RESETLIMITS -- reset system controlled resource limits
277868508Seric **
277968508Seric **	This is to avoid denial-of-service attacks
278068508Seric **
278168508Seric **	Parameters:
278268508Seric **		none
278368508Seric **
278468508Seric **	Returns:
278568508Seric **		none
278668508Seric */
278768508Seric 
278868508Seric #if HASSETRLIMIT
278968508Seric # include <sys/resource.h>
279068508Seric #endif
279168508Seric 
279268508Seric void
279368508Seric resetlimits()
279468508Seric {
279568508Seric #if HASSETRLIMIT
279668508Seric 	struct rlimit lim;
279768508Seric 
279868508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
279968508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
280068508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
280168508Seric #else
280268508Seric # if HASULIMIT
280368508Seric 	(void) ulimit(2, 0x3fffff);
280468508Seric # endif
280568508Seric #endif
280668508Seric }
280768508Seric /*
280864556Seric **  GETCFNAME -- return the name of the .cf file.
280964556Seric **
281064556Seric **	Some systems (e.g., NeXT) determine this dynamically.
281164556Seric */
281264556Seric 
281364556Seric char *
281464556Seric getcfname()
281564556Seric {
281669748Seric #ifdef TRY_VERSIONED_CF_NAME
281768842Seric 	int i;
281868842Seric 	static char cbuf[200];
281969748Seric #endif
282068842Seric 
282164556Seric 	if (ConfFile != NULL)
282264556Seric 		return ConfFile;
282365214Seric #ifdef NETINFO
282465214Seric 	{
282565214Seric 		extern char *ni_propval();
282665214Seric 		char *cflocation;
282765214Seric 
282868508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
282968508Seric 					"sendmail.cf", '\0');
283065214Seric 		if (cflocation != NULL)
283165214Seric 			return cflocation;
283265214Seric 	}
283365214Seric #endif
283468842Seric 
283569396Seric #ifdef TRY_VERSIONED_CF_NAME
283668842Seric 	/*
283768842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
283868842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
283968842Seric 	**
284068842Seric 	**	I suppose it should really try a search path here --
284168842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
284268842Seric 	**	/usr/lib/sendmail.cf, and so forth.
284368842Seric 	*/
284468842Seric 
284568842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
284668842Seric 	i = strlen(cbuf);
284768842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
284868842Seric 	{
284968842Seric 		char *p;
285068842Seric 		extern char Version[];
285168842Seric 
285268842Seric 		strcpy(&cbuf[i - 2], Version);
285368842Seric 		p = strchr(&cbuf[i - 2], '/');
285468842Seric 		if (p != NULL)
285568842Seric 			*p = '\0';
285668842Seric 		p = &cbuf[strlen(cbuf)];
285768842Seric 		do
285868842Seric 		{
285968842Seric 			int fd;
286068842Seric 
286168842Seric 			strcpy(p, ".cf");
286268842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
286368842Seric 			{
286468842Seric 				close(fd);
286568842Seric 				return cbuf;
286668842Seric 			}
286768842Seric 			*p = '\0';
286868842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
286968842Seric 	}
287069396Seric #endif
287164556Seric 	return _PATH_SENDMAILCF;
287264556Seric }
287364718Seric /*
287464718Seric **  SETVENDOR -- process vendor code from V configuration line
287564718Seric **
287664718Seric **	Parameters:
287764718Seric **		vendor -- string representation of vendor.
287864718Seric **
287964718Seric **	Returns:
288064718Seric **		TRUE -- if ok.
288164718Seric **		FALSE -- if vendor code could not be processed.
288264926Seric **
288364926Seric **	Side Effects:
288464926Seric **		It is reasonable to set mode flags here to tweak
288564926Seric **		processing in other parts of the code if necessary.
288664926Seric **		For example, if you are a vendor that uses $%y to
288764926Seric **		indicate YP lookups, you could enable that here.
288864718Seric */
288964718Seric 
289064718Seric bool
289164718Seric setvendor(vendor)
289264718Seric 	char *vendor;
289364718Seric {
289464926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
289568508Seric 	{
289668508Seric 		VendorCode = VENDOR_BERKELEY;
289764926Seric 		return TRUE;
289868508Seric 	}
289964926Seric 
290064926Seric 	/* add vendor extensions here */
290164926Seric 
290268508Seric #ifdef SUN_EXTENSIONS
290368508Seric 	if (strcasecmp(vendor, "Sun") == 0)
290468508Seric 	{
290568508Seric 		VendorCode = VENDOR_SUN;
290668508Seric 		return TRUE;
290768508Seric 	}
290868508Seric #endif
290968508Seric 
291064926Seric 	return FALSE;
291164718Seric }
291264816Seric /*
291369510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
291469510Seric **
291569510Seric **	Vendor_pre_defaults is called before reading the configuration
291669510Seric **	file; vendor_post_defaults is called immediately after.
291769510Seric **
291869510Seric **	Parameters:
291969510Seric **		e -- the global environment to initialize.
292069510Seric **
292169510Seric **	Returns:
292269510Seric **		none.
292369510Seric */
292469510Seric 
292569510Seric void
292669510Seric vendor_pre_defaults(e)
292769510Seric 	ENVELOPE *e;
292869510Seric {
292969510Seric }
293069510Seric 
293169510Seric 
293269510Seric void
293369510Seric vendor_post_defaults(e)
293469510Seric 	ENVELOPE *e;
293569510Seric {
293669510Seric }
293769510Seric /*
293864816Seric **  STRTOL -- convert string to long integer
293964816Seric **
294064816Seric **	For systems that don't have it in the C library.
294166161Seric **
294266161Seric **	This is taken verbatim from the 4.4-Lite C library.
294364816Seric */
294464816Seric 
294564816Seric #ifdef NEEDSTRTOL
294664816Seric 
294766161Seric #if defined(LIBC_SCCS) && !defined(lint)
294866161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
294966161Seric #endif /* LIBC_SCCS and not lint */
295066161Seric 
295166161Seric #include <limits.h>
295266161Seric 
295366161Seric /*
295466161Seric  * Convert a string to a long integer.
295566161Seric  *
295666161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
295766161Seric  * alphabets and digits are each contiguous.
295866161Seric  */
295966161Seric 
296064816Seric long
296166161Seric strtol(nptr, endptr, base)
296266161Seric 	const char *nptr;
296366161Seric 	char **endptr;
296466161Seric 	register int base;
296564816Seric {
296666161Seric 	register const char *s = nptr;
296766161Seric 	register unsigned long acc;
296866161Seric 	register int c;
296966161Seric 	register unsigned long cutoff;
297066161Seric 	register int neg = 0, any, cutlim;
297164816Seric 
297266161Seric 	/*
297366161Seric 	 * Skip white space and pick up leading +/- sign if any.
297466161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
297566161Seric 	 * assume decimal; if base is already 16, allow 0x.
297666161Seric 	 */
297766161Seric 	do {
297866161Seric 		c = *s++;
297966161Seric 	} while (isspace(c));
298066161Seric 	if (c == '-') {
298166161Seric 		neg = 1;
298266161Seric 		c = *s++;
298366161Seric 	} else if (c == '+')
298466161Seric 		c = *s++;
298566161Seric 	if ((base == 0 || base == 16) &&
298666161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
298766161Seric 		c = s[1];
298866161Seric 		s += 2;
298966161Seric 		base = 16;
299066161Seric 	}
299166161Seric 	if (base == 0)
299266161Seric 		base = c == '0' ? 8 : 10;
299364816Seric 
299466161Seric 	/*
299566161Seric 	 * Compute the cutoff value between legal numbers and illegal
299666161Seric 	 * numbers.  That is the largest legal value, divided by the
299766161Seric 	 * base.  An input number that is greater than this value, if
299866161Seric 	 * followed by a legal input character, is too big.  One that
299966161Seric 	 * is equal to this value may be valid or not; the limit
300066161Seric 	 * between valid and invalid numbers is then based on the last
300166161Seric 	 * digit.  For instance, if the range for longs is
300266161Seric 	 * [-2147483648..2147483647] and the input base is 10,
300366161Seric 	 * cutoff will be set to 214748364 and cutlim to either
300466161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
300566161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
300666161Seric 	 * the number is too big, and we will return a range error.
300766161Seric 	 *
300866161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
300966161Seric 	 * overflow.
301066161Seric 	 */
301166161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
301266161Seric 	cutlim = cutoff % (unsigned long)base;
301366161Seric 	cutoff /= (unsigned long)base;
301466161Seric 	for (acc = 0, any = 0;; c = *s++) {
301566161Seric 		if (isdigit(c))
301666161Seric 			c -= '0';
301766161Seric 		else if (isalpha(c))
301866161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
301966161Seric 		else
302066161Seric 			break;
302166161Seric 		if (c >= base)
302266161Seric 			break;
302366161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
302466161Seric 			any = -1;
302566161Seric 		else {
302666161Seric 			any = 1;
302766161Seric 			acc *= base;
302866161Seric 			acc += c;
302964816Seric 		}
303064816Seric 	}
303166161Seric 	if (any < 0) {
303266161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
303366161Seric 		errno = ERANGE;
303466161Seric 	} else if (neg)
303566161Seric 		acc = -acc;
303666161Seric 	if (endptr != 0)
303766161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
303866161Seric 	return (acc);
303964816Seric }
304064816Seric 
304164816Seric #endif
304264841Seric /*
304369679Seric **  STRSTR -- find first substring in string
304469679Seric **
304569679Seric **	Parameters:
304669679Seric **		big -- the big (full) string.
304769679Seric **		little -- the little (sub) string.
304869679Seric **
304969679Seric **	Returns:
305069679Seric **		A pointer to the first instance of little in big.
305169679Seric **		big if little is the null string.
305269679Seric **		NULL if little is not contained in big.
305369679Seric */
305469679Seric 
305569679Seric #ifdef NEEDSTRSTR
305669679Seric 
305769679Seric char *
305869679Seric strstr(big, little)
305969679Seric 	char *big;
306069679Seric 	char *little;
306169679Seric {
306269679Seric 	register char *p = big;
306369679Seric 	int l;
306469679Seric 
306569679Seric 	if (*little == '\0')
306669679Seric 		return big;
306769679Seric 	l = strlen(little);
306869679Seric 
306969679Seric 	while ((p = strchr(p, *little)) != NULL)
307069679Seric 	{
307169679Seric 		if (strncmp(p, little, l) == 0)
307269679Seric 			return p;
307369679Seric 		p++;
307469679Seric 	}
307569679Seric 	return NULL;
307669679Seric }
307769679Seric 
307869679Seric #endif
307969679Seric /*
308068693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
308164841Seric **
308268693Seric **	Some operating systems have wierd problems with the gethostbyXXX
308368693Seric **	routines.  For example, Solaris versions at least through 2.3
308468693Seric **	don't properly deliver a canonical h_name field.  This tries to
308568693Seric **	work around these problems.
308664841Seric */
308764841Seric 
308864841Seric struct hostent *
308968693Seric sm_gethostbyname(name)
309068707Seric 	char *name;
309164841Seric {
309269654Seric #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4)
309368693Seric 	extern int h_errno;
309468693Seric 
309568508Seric # if SOLARIS == 203
309664841Seric 	static struct hostent hp;
309764841Seric 	static char buf[1000];
309864841Seric 	extern struct hostent *_switch_gethostbyname_r();
309964841Seric 
310064841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
310164841Seric # else
310265009Seric 	extern struct hostent *__switch_gethostbyname();
310365009Seric 
310464841Seric 	return __switch_gethostbyname(name);
310564841Seric # endif
310668693Seric #else
310769687Seric 	struct hostent *h;
310869687Seric 	int nmaps;
310969687Seric 	char *maptype[MAXMAPSTACK];
311069687Seric 	short mapreturn[MAXMAPACTIONS];
311169687Seric 	char hbuf[MAXNAME];
311269687Seric 
311369687Seric 	h = gethostbyname(name);
311469687Seric # if defined(NIS)
311569687Seric 	if (h != NULL)
311669687Seric 		return h;
311769687Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
311869687Seric 	while (--nmaps >= 0)
311969687Seric 		if (strcmp(maptype[nmaps], "nis") == 0)
312069687Seric 			break;
312169687Seric 	if (nmaps >= 0)
312269687Seric 	{
312369687Seric 		/* try short name */
312469687Seric 		if (strlen(name) > sizeof hbuf - 1)
312569687Seric 			return NULL;
312669687Seric 		strcpy(hbuf, name);
312769687Seric 		shorten_hostname(hbuf);
312869687Seric 
312969687Seric 		/* if it hasn't been shortened, there's no point */
313069687Seric 		if (strcmp(hbuf, name) != 0)
313169687Seric 			return gethostbyname(hbuf);
313269687Seric 	}
313369687Seric # endif
313469687Seric 	return h;
313568693Seric #endif
313664841Seric }
313764841Seric 
313864841Seric struct hostent *
313968693Seric sm_gethostbyaddr(addr, len, type)
314068707Seric 	char *addr;
314164841Seric 	int len;
314264841Seric 	int type;
314364841Seric {
314468693Seric #if defined(SOLARIS) && SOLARIS < 204
314568693Seric 	extern int h_errno;
314668693Seric 
314768508Seric # if SOLARIS == 203
314864841Seric 	static struct hostent hp;
314964841Seric 	static char buf[1000];
315064841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
315164841Seric 
315264841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
315364841Seric # else
315465009Seric 	extern struct hostent *__switch_gethostbyaddr();
315565009Seric 
315664841Seric 	return __switch_gethostbyaddr(addr, len, type);
315764841Seric # endif
315868693Seric #else
315968693Seric 	return gethostbyaddr(addr, len, type);
316068693Seric #endif
316164841Seric }
316268693Seric /*
316368693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
316468693Seric */
316564841Seric 
316668693Seric struct passwd *
316768693Seric sm_getpwnam(user)
316868704Seric 	char *user;
316968693Seric {
317068693Seric 	return getpwnam(user);
317168693Seric }
317268693Seric 
317368693Seric struct passwd *
317468693Seric sm_getpwuid(uid)
317568693Seric 	uid_t uid;
317668693Seric {
317768693Seric 	return getpwuid(uid);
317868693Seric }
317965214Seric /*
318069472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
318169472Seric **
318269472Seric **	Parameters:
318369472Seric **		none.
318469472Seric **
318569472Seric **	Returns:
318669472Seric **		none.
318769472Seric **
318869472Seric **	Side Effects:
318969472Seric **		Loads $=w with the names of all the interfaces.
319069472Seric */
319169472Seric 
319269472Seric #ifdef SIOCGIFCONF
319369565Seric struct rtentry;
319469565Seric struct mbuf;
319569472Seric # include <arpa/inet.h>
319669562Seric # include <sys/time.h>
319769472Seric # include <net/if.h>
319869472Seric #endif
319969472Seric 
320069472Seric void
320169472Seric load_if_names()
320269472Seric {
320369472Seric #ifdef SIOCGIFCONF
320469472Seric 	int s;
320569516Seric 	int i;
320669472Seric         struct ifconf ifc;
320769472Seric 	char interfacebuf[1024];
320869472Seric 
320969472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
321069472Seric 	if (s == -1)
321169472Seric 		return;
321269472Seric 
321369472Seric 	/* get the list of known IP address from the kernel */
321469472Seric         ifc.ifc_buf = interfacebuf;
321569516Seric         ifc.ifc_len = sizeof interfacebuf;
321669472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
321769472Seric 	{
321869472Seric 		if (tTd(0, 4))
321969472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
322069516Seric 		close(s);
322169472Seric 		return;
322269472Seric 	}
322369516Seric 	close(s);
322469472Seric 
322569472Seric 	/* scan the list of IP address */
322669472Seric 	if (tTd(0, 4))
322769472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
322869472Seric 			ifc.ifc_len);
322969472Seric 
323069516Seric 	for (i = 0; i < ifc.ifc_len; )
323169516Seric 	{
323269516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
323369516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
323469472Seric 		struct in_addr ia;
323569516Seric 		struct hostent *hp;
323669472Seric 		char ip_addr[256];
323769516Seric 		extern char *inet_ntoa();
323869516Seric 		extern struct hostent *gethostbyaddr();
323969472Seric 
324069516Seric #ifdef BSD4_4_SOCKADDR
324169516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
324269516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
324369516Seric 		else
324469516Seric #endif
324569516Seric 			i += sizeof *ifr;
324669516Seric 
324769472Seric 		if (tTd(0, 20))
324869516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
324969472Seric 
325069472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
325169472Seric 			continue;
325269472Seric 
325369472Seric 		/* extract IP address from the list*/
325469516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
325569472Seric 
325669472Seric 		/* save IP address in text from */
325769472Seric 		(void) sprintf(ip_addr, "[%s]",
325869516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
325969472Seric 		if (!wordinclass(ip_addr, 'w'))
326069472Seric 		{
326169472Seric 			setclass('w', ip_addr);
326269472Seric 			if (tTd(0, 4))
326369472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
326469472Seric 		}
326569472Seric 
326669472Seric 		/* skip "loopback" interface "lo" */
326769472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
326869472Seric 			continue;
326969472Seric 
327069472Seric 		/* lookup name with IP address */
327169472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
327269472Seric 		if (hp == NULL)
327369472Seric 		{
327469472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
327569472Seric 				inet_ntoa(ia));
327669472Seric 			continue;
327769472Seric 		}
327869472Seric 
327969472Seric 		/* save its cname */
328069472Seric 		if (!wordinclass(hp->h_name, 'w'))
328169472Seric 		{
328269472Seric 			setclass('w', hp->h_name);
328369472Seric 			if (tTd(0, 4))
328469472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
328569472Seric 		}
328669472Seric 
328769472Seric 		/* save all it aliases name */
328869472Seric 		while (*hp->h_aliases)
328969472Seric 		{
329069472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
329169472Seric 			{
329269472Seric 				setclass('w', *hp->h_aliases);
329369472Seric 				if (tTd(0, 4))
329469472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
329569472Seric 			}
329669472Seric 			hp->h_aliases++;
329769472Seric 		}
329869472Seric 	}
329969472Seric #endif
330069472Seric }
330169472Seric /*
330265214Seric **  NI_PROPVAL -- netinfo property value lookup routine
330365214Seric **
330465214Seric **	Parameters:
330568508Seric **		keydir -- the Netinfo directory name in which to search
330668508Seric **			for the key.
330768508Seric **		keyprop -- the name of the property in which to find the
330868508Seric **			property we are interested.  Defaults to "name".
330968508Seric **		keyval -- the value for which we are really searching.
331068508Seric **		valprop -- the property name for the value in which we
331168508Seric **			are interested.
331268508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
331368508Seric **			we should return a string separated by this
331468508Seric **			character.
331565214Seric **
331665214Seric **	Returns:
331765214Seric **		NULL -- if:
331865214Seric **			1. the directory is not found
331965214Seric **			2. the property name is not found
332065214Seric **			3. the property contains multiple values
332165214Seric **			4. some error occured
332265214Seric **		else -- the location of the config file.
332365214Seric **
332468508Seric **	Example:
332568508Seric **		To search for an alias value, use:
332668508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
332768508Seric **
332865214Seric **	Notes:
332965214Seric **      	Caller should free the return value of ni_proval
333065214Seric */
333165214Seric 
333265214Seric #ifdef NETINFO
333365214Seric 
333465214Seric # include <netinfo/ni.h>
333565214Seric 
333665214Seric # define LOCAL_NETINFO_DOMAIN    "."
333765214Seric # define PARENT_NETINFO_DOMAIN   ".."
333865214Seric # define MAX_NI_LEVELS           256
333965214Seric 
334065214Seric char *
334168508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
334268508Seric 	char *keydir;
334368508Seric 	char *keyprop;
334468508Seric 	char *keyval;
334568508Seric 	char *valprop;
334668508Seric 	char sepchar;
334765214Seric {
334865820Seric 	char *propval = NULL;
334965214Seric 	int i;
335068508Seric 	int j, alen;
335165214Seric 	void *ni = NULL;
335265214Seric 	void *lastni = NULL;
335365214Seric 	ni_status nis;
335465214Seric 	ni_id nid;
335565214Seric 	ni_namelist ninl;
335668508Seric 	register char *p;
335768508Seric 	char keybuf[1024];
335865214Seric 
335965214Seric 	/*
336068508Seric 	**  Create the full key from the two parts.
336168508Seric 	**
336268508Seric 	**	Note that directory can end with, e.g., "name=" to specify
336368508Seric 	**	an alternate search property.
336468508Seric 	*/
336568508Seric 
336668508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
336768508Seric 	if (keyprop != NULL)
336868508Seric 		i += strlen(keyprop) + 1;
336968508Seric 	if (i > sizeof keybuf)
337068508Seric 		return NULL;
337168508Seric 	strcpy(keybuf, keydir);
337268508Seric 	strcat(keybuf, "/");
337368508Seric 	if (keyprop != NULL)
337468508Seric 	{
337568508Seric 		strcat(keybuf, keyprop);
337668508Seric 		strcat(keybuf, "=");
337768508Seric 	}
337868508Seric 	strcat(keybuf, keyval);
337968508Seric 
338068508Seric 	/*
338165214Seric 	**  If the passed directory and property name are found
338265214Seric 	**  in one of netinfo domains we need to search (starting
338365214Seric 	**  from the local domain moving all the way back to the
338465214Seric 	**  root domain) set propval to the property's value
338565214Seric 	**  and return it.
338665214Seric 	*/
338765214Seric 
338865214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
338965214Seric 	{
339065214Seric 		if (i == 0)
339165214Seric 		{
339265214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
339365214Seric 		}
339465214Seric 		else
339565214Seric 		{
339665214Seric 			if (lastni != NULL)
339765214Seric 				ni_free(lastni);
339865214Seric 			lastni = ni;
339965214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
340065214Seric 		}
340165214Seric 
340265214Seric 		/*
340365214Seric 		**  Don't bother if we didn't get a handle on a
340465214Seric 		**  proper domain.  This is not necessarily an error.
340565214Seric 		**  We would get a positive ni_status if, for instance
340665214Seric 		**  we never found the directory or property and tried
340765214Seric 		**  to open the parent of the root domain!
340865214Seric 		*/
340965214Seric 
341065214Seric 		if (nis != 0)
341165214Seric 			break;
341265214Seric 
341365214Seric 		/*
341465214Seric 		**  Find the path to the server information.
341565214Seric 		*/
341665214Seric 
341768508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
341865214Seric 			continue;
341965214Seric 
342065214Seric 		/*
342168508Seric 		**  Find associated value information.
342265214Seric 		*/
342365214Seric 
342468508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
342565214Seric 			continue;
342665214Seric 
342765214Seric 		/*
342868508Seric 		**  See if we have an acceptable number of values.
342965214Seric 		*/
343065214Seric 
343168508Seric 		if (ninl.ni_namelist_len <= 0)
343268508Seric 			continue;
343368508Seric 
343468508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
343565214Seric 		{
343668508Seric 			ni_namelist_free(&ninl);
343768508Seric 			continue;
343865214Seric 		}
343968508Seric 
344069472Seric 		/*
344168508Seric 		**  Calculate number of bytes needed and build result
344268508Seric 		*/
344368508Seric 
344468508Seric 		alen = 1;
344568508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
344668508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
344768508Seric 		propval = p = xalloc(alen);
344868508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
344968508Seric 		{
345068508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
345168508Seric 			p += strlen(p);
345268508Seric 			*p++ = sepchar;
345369472Seric 		}
345468508Seric 		*--p = '\0';
345568508Seric 
345668508Seric 		ni_namelist_free(&ninl);
345765214Seric 	}
345865214Seric 
345965214Seric 	/*
346065214Seric 	**  Clean up.
346165214Seric 	*/
346265214Seric 
346365214Seric 	if (ni != NULL)
346465214Seric 		ni_free(ni);
346565214Seric 	if (lastni != NULL && ni != lastni)
346665214Seric 		ni_free(lastni);
346765214Seric 
346865214Seric 	return propval;
346965214Seric }
347065214Seric 
347165214Seric #endif /* NETINFO */
347265504Seric /*
347365504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
347465504Seric **
347565504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
347665504Seric **	syslog succeeds during interrupt handlers.
347765504Seric */
347865504Seric 
347965504Seric #ifdef __hpux
348065504Seric 
348165504Seric # define MAXSYSLOGTRIES	100
348265504Seric # undef syslog
348365504Seric 
348469758Seric # ifdef __STDC__
348569748Seric void
348665504Seric hard_syslog(int pri, char *msg, ...)
348765504Seric # else
348865504Seric hard_syslog(pri, msg, va_alist)
348965504Seric 	int pri;
349065504Seric 	char *msg;
349165504Seric 	va_dcl
349265504Seric # endif
349365504Seric {
349465504Seric 	int i;
349565504Seric 	char buf[SYSLOG_BUFSIZE * 2];
349665504Seric 	VA_LOCAL_DECL;
349765504Seric 
349865504Seric 	VA_START(msg);
349965504Seric 	vsprintf(buf, msg, ap);
350065504Seric 	VA_END;
350165504Seric 
350265504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
350365504Seric 		continue;
350465504Seric }
350565504Seric 
350665504Seric #endif
350768696Seric /*
350868696Seric **  LOCAL_HOSTNAME_LENGTH
350968696Seric **
351068696Seric **	This is required to get sendmail to compile against BIND 4.9.x
351168696Seric **	on Ultrix.
351268696Seric */
351368696Seric 
351468696Seric #if defined(ultrix) && NAMED_BIND
351568696Seric 
351668696Seric # include <resolv.h>
351768696Seric # if __RES >= 19931104
351868696Seric 
351968696Seric int
352068696Seric local_hostname_length(hostname)
352168696Seric 	char *hostname;
352268696Seric {
352368696Seric 	int len_host, len_domain;
352468696Seric 
352568696Seric 	if (!*_res.defdname)
352668696Seric 		res_init();
352768696Seric 	len_host = strlen(hostname);
352868696Seric 	len_domain = strlen(_res.defdname);
352968696Seric 	if (len_host > len_domain &&
353068696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
353168696Seric 	    hostname[len_host - len_domain - 1] == '.')
353268696Seric 		return len_host - len_domain - 1;
353368696Seric 	else
353468696Seric 		return 0;
353568696Seric }
353668696Seric 
353768696Seric # endif
353868696Seric #endif
353968741Seric /*
354068741Seric **  Compile-Time options
354168741Seric */
354268741Seric 
354368741Seric char	*CompileOptions[] =
354468741Seric {
354568741Seric #if HESIOD
354668741Seric 	"HESIOD",
354768741Seric #endif
354868741Seric #if LOG
354968741Seric 	"LOG",
355068741Seric #endif
355168741Seric #if MATCHGECOS
355268741Seric 	"MATCHGECOS",
355368741Seric #endif
355468741Seric #if NAMED_BIND
355568741Seric 	"NAMED_BIND",
355668741Seric #endif
355768741Seric #if NDBM
355868741Seric 	"NDBM",
355968741Seric #endif
356068741Seric #if NETINET
356168741Seric 	"NETINET",
356268741Seric #endif
356368741Seric #if NETINFO
356468741Seric 	"NETINFO",
356568741Seric #endif
356668741Seric #if NETISO
356768741Seric 	"NETISO",
356868741Seric #endif
356968741Seric #if NETNS
357068741Seric 	"NETNS",
357168741Seric #endif
357268741Seric #if NETUNIX
357368741Seric 	"NETUNIX",
357468741Seric #endif
357568741Seric #if NETX25
357668741Seric 	"NETX25",
357768741Seric #endif
357868741Seric #if NEWDB
357968741Seric 	"NEWDB",
358068741Seric #endif
358168741Seric #if NIS
358268741Seric 	"NIS",
358368741Seric #endif
358469518Seric #if NISPLUS
358569518Seric 	"NISPLUS",
358669518Seric #endif
358768741Seric #if SCANF
358868741Seric 	"SCANF",
358968741Seric #endif
359068741Seric #if SUID_ROOT_FILES_OK
359168741Seric 	"SUID_ROOT_FILES_OK",
359268741Seric #endif
359368741Seric #if USERDB
359468741Seric 	"USERDB",
359568741Seric #endif
359668741Seric #if XDEBUG
359768741Seric 	"XDEBUG",
359868741Seric #endif
359968741Seric #if XLA
360068741Seric 	"XLA",
360168741Seric #endif
360268741Seric 	NULL
360368741Seric };
3604