xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 59318)
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*59318Seric static char sccsid[] = "@(#)conf.c	6.48 (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,
88*59318Seric 	"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 # include <sys/stat.h>
447294Seric 
448294Seric char *
449294Seric ttypath()
450294Seric {
451294Seric 	struct stat stbuf;
452294Seric 	register char *pathn;
453294Seric 	extern char *ttyname();
4544081Seric 	extern char *getlogin();
455294Seric 
456294Seric 	/* compute the pathname of the controlling tty */
4579369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4589369Seric 	    (pathn = ttyname(0)) == NULL)
459294Seric 	{
460294Seric 		errno = 0;
461294Seric 		return (NULL);
462294Seric 	}
463294Seric 
464294Seric 	/* see if we have write permission */
4652967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
466294Seric 	{
467294Seric 		errno = 0;
468294Seric 		return (NULL);
469294Seric 	}
470294Seric 
471294Seric 	/* see if the user is logged in */
472294Seric 	if (getlogin() == NULL)
473294Seric 		return (NULL);
474294Seric 
475294Seric 	/* looks good */
476294Seric 	return (pathn);
477294Seric }
4782967Seric /*
4792967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4802967Seric **
4812967Seric **	This routine can be supplied on a per-installation basis
4822967Seric **	to determine whether a person is allowed to send a message.
4832967Seric **	This allows restriction of certain types of internet
4842967Seric **	forwarding or registration of users.
4852967Seric **
4862967Seric **	If the hosts are found to be incompatible, an error
48757454Seric **	message should be given using "usrerr" and 0 should
4882967Seric **	be returned.
4892967Seric **
4904288Seric **	'NoReturn' can be set to suppress the return-to-sender
4914288Seric **	function; this should be done on huge messages.
4924288Seric **
4932967Seric **	Parameters:
4942967Seric **		to -- the person being sent to.
4952967Seric **
4962967Seric **	Returns:
49757459Seric **		an exit status
4982967Seric **
4992967Seric **	Side Effects:
5002967Seric **		none (unless you include the usrerr stuff)
5012967Seric */
5022967Seric 
50355012Seric checkcompat(to, e)
5042967Seric 	register ADDRESS *to;
50555012Seric 	register ENVELOPE *e;
5062967Seric {
50712133Seric # ifdef lint
50812133Seric 	if (to == NULL)
50912133Seric 		to++;
51012133Seric # endif lint
51110698Seric # ifdef EXAMPLE_CODE
51210698Seric 	/* this code is intended as an example only */
5134437Seric 	register STAB *s;
5144437Seric 
5154437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
51655012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
5179369Seric 	    to->q_mailer == s->s_mailer)
5184437Seric 	{
51958151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
52010698Seric 		/* NoReturn = TRUE; to supress return copy */
52157459Seric 		return (EX_UNAVAILABLE);
5224437Seric 	}
52356795Seric # endif /* EXAMPLE_CODE */
52457459Seric 	return (EX_OK);
5252967Seric }
5269369Seric /*
5279369Seric **  HOLDSIGS -- arrange to hold all signals
5289369Seric **
5299369Seric **	Parameters:
5309369Seric **		none.
5319369Seric **
5329369Seric **	Returns:
5339369Seric **		none.
5349369Seric **
5359369Seric **	Side Effects:
5369369Seric **		Arranges that signals are held.
5379369Seric */
5389369Seric 
5399369Seric holdsigs()
5409369Seric {
5419369Seric }
5429369Seric /*
5439369Seric **  RLSESIGS -- arrange to release all signals
5449369Seric **
5459369Seric **	This undoes the effect of holdsigs.
5469369Seric **
5479369Seric **	Parameters:
5489369Seric **		none.
5499369Seric **
5509369Seric **	Returns:
5519369Seric **		none.
5529369Seric **
5539369Seric **	Side Effects:
5549369Seric **		Arranges that signals are released.
5559369Seric */
5569369Seric 
5579369Seric rlsesigs()
5589369Seric {
5599369Seric }
56014872Seric /*
56114872Seric **  GETLA -- get the current load average
56214872Seric **
56314881Seric **	This code stolen from la.c.
56414881Seric **
56514872Seric **	Parameters:
56614872Seric **		none.
56714872Seric **
56814872Seric **	Returns:
56914872Seric **		The current load average as an integer.
57014872Seric **
57114872Seric **	Side Effects:
57214872Seric **		none.
57314872Seric */
57414872Seric 
57551920Seric /* try to guess what style of load average we have */
57651920Seric #define LA_ZERO		1	/* always return load average as zero */
57751920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
57851920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
57951920Seric #define LA_SUBR		4	/* call getloadavg */
58014872Seric 
58151920Seric #ifndef LA_TYPE
58251920Seric #  if defined(sun)
58351920Seric #    define LA_TYPE		LA_INT
58451920Seric #  endif
58557977Seric #  if defined(mips) || defined(__alpha)
58657977Seric      /* Ultrix or OSF/1 or RISC/os */
58751920Seric #    define LA_TYPE		LA_INT
58851920Seric #    define LA_AVENRUN		"avenrun"
58951920Seric #  endif
59051920Seric #  if defined(hpux)
59151920Seric #    define LA_TYPE		LA_FLOAT
59259253Seric #    define LA_AVENRUN		"avenrun"
59351920Seric #  endif
59451920Seric 
59551920Seric #  ifndef LA_TYPE
59657736Seric #   if defined(SYSTEM5)
59757736Seric #    define LA_TYPE		LA_INT
59857736Seric #    define LA_AVENRUN		"avenrun"
59957736Seric #   else
60057736Seric #    if defined(BSD)
60157736Seric #     define LA_TYPE		LA_SUBR
60257736Seric #    else
60357736Seric #     define LA_TYPE		LA_ZERO
60457736Seric #    endif
60557736Seric #   endif
60651920Seric #  endif
60751920Seric #endif
60851920Seric 
60951920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
61051920Seric 
61114872Seric #include <nlist.h>
61214872Seric 
61351920Seric #ifndef LA_AVENRUN
61451920Seric #define LA_AVENRUN	"_avenrun"
61551920Seric #endif
61651920Seric 
61751920Seric /* _PATH_UNIX should be defined in <paths.h> */
61851920Seric #ifndef _PATH_UNIX
61951920Seric #  if defined(hpux)
62051920Seric #    define _PATH_UNIX		"/hp-ux"
62151920Seric #  endif
62251920Seric #  if defined(mips) && !defined(ultrix)
62351920Seric      /* powerful RISC/os */
62451920Seric #    define _PATH_UNIX		"/unix"
62551920Seric #  endif
62657736Seric #  if defined(SYSTEM5)
62757977Seric #    ifndef _PATH_UNIX
62857977Seric #      define _PATH_UNIX	"/unix"
62957977Seric #    endif
63057736Seric #  endif
63151920Seric #  ifndef _PATH_UNIX
63251920Seric #    define _PATH_UNIX		"/vmunix"
63351920Seric #  endif
63451920Seric #endif
63551920Seric 
63614872Seric struct	nlist Nl[] =
63714872Seric {
63851920Seric 	{ LA_AVENRUN },
63914872Seric #define	X_AVENRUN	0
64014872Seric 	{ 0 },
64114872Seric };
64214872Seric 
64357736Seric #if defined(unixpc)
64457736Seric # define FSHIFT		5
64557736Seric #endif
64657736Seric 
64757977Seric #if defined(__alpha)
64857977Seric # define FSHIFT		10
64957977Seric #endif
65057977Seric 
65151920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
65251920Seric #  define FSHIFT	8
65357736Seric #endif
65457736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
65551920Seric #  define FSCALE	(1 << FSHIFT)
65651920Seric #endif
65740930Srick 
65814872Seric getla()
65914872Seric {
66014872Seric 	static int kmem = -1;
66151920Seric #if LA_TYPE == LA_INT
66224943Seric 	long avenrun[3];
66351920Seric #else
66451920Seric 	double avenrun[3];
66551920Seric #endif
66625615Seric 	extern off_t lseek();
66757736Seric 	extern char *errstring();
66857736Seric 	extern int errno;
66914872Seric 
67014872Seric 	if (kmem < 0)
67114872Seric 	{
67224945Seric 		kmem = open("/dev/kmem", 0, 0);
67314872Seric 		if (kmem < 0)
67457736Seric 		{
67557736Seric 			if (tTd(3, 1))
67657736Seric 				printf("getla: open(/dev/kmem): %s\n",
67757736Seric 					errstring(errno));
67814872Seric 			return (-1);
67957736Seric 		}
68051920Seric 		(void) fcntl(kmem, F_SETFD, 1);
68157736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68257736Seric 		{
68357736Seric 			if (tTd(3, 1))
68457736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68557736Seric 					errstring(errno));
68614872Seric 			return (-1);
68757736Seric 		}
68859253Seric 		if (Nl[X_AVENRUN].n_value == 0)
68959253Seric 		{
69059253Seric 			if (tTd(3, 1))
69159253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
69259253Seric 					_PATH_UNIX, LA_AVENRUN);
69359253Seric 			return (-1);
69459253Seric 		}
69514872Seric 	}
69657736Seric 	if (tTd(3, 20))
69757736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
69824945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
69923118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
70019967Seric 	{
70119967Seric 		/* thank you Ian */
70257736Seric 		if (tTd(3, 1))
70357736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
70419967Seric 		return (-1);
70519967Seric 	}
70651920Seric #if LA_TYPE == LA_INT
70757736Seric 	if (tTd(3, 5))
70857736Seric 	{
70957736Seric 		printf("getla: avenrun = %d", avenrun[0]);
71057736Seric 		if (tTd(3, 15))
71157736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71257736Seric 		printf("\n");
71357736Seric 	}
71457736Seric 	if (tTd(3, 1))
71557736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71624943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71751920Seric #else
71857736Seric 	if (tTd(3, 5))
71957736Seric 	{
72057736Seric 		printf("getla: avenrun = %g", avenrun[0]);
72157736Seric 		if (tTd(3, 15))
72257736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
72357736Seric 		printf("\n");
72457736Seric 	}
72557736Seric 	if (tTd(3, 1))
72657736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72751920Seric 	return ((int) (avenrun[0] + 0.5));
72851920Seric #endif
72914872Seric }
73014872Seric 
73151773Seric #else
73251920Seric #if LA_TYPE == LA_SUBR
73351773Seric 
73451773Seric getla()
73551773Seric {
73651920Seric 	double avenrun[3];
73751920Seric 
73851920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
73957736Seric 	{
74057736Seric 		if (tTd(3, 1))
74157736Seric 			perror("getla: getloadavg failed:");
74251920Seric 		return (-1);
74357736Seric 	}
74457736Seric 	if (tTd(3, 1))
74557736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
74651920Seric 	return ((int) (avenrun[0] + 0.5));
74751773Seric }
74851773Seric 
74951773Seric #else
75051773Seric 
75151773Seric getla()
75251773Seric {
75357736Seric 	if (tTd(3, 1))
75457736Seric 		printf("getla: ZERO\n");
75551920Seric 	return (0);
75651773Seric }
75751773Seric 
75851773Seric #endif
75951773Seric #endif
76024943Seric /*
76124943Seric **  SHOULDQUEUE -- should this message be queued or sent?
76224943Seric **
76324943Seric **	Compares the message cost to the load average to decide.
76424943Seric **
76524943Seric **	Parameters:
76624943Seric **		pri -- the priority of the message in question.
76757438Seric **		ctime -- the message creation time.
76824943Seric **
76924943Seric **	Returns:
77024943Seric **		TRUE -- if this message should be queued up for the
77124943Seric **			time being.
77224943Seric **		FALSE -- if the load is low enough to send this message.
77324943Seric **
77424943Seric **	Side Effects:
77524943Seric **		none.
77624943Seric */
77724943Seric 
77824943Seric bool
77957438Seric shouldqueue(pri, ctime)
78024943Seric 	long pri;
78157438Seric 	time_t ctime;
78224943Seric {
78351920Seric 	if (CurrentLA < QueueLA)
78424943Seric 		return (FALSE);
78558132Seric 	if (CurrentLA >= RefuseLA)
78658132Seric 		return (TRUE);
78751920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
78824943Seric }
78924943Seric /*
79053037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
79153037Seric **
79253037Seric **	Parameters:
79353037Seric **		none.
79453037Seric **
79553037Seric **	Returns:
79653037Seric **		TRUE if incoming SMTP connections should be refused
79753037Seric **			(for now).
79853037Seric **		FALSE if we should accept new work.
79953037Seric **
80053037Seric **	Side Effects:
80153037Seric **		none.
80253037Seric */
80353037Seric 
80453037Seric bool
80553037Seric refuseconnections()
80653037Seric {
80759156Seric #ifdef XLA
80859156Seric 	if (!xla_smtp_ok())
80959156Seric 		return TRUE;
81059156Seric #endif
81159156Seric 
81253037Seric 	/* this is probably too simplistic */
81358132Seric 	return (CurrentLA >= RefuseLA);
81453037Seric }
81553037Seric /*
81624943Seric **  SETPROCTITLE -- set process title for ps
81724943Seric **
81824943Seric **	Parameters:
81958674Seric **		fmt -- a printf style format string.
82058674Seric **		a, b, c -- possible parameters to fmt.
82124943Seric **
82224943Seric **	Returns:
82324943Seric **		none.
82424943Seric **
82524943Seric **	Side Effects:
82624943Seric **		Clobbers argv of our main procedure so ps(1) will
82724943Seric **		display the title.
82824943Seric */
82924943Seric 
83058689Seric #ifdef SETPROCTITLE
83158689Seric # ifdef __hpux
83258689Seric #  include <sys/pstat.h>
83358689Seric # endif
83458689Seric #endif
83558689Seric 
83624943Seric /*VARARGS1*/
83757642Seric #ifdef __STDC__
83857642Seric setproctitle(char *fmt, ...)
83957642Seric #else
84057642Seric setproctitle(fmt, va_alist)
84124943Seric 	char *fmt;
84257642Seric 	va_dcl
84357642Seric #endif
84424943Seric {
84524943Seric # ifdef SETPROCTITLE
84624943Seric 	register char *p;
84725049Seric 	register int i;
84858674Seric 	char buf[MAXLINE];
84956852Seric 	VA_LOCAL_DECL
85058689Seric #  ifdef __hpux
85158689Seric 	union pstun pst;
85258689Seric #  endif
85324943Seric 	extern char **Argv;
85424943Seric 	extern char *LastArgv;
85524943Seric 
85658674Seric 	p = buf;
85724943Seric 
85858674Seric 	/* print sendmail: heading for grep */
85958674Seric 	(void) strcpy(p, "sendmail: ");
86058674Seric 	p += strlen(p);
86124943Seric 
86258674Seric 	/* print the argument string */
86358674Seric 	VA_START(fmt);
86458674Seric 	(void) vsprintf(p, fmt, ap);
86556852Seric 	VA_END;
86654996Seric 
86758674Seric 	i = strlen(buf);
86858689Seric 
86958689Seric #  ifdef __hpux
87058689Seric 	pst.pst_command = buf;
87158689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87258689Seric #  else
87358689Seric 
87454996Seric 	if (i > LastArgv - Argv[0] - 2)
87525049Seric 	{
87654996Seric 		i = LastArgv - Argv[0] - 2;
87758674Seric 		buf[i] = '\0';
87825049Seric 	}
87958674Seric 	(void) strcpy(Argv[0], buf);
88054997Seric 	p = &Argv[0][i];
88124943Seric 	while (p < LastArgv)
88224943Seric 		*p++ = ' ';
88358689Seric #  endif
88456795Seric # endif /* SETPROCTITLE */
88524943Seric }
88625698Seric /*
88725698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
88825698Seric **
88925698Seric **	Parameters:
89025698Seric **		none.
89125698Seric **
89225698Seric **	Returns:
89325698Seric **		none.
89425698Seric **
89525698Seric **	Side Effects:
89625698Seric **		Picks up extant zombies.
89725698Seric */
89825698Seric 
89925698Seric # include <sys/wait.h>
90025698Seric 
90146928Sbostic void
90225698Seric reapchild()
90325698Seric {
90425698Seric # ifdef WNOHANG
90525698Seric 	union wait status;
90625698Seric 
90746928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
90825698Seric 		continue;
90956795Seric # else /* WNOHANG */
91025698Seric 	auto int status;
91125698Seric 
91246928Sbostic 	while (wait((int *)&status) > 0)
91325698Seric 		continue;
91456795Seric # endif /* WNOHANG */
91558061Seric # ifdef SYSTEM5
91658061Seric 	(void) signal(SIGCHLD, reapchild);
91758061Seric # endif
91825698Seric }
91955418Seric /*
92055418Seric **  UNSETENV -- remove a variable from the environment
92155418Seric **
92255418Seric **	Not needed on newer systems.
92355418Seric **
92455418Seric **	Parameters:
92555418Seric **		name -- the string name of the environment variable to be
92655418Seric **			deleted from the current environment.
92755418Seric **
92855418Seric **	Returns:
92955418Seric **		none.
93055418Seric **
93155418Seric **	Globals:
93255418Seric **		environ -- a pointer to the current environment.
93355418Seric **
93455418Seric **	Side Effects:
93555418Seric **		Modifies environ.
93655418Seric */
93755418Seric 
93855418Seric #ifdef UNSETENV
93955418Seric 
94055418Seric void
94155418Seric unsetenv(name)
94255418Seric 	char *name;
94355418Seric {
94455418Seric 	extern char **environ;
94555418Seric 	register char **pp;
94655418Seric 	int len = strlen(name);
94755418Seric 
94855418Seric 	for (pp = environ; *pp != NULL; pp++)
94955418Seric 	{
95055418Seric 		if (strncmp(name, *pp, len) == 0 &&
95155418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
95255418Seric 			break;
95355418Seric 	}
95455418Seric 
95555418Seric 	for (; *pp != NULL; pp++)
95655418Seric 		*pp = pp[1];
95755418Seric }
95855418Seric 
95955418Seric #endif /* UNSETENV */
96056215Seric /*
96156215Seric **  GETDTABLESIZE -- return number of file descriptors
96256215Seric **
96356215Seric **	Only on non-BSD systems
96456215Seric **
96556215Seric **	Parameters:
96656215Seric **		none
96756215Seric **
96856215Seric **	Returns:
96956215Seric **		size of file descriptor table
97056215Seric **
97156215Seric **	Side Effects:
97256215Seric **		none
97356215Seric */
97456215Seric 
97556215Seric #ifdef SYSTEM5
97656215Seric 
97756215Seric int
97856215Seric getdtablesize()
97956215Seric {
98058689Seric # ifdef _SC_OPEN_MAX
98158689Seric 	return sysconf(_SC_OPEN_MAX);
98258689Seric # else
98356215Seric 	return NOFILE;
98458689Seric # endif
98556215Seric }
98656215Seric 
98756215Seric #endif
98857631Seric /*
98957631Seric **  UNAME -- get the UUCP name of this system.
99057631Seric */
99157631Seric 
99257943Seric #ifndef HASUNAME
99357631Seric 
99457631Seric int
99557631Seric uname(name)
99657631Seric 	struct utsname *name;
99757631Seric {
99857631Seric 	FILE *file;
99957631Seric 	char *n;
100057631Seric 
100157631Seric 	name->nodename[0] = '\0';
100257631Seric 
100357661Seric 	/* try /etc/whoami -- one line with the node name */
100457631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
100557631Seric 	{
100657661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
100757631Seric 		(void) fclose(file);
100857661Seric 		n = strchr(name->nodename, '\n');
100957631Seric 		if (n != NULL)
101057631Seric 			*n = '\0';
101157631Seric 		if (name->nodename[0] != '\0')
101257631Seric 			return (0);
101357631Seric 	}
101457631Seric 
101557661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
101657631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
101757631Seric 	{
101857631Seric 		char buf[MAXLINE];
101957631Seric 
102057631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
102157631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
102257631Seric 					NODE_LENGTH, name->nodename) > 0)
102357631Seric 				break;
102457631Seric 		(void) fclose(file);
102557631Seric 		if (name->nodename[0] != '\0')
102657631Seric 			return (0);
102757631Seric 	}
102857631Seric 
102957631Seric #ifdef TRUST_POPEN
103057631Seric 	/*
103157631Seric 	**  Popen is known to have security holes.
103257631Seric 	*/
103357631Seric 
103457661Seric 	/* try uuname -l to return local name */
103557631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
103657631Seric 	{
103757661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
103857631Seric 		(void) pclose(file);
103957661Seric 		n = strchr(name, '\n');
104057631Seric 		if (n != NULL)
104157631Seric 			*n = '\0';
104257661Seric 		if (name->nodename[0] != '\0')
104357631Seric 			return (0);
104457631Seric 	}
104557631Seric #endif
104657631Seric 
104757631Seric 	return (-1);
104857631Seric }
104957943Seric #endif /* HASUNAME */
105058068Seric /*
105158068Seric **  INITGROUPS -- initialize groups
105258068Seric **
105358068Seric **	Stub implementation for System V style systems
105458068Seric */
105558068Seric 
105658068Seric #ifndef HASINITGROUPS
105758068Seric # if !defined(SYSTEM5) || defined(hpux)
105858068Seric #  define HASINITGROUPS
105958068Seric # endif
106058068Seric #endif
106158068Seric 
106258068Seric #ifndef HASINITGROUPS
106358068Seric 
106458068Seric initgroups(name, basegid)
106558068Seric 	char *name;
106658068Seric 	int basegid;
106758068Seric {
106858068Seric 	return 0;
106958068Seric }
107058068Seric 
107158068Seric #endif
107258082Seric /*
107359289Seric **  SETSID -- set session id (for non-POSIX systems)
107459289Seric */
107559289Seric 
107659289Seric #ifndef HASSETSID
107759289Seric 
107859289Seric setsid()
107959289Seric {
108059289Seric # ifdef SYSTEM5
108159289Seric 	setpgrp();
108259289Seric # endif
108359289Seric }
108459289Seric 
108559289Seric #endif
108659289Seric /*
108758082Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
108858082Seric **
108958082Seric **	Only implemented if you have statfs.
109058082Seric **
109158082Seric **	Parameters:
109258333Seric **		msize -- the size to check against.  If zero, we don't yet
109358333Seric **			know how big the message will be, so just check for
109458333Seric **			a "reasonable" amount.
109558082Seric **
109658082Seric **	Returns:
109758082Seric **		TRUE if there is enough space.
109858082Seric **		FALSE otherwise.
109958082Seric */
110058082Seric 
110158082Seric #ifndef HASSTATFS
110258082Seric # if defined(BSD4_4) || defined(__osf__)
110358082Seric #  define HASSTATFS
110458082Seric # endif
110558082Seric #endif
110658082Seric 
110758082Seric #ifdef HASSTATFS
110858157Seric # undef HASUSTAT
110958157Seric #endif
111058157Seric 
111158157Seric #if defined(HASUSTAT)
111258157Seric # include <sys/stat.h>
111358157Seric # include <ustat.h>
111458157Seric #endif
111558157Seric 
111658157Seric #ifdef HASSTATFS
111758133Seric # if defined(sgi) || defined(apollo)
111858133Seric #  include <sys/statfs.h>
111958133Seric # else
112058133Seric #  if defined(sun) || defined(hpux)
112158133Seric #   include <sys/vfs.h>
112258133Seric #  else
112358157Seric #   include <sys/mount.h>
112458133Seric #  endif
112558133Seric # endif
112658082Seric #endif
112758082Seric 
112858082Seric bool
112958333Seric enoughspace(msize)
113058333Seric 	long msize;
113158082Seric {
113258160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
113358157Seric # if defined(HASUSTAT)
113458153Seric 	struct ustat fs;
113558153Seric 	struct stat statbuf;
113658366Seric #  define FSBLOCKSIZE	DEV_BSIZE
113758157Seric #  define f_bavail	f_tfree
113858157Seric # else
113958157Seric #  if defined(ultrix)
114058157Seric 	struct fs_data fs;
114158157Seric #   define f_bavail	fd_bfreen
114258366Seric #   define FSBLOCKSIZE	fs.fd_bsize
114358153Seric #  else
114458082Seric 	struct statfs fs;
114558366Seric #   define FSBLOCKSIZE	fs.f_bsize
114658153Seric #  endif
114758133Seric # endif
114858333Seric 	long blocksneeded;
114958082Seric 	extern int errno;
115058082Seric 	extern char *errstring();
115158082Seric 
115258333Seric 	if (MinBlocksFree <= 0 && msize <= 0)
115358082Seric 	{
115458082Seric 		if (tTd(4, 80))
115558133Seric 			printf("enoughspace: no threshold\n");
115658133Seric 		return TRUE;
115758133Seric 	}
115858333Seric 
115958157Seric # if defined(HASUSTAT)
116058157Seric 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
116158157Seric # else
116258157Seric #  if defined(sgi) || defined(apollo)
116358133Seric 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
116458157Seric #  else
116558157Seric #   if defined(ultrix)
116658133Seric 	if (statfs(QueueDir, &fs) > 0)
116758153Seric #   else
116858133Seric 	if (statfs(QueueDir, &fs) == 0)
116958153Seric #   endif
117058133Seric #  endif
117158133Seric # endif
117258133Seric 	{
117358133Seric 		if (tTd(4, 80))
117458333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
117558333Seric 				fs.f_bavail, msize);
117658333Seric 
117758333Seric 		/* convert msize to block count */
117858366Seric 		msize = msize / FSBLOCKSIZE + 1;
117958333Seric 		if (MinBlocksFree >= 0)
118058333Seric 			msize += MinBlocksFree;
118158333Seric 
118258333Seric 		if (fs.f_bavail < msize)
118358090Seric 		{
118458090Seric #ifdef LOG
118558090Seric 			if (LogLevel > 0)
118658090Seric 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
118758333Seric 					QueueDir, fs.f_bavail, msize);
118858090Seric #endif
118958082Seric 			return FALSE;
119058090Seric 		}
119158082Seric 	}
119258082Seric 	else if (tTd(4, 80))
119358333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
119458333Seric 			MinBlocksFree, msize, errstring(errno));
119558082Seric #endif
119658082Seric 	return TRUE;
119758082Seric }
119858542Seric /*
119958542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
120058542Seric **
120158542Seric **	This looks at an errno value and tells if this is likely to
120258542Seric **	go away if retried later.
120358542Seric **
120458542Seric **	Parameters:
120558542Seric **		err -- the errno code to classify.
120658542Seric **
120758542Seric **	Returns:
120858542Seric **		TRUE if this is probably transient.
120958542Seric **		FALSE otherwise.
121058542Seric */
121158542Seric 
121258542Seric bool
121358542Seric transienterror(err)
121458542Seric 	int err;
121558542Seric {
121658542Seric 	switch (err)
121758542Seric 	{
121858542Seric 	  case EIO:			/* I/O error */
121958542Seric 	  case ENXIO:			/* Device not configured */
122058542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
122158542Seric 	  case ENOMEM:			/* Cannot allocate memory */
122258542Seric 	  case ENODEV:			/* Operation not supported by device */
122358542Seric 	  case ENFILE:			/* Too many open files in system */
122458542Seric 	  case EMFILE:			/* Too many open files */
122558542Seric 	  case ENOSPC:			/* No space left on device */
122658542Seric #ifdef ETIMEDOUT
122758542Seric 	  case ETIMEDOUT:		/* Connection timed out */
122858542Seric #endif
122958542Seric #ifdef ESTALE
123058542Seric 	  case ESTALE:			/* Stale NFS file handle */
123158542Seric #endif
123258542Seric #ifdef ENETDOWN
123358542Seric 	  case ENETDOWN:		/* Network is down */
123458542Seric #endif
123558542Seric #ifdef ENETUNREACH
123658542Seric 	  case ENETUNREACH:		/* Network is unreachable */
123758542Seric #endif
123858542Seric #ifdef ENETRESET
123958542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
124058542Seric #endif
124158542Seric #ifdef ECONNABORTED
124258542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
124358542Seric #endif
124458542Seric #ifdef ECONNRESET
124558542Seric 	  case ECONNRESET:		/* Connection reset by peer */
124658542Seric #endif
124758542Seric #ifdef ENOBUFS
124858542Seric 	  case ENOBUFS:			/* No buffer space available */
124958542Seric #endif
125058542Seric #ifdef ESHUTDOWN
125158542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
125258542Seric #endif
125358542Seric #ifdef ECONNREFUSED
125458542Seric 	  case ECONNREFUSED:		/* Connection refused */
125558542Seric #endif
125658542Seric #ifdef EHOSTDOWN
125758542Seric 	  case EHOSTDOWN:		/* Host is down */
125858542Seric #endif
125958542Seric #ifdef EHOSTUNREACH
126058542Seric 	  case EHOSTUNREACH:		/* No route to host */
126158542Seric #endif
126258542Seric #ifdef EDQUOT
126358542Seric 	  case EDQUOT:			/* Disc quota exceeded */
126458542Seric #endif
126558542Seric #ifdef EPROCLIM
126658542Seric 	  case EPROCLIM:		/* Too many processes */
126758542Seric #endif
126858542Seric #ifdef EUSERS
126958542Seric 	  case EUSERS:			/* Too many users */
127058542Seric #endif
127158542Seric #ifdef EDEADLK
127258542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
127358542Seric #endif
127458542Seric #ifdef EISCONN
127558542Seric 	  case EISCONN:			/* Socket already connected */
127658542Seric #endif
127758542Seric #ifdef EINPROGRESS
127858542Seric 	  case EINPROGRESS:		/* Operation now in progress */
127958542Seric #endif
128058542Seric #ifdef EALREADY
128158542Seric 	  case EALREADY:		/* Operation already in progress */
128258542Seric #endif
128358542Seric #ifdef EADDRINUSE
128458542Seric 	  case EADDRINUSE:		/* Address already in use */
128558542Seric #endif
128658542Seric #ifdef EADDRNOTAVAIL
128758542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
128858542Seric #endif
128958542Seric #ifdef ENOSR
129058542Seric 	  case ENOSR:			/* Out of streams resources */
129158542Seric #endif
129258542Seric 		return TRUE;
129358542Seric 	}
129458542Seric 
129558542Seric 	/* nope, must be permanent */
129658542Seric 	return FALSE;
129758542Seric }
129858689Seric /*
129958689Seric **  LOCKFILE -- lock a file using flock or (shudder) lockf
130058689Seric **
130158689Seric **	Parameters:
130258689Seric **		fd -- the file descriptor of the file.
130358689Seric **		filename -- the file name (for error messages).
130458689Seric **		type -- type of the lock.  Bits can be:
130558689Seric **			LOCK_EX -- exclusive lock.
130658689Seric **			LOCK_NB -- non-blocking.
130758689Seric **
130858689Seric **	Returns:
130958689Seric **		TRUE if the lock was acquired.
131058689Seric **		FALSE otherwise.
131158689Seric */
131258689Seric 
131358689Seric bool
131458689Seric lockfile(fd, filename, type)
131558689Seric 	int fd;
131658689Seric 	char *filename;
131758689Seric 	int type;
131858689Seric {
131958689Seric # ifdef LOCKF
132058689Seric 	int action;
132158689Seric 	struct flock lfd;
132258689Seric 
132358701Seric 	if (bitset(LOCK_EX, type))
132458689Seric 		lfd.l_type = F_WRLCK;
132558689Seric 	else
132658689Seric 		lfd.l_type = F_RDLCK;
132758689Seric 
132858689Seric 	if (bitset(LOCK_NB, type))
132958689Seric 		action = F_SETLK;
133058689Seric 	else
133158689Seric 		action = F_SETLKW;
133258689Seric 
133358689Seric 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
133458689Seric 
133558689Seric 	if (fcntl(fd, action, &lfd) >= 0)
133658689Seric 		return TRUE;
133758689Seric 
133858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
133958689Seric 		syserr("cannot lockf(%s)", filename);
134058689Seric # else
134158689Seric 	if (flock(fd, type) >= 0)
134258689Seric 		return TRUE;
134358689Seric 
134458689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
134558689Seric 		syserr("cannot flock(%s)", filename);
134658689Seric # endif
134758689Seric 	return FALSE;
134858689Seric }
1349