xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 59430)
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*59430Seric static char sccsid[] = "@(#)conf.c	6.49 (Berkeley) 04/27/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 */
17357438Seric 	EightBit = FALSE;			/* option 8 */
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 /*
20353654Seric **  SETUPMAPS -- set up map classes
20453654Seric **
20553654Seric **	Since these are compiled in, they cannot be in the config file.
20653654Seric **
20753654Seric */
20840973Sbostic 
20953654Seric setupmaps()
21053654Seric {
21153654Seric 	register STAB *s;
21240973Sbostic 
21358802Seric 	/* host name lookup map */
21458802Seric 	{
21558802Seric 		extern bool host_map_init();
21658802Seric 		extern char *maphostname();
21753654Seric 
21858802Seric 		s = stab("host", ST_MAPCLASS, ST_ENTER);
21958802Seric 		s->s_mapclass.map_init = host_map_init;
22058802Seric 		s->s_mapclass.map_lookup = maphostname;
22158802Seric 	}
22253654Seric 
22358802Seric 	/* dequote map */
22458802Seric 	{
22558802Seric 		extern bool dequote_init();
22658802Seric 		extern char *dequote_map();
22758802Seric 
22858802Seric 		s = stab("dequote", ST_MAPCLASS, ST_ENTER);
22958802Seric 		s->s_mapclass.map_init = dequote_init;
23058802Seric 		s->s_mapclass.map_lookup = dequote_map;
23158802Seric 	}
23258802Seric 
23353654Seric # ifdef DBM_MAP
23453654Seric 	/* dbm file access */
23553654Seric 	{
23656836Seric 		extern bool dbm_map_init();
23753654Seric 		extern char *dbm_map_lookup();
23853654Seric 
23953654Seric 		s = stab("dbm", ST_MAPCLASS, ST_ENTER);
24053654Seric 		s->s_mapclass.map_init = dbm_map_init;
24153654Seric 		s->s_mapclass.map_lookup = dbm_map_lookup;
24253654Seric 	}
24353654Seric # endif
24453654Seric 
24553654Seric # ifdef BTREE_MAP
24653654Seric 	/* new database file access -- btree files */
24753654Seric 	{
24856823Seric 		extern bool bt_map_init();
24956823Seric 		extern char *db_map_lookup();
25053654Seric 
25153654Seric 		s = stab("btree", ST_MAPCLASS, ST_ENTER);
25253654Seric 		s->s_mapclass.map_init = bt_map_init;
25356823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
25453654Seric 	}
25553654Seric # endif
25653654Seric 
25753654Seric # ifdef HASH_MAP
25853654Seric 	/* new database file access -- hash files */
25953654Seric 	{
26056823Seric 		extern bool hash_map_init();
26156823Seric 		extern char *db_map_lookup();
26253654Seric 
26353654Seric 		s = stab("hash", ST_MAPCLASS, ST_ENTER);
26453654Seric 		s->s_mapclass.map_init = hash_map_init;
26556823Seric 		s->s_mapclass.map_lookup = db_map_lookup;
26653654Seric 	}
26753654Seric # endif
26853654Seric 
26957208Seric # ifdef NIS_MAP
27057208Seric 	/* NIS map access */
27157208Seric 	{
27257208Seric 		extern bool nis_map_init();
27357208Seric 		extern char *nis_map_lookup();
27457208Seric 
27557208Seric 		s = stab("nis", ST_MAPCLASS, ST_ENTER);
27657208Seric 		s->s_mapclass.map_init = nis_map_init;
27757208Seric 		s->s_mapclass.map_lookup = nis_map_lookup;
27857208Seric 	}
27957208Seric # endif
28057208Seric 
28153654Seric # ifdef USERDB_MAP
28253654Seric 	/* user database */
28353654Seric 	{
28456836Seric 		extern bool udb_map_init();
28553654Seric 		extern char *udb_map_lookup();
28653654Seric 
28753654Seric 		s = stab("udb", ST_MAPCLASS, ST_ENTER);
28853654Seric 		s->s_mapclass.map_init = udb_map_init;
28953654Seric 		s->s_mapclass.map_lookup = udb_map_lookup;
29053654Seric 	}
29153654Seric # endif
29253654Seric }
29353654Seric /*
29456836Seric **  HOST_MAP_INIT -- initialize host class structures
29556836Seric */
29656836Seric 
29756836Seric bool
29856836Seric host_map_init(map, mapname, args)
29956836Seric 	MAP *map;
30056836Seric 	char *mapname;
30156836Seric 	char *args;
30256836Seric {
30356836Seric 	register char *p = args;
30456836Seric 
30556836Seric 	for (;;)
30656836Seric 	{
30758050Seric 		while (isascii(*p) && isspace(*p))
30856836Seric 			p++;
30956836Seric 		if (*p != '-')
31056836Seric 			break;
31156836Seric 		switch (*++p)
31256836Seric 		{
31356836Seric 		  case 'a':
31456836Seric 			map->map_app = ++p;
31556836Seric 			break;
31656836Seric 		}
31758050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
31856836Seric 			p++;
31956836Seric 		if (*p != '\0')
32056836Seric 			*p++ = '\0';
32156836Seric 	}
32256836Seric 	if (map->map_app != NULL)
32356836Seric 		map->map_app = newstr(map->map_app);
32456836Seric 	return TRUE;
32556836Seric }
32657402Seric /*
32757402Seric **  SETUPMAILERS -- initialize default mailers
32857402Seric */
32956836Seric 
33057402Seric setupmailers()
33157402Seric {
33257402Seric 	char buf[100];
33357402Seric 
33457403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
33557403Seric 	makemailer(buf);
33657403Seric 
33757402Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE");
33857402Seric 	makemailer(buf);
33957402Seric 
34057402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
34157402Seric 	makemailer(buf);
34257402Seric }
34356836Seric /*
3444326Seric **  GETRUID -- get real user id (V7)
3454326Seric */
3464326Seric 
3474326Seric getruid()
3484326Seric {
3499274Seric 	if (OpMode == MD_DAEMON)
3504536Seric 		return (RealUid);
3514536Seric 	else
3524536Seric 		return (getuid());
3534326Seric }
3544326Seric 
3554326Seric 
3564326Seric /*
3574326Seric **  GETRGID -- get real group id (V7).
3584326Seric */
3594326Seric 
3604326Seric getrgid()
3614326Seric {
3629274Seric 	if (OpMode == MD_DAEMON)
3634536Seric 		return (RealGid);
3644536Seric 	else
3654536Seric 		return (getgid());
3664326Seric }
36753654Seric /*
3689369Seric **  USERNAME -- return the user id of the logged in user.
3699369Seric **
3709369Seric **	Parameters:
3719369Seric **		none.
3729369Seric **
3739369Seric **	Returns:
3749369Seric **		The login name of the logged in user.
3759369Seric **
3769369Seric **	Side Effects:
3779369Seric **		none.
3789369Seric **
3799369Seric **	Notes:
3809369Seric **		The return value is statically allocated.
3819369Seric */
3829369Seric 
3839369Seric char *
3849369Seric username()
3859369Seric {
38617469Seric 	static char *myname = NULL;
3879369Seric 	extern char *getlogin();
38819904Smiriam 	register struct passwd *pw;
3899369Seric 
39017469Seric 	/* cache the result */
39117469Seric 	if (myname == NULL)
39217469Seric 	{
39317469Seric 		myname = getlogin();
39417469Seric 		if (myname == NULL || myname[0] == '\0')
39517469Seric 		{
39617469Seric 			pw = getpwuid(getruid());
39717469Seric 			if (pw != NULL)
39840993Sbostic 				myname = newstr(pw->pw_name);
39917469Seric 		}
40019904Smiriam 		else
40119904Smiriam 		{
40258736Seric 			uid_t uid = getuid();
40319873Smiriam 
40440993Sbostic 			myname = newstr(myname);
40540993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
40658736Seric 			      (uid != 0 && uid != pw->pw_uid))
40719904Smiriam 			{
40858736Seric 				pw = getpwuid(uid);
40924945Seric 				if (pw != NULL)
41040993Sbostic 					myname = newstr(pw->pw_name);
41119873Smiriam 			}
41219873Smiriam 		}
41317469Seric 		if (myname == NULL || myname[0] == '\0')
41417469Seric 		{
41558151Seric 			syserr("554 Who are you?");
41617469Seric 			myname = "postmaster";
41717469Seric 		}
41817469Seric 	}
41917469Seric 
42017469Seric 	return (myname);
4219369Seric }
4229369Seric /*
4234190Seric **  TTYPATH -- Get the path of the user's tty
424294Seric **
425294Seric **	Returns the pathname of the user's tty.  Returns NULL if
426294Seric **	the user is not logged in or if s/he has write permission
427294Seric **	denied.
428294Seric **
429294Seric **	Parameters:
430294Seric **		none
431294Seric **
432294Seric **	Returns:
433294Seric **		pathname of the user's tty.
434294Seric **		NULL if not logged in or write permission denied.
435294Seric **
436294Seric **	Side Effects:
437294Seric **		none.
438294Seric **
439294Seric **	WARNING:
440294Seric **		Return value is in a local buffer.
441294Seric **
442294Seric **	Called By:
443294Seric **		savemail
444294Seric */
445294Seric 
446294Seric char *
447294Seric ttypath()
448294Seric {
449294Seric 	struct stat stbuf;
450294Seric 	register char *pathn;
451294Seric 	extern char *ttyname();
4524081Seric 	extern char *getlogin();
453294Seric 
454294Seric 	/* compute the pathname of the controlling tty */
4559369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4569369Seric 	    (pathn = ttyname(0)) == NULL)
457294Seric 	{
458294Seric 		errno = 0;
459294Seric 		return (NULL);
460294Seric 	}
461294Seric 
462294Seric 	/* see if we have write permission */
4632967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
464294Seric 	{
465294Seric 		errno = 0;
466294Seric 		return (NULL);
467294Seric 	}
468294Seric 
469294Seric 	/* see if the user is logged in */
470294Seric 	if (getlogin() == NULL)
471294Seric 		return (NULL);
472294Seric 
473294Seric 	/* looks good */
474294Seric 	return (pathn);
475294Seric }
4762967Seric /*
4772967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4782967Seric **
4792967Seric **	This routine can be supplied on a per-installation basis
4802967Seric **	to determine whether a person is allowed to send a message.
4812967Seric **	This allows restriction of certain types of internet
4822967Seric **	forwarding or registration of users.
4832967Seric **
4842967Seric **	If the hosts are found to be incompatible, an error
48557454Seric **	message should be given using "usrerr" and 0 should
4862967Seric **	be returned.
4872967Seric **
4884288Seric **	'NoReturn' can be set to suppress the return-to-sender
4894288Seric **	function; this should be done on huge messages.
4904288Seric **
4912967Seric **	Parameters:
4922967Seric **		to -- the person being sent to.
4932967Seric **
4942967Seric **	Returns:
49557459Seric **		an exit status
4962967Seric **
4972967Seric **	Side Effects:
4982967Seric **		none (unless you include the usrerr stuff)
4992967Seric */
5002967Seric 
50155012Seric checkcompat(to, e)
5022967Seric 	register ADDRESS *to;
50355012Seric 	register ENVELOPE *e;
5042967Seric {
50512133Seric # ifdef lint
50612133Seric 	if (to == NULL)
50712133Seric 		to++;
50812133Seric # endif lint
50910698Seric # ifdef EXAMPLE_CODE
51010698Seric 	/* this code is intended as an example only */
5114437Seric 	register STAB *s;
5124437Seric 
5134437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
51455012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
5159369Seric 	    to->q_mailer == s->s_mailer)
5164437Seric 	{
51758151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
51810698Seric 		/* NoReturn = TRUE; to supress return copy */
51957459Seric 		return (EX_UNAVAILABLE);
5204437Seric 	}
52156795Seric # endif /* EXAMPLE_CODE */
52257459Seric 	return (EX_OK);
5232967Seric }
5249369Seric /*
5259369Seric **  HOLDSIGS -- arrange to hold all signals
5269369Seric **
5279369Seric **	Parameters:
5289369Seric **		none.
5299369Seric **
5309369Seric **	Returns:
5319369Seric **		none.
5329369Seric **
5339369Seric **	Side Effects:
5349369Seric **		Arranges that signals are held.
5359369Seric */
5369369Seric 
5379369Seric holdsigs()
5389369Seric {
5399369Seric }
5409369Seric /*
5419369Seric **  RLSESIGS -- arrange to release all signals
5429369Seric **
5439369Seric **	This undoes the effect of holdsigs.
5449369Seric **
5459369Seric **	Parameters:
5469369Seric **		none.
5479369Seric **
5489369Seric **	Returns:
5499369Seric **		none.
5509369Seric **
5519369Seric **	Side Effects:
5529369Seric **		Arranges that signals are released.
5539369Seric */
5549369Seric 
5559369Seric rlsesigs()
5569369Seric {
5579369Seric }
55814872Seric /*
55914872Seric **  GETLA -- get the current load average
56014872Seric **
56114881Seric **	This code stolen from la.c.
56214881Seric **
56314872Seric **	Parameters:
56414872Seric **		none.
56514872Seric **
56614872Seric **	Returns:
56714872Seric **		The current load average as an integer.
56814872Seric **
56914872Seric **	Side Effects:
57014872Seric **		none.
57114872Seric */
57214872Seric 
57351920Seric /* try to guess what style of load average we have */
57451920Seric #define LA_ZERO		1	/* always return load average as zero */
57551920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
57651920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
57751920Seric #define LA_SUBR		4	/* call getloadavg */
57814872Seric 
57951920Seric #ifndef LA_TYPE
58051920Seric #  if defined(sun)
58151920Seric #    define LA_TYPE		LA_INT
58251920Seric #  endif
58357977Seric #  if defined(mips) || defined(__alpha)
58457977Seric      /* Ultrix or OSF/1 or RISC/os */
58551920Seric #    define LA_TYPE		LA_INT
58651920Seric #    define LA_AVENRUN		"avenrun"
58751920Seric #  endif
588*59430Seric #  if defined(__hpux)
58951920Seric #    define LA_TYPE		LA_FLOAT
59059253Seric #    define LA_AVENRUN		"avenrun"
59151920Seric #  endif
59251920Seric 
59351920Seric #  ifndef LA_TYPE
59457736Seric #   if defined(SYSTEM5)
59557736Seric #    define LA_TYPE		LA_INT
59657736Seric #    define LA_AVENRUN		"avenrun"
59757736Seric #   else
59857736Seric #    if defined(BSD)
59957736Seric #     define LA_TYPE		LA_SUBR
60057736Seric #    else
60157736Seric #     define LA_TYPE		LA_ZERO
60257736Seric #    endif
60357736Seric #   endif
60451920Seric #  endif
60551920Seric #endif
60651920Seric 
60751920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
60851920Seric 
60914872Seric #include <nlist.h>
61014872Seric 
61151920Seric #ifndef LA_AVENRUN
61251920Seric #define LA_AVENRUN	"_avenrun"
61351920Seric #endif
61451920Seric 
61551920Seric /* _PATH_UNIX should be defined in <paths.h> */
61651920Seric #ifndef _PATH_UNIX
617*59430Seric #  if defined(__hpux)
61851920Seric #    define _PATH_UNIX		"/hp-ux"
61951920Seric #  endif
62051920Seric #  if defined(mips) && !defined(ultrix)
62151920Seric      /* powerful RISC/os */
62251920Seric #    define _PATH_UNIX		"/unix"
62351920Seric #  endif
62457736Seric #  if defined(SYSTEM5)
62557977Seric #    ifndef _PATH_UNIX
62657977Seric #      define _PATH_UNIX	"/unix"
62757977Seric #    endif
62857736Seric #  endif
62951920Seric #  ifndef _PATH_UNIX
63051920Seric #    define _PATH_UNIX		"/vmunix"
63151920Seric #  endif
63251920Seric #endif
63351920Seric 
63414872Seric struct	nlist Nl[] =
63514872Seric {
63651920Seric 	{ LA_AVENRUN },
63714872Seric #define	X_AVENRUN	0
63814872Seric 	{ 0 },
63914872Seric };
64014872Seric 
64157736Seric #if defined(unixpc)
64257736Seric # define FSHIFT		5
64357736Seric #endif
64457736Seric 
64557977Seric #if defined(__alpha)
64657977Seric # define FSHIFT		10
64757977Seric #endif
64857977Seric 
64951920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
65051920Seric #  define FSHIFT	8
65157736Seric #endif
65257736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
65351920Seric #  define FSCALE	(1 << FSHIFT)
65451920Seric #endif
65540930Srick 
65614872Seric getla()
65714872Seric {
65814872Seric 	static int kmem = -1;
65951920Seric #if LA_TYPE == LA_INT
66024943Seric 	long avenrun[3];
66151920Seric #else
66251920Seric 	double avenrun[3];
66351920Seric #endif
66425615Seric 	extern off_t lseek();
66557736Seric 	extern char *errstring();
66657736Seric 	extern int errno;
66714872Seric 
66814872Seric 	if (kmem < 0)
66914872Seric 	{
67024945Seric 		kmem = open("/dev/kmem", 0, 0);
67114872Seric 		if (kmem < 0)
67257736Seric 		{
67357736Seric 			if (tTd(3, 1))
67457736Seric 				printf("getla: open(/dev/kmem): %s\n",
67557736Seric 					errstring(errno));
67614872Seric 			return (-1);
67757736Seric 		}
67851920Seric 		(void) fcntl(kmem, F_SETFD, 1);
67957736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68057736Seric 		{
68157736Seric 			if (tTd(3, 1))
68257736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68357736Seric 					errstring(errno));
68414872Seric 			return (-1);
68557736Seric 		}
68659253Seric 		if (Nl[X_AVENRUN].n_value == 0)
68759253Seric 		{
68859253Seric 			if (tTd(3, 1))
68959253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
69059253Seric 					_PATH_UNIX, LA_AVENRUN);
69159253Seric 			return (-1);
69259253Seric 		}
69314872Seric 	}
69457736Seric 	if (tTd(3, 20))
69557736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
69624945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
69723118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
69819967Seric 	{
69919967Seric 		/* thank you Ian */
70057736Seric 		if (tTd(3, 1))
70157736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
70219967Seric 		return (-1);
70319967Seric 	}
70451920Seric #if LA_TYPE == LA_INT
70557736Seric 	if (tTd(3, 5))
70657736Seric 	{
70757736Seric 		printf("getla: avenrun = %d", avenrun[0]);
70857736Seric 		if (tTd(3, 15))
70957736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71057736Seric 		printf("\n");
71157736Seric 	}
71257736Seric 	if (tTd(3, 1))
71357736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71424943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71551920Seric #else
71657736Seric 	if (tTd(3, 5))
71757736Seric 	{
71857736Seric 		printf("getla: avenrun = %g", avenrun[0]);
71957736Seric 		if (tTd(3, 15))
72057736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
72157736Seric 		printf("\n");
72257736Seric 	}
72357736Seric 	if (tTd(3, 1))
72457736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72551920Seric 	return ((int) (avenrun[0] + 0.5));
72651920Seric #endif
72714872Seric }
72814872Seric 
72951773Seric #else
73051920Seric #if LA_TYPE == LA_SUBR
73151773Seric 
73251773Seric getla()
73351773Seric {
73451920Seric 	double avenrun[3];
73551920Seric 
73651920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
73757736Seric 	{
73857736Seric 		if (tTd(3, 1))
73957736Seric 			perror("getla: getloadavg failed:");
74051920Seric 		return (-1);
74157736Seric 	}
74257736Seric 	if (tTd(3, 1))
74357736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
74451920Seric 	return ((int) (avenrun[0] + 0.5));
74551773Seric }
74651773Seric 
74751773Seric #else
74851773Seric 
74951773Seric getla()
75051773Seric {
75157736Seric 	if (tTd(3, 1))
75257736Seric 		printf("getla: ZERO\n");
75351920Seric 	return (0);
75451773Seric }
75551773Seric 
75651773Seric #endif
75751773Seric #endif
75824943Seric /*
75924943Seric **  SHOULDQUEUE -- should this message be queued or sent?
76024943Seric **
76124943Seric **	Compares the message cost to the load average to decide.
76224943Seric **
76324943Seric **	Parameters:
76424943Seric **		pri -- the priority of the message in question.
76557438Seric **		ctime -- the message creation time.
76624943Seric **
76724943Seric **	Returns:
76824943Seric **		TRUE -- if this message should be queued up for the
76924943Seric **			time being.
77024943Seric **		FALSE -- if the load is low enough to send this message.
77124943Seric **
77224943Seric **	Side Effects:
77324943Seric **		none.
77424943Seric */
77524943Seric 
77624943Seric bool
77757438Seric shouldqueue(pri, ctime)
77824943Seric 	long pri;
77957438Seric 	time_t ctime;
78024943Seric {
78151920Seric 	if (CurrentLA < QueueLA)
78224943Seric 		return (FALSE);
78358132Seric 	if (CurrentLA >= RefuseLA)
78458132Seric 		return (TRUE);
78551920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
78624943Seric }
78724943Seric /*
78853037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
78953037Seric **
79053037Seric **	Parameters:
79153037Seric **		none.
79253037Seric **
79353037Seric **	Returns:
79453037Seric **		TRUE if incoming SMTP connections should be refused
79553037Seric **			(for now).
79653037Seric **		FALSE if we should accept new work.
79753037Seric **
79853037Seric **	Side Effects:
79953037Seric **		none.
80053037Seric */
80153037Seric 
80253037Seric bool
80353037Seric refuseconnections()
80453037Seric {
80559156Seric #ifdef XLA
80659156Seric 	if (!xla_smtp_ok())
80759156Seric 		return TRUE;
80859156Seric #endif
80959156Seric 
81053037Seric 	/* this is probably too simplistic */
81158132Seric 	return (CurrentLA >= RefuseLA);
81253037Seric }
81353037Seric /*
81424943Seric **  SETPROCTITLE -- set process title for ps
81524943Seric **
81624943Seric **	Parameters:
81758674Seric **		fmt -- a printf style format string.
81858674Seric **		a, b, c -- possible parameters to fmt.
81924943Seric **
82024943Seric **	Returns:
82124943Seric **		none.
82224943Seric **
82324943Seric **	Side Effects:
82424943Seric **		Clobbers argv of our main procedure so ps(1) will
82524943Seric **		display the title.
82624943Seric */
82724943Seric 
82858689Seric #ifdef SETPROCTITLE
82958689Seric # ifdef __hpux
83058689Seric #  include <sys/pstat.h>
83158689Seric # endif
83258689Seric #endif
83358689Seric 
83424943Seric /*VARARGS1*/
83557642Seric #ifdef __STDC__
83657642Seric setproctitle(char *fmt, ...)
83757642Seric #else
83857642Seric setproctitle(fmt, va_alist)
83924943Seric 	char *fmt;
84057642Seric 	va_dcl
84157642Seric #endif
84224943Seric {
84324943Seric # ifdef SETPROCTITLE
84424943Seric 	register char *p;
84525049Seric 	register int i;
84658674Seric 	char buf[MAXLINE];
84756852Seric 	VA_LOCAL_DECL
84858689Seric #  ifdef __hpux
84958689Seric 	union pstun pst;
85058689Seric #  endif
85124943Seric 	extern char **Argv;
85224943Seric 	extern char *LastArgv;
85324943Seric 
85458674Seric 	p = buf;
85524943Seric 
85658674Seric 	/* print sendmail: heading for grep */
85758674Seric 	(void) strcpy(p, "sendmail: ");
85858674Seric 	p += strlen(p);
85924943Seric 
86058674Seric 	/* print the argument string */
86158674Seric 	VA_START(fmt);
86258674Seric 	(void) vsprintf(p, fmt, ap);
86356852Seric 	VA_END;
86454996Seric 
86558674Seric 	i = strlen(buf);
86658689Seric 
86758689Seric #  ifdef __hpux
86858689Seric 	pst.pst_command = buf;
86958689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87058689Seric #  else
87158689Seric 
87254996Seric 	if (i > LastArgv - Argv[0] - 2)
87325049Seric 	{
87454996Seric 		i = LastArgv - Argv[0] - 2;
87558674Seric 		buf[i] = '\0';
87625049Seric 	}
87758674Seric 	(void) strcpy(Argv[0], buf);
87854997Seric 	p = &Argv[0][i];
87924943Seric 	while (p < LastArgv)
88024943Seric 		*p++ = ' ';
88158689Seric #  endif
88256795Seric # endif /* SETPROCTITLE */
88324943Seric }
88425698Seric /*
88525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88625698Seric **
88725698Seric **	Parameters:
88825698Seric **		none.
88925698Seric **
89025698Seric **	Returns:
89125698Seric **		none.
89225698Seric **
89325698Seric **	Side Effects:
89425698Seric **		Picks up extant zombies.
89525698Seric */
89625698Seric 
89725698Seric # include <sys/wait.h>
89825698Seric 
89946928Sbostic void
90025698Seric reapchild()
90125698Seric {
90225698Seric # ifdef WNOHANG
90325698Seric 	union wait status;
90425698Seric 
90546928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
90625698Seric 		continue;
90756795Seric # else /* WNOHANG */
90825698Seric 	auto int status;
90925698Seric 
91046928Sbostic 	while (wait((int *)&status) > 0)
91125698Seric 		continue;
91256795Seric # endif /* WNOHANG */
91358061Seric # ifdef SYSTEM5
91458061Seric 	(void) signal(SIGCHLD, reapchild);
91558061Seric # endif
91625698Seric }
91755418Seric /*
91855418Seric **  UNSETENV -- remove a variable from the environment
91955418Seric **
92055418Seric **	Not needed on newer systems.
92155418Seric **
92255418Seric **	Parameters:
92355418Seric **		name -- the string name of the environment variable to be
92455418Seric **			deleted from the current environment.
92555418Seric **
92655418Seric **	Returns:
92755418Seric **		none.
92855418Seric **
92955418Seric **	Globals:
93055418Seric **		environ -- a pointer to the current environment.
93155418Seric **
93255418Seric **	Side Effects:
93355418Seric **		Modifies environ.
93455418Seric */
93555418Seric 
93655418Seric #ifdef UNSETENV
93755418Seric 
93855418Seric void
93955418Seric unsetenv(name)
94055418Seric 	char *name;
94155418Seric {
94255418Seric 	extern char **environ;
94355418Seric 	register char **pp;
94455418Seric 	int len = strlen(name);
94555418Seric 
94655418Seric 	for (pp = environ; *pp != NULL; pp++)
94755418Seric 	{
94855418Seric 		if (strncmp(name, *pp, len) == 0 &&
94955418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
95055418Seric 			break;
95155418Seric 	}
95255418Seric 
95355418Seric 	for (; *pp != NULL; pp++)
95455418Seric 		*pp = pp[1];
95555418Seric }
95655418Seric 
95755418Seric #endif /* UNSETENV */
95856215Seric /*
95956215Seric **  GETDTABLESIZE -- return number of file descriptors
96056215Seric **
96156215Seric **	Only on non-BSD systems
96256215Seric **
96356215Seric **	Parameters:
96456215Seric **		none
96556215Seric **
96656215Seric **	Returns:
96756215Seric **		size of file descriptor table
96856215Seric **
96956215Seric **	Side Effects:
97056215Seric **		none
97156215Seric */
97256215Seric 
97356215Seric #ifdef SYSTEM5
97456215Seric 
97556215Seric int
97656215Seric getdtablesize()
97756215Seric {
97858689Seric # ifdef _SC_OPEN_MAX
97958689Seric 	return sysconf(_SC_OPEN_MAX);
98058689Seric # else
98156215Seric 	return NOFILE;
98258689Seric # endif
98356215Seric }
98456215Seric 
98556215Seric #endif
98657631Seric /*
98757631Seric **  UNAME -- get the UUCP name of this system.
98857631Seric */
98957631Seric 
99057943Seric #ifndef HASUNAME
99157631Seric 
99257631Seric int
99357631Seric uname(name)
99457631Seric 	struct utsname *name;
99557631Seric {
99657631Seric 	FILE *file;
99757631Seric 	char *n;
99857631Seric 
99957631Seric 	name->nodename[0] = '\0';
100057631Seric 
100157661Seric 	/* try /etc/whoami -- one line with the node name */
100257631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
100357631Seric 	{
100457661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
100557631Seric 		(void) fclose(file);
100657661Seric 		n = strchr(name->nodename, '\n');
100757631Seric 		if (n != NULL)
100857631Seric 			*n = '\0';
100957631Seric 		if (name->nodename[0] != '\0')
101057631Seric 			return (0);
101157631Seric 	}
101257631Seric 
101357661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
101457631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
101557631Seric 	{
101657631Seric 		char buf[MAXLINE];
101757631Seric 
101857631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
101957631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
102057631Seric 					NODE_LENGTH, name->nodename) > 0)
102157631Seric 				break;
102257631Seric 		(void) fclose(file);
102357631Seric 		if (name->nodename[0] != '\0')
102457631Seric 			return (0);
102557631Seric 	}
102657631Seric 
102757631Seric #ifdef TRUST_POPEN
102857631Seric 	/*
102957631Seric 	**  Popen is known to have security holes.
103057631Seric 	*/
103157631Seric 
103257661Seric 	/* try uuname -l to return local name */
103357631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
103457631Seric 	{
103557661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
103657631Seric 		(void) pclose(file);
103757661Seric 		n = strchr(name, '\n');
103857631Seric 		if (n != NULL)
103957631Seric 			*n = '\0';
104057661Seric 		if (name->nodename[0] != '\0')
104157631Seric 			return (0);
104257631Seric 	}
104357631Seric #endif
104457631Seric 
104557631Seric 	return (-1);
104657631Seric }
104757943Seric #endif /* HASUNAME */
104858068Seric /*
104958068Seric **  INITGROUPS -- initialize groups
105058068Seric **
105158068Seric **	Stub implementation for System V style systems
105258068Seric */
105358068Seric 
105458068Seric #ifndef HASINITGROUPS
1055*59430Seric # if !defined(SYSTEM5) || defined(__hpux)
105658068Seric #  define HASINITGROUPS
105758068Seric # endif
105858068Seric #endif
105958068Seric 
106058068Seric #ifndef HASINITGROUPS
106158068Seric 
106258068Seric initgroups(name, basegid)
106358068Seric 	char *name;
106458068Seric 	int basegid;
106558068Seric {
106658068Seric 	return 0;
106758068Seric }
106858068Seric 
106958068Seric #endif
107058082Seric /*
107159289Seric **  SETSID -- set session id (for non-POSIX systems)
107259289Seric */
107359289Seric 
107459289Seric #ifndef HASSETSID
107559289Seric 
107659289Seric setsid()
107759289Seric {
107859289Seric # ifdef SYSTEM5
107959289Seric 	setpgrp();
108059289Seric # endif
108159289Seric }
108259289Seric 
108359289Seric #endif
108459289Seric /*
108558082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
108658082Seric **
108758082Seric **	Only implemented if you have statfs.
108858082Seric **
108958082Seric **	Parameters:
109058333Seric **		msize -- the size to check against.  If zero, we don't yet
109158333Seric **			know how big the message will be, so just check for
109258333Seric **			a "reasonable" amount.
109358082Seric **
109458082Seric **	Returns:
109558082Seric **		TRUE if there is enough space.
109658082Seric **		FALSE otherwise.
109758082Seric */
109858082Seric 
109958082Seric #ifndef HASSTATFS
110058082Seric # if defined(BSD4_4) || defined(__osf__)
110158082Seric #  define HASSTATFS
110258082Seric # endif
110358082Seric #endif
110458082Seric 
110558082Seric #ifdef HASSTATFS
110658157Seric # undef HASUSTAT
110758157Seric #endif
110858157Seric 
110958157Seric #if defined(HASUSTAT)
111058157Seric # include <ustat.h>
111158157Seric #endif
111258157Seric 
111358157Seric #ifdef HASSTATFS
111458133Seric # if defined(sgi) || defined(apollo)
111558133Seric #  include <sys/statfs.h>
111658133Seric # else
1117*59430Seric #  if defined(sun) || defined(__hpux)
111858133Seric #   include <sys/vfs.h>
111958133Seric #  else
112058157Seric #   include <sys/mount.h>
112158133Seric #  endif
112258133Seric # endif
112358082Seric #endif
112458082Seric 
112558082Seric bool
112658333Seric enoughspace(msize)
112758333Seric 	long msize;
112858082Seric {
112958160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
113058157Seric # if defined(HASUSTAT)
113158153Seric 	struct ustat fs;
113258153Seric 	struct stat statbuf;
113358366Seric #  define FSBLOCKSIZE	DEV_BSIZE
113458157Seric #  define f_bavail	f_tfree
113558157Seric # else
113658157Seric #  if defined(ultrix)
113758157Seric 	struct fs_data fs;
113858157Seric #   define f_bavail	fd_bfreen
113958366Seric #   define FSBLOCKSIZE	fs.fd_bsize
114058153Seric #  else
114158082Seric 	struct statfs fs;
114258366Seric #   define FSBLOCKSIZE	fs.f_bsize
114358153Seric #  endif
114458133Seric # endif
114558333Seric 	long blocksneeded;
114658082Seric 	extern int errno;
114758082Seric 	extern char *errstring();
114858082Seric 
114958333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
115058082Seric 	{
115158082Seric 		if (tTd(4, 80))
115258133Seric 			printf("enoughspace: no threshold\n");
115358133Seric 		return TRUE;
115458133Seric 	}
115558333Seric 
115658157Seric # if defined(HASUSTAT)
115758157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
115858157Seric # else
115958157Seric #  if defined(sgi) || defined(apollo)
116058133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
116158157Seric #  else
116258157Seric #   if defined(ultrix)
116358133Seric 	if (statfs(QueueDir, &fs) > 0)
116458153Seric #   else
116558133Seric 	if (statfs(QueueDir, &fs) == 0)
116658153Seric #   endif
116758133Seric #  endif
116858133Seric # endif
116958133Seric 	{
117058133Seric 		if (tTd(4, 80))
117158333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
117258333Seric 				fs.f_bavail, msize);
117358333Seric 
117458333Seric 		/* convert msize to block count */
117558366Seric 		msize = msize / FSBLOCKSIZE + 1;
117658333Seric 		if (MinBlocksFree >= 0)
117758333Seric 			msize += MinBlocksFree;
117858333Seric 
117958333Seric 		if (fs.f_bavail < msize)
118058090Seric 		{
118158090Seric #ifdef LOG
118258090Seric 			if (LogLevel > 0)
118358090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
118458333Seric 					QueueDir, fs.f_bavail, msize);
118558090Seric #endif
118658082Seric 			return FALSE;
118758090Seric 		}
118858082Seric 	}
118958082Seric 	else if (tTd(4, 80))
119058333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
119158333Seric 			MinBlocksFree, msize, errstring(errno));
119258082Seric #endif
119358082Seric 	return TRUE;
119458082Seric }
119558542Seric /*
119658542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
119758542Seric **
119858542Seric **	This looks at an errno value and tells if this is likely to
119958542Seric **	go away if retried later.
120058542Seric **
120158542Seric **	Parameters:
120258542Seric **		err -- the errno code to classify.
120358542Seric **
120458542Seric **	Returns:
120558542Seric **		TRUE if this is probably transient.
120658542Seric **		FALSE otherwise.
120758542Seric */
120858542Seric 
120958542Seric bool
121058542Seric transienterror(err)
121158542Seric 	int err;
121258542Seric {
121358542Seric 	switch (err)
121458542Seric 	{
121558542Seric 	  case EIO:			/* I/O error */
121658542Seric 	  case ENXIO:			/* Device not configured */
121758542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
121858542Seric 	  case ENOMEM:			/* Cannot allocate memory */
121958542Seric 	  case ENODEV:			/* Operation not supported by device */
122058542Seric 	  case ENFILE:			/* Too many open files in system */
122158542Seric 	  case EMFILE:			/* Too many open files */
122258542Seric 	  case ENOSPC:			/* No space left on device */
122358542Seric #ifdef ETIMEDOUT
122458542Seric 	  case ETIMEDOUT:		/* Connection timed out */
122558542Seric #endif
122658542Seric #ifdef ESTALE
122758542Seric 	  case ESTALE:			/* Stale NFS file handle */
122858542Seric #endif
122958542Seric #ifdef ENETDOWN
123058542Seric 	  case ENETDOWN:		/* Network is down */
123158542Seric #endif
123258542Seric #ifdef ENETUNREACH
123358542Seric 	  case ENETUNREACH:		/* Network is unreachable */
123458542Seric #endif
123558542Seric #ifdef ENETRESET
123658542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
123758542Seric #endif
123858542Seric #ifdef ECONNABORTED
123958542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
124058542Seric #endif
124158542Seric #ifdef ECONNRESET
124258542Seric 	  case ECONNRESET:		/* Connection reset by peer */
124358542Seric #endif
124458542Seric #ifdef ENOBUFS
124558542Seric 	  case ENOBUFS:			/* No buffer space available */
124658542Seric #endif
124758542Seric #ifdef ESHUTDOWN
124858542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
124958542Seric #endif
125058542Seric #ifdef ECONNREFUSED
125158542Seric 	  case ECONNREFUSED:		/* Connection refused */
125258542Seric #endif
125358542Seric #ifdef EHOSTDOWN
125458542Seric 	  case EHOSTDOWN:		/* Host is down */
125558542Seric #endif
125658542Seric #ifdef EHOSTUNREACH
125758542Seric 	  case EHOSTUNREACH:		/* No route to host */
125858542Seric #endif
125958542Seric #ifdef EDQUOT
126058542Seric 	  case EDQUOT:			/* Disc quota exceeded */
126158542Seric #endif
126258542Seric #ifdef EPROCLIM
126358542Seric 	  case EPROCLIM:		/* Too many processes */
126458542Seric #endif
126558542Seric #ifdef EUSERS
126658542Seric 	  case EUSERS:			/* Too many users */
126758542Seric #endif
126858542Seric #ifdef EDEADLK
126958542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
127058542Seric #endif
127158542Seric #ifdef EISCONN
127258542Seric 	  case EISCONN:			/* Socket already connected */
127358542Seric #endif
127458542Seric #ifdef EINPROGRESS
127558542Seric 	  case EINPROGRESS:		/* Operation now in progress */
127658542Seric #endif
127758542Seric #ifdef EALREADY
127858542Seric 	  case EALREADY:		/* Operation already in progress */
127958542Seric #endif
128058542Seric #ifdef EADDRINUSE
128158542Seric 	  case EADDRINUSE:		/* Address already in use */
128258542Seric #endif
128358542Seric #ifdef EADDRNOTAVAIL
128458542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
128558542Seric #endif
128658542Seric #ifdef ENOSR
128758542Seric 	  case ENOSR:			/* Out of streams resources */
128858542Seric #endif
128958542Seric 		return TRUE;
129058542Seric 	}
129158542Seric 
129258542Seric 	/* nope, must be permanent */
129358542Seric 	return FALSE;
129458542Seric }
129558689Seric /*
129658689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
129758689Seric **
129858689Seric **	Parameters:
129958689Seric **		fd -- the file descriptor of the file.
130058689Seric **		filename -- the file name (for error messages).
130158689Seric **		type -- type of the lock.  Bits can be:
130258689Seric **			LOCK_EX -- exclusive lock.
130358689Seric **			LOCK_NB -- non-blocking.
130458689Seric **
130558689Seric **	Returns:
130658689Seric **		TRUE if the lock was acquired.
130758689Seric **		FALSE otherwise.
130858689Seric */
130958689Seric 
131058689Seric bool
131158689Seric lockfile(fd, filename, type)
131258689Seric 	int fd;
131358689Seric 	char *filename;
131458689Seric 	int type;
131558689Seric {
131658689Seric # ifdef LOCKF
131758689Seric 	int action;
131858689Seric 	struct flock lfd;
131958689Seric 
132058701Seric 	if (bitset(LOCK_EX, type))
132158689Seric 		lfd.l_type = F_WRLCK;
132258689Seric 	else
132358689Seric 		lfd.l_type = F_RDLCK;
132458689Seric 
132558689Seric 	if (bitset(LOCK_NB, type))
132658689Seric 		action = F_SETLK;
132758689Seric 	else
132858689Seric 		action = F_SETLKW;
132958689Seric 
133058689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
133158689Seric 
133258689Seric 	if (fcntl(fd, action, &lfd) >= 0)
133358689Seric 		return TRUE;
133458689Seric 
133558689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
133658689Seric 		syserr("cannot lockf(%s)", filename);
133758689Seric # else
133858689Seric 	if (flock(fd, type) >= 0)
133958689Seric 		return TRUE;
134058689Seric 
134158689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
134258689Seric 		syserr("cannot flock(%s)", filename);
134358689Seric # endif
134458689Seric 	return FALSE;
134558689Seric }
1346