xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69654)
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*69654Seric static char sccsid[] = "@(#)conf.c	8.174 (Berkeley) 05/24/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 
1091*69654Seric #ifndef LA_AVENRUN
1092*69654Seric # ifdef SYSTEM5
1093*69654Seric #  define LA_AVENRUN	"avenrun"
1094*69654Seric # else
1095*69654Seric #  define LA_AVENRUN	"_avenrun"
1096*69654Seric # endif
1097*69654Seric #endif
1098*69654Seric 
109964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
110051920Seric 
110114872Seric #include <nlist.h>
110214872Seric 
110368508Seric #ifdef IRIX64
110468508Seric # define nlist		nlist64
110568508Seric #endif
110668508Seric 
110751920Seric /* _PATH_UNIX should be defined in <paths.h> */
110851920Seric #ifndef _PATH_UNIX
110963962Seric # if defined(SYSTEM5)
111063962Seric #  define _PATH_UNIX	"/unix"
111163962Seric # else
111263962Seric #  define _PATH_UNIX	"/vmunix"
111363962Seric # endif
111451920Seric #endif
111551920Seric 
111669462Seric struct nlist	Nl[] =
111714872Seric {
111851920Seric 	{ LA_AVENRUN },
111914872Seric #define	X_AVENRUN	0
112014872Seric 	{ 0 },
112114872Seric };
112214872Seric 
112314872Seric getla()
112414872Seric {
112514872Seric 	static int kmem = -1;
112651920Seric #if LA_TYPE == LA_INT
112724943Seric 	long avenrun[3];
112851920Seric #else
112964360Seric # if LA_TYPE == LA_SHORT
113064360Seric 	short avenrun[3];
113164360Seric # else
113251920Seric 	double avenrun[3];
113364360Seric # endif
113451920Seric #endif
113569462Seric 	extern int errno;
113625615Seric 	extern off_t lseek();
113714872Seric 
113814872Seric 	if (kmem < 0)
113914872Seric 	{
114024945Seric 		kmem = open("/dev/kmem", 0, 0);
114114872Seric 		if (kmem < 0)
114257736Seric 		{
114357736Seric 			if (tTd(3, 1))
114457736Seric 				printf("getla: open(/dev/kmem): %s\n",
114557736Seric 					errstring(errno));
114614872Seric 			return (-1);
114757736Seric 		}
114851920Seric 		(void) fcntl(kmem, F_SETFD, 1);
114969462Seric 
115068508Seric #ifdef _AIX3
115168508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
115268508Seric #else
115357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
115468508Seric #endif
115557736Seric 		{
115657736Seric 			if (tTd(3, 1))
115757736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
115857736Seric 					errstring(errno));
115914872Seric 			return (-1);
116057736Seric 		}
116159253Seric 		if (Nl[X_AVENRUN].n_value == 0)
116259253Seric 		{
116359253Seric 			if (tTd(3, 1))
116459253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
116559253Seric 					_PATH_UNIX, LA_AVENRUN);
116659253Seric 			return (-1);
116759253Seric 		}
116868508Seric #ifdef NAMELISTMASK
116968508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
117066763Seric #endif
117114872Seric 	}
117257736Seric 	if (tTd(3, 20))
117357736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
117468508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
117523118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
117619967Seric 	{
117719967Seric 		/* thank you Ian */
117857736Seric 		if (tTd(3, 1))
117957736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
118019967Seric 		return (-1);
118119967Seric 	}
118269462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
118357736Seric 	if (tTd(3, 5))
118457736Seric 	{
118557736Seric 		printf("getla: avenrun = %d", avenrun[0]);
118657736Seric 		if (tTd(3, 15))
118757736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
118857736Seric 		printf("\n");
118957736Seric 	}
119057736Seric 	if (tTd(3, 1))
119157736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119224943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119369462Seric # else /* LA_TYPE == LA_FLOAT */
119457736Seric 	if (tTd(3, 5))
119557736Seric 	{
119657736Seric 		printf("getla: avenrun = %g", avenrun[0]);
119757736Seric 		if (tTd(3, 15))
119857736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
119957736Seric 		printf("\n");
120057736Seric 	}
120157736Seric 	if (tTd(3, 1))
120257736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
120351920Seric 	return ((int) (avenrun[0] + 0.5));
120469462Seric # endif
120514872Seric }
120614872Seric 
120769462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
120851773Seric 
120969462Seric #if LA_TYPE == LA_READKSYM
121064718Seric 
121169473Seric # include <sys/ksym.h>
121269473Seric 
121369462Seric getla()
121469462Seric {
121569462Seric 	static int kmem = -1;
121669462Seric 	long avenrun[3];
121769462Seric 	extern int errno;
121869462Seric 	struct mioc_rksym mirk;
121964718Seric 
122069462Seric 	if (kmem < 0)
122169462Seric 	{
122269462Seric 		kmem = open("/dev/kmem", 0, 0);
122369462Seric 		if (kmem < 0)
122469462Seric 		{
122569462Seric 			if (tTd(3, 1))
122669462Seric 				printf("getla: open(/dev/kmem): %s\n",
122769462Seric 					errstring(errno));
122869462Seric 			return (-1);
122969462Seric 		}
123069462Seric 		(void) fcntl(kmem, F_SETFD, 1);
123169462Seric 	}
123269462Seric 	mirk.mirk_symname = LA_AVENRUN;
123369462Seric 	mirk.mirk_buf = avenrun;
123469462Seric 	mirk.mirk_buflen = sizeof(avenrun);
123569462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
123669462Seric 	{
123769462Seric 		if (tTd(3, 1))
123869462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
123969462Seric 				errstring(errno));
124069462Seric 		return -1;
124169462Seric 	}
124269462Seric 	if (tTd(3, 5))
124369462Seric 	{
124469462Seric 		printf("getla: avenrun = %d", avenrun[0]);
124569462Seric 		if (tTd(3, 15))
124669462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
124769462Seric 		printf("\n");
124869462Seric 	}
124969462Seric 	if (tTd(3, 1))
125069462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125169462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125269462Seric }
125369462Seric 
125469462Seric #endif /* LA_TYPE == LA_READKSYM */
125569462Seric 
125669462Seric #if LA_TYPE == LA_DGUX
125769462Seric 
125869462Seric # include <sys/dg_sys_info.h>
125969462Seric 
126068508Seric int
126168508Seric getla()
126264718Seric {
126364718Seric 	struct dg_sys_info_load_info load_info;
126464718Seric 
126564718Seric 	dg_sys_info((long *)&load_info,
126664718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
126764718Seric 
126868508Seric         if (tTd(3, 1))
126968508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
127068508Seric 
127164718Seric 	return((int) (load_info.one_minute + 0.5));
127264718Seric }
127364718Seric 
127469462Seric #endif /* LA_TYPE == LA_DGUX */
127564718Seric 
127669462Seric #if LA_TYPE == LA_HPUX
127769462Seric 
127869563Seric /* forward declarations to keep gcc from complaining */
127968869Seric struct pst_dynamic;
128069563Seric struct pst_status;
128169563Seric struct pst_static;
128269563Seric struct pst_vminfo;
128369563Seric struct pst_diskinfo;
128469563Seric struct pst_processor;
128569563Seric struct pst_lv;
128669563Seric struct pst_swapinfo;
128768869Seric 
128869462Seric # include <sys/param.h>
128969462Seric # include <sys/pstat.h>
129068508Seric 
129168508Seric int
129251773Seric getla()
129351773Seric {
129468508Seric 	struct pst_dynamic pstd;
129568508Seric 
129668508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
129768869Seric 			     (size_t) 1, 0) == -1)
129868508Seric 		return 0;
129968508Seric 
130068508Seric         if (tTd(3, 1))
130168508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
130268508Seric 
130368508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
130468508Seric }
130568508Seric 
130669462Seric #endif /* LA_TYPE == LA_HPUX */
130768508Seric 
130869462Seric #if LA_TYPE == LA_SUBR
130969462Seric 
131068508Seric int
131168508Seric getla()
131268508Seric {
131351920Seric 	double avenrun[3];
131451920Seric 
131551920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
131657736Seric 	{
131757736Seric 		if (tTd(3, 1))
131857736Seric 			perror("getla: getloadavg failed:");
131951920Seric 		return (-1);
132057736Seric 	}
132157736Seric 	if (tTd(3, 1))
132257736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
132351920Seric 	return ((int) (avenrun[0] + 0.5));
132451773Seric }
132551773Seric 
132669462Seric #endif /* LA_TYPE == LA_SUBR */
132769462Seric 
132864295Seric #if LA_TYPE == LA_MACH
132951773Seric 
133064295Seric /*
133165173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
133264295Seric */
133364295Seric 
133465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
133565173Seric # include <mach/mach.h>
133665173Seric #else
133765173Seric # include <mach.h>
133865173Seric #endif
133964295Seric 
134051773Seric getla()
134151773Seric {
134264295Seric 	processor_set_t default_set;
134364295Seric 	kern_return_t error;
134464295Seric 	unsigned int info_count;
134564295Seric 	struct processor_set_basic_info info;
134664295Seric 	host_t host;
134764295Seric 
134864295Seric 	error = processor_set_default(host_self(), &default_set);
134964295Seric 	if (error != KERN_SUCCESS)
135064295Seric 		return -1;
135164295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
135264295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
135364295Seric 			       &host, (processor_set_info_t)&info,
135464295Seric 			       &info_count) != KERN_SUCCESS)
135564295Seric 	{
135664295Seric 		return -1;
135764295Seric 	}
135864295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
135964295Seric }
136064295Seric 
136169462Seric #endif /* LA_TYPE == LA_MACH */
136264295Seric 
136366313Seric #if LA_TYPE == LA_PROCSTR
136464295Seric 
136566301Seric /*
136666301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
136766301Seric **  in a format like "0.15 0.12 0.06".
136866301Seric **
136966301Seric **	Initially intended for Linux.  This has been in the kernel
137066301Seric **	since at least 0.99.15.
137166301Seric */
137266301Seric 
137366301Seric # ifndef _PATH_LOADAVG
137466301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
137566301Seric # endif
137666301Seric 
137766301Seric int
137864295Seric getla()
137964295Seric {
138066319Seric 	double avenrun;
138166301Seric 	register int result;
138266319Seric 	FILE *fp;
138366301Seric 
138466319Seric 	fp = fopen(_PATH_LOADAVG, "r");
138569472Seric 	if (fp == NULL)
138666301Seric 	{
138766319Seric 		if (tTd(3, 1))
138866319Seric 			printf("getla: fopen(%s): %s\n",
138966319Seric 				_PATH_LOADAVG, errstring(errno));
139066319Seric 		return -1;
139166301Seric 	}
139266301Seric 	result = fscanf(fp, "%lf", &avenrun);
139366319Seric 	fclose(fp);
139466301Seric 	if (result != 1)
139566301Seric 	{
139666310Seric 		if (tTd(3, 1))
139766301Seric 			printf("getla: fscanf() = %d: %s\n",
139866301Seric 				result, errstring(errno));
139966301Seric 		return -1;
140066301Seric 	}
140166301Seric 
140257736Seric 	if (tTd(3, 1))
140366301Seric 		printf("getla(): %.2f\n", avenrun);
140466301Seric 
140566301Seric 	return ((int) (avenrun + 0.5));
140666301Seric }
140766301Seric 
140869462Seric #endif /* LA_TYPE == LA_PROCSTR */
140966301Seric 
141069462Seric #if LA_TYPE == LA_ZERO
141169462Seric 
141266301Seric getla()
141366301Seric {
141466301Seric 	if (tTd(3, 1))
141557736Seric 		printf("getla: ZERO\n");
141651920Seric 	return (0);
141751773Seric }
141851773Seric 
141969462Seric #endif /* LA_TYPE == LA_ZERO */
142066045Seric 
142166045Seric 
142266045Seric /*
142366045Seric  * Copyright 1989 Massachusetts Institute of Technology
142466045Seric  *
142566045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
142666045Seric  * documentation for any purpose is hereby granted without fee, provided that
142766045Seric  * the above copyright notice appear in all copies and that both that
142866045Seric  * copyright notice and this permission notice appear in supporting
142966045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
143066045Seric  * publicity pertaining to distribution of the software without specific,
143166045Seric  * written prior permission.  M.I.T. makes no representations about the
143266045Seric  * suitability of this software for any purpose.  It is provided "as is"
143366045Seric  * without express or implied warranty.
143466045Seric  *
143566045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
143666045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
143766045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143866045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
143966045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
144066045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
144166045Seric  *
144266045Seric  * Authors:  Many and varied...
144366045Seric  */
144466045Seric 
144566045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
144666045Seric #ifndef lint
144766045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
144866045Seric #endif /* !lint */
144966045Seric 
145066179Seric #ifdef apollo
145166045Seric # undef volatile
145266045Seric #    include <apollo/base.h>
145366045Seric 
145466045Seric /* ARGSUSED */
145566045Seric int getloadavg( call_data )
145666045Seric      caddr_t	call_data;	/* pointer to (double) return value */
145766045Seric {
145866045Seric      double *avenrun = (double *) call_data;
145966045Seric      int i;
146066045Seric      status_$t      st;
146166045Seric      long loadav[3];
146266045Seric      proc1_$get_loadav(loadav, &st);
146366045Seric      *avenrun = loadav[0] / (double) (1 << 16);
146466045Seric      return(0);
146566045Seric }
146666045Seric #   endif /* apollo */
146724943Seric /*
146824943Seric **  SHOULDQUEUE -- should this message be queued or sent?
146924943Seric **
147024943Seric **	Compares the message cost to the load average to decide.
147124943Seric **
147224943Seric **	Parameters:
147324943Seric **		pri -- the priority of the message in question.
147457438Seric **		ctime -- the message creation time.
147524943Seric **
147624943Seric **	Returns:
147724943Seric **		TRUE -- if this message should be queued up for the
147824943Seric **			time being.
147924943Seric **		FALSE -- if the load is low enough to send this message.
148024943Seric **
148124943Seric **	Side Effects:
148224943Seric **		none.
148324943Seric */
148424943Seric 
148524943Seric bool
148657438Seric shouldqueue(pri, ctime)
148724943Seric 	long pri;
148857438Seric 	time_t ctime;
148924943Seric {
149068508Seric 	bool rval;
149168508Seric 
149268508Seric 	if (tTd(3, 30))
149368508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
149451920Seric 	if (CurrentLA < QueueLA)
149568508Seric 	{
149668508Seric 		if (tTd(3, 30))
149768508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
149824943Seric 		return (FALSE);
149968508Seric 	}
150058132Seric 	if (CurrentLA >= RefuseLA)
150168508Seric 	{
150268508Seric 		if (tTd(3, 30))
150368508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
150458132Seric 		return (TRUE);
150568508Seric 	}
150668508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
150768508Seric 	if (tTd(3, 30))
150868508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
150968508Seric 	return rval;
151024943Seric }
151124943Seric /*
151253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
151353037Seric **
151453037Seric **	Parameters:
151553037Seric **		none.
151653037Seric **
151753037Seric **	Returns:
151853037Seric **		TRUE if incoming SMTP connections should be refused
151953037Seric **			(for now).
152053037Seric **		FALSE if we should accept new work.
152153037Seric **
152253037Seric **	Side Effects:
152353037Seric **		none.
152453037Seric */
152553037Seric 
152653037Seric bool
152753037Seric refuseconnections()
152853037Seric {
152968508Seric 	extern bool enoughspace();
153068508Seric 
153159156Seric #ifdef XLA
153259156Seric 	if (!xla_smtp_ok())
153359156Seric 		return TRUE;
153459156Seric #endif
153559156Seric 
153653037Seric 	/* this is probably too simplistic */
153768508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
153853037Seric }
153953037Seric /*
154024943Seric **  SETPROCTITLE -- set process title for ps
154124943Seric **
154224943Seric **	Parameters:
154358674Seric **		fmt -- a printf style format string.
154458674Seric **		a, b, c -- possible parameters to fmt.
154524943Seric **
154624943Seric **	Returns:
154724943Seric **		none.
154824943Seric **
154924943Seric **	Side Effects:
155024943Seric **		Clobbers argv of our main procedure so ps(1) will
155124943Seric **		display the title.
155224943Seric */
155324943Seric 
155468508Seric #define SPT_NONE	0	/* don't use it at all */
155568508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
155668508Seric #define SPT_BUILTIN	2	/* use libc builtin */
155768508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
155868508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
1559*69654Seric #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
156068508Seric 
156168508Seric #ifndef SPT_TYPE
156268508Seric # define SPT_TYPE	SPT_REUSEARGV
156368508Seric #endif
156468508Seric 
156568508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
156668508Seric 
156768508Seric # if SPT_TYPE == SPT_PSTAT
156858689Seric #  include <sys/pstat.h>
156958689Seric # endif
157068508Seric # if SPT_TYPE == SPT_PSSTRINGS
157159732Seric #  include <machine/vmparam.h>
157259732Seric #  include <sys/exec.h>
157368508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
157468508Seric #   undef SPT_TYPE
157568508Seric #   define SPT_TYPE	SPT_REUSEARGV
157668734Seric #  else
157768734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
157868734Seric #    define NKPDE 63
157968734Seric typedef unsigned int	*pt_entry_t;
158068734Seric #   endif
158164247Seric #  endif
158259732Seric # endif
158368508Seric 
158468508Seric # if SPT_TYPE == SPT_PSSTRINGS
158568508Seric #  define SETPROC_STATIC	static
158668508Seric # else
158759732Seric #  define SETPROC_STATIC
158859732Seric # endif
158967626Seric 
1590*69654Seric # if SPT_TYPE == SPT_SYSMIPS
1591*69654Seric #  include <sys/sysmips.h>
1592*69654Seric #  include <sys/sysnews.h>
1593*69654Seric # endif
1594*69654Seric 
159568508Seric # ifndef SPT_PADCHAR
159668508Seric #  define SPT_PADCHAR	' '
159768508Seric # endif
159858689Seric 
159968508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
160064843Seric 
160168508Seric #if SPT_TYPE != SPT_BUILTIN
160268508Seric 
160324943Seric /*VARARGS1*/
160468508Seric void
160568508Seric # ifdef __STDC__
160657642Seric setproctitle(char *fmt, ...)
160768508Seric # else
160857642Seric setproctitle(fmt, va_alist)
160924943Seric 	char *fmt;
161057642Seric 	va_dcl
161168508Seric # endif
161224943Seric {
161368508Seric # if SPT_TYPE != SPT_NONE
161424943Seric 	register char *p;
161525049Seric 	register int i;
161659732Seric 	SETPROC_STATIC char buf[MAXLINE];
161756852Seric 	VA_LOCAL_DECL
161868508Seric #  if SPT_TYPE == SPT_PSTAT
161958689Seric 	union pstun pst;
162058689Seric #  endif
162124943Seric 	extern char **Argv;
162224943Seric 	extern char *LastArgv;
162324943Seric 
162458674Seric 	p = buf;
162524943Seric 
162658674Seric 	/* print sendmail: heading for grep */
162758674Seric 	(void) strcpy(p, "sendmail: ");
162858674Seric 	p += strlen(p);
162924943Seric 
163058674Seric 	/* print the argument string */
163158674Seric 	VA_START(fmt);
163258674Seric 	(void) vsprintf(p, fmt, ap);
163356852Seric 	VA_END;
163454996Seric 
163558674Seric 	i = strlen(buf);
163658689Seric 
163768508Seric #  if SPT_TYPE == SPT_PSTAT
163858689Seric 	pst.pst_command = buf;
163958689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
1640*69654Seric #  endif
1641*69654Seric #  if SPT_TYPE == SPT_PSSTRINGS
164259732Seric 	PS_STRINGS->ps_nargvstr = 1;
164359732Seric 	PS_STRINGS->ps_argvstr = buf;
1644*69654Seric #  endif
1645*69654Seric #  if SPT_TYPE == SPT_SYSMIPS
1646*69654Seric 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
1647*69654Seric #  endif
1648*69654Seric #  if SPT_TYPE == SPT_REUSEARGV
164954996Seric 	if (i > LastArgv - Argv[0] - 2)
165025049Seric 	{
165154996Seric 		i = LastArgv - Argv[0] - 2;
165258674Seric 		buf[i] = '\0';
165325049Seric 	}
165458674Seric 	(void) strcpy(Argv[0], buf);
165554997Seric 	p = &Argv[0][i];
165624943Seric 	while (p < LastArgv)
165768508Seric 		*p++ = SPT_PADCHAR;
165868508Seric 	Argv[1] = NULL;
1659*69654Seric #  endif
166068508Seric # endif /* SPT_TYPE != SPT_NONE */
166124943Seric }
166265982Seric 
166368508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
166425698Seric /*
166525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
166625698Seric **
166725698Seric **	Parameters:
166868508Seric **		sig -- the signal that got us here (unused).
166925698Seric **
167025698Seric **	Returns:
167125698Seric **		none.
167225698Seric **
167325698Seric **	Side Effects:
167425698Seric **		Picks up extant zombies.
167525698Seric */
167625698Seric 
167746928Sbostic void
167868508Seric reapchild(sig)
167968508Seric 	int sig;
168025698Seric {
168164738Seric 	int olderrno = errno;
168263966Seric # ifdef HASWAITPID
168360219Seric 	auto int status;
168460560Seric 	int count;
168560560Seric 	int pid;
168660219Seric 
168760560Seric 	count = 0;
168860560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
168960560Seric 	{
169060560Seric 		if (count++ > 1000)
169160560Seric 		{
169266748Seric #ifdef LOG
169360560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
169460560Seric 				pid, status);
169566748Seric #endif
169660560Seric 			break;
169760560Seric 		}
169860560Seric 	}
169960219Seric # else
170025698Seric # ifdef WNOHANG
170125698Seric 	union wait status;
170225698Seric 
170363753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
170425698Seric 		continue;
170556795Seric # else /* WNOHANG */
170625698Seric 	auto int status;
170725698Seric 
170860219Seric 	while (wait(&status) > 0)
170925698Seric 		continue;
171056795Seric # endif /* WNOHANG */
171160219Seric # endif
171264035Seric # ifdef SYS5SIGNALS
171364035Seric 	(void) setsignal(SIGCHLD, reapchild);
171458061Seric # endif
171564738Seric 	errno = olderrno;
171625698Seric }
171755418Seric /*
1718*69654Seric **  PUTENV -- emulation of putenv() in terms of setenv()
1719*69654Seric **
1720*69654Seric **	Not needed on Posix-compliant systems.
1721*69654Seric **	This doesn't have full Posix semantics, but it's good enough
1722*69654Seric **		for sendmail.
1723*69654Seric **
1724*69654Seric **	Parameter:
1725*69654Seric **		env -- the environment to put.
1726*69654Seric **
1727*69654Seric **	Returns:
1728*69654Seric **		none.
1729*69654Seric */
1730*69654Seric 
1731*69654Seric #ifdef NEEDPUTENV
1732*69654Seric 
1733*69654Seric void
1734*69654Seric putenv(env)
1735*69654Seric 	char *env;
1736*69654Seric {
1737*69654Seric 	char *p;
1738*69654Seric 	int l;
1739*69654Seric 	char nbuf[100];
1740*69654Seric 
1741*69654Seric 	p = strchr(env, '=');
1742*69654Seric 	if (p == NULL)
1743*69654Seric 		return;
1744*69654Seric 	l = p - env;
1745*69654Seric 	if (l > sizeof nbuf - 1)
1746*69654Seric 		l = sizeof nbuf - 1;
1747*69654Seric 	bcopy(env, nbuf, l);
1748*69654Seric 	nbuf[l] = '\0';
1749*69654Seric 	setenv(nbuf, ++p, 1);
1750*69654Seric }
1751*69654Seric 
1752*69654Seric #endif
1753*69654Seric /*
175455418Seric **  UNSETENV -- remove a variable from the environment
175555418Seric **
175655418Seric **	Not needed on newer systems.
175755418Seric **
175855418Seric **	Parameters:
175955418Seric **		name -- the string name of the environment variable to be
176055418Seric **			deleted from the current environment.
176155418Seric **
176255418Seric **	Returns:
176355418Seric **		none.
176455418Seric **
176555418Seric **	Globals:
176655418Seric **		environ -- a pointer to the current environment.
176755418Seric **
176855418Seric **	Side Effects:
176955418Seric **		Modifies environ.
177055418Seric */
177155418Seric 
177263962Seric #ifndef HASUNSETENV
177355418Seric 
177455418Seric void
177555418Seric unsetenv(name)
177655418Seric 	char *name;
177755418Seric {
177855418Seric 	extern char **environ;
177955418Seric 	register char **pp;
178055418Seric 	int len = strlen(name);
178155418Seric 
178255418Seric 	for (pp = environ; *pp != NULL; pp++)
178355418Seric 	{
178455418Seric 		if (strncmp(name, *pp, len) == 0 &&
178555418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
178655418Seric 			break;
178755418Seric 	}
178855418Seric 
178955418Seric 	for (; *pp != NULL; pp++)
179055418Seric 		*pp = pp[1];
179155418Seric }
179255418Seric 
179363962Seric #endif
179456215Seric /*
179556215Seric **  GETDTABLESIZE -- return number of file descriptors
179656215Seric **
179756215Seric **	Only on non-BSD systems
179856215Seric **
179956215Seric **	Parameters:
180056215Seric **		none
180156215Seric **
180256215Seric **	Returns:
180356215Seric **		size of file descriptor table
180456215Seric **
180556215Seric **	Side Effects:
180656215Seric **		none
180756215Seric */
180856215Seric 
180963787Seric #ifdef SOLARIS
181063787Seric # include <sys/resource.h>
181163787Seric #endif
181256215Seric 
181356215Seric int
181463787Seric getdtsize()
181556215Seric {
181663787Seric #ifdef RLIMIT_NOFILE
181763787Seric 	struct rlimit rl;
181863787Seric 
181963787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
182063787Seric 		return rl.rlim_cur;
182163787Seric #endif
182263787Seric 
182364031Seric # ifdef HASGETDTABLESIZE
182464031Seric 	return getdtablesize();
182564031Seric # else
182664031Seric #  ifdef _SC_OPEN_MAX
182758689Seric 	return sysconf(_SC_OPEN_MAX);
182863787Seric #  else
182956215Seric 	return NOFILE;
183063787Seric #  endif
183158689Seric # endif
183256215Seric }
183357631Seric /*
183457631Seric **  UNAME -- get the UUCP name of this system.
183557631Seric */
183657631Seric 
183757943Seric #ifndef HASUNAME
183857631Seric 
183957631Seric int
184057631Seric uname(name)
184157631Seric 	struct utsname *name;
184257631Seric {
184357631Seric 	FILE *file;
184457631Seric 	char *n;
184557631Seric 
184657631Seric 	name->nodename[0] = '\0';
184757631Seric 
184857661Seric 	/* try /etc/whoami -- one line with the node name */
184957631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
185057631Seric 	{
185157661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
185257631Seric 		(void) fclose(file);
185357661Seric 		n = strchr(name->nodename, '\n');
185457631Seric 		if (n != NULL)
185557631Seric 			*n = '\0';
185657631Seric 		if (name->nodename[0] != '\0')
185757631Seric 			return (0);
185857631Seric 	}
185957631Seric 
186057661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
186157631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
186257631Seric 	{
186357631Seric 		char buf[MAXLINE];
186457631Seric 
186557631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
186657631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
186757631Seric 					NODE_LENGTH, name->nodename) > 0)
186857631Seric 				break;
186957631Seric 		(void) fclose(file);
187057631Seric 		if (name->nodename[0] != '\0')
187157631Seric 			return (0);
187257631Seric 	}
187357631Seric 
187457631Seric #ifdef TRUST_POPEN
187557631Seric 	/*
187657631Seric 	**  Popen is known to have security holes.
187757631Seric 	*/
187857631Seric 
187957661Seric 	/* try uuname -l to return local name */
188057631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
188157631Seric 	{
188257661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
188357631Seric 		(void) pclose(file);
188457661Seric 		n = strchr(name, '\n');
188557631Seric 		if (n != NULL)
188657631Seric 			*n = '\0';
188757661Seric 		if (name->nodename[0] != '\0')
188857631Seric 			return (0);
188957631Seric 	}
189057631Seric #endif
189169472Seric 
189257631Seric 	return (-1);
189357631Seric }
189457943Seric #endif /* HASUNAME */
189558068Seric /*
189658068Seric **  INITGROUPS -- initialize groups
189758068Seric **
189858068Seric **	Stub implementation for System V style systems
189958068Seric */
190058068Seric 
190158068Seric #ifndef HASINITGROUPS
190258068Seric 
190358068Seric initgroups(name, basegid)
190458068Seric 	char *name;
190558068Seric 	int basegid;
190658068Seric {
190758068Seric 	return 0;
190858068Seric }
190958068Seric 
191058068Seric #endif
191158082Seric /*
191259289Seric **  SETSID -- set session id (for non-POSIX systems)
191359289Seric */
191459289Seric 
191559289Seric #ifndef HASSETSID
191659289Seric 
191763753Seric pid_t
191863753Seric setsid __P ((void))
191959289Seric {
192064155Seric #ifdef TIOCNOTTY
192164155Seric 	int fd;
192264155Seric 
192366743Seric 	fd = open("/dev/tty", O_RDWR, 0);
192464155Seric 	if (fd >= 0)
192564155Seric 	{
192664155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
192764155Seric 		(void) close(fd);
192864155Seric 	}
192964155Seric #endif /* TIOCNOTTY */
193064705Seric # ifdef SYS5SETPGRP
193163753Seric 	return setpgrp();
193263753Seric # else
193364155Seric 	return setpgid(0, getpid());
193459289Seric # endif
193559289Seric }
193659289Seric 
193759289Seric #endif
193859289Seric /*
193966748Seric **  FSYNC -- dummy fsync
194066748Seric */
194166748Seric 
194266748Seric #ifdef NEEDFSYNC
194366748Seric 
194466748Seric fsync(fd)
194566748Seric 	int fd;
194666748Seric {
194766748Seric # ifdef O_SYNC
194866748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
194966748Seric # else
195066748Seric 	/* nothing we can do */
195166748Seric 	return 0;
195266748Seric # endif
195366748Seric }
195466748Seric 
195566748Seric #endif
195666748Seric /*
195764718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
195864718Seric **
195964718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
196068508Seric **	instead of a long.  This patches things.  Only needed on versions
196168508Seric **	prior to 5.4.3.
196264718Seric */
196364718Seric 
196468508Seric #ifdef DGUX_5_4_2
196564718Seric 
196664718Seric #undef inet_addr
196764718Seric 
196864718Seric long
196964718Seric dgux_inet_addr(host)
197064718Seric 	char *host;
197164718Seric {
197264718Seric 	struct in_addr haddr;
197364718Seric 
197464718Seric 	haddr = inet_addr(host);
197564718Seric 	return haddr.s_addr;
197664718Seric }
197764718Seric 
197864718Seric #endif
197964718Seric /*
198063969Seric **  GETOPT -- for old systems or systems with bogus implementations
198163969Seric */
198263969Seric 
198363969Seric #ifdef NEEDGETOPT
198463969Seric 
198563969Seric /*
198663969Seric  * Copyright (c) 1985 Regents of the University of California.
198763969Seric  * All rights reserved.  The Berkeley software License Agreement
198863969Seric  * specifies the terms and conditions for redistribution.
198963969Seric  */
199063969Seric 
199163969Seric 
199263969Seric /*
199363969Seric ** this version hacked to add `atend' flag to allow state machine
199463969Seric ** to reset if invoked by the program to scan args for a 2nd time
199563969Seric */
199663969Seric 
199763969Seric #if defined(LIBC_SCCS) && !defined(lint)
199863969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
199964072Seric #endif /* LIBC_SCCS and not lint */
200063969Seric 
200163969Seric #include <stdio.h>
200263969Seric 
200363969Seric /*
200463969Seric  * get option letter from argument vector
200563969Seric  */
200665352Seric #ifdef _CONVEX_SOURCE
200768508Seric extern int	optind, opterr, optopt;
200868508Seric extern char	*optarg;
200965352Seric #else
201065352Seric int	opterr = 1;		/* if error message should be printed */
201165352Seric int	optind = 1;		/* index into parent argv vector */
201268470Seric int	optopt = 0;		/* character checked for validity */
201368470Seric char	*optarg = NULL;		/* argument associated with option */
201468508Seric #endif
201563969Seric 
201663969Seric #define BADCH	(int)'?'
201763969Seric #define EMSG	""
201863969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
201963969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
202063969Seric 
202163969Seric getopt(nargc,nargv,ostr)
202265160Seric 	int		nargc;
202365160Seric 	char *const	*nargv;
202465160Seric 	const char	*ostr;
202563969Seric {
202663969Seric 	static char	*place = EMSG;	/* option letter processing */
202763969Seric 	static char	atend = 0;
202863969Seric 	register char	*oli;		/* option letter list index */
202963969Seric 
203063969Seric 	if (atend) {
203163969Seric 		atend = 0;
203263969Seric 		place = EMSG;
203363969Seric 	}
203463969Seric 	if(!*place) {			/* update scanning pointer */
203564136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
203663969Seric 			atend++;
203763969Seric 			return(EOF);
203863969Seric 		}
203963969Seric 		if (*place == '-') {	/* found "--" */
204063969Seric 			++optind;
204163969Seric 			atend++;
204263969Seric 			return(EOF);
204363969Seric 		}
204463969Seric 	}				/* option letter okay? */
204564125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
204664136Seric 		if (!*place) ++optind;
204763969Seric 		tell(": illegal option -- ");
204863969Seric 	}
204963969Seric 	if (*++oli != ':') {		/* don't need argument */
205063969Seric 		optarg = NULL;
205163969Seric 		if (!*place) ++optind;
205263969Seric 	}
205363969Seric 	else {				/* need an argument */
205463969Seric 		if (*place) optarg = place;	/* no white space */
205563969Seric 		else if (nargc <= ++optind) {	/* no arg */
205663969Seric 			place = EMSG;
205763969Seric 			tell(": option requires an argument -- ");
205863969Seric 		}
205963969Seric 	 	else optarg = nargv[optind];	/* white space */
206063969Seric 		place = EMSG;
206163969Seric 		++optind;
206263969Seric 	}
206363969Seric 	return(optopt);			/* dump back option letter */
206463969Seric }
206563969Seric 
206663969Seric #endif
206763969Seric /*
206863969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
206963969Seric */
207063969Seric 
207163969Seric #ifdef NEEDVPRINTF
207263969Seric 
207363969Seric #define MAXARG	16
207463969Seric 
207563969Seric vfprintf(fp, fmt, ap)
207663969Seric 	FILE *	fp;
207763969Seric 	char *	fmt;
207863969Seric 	char **	ap;
207963969Seric {
208063969Seric 	char *	bp[MAXARG];
208163969Seric 	int	i = 0;
208263969Seric 
208363969Seric 	while (*ap && i < MAXARG)
208463969Seric 		bp[i++] = *ap++;
208563969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
208663969Seric 			 bp[4], bp[5], bp[6], bp[7],
208763969Seric 			 bp[8], bp[9], bp[10], bp[11],
208863969Seric 			 bp[12], bp[13], bp[14], bp[15]);
208963969Seric }
209063969Seric 
209163969Seric vsprintf(s, fmt, ap)
209263969Seric 	char *	s;
209363969Seric 	char *	fmt;
209463969Seric 	char **	ap;
209563969Seric {
209663969Seric 	char *	bp[MAXARG];
209763969Seric 	int	i = 0;
209863969Seric 
209963969Seric 	while (*ap && i < MAXARG)
210063969Seric 		bp[i++] = *ap++;
210163969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
210263969Seric 			bp[4], bp[5], bp[6], bp[7],
210363969Seric 			bp[8], bp[9], bp[10], bp[11],
210463969Seric 			bp[12], bp[13], bp[14], bp[15]);
210563969Seric }
210663969Seric 
210763969Seric #endif
210863969Seric /*
210965220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
211065208Seric **
211165211Seric **	Parameters:
211265211Seric **		shell -- the user's shell from /etc/passwd
211365211Seric **
211465211Seric **	Returns:
211565211Seric **		TRUE -- if it is ok to use this for unrestricted access.
211665211Seric **		FALSE -- if the shell is restricted.
211765208Seric */
211865208Seric 
211965587Seric #if !HASGETUSERSHELL
212065587Seric 
212165587Seric # ifndef _PATH_SHELLS
212265587Seric #  define _PATH_SHELLS	"/etc/shells"
212365587Seric # endif
212465587Seric 
212565587Seric char	*DefaultUserShells[] =
212665587Seric {
212768508Seric 	"/bin/sh",		/* standard shell */
212865587Seric 	"/usr/bin/sh",
212968508Seric 	"/bin/csh",		/* C shell */
213065587Seric 	"/usr/bin/csh",
213165587Seric #ifdef __hpux
213268889Seric # ifdef V4FS
213368889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
213468889Seric 	"/usr/bin/ksh",		/* Korn shell */
213568889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
213668889Seric 	"/usr/bin/pam",
213768889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
213868889Seric 	"/usr/bin/posix/sh",
213968889Seric # else
214068508Seric 	"/bin/rsh",		/* restricted Bourne shell */
214168508Seric 	"/bin/ksh",		/* Korn shell */
214268508Seric 	"/bin/rksh",		/* restricted Korn shell */
214365587Seric 	"/bin/pam",
214468508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
214565587Seric 	"/bin/posix/sh",
214668889Seric # endif
214765211Seric #endif
214868508Seric #ifdef _AIX3
214968508Seric 	"/bin/ksh",		/* Korn shell */
215068508Seric 	"/usr/bin/ksh",
215168508Seric 	"/bin/tsh",		/* trusted shell */
215268508Seric 	"/usr/bin/tsh",
215368508Seric 	"/bin/bsh",		/* Bourne shell */
215468508Seric 	"/usr/bin/bsh",
215568508Seric #endif
215665587Seric 	NULL
215765587Seric };
215865587Seric 
215965208Seric #endif
216065208Seric 
216165747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
216265747Seric 
216365211Seric bool
216465211Seric usershellok(shell)
216565211Seric 	char *shell;
216665208Seric {
216765211Seric #if HASGETUSERSHELL
216865211Seric 	register char *p;
216965211Seric 	extern char *getusershell();
217065208Seric 
217169505Seric 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
217268457Seric 		return TRUE;
217368457Seric 
217465211Seric 	setusershell();
217565211Seric 	while ((p = getusershell()) != NULL)
217665747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
217765211Seric 			break;
217865211Seric 	endusershell();
217965211Seric 	return p != NULL;
218065211Seric #else
218165211Seric 	register FILE *shellf;
218265211Seric 	char buf[MAXLINE];
218365208Seric 
218468457Seric 	if (shell == NULL || shell[0] == '\0')
218568457Seric 		return TRUE;
218668457Seric 
218765211Seric 	shellf = fopen(_PATH_SHELLS, "r");
218865211Seric 	if (shellf == NULL)
218965211Seric 	{
219065211Seric 		/* no /etc/shells; see if it is one of the std shells */
219165587Seric 		char **d;
219265587Seric 
219365587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
219465587Seric 		{
219565587Seric 			if (strcmp(shell, *d) == 0)
219665587Seric 				return TRUE;
219765587Seric 		}
219865587Seric 		return FALSE;
219965211Seric 	}
220065208Seric 
220165211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
220265211Seric 	{
220365211Seric 		register char *p, *q;
220465208Seric 
220565211Seric 		p = buf;
220665211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
220765211Seric 			p++;
220865211Seric 		if (*p == '#' || *p == '\0')
220965208Seric 			continue;
221065211Seric 		q = p;
221165211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
221265211Seric 			p++;
221365211Seric 		*p = '\0';
221465747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
221565211Seric 		{
221665211Seric 			fclose(shellf);
221765211Seric 			return TRUE;
221865211Seric 		}
221965208Seric 	}
222065211Seric 	fclose(shellf);
222165211Seric 	return FALSE;
222265211Seric #endif
222365208Seric }
222465208Seric /*
222564311Seric **  FREESPACE -- see how much free space is on the queue filesystem
222658082Seric **
222758082Seric **	Only implemented if you have statfs.
222858082Seric **
222958082Seric **	Parameters:
223064311Seric **		dir -- the directory in question.
223164311Seric **		bsize -- a variable into which the filesystem
223264311Seric **			block size is stored.
223358082Seric **
223458082Seric **	Returns:
223564311Seric **		The number of bytes free on the queue filesystem.
223664311Seric **		-1 if the statfs call fails.
223764311Seric **
223864311Seric **	Side effects:
223964311Seric **		Puts the filesystem block size into bsize.
224058082Seric */
224158082Seric 
224265749Seric /* statfs types */
224365749Seric #define SFS_NONE	0	/* no statfs implementation */
224465749Seric #define SFS_USTAT	1	/* use ustat */
224565749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
224665749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
224765749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
224865749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
224966752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
225065749Seric 
225165749Seric #ifndef SFS_TYPE
225265749Seric # define SFS_TYPE	SFS_NONE
225358157Seric #endif
225458157Seric 
225565749Seric #if SFS_TYPE == SFS_USTAT
225658157Seric # include <ustat.h>
225758157Seric #endif
225865749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
225965749Seric # include <sys/statfs.h>
226058082Seric #endif
226165749Seric #if SFS_TYPE == SFS_VFS
226265749Seric # include <sys/vfs.h>
226365749Seric #endif
226465749Seric #if SFS_TYPE == SFS_MOUNT
226565749Seric # include <sys/mount.h>
226665749Seric #endif
226766752Seric #if SFS_TYPE == SFS_STATVFS
226866752Seric # include <sys/statvfs.h>
226966752Seric #endif
227058082Seric 
227164311Seric long
227264311Seric freespace(dir, bsize)
227364311Seric 	char *dir;
227464311Seric 	long *bsize;
227558082Seric {
227665749Seric #if SFS_TYPE != SFS_NONE
227765749Seric # if SFS_TYPE == SFS_USTAT
227858153Seric 	struct ustat fs;
227958153Seric 	struct stat statbuf;
228058366Seric #  define FSBLOCKSIZE	DEV_BSIZE
228168695Seric #  define SFS_BAVAIL	f_tfree
228258157Seric # else
228358157Seric #  if defined(ultrix)
228458157Seric 	struct fs_data fs;
228568695Seric #   define SFS_BAVAIL	fd_bfreen
228668508Seric #   define FSBLOCKSIZE	1024L
228758153Seric #  else
228866752Seric #   if SFS_TYPE == SFS_STATVFS
228966752Seric 	struct statvfs fs;
229068508Seric #    define FSBLOCKSIZE	fs.f_frsize
229166752Seric #   else
229258082Seric 	struct statfs fs;
229366752Seric #    define FSBLOCKSIZE	fs.f_bsize
229463987Seric #   endif
229558153Seric #  endif
229658133Seric # endif
229768695Seric # ifndef SFS_BAVAIL
229868695Seric #  define SFS_BAVAIL f_bavail
229968508Seric # endif
230058082Seric 
230165749Seric # if SFS_TYPE == SFS_USTAT
230264311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
230358157Seric # else
230465749Seric #  if SFS_TYPE == SFS_4ARGS
230564311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
230658157Seric #  else
230768508Seric #   if SFS_TYPE == SFS_STATVFS
230868508Seric 	if (statvfs(dir, &fs) == 0)
230968508Seric #   else
231068508Seric #    if defined(ultrix)
231168499Seric 	if (statfs(dir, &fs) > 0)
231268508Seric #    else
231364311Seric 	if (statfs(dir, &fs) == 0)
231468508Seric #    endif
231558153Seric #   endif
231658133Seric #  endif
231758133Seric # endif
231858133Seric 	{
231964311Seric 		if (bsize != NULL)
232064311Seric 			*bsize = FSBLOCKSIZE;
232168695Seric 		return (fs.SFS_BAVAIL);
232264311Seric 	}
232364337Seric #endif
232464311Seric 	return (-1);
232564311Seric }
232664311Seric /*
232764311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
232864311Seric **
232964311Seric **	Only implemented if you have statfs.
233064311Seric **
233164311Seric **	Parameters:
233264311Seric **		msize -- the size to check against.  If zero, we don't yet
233364311Seric **		know how big the message will be, so just check for
233464311Seric **		a "reasonable" amount.
233564311Seric **
233664311Seric **	Returns:
233764311Seric **		TRUE if there is enough space.
233864311Seric **		FALSE otherwise.
233964311Seric */
234064311Seric 
234164311Seric bool
234264311Seric enoughspace(msize)
234364311Seric 	long msize;
234464311Seric {
234564311Seric 	long bfree, bsize;
234664311Seric 
234764311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
234864311Seric 	{
234958133Seric 		if (tTd(4, 80))
235064311Seric 			printf("enoughspace: no threshold\n");
235164311Seric 		return TRUE;
235264311Seric 	}
235364311Seric 
235464311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
235564311Seric 	{
235664311Seric 		if (tTd(4, 80))
235758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
235864311Seric 				bfree, msize);
235958333Seric 
236058333Seric 		/* convert msize to block count */
236164311Seric 		msize = msize / bsize + 1;
236258333Seric 		if (MinBlocksFree >= 0)
236358333Seric 			msize += MinBlocksFree;
236458333Seric 
236564311Seric 		if (bfree < msize)
236658090Seric 		{
236758090Seric #ifdef LOG
236858090Seric 			if (LogLevel > 0)
236964254Seric 				syslog(LOG_ALERT,
237064254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
237168508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
237268508Seric 					bfree,
237368508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
237468508Seric 					msize, QueueDir);
237558090Seric #endif
237658082Seric 			return FALSE;
237758090Seric 		}
237858082Seric 	}
237958082Seric 	else if (tTd(4, 80))
238058333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
238158333Seric 			MinBlocksFree, msize, errstring(errno));
238258082Seric 	return TRUE;
238358082Seric }
238458542Seric /*
238558542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
238658542Seric **
238758542Seric **	This looks at an errno value and tells if this is likely to
238858542Seric **	go away if retried later.
238958542Seric **
239058542Seric **	Parameters:
239158542Seric **		err -- the errno code to classify.
239258542Seric **
239358542Seric **	Returns:
239458542Seric **		TRUE if this is probably transient.
239558542Seric **		FALSE otherwise.
239658542Seric */
239758542Seric 
239858542Seric bool
239958542Seric transienterror(err)
240058542Seric 	int err;
240158542Seric {
240258542Seric 	switch (err)
240358542Seric 	{
240458542Seric 	  case EIO:			/* I/O error */
240558542Seric 	  case ENXIO:			/* Device not configured */
240658542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
240758542Seric 	  case ENOMEM:			/* Cannot allocate memory */
240858542Seric 	  case ENODEV:			/* Operation not supported by device */
240958542Seric 	  case ENFILE:			/* Too many open files in system */
241058542Seric 	  case EMFILE:			/* Too many open files */
241158542Seric 	  case ENOSPC:			/* No space left on device */
241258542Seric #ifdef ETIMEDOUT
241358542Seric 	  case ETIMEDOUT:		/* Connection timed out */
241458542Seric #endif
241558542Seric #ifdef ESTALE
241658542Seric 	  case ESTALE:			/* Stale NFS file handle */
241758542Seric #endif
241858542Seric #ifdef ENETDOWN
241958542Seric 	  case ENETDOWN:		/* Network is down */
242058542Seric #endif
242158542Seric #ifdef ENETUNREACH
242258542Seric 	  case ENETUNREACH:		/* Network is unreachable */
242358542Seric #endif
242458542Seric #ifdef ENETRESET
242558542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
242658542Seric #endif
242758542Seric #ifdef ECONNABORTED
242858542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
242958542Seric #endif
243058542Seric #ifdef ECONNRESET
243158542Seric 	  case ECONNRESET:		/* Connection reset by peer */
243258542Seric #endif
243358542Seric #ifdef ENOBUFS
243458542Seric 	  case ENOBUFS:			/* No buffer space available */
243558542Seric #endif
243658542Seric #ifdef ESHUTDOWN
243758542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
243858542Seric #endif
243958542Seric #ifdef ECONNREFUSED
244058542Seric 	  case ECONNREFUSED:		/* Connection refused */
244158542Seric #endif
244258542Seric #ifdef EHOSTDOWN
244358542Seric 	  case EHOSTDOWN:		/* Host is down */
244458542Seric #endif
244558542Seric #ifdef EHOSTUNREACH
244658542Seric 	  case EHOSTUNREACH:		/* No route to host */
244758542Seric #endif
244858542Seric #ifdef EDQUOT
244958542Seric 	  case EDQUOT:			/* Disc quota exceeded */
245058542Seric #endif
245158542Seric #ifdef EPROCLIM
245258542Seric 	  case EPROCLIM:		/* Too many processes */
245358542Seric #endif
245458542Seric #ifdef EUSERS
245558542Seric 	  case EUSERS:			/* Too many users */
245658542Seric #endif
245758542Seric #ifdef EDEADLK
245858542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
245958542Seric #endif
246058542Seric #ifdef EISCONN
246158542Seric 	  case EISCONN:			/* Socket already connected */
246258542Seric #endif
246358542Seric #ifdef EINPROGRESS
246458542Seric 	  case EINPROGRESS:		/* Operation now in progress */
246558542Seric #endif
246658542Seric #ifdef EALREADY
246758542Seric 	  case EALREADY:		/* Operation already in progress */
246858542Seric #endif
246958542Seric #ifdef EADDRINUSE
247058542Seric 	  case EADDRINUSE:		/* Address already in use */
247158542Seric #endif
247258542Seric #ifdef EADDRNOTAVAIL
247358542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
247458542Seric #endif
247566018Seric #ifdef ETXTBSY
247666017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
247766017Seric #endif
247863834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
247958542Seric 	  case ENOSR:			/* Out of streams resources */
248058542Seric #endif
248158542Seric 		return TRUE;
248258542Seric 	}
248358542Seric 
248458542Seric 	/* nope, must be permanent */
248558542Seric 	return FALSE;
248658542Seric }
248758689Seric /*
248864035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
248958689Seric **
249058689Seric **	Parameters:
249158689Seric **		fd -- the file descriptor of the file.
249258689Seric **		filename -- the file name (for error messages).
249364335Seric **		ext -- the filename extension.
249458689Seric **		type -- type of the lock.  Bits can be:
249558689Seric **			LOCK_EX -- exclusive lock.
249658689Seric **			LOCK_NB -- non-blocking.
249758689Seric **
249858689Seric **	Returns:
249958689Seric **		TRUE if the lock was acquired.
250058689Seric **		FALSE otherwise.
250158689Seric */
250258689Seric 
250358689Seric bool
250464335Seric lockfile(fd, filename, ext, type)
250558689Seric 	int fd;
250658689Seric 	char *filename;
250764335Seric 	char *ext;
250858689Seric 	int type;
250958689Seric {
251065830Seric # if !HASFLOCK
251158689Seric 	int action;
251258689Seric 	struct flock lfd;
251364335Seric 
251464335Seric 	if (ext == NULL)
251564335Seric 		ext = "";
251669472Seric 
251763983Seric 	bzero(&lfd, sizeof lfd);
251859447Seric 	if (bitset(LOCK_UN, type))
251959447Seric 		lfd.l_type = F_UNLCK;
252059447Seric 	else if (bitset(LOCK_EX, type))
252158689Seric 		lfd.l_type = F_WRLCK;
252258689Seric 	else
252358689Seric 		lfd.l_type = F_RDLCK;
252458689Seric 
252558689Seric 	if (bitset(LOCK_NB, type))
252658689Seric 		action = F_SETLK;
252758689Seric 	else
252858689Seric 		action = F_SETLKW;
252958689Seric 
253064264Seric 	if (tTd(55, 60))
253164335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
253264335Seric 			filename, ext, action, lfd.l_type);
253364264Seric 
253458689Seric 	if (fcntl(fd, action, &lfd) >= 0)
253564264Seric 	{
253664264Seric 		if (tTd(55, 60))
253764264Seric 			printf("SUCCESS\n");
253858689Seric 		return TRUE;
253964264Seric 	}
254058689Seric 
254164264Seric 	if (tTd(55, 60))
254264264Seric 		printf("(%s) ", errstring(errno));
254364264Seric 
254464136Seric 	/*
254564136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
254664136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
254764136Seric 	**  as type "tmp" (that is, served from swap space), the
254864136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
254964136Seric 	**  Since this is fairly common during testing, we will assume
255064136Seric 	**  that this indicates that the lock is successfully grabbed.
255164136Seric 	*/
255264136Seric 
255364136Seric 	if (errno == EINVAL)
255464264Seric 	{
255564264Seric 		if (tTd(55, 60))
255664264Seric 			printf("SUCCESS\n");
255764136Seric 		return TRUE;
255864264Seric 	}
255964136Seric 
256058689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
256164378Seric 	{
256264378Seric 		int omode = -1;
256364378Seric #  ifdef F_GETFL
256464378Seric 		int oerrno = errno;
256564378Seric 
256664378Seric 		(void) fcntl(fd, F_GETFL, &omode);
256764378Seric 		errno = oerrno;
256864378Seric #  endif
256964378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
257064378Seric 			filename, ext, fd, type, omode, geteuid());
257164378Seric 	}
257258689Seric # else
257364335Seric 	if (ext == NULL)
257464335Seric 		ext = "";
257564335Seric 
257664264Seric 	if (tTd(55, 60))
257764335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
257864264Seric 
257958689Seric 	if (flock(fd, type) >= 0)
258064264Seric 	{
258164264Seric 		if (tTd(55, 60))
258264264Seric 			printf("SUCCESS\n");
258358689Seric 		return TRUE;
258464264Seric 	}
258558689Seric 
258664264Seric 	if (tTd(55, 60))
258764264Seric 		printf("(%s) ", errstring(errno));
258864264Seric 
258958689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
259064378Seric 	{
259164378Seric 		int omode = -1;
259264378Seric #  ifdef F_GETFL
259364378Seric 		int oerrno = errno;
259464378Seric 
259564378Seric 		(void) fcntl(fd, F_GETFL, &omode);
259664378Seric 		errno = oerrno;
259764378Seric #  endif
259864378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
259964378Seric 			filename, ext, fd, type, omode, geteuid());
260064386Seric 	}
260158689Seric # endif
260264264Seric 	if (tTd(55, 60))
260364264Seric 		printf("FAIL\n");
260458689Seric 	return FALSE;
260558689Seric }
260664556Seric /*
260765948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
260865948Seric **
260965948Seric **	Parameters:
261065948Seric **		fd -- the file descriptor to check.
261165948Seric **
261265948Seric **	Returns:
261365948Seric **		TRUE -- if only root can chown the file to an arbitrary
261465948Seric **			user.
261565948Seric **		FALSE -- if an arbitrary user can give away a file.
261665948Seric */
261765948Seric 
261865948Seric bool
261965948Seric chownsafe(fd)
262065948Seric 	int fd;
262165948Seric {
262265950Seric #ifdef __hpux
262365948Seric 	char *s;
262465948Seric 	int tfd;
262565948Seric 	uid_t o_uid, o_euid;
262665948Seric 	gid_t o_gid, o_egid;
262765948Seric 	bool rval;
262865948Seric 	struct stat stbuf;
262965948Seric 
263065948Seric 	o_uid = getuid();
263165948Seric 	o_euid = geteuid();
263265948Seric 	o_gid = getgid();
263365948Seric 	o_egid = getegid();
263465959Seric 	fstat(fd, &stbuf);
263565948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
263665948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
263765948Seric 	s = tmpnam(NULL);
263865948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
263965974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
264065948Seric 	close(tfd);
264165948Seric 	unlink(s);
264269638Seric 	setresuid(o_uid, o_euid, -1);
264365948Seric 	setresgid(o_gid, o_egid, -1);
264465948Seric 	return rval;
264565948Seric #else
264666088Seric # ifdef _POSIX_CHOWN_RESTRICTED
264766088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
264866088Seric 	return FALSE;
264966088Seric #  else
265066088Seric 	return TRUE;
265166088Seric #  endif
265266088Seric # else
265366088Seric #  ifdef _PC_CHOWN_RESTRICTED
265468508Seric 	int rval;
265568508Seric 
265668508Seric 	/*
265768508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
265868508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
265968508Seric 	**  the call.  This heuristic checks for that.
266068508Seric 	*/
266168508Seric 
266268508Seric 	errno = 0;
266368508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
266468508Seric 	if (errno == 0)
266568508Seric 		return rval > 0;
266668508Seric #  endif
266768508Seric #  ifdef BSD
266868508Seric 	return TRUE;
266968499Seric #  else
267065948Seric 	return FALSE;
267165948Seric #  endif
267265948Seric # endif
267365948Seric #endif
267465948Seric }
267565948Seric /*
267668508Seric **  RESETLIMITS -- reset system controlled resource limits
267768508Seric **
267868508Seric **	This is to avoid denial-of-service attacks
267968508Seric **
268068508Seric **	Parameters:
268168508Seric **		none
268268508Seric **
268368508Seric **	Returns:
268468508Seric **		none
268568508Seric */
268668508Seric 
268768508Seric #if HASSETRLIMIT
268868508Seric # include <sys/resource.h>
268968508Seric #endif
269068508Seric 
269168508Seric void
269268508Seric resetlimits()
269368508Seric {
269468508Seric #if HASSETRLIMIT
269568508Seric 	struct rlimit lim;
269668508Seric 
269768508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
269868508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
269968508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
270068508Seric #else
270168508Seric # if HASULIMIT
270268508Seric 	(void) ulimit(2, 0x3fffff);
270368508Seric # endif
270468508Seric #endif
270568508Seric }
270668508Seric /*
270764556Seric **  GETCFNAME -- return the name of the .cf file.
270864556Seric **
270964556Seric **	Some systems (e.g., NeXT) determine this dynamically.
271064556Seric */
271164556Seric 
271264556Seric char *
271364556Seric getcfname()
271464556Seric {
271568842Seric 	int i;
271668842Seric 	static char cbuf[200];
271768842Seric 
271864556Seric 	if (ConfFile != NULL)
271964556Seric 		return ConfFile;
272065214Seric #ifdef NETINFO
272165214Seric 	{
272265214Seric 		extern char *ni_propval();
272365214Seric 		char *cflocation;
272465214Seric 
272568508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
272668508Seric 					"sendmail.cf", '\0');
272765214Seric 		if (cflocation != NULL)
272865214Seric 			return cflocation;
272965214Seric 	}
273065214Seric #endif
273168842Seric 
273269396Seric #ifdef TRY_VERSIONED_CF_NAME
273368842Seric 	/*
273468842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
273568842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
273668842Seric 	**
273768842Seric 	**	I suppose it should really try a search path here --
273868842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
273968842Seric 	**	/usr/lib/sendmail.cf, and so forth.
274068842Seric 	*/
274168842Seric 
274268842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
274368842Seric 	i = strlen(cbuf);
274468842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
274568842Seric 	{
274668842Seric 		char *p;
274768842Seric 		extern char Version[];
274868842Seric 
274968842Seric 		strcpy(&cbuf[i - 2], Version);
275068842Seric 		p = strchr(&cbuf[i - 2], '/');
275168842Seric 		if (p != NULL)
275268842Seric 			*p = '\0';
275368842Seric 		p = &cbuf[strlen(cbuf)];
275468842Seric 		do
275568842Seric 		{
275668842Seric 			int fd;
275768842Seric 
275868842Seric 			strcpy(p, ".cf");
275968842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
276068842Seric 			{
276168842Seric 				close(fd);
276268842Seric 				return cbuf;
276368842Seric 			}
276468842Seric 			*p = '\0';
276568842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
276668842Seric 	}
276769396Seric #endif
276864556Seric 	return _PATH_SENDMAILCF;
276964556Seric }
277064718Seric /*
277164718Seric **  SETVENDOR -- process vendor code from V configuration line
277264718Seric **
277364718Seric **	Parameters:
277464718Seric **		vendor -- string representation of vendor.
277564718Seric **
277664718Seric **	Returns:
277764718Seric **		TRUE -- if ok.
277864718Seric **		FALSE -- if vendor code could not be processed.
277964926Seric **
278064926Seric **	Side Effects:
278164926Seric **		It is reasonable to set mode flags here to tweak
278264926Seric **		processing in other parts of the code if necessary.
278364926Seric **		For example, if you are a vendor that uses $%y to
278464926Seric **		indicate YP lookups, you could enable that here.
278564718Seric */
278664718Seric 
278764718Seric bool
278864718Seric setvendor(vendor)
278964718Seric 	char *vendor;
279064718Seric {
279164926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
279268508Seric 	{
279368508Seric 		VendorCode = VENDOR_BERKELEY;
279464926Seric 		return TRUE;
279568508Seric 	}
279664926Seric 
279764926Seric 	/* add vendor extensions here */
279864926Seric 
279968508Seric #ifdef SUN_EXTENSIONS
280068508Seric 	if (strcasecmp(vendor, "Sun") == 0)
280168508Seric 	{
280268508Seric 		VendorCode = VENDOR_SUN;
280368508Seric 		return TRUE;
280468508Seric 	}
280568508Seric #endif
280668508Seric 
280764926Seric 	return FALSE;
280864718Seric }
280964816Seric /*
281069510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
281169510Seric **
281269510Seric **	Vendor_pre_defaults is called before reading the configuration
281369510Seric **	file; vendor_post_defaults is called immediately after.
281469510Seric **
281569510Seric **	Parameters:
281669510Seric **		e -- the global environment to initialize.
281769510Seric **
281869510Seric **	Returns:
281969510Seric **		none.
282069510Seric */
282169510Seric 
282269510Seric void
282369510Seric vendor_pre_defaults(e)
282469510Seric 	ENVELOPE *e;
282569510Seric {
282669510Seric }
282769510Seric 
282869510Seric 
282969510Seric void
283069510Seric vendor_post_defaults(e)
283169510Seric 	ENVELOPE *e;
283269510Seric {
283369510Seric }
283469510Seric /*
283564816Seric **  STRTOL -- convert string to long integer
283664816Seric **
283764816Seric **	For systems that don't have it in the C library.
283866161Seric **
283966161Seric **	This is taken verbatim from the 4.4-Lite C library.
284064816Seric */
284164816Seric 
284264816Seric #ifdef NEEDSTRTOL
284364816Seric 
284466161Seric #if defined(LIBC_SCCS) && !defined(lint)
284566161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
284666161Seric #endif /* LIBC_SCCS and not lint */
284766161Seric 
284866161Seric #include <limits.h>
284966161Seric 
285066161Seric /*
285166161Seric  * Convert a string to a long integer.
285266161Seric  *
285366161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
285466161Seric  * alphabets and digits are each contiguous.
285566161Seric  */
285666161Seric 
285764816Seric long
285866161Seric strtol(nptr, endptr, base)
285966161Seric 	const char *nptr;
286066161Seric 	char **endptr;
286166161Seric 	register int base;
286264816Seric {
286366161Seric 	register const char *s = nptr;
286466161Seric 	register unsigned long acc;
286566161Seric 	register int c;
286666161Seric 	register unsigned long cutoff;
286766161Seric 	register int neg = 0, any, cutlim;
286864816Seric 
286966161Seric 	/*
287066161Seric 	 * Skip white space and pick up leading +/- sign if any.
287166161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
287266161Seric 	 * assume decimal; if base is already 16, allow 0x.
287366161Seric 	 */
287466161Seric 	do {
287566161Seric 		c = *s++;
287666161Seric 	} while (isspace(c));
287766161Seric 	if (c == '-') {
287866161Seric 		neg = 1;
287966161Seric 		c = *s++;
288066161Seric 	} else if (c == '+')
288166161Seric 		c = *s++;
288266161Seric 	if ((base == 0 || base == 16) &&
288366161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
288466161Seric 		c = s[1];
288566161Seric 		s += 2;
288666161Seric 		base = 16;
288766161Seric 	}
288866161Seric 	if (base == 0)
288966161Seric 		base = c == '0' ? 8 : 10;
289064816Seric 
289166161Seric 	/*
289266161Seric 	 * Compute the cutoff value between legal numbers and illegal
289366161Seric 	 * numbers.  That is the largest legal value, divided by the
289466161Seric 	 * base.  An input number that is greater than this value, if
289566161Seric 	 * followed by a legal input character, is too big.  One that
289666161Seric 	 * is equal to this value may be valid or not; the limit
289766161Seric 	 * between valid and invalid numbers is then based on the last
289866161Seric 	 * digit.  For instance, if the range for longs is
289966161Seric 	 * [-2147483648..2147483647] and the input base is 10,
290066161Seric 	 * cutoff will be set to 214748364 and cutlim to either
290166161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
290266161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
290366161Seric 	 * the number is too big, and we will return a range error.
290466161Seric 	 *
290566161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
290666161Seric 	 * overflow.
290766161Seric 	 */
290866161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
290966161Seric 	cutlim = cutoff % (unsigned long)base;
291066161Seric 	cutoff /= (unsigned long)base;
291166161Seric 	for (acc = 0, any = 0;; c = *s++) {
291266161Seric 		if (isdigit(c))
291366161Seric 			c -= '0';
291466161Seric 		else if (isalpha(c))
291566161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
291666161Seric 		else
291766161Seric 			break;
291866161Seric 		if (c >= base)
291966161Seric 			break;
292066161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
292166161Seric 			any = -1;
292266161Seric 		else {
292366161Seric 			any = 1;
292466161Seric 			acc *= base;
292566161Seric 			acc += c;
292664816Seric 		}
292764816Seric 	}
292866161Seric 	if (any < 0) {
292966161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
293066161Seric 		errno = ERANGE;
293166161Seric 	} else if (neg)
293266161Seric 		acc = -acc;
293366161Seric 	if (endptr != 0)
293466161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
293566161Seric 	return (acc);
293664816Seric }
293764816Seric 
293864816Seric #endif
293964841Seric /*
294068693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
294164841Seric **
294268693Seric **	Some operating systems have wierd problems with the gethostbyXXX
294368693Seric **	routines.  For example, Solaris versions at least through 2.3
294468693Seric **	don't properly deliver a canonical h_name field.  This tries to
294568693Seric **	work around these problems.
294664841Seric */
294764841Seric 
294864841Seric struct hostent *
294968693Seric sm_gethostbyname(name)
295068707Seric 	char *name;
295164841Seric {
2952*69654Seric #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4)
295368693Seric 	extern int h_errno;
295468693Seric 
295568508Seric # if SOLARIS == 203
295664841Seric 	static struct hostent hp;
295764841Seric 	static char buf[1000];
295864841Seric 	extern struct hostent *_switch_gethostbyname_r();
295964841Seric 
296064841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
296164841Seric # else
296265009Seric 	extern struct hostent *__switch_gethostbyname();
296365009Seric 
296464841Seric 	return __switch_gethostbyname(name);
296564841Seric # endif
296668693Seric #else
296768693Seric 	return gethostbyname(name);
296868693Seric #endif
296964841Seric }
297064841Seric 
297164841Seric struct hostent *
297268693Seric sm_gethostbyaddr(addr, len, type)
297368707Seric 	char *addr;
297464841Seric 	int len;
297564841Seric 	int type;
297664841Seric {
297768693Seric #if defined(SOLARIS) && SOLARIS < 204
297868693Seric 	extern int h_errno;
297968693Seric 
298068508Seric # if SOLARIS == 203
298164841Seric 	static struct hostent hp;
298264841Seric 	static char buf[1000];
298364841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
298464841Seric 
298564841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
298664841Seric # else
298765009Seric 	extern struct hostent *__switch_gethostbyaddr();
298865009Seric 
298964841Seric 	return __switch_gethostbyaddr(addr, len, type);
299064841Seric # endif
299168693Seric #else
299268693Seric 	return gethostbyaddr(addr, len, type);
299368693Seric #endif
299464841Seric }
299568693Seric /*
299668693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
299768693Seric */
299864841Seric 
299968693Seric struct passwd *
300068693Seric sm_getpwnam(user)
300168704Seric 	char *user;
300268693Seric {
300368693Seric 	return getpwnam(user);
300468693Seric }
300568693Seric 
300668693Seric struct passwd *
300768693Seric sm_getpwuid(uid)
300868693Seric 	uid_t uid;
300968693Seric {
301068693Seric 	return getpwuid(uid);
301168693Seric }
301265214Seric /*
301369472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
301469472Seric **
301569472Seric **	Parameters:
301669472Seric **		none.
301769472Seric **
301869472Seric **	Returns:
301969472Seric **		none.
302069472Seric **
302169472Seric **	Side Effects:
302269472Seric **		Loads $=w with the names of all the interfaces.
302369472Seric */
302469472Seric 
302569472Seric #ifdef SIOCGIFCONF
302669565Seric struct rtentry;
302769565Seric struct mbuf;
302869472Seric # include <arpa/inet.h>
302969562Seric # include <sys/time.h>
303069472Seric # include <net/if.h>
303169472Seric #endif
303269472Seric 
303369472Seric void
303469472Seric load_if_names()
303569472Seric {
303669472Seric #ifdef SIOCGIFCONF
303769472Seric 	int s;
303869516Seric 	int i;
303969472Seric         struct ifconf ifc;
304069472Seric 	char interfacebuf[1024];
304169472Seric 
304269472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
304369472Seric 	if (s == -1)
304469472Seric 		return;
304569472Seric 
304669472Seric 	/* get the list of known IP address from the kernel */
304769472Seric         ifc.ifc_buf = interfacebuf;
304869516Seric         ifc.ifc_len = sizeof interfacebuf;
304969472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
305069472Seric 	{
305169472Seric 		if (tTd(0, 4))
305269472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
305369516Seric 		close(s);
305469472Seric 		return;
305569472Seric 	}
305669516Seric 	close(s);
305769472Seric 
305869472Seric 	/* scan the list of IP address */
305969472Seric 	if (tTd(0, 4))
306069472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
306169472Seric 			ifc.ifc_len);
306269472Seric 
306369516Seric 	for (i = 0; i < ifc.ifc_len; )
306469516Seric 	{
306569516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
306669516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
306769472Seric 		struct in_addr ia;
306869516Seric 		struct hostent *hp;
306969472Seric 		char ip_addr[256];
307069516Seric 		extern char *inet_ntoa();
307169516Seric 		extern struct hostent *gethostbyaddr();
307269472Seric 
307369516Seric #ifdef BSD4_4_SOCKADDR
307469516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
307569516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
307669516Seric 		else
307769516Seric #endif
307869516Seric 			i += sizeof *ifr;
307969516Seric 
308069472Seric 		if (tTd(0, 20))
308169516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
308269472Seric 
308369472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
308469472Seric 			continue;
308569472Seric 
308669472Seric 		/* extract IP address from the list*/
308769516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
308869472Seric 
308969472Seric 		/* save IP address in text from */
309069472Seric 		(void) sprintf(ip_addr, "[%s]",
309169516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
309269472Seric 		if (!wordinclass(ip_addr, 'w'))
309369472Seric 		{
309469472Seric 			setclass('w', ip_addr);
309569472Seric 			if (tTd(0, 4))
309669472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
309769472Seric 		}
309869472Seric 
309969472Seric 		/* skip "loopback" interface "lo" */
310069472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
310169472Seric 			continue;
310269472Seric 
310369472Seric 		/* lookup name with IP address */
310469472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
310569472Seric 		if (hp == NULL)
310669472Seric 		{
310769472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
310869472Seric 				inet_ntoa(ia));
310969472Seric 			continue;
311069472Seric 		}
311169472Seric 
311269472Seric 		/* save its cname */
311369472Seric 		if (!wordinclass(hp->h_name, 'w'))
311469472Seric 		{
311569472Seric 			setclass('w', hp->h_name);
311669472Seric 			if (tTd(0, 4))
311769472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
311869472Seric 		}
311969472Seric 
312069472Seric 		/* save all it aliases name */
312169472Seric 		while (*hp->h_aliases)
312269472Seric 		{
312369472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
312469472Seric 			{
312569472Seric 				setclass('w', *hp->h_aliases);
312669472Seric 				if (tTd(0, 4))
312769472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
312869472Seric 			}
312969472Seric 			hp->h_aliases++;
313069472Seric 		}
313169472Seric 	}
313269472Seric #endif
313369472Seric }
313469472Seric /*
313565214Seric **  NI_PROPVAL -- netinfo property value lookup routine
313665214Seric **
313765214Seric **	Parameters:
313868508Seric **		keydir -- the Netinfo directory name in which to search
313968508Seric **			for the key.
314068508Seric **		keyprop -- the name of the property in which to find the
314168508Seric **			property we are interested.  Defaults to "name".
314268508Seric **		keyval -- the value for which we are really searching.
314368508Seric **		valprop -- the property name for the value in which we
314468508Seric **			are interested.
314568508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
314668508Seric **			we should return a string separated by this
314768508Seric **			character.
314865214Seric **
314965214Seric **	Returns:
315065214Seric **		NULL -- if:
315165214Seric **			1. the directory is not found
315265214Seric **			2. the property name is not found
315365214Seric **			3. the property contains multiple values
315465214Seric **			4. some error occured
315565214Seric **		else -- the location of the config file.
315665214Seric **
315768508Seric **	Example:
315868508Seric **		To search for an alias value, use:
315968508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
316068508Seric **
316165214Seric **	Notes:
316265214Seric **      	Caller should free the return value of ni_proval
316365214Seric */
316465214Seric 
316565214Seric #ifdef NETINFO
316665214Seric 
316765214Seric # include <netinfo/ni.h>
316865214Seric 
316965214Seric # define LOCAL_NETINFO_DOMAIN    "."
317065214Seric # define PARENT_NETINFO_DOMAIN   ".."
317165214Seric # define MAX_NI_LEVELS           256
317265214Seric 
317365214Seric char *
317468508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
317568508Seric 	char *keydir;
317668508Seric 	char *keyprop;
317768508Seric 	char *keyval;
317868508Seric 	char *valprop;
317968508Seric 	char sepchar;
318065214Seric {
318165820Seric 	char *propval = NULL;
318265214Seric 	int i;
318368508Seric 	int j, alen;
318465214Seric 	void *ni = NULL;
318565214Seric 	void *lastni = NULL;
318665214Seric 	ni_status nis;
318765214Seric 	ni_id nid;
318865214Seric 	ni_namelist ninl;
318968508Seric 	register char *p;
319068508Seric 	char keybuf[1024];
319165214Seric 
319265214Seric 	/*
319368508Seric 	**  Create the full key from the two parts.
319468508Seric 	**
319568508Seric 	**	Note that directory can end with, e.g., "name=" to specify
319668508Seric 	**	an alternate search property.
319768508Seric 	*/
319868508Seric 
319968508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
320068508Seric 	if (keyprop != NULL)
320168508Seric 		i += strlen(keyprop) + 1;
320268508Seric 	if (i > sizeof keybuf)
320368508Seric 		return NULL;
320468508Seric 	strcpy(keybuf, keydir);
320568508Seric 	strcat(keybuf, "/");
320668508Seric 	if (keyprop != NULL)
320768508Seric 	{
320868508Seric 		strcat(keybuf, keyprop);
320968508Seric 		strcat(keybuf, "=");
321068508Seric 	}
321168508Seric 	strcat(keybuf, keyval);
321268508Seric 
321368508Seric 	/*
321465214Seric 	**  If the passed directory and property name are found
321565214Seric 	**  in one of netinfo domains we need to search (starting
321665214Seric 	**  from the local domain moving all the way back to the
321765214Seric 	**  root domain) set propval to the property's value
321865214Seric 	**  and return it.
321965214Seric 	*/
322065214Seric 
322165214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
322265214Seric 	{
322365214Seric 		if (i == 0)
322465214Seric 		{
322565214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
322665214Seric 		}
322765214Seric 		else
322865214Seric 		{
322965214Seric 			if (lastni != NULL)
323065214Seric 				ni_free(lastni);
323165214Seric 			lastni = ni;
323265214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
323365214Seric 		}
323465214Seric 
323565214Seric 		/*
323665214Seric 		**  Don't bother if we didn't get a handle on a
323765214Seric 		**  proper domain.  This is not necessarily an error.
323865214Seric 		**  We would get a positive ni_status if, for instance
323965214Seric 		**  we never found the directory or property and tried
324065214Seric 		**  to open the parent of the root domain!
324165214Seric 		*/
324265214Seric 
324365214Seric 		if (nis != 0)
324465214Seric 			break;
324565214Seric 
324665214Seric 		/*
324765214Seric 		**  Find the path to the server information.
324865214Seric 		*/
324965214Seric 
325068508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
325165214Seric 			continue;
325265214Seric 
325365214Seric 		/*
325468508Seric 		**  Find associated value information.
325565214Seric 		*/
325665214Seric 
325768508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
325865214Seric 			continue;
325965214Seric 
326065214Seric 		/*
326168508Seric 		**  See if we have an acceptable number of values.
326265214Seric 		*/
326365214Seric 
326468508Seric 		if (ninl.ni_namelist_len <= 0)
326568508Seric 			continue;
326668508Seric 
326768508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
326865214Seric 		{
326968508Seric 			ni_namelist_free(&ninl);
327068508Seric 			continue;
327165214Seric 		}
327268508Seric 
327369472Seric 		/*
327468508Seric 		**  Calculate number of bytes needed and build result
327568508Seric 		*/
327668508Seric 
327768508Seric 		alen = 1;
327868508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
327968508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
328068508Seric 		propval = p = xalloc(alen);
328168508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
328268508Seric 		{
328368508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
328468508Seric 			p += strlen(p);
328568508Seric 			*p++ = sepchar;
328669472Seric 		}
328768508Seric 		*--p = '\0';
328868508Seric 
328968508Seric 		ni_namelist_free(&ninl);
329065214Seric 	}
329165214Seric 
329265214Seric 	/*
329365214Seric 	**  Clean up.
329465214Seric 	*/
329565214Seric 
329665214Seric 	if (ni != NULL)
329765214Seric 		ni_free(ni);
329865214Seric 	if (lastni != NULL && ni != lastni)
329965214Seric 		ni_free(lastni);
330065214Seric 
330165214Seric 	return propval;
330265214Seric }
330365214Seric 
330465214Seric #endif /* NETINFO */
330565504Seric /*
330665504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
330765504Seric **
330865504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
330965504Seric **	syslog succeeds during interrupt handlers.
331065504Seric */
331165504Seric 
331265504Seric #ifdef __hpux
331365504Seric 
331465504Seric # define MAXSYSLOGTRIES	100
331565504Seric # undef syslog
331665504Seric 
331765504Seric # ifdef __STDC__
331865504Seric hard_syslog(int pri, char *msg, ...)
331965504Seric # else
332065504Seric hard_syslog(pri, msg, va_alist)
332165504Seric 	int pri;
332265504Seric 	char *msg;
332365504Seric 	va_dcl
332465504Seric # endif
332565504Seric {
332665504Seric 	int i;
332765504Seric 	char buf[SYSLOG_BUFSIZE * 2];
332865504Seric 	VA_LOCAL_DECL;
332965504Seric 
333065504Seric 	VA_START(msg);
333165504Seric 	vsprintf(buf, msg, ap);
333265504Seric 	VA_END;
333365504Seric 
333465504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
333565504Seric 		continue;
333665504Seric }
333765504Seric 
333865504Seric #endif
333968696Seric /*
334068696Seric **  LOCAL_HOSTNAME_LENGTH
334168696Seric **
334268696Seric **	This is required to get sendmail to compile against BIND 4.9.x
334368696Seric **	on Ultrix.
334468696Seric */
334568696Seric 
334668696Seric #if defined(ultrix) && NAMED_BIND
334768696Seric 
334868696Seric # include <resolv.h>
334968696Seric # if __RES >= 19931104
335068696Seric 
335168696Seric int
335268696Seric local_hostname_length(hostname)
335368696Seric 	char *hostname;
335468696Seric {
335568696Seric 	int len_host, len_domain;
335668696Seric 
335768696Seric 	if (!*_res.defdname)
335868696Seric 		res_init();
335968696Seric 	len_host = strlen(hostname);
336068696Seric 	len_domain = strlen(_res.defdname);
336168696Seric 	if (len_host > len_domain &&
336268696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
336368696Seric 	    hostname[len_host - len_domain - 1] == '.')
336468696Seric 		return len_host - len_domain - 1;
336568696Seric 	else
336668696Seric 		return 0;
336768696Seric }
336868696Seric 
336968696Seric # endif
337068696Seric #endif
337168741Seric /*
337268741Seric **  Compile-Time options
337368741Seric */
337468741Seric 
337568741Seric char	*CompileOptions[] =
337668741Seric {
337768741Seric #if HESIOD
337868741Seric 	"HESIOD",
337968741Seric #endif
338068741Seric #if LOG
338168741Seric 	"LOG",
338268741Seric #endif
338368741Seric #if MATCHGECOS
338468741Seric 	"MATCHGECOS",
338568741Seric #endif
338668741Seric #if NAMED_BIND
338768741Seric 	"NAMED_BIND",
338868741Seric #endif
338968741Seric #if NDBM
339068741Seric 	"NDBM",
339168741Seric #endif
339268741Seric #if NETINET
339368741Seric 	"NETINET",
339468741Seric #endif
339568741Seric #if NETINFO
339668741Seric 	"NETINFO",
339768741Seric #endif
339868741Seric #if NETISO
339968741Seric 	"NETISO",
340068741Seric #endif
340168741Seric #if NETNS
340268741Seric 	"NETNS",
340368741Seric #endif
340468741Seric #if NETUNIX
340568741Seric 	"NETUNIX",
340668741Seric #endif
340768741Seric #if NETX25
340868741Seric 	"NETX25",
340968741Seric #endif
341068741Seric #if NEWDB
341168741Seric 	"NEWDB",
341268741Seric #endif
341368741Seric #if NIS
341468741Seric 	"NIS",
341568741Seric #endif
341669518Seric #if NISPLUS
341769518Seric 	"NISPLUS",
341869518Seric #endif
341968741Seric #if SCANF
342068741Seric 	"SCANF",
342168741Seric #endif
342268741Seric #if SUID_ROOT_FILES_OK
342368741Seric 	"SUID_ROOT_FILES_OK",
342468741Seric #endif
342568741Seric #if USERDB
342668741Seric 	"USERDB",
342768741Seric #endif
342868741Seric #if XDEBUG
342968741Seric 	"XDEBUG",
343068741Seric #endif
343168741Seric #if XLA
343268741Seric 	"XLA",
343368741Seric #endif
343468741Seric 	NULL
343568741Seric };
3436