xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 60583)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*60583Seric static char sccsid[] = "@(#)conf.c	6.62 (Berkeley) 05/29/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1314881Seric # include <sys/ioctl.h>
1458082Seric # include <sys/param.h>
1558153Seric # include <signal.h>
1636928Sbostic # include <pwd.h>
173309Seric # include "sendmail.h"
1840980Sbostic # include "pathnames.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,
6457359Seric 	"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,
9458796Seric 	"return-path",		H_ACHECK,
9558796Seric 
969055Seric 	NULL,			0,
972897Seric };
984166Seric 
994166Seric 
1004166Seric 
1014282Seric /*
1024282Seric **  Location of system files/databases/etc.
1034282Seric */
1044282Seric 
10540980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
10640980Sbostic char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
10758082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1089039Seric 
1099064Seric 
1109064Seric 
1119039Seric /*
11258082Seric **  Privacy values
11358082Seric */
11458082Seric 
11558082Seric struct prival PrivacyValues[] =
11658082Seric {
11758082Seric 	"public",		PRIV_PUBLIC,
11858082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11958114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
12058082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12158082Seric 	"noexpn",		PRIV_NOEXPN,
12258082Seric 	"novrfy",		PRIV_NOVRFY,
12358249Seric 	"restrictmailq",	PRIV_RESTRMAILQ,
12458789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
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 */
13624943Seric /*
13724943Seric **  SETDEFAULTS -- set default values
13824943Seric **
13924943Seric **	Because of the way freezing is done, these must be initialized
14024943Seric **	using direct code.
14124943Seric **
14224943Seric **	Parameters:
14358734Seric **		e -- the default envelope.
14424943Seric **
14524943Seric **	Returns:
14624943Seric **		none.
14724943Seric **
14824943Seric **	Side Effects:
14924943Seric **		Initializes a bunch of global variables to their
15024943Seric **		default values.
15124943Seric */
15224943Seric 
15358737Seric #define DAYS		* 24 * 60 * 60
15458737Seric 
15558734Seric setdefaults(e)
15658734Seric 	register ENVELOPE *e;
15724943Seric {
15857438Seric 	SpaceSub = ' ';				/* option B */
15957438Seric 	QueueLA = 8;				/* option x */
16057438Seric 	RefuseLA = 12;				/* option X */
16157438Seric 	WkRecipFact = 30000L;			/* option y */
16257438Seric 	WkClassFact = 1800L;			/* option z */
16357438Seric 	WkTimeFact = 90000L;			/* option Z */
16457438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
16557142Seric 	FileMode = (getuid() != geteuid()) ? 0644 : 0600;
16657438Seric 						/* option F */
16757438Seric 	DefUid = 1;				/* option u */
16857438Seric 	DefGid = 1;				/* option g */
16957438Seric 	CheckpointInterval = 10;		/* option C */
17057438Seric 	MaxHopCount = 25;			/* option h */
17158734Seric 	e->e_sendmode = SM_FORK;		/* option d */
17258734Seric 	e->e_errormode = EM_PRINT;		/* option e */
17359709Seric 	SevenBit = FALSE;			/* option 7 */
17457438Seric 	MaxMciCache = 1;			/* option k */
17557438Seric 	MciCacheTimeout = 300;			/* option K */
17657438Seric 	LogLevel = 9;				/* option L */
17758112Seric 	settimeouts(NULL);			/* option r */
17858737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
17958737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
18058853Seric 	PrivacyFlags = 0;			/* option p */
18140973Sbostic 	setdefuser();
18253654Seric 	setupmaps();
18357402Seric 	setupmailers();
18424943Seric }
185294Seric 
18640973Sbostic 
1874326Seric /*
18840973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
18940973Sbostic */
19040973Sbostic 
19140973Sbostic setdefuser()
19240973Sbostic {
19340973Sbostic 	struct passwd *defpwent;
19457386Seric 	static char defuserbuf[40];
19540973Sbostic 
19657386Seric 	DefUser = defuserbuf;
19740973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
19857386Seric 		strcpy(defuserbuf, defpwent->pw_name);
19940973Sbostic 	else
20057386Seric 		strcpy(defuserbuf, "nobody");
20140973Sbostic }
20253654Seric /*
20356836Seric **  HOST_MAP_INIT -- initialize host class structures
20456836Seric */
20556836Seric 
20656836Seric bool
20760219Seric host_map_init(map, args)
20856836Seric 	MAP *map;
20956836Seric 	char *args;
21056836Seric {
21156836Seric 	register char *p = args;
21256836Seric 
21356836Seric 	for (;;)
21456836Seric 	{
21558050Seric 		while (isascii(*p) && isspace(*p))
21656836Seric 			p++;
21756836Seric 		if (*p != '-')
21856836Seric 			break;
21956836Seric 		switch (*++p)
22056836Seric 		{
22156836Seric 		  case 'a':
22256836Seric 			map->map_app = ++p;
22356836Seric 			break;
22456836Seric 		}
22558050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
22656836Seric 			p++;
22756836Seric 		if (*p != '\0')
22856836Seric 			*p++ = '\0';
22956836Seric 	}
23056836Seric 	if (map->map_app != NULL)
23156836Seric 		map->map_app = newstr(map->map_app);
23256836Seric 	return TRUE;
23356836Seric }
23457402Seric /*
23557402Seric **  SETUPMAILERS -- initialize default mailers
23657402Seric */
23756836Seric 
23857402Seric setupmailers()
23957402Seric {
24057402Seric 	char buf[100];
24157402Seric 
24257403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
24357403Seric 	makemailer(buf);
24457403Seric 
24559883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
24657402Seric 	makemailer(buf);
24757402Seric 
24857402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
24957402Seric 	makemailer(buf);
25057402Seric }
25156836Seric /*
25260207Seric **  SETUPMAPS -- set up map classes
25360207Seric */
25460207Seric 
25560207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
25660207Seric 	{ \
25760207Seric 		extern bool parse __P((MAP *, char *)); \
25860207Seric 		extern bool open __P((MAP *, int)); \
25960207Seric 		extern void close __P((MAP *)); \
26060207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
26160207Seric 		extern void store __P((MAP *, char *, char *)); \
26260207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
26360207Seric 		s->s_mapclass.map_cname = name; \
26460207Seric 		s->s_mapclass.map_ext = ext; \
26560207Seric 		s->s_mapclass.map_cflags = flags; \
26660207Seric 		s->s_mapclass.map_parse = parse; \
26760207Seric 		s->s_mapclass.map_open = open; \
26860207Seric 		s->s_mapclass.map_close = close; \
26960207Seric 		s->s_mapclass.map_lookup = lookup; \
27060207Seric 		s->s_mapclass.map_store = store; \
27160207Seric 	}
27260207Seric 
27360207Seric setupmaps()
27460207Seric {
27560207Seric 	register STAB *s;
27660207Seric 
27760207Seric #ifdef NEWDB
27860207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
27960207Seric 		map_parseargs, hash_map_open, db_map_close,
28060207Seric 		db_map_lookup, db_map_store);
28160207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
28260207Seric 		map_parseargs, bt_map_open, db_map_close,
28360207Seric 		db_map_lookup, db_map_store);
28460207Seric #endif
28560207Seric 
28660207Seric #ifdef NDBM
28760207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
28860207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
28960207Seric 		ndbm_map_lookup, ndbm_map_store);
29060207Seric #endif
29160207Seric 
29260207Seric #ifdef NIS
29360207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
29460207Seric 		map_parseargs, nis_map_open, nis_map_close,
29560207Seric 		nis_map_lookup, nis_map_store);
29660207Seric #endif
29760207Seric 
29860207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
29960207Seric 		map_parseargs, stab_map_open, stab_map_close,
30060207Seric 		stab_map_lookup, stab_map_store);
30160207Seric 
30260207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
30360207Seric 		map_parseargs, impl_map_open, impl_map_close,
30460207Seric 		impl_map_lookup, impl_map_store);
30560207Seric 
30660207Seric 	/* host DNS lookup */
30760207Seric 	MAPDEF("host", NULL, 0,
30860207Seric 		host_map_init, null_map_open, null_map_close,
30960207Seric 		host_map_lookup, null_map_store);
31060207Seric 
31160207Seric 	/* dequote map */
31260207Seric 	MAPDEF("dequote", NULL, 0,
31360207Seric 		dequote_init, null_map_open, null_map_close,
31460207Seric 		dequote_map, null_map_store);
31560207Seric 
31660207Seric #if 0
31760207Seric # ifdef USERDB
31860207Seric 	/* user database */
31960207Seric 	MAPDEF("udb", ".db", 0,
32060207Seric 		udb_map_parse, null_map_open, null_map_close,
32160207Seric 		udb_map_lookup, null_map_store);
32260207Seric # endif
32360207Seric #endif
32460207Seric }
32560207Seric 
32660207Seric #undef MAPDEF
32760207Seric /*
3284326Seric **  GETRUID -- get real user id (V7)
3294326Seric */
3304326Seric 
3314326Seric getruid()
3324326Seric {
3339274Seric 	if (OpMode == MD_DAEMON)
3344536Seric 		return (RealUid);
3354536Seric 	else
3364536Seric 		return (getuid());
3374326Seric }
3384326Seric 
3394326Seric 
3404326Seric /*
3414326Seric **  GETRGID -- get real group id (V7).
3424326Seric */
3434326Seric 
3444326Seric getrgid()
3454326Seric {
3469274Seric 	if (OpMode == MD_DAEMON)
3474536Seric 		return (RealGid);
3484536Seric 	else
3494536Seric 		return (getgid());
3504326Seric }
35153654Seric /*
3529369Seric **  USERNAME -- return the user id of the logged in user.
3539369Seric **
3549369Seric **	Parameters:
3559369Seric **		none.
3569369Seric **
3579369Seric **	Returns:
3589369Seric **		The login name of the logged in user.
3599369Seric **
3609369Seric **	Side Effects:
3619369Seric **		none.
3629369Seric **
3639369Seric **	Notes:
3649369Seric **		The return value is statically allocated.
3659369Seric */
3669369Seric 
3679369Seric char *
3689369Seric username()
3699369Seric {
37017469Seric 	static char *myname = NULL;
3719369Seric 	extern char *getlogin();
37219904Smiriam 	register struct passwd *pw;
3739369Seric 
37417469Seric 	/* cache the result */
37517469Seric 	if (myname == NULL)
37617469Seric 	{
37717469Seric 		myname = getlogin();
37817469Seric 		if (myname == NULL || myname[0] == '\0')
37917469Seric 		{
38017469Seric 			pw = getpwuid(getruid());
38117469Seric 			if (pw != NULL)
38240993Sbostic 				myname = newstr(pw->pw_name);
38317469Seric 		}
38419904Smiriam 		else
38519904Smiriam 		{
38658736Seric 			uid_t uid = getuid();
38719873Smiriam 
38840993Sbostic 			myname = newstr(myname);
38940993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
39058736Seric 			      (uid != 0 && uid != pw->pw_uid))
39119904Smiriam 			{
39258736Seric 				pw = getpwuid(uid);
39324945Seric 				if (pw != NULL)
39440993Sbostic 					myname = newstr(pw->pw_name);
39519873Smiriam 			}
39619873Smiriam 		}
39717469Seric 		if (myname == NULL || myname[0] == '\0')
39817469Seric 		{
39958151Seric 			syserr("554 Who are you?");
40017469Seric 			myname = "postmaster";
40117469Seric 		}
40217469Seric 	}
40317469Seric 
40417469Seric 	return (myname);
4059369Seric }
4069369Seric /*
4074190Seric **  TTYPATH -- Get the path of the user's tty
408294Seric **
409294Seric **	Returns the pathname of the user's tty.  Returns NULL if
410294Seric **	the user is not logged in or if s/he has write permission
411294Seric **	denied.
412294Seric **
413294Seric **	Parameters:
414294Seric **		none
415294Seric **
416294Seric **	Returns:
417294Seric **		pathname of the user's tty.
418294Seric **		NULL if not logged in or write permission denied.
419294Seric **
420294Seric **	Side Effects:
421294Seric **		none.
422294Seric **
423294Seric **	WARNING:
424294Seric **		Return value is in a local buffer.
425294Seric **
426294Seric **	Called By:
427294Seric **		savemail
428294Seric */
429294Seric 
430294Seric char *
431294Seric ttypath()
432294Seric {
433294Seric 	struct stat stbuf;
434294Seric 	register char *pathn;
435294Seric 	extern char *ttyname();
4364081Seric 	extern char *getlogin();
437294Seric 
438294Seric 	/* compute the pathname of the controlling tty */
4399369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4409369Seric 	    (pathn = ttyname(0)) == NULL)
441294Seric 	{
442294Seric 		errno = 0;
443294Seric 		return (NULL);
444294Seric 	}
445294Seric 
446294Seric 	/* see if we have write permission */
4472967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
448294Seric 	{
449294Seric 		errno = 0;
450294Seric 		return (NULL);
451294Seric 	}
452294Seric 
453294Seric 	/* see if the user is logged in */
454294Seric 	if (getlogin() == NULL)
455294Seric 		return (NULL);
456294Seric 
457294Seric 	/* looks good */
458294Seric 	return (pathn);
459294Seric }
4602967Seric /*
4612967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4622967Seric **
4632967Seric **	This routine can be supplied on a per-installation basis
4642967Seric **	to determine whether a person is allowed to send a message.
4652967Seric **	This allows restriction of certain types of internet
4662967Seric **	forwarding or registration of users.
4672967Seric **
4682967Seric **	If the hosts are found to be incompatible, an error
46957454Seric **	message should be given using "usrerr" and 0 should
4702967Seric **	be returned.
4712967Seric **
4724288Seric **	'NoReturn' can be set to suppress the return-to-sender
4734288Seric **	function; this should be done on huge messages.
4744288Seric **
4752967Seric **	Parameters:
4762967Seric **		to -- the person being sent to.
4772967Seric **
4782967Seric **	Returns:
47957459Seric **		an exit status
4802967Seric **
4812967Seric **	Side Effects:
4822967Seric **		none (unless you include the usrerr stuff)
4832967Seric */
4842967Seric 
48555012Seric checkcompat(to, e)
4862967Seric 	register ADDRESS *to;
48755012Seric 	register ENVELOPE *e;
4882967Seric {
48912133Seric # ifdef lint
49012133Seric 	if (to == NULL)
49112133Seric 		to++;
49212133Seric # endif lint
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 /*
5099369Seric **  HOLDSIGS -- arrange to hold all signals
5109369Seric **
5119369Seric **	Parameters:
5129369Seric **		none.
5139369Seric **
5149369Seric **	Returns:
5159369Seric **		none.
5169369Seric **
5179369Seric **	Side Effects:
5189369Seric **		Arranges that signals are held.
5199369Seric */
5209369Seric 
5219369Seric holdsigs()
5229369Seric {
5239369Seric }
5249369Seric /*
5259369Seric **  RLSESIGS -- arrange to release all signals
5269369Seric **
5279369Seric **	This undoes the effect of holdsigs.
5289369Seric **
5299369Seric **	Parameters:
5309369Seric **		none.
5319369Seric **
5329369Seric **	Returns:
5339369Seric **		none.
5349369Seric **
5359369Seric **	Side Effects:
5369369Seric **		Arranges that signals are released.
5379369Seric */
5389369Seric 
5399369Seric rlsesigs()
5409369Seric {
5419369Seric }
54214872Seric /*
54314872Seric **  GETLA -- get the current load average
54414872Seric **
54514881Seric **	This code stolen from la.c.
54614881Seric **
54714872Seric **	Parameters:
54814872Seric **		none.
54914872Seric **
55014872Seric **	Returns:
55114872Seric **		The current load average as an integer.
55214872Seric **
55314872Seric **	Side Effects:
55414872Seric **		none.
55514872Seric */
55614872Seric 
55751920Seric /* try to guess what style of load average we have */
55851920Seric #define LA_ZERO		1	/* always return load average as zero */
55951920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
56051920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
56151920Seric #define LA_SUBR		4	/* call getloadavg */
56214872Seric 
56351920Seric #ifndef LA_TYPE
56460160Seric #  if defined(sun) && !defined(BSD)
56551920Seric #    define LA_TYPE		LA_INT
56651920Seric #  endif
56757977Seric #  if defined(mips) || defined(__alpha)
56857977Seric      /* Ultrix or OSF/1 or RISC/os */
56951920Seric #    define LA_TYPE		LA_INT
57051920Seric #    define LA_AVENRUN		"avenrun"
57151920Seric #  endif
57259430Seric #  if defined(__hpux)
57351920Seric #    define LA_TYPE		LA_FLOAT
57459253Seric #    define LA_AVENRUN		"avenrun"
57551920Seric #  endif
57651920Seric 
57760219Seric /* now do the guesses based on general OS type */
57851920Seric #  ifndef LA_TYPE
57957736Seric #   if defined(SYSTEM5)
58057736Seric #    define LA_TYPE		LA_INT
58157736Seric #    define LA_AVENRUN		"avenrun"
58257736Seric #   else
58357736Seric #    if defined(BSD)
58457736Seric #     define LA_TYPE		LA_SUBR
58557736Seric #    else
58657736Seric #     define LA_TYPE		LA_ZERO
58757736Seric #    endif
58857736Seric #   endif
58951920Seric #  endif
59051920Seric #endif
59151920Seric 
59251920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
59351920Seric 
59414872Seric #include <nlist.h>
59514872Seric 
59651920Seric #ifndef LA_AVENRUN
59751920Seric #define LA_AVENRUN	"_avenrun"
59851920Seric #endif
59951920Seric 
60051920Seric /* _PATH_UNIX should be defined in <paths.h> */
60151920Seric #ifndef _PATH_UNIX
60259430Seric #  if defined(__hpux)
60351920Seric #    define _PATH_UNIX		"/hp-ux"
60451920Seric #  endif
60551920Seric #  if defined(mips) && !defined(ultrix)
60651920Seric      /* powerful RISC/os */
60751920Seric #    define _PATH_UNIX		"/unix"
60851920Seric #  endif
60957736Seric #  if defined(SYSTEM5)
61057977Seric #    ifndef _PATH_UNIX
61157977Seric #      define _PATH_UNIX	"/unix"
61257977Seric #    endif
61357736Seric #  endif
61451920Seric #  ifndef _PATH_UNIX
61551920Seric #    define _PATH_UNIX		"/vmunix"
61651920Seric #  endif
61751920Seric #endif
61851920Seric 
61914872Seric struct	nlist Nl[] =
62014872Seric {
62151920Seric 	{ LA_AVENRUN },
62214872Seric #define	X_AVENRUN	0
62314872Seric 	{ 0 },
62414872Seric };
62514872Seric 
626*60583Seric #ifndef FSHIFT
627*60583Seric # if defined(unixpc)
628*60583Seric #  define FSHIFT	5
629*60583Seric # endif
63057736Seric 
631*60583Seric # if defined(__alpha)
632*60583Seric #  define FSHIFT	10
633*60583Seric # endif
63457977Seric 
635*60583Seric # if (LA_TYPE == LA_INT)
63651920Seric #  define FSHIFT	8
637*60583Seric # endif
63857736Seric #endif
639*60583Seric 
64057736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
64151920Seric #  define FSCALE	(1 << FSHIFT)
64251920Seric #endif
64340930Srick 
64414872Seric getla()
64514872Seric {
64614872Seric 	static int kmem = -1;
64751920Seric #if LA_TYPE == LA_INT
64824943Seric 	long avenrun[3];
64951920Seric #else
65051920Seric 	double avenrun[3];
65151920Seric #endif
65225615Seric 	extern off_t lseek();
65357736Seric 	extern int errno;
65414872Seric 
65514872Seric 	if (kmem < 0)
65614872Seric 	{
65724945Seric 		kmem = open("/dev/kmem", 0, 0);
65814872Seric 		if (kmem < 0)
65957736Seric 		{
66057736Seric 			if (tTd(3, 1))
66157736Seric 				printf("getla: open(/dev/kmem): %s\n",
66257736Seric 					errstring(errno));
66314872Seric 			return (-1);
66457736Seric 		}
66551920Seric 		(void) fcntl(kmem, F_SETFD, 1);
66657736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
66757736Seric 		{
66857736Seric 			if (tTd(3, 1))
66957736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
67057736Seric 					errstring(errno));
67114872Seric 			return (-1);
67257736Seric 		}
67359253Seric 		if (Nl[X_AVENRUN].n_value == 0)
67459253Seric 		{
67559253Seric 			if (tTd(3, 1))
67659253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
67759253Seric 					_PATH_UNIX, LA_AVENRUN);
67859253Seric 			return (-1);
67959253Seric 		}
68014872Seric 	}
68157736Seric 	if (tTd(3, 20))
68257736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
68324945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
68423118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
68519967Seric 	{
68619967Seric 		/* thank you Ian */
68757736Seric 		if (tTd(3, 1))
68857736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
68919967Seric 		return (-1);
69019967Seric 	}
69151920Seric #if LA_TYPE == LA_INT
69257736Seric 	if (tTd(3, 5))
69357736Seric 	{
69457736Seric 		printf("getla: avenrun = %d", avenrun[0]);
69557736Seric 		if (tTd(3, 15))
69657736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
69757736Seric 		printf("\n");
69857736Seric 	}
69957736Seric 	if (tTd(3, 1))
70057736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
70124943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
70251920Seric #else
70357736Seric 	if (tTd(3, 5))
70457736Seric 	{
70557736Seric 		printf("getla: avenrun = %g", avenrun[0]);
70657736Seric 		if (tTd(3, 15))
70757736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
70857736Seric 		printf("\n");
70957736Seric 	}
71057736Seric 	if (tTd(3, 1))
71157736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
71251920Seric 	return ((int) (avenrun[0] + 0.5));
71351920Seric #endif
71414872Seric }
71514872Seric 
71651773Seric #else
71751920Seric #if LA_TYPE == LA_SUBR
71851773Seric 
71951773Seric getla()
72051773Seric {
72151920Seric 	double avenrun[3];
72251920Seric 
72351920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
72457736Seric 	{
72557736Seric 		if (tTd(3, 1))
72657736Seric 			perror("getla: getloadavg failed:");
72751920Seric 		return (-1);
72857736Seric 	}
72957736Seric 	if (tTd(3, 1))
73057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
73151920Seric 	return ((int) (avenrun[0] + 0.5));
73251773Seric }
73351773Seric 
73451773Seric #else
73551773Seric 
73651773Seric getla()
73751773Seric {
73857736Seric 	if (tTd(3, 1))
73957736Seric 		printf("getla: ZERO\n");
74051920Seric 	return (0);
74151773Seric }
74251773Seric 
74351773Seric #endif
74451773Seric #endif
74524943Seric /*
74624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
74724943Seric **
74824943Seric **	Compares the message cost to the load average to decide.
74924943Seric **
75024943Seric **	Parameters:
75124943Seric **		pri -- the priority of the message in question.
75257438Seric **		ctime -- the message creation time.
75324943Seric **
75424943Seric **	Returns:
75524943Seric **		TRUE -- if this message should be queued up for the
75624943Seric **			time being.
75724943Seric **		FALSE -- if the load is low enough to send this message.
75824943Seric **
75924943Seric **	Side Effects:
76024943Seric **		none.
76124943Seric */
76224943Seric 
76324943Seric bool
76457438Seric shouldqueue(pri, ctime)
76524943Seric 	long pri;
76657438Seric 	time_t ctime;
76724943Seric {
76851920Seric 	if (CurrentLA < QueueLA)
76924943Seric 		return (FALSE);
77058132Seric 	if (CurrentLA >= RefuseLA)
77158132Seric 		return (TRUE);
77251920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77324943Seric }
77424943Seric /*
77553037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
77653037Seric **
77753037Seric **	Parameters:
77853037Seric **		none.
77953037Seric **
78053037Seric **	Returns:
78153037Seric **		TRUE if incoming SMTP connections should be refused
78253037Seric **			(for now).
78353037Seric **		FALSE if we should accept new work.
78453037Seric **
78553037Seric **	Side Effects:
78653037Seric **		none.
78753037Seric */
78853037Seric 
78953037Seric bool
79053037Seric refuseconnections()
79153037Seric {
79259156Seric #ifdef XLA
79359156Seric 	if (!xla_smtp_ok())
79459156Seric 		return TRUE;
79559156Seric #endif
79659156Seric 
79753037Seric 	/* this is probably too simplistic */
79858132Seric 	return (CurrentLA >= RefuseLA);
79953037Seric }
80053037Seric /*
80124943Seric **  SETPROCTITLE -- set process title for ps
80224943Seric **
80324943Seric **	Parameters:
80458674Seric **		fmt -- a printf style format string.
80558674Seric **		a, b, c -- possible parameters to fmt.
80624943Seric **
80724943Seric **	Returns:
80824943Seric **		none.
80924943Seric **
81024943Seric **	Side Effects:
81124943Seric **		Clobbers argv of our main procedure so ps(1) will
81224943Seric **		display the title.
81324943Seric */
81424943Seric 
81558689Seric #ifdef SETPROCTITLE
81658689Seric # ifdef __hpux
81758689Seric #  include <sys/pstat.h>
81858689Seric # endif
81959732Seric # ifdef BSD4_4
82059732Seric #  include <machine/vmparam.h>
82159732Seric #  include <sys/exec.h>
82259992Seric #  ifdef PS_STRINGS
82359992Seric #   define SETPROC_STATIC static
82459992Seric #  endif
82559732Seric # endif
82659732Seric # ifndef SETPROC_STATIC
82759732Seric #  define SETPROC_STATIC
82859732Seric # endif
82958689Seric #endif
83058689Seric 
83124943Seric /*VARARGS1*/
83257642Seric #ifdef __STDC__
83357642Seric setproctitle(char *fmt, ...)
83457642Seric #else
83557642Seric setproctitle(fmt, va_alist)
83624943Seric 	char *fmt;
83757642Seric 	va_dcl
83857642Seric #endif
83924943Seric {
84024943Seric # ifdef SETPROCTITLE
84124943Seric 	register char *p;
84225049Seric 	register int i;
84359732Seric 	SETPROC_STATIC char buf[MAXLINE];
84456852Seric 	VA_LOCAL_DECL
84558689Seric #  ifdef __hpux
84658689Seric 	union pstun pst;
84758689Seric #  endif
84824943Seric 	extern char **Argv;
84924943Seric 	extern char *LastArgv;
85024943Seric 
85158674Seric 	p = buf;
85224943Seric 
85358674Seric 	/* print sendmail: heading for grep */
85458674Seric 	(void) strcpy(p, "sendmail: ");
85558674Seric 	p += strlen(p);
85624943Seric 
85758674Seric 	/* print the argument string */
85858674Seric 	VA_START(fmt);
85958674Seric 	(void) vsprintf(p, fmt, ap);
86056852Seric 	VA_END;
86154996Seric 
86258674Seric 	i = strlen(buf);
86358689Seric 
86458689Seric #  ifdef __hpux
86558689Seric 	pst.pst_command = buf;
86658689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
86758689Seric #  else
86859992Seric #   ifdef PS_STRINGS
86959732Seric 	PS_STRINGS->ps_nargvstr = 1;
87059732Seric 	PS_STRINGS->ps_argvstr = buf;
87159732Seric #   else
87254996Seric 	if (i > LastArgv - Argv[0] - 2)
87325049Seric 	{
87454996Seric 		i = LastArgv - Argv[0] - 2;
87558674Seric 		buf[i] = '\0';
87625049Seric 	}
87758674Seric 	(void) strcpy(Argv[0], buf);
87854997Seric 	p = &Argv[0][i];
87924943Seric 	while (p < LastArgv)
88024943Seric 		*p++ = ' ';
88159732Seric #   endif
88258689Seric #  endif
88356795Seric # endif /* SETPROCTITLE */
88424943Seric }
88525698Seric /*
88625698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88725698Seric **
88825698Seric **	Parameters:
88925698Seric **		none.
89025698Seric **
89125698Seric **	Returns:
89225698Seric **		none.
89325698Seric **
89425698Seric **	Side Effects:
89525698Seric **		Picks up extant zombies.
89625698Seric */
89725698Seric 
89825698Seric # include <sys/wait.h>
89925698Seric 
90046928Sbostic void
90125698Seric reapchild()
90225698Seric {
90360219Seric # ifdef WIFEXITED
90460219Seric 	auto int status;
90560560Seric 	int count;
90660560Seric 	int pid;
90760219Seric 
90860560Seric 	count = 0;
90960560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
91060560Seric 	{
91160560Seric 		if (count++ > 1000)
91260560Seric 		{
91360560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
91460560Seric 				pid, status);
91560560Seric 			break;
91660560Seric 		}
91760560Seric 	}
91860219Seric # else
91925698Seric # ifdef WNOHANG
92025698Seric 	union wait status;
92125698Seric 
92246928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
92325698Seric 		continue;
92456795Seric # else /* WNOHANG */
92525698Seric 	auto int status;
92625698Seric 
92760219Seric 	while (wait(&status) > 0)
92825698Seric 		continue;
92956795Seric # endif /* WNOHANG */
93060219Seric # endif
93158061Seric # ifdef SYSTEM5
93258061Seric 	(void) signal(SIGCHLD, reapchild);
93358061Seric # endif
93425698Seric }
93555418Seric /*
93655418Seric **  UNSETENV -- remove a variable from the environment
93755418Seric **
93855418Seric **	Not needed on newer systems.
93955418Seric **
94055418Seric **	Parameters:
94155418Seric **		name -- the string name of the environment variable to be
94255418Seric **			deleted from the current environment.
94355418Seric **
94455418Seric **	Returns:
94555418Seric **		none.
94655418Seric **
94755418Seric **	Globals:
94855418Seric **		environ -- a pointer to the current environment.
94955418Seric **
95055418Seric **	Side Effects:
95155418Seric **		Modifies environ.
95255418Seric */
95355418Seric 
95455418Seric #ifdef UNSETENV
95555418Seric 
95655418Seric void
95755418Seric unsetenv(name)
95855418Seric 	char *name;
95955418Seric {
96055418Seric 	extern char **environ;
96155418Seric 	register char **pp;
96255418Seric 	int len = strlen(name);
96355418Seric 
96455418Seric 	for (pp = environ; *pp != NULL; pp++)
96555418Seric 	{
96655418Seric 		if (strncmp(name, *pp, len) == 0 &&
96755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
96855418Seric 			break;
96955418Seric 	}
97055418Seric 
97155418Seric 	for (; *pp != NULL; pp++)
97255418Seric 		*pp = pp[1];
97355418Seric }
97455418Seric 
97555418Seric #endif /* UNSETENV */
97656215Seric /*
97756215Seric **  GETDTABLESIZE -- return number of file descriptors
97856215Seric **
97956215Seric **	Only on non-BSD systems
98056215Seric **
98156215Seric **	Parameters:
98256215Seric **		none
98356215Seric **
98456215Seric **	Returns:
98556215Seric **		size of file descriptor table
98656215Seric **
98756215Seric **	Side Effects:
98856215Seric **		none
98956215Seric */
99056215Seric 
99156215Seric #ifdef SYSTEM5
99256215Seric 
99356215Seric int
99456215Seric getdtablesize()
99556215Seric {
99658689Seric # ifdef _SC_OPEN_MAX
99758689Seric 	return sysconf(_SC_OPEN_MAX);
99858689Seric # else
99956215Seric 	return NOFILE;
100058689Seric # endif
100156215Seric }
100256215Seric 
100356215Seric #endif
100457631Seric /*
100557631Seric **  UNAME -- get the UUCP name of this system.
100657631Seric */
100757631Seric 
100857943Seric #ifndef HASUNAME
100957631Seric 
101057631Seric int
101157631Seric uname(name)
101257631Seric 	struct utsname *name;
101357631Seric {
101457631Seric 	FILE *file;
101557631Seric 	char *n;
101657631Seric 
101757631Seric 	name->nodename[0] = '\0';
101857631Seric 
101957661Seric 	/* try /etc/whoami -- one line with the node name */
102057631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
102157631Seric 	{
102257661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
102357631Seric 		(void) fclose(file);
102457661Seric 		n = strchr(name->nodename, '\n');
102557631Seric 		if (n != NULL)
102657631Seric 			*n = '\0';
102757631Seric 		if (name->nodename[0] != '\0')
102857631Seric 			return (0);
102957631Seric 	}
103057631Seric 
103157661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
103257631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
103357631Seric 	{
103457631Seric 		char buf[MAXLINE];
103557631Seric 
103657631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
103757631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
103857631Seric 					NODE_LENGTH, name->nodename) > 0)
103957631Seric 				break;
104057631Seric 		(void) fclose(file);
104157631Seric 		if (name->nodename[0] != '\0')
104257631Seric 			return (0);
104357631Seric 	}
104457631Seric 
104557631Seric #ifdef TRUST_POPEN
104657631Seric 	/*
104757631Seric 	**  Popen is known to have security holes.
104857631Seric 	*/
104957631Seric 
105057661Seric 	/* try uuname -l to return local name */
105157631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
105257631Seric 	{
105357661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
105457631Seric 		(void) pclose(file);
105557661Seric 		n = strchr(name, '\n');
105657631Seric 		if (n != NULL)
105757631Seric 			*n = '\0';
105857661Seric 		if (name->nodename[0] != '\0')
105957631Seric 			return (0);
106057631Seric 	}
106157631Seric #endif
106257631Seric 
106357631Seric 	return (-1);
106457631Seric }
106557943Seric #endif /* HASUNAME */
106658068Seric /*
106758068Seric **  INITGROUPS -- initialize groups
106858068Seric **
106958068Seric **	Stub implementation for System V style systems
107058068Seric */
107158068Seric 
107258068Seric #ifndef HASINITGROUPS
107359430Seric # if !defined(SYSTEM5) || defined(__hpux)
107458068Seric #  define HASINITGROUPS
107558068Seric # endif
107658068Seric #endif
107758068Seric 
107858068Seric #ifndef HASINITGROUPS
107958068Seric 
108058068Seric initgroups(name, basegid)
108158068Seric 	char *name;
108258068Seric 	int basegid;
108358068Seric {
108458068Seric 	return 0;
108558068Seric }
108658068Seric 
108758068Seric #endif
108858082Seric /*
108959289Seric **  SETSID -- set session id (for non-POSIX systems)
109059289Seric */
109159289Seric 
109259289Seric #ifndef HASSETSID
109359289Seric 
109459289Seric setsid()
109559289Seric {
109659289Seric # ifdef SYSTEM5
109759289Seric 	setpgrp();
109859289Seric # endif
109959289Seric }
110059289Seric 
110159289Seric #endif
110259289Seric /*
110358082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
110458082Seric **
110558082Seric **	Only implemented if you have statfs.
110658082Seric **
110758082Seric **	Parameters:
110858333Seric **		msize -- the size to check against.  If zero, we don't yet
110958333Seric **			know how big the message will be, so just check for
111058333Seric **			a "reasonable" amount.
111158082Seric **
111258082Seric **	Returns:
111358082Seric **		TRUE if there is enough space.
111458082Seric **		FALSE otherwise.
111558082Seric */
111658082Seric 
111758082Seric #ifndef HASSTATFS
111858082Seric # if defined(BSD4_4) || defined(__osf__)
111958082Seric #  define HASSTATFS
112058082Seric # endif
112158082Seric #endif
112258082Seric 
112358082Seric #ifdef HASSTATFS
112458157Seric # undef HASUSTAT
112558157Seric #endif
112658157Seric 
112758157Seric #if defined(HASUSTAT)
112858157Seric # include <ustat.h>
112958157Seric #endif
113058157Seric 
113158157Seric #ifdef HASSTATFS
113258133Seric # if defined(sgi) || defined(apollo)
113358133Seric #  include <sys/statfs.h>
113458133Seric # else
113560160Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
113658133Seric #   include <sys/vfs.h>
113758133Seric #  else
113858157Seric #   include <sys/mount.h>
113958133Seric #  endif
114058133Seric # endif
114158082Seric #endif
114258082Seric 
114358082Seric bool
114458333Seric enoughspace(msize)
114558333Seric 	long msize;
114658082Seric {
114758160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
114858157Seric # if defined(HASUSTAT)
114958153Seric 	struct ustat fs;
115058153Seric 	struct stat statbuf;
115158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
115258157Seric #  define f_bavail	f_tfree
115358157Seric # else
115458157Seric #  if defined(ultrix)
115558157Seric 	struct fs_data fs;
115658157Seric #   define f_bavail	fd_bfreen
115758366Seric #   define FSBLOCKSIZE	fs.fd_bsize
115858153Seric #  else
115958082Seric 	struct statfs fs;
116058366Seric #   define FSBLOCKSIZE	fs.f_bsize
116158153Seric #  endif
116258133Seric # endif
116358333Seric 	long blocksneeded;
116458082Seric 	extern int errno;
116558082Seric 
116658333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
116758082Seric 	{
116858082Seric 		if (tTd(4, 80))
116958133Seric 			printf("enoughspace: no threshold\n");
117058133Seric 		return TRUE;
117158133Seric 	}
117258333Seric 
117358157Seric # if defined(HASUSTAT)
117458157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
117558157Seric # else
117658157Seric #  if defined(sgi) || defined(apollo)
117758133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
117858157Seric #  else
117958157Seric #   if defined(ultrix)
118058133Seric 	if (statfs(QueueDir, &fs) > 0)
118158153Seric #   else
118258133Seric 	if (statfs(QueueDir, &fs) == 0)
118358153Seric #   endif
118458133Seric #  endif
118558133Seric # endif
118658133Seric 	{
118758133Seric 		if (tTd(4, 80))
118858333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
118958333Seric 				fs.f_bavail, msize);
119058333Seric 
119158333Seric 		/* convert msize to block count */
119258366Seric 		msize = msize / FSBLOCKSIZE + 1;
119358333Seric 		if (MinBlocksFree >= 0)
119458333Seric 			msize += MinBlocksFree;
119558333Seric 
119658333Seric 		if (fs.f_bavail < msize)
119758090Seric 		{
119858090Seric #ifdef LOG
119958090Seric 			if (LogLevel > 0)
120058090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
120158333Seric 					QueueDir, fs.f_bavail, msize);
120258090Seric #endif
120358082Seric 			return FALSE;
120458090Seric 		}
120558082Seric 	}
120658082Seric 	else if (tTd(4, 80))
120758333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
120858333Seric 			MinBlocksFree, msize, errstring(errno));
120958082Seric #endif
121058082Seric 	return TRUE;
121158082Seric }
121258542Seric /*
121358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
121458542Seric **
121558542Seric **	This looks at an errno value and tells if this is likely to
121658542Seric **	go away if retried later.
121758542Seric **
121858542Seric **	Parameters:
121958542Seric **		err -- the errno code to classify.
122058542Seric **
122158542Seric **	Returns:
122258542Seric **		TRUE if this is probably transient.
122358542Seric **		FALSE otherwise.
122458542Seric */
122558542Seric 
122658542Seric bool
122758542Seric transienterror(err)
122858542Seric 	int err;
122958542Seric {
123058542Seric 	switch (err)
123158542Seric 	{
123258542Seric 	  case EIO:			/* I/O error */
123358542Seric 	  case ENXIO:			/* Device not configured */
123458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
123558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
123658542Seric 	  case ENODEV:			/* Operation not supported by device */
123758542Seric 	  case ENFILE:			/* Too many open files in system */
123858542Seric 	  case EMFILE:			/* Too many open files */
123958542Seric 	  case ENOSPC:			/* No space left on device */
124058542Seric #ifdef ETIMEDOUT
124158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
124258542Seric #endif
124358542Seric #ifdef ESTALE
124458542Seric 	  case ESTALE:			/* Stale NFS file handle */
124558542Seric #endif
124658542Seric #ifdef ENETDOWN
124758542Seric 	  case ENETDOWN:		/* Network is down */
124858542Seric #endif
124958542Seric #ifdef ENETUNREACH
125058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
125158542Seric #endif
125258542Seric #ifdef ENETRESET
125358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
125458542Seric #endif
125558542Seric #ifdef ECONNABORTED
125658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
125758542Seric #endif
125858542Seric #ifdef ECONNRESET
125958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
126058542Seric #endif
126158542Seric #ifdef ENOBUFS
126258542Seric 	  case ENOBUFS:			/* No buffer space available */
126358542Seric #endif
126458542Seric #ifdef ESHUTDOWN
126558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
126658542Seric #endif
126758542Seric #ifdef ECONNREFUSED
126858542Seric 	  case ECONNREFUSED:		/* Connection refused */
126958542Seric #endif
127058542Seric #ifdef EHOSTDOWN
127158542Seric 	  case EHOSTDOWN:		/* Host is down */
127258542Seric #endif
127358542Seric #ifdef EHOSTUNREACH
127458542Seric 	  case EHOSTUNREACH:		/* No route to host */
127558542Seric #endif
127658542Seric #ifdef EDQUOT
127758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
127858542Seric #endif
127958542Seric #ifdef EPROCLIM
128058542Seric 	  case EPROCLIM:		/* Too many processes */
128158542Seric #endif
128258542Seric #ifdef EUSERS
128358542Seric 	  case EUSERS:			/* Too many users */
128458542Seric #endif
128558542Seric #ifdef EDEADLK
128658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
128758542Seric #endif
128858542Seric #ifdef EISCONN
128958542Seric 	  case EISCONN:			/* Socket already connected */
129058542Seric #endif
129158542Seric #ifdef EINPROGRESS
129258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
129358542Seric #endif
129458542Seric #ifdef EALREADY
129558542Seric 	  case EALREADY:		/* Operation already in progress */
129658542Seric #endif
129758542Seric #ifdef EADDRINUSE
129858542Seric 	  case EADDRINUSE:		/* Address already in use */
129958542Seric #endif
130058542Seric #ifdef EADDRNOTAVAIL
130158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
130258542Seric #endif
130358542Seric #ifdef ENOSR
130458542Seric 	  case ENOSR:			/* Out of streams resources */
130558542Seric #endif
130658542Seric 		return TRUE;
130758542Seric 	}
130858542Seric 
130958542Seric 	/* nope, must be permanent */
131058542Seric 	return FALSE;
131158542Seric }
131258689Seric /*
131358689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
131458689Seric **
131558689Seric **	Parameters:
131658689Seric **		fd -- the file descriptor of the file.
131758689Seric **		filename -- the file name (for error messages).
131858689Seric **		type -- type of the lock.  Bits can be:
131958689Seric **			LOCK_EX -- exclusive lock.
132058689Seric **			LOCK_NB -- non-blocking.
132158689Seric **
132258689Seric **	Returns:
132358689Seric **		TRUE if the lock was acquired.
132458689Seric **		FALSE otherwise.
132558689Seric */
132658689Seric 
132758689Seric bool
132858689Seric lockfile(fd, filename, type)
132958689Seric 	int fd;
133058689Seric 	char *filename;
133158689Seric 	int type;
133258689Seric {
133358689Seric # ifdef LOCKF
133458689Seric 	int action;
133558689Seric 	struct flock lfd;
133658689Seric 
133759447Seric 	if (bitset(LOCK_UN, type))
133859447Seric 		lfd.l_type = F_UNLCK;
133959447Seric 	else if (bitset(LOCK_EX, type))
134058689Seric 		lfd.l_type = F_WRLCK;
134158689Seric 	else
134258689Seric 		lfd.l_type = F_RDLCK;
134358689Seric 
134458689Seric 	if (bitset(LOCK_NB, type))
134558689Seric 		action = F_SETLK;
134658689Seric 	else
134758689Seric 		action = F_SETLKW;
134858689Seric 
134958689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
135058689Seric 
135158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
135258689Seric 		return TRUE;
135358689Seric 
135458689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
135559447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
135658689Seric # else
135758689Seric 	if (flock(fd, type) >= 0)
135858689Seric 		return TRUE;
135958689Seric 
136058689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
136159447Seric 		syserr("cannot flock(%s, %o)", filename, type);
136258689Seric # endif
136358689Seric 	return FALSE;
136458689Seric }
1365