xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 63753)
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*63753Seric static char sccsid[] = "@(#)conf.c	8.2 (Berkeley) 07/11/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,
94*63753Seric 	"return-path",		H_FORCE|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
576*63753Seric #  if defined(__NeXT__)
577*63753Seric #    define LA_TYPE		LA_ZERO
578*63753Seric #  endif
57951920Seric 
58060219Seric /* now do the guesses based on general OS type */
58151920Seric #  ifndef LA_TYPE
58257736Seric #   if defined(SYSTEM5)
58357736Seric #    define LA_TYPE		LA_INT
58457736Seric #    define LA_AVENRUN		"avenrun"
58557736Seric #   else
58657736Seric #    if defined(BSD)
58757736Seric #     define LA_TYPE		LA_SUBR
58857736Seric #    else
58957736Seric #     define LA_TYPE		LA_ZERO
59057736Seric #    endif
59157736Seric #   endif
59251920Seric #  endif
59351920Seric #endif
59451920Seric 
59551920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
59651920Seric 
59714872Seric #include <nlist.h>
59814872Seric 
59951920Seric #ifndef LA_AVENRUN
60051920Seric #define LA_AVENRUN	"_avenrun"
60151920Seric #endif
60251920Seric 
60351920Seric /* _PATH_UNIX should be defined in <paths.h> */
60451920Seric #ifndef _PATH_UNIX
60559430Seric #  if defined(__hpux)
60651920Seric #    define _PATH_UNIX		"/hp-ux"
60751920Seric #  endif
60851920Seric #  if defined(mips) && !defined(ultrix)
60951920Seric      /* powerful RISC/os */
61051920Seric #    define _PATH_UNIX		"/unix"
61151920Seric #  endif
612*63753Seric #  if defined(Solaris2)
613*63753Seric      /* Solaris 2 */
614*63753Seric #    define _PATH_UNIX		"/kernel/unix"
615*63753Seric #  endif
61657736Seric #  if defined(SYSTEM5)
61757977Seric #    ifndef _PATH_UNIX
61857977Seric #      define _PATH_UNIX	"/unix"
61957977Seric #    endif
62057736Seric #  endif
62151920Seric #  ifndef _PATH_UNIX
62251920Seric #    define _PATH_UNIX		"/vmunix"
62351920Seric #  endif
62451920Seric #endif
62551920Seric 
62614872Seric struct	nlist Nl[] =
62714872Seric {
62851920Seric 	{ LA_AVENRUN },
62914872Seric #define	X_AVENRUN	0
63014872Seric 	{ 0 },
63114872Seric };
63214872Seric 
63360583Seric #ifndef FSHIFT
63460583Seric # if defined(unixpc)
63560583Seric #  define FSHIFT	5
63660583Seric # endif
63757736Seric 
63860583Seric # if defined(__alpha)
63960583Seric #  define FSHIFT	10
64060583Seric # endif
64157977Seric 
64260583Seric # if (LA_TYPE == LA_INT)
64351920Seric #  define FSHIFT	8
64460583Seric # endif
64557736Seric #endif
64660583Seric 
64757736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
64851920Seric #  define FSCALE	(1 << FSHIFT)
64951920Seric #endif
65040930Srick 
65114872Seric getla()
65214872Seric {
65314872Seric 	static int kmem = -1;
65451920Seric #if LA_TYPE == LA_INT
65524943Seric 	long avenrun[3];
65651920Seric #else
65751920Seric 	double avenrun[3];
65851920Seric #endif
65925615Seric 	extern off_t lseek();
66057736Seric 	extern int errno;
66114872Seric 
66214872Seric 	if (kmem < 0)
66314872Seric 	{
66424945Seric 		kmem = open("/dev/kmem", 0, 0);
66514872Seric 		if (kmem < 0)
66657736Seric 		{
66757736Seric 			if (tTd(3, 1))
66857736Seric 				printf("getla: open(/dev/kmem): %s\n",
66957736Seric 					errstring(errno));
67014872Seric 			return (-1);
67157736Seric 		}
67251920Seric 		(void) fcntl(kmem, F_SETFD, 1);
67357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
67457736Seric 		{
67557736Seric 			if (tTd(3, 1))
67657736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
67757736Seric 					errstring(errno));
67814872Seric 			return (-1);
67957736Seric 		}
68059253Seric 		if (Nl[X_AVENRUN].n_value == 0)
68159253Seric 		{
68259253Seric 			if (tTd(3, 1))
68359253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
68459253Seric 					_PATH_UNIX, LA_AVENRUN);
68559253Seric 			return (-1);
68659253Seric 		}
68714872Seric 	}
68857736Seric 	if (tTd(3, 20))
68957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
69024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
69123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
69219967Seric 	{
69319967Seric 		/* thank you Ian */
69457736Seric 		if (tTd(3, 1))
69557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
69619967Seric 		return (-1);
69719967Seric 	}
69851920Seric #if LA_TYPE == LA_INT
69957736Seric 	if (tTd(3, 5))
70057736Seric 	{
70157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
70257736Seric 		if (tTd(3, 15))
70357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
70457736Seric 		printf("\n");
70557736Seric 	}
70657736Seric 	if (tTd(3, 1))
70757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
70824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
70951920Seric #else
71057736Seric 	if (tTd(3, 5))
71157736Seric 	{
71257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
71357736Seric 		if (tTd(3, 15))
71457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
71557736Seric 		printf("\n");
71657736Seric 	}
71757736Seric 	if (tTd(3, 1))
71857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
71951920Seric 	return ((int) (avenrun[0] + 0.5));
72051920Seric #endif
72114872Seric }
72214872Seric 
72351773Seric #else
72451920Seric #if LA_TYPE == LA_SUBR
72551773Seric 
72651773Seric getla()
72751773Seric {
72851920Seric 	double avenrun[3];
72951920Seric 
73051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
73157736Seric 	{
73257736Seric 		if (tTd(3, 1))
73357736Seric 			perror("getla: getloadavg failed:");
73451920Seric 		return (-1);
73557736Seric 	}
73657736Seric 	if (tTd(3, 1))
73757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
73851920Seric 	return ((int) (avenrun[0] + 0.5));
73951773Seric }
74051773Seric 
74151773Seric #else
74251773Seric 
74351773Seric getla()
74451773Seric {
74557736Seric 	if (tTd(3, 1))
74657736Seric 		printf("getla: ZERO\n");
74751920Seric 	return (0);
74851773Seric }
74951773Seric 
75051773Seric #endif
75151773Seric #endif
75224943Seric /*
75324943Seric **  SHOULDQUEUE -- should this message be queued or sent?
75424943Seric **
75524943Seric **	Compares the message cost to the load average to decide.
75624943Seric **
75724943Seric **	Parameters:
75824943Seric **		pri -- the priority of the message in question.
75957438Seric **		ctime -- the message creation time.
76024943Seric **
76124943Seric **	Returns:
76224943Seric **		TRUE -- if this message should be queued up for the
76324943Seric **			time being.
76424943Seric **		FALSE -- if the load is low enough to send this message.
76524943Seric **
76624943Seric **	Side Effects:
76724943Seric **		none.
76824943Seric */
76924943Seric 
77024943Seric bool
77157438Seric shouldqueue(pri, ctime)
77224943Seric 	long pri;
77357438Seric 	time_t ctime;
77424943Seric {
77551920Seric 	if (CurrentLA < QueueLA)
77624943Seric 		return (FALSE);
77758132Seric 	if (CurrentLA >= RefuseLA)
77858132Seric 		return (TRUE);
77951920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
78024943Seric }
78124943Seric /*
78253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
78353037Seric **
78453037Seric **	Parameters:
78553037Seric **		none.
78653037Seric **
78753037Seric **	Returns:
78853037Seric **		TRUE if incoming SMTP connections should be refused
78953037Seric **			(for now).
79053037Seric **		FALSE if we should accept new work.
79153037Seric **
79253037Seric **	Side Effects:
79353037Seric **		none.
79453037Seric */
79553037Seric 
79653037Seric bool
79753037Seric refuseconnections()
79853037Seric {
79959156Seric #ifdef XLA
80059156Seric 	if (!xla_smtp_ok())
80159156Seric 		return TRUE;
80259156Seric #endif
80359156Seric 
80453037Seric 	/* this is probably too simplistic */
80558132Seric 	return (CurrentLA >= RefuseLA);
80653037Seric }
80753037Seric /*
80824943Seric **  SETPROCTITLE -- set process title for ps
80924943Seric **
81024943Seric **	Parameters:
81158674Seric **		fmt -- a printf style format string.
81258674Seric **		a, b, c -- possible parameters to fmt.
81324943Seric **
81424943Seric **	Returns:
81524943Seric **		none.
81624943Seric **
81724943Seric **	Side Effects:
81824943Seric **		Clobbers argv of our main procedure so ps(1) will
81924943Seric **		display the title.
82024943Seric */
82124943Seric 
82258689Seric #ifdef SETPROCTITLE
82358689Seric # ifdef __hpux
82458689Seric #  include <sys/pstat.h>
82558689Seric # endif
82659732Seric # ifdef BSD4_4
82759732Seric #  include <machine/vmparam.h>
82859732Seric #  include <sys/exec.h>
82959992Seric #  ifdef PS_STRINGS
83059992Seric #   define SETPROC_STATIC static
83159992Seric #  endif
83259732Seric # endif
83359732Seric # ifndef SETPROC_STATIC
83459732Seric #  define SETPROC_STATIC
83559732Seric # endif
83658689Seric #endif
83758689Seric 
83824943Seric /*VARARGS1*/
83957642Seric #ifdef __STDC__
84057642Seric setproctitle(char *fmt, ...)
84157642Seric #else
84257642Seric setproctitle(fmt, va_alist)
84324943Seric 	char *fmt;
84457642Seric 	va_dcl
84557642Seric #endif
84624943Seric {
84724943Seric # ifdef SETPROCTITLE
84824943Seric 	register char *p;
84925049Seric 	register int i;
85059732Seric 	SETPROC_STATIC char buf[MAXLINE];
85156852Seric 	VA_LOCAL_DECL
85258689Seric #  ifdef __hpux
85358689Seric 	union pstun pst;
85458689Seric #  endif
85524943Seric 	extern char **Argv;
85624943Seric 	extern char *LastArgv;
85724943Seric 
85858674Seric 	p = buf;
85924943Seric 
86058674Seric 	/* print sendmail: heading for grep */
86158674Seric 	(void) strcpy(p, "sendmail: ");
86258674Seric 	p += strlen(p);
86324943Seric 
86458674Seric 	/* print the argument string */
86558674Seric 	VA_START(fmt);
86658674Seric 	(void) vsprintf(p, fmt, ap);
86756852Seric 	VA_END;
86854996Seric 
86958674Seric 	i = strlen(buf);
87058689Seric 
87158689Seric #  ifdef __hpux
87258689Seric 	pst.pst_command = buf;
87358689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87458689Seric #  else
87559992Seric #   ifdef PS_STRINGS
87659732Seric 	PS_STRINGS->ps_nargvstr = 1;
87759732Seric 	PS_STRINGS->ps_argvstr = buf;
87859732Seric #   else
87954996Seric 	if (i > LastArgv - Argv[0] - 2)
88025049Seric 	{
88154996Seric 		i = LastArgv - Argv[0] - 2;
88258674Seric 		buf[i] = '\0';
88325049Seric 	}
88458674Seric 	(void) strcpy(Argv[0], buf);
88554997Seric 	p = &Argv[0][i];
88624943Seric 	while (p < LastArgv)
88724943Seric 		*p++ = ' ';
88859732Seric #   endif
88958689Seric #  endif
89056795Seric # endif /* SETPROCTITLE */
89124943Seric }
89225698Seric /*
89325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
89425698Seric **
89525698Seric **	Parameters:
89625698Seric **		none.
89725698Seric **
89825698Seric **	Returns:
89925698Seric **		none.
90025698Seric **
90125698Seric **	Side Effects:
90225698Seric **		Picks up extant zombies.
90325698Seric */
90425698Seric 
90525698Seric # include <sys/wait.h>
90625698Seric 
90746928Sbostic void
90825698Seric reapchild()
90925698Seric {
910*63753Seric # if defined(WIFEXITED) && !defined(__NeXT__)
91160219Seric 	auto int status;
91260560Seric 	int count;
91360560Seric 	int pid;
91460219Seric 
91560560Seric 	count = 0;
91660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
91760560Seric 	{
91860560Seric 		if (count++ > 1000)
91960560Seric 		{
92060560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
92160560Seric 				pid, status);
92260560Seric 			break;
92360560Seric 		}
92460560Seric 	}
92560219Seric # else
92625698Seric # ifdef WNOHANG
92725698Seric 	union wait status;
92825698Seric 
929*63753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
93025698Seric 		continue;
93156795Seric # else /* WNOHANG */
93225698Seric 	auto int status;
93325698Seric 
93460219Seric 	while (wait(&status) > 0)
93525698Seric 		continue;
93656795Seric # endif /* WNOHANG */
93760219Seric # endif
93858061Seric # ifdef SYSTEM5
93958061Seric 	(void) signal(SIGCHLD, reapchild);
94058061Seric # endif
94125698Seric }
94255418Seric /*
94355418Seric **  UNSETENV -- remove a variable from the environment
94455418Seric **
94555418Seric **	Not needed on newer systems.
94655418Seric **
94755418Seric **	Parameters:
94855418Seric **		name -- the string name of the environment variable to be
94955418Seric **			deleted from the current environment.
95055418Seric **
95155418Seric **	Returns:
95255418Seric **		none.
95355418Seric **
95455418Seric **	Globals:
95555418Seric **		environ -- a pointer to the current environment.
95655418Seric **
95755418Seric **	Side Effects:
95855418Seric **		Modifies environ.
95955418Seric */
96055418Seric 
96155418Seric #ifdef UNSETENV
96255418Seric 
96355418Seric void
96455418Seric unsetenv(name)
96555418Seric 	char *name;
96655418Seric {
96755418Seric 	extern char **environ;
96855418Seric 	register char **pp;
96955418Seric 	int len = strlen(name);
97055418Seric 
97155418Seric 	for (pp = environ; *pp != NULL; pp++)
97255418Seric 	{
97355418Seric 		if (strncmp(name, *pp, len) == 0 &&
97455418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
97555418Seric 			break;
97655418Seric 	}
97755418Seric 
97855418Seric 	for (; *pp != NULL; pp++)
97955418Seric 		*pp = pp[1];
98055418Seric }
98155418Seric 
98255418Seric #endif /* UNSETENV */
98356215Seric /*
98456215Seric **  GETDTABLESIZE -- return number of file descriptors
98556215Seric **
98656215Seric **	Only on non-BSD systems
98756215Seric **
98856215Seric **	Parameters:
98956215Seric **		none
99056215Seric **
99156215Seric **	Returns:
99256215Seric **		size of file descriptor table
99356215Seric **
99456215Seric **	Side Effects:
99556215Seric **		none
99656215Seric */
99756215Seric 
99856215Seric #ifdef SYSTEM5
99956215Seric 
100056215Seric int
100156215Seric getdtablesize()
100256215Seric {
100358689Seric # ifdef _SC_OPEN_MAX
100458689Seric 	return sysconf(_SC_OPEN_MAX);
100558689Seric # else
100656215Seric 	return NOFILE;
100758689Seric # endif
100856215Seric }
100956215Seric 
101056215Seric #endif
101157631Seric /*
101257631Seric **  UNAME -- get the UUCP name of this system.
101357631Seric */
101457631Seric 
101557943Seric #ifndef HASUNAME
101657631Seric 
101757631Seric int
101857631Seric uname(name)
101957631Seric 	struct utsname *name;
102057631Seric {
102157631Seric 	FILE *file;
102257631Seric 	char *n;
102357631Seric 
102457631Seric 	name->nodename[0] = '\0';
102557631Seric 
102657661Seric 	/* try /etc/whoami -- one line with the node name */
102757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
102857631Seric 	{
102957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
103057631Seric 		(void) fclose(file);
103157661Seric 		n = strchr(name->nodename, '\n');
103257631Seric 		if (n != NULL)
103357631Seric 			*n = '\0';
103457631Seric 		if (name->nodename[0] != '\0')
103557631Seric 			return (0);
103657631Seric 	}
103757631Seric 
103857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
103957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
104057631Seric 	{
104157631Seric 		char buf[MAXLINE];
104257631Seric 
104357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
104457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
104557631Seric 					NODE_LENGTH, name->nodename) > 0)
104657631Seric 				break;
104757631Seric 		(void) fclose(file);
104857631Seric 		if (name->nodename[0] != '\0')
104957631Seric 			return (0);
105057631Seric 	}
105157631Seric 
105257631Seric #ifdef TRUST_POPEN
105357631Seric 	/*
105457631Seric 	**  Popen is known to have security holes.
105557631Seric 	*/
105657631Seric 
105757661Seric 	/* try uuname -l to return local name */
105857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
105957631Seric 	{
106057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
106157631Seric 		(void) pclose(file);
106257661Seric 		n = strchr(name, '\n');
106357631Seric 		if (n != NULL)
106457631Seric 			*n = '\0';
106557661Seric 		if (name->nodename[0] != '\0')
106657631Seric 			return (0);
106757631Seric 	}
106857631Seric #endif
106957631Seric 
107057631Seric 	return (-1);
107157631Seric }
107257943Seric #endif /* HASUNAME */
107358068Seric /*
107458068Seric **  INITGROUPS -- initialize groups
107558068Seric **
107658068Seric **	Stub implementation for System V style systems
107758068Seric */
107858068Seric 
107958068Seric #ifndef HASINITGROUPS
108059430Seric # if !defined(SYSTEM5) || defined(__hpux)
108158068Seric #  define HASINITGROUPS
108258068Seric # endif
108358068Seric #endif
108458068Seric 
108558068Seric #ifndef HASINITGROUPS
108658068Seric 
108758068Seric initgroups(name, basegid)
108858068Seric 	char *name;
108958068Seric 	int basegid;
109058068Seric {
109158068Seric 	return 0;
109258068Seric }
109358068Seric 
109458068Seric #endif
109558082Seric /*
109659289Seric **  SETSID -- set session id (for non-POSIX systems)
109759289Seric */
109859289Seric 
109959289Seric #ifndef HASSETSID
110059289Seric 
1101*63753Seric pid_t
1102*63753Seric setsid __P ((void))
110359289Seric {
110459289Seric # ifdef SYSTEM5
1105*63753Seric 	return setpgrp();
1106*63753Seric # else
1107*63753Seric 	return 0;
110859289Seric # endif
110959289Seric }
111059289Seric 
111159289Seric #endif
111259289Seric /*
111358082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
111458082Seric **
111558082Seric **	Only implemented if you have statfs.
111658082Seric **
111758082Seric **	Parameters:
111858333Seric **		msize -- the size to check against.  If zero, we don't yet
111958333Seric **			know how big the message will be, so just check for
112058333Seric **			a "reasonable" amount.
112158082Seric **
112258082Seric **	Returns:
112358082Seric **		TRUE if there is enough space.
112458082Seric **		FALSE otherwise.
112558082Seric */
112658082Seric 
112758082Seric #ifndef HASSTATFS
112858082Seric # if defined(BSD4_4) || defined(__osf__)
112958082Seric #  define HASSTATFS
113058082Seric # endif
113158082Seric #endif
113258082Seric 
113358082Seric #ifdef HASSTATFS
113458157Seric # undef HASUSTAT
113558157Seric #endif
113658157Seric 
113758157Seric #if defined(HASUSTAT)
113858157Seric # include <ustat.h>
113958157Seric #endif
114058157Seric 
114158157Seric #ifdef HASSTATFS
114258133Seric # if defined(sgi) || defined(apollo)
114358133Seric #  include <sys/statfs.h>
114458133Seric # else
114560160Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
114658133Seric #   include <sys/vfs.h>
114758133Seric #  else
114858157Seric #   include <sys/mount.h>
114958133Seric #  endif
115058133Seric # endif
115158082Seric #endif
115258082Seric 
115358082Seric bool
115458333Seric enoughspace(msize)
115558333Seric 	long msize;
115658082Seric {
115758160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
115858157Seric # if defined(HASUSTAT)
115958153Seric 	struct ustat fs;
116058153Seric 	struct stat statbuf;
116158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
116258157Seric #  define f_bavail	f_tfree
116358157Seric # else
116458157Seric #  if defined(ultrix)
116558157Seric 	struct fs_data fs;
116658157Seric #   define f_bavail	fd_bfreen
116758366Seric #   define FSBLOCKSIZE	fs.fd_bsize
116858153Seric #  else
116958082Seric 	struct statfs fs;
117058366Seric #   define FSBLOCKSIZE	fs.f_bsize
117158153Seric #  endif
117258133Seric # endif
117358082Seric 	extern int errno;
117458082Seric 
117558333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
117658082Seric 	{
117758082Seric 		if (tTd(4, 80))
117858133Seric 			printf("enoughspace: no threshold\n");
117958133Seric 		return TRUE;
118058133Seric 	}
118158333Seric 
118258157Seric # if defined(HASUSTAT)
118358157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
118458157Seric # else
118558157Seric #  if defined(sgi) || defined(apollo)
118658133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
118758157Seric #  else
118858157Seric #   if defined(ultrix)
118958133Seric 	if (statfs(QueueDir, &fs) > 0)
119058153Seric #   else
119158133Seric 	if (statfs(QueueDir, &fs) == 0)
119258153Seric #   endif
119358133Seric #  endif
119458133Seric # endif
119558133Seric 	{
119658133Seric 		if (tTd(4, 80))
119758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
119858333Seric 				fs.f_bavail, msize);
119958333Seric 
120058333Seric 		/* convert msize to block count */
120158366Seric 		msize = msize / FSBLOCKSIZE + 1;
120258333Seric 		if (MinBlocksFree >= 0)
120358333Seric 			msize += MinBlocksFree;
120458333Seric 
120558333Seric 		if (fs.f_bavail < msize)
120658090Seric 		{
120758090Seric #ifdef LOG
120858090Seric 			if (LogLevel > 0)
120958090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
121058333Seric 					QueueDir, fs.f_bavail, msize);
121158090Seric #endif
121258082Seric 			return FALSE;
121358090Seric 		}
121458082Seric 	}
121558082Seric 	else if (tTd(4, 80))
121658333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
121758333Seric 			MinBlocksFree, msize, errstring(errno));
121858082Seric #endif
121958082Seric 	return TRUE;
122058082Seric }
122158542Seric /*
122258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
122358542Seric **
122458542Seric **	This looks at an errno value and tells if this is likely to
122558542Seric **	go away if retried later.
122658542Seric **
122758542Seric **	Parameters:
122858542Seric **		err -- the errno code to classify.
122958542Seric **
123058542Seric **	Returns:
123158542Seric **		TRUE if this is probably transient.
123258542Seric **		FALSE otherwise.
123358542Seric */
123458542Seric 
123558542Seric bool
123658542Seric transienterror(err)
123758542Seric 	int err;
123858542Seric {
123958542Seric 	switch (err)
124058542Seric 	{
124158542Seric 	  case EIO:			/* I/O error */
124258542Seric 	  case ENXIO:			/* Device not configured */
124358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
124458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
124558542Seric 	  case ENODEV:			/* Operation not supported by device */
124658542Seric 	  case ENFILE:			/* Too many open files in system */
124758542Seric 	  case EMFILE:			/* Too many open files */
124858542Seric 	  case ENOSPC:			/* No space left on device */
124958542Seric #ifdef ETIMEDOUT
125058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
125158542Seric #endif
125258542Seric #ifdef ESTALE
125358542Seric 	  case ESTALE:			/* Stale NFS file handle */
125458542Seric #endif
125558542Seric #ifdef ENETDOWN
125658542Seric 	  case ENETDOWN:		/* Network is down */
125758542Seric #endif
125858542Seric #ifdef ENETUNREACH
125958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
126058542Seric #endif
126158542Seric #ifdef ENETRESET
126258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
126358542Seric #endif
126458542Seric #ifdef ECONNABORTED
126558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
126658542Seric #endif
126758542Seric #ifdef ECONNRESET
126858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
126958542Seric #endif
127058542Seric #ifdef ENOBUFS
127158542Seric 	  case ENOBUFS:			/* No buffer space available */
127258542Seric #endif
127358542Seric #ifdef ESHUTDOWN
127458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
127558542Seric #endif
127658542Seric #ifdef ECONNREFUSED
127758542Seric 	  case ECONNREFUSED:		/* Connection refused */
127858542Seric #endif
127958542Seric #ifdef EHOSTDOWN
128058542Seric 	  case EHOSTDOWN:		/* Host is down */
128158542Seric #endif
128258542Seric #ifdef EHOSTUNREACH
128358542Seric 	  case EHOSTUNREACH:		/* No route to host */
128458542Seric #endif
128558542Seric #ifdef EDQUOT
128658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
128758542Seric #endif
128858542Seric #ifdef EPROCLIM
128958542Seric 	  case EPROCLIM:		/* Too many processes */
129058542Seric #endif
129158542Seric #ifdef EUSERS
129258542Seric 	  case EUSERS:			/* Too many users */
129358542Seric #endif
129458542Seric #ifdef EDEADLK
129558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
129658542Seric #endif
129758542Seric #ifdef EISCONN
129858542Seric 	  case EISCONN:			/* Socket already connected */
129958542Seric #endif
130058542Seric #ifdef EINPROGRESS
130158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
130258542Seric #endif
130358542Seric #ifdef EALREADY
130458542Seric 	  case EALREADY:		/* Operation already in progress */
130558542Seric #endif
130658542Seric #ifdef EADDRINUSE
130758542Seric 	  case EADDRINUSE:		/* Address already in use */
130858542Seric #endif
130958542Seric #ifdef EADDRNOTAVAIL
131058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
131158542Seric #endif
131258542Seric #ifdef ENOSR
131358542Seric 	  case ENOSR:			/* Out of streams resources */
131458542Seric #endif
131558542Seric 		return TRUE;
131658542Seric 	}
131758542Seric 
131858542Seric 	/* nope, must be permanent */
131958542Seric 	return FALSE;
132058542Seric }
132158689Seric /*
132258689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
132358689Seric **
132458689Seric **	Parameters:
132558689Seric **		fd -- the file descriptor of the file.
132658689Seric **		filename -- the file name (for error messages).
132758689Seric **		type -- type of the lock.  Bits can be:
132858689Seric **			LOCK_EX -- exclusive lock.
132958689Seric **			LOCK_NB -- non-blocking.
133058689Seric **
133158689Seric **	Returns:
133258689Seric **		TRUE if the lock was acquired.
133358689Seric **		FALSE otherwise.
133458689Seric */
133558689Seric 
133658689Seric bool
133758689Seric lockfile(fd, filename, type)
133858689Seric 	int fd;
133958689Seric 	char *filename;
134058689Seric 	int type;
134158689Seric {
134258689Seric # ifdef LOCKF
134358689Seric 	int action;
134458689Seric 	struct flock lfd;
134558689Seric 
134659447Seric 	if (bitset(LOCK_UN, type))
134759447Seric 		lfd.l_type = F_UNLCK;
134859447Seric 	else if (bitset(LOCK_EX, type))
134958689Seric 		lfd.l_type = F_WRLCK;
135058689Seric 	else
135158689Seric 		lfd.l_type = F_RDLCK;
135258689Seric 
135358689Seric 	if (bitset(LOCK_NB, type))
135458689Seric 		action = F_SETLK;
135558689Seric 	else
135658689Seric 		action = F_SETLKW;
135758689Seric 
135858689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
135958689Seric 
136058689Seric 	if (fcntl(fd, action, &lfd) >= 0)
136158689Seric 		return TRUE;
136258689Seric 
136358689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
136459447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
136558689Seric # else
136658689Seric 	if (flock(fd, type) >= 0)
136758689Seric 		return TRUE;
136858689Seric 
136958689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
137059447Seric 		syserr("cannot flock(%s, %o)", filename, type);
137158689Seric # endif
137258689Seric 	return FALSE;
137358689Seric }
1374