xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64718)
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*64718Seric static char sccsid[] = "@(#)conf.c	8.41 (Berkeley) 10/15/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 
733*64718Seric #ifdef DGUX
734*64718Seric 
735*64718Seric #include <sys/dg_sys_info.h>
736*64718Seric 
737*64718Seric int getla()
738*64718Seric {
739*64718Seric 	struct dg_sys_info_load_info load_info;
740*64718Seric 
741*64718Seric 	dg_sys_info((long *)&load_info,
742*64718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
743*64718Seric 
744*64718Seric 	return((int) (load_info.one_minute + 0.5));
745*64718Seric }
746*64718Seric 
747*64718Seric #else
748*64718Seric 
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 
764*64718Seric #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 {
96763966Seric # ifdef HASWAITPID
96860219Seric 	auto int status;
96960560Seric 	int count;
97060560Seric 	int pid;
97160219Seric 
97260560Seric 	count = 0;
97360560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
97460560Seric 	{
97560560Seric 		if (count++ > 1000)
97660560Seric 		{
97760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
97860560Seric 				pid, status);
97960560Seric 			break;
98060560Seric 		}
98160560Seric 	}
98260219Seric # else
98325698Seric # ifdef WNOHANG
98425698Seric 	union wait status;
98525698Seric 
98663753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
98725698Seric 		continue;
98856795Seric # else /* WNOHANG */
98925698Seric 	auto int status;
99025698Seric 
99160219Seric 	while (wait(&status) > 0)
99225698Seric 		continue;
99356795Seric # endif /* WNOHANG */
99460219Seric # endif
99564035Seric # ifdef SYS5SIGNALS
99664035Seric 	(void) setsignal(SIGCHLD, reapchild);
99758061Seric # endif
99825698Seric }
99955418Seric /*
100055418Seric **  UNSETENV -- remove a variable from the environment
100155418Seric **
100255418Seric **	Not needed on newer systems.
100355418Seric **
100455418Seric **	Parameters:
100555418Seric **		name -- the string name of the environment variable to be
100655418Seric **			deleted from the current environment.
100755418Seric **
100855418Seric **	Returns:
100955418Seric **		none.
101055418Seric **
101155418Seric **	Globals:
101255418Seric **		environ -- a pointer to the current environment.
101355418Seric **
101455418Seric **	Side Effects:
101555418Seric **		Modifies environ.
101655418Seric */
101755418Seric 
101863962Seric #ifndef HASUNSETENV
101955418Seric 
102055418Seric void
102155418Seric unsetenv(name)
102255418Seric 	char *name;
102355418Seric {
102455418Seric 	extern char **environ;
102555418Seric 	register char **pp;
102655418Seric 	int len = strlen(name);
102755418Seric 
102855418Seric 	for (pp = environ; *pp != NULL; pp++)
102955418Seric 	{
103055418Seric 		if (strncmp(name, *pp, len) == 0 &&
103155418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
103255418Seric 			break;
103355418Seric 	}
103455418Seric 
103555418Seric 	for (; *pp != NULL; pp++)
103655418Seric 		*pp = pp[1];
103755418Seric }
103855418Seric 
103963962Seric #endif
104056215Seric /*
104156215Seric **  GETDTABLESIZE -- return number of file descriptors
104256215Seric **
104356215Seric **	Only on non-BSD systems
104456215Seric **
104556215Seric **	Parameters:
104656215Seric **		none
104756215Seric **
104856215Seric **	Returns:
104956215Seric **		size of file descriptor table
105056215Seric **
105156215Seric **	Side Effects:
105256215Seric **		none
105356215Seric */
105456215Seric 
105563787Seric #ifdef SOLARIS
105663787Seric # include <sys/resource.h>
105763787Seric #endif
105856215Seric 
105956215Seric int
106063787Seric getdtsize()
106156215Seric {
106263787Seric #ifdef RLIMIT_NOFILE
106363787Seric 	struct rlimit rl;
106463787Seric 
106563787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
106663787Seric 		return rl.rlim_cur;
106763787Seric #endif
106863787Seric 
106964031Seric # ifdef HASGETDTABLESIZE
107064031Seric 	return getdtablesize();
107164031Seric # else
107264031Seric #  ifdef _SC_OPEN_MAX
107358689Seric 	return sysconf(_SC_OPEN_MAX);
107463787Seric #  else
107556215Seric 	return NOFILE;
107663787Seric #  endif
107758689Seric # endif
107856215Seric }
107957631Seric /*
108057631Seric **  UNAME -- get the UUCP name of this system.
108157631Seric */
108257631Seric 
108357943Seric #ifndef HASUNAME
108457631Seric 
108557631Seric int
108657631Seric uname(name)
108757631Seric 	struct utsname *name;
108857631Seric {
108957631Seric 	FILE *file;
109057631Seric 	char *n;
109157631Seric 
109257631Seric 	name->nodename[0] = '\0';
109357631Seric 
109457661Seric 	/* try /etc/whoami -- one line with the node name */
109557631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
109657631Seric 	{
109757661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
109857631Seric 		(void) fclose(file);
109957661Seric 		n = strchr(name->nodename, '\n');
110057631Seric 		if (n != NULL)
110157631Seric 			*n = '\0';
110257631Seric 		if (name->nodename[0] != '\0')
110357631Seric 			return (0);
110457631Seric 	}
110557631Seric 
110657661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
110757631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
110857631Seric 	{
110957631Seric 		char buf[MAXLINE];
111057631Seric 
111157631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
111257631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
111357631Seric 					NODE_LENGTH, name->nodename) > 0)
111457631Seric 				break;
111557631Seric 		(void) fclose(file);
111657631Seric 		if (name->nodename[0] != '\0')
111757631Seric 			return (0);
111857631Seric 	}
111957631Seric 
112057631Seric #ifdef TRUST_POPEN
112157631Seric 	/*
112257631Seric 	**  Popen is known to have security holes.
112357631Seric 	*/
112457631Seric 
112557661Seric 	/* try uuname -l to return local name */
112657631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
112757631Seric 	{
112857661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
112957631Seric 		(void) pclose(file);
113057661Seric 		n = strchr(name, '\n');
113157631Seric 		if (n != NULL)
113257631Seric 			*n = '\0';
113357661Seric 		if (name->nodename[0] != '\0')
113457631Seric 			return (0);
113557631Seric 	}
113657631Seric #endif
113757631Seric 
113857631Seric 	return (-1);
113957631Seric }
114057943Seric #endif /* HASUNAME */
114158068Seric /*
114258068Seric **  INITGROUPS -- initialize groups
114358068Seric **
114458068Seric **	Stub implementation for System V style systems
114558068Seric */
114658068Seric 
114758068Seric #ifndef HASINITGROUPS
114858068Seric 
114958068Seric initgroups(name, basegid)
115058068Seric 	char *name;
115158068Seric 	int basegid;
115258068Seric {
115358068Seric 	return 0;
115458068Seric }
115558068Seric 
115658068Seric #endif
115758082Seric /*
115859289Seric **  SETSID -- set session id (for non-POSIX systems)
115959289Seric */
116059289Seric 
116159289Seric #ifndef HASSETSID
116259289Seric 
116363753Seric pid_t
116463753Seric setsid __P ((void))
116559289Seric {
116664155Seric #ifdef TIOCNOTTY
116764155Seric 	int fd;
116864155Seric 
116964155Seric 	fd = open("/dev/tty", 2);
117064155Seric 	if (fd >= 0)
117164155Seric 	{
117264155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
117364155Seric 		(void) close(fd);
117464155Seric 	}
117564155Seric #endif /* TIOCNOTTY */
117664705Seric # ifdef SYS5SETPGRP
117763753Seric 	return setpgrp();
117863753Seric # else
117964155Seric 	return setpgid(0, getpid());
118059289Seric # endif
118159289Seric }
118259289Seric 
118359289Seric #endif
118459289Seric /*
1185*64718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
1186*64718Seric **
1187*64718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
1188*64718Seric **	instead of a long.  This patches things.
1189*64718Seric */
1190*64718Seric 
1191*64718Seric #ifdef DGUX
1192*64718Seric 
1193*64718Seric #undef inet_addr
1194*64718Seric 
1195*64718Seric long
1196*64718Seric dgux_inet_addr(host)
1197*64718Seric 	char *host;
1198*64718Seric {
1199*64718Seric 	struct in_addr haddr;
1200*64718Seric 
1201*64718Seric 	haddr = inet_addr(host);
1202*64718Seric 	return haddr.s_addr;
1203*64718Seric }
1204*64718Seric 
1205*64718Seric #endif
1206*64718Seric /*
120763969Seric **  GETOPT -- for old systems or systems with bogus implementations
120863969Seric */
120963969Seric 
121063969Seric #ifdef NEEDGETOPT
121163969Seric 
121263969Seric /*
121363969Seric  * Copyright (c) 1985 Regents of the University of California.
121463969Seric  * All rights reserved.  The Berkeley software License Agreement
121563969Seric  * specifies the terms and conditions for redistribution.
121663969Seric  */
121763969Seric 
121863969Seric 
121963969Seric /*
122063969Seric ** this version hacked to add `atend' flag to allow state machine
122163969Seric ** to reset if invoked by the program to scan args for a 2nd time
122263969Seric */
122363969Seric 
122463969Seric #if defined(LIBC_SCCS) && !defined(lint)
122563969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
122664072Seric #endif /* LIBC_SCCS and not lint */
122763969Seric 
122863969Seric #include <stdio.h>
122963969Seric 
123063969Seric /*
123163969Seric  * get option letter from argument vector
123263969Seric  */
123363969Seric int	opterr = 1,		/* if error message should be printed */
123463969Seric 	optind = 1,		/* index into parent argv vector */
123563969Seric 	optopt;			/* character checked for validity */
123663969Seric char	*optarg;		/* argument associated with option */
123763969Seric 
123863969Seric #define BADCH	(int)'?'
123963969Seric #define EMSG	""
124063969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
124163969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
124263969Seric 
124363969Seric getopt(nargc,nargv,ostr)
124463969Seric int	nargc;
124563969Seric char	**nargv,
124663969Seric 	*ostr;
124763969Seric {
124863969Seric 	static char	*place = EMSG;	/* option letter processing */
124963969Seric 	static char	atend = 0;
125063969Seric 	register char	*oli;		/* option letter list index */
125163969Seric 
125263969Seric 	if (atend) {
125363969Seric 		atend = 0;
125463969Seric 		place = EMSG;
125563969Seric 	}
125663969Seric 	if(!*place) {			/* update scanning pointer */
125764136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
125863969Seric 			atend++;
125963969Seric 			return(EOF);
126063969Seric 		}
126163969Seric 		if (*place == '-') {	/* found "--" */
126263969Seric 			++optind;
126363969Seric 			atend++;
126463969Seric 			return(EOF);
126563969Seric 		}
126663969Seric 	}				/* option letter okay? */
126764125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
126864136Seric 		if (!*place) ++optind;
126963969Seric 		tell(": illegal option -- ");
127063969Seric 	}
127163969Seric 	if (*++oli != ':') {		/* don't need argument */
127263969Seric 		optarg = NULL;
127363969Seric 		if (!*place) ++optind;
127463969Seric 	}
127563969Seric 	else {				/* need an argument */
127663969Seric 		if (*place) optarg = place;	/* no white space */
127763969Seric 		else if (nargc <= ++optind) {	/* no arg */
127863969Seric 			place = EMSG;
127963969Seric 			tell(": option requires an argument -- ");
128063969Seric 		}
128163969Seric 	 	else optarg = nargv[optind];	/* white space */
128263969Seric 		place = EMSG;
128363969Seric 		++optind;
128463969Seric 	}
128563969Seric 	return(optopt);			/* dump back option letter */
128663969Seric }
128763969Seric 
128863969Seric #endif
128963969Seric /*
129063969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
129163969Seric */
129263969Seric 
129363969Seric #ifdef NEEDVPRINTF
129463969Seric 
129563969Seric #define MAXARG	16
129663969Seric 
129763969Seric vfprintf(fp, fmt, ap)
129863969Seric 	FILE *	fp;
129963969Seric 	char *	fmt;
130063969Seric 	char **	ap;
130163969Seric {
130263969Seric 	char *	bp[MAXARG];
130363969Seric 	int	i = 0;
130463969Seric 
130563969Seric 	while (*ap && i < MAXARG)
130663969Seric 		bp[i++] = *ap++;
130763969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
130863969Seric 			 bp[4], bp[5], bp[6], bp[7],
130963969Seric 			 bp[8], bp[9], bp[10], bp[11],
131063969Seric 			 bp[12], bp[13], bp[14], bp[15]);
131163969Seric }
131263969Seric 
131363969Seric vsprintf(s, fmt, ap)
131463969Seric 	char *	s;
131563969Seric 	char *	fmt;
131663969Seric 	char **	ap;
131763969Seric {
131863969Seric 	char *	bp[MAXARG];
131963969Seric 	int	i = 0;
132063969Seric 
132163969Seric 	while (*ap && i < MAXARG)
132263969Seric 		bp[i++] = *ap++;
132363969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
132463969Seric 			bp[4], bp[5], bp[6], bp[7],
132563969Seric 			bp[8], bp[9], bp[10], bp[11],
132663969Seric 			bp[12], bp[13], bp[14], bp[15]);
132763969Seric }
132863969Seric 
132963969Seric #endif
133063969Seric /*
133164311Seric **  FREESPACE -- see how much free space is on the queue filesystem
133258082Seric **
133358082Seric **	Only implemented if you have statfs.
133458082Seric **
133558082Seric **	Parameters:
133664311Seric **		dir -- the directory in question.
133764311Seric **		bsize -- a variable into which the filesystem
133864311Seric **			block size is stored.
133958082Seric **
134058082Seric **	Returns:
134164311Seric **		The number of bytes free on the queue filesystem.
134264311Seric **		-1 if the statfs call fails.
134364311Seric **
134464311Seric **	Side effects:
134564311Seric **		Puts the filesystem block size into bsize.
134658082Seric */
134758082Seric 
134858082Seric #ifdef HASSTATFS
134958157Seric # undef HASUSTAT
135058157Seric #endif
135158157Seric 
135258157Seric #if defined(HASUSTAT)
135358157Seric # include <ustat.h>
135458157Seric #endif
135558157Seric 
135658157Seric #ifdef HASSTATFS
1357*64718Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX)
135858133Seric #  include <sys/statfs.h>
135958133Seric # else
136064380Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
136158133Seric #   include <sys/vfs.h>
136258133Seric #  else
136358157Seric #   include <sys/mount.h>
136458133Seric #  endif
136558133Seric # endif
136658082Seric #endif
136758082Seric 
136864311Seric long
136964311Seric freespace(dir, bsize)
137064311Seric 	char *dir;
137164311Seric 	long *bsize;
137258082Seric {
137358160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
137458157Seric # if defined(HASUSTAT)
137558153Seric 	struct ustat fs;
137658153Seric 	struct stat statbuf;
137758366Seric #  define FSBLOCKSIZE	DEV_BSIZE
137858157Seric #  define f_bavail	f_tfree
137958157Seric # else
138058157Seric #  if defined(ultrix)
138158157Seric 	struct fs_data fs;
138258157Seric #   define f_bavail	fd_bfreen
138358366Seric #   define FSBLOCKSIZE	fs.fd_bsize
138458153Seric #  else
138558082Seric 	struct statfs fs;
138658366Seric #   define FSBLOCKSIZE	fs.f_bsize
138764336Seric #   if defined(_SCO_unix_) || defined(IRIX)
138863987Seric #    define f_bavail f_bfree
138963987Seric #   endif
139058153Seric #  endif
139158133Seric # endif
139258082Seric 	extern int errno;
139358082Seric 
139458157Seric # if defined(HASUSTAT)
139564311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
139658157Seric # else
1397*64718Seric #  if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX)
139864311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
139958157Seric #  else
140058157Seric #   if defined(ultrix)
140164311Seric 	if (statfs(dir, &fs) > 0)
140258153Seric #   else
140364311Seric 	if (statfs(dir, &fs) == 0)
140458153Seric #   endif
140558133Seric #  endif
140658133Seric # endif
140758133Seric 	{
140864311Seric 		if (bsize != NULL)
140964311Seric 			*bsize = FSBLOCKSIZE;
141064311Seric 		return (fs.f_bavail);
141164311Seric 	}
141264337Seric #endif
141364311Seric 	return (-1);
141464311Seric }
141564311Seric /*
141664311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
141764311Seric **
141864311Seric **	Only implemented if you have statfs.
141964311Seric **
142064311Seric **	Parameters:
142164311Seric **		msize -- the size to check against.  If zero, we don't yet
142264311Seric **		know how big the message will be, so just check for
142364311Seric **		a "reasonable" amount.
142464311Seric **
142564311Seric **	Returns:
142664311Seric **		TRUE if there is enough space.
142764311Seric **		FALSE otherwise.
142864311Seric */
142964311Seric 
143064311Seric bool
143164311Seric enoughspace(msize)
143264311Seric 	long msize;
143364311Seric {
143464311Seric 	long bfree, bsize;
143564311Seric 
143664311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
143764311Seric 	{
143858133Seric 		if (tTd(4, 80))
143964311Seric 			printf("enoughspace: no threshold\n");
144064311Seric 		return TRUE;
144164311Seric 	}
144264311Seric 
144364311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
144464311Seric 	{
144564311Seric 		if (tTd(4, 80))
144658333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
144764311Seric 				bfree, msize);
144858333Seric 
144958333Seric 		/* convert msize to block count */
145064311Seric 		msize = msize / bsize + 1;
145158333Seric 		if (MinBlocksFree >= 0)
145258333Seric 			msize += MinBlocksFree;
145358333Seric 
145464311Seric 		if (bfree < msize)
145558090Seric 		{
145658090Seric #ifdef LOG
145758090Seric 			if (LogLevel > 0)
145864254Seric 				syslog(LOG_ALERT,
145964254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
146064311Seric 					CurEnv->e_id, bfree,
146164254Seric 					CurHostName, msize, QueueDir);
146258090Seric #endif
146358082Seric 			return FALSE;
146458090Seric 		}
146558082Seric 	}
146658082Seric 	else if (tTd(4, 80))
146758333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
146858333Seric 			MinBlocksFree, msize, errstring(errno));
146958082Seric 	return TRUE;
147058082Seric }
147158542Seric /*
147258542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
147358542Seric **
147458542Seric **	This looks at an errno value and tells if this is likely to
147558542Seric **	go away if retried later.
147658542Seric **
147758542Seric **	Parameters:
147858542Seric **		err -- the errno code to classify.
147958542Seric **
148058542Seric **	Returns:
148158542Seric **		TRUE if this is probably transient.
148258542Seric **		FALSE otherwise.
148358542Seric */
148458542Seric 
148558542Seric bool
148658542Seric transienterror(err)
148758542Seric 	int err;
148858542Seric {
148958542Seric 	switch (err)
149058542Seric 	{
149158542Seric 	  case EIO:			/* I/O error */
149258542Seric 	  case ENXIO:			/* Device not configured */
149358542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
149458542Seric 	  case ENOMEM:			/* Cannot allocate memory */
149558542Seric 	  case ENODEV:			/* Operation not supported by device */
149658542Seric 	  case ENFILE:			/* Too many open files in system */
149758542Seric 	  case EMFILE:			/* Too many open files */
149858542Seric 	  case ENOSPC:			/* No space left on device */
149958542Seric #ifdef ETIMEDOUT
150058542Seric 	  case ETIMEDOUT:		/* Connection timed out */
150158542Seric #endif
150258542Seric #ifdef ESTALE
150358542Seric 	  case ESTALE:			/* Stale NFS file handle */
150458542Seric #endif
150558542Seric #ifdef ENETDOWN
150658542Seric 	  case ENETDOWN:		/* Network is down */
150758542Seric #endif
150858542Seric #ifdef ENETUNREACH
150958542Seric 	  case ENETUNREACH:		/* Network is unreachable */
151058542Seric #endif
151158542Seric #ifdef ENETRESET
151258542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
151358542Seric #endif
151458542Seric #ifdef ECONNABORTED
151558542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
151658542Seric #endif
151758542Seric #ifdef ECONNRESET
151858542Seric 	  case ECONNRESET:		/* Connection reset by peer */
151958542Seric #endif
152058542Seric #ifdef ENOBUFS
152158542Seric 	  case ENOBUFS:			/* No buffer space available */
152258542Seric #endif
152358542Seric #ifdef ESHUTDOWN
152458542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
152558542Seric #endif
152658542Seric #ifdef ECONNREFUSED
152758542Seric 	  case ECONNREFUSED:		/* Connection refused */
152858542Seric #endif
152958542Seric #ifdef EHOSTDOWN
153058542Seric 	  case EHOSTDOWN:		/* Host is down */
153158542Seric #endif
153258542Seric #ifdef EHOSTUNREACH
153358542Seric 	  case EHOSTUNREACH:		/* No route to host */
153458542Seric #endif
153558542Seric #ifdef EDQUOT
153658542Seric 	  case EDQUOT:			/* Disc quota exceeded */
153758542Seric #endif
153858542Seric #ifdef EPROCLIM
153958542Seric 	  case EPROCLIM:		/* Too many processes */
154058542Seric #endif
154158542Seric #ifdef EUSERS
154258542Seric 	  case EUSERS:			/* Too many users */
154358542Seric #endif
154458542Seric #ifdef EDEADLK
154558542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
154658542Seric #endif
154758542Seric #ifdef EISCONN
154858542Seric 	  case EISCONN:			/* Socket already connected */
154958542Seric #endif
155058542Seric #ifdef EINPROGRESS
155158542Seric 	  case EINPROGRESS:		/* Operation now in progress */
155258542Seric #endif
155358542Seric #ifdef EALREADY
155458542Seric 	  case EALREADY:		/* Operation already in progress */
155558542Seric #endif
155658542Seric #ifdef EADDRINUSE
155758542Seric 	  case EADDRINUSE:		/* Address already in use */
155858542Seric #endif
155958542Seric #ifdef EADDRNOTAVAIL
156058542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
156158542Seric #endif
156263834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
156358542Seric 	  case ENOSR:			/* Out of streams resources */
156458542Seric #endif
156558542Seric 		return TRUE;
156658542Seric 	}
156758542Seric 
156858542Seric 	/* nope, must be permanent */
156958542Seric 	return FALSE;
157058542Seric }
157158689Seric /*
157264035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
157358689Seric **
157458689Seric **	Parameters:
157558689Seric **		fd -- the file descriptor of the file.
157658689Seric **		filename -- the file name (for error messages).
157764335Seric **		ext -- the filename extension.
157858689Seric **		type -- type of the lock.  Bits can be:
157958689Seric **			LOCK_EX -- exclusive lock.
158058689Seric **			LOCK_NB -- non-blocking.
158158689Seric **
158258689Seric **	Returns:
158358689Seric **		TRUE if the lock was acquired.
158458689Seric **		FALSE otherwise.
158558689Seric */
158658689Seric 
158758689Seric bool
158864335Seric lockfile(fd, filename, ext, type)
158958689Seric 	int fd;
159058689Seric 	char *filename;
159164335Seric 	char *ext;
159258689Seric 	int type;
159358689Seric {
159464035Seric # ifndef HASFLOCK
159558689Seric 	int action;
159658689Seric 	struct flock lfd;
159764335Seric 
159864335Seric 	if (ext == NULL)
159964335Seric 		ext = "";
160064035Seric 
160163983Seric 	bzero(&lfd, sizeof lfd);
160259447Seric 	if (bitset(LOCK_UN, type))
160359447Seric 		lfd.l_type = F_UNLCK;
160459447Seric 	else if (bitset(LOCK_EX, type))
160558689Seric 		lfd.l_type = F_WRLCK;
160658689Seric 	else
160758689Seric 		lfd.l_type = F_RDLCK;
160858689Seric 
160958689Seric 	if (bitset(LOCK_NB, type))
161058689Seric 		action = F_SETLK;
161158689Seric 	else
161258689Seric 		action = F_SETLKW;
161358689Seric 
161464264Seric 	if (tTd(55, 60))
161564335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
161664335Seric 			filename, ext, action, lfd.l_type);
161764264Seric 
161858689Seric 	if (fcntl(fd, action, &lfd) >= 0)
161964264Seric 	{
162064264Seric 		if (tTd(55, 60))
162164264Seric 			printf("SUCCESS\n");
162258689Seric 		return TRUE;
162364264Seric 	}
162458689Seric 
162564264Seric 	if (tTd(55, 60))
162664264Seric 		printf("(%s) ", errstring(errno));
162764264Seric 
162864136Seric 	/*
162964136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
163064136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
163164136Seric 	**  as type "tmp" (that is, served from swap space), the
163264136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
163364136Seric 	**  Since this is fairly common during testing, we will assume
163464136Seric 	**  that this indicates that the lock is successfully grabbed.
163564136Seric 	*/
163664136Seric 
163764136Seric 	if (errno == EINVAL)
163864264Seric 	{
163964264Seric 		if (tTd(55, 60))
164064264Seric 			printf("SUCCESS\n");
164164136Seric 		return TRUE;
164264264Seric 	}
164364136Seric 
164458689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
164564378Seric 	{
164664378Seric 		int omode = -1;
164764378Seric #  ifdef F_GETFL
164864378Seric 		int oerrno = errno;
164964378Seric 
165064378Seric 		(void) fcntl(fd, F_GETFL, &omode);
165164378Seric 		errno = oerrno;
165264378Seric #  endif
165364378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
165464378Seric 			filename, ext, fd, type, omode, geteuid());
165564378Seric 	}
165658689Seric # else
165764335Seric 	if (ext == NULL)
165864335Seric 		ext = "";
165964335Seric 
166064264Seric 	if (tTd(55, 60))
166164335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
166264264Seric 
166358689Seric 	if (flock(fd, type) >= 0)
166464264Seric 	{
166564264Seric 		if (tTd(55, 60))
166664264Seric 			printf("SUCCESS\n");
166758689Seric 		return TRUE;
166864264Seric 	}
166958689Seric 
167064264Seric 	if (tTd(55, 60))
167164264Seric 		printf("(%s) ", errstring(errno));
167264264Seric 
167358689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
167464378Seric 	{
167564378Seric 		int omode = -1;
167664378Seric #  ifdef F_GETFL
167764378Seric 		int oerrno = errno;
167864378Seric 
167964378Seric 		(void) fcntl(fd, F_GETFL, &omode);
168064378Seric 		errno = oerrno;
168164378Seric #  endif
168264378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
168364378Seric 			filename, ext, fd, type, omode, geteuid());
168464386Seric 	}
168558689Seric # endif
168664264Seric 	if (tTd(55, 60))
168764264Seric 		printf("FAIL\n");
168858689Seric 	return FALSE;
168958689Seric }
169064556Seric /*
169164556Seric **  GETCFNAME -- return the name of the .cf file.
169264556Seric **
169364556Seric **	Some systems (e.g., NeXT) determine this dynamically.
169464556Seric */
169564556Seric 
169664556Seric char *
169764556Seric getcfname()
169864556Seric {
169964556Seric 	if (ConfFile != NULL)
170064556Seric 		return ConfFile;
170164556Seric 	return _PATH_SENDMAILCF;
170264556Seric }
1703*64718Seric /*
1704*64718Seric **  SETVENDOR -- process vendor code from V configuration line
1705*64718Seric **
1706*64718Seric **	Parameters:
1707*64718Seric **		vendor -- string representation of vendor.
1708*64718Seric **
1709*64718Seric **	Returns:
1710*64718Seric **		TRUE -- if ok.
1711*64718Seric **		FALSE -- if vendor code could not be processed.
1712*64718Seric */
1713*64718Seric 
1714*64718Seric bool
1715*64718Seric setvendor(vendor)
1716*64718Seric 	char *vendor;
1717*64718Seric {
1718*64718Seric 	return (strcasecmp(vendor, "Berkeley") == 0);
1719*64718Seric }
1720