xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64816)
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*64816Seric static char sccsid[] = "@(#)conf.c	8.43 (Berkeley) 11/08/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,
6364682Seric 	"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 
10458082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1059039Seric 
1069064Seric 
1079064Seric 
1089039Seric /*
10958082Seric **  Privacy values
11058082Seric */
11158082Seric 
11258082Seric struct prival PrivacyValues[] =
11358082Seric {
11458082Seric 	"public",		PRIV_PUBLIC,
11558082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11658114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11758082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11858082Seric 	"noexpn",		PRIV_NOEXPN,
11958082Seric 	"novrfy",		PRIV_NOVRFY,
12064333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12164333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12258789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12358082Seric 	"goaway",		PRIV_GOAWAY,
12458789Seric 	NULL,			0,
12558082Seric };
12658082Seric 
12758082Seric 
12858082Seric 
12958082Seric /*
13024943Seric **  Miscellaneous stuff.
1319039Seric */
1329039Seric 
13324943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13464644Seric 
13564644Seric 
13664644Seric /*
13764644Seric **  Following should be config parameters (and probably will be in
13864644Seric **  future releases).  In the meantime, setting these is considered
13964644Seric **  unsupported, and is intentionally undocumented.
14064644Seric */
14164644Seric 
14264644Seric #ifdef BROKENSMTPPEERS
14364644Seric bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
14464644Seric #else
14564644Seric bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
14664644Seric #endif
14764644Seric #ifdef NOLOOPBACKCHECK
14864644Seric bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
14964644Seric #else
15064644Seric bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
15164644Seric #endif
15264644Seric 
15324943Seric /*
15424943Seric **  SETDEFAULTS -- set default values
15524943Seric **
15624943Seric **	Because of the way freezing is done, these must be initialized
15724943Seric **	using direct code.
15824943Seric **
15924943Seric **	Parameters:
16058734Seric **		e -- the default envelope.
16124943Seric **
16224943Seric **	Returns:
16324943Seric **		none.
16424943Seric **
16524943Seric **	Side Effects:
16624943Seric **		Initializes a bunch of global variables to their
16724943Seric **		default values.
16824943Seric */
16924943Seric 
17058737Seric #define DAYS		* 24 * 60 * 60
17158737Seric 
17258734Seric setdefaults(e)
17358734Seric 	register ENVELOPE *e;
17424943Seric {
17557438Seric 	SpaceSub = ' ';				/* option B */
17657438Seric 	QueueLA = 8;				/* option x */
17757438Seric 	RefuseLA = 12;				/* option X */
17857438Seric 	WkRecipFact = 30000L;			/* option y */
17957438Seric 	WkClassFact = 1800L;			/* option z */
18057438Seric 	WkTimeFact = 90000L;			/* option Z */
18157438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
18263787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
18357438Seric 						/* option F */
18457438Seric 	DefUid = 1;				/* option u */
18557438Seric 	DefGid = 1;				/* option g */
18657438Seric 	CheckpointInterval = 10;		/* option C */
18757438Seric 	MaxHopCount = 25;			/* option h */
18858734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18958734Seric 	e->e_errormode = EM_PRINT;		/* option e */
19059709Seric 	SevenBit = FALSE;			/* option 7 */
19157438Seric 	MaxMciCache = 1;			/* option k */
19257438Seric 	MciCacheTimeout = 300;			/* option K */
19357438Seric 	LogLevel = 9;				/* option L */
19458112Seric 	settimeouts(NULL);			/* option r */
19558737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
19658737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
19758853Seric 	PrivacyFlags = 0;			/* option p */
19840973Sbostic 	setdefuser();
19953654Seric 	setupmaps();
20057402Seric 	setupmailers();
20124943Seric }
202294Seric 
20340973Sbostic 
2044326Seric /*
20540973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20640973Sbostic */
20740973Sbostic 
20840973Sbostic setdefuser()
20940973Sbostic {
21040973Sbostic 	struct passwd *defpwent;
21157386Seric 	static char defuserbuf[40];
21240973Sbostic 
21357386Seric 	DefUser = defuserbuf;
21440973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
21557386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21640973Sbostic 	else
21757386Seric 		strcpy(defuserbuf, "nobody");
21840973Sbostic }
21953654Seric /*
22056836Seric **  HOST_MAP_INIT -- initialize host class structures
22156836Seric */
22256836Seric 
22356836Seric bool
22460219Seric host_map_init(map, args)
22556836Seric 	MAP *map;
22656836Seric 	char *args;
22756836Seric {
22856836Seric 	register char *p = args;
22956836Seric 
23056836Seric 	for (;;)
23156836Seric 	{
23258050Seric 		while (isascii(*p) && isspace(*p))
23356836Seric 			p++;
23456836Seric 		if (*p != '-')
23556836Seric 			break;
23656836Seric 		switch (*++p)
23756836Seric 		{
23856836Seric 		  case 'a':
23956836Seric 			map->map_app = ++p;
24056836Seric 			break;
24156836Seric 		}
24258050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24356836Seric 			p++;
24456836Seric 		if (*p != '\0')
24556836Seric 			*p++ = '\0';
24656836Seric 	}
24756836Seric 	if (map->map_app != NULL)
24856836Seric 		map->map_app = newstr(map->map_app);
24956836Seric 	return TRUE;
25056836Seric }
25157402Seric /*
25257402Seric **  SETUPMAILERS -- initialize default mailers
25357402Seric */
25456836Seric 
25557402Seric setupmailers()
25657402Seric {
25757402Seric 	char buf[100];
25857402Seric 
25957403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
26057403Seric 	makemailer(buf);
26157403Seric 
26259883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
26357402Seric 	makemailer(buf);
26457402Seric 
26557402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26657402Seric 	makemailer(buf);
26757402Seric }
26856836Seric /*
26960207Seric **  SETUPMAPS -- set up map classes
27060207Seric */
27160207Seric 
27260207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27360207Seric 	{ \
27460207Seric 		extern bool parse __P((MAP *, char *)); \
27560207Seric 		extern bool open __P((MAP *, int)); \
27660207Seric 		extern void close __P((MAP *)); \
27760207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
27860207Seric 		extern void store __P((MAP *, char *, char *)); \
27960207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28060207Seric 		s->s_mapclass.map_cname = name; \
28160207Seric 		s->s_mapclass.map_ext = ext; \
28260207Seric 		s->s_mapclass.map_cflags = flags; \
28360207Seric 		s->s_mapclass.map_parse = parse; \
28460207Seric 		s->s_mapclass.map_open = open; \
28560207Seric 		s->s_mapclass.map_close = close; \
28660207Seric 		s->s_mapclass.map_lookup = lookup; \
28760207Seric 		s->s_mapclass.map_store = store; \
28860207Seric 	}
28960207Seric 
29060207Seric setupmaps()
29160207Seric {
29260207Seric 	register STAB *s;
29360207Seric 
29460207Seric #ifdef NEWDB
29560207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29660207Seric 		map_parseargs, hash_map_open, db_map_close,
29760207Seric 		db_map_lookup, db_map_store);
29860207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29960207Seric 		map_parseargs, bt_map_open, db_map_close,
30060207Seric 		db_map_lookup, db_map_store);
30160207Seric #endif
30260207Seric 
30360207Seric #ifdef NDBM
30460207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30560207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30660207Seric 		ndbm_map_lookup, ndbm_map_store);
30760207Seric #endif
30860207Seric 
30960207Seric #ifdef NIS
31060207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31160207Seric 		map_parseargs, nis_map_open, nis_map_close,
31260207Seric 		nis_map_lookup, nis_map_store);
31360207Seric #endif
31460207Seric 
31560207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
31660207Seric 		map_parseargs, stab_map_open, stab_map_close,
31760207Seric 		stab_map_lookup, stab_map_store);
31860207Seric 
31960207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
32060207Seric 		map_parseargs, impl_map_open, impl_map_close,
32160207Seric 		impl_map_lookup, impl_map_store);
32260207Seric 
32360207Seric 	/* host DNS lookup */
32460207Seric 	MAPDEF("host", NULL, 0,
32560207Seric 		host_map_init, null_map_open, null_map_close,
32660207Seric 		host_map_lookup, null_map_store);
32760207Seric 
32860207Seric 	/* dequote map */
32960207Seric 	MAPDEF("dequote", NULL, 0,
33060207Seric 		dequote_init, null_map_open, null_map_close,
33160207Seric 		dequote_map, null_map_store);
33260207Seric 
33360207Seric #if 0
33460207Seric # ifdef USERDB
33560207Seric 	/* user database */
33660207Seric 	MAPDEF("udb", ".db", 0,
33760207Seric 		udb_map_parse, null_map_open, null_map_close,
33860207Seric 		udb_map_lookup, null_map_store);
33960207Seric # endif
34060207Seric #endif
34160207Seric }
34260207Seric 
34360207Seric #undef MAPDEF
34460207Seric /*
3459369Seric **  USERNAME -- return the user id of the logged in user.
3469369Seric **
3479369Seric **	Parameters:
3489369Seric **		none.
3499369Seric **
3509369Seric **	Returns:
3519369Seric **		The login name of the logged in user.
3529369Seric **
3539369Seric **	Side Effects:
3549369Seric **		none.
3559369Seric **
3569369Seric **	Notes:
3579369Seric **		The return value is statically allocated.
3589369Seric */
3599369Seric 
3609369Seric char *
3619369Seric username()
3629369Seric {
36317469Seric 	static char *myname = NULL;
3649369Seric 	extern char *getlogin();
36519904Smiriam 	register struct passwd *pw;
3669369Seric 
36717469Seric 	/* cache the result */
36817469Seric 	if (myname == NULL)
36917469Seric 	{
37017469Seric 		myname = getlogin();
37117469Seric 		if (myname == NULL || myname[0] == '\0')
37217469Seric 		{
37363787Seric 			pw = getpwuid(RealUid);
37417469Seric 			if (pw != NULL)
37540993Sbostic 				myname = newstr(pw->pw_name);
37617469Seric 		}
37719904Smiriam 		else
37819904Smiriam 		{
37963787Seric 			uid_t uid = RealUid;
38019873Smiriam 
38140993Sbostic 			myname = newstr(myname);
38240993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
38358736Seric 			      (uid != 0 && uid != pw->pw_uid))
38419904Smiriam 			{
38558736Seric 				pw = getpwuid(uid);
38624945Seric 				if (pw != NULL)
38740993Sbostic 					myname = newstr(pw->pw_name);
38819873Smiriam 			}
38919873Smiriam 		}
39017469Seric 		if (myname == NULL || myname[0] == '\0')
39117469Seric 		{
39258151Seric 			syserr("554 Who are you?");
39317469Seric 			myname = "postmaster";
39417469Seric 		}
39517469Seric 	}
39617469Seric 
39717469Seric 	return (myname);
3989369Seric }
3999369Seric /*
4004190Seric **  TTYPATH -- Get the path of the user's tty
401294Seric **
402294Seric **	Returns the pathname of the user's tty.  Returns NULL if
403294Seric **	the user is not logged in or if s/he has write permission
404294Seric **	denied.
405294Seric **
406294Seric **	Parameters:
407294Seric **		none
408294Seric **
409294Seric **	Returns:
410294Seric **		pathname of the user's tty.
411294Seric **		NULL if not logged in or write permission denied.
412294Seric **
413294Seric **	Side Effects:
414294Seric **		none.
415294Seric **
416294Seric **	WARNING:
417294Seric **		Return value is in a local buffer.
418294Seric **
419294Seric **	Called By:
420294Seric **		savemail
421294Seric */
422294Seric 
423294Seric char *
424294Seric ttypath()
425294Seric {
426294Seric 	struct stat stbuf;
427294Seric 	register char *pathn;
428294Seric 	extern char *ttyname();
4294081Seric 	extern char *getlogin();
430294Seric 
431294Seric 	/* compute the pathname of the controlling tty */
4329369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4339369Seric 	    (pathn = ttyname(0)) == NULL)
434294Seric 	{
435294Seric 		errno = 0;
436294Seric 		return (NULL);
437294Seric 	}
438294Seric 
439294Seric 	/* see if we have write permission */
4402967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
441294Seric 	{
442294Seric 		errno = 0;
443294Seric 		return (NULL);
444294Seric 	}
445294Seric 
446294Seric 	/* see if the user is logged in */
447294Seric 	if (getlogin() == NULL)
448294Seric 		return (NULL);
449294Seric 
450294Seric 	/* looks good */
451294Seric 	return (pathn);
452294Seric }
4532967Seric /*
4542967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4552967Seric **
4562967Seric **	This routine can be supplied on a per-installation basis
4572967Seric **	to determine whether a person is allowed to send a message.
4582967Seric **	This allows restriction of certain types of internet
4592967Seric **	forwarding or registration of users.
4602967Seric **
4612967Seric **	If the hosts are found to be incompatible, an error
46257454Seric **	message should be given using "usrerr" and 0 should
4632967Seric **	be returned.
4642967Seric **
4654288Seric **	'NoReturn' can be set to suppress the return-to-sender
4664288Seric **	function; this should be done on huge messages.
4674288Seric **
4682967Seric **	Parameters:
4692967Seric **		to -- the person being sent to.
4702967Seric **
4712967Seric **	Returns:
47257459Seric **		an exit status
4732967Seric **
4742967Seric **	Side Effects:
4752967Seric **		none (unless you include the usrerr stuff)
4762967Seric */
4772967Seric 
47855012Seric checkcompat(to, e)
4792967Seric 	register ADDRESS *to;
48055012Seric 	register ENVELOPE *e;
4812967Seric {
48212133Seric # ifdef lint
48312133Seric 	if (to == NULL)
48412133Seric 		to++;
48563834Seric # endif /* lint */
48664402Seric 
48764402Seric 	if (tTd(49, 1))
48864402Seric 		printf("checkcompat(to=%s, from=%s)\n",
48964402Seric 			to->q_paddr, e->e_from.q_paddr);
49064402Seric 
49110698Seric # ifdef EXAMPLE_CODE
49210698Seric 	/* this code is intended as an example only */
4934437Seric 	register STAB *s;
4944437Seric 
4954437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49655012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4979369Seric 	    to->q_mailer == s->s_mailer)
4984437Seric 	{
49958151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50010698Seric 		/* NoReturn = TRUE; to supress return copy */
50157459Seric 		return (EX_UNAVAILABLE);
5024437Seric 	}
50356795Seric # endif /* EXAMPLE_CODE */
50457459Seric 	return (EX_OK);
5052967Seric }
5069369Seric /*
50764035Seric **  SETSIGNAL -- set a signal handler
50864035Seric **
50964035Seric **	This is essentially old BSD "signal(3)".
51064035Seric */
51164035Seric 
51264561Seric sigfunc_t
51364035Seric setsignal(sig, handler)
51464035Seric 	int sig;
51564561Seric 	sigfunc_t handler;
51664035Seric {
51764380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
51864035Seric 	return signal(sig, handler);
51964035Seric #else
52064035Seric 	struct sigaction n, o;
52164035Seric 
52264035Seric 	bzero(&n, sizeof n);
52364035Seric 	n.sa_handler = handler;
52464035Seric 	if (sigaction(sig, &n, &o) < 0)
52564035Seric 		return SIG_ERR;
52664035Seric 	return o.sa_handler;
52764035Seric #endif
52864035Seric }
52964035Seric /*
5309369Seric **  HOLDSIGS -- arrange to hold all signals
5319369Seric **
5329369Seric **	Parameters:
5339369Seric **		none.
5349369Seric **
5359369Seric **	Returns:
5369369Seric **		none.
5379369Seric **
5389369Seric **	Side Effects:
5399369Seric **		Arranges that signals are held.
5409369Seric */
5419369Seric 
5429369Seric holdsigs()
5439369Seric {
5449369Seric }
5459369Seric /*
5469369Seric **  RLSESIGS -- arrange to release all signals
5479369Seric **
5489369Seric **	This undoes the effect of holdsigs.
5499369Seric **
5509369Seric **	Parameters:
5519369Seric **		none.
5529369Seric **
5539369Seric **	Returns:
5549369Seric **		none.
5559369Seric **
5569369Seric **	Side Effects:
5579369Seric **		Arranges that signals are released.
5589369Seric */
5599369Seric 
5609369Seric rlsesigs()
5619369Seric {
5629369Seric }
56314872Seric /*
56464705Seric **  INIT_MD -- do machine dependent initializations
56564705Seric **
56664705Seric **	Systems that have global modes that should be set should do
56764705Seric **	them here rather than in main.
56864705Seric */
56964705Seric 
57064705Seric #ifdef _AUX_SOURCE
57164705Seric # include	<compat.h>
57264705Seric #endif
57364705Seric 
57464705Seric init_md()
57564705Seric {
57664705Seric #ifdef _AUX_SOURCE
57764705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
57864705Seric #endif
57964705Seric }
58064705Seric /*
58114872Seric **  GETLA -- get the current load average
58214872Seric **
58314881Seric **	This code stolen from la.c.
58414881Seric **
58514872Seric **	Parameters:
58614872Seric **		none.
58714872Seric **
58814872Seric **	Returns:
58914872Seric **		The current load average as an integer.
59014872Seric **
59114872Seric **	Side Effects:
59214872Seric **		none.
59314872Seric */
59414872Seric 
59551920Seric /* try to guess what style of load average we have */
59651920Seric #define LA_ZERO		1	/* always return load average as zero */
59764360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
59851920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
59951920Seric #define LA_SUBR		4	/* call getloadavg */
60064295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
60164360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
60214872Seric 
60363962Seric /* do guesses based on general OS type */
60451920Seric #ifndef LA_TYPE
60563962Seric # define LA_TYPE	LA_ZERO
60651920Seric #endif
60751920Seric 
60864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
60951920Seric 
61014872Seric #include <nlist.h>
61114872Seric 
61251920Seric #ifndef LA_AVENRUN
61363962Seric # ifdef SYSTEM5
61463962Seric #  define LA_AVENRUN	"avenrun"
61563962Seric # else
61663962Seric #  define LA_AVENRUN	"_avenrun"
61763962Seric # endif
61851920Seric #endif
61951920Seric 
62051920Seric /* _PATH_UNIX should be defined in <paths.h> */
62151920Seric #ifndef _PATH_UNIX
62263962Seric # if defined(SYSTEM5)
62363962Seric #  define _PATH_UNIX	"/unix"
62463962Seric # else
62563962Seric #  define _PATH_UNIX	"/vmunix"
62663962Seric # endif
62751920Seric #endif
62851920Seric 
62914872Seric struct	nlist Nl[] =
63014872Seric {
63151920Seric 	{ LA_AVENRUN },
63214872Seric #define	X_AVENRUN	0
63314872Seric 	{ 0 },
63414872Seric };
63514872Seric 
63660583Seric #ifndef FSHIFT
63760583Seric # if defined(unixpc)
63860583Seric #  define FSHIFT	5
63960583Seric # endif
64057736Seric 
64160583Seric # if defined(__alpha)
64260583Seric #  define FSHIFT	10
64360583Seric # endif
64457977Seric 
64564360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
64651920Seric #  define FSHIFT	8
64760583Seric # endif
64857736Seric #endif
64960583Seric 
65064360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
65151920Seric #  define FSCALE	(1 << FSHIFT)
65251920Seric #endif
65340930Srick 
65414872Seric getla()
65514872Seric {
65614872Seric 	static int kmem = -1;
65751920Seric #if LA_TYPE == LA_INT
65824943Seric 	long avenrun[3];
65951920Seric #else
66064360Seric # if LA_TYPE == LA_SHORT
66164360Seric 	short avenrun[3];
66264360Seric # else
66351920Seric 	double avenrun[3];
66464360Seric # endif
66551920Seric #endif
66625615Seric 	extern off_t lseek();
66757736Seric 	extern int errno;
66814872Seric 
66914872Seric 	if (kmem < 0)
67014872Seric 	{
67124945Seric 		kmem = open("/dev/kmem", 0, 0);
67214872Seric 		if (kmem < 0)
67357736Seric 		{
67457736Seric 			if (tTd(3, 1))
67557736Seric 				printf("getla: open(/dev/kmem): %s\n",
67657736Seric 					errstring(errno));
67714872Seric 			return (-1);
67857736Seric 		}
67951920Seric 		(void) fcntl(kmem, F_SETFD, 1);
68057736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68157736Seric 		{
68257736Seric 			if (tTd(3, 1))
68357736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68457736Seric 					errstring(errno));
68514872Seric 			return (-1);
68657736Seric 		}
68759253Seric 		if (Nl[X_AVENRUN].n_value == 0)
68859253Seric 		{
68959253Seric 			if (tTd(3, 1))
69059253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
69159253Seric 					_PATH_UNIX, LA_AVENRUN);
69259253Seric 			return (-1);
69359253Seric 		}
69414872Seric 	}
69557736Seric 	if (tTd(3, 20))
69657736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
69724945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
69823118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
69919967Seric 	{
70019967Seric 		/* thank you Ian */
70157736Seric 		if (tTd(3, 1))
70257736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
70319967Seric 		return (-1);
70419967Seric 	}
70564360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
70657736Seric 	if (tTd(3, 5))
70757736Seric 	{
70857736Seric 		printf("getla: avenrun = %d", avenrun[0]);
70957736Seric 		if (tTd(3, 15))
71057736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71157736Seric 		printf("\n");
71257736Seric 	}
71357736Seric 	if (tTd(3, 1))
71457736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71524943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71651920Seric #else
71757736Seric 	if (tTd(3, 5))
71857736Seric 	{
71957736Seric 		printf("getla: avenrun = %g", avenrun[0]);
72057736Seric 		if (tTd(3, 15))
72157736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
72257736Seric 		printf("\n");
72357736Seric 	}
72457736Seric 	if (tTd(3, 1))
72557736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72651920Seric 	return ((int) (avenrun[0] + 0.5));
72751920Seric #endif
72814872Seric }
72914872Seric 
73051773Seric #else
73151920Seric #if LA_TYPE == LA_SUBR
73251773Seric 
73364718Seric #ifdef DGUX
73464718Seric 
73564718Seric #include <sys/dg_sys_info.h>
73664718Seric 
73764718Seric int getla()
73864718Seric {
73964718Seric 	struct dg_sys_info_load_info load_info;
74064718Seric 
74164718Seric 	dg_sys_info((long *)&load_info,
74264718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
74364718Seric 
74464718Seric 	return((int) (load_info.one_minute + 0.5));
74564718Seric }
74664718Seric 
74764718Seric #else
74864718Seric 
74951773Seric getla()
75051773Seric {
75151920Seric 	double avenrun[3];
75251920Seric 
75351920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
75457736Seric 	{
75557736Seric 		if (tTd(3, 1))
75657736Seric 			perror("getla: getloadavg failed:");
75751920Seric 		return (-1);
75857736Seric 	}
75957736Seric 	if (tTd(3, 1))
76057736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
76151920Seric 	return ((int) (avenrun[0] + 0.5));
76251773Seric }
76351773Seric 
76464718Seric #endif /* DGUX */
76551773Seric #else
76664295Seric #if LA_TYPE == LA_MACH
76751773Seric 
76864295Seric /*
76964295Seric **  This has been tested on NeXT release 2.1.
77064295Seric */
77164295Seric 
77264295Seric #include <mach.h>
77364295Seric 
77451773Seric getla()
77551773Seric {
77664295Seric 	processor_set_t default_set;
77764295Seric 	kern_return_t error;
77864295Seric 	unsigned int info_count;
77964295Seric 	struct processor_set_basic_info info;
78064295Seric 	host_t host;
78164295Seric 
78264295Seric 	error = processor_set_default(host_self(), &default_set);
78364295Seric 	if (error != KERN_SUCCESS)
78464295Seric 		return -1;
78564295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
78664295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
78764295Seric 			       &host, (processor_set_info_t)&info,
78864295Seric 			       &info_count) != KERN_SUCCESS)
78964295Seric 	{
79064295Seric 		return -1;
79164295Seric 	}
79264295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
79364295Seric }
79464295Seric 
79564295Seric 
79664295Seric #else
79764295Seric 
79864295Seric getla()
79964295Seric {
80057736Seric 	if (tTd(3, 1))
80157736Seric 		printf("getla: ZERO\n");
80251920Seric 	return (0);
80351773Seric }
80451773Seric 
80551773Seric #endif
80651773Seric #endif
80764295Seric #endif
80824943Seric /*
80924943Seric **  SHOULDQUEUE -- should this message be queued or sent?
81024943Seric **
81124943Seric **	Compares the message cost to the load average to decide.
81224943Seric **
81324943Seric **	Parameters:
81424943Seric **		pri -- the priority of the message in question.
81557438Seric **		ctime -- the message creation time.
81624943Seric **
81724943Seric **	Returns:
81824943Seric **		TRUE -- if this message should be queued up for the
81924943Seric **			time being.
82024943Seric **		FALSE -- if the load is low enough to send this message.
82124943Seric **
82224943Seric **	Side Effects:
82324943Seric **		none.
82424943Seric */
82524943Seric 
82624943Seric bool
82757438Seric shouldqueue(pri, ctime)
82824943Seric 	long pri;
82957438Seric 	time_t ctime;
83024943Seric {
83151920Seric 	if (CurrentLA < QueueLA)
83224943Seric 		return (FALSE);
83358132Seric 	if (CurrentLA >= RefuseLA)
83458132Seric 		return (TRUE);
83551920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
83624943Seric }
83724943Seric /*
83853037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
83953037Seric **
84053037Seric **	Parameters:
84153037Seric **		none.
84253037Seric **
84353037Seric **	Returns:
84453037Seric **		TRUE if incoming SMTP connections should be refused
84553037Seric **			(for now).
84653037Seric **		FALSE if we should accept new work.
84753037Seric **
84853037Seric **	Side Effects:
84953037Seric **		none.
85053037Seric */
85153037Seric 
85253037Seric bool
85353037Seric refuseconnections()
85453037Seric {
85559156Seric #ifdef XLA
85659156Seric 	if (!xla_smtp_ok())
85759156Seric 		return TRUE;
85859156Seric #endif
85959156Seric 
86053037Seric 	/* this is probably too simplistic */
86158132Seric 	return (CurrentLA >= RefuseLA);
86253037Seric }
86353037Seric /*
86424943Seric **  SETPROCTITLE -- set process title for ps
86524943Seric **
86624943Seric **	Parameters:
86758674Seric **		fmt -- a printf style format string.
86858674Seric **		a, b, c -- possible parameters to fmt.
86924943Seric **
87024943Seric **	Returns:
87124943Seric **		none.
87224943Seric **
87324943Seric **	Side Effects:
87424943Seric **		Clobbers argv of our main procedure so ps(1) will
87524943Seric **		display the title.
87624943Seric */
87724943Seric 
87858689Seric #ifdef SETPROCTITLE
87958689Seric # ifdef __hpux
88058689Seric #  include <sys/pstat.h>
88158689Seric # endif
88259732Seric # ifdef BSD4_4
88359732Seric #  include <machine/vmparam.h>
88459732Seric #  include <sys/exec.h>
88564247Seric #  ifdef __bsdi__
88664247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
88764247Seric #  endif
88859992Seric #  ifdef PS_STRINGS
88959992Seric #   define SETPROC_STATIC static
89059992Seric #  endif
89159732Seric # endif
89259732Seric # ifndef SETPROC_STATIC
89359732Seric #  define SETPROC_STATIC
89459732Seric # endif
89558689Seric #endif
89658689Seric 
89724943Seric /*VARARGS1*/
89857642Seric #ifdef __STDC__
89957642Seric setproctitle(char *fmt, ...)
90057642Seric #else
90157642Seric setproctitle(fmt, va_alist)
90224943Seric 	char *fmt;
90357642Seric 	va_dcl
90457642Seric #endif
90524943Seric {
90624943Seric # ifdef SETPROCTITLE
90724943Seric 	register char *p;
90825049Seric 	register int i;
90959732Seric 	SETPROC_STATIC char buf[MAXLINE];
91056852Seric 	VA_LOCAL_DECL
91158689Seric #  ifdef __hpux
91258689Seric 	union pstun pst;
91358689Seric #  endif
91424943Seric 	extern char **Argv;
91524943Seric 	extern char *LastArgv;
91624943Seric 
91758674Seric 	p = buf;
91824943Seric 
91958674Seric 	/* print sendmail: heading for grep */
92058674Seric 	(void) strcpy(p, "sendmail: ");
92158674Seric 	p += strlen(p);
92224943Seric 
92358674Seric 	/* print the argument string */
92458674Seric 	VA_START(fmt);
92558674Seric 	(void) vsprintf(p, fmt, ap);
92656852Seric 	VA_END;
92754996Seric 
92858674Seric 	i = strlen(buf);
92958689Seric 
93058689Seric #  ifdef __hpux
93158689Seric 	pst.pst_command = buf;
93258689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
93358689Seric #  else
93459992Seric #   ifdef PS_STRINGS
93559732Seric 	PS_STRINGS->ps_nargvstr = 1;
93659732Seric 	PS_STRINGS->ps_argvstr = buf;
93759732Seric #   else
93854996Seric 	if (i > LastArgv - Argv[0] - 2)
93925049Seric 	{
94054996Seric 		i = LastArgv - Argv[0] - 2;
94158674Seric 		buf[i] = '\0';
94225049Seric 	}
94358674Seric 	(void) strcpy(Argv[0], buf);
94454997Seric 	p = &Argv[0][i];
94524943Seric 	while (p < LastArgv)
94624943Seric 		*p++ = ' ';
94759732Seric #   endif
94858689Seric #  endif
94956795Seric # endif /* SETPROCTITLE */
95024943Seric }
95125698Seric /*
95225698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
95325698Seric **
95425698Seric **	Parameters:
95525698Seric **		none.
95625698Seric **
95725698Seric **	Returns:
95825698Seric **		none.
95925698Seric **
96025698Seric **	Side Effects:
96125698Seric **		Picks up extant zombies.
96225698Seric */
96325698Seric 
96446928Sbostic void
96525698Seric reapchild()
96625698Seric {
96764738Seric 	int olderrno = errno;
96863966Seric # ifdef HASWAITPID
96960219Seric 	auto int status;
97060560Seric 	int count;
97160560Seric 	int pid;
97260219Seric 
97360560Seric 	count = 0;
97460560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
97560560Seric 	{
97660560Seric 		if (count++ > 1000)
97760560Seric 		{
97860560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
97960560Seric 				pid, status);
98060560Seric 			break;
98160560Seric 		}
98260560Seric 	}
98360219Seric # else
98425698Seric # ifdef WNOHANG
98525698Seric 	union wait status;
98625698Seric 
98763753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
98825698Seric 		continue;
98956795Seric # else /* WNOHANG */
99025698Seric 	auto int status;
99125698Seric 
99260219Seric 	while (wait(&status) > 0)
99325698Seric 		continue;
99456795Seric # endif /* WNOHANG */
99560219Seric # endif
99664035Seric # ifdef SYS5SIGNALS
99764035Seric 	(void) setsignal(SIGCHLD, reapchild);
99858061Seric # endif
99964738Seric 	errno = olderrno;
100025698Seric }
100155418Seric /*
100255418Seric **  UNSETENV -- remove a variable from the environment
100355418Seric **
100455418Seric **	Not needed on newer systems.
100555418Seric **
100655418Seric **	Parameters:
100755418Seric **		name -- the string name of the environment variable to be
100855418Seric **			deleted from the current environment.
100955418Seric **
101055418Seric **	Returns:
101155418Seric **		none.
101255418Seric **
101355418Seric **	Globals:
101455418Seric **		environ -- a pointer to the current environment.
101555418Seric **
101655418Seric **	Side Effects:
101755418Seric **		Modifies environ.
101855418Seric */
101955418Seric 
102063962Seric #ifndef HASUNSETENV
102155418Seric 
102255418Seric void
102355418Seric unsetenv(name)
102455418Seric 	char *name;
102555418Seric {
102655418Seric 	extern char **environ;
102755418Seric 	register char **pp;
102855418Seric 	int len = strlen(name);
102955418Seric 
103055418Seric 	for (pp = environ; *pp != NULL; pp++)
103155418Seric 	{
103255418Seric 		if (strncmp(name, *pp, len) == 0 &&
103355418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
103455418Seric 			break;
103555418Seric 	}
103655418Seric 
103755418Seric 	for (; *pp != NULL; pp++)
103855418Seric 		*pp = pp[1];
103955418Seric }
104055418Seric 
104163962Seric #endif
104256215Seric /*
104356215Seric **  GETDTABLESIZE -- return number of file descriptors
104456215Seric **
104556215Seric **	Only on non-BSD systems
104656215Seric **
104756215Seric **	Parameters:
104856215Seric **		none
104956215Seric **
105056215Seric **	Returns:
105156215Seric **		size of file descriptor table
105256215Seric **
105356215Seric **	Side Effects:
105456215Seric **		none
105556215Seric */
105656215Seric 
105763787Seric #ifdef SOLARIS
105863787Seric # include <sys/resource.h>
105963787Seric #endif
106056215Seric 
106156215Seric int
106263787Seric getdtsize()
106356215Seric {
106463787Seric #ifdef RLIMIT_NOFILE
106563787Seric 	struct rlimit rl;
106663787Seric 
106763787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
106863787Seric 		return rl.rlim_cur;
106963787Seric #endif
107063787Seric 
107164031Seric # ifdef HASGETDTABLESIZE
107264031Seric 	return getdtablesize();
107364031Seric # else
107464031Seric #  ifdef _SC_OPEN_MAX
107558689Seric 	return sysconf(_SC_OPEN_MAX);
107663787Seric #  else
107756215Seric 	return NOFILE;
107863787Seric #  endif
107958689Seric # endif
108056215Seric }
108157631Seric /*
108257631Seric **  UNAME -- get the UUCP name of this system.
108357631Seric */
108457631Seric 
108557943Seric #ifndef HASUNAME
108657631Seric 
108757631Seric int
108857631Seric uname(name)
108957631Seric 	struct utsname *name;
109057631Seric {
109157631Seric 	FILE *file;
109257631Seric 	char *n;
109357631Seric 
109457631Seric 	name->nodename[0] = '\0';
109557631Seric 
109657661Seric 	/* try /etc/whoami -- one line with the node name */
109757631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
109857631Seric 	{
109957661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
110057631Seric 		(void) fclose(file);
110157661Seric 		n = strchr(name->nodename, '\n');
110257631Seric 		if (n != NULL)
110357631Seric 			*n = '\0';
110457631Seric 		if (name->nodename[0] != '\0')
110557631Seric 			return (0);
110657631Seric 	}
110757631Seric 
110857661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
110957631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
111057631Seric 	{
111157631Seric 		char buf[MAXLINE];
111257631Seric 
111357631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
111457631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
111557631Seric 					NODE_LENGTH, name->nodename) > 0)
111657631Seric 				break;
111757631Seric 		(void) fclose(file);
111857631Seric 		if (name->nodename[0] != '\0')
111957631Seric 			return (0);
112057631Seric 	}
112157631Seric 
112257631Seric #ifdef TRUST_POPEN
112357631Seric 	/*
112457631Seric 	**  Popen is known to have security holes.
112557631Seric 	*/
112657631Seric 
112757661Seric 	/* try uuname -l to return local name */
112857631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
112957631Seric 	{
113057661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
113157631Seric 		(void) pclose(file);
113257661Seric 		n = strchr(name, '\n');
113357631Seric 		if (n != NULL)
113457631Seric 			*n = '\0';
113557661Seric 		if (name->nodename[0] != '\0')
113657631Seric 			return (0);
113757631Seric 	}
113857631Seric #endif
113957631Seric 
114057631Seric 	return (-1);
114157631Seric }
114257943Seric #endif /* HASUNAME */
114358068Seric /*
114458068Seric **  INITGROUPS -- initialize groups
114558068Seric **
114658068Seric **	Stub implementation for System V style systems
114758068Seric */
114858068Seric 
114958068Seric #ifndef HASINITGROUPS
115058068Seric 
115158068Seric initgroups(name, basegid)
115258068Seric 	char *name;
115358068Seric 	int basegid;
115458068Seric {
115558068Seric 	return 0;
115658068Seric }
115758068Seric 
115858068Seric #endif
115958082Seric /*
116059289Seric **  SETSID -- set session id (for non-POSIX systems)
116159289Seric */
116259289Seric 
116359289Seric #ifndef HASSETSID
116459289Seric 
116563753Seric pid_t
116663753Seric setsid __P ((void))
116759289Seric {
116864155Seric #ifdef TIOCNOTTY
116964155Seric 	int fd;
117064155Seric 
117164155Seric 	fd = open("/dev/tty", 2);
117264155Seric 	if (fd >= 0)
117364155Seric 	{
117464155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
117564155Seric 		(void) close(fd);
117664155Seric 	}
117764155Seric #endif /* TIOCNOTTY */
117864705Seric # ifdef SYS5SETPGRP
117963753Seric 	return setpgrp();
118063753Seric # else
118164155Seric 	return setpgid(0, getpid());
118259289Seric # endif
118359289Seric }
118459289Seric 
118559289Seric #endif
118659289Seric /*
118764718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
118864718Seric **
118964718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
119064718Seric **	instead of a long.  This patches things.
119164718Seric */
119264718Seric 
119364718Seric #ifdef DGUX
119464718Seric 
119564718Seric #undef inet_addr
119664718Seric 
119764718Seric long
119864718Seric dgux_inet_addr(host)
119964718Seric 	char *host;
120064718Seric {
120164718Seric 	struct in_addr haddr;
120264718Seric 
120364718Seric 	haddr = inet_addr(host);
120464718Seric 	return haddr.s_addr;
120564718Seric }
120664718Seric 
120764718Seric #endif
120864718Seric /*
120963969Seric **  GETOPT -- for old systems or systems with bogus implementations
121063969Seric */
121163969Seric 
121263969Seric #ifdef NEEDGETOPT
121363969Seric 
121463969Seric /*
121563969Seric  * Copyright (c) 1985 Regents of the University of California.
121663969Seric  * All rights reserved.  The Berkeley software License Agreement
121763969Seric  * specifies the terms and conditions for redistribution.
121863969Seric  */
121963969Seric 
122063969Seric 
122163969Seric /*
122263969Seric ** this version hacked to add `atend' flag to allow state machine
122363969Seric ** to reset if invoked by the program to scan args for a 2nd time
122463969Seric */
122563969Seric 
122663969Seric #if defined(LIBC_SCCS) && !defined(lint)
122763969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
122864072Seric #endif /* LIBC_SCCS and not lint */
122963969Seric 
123063969Seric #include <stdio.h>
123163969Seric 
123263969Seric /*
123363969Seric  * get option letter from argument vector
123463969Seric  */
123563969Seric int	opterr = 1,		/* if error message should be printed */
123663969Seric 	optind = 1,		/* index into parent argv vector */
123763969Seric 	optopt;			/* character checked for validity */
123863969Seric char	*optarg;		/* argument associated with option */
123963969Seric 
124063969Seric #define BADCH	(int)'?'
124163969Seric #define EMSG	""
124263969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
124363969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
124463969Seric 
124563969Seric getopt(nargc,nargv,ostr)
124663969Seric int	nargc;
124763969Seric char	**nargv,
124863969Seric 	*ostr;
124963969Seric {
125063969Seric 	static char	*place = EMSG;	/* option letter processing */
125163969Seric 	static char	atend = 0;
125263969Seric 	register char	*oli;		/* option letter list index */
125363969Seric 
125463969Seric 	if (atend) {
125563969Seric 		atend = 0;
125663969Seric 		place = EMSG;
125763969Seric 	}
125863969Seric 	if(!*place) {			/* update scanning pointer */
125964136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
126063969Seric 			atend++;
126163969Seric 			return(EOF);
126263969Seric 		}
126363969Seric 		if (*place == '-') {	/* found "--" */
126463969Seric 			++optind;
126563969Seric 			atend++;
126663969Seric 			return(EOF);
126763969Seric 		}
126863969Seric 	}				/* option letter okay? */
126964125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
127064136Seric 		if (!*place) ++optind;
127163969Seric 		tell(": illegal option -- ");
127263969Seric 	}
127363969Seric 	if (*++oli != ':') {		/* don't need argument */
127463969Seric 		optarg = NULL;
127563969Seric 		if (!*place) ++optind;
127663969Seric 	}
127763969Seric 	else {				/* need an argument */
127863969Seric 		if (*place) optarg = place;	/* no white space */
127963969Seric 		else if (nargc <= ++optind) {	/* no arg */
128063969Seric 			place = EMSG;
128163969Seric 			tell(": option requires an argument -- ");
128263969Seric 		}
128363969Seric 	 	else optarg = nargv[optind];	/* white space */
128463969Seric 		place = EMSG;
128563969Seric 		++optind;
128663969Seric 	}
128763969Seric 	return(optopt);			/* dump back option letter */
128863969Seric }
128963969Seric 
129063969Seric #endif
129163969Seric /*
129263969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
129363969Seric */
129463969Seric 
129563969Seric #ifdef NEEDVPRINTF
129663969Seric 
129763969Seric #define MAXARG	16
129863969Seric 
129963969Seric vfprintf(fp, fmt, ap)
130063969Seric 	FILE *	fp;
130163969Seric 	char *	fmt;
130263969Seric 	char **	ap;
130363969Seric {
130463969Seric 	char *	bp[MAXARG];
130563969Seric 	int	i = 0;
130663969Seric 
130763969Seric 	while (*ap && i < MAXARG)
130863969Seric 		bp[i++] = *ap++;
130963969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
131063969Seric 			 bp[4], bp[5], bp[6], bp[7],
131163969Seric 			 bp[8], bp[9], bp[10], bp[11],
131263969Seric 			 bp[12], bp[13], bp[14], bp[15]);
131363969Seric }
131463969Seric 
131563969Seric vsprintf(s, fmt, ap)
131663969Seric 	char *	s;
131763969Seric 	char *	fmt;
131863969Seric 	char **	ap;
131963969Seric {
132063969Seric 	char *	bp[MAXARG];
132163969Seric 	int	i = 0;
132263969Seric 
132363969Seric 	while (*ap && i < MAXARG)
132463969Seric 		bp[i++] = *ap++;
132563969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
132663969Seric 			bp[4], bp[5], bp[6], bp[7],
132763969Seric 			bp[8], bp[9], bp[10], bp[11],
132863969Seric 			bp[12], bp[13], bp[14], bp[15]);
132963969Seric }
133063969Seric 
133163969Seric #endif
133263969Seric /*
133364311Seric **  FREESPACE -- see how much free space is on the queue filesystem
133458082Seric **
133558082Seric **	Only implemented if you have statfs.
133658082Seric **
133758082Seric **	Parameters:
133864311Seric **		dir -- the directory in question.
133964311Seric **		bsize -- a variable into which the filesystem
134064311Seric **			block size is stored.
134158082Seric **
134258082Seric **	Returns:
134364311Seric **		The number of bytes free on the queue filesystem.
134464311Seric **		-1 if the statfs call fails.
134564311Seric **
134664311Seric **	Side effects:
134764311Seric **		Puts the filesystem block size into bsize.
134858082Seric */
134958082Seric 
135058082Seric #ifdef HASSTATFS
135158157Seric # undef HASUSTAT
135258157Seric #endif
135358157Seric 
135458157Seric #if defined(HASUSTAT)
135558157Seric # include <ustat.h>
135658157Seric #endif
135758157Seric 
135858157Seric #ifdef HASSTATFS
135964718Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX)
136058133Seric #  include <sys/statfs.h>
136158133Seric # else
1362*64816Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) || defined(MACH386)
136358133Seric #   include <sys/vfs.h>
136458133Seric #  else
136558157Seric #   include <sys/mount.h>
136658133Seric #  endif
136758133Seric # endif
136858082Seric #endif
136958082Seric 
137064311Seric long
137164311Seric freespace(dir, bsize)
137264311Seric 	char *dir;
137364311Seric 	long *bsize;
137458082Seric {
137558160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
137658157Seric # if defined(HASUSTAT)
137758153Seric 	struct ustat fs;
137858153Seric 	struct stat statbuf;
137958366Seric #  define FSBLOCKSIZE	DEV_BSIZE
138058157Seric #  define f_bavail	f_tfree
138158157Seric # else
138258157Seric #  if defined(ultrix)
138358157Seric 	struct fs_data fs;
138458157Seric #   define f_bavail	fd_bfreen
138558366Seric #   define FSBLOCKSIZE	fs.fd_bsize
138658153Seric #  else
138758082Seric 	struct statfs fs;
138858366Seric #   define FSBLOCKSIZE	fs.f_bsize
138964336Seric #   if defined(_SCO_unix_) || defined(IRIX)
139063987Seric #    define f_bavail f_bfree
139163987Seric #   endif
139258153Seric #  endif
139358133Seric # endif
139458082Seric 	extern int errno;
139558082Seric 
139658157Seric # if defined(HASUSTAT)
139764311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
139858157Seric # else
139964718Seric #  if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX)
140064311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
140158157Seric #  else
140258157Seric #   if defined(ultrix)
140364311Seric 	if (statfs(dir, &fs) > 0)
140458153Seric #   else
140564311Seric 	if (statfs(dir, &fs) == 0)
140658153Seric #   endif
140758133Seric #  endif
140858133Seric # endif
140958133Seric 	{
141064311Seric 		if (bsize != NULL)
141164311Seric 			*bsize = FSBLOCKSIZE;
141264311Seric 		return (fs.f_bavail);
141364311Seric 	}
141464337Seric #endif
141564311Seric 	return (-1);
141664311Seric }
141764311Seric /*
141864311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
141964311Seric **
142064311Seric **	Only implemented if you have statfs.
142164311Seric **
142264311Seric **	Parameters:
142364311Seric **		msize -- the size to check against.  If zero, we don't yet
142464311Seric **		know how big the message will be, so just check for
142564311Seric **		a "reasonable" amount.
142664311Seric **
142764311Seric **	Returns:
142864311Seric **		TRUE if there is enough space.
142964311Seric **		FALSE otherwise.
143064311Seric */
143164311Seric 
143264311Seric bool
143364311Seric enoughspace(msize)
143464311Seric 	long msize;
143564311Seric {
143664311Seric 	long bfree, bsize;
143764311Seric 
143864311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
143964311Seric 	{
144058133Seric 		if (tTd(4, 80))
144164311Seric 			printf("enoughspace: no threshold\n");
144264311Seric 		return TRUE;
144364311Seric 	}
144464311Seric 
144564311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
144664311Seric 	{
144764311Seric 		if (tTd(4, 80))
144858333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
144964311Seric 				bfree, msize);
145058333Seric 
145158333Seric 		/* convert msize to block count */
145264311Seric 		msize = msize / bsize + 1;
145358333Seric 		if (MinBlocksFree >= 0)
145458333Seric 			msize += MinBlocksFree;
145558333Seric 
145664311Seric 		if (bfree < msize)
145758090Seric 		{
145858090Seric #ifdef LOG
145958090Seric 			if (LogLevel > 0)
146064254Seric 				syslog(LOG_ALERT,
146164254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
146264311Seric 					CurEnv->e_id, bfree,
146364254Seric 					CurHostName, msize, QueueDir);
146458090Seric #endif
146558082Seric 			return FALSE;
146658090Seric 		}
146758082Seric 	}
146858082Seric 	else if (tTd(4, 80))
146958333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
147058333Seric 			MinBlocksFree, msize, errstring(errno));
147158082Seric 	return TRUE;
147258082Seric }
147358542Seric /*
147458542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
147558542Seric **
147658542Seric **	This looks at an errno value and tells if this is likely to
147758542Seric **	go away if retried later.
147858542Seric **
147958542Seric **	Parameters:
148058542Seric **		err -- the errno code to classify.
148158542Seric **
148258542Seric **	Returns:
148358542Seric **		TRUE if this is probably transient.
148458542Seric **		FALSE otherwise.
148558542Seric */
148658542Seric 
148758542Seric bool
148858542Seric transienterror(err)
148958542Seric 	int err;
149058542Seric {
149158542Seric 	switch (err)
149258542Seric 	{
149358542Seric 	  case EIO:			/* I/O error */
149458542Seric 	  case ENXIO:			/* Device not configured */
149558542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
149658542Seric 	  case ENOMEM:			/* Cannot allocate memory */
149758542Seric 	  case ENODEV:			/* Operation not supported by device */
149858542Seric 	  case ENFILE:			/* Too many open files in system */
149958542Seric 	  case EMFILE:			/* Too many open files */
150058542Seric 	  case ENOSPC:			/* No space left on device */
150158542Seric #ifdef ETIMEDOUT
150258542Seric 	  case ETIMEDOUT:		/* Connection timed out */
150358542Seric #endif
150458542Seric #ifdef ESTALE
150558542Seric 	  case ESTALE:			/* Stale NFS file handle */
150658542Seric #endif
150758542Seric #ifdef ENETDOWN
150858542Seric 	  case ENETDOWN:		/* Network is down */
150958542Seric #endif
151058542Seric #ifdef ENETUNREACH
151158542Seric 	  case ENETUNREACH:		/* Network is unreachable */
151258542Seric #endif
151358542Seric #ifdef ENETRESET
151458542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
151558542Seric #endif
151658542Seric #ifdef ECONNABORTED
151758542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
151858542Seric #endif
151958542Seric #ifdef ECONNRESET
152058542Seric 	  case ECONNRESET:		/* Connection reset by peer */
152158542Seric #endif
152258542Seric #ifdef ENOBUFS
152358542Seric 	  case ENOBUFS:			/* No buffer space available */
152458542Seric #endif
152558542Seric #ifdef ESHUTDOWN
152658542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
152758542Seric #endif
152858542Seric #ifdef ECONNREFUSED
152958542Seric 	  case ECONNREFUSED:		/* Connection refused */
153058542Seric #endif
153158542Seric #ifdef EHOSTDOWN
153258542Seric 	  case EHOSTDOWN:		/* Host is down */
153358542Seric #endif
153458542Seric #ifdef EHOSTUNREACH
153558542Seric 	  case EHOSTUNREACH:		/* No route to host */
153658542Seric #endif
153758542Seric #ifdef EDQUOT
153858542Seric 	  case EDQUOT:			/* Disc quota exceeded */
153958542Seric #endif
154058542Seric #ifdef EPROCLIM
154158542Seric 	  case EPROCLIM:		/* Too many processes */
154258542Seric #endif
154358542Seric #ifdef EUSERS
154458542Seric 	  case EUSERS:			/* Too many users */
154558542Seric #endif
154658542Seric #ifdef EDEADLK
154758542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
154858542Seric #endif
154958542Seric #ifdef EISCONN
155058542Seric 	  case EISCONN:			/* Socket already connected */
155158542Seric #endif
155258542Seric #ifdef EINPROGRESS
155358542Seric 	  case EINPROGRESS:		/* Operation now in progress */
155458542Seric #endif
155558542Seric #ifdef EALREADY
155658542Seric 	  case EALREADY:		/* Operation already in progress */
155758542Seric #endif
155858542Seric #ifdef EADDRINUSE
155958542Seric 	  case EADDRINUSE:		/* Address already in use */
156058542Seric #endif
156158542Seric #ifdef EADDRNOTAVAIL
156258542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
156358542Seric #endif
156463834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
156558542Seric 	  case ENOSR:			/* Out of streams resources */
156658542Seric #endif
156758542Seric 		return TRUE;
156858542Seric 	}
156958542Seric 
157058542Seric 	/* nope, must be permanent */
157158542Seric 	return FALSE;
157258542Seric }
157358689Seric /*
157464035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
157558689Seric **
157658689Seric **	Parameters:
157758689Seric **		fd -- the file descriptor of the file.
157858689Seric **		filename -- the file name (for error messages).
157964335Seric **		ext -- the filename extension.
158058689Seric **		type -- type of the lock.  Bits can be:
158158689Seric **			LOCK_EX -- exclusive lock.
158258689Seric **			LOCK_NB -- non-blocking.
158358689Seric **
158458689Seric **	Returns:
158558689Seric **		TRUE if the lock was acquired.
158658689Seric **		FALSE otherwise.
158758689Seric */
158858689Seric 
158958689Seric bool
159064335Seric lockfile(fd, filename, ext, type)
159158689Seric 	int fd;
159258689Seric 	char *filename;
159364335Seric 	char *ext;
159458689Seric 	int type;
159558689Seric {
159664035Seric # ifndef HASFLOCK
159758689Seric 	int action;
159858689Seric 	struct flock lfd;
159964335Seric 
160064335Seric 	if (ext == NULL)
160164335Seric 		ext = "";
160264035Seric 
160363983Seric 	bzero(&lfd, sizeof lfd);
160459447Seric 	if (bitset(LOCK_UN, type))
160559447Seric 		lfd.l_type = F_UNLCK;
160659447Seric 	else if (bitset(LOCK_EX, type))
160758689Seric 		lfd.l_type = F_WRLCK;
160858689Seric 	else
160958689Seric 		lfd.l_type = F_RDLCK;
161058689Seric 
161158689Seric 	if (bitset(LOCK_NB, type))
161258689Seric 		action = F_SETLK;
161358689Seric 	else
161458689Seric 		action = F_SETLKW;
161558689Seric 
161664264Seric 	if (tTd(55, 60))
161764335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
161864335Seric 			filename, ext, action, lfd.l_type);
161964264Seric 
162058689Seric 	if (fcntl(fd, action, &lfd) >= 0)
162164264Seric 	{
162264264Seric 		if (tTd(55, 60))
162364264Seric 			printf("SUCCESS\n");
162458689Seric 		return TRUE;
162564264Seric 	}
162658689Seric 
162764264Seric 	if (tTd(55, 60))
162864264Seric 		printf("(%s) ", errstring(errno));
162964264Seric 
163064136Seric 	/*
163164136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
163264136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
163364136Seric 	**  as type "tmp" (that is, served from swap space), the
163464136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
163564136Seric 	**  Since this is fairly common during testing, we will assume
163664136Seric 	**  that this indicates that the lock is successfully grabbed.
163764136Seric 	*/
163864136Seric 
163964136Seric 	if (errno == EINVAL)
164064264Seric 	{
164164264Seric 		if (tTd(55, 60))
164264264Seric 			printf("SUCCESS\n");
164364136Seric 		return TRUE;
164464264Seric 	}
164564136Seric 
164658689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
164764378Seric 	{
164864378Seric 		int omode = -1;
164964378Seric #  ifdef F_GETFL
165064378Seric 		int oerrno = errno;
165164378Seric 
165264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
165364378Seric 		errno = oerrno;
165464378Seric #  endif
165564378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
165664378Seric 			filename, ext, fd, type, omode, geteuid());
165764378Seric 	}
165858689Seric # else
165964335Seric 	if (ext == NULL)
166064335Seric 		ext = "";
166164335Seric 
166264264Seric 	if (tTd(55, 60))
166364335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
166464264Seric 
166558689Seric 	if (flock(fd, type) >= 0)
166664264Seric 	{
166764264Seric 		if (tTd(55, 60))
166864264Seric 			printf("SUCCESS\n");
166958689Seric 		return TRUE;
167064264Seric 	}
167158689Seric 
167264264Seric 	if (tTd(55, 60))
167364264Seric 		printf("(%s) ", errstring(errno));
167464264Seric 
167558689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
167664378Seric 	{
167764378Seric 		int omode = -1;
167864378Seric #  ifdef F_GETFL
167964378Seric 		int oerrno = errno;
168064378Seric 
168164378Seric 		(void) fcntl(fd, F_GETFL, &omode);
168264378Seric 		errno = oerrno;
168364378Seric #  endif
168464378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
168564378Seric 			filename, ext, fd, type, omode, geteuid());
168664386Seric 	}
168758689Seric # endif
168864264Seric 	if (tTd(55, 60))
168964264Seric 		printf("FAIL\n");
169058689Seric 	return FALSE;
169158689Seric }
169264556Seric /*
169364556Seric **  GETCFNAME -- return the name of the .cf file.
169464556Seric **
169564556Seric **	Some systems (e.g., NeXT) determine this dynamically.
169664556Seric */
169764556Seric 
169864556Seric char *
169964556Seric getcfname()
170064556Seric {
170164556Seric 	if (ConfFile != NULL)
170264556Seric 		return ConfFile;
170364556Seric 	return _PATH_SENDMAILCF;
170464556Seric }
170564718Seric /*
170664718Seric **  SETVENDOR -- process vendor code from V configuration line
170764718Seric **
170864718Seric **	Parameters:
170964718Seric **		vendor -- string representation of vendor.
171064718Seric **
171164718Seric **	Returns:
171264718Seric **		TRUE -- if ok.
171364718Seric **		FALSE -- if vendor code could not be processed.
171464718Seric */
171564718Seric 
171664718Seric bool
171764718Seric setvendor(vendor)
171864718Seric 	char *vendor;
171964718Seric {
172064718Seric 	return (strcasecmp(vendor, "Berkeley") == 0);
172164718Seric }
1722*64816Seric /*
1723*64816Seric **  STRTOL -- convert string to long integer
1724*64816Seric **
1725*64816Seric **	For systems that don't have it in the C library.
1726*64816Seric */
1727*64816Seric 
1728*64816Seric #ifdef NEEDSTRTOL
1729*64816Seric 
1730*64816Seric long
1731*64816Seric strtol(p, ep, b)
1732*64816Seric 	char *p;
1733*64816Seric 	char **ep;
1734*64816Seric 	int b;
1735*64816Seric {
1736*64816Seric 	long l = 0;
1737*64816Seric 	char c;
1738*64816Seric 	char maxd;
1739*64816Seric 	int neg = 1;
1740*64816Seric 
1741*64816Seric 	maxd = (b > 10) ? '9' : b + '0';
1742*64816Seric 
1743*64816Seric 	if (p && *p == '-') {
1744*64816Seric 		neg = -1;
1745*64816Seric 		p++;
1746*64816Seric 	}
1747*64816Seric 	while (p && (c = *p)) {
1748*64816Seric 		if (c >= '0' && c <= maxd) {
1749*64816Seric 			l = l*b + *p++ - '0';
1750*64816Seric 			continue;
1751*64816Seric 		}
1752*64816Seric 		if (c >= 'A' && c <= 'Z')
1753*64816Seric 			c -= 'A' + 'a';
1754*64816Seric 		c = c - 'a' + 10;
1755*64816Seric 		if (b > c) {
1756*64816Seric 			l = l*b + c;
1757*64816Seric 			p++;
1758*64816Seric 			continue;
1759*64816Seric 		}
1760*64816Seric 		break;
1761*64816Seric 	}
1762*64816Seric 	l *= neg;
1763*64816Seric 	if (ep)
1764*64816Seric 		*ep = p;
1765*64816Seric 	return l;
1766*64816Seric }
1767*64816Seric 
1768*64816Seric #endif
1769