xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 66893)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*66893Seric static char sccsid[] = "@(#)conf.c	8.89 (Berkeley) 04/18/94";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1765018Seric # include <netdb.h>
1836928Sbostic # include <pwd.h>
19404Seric 
20294Seric /*
213309Seric **  CONF.C -- Sendmail Configuration Tables.
22294Seric **
23294Seric **	Defines the configuration of this installation.
24294Seric **
251388Seric **	Configuration Variables:
262897Seric **		HdrInfo -- a table describing well-known header fields.
272897Seric **			Each entry has the field name and some flags,
284147Seric **			which are described in sendmail.h.
294093Seric **
304093Seric **	Notes:
314093Seric **		I have tried to put almost all the reasonable
324093Seric **		configuration information into the configuration
334093Seric **		file read at runtime.  My intent is that anything
344093Seric **		here is a function of the version of UNIX you
354093Seric **		are running, or is really static -- for example
364093Seric **		the headers are a superset of widely used
374093Seric **		protocols.  If you find yourself playing with
384093Seric **		this file too much, you may be making a mistake!
39294Seric */
40294Seric 
41294Seric 
42294Seric 
43294Seric 
444437Seric /*
452897Seric **  Header info table
463057Seric **	Final (null) entry contains the flags used for any other field.
474147Seric **
484147Seric **	Not all of these are actually handled specially by sendmail
494147Seric **	at this time.  They are included as placeholders, to let
504147Seric **	you know that "someday" I intend to have sendmail do
514147Seric **	something with them.
522897Seric */
532897Seric 
542897Seric struct hdrinfo	HdrInfo[] =
552897Seric {
568060Seric 		/* originator fields, most to least significant  */
5711417Seric 	"resent-sender",	H_FROM|H_RESENT,
5811417Seric 	"resent-from",		H_FROM|H_RESENT,
5925686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
609055Seric 	"sender",		H_FROM,
619055Seric 	"from",			H_FROM,
6225686Seric 	"reply-to",		H_FROM,
639055Seric 	"full-name",		H_ACHECK,
6464682Seric 	"return-receipt-to",	H_FROM|H_RECEIPTTO,
6557359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
689055Seric 	"to",			H_RCPT,
6911417Seric 	"resent-to",		H_RCPT|H_RESENT,
709055Seric 	"cc",			H_RCPT,
7111417Seric 	"resent-cc",		H_RCPT|H_RESENT,
729055Seric 	"bcc",			H_RCPT|H_ACHECK,
7311417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7456215Seric 	"apparently-to",	H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
7711417Seric 	"message-id",		0,
7811417Seric 	"resent-message-id",	H_RESENT,
799055Seric 	"message",		H_EOH,
809055Seric 	"text",			H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
8311417Seric 	"date",			0,
8411417Seric 	"resent-date",		H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
879055Seric 	"received",		H_TRACE|H_FORCE,
8859318Seric 	"x400-received",	H_TRACE|H_FORCE,
899055Seric 	"via",			H_TRACE|H_FORCE,
909055Seric 	"mail-from",		H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
9358796Seric 	"comments",		H_FORCE,
9463753Seric 	"return-path",		H_FORCE|H_ACHECK,
9558796Seric 
969055Seric 	NULL,			0,
972897Seric };
984166Seric 
994166Seric 
1004166Seric 
1014282Seric /*
1024282Seric **  Location of system files/databases/etc.
1034282Seric */
1044282Seric 
10558082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1069039Seric 
1079064Seric 
1089064Seric 
1099039Seric /*
11058082Seric **  Privacy values
11158082Seric */
11258082Seric 
11358082Seric struct prival PrivacyValues[] =
11458082Seric {
11558082Seric 	"public",		PRIV_PUBLIC,
11658082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11758114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11858082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11958082Seric 	"noexpn",		PRIV_NOEXPN,
12058082Seric 	"novrfy",		PRIV_NOVRFY,
12164333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12264333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12358789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12466783Seric 	"noreceipts",		PRIV_NORECEIPTS,
12558082Seric 	"goaway",		PRIV_GOAWAY,
12658789Seric 	NULL,			0,
12758082Seric };
12858082Seric 
12958082Seric 
13058082Seric 
13158082Seric /*
13224943Seric **  Miscellaneous stuff.
1339039Seric */
1349039Seric 
13524943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13664644Seric 
13764644Seric 
13864644Seric /*
13964644Seric **  Following should be config parameters (and probably will be in
14064644Seric **  future releases).  In the meantime, setting these is considered
14164644Seric **  unsupported, and is intentionally undocumented.
14264644Seric */
14364644Seric 
14464644Seric #ifdef BROKENSMTPPEERS
14564644Seric bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
14664644Seric #else
14764644Seric bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
14864644Seric #endif
14964644Seric #ifdef NOLOOPBACKCHECK
15064644Seric bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
15164644Seric #else
15264644Seric bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
15364644Seric #endif
15464644Seric 
15524943Seric /*
15624943Seric **  SETDEFAULTS -- set default values
15724943Seric **
15824943Seric **	Because of the way freezing is done, these must be initialized
15924943Seric **	using direct code.
16024943Seric **
16124943Seric **	Parameters:
16258734Seric **		e -- the default envelope.
16324943Seric **
16424943Seric **	Returns:
16524943Seric **		none.
16624943Seric **
16724943Seric **	Side Effects:
16824943Seric **		Initializes a bunch of global variables to their
16924943Seric **		default values.
17024943Seric */
17124943Seric 
17258737Seric #define DAYS		* 24 * 60 * 60
17358737Seric 
17458734Seric setdefaults(e)
17558734Seric 	register ENVELOPE *e;
17624943Seric {
17757438Seric 	SpaceSub = ' ';				/* option B */
17857438Seric 	QueueLA = 8;				/* option x */
17957438Seric 	RefuseLA = 12;				/* option X */
18057438Seric 	WkRecipFact = 30000L;			/* option y */
18157438Seric 	WkClassFact = 1800L;			/* option z */
18257438Seric 	WkTimeFact = 90000L;			/* option Z */
18357438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
18463787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
18557438Seric 						/* option F */
18657438Seric 	DefUid = 1;				/* option u */
18757438Seric 	DefGid = 1;				/* option g */
18857438Seric 	CheckpointInterval = 10;		/* option C */
18957438Seric 	MaxHopCount = 25;			/* option h */
19058734Seric 	e->e_sendmode = SM_FORK;		/* option d */
19158734Seric 	e->e_errormode = EM_PRINT;		/* option e */
19259709Seric 	SevenBit = FALSE;			/* option 7 */
19357438Seric 	MaxMciCache = 1;			/* option k */
19457438Seric 	MciCacheTimeout = 300;			/* option K */
19557438Seric 	LogLevel = 9;				/* option L */
19658112Seric 	settimeouts(NULL);			/* option r */
19758737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
19858737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
19958853Seric 	PrivacyFlags = 0;			/* option p */
20040973Sbostic 	setdefuser();
20153654Seric 	setupmaps();
20257402Seric 	setupmailers();
20324943Seric }
204294Seric 
20540973Sbostic 
2064326Seric /*
20740973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20840973Sbostic */
20940973Sbostic 
21040973Sbostic setdefuser()
21140973Sbostic {
21240973Sbostic 	struct passwd *defpwent;
21357386Seric 	static char defuserbuf[40];
21440973Sbostic 
21557386Seric 	DefUser = defuserbuf;
21640973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
21757386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21840973Sbostic 	else
21957386Seric 		strcpy(defuserbuf, "nobody");
22040973Sbostic }
22153654Seric /*
22256836Seric **  HOST_MAP_INIT -- initialize host class structures
22356836Seric */
22456836Seric 
22556836Seric bool
22660219Seric host_map_init(map, args)
22756836Seric 	MAP *map;
22856836Seric 	char *args;
22956836Seric {
23056836Seric 	register char *p = args;
23156836Seric 
23256836Seric 	for (;;)
23356836Seric 	{
23458050Seric 		while (isascii(*p) && isspace(*p))
23556836Seric 			p++;
23656836Seric 		if (*p != '-')
23756836Seric 			break;
23856836Seric 		switch (*++p)
23956836Seric 		{
24056836Seric 		  case 'a':
24156836Seric 			map->map_app = ++p;
24256836Seric 			break;
24356836Seric 		}
24458050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24556836Seric 			p++;
24656836Seric 		if (*p != '\0')
24756836Seric 			*p++ = '\0';
24856836Seric 	}
24956836Seric 	if (map->map_app != NULL)
25056836Seric 		map->map_app = newstr(map->map_app);
25156836Seric 	return TRUE;
25256836Seric }
25357402Seric /*
25457402Seric **  SETUPMAILERS -- initialize default mailers
25557402Seric */
25656836Seric 
25757402Seric setupmailers()
25857402Seric {
25957402Seric 	char buf[100];
26057402Seric 
26157403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
26257403Seric 	makemailer(buf);
26357403Seric 
26459883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
26557402Seric 	makemailer(buf);
26657402Seric 
26757402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26857402Seric 	makemailer(buf);
26957402Seric }
27056836Seric /*
27160207Seric **  SETUPMAPS -- set up map classes
27260207Seric */
27360207Seric 
27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27560207Seric 	{ \
27660207Seric 		extern bool parse __P((MAP *, char *)); \
27760207Seric 		extern bool open __P((MAP *, int)); \
27860207Seric 		extern void close __P((MAP *)); \
27960207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
28060207Seric 		extern void store __P((MAP *, char *, char *)); \
28160207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28260207Seric 		s->s_mapclass.map_cname = name; \
28360207Seric 		s->s_mapclass.map_ext = ext; \
28460207Seric 		s->s_mapclass.map_cflags = flags; \
28560207Seric 		s->s_mapclass.map_parse = parse; \
28660207Seric 		s->s_mapclass.map_open = open; \
28760207Seric 		s->s_mapclass.map_close = close; \
28860207Seric 		s->s_mapclass.map_lookup = lookup; \
28960207Seric 		s->s_mapclass.map_store = store; \
29060207Seric 	}
29160207Seric 
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);
30060207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30160207Seric 		map_parseargs, bt_map_open, db_map_close,
30260207Seric 		db_map_lookup, db_map_store);
30360207Seric #endif
30460207Seric 
30560207Seric #ifdef NDBM
30660207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30760207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30860207Seric 		ndbm_map_lookup, ndbm_map_store);
30960207Seric #endif
31060207Seric 
31160207Seric #ifdef NIS
31260207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31360207Seric 		map_parseargs, nis_map_open, nis_map_close,
31460207Seric 		nis_map_lookup, nis_map_store);
31560207Seric #endif
31660207Seric 
31760207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
31860207Seric 		map_parseargs, stab_map_open, stab_map_close,
31960207Seric 		stab_map_lookup, stab_map_store);
32060207Seric 
32160207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
32260207Seric 		map_parseargs, impl_map_open, impl_map_close,
32360207Seric 		impl_map_lookup, impl_map_store);
32460207Seric 
32560207Seric 	/* host DNS lookup */
32660207Seric 	MAPDEF("host", NULL, 0,
32760207Seric 		host_map_init, null_map_open, null_map_close,
32860207Seric 		host_map_lookup, null_map_store);
32960207Seric 
33060207Seric 	/* dequote map */
33160207Seric 	MAPDEF("dequote", NULL, 0,
33260207Seric 		dequote_init, null_map_open, null_map_close,
33360207Seric 		dequote_map, null_map_store);
33460207Seric 
33560207Seric #if 0
33660207Seric # ifdef USERDB
33760207Seric 	/* user database */
33860207Seric 	MAPDEF("udb", ".db", 0,
33960207Seric 		udb_map_parse, null_map_open, null_map_close,
34060207Seric 		udb_map_lookup, null_map_store);
34160207Seric # endif
34260207Seric #endif
34360207Seric }
34460207Seric 
34560207Seric #undef MAPDEF
34660207Seric /*
3479369Seric **  USERNAME -- return the user id of the logged in user.
3489369Seric **
3499369Seric **	Parameters:
3509369Seric **		none.
3519369Seric **
3529369Seric **	Returns:
3539369Seric **		The login name of the logged in user.
3549369Seric **
3559369Seric **	Side Effects:
3569369Seric **		none.
3579369Seric **
3589369Seric **	Notes:
3599369Seric **		The return value is statically allocated.
3609369Seric */
3619369Seric 
3629369Seric char *
3639369Seric username()
3649369Seric {
36517469Seric 	static char *myname = NULL;
3669369Seric 	extern char *getlogin();
36719904Smiriam 	register struct passwd *pw;
3689369Seric 
36917469Seric 	/* cache the result */
37017469Seric 	if (myname == NULL)
37117469Seric 	{
37217469Seric 		myname = getlogin();
37317469Seric 		if (myname == NULL || myname[0] == '\0')
37417469Seric 		{
37563787Seric 			pw = getpwuid(RealUid);
37617469Seric 			if (pw != NULL)
37740993Sbostic 				myname = newstr(pw->pw_name);
37817469Seric 		}
37919904Smiriam 		else
38019904Smiriam 		{
38163787Seric 			uid_t uid = RealUid;
38219873Smiriam 
38340993Sbostic 			myname = newstr(myname);
38440993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
38558736Seric 			      (uid != 0 && uid != pw->pw_uid))
38619904Smiriam 			{
38758736Seric 				pw = getpwuid(uid);
38824945Seric 				if (pw != NULL)
38940993Sbostic 					myname = newstr(pw->pw_name);
39019873Smiriam 			}
39119873Smiriam 		}
39217469Seric 		if (myname == NULL || myname[0] == '\0')
39317469Seric 		{
39458151Seric 			syserr("554 Who are you?");
39517469Seric 			myname = "postmaster";
39617469Seric 		}
39717469Seric 	}
39817469Seric 
39917469Seric 	return (myname);
4009369Seric }
4019369Seric /*
4024190Seric **  TTYPATH -- Get the path of the user's tty
403294Seric **
404294Seric **	Returns the pathname of the user's tty.  Returns NULL if
405294Seric **	the user is not logged in or if s/he has write permission
406294Seric **	denied.
407294Seric **
408294Seric **	Parameters:
409294Seric **		none
410294Seric **
411294Seric **	Returns:
412294Seric **		pathname of the user's tty.
413294Seric **		NULL if not logged in or write permission denied.
414294Seric **
415294Seric **	Side Effects:
416294Seric **		none.
417294Seric **
418294Seric **	WARNING:
419294Seric **		Return value is in a local buffer.
420294Seric **
421294Seric **	Called By:
422294Seric **		savemail
423294Seric */
424294Seric 
425294Seric char *
426294Seric ttypath()
427294Seric {
428294Seric 	struct stat stbuf;
429294Seric 	register char *pathn;
430294Seric 	extern char *ttyname();
4314081Seric 	extern char *getlogin();
432294Seric 
433294Seric 	/* compute the pathname of the controlling tty */
4349369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4359369Seric 	    (pathn = ttyname(0)) == NULL)
436294Seric 	{
437294Seric 		errno = 0;
438294Seric 		return (NULL);
439294Seric 	}
440294Seric 
441294Seric 	/* see if we have write permission */
4422967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
443294Seric 	{
444294Seric 		errno = 0;
445294Seric 		return (NULL);
446294Seric 	}
447294Seric 
448294Seric 	/* see if the user is logged in */
449294Seric 	if (getlogin() == NULL)
450294Seric 		return (NULL);
451294Seric 
452294Seric 	/* looks good */
453294Seric 	return (pathn);
454294Seric }
4552967Seric /*
4562967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4572967Seric **
4582967Seric **	This routine can be supplied on a per-installation basis
4592967Seric **	to determine whether a person is allowed to send a message.
4602967Seric **	This allows restriction of certain types of internet
4612967Seric **	forwarding or registration of users.
4622967Seric **
4632967Seric **	If the hosts are found to be incompatible, an error
46457454Seric **	message should be given using "usrerr" and 0 should
4652967Seric **	be returned.
4662967Seric **
4674288Seric **	'NoReturn' can be set to suppress the return-to-sender
4684288Seric **	function; this should be done on huge messages.
4694288Seric **
4702967Seric **	Parameters:
4712967Seric **		to -- the person being sent to.
4722967Seric **
4732967Seric **	Returns:
47457459Seric **		an exit status
4752967Seric **
4762967Seric **	Side Effects:
4772967Seric **		none (unless you include the usrerr stuff)
4782967Seric */
4792967Seric 
48055012Seric checkcompat(to, e)
4812967Seric 	register ADDRESS *to;
48255012Seric 	register ENVELOPE *e;
4832967Seric {
48412133Seric # ifdef lint
48512133Seric 	if (to == NULL)
48612133Seric 		to++;
48763834Seric # endif /* lint */
48864402Seric 
48964402Seric 	if (tTd(49, 1))
49064402Seric 		printf("checkcompat(to=%s, from=%s)\n",
49164402Seric 			to->q_paddr, e->e_from.q_paddr);
49264402Seric 
49310698Seric # ifdef EXAMPLE_CODE
49410698Seric 	/* this code is intended as an example only */
4954437Seric 	register STAB *s;
4964437Seric 
4974437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49855012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4999369Seric 	    to->q_mailer == s->s_mailer)
5004437Seric 	{
50158151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50210698Seric 		/* NoReturn = TRUE; to supress return copy */
50357459Seric 		return (EX_UNAVAILABLE);
5044437Seric 	}
50556795Seric # endif /* EXAMPLE_CODE */
50657459Seric 	return (EX_OK);
5072967Seric }
5089369Seric /*
50964035Seric **  SETSIGNAL -- set a signal handler
51064035Seric **
51164035Seric **	This is essentially old BSD "signal(3)".
51264035Seric */
51364035Seric 
51464561Seric sigfunc_t
51564035Seric setsignal(sig, handler)
51664035Seric 	int sig;
51764561Seric 	sigfunc_t handler;
51864035Seric {
51964380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
52064035Seric 	return signal(sig, handler);
52164035Seric #else
52264035Seric 	struct sigaction n, o;
52364035Seric 
52464035Seric 	bzero(&n, sizeof n);
52564035Seric 	n.sa_handler = handler;
52664035Seric 	if (sigaction(sig, &n, &o) < 0)
52764035Seric 		return SIG_ERR;
52864035Seric 	return o.sa_handler;
52964035Seric #endif
53064035Seric }
53164035Seric /*
5329369Seric **  HOLDSIGS -- arrange to hold all signals
5339369Seric **
5349369Seric **	Parameters:
5359369Seric **		none.
5369369Seric **
5379369Seric **	Returns:
5389369Seric **		none.
5399369Seric **
5409369Seric **	Side Effects:
5419369Seric **		Arranges that signals are held.
5429369Seric */
5439369Seric 
5449369Seric holdsigs()
5459369Seric {
5469369Seric }
5479369Seric /*
5489369Seric **  RLSESIGS -- arrange to release all signals
5499369Seric **
5509369Seric **	This undoes the effect of holdsigs.
5519369Seric **
5529369Seric **	Parameters:
5539369Seric **		none.
5549369Seric **
5559369Seric **	Returns:
5569369Seric **		none.
5579369Seric **
5589369Seric **	Side Effects:
5599369Seric **		Arranges that signals are released.
5609369Seric */
5619369Seric 
5629369Seric rlsesigs()
5639369Seric {
5649369Seric }
56514872Seric /*
56664705Seric **  INIT_MD -- do machine dependent initializations
56764705Seric **
56864705Seric **	Systems that have global modes that should be set should do
56964705Seric **	them here rather than in main.
57064705Seric */
57164705Seric 
57264705Seric #ifdef _AUX_SOURCE
57364705Seric # include	<compat.h>
57464705Seric #endif
57564705Seric 
57664825Seric init_md(argc, argv)
57764825Seric 	int argc;
57864825Seric 	char **argv;
57964705Seric {
58064705Seric #ifdef _AUX_SOURCE
58164705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
58264705Seric #endif
58364705Seric }
58464705Seric /*
58514872Seric **  GETLA -- get the current load average
58614872Seric **
58714881Seric **	This code stolen from la.c.
58814881Seric **
58914872Seric **	Parameters:
59014872Seric **		none.
59114872Seric **
59214872Seric **	Returns:
59314872Seric **		The current load average as an integer.
59414872Seric **
59514872Seric **	Side Effects:
59614872Seric **		none.
59714872Seric */
59814872Seric 
59951920Seric /* try to guess what style of load average we have */
60051920Seric #define LA_ZERO		1	/* always return load average as zero */
60164360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
60251920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
60351920Seric #define LA_SUBR		4	/* call getloadavg */
60464295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
60564360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
60666301Seric #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
60714872Seric 
60863962Seric /* do guesses based on general OS type */
60951920Seric #ifndef LA_TYPE
61063962Seric # define LA_TYPE	LA_ZERO
61151920Seric #endif
61251920Seric 
61364360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
61451920Seric 
61514872Seric #include <nlist.h>
61614872Seric 
61751920Seric #ifndef LA_AVENRUN
61863962Seric # ifdef SYSTEM5
61963962Seric #  define LA_AVENRUN	"avenrun"
62063962Seric # else
62163962Seric #  define LA_AVENRUN	"_avenrun"
62263962Seric # endif
62351920Seric #endif
62451920Seric 
62551920Seric /* _PATH_UNIX should be defined in <paths.h> */
62651920Seric #ifndef _PATH_UNIX
62763962Seric # if defined(SYSTEM5)
62863962Seric #  define _PATH_UNIX	"/unix"
62963962Seric # else
63063962Seric #  define _PATH_UNIX	"/vmunix"
63163962Seric # endif
63251920Seric #endif
63351920Seric 
63414872Seric struct	nlist Nl[] =
63514872Seric {
63651920Seric 	{ LA_AVENRUN },
63714872Seric #define	X_AVENRUN	0
63814872Seric 	{ 0 },
63914872Seric };
64014872Seric 
64160583Seric #ifndef FSHIFT
64260583Seric # if defined(unixpc)
64360583Seric #  define FSHIFT	5
64460583Seric # endif
64557736Seric 
64666763Seric # if defined(__alpha) || defined(IRIX)
64760583Seric #  define FSHIFT	10
64860583Seric # endif
64966788Seric #endif
65057977Seric 
65166788Seric #ifndef FSHIFT
65266788Seric # define FSHIFT		8
65357736Seric #endif
65460583Seric 
65566788Seric #ifndef FSCALE
65666788Seric # define FSCALE		(1 << FSHIFT)
65751920Seric #endif
65840930Srick 
65914872Seric getla()
66014872Seric {
66114872Seric 	static int kmem = -1;
66251920Seric #if LA_TYPE == LA_INT
66324943Seric 	long avenrun[3];
66451920Seric #else
66564360Seric # if LA_TYPE == LA_SHORT
66664360Seric 	short avenrun[3];
66764360Seric # else
66851920Seric 	double avenrun[3];
66964360Seric # endif
67051920Seric #endif
67125615Seric 	extern off_t lseek();
67257736Seric 	extern int errno;
67314872Seric 
67414872Seric 	if (kmem < 0)
67514872Seric 	{
67624945Seric 		kmem = open("/dev/kmem", 0, 0);
67714872Seric 		if (kmem < 0)
67857736Seric 		{
67957736Seric 			if (tTd(3, 1))
68057736Seric 				printf("getla: open(/dev/kmem): %s\n",
68157736Seric 					errstring(errno));
68214872Seric 			return (-1);
68357736Seric 		}
68451920Seric 		(void) fcntl(kmem, F_SETFD, 1);
68557736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68657736Seric 		{
68757736Seric 			if (tTd(3, 1))
68857736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68957736Seric 					errstring(errno));
69014872Seric 			return (-1);
69157736Seric 		}
69259253Seric 		if (Nl[X_AVENRUN].n_value == 0)
69359253Seric 		{
69459253Seric 			if (tTd(3, 1))
69559253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
69659253Seric 					_PATH_UNIX, LA_AVENRUN);
69759253Seric 			return (-1);
69859253Seric 		}
69966763Seric #ifdef IRIX
70066763Seric 		Nl[X_AVENRUN].n_value &= 0x7fffffff;
70166763Seric #endif
70214872Seric 	}
70357736Seric 	if (tTd(3, 20))
70457736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
70524945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
70623118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
70719967Seric 	{
70819967Seric 		/* thank you Ian */
70957736Seric 		if (tTd(3, 1))
71057736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
71119967Seric 		return (-1);
71219967Seric 	}
71364360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
71457736Seric 	if (tTd(3, 5))
71557736Seric 	{
71657736Seric 		printf("getla: avenrun = %d", avenrun[0]);
71757736Seric 		if (tTd(3, 15))
71857736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71957736Seric 		printf("\n");
72057736Seric 	}
72157736Seric 	if (tTd(3, 1))
72257736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
72324943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
72451920Seric #else
72557736Seric 	if (tTd(3, 5))
72657736Seric 	{
72757736Seric 		printf("getla: avenrun = %g", avenrun[0]);
72857736Seric 		if (tTd(3, 15))
72957736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
73057736Seric 		printf("\n");
73157736Seric 	}
73257736Seric 	if (tTd(3, 1))
73357736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
73451920Seric 	return ((int) (avenrun[0] + 0.5));
73551920Seric #endif
73614872Seric }
73714872Seric 
73851773Seric #else
73951920Seric #if LA_TYPE == LA_SUBR
74051773Seric 
74164718Seric #ifdef DGUX
74264718Seric 
74364718Seric #include <sys/dg_sys_info.h>
74464718Seric 
74564718Seric int getla()
74664718Seric {
74764718Seric 	struct dg_sys_info_load_info load_info;
74864718Seric 
74964718Seric 	dg_sys_info((long *)&load_info,
75064718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
75164718Seric 
75264718Seric 	return((int) (load_info.one_minute + 0.5));
75364718Seric }
75464718Seric 
75564718Seric #else
75664718Seric 
75751773Seric getla()
75851773Seric {
75951920Seric 	double avenrun[3];
76051920Seric 
76151920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
76257736Seric 	{
76357736Seric 		if (tTd(3, 1))
76457736Seric 			perror("getla: getloadavg failed:");
76551920Seric 		return (-1);
76657736Seric 	}
76757736Seric 	if (tTd(3, 1))
76857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
76951920Seric 	return ((int) (avenrun[0] + 0.5));
77051773Seric }
77151773Seric 
77264718Seric #endif /* DGUX */
77351773Seric #else
77464295Seric #if LA_TYPE == LA_MACH
77551773Seric 
77664295Seric /*
77765173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
77864295Seric */
77964295Seric 
78065173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
78165173Seric # include <mach/mach.h>
78265173Seric #else
78365173Seric # include <mach.h>
78465173Seric #endif
78564295Seric 
78651773Seric getla()
78751773Seric {
78864295Seric 	processor_set_t default_set;
78964295Seric 	kern_return_t error;
79064295Seric 	unsigned int info_count;
79164295Seric 	struct processor_set_basic_info info;
79264295Seric 	host_t host;
79364295Seric 
79464295Seric 	error = processor_set_default(host_self(), &default_set);
79564295Seric 	if (error != KERN_SUCCESS)
79664295Seric 		return -1;
79764295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
79864295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
79964295Seric 			       &host, (processor_set_info_t)&info,
80064295Seric 			       &info_count) != KERN_SUCCESS)
80164295Seric 	{
80264295Seric 		return -1;
80364295Seric 	}
80464295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
80564295Seric }
80664295Seric 
80764295Seric 
80864295Seric #else
80966313Seric #if LA_TYPE == LA_PROCSTR
81064295Seric 
81166301Seric /*
81266301Seric **  Read /proc/loadavg for the load average.  This is assumed to be
81366301Seric **  in a format like "0.15 0.12 0.06".
81466301Seric **
81566301Seric **	Initially intended for Linux.  This has been in the kernel
81666301Seric **	since at least 0.99.15.
81766301Seric */
81866301Seric 
81966301Seric # ifndef _PATH_LOADAVG
82066301Seric #  define _PATH_LOADAVG	"/proc/loadavg"
82166301Seric # endif
82266301Seric 
82366301Seric int
82464295Seric getla()
82564295Seric {
82666319Seric 	double avenrun;
82766301Seric 	register int result;
82866319Seric 	FILE *fp;
82966301Seric 
83066319Seric 	fp = fopen(_PATH_LOADAVG, "r");
83166319Seric 	if (fp == NULL)
83266301Seric 	{
83366319Seric 		if (tTd(3, 1))
83466319Seric 			printf("getla: fopen(%s): %s\n",
83566319Seric 				_PATH_LOADAVG, errstring(errno));
83666319Seric 		return -1;
83766301Seric 	}
83866301Seric 	result = fscanf(fp, "%lf", &avenrun);
83966319Seric 	fclose(fp);
84066301Seric 	if (result != 1)
84166301Seric 	{
84266310Seric 		if (tTd(3, 1))
84366301Seric 			printf("getla: fscanf() = %d: %s\n",
84466301Seric 				result, errstring(errno));
84566301Seric 		return -1;
84666301Seric 	}
84766301Seric 
84857736Seric 	if (tTd(3, 1))
84966301Seric 		printf("getla(): %.2f\n", avenrun);
85066301Seric 
85166301Seric 	return ((int) (avenrun + 0.5));
85266301Seric }
85366301Seric 
85466301Seric #else
85566301Seric 
85666301Seric getla()
85766301Seric {
85866301Seric 	if (tTd(3, 1))
85957736Seric 		printf("getla: ZERO\n");
86051920Seric 	return (0);
86151773Seric }
86251773Seric 
86351773Seric #endif
86451773Seric #endif
86564295Seric #endif
86666301Seric #endif
86766045Seric 
86866045Seric 
86966045Seric /*
87066045Seric  * Copyright 1989 Massachusetts Institute of Technology
87166045Seric  *
87266045Seric  * Permission to use, copy, modify, distribute, and sell this software and its
87366045Seric  * documentation for any purpose is hereby granted without fee, provided that
87466045Seric  * the above copyright notice appear in all copies and that both that
87566045Seric  * copyright notice and this permission notice appear in supporting
87666045Seric  * documentation, and that the name of M.I.T. not be used in advertising or
87766045Seric  * publicity pertaining to distribution of the software without specific,
87866045Seric  * written prior permission.  M.I.T. makes no representations about the
87966045Seric  * suitability of this software for any purpose.  It is provided "as is"
88066045Seric  * without express or implied warranty.
88166045Seric  *
88266045Seric  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
88366045Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
88466045Seric  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
88566045Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
88666045Seric  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
88766045Seric  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
88866045Seric  *
88966045Seric  * Authors:  Many and varied...
89066045Seric  */
89166045Seric 
89266045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */
89366045Seric #ifndef lint
89466045Seric static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
89566045Seric #endif /* !lint */
89666045Seric 
89766179Seric #ifdef apollo
89866045Seric # undef volatile
89966045Seric #    include <apollo/base.h>
90066045Seric 
90166045Seric /* ARGSUSED */
90266045Seric int getloadavg( call_data )
90366045Seric      caddr_t	call_data;	/* pointer to (double) return value */
90466045Seric {
90566045Seric      double *avenrun = (double *) call_data;
90666045Seric      int i;
90766045Seric      status_$t      st;
90866045Seric      long loadav[3];
90966045Seric      proc1_$get_loadav(loadav, &st);
91066045Seric      *avenrun = loadav[0] / (double) (1 << 16);
91166045Seric      return(0);
91266045Seric }
91366045Seric #   endif /* apollo */
91424943Seric /*
91524943Seric **  SHOULDQUEUE -- should this message be queued or sent?
91624943Seric **
91724943Seric **	Compares the message cost to the load average to decide.
91824943Seric **
91924943Seric **	Parameters:
92024943Seric **		pri -- the priority of the message in question.
92157438Seric **		ctime -- the message creation time.
92224943Seric **
92324943Seric **	Returns:
92424943Seric **		TRUE -- if this message should be queued up for the
92524943Seric **			time being.
92624943Seric **		FALSE -- if the load is low enough to send this message.
92724943Seric **
92824943Seric **	Side Effects:
92924943Seric **		none.
93024943Seric */
93124943Seric 
93224943Seric bool
93357438Seric shouldqueue(pri, ctime)
93424943Seric 	long pri;
93557438Seric 	time_t ctime;
93624943Seric {
93751920Seric 	if (CurrentLA < QueueLA)
93824943Seric 		return (FALSE);
93958132Seric 	if (CurrentLA >= RefuseLA)
94058132Seric 		return (TRUE);
94151920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
94224943Seric }
94324943Seric /*
94453037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
94553037Seric **
94653037Seric **	Parameters:
94753037Seric **		none.
94853037Seric **
94953037Seric **	Returns:
95053037Seric **		TRUE if incoming SMTP connections should be refused
95153037Seric **			(for now).
95253037Seric **		FALSE if we should accept new work.
95353037Seric **
95453037Seric **	Side Effects:
95553037Seric **		none.
95653037Seric */
95753037Seric 
95853037Seric bool
95953037Seric refuseconnections()
96053037Seric {
96159156Seric #ifdef XLA
96259156Seric 	if (!xla_smtp_ok())
96359156Seric 		return TRUE;
96459156Seric #endif
96559156Seric 
96653037Seric 	/* this is probably too simplistic */
96758132Seric 	return (CurrentLA >= RefuseLA);
96853037Seric }
96953037Seric /*
97024943Seric **  SETPROCTITLE -- set process title for ps
97124943Seric **
97224943Seric **	Parameters:
97358674Seric **		fmt -- a printf style format string.
97458674Seric **		a, b, c -- possible parameters to fmt.
97524943Seric **
97624943Seric **	Returns:
97724943Seric **		none.
97824943Seric **
97924943Seric **	Side Effects:
98024943Seric **		Clobbers argv of our main procedure so ps(1) will
98124943Seric **		display the title.
98224943Seric */
98324943Seric 
98458689Seric #ifdef SETPROCTITLE
98566002Seric # ifdef HASSETPROCTITLE
98666002Seric    *** ERROR ***  Cannot have both SETPROCTITLE and HASSETPROCTITLE defined
98766002Seric # endif
98858689Seric # ifdef __hpux
98958689Seric #  include <sys/pstat.h>
99058689Seric # endif
99159732Seric # ifdef BSD4_4
99259732Seric #  include <machine/vmparam.h>
99359732Seric #  include <sys/exec.h>
99464247Seric #  ifdef __bsdi__
99564247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
99664843Seric #   define PROCTITLEPAD	'\0'
99764247Seric #  endif
99859992Seric #  ifdef PS_STRINGS
99959992Seric #   define SETPROC_STATIC static
100059992Seric #  endif
100159732Seric # endif
100259732Seric # ifndef SETPROC_STATIC
100359732Seric #  define SETPROC_STATIC
100459732Seric # endif
100558689Seric #endif
100658689Seric 
100764843Seric #ifndef PROCTITLEPAD
100864843Seric # define PROCTITLEPAD	' '
100964843Seric #endif
101064843Seric 
101165982Seric #ifndef HASSETPROCTITLE
101265982Seric 
101324943Seric /*VARARGS1*/
101457642Seric #ifdef __STDC__
101557642Seric setproctitle(char *fmt, ...)
101657642Seric #else
101757642Seric setproctitle(fmt, va_alist)
101824943Seric 	char *fmt;
101957642Seric 	va_dcl
102057642Seric #endif
102124943Seric {
102224943Seric # ifdef SETPROCTITLE
102324943Seric 	register char *p;
102425049Seric 	register int i;
102559732Seric 	SETPROC_STATIC char buf[MAXLINE];
102656852Seric 	VA_LOCAL_DECL
102758689Seric #  ifdef __hpux
102858689Seric 	union pstun pst;
102958689Seric #  endif
103024943Seric 	extern char **Argv;
103124943Seric 	extern char *LastArgv;
103224943Seric 
103358674Seric 	p = buf;
103424943Seric 
103558674Seric 	/* print sendmail: heading for grep */
103658674Seric 	(void) strcpy(p, "sendmail: ");
103758674Seric 	p += strlen(p);
103824943Seric 
103958674Seric 	/* print the argument string */
104058674Seric 	VA_START(fmt);
104158674Seric 	(void) vsprintf(p, fmt, ap);
104256852Seric 	VA_END;
104354996Seric 
104458674Seric 	i = strlen(buf);
104558689Seric 
104658689Seric #  ifdef __hpux
104758689Seric 	pst.pst_command = buf;
104858689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
104958689Seric #  else
105059992Seric #   ifdef PS_STRINGS
105159732Seric 	PS_STRINGS->ps_nargvstr = 1;
105259732Seric 	PS_STRINGS->ps_argvstr = buf;
105359732Seric #   else
105454996Seric 	if (i > LastArgv - Argv[0] - 2)
105525049Seric 	{
105654996Seric 		i = LastArgv - Argv[0] - 2;
105758674Seric 		buf[i] = '\0';
105825049Seric 	}
105958674Seric 	(void) strcpy(Argv[0], buf);
106054997Seric 	p = &Argv[0][i];
106124943Seric 	while (p < LastArgv)
106264843Seric 		*p++ = PROCTITLEPAD;
106359732Seric #   endif
106458689Seric #  endif
106556795Seric # endif /* SETPROCTITLE */
106624943Seric }
106765982Seric 
106865982Seric #endif
106925698Seric /*
107025698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
107125698Seric **
107225698Seric **	Parameters:
107325698Seric **		none.
107425698Seric **
107525698Seric **	Returns:
107625698Seric **		none.
107725698Seric **
107825698Seric **	Side Effects:
107925698Seric **		Picks up extant zombies.
108025698Seric */
108125698Seric 
108246928Sbostic void
108325698Seric reapchild()
108425698Seric {
108564738Seric 	int olderrno = errno;
108663966Seric # ifdef HASWAITPID
108760219Seric 	auto int status;
108860560Seric 	int count;
108960560Seric 	int pid;
109060219Seric 
109160560Seric 	count = 0;
109260560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
109360560Seric 	{
109460560Seric 		if (count++ > 1000)
109560560Seric 		{
109666748Seric #ifdef LOG
109760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
109860560Seric 				pid, status);
109966748Seric #endif
110060560Seric 			break;
110160560Seric 		}
110260560Seric 	}
110360219Seric # else
110425698Seric # ifdef WNOHANG
110525698Seric 	union wait status;
110625698Seric 
110763753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
110825698Seric 		continue;
110956795Seric # else /* WNOHANG */
111025698Seric 	auto int status;
111125698Seric 
111260219Seric 	while (wait(&status) > 0)
111325698Seric 		continue;
111456795Seric # endif /* WNOHANG */
111560219Seric # endif
111664035Seric # ifdef SYS5SIGNALS
111764035Seric 	(void) setsignal(SIGCHLD, reapchild);
111858061Seric # endif
111964738Seric 	errno = olderrno;
112025698Seric }
112155418Seric /*
112255418Seric **  UNSETENV -- remove a variable from the environment
112355418Seric **
112455418Seric **	Not needed on newer systems.
112555418Seric **
112655418Seric **	Parameters:
112755418Seric **		name -- the string name of the environment variable to be
112855418Seric **			deleted from the current environment.
112955418Seric **
113055418Seric **	Returns:
113155418Seric **		none.
113255418Seric **
113355418Seric **	Globals:
113455418Seric **		environ -- a pointer to the current environment.
113555418Seric **
113655418Seric **	Side Effects:
113755418Seric **		Modifies environ.
113855418Seric */
113955418Seric 
114063962Seric #ifndef HASUNSETENV
114155418Seric 
114255418Seric void
114355418Seric unsetenv(name)
114455418Seric 	char *name;
114555418Seric {
114655418Seric 	extern char **environ;
114755418Seric 	register char **pp;
114855418Seric 	int len = strlen(name);
114955418Seric 
115055418Seric 	for (pp = environ; *pp != NULL; pp++)
115155418Seric 	{
115255418Seric 		if (strncmp(name, *pp, len) == 0 &&
115355418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
115455418Seric 			break;
115555418Seric 	}
115655418Seric 
115755418Seric 	for (; *pp != NULL; pp++)
115855418Seric 		*pp = pp[1];
115955418Seric }
116055418Seric 
116163962Seric #endif
116256215Seric /*
116356215Seric **  GETDTABLESIZE -- return number of file descriptors
116456215Seric **
116556215Seric **	Only on non-BSD systems
116656215Seric **
116756215Seric **	Parameters:
116856215Seric **		none
116956215Seric **
117056215Seric **	Returns:
117156215Seric **		size of file descriptor table
117256215Seric **
117356215Seric **	Side Effects:
117456215Seric **		none
117556215Seric */
117656215Seric 
117763787Seric #ifdef SOLARIS
117863787Seric # include <sys/resource.h>
117963787Seric #endif
118056215Seric 
118156215Seric int
118263787Seric getdtsize()
118356215Seric {
118463787Seric #ifdef RLIMIT_NOFILE
118563787Seric 	struct rlimit rl;
118663787Seric 
118763787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
118863787Seric 		return rl.rlim_cur;
118963787Seric #endif
119063787Seric 
119164031Seric # ifdef HASGETDTABLESIZE
119264031Seric 	return getdtablesize();
119364031Seric # else
119464031Seric #  ifdef _SC_OPEN_MAX
119558689Seric 	return sysconf(_SC_OPEN_MAX);
119663787Seric #  else
119756215Seric 	return NOFILE;
119863787Seric #  endif
119958689Seric # endif
120056215Seric }
120157631Seric /*
120257631Seric **  UNAME -- get the UUCP name of this system.
120357631Seric */
120457631Seric 
120557943Seric #ifndef HASUNAME
120657631Seric 
120757631Seric int
120857631Seric uname(name)
120957631Seric 	struct utsname *name;
121057631Seric {
121157631Seric 	FILE *file;
121257631Seric 	char *n;
121357631Seric 
121457631Seric 	name->nodename[0] = '\0';
121557631Seric 
121657661Seric 	/* try /etc/whoami -- one line with the node name */
121757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
121857631Seric 	{
121957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
122057631Seric 		(void) fclose(file);
122157661Seric 		n = strchr(name->nodename, '\n');
122257631Seric 		if (n != NULL)
122357631Seric 			*n = '\0';
122457631Seric 		if (name->nodename[0] != '\0')
122557631Seric 			return (0);
122657631Seric 	}
122757631Seric 
122857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
122957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
123057631Seric 	{
123157631Seric 		char buf[MAXLINE];
123257631Seric 
123357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
123457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
123557631Seric 					NODE_LENGTH, name->nodename) > 0)
123657631Seric 				break;
123757631Seric 		(void) fclose(file);
123857631Seric 		if (name->nodename[0] != '\0')
123957631Seric 			return (0);
124057631Seric 	}
124157631Seric 
124257631Seric #ifdef TRUST_POPEN
124357631Seric 	/*
124457631Seric 	**  Popen is known to have security holes.
124557631Seric 	*/
124657631Seric 
124757661Seric 	/* try uuname -l to return local name */
124857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
124957631Seric 	{
125057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
125157631Seric 		(void) pclose(file);
125257661Seric 		n = strchr(name, '\n');
125357631Seric 		if (n != NULL)
125457631Seric 			*n = '\0';
125557661Seric 		if (name->nodename[0] != '\0')
125657631Seric 			return (0);
125757631Seric 	}
125857631Seric #endif
125957631Seric 
126057631Seric 	return (-1);
126157631Seric }
126257943Seric #endif /* HASUNAME */
126358068Seric /*
126458068Seric **  INITGROUPS -- initialize groups
126558068Seric **
126658068Seric **	Stub implementation for System V style systems
126758068Seric */
126858068Seric 
126958068Seric #ifndef HASINITGROUPS
127058068Seric 
127158068Seric initgroups(name, basegid)
127258068Seric 	char *name;
127358068Seric 	int basegid;
127458068Seric {
127558068Seric 	return 0;
127658068Seric }
127758068Seric 
127858068Seric #endif
127958082Seric /*
128059289Seric **  SETSID -- set session id (for non-POSIX systems)
128159289Seric */
128259289Seric 
128359289Seric #ifndef HASSETSID
128459289Seric 
128563753Seric pid_t
128663753Seric setsid __P ((void))
128759289Seric {
128864155Seric #ifdef TIOCNOTTY
128964155Seric 	int fd;
129064155Seric 
129166743Seric 	fd = open("/dev/tty", O_RDWR, 0);
129264155Seric 	if (fd >= 0)
129364155Seric 	{
129464155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
129564155Seric 		(void) close(fd);
129664155Seric 	}
129764155Seric #endif /* TIOCNOTTY */
129864705Seric # ifdef SYS5SETPGRP
129963753Seric 	return setpgrp();
130063753Seric # else
130164155Seric 	return setpgid(0, getpid());
130259289Seric # endif
130359289Seric }
130459289Seric 
130559289Seric #endif
130659289Seric /*
130766748Seric **  FSYNC -- dummy fsync
130866748Seric */
130966748Seric 
131066748Seric #ifdef NEEDFSYNC
131166748Seric 
131266748Seric fsync(fd)
131366748Seric 	int fd;
131466748Seric {
131566748Seric # ifdef O_SYNC
131666748Seric 	return fcntl(fd, F_SETFL, O_SYNC);
131766748Seric # else
131866748Seric 	/* nothing we can do */
131966748Seric 	return 0;
132066748Seric # endif
132166748Seric }
132266748Seric 
132366748Seric #endif
132466748Seric /*
132564718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
132664718Seric **
132764718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
132864718Seric **	instead of a long.  This patches things.
132964718Seric */
133064718Seric 
133164718Seric #ifdef DGUX
133264718Seric 
133364718Seric #undef inet_addr
133464718Seric 
133564718Seric long
133664718Seric dgux_inet_addr(host)
133764718Seric 	char *host;
133864718Seric {
133964718Seric 	struct in_addr haddr;
134064718Seric 
134164718Seric 	haddr = inet_addr(host);
134264718Seric 	return haddr.s_addr;
134364718Seric }
134464718Seric 
134564718Seric #endif
134664718Seric /*
134763969Seric **  GETOPT -- for old systems or systems with bogus implementations
134863969Seric */
134963969Seric 
135063969Seric #ifdef NEEDGETOPT
135163969Seric 
135263969Seric /*
135363969Seric  * Copyright (c) 1985 Regents of the University of California.
135463969Seric  * All rights reserved.  The Berkeley software License Agreement
135563969Seric  * specifies the terms and conditions for redistribution.
135663969Seric  */
135763969Seric 
135863969Seric 
135963969Seric /*
136063969Seric ** this version hacked to add `atend' flag to allow state machine
136163969Seric ** to reset if invoked by the program to scan args for a 2nd time
136263969Seric */
136363969Seric 
136463969Seric #if defined(LIBC_SCCS) && !defined(lint)
136563969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
136664072Seric #endif /* LIBC_SCCS and not lint */
136763969Seric 
136863969Seric #include <stdio.h>
136963969Seric 
137063969Seric /*
137163969Seric  * get option letter from argument vector
137263969Seric  */
137365352Seric #ifdef _CONVEX_SOURCE
137465352Seric extern int	optind, opterr;
137565352Seric #else
137665352Seric int	opterr = 1;		/* if error message should be printed */
137765352Seric int	optind = 1;		/* index into parent argv vector */
137865352Seric #endif
137965352Seric int	optopt;			/* character checked for validity */
138063969Seric char	*optarg;		/* argument associated with option */
138163969Seric 
138263969Seric #define BADCH	(int)'?'
138363969Seric #define EMSG	""
138463969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
138563969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
138663969Seric 
138763969Seric getopt(nargc,nargv,ostr)
138865160Seric 	int		nargc;
138965160Seric 	char *const	*nargv;
139065160Seric 	const char	*ostr;
139163969Seric {
139263969Seric 	static char	*place = EMSG;	/* option letter processing */
139363969Seric 	static char	atend = 0;
139463969Seric 	register char	*oli;		/* option letter list index */
139563969Seric 
139663969Seric 	if (atend) {
139763969Seric 		atend = 0;
139863969Seric 		place = EMSG;
139963969Seric 	}
140063969Seric 	if(!*place) {			/* update scanning pointer */
140164136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
140263969Seric 			atend++;
140363969Seric 			return(EOF);
140463969Seric 		}
140563969Seric 		if (*place == '-') {	/* found "--" */
140663969Seric 			++optind;
140763969Seric 			atend++;
140863969Seric 			return(EOF);
140963969Seric 		}
141063969Seric 	}				/* option letter okay? */
141164125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
141264136Seric 		if (!*place) ++optind;
141363969Seric 		tell(": illegal option -- ");
141463969Seric 	}
141563969Seric 	if (*++oli != ':') {		/* don't need argument */
141663969Seric 		optarg = NULL;
141763969Seric 		if (!*place) ++optind;
141863969Seric 	}
141963969Seric 	else {				/* need an argument */
142063969Seric 		if (*place) optarg = place;	/* no white space */
142163969Seric 		else if (nargc <= ++optind) {	/* no arg */
142263969Seric 			place = EMSG;
142363969Seric 			tell(": option requires an argument -- ");
142463969Seric 		}
142563969Seric 	 	else optarg = nargv[optind];	/* white space */
142663969Seric 		place = EMSG;
142763969Seric 		++optind;
142863969Seric 	}
142963969Seric 	return(optopt);			/* dump back option letter */
143063969Seric }
143163969Seric 
143263969Seric #endif
143363969Seric /*
143463969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
143563969Seric */
143663969Seric 
143763969Seric #ifdef NEEDVPRINTF
143863969Seric 
143963969Seric #define MAXARG	16
144063969Seric 
144163969Seric vfprintf(fp, fmt, ap)
144263969Seric 	FILE *	fp;
144363969Seric 	char *	fmt;
144463969Seric 	char **	ap;
144563969Seric {
144663969Seric 	char *	bp[MAXARG];
144763969Seric 	int	i = 0;
144863969Seric 
144963969Seric 	while (*ap && i < MAXARG)
145063969Seric 		bp[i++] = *ap++;
145163969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
145263969Seric 			 bp[4], bp[5], bp[6], bp[7],
145363969Seric 			 bp[8], bp[9], bp[10], bp[11],
145463969Seric 			 bp[12], bp[13], bp[14], bp[15]);
145563969Seric }
145663969Seric 
145763969Seric vsprintf(s, fmt, ap)
145863969Seric 	char *	s;
145963969Seric 	char *	fmt;
146063969Seric 	char **	ap;
146163969Seric {
146263969Seric 	char *	bp[MAXARG];
146363969Seric 	int	i = 0;
146463969Seric 
146563969Seric 	while (*ap && i < MAXARG)
146663969Seric 		bp[i++] = *ap++;
146763969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
146863969Seric 			bp[4], bp[5], bp[6], bp[7],
146963969Seric 			bp[8], bp[9], bp[10], bp[11],
147063969Seric 			bp[12], bp[13], bp[14], bp[15]);
147163969Seric }
147263969Seric 
147363969Seric #endif
147463969Seric /*
147565220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
147665208Seric **
147765211Seric **	Parameters:
147865211Seric **		shell -- the user's shell from /etc/passwd
147965211Seric **
148065211Seric **	Returns:
148165211Seric **		TRUE -- if it is ok to use this for unrestricted access.
148265211Seric **		FALSE -- if the shell is restricted.
148365208Seric */
148465208Seric 
148565587Seric #if !HASGETUSERSHELL
148665587Seric 
148765587Seric # ifndef _PATH_SHELLS
148865587Seric #  define _PATH_SHELLS	"/etc/shells"
148965587Seric # endif
149065587Seric 
149165587Seric char	*DefaultUserShells[] =
149265587Seric {
149365587Seric 	"/bin/sh",
149465587Seric 	"/usr/bin/sh",
149565587Seric 	"/bin/csh",
149665587Seric 	"/usr/bin/csh",
149765587Seric #ifdef __hpux
149865587Seric 	"/bin/rsh",
149965587Seric 	"/bin/ksh",
150065587Seric 	"/bin/rksh",
150165587Seric 	"/bin/pam",
150265587Seric 	"/usr/bin/keysh",
150365587Seric 	"/bin/posix/sh",
150465211Seric #endif
150565587Seric 	NULL
150665587Seric };
150765587Seric 
150865208Seric #endif
150965208Seric 
151065747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
151165747Seric 
151265211Seric bool
151365211Seric usershellok(shell)
151465211Seric 	char *shell;
151565208Seric {
151665211Seric #if HASGETUSERSHELL
151765211Seric 	register char *p;
151865211Seric 	extern char *getusershell();
151965208Seric 
152065211Seric 	setusershell();
152165211Seric 	while ((p = getusershell()) != NULL)
152265747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
152365211Seric 			break;
152465211Seric 	endusershell();
152565211Seric 	return p != NULL;
152665211Seric #else
152765211Seric 	register FILE *shellf;
152865211Seric 	char buf[MAXLINE];
152965208Seric 
153065211Seric 	shellf = fopen(_PATH_SHELLS, "r");
153165211Seric 	if (shellf == NULL)
153265211Seric 	{
153365211Seric 		/* no /etc/shells; see if it is one of the std shells */
153465587Seric 		char **d;
153565587Seric 
153665587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
153765587Seric 		{
153865587Seric 			if (strcmp(shell, *d) == 0)
153965587Seric 				return TRUE;
154065587Seric 		}
154165587Seric 		return FALSE;
154265211Seric 	}
154365208Seric 
154465211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
154565211Seric 	{
154665211Seric 		register char *p, *q;
154765208Seric 
154865211Seric 		p = buf;
154965211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
155065211Seric 			p++;
155165211Seric 		if (*p == '#' || *p == '\0')
155265208Seric 			continue;
155365211Seric 		q = p;
155465211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
155565211Seric 			p++;
155665211Seric 		*p = '\0';
155765747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
155865211Seric 		{
155965211Seric 			fclose(shellf);
156065211Seric 			return TRUE;
156165211Seric 		}
156265208Seric 	}
156365211Seric 	fclose(shellf);
156465211Seric 	return FALSE;
156565211Seric #endif
156665208Seric }
156765208Seric /*
156864311Seric **  FREESPACE -- see how much free space is on the queue filesystem
156958082Seric **
157058082Seric **	Only implemented if you have statfs.
157158082Seric **
157258082Seric **	Parameters:
157364311Seric **		dir -- the directory in question.
157464311Seric **		bsize -- a variable into which the filesystem
157564311Seric **			block size is stored.
157658082Seric **
157758082Seric **	Returns:
157864311Seric **		The number of bytes free on the queue filesystem.
157964311Seric **		-1 if the statfs call fails.
158064311Seric **
158164311Seric **	Side effects:
158264311Seric **		Puts the filesystem block size into bsize.
158358082Seric */
158458082Seric 
158565749Seric /* statfs types */
158665749Seric #define SFS_NONE	0	/* no statfs implementation */
158765749Seric #define SFS_USTAT	1	/* use ustat */
158865749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
158965749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
159065749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
159165749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
159266752Seric #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
159365749Seric 
159465749Seric #ifndef SFS_TYPE
159565749Seric # define SFS_TYPE	SFS_NONE
159658157Seric #endif
159758157Seric 
159865749Seric #if SFS_TYPE == SFS_USTAT
159958157Seric # include <ustat.h>
160058157Seric #endif
160165749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
160265749Seric # include <sys/statfs.h>
160358082Seric #endif
160465749Seric #if SFS_TYPE == SFS_VFS
160565749Seric # include <sys/vfs.h>
160665749Seric #endif
160765749Seric #if SFS_TYPE == SFS_MOUNT
160865749Seric # include <sys/mount.h>
160965749Seric #endif
161066752Seric #if SFS_TYPE == SFS_STATVFS
161166752Seric # include <sys/statvfs.h>
161266752Seric #endif
161358082Seric 
161464311Seric long
161564311Seric freespace(dir, bsize)
161664311Seric 	char *dir;
161764311Seric 	long *bsize;
161858082Seric {
161965749Seric #if SFS_TYPE != SFS_NONE
162065749Seric # if SFS_TYPE == SFS_USTAT
162158153Seric 	struct ustat fs;
162258153Seric 	struct stat statbuf;
162358366Seric #  define FSBLOCKSIZE	DEV_BSIZE
162458157Seric #  define f_bavail	f_tfree
162558157Seric # else
162658157Seric #  if defined(ultrix)
162758157Seric 	struct fs_data fs;
162858157Seric #   define f_bavail	fd_bfreen
162958366Seric #   define FSBLOCKSIZE	fs.fd_bsize
163058153Seric #  else
163166752Seric #   if SFS_TYPE == SFS_STATVFS
163266752Seric 	struct statvfs fs;
1633*66893Seric #    define FSBLOCKSIZE	fs.f_bsize
163466752Seric #   else
163558082Seric 	struct statfs fs;
163666752Seric #    define FSBLOCKSIZE	fs.f_bsize
163766752Seric #    if defined(_SCO_unix_) || defined(IRIX) || defined(apollo)
163866752Seric #     define f_bavail f_bfree
163966752Seric #    endif
164063987Seric #   endif
164158153Seric #  endif
164258133Seric # endif
164358082Seric 	extern int errno;
164458082Seric 
164565749Seric # if SFS_TYPE == SFS_USTAT
164664311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
164758157Seric # else
164865749Seric #  if SFS_TYPE == SFS_4ARGS
164964311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
165058157Seric #  else
165158157Seric #   if defined(ultrix)
165264311Seric 	if (statfs(dir, &fs) > 0)
165358153Seric #   else
165464311Seric 	if (statfs(dir, &fs) == 0)
165558153Seric #   endif
165658133Seric #  endif
165758133Seric # endif
165858133Seric 	{
165964311Seric 		if (bsize != NULL)
166064311Seric 			*bsize = FSBLOCKSIZE;
166164311Seric 		return (fs.f_bavail);
166264311Seric 	}
166364337Seric #endif
166464311Seric 	return (-1);
166564311Seric }
166664311Seric /*
166764311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
166864311Seric **
166964311Seric **	Only implemented if you have statfs.
167064311Seric **
167164311Seric **	Parameters:
167264311Seric **		msize -- the size to check against.  If zero, we don't yet
167364311Seric **		know how big the message will be, so just check for
167464311Seric **		a "reasonable" amount.
167564311Seric **
167664311Seric **	Returns:
167764311Seric **		TRUE if there is enough space.
167864311Seric **		FALSE otherwise.
167964311Seric */
168064311Seric 
168164311Seric bool
168264311Seric enoughspace(msize)
168364311Seric 	long msize;
168464311Seric {
168564311Seric 	long bfree, bsize;
168664311Seric 
168764311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
168864311Seric 	{
168958133Seric 		if (tTd(4, 80))
169064311Seric 			printf("enoughspace: no threshold\n");
169164311Seric 		return TRUE;
169264311Seric 	}
169364311Seric 
169464311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
169564311Seric 	{
169664311Seric 		if (tTd(4, 80))
169758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
169864311Seric 				bfree, msize);
169958333Seric 
170058333Seric 		/* convert msize to block count */
170164311Seric 		msize = msize / bsize + 1;
170258333Seric 		if (MinBlocksFree >= 0)
170358333Seric 			msize += MinBlocksFree;
170458333Seric 
170564311Seric 		if (bfree < msize)
170658090Seric 		{
170758090Seric #ifdef LOG
170858090Seric 			if (LogLevel > 0)
170964254Seric 				syslog(LOG_ALERT,
171064254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
171164311Seric 					CurEnv->e_id, bfree,
171264254Seric 					CurHostName, msize, QueueDir);
171358090Seric #endif
171458082Seric 			return FALSE;
171558090Seric 		}
171658082Seric 	}
171758082Seric 	else if (tTd(4, 80))
171858333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
171958333Seric 			MinBlocksFree, msize, errstring(errno));
172058082Seric 	return TRUE;
172158082Seric }
172258542Seric /*
172358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
172458542Seric **
172558542Seric **	This looks at an errno value and tells if this is likely to
172658542Seric **	go away if retried later.
172758542Seric **
172858542Seric **	Parameters:
172958542Seric **		err -- the errno code to classify.
173058542Seric **
173158542Seric **	Returns:
173258542Seric **		TRUE if this is probably transient.
173358542Seric **		FALSE otherwise.
173458542Seric */
173558542Seric 
173658542Seric bool
173758542Seric transienterror(err)
173858542Seric 	int err;
173958542Seric {
174058542Seric 	switch (err)
174158542Seric 	{
174258542Seric 	  case EIO:			/* I/O error */
174358542Seric 	  case ENXIO:			/* Device not configured */
174458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
174558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
174658542Seric 	  case ENODEV:			/* Operation not supported by device */
174758542Seric 	  case ENFILE:			/* Too many open files in system */
174858542Seric 	  case EMFILE:			/* Too many open files */
174958542Seric 	  case ENOSPC:			/* No space left on device */
175058542Seric #ifdef ETIMEDOUT
175158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
175258542Seric #endif
175358542Seric #ifdef ESTALE
175458542Seric 	  case ESTALE:			/* Stale NFS file handle */
175558542Seric #endif
175658542Seric #ifdef ENETDOWN
175758542Seric 	  case ENETDOWN:		/* Network is down */
175858542Seric #endif
175958542Seric #ifdef ENETUNREACH
176058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
176158542Seric #endif
176258542Seric #ifdef ENETRESET
176358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
176458542Seric #endif
176558542Seric #ifdef ECONNABORTED
176658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
176758542Seric #endif
176858542Seric #ifdef ECONNRESET
176958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
177058542Seric #endif
177158542Seric #ifdef ENOBUFS
177258542Seric 	  case ENOBUFS:			/* No buffer space available */
177358542Seric #endif
177458542Seric #ifdef ESHUTDOWN
177558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
177658542Seric #endif
177758542Seric #ifdef ECONNREFUSED
177858542Seric 	  case ECONNREFUSED:		/* Connection refused */
177958542Seric #endif
178058542Seric #ifdef EHOSTDOWN
178158542Seric 	  case EHOSTDOWN:		/* Host is down */
178258542Seric #endif
178358542Seric #ifdef EHOSTUNREACH
178458542Seric 	  case EHOSTUNREACH:		/* No route to host */
178558542Seric #endif
178658542Seric #ifdef EDQUOT
178758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
178858542Seric #endif
178958542Seric #ifdef EPROCLIM
179058542Seric 	  case EPROCLIM:		/* Too many processes */
179158542Seric #endif
179258542Seric #ifdef EUSERS
179358542Seric 	  case EUSERS:			/* Too many users */
179458542Seric #endif
179558542Seric #ifdef EDEADLK
179658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
179758542Seric #endif
179858542Seric #ifdef EISCONN
179958542Seric 	  case EISCONN:			/* Socket already connected */
180058542Seric #endif
180158542Seric #ifdef EINPROGRESS
180258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
180358542Seric #endif
180458542Seric #ifdef EALREADY
180558542Seric 	  case EALREADY:		/* Operation already in progress */
180658542Seric #endif
180758542Seric #ifdef EADDRINUSE
180858542Seric 	  case EADDRINUSE:		/* Address already in use */
180958542Seric #endif
181058542Seric #ifdef EADDRNOTAVAIL
181158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
181258542Seric #endif
181366018Seric #ifdef ETXTBSY
181466017Seric 	  case ETXTBSY:			/* (Apollo) file locked */
181566017Seric #endif
181663834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
181758542Seric 	  case ENOSR:			/* Out of streams resources */
181858542Seric #endif
181958542Seric 		return TRUE;
182058542Seric 	}
182158542Seric 
182258542Seric 	/* nope, must be permanent */
182358542Seric 	return FALSE;
182458542Seric }
182558689Seric /*
182664035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
182758689Seric **
182858689Seric **	Parameters:
182958689Seric **		fd -- the file descriptor of the file.
183058689Seric **		filename -- the file name (for error messages).
183164335Seric **		ext -- the filename extension.
183258689Seric **		type -- type of the lock.  Bits can be:
183358689Seric **			LOCK_EX -- exclusive lock.
183458689Seric **			LOCK_NB -- non-blocking.
183558689Seric **
183658689Seric **	Returns:
183758689Seric **		TRUE if the lock was acquired.
183858689Seric **		FALSE otherwise.
183958689Seric */
184058689Seric 
184158689Seric bool
184264335Seric lockfile(fd, filename, ext, type)
184358689Seric 	int fd;
184458689Seric 	char *filename;
184564335Seric 	char *ext;
184658689Seric 	int type;
184758689Seric {
184865830Seric # if !HASFLOCK
184958689Seric 	int action;
185058689Seric 	struct flock lfd;
185164335Seric 
185264335Seric 	if (ext == NULL)
185364335Seric 		ext = "";
185464035Seric 
185563983Seric 	bzero(&lfd, sizeof lfd);
185659447Seric 	if (bitset(LOCK_UN, type))
185759447Seric 		lfd.l_type = F_UNLCK;
185859447Seric 	else if (bitset(LOCK_EX, type))
185958689Seric 		lfd.l_type = F_WRLCK;
186058689Seric 	else
186158689Seric 		lfd.l_type = F_RDLCK;
186258689Seric 
186358689Seric 	if (bitset(LOCK_NB, type))
186458689Seric 		action = F_SETLK;
186558689Seric 	else
186658689Seric 		action = F_SETLKW;
186758689Seric 
186864264Seric 	if (tTd(55, 60))
186964335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
187064335Seric 			filename, ext, action, lfd.l_type);
187164264Seric 
187258689Seric 	if (fcntl(fd, action, &lfd) >= 0)
187364264Seric 	{
187464264Seric 		if (tTd(55, 60))
187564264Seric 			printf("SUCCESS\n");
187658689Seric 		return TRUE;
187764264Seric 	}
187858689Seric 
187964264Seric 	if (tTd(55, 60))
188064264Seric 		printf("(%s) ", errstring(errno));
188164264Seric 
188264136Seric 	/*
188364136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
188464136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
188564136Seric 	**  as type "tmp" (that is, served from swap space), the
188664136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
188764136Seric 	**  Since this is fairly common during testing, we will assume
188864136Seric 	**  that this indicates that the lock is successfully grabbed.
188964136Seric 	*/
189064136Seric 
189164136Seric 	if (errno == EINVAL)
189264264Seric 	{
189364264Seric 		if (tTd(55, 60))
189464264Seric 			printf("SUCCESS\n");
189564136Seric 		return TRUE;
189664264Seric 	}
189764136Seric 
189858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
189964378Seric 	{
190064378Seric 		int omode = -1;
190164378Seric #  ifdef F_GETFL
190264378Seric 		int oerrno = errno;
190364378Seric 
190464378Seric 		(void) fcntl(fd, F_GETFL, &omode);
190564378Seric 		errno = oerrno;
190664378Seric #  endif
190764378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
190864378Seric 			filename, ext, fd, type, omode, geteuid());
190964378Seric 	}
191058689Seric # else
191164335Seric 	if (ext == NULL)
191264335Seric 		ext = "";
191364335Seric 
191464264Seric 	if (tTd(55, 60))
191564335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
191664264Seric 
191758689Seric 	if (flock(fd, type) >= 0)
191864264Seric 	{
191964264Seric 		if (tTd(55, 60))
192064264Seric 			printf("SUCCESS\n");
192158689Seric 		return TRUE;
192264264Seric 	}
192358689Seric 
192464264Seric 	if (tTd(55, 60))
192564264Seric 		printf("(%s) ", errstring(errno));
192664264Seric 
192758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
192864378Seric 	{
192964378Seric 		int omode = -1;
193064378Seric #  ifdef F_GETFL
193164378Seric 		int oerrno = errno;
193264378Seric 
193364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
193464378Seric 		errno = oerrno;
193564378Seric #  endif
193664378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
193764378Seric 			filename, ext, fd, type, omode, geteuid());
193864386Seric 	}
193958689Seric # endif
194064264Seric 	if (tTd(55, 60))
194164264Seric 		printf("FAIL\n");
194258689Seric 	return FALSE;
194358689Seric }
194464556Seric /*
194565948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
194665948Seric **
194765948Seric **	Parameters:
194865948Seric **		fd -- the file descriptor to check.
194965948Seric **
195065948Seric **	Returns:
195165948Seric **		TRUE -- if only root can chown the file to an arbitrary
195265948Seric **			user.
195365948Seric **		FALSE -- if an arbitrary user can give away a file.
195465948Seric */
195565948Seric 
195665948Seric bool
195765948Seric chownsafe(fd)
195865948Seric 	int fd;
195965948Seric {
196065950Seric #ifdef __hpux
196165948Seric 	char *s;
196265948Seric 	int tfd;
196365948Seric 	uid_t o_uid, o_euid;
196465948Seric 	gid_t o_gid, o_egid;
196565948Seric 	bool rval;
196665948Seric 	struct stat stbuf;
196765948Seric 
196865948Seric 	o_uid = getuid();
196965948Seric 	o_euid = geteuid();
197065948Seric 	o_gid = getgid();
197165948Seric 	o_egid = getegid();
197265959Seric 	fstat(fd, &stbuf);
197365948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
197465948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
197565948Seric 	s = tmpnam(NULL);
197665948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
197765974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
197865948Seric 	close(tfd);
197965948Seric 	unlink(s);
198065948Seric 	setreuid(o_uid, o_euid);
198165948Seric 	setresgid(o_gid, o_egid, -1);
198265948Seric 	return rval;
198365948Seric #else
198466088Seric # ifdef _POSIX_CHOWN_RESTRICTED
198566088Seric #  if _POSIX_CHOWN_RESTRICTED == -1
198666088Seric 	return FALSE;
198766088Seric #  else
198866088Seric 	return TRUE;
198966088Seric #  endif
199066088Seric # else
199166088Seric #  ifdef _PC_CHOWN_RESTRICTED
199265948Seric 	return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0;
199366088Seric #  else
199466088Seric #   ifdef BSD
199565948Seric 	return TRUE;
199666088Seric #   else
199765948Seric 	return FALSE;
199866088Seric #   endif
199965948Seric #  endif
200065948Seric # endif
200165948Seric #endif
200265948Seric }
200365948Seric /*
200464556Seric **  GETCFNAME -- return the name of the .cf file.
200564556Seric **
200664556Seric **	Some systems (e.g., NeXT) determine this dynamically.
200764556Seric */
200864556Seric 
200964556Seric char *
201064556Seric getcfname()
201164556Seric {
201264556Seric 	if (ConfFile != NULL)
201364556Seric 		return ConfFile;
201465214Seric #ifdef NETINFO
201565214Seric 	{
201665214Seric 		extern char *ni_propval();
201765214Seric 		char *cflocation;
201865214Seric 
201965214Seric 		cflocation = ni_propval("/locations/sendmail", "sendmail.cf");
202065214Seric 		if (cflocation != NULL)
202165214Seric 			return cflocation;
202265214Seric 	}
202365214Seric #endif
202464556Seric 	return _PATH_SENDMAILCF;
202564556Seric }
202664718Seric /*
202764718Seric **  SETVENDOR -- process vendor code from V configuration line
202864718Seric **
202964718Seric **	Parameters:
203064718Seric **		vendor -- string representation of vendor.
203164718Seric **
203264718Seric **	Returns:
203364718Seric **		TRUE -- if ok.
203464718Seric **		FALSE -- if vendor code could not be processed.
203564926Seric **
203664926Seric **	Side Effects:
203764926Seric **		It is reasonable to set mode flags here to tweak
203864926Seric **		processing in other parts of the code if necessary.
203964926Seric **		For example, if you are a vendor that uses $%y to
204064926Seric **		indicate YP lookups, you could enable that here.
204164718Seric */
204264718Seric 
204364718Seric bool
204464718Seric setvendor(vendor)
204564718Seric 	char *vendor;
204664718Seric {
204764926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
204864926Seric 		return TRUE;
204964926Seric 
205064926Seric 	/* add vendor extensions here */
205164926Seric 
205264926Seric 	return FALSE;
205364718Seric }
205464816Seric /*
205564816Seric **  STRTOL -- convert string to long integer
205664816Seric **
205764816Seric **	For systems that don't have it in the C library.
205866161Seric **
205966161Seric **	This is taken verbatim from the 4.4-Lite C library.
206064816Seric */
206164816Seric 
206264816Seric #ifdef NEEDSTRTOL
206364816Seric 
206466161Seric #if defined(LIBC_SCCS) && !defined(lint)
206566161Seric static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
206666161Seric #endif /* LIBC_SCCS and not lint */
206766161Seric 
206866161Seric #include <limits.h>
206966161Seric 
207066161Seric /*
207166161Seric  * Convert a string to a long integer.
207266161Seric  *
207366161Seric  * Ignores `locale' stuff.  Assumes that the upper and lower case
207466161Seric  * alphabets and digits are each contiguous.
207566161Seric  */
207666161Seric 
207764816Seric long
207866161Seric strtol(nptr, endptr, base)
207966161Seric 	const char *nptr;
208066161Seric 	char **endptr;
208166161Seric 	register int base;
208264816Seric {
208366161Seric 	register const char *s = nptr;
208466161Seric 	register unsigned long acc;
208566161Seric 	register int c;
208666161Seric 	register unsigned long cutoff;
208766161Seric 	register int neg = 0, any, cutlim;
208864816Seric 
208966161Seric 	/*
209066161Seric 	 * Skip white space and pick up leading +/- sign if any.
209166161Seric 	 * If base is 0, allow 0x for hex and 0 for octal, else
209266161Seric 	 * assume decimal; if base is already 16, allow 0x.
209366161Seric 	 */
209466161Seric 	do {
209566161Seric 		c = *s++;
209666161Seric 	} while (isspace(c));
209766161Seric 	if (c == '-') {
209866161Seric 		neg = 1;
209966161Seric 		c = *s++;
210066161Seric 	} else if (c == '+')
210166161Seric 		c = *s++;
210266161Seric 	if ((base == 0 || base == 16) &&
210366161Seric 	    c == '0' && (*s == 'x' || *s == 'X')) {
210466161Seric 		c = s[1];
210566161Seric 		s += 2;
210666161Seric 		base = 16;
210766161Seric 	}
210866161Seric 	if (base == 0)
210966161Seric 		base = c == '0' ? 8 : 10;
211064816Seric 
211166161Seric 	/*
211266161Seric 	 * Compute the cutoff value between legal numbers and illegal
211366161Seric 	 * numbers.  That is the largest legal value, divided by the
211466161Seric 	 * base.  An input number that is greater than this value, if
211566161Seric 	 * followed by a legal input character, is too big.  One that
211666161Seric 	 * is equal to this value may be valid or not; the limit
211766161Seric 	 * between valid and invalid numbers is then based on the last
211866161Seric 	 * digit.  For instance, if the range for longs is
211966161Seric 	 * [-2147483648..2147483647] and the input base is 10,
212066161Seric 	 * cutoff will be set to 214748364 and cutlim to either
212166161Seric 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
212266161Seric 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
212366161Seric 	 * the number is too big, and we will return a range error.
212466161Seric 	 *
212566161Seric 	 * Set any if any `digits' consumed; make it negative to indicate
212666161Seric 	 * overflow.
212766161Seric 	 */
212866161Seric 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
212966161Seric 	cutlim = cutoff % (unsigned long)base;
213066161Seric 	cutoff /= (unsigned long)base;
213166161Seric 	for (acc = 0, any = 0;; c = *s++) {
213266161Seric 		if (isdigit(c))
213366161Seric 			c -= '0';
213466161Seric 		else if (isalpha(c))
213566161Seric 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
213666161Seric 		else
213766161Seric 			break;
213866161Seric 		if (c >= base)
213966161Seric 			break;
214066161Seric 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
214166161Seric 			any = -1;
214266161Seric 		else {
214366161Seric 			any = 1;
214466161Seric 			acc *= base;
214566161Seric 			acc += c;
214664816Seric 		}
214764816Seric 	}
214866161Seric 	if (any < 0) {
214966161Seric 		acc = neg ? LONG_MIN : LONG_MAX;
215066161Seric 		errno = ERANGE;
215166161Seric 	} else if (neg)
215266161Seric 		acc = -acc;
215366161Seric 	if (endptr != 0)
215466161Seric 		*endptr = (char *)(any ? s - 1 : nptr);
215566161Seric 	return (acc);
215664816Seric }
215764816Seric 
215864816Seric #endif
215964841Seric /*
216064841Seric **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
216164841Seric **
216264841Seric **	Solaris versions prior through 2.3 don't properly deliver a
216364841Seric **	canonical h_name field.  This tries to work around it.
216464841Seric */
216564841Seric 
216664841Seric #ifdef SOLARIS
216764841Seric 
216864841Seric struct hostent *
216964841Seric solaris_gethostbyname(name)
217065094Seric 	const char *name;
217164841Seric {
217264841Seric # ifdef SOLARIS_2_3
217364841Seric 	static struct hostent hp;
217464841Seric 	static char buf[1000];
217564841Seric 	extern struct hostent *_switch_gethostbyname_r();
217664841Seric 
217764841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
217864841Seric # else
217965009Seric 	extern struct hostent *__switch_gethostbyname();
218065009Seric 
218164841Seric 	return __switch_gethostbyname(name);
218264841Seric # endif
218364841Seric }
218464841Seric 
218564841Seric struct hostent *
218664841Seric solaris_gethostbyaddr(addr, len, type)
218765094Seric 	const char *addr;
218864841Seric 	int len;
218964841Seric 	int type;
219064841Seric {
219164841Seric # ifdef SOLARIS_2_3
219264841Seric 	static struct hostent hp;
219364841Seric 	static char buf[1000];
219464841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
219564841Seric 
219664841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
219764841Seric # else
219865009Seric 	extern struct hostent *__switch_gethostbyaddr();
219965009Seric 
220064841Seric 	return __switch_gethostbyaddr(addr, len, type);
220164841Seric # endif
220264841Seric }
220364841Seric 
220464841Seric #endif
220565214Seric /*
220665214Seric **  NI_PROPVAL -- netinfo property value lookup routine
220765214Seric **
220865214Seric **	Parameters:
220965214Seric **		directory -- the Netinfo directory name.
221065214Seric **		propname -- the Netinfo property name.
221165214Seric **
221265214Seric **	Returns:
221365214Seric **		NULL -- if:
221465214Seric **			1. the directory is not found
221565214Seric **			2. the property name is not found
221665214Seric **			3. the property contains multiple values
221765214Seric **			4. some error occured
221865214Seric **		else -- the location of the config file.
221965214Seric **
222065214Seric **	Notes:
222165214Seric **      	Caller should free the return value of ni_proval
222265214Seric */
222365214Seric 
222465214Seric #ifdef NETINFO
222565214Seric 
222665214Seric # include <netinfo/ni.h>
222765214Seric 
222865214Seric # define LOCAL_NETINFO_DOMAIN    "."
222965214Seric # define PARENT_NETINFO_DOMAIN   ".."
223065214Seric # define MAX_NI_LEVELS           256
223165214Seric 
223265214Seric char *
223365214Seric ni_propval(directory, propname)
223465214Seric 	char *directory;
223565214Seric 	char *propname;
223665214Seric {
223765820Seric 	char *propval = NULL;
223865214Seric 	int i;
223965214Seric 	void *ni = NULL;
224065214Seric 	void *lastni = NULL;
224165214Seric 	ni_status nis;
224265214Seric 	ni_id nid;
224365214Seric 	ni_namelist ninl;
224465214Seric 
224565214Seric 	/*
224665214Seric 	**  If the passed directory and property name are found
224765214Seric 	**  in one of netinfo domains we need to search (starting
224865214Seric 	**  from the local domain moving all the way back to the
224965214Seric 	**  root domain) set propval to the property's value
225065214Seric 	**  and return it.
225165214Seric 	*/
225265214Seric 
225365214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
225465214Seric 	{
225565214Seric 		if (i == 0)
225665214Seric 		{
225765214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
225865214Seric 		}
225965214Seric 		else
226065214Seric 		{
226165214Seric 			if (lastni != NULL)
226265214Seric 				ni_free(lastni);
226365214Seric 			lastni = ni;
226465214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
226565214Seric 		}
226665214Seric 
226765214Seric 		/*
226865214Seric 		**  Don't bother if we didn't get a handle on a
226965214Seric 		**  proper domain.  This is not necessarily an error.
227065214Seric 		**  We would get a positive ni_status if, for instance
227165214Seric 		**  we never found the directory or property and tried
227265214Seric 		**  to open the parent of the root domain!
227365214Seric 		*/
227465214Seric 
227565214Seric 		if (nis != 0)
227665214Seric 			break;
227765214Seric 
227865214Seric 		/*
227965214Seric 		**  Find the path to the server information.
228065214Seric 		*/
228165214Seric 
228265214Seric 		if (ni_pathsearch(ni, &nid, directory) != 0)
228365214Seric 			continue;
228465214Seric 
228565214Seric 		/*
228665214Seric 		**  Find "host" information.
228765214Seric 		*/
228865214Seric 
228965214Seric 		if (ni_lookupprop(ni, &nid, propname, &ninl) != 0)
229065214Seric 			continue;
229165214Seric 
229265214Seric 		/*
229365214Seric 		**  If there's only one name in
229465214Seric 		**  the list, assume we've got
229565214Seric 		**  what we want.
229665214Seric 		*/
229765214Seric 
229865214Seric 		if (ninl.ni_namelist_len == 1)
229965214Seric 		{
230065214Seric 			propval = ni_name_dup(ninl.ni_namelist_val[0]);
230165214Seric 			break;
230265214Seric 		}
230365214Seric 	}
230465214Seric 
230565214Seric 	/*
230665214Seric 	**  Clean up.
230765214Seric 	*/
230865214Seric 
230965214Seric 	if (ni != NULL)
231065214Seric 		ni_free(ni);
231165214Seric 	if (lastni != NULL && ni != lastni)
231265214Seric 		ni_free(lastni);
231365214Seric 
231465214Seric 	return propval;
231565214Seric }
231665214Seric 
231765214Seric #endif /* NETINFO */
231865504Seric /*
231965504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
232065504Seric **
232165504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
232265504Seric **	syslog succeeds during interrupt handlers.
232365504Seric */
232465504Seric 
232565504Seric #ifdef __hpux
232665504Seric 
232765504Seric # define MAXSYSLOGTRIES	100
232865504Seric # undef syslog
232965504Seric 
233065504Seric # ifdef __STDC__
233165504Seric hard_syslog(int pri, char *msg, ...)
233265504Seric # else
233365504Seric hard_syslog(pri, msg, va_alist)
233465504Seric 	int pri;
233565504Seric 	char *msg;
233665504Seric 	va_dcl
233765504Seric # endif
233865504Seric {
233965504Seric 	int i;
234065504Seric 	char buf[SYSLOG_BUFSIZE * 2];
234165504Seric 	VA_LOCAL_DECL;
234265504Seric 
234365504Seric 	VA_START(msg);
234465504Seric 	vsprintf(buf, msg, ap);
234565504Seric 	VA_END;
234665504Seric 
234765504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
234865504Seric 		continue;
234965504Seric }
235065504Seric 
235165504Seric #endif
2352