xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69473)
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*69473Seric static char sccsid[] = "@(#)conf.c	8.163 (Berkeley) 05/15/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 */
105669462Seric #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
105769462Seric #define LA_DGUX		9	/* special DGUX implementation */
105869462Seric #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 
106569462Seric #ifndef FSHIFT
106669462Seric # if defined(unixpc)
106769462Seric #  define FSHIFT	5
106869462Seric # endif
106969462Seric 
107069462Seric # if defined(__alpha) || defined(IRIX)
107169462Seric #  define FSHIFT	10
107269462Seric # endif
107369462Seric 
107469462Seric # if defined(_AIX3)
107569462Seric #  define FSHIFT	16
107669462Seric # endif
107769462Seric #endif
107869462Seric 
107969462Seric #ifndef FSHIFT
108069462Seric # define FSHIFT		8
108169462Seric #endif
108269462Seric 
108369462Seric #ifndef FSCALE
108469462Seric # define FSCALE		(1 << FSHIFT)
108569462Seric #endif
108669462Seric 
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 
111269462Seric 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
113169462Seric 	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);
114569462Seric 
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 	}
117869462Seric # 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);
118969462Seric # 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));
120069462Seric # endif
120114872Seric }
120214872Seric 
120369462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
120451773Seric 
120569462Seric #if LA_TYPE == LA_READKSYM
120664718Seric 
1207*69473Seric # include <sys/ksym.h>
1208*69473Seric 
120969462Seric getla()
121069462Seric {
121169462Seric 	static int kmem = -1;
121269462Seric 	long avenrun[3];
121369462Seric 	extern int errno;
121469462Seric 	struct mioc_rksym mirk;
121564718Seric 
121669462Seric 	if (kmem < 0)
121769462Seric 	{
121869462Seric 		kmem = open("/dev/kmem", 0, 0);
121969462Seric 		if (kmem < 0)
122069462Seric 		{
122169462Seric 			if (tTd(3, 1))
122269462Seric 				printf("getla: open(/dev/kmem): %s\n",
122369462Seric 					errstring(errno));
122469462Seric 			return (-1);
122569462Seric 		}
122669462Seric 		(void) fcntl(kmem, F_SETFD, 1);
122769462Seric 	}
122869462Seric 	mirk.mirk_symname = LA_AVENRUN;
122969462Seric 	mirk.mirk_buf = avenrun;
123069462Seric 	mirk.mirk_buflen = sizeof(avenrun);
123169462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
123269462Seric 	{
123369462Seric 		if (tTd(3, 1))
123469462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
123569462Seric 				errstring(errno));
123669462Seric 		return -1;
123769462Seric 	}
123869462Seric 	if (tTd(3, 5))
123969462Seric 	{
124069462Seric 		printf("getla: avenrun = %d", avenrun[0]);
124169462Seric 		if (tTd(3, 15))
124269462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
124369462Seric 		printf("\n");
124469462Seric 	}
124569462Seric 	if (tTd(3, 1))
124669462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
124769462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
124869462Seric }
124969462Seric 
125069462Seric #endif /* LA_TYPE == LA_READKSYM */
125169462Seric 
125269462Seric #if LA_TYPE == LA_DGUX
125369462Seric 
125469462Seric # include <sys/dg_sys_info.h>
125569462Seric 
125668508Seric int
125768508Seric getla()
125864718Seric {
125964718Seric 	struct dg_sys_info_load_info load_info;
126064718Seric 
126164718Seric 	dg_sys_info((long *)&load_info,
126264718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
126364718Seric 
126468508Seric         if (tTd(3, 1))
126568508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
126668508Seric 
126764718Seric 	return((int) (load_info.one_minute + 0.5));
126864718Seric }
126964718Seric 
127069462Seric #endif /* LA_TYPE == LA_DGUX */
127164718Seric 
127269462Seric #if LA_TYPE == LA_HPUX
127369462Seric 
127468869Seric struct pst_dynamic;
127568869Seric 
127669462Seric # include <sys/param.h>
127769462Seric # include <sys/pstat.h>
127868508Seric 
127968508Seric int
128051773Seric getla()
128151773Seric {
128268508Seric 	struct pst_dynamic pstd;
128368508Seric 
128468508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
128568869Seric 			     (size_t) 1, 0) == -1)
128668508Seric 		return 0;
128768508Seric 
128868508Seric         if (tTd(3, 1))
128968508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
129068508Seric 
129168508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
129268508Seric }
129368508Seric 
129469462Seric #endif /* LA_TYPE == LA_HPUX */
129568508Seric 
129669462Seric #if LA_TYPE == LA_SUBR
129769462Seric 
129868508Seric int
129968508Seric getla()
130068508Seric {
130151920Seric 	double avenrun[3];
130251920Seric 
130351920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
130457736Seric 	{
130557736Seric 		if (tTd(3, 1))
130657736Seric 			perror("getla: getloadavg failed:");
130751920Seric 		return (-1);
130857736Seric 	}
130957736Seric 	if (tTd(3, 1))
131057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
131151920Seric 	return ((int) (avenrun[0] + 0.5));
131251773Seric }
131351773Seric 
131469462Seric #endif /* LA_TYPE == LA_SUBR */
131569462Seric 
131664295Seric #if LA_TYPE == LA_MACH
131751773Seric 
131864295Seric /*
131965173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
132064295Seric */
132164295Seric 
132265173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
132365173Seric # include <mach/mach.h>
132465173Seric #else
132565173Seric # include <mach.h>
132665173Seric #endif
132764295Seric 
132851773Seric getla()
132951773Seric {
133064295Seric 	processor_set_t default_set;
133164295Seric 	kern_return_t error;
133264295Seric 	unsigned int info_count;
133364295Seric 	struct processor_set_basic_info info;
133464295Seric 	host_t host;
133564295Seric 
133664295Seric 	error = processor_set_default(host_self(), &default_set);
133764295Seric 	if (error != KERN_SUCCESS)
133864295Seric 		return -1;
133964295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
134064295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
134164295Seric 			       &host, (processor_set_info_t)&info,
134264295Seric 			       &info_count) != KERN_SUCCESS)
134364295Seric 	{
134464295Seric 		return -1;
134564295Seric 	}
134664295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
134764295Seric }
134864295Seric 
134969462Seric #endif /* LA_TYPE == LA_MACH */
135064295Seric 
135166313Seric #if LA_TYPE == LA_PROCSTR
135264295Seric 
135366301Seric /*
135466301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
135566301Seric **  in a format like "0.15 0.12 0.06".
135666301Seric **
135766301Seric **	Initially intended for Linux.  This has been in the kernel
135866301Seric **	since at least 0.99.15.
135966301Seric */
136066301Seric 
136166301Seric # ifndef _PATH_LOADAVG
136266301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
136366301Seric # endif
136466301Seric 
136566301Seric int
136664295Seric getla()
136764295Seric {
136866319Seric 	double avenrun;
136966301Seric 	register int result;
137066319Seric 	FILE *fp;
137166301Seric 
137266319Seric 	fp = fopen(_PATH_LOADAVG, "r");
137369472Seric 	if (fp == NULL)
137466301Seric 	{
137566319Seric 		if (tTd(3, 1))
137666319Seric 			printf("getla: fopen(%s): %s\n",
137766319Seric 				_PATH_LOADAVG, errstring(errno));
137866319Seric 		return -1;
137966301Seric 	}
138066301Seric 	result = fscanf(fp, "%lf", &avenrun);
138166319Seric 	fclose(fp);
138266301Seric 	if (result != 1)
138366301Seric 	{
138466310Seric 		if (tTd(3, 1))
138566301Seric 			printf("getla: fscanf() = %d: %s\n",
138666301Seric 				result, errstring(errno));
138766301Seric 		return -1;
138866301Seric 	}
138966301Seric 
139057736Seric 	if (tTd(3, 1))
139166301Seric 		printf("getla(): %.2f\n", avenrun);
139266301Seric 
139366301Seric 	return ((int) (avenrun + 0.5));
139466301Seric }
139566301Seric 
139669462Seric #endif /* LA_TYPE == LA_PROCSTR */
139766301Seric 
139869462Seric #if LA_TYPE == LA_ZERO
139969462Seric 
140066301Seric getla()
140166301Seric {
140266301Seric 	if (tTd(3, 1))
140357736Seric 		printf("getla: ZERO\n");
140451920Seric 	return (0);
140551773Seric }
140651773Seric 
140769462Seric #endif /* LA_TYPE == LA_ZERO */
140866045Seric 
140966045Seric 
141066045Seric /*
141166045Seric  * Copyright 1989 Massachusetts Institute of Technology
141266045Seric  *
141366045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
141466045Seric  * documentation for any purpose is hereby granted without fee, provided that
141566045Seric  * the above copyright notice appear in all copies and that both that
141666045Seric  * copyright notice and this permission notice appear in supporting
141766045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
141866045Seric  * publicity pertaining to distribution of the software without specific,
141966045Seric  * written prior permission.  M.I.T. makes no representations about the
142066045Seric  * suitability of this software for any purpose.  It is provided "as is"
142166045Seric  * without express or implied warranty.
142266045Seric  *
142366045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
142466045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
142566045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
142666045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
142766045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
142866045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
142966045Seric  *
143066045Seric  * Authors:  Many and varied...
143166045Seric  */
143266045Seric 
143366045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
143466045Seric #ifndef lint
143566045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
143666045Seric #endif /* !lint */
143766045Seric 
143866179Seric #ifdef apollo
143966045Seric # undef volatile
144066045Seric #    include <apollo/base.h>
144166045Seric 
144266045Seric /* ARGSUSED */
144366045Seric int getloadavg( call_data )
144466045Seric      caddr_t	call_data;	/* pointer to (double) return value */
144566045Seric {
144666045Seric      double *avenrun = (double *) call_data;
144766045Seric      int i;
144866045Seric      status_$t      st;
144966045Seric      long loadav[3];
145066045Seric      proc1_$get_loadav(loadav, &st);
145166045Seric      *avenrun = loadav[0] / (double) (1 << 16);
145266045Seric      return(0);
145366045Seric }
145466045Seric #   endif /* apollo */
145524943Seric /*
145624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
145724943Seric **
145824943Seric **	Compares the message cost to the load average to decide.
145924943Seric **
146024943Seric **	Parameters:
146124943Seric **		pri -- the priority of the message in question.
146257438Seric **		ctime -- the message creation time.
146324943Seric **
146424943Seric **	Returns:
146524943Seric **		TRUE -- if this message should be queued up for the
146624943Seric **			time being.
146724943Seric **		FALSE -- if the load is low enough to send this message.
146824943Seric **
146924943Seric **	Side Effects:
147024943Seric **		none.
147124943Seric */
147224943Seric 
147324943Seric bool
147457438Seric shouldqueue(pri, ctime)
147524943Seric 	long pri;
147657438Seric 	time_t ctime;
147724943Seric {
147868508Seric 	bool rval;
147968508Seric 
148068508Seric 	if (tTd(3, 30))
148168508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
148251920Seric 	if (CurrentLA < QueueLA)
148368508Seric 	{
148468508Seric 		if (tTd(3, 30))
148568508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
148624943Seric 		return (FALSE);
148768508Seric 	}
148858132Seric 	if (CurrentLA >= RefuseLA)
148968508Seric 	{
149068508Seric 		if (tTd(3, 30))
149168508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
149258132Seric 		return (TRUE);
149368508Seric 	}
149468508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
149568508Seric 	if (tTd(3, 30))
149668508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
149768508Seric 	return rval;
149824943Seric }
149924943Seric /*
150053037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
150153037Seric **
150253037Seric **	Parameters:
150353037Seric **		none.
150453037Seric **
150553037Seric **	Returns:
150653037Seric **		TRUE if incoming SMTP connections should be refused
150753037Seric **			(for now).
150853037Seric **		FALSE if we should accept new work.
150953037Seric **
151053037Seric **	Side Effects:
151153037Seric **		none.
151253037Seric */
151353037Seric 
151453037Seric bool
151553037Seric refuseconnections()
151653037Seric {
151768508Seric 	extern bool enoughspace();
151868508Seric 
151959156Seric #ifdef XLA
152059156Seric 	if (!xla_smtp_ok())
152159156Seric 		return TRUE;
152259156Seric #endif
152359156Seric 
152453037Seric 	/* this is probably too simplistic */
152568508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
152653037Seric }
152753037Seric /*
152824943Seric **  SETPROCTITLE -- set process title for ps
152924943Seric **
153024943Seric **	Parameters:
153158674Seric **		fmt -- a printf style format string.
153258674Seric **		a, b, c -- possible parameters to fmt.
153324943Seric **
153424943Seric **	Returns:
153524943Seric **		none.
153624943Seric **
153724943Seric **	Side Effects:
153824943Seric **		Clobbers argv of our main procedure so ps(1) will
153924943Seric **		display the title.
154024943Seric */
154124943Seric 
154268508Seric #define SPT_NONE	0	/* don't use it at all */
154368508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
154468508Seric #define SPT_BUILTIN	2	/* use libc builtin */
154568508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
154668508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
154768508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
154868508Seric 
154968508Seric #ifndef SPT_TYPE
155068508Seric # define SPT_TYPE	SPT_REUSEARGV
155168508Seric #endif
155268508Seric 
155368508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
155468508Seric 
155568508Seric # if SPT_TYPE == SPT_PSTAT
155658689Seric #  include <sys/pstat.h>
155758689Seric # endif
155868508Seric # if SPT_TYPE == SPT_PSSTRINGS
155959732Seric #  include <machine/vmparam.h>
156059732Seric #  include <sys/exec.h>
156168508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
156268508Seric #   undef SPT_TYPE
156368508Seric #   define SPT_TYPE	SPT_REUSEARGV
156468734Seric #  else
156568734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
156668734Seric #    define NKPDE 63
156768734Seric typedef unsigned int	*pt_entry_t;
156868734Seric #   endif
156964247Seric #  endif
157059732Seric # endif
157168508Seric 
157268508Seric # if SPT_TYPE == SPT_PSSTRINGS
157368508Seric #  define SETPROC_STATIC	static
157468508Seric # else
157559732Seric #  define SETPROC_STATIC
157659732Seric # endif
157767626Seric 
157868508Seric # ifndef SPT_PADCHAR
157968508Seric #  define SPT_PADCHAR	' '
158068508Seric # endif
158158689Seric 
158268508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
158364843Seric 
158468508Seric #if SPT_TYPE != SPT_BUILTIN
158568508Seric 
158624943Seric /*VARARGS1*/
158768508Seric void
158868508Seric # ifdef __STDC__
158957642Seric setproctitle(char *fmt, ...)
159068508Seric # else
159157642Seric setproctitle(fmt, va_alist)
159224943Seric 	char *fmt;
159357642Seric 	va_dcl
159468508Seric # endif
159524943Seric {
159668508Seric # if SPT_TYPE != SPT_NONE
159724943Seric 	register char *p;
159825049Seric 	register int i;
159959732Seric 	SETPROC_STATIC char buf[MAXLINE];
160056852Seric 	VA_LOCAL_DECL
160168508Seric #  if SPT_TYPE == SPT_PSTAT
160258689Seric 	union pstun pst;
160358689Seric #  endif
160424943Seric 	extern char **Argv;
160524943Seric 	extern char *LastArgv;
160624943Seric 
160758674Seric 	p = buf;
160824943Seric 
160958674Seric 	/* print sendmail: heading for grep */
161058674Seric 	(void) strcpy(p, "sendmail: ");
161158674Seric 	p += strlen(p);
161224943Seric 
161358674Seric 	/* print the argument string */
161458674Seric 	VA_START(fmt);
161558674Seric 	(void) vsprintf(p, fmt, ap);
161656852Seric 	VA_END;
161754996Seric 
161858674Seric 	i = strlen(buf);
161958689Seric 
162068508Seric #  if SPT_TYPE == SPT_PSTAT
162158689Seric 	pst.pst_command = buf;
162258689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
162358689Seric #  else
162468508Seric #   if SPT_TYPE == SPT_PSSTRINGS
162559732Seric 	PS_STRINGS->ps_nargvstr = 1;
162659732Seric 	PS_STRINGS->ps_argvstr = buf;
162759732Seric #   else
162854996Seric 	if (i > LastArgv - Argv[0] - 2)
162925049Seric 	{
163054996Seric 		i = LastArgv - Argv[0] - 2;
163158674Seric 		buf[i] = '\0';
163225049Seric 	}
163358674Seric 	(void) strcpy(Argv[0], buf);
163454997Seric 	p = &Argv[0][i];
163524943Seric 	while (p < LastArgv)
163668508Seric 		*p++ = SPT_PADCHAR;
163768508Seric 	Argv[1] = NULL;
163868508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
163968508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
164068508Seric # endif /* SPT_TYPE != SPT_NONE */
164124943Seric }
164265982Seric 
164368508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
164425698Seric /*
164525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
164625698Seric **
164725698Seric **	Parameters:
164868508Seric **		sig -- the signal that got us here (unused).
164925698Seric **
165025698Seric **	Returns:
165125698Seric **		none.
165225698Seric **
165325698Seric **	Side Effects:
165425698Seric **		Picks up extant zombies.
165525698Seric */
165625698Seric 
165746928Sbostic void
165868508Seric reapchild(sig)
165968508Seric 	int sig;
166025698Seric {
166164738Seric 	int olderrno = errno;
166263966Seric # ifdef HASWAITPID
166360219Seric 	auto int status;
166460560Seric 	int count;
166560560Seric 	int pid;
166660219Seric 
166760560Seric 	count = 0;
166860560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
166960560Seric 	{
167060560Seric 		if (count++ > 1000)
167160560Seric 		{
167266748Seric #ifdef LOG
167360560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
167460560Seric 				pid, status);
167566748Seric #endif
167660560Seric 			break;
167760560Seric 		}
167860560Seric 	}
167960219Seric # else
168025698Seric # ifdef WNOHANG
168125698Seric 	union wait status;
168225698Seric 
168363753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
168425698Seric 		continue;
168556795Seric # else /* WNOHANG */
168625698Seric 	auto int status;
168725698Seric 
168860219Seric 	while (wait(&status) > 0)
168925698Seric 		continue;
169056795Seric # endif /* WNOHANG */
169160219Seric # endif
169264035Seric # ifdef SYS5SIGNALS
169364035Seric 	(void) setsignal(SIGCHLD, reapchild);
169458061Seric # endif
169564738Seric 	errno = olderrno;
169625698Seric }
169755418Seric /*
169855418Seric **  UNSETENV -- remove a variable from the environment
169955418Seric **
170055418Seric **	Not needed on newer systems.
170155418Seric **
170255418Seric **	Parameters:
170355418Seric **		name -- the string name of the environment variable to be
170455418Seric **			deleted from the current environment.
170555418Seric **
170655418Seric **	Returns:
170755418Seric **		none.
170855418Seric **
170955418Seric **	Globals:
171055418Seric **		environ -- a pointer to the current environment.
171155418Seric **
171255418Seric **	Side Effects:
171355418Seric **		Modifies environ.
171455418Seric */
171555418Seric 
171663962Seric #ifndef HASUNSETENV
171755418Seric 
171855418Seric void
171955418Seric unsetenv(name)
172055418Seric 	char *name;
172155418Seric {
172255418Seric 	extern char **environ;
172355418Seric 	register char **pp;
172455418Seric 	int len = strlen(name);
172555418Seric 
172655418Seric 	for (pp = environ; *pp != NULL; pp++)
172755418Seric 	{
172855418Seric 		if (strncmp(name, *pp, len) == 0 &&
172955418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
173055418Seric 			break;
173155418Seric 	}
173255418Seric 
173355418Seric 	for (; *pp != NULL; pp++)
173455418Seric 		*pp = pp[1];
173555418Seric }
173655418Seric 
173763962Seric #endif
173856215Seric /*
173956215Seric **  GETDTABLESIZE -- return number of file descriptors
174056215Seric **
174156215Seric **	Only on non-BSD systems
174256215Seric **
174356215Seric **	Parameters:
174456215Seric **		none
174556215Seric **
174656215Seric **	Returns:
174756215Seric **		size of file descriptor table
174856215Seric **
174956215Seric **	Side Effects:
175056215Seric **		none
175156215Seric */
175256215Seric 
175363787Seric #ifdef SOLARIS
175463787Seric # include <sys/resource.h>
175563787Seric #endif
175656215Seric 
175756215Seric int
175863787Seric getdtsize()
175956215Seric {
176063787Seric #ifdef RLIMIT_NOFILE
176163787Seric 	struct rlimit rl;
176263787Seric 
176363787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
176463787Seric 		return rl.rlim_cur;
176563787Seric #endif
176663787Seric 
176764031Seric # ifdef HASGETDTABLESIZE
176864031Seric 	return getdtablesize();
176964031Seric # else
177064031Seric #  ifdef _SC_OPEN_MAX
177158689Seric 	return sysconf(_SC_OPEN_MAX);
177263787Seric #  else
177356215Seric 	return NOFILE;
177463787Seric #  endif
177558689Seric # endif
177656215Seric }
177757631Seric /*
177857631Seric **  UNAME -- get the UUCP name of this system.
177957631Seric */
178057631Seric 
178157943Seric #ifndef HASUNAME
178257631Seric 
178357631Seric int
178457631Seric uname(name)
178557631Seric 	struct utsname *name;
178657631Seric {
178757631Seric 	FILE *file;
178857631Seric 	char *n;
178957631Seric 
179057631Seric 	name->nodename[0] = '\0';
179157631Seric 
179257661Seric 	/* try /etc/whoami -- one line with the node name */
179357631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
179457631Seric 	{
179557661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
179657631Seric 		(void) fclose(file);
179757661Seric 		n = strchr(name->nodename, '\n');
179857631Seric 		if (n != NULL)
179957631Seric 			*n = '\0';
180057631Seric 		if (name->nodename[0] != '\0')
180157631Seric 			return (0);
180257631Seric 	}
180357631Seric 
180457661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
180557631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
180657631Seric 	{
180757631Seric 		char buf[MAXLINE];
180857631Seric 
180957631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
181057631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
181157631Seric 					NODE_LENGTH, name->nodename) > 0)
181257631Seric 				break;
181357631Seric 		(void) fclose(file);
181457631Seric 		if (name->nodename[0] != '\0')
181557631Seric 			return (0);
181657631Seric 	}
181757631Seric 
181857631Seric #ifdef TRUST_POPEN
181957631Seric 	/*
182057631Seric 	**  Popen is known to have security holes.
182157631Seric 	*/
182257631Seric 
182357661Seric 	/* try uuname -l to return local name */
182457631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
182557631Seric 	{
182657661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
182757631Seric 		(void) pclose(file);
182857661Seric 		n = strchr(name, '\n');
182957631Seric 		if (n != NULL)
183057631Seric 			*n = '\0';
183157661Seric 		if (name->nodename[0] != '\0')
183257631Seric 			return (0);
183357631Seric 	}
183457631Seric #endif
183569472Seric 
183657631Seric 	return (-1);
183757631Seric }
183857943Seric #endif /* HASUNAME */
183958068Seric /*
184058068Seric **  INITGROUPS -- initialize groups
184158068Seric **
184258068Seric **	Stub implementation for System V style systems
184358068Seric */
184458068Seric 
184558068Seric #ifndef HASINITGROUPS
184658068Seric 
184758068Seric initgroups(name, basegid)
184858068Seric 	char *name;
184958068Seric 	int basegid;
185058068Seric {
185158068Seric 	return 0;
185258068Seric }
185358068Seric 
185458068Seric #endif
185558082Seric /*
185659289Seric **  SETSID -- set session id (for non-POSIX systems)
185759289Seric */
185859289Seric 
185959289Seric #ifndef HASSETSID
186059289Seric 
186163753Seric pid_t
186263753Seric setsid __P ((void))
186359289Seric {
186464155Seric #ifdef TIOCNOTTY
186564155Seric 	int fd;
186664155Seric 
186766743Seric 	fd = open("/dev/tty", O_RDWR, 0);
186864155Seric 	if (fd >= 0)
186964155Seric 	{
187064155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
187164155Seric 		(void) close(fd);
187264155Seric 	}
187364155Seric #endif /* TIOCNOTTY */
187464705Seric # ifdef SYS5SETPGRP
187563753Seric 	return setpgrp();
187663753Seric # else
187764155Seric 	return setpgid(0, getpid());
187859289Seric # endif
187959289Seric }
188059289Seric 
188159289Seric #endif
188259289Seric /*
188366748Seric **  FSYNC -- dummy fsync
188466748Seric */
188566748Seric 
188666748Seric #ifdef NEEDFSYNC
188766748Seric 
188866748Seric fsync(fd)
188966748Seric 	int fd;
189066748Seric {
189166748Seric # ifdef O_SYNC
189266748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
189366748Seric # else
189466748Seric 	/* nothing we can do */
189566748Seric 	return 0;
189666748Seric # endif
189766748Seric }
189866748Seric 
189966748Seric #endif
190066748Seric /*
190164718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
190264718Seric **
190364718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
190468508Seric **	instead of a long.  This patches things.  Only needed on versions
190568508Seric **	prior to 5.4.3.
190664718Seric */
190764718Seric 
190868508Seric #ifdef DGUX_5_4_2
190964718Seric 
191064718Seric #undef inet_addr
191164718Seric 
191264718Seric long
191364718Seric dgux_inet_addr(host)
191464718Seric 	char *host;
191564718Seric {
191664718Seric 	struct in_addr haddr;
191764718Seric 
191864718Seric 	haddr = inet_addr(host);
191964718Seric 	return haddr.s_addr;
192064718Seric }
192164718Seric 
192264718Seric #endif
192364718Seric /*
192463969Seric **  GETOPT -- for old systems or systems with bogus implementations
192563969Seric */
192663969Seric 
192763969Seric #ifdef NEEDGETOPT
192863969Seric 
192963969Seric /*
193063969Seric  * Copyright (c) 1985 Regents of the University of California.
193163969Seric  * All rights reserved.  The Berkeley software License Agreement
193263969Seric  * specifies the terms and conditions for redistribution.
193363969Seric  */
193463969Seric 
193563969Seric 
193663969Seric /*
193763969Seric ** this version hacked to add `atend' flag to allow state machine
193863969Seric ** to reset if invoked by the program to scan args for a 2nd time
193963969Seric */
194063969Seric 
194163969Seric #if defined(LIBC_SCCS) && !defined(lint)
194263969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
194364072Seric #endif /* LIBC_SCCS and not lint */
194463969Seric 
194563969Seric #include <stdio.h>
194663969Seric 
194763969Seric /*
194863969Seric  * get option letter from argument vector
194963969Seric  */
195065352Seric #ifdef _CONVEX_SOURCE
195168508Seric extern int	optind, opterr, optopt;
195268508Seric extern char	*optarg;
195365352Seric #else
195465352Seric int	opterr = 1;		/* if error message should be printed */
195565352Seric int	optind = 1;		/* index into parent argv vector */
195668470Seric int	optopt = 0;		/* character checked for validity */
195768470Seric char	*optarg = NULL;		/* argument associated with option */
195868508Seric #endif
195963969Seric 
196063969Seric #define BADCH	(int)'?'
196163969Seric #define EMSG	""
196263969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
196363969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
196463969Seric 
196563969Seric getopt(nargc,nargv,ostr)
196665160Seric 	int		nargc;
196765160Seric 	char *const	*nargv;
196865160Seric 	const char	*ostr;
196963969Seric {
197063969Seric 	static char	*place = EMSG;	/* option letter processing */
197163969Seric 	static char	atend = 0;
197263969Seric 	register char	*oli;		/* option letter list index */
197363969Seric 
197463969Seric 	if (atend) {
197563969Seric 		atend = 0;
197663969Seric 		place = EMSG;
197763969Seric 	}
197863969Seric 	if(!*place) {			/* update scanning pointer */
197964136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
198063969Seric 			atend++;
198163969Seric 			return(EOF);
198263969Seric 		}
198363969Seric 		if (*place == '-') {	/* found "--" */
198463969Seric 			++optind;
198563969Seric 			atend++;
198663969Seric 			return(EOF);
198763969Seric 		}
198863969Seric 	}				/* option letter okay? */
198964125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
199064136Seric 		if (!*place) ++optind;
199163969Seric 		tell(": illegal option -- ");
199263969Seric 	}
199363969Seric 	if (*++oli != ':') {		/* don't need argument */
199463969Seric 		optarg = NULL;
199563969Seric 		if (!*place) ++optind;
199663969Seric 	}
199763969Seric 	else {				/* need an argument */
199863969Seric 		if (*place) optarg = place;	/* no white space */
199963969Seric 		else if (nargc <= ++optind) {	/* no arg */
200063969Seric 			place = EMSG;
200163969Seric 			tell(": option requires an argument -- ");
200263969Seric 		}
200363969Seric 	 	else optarg = nargv[optind];	/* white space */
200463969Seric 		place = EMSG;
200563969Seric 		++optind;
200663969Seric 	}
200763969Seric 	return(optopt);			/* dump back option letter */
200863969Seric }
200963969Seric 
201063969Seric #endif
201163969Seric /*
201263969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
201363969Seric */
201463969Seric 
201563969Seric #ifdef NEEDVPRINTF
201663969Seric 
201763969Seric #define MAXARG	16
201863969Seric 
201963969Seric vfprintf(fp, fmt, ap)
202063969Seric 	FILE *	fp;
202163969Seric 	char *	fmt;
202263969Seric 	char **	ap;
202363969Seric {
202463969Seric 	char *	bp[MAXARG];
202563969Seric 	int	i = 0;
202663969Seric 
202763969Seric 	while (*ap && i < MAXARG)
202863969Seric 		bp[i++] = *ap++;
202963969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
203063969Seric 			 bp[4], bp[5], bp[6], bp[7],
203163969Seric 			 bp[8], bp[9], bp[10], bp[11],
203263969Seric 			 bp[12], bp[13], bp[14], bp[15]);
203363969Seric }
203463969Seric 
203563969Seric vsprintf(s, fmt, ap)
203663969Seric 	char *	s;
203763969Seric 	char *	fmt;
203863969Seric 	char **	ap;
203963969Seric {
204063969Seric 	char *	bp[MAXARG];
204163969Seric 	int	i = 0;
204263969Seric 
204363969Seric 	while (*ap && i < MAXARG)
204463969Seric 		bp[i++] = *ap++;
204563969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
204663969Seric 			bp[4], bp[5], bp[6], bp[7],
204763969Seric 			bp[8], bp[9], bp[10], bp[11],
204863969Seric 			bp[12], bp[13], bp[14], bp[15]);
204963969Seric }
205063969Seric 
205163969Seric #endif
205263969Seric /*
205365220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
205465208Seric **
205565211Seric **	Parameters:
205665211Seric **		shell -- the user's shell from /etc/passwd
205765211Seric **
205865211Seric **	Returns:
205965211Seric **		TRUE -- if it is ok to use this for unrestricted access.
206065211Seric **		FALSE -- if the shell is restricted.
206165208Seric */
206265208Seric 
206365587Seric #if !HASGETUSERSHELL
206465587Seric 
206565587Seric # ifndef _PATH_SHELLS
206665587Seric #  define _PATH_SHELLS	"/etc/shells"
206765587Seric # endif
206865587Seric 
206965587Seric char	*DefaultUserShells[] =
207065587Seric {
207168508Seric 	"/bin/sh",		/* standard shell */
207265587Seric 	"/usr/bin/sh",
207368508Seric 	"/bin/csh",		/* C shell */
207465587Seric 	"/usr/bin/csh",
207565587Seric #ifdef __hpux
207668889Seric # ifdef V4FS
207768889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
207868889Seric 	"/usr/bin/ksh",		/* Korn shell */
207968889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
208068889Seric 	"/usr/bin/pam",
208168889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
208268889Seric 	"/usr/bin/posix/sh",
208368889Seric # else
208468508Seric 	"/bin/rsh",		/* restricted Bourne shell */
208568508Seric 	"/bin/ksh",		/* Korn shell */
208668508Seric 	"/bin/rksh",		/* restricted Korn shell */
208765587Seric 	"/bin/pam",
208868508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
208965587Seric 	"/bin/posix/sh",
209068889Seric # endif
209165211Seric #endif
209268508Seric #ifdef _AIX3
209368508Seric 	"/bin/ksh",		/* Korn shell */
209468508Seric 	"/usr/bin/ksh",
209568508Seric 	"/bin/tsh",		/* trusted shell */
209668508Seric 	"/usr/bin/tsh",
209768508Seric 	"/bin/bsh",		/* Bourne shell */
209868508Seric 	"/usr/bin/bsh",
209968508Seric #endif
210065587Seric 	NULL
210165587Seric };
210265587Seric 
210365208Seric #endif
210465208Seric 
210565747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
210665747Seric 
210765211Seric bool
210865211Seric usershellok(shell)
210965211Seric 	char *shell;
211065208Seric {
211165211Seric #if HASGETUSERSHELL
211265211Seric 	register char *p;
211365211Seric 	extern char *getusershell();
211465208Seric 
211568457Seric 	if (shell == NULL || shell[0] == '\0')
211668457Seric 		return TRUE;
211768457Seric 
211865211Seric 	setusershell();
211965211Seric 	while ((p = getusershell()) != NULL)
212065747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
212165211Seric 			break;
212265211Seric 	endusershell();
212365211Seric 	return p != NULL;
212465211Seric #else
212565211Seric 	register FILE *shellf;
212665211Seric 	char buf[MAXLINE];
212765208Seric 
212868457Seric 	if (shell == NULL || shell[0] == '\0')
212968457Seric 		return TRUE;
213068457Seric 
213165211Seric 	shellf = fopen(_PATH_SHELLS, "r");
213265211Seric 	if (shellf == NULL)
213365211Seric 	{
213465211Seric 		/* no /etc/shells; see if it is one of the std shells */
213565587Seric 		char **d;
213665587Seric 
213765587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
213865587Seric 		{
213965587Seric 			if (strcmp(shell, *d) == 0)
214065587Seric 				return TRUE;
214165587Seric 		}
214265587Seric 		return FALSE;
214365211Seric 	}
214465208Seric 
214565211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
214665211Seric 	{
214765211Seric 		register char *p, *q;
214865208Seric 
214965211Seric 		p = buf;
215065211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
215165211Seric 			p++;
215265211Seric 		if (*p == '#' || *p == '\0')
215365208Seric 			continue;
215465211Seric 		q = p;
215565211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
215665211Seric 			p++;
215765211Seric 		*p = '\0';
215865747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
215965211Seric 		{
216065211Seric 			fclose(shellf);
216165211Seric 			return TRUE;
216265211Seric 		}
216365208Seric 	}
216465211Seric 	fclose(shellf);
216565211Seric 	return FALSE;
216665211Seric #endif
216765208Seric }
216865208Seric /*
216964311Seric **  FREESPACE -- see how much free space is on the queue filesystem
217058082Seric **
217158082Seric **	Only implemented if you have statfs.
217258082Seric **
217358082Seric **	Parameters:
217464311Seric **		dir -- the directory in question.
217564311Seric **		bsize -- a variable into which the filesystem
217664311Seric **			block size is stored.
217758082Seric **
217858082Seric **	Returns:
217964311Seric **		The number of bytes free on the queue filesystem.
218064311Seric **		-1 if the statfs call fails.
218164311Seric **
218264311Seric **	Side effects:
218364311Seric **		Puts the filesystem block size into bsize.
218458082Seric */
218558082Seric 
218665749Seric /* statfs types */
218765749Seric #define SFS_NONE	0	/* no statfs implementation */
218865749Seric #define SFS_USTAT	1	/* use ustat */
218965749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
219065749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
219165749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
219265749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
219366752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
219465749Seric 
219565749Seric #ifndef SFS_TYPE
219665749Seric # define SFS_TYPE	SFS_NONE
219758157Seric #endif
219858157Seric 
219965749Seric #if SFS_TYPE == SFS_USTAT
220058157Seric # include <ustat.h>
220158157Seric #endif
220265749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
220365749Seric # include <sys/statfs.h>
220458082Seric #endif
220565749Seric #if SFS_TYPE == SFS_VFS
220665749Seric # include <sys/vfs.h>
220765749Seric #endif
220865749Seric #if SFS_TYPE == SFS_MOUNT
220965749Seric # include <sys/mount.h>
221065749Seric #endif
221166752Seric #if SFS_TYPE == SFS_STATVFS
221266752Seric # include <sys/statvfs.h>
221366752Seric #endif
221458082Seric 
221564311Seric long
221664311Seric freespace(dir, bsize)
221764311Seric 	char *dir;
221864311Seric 	long *bsize;
221958082Seric {
222065749Seric #if SFS_TYPE != SFS_NONE
222165749Seric # if SFS_TYPE == SFS_USTAT
222258153Seric 	struct ustat fs;
222358153Seric 	struct stat statbuf;
222458366Seric #  define FSBLOCKSIZE	DEV_BSIZE
222568695Seric #  define SFS_BAVAIL	f_tfree
222658157Seric # else
222758157Seric #  if defined(ultrix)
222858157Seric 	struct fs_data fs;
222968695Seric #   define SFS_BAVAIL	fd_bfreen
223068508Seric #   define FSBLOCKSIZE	1024L
223158153Seric #  else
223266752Seric #   if SFS_TYPE == SFS_STATVFS
223366752Seric 	struct statvfs fs;
223468508Seric #    define FSBLOCKSIZE	fs.f_frsize
223566752Seric #   else
223658082Seric 	struct statfs fs;
223766752Seric #    define FSBLOCKSIZE	fs.f_bsize
223863987Seric #   endif
223958153Seric #  endif
224058133Seric # endif
224168695Seric # ifndef SFS_BAVAIL
224268695Seric #  define SFS_BAVAIL f_bavail
224368508Seric # endif
224458082Seric 
224565749Seric # if SFS_TYPE == SFS_USTAT
224664311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
224758157Seric # else
224865749Seric #  if SFS_TYPE == SFS_4ARGS
224964311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
225058157Seric #  else
225168508Seric #   if SFS_TYPE == SFS_STATVFS
225268508Seric 	if (statvfs(dir, &fs) == 0)
225368508Seric #   else
225468508Seric #    if defined(ultrix)
225568499Seric 	if (statfs(dir, &fs) > 0)
225668508Seric #    else
225764311Seric 	if (statfs(dir, &fs) == 0)
225868508Seric #    endif
225958153Seric #   endif
226058133Seric #  endif
226158133Seric # endif
226258133Seric 	{
226364311Seric 		if (bsize != NULL)
226464311Seric 			*bsize = FSBLOCKSIZE;
226568695Seric 		return (fs.SFS_BAVAIL);
226664311Seric 	}
226764337Seric #endif
226864311Seric 	return (-1);
226964311Seric }
227064311Seric /*
227164311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
227264311Seric **
227364311Seric **	Only implemented if you have statfs.
227464311Seric **
227564311Seric **	Parameters:
227664311Seric **		msize -- the size to check against.  If zero, we don't yet
227764311Seric **		know how big the message will be, so just check for
227864311Seric **		a "reasonable" amount.
227964311Seric **
228064311Seric **	Returns:
228164311Seric **		TRUE if there is enough space.
228264311Seric **		FALSE otherwise.
228364311Seric */
228464311Seric 
228564311Seric bool
228664311Seric enoughspace(msize)
228764311Seric 	long msize;
228864311Seric {
228964311Seric 	long bfree, bsize;
229064311Seric 
229164311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
229264311Seric 	{
229358133Seric 		if (tTd(4, 80))
229464311Seric 			printf("enoughspace: no threshold\n");
229564311Seric 		return TRUE;
229664311Seric 	}
229764311Seric 
229864311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
229964311Seric 	{
230064311Seric 		if (tTd(4, 80))
230158333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
230264311Seric 				bfree, msize);
230358333Seric 
230458333Seric 		/* convert msize to block count */
230564311Seric 		msize = msize / bsize + 1;
230658333Seric 		if (MinBlocksFree >= 0)
230758333Seric 			msize += MinBlocksFree;
230858333Seric 
230964311Seric 		if (bfree < msize)
231058090Seric 		{
231158090Seric #ifdef LOG
231258090Seric 			if (LogLevel > 0)
231364254Seric 				syslog(LOG_ALERT,
231464254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
231568508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
231668508Seric 					bfree,
231768508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
231868508Seric 					msize, QueueDir);
231958090Seric #endif
232058082Seric 			return FALSE;
232158090Seric 		}
232258082Seric 	}
232358082Seric 	else if (tTd(4, 80))
232458333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
232558333Seric 			MinBlocksFree, msize, errstring(errno));
232658082Seric 	return TRUE;
232758082Seric }
232858542Seric /*
232958542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
233058542Seric **
233158542Seric **	This looks at an errno value and tells if this is likely to
233258542Seric **	go away if retried later.
233358542Seric **
233458542Seric **	Parameters:
233558542Seric **		err -- the errno code to classify.
233658542Seric **
233758542Seric **	Returns:
233858542Seric **		TRUE if this is probably transient.
233958542Seric **		FALSE otherwise.
234058542Seric */
234158542Seric 
234258542Seric bool
234358542Seric transienterror(err)
234458542Seric 	int err;
234558542Seric {
234658542Seric 	switch (err)
234758542Seric 	{
234858542Seric 	  case EIO:			/* I/O error */
234958542Seric 	  case ENXIO:			/* Device not configured */
235058542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
235158542Seric 	  case ENOMEM:			/* Cannot allocate memory */
235258542Seric 	  case ENODEV:			/* Operation not supported by device */
235358542Seric 	  case ENFILE:			/* Too many open files in system */
235458542Seric 	  case EMFILE:			/* Too many open files */
235558542Seric 	  case ENOSPC:			/* No space left on device */
235658542Seric #ifdef ETIMEDOUT
235758542Seric 	  case ETIMEDOUT:		/* Connection timed out */
235858542Seric #endif
235958542Seric #ifdef ESTALE
236058542Seric 	  case ESTALE:			/* Stale NFS file handle */
236158542Seric #endif
236258542Seric #ifdef ENETDOWN
236358542Seric 	  case ENETDOWN:		/* Network is down */
236458542Seric #endif
236558542Seric #ifdef ENETUNREACH
236658542Seric 	  case ENETUNREACH:		/* Network is unreachable */
236758542Seric #endif
236858542Seric #ifdef ENETRESET
236958542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
237058542Seric #endif
237158542Seric #ifdef ECONNABORTED
237258542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
237358542Seric #endif
237458542Seric #ifdef ECONNRESET
237558542Seric 	  case ECONNRESET:		/* Connection reset by peer */
237658542Seric #endif
237758542Seric #ifdef ENOBUFS
237858542Seric 	  case ENOBUFS:			/* No buffer space available */
237958542Seric #endif
238058542Seric #ifdef ESHUTDOWN
238158542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
238258542Seric #endif
238358542Seric #ifdef ECONNREFUSED
238458542Seric 	  case ECONNREFUSED:		/* Connection refused */
238558542Seric #endif
238658542Seric #ifdef EHOSTDOWN
238758542Seric 	  case EHOSTDOWN:		/* Host is down */
238858542Seric #endif
238958542Seric #ifdef EHOSTUNREACH
239058542Seric 	  case EHOSTUNREACH:		/* No route to host */
239158542Seric #endif
239258542Seric #ifdef EDQUOT
239358542Seric 	  case EDQUOT:			/* Disc quota exceeded */
239458542Seric #endif
239558542Seric #ifdef EPROCLIM
239658542Seric 	  case EPROCLIM:		/* Too many processes */
239758542Seric #endif
239858542Seric #ifdef EUSERS
239958542Seric 	  case EUSERS:			/* Too many users */
240058542Seric #endif
240158542Seric #ifdef EDEADLK
240258542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
240358542Seric #endif
240458542Seric #ifdef EISCONN
240558542Seric 	  case EISCONN:			/* Socket already connected */
240658542Seric #endif
240758542Seric #ifdef EINPROGRESS
240858542Seric 	  case EINPROGRESS:		/* Operation now in progress */
240958542Seric #endif
241058542Seric #ifdef EALREADY
241158542Seric 	  case EALREADY:		/* Operation already in progress */
241258542Seric #endif
241358542Seric #ifdef EADDRINUSE
241458542Seric 	  case EADDRINUSE:		/* Address already in use */
241558542Seric #endif
241658542Seric #ifdef EADDRNOTAVAIL
241758542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
241858542Seric #endif
241966018Seric #ifdef ETXTBSY
242066017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
242166017Seric #endif
242263834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
242358542Seric 	  case ENOSR:			/* Out of streams resources */
242458542Seric #endif
242558542Seric 		return TRUE;
242658542Seric 	}
242758542Seric 
242858542Seric 	/* nope, must be permanent */
242958542Seric 	return FALSE;
243058542Seric }
243158689Seric /*
243264035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
243358689Seric **
243458689Seric **	Parameters:
243558689Seric **		fd -- the file descriptor of the file.
243658689Seric **		filename -- the file name (for error messages).
243764335Seric **		ext -- the filename extension.
243858689Seric **		type -- type of the lock.  Bits can be:
243958689Seric **			LOCK_EX -- exclusive lock.
244058689Seric **			LOCK_NB -- non-blocking.
244158689Seric **
244258689Seric **	Returns:
244358689Seric **		TRUE if the lock was acquired.
244458689Seric **		FALSE otherwise.
244558689Seric */
244658689Seric 
244758689Seric bool
244864335Seric lockfile(fd, filename, ext, type)
244958689Seric 	int fd;
245058689Seric 	char *filename;
245164335Seric 	char *ext;
245258689Seric 	int type;
245358689Seric {
245465830Seric # if !HASFLOCK
245558689Seric 	int action;
245658689Seric 	struct flock lfd;
245764335Seric 
245864335Seric 	if (ext == NULL)
245964335Seric 		ext = "";
246069472Seric 
246163983Seric 	bzero(&lfd, sizeof lfd);
246259447Seric 	if (bitset(LOCK_UN, type))
246359447Seric 		lfd.l_type = F_UNLCK;
246459447Seric 	else if (bitset(LOCK_EX, type))
246558689Seric 		lfd.l_type = F_WRLCK;
246658689Seric 	else
246758689Seric 		lfd.l_type = F_RDLCK;
246858689Seric 
246958689Seric 	if (bitset(LOCK_NB, type))
247058689Seric 		action = F_SETLK;
247158689Seric 	else
247258689Seric 		action = F_SETLKW;
247358689Seric 
247464264Seric 	if (tTd(55, 60))
247564335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
247664335Seric 			filename, ext, action, lfd.l_type);
247764264Seric 
247858689Seric 	if (fcntl(fd, action, &lfd) >= 0)
247964264Seric 	{
248064264Seric 		if (tTd(55, 60))
248164264Seric 			printf("SUCCESS\n");
248258689Seric 		return TRUE;
248364264Seric 	}
248458689Seric 
248564264Seric 	if (tTd(55, 60))
248664264Seric 		printf("(%s) ", errstring(errno));
248764264Seric 
248864136Seric 	/*
248964136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
249064136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
249164136Seric 	**  as type "tmp" (that is, served from swap space), the
249264136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
249364136Seric 	**  Since this is fairly common during testing, we will assume
249464136Seric 	**  that this indicates that the lock is successfully grabbed.
249564136Seric 	*/
249664136Seric 
249764136Seric 	if (errno == EINVAL)
249864264Seric 	{
249964264Seric 		if (tTd(55, 60))
250064264Seric 			printf("SUCCESS\n");
250164136Seric 		return TRUE;
250264264Seric 	}
250364136Seric 
250458689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
250564378Seric 	{
250664378Seric 		int omode = -1;
250764378Seric #  ifdef F_GETFL
250864378Seric 		int oerrno = errno;
250964378Seric 
251064378Seric 		(void) fcntl(fd, F_GETFL, &omode);
251164378Seric 		errno = oerrno;
251264378Seric #  endif
251364378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
251464378Seric 			filename, ext, fd, type, omode, geteuid());
251564378Seric 	}
251658689Seric # else
251764335Seric 	if (ext == NULL)
251864335Seric 		ext = "";
251964335Seric 
252064264Seric 	if (tTd(55, 60))
252164335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
252264264Seric 
252358689Seric 	if (flock(fd, type) >= 0)
252464264Seric 	{
252564264Seric 		if (tTd(55, 60))
252664264Seric 			printf("SUCCESS\n");
252758689Seric 		return TRUE;
252864264Seric 	}
252958689Seric 
253064264Seric 	if (tTd(55, 60))
253164264Seric 		printf("(%s) ", errstring(errno));
253264264Seric 
253358689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
253464378Seric 	{
253564378Seric 		int omode = -1;
253664378Seric #  ifdef F_GETFL
253764378Seric 		int oerrno = errno;
253864378Seric 
253964378Seric 		(void) fcntl(fd, F_GETFL, &omode);
254064378Seric 		errno = oerrno;
254164378Seric #  endif
254264378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
254364378Seric 			filename, ext, fd, type, omode, geteuid());
254464386Seric 	}
254558689Seric # endif
254664264Seric 	if (tTd(55, 60))
254764264Seric 		printf("FAIL\n");
254858689Seric 	return FALSE;
254958689Seric }
255064556Seric /*
255165948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
255265948Seric **
255365948Seric **	Parameters:
255465948Seric **		fd -- the file descriptor to check.
255565948Seric **
255665948Seric **	Returns:
255765948Seric **		TRUE -- if only root can chown the file to an arbitrary
255865948Seric **			user.
255965948Seric **		FALSE -- if an arbitrary user can give away a file.
256065948Seric */
256165948Seric 
256265948Seric bool
256365948Seric chownsafe(fd)
256465948Seric 	int fd;
256565948Seric {
256665950Seric #ifdef __hpux
256765948Seric 	char *s;
256865948Seric 	int tfd;
256965948Seric 	uid_t o_uid, o_euid;
257065948Seric 	gid_t o_gid, o_egid;
257165948Seric 	bool rval;
257265948Seric 	struct stat stbuf;
257365948Seric 
257465948Seric 	o_uid = getuid();
257565948Seric 	o_euid = geteuid();
257665948Seric 	o_gid = getgid();
257765948Seric 	o_egid = getegid();
257865959Seric 	fstat(fd, &stbuf);
257965948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
258065948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
258165948Seric 	s = tmpnam(NULL);
258265948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
258365974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
258465948Seric 	close(tfd);
258565948Seric 	unlink(s);
258665948Seric 	setreuid(o_uid, o_euid);
258765948Seric 	setresgid(o_gid, o_egid, -1);
258865948Seric 	return rval;
258965948Seric #else
259066088Seric # ifdef _POSIX_CHOWN_RESTRICTED
259166088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
259266088Seric 	return FALSE;
259366088Seric #  else
259466088Seric 	return TRUE;
259566088Seric #  endif
259666088Seric # else
259766088Seric #  ifdef _PC_CHOWN_RESTRICTED
259868508Seric 	int rval;
259968508Seric 
260068508Seric 	/*
260168508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
260268508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
260368508Seric 	**  the call.  This heuristic checks for that.
260468508Seric 	*/
260568508Seric 
260668508Seric 	errno = 0;
260768508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
260868508Seric 	if (errno == 0)
260968508Seric 		return rval > 0;
261068508Seric #  endif
261168508Seric #  ifdef BSD
261268508Seric 	return TRUE;
261368499Seric #  else
261465948Seric 	return FALSE;
261565948Seric #  endif
261665948Seric # endif
261765948Seric #endif
261865948Seric }
261965948Seric /*
262068508Seric **  RESETLIMITS -- reset system controlled resource limits
262168508Seric **
262268508Seric **	This is to avoid denial-of-service attacks
262368508Seric **
262468508Seric **	Parameters:
262568508Seric **		none
262668508Seric **
262768508Seric **	Returns:
262868508Seric **		none
262968508Seric */
263068508Seric 
263168508Seric #if HASSETRLIMIT
263268508Seric # include <sys/resource.h>
263368508Seric #endif
263468508Seric 
263568508Seric void
263668508Seric resetlimits()
263768508Seric {
263868508Seric #if HASSETRLIMIT
263968508Seric 	struct rlimit lim;
264068508Seric 
264168508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
264268508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
264368508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
264468508Seric #else
264568508Seric # if HASULIMIT
264668508Seric 	(void) ulimit(2, 0x3fffff);
264768508Seric # endif
264868508Seric #endif
264968508Seric }
265068508Seric /*
265164556Seric **  GETCFNAME -- return the name of the .cf file.
265264556Seric **
265364556Seric **	Some systems (e.g., NeXT) determine this dynamically.
265464556Seric */
265564556Seric 
265664556Seric char *
265764556Seric getcfname()
265864556Seric {
265968842Seric 	int i;
266068842Seric 	static char cbuf[200];
266168842Seric 
266264556Seric 	if (ConfFile != NULL)
266364556Seric 		return ConfFile;
266465214Seric #ifdef NETINFO
266565214Seric 	{
266665214Seric 		extern char *ni_propval();
266765214Seric 		char *cflocation;
266865214Seric 
266968508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
267068508Seric 					"sendmail.cf", '\0');
267165214Seric 		if (cflocation != NULL)
267265214Seric 			return cflocation;
267365214Seric 	}
267465214Seric #endif
267568842Seric 
267669396Seric #ifdef TRY_VERSIONED_CF_NAME
267768842Seric 	/*
267868842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
267968842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
268068842Seric 	**
268168842Seric 	**	I suppose it should really try a search path here --
268268842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
268368842Seric 	**	/usr/lib/sendmail.cf, and so forth.
268468842Seric 	*/
268568842Seric 
268668842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
268768842Seric 	i = strlen(cbuf);
268868842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
268968842Seric 	{
269068842Seric 		char *p;
269168842Seric 		extern char Version[];
269268842Seric 
269368842Seric 		strcpy(&cbuf[i - 2], Version);
269468842Seric 		p = strchr(&cbuf[i - 2], '/');
269568842Seric 		if (p != NULL)
269668842Seric 			*p = '\0';
269768842Seric 		p = &cbuf[strlen(cbuf)];
269868842Seric 		do
269968842Seric 		{
270068842Seric 			int fd;
270168842Seric 
270268842Seric 			strcpy(p, ".cf");
270368842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
270468842Seric 			{
270568842Seric 				close(fd);
270668842Seric 				return cbuf;
270768842Seric 			}
270868842Seric 			*p = '\0';
270968842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
271068842Seric 	}
271169396Seric #endif
271264556Seric 	return _PATH_SENDMAILCF;
271364556Seric }
271464718Seric /*
271564718Seric **  SETVENDOR -- process vendor code from V configuration line
271664718Seric **
271764718Seric **	Parameters:
271864718Seric **		vendor -- string representation of vendor.
271964718Seric **
272064718Seric **	Returns:
272164718Seric **		TRUE -- if ok.
272264718Seric **		FALSE -- if vendor code could not be processed.
272364926Seric **
272464926Seric **	Side Effects:
272564926Seric **		It is reasonable to set mode flags here to tweak
272664926Seric **		processing in other parts of the code if necessary.
272764926Seric **		For example, if you are a vendor that uses $%y to
272864926Seric **		indicate YP lookups, you could enable that here.
272964718Seric */
273064718Seric 
273164718Seric bool
273264718Seric setvendor(vendor)
273364718Seric 	char *vendor;
273464718Seric {
273564926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
273668508Seric 	{
273768508Seric 		VendorCode = VENDOR_BERKELEY;
273864926Seric 		return TRUE;
273968508Seric 	}
274064926Seric 
274164926Seric 	/* add vendor extensions here */
274264926Seric 
274368508Seric #ifdef SUN_EXTENSIONS
274468508Seric 	if (strcasecmp(vendor, "Sun") == 0)
274568508Seric 	{
274668508Seric 		VendorCode = VENDOR_SUN;
274768508Seric 		return TRUE;
274868508Seric 	}
274968508Seric #endif
275068508Seric 
275164926Seric 	return FALSE;
275264718Seric }
275364816Seric /*
275464816Seric **  STRTOL -- convert string to long integer
275564816Seric **
275664816Seric **	For systems that don't have it in the C library.
275766161Seric **
275866161Seric **	This is taken verbatim from the 4.4-Lite C library.
275964816Seric */
276064816Seric 
276164816Seric #ifdef NEEDSTRTOL
276264816Seric 
276366161Seric #if defined(LIBC_SCCS) && !defined(lint)
276466161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
276566161Seric #endif /* LIBC_SCCS and not lint */
276666161Seric 
276766161Seric #include <limits.h>
276866161Seric 
276966161Seric /*
277066161Seric  * Convert a string to a long integer.
277166161Seric  *
277266161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
277366161Seric  * alphabets and digits are each contiguous.
277466161Seric  */
277566161Seric 
277664816Seric long
277766161Seric strtol(nptr, endptr, base)
277866161Seric 	const char *nptr;
277966161Seric 	char **endptr;
278066161Seric 	register int base;
278164816Seric {
278266161Seric 	register const char *s = nptr;
278366161Seric 	register unsigned long acc;
278466161Seric 	register int c;
278566161Seric 	register unsigned long cutoff;
278666161Seric 	register int neg = 0, any, cutlim;
278764816Seric 
278866161Seric 	/*
278966161Seric 	 * Skip white space and pick up leading +/- sign if any.
279066161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
279166161Seric 	 * assume decimal; if base is already 16, allow 0x.
279266161Seric 	 */
279366161Seric 	do {
279466161Seric 		c = *s++;
279566161Seric 	} while (isspace(c));
279666161Seric 	if (c == '-') {
279766161Seric 		neg = 1;
279866161Seric 		c = *s++;
279966161Seric 	} else if (c == '+')
280066161Seric 		c = *s++;
280166161Seric 	if ((base == 0 || base == 16) &&
280266161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
280366161Seric 		c = s[1];
280466161Seric 		s += 2;
280566161Seric 		base = 16;
280666161Seric 	}
280766161Seric 	if (base == 0)
280866161Seric 		base = c == '0' ? 8 : 10;
280964816Seric 
281066161Seric 	/*
281166161Seric 	 * Compute the cutoff value between legal numbers and illegal
281266161Seric 	 * numbers.  That is the largest legal value, divided by the
281366161Seric 	 * base.  An input number that is greater than this value, if
281466161Seric 	 * followed by a legal input character, is too big.  One that
281566161Seric 	 * is equal to this value may be valid or not; the limit
281666161Seric 	 * between valid and invalid numbers is then based on the last
281766161Seric 	 * digit.  For instance, if the range for longs is
281866161Seric 	 * [-2147483648..2147483647] and the input base is 10,
281966161Seric 	 * cutoff will be set to 214748364 and cutlim to either
282066161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
282166161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
282266161Seric 	 * the number is too big, and we will return a range error.
282366161Seric 	 *
282466161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
282566161Seric 	 * overflow.
282666161Seric 	 */
282766161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
282866161Seric 	cutlim = cutoff % (unsigned long)base;
282966161Seric 	cutoff /= (unsigned long)base;
283066161Seric 	for (acc = 0, any = 0;; c = *s++) {
283166161Seric 		if (isdigit(c))
283266161Seric 			c -= '0';
283366161Seric 		else if (isalpha(c))
283466161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
283566161Seric 		else
283666161Seric 			break;
283766161Seric 		if (c >= base)
283866161Seric 			break;
283966161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
284066161Seric 			any = -1;
284166161Seric 		else {
284266161Seric 			any = 1;
284366161Seric 			acc *= base;
284466161Seric 			acc += c;
284564816Seric 		}
284664816Seric 	}
284766161Seric 	if (any < 0) {
284866161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
284966161Seric 		errno = ERANGE;
285066161Seric 	} else if (neg)
285166161Seric 		acc = -acc;
285266161Seric 	if (endptr != 0)
285366161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
285466161Seric 	return (acc);
285564816Seric }
285664816Seric 
285764816Seric #endif
285864841Seric /*
285968693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
286064841Seric **
286168693Seric **	Some operating systems have wierd problems with the gethostbyXXX
286268693Seric **	routines.  For example, Solaris versions at least through 2.3
286368693Seric **	don't properly deliver a canonical h_name field.  This tries to
286468693Seric **	work around these problems.
286564841Seric */
286664841Seric 
286764841Seric struct hostent *
286868693Seric sm_gethostbyname(name)
286968707Seric 	char *name;
287064841Seric {
287168693Seric #if defined(SOLARIS) && SOLARIS < 204
287268693Seric 	extern int h_errno;
287368693Seric 
287468508Seric # if SOLARIS == 203
287564841Seric 	static struct hostent hp;
287664841Seric 	static char buf[1000];
287764841Seric 	extern struct hostent *_switch_gethostbyname_r();
287864841Seric 
287964841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
288064841Seric # else
288165009Seric 	extern struct hostent *__switch_gethostbyname();
288265009Seric 
288364841Seric 	return __switch_gethostbyname(name);
288464841Seric # endif
288568693Seric #else
288668693Seric 	return gethostbyname(name);
288768693Seric #endif
288864841Seric }
288964841Seric 
289064841Seric struct hostent *
289168693Seric sm_gethostbyaddr(addr, len, type)
289268707Seric 	char *addr;
289364841Seric 	int len;
289464841Seric 	int type;
289564841Seric {
289668693Seric #if defined(SOLARIS) && SOLARIS < 204
289768693Seric 	extern int h_errno;
289868693Seric 
289968508Seric # if SOLARIS == 203
290064841Seric 	static struct hostent hp;
290164841Seric 	static char buf[1000];
290264841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
290364841Seric 
290464841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
290564841Seric # else
290665009Seric 	extern struct hostent *__switch_gethostbyaddr();
290765009Seric 
290864841Seric 	return __switch_gethostbyaddr(addr, len, type);
290964841Seric # endif
291068693Seric #else
291168693Seric 	return gethostbyaddr(addr, len, type);
291268693Seric #endif
291364841Seric }
291468693Seric /*
291568693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
291668693Seric */
291764841Seric 
291868693Seric struct passwd *
291968693Seric sm_getpwnam(user)
292068704Seric 	char *user;
292168693Seric {
292268693Seric 	return getpwnam(user);
292368693Seric }
292468693Seric 
292568693Seric struct passwd *
292668693Seric sm_getpwuid(uid)
292768693Seric 	uid_t uid;
292868693Seric {
292968693Seric 	return getpwuid(uid);
293068693Seric }
293165214Seric /*
293269472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
293369472Seric **
293469472Seric **	Parameters:
293569472Seric **		none.
293669472Seric **
293769472Seric **	Returns:
293869472Seric **		none.
293969472Seric **
294069472Seric **	Side Effects:
294169472Seric **		Loads $=w with the names of all the interfaces.
294269472Seric */
294369472Seric 
294469472Seric #ifdef SIOCGIFCONF
294569472Seric # include <netdb.h>
294669472Seric # include <arpa/inet.h>
294769472Seric # include <net/if.h>
294869472Seric #endif
294969472Seric 
295069472Seric void
295169472Seric load_if_names()
295269472Seric {
295369472Seric #ifdef SIOCGIFCONF
295469472Seric 	struct hostent *hp;
295569472Seric 	int s;
295669472Seric         struct ifconf ifc;
295769472Seric         struct ifreq *ifr;
295869472Seric 	char interfacebuf[1024];
295969472Seric 	int n;
296069472Seric 	extern char *inet_ntoa();
296169472Seric 	extern struct hostent *gethostbyaddr();
296269472Seric 
296369472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
296469472Seric 	if (s == -1)
296569472Seric 		return;
296669472Seric 
296769472Seric 	/* get the list of known IP address from the kernel */
296869472Seric         ifc.ifc_len = sizeof(interfacebuf);
296969472Seric         ifc.ifc_buf = interfacebuf;
297069472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
297169472Seric 	{
297269472Seric 		if (tTd(0, 4))
297369472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
297469472Seric 		return;
297569472Seric 	}
297669472Seric 
297769472Seric 	/* scan the list of IP address */
297869472Seric 	if (tTd(0, 4))
297969472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
298069472Seric 			ifc.ifc_len);
298169472Seric 
298269472Seric         ifr = ifc.ifc_req;
298369472Seric         for (n = ifc.ifc_len / sizeof(struct ifreq); n > 0; n--, ifr++)
298469472Seric         {
298569472Seric 		struct in_addr ia;
298669472Seric 		char ip_addr[256];
298769472Seric 
298869472Seric 		if (tTd(0, 20))
298969472Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) &ifr->ifr_addr));
299069472Seric 
299169472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
299269472Seric 			continue;
299369472Seric 
299469472Seric 		/* extract IP address from the list*/
299569472Seric 		ia = (((struct sockaddr_in *) (&ifr->ifr_addr))->sin_addr);
299669472Seric 
299769472Seric 		/* save IP address in text from */
299869472Seric 		(void) sprintf(ip_addr, "[%s]",
299969472Seric 			inet_ntoa(((struct sockaddr_in *)(&ifr->ifr_addr))->sin_addr));
300069472Seric 		if (!wordinclass(ip_addr, 'w'))
300169472Seric 		{
300269472Seric 			setclass('w', ip_addr);
300369472Seric 			if (tTd(0, 4))
300469472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
300569472Seric 		}
300669472Seric 
300769472Seric 		/* skip "loopback" interface "lo" */
300869472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
300969472Seric 			continue;
301069472Seric 
301169472Seric 		/* lookup name with IP address */
301269472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
301369472Seric 		if (hp == NULL)
301469472Seric 		{
301569472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
301669472Seric 				inet_ntoa(ia));
301769472Seric 			continue;
301869472Seric 		}
301969472Seric 
302069472Seric 		/* save its cname */
302169472Seric 		if (!wordinclass(hp->h_name, 'w'))
302269472Seric 		{
302369472Seric 			setclass('w', hp->h_name);
302469472Seric 			if (tTd(0, 4))
302569472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
302669472Seric 		}
302769472Seric 
302869472Seric 		/* save all it aliases name */
302969472Seric 		while (*hp->h_aliases)
303069472Seric 		{
303169472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
303269472Seric 			{
303369472Seric 				setclass('w', *hp->h_aliases);
303469472Seric 				if (tTd(0, 4))
303569472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
303669472Seric 			}
303769472Seric 			hp->h_aliases++;
303869472Seric 		}
303969472Seric 	}
304069472Seric #endif
304169472Seric }
304269472Seric /*
304365214Seric **  NI_PROPVAL -- netinfo property value lookup routine
304465214Seric **
304565214Seric **	Parameters:
304668508Seric **		keydir -- the Netinfo directory name in which to search
304768508Seric **			for the key.
304868508Seric **		keyprop -- the name of the property in which to find the
304968508Seric **			property we are interested.  Defaults to "name".
305068508Seric **		keyval -- the value for which we are really searching.
305168508Seric **		valprop -- the property name for the value in which we
305268508Seric **			are interested.
305368508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
305468508Seric **			we should return a string separated by this
305568508Seric **			character.
305665214Seric **
305765214Seric **	Returns:
305865214Seric **		NULL -- if:
305965214Seric **			1. the directory is not found
306065214Seric **			2. the property name is not found
306165214Seric **			3. the property contains multiple values
306265214Seric **			4. some error occured
306365214Seric **		else -- the location of the config file.
306465214Seric **
306568508Seric **	Example:
306668508Seric **		To search for an alias value, use:
306768508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
306868508Seric **
306965214Seric **	Notes:
307065214Seric **      	Caller should free the return value of ni_proval
307165214Seric */
307265214Seric 
307365214Seric #ifdef NETINFO
307465214Seric 
307565214Seric # include <netinfo/ni.h>
307665214Seric 
307765214Seric # define LOCAL_NETINFO_DOMAIN    "."
307865214Seric # define PARENT_NETINFO_DOMAIN   ".."
307965214Seric # define MAX_NI_LEVELS           256
308065214Seric 
308165214Seric char *
308268508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
308368508Seric 	char *keydir;
308468508Seric 	char *keyprop;
308568508Seric 	char *keyval;
308668508Seric 	char *valprop;
308768508Seric 	char sepchar;
308865214Seric {
308965820Seric 	char *propval = NULL;
309065214Seric 	int i;
309168508Seric 	int j, alen;
309265214Seric 	void *ni = NULL;
309365214Seric 	void *lastni = NULL;
309465214Seric 	ni_status nis;
309565214Seric 	ni_id nid;
309665214Seric 	ni_namelist ninl;
309768508Seric 	register char *p;
309868508Seric 	char keybuf[1024];
309965214Seric 
310065214Seric 	/*
310168508Seric 	**  Create the full key from the two parts.
310268508Seric 	**
310368508Seric 	**	Note that directory can end with, e.g., "name=" to specify
310468508Seric 	**	an alternate search property.
310568508Seric 	*/
310668508Seric 
310768508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
310868508Seric 	if (keyprop != NULL)
310968508Seric 		i += strlen(keyprop) + 1;
311068508Seric 	if (i > sizeof keybuf)
311168508Seric 		return NULL;
311268508Seric 	strcpy(keybuf, keydir);
311368508Seric 	strcat(keybuf, "/");
311468508Seric 	if (keyprop != NULL)
311568508Seric 	{
311668508Seric 		strcat(keybuf, keyprop);
311768508Seric 		strcat(keybuf, "=");
311868508Seric 	}
311968508Seric 	strcat(keybuf, keyval);
312068508Seric 
312168508Seric 	/*
312265214Seric 	**  If the passed directory and property name are found
312365214Seric 	**  in one of netinfo domains we need to search (starting
312465214Seric 	**  from the local domain moving all the way back to the
312565214Seric 	**  root domain) set propval to the property's value
312665214Seric 	**  and return it.
312765214Seric 	*/
312865214Seric 
312965214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
313065214Seric 	{
313165214Seric 		if (i == 0)
313265214Seric 		{
313365214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
313465214Seric 		}
313565214Seric 		else
313665214Seric 		{
313765214Seric 			if (lastni != NULL)
313865214Seric 				ni_free(lastni);
313965214Seric 			lastni = ni;
314065214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
314165214Seric 		}
314265214Seric 
314365214Seric 		/*
314465214Seric 		**  Don't bother if we didn't get a handle on a
314565214Seric 		**  proper domain.  This is not necessarily an error.
314665214Seric 		**  We would get a positive ni_status if, for instance
314765214Seric 		**  we never found the directory or property and tried
314865214Seric 		**  to open the parent of the root domain!
314965214Seric 		*/
315065214Seric 
315165214Seric 		if (nis != 0)
315265214Seric 			break;
315365214Seric 
315465214Seric 		/*
315565214Seric 		**  Find the path to the server information.
315665214Seric 		*/
315765214Seric 
315868508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
315965214Seric 			continue;
316065214Seric 
316165214Seric 		/*
316268508Seric 		**  Find associated value information.
316365214Seric 		*/
316465214Seric 
316568508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
316665214Seric 			continue;
316765214Seric 
316865214Seric 		/*
316968508Seric 		**  See if we have an acceptable number of values.
317065214Seric 		*/
317165214Seric 
317268508Seric 		if (ninl.ni_namelist_len <= 0)
317368508Seric 			continue;
317468508Seric 
317568508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
317665214Seric 		{
317768508Seric 			ni_namelist_free(&ninl);
317868508Seric 			continue;
317965214Seric 		}
318068508Seric 
318169472Seric 		/*
318268508Seric 		**  Calculate number of bytes needed and build result
318368508Seric 		*/
318468508Seric 
318568508Seric 		alen = 1;
318668508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
318768508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
318868508Seric 		propval = p = xalloc(alen);
318968508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
319068508Seric 		{
319168508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
319268508Seric 			p += strlen(p);
319368508Seric 			*p++ = sepchar;
319469472Seric 		}
319568508Seric 		*--p = '\0';
319668508Seric 
319768508Seric 		ni_namelist_free(&ninl);
319865214Seric 	}
319965214Seric 
320065214Seric 	/*
320165214Seric 	**  Clean up.
320265214Seric 	*/
320365214Seric 
320465214Seric 	if (ni != NULL)
320565214Seric 		ni_free(ni);
320665214Seric 	if (lastni != NULL && ni != lastni)
320765214Seric 		ni_free(lastni);
320865214Seric 
320965214Seric 	return propval;
321065214Seric }
321165214Seric 
321265214Seric #endif /* NETINFO */
321365504Seric /*
321465504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
321565504Seric **
321665504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
321765504Seric **	syslog succeeds during interrupt handlers.
321865504Seric */
321965504Seric 
322065504Seric #ifdef __hpux
322165504Seric 
322265504Seric # define MAXSYSLOGTRIES	100
322365504Seric # undef syslog
322465504Seric 
322565504Seric # ifdef __STDC__
322665504Seric hard_syslog(int pri, char *msg, ...)
322765504Seric # else
322865504Seric hard_syslog(pri, msg, va_alist)
322965504Seric 	int pri;
323065504Seric 	char *msg;
323165504Seric 	va_dcl
323265504Seric # endif
323365504Seric {
323465504Seric 	int i;
323565504Seric 	char buf[SYSLOG_BUFSIZE * 2];
323665504Seric 	VA_LOCAL_DECL;
323765504Seric 
323865504Seric 	VA_START(msg);
323965504Seric 	vsprintf(buf, msg, ap);
324065504Seric 	VA_END;
324165504Seric 
324265504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
324365504Seric 		continue;
324465504Seric }
324565504Seric 
324665504Seric #endif
324768696Seric /*
324868696Seric **  LOCAL_HOSTNAME_LENGTH
324968696Seric **
325068696Seric **	This is required to get sendmail to compile against BIND 4.9.x
325168696Seric **	on Ultrix.
325268696Seric */
325368696Seric 
325468696Seric #if defined(ultrix) && NAMED_BIND
325568696Seric 
325668696Seric # include <resolv.h>
325768696Seric # if __RES >= 19931104
325868696Seric 
325968696Seric int
326068696Seric local_hostname_length(hostname)
326168696Seric 	char *hostname;
326268696Seric {
326368696Seric 	int len_host, len_domain;
326468696Seric 
326568696Seric 	if (!*_res.defdname)
326668696Seric 		res_init();
326768696Seric 	len_host = strlen(hostname);
326868696Seric 	len_domain = strlen(_res.defdname);
326968696Seric 	if (len_host > len_domain &&
327068696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
327168696Seric 	    hostname[len_host - len_domain - 1] == '.')
327268696Seric 		return len_host - len_domain - 1;
327368696Seric 	else
327468696Seric 		return 0;
327568696Seric }
327668696Seric 
327768696Seric # endif
327868696Seric #endif
327968741Seric /*
328068741Seric **  Compile-Time options
328168741Seric */
328268741Seric 
328368741Seric char	*CompileOptions[] =
328468741Seric {
328568741Seric #if HESIOD
328668741Seric 	"HESIOD",
328768741Seric #endif
328868741Seric #if LOG
328968741Seric 	"LOG",
329068741Seric #endif
329168741Seric #if MATCHGECOS
329268741Seric 	"MATCHGECOS",
329368741Seric #endif
329468741Seric #if NAMED_BIND
329568741Seric 	"NAMED_BIND",
329668741Seric #endif
329768741Seric #if NDBM
329868741Seric 	"NDBM",
329968741Seric #endif
330068741Seric #if NETINET
330168741Seric 	"NETINET",
330268741Seric #endif
330368741Seric #if NETINFO
330468741Seric 	"NETINFO",
330568741Seric #endif
330668741Seric #if NETISO
330768741Seric 	"NETISO",
330868741Seric #endif
330968741Seric #if NETNS
331068741Seric 	"NETNS",
331168741Seric #endif
331268741Seric #if NETUNIX
331368741Seric 	"NETUNIX",
331468741Seric #endif
331568741Seric #if NETX25
331668741Seric 	"NETX25",
331768741Seric #endif
331868741Seric #if NEWDB
331968741Seric 	"NEWDB",
332068741Seric #endif
332168741Seric #if NIS
332268741Seric 	"NIS",
332368741Seric #endif
332468741Seric #if SCANF
332568741Seric 	"SCANF",
332668741Seric #endif
332768741Seric #if SUID_ROOT_FILES_OK
332868741Seric 	"SUID_ROOT_FILES_OK",
332968741Seric #endif
333068741Seric #if USERDB
333168741Seric 	"USERDB",
333268741Seric #endif
333368741Seric #if XDEBUG
333468741Seric 	"XDEBUG",
333568741Seric #endif
333668741Seric #if XLA
333768741Seric 	"XLA",
333868741Seric #endif
333968741Seric 	NULL
334068741Seric };
3341