xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69563)
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*69563Seric static char sccsid[] = "@(#)conf.c	8.171 (Berkeley) 05/19/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 
26568868Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=X-Unix, A=sh -c $u");
26657403Seric 	makemailer(buf);
26757403Seric 
26868880Seric 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=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 
38068521Seric #ifdef 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 
109164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
109251920Seric 
109314872Seric #include <nlist.h>
109414872Seric 
109568508Seric #ifdef IRIX64
109668508Seric # define nlist		nlist64
109768508Seric #endif
109868508Seric 
109951920Seric #ifndef LA_AVENRUN
110063962Seric # ifdef SYSTEM5
110163962Seric #  define LA_AVENRUN	"avenrun"
110263962Seric # else
110363962Seric #  define LA_AVENRUN	"_avenrun"
110463962Seric # endif
110551920Seric #endif
110651920Seric 
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 
1278*69563Seric /* forward declarations to keep gcc from complaining */
127968869Seric struct pst_dynamic;
1280*69563Seric struct pst_status;
1281*69563Seric struct pst_static;
1282*69563Seric struct pst_vminfo;
1283*69563Seric struct pst_diskinfo;
1284*69563Seric struct pst_processor;
1285*69563Seric struct pst_lv;
1286*69563Seric 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->... */
155968508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
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 
159068508Seric # ifndef SPT_PADCHAR
159168508Seric #  define SPT_PADCHAR	' '
159268508Seric # endif
159358689Seric 
159468508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
159564843Seric 
159668508Seric #if SPT_TYPE != SPT_BUILTIN
159768508Seric 
159824943Seric /*VARARGS1*/
159968508Seric void
160068508Seric # ifdef __STDC__
160157642Seric setproctitle(char *fmt, ...)
160268508Seric # else
160357642Seric setproctitle(fmt, va_alist)
160424943Seric 	char *fmt;
160557642Seric 	va_dcl
160668508Seric # endif
160724943Seric {
160868508Seric # if SPT_TYPE != SPT_NONE
160924943Seric 	register char *p;
161025049Seric 	register int i;
161159732Seric 	SETPROC_STATIC char buf[MAXLINE];
161256852Seric 	VA_LOCAL_DECL
161368508Seric #  if SPT_TYPE == SPT_PSTAT
161458689Seric 	union pstun pst;
161558689Seric #  endif
161624943Seric 	extern char **Argv;
161724943Seric 	extern char *LastArgv;
161824943Seric 
161958674Seric 	p = buf;
162024943Seric 
162158674Seric 	/* print sendmail: heading for grep */
162258674Seric 	(void) strcpy(p, "sendmail: ");
162358674Seric 	p += strlen(p);
162424943Seric 
162558674Seric 	/* print the argument string */
162658674Seric 	VA_START(fmt);
162758674Seric 	(void) vsprintf(p, fmt, ap);
162856852Seric 	VA_END;
162954996Seric 
163058674Seric 	i = strlen(buf);
163158689Seric 
163268508Seric #  if SPT_TYPE == SPT_PSTAT
163358689Seric 	pst.pst_command = buf;
163458689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
163558689Seric #  else
163668508Seric #   if SPT_TYPE == SPT_PSSTRINGS
163759732Seric 	PS_STRINGS->ps_nargvstr = 1;
163859732Seric 	PS_STRINGS->ps_argvstr = buf;
163959732Seric #   else
164054996Seric 	if (i > LastArgv - Argv[0] - 2)
164125049Seric 	{
164254996Seric 		i = LastArgv - Argv[0] - 2;
164358674Seric 		buf[i] = '\0';
164425049Seric 	}
164558674Seric 	(void) strcpy(Argv[0], buf);
164654997Seric 	p = &Argv[0][i];
164724943Seric 	while (p < LastArgv)
164868508Seric 		*p++ = SPT_PADCHAR;
164968508Seric 	Argv[1] = NULL;
165068508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
165168508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
165268508Seric # endif /* SPT_TYPE != SPT_NONE */
165324943Seric }
165465982Seric 
165568508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
165625698Seric /*
165725698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
165825698Seric **
165925698Seric **	Parameters:
166068508Seric **		sig -- the signal that got us here (unused).
166125698Seric **
166225698Seric **	Returns:
166325698Seric **		none.
166425698Seric **
166525698Seric **	Side Effects:
166625698Seric **		Picks up extant zombies.
166725698Seric */
166825698Seric 
166946928Sbostic void
167068508Seric reapchild(sig)
167168508Seric 	int sig;
167225698Seric {
167364738Seric 	int olderrno = errno;
167463966Seric # ifdef HASWAITPID
167560219Seric 	auto int status;
167660560Seric 	int count;
167760560Seric 	int pid;
167860219Seric 
167960560Seric 	count = 0;
168060560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
168160560Seric 	{
168260560Seric 		if (count++ > 1000)
168360560Seric 		{
168466748Seric #ifdef LOG
168560560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
168660560Seric 				pid, status);
168766748Seric #endif
168860560Seric 			break;
168960560Seric 		}
169060560Seric 	}
169160219Seric # else
169225698Seric # ifdef WNOHANG
169325698Seric 	union wait status;
169425698Seric 
169563753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
169625698Seric 		continue;
169756795Seric # else /* WNOHANG */
169825698Seric 	auto int status;
169925698Seric 
170060219Seric 	while (wait(&status) > 0)
170125698Seric 		continue;
170256795Seric # endif /* WNOHANG */
170360219Seric # endif
170464035Seric # ifdef SYS5SIGNALS
170564035Seric 	(void) setsignal(SIGCHLD, reapchild);
170658061Seric # endif
170764738Seric 	errno = olderrno;
170825698Seric }
170955418Seric /*
171055418Seric **  UNSETENV -- remove a variable from the environment
171155418Seric **
171255418Seric **	Not needed on newer systems.
171355418Seric **
171455418Seric **	Parameters:
171555418Seric **		name -- the string name of the environment variable to be
171655418Seric **			deleted from the current environment.
171755418Seric **
171855418Seric **	Returns:
171955418Seric **		none.
172055418Seric **
172155418Seric **	Globals:
172255418Seric **		environ -- a pointer to the current environment.
172355418Seric **
172455418Seric **	Side Effects:
172555418Seric **		Modifies environ.
172655418Seric */
172755418Seric 
172863962Seric #ifndef HASUNSETENV
172955418Seric 
173055418Seric void
173155418Seric unsetenv(name)
173255418Seric 	char *name;
173355418Seric {
173455418Seric 	extern char **environ;
173555418Seric 	register char **pp;
173655418Seric 	int len = strlen(name);
173755418Seric 
173855418Seric 	for (pp = environ; *pp != NULL; pp++)
173955418Seric 	{
174055418Seric 		if (strncmp(name, *pp, len) == 0 &&
174155418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
174255418Seric 			break;
174355418Seric 	}
174455418Seric 
174555418Seric 	for (; *pp != NULL; pp++)
174655418Seric 		*pp = pp[1];
174755418Seric }
174855418Seric 
174963962Seric #endif
175056215Seric /*
175156215Seric **  GETDTABLESIZE -- return number of file descriptors
175256215Seric **
175356215Seric **	Only on non-BSD systems
175456215Seric **
175556215Seric **	Parameters:
175656215Seric **		none
175756215Seric **
175856215Seric **	Returns:
175956215Seric **		size of file descriptor table
176056215Seric **
176156215Seric **	Side Effects:
176256215Seric **		none
176356215Seric */
176456215Seric 
176563787Seric #ifdef SOLARIS
176663787Seric # include <sys/resource.h>
176763787Seric #endif
176856215Seric 
176956215Seric int
177063787Seric getdtsize()
177156215Seric {
177263787Seric #ifdef RLIMIT_NOFILE
177363787Seric 	struct rlimit rl;
177463787Seric 
177563787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
177663787Seric 		return rl.rlim_cur;
177763787Seric #endif
177863787Seric 
177964031Seric # ifdef HASGETDTABLESIZE
178064031Seric 	return getdtablesize();
178164031Seric # else
178264031Seric #  ifdef _SC_OPEN_MAX
178358689Seric 	return sysconf(_SC_OPEN_MAX);
178463787Seric #  else
178556215Seric 	return NOFILE;
178663787Seric #  endif
178758689Seric # endif
178856215Seric }
178957631Seric /*
179057631Seric **  UNAME -- get the UUCP name of this system.
179157631Seric */
179257631Seric 
179357943Seric #ifndef HASUNAME
179457631Seric 
179557631Seric int
179657631Seric uname(name)
179757631Seric 	struct utsname *name;
179857631Seric {
179957631Seric 	FILE *file;
180057631Seric 	char *n;
180157631Seric 
180257631Seric 	name->nodename[0] = '\0';
180357631Seric 
180457661Seric 	/* try /etc/whoami -- one line with the node name */
180557631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
180657631Seric 	{
180757661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
180857631Seric 		(void) fclose(file);
180957661Seric 		n = strchr(name->nodename, '\n');
181057631Seric 		if (n != NULL)
181157631Seric 			*n = '\0';
181257631Seric 		if (name->nodename[0] != '\0')
181357631Seric 			return (0);
181457631Seric 	}
181557631Seric 
181657661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
181757631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
181857631Seric 	{
181957631Seric 		char buf[MAXLINE];
182057631Seric 
182157631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
182257631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
182357631Seric 					NODE_LENGTH, name->nodename) > 0)
182457631Seric 				break;
182557631Seric 		(void) fclose(file);
182657631Seric 		if (name->nodename[0] != '\0')
182757631Seric 			return (0);
182857631Seric 	}
182957631Seric 
183057631Seric #ifdef TRUST_POPEN
183157631Seric 	/*
183257631Seric 	**  Popen is known to have security holes.
183357631Seric 	*/
183457631Seric 
183557661Seric 	/* try uuname -l to return local name */
183657631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
183757631Seric 	{
183857661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
183957631Seric 		(void) pclose(file);
184057661Seric 		n = strchr(name, '\n');
184157631Seric 		if (n != NULL)
184257631Seric 			*n = '\0';
184357661Seric 		if (name->nodename[0] != '\0')
184457631Seric 			return (0);
184557631Seric 	}
184657631Seric #endif
184769472Seric 
184857631Seric 	return (-1);
184957631Seric }
185057943Seric #endif /* HASUNAME */
185158068Seric /*
185258068Seric **  INITGROUPS -- initialize groups
185358068Seric **
185458068Seric **	Stub implementation for System V style systems
185558068Seric */
185658068Seric 
185758068Seric #ifndef HASINITGROUPS
185858068Seric 
185958068Seric initgroups(name, basegid)
186058068Seric 	char *name;
186158068Seric 	int basegid;
186258068Seric {
186358068Seric 	return 0;
186458068Seric }
186558068Seric 
186658068Seric #endif
186758082Seric /*
186859289Seric **  SETSID -- set session id (for non-POSIX systems)
186959289Seric */
187059289Seric 
187159289Seric #ifndef HASSETSID
187259289Seric 
187363753Seric pid_t
187463753Seric setsid __P ((void))
187559289Seric {
187664155Seric #ifdef TIOCNOTTY
187764155Seric 	int fd;
187864155Seric 
187966743Seric 	fd = open("/dev/tty", O_RDWR, 0);
188064155Seric 	if (fd >= 0)
188164155Seric 	{
188264155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
188364155Seric 		(void) close(fd);
188464155Seric 	}
188564155Seric #endif /* TIOCNOTTY */
188664705Seric # ifdef SYS5SETPGRP
188763753Seric 	return setpgrp();
188863753Seric # else
188964155Seric 	return setpgid(0, getpid());
189059289Seric # endif
189159289Seric }
189259289Seric 
189359289Seric #endif
189459289Seric /*
189566748Seric **  FSYNC -- dummy fsync
189666748Seric */
189766748Seric 
189866748Seric #ifdef NEEDFSYNC
189966748Seric 
190066748Seric fsync(fd)
190166748Seric 	int fd;
190266748Seric {
190366748Seric # ifdef O_SYNC
190466748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
190566748Seric # else
190666748Seric 	/* nothing we can do */
190766748Seric 	return 0;
190866748Seric # endif
190966748Seric }
191066748Seric 
191166748Seric #endif
191266748Seric /*
191364718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
191464718Seric **
191564718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
191668508Seric **	instead of a long.  This patches things.  Only needed on versions
191768508Seric **	prior to 5.4.3.
191864718Seric */
191964718Seric 
192068508Seric #ifdef DGUX_5_4_2
192164718Seric 
192264718Seric #undef inet_addr
192364718Seric 
192464718Seric long
192564718Seric dgux_inet_addr(host)
192664718Seric 	char *host;
192764718Seric {
192864718Seric 	struct in_addr haddr;
192964718Seric 
193064718Seric 	haddr = inet_addr(host);
193164718Seric 	return haddr.s_addr;
193264718Seric }
193364718Seric 
193464718Seric #endif
193564718Seric /*
193663969Seric **  GETOPT -- for old systems or systems with bogus implementations
193763969Seric */
193863969Seric 
193963969Seric #ifdef NEEDGETOPT
194063969Seric 
194163969Seric /*
194263969Seric  * Copyright (c) 1985 Regents of the University of California.
194363969Seric  * All rights reserved.  The Berkeley software License Agreement
194463969Seric  * specifies the terms and conditions for redistribution.
194563969Seric  */
194663969Seric 
194763969Seric 
194863969Seric /*
194963969Seric ** this version hacked to add `atend' flag to allow state machine
195063969Seric ** to reset if invoked by the program to scan args for a 2nd time
195163969Seric */
195263969Seric 
195363969Seric #if defined(LIBC_SCCS) && !defined(lint)
195463969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
195564072Seric #endif /* LIBC_SCCS and not lint */
195663969Seric 
195763969Seric #include <stdio.h>
195863969Seric 
195963969Seric /*
196063969Seric  * get option letter from argument vector
196163969Seric  */
196265352Seric #ifdef _CONVEX_SOURCE
196368508Seric extern int	optind, opterr, optopt;
196468508Seric extern char	*optarg;
196565352Seric #else
196665352Seric int	opterr = 1;		/* if error message should be printed */
196765352Seric int	optind = 1;		/* index into parent argv vector */
196868470Seric int	optopt = 0;		/* character checked for validity */
196968470Seric char	*optarg = NULL;		/* argument associated with option */
197068508Seric #endif
197163969Seric 
197263969Seric #define BADCH	(int)'?'
197363969Seric #define EMSG	""
197463969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
197563969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
197663969Seric 
197763969Seric getopt(nargc,nargv,ostr)
197865160Seric 	int		nargc;
197965160Seric 	char *const	*nargv;
198065160Seric 	const char	*ostr;
198163969Seric {
198263969Seric 	static char	*place = EMSG;	/* option letter processing */
198363969Seric 	static char	atend = 0;
198463969Seric 	register char	*oli;		/* option letter list index */
198563969Seric 
198663969Seric 	if (atend) {
198763969Seric 		atend = 0;
198863969Seric 		place = EMSG;
198963969Seric 	}
199063969Seric 	if(!*place) {			/* update scanning pointer */
199164136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
199263969Seric 			atend++;
199363969Seric 			return(EOF);
199463969Seric 		}
199563969Seric 		if (*place == '-') {	/* found "--" */
199663969Seric 			++optind;
199763969Seric 			atend++;
199863969Seric 			return(EOF);
199963969Seric 		}
200063969Seric 	}				/* option letter okay? */
200164125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
200264136Seric 		if (!*place) ++optind;
200363969Seric 		tell(": illegal option -- ");
200463969Seric 	}
200563969Seric 	if (*++oli != ':') {		/* don't need argument */
200663969Seric 		optarg = NULL;
200763969Seric 		if (!*place) ++optind;
200863969Seric 	}
200963969Seric 	else {				/* need an argument */
201063969Seric 		if (*place) optarg = place;	/* no white space */
201163969Seric 		else if (nargc <= ++optind) {	/* no arg */
201263969Seric 			place = EMSG;
201363969Seric 			tell(": option requires an argument -- ");
201463969Seric 		}
201563969Seric 	 	else optarg = nargv[optind];	/* white space */
201663969Seric 		place = EMSG;
201763969Seric 		++optind;
201863969Seric 	}
201963969Seric 	return(optopt);			/* dump back option letter */
202063969Seric }
202163969Seric 
202263969Seric #endif
202363969Seric /*
202463969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
202563969Seric */
202663969Seric 
202763969Seric #ifdef NEEDVPRINTF
202863969Seric 
202963969Seric #define MAXARG	16
203063969Seric 
203163969Seric vfprintf(fp, fmt, ap)
203263969Seric 	FILE *	fp;
203363969Seric 	char *	fmt;
203463969Seric 	char **	ap;
203563969Seric {
203663969Seric 	char *	bp[MAXARG];
203763969Seric 	int	i = 0;
203863969Seric 
203963969Seric 	while (*ap && i < MAXARG)
204063969Seric 		bp[i++] = *ap++;
204163969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
204263969Seric 			 bp[4], bp[5], bp[6], bp[7],
204363969Seric 			 bp[8], bp[9], bp[10], bp[11],
204463969Seric 			 bp[12], bp[13], bp[14], bp[15]);
204563969Seric }
204663969Seric 
204763969Seric vsprintf(s, fmt, ap)
204863969Seric 	char *	s;
204963969Seric 	char *	fmt;
205063969Seric 	char **	ap;
205163969Seric {
205263969Seric 	char *	bp[MAXARG];
205363969Seric 	int	i = 0;
205463969Seric 
205563969Seric 	while (*ap && i < MAXARG)
205663969Seric 		bp[i++] = *ap++;
205763969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
205863969Seric 			bp[4], bp[5], bp[6], bp[7],
205963969Seric 			bp[8], bp[9], bp[10], bp[11],
206063969Seric 			bp[12], bp[13], bp[14], bp[15]);
206163969Seric }
206263969Seric 
206363969Seric #endif
206463969Seric /*
206565220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
206665208Seric **
206765211Seric **	Parameters:
206865211Seric **		shell -- the user's shell from /etc/passwd
206965211Seric **
207065211Seric **	Returns:
207165211Seric **		TRUE -- if it is ok to use this for unrestricted access.
207265211Seric **		FALSE -- if the shell is restricted.
207365208Seric */
207465208Seric 
207565587Seric #if !HASGETUSERSHELL
207665587Seric 
207765587Seric # ifndef _PATH_SHELLS
207865587Seric #  define _PATH_SHELLS	"/etc/shells"
207965587Seric # endif
208065587Seric 
208165587Seric char	*DefaultUserShells[] =
208265587Seric {
208368508Seric 	"/bin/sh",		/* standard shell */
208465587Seric 	"/usr/bin/sh",
208568508Seric 	"/bin/csh",		/* C shell */
208665587Seric 	"/usr/bin/csh",
208765587Seric #ifdef __hpux
208868889Seric # ifdef V4FS
208968889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
209068889Seric 	"/usr/bin/ksh",		/* Korn shell */
209168889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
209268889Seric 	"/usr/bin/pam",
209368889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
209468889Seric 	"/usr/bin/posix/sh",
209568889Seric # else
209668508Seric 	"/bin/rsh",		/* restricted Bourne shell */
209768508Seric 	"/bin/ksh",		/* Korn shell */
209868508Seric 	"/bin/rksh",		/* restricted Korn shell */
209965587Seric 	"/bin/pam",
210068508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
210165587Seric 	"/bin/posix/sh",
210268889Seric # endif
210365211Seric #endif
210468508Seric #ifdef _AIX3
210568508Seric 	"/bin/ksh",		/* Korn shell */
210668508Seric 	"/usr/bin/ksh",
210768508Seric 	"/bin/tsh",		/* trusted shell */
210868508Seric 	"/usr/bin/tsh",
210968508Seric 	"/bin/bsh",		/* Bourne shell */
211068508Seric 	"/usr/bin/bsh",
211168508Seric #endif
211265587Seric 	NULL
211365587Seric };
211465587Seric 
211565208Seric #endif
211665208Seric 
211765747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
211865747Seric 
211965211Seric bool
212065211Seric usershellok(shell)
212165211Seric 	char *shell;
212265208Seric {
212365211Seric #if HASGETUSERSHELL
212465211Seric 	register char *p;
212565211Seric 	extern char *getusershell();
212665208Seric 
212769505Seric 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
212868457Seric 		return TRUE;
212968457Seric 
213065211Seric 	setusershell();
213165211Seric 	while ((p = getusershell()) != NULL)
213265747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
213365211Seric 			break;
213465211Seric 	endusershell();
213565211Seric 	return p != NULL;
213665211Seric #else
213765211Seric 	register FILE *shellf;
213865211Seric 	char buf[MAXLINE];
213965208Seric 
214068457Seric 	if (shell == NULL || shell[0] == '\0')
214168457Seric 		return TRUE;
214268457Seric 
214365211Seric 	shellf = fopen(_PATH_SHELLS, "r");
214465211Seric 	if (shellf == NULL)
214565211Seric 	{
214665211Seric 		/* no /etc/shells; see if it is one of the std shells */
214765587Seric 		char **d;
214865587Seric 
214965587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
215065587Seric 		{
215165587Seric 			if (strcmp(shell, *d) == 0)
215265587Seric 				return TRUE;
215365587Seric 		}
215465587Seric 		return FALSE;
215565211Seric 	}
215665208Seric 
215765211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
215865211Seric 	{
215965211Seric 		register char *p, *q;
216065208Seric 
216165211Seric 		p = buf;
216265211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
216365211Seric 			p++;
216465211Seric 		if (*p == '#' || *p == '\0')
216565208Seric 			continue;
216665211Seric 		q = p;
216765211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
216865211Seric 			p++;
216965211Seric 		*p = '\0';
217065747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
217165211Seric 		{
217265211Seric 			fclose(shellf);
217365211Seric 			return TRUE;
217465211Seric 		}
217565208Seric 	}
217665211Seric 	fclose(shellf);
217765211Seric 	return FALSE;
217865211Seric #endif
217965208Seric }
218065208Seric /*
218164311Seric **  FREESPACE -- see how much free space is on the queue filesystem
218258082Seric **
218358082Seric **	Only implemented if you have statfs.
218458082Seric **
218558082Seric **	Parameters:
218664311Seric **		dir -- the directory in question.
218764311Seric **		bsize -- a variable into which the filesystem
218864311Seric **			block size is stored.
218958082Seric **
219058082Seric **	Returns:
219164311Seric **		The number of bytes free on the queue filesystem.
219264311Seric **		-1 if the statfs call fails.
219364311Seric **
219464311Seric **	Side effects:
219564311Seric **		Puts the filesystem block size into bsize.
219658082Seric */
219758082Seric 
219865749Seric /* statfs types */
219965749Seric #define SFS_NONE	0	/* no statfs implementation */
220065749Seric #define SFS_USTAT	1	/* use ustat */
220165749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
220265749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
220365749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
220465749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
220566752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
220665749Seric 
220765749Seric #ifndef SFS_TYPE
220865749Seric # define SFS_TYPE	SFS_NONE
220958157Seric #endif
221058157Seric 
221165749Seric #if SFS_TYPE == SFS_USTAT
221258157Seric # include <ustat.h>
221358157Seric #endif
221465749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
221565749Seric # include <sys/statfs.h>
221658082Seric #endif
221765749Seric #if SFS_TYPE == SFS_VFS
221865749Seric # include <sys/vfs.h>
221965749Seric #endif
222065749Seric #if SFS_TYPE == SFS_MOUNT
222165749Seric # include <sys/mount.h>
222265749Seric #endif
222366752Seric #if SFS_TYPE == SFS_STATVFS
222466752Seric # include <sys/statvfs.h>
222566752Seric #endif
222658082Seric 
222764311Seric long
222864311Seric freespace(dir, bsize)
222964311Seric 	char *dir;
223064311Seric 	long *bsize;
223158082Seric {
223265749Seric #if SFS_TYPE != SFS_NONE
223365749Seric # if SFS_TYPE == SFS_USTAT
223458153Seric 	struct ustat fs;
223558153Seric 	struct stat statbuf;
223658366Seric #  define FSBLOCKSIZE	DEV_BSIZE
223768695Seric #  define SFS_BAVAIL	f_tfree
223858157Seric # else
223958157Seric #  if defined(ultrix)
224058157Seric 	struct fs_data fs;
224168695Seric #   define SFS_BAVAIL	fd_bfreen
224268508Seric #   define FSBLOCKSIZE	1024L
224358153Seric #  else
224466752Seric #   if SFS_TYPE == SFS_STATVFS
224566752Seric 	struct statvfs fs;
224668508Seric #    define FSBLOCKSIZE	fs.f_frsize
224766752Seric #   else
224858082Seric 	struct statfs fs;
224966752Seric #    define FSBLOCKSIZE	fs.f_bsize
225063987Seric #   endif
225158153Seric #  endif
225258133Seric # endif
225368695Seric # ifndef SFS_BAVAIL
225468695Seric #  define SFS_BAVAIL f_bavail
225568508Seric # endif
225658082Seric 
225765749Seric # if SFS_TYPE == SFS_USTAT
225864311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
225958157Seric # else
226065749Seric #  if SFS_TYPE == SFS_4ARGS
226164311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
226258157Seric #  else
226368508Seric #   if SFS_TYPE == SFS_STATVFS
226468508Seric 	if (statvfs(dir, &fs) == 0)
226568508Seric #   else
226668508Seric #    if defined(ultrix)
226768499Seric 	if (statfs(dir, &fs) > 0)
226868508Seric #    else
226964311Seric 	if (statfs(dir, &fs) == 0)
227068508Seric #    endif
227158153Seric #   endif
227258133Seric #  endif
227358133Seric # endif
227458133Seric 	{
227564311Seric 		if (bsize != NULL)
227664311Seric 			*bsize = FSBLOCKSIZE;
227768695Seric 		return (fs.SFS_BAVAIL);
227864311Seric 	}
227964337Seric #endif
228064311Seric 	return (-1);
228164311Seric }
228264311Seric /*
228364311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
228464311Seric **
228564311Seric **	Only implemented if you have statfs.
228664311Seric **
228764311Seric **	Parameters:
228864311Seric **		msize -- the size to check against.  If zero, we don't yet
228964311Seric **		know how big the message will be, so just check for
229064311Seric **		a "reasonable" amount.
229164311Seric **
229264311Seric **	Returns:
229364311Seric **		TRUE if there is enough space.
229464311Seric **		FALSE otherwise.
229564311Seric */
229664311Seric 
229764311Seric bool
229864311Seric enoughspace(msize)
229964311Seric 	long msize;
230064311Seric {
230164311Seric 	long bfree, bsize;
230264311Seric 
230364311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
230464311Seric 	{
230558133Seric 		if (tTd(4, 80))
230664311Seric 			printf("enoughspace: no threshold\n");
230764311Seric 		return TRUE;
230864311Seric 	}
230964311Seric 
231064311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
231164311Seric 	{
231264311Seric 		if (tTd(4, 80))
231358333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
231464311Seric 				bfree, msize);
231558333Seric 
231658333Seric 		/* convert msize to block count */
231764311Seric 		msize = msize / bsize + 1;
231858333Seric 		if (MinBlocksFree >= 0)
231958333Seric 			msize += MinBlocksFree;
232058333Seric 
232164311Seric 		if (bfree < msize)
232258090Seric 		{
232358090Seric #ifdef LOG
232458090Seric 			if (LogLevel > 0)
232564254Seric 				syslog(LOG_ALERT,
232664254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
232768508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
232868508Seric 					bfree,
232968508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
233068508Seric 					msize, QueueDir);
233158090Seric #endif
233258082Seric 			return FALSE;
233358090Seric 		}
233458082Seric 	}
233558082Seric 	else if (tTd(4, 80))
233658333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
233758333Seric 			MinBlocksFree, msize, errstring(errno));
233858082Seric 	return TRUE;
233958082Seric }
234058542Seric /*
234158542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
234258542Seric **
234358542Seric **	This looks at an errno value and tells if this is likely to
234458542Seric **	go away if retried later.
234558542Seric **
234658542Seric **	Parameters:
234758542Seric **		err -- the errno code to classify.
234858542Seric **
234958542Seric **	Returns:
235058542Seric **		TRUE if this is probably transient.
235158542Seric **		FALSE otherwise.
235258542Seric */
235358542Seric 
235458542Seric bool
235558542Seric transienterror(err)
235658542Seric 	int err;
235758542Seric {
235858542Seric 	switch (err)
235958542Seric 	{
236058542Seric 	  case EIO:			/* I/O error */
236158542Seric 	  case ENXIO:			/* Device not configured */
236258542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
236358542Seric 	  case ENOMEM:			/* Cannot allocate memory */
236458542Seric 	  case ENODEV:			/* Operation not supported by device */
236558542Seric 	  case ENFILE:			/* Too many open files in system */
236658542Seric 	  case EMFILE:			/* Too many open files */
236758542Seric 	  case ENOSPC:			/* No space left on device */
236858542Seric #ifdef ETIMEDOUT
236958542Seric 	  case ETIMEDOUT:		/* Connection timed out */
237058542Seric #endif
237158542Seric #ifdef ESTALE
237258542Seric 	  case ESTALE:			/* Stale NFS file handle */
237358542Seric #endif
237458542Seric #ifdef ENETDOWN
237558542Seric 	  case ENETDOWN:		/* Network is down */
237658542Seric #endif
237758542Seric #ifdef ENETUNREACH
237858542Seric 	  case ENETUNREACH:		/* Network is unreachable */
237958542Seric #endif
238058542Seric #ifdef ENETRESET
238158542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
238258542Seric #endif
238358542Seric #ifdef ECONNABORTED
238458542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
238558542Seric #endif
238658542Seric #ifdef ECONNRESET
238758542Seric 	  case ECONNRESET:		/* Connection reset by peer */
238858542Seric #endif
238958542Seric #ifdef ENOBUFS
239058542Seric 	  case ENOBUFS:			/* No buffer space available */
239158542Seric #endif
239258542Seric #ifdef ESHUTDOWN
239358542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
239458542Seric #endif
239558542Seric #ifdef ECONNREFUSED
239658542Seric 	  case ECONNREFUSED:		/* Connection refused */
239758542Seric #endif
239858542Seric #ifdef EHOSTDOWN
239958542Seric 	  case EHOSTDOWN:		/* Host is down */
240058542Seric #endif
240158542Seric #ifdef EHOSTUNREACH
240258542Seric 	  case EHOSTUNREACH:		/* No route to host */
240358542Seric #endif
240458542Seric #ifdef EDQUOT
240558542Seric 	  case EDQUOT:			/* Disc quota exceeded */
240658542Seric #endif
240758542Seric #ifdef EPROCLIM
240858542Seric 	  case EPROCLIM:		/* Too many processes */
240958542Seric #endif
241058542Seric #ifdef EUSERS
241158542Seric 	  case EUSERS:			/* Too many users */
241258542Seric #endif
241358542Seric #ifdef EDEADLK
241458542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
241558542Seric #endif
241658542Seric #ifdef EISCONN
241758542Seric 	  case EISCONN:			/* Socket already connected */
241858542Seric #endif
241958542Seric #ifdef EINPROGRESS
242058542Seric 	  case EINPROGRESS:		/* Operation now in progress */
242158542Seric #endif
242258542Seric #ifdef EALREADY
242358542Seric 	  case EALREADY:		/* Operation already in progress */
242458542Seric #endif
242558542Seric #ifdef EADDRINUSE
242658542Seric 	  case EADDRINUSE:		/* Address already in use */
242758542Seric #endif
242858542Seric #ifdef EADDRNOTAVAIL
242958542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
243058542Seric #endif
243166018Seric #ifdef ETXTBSY
243266017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
243366017Seric #endif
243463834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
243558542Seric 	  case ENOSR:			/* Out of streams resources */
243658542Seric #endif
243758542Seric 		return TRUE;
243858542Seric 	}
243958542Seric 
244058542Seric 	/* nope, must be permanent */
244158542Seric 	return FALSE;
244258542Seric }
244358689Seric /*
244464035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
244558689Seric **
244658689Seric **	Parameters:
244758689Seric **		fd -- the file descriptor of the file.
244858689Seric **		filename -- the file name (for error messages).
244964335Seric **		ext -- the filename extension.
245058689Seric **		type -- type of the lock.  Bits can be:
245158689Seric **			LOCK_EX -- exclusive lock.
245258689Seric **			LOCK_NB -- non-blocking.
245358689Seric **
245458689Seric **	Returns:
245558689Seric **		TRUE if the lock was acquired.
245658689Seric **		FALSE otherwise.
245758689Seric */
245858689Seric 
245958689Seric bool
246064335Seric lockfile(fd, filename, ext, type)
246158689Seric 	int fd;
246258689Seric 	char *filename;
246364335Seric 	char *ext;
246458689Seric 	int type;
246558689Seric {
246665830Seric # if !HASFLOCK
246758689Seric 	int action;
246858689Seric 	struct flock lfd;
246964335Seric 
247064335Seric 	if (ext == NULL)
247164335Seric 		ext = "";
247269472Seric 
247363983Seric 	bzero(&lfd, sizeof lfd);
247459447Seric 	if (bitset(LOCK_UN, type))
247559447Seric 		lfd.l_type = F_UNLCK;
247659447Seric 	else if (bitset(LOCK_EX, type))
247758689Seric 		lfd.l_type = F_WRLCK;
247858689Seric 	else
247958689Seric 		lfd.l_type = F_RDLCK;
248058689Seric 
248158689Seric 	if (bitset(LOCK_NB, type))
248258689Seric 		action = F_SETLK;
248358689Seric 	else
248458689Seric 		action = F_SETLKW;
248558689Seric 
248664264Seric 	if (tTd(55, 60))
248764335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
248864335Seric 			filename, ext, action, lfd.l_type);
248964264Seric 
249058689Seric 	if (fcntl(fd, action, &lfd) >= 0)
249164264Seric 	{
249264264Seric 		if (tTd(55, 60))
249364264Seric 			printf("SUCCESS\n");
249458689Seric 		return TRUE;
249564264Seric 	}
249658689Seric 
249764264Seric 	if (tTd(55, 60))
249864264Seric 		printf("(%s) ", errstring(errno));
249964264Seric 
250064136Seric 	/*
250164136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
250264136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
250364136Seric 	**  as type "tmp" (that is, served from swap space), the
250464136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
250564136Seric 	**  Since this is fairly common during testing, we will assume
250664136Seric 	**  that this indicates that the lock is successfully grabbed.
250764136Seric 	*/
250864136Seric 
250964136Seric 	if (errno == EINVAL)
251064264Seric 	{
251164264Seric 		if (tTd(55, 60))
251264264Seric 			printf("SUCCESS\n");
251364136Seric 		return TRUE;
251464264Seric 	}
251564136Seric 
251658689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
251764378Seric 	{
251864378Seric 		int omode = -1;
251964378Seric #  ifdef F_GETFL
252064378Seric 		int oerrno = errno;
252164378Seric 
252264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
252364378Seric 		errno = oerrno;
252464378Seric #  endif
252564378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
252664378Seric 			filename, ext, fd, type, omode, geteuid());
252764378Seric 	}
252858689Seric # else
252964335Seric 	if (ext == NULL)
253064335Seric 		ext = "";
253164335Seric 
253264264Seric 	if (tTd(55, 60))
253364335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
253464264Seric 
253558689Seric 	if (flock(fd, type) >= 0)
253664264Seric 	{
253764264Seric 		if (tTd(55, 60))
253864264Seric 			printf("SUCCESS\n");
253958689Seric 		return TRUE;
254064264Seric 	}
254158689Seric 
254264264Seric 	if (tTd(55, 60))
254364264Seric 		printf("(%s) ", errstring(errno));
254464264Seric 
254558689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
254664378Seric 	{
254764378Seric 		int omode = -1;
254864378Seric #  ifdef F_GETFL
254964378Seric 		int oerrno = errno;
255064378Seric 
255164378Seric 		(void) fcntl(fd, F_GETFL, &omode);
255264378Seric 		errno = oerrno;
255364378Seric #  endif
255464378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
255564378Seric 			filename, ext, fd, type, omode, geteuid());
255664386Seric 	}
255758689Seric # endif
255864264Seric 	if (tTd(55, 60))
255964264Seric 		printf("FAIL\n");
256058689Seric 	return FALSE;
256158689Seric }
256264556Seric /*
256365948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
256465948Seric **
256565948Seric **	Parameters:
256665948Seric **		fd -- the file descriptor to check.
256765948Seric **
256865948Seric **	Returns:
256965948Seric **		TRUE -- if only root can chown the file to an arbitrary
257065948Seric **			user.
257165948Seric **		FALSE -- if an arbitrary user can give away a file.
257265948Seric */
257365948Seric 
257465948Seric bool
257565948Seric chownsafe(fd)
257665948Seric 	int fd;
257765948Seric {
257865950Seric #ifdef __hpux
257965948Seric 	char *s;
258065948Seric 	int tfd;
258165948Seric 	uid_t o_uid, o_euid;
258265948Seric 	gid_t o_gid, o_egid;
258365948Seric 	bool rval;
258465948Seric 	struct stat stbuf;
258565948Seric 
258665948Seric 	o_uid = getuid();
258765948Seric 	o_euid = geteuid();
258865948Seric 	o_gid = getgid();
258965948Seric 	o_egid = getegid();
259065959Seric 	fstat(fd, &stbuf);
259165948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
259265948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
259365948Seric 	s = tmpnam(NULL);
259465948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
259565974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
259665948Seric 	close(tfd);
259765948Seric 	unlink(s);
259865948Seric 	setreuid(o_uid, o_euid);
259965948Seric 	setresgid(o_gid, o_egid, -1);
260065948Seric 	return rval;
260165948Seric #else
260266088Seric # ifdef _POSIX_CHOWN_RESTRICTED
260366088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
260466088Seric 	return FALSE;
260566088Seric #  else
260666088Seric 	return TRUE;
260766088Seric #  endif
260866088Seric # else
260966088Seric #  ifdef _PC_CHOWN_RESTRICTED
261068508Seric 	int rval;
261168508Seric 
261268508Seric 	/*
261368508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
261468508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
261568508Seric 	**  the call.  This heuristic checks for that.
261668508Seric 	*/
261768508Seric 
261868508Seric 	errno = 0;
261968508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
262068508Seric 	if (errno == 0)
262168508Seric 		return rval > 0;
262268508Seric #  endif
262368508Seric #  ifdef BSD
262468508Seric 	return TRUE;
262568499Seric #  else
262665948Seric 	return FALSE;
262765948Seric #  endif
262865948Seric # endif
262965948Seric #endif
263065948Seric }
263165948Seric /*
263268508Seric **  RESETLIMITS -- reset system controlled resource limits
263368508Seric **
263468508Seric **	This is to avoid denial-of-service attacks
263568508Seric **
263668508Seric **	Parameters:
263768508Seric **		none
263868508Seric **
263968508Seric **	Returns:
264068508Seric **		none
264168508Seric */
264268508Seric 
264368508Seric #if HASSETRLIMIT
264468508Seric # include <sys/resource.h>
264568508Seric #endif
264668508Seric 
264768508Seric void
264868508Seric resetlimits()
264968508Seric {
265068508Seric #if HASSETRLIMIT
265168508Seric 	struct rlimit lim;
265268508Seric 
265368508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
265468508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
265568508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
265668508Seric #else
265768508Seric # if HASULIMIT
265868508Seric 	(void) ulimit(2, 0x3fffff);
265968508Seric # endif
266068508Seric #endif
266168508Seric }
266268508Seric /*
266364556Seric **  GETCFNAME -- return the name of the .cf file.
266464556Seric **
266564556Seric **	Some systems (e.g., NeXT) determine this dynamically.
266664556Seric */
266764556Seric 
266864556Seric char *
266964556Seric getcfname()
267064556Seric {
267168842Seric 	int i;
267268842Seric 	static char cbuf[200];
267368842Seric 
267464556Seric 	if (ConfFile != NULL)
267564556Seric 		return ConfFile;
267665214Seric #ifdef NETINFO
267765214Seric 	{
267865214Seric 		extern char *ni_propval();
267965214Seric 		char *cflocation;
268065214Seric 
268168508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
268268508Seric 					"sendmail.cf", '\0');
268365214Seric 		if (cflocation != NULL)
268465214Seric 			return cflocation;
268565214Seric 	}
268665214Seric #endif
268768842Seric 
268869396Seric #ifdef TRY_VERSIONED_CF_NAME
268968842Seric 	/*
269068842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
269168842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
269268842Seric 	**
269368842Seric 	**	I suppose it should really try a search path here --
269468842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
269568842Seric 	**	/usr/lib/sendmail.cf, and so forth.
269668842Seric 	*/
269768842Seric 
269868842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
269968842Seric 	i = strlen(cbuf);
270068842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
270168842Seric 	{
270268842Seric 		char *p;
270368842Seric 		extern char Version[];
270468842Seric 
270568842Seric 		strcpy(&cbuf[i - 2], Version);
270668842Seric 		p = strchr(&cbuf[i - 2], '/');
270768842Seric 		if (p != NULL)
270868842Seric 			*p = '\0';
270968842Seric 		p = &cbuf[strlen(cbuf)];
271068842Seric 		do
271168842Seric 		{
271268842Seric 			int fd;
271368842Seric 
271468842Seric 			strcpy(p, ".cf");
271568842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
271668842Seric 			{
271768842Seric 				close(fd);
271868842Seric 				return cbuf;
271968842Seric 			}
272068842Seric 			*p = '\0';
272168842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
272268842Seric 	}
272369396Seric #endif
272464556Seric 	return _PATH_SENDMAILCF;
272564556Seric }
272664718Seric /*
272764718Seric **  SETVENDOR -- process vendor code from V configuration line
272864718Seric **
272964718Seric **	Parameters:
273064718Seric **		vendor -- string representation of vendor.
273164718Seric **
273264718Seric **	Returns:
273364718Seric **		TRUE -- if ok.
273464718Seric **		FALSE -- if vendor code could not be processed.
273564926Seric **
273664926Seric **	Side Effects:
273764926Seric **		It is reasonable to set mode flags here to tweak
273864926Seric **		processing in other parts of the code if necessary.
273964926Seric **		For example, if you are a vendor that uses $%y to
274064926Seric **		indicate YP lookups, you could enable that here.
274164718Seric */
274264718Seric 
274364718Seric bool
274464718Seric setvendor(vendor)
274564718Seric 	char *vendor;
274664718Seric {
274764926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
274868508Seric 	{
274968508Seric 		VendorCode = VENDOR_BERKELEY;
275064926Seric 		return TRUE;
275168508Seric 	}
275264926Seric 
275364926Seric 	/* add vendor extensions here */
275464926Seric 
275568508Seric #ifdef SUN_EXTENSIONS
275668508Seric 	if (strcasecmp(vendor, "Sun") == 0)
275768508Seric 	{
275868508Seric 		VendorCode = VENDOR_SUN;
275968508Seric 		return TRUE;
276068508Seric 	}
276168508Seric #endif
276268508Seric 
276364926Seric 	return FALSE;
276464718Seric }
276564816Seric /*
276669510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
276769510Seric **
276869510Seric **	Vendor_pre_defaults is called before reading the configuration
276969510Seric **	file; vendor_post_defaults is called immediately after.
277069510Seric **
277169510Seric **	Parameters:
277269510Seric **		e -- the global environment to initialize.
277369510Seric **
277469510Seric **	Returns:
277569510Seric **		none.
277669510Seric */
277769510Seric 
277869510Seric void
277969510Seric vendor_pre_defaults(e)
278069510Seric 	ENVELOPE *e;
278169510Seric {
278269510Seric }
278369510Seric 
278469510Seric 
278569510Seric void
278669510Seric vendor_post_defaults(e)
278769510Seric 	ENVELOPE *e;
278869510Seric {
278969510Seric }
279069510Seric /*
279164816Seric **  STRTOL -- convert string to long integer
279264816Seric **
279364816Seric **	For systems that don't have it in the C library.
279466161Seric **
279566161Seric **	This is taken verbatim from the 4.4-Lite C library.
279664816Seric */
279764816Seric 
279864816Seric #ifdef NEEDSTRTOL
279964816Seric 
280066161Seric #if defined(LIBC_SCCS) && !defined(lint)
280166161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
280266161Seric #endif /* LIBC_SCCS and not lint */
280366161Seric 
280466161Seric #include <limits.h>
280566161Seric 
280666161Seric /*
280766161Seric  * Convert a string to a long integer.
280866161Seric  *
280966161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
281066161Seric  * alphabets and digits are each contiguous.
281166161Seric  */
281266161Seric 
281364816Seric long
281466161Seric strtol(nptr, endptr, base)
281566161Seric 	const char *nptr;
281666161Seric 	char **endptr;
281766161Seric 	register int base;
281864816Seric {
281966161Seric 	register const char *s = nptr;
282066161Seric 	register unsigned long acc;
282166161Seric 	register int c;
282266161Seric 	register unsigned long cutoff;
282366161Seric 	register int neg = 0, any, cutlim;
282464816Seric 
282566161Seric 	/*
282666161Seric 	 * Skip white space and pick up leading +/- sign if any.
282766161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
282866161Seric 	 * assume decimal; if base is already 16, allow 0x.
282966161Seric 	 */
283066161Seric 	do {
283166161Seric 		c = *s++;
283266161Seric 	} while (isspace(c));
283366161Seric 	if (c == '-') {
283466161Seric 		neg = 1;
283566161Seric 		c = *s++;
283666161Seric 	} else if (c == '+')
283766161Seric 		c = *s++;
283866161Seric 	if ((base == 0 || base == 16) &&
283966161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
284066161Seric 		c = s[1];
284166161Seric 		s += 2;
284266161Seric 		base = 16;
284366161Seric 	}
284466161Seric 	if (base == 0)
284566161Seric 		base = c == '0' ? 8 : 10;
284664816Seric 
284766161Seric 	/*
284866161Seric 	 * Compute the cutoff value between legal numbers and illegal
284966161Seric 	 * numbers.  That is the largest legal value, divided by the
285066161Seric 	 * base.  An input number that is greater than this value, if
285166161Seric 	 * followed by a legal input character, is too big.  One that
285266161Seric 	 * is equal to this value may be valid or not; the limit
285366161Seric 	 * between valid and invalid numbers is then based on the last
285466161Seric 	 * digit.  For instance, if the range for longs is
285566161Seric 	 * [-2147483648..2147483647] and the input base is 10,
285666161Seric 	 * cutoff will be set to 214748364 and cutlim to either
285766161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
285866161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
285966161Seric 	 * the number is too big, and we will return a range error.
286066161Seric 	 *
286166161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
286266161Seric 	 * overflow.
286366161Seric 	 */
286466161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
286566161Seric 	cutlim = cutoff % (unsigned long)base;
286666161Seric 	cutoff /= (unsigned long)base;
286766161Seric 	for (acc = 0, any = 0;; c = *s++) {
286866161Seric 		if (isdigit(c))
286966161Seric 			c -= '0';
287066161Seric 		else if (isalpha(c))
287166161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
287266161Seric 		else
287366161Seric 			break;
287466161Seric 		if (c >= base)
287566161Seric 			break;
287666161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
287766161Seric 			any = -1;
287866161Seric 		else {
287966161Seric 			any = 1;
288066161Seric 			acc *= base;
288166161Seric 			acc += c;
288264816Seric 		}
288364816Seric 	}
288466161Seric 	if (any < 0) {
288566161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
288666161Seric 		errno = ERANGE;
288766161Seric 	} else if (neg)
288866161Seric 		acc = -acc;
288966161Seric 	if (endptr != 0)
289066161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
289166161Seric 	return (acc);
289264816Seric }
289364816Seric 
289464816Seric #endif
289564841Seric /*
289668693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
289764841Seric **
289868693Seric **	Some operating systems have wierd problems with the gethostbyXXX
289968693Seric **	routines.  For example, Solaris versions at least through 2.3
290068693Seric **	don't properly deliver a canonical h_name field.  This tries to
290168693Seric **	work around these problems.
290264841Seric */
290364841Seric 
290464841Seric struct hostent *
290568693Seric sm_gethostbyname(name)
290668707Seric 	char *name;
290764841Seric {
290868693Seric #if defined(SOLARIS) && SOLARIS < 204
290968693Seric 	extern int h_errno;
291068693Seric 
291168508Seric # if SOLARIS == 203
291264841Seric 	static struct hostent hp;
291364841Seric 	static char buf[1000];
291464841Seric 	extern struct hostent *_switch_gethostbyname_r();
291564841Seric 
291664841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
291764841Seric # else
291865009Seric 	extern struct hostent *__switch_gethostbyname();
291965009Seric 
292064841Seric 	return __switch_gethostbyname(name);
292164841Seric # endif
292268693Seric #else
292368693Seric 	return gethostbyname(name);
292468693Seric #endif
292564841Seric }
292664841Seric 
292764841Seric struct hostent *
292868693Seric sm_gethostbyaddr(addr, len, type)
292968707Seric 	char *addr;
293064841Seric 	int len;
293164841Seric 	int type;
293264841Seric {
293368693Seric #if defined(SOLARIS) && SOLARIS < 204
293468693Seric 	extern int h_errno;
293568693Seric 
293668508Seric # if SOLARIS == 203
293764841Seric 	static struct hostent hp;
293864841Seric 	static char buf[1000];
293964841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
294064841Seric 
294164841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
294264841Seric # else
294365009Seric 	extern struct hostent *__switch_gethostbyaddr();
294465009Seric 
294564841Seric 	return __switch_gethostbyaddr(addr, len, type);
294664841Seric # endif
294768693Seric #else
294868693Seric 	return gethostbyaddr(addr, len, type);
294968693Seric #endif
295064841Seric }
295168693Seric /*
295268693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
295368693Seric */
295464841Seric 
295568693Seric struct passwd *
295668693Seric sm_getpwnam(user)
295768704Seric 	char *user;
295868693Seric {
295968693Seric 	return getpwnam(user);
296068693Seric }
296168693Seric 
296268693Seric struct passwd *
296368693Seric sm_getpwuid(uid)
296468693Seric 	uid_t uid;
296568693Seric {
296668693Seric 	return getpwuid(uid);
296768693Seric }
296865214Seric /*
296969472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
297069472Seric **
297169472Seric **	Parameters:
297269472Seric **		none.
297369472Seric **
297469472Seric **	Returns:
297569472Seric **		none.
297669472Seric **
297769472Seric **	Side Effects:
297869472Seric **		Loads $=w with the names of all the interfaces.
297969472Seric */
298069472Seric 
298169472Seric #ifdef SIOCGIFCONF
298269472Seric # include <arpa/inet.h>
298369562Seric # include <sys/time.h>
298469472Seric # include <net/if.h>
298569472Seric #endif
298669472Seric 
298769472Seric void
298869472Seric load_if_names()
298969472Seric {
299069472Seric #ifdef SIOCGIFCONF
299169472Seric 	int s;
299269516Seric 	int i;
299369472Seric         struct ifconf ifc;
299469472Seric 	char interfacebuf[1024];
299569472Seric 
299669472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
299769472Seric 	if (s == -1)
299869472Seric 		return;
299969472Seric 
300069472Seric 	/* get the list of known IP address from the kernel */
300169472Seric         ifc.ifc_buf = interfacebuf;
300269516Seric         ifc.ifc_len = sizeof interfacebuf;
300369472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
300469472Seric 	{
300569472Seric 		if (tTd(0, 4))
300669472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
300769516Seric 		close(s);
300869472Seric 		return;
300969472Seric 	}
301069516Seric 	close(s);
301169472Seric 
301269472Seric 	/* scan the list of IP address */
301369472Seric 	if (tTd(0, 4))
301469472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
301569472Seric 			ifc.ifc_len);
301669472Seric 
301769516Seric 	for (i = 0; i < ifc.ifc_len; )
301869516Seric 	{
301969516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
302069516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
302169472Seric 		struct in_addr ia;
302269516Seric 		struct hostent *hp;
302369472Seric 		char ip_addr[256];
302469516Seric 		extern char *inet_ntoa();
302569516Seric 		extern struct hostent *gethostbyaddr();
302669472Seric 
302769516Seric #ifdef BSD4_4_SOCKADDR
302869516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
302969516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
303069516Seric 		else
303169516Seric #endif
303269516Seric 			i += sizeof *ifr;
303369516Seric 
303469472Seric 		if (tTd(0, 20))
303569516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
303669472Seric 
303769472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
303869472Seric 			continue;
303969472Seric 
304069472Seric 		/* extract IP address from the list*/
304169516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
304269472Seric 
304369472Seric 		/* save IP address in text from */
304469472Seric 		(void) sprintf(ip_addr, "[%s]",
304569516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
304669472Seric 		if (!wordinclass(ip_addr, 'w'))
304769472Seric 		{
304869472Seric 			setclass('w', ip_addr);
304969472Seric 			if (tTd(0, 4))
305069472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
305169472Seric 		}
305269472Seric 
305369472Seric 		/* skip "loopback" interface "lo" */
305469472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
305569472Seric 			continue;
305669472Seric 
305769472Seric 		/* lookup name with IP address */
305869472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
305969472Seric 		if (hp == NULL)
306069472Seric 		{
306169472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
306269472Seric 				inet_ntoa(ia));
306369472Seric 			continue;
306469472Seric 		}
306569472Seric 
306669472Seric 		/* save its cname */
306769472Seric 		if (!wordinclass(hp->h_name, 'w'))
306869472Seric 		{
306969472Seric 			setclass('w', hp->h_name);
307069472Seric 			if (tTd(0, 4))
307169472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
307269472Seric 		}
307369472Seric 
307469472Seric 		/* save all it aliases name */
307569472Seric 		while (*hp->h_aliases)
307669472Seric 		{
307769472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
307869472Seric 			{
307969472Seric 				setclass('w', *hp->h_aliases);
308069472Seric 				if (tTd(0, 4))
308169472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
308269472Seric 			}
308369472Seric 			hp->h_aliases++;
308469472Seric 		}
308569472Seric 	}
308669472Seric #endif
308769472Seric }
308869472Seric /*
308965214Seric **  NI_PROPVAL -- netinfo property value lookup routine
309065214Seric **
309165214Seric **	Parameters:
309268508Seric **		keydir -- the Netinfo directory name in which to search
309368508Seric **			for the key.
309468508Seric **		keyprop -- the name of the property in which to find the
309568508Seric **			property we are interested.  Defaults to "name".
309668508Seric **		keyval -- the value for which we are really searching.
309768508Seric **		valprop -- the property name for the value in which we
309868508Seric **			are interested.
309968508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
310068508Seric **			we should return a string separated by this
310168508Seric **			character.
310265214Seric **
310365214Seric **	Returns:
310465214Seric **		NULL -- if:
310565214Seric **			1. the directory is not found
310665214Seric **			2. the property name is not found
310765214Seric **			3. the property contains multiple values
310865214Seric **			4. some error occured
310965214Seric **		else -- the location of the config file.
311065214Seric **
311168508Seric **	Example:
311268508Seric **		To search for an alias value, use:
311368508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
311468508Seric **
311565214Seric **	Notes:
311665214Seric **      	Caller should free the return value of ni_proval
311765214Seric */
311865214Seric 
311965214Seric #ifdef NETINFO
312065214Seric 
312165214Seric # include <netinfo/ni.h>
312265214Seric 
312365214Seric # define LOCAL_NETINFO_DOMAIN    "."
312465214Seric # define PARENT_NETINFO_DOMAIN   ".."
312565214Seric # define MAX_NI_LEVELS           256
312665214Seric 
312765214Seric char *
312868508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
312968508Seric 	char *keydir;
313068508Seric 	char *keyprop;
313168508Seric 	char *keyval;
313268508Seric 	char *valprop;
313368508Seric 	char sepchar;
313465214Seric {
313565820Seric 	char *propval = NULL;
313665214Seric 	int i;
313768508Seric 	int j, alen;
313865214Seric 	void *ni = NULL;
313965214Seric 	void *lastni = NULL;
314065214Seric 	ni_status nis;
314165214Seric 	ni_id nid;
314265214Seric 	ni_namelist ninl;
314368508Seric 	register char *p;
314468508Seric 	char keybuf[1024];
314565214Seric 
314665214Seric 	/*
314768508Seric 	**  Create the full key from the two parts.
314868508Seric 	**
314968508Seric 	**	Note that directory can end with, e.g., "name=" to specify
315068508Seric 	**	an alternate search property.
315168508Seric 	*/
315268508Seric 
315368508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
315468508Seric 	if (keyprop != NULL)
315568508Seric 		i += strlen(keyprop) + 1;
315668508Seric 	if (i > sizeof keybuf)
315768508Seric 		return NULL;
315868508Seric 	strcpy(keybuf, keydir);
315968508Seric 	strcat(keybuf, "/");
316068508Seric 	if (keyprop != NULL)
316168508Seric 	{
316268508Seric 		strcat(keybuf, keyprop);
316368508Seric 		strcat(keybuf, "=");
316468508Seric 	}
316568508Seric 	strcat(keybuf, keyval);
316668508Seric 
316768508Seric 	/*
316865214Seric 	**  If the passed directory and property name are found
316965214Seric 	**  in one of netinfo domains we need to search (starting
317065214Seric 	**  from the local domain moving all the way back to the
317165214Seric 	**  root domain) set propval to the property's value
317265214Seric 	**  and return it.
317365214Seric 	*/
317465214Seric 
317565214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
317665214Seric 	{
317765214Seric 		if (i == 0)
317865214Seric 		{
317965214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
318065214Seric 		}
318165214Seric 		else
318265214Seric 		{
318365214Seric 			if (lastni != NULL)
318465214Seric 				ni_free(lastni);
318565214Seric 			lastni = ni;
318665214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
318765214Seric 		}
318865214Seric 
318965214Seric 		/*
319065214Seric 		**  Don't bother if we didn't get a handle on a
319165214Seric 		**  proper domain.  This is not necessarily an error.
319265214Seric 		**  We would get a positive ni_status if, for instance
319365214Seric 		**  we never found the directory or property and tried
319465214Seric 		**  to open the parent of the root domain!
319565214Seric 		*/
319665214Seric 
319765214Seric 		if (nis != 0)
319865214Seric 			break;
319965214Seric 
320065214Seric 		/*
320165214Seric 		**  Find the path to the server information.
320265214Seric 		*/
320365214Seric 
320468508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
320565214Seric 			continue;
320665214Seric 
320765214Seric 		/*
320868508Seric 		**  Find associated value information.
320965214Seric 		*/
321065214Seric 
321168508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
321265214Seric 			continue;
321365214Seric 
321465214Seric 		/*
321568508Seric 		**  See if we have an acceptable number of values.
321665214Seric 		*/
321765214Seric 
321868508Seric 		if (ninl.ni_namelist_len <= 0)
321968508Seric 			continue;
322068508Seric 
322168508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
322265214Seric 		{
322368508Seric 			ni_namelist_free(&ninl);
322468508Seric 			continue;
322565214Seric 		}
322668508Seric 
322769472Seric 		/*
322868508Seric 		**  Calculate number of bytes needed and build result
322968508Seric 		*/
323068508Seric 
323168508Seric 		alen = 1;
323268508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
323368508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
323468508Seric 		propval = p = xalloc(alen);
323568508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
323668508Seric 		{
323768508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
323868508Seric 			p += strlen(p);
323968508Seric 			*p++ = sepchar;
324069472Seric 		}
324168508Seric 		*--p = '\0';
324268508Seric 
324368508Seric 		ni_namelist_free(&ninl);
324465214Seric 	}
324565214Seric 
324665214Seric 	/*
324765214Seric 	**  Clean up.
324865214Seric 	*/
324965214Seric 
325065214Seric 	if (ni != NULL)
325165214Seric 		ni_free(ni);
325265214Seric 	if (lastni != NULL && ni != lastni)
325365214Seric 		ni_free(lastni);
325465214Seric 
325565214Seric 	return propval;
325665214Seric }
325765214Seric 
325865214Seric #endif /* NETINFO */
325965504Seric /*
326065504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
326165504Seric **
326265504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
326365504Seric **	syslog succeeds during interrupt handlers.
326465504Seric */
326565504Seric 
326665504Seric #ifdef __hpux
326765504Seric 
326865504Seric # define MAXSYSLOGTRIES	100
326965504Seric # undef syslog
327065504Seric 
327165504Seric # ifdef __STDC__
327265504Seric hard_syslog(int pri, char *msg, ...)
327365504Seric # else
327465504Seric hard_syslog(pri, msg, va_alist)
327565504Seric 	int pri;
327665504Seric 	char *msg;
327765504Seric 	va_dcl
327865504Seric # endif
327965504Seric {
328065504Seric 	int i;
328165504Seric 	char buf[SYSLOG_BUFSIZE * 2];
328265504Seric 	VA_LOCAL_DECL;
328365504Seric 
328465504Seric 	VA_START(msg);
328565504Seric 	vsprintf(buf, msg, ap);
328665504Seric 	VA_END;
328765504Seric 
328865504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
328965504Seric 		continue;
329065504Seric }
329165504Seric 
329265504Seric #endif
329368696Seric /*
329468696Seric **  LOCAL_HOSTNAME_LENGTH
329568696Seric **
329668696Seric **	This is required to get sendmail to compile against BIND 4.9.x
329768696Seric **	on Ultrix.
329868696Seric */
329968696Seric 
330068696Seric #if defined(ultrix) && NAMED_BIND
330168696Seric 
330268696Seric # include <resolv.h>
330368696Seric # if __RES >= 19931104
330468696Seric 
330568696Seric int
330668696Seric local_hostname_length(hostname)
330768696Seric 	char *hostname;
330868696Seric {
330968696Seric 	int len_host, len_domain;
331068696Seric 
331168696Seric 	if (!*_res.defdname)
331268696Seric 		res_init();
331368696Seric 	len_host = strlen(hostname);
331468696Seric 	len_domain = strlen(_res.defdname);
331568696Seric 	if (len_host > len_domain &&
331668696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
331768696Seric 	    hostname[len_host - len_domain - 1] == '.')
331868696Seric 		return len_host - len_domain - 1;
331968696Seric 	else
332068696Seric 		return 0;
332168696Seric }
332268696Seric 
332368696Seric # endif
332468696Seric #endif
332568741Seric /*
332668741Seric **  Compile-Time options
332768741Seric */
332868741Seric 
332968741Seric char	*CompileOptions[] =
333068741Seric {
333168741Seric #if HESIOD
333268741Seric 	"HESIOD",
333368741Seric #endif
333468741Seric #if LOG
333568741Seric 	"LOG",
333668741Seric #endif
333768741Seric #if MATCHGECOS
333868741Seric 	"MATCHGECOS",
333968741Seric #endif
334068741Seric #if NAMED_BIND
334168741Seric 	"NAMED_BIND",
334268741Seric #endif
334368741Seric #if NDBM
334468741Seric 	"NDBM",
334568741Seric #endif
334668741Seric #if NETINET
334768741Seric 	"NETINET",
334868741Seric #endif
334968741Seric #if NETINFO
335068741Seric 	"NETINFO",
335168741Seric #endif
335268741Seric #if NETISO
335368741Seric 	"NETISO",
335468741Seric #endif
335568741Seric #if NETNS
335668741Seric 	"NETNS",
335768741Seric #endif
335868741Seric #if NETUNIX
335968741Seric 	"NETUNIX",
336068741Seric #endif
336168741Seric #if NETX25
336268741Seric 	"NETX25",
336368741Seric #endif
336468741Seric #if NEWDB
336568741Seric 	"NEWDB",
336668741Seric #endif
336768741Seric #if NIS
336868741Seric 	"NIS",
336968741Seric #endif
337069518Seric #if NISPLUS
337169518Seric 	"NISPLUS",
337269518Seric #endif
337368741Seric #if SCANF
337468741Seric 	"SCANF",
337568741Seric #endif
337668741Seric #if SUID_ROOT_FILES_OK
337768741Seric 	"SUID_ROOT_FILES_OK",
337868741Seric #endif
337968741Seric #if USERDB
338068741Seric 	"USERDB",
338168741Seric #endif
338268741Seric #if XDEBUG
338368741Seric 	"XDEBUG",
338468741Seric #endif
338568741Seric #if XLA
338668741Seric 	"XLA",
338768741Seric #endif
338868741Seric 	NULL
338968741Seric };
3390