xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 60207)
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*60207Seric static char sccsid[] = "@(#)conf.c	6.59 (Berkeley) 05/21/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
20756836Seric host_map_init(map, mapname, args)
20856836Seric 	MAP *map;
20956836Seric 	char *mapname;
21056836Seric 	char *args;
21156836Seric {
21256836Seric 	register char *p = args;
21356836Seric 
21456836Seric 	for (;;)
21556836Seric 	{
21658050Seric 		while (isascii(*p) && isspace(*p))
21756836Seric 			p++;
21856836Seric 		if (*p != '-')
21956836Seric 			break;
22056836Seric 		switch (*++p)
22156836Seric 		{
22256836Seric 		  case 'a':
22356836Seric 			map->map_app = ++p;
22456836Seric 			break;
22556836Seric 		}
22658050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
22756836Seric 			p++;
22856836Seric 		if (*p != '\0')
22956836Seric 			*p++ = '\0';
23056836Seric 	}
23156836Seric 	if (map->map_app != NULL)
23256836Seric 		map->map_app = newstr(map->map_app);
23356836Seric 	return TRUE;
23456836Seric }
23557402Seric /*
23657402Seric **  SETUPMAILERS -- initialize default mailers
23757402Seric */
23856836Seric 
23957402Seric setupmailers()
24057402Seric {
24157402Seric 	char buf[100];
24257402Seric 
24357403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
24457403Seric 	makemailer(buf);
24557403Seric 
24659883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
24757402Seric 	makemailer(buf);
24857402Seric 
24957402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
25057402Seric 	makemailer(buf);
25157402Seric }
25256836Seric /*
253*60207Seric **  SETUPMAPS -- set up map classes
254*60207Seric */
255*60207Seric 
256*60207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
257*60207Seric 	{ \
258*60207Seric 		extern bool parse __P((MAP *, char *)); \
259*60207Seric 		extern bool open __P((MAP *, int)); \
260*60207Seric 		extern void close __P((MAP *)); \
261*60207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
262*60207Seric 		extern void store __P((MAP *, char *, char *)); \
263*60207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
264*60207Seric 		s->s_mapclass.map_cname = name; \
265*60207Seric 		s->s_mapclass.map_ext = ext; \
266*60207Seric 		s->s_mapclass.map_cflags = flags; \
267*60207Seric 		s->s_mapclass.map_parse = parse; \
268*60207Seric 		s->s_mapclass.map_open = open; \
269*60207Seric 		s->s_mapclass.map_close = close; \
270*60207Seric 		s->s_mapclass.map_lookup = lookup; \
271*60207Seric 		s->s_mapclass.map_store = store; \
272*60207Seric 	}
273*60207Seric 
274*60207Seric setupmaps()
275*60207Seric {
276*60207Seric 	register STAB *s;
277*60207Seric 
278*60207Seric #ifdef NEWDB
279*60207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
280*60207Seric 		map_parseargs, hash_map_open, db_map_close,
281*60207Seric 		db_map_lookup, db_map_store);
282*60207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
283*60207Seric 		map_parseargs, bt_map_open, db_map_close,
284*60207Seric 		db_map_lookup, db_map_store);
285*60207Seric #endif
286*60207Seric 
287*60207Seric #ifdef NDBM
288*60207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
289*60207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
290*60207Seric 		ndbm_map_lookup, ndbm_map_store);
291*60207Seric #endif
292*60207Seric 
293*60207Seric #ifdef NIS
294*60207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
295*60207Seric 		map_parseargs, nis_map_open, nis_map_close,
296*60207Seric 		nis_map_lookup, nis_map_store);
297*60207Seric #endif
298*60207Seric 
299*60207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
300*60207Seric 		map_parseargs, stab_map_open, stab_map_close,
301*60207Seric 		stab_map_lookup, stab_map_store);
302*60207Seric 
303*60207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
304*60207Seric 		map_parseargs, impl_map_open, impl_map_close,
305*60207Seric 		impl_map_lookup, impl_map_store);
306*60207Seric 
307*60207Seric 	/* host DNS lookup */
308*60207Seric 	MAPDEF("host", NULL, 0,
309*60207Seric 		host_map_init, null_map_open, null_map_close,
310*60207Seric 		host_map_lookup, null_map_store);
311*60207Seric 
312*60207Seric 	/* dequote map */
313*60207Seric 	MAPDEF("dequote", NULL, 0,
314*60207Seric 		dequote_init, null_map_open, null_map_close,
315*60207Seric 		dequote_map, null_map_store);
316*60207Seric 
317*60207Seric #if 0
318*60207Seric # ifdef USERDB
319*60207Seric 	/* user database */
320*60207Seric 	MAPDEF("udb", ".db", 0,
321*60207Seric 		udb_map_parse, null_map_open, null_map_close,
322*60207Seric 		udb_map_lookup, null_map_store);
323*60207Seric # endif
324*60207Seric #endif
325*60207Seric }
326*60207Seric 
327*60207Seric #undef MAPDEF
328*60207Seric /*
3294326Seric **  GETRUID -- get real user id (V7)
3304326Seric */
3314326Seric 
3324326Seric getruid()
3334326Seric {
3349274Seric 	if (OpMode == MD_DAEMON)
3354536Seric 		return (RealUid);
3364536Seric 	else
3374536Seric 		return (getuid());
3384326Seric }
3394326Seric 
3404326Seric 
3414326Seric /*
3424326Seric **  GETRGID -- get real group id (V7).
3434326Seric */
3444326Seric 
3454326Seric getrgid()
3464326Seric {
3479274Seric 	if (OpMode == MD_DAEMON)
3484536Seric 		return (RealGid);
3494536Seric 	else
3504536Seric 		return (getgid());
3514326Seric }
35253654Seric /*
3539369Seric **  USERNAME -- return the user id of the logged in user.
3549369Seric **
3559369Seric **	Parameters:
3569369Seric **		none.
3579369Seric **
3589369Seric **	Returns:
3599369Seric **		The login name of the logged in user.
3609369Seric **
3619369Seric **	Side Effects:
3629369Seric **		none.
3639369Seric **
3649369Seric **	Notes:
3659369Seric **		The return value is statically allocated.
3669369Seric */
3679369Seric 
3689369Seric char *
3699369Seric username()
3709369Seric {
37117469Seric 	static char *myname = NULL;
3729369Seric 	extern char *getlogin();
37319904Smiriam 	register struct passwd *pw;
3749369Seric 
37517469Seric 	/* cache the result */
37617469Seric 	if (myname == NULL)
37717469Seric 	{
37817469Seric 		myname = getlogin();
37917469Seric 		if (myname == NULL || myname[0] == '\0')
38017469Seric 		{
38117469Seric 			pw = getpwuid(getruid());
38217469Seric 			if (pw != NULL)
38340993Sbostic 				myname = newstr(pw->pw_name);
38417469Seric 		}
38519904Smiriam 		else
38619904Smiriam 		{
38758736Seric 			uid_t uid = getuid();
38819873Smiriam 
38940993Sbostic 			myname = newstr(myname);
39040993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
39158736Seric 			      (uid != 0 && uid != pw->pw_uid))
39219904Smiriam 			{
39358736Seric 				pw = getpwuid(uid);
39424945Seric 				if (pw != NULL)
39540993Sbostic 					myname = newstr(pw->pw_name);
39619873Smiriam 			}
39719873Smiriam 		}
39817469Seric 		if (myname == NULL || myname[0] == '\0')
39917469Seric 		{
40058151Seric 			syserr("554 Who are you?");
40117469Seric 			myname = "postmaster";
40217469Seric 		}
40317469Seric 	}
40417469Seric 
40517469Seric 	return (myname);
4069369Seric }
4079369Seric /*
4084190Seric **  TTYPATH -- Get the path of the user's tty
409294Seric **
410294Seric **	Returns the pathname of the user's tty.  Returns NULL if
411294Seric **	the user is not logged in or if s/he has write permission
412294Seric **	denied.
413294Seric **
414294Seric **	Parameters:
415294Seric **		none
416294Seric **
417294Seric **	Returns:
418294Seric **		pathname of the user's tty.
419294Seric **		NULL if not logged in or write permission denied.
420294Seric **
421294Seric **	Side Effects:
422294Seric **		none.
423294Seric **
424294Seric **	WARNING:
425294Seric **		Return value is in a local buffer.
426294Seric **
427294Seric **	Called By:
428294Seric **		savemail
429294Seric */
430294Seric 
431294Seric char *
432294Seric ttypath()
433294Seric {
434294Seric 	struct stat stbuf;
435294Seric 	register char *pathn;
436294Seric 	extern char *ttyname();
4374081Seric 	extern char *getlogin();
438294Seric 
439294Seric 	/* compute the pathname of the controlling tty */
4409369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4419369Seric 	    (pathn = ttyname(0)) == NULL)
442294Seric 	{
443294Seric 		errno = 0;
444294Seric 		return (NULL);
445294Seric 	}
446294Seric 
447294Seric 	/* see if we have write permission */
4482967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
449294Seric 	{
450294Seric 		errno = 0;
451294Seric 		return (NULL);
452294Seric 	}
453294Seric 
454294Seric 	/* see if the user is logged in */
455294Seric 	if (getlogin() == NULL)
456294Seric 		return (NULL);
457294Seric 
458294Seric 	/* looks good */
459294Seric 	return (pathn);
460294Seric }
4612967Seric /*
4622967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4632967Seric **
4642967Seric **	This routine can be supplied on a per-installation basis
4652967Seric **	to determine whether a person is allowed to send a message.
4662967Seric **	This allows restriction of certain types of internet
4672967Seric **	forwarding or registration of users.
4682967Seric **
4692967Seric **	If the hosts are found to be incompatible, an error
47057454Seric **	message should be given using "usrerr" and 0 should
4712967Seric **	be returned.
4722967Seric **
4734288Seric **	'NoReturn' can be set to suppress the return-to-sender
4744288Seric **	function; this should be done on huge messages.
4754288Seric **
4762967Seric **	Parameters:
4772967Seric **		to -- the person being sent to.
4782967Seric **
4792967Seric **	Returns:
48057459Seric **		an exit status
4812967Seric **
4822967Seric **	Side Effects:
4832967Seric **		none (unless you include the usrerr stuff)
4842967Seric */
4852967Seric 
48655012Seric checkcompat(to, e)
4872967Seric 	register ADDRESS *to;
48855012Seric 	register ENVELOPE *e;
4892967Seric {
49012133Seric # ifdef lint
49112133Seric 	if (to == NULL)
49212133Seric 		to++;
49312133Seric # endif lint
49410698Seric # ifdef EXAMPLE_CODE
49510698Seric 	/* this code is intended as an example only */
4964437Seric 	register STAB *s;
4974437Seric 
4984437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49955012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
5009369Seric 	    to->q_mailer == s->s_mailer)
5014437Seric 	{
50258151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50310698Seric 		/* NoReturn = TRUE; to supress return copy */
50457459Seric 		return (EX_UNAVAILABLE);
5054437Seric 	}
50656795Seric # endif /* EXAMPLE_CODE */
50757459Seric 	return (EX_OK);
5082967Seric }
5099369Seric /*
5109369Seric **  HOLDSIGS -- arrange to hold all signals
5119369Seric **
5129369Seric **	Parameters:
5139369Seric **		none.
5149369Seric **
5159369Seric **	Returns:
5169369Seric **		none.
5179369Seric **
5189369Seric **	Side Effects:
5199369Seric **		Arranges that signals are held.
5209369Seric */
5219369Seric 
5229369Seric holdsigs()
5239369Seric {
5249369Seric }
5259369Seric /*
5269369Seric **  RLSESIGS -- arrange to release all signals
5279369Seric **
5289369Seric **	This undoes the effect of holdsigs.
5299369Seric **
5309369Seric **	Parameters:
5319369Seric **		none.
5329369Seric **
5339369Seric **	Returns:
5349369Seric **		none.
5359369Seric **
5369369Seric **	Side Effects:
5379369Seric **		Arranges that signals are released.
5389369Seric */
5399369Seric 
5409369Seric rlsesigs()
5419369Seric {
5429369Seric }
54314872Seric /*
54414872Seric **  GETLA -- get the current load average
54514872Seric **
54614881Seric **	This code stolen from la.c.
54714881Seric **
54814872Seric **	Parameters:
54914872Seric **		none.
55014872Seric **
55114872Seric **	Returns:
55214872Seric **		The current load average as an integer.
55314872Seric **
55414872Seric **	Side Effects:
55514872Seric **		none.
55614872Seric */
55714872Seric 
55851920Seric /* try to guess what style of load average we have */
55951920Seric #define LA_ZERO		1	/* always return load average as zero */
56051920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
56151920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
56251920Seric #define LA_SUBR		4	/* call getloadavg */
56314872Seric 
56451920Seric #ifndef LA_TYPE
56560160Seric #  if defined(sun) && !defined(BSD)
56651920Seric #    define LA_TYPE		LA_INT
56751920Seric #  endif
56857977Seric #  if defined(mips) || defined(__alpha)
56957977Seric      /* Ultrix or OSF/1 or RISC/os */
57051920Seric #    define LA_TYPE		LA_INT
57151920Seric #    define LA_AVENRUN		"avenrun"
57251920Seric #  endif
57359430Seric #  if defined(__hpux)
57451920Seric #    define LA_TYPE		LA_FLOAT
57559253Seric #    define LA_AVENRUN		"avenrun"
57651920Seric #  endif
57751920Seric 
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 
62657736Seric #if defined(unixpc)
62757736Seric # define FSHIFT		5
62857736Seric #endif
62957736Seric 
63057977Seric #if defined(__alpha)
63157977Seric # define FSHIFT		10
63257977Seric #endif
63357977Seric 
63451920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
63551920Seric #  define FSHIFT	8
63657736Seric #endif
63757736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
63851920Seric #  define FSCALE	(1 << FSHIFT)
63951920Seric #endif
64040930Srick 
64114872Seric getla()
64214872Seric {
64314872Seric 	static int kmem = -1;
64451920Seric #if LA_TYPE == LA_INT
64524943Seric 	long avenrun[3];
64651920Seric #else
64751920Seric 	double avenrun[3];
64851920Seric #endif
64925615Seric 	extern off_t lseek();
65057736Seric 	extern int errno;
65114872Seric 
65214872Seric 	if (kmem < 0)
65314872Seric 	{
65424945Seric 		kmem = open("/dev/kmem", 0, 0);
65514872Seric 		if (kmem < 0)
65657736Seric 		{
65757736Seric 			if (tTd(3, 1))
65857736Seric 				printf("getla: open(/dev/kmem): %s\n",
65957736Seric 					errstring(errno));
66014872Seric 			return (-1);
66157736Seric 		}
66251920Seric 		(void) fcntl(kmem, F_SETFD, 1);
66357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
66457736Seric 		{
66557736Seric 			if (tTd(3, 1))
66657736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
66757736Seric 					errstring(errno));
66814872Seric 			return (-1);
66957736Seric 		}
67059253Seric 		if (Nl[X_AVENRUN].n_value == 0)
67159253Seric 		{
67259253Seric 			if (tTd(3, 1))
67359253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
67459253Seric 					_PATH_UNIX, LA_AVENRUN);
67559253Seric 			return (-1);
67659253Seric 		}
67714872Seric 	}
67857736Seric 	if (tTd(3, 20))
67957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
68024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
68123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
68219967Seric 	{
68319967Seric 		/* thank you Ian */
68457736Seric 		if (tTd(3, 1))
68557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
68619967Seric 		return (-1);
68719967Seric 	}
68851920Seric #if LA_TYPE == LA_INT
68957736Seric 	if (tTd(3, 5))
69057736Seric 	{
69157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
69257736Seric 		if (tTd(3, 15))
69357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
69457736Seric 		printf("\n");
69557736Seric 	}
69657736Seric 	if (tTd(3, 1))
69757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69951920Seric #else
70057736Seric 	if (tTd(3, 5))
70157736Seric 	{
70257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
70357736Seric 		if (tTd(3, 15))
70457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
70557736Seric 		printf("\n");
70657736Seric 	}
70757736Seric 	if (tTd(3, 1))
70857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
70951920Seric 	return ((int) (avenrun[0] + 0.5));
71051920Seric #endif
71114872Seric }
71214872Seric 
71351773Seric #else
71451920Seric #if LA_TYPE == LA_SUBR
71551773Seric 
71651773Seric getla()
71751773Seric {
71851920Seric 	double avenrun[3];
71951920Seric 
72051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
72157736Seric 	{
72257736Seric 		if (tTd(3, 1))
72357736Seric 			perror("getla: getloadavg failed:");
72451920Seric 		return (-1);
72557736Seric 	}
72657736Seric 	if (tTd(3, 1))
72757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72851920Seric 	return ((int) (avenrun[0] + 0.5));
72951773Seric }
73051773Seric 
73151773Seric #else
73251773Seric 
73351773Seric getla()
73451773Seric {
73557736Seric 	if (tTd(3, 1))
73657736Seric 		printf("getla: ZERO\n");
73751920Seric 	return (0);
73851773Seric }
73951773Seric 
74051773Seric #endif
74151773Seric #endif
74224943Seric /*
74324943Seric **  SHOULDQUEUE -- should this message be queued or sent?
74424943Seric **
74524943Seric **	Compares the message cost to the load average to decide.
74624943Seric **
74724943Seric **	Parameters:
74824943Seric **		pri -- the priority of the message in question.
74957438Seric **		ctime -- the message creation time.
75024943Seric **
75124943Seric **	Returns:
75224943Seric **		TRUE -- if this message should be queued up for the
75324943Seric **			time being.
75424943Seric **		FALSE -- if the load is low enough to send this message.
75524943Seric **
75624943Seric **	Side Effects:
75724943Seric **		none.
75824943Seric */
75924943Seric 
76024943Seric bool
76157438Seric shouldqueue(pri, ctime)
76224943Seric 	long pri;
76357438Seric 	time_t ctime;
76424943Seric {
76551920Seric 	if (CurrentLA < QueueLA)
76624943Seric 		return (FALSE);
76758132Seric 	if (CurrentLA >= RefuseLA)
76858132Seric 		return (TRUE);
76951920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77024943Seric }
77124943Seric /*
77253037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
77353037Seric **
77453037Seric **	Parameters:
77553037Seric **		none.
77653037Seric **
77753037Seric **	Returns:
77853037Seric **		TRUE if incoming SMTP connections should be refused
77953037Seric **			(for now).
78053037Seric **		FALSE if we should accept new work.
78153037Seric **
78253037Seric **	Side Effects:
78353037Seric **		none.
78453037Seric */
78553037Seric 
78653037Seric bool
78753037Seric refuseconnections()
78853037Seric {
78959156Seric #ifdef XLA
79059156Seric 	if (!xla_smtp_ok())
79159156Seric 		return TRUE;
79259156Seric #endif
79359156Seric 
79453037Seric 	/* this is probably too simplistic */
79558132Seric 	return (CurrentLA >= RefuseLA);
79653037Seric }
79753037Seric /*
79824943Seric **  SETPROCTITLE -- set process title for ps
79924943Seric **
80024943Seric **	Parameters:
80158674Seric **		fmt -- a printf style format string.
80258674Seric **		a, b, c -- possible parameters to fmt.
80324943Seric **
80424943Seric **	Returns:
80524943Seric **		none.
80624943Seric **
80724943Seric **	Side Effects:
80824943Seric **		Clobbers argv of our main procedure so ps(1) will
80924943Seric **		display the title.
81024943Seric */
81124943Seric 
81258689Seric #ifdef SETPROCTITLE
81358689Seric # ifdef __hpux
81458689Seric #  include <sys/pstat.h>
81558689Seric # endif
81659732Seric # ifdef BSD4_4
81759732Seric #  include <machine/vmparam.h>
81859732Seric #  include <sys/exec.h>
81959992Seric #  ifdef PS_STRINGS
82059992Seric #   define SETPROC_STATIC static
82159992Seric #  endif
82259732Seric # endif
82359732Seric # ifndef SETPROC_STATIC
82459732Seric #  define SETPROC_STATIC
82559732Seric # endif
82658689Seric #endif
82758689Seric 
82824943Seric /*VARARGS1*/
82957642Seric #ifdef __STDC__
83057642Seric setproctitle(char *fmt, ...)
83157642Seric #else
83257642Seric setproctitle(fmt, va_alist)
83324943Seric 	char *fmt;
83457642Seric 	va_dcl
83557642Seric #endif
83624943Seric {
83724943Seric # ifdef SETPROCTITLE
83824943Seric 	register char *p;
83925049Seric 	register int i;
84059732Seric 	SETPROC_STATIC char buf[MAXLINE];
84156852Seric 	VA_LOCAL_DECL
84258689Seric #  ifdef __hpux
84358689Seric 	union pstun pst;
84458689Seric #  endif
84524943Seric 	extern char **Argv;
84624943Seric 	extern char *LastArgv;
84724943Seric 
84858674Seric 	p = buf;
84924943Seric 
85058674Seric 	/* print sendmail: heading for grep */
85158674Seric 	(void) strcpy(p, "sendmail: ");
85258674Seric 	p += strlen(p);
85324943Seric 
85458674Seric 	/* print the argument string */
85558674Seric 	VA_START(fmt);
85658674Seric 	(void) vsprintf(p, fmt, ap);
85756852Seric 	VA_END;
85854996Seric 
85958674Seric 	i = strlen(buf);
86058689Seric 
86158689Seric #  ifdef __hpux
86258689Seric 	pst.pst_command = buf;
86358689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
86458689Seric #  else
86559992Seric #   ifdef PS_STRINGS
86659732Seric 	PS_STRINGS->ps_nargvstr = 1;
86759732Seric 	PS_STRINGS->ps_argvstr = buf;
86859732Seric #   else
86954996Seric 	if (i > LastArgv - Argv[0] - 2)
87025049Seric 	{
87154996Seric 		i = LastArgv - Argv[0] - 2;
87258674Seric 		buf[i] = '\0';
87325049Seric 	}
87458674Seric 	(void) strcpy(Argv[0], buf);
87554997Seric 	p = &Argv[0][i];
87624943Seric 	while (p < LastArgv)
87724943Seric 		*p++ = ' ';
87859732Seric #   endif
87958689Seric #  endif
88056795Seric # endif /* SETPROCTITLE */
88124943Seric }
88225698Seric /*
88325698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88425698Seric **
88525698Seric **	Parameters:
88625698Seric **		none.
88725698Seric **
88825698Seric **	Returns:
88925698Seric **		none.
89025698Seric **
89125698Seric **	Side Effects:
89225698Seric **		Picks up extant zombies.
89325698Seric */
89425698Seric 
89525698Seric # include <sys/wait.h>
89625698Seric 
89746928Sbostic void
89825698Seric reapchild()
89925698Seric {
90025698Seric # ifdef WNOHANG
90125698Seric 	union wait status;
90225698Seric 
90346928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
90425698Seric 		continue;
90556795Seric # else /* WNOHANG */
90625698Seric 	auto int status;
90725698Seric 
90846928Sbostic 	while (wait((int *)&status) > 0)
90925698Seric 		continue;
91056795Seric # endif /* WNOHANG */
91158061Seric # ifdef SYSTEM5
91258061Seric 	(void) signal(SIGCHLD, reapchild);
91358061Seric # endif
91425698Seric }
91555418Seric /*
91655418Seric **  UNSETENV -- remove a variable from the environment
91755418Seric **
91855418Seric **	Not needed on newer systems.
91955418Seric **
92055418Seric **	Parameters:
92155418Seric **		name -- the string name of the environment variable to be
92255418Seric **			deleted from the current environment.
92355418Seric **
92455418Seric **	Returns:
92555418Seric **		none.
92655418Seric **
92755418Seric **	Globals:
92855418Seric **		environ -- a pointer to the current environment.
92955418Seric **
93055418Seric **	Side Effects:
93155418Seric **		Modifies environ.
93255418Seric */
93355418Seric 
93455418Seric #ifdef UNSETENV
93555418Seric 
93655418Seric void
93755418Seric unsetenv(name)
93855418Seric 	char *name;
93955418Seric {
94055418Seric 	extern char **environ;
94155418Seric 	register char **pp;
94255418Seric 	int len = strlen(name);
94355418Seric 
94455418Seric 	for (pp = environ; *pp != NULL; pp++)
94555418Seric 	{
94655418Seric 		if (strncmp(name, *pp, len) == 0 &&
94755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
94855418Seric 			break;
94955418Seric 	}
95055418Seric 
95155418Seric 	for (; *pp != NULL; pp++)
95255418Seric 		*pp = pp[1];
95355418Seric }
95455418Seric 
95555418Seric #endif /* UNSETENV */
95656215Seric /*
95756215Seric **  GETDTABLESIZE -- return number of file descriptors
95856215Seric **
95956215Seric **	Only on non-BSD systems
96056215Seric **
96156215Seric **	Parameters:
96256215Seric **		none
96356215Seric **
96456215Seric **	Returns:
96556215Seric **		size of file descriptor table
96656215Seric **
96756215Seric **	Side Effects:
96856215Seric **		none
96956215Seric */
97056215Seric 
97156215Seric #ifdef SYSTEM5
97256215Seric 
97356215Seric int
97456215Seric getdtablesize()
97556215Seric {
97658689Seric # ifdef _SC_OPEN_MAX
97758689Seric 	return sysconf(_SC_OPEN_MAX);
97858689Seric # else
97956215Seric 	return NOFILE;
98058689Seric # endif
98156215Seric }
98256215Seric 
98356215Seric #endif
98457631Seric /*
98557631Seric **  UNAME -- get the UUCP name of this system.
98657631Seric */
98757631Seric 
98857943Seric #ifndef HASUNAME
98957631Seric 
99057631Seric int
99157631Seric uname(name)
99257631Seric 	struct utsname *name;
99357631Seric {
99457631Seric 	FILE *file;
99557631Seric 	char *n;
99657631Seric 
99757631Seric 	name->nodename[0] = '\0';
99857631Seric 
99957661Seric 	/* try /etc/whoami -- one line with the node name */
100057631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
100157631Seric 	{
100257661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
100357631Seric 		(void) fclose(file);
100457661Seric 		n = strchr(name->nodename, '\n');
100557631Seric 		if (n != NULL)
100657631Seric 			*n = '\0';
100757631Seric 		if (name->nodename[0] != '\0')
100857631Seric 			return (0);
100957631Seric 	}
101057631Seric 
101157661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
101257631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
101357631Seric 	{
101457631Seric 		char buf[MAXLINE];
101557631Seric 
101657631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
101757631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
101857631Seric 					NODE_LENGTH, name->nodename) > 0)
101957631Seric 				break;
102057631Seric 		(void) fclose(file);
102157631Seric 		if (name->nodename[0] != '\0')
102257631Seric 			return (0);
102357631Seric 	}
102457631Seric 
102557631Seric #ifdef TRUST_POPEN
102657631Seric 	/*
102757631Seric 	**  Popen is known to have security holes.
102857631Seric 	*/
102957631Seric 
103057661Seric 	/* try uuname -l to return local name */
103157631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
103257631Seric 	{
103357661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
103457631Seric 		(void) pclose(file);
103557661Seric 		n = strchr(name, '\n');
103657631Seric 		if (n != NULL)
103757631Seric 			*n = '\0';
103857661Seric 		if (name->nodename[0] != '\0')
103957631Seric 			return (0);
104057631Seric 	}
104157631Seric #endif
104257631Seric 
104357631Seric 	return (-1);
104457631Seric }
104557943Seric #endif /* HASUNAME */
104658068Seric /*
104758068Seric **  INITGROUPS -- initialize groups
104858068Seric **
104958068Seric **	Stub implementation for System V style systems
105058068Seric */
105158068Seric 
105258068Seric #ifndef HASINITGROUPS
105359430Seric # if !defined(SYSTEM5) || defined(__hpux)
105458068Seric #  define HASINITGROUPS
105558068Seric # endif
105658068Seric #endif
105758068Seric 
105858068Seric #ifndef HASINITGROUPS
105958068Seric 
106058068Seric initgroups(name, basegid)
106158068Seric 	char *name;
106258068Seric 	int basegid;
106358068Seric {
106458068Seric 	return 0;
106558068Seric }
106658068Seric 
106758068Seric #endif
106858082Seric /*
106959289Seric **  SETSID -- set session id (for non-POSIX systems)
107059289Seric */
107159289Seric 
107259289Seric #ifndef HASSETSID
107359289Seric 
107459289Seric setsid()
107559289Seric {
107659289Seric # ifdef SYSTEM5
107759289Seric 	setpgrp();
107859289Seric # endif
107959289Seric }
108059289Seric 
108159289Seric #endif
108259289Seric /*
108358082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
108458082Seric **
108558082Seric **	Only implemented if you have statfs.
108658082Seric **
108758082Seric **	Parameters:
108858333Seric **		msize -- the size to check against.  If zero, we don't yet
108958333Seric **			know how big the message will be, so just check for
109058333Seric **			a "reasonable" amount.
109158082Seric **
109258082Seric **	Returns:
109358082Seric **		TRUE if there is enough space.
109458082Seric **		FALSE otherwise.
109558082Seric */
109658082Seric 
109758082Seric #ifndef HASSTATFS
109858082Seric # if defined(BSD4_4) || defined(__osf__)
109958082Seric #  define HASSTATFS
110058082Seric # endif
110158082Seric #endif
110258082Seric 
110358082Seric #ifdef HASSTATFS
110458157Seric # undef HASUSTAT
110558157Seric #endif
110658157Seric 
110758157Seric #if defined(HASUSTAT)
110858157Seric # include <ustat.h>
110958157Seric #endif
111058157Seric 
111158157Seric #ifdef HASSTATFS
111258133Seric # if defined(sgi) || defined(apollo)
111358133Seric #  include <sys/statfs.h>
111458133Seric # else
111560160Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
111658133Seric #   include <sys/vfs.h>
111758133Seric #  else
111858157Seric #   include <sys/mount.h>
111958133Seric #  endif
112058133Seric # endif
112158082Seric #endif
112258082Seric 
112358082Seric bool
112458333Seric enoughspace(msize)
112558333Seric 	long msize;
112658082Seric {
112758160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
112858157Seric # if defined(HASUSTAT)
112958153Seric 	struct ustat fs;
113058153Seric 	struct stat statbuf;
113158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
113258157Seric #  define f_bavail	f_tfree
113358157Seric # else
113458157Seric #  if defined(ultrix)
113558157Seric 	struct fs_data fs;
113658157Seric #   define f_bavail	fd_bfreen
113758366Seric #   define FSBLOCKSIZE	fs.fd_bsize
113858153Seric #  else
113958082Seric 	struct statfs fs;
114058366Seric #   define FSBLOCKSIZE	fs.f_bsize
114158153Seric #  endif
114258133Seric # endif
114358333Seric 	long blocksneeded;
114458082Seric 	extern int errno;
114558082Seric 
114658333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
114758082Seric 	{
114858082Seric 		if (tTd(4, 80))
114958133Seric 			printf("enoughspace: no threshold\n");
115058133Seric 		return TRUE;
115158133Seric 	}
115258333Seric 
115358157Seric # if defined(HASUSTAT)
115458157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
115558157Seric # else
115658157Seric #  if defined(sgi) || defined(apollo)
115758133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
115858157Seric #  else
115958157Seric #   if defined(ultrix)
116058133Seric 	if (statfs(QueueDir, &fs) > 0)
116158153Seric #   else
116258133Seric 	if (statfs(QueueDir, &fs) == 0)
116358153Seric #   endif
116458133Seric #  endif
116558133Seric # endif
116658133Seric 	{
116758133Seric 		if (tTd(4, 80))
116858333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
116958333Seric 				fs.f_bavail, msize);
117058333Seric 
117158333Seric 		/* convert msize to block count */
117258366Seric 		msize = msize / FSBLOCKSIZE + 1;
117358333Seric 		if (MinBlocksFree >= 0)
117458333Seric 			msize += MinBlocksFree;
117558333Seric 
117658333Seric 		if (fs.f_bavail < msize)
117758090Seric 		{
117858090Seric #ifdef LOG
117958090Seric 			if (LogLevel > 0)
118058090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
118158333Seric 					QueueDir, fs.f_bavail, msize);
118258090Seric #endif
118358082Seric 			return FALSE;
118458090Seric 		}
118558082Seric 	}
118658082Seric 	else if (tTd(4, 80))
118758333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
118858333Seric 			MinBlocksFree, msize, errstring(errno));
118958082Seric #endif
119058082Seric 	return TRUE;
119158082Seric }
119258542Seric /*
119358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
119458542Seric **
119558542Seric **	This looks at an errno value and tells if this is likely to
119658542Seric **	go away if retried later.
119758542Seric **
119858542Seric **	Parameters:
119958542Seric **		err -- the errno code to classify.
120058542Seric **
120158542Seric **	Returns:
120258542Seric **		TRUE if this is probably transient.
120358542Seric **		FALSE otherwise.
120458542Seric */
120558542Seric 
120658542Seric bool
120758542Seric transienterror(err)
120858542Seric 	int err;
120958542Seric {
121058542Seric 	switch (err)
121158542Seric 	{
121258542Seric 	  case EIO:			/* I/O error */
121358542Seric 	  case ENXIO:			/* Device not configured */
121458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
121558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
121658542Seric 	  case ENODEV:			/* Operation not supported by device */
121758542Seric 	  case ENFILE:			/* Too many open files in system */
121858542Seric 	  case EMFILE:			/* Too many open files */
121958542Seric 	  case ENOSPC:			/* No space left on device */
122058542Seric #ifdef ETIMEDOUT
122158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
122258542Seric #endif
122358542Seric #ifdef ESTALE
122458542Seric 	  case ESTALE:			/* Stale NFS file handle */
122558542Seric #endif
122658542Seric #ifdef ENETDOWN
122758542Seric 	  case ENETDOWN:		/* Network is down */
122858542Seric #endif
122958542Seric #ifdef ENETUNREACH
123058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
123158542Seric #endif
123258542Seric #ifdef ENETRESET
123358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
123458542Seric #endif
123558542Seric #ifdef ECONNABORTED
123658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
123758542Seric #endif
123858542Seric #ifdef ECONNRESET
123958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
124058542Seric #endif
124158542Seric #ifdef ENOBUFS
124258542Seric 	  case ENOBUFS:			/* No buffer space available */
124358542Seric #endif
124458542Seric #ifdef ESHUTDOWN
124558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
124658542Seric #endif
124758542Seric #ifdef ECONNREFUSED
124858542Seric 	  case ECONNREFUSED:		/* Connection refused */
124958542Seric #endif
125058542Seric #ifdef EHOSTDOWN
125158542Seric 	  case EHOSTDOWN:		/* Host is down */
125258542Seric #endif
125358542Seric #ifdef EHOSTUNREACH
125458542Seric 	  case EHOSTUNREACH:		/* No route to host */
125558542Seric #endif
125658542Seric #ifdef EDQUOT
125758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
125858542Seric #endif
125958542Seric #ifdef EPROCLIM
126058542Seric 	  case EPROCLIM:		/* Too many processes */
126158542Seric #endif
126258542Seric #ifdef EUSERS
126358542Seric 	  case EUSERS:			/* Too many users */
126458542Seric #endif
126558542Seric #ifdef EDEADLK
126658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
126758542Seric #endif
126858542Seric #ifdef EISCONN
126958542Seric 	  case EISCONN:			/* Socket already connected */
127058542Seric #endif
127158542Seric #ifdef EINPROGRESS
127258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
127358542Seric #endif
127458542Seric #ifdef EALREADY
127558542Seric 	  case EALREADY:		/* Operation already in progress */
127658542Seric #endif
127758542Seric #ifdef EADDRINUSE
127858542Seric 	  case EADDRINUSE:		/* Address already in use */
127958542Seric #endif
128058542Seric #ifdef EADDRNOTAVAIL
128158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
128258542Seric #endif
128358542Seric #ifdef ENOSR
128458542Seric 	  case ENOSR:			/* Out of streams resources */
128558542Seric #endif
128658542Seric 		return TRUE;
128758542Seric 	}
128858542Seric 
128958542Seric 	/* nope, must be permanent */
129058542Seric 	return FALSE;
129158542Seric }
129258689Seric /*
129358689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
129458689Seric **
129558689Seric **	Parameters:
129658689Seric **		fd -- the file descriptor of the file.
129758689Seric **		filename -- the file name (for error messages).
129858689Seric **		type -- type of the lock.  Bits can be:
129958689Seric **			LOCK_EX -- exclusive lock.
130058689Seric **			LOCK_NB -- non-blocking.
130158689Seric **
130258689Seric **	Returns:
130358689Seric **		TRUE if the lock was acquired.
130458689Seric **		FALSE otherwise.
130558689Seric */
130658689Seric 
130758689Seric bool
130858689Seric lockfile(fd, filename, type)
130958689Seric 	int fd;
131058689Seric 	char *filename;
131158689Seric 	int type;
131258689Seric {
131358689Seric # ifdef LOCKF
131458689Seric 	int action;
131558689Seric 	struct flock lfd;
131658689Seric 
131759447Seric 	if (bitset(LOCK_UN, type))
131859447Seric 		lfd.l_type = F_UNLCK;
131959447Seric 	else if (bitset(LOCK_EX, type))
132058689Seric 		lfd.l_type = F_WRLCK;
132158689Seric 	else
132258689Seric 		lfd.l_type = F_RDLCK;
132358689Seric 
132458689Seric 	if (bitset(LOCK_NB, type))
132558689Seric 		action = F_SETLK;
132658689Seric 	else
132758689Seric 		action = F_SETLKW;
132858689Seric 
132958689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
133058689Seric 
133158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
133258689Seric 		return TRUE;
133358689Seric 
133458689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
133559447Seric 		syserr("cannot lockf(%s, %o)", filename, type);
133658689Seric # else
133758689Seric 	if (flock(fd, type) >= 0)
133858689Seric 		return TRUE;
133958689Seric 
134058689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
134159447Seric 		syserr("cannot flock(%s, %o)", filename, type);
134258689Seric # endif
134358689Seric 	return FALSE;
134458689Seric }
1345