xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69516)
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*69516Seric static char sccsid[] = "@(#)conf.c	8.168 (Berkeley) 05/17/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 
127868869Seric struct pst_dynamic;
127968869Seric 
128069462Seric # include <sys/param.h>
128169462Seric # include <sys/pstat.h>
128268508Seric 
128368508Seric int
128451773Seric getla()
128551773Seric {
128668508Seric 	struct pst_dynamic pstd;
128768508Seric 
128868508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
128968869Seric 			     (size_t) 1, 0) == -1)
129068508Seric 		return 0;
129168508Seric 
129268508Seric         if (tTd(3, 1))
129368508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
129468508Seric 
129568508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
129668508Seric }
129768508Seric 
129869462Seric #endif /* LA_TYPE == LA_HPUX */
129968508Seric 
130069462Seric #if LA_TYPE == LA_SUBR
130169462Seric 
130268508Seric int
130368508Seric getla()
130468508Seric {
130551920Seric 	double avenrun[3];
130651920Seric 
130751920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
130857736Seric 	{
130957736Seric 		if (tTd(3, 1))
131057736Seric 			perror("getla: getloadavg failed:");
131151920Seric 		return (-1);
131257736Seric 	}
131357736Seric 	if (tTd(3, 1))
131457736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
131551920Seric 	return ((int) (avenrun[0] + 0.5));
131651773Seric }
131751773Seric 
131869462Seric #endif /* LA_TYPE == LA_SUBR */
131969462Seric 
132064295Seric #if LA_TYPE == LA_MACH
132151773Seric 
132264295Seric /*
132365173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
132464295Seric */
132564295Seric 
132665173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
132765173Seric # include <mach/mach.h>
132865173Seric #else
132965173Seric # include <mach.h>
133065173Seric #endif
133164295Seric 
133251773Seric getla()
133351773Seric {
133464295Seric 	processor_set_t default_set;
133564295Seric 	kern_return_t error;
133664295Seric 	unsigned int info_count;
133764295Seric 	struct processor_set_basic_info info;
133864295Seric 	host_t host;
133964295Seric 
134064295Seric 	error = processor_set_default(host_self(), &default_set);
134164295Seric 	if (error != KERN_SUCCESS)
134264295Seric 		return -1;
134364295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
134464295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
134564295Seric 			       &host, (processor_set_info_t)&info,
134664295Seric 			       &info_count) != KERN_SUCCESS)
134764295Seric 	{
134864295Seric 		return -1;
134964295Seric 	}
135064295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
135164295Seric }
135264295Seric 
135369462Seric #endif /* LA_TYPE == LA_MACH */
135464295Seric 
135566313Seric #if LA_TYPE == LA_PROCSTR
135664295Seric 
135766301Seric /*
135866301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
135966301Seric **  in a format like "0.15 0.12 0.06".
136066301Seric **
136166301Seric **	Initially intended for Linux.  This has been in the kernel
136266301Seric **	since at least 0.99.15.
136366301Seric */
136466301Seric 
136566301Seric # ifndef _PATH_LOADAVG
136666301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
136766301Seric # endif
136866301Seric 
136966301Seric int
137064295Seric getla()
137164295Seric {
137266319Seric 	double avenrun;
137366301Seric 	register int result;
137466319Seric 	FILE *fp;
137566301Seric 
137666319Seric 	fp = fopen(_PATH_LOADAVG, "r");
137769472Seric 	if (fp == NULL)
137866301Seric 	{
137966319Seric 		if (tTd(3, 1))
138066319Seric 			printf("getla: fopen(%s): %s\n",
138166319Seric 				_PATH_LOADAVG, errstring(errno));
138266319Seric 		return -1;
138366301Seric 	}
138466301Seric 	result = fscanf(fp, "%lf", &avenrun);
138566319Seric 	fclose(fp);
138666301Seric 	if (result != 1)
138766301Seric 	{
138866310Seric 		if (tTd(3, 1))
138966301Seric 			printf("getla: fscanf() = %d: %s\n",
139066301Seric 				result, errstring(errno));
139166301Seric 		return -1;
139266301Seric 	}
139366301Seric 
139457736Seric 	if (tTd(3, 1))
139566301Seric 		printf("getla(): %.2f\n", avenrun);
139666301Seric 
139766301Seric 	return ((int) (avenrun + 0.5));
139866301Seric }
139966301Seric 
140069462Seric #endif /* LA_TYPE == LA_PROCSTR */
140166301Seric 
140269462Seric #if LA_TYPE == LA_ZERO
140369462Seric 
140466301Seric getla()
140566301Seric {
140666301Seric 	if (tTd(3, 1))
140757736Seric 		printf("getla: ZERO\n");
140851920Seric 	return (0);
140951773Seric }
141051773Seric 
141169462Seric #endif /* LA_TYPE == LA_ZERO */
141266045Seric 
141366045Seric 
141466045Seric /*
141566045Seric  * Copyright 1989 Massachusetts Institute of Technology
141666045Seric  *
141766045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
141866045Seric  * documentation for any purpose is hereby granted without fee, provided that
141966045Seric  * the above copyright notice appear in all copies and that both that
142066045Seric  * copyright notice and this permission notice appear in supporting
142166045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
142266045Seric  * publicity pertaining to distribution of the software without specific,
142366045Seric  * written prior permission.  M.I.T. makes no representations about the
142466045Seric  * suitability of this software for any purpose.  It is provided "as is"
142566045Seric  * without express or implied warranty.
142666045Seric  *
142766045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
142866045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
142966045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143066045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
143166045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
143266045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
143366045Seric  *
143466045Seric  * Authors:  Many and varied...
143566045Seric  */
143666045Seric 
143766045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
143866045Seric #ifndef lint
143966045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
144066045Seric #endif /* !lint */
144166045Seric 
144266179Seric #ifdef apollo
144366045Seric # undef volatile
144466045Seric #    include <apollo/base.h>
144566045Seric 
144666045Seric /* ARGSUSED */
144766045Seric int getloadavg( call_data )
144866045Seric      caddr_t	call_data;	/* pointer to (double) return value */
144966045Seric {
145066045Seric      double *avenrun = (double *) call_data;
145166045Seric      int i;
145266045Seric      status_$t      st;
145366045Seric      long loadav[3];
145466045Seric      proc1_$get_loadav(loadav, &st);
145566045Seric      *avenrun = loadav[0] / (double) (1 << 16);
145666045Seric      return(0);
145766045Seric }
145866045Seric #   endif /* apollo */
145924943Seric /*
146024943Seric **  SHOULDQUEUE -- should this message be queued or sent?
146124943Seric **
146224943Seric **	Compares the message cost to the load average to decide.
146324943Seric **
146424943Seric **	Parameters:
146524943Seric **		pri -- the priority of the message in question.
146657438Seric **		ctime -- the message creation time.
146724943Seric **
146824943Seric **	Returns:
146924943Seric **		TRUE -- if this message should be queued up for the
147024943Seric **			time being.
147124943Seric **		FALSE -- if the load is low enough to send this message.
147224943Seric **
147324943Seric **	Side Effects:
147424943Seric **		none.
147524943Seric */
147624943Seric 
147724943Seric bool
147857438Seric shouldqueue(pri, ctime)
147924943Seric 	long pri;
148057438Seric 	time_t ctime;
148124943Seric {
148268508Seric 	bool rval;
148368508Seric 
148468508Seric 	if (tTd(3, 30))
148568508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
148651920Seric 	if (CurrentLA < QueueLA)
148768508Seric 	{
148868508Seric 		if (tTd(3, 30))
148968508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
149024943Seric 		return (FALSE);
149168508Seric 	}
149258132Seric 	if (CurrentLA >= RefuseLA)
149368508Seric 	{
149468508Seric 		if (tTd(3, 30))
149568508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
149658132Seric 		return (TRUE);
149768508Seric 	}
149868508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
149968508Seric 	if (tTd(3, 30))
150068508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
150168508Seric 	return rval;
150224943Seric }
150324943Seric /*
150453037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
150553037Seric **
150653037Seric **	Parameters:
150753037Seric **		none.
150853037Seric **
150953037Seric **	Returns:
151053037Seric **		TRUE if incoming SMTP connections should be refused
151153037Seric **			(for now).
151253037Seric **		FALSE if we should accept new work.
151353037Seric **
151453037Seric **	Side Effects:
151553037Seric **		none.
151653037Seric */
151753037Seric 
151853037Seric bool
151953037Seric refuseconnections()
152053037Seric {
152168508Seric 	extern bool enoughspace();
152268508Seric 
152359156Seric #ifdef XLA
152459156Seric 	if (!xla_smtp_ok())
152559156Seric 		return TRUE;
152659156Seric #endif
152759156Seric 
152853037Seric 	/* this is probably too simplistic */
152968508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
153053037Seric }
153153037Seric /*
153224943Seric **  SETPROCTITLE -- set process title for ps
153324943Seric **
153424943Seric **	Parameters:
153558674Seric **		fmt -- a printf style format string.
153658674Seric **		a, b, c -- possible parameters to fmt.
153724943Seric **
153824943Seric **	Returns:
153924943Seric **		none.
154024943Seric **
154124943Seric **	Side Effects:
154224943Seric **		Clobbers argv of our main procedure so ps(1) will
154324943Seric **		display the title.
154424943Seric */
154524943Seric 
154668508Seric #define SPT_NONE	0	/* don't use it at all */
154768508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
154868508Seric #define SPT_BUILTIN	2	/* use libc builtin */
154968508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
155068508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
155168508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
155268508Seric 
155368508Seric #ifndef SPT_TYPE
155468508Seric # define SPT_TYPE	SPT_REUSEARGV
155568508Seric #endif
155668508Seric 
155768508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
155868508Seric 
155968508Seric # if SPT_TYPE == SPT_PSTAT
156058689Seric #  include <sys/pstat.h>
156158689Seric # endif
156268508Seric # if SPT_TYPE == SPT_PSSTRINGS
156359732Seric #  include <machine/vmparam.h>
156459732Seric #  include <sys/exec.h>
156568508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
156668508Seric #   undef SPT_TYPE
156768508Seric #   define SPT_TYPE	SPT_REUSEARGV
156868734Seric #  else
156968734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
157068734Seric #    define NKPDE 63
157168734Seric typedef unsigned int	*pt_entry_t;
157268734Seric #   endif
157364247Seric #  endif
157459732Seric # endif
157568508Seric 
157668508Seric # if SPT_TYPE == SPT_PSSTRINGS
157768508Seric #  define SETPROC_STATIC	static
157868508Seric # else
157959732Seric #  define SETPROC_STATIC
158059732Seric # endif
158167626Seric 
158268508Seric # ifndef SPT_PADCHAR
158368508Seric #  define SPT_PADCHAR	' '
158468508Seric # endif
158558689Seric 
158668508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
158764843Seric 
158868508Seric #if SPT_TYPE != SPT_BUILTIN
158968508Seric 
159024943Seric /*VARARGS1*/
159168508Seric void
159268508Seric # ifdef __STDC__
159357642Seric setproctitle(char *fmt, ...)
159468508Seric # else
159557642Seric setproctitle(fmt, va_alist)
159624943Seric 	char *fmt;
159757642Seric 	va_dcl
159868508Seric # endif
159924943Seric {
160068508Seric # if SPT_TYPE != SPT_NONE
160124943Seric 	register char *p;
160225049Seric 	register int i;
160359732Seric 	SETPROC_STATIC char buf[MAXLINE];
160456852Seric 	VA_LOCAL_DECL
160568508Seric #  if SPT_TYPE == SPT_PSTAT
160658689Seric 	union pstun pst;
160758689Seric #  endif
160824943Seric 	extern char **Argv;
160924943Seric 	extern char *LastArgv;
161024943Seric 
161158674Seric 	p = buf;
161224943Seric 
161358674Seric 	/* print sendmail: heading for grep */
161458674Seric 	(void) strcpy(p, "sendmail: ");
161558674Seric 	p += strlen(p);
161624943Seric 
161758674Seric 	/* print the argument string */
161858674Seric 	VA_START(fmt);
161958674Seric 	(void) vsprintf(p, fmt, ap);
162056852Seric 	VA_END;
162154996Seric 
162258674Seric 	i = strlen(buf);
162358689Seric 
162468508Seric #  if SPT_TYPE == SPT_PSTAT
162558689Seric 	pst.pst_command = buf;
162658689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
162758689Seric #  else
162868508Seric #   if SPT_TYPE == SPT_PSSTRINGS
162959732Seric 	PS_STRINGS->ps_nargvstr = 1;
163059732Seric 	PS_STRINGS->ps_argvstr = buf;
163159732Seric #   else
163254996Seric 	if (i > LastArgv - Argv[0] - 2)
163325049Seric 	{
163454996Seric 		i = LastArgv - Argv[0] - 2;
163558674Seric 		buf[i] = '\0';
163625049Seric 	}
163758674Seric 	(void) strcpy(Argv[0], buf);
163854997Seric 	p = &Argv[0][i];
163924943Seric 	while (p < LastArgv)
164068508Seric 		*p++ = SPT_PADCHAR;
164168508Seric 	Argv[1] = NULL;
164268508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
164368508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
164468508Seric # endif /* SPT_TYPE != SPT_NONE */
164524943Seric }
164665982Seric 
164768508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
164825698Seric /*
164925698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
165025698Seric **
165125698Seric **	Parameters:
165268508Seric **		sig -- the signal that got us here (unused).
165325698Seric **
165425698Seric **	Returns:
165525698Seric **		none.
165625698Seric **
165725698Seric **	Side Effects:
165825698Seric **		Picks up extant zombies.
165925698Seric */
166025698Seric 
166146928Sbostic void
166268508Seric reapchild(sig)
166368508Seric 	int sig;
166425698Seric {
166564738Seric 	int olderrno = errno;
166663966Seric # ifdef HASWAITPID
166760219Seric 	auto int status;
166860560Seric 	int count;
166960560Seric 	int pid;
167060219Seric 
167160560Seric 	count = 0;
167260560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
167360560Seric 	{
167460560Seric 		if (count++ > 1000)
167560560Seric 		{
167666748Seric #ifdef LOG
167760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
167860560Seric 				pid, status);
167966748Seric #endif
168060560Seric 			break;
168160560Seric 		}
168260560Seric 	}
168360219Seric # else
168425698Seric # ifdef WNOHANG
168525698Seric 	union wait status;
168625698Seric 
168763753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
168825698Seric 		continue;
168956795Seric # else /* WNOHANG */
169025698Seric 	auto int status;
169125698Seric 
169260219Seric 	while (wait(&status) > 0)
169325698Seric 		continue;
169456795Seric # endif /* WNOHANG */
169560219Seric # endif
169664035Seric # ifdef SYS5SIGNALS
169764035Seric 	(void) setsignal(SIGCHLD, reapchild);
169858061Seric # endif
169964738Seric 	errno = olderrno;
170025698Seric }
170155418Seric /*
170255418Seric **  UNSETENV -- remove a variable from the environment
170355418Seric **
170455418Seric **	Not needed on newer systems.
170555418Seric **
170655418Seric **	Parameters:
170755418Seric **		name -- the string name of the environment variable to be
170855418Seric **			deleted from the current environment.
170955418Seric **
171055418Seric **	Returns:
171155418Seric **		none.
171255418Seric **
171355418Seric **	Globals:
171455418Seric **		environ -- a pointer to the current environment.
171555418Seric **
171655418Seric **	Side Effects:
171755418Seric **		Modifies environ.
171855418Seric */
171955418Seric 
172063962Seric #ifndef HASUNSETENV
172155418Seric 
172255418Seric void
172355418Seric unsetenv(name)
172455418Seric 	char *name;
172555418Seric {
172655418Seric 	extern char **environ;
172755418Seric 	register char **pp;
172855418Seric 	int len = strlen(name);
172955418Seric 
173055418Seric 	for (pp = environ; *pp != NULL; pp++)
173155418Seric 	{
173255418Seric 		if (strncmp(name, *pp, len) == 0 &&
173355418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
173455418Seric 			break;
173555418Seric 	}
173655418Seric 
173755418Seric 	for (; *pp != NULL; pp++)
173855418Seric 		*pp = pp[1];
173955418Seric }
174055418Seric 
174163962Seric #endif
174256215Seric /*
174356215Seric **  GETDTABLESIZE -- return number of file descriptors
174456215Seric **
174556215Seric **	Only on non-BSD systems
174656215Seric **
174756215Seric **	Parameters:
174856215Seric **		none
174956215Seric **
175056215Seric **	Returns:
175156215Seric **		size of file descriptor table
175256215Seric **
175356215Seric **	Side Effects:
175456215Seric **		none
175556215Seric */
175656215Seric 
175763787Seric #ifdef SOLARIS
175863787Seric # include <sys/resource.h>
175963787Seric #endif
176056215Seric 
176156215Seric int
176263787Seric getdtsize()
176356215Seric {
176463787Seric #ifdef RLIMIT_NOFILE
176563787Seric 	struct rlimit rl;
176663787Seric 
176763787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
176863787Seric 		return rl.rlim_cur;
176963787Seric #endif
177063787Seric 
177164031Seric # ifdef HASGETDTABLESIZE
177264031Seric 	return getdtablesize();
177364031Seric # else
177464031Seric #  ifdef _SC_OPEN_MAX
177558689Seric 	return sysconf(_SC_OPEN_MAX);
177663787Seric #  else
177756215Seric 	return NOFILE;
177863787Seric #  endif
177958689Seric # endif
178056215Seric }
178157631Seric /*
178257631Seric **  UNAME -- get the UUCP name of this system.
178357631Seric */
178457631Seric 
178557943Seric #ifndef HASUNAME
178657631Seric 
178757631Seric int
178857631Seric uname(name)
178957631Seric 	struct utsname *name;
179057631Seric {
179157631Seric 	FILE *file;
179257631Seric 	char *n;
179357631Seric 
179457631Seric 	name->nodename[0] = '\0';
179557631Seric 
179657661Seric 	/* try /etc/whoami -- one line with the node name */
179757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
179857631Seric 	{
179957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
180057631Seric 		(void) fclose(file);
180157661Seric 		n = strchr(name->nodename, '\n');
180257631Seric 		if (n != NULL)
180357631Seric 			*n = '\0';
180457631Seric 		if (name->nodename[0] != '\0')
180557631Seric 			return (0);
180657631Seric 	}
180757631Seric 
180857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
180957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
181057631Seric 	{
181157631Seric 		char buf[MAXLINE];
181257631Seric 
181357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
181457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
181557631Seric 					NODE_LENGTH, name->nodename) > 0)
181657631Seric 				break;
181757631Seric 		(void) fclose(file);
181857631Seric 		if (name->nodename[0] != '\0')
181957631Seric 			return (0);
182057631Seric 	}
182157631Seric 
182257631Seric #ifdef TRUST_POPEN
182357631Seric 	/*
182457631Seric 	**  Popen is known to have security holes.
182557631Seric 	*/
182657631Seric 
182757661Seric 	/* try uuname -l to return local name */
182857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
182957631Seric 	{
183057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
183157631Seric 		(void) pclose(file);
183257661Seric 		n = strchr(name, '\n');
183357631Seric 		if (n != NULL)
183457631Seric 			*n = '\0';
183557661Seric 		if (name->nodename[0] != '\0')
183657631Seric 			return (0);
183757631Seric 	}
183857631Seric #endif
183969472Seric 
184057631Seric 	return (-1);
184157631Seric }
184257943Seric #endif /* HASUNAME */
184358068Seric /*
184458068Seric **  INITGROUPS -- initialize groups
184558068Seric **
184658068Seric **	Stub implementation for System V style systems
184758068Seric */
184858068Seric 
184958068Seric #ifndef HASINITGROUPS
185058068Seric 
185158068Seric initgroups(name, basegid)
185258068Seric 	char *name;
185358068Seric 	int basegid;
185458068Seric {
185558068Seric 	return 0;
185658068Seric }
185758068Seric 
185858068Seric #endif
185958082Seric /*
186059289Seric **  SETSID -- set session id (for non-POSIX systems)
186159289Seric */
186259289Seric 
186359289Seric #ifndef HASSETSID
186459289Seric 
186563753Seric pid_t
186663753Seric setsid __P ((void))
186759289Seric {
186864155Seric #ifdef TIOCNOTTY
186964155Seric 	int fd;
187064155Seric 
187166743Seric 	fd = open("/dev/tty", O_RDWR, 0);
187264155Seric 	if (fd >= 0)
187364155Seric 	{
187464155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
187564155Seric 		(void) close(fd);
187664155Seric 	}
187764155Seric #endif /* TIOCNOTTY */
187864705Seric # ifdef SYS5SETPGRP
187963753Seric 	return setpgrp();
188063753Seric # else
188164155Seric 	return setpgid(0, getpid());
188259289Seric # endif
188359289Seric }
188459289Seric 
188559289Seric #endif
188659289Seric /*
188766748Seric **  FSYNC -- dummy fsync
188866748Seric */
188966748Seric 
189066748Seric #ifdef NEEDFSYNC
189166748Seric 
189266748Seric fsync(fd)
189366748Seric 	int fd;
189466748Seric {
189566748Seric # ifdef O_SYNC
189666748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
189766748Seric # else
189866748Seric 	/* nothing we can do */
189966748Seric 	return 0;
190066748Seric # endif
190166748Seric }
190266748Seric 
190366748Seric #endif
190466748Seric /*
190564718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
190664718Seric **
190764718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
190868508Seric **	instead of a long.  This patches things.  Only needed on versions
190968508Seric **	prior to 5.4.3.
191064718Seric */
191164718Seric 
191268508Seric #ifdef DGUX_5_4_2
191364718Seric 
191464718Seric #undef inet_addr
191564718Seric 
191664718Seric long
191764718Seric dgux_inet_addr(host)
191864718Seric 	char *host;
191964718Seric {
192064718Seric 	struct in_addr haddr;
192164718Seric 
192264718Seric 	haddr = inet_addr(host);
192364718Seric 	return haddr.s_addr;
192464718Seric }
192564718Seric 
192664718Seric #endif
192764718Seric /*
192863969Seric **  GETOPT -- for old systems or systems with bogus implementations
192963969Seric */
193063969Seric 
193163969Seric #ifdef NEEDGETOPT
193263969Seric 
193363969Seric /*
193463969Seric  * Copyright (c) 1985 Regents of the University of California.
193563969Seric  * All rights reserved.  The Berkeley software License Agreement
193663969Seric  * specifies the terms and conditions for redistribution.
193763969Seric  */
193863969Seric 
193963969Seric 
194063969Seric /*
194163969Seric ** this version hacked to add `atend' flag to allow state machine
194263969Seric ** to reset if invoked by the program to scan args for a 2nd time
194363969Seric */
194463969Seric 
194563969Seric #if defined(LIBC_SCCS) && !defined(lint)
194663969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
194764072Seric #endif /* LIBC_SCCS and not lint */
194863969Seric 
194963969Seric #include <stdio.h>
195063969Seric 
195163969Seric /*
195263969Seric  * get option letter from argument vector
195363969Seric  */
195465352Seric #ifdef _CONVEX_SOURCE
195568508Seric extern int	optind, opterr, optopt;
195668508Seric extern char	*optarg;
195765352Seric #else
195865352Seric int	opterr = 1;		/* if error message should be printed */
195965352Seric int	optind = 1;		/* index into parent argv vector */
196068470Seric int	optopt = 0;		/* character checked for validity */
196168470Seric char	*optarg = NULL;		/* argument associated with option */
196268508Seric #endif
196363969Seric 
196463969Seric #define BADCH	(int)'?'
196563969Seric #define EMSG	""
196663969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
196763969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
196863969Seric 
196963969Seric getopt(nargc,nargv,ostr)
197065160Seric 	int		nargc;
197165160Seric 	char *const	*nargv;
197265160Seric 	const char	*ostr;
197363969Seric {
197463969Seric 	static char	*place = EMSG;	/* option letter processing */
197563969Seric 	static char	atend = 0;
197663969Seric 	register char	*oli;		/* option letter list index */
197763969Seric 
197863969Seric 	if (atend) {
197963969Seric 		atend = 0;
198063969Seric 		place = EMSG;
198163969Seric 	}
198263969Seric 	if(!*place) {			/* update scanning pointer */
198364136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
198463969Seric 			atend++;
198563969Seric 			return(EOF);
198663969Seric 		}
198763969Seric 		if (*place == '-') {	/* found "--" */
198863969Seric 			++optind;
198963969Seric 			atend++;
199063969Seric 			return(EOF);
199163969Seric 		}
199263969Seric 	}				/* option letter okay? */
199364125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
199464136Seric 		if (!*place) ++optind;
199563969Seric 		tell(": illegal option -- ");
199663969Seric 	}
199763969Seric 	if (*++oli != ':') {		/* don't need argument */
199863969Seric 		optarg = NULL;
199963969Seric 		if (!*place) ++optind;
200063969Seric 	}
200163969Seric 	else {				/* need an argument */
200263969Seric 		if (*place) optarg = place;	/* no white space */
200363969Seric 		else if (nargc <= ++optind) {	/* no arg */
200463969Seric 			place = EMSG;
200563969Seric 			tell(": option requires an argument -- ");
200663969Seric 		}
200763969Seric 	 	else optarg = nargv[optind];	/* white space */
200863969Seric 		place = EMSG;
200963969Seric 		++optind;
201063969Seric 	}
201163969Seric 	return(optopt);			/* dump back option letter */
201263969Seric }
201363969Seric 
201463969Seric #endif
201563969Seric /*
201663969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
201763969Seric */
201863969Seric 
201963969Seric #ifdef NEEDVPRINTF
202063969Seric 
202163969Seric #define MAXARG	16
202263969Seric 
202363969Seric vfprintf(fp, fmt, ap)
202463969Seric 	FILE *	fp;
202563969Seric 	char *	fmt;
202663969Seric 	char **	ap;
202763969Seric {
202863969Seric 	char *	bp[MAXARG];
202963969Seric 	int	i = 0;
203063969Seric 
203163969Seric 	while (*ap && i < MAXARG)
203263969Seric 		bp[i++] = *ap++;
203363969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
203463969Seric 			 bp[4], bp[5], bp[6], bp[7],
203563969Seric 			 bp[8], bp[9], bp[10], bp[11],
203663969Seric 			 bp[12], bp[13], bp[14], bp[15]);
203763969Seric }
203863969Seric 
203963969Seric vsprintf(s, fmt, ap)
204063969Seric 	char *	s;
204163969Seric 	char *	fmt;
204263969Seric 	char **	ap;
204363969Seric {
204463969Seric 	char *	bp[MAXARG];
204563969Seric 	int	i = 0;
204663969Seric 
204763969Seric 	while (*ap && i < MAXARG)
204863969Seric 		bp[i++] = *ap++;
204963969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
205063969Seric 			bp[4], bp[5], bp[6], bp[7],
205163969Seric 			bp[8], bp[9], bp[10], bp[11],
205263969Seric 			bp[12], bp[13], bp[14], bp[15]);
205363969Seric }
205463969Seric 
205563969Seric #endif
205663969Seric /*
205765220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
205865208Seric **
205965211Seric **	Parameters:
206065211Seric **		shell -- the user's shell from /etc/passwd
206165211Seric **
206265211Seric **	Returns:
206365211Seric **		TRUE -- if it is ok to use this for unrestricted access.
206465211Seric **		FALSE -- if the shell is restricted.
206565208Seric */
206665208Seric 
206765587Seric #if !HASGETUSERSHELL
206865587Seric 
206965587Seric # ifndef _PATH_SHELLS
207065587Seric #  define _PATH_SHELLS	"/etc/shells"
207165587Seric # endif
207265587Seric 
207365587Seric char	*DefaultUserShells[] =
207465587Seric {
207568508Seric 	"/bin/sh",		/* standard shell */
207665587Seric 	"/usr/bin/sh",
207768508Seric 	"/bin/csh",		/* C shell */
207865587Seric 	"/usr/bin/csh",
207965587Seric #ifdef __hpux
208068889Seric # ifdef V4FS
208168889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
208268889Seric 	"/usr/bin/ksh",		/* Korn shell */
208368889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
208468889Seric 	"/usr/bin/pam",
208568889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
208668889Seric 	"/usr/bin/posix/sh",
208768889Seric # else
208868508Seric 	"/bin/rsh",		/* restricted Bourne shell */
208968508Seric 	"/bin/ksh",		/* Korn shell */
209068508Seric 	"/bin/rksh",		/* restricted Korn shell */
209165587Seric 	"/bin/pam",
209268508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
209365587Seric 	"/bin/posix/sh",
209468889Seric # endif
209565211Seric #endif
209668508Seric #ifdef _AIX3
209768508Seric 	"/bin/ksh",		/* Korn shell */
209868508Seric 	"/usr/bin/ksh",
209968508Seric 	"/bin/tsh",		/* trusted shell */
210068508Seric 	"/usr/bin/tsh",
210168508Seric 	"/bin/bsh",		/* Bourne shell */
210268508Seric 	"/usr/bin/bsh",
210368508Seric #endif
210465587Seric 	NULL
210565587Seric };
210665587Seric 
210765208Seric #endif
210865208Seric 
210965747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
211065747Seric 
211165211Seric bool
211265211Seric usershellok(shell)
211365211Seric 	char *shell;
211465208Seric {
211565211Seric #if HASGETUSERSHELL
211665211Seric 	register char *p;
211765211Seric 	extern char *getusershell();
211865208Seric 
211969505Seric 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
212068457Seric 		return TRUE;
212168457Seric 
212265211Seric 	setusershell();
212365211Seric 	while ((p = getusershell()) != NULL)
212465747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
212565211Seric 			break;
212665211Seric 	endusershell();
212765211Seric 	return p != NULL;
212865211Seric #else
212965211Seric 	register FILE *shellf;
213065211Seric 	char buf[MAXLINE];
213165208Seric 
213268457Seric 	if (shell == NULL || shell[0] == '\0')
213368457Seric 		return TRUE;
213468457Seric 
213565211Seric 	shellf = fopen(_PATH_SHELLS, "r");
213665211Seric 	if (shellf == NULL)
213765211Seric 	{
213865211Seric 		/* no /etc/shells; see if it is one of the std shells */
213965587Seric 		char **d;
214065587Seric 
214165587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
214265587Seric 		{
214365587Seric 			if (strcmp(shell, *d) == 0)
214465587Seric 				return TRUE;
214565587Seric 		}
214665587Seric 		return FALSE;
214765211Seric 	}
214865208Seric 
214965211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
215065211Seric 	{
215165211Seric 		register char *p, *q;
215265208Seric 
215365211Seric 		p = buf;
215465211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
215565211Seric 			p++;
215665211Seric 		if (*p == '#' || *p == '\0')
215765208Seric 			continue;
215865211Seric 		q = p;
215965211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
216065211Seric 			p++;
216165211Seric 		*p = '\0';
216265747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
216365211Seric 		{
216465211Seric 			fclose(shellf);
216565211Seric 			return TRUE;
216665211Seric 		}
216765208Seric 	}
216865211Seric 	fclose(shellf);
216965211Seric 	return FALSE;
217065211Seric #endif
217165208Seric }
217265208Seric /*
217364311Seric **  FREESPACE -- see how much free space is on the queue filesystem
217458082Seric **
217558082Seric **	Only implemented if you have statfs.
217658082Seric **
217758082Seric **	Parameters:
217864311Seric **		dir -- the directory in question.
217964311Seric **		bsize -- a variable into which the filesystem
218064311Seric **			block size is stored.
218158082Seric **
218258082Seric **	Returns:
218364311Seric **		The number of bytes free on the queue filesystem.
218464311Seric **		-1 if the statfs call fails.
218564311Seric **
218664311Seric **	Side effects:
218764311Seric **		Puts the filesystem block size into bsize.
218858082Seric */
218958082Seric 
219065749Seric /* statfs types */
219165749Seric #define SFS_NONE	0	/* no statfs implementation */
219265749Seric #define SFS_USTAT	1	/* use ustat */
219365749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
219465749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
219565749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
219665749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
219766752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
219865749Seric 
219965749Seric #ifndef SFS_TYPE
220065749Seric # define SFS_TYPE	SFS_NONE
220158157Seric #endif
220258157Seric 
220365749Seric #if SFS_TYPE == SFS_USTAT
220458157Seric # include <ustat.h>
220558157Seric #endif
220665749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
220765749Seric # include <sys/statfs.h>
220858082Seric #endif
220965749Seric #if SFS_TYPE == SFS_VFS
221065749Seric # include <sys/vfs.h>
221165749Seric #endif
221265749Seric #if SFS_TYPE == SFS_MOUNT
221365749Seric # include <sys/mount.h>
221465749Seric #endif
221566752Seric #if SFS_TYPE == SFS_STATVFS
221666752Seric # include <sys/statvfs.h>
221766752Seric #endif
221858082Seric 
221964311Seric long
222064311Seric freespace(dir, bsize)
222164311Seric 	char *dir;
222264311Seric 	long *bsize;
222358082Seric {
222465749Seric #if SFS_TYPE != SFS_NONE
222565749Seric # if SFS_TYPE == SFS_USTAT
222658153Seric 	struct ustat fs;
222758153Seric 	struct stat statbuf;
222858366Seric #  define FSBLOCKSIZE	DEV_BSIZE
222968695Seric #  define SFS_BAVAIL	f_tfree
223058157Seric # else
223158157Seric #  if defined(ultrix)
223258157Seric 	struct fs_data fs;
223368695Seric #   define SFS_BAVAIL	fd_bfreen
223468508Seric #   define FSBLOCKSIZE	1024L
223558153Seric #  else
223666752Seric #   if SFS_TYPE == SFS_STATVFS
223766752Seric 	struct statvfs fs;
223868508Seric #    define FSBLOCKSIZE	fs.f_frsize
223966752Seric #   else
224058082Seric 	struct statfs fs;
224166752Seric #    define FSBLOCKSIZE	fs.f_bsize
224263987Seric #   endif
224358153Seric #  endif
224458133Seric # endif
224568695Seric # ifndef SFS_BAVAIL
224668695Seric #  define SFS_BAVAIL f_bavail
224768508Seric # endif
224858082Seric 
224965749Seric # if SFS_TYPE == SFS_USTAT
225064311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
225158157Seric # else
225265749Seric #  if SFS_TYPE == SFS_4ARGS
225364311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
225458157Seric #  else
225568508Seric #   if SFS_TYPE == SFS_STATVFS
225668508Seric 	if (statvfs(dir, &fs) == 0)
225768508Seric #   else
225868508Seric #    if defined(ultrix)
225968499Seric 	if (statfs(dir, &fs) > 0)
226068508Seric #    else
226164311Seric 	if (statfs(dir, &fs) == 0)
226268508Seric #    endif
226358153Seric #   endif
226458133Seric #  endif
226558133Seric # endif
226658133Seric 	{
226764311Seric 		if (bsize != NULL)
226864311Seric 			*bsize = FSBLOCKSIZE;
226968695Seric 		return (fs.SFS_BAVAIL);
227064311Seric 	}
227164337Seric #endif
227264311Seric 	return (-1);
227364311Seric }
227464311Seric /*
227564311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
227664311Seric **
227764311Seric **	Only implemented if you have statfs.
227864311Seric **
227964311Seric **	Parameters:
228064311Seric **		msize -- the size to check against.  If zero, we don't yet
228164311Seric **		know how big the message will be, so just check for
228264311Seric **		a "reasonable" amount.
228364311Seric **
228464311Seric **	Returns:
228564311Seric **		TRUE if there is enough space.
228664311Seric **		FALSE otherwise.
228764311Seric */
228864311Seric 
228964311Seric bool
229064311Seric enoughspace(msize)
229164311Seric 	long msize;
229264311Seric {
229364311Seric 	long bfree, bsize;
229464311Seric 
229564311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
229664311Seric 	{
229758133Seric 		if (tTd(4, 80))
229864311Seric 			printf("enoughspace: no threshold\n");
229964311Seric 		return TRUE;
230064311Seric 	}
230164311Seric 
230264311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
230364311Seric 	{
230464311Seric 		if (tTd(4, 80))
230558333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
230664311Seric 				bfree, msize);
230758333Seric 
230858333Seric 		/* convert msize to block count */
230964311Seric 		msize = msize / bsize + 1;
231058333Seric 		if (MinBlocksFree >= 0)
231158333Seric 			msize += MinBlocksFree;
231258333Seric 
231364311Seric 		if (bfree < msize)
231458090Seric 		{
231558090Seric #ifdef LOG
231658090Seric 			if (LogLevel > 0)
231764254Seric 				syslog(LOG_ALERT,
231864254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
231968508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
232068508Seric 					bfree,
232168508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
232268508Seric 					msize, QueueDir);
232358090Seric #endif
232458082Seric 			return FALSE;
232558090Seric 		}
232658082Seric 	}
232758082Seric 	else if (tTd(4, 80))
232858333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
232958333Seric 			MinBlocksFree, msize, errstring(errno));
233058082Seric 	return TRUE;
233158082Seric }
233258542Seric /*
233358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
233458542Seric **
233558542Seric **	This looks at an errno value and tells if this is likely to
233658542Seric **	go away if retried later.
233758542Seric **
233858542Seric **	Parameters:
233958542Seric **		err -- the errno code to classify.
234058542Seric **
234158542Seric **	Returns:
234258542Seric **		TRUE if this is probably transient.
234358542Seric **		FALSE otherwise.
234458542Seric */
234558542Seric 
234658542Seric bool
234758542Seric transienterror(err)
234858542Seric 	int err;
234958542Seric {
235058542Seric 	switch (err)
235158542Seric 	{
235258542Seric 	  case EIO:			/* I/O error */
235358542Seric 	  case ENXIO:			/* Device not configured */
235458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
235558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
235658542Seric 	  case ENODEV:			/* Operation not supported by device */
235758542Seric 	  case ENFILE:			/* Too many open files in system */
235858542Seric 	  case EMFILE:			/* Too many open files */
235958542Seric 	  case ENOSPC:			/* No space left on device */
236058542Seric #ifdef ETIMEDOUT
236158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
236258542Seric #endif
236358542Seric #ifdef ESTALE
236458542Seric 	  case ESTALE:			/* Stale NFS file handle */
236558542Seric #endif
236658542Seric #ifdef ENETDOWN
236758542Seric 	  case ENETDOWN:		/* Network is down */
236858542Seric #endif
236958542Seric #ifdef ENETUNREACH
237058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
237158542Seric #endif
237258542Seric #ifdef ENETRESET
237358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
237458542Seric #endif
237558542Seric #ifdef ECONNABORTED
237658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
237758542Seric #endif
237858542Seric #ifdef ECONNRESET
237958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
238058542Seric #endif
238158542Seric #ifdef ENOBUFS
238258542Seric 	  case ENOBUFS:			/* No buffer space available */
238358542Seric #endif
238458542Seric #ifdef ESHUTDOWN
238558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
238658542Seric #endif
238758542Seric #ifdef ECONNREFUSED
238858542Seric 	  case ECONNREFUSED:		/* Connection refused */
238958542Seric #endif
239058542Seric #ifdef EHOSTDOWN
239158542Seric 	  case EHOSTDOWN:		/* Host is down */
239258542Seric #endif
239358542Seric #ifdef EHOSTUNREACH
239458542Seric 	  case EHOSTUNREACH:		/* No route to host */
239558542Seric #endif
239658542Seric #ifdef EDQUOT
239758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
239858542Seric #endif
239958542Seric #ifdef EPROCLIM
240058542Seric 	  case EPROCLIM:		/* Too many processes */
240158542Seric #endif
240258542Seric #ifdef EUSERS
240358542Seric 	  case EUSERS:			/* Too many users */
240458542Seric #endif
240558542Seric #ifdef EDEADLK
240658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
240758542Seric #endif
240858542Seric #ifdef EISCONN
240958542Seric 	  case EISCONN:			/* Socket already connected */
241058542Seric #endif
241158542Seric #ifdef EINPROGRESS
241258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
241358542Seric #endif
241458542Seric #ifdef EALREADY
241558542Seric 	  case EALREADY:		/* Operation already in progress */
241658542Seric #endif
241758542Seric #ifdef EADDRINUSE
241858542Seric 	  case EADDRINUSE:		/* Address already in use */
241958542Seric #endif
242058542Seric #ifdef EADDRNOTAVAIL
242158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
242258542Seric #endif
242366018Seric #ifdef ETXTBSY
242466017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
242566017Seric #endif
242663834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
242758542Seric 	  case ENOSR:			/* Out of streams resources */
242858542Seric #endif
242958542Seric 		return TRUE;
243058542Seric 	}
243158542Seric 
243258542Seric 	/* nope, must be permanent */
243358542Seric 	return FALSE;
243458542Seric }
243558689Seric /*
243664035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
243758689Seric **
243858689Seric **	Parameters:
243958689Seric **		fd -- the file descriptor of the file.
244058689Seric **		filename -- the file name (for error messages).
244164335Seric **		ext -- the filename extension.
244258689Seric **		type -- type of the lock.  Bits can be:
244358689Seric **			LOCK_EX -- exclusive lock.
244458689Seric **			LOCK_NB -- non-blocking.
244558689Seric **
244658689Seric **	Returns:
244758689Seric **		TRUE if the lock was acquired.
244858689Seric **		FALSE otherwise.
244958689Seric */
245058689Seric 
245158689Seric bool
245264335Seric lockfile(fd, filename, ext, type)
245358689Seric 	int fd;
245458689Seric 	char *filename;
245564335Seric 	char *ext;
245658689Seric 	int type;
245758689Seric {
245865830Seric # if !HASFLOCK
245958689Seric 	int action;
246058689Seric 	struct flock lfd;
246164335Seric 
246264335Seric 	if (ext == NULL)
246364335Seric 		ext = "";
246469472Seric 
246563983Seric 	bzero(&lfd, sizeof lfd);
246659447Seric 	if (bitset(LOCK_UN, type))
246759447Seric 		lfd.l_type = F_UNLCK;
246859447Seric 	else if (bitset(LOCK_EX, type))
246958689Seric 		lfd.l_type = F_WRLCK;
247058689Seric 	else
247158689Seric 		lfd.l_type = F_RDLCK;
247258689Seric 
247358689Seric 	if (bitset(LOCK_NB, type))
247458689Seric 		action = F_SETLK;
247558689Seric 	else
247658689Seric 		action = F_SETLKW;
247758689Seric 
247864264Seric 	if (tTd(55, 60))
247964335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
248064335Seric 			filename, ext, action, lfd.l_type);
248164264Seric 
248258689Seric 	if (fcntl(fd, action, &lfd) >= 0)
248364264Seric 	{
248464264Seric 		if (tTd(55, 60))
248564264Seric 			printf("SUCCESS\n");
248658689Seric 		return TRUE;
248764264Seric 	}
248858689Seric 
248964264Seric 	if (tTd(55, 60))
249064264Seric 		printf("(%s) ", errstring(errno));
249164264Seric 
249264136Seric 	/*
249364136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
249464136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
249564136Seric 	**  as type "tmp" (that is, served from swap space), the
249664136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
249764136Seric 	**  Since this is fairly common during testing, we will assume
249864136Seric 	**  that this indicates that the lock is successfully grabbed.
249964136Seric 	*/
250064136Seric 
250164136Seric 	if (errno == EINVAL)
250264264Seric 	{
250364264Seric 		if (tTd(55, 60))
250464264Seric 			printf("SUCCESS\n");
250564136Seric 		return TRUE;
250664264Seric 	}
250764136Seric 
250858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
250964378Seric 	{
251064378Seric 		int omode = -1;
251164378Seric #  ifdef F_GETFL
251264378Seric 		int oerrno = errno;
251364378Seric 
251464378Seric 		(void) fcntl(fd, F_GETFL, &omode);
251564378Seric 		errno = oerrno;
251664378Seric #  endif
251764378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
251864378Seric 			filename, ext, fd, type, omode, geteuid());
251964378Seric 	}
252058689Seric # else
252164335Seric 	if (ext == NULL)
252264335Seric 		ext = "";
252364335Seric 
252464264Seric 	if (tTd(55, 60))
252564335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
252664264Seric 
252758689Seric 	if (flock(fd, type) >= 0)
252864264Seric 	{
252964264Seric 		if (tTd(55, 60))
253064264Seric 			printf("SUCCESS\n");
253158689Seric 		return TRUE;
253264264Seric 	}
253358689Seric 
253464264Seric 	if (tTd(55, 60))
253564264Seric 		printf("(%s) ", errstring(errno));
253664264Seric 
253758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
253864378Seric 	{
253964378Seric 		int omode = -1;
254064378Seric #  ifdef F_GETFL
254164378Seric 		int oerrno = errno;
254264378Seric 
254364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
254464378Seric 		errno = oerrno;
254564378Seric #  endif
254664378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
254764378Seric 			filename, ext, fd, type, omode, geteuid());
254864386Seric 	}
254958689Seric # endif
255064264Seric 	if (tTd(55, 60))
255164264Seric 		printf("FAIL\n");
255258689Seric 	return FALSE;
255358689Seric }
255464556Seric /*
255565948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
255665948Seric **
255765948Seric **	Parameters:
255865948Seric **		fd -- the file descriptor to check.
255965948Seric **
256065948Seric **	Returns:
256165948Seric **		TRUE -- if only root can chown the file to an arbitrary
256265948Seric **			user.
256365948Seric **		FALSE -- if an arbitrary user can give away a file.
256465948Seric */
256565948Seric 
256665948Seric bool
256765948Seric chownsafe(fd)
256865948Seric 	int fd;
256965948Seric {
257065950Seric #ifdef __hpux
257165948Seric 	char *s;
257265948Seric 	int tfd;
257365948Seric 	uid_t o_uid, o_euid;
257465948Seric 	gid_t o_gid, o_egid;
257565948Seric 	bool rval;
257665948Seric 	struct stat stbuf;
257765948Seric 
257865948Seric 	o_uid = getuid();
257965948Seric 	o_euid = geteuid();
258065948Seric 	o_gid = getgid();
258165948Seric 	o_egid = getegid();
258265959Seric 	fstat(fd, &stbuf);
258365948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
258465948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
258565948Seric 	s = tmpnam(NULL);
258665948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
258765974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
258865948Seric 	close(tfd);
258965948Seric 	unlink(s);
259065948Seric 	setreuid(o_uid, o_euid);
259165948Seric 	setresgid(o_gid, o_egid, -1);
259265948Seric 	return rval;
259365948Seric #else
259466088Seric # ifdef _POSIX_CHOWN_RESTRICTED
259566088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
259666088Seric 	return FALSE;
259766088Seric #  else
259866088Seric 	return TRUE;
259966088Seric #  endif
260066088Seric # else
260166088Seric #  ifdef _PC_CHOWN_RESTRICTED
260268508Seric 	int rval;
260368508Seric 
260468508Seric 	/*
260568508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
260668508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
260768508Seric 	**  the call.  This heuristic checks for that.
260868508Seric 	*/
260968508Seric 
261068508Seric 	errno = 0;
261168508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
261268508Seric 	if (errno == 0)
261368508Seric 		return rval > 0;
261468508Seric #  endif
261568508Seric #  ifdef BSD
261668508Seric 	return TRUE;
261768499Seric #  else
261865948Seric 	return FALSE;
261965948Seric #  endif
262065948Seric # endif
262165948Seric #endif
262265948Seric }
262365948Seric /*
262468508Seric **  RESETLIMITS -- reset system controlled resource limits
262568508Seric **
262668508Seric **	This is to avoid denial-of-service attacks
262768508Seric **
262868508Seric **	Parameters:
262968508Seric **		none
263068508Seric **
263168508Seric **	Returns:
263268508Seric **		none
263368508Seric */
263468508Seric 
263568508Seric #if HASSETRLIMIT
263668508Seric # include <sys/resource.h>
263768508Seric #endif
263868508Seric 
263968508Seric void
264068508Seric resetlimits()
264168508Seric {
264268508Seric #if HASSETRLIMIT
264368508Seric 	struct rlimit lim;
264468508Seric 
264568508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
264668508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
264768508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
264868508Seric #else
264968508Seric # if HASULIMIT
265068508Seric 	(void) ulimit(2, 0x3fffff);
265168508Seric # endif
265268508Seric #endif
265368508Seric }
265468508Seric /*
265564556Seric **  GETCFNAME -- return the name of the .cf file.
265664556Seric **
265764556Seric **	Some systems (e.g., NeXT) determine this dynamically.
265864556Seric */
265964556Seric 
266064556Seric char *
266164556Seric getcfname()
266264556Seric {
266368842Seric 	int i;
266468842Seric 	static char cbuf[200];
266568842Seric 
266664556Seric 	if (ConfFile != NULL)
266764556Seric 		return ConfFile;
266865214Seric #ifdef NETINFO
266965214Seric 	{
267065214Seric 		extern char *ni_propval();
267165214Seric 		char *cflocation;
267265214Seric 
267368508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
267468508Seric 					"sendmail.cf", '\0');
267565214Seric 		if (cflocation != NULL)
267665214Seric 			return cflocation;
267765214Seric 	}
267865214Seric #endif
267968842Seric 
268069396Seric #ifdef TRY_VERSIONED_CF_NAME
268168842Seric 	/*
268268842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
268368842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
268468842Seric 	**
268568842Seric 	**	I suppose it should really try a search path here --
268668842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
268768842Seric 	**	/usr/lib/sendmail.cf, and so forth.
268868842Seric 	*/
268968842Seric 
269068842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
269168842Seric 	i = strlen(cbuf);
269268842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
269368842Seric 	{
269468842Seric 		char *p;
269568842Seric 		extern char Version[];
269668842Seric 
269768842Seric 		strcpy(&cbuf[i - 2], Version);
269868842Seric 		p = strchr(&cbuf[i - 2], '/');
269968842Seric 		if (p != NULL)
270068842Seric 			*p = '\0';
270168842Seric 		p = &cbuf[strlen(cbuf)];
270268842Seric 		do
270368842Seric 		{
270468842Seric 			int fd;
270568842Seric 
270668842Seric 			strcpy(p, ".cf");
270768842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
270868842Seric 			{
270968842Seric 				close(fd);
271068842Seric 				return cbuf;
271168842Seric 			}
271268842Seric 			*p = '\0';
271368842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
271468842Seric 	}
271569396Seric #endif
271664556Seric 	return _PATH_SENDMAILCF;
271764556Seric }
271864718Seric /*
271964718Seric **  SETVENDOR -- process vendor code from V configuration line
272064718Seric **
272164718Seric **	Parameters:
272264718Seric **		vendor -- string representation of vendor.
272364718Seric **
272464718Seric **	Returns:
272564718Seric **		TRUE -- if ok.
272664718Seric **		FALSE -- if vendor code could not be processed.
272764926Seric **
272864926Seric **	Side Effects:
272964926Seric **		It is reasonable to set mode flags here to tweak
273064926Seric **		processing in other parts of the code if necessary.
273164926Seric **		For example, if you are a vendor that uses $%y to
273264926Seric **		indicate YP lookups, you could enable that here.
273364718Seric */
273464718Seric 
273564718Seric bool
273664718Seric setvendor(vendor)
273764718Seric 	char *vendor;
273864718Seric {
273964926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
274068508Seric 	{
274168508Seric 		VendorCode = VENDOR_BERKELEY;
274264926Seric 		return TRUE;
274368508Seric 	}
274464926Seric 
274564926Seric 	/* add vendor extensions here */
274664926Seric 
274768508Seric #ifdef SUN_EXTENSIONS
274868508Seric 	if (strcasecmp(vendor, "Sun") == 0)
274968508Seric 	{
275068508Seric 		VendorCode = VENDOR_SUN;
275168508Seric 		return TRUE;
275268508Seric 	}
275368508Seric #endif
275468508Seric 
275564926Seric 	return FALSE;
275664718Seric }
275764816Seric /*
275869510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
275969510Seric **
276069510Seric **	Vendor_pre_defaults is called before reading the configuration
276169510Seric **	file; vendor_post_defaults is called immediately after.
276269510Seric **
276369510Seric **	Parameters:
276469510Seric **		e -- the global environment to initialize.
276569510Seric **
276669510Seric **	Returns:
276769510Seric **		none.
276869510Seric */
276969510Seric 
277069510Seric void
277169510Seric vendor_pre_defaults(e)
277269510Seric 	ENVELOPE *e;
277369510Seric {
277469510Seric }
277569510Seric 
277669510Seric 
277769510Seric void
277869510Seric vendor_post_defaults(e)
277969510Seric 	ENVELOPE *e;
278069510Seric {
278169510Seric }
278269510Seric /*
278364816Seric **  STRTOL -- convert string to long integer
278464816Seric **
278564816Seric **	For systems that don't have it in the C library.
278666161Seric **
278766161Seric **	This is taken verbatim from the 4.4-Lite C library.
278864816Seric */
278964816Seric 
279064816Seric #ifdef NEEDSTRTOL
279164816Seric 
279266161Seric #if defined(LIBC_SCCS) && !defined(lint)
279366161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
279466161Seric #endif /* LIBC_SCCS and not lint */
279566161Seric 
279666161Seric #include <limits.h>
279766161Seric 
279866161Seric /*
279966161Seric  * Convert a string to a long integer.
280066161Seric  *
280166161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
280266161Seric  * alphabets and digits are each contiguous.
280366161Seric  */
280466161Seric 
280564816Seric long
280666161Seric strtol(nptr, endptr, base)
280766161Seric 	const char *nptr;
280866161Seric 	char **endptr;
280966161Seric 	register int base;
281064816Seric {
281166161Seric 	register const char *s = nptr;
281266161Seric 	register unsigned long acc;
281366161Seric 	register int c;
281466161Seric 	register unsigned long cutoff;
281566161Seric 	register int neg = 0, any, cutlim;
281664816Seric 
281766161Seric 	/*
281866161Seric 	 * Skip white space and pick up leading +/- sign if any.
281966161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
282066161Seric 	 * assume decimal; if base is already 16, allow 0x.
282166161Seric 	 */
282266161Seric 	do {
282366161Seric 		c = *s++;
282466161Seric 	} while (isspace(c));
282566161Seric 	if (c == '-') {
282666161Seric 		neg = 1;
282766161Seric 		c = *s++;
282866161Seric 	} else if (c == '+')
282966161Seric 		c = *s++;
283066161Seric 	if ((base == 0 || base == 16) &&
283166161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
283266161Seric 		c = s[1];
283366161Seric 		s += 2;
283466161Seric 		base = 16;
283566161Seric 	}
283666161Seric 	if (base == 0)
283766161Seric 		base = c == '0' ? 8 : 10;
283864816Seric 
283966161Seric 	/*
284066161Seric 	 * Compute the cutoff value between legal numbers and illegal
284166161Seric 	 * numbers.  That is the largest legal value, divided by the
284266161Seric 	 * base.  An input number that is greater than this value, if
284366161Seric 	 * followed by a legal input character, is too big.  One that
284466161Seric 	 * is equal to this value may be valid or not; the limit
284566161Seric 	 * between valid and invalid numbers is then based on the last
284666161Seric 	 * digit.  For instance, if the range for longs is
284766161Seric 	 * [-2147483648..2147483647] and the input base is 10,
284866161Seric 	 * cutoff will be set to 214748364 and cutlim to either
284966161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
285066161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
285166161Seric 	 * the number is too big, and we will return a range error.
285266161Seric 	 *
285366161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
285466161Seric 	 * overflow.
285566161Seric 	 */
285666161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
285766161Seric 	cutlim = cutoff % (unsigned long)base;
285866161Seric 	cutoff /= (unsigned long)base;
285966161Seric 	for (acc = 0, any = 0;; c = *s++) {
286066161Seric 		if (isdigit(c))
286166161Seric 			c -= '0';
286266161Seric 		else if (isalpha(c))
286366161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
286466161Seric 		else
286566161Seric 			break;
286666161Seric 		if (c >= base)
286766161Seric 			break;
286866161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
286966161Seric 			any = -1;
287066161Seric 		else {
287166161Seric 			any = 1;
287266161Seric 			acc *= base;
287366161Seric 			acc += c;
287464816Seric 		}
287564816Seric 	}
287666161Seric 	if (any < 0) {
287766161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
287866161Seric 		errno = ERANGE;
287966161Seric 	} else if (neg)
288066161Seric 		acc = -acc;
288166161Seric 	if (endptr != 0)
288266161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
288366161Seric 	return (acc);
288464816Seric }
288564816Seric 
288664816Seric #endif
288764841Seric /*
288868693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
288964841Seric **
289068693Seric **	Some operating systems have wierd problems with the gethostbyXXX
289168693Seric **	routines.  For example, Solaris versions at least through 2.3
289268693Seric **	don't properly deliver a canonical h_name field.  This tries to
289368693Seric **	work around these problems.
289464841Seric */
289564841Seric 
289664841Seric struct hostent *
289768693Seric sm_gethostbyname(name)
289868707Seric 	char *name;
289964841Seric {
290068693Seric #if defined(SOLARIS) && SOLARIS < 204
290168693Seric 	extern int h_errno;
290268693Seric 
290368508Seric # if SOLARIS == 203
290464841Seric 	static struct hostent hp;
290564841Seric 	static char buf[1000];
290664841Seric 	extern struct hostent *_switch_gethostbyname_r();
290764841Seric 
290864841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
290964841Seric # else
291065009Seric 	extern struct hostent *__switch_gethostbyname();
291165009Seric 
291264841Seric 	return __switch_gethostbyname(name);
291364841Seric # endif
291468693Seric #else
291568693Seric 	return gethostbyname(name);
291668693Seric #endif
291764841Seric }
291864841Seric 
291964841Seric struct hostent *
292068693Seric sm_gethostbyaddr(addr, len, type)
292168707Seric 	char *addr;
292264841Seric 	int len;
292364841Seric 	int type;
292464841Seric {
292568693Seric #if defined(SOLARIS) && SOLARIS < 204
292668693Seric 	extern int h_errno;
292768693Seric 
292868508Seric # if SOLARIS == 203
292964841Seric 	static struct hostent hp;
293064841Seric 	static char buf[1000];
293164841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
293264841Seric 
293364841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
293464841Seric # else
293565009Seric 	extern struct hostent *__switch_gethostbyaddr();
293665009Seric 
293764841Seric 	return __switch_gethostbyaddr(addr, len, type);
293864841Seric # endif
293968693Seric #else
294068693Seric 	return gethostbyaddr(addr, len, type);
294168693Seric #endif
294264841Seric }
294368693Seric /*
294468693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
294568693Seric */
294664841Seric 
294768693Seric struct passwd *
294868693Seric sm_getpwnam(user)
294968704Seric 	char *user;
295068693Seric {
295168693Seric 	return getpwnam(user);
295268693Seric }
295368693Seric 
295468693Seric struct passwd *
295568693Seric sm_getpwuid(uid)
295668693Seric 	uid_t uid;
295768693Seric {
295868693Seric 	return getpwuid(uid);
295968693Seric }
296065214Seric /*
296169472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
296269472Seric **
296369472Seric **	Parameters:
296469472Seric **		none.
296569472Seric **
296669472Seric **	Returns:
296769472Seric **		none.
296869472Seric **
296969472Seric **	Side Effects:
297069472Seric **		Loads $=w with the names of all the interfaces.
297169472Seric */
297269472Seric 
297369472Seric #ifdef SIOCGIFCONF
297469472Seric # include <netdb.h>
297569472Seric # include <arpa/inet.h>
297669472Seric # include <net/if.h>
297769472Seric #endif
297869472Seric 
297969472Seric void
298069472Seric load_if_names()
298169472Seric {
298269472Seric #ifdef SIOCGIFCONF
298369472Seric 	int s;
2984*69516Seric 	int i;
298569472Seric         struct ifconf ifc;
298669472Seric 	char interfacebuf[1024];
298769472Seric 
298869472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
298969472Seric 	if (s == -1)
299069472Seric 		return;
299169472Seric 
299269472Seric 	/* get the list of known IP address from the kernel */
299369472Seric         ifc.ifc_buf = interfacebuf;
2994*69516Seric         ifc.ifc_len = sizeof interfacebuf;
299569472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
299669472Seric 	{
299769472Seric 		if (tTd(0, 4))
299869472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
2999*69516Seric 		close(s);
300069472Seric 		return;
300169472Seric 	}
3002*69516Seric 	close(s);
300369472Seric 
300469472Seric 	/* scan the list of IP address */
300569472Seric 	if (tTd(0, 4))
300669472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
300769472Seric 			ifc.ifc_len);
300869472Seric 
3009*69516Seric 	for (i = 0; i < ifc.ifc_len; )
3010*69516Seric 	{
3011*69516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
3012*69516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
301369472Seric 		struct in_addr ia;
3014*69516Seric 		struct hostent *hp;
301569472Seric 		char ip_addr[256];
3016*69516Seric 		extern char *inet_ntoa();
3017*69516Seric 		extern struct hostent *gethostbyaddr();
301869472Seric 
3019*69516Seric #ifdef BSD4_4_SOCKADDR
3020*69516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
3021*69516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
3022*69516Seric 		else
3023*69516Seric #endif
3024*69516Seric 			i += sizeof *ifr;
3025*69516Seric 
302669472Seric 		if (tTd(0, 20))
3027*69516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
302869472Seric 
302969472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
303069472Seric 			continue;
303169472Seric 
303269472Seric 		/* extract IP address from the list*/
3033*69516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
303469472Seric 
303569472Seric 		/* save IP address in text from */
303669472Seric 		(void) sprintf(ip_addr, "[%s]",
3037*69516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
303869472Seric 		if (!wordinclass(ip_addr, 'w'))
303969472Seric 		{
304069472Seric 			setclass('w', ip_addr);
304169472Seric 			if (tTd(0, 4))
304269472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
304369472Seric 		}
304469472Seric 
304569472Seric 		/* skip "loopback" interface "lo" */
304669472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
304769472Seric 			continue;
304869472Seric 
304969472Seric 		/* lookup name with IP address */
305069472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
305169472Seric 		if (hp == NULL)
305269472Seric 		{
305369472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
305469472Seric 				inet_ntoa(ia));
305569472Seric 			continue;
305669472Seric 		}
305769472Seric 
305869472Seric 		/* save its cname */
305969472Seric 		if (!wordinclass(hp->h_name, 'w'))
306069472Seric 		{
306169472Seric 			setclass('w', hp->h_name);
306269472Seric 			if (tTd(0, 4))
306369472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
306469472Seric 		}
306569472Seric 
306669472Seric 		/* save all it aliases name */
306769472Seric 		while (*hp->h_aliases)
306869472Seric 		{
306969472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
307069472Seric 			{
307169472Seric 				setclass('w', *hp->h_aliases);
307269472Seric 				if (tTd(0, 4))
307369472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
307469472Seric 			}
307569472Seric 			hp->h_aliases++;
307669472Seric 		}
307769472Seric 	}
307869472Seric #endif
307969472Seric }
308069472Seric /*
308165214Seric **  NI_PROPVAL -- netinfo property value lookup routine
308265214Seric **
308365214Seric **	Parameters:
308468508Seric **		keydir -- the Netinfo directory name in which to search
308568508Seric **			for the key.
308668508Seric **		keyprop -- the name of the property in which to find the
308768508Seric **			property we are interested.  Defaults to "name".
308868508Seric **		keyval -- the value for which we are really searching.
308968508Seric **		valprop -- the property name for the value in which we
309068508Seric **			are interested.
309168508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
309268508Seric **			we should return a string separated by this
309368508Seric **			character.
309465214Seric **
309565214Seric **	Returns:
309665214Seric **		NULL -- if:
309765214Seric **			1. the directory is not found
309865214Seric **			2. the property name is not found
309965214Seric **			3. the property contains multiple values
310065214Seric **			4. some error occured
310165214Seric **		else -- the location of the config file.
310265214Seric **
310368508Seric **	Example:
310468508Seric **		To search for an alias value, use:
310568508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
310668508Seric **
310765214Seric **	Notes:
310865214Seric **      	Caller should free the return value of ni_proval
310965214Seric */
311065214Seric 
311165214Seric #ifdef NETINFO
311265214Seric 
311365214Seric # include <netinfo/ni.h>
311465214Seric 
311565214Seric # define LOCAL_NETINFO_DOMAIN    "."
311665214Seric # define PARENT_NETINFO_DOMAIN   ".."
311765214Seric # define MAX_NI_LEVELS           256
311865214Seric 
311965214Seric char *
312068508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
312168508Seric 	char *keydir;
312268508Seric 	char *keyprop;
312368508Seric 	char *keyval;
312468508Seric 	char *valprop;
312568508Seric 	char sepchar;
312665214Seric {
312765820Seric 	char *propval = NULL;
312865214Seric 	int i;
312968508Seric 	int j, alen;
313065214Seric 	void *ni = NULL;
313165214Seric 	void *lastni = NULL;
313265214Seric 	ni_status nis;
313365214Seric 	ni_id nid;
313465214Seric 	ni_namelist ninl;
313568508Seric 	register char *p;
313668508Seric 	char keybuf[1024];
313765214Seric 
313865214Seric 	/*
313968508Seric 	**  Create the full key from the two parts.
314068508Seric 	**
314168508Seric 	**	Note that directory can end with, e.g., "name=" to specify
314268508Seric 	**	an alternate search property.
314368508Seric 	*/
314468508Seric 
314568508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
314668508Seric 	if (keyprop != NULL)
314768508Seric 		i += strlen(keyprop) + 1;
314868508Seric 	if (i > sizeof keybuf)
314968508Seric 		return NULL;
315068508Seric 	strcpy(keybuf, keydir);
315168508Seric 	strcat(keybuf, "/");
315268508Seric 	if (keyprop != NULL)
315368508Seric 	{
315468508Seric 		strcat(keybuf, keyprop);
315568508Seric 		strcat(keybuf, "=");
315668508Seric 	}
315768508Seric 	strcat(keybuf, keyval);
315868508Seric 
315968508Seric 	/*
316065214Seric 	**  If the passed directory and property name are found
316165214Seric 	**  in one of netinfo domains we need to search (starting
316265214Seric 	**  from the local domain moving all the way back to the
316365214Seric 	**  root domain) set propval to the property's value
316465214Seric 	**  and return it.
316565214Seric 	*/
316665214Seric 
316765214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
316865214Seric 	{
316965214Seric 		if (i == 0)
317065214Seric 		{
317165214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
317265214Seric 		}
317365214Seric 		else
317465214Seric 		{
317565214Seric 			if (lastni != NULL)
317665214Seric 				ni_free(lastni);
317765214Seric 			lastni = ni;
317865214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
317965214Seric 		}
318065214Seric 
318165214Seric 		/*
318265214Seric 		**  Don't bother if we didn't get a handle on a
318365214Seric 		**  proper domain.  This is not necessarily an error.
318465214Seric 		**  We would get a positive ni_status if, for instance
318565214Seric 		**  we never found the directory or property and tried
318665214Seric 		**  to open the parent of the root domain!
318765214Seric 		*/
318865214Seric 
318965214Seric 		if (nis != 0)
319065214Seric 			break;
319165214Seric 
319265214Seric 		/*
319365214Seric 		**  Find the path to the server information.
319465214Seric 		*/
319565214Seric 
319668508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
319765214Seric 			continue;
319865214Seric 
319965214Seric 		/*
320068508Seric 		**  Find associated value information.
320165214Seric 		*/
320265214Seric 
320368508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
320465214Seric 			continue;
320565214Seric 
320665214Seric 		/*
320768508Seric 		**  See if we have an acceptable number of values.
320865214Seric 		*/
320965214Seric 
321068508Seric 		if (ninl.ni_namelist_len <= 0)
321168508Seric 			continue;
321268508Seric 
321368508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
321465214Seric 		{
321568508Seric 			ni_namelist_free(&ninl);
321668508Seric 			continue;
321765214Seric 		}
321868508Seric 
321969472Seric 		/*
322068508Seric 		**  Calculate number of bytes needed and build result
322168508Seric 		*/
322268508Seric 
322368508Seric 		alen = 1;
322468508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
322568508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
322668508Seric 		propval = p = xalloc(alen);
322768508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
322868508Seric 		{
322968508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
323068508Seric 			p += strlen(p);
323168508Seric 			*p++ = sepchar;
323269472Seric 		}
323368508Seric 		*--p = '\0';
323468508Seric 
323568508Seric 		ni_namelist_free(&ninl);
323665214Seric 	}
323765214Seric 
323865214Seric 	/*
323965214Seric 	**  Clean up.
324065214Seric 	*/
324165214Seric 
324265214Seric 	if (ni != NULL)
324365214Seric 		ni_free(ni);
324465214Seric 	if (lastni != NULL && ni != lastni)
324565214Seric 		ni_free(lastni);
324665214Seric 
324765214Seric 	return propval;
324865214Seric }
324965214Seric 
325065214Seric #endif /* NETINFO */
325165504Seric /*
325265504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
325365504Seric **
325465504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
325565504Seric **	syslog succeeds during interrupt handlers.
325665504Seric */
325765504Seric 
325865504Seric #ifdef __hpux
325965504Seric 
326065504Seric # define MAXSYSLOGTRIES	100
326165504Seric # undef syslog
326265504Seric 
326365504Seric # ifdef __STDC__
326465504Seric hard_syslog(int pri, char *msg, ...)
326565504Seric # else
326665504Seric hard_syslog(pri, msg, va_alist)
326765504Seric 	int pri;
326865504Seric 	char *msg;
326965504Seric 	va_dcl
327065504Seric # endif
327165504Seric {
327265504Seric 	int i;
327365504Seric 	char buf[SYSLOG_BUFSIZE * 2];
327465504Seric 	VA_LOCAL_DECL;
327565504Seric 
327665504Seric 	VA_START(msg);
327765504Seric 	vsprintf(buf, msg, ap);
327865504Seric 	VA_END;
327965504Seric 
328065504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
328165504Seric 		continue;
328265504Seric }
328365504Seric 
328465504Seric #endif
328568696Seric /*
328668696Seric **  LOCAL_HOSTNAME_LENGTH
328768696Seric **
328868696Seric **	This is required to get sendmail to compile against BIND 4.9.x
328968696Seric **	on Ultrix.
329068696Seric */
329168696Seric 
329268696Seric #if defined(ultrix) && NAMED_BIND
329368696Seric 
329468696Seric # include <resolv.h>
329568696Seric # if __RES >= 19931104
329668696Seric 
329768696Seric int
329868696Seric local_hostname_length(hostname)
329968696Seric 	char *hostname;
330068696Seric {
330168696Seric 	int len_host, len_domain;
330268696Seric 
330368696Seric 	if (!*_res.defdname)
330468696Seric 		res_init();
330568696Seric 	len_host = strlen(hostname);
330668696Seric 	len_domain = strlen(_res.defdname);
330768696Seric 	if (len_host > len_domain &&
330868696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
330968696Seric 	    hostname[len_host - len_domain - 1] == '.')
331068696Seric 		return len_host - len_domain - 1;
331168696Seric 	else
331268696Seric 		return 0;
331368696Seric }
331468696Seric 
331568696Seric # endif
331668696Seric #endif
331768741Seric /*
331868741Seric **  Compile-Time options
331968741Seric */
332068741Seric 
332168741Seric char	*CompileOptions[] =
332268741Seric {
332368741Seric #if HESIOD
332468741Seric 	"HESIOD",
332568741Seric #endif
332668741Seric #if LOG
332768741Seric 	"LOG",
332868741Seric #endif
332968741Seric #if MATCHGECOS
333068741Seric 	"MATCHGECOS",
333168741Seric #endif
333268741Seric #if NAMED_BIND
333368741Seric 	"NAMED_BIND",
333468741Seric #endif
333568741Seric #if NDBM
333668741Seric 	"NDBM",
333768741Seric #endif
333868741Seric #if NETINET
333968741Seric 	"NETINET",
334068741Seric #endif
334168741Seric #if NETINFO
334268741Seric 	"NETINFO",
334368741Seric #endif
334468741Seric #if NETISO
334568741Seric 	"NETISO",
334668741Seric #endif
334768741Seric #if NETNS
334868741Seric 	"NETNS",
334968741Seric #endif
335068741Seric #if NETUNIX
335168741Seric 	"NETUNIX",
335268741Seric #endif
335368741Seric #if NETX25
335468741Seric 	"NETX25",
335568741Seric #endif
335668741Seric #if NEWDB
335768741Seric 	"NEWDB",
335868741Seric #endif
335968741Seric #if NIS
336068741Seric 	"NIS",
336168741Seric #endif
336268741Seric #if SCANF
336368741Seric 	"SCANF",
336468741Seric #endif
336568741Seric #if SUID_ROOT_FILES_OK
336668741Seric 	"SUID_ROOT_FILES_OK",
336768741Seric #endif
336868741Seric #if USERDB
336968741Seric 	"USERDB",
337068741Seric #endif
337168741Seric #if XDEBUG
337268741Seric 	"XDEBUG",
337368741Seric #endif
337468741Seric #if XLA
337568741Seric 	"XLA",
337668741Seric #endif
337768741Seric 	NULL
337868741Seric };
3379