xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68706)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 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*68706Seric static char sccsid[] = "@(#)conf.c	8.147 (Berkeley) 03/31/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,
95*68706Seric 	"content-length",		H_ACHECK,
9658796Seric 
979055Seric 	NULL,			0,
982897Seric };
994166Seric 
1004166Seric 
1014166Seric 
1024282Seric /*
1034282Seric **  Location of system files/databases/etc.
1044282Seric */
1054282Seric 
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12264333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12364333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12566783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12658082Seric 	"goaway",		PRIV_GOAWAY,
12758789Seric 	NULL,			0,
12858082Seric };
12958082Seric 
13058082Seric 
13158082Seric 
13258082Seric /*
13324943Seric **  Miscellaneous stuff.
1349039Seric */
1359039Seric 
13624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13724943Seric /*
13824943Seric **  SETDEFAULTS -- set default values
13924943Seric **
14024943Seric **	Because of the way freezing is done, these must be initialized
14124943Seric **	using direct code.
14224943Seric **
14324943Seric **	Parameters:
14458734Seric **		e -- the default envelope.
14524943Seric **
14624943Seric **	Returns:
14724943Seric **		none.
14824943Seric **
14924943Seric **	Side Effects:
15024943Seric **		Initializes a bunch of global variables to their
15124943Seric **		default values.
15224943Seric */
15324943Seric 
15458737Seric #define DAYS		* 24 * 60 * 60
15558737Seric 
15668508Seric void
15758734Seric setdefaults(e)
15858734Seric 	register ENVELOPE *e;
15924943Seric {
16068508Seric 	int i;
16168508Seric 	extern void inittimeouts();
16268508Seric 	extern void setdefuser();
16368508Seric 	extern void setupmaps();
16468508Seric 	extern void setupmailers();
16568508Seric 
16657438Seric 	SpaceSub = ' ';				/* option B */
16757438Seric 	QueueLA = 8;				/* option x */
16857438Seric 	RefuseLA = 12;				/* option X */
16957438Seric 	WkRecipFact = 30000L;			/* option y */
17057438Seric 	WkClassFact = 1800L;			/* option z */
17157438Seric 	WkTimeFact = 90000L;			/* option Z */
17257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17457438Seric 						/* option F */
17557438Seric 	DefUid = 1;				/* option u */
17657438Seric 	DefGid = 1;				/* option g */
17757438Seric 	CheckpointInterval = 10;		/* option C */
17857438Seric 	MaxHopCount = 25;			/* option h */
17958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
18168508Seric 	SevenBitInput = FALSE;			/* option 7 */
18257438Seric 	MaxMciCache = 1;			/* option k */
18357438Seric 	MciCacheTimeout = 300;			/* option K */
18457438Seric 	LogLevel = 9;				/* option L */
18568508Seric 	inittimeouts(NULL);			/* option r */
18658853Seric 	PrivacyFlags = 0;			/* option p */
18768508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18868508Seric 	for (i = 0; i < MAXTOCLASS; i++)
18968508Seric 	{
19068508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19168508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19268508Seric 	}
19368508Seric 	ServiceSwitchFile = "/etc/service.switch";
19440973Sbostic 	setdefuser();
19553654Seric 	setupmaps();
19657402Seric 	setupmailers();
19724943Seric }
198294Seric 
19940973Sbostic 
2004326Seric /*
20140973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20240973Sbostic */
20340973Sbostic 
20468508Seric void
20540973Sbostic setdefuser()
20640973Sbostic {
20740973Sbostic 	struct passwd *defpwent;
20857386Seric 	static char defuserbuf[40];
20940973Sbostic 
21057386Seric 	DefUser = defuserbuf;
21168693Seric 	if ((defpwent = sm_getpwuid(DefUid)) != NULL)
21257386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21340973Sbostic 	else
21457386Seric 		strcpy(defuserbuf, "nobody");
21540973Sbostic }
21653654Seric /*
21756836Seric **  HOST_MAP_INIT -- initialize host class structures
21856836Seric */
21956836Seric 
22068508Seric bool	host_map_init __P((MAP *map, char *args));
22168508Seric 
22256836Seric bool
22360219Seric host_map_init(map, args)
22456836Seric 	MAP *map;
22556836Seric 	char *args;
22656836Seric {
22756836Seric 	register char *p = args;
22856836Seric 
22956836Seric 	for (;;)
23056836Seric 	{
23158050Seric 		while (isascii(*p) && isspace(*p))
23256836Seric 			p++;
23356836Seric 		if (*p != '-')
23456836Seric 			break;
23556836Seric 		switch (*++p)
23656836Seric 		{
23756836Seric 		  case 'a':
23856836Seric 			map->map_app = ++p;
23956836Seric 			break;
24056836Seric 		}
24158050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24256836Seric 			p++;
24356836Seric 		if (*p != '\0')
24456836Seric 			*p++ = '\0';
24556836Seric 	}
24656836Seric 	if (map->map_app != NULL)
24756836Seric 		map->map_app = newstr(map->map_app);
24856836Seric 	return TRUE;
24956836Seric }
25057402Seric /*
25157402Seric **  SETUPMAILERS -- initialize default mailers
25257402Seric */
25356836Seric 
25468508Seric void
25557402Seric setupmailers()
25657402Seric {
25757402Seric 	char buf[100];
25868508Seric 	extern void makemailer();
25957402Seric 
26068508Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u");
26157403Seric 	makemailer(buf);
26257403Seric 
26368508Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE");
26457402Seric 	makemailer(buf);
26557402Seric 
26657402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26757402Seric 	makemailer(buf);
26857402Seric }
26956836Seric /*
27060207Seric **  SETUPMAPS -- set up map classes
27160207Seric */
27260207Seric 
27360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27460207Seric 	{ \
27560207Seric 		extern bool parse __P((MAP *, char *)); \
27660207Seric 		extern bool open __P((MAP *, int)); \
27760207Seric 		extern void close __P((MAP *)); \
27860207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
27960207Seric 		extern void store __P((MAP *, char *, char *)); \
28060207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28160207Seric 		s->s_mapclass.map_cname = name; \
28260207Seric 		s->s_mapclass.map_ext = ext; \
28360207Seric 		s->s_mapclass.map_cflags = flags; \
28460207Seric 		s->s_mapclass.map_parse = parse; \
28560207Seric 		s->s_mapclass.map_open = open; \
28660207Seric 		s->s_mapclass.map_close = close; \
28760207Seric 		s->s_mapclass.map_lookup = lookup; \
28860207Seric 		s->s_mapclass.map_store = store; \
28960207Seric 	}
29060207Seric 
29168508Seric void
29260207Seric setupmaps()
29360207Seric {
29460207Seric 	register STAB *s;
29560207Seric 
29660207Seric #ifdef NEWDB
29760207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29860207Seric 		map_parseargs, hash_map_open, db_map_close,
29960207Seric 		db_map_lookup, db_map_store);
30068508Seric 
30160207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30260207Seric 		map_parseargs, bt_map_open, db_map_close,
30360207Seric 		db_map_lookup, db_map_store);
30460207Seric #endif
30560207Seric 
30660207Seric #ifdef NDBM
30760207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30860207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30960207Seric 		ndbm_map_lookup, ndbm_map_store);
31060207Seric #endif
31160207Seric 
31260207Seric #ifdef NIS
31360207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31468508Seric 		map_parseargs, nis_map_open, null_map_close,
31568508Seric 		nis_map_lookup, null_map_store);
31660207Seric #endif
31760207Seric 
31868508Seric #ifdef NISPLUS
31968508Seric 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
32068508Seric 		map_parseargs, nisplus_map_open, null_map_close,
32168508Seric 		nisplus_map_lookup, null_map_store);
32268508Seric #endif
32368508Seric 
32468508Seric #ifdef HESIOD
32568508Seric 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
32668508Seric 		map_parseargs, null_map_open, null_map_close,
32768508Seric 		hes_map_lookup, null_map_store);
32868508Seric #endif
32968508Seric 
33068508Seric #ifdef NETINFO
33168508Seric 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
33268508Seric 		map_parseargs, ni_map_open, null_map_close,
33368508Seric 		ni_map_lookup, null_map_store);
33468508Seric #endif
33568508Seric 
33668508Seric #if 0
33768508Seric 	MAPDEF("dns", NULL, 0,
33868508Seric 		dns_map_init, null_map_open, null_map_close,
33968508Seric 		dns_map_lookup, null_map_store);
34068508Seric #endif
34168508Seric 
34268508Seric #if NAMED_BIND
34368508Seric 	/* best MX DNS lookup */
34468508Seric 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
34568508Seric 		map_parseargs, null_map_open, null_map_close,
34668508Seric 		bestmx_map_lookup, null_map_store);
34768508Seric #endif
34868508Seric 
34968508Seric 	MAPDEF("host", NULL, 0,
35068508Seric 		host_map_init, null_map_open, null_map_close,
35168508Seric 		host_map_lookup, null_map_store);
35268508Seric 
35368508Seric 	MAPDEF("text", NULL, MCF_ALIASOK,
35468508Seric 		map_parseargs, text_map_open, null_map_close,
35568508Seric 		text_map_lookup, null_map_store);
35668508Seric 
35760207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
35868508Seric 		map_parseargs, stab_map_open, null_map_close,
35960207Seric 		stab_map_lookup, stab_map_store);
36060207Seric 
36160207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
36260207Seric 		map_parseargs, impl_map_open, impl_map_close,
36360207Seric 		impl_map_lookup, impl_map_store);
36460207Seric 
36568508Seric 	/* access to system passwd file */
36668508Seric 	MAPDEF("user", NULL, MCF_OPTFILE,
36768508Seric 		map_parseargs, user_map_open, null_map_close,
36868508Seric 		user_map_lookup, null_map_store);
36960207Seric 
37060207Seric 	/* dequote map */
37160207Seric 	MAPDEF("dequote", NULL, 0,
37260207Seric 		dequote_init, null_map_open, null_map_close,
37360207Seric 		dequote_map, null_map_store);
37460207Seric 
37568521Seric #ifdef USERDB
37660207Seric 	/* user database */
37768521Seric 	MAPDEF("userdb", ".db", 0,
37868521Seric 		map_parseargs, null_map_open, null_map_close,
37960207Seric 		udb_map_lookup, null_map_store);
38060207Seric #endif
38168508Seric 
38268508Seric 	/* sequenced maps */
38368508Seric 	MAPDEF("sequence", NULL, MCF_ALIASOK,
38468508Seric 		seq_map_parse, null_map_open, null_map_close,
38568508Seric 		seq_map_lookup, seq_map_store);
38668508Seric 
38768508Seric 	/* switched interface to sequenced maps */
38868508Seric 	MAPDEF("switch", NULL, MCF_ALIASOK,
38968508Seric 		map_parseargs, switch_map_open, null_map_close,
39068508Seric 		seq_map_lookup, seq_map_store);
39160207Seric }
39260207Seric 
39360207Seric #undef MAPDEF
39460207Seric /*
39568508Seric **  INITHOSTMAPS -- initial host-dependent maps
39668508Seric **
39768508Seric **	This should act as an interface to any local service switch
39868508Seric **	provided by the host operating system.
39968508Seric **
40068508Seric **	Parameters:
40168508Seric **		none
40268508Seric **
40368508Seric **	Returns:
40468508Seric **		none
40568508Seric **
40668508Seric **	Side Effects:
40768508Seric **		Should define maps "host" and "users" as necessary
40868508Seric **		for this OS.  If they are not defined, they will get
40968508Seric **		a default value later.  It should check to make sure
41068508Seric **		they are not defined first, since it's possible that
41168508Seric **		the config file has provided an override.
41268508Seric */
41368508Seric 
41468508Seric void
41568508Seric inithostmaps()
41668508Seric {
41768508Seric 	register int i;
41868508Seric 	int nmaps;
41968508Seric 	char *maptype[MAXMAPSTACK];
42068508Seric 	short mapreturn[MAXMAPACTIONS];
42168508Seric 	char buf[MAXLINE];
42268508Seric 
42368508Seric 	/*
42468508Seric 	**  Set up default hosts maps.
42568508Seric 	*/
42668508Seric 
42768508Seric #if 0
42868508Seric 	nmaps = switch_map_find("hosts", maptype, mapreturn);
42968508Seric 	for (i = 0; i < nmaps; i++)
43068508Seric 	{
43168508Seric 		if (strcmp(maptype[i], "files") == 0 &&
43268508Seric 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
43368508Seric 		{
43468508Seric 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
43568508Seric 			makemapentry(buf);
43668508Seric 		}
43768508Seric #if NAMED_BIND
43868508Seric 		else if (strcmp(maptype[i], "dns") == 0 &&
43968508Seric 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
44068508Seric 		{
44168508Seric 			strcpy(buf, "hosts.dns dns A");
44268508Seric 			makemapentry(buf);
44368508Seric 		}
44468508Seric #endif
44568508Seric #ifdef NISPLUS
44668508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
44768508Seric 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
44868508Seric 		{
44968508Seric 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
45068508Seric 			makemapentry(buf);
45168508Seric 		}
45268508Seric #endif
45368508Seric #ifdef NIS
45468508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
45568508Seric 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
45668508Seric 		{
45768508Seric 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
45868508Seric 			makemapentry(buf);
45968508Seric 		}
46068508Seric #endif
46168508Seric 	}
46268508Seric #endif
46368508Seric 
46468508Seric 	/*
46568508Seric 	**  Make sure we have a host map.
46668508Seric 	*/
46768508Seric 
46868508Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
46968508Seric 	{
47068508Seric 		/* user didn't initialize: set up host map */
47168508Seric 		strcpy(buf, "host host");
47268508Seric #if NAMED_BIND
47368508Seric 		if (ConfigLevel >= 2)
47468508Seric 			strcat(buf, " -a.");
47568508Seric #endif
47668508Seric 		makemapentry(buf);
47768508Seric 	}
47868508Seric 
47968508Seric 	/*
48068508Seric 	**  Set up default aliases maps
48168508Seric 	*/
48268508Seric 
48368508Seric 	nmaps = switch_map_find("aliases", maptype, mapreturn);
48468508Seric 	for (i = 0; i < nmaps; i++)
48568508Seric 	{
48668508Seric 		if (strcmp(maptype[i], "files") == 0 &&
48768508Seric 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
48868508Seric 		{
48968508Seric 			strcpy(buf, "aliases.files implicit /etc/aliases");
49068508Seric 			makemapentry(buf);
49168508Seric 		}
49268508Seric #ifdef NISPLUS
49368508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
49468508Seric 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
49568508Seric 		{
49668508Seric 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
49768508Seric 			makemapentry(buf);
49868508Seric 		}
49968508Seric #endif
50068508Seric #ifdef NIS
50168508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
50268508Seric 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
50368508Seric 		{
50468508Seric 			strcpy(buf, "aliases.nis nis -d mail.aliases");
50568508Seric 			makemapentry(buf);
50668508Seric 		}
50768508Seric #endif
50868508Seric 	}
50968508Seric 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
51068508Seric 	{
51168508Seric 		strcpy(buf, "aliases switch aliases");
51268508Seric 		makemapentry(buf);
51368508Seric 	}
51468508Seric 	strcpy(buf, "switch:aliases");
51568508Seric 	setalias(buf);
51668508Seric 
51768508Seric #if 0		/* "user" map class is a better choice */
51868508Seric 	/*
51968508Seric 	**  Set up default users maps.
52068508Seric 	*/
52168508Seric 
52268508Seric 	nmaps = switch_map_find("passwd", maptype, mapreturn);
52368508Seric 	for (i = 0; i < nmaps; i++)
52468508Seric 	{
52568508Seric 		if (strcmp(maptype[i], "files") == 0 &&
52668508Seric 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
52768508Seric 		{
52868508Seric 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
52968508Seric 			makemapentry(buf);
53068508Seric 		}
53168508Seric #ifdef NISPLUS
53268508Seric 		else if (strcmp(maptype[i], "nisplus") == 0 &&
53368508Seric 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
53468508Seric 		{
53568508Seric 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
53668508Seric 			makemapentry(buf);
53768508Seric 		}
53868508Seric #endif
53968508Seric #ifdef NIS
54068508Seric 		else if (strcmp(maptype[i], "nis") == 0 &&
54168508Seric 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
54268508Seric 		{
54368508Seric 			strcpy(buf, "users.nis nis -m -d passwd.byname");
54468508Seric 			makemapentry(buf);
54568508Seric 		}
54668508Seric #endif
54768508Seric #ifdef HESIOD
54868508Seric 		else if (strcmp(maptype[i], "hesiod") == 0) &&
54968508Seric 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
55068508Seric 		{
55168508Seric 			strcpy(buf, "users.hesiod hesiod");
55268508Seric 			makemapentry(buf);
55368508Seric 		}
55468508Seric #endif
55568508Seric 	}
55668508Seric 	if (stab("users", ST_MAP, ST_FIND) == NULL)
55768508Seric 	{
55868508Seric 		strcpy(buf, "users switch -m passwd");
55968508Seric 		makemapentry(buf);
56068508Seric 	}
56168508Seric #endif
56268508Seric }
56368508Seric /*
56468508Seric **  SWITCH_MAP_FIND -- find the list of types associated with a map
56568508Seric **
56668508Seric **	This is the system-dependent interface to the service switch.
56768508Seric **
56868508Seric **	Parameters:
56968508Seric **		service -- the name of the service of interest.
57068508Seric **		maptype -- an out-array of strings containing the types
57168508Seric **			of access to use for this service.  There can
57268508Seric **			be at most MAXMAPSTACK types for a single service.
57368508Seric **		mapreturn -- an out-array of return information bitmaps
57468508Seric **			for the map.
57568508Seric **
57668508Seric **	Returns:
57768508Seric **		The number of map types filled in, or -1 for failure.
57868508Seric */
57968508Seric 
58068508Seric #ifdef SOLARIS
58168508Seric # include <nsswitch.h>
58268508Seric #endif
58368508Seric 
58468508Seric #if defined(ultrix) || defined(__osf__)
58568508Seric # include <sys/svcinfo.h>
58668508Seric #endif
58768508Seric 
58868508Seric int
58968508Seric switch_map_find(service, maptype, mapreturn)
59068508Seric 	char *service;
59168508Seric 	char *maptype[MAXMAPSTACK];
59268508Seric 	short mapreturn[MAXMAPACTIONS];
59368508Seric {
59468508Seric 	register FILE *fp;
59568508Seric 	int svcno;
59668508Seric 	static char buf[MAXLINE];
59768508Seric 
59868508Seric #ifdef SOLARIS
59968508Seric 	struct __nsw_switchconfig *nsw_conf;
60068508Seric 	enum __nsw_parse_err pserr;
60168508Seric 	struct __nsw_lookup *lk;
60268508Seric 	int nsw_rc;
60368508Seric 	static struct __nsw_lookup lkp0 =
60468508Seric 		{ "files", {1, 0, 0, 0}, NULL, NULL };
60568508Seric 	static struct __nsw_switchconfig lkp_default =
60668508Seric 		{ 0, "sendmail", 3, &lkp0 };
60768508Seric 
60868508Seric 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
60968508Seric 		lk = lkp_default.lookups;
61068508Seric 	else
61168508Seric 		lk = nsw_conf->lookups;
61268508Seric 	svcno = 0;
61368508Seric 	while (lk != NULL)
61468508Seric 	{
61568508Seric 		maptype[svcno] = lk->service_name;
61668508Seric 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
61768508Seric 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
61868508Seric 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
61968508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62068508Seric 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
62168508Seric 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
62268508Seric 		svcno++;
62368508Seric 		lk = lk->next;
62468508Seric 	}
62568508Seric 	return svcno;
62668508Seric #endif
62768508Seric 
62868508Seric #if defined(ultrix) || defined(__osf__)
62968508Seric 	struct svcinfo *svcinfo;
63068508Seric 	int svc;
63168508Seric 
63268508Seric 	svcinfo = getsvc();
63368508Seric 	if (svcinfo == NULL)
63468508Seric 		goto punt;
63568508Seric 	if (strcmp(service, "hosts") == 0)
63668508Seric 		svc = SVC_HOSTS;
63768508Seric 	else if (strcmp(service, "aliases") == 0)
63868508Seric 		svc = SVC_ALIASES;
63968508Seric 	else if (strcmp(service, "passwd") == 0)
64068508Seric 		svc = SVC_PASSWD;
64168508Seric 	else
64268508Seric 		return -1;
64368508Seric 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
64468508Seric 	{
64568508Seric 		switch (svcinfo->svcpath[svc][svcno])
64668508Seric 		{
64768508Seric 		  case SVC_LOCAL:
64868508Seric 			maptype[svcno] = "files";
64968508Seric 			break;
65068508Seric 
65168508Seric 		  case SVC_YP:
65268508Seric 			maptype[svcno] = "nis";
65368508Seric 			break;
65468508Seric 
65568508Seric 		  case SVC_BIND:
65668508Seric 			maptype[svcno] = "dns";
65768508Seric 			break;
65868508Seric 
65968508Seric #ifdef SVC_HESIOD
66068508Seric 		  case SVC_HESIOD:
66168508Seric 			maptype[svcno] = "hesiod";
66268508Seric 			break;
66368508Seric #endif
66468508Seric 
66568508Seric 		  case SVC_LAST:
66668508Seric 			return svcno;
66768508Seric 		}
66868508Seric 	}
66968508Seric 	return svcno;
67068508Seric #endif
67168508Seric 
67268508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
67368508Seric 	/*
67468508Seric 	**  Fall-back mechanism.
67568508Seric 	*/
67668508Seric 
67768508Seric 	svcno = 0;
67868508Seric 	fp = fopen(ServiceSwitchFile, "r");
67968508Seric 	if (fp != NULL)
68068508Seric 	{
68168508Seric 		while (fgets(buf, sizeof buf, fp) != NULL)
68268508Seric 		{
68368508Seric 			register char *p;
68468508Seric 
68568508Seric 			p = strpbrk(buf, "#\n");
68668508Seric 			if (p != NULL)
68768508Seric 				*p = '\0';
68868508Seric 			p = strpbrk(buf, " \t");
68968508Seric 			if (p != NULL)
69068508Seric 				*p++ = '\0';
69168508Seric 			if (strcmp(buf, service) != 0)
69268508Seric 				continue;
69368508Seric 
69468508Seric 			/* got the right service -- extract data */
69568508Seric 			do
69668508Seric 			{
69768508Seric 				while (isspace(*p))
69868508Seric 					p++;
69968508Seric 				if (*p == '\0')
70068508Seric 					break;
70168508Seric 				maptype[svcno++] = p;
70268508Seric 				p = strpbrk(p, " \t");
70368508Seric 				if (p != NULL)
70468508Seric 					*p++ = '\0';
70568508Seric 			} while (p != NULL);
70668508Seric 			break;
70768508Seric 		}
70868508Seric 		fclose(fp);
70968508Seric 		return svcno;
71068508Seric 	}
71168508Seric #endif
71268508Seric 
71368508Seric 	/* if the service file doesn't work, use an absolute fallback */
71468508Seric   punt:
71568508Seric 	if (strcmp(service, "aliases") == 0)
71668508Seric 	{
71768628Seric 		maptype[svcno++] = "files";
71868628Seric #ifdef AUTO_NIS_ALIASES
71968628Seric # ifdef NISPLUS
72068628Seric 		maptype[svcno++] = "nisplus";
72168628Seric # endif
72268628Seric # ifdef NIS
72368628Seric 		maptype[svcno++] = "nis";
72468628Seric # endif
72568628Seric #endif
72668628Seric 		return svcno;
72768508Seric 	}
72868508Seric 	if (strcmp(service, "hosts") == 0)
72968508Seric 	{
73068508Seric # if NAMED_BIND
73168508Seric 		maptype[svcno++] = "dns";
73268508Seric # else
73368508Seric #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
73468508Seric 		/* SunOS */
73568508Seric 		maptype[svcno++] = "nis";
73668508Seric #  endif
73768508Seric # endif
73868508Seric 		maptype[svcno++] = "files";
73968508Seric 		return svcno;
74068508Seric 	}
74168508Seric 	return -1;
74268508Seric }
74368508Seric /*
7449369Seric **  USERNAME -- return the user id of the logged in user.
7459369Seric **
7469369Seric **	Parameters:
7479369Seric **		none.
7489369Seric **
7499369Seric **	Returns:
7509369Seric **		The login name of the logged in user.
7519369Seric **
7529369Seric **	Side Effects:
7539369Seric **		none.
7549369Seric **
7559369Seric **	Notes:
7569369Seric **		The return value is statically allocated.
7579369Seric */
7589369Seric 
7599369Seric char *
7609369Seric username()
7619369Seric {
76217469Seric 	static char *myname = NULL;
7639369Seric 	extern char *getlogin();
76419904Smiriam 	register struct passwd *pw;
7659369Seric 
76617469Seric 	/* cache the result */
76717469Seric 	if (myname == NULL)
76817469Seric 	{
76917469Seric 		myname = getlogin();
77017469Seric 		if (myname == NULL || myname[0] == '\0')
77117469Seric 		{
77268693Seric 			pw = sm_getpwuid(RealUid);
77317469Seric 			if (pw != NULL)
77440993Sbostic 				myname = newstr(pw->pw_name);
77517469Seric 		}
77619904Smiriam 		else
77719904Smiriam 		{
77863787Seric 			uid_t uid = RealUid;
77919873Smiriam 
78040993Sbostic 			myname = newstr(myname);
78168693Seric 			if ((pw = sm_getpwnam(myname)) == NULL ||
78258736Seric 			      (uid != 0 && uid != pw->pw_uid))
78319904Smiriam 			{
78468693Seric 				pw = sm_getpwuid(uid);
78524945Seric 				if (pw != NULL)
78640993Sbostic 					myname = newstr(pw->pw_name);
78719873Smiriam 			}
78819873Smiriam 		}
78917469Seric 		if (myname == NULL || myname[0] == '\0')
79017469Seric 		{
79158151Seric 			syserr("554 Who are you?");
79217469Seric 			myname = "postmaster";
79317469Seric 		}
79417469Seric 	}
79517469Seric 
79617469Seric 	return (myname);
7979369Seric }
7989369Seric /*
7994190Seric **  TTYPATH -- Get the path of the user's tty
800294Seric **
801294Seric **	Returns the pathname of the user's tty.  Returns NULL if
802294Seric **	the user is not logged in or if s/he has write permission
803294Seric **	denied.
804294Seric **
805294Seric **	Parameters:
806294Seric **		none
807294Seric **
808294Seric **	Returns:
809294Seric **		pathname of the user's tty.
810294Seric **		NULL if not logged in or write permission denied.
811294Seric **
812294Seric **	Side Effects:
813294Seric **		none.
814294Seric **
815294Seric **	WARNING:
816294Seric **		Return value is in a local buffer.
817294Seric **
818294Seric **	Called By:
819294Seric **		savemail
820294Seric */
821294Seric 
822294Seric char *
823294Seric ttypath()
824294Seric {
825294Seric 	struct stat stbuf;
826294Seric 	register char *pathn;
827294Seric 	extern char *ttyname();
8284081Seric 	extern char *getlogin();
829294Seric 
830294Seric 	/* compute the pathname of the controlling tty */
8319369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
8329369Seric 	    (pathn = ttyname(0)) == NULL)
833294Seric 	{
834294Seric 		errno = 0;
835294Seric 		return (NULL);
836294Seric 	}
837294Seric 
838294Seric 	/* see if we have write permission */
8392967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
840294Seric 	{
841294Seric 		errno = 0;
842294Seric 		return (NULL);
843294Seric 	}
844294Seric 
845294Seric 	/* see if the user is logged in */
846294Seric 	if (getlogin() == NULL)
847294Seric 		return (NULL);
848294Seric 
849294Seric 	/* looks good */
850294Seric 	return (pathn);
851294Seric }
8522967Seric /*
8532967Seric **  CHECKCOMPAT -- check for From and To person compatible.
8542967Seric **
8552967Seric **	This routine can be supplied on a per-installation basis
8562967Seric **	to determine whether a person is allowed to send a message.
8572967Seric **	This allows restriction of certain types of internet
8582967Seric **	forwarding or registration of users.
8592967Seric **
8602967Seric **	If the hosts are found to be incompatible, an error
86157454Seric **	message should be given using "usrerr" and 0 should
8622967Seric **	be returned.
8632967Seric **
86468559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
86568559Seric **	body during the return-to-sender function; this should be done
86668559Seric **	on huge messages.  This bit may already be set by the ESMTP
86768559Seric **	protocol.
8684288Seric **
8692967Seric **	Parameters:
8702967Seric **		to -- the person being sent to.
8712967Seric **
8722967Seric **	Returns:
87357459Seric **		an exit status
8742967Seric **
8752967Seric **	Side Effects:
8762967Seric **		none (unless you include the usrerr stuff)
8772967Seric */
8782967Seric 
87968508Seric int
88055012Seric checkcompat(to, e)
8812967Seric 	register ADDRESS *to;
88255012Seric 	register ENVELOPE *e;
8832967Seric {
88412133Seric # ifdef lint
88512133Seric 	if (to == NULL)
88612133Seric 		to++;
88763834Seric # endif /* lint */
88864402Seric 
88964402Seric 	if (tTd(49, 1))
89064402Seric 		printf("checkcompat(to=%s, from=%s)\n",
89164402Seric 			to->q_paddr, e->e_from.q_paddr);
89264402Seric 
89310698Seric # ifdef EXAMPLE_CODE
89410698Seric 	/* this code is intended as an example only */
8954437Seric 	register STAB *s;
8964437Seric 
8974437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
89868508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
8999369Seric 	    to->q_mailer == s->s_mailer)
9004437Seric 	{
90158151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
90268559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
90357459Seric 		return (EX_UNAVAILABLE);
9044437Seric 	}
90556795Seric # endif /* EXAMPLE_CODE */
90657459Seric 	return (EX_OK);
9072967Seric }
9089369Seric /*
90964035Seric **  SETSIGNAL -- set a signal handler
91064035Seric **
91164035Seric **	This is essentially old BSD "signal(3)".
91264035Seric */
91364035Seric 
91464561Seric sigfunc_t
91564035Seric setsignal(sig, handler)
91664035Seric 	int sig;
91764561Seric 	sigfunc_t handler;
91864035Seric {
91964380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
92064035Seric 	return signal(sig, handler);
92164035Seric #else
92264035Seric 	struct sigaction n, o;
92364035Seric 
92464035Seric 	bzero(&n, sizeof n);
92564035Seric 	n.sa_handler = handler;
92668508Seric # ifdef SA_RESTART
92768508Seric 	n.sa_flags = SA_RESTART;
92868508Seric # endif
92964035Seric 	if (sigaction(sig, &n, &o) < 0)
93064035Seric 		return SIG_ERR;
93164035Seric 	return o.sa_handler;
93264035Seric #endif
93364035Seric }
93464035Seric /*
9359369Seric **  HOLDSIGS -- arrange to hold all signals
9369369Seric **
9379369Seric **	Parameters:
9389369Seric **		none.
9399369Seric **
9409369Seric **	Returns:
9419369Seric **		none.
9429369Seric **
9439369Seric **	Side Effects:
9449369Seric **		Arranges that signals are held.
9459369Seric */
9469369Seric 
94768508Seric void
9489369Seric holdsigs()
9499369Seric {
9509369Seric }
9519369Seric /*
9529369Seric **  RLSESIGS -- arrange to release all signals
9539369Seric **
9549369Seric **	This undoes the effect of holdsigs.
9559369Seric **
9569369Seric **	Parameters:
9579369Seric **		none.
9589369Seric **
9599369Seric **	Returns:
9609369Seric **		none.
9619369Seric **
9629369Seric **	Side Effects:
9639369Seric **		Arranges that signals are released.
9649369Seric */
9659369Seric 
96668508Seric void
9679369Seric rlsesigs()
9689369Seric {
9699369Seric }
97014872Seric /*
97164705Seric **  INIT_MD -- do machine dependent initializations
97264705Seric **
97364705Seric **	Systems that have global modes that should be set should do
97464705Seric **	them here rather than in main.
97564705Seric */
97664705Seric 
97764705Seric #ifdef _AUX_SOURCE
97864705Seric # include	<compat.h>
97964705Seric #endif
98064705Seric 
98168508Seric void
98264825Seric init_md(argc, argv)
98364825Seric 	int argc;
98464825Seric 	char **argv;
98564705Seric {
98664705Seric #ifdef _AUX_SOURCE
98764705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
98864705Seric #endif
98968508Seric 
99068508Seric #ifdef VENDOR_DEFAULT
99168508Seric 	VendorCode = VENDOR_DEFAULT;
99268508Seric #else
99368508Seric 	VendorCode = VENDOR_BERKELEY;
99468508Seric #endif
99564705Seric }
99664705Seric /*
99768508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
99868508Seric **
99968508Seric **	Called once, on startup.
100068508Seric **
100168508Seric **	Parameters:
100268508Seric **		e -- the global envelope.
100368508Seric **
100468508Seric **	Returns:
100568508Seric **		none.
100668508Seric **
100768508Seric **	Side Effects:
100868508Seric **		vendor-dependent.
100968508Seric */
101068508Seric 
101168508Seric void
101268508Seric init_vendor_macros(e)
101368508Seric 	register ENVELOPE *e;
101468508Seric {
101568508Seric }
101668508Seric /*
101714872Seric **  GETLA -- get the current load average
101814872Seric **
101914881Seric **	This code stolen from la.c.
102014881Seric **
102114872Seric **	Parameters:
102214872Seric **		none.
102314872Seric **
102414872Seric **	Returns:
102514872Seric **		The current load average as an integer.
102614872Seric **
102714872Seric **	Side Effects:
102814872Seric **		none.
102914872Seric */
103014872Seric 
103151920Seric /* try to guess what style of load average we have */
103251920Seric #define LA_ZERO		1	/* always return load average as zero */
103364360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
103451920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
103551920Seric #define LA_SUBR		4	/* call getloadavg */
103664295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
103764360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
103866301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
103914872Seric 
104063962Seric /* do guesses based on general OS type */
104151920Seric #ifndef LA_TYPE
104263962Seric # define LA_TYPE	LA_ZERO
104351920Seric #endif
104451920Seric 
104564360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
104651920Seric 
104714872Seric #include <nlist.h>
104814872Seric 
104968508Seric #ifdef IRIX64
105068508Seric # define nlist		nlist64
105168508Seric #endif
105268508Seric 
105351920Seric #ifndef LA_AVENRUN
105463962Seric # ifdef SYSTEM5
105563962Seric #  define LA_AVENRUN	"avenrun"
105663962Seric # else
105763962Seric #  define LA_AVENRUN	"_avenrun"
105863962Seric # endif
105951920Seric #endif
106051920Seric 
106151920Seric /* _PATH_UNIX should be defined in <paths.h> */
106251920Seric #ifndef _PATH_UNIX
106363962Seric # if defined(SYSTEM5)
106463962Seric #  define _PATH_UNIX	"/unix"
106563962Seric # else
106663962Seric #  define _PATH_UNIX	"/vmunix"
106763962Seric # endif
106851920Seric #endif
106951920Seric 
107014872Seric struct	nlist Nl[] =
107114872Seric {
107251920Seric 	{ LA_AVENRUN },
107314872Seric #define	X_AVENRUN	0
107414872Seric 	{ 0 },
107514872Seric };
107614872Seric 
107760583Seric #ifndef FSHIFT
107860583Seric # if defined(unixpc)
107960583Seric #  define FSHIFT	5
108060583Seric # endif
108157736Seric 
108266763Seric # if defined(__alpha) || defined(IRIX)
108360583Seric #  define FSHIFT	10
108460583Seric # endif
108568508Seric 
108668508Seric # if defined(_AIX3)
108768508Seric #  define FSHIFT	16
108868508Seric # endif
108966788Seric #endif
109057977Seric 
109166788Seric #ifndef FSHIFT
109266788Seric # define FSHIFT		8
109357736Seric #endif
109460583Seric 
109566788Seric #ifndef FSCALE
109666788Seric # define FSCALE		(1 << FSHIFT)
109751920Seric #endif
109840930Srick 
109914872Seric getla()
110014872Seric {
110114872Seric 	static int kmem = -1;
110251920Seric #if LA_TYPE == LA_INT
110324943Seric 	long avenrun[3];
110451920Seric #else
110564360Seric # if LA_TYPE == LA_SHORT
110664360Seric 	short avenrun[3];
110764360Seric # else
110851920Seric 	double avenrun[3];
110964360Seric # endif
111051920Seric #endif
111125615Seric 	extern off_t lseek();
111257736Seric 	extern int errno;
111314872Seric 
111414872Seric 	if (kmem < 0)
111514872Seric 	{
111624945Seric 		kmem = open("/dev/kmem", 0, 0);
111714872Seric 		if (kmem < 0)
111857736Seric 		{
111957736Seric 			if (tTd(3, 1))
112057736Seric 				printf("getla: open(/dev/kmem): %s\n",
112157736Seric 					errstring(errno));
112214872Seric 			return (-1);
112357736Seric 		}
112451920Seric 		(void) fcntl(kmem, F_SETFD, 1);
112568508Seric #ifdef _AIX3
112668508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
112768508Seric #else
112857736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
112968508Seric #endif
113057736Seric 		{
113157736Seric 			if (tTd(3, 1))
113257736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
113357736Seric 					errstring(errno));
113414872Seric 			return (-1);
113557736Seric 		}
113659253Seric 		if (Nl[X_AVENRUN].n_value == 0)
113759253Seric 		{
113859253Seric 			if (tTd(3, 1))
113959253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
114059253Seric 					_PATH_UNIX, LA_AVENRUN);
114159253Seric 			return (-1);
114259253Seric 		}
114368508Seric #ifdef NAMELISTMASK
114468508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
114566763Seric #endif
114614872Seric 	}
114757736Seric 	if (tTd(3, 20))
114857736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
114968508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
115023118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
115119967Seric 	{
115219967Seric 		/* thank you Ian */
115357736Seric 		if (tTd(3, 1))
115457736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
115519967Seric 		return (-1);
115619967Seric 	}
115764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
115857736Seric 	if (tTd(3, 5))
115957736Seric 	{
116057736Seric 		printf("getla: avenrun = %d", avenrun[0]);
116157736Seric 		if (tTd(3, 15))
116257736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
116357736Seric 		printf("\n");
116457736Seric 	}
116557736Seric 	if (tTd(3, 1))
116657736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116724943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116851920Seric #else
116957736Seric 	if (tTd(3, 5))
117057736Seric 	{
117157736Seric 		printf("getla: avenrun = %g", avenrun[0]);
117257736Seric 		if (tTd(3, 15))
117357736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
117457736Seric 		printf("\n");
117557736Seric 	}
117657736Seric 	if (tTd(3, 1))
117757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
117851920Seric 	return ((int) (avenrun[0] + 0.5));
117951920Seric #endif
118014872Seric }
118114872Seric 
118251773Seric #else
118351920Seric #if LA_TYPE == LA_SUBR
118451773Seric 
118564718Seric #ifdef DGUX
118664718Seric 
118764718Seric #include <sys/dg_sys_info.h>
118864718Seric 
118968508Seric int
119068508Seric getla()
119164718Seric {
119264718Seric 	struct dg_sys_info_load_info load_info;
119364718Seric 
119464718Seric 	dg_sys_info((long *)&load_info,
119564718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
119664718Seric 
119768508Seric         if (tTd(3, 1))
119868508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
119968508Seric 
120064718Seric 	return((int) (load_info.one_minute + 0.5));
120164718Seric }
120264718Seric 
120364718Seric #else
120468508Seric # ifdef __hpux
120564718Seric 
120668508Seric #  include <sys/param.h>
120768508Seric #  include <sys/pstat.h>
120868508Seric 
120968508Seric int
121051773Seric getla()
121151773Seric {
121268508Seric 	struct pst_dynamic pstd;
121368508Seric 
121468508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
121568508Seric 			     (size_t) 1 ,0) == -1)
121668508Seric 		return 0;
121768508Seric 
121868508Seric         if (tTd(3, 1))
121968508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
122068508Seric 
122168508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
122268508Seric }
122368508Seric 
122468508Seric # else
122568508Seric 
122668508Seric int
122768508Seric getla()
122868508Seric {
122951920Seric 	double avenrun[3];
123051920Seric 
123151920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
123257736Seric 	{
123357736Seric 		if (tTd(3, 1))
123457736Seric 			perror("getla: getloadavg failed:");
123551920Seric 		return (-1);
123657736Seric 	}
123757736Seric 	if (tTd(3, 1))
123857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
123951920Seric 	return ((int) (avenrun[0] + 0.5));
124051773Seric }
124151773Seric 
124268508Seric # endif /* __hpux */
124364718Seric #endif /* DGUX */
124451773Seric #else
124564295Seric #if LA_TYPE == LA_MACH
124651773Seric 
124764295Seric /*
124865173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
124964295Seric */
125064295Seric 
125165173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
125265173Seric # include <mach/mach.h>
125365173Seric #else
125465173Seric # include <mach.h>
125565173Seric #endif
125664295Seric 
125751773Seric getla()
125851773Seric {
125964295Seric 	processor_set_t default_set;
126064295Seric 	kern_return_t error;
126164295Seric 	unsigned int info_count;
126264295Seric 	struct processor_set_basic_info info;
126364295Seric 	host_t host;
126464295Seric 
126564295Seric 	error = processor_set_default(host_self(), &default_set);
126664295Seric 	if (error != KERN_SUCCESS)
126764295Seric 		return -1;
126864295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
126964295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
127064295Seric 			       &host, (processor_set_info_t)&info,
127164295Seric 			       &info_count) != KERN_SUCCESS)
127264295Seric 	{
127364295Seric 		return -1;
127464295Seric 	}
127564295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
127664295Seric }
127764295Seric 
127864295Seric 
127964295Seric #else
128066313Seric #if LA_TYPE == LA_PROCSTR
128164295Seric 
128266301Seric /*
128366301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
128466301Seric **  in a format like "0.15 0.12 0.06".
128566301Seric **
128666301Seric **	Initially intended for Linux.  This has been in the kernel
128766301Seric **	since at least 0.99.15.
128866301Seric */
128966301Seric 
129066301Seric # ifndef _PATH_LOADAVG
129166301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
129266301Seric # endif
129366301Seric 
129466301Seric int
129564295Seric getla()
129664295Seric {
129766319Seric 	double avenrun;
129866301Seric 	register int result;
129966319Seric 	FILE *fp;
130066301Seric 
130166319Seric 	fp = fopen(_PATH_LOADAVG, "r");
130266319Seric 	if (fp == NULL)
130366301Seric 	{
130466319Seric 		if (tTd(3, 1))
130566319Seric 			printf("getla: fopen(%s): %s\n",
130666319Seric 				_PATH_LOADAVG, errstring(errno));
130766319Seric 		return -1;
130866301Seric 	}
130966301Seric 	result = fscanf(fp, "%lf", &avenrun);
131066319Seric 	fclose(fp);
131166301Seric 	if (result != 1)
131266301Seric 	{
131366310Seric 		if (tTd(3, 1))
131466301Seric 			printf("getla: fscanf() = %d: %s\n",
131566301Seric 				result, errstring(errno));
131666301Seric 		return -1;
131766301Seric 	}
131866301Seric 
131957736Seric 	if (tTd(3, 1))
132066301Seric 		printf("getla(): %.2f\n", avenrun);
132166301Seric 
132266301Seric 	return ((int) (avenrun + 0.5));
132366301Seric }
132466301Seric 
132566301Seric #else
132666301Seric 
132766301Seric getla()
132866301Seric {
132966301Seric 	if (tTd(3, 1))
133057736Seric 		printf("getla: ZERO\n");
133151920Seric 	return (0);
133251773Seric }
133351773Seric 
133451773Seric #endif
133551773Seric #endif
133664295Seric #endif
133766301Seric #endif
133866045Seric 
133966045Seric 
134066045Seric /*
134166045Seric  * Copyright 1989 Massachusetts Institute of Technology
134266045Seric  *
134366045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
134466045Seric  * documentation for any purpose is hereby granted without fee, provided that
134566045Seric  * the above copyright notice appear in all copies and that both that
134666045Seric  * copyright notice and this permission notice appear in supporting
134766045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
134866045Seric  * publicity pertaining to distribution of the software without specific,
134966045Seric  * written prior permission.  M.I.T. makes no representations about the
135066045Seric  * suitability of this software for any purpose.  It is provided "as is"
135166045Seric  * without express or implied warranty.
135266045Seric  *
135366045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
135466045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
135566045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135666045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
135766045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
135866045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
135966045Seric  *
136066045Seric  * Authors:  Many and varied...
136166045Seric  */
136266045Seric 
136366045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
136466045Seric #ifndef lint
136566045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
136666045Seric #endif /* !lint */
136766045Seric 
136866179Seric #ifdef apollo
136966045Seric # undef volatile
137066045Seric #    include <apollo/base.h>
137166045Seric 
137266045Seric /* ARGSUSED */
137366045Seric int getloadavg( call_data )
137466045Seric      caddr_t	call_data;	/* pointer to (double) return value */
137566045Seric {
137666045Seric      double *avenrun = (double *) call_data;
137766045Seric      int i;
137866045Seric      status_$t      st;
137966045Seric      long loadav[3];
138066045Seric      proc1_$get_loadav(loadav, &st);
138166045Seric      *avenrun = loadav[0] / (double) (1 << 16);
138266045Seric      return(0);
138366045Seric }
138466045Seric #   endif /* apollo */
138524943Seric /*
138624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
138724943Seric **
138824943Seric **	Compares the message cost to the load average to decide.
138924943Seric **
139024943Seric **	Parameters:
139124943Seric **		pri -- the priority of the message in question.
139257438Seric **		ctime -- the message creation time.
139324943Seric **
139424943Seric **	Returns:
139524943Seric **		TRUE -- if this message should be queued up for the
139624943Seric **			time being.
139724943Seric **		FALSE -- if the load is low enough to send this message.
139824943Seric **
139924943Seric **	Side Effects:
140024943Seric **		none.
140124943Seric */
140224943Seric 
140324943Seric bool
140457438Seric shouldqueue(pri, ctime)
140524943Seric 	long pri;
140657438Seric 	time_t ctime;
140724943Seric {
140868508Seric 	bool rval;
140968508Seric 
141068508Seric 	if (tTd(3, 30))
141168508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
141251920Seric 	if (CurrentLA < QueueLA)
141368508Seric 	{
141468508Seric 		if (tTd(3, 30))
141568508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
141624943Seric 		return (FALSE);
141768508Seric 	}
141858132Seric 	if (CurrentLA >= RefuseLA)
141968508Seric 	{
142068508Seric 		if (tTd(3, 30))
142168508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
142258132Seric 		return (TRUE);
142368508Seric 	}
142468508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
142568508Seric 	if (tTd(3, 30))
142668508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
142768508Seric 	return rval;
142824943Seric }
142924943Seric /*
143053037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
143153037Seric **
143253037Seric **	Parameters:
143353037Seric **		none.
143453037Seric **
143553037Seric **	Returns:
143653037Seric **		TRUE if incoming SMTP connections should be refused
143753037Seric **			(for now).
143853037Seric **		FALSE if we should accept new work.
143953037Seric **
144053037Seric **	Side Effects:
144153037Seric **		none.
144253037Seric */
144353037Seric 
144453037Seric bool
144553037Seric refuseconnections()
144653037Seric {
144768508Seric 	extern bool enoughspace();
144868508Seric 
144959156Seric #ifdef XLA
145059156Seric 	if (!xla_smtp_ok())
145159156Seric 		return TRUE;
145259156Seric #endif
145359156Seric 
145453037Seric 	/* this is probably too simplistic */
145568508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
145653037Seric }
145753037Seric /*
145824943Seric **  SETPROCTITLE -- set process title for ps
145924943Seric **
146024943Seric **	Parameters:
146158674Seric **		fmt -- a printf style format string.
146258674Seric **		a, b, c -- possible parameters to fmt.
146324943Seric **
146424943Seric **	Returns:
146524943Seric **		none.
146624943Seric **
146724943Seric **	Side Effects:
146824943Seric **		Clobbers argv of our main procedure so ps(1) will
146924943Seric **		display the title.
147024943Seric */
147124943Seric 
147268508Seric #define SPT_NONE	0	/* don't use it at all */
147368508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
147468508Seric #define SPT_BUILTIN	2	/* use libc builtin */
147568508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
147668508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
147768508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
147868508Seric 
147968508Seric #ifndef SPT_TYPE
148068508Seric # define SPT_TYPE	SPT_REUSEARGV
148168508Seric #endif
148268508Seric 
148368508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
148468508Seric 
148568508Seric # if SPT_TYPE == SPT_PSTAT
148658689Seric #  include <sys/pstat.h>
148758689Seric # endif
148868508Seric # if SPT_TYPE == SPT_PSSTRINGS
148959732Seric #  include <machine/vmparam.h>
149059732Seric #  include <sys/exec.h>
149168508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
149268508Seric #   undef SPT_TYPE
149368508Seric #   define SPT_TYPE	SPT_REUSEARGV
149464247Seric #  endif
149559732Seric # endif
149668508Seric 
149768508Seric # if SPT_TYPE == SPT_PSSTRINGS
149868508Seric #  define SETPROC_STATIC	static
149968508Seric # else
150059732Seric #  define SETPROC_STATIC
150159732Seric # endif
150267626Seric 
150368508Seric # ifndef SPT_PADCHAR
150468508Seric #  define SPT_PADCHAR	' '
150568508Seric # endif
150658689Seric 
150768508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
150864843Seric 
150968508Seric #if SPT_TYPE != SPT_BUILTIN
151068508Seric 
151124943Seric /*VARARGS1*/
151268508Seric void
151368508Seric # ifdef __STDC__
151457642Seric setproctitle(char *fmt, ...)
151568508Seric # else
151657642Seric setproctitle(fmt, va_alist)
151724943Seric 	char *fmt;
151857642Seric 	va_dcl
151968508Seric # endif
152024943Seric {
152168508Seric # if SPT_TYPE != SPT_NONE
152224943Seric 	register char *p;
152325049Seric 	register int i;
152459732Seric 	SETPROC_STATIC char buf[MAXLINE];
152556852Seric 	VA_LOCAL_DECL
152668508Seric #  if SPT_TYPE == SPT_PSTAT
152758689Seric 	union pstun pst;
152858689Seric #  endif
152924943Seric 	extern char **Argv;
153024943Seric 	extern char *LastArgv;
153124943Seric 
153258674Seric 	p = buf;
153324943Seric 
153458674Seric 	/* print sendmail: heading for grep */
153558674Seric 	(void) strcpy(p, "sendmail: ");
153658674Seric 	p += strlen(p);
153724943Seric 
153858674Seric 	/* print the argument string */
153958674Seric 	VA_START(fmt);
154058674Seric 	(void) vsprintf(p, fmt, ap);
154156852Seric 	VA_END;
154254996Seric 
154358674Seric 	i = strlen(buf);
154458689Seric 
154568508Seric #  if SPT_TYPE == SPT_PSTAT
154658689Seric 	pst.pst_command = buf;
154758689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
154858689Seric #  else
154968508Seric #   if SPT_TYPE == SPT_PSSTRINGS
155059732Seric 	PS_STRINGS->ps_nargvstr = 1;
155159732Seric 	PS_STRINGS->ps_argvstr = buf;
155259732Seric #   else
155354996Seric 	if (i > LastArgv - Argv[0] - 2)
155425049Seric 	{
155554996Seric 		i = LastArgv - Argv[0] - 2;
155658674Seric 		buf[i] = '\0';
155725049Seric 	}
155858674Seric 	(void) strcpy(Argv[0], buf);
155954997Seric 	p = &Argv[0][i];
156024943Seric 	while (p < LastArgv)
156168508Seric 		*p++ = SPT_PADCHAR;
156268508Seric 	Argv[1] = NULL;
156368508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
156468508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
156568508Seric # endif /* SPT_TYPE != SPT_NONE */
156624943Seric }
156765982Seric 
156868508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
156925698Seric /*
157025698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
157125698Seric **
157225698Seric **	Parameters:
157368508Seric **		sig -- the signal that got us here (unused).
157425698Seric **
157525698Seric **	Returns:
157625698Seric **		none.
157725698Seric **
157825698Seric **	Side Effects:
157925698Seric **		Picks up extant zombies.
158025698Seric */
158125698Seric 
158246928Sbostic void
158368508Seric reapchild(sig)
158468508Seric 	int sig;
158525698Seric {
158664738Seric 	int olderrno = errno;
158763966Seric # ifdef HASWAITPID
158860219Seric 	auto int status;
158960560Seric 	int count;
159060560Seric 	int pid;
159160219Seric 
159260560Seric 	count = 0;
159360560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
159460560Seric 	{
159560560Seric 		if (count++ > 1000)
159660560Seric 		{
159766748Seric #ifdef LOG
159860560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
159960560Seric 				pid, status);
160066748Seric #endif
160160560Seric 			break;
160260560Seric 		}
160360560Seric 	}
160460219Seric # else
160525698Seric # ifdef WNOHANG
160625698Seric 	union wait status;
160725698Seric 
160863753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
160925698Seric 		continue;
161056795Seric # else /* WNOHANG */
161125698Seric 	auto int status;
161225698Seric 
161360219Seric 	while (wait(&status) > 0)
161425698Seric 		continue;
161556795Seric # endif /* WNOHANG */
161660219Seric # endif
161764035Seric # ifdef SYS5SIGNALS
161864035Seric 	(void) setsignal(SIGCHLD, reapchild);
161958061Seric # endif
162064738Seric 	errno = olderrno;
162125698Seric }
162255418Seric /*
162355418Seric **  UNSETENV -- remove a variable from the environment
162455418Seric **
162555418Seric **	Not needed on newer systems.
162655418Seric **
162755418Seric **	Parameters:
162855418Seric **		name -- the string name of the environment variable to be
162955418Seric **			deleted from the current environment.
163055418Seric **
163155418Seric **	Returns:
163255418Seric **		none.
163355418Seric **
163455418Seric **	Globals:
163555418Seric **		environ -- a pointer to the current environment.
163655418Seric **
163755418Seric **	Side Effects:
163855418Seric **		Modifies environ.
163955418Seric */
164055418Seric 
164163962Seric #ifndef HASUNSETENV
164255418Seric 
164355418Seric void
164455418Seric unsetenv(name)
164555418Seric 	char *name;
164655418Seric {
164755418Seric 	extern char **environ;
164855418Seric 	register char **pp;
164955418Seric 	int len = strlen(name);
165055418Seric 
165155418Seric 	for (pp = environ; *pp != NULL; pp++)
165255418Seric 	{
165355418Seric 		if (strncmp(name, *pp, len) == 0 &&
165455418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
165555418Seric 			break;
165655418Seric 	}
165755418Seric 
165855418Seric 	for (; *pp != NULL; pp++)
165955418Seric 		*pp = pp[1];
166055418Seric }
166155418Seric 
166263962Seric #endif
166356215Seric /*
166456215Seric **  GETDTABLESIZE -- return number of file descriptors
166556215Seric **
166656215Seric **	Only on non-BSD systems
166756215Seric **
166856215Seric **	Parameters:
166956215Seric **		none
167056215Seric **
167156215Seric **	Returns:
167256215Seric **		size of file descriptor table
167356215Seric **
167456215Seric **	Side Effects:
167556215Seric **		none
167656215Seric */
167756215Seric 
167863787Seric #ifdef SOLARIS
167963787Seric # include <sys/resource.h>
168063787Seric #endif
168156215Seric 
168256215Seric int
168363787Seric getdtsize()
168456215Seric {
168563787Seric #ifdef RLIMIT_NOFILE
168663787Seric 	struct rlimit rl;
168763787Seric 
168863787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
168963787Seric 		return rl.rlim_cur;
169063787Seric #endif
169163787Seric 
169264031Seric # ifdef HASGETDTABLESIZE
169364031Seric 	return getdtablesize();
169464031Seric # else
169564031Seric #  ifdef _SC_OPEN_MAX
169658689Seric 	return sysconf(_SC_OPEN_MAX);
169763787Seric #  else
169856215Seric 	return NOFILE;
169963787Seric #  endif
170058689Seric # endif
170156215Seric }
170257631Seric /*
170357631Seric **  UNAME -- get the UUCP name of this system.
170457631Seric */
170557631Seric 
170657943Seric #ifndef HASUNAME
170757631Seric 
170857631Seric int
170957631Seric uname(name)
171057631Seric 	struct utsname *name;
171157631Seric {
171257631Seric 	FILE *file;
171357631Seric 	char *n;
171457631Seric 
171557631Seric 	name->nodename[0] = '\0';
171657631Seric 
171757661Seric 	/* try /etc/whoami -- one line with the node name */
171857631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
171957631Seric 	{
172057661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
172157631Seric 		(void) fclose(file);
172257661Seric 		n = strchr(name->nodename, '\n');
172357631Seric 		if (n != NULL)
172457631Seric 			*n = '\0';
172557631Seric 		if (name->nodename[0] != '\0')
172657631Seric 			return (0);
172757631Seric 	}
172857631Seric 
172957661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
173057631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
173157631Seric 	{
173257631Seric 		char buf[MAXLINE];
173357631Seric 
173457631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
173557631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
173657631Seric 					NODE_LENGTH, name->nodename) > 0)
173757631Seric 				break;
173857631Seric 		(void) fclose(file);
173957631Seric 		if (name->nodename[0] != '\0')
174057631Seric 			return (0);
174157631Seric 	}
174257631Seric 
174357631Seric #ifdef TRUST_POPEN
174457631Seric 	/*
174557631Seric 	**  Popen is known to have security holes.
174657631Seric 	*/
174757631Seric 
174857661Seric 	/* try uuname -l to return local name */
174957631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
175057631Seric 	{
175157661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
175257631Seric 		(void) pclose(file);
175357661Seric 		n = strchr(name, '\n');
175457631Seric 		if (n != NULL)
175557631Seric 			*n = '\0';
175657661Seric 		if (name->nodename[0] != '\0')
175757631Seric 			return (0);
175857631Seric 	}
175957631Seric #endif
176057631Seric 
176157631Seric 	return (-1);
176257631Seric }
176357943Seric #endif /* HASUNAME */
176458068Seric /*
176558068Seric **  INITGROUPS -- initialize groups
176658068Seric **
176758068Seric **	Stub implementation for System V style systems
176858068Seric */
176958068Seric 
177058068Seric #ifndef HASINITGROUPS
177158068Seric 
177258068Seric initgroups(name, basegid)
177358068Seric 	char *name;
177458068Seric 	int basegid;
177558068Seric {
177658068Seric 	return 0;
177758068Seric }
177858068Seric 
177958068Seric #endif
178058082Seric /*
178159289Seric **  SETSID -- set session id (for non-POSIX systems)
178259289Seric */
178359289Seric 
178459289Seric #ifndef HASSETSID
178559289Seric 
178663753Seric pid_t
178763753Seric setsid __P ((void))
178859289Seric {
178964155Seric #ifdef TIOCNOTTY
179064155Seric 	int fd;
179164155Seric 
179266743Seric 	fd = open("/dev/tty", O_RDWR, 0);
179364155Seric 	if (fd >= 0)
179464155Seric 	{
179564155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
179664155Seric 		(void) close(fd);
179764155Seric 	}
179864155Seric #endif /* TIOCNOTTY */
179964705Seric # ifdef SYS5SETPGRP
180063753Seric 	return setpgrp();
180163753Seric # else
180264155Seric 	return setpgid(0, getpid());
180359289Seric # endif
180459289Seric }
180559289Seric 
180659289Seric #endif
180759289Seric /*
180866748Seric **  FSYNC -- dummy fsync
180966748Seric */
181066748Seric 
181166748Seric #ifdef NEEDFSYNC
181266748Seric 
181366748Seric fsync(fd)
181466748Seric 	int fd;
181566748Seric {
181666748Seric # ifdef O_SYNC
181766748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
181866748Seric # else
181966748Seric 	/* nothing we can do */
182066748Seric 	return 0;
182166748Seric # endif
182266748Seric }
182366748Seric 
182466748Seric #endif
182566748Seric /*
182664718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
182764718Seric **
182864718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
182968508Seric **	instead of a long.  This patches things.  Only needed on versions
183068508Seric **	prior to 5.4.3.
183164718Seric */
183264718Seric 
183368508Seric #ifdef DGUX_5_4_2
183464718Seric 
183564718Seric #undef inet_addr
183664718Seric 
183764718Seric long
183864718Seric dgux_inet_addr(host)
183964718Seric 	char *host;
184064718Seric {
184164718Seric 	struct in_addr haddr;
184264718Seric 
184364718Seric 	haddr = inet_addr(host);
184464718Seric 	return haddr.s_addr;
184564718Seric }
184664718Seric 
184764718Seric #endif
184864718Seric /*
184963969Seric **  GETOPT -- for old systems or systems with bogus implementations
185063969Seric */
185163969Seric 
185263969Seric #ifdef NEEDGETOPT
185363969Seric 
185463969Seric /*
185563969Seric  * Copyright (c) 1985 Regents of the University of California.
185663969Seric  * All rights reserved.  The Berkeley software License Agreement
185763969Seric  * specifies the terms and conditions for redistribution.
185863969Seric  */
185963969Seric 
186063969Seric 
186163969Seric /*
186263969Seric ** this version hacked to add `atend' flag to allow state machine
186363969Seric ** to reset if invoked by the program to scan args for a 2nd time
186463969Seric */
186563969Seric 
186663969Seric #if defined(LIBC_SCCS) && !defined(lint)
186763969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
186864072Seric #endif /* LIBC_SCCS and not lint */
186963969Seric 
187063969Seric #include <stdio.h>
187163969Seric 
187263969Seric /*
187363969Seric  * get option letter from argument vector
187463969Seric  */
187565352Seric #ifdef _CONVEX_SOURCE
187668508Seric extern int	optind, opterr, optopt;
187768508Seric extern char	*optarg;
187865352Seric #else
187965352Seric int	opterr = 1;		/* if error message should be printed */
188065352Seric int	optind = 1;		/* index into parent argv vector */
188168470Seric int	optopt = 0;		/* character checked for validity */
188268470Seric char	*optarg = NULL;		/* argument associated with option */
188368508Seric #endif
188463969Seric 
188563969Seric #define BADCH	(int)'?'
188663969Seric #define EMSG	""
188763969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
188863969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
188963969Seric 
189063969Seric getopt(nargc,nargv,ostr)
189165160Seric 	int		nargc;
189265160Seric 	char *const	*nargv;
189365160Seric 	const char	*ostr;
189463969Seric {
189563969Seric 	static char	*place = EMSG;	/* option letter processing */
189663969Seric 	static char	atend = 0;
189763969Seric 	register char	*oli;		/* option letter list index */
189863969Seric 
189963969Seric 	if (atend) {
190063969Seric 		atend = 0;
190163969Seric 		place = EMSG;
190263969Seric 	}
190363969Seric 	if(!*place) {			/* update scanning pointer */
190464136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
190563969Seric 			atend++;
190663969Seric 			return(EOF);
190763969Seric 		}
190863969Seric 		if (*place == '-') {	/* found "--" */
190963969Seric 			++optind;
191063969Seric 			atend++;
191163969Seric 			return(EOF);
191263969Seric 		}
191363969Seric 	}				/* option letter okay? */
191464125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
191564136Seric 		if (!*place) ++optind;
191663969Seric 		tell(": illegal option -- ");
191763969Seric 	}
191863969Seric 	if (*++oli != ':') {		/* don't need argument */
191963969Seric 		optarg = NULL;
192063969Seric 		if (!*place) ++optind;
192163969Seric 	}
192263969Seric 	else {				/* need an argument */
192363969Seric 		if (*place) optarg = place;	/* no white space */
192463969Seric 		else if (nargc <= ++optind) {	/* no arg */
192563969Seric 			place = EMSG;
192663969Seric 			tell(": option requires an argument -- ");
192763969Seric 		}
192863969Seric 	 	else optarg = nargv[optind];	/* white space */
192963969Seric 		place = EMSG;
193063969Seric 		++optind;
193163969Seric 	}
193263969Seric 	return(optopt);			/* dump back option letter */
193363969Seric }
193463969Seric 
193563969Seric #endif
193663969Seric /*
193763969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
193863969Seric */
193963969Seric 
194063969Seric #ifdef NEEDVPRINTF
194163969Seric 
194263969Seric #define MAXARG	16
194363969Seric 
194463969Seric vfprintf(fp, fmt, ap)
194563969Seric 	FILE *	fp;
194663969Seric 	char *	fmt;
194763969Seric 	char **	ap;
194863969Seric {
194963969Seric 	char *	bp[MAXARG];
195063969Seric 	int	i = 0;
195163969Seric 
195263969Seric 	while (*ap && i < MAXARG)
195363969Seric 		bp[i++] = *ap++;
195463969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
195563969Seric 			 bp[4], bp[5], bp[6], bp[7],
195663969Seric 			 bp[8], bp[9], bp[10], bp[11],
195763969Seric 			 bp[12], bp[13], bp[14], bp[15]);
195863969Seric }
195963969Seric 
196063969Seric vsprintf(s, fmt, ap)
196163969Seric 	char *	s;
196263969Seric 	char *	fmt;
196363969Seric 	char **	ap;
196463969Seric {
196563969Seric 	char *	bp[MAXARG];
196663969Seric 	int	i = 0;
196763969Seric 
196863969Seric 	while (*ap && i < MAXARG)
196963969Seric 		bp[i++] = *ap++;
197063969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
197163969Seric 			bp[4], bp[5], bp[6], bp[7],
197263969Seric 			bp[8], bp[9], bp[10], bp[11],
197363969Seric 			bp[12], bp[13], bp[14], bp[15]);
197463969Seric }
197563969Seric 
197663969Seric #endif
197763969Seric /*
197865220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
197965208Seric **
198065211Seric **	Parameters:
198165211Seric **		shell -- the user's shell from /etc/passwd
198265211Seric **
198365211Seric **	Returns:
198465211Seric **		TRUE -- if it is ok to use this for unrestricted access.
198565211Seric **		FALSE -- if the shell is restricted.
198665208Seric */
198765208Seric 
198865587Seric #if !HASGETUSERSHELL
198965587Seric 
199065587Seric # ifndef _PATH_SHELLS
199165587Seric #  define _PATH_SHELLS	"/etc/shells"
199265587Seric # endif
199365587Seric 
199465587Seric char	*DefaultUserShells[] =
199565587Seric {
199668508Seric 	"/bin/sh",		/* standard shell */
199765587Seric 	"/usr/bin/sh",
199868508Seric 	"/bin/csh",		/* C shell */
199965587Seric 	"/usr/bin/csh",
200065587Seric #ifdef __hpux
200168508Seric 	"/bin/rsh",		/* restricted Bourne shell */
200268508Seric 	"/bin/ksh",		/* Korn shell */
200368508Seric 	"/bin/rksh",		/* restricted Korn shell */
200465587Seric 	"/bin/pam",
200568508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
200665587Seric 	"/bin/posix/sh",
200765211Seric #endif
200868508Seric #ifdef _AIX3
200968508Seric 	"/bin/ksh",		/* Korn shell */
201068508Seric 	"/usr/bin/ksh",
201168508Seric 	"/bin/tsh",		/* trusted shell */
201268508Seric 	"/usr/bin/tsh",
201368508Seric 	"/bin/bsh",		/* Bourne shell */
201468508Seric 	"/usr/bin/bsh",
201568508Seric #endif
201665587Seric 	NULL
201765587Seric };
201865587Seric 
201965208Seric #endif
202065208Seric 
202165747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
202265747Seric 
202365211Seric bool
202465211Seric usershellok(shell)
202565211Seric 	char *shell;
202665208Seric {
202765211Seric #if HASGETUSERSHELL
202865211Seric 	register char *p;
202965211Seric 	extern char *getusershell();
203065208Seric 
203168457Seric 	if (shell == NULL || shell[0] == '\0')
203268457Seric 		return TRUE;
203368457Seric 
203465211Seric 	setusershell();
203565211Seric 	while ((p = getusershell()) != NULL)
203665747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
203765211Seric 			break;
203865211Seric 	endusershell();
203965211Seric 	return p != NULL;
204065211Seric #else
204165211Seric 	register FILE *shellf;
204265211Seric 	char buf[MAXLINE];
204365208Seric 
204468457Seric 	if (shell == NULL || shell[0] == '\0')
204568457Seric 		return TRUE;
204668457Seric 
204765211Seric 	shellf = fopen(_PATH_SHELLS, "r");
204865211Seric 	if (shellf == NULL)
204965211Seric 	{
205065211Seric 		/* no /etc/shells; see if it is one of the std shells */
205165587Seric 		char **d;
205265587Seric 
205365587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
205465587Seric 		{
205565587Seric 			if (strcmp(shell, *d) == 0)
205665587Seric 				return TRUE;
205765587Seric 		}
205865587Seric 		return FALSE;
205965211Seric 	}
206065208Seric 
206165211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
206265211Seric 	{
206365211Seric 		register char *p, *q;
206465208Seric 
206565211Seric 		p = buf;
206665211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
206765211Seric 			p++;
206865211Seric 		if (*p == '#' || *p == '\0')
206965208Seric 			continue;
207065211Seric 		q = p;
207165211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
207265211Seric 			p++;
207365211Seric 		*p = '\0';
207465747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
207565211Seric 		{
207665211Seric 			fclose(shellf);
207765211Seric 			return TRUE;
207865211Seric 		}
207965208Seric 	}
208065211Seric 	fclose(shellf);
208165211Seric 	return FALSE;
208265211Seric #endif
208365208Seric }
208465208Seric /*
208564311Seric **  FREESPACE -- see how much free space is on the queue filesystem
208658082Seric **
208758082Seric **	Only implemented if you have statfs.
208858082Seric **
208958082Seric **	Parameters:
209064311Seric **		dir -- the directory in question.
209164311Seric **		bsize -- a variable into which the filesystem
209264311Seric **			block size is stored.
209358082Seric **
209458082Seric **	Returns:
209564311Seric **		The number of bytes free on the queue filesystem.
209664311Seric **		-1 if the statfs call fails.
209764311Seric **
209864311Seric **	Side effects:
209964311Seric **		Puts the filesystem block size into bsize.
210058082Seric */
210158082Seric 
210265749Seric /* statfs types */
210365749Seric #define SFS_NONE	0	/* no statfs implementation */
210465749Seric #define SFS_USTAT	1	/* use ustat */
210565749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
210665749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
210765749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
210865749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
210966752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
211065749Seric 
211165749Seric #ifndef SFS_TYPE
211265749Seric # define SFS_TYPE	SFS_NONE
211358157Seric #endif
211458157Seric 
211565749Seric #if SFS_TYPE == SFS_USTAT
211658157Seric # include <ustat.h>
211758157Seric #endif
211865749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
211965749Seric # include <sys/statfs.h>
212058082Seric #endif
212165749Seric #if SFS_TYPE == SFS_VFS
212265749Seric # include <sys/vfs.h>
212365749Seric #endif
212465749Seric #if SFS_TYPE == SFS_MOUNT
212565749Seric # include <sys/mount.h>
212665749Seric #endif
212766752Seric #if SFS_TYPE == SFS_STATVFS
212866752Seric # include <sys/statvfs.h>
212966752Seric #endif
213058082Seric 
213164311Seric long
213264311Seric freespace(dir, bsize)
213364311Seric 	char *dir;
213464311Seric 	long *bsize;
213558082Seric {
213665749Seric #if SFS_TYPE != SFS_NONE
213765749Seric # if SFS_TYPE == SFS_USTAT
213858153Seric 	struct ustat fs;
213958153Seric 	struct stat statbuf;
214058366Seric #  define FSBLOCKSIZE	DEV_BSIZE
214168695Seric #  define SFS_BAVAIL	f_tfree
214258157Seric # else
214358157Seric #  if defined(ultrix)
214458157Seric 	struct fs_data fs;
214568695Seric #   define SFS_BAVAIL	fd_bfreen
214668508Seric #   define FSBLOCKSIZE	1024L
214758153Seric #  else
214866752Seric #   if SFS_TYPE == SFS_STATVFS
214966752Seric 	struct statvfs fs;
215068508Seric #    define FSBLOCKSIZE	fs.f_frsize
215166752Seric #   else
215258082Seric 	struct statfs fs;
215366752Seric #    define FSBLOCKSIZE	fs.f_bsize
215463987Seric #   endif
215558153Seric #  endif
215658133Seric # endif
215768695Seric # ifndef SFS_BAVAIL
215868695Seric #  define SFS_BAVAIL f_bavail
215968508Seric # endif
216058082Seric 
216165749Seric # if SFS_TYPE == SFS_USTAT
216264311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
216358157Seric # else
216465749Seric #  if SFS_TYPE == SFS_4ARGS
216564311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
216658157Seric #  else
216768508Seric #   if SFS_TYPE == SFS_STATVFS
216868508Seric 	if (statvfs(dir, &fs) == 0)
216968508Seric #   else
217068508Seric #    if defined(ultrix)
217168499Seric 	if (statfs(dir, &fs) > 0)
217268508Seric #    else
217364311Seric 	if (statfs(dir, &fs) == 0)
217468508Seric #    endif
217558153Seric #   endif
217658133Seric #  endif
217758133Seric # endif
217858133Seric 	{
217964311Seric 		if (bsize != NULL)
218064311Seric 			*bsize = FSBLOCKSIZE;
218168695Seric 		return (fs.SFS_BAVAIL);
218264311Seric 	}
218364337Seric #endif
218464311Seric 	return (-1);
218564311Seric }
218664311Seric /*
218764311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
218864311Seric **
218964311Seric **	Only implemented if you have statfs.
219064311Seric **
219164311Seric **	Parameters:
219264311Seric **		msize -- the size to check against.  If zero, we don't yet
219364311Seric **		know how big the message will be, so just check for
219464311Seric **		a "reasonable" amount.
219564311Seric **
219664311Seric **	Returns:
219764311Seric **		TRUE if there is enough space.
219864311Seric **		FALSE otherwise.
219964311Seric */
220064311Seric 
220164311Seric bool
220264311Seric enoughspace(msize)
220364311Seric 	long msize;
220464311Seric {
220564311Seric 	long bfree, bsize;
220664311Seric 
220764311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
220864311Seric 	{
220958133Seric 		if (tTd(4, 80))
221064311Seric 			printf("enoughspace: no threshold\n");
221164311Seric 		return TRUE;
221264311Seric 	}
221364311Seric 
221464311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
221564311Seric 	{
221664311Seric 		if (tTd(4, 80))
221758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
221864311Seric 				bfree, msize);
221958333Seric 
222058333Seric 		/* convert msize to block count */
222164311Seric 		msize = msize / bsize + 1;
222258333Seric 		if (MinBlocksFree >= 0)
222358333Seric 			msize += MinBlocksFree;
222458333Seric 
222564311Seric 		if (bfree < msize)
222658090Seric 		{
222758090Seric #ifdef LOG
222858090Seric 			if (LogLevel > 0)
222964254Seric 				syslog(LOG_ALERT,
223064254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
223168508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
223268508Seric 					bfree,
223368508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
223468508Seric 					msize, QueueDir);
223558090Seric #endif
223658082Seric 			return FALSE;
223758090Seric 		}
223858082Seric 	}
223958082Seric 	else if (tTd(4, 80))
224058333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
224158333Seric 			MinBlocksFree, msize, errstring(errno));
224258082Seric 	return TRUE;
224358082Seric }
224458542Seric /*
224558542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
224658542Seric **
224758542Seric **	This looks at an errno value and tells if this is likely to
224858542Seric **	go away if retried later.
224958542Seric **
225058542Seric **	Parameters:
225158542Seric **		err -- the errno code to classify.
225258542Seric **
225358542Seric **	Returns:
225458542Seric **		TRUE if this is probably transient.
225558542Seric **		FALSE otherwise.
225658542Seric */
225758542Seric 
225858542Seric bool
225958542Seric transienterror(err)
226058542Seric 	int err;
226158542Seric {
226258542Seric 	switch (err)
226358542Seric 	{
226458542Seric 	  case EIO:			/* I/O error */
226558542Seric 	  case ENXIO:			/* Device not configured */
226658542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
226758542Seric 	  case ENOMEM:			/* Cannot allocate memory */
226858542Seric 	  case ENODEV:			/* Operation not supported by device */
226958542Seric 	  case ENFILE:			/* Too many open files in system */
227058542Seric 	  case EMFILE:			/* Too many open files */
227158542Seric 	  case ENOSPC:			/* No space left on device */
227258542Seric #ifdef ETIMEDOUT
227358542Seric 	  case ETIMEDOUT:		/* Connection timed out */
227458542Seric #endif
227558542Seric #ifdef ESTALE
227658542Seric 	  case ESTALE:			/* Stale NFS file handle */
227758542Seric #endif
227858542Seric #ifdef ENETDOWN
227958542Seric 	  case ENETDOWN:		/* Network is down */
228058542Seric #endif
228158542Seric #ifdef ENETUNREACH
228258542Seric 	  case ENETUNREACH:		/* Network is unreachable */
228358542Seric #endif
228458542Seric #ifdef ENETRESET
228558542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
228658542Seric #endif
228758542Seric #ifdef ECONNABORTED
228858542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
228958542Seric #endif
229058542Seric #ifdef ECONNRESET
229158542Seric 	  case ECONNRESET:		/* Connection reset by peer */
229258542Seric #endif
229358542Seric #ifdef ENOBUFS
229458542Seric 	  case ENOBUFS:			/* No buffer space available */
229558542Seric #endif
229658542Seric #ifdef ESHUTDOWN
229758542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
229858542Seric #endif
229958542Seric #ifdef ECONNREFUSED
230058542Seric 	  case ECONNREFUSED:		/* Connection refused */
230158542Seric #endif
230258542Seric #ifdef EHOSTDOWN
230358542Seric 	  case EHOSTDOWN:		/* Host is down */
230458542Seric #endif
230558542Seric #ifdef EHOSTUNREACH
230658542Seric 	  case EHOSTUNREACH:		/* No route to host */
230758542Seric #endif
230858542Seric #ifdef EDQUOT
230958542Seric 	  case EDQUOT:			/* Disc quota exceeded */
231058542Seric #endif
231158542Seric #ifdef EPROCLIM
231258542Seric 	  case EPROCLIM:		/* Too many processes */
231358542Seric #endif
231458542Seric #ifdef EUSERS
231558542Seric 	  case EUSERS:			/* Too many users */
231658542Seric #endif
231758542Seric #ifdef EDEADLK
231858542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
231958542Seric #endif
232058542Seric #ifdef EISCONN
232158542Seric 	  case EISCONN:			/* Socket already connected */
232258542Seric #endif
232358542Seric #ifdef EINPROGRESS
232458542Seric 	  case EINPROGRESS:		/* Operation now in progress */
232558542Seric #endif
232658542Seric #ifdef EALREADY
232758542Seric 	  case EALREADY:		/* Operation already in progress */
232858542Seric #endif
232958542Seric #ifdef EADDRINUSE
233058542Seric 	  case EADDRINUSE:		/* Address already in use */
233158542Seric #endif
233258542Seric #ifdef EADDRNOTAVAIL
233358542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
233458542Seric #endif
233566018Seric #ifdef ETXTBSY
233666017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
233766017Seric #endif
233863834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
233958542Seric 	  case ENOSR:			/* Out of streams resources */
234058542Seric #endif
234158542Seric 		return TRUE;
234258542Seric 	}
234358542Seric 
234458542Seric 	/* nope, must be permanent */
234558542Seric 	return FALSE;
234658542Seric }
234758689Seric /*
234864035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
234958689Seric **
235058689Seric **	Parameters:
235158689Seric **		fd -- the file descriptor of the file.
235258689Seric **		filename -- the file name (for error messages).
235364335Seric **		ext -- the filename extension.
235458689Seric **		type -- type of the lock.  Bits can be:
235558689Seric **			LOCK_EX -- exclusive lock.
235658689Seric **			LOCK_NB -- non-blocking.
235758689Seric **
235858689Seric **	Returns:
235958689Seric **		TRUE if the lock was acquired.
236058689Seric **		FALSE otherwise.
236158689Seric */
236258689Seric 
236358689Seric bool
236464335Seric lockfile(fd, filename, ext, type)
236558689Seric 	int fd;
236658689Seric 	char *filename;
236764335Seric 	char *ext;
236858689Seric 	int type;
236958689Seric {
237065830Seric # if !HASFLOCK
237158689Seric 	int action;
237258689Seric 	struct flock lfd;
237364335Seric 
237464335Seric 	if (ext == NULL)
237564335Seric 		ext = "";
237664035Seric 
237763983Seric 	bzero(&lfd, sizeof lfd);
237859447Seric 	if (bitset(LOCK_UN, type))
237959447Seric 		lfd.l_type = F_UNLCK;
238059447Seric 	else if (bitset(LOCK_EX, type))
238158689Seric 		lfd.l_type = F_WRLCK;
238258689Seric 	else
238358689Seric 		lfd.l_type = F_RDLCK;
238458689Seric 
238558689Seric 	if (bitset(LOCK_NB, type))
238658689Seric 		action = F_SETLK;
238758689Seric 	else
238858689Seric 		action = F_SETLKW;
238958689Seric 
239064264Seric 	if (tTd(55, 60))
239164335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
239264335Seric 			filename, ext, action, lfd.l_type);
239364264Seric 
239458689Seric 	if (fcntl(fd, action, &lfd) >= 0)
239564264Seric 	{
239664264Seric 		if (tTd(55, 60))
239764264Seric 			printf("SUCCESS\n");
239858689Seric 		return TRUE;
239964264Seric 	}
240058689Seric 
240164264Seric 	if (tTd(55, 60))
240264264Seric 		printf("(%s) ", errstring(errno));
240364264Seric 
240464136Seric 	/*
240564136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
240664136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
240764136Seric 	**  as type "tmp" (that is, served from swap space), the
240864136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
240964136Seric 	**  Since this is fairly common during testing, we will assume
241064136Seric 	**  that this indicates that the lock is successfully grabbed.
241164136Seric 	*/
241264136Seric 
241364136Seric 	if (errno == EINVAL)
241464264Seric 	{
241564264Seric 		if (tTd(55, 60))
241664264Seric 			printf("SUCCESS\n");
241764136Seric 		return TRUE;
241864264Seric 	}
241964136Seric 
242058689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
242164378Seric 	{
242264378Seric 		int omode = -1;
242364378Seric #  ifdef F_GETFL
242464378Seric 		int oerrno = errno;
242564378Seric 
242664378Seric 		(void) fcntl(fd, F_GETFL, &omode);
242764378Seric 		errno = oerrno;
242864378Seric #  endif
242964378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
243064378Seric 			filename, ext, fd, type, omode, geteuid());
243164378Seric 	}
243258689Seric # else
243364335Seric 	if (ext == NULL)
243464335Seric 		ext = "";
243564335Seric 
243664264Seric 	if (tTd(55, 60))
243764335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
243864264Seric 
243958689Seric 	if (flock(fd, type) >= 0)
244064264Seric 	{
244164264Seric 		if (tTd(55, 60))
244264264Seric 			printf("SUCCESS\n");
244358689Seric 		return TRUE;
244464264Seric 	}
244558689Seric 
244664264Seric 	if (tTd(55, 60))
244764264Seric 		printf("(%s) ", errstring(errno));
244864264Seric 
244958689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
245064378Seric 	{
245164378Seric 		int omode = -1;
245264378Seric #  ifdef F_GETFL
245364378Seric 		int oerrno = errno;
245464378Seric 
245564378Seric 		(void) fcntl(fd, F_GETFL, &omode);
245664378Seric 		errno = oerrno;
245764378Seric #  endif
245864378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
245964378Seric 			filename, ext, fd, type, omode, geteuid());
246064386Seric 	}
246158689Seric # endif
246264264Seric 	if (tTd(55, 60))
246364264Seric 		printf("FAIL\n");
246458689Seric 	return FALSE;
246558689Seric }
246664556Seric /*
246765948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
246865948Seric **
246965948Seric **	Parameters:
247065948Seric **		fd -- the file descriptor to check.
247165948Seric **
247265948Seric **	Returns:
247365948Seric **		TRUE -- if only root can chown the file to an arbitrary
247465948Seric **			user.
247565948Seric **		FALSE -- if an arbitrary user can give away a file.
247665948Seric */
247765948Seric 
247865948Seric bool
247965948Seric chownsafe(fd)
248065948Seric 	int fd;
248165948Seric {
248265950Seric #ifdef __hpux
248365948Seric 	char *s;
248465948Seric 	int tfd;
248565948Seric 	uid_t o_uid, o_euid;
248665948Seric 	gid_t o_gid, o_egid;
248765948Seric 	bool rval;
248865948Seric 	struct stat stbuf;
248965948Seric 
249065948Seric 	o_uid = getuid();
249165948Seric 	o_euid = geteuid();
249265948Seric 	o_gid = getgid();
249365948Seric 	o_egid = getegid();
249465959Seric 	fstat(fd, &stbuf);
249565948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
249665948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
249765948Seric 	s = tmpnam(NULL);
249865948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
249965974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
250065948Seric 	close(tfd);
250165948Seric 	unlink(s);
250265948Seric 	setreuid(o_uid, o_euid);
250365948Seric 	setresgid(o_gid, o_egid, -1);
250465948Seric 	return rval;
250565948Seric #else
250666088Seric # ifdef _POSIX_CHOWN_RESTRICTED
250766088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
250866088Seric 	return FALSE;
250966088Seric #  else
251066088Seric 	return TRUE;
251166088Seric #  endif
251266088Seric # else
251366088Seric #  ifdef _PC_CHOWN_RESTRICTED
251468508Seric 	int rval;
251568508Seric 
251668508Seric 	/*
251768508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
251868508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
251968508Seric 	**  the call.  This heuristic checks for that.
252068508Seric 	*/
252168508Seric 
252268508Seric 	errno = 0;
252368508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
252468508Seric 	if (errno == 0)
252568508Seric 		return rval > 0;
252668508Seric #  endif
252768508Seric #  ifdef BSD
252868508Seric 	return TRUE;
252968499Seric #  else
253065948Seric 	return FALSE;
253165948Seric #  endif
253265948Seric # endif
253365948Seric #endif
253465948Seric }
253565948Seric /*
253668508Seric **  RESETLIMITS -- reset system controlled resource limits
253768508Seric **
253868508Seric **	This is to avoid denial-of-service attacks
253968508Seric **
254068508Seric **	Parameters:
254168508Seric **		none
254268508Seric **
254368508Seric **	Returns:
254468508Seric **		none
254568508Seric */
254668508Seric 
254768508Seric #if HASSETRLIMIT
254868508Seric # include <sys/resource.h>
254968508Seric #endif
255068508Seric 
255168508Seric void
255268508Seric resetlimits()
255368508Seric {
255468508Seric #if HASSETRLIMIT
255568508Seric 	struct rlimit lim;
255668508Seric 
255768508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
255868508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
255968508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
256068508Seric #else
256168508Seric # if HASULIMIT
256268508Seric 	(void) ulimit(2, 0x3fffff);
256368508Seric # endif
256468508Seric #endif
256568508Seric }
256668508Seric /*
256764556Seric **  GETCFNAME -- return the name of the .cf file.
256864556Seric **
256964556Seric **	Some systems (e.g., NeXT) determine this dynamically.
257064556Seric */
257164556Seric 
257264556Seric char *
257364556Seric getcfname()
257464556Seric {
257564556Seric 	if (ConfFile != NULL)
257664556Seric 		return ConfFile;
257765214Seric #ifdef NETINFO
257865214Seric 	{
257965214Seric 		extern char *ni_propval();
258065214Seric 		char *cflocation;
258165214Seric 
258268508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
258368508Seric 					"sendmail.cf", '\0');
258465214Seric 		if (cflocation != NULL)
258565214Seric 			return cflocation;
258665214Seric 	}
258765214Seric #endif
258864556Seric 	return _PATH_SENDMAILCF;
258964556Seric }
259064718Seric /*
259164718Seric **  SETVENDOR -- process vendor code from V configuration line
259264718Seric **
259364718Seric **	Parameters:
259464718Seric **		vendor -- string representation of vendor.
259564718Seric **
259664718Seric **	Returns:
259764718Seric **		TRUE -- if ok.
259864718Seric **		FALSE -- if vendor code could not be processed.
259964926Seric **
260064926Seric **	Side Effects:
260164926Seric **		It is reasonable to set mode flags here to tweak
260264926Seric **		processing in other parts of the code if necessary.
260364926Seric **		For example, if you are a vendor that uses $%y to
260464926Seric **		indicate YP lookups, you could enable that here.
260564718Seric */
260664718Seric 
260764718Seric bool
260864718Seric setvendor(vendor)
260964718Seric 	char *vendor;
261064718Seric {
261164926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
261268508Seric 	{
261368508Seric 		VendorCode = VENDOR_BERKELEY;
261464926Seric 		return TRUE;
261568508Seric 	}
261664926Seric 
261764926Seric 	/* add vendor extensions here */
261864926Seric 
261968508Seric #ifdef SUN_EXTENSIONS
262068508Seric 	if (strcasecmp(vendor, "Sun") == 0)
262168508Seric 	{
262268508Seric 		VendorCode = VENDOR_SUN;
262368508Seric 		return TRUE;
262468508Seric 	}
262568508Seric #endif
262668508Seric 
262764926Seric 	return FALSE;
262864718Seric }
262964816Seric /*
263064816Seric **  STRTOL -- convert string to long integer
263164816Seric **
263264816Seric **	For systems that don't have it in the C library.
263366161Seric **
263466161Seric **	This is taken verbatim from the 4.4-Lite C library.
263564816Seric */
263664816Seric 
263764816Seric #ifdef NEEDSTRTOL
263864816Seric 
263966161Seric #if defined(LIBC_SCCS) && !defined(lint)
264066161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
264166161Seric #endif /* LIBC_SCCS and not lint */
264266161Seric 
264366161Seric #include <limits.h>
264466161Seric 
264566161Seric /*
264666161Seric  * Convert a string to a long integer.
264766161Seric  *
264866161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
264966161Seric  * alphabets and digits are each contiguous.
265066161Seric  */
265166161Seric 
265264816Seric long
265366161Seric strtol(nptr, endptr, base)
265466161Seric 	const char *nptr;
265566161Seric 	char **endptr;
265666161Seric 	register int base;
265764816Seric {
265866161Seric 	register const char *s = nptr;
265966161Seric 	register unsigned long acc;
266066161Seric 	register int c;
266166161Seric 	register unsigned long cutoff;
266266161Seric 	register int neg = 0, any, cutlim;
266364816Seric 
266466161Seric 	/*
266566161Seric 	 * Skip white space and pick up leading +/- sign if any.
266666161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
266766161Seric 	 * assume decimal; if base is already 16, allow 0x.
266866161Seric 	 */
266966161Seric 	do {
267066161Seric 		c = *s++;
267166161Seric 	} while (isspace(c));
267266161Seric 	if (c == '-') {
267366161Seric 		neg = 1;
267466161Seric 		c = *s++;
267566161Seric 	} else if (c == '+')
267666161Seric 		c = *s++;
267766161Seric 	if ((base == 0 || base == 16) &&
267866161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
267966161Seric 		c = s[1];
268066161Seric 		s += 2;
268166161Seric 		base = 16;
268266161Seric 	}
268366161Seric 	if (base == 0)
268466161Seric 		base = c == '0' ? 8 : 10;
268564816Seric 
268666161Seric 	/*
268766161Seric 	 * Compute the cutoff value between legal numbers and illegal
268866161Seric 	 * numbers.  That is the largest legal value, divided by the
268966161Seric 	 * base.  An input number that is greater than this value, if
269066161Seric 	 * followed by a legal input character, is too big.  One that
269166161Seric 	 * is equal to this value may be valid or not; the limit
269266161Seric 	 * between valid and invalid numbers is then based on the last
269366161Seric 	 * digit.  For instance, if the range for longs is
269466161Seric 	 * [-2147483648..2147483647] and the input base is 10,
269566161Seric 	 * cutoff will be set to 214748364 and cutlim to either
269666161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
269766161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
269866161Seric 	 * the number is too big, and we will return a range error.
269966161Seric 	 *
270066161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
270166161Seric 	 * overflow.
270266161Seric 	 */
270366161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
270466161Seric 	cutlim = cutoff % (unsigned long)base;
270566161Seric 	cutoff /= (unsigned long)base;
270666161Seric 	for (acc = 0, any = 0;; c = *s++) {
270766161Seric 		if (isdigit(c))
270866161Seric 			c -= '0';
270966161Seric 		else if (isalpha(c))
271066161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
271166161Seric 		else
271266161Seric 			break;
271366161Seric 		if (c >= base)
271466161Seric 			break;
271566161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
271666161Seric 			any = -1;
271766161Seric 		else {
271866161Seric 			any = 1;
271966161Seric 			acc *= base;
272066161Seric 			acc += c;
272164816Seric 		}
272264816Seric 	}
272366161Seric 	if (any < 0) {
272466161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
272566161Seric 		errno = ERANGE;
272666161Seric 	} else if (neg)
272766161Seric 		acc = -acc;
272866161Seric 	if (endptr != 0)
272966161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
273066161Seric 	return (acc);
273164816Seric }
273264816Seric 
273364816Seric #endif
273464841Seric /*
273568693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
273664841Seric **
273768693Seric **	Some operating systems have wierd problems with the gethostbyXXX
273868693Seric **	routines.  For example, Solaris versions at least through 2.3
273968693Seric **	don't properly deliver a canonical h_name field.  This tries to
274068693Seric **	work around these problems.
274164841Seric */
274264841Seric 
274364841Seric struct hostent *
274468693Seric sm_gethostbyname(name)
274565094Seric 	const char *name;
274664841Seric {
274768693Seric #if defined(SOLARIS) && SOLARIS < 204
274868693Seric 	extern int h_errno;
274968693Seric 
275068508Seric # if SOLARIS == 203
275164841Seric 	static struct hostent hp;
275264841Seric 	static char buf[1000];
275364841Seric 	extern struct hostent *_switch_gethostbyname_r();
275464841Seric 
275564841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
275664841Seric # else
275765009Seric 	extern struct hostent *__switch_gethostbyname();
275865009Seric 
275964841Seric 	return __switch_gethostbyname(name);
276064841Seric # endif
276168693Seric #else
276268693Seric 	return gethostbyname(name);
276368693Seric #endif
276464841Seric }
276564841Seric 
276664841Seric struct hostent *
276768693Seric sm_gethostbyaddr(addr, len, type)
276865094Seric 	const char *addr;
276964841Seric 	int len;
277064841Seric 	int type;
277164841Seric {
277268693Seric #if defined(SOLARIS) && SOLARIS < 204
277368693Seric 	extern int h_errno;
277468693Seric 
277568508Seric # if SOLARIS == 203
277664841Seric 	static struct hostent hp;
277764841Seric 	static char buf[1000];
277864841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
277964841Seric 
278064841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
278164841Seric # else
278265009Seric 	extern struct hostent *__switch_gethostbyaddr();
278365009Seric 
278464841Seric 	return __switch_gethostbyaddr(addr, len, type);
278564841Seric # endif
278668693Seric #else
278768693Seric 	return gethostbyaddr(addr, len, type);
278868693Seric #endif
278964841Seric }
279068693Seric /*
279168693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
279268693Seric */
279364841Seric 
279468693Seric struct passwd *
279568693Seric sm_getpwnam(user)
279668704Seric 	char *user;
279768693Seric {
279868693Seric 	return getpwnam(user);
279968693Seric }
280068693Seric 
280168693Seric struct passwd *
280268693Seric sm_getpwuid(uid)
280368693Seric 	uid_t uid;
280468693Seric {
280568693Seric 	return getpwuid(uid);
280668693Seric }
280765214Seric /*
280865214Seric **  NI_PROPVAL -- netinfo property value lookup routine
280965214Seric **
281065214Seric **	Parameters:
281168508Seric **		keydir -- the Netinfo directory name in which to search
281268508Seric **			for the key.
281368508Seric **		keyprop -- the name of the property in which to find the
281468508Seric **			property we are interested.  Defaults to "name".
281568508Seric **		keyval -- the value for which we are really searching.
281668508Seric **		valprop -- the property name for the value in which we
281768508Seric **			are interested.
281868508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
281968508Seric **			we should return a string separated by this
282068508Seric **			character.
282165214Seric **
282265214Seric **	Returns:
282365214Seric **		NULL -- if:
282465214Seric **			1. the directory is not found
282565214Seric **			2. the property name is not found
282665214Seric **			3. the property contains multiple values
282765214Seric **			4. some error occured
282865214Seric **		else -- the location of the config file.
282965214Seric **
283068508Seric **	Example:
283168508Seric **		To search for an alias value, use:
283268508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
283368508Seric **
283465214Seric **	Notes:
283565214Seric **      	Caller should free the return value of ni_proval
283665214Seric */
283765214Seric 
283865214Seric #ifdef NETINFO
283965214Seric 
284065214Seric # include <netinfo/ni.h>
284165214Seric 
284265214Seric # define LOCAL_NETINFO_DOMAIN    "."
284365214Seric # define PARENT_NETINFO_DOMAIN   ".."
284465214Seric # define MAX_NI_LEVELS           256
284565214Seric 
284665214Seric char *
284768508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
284868508Seric 	char *keydir;
284968508Seric 	char *keyprop;
285068508Seric 	char *keyval;
285168508Seric 	char *valprop;
285268508Seric 	char sepchar;
285365214Seric {
285465820Seric 	char *propval = NULL;
285565214Seric 	int i;
285668508Seric 	int j, alen;
285765214Seric 	void *ni = NULL;
285865214Seric 	void *lastni = NULL;
285965214Seric 	ni_status nis;
286065214Seric 	ni_id nid;
286165214Seric 	ni_namelist ninl;
286268508Seric 	register char *p;
286368508Seric 	char keybuf[1024];
286465214Seric 
286565214Seric 	/*
286668508Seric 	**  Create the full key from the two parts.
286768508Seric 	**
286868508Seric 	**	Note that directory can end with, e.g., "name=" to specify
286968508Seric 	**	an alternate search property.
287068508Seric 	*/
287168508Seric 
287268508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
287368508Seric 	if (keyprop != NULL)
287468508Seric 		i += strlen(keyprop) + 1;
287568508Seric 	if (i > sizeof keybuf)
287668508Seric 		return NULL;
287768508Seric 	strcpy(keybuf, keydir);
287868508Seric 	strcat(keybuf, "/");
287968508Seric 	if (keyprop != NULL)
288068508Seric 	{
288168508Seric 		strcat(keybuf, keyprop);
288268508Seric 		strcat(keybuf, "=");
288368508Seric 	}
288468508Seric 	strcat(keybuf, keyval);
288568508Seric 
288668508Seric 	/*
288765214Seric 	**  If the passed directory and property name are found
288865214Seric 	**  in one of netinfo domains we need to search (starting
288965214Seric 	**  from the local domain moving all the way back to the
289065214Seric 	**  root domain) set propval to the property's value
289165214Seric 	**  and return it.
289265214Seric 	*/
289365214Seric 
289465214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
289565214Seric 	{
289665214Seric 		if (i == 0)
289765214Seric 		{
289865214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
289965214Seric 		}
290065214Seric 		else
290165214Seric 		{
290265214Seric 			if (lastni != NULL)
290365214Seric 				ni_free(lastni);
290465214Seric 			lastni = ni;
290565214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
290665214Seric 		}
290765214Seric 
290865214Seric 		/*
290965214Seric 		**  Don't bother if we didn't get a handle on a
291065214Seric 		**  proper domain.  This is not necessarily an error.
291165214Seric 		**  We would get a positive ni_status if, for instance
291265214Seric 		**  we never found the directory or property and tried
291365214Seric 		**  to open the parent of the root domain!
291465214Seric 		*/
291565214Seric 
291665214Seric 		if (nis != 0)
291765214Seric 			break;
291865214Seric 
291965214Seric 		/*
292065214Seric 		**  Find the path to the server information.
292165214Seric 		*/
292265214Seric 
292368508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
292465214Seric 			continue;
292565214Seric 
292665214Seric 		/*
292768508Seric 		**  Find associated value information.
292865214Seric 		*/
292965214Seric 
293068508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
293165214Seric 			continue;
293265214Seric 
293365214Seric 		/*
293468508Seric 		**  See if we have an acceptable number of values.
293565214Seric 		*/
293665214Seric 
293768508Seric 		if (ninl.ni_namelist_len <= 0)
293868508Seric 			continue;
293968508Seric 
294068508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
294165214Seric 		{
294268508Seric 			ni_namelist_free(&ninl);
294368508Seric 			continue;
294465214Seric 		}
294568508Seric 
294668508Seric 		/*
294768508Seric 		**  Calculate number of bytes needed and build result
294868508Seric 		*/
294968508Seric 
295068508Seric 		alen = 1;
295168508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
295268508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
295368508Seric 		propval = p = xalloc(alen);
295468508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
295568508Seric 		{
295668508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
295768508Seric 			p += strlen(p);
295868508Seric 			*p++ = sepchar;
295968508Seric 		}
296068508Seric 		*--p = '\0';
296168508Seric 
296268508Seric 		ni_namelist_free(&ninl);
296365214Seric 	}
296465214Seric 
296565214Seric 	/*
296665214Seric 	**  Clean up.
296765214Seric 	*/
296865214Seric 
296965214Seric 	if (ni != NULL)
297065214Seric 		ni_free(ni);
297165214Seric 	if (lastni != NULL && ni != lastni)
297265214Seric 		ni_free(lastni);
297365214Seric 
297465214Seric 	return propval;
297565214Seric }
297665214Seric 
297765214Seric #endif /* NETINFO */
297865504Seric /*
297965504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
298065504Seric **
298165504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
298265504Seric **	syslog succeeds during interrupt handlers.
298365504Seric */
298465504Seric 
298565504Seric #ifdef __hpux
298665504Seric 
298765504Seric # define MAXSYSLOGTRIES	100
298865504Seric # undef syslog
298965504Seric 
299065504Seric # ifdef __STDC__
299165504Seric hard_syslog(int pri, char *msg, ...)
299265504Seric # else
299365504Seric hard_syslog(pri, msg, va_alist)
299465504Seric 	int pri;
299565504Seric 	char *msg;
299665504Seric 	va_dcl
299765504Seric # endif
299865504Seric {
299965504Seric 	int i;
300065504Seric 	char buf[SYSLOG_BUFSIZE * 2];
300165504Seric 	VA_LOCAL_DECL;
300265504Seric 
300365504Seric 	VA_START(msg);
300465504Seric 	vsprintf(buf, msg, ap);
300565504Seric 	VA_END;
300665504Seric 
300765504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
300865504Seric 		continue;
300965504Seric }
301065504Seric 
301165504Seric #endif
301268696Seric /*
301368696Seric **  LOCAL_HOSTNAME_LENGTH
301468696Seric **
301568696Seric **	This is required to get sendmail to compile against BIND 4.9.x
301668696Seric **	on Ultrix.
301768696Seric */
301868696Seric 
301968696Seric #if defined(ultrix) && NAMED_BIND
302068696Seric 
302168696Seric # include <resolv.h>
302268696Seric # if __RES >= 19931104
302368696Seric 
302468696Seric int
302568696Seric local_hostname_length(hostname)
302668696Seric 	char *hostname;
302768696Seric {
302868696Seric 	int len_host, len_domain;
302968696Seric 
303068696Seric 	if (!*_res.defdname)
303168696Seric 		res_init();
303268696Seric 	len_host = strlen(hostname);
303368696Seric 	len_domain = strlen(_res.defdname);
303468696Seric 	if (len_host > len_domain &&
303568696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
303668696Seric 	    hostname[len_host - len_domain - 1] == '.')
303768696Seric 		return len_host - len_domain - 1;
303868696Seric 	else
303968696Seric 		return 0;
304068696Seric }
304168696Seric 
304268696Seric # endif
304368696Seric #endif
3044