xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 68868)
122698Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*68868Seric static char sccsid[] = "@(#)conf.c	8.154 (Berkeley) 04/23/95";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
17404Seric 
18294Seric /*
193309Seric **  CONF.C -- Sendmail Configuration Tables.
20294Seric **
21294Seric **	Defines the configuration of this installation.
22294Seric **
231388Seric **	Configuration Variables:
242897Seric **		HdrInfo -- a table describing well-known header fields.
252897Seric **			Each entry has the field name and some flags,
264147Seric **			which are described in sendmail.h.
274093Seric **
284093Seric **	Notes:
294093Seric **		I have tried to put almost all the reasonable
304093Seric **		configuration information into the configuration
314093Seric **		file read at runtime.  My intent is that anything
324093Seric **		here is a function of the version of UNIX you
334093Seric **		are running, or is really static -- for example
344093Seric **		the headers are a superset of widely used
354093Seric **		protocols.  If you find yourself playing with
364093Seric **		this file too much, you may be making a mistake!
37294Seric */
38294Seric 
39294Seric 
40294Seric 
41294Seric 
424437Seric /*
432897Seric **  Header info table
443057Seric **	Final (null) entry contains the flags used for any other field.
454147Seric **
464147Seric **	Not all of these are actually handled specially by sendmail
474147Seric **	at this time.  They are included as placeholders, to let
484147Seric **	you know that "someday" I intend to have sendmail do
494147Seric **	something with them.
502897Seric */
512897Seric 
522897Seric struct hdrinfo	HdrInfo[] =
532897Seric {
548060Seric 		/* originator fields, most to least significant  */
5568508Seric 	"resent-sender",		H_FROM|H_RESENT,
5668508Seric 	"resent-from",			H_FROM|H_RESENT,
5768508Seric 	"resent-reply-to",		H_FROM|H_RESENT,
5868508Seric 	"sender",			H_FROM,
5968508Seric 	"from",				H_FROM,
6068508Seric 	"reply-to",			H_FROM,
6168508Seric 	"full-name",			H_ACHECK,
6268508Seric 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
6368508Seric 	"errors-to",			H_FROM|H_ERRORSTO,
6458796Seric 
658060Seric 		/* destination fields */
6668508Seric 	"to",				H_RCPT,
6768508Seric 	"resent-to",			H_RCPT|H_RESENT,
6868508Seric 	"cc",				H_RCPT,
6968508Seric 	"resent-cc",			H_RCPT|H_RESENT,
7068508Seric 	"bcc",				H_RCPT|H_STRIPVAL,
7168508Seric 	"resent-bcc",			H_RCPT|H_STRIPVAL|H_RESENT,
7268508Seric 	"apparently-to",		H_RCPT,
7358796Seric 
748060Seric 		/* message identification and control */
7568508Seric 	"message-id",			0,
7668508Seric 	"resent-message-id",		H_RESENT,
7768508Seric 	"message",			H_EOH,
7868508Seric 	"text",				H_EOH,
7958796Seric 
8011417Seric 		/* date fields */
8168508Seric 	"date",				0,
8268508Seric 	"resent-date",			H_RESENT,
8358796Seric 
848060Seric 		/* trace fields */
8568508Seric 	"received",			H_TRACE|H_FORCE,
8668508Seric 	"x400-received",		H_TRACE|H_FORCE,
8768508Seric 	"via",				H_TRACE|H_FORCE,
8868508Seric 	"mail-from",			H_TRACE|H_FORCE,
898060Seric 
9058796Seric 		/* miscellaneous fields */
9168508Seric 	"comments",			H_FORCE,
9268508Seric 	"return-path",			H_FORCE|H_ACHECK,
9368508Seric 	"content-transfer-encoding",	H_CTE,
9468508Seric 	"content-type",			H_CTYPE,
9568706Seric 	"content-length",		H_ACHECK,
9658796Seric 
979055Seric 	NULL,			0,
982897Seric };
994166Seric 
1004166Seric 
1014166Seric 
1024282Seric /*
1034282Seric **  Location of system files/databases/etc.
1044282Seric */
1054282Seric 
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12264333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12364333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12566783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12658082Seric 	"goaway",		PRIV_GOAWAY,
12758789Seric 	NULL,			0,
12858082Seric };
12958082Seric 
13058082Seric 
13158082Seric 
13258082Seric /*
13324943Seric **  Miscellaneous stuff.
1349039Seric */
1359039Seric 
13624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13724943Seric /*
13824943Seric **  SETDEFAULTS -- set default values
13924943Seric **
14024943Seric **	Because of the way freezing is done, these must be initialized
14124943Seric **	using direct code.
14224943Seric **
14324943Seric **	Parameters:
14458734Seric **		e -- the default envelope.
14524943Seric **
14624943Seric **	Returns:
14724943Seric **		none.
14824943Seric **
14924943Seric **	Side Effects:
15024943Seric **		Initializes a bunch of global variables to their
15124943Seric **		default values.
15224943Seric */
15324943Seric 
15458737Seric #define DAYS		* 24 * 60 * 60
15558737Seric 
15668508Seric void
15758734Seric setdefaults(e)
15858734Seric 	register ENVELOPE *e;
15924943Seric {
16068508Seric 	int i;
16168508Seric 	extern void inittimeouts();
16268508Seric 	extern void setdefuser();
16368508Seric 	extern void setupmaps();
16468508Seric 	extern void setupmailers();
16568508Seric 
16657438Seric 	SpaceSub = ' ';				/* option B */
16757438Seric 	QueueLA = 8;				/* option x */
16857438Seric 	RefuseLA = 12;				/* option X */
16957438Seric 	WkRecipFact = 30000L;			/* option y */
17057438Seric 	WkClassFact = 1800L;			/* option z */
17157438Seric 	WkTimeFact = 90000L;			/* option Z */
17257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
17363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
17457438Seric 						/* option F */
17557438Seric 	DefUid = 1;				/* option u */
17657438Seric 	DefGid = 1;				/* option g */
17757438Seric 	CheckpointInterval = 10;		/* option C */
17857438Seric 	MaxHopCount = 25;			/* option h */
17958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
18168508Seric 	SevenBitInput = FALSE;			/* option 7 */
18257438Seric 	MaxMciCache = 1;			/* option k */
18357438Seric 	MciCacheTimeout = 300;			/* option K */
18457438Seric 	LogLevel = 9;				/* option L */
18568508Seric 	inittimeouts(NULL);			/* option r */
18658853Seric 	PrivacyFlags = 0;			/* option p */
18768508Seric 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
18868508Seric 	for (i = 0; i < MAXTOCLASS; i++)
18968508Seric 	{
19068508Seric 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
19168508Seric 		TimeOuts.to_q_warning[i] = 0;		/* option T */
19268508Seric 	}
19368508Seric 	ServiceSwitchFile = "/etc/service.switch";
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 
260*68868Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=X-Unix, A=sh -c $u");
26157403Seric 	makemailer(buf);
26257403Seric 
263*68868Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, T=X-Unix, 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
86168857Seric **	message should be given using "usrerr" and an EX_ code
86268857Seric **	should be returned.  You can also set to->q_status to
86368857Seric **	a DSN-style status code.
8642967Seric **
86568559Seric **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
86668559Seric **	body during the return-to-sender function; this should be done
86768559Seric **	on huge messages.  This bit may already be set by the ESMTP
86868559Seric **	protocol.
8694288Seric **
8702967Seric **	Parameters:
8712967Seric **		to -- the person being sent to.
8722967Seric **
8732967Seric **	Returns:
87457459Seric **		an exit status
8752967Seric **
8762967Seric **	Side Effects:
8772967Seric **		none (unless you include the usrerr stuff)
8782967Seric */
8792967Seric 
88068508Seric int
88155012Seric checkcompat(to, e)
8822967Seric 	register ADDRESS *to;
88355012Seric 	register ENVELOPE *e;
8842967Seric {
88512133Seric # ifdef lint
88612133Seric 	if (to == NULL)
88712133Seric 		to++;
88863834Seric # endif /* lint */
88964402Seric 
89064402Seric 	if (tTd(49, 1))
89164402Seric 		printf("checkcompat(to=%s, from=%s)\n",
89264402Seric 			to->q_paddr, e->e_from.q_paddr);
89364402Seric 
89410698Seric # ifdef EXAMPLE_CODE
89510698Seric 	/* this code is intended as an example only */
8964437Seric 	register STAB *s;
8974437Seric 
8984437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
89968508Seric 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
9009369Seric 	    to->q_mailer == s->s_mailer)
9014437Seric 	{
90258151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
90368559Seric 		/* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */
90468857Seric 		to->q_status = "5.7.1";
90557459Seric 		return (EX_UNAVAILABLE);
9064437Seric 	}
90756795Seric # endif /* EXAMPLE_CODE */
90857459Seric 	return (EX_OK);
9092967Seric }
9109369Seric /*
91164035Seric **  SETSIGNAL -- set a signal handler
91264035Seric **
91364035Seric **	This is essentially old BSD "signal(3)".
91464035Seric */
91564035Seric 
91664561Seric sigfunc_t
91764035Seric setsignal(sig, handler)
91864035Seric 	int sig;
91964561Seric 	sigfunc_t handler;
92064035Seric {
92164380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
92264035Seric 	return signal(sig, handler);
92364035Seric #else
92464035Seric 	struct sigaction n, o;
92564035Seric 
92664035Seric 	bzero(&n, sizeof n);
92764035Seric 	n.sa_handler = handler;
92868508Seric # ifdef SA_RESTART
92968508Seric 	n.sa_flags = SA_RESTART;
93068508Seric # endif
93164035Seric 	if (sigaction(sig, &n, &o) < 0)
93264035Seric 		return SIG_ERR;
93364035Seric 	return o.sa_handler;
93464035Seric #endif
93564035Seric }
93664035Seric /*
9379369Seric **  HOLDSIGS -- arrange to hold all signals
9389369Seric **
9399369Seric **	Parameters:
9409369Seric **		none.
9419369Seric **
9429369Seric **	Returns:
9439369Seric **		none.
9449369Seric **
9459369Seric **	Side Effects:
9469369Seric **		Arranges that signals are held.
9479369Seric */
9489369Seric 
94968508Seric void
9509369Seric holdsigs()
9519369Seric {
9529369Seric }
9539369Seric /*
9549369Seric **  RLSESIGS -- arrange to release all signals
9559369Seric **
9569369Seric **	This undoes the effect of holdsigs.
9579369Seric **
9589369Seric **	Parameters:
9599369Seric **		none.
9609369Seric **
9619369Seric **	Returns:
9629369Seric **		none.
9639369Seric **
9649369Seric **	Side Effects:
9659369Seric **		Arranges that signals are released.
9669369Seric */
9679369Seric 
96868508Seric void
9699369Seric rlsesigs()
9709369Seric {
9719369Seric }
97214872Seric /*
97364705Seric **  INIT_MD -- do machine dependent initializations
97464705Seric **
97564705Seric **	Systems that have global modes that should be set should do
97664705Seric **	them here rather than in main.
97764705Seric */
97864705Seric 
97964705Seric #ifdef _AUX_SOURCE
98064705Seric # include	<compat.h>
98164705Seric #endif
98264705Seric 
98368508Seric void
98464825Seric init_md(argc, argv)
98564825Seric 	int argc;
98664825Seric 	char **argv;
98764705Seric {
98864705Seric #ifdef _AUX_SOURCE
98964705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
99064705Seric #endif
99168508Seric 
99268508Seric #ifdef VENDOR_DEFAULT
99368508Seric 	VendorCode = VENDOR_DEFAULT;
99468508Seric #else
99568508Seric 	VendorCode = VENDOR_BERKELEY;
99668508Seric #endif
99764705Seric }
99864705Seric /*
99968508Seric **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
100068508Seric **
100168508Seric **	Called once, on startup.
100268508Seric **
100368508Seric **	Parameters:
100468508Seric **		e -- the global envelope.
100568508Seric **
100668508Seric **	Returns:
100768508Seric **		none.
100868508Seric **
100968508Seric **	Side Effects:
101068508Seric **		vendor-dependent.
101168508Seric */
101268508Seric 
101368508Seric void
101468508Seric init_vendor_macros(e)
101568508Seric 	register ENVELOPE *e;
101668508Seric {
101768508Seric }
101868508Seric /*
101914872Seric **  GETLA -- get the current load average
102014872Seric **
102114881Seric **	This code stolen from la.c.
102214881Seric **
102314872Seric **	Parameters:
102414872Seric **		none.
102514872Seric **
102614872Seric **	Returns:
102714872Seric **		The current load average as an integer.
102814872Seric **
102914872Seric **	Side Effects:
103014872Seric **		none.
103114872Seric */
103214872Seric 
103351920Seric /* try to guess what style of load average we have */
103451920Seric #define LA_ZERO		1	/* always return load average as zero */
103564360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
103651920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
103751920Seric #define LA_SUBR		4	/* call getloadavg */
103864295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
103964360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
104066301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
104114872Seric 
104263962Seric /* do guesses based on general OS type */
104351920Seric #ifndef LA_TYPE
104463962Seric # define LA_TYPE	LA_ZERO
104551920Seric #endif
104651920Seric 
104764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
104851920Seric 
104914872Seric #include <nlist.h>
105014872Seric 
105168508Seric #ifdef IRIX64
105268508Seric # define nlist		nlist64
105368508Seric #endif
105468508Seric 
105551920Seric #ifndef LA_AVENRUN
105663962Seric # ifdef SYSTEM5
105763962Seric #  define LA_AVENRUN	"avenrun"
105863962Seric # else
105963962Seric #  define LA_AVENRUN	"_avenrun"
106063962Seric # endif
106151920Seric #endif
106251920Seric 
106351920Seric /* _PATH_UNIX should be defined in <paths.h> */
106451920Seric #ifndef _PATH_UNIX
106563962Seric # if defined(SYSTEM5)
106663962Seric #  define _PATH_UNIX	"/unix"
106763962Seric # else
106863962Seric #  define _PATH_UNIX	"/vmunix"
106963962Seric # endif
107051920Seric #endif
107151920Seric 
107214872Seric struct	nlist Nl[] =
107314872Seric {
107451920Seric 	{ LA_AVENRUN },
107514872Seric #define	X_AVENRUN	0
107614872Seric 	{ 0 },
107714872Seric };
107814872Seric 
107960583Seric #ifndef FSHIFT
108060583Seric # if defined(unixpc)
108160583Seric #  define FSHIFT	5
108260583Seric # endif
108357736Seric 
108466763Seric # if defined(__alpha) || defined(IRIX)
108560583Seric #  define FSHIFT	10
108660583Seric # endif
108768508Seric 
108868508Seric # if defined(_AIX3)
108968508Seric #  define FSHIFT	16
109068508Seric # endif
109166788Seric #endif
109257977Seric 
109366788Seric #ifndef FSHIFT
109466788Seric # define FSHIFT		8
109557736Seric #endif
109660583Seric 
109766788Seric #ifndef FSCALE
109866788Seric # define FSCALE		(1 << FSHIFT)
109951920Seric #endif
110040930Srick 
110114872Seric getla()
110214872Seric {
110314872Seric 	static int kmem = -1;
110451920Seric #if LA_TYPE == LA_INT
110524943Seric 	long avenrun[3];
110651920Seric #else
110764360Seric # if LA_TYPE == LA_SHORT
110864360Seric 	short avenrun[3];
110964360Seric # else
111051920Seric 	double avenrun[3];
111164360Seric # endif
111251920Seric #endif
111325615Seric 	extern off_t lseek();
111457736Seric 	extern int errno;
111514872Seric 
111614872Seric 	if (kmem < 0)
111714872Seric 	{
111824945Seric 		kmem = open("/dev/kmem", 0, 0);
111914872Seric 		if (kmem < 0)
112057736Seric 		{
112157736Seric 			if (tTd(3, 1))
112257736Seric 				printf("getla: open(/dev/kmem): %s\n",
112357736Seric 					errstring(errno));
112414872Seric 			return (-1);
112557736Seric 		}
112651920Seric 		(void) fcntl(kmem, F_SETFD, 1);
112768508Seric #ifdef _AIX3
112868508Seric 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
112968508Seric #else
113057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
113168508Seric #endif
113257736Seric 		{
113357736Seric 			if (tTd(3, 1))
113457736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
113557736Seric 					errstring(errno));
113614872Seric 			return (-1);
113757736Seric 		}
113859253Seric 		if (Nl[X_AVENRUN].n_value == 0)
113959253Seric 		{
114059253Seric 			if (tTd(3, 1))
114159253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
114259253Seric 					_PATH_UNIX, LA_AVENRUN);
114359253Seric 			return (-1);
114459253Seric 		}
114568508Seric #ifdef NAMELISTMASK
114668508Seric 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
114766763Seric #endif
114814872Seric 	}
114957736Seric 	if (tTd(3, 20))
115057736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
115168508Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
115223118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
115319967Seric 	{
115419967Seric 		/* thank you Ian */
115557736Seric 		if (tTd(3, 1))
115657736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
115719967Seric 		return (-1);
115819967Seric 	}
115964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
116057736Seric 	if (tTd(3, 5))
116157736Seric 	{
116257736Seric 		printf("getla: avenrun = %d", avenrun[0]);
116357736Seric 		if (tTd(3, 15))
116457736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
116557736Seric 		printf("\n");
116657736Seric 	}
116757736Seric 	if (tTd(3, 1))
116857736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
116924943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
117051920Seric #else
117157736Seric 	if (tTd(3, 5))
117257736Seric 	{
117357736Seric 		printf("getla: avenrun = %g", avenrun[0]);
117457736Seric 		if (tTd(3, 15))
117557736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
117657736Seric 		printf("\n");
117757736Seric 	}
117857736Seric 	if (tTd(3, 1))
117957736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
118051920Seric 	return ((int) (avenrun[0] + 0.5));
118151920Seric #endif
118214872Seric }
118314872Seric 
118451773Seric #else
118551920Seric #if LA_TYPE == LA_SUBR
118651773Seric 
118764718Seric #ifdef DGUX
118864718Seric 
118964718Seric #include <sys/dg_sys_info.h>
119064718Seric 
119168508Seric int
119268508Seric getla()
119364718Seric {
119464718Seric 	struct dg_sys_info_load_info load_info;
119564718Seric 
119664718Seric 	dg_sys_info((long *)&load_info,
119764718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
119864718Seric 
119968508Seric         if (tTd(3, 1))
120068508Seric                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
120168508Seric 
120264718Seric 	return((int) (load_info.one_minute + 0.5));
120364718Seric }
120464718Seric 
120564718Seric #else
120668508Seric # ifdef __hpux
120764718Seric 
120868508Seric #  include <sys/param.h>
120968508Seric #  include <sys/pstat.h>
121068508Seric 
121168508Seric int
121251773Seric getla()
121351773Seric {
121468508Seric 	struct pst_dynamic pstd;
121568508Seric 
121668508Seric 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
121768508Seric 			     (size_t) 1 ,0) == -1)
121868508Seric 		return 0;
121968508Seric 
122068508Seric         if (tTd(3, 1))
122168508Seric                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
122268508Seric 
122368508Seric 	return (int) (pstd.psd_avg_1_min + 0.5);
122468508Seric }
122568508Seric 
122668508Seric # else
122768508Seric 
122868508Seric int
122968508Seric getla()
123068508Seric {
123151920Seric 	double avenrun[3];
123251920Seric 
123351920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
123457736Seric 	{
123557736Seric 		if (tTd(3, 1))
123657736Seric 			perror("getla: getloadavg failed:");
123751920Seric 		return (-1);
123857736Seric 	}
123957736Seric 	if (tTd(3, 1))
124057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
124151920Seric 	return ((int) (avenrun[0] + 0.5));
124251773Seric }
124351773Seric 
124468508Seric # endif /* __hpux */
124564718Seric #endif /* DGUX */
124651773Seric #else
124764295Seric #if LA_TYPE == LA_MACH
124851773Seric 
124964295Seric /*
125065173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
125164295Seric */
125264295Seric 
125365173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
125465173Seric # include <mach/mach.h>
125565173Seric #else
125665173Seric # include <mach.h>
125765173Seric #endif
125864295Seric 
125951773Seric getla()
126051773Seric {
126164295Seric 	processor_set_t default_set;
126264295Seric 	kern_return_t error;
126364295Seric 	unsigned int info_count;
126464295Seric 	struct processor_set_basic_info info;
126564295Seric 	host_t host;
126664295Seric 
126764295Seric 	error = processor_set_default(host_self(), &default_set);
126864295Seric 	if (error != KERN_SUCCESS)
126964295Seric 		return -1;
127064295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
127164295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
127264295Seric 			       &host, (processor_set_info_t)&info,
127364295Seric 			       &info_count) != KERN_SUCCESS)
127464295Seric 	{
127564295Seric 		return -1;
127664295Seric 	}
127764295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
127864295Seric }
127964295Seric 
128064295Seric 
128164295Seric #else
128266313Seric #if LA_TYPE == LA_PROCSTR
128364295Seric 
128466301Seric /*
128566301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
128666301Seric **  in a format like "0.15 0.12 0.06".
128766301Seric **
128866301Seric **	Initially intended for Linux.  This has been in the kernel
128966301Seric **	since at least 0.99.15.
129066301Seric */
129166301Seric 
129266301Seric # ifndef _PATH_LOADAVG
129366301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
129466301Seric # endif
129566301Seric 
129666301Seric int
129764295Seric getla()
129864295Seric {
129966319Seric 	double avenrun;
130066301Seric 	register int result;
130166319Seric 	FILE *fp;
130266301Seric 
130366319Seric 	fp = fopen(_PATH_LOADAVG, "r");
130466319Seric 	if (fp == NULL)
130566301Seric 	{
130666319Seric 		if (tTd(3, 1))
130766319Seric 			printf("getla: fopen(%s): %s\n",
130866319Seric 				_PATH_LOADAVG, errstring(errno));
130966319Seric 		return -1;
131066301Seric 	}
131166301Seric 	result = fscanf(fp, "%lf", &avenrun);
131266319Seric 	fclose(fp);
131366301Seric 	if (result != 1)
131466301Seric 	{
131566310Seric 		if (tTd(3, 1))
131666301Seric 			printf("getla: fscanf() = %d: %s\n",
131766301Seric 				result, errstring(errno));
131866301Seric 		return -1;
131966301Seric 	}
132066301Seric 
132157736Seric 	if (tTd(3, 1))
132266301Seric 		printf("getla(): %.2f\n", avenrun);
132366301Seric 
132466301Seric 	return ((int) (avenrun + 0.5));
132566301Seric }
132666301Seric 
132766301Seric #else
132866301Seric 
132966301Seric getla()
133066301Seric {
133166301Seric 	if (tTd(3, 1))
133257736Seric 		printf("getla: ZERO\n");
133351920Seric 	return (0);
133451773Seric }
133551773Seric 
133651773Seric #endif
133751773Seric #endif
133864295Seric #endif
133966301Seric #endif
134066045Seric 
134166045Seric 
134266045Seric /*
134366045Seric  * Copyright 1989 Massachusetts Institute of Technology
134466045Seric  *
134566045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
134666045Seric  * documentation for any purpose is hereby granted without fee, provided that
134766045Seric  * the above copyright notice appear in all copies and that both that
134866045Seric  * copyright notice and this permission notice appear in supporting
134966045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
135066045Seric  * publicity pertaining to distribution of the software without specific,
135166045Seric  * written prior permission.  M.I.T. makes no representations about the
135266045Seric  * suitability of this software for any purpose.  It is provided "as is"
135366045Seric  * without express or implied warranty.
135466045Seric  *
135566045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
135666045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
135766045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135866045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
135966045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
136066045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
136166045Seric  *
136266045Seric  * Authors:  Many and varied...
136366045Seric  */
136466045Seric 
136566045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
136666045Seric #ifndef lint
136766045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
136866045Seric #endif /* !lint */
136966045Seric 
137066179Seric #ifdef apollo
137166045Seric # undef volatile
137266045Seric #    include <apollo/base.h>
137366045Seric 
137466045Seric /* ARGSUSED */
137566045Seric int getloadavg( call_data )
137666045Seric      caddr_t	call_data;	/* pointer to (double) return value */
137766045Seric {
137866045Seric      double *avenrun = (double *) call_data;
137966045Seric      int i;
138066045Seric      status_$t      st;
138166045Seric      long loadav[3];
138266045Seric      proc1_$get_loadav(loadav, &st);
138366045Seric      *avenrun = loadav[0] / (double) (1 << 16);
138466045Seric      return(0);
138566045Seric }
138666045Seric #   endif /* apollo */
138724943Seric /*
138824943Seric **  SHOULDQUEUE -- should this message be queued or sent?
138924943Seric **
139024943Seric **	Compares the message cost to the load average to decide.
139124943Seric **
139224943Seric **	Parameters:
139324943Seric **		pri -- the priority of the message in question.
139457438Seric **		ctime -- the message creation time.
139524943Seric **
139624943Seric **	Returns:
139724943Seric **		TRUE -- if this message should be queued up for the
139824943Seric **			time being.
139924943Seric **		FALSE -- if the load is low enough to send this message.
140024943Seric **
140124943Seric **	Side Effects:
140224943Seric **		none.
140324943Seric */
140424943Seric 
140524943Seric bool
140657438Seric shouldqueue(pri, ctime)
140724943Seric 	long pri;
140857438Seric 	time_t ctime;
140924943Seric {
141068508Seric 	bool rval;
141168508Seric 
141268508Seric 	if (tTd(3, 30))
141368508Seric 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
141451920Seric 	if (CurrentLA < QueueLA)
141568508Seric 	{
141668508Seric 		if (tTd(3, 30))
141768508Seric 			printf("FALSE (CurrentLA < QueueLA)\n");
141824943Seric 		return (FALSE);
141968508Seric 	}
142058132Seric 	if (CurrentLA >= RefuseLA)
142168508Seric 	{
142268508Seric 		if (tTd(3, 30))
142368508Seric 			printf("TRUE (CurrentLA >= RefuseLA)\n");
142458132Seric 		return (TRUE);
142568508Seric 	}
142668508Seric 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
142768508Seric 	if (tTd(3, 30))
142868508Seric 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
142968508Seric 	return rval;
143024943Seric }
143124943Seric /*
143253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
143353037Seric **
143453037Seric **	Parameters:
143553037Seric **		none.
143653037Seric **
143753037Seric **	Returns:
143853037Seric **		TRUE if incoming SMTP connections should be refused
143953037Seric **			(for now).
144053037Seric **		FALSE if we should accept new work.
144153037Seric **
144253037Seric **	Side Effects:
144353037Seric **		none.
144453037Seric */
144553037Seric 
144653037Seric bool
144753037Seric refuseconnections()
144853037Seric {
144968508Seric 	extern bool enoughspace();
145068508Seric 
145159156Seric #ifdef XLA
145259156Seric 	if (!xla_smtp_ok())
145359156Seric 		return TRUE;
145459156Seric #endif
145559156Seric 
145653037Seric 	/* this is probably too simplistic */
145768508Seric 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
145853037Seric }
145953037Seric /*
146024943Seric **  SETPROCTITLE -- set process title for ps
146124943Seric **
146224943Seric **	Parameters:
146358674Seric **		fmt -- a printf style format string.
146458674Seric **		a, b, c -- possible parameters to fmt.
146524943Seric **
146624943Seric **	Returns:
146724943Seric **		none.
146824943Seric **
146924943Seric **	Side Effects:
147024943Seric **		Clobbers argv of our main procedure so ps(1) will
147124943Seric **		display the title.
147224943Seric */
147324943Seric 
147468508Seric #define SPT_NONE	0	/* don't use it at all */
147568508Seric #define SPT_REUSEARGV	1	/* cover argv with title information */
147668508Seric #define SPT_BUILTIN	2	/* use libc builtin */
147768508Seric #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
147868508Seric #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
147968508Seric #define SPT_WRITEUDOT	5	/* write u. area in kmem */
148068508Seric 
148168508Seric #ifndef SPT_TYPE
148268508Seric # define SPT_TYPE	SPT_REUSEARGV
148368508Seric #endif
148468508Seric 
148568508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
148668508Seric 
148768508Seric # if SPT_TYPE == SPT_PSTAT
148858689Seric #  include <sys/pstat.h>
148958689Seric # endif
149068508Seric # if SPT_TYPE == SPT_PSSTRINGS
149159732Seric #  include <machine/vmparam.h>
149259732Seric #  include <sys/exec.h>
149368508Seric #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
149468508Seric #   undef SPT_TYPE
149568508Seric #   define SPT_TYPE	SPT_REUSEARGV
149668734Seric #  else
149768734Seric #   ifndef NKPDE			/* FreeBSD 2.0 */
149868734Seric #    define NKPDE 63
149968734Seric typedef unsigned int	*pt_entry_t;
150068734Seric #   endif
150164247Seric #  endif
150259732Seric # endif
150368508Seric 
150468508Seric # if SPT_TYPE == SPT_PSSTRINGS
150568508Seric #  define SETPROC_STATIC	static
150668508Seric # else
150759732Seric #  define SETPROC_STATIC
150859732Seric # endif
150967626Seric 
151068508Seric # ifndef SPT_PADCHAR
151168508Seric #  define SPT_PADCHAR	' '
151268508Seric # endif
151358689Seric 
151468508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
151564843Seric 
151668508Seric #if SPT_TYPE != SPT_BUILTIN
151768508Seric 
151824943Seric /*VARARGS1*/
151968508Seric void
152068508Seric # ifdef __STDC__
152157642Seric setproctitle(char *fmt, ...)
152268508Seric # else
152357642Seric setproctitle(fmt, va_alist)
152424943Seric 	char *fmt;
152557642Seric 	va_dcl
152668508Seric # endif
152724943Seric {
152868508Seric # if SPT_TYPE != SPT_NONE
152924943Seric 	register char *p;
153025049Seric 	register int i;
153159732Seric 	SETPROC_STATIC char buf[MAXLINE];
153256852Seric 	VA_LOCAL_DECL
153368508Seric #  if SPT_TYPE == SPT_PSTAT
153458689Seric 	union pstun pst;
153558689Seric #  endif
153624943Seric 	extern char **Argv;
153724943Seric 	extern char *LastArgv;
153824943Seric 
153958674Seric 	p = buf;
154024943Seric 
154158674Seric 	/* print sendmail: heading for grep */
154258674Seric 	(void) strcpy(p, "sendmail: ");
154358674Seric 	p += strlen(p);
154424943Seric 
154558674Seric 	/* print the argument string */
154658674Seric 	VA_START(fmt);
154758674Seric 	(void) vsprintf(p, fmt, ap);
154856852Seric 	VA_END;
154954996Seric 
155058674Seric 	i = strlen(buf);
155158689Seric 
155268508Seric #  if SPT_TYPE == SPT_PSTAT
155358689Seric 	pst.pst_command = buf;
155458689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
155558689Seric #  else
155668508Seric #   if SPT_TYPE == SPT_PSSTRINGS
155759732Seric 	PS_STRINGS->ps_nargvstr = 1;
155859732Seric 	PS_STRINGS->ps_argvstr = buf;
155959732Seric #   else
156054996Seric 	if (i > LastArgv - Argv[0] - 2)
156125049Seric 	{
156254996Seric 		i = LastArgv - Argv[0] - 2;
156358674Seric 		buf[i] = '\0';
156425049Seric 	}
156558674Seric 	(void) strcpy(Argv[0], buf);
156654997Seric 	p = &Argv[0][i];
156724943Seric 	while (p < LastArgv)
156868508Seric 		*p++ = SPT_PADCHAR;
156968508Seric 	Argv[1] = NULL;
157068508Seric #   endif /* SPT_TYPE == SPT_PSSTRINGS */
157168508Seric #  endif /* SPT_TYPE == SPT_PSTAT */
157268508Seric # endif /* SPT_TYPE != SPT_NONE */
157324943Seric }
157465982Seric 
157568508Seric #endif /* SPT_TYPE != SPT_BUILTIN */
157625698Seric /*
157725698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
157825698Seric **
157925698Seric **	Parameters:
158068508Seric **		sig -- the signal that got us here (unused).
158125698Seric **
158225698Seric **	Returns:
158325698Seric **		none.
158425698Seric **
158525698Seric **	Side Effects:
158625698Seric **		Picks up extant zombies.
158725698Seric */
158825698Seric 
158946928Sbostic void
159068508Seric reapchild(sig)
159168508Seric 	int sig;
159225698Seric {
159364738Seric 	int olderrno = errno;
159463966Seric # ifdef HASWAITPID
159560219Seric 	auto int status;
159660560Seric 	int count;
159760560Seric 	int pid;
159860219Seric 
159960560Seric 	count = 0;
160060560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
160160560Seric 	{
160260560Seric 		if (count++ > 1000)
160360560Seric 		{
160466748Seric #ifdef LOG
160560560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
160660560Seric 				pid, status);
160766748Seric #endif
160860560Seric 			break;
160960560Seric 		}
161060560Seric 	}
161160219Seric # else
161225698Seric # ifdef WNOHANG
161325698Seric 	union wait status;
161425698Seric 
161563753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
161625698Seric 		continue;
161756795Seric # else /* WNOHANG */
161825698Seric 	auto int status;
161925698Seric 
162060219Seric 	while (wait(&status) > 0)
162125698Seric 		continue;
162256795Seric # endif /* WNOHANG */
162360219Seric # endif
162464035Seric # ifdef SYS5SIGNALS
162564035Seric 	(void) setsignal(SIGCHLD, reapchild);
162658061Seric # endif
162764738Seric 	errno = olderrno;
162825698Seric }
162955418Seric /*
163055418Seric **  UNSETENV -- remove a variable from the environment
163155418Seric **
163255418Seric **	Not needed on newer systems.
163355418Seric **
163455418Seric **	Parameters:
163555418Seric **		name -- the string name of the environment variable to be
163655418Seric **			deleted from the current environment.
163755418Seric **
163855418Seric **	Returns:
163955418Seric **		none.
164055418Seric **
164155418Seric **	Globals:
164255418Seric **		environ -- a pointer to the current environment.
164355418Seric **
164455418Seric **	Side Effects:
164555418Seric **		Modifies environ.
164655418Seric */
164755418Seric 
164863962Seric #ifndef HASUNSETENV
164955418Seric 
165055418Seric void
165155418Seric unsetenv(name)
165255418Seric 	char *name;
165355418Seric {
165455418Seric 	extern char **environ;
165555418Seric 	register char **pp;
165655418Seric 	int len = strlen(name);
165755418Seric 
165855418Seric 	for (pp = environ; *pp != NULL; pp++)
165955418Seric 	{
166055418Seric 		if (strncmp(name, *pp, len) == 0 &&
166155418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
166255418Seric 			break;
166355418Seric 	}
166455418Seric 
166555418Seric 	for (; *pp != NULL; pp++)
166655418Seric 		*pp = pp[1];
166755418Seric }
166855418Seric 
166963962Seric #endif
167056215Seric /*
167156215Seric **  GETDTABLESIZE -- return number of file descriptors
167256215Seric **
167356215Seric **	Only on non-BSD systems
167456215Seric **
167556215Seric **	Parameters:
167656215Seric **		none
167756215Seric **
167856215Seric **	Returns:
167956215Seric **		size of file descriptor table
168056215Seric **
168156215Seric **	Side Effects:
168256215Seric **		none
168356215Seric */
168456215Seric 
168563787Seric #ifdef SOLARIS
168663787Seric # include <sys/resource.h>
168763787Seric #endif
168856215Seric 
168956215Seric int
169063787Seric getdtsize()
169156215Seric {
169263787Seric #ifdef RLIMIT_NOFILE
169363787Seric 	struct rlimit rl;
169463787Seric 
169563787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
169663787Seric 		return rl.rlim_cur;
169763787Seric #endif
169863787Seric 
169964031Seric # ifdef HASGETDTABLESIZE
170064031Seric 	return getdtablesize();
170164031Seric # else
170264031Seric #  ifdef _SC_OPEN_MAX
170358689Seric 	return sysconf(_SC_OPEN_MAX);
170463787Seric #  else
170556215Seric 	return NOFILE;
170663787Seric #  endif
170758689Seric # endif
170856215Seric }
170957631Seric /*
171057631Seric **  UNAME -- get the UUCP name of this system.
171157631Seric */
171257631Seric 
171357943Seric #ifndef HASUNAME
171457631Seric 
171557631Seric int
171657631Seric uname(name)
171757631Seric 	struct utsname *name;
171857631Seric {
171957631Seric 	FILE *file;
172057631Seric 	char *n;
172157631Seric 
172257631Seric 	name->nodename[0] = '\0';
172357631Seric 
172457661Seric 	/* try /etc/whoami -- one line with the node name */
172557631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
172657631Seric 	{
172757661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
172857631Seric 		(void) fclose(file);
172957661Seric 		n = strchr(name->nodename, '\n');
173057631Seric 		if (n != NULL)
173157631Seric 			*n = '\0';
173257631Seric 		if (name->nodename[0] != '\0')
173357631Seric 			return (0);
173457631Seric 	}
173557631Seric 
173657661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
173757631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
173857631Seric 	{
173957631Seric 		char buf[MAXLINE];
174057631Seric 
174157631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
174257631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
174357631Seric 					NODE_LENGTH, name->nodename) > 0)
174457631Seric 				break;
174557631Seric 		(void) fclose(file);
174657631Seric 		if (name->nodename[0] != '\0')
174757631Seric 			return (0);
174857631Seric 	}
174957631Seric 
175057631Seric #ifdef TRUST_POPEN
175157631Seric 	/*
175257631Seric 	**  Popen is known to have security holes.
175357631Seric 	*/
175457631Seric 
175557661Seric 	/* try uuname -l to return local name */
175657631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
175757631Seric 	{
175857661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
175957631Seric 		(void) pclose(file);
176057661Seric 		n = strchr(name, '\n');
176157631Seric 		if (n != NULL)
176257631Seric 			*n = '\0';
176357661Seric 		if (name->nodename[0] != '\0')
176457631Seric 			return (0);
176557631Seric 	}
176657631Seric #endif
176757631Seric 
176857631Seric 	return (-1);
176957631Seric }
177057943Seric #endif /* HASUNAME */
177158068Seric /*
177258068Seric **  INITGROUPS -- initialize groups
177358068Seric **
177458068Seric **	Stub implementation for System V style systems
177558068Seric */
177658068Seric 
177758068Seric #ifndef HASINITGROUPS
177858068Seric 
177958068Seric initgroups(name, basegid)
178058068Seric 	char *name;
178158068Seric 	int basegid;
178258068Seric {
178358068Seric 	return 0;
178458068Seric }
178558068Seric 
178658068Seric #endif
178758082Seric /*
178859289Seric **  SETSID -- set session id (for non-POSIX systems)
178959289Seric */
179059289Seric 
179159289Seric #ifndef HASSETSID
179259289Seric 
179363753Seric pid_t
179463753Seric setsid __P ((void))
179559289Seric {
179664155Seric #ifdef TIOCNOTTY
179764155Seric 	int fd;
179864155Seric 
179966743Seric 	fd = open("/dev/tty", O_RDWR, 0);
180064155Seric 	if (fd >= 0)
180164155Seric 	{
180264155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
180364155Seric 		(void) close(fd);
180464155Seric 	}
180564155Seric #endif /* TIOCNOTTY */
180664705Seric # ifdef SYS5SETPGRP
180763753Seric 	return setpgrp();
180863753Seric # else
180964155Seric 	return setpgid(0, getpid());
181059289Seric # endif
181159289Seric }
181259289Seric 
181359289Seric #endif
181459289Seric /*
181566748Seric **  FSYNC -- dummy fsync
181666748Seric */
181766748Seric 
181866748Seric #ifdef NEEDFSYNC
181966748Seric 
182066748Seric fsync(fd)
182166748Seric 	int fd;
182266748Seric {
182366748Seric # ifdef O_SYNC
182466748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
182566748Seric # else
182666748Seric 	/* nothing we can do */
182766748Seric 	return 0;
182866748Seric # endif
182966748Seric }
183066748Seric 
183166748Seric #endif
183266748Seric /*
183364718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
183464718Seric **
183564718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
183668508Seric **	instead of a long.  This patches things.  Only needed on versions
183768508Seric **	prior to 5.4.3.
183864718Seric */
183964718Seric 
184068508Seric #ifdef DGUX_5_4_2
184164718Seric 
184264718Seric #undef inet_addr
184364718Seric 
184464718Seric long
184564718Seric dgux_inet_addr(host)
184664718Seric 	char *host;
184764718Seric {
184864718Seric 	struct in_addr haddr;
184964718Seric 
185064718Seric 	haddr = inet_addr(host);
185164718Seric 	return haddr.s_addr;
185264718Seric }
185364718Seric 
185464718Seric #endif
185564718Seric /*
185663969Seric **  GETOPT -- for old systems or systems with bogus implementations
185763969Seric */
185863969Seric 
185963969Seric #ifdef NEEDGETOPT
186063969Seric 
186163969Seric /*
186263969Seric  * Copyright (c) 1985 Regents of the University of California.
186363969Seric  * All rights reserved.  The Berkeley software License Agreement
186463969Seric  * specifies the terms and conditions for redistribution.
186563969Seric  */
186663969Seric 
186763969Seric 
186863969Seric /*
186963969Seric ** this version hacked to add `atend' flag to allow state machine
187063969Seric ** to reset if invoked by the program to scan args for a 2nd time
187163969Seric */
187263969Seric 
187363969Seric #if defined(LIBC_SCCS) && !defined(lint)
187463969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
187564072Seric #endif /* LIBC_SCCS and not lint */
187663969Seric 
187763969Seric #include <stdio.h>
187863969Seric 
187963969Seric /*
188063969Seric  * get option letter from argument vector
188163969Seric  */
188265352Seric #ifdef _CONVEX_SOURCE
188368508Seric extern int	optind, opterr, optopt;
188468508Seric extern char	*optarg;
188565352Seric #else
188665352Seric int	opterr = 1;		/* if error message should be printed */
188765352Seric int	optind = 1;		/* index into parent argv vector */
188868470Seric int	optopt = 0;		/* character checked for validity */
188968470Seric char	*optarg = NULL;		/* argument associated with option */
189068508Seric #endif
189163969Seric 
189263969Seric #define BADCH	(int)'?'
189363969Seric #define EMSG	""
189463969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
189563969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
189663969Seric 
189763969Seric getopt(nargc,nargv,ostr)
189865160Seric 	int		nargc;
189965160Seric 	char *const	*nargv;
190065160Seric 	const char	*ostr;
190163969Seric {
190263969Seric 	static char	*place = EMSG;	/* option letter processing */
190363969Seric 	static char	atend = 0;
190463969Seric 	register char	*oli;		/* option letter list index */
190563969Seric 
190663969Seric 	if (atend) {
190763969Seric 		atend = 0;
190863969Seric 		place = EMSG;
190963969Seric 	}
191063969Seric 	if(!*place) {			/* update scanning pointer */
191164136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
191263969Seric 			atend++;
191363969Seric 			return(EOF);
191463969Seric 		}
191563969Seric 		if (*place == '-') {	/* found "--" */
191663969Seric 			++optind;
191763969Seric 			atend++;
191863969Seric 			return(EOF);
191963969Seric 		}
192063969Seric 	}				/* option letter okay? */
192164125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
192264136Seric 		if (!*place) ++optind;
192363969Seric 		tell(": illegal option -- ");
192463969Seric 	}
192563969Seric 	if (*++oli != ':') {		/* don't need argument */
192663969Seric 		optarg = NULL;
192763969Seric 		if (!*place) ++optind;
192863969Seric 	}
192963969Seric 	else {				/* need an argument */
193063969Seric 		if (*place) optarg = place;	/* no white space */
193163969Seric 		else if (nargc <= ++optind) {	/* no arg */
193263969Seric 			place = EMSG;
193363969Seric 			tell(": option requires an argument -- ");
193463969Seric 		}
193563969Seric 	 	else optarg = nargv[optind];	/* white space */
193663969Seric 		place = EMSG;
193763969Seric 		++optind;
193863969Seric 	}
193963969Seric 	return(optopt);			/* dump back option letter */
194063969Seric }
194163969Seric 
194263969Seric #endif
194363969Seric /*
194463969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
194563969Seric */
194663969Seric 
194763969Seric #ifdef NEEDVPRINTF
194863969Seric 
194963969Seric #define MAXARG	16
195063969Seric 
195163969Seric vfprintf(fp, fmt, ap)
195263969Seric 	FILE *	fp;
195363969Seric 	char *	fmt;
195463969Seric 	char **	ap;
195563969Seric {
195663969Seric 	char *	bp[MAXARG];
195763969Seric 	int	i = 0;
195863969Seric 
195963969Seric 	while (*ap && i < MAXARG)
196063969Seric 		bp[i++] = *ap++;
196163969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
196263969Seric 			 bp[4], bp[5], bp[6], bp[7],
196363969Seric 			 bp[8], bp[9], bp[10], bp[11],
196463969Seric 			 bp[12], bp[13], bp[14], bp[15]);
196563969Seric }
196663969Seric 
196763969Seric vsprintf(s, fmt, ap)
196863969Seric 	char *	s;
196963969Seric 	char *	fmt;
197063969Seric 	char **	ap;
197163969Seric {
197263969Seric 	char *	bp[MAXARG];
197363969Seric 	int	i = 0;
197463969Seric 
197563969Seric 	while (*ap && i < MAXARG)
197663969Seric 		bp[i++] = *ap++;
197763969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
197863969Seric 			bp[4], bp[5], bp[6], bp[7],
197963969Seric 			bp[8], bp[9], bp[10], bp[11],
198063969Seric 			bp[12], bp[13], bp[14], bp[15]);
198163969Seric }
198263969Seric 
198363969Seric #endif
198463969Seric /*
198565220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
198665208Seric **
198765211Seric **	Parameters:
198865211Seric **		shell -- the user's shell from /etc/passwd
198965211Seric **
199065211Seric **	Returns:
199165211Seric **		TRUE -- if it is ok to use this for unrestricted access.
199265211Seric **		FALSE -- if the shell is restricted.
199365208Seric */
199465208Seric 
199565587Seric #if !HASGETUSERSHELL
199665587Seric 
199765587Seric # ifndef _PATH_SHELLS
199865587Seric #  define _PATH_SHELLS	"/etc/shells"
199965587Seric # endif
200065587Seric 
200165587Seric char	*DefaultUserShells[] =
200265587Seric {
200368508Seric 	"/bin/sh",		/* standard shell */
200465587Seric 	"/usr/bin/sh",
200568508Seric 	"/bin/csh",		/* C shell */
200665587Seric 	"/usr/bin/csh",
200765587Seric #ifdef __hpux
200868508Seric 	"/bin/rsh",		/* restricted Bourne shell */
200968508Seric 	"/bin/ksh",		/* Korn shell */
201068508Seric 	"/bin/rksh",		/* restricted Korn shell */
201165587Seric 	"/bin/pam",
201268508Seric 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
201365587Seric 	"/bin/posix/sh",
201465211Seric #endif
201568508Seric #ifdef _AIX3
201668508Seric 	"/bin/ksh",		/* Korn shell */
201768508Seric 	"/usr/bin/ksh",
201868508Seric 	"/bin/tsh",		/* trusted shell */
201968508Seric 	"/usr/bin/tsh",
202068508Seric 	"/bin/bsh",		/* Bourne shell */
202168508Seric 	"/usr/bin/bsh",
202268508Seric #endif
202365587Seric 	NULL
202465587Seric };
202565587Seric 
202665208Seric #endif
202765208Seric 
202865747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
202965747Seric 
203065211Seric bool
203165211Seric usershellok(shell)
203265211Seric 	char *shell;
203365208Seric {
203465211Seric #if HASGETUSERSHELL
203565211Seric 	register char *p;
203665211Seric 	extern char *getusershell();
203765208Seric 
203868457Seric 	if (shell == NULL || shell[0] == '\0')
203968457Seric 		return TRUE;
204068457Seric 
204165211Seric 	setusershell();
204265211Seric 	while ((p = getusershell()) != NULL)
204365747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
204465211Seric 			break;
204565211Seric 	endusershell();
204665211Seric 	return p != NULL;
204765211Seric #else
204865211Seric 	register FILE *shellf;
204965211Seric 	char buf[MAXLINE];
205065208Seric 
205168457Seric 	if (shell == NULL || shell[0] == '\0')
205268457Seric 		return TRUE;
205368457Seric 
205465211Seric 	shellf = fopen(_PATH_SHELLS, "r");
205565211Seric 	if (shellf == NULL)
205665211Seric 	{
205765211Seric 		/* no /etc/shells; see if it is one of the std shells */
205865587Seric 		char **d;
205965587Seric 
206065587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
206165587Seric 		{
206265587Seric 			if (strcmp(shell, *d) == 0)
206365587Seric 				return TRUE;
206465587Seric 		}
206565587Seric 		return FALSE;
206665211Seric 	}
206765208Seric 
206865211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
206965211Seric 	{
207065211Seric 		register char *p, *q;
207165208Seric 
207265211Seric 		p = buf;
207365211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
207465211Seric 			p++;
207565211Seric 		if (*p == '#' || *p == '\0')
207665208Seric 			continue;
207765211Seric 		q = p;
207865211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
207965211Seric 			p++;
208065211Seric 		*p = '\0';
208165747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
208265211Seric 		{
208365211Seric 			fclose(shellf);
208465211Seric 			return TRUE;
208565211Seric 		}
208665208Seric 	}
208765211Seric 	fclose(shellf);
208865211Seric 	return FALSE;
208965211Seric #endif
209065208Seric }
209165208Seric /*
209264311Seric **  FREESPACE -- see how much free space is on the queue filesystem
209358082Seric **
209458082Seric **	Only implemented if you have statfs.
209558082Seric **
209658082Seric **	Parameters:
209764311Seric **		dir -- the directory in question.
209864311Seric **		bsize -- a variable into which the filesystem
209964311Seric **			block size is stored.
210058082Seric **
210158082Seric **	Returns:
210264311Seric **		The number of bytes free on the queue filesystem.
210364311Seric **		-1 if the statfs call fails.
210464311Seric **
210564311Seric **	Side effects:
210664311Seric **		Puts the filesystem block size into bsize.
210758082Seric */
210858082Seric 
210965749Seric /* statfs types */
211065749Seric #define SFS_NONE	0	/* no statfs implementation */
211165749Seric #define SFS_USTAT	1	/* use ustat */
211265749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
211365749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
211465749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
211565749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
211666752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
211765749Seric 
211865749Seric #ifndef SFS_TYPE
211965749Seric # define SFS_TYPE	SFS_NONE
212058157Seric #endif
212158157Seric 
212265749Seric #if SFS_TYPE == SFS_USTAT
212358157Seric # include <ustat.h>
212458157Seric #endif
212565749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
212665749Seric # include <sys/statfs.h>
212758082Seric #endif
212865749Seric #if SFS_TYPE == SFS_VFS
212965749Seric # include <sys/vfs.h>
213065749Seric #endif
213165749Seric #if SFS_TYPE == SFS_MOUNT
213265749Seric # include <sys/mount.h>
213365749Seric #endif
213466752Seric #if SFS_TYPE == SFS_STATVFS
213566752Seric # include <sys/statvfs.h>
213666752Seric #endif
213758082Seric 
213864311Seric long
213964311Seric freespace(dir, bsize)
214064311Seric 	char *dir;
214164311Seric 	long *bsize;
214258082Seric {
214365749Seric #if SFS_TYPE != SFS_NONE
214465749Seric # if SFS_TYPE == SFS_USTAT
214558153Seric 	struct ustat fs;
214658153Seric 	struct stat statbuf;
214758366Seric #  define FSBLOCKSIZE	DEV_BSIZE
214868695Seric #  define SFS_BAVAIL	f_tfree
214958157Seric # else
215058157Seric #  if defined(ultrix)
215158157Seric 	struct fs_data fs;
215268695Seric #   define SFS_BAVAIL	fd_bfreen
215368508Seric #   define FSBLOCKSIZE	1024L
215458153Seric #  else
215566752Seric #   if SFS_TYPE == SFS_STATVFS
215666752Seric 	struct statvfs fs;
215768508Seric #    define FSBLOCKSIZE	fs.f_frsize
215866752Seric #   else
215958082Seric 	struct statfs fs;
216066752Seric #    define FSBLOCKSIZE	fs.f_bsize
216163987Seric #   endif
216258153Seric #  endif
216358133Seric # endif
216468695Seric # ifndef SFS_BAVAIL
216568695Seric #  define SFS_BAVAIL f_bavail
216668508Seric # endif
216758082Seric 
216865749Seric # if SFS_TYPE == SFS_USTAT
216964311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
217058157Seric # else
217165749Seric #  if SFS_TYPE == SFS_4ARGS
217264311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
217358157Seric #  else
217468508Seric #   if SFS_TYPE == SFS_STATVFS
217568508Seric 	if (statvfs(dir, &fs) == 0)
217668508Seric #   else
217768508Seric #    if defined(ultrix)
217868499Seric 	if (statfs(dir, &fs) > 0)
217968508Seric #    else
218064311Seric 	if (statfs(dir, &fs) == 0)
218168508Seric #    endif
218258153Seric #   endif
218358133Seric #  endif
218458133Seric # endif
218558133Seric 	{
218664311Seric 		if (bsize != NULL)
218764311Seric 			*bsize = FSBLOCKSIZE;
218868695Seric 		return (fs.SFS_BAVAIL);
218964311Seric 	}
219064337Seric #endif
219164311Seric 	return (-1);
219264311Seric }
219364311Seric /*
219464311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
219564311Seric **
219664311Seric **	Only implemented if you have statfs.
219764311Seric **
219864311Seric **	Parameters:
219964311Seric **		msize -- the size to check against.  If zero, we don't yet
220064311Seric **		know how big the message will be, so just check for
220164311Seric **		a "reasonable" amount.
220264311Seric **
220364311Seric **	Returns:
220464311Seric **		TRUE if there is enough space.
220564311Seric **		FALSE otherwise.
220664311Seric */
220764311Seric 
220864311Seric bool
220964311Seric enoughspace(msize)
221064311Seric 	long msize;
221164311Seric {
221264311Seric 	long bfree, bsize;
221364311Seric 
221464311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
221564311Seric 	{
221658133Seric 		if (tTd(4, 80))
221764311Seric 			printf("enoughspace: no threshold\n");
221864311Seric 		return TRUE;
221964311Seric 	}
222064311Seric 
222164311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
222264311Seric 	{
222364311Seric 		if (tTd(4, 80))
222458333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
222564311Seric 				bfree, msize);
222658333Seric 
222758333Seric 		/* convert msize to block count */
222864311Seric 		msize = msize / bsize + 1;
222958333Seric 		if (MinBlocksFree >= 0)
223058333Seric 			msize += MinBlocksFree;
223158333Seric 
223264311Seric 		if (bfree < msize)
223358090Seric 		{
223458090Seric #ifdef LOG
223558090Seric 			if (LogLevel > 0)
223664254Seric 				syslog(LOG_ALERT,
223764254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
223868508Seric 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
223968508Seric 					bfree,
224068508Seric 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
224168508Seric 					msize, QueueDir);
224258090Seric #endif
224358082Seric 			return FALSE;
224458090Seric 		}
224558082Seric 	}
224658082Seric 	else if (tTd(4, 80))
224758333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
224858333Seric 			MinBlocksFree, msize, errstring(errno));
224958082Seric 	return TRUE;
225058082Seric }
225158542Seric /*
225258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
225358542Seric **
225458542Seric **	This looks at an errno value and tells if this is likely to
225558542Seric **	go away if retried later.
225658542Seric **
225758542Seric **	Parameters:
225858542Seric **		err -- the errno code to classify.
225958542Seric **
226058542Seric **	Returns:
226158542Seric **		TRUE if this is probably transient.
226258542Seric **		FALSE otherwise.
226358542Seric */
226458542Seric 
226558542Seric bool
226658542Seric transienterror(err)
226758542Seric 	int err;
226858542Seric {
226958542Seric 	switch (err)
227058542Seric 	{
227158542Seric 	  case EIO:			/* I/O error */
227258542Seric 	  case ENXIO:			/* Device not configured */
227358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
227458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
227558542Seric 	  case ENODEV:			/* Operation not supported by device */
227658542Seric 	  case ENFILE:			/* Too many open files in system */
227758542Seric 	  case EMFILE:			/* Too many open files */
227858542Seric 	  case ENOSPC:			/* No space left on device */
227958542Seric #ifdef ETIMEDOUT
228058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
228158542Seric #endif
228258542Seric #ifdef ESTALE
228358542Seric 	  case ESTALE:			/* Stale NFS file handle */
228458542Seric #endif
228558542Seric #ifdef ENETDOWN
228658542Seric 	  case ENETDOWN:		/* Network is down */
228758542Seric #endif
228858542Seric #ifdef ENETUNREACH
228958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
229058542Seric #endif
229158542Seric #ifdef ENETRESET
229258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
229358542Seric #endif
229458542Seric #ifdef ECONNABORTED
229558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
229658542Seric #endif
229758542Seric #ifdef ECONNRESET
229858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
229958542Seric #endif
230058542Seric #ifdef ENOBUFS
230158542Seric 	  case ENOBUFS:			/* No buffer space available */
230258542Seric #endif
230358542Seric #ifdef ESHUTDOWN
230458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
230558542Seric #endif
230658542Seric #ifdef ECONNREFUSED
230758542Seric 	  case ECONNREFUSED:		/* Connection refused */
230858542Seric #endif
230958542Seric #ifdef EHOSTDOWN
231058542Seric 	  case EHOSTDOWN:		/* Host is down */
231158542Seric #endif
231258542Seric #ifdef EHOSTUNREACH
231358542Seric 	  case EHOSTUNREACH:		/* No route to host */
231458542Seric #endif
231558542Seric #ifdef EDQUOT
231658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
231758542Seric #endif
231858542Seric #ifdef EPROCLIM
231958542Seric 	  case EPROCLIM:		/* Too many processes */
232058542Seric #endif
232158542Seric #ifdef EUSERS
232258542Seric 	  case EUSERS:			/* Too many users */
232358542Seric #endif
232458542Seric #ifdef EDEADLK
232558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
232658542Seric #endif
232758542Seric #ifdef EISCONN
232858542Seric 	  case EISCONN:			/* Socket already connected */
232958542Seric #endif
233058542Seric #ifdef EINPROGRESS
233158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
233258542Seric #endif
233358542Seric #ifdef EALREADY
233458542Seric 	  case EALREADY:		/* Operation already in progress */
233558542Seric #endif
233658542Seric #ifdef EADDRINUSE
233758542Seric 	  case EADDRINUSE:		/* Address already in use */
233858542Seric #endif
233958542Seric #ifdef EADDRNOTAVAIL
234058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
234158542Seric #endif
234266018Seric #ifdef ETXTBSY
234366017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
234466017Seric #endif
234563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
234658542Seric 	  case ENOSR:			/* Out of streams resources */
234758542Seric #endif
234858542Seric 		return TRUE;
234958542Seric 	}
235058542Seric 
235158542Seric 	/* nope, must be permanent */
235258542Seric 	return FALSE;
235358542Seric }
235458689Seric /*
235564035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
235658689Seric **
235758689Seric **	Parameters:
235858689Seric **		fd -- the file descriptor of the file.
235958689Seric **		filename -- the file name (for error messages).
236064335Seric **		ext -- the filename extension.
236158689Seric **		type -- type of the lock.  Bits can be:
236258689Seric **			LOCK_EX -- exclusive lock.
236358689Seric **			LOCK_NB -- non-blocking.
236458689Seric **
236558689Seric **	Returns:
236658689Seric **		TRUE if the lock was acquired.
236758689Seric **		FALSE otherwise.
236858689Seric */
236958689Seric 
237058689Seric bool
237164335Seric lockfile(fd, filename, ext, type)
237258689Seric 	int fd;
237358689Seric 	char *filename;
237464335Seric 	char *ext;
237558689Seric 	int type;
237658689Seric {
237765830Seric # if !HASFLOCK
237858689Seric 	int action;
237958689Seric 	struct flock lfd;
238064335Seric 
238164335Seric 	if (ext == NULL)
238264335Seric 		ext = "";
238364035Seric 
238463983Seric 	bzero(&lfd, sizeof lfd);
238559447Seric 	if (bitset(LOCK_UN, type))
238659447Seric 		lfd.l_type = F_UNLCK;
238759447Seric 	else if (bitset(LOCK_EX, type))
238858689Seric 		lfd.l_type = F_WRLCK;
238958689Seric 	else
239058689Seric 		lfd.l_type = F_RDLCK;
239158689Seric 
239258689Seric 	if (bitset(LOCK_NB, type))
239358689Seric 		action = F_SETLK;
239458689Seric 	else
239558689Seric 		action = F_SETLKW;
239658689Seric 
239764264Seric 	if (tTd(55, 60))
239864335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
239964335Seric 			filename, ext, action, lfd.l_type);
240064264Seric 
240158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
240264264Seric 	{
240364264Seric 		if (tTd(55, 60))
240464264Seric 			printf("SUCCESS\n");
240558689Seric 		return TRUE;
240664264Seric 	}
240758689Seric 
240864264Seric 	if (tTd(55, 60))
240964264Seric 		printf("(%s) ", errstring(errno));
241064264Seric 
241164136Seric 	/*
241264136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
241364136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
241464136Seric 	**  as type "tmp" (that is, served from swap space), the
241564136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
241664136Seric 	**  Since this is fairly common during testing, we will assume
241764136Seric 	**  that this indicates that the lock is successfully grabbed.
241864136Seric 	*/
241964136Seric 
242064136Seric 	if (errno == EINVAL)
242164264Seric 	{
242264264Seric 		if (tTd(55, 60))
242364264Seric 			printf("SUCCESS\n");
242464136Seric 		return TRUE;
242564264Seric 	}
242664136Seric 
242758689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
242864378Seric 	{
242964378Seric 		int omode = -1;
243064378Seric #  ifdef F_GETFL
243164378Seric 		int oerrno = errno;
243264378Seric 
243364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
243464378Seric 		errno = oerrno;
243564378Seric #  endif
243664378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
243764378Seric 			filename, ext, fd, type, omode, geteuid());
243864378Seric 	}
243958689Seric # else
244064335Seric 	if (ext == NULL)
244164335Seric 		ext = "";
244264335Seric 
244364264Seric 	if (tTd(55, 60))
244464335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
244564264Seric 
244658689Seric 	if (flock(fd, type) >= 0)
244764264Seric 	{
244864264Seric 		if (tTd(55, 60))
244964264Seric 			printf("SUCCESS\n");
245058689Seric 		return TRUE;
245164264Seric 	}
245258689Seric 
245364264Seric 	if (tTd(55, 60))
245464264Seric 		printf("(%s) ", errstring(errno));
245564264Seric 
245658689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
245764378Seric 	{
245864378Seric 		int omode = -1;
245964378Seric #  ifdef F_GETFL
246064378Seric 		int oerrno = errno;
246164378Seric 
246264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
246364378Seric 		errno = oerrno;
246464378Seric #  endif
246564378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
246664378Seric 			filename, ext, fd, type, omode, geteuid());
246764386Seric 	}
246858689Seric # endif
246964264Seric 	if (tTd(55, 60))
247064264Seric 		printf("FAIL\n");
247158689Seric 	return FALSE;
247258689Seric }
247364556Seric /*
247465948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
247565948Seric **
247665948Seric **	Parameters:
247765948Seric **		fd -- the file descriptor to check.
247865948Seric **
247965948Seric **	Returns:
248065948Seric **		TRUE -- if only root can chown the file to an arbitrary
248165948Seric **			user.
248265948Seric **		FALSE -- if an arbitrary user can give away a file.
248365948Seric */
248465948Seric 
248565948Seric bool
248665948Seric chownsafe(fd)
248765948Seric 	int fd;
248865948Seric {
248965950Seric #ifdef __hpux
249065948Seric 	char *s;
249165948Seric 	int tfd;
249265948Seric 	uid_t o_uid, o_euid;
249365948Seric 	gid_t o_gid, o_egid;
249465948Seric 	bool rval;
249565948Seric 	struct stat stbuf;
249665948Seric 
249765948Seric 	o_uid = getuid();
249865948Seric 	o_euid = geteuid();
249965948Seric 	o_gid = getgid();
250065948Seric 	o_egid = getegid();
250165959Seric 	fstat(fd, &stbuf);
250265948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
250365948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
250465948Seric 	s = tmpnam(NULL);
250565948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
250665974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
250765948Seric 	close(tfd);
250865948Seric 	unlink(s);
250965948Seric 	setreuid(o_uid, o_euid);
251065948Seric 	setresgid(o_gid, o_egid, -1);
251165948Seric 	return rval;
251265948Seric #else
251366088Seric # ifdef _POSIX_CHOWN_RESTRICTED
251466088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
251566088Seric 	return FALSE;
251666088Seric #  else
251766088Seric 	return TRUE;
251866088Seric #  endif
251966088Seric # else
252066088Seric #  ifdef _PC_CHOWN_RESTRICTED
252168508Seric 	int rval;
252268508Seric 
252368508Seric 	/*
252468508Seric 	**  Some systems (e.g., SunOS) seem to have the call and the
252568508Seric 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
252668508Seric 	**  the call.  This heuristic checks for that.
252768508Seric 	*/
252868508Seric 
252968508Seric 	errno = 0;
253068508Seric 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
253168508Seric 	if (errno == 0)
253268508Seric 		return rval > 0;
253368508Seric #  endif
253468508Seric #  ifdef BSD
253568508Seric 	return TRUE;
253668499Seric #  else
253765948Seric 	return FALSE;
253865948Seric #  endif
253965948Seric # endif
254065948Seric #endif
254165948Seric }
254265948Seric /*
254368508Seric **  RESETLIMITS -- reset system controlled resource limits
254468508Seric **
254568508Seric **	This is to avoid denial-of-service attacks
254668508Seric **
254768508Seric **	Parameters:
254868508Seric **		none
254968508Seric **
255068508Seric **	Returns:
255168508Seric **		none
255268508Seric */
255368508Seric 
255468508Seric #if HASSETRLIMIT
255568508Seric # include <sys/resource.h>
255668508Seric #endif
255768508Seric 
255868508Seric void
255968508Seric resetlimits()
256068508Seric {
256168508Seric #if HASSETRLIMIT
256268508Seric 	struct rlimit lim;
256368508Seric 
256468508Seric 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
256568508Seric 	(void) setrlimit(RLIMIT_CPU, &lim);
256668508Seric 	(void) setrlimit(RLIMIT_FSIZE, &lim);
256768508Seric #else
256868508Seric # if HASULIMIT
256968508Seric 	(void) ulimit(2, 0x3fffff);
257068508Seric # endif
257168508Seric #endif
257268508Seric }
257368508Seric /*
257464556Seric **  GETCFNAME -- return the name of the .cf file.
257564556Seric **
257664556Seric **	Some systems (e.g., NeXT) determine this dynamically.
257764556Seric */
257864556Seric 
257964556Seric char *
258064556Seric getcfname()
258164556Seric {
258268842Seric 	int i;
258368842Seric 	static char cbuf[200];
258468842Seric 
258564556Seric 	if (ConfFile != NULL)
258664556Seric 		return ConfFile;
258765214Seric #ifdef NETINFO
258865214Seric 	{
258965214Seric 		extern char *ni_propval();
259065214Seric 		char *cflocation;
259165214Seric 
259268508Seric 		cflocation = ni_propval("/locations", NULL, "sendmail",
259368508Seric 					"sendmail.cf", '\0');
259465214Seric 		if (cflocation != NULL)
259565214Seric 			return cflocation;
259665214Seric 	}
259765214Seric #endif
259868842Seric 
259968842Seric 	/*
260068842Seric 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
260168842Seric 	**  sendmail.8.cf, and finally sendmail.cf.
260268842Seric 	**
260368842Seric 	**	I suppose it should really try a search path here --
260468842Seric 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
260568842Seric 	**	/usr/lib/sendmail.cf, and so forth.
260668842Seric 	*/
260768842Seric 
260868842Seric 	strcpy(cbuf, _PATH_SENDMAILCF);
260968842Seric 	i = strlen(cbuf);
261068842Seric 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
261168842Seric 	{
261268842Seric 		char *p;
261368842Seric 		extern char Version[];
261468842Seric 
261568842Seric 		strcpy(&cbuf[i - 2], Version);
261668842Seric 		p = strchr(&cbuf[i - 2], '/');
261768842Seric 		if (p != NULL)
261868842Seric 			*p = '\0';
261968842Seric 		p = &cbuf[strlen(cbuf)];
262068842Seric 		do
262168842Seric 		{
262268842Seric 			int fd;
262368842Seric 
262468842Seric 			strcpy(p, ".cf");
262568842Seric 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
262668842Seric 			{
262768842Seric 				close(fd);
262868842Seric 				return cbuf;
262968842Seric 			}
263068842Seric 			*p = '\0';
263168842Seric 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
263268842Seric 	}
263364556Seric 	return _PATH_SENDMAILCF;
263464556Seric }
263564718Seric /*
263664718Seric **  SETVENDOR -- process vendor code from V configuration line
263764718Seric **
263864718Seric **	Parameters:
263964718Seric **		vendor -- string representation of vendor.
264064718Seric **
264164718Seric **	Returns:
264264718Seric **		TRUE -- if ok.
264364718Seric **		FALSE -- if vendor code could not be processed.
264464926Seric **
264564926Seric **	Side Effects:
264664926Seric **		It is reasonable to set mode flags here to tweak
264764926Seric **		processing in other parts of the code if necessary.
264864926Seric **		For example, if you are a vendor that uses $%y to
264964926Seric **		indicate YP lookups, you could enable that here.
265064718Seric */
265164718Seric 
265264718Seric bool
265364718Seric setvendor(vendor)
265464718Seric 	char *vendor;
265564718Seric {
265664926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
265768508Seric 	{
265868508Seric 		VendorCode = VENDOR_BERKELEY;
265964926Seric 		return TRUE;
266068508Seric 	}
266164926Seric 
266264926Seric 	/* add vendor extensions here */
266364926Seric 
266468508Seric #ifdef SUN_EXTENSIONS
266568508Seric 	if (strcasecmp(vendor, "Sun") == 0)
266668508Seric 	{
266768508Seric 		VendorCode = VENDOR_SUN;
266868508Seric 		return TRUE;
266968508Seric 	}
267068508Seric #endif
267168508Seric 
267264926Seric 	return FALSE;
267364718Seric }
267464816Seric /*
267564816Seric **  STRTOL -- convert string to long integer
267664816Seric **
267764816Seric **	For systems that don't have it in the C library.
267866161Seric **
267966161Seric **	This is taken verbatim from the 4.4-Lite C library.
268064816Seric */
268164816Seric 
268264816Seric #ifdef NEEDSTRTOL
268364816Seric 
268466161Seric #if defined(LIBC_SCCS) && !defined(lint)
268566161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
268666161Seric #endif /* LIBC_SCCS and not lint */
268766161Seric 
268866161Seric #include <limits.h>
268966161Seric 
269066161Seric /*
269166161Seric  * Convert a string to a long integer.
269266161Seric  *
269366161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
269466161Seric  * alphabets and digits are each contiguous.
269566161Seric  */
269666161Seric 
269764816Seric long
269866161Seric strtol(nptr, endptr, base)
269966161Seric 	const char *nptr;
270066161Seric 	char **endptr;
270166161Seric 	register int base;
270264816Seric {
270366161Seric 	register const char *s = nptr;
270466161Seric 	register unsigned long acc;
270566161Seric 	register int c;
270666161Seric 	register unsigned long cutoff;
270766161Seric 	register int neg = 0, any, cutlim;
270864816Seric 
270966161Seric 	/*
271066161Seric 	 * Skip white space and pick up leading +/- sign if any.
271166161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
271266161Seric 	 * assume decimal; if base is already 16, allow 0x.
271366161Seric 	 */
271466161Seric 	do {
271566161Seric 		c = *s++;
271666161Seric 	} while (isspace(c));
271766161Seric 	if (c == '-') {
271866161Seric 		neg = 1;
271966161Seric 		c = *s++;
272066161Seric 	} else if (c == '+')
272166161Seric 		c = *s++;
272266161Seric 	if ((base == 0 || base == 16) &&
272366161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
272466161Seric 		c = s[1];
272566161Seric 		s += 2;
272666161Seric 		base = 16;
272766161Seric 	}
272866161Seric 	if (base == 0)
272966161Seric 		base = c == '0' ? 8 : 10;
273064816Seric 
273166161Seric 	/*
273266161Seric 	 * Compute the cutoff value between legal numbers and illegal
273366161Seric 	 * numbers.  That is the largest legal value, divided by the
273466161Seric 	 * base.  An input number that is greater than this value, if
273566161Seric 	 * followed by a legal input character, is too big.  One that
273666161Seric 	 * is equal to this value may be valid or not; the limit
273766161Seric 	 * between valid and invalid numbers is then based on the last
273866161Seric 	 * digit.  For instance, if the range for longs is
273966161Seric 	 * [-2147483648..2147483647] and the input base is 10,
274066161Seric 	 * cutoff will be set to 214748364 and cutlim to either
274166161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
274266161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
274366161Seric 	 * the number is too big, and we will return a range error.
274466161Seric 	 *
274566161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
274666161Seric 	 * overflow.
274766161Seric 	 */
274866161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
274966161Seric 	cutlim = cutoff % (unsigned long)base;
275066161Seric 	cutoff /= (unsigned long)base;
275166161Seric 	for (acc = 0, any = 0;; c = *s++) {
275266161Seric 		if (isdigit(c))
275366161Seric 			c -= '0';
275466161Seric 		else if (isalpha(c))
275566161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
275666161Seric 		else
275766161Seric 			break;
275866161Seric 		if (c >= base)
275966161Seric 			break;
276066161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
276166161Seric 			any = -1;
276266161Seric 		else {
276366161Seric 			any = 1;
276466161Seric 			acc *= base;
276566161Seric 			acc += c;
276664816Seric 		}
276764816Seric 	}
276866161Seric 	if (any < 0) {
276966161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
277066161Seric 		errno = ERANGE;
277166161Seric 	} else if (neg)
277266161Seric 		acc = -acc;
277366161Seric 	if (endptr != 0)
277466161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
277566161Seric 	return (acc);
277664816Seric }
277764816Seric 
277864816Seric #endif
277964841Seric /*
278068693Seric **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
278164841Seric **
278268693Seric **	Some operating systems have wierd problems with the gethostbyXXX
278368693Seric **	routines.  For example, Solaris versions at least through 2.3
278468693Seric **	don't properly deliver a canonical h_name field.  This tries to
278568693Seric **	work around these problems.
278664841Seric */
278764841Seric 
278864841Seric struct hostent *
278968693Seric sm_gethostbyname(name)
279068707Seric 	char *name;
279164841Seric {
279268693Seric #if defined(SOLARIS) && SOLARIS < 204
279368693Seric 	extern int h_errno;
279468693Seric 
279568508Seric # if SOLARIS == 203
279664841Seric 	static struct hostent hp;
279764841Seric 	static char buf[1000];
279864841Seric 	extern struct hostent *_switch_gethostbyname_r();
279964841Seric 
280064841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
280164841Seric # else
280265009Seric 	extern struct hostent *__switch_gethostbyname();
280365009Seric 
280464841Seric 	return __switch_gethostbyname(name);
280564841Seric # endif
280668693Seric #else
280768693Seric 	return gethostbyname(name);
280868693Seric #endif
280964841Seric }
281064841Seric 
281164841Seric struct hostent *
281268693Seric sm_gethostbyaddr(addr, len, type)
281368707Seric 	char *addr;
281464841Seric 	int len;
281564841Seric 	int type;
281664841Seric {
281768693Seric #if defined(SOLARIS) && SOLARIS < 204
281868693Seric 	extern int h_errno;
281968693Seric 
282068508Seric # if SOLARIS == 203
282164841Seric 	static struct hostent hp;
282264841Seric 	static char buf[1000];
282364841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
282464841Seric 
282564841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
282664841Seric # else
282765009Seric 	extern struct hostent *__switch_gethostbyaddr();
282865009Seric 
282964841Seric 	return __switch_gethostbyaddr(addr, len, type);
283064841Seric # endif
283168693Seric #else
283268693Seric 	return gethostbyaddr(addr, len, type);
283368693Seric #endif
283464841Seric }
283568693Seric /*
283668693Seric **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
283768693Seric */
283864841Seric 
283968693Seric struct passwd *
284068693Seric sm_getpwnam(user)
284168704Seric 	char *user;
284268693Seric {
284368693Seric 	return getpwnam(user);
284468693Seric }
284568693Seric 
284668693Seric struct passwd *
284768693Seric sm_getpwuid(uid)
284868693Seric 	uid_t uid;
284968693Seric {
285068693Seric 	return getpwuid(uid);
285168693Seric }
285265214Seric /*
285365214Seric **  NI_PROPVAL -- netinfo property value lookup routine
285465214Seric **
285565214Seric **	Parameters:
285668508Seric **		keydir -- the Netinfo directory name in which to search
285768508Seric **			for the key.
285868508Seric **		keyprop -- the name of the property in which to find the
285968508Seric **			property we are interested.  Defaults to "name".
286068508Seric **		keyval -- the value for which we are really searching.
286168508Seric **		valprop -- the property name for the value in which we
286268508Seric **			are interested.
286368508Seric **		sepchar -- if non-nil, this can be multiple-valued, and
286468508Seric **			we should return a string separated by this
286568508Seric **			character.
286665214Seric **
286765214Seric **	Returns:
286865214Seric **		NULL -- if:
286965214Seric **			1. the directory is not found
287065214Seric **			2. the property name is not found
287165214Seric **			3. the property contains multiple values
287265214Seric **			4. some error occured
287365214Seric **		else -- the location of the config file.
287465214Seric **
287568508Seric **	Example:
287668508Seric **		To search for an alias value, use:
287768508Seric **		  ni_propval("/aliases", "name", aliasname, "members", ',')
287868508Seric **
287965214Seric **	Notes:
288065214Seric **      	Caller should free the return value of ni_proval
288165214Seric */
288265214Seric 
288365214Seric #ifdef NETINFO
288465214Seric 
288565214Seric # include <netinfo/ni.h>
288665214Seric 
288765214Seric # define LOCAL_NETINFO_DOMAIN    "."
288865214Seric # define PARENT_NETINFO_DOMAIN   ".."
288965214Seric # define MAX_NI_LEVELS           256
289065214Seric 
289165214Seric char *
289268508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar)
289368508Seric 	char *keydir;
289468508Seric 	char *keyprop;
289568508Seric 	char *keyval;
289668508Seric 	char *valprop;
289768508Seric 	char sepchar;
289865214Seric {
289965820Seric 	char *propval = NULL;
290065214Seric 	int i;
290168508Seric 	int j, alen;
290265214Seric 	void *ni = NULL;
290365214Seric 	void *lastni = NULL;
290465214Seric 	ni_status nis;
290565214Seric 	ni_id nid;
290665214Seric 	ni_namelist ninl;
290768508Seric 	register char *p;
290868508Seric 	char keybuf[1024];
290965214Seric 
291065214Seric 	/*
291168508Seric 	**  Create the full key from the two parts.
291268508Seric 	**
291368508Seric 	**	Note that directory can end with, e.g., "name=" to specify
291468508Seric 	**	an alternate search property.
291568508Seric 	*/
291668508Seric 
291768508Seric 	i = strlen(keydir) + strlen(keyval) + 2;
291868508Seric 	if (keyprop != NULL)
291968508Seric 		i += strlen(keyprop) + 1;
292068508Seric 	if (i > sizeof keybuf)
292168508Seric 		return NULL;
292268508Seric 	strcpy(keybuf, keydir);
292368508Seric 	strcat(keybuf, "/");
292468508Seric 	if (keyprop != NULL)
292568508Seric 	{
292668508Seric 		strcat(keybuf, keyprop);
292768508Seric 		strcat(keybuf, "=");
292868508Seric 	}
292968508Seric 	strcat(keybuf, keyval);
293068508Seric 
293168508Seric 	/*
293265214Seric 	**  If the passed directory and property name are found
293365214Seric 	**  in one of netinfo domains we need to search (starting
293465214Seric 	**  from the local domain moving all the way back to the
293565214Seric 	**  root domain) set propval to the property's value
293665214Seric 	**  and return it.
293765214Seric 	*/
293865214Seric 
293965214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
294065214Seric 	{
294165214Seric 		if (i == 0)
294265214Seric 		{
294365214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
294465214Seric 		}
294565214Seric 		else
294665214Seric 		{
294765214Seric 			if (lastni != NULL)
294865214Seric 				ni_free(lastni);
294965214Seric 			lastni = ni;
295065214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
295165214Seric 		}
295265214Seric 
295365214Seric 		/*
295465214Seric 		**  Don't bother if we didn't get a handle on a
295565214Seric 		**  proper domain.  This is not necessarily an error.
295665214Seric 		**  We would get a positive ni_status if, for instance
295765214Seric 		**  we never found the directory or property and tried
295865214Seric 		**  to open the parent of the root domain!
295965214Seric 		*/
296065214Seric 
296165214Seric 		if (nis != 0)
296265214Seric 			break;
296365214Seric 
296465214Seric 		/*
296565214Seric 		**  Find the path to the server information.
296665214Seric 		*/
296765214Seric 
296868508Seric 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
296965214Seric 			continue;
297065214Seric 
297165214Seric 		/*
297268508Seric 		**  Find associated value information.
297365214Seric 		*/
297465214Seric 
297568508Seric 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
297665214Seric 			continue;
297765214Seric 
297865214Seric 		/*
297968508Seric 		**  See if we have an acceptable number of values.
298065214Seric 		*/
298165214Seric 
298268508Seric 		if (ninl.ni_namelist_len <= 0)
298368508Seric 			continue;
298468508Seric 
298568508Seric 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
298665214Seric 		{
298768508Seric 			ni_namelist_free(&ninl);
298868508Seric 			continue;
298965214Seric 		}
299068508Seric 
299168508Seric 		/*
299268508Seric 		**  Calculate number of bytes needed and build result
299368508Seric 		*/
299468508Seric 
299568508Seric 		alen = 1;
299668508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
299768508Seric 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
299868508Seric 		propval = p = xalloc(alen);
299968508Seric 		for (j = 0; j < ninl.ni_namelist_len; j++)
300068508Seric 		{
300168508Seric 			strcpy(p, ninl.ni_namelist_val[j]);
300268508Seric 			p += strlen(p);
300368508Seric 			*p++ = sepchar;
300468508Seric 		}
300568508Seric 		*--p = '\0';
300668508Seric 
300768508Seric 		ni_namelist_free(&ninl);
300865214Seric 	}
300965214Seric 
301065214Seric 	/*
301165214Seric 	**  Clean up.
301265214Seric 	*/
301365214Seric 
301465214Seric 	if (ni != NULL)
301565214Seric 		ni_free(ni);
301665214Seric 	if (lastni != NULL && ni != lastni)
301765214Seric 		ni_free(lastni);
301865214Seric 
301965214Seric 	return propval;
302065214Seric }
302165214Seric 
302265214Seric #endif /* NETINFO */
302365504Seric /*
302465504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
302565504Seric **
302665504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
302765504Seric **	syslog succeeds during interrupt handlers.
302865504Seric */
302965504Seric 
303065504Seric #ifdef __hpux
303165504Seric 
303265504Seric # define MAXSYSLOGTRIES	100
303365504Seric # undef syslog
303465504Seric 
303565504Seric # ifdef __STDC__
303665504Seric hard_syslog(int pri, char *msg, ...)
303765504Seric # else
303865504Seric hard_syslog(pri, msg, va_alist)
303965504Seric 	int pri;
304065504Seric 	char *msg;
304165504Seric 	va_dcl
304265504Seric # endif
304365504Seric {
304465504Seric 	int i;
304565504Seric 	char buf[SYSLOG_BUFSIZE * 2];
304665504Seric 	VA_LOCAL_DECL;
304765504Seric 
304865504Seric 	VA_START(msg);
304965504Seric 	vsprintf(buf, msg, ap);
305065504Seric 	VA_END;
305165504Seric 
305265504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
305365504Seric 		continue;
305465504Seric }
305565504Seric 
305665504Seric #endif
305768696Seric /*
305868696Seric **  LOCAL_HOSTNAME_LENGTH
305968696Seric **
306068696Seric **	This is required to get sendmail to compile against BIND 4.9.x
306168696Seric **	on Ultrix.
306268696Seric */
306368696Seric 
306468696Seric #if defined(ultrix) && NAMED_BIND
306568696Seric 
306668696Seric # include <resolv.h>
306768696Seric # if __RES >= 19931104
306868696Seric 
306968696Seric int
307068696Seric local_hostname_length(hostname)
307168696Seric 	char *hostname;
307268696Seric {
307368696Seric 	int len_host, len_domain;
307468696Seric 
307568696Seric 	if (!*_res.defdname)
307668696Seric 		res_init();
307768696Seric 	len_host = strlen(hostname);
307868696Seric 	len_domain = strlen(_res.defdname);
307968696Seric 	if (len_host > len_domain &&
308068696Seric 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
308168696Seric 	    hostname[len_host - len_domain - 1] == '.')
308268696Seric 		return len_host - len_domain - 1;
308368696Seric 	else
308468696Seric 		return 0;
308568696Seric }
308668696Seric 
308768696Seric # endif
308868696Seric #endif
308968741Seric /*
309068741Seric **  Compile-Time options
309168741Seric */
309268741Seric 
309368741Seric char	*CompileOptions[] =
309468741Seric {
309568741Seric #if HESIOD
309668741Seric 	"HESIOD",
309768741Seric #endif
309868741Seric #if LOG
309968741Seric 	"LOG",
310068741Seric #endif
310168741Seric #if MATCHGECOS
310268741Seric 	"MATCHGECOS",
310368741Seric #endif
310468741Seric #if NAMED_BIND
310568741Seric 	"NAMED_BIND",
310668741Seric #endif
310768741Seric #if NDBM
310868741Seric 	"NDBM",
310968741Seric #endif
311068741Seric #if NETINET
311168741Seric 	"NETINET",
311268741Seric #endif
311368741Seric #if NETINFO
311468741Seric 	"NETINFO",
311568741Seric #endif
311668741Seric #if NETISO
311768741Seric 	"NETISO",
311868741Seric #endif
311968741Seric #if NETNS
312068741Seric 	"NETNS",
312168741Seric #endif
312268741Seric #if NETUNIX
312368741Seric 	"NETUNIX",
312468741Seric #endif
312568741Seric #if NETX25
312668741Seric 	"NETX25",
312768741Seric #endif
312868741Seric #if NEWDB
312968741Seric 	"NEWDB",
313068741Seric #endif
313168741Seric #if NIS
313268741Seric 	"NIS",
313368741Seric #endif
313468741Seric #if SCANF
313568741Seric 	"SCANF",
313668741Seric #endif
313768741Seric #if SUID_ROOT_FILES_OK
313868741Seric 	"SUID_ROOT_FILES_OK",
313968741Seric #endif
314068741Seric #if USERDB
314168741Seric 	"USERDB",
314268741Seric #endif
314368741Seric #if XDEBUG
314468741Seric 	"XDEBUG",
314568741Seric #endif
314668741Seric #if XLA
314768741Seric 	"XLA",
314868741Seric #endif
314968741Seric 	NULL
315068741Seric };
3151