xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69462)
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*69462Seric static char sccsid[] = "@(#)conf.c	8.161 (Berkeley) 05/14/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 */
18768508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18868508Seric 	for (i = 0; i < MAXTOCLASS; i++)
18968508Seric 	{
19068508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19168508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19268508Seric 	}
19368508Seric 	ServiceSwitchFile = "/etc/service.switch";
19469401Seric 	HostsFile = _PATH_HOSTS;
19540973Sbostic 	setdefuser();
19653654Seric 	setupmaps();
19757402Seric 	setupmailers();
19824943Seric }
199294Seric 
20040973Sbostic 
2014326Seric /*
20240973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20340973Sbostic */
20440973Sbostic 
20568508Seric void
20640973Sbostic setdefuser()
20740973Sbostic {
20840973Sbostic 	struct passwd *defpwent;
20957386Seric 	static char defuserbuf[40];
21040973Sbostic 
21157386Seric 	DefUser = defuserbuf;
21268693Seric 	if ((defpwent = sm_getpwuid(DefUid)) != NULL)
21357386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21440973Sbostic 	else
21557386Seric 		strcpy(defuserbuf, "nobody");
21640973Sbostic }
21753654Seric /*
21856836Seric **  HOST_MAP_INIT -- initialize host class structures
21956836Seric */
22056836Seric 
22168508Seric bool	host_map_init __P((MAP *map, char *args));
22268508Seric 
22356836Seric bool
22460219Seric host_map_init(map, args)
22556836Seric 	MAP *map;
22656836Seric 	char *args;
22756836Seric {
22856836Seric 	register char *p = args;
22956836Seric 
23056836Seric 	for (;;)
23156836Seric 	{
23258050Seric 		while (isascii(*p) && isspace(*p))
23356836Seric 			p++;
23456836Seric 		if (*p != '-')
23556836Seric 			break;
23656836Seric 		switch (*++p)
23756836Seric 		{
23856836Seric 		  case 'a':
23956836Seric 			map->map_app = ++p;
24056836Seric 			break;
24156836Seric 		}
24258050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24356836Seric 			p++;
24456836Seric 		if (*p != '\0')
24556836Seric 			*p++ = '\0';
24656836Seric 	}
24756836Seric 	if (map->map_app != NULL)
24856836Seric 		map->map_app = newstr(map->map_app);
24956836Seric 	return TRUE;
25056836Seric }
25157402Seric /*
25257402Seric **  SETUPMAILERS -- initialize default mailers
25357402Seric */
25456836Seric 
25568508Seric void
25657402Seric setupmailers()
25757402Seric {
25857402Seric 	char buf[100];
25968508Seric 	extern void makemailer();
26057402Seric 
26168868Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=X-Unix, A=sh -c $u");
26257403Seric 	makemailer(buf);
26357403Seric 
26468880Seric 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=X-Unix, A=FILE");
26557402Seric 	makemailer(buf);
26657402Seric 
26757402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26857402Seric 	makemailer(buf);
26957402Seric }
27056836Seric /*
27160207Seric **  SETUPMAPS -- set up map classes
27260207Seric */
27360207Seric 
27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27560207Seric 	{ \
27660207Seric 		extern bool parse __P((MAP *, char *)); \
27760207Seric 		extern bool open __P((MAP *, int)); \
27860207Seric 		extern void close __P((MAP *)); \
27960207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
28060207Seric 		extern void store __P((MAP *, char *, char *)); \
28160207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28260207Seric 		s->s_mapclass.map_cname = name; \
28360207Seric 		s->s_mapclass.map_ext = ext; \
28460207Seric 		s->s_mapclass.map_cflags = flags; \
28560207Seric 		s->s_mapclass.map_parse = parse; \
28660207Seric 		s->s_mapclass.map_open = open; \
28760207Seric 		s->s_mapclass.map_close = close; \
28860207Seric 		s->s_mapclass.map_lookup = lookup; \
28960207Seric 		s->s_mapclass.map_store = store; \
29060207Seric 	}
29160207Seric 
29268508Seric void
29360207Seric setupmaps()
29460207Seric {
29560207Seric 	register STAB *s;
29660207Seric 
29760207Seric #ifdef NEWDB
29860207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29960207Seric 		map_parseargs, hash_map_open, db_map_close,
30060207Seric 		db_map_lookup, db_map_store);
30168508Seric 
30260207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, bt_map_open, db_map_close,
30460207Seric 		db_map_lookup, db_map_store);
30560207Seric #endif
30660207Seric 
30760207Seric #ifdef NDBM
30860207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30960207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
31060207Seric 		ndbm_map_lookup, ndbm_map_store);
31160207Seric #endif
31260207Seric 
31360207Seric #ifdef NIS
31460207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31568508Seric 		map_parseargs, nis_map_open, null_map_close,
31668508Seric 		nis_map_lookup, null_map_store);
31760207Seric #endif
31860207Seric 
31968508Seric #ifdef NISPLUS
32068508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32168508Seric 		map_parseargs, nisplus_map_open, null_map_close,
32268508Seric 		nisplus_map_lookup, null_map_store);
32368508Seric #endif
32468508Seric 
32568508Seric #ifdef HESIOD
32668508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
32768508Seric 		map_parseargs, null_map_open, null_map_close,
32868508Seric 		hes_map_lookup, null_map_store);
32968508Seric #endif
33068508Seric 
33168508Seric #ifdef NETINFO
33268508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33368508Seric 		map_parseargs, ni_map_open, null_map_close,
33468508Seric 		ni_map_lookup, null_map_store);
33568508Seric #endif
33668508Seric 
33768508Seric #if 0
33868508Seric 	MAPDEF("dns", NULL, 0,
33968508Seric 		dns_map_init, null_map_open, null_map_close,
34068508Seric 		dns_map_lookup, null_map_store);
34168508Seric #endif
34268508Seric 
34368508Seric #if NAMED_BIND
34468508Seric 	/* best MX DNS lookup */
34568508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
34668508Seric 		map_parseargs, null_map_open, null_map_close,
34768508Seric 		bestmx_map_lookup, null_map_store);
34868508Seric #endif
34968508Seric 
35068508Seric 	MAPDEF("host", NULL, 0,
35168508Seric 		host_map_init, null_map_open, null_map_close,
35268508Seric 		host_map_lookup, null_map_store);
35368508Seric 
35468508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35568508Seric 		map_parseargs, text_map_open, null_map_close,
35668508Seric 		text_map_lookup, null_map_store);
35768508Seric 
35860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
35968508Seric 		map_parseargs, stab_map_open, null_map_close,
36060207Seric 		stab_map_lookup, stab_map_store);
36160207Seric 
36260207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36360207Seric 		map_parseargs, impl_map_open, impl_map_close,
36460207Seric 		impl_map_lookup, impl_map_store);
36560207Seric 
36668508Seric 	/* access to system passwd file */
36768508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
36868508Seric 		map_parseargs, user_map_open, null_map_close,
36968508Seric 		user_map_lookup, null_map_store);
37060207Seric 
37160207Seric 	/* dequote map */
37260207Seric 	MAPDEF("dequote", NULL, 0,
37360207Seric 		dequote_init, null_map_open, null_map_close,
37460207Seric 		dequote_map, null_map_store);
37560207Seric 
37668521Seric #ifdef USERDB
37760207Seric 	/* user database */
37868521Seric 	MAPDEF("userdb", ".db", 0,
37968521Seric 		map_parseargs, null_map_open, null_map_close,
38060207Seric 		udb_map_lookup, null_map_store);
38160207Seric #endif
38268508Seric 
38369453Seric 	/* arbitrary programs */
38469453Seric 	MAPDEF("program", NULL, 0,
38569453Seric 		map_parseargs, null_map_open, null_map_close,
38669453Seric 		prog_map_lookup, null_map_store);
38769453Seric 
38868508Seric 	/* sequenced maps */
38968508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
39068508Seric 		seq_map_parse, null_map_open, null_map_close,
39168508Seric 		seq_map_lookup, seq_map_store);
39268508Seric 
39368508Seric 	/* switched interface to sequenced maps */
39468508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
39568508Seric 		map_parseargs, switch_map_open, null_map_close,
39668508Seric 		seq_map_lookup, seq_map_store);
39760207Seric }
39860207Seric 
39960207Seric #undef MAPDEF
40060207Seric /*
40168508Seric **  INITHOSTMAPS -- initial host-dependent maps
40268508Seric **
40368508Seric **	This should act as an interface to any local service switch
40468508Seric **	provided by the host operating system.
40568508Seric **
40668508Seric **	Parameters:
40768508Seric **		none
40868508Seric **
40968508Seric **	Returns:
41068508Seric **		none
41168508Seric **
41268508Seric **	Side Effects:
41368508Seric **		Should define maps "host" and "users" as necessary
41468508Seric **		for this OS.  If they are not defined, they will get
41568508Seric **		a default value later.  It should check to make sure
41668508Seric **		they are not defined first, since it's possible that
41768508Seric **		the config file has provided an override.
41868508Seric */
41968508Seric 
42068508Seric void
42168508Seric inithostmaps()
42268508Seric {
42368508Seric 	register int i;
42468508Seric 	int nmaps;
42568508Seric 	char *maptype[MAXMAPSTACK];
42668508Seric 	short mapreturn[MAXMAPACTIONS];
42768508Seric 	char buf[MAXLINE];
42868508Seric 
42968508Seric 	/*
43068508Seric 	**  Set up default hosts maps.
43168508Seric 	*/
43268508Seric 
43368508Seric #if 0
43468508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
43568508Seric 	for (i = 0; i < nmaps; i++)
43668508Seric 	{
43768508Seric 		if (strcmp(maptype[i], "files") == 0 &&
43868508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
43968508Seric 		{
44068508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
44168508Seric 			makemapentry(buf);
44268508Seric 		}
44368508Seric #if NAMED_BIND
44468508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
44568508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
44668508Seric 		{
44768508Seric 			strcpy(buf, "hosts.dns dns A");
44868508Seric 			makemapentry(buf);
44968508Seric 		}
45068508Seric #endif
45168508Seric #ifdef NISPLUS
45268508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
45368508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
45468508Seric 		{
45568508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
45668508Seric 			makemapentry(buf);
45768508Seric 		}
45868508Seric #endif
45968508Seric #ifdef NIS
46068508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
46168508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
46268508Seric 		{
46368508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
46468508Seric 			makemapentry(buf);
46568508Seric 		}
46668508Seric #endif
46768508Seric 	}
46868508Seric #endif
46968508Seric 
47068508Seric 	/*
47168508Seric 	**  Make sure we have a host map.
47268508Seric 	*/
47368508Seric 
47468508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
47568508Seric 	{
47668508Seric 		/* user didn't initialize: set up host map */
47768508Seric 		strcpy(buf, "host host");
47868508Seric #if NAMED_BIND
47968508Seric 		if (ConfigLevel >= 2)
48068508Seric 			strcat(buf, " -a.");
48168508Seric #endif
48268508Seric 		makemapentry(buf);
48368508Seric 	}
48468508Seric 
48568508Seric 	/*
48668508Seric 	**  Set up default aliases maps
48768508Seric 	*/
48868508Seric 
48968508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
49068508Seric 	for (i = 0; i < nmaps; i++)
49168508Seric 	{
49268508Seric 		if (strcmp(maptype[i], "files") == 0 &&
49368508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
49468508Seric 		{
49568508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
49668508Seric 			makemapentry(buf);
49768508Seric 		}
49868508Seric #ifdef NISPLUS
49968508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
50068508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
50168508Seric 		{
50268508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
50368508Seric 			makemapentry(buf);
50468508Seric 		}
50568508Seric #endif
50668508Seric #ifdef NIS
50768508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
50868508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
50968508Seric 		{
51068508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
51168508Seric 			makemapentry(buf);
51268508Seric 		}
51368508Seric #endif
51468508Seric 	}
51568508Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
51668508Seric 	{
51768508Seric 		strcpy(buf, "aliases switch aliases");
51868508Seric 		makemapentry(buf);
51968508Seric 	}
52068508Seric 	strcpy(buf, "switch:aliases");
52168508Seric 	setalias(buf);
52268508Seric 
52368508Seric #if 0		/* "user" map class is a better choice */
52468508Seric 	/*
52568508Seric 	**  Set up default users maps.
52668508Seric 	*/
52768508Seric 
52868508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
52968508Seric 	for (i = 0; i < nmaps; i++)
53068508Seric 	{
53168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
53268508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
53368508Seric 		{
53468508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
53568508Seric 			makemapentry(buf);
53668508Seric 		}
53768508Seric #ifdef NISPLUS
53868508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
53968508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
54068508Seric 		{
54168508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
54268508Seric 			makemapentry(buf);
54368508Seric 		}
54468508Seric #endif
54568508Seric #ifdef NIS
54668508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
54768508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
54868508Seric 		{
54968508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
55068508Seric 			makemapentry(buf);
55168508Seric 		}
55268508Seric #endif
55368508Seric #ifdef HESIOD
55468508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
55568508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
55668508Seric 		{
55768508Seric 			strcpy(buf, "users.hesiod hesiod");
55868508Seric 			makemapentry(buf);
55968508Seric 		}
56068508Seric #endif
56168508Seric 	}
56268508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
56368508Seric 	{
56468508Seric 		strcpy(buf, "users switch -m passwd");
56568508Seric 		makemapentry(buf);
56668508Seric 	}
56768508Seric #endif
56868508Seric }
56968508Seric /*
57068508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
57168508Seric **
57268508Seric **	This is the system-dependent interface to the service switch.
57368508Seric **
57468508Seric **	Parameters:
57568508Seric **		service -- the name of the service of interest.
57668508Seric **		maptype -- an out-array of strings containing the types
57768508Seric **			of access to use for this service.  There can
57868508Seric **			be at most MAXMAPSTACK types for a single service.
57968508Seric **		mapreturn -- an out-array of return information bitmaps
58068508Seric **			for the map.
58168508Seric **
58268508Seric **	Returns:
58368508Seric **		The number of map types filled in, or -1 for failure.
58468508Seric */
58568508Seric 
58668508Seric #ifdef SOLARIS
58768508Seric # include <nsswitch.h>
58868508Seric #endif
58968508Seric 
59068508Seric #if defined(ultrix) || defined(__osf__)
59168508Seric # include <sys/svcinfo.h>
59268508Seric #endif
59368508Seric 
59468508Seric int
59568508Seric switch_map_find(service, maptype, mapreturn)
59668508Seric 	char *service;
59768508Seric 	char *maptype[MAXMAPSTACK];
59868508Seric 	short mapreturn[MAXMAPACTIONS];
59968508Seric {
60068508Seric 	register FILE *fp;
60168508Seric 	int svcno;
60268508Seric 	static char buf[MAXLINE];
60368508Seric 
60468508Seric #ifdef SOLARIS
60568508Seric 	struct __nsw_switchconfig *nsw_conf;
60668508Seric 	enum __nsw_parse_err pserr;
60768508Seric 	struct __nsw_lookup *lk;
60868508Seric 	int nsw_rc;
60968508Seric 	static struct __nsw_lookup lkp0 =
61068508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
61168508Seric 	static struct __nsw_switchconfig lkp_default =
61268508Seric 		{ 0, "sendmail", 3, &lkp0 };
61368508Seric 
61468508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
61568508Seric 		lk = lkp_default.lookups;
61668508Seric 	else
61768508Seric 		lk = nsw_conf->lookups;
61868508Seric 	svcno = 0;
61968508Seric 	while (lk != NULL)
62068508Seric 	{
62168508Seric 		maptype[svcno] = lk->service_name;
62268508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
62368508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
62468508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
62568508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62668508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
62768508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62868508Seric 		svcno++;
62968508Seric 		lk = lk->next;
63068508Seric 	}
63168508Seric 	return svcno;
63268508Seric #endif
63368508Seric 
63468508Seric #if defined(ultrix) || defined(__osf__)
63568508Seric 	struct svcinfo *svcinfo;
63668508Seric 	int svc;
63768508Seric 
63869401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
63969401Seric 		mapreturn[svcno] = 0;
64069401Seric 
64168508Seric 	svcinfo = getsvc();
64268508Seric 	if (svcinfo == NULL)
64368508Seric 		goto punt;
64468508Seric 	if (strcmp(service, "hosts") == 0)
64568508Seric 		svc = SVC_HOSTS;
64668508Seric 	else if (strcmp(service, "aliases") == 0)
64768508Seric 		svc = SVC_ALIASES;
64868508Seric 	else if (strcmp(service, "passwd") == 0)
64968508Seric 		svc = SVC_PASSWD;
65068508Seric 	else
65168508Seric 		return -1;
65268508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
65368508Seric 	{
65468508Seric 		switch (svcinfo->svcpath[svc][svcno])
65568508Seric 		{
65668508Seric 		  case SVC_LOCAL:
65768508Seric 			maptype[svcno] = "files";
65868508Seric 			break;
65968508Seric 
66068508Seric 		  case SVC_YP:
66168508Seric 			maptype[svcno] = "nis";
66268508Seric 			break;
66368508Seric 
66468508Seric 		  case SVC_BIND:
66568508Seric 			maptype[svcno] = "dns";
66668508Seric 			break;
66768508Seric 
66868508Seric #ifdef SVC_HESIOD
66968508Seric 		  case SVC_HESIOD:
67068508Seric 			maptype[svcno] = "hesiod";
67168508Seric 			break;
67268508Seric #endif
67368508Seric 
67468508Seric 		  case SVC_LAST:
67568508Seric 			return svcno;
67668508Seric 		}
67768508Seric 	}
67868508Seric 	return svcno;
67968508Seric #endif
68068508Seric 
68168508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
68268508Seric 	/*
68368508Seric 	**  Fall-back mechanism.
68468508Seric 	*/
68568508Seric 
68669401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
68769401Seric 		mapreturn[svcno] = 0;
68869401Seric 
68968508Seric 	svcno = 0;
69068508Seric 	fp = fopen(ServiceSwitchFile, "r");
69168508Seric 	if (fp != NULL)
69268508Seric 	{
69368508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
69468508Seric 		{
69568508Seric 			register char *p;
69668508Seric 
69768508Seric 			p = strpbrk(buf, "#\n");
69868508Seric 			if (p != NULL)
69968508Seric 				*p = '\0';
70068508Seric 			p = strpbrk(buf, " \t");
70168508Seric 			if (p != NULL)
70268508Seric 				*p++ = '\0';
70368508Seric 			if (strcmp(buf, service) != 0)
70468508Seric 				continue;
70568508Seric 
70668508Seric 			/* got the right service -- extract data */
70768508Seric 			do
70868508Seric 			{
70968508Seric 				while (isspace(*p))
71068508Seric 					p++;
71168508Seric 				if (*p == '\0')
71268508Seric 					break;
71368508Seric 				maptype[svcno++] = p;
71468508Seric 				p = strpbrk(p, " \t");
71568508Seric 				if (p != NULL)
71668508Seric 					*p++ = '\0';
71768508Seric 			} while (p != NULL);
71868508Seric 			break;
71968508Seric 		}
72068508Seric 		fclose(fp);
72168508Seric 		return svcno;
72268508Seric 	}
72368508Seric #endif
72468508Seric 
72568508Seric 	/* if the service file doesn't work, use an absolute fallback */
72668508Seric   punt:
72769401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
72869401Seric 		mapreturn[svcno] = 0;
72969401Seric 	svcno = 0;
73068508Seric 	if (strcmp(service, "aliases") == 0)
73168508Seric 	{
73268628Seric 		maptype[svcno++] = "files";
73368628Seric #ifdef AUTO_NIS_ALIASES
73468628Seric # ifdef NISPLUS
73568628Seric 		maptype[svcno++] = "nisplus";
73668628Seric # endif
73768628Seric # ifdef NIS
73868628Seric 		maptype[svcno++] = "nis";
73968628Seric # endif
74068628Seric #endif
74168628Seric 		return svcno;
74268508Seric 	}
74368508Seric 	if (strcmp(service, "hosts") == 0)
74468508Seric 	{
74568508Seric # if NAMED_BIND
74668508Seric 		maptype[svcno++] = "dns";
74768508Seric # else
74868508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
74968508Seric 		/* SunOS */
75068508Seric 		maptype[svcno++] = "nis";
75168508Seric #  endif
75268508Seric # endif
75368508Seric 		maptype[svcno++] = "files";
75468508Seric 		return svcno;
75568508Seric 	}
75668508Seric 	return -1;
75768508Seric }
75868508Seric /*
7599369Seric **  USERNAME -- return the user id of the logged in user.
7609369Seric **
7619369Seric **	Parameters:
7629369Seric **		none.
7639369Seric **
7649369Seric **	Returns:
7659369Seric **		The login name of the logged in user.
7669369Seric **
7679369Seric **	Side Effects:
7689369Seric **		none.
7699369Seric **
7709369Seric **	Notes:
7719369Seric **		The return value is statically allocated.
7729369Seric */
7739369Seric 
7749369Seric char *
7759369Seric username()
7769369Seric {
77717469Seric 	static char *myname = NULL;
7789369Seric 	extern char *getlogin();
77919904Smiriam 	register struct passwd *pw;
7809369Seric 
78117469Seric 	/* cache the result */
78217469Seric 	if (myname == NULL)
78317469Seric 	{
78417469Seric 		myname = getlogin();
78517469Seric 		if (myname == NULL || myname[0] == '\0')
78617469Seric 		{
78768693Seric 			pw = sm_getpwuid(RealUid);
78817469Seric 			if (pw != NULL)
78940993Sbostic 				myname = newstr(pw->pw_name);
79017469Seric 		}
79119904Smiriam 		else
79219904Smiriam 		{
79363787Seric 			uid_t uid = RealUid;
79419873Smiriam 
79540993Sbostic 			myname = newstr(myname);
79668693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
79758736Seric 			      (uid != 0 && uid != pw->pw_uid))
79819904Smiriam 			{
79968693Seric 				pw = sm_getpwuid(uid);
80024945Seric 				if (pw != NULL)
80140993Sbostic 					myname = newstr(pw->pw_name);
80219873Smiriam 			}
80319873Smiriam 		}
80417469Seric 		if (myname == NULL || myname[0] == '\0')
80517469Seric 		{
80658151Seric 			syserr("554 Who are you?");
80717469Seric 			myname = "postmaster";
80817469Seric 		}
80917469Seric 	}
81017469Seric 
81117469Seric 	return (myname);
8129369Seric }
8139369Seric /*
8144190Seric **  TTYPATH -- Get the path of the user's tty
815294Seric **
816294Seric **	Returns the pathname of the user's tty.  Returns NULL if
817294Seric **	the user is not logged in or if s/he has write permission
818294Seric **	denied.
819294Seric **
820294Seric **	Parameters:
821294Seric **		none
822294Seric **
823294Seric **	Returns:
824294Seric **		pathname of the user's tty.
825294Seric **		NULL if not logged in or write permission denied.
826294Seric **
827294Seric **	Side Effects:
828294Seric **		none.
829294Seric **
830294Seric **	WARNING:
831294Seric **		Return value is in a local buffer.
832294Seric **
833294Seric **	Called By:
834294Seric **		savemail
835294Seric */
836294Seric 
837294Seric char *
838294Seric ttypath()
839294Seric {
840294Seric 	struct stat stbuf;
841294Seric 	register char *pathn;
842294Seric 	extern char *ttyname();
8434081Seric 	extern char *getlogin();
844294Seric 
845294Seric 	/* compute the pathname of the controlling tty */
8469369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8479369Seric 	    (pathn = ttyname(0)) == NULL)
848294Seric 	{
849294Seric 		errno = 0;
850294Seric 		return (NULL);
851294Seric 	}
852294Seric 
853294Seric 	/* see if we have write permission */
8542967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
855294Seric 	{
856294Seric 		errno = 0;
857294Seric 		return (NULL);
858294Seric 	}
859294Seric 
860294Seric 	/* see if the user is logged in */
861294Seric 	if (getlogin() == NULL)
862294Seric 		return (NULL);
863294Seric 
864294Seric 	/* looks good */
865294Seric 	return (pathn);
866294Seric }
8672967Seric /*
8682967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8692967Seric **
8702967Seric **	This routine can be supplied on a per-installation basis
8712967Seric **	to determine whether a person is allowed to send a message.
8722967Seric **	This allows restriction of certain types of internet
8732967Seric **	forwarding or registration of users.
8742967Seric **
8752967Seric **	If the hosts are found to be incompatible, an error
87668857Seric **	message should be given using "usrerr" and an EX_ code
87768857Seric **	should be returned.  You can also set to->q_status to
87868857Seric **	a DSN-style status code.
8792967Seric **
88068559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
88168559Seric **	body during the return-to-sender function; this should be done
88268559Seric **	on huge messages.  This bit may already be set by the ESMTP
88368559Seric **	protocol.
8844288Seric **
8852967Seric **	Parameters:
8862967Seric **		to -- the person being sent to.
8872967Seric **
8882967Seric **	Returns:
88957459Seric **		an exit status
8902967Seric **
8912967Seric **	Side Effects:
8922967Seric **		none (unless you include the usrerr stuff)
8932967Seric */
8942967Seric 
89568508Seric int
89655012Seric checkcompat(to, e)
8972967Seric 	register ADDRESS *to;
89855012Seric 	register ENVELOPE *e;
8992967Seric {
90012133Seric # ifdef lint
90112133Seric 	if (to == NULL)
90212133Seric 		to++;
90363834Seric # endif /* lint */
90464402Seric 
90564402Seric 	if (tTd(49, 1))
90664402Seric 		printf("checkcompat(to=%s, from=%s)\n",
90764402Seric 			to->q_paddr, e->e_from.q_paddr);
90864402Seric 
90910698Seric # ifdef EXAMPLE_CODE
91010698Seric 	/* this code is intended as an example only */
9114437Seric 	register STAB *s;
9124437Seric 
9134437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
91468508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9159369Seric 	    to->q_mailer == s->s_mailer)
9164437Seric 	{
91758151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
91868559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
91968857Seric 		to->q_status = "5.7.1";
92057459Seric 		return (EX_UNAVAILABLE);
9214437Seric 	}
92256795Seric # endif /* EXAMPLE_CODE */
92357459Seric 	return (EX_OK);
9242967Seric }
9259369Seric /*
92664035Seric **  SETSIGNAL -- set a signal handler
92764035Seric **
92864035Seric **	This is essentially old BSD "signal(3)".
92964035Seric */
93064035Seric 
93164561Seric sigfunc_t
93264035Seric setsignal(sig, handler)
93364035Seric 	int sig;
93464561Seric 	sigfunc_t handler;
93564035Seric {
93664380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
93764035Seric 	return signal(sig, handler);
93864035Seric #else
93964035Seric 	struct sigaction n, o;
94064035Seric 
94164035Seric 	bzero(&n, sizeof n);
94264035Seric 	n.sa_handler = handler;
94368508Seric # ifdef SA_RESTART
94468508Seric 	n.sa_flags = SA_RESTART;
94568508Seric # endif
94664035Seric 	if (sigaction(sig, &n, &o) < 0)
94764035Seric 		return SIG_ERR;
94864035Seric 	return o.sa_handler;
94964035Seric #endif
95064035Seric }
95164035Seric /*
9529369Seric **  HOLDSIGS -- arrange to hold all signals
9539369Seric **
9549369Seric **	Parameters:
9559369Seric **		none.
9569369Seric **
9579369Seric **	Returns:
9589369Seric **		none.
9599369Seric **
9609369Seric **	Side Effects:
9619369Seric **		Arranges that signals are held.
9629369Seric */
9639369Seric 
96468508Seric void
9659369Seric holdsigs()
9669369Seric {
9679369Seric }
9689369Seric /*
9699369Seric **  RLSESIGS -- arrange to release all signals
9709369Seric **
9719369Seric **	This undoes the effect of holdsigs.
9729369Seric **
9739369Seric **	Parameters:
9749369Seric **		none.
9759369Seric **
9769369Seric **	Returns:
9779369Seric **		none.
9789369Seric **
9799369Seric **	Side Effects:
9809369Seric **		Arranges that signals are released.
9819369Seric */
9829369Seric 
98368508Seric void
9849369Seric rlsesigs()
9859369Seric {
9869369Seric }
98714872Seric /*
98864705Seric **  INIT_MD -- do machine dependent initializations
98964705Seric **
99064705Seric **	Systems that have global modes that should be set should do
99164705Seric **	them here rather than in main.
99264705Seric */
99364705Seric 
99464705Seric #ifdef _AUX_SOURCE
99564705Seric # include	<compat.h>
99664705Seric #endif
99764705Seric 
99868508Seric void
99964825Seric init_md(argc, argv)
100064825Seric 	int argc;
100164825Seric 	char **argv;
100264705Seric {
100364705Seric #ifdef _AUX_SOURCE
100464705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
100564705Seric #endif
100668508Seric 
100768508Seric #ifdef VENDOR_DEFAULT
100868508Seric 	VendorCode = VENDOR_DEFAULT;
100968508Seric #else
101068508Seric 	VendorCode = VENDOR_BERKELEY;
101168508Seric #endif
101264705Seric }
101364705Seric /*
101468508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
101568508Seric **
101668508Seric **	Called once, on startup.
101768508Seric **
101868508Seric **	Parameters:
101968508Seric **		e -- the global envelope.
102068508Seric **
102168508Seric **	Returns:
102268508Seric **		none.
102368508Seric **
102468508Seric **	Side Effects:
102568508Seric **		vendor-dependent.
102668508Seric */
102768508Seric 
102868508Seric void
102968508Seric init_vendor_macros(e)
103068508Seric 	register ENVELOPE *e;
103168508Seric {
103268508Seric }
103368508Seric /*
103414872Seric **  GETLA -- get the current load average
103514872Seric **
103614881Seric **	This code stolen from la.c.
103714881Seric **
103814872Seric **	Parameters:
103914872Seric **		none.
104014872Seric **
104114872Seric **	Returns:
104214872Seric **		The current load average as an integer.
104314872Seric **
104414872Seric **	Side Effects:
104514872Seric **		none.
104614872Seric */
104714872Seric 
104851920Seric /* try to guess what style of load average we have */
104951920Seric #define LA_ZERO		1	/* always return load average as zero */
105064360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
105151920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
105251920Seric #define LA_SUBR		4	/* call getloadavg */
105364295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
105464360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
105566301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
1056*69462Seric #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
1057*69462Seric #define LA_DGUX		9	/* special DGUX implementation */
1058*69462Seric #define LA_HPUX		10	/* special HPUX implementation */
105914872Seric 
106063962Seric /* do guesses based on general OS type */
106151920Seric #ifndef LA_TYPE
106263962Seric # define LA_TYPE	LA_ZERO
106351920Seric #endif
106451920Seric 
1065*69462Seric #ifndef FSHIFT
1066*69462Seric # if defined(unixpc)
1067*69462Seric #  define FSHIFT	5
1068*69462Seric # endif
1069*69462Seric 
1070*69462Seric # if defined(__alpha) || defined(IRIX)
1071*69462Seric #  define FSHIFT	10
1072*69462Seric # endif
1073*69462Seric 
1074*69462Seric # if defined(_AIX3)
1075*69462Seric #  define FSHIFT	16
1076*69462Seric # endif
1077*69462Seric #endif
1078*69462Seric 
1079*69462Seric #ifndef FSHIFT
1080*69462Seric # define FSHIFT		8
1081*69462Seric #endif
1082*69462Seric 
1083*69462Seric #ifndef FSCALE
1084*69462Seric # define FSCALE		(1 << FSHIFT)
1085*69462Seric #endif
1086*69462Seric 
108764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
108851920Seric 
108914872Seric #include <nlist.h>
109014872Seric 
109168508Seric #ifdef IRIX64
109268508Seric # define nlist		nlist64
109368508Seric #endif
109468508Seric 
109551920Seric #ifndef LA_AVENRUN
109663962Seric # ifdef SYSTEM5
109763962Seric #  define LA_AVENRUN	"avenrun"
109863962Seric # else
109963962Seric #  define LA_AVENRUN	"_avenrun"
110063962Seric # endif
110151920Seric #endif
110251920Seric 
110351920Seric /* _PATH_UNIX should be defined in <paths.h> */
110451920Seric #ifndef _PATH_UNIX
110563962Seric # if defined(SYSTEM5)
110663962Seric #  define _PATH_UNIX	"/unix"
110763962Seric # else
110863962Seric #  define _PATH_UNIX	"/vmunix"
110963962Seric # endif
111051920Seric #endif
111151920Seric 
1112*69462Seric struct nlist	Nl[] =
111314872Seric {
111451920Seric 	{ LA_AVENRUN },
111514872Seric #define	X_AVENRUN	0
111614872Seric 	{ 0 },
111714872Seric };
111814872Seric 
111914872Seric getla()
112014872Seric {
112114872Seric 	static int kmem = -1;
112251920Seric #if LA_TYPE == LA_INT
112324943Seric 	long avenrun[3];
112451920Seric #else
112564360Seric # if LA_TYPE == LA_SHORT
112664360Seric 	short avenrun[3];
112764360Seric # else
112851920Seric 	double avenrun[3];
112964360Seric # endif
113051920Seric #endif
1131*69462Seric 	extern int errno;
113225615Seric 	extern off_t lseek();
113314872Seric 
113414872Seric 	if (kmem < 0)
113514872Seric 	{
113624945Seric 		kmem = open("/dev/kmem", 0, 0);
113714872Seric 		if (kmem < 0)
113857736Seric 		{
113957736Seric 			if (tTd(3, 1))
114057736Seric 				printf("getla: open(/dev/kmem): %s\n",
114157736Seric 					errstring(errno));
114214872Seric 			return (-1);
114357736Seric 		}
114451920Seric 		(void) fcntl(kmem, F_SETFD, 1);
1145*69462Seric 
114668508Seric #ifdef _AIX3
114768508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
114868508Seric #else
114957736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
115068508Seric #endif
115157736Seric 		{
115257736Seric 			if (tTd(3, 1))
115357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
115457736Seric 					errstring(errno));
115514872Seric 			return (-1);
115657736Seric 		}
115759253Seric 		if (Nl[X_AVENRUN].n_value == 0)
115859253Seric 		{
115959253Seric 			if (tTd(3, 1))
116059253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
116159253Seric 					_PATH_UNIX, LA_AVENRUN);
116259253Seric 			return (-1);
116359253Seric 		}
116468508Seric #ifdef NAMELISTMASK
116568508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
116666763Seric #endif
116714872Seric 	}
116857736Seric 	if (tTd(3, 20))
116957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
117068508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
117123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
117219967Seric 	{
117319967Seric 		/* thank you Ian */
117457736Seric 		if (tTd(3, 1))
117557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
117619967Seric 		return (-1);
117719967Seric 	}
1178*69462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
117957736Seric 	if (tTd(3, 5))
118057736Seric 	{
118157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
118257736Seric 		if (tTd(3, 15))
118357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
118457736Seric 		printf("\n");
118557736Seric 	}
118657736Seric 	if (tTd(3, 1))
118757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
118824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1189*69462Seric # else /* LA_TYPE == LA_FLOAT */
119057736Seric 	if (tTd(3, 5))
119157736Seric 	{
119257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
119357736Seric 		if (tTd(3, 15))
119457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
119557736Seric 		printf("\n");
119657736Seric 	}
119757736Seric 	if (tTd(3, 1))
119857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
119951920Seric 	return ((int) (avenrun[0] + 0.5));
1200*69462Seric # endif
120114872Seric }
120214872Seric 
1203*69462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
120451773Seric 
1205*69462Seric #if LA_TYPE == LA_READKSYM
120664718Seric 
1207*69462Seric getla()
1208*69462Seric {
1209*69462Seric 	static int kmem = -1;
1210*69462Seric 	long avenrun[3];
1211*69462Seric 	extern int errno;
1212*69462Seric 	struct mioc_rksym mirk;
121364718Seric 
1214*69462Seric 	if (kmem < 0)
1215*69462Seric 	{
1216*69462Seric 		kmem = open("/dev/kmem", 0, 0);
1217*69462Seric 		if (kmem < 0)
1218*69462Seric 		{
1219*69462Seric 			if (tTd(3, 1))
1220*69462Seric 				printf("getla: open(/dev/kmem): %s\n",
1221*69462Seric 					errstring(errno));
1222*69462Seric 			return (-1);
1223*69462Seric 		}
1224*69462Seric 		(void) fcntl(kmem, F_SETFD, 1);
1225*69462Seric 	}
1226*69462Seric 	mirk.mirk_symname = LA_AVENRUN;
1227*69462Seric 	mirk.mirk_buf = avenrun;
1228*69462Seric 	mirk.mirk_buflen = sizeof(avenrun);
1229*69462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
1230*69462Seric 	{
1231*69462Seric 		if (tTd(3, 1))
1232*69462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
1233*69462Seric 				errstring(errno));
1234*69462Seric 		return -1;
1235*69462Seric 	}
1236*69462Seric 	if (tTd(3, 5))
1237*69462Seric 	{
1238*69462Seric 		printf("getla: avenrun = %d", avenrun[0]);
1239*69462Seric 		if (tTd(3, 15))
1240*69462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
1241*69462Seric 		printf("\n");
1242*69462Seric 	}
1243*69462Seric 	if (tTd(3, 1))
1244*69462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1245*69462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1246*69462Seric }
1247*69462Seric 
1248*69462Seric #endif /* LA_TYPE == LA_READKSYM */
1249*69462Seric 
1250*69462Seric #if LA_TYPE == LA_DGUX
1251*69462Seric 
1252*69462Seric # include <sys/dg_sys_info.h>
1253*69462Seric 
125468508Seric int
125568508Seric getla()
125664718Seric {
125764718Seric 	struct dg_sys_info_load_info load_info;
125864718Seric 
125964718Seric 	dg_sys_info((long *)&load_info,
126064718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
126164718Seric 
126268508Seric         if (tTd(3, 1))
126368508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
126468508Seric 
126564718Seric 	return((int) (load_info.one_minute + 0.5));
126664718Seric }
126764718Seric 
1268*69462Seric #endif /* LA_TYPE == LA_DGUX */
126964718Seric 
1270*69462Seric #if LA_TYPE == LA_HPUX
1271*69462Seric 
127268869Seric struct pst_dynamic;
127368869Seric 
1274*69462Seric # include <sys/param.h>
1275*69462Seric # include <sys/pstat.h>
127668508Seric 
127768508Seric int
127851773Seric getla()
127951773Seric {
128068508Seric 	struct pst_dynamic pstd;
128168508Seric 
128268508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
128368869Seric 			     (size_t) 1, 0) == -1)
128468508Seric 		return 0;
128568508Seric 
128668508Seric         if (tTd(3, 1))
128768508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
128868508Seric 
128968508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
129068508Seric }
129168508Seric 
1292*69462Seric #endif /* LA_TYPE == LA_HPUX */
129368508Seric 
1294*69462Seric #if LA_TYPE == LA_SUBR
1295*69462Seric 
129668508Seric int
129768508Seric getla()
129868508Seric {
129951920Seric 	double avenrun[3];
130051920Seric 
130151920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
130257736Seric 	{
130357736Seric 		if (tTd(3, 1))
130457736Seric 			perror("getla: getloadavg failed:");
130551920Seric 		return (-1);
130657736Seric 	}
130757736Seric 	if (tTd(3, 1))
130857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
130951920Seric 	return ((int) (avenrun[0] + 0.5));
131051773Seric }
131151773Seric 
1312*69462Seric #endif /* LA_TYPE == LA_SUBR */
1313*69462Seric 
131464295Seric #if LA_TYPE == LA_MACH
131551773Seric 
131664295Seric /*
131765173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
131864295Seric */
131964295Seric 
132065173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
132165173Seric # include <mach/mach.h>
132265173Seric #else
132365173Seric # include <mach.h>
132465173Seric #endif
132564295Seric 
132651773Seric getla()
132751773Seric {
132864295Seric 	processor_set_t default_set;
132964295Seric 	kern_return_t error;
133064295Seric 	unsigned int info_count;
133164295Seric 	struct processor_set_basic_info info;
133264295Seric 	host_t host;
133364295Seric 
133464295Seric 	error = processor_set_default(host_self(), &default_set);
133564295Seric 	if (error != KERN_SUCCESS)
133664295Seric 		return -1;
133764295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
133864295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
133964295Seric 			       &host, (processor_set_info_t)&info,
134064295Seric 			       &info_count) != KERN_SUCCESS)
134164295Seric 	{
134264295Seric 		return -1;
134364295Seric 	}
134464295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
134564295Seric }
134664295Seric 
1347*69462Seric #endif /* LA_TYPE == LA_MACH */
134864295Seric 
134966313Seric #if LA_TYPE == LA_PROCSTR
135064295Seric 
135166301Seric /*
135266301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
135366301Seric **  in a format like "0.15 0.12 0.06".
135466301Seric **
135566301Seric **	Initially intended for Linux.  This has been in the kernel
135666301Seric **	since at least 0.99.15.
135766301Seric */
135866301Seric 
135966301Seric # ifndef _PATH_LOADAVG
136066301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
136166301Seric # endif
136266301Seric 
136366301Seric int
136464295Seric getla()
136564295Seric {
136666319Seric 	double avenrun;
136766301Seric 	register int result;
136866319Seric 	FILE *fp;
136966301Seric 
137066319Seric 	fp = fopen(_PATH_LOADAVG, "r");
137166319Seric 	if (fp == NULL)
137266301Seric 	{
137366319Seric 		if (tTd(3, 1))
137466319Seric 			printf("getla: fopen(%s): %s\n",
137566319Seric 				_PATH_LOADAVG, errstring(errno));
137666319Seric 		return -1;
137766301Seric 	}
137866301Seric 	result = fscanf(fp, "%lf", &avenrun);
137966319Seric 	fclose(fp);
138066301Seric 	if (result != 1)
138166301Seric 	{
138266310Seric 		if (tTd(3, 1))
138366301Seric 			printf("getla: fscanf() = %d: %s\n",
138466301Seric 				result, errstring(errno));
138566301Seric 		return -1;
138666301Seric 	}
138766301Seric 
138857736Seric 	if (tTd(3, 1))
138966301Seric 		printf("getla(): %.2f\n", avenrun);
139066301Seric 
139166301Seric 	return ((int) (avenrun + 0.5));
139266301Seric }
139366301Seric 
1394*69462Seric #endif /* LA_TYPE == LA_PROCSTR */
139566301Seric 
1396*69462Seric #if LA_TYPE == LA_ZERO
1397*69462Seric 
139866301Seric getla()
139966301Seric {
140066301Seric 	if (tTd(3, 1))
140157736Seric 		printf("getla: ZERO\n");
140251920Seric 	return (0);
140351773Seric }
140451773Seric 
1405*69462Seric #endif /* LA_TYPE == LA_ZERO */
140666045Seric 
140766045Seric 
140866045Seric /*
140966045Seric  * Copyright 1989 Massachusetts Institute of Technology
141066045Seric  *
141166045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
141266045Seric  * documentation for any purpose is hereby granted without fee, provided that
141366045Seric  * the above copyright notice appear in all copies and that both that
141466045Seric  * copyright notice and this permission notice appear in supporting
141566045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
141666045Seric  * publicity pertaining to distribution of the software without specific,
141766045Seric  * written prior permission.  M.I.T. makes no representations about the
141866045Seric  * suitability of this software for any purpose.  It is provided "as is"
141966045Seric  * without express or implied warranty.
142066045Seric  *
142166045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
142266045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
142366045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
142466045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
142566045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
142666045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
142766045Seric  *
142866045Seric  * Authors:  Many and varied...
142966045Seric  */
143066045Seric 
143166045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
143266045Seric #ifndef lint
143366045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
143466045Seric #endif /* !lint */
143566045Seric 
143666179Seric #ifdef apollo
143766045Seric # undef volatile
143866045Seric #    include <apollo/base.h>
143966045Seric 
144066045Seric /* ARGSUSED */
144166045Seric int getloadavg( call_data )
144266045Seric      caddr_t	call_data;	/* pointer to (double) return value */
144366045Seric {
144466045Seric      double *avenrun = (double *) call_data;
144566045Seric      int i;
144666045Seric      status_$t      st;
144766045Seric      long loadav[3];
144866045Seric      proc1_$get_loadav(loadav, &st);
144966045Seric      *avenrun = loadav[0] / (double) (1 << 16);
145066045Seric      return(0);
145166045Seric }
145266045Seric #   endif /* apollo */
145324943Seric /*
145424943Seric **  SHOULDQUEUE -- should this message be queued or sent?
145524943Seric **
145624943Seric **	Compares the message cost to the load average to decide.
145724943Seric **
145824943Seric **	Parameters:
145924943Seric **		pri -- the priority of the message in question.
146057438Seric **		ctime -- the message creation time.
146124943Seric **
146224943Seric **	Returns:
146324943Seric **		TRUE -- if this message should be queued up for the
146424943Seric **			time being.
146524943Seric **		FALSE -- if the load is low enough to send this message.
146624943Seric **
146724943Seric **	Side Effects:
146824943Seric **		none.
146924943Seric */
147024943Seric 
147124943Seric bool
147257438Seric shouldqueue(pri, ctime)
147324943Seric 	long pri;
147457438Seric 	time_t ctime;
147524943Seric {
147668508Seric 	bool rval;
147768508Seric 
147868508Seric 	if (tTd(3, 30))
147968508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
148051920Seric 	if (CurrentLA < QueueLA)
148168508Seric 	{
148268508Seric 		if (tTd(3, 30))
148368508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
148424943Seric 		return (FALSE);
148568508Seric 	}
148658132Seric 	if (CurrentLA >= RefuseLA)
148768508Seric 	{
148868508Seric 		if (tTd(3, 30))
148968508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
149058132Seric 		return (TRUE);
149168508Seric 	}
149268508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
149368508Seric 	if (tTd(3, 30))
149468508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
149568508Seric 	return rval;
149624943Seric }
149724943Seric /*
149853037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
149953037Seric **
150053037Seric **	Parameters:
150153037Seric **		none.
150253037Seric **
150353037Seric **	Returns:
150453037Seric **		TRUE if incoming SMTP connections should be refused
150553037Seric **			(for now).
150653037Seric **		FALSE if we should accept new work.
150753037Seric **
150853037Seric **	Side Effects:
150953037Seric **		none.
151053037Seric */
151153037Seric 
151253037Seric bool
151353037Seric refuseconnections()
151453037Seric {
151568508Seric 	extern bool enoughspace();
151668508Seric 
151759156Seric #ifdef XLA
151859156Seric 	if (!xla_smtp_ok())
151959156Seric 		return TRUE;
152059156Seric #endif
152159156Seric 
152253037Seric 	/* this is probably too simplistic */
152368508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
152453037Seric }
152553037Seric /*
152624943Seric **  SETPROCTITLE -- set process title for ps
152724943Seric **
152824943Seric **	Parameters:
152958674Seric **		fmt -- a printf style format string.
153058674Seric **		a, b, c -- possible parameters to fmt.
153124943Seric **
153224943Seric **	Returns:
153324943Seric **		none.
153424943Seric **
153524943Seric **	Side Effects:
153624943Seric **		Clobbers argv of our main procedure so ps(1) will
153724943Seric **		display the title.
153824943Seric */
153924943Seric 
154068508Seric #define SPT_NONE	0	/* don't use it at all */
154168508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
154268508Seric #define SPT_BUILTIN	2	/* use libc builtin */
154368508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
154468508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
154568508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
154668508Seric 
154768508Seric #ifndef SPT_TYPE
154868508Seric # define SPT_TYPE	SPT_REUSEARGV
154968508Seric #endif
155068508Seric 
155168508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
155268508Seric 
155368508Seric # if SPT_TYPE == SPT_PSTAT
155458689Seric #  include <sys/pstat.h>
155558689Seric # endif
155668508Seric # if SPT_TYPE == SPT_PSSTRINGS
155759732Seric #  include <machine/vmparam.h>
155859732Seric #  include <sys/exec.h>
155968508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
156068508Seric #   undef SPT_TYPE
156168508Seric #   define SPT_TYPE	SPT_REUSEARGV
156268734Seric #  else
156368734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
156468734Seric #    define NKPDE 63
156568734Seric typedef unsigned int	*pt_entry_t;
156668734Seric #   endif
156764247Seric #  endif
156859732Seric # endif
156968508Seric 
157068508Seric # if SPT_TYPE == SPT_PSSTRINGS
157168508Seric #  define SETPROC_STATIC	static
157268508Seric # else
157359732Seric #  define SETPROC_STATIC
157459732Seric # endif
157567626Seric 
157668508Seric # ifndef SPT_PADCHAR
157768508Seric #  define SPT_PADCHAR	' '
157868508Seric # endif
157958689Seric 
158068508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
158164843Seric 
158268508Seric #if SPT_TYPE != SPT_BUILTIN
158368508Seric 
158424943Seric /*VARARGS1*/
158568508Seric void
158668508Seric # ifdef __STDC__
158757642Seric setproctitle(char *fmt, ...)
158868508Seric # else
158957642Seric setproctitle(fmt, va_alist)
159024943Seric 	char *fmt;
159157642Seric 	va_dcl
159268508Seric # endif
159324943Seric {
159468508Seric # if SPT_TYPE != SPT_NONE
159524943Seric 	register char *p;
159625049Seric 	register int i;
159759732Seric 	SETPROC_STATIC char buf[MAXLINE];
159856852Seric 	VA_LOCAL_DECL
159968508Seric #  if SPT_TYPE == SPT_PSTAT
160058689Seric 	union pstun pst;
160158689Seric #  endif
160224943Seric 	extern char **Argv;
160324943Seric 	extern char *LastArgv;
160424943Seric 
160558674Seric 	p = buf;
160624943Seric 
160758674Seric 	/* print sendmail: heading for grep */
160858674Seric 	(void) strcpy(p, "sendmail: ");
160958674Seric 	p += strlen(p);
161024943Seric 
161158674Seric 	/* print the argument string */
161258674Seric 	VA_START(fmt);
161358674Seric 	(void) vsprintf(p, fmt, ap);
161456852Seric 	VA_END;
161554996Seric 
161658674Seric 	i = strlen(buf);
161758689Seric 
161868508Seric #  if SPT_TYPE == SPT_PSTAT
161958689Seric 	pst.pst_command = buf;
162058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
162158689Seric #  else
162268508Seric #   if SPT_TYPE == SPT_PSSTRINGS
162359732Seric 	PS_STRINGS->ps_nargvstr = 1;
162459732Seric 	PS_STRINGS->ps_argvstr = buf;
162559732Seric #   else
162654996Seric 	if (i > LastArgv - Argv[0] - 2)
162725049Seric 	{
162854996Seric 		i = LastArgv - Argv[0] - 2;
162958674Seric 		buf[i] = '\0';
163025049Seric 	}
163158674Seric 	(void) strcpy(Argv[0], buf);
163254997Seric 	p = &Argv[0][i];
163324943Seric 	while (p < LastArgv)
163468508Seric 		*p++ = SPT_PADCHAR;
163568508Seric 	Argv[1] = NULL;
163668508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
163768508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
163868508Seric # endif /* SPT_TYPE != SPT_NONE */
163924943Seric }
164065982Seric 
164168508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
164225698Seric /*
164325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
164425698Seric **
164525698Seric **	Parameters:
164668508Seric **		sig -- the signal that got us here (unused).
164725698Seric **
164825698Seric **	Returns:
164925698Seric **		none.
165025698Seric **
165125698Seric **	Side Effects:
165225698Seric **		Picks up extant zombies.
165325698Seric */
165425698Seric 
165546928Sbostic void
165668508Seric reapchild(sig)
165768508Seric 	int sig;
165825698Seric {
165964738Seric 	int olderrno = errno;
166063966Seric # ifdef HASWAITPID
166160219Seric 	auto int status;
166260560Seric 	int count;
166360560Seric 	int pid;
166460219Seric 
166560560Seric 	count = 0;
166660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
166760560Seric 	{
166860560Seric 		if (count++ > 1000)
166960560Seric 		{
167066748Seric #ifdef LOG
167160560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
167260560Seric 				pid, status);
167366748Seric #endif
167460560Seric 			break;
167560560Seric 		}
167660560Seric 	}
167760219Seric # else
167825698Seric # ifdef WNOHANG
167925698Seric 	union wait status;
168025698Seric 
168163753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
168225698Seric 		continue;
168356795Seric # else /* WNOHANG */
168425698Seric 	auto int status;
168525698Seric 
168660219Seric 	while (wait(&status) > 0)
168725698Seric 		continue;
168856795Seric # endif /* WNOHANG */
168960219Seric # endif
169064035Seric # ifdef SYS5SIGNALS
169164035Seric 	(void) setsignal(SIGCHLD, reapchild);
169258061Seric # endif
169364738Seric 	errno = olderrno;
169425698Seric }
169555418Seric /*
169655418Seric **  UNSETENV -- remove a variable from the environment
169755418Seric **
169855418Seric **	Not needed on newer systems.
169955418Seric **
170055418Seric **	Parameters:
170155418Seric **		name -- the string name of the environment variable to be
170255418Seric **			deleted from the current environment.
170355418Seric **
170455418Seric **	Returns:
170555418Seric **		none.
170655418Seric **
170755418Seric **	Globals:
170855418Seric **		environ -- a pointer to the current environment.
170955418Seric **
171055418Seric **	Side Effects:
171155418Seric **		Modifies environ.
171255418Seric */
171355418Seric 
171463962Seric #ifndef HASUNSETENV
171555418Seric 
171655418Seric void
171755418Seric unsetenv(name)
171855418Seric 	char *name;
171955418Seric {
172055418Seric 	extern char **environ;
172155418Seric 	register char **pp;
172255418Seric 	int len = strlen(name);
172355418Seric 
172455418Seric 	for (pp = environ; *pp != NULL; pp++)
172555418Seric 	{
172655418Seric 		if (strncmp(name, *pp, len) == 0 &&
172755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
172855418Seric 			break;
172955418Seric 	}
173055418Seric 
173155418Seric 	for (; *pp != NULL; pp++)
173255418Seric 		*pp = pp[1];
173355418Seric }
173455418Seric 
173563962Seric #endif
173656215Seric /*
173756215Seric **  GETDTABLESIZE -- return number of file descriptors
173856215Seric **
173956215Seric **	Only on non-BSD systems
174056215Seric **
174156215Seric **	Parameters:
174256215Seric **		none
174356215Seric **
174456215Seric **	Returns:
174556215Seric **		size of file descriptor table
174656215Seric **
174756215Seric **	Side Effects:
174856215Seric **		none
174956215Seric */
175056215Seric 
175163787Seric #ifdef SOLARIS
175263787Seric # include <sys/resource.h>
175363787Seric #endif
175456215Seric 
175556215Seric int
175663787Seric getdtsize()
175756215Seric {
175863787Seric #ifdef RLIMIT_NOFILE
175963787Seric 	struct rlimit rl;
176063787Seric 
176163787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
176263787Seric 		return rl.rlim_cur;
176363787Seric #endif
176463787Seric 
176564031Seric # ifdef HASGETDTABLESIZE
176664031Seric 	return getdtablesize();
176764031Seric # else
176864031Seric #  ifdef _SC_OPEN_MAX
176958689Seric 	return sysconf(_SC_OPEN_MAX);
177063787Seric #  else
177156215Seric 	return NOFILE;
177263787Seric #  endif
177358689Seric # endif
177456215Seric }
177557631Seric /*
177657631Seric **  UNAME -- get the UUCP name of this system.
177757631Seric */
177857631Seric 
177957943Seric #ifndef HASUNAME
178057631Seric 
178157631Seric int
178257631Seric uname(name)
178357631Seric 	struct utsname *name;
178457631Seric {
178557631Seric 	FILE *file;
178657631Seric 	char *n;
178757631Seric 
178857631Seric 	name->nodename[0] = '\0';
178957631Seric 
179057661Seric 	/* try /etc/whoami -- one line with the node name */
179157631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
179257631Seric 	{
179357661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
179457631Seric 		(void) fclose(file);
179557661Seric 		n = strchr(name->nodename, '\n');
179657631Seric 		if (n != NULL)
179757631Seric 			*n = '\0';
179857631Seric 		if (name->nodename[0] != '\0')
179957631Seric 			return (0);
180057631Seric 	}
180157631Seric 
180257661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
180357631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
180457631Seric 	{
180557631Seric 		char buf[MAXLINE];
180657631Seric 
180757631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
180857631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
180957631Seric 					NODE_LENGTH, name->nodename) > 0)
181057631Seric 				break;
181157631Seric 		(void) fclose(file);
181257631Seric 		if (name->nodename[0] != '\0')
181357631Seric 			return (0);
181457631Seric 	}
181557631Seric 
181657631Seric #ifdef TRUST_POPEN
181757631Seric 	/*
181857631Seric 	**  Popen is known to have security holes.
181957631Seric 	*/
182057631Seric 
182157661Seric 	/* try uuname -l to return local name */
182257631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
182357631Seric 	{
182457661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
182557631Seric 		(void) pclose(file);
182657661Seric 		n = strchr(name, '\n');
182757631Seric 		if (n != NULL)
182857631Seric 			*n = '\0';
182957661Seric 		if (name->nodename[0] != '\0')
183057631Seric 			return (0);
183157631Seric 	}
183257631Seric #endif
183357631Seric 
183457631Seric 	return (-1);
183557631Seric }
183657943Seric #endif /* HASUNAME */
183758068Seric /*
183858068Seric **  INITGROUPS -- initialize groups
183958068Seric **
184058068Seric **	Stub implementation for System V style systems
184158068Seric */
184258068Seric 
184358068Seric #ifndef HASINITGROUPS
184458068Seric 
184558068Seric initgroups(name, basegid)
184658068Seric 	char *name;
184758068Seric 	int basegid;
184858068Seric {
184958068Seric 	return 0;
185058068Seric }
185158068Seric 
185258068Seric #endif
185358082Seric /*
185459289Seric **  SETSID -- set session id (for non-POSIX systems)
185559289Seric */
185659289Seric 
185759289Seric #ifndef HASSETSID
185859289Seric 
185963753Seric pid_t
186063753Seric setsid __P ((void))
186159289Seric {
186264155Seric #ifdef TIOCNOTTY
186364155Seric 	int fd;
186464155Seric 
186566743Seric 	fd = open("/dev/tty", O_RDWR, 0);
186664155Seric 	if (fd >= 0)
186764155Seric 	{
186864155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
186964155Seric 		(void) close(fd);
187064155Seric 	}
187164155Seric #endif /* TIOCNOTTY */
187264705Seric # ifdef SYS5SETPGRP
187363753Seric 	return setpgrp();
187463753Seric # else
187564155Seric 	return setpgid(0, getpid());
187659289Seric # endif
187759289Seric }
187859289Seric 
187959289Seric #endif
188059289Seric /*
188166748Seric **  FSYNC -- dummy fsync
188266748Seric */
188366748Seric 
188466748Seric #ifdef NEEDFSYNC
188566748Seric 
188666748Seric fsync(fd)
188766748Seric 	int fd;
188866748Seric {
188966748Seric # ifdef O_SYNC
189066748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
189166748Seric # else
189266748Seric 	/* nothing we can do */
189366748Seric 	return 0;
189466748Seric # endif
189566748Seric }
189666748Seric 
189766748Seric #endif
189866748Seric /*
189964718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
190064718Seric **
190164718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
190268508Seric **	instead of a long.  This patches things.  Only needed on versions
190368508Seric **	prior to 5.4.3.
190464718Seric */
190564718Seric 
190668508Seric #ifdef DGUX_5_4_2
190764718Seric 
190864718Seric #undef inet_addr
190964718Seric 
191064718Seric long
191164718Seric dgux_inet_addr(host)
191264718Seric 	char *host;
191364718Seric {
191464718Seric 	struct in_addr haddr;
191564718Seric 
191664718Seric 	haddr = inet_addr(host);
191764718Seric 	return haddr.s_addr;
191864718Seric }
191964718Seric 
192064718Seric #endif
192164718Seric /*
192263969Seric **  GETOPT -- for old systems or systems with bogus implementations
192363969Seric */
192463969Seric 
192563969Seric #ifdef NEEDGETOPT
192663969Seric 
192763969Seric /*
192863969Seric  * Copyright (c) 1985 Regents of the University of California.
192963969Seric  * All rights reserved.  The Berkeley software License Agreement
193063969Seric  * specifies the terms and conditions for redistribution.
193163969Seric  */
193263969Seric 
193363969Seric 
193463969Seric /*
193563969Seric ** this version hacked to add `atend' flag to allow state machine
193663969Seric ** to reset if invoked by the program to scan args for a 2nd time
193763969Seric */
193863969Seric 
193963969Seric #if defined(LIBC_SCCS) && !defined(lint)
194063969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
194164072Seric #endif /* LIBC_SCCS and not lint */
194263969Seric 
194363969Seric #include <stdio.h>
194463969Seric 
194563969Seric /*
194663969Seric  * get option letter from argument vector
194763969Seric  */
194865352Seric #ifdef _CONVEX_SOURCE
194968508Seric extern int	optind, opterr, optopt;
195068508Seric extern char	*optarg;
195165352Seric #else
195265352Seric int	opterr = 1;		/* if error message should be printed */
195365352Seric int	optind = 1;		/* index into parent argv vector */
195468470Seric int	optopt = 0;		/* character checked for validity */
195568470Seric char	*optarg = NULL;		/* argument associated with option */
195668508Seric #endif
195763969Seric 
195863969Seric #define BADCH	(int)'?'
195963969Seric #define EMSG	""
196063969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
196163969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
196263969Seric 
196363969Seric getopt(nargc,nargv,ostr)
196465160Seric 	int		nargc;
196565160Seric 	char *const	*nargv;
196665160Seric 	const char	*ostr;
196763969Seric {
196863969Seric 	static char	*place = EMSG;	/* option letter processing */
196963969Seric 	static char	atend = 0;
197063969Seric 	register char	*oli;		/* option letter list index */
197163969Seric 
197263969Seric 	if (atend) {
197363969Seric 		atend = 0;
197463969Seric 		place = EMSG;
197563969Seric 	}
197663969Seric 	if(!*place) {			/* update scanning pointer */
197764136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
197863969Seric 			atend++;
197963969Seric 			return(EOF);
198063969Seric 		}
198163969Seric 		if (*place == '-') {	/* found "--" */
198263969Seric 			++optind;
198363969Seric 			atend++;
198463969Seric 			return(EOF);
198563969Seric 		}
198663969Seric 	}				/* option letter okay? */
198764125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
198864136Seric 		if (!*place) ++optind;
198963969Seric 		tell(": illegal option -- ");
199063969Seric 	}
199163969Seric 	if (*++oli != ':') {		/* don't need argument */
199263969Seric 		optarg = NULL;
199363969Seric 		if (!*place) ++optind;
199463969Seric 	}
199563969Seric 	else {				/* need an argument */
199663969Seric 		if (*place) optarg = place;	/* no white space */
199763969Seric 		else if (nargc <= ++optind) {	/* no arg */
199863969Seric 			place = EMSG;
199963969Seric 			tell(": option requires an argument -- ");
200063969Seric 		}
200163969Seric 	 	else optarg = nargv[optind];	/* white space */
200263969Seric 		place = EMSG;
200363969Seric 		++optind;
200463969Seric 	}
200563969Seric 	return(optopt);			/* dump back option letter */
200663969Seric }
200763969Seric 
200863969Seric #endif
200963969Seric /*
201063969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
201163969Seric */
201263969Seric 
201363969Seric #ifdef NEEDVPRINTF
201463969Seric 
201563969Seric #define MAXARG	16
201663969Seric 
201763969Seric vfprintf(fp, fmt, ap)
201863969Seric 	FILE *	fp;
201963969Seric 	char *	fmt;
202063969Seric 	char **	ap;
202163969Seric {
202263969Seric 	char *	bp[MAXARG];
202363969Seric 	int	i = 0;
202463969Seric 
202563969Seric 	while (*ap && i < MAXARG)
202663969Seric 		bp[i++] = *ap++;
202763969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
202863969Seric 			 bp[4], bp[5], bp[6], bp[7],
202963969Seric 			 bp[8], bp[9], bp[10], bp[11],
203063969Seric 			 bp[12], bp[13], bp[14], bp[15]);
203163969Seric }
203263969Seric 
203363969Seric vsprintf(s, fmt, ap)
203463969Seric 	char *	s;
203563969Seric 	char *	fmt;
203663969Seric 	char **	ap;
203763969Seric {
203863969Seric 	char *	bp[MAXARG];
203963969Seric 	int	i = 0;
204063969Seric 
204163969Seric 	while (*ap && i < MAXARG)
204263969Seric 		bp[i++] = *ap++;
204363969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
204463969Seric 			bp[4], bp[5], bp[6], bp[7],
204563969Seric 			bp[8], bp[9], bp[10], bp[11],
204663969Seric 			bp[12], bp[13], bp[14], bp[15]);
204763969Seric }
204863969Seric 
204963969Seric #endif
205063969Seric /*
205165220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
205265208Seric **
205365211Seric **	Parameters:
205465211Seric **		shell -- the user's shell from /etc/passwd
205565211Seric **
205665211Seric **	Returns:
205765211Seric **		TRUE -- if it is ok to use this for unrestricted access.
205865211Seric **		FALSE -- if the shell is restricted.
205965208Seric */
206065208Seric 
206165587Seric #if !HASGETUSERSHELL
206265587Seric 
206365587Seric # ifndef _PATH_SHELLS
206465587Seric #  define _PATH_SHELLS	"/etc/shells"
206565587Seric # endif
206665587Seric 
206765587Seric char	*DefaultUserShells[] =
206865587Seric {
206968508Seric 	"/bin/sh",		/* standard shell */
207065587Seric 	"/usr/bin/sh",
207168508Seric 	"/bin/csh",		/* C shell */
207265587Seric 	"/usr/bin/csh",
207365587Seric #ifdef __hpux
207468889Seric # ifdef V4FS
207568889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
207668889Seric 	"/usr/bin/ksh",		/* Korn shell */
207768889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
207868889Seric 	"/usr/bin/pam",
207968889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
208068889Seric 	"/usr/bin/posix/sh",
208168889Seric # else
208268508Seric 	"/bin/rsh",		/* restricted Bourne shell */
208368508Seric 	"/bin/ksh",		/* Korn shell */
208468508Seric 	"/bin/rksh",		/* restricted Korn shell */
208565587Seric 	"/bin/pam",
208668508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
208765587Seric 	"/bin/posix/sh",
208868889Seric # endif
208965211Seric #endif
209068508Seric #ifdef _AIX3
209168508Seric 	"/bin/ksh",		/* Korn shell */
209268508Seric 	"/usr/bin/ksh",
209368508Seric 	"/bin/tsh",		/* trusted shell */
209468508Seric 	"/usr/bin/tsh",
209568508Seric 	"/bin/bsh",		/* Bourne shell */
209668508Seric 	"/usr/bin/bsh",
209768508Seric #endif
209865587Seric 	NULL
209965587Seric };
210065587Seric 
210165208Seric #endif
210265208Seric 
210365747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
210465747Seric 
210565211Seric bool
210665211Seric usershellok(shell)
210765211Seric 	char *shell;
210865208Seric {
210965211Seric #if HASGETUSERSHELL
211065211Seric 	register char *p;
211165211Seric 	extern char *getusershell();
211265208Seric 
211368457Seric 	if (shell == NULL || shell[0] == '\0')
211468457Seric 		return TRUE;
211568457Seric 
211665211Seric 	setusershell();
211765211Seric 	while ((p = getusershell()) != NULL)
211865747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
211965211Seric 			break;
212065211Seric 	endusershell();
212165211Seric 	return p != NULL;
212265211Seric #else
212365211Seric 	register FILE *shellf;
212465211Seric 	char buf[MAXLINE];
212565208Seric 
212668457Seric 	if (shell == NULL || shell[0] == '\0')
212768457Seric 		return TRUE;
212868457Seric 
212965211Seric 	shellf = fopen(_PATH_SHELLS, "r");
213065211Seric 	if (shellf == NULL)
213165211Seric 	{
213265211Seric 		/* no /etc/shells; see if it is one of the std shells */
213365587Seric 		char **d;
213465587Seric 
213565587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
213665587Seric 		{
213765587Seric 			if (strcmp(shell, *d) == 0)
213865587Seric 				return TRUE;
213965587Seric 		}
214065587Seric 		return FALSE;
214165211Seric 	}
214265208Seric 
214365211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
214465211Seric 	{
214565211Seric 		register char *p, *q;
214665208Seric 
214765211Seric 		p = buf;
214865211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
214965211Seric 			p++;
215065211Seric 		if (*p == '#' || *p == '\0')
215165208Seric 			continue;
215265211Seric 		q = p;
215365211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
215465211Seric 			p++;
215565211Seric 		*p = '\0';
215665747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
215765211Seric 		{
215865211Seric 			fclose(shellf);
215965211Seric 			return TRUE;
216065211Seric 		}
216165208Seric 	}
216265211Seric 	fclose(shellf);
216365211Seric 	return FALSE;
216465211Seric #endif
216565208Seric }
216665208Seric /*
216764311Seric **  FREESPACE -- see how much free space is on the queue filesystem
216858082Seric **
216958082Seric **	Only implemented if you have statfs.
217058082Seric **
217158082Seric **	Parameters:
217264311Seric **		dir -- the directory in question.
217364311Seric **		bsize -- a variable into which the filesystem
217464311Seric **			block size is stored.
217558082Seric **
217658082Seric **	Returns:
217764311Seric **		The number of bytes free on the queue filesystem.
217864311Seric **		-1 if the statfs call fails.
217964311Seric **
218064311Seric **	Side effects:
218164311Seric **		Puts the filesystem block size into bsize.
218258082Seric */
218358082Seric 
218465749Seric /* statfs types */
218565749Seric #define SFS_NONE	0	/* no statfs implementation */
218665749Seric #define SFS_USTAT	1	/* use ustat */
218765749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
218865749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
218965749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
219065749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
219166752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
219265749Seric 
219365749Seric #ifndef SFS_TYPE
219465749Seric # define SFS_TYPE	SFS_NONE
219558157Seric #endif
219658157Seric 
219765749Seric #if SFS_TYPE == SFS_USTAT
219858157Seric # include <ustat.h>
219958157Seric #endif
220065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
220165749Seric # include <sys/statfs.h>
220258082Seric #endif
220365749Seric #if SFS_TYPE == SFS_VFS
220465749Seric # include <sys/vfs.h>
220565749Seric #endif
220665749Seric #if SFS_TYPE == SFS_MOUNT
220765749Seric # include <sys/mount.h>
220865749Seric #endif
220966752Seric #if SFS_TYPE == SFS_STATVFS
221066752Seric # include <sys/statvfs.h>
221166752Seric #endif
221258082Seric 
221364311Seric long
221464311Seric freespace(dir, bsize)
221564311Seric 	char *dir;
221664311Seric 	long *bsize;
221758082Seric {
221865749Seric #if SFS_TYPE != SFS_NONE
221965749Seric # if SFS_TYPE == SFS_USTAT
222058153Seric 	struct ustat fs;
222158153Seric 	struct stat statbuf;
222258366Seric #  define FSBLOCKSIZE	DEV_BSIZE
222368695Seric #  define SFS_BAVAIL	f_tfree
222458157Seric # else
222558157Seric #  if defined(ultrix)
222658157Seric 	struct fs_data fs;
222768695Seric #   define SFS_BAVAIL	fd_bfreen
222868508Seric #   define FSBLOCKSIZE	1024L
222958153Seric #  else
223066752Seric #   if SFS_TYPE == SFS_STATVFS
223166752Seric 	struct statvfs fs;
223268508Seric #    define FSBLOCKSIZE	fs.f_frsize
223366752Seric #   else
223458082Seric 	struct statfs fs;
223566752Seric #    define FSBLOCKSIZE	fs.f_bsize
223663987Seric #   endif
223758153Seric #  endif
223858133Seric # endif
223968695Seric # ifndef SFS_BAVAIL
224068695Seric #  define SFS_BAVAIL f_bavail
224168508Seric # endif
224258082Seric 
224365749Seric # if SFS_TYPE == SFS_USTAT
224464311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
224558157Seric # else
224665749Seric #  if SFS_TYPE == SFS_4ARGS
224764311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
224858157Seric #  else
224968508Seric #   if SFS_TYPE == SFS_STATVFS
225068508Seric 	if (statvfs(dir, &fs) == 0)
225168508Seric #   else
225268508Seric #    if defined(ultrix)
225368499Seric 	if (statfs(dir, &fs) > 0)
225468508Seric #    else
225564311Seric 	if (statfs(dir, &fs) == 0)
225668508Seric #    endif
225758153Seric #   endif
225858133Seric #  endif
225958133Seric # endif
226058133Seric 	{
226164311Seric 		if (bsize != NULL)
226264311Seric 			*bsize = FSBLOCKSIZE;
226368695Seric 		return (fs.SFS_BAVAIL);
226464311Seric 	}
226564337Seric #endif
226664311Seric 	return (-1);
226764311Seric }
226864311Seric /*
226964311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
227064311Seric **
227164311Seric **	Only implemented if you have statfs.
227264311Seric **
227364311Seric **	Parameters:
227464311Seric **		msize -- the size to check against.  If zero, we don't yet
227564311Seric **		know how big the message will be, so just check for
227664311Seric **		a "reasonable" amount.
227764311Seric **
227864311Seric **	Returns:
227964311Seric **		TRUE if there is enough space.
228064311Seric **		FALSE otherwise.
228164311Seric */
228264311Seric 
228364311Seric bool
228464311Seric enoughspace(msize)
228564311Seric 	long msize;
228664311Seric {
228764311Seric 	long bfree, bsize;
228864311Seric 
228964311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
229064311Seric 	{
229158133Seric 		if (tTd(4, 80))
229264311Seric 			printf("enoughspace: no threshold\n");
229364311Seric 		return TRUE;
229464311Seric 	}
229564311Seric 
229664311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
229764311Seric 	{
229864311Seric 		if (tTd(4, 80))
229958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
230064311Seric 				bfree, msize);
230158333Seric 
230258333Seric 		/* convert msize to block count */
230364311Seric 		msize = msize / bsize + 1;
230458333Seric 		if (MinBlocksFree >= 0)
230558333Seric 			msize += MinBlocksFree;
230658333Seric 
230764311Seric 		if (bfree < msize)
230858090Seric 		{
230958090Seric #ifdef LOG
231058090Seric 			if (LogLevel > 0)
231164254Seric 				syslog(LOG_ALERT,
231264254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
231368508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
231468508Seric 					bfree,
231568508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
231668508Seric 					msize, QueueDir);
231758090Seric #endif
231858082Seric 			return FALSE;
231958090Seric 		}
232058082Seric 	}
232158082Seric 	else if (tTd(4, 80))
232258333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
232358333Seric 			MinBlocksFree, msize, errstring(errno));
232458082Seric 	return TRUE;
232558082Seric }
232658542Seric /*
232758542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
232858542Seric **
232958542Seric **	This looks at an errno value and tells if this is likely to
233058542Seric **	go away if retried later.
233158542Seric **
233258542Seric **	Parameters:
233358542Seric **		err -- the errno code to classify.
233458542Seric **
233558542Seric **	Returns:
233658542Seric **		TRUE if this is probably transient.
233758542Seric **		FALSE otherwise.
233858542Seric */
233958542Seric 
234058542Seric bool
234158542Seric transienterror(err)
234258542Seric 	int err;
234358542Seric {
234458542Seric 	switch (err)
234558542Seric 	{
234658542Seric 	  case EIO:			/* I/O error */
234758542Seric 	  case ENXIO:			/* Device not configured */
234858542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
234958542Seric 	  case ENOMEM:			/* Cannot allocate memory */
235058542Seric 	  case ENODEV:			/* Operation not supported by device */
235158542Seric 	  case ENFILE:			/* Too many open files in system */
235258542Seric 	  case EMFILE:			/* Too many open files */
235358542Seric 	  case ENOSPC:			/* No space left on device */
235458542Seric #ifdef ETIMEDOUT
235558542Seric 	  case ETIMEDOUT:		/* Connection timed out */
235658542Seric #endif
235758542Seric #ifdef ESTALE
235858542Seric 	  case ESTALE:			/* Stale NFS file handle */
235958542Seric #endif
236058542Seric #ifdef ENETDOWN
236158542Seric 	  case ENETDOWN:		/* Network is down */
236258542Seric #endif
236358542Seric #ifdef ENETUNREACH
236458542Seric 	  case ENETUNREACH:		/* Network is unreachable */
236558542Seric #endif
236658542Seric #ifdef ENETRESET
236758542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
236858542Seric #endif
236958542Seric #ifdef ECONNABORTED
237058542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
237158542Seric #endif
237258542Seric #ifdef ECONNRESET
237358542Seric 	  case ECONNRESET:		/* Connection reset by peer */
237458542Seric #endif
237558542Seric #ifdef ENOBUFS
237658542Seric 	  case ENOBUFS:			/* No buffer space available */
237758542Seric #endif
237858542Seric #ifdef ESHUTDOWN
237958542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
238058542Seric #endif
238158542Seric #ifdef ECONNREFUSED
238258542Seric 	  case ECONNREFUSED:		/* Connection refused */
238358542Seric #endif
238458542Seric #ifdef EHOSTDOWN
238558542Seric 	  case EHOSTDOWN:		/* Host is down */
238658542Seric #endif
238758542Seric #ifdef EHOSTUNREACH
238858542Seric 	  case EHOSTUNREACH:		/* No route to host */
238958542Seric #endif
239058542Seric #ifdef EDQUOT
239158542Seric 	  case EDQUOT:			/* Disc quota exceeded */
239258542Seric #endif
239358542Seric #ifdef EPROCLIM
239458542Seric 	  case EPROCLIM:		/* Too many processes */
239558542Seric #endif
239658542Seric #ifdef EUSERS
239758542Seric 	  case EUSERS:			/* Too many users */
239858542Seric #endif
239958542Seric #ifdef EDEADLK
240058542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
240158542Seric #endif
240258542Seric #ifdef EISCONN
240358542Seric 	  case EISCONN:			/* Socket already connected */
240458542Seric #endif
240558542Seric #ifdef EINPROGRESS
240658542Seric 	  case EINPROGRESS:		/* Operation now in progress */
240758542Seric #endif
240858542Seric #ifdef EALREADY
240958542Seric 	  case EALREADY:		/* Operation already in progress */
241058542Seric #endif
241158542Seric #ifdef EADDRINUSE
241258542Seric 	  case EADDRINUSE:		/* Address already in use */
241358542Seric #endif
241458542Seric #ifdef EADDRNOTAVAIL
241558542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
241658542Seric #endif
241766018Seric #ifdef ETXTBSY
241866017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
241966017Seric #endif
242063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
242158542Seric 	  case ENOSR:			/* Out of streams resources */
242258542Seric #endif
242358542Seric 		return TRUE;
242458542Seric 	}
242558542Seric 
242658542Seric 	/* nope, must be permanent */
242758542Seric 	return FALSE;
242858542Seric }
242958689Seric /*
243064035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
243158689Seric **
243258689Seric **	Parameters:
243358689Seric **		fd -- the file descriptor of the file.
243458689Seric **		filename -- the file name (for error messages).
243564335Seric **		ext -- the filename extension.
243658689Seric **		type -- type of the lock.  Bits can be:
243758689Seric **			LOCK_EX -- exclusive lock.
243858689Seric **			LOCK_NB -- non-blocking.
243958689Seric **
244058689Seric **	Returns:
244158689Seric **		TRUE if the lock was acquired.
244258689Seric **		FALSE otherwise.
244358689Seric */
244458689Seric 
244558689Seric bool
244664335Seric lockfile(fd, filename, ext, type)
244758689Seric 	int fd;
244858689Seric 	char *filename;
244964335Seric 	char *ext;
245058689Seric 	int type;
245158689Seric {
245265830Seric # if !HASFLOCK
245358689Seric 	int action;
245458689Seric 	struct flock lfd;
245564335Seric 
245664335Seric 	if (ext == NULL)
245764335Seric 		ext = "";
245864035Seric 
245963983Seric 	bzero(&lfd, sizeof lfd);
246059447Seric 	if (bitset(LOCK_UN, type))
246159447Seric 		lfd.l_type = F_UNLCK;
246259447Seric 	else if (bitset(LOCK_EX, type))
246358689Seric 		lfd.l_type = F_WRLCK;
246458689Seric 	else
246558689Seric 		lfd.l_type = F_RDLCK;
246658689Seric 
246758689Seric 	if (bitset(LOCK_NB, type))
246858689Seric 		action = F_SETLK;
246958689Seric 	else
247058689Seric 		action = F_SETLKW;
247158689Seric 
247264264Seric 	if (tTd(55, 60))
247364335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
247464335Seric 			filename, ext, action, lfd.l_type);
247564264Seric 
247658689Seric 	if (fcntl(fd, action, &lfd) >= 0)
247764264Seric 	{
247864264Seric 		if (tTd(55, 60))
247964264Seric 			printf("SUCCESS\n");
248058689Seric 		return TRUE;
248164264Seric 	}
248258689Seric 
248364264Seric 	if (tTd(55, 60))
248464264Seric 		printf("(%s) ", errstring(errno));
248564264Seric 
248664136Seric 	/*
248764136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
248864136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
248964136Seric 	**  as type "tmp" (that is, served from swap space), the
249064136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
249164136Seric 	**  Since this is fairly common during testing, we will assume
249264136Seric 	**  that this indicates that the lock is successfully grabbed.
249364136Seric 	*/
249464136Seric 
249564136Seric 	if (errno == EINVAL)
249664264Seric 	{
249764264Seric 		if (tTd(55, 60))
249864264Seric 			printf("SUCCESS\n");
249964136Seric 		return TRUE;
250064264Seric 	}
250164136Seric 
250258689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
250364378Seric 	{
250464378Seric 		int omode = -1;
250564378Seric #  ifdef F_GETFL
250664378Seric 		int oerrno = errno;
250764378Seric 
250864378Seric 		(void) fcntl(fd, F_GETFL, &omode);
250964378Seric 		errno = oerrno;
251064378Seric #  endif
251164378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
251264378Seric 			filename, ext, fd, type, omode, geteuid());
251364378Seric 	}
251458689Seric # else
251564335Seric 	if (ext == NULL)
251664335Seric 		ext = "";
251764335Seric 
251864264Seric 	if (tTd(55, 60))
251964335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
252064264Seric 
252158689Seric 	if (flock(fd, type) >= 0)
252264264Seric 	{
252364264Seric 		if (tTd(55, 60))
252464264Seric 			printf("SUCCESS\n");
252558689Seric 		return TRUE;
252664264Seric 	}
252758689Seric 
252864264Seric 	if (tTd(55, 60))
252964264Seric 		printf("(%s) ", errstring(errno));
253064264Seric 
253158689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
253264378Seric 	{
253364378Seric 		int omode = -1;
253464378Seric #  ifdef F_GETFL
253564378Seric 		int oerrno = errno;
253664378Seric 
253764378Seric 		(void) fcntl(fd, F_GETFL, &omode);
253864378Seric 		errno = oerrno;
253964378Seric #  endif
254064378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
254164378Seric 			filename, ext, fd, type, omode, geteuid());
254264386Seric 	}
254358689Seric # endif
254464264Seric 	if (tTd(55, 60))
254564264Seric 		printf("FAIL\n");
254658689Seric 	return FALSE;
254758689Seric }
254864556Seric /*
254965948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
255065948Seric **
255165948Seric **	Parameters:
255265948Seric **		fd -- the file descriptor to check.
255365948Seric **
255465948Seric **	Returns:
255565948Seric **		TRUE -- if only root can chown the file to an arbitrary
255665948Seric **			user.
255765948Seric **		FALSE -- if an arbitrary user can give away a file.
255865948Seric */
255965948Seric 
256065948Seric bool
256165948Seric chownsafe(fd)
256265948Seric 	int fd;
256365948Seric {
256465950Seric #ifdef __hpux
256565948Seric 	char *s;
256665948Seric 	int tfd;
256765948Seric 	uid_t o_uid, o_euid;
256865948Seric 	gid_t o_gid, o_egid;
256965948Seric 	bool rval;
257065948Seric 	struct stat stbuf;
257165948Seric 
257265948Seric 	o_uid = getuid();
257365948Seric 	o_euid = geteuid();
257465948Seric 	o_gid = getgid();
257565948Seric 	o_egid = getegid();
257665959Seric 	fstat(fd, &stbuf);
257765948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
257865948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
257965948Seric 	s = tmpnam(NULL);
258065948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
258165974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
258265948Seric 	close(tfd);
258365948Seric 	unlink(s);
258465948Seric 	setreuid(o_uid, o_euid);
258565948Seric 	setresgid(o_gid, o_egid, -1);
258665948Seric 	return rval;
258765948Seric #else
258866088Seric # ifdef _POSIX_CHOWN_RESTRICTED
258966088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
259066088Seric 	return FALSE;
259166088Seric #  else
259266088Seric 	return TRUE;
259366088Seric #  endif
259466088Seric # else
259566088Seric #  ifdef _PC_CHOWN_RESTRICTED
259668508Seric 	int rval;
259768508Seric 
259868508Seric 	/*
259968508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
260068508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
260168508Seric 	**  the call.  This heuristic checks for that.
260268508Seric 	*/
260368508Seric 
260468508Seric 	errno = 0;
260568508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
260668508Seric 	if (errno == 0)
260768508Seric 		return rval > 0;
260868508Seric #  endif
260968508Seric #  ifdef BSD
261068508Seric 	return TRUE;
261168499Seric #  else
261265948Seric 	return FALSE;
261365948Seric #  endif
261465948Seric # endif
261565948Seric #endif
261665948Seric }
261765948Seric /*
261868508Seric **  RESETLIMITS -- reset system controlled resource limits
261968508Seric **
262068508Seric **	This is to avoid denial-of-service attacks
262168508Seric **
262268508Seric **	Parameters:
262368508Seric **		none
262468508Seric **
262568508Seric **	Returns:
262668508Seric **		none
262768508Seric */
262868508Seric 
262968508Seric #if HASSETRLIMIT
263068508Seric # include <sys/resource.h>
263168508Seric #endif
263268508Seric 
263368508Seric void
263468508Seric resetlimits()
263568508Seric {
263668508Seric #if HASSETRLIMIT
263768508Seric 	struct rlimit lim;
263868508Seric 
263968508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
264068508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
264168508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
264268508Seric #else
264368508Seric # if HASULIMIT
264468508Seric 	(void) ulimit(2, 0x3fffff);
264568508Seric # endif
264668508Seric #endif
264768508Seric }
264868508Seric /*
264964556Seric **  GETCFNAME -- return the name of the .cf file.
265064556Seric **
265164556Seric **	Some systems (e.g., NeXT) determine this dynamically.
265264556Seric */
265364556Seric 
265464556Seric char *
265564556Seric getcfname()
265664556Seric {
265768842Seric 	int i;
265868842Seric 	static char cbuf[200];
265968842Seric 
266064556Seric 	if (ConfFile != NULL)
266164556Seric 		return ConfFile;
266265214Seric #ifdef NETINFO
266365214Seric 	{
266465214Seric 		extern char *ni_propval();
266565214Seric 		char *cflocation;
266665214Seric 
266768508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
266868508Seric 					"sendmail.cf", '\0');
266965214Seric 		if (cflocation != NULL)
267065214Seric 			return cflocation;
267165214Seric 	}
267265214Seric #endif
267368842Seric 
267469396Seric #ifdef TRY_VERSIONED_CF_NAME
267568842Seric 	/*
267668842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
267768842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
267868842Seric 	**
267968842Seric 	**	I suppose it should really try a search path here --
268068842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
268168842Seric 	**	/usr/lib/sendmail.cf, and so forth.
268268842Seric 	*/
268368842Seric 
268468842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
268568842Seric 	i = strlen(cbuf);
268668842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
268768842Seric 	{
268868842Seric 		char *p;
268968842Seric 		extern char Version[];
269068842Seric 
269168842Seric 		strcpy(&cbuf[i - 2], Version);
269268842Seric 		p = strchr(&cbuf[i - 2], '/');
269368842Seric 		if (p != NULL)
269468842Seric 			*p = '\0';
269568842Seric 		p = &cbuf[strlen(cbuf)];
269668842Seric 		do
269768842Seric 		{
269868842Seric 			int fd;
269968842Seric 
270068842Seric 			strcpy(p, ".cf");
270168842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
270268842Seric 			{
270368842Seric 				close(fd);
270468842Seric 				return cbuf;
270568842Seric 			}
270668842Seric 			*p = '\0';
270768842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
270868842Seric 	}
270969396Seric #endif
271064556Seric 	return _PATH_SENDMAILCF;
271164556Seric }
271264718Seric /*
271364718Seric **  SETVENDOR -- process vendor code from V configuration line
271464718Seric **
271564718Seric **	Parameters:
271664718Seric **		vendor -- string representation of vendor.
271764718Seric **
271864718Seric **	Returns:
271964718Seric **		TRUE -- if ok.
272064718Seric **		FALSE -- if vendor code could not be processed.
272164926Seric **
272264926Seric **	Side Effects:
272364926Seric **		It is reasonable to set mode flags here to tweak
272464926Seric **		processing in other parts of the code if necessary.
272564926Seric **		For example, if you are a vendor that uses $%y to
272664926Seric **		indicate YP lookups, you could enable that here.
272764718Seric */
272864718Seric 
272964718Seric bool
273064718Seric setvendor(vendor)
273164718Seric 	char *vendor;
273264718Seric {
273364926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
273468508Seric 	{
273568508Seric 		VendorCode = VENDOR_BERKELEY;
273664926Seric 		return TRUE;
273768508Seric 	}
273864926Seric 
273964926Seric 	/* add vendor extensions here */
274064926Seric 
274168508Seric #ifdef SUN_EXTENSIONS
274268508Seric 	if (strcasecmp(vendor, "Sun") == 0)
274368508Seric 	{
274468508Seric 		VendorCode = VENDOR_SUN;
274568508Seric 		return TRUE;
274668508Seric 	}
274768508Seric #endif
274868508Seric 
274964926Seric 	return FALSE;
275064718Seric }
275164816Seric /*
275264816Seric **  STRTOL -- convert string to long integer
275364816Seric **
275464816Seric **	For systems that don't have it in the C library.
275566161Seric **
275666161Seric **	This is taken verbatim from the 4.4-Lite C library.
275764816Seric */
275864816Seric 
275964816Seric #ifdef NEEDSTRTOL
276064816Seric 
276166161Seric #if defined(LIBC_SCCS) && !defined(lint)
276266161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
276366161Seric #endif /* LIBC_SCCS and not lint */
276466161Seric 
276566161Seric #include <limits.h>
276666161Seric 
276766161Seric /*
276866161Seric  * Convert a string to a long integer.
276966161Seric  *
277066161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
277166161Seric  * alphabets and digits are each contiguous.
277266161Seric  */
277366161Seric 
277464816Seric long
277566161Seric strtol(nptr, endptr, base)
277666161Seric 	const char *nptr;
277766161Seric 	char **endptr;
277866161Seric 	register int base;
277964816Seric {
278066161Seric 	register const char *s = nptr;
278166161Seric 	register unsigned long acc;
278266161Seric 	register int c;
278366161Seric 	register unsigned long cutoff;
278466161Seric 	register int neg = 0, any, cutlim;
278564816Seric 
278666161Seric 	/*
278766161Seric 	 * Skip white space and pick up leading +/- sign if any.
278866161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
278966161Seric 	 * assume decimal; if base is already 16, allow 0x.
279066161Seric 	 */
279166161Seric 	do {
279266161Seric 		c = *s++;
279366161Seric 	} while (isspace(c));
279466161Seric 	if (c == '-') {
279566161Seric 		neg = 1;
279666161Seric 		c = *s++;
279766161Seric 	} else if (c == '+')
279866161Seric 		c = *s++;
279966161Seric 	if ((base == 0 || base == 16) &&
280066161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
280166161Seric 		c = s[1];
280266161Seric 		s += 2;
280366161Seric 		base = 16;
280466161Seric 	}
280566161Seric 	if (base == 0)
280666161Seric 		base = c == '0' ? 8 : 10;
280764816Seric 
280866161Seric 	/*
280966161Seric 	 * Compute the cutoff value between legal numbers and illegal
281066161Seric 	 * numbers.  That is the largest legal value, divided by the
281166161Seric 	 * base.  An input number that is greater than this value, if
281266161Seric 	 * followed by a legal input character, is too big.  One that
281366161Seric 	 * is equal to this value may be valid or not; the limit
281466161Seric 	 * between valid and invalid numbers is then based on the last
281566161Seric 	 * digit.  For instance, if the range for longs is
281666161Seric 	 * [-2147483648..2147483647] and the input base is 10,
281766161Seric 	 * cutoff will be set to 214748364 and cutlim to either
281866161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
281966161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
282066161Seric 	 * the number is too big, and we will return a range error.
282166161Seric 	 *
282266161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
282366161Seric 	 * overflow.
282466161Seric 	 */
282566161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
282666161Seric 	cutlim = cutoff % (unsigned long)base;
282766161Seric 	cutoff /= (unsigned long)base;
282866161Seric 	for (acc = 0, any = 0;; c = *s++) {
282966161Seric 		if (isdigit(c))
283066161Seric 			c -= '0';
283166161Seric 		else if (isalpha(c))
283266161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
283366161Seric 		else
283466161Seric 			break;
283566161Seric 		if (c >= base)
283666161Seric 			break;
283766161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
283866161Seric 			any = -1;
283966161Seric 		else {
284066161Seric 			any = 1;
284166161Seric 			acc *= base;
284266161Seric 			acc += c;
284364816Seric 		}
284464816Seric 	}
284566161Seric 	if (any < 0) {
284666161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
284766161Seric 		errno = ERANGE;
284866161Seric 	} else if (neg)
284966161Seric 		acc = -acc;
285066161Seric 	if (endptr != 0)
285166161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
285266161Seric 	return (acc);
285364816Seric }
285464816Seric 
285564816Seric #endif
285664841Seric /*
285768693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
285864841Seric **
285968693Seric **	Some operating systems have wierd problems with the gethostbyXXX
286068693Seric **	routines.  For example, Solaris versions at least through 2.3
286168693Seric **	don't properly deliver a canonical h_name field.  This tries to
286268693Seric **	work around these problems.
286364841Seric */
286464841Seric 
286564841Seric struct hostent *
286668693Seric sm_gethostbyname(name)
286768707Seric 	char *name;
286864841Seric {
286968693Seric #if defined(SOLARIS) && SOLARIS < 204
287068693Seric 	extern int h_errno;
287168693Seric 
287268508Seric # if SOLARIS == 203
287364841Seric 	static struct hostent hp;
287464841Seric 	static char buf[1000];
287564841Seric 	extern struct hostent *_switch_gethostbyname_r();
287664841Seric 
287764841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
287864841Seric # else
287965009Seric 	extern struct hostent *__switch_gethostbyname();
288065009Seric 
288164841Seric 	return __switch_gethostbyname(name);
288264841Seric # endif
288368693Seric #else
288468693Seric 	return gethostbyname(name);
288568693Seric #endif
288664841Seric }
288764841Seric 
288864841Seric struct hostent *
288968693Seric sm_gethostbyaddr(addr, len, type)
289068707Seric 	char *addr;
289164841Seric 	int len;
289264841Seric 	int type;
289364841Seric {
289468693Seric #if defined(SOLARIS) && SOLARIS < 204
289568693Seric 	extern int h_errno;
289668693Seric 
289768508Seric # if SOLARIS == 203
289864841Seric 	static struct hostent hp;
289964841Seric 	static char buf[1000];
290064841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
290164841Seric 
290264841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
290364841Seric # else
290465009Seric 	extern struct hostent *__switch_gethostbyaddr();
290565009Seric 
290664841Seric 	return __switch_gethostbyaddr(addr, len, type);
290764841Seric # endif
290868693Seric #else
290968693Seric 	return gethostbyaddr(addr, len, type);
291068693Seric #endif
291164841Seric }
291268693Seric /*
291368693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
291468693Seric */
291564841Seric 
291668693Seric struct passwd *
291768693Seric sm_getpwnam(user)
291868704Seric 	char *user;
291968693Seric {
292068693Seric 	return getpwnam(user);
292168693Seric }
292268693Seric 
292368693Seric struct passwd *
292468693Seric sm_getpwuid(uid)
292568693Seric 	uid_t uid;
292668693Seric {
292768693Seric 	return getpwuid(uid);
292868693Seric }
292965214Seric /*
293065214Seric **  NI_PROPVAL -- netinfo property value lookup routine
293165214Seric **
293265214Seric **	Parameters:
293368508Seric **		keydir -- the Netinfo directory name in which to search
293468508Seric **			for the key.
293568508Seric **		keyprop -- the name of the property in which to find the
293668508Seric **			property we are interested.  Defaults to "name".
293768508Seric **		keyval -- the value for which we are really searching.
293868508Seric **		valprop -- the property name for the value in which we
293968508Seric **			are interested.
294068508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
294168508Seric **			we should return a string separated by this
294268508Seric **			character.
294365214Seric **
294465214Seric **	Returns:
294565214Seric **		NULL -- if:
294665214Seric **			1. the directory is not found
294765214Seric **			2. the property name is not found
294865214Seric **			3. the property contains multiple values
294965214Seric **			4. some error occured
295065214Seric **		else -- the location of the config file.
295165214Seric **
295268508Seric **	Example:
295368508Seric **		To search for an alias value, use:
295468508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
295568508Seric **
295665214Seric **	Notes:
295765214Seric **      	Caller should free the return value of ni_proval
295865214Seric */
295965214Seric 
296065214Seric #ifdef NETINFO
296165214Seric 
296265214Seric # include <netinfo/ni.h>
296365214Seric 
296465214Seric # define LOCAL_NETINFO_DOMAIN    "."
296565214Seric # define PARENT_NETINFO_DOMAIN   ".."
296665214Seric # define MAX_NI_LEVELS           256
296765214Seric 
296865214Seric char *
296968508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
297068508Seric 	char *keydir;
297168508Seric 	char *keyprop;
297268508Seric 	char *keyval;
297368508Seric 	char *valprop;
297468508Seric 	char sepchar;
297565214Seric {
297665820Seric 	char *propval = NULL;
297765214Seric 	int i;
297868508Seric 	int j, alen;
297965214Seric 	void *ni = NULL;
298065214Seric 	void *lastni = NULL;
298165214Seric 	ni_status nis;
298265214Seric 	ni_id nid;
298365214Seric 	ni_namelist ninl;
298468508Seric 	register char *p;
298568508Seric 	char keybuf[1024];
298665214Seric 
298765214Seric 	/*
298868508Seric 	**  Create the full key from the two parts.
298968508Seric 	**
299068508Seric 	**	Note that directory can end with, e.g., "name=" to specify
299168508Seric 	**	an alternate search property.
299268508Seric 	*/
299368508Seric 
299468508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
299568508Seric 	if (keyprop != NULL)
299668508Seric 		i += strlen(keyprop) + 1;
299768508Seric 	if (i > sizeof keybuf)
299868508Seric 		return NULL;
299968508Seric 	strcpy(keybuf, keydir);
300068508Seric 	strcat(keybuf, "/");
300168508Seric 	if (keyprop != NULL)
300268508Seric 	{
300368508Seric 		strcat(keybuf, keyprop);
300468508Seric 		strcat(keybuf, "=");
300568508Seric 	}
300668508Seric 	strcat(keybuf, keyval);
300768508Seric 
300868508Seric 	/*
300965214Seric 	**  If the passed directory and property name are found
301065214Seric 	**  in one of netinfo domains we need to search (starting
301165214Seric 	**  from the local domain moving all the way back to the
301265214Seric 	**  root domain) set propval to the property's value
301365214Seric 	**  and return it.
301465214Seric 	*/
301565214Seric 
301665214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
301765214Seric 	{
301865214Seric 		if (i == 0)
301965214Seric 		{
302065214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
302165214Seric 		}
302265214Seric 		else
302365214Seric 		{
302465214Seric 			if (lastni != NULL)
302565214Seric 				ni_free(lastni);
302665214Seric 			lastni = ni;
302765214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
302865214Seric 		}
302965214Seric 
303065214Seric 		/*
303165214Seric 		**  Don't bother if we didn't get a handle on a
303265214Seric 		**  proper domain.  This is not necessarily an error.
303365214Seric 		**  We would get a positive ni_status if, for instance
303465214Seric 		**  we never found the directory or property and tried
303565214Seric 		**  to open the parent of the root domain!
303665214Seric 		*/
303765214Seric 
303865214Seric 		if (nis != 0)
303965214Seric 			break;
304065214Seric 
304165214Seric 		/*
304265214Seric 		**  Find the path to the server information.
304365214Seric 		*/
304465214Seric 
304568508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
304665214Seric 			continue;
304765214Seric 
304865214Seric 		/*
304968508Seric 		**  Find associated value information.
305065214Seric 		*/
305165214Seric 
305268508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
305365214Seric 			continue;
305465214Seric 
305565214Seric 		/*
305668508Seric 		**  See if we have an acceptable number of values.
305765214Seric 		*/
305865214Seric 
305968508Seric 		if (ninl.ni_namelist_len <= 0)
306068508Seric 			continue;
306168508Seric 
306268508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
306365214Seric 		{
306468508Seric 			ni_namelist_free(&ninl);
306568508Seric 			continue;
306665214Seric 		}
306768508Seric 
306868508Seric 		/*
306968508Seric 		**  Calculate number of bytes needed and build result
307068508Seric 		*/
307168508Seric 
307268508Seric 		alen = 1;
307368508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
307468508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
307568508Seric 		propval = p = xalloc(alen);
307668508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
307768508Seric 		{
307868508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
307968508Seric 			p += strlen(p);
308068508Seric 			*p++ = sepchar;
308168508Seric 		}
308268508Seric 		*--p = '\0';
308368508Seric 
308468508Seric 		ni_namelist_free(&ninl);
308565214Seric 	}
308665214Seric 
308765214Seric 	/*
308865214Seric 	**  Clean up.
308965214Seric 	*/
309065214Seric 
309165214Seric 	if (ni != NULL)
309265214Seric 		ni_free(ni);
309365214Seric 	if (lastni != NULL && ni != lastni)
309465214Seric 		ni_free(lastni);
309565214Seric 
309665214Seric 	return propval;
309765214Seric }
309865214Seric 
309965214Seric #endif /* NETINFO */
310065504Seric /*
310165504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
310265504Seric **
310365504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
310465504Seric **	syslog succeeds during interrupt handlers.
310565504Seric */
310665504Seric 
310765504Seric #ifdef __hpux
310865504Seric 
310965504Seric # define MAXSYSLOGTRIES	100
311065504Seric # undef syslog
311165504Seric 
311265504Seric # ifdef __STDC__
311365504Seric hard_syslog(int pri, char *msg, ...)
311465504Seric # else
311565504Seric hard_syslog(pri, msg, va_alist)
311665504Seric 	int pri;
311765504Seric 	char *msg;
311865504Seric 	va_dcl
311965504Seric # endif
312065504Seric {
312165504Seric 	int i;
312265504Seric 	char buf[SYSLOG_BUFSIZE * 2];
312365504Seric 	VA_LOCAL_DECL;
312465504Seric 
312565504Seric 	VA_START(msg);
312665504Seric 	vsprintf(buf, msg, ap);
312765504Seric 	VA_END;
312865504Seric 
312965504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
313065504Seric 		continue;
313165504Seric }
313265504Seric 
313365504Seric #endif
313468696Seric /*
313568696Seric **  LOCAL_HOSTNAME_LENGTH
313668696Seric **
313768696Seric **	This is required to get sendmail to compile against BIND 4.9.x
313868696Seric **	on Ultrix.
313968696Seric */
314068696Seric 
314168696Seric #if defined(ultrix) && NAMED_BIND
314268696Seric 
314368696Seric # include <resolv.h>
314468696Seric # if __RES >= 19931104
314568696Seric 
314668696Seric int
314768696Seric local_hostname_length(hostname)
314868696Seric 	char *hostname;
314968696Seric {
315068696Seric 	int len_host, len_domain;
315168696Seric 
315268696Seric 	if (!*_res.defdname)
315368696Seric 		res_init();
315468696Seric 	len_host = strlen(hostname);
315568696Seric 	len_domain = strlen(_res.defdname);
315668696Seric 	if (len_host > len_domain &&
315768696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
315868696Seric 	    hostname[len_host - len_domain - 1] == '.')
315968696Seric 		return len_host - len_domain - 1;
316068696Seric 	else
316168696Seric 		return 0;
316268696Seric }
316368696Seric 
316468696Seric # endif
316568696Seric #endif
316668741Seric /*
316768741Seric **  Compile-Time options
316868741Seric */
316968741Seric 
317068741Seric char	*CompileOptions[] =
317168741Seric {
317268741Seric #if HESIOD
317368741Seric 	"HESIOD",
317468741Seric #endif
317568741Seric #if LOG
317668741Seric 	"LOG",
317768741Seric #endif
317868741Seric #if MATCHGECOS
317968741Seric 	"MATCHGECOS",
318068741Seric #endif
318168741Seric #if NAMED_BIND
318268741Seric 	"NAMED_BIND",
318368741Seric #endif
318468741Seric #if NDBM
318568741Seric 	"NDBM",
318668741Seric #endif
318768741Seric #if NETINET
318868741Seric 	"NETINET",
318968741Seric #endif
319068741Seric #if NETINFO
319168741Seric 	"NETINFO",
319268741Seric #endif
319368741Seric #if NETISO
319468741Seric 	"NETISO",
319568741Seric #endif
319668741Seric #if NETNS
319768741Seric 	"NETNS",
319868741Seric #endif
319968741Seric #if NETUNIX
320068741Seric 	"NETUNIX",
320168741Seric #endif
320268741Seric #if NETX25
320368741Seric 	"NETX25",
320468741Seric #endif
320568741Seric #if NEWDB
320668741Seric 	"NEWDB",
320768741Seric #endif
320868741Seric #if NIS
320968741Seric 	"NIS",
321068741Seric #endif
321168741Seric #if SCANF
321268741Seric 	"SCANF",
321368741Seric #endif
321468741Seric #if SUID_ROOT_FILES_OK
321568741Seric 	"SUID_ROOT_FILES_OK",
321668741Seric #endif
321768741Seric #if USERDB
321868741Seric 	"USERDB",
321968741Seric #endif
322068741Seric #if XDEBUG
322168741Seric 	"XDEBUG",
322268741Seric #endif
322368741Seric #if XLA
322468741Seric 	"XLA",
322568741Seric #endif
322668741Seric 	NULL
322768741Seric };
3228