xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64336)
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*64336Seric static char sccsid[] = "@(#)conf.c	8.27 (Berkeley) 08/23/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 {
49464072Seric #if defined(SYS5SIGNALS) || defined(BSD4_3)
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 */
55751920Seric #define LA_INT		2	/* read kmem for avenrun; interpret as int */
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) */
56114872Seric 
56263962Seric /* do guesses based on general OS type */
56351920Seric #ifndef LA_TYPE
56463962Seric # define LA_TYPE	LA_ZERO
56551920Seric #endif
56651920Seric 
56751920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
56851920Seric 
56914872Seric #include <nlist.h>
57014872Seric 
57151920Seric #ifndef LA_AVENRUN
57263962Seric # ifdef SYSTEM5
57363962Seric #  define LA_AVENRUN	"avenrun"
57463962Seric # else
57563962Seric #  define LA_AVENRUN	"_avenrun"
57663962Seric # endif
57751920Seric #endif
57851920Seric 
57951920Seric /* _PATH_UNIX should be defined in <paths.h> */
58051920Seric #ifndef _PATH_UNIX
58163962Seric # if defined(SYSTEM5)
58263962Seric #  define _PATH_UNIX	"/unix"
58363962Seric # else
58463962Seric #  define _PATH_UNIX	"/vmunix"
58563962Seric # endif
58651920Seric #endif
58751920Seric 
58814872Seric struct	nlist Nl[] =
58914872Seric {
59051920Seric 	{ LA_AVENRUN },
59114872Seric #define	X_AVENRUN	0
59214872Seric 	{ 0 },
59314872Seric };
59414872Seric 
59560583Seric #ifndef FSHIFT
59660583Seric # if defined(unixpc)
59760583Seric #  define FSHIFT	5
59860583Seric # endif
59957736Seric 
60060583Seric # if defined(__alpha)
60160583Seric #  define FSHIFT	10
60260583Seric # endif
60357977Seric 
60460583Seric # if (LA_TYPE == LA_INT)
60551920Seric #  define FSHIFT	8
60660583Seric # endif
60757736Seric #endif
60860583Seric 
60957736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE)
61051920Seric #  define FSCALE	(1 << FSHIFT)
61151920Seric #endif
61240930Srick 
61314872Seric getla()
61414872Seric {
61514872Seric 	static int kmem = -1;
61651920Seric #if LA_TYPE == LA_INT
61724943Seric 	long avenrun[3];
61851920Seric #else
61951920Seric 	double avenrun[3];
62051920Seric #endif
62125615Seric 	extern off_t lseek();
62257736Seric 	extern int errno;
62314872Seric 
62414872Seric 	if (kmem < 0)
62514872Seric 	{
62624945Seric 		kmem = open("/dev/kmem", 0, 0);
62714872Seric 		if (kmem < 0)
62857736Seric 		{
62957736Seric 			if (tTd(3, 1))
63057736Seric 				printf("getla: open(/dev/kmem): %s\n",
63157736Seric 					errstring(errno));
63214872Seric 			return (-1);
63357736Seric 		}
63451920Seric 		(void) fcntl(kmem, F_SETFD, 1);
63557736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
63657736Seric 		{
63757736Seric 			if (tTd(3, 1))
63857736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
63957736Seric 					errstring(errno));
64014872Seric 			return (-1);
64157736Seric 		}
64259253Seric 		if (Nl[X_AVENRUN].n_value == 0)
64359253Seric 		{
64459253Seric 			if (tTd(3, 1))
64559253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
64659253Seric 					_PATH_UNIX, LA_AVENRUN);
64759253Seric 			return (-1);
64859253Seric 		}
64914872Seric 	}
65057736Seric 	if (tTd(3, 20))
65157736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
65224945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
65323118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
65419967Seric 	{
65519967Seric 		/* thank you Ian */
65657736Seric 		if (tTd(3, 1))
65757736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
65819967Seric 		return (-1);
65919967Seric 	}
66051920Seric #if LA_TYPE == LA_INT
66157736Seric 	if (tTd(3, 5))
66257736Seric 	{
66357736Seric 		printf("getla: avenrun = %d", avenrun[0]);
66457736Seric 		if (tTd(3, 15))
66557736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
66657736Seric 		printf("\n");
66757736Seric 	}
66857736Seric 	if (tTd(3, 1))
66957736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
67024943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
67151920Seric #else
67257736Seric 	if (tTd(3, 5))
67357736Seric 	{
67457736Seric 		printf("getla: avenrun = %g", avenrun[0]);
67557736Seric 		if (tTd(3, 15))
67657736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
67757736Seric 		printf("\n");
67857736Seric 	}
67957736Seric 	if (tTd(3, 1))
68057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
68151920Seric 	return ((int) (avenrun[0] + 0.5));
68251920Seric #endif
68314872Seric }
68414872Seric 
68551773Seric #else
68651920Seric #if LA_TYPE == LA_SUBR
68751773Seric 
68851773Seric getla()
68951773Seric {
69051920Seric 	double avenrun[3];
69151920Seric 
69251920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
69357736Seric 	{
69457736Seric 		if (tTd(3, 1))
69557736Seric 			perror("getla: getloadavg failed:");
69651920Seric 		return (-1);
69757736Seric 	}
69857736Seric 	if (tTd(3, 1))
69957736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
70051920Seric 	return ((int) (avenrun[0] + 0.5));
70151773Seric }
70251773Seric 
70351773Seric #else
70464295Seric #if LA_TYPE == LA_MACH
70551773Seric 
70664295Seric /*
70764295Seric **  This has been tested on NeXT release 2.1.
70864295Seric */
70964295Seric 
71064295Seric #include <mach.h>
71164295Seric 
71251773Seric getla()
71351773Seric {
71464295Seric 	processor_set_t default_set;
71564295Seric 	kern_return_t error;
71664295Seric 	unsigned int info_count;
71764295Seric 	struct processor_set_basic_info info;
71864295Seric 	host_t host;
71964295Seric 
72064295Seric 	error = processor_set_default(host_self(), &default_set);
72164295Seric 	if (error != KERN_SUCCESS)
72264295Seric 		return -1;
72364295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
72464295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
72564295Seric 			       &host, (processor_set_info_t)&info,
72664295Seric 			       &info_count) != KERN_SUCCESS)
72764295Seric 	{
72864295Seric 		return -1;
72964295Seric 	}
73064295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
73164295Seric }
73264295Seric 
73364295Seric 
73464295Seric #else
73564295Seric 
73664295Seric getla()
73764295Seric {
73857736Seric 	if (tTd(3, 1))
73957736Seric 		printf("getla: ZERO\n");
74051920Seric 	return (0);
74151773Seric }
74251773Seric 
74351773Seric #endif
74451773Seric #endif
74564295Seric #endif
74624943Seric /*
74724943Seric **  SHOULDQUEUE -- should this message be queued or sent?
74824943Seric **
74924943Seric **	Compares the message cost to the load average to decide.
75024943Seric **
75124943Seric **	Parameters:
75224943Seric **		pri -- the priority of the message in question.
75357438Seric **		ctime -- the message creation time.
75424943Seric **
75524943Seric **	Returns:
75624943Seric **		TRUE -- if this message should be queued up for the
75724943Seric **			time being.
75824943Seric **		FALSE -- if the load is low enough to send this message.
75924943Seric **
76024943Seric **	Side Effects:
76124943Seric **		none.
76224943Seric */
76324943Seric 
76424943Seric bool
76557438Seric shouldqueue(pri, ctime)
76624943Seric 	long pri;
76757438Seric 	time_t ctime;
76824943Seric {
76951920Seric 	if (CurrentLA < QueueLA)
77024943Seric 		return (FALSE);
77158132Seric 	if (CurrentLA >= RefuseLA)
77258132Seric 		return (TRUE);
77351920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
77424943Seric }
77524943Seric /*
77653037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
77753037Seric **
77853037Seric **	Parameters:
77953037Seric **		none.
78053037Seric **
78153037Seric **	Returns:
78253037Seric **		TRUE if incoming SMTP connections should be refused
78353037Seric **			(for now).
78453037Seric **		FALSE if we should accept new work.
78553037Seric **
78653037Seric **	Side Effects:
78753037Seric **		none.
78853037Seric */
78953037Seric 
79053037Seric bool
79153037Seric refuseconnections()
79253037Seric {
79359156Seric #ifdef XLA
79459156Seric 	if (!xla_smtp_ok())
79559156Seric 		return TRUE;
79659156Seric #endif
79759156Seric 
79853037Seric 	/* this is probably too simplistic */
79958132Seric 	return (CurrentLA >= RefuseLA);
80053037Seric }
80153037Seric /*
80224943Seric **  SETPROCTITLE -- set process title for ps
80324943Seric **
80424943Seric **	Parameters:
80558674Seric **		fmt -- a printf style format string.
80658674Seric **		a, b, c -- possible parameters to fmt.
80724943Seric **
80824943Seric **	Returns:
80924943Seric **		none.
81024943Seric **
81124943Seric **	Side Effects:
81224943Seric **		Clobbers argv of our main procedure so ps(1) will
81324943Seric **		display the title.
81424943Seric */
81524943Seric 
81658689Seric #ifdef SETPROCTITLE
81758689Seric # ifdef __hpux
81858689Seric #  include <sys/pstat.h>
81958689Seric # endif
82059732Seric # ifdef BSD4_4
82159732Seric #  include <machine/vmparam.h>
82259732Seric #  include <sys/exec.h>
82364247Seric #  ifdef __bsdi__
82464247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
82564247Seric #  endif
82659992Seric #  ifdef PS_STRINGS
82759992Seric #   define SETPROC_STATIC static
82859992Seric #  endif
82959732Seric # endif
83059732Seric # ifndef SETPROC_STATIC
83159732Seric #  define SETPROC_STATIC
83259732Seric # endif
83358689Seric #endif
83458689Seric 
83524943Seric /*VARARGS1*/
83657642Seric #ifdef __STDC__
83757642Seric setproctitle(char *fmt, ...)
83857642Seric #else
83957642Seric setproctitle(fmt, va_alist)
84024943Seric 	char *fmt;
84157642Seric 	va_dcl
84257642Seric #endif
84324943Seric {
84424943Seric # ifdef SETPROCTITLE
84524943Seric 	register char *p;
84625049Seric 	register int i;
84759732Seric 	SETPROC_STATIC char buf[MAXLINE];
84856852Seric 	VA_LOCAL_DECL
84958689Seric #  ifdef __hpux
85058689Seric 	union pstun pst;
85158689Seric #  endif
85224943Seric 	extern char **Argv;
85324943Seric 	extern char *LastArgv;
85424943Seric 
85558674Seric 	p = buf;
85624943Seric 
85758674Seric 	/* print sendmail: heading for grep */
85858674Seric 	(void) strcpy(p, "sendmail: ");
85958674Seric 	p += strlen(p);
86024943Seric 
86158674Seric 	/* print the argument string */
86258674Seric 	VA_START(fmt);
86358674Seric 	(void) vsprintf(p, fmt, ap);
86456852Seric 	VA_END;
86554996Seric 
86658674Seric 	i = strlen(buf);
86758689Seric 
86858689Seric #  ifdef __hpux
86958689Seric 	pst.pst_command = buf;
87058689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
87158689Seric #  else
87259992Seric #   ifdef PS_STRINGS
87359732Seric 	PS_STRINGS->ps_nargvstr = 1;
87459732Seric 	PS_STRINGS->ps_argvstr = buf;
87559732Seric #   else
87654996Seric 	if (i > LastArgv - Argv[0] - 2)
87725049Seric 	{
87854996Seric 		i = LastArgv - Argv[0] - 2;
87958674Seric 		buf[i] = '\0';
88025049Seric 	}
88158674Seric 	(void) strcpy(Argv[0], buf);
88254997Seric 	p = &Argv[0][i];
88324943Seric 	while (p < LastArgv)
88424943Seric 		*p++ = ' ';
88559732Seric #   endif
88658689Seric #  endif
88756795Seric # endif /* SETPROCTITLE */
88824943Seric }
88925698Seric /*
89025698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
89125698Seric **
89225698Seric **	Parameters:
89325698Seric **		none.
89425698Seric **
89525698Seric **	Returns:
89625698Seric **		none.
89725698Seric **
89825698Seric **	Side Effects:
89925698Seric **		Picks up extant zombies.
90025698Seric */
90125698Seric 
90225698Seric # include <sys/wait.h>
90325698Seric 
90446928Sbostic void
90525698Seric reapchild()
90625698Seric {
90763966Seric # ifdef HASWAITPID
90860219Seric 	auto int status;
90960560Seric 	int count;
91060560Seric 	int pid;
91160219Seric 
91260560Seric 	count = 0;
91360560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
91460560Seric 	{
91560560Seric 		if (count++ > 1000)
91660560Seric 		{
91760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
91860560Seric 				pid, status);
91960560Seric 			break;
92060560Seric 		}
92160560Seric 	}
92260219Seric # else
92325698Seric # ifdef WNOHANG
92425698Seric 	union wait status;
92525698Seric 
92663753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
92725698Seric 		continue;
92856795Seric # else /* WNOHANG */
92925698Seric 	auto int status;
93025698Seric 
93160219Seric 	while (wait(&status) > 0)
93225698Seric 		continue;
93356795Seric # endif /* WNOHANG */
93460219Seric # endif
93564035Seric # ifdef SYS5SIGNALS
93664035Seric 	(void) setsignal(SIGCHLD, reapchild);
93758061Seric # endif
93825698Seric }
93955418Seric /*
94055418Seric **  UNSETENV -- remove a variable from the environment
94155418Seric **
94255418Seric **	Not needed on newer systems.
94355418Seric **
94455418Seric **	Parameters:
94555418Seric **		name -- the string name of the environment variable to be
94655418Seric **			deleted from the current environment.
94755418Seric **
94855418Seric **	Returns:
94955418Seric **		none.
95055418Seric **
95155418Seric **	Globals:
95255418Seric **		environ -- a pointer to the current environment.
95355418Seric **
95455418Seric **	Side Effects:
95555418Seric **		Modifies environ.
95655418Seric */
95755418Seric 
95863962Seric #ifndef HASUNSETENV
95955418Seric 
96055418Seric void
96155418Seric unsetenv(name)
96255418Seric 	char *name;
96355418Seric {
96455418Seric 	extern char **environ;
96555418Seric 	register char **pp;
96655418Seric 	int len = strlen(name);
96755418Seric 
96855418Seric 	for (pp = environ; *pp != NULL; pp++)
96955418Seric 	{
97055418Seric 		if (strncmp(name, *pp, len) == 0 &&
97155418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
97255418Seric 			break;
97355418Seric 	}
97455418Seric 
97555418Seric 	for (; *pp != NULL; pp++)
97655418Seric 		*pp = pp[1];
97755418Seric }
97855418Seric 
97963962Seric #endif
98056215Seric /*
98156215Seric **  GETDTABLESIZE -- return number of file descriptors
98256215Seric **
98356215Seric **	Only on non-BSD systems
98456215Seric **
98556215Seric **	Parameters:
98656215Seric **		none
98756215Seric **
98856215Seric **	Returns:
98956215Seric **		size of file descriptor table
99056215Seric **
99156215Seric **	Side Effects:
99256215Seric **		none
99356215Seric */
99456215Seric 
99563787Seric #ifdef SOLARIS
99663787Seric # include <sys/resource.h>
99763787Seric #endif
99856215Seric 
99956215Seric int
100063787Seric getdtsize()
100156215Seric {
100263787Seric #ifdef RLIMIT_NOFILE
100363787Seric 	struct rlimit rl;
100463787Seric 
100563787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
100663787Seric 		return rl.rlim_cur;
100763787Seric #endif
100863787Seric 
100964031Seric # ifdef HASGETDTABLESIZE
101064031Seric 	return getdtablesize();
101164031Seric # else
101264031Seric #  ifdef _SC_OPEN_MAX
101358689Seric 	return sysconf(_SC_OPEN_MAX);
101463787Seric #  else
101556215Seric 	return NOFILE;
101663787Seric #  endif
101758689Seric # endif
101856215Seric }
101957631Seric /*
102057631Seric **  UNAME -- get the UUCP name of this system.
102157631Seric */
102257631Seric 
102357943Seric #ifndef HASUNAME
102457631Seric 
102557631Seric int
102657631Seric uname(name)
102757631Seric 	struct utsname *name;
102857631Seric {
102957631Seric 	FILE *file;
103057631Seric 	char *n;
103157631Seric 
103257631Seric 	name->nodename[0] = '\0';
103357631Seric 
103457661Seric 	/* try /etc/whoami -- one line with the node name */
103557631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
103657631Seric 	{
103757661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
103857631Seric 		(void) fclose(file);
103957661Seric 		n = strchr(name->nodename, '\n');
104057631Seric 		if (n != NULL)
104157631Seric 			*n = '\0';
104257631Seric 		if (name->nodename[0] != '\0')
104357631Seric 			return (0);
104457631Seric 	}
104557631Seric 
104657661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
104757631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
104857631Seric 	{
104957631Seric 		char buf[MAXLINE];
105057631Seric 
105157631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
105257631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
105357631Seric 					NODE_LENGTH, name->nodename) > 0)
105457631Seric 				break;
105557631Seric 		(void) fclose(file);
105657631Seric 		if (name->nodename[0] != '\0')
105757631Seric 			return (0);
105857631Seric 	}
105957631Seric 
106057631Seric #ifdef TRUST_POPEN
106157631Seric 	/*
106257631Seric 	**  Popen is known to have security holes.
106357631Seric 	*/
106457631Seric 
106557661Seric 	/* try uuname -l to return local name */
106657631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
106757631Seric 	{
106857661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
106957631Seric 		(void) pclose(file);
107057661Seric 		n = strchr(name, '\n');
107157631Seric 		if (n != NULL)
107257631Seric 			*n = '\0';
107357661Seric 		if (name->nodename[0] != '\0')
107457631Seric 			return (0);
107557631Seric 	}
107657631Seric #endif
107757631Seric 
107857631Seric 	return (-1);
107957631Seric }
108057943Seric #endif /* HASUNAME */
108158068Seric /*
108258068Seric **  INITGROUPS -- initialize groups
108358068Seric **
108458068Seric **	Stub implementation for System V style systems
108558068Seric */
108658068Seric 
108758068Seric #ifndef HASINITGROUPS
108858068Seric 
108958068Seric initgroups(name, basegid)
109058068Seric 	char *name;
109158068Seric 	int basegid;
109258068Seric {
109358068Seric 	return 0;
109458068Seric }
109558068Seric 
109658068Seric #endif
109758082Seric /*
109859289Seric **  SETSID -- set session id (for non-POSIX systems)
109959289Seric */
110059289Seric 
110159289Seric #ifndef HASSETSID
110259289Seric 
110363753Seric pid_t
110463753Seric setsid __P ((void))
110559289Seric {
110664155Seric #ifdef TIOCNOTTY
110764155Seric 	int fd;
110864155Seric 
110964155Seric 	fd = open("/dev/tty", 2);
111064155Seric 	if (fd >= 0)
111164155Seric 	{
111264155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
111364155Seric 		(void) close(fd);
111464155Seric 	}
111564155Seric #endif /* TIOCNOTTY */
111659289Seric # ifdef SYSTEM5
111763753Seric 	return setpgrp();
111863753Seric # else
111964155Seric 	return setpgid(0, getpid());
112059289Seric # endif
112159289Seric }
112259289Seric 
112359289Seric #endif
112459289Seric /*
112563969Seric **  GETOPT -- for old systems or systems with bogus implementations
112663969Seric */
112763969Seric 
112863969Seric #ifdef NEEDGETOPT
112963969Seric 
113063969Seric /*
113163969Seric  * Copyright (c) 1985 Regents of the University of California.
113263969Seric  * All rights reserved.  The Berkeley software License Agreement
113363969Seric  * specifies the terms and conditions for redistribution.
113463969Seric  */
113563969Seric 
113663969Seric 
113763969Seric /*
113863969Seric ** this version hacked to add `atend' flag to allow state machine
113963969Seric ** to reset if invoked by the program to scan args for a 2nd time
114063969Seric */
114163969Seric 
114263969Seric #if defined(LIBC_SCCS) && !defined(lint)
114363969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
114464072Seric #endif /* LIBC_SCCS and not lint */
114563969Seric 
114663969Seric #include <stdio.h>
114763969Seric 
114863969Seric /*
114963969Seric  * get option letter from argument vector
115063969Seric  */
115163969Seric int	opterr = 1,		/* if error message should be printed */
115263969Seric 	optind = 1,		/* index into parent argv vector */
115363969Seric 	optopt;			/* character checked for validity */
115463969Seric char	*optarg;		/* argument associated with option */
115563969Seric 
115663969Seric #define BADCH	(int)'?'
115763969Seric #define EMSG	""
115863969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
115963969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
116063969Seric 
116163969Seric getopt(nargc,nargv,ostr)
116263969Seric int	nargc;
116363969Seric char	**nargv,
116463969Seric 	*ostr;
116563969Seric {
116663969Seric 	static char	*place = EMSG;	/* option letter processing */
116763969Seric 	static char	atend = 0;
116863969Seric 	register char	*oli;		/* option letter list index */
116963969Seric 
117063969Seric 	if (atend) {
117163969Seric 		atend = 0;
117263969Seric 		place = EMSG;
117363969Seric 	}
117463969Seric 	if(!*place) {			/* update scanning pointer */
117564136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
117663969Seric 			atend++;
117763969Seric 			return(EOF);
117863969Seric 		}
117963969Seric 		if (*place == '-') {	/* found "--" */
118063969Seric 			++optind;
118163969Seric 			atend++;
118263969Seric 			return(EOF);
118363969Seric 		}
118463969Seric 	}				/* option letter okay? */
118564125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
118664136Seric 		if (!*place) ++optind;
118763969Seric 		tell(": illegal option -- ");
118863969Seric 	}
118963969Seric 	if (*++oli != ':') {		/* don't need argument */
119063969Seric 		optarg = NULL;
119163969Seric 		if (!*place) ++optind;
119263969Seric 	}
119363969Seric 	else {				/* need an argument */
119463969Seric 		if (*place) optarg = place;	/* no white space */
119563969Seric 		else if (nargc <= ++optind) {	/* no arg */
119663969Seric 			place = EMSG;
119763969Seric 			tell(": option requires an argument -- ");
119863969Seric 		}
119963969Seric 	 	else optarg = nargv[optind];	/* white space */
120063969Seric 		place = EMSG;
120163969Seric 		++optind;
120263969Seric 	}
120363969Seric 	return(optopt);			/* dump back option letter */
120463969Seric }
120563969Seric 
120663969Seric #endif
120763969Seric /*
120863969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
120963969Seric */
121063969Seric 
121163969Seric #ifdef NEEDVPRINTF
121263969Seric 
121363969Seric #define MAXARG	16
121463969Seric 
121563969Seric vfprintf(fp, fmt, ap)
121663969Seric 	FILE *	fp;
121763969Seric 	char *	fmt;
121863969Seric 	char **	ap;
121963969Seric {
122063969Seric 	char *	bp[MAXARG];
122163969Seric 	int	i = 0;
122263969Seric 
122363969Seric 	while (*ap && i < MAXARG)
122463969Seric 		bp[i++] = *ap++;
122563969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
122663969Seric 			 bp[4], bp[5], bp[6], bp[7],
122763969Seric 			 bp[8], bp[9], bp[10], bp[11],
122863969Seric 			 bp[12], bp[13], bp[14], bp[15]);
122963969Seric }
123063969Seric 
123163969Seric vsprintf(s, fmt, ap)
123263969Seric 	char *	s;
123363969Seric 	char *	fmt;
123463969Seric 	char **	ap;
123563969Seric {
123663969Seric 	char *	bp[MAXARG];
123763969Seric 	int	i = 0;
123863969Seric 
123963969Seric 	while (*ap && i < MAXARG)
124063969Seric 		bp[i++] = *ap++;
124163969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
124263969Seric 			bp[4], bp[5], bp[6], bp[7],
124363969Seric 			bp[8], bp[9], bp[10], bp[11],
124463969Seric 			bp[12], bp[13], bp[14], bp[15]);
124563969Seric }
124663969Seric 
124763969Seric #endif
124863969Seric /*
124964311Seric **  FREESPACE -- see how much free space is on the queue filesystem
125058082Seric **
125158082Seric **	Only implemented if you have statfs.
125258082Seric **
125358082Seric **	Parameters:
125464311Seric **		dir -- the directory in question.
125564311Seric **		bsize -- a variable into which the filesystem
125664311Seric **			block size is stored.
125758082Seric **
125858082Seric **	Returns:
125964311Seric **		The number of bytes free on the queue filesystem.
126064311Seric **		-1 if the statfs call fails.
126164311Seric **
126264311Seric **	Side effects:
126364311Seric **		Puts the filesystem block size into bsize.
126458082Seric */
126558082Seric 
126658082Seric #ifdef HASSTATFS
126758157Seric # undef HASUSTAT
126858157Seric #endif
126958157Seric 
127058157Seric #if defined(HASUSTAT)
127158157Seric # include <ustat.h>
127258157Seric #endif
127358157Seric 
127458157Seric #ifdef HASSTATFS
127564314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_)
127658133Seric #  include <sys/statfs.h>
127758133Seric # else
127864314Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT)
127958133Seric #   include <sys/vfs.h>
128058133Seric #  else
128158157Seric #   include <sys/mount.h>
128258133Seric #  endif
128358133Seric # endif
128458082Seric #endif
128558082Seric 
128664311Seric long
128764311Seric freespace(dir, bsize)
128864311Seric 	char *dir;
128964311Seric 	long *bsize;
129058082Seric {
129158160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
129258157Seric # if defined(HASUSTAT)
129358153Seric 	struct ustat fs;
129458153Seric 	struct stat statbuf;
129558366Seric #  define FSBLOCKSIZE	DEV_BSIZE
129658157Seric #  define f_bavail	f_tfree
129758157Seric # else
129858157Seric #  if defined(ultrix)
129958157Seric 	struct fs_data fs;
130058157Seric #   define f_bavail	fd_bfreen
130158366Seric #   define FSBLOCKSIZE	fs.fd_bsize
130258153Seric #  else
130358082Seric 	struct statfs fs;
130458366Seric #   define FSBLOCKSIZE	fs.f_bsize
1305*64336Seric #   if defined(_SCO_unix_) || defined(IRIX)
130663987Seric #    define f_bavail f_bfree
130763987Seric #   endif
130858153Seric #  endif
130958133Seric # endif
131058082Seric 	extern int errno;
131158082Seric 
131258157Seric # if defined(HASUSTAT)
131364311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
131458157Seric # else
131564314Seric #  if defined(IRIX) || defined(apollo)
131664311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
131758157Seric #  else
131858157Seric #   if defined(ultrix)
131964311Seric 	if (statfs(dir, &fs) > 0)
132058153Seric #   else
132164311Seric 	if (statfs(dir, &fs) == 0)
132258153Seric #   endif
132358133Seric #  endif
132458133Seric # endif
132558133Seric 	{
132664311Seric 		if (bsize != NULL)
132764311Seric 			*bsize = FSBLOCKSIZE;
132864311Seric 		return (fs.f_bavail);
132964311Seric 	}
133064311Seric 	return (-1);
133164311Seric }
133264311Seric /*
133364311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
133464311Seric **
133564311Seric **	Only implemented if you have statfs.
133664311Seric **
133764311Seric **	Parameters:
133864311Seric **		msize -- the size to check against.  If zero, we don't yet
133964311Seric **		know how big the message will be, so just check for
134064311Seric **		a "reasonable" amount.
134164311Seric **
134264311Seric **	Returns:
134364311Seric **		TRUE if there is enough space.
134464311Seric **		FALSE otherwise.
134564311Seric */
134664311Seric 
134764311Seric bool
134864311Seric enoughspace(msize)
134964311Seric 	long msize;
135064311Seric {
135164311Seric 	long bfree, bsize;
135264311Seric 
135364311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
135464311Seric 	{
135558133Seric 		if (tTd(4, 80))
135664311Seric 			printf("enoughspace: no threshold\n");
135764311Seric 		return TRUE;
135864311Seric 	}
135964311Seric 
136064311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
136164311Seric 	{
136264311Seric 		if (tTd(4, 80))
136358333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
136464311Seric 				bfree, msize);
136558333Seric 
136658333Seric 		/* convert msize to block count */
136764311Seric 		msize = msize / bsize + 1;
136858333Seric 		if (MinBlocksFree >= 0)
136958333Seric 			msize += MinBlocksFree;
137058333Seric 
137164311Seric 		if (bfree < msize)
137258090Seric 		{
137358090Seric #ifdef LOG
137458090Seric 			if (LogLevel > 0)
137564254Seric 				syslog(LOG_ALERT,
137664254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
137764311Seric 					CurEnv->e_id, bfree,
137864254Seric 					CurHostName, msize, QueueDir);
137958090Seric #endif
138058082Seric 			return FALSE;
138158090Seric 		}
138258082Seric 	}
138358082Seric 	else if (tTd(4, 80))
138458333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
138558333Seric 			MinBlocksFree, msize, errstring(errno));
138658082Seric #endif
138758082Seric 	return TRUE;
138858082Seric }
138958542Seric /*
139058542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
139158542Seric **
139258542Seric **	This looks at an errno value and tells if this is likely to
139358542Seric **	go away if retried later.
139458542Seric **
139558542Seric **	Parameters:
139658542Seric **		err -- the errno code to classify.
139758542Seric **
139858542Seric **	Returns:
139958542Seric **		TRUE if this is probably transient.
140058542Seric **		FALSE otherwise.
140158542Seric */
140258542Seric 
140358542Seric bool
140458542Seric transienterror(err)
140558542Seric 	int err;
140658542Seric {
140758542Seric 	switch (err)
140858542Seric 	{
140958542Seric 	  case EIO:			/* I/O error */
141058542Seric 	  case ENXIO:			/* Device not configured */
141158542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
141258542Seric 	  case ENOMEM:			/* Cannot allocate memory */
141358542Seric 	  case ENODEV:			/* Operation not supported by device */
141458542Seric 	  case ENFILE:			/* Too many open files in system */
141558542Seric 	  case EMFILE:			/* Too many open files */
141658542Seric 	  case ENOSPC:			/* No space left on device */
141758542Seric #ifdef ETIMEDOUT
141858542Seric 	  case ETIMEDOUT:		/* Connection timed out */
141958542Seric #endif
142058542Seric #ifdef ESTALE
142158542Seric 	  case ESTALE:			/* Stale NFS file handle */
142258542Seric #endif
142358542Seric #ifdef ENETDOWN
142458542Seric 	  case ENETDOWN:		/* Network is down */
142558542Seric #endif
142658542Seric #ifdef ENETUNREACH
142758542Seric 	  case ENETUNREACH:		/* Network is unreachable */
142858542Seric #endif
142958542Seric #ifdef ENETRESET
143058542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
143158542Seric #endif
143258542Seric #ifdef ECONNABORTED
143358542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
143458542Seric #endif
143558542Seric #ifdef ECONNRESET
143658542Seric 	  case ECONNRESET:		/* Connection reset by peer */
143758542Seric #endif
143858542Seric #ifdef ENOBUFS
143958542Seric 	  case ENOBUFS:			/* No buffer space available */
144058542Seric #endif
144158542Seric #ifdef ESHUTDOWN
144258542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
144358542Seric #endif
144458542Seric #ifdef ECONNREFUSED
144558542Seric 	  case ECONNREFUSED:		/* Connection refused */
144658542Seric #endif
144758542Seric #ifdef EHOSTDOWN
144858542Seric 	  case EHOSTDOWN:		/* Host is down */
144958542Seric #endif
145058542Seric #ifdef EHOSTUNREACH
145158542Seric 	  case EHOSTUNREACH:		/* No route to host */
145258542Seric #endif
145358542Seric #ifdef EDQUOT
145458542Seric 	  case EDQUOT:			/* Disc quota exceeded */
145558542Seric #endif
145658542Seric #ifdef EPROCLIM
145758542Seric 	  case EPROCLIM:		/* Too many processes */
145858542Seric #endif
145958542Seric #ifdef EUSERS
146058542Seric 	  case EUSERS:			/* Too many users */
146158542Seric #endif
146258542Seric #ifdef EDEADLK
146358542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
146458542Seric #endif
146558542Seric #ifdef EISCONN
146658542Seric 	  case EISCONN:			/* Socket already connected */
146758542Seric #endif
146858542Seric #ifdef EINPROGRESS
146958542Seric 	  case EINPROGRESS:		/* Operation now in progress */
147058542Seric #endif
147158542Seric #ifdef EALREADY
147258542Seric 	  case EALREADY:		/* Operation already in progress */
147358542Seric #endif
147458542Seric #ifdef EADDRINUSE
147558542Seric 	  case EADDRINUSE:		/* Address already in use */
147658542Seric #endif
147758542Seric #ifdef EADDRNOTAVAIL
147858542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
147958542Seric #endif
148063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
148158542Seric 	  case ENOSR:			/* Out of streams resources */
148258542Seric #endif
148358542Seric 		return TRUE;
148458542Seric 	}
148558542Seric 
148658542Seric 	/* nope, must be permanent */
148758542Seric 	return FALSE;
148858542Seric }
148958689Seric /*
149064035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
149158689Seric **
149258689Seric **	Parameters:
149358689Seric **		fd -- the file descriptor of the file.
149458689Seric **		filename -- the file name (for error messages).
149564335Seric **		ext -- the filename extension.
149658689Seric **		type -- type of the lock.  Bits can be:
149758689Seric **			LOCK_EX -- exclusive lock.
149858689Seric **			LOCK_NB -- non-blocking.
149958689Seric **
150058689Seric **	Returns:
150158689Seric **		TRUE if the lock was acquired.
150258689Seric **		FALSE otherwise.
150358689Seric */
150458689Seric 
150558689Seric bool
150664335Seric lockfile(fd, filename, ext, type)
150758689Seric 	int fd;
150858689Seric 	char *filename;
150964335Seric 	char *ext;
151058689Seric 	int type;
151158689Seric {
151264035Seric # ifndef HASFLOCK
151358689Seric 	int action;
151458689Seric 	struct flock lfd;
151564335Seric 
151664335Seric 	if (ext == NULL)
151764335Seric 		ext = "";
151864035Seric 
151963983Seric 	bzero(&lfd, sizeof lfd);
152059447Seric 	if (bitset(LOCK_UN, type))
152159447Seric 		lfd.l_type = F_UNLCK;
152259447Seric 	else if (bitset(LOCK_EX, type))
152358689Seric 		lfd.l_type = F_WRLCK;
152458689Seric 	else
152558689Seric 		lfd.l_type = F_RDLCK;
152658689Seric 
152758689Seric 	if (bitset(LOCK_NB, type))
152858689Seric 		action = F_SETLK;
152958689Seric 	else
153058689Seric 		action = F_SETLKW;
153158689Seric 
153264264Seric 	if (tTd(55, 60))
153364335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
153464335Seric 			filename, ext, action, lfd.l_type);
153564264Seric 
153658689Seric 	if (fcntl(fd, action, &lfd) >= 0)
153764264Seric 	{
153864264Seric 		if (tTd(55, 60))
153964264Seric 			printf("SUCCESS\n");
154058689Seric 		return TRUE;
154164264Seric 	}
154258689Seric 
154364264Seric 	if (tTd(55, 60))
154464264Seric 		printf("(%s) ", errstring(errno));
154564264Seric 
154664136Seric 	/*
154764136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
154864136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
154964136Seric 	**  as type "tmp" (that is, served from swap space), the
155064136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
155164136Seric 	**  Since this is fairly common during testing, we will assume
155264136Seric 	**  that this indicates that the lock is successfully grabbed.
155364136Seric 	*/
155464136Seric 
155564136Seric 	if (errno == EINVAL)
155664264Seric 	{
155764264Seric 		if (tTd(55, 60))
155864264Seric 			printf("SUCCESS\n");
155964136Seric 		return TRUE;
156064264Seric 	}
156164136Seric 
156258689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
156364335Seric 		syserr("cannot lockf(%s%s, %o)", filename, ext, type);
156458689Seric # else
156564335Seric 	if (ext == NULL)
156664335Seric 		ext = "";
156764335Seric 
156864264Seric 	if (tTd(55, 60))
156964335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
157064264Seric 
157158689Seric 	if (flock(fd, type) >= 0)
157264264Seric 	{
157364264Seric 		if (tTd(55, 60))
157464264Seric 			printf("SUCCESS\n");
157558689Seric 		return TRUE;
157664264Seric 	}
157758689Seric 
157864264Seric 	if (tTd(55, 60))
157964264Seric 		printf("(%s) ", errstring(errno));
158064264Seric 
158158689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
158264335Seric 		syserr("cannot flock(%s%s, %o)", filename, ext, type);
158358689Seric # endif
158464264Seric 	if (tTd(55, 60))
158564264Seric 		printf("FAIL\n");
158658689Seric 	return FALSE;
158758689Seric }
1588