xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64380)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*64380Seric static char sccsid[] = "@(#)conf.c	8.32 (Berkeley) 09/02/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1736928Sbostic # include <pwd.h>
18404Seric 
19294Seric /*
203309Seric **  CONF.C -- Sendmail Configuration Tables.
21294Seric **
22294Seric **	Defines the configuration of this installation.
23294Seric **
241388Seric **	Configuration Variables:
252897Seric **		HdrInfo -- a table describing well-known header fields.
262897Seric **			Each entry has the field name and some flags,
274147Seric **			which are described in sendmail.h.
284093Seric **
294093Seric **	Notes:
304093Seric **		I have tried to put almost all the reasonable
314093Seric **		configuration information into the configuration
324093Seric **		file read at runtime.  My intent is that anything
334093Seric **		here is a function of the version of UNIX you
344093Seric **		are running, or is really static -- for example
354093Seric **		the headers are a superset of widely used
364093Seric **		protocols.  If you find yourself playing with
374093Seric **		this file too much, you may be making a mistake!
38294Seric */
39294Seric 
40294Seric 
41294Seric 
42294Seric 
434437Seric /*
442897Seric **  Header info table
453057Seric **	Final (null) entry contains the flags used for any other field.
464147Seric **
474147Seric **	Not all of these are actually handled specially by sendmail
484147Seric **	at this time.  They are included as placeholders, to let
494147Seric **	you know that "someday" I intend to have sendmail do
504147Seric **	something with them.
512897Seric */
522897Seric 
532897Seric struct hdrinfo	HdrInfo[] =
542897Seric {
558060Seric 		/* originator fields, most to least significant  */
5611417Seric 	"resent-sender",	H_FROM|H_RESENT,
5711417Seric 	"resent-from",		H_FROM|H_RESENT,
5825686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
599055Seric 	"sender",		H_FROM,
609055Seric 	"from",			H_FROM,
6125686Seric 	"reply-to",		H_FROM,
629055Seric 	"full-name",		H_ACHECK,
6357359Seric 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
6457359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6558796Seric 
668060Seric 		/* destination fields */
679055Seric 	"to",			H_RCPT,
6811417Seric 	"resent-to",		H_RCPT|H_RESENT,
699055Seric 	"cc",			H_RCPT,
7011417Seric 	"resent-cc",		H_RCPT|H_RESENT,
719055Seric 	"bcc",			H_RCPT|H_ACHECK,
7211417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7356215Seric 	"apparently-to",	H_RCPT,
7458796Seric 
758060Seric 		/* message identification and control */
7611417Seric 	"message-id",		0,
7711417Seric 	"resent-message-id",	H_RESENT,
789055Seric 	"message",		H_EOH,
799055Seric 	"text",			H_EOH,
8058796Seric 
8111417Seric 		/* date fields */
8211417Seric 	"date",			0,
8311417Seric 	"resent-date",		H_RESENT,
8458796Seric 
858060Seric 		/* trace fields */
869055Seric 	"received",		H_TRACE|H_FORCE,
8759318Seric 	"x400-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,
9363753Seric 	"return-path",		H_FORCE|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 */
10558082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1069039Seric 
1079064Seric 
1089064Seric 
1099039Seric /*
11058082Seric **  Privacy values
11158082Seric */
11258082Seric 
11358082Seric struct prival PrivacyValues[] =
11458082Seric {
11558082Seric 	"public",		PRIV_PUBLIC,
11658082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11758114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11858082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11958082Seric 	"noexpn",		PRIV_NOEXPN,
12058082Seric 	"novrfy",		PRIV_NOVRFY,
12164333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12264333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
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 */
16463787Seric 	FileMode = (RealUid != 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 */
17259709Seric 	SevenBit = FALSE;			/* option 7 */
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 /*
20256836Seric **  HOST_MAP_INIT -- initialize host class structures
20356836Seric */
20456836Seric 
20556836Seric bool
20660219Seric host_map_init(map, args)
20756836Seric 	MAP *map;
20856836Seric 	char *args;
20956836Seric {
21056836Seric 	register char *p = args;
21156836Seric 
21256836Seric 	for (;;)
21356836Seric 	{
21458050Seric 		while (isascii(*p) && isspace(*p))
21556836Seric 			p++;
21656836Seric 		if (*p != '-')
21756836Seric 			break;
21856836Seric 		switch (*++p)
21956836Seric 		{
22056836Seric 		  case 'a':
22156836Seric 			map->map_app = ++p;
22256836Seric 			break;
22356836Seric 		}
22458050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
22556836Seric 			p++;
22656836Seric 		if (*p != '\0')
22756836Seric 			*p++ = '\0';
22856836Seric 	}
22956836Seric 	if (map->map_app != NULL)
23056836Seric 		map->map_app = newstr(map->map_app);
23156836Seric 	return TRUE;
23256836Seric }
23357402Seric /*
23457402Seric **  SETUPMAILERS -- initialize default mailers
23557402Seric */
23656836Seric 
23757402Seric setupmailers()
23857402Seric {
23957402Seric 	char buf[100];
24057402Seric 
24157403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
24257403Seric 	makemailer(buf);
24357403Seric 
24459883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
24557402Seric 	makemailer(buf);
24657402Seric 
24757402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
24857402Seric 	makemailer(buf);
24957402Seric }
25056836Seric /*
25160207Seric **  SETUPMAPS -- set up map classes
25260207Seric */
25360207Seric 
25460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
25560207Seric 	{ \
25660207Seric 		extern bool parse __P((MAP *, char *)); \
25760207Seric 		extern bool open __P((MAP *, int)); \
25860207Seric 		extern void close __P((MAP *)); \
25960207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
26060207Seric 		extern void store __P((MAP *, char *, char *)); \
26160207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
26260207Seric 		s->s_mapclass.map_cname = name; \
26360207Seric 		s->s_mapclass.map_ext = ext; \
26460207Seric 		s->s_mapclass.map_cflags = flags; \
26560207Seric 		s->s_mapclass.map_parse = parse; \
26660207Seric 		s->s_mapclass.map_open = open; \
26760207Seric 		s->s_mapclass.map_close = close; \
26860207Seric 		s->s_mapclass.map_lookup = lookup; \
26960207Seric 		s->s_mapclass.map_store = store; \
27060207Seric 	}
27160207Seric 
27260207Seric setupmaps()
27360207Seric {
27460207Seric 	register STAB *s;
27560207Seric 
27660207Seric #ifdef NEWDB
27760207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
27860207Seric 		map_parseargs, hash_map_open, db_map_close,
27960207Seric 		db_map_lookup, db_map_store);
28060207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
28160207Seric 		map_parseargs, bt_map_open, db_map_close,
28260207Seric 		db_map_lookup, db_map_store);
28360207Seric #endif
28460207Seric 
28560207Seric #ifdef NDBM
28660207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
28760207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
28860207Seric 		ndbm_map_lookup, ndbm_map_store);
28960207Seric #endif
29060207Seric 
29160207Seric #ifdef NIS
29260207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
29360207Seric 		map_parseargs, nis_map_open, nis_map_close,
29460207Seric 		nis_map_lookup, nis_map_store);
29560207Seric #endif
29660207Seric 
29760207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
29860207Seric 		map_parseargs, stab_map_open, stab_map_close,
29960207Seric 		stab_map_lookup, stab_map_store);
30060207Seric 
30160207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
30260207Seric 		map_parseargs, impl_map_open, impl_map_close,
30360207Seric 		impl_map_lookup, impl_map_store);
30460207Seric 
30560207Seric 	/* host DNS lookup */
30660207Seric 	MAPDEF("host", NULL, 0,
30760207Seric 		host_map_init, null_map_open, null_map_close,
30860207Seric 		host_map_lookup, null_map_store);
30960207Seric 
31060207Seric 	/* dequote map */
31160207Seric 	MAPDEF("dequote", NULL, 0,
31260207Seric 		dequote_init, null_map_open, null_map_close,
31360207Seric 		dequote_map, null_map_store);
31460207Seric 
31560207Seric #if 0
31660207Seric # ifdef USERDB
31760207Seric 	/* user database */
31860207Seric 	MAPDEF("udb", ".db", 0,
31960207Seric 		udb_map_parse, null_map_open, null_map_close,
32060207Seric 		udb_map_lookup, null_map_store);
32160207Seric # endif
32260207Seric #endif
32360207Seric }
32460207Seric 
32560207Seric #undef MAPDEF
32660207Seric /*
3279369Seric **  USERNAME -- return the user id of the logged in user.
3289369Seric **
3299369Seric **	Parameters:
3309369Seric **		none.
3319369Seric **
3329369Seric **	Returns:
3339369Seric **		The login name of the logged in user.
3349369Seric **
3359369Seric **	Side Effects:
3369369Seric **		none.
3379369Seric **
3389369Seric **	Notes:
3399369Seric **		The return value is statically allocated.
3409369Seric */
3419369Seric 
3429369Seric char *
3439369Seric username()
3449369Seric {
34517469Seric 	static char *myname = NULL;
3469369Seric 	extern char *getlogin();
34719904Smiriam 	register struct passwd *pw;
3489369Seric 
34917469Seric 	/* cache the result */
35017469Seric 	if (myname == NULL)
35117469Seric 	{
35217469Seric 		myname = getlogin();
35317469Seric 		if (myname == NULL || myname[0] == '\0')
35417469Seric 		{
35563787Seric 			pw = getpwuid(RealUid);
35617469Seric 			if (pw != NULL)
35740993Sbostic 				myname = newstr(pw->pw_name);
35817469Seric 		}
35919904Smiriam 		else
36019904Smiriam 		{
36163787Seric 			uid_t uid = RealUid;
36219873Smiriam 
36340993Sbostic 			myname = newstr(myname);
36440993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
36558736Seric 			      (uid != 0 && uid != pw->pw_uid))
36619904Smiriam 			{
36758736Seric 				pw = getpwuid(uid);
36824945Seric 				if (pw != NULL)
36940993Sbostic 					myname = newstr(pw->pw_name);
37019873Smiriam 			}
37119873Smiriam 		}
37217469Seric 		if (myname == NULL || myname[0] == '\0')
37317469Seric 		{
37458151Seric 			syserr("554 Who are you?");
37517469Seric 			myname = "postmaster";
37617469Seric 		}
37717469Seric 	}
37817469Seric 
37917469Seric 	return (myname);
3809369Seric }
3819369Seric /*
3824190Seric **  TTYPATH -- Get the path of the user's tty
383294Seric **
384294Seric **	Returns the pathname of the user's tty.  Returns NULL if
385294Seric **	the user is not logged in or if s/he has write permission
386294Seric **	denied.
387294Seric **
388294Seric **	Parameters:
389294Seric **		none
390294Seric **
391294Seric **	Returns:
392294Seric **		pathname of the user's tty.
393294Seric **		NULL if not logged in or write permission denied.
394294Seric **
395294Seric **	Side Effects:
396294Seric **		none.
397294Seric **
398294Seric **	WARNING:
399294Seric **		Return value is in a local buffer.
400294Seric **
401294Seric **	Called By:
402294Seric **		savemail
403294Seric */
404294Seric 
405294Seric char *
406294Seric ttypath()
407294Seric {
408294Seric 	struct stat stbuf;
409294Seric 	register char *pathn;
410294Seric 	extern char *ttyname();
4114081Seric 	extern char *getlogin();
412294Seric 
413294Seric 	/* compute the pathname of the controlling tty */
4149369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4159369Seric 	    (pathn = ttyname(0)) == NULL)
416294Seric 	{
417294Seric 		errno = 0;
418294Seric 		return (NULL);
419294Seric 	}
420294Seric 
421294Seric 	/* see if we have write permission */
4222967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
423294Seric 	{
424294Seric 		errno = 0;
425294Seric 		return (NULL);
426294Seric 	}
427294Seric 
428294Seric 	/* see if the user is logged in */
429294Seric 	if (getlogin() == NULL)
430294Seric 		return (NULL);
431294Seric 
432294Seric 	/* looks good */
433294Seric 	return (pathn);
434294Seric }
4352967Seric /*
4362967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4372967Seric **
4382967Seric **	This routine can be supplied on a per-installation basis
4392967Seric **	to determine whether a person is allowed to send a message.
4402967Seric **	This allows restriction of certain types of internet
4412967Seric **	forwarding or registration of users.
4422967Seric **
4432967Seric **	If the hosts are found to be incompatible, an error
44457454Seric **	message should be given using "usrerr" and 0 should
4452967Seric **	be returned.
4462967Seric **
4474288Seric **	'NoReturn' can be set to suppress the return-to-sender
4484288Seric **	function; this should be done on huge messages.
4494288Seric **
4502967Seric **	Parameters:
4512967Seric **		to -- the person being sent to.
4522967Seric **
4532967Seric **	Returns:
45457459Seric **		an exit status
4552967Seric **
4562967Seric **	Side Effects:
4572967Seric **		none (unless you include the usrerr stuff)
4582967Seric */
4592967Seric 
46055012Seric checkcompat(to, e)
4612967Seric 	register ADDRESS *to;
46255012Seric 	register ENVELOPE *e;
4632967Seric {
46412133Seric # ifdef lint
46512133Seric 	if (to == NULL)
46612133Seric 		to++;
46763834Seric # endif /* lint */
46810698Seric # ifdef EXAMPLE_CODE
46910698Seric 	/* this code is intended as an example only */
4704437Seric 	register STAB *s;
4714437Seric 
4724437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
47355012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4749369Seric 	    to->q_mailer == s->s_mailer)
4754437Seric 	{
47658151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
47710698Seric 		/* NoReturn = TRUE; to supress return copy */
47857459Seric 		return (EX_UNAVAILABLE);
4794437Seric 	}
48056795Seric # endif /* EXAMPLE_CODE */
48157459Seric 	return (EX_OK);
4822967Seric }
4839369Seric /*
48464035Seric **  SETSIGNAL -- set a signal handler
48564035Seric **
48664035Seric **	This is essentially old BSD "signal(3)".
48764035Seric */
48864035Seric 
48964035Seric setsig_t
49064035Seric setsignal(sig, handler)
49164035Seric 	int sig;
49264035Seric 	setsig_t handler;
49364035Seric {
494*64380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
49564035Seric 	return signal(sig, handler);
49664035Seric #else
49764035Seric 	struct sigaction n, o;
49864035Seric 
49964035Seric 	bzero(&n, sizeof n);
50064035Seric 	n.sa_handler = handler;
50164035Seric 	if (sigaction(sig, &n, &o) < 0)
50264035Seric 		return SIG_ERR;
50364035Seric 	return o.sa_handler;
50464035Seric #endif
50564035Seric }
50664035Seric /*
5079369Seric **  HOLDSIGS -- arrange to hold all signals
5089369Seric **
5099369Seric **	Parameters:
5109369Seric **		none.
5119369Seric **
5129369Seric **	Returns:
5139369Seric **		none.
5149369Seric **
5159369Seric **	Side Effects:
5169369Seric **		Arranges that signals are held.
5179369Seric */
5189369Seric 
5199369Seric holdsigs()
5209369Seric {
5219369Seric }
5229369Seric /*
5239369Seric **  RLSESIGS -- arrange to release all signals
5249369Seric **
5259369Seric **	This undoes the effect of holdsigs.
5269369Seric **
5279369Seric **	Parameters:
5289369Seric **		none.
5299369Seric **
5309369Seric **	Returns:
5319369Seric **		none.
5329369Seric **
5339369Seric **	Side Effects:
5349369Seric **		Arranges that signals are released.
5359369Seric */
5369369Seric 
5379369Seric rlsesigs()
5389369Seric {
5399369Seric }
54014872Seric /*
54114872Seric **  GETLA -- get the current load average
54214872Seric **
54314881Seric **	This code stolen from la.c.
54414881Seric **
54514872Seric **	Parameters:
54614872Seric **		none.
54714872Seric **
54814872Seric **	Returns:
54914872Seric **		The current load average as an integer.
55014872Seric **
55114872Seric **	Side Effects:
55214872Seric **		none.
55314872Seric */
55414872Seric 
55551920Seric /* try to guess what style of load average we have */
55651920Seric #define LA_ZERO		1	/* always return load average as zero */
55764360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
55851920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
55951920Seric #define LA_SUBR		4	/* call getloadavg */
56064295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
56164360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
56214872Seric 
56363962Seric /* do guesses based on general OS type */
56451920Seric #ifndef LA_TYPE
56563962Seric # define LA_TYPE	LA_ZERO
56651920Seric #endif
56751920Seric 
56864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
56951920Seric 
57014872Seric #include <nlist.h>
57114872Seric 
57251920Seric #ifndef LA_AVENRUN
57363962Seric # ifdef SYSTEM5
57463962Seric #  define LA_AVENRUN	"avenrun"
57563962Seric # else
57663962Seric #  define LA_AVENRUN	"_avenrun"
57763962Seric # endif
57851920Seric #endif
57951920Seric 
58051920Seric /* _PATH_UNIX should be defined in <paths.h> */
58151920Seric #ifndef _PATH_UNIX
58263962Seric # if defined(SYSTEM5)
58363962Seric #  define _PATH_UNIX	"/unix"
58463962Seric # else
58563962Seric #  define _PATH_UNIX	"/vmunix"
58663962Seric # endif
58751920Seric #endif
58851920Seric 
58914872Seric struct	nlist Nl[] =
59014872Seric {
59151920Seric 	{ LA_AVENRUN },
59214872Seric #define	X_AVENRUN	0
59314872Seric 	{ 0 },
59414872Seric };
59514872Seric 
59660583Seric #ifndef FSHIFT
59760583Seric # if defined(unixpc)
59860583Seric #  define FSHIFT	5
59960583Seric # endif
60057736Seric 
60160583Seric # if defined(__alpha)
60260583Seric #  define FSHIFT	10
60360583Seric # endif
60457977Seric 
60564360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
60651920Seric #  define FSHIFT	8
60760583Seric # endif
60857736Seric #endif
60960583Seric 
61064360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
61151920Seric #  define FSCALE	(1 << FSHIFT)
61251920Seric #endif
61340930Srick 
61414872Seric getla()
61514872Seric {
61614872Seric 	static int kmem = -1;
61751920Seric #if LA_TYPE == LA_INT
61824943Seric 	long avenrun[3];
61951920Seric #else
62064360Seric # if LA_TYPE == LA_SHORT
62164360Seric 	short avenrun[3];
62264360Seric # else
62351920Seric 	double avenrun[3];
62464360Seric # endif
62551920Seric #endif
62625615Seric 	extern off_t lseek();
62757736Seric 	extern int errno;
62814872Seric 
62914872Seric 	if (kmem < 0)
63014872Seric 	{
63124945Seric 		kmem = open("/dev/kmem", 0, 0);
63214872Seric 		if (kmem < 0)
63357736Seric 		{
63457736Seric 			if (tTd(3, 1))
63557736Seric 				printf("getla: open(/dev/kmem): %s\n",
63657736Seric 					errstring(errno));
63714872Seric 			return (-1);
63857736Seric 		}
63951920Seric 		(void) fcntl(kmem, F_SETFD, 1);
64057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
64157736Seric 		{
64257736Seric 			if (tTd(3, 1))
64357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
64457736Seric 					errstring(errno));
64514872Seric 			return (-1);
64657736Seric 		}
64759253Seric 		if (Nl[X_AVENRUN].n_value == 0)
64859253Seric 		{
64959253Seric 			if (tTd(3, 1))
65059253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
65159253Seric 					_PATH_UNIX, LA_AVENRUN);
65259253Seric 			return (-1);
65359253Seric 		}
65414872Seric 	}
65557736Seric 	if (tTd(3, 20))
65657736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
65724945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
65823118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
65919967Seric 	{
66019967Seric 		/* thank you Ian */
66157736Seric 		if (tTd(3, 1))
66257736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
66319967Seric 		return (-1);
66419967Seric 	}
66564360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
66657736Seric 	if (tTd(3, 5))
66757736Seric 	{
66857736Seric 		printf("getla: avenrun = %d", avenrun[0]);
66957736Seric 		if (tTd(3, 15))
67057736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
67157736Seric 		printf("\n");
67257736Seric 	}
67357736Seric 	if (tTd(3, 1))
67457736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
67524943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
67651920Seric #else
67757736Seric 	if (tTd(3, 5))
67857736Seric 	{
67957736Seric 		printf("getla: avenrun = %g", avenrun[0]);
68057736Seric 		if (tTd(3, 15))
68157736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
68257736Seric 		printf("\n");
68357736Seric 	}
68457736Seric 	if (tTd(3, 1))
68557736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
68651920Seric 	return ((int) (avenrun[0] + 0.5));
68751920Seric #endif
68814872Seric }
68914872Seric 
69051773Seric #else
69151920Seric #if LA_TYPE == LA_SUBR
69251773Seric 
69351773Seric getla()
69451773Seric {
69551920Seric 	double avenrun[3];
69651920Seric 
69751920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
69857736Seric 	{
69957736Seric 		if (tTd(3, 1))
70057736Seric 			perror("getla: getloadavg failed:");
70151920Seric 		return (-1);
70257736Seric 	}
70357736Seric 	if (tTd(3, 1))
70457736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
70551920Seric 	return ((int) (avenrun[0] + 0.5));
70651773Seric }
70751773Seric 
70851773Seric #else
70964295Seric #if LA_TYPE == LA_MACH
71051773Seric 
71164295Seric /*
71264295Seric **  This has been tested on NeXT release 2.1.
71364295Seric */
71464295Seric 
71564295Seric #include <mach.h>
71664295Seric 
71751773Seric getla()
71851773Seric {
71964295Seric 	processor_set_t default_set;
72064295Seric 	kern_return_t error;
72164295Seric 	unsigned int info_count;
72264295Seric 	struct processor_set_basic_info info;
72364295Seric 	host_t host;
72464295Seric 
72564295Seric 	error = processor_set_default(host_self(), &default_set);
72664295Seric 	if (error != KERN_SUCCESS)
72764295Seric 		return -1;
72864295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
72964295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
73064295Seric 			       &host, (processor_set_info_t)&info,
73164295Seric 			       &info_count) != KERN_SUCCESS)
73264295Seric 	{
73364295Seric 		return -1;
73464295Seric 	}
73564295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
73664295Seric }
73764295Seric 
73864295Seric 
73964295Seric #else
74064295Seric 
74164295Seric getla()
74264295Seric {
74357736Seric 	if (tTd(3, 1))
74457736Seric 		printf("getla: ZERO\n");
74551920Seric 	return (0);
74651773Seric }
74751773Seric 
74851773Seric #endif
74951773Seric #endif
75064295Seric #endif
75124943Seric /*
75224943Seric **  SHOULDQUEUE -- should this message be queued or sent?
75324943Seric **
75424943Seric **	Compares the message cost to the load average to decide.
75524943Seric **
75624943Seric **	Parameters:
75724943Seric **		pri -- the priority of the message in question.
75857438Seric **		ctime -- the message creation time.
75924943Seric **
76024943Seric **	Returns:
76124943Seric **		TRUE -- if this message should be queued up for the
76224943Seric **			time being.
76324943Seric **		FALSE -- if the load is low enough to send this message.
76424943Seric **
76524943Seric **	Side Effects:
76624943Seric **		none.
76724943Seric */
76824943Seric 
76924943Seric bool
77057438Seric shouldqueue(pri, ctime)
77124943Seric 	long pri;
77257438Seric 	time_t ctime;
77324943Seric {
77451920Seric 	if (CurrentLA < QueueLA)
77524943Seric 		return (FALSE);
77658132Seric 	if (CurrentLA >= RefuseLA)
77758132Seric 		return (TRUE);
77851920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77924943Seric }
78024943Seric /*
78153037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
78253037Seric **
78353037Seric **	Parameters:
78453037Seric **		none.
78553037Seric **
78653037Seric **	Returns:
78753037Seric **		TRUE if incoming SMTP connections should be refused
78853037Seric **			(for now).
78953037Seric **		FALSE if we should accept new work.
79053037Seric **
79153037Seric **	Side Effects:
79253037Seric **		none.
79353037Seric */
79453037Seric 
79553037Seric bool
79653037Seric refuseconnections()
79753037Seric {
79859156Seric #ifdef XLA
79959156Seric 	if (!xla_smtp_ok())
80059156Seric 		return TRUE;
80159156Seric #endif
80259156Seric 
80353037Seric 	/* this is probably too simplistic */
80458132Seric 	return (CurrentLA >= RefuseLA);
80553037Seric }
80653037Seric /*
80724943Seric **  SETPROCTITLE -- set process title for ps
80824943Seric **
80924943Seric **	Parameters:
81058674Seric **		fmt -- a printf style format string.
81158674Seric **		a, b, c -- possible parameters to fmt.
81224943Seric **
81324943Seric **	Returns:
81424943Seric **		none.
81524943Seric **
81624943Seric **	Side Effects:
81724943Seric **		Clobbers argv of our main procedure so ps(1) will
81824943Seric **		display the title.
81924943Seric */
82024943Seric 
82158689Seric #ifdef SETPROCTITLE
82258689Seric # ifdef __hpux
82358689Seric #  include <sys/pstat.h>
82458689Seric # endif
82559732Seric # ifdef BSD4_4
82659732Seric #  include <machine/vmparam.h>
82759732Seric #  include <sys/exec.h>
82864247Seric #  ifdef __bsdi__
82964247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
83064247Seric #  endif
83159992Seric #  ifdef PS_STRINGS
83259992Seric #   define SETPROC_STATIC static
83359992Seric #  endif
83459732Seric # endif
83559732Seric # ifndef SETPROC_STATIC
83659732Seric #  define SETPROC_STATIC
83759732Seric # endif
83858689Seric #endif
83958689Seric 
84024943Seric /*VARARGS1*/
84157642Seric #ifdef __STDC__
84257642Seric setproctitle(char *fmt, ...)
84357642Seric #else
84457642Seric setproctitle(fmt, va_alist)
84524943Seric 	char *fmt;
84657642Seric 	va_dcl
84757642Seric #endif
84824943Seric {
84924943Seric # ifdef SETPROCTITLE
85024943Seric 	register char *p;
85125049Seric 	register int i;
85259732Seric 	SETPROC_STATIC char buf[MAXLINE];
85356852Seric 	VA_LOCAL_DECL
85458689Seric #  ifdef __hpux
85558689Seric 	union pstun pst;
85658689Seric #  endif
85724943Seric 	extern char **Argv;
85824943Seric 	extern char *LastArgv;
85924943Seric 
86058674Seric 	p = buf;
86124943Seric 
86258674Seric 	/* print sendmail: heading for grep */
86358674Seric 	(void) strcpy(p, "sendmail: ");
86458674Seric 	p += strlen(p);
86524943Seric 
86658674Seric 	/* print the argument string */
86758674Seric 	VA_START(fmt);
86858674Seric 	(void) vsprintf(p, fmt, ap);
86956852Seric 	VA_END;
87054996Seric 
87158674Seric 	i = strlen(buf);
87258689Seric 
87358689Seric #  ifdef __hpux
87458689Seric 	pst.pst_command = buf;
87558689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87658689Seric #  else
87759992Seric #   ifdef PS_STRINGS
87859732Seric 	PS_STRINGS->ps_nargvstr = 1;
87959732Seric 	PS_STRINGS->ps_argvstr = buf;
88059732Seric #   else
88154996Seric 	if (i > LastArgv - Argv[0] - 2)
88225049Seric 	{
88354996Seric 		i = LastArgv - Argv[0] - 2;
88458674Seric 		buf[i] = '\0';
88525049Seric 	}
88658674Seric 	(void) strcpy(Argv[0], buf);
88754997Seric 	p = &Argv[0][i];
88824943Seric 	while (p < LastArgv)
88924943Seric 		*p++ = ' ';
89059732Seric #   endif
89158689Seric #  endif
89256795Seric # endif /* SETPROCTITLE */
89324943Seric }
89425698Seric /*
89525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
89625698Seric **
89725698Seric **	Parameters:
89825698Seric **		none.
89925698Seric **
90025698Seric **	Returns:
90125698Seric **		none.
90225698Seric **
90325698Seric **	Side Effects:
90425698Seric **		Picks up extant zombies.
90525698Seric */
90625698Seric 
90725698Seric # include <sys/wait.h>
90825698Seric 
90946928Sbostic void
91025698Seric reapchild()
91125698Seric {
91263966Seric # ifdef HASWAITPID
91360219Seric 	auto int status;
91460560Seric 	int count;
91560560Seric 	int pid;
91660219Seric 
91760560Seric 	count = 0;
91860560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
91960560Seric 	{
92060560Seric 		if (count++ > 1000)
92160560Seric 		{
92260560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
92360560Seric 				pid, status);
92460560Seric 			break;
92560560Seric 		}
92660560Seric 	}
92760219Seric # else
92825698Seric # ifdef WNOHANG
92925698Seric 	union wait status;
93025698Seric 
93163753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
93225698Seric 		continue;
93356795Seric # else /* WNOHANG */
93425698Seric 	auto int status;
93525698Seric 
93660219Seric 	while (wait(&status) > 0)
93725698Seric 		continue;
93856795Seric # endif /* WNOHANG */
93960219Seric # endif
94064035Seric # ifdef SYS5SIGNALS
94164035Seric 	(void) setsignal(SIGCHLD, reapchild);
94258061Seric # endif
94325698Seric }
94455418Seric /*
94555418Seric **  UNSETENV -- remove a variable from the environment
94655418Seric **
94755418Seric **	Not needed on newer systems.
94855418Seric **
94955418Seric **	Parameters:
95055418Seric **		name -- the string name of the environment variable to be
95155418Seric **			deleted from the current environment.
95255418Seric **
95355418Seric **	Returns:
95455418Seric **		none.
95555418Seric **
95655418Seric **	Globals:
95755418Seric **		environ -- a pointer to the current environment.
95855418Seric **
95955418Seric **	Side Effects:
96055418Seric **		Modifies environ.
96155418Seric */
96255418Seric 
96363962Seric #ifndef HASUNSETENV
96455418Seric 
96555418Seric void
96655418Seric unsetenv(name)
96755418Seric 	char *name;
96855418Seric {
96955418Seric 	extern char **environ;
97055418Seric 	register char **pp;
97155418Seric 	int len = strlen(name);
97255418Seric 
97355418Seric 	for (pp = environ; *pp != NULL; pp++)
97455418Seric 	{
97555418Seric 		if (strncmp(name, *pp, len) == 0 &&
97655418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
97755418Seric 			break;
97855418Seric 	}
97955418Seric 
98055418Seric 	for (; *pp != NULL; pp++)
98155418Seric 		*pp = pp[1];
98255418Seric }
98355418Seric 
98463962Seric #endif
98556215Seric /*
98656215Seric **  GETDTABLESIZE -- return number of file descriptors
98756215Seric **
98856215Seric **	Only on non-BSD systems
98956215Seric **
99056215Seric **	Parameters:
99156215Seric **		none
99256215Seric **
99356215Seric **	Returns:
99456215Seric **		size of file descriptor table
99556215Seric **
99656215Seric **	Side Effects:
99756215Seric **		none
99856215Seric */
99956215Seric 
100063787Seric #ifdef SOLARIS
100163787Seric # include <sys/resource.h>
100263787Seric #endif
100356215Seric 
100456215Seric int
100563787Seric getdtsize()
100656215Seric {
100763787Seric #ifdef RLIMIT_NOFILE
100863787Seric 	struct rlimit rl;
100963787Seric 
101063787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
101163787Seric 		return rl.rlim_cur;
101263787Seric #endif
101363787Seric 
101464031Seric # ifdef HASGETDTABLESIZE
101564031Seric 	return getdtablesize();
101664031Seric # else
101764031Seric #  ifdef _SC_OPEN_MAX
101858689Seric 	return sysconf(_SC_OPEN_MAX);
101963787Seric #  else
102056215Seric 	return NOFILE;
102163787Seric #  endif
102258689Seric # endif
102356215Seric }
102457631Seric /*
102557631Seric **  UNAME -- get the UUCP name of this system.
102657631Seric */
102757631Seric 
102857943Seric #ifndef HASUNAME
102957631Seric 
103057631Seric int
103157631Seric uname(name)
103257631Seric 	struct utsname *name;
103357631Seric {
103457631Seric 	FILE *file;
103557631Seric 	char *n;
103657631Seric 
103757631Seric 	name->nodename[0] = '\0';
103857631Seric 
103957661Seric 	/* try /etc/whoami -- one line with the node name */
104057631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
104157631Seric 	{
104257661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
104357631Seric 		(void) fclose(file);
104457661Seric 		n = strchr(name->nodename, '\n');
104557631Seric 		if (n != NULL)
104657631Seric 			*n = '\0';
104757631Seric 		if (name->nodename[0] != '\0')
104857631Seric 			return (0);
104957631Seric 	}
105057631Seric 
105157661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
105257631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
105357631Seric 	{
105457631Seric 		char buf[MAXLINE];
105557631Seric 
105657631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
105757631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
105857631Seric 					NODE_LENGTH, name->nodename) > 0)
105957631Seric 				break;
106057631Seric 		(void) fclose(file);
106157631Seric 		if (name->nodename[0] != '\0')
106257631Seric 			return (0);
106357631Seric 	}
106457631Seric 
106557631Seric #ifdef TRUST_POPEN
106657631Seric 	/*
106757631Seric 	**  Popen is known to have security holes.
106857631Seric 	*/
106957631Seric 
107057661Seric 	/* try uuname -l to return local name */
107157631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
107257631Seric 	{
107357661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
107457631Seric 		(void) pclose(file);
107557661Seric 		n = strchr(name, '\n');
107657631Seric 		if (n != NULL)
107757631Seric 			*n = '\0';
107857661Seric 		if (name->nodename[0] != '\0')
107957631Seric 			return (0);
108057631Seric 	}
108157631Seric #endif
108257631Seric 
108357631Seric 	return (-1);
108457631Seric }
108557943Seric #endif /* HASUNAME */
108658068Seric /*
108758068Seric **  INITGROUPS -- initialize groups
108858068Seric **
108958068Seric **	Stub implementation for System V style systems
109058068Seric */
109158068Seric 
109258068Seric #ifndef HASINITGROUPS
109358068Seric 
109458068Seric initgroups(name, basegid)
109558068Seric 	char *name;
109658068Seric 	int basegid;
109758068Seric {
109858068Seric 	return 0;
109958068Seric }
110058068Seric 
110158068Seric #endif
110258082Seric /*
110359289Seric **  SETSID -- set session id (for non-POSIX systems)
110459289Seric */
110559289Seric 
110659289Seric #ifndef HASSETSID
110759289Seric 
110863753Seric pid_t
110963753Seric setsid __P ((void))
111059289Seric {
111164155Seric #ifdef TIOCNOTTY
111264155Seric 	int fd;
111364155Seric 
111464155Seric 	fd = open("/dev/tty", 2);
111564155Seric 	if (fd >= 0)
111664155Seric 	{
111764155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
111864155Seric 		(void) close(fd);
111964155Seric 	}
112064155Seric #endif /* TIOCNOTTY */
112159289Seric # ifdef SYSTEM5
112263753Seric 	return setpgrp();
112363753Seric # else
112464155Seric 	return setpgid(0, getpid());
112559289Seric # endif
112659289Seric }
112759289Seric 
112859289Seric #endif
112959289Seric /*
113063969Seric **  GETOPT -- for old systems or systems with bogus implementations
113163969Seric */
113263969Seric 
113363969Seric #ifdef NEEDGETOPT
113463969Seric 
113563969Seric /*
113663969Seric  * Copyright (c) 1985 Regents of the University of California.
113763969Seric  * All rights reserved.  The Berkeley software License Agreement
113863969Seric  * specifies the terms and conditions for redistribution.
113963969Seric  */
114063969Seric 
114163969Seric 
114263969Seric /*
114363969Seric ** this version hacked to add `atend' flag to allow state machine
114463969Seric ** to reset if invoked by the program to scan args for a 2nd time
114563969Seric */
114663969Seric 
114763969Seric #if defined(LIBC_SCCS) && !defined(lint)
114863969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
114964072Seric #endif /* LIBC_SCCS and not lint */
115063969Seric 
115163969Seric #include <stdio.h>
115263969Seric 
115363969Seric /*
115463969Seric  * get option letter from argument vector
115563969Seric  */
115663969Seric int	opterr = 1,		/* if error message should be printed */
115763969Seric 	optind = 1,		/* index into parent argv vector */
115863969Seric 	optopt;			/* character checked for validity */
115963969Seric char	*optarg;		/* argument associated with option */
116063969Seric 
116163969Seric #define BADCH	(int)'?'
116263969Seric #define EMSG	""
116363969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
116463969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
116563969Seric 
116663969Seric getopt(nargc,nargv,ostr)
116763969Seric int	nargc;
116863969Seric char	**nargv,
116963969Seric 	*ostr;
117063969Seric {
117163969Seric 	static char	*place = EMSG;	/* option letter processing */
117263969Seric 	static char	atend = 0;
117363969Seric 	register char	*oli;		/* option letter list index */
117463969Seric 
117563969Seric 	if (atend) {
117663969Seric 		atend = 0;
117763969Seric 		place = EMSG;
117863969Seric 	}
117963969Seric 	if(!*place) {			/* update scanning pointer */
118064136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
118163969Seric 			atend++;
118263969Seric 			return(EOF);
118363969Seric 		}
118463969Seric 		if (*place == '-') {	/* found "--" */
118563969Seric 			++optind;
118663969Seric 			atend++;
118763969Seric 			return(EOF);
118863969Seric 		}
118963969Seric 	}				/* option letter okay? */
119064125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
119164136Seric 		if (!*place) ++optind;
119263969Seric 		tell(": illegal option -- ");
119363969Seric 	}
119463969Seric 	if (*++oli != ':') {		/* don't need argument */
119563969Seric 		optarg = NULL;
119663969Seric 		if (!*place) ++optind;
119763969Seric 	}
119863969Seric 	else {				/* need an argument */
119963969Seric 		if (*place) optarg = place;	/* no white space */
120063969Seric 		else if (nargc <= ++optind) {	/* no arg */
120163969Seric 			place = EMSG;
120263969Seric 			tell(": option requires an argument -- ");
120363969Seric 		}
120463969Seric 	 	else optarg = nargv[optind];	/* white space */
120563969Seric 		place = EMSG;
120663969Seric 		++optind;
120763969Seric 	}
120863969Seric 	return(optopt);			/* dump back option letter */
120963969Seric }
121063969Seric 
121163969Seric #endif
121263969Seric /*
121363969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
121463969Seric */
121563969Seric 
121663969Seric #ifdef NEEDVPRINTF
121763969Seric 
121863969Seric #define MAXARG	16
121963969Seric 
122063969Seric vfprintf(fp, fmt, ap)
122163969Seric 	FILE *	fp;
122263969Seric 	char *	fmt;
122363969Seric 	char **	ap;
122463969Seric {
122563969Seric 	char *	bp[MAXARG];
122663969Seric 	int	i = 0;
122763969Seric 
122863969Seric 	while (*ap && i < MAXARG)
122963969Seric 		bp[i++] = *ap++;
123063969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
123163969Seric 			 bp[4], bp[5], bp[6], bp[7],
123263969Seric 			 bp[8], bp[9], bp[10], bp[11],
123363969Seric 			 bp[12], bp[13], bp[14], bp[15]);
123463969Seric }
123563969Seric 
123663969Seric vsprintf(s, fmt, ap)
123763969Seric 	char *	s;
123863969Seric 	char *	fmt;
123963969Seric 	char **	ap;
124063969Seric {
124163969Seric 	char *	bp[MAXARG];
124263969Seric 	int	i = 0;
124363969Seric 
124463969Seric 	while (*ap && i < MAXARG)
124563969Seric 		bp[i++] = *ap++;
124663969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
124763969Seric 			bp[4], bp[5], bp[6], bp[7],
124863969Seric 			bp[8], bp[9], bp[10], bp[11],
124963969Seric 			bp[12], bp[13], bp[14], bp[15]);
125063969Seric }
125163969Seric 
125263969Seric #endif
125363969Seric /*
125464311Seric **  FREESPACE -- see how much free space is on the queue filesystem
125558082Seric **
125658082Seric **	Only implemented if you have statfs.
125758082Seric **
125858082Seric **	Parameters:
125964311Seric **		dir -- the directory in question.
126064311Seric **		bsize -- a variable into which the filesystem
126164311Seric **			block size is stored.
126258082Seric **
126358082Seric **	Returns:
126464311Seric **		The number of bytes free on the queue filesystem.
126564311Seric **		-1 if the statfs call fails.
126664311Seric **
126764311Seric **	Side effects:
126864311Seric **		Puts the filesystem block size into bsize.
126958082Seric */
127058082Seric 
127158082Seric #ifdef HASSTATFS
127258157Seric # undef HASUSTAT
127358157Seric #endif
127458157Seric 
127558157Seric #if defined(HASUSTAT)
127658157Seric # include <ustat.h>
127758157Seric #endif
127858157Seric 
127958157Seric #ifdef HASSTATFS
128064314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_)
128158133Seric #  include <sys/statfs.h>
128258133Seric # else
1283*64380Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
128458133Seric #   include <sys/vfs.h>
128558133Seric #  else
128658157Seric #   include <sys/mount.h>
128758133Seric #  endif
128858133Seric # endif
128958082Seric #endif
129058082Seric 
129164311Seric long
129264311Seric freespace(dir, bsize)
129364311Seric 	char *dir;
129464311Seric 	long *bsize;
129558082Seric {
129658160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
129758157Seric # if defined(HASUSTAT)
129858153Seric 	struct ustat fs;
129958153Seric 	struct stat statbuf;
130058366Seric #  define FSBLOCKSIZE	DEV_BSIZE
130158157Seric #  define f_bavail	f_tfree
130258157Seric # else
130358157Seric #  if defined(ultrix)
130458157Seric 	struct fs_data fs;
130558157Seric #   define f_bavail	fd_bfreen
130658366Seric #   define FSBLOCKSIZE	fs.fd_bsize
130758153Seric #  else
130858082Seric 	struct statfs fs;
130958366Seric #   define FSBLOCKSIZE	fs.f_bsize
131064336Seric #   if defined(_SCO_unix_) || defined(IRIX)
131163987Seric #    define f_bavail f_bfree
131263987Seric #   endif
131358153Seric #  endif
131458133Seric # endif
131558082Seric 	extern int errno;
131658082Seric 
131758157Seric # if defined(HASUSTAT)
131864311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
131958157Seric # else
132064314Seric #  if defined(IRIX) || defined(apollo)
132164311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
132258157Seric #  else
132358157Seric #   if defined(ultrix)
132464311Seric 	if (statfs(dir, &fs) > 0)
132558153Seric #   else
132664311Seric 	if (statfs(dir, &fs) == 0)
132758153Seric #   endif
132858133Seric #  endif
132958133Seric # endif
133058133Seric 	{
133164311Seric 		if (bsize != NULL)
133264311Seric 			*bsize = FSBLOCKSIZE;
133364311Seric 		return (fs.f_bavail);
133464311Seric 	}
133564337Seric #endif
133664311Seric 	return (-1);
133764311Seric }
133864311Seric /*
133964311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
134064311Seric **
134164311Seric **	Only implemented if you have statfs.
134264311Seric **
134364311Seric **	Parameters:
134464311Seric **		msize -- the size to check against.  If zero, we don't yet
134564311Seric **		know how big the message will be, so just check for
134664311Seric **		a "reasonable" amount.
134764311Seric **
134864311Seric **	Returns:
134964311Seric **		TRUE if there is enough space.
135064311Seric **		FALSE otherwise.
135164311Seric */
135264311Seric 
135364311Seric bool
135464311Seric enoughspace(msize)
135564311Seric 	long msize;
135664311Seric {
135764311Seric 	long bfree, bsize;
135864311Seric 
135964311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
136064311Seric 	{
136158133Seric 		if (tTd(4, 80))
136264311Seric 			printf("enoughspace: no threshold\n");
136364311Seric 		return TRUE;
136464311Seric 	}
136564311Seric 
136664311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
136764311Seric 	{
136864311Seric 		if (tTd(4, 80))
136958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
137064311Seric 				bfree, msize);
137158333Seric 
137258333Seric 		/* convert msize to block count */
137364311Seric 		msize = msize / bsize + 1;
137458333Seric 		if (MinBlocksFree >= 0)
137558333Seric 			msize += MinBlocksFree;
137658333Seric 
137764311Seric 		if (bfree < msize)
137858090Seric 		{
137958090Seric #ifdef LOG
138058090Seric 			if (LogLevel > 0)
138164254Seric 				syslog(LOG_ALERT,
138264254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
138364311Seric 					CurEnv->e_id, bfree,
138464254Seric 					CurHostName, msize, QueueDir);
138558090Seric #endif
138658082Seric 			return FALSE;
138758090Seric 		}
138858082Seric 	}
138958082Seric 	else if (tTd(4, 80))
139058333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
139158333Seric 			MinBlocksFree, msize, errstring(errno));
139258082Seric 	return TRUE;
139358082Seric }
139458542Seric /*
139558542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
139658542Seric **
139758542Seric **	This looks at an errno value and tells if this is likely to
139858542Seric **	go away if retried later.
139958542Seric **
140058542Seric **	Parameters:
140158542Seric **		err -- the errno code to classify.
140258542Seric **
140358542Seric **	Returns:
140458542Seric **		TRUE if this is probably transient.
140558542Seric **		FALSE otherwise.
140658542Seric */
140758542Seric 
140858542Seric bool
140958542Seric transienterror(err)
141058542Seric 	int err;
141158542Seric {
141258542Seric 	switch (err)
141358542Seric 	{
141458542Seric 	  case EIO:			/* I/O error */
141558542Seric 	  case ENXIO:			/* Device not configured */
141658542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
141758542Seric 	  case ENOMEM:			/* Cannot allocate memory */
141858542Seric 	  case ENODEV:			/* Operation not supported by device */
141958542Seric 	  case ENFILE:			/* Too many open files in system */
142058542Seric 	  case EMFILE:			/* Too many open files */
142158542Seric 	  case ENOSPC:			/* No space left on device */
142258542Seric #ifdef ETIMEDOUT
142358542Seric 	  case ETIMEDOUT:		/* Connection timed out */
142458542Seric #endif
142558542Seric #ifdef ESTALE
142658542Seric 	  case ESTALE:			/* Stale NFS file handle */
142758542Seric #endif
142858542Seric #ifdef ENETDOWN
142958542Seric 	  case ENETDOWN:		/* Network is down */
143058542Seric #endif
143158542Seric #ifdef ENETUNREACH
143258542Seric 	  case ENETUNREACH:		/* Network is unreachable */
143358542Seric #endif
143458542Seric #ifdef ENETRESET
143558542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
143658542Seric #endif
143758542Seric #ifdef ECONNABORTED
143858542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
143958542Seric #endif
144058542Seric #ifdef ECONNRESET
144158542Seric 	  case ECONNRESET:		/* Connection reset by peer */
144258542Seric #endif
144358542Seric #ifdef ENOBUFS
144458542Seric 	  case ENOBUFS:			/* No buffer space available */
144558542Seric #endif
144658542Seric #ifdef ESHUTDOWN
144758542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
144858542Seric #endif
144958542Seric #ifdef ECONNREFUSED
145058542Seric 	  case ECONNREFUSED:		/* Connection refused */
145158542Seric #endif
145258542Seric #ifdef EHOSTDOWN
145358542Seric 	  case EHOSTDOWN:		/* Host is down */
145458542Seric #endif
145558542Seric #ifdef EHOSTUNREACH
145658542Seric 	  case EHOSTUNREACH:		/* No route to host */
145758542Seric #endif
145858542Seric #ifdef EDQUOT
145958542Seric 	  case EDQUOT:			/* Disc quota exceeded */
146058542Seric #endif
146158542Seric #ifdef EPROCLIM
146258542Seric 	  case EPROCLIM:		/* Too many processes */
146358542Seric #endif
146458542Seric #ifdef EUSERS
146558542Seric 	  case EUSERS:			/* Too many users */
146658542Seric #endif
146758542Seric #ifdef EDEADLK
146858542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
146958542Seric #endif
147058542Seric #ifdef EISCONN
147158542Seric 	  case EISCONN:			/* Socket already connected */
147258542Seric #endif
147358542Seric #ifdef EINPROGRESS
147458542Seric 	  case EINPROGRESS:		/* Operation now in progress */
147558542Seric #endif
147658542Seric #ifdef EALREADY
147758542Seric 	  case EALREADY:		/* Operation already in progress */
147858542Seric #endif
147958542Seric #ifdef EADDRINUSE
148058542Seric 	  case EADDRINUSE:		/* Address already in use */
148158542Seric #endif
148258542Seric #ifdef EADDRNOTAVAIL
148358542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
148458542Seric #endif
148563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
148658542Seric 	  case ENOSR:			/* Out of streams resources */
148758542Seric #endif
148858542Seric 		return TRUE;
148958542Seric 	}
149058542Seric 
149158542Seric 	/* nope, must be permanent */
149258542Seric 	return FALSE;
149358542Seric }
149458689Seric /*
149564035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
149658689Seric **
149758689Seric **	Parameters:
149858689Seric **		fd -- the file descriptor of the file.
149958689Seric **		filename -- the file name (for error messages).
150064335Seric **		ext -- the filename extension.
150158689Seric **		type -- type of the lock.  Bits can be:
150258689Seric **			LOCK_EX -- exclusive lock.
150358689Seric **			LOCK_NB -- non-blocking.
150458689Seric **
150558689Seric **	Returns:
150658689Seric **		TRUE if the lock was acquired.
150758689Seric **		FALSE otherwise.
150858689Seric */
150958689Seric 
151058689Seric bool
151164335Seric lockfile(fd, filename, ext, type)
151258689Seric 	int fd;
151358689Seric 	char *filename;
151464335Seric 	char *ext;
151558689Seric 	int type;
151658689Seric {
151764035Seric # ifndef HASFLOCK
151858689Seric 	int action;
151958689Seric 	struct flock lfd;
152064335Seric 
152164335Seric 	if (ext == NULL)
152264335Seric 		ext = "";
152364035Seric 
152463983Seric 	bzero(&lfd, sizeof lfd);
152559447Seric 	if (bitset(LOCK_UN, type))
152659447Seric 		lfd.l_type = F_UNLCK;
152759447Seric 	else if (bitset(LOCK_EX, type))
152858689Seric 		lfd.l_type = F_WRLCK;
152958689Seric 	else
153058689Seric 		lfd.l_type = F_RDLCK;
153158689Seric 
153258689Seric 	if (bitset(LOCK_NB, type))
153358689Seric 		action = F_SETLK;
153458689Seric 	else
153558689Seric 		action = F_SETLKW;
153658689Seric 
153764264Seric 	if (tTd(55, 60))
153864335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
153964335Seric 			filename, ext, action, lfd.l_type);
154064264Seric 
154158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
154264264Seric 	{
154364264Seric 		if (tTd(55, 60))
154464264Seric 			printf("SUCCESS\n");
154558689Seric 		return TRUE;
154664264Seric 	}
154758689Seric 
154864264Seric 	if (tTd(55, 60))
154964264Seric 		printf("(%s) ", errstring(errno));
155064264Seric 
155164136Seric 	/*
155264136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
155364136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
155464136Seric 	**  as type "tmp" (that is, served from swap space), the
155564136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
155664136Seric 	**  Since this is fairly common during testing, we will assume
155764136Seric 	**  that this indicates that the lock is successfully grabbed.
155864136Seric 	*/
155964136Seric 
156064136Seric 	if (errno == EINVAL)
156164264Seric 	{
156264264Seric 		if (tTd(55, 60))
156364264Seric 			printf("SUCCESS\n");
156464136Seric 		return TRUE;
156564264Seric 	}
156664136Seric 
156758689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
156864378Seric 	{
156964378Seric 		int omode = -1;
157064378Seric #  ifdef F_GETFL
157164378Seric 		int oerrno = errno;
157264378Seric 
157364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
157464378Seric 		errno = oerrno;
157564378Seric #  endif
157664378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
157764378Seric 			filename, ext, fd, type, omode, geteuid());
157864378Seric 	}
157958689Seric # else
158064335Seric 	if (ext == NULL)
158164335Seric 		ext = "";
158264335Seric 
158364264Seric 	if (tTd(55, 60))
158464335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
158564264Seric 
158658689Seric 	if (flock(fd, type) >= 0)
158764264Seric 	{
158864264Seric 		if (tTd(55, 60))
158964264Seric 			printf("SUCCESS\n");
159058689Seric 		return TRUE;
159164264Seric 	}
159258689Seric 
159364264Seric 	if (tTd(55, 60))
159464264Seric 		printf("(%s) ", errstring(errno));
159564264Seric 
159658689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
159764378Seric 	{
159864378Seric 		int omode = -1;
159964378Seric #  ifdef F_GETFL
160064378Seric 		int oerrno = errno;
160164378Seric 
160264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
160364378Seric 		errno = oerrno;
160464378Seric #  endif
160564378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
160664378Seric 			filename, ext, fd, type, omode, geteuid());
160758689Seric # endif
160864378Seric 	}
160964264Seric 	if (tTd(55, 60))
161064264Seric 		printf("FAIL\n");
161158689Seric 	return FALSE;
161258689Seric }
1613