xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 59253)
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*59253Seric static char sccsid[] = "@(#)conf.c	6.46 (Berkeley) 04/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,
889055Seric 	"via",			H_TRACE|H_FORCE,
899055Seric 	"mail-from",		H_TRACE|H_FORCE,
908060Seric 
9158796Seric 		/* miscellaneous fields */
9258796Seric 	"comments",		H_FORCE,
9358796Seric 	"return-path",		H_ACHECK,
9458796Seric 
959055Seric 	NULL,			0,
962897Seric };
974166Seric 
984166Seric 
994166Seric 
1004282Seric /*
1014282Seric **  Location of system files/databases/etc.
1024282Seric */
1034282Seric 
10440980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
10540980Sbostic char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
10658082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1079039Seric 
1089064Seric 
1099064Seric 
1109039Seric /*
11158082Seric **  Privacy values
11258082Seric */
11358082Seric 
11458082Seric struct prival PrivacyValues[] =
11558082Seric {
11658082Seric 	"public",		PRIV_PUBLIC,
11758082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11858114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11958082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
12058082Seric 	"noexpn",		PRIV_NOEXPN,
12158082Seric 	"novrfy",		PRIV_NOVRFY,
12258249Seric 	"restrictmailq",	PRIV_RESTRMAILQ,
12358789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12458082Seric 	"goaway",		PRIV_GOAWAY,
12558789Seric 	NULL,			0,
12658082Seric };
12758082Seric 
12858082Seric 
12958082Seric 
13058082Seric /*
13124943Seric **  Miscellaneous stuff.
1329039Seric */
1339039Seric 
13424943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13524943Seric /*
13624943Seric **  SETDEFAULTS -- set default values
13724943Seric **
13824943Seric **	Because of the way freezing is done, these must be initialized
13924943Seric **	using direct code.
14024943Seric **
14124943Seric **	Parameters:
14258734Seric **		e -- the default envelope.
14324943Seric **
14424943Seric **	Returns:
14524943Seric **		none.
14624943Seric **
14724943Seric **	Side Effects:
14824943Seric **		Initializes a bunch of global variables to their
14924943Seric **		default values.
15024943Seric */
15124943Seric 
15258737Seric #define DAYS		* 24 * 60 * 60
15358737Seric 
15458734Seric setdefaults(e)
15558734Seric 	register ENVELOPE *e;
15624943Seric {
15757438Seric 	SpaceSub = ' ';				/* option B */
15857438Seric 	QueueLA = 8;				/* option x */
15957438Seric 	RefuseLA = 12;				/* option X */
16057438Seric 	WkRecipFact = 30000L;			/* option y */
16157438Seric 	WkClassFact = 1800L;			/* option z */
16257438Seric 	WkTimeFact = 90000L;			/* option Z */
16357438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
16457142Seric 	FileMode = (getuid() != geteuid()) ? 0644 : 0600;
16557438Seric 						/* option F */
16657438Seric 	DefUid = 1;				/* option u */
16757438Seric 	DefGid = 1;				/* option g */
16857438Seric 	CheckpointInterval = 10;		/* option C */
16957438Seric 	MaxHopCount = 25;			/* option h */
17058734Seric 	e->e_sendmode = SM_FORK;		/* option d */
17158734Seric 	e->e_errormode = EM_PRINT;		/* option e */
17257438Seric 	EightBit = FALSE;			/* option 8 */
17357438Seric 	MaxMciCache = 1;			/* option k */
17457438Seric 	MciCacheTimeout = 300;			/* option K */
17557438Seric 	LogLevel = 9;				/* option L */
17658112Seric 	settimeouts(NULL);			/* option r */
17758737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
17858737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
17958853Seric 	PrivacyFlags = 0;			/* option p */
18040973Sbostic 	setdefuser();
18153654Seric 	setupmaps();
18257402Seric 	setupmailers();
18324943Seric }
184294Seric 
18540973Sbostic 
1864326Seric /*
18740973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
18840973Sbostic */
18940973Sbostic 
19040973Sbostic setdefuser()
19140973Sbostic {
19240973Sbostic 	struct passwd *defpwent;
19357386Seric 	static char defuserbuf[40];
19440973Sbostic 
19557386Seric 	DefUser = defuserbuf;
19640973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
19757386Seric 		strcpy(defuserbuf, defpwent->pw_name);
19840973Sbostic 	else
19957386Seric 		strcpy(defuserbuf, "nobody");
20040973Sbostic }
20153654Seric /*
20253654Seric **  SETUPMAPS -- set up map classes
20353654Seric **
20453654Seric **	Since these are compiled in, they cannot be in the config file.
20553654Seric **
20653654Seric */
20740973Sbostic 
20853654Seric setupmaps()
20953654Seric {
21053654Seric 	register STAB *s;
21140973Sbostic 
21258802Seric 	/* host name lookup map */
21358802Seric 	{
21458802Seric 		extern bool host_map_init();
21558802Seric 		extern char *maphostname();
21653654Seric 
21758802Seric 		s = stab("host", ST_MAPCLASS, ST_ENTER);
21858802Seric 		s->s_mapclass.map_init = host_map_init;
21958802Seric 		s->s_mapclass.map_lookup = maphostname;
22058802Seric 	}
22153654Seric 
22258802Seric 	/* dequote map */
22358802Seric 	{
22458802Seric 		extern bool dequote_init();
22558802Seric 		extern char *dequote_map();
22658802Seric 
22758802Seric 		s = stab("dequote", ST_MAPCLASS, ST_ENTER);
22858802Seric 		s->s_mapclass.map_init = dequote_init;
22958802Seric 		s->s_mapclass.map_lookup = dequote_map;
23058802Seric 	}
23158802Seric 
23253654Seric # ifdef DBM_MAP
23353654Seric 	/* dbm file access */
23453654Seric 	{
23556836Seric 		extern bool dbm_map_init();
23653654Seric 		extern char *dbm_map_lookup();
23753654Seric 
23853654Seric 		s = stab("dbm", ST_MAPCLASS, ST_ENTER);
23953654Seric 		s->s_mapclass.map_init = dbm_map_init;
24053654Seric 		s->s_mapclass.map_lookup = dbm_map_lookup;
24153654Seric 	}
24253654Seric # endif
24353654Seric 
24453654Seric # ifdef BTREE_MAP
24553654Seric 	/* new database file access -- btree files */
24653654Seric 	{
24756823Seric 		extern bool bt_map_init();
24856823Seric 		extern char *db_map_lookup();
24953654Seric 
25053654Seric 		s = stab("btree", ST_MAPCLASS, ST_ENTER);
25153654Seric 		s->s_mapclass.map_init = bt_map_init;
25256823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
25353654Seric 	}
25453654Seric # endif
25553654Seric 
25653654Seric # ifdef HASH_MAP
25753654Seric 	/* new database file access -- hash files */
25853654Seric 	{
25956823Seric 		extern bool hash_map_init();
26056823Seric 		extern char *db_map_lookup();
26153654Seric 
26253654Seric 		s = stab("hash", ST_MAPCLASS, ST_ENTER);
26353654Seric 		s->s_mapclass.map_init = hash_map_init;
26456823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
26553654Seric 	}
26653654Seric # endif
26753654Seric 
26857208Seric # ifdef NIS_MAP
26957208Seric 	/* NIS map access */
27057208Seric 	{
27157208Seric 		extern bool nis_map_init();
27257208Seric 		extern char *nis_map_lookup();
27357208Seric 
27457208Seric 		s = stab("nis", ST_MAPCLASS, ST_ENTER);
27557208Seric 		s->s_mapclass.map_init = nis_map_init;
27657208Seric 		s->s_mapclass.map_lookup = nis_map_lookup;
27757208Seric 	}
27857208Seric # endif
27957208Seric 
28053654Seric # ifdef USERDB_MAP
28153654Seric 	/* user database */
28253654Seric 	{
28356836Seric 		extern bool udb_map_init();
28453654Seric 		extern char *udb_map_lookup();
28553654Seric 
28653654Seric 		s = stab("udb", ST_MAPCLASS, ST_ENTER);
28753654Seric 		s->s_mapclass.map_init = udb_map_init;
28853654Seric 		s->s_mapclass.map_lookup = udb_map_lookup;
28953654Seric 	}
29053654Seric # endif
29153654Seric }
29253654Seric /*
29356836Seric **  HOST_MAP_INIT -- initialize host class structures
29456836Seric */
29556836Seric 
29656836Seric bool
29756836Seric host_map_init(map, mapname, args)
29856836Seric 	MAP *map;
29956836Seric 	char *mapname;
30056836Seric 	char *args;
30156836Seric {
30256836Seric 	register char *p = args;
30356836Seric 
30456836Seric 	for (;;)
30556836Seric 	{
30658050Seric 		while (isascii(*p) && isspace(*p))
30756836Seric 			p++;
30856836Seric 		if (*p != '-')
30956836Seric 			break;
31056836Seric 		switch (*++p)
31156836Seric 		{
31256836Seric 		  case 'a':
31356836Seric 			map->map_app = ++p;
31456836Seric 			break;
31556836Seric 		}
31658050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
31756836Seric 			p++;
31856836Seric 		if (*p != '\0')
31956836Seric 			*p++ = '\0';
32056836Seric 	}
32156836Seric 	if (map->map_app != NULL)
32256836Seric 		map->map_app = newstr(map->map_app);
32356836Seric 	return TRUE;
32456836Seric }
32557402Seric /*
32657402Seric **  SETUPMAILERS -- initialize default mailers
32757402Seric */
32856836Seric 
32957402Seric setupmailers()
33057402Seric {
33157402Seric 	char buf[100];
33257402Seric 
33357403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
33457403Seric 	makemailer(buf);
33557403Seric 
33657402Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE");
33757402Seric 	makemailer(buf);
33857402Seric 
33957402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
34057402Seric 	makemailer(buf);
34157402Seric }
34256836Seric /*
3434326Seric **  GETRUID -- get real user id (V7)
3444326Seric */
3454326Seric 
3464326Seric getruid()
3474326Seric {
3489274Seric 	if (OpMode == MD_DAEMON)
3494536Seric 		return (RealUid);
3504536Seric 	else
3514536Seric 		return (getuid());
3524326Seric }
3534326Seric 
3544326Seric 
3554326Seric /*
3564326Seric **  GETRGID -- get real group id (V7).
3574326Seric */
3584326Seric 
3594326Seric getrgid()
3604326Seric {
3619274Seric 	if (OpMode == MD_DAEMON)
3624536Seric 		return (RealGid);
3634536Seric 	else
3644536Seric 		return (getgid());
3654326Seric }
36653654Seric /*
3679369Seric **  USERNAME -- return the user id of the logged in user.
3689369Seric **
3699369Seric **	Parameters:
3709369Seric **		none.
3719369Seric **
3729369Seric **	Returns:
3739369Seric **		The login name of the logged in user.
3749369Seric **
3759369Seric **	Side Effects:
3769369Seric **		none.
3779369Seric **
3789369Seric **	Notes:
3799369Seric **		The return value is statically allocated.
3809369Seric */
3819369Seric 
3829369Seric char *
3839369Seric username()
3849369Seric {
38517469Seric 	static char *myname = NULL;
3869369Seric 	extern char *getlogin();
38719904Smiriam 	register struct passwd *pw;
3889369Seric 
38917469Seric 	/* cache the result */
39017469Seric 	if (myname == NULL)
39117469Seric 	{
39217469Seric 		myname = getlogin();
39317469Seric 		if (myname == NULL || myname[0] == '\0')
39417469Seric 		{
39517469Seric 			pw = getpwuid(getruid());
39617469Seric 			if (pw != NULL)
39740993Sbostic 				myname = newstr(pw->pw_name);
39817469Seric 		}
39919904Smiriam 		else
40019904Smiriam 		{
40158736Seric 			uid_t uid = getuid();
40219873Smiriam 
40340993Sbostic 			myname = newstr(myname);
40440993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
40558736Seric 			      (uid != 0 && uid != pw->pw_uid))
40619904Smiriam 			{
40758736Seric 				pw = getpwuid(uid);
40824945Seric 				if (pw != NULL)
40940993Sbostic 					myname = newstr(pw->pw_name);
41019873Smiriam 			}
41119873Smiriam 		}
41217469Seric 		if (myname == NULL || myname[0] == '\0')
41317469Seric 		{
41458151Seric 			syserr("554 Who are you?");
41517469Seric 			myname = "postmaster";
41617469Seric 		}
41717469Seric 	}
41817469Seric 
41917469Seric 	return (myname);
4209369Seric }
4219369Seric /*
4224190Seric **  TTYPATH -- Get the path of the user's tty
423294Seric **
424294Seric **	Returns the pathname of the user's tty.  Returns NULL if
425294Seric **	the user is not logged in or if s/he has write permission
426294Seric **	denied.
427294Seric **
428294Seric **	Parameters:
429294Seric **		none
430294Seric **
431294Seric **	Returns:
432294Seric **		pathname of the user's tty.
433294Seric **		NULL if not logged in or write permission denied.
434294Seric **
435294Seric **	Side Effects:
436294Seric **		none.
437294Seric **
438294Seric **	WARNING:
439294Seric **		Return value is in a local buffer.
440294Seric **
441294Seric **	Called By:
442294Seric **		savemail
443294Seric */
444294Seric 
445294Seric # include <sys/stat.h>
446294Seric 
447294Seric char *
448294Seric ttypath()
449294Seric {
450294Seric 	struct stat stbuf;
451294Seric 	register char *pathn;
452294Seric 	extern char *ttyname();
4534081Seric 	extern char *getlogin();
454294Seric 
455294Seric 	/* compute the pathname of the controlling tty */
4569369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4579369Seric 	    (pathn = ttyname(0)) == NULL)
458294Seric 	{
459294Seric 		errno = 0;
460294Seric 		return (NULL);
461294Seric 	}
462294Seric 
463294Seric 	/* see if we have write permission */
4642967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
465294Seric 	{
466294Seric 		errno = 0;
467294Seric 		return (NULL);
468294Seric 	}
469294Seric 
470294Seric 	/* see if the user is logged in */
471294Seric 	if (getlogin() == NULL)
472294Seric 		return (NULL);
473294Seric 
474294Seric 	/* looks good */
475294Seric 	return (pathn);
476294Seric }
4772967Seric /*
4782967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4792967Seric **
4802967Seric **	This routine can be supplied on a per-installation basis
4812967Seric **	to determine whether a person is allowed to send a message.
4822967Seric **	This allows restriction of certain types of internet
4832967Seric **	forwarding or registration of users.
4842967Seric **
4852967Seric **	If the hosts are found to be incompatible, an error
48657454Seric **	message should be given using "usrerr" and 0 should
4872967Seric **	be returned.
4882967Seric **
4894288Seric **	'NoReturn' can be set to suppress the return-to-sender
4904288Seric **	function; this should be done on huge messages.
4914288Seric **
4922967Seric **	Parameters:
4932967Seric **		to -- the person being sent to.
4942967Seric **
4952967Seric **	Returns:
49657459Seric **		an exit status
4972967Seric **
4982967Seric **	Side Effects:
4992967Seric **		none (unless you include the usrerr stuff)
5002967Seric */
5012967Seric 
50255012Seric checkcompat(to, e)
5032967Seric 	register ADDRESS *to;
50455012Seric 	register ENVELOPE *e;
5052967Seric {
50612133Seric # ifdef lint
50712133Seric 	if (to == NULL)
50812133Seric 		to++;
50912133Seric # endif lint
51010698Seric # ifdef EXAMPLE_CODE
51110698Seric 	/* this code is intended as an example only */
5124437Seric 	register STAB *s;
5134437Seric 
5144437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
51555012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
5169369Seric 	    to->q_mailer == s->s_mailer)
5174437Seric 	{
51858151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
51910698Seric 		/* NoReturn = TRUE; to supress return copy */
52057459Seric 		return (EX_UNAVAILABLE);
5214437Seric 	}
52256795Seric # endif /* EXAMPLE_CODE */
52357459Seric 	return (EX_OK);
5242967Seric }
5259369Seric /*
5269369Seric **  HOLDSIGS -- arrange to hold all signals
5279369Seric **
5289369Seric **	Parameters:
5299369Seric **		none.
5309369Seric **
5319369Seric **	Returns:
5329369Seric **		none.
5339369Seric **
5349369Seric **	Side Effects:
5359369Seric **		Arranges that signals are held.
5369369Seric */
5379369Seric 
5389369Seric holdsigs()
5399369Seric {
5409369Seric }
5419369Seric /*
5429369Seric **  RLSESIGS -- arrange to release all signals
5439369Seric **
5449369Seric **	This undoes the effect of holdsigs.
5459369Seric **
5469369Seric **	Parameters:
5479369Seric **		none.
5489369Seric **
5499369Seric **	Returns:
5509369Seric **		none.
5519369Seric **
5529369Seric **	Side Effects:
5539369Seric **		Arranges that signals are released.
5549369Seric */
5559369Seric 
5569369Seric rlsesigs()
5579369Seric {
5589369Seric }
55914872Seric /*
56014872Seric **  GETLA -- get the current load average
56114872Seric **
56214881Seric **	This code stolen from la.c.
56314881Seric **
56414872Seric **	Parameters:
56514872Seric **		none.
56614872Seric **
56714872Seric **	Returns:
56814872Seric **		The current load average as an integer.
56914872Seric **
57014872Seric **	Side Effects:
57114872Seric **		none.
57214872Seric */
57314872Seric 
57451920Seric /* try to guess what style of load average we have */
57551920Seric #define LA_ZERO		1	/* always return load average as zero */
57651920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
57751920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
57851920Seric #define LA_SUBR		4	/* call getloadavg */
57914872Seric 
58051920Seric #ifndef LA_TYPE
58151920Seric #  if defined(sun)
58251920Seric #    define LA_TYPE		LA_INT
58351920Seric #  endif
58457977Seric #  if defined(mips) || defined(__alpha)
58557977Seric      /* Ultrix or OSF/1 or RISC/os */
58651920Seric #    define LA_TYPE		LA_INT
58751920Seric #    define LA_AVENRUN		"avenrun"
58851920Seric #  endif
58951920Seric #  if defined(hpux)
59051920Seric #    define LA_TYPE		LA_FLOAT
591*59253Seric #    define LA_AVENRUN		"avenrun"
59251920Seric #  endif
59351920Seric 
59451920Seric #  ifndef LA_TYPE
59557736Seric #   if defined(SYSTEM5)
59657736Seric #    define LA_TYPE		LA_INT
59757736Seric #    define LA_AVENRUN		"avenrun"
59857736Seric #   else
59957736Seric #    if defined(BSD)
60057736Seric #     define LA_TYPE		LA_SUBR
60157736Seric #    else
60257736Seric #     define LA_TYPE		LA_ZERO
60357736Seric #    endif
60457736Seric #   endif
60551920Seric #  endif
60651920Seric #endif
60751920Seric 
60851920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
60951920Seric 
61014872Seric #include <nlist.h>
61114872Seric 
61251920Seric #ifndef LA_AVENRUN
61351920Seric #define LA_AVENRUN	"_avenrun"
61451920Seric #endif
61551920Seric 
61651920Seric /* _PATH_UNIX should be defined in <paths.h> */
61751920Seric #ifndef _PATH_UNIX
61851920Seric #  if defined(hpux)
61951920Seric #    define _PATH_UNIX		"/hp-ux"
62051920Seric #  endif
62151920Seric #  if defined(mips) && !defined(ultrix)
62251920Seric      /* powerful RISC/os */
62351920Seric #    define _PATH_UNIX		"/unix"
62451920Seric #  endif
62557736Seric #  if defined(SYSTEM5)
62657977Seric #    ifndef _PATH_UNIX
62757977Seric #      define _PATH_UNIX	"/unix"
62857977Seric #    endif
62957736Seric #  endif
63051920Seric #  ifndef _PATH_UNIX
63151920Seric #    define _PATH_UNIX		"/vmunix"
63251920Seric #  endif
63351920Seric #endif
63451920Seric 
63514872Seric struct	nlist Nl[] =
63614872Seric {
63751920Seric 	{ LA_AVENRUN },
63814872Seric #define	X_AVENRUN	0
63914872Seric 	{ 0 },
64014872Seric };
64114872Seric 
64257736Seric #if defined(unixpc)
64357736Seric # define FSHIFT		5
64457736Seric #endif
64557736Seric 
64657977Seric #if defined(__alpha)
64757977Seric # define FSHIFT		10
64857977Seric #endif
64957977Seric 
65051920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
65151920Seric #  define FSHIFT	8
65257736Seric #endif
65357736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
65451920Seric #  define FSCALE	(1 << FSHIFT)
65551920Seric #endif
65640930Srick 
65714872Seric getla()
65814872Seric {
65914872Seric 	static int kmem = -1;
66051920Seric #if LA_TYPE == LA_INT
66124943Seric 	long avenrun[3];
66251920Seric #else
66351920Seric 	double avenrun[3];
66451920Seric #endif
66525615Seric 	extern off_t lseek();
66657736Seric 	extern char *errstring();
66757736Seric 	extern int errno;
66814872Seric 
66914872Seric 	if (kmem < 0)
67014872Seric 	{
67124945Seric 		kmem = open("/dev/kmem", 0, 0);
67214872Seric 		if (kmem < 0)
67357736Seric 		{
67457736Seric 			if (tTd(3, 1))
67557736Seric 				printf("getla: open(/dev/kmem): %s\n",
67657736Seric 					errstring(errno));
67714872Seric 			return (-1);
67857736Seric 		}
67951920Seric 		(void) fcntl(kmem, F_SETFD, 1);
68057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68157736Seric 		{
68257736Seric 			if (tTd(3, 1))
68357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68457736Seric 					errstring(errno));
68514872Seric 			return (-1);
68657736Seric 		}
687*59253Seric 		if (Nl[X_AVENRUN].n_value == 0)
688*59253Seric 		{
689*59253Seric 			if (tTd(3, 1))
690*59253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
691*59253Seric 					_PATH_UNIX, LA_AVENRUN);
692*59253Seric 			return (-1);
693*59253Seric 		}
69414872Seric 	}
69557736Seric 	if (tTd(3, 20))
69657736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
69724945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
69823118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
69919967Seric 	{
70019967Seric 		/* thank you Ian */
70157736Seric 		if (tTd(3, 1))
70257736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
70319967Seric 		return (-1);
70419967Seric 	}
70551920Seric #if LA_TYPE == LA_INT
70657736Seric 	if (tTd(3, 5))
70757736Seric 	{
70857736Seric 		printf("getla: avenrun = %d", avenrun[0]);
70957736Seric 		if (tTd(3, 15))
71057736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71157736Seric 		printf("\n");
71257736Seric 	}
71357736Seric 	if (tTd(3, 1))
71457736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71524943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71651920Seric #else
71757736Seric 	if (tTd(3, 5))
71857736Seric 	{
71957736Seric 		printf("getla: avenrun = %g", avenrun[0]);
72057736Seric 		if (tTd(3, 15))
72157736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
72257736Seric 		printf("\n");
72357736Seric 	}
72457736Seric 	if (tTd(3, 1))
72557736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72651920Seric 	return ((int) (avenrun[0] + 0.5));
72751920Seric #endif
72814872Seric }
72914872Seric 
73051773Seric #else
73151920Seric #if LA_TYPE == LA_SUBR
73251773Seric 
73351773Seric getla()
73451773Seric {
73551920Seric 	double avenrun[3];
73651920Seric 
73751920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
73857736Seric 	{
73957736Seric 		if (tTd(3, 1))
74057736Seric 			perror("getla: getloadavg failed:");
74151920Seric 		return (-1);
74257736Seric 	}
74357736Seric 	if (tTd(3, 1))
74457736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
74551920Seric 	return ((int) (avenrun[0] + 0.5));
74651773Seric }
74751773Seric 
74851773Seric #else
74951773Seric 
75051773Seric getla()
75151773Seric {
75257736Seric 	if (tTd(3, 1))
75357736Seric 		printf("getla: ZERO\n");
75451920Seric 	return (0);
75551773Seric }
75651773Seric 
75751773Seric #endif
75851773Seric #endif
75924943Seric /*
76024943Seric **  SHOULDQUEUE -- should this message be queued or sent?
76124943Seric **
76224943Seric **	Compares the message cost to the load average to decide.
76324943Seric **
76424943Seric **	Parameters:
76524943Seric **		pri -- the priority of the message in question.
76657438Seric **		ctime -- the message creation time.
76724943Seric **
76824943Seric **	Returns:
76924943Seric **		TRUE -- if this message should be queued up for the
77024943Seric **			time being.
77124943Seric **		FALSE -- if the load is low enough to send this message.
77224943Seric **
77324943Seric **	Side Effects:
77424943Seric **		none.
77524943Seric */
77624943Seric 
77724943Seric bool
77857438Seric shouldqueue(pri, ctime)
77924943Seric 	long pri;
78057438Seric 	time_t ctime;
78124943Seric {
78251920Seric 	if (CurrentLA < QueueLA)
78324943Seric 		return (FALSE);
78458132Seric 	if (CurrentLA >= RefuseLA)
78558132Seric 		return (TRUE);
78651920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
78724943Seric }
78824943Seric /*
78953037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
79053037Seric **
79153037Seric **	Parameters:
79253037Seric **		none.
79353037Seric **
79453037Seric **	Returns:
79553037Seric **		TRUE if incoming SMTP connections should be refused
79653037Seric **			(for now).
79753037Seric **		FALSE if we should accept new work.
79853037Seric **
79953037Seric **	Side Effects:
80053037Seric **		none.
80153037Seric */
80253037Seric 
80353037Seric bool
80453037Seric refuseconnections()
80553037Seric {
80659156Seric #ifdef XLA
80759156Seric 	if (!xla_smtp_ok())
80859156Seric 		return TRUE;
80959156Seric #endif
81059156Seric 
81153037Seric 	/* this is probably too simplistic */
81258132Seric 	return (CurrentLA >= RefuseLA);
81353037Seric }
81453037Seric /*
81524943Seric **  SETPROCTITLE -- set process title for ps
81624943Seric **
81724943Seric **	Parameters:
81858674Seric **		fmt -- a printf style format string.
81958674Seric **		a, b, c -- possible parameters to fmt.
82024943Seric **
82124943Seric **	Returns:
82224943Seric **		none.
82324943Seric **
82424943Seric **	Side Effects:
82524943Seric **		Clobbers argv of our main procedure so ps(1) will
82624943Seric **		display the title.
82724943Seric */
82824943Seric 
82958689Seric #ifdef SETPROCTITLE
83058689Seric # ifdef __hpux
83158689Seric #  include <sys/pstat.h>
83258689Seric # endif
83358689Seric #endif
83458689Seric 
83524943Seric /*VARARGS1*/
83657642Seric #ifdef __STDC__
83757642Seric setproctitle(char *fmt, ...)
83857642Seric #else
83957642Seric setproctitle(fmt, va_alist)
84024943Seric 	char *fmt;
84157642Seric 	va_dcl
84257642Seric #endif
84324943Seric {
84424943Seric # ifdef SETPROCTITLE
84524943Seric 	register char *p;
84625049Seric 	register int i;
84758674Seric 	char buf[MAXLINE];
84856852Seric 	VA_LOCAL_DECL
84958689Seric #  ifdef __hpux
85058689Seric 	union pstun pst;
85158689Seric #  endif
85224943Seric 	extern char **Argv;
85324943Seric 	extern char *LastArgv;
85424943Seric 
85558674Seric 	p = buf;
85624943Seric 
85758674Seric 	/* print sendmail: heading for grep */
85858674Seric 	(void) strcpy(p, "sendmail: ");
85958674Seric 	p += strlen(p);
86024943Seric 
86158674Seric 	/* print the argument string */
86258674Seric 	VA_START(fmt);
86358674Seric 	(void) vsprintf(p, fmt, ap);
86456852Seric 	VA_END;
86554996Seric 
86658674Seric 	i = strlen(buf);
86758689Seric 
86858689Seric #  ifdef __hpux
86958689Seric 	pst.pst_command = buf;
87058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87158689Seric #  else
87258689Seric 
87354996Seric 	if (i > LastArgv - Argv[0] - 2)
87425049Seric 	{
87554996Seric 		i = LastArgv - Argv[0] - 2;
87658674Seric 		buf[i] = '\0';
87725049Seric 	}
87858674Seric 	(void) strcpy(Argv[0], buf);
87954997Seric 	p = &Argv[0][i];
88024943Seric 	while (p < LastArgv)
88124943Seric 		*p++ = ' ';
88258689Seric #  endif
88356795Seric # endif /* SETPROCTITLE */
88424943Seric }
88525698Seric /*
88625698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88725698Seric **
88825698Seric **	Parameters:
88925698Seric **		none.
89025698Seric **
89125698Seric **	Returns:
89225698Seric **		none.
89325698Seric **
89425698Seric **	Side Effects:
89525698Seric **		Picks up extant zombies.
89625698Seric */
89725698Seric 
89825698Seric # include <sys/wait.h>
89925698Seric 
90046928Sbostic void
90125698Seric reapchild()
90225698Seric {
90325698Seric # ifdef WNOHANG
90425698Seric 	union wait status;
90525698Seric 
90646928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
90725698Seric 		continue;
90856795Seric # else /* WNOHANG */
90925698Seric 	auto int status;
91025698Seric 
91146928Sbostic 	while (wait((int *)&status) > 0)
91225698Seric 		continue;
91356795Seric # endif /* WNOHANG */
91458061Seric # ifdef SYSTEM5
91558061Seric 	(void) signal(SIGCHLD, reapchild);
91658061Seric # endif
91725698Seric }
91855418Seric /*
91955418Seric **  UNSETENV -- remove a variable from the environment
92055418Seric **
92155418Seric **	Not needed on newer systems.
92255418Seric **
92355418Seric **	Parameters:
92455418Seric **		name -- the string name of the environment variable to be
92555418Seric **			deleted from the current environment.
92655418Seric **
92755418Seric **	Returns:
92855418Seric **		none.
92955418Seric **
93055418Seric **	Globals:
93155418Seric **		environ -- a pointer to the current environment.
93255418Seric **
93355418Seric **	Side Effects:
93455418Seric **		Modifies environ.
93555418Seric */
93655418Seric 
93755418Seric #ifdef UNSETENV
93855418Seric 
93955418Seric void
94055418Seric unsetenv(name)
94155418Seric 	char *name;
94255418Seric {
94355418Seric 	extern char **environ;
94455418Seric 	register char **pp;
94555418Seric 	int len = strlen(name);
94655418Seric 
94755418Seric 	for (pp = environ; *pp != NULL; pp++)
94855418Seric 	{
94955418Seric 		if (strncmp(name, *pp, len) == 0 &&
95055418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
95155418Seric 			break;
95255418Seric 	}
95355418Seric 
95455418Seric 	for (; *pp != NULL; pp++)
95555418Seric 		*pp = pp[1];
95655418Seric }
95755418Seric 
95855418Seric #endif /* UNSETENV */
95956215Seric /*
96056215Seric **  GETDTABLESIZE -- return number of file descriptors
96156215Seric **
96256215Seric **	Only on non-BSD systems
96356215Seric **
96456215Seric **	Parameters:
96556215Seric **		none
96656215Seric **
96756215Seric **	Returns:
96856215Seric **		size of file descriptor table
96956215Seric **
97056215Seric **	Side Effects:
97156215Seric **		none
97256215Seric */
97356215Seric 
97456215Seric #ifdef SYSTEM5
97556215Seric 
97656215Seric int
97756215Seric getdtablesize()
97856215Seric {
97958689Seric # ifdef _SC_OPEN_MAX
98058689Seric 	return sysconf(_SC_OPEN_MAX);
98158689Seric # else
98256215Seric 	return NOFILE;
98358689Seric # endif
98456215Seric }
98556215Seric 
98656215Seric #endif
98757631Seric /*
98857631Seric **  UNAME -- get the UUCP name of this system.
98957631Seric */
99057631Seric 
99157943Seric #ifndef HASUNAME
99257631Seric 
99357631Seric int
99457631Seric uname(name)
99557631Seric 	struct utsname *name;
99657631Seric {
99757631Seric 	FILE *file;
99857631Seric 	char *n;
99957631Seric 
100057631Seric 	name->nodename[0] = '\0';
100157631Seric 
100257661Seric 	/* try /etc/whoami -- one line with the node name */
100357631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
100457631Seric 	{
100557661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
100657631Seric 		(void) fclose(file);
100757661Seric 		n = strchr(name->nodename, '\n');
100857631Seric 		if (n != NULL)
100957631Seric 			*n = '\0';
101057631Seric 		if (name->nodename[0] != '\0')
101157631Seric 			return (0);
101257631Seric 	}
101357631Seric 
101457661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
101557631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
101657631Seric 	{
101757631Seric 		char buf[MAXLINE];
101857631Seric 
101957631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
102057631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
102157631Seric 					NODE_LENGTH, name->nodename) > 0)
102257631Seric 				break;
102357631Seric 		(void) fclose(file);
102457631Seric 		if (name->nodename[0] != '\0')
102557631Seric 			return (0);
102657631Seric 	}
102757631Seric 
102857631Seric #ifdef TRUST_POPEN
102957631Seric 	/*
103057631Seric 	**  Popen is known to have security holes.
103157631Seric 	*/
103257631Seric 
103357661Seric 	/* try uuname -l to return local name */
103457631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
103557631Seric 	{
103657661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
103757631Seric 		(void) pclose(file);
103857661Seric 		n = strchr(name, '\n');
103957631Seric 		if (n != NULL)
104057631Seric 			*n = '\0';
104157661Seric 		if (name->nodename[0] != '\0')
104257631Seric 			return (0);
104357631Seric 	}
104457631Seric #endif
104557631Seric 
104657631Seric 	return (-1);
104757631Seric }
104857943Seric #endif /* HASUNAME */
104958068Seric /*
105058068Seric **  INITGROUPS -- initialize groups
105158068Seric **
105258068Seric **	Stub implementation for System V style systems
105358068Seric */
105458068Seric 
105558068Seric #ifndef HASINITGROUPS
105658068Seric # if !defined(SYSTEM5) || defined(hpux)
105758068Seric #  define HASINITGROUPS
105858068Seric # endif
105958068Seric #endif
106058068Seric 
106158068Seric #ifndef HASINITGROUPS
106258068Seric 
106358068Seric initgroups(name, basegid)
106458068Seric 	char *name;
106558068Seric 	int basegid;
106658068Seric {
106758068Seric 	return 0;
106858068Seric }
106958068Seric 
107058068Seric #endif
107158082Seric /*
107258082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
107358082Seric **
107458082Seric **	Only implemented if you have statfs.
107558082Seric **
107658082Seric **	Parameters:
107758333Seric **		msize -- the size to check against.  If zero, we don't yet
107858333Seric **			know how big the message will be, so just check for
107958333Seric **			a "reasonable" amount.
108058082Seric **
108158082Seric **	Returns:
108258082Seric **		TRUE if there is enough space.
108358082Seric **		FALSE otherwise.
108458082Seric */
108558082Seric 
108658082Seric #ifndef HASSTATFS
108758082Seric # if defined(BSD4_4) || defined(__osf__)
108858082Seric #  define HASSTATFS
108958082Seric # endif
109058082Seric #endif
109158082Seric 
109258082Seric #ifdef HASSTATFS
109358157Seric # undef HASUSTAT
109458157Seric #endif
109558157Seric 
109658157Seric #if defined(HASUSTAT)
109758157Seric # include <sys/stat.h>
109858157Seric # include <ustat.h>
109958157Seric #endif
110058157Seric 
110158157Seric #ifdef HASSTATFS
110258133Seric # if defined(sgi) || defined(apollo)
110358133Seric #  include <sys/statfs.h>
110458133Seric # else
110558133Seric #  if defined(sun) || defined(hpux)
110658133Seric #   include <sys/vfs.h>
110758133Seric #  else
110858157Seric #   include <sys/mount.h>
110958133Seric #  endif
111058133Seric # endif
111158082Seric #endif
111258082Seric 
111358082Seric bool
111458333Seric enoughspace(msize)
111558333Seric 	long msize;
111658082Seric {
111758160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
111858157Seric # if defined(HASUSTAT)
111958153Seric 	struct ustat fs;
112058153Seric 	struct stat statbuf;
112158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
112258157Seric #  define f_bavail	f_tfree
112358157Seric # else
112458157Seric #  if defined(ultrix)
112558157Seric 	struct fs_data fs;
112658157Seric #   define f_bavail	fd_bfreen
112758366Seric #   define FSBLOCKSIZE	fs.fd_bsize
112858153Seric #  else
112958082Seric 	struct statfs fs;
113058366Seric #   define FSBLOCKSIZE	fs.f_bsize
113158153Seric #  endif
113258133Seric # endif
113358333Seric 	long blocksneeded;
113458082Seric 	extern int errno;
113558082Seric 	extern char *errstring();
113658082Seric 
113758333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
113858082Seric 	{
113958082Seric 		if (tTd(4, 80))
114058133Seric 			printf("enoughspace: no threshold\n");
114158133Seric 		return TRUE;
114258133Seric 	}
114358333Seric 
114458157Seric # if defined(HASUSTAT)
114558157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
114658157Seric # else
114758157Seric #  if defined(sgi) || defined(apollo)
114858133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
114958157Seric #  else
115058157Seric #   if defined(ultrix)
115158133Seric 	if (statfs(QueueDir, &fs) > 0)
115258153Seric #   else
115358133Seric 	if (statfs(QueueDir, &fs) == 0)
115458153Seric #   endif
115558133Seric #  endif
115658133Seric # endif
115758133Seric 	{
115858133Seric 		if (tTd(4, 80))
115958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
116058333Seric 				fs.f_bavail, msize);
116158333Seric 
116258333Seric 		/* convert msize to block count */
116358366Seric 		msize = msize / FSBLOCKSIZE + 1;
116458333Seric 		if (MinBlocksFree >= 0)
116558333Seric 			msize += MinBlocksFree;
116658333Seric 
116758333Seric 		if (fs.f_bavail < msize)
116858090Seric 		{
116958090Seric #ifdef LOG
117058090Seric 			if (LogLevel > 0)
117158090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
117258333Seric 					QueueDir, fs.f_bavail, msize);
117358090Seric #endif
117458082Seric 			return FALSE;
117558090Seric 		}
117658082Seric 	}
117758082Seric 	else if (tTd(4, 80))
117858333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
117958333Seric 			MinBlocksFree, msize, errstring(errno));
118058082Seric #endif
118158082Seric 	return TRUE;
118258082Seric }
118358542Seric /*
118458542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
118558542Seric **
118658542Seric **	This looks at an errno value and tells if this is likely to
118758542Seric **	go away if retried later.
118858542Seric **
118958542Seric **	Parameters:
119058542Seric **		err -- the errno code to classify.
119158542Seric **
119258542Seric **	Returns:
119358542Seric **		TRUE if this is probably transient.
119458542Seric **		FALSE otherwise.
119558542Seric */
119658542Seric 
119758542Seric bool
119858542Seric transienterror(err)
119958542Seric 	int err;
120058542Seric {
120158542Seric 	switch (err)
120258542Seric 	{
120358542Seric 	  case EIO:			/* I/O error */
120458542Seric 	  case ENXIO:			/* Device not configured */
120558542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
120658542Seric 	  case ENOMEM:			/* Cannot allocate memory */
120758542Seric 	  case ENODEV:			/* Operation not supported by device */
120858542Seric 	  case ENFILE:			/* Too many open files in system */
120958542Seric 	  case EMFILE:			/* Too many open files */
121058542Seric 	  case ENOSPC:			/* No space left on device */
121158542Seric #ifdef ETIMEDOUT
121258542Seric 	  case ETIMEDOUT:		/* Connection timed out */
121358542Seric #endif
121458542Seric #ifdef ESTALE
121558542Seric 	  case ESTALE:			/* Stale NFS file handle */
121658542Seric #endif
121758542Seric #ifdef ENETDOWN
121858542Seric 	  case ENETDOWN:		/* Network is down */
121958542Seric #endif
122058542Seric #ifdef ENETUNREACH
122158542Seric 	  case ENETUNREACH:		/* Network is unreachable */
122258542Seric #endif
122358542Seric #ifdef ENETRESET
122458542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
122558542Seric #endif
122658542Seric #ifdef ECONNABORTED
122758542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
122858542Seric #endif
122958542Seric #ifdef ECONNRESET
123058542Seric 	  case ECONNRESET:		/* Connection reset by peer */
123158542Seric #endif
123258542Seric #ifdef ENOBUFS
123358542Seric 	  case ENOBUFS:			/* No buffer space available */
123458542Seric #endif
123558542Seric #ifdef ESHUTDOWN
123658542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
123758542Seric #endif
123858542Seric #ifdef ECONNREFUSED
123958542Seric 	  case ECONNREFUSED:		/* Connection refused */
124058542Seric #endif
124158542Seric #ifdef EHOSTDOWN
124258542Seric 	  case EHOSTDOWN:		/* Host is down */
124358542Seric #endif
124458542Seric #ifdef EHOSTUNREACH
124558542Seric 	  case EHOSTUNREACH:		/* No route to host */
124658542Seric #endif
124758542Seric #ifdef EDQUOT
124858542Seric 	  case EDQUOT:			/* Disc quota exceeded */
124958542Seric #endif
125058542Seric #ifdef EPROCLIM
125158542Seric 	  case EPROCLIM:		/* Too many processes */
125258542Seric #endif
125358542Seric #ifdef EUSERS
125458542Seric 	  case EUSERS:			/* Too many users */
125558542Seric #endif
125658542Seric #ifdef EDEADLK
125758542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
125858542Seric #endif
125958542Seric #ifdef EISCONN
126058542Seric 	  case EISCONN:			/* Socket already connected */
126158542Seric #endif
126258542Seric #ifdef EINPROGRESS
126358542Seric 	  case EINPROGRESS:		/* Operation now in progress */
126458542Seric #endif
126558542Seric #ifdef EALREADY
126658542Seric 	  case EALREADY:		/* Operation already in progress */
126758542Seric #endif
126858542Seric #ifdef EADDRINUSE
126958542Seric 	  case EADDRINUSE:		/* Address already in use */
127058542Seric #endif
127158542Seric #ifdef EADDRNOTAVAIL
127258542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
127358542Seric #endif
127458542Seric #ifdef ENOSR
127558542Seric 	  case ENOSR:			/* Out of streams resources */
127658542Seric #endif
127758542Seric 		return TRUE;
127858542Seric 	}
127958542Seric 
128058542Seric 	/* nope, must be permanent */
128158542Seric 	return FALSE;
128258542Seric }
128358689Seric /*
128458689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
128558689Seric **
128658689Seric **	Parameters:
128758689Seric **		fd -- the file descriptor of the file.
128858689Seric **		filename -- the file name (for error messages).
128958689Seric **		type -- type of the lock.  Bits can be:
129058689Seric **			LOCK_EX -- exclusive lock.
129158689Seric **			LOCK_NB -- non-blocking.
129258689Seric **
129358689Seric **	Returns:
129458689Seric **		TRUE if the lock was acquired.
129558689Seric **		FALSE otherwise.
129658689Seric */
129758689Seric 
129858689Seric bool
129958689Seric lockfile(fd, filename, type)
130058689Seric 	int fd;
130158689Seric 	char *filename;
130258689Seric 	int type;
130358689Seric {
130458689Seric # ifdef LOCKF
130558689Seric 	int action;
130658689Seric 	struct flock lfd;
130758689Seric 
130858701Seric 	if (bitset(LOCK_EX, type))
130958689Seric 		lfd.l_type = F_WRLCK;
131058689Seric 	else
131158689Seric 		lfd.l_type = F_RDLCK;
131258689Seric 
131358689Seric 	if (bitset(LOCK_NB, type))
131458689Seric 		action = F_SETLK;
131558689Seric 	else
131658689Seric 		action = F_SETLKW;
131758689Seric 
131858689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
131958689Seric 
132058689Seric 	if (fcntl(fd, action, &lfd) >= 0)
132158689Seric 		return TRUE;
132258689Seric 
132358689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
132458689Seric 		syserr("cannot lockf(%s)", filename);
132558689Seric # else
132658689Seric 	if (flock(fd, type) >= 0)
132758689Seric 		return TRUE;
132858689Seric 
132958689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
133058689Seric 		syserr("cannot flock(%s)", filename);
133158689Seric # endif
133258689Seric 	return FALSE;
133358689Seric }
1334