xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 69801)
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*69801Seric static char sccsid[] = "@(#)conf.c	8.187 (Berkeley) 06/05/95";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
17404Seric 
18294Seric /*
193309Seric **  CONF.C -- Sendmail Configuration Tables.
20294Seric **
21294Seric **	Defines the configuration of this installation.
22294Seric **
231388Seric **	Configuration Variables:
242897Seric **		HdrInfo -- a table describing well-known header fields.
252897Seric **			Each entry has the field name and some flags,
264147Seric **			which are described in sendmail.h.
274093Seric **
284093Seric **	Notes:
294093Seric **		I have tried to put almost all the reasonable
304093Seric **		configuration information into the configuration
314093Seric **		file read at runtime.  My intent is that anything
324093Seric **		here is a function of the version of UNIX you
334093Seric **		are running, or is really static -- for example
344093Seric **		the headers are a superset of widely used
354093Seric **		protocols.  If you find yourself playing with
364093Seric **		this file too much, you may be making a mistake!
37294Seric */
38294Seric 
39294Seric 
40294Seric 
41294Seric 
424437Seric /*
432897Seric **  Header info table
443057Seric **	Final (null) entry contains the flags used for any other field.
454147Seric **
464147Seric **	Not all of these are actually handled specially by sendmail
474147Seric **	at this time.  They are included as placeholders, to let
484147Seric **	you know that "someday" I intend to have sendmail do
494147Seric **	something with them.
502897Seric */
512897Seric 
522897Seric struct hdrinfo	HdrInfo[] =
532897Seric {
548060Seric 		/* originator fields, most to least significant  */
5568508Seric 	"resent-sender",		H_FROM|H_RESENT,
5668508Seric 	"resent-from",			H_FROM|H_RESENT,
5768508Seric 	"resent-reply-to",		H_FROM|H_RESENT,
5868508Seric 	"sender",			H_FROM,
5968508Seric 	"from",				H_FROM,
6068508Seric 	"reply-to",			H_FROM,
6168508Seric 	"full-name",			H_ACHECK,
6268508Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
6368508Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6458796Seric 
658060Seric 		/* destination fields */
6668508Seric 	"to",				H_RCPT,
6768508Seric 	"resent-to",			H_RCPT|H_RESENT,
6868508Seric 	"cc",				H_RCPT,
6968508Seric 	"resent-cc",			H_RCPT|H_RESENT,
7068508Seric 	"bcc",				H_RCPT|H_STRIPVAL,
7168508Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
7268508Seric 	"apparently-to",		H_RCPT,
7358796Seric 
748060Seric 		/* message identification and control */
7568508Seric 	"message-id",			0,
7668508Seric 	"resent-message-id",		H_RESENT,
7768508Seric 	"message",			H_EOH,
7868508Seric 	"text",				H_EOH,
7958796Seric 
8011417Seric 		/* date fields */
8168508Seric 	"date",				0,
8268508Seric 	"resent-date",			H_RESENT,
8358796Seric 
848060Seric 		/* trace fields */
8568508Seric 	"received",			H_TRACE|H_FORCE,
8668508Seric 	"x400-received",		H_TRACE|H_FORCE,
8768508Seric 	"via",				H_TRACE|H_FORCE,
8868508Seric 	"mail-from",			H_TRACE|H_FORCE,
898060Seric 
9058796Seric 		/* miscellaneous fields */
9168508Seric 	"comments",			H_FORCE,
9268508Seric 	"return-path",			H_FORCE|H_ACHECK,
9368508Seric 	"content-transfer-encoding",	H_CTE,
9468508Seric 	"content-type",			H_CTYPE,
9568706Seric 	"content-length",		H_ACHECK,
9658796Seric 
979055Seric 	NULL,			0,
982897Seric };
994166Seric 
1004166Seric 
1014166Seric 
1024282Seric /*
1034282Seric **  Location of system files/databases/etc.
1044282Seric */
1054282Seric 
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12264333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12364333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12566783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12658082Seric 	"goaway",		PRIV_GOAWAY,
12758789Seric 	NULL,			0,
12858082Seric };
12958082Seric 
13058082Seric 
13158082Seric 
13258082Seric /*
13324943Seric **  Miscellaneous stuff.
1349039Seric */
1359039Seric 
13624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13724943Seric /*
13824943Seric **  SETDEFAULTS -- set default values
13924943Seric **
14024943Seric **	Because of the way freezing is done, these must be initialized
14124943Seric **	using direct code.
14224943Seric **
14324943Seric **	Parameters:
14458734Seric **		e -- the default envelope.
14524943Seric **
14624943Seric **	Returns:
14724943Seric **		none.
14824943Seric **
14924943Seric **	Side Effects:
15024943Seric **		Initializes a bunch of global variables to their
15124943Seric **		default values.
15224943Seric */
15324943Seric 
15458737Seric #define DAYS		* 24 * 60 * 60
15558737Seric 
15668508Seric void
15758734Seric setdefaults(e)
15858734Seric 	register ENVELOPE *e;
15924943Seric {
16068508Seric 	int i;
16168508Seric 	extern void inittimeouts();
16268508Seric 	extern void setdefuser();
16368508Seric 	extern void setupmaps();
16468508Seric 	extern void setupmailers();
16568508Seric 
16657438Seric 	SpaceSub = ' ';				/* option B */
16757438Seric 	QueueLA = 8;				/* option x */
16857438Seric 	RefuseLA = 12;				/* option X */
16957438Seric 	WkRecipFact = 30000L;			/* option y */
17057438Seric 	WkClassFact = 1800L;			/* option z */
17157438Seric 	WkTimeFact = 90000L;			/* option Z */
17257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17457438Seric 						/* option F */
17557438Seric 	DefUid = 1;				/* option u */
17657438Seric 	DefGid = 1;				/* option g */
17757438Seric 	CheckpointInterval = 10;		/* option C */
17857438Seric 	MaxHopCount = 25;			/* option h */
17958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
18168508Seric 	SevenBitInput = FALSE;			/* option 7 */
18257438Seric 	MaxMciCache = 1;			/* option k */
18357438Seric 	MciCacheTimeout = 300;			/* option K */
18457438Seric 	LogLevel = 9;				/* option L */
18568508Seric 	inittimeouts(NULL);			/* option r */
18658853Seric 	PrivacyFlags = 0;			/* option p */
18769480Seric #if MIME8TO7
18868508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18969480Seric #else
19069480Seric 	MimeMode = MM_PASS8BIT;
19169480Seric #endif
19268508Seric 	for (i = 0; i < MAXTOCLASS; i++)
19368508Seric 	{
19468508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19568508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19668508Seric 	}
19768508Seric 	ServiceSwitchFile = "/etc/service.switch";
19869401Seric 	HostsFile = _PATH_HOSTS;
19940973Sbostic 	setdefuser();
20053654Seric 	setupmaps();
20157402Seric 	setupmailers();
20224943Seric }
203294Seric 
20440973Sbostic 
2054326Seric /*
20640973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20740973Sbostic */
20840973Sbostic 
20968508Seric void
21040973Sbostic setdefuser()
21140973Sbostic {
21240973Sbostic 	struct passwd *defpwent;
21357386Seric 	static char defuserbuf[40];
21440973Sbostic 
21557386Seric 	DefUser = defuserbuf;
21668693Seric 	if ((defpwent = sm_getpwuid(DefUid)) != NULL)
21757386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21840973Sbostic 	else
21957386Seric 		strcpy(defuserbuf, "nobody");
22040973Sbostic }
22153654Seric /*
22256836Seric **  HOST_MAP_INIT -- initialize host class structures
22356836Seric */
22456836Seric 
22568508Seric bool	host_map_init __P((MAP *map, char *args));
22668508Seric 
22756836Seric bool
22860219Seric host_map_init(map, args)
22956836Seric 	MAP *map;
23056836Seric 	char *args;
23156836Seric {
23256836Seric 	register char *p = args;
23356836Seric 
23456836Seric 	for (;;)
23556836Seric 	{
23658050Seric 		while (isascii(*p) && isspace(*p))
23756836Seric 			p++;
23856836Seric 		if (*p != '-')
23956836Seric 			break;
24056836Seric 		switch (*++p)
24156836Seric 		{
24256836Seric 		  case 'a':
24356836Seric 			map->map_app = ++p;
24456836Seric 			break;
24556836Seric 		}
24658050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24756836Seric 			p++;
24856836Seric 		if (*p != '\0')
24956836Seric 			*p++ = '\0';
25056836Seric 	}
25156836Seric 	if (map->map_app != NULL)
25256836Seric 		map->map_app = newstr(map->map_app);
25356836Seric 	return TRUE;
25456836Seric }
25557402Seric /*
25657402Seric **  SETUPMAILERS -- initialize default mailers
25757402Seric */
25856836Seric 
25968508Seric void
26057402Seric setupmailers()
26157402Seric {
26257402Seric 	char buf[100];
26368508Seric 	extern void makemailer();
26457402Seric 
26569793Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=DNS/RFC822/X-Unix, A=sh -c \201u");
26657403Seric 	makemailer(buf);
26757403Seric 
26869721Seric 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=DNS/RFC822/X-Unix, A=FILE");
26957402Seric 	makemailer(buf);
27057402Seric 
27157402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
27257402Seric 	makemailer(buf);
27357402Seric }
27456836Seric /*
27560207Seric **  SETUPMAPS -- set up map classes
27660207Seric */
27760207Seric 
27860207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27960207Seric 	{ \
28060207Seric 		extern bool parse __P((MAP *, char *)); \
28160207Seric 		extern bool open __P((MAP *, int)); \
28260207Seric 		extern void close __P((MAP *)); \
28360207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
28460207Seric 		extern void store __P((MAP *, char *, char *)); \
28560207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28660207Seric 		s->s_mapclass.map_cname = name; \
28760207Seric 		s->s_mapclass.map_ext = ext; \
28860207Seric 		s->s_mapclass.map_cflags = flags; \
28960207Seric 		s->s_mapclass.map_parse = parse; \
29060207Seric 		s->s_mapclass.map_open = open; \
29160207Seric 		s->s_mapclass.map_close = close; \
29260207Seric 		s->s_mapclass.map_lookup = lookup; \
29360207Seric 		s->s_mapclass.map_store = store; \
29460207Seric 	}
29560207Seric 
29668508Seric void
29760207Seric setupmaps()
29860207Seric {
29960207Seric 	register STAB *s;
30060207Seric 
30160207Seric #ifdef NEWDB
30260207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, hash_map_open, db_map_close,
30460207Seric 		db_map_lookup, db_map_store);
30568508Seric 
30660207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30760207Seric 		map_parseargs, bt_map_open, db_map_close,
30860207Seric 		db_map_lookup, db_map_store);
30960207Seric #endif
31060207Seric 
31160207Seric #ifdef NDBM
31260207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
31360207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
31460207Seric 		ndbm_map_lookup, ndbm_map_store);
31560207Seric #endif
31660207Seric 
31760207Seric #ifdef NIS
31860207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31968508Seric 		map_parseargs, nis_map_open, null_map_close,
32068508Seric 		nis_map_lookup, null_map_store);
32160207Seric #endif
32260207Seric 
32368508Seric #ifdef NISPLUS
32468508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32568508Seric 		map_parseargs, nisplus_map_open, null_map_close,
32668508Seric 		nisplus_map_lookup, null_map_store);
32768508Seric #endif
32868508Seric 
32968508Seric #ifdef HESIOD
33068508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
33168508Seric 		map_parseargs, null_map_open, null_map_close,
33268508Seric 		hes_map_lookup, null_map_store);
33368508Seric #endif
33468508Seric 
33568508Seric #ifdef NETINFO
33668508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33768508Seric 		map_parseargs, ni_map_open, null_map_close,
33868508Seric 		ni_map_lookup, null_map_store);
33968508Seric #endif
34068508Seric 
34168508Seric #if 0
34268508Seric 	MAPDEF("dns", NULL, 0,
34368508Seric 		dns_map_init, null_map_open, null_map_close,
34468508Seric 		dns_map_lookup, null_map_store);
34568508Seric #endif
34668508Seric 
34768508Seric #if NAMED_BIND
34868508Seric 	/* best MX DNS lookup */
34968508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
35068508Seric 		map_parseargs, null_map_open, null_map_close,
35168508Seric 		bestmx_map_lookup, null_map_store);
35268508Seric #endif
35368508Seric 
35468508Seric 	MAPDEF("host", NULL, 0,
35568508Seric 		host_map_init, null_map_open, null_map_close,
35668508Seric 		host_map_lookup, null_map_store);
35768508Seric 
35868508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35968508Seric 		map_parseargs, text_map_open, null_map_close,
36068508Seric 		text_map_lookup, null_map_store);
36168508Seric 
36260207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
36368508Seric 		map_parseargs, stab_map_open, null_map_close,
36460207Seric 		stab_map_lookup, stab_map_store);
36560207Seric 
36660207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36760207Seric 		map_parseargs, impl_map_open, impl_map_close,
36860207Seric 		impl_map_lookup, impl_map_store);
36960207Seric 
37068508Seric 	/* access to system passwd file */
37168508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
37268508Seric 		map_parseargs, user_map_open, null_map_close,
37368508Seric 		user_map_lookup, null_map_store);
37460207Seric 
37560207Seric 	/* dequote map */
37660207Seric 	MAPDEF("dequote", NULL, 0,
37760207Seric 		dequote_init, null_map_open, null_map_close,
37860207Seric 		dequote_map, null_map_store);
37960207Seric 
38069717Seric #if USERDB
38160207Seric 	/* user database */
38268521Seric 	MAPDEF("userdb", ".db", 0,
38368521Seric 		map_parseargs, null_map_open, null_map_close,
38460207Seric 		udb_map_lookup, null_map_store);
38560207Seric #endif
38668508Seric 
38769453Seric 	/* arbitrary programs */
38869499Seric 	MAPDEF("program", NULL, MCF_ALIASOK,
38969453Seric 		map_parseargs, null_map_open, null_map_close,
39069453Seric 		prog_map_lookup, null_map_store);
39169453Seric 
39268508Seric 	/* sequenced maps */
39368508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
39468508Seric 		seq_map_parse, null_map_open, null_map_close,
39568508Seric 		seq_map_lookup, seq_map_store);
39668508Seric 
39768508Seric 	/* switched interface to sequenced maps */
39868508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
39968508Seric 		map_parseargs, switch_map_open, null_map_close,
40068508Seric 		seq_map_lookup, seq_map_store);
40160207Seric }
40260207Seric 
40360207Seric #undef MAPDEF
40460207Seric /*
40568508Seric **  INITHOSTMAPS -- initial host-dependent maps
40668508Seric **
40768508Seric **	This should act as an interface to any local service switch
40868508Seric **	provided by the host operating system.
40968508Seric **
41068508Seric **	Parameters:
41168508Seric **		none
41268508Seric **
41368508Seric **	Returns:
41468508Seric **		none
41568508Seric **
41668508Seric **	Side Effects:
41768508Seric **		Should define maps "host" and "users" as necessary
41868508Seric **		for this OS.  If they are not defined, they will get
41968508Seric **		a default value later.  It should check to make sure
42068508Seric **		they are not defined first, since it's possible that
42168508Seric **		the config file has provided an override.
42268508Seric */
42368508Seric 
42468508Seric void
42568508Seric inithostmaps()
42668508Seric {
42768508Seric 	register int i;
42868508Seric 	int nmaps;
42968508Seric 	char *maptype[MAXMAPSTACK];
43068508Seric 	short mapreturn[MAXMAPACTIONS];
43168508Seric 	char buf[MAXLINE];
43268508Seric 
43368508Seric 	/*
43468508Seric 	**  Set up default hosts maps.
43568508Seric 	*/
43668508Seric 
43768508Seric #if 0
43868508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
43968508Seric 	for (i = 0; i < nmaps; i++)
44068508Seric 	{
44168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
44268508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
44368508Seric 		{
44468508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
44569774Seric 			(void) makemapentry(buf);
44668508Seric 		}
44768508Seric #if NAMED_BIND
44868508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
44968508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
45068508Seric 		{
45168508Seric 			strcpy(buf, "hosts.dns dns A");
45269774Seric 			(void) makemapentry(buf);
45368508Seric 		}
45468508Seric #endif
45568508Seric #ifdef NISPLUS
45668508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
45768508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
45868508Seric 		{
45968508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
46069774Seric 			(void) makemapentry(buf);
46168508Seric 		}
46268508Seric #endif
46368508Seric #ifdef NIS
46468508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
46568508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
46668508Seric 		{
46768508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
46869774Seric 			(void) makemapentry(buf);
46968508Seric 		}
47068508Seric #endif
47168508Seric 	}
47268508Seric #endif
47368508Seric 
47468508Seric 	/*
47568508Seric 	**  Make sure we have a host map.
47668508Seric 	*/
47768508Seric 
47868508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
47968508Seric 	{
48068508Seric 		/* user didn't initialize: set up host map */
48168508Seric 		strcpy(buf, "host host");
48268508Seric #if NAMED_BIND
48368508Seric 		if (ConfigLevel >= 2)
48468508Seric 			strcat(buf, " -a.");
48568508Seric #endif
48669774Seric 		(void) makemapentry(buf);
48768508Seric 	}
48868508Seric 
48968508Seric 	/*
49068508Seric 	**  Set up default aliases maps
49168508Seric 	*/
49268508Seric 
49368508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
49468508Seric 	for (i = 0; i < nmaps; i++)
49568508Seric 	{
49668508Seric 		if (strcmp(maptype[i], "files") == 0 &&
49768508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
49868508Seric 		{
49968508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
50069774Seric 			(void) makemapentry(buf);
50168508Seric 		}
50268508Seric #ifdef NISPLUS
50368508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
50468508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
50568508Seric 		{
50668508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
50769774Seric 			(void) makemapentry(buf);
50868508Seric 		}
50968508Seric #endif
51068508Seric #ifdef NIS
51168508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
51268508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
51368508Seric 		{
51468508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
51569774Seric 			(void) makemapentry(buf);
51668508Seric 		}
51768508Seric #endif
51869774Seric #ifdef HESIOD
51969774Seric 		else if (strcmp(maptype[i], "hesiod") == 0 &&
52069774Seric 		    stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL)
52169774Seric 		{
52269774Seric 			strcpy(buf, "aliases.hesiod hesiod aliases");
52369774Seric 			(void) makemapentry(buf);
52469774Seric 		}
52569774Seric #endif
52668508Seric 	}
52769774Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
52868508Seric 	{
52968508Seric 		strcpy(buf, "aliases switch aliases");
53069774Seric 		(void) makemapentry(buf);
53168508Seric 	}
53268508Seric 
53368508Seric #if 0		/* "user" map class is a better choice */
53468508Seric 	/*
53568508Seric 	**  Set up default users maps.
53668508Seric 	*/
53768508Seric 
53868508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
53968508Seric 	for (i = 0; i < nmaps; i++)
54068508Seric 	{
54168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
54268508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
54368508Seric 		{
54468508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
54569774Seric 			(void) makemapentry(buf);
54668508Seric 		}
54768508Seric #ifdef NISPLUS
54868508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
54968508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
55068508Seric 		{
55168508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
55269774Seric 			(void) makemapentry(buf);
55368508Seric 		}
55468508Seric #endif
55568508Seric #ifdef NIS
55668508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
55768508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
55868508Seric 		{
55968508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
56069774Seric 			(void) makemapentry(buf);
56168508Seric 		}
56268508Seric #endif
56368508Seric #ifdef HESIOD
56468508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
56568508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
56668508Seric 		{
56768508Seric 			strcpy(buf, "users.hesiod hesiod");
56869774Seric 			(void) makemapentry(buf);
56968508Seric 		}
57068508Seric #endif
57168508Seric 	}
57268508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
57368508Seric 	{
57468508Seric 		strcpy(buf, "users switch -m passwd");
57569774Seric 		(void) makemapentry(buf);
57668508Seric 	}
57768508Seric #endif
57868508Seric }
57968508Seric /*
58068508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
58168508Seric **
58268508Seric **	This is the system-dependent interface to the service switch.
58368508Seric **
58468508Seric **	Parameters:
58568508Seric **		service -- the name of the service of interest.
58668508Seric **		maptype -- an out-array of strings containing the types
58768508Seric **			of access to use for this service.  There can
58868508Seric **			be at most MAXMAPSTACK types for a single service.
58968508Seric **		mapreturn -- an out-array of return information bitmaps
59068508Seric **			for the map.
59168508Seric **
59268508Seric **	Returns:
59368508Seric **		The number of map types filled in, or -1 for failure.
59468508Seric */
59568508Seric 
59668508Seric #ifdef SOLARIS
59768508Seric # include <nsswitch.h>
59868508Seric #endif
59968508Seric 
60068508Seric #if defined(ultrix) || defined(__osf__)
60168508Seric # include <sys/svcinfo.h>
60268508Seric #endif
60368508Seric 
60468508Seric int
60568508Seric switch_map_find(service, maptype, mapreturn)
60668508Seric 	char *service;
60768508Seric 	char *maptype[MAXMAPSTACK];
60868508Seric 	short mapreturn[MAXMAPACTIONS];
60968508Seric {
61068508Seric 	register FILE *fp;
61168508Seric 	int svcno;
61268508Seric 	static char buf[MAXLINE];
61368508Seric 
61468508Seric #ifdef SOLARIS
61568508Seric 	struct __nsw_switchconfig *nsw_conf;
61668508Seric 	enum __nsw_parse_err pserr;
61768508Seric 	struct __nsw_lookup *lk;
61868508Seric 	int nsw_rc;
61968508Seric 	static struct __nsw_lookup lkp0 =
62068508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
62168508Seric 	static struct __nsw_switchconfig lkp_default =
62268508Seric 		{ 0, "sendmail", 3, &lkp0 };
62368508Seric 
62468508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
62568508Seric 		lk = lkp_default.lookups;
62668508Seric 	else
62768508Seric 		lk = nsw_conf->lookups;
62868508Seric 	svcno = 0;
62968508Seric 	while (lk != NULL)
63068508Seric 	{
63168508Seric 		maptype[svcno] = lk->service_name;
63268508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
63368508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
63468508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
63568508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
63668508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
63768508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
63868508Seric 		svcno++;
63968508Seric 		lk = lk->next;
64068508Seric 	}
64168508Seric 	return svcno;
64268508Seric #endif
64368508Seric 
64468508Seric #if defined(ultrix) || defined(__osf__)
64568508Seric 	struct svcinfo *svcinfo;
64668508Seric 	int svc;
64768508Seric 
64869401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
64969401Seric 		mapreturn[svcno] = 0;
65069401Seric 
65168508Seric 	svcinfo = getsvc();
65268508Seric 	if (svcinfo == NULL)
65368508Seric 		goto punt;
65468508Seric 	if (strcmp(service, "hosts") == 0)
65568508Seric 		svc = SVC_HOSTS;
65668508Seric 	else if (strcmp(service, "aliases") == 0)
65768508Seric 		svc = SVC_ALIASES;
65868508Seric 	else if (strcmp(service, "passwd") == 0)
65968508Seric 		svc = SVC_PASSWD;
66068508Seric 	else
66168508Seric 		return -1;
66268508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
66368508Seric 	{
66468508Seric 		switch (svcinfo->svcpath[svc][svcno])
66568508Seric 		{
66668508Seric 		  case SVC_LOCAL:
66768508Seric 			maptype[svcno] = "files";
66868508Seric 			break;
66968508Seric 
67068508Seric 		  case SVC_YP:
67168508Seric 			maptype[svcno] = "nis";
67268508Seric 			break;
67368508Seric 
67468508Seric 		  case SVC_BIND:
67568508Seric 			maptype[svcno] = "dns";
67668508Seric 			break;
67768508Seric 
67868508Seric #ifdef SVC_HESIOD
67968508Seric 		  case SVC_HESIOD:
68068508Seric 			maptype[svcno] = "hesiod";
68168508Seric 			break;
68268508Seric #endif
68368508Seric 
68468508Seric 		  case SVC_LAST:
68568508Seric 			return svcno;
68668508Seric 		}
68768508Seric 	}
68868508Seric 	return svcno;
68968508Seric #endif
69068508Seric 
69168508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
69268508Seric 	/*
69368508Seric 	**  Fall-back mechanism.
69468508Seric 	*/
69568508Seric 
69669401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
69769401Seric 		mapreturn[svcno] = 0;
69869401Seric 
69968508Seric 	svcno = 0;
70068508Seric 	fp = fopen(ServiceSwitchFile, "r");
70168508Seric 	if (fp != NULL)
70268508Seric 	{
70368508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
70468508Seric 		{
70568508Seric 			register char *p;
70668508Seric 
70768508Seric 			p = strpbrk(buf, "#\n");
70868508Seric 			if (p != NULL)
70968508Seric 				*p = '\0';
71068508Seric 			p = strpbrk(buf, " \t");
71168508Seric 			if (p != NULL)
71268508Seric 				*p++ = '\0';
71368508Seric 			if (strcmp(buf, service) != 0)
71468508Seric 				continue;
71568508Seric 
71668508Seric 			/* got the right service -- extract data */
71768508Seric 			do
71868508Seric 			{
71968508Seric 				while (isspace(*p))
72068508Seric 					p++;
72168508Seric 				if (*p == '\0')
72268508Seric 					break;
72368508Seric 				maptype[svcno++] = p;
72468508Seric 				p = strpbrk(p, " \t");
72568508Seric 				if (p != NULL)
72668508Seric 					*p++ = '\0';
72768508Seric 			} while (p != NULL);
72868508Seric 			break;
72968508Seric 		}
73068508Seric 		fclose(fp);
73168508Seric 		return svcno;
73268508Seric 	}
73368508Seric #endif
73468508Seric 
73568508Seric 	/* if the service file doesn't work, use an absolute fallback */
73668508Seric   punt:
73769401Seric 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
73869401Seric 		mapreturn[svcno] = 0;
73969401Seric 	svcno = 0;
74068508Seric 	if (strcmp(service, "aliases") == 0)
74168508Seric 	{
74268628Seric 		maptype[svcno++] = "files";
74368628Seric #ifdef AUTO_NIS_ALIASES
74468628Seric # ifdef NISPLUS
74568628Seric 		maptype[svcno++] = "nisplus";
74668628Seric # endif
74768628Seric # ifdef NIS
74868628Seric 		maptype[svcno++] = "nis";
74968628Seric # endif
75068628Seric #endif
75168628Seric 		return svcno;
75268508Seric 	}
75368508Seric 	if (strcmp(service, "hosts") == 0)
75468508Seric 	{
75568508Seric # if NAMED_BIND
75668508Seric 		maptype[svcno++] = "dns";
75768508Seric # else
75868508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
75968508Seric 		/* SunOS */
76068508Seric 		maptype[svcno++] = "nis";
76168508Seric #  endif
76268508Seric # endif
76368508Seric 		maptype[svcno++] = "files";
76468508Seric 		return svcno;
76568508Seric 	}
76668508Seric 	return -1;
76768508Seric }
76868508Seric /*
7699369Seric **  USERNAME -- return the user id of the logged in user.
7709369Seric **
7719369Seric **	Parameters:
7729369Seric **		none.
7739369Seric **
7749369Seric **	Returns:
7759369Seric **		The login name of the logged in user.
7769369Seric **
7779369Seric **	Side Effects:
7789369Seric **		none.
7799369Seric **
7809369Seric **	Notes:
7819369Seric **		The return value is statically allocated.
7829369Seric */
7839369Seric 
7849369Seric char *
7859369Seric username()
7869369Seric {
78717469Seric 	static char *myname = NULL;
7889369Seric 	extern char *getlogin();
78919904Smiriam 	register struct passwd *pw;
7909369Seric 
79117469Seric 	/* cache the result */
79217469Seric 	if (myname == NULL)
79317469Seric 	{
79417469Seric 		myname = getlogin();
79517469Seric 		if (myname == NULL || myname[0] == '\0')
79617469Seric 		{
79768693Seric 			pw = sm_getpwuid(RealUid);
79817469Seric 			if (pw != NULL)
79940993Sbostic 				myname = newstr(pw->pw_name);
80017469Seric 		}
80119904Smiriam 		else
80219904Smiriam 		{
80363787Seric 			uid_t uid = RealUid;
80419873Smiriam 
80540993Sbostic 			myname = newstr(myname);
80668693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
80758736Seric 			      (uid != 0 && uid != pw->pw_uid))
80819904Smiriam 			{
80968693Seric 				pw = sm_getpwuid(uid);
81024945Seric 				if (pw != NULL)
81140993Sbostic 					myname = newstr(pw->pw_name);
81219873Smiriam 			}
81319873Smiriam 		}
81417469Seric 		if (myname == NULL || myname[0] == '\0')
81517469Seric 		{
81658151Seric 			syserr("554 Who are you?");
81717469Seric 			myname = "postmaster";
81817469Seric 		}
81917469Seric 	}
82017469Seric 
82117469Seric 	return (myname);
8229369Seric }
8239369Seric /*
8244190Seric **  TTYPATH -- Get the path of the user's tty
825294Seric **
826294Seric **	Returns the pathname of the user's tty.  Returns NULL if
827294Seric **	the user is not logged in or if s/he has write permission
828294Seric **	denied.
829294Seric **
830294Seric **	Parameters:
831294Seric **		none
832294Seric **
833294Seric **	Returns:
834294Seric **		pathname of the user's tty.
835294Seric **		NULL if not logged in or write permission denied.
836294Seric **
837294Seric **	Side Effects:
838294Seric **		none.
839294Seric **
840294Seric **	WARNING:
841294Seric **		Return value is in a local buffer.
842294Seric **
843294Seric **	Called By:
844294Seric **		savemail
845294Seric */
846294Seric 
847294Seric char *
848294Seric ttypath()
849294Seric {
850294Seric 	struct stat stbuf;
851294Seric 	register char *pathn;
852294Seric 	extern char *ttyname();
8534081Seric 	extern char *getlogin();
854294Seric 
855294Seric 	/* compute the pathname of the controlling tty */
8569369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8579369Seric 	    (pathn = ttyname(0)) == NULL)
858294Seric 	{
859294Seric 		errno = 0;
860294Seric 		return (NULL);
861294Seric 	}
862294Seric 
863294Seric 	/* see if we have write permission */
8642967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
865294Seric 	{
866294Seric 		errno = 0;
867294Seric 		return (NULL);
868294Seric 	}
869294Seric 
870294Seric 	/* see if the user is logged in */
871294Seric 	if (getlogin() == NULL)
872294Seric 		return (NULL);
873294Seric 
874294Seric 	/* looks good */
875294Seric 	return (pathn);
876294Seric }
8772967Seric /*
8782967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8792967Seric **
8802967Seric **	This routine can be supplied on a per-installation basis
8812967Seric **	to determine whether a person is allowed to send a message.
8822967Seric **	This allows restriction of certain types of internet
8832967Seric **	forwarding or registration of users.
8842967Seric **
8852967Seric **	If the hosts are found to be incompatible, an error
88668857Seric **	message should be given using "usrerr" and an EX_ code
88768857Seric **	should be returned.  You can also set to->q_status to
88868857Seric **	a DSN-style status code.
8892967Seric **
89068559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
89168559Seric **	body during the return-to-sender function; this should be done
89268559Seric **	on huge messages.  This bit may already be set by the ESMTP
89368559Seric **	protocol.
8944288Seric **
8952967Seric **	Parameters:
8962967Seric **		to -- the person being sent to.
8972967Seric **
8982967Seric **	Returns:
89957459Seric **		an exit status
9002967Seric **
9012967Seric **	Side Effects:
9022967Seric **		none (unless you include the usrerr stuff)
9032967Seric */
9042967Seric 
90568508Seric int
90655012Seric checkcompat(to, e)
9072967Seric 	register ADDRESS *to;
90855012Seric 	register ENVELOPE *e;
9092967Seric {
91012133Seric # ifdef lint
91112133Seric 	if (to == NULL)
91212133Seric 		to++;
91363834Seric # endif /* lint */
91464402Seric 
91564402Seric 	if (tTd(49, 1))
91664402Seric 		printf("checkcompat(to=%s, from=%s)\n",
91764402Seric 			to->q_paddr, e->e_from.q_paddr);
91864402Seric 
91910698Seric # ifdef EXAMPLE_CODE
92010698Seric 	/* this code is intended as an example only */
9214437Seric 	register STAB *s;
9224437Seric 
9234437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
92468508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9259369Seric 	    to->q_mailer == s->s_mailer)
9264437Seric 	{
92758151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
92868559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
92968857Seric 		to->q_status = "5.7.1";
93057459Seric 		return (EX_UNAVAILABLE);
9314437Seric 	}
93256795Seric # endif /* EXAMPLE_CODE */
93357459Seric 	return (EX_OK);
9342967Seric }
9359369Seric /*
93664035Seric **  SETSIGNAL -- set a signal handler
93764035Seric **
93864035Seric **	This is essentially old BSD "signal(3)".
93964035Seric */
94064035Seric 
94164561Seric sigfunc_t
94264035Seric setsignal(sig, handler)
94364035Seric 	int sig;
94464561Seric 	sigfunc_t handler;
94564035Seric {
94664380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
94764035Seric 	return signal(sig, handler);
94864035Seric #else
94964035Seric 	struct sigaction n, o;
95064035Seric 
95164035Seric 	bzero(&n, sizeof n);
95264035Seric 	n.sa_handler = handler;
95368508Seric # ifdef SA_RESTART
95468508Seric 	n.sa_flags = SA_RESTART;
95568508Seric # endif
95664035Seric 	if (sigaction(sig, &n, &o) < 0)
95764035Seric 		return SIG_ERR;
95864035Seric 	return o.sa_handler;
95964035Seric #endif
96064035Seric }
96164035Seric /*
9629369Seric **  HOLDSIGS -- arrange to hold all signals
9639369Seric **
9649369Seric **	Parameters:
9659369Seric **		none.
9669369Seric **
9679369Seric **	Returns:
9689369Seric **		none.
9699369Seric **
9709369Seric **	Side Effects:
9719369Seric **		Arranges that signals are held.
9729369Seric */
9739369Seric 
97468508Seric void
9759369Seric holdsigs()
9769369Seric {
9779369Seric }
9789369Seric /*
9799369Seric **  RLSESIGS -- arrange to release all signals
9809369Seric **
9819369Seric **	This undoes the effect of holdsigs.
9829369Seric **
9839369Seric **	Parameters:
9849369Seric **		none.
9859369Seric **
9869369Seric **	Returns:
9879369Seric **		none.
9889369Seric **
9899369Seric **	Side Effects:
9909369Seric **		Arranges that signals are released.
9919369Seric */
9929369Seric 
99368508Seric void
9949369Seric rlsesigs()
9959369Seric {
9969369Seric }
99714872Seric /*
99864705Seric **  INIT_MD -- do machine dependent initializations
99964705Seric **
100064705Seric **	Systems that have global modes that should be set should do
100164705Seric **	them here rather than in main.
100264705Seric */
100364705Seric 
100464705Seric #ifdef _AUX_SOURCE
100564705Seric # include	<compat.h>
100664705Seric #endif
100764705Seric 
100868508Seric void
100964825Seric init_md(argc, argv)
101064825Seric 	int argc;
101164825Seric 	char **argv;
101264705Seric {
101364705Seric #ifdef _AUX_SOURCE
101464705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
101564705Seric #endif
101668508Seric 
101768508Seric #ifdef VENDOR_DEFAULT
101868508Seric 	VendorCode = VENDOR_DEFAULT;
101968508Seric #else
102068508Seric 	VendorCode = VENDOR_BERKELEY;
102168508Seric #endif
102264705Seric }
102364705Seric /*
102468508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
102568508Seric **
102668508Seric **	Called once, on startup.
102768508Seric **
102868508Seric **	Parameters:
102968508Seric **		e -- the global envelope.
103068508Seric **
103168508Seric **	Returns:
103268508Seric **		none.
103368508Seric **
103468508Seric **	Side Effects:
103568508Seric **		vendor-dependent.
103668508Seric */
103768508Seric 
103868508Seric void
103968508Seric init_vendor_macros(e)
104068508Seric 	register ENVELOPE *e;
104168508Seric {
104268508Seric }
104368508Seric /*
104414872Seric **  GETLA -- get the current load average
104514872Seric **
104614881Seric **	This code stolen from la.c.
104714881Seric **
104814872Seric **	Parameters:
104914872Seric **		none.
105014872Seric **
105114872Seric **	Returns:
105214872Seric **		The current load average as an integer.
105314872Seric **
105414872Seric **	Side Effects:
105514872Seric **		none.
105614872Seric */
105714872Seric 
105851920Seric /* try to guess what style of load average we have */
105951920Seric #define LA_ZERO		1	/* always return load average as zero */
106064360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
106151920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
106251920Seric #define LA_SUBR		4	/* call getloadavg */
106364295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
106464360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
106566301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
106669462Seric #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
106769462Seric #define LA_DGUX		9	/* special DGUX implementation */
106869462Seric #define LA_HPUX		10	/* special HPUX implementation */
106914872Seric 
107063962Seric /* do guesses based on general OS type */
107151920Seric #ifndef LA_TYPE
107263962Seric # define LA_TYPE	LA_ZERO
107351920Seric #endif
107451920Seric 
107569462Seric #ifndef FSHIFT
107669462Seric # if defined(unixpc)
107769462Seric #  define FSHIFT	5
107869462Seric # endif
107969462Seric 
108069462Seric # if defined(__alpha) || defined(IRIX)
108169462Seric #  define FSHIFT	10
108269462Seric # endif
108369462Seric 
108469462Seric # if defined(_AIX3)
108569462Seric #  define FSHIFT	16
108669462Seric # endif
108769462Seric #endif
108869462Seric 
108969462Seric #ifndef FSHIFT
109069462Seric # define FSHIFT		8
109169462Seric #endif
109269462Seric 
109369462Seric #ifndef FSCALE
109469462Seric # define FSCALE		(1 << FSHIFT)
109569462Seric #endif
109669462Seric 
109769654Seric #ifndef LA_AVENRUN
109869654Seric # ifdef SYSTEM5
109969654Seric #  define LA_AVENRUN	"avenrun"
110069654Seric # else
110169654Seric #  define LA_AVENRUN	"_avenrun"
110269654Seric # endif
110369654Seric #endif
110469654Seric 
110564360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
110651920Seric 
110714872Seric #include <nlist.h>
110814872Seric 
110968508Seric #ifdef IRIX64
111068508Seric # define nlist		nlist64
111168508Seric #endif
111268508Seric 
111351920Seric /* _PATH_UNIX should be defined in <paths.h> */
111451920Seric #ifndef _PATH_UNIX
111563962Seric # if defined(SYSTEM5)
111663962Seric #  define _PATH_UNIX	"/unix"
111763962Seric # else
111863962Seric #  define _PATH_UNIX	"/vmunix"
111963962Seric # endif
112051920Seric #endif
112151920Seric 
112269462Seric struct nlist	Nl[] =
112314872Seric {
112451920Seric 	{ LA_AVENRUN },
112514872Seric #define	X_AVENRUN	0
112614872Seric 	{ 0 },
112714872Seric };
112814872Seric 
112914872Seric getla()
113014872Seric {
113114872Seric 	static int kmem = -1;
113251920Seric #if LA_TYPE == LA_INT
113324943Seric 	long avenrun[3];
113451920Seric #else
113564360Seric # if LA_TYPE == LA_SHORT
113664360Seric 	short avenrun[3];
113764360Seric # else
113851920Seric 	double avenrun[3];
113964360Seric # endif
114051920Seric #endif
114169462Seric 	extern int errno;
114225615Seric 	extern off_t lseek();
114314872Seric 
114414872Seric 	if (kmem < 0)
114514872Seric 	{
114624945Seric 		kmem = open("/dev/kmem", 0, 0);
114714872Seric 		if (kmem < 0)
114857736Seric 		{
114957736Seric 			if (tTd(3, 1))
115057736Seric 				printf("getla: open(/dev/kmem): %s\n",
115157736Seric 					errstring(errno));
115214872Seric 			return (-1);
115357736Seric 		}
115451920Seric 		(void) fcntl(kmem, F_SETFD, 1);
115569462Seric 
115668508Seric #ifdef _AIX3
115768508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
115868508Seric #else
115957736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
116068508Seric #endif
116157736Seric 		{
116257736Seric 			if (tTd(3, 1))
116357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
116457736Seric 					errstring(errno));
116514872Seric 			return (-1);
116657736Seric 		}
116759253Seric 		if (Nl[X_AVENRUN].n_value == 0)
116859253Seric 		{
116959253Seric 			if (tTd(3, 1))
117059253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
117159253Seric 					_PATH_UNIX, LA_AVENRUN);
117259253Seric 			return (-1);
117359253Seric 		}
117468508Seric #ifdef NAMELISTMASK
117568508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
117666763Seric #endif
117714872Seric 	}
117857736Seric 	if (tTd(3, 20))
117957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
118068508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
118123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
118219967Seric 	{
118319967Seric 		/* thank you Ian */
118457736Seric 		if (tTd(3, 1))
118557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
118619967Seric 		return (-1);
118719967Seric 	}
118869462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
118957736Seric 	if (tTd(3, 5))
119057736Seric 	{
119157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
119257736Seric 		if (tTd(3, 15))
119357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
119457736Seric 		printf("\n");
119557736Seric 	}
119657736Seric 	if (tTd(3, 1))
119757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
119969462Seric # else /* LA_TYPE == LA_FLOAT */
120057736Seric 	if (tTd(3, 5))
120157736Seric 	{
120257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
120357736Seric 		if (tTd(3, 15))
120457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
120557736Seric 		printf("\n");
120657736Seric 	}
120757736Seric 	if (tTd(3, 1))
120857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
120951920Seric 	return ((int) (avenrun[0] + 0.5));
121069462Seric # endif
121114872Seric }
121214872Seric 
121369462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */
121451773Seric 
121569462Seric #if LA_TYPE == LA_READKSYM
121664718Seric 
121769473Seric # include <sys/ksym.h>
121869473Seric 
121969462Seric getla()
122069462Seric {
122169462Seric 	static int kmem = -1;
122269462Seric 	long avenrun[3];
122369462Seric 	extern int errno;
122469462Seric 	struct mioc_rksym mirk;
122564718Seric 
122669462Seric 	if (kmem < 0)
122769462Seric 	{
122869462Seric 		kmem = open("/dev/kmem", 0, 0);
122969462Seric 		if (kmem < 0)
123069462Seric 		{
123169462Seric 			if (tTd(3, 1))
123269462Seric 				printf("getla: open(/dev/kmem): %s\n",
123369462Seric 					errstring(errno));
123469462Seric 			return (-1);
123569462Seric 		}
123669462Seric 		(void) fcntl(kmem, F_SETFD, 1);
123769462Seric 	}
123869462Seric 	mirk.mirk_symname = LA_AVENRUN;
123969462Seric 	mirk.mirk_buf = avenrun;
124069462Seric 	mirk.mirk_buflen = sizeof(avenrun);
124169462Seric 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
124269462Seric 	{
124369462Seric 		if (tTd(3, 1))
124469462Seric 			printf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
124569462Seric 				errstring(errno));
124669462Seric 		return -1;
124769462Seric 	}
124869462Seric 	if (tTd(3, 5))
124969462Seric 	{
125069462Seric 		printf("getla: avenrun = %d", avenrun[0]);
125169462Seric 		if (tTd(3, 15))
125269462Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
125369462Seric 		printf("\n");
125469462Seric 	}
125569462Seric 	if (tTd(3, 1))
125669462Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125769462Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
125869462Seric }
125969462Seric 
126069462Seric #endif /* LA_TYPE == LA_READKSYM */
126169462Seric 
126269462Seric #if LA_TYPE == LA_DGUX
126369462Seric 
126469462Seric # include <sys/dg_sys_info.h>
126569462Seric 
126668508Seric int
126768508Seric getla()
126864718Seric {
126964718Seric 	struct dg_sys_info_load_info load_info;
127064718Seric 
127164718Seric 	dg_sys_info((long *)&load_info,
127264718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
127364718Seric 
127468508Seric         if (tTd(3, 1))
127568508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
127668508Seric 
127764718Seric 	return((int) (load_info.one_minute + 0.5));
127864718Seric }
127964718Seric 
128069462Seric #endif /* LA_TYPE == LA_DGUX */
128164718Seric 
128269462Seric #if LA_TYPE == LA_HPUX
128369462Seric 
128469563Seric /* forward declarations to keep gcc from complaining */
128568869Seric struct pst_dynamic;
128669563Seric struct pst_status;
128769563Seric struct pst_static;
128869563Seric struct pst_vminfo;
128969563Seric struct pst_diskinfo;
129069563Seric struct pst_processor;
129169563Seric struct pst_lv;
129269563Seric struct pst_swapinfo;
129368869Seric 
129469462Seric # include <sys/param.h>
129569462Seric # include <sys/pstat.h>
129668508Seric 
129768508Seric int
129851773Seric getla()
129951773Seric {
130068508Seric 	struct pst_dynamic pstd;
130168508Seric 
130268508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
130368869Seric 			     (size_t) 1, 0) == -1)
130468508Seric 		return 0;
130568508Seric 
130668508Seric         if (tTd(3, 1))
130768508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
130868508Seric 
130968508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
131068508Seric }
131168508Seric 
131269462Seric #endif /* LA_TYPE == LA_HPUX */
131368508Seric 
131469462Seric #if LA_TYPE == LA_SUBR
131569462Seric 
131668508Seric int
131768508Seric getla()
131868508Seric {
131951920Seric 	double avenrun[3];
132051920Seric 
132151920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
132257736Seric 	{
132357736Seric 		if (tTd(3, 1))
132457736Seric 			perror("getla: getloadavg failed:");
132551920Seric 		return (-1);
132657736Seric 	}
132757736Seric 	if (tTd(3, 1))
132857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
132951920Seric 	return ((int) (avenrun[0] + 0.5));
133051773Seric }
133151773Seric 
133269462Seric #endif /* LA_TYPE == LA_SUBR */
133369462Seric 
133464295Seric #if LA_TYPE == LA_MACH
133551773Seric 
133664295Seric /*
133765173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
133864295Seric */
133964295Seric 
134065173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
134165173Seric # include <mach/mach.h>
134265173Seric #else
134365173Seric # include <mach.h>
134465173Seric #endif
134564295Seric 
134651773Seric getla()
134751773Seric {
134864295Seric 	processor_set_t default_set;
134964295Seric 	kern_return_t error;
135064295Seric 	unsigned int info_count;
135164295Seric 	struct processor_set_basic_info info;
135264295Seric 	host_t host;
135364295Seric 
135464295Seric 	error = processor_set_default(host_self(), &default_set);
135564295Seric 	if (error != KERN_SUCCESS)
135664295Seric 		return -1;
135764295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
135864295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
135964295Seric 			       &host, (processor_set_info_t)&info,
136064295Seric 			       &info_count) != KERN_SUCCESS)
136164295Seric 	{
136264295Seric 		return -1;
136364295Seric 	}
136464295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
136564295Seric }
136664295Seric 
136769462Seric #endif /* LA_TYPE == LA_MACH */
136864295Seric 
136966313Seric #if LA_TYPE == LA_PROCSTR
137064295Seric 
137166301Seric /*
137266301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
137366301Seric **  in a format like "0.15 0.12 0.06".
137466301Seric **
137566301Seric **	Initially intended for Linux.  This has been in the kernel
137666301Seric **	since at least 0.99.15.
137766301Seric */
137866301Seric 
137966301Seric # ifndef _PATH_LOADAVG
138066301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
138166301Seric # endif
138266301Seric 
138366301Seric int
138464295Seric getla()
138564295Seric {
138666319Seric 	double avenrun;
138766301Seric 	register int result;
138866319Seric 	FILE *fp;
138966301Seric 
139066319Seric 	fp = fopen(_PATH_LOADAVG, "r");
139169472Seric 	if (fp == NULL)
139266301Seric 	{
139366319Seric 		if (tTd(3, 1))
139466319Seric 			printf("getla: fopen(%s): %s\n",
139566319Seric 				_PATH_LOADAVG, errstring(errno));
139666319Seric 		return -1;
139766301Seric 	}
139866301Seric 	result = fscanf(fp, "%lf", &avenrun);
139966319Seric 	fclose(fp);
140066301Seric 	if (result != 1)
140166301Seric 	{
140266310Seric 		if (tTd(3, 1))
140366301Seric 			printf("getla: fscanf() = %d: %s\n",
140466301Seric 				result, errstring(errno));
140566301Seric 		return -1;
140666301Seric 	}
140766301Seric 
140857736Seric 	if (tTd(3, 1))
140966301Seric 		printf("getla(): %.2f\n", avenrun);
141066301Seric 
141166301Seric 	return ((int) (avenrun + 0.5));
141266301Seric }
141366301Seric 
141469462Seric #endif /* LA_TYPE == LA_PROCSTR */
141566301Seric 
141669462Seric #if LA_TYPE == LA_ZERO
141769462Seric 
141866301Seric getla()
141966301Seric {
142066301Seric 	if (tTd(3, 1))
142157736Seric 		printf("getla: ZERO\n");
142251920Seric 	return (0);
142351773Seric }
142451773Seric 
142569462Seric #endif /* LA_TYPE == LA_ZERO */
142666045Seric 
142766045Seric 
142866045Seric /*
142966045Seric  * Copyright 1989 Massachusetts Institute of Technology
143066045Seric  *
143166045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
143266045Seric  * documentation for any purpose is hereby granted without fee, provided that
143366045Seric  * the above copyright notice appear in all copies and that both that
143466045Seric  * copyright notice and this permission notice appear in supporting
143566045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
143666045Seric  * publicity pertaining to distribution of the software without specific,
143766045Seric  * written prior permission.  M.I.T. makes no representations about the
143866045Seric  * suitability of this software for any purpose.  It is provided "as is"
143966045Seric  * without express or implied warranty.
144066045Seric  *
144166045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
144266045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
144366045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144466045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
144566045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
144666045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
144766045Seric  *
144866045Seric  * Authors:  Many and varied...
144966045Seric  */
145066045Seric 
145166045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
145266045Seric #ifndef lint
145366045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
145466045Seric #endif /* !lint */
145566045Seric 
145666179Seric #ifdef apollo
145766045Seric # undef volatile
145866045Seric #    include <apollo/base.h>
145966045Seric 
146066045Seric /* ARGSUSED */
146166045Seric int getloadavg( call_data )
146266045Seric      caddr_t	call_data;	/* pointer to (double) return value */
146366045Seric {
146466045Seric      double *avenrun = (double *) call_data;
146566045Seric      int i;
146666045Seric      status_$t      st;
146766045Seric      long loadav[3];
146866045Seric      proc1_$get_loadav(loadav, &st);
146966045Seric      *avenrun = loadav[0] / (double) (1 << 16);
147066045Seric      return(0);
147166045Seric }
147266045Seric #   endif /* apollo */
147324943Seric /*
147424943Seric **  SHOULDQUEUE -- should this message be queued or sent?
147524943Seric **
147624943Seric **	Compares the message cost to the load average to decide.
147724943Seric **
147824943Seric **	Parameters:
147924943Seric **		pri -- the priority of the message in question.
148057438Seric **		ctime -- the message creation time.
148124943Seric **
148224943Seric **	Returns:
148324943Seric **		TRUE -- if this message should be queued up for the
148424943Seric **			time being.
148524943Seric **		FALSE -- if the load is low enough to send this message.
148624943Seric **
148724943Seric **	Side Effects:
148824943Seric **		none.
148924943Seric */
149024943Seric 
149124943Seric bool
149257438Seric shouldqueue(pri, ctime)
149324943Seric 	long pri;
149457438Seric 	time_t ctime;
149524943Seric {
149668508Seric 	bool rval;
149768508Seric 
149868508Seric 	if (tTd(3, 30))
149968508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
150051920Seric 	if (CurrentLA < QueueLA)
150168508Seric 	{
150268508Seric 		if (tTd(3, 30))
150368508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
150424943Seric 		return (FALSE);
150568508Seric 	}
150658132Seric 	if (CurrentLA >= RefuseLA)
150768508Seric 	{
150868508Seric 		if (tTd(3, 30))
150968508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
151058132Seric 		return (TRUE);
151168508Seric 	}
151268508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
151368508Seric 	if (tTd(3, 30))
151468508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
151568508Seric 	return rval;
151624943Seric }
151724943Seric /*
151853037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
151953037Seric **
152053037Seric **	Parameters:
152153037Seric **		none.
152253037Seric **
152353037Seric **	Returns:
152453037Seric **		TRUE if incoming SMTP connections should be refused
152553037Seric **			(for now).
152653037Seric **		FALSE if we should accept new work.
152753037Seric **
152853037Seric **	Side Effects:
152953037Seric **		none.
153053037Seric */
153153037Seric 
153253037Seric bool
153353037Seric refuseconnections()
153453037Seric {
1535*69801Seric 	extern bool enoughdiskspace();
153668508Seric 
153759156Seric #ifdef XLA
153859156Seric 	if (!xla_smtp_ok())
153959156Seric 		return TRUE;
154059156Seric #endif
154159156Seric 
154253037Seric 	/* this is probably too simplistic */
1543*69801Seric 	return CurrentLA >= RefuseLA || !enoughdiskspace(MinBlocksFree + 1);
154453037Seric }
154553037Seric /*
154624943Seric **  SETPROCTITLE -- set process title for ps
154724943Seric **
154824943Seric **	Parameters:
154958674Seric **		fmt -- a printf style format string.
155058674Seric **		a, b, c -- possible parameters to fmt.
155124943Seric **
155224943Seric **	Returns:
155324943Seric **		none.
155424943Seric **
155524943Seric **	Side Effects:
155624943Seric **		Clobbers argv of our main procedure so ps(1) will
155724943Seric **		display the title.
155824943Seric */
155924943Seric 
156068508Seric #define SPT_NONE	0	/* don't use it at all */
156168508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
156268508Seric #define SPT_BUILTIN	2	/* use libc builtin */
156368508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
156468508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
156569654Seric #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
156668508Seric 
156768508Seric #ifndef SPT_TYPE
156868508Seric # define SPT_TYPE	SPT_REUSEARGV
156968508Seric #endif
157068508Seric 
157168508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
157268508Seric 
157368508Seric # if SPT_TYPE == SPT_PSTAT
157458689Seric #  include <sys/pstat.h>
157558689Seric # endif
157668508Seric # if SPT_TYPE == SPT_PSSTRINGS
157759732Seric #  include <machine/vmparam.h>
157859732Seric #  include <sys/exec.h>
157968508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
158068508Seric #   undef SPT_TYPE
158168508Seric #   define SPT_TYPE	SPT_REUSEARGV
158268734Seric #  else
158368734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
158468734Seric #    define NKPDE 63
158568734Seric typedef unsigned int	*pt_entry_t;
158668734Seric #   endif
158764247Seric #  endif
158859732Seric # endif
158968508Seric 
159068508Seric # if SPT_TYPE == SPT_PSSTRINGS
159168508Seric #  define SETPROC_STATIC	static
159268508Seric # else
159359732Seric #  define SETPROC_STATIC
159459732Seric # endif
159567626Seric 
159669654Seric # if SPT_TYPE == SPT_SYSMIPS
159769654Seric #  include <sys/sysmips.h>
159869654Seric #  include <sys/sysnews.h>
159969654Seric # endif
160069654Seric 
160168508Seric # ifndef SPT_PADCHAR
160268508Seric #  define SPT_PADCHAR	' '
160368508Seric # endif
160458689Seric 
160568508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
160664843Seric 
160768508Seric #if SPT_TYPE != SPT_BUILTIN
160868508Seric 
160924943Seric /*VARARGS1*/
161068508Seric void
161168508Seric # ifdef __STDC__
161257642Seric setproctitle(char *fmt, ...)
161368508Seric # else
161457642Seric setproctitle(fmt, va_alist)
161524943Seric 	char *fmt;
161657642Seric 	va_dcl
161768508Seric # endif
161824943Seric {
161968508Seric # if SPT_TYPE != SPT_NONE
162024943Seric 	register char *p;
162125049Seric 	register int i;
162259732Seric 	SETPROC_STATIC char buf[MAXLINE];
162356852Seric 	VA_LOCAL_DECL
162468508Seric #  if SPT_TYPE == SPT_PSTAT
162558689Seric 	union pstun pst;
162658689Seric #  endif
162769748Seric #  if SPT_TYPE == SPT_REUSEARGV
162824943Seric 	extern char **Argv;
162924943Seric 	extern char *LastArgv;
163069748Seric #  endif
163124943Seric 
163258674Seric 	p = buf;
163324943Seric 
163458674Seric 	/* print sendmail: heading for grep */
163558674Seric 	(void) strcpy(p, "sendmail: ");
163658674Seric 	p += strlen(p);
163724943Seric 
163858674Seric 	/* print the argument string */
163958674Seric 	VA_START(fmt);
164058674Seric 	(void) vsprintf(p, fmt, ap);
164156852Seric 	VA_END;
164254996Seric 
164358674Seric 	i = strlen(buf);
164458689Seric 
164568508Seric #  if SPT_TYPE == SPT_PSTAT
164658689Seric 	pst.pst_command = buf;
164758689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
164869654Seric #  endif
164969654Seric #  if SPT_TYPE == SPT_PSSTRINGS
165059732Seric 	PS_STRINGS->ps_nargvstr = 1;
165159732Seric 	PS_STRINGS->ps_argvstr = buf;
165269654Seric #  endif
165369654Seric #  if SPT_TYPE == SPT_SYSMIPS
165469654Seric 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
165569654Seric #  endif
165669654Seric #  if SPT_TYPE == SPT_REUSEARGV
165754996Seric 	if (i > LastArgv - Argv[0] - 2)
165825049Seric 	{
165954996Seric 		i = LastArgv - Argv[0] - 2;
166058674Seric 		buf[i] = '\0';
166125049Seric 	}
166258674Seric 	(void) strcpy(Argv[0], buf);
166354997Seric 	p = &Argv[0][i];
166424943Seric 	while (p < LastArgv)
166568508Seric 		*p++ = SPT_PADCHAR;
166668508Seric 	Argv[1] = NULL;
166769654Seric #  endif
166868508Seric # endif /* SPT_TYPE != SPT_NONE */
166924943Seric }
167065982Seric 
167168508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
167225698Seric /*
167325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
167425698Seric **
167525698Seric **	Parameters:
167668508Seric **		sig -- the signal that got us here (unused).
167725698Seric **
167825698Seric **	Returns:
167925698Seric **		none.
168025698Seric **
168125698Seric **	Side Effects:
168225698Seric **		Picks up extant zombies.
168325698Seric */
168425698Seric 
168546928Sbostic void
168668508Seric reapchild(sig)
168768508Seric 	int sig;
168825698Seric {
168964738Seric 	int olderrno = errno;
169063966Seric # ifdef HASWAITPID
169160219Seric 	auto int status;
169260560Seric 	int count;
169360560Seric 	int pid;
169460219Seric 
169560560Seric 	count = 0;
169660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
169760560Seric 	{
169860560Seric 		if (count++ > 1000)
169960560Seric 		{
170066748Seric #ifdef LOG
170160560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
170260560Seric 				pid, status);
170366748Seric #endif
170460560Seric 			break;
170560560Seric 		}
170660560Seric 	}
170760219Seric # else
170825698Seric # ifdef WNOHANG
170925698Seric 	union wait status;
171025698Seric 
171163753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
171225698Seric 		continue;
171356795Seric # else /* WNOHANG */
171425698Seric 	auto int status;
171525698Seric 
171660219Seric 	while (wait(&status) > 0)
171725698Seric 		continue;
171856795Seric # endif /* WNOHANG */
171960219Seric # endif
172064035Seric # ifdef SYS5SIGNALS
172164035Seric 	(void) setsignal(SIGCHLD, reapchild);
172258061Seric # endif
172364738Seric 	errno = olderrno;
172425698Seric }
172555418Seric /*
172669654Seric **  PUTENV -- emulation of putenv() in terms of setenv()
172769654Seric **
172869654Seric **	Not needed on Posix-compliant systems.
172969654Seric **	This doesn't have full Posix semantics, but it's good enough
173069654Seric **		for sendmail.
173169654Seric **
173269654Seric **	Parameter:
173369654Seric **		env -- the environment to put.
173469654Seric **
173569654Seric **	Returns:
173669654Seric **		none.
173769654Seric */
173869654Seric 
173969654Seric #ifdef NEEDPUTENV
174069654Seric 
174169717Seric # if NEEDPUTENV == 2		/* no setenv(3) call available */
174269717Seric 
174369717Seric int
174469717Seric putenv(str)
174569717Seric 	char *str;
174669717Seric {
174769717Seric 	char **current;
174869717Seric 	int matchlen, envlen=0;
174969717Seric 	char *tmp;
175069717Seric 	char **newenv;
175169717Seric 	static int first=1;
175269717Seric 	extern char **environ;
175369717Seric 
175469717Seric 	/*
175569717Seric 	 * find out how much of str to match when searching
175669717Seric 	 * for a string to replace.
175769717Seric 	 */
175869717Seric 	if ((tmp = index(str, '=')) == NULL || tmp == str)
175969717Seric 		matchlen = strlen(str);
176069717Seric 	else
176169717Seric 		matchlen = (int) (tmp - str);
176269717Seric 	++matchlen;
176369717Seric 
176469717Seric 	/*
176569717Seric 	 * Search for an existing string in the environment and find the
176669717Seric 	 * length of environ.  If found, replace and exit.
176769717Seric 	 */
176869717Seric 	for (current=environ; *current; current++) {
176969717Seric 		++envlen;
177069717Seric 
177169717Seric 		if (strncmp(str, *current, matchlen) == 0) {
177269717Seric 			/* found it, now insert the new version */
177369717Seric 			*current = (char *)str;
177469717Seric 			return(0);
177569717Seric 		}
177669717Seric 	}
177769717Seric 
177869717Seric 	/*
177969717Seric 	 * There wasn't already a slot so add space for a new slot.
178069717Seric 	 * If this is our first time through, use malloc(), else realloc().
178169717Seric 	 */
178269717Seric 	if (first) {
178369717Seric 		newenv = (char **) malloc(sizeof(char *) * (envlen + 2));
178469717Seric 		if (newenv == NULL)
178569717Seric 			return(-1);
178669717Seric 
178769717Seric 		first=0;
178869717Seric 		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
178969717Seric 	} else {
179069717Seric 		newenv = (char **) realloc((char *)environ, sizeof(char *) * (envlen + 2));
179169717Seric 		if (newenv == NULL)
179269717Seric 			return(-1);
179369717Seric 	}
179469717Seric 
179569717Seric 	/* actually add in the new entry */
179669717Seric 	environ = newenv;
179769717Seric 	environ[envlen] = (char *)str;
179869717Seric 	environ[envlen+1] = NULL;
179969717Seric 
180069717Seric 	return(0);
180169717Seric }
180269717Seric 
180369717Seric #else			/* implement putenv() in terms of setenv() */
180469717Seric 
180569717Seric int
180669654Seric putenv(env)
180769654Seric 	char *env;
180869654Seric {
180969654Seric 	char *p;
181069654Seric 	int l;
181169654Seric 	char nbuf[100];
181269654Seric 
181369654Seric 	p = strchr(env, '=');
181469654Seric 	if (p == NULL)
181569717Seric 		return 0;
181669654Seric 	l = p - env;
181769654Seric 	if (l > sizeof nbuf - 1)
181869654Seric 		l = sizeof nbuf - 1;
181969654Seric 	bcopy(env, nbuf, l);
182069654Seric 	nbuf[l] = '\0';
182169717Seric 	return setenv(nbuf, ++p, 1);
182269654Seric }
182369654Seric 
182469717Seric # endif
182569654Seric #endif
182669654Seric /*
182755418Seric **  UNSETENV -- remove a variable from the environment
182855418Seric **
182955418Seric **	Not needed on newer systems.
183055418Seric **
183155418Seric **	Parameters:
183255418Seric **		name -- the string name of the environment variable to be
183355418Seric **			deleted from the current environment.
183455418Seric **
183555418Seric **	Returns:
183655418Seric **		none.
183755418Seric **
183855418Seric **	Globals:
183955418Seric **		environ -- a pointer to the current environment.
184055418Seric **
184155418Seric **	Side Effects:
184255418Seric **		Modifies environ.
184355418Seric */
184455418Seric 
184563962Seric #ifndef HASUNSETENV
184655418Seric 
184755418Seric void
184855418Seric unsetenv(name)
184955418Seric 	char *name;
185055418Seric {
185155418Seric 	extern char **environ;
185255418Seric 	register char **pp;
185355418Seric 	int len = strlen(name);
185455418Seric 
185555418Seric 	for (pp = environ; *pp != NULL; pp++)
185655418Seric 	{
185755418Seric 		if (strncmp(name, *pp, len) == 0 &&
185855418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
185955418Seric 			break;
186055418Seric 	}
186155418Seric 
186255418Seric 	for (; *pp != NULL; pp++)
186355418Seric 		*pp = pp[1];
186455418Seric }
186555418Seric 
186663962Seric #endif
186756215Seric /*
186856215Seric **  GETDTABLESIZE -- return number of file descriptors
186956215Seric **
187056215Seric **	Only on non-BSD systems
187156215Seric **
187256215Seric **	Parameters:
187356215Seric **		none
187456215Seric **
187556215Seric **	Returns:
187656215Seric **		size of file descriptor table
187756215Seric **
187856215Seric **	Side Effects:
187956215Seric **		none
188056215Seric */
188156215Seric 
188263787Seric #ifdef SOLARIS
188363787Seric # include <sys/resource.h>
188463787Seric #endif
188556215Seric 
188656215Seric int
188763787Seric getdtsize()
188856215Seric {
188963787Seric #ifdef RLIMIT_NOFILE
189063787Seric 	struct rlimit rl;
189163787Seric 
189263787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
189363787Seric 		return rl.rlim_cur;
189463787Seric #endif
189563787Seric 
189664031Seric # ifdef HASGETDTABLESIZE
189764031Seric 	return getdtablesize();
189864031Seric # else
189964031Seric #  ifdef _SC_OPEN_MAX
190058689Seric 	return sysconf(_SC_OPEN_MAX);
190163787Seric #  else
190256215Seric 	return NOFILE;
190363787Seric #  endif
190458689Seric # endif
190556215Seric }
190657631Seric /*
190757631Seric **  UNAME -- get the UUCP name of this system.
190857631Seric */
190957631Seric 
191057943Seric #ifndef HASUNAME
191157631Seric 
191257631Seric int
191357631Seric uname(name)
191457631Seric 	struct utsname *name;
191557631Seric {
191657631Seric 	FILE *file;
191757631Seric 	char *n;
191857631Seric 
191957631Seric 	name->nodename[0] = '\0';
192057631Seric 
192157661Seric 	/* try /etc/whoami -- one line with the node name */
192257631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
192357631Seric 	{
192457661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
192557631Seric 		(void) fclose(file);
192657661Seric 		n = strchr(name->nodename, '\n');
192757631Seric 		if (n != NULL)
192857631Seric 			*n = '\0';
192957631Seric 		if (name->nodename[0] != '\0')
193057631Seric 			return (0);
193157631Seric 	}
193257631Seric 
193357661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
193457631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
193557631Seric 	{
193657631Seric 		char buf[MAXLINE];
193757631Seric 
193857631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
193957631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
194057631Seric 					NODE_LENGTH, name->nodename) > 0)
194157631Seric 				break;
194257631Seric 		(void) fclose(file);
194357631Seric 		if (name->nodename[0] != '\0')
194457631Seric 			return (0);
194557631Seric 	}
194657631Seric 
194757631Seric #ifdef TRUST_POPEN
194857631Seric 	/*
194957631Seric 	**  Popen is known to have security holes.
195057631Seric 	*/
195157631Seric 
195257661Seric 	/* try uuname -l to return local name */
195357631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
195457631Seric 	{
195557661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
195657631Seric 		(void) pclose(file);
195757661Seric 		n = strchr(name, '\n');
195857631Seric 		if (n != NULL)
195957631Seric 			*n = '\0';
196057661Seric 		if (name->nodename[0] != '\0')
196157631Seric 			return (0);
196257631Seric 	}
196357631Seric #endif
196469472Seric 
196557631Seric 	return (-1);
196657631Seric }
196757943Seric #endif /* HASUNAME */
196858068Seric /*
196958068Seric **  INITGROUPS -- initialize groups
197058068Seric **
197158068Seric **	Stub implementation for System V style systems
197258068Seric */
197358068Seric 
197458068Seric #ifndef HASINITGROUPS
197558068Seric 
197658068Seric initgroups(name, basegid)
197758068Seric 	char *name;
197858068Seric 	int basegid;
197958068Seric {
198058068Seric 	return 0;
198158068Seric }
198258068Seric 
198358068Seric #endif
198458082Seric /*
198559289Seric **  SETSID -- set session id (for non-POSIX systems)
198659289Seric */
198759289Seric 
198859289Seric #ifndef HASSETSID
198959289Seric 
199063753Seric pid_t
199163753Seric setsid __P ((void))
199259289Seric {
199364155Seric #ifdef TIOCNOTTY
199464155Seric 	int fd;
199564155Seric 
199666743Seric 	fd = open("/dev/tty", O_RDWR, 0);
199764155Seric 	if (fd >= 0)
199864155Seric 	{
199964155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
200064155Seric 		(void) close(fd);
200164155Seric 	}
200264155Seric #endif /* TIOCNOTTY */
200364705Seric # ifdef SYS5SETPGRP
200463753Seric 	return setpgrp();
200563753Seric # else
200664155Seric 	return setpgid(0, getpid());
200759289Seric # endif
200859289Seric }
200959289Seric 
201059289Seric #endif
201159289Seric /*
201266748Seric **  FSYNC -- dummy fsync
201366748Seric */
201466748Seric 
201566748Seric #ifdef NEEDFSYNC
201666748Seric 
201766748Seric fsync(fd)
201866748Seric 	int fd;
201966748Seric {
202066748Seric # ifdef O_SYNC
202166748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
202266748Seric # else
202366748Seric 	/* nothing we can do */
202466748Seric 	return 0;
202566748Seric # endif
202666748Seric }
202766748Seric 
202866748Seric #endif
202966748Seric /*
203064718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
203164718Seric **
203264718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
203368508Seric **	instead of a long.  This patches things.  Only needed on versions
203468508Seric **	prior to 5.4.3.
203564718Seric */
203664718Seric 
203768508Seric #ifdef DGUX_5_4_2
203864718Seric 
203964718Seric #undef inet_addr
204064718Seric 
204164718Seric long
204264718Seric dgux_inet_addr(host)
204364718Seric 	char *host;
204464718Seric {
204564718Seric 	struct in_addr haddr;
204664718Seric 
204764718Seric 	haddr = inet_addr(host);
204864718Seric 	return haddr.s_addr;
204964718Seric }
205064718Seric 
205164718Seric #endif
205264718Seric /*
205363969Seric **  GETOPT -- for old systems or systems with bogus implementations
205463969Seric */
205563969Seric 
205663969Seric #ifdef NEEDGETOPT
205763969Seric 
205863969Seric /*
205963969Seric  * Copyright (c) 1985 Regents of the University of California.
206063969Seric  * All rights reserved.  The Berkeley software License Agreement
206163969Seric  * specifies the terms and conditions for redistribution.
206263969Seric  */
206363969Seric 
206463969Seric 
206563969Seric /*
206663969Seric ** this version hacked to add `atend' flag to allow state machine
206763969Seric ** to reset if invoked by the program to scan args for a 2nd time
206863969Seric */
206963969Seric 
207063969Seric #if defined(LIBC_SCCS) && !defined(lint)
207163969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
207264072Seric #endif /* LIBC_SCCS and not lint */
207363969Seric 
207463969Seric #include <stdio.h>
207563969Seric 
207663969Seric /*
207763969Seric  * get option letter from argument vector
207863969Seric  */
207965352Seric #ifdef _CONVEX_SOURCE
208068508Seric extern int	optind, opterr, optopt;
208168508Seric extern char	*optarg;
208265352Seric #else
208365352Seric int	opterr = 1;		/* if error message should be printed */
208465352Seric int	optind = 1;		/* index into parent argv vector */
208568470Seric int	optopt = 0;		/* character checked for validity */
208668470Seric char	*optarg = NULL;		/* argument associated with option */
208768508Seric #endif
208863969Seric 
208963969Seric #define BADCH	(int)'?'
209063969Seric #define EMSG	""
209163969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
209263969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
209363969Seric 
209463969Seric getopt(nargc,nargv,ostr)
209565160Seric 	int		nargc;
209665160Seric 	char *const	*nargv;
209765160Seric 	const char	*ostr;
209863969Seric {
209963969Seric 	static char	*place = EMSG;	/* option letter processing */
210063969Seric 	static char	atend = 0;
210163969Seric 	register char	*oli;		/* option letter list index */
210263969Seric 
210363969Seric 	if (atend) {
210463969Seric 		atend = 0;
210563969Seric 		place = EMSG;
210663969Seric 	}
210763969Seric 	if(!*place) {			/* update scanning pointer */
210864136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
210963969Seric 			atend++;
211063969Seric 			return(EOF);
211163969Seric 		}
211263969Seric 		if (*place == '-') {	/* found "--" */
211363969Seric 			++optind;
211463969Seric 			atend++;
211563969Seric 			return(EOF);
211663969Seric 		}
211763969Seric 	}				/* option letter okay? */
211864125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
211964136Seric 		if (!*place) ++optind;
212063969Seric 		tell(": illegal option -- ");
212163969Seric 	}
212263969Seric 	if (*++oli != ':') {		/* don't need argument */
212363969Seric 		optarg = NULL;
212463969Seric 		if (!*place) ++optind;
212563969Seric 	}
212663969Seric 	else {				/* need an argument */
212763969Seric 		if (*place) optarg = place;	/* no white space */
212863969Seric 		else if (nargc <= ++optind) {	/* no arg */
212963969Seric 			place = EMSG;
213063969Seric 			tell(": option requires an argument -- ");
213163969Seric 		}
213263969Seric 	 	else optarg = nargv[optind];	/* white space */
213363969Seric 		place = EMSG;
213463969Seric 		++optind;
213563969Seric 	}
213663969Seric 	return(optopt);			/* dump back option letter */
213763969Seric }
213863969Seric 
213963969Seric #endif
214063969Seric /*
214163969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
214263969Seric */
214363969Seric 
214463969Seric #ifdef NEEDVPRINTF
214563969Seric 
214663969Seric #define MAXARG	16
214763969Seric 
214863969Seric vfprintf(fp, fmt, ap)
214963969Seric 	FILE *	fp;
215063969Seric 	char *	fmt;
215163969Seric 	char **	ap;
215263969Seric {
215363969Seric 	char *	bp[MAXARG];
215463969Seric 	int	i = 0;
215563969Seric 
215663969Seric 	while (*ap && i < MAXARG)
215763969Seric 		bp[i++] = *ap++;
215863969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
215963969Seric 			 bp[4], bp[5], bp[6], bp[7],
216063969Seric 			 bp[8], bp[9], bp[10], bp[11],
216163969Seric 			 bp[12], bp[13], bp[14], bp[15]);
216263969Seric }
216363969Seric 
216463969Seric vsprintf(s, fmt, ap)
216563969Seric 	char *	s;
216663969Seric 	char *	fmt;
216763969Seric 	char **	ap;
216863969Seric {
216963969Seric 	char *	bp[MAXARG];
217063969Seric 	int	i = 0;
217163969Seric 
217263969Seric 	while (*ap && i < MAXARG)
217363969Seric 		bp[i++] = *ap++;
217463969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
217563969Seric 			bp[4], bp[5], bp[6], bp[7],
217663969Seric 			bp[8], bp[9], bp[10], bp[11],
217763969Seric 			bp[12], bp[13], bp[14], bp[15]);
217863969Seric }
217963969Seric 
218063969Seric #endif
218163969Seric /*
218265220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
218365208Seric **
218465211Seric **	Parameters:
218565211Seric **		shell -- the user's shell from /etc/passwd
218665211Seric **
218765211Seric **	Returns:
218865211Seric **		TRUE -- if it is ok to use this for unrestricted access.
218965211Seric **		FALSE -- if the shell is restricted.
219065208Seric */
219165208Seric 
219265587Seric #if !HASGETUSERSHELL
219365587Seric 
219465587Seric # ifndef _PATH_SHELLS
219565587Seric #  define _PATH_SHELLS	"/etc/shells"
219665587Seric # endif
219765587Seric 
219869762Seric # ifdef _AIX3
219969762Seric #  include <userconf.h>
220069762Seric #  include <usersec.h>
220169762Seric # endif
220269762Seric 
220365587Seric char	*DefaultUserShells[] =
220465587Seric {
220568508Seric 	"/bin/sh",		/* standard shell */
220665587Seric 	"/usr/bin/sh",
220768508Seric 	"/bin/csh",		/* C shell */
220865587Seric 	"/usr/bin/csh",
220965587Seric #ifdef __hpux
221068889Seric # ifdef V4FS
221168889Seric 	"/usr/bin/rsh",		/* restricted Bourne shell */
221268889Seric 	"/usr/bin/ksh",		/* Korn shell */
221368889Seric 	"/usr/bin/rksh",	/* restricted Korn shell */
221468889Seric 	"/usr/bin/pam",
221568889Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
221668889Seric 	"/usr/bin/posix/sh",
221768889Seric # else
221868508Seric 	"/bin/rsh",		/* restricted Bourne shell */
221968508Seric 	"/bin/ksh",		/* Korn shell */
222068508Seric 	"/bin/rksh",		/* restricted Korn shell */
222165587Seric 	"/bin/pam",
222268508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
222365587Seric 	"/bin/posix/sh",
222468889Seric # endif
222565211Seric #endif
222668508Seric #ifdef _AIX3
222768508Seric 	"/bin/ksh",		/* Korn shell */
222868508Seric 	"/usr/bin/ksh",
222968508Seric 	"/bin/tsh",		/* trusted shell */
223068508Seric 	"/usr/bin/tsh",
223168508Seric 	"/bin/bsh",		/* Bourne shell */
223268508Seric 	"/usr/bin/bsh",
223368508Seric #endif
223465587Seric 	NULL
223565587Seric };
223665587Seric 
223765208Seric #endif
223865208Seric 
223965747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
224065747Seric 
224165211Seric bool
224265211Seric usershellok(shell)
224365211Seric 	char *shell;
224465208Seric {
224565211Seric #if HASGETUSERSHELL
224665211Seric 	register char *p;
224765211Seric 	extern char *getusershell();
224865208Seric 
224969505Seric 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
225068457Seric 		return TRUE;
225168457Seric 
225265211Seric 	setusershell();
225365211Seric 	while ((p = getusershell()) != NULL)
225465747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
225565211Seric 			break;
225665211Seric 	endusershell();
225765211Seric 	return p != NULL;
225865211Seric #else
225969769Seric # if USEGETCONFATTR
226069762Seric 	auto char *v;
226169762Seric # endif
226265211Seric 	register FILE *shellf;
226365211Seric 	char buf[MAXLINE];
226465208Seric 
226568457Seric 	if (shell == NULL || shell[0] == '\0')
226668457Seric 		return TRUE;
226768457Seric 
226869769Seric # if USEGETCONFATTR
226969769Seric 	/*
227069769Seric 	**  Naturally IBM has a "better" idea.....
227169769Seric 	**
227269769Seric 	**	What a crock.  This interface isn't documented, it is
227369769Seric 	**	considered part of the security library (-ls), and it
227469769Seric 	**	only works if you are running as root (since the list
227569769Seric 	**	of valid shells is obviously a source of great concern).
227669769Seric 	**	I recommend that you do NOT define USEGETCONFATTR,
227769769Seric 	**	especially since you are going to have to set up an
227869769Seric 	**	/etc/shells anyhow to handle the cases where getconfattr
227969769Seric 	**	fails.
228069769Seric 	*/
228169769Seric 
228269769Seric 	if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
228369762Seric 	{
228469769Seric 		while (*v != '\0')
228569762Seric 		{
228669762Seric 			if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
228769762Seric 				return TRUE;
228869762Seric 			v += strlen(v) + 1;
228969762Seric 		}
229069769Seric 		return FALSE;
229169762Seric 	}
229269762Seric # endif
229369762Seric 
229465211Seric 	shellf = fopen(_PATH_SHELLS, "r");
229565211Seric 	if (shellf == NULL)
229665211Seric 	{
229765211Seric 		/* no /etc/shells; see if it is one of the std shells */
229865587Seric 		char **d;
229965587Seric 
230065587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
230165587Seric 		{
230265587Seric 			if (strcmp(shell, *d) == 0)
230365587Seric 				return TRUE;
230465587Seric 		}
230565587Seric 		return FALSE;
230665211Seric 	}
230765208Seric 
230865211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
230965211Seric 	{
231065211Seric 		register char *p, *q;
231165208Seric 
231265211Seric 		p = buf;
231365211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
231465211Seric 			p++;
231565211Seric 		if (*p == '#' || *p == '\0')
231665208Seric 			continue;
231765211Seric 		q = p;
231865211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
231965211Seric 			p++;
232065211Seric 		*p = '\0';
232165747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
232265211Seric 		{
232365211Seric 			fclose(shellf);
232465211Seric 			return TRUE;
232565211Seric 		}
232665208Seric 	}
232765211Seric 	fclose(shellf);
232865211Seric 	return FALSE;
232965211Seric #endif
233065208Seric }
233165208Seric /*
2332*69801Seric **  FREEDISKSPACE -- see how much free space is on the queue filesystem
233358082Seric **
233458082Seric **	Only implemented if you have statfs.
233558082Seric **
233658082Seric **	Parameters:
233764311Seric **		dir -- the directory in question.
233864311Seric **		bsize -- a variable into which the filesystem
233964311Seric **			block size is stored.
234058082Seric **
234158082Seric **	Returns:
234264311Seric **		The number of bytes free on the queue filesystem.
234364311Seric **		-1 if the statfs call fails.
234464311Seric **
234564311Seric **	Side effects:
234664311Seric **		Puts the filesystem block size into bsize.
234758082Seric */
234858082Seric 
234965749Seric /* statfs types */
235065749Seric #define SFS_NONE	0	/* no statfs implementation */
235165749Seric #define SFS_USTAT	1	/* use ustat */
235265749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
235365749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
235465749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
235565749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
235666752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
235765749Seric 
235865749Seric #ifndef SFS_TYPE
235965749Seric # define SFS_TYPE	SFS_NONE
236058157Seric #endif
236158157Seric 
236265749Seric #if SFS_TYPE == SFS_USTAT
236358157Seric # include <ustat.h>
236458157Seric #endif
236565749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
236665749Seric # include <sys/statfs.h>
236758082Seric #endif
236865749Seric #if SFS_TYPE == SFS_VFS
236965749Seric # include <sys/vfs.h>
237065749Seric #endif
237165749Seric #if SFS_TYPE == SFS_MOUNT
237265749Seric # include <sys/mount.h>
237365749Seric #endif
237466752Seric #if SFS_TYPE == SFS_STATVFS
237566752Seric # include <sys/statvfs.h>
237666752Seric #endif
237758082Seric 
237864311Seric long
2379*69801Seric freediskspace(dir, bsize)
238064311Seric 	char *dir;
238164311Seric 	long *bsize;
238258082Seric {
238365749Seric #if SFS_TYPE != SFS_NONE
238465749Seric # if SFS_TYPE == SFS_USTAT
238558153Seric 	struct ustat fs;
238658153Seric 	struct stat statbuf;
238758366Seric #  define FSBLOCKSIZE	DEV_BSIZE
238868695Seric #  define SFS_BAVAIL	f_tfree
238958157Seric # else
239058157Seric #  if defined(ultrix)
239158157Seric 	struct fs_data fs;
239268695Seric #   define SFS_BAVAIL	fd_bfreen
239368508Seric #   define FSBLOCKSIZE	1024L
239458153Seric #  else
239566752Seric #   if SFS_TYPE == SFS_STATVFS
239666752Seric 	struct statvfs fs;
239768508Seric #    define FSBLOCKSIZE	fs.f_frsize
239866752Seric #   else
239958082Seric 	struct statfs fs;
240066752Seric #    define FSBLOCKSIZE	fs.f_bsize
240163987Seric #   endif
240258153Seric #  endif
240358133Seric # endif
240468695Seric # ifndef SFS_BAVAIL
240568695Seric #  define SFS_BAVAIL f_bavail
240668508Seric # endif
240758082Seric 
240865749Seric # if SFS_TYPE == SFS_USTAT
240964311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
241058157Seric # else
241165749Seric #  if SFS_TYPE == SFS_4ARGS
241264311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
241358157Seric #  else
241468508Seric #   if SFS_TYPE == SFS_STATVFS
241568508Seric 	if (statvfs(dir, &fs) == 0)
241668508Seric #   else
241768508Seric #    if defined(ultrix)
241868499Seric 	if (statfs(dir, &fs) > 0)
241968508Seric #    else
242064311Seric 	if (statfs(dir, &fs) == 0)
242168508Seric #    endif
242258153Seric #   endif
242358133Seric #  endif
242458133Seric # endif
242558133Seric 	{
242664311Seric 		if (bsize != NULL)
242764311Seric 			*bsize = FSBLOCKSIZE;
242868695Seric 		return (fs.SFS_BAVAIL);
242964311Seric 	}
243064337Seric #endif
243164311Seric 	return (-1);
243264311Seric }
243364311Seric /*
2434*69801Seric **  ENOUGHDISKSPACE -- is there enough free space on the queue fs?
243564311Seric **
243664311Seric **	Only implemented if you have statfs.
243764311Seric **
243864311Seric **	Parameters:
243964311Seric **		msize -- the size to check against.  If zero, we don't yet
244064311Seric **		know how big the message will be, so just check for
244164311Seric **		a "reasonable" amount.
244264311Seric **
244364311Seric **	Returns:
244464311Seric **		TRUE if there is enough space.
244564311Seric **		FALSE otherwise.
244664311Seric */
244764311Seric 
244864311Seric bool
2449*69801Seric enoughdiskspace(msize)
245064311Seric 	long msize;
245164311Seric {
245264311Seric 	long bfree, bsize;
245364311Seric 
245464311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
245564311Seric 	{
245658133Seric 		if (tTd(4, 80))
2457*69801Seric 			printf("enoughdiskspace: no threshold\n");
245864311Seric 		return TRUE;
245964311Seric 	}
246064311Seric 
2461*69801Seric 	if ((bfree = freediskspace(QueueDir, &bsize)) >= 0)
246264311Seric 	{
246364311Seric 		if (tTd(4, 80))
2464*69801Seric 			printf("enoughdiskspace: bavail=%ld, need=%ld\n",
246564311Seric 				bfree, msize);
246658333Seric 
246758333Seric 		/* convert msize to block count */
246864311Seric 		msize = msize / bsize + 1;
246958333Seric 		if (MinBlocksFree >= 0)
247058333Seric 			msize += MinBlocksFree;
247158333Seric 
247264311Seric 		if (bfree < msize)
247358090Seric 		{
247458090Seric #ifdef LOG
247558090Seric 			if (LogLevel > 0)
247664254Seric 				syslog(LOG_ALERT,
247764254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
247868508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
247968508Seric 					bfree,
248068508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
248168508Seric 					msize, QueueDir);
248258090Seric #endif
248358082Seric 			return FALSE;
248458090Seric 		}
248558082Seric 	}
248658082Seric 	else if (tTd(4, 80))
2487*69801Seric 		printf("enoughdiskspace failure: min=%ld, need=%ld: %s\n",
248858333Seric 			MinBlocksFree, msize, errstring(errno));
248958082Seric 	return TRUE;
249058082Seric }
249158542Seric /*
249258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
249358542Seric **
249458542Seric **	This looks at an errno value and tells if this is likely to
249558542Seric **	go away if retried later.
249658542Seric **
249758542Seric **	Parameters:
249858542Seric **		err -- the errno code to classify.
249958542Seric **
250058542Seric **	Returns:
250158542Seric **		TRUE if this is probably transient.
250258542Seric **		FALSE otherwise.
250358542Seric */
250458542Seric 
250558542Seric bool
250658542Seric transienterror(err)
250758542Seric 	int err;
250858542Seric {
250958542Seric 	switch (err)
251058542Seric 	{
251158542Seric 	  case EIO:			/* I/O error */
251258542Seric 	  case ENXIO:			/* Device not configured */
251358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
251458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
251558542Seric 	  case ENODEV:			/* Operation not supported by device */
251658542Seric 	  case ENFILE:			/* Too many open files in system */
251758542Seric 	  case EMFILE:			/* Too many open files */
251858542Seric 	  case ENOSPC:			/* No space left on device */
251958542Seric #ifdef ETIMEDOUT
252058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
252158542Seric #endif
252258542Seric #ifdef ESTALE
252358542Seric 	  case ESTALE:			/* Stale NFS file handle */
252458542Seric #endif
252558542Seric #ifdef ENETDOWN
252658542Seric 	  case ENETDOWN:		/* Network is down */
252758542Seric #endif
252858542Seric #ifdef ENETUNREACH
252958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
253058542Seric #endif
253158542Seric #ifdef ENETRESET
253258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
253358542Seric #endif
253458542Seric #ifdef ECONNABORTED
253558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
253658542Seric #endif
253758542Seric #ifdef ECONNRESET
253858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
253958542Seric #endif
254058542Seric #ifdef ENOBUFS
254158542Seric 	  case ENOBUFS:			/* No buffer space available */
254258542Seric #endif
254358542Seric #ifdef ESHUTDOWN
254458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
254558542Seric #endif
254658542Seric #ifdef ECONNREFUSED
254758542Seric 	  case ECONNREFUSED:		/* Connection refused */
254858542Seric #endif
254958542Seric #ifdef EHOSTDOWN
255058542Seric 	  case EHOSTDOWN:		/* Host is down */
255158542Seric #endif
255258542Seric #ifdef EHOSTUNREACH
255358542Seric 	  case EHOSTUNREACH:		/* No route to host */
255458542Seric #endif
255558542Seric #ifdef EDQUOT
255658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
255758542Seric #endif
255858542Seric #ifdef EPROCLIM
255958542Seric 	  case EPROCLIM:		/* Too many processes */
256058542Seric #endif
256158542Seric #ifdef EUSERS
256258542Seric 	  case EUSERS:			/* Too many users */
256358542Seric #endif
256458542Seric #ifdef EDEADLK
256558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
256658542Seric #endif
256758542Seric #ifdef EISCONN
256858542Seric 	  case EISCONN:			/* Socket already connected */
256958542Seric #endif
257058542Seric #ifdef EINPROGRESS
257158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
257258542Seric #endif
257358542Seric #ifdef EALREADY
257458542Seric 	  case EALREADY:		/* Operation already in progress */
257558542Seric #endif
257658542Seric #ifdef EADDRINUSE
257758542Seric 	  case EADDRINUSE:		/* Address already in use */
257858542Seric #endif
257958542Seric #ifdef EADDRNOTAVAIL
258058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
258158542Seric #endif
258266018Seric #ifdef ETXTBSY
258366017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
258466017Seric #endif
258563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
258658542Seric 	  case ENOSR:			/* Out of streams resources */
258758542Seric #endif
258858542Seric 		return TRUE;
258958542Seric 	}
259058542Seric 
259158542Seric 	/* nope, must be permanent */
259258542Seric 	return FALSE;
259358542Seric }
259458689Seric /*
259564035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
259658689Seric **
259758689Seric **	Parameters:
259858689Seric **		fd -- the file descriptor of the file.
259958689Seric **		filename -- the file name (for error messages).
260064335Seric **		ext -- the filename extension.
260158689Seric **		type -- type of the lock.  Bits can be:
260258689Seric **			LOCK_EX -- exclusive lock.
260358689Seric **			LOCK_NB -- non-blocking.
260458689Seric **
260558689Seric **	Returns:
260658689Seric **		TRUE if the lock was acquired.
260758689Seric **		FALSE otherwise.
260858689Seric */
260958689Seric 
261058689Seric bool
261164335Seric lockfile(fd, filename, ext, type)
261258689Seric 	int fd;
261358689Seric 	char *filename;
261464335Seric 	char *ext;
261558689Seric 	int type;
261658689Seric {
261765830Seric # if !HASFLOCK
261858689Seric 	int action;
261958689Seric 	struct flock lfd;
262064335Seric 
262164335Seric 	if (ext == NULL)
262264335Seric 		ext = "";
262369472Seric 
262463983Seric 	bzero(&lfd, sizeof lfd);
262559447Seric 	if (bitset(LOCK_UN, type))
262659447Seric 		lfd.l_type = F_UNLCK;
262759447Seric 	else if (bitset(LOCK_EX, type))
262858689Seric 		lfd.l_type = F_WRLCK;
262958689Seric 	else
263058689Seric 		lfd.l_type = F_RDLCK;
263158689Seric 
263258689Seric 	if (bitset(LOCK_NB, type))
263358689Seric 		action = F_SETLK;
263458689Seric 	else
263558689Seric 		action = F_SETLKW;
263658689Seric 
263764264Seric 	if (tTd(55, 60))
263864335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
263964335Seric 			filename, ext, action, lfd.l_type);
264064264Seric 
264158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
264264264Seric 	{
264364264Seric 		if (tTd(55, 60))
264464264Seric 			printf("SUCCESS\n");
264558689Seric 		return TRUE;
264664264Seric 	}
264758689Seric 
264864264Seric 	if (tTd(55, 60))
264964264Seric 		printf("(%s) ", errstring(errno));
265064264Seric 
265164136Seric 	/*
265264136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
265364136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
265464136Seric 	**  as type "tmp" (that is, served from swap space), the
265564136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
265664136Seric 	**  Since this is fairly common during testing, we will assume
265764136Seric 	**  that this indicates that the lock is successfully grabbed.
265864136Seric 	*/
265964136Seric 
266064136Seric 	if (errno == EINVAL)
266164264Seric 	{
266264264Seric 		if (tTd(55, 60))
266364264Seric 			printf("SUCCESS\n");
266464136Seric 		return TRUE;
266564264Seric 	}
266664136Seric 
266758689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
266864378Seric 	{
266964378Seric 		int omode = -1;
267064378Seric #  ifdef F_GETFL
267164378Seric 		int oerrno = errno;
267264378Seric 
267364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
267464378Seric 		errno = oerrno;
267564378Seric #  endif
267664378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
267764378Seric 			filename, ext, fd, type, omode, geteuid());
267864378Seric 	}
267958689Seric # else
268064335Seric 	if (ext == NULL)
268164335Seric 		ext = "";
268264335Seric 
268364264Seric 	if (tTd(55, 60))
268464335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
268564264Seric 
268658689Seric 	if (flock(fd, type) >= 0)
268764264Seric 	{
268864264Seric 		if (tTd(55, 60))
268964264Seric 			printf("SUCCESS\n");
269058689Seric 		return TRUE;
269164264Seric 	}
269258689Seric 
269364264Seric 	if (tTd(55, 60))
269464264Seric 		printf("(%s) ", errstring(errno));
269564264Seric 
269658689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
269764378Seric 	{
269864378Seric 		int omode = -1;
269964378Seric #  ifdef F_GETFL
270064378Seric 		int oerrno = errno;
270164378Seric 
270264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
270364378Seric 		errno = oerrno;
270464378Seric #  endif
270564378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
270664378Seric 			filename, ext, fd, type, omode, geteuid());
270764386Seric 	}
270858689Seric # endif
270964264Seric 	if (tTd(55, 60))
271064264Seric 		printf("FAIL\n");
271158689Seric 	return FALSE;
271258689Seric }
271364556Seric /*
271465948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
271565948Seric **
271665948Seric **	Parameters:
271765948Seric **		fd -- the file descriptor to check.
271865948Seric **
271965948Seric **	Returns:
272065948Seric **		TRUE -- if only root can chown the file to an arbitrary
272165948Seric **			user.
272265948Seric **		FALSE -- if an arbitrary user can give away a file.
272365948Seric */
272465948Seric 
272565948Seric bool
272665948Seric chownsafe(fd)
272765948Seric 	int fd;
272865948Seric {
272965950Seric #ifdef __hpux
273065948Seric 	char *s;
273165948Seric 	int tfd;
273265948Seric 	uid_t o_uid, o_euid;
273365948Seric 	gid_t o_gid, o_egid;
273465948Seric 	bool rval;
273565948Seric 	struct stat stbuf;
273665948Seric 
273765948Seric 	o_uid = getuid();
273865948Seric 	o_euid = geteuid();
273965948Seric 	o_gid = getgid();
274065948Seric 	o_egid = getegid();
274165959Seric 	fstat(fd, &stbuf);
274265948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
274365948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
274465948Seric 	s = tmpnam(NULL);
274565948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
274665974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
274765948Seric 	close(tfd);
274865948Seric 	unlink(s);
274969638Seric 	setresuid(o_uid, o_euid, -1);
275065948Seric 	setresgid(o_gid, o_egid, -1);
275165948Seric 	return rval;
275265948Seric #else
275366088Seric # ifdef _POSIX_CHOWN_RESTRICTED
275466088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
275566088Seric 	return FALSE;
275666088Seric #  else
275766088Seric 	return TRUE;
275866088Seric #  endif
275966088Seric # else
276066088Seric #  ifdef _PC_CHOWN_RESTRICTED
276168508Seric 	int rval;
276268508Seric 
276368508Seric 	/*
276468508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
276568508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
276668508Seric 	**  the call.  This heuristic checks for that.
276768508Seric 	*/
276868508Seric 
276968508Seric 	errno = 0;
277068508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
277168508Seric 	if (errno == 0)
277268508Seric 		return rval > 0;
277368508Seric #  endif
277468508Seric #  ifdef BSD
277568508Seric 	return TRUE;
277668499Seric #  else
277765948Seric 	return FALSE;
277865948Seric #  endif
277965948Seric # endif
278065948Seric #endif
278165948Seric }
278265948Seric /*
278368508Seric **  RESETLIMITS -- reset system controlled resource limits
278468508Seric **
278568508Seric **	This is to avoid denial-of-service attacks
278668508Seric **
278768508Seric **	Parameters:
278868508Seric **		none
278968508Seric **
279068508Seric **	Returns:
279168508Seric **		none
279268508Seric */
279368508Seric 
279468508Seric #if HASSETRLIMIT
279568508Seric # include <sys/resource.h>
279668508Seric #endif
279768508Seric 
279868508Seric void
279968508Seric resetlimits()
280068508Seric {
280168508Seric #if HASSETRLIMIT
280268508Seric 	struct rlimit lim;
280368508Seric 
280468508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
280568508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
280668508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
280768508Seric #else
280868508Seric # if HASULIMIT
280968508Seric 	(void) ulimit(2, 0x3fffff);
281068508Seric # endif
281168508Seric #endif
281268508Seric }
281368508Seric /*
281464556Seric **  GETCFNAME -- return the name of the .cf file.
281564556Seric **
281664556Seric **	Some systems (e.g., NeXT) determine this dynamically.
281764556Seric */
281864556Seric 
281964556Seric char *
282064556Seric getcfname()
282164556Seric {
282269748Seric #ifdef TRY_VERSIONED_CF_NAME
282368842Seric 	int i;
282468842Seric 	static char cbuf[200];
282569748Seric #endif
282668842Seric 
282764556Seric 	if (ConfFile != NULL)
282864556Seric 		return ConfFile;
282965214Seric #ifdef NETINFO
283065214Seric 	{
283165214Seric 		extern char *ni_propval();
283265214Seric 		char *cflocation;
283365214Seric 
283468508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
283568508Seric 					"sendmail.cf", '\0');
283665214Seric 		if (cflocation != NULL)
283765214Seric 			return cflocation;
283865214Seric 	}
283965214Seric #endif
284068842Seric 
284169396Seric #ifdef TRY_VERSIONED_CF_NAME
284268842Seric 	/*
284368842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
284468842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
284568842Seric 	**
284668842Seric 	**	I suppose it should really try a search path here --
284768842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
284868842Seric 	**	/usr/lib/sendmail.cf, and so forth.
284968842Seric 	*/
285068842Seric 
285168842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
285268842Seric 	i = strlen(cbuf);
285368842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
285468842Seric 	{
285568842Seric 		char *p;
285668842Seric 		extern char Version[];
285768842Seric 
285868842Seric 		strcpy(&cbuf[i - 2], Version);
285968842Seric 		p = strchr(&cbuf[i - 2], '/');
286068842Seric 		if (p != NULL)
286168842Seric 			*p = '\0';
286268842Seric 		p = &cbuf[strlen(cbuf)];
286368842Seric 		do
286468842Seric 		{
286568842Seric 			int fd;
286668842Seric 
286768842Seric 			strcpy(p, ".cf");
286868842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
286968842Seric 			{
287068842Seric 				close(fd);
287168842Seric 				return cbuf;
287268842Seric 			}
287368842Seric 			*p = '\0';
287468842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
287568842Seric 	}
287669396Seric #endif
287764556Seric 	return _PATH_SENDMAILCF;
287864556Seric }
287964718Seric /*
288064718Seric **  SETVENDOR -- process vendor code from V configuration line
288164718Seric **
288264718Seric **	Parameters:
288364718Seric **		vendor -- string representation of vendor.
288464718Seric **
288564718Seric **	Returns:
288664718Seric **		TRUE -- if ok.
288764718Seric **		FALSE -- if vendor code could not be processed.
288864926Seric **
288964926Seric **	Side Effects:
289064926Seric **		It is reasonable to set mode flags here to tweak
289164926Seric **		processing in other parts of the code if necessary.
289264926Seric **		For example, if you are a vendor that uses $%y to
289364926Seric **		indicate YP lookups, you could enable that here.
289464718Seric */
289564718Seric 
289664718Seric bool
289764718Seric setvendor(vendor)
289864718Seric 	char *vendor;
289964718Seric {
290064926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
290168508Seric 	{
290268508Seric 		VendorCode = VENDOR_BERKELEY;
290364926Seric 		return TRUE;
290468508Seric 	}
290564926Seric 
290664926Seric 	/* add vendor extensions here */
290764926Seric 
290868508Seric #ifdef SUN_EXTENSIONS
290968508Seric 	if (strcasecmp(vendor, "Sun") == 0)
291068508Seric 	{
291168508Seric 		VendorCode = VENDOR_SUN;
291268508Seric 		return TRUE;
291368508Seric 	}
291468508Seric #endif
291568508Seric 
291664926Seric 	return FALSE;
291764718Seric }
291864816Seric /*
291969510Seric **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
292069510Seric **
292169510Seric **	Vendor_pre_defaults is called before reading the configuration
292269510Seric **	file; vendor_post_defaults is called immediately after.
292369510Seric **
292469510Seric **	Parameters:
292569510Seric **		e -- the global environment to initialize.
292669510Seric **
292769510Seric **	Returns:
292869510Seric **		none.
292969510Seric */
293069510Seric 
293169510Seric void
293269510Seric vendor_pre_defaults(e)
293369510Seric 	ENVELOPE *e;
293469510Seric {
293569510Seric }
293669510Seric 
293769510Seric 
293869510Seric void
293969510Seric vendor_post_defaults(e)
294069510Seric 	ENVELOPE *e;
294169510Seric {
294269510Seric }
294369510Seric /*
294464816Seric **  STRTOL -- convert string to long integer
294564816Seric **
294664816Seric **	For systems that don't have it in the C library.
294766161Seric **
294866161Seric **	This is taken verbatim from the 4.4-Lite C library.
294964816Seric */
295064816Seric 
295164816Seric #ifdef NEEDSTRTOL
295264816Seric 
295366161Seric #if defined(LIBC_SCCS) && !defined(lint)
295466161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
295566161Seric #endif /* LIBC_SCCS and not lint */
295666161Seric 
295766161Seric #include <limits.h>
295866161Seric 
295966161Seric /*
296066161Seric  * Convert a string to a long integer.
296166161Seric  *
296266161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
296366161Seric  * alphabets and digits are each contiguous.
296466161Seric  */
296566161Seric 
296664816Seric long
296766161Seric strtol(nptr, endptr, base)
296866161Seric 	const char *nptr;
296966161Seric 	char **endptr;
297066161Seric 	register int base;
297164816Seric {
297266161Seric 	register const char *s = nptr;
297366161Seric 	register unsigned long acc;
297466161Seric 	register int c;
297566161Seric 	register unsigned long cutoff;
297666161Seric 	register int neg = 0, any, cutlim;
297764816Seric 
297866161Seric 	/*
297966161Seric 	 * Skip white space and pick up leading +/- sign if any.
298066161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
298166161Seric 	 * assume decimal; if base is already 16, allow 0x.
298266161Seric 	 */
298366161Seric 	do {
298466161Seric 		c = *s++;
298566161Seric 	} while (isspace(c));
298666161Seric 	if (c == '-') {
298766161Seric 		neg = 1;
298866161Seric 		c = *s++;
298966161Seric 	} else if (c == '+')
299066161Seric 		c = *s++;
299166161Seric 	if ((base == 0 || base == 16) &&
299266161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
299366161Seric 		c = s[1];
299466161Seric 		s += 2;
299566161Seric 		base = 16;
299666161Seric 	}
299766161Seric 	if (base == 0)
299866161Seric 		base = c == '0' ? 8 : 10;
299964816Seric 
300066161Seric 	/*
300166161Seric 	 * Compute the cutoff value between legal numbers and illegal
300266161Seric 	 * numbers.  That is the largest legal value, divided by the
300366161Seric 	 * base.  An input number that is greater than this value, if
300466161Seric 	 * followed by a legal input character, is too big.  One that
300566161Seric 	 * is equal to this value may be valid or not; the limit
300666161Seric 	 * between valid and invalid numbers is then based on the last
300766161Seric 	 * digit.  For instance, if the range for longs is
300866161Seric 	 * [-2147483648..2147483647] and the input base is 10,
300966161Seric 	 * cutoff will be set to 214748364 and cutlim to either
301066161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
301166161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
301266161Seric 	 * the number is too big, and we will return a range error.
301366161Seric 	 *
301466161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
301566161Seric 	 * overflow.
301666161Seric 	 */
301766161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
301866161Seric 	cutlim = cutoff % (unsigned long)base;
301966161Seric 	cutoff /= (unsigned long)base;
302066161Seric 	for (acc = 0, any = 0;; c = *s++) {
302166161Seric 		if (isdigit(c))
302266161Seric 			c -= '0';
302366161Seric 		else if (isalpha(c))
302466161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
302566161Seric 		else
302666161Seric 			break;
302766161Seric 		if (c >= base)
302866161Seric 			break;
302966161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
303066161Seric 			any = -1;
303166161Seric 		else {
303266161Seric 			any = 1;
303366161Seric 			acc *= base;
303466161Seric 			acc += c;
303564816Seric 		}
303664816Seric 	}
303766161Seric 	if (any < 0) {
303866161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
303966161Seric 		errno = ERANGE;
304066161Seric 	} else if (neg)
304166161Seric 		acc = -acc;
304266161Seric 	if (endptr != 0)
304366161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
304466161Seric 	return (acc);
304564816Seric }
304664816Seric 
304764816Seric #endif
304864841Seric /*
304969679Seric **  STRSTR -- find first substring in string
305069679Seric **
305169679Seric **	Parameters:
305269679Seric **		big -- the big (full) string.
305369679Seric **		little -- the little (sub) string.
305469679Seric **
305569679Seric **	Returns:
305669679Seric **		A pointer to the first instance of little in big.
305769679Seric **		big if little is the null string.
305869679Seric **		NULL if little is not contained in big.
305969679Seric */
306069679Seric 
306169679Seric #ifdef NEEDSTRSTR
306269679Seric 
306369679Seric char *
306469679Seric strstr(big, little)
306569679Seric 	char *big;
306669679Seric 	char *little;
306769679Seric {
306869679Seric 	register char *p = big;
306969679Seric 	int l;
307069679Seric 
307169679Seric 	if (*little == '\0')
307269679Seric 		return big;
307369679Seric 	l = strlen(little);
307469679Seric 
307569679Seric 	while ((p = strchr(p, *little)) != NULL)
307669679Seric 	{
307769679Seric 		if (strncmp(p, little, l) == 0)
307869679Seric 			return p;
307969679Seric 		p++;
308069679Seric 	}
308169679Seric 	return NULL;
308269679Seric }
308369679Seric 
308469679Seric #endif
308569679Seric /*
308668693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
308764841Seric **
308868693Seric **	Some operating systems have wierd problems with the gethostbyXXX
308968693Seric **	routines.  For example, Solaris versions at least through 2.3
309068693Seric **	don't properly deliver a canonical h_name field.  This tries to
309168693Seric **	work around these problems.
309264841Seric */
309364841Seric 
309464841Seric struct hostent *
309568693Seric sm_gethostbyname(name)
309668707Seric 	char *name;
309764841Seric {
309869777Seric 	struct hostent *h;
309969654Seric #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4)
310068693Seric 	extern int h_errno;
310168693Seric 
310268508Seric # if SOLARIS == 203
310364841Seric 	static struct hostent hp;
310464841Seric 	static char buf[1000];
310564841Seric 	extern struct hostent *_switch_gethostbyname_r();
310664841Seric 
310769777Seric 	if (tTd(61, 10))
310869777Seric 		printf("_switch_gethostbyname_r(%s)... ", name);
310969777Seric 	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
311064841Seric # else
311165009Seric 	extern struct hostent *__switch_gethostbyname();
311265009Seric 
311369777Seric 	if (tTd(61, 10))
311469777Seric 		printf("__switch_gethostbyname(%s)... ", name);
311569777Seric 	h = __switch_gethostbyname(name);
311664841Seric # endif
311768693Seric #else
311869777Seric # if defined(NIS)
311969687Seric 	int nmaps;
312069687Seric 	char *maptype[MAXMAPSTACK];
312169687Seric 	short mapreturn[MAXMAPACTIONS];
312269687Seric 	char hbuf[MAXNAME];
312369777Seric # endif
312469687Seric 
312569777Seric 	if (tTd(61, 10))
312669777Seric 		printf("gethostbyname(%s)... ", name);
312769687Seric 	h = gethostbyname(name);
312869687Seric # if defined(NIS)
312969777Seric 	if (h == NULL)
313069687Seric 	{
313169777Seric 		if (tTd(61, 10))
313269777Seric 			printf("failure\n");
313369687Seric 
313469777Seric 		nmaps = switch_map_find("hosts", maptype, mapreturn);
313569777Seric 		while (--nmaps >= 0)
313669777Seric 			if (strcmp(maptype[nmaps], "nis") == 0)
313769777Seric 				break;
313869777Seric 		if (nmaps >= 0)
313969777Seric 		{
314069777Seric 			/* try short name */
314169777Seric 			if (strlen(name) > sizeof hbuf - 1)
314269777Seric 				return NULL;
314369777Seric 			strcpy(hbuf, name);
314469777Seric 			shorten_hostname(hbuf);
314569777Seric 
314669777Seric 			/* if it hasn't been shortened, there's no point */
314769777Seric 			if (strcmp(hbuf, name) != 0)
314869777Seric 			{
314969777Seric 				if (tTd(61, 10))
315069777Seric 					printf("gethostbyname(%s)... ", hbuf);
315169777Seric 				h = gethostbyname(hbuf);
315269777Seric 			}
315369777Seric 		}
315469687Seric 	}
315569687Seric # endif
315669777Seric #endif
315769777Seric 	if (tTd(61, 10))
315869777Seric 	{
315969777Seric 		if (h == NULL)
316069777Seric 			printf("failure\n");
316169777Seric 		else
316269777Seric 			printf("%s\n", h->h_name);
316369777Seric 	}
316469687Seric 	return h;
316564841Seric }
316664841Seric 
316764841Seric struct hostent *
316868693Seric sm_gethostbyaddr(addr, len, type)
316968707Seric 	char *addr;
317064841Seric 	int len;
317164841Seric 	int type;
317264841Seric {
317368693Seric #if defined(SOLARIS) && SOLARIS < 204
317468693Seric 	extern int h_errno;
317568693Seric 
317668508Seric # if SOLARIS == 203
317764841Seric 	static struct hostent hp;
317864841Seric 	static char buf[1000];
317964841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
318064841Seric 
318164841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
318264841Seric # else
318365009Seric 	extern struct hostent *__switch_gethostbyaddr();
318465009Seric 
318564841Seric 	return __switch_gethostbyaddr(addr, len, type);
318664841Seric # endif
318768693Seric #else
318868693Seric 	return gethostbyaddr(addr, len, type);
318968693Seric #endif
319064841Seric }
319168693Seric /*
319268693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
319368693Seric */
319464841Seric 
319568693Seric struct passwd *
319668693Seric sm_getpwnam(user)
319768704Seric 	char *user;
319868693Seric {
319968693Seric 	return getpwnam(user);
320068693Seric }
320168693Seric 
320268693Seric struct passwd *
320368693Seric sm_getpwuid(uid)
320468693Seric 	uid_t uid;
320568693Seric {
320668693Seric 	return getpwuid(uid);
320768693Seric }
320865214Seric /*
320969472Seric **  LOAD_IF_NAMES -- load interface-specific names into $=w
321069472Seric **
321169472Seric **	Parameters:
321269472Seric **		none.
321369472Seric **
321469472Seric **	Returns:
321569472Seric **		none.
321669472Seric **
321769472Seric **	Side Effects:
321869472Seric **		Loads $=w with the names of all the interfaces.
321969472Seric */
322069472Seric 
322169472Seric #ifdef SIOCGIFCONF
322269565Seric struct rtentry;
322369565Seric struct mbuf;
322469472Seric # include <arpa/inet.h>
322569562Seric # include <sys/time.h>
322669472Seric # include <net/if.h>
322769472Seric #endif
322869472Seric 
322969472Seric void
323069472Seric load_if_names()
323169472Seric {
323269472Seric #ifdef SIOCGIFCONF
323369472Seric 	int s;
323469516Seric 	int i;
323569472Seric         struct ifconf ifc;
323669472Seric 	char interfacebuf[1024];
323769472Seric 
323869472Seric 	s = socket(AF_INET, SOCK_DGRAM, 0);
323969472Seric 	if (s == -1)
324069472Seric 		return;
324169472Seric 
324269472Seric 	/* get the list of known IP address from the kernel */
324369472Seric         ifc.ifc_buf = interfacebuf;
324469516Seric         ifc.ifc_len = sizeof interfacebuf;
324569472Seric 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
324669472Seric 	{
324769472Seric 		if (tTd(0, 4))
324869472Seric 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
324969516Seric 		close(s);
325069472Seric 		return;
325169472Seric 	}
325269516Seric 	close(s);
325369472Seric 
325469472Seric 	/* scan the list of IP address */
325569472Seric 	if (tTd(0, 4))
325669472Seric 		printf("scanning for interface specific names, ifc_len=%d\n",
325769472Seric 			ifc.ifc_len);
325869472Seric 
325969516Seric 	for (i = 0; i < ifc.ifc_len; )
326069516Seric 	{
326169516Seric 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
326269516Seric 		struct sockaddr *sa = &ifr->ifr_addr;
326369472Seric 		struct in_addr ia;
326469516Seric 		struct hostent *hp;
326569472Seric 		char ip_addr[256];
326669516Seric 		extern char *inet_ntoa();
326769516Seric 		extern struct hostent *gethostbyaddr();
326869472Seric 
326969516Seric #ifdef BSD4_4_SOCKADDR
327069516Seric 		if (sa->sa_len > sizeof ifr->ifr_addr)
327169516Seric 			i += sizeof ifr->ifr_name + sa->sa_len;
327269516Seric 		else
327369516Seric #endif
327469516Seric 			i += sizeof *ifr;
327569516Seric 
327669472Seric 		if (tTd(0, 20))
327769516Seric 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
327869472Seric 
327969472Seric 		if (ifr->ifr_addr.sa_family != AF_INET)
328069472Seric 			continue;
328169472Seric 
328269472Seric 		/* extract IP address from the list*/
328369516Seric 		ia = (((struct sockaddr_in *) sa)->sin_addr);
328469472Seric 
328569472Seric 		/* save IP address in text from */
328669472Seric 		(void) sprintf(ip_addr, "[%s]",
328769516Seric 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
328869472Seric 		if (!wordinclass(ip_addr, 'w'))
328969472Seric 		{
329069472Seric 			setclass('w', ip_addr);
329169472Seric 			if (tTd(0, 4))
329269472Seric 				printf("\ta.k.a.: %s\n", ip_addr);
329369472Seric 		}
329469472Seric 
329569472Seric 		/* skip "loopback" interface "lo" */
329669472Seric 		if (strcmp("lo0", ifr->ifr_name) == 0)
329769472Seric 			continue;
329869472Seric 
329969472Seric 		/* lookup name with IP address */
330069472Seric 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
330169472Seric 		if (hp == NULL)
330269472Seric 		{
330369472Seric 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
330469472Seric 				inet_ntoa(ia));
330569472Seric 			continue;
330669472Seric 		}
330769472Seric 
330869472Seric 		/* save its cname */
330969472Seric 		if (!wordinclass(hp->h_name, 'w'))
331069472Seric 		{
331169472Seric 			setclass('w', hp->h_name);
331269472Seric 			if (tTd(0, 4))
331369472Seric 				printf("\ta.k.a.: %s\n", hp->h_name);
331469472Seric 		}
331569472Seric 
331669472Seric 		/* save all it aliases name */
331769472Seric 		while (*hp->h_aliases)
331869472Seric 		{
331969472Seric 			if (!wordinclass(*hp->h_aliases, 'w'))
332069472Seric 			{
332169472Seric 				setclass('w', *hp->h_aliases);
332269472Seric 				if (tTd(0, 4))
332369472Seric 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
332469472Seric 			}
332569472Seric 			hp->h_aliases++;
332669472Seric 		}
332769472Seric 	}
332869472Seric #endif
332969472Seric }
333069472Seric /*
333165214Seric **  NI_PROPVAL -- netinfo property value lookup routine
333265214Seric **
333365214Seric **	Parameters:
333468508Seric **		keydir -- the Netinfo directory name in which to search
333568508Seric **			for the key.
333668508Seric **		keyprop -- the name of the property in which to find the
333768508Seric **			property we are interested.  Defaults to "name".
333868508Seric **		keyval -- the value for which we are really searching.
333968508Seric **		valprop -- the property name for the value in which we
334068508Seric **			are interested.
334168508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
334268508Seric **			we should return a string separated by this
334368508Seric **			character.
334465214Seric **
334565214Seric **	Returns:
334665214Seric **		NULL -- if:
334765214Seric **			1. the directory is not found
334865214Seric **			2. the property name is not found
334965214Seric **			3. the property contains multiple values
335065214Seric **			4. some error occured
335165214Seric **		else -- the location of the config file.
335265214Seric **
335368508Seric **	Example:
335468508Seric **		To search for an alias value, use:
335568508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
335668508Seric **
335765214Seric **	Notes:
335865214Seric **      	Caller should free the return value of ni_proval
335965214Seric */
336065214Seric 
336165214Seric #ifdef NETINFO
336265214Seric 
336365214Seric # include <netinfo/ni.h>
336465214Seric 
336565214Seric # define LOCAL_NETINFO_DOMAIN    "."
336665214Seric # define PARENT_NETINFO_DOMAIN   ".."
336765214Seric # define MAX_NI_LEVELS           256
336865214Seric 
336965214Seric char *
337068508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
337168508Seric 	char *keydir;
337268508Seric 	char *keyprop;
337368508Seric 	char *keyval;
337468508Seric 	char *valprop;
337568508Seric 	char sepchar;
337665214Seric {
337765820Seric 	char *propval = NULL;
337865214Seric 	int i;
337968508Seric 	int j, alen;
338065214Seric 	void *ni = NULL;
338165214Seric 	void *lastni = NULL;
338265214Seric 	ni_status nis;
338365214Seric 	ni_id nid;
338465214Seric 	ni_namelist ninl;
338568508Seric 	register char *p;
338668508Seric 	char keybuf[1024];
338765214Seric 
338865214Seric 	/*
338968508Seric 	**  Create the full key from the two parts.
339068508Seric 	**
339168508Seric 	**	Note that directory can end with, e.g., "name=" to specify
339268508Seric 	**	an alternate search property.
339368508Seric 	*/
339468508Seric 
339568508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
339668508Seric 	if (keyprop != NULL)
339768508Seric 		i += strlen(keyprop) + 1;
339868508Seric 	if (i > sizeof keybuf)
339968508Seric 		return NULL;
340068508Seric 	strcpy(keybuf, keydir);
340168508Seric 	strcat(keybuf, "/");
340268508Seric 	if (keyprop != NULL)
340368508Seric 	{
340468508Seric 		strcat(keybuf, keyprop);
340568508Seric 		strcat(keybuf, "=");
340668508Seric 	}
340768508Seric 	strcat(keybuf, keyval);
340868508Seric 
340968508Seric 	/*
341065214Seric 	**  If the passed directory and property name are found
341165214Seric 	**  in one of netinfo domains we need to search (starting
341265214Seric 	**  from the local domain moving all the way back to the
341365214Seric 	**  root domain) set propval to the property's value
341465214Seric 	**  and return it.
341565214Seric 	*/
341665214Seric 
341765214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
341865214Seric 	{
341965214Seric 		if (i == 0)
342065214Seric 		{
342165214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
342265214Seric 		}
342365214Seric 		else
342465214Seric 		{
342565214Seric 			if (lastni != NULL)
342665214Seric 				ni_free(lastni);
342765214Seric 			lastni = ni;
342865214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
342965214Seric 		}
343065214Seric 
343165214Seric 		/*
343265214Seric 		**  Don't bother if we didn't get a handle on a
343365214Seric 		**  proper domain.  This is not necessarily an error.
343465214Seric 		**  We would get a positive ni_status if, for instance
343565214Seric 		**  we never found the directory or property and tried
343665214Seric 		**  to open the parent of the root domain!
343765214Seric 		*/
343865214Seric 
343965214Seric 		if (nis != 0)
344065214Seric 			break;
344165214Seric 
344265214Seric 		/*
344365214Seric 		**  Find the path to the server information.
344465214Seric 		*/
344565214Seric 
344668508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
344765214Seric 			continue;
344865214Seric 
344965214Seric 		/*
345068508Seric 		**  Find associated value information.
345165214Seric 		*/
345265214Seric 
345368508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
345465214Seric 			continue;
345565214Seric 
345665214Seric 		/*
345768508Seric 		**  See if we have an acceptable number of values.
345865214Seric 		*/
345965214Seric 
346068508Seric 		if (ninl.ni_namelist_len <= 0)
346168508Seric 			continue;
346268508Seric 
346368508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
346465214Seric 		{
346568508Seric 			ni_namelist_free(&ninl);
346668508Seric 			continue;
346765214Seric 		}
346868508Seric 
346969472Seric 		/*
347068508Seric 		**  Calculate number of bytes needed and build result
347168508Seric 		*/
347268508Seric 
347368508Seric 		alen = 1;
347468508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
347568508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
347668508Seric 		propval = p = xalloc(alen);
347768508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
347868508Seric 		{
347968508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
348068508Seric 			p += strlen(p);
348168508Seric 			*p++ = sepchar;
348269472Seric 		}
348368508Seric 		*--p = '\0';
348468508Seric 
348568508Seric 		ni_namelist_free(&ninl);
348665214Seric 	}
348765214Seric 
348865214Seric 	/*
348965214Seric 	**  Clean up.
349065214Seric 	*/
349165214Seric 
349265214Seric 	if (ni != NULL)
349365214Seric 		ni_free(ni);
349465214Seric 	if (lastni != NULL && ni != lastni)
349565214Seric 		ni_free(lastni);
349665214Seric 
349765214Seric 	return propval;
349865214Seric }
349965214Seric 
350065214Seric #endif /* NETINFO */
350165504Seric /*
350265504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
350365504Seric **
350465504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
350565504Seric **	syslog succeeds during interrupt handlers.
350665504Seric */
350765504Seric 
350865504Seric #ifdef __hpux
350965504Seric 
351065504Seric # define MAXSYSLOGTRIES	100
351165504Seric # undef syslog
351265504Seric 
351369758Seric # ifdef __STDC__
351469748Seric void
351565504Seric hard_syslog(int pri, char *msg, ...)
351665504Seric # else
351765504Seric hard_syslog(pri, msg, va_alist)
351865504Seric 	int pri;
351965504Seric 	char *msg;
352065504Seric 	va_dcl
352165504Seric # endif
352265504Seric {
352365504Seric 	int i;
352465504Seric 	char buf[SYSLOG_BUFSIZE * 2];
352565504Seric 	VA_LOCAL_DECL;
352665504Seric 
352765504Seric 	VA_START(msg);
352865504Seric 	vsprintf(buf, msg, ap);
352965504Seric 	VA_END;
353065504Seric 
353165504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
353265504Seric 		continue;
353365504Seric }
353465504Seric 
353565504Seric #endif
353668696Seric /*
353768696Seric **  LOCAL_HOSTNAME_LENGTH
353868696Seric **
353968696Seric **	This is required to get sendmail to compile against BIND 4.9.x
354068696Seric **	on Ultrix.
354168696Seric */
354268696Seric 
354368696Seric #if defined(ultrix) && NAMED_BIND
354468696Seric 
354568696Seric # include <resolv.h>
354668696Seric # if __RES >= 19931104
354768696Seric 
354868696Seric int
354968696Seric local_hostname_length(hostname)
355068696Seric 	char *hostname;
355168696Seric {
355268696Seric 	int len_host, len_domain;
355368696Seric 
355468696Seric 	if (!*_res.defdname)
355568696Seric 		res_init();
355668696Seric 	len_host = strlen(hostname);
355768696Seric 	len_domain = strlen(_res.defdname);
355868696Seric 	if (len_host > len_domain &&
355968696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
356068696Seric 	    hostname[len_host - len_domain - 1] == '.')
356168696Seric 		return len_host - len_domain - 1;
356268696Seric 	else
356368696Seric 		return 0;
356468696Seric }
356568696Seric 
356668696Seric # endif
356768696Seric #endif
356868741Seric /*
356968741Seric **  Compile-Time options
357068741Seric */
357168741Seric 
357268741Seric char	*CompileOptions[] =
357368741Seric {
357468741Seric #if HESIOD
357568741Seric 	"HESIOD",
357668741Seric #endif
357768741Seric #if LOG
357868741Seric 	"LOG",
357968741Seric #endif
358068741Seric #if MATCHGECOS
358168741Seric 	"MATCHGECOS",
358268741Seric #endif
358368741Seric #if NAMED_BIND
358468741Seric 	"NAMED_BIND",
358568741Seric #endif
358668741Seric #if NDBM
358768741Seric 	"NDBM",
358868741Seric #endif
358968741Seric #if NETINET
359068741Seric 	"NETINET",
359168741Seric #endif
359268741Seric #if NETINFO
359368741Seric 	"NETINFO",
359468741Seric #endif
359568741Seric #if NETISO
359668741Seric 	"NETISO",
359768741Seric #endif
359868741Seric #if NETNS
359968741Seric 	"NETNS",
360068741Seric #endif
360168741Seric #if NETUNIX
360268741Seric 	"NETUNIX",
360368741Seric #endif
360468741Seric #if NETX25
360568741Seric 	"NETX25",
360668741Seric #endif
360768741Seric #if NEWDB
360868741Seric 	"NEWDB",
360968741Seric #endif
361068741Seric #if NIS
361168741Seric 	"NIS",
361268741Seric #endif
361369518Seric #if NISPLUS
361469518Seric 	"NISPLUS",
361569518Seric #endif
361668741Seric #if SCANF
361768741Seric 	"SCANF",
361868741Seric #endif
361968741Seric #if SUID_ROOT_FILES_OK
362068741Seric 	"SUID_ROOT_FILES_OK",
362168741Seric #endif
362268741Seric #if USERDB
362368741Seric 	"USERDB",
362468741Seric #endif
362568741Seric #if XDEBUG
362668741Seric 	"XDEBUG",
362768741Seric #endif
362868741Seric #if XLA
362968741Seric 	"XLA",
363068741Seric #endif
363168741Seric 	NULL
363268741Seric };
3633