xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64644)
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*64644Seric static char sccsid[] = "@(#)conf.c	8.38 (Berkeley) 09/24/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1736928Sbostic # include <pwd.h>
18404Seric 
19294Seric /*
203309Seric **  CONF.C -- Sendmail Configuration Tables.
21294Seric **
22294Seric **	Defines the configuration of this installation.
23294Seric **
241388Seric **	Configuration Variables:
252897Seric **		HdrInfo -- a table describing well-known header fields.
262897Seric **			Each entry has the field name and some flags,
274147Seric **			which are described in sendmail.h.
284093Seric **
294093Seric **	Notes:
304093Seric **		I have tried to put almost all the reasonable
314093Seric **		configuration information into the configuration
324093Seric **		file read at runtime.  My intent is that anything
334093Seric **		here is a function of the version of UNIX you
344093Seric **		are running, or is really static -- for example
354093Seric **		the headers are a superset of widely used
364093Seric **		protocols.  If you find yourself playing with
374093Seric **		this file too much, you may be making a mistake!
38294Seric */
39294Seric 
40294Seric 
41294Seric 
42294Seric 
434437Seric /*
442897Seric **  Header info table
453057Seric **	Final (null) entry contains the flags used for any other field.
464147Seric **
474147Seric **	Not all of these are actually handled specially by sendmail
484147Seric **	at this time.  They are included as placeholders, to let
494147Seric **	you know that "someday" I intend to have sendmail do
504147Seric **	something with them.
512897Seric */
522897Seric 
532897Seric struct hdrinfo	HdrInfo[] =
542897Seric {
558060Seric 		/* originator fields, most to least significant  */
5611417Seric 	"resent-sender",	H_FROM|H_RESENT,
5711417Seric 	"resent-from",		H_FROM|H_RESENT,
5825686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
599055Seric 	"sender",		H_FROM,
609055Seric 	"from",			H_FROM,
6125686Seric 	"reply-to",		H_FROM,
629055Seric 	"full-name",		H_ACHECK,
6357359Seric 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
6457359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6558796Seric 
668060Seric 		/* destination fields */
679055Seric 	"to",			H_RCPT,
6811417Seric 	"resent-to",		H_RCPT|H_RESENT,
699055Seric 	"cc",			H_RCPT,
7011417Seric 	"resent-cc",		H_RCPT|H_RESENT,
719055Seric 	"bcc",			H_RCPT|H_ACHECK,
7211417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7356215Seric 	"apparently-to",	H_RCPT,
7458796Seric 
758060Seric 		/* message identification and control */
7611417Seric 	"message-id",		0,
7711417Seric 	"resent-message-id",	H_RESENT,
789055Seric 	"message",		H_EOH,
799055Seric 	"text",			H_EOH,
8058796Seric 
8111417Seric 		/* date fields */
8211417Seric 	"date",			0,
8311417Seric 	"resent-date",		H_RESENT,
8458796Seric 
858060Seric 		/* trace fields */
869055Seric 	"received",		H_TRACE|H_FORCE,
8759318Seric 	"x400-received",	H_TRACE|H_FORCE,
889055Seric 	"via",			H_TRACE|H_FORCE,
899055Seric 	"mail-from",		H_TRACE|H_FORCE,
908060Seric 
9158796Seric 		/* miscellaneous fields */
9258796Seric 	"comments",		H_FORCE,
9363753Seric 	"return-path",		H_FORCE|H_ACHECK,
9458796Seric 
959055Seric 	NULL,			0,
962897Seric };
974166Seric 
984166Seric 
994166Seric 
1004282Seric /*
1014282Seric **  Location of system files/databases/etc.
1024282Seric */
1034282Seric 
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 */
134*64644Seric 
135*64644Seric 
136*64644Seric /*
137*64644Seric **  Following should be config parameters (and probably will be in
138*64644Seric **  future releases).  In the meantime, setting these is considered
139*64644Seric **  unsupported, and is intentionally undocumented.
140*64644Seric */
141*64644Seric 
142*64644Seric #ifdef BROKENSMTPPEERS
143*64644Seric bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
144*64644Seric #else
145*64644Seric bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
146*64644Seric #endif
147*64644Seric #ifdef NOLOOPBACKCHECK
148*64644Seric bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
149*64644Seric #else
150*64644Seric bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
151*64644Seric #endif
152*64644Seric 
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 /*
56414872Seric **  GETLA -- get the current load average
56514872Seric **
56614881Seric **	This code stolen from la.c.
56714881Seric **
56814872Seric **	Parameters:
56914872Seric **		none.
57014872Seric **
57114872Seric **	Returns:
57214872Seric **		The current load average as an integer.
57314872Seric **
57414872Seric **	Side Effects:
57514872Seric **		none.
57614872Seric */
57714872Seric 
57851920Seric /* try to guess what style of load average we have */
57951920Seric #define LA_ZERO		1	/* always return load average as zero */
58064360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
58151920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
58251920Seric #define LA_SUBR		4	/* call getloadavg */
58364295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
58464360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
58514872Seric 
58663962Seric /* do guesses based on general OS type */
58751920Seric #ifndef LA_TYPE
58863962Seric # define LA_TYPE	LA_ZERO
58951920Seric #endif
59051920Seric 
59164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
59251920Seric 
59314872Seric #include <nlist.h>
59414872Seric 
59551920Seric #ifndef LA_AVENRUN
59663962Seric # ifdef SYSTEM5
59763962Seric #  define LA_AVENRUN	"avenrun"
59863962Seric # else
59963962Seric #  define LA_AVENRUN	"_avenrun"
60063962Seric # endif
60151920Seric #endif
60251920Seric 
60351920Seric /* _PATH_UNIX should be defined in <paths.h> */
60451920Seric #ifndef _PATH_UNIX
60563962Seric # if defined(SYSTEM5)
60663962Seric #  define _PATH_UNIX	"/unix"
60763962Seric # else
60863962Seric #  define _PATH_UNIX	"/vmunix"
60963962Seric # endif
61051920Seric #endif
61151920Seric 
61214872Seric struct	nlist Nl[] =
61314872Seric {
61451920Seric 	{ LA_AVENRUN },
61514872Seric #define	X_AVENRUN	0
61614872Seric 	{ 0 },
61714872Seric };
61814872Seric 
61960583Seric #ifndef FSHIFT
62060583Seric # if defined(unixpc)
62160583Seric #  define FSHIFT	5
62260583Seric # endif
62357736Seric 
62460583Seric # if defined(__alpha)
62560583Seric #  define FSHIFT	10
62660583Seric # endif
62757977Seric 
62864360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
62951920Seric #  define FSHIFT	8
63060583Seric # endif
63157736Seric #endif
63260583Seric 
63364360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
63451920Seric #  define FSCALE	(1 << FSHIFT)
63551920Seric #endif
63640930Srick 
63714872Seric getla()
63814872Seric {
63914872Seric 	static int kmem = -1;
64051920Seric #if LA_TYPE == LA_INT
64124943Seric 	long avenrun[3];
64251920Seric #else
64364360Seric # if LA_TYPE == LA_SHORT
64464360Seric 	short avenrun[3];
64564360Seric # else
64651920Seric 	double avenrun[3];
64764360Seric # endif
64851920Seric #endif
64925615Seric 	extern off_t lseek();
65057736Seric 	extern int errno;
65114872Seric 
65214872Seric 	if (kmem < 0)
65314872Seric 	{
65424945Seric 		kmem = open("/dev/kmem", 0, 0);
65514872Seric 		if (kmem < 0)
65657736Seric 		{
65757736Seric 			if (tTd(3, 1))
65857736Seric 				printf("getla: open(/dev/kmem): %s\n",
65957736Seric 					errstring(errno));
66014872Seric 			return (-1);
66157736Seric 		}
66251920Seric 		(void) fcntl(kmem, F_SETFD, 1);
66357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
66457736Seric 		{
66557736Seric 			if (tTd(3, 1))
66657736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
66757736Seric 					errstring(errno));
66814872Seric 			return (-1);
66957736Seric 		}
67059253Seric 		if (Nl[X_AVENRUN].n_value == 0)
67159253Seric 		{
67259253Seric 			if (tTd(3, 1))
67359253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
67459253Seric 					_PATH_UNIX, LA_AVENRUN);
67559253Seric 			return (-1);
67659253Seric 		}
67714872Seric 	}
67857736Seric 	if (tTd(3, 20))
67957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
68024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
68123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
68219967Seric 	{
68319967Seric 		/* thank you Ian */
68457736Seric 		if (tTd(3, 1))
68557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
68619967Seric 		return (-1);
68719967Seric 	}
68864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
68957736Seric 	if (tTd(3, 5))
69057736Seric 	{
69157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
69257736Seric 		if (tTd(3, 15))
69357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
69457736Seric 		printf("\n");
69557736Seric 	}
69657736Seric 	if (tTd(3, 1))
69757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
69951920Seric #else
70057736Seric 	if (tTd(3, 5))
70157736Seric 	{
70257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
70357736Seric 		if (tTd(3, 15))
70457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
70557736Seric 		printf("\n");
70657736Seric 	}
70757736Seric 	if (tTd(3, 1))
70857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
70951920Seric 	return ((int) (avenrun[0] + 0.5));
71051920Seric #endif
71114872Seric }
71214872Seric 
71351773Seric #else
71451920Seric #if LA_TYPE == LA_SUBR
71551773Seric 
71651773Seric getla()
71751773Seric {
71851920Seric 	double avenrun[3];
71951920Seric 
72051920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
72157736Seric 	{
72257736Seric 		if (tTd(3, 1))
72357736Seric 			perror("getla: getloadavg failed:");
72451920Seric 		return (-1);
72557736Seric 	}
72657736Seric 	if (tTd(3, 1))
72757736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72851920Seric 	return ((int) (avenrun[0] + 0.5));
72951773Seric }
73051773Seric 
73151773Seric #else
73264295Seric #if LA_TYPE == LA_MACH
73351773Seric 
73464295Seric /*
73564295Seric **  This has been tested on NeXT release 2.1.
73664295Seric */
73764295Seric 
73864295Seric #include <mach.h>
73964295Seric 
74051773Seric getla()
74151773Seric {
74264295Seric 	processor_set_t default_set;
74364295Seric 	kern_return_t error;
74464295Seric 	unsigned int info_count;
74564295Seric 	struct processor_set_basic_info info;
74664295Seric 	host_t host;
74764295Seric 
74864295Seric 	error = processor_set_default(host_self(), &default_set);
74964295Seric 	if (error != KERN_SUCCESS)
75064295Seric 		return -1;
75164295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
75264295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
75364295Seric 			       &host, (processor_set_info_t)&info,
75464295Seric 			       &info_count) != KERN_SUCCESS)
75564295Seric 	{
75664295Seric 		return -1;
75764295Seric 	}
75864295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
75964295Seric }
76064295Seric 
76164295Seric 
76264295Seric #else
76364295Seric 
76464295Seric getla()
76564295Seric {
76657736Seric 	if (tTd(3, 1))
76757736Seric 		printf("getla: ZERO\n");
76851920Seric 	return (0);
76951773Seric }
77051773Seric 
77151773Seric #endif
77251773Seric #endif
77364295Seric #endif
77424943Seric /*
77524943Seric **  SHOULDQUEUE -- should this message be queued or sent?
77624943Seric **
77724943Seric **	Compares the message cost to the load average to decide.
77824943Seric **
77924943Seric **	Parameters:
78024943Seric **		pri -- the priority of the message in question.
78157438Seric **		ctime -- the message creation time.
78224943Seric **
78324943Seric **	Returns:
78424943Seric **		TRUE -- if this message should be queued up for the
78524943Seric **			time being.
78624943Seric **		FALSE -- if the load is low enough to send this message.
78724943Seric **
78824943Seric **	Side Effects:
78924943Seric **		none.
79024943Seric */
79124943Seric 
79224943Seric bool
79357438Seric shouldqueue(pri, ctime)
79424943Seric 	long pri;
79557438Seric 	time_t ctime;
79624943Seric {
79751920Seric 	if (CurrentLA < QueueLA)
79824943Seric 		return (FALSE);
79958132Seric 	if (CurrentLA >= RefuseLA)
80058132Seric 		return (TRUE);
80151920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
80224943Seric }
80324943Seric /*
80453037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
80553037Seric **
80653037Seric **	Parameters:
80753037Seric **		none.
80853037Seric **
80953037Seric **	Returns:
81053037Seric **		TRUE if incoming SMTP connections should be refused
81153037Seric **			(for now).
81253037Seric **		FALSE if we should accept new work.
81353037Seric **
81453037Seric **	Side Effects:
81553037Seric **		none.
81653037Seric */
81753037Seric 
81853037Seric bool
81953037Seric refuseconnections()
82053037Seric {
82159156Seric #ifdef XLA
82259156Seric 	if (!xla_smtp_ok())
82359156Seric 		return TRUE;
82459156Seric #endif
82559156Seric 
82653037Seric 	/* this is probably too simplistic */
82758132Seric 	return (CurrentLA >= RefuseLA);
82853037Seric }
82953037Seric /*
83024943Seric **  SETPROCTITLE -- set process title for ps
83124943Seric **
83224943Seric **	Parameters:
83358674Seric **		fmt -- a printf style format string.
83458674Seric **		a, b, c -- possible parameters to fmt.
83524943Seric **
83624943Seric **	Returns:
83724943Seric **		none.
83824943Seric **
83924943Seric **	Side Effects:
84024943Seric **		Clobbers argv of our main procedure so ps(1) will
84124943Seric **		display the title.
84224943Seric */
84324943Seric 
84458689Seric #ifdef SETPROCTITLE
84558689Seric # ifdef __hpux
84658689Seric #  include <sys/pstat.h>
84758689Seric # endif
84859732Seric # ifdef BSD4_4
84959732Seric #  include <machine/vmparam.h>
85059732Seric #  include <sys/exec.h>
85164247Seric #  ifdef __bsdi__
85264247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
85364247Seric #  endif
85459992Seric #  ifdef PS_STRINGS
85559992Seric #   define SETPROC_STATIC static
85659992Seric #  endif
85759732Seric # endif
85859732Seric # ifndef SETPROC_STATIC
85959732Seric #  define SETPROC_STATIC
86059732Seric # endif
86158689Seric #endif
86258689Seric 
86324943Seric /*VARARGS1*/
86457642Seric #ifdef __STDC__
86557642Seric setproctitle(char *fmt, ...)
86657642Seric #else
86757642Seric setproctitle(fmt, va_alist)
86824943Seric 	char *fmt;
86957642Seric 	va_dcl
87057642Seric #endif
87124943Seric {
87224943Seric # ifdef SETPROCTITLE
87324943Seric 	register char *p;
87425049Seric 	register int i;
87559732Seric 	SETPROC_STATIC char buf[MAXLINE];
87656852Seric 	VA_LOCAL_DECL
87758689Seric #  ifdef __hpux
87858689Seric 	union pstun pst;
87958689Seric #  endif
88024943Seric 	extern char **Argv;
88124943Seric 	extern char *LastArgv;
88224943Seric 
88358674Seric 	p = buf;
88424943Seric 
88558674Seric 	/* print sendmail: heading for grep */
88658674Seric 	(void) strcpy(p, "sendmail: ");
88758674Seric 	p += strlen(p);
88824943Seric 
88958674Seric 	/* print the argument string */
89058674Seric 	VA_START(fmt);
89158674Seric 	(void) vsprintf(p, fmt, ap);
89256852Seric 	VA_END;
89354996Seric 
89458674Seric 	i = strlen(buf);
89558689Seric 
89658689Seric #  ifdef __hpux
89758689Seric 	pst.pst_command = buf;
89858689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
89958689Seric #  else
90059992Seric #   ifdef PS_STRINGS
90159732Seric 	PS_STRINGS->ps_nargvstr = 1;
90259732Seric 	PS_STRINGS->ps_argvstr = buf;
90359732Seric #   else
90454996Seric 	if (i > LastArgv - Argv[0] - 2)
90525049Seric 	{
90654996Seric 		i = LastArgv - Argv[0] - 2;
90758674Seric 		buf[i] = '\0';
90825049Seric 	}
90958674Seric 	(void) strcpy(Argv[0], buf);
91054997Seric 	p = &Argv[0][i];
91124943Seric 	while (p < LastArgv)
91224943Seric 		*p++ = ' ';
91359732Seric #   endif
91458689Seric #  endif
91556795Seric # endif /* SETPROCTITLE */
91624943Seric }
91725698Seric /*
91825698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
91925698Seric **
92025698Seric **	Parameters:
92125698Seric **		none.
92225698Seric **
92325698Seric **	Returns:
92425698Seric **		none.
92525698Seric **
92625698Seric **	Side Effects:
92725698Seric **		Picks up extant zombies.
92825698Seric */
92925698Seric 
93046928Sbostic void
93125698Seric reapchild()
93225698Seric {
93363966Seric # ifdef HASWAITPID
93460219Seric 	auto int status;
93560560Seric 	int count;
93660560Seric 	int pid;
93760219Seric 
93860560Seric 	count = 0;
93960560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
94060560Seric 	{
94160560Seric 		if (count++ > 1000)
94260560Seric 		{
94360560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
94460560Seric 				pid, status);
94560560Seric 			break;
94660560Seric 		}
94760560Seric 	}
94860219Seric # else
94925698Seric # ifdef WNOHANG
95025698Seric 	union wait status;
95125698Seric 
95263753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
95325698Seric 		continue;
95456795Seric # else /* WNOHANG */
95525698Seric 	auto int status;
95625698Seric 
95760219Seric 	while (wait(&status) > 0)
95825698Seric 		continue;
95956795Seric # endif /* WNOHANG */
96060219Seric # endif
96164035Seric # ifdef SYS5SIGNALS
96264035Seric 	(void) setsignal(SIGCHLD, reapchild);
96358061Seric # endif
96425698Seric }
96555418Seric /*
96655418Seric **  UNSETENV -- remove a variable from the environment
96755418Seric **
96855418Seric **	Not needed on newer systems.
96955418Seric **
97055418Seric **	Parameters:
97155418Seric **		name -- the string name of the environment variable to be
97255418Seric **			deleted from the current environment.
97355418Seric **
97455418Seric **	Returns:
97555418Seric **		none.
97655418Seric **
97755418Seric **	Globals:
97855418Seric **		environ -- a pointer to the current environment.
97955418Seric **
98055418Seric **	Side Effects:
98155418Seric **		Modifies environ.
98255418Seric */
98355418Seric 
98463962Seric #ifndef HASUNSETENV
98555418Seric 
98655418Seric void
98755418Seric unsetenv(name)
98855418Seric 	char *name;
98955418Seric {
99055418Seric 	extern char **environ;
99155418Seric 	register char **pp;
99255418Seric 	int len = strlen(name);
99355418Seric 
99455418Seric 	for (pp = environ; *pp != NULL; pp++)
99555418Seric 	{
99655418Seric 		if (strncmp(name, *pp, len) == 0 &&
99755418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
99855418Seric 			break;
99955418Seric 	}
100055418Seric 
100155418Seric 	for (; *pp != NULL; pp++)
100255418Seric 		*pp = pp[1];
100355418Seric }
100455418Seric 
100563962Seric #endif
100656215Seric /*
100756215Seric **  GETDTABLESIZE -- return number of file descriptors
100856215Seric **
100956215Seric **	Only on non-BSD systems
101056215Seric **
101156215Seric **	Parameters:
101256215Seric **		none
101356215Seric **
101456215Seric **	Returns:
101556215Seric **		size of file descriptor table
101656215Seric **
101756215Seric **	Side Effects:
101856215Seric **		none
101956215Seric */
102056215Seric 
102163787Seric #ifdef SOLARIS
102263787Seric # include <sys/resource.h>
102363787Seric #endif
102456215Seric 
102556215Seric int
102663787Seric getdtsize()
102756215Seric {
102863787Seric #ifdef RLIMIT_NOFILE
102963787Seric 	struct rlimit rl;
103063787Seric 
103163787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
103263787Seric 		return rl.rlim_cur;
103363787Seric #endif
103463787Seric 
103564031Seric # ifdef HASGETDTABLESIZE
103664031Seric 	return getdtablesize();
103764031Seric # else
103864031Seric #  ifdef _SC_OPEN_MAX
103958689Seric 	return sysconf(_SC_OPEN_MAX);
104063787Seric #  else
104156215Seric 	return NOFILE;
104263787Seric #  endif
104358689Seric # endif
104456215Seric }
104557631Seric /*
104657631Seric **  UNAME -- get the UUCP name of this system.
104757631Seric */
104857631Seric 
104957943Seric #ifndef HASUNAME
105057631Seric 
105157631Seric int
105257631Seric uname(name)
105357631Seric 	struct utsname *name;
105457631Seric {
105557631Seric 	FILE *file;
105657631Seric 	char *n;
105757631Seric 
105857631Seric 	name->nodename[0] = '\0';
105957631Seric 
106057661Seric 	/* try /etc/whoami -- one line with the node name */
106157631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
106257631Seric 	{
106357661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
106457631Seric 		(void) fclose(file);
106557661Seric 		n = strchr(name->nodename, '\n');
106657631Seric 		if (n != NULL)
106757631Seric 			*n = '\0';
106857631Seric 		if (name->nodename[0] != '\0')
106957631Seric 			return (0);
107057631Seric 	}
107157631Seric 
107257661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
107357631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
107457631Seric 	{
107557631Seric 		char buf[MAXLINE];
107657631Seric 
107757631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
107857631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
107957631Seric 					NODE_LENGTH, name->nodename) > 0)
108057631Seric 				break;
108157631Seric 		(void) fclose(file);
108257631Seric 		if (name->nodename[0] != '\0')
108357631Seric 			return (0);
108457631Seric 	}
108557631Seric 
108657631Seric #ifdef TRUST_POPEN
108757631Seric 	/*
108857631Seric 	**  Popen is known to have security holes.
108957631Seric 	*/
109057631Seric 
109157661Seric 	/* try uuname -l to return local name */
109257631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
109357631Seric 	{
109457661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
109557631Seric 		(void) pclose(file);
109657661Seric 		n = strchr(name, '\n');
109757631Seric 		if (n != NULL)
109857631Seric 			*n = '\0';
109957661Seric 		if (name->nodename[0] != '\0')
110057631Seric 			return (0);
110157631Seric 	}
110257631Seric #endif
110357631Seric 
110457631Seric 	return (-1);
110557631Seric }
110657943Seric #endif /* HASUNAME */
110758068Seric /*
110858068Seric **  INITGROUPS -- initialize groups
110958068Seric **
111058068Seric **	Stub implementation for System V style systems
111158068Seric */
111258068Seric 
111358068Seric #ifndef HASINITGROUPS
111458068Seric 
111558068Seric initgroups(name, basegid)
111658068Seric 	char *name;
111758068Seric 	int basegid;
111858068Seric {
111958068Seric 	return 0;
112058068Seric }
112158068Seric 
112258068Seric #endif
112358082Seric /*
112459289Seric **  SETSID -- set session id (for non-POSIX systems)
112559289Seric */
112659289Seric 
112759289Seric #ifndef HASSETSID
112859289Seric 
112963753Seric pid_t
113063753Seric setsid __P ((void))
113159289Seric {
113264155Seric #ifdef TIOCNOTTY
113364155Seric 	int fd;
113464155Seric 
113564155Seric 	fd = open("/dev/tty", 2);
113664155Seric 	if (fd >= 0)
113764155Seric 	{
113864155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
113964155Seric 		(void) close(fd);
114064155Seric 	}
114164155Seric #endif /* TIOCNOTTY */
114259289Seric # ifdef SYSTEM5
114363753Seric 	return setpgrp();
114463753Seric # else
114564155Seric 	return setpgid(0, getpid());
114659289Seric # endif
114759289Seric }
114859289Seric 
114959289Seric #endif
115059289Seric /*
115163969Seric **  GETOPT -- for old systems or systems with bogus implementations
115263969Seric */
115363969Seric 
115463969Seric #ifdef NEEDGETOPT
115563969Seric 
115663969Seric /*
115763969Seric  * Copyright (c) 1985 Regents of the University of California.
115863969Seric  * All rights reserved.  The Berkeley software License Agreement
115963969Seric  * specifies the terms and conditions for redistribution.
116063969Seric  */
116163969Seric 
116263969Seric 
116363969Seric /*
116463969Seric ** this version hacked to add `atend' flag to allow state machine
116563969Seric ** to reset if invoked by the program to scan args for a 2nd time
116663969Seric */
116763969Seric 
116863969Seric #if defined(LIBC_SCCS) && !defined(lint)
116963969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
117064072Seric #endif /* LIBC_SCCS and not lint */
117163969Seric 
117263969Seric #include <stdio.h>
117363969Seric 
117463969Seric /*
117563969Seric  * get option letter from argument vector
117663969Seric  */
117763969Seric int	opterr = 1,		/* if error message should be printed */
117863969Seric 	optind = 1,		/* index into parent argv vector */
117963969Seric 	optopt;			/* character checked for validity */
118063969Seric char	*optarg;		/* argument associated with option */
118163969Seric 
118263969Seric #define BADCH	(int)'?'
118363969Seric #define EMSG	""
118463969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
118563969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
118663969Seric 
118763969Seric getopt(nargc,nargv,ostr)
118863969Seric int	nargc;
118963969Seric char	**nargv,
119063969Seric 	*ostr;
119163969Seric {
119263969Seric 	static char	*place = EMSG;	/* option letter processing */
119363969Seric 	static char	atend = 0;
119463969Seric 	register char	*oli;		/* option letter list index */
119563969Seric 
119663969Seric 	if (atend) {
119763969Seric 		atend = 0;
119863969Seric 		place = EMSG;
119963969Seric 	}
120063969Seric 	if(!*place) {			/* update scanning pointer */
120164136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
120263969Seric 			atend++;
120363969Seric 			return(EOF);
120463969Seric 		}
120563969Seric 		if (*place == '-') {	/* found "--" */
120663969Seric 			++optind;
120763969Seric 			atend++;
120863969Seric 			return(EOF);
120963969Seric 		}
121063969Seric 	}				/* option letter okay? */
121164125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
121264136Seric 		if (!*place) ++optind;
121363969Seric 		tell(": illegal option -- ");
121463969Seric 	}
121563969Seric 	if (*++oli != ':') {		/* don't need argument */
121663969Seric 		optarg = NULL;
121763969Seric 		if (!*place) ++optind;
121863969Seric 	}
121963969Seric 	else {				/* need an argument */
122063969Seric 		if (*place) optarg = place;	/* no white space */
122163969Seric 		else if (nargc <= ++optind) {	/* no arg */
122263969Seric 			place = EMSG;
122363969Seric 			tell(": option requires an argument -- ");
122463969Seric 		}
122563969Seric 	 	else optarg = nargv[optind];	/* white space */
122663969Seric 		place = EMSG;
122763969Seric 		++optind;
122863969Seric 	}
122963969Seric 	return(optopt);			/* dump back option letter */
123063969Seric }
123163969Seric 
123263969Seric #endif
123363969Seric /*
123463969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
123563969Seric */
123663969Seric 
123763969Seric #ifdef NEEDVPRINTF
123863969Seric 
123963969Seric #define MAXARG	16
124063969Seric 
124163969Seric vfprintf(fp, fmt, ap)
124263969Seric 	FILE *	fp;
124363969Seric 	char *	fmt;
124463969Seric 	char **	ap;
124563969Seric {
124663969Seric 	char *	bp[MAXARG];
124763969Seric 	int	i = 0;
124863969Seric 
124963969Seric 	while (*ap && i < MAXARG)
125063969Seric 		bp[i++] = *ap++;
125163969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
125263969Seric 			 bp[4], bp[5], bp[6], bp[7],
125363969Seric 			 bp[8], bp[9], bp[10], bp[11],
125463969Seric 			 bp[12], bp[13], bp[14], bp[15]);
125563969Seric }
125663969Seric 
125763969Seric vsprintf(s, fmt, ap)
125863969Seric 	char *	s;
125963969Seric 	char *	fmt;
126063969Seric 	char **	ap;
126163969Seric {
126263969Seric 	char *	bp[MAXARG];
126363969Seric 	int	i = 0;
126463969Seric 
126563969Seric 	while (*ap && i < MAXARG)
126663969Seric 		bp[i++] = *ap++;
126763969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
126863969Seric 			bp[4], bp[5], bp[6], bp[7],
126963969Seric 			bp[8], bp[9], bp[10], bp[11],
127063969Seric 			bp[12], bp[13], bp[14], bp[15]);
127163969Seric }
127263969Seric 
127363969Seric #endif
127463969Seric /*
127564311Seric **  FREESPACE -- see how much free space is on the queue filesystem
127658082Seric **
127758082Seric **	Only implemented if you have statfs.
127858082Seric **
127958082Seric **	Parameters:
128064311Seric **		dir -- the directory in question.
128164311Seric **		bsize -- a variable into which the filesystem
128264311Seric **			block size is stored.
128358082Seric **
128458082Seric **	Returns:
128564311Seric **		The number of bytes free on the queue filesystem.
128664311Seric **		-1 if the statfs call fails.
128764311Seric **
128864311Seric **	Side effects:
128964311Seric **		Puts the filesystem block size into bsize.
129058082Seric */
129158082Seric 
129258082Seric #ifdef HASSTATFS
129358157Seric # undef HASUSTAT
129458157Seric #endif
129558157Seric 
129658157Seric #if defined(HASUSTAT)
129758157Seric # include <ustat.h>
129858157Seric #endif
129958157Seric 
130058157Seric #ifdef HASSTATFS
130164314Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_)
130258133Seric #  include <sys/statfs.h>
130358133Seric # else
130464380Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
130558133Seric #   include <sys/vfs.h>
130658133Seric #  else
130758157Seric #   include <sys/mount.h>
130858133Seric #  endif
130958133Seric # endif
131058082Seric #endif
131158082Seric 
131264311Seric long
131364311Seric freespace(dir, bsize)
131464311Seric 	char *dir;
131564311Seric 	long *bsize;
131658082Seric {
131758160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
131858157Seric # if defined(HASUSTAT)
131958153Seric 	struct ustat fs;
132058153Seric 	struct stat statbuf;
132158366Seric #  define FSBLOCKSIZE	DEV_BSIZE
132258157Seric #  define f_bavail	f_tfree
132358157Seric # else
132458157Seric #  if defined(ultrix)
132558157Seric 	struct fs_data fs;
132658157Seric #   define f_bavail	fd_bfreen
132758366Seric #   define FSBLOCKSIZE	fs.fd_bsize
132858153Seric #  else
132958082Seric 	struct statfs fs;
133058366Seric #   define FSBLOCKSIZE	fs.f_bsize
133164336Seric #   if defined(_SCO_unix_) || defined(IRIX)
133263987Seric #    define f_bavail f_bfree
133363987Seric #   endif
133458153Seric #  endif
133558133Seric # endif
133658082Seric 	extern int errno;
133758082Seric 
133858157Seric # if defined(HASUSTAT)
133964311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
134058157Seric # else
134164314Seric #  if defined(IRIX) || defined(apollo)
134264311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
134358157Seric #  else
134458157Seric #   if defined(ultrix)
134564311Seric 	if (statfs(dir, &fs) > 0)
134658153Seric #   else
134764311Seric 	if (statfs(dir, &fs) == 0)
134858153Seric #   endif
134958133Seric #  endif
135058133Seric # endif
135158133Seric 	{
135264311Seric 		if (bsize != NULL)
135364311Seric 			*bsize = FSBLOCKSIZE;
135464311Seric 		return (fs.f_bavail);
135564311Seric 	}
135664337Seric #endif
135764311Seric 	return (-1);
135864311Seric }
135964311Seric /*
136064311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
136164311Seric **
136264311Seric **	Only implemented if you have statfs.
136364311Seric **
136464311Seric **	Parameters:
136564311Seric **		msize -- the size to check against.  If zero, we don't yet
136664311Seric **		know how big the message will be, so just check for
136764311Seric **		a "reasonable" amount.
136864311Seric **
136964311Seric **	Returns:
137064311Seric **		TRUE if there is enough space.
137164311Seric **		FALSE otherwise.
137264311Seric */
137364311Seric 
137464311Seric bool
137564311Seric enoughspace(msize)
137664311Seric 	long msize;
137764311Seric {
137864311Seric 	long bfree, bsize;
137964311Seric 
138064311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
138164311Seric 	{
138258133Seric 		if (tTd(4, 80))
138364311Seric 			printf("enoughspace: no threshold\n");
138464311Seric 		return TRUE;
138564311Seric 	}
138664311Seric 
138764311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
138864311Seric 	{
138964311Seric 		if (tTd(4, 80))
139058333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
139164311Seric 				bfree, msize);
139258333Seric 
139358333Seric 		/* convert msize to block count */
139464311Seric 		msize = msize / bsize + 1;
139558333Seric 		if (MinBlocksFree >= 0)
139658333Seric 			msize += MinBlocksFree;
139758333Seric 
139864311Seric 		if (bfree < msize)
139958090Seric 		{
140058090Seric #ifdef LOG
140158090Seric 			if (LogLevel > 0)
140264254Seric 				syslog(LOG_ALERT,
140364254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
140464311Seric 					CurEnv->e_id, bfree,
140564254Seric 					CurHostName, msize, QueueDir);
140658090Seric #endif
140758082Seric 			return FALSE;
140858090Seric 		}
140958082Seric 	}
141058082Seric 	else if (tTd(4, 80))
141158333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
141258333Seric 			MinBlocksFree, msize, errstring(errno));
141358082Seric 	return TRUE;
141458082Seric }
141558542Seric /*
141658542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
141758542Seric **
141858542Seric **	This looks at an errno value and tells if this is likely to
141958542Seric **	go away if retried later.
142058542Seric **
142158542Seric **	Parameters:
142258542Seric **		err -- the errno code to classify.
142358542Seric **
142458542Seric **	Returns:
142558542Seric **		TRUE if this is probably transient.
142658542Seric **		FALSE otherwise.
142758542Seric */
142858542Seric 
142958542Seric bool
143058542Seric transienterror(err)
143158542Seric 	int err;
143258542Seric {
143358542Seric 	switch (err)
143458542Seric 	{
143558542Seric 	  case EIO:			/* I/O error */
143658542Seric 	  case ENXIO:			/* Device not configured */
143758542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
143858542Seric 	  case ENOMEM:			/* Cannot allocate memory */
143958542Seric 	  case ENODEV:			/* Operation not supported by device */
144058542Seric 	  case ENFILE:			/* Too many open files in system */
144158542Seric 	  case EMFILE:			/* Too many open files */
144258542Seric 	  case ENOSPC:			/* No space left on device */
144358542Seric #ifdef ETIMEDOUT
144458542Seric 	  case ETIMEDOUT:		/* Connection timed out */
144558542Seric #endif
144658542Seric #ifdef ESTALE
144758542Seric 	  case ESTALE:			/* Stale NFS file handle */
144858542Seric #endif
144958542Seric #ifdef ENETDOWN
145058542Seric 	  case ENETDOWN:		/* Network is down */
145158542Seric #endif
145258542Seric #ifdef ENETUNREACH
145358542Seric 	  case ENETUNREACH:		/* Network is unreachable */
145458542Seric #endif
145558542Seric #ifdef ENETRESET
145658542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
145758542Seric #endif
145858542Seric #ifdef ECONNABORTED
145958542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
146058542Seric #endif
146158542Seric #ifdef ECONNRESET
146258542Seric 	  case ECONNRESET:		/* Connection reset by peer */
146358542Seric #endif
146458542Seric #ifdef ENOBUFS
146558542Seric 	  case ENOBUFS:			/* No buffer space available */
146658542Seric #endif
146758542Seric #ifdef ESHUTDOWN
146858542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
146958542Seric #endif
147058542Seric #ifdef ECONNREFUSED
147158542Seric 	  case ECONNREFUSED:		/* Connection refused */
147258542Seric #endif
147358542Seric #ifdef EHOSTDOWN
147458542Seric 	  case EHOSTDOWN:		/* Host is down */
147558542Seric #endif
147658542Seric #ifdef EHOSTUNREACH
147758542Seric 	  case EHOSTUNREACH:		/* No route to host */
147858542Seric #endif
147958542Seric #ifdef EDQUOT
148058542Seric 	  case EDQUOT:			/* Disc quota exceeded */
148158542Seric #endif
148258542Seric #ifdef EPROCLIM
148358542Seric 	  case EPROCLIM:		/* Too many processes */
148458542Seric #endif
148558542Seric #ifdef EUSERS
148658542Seric 	  case EUSERS:			/* Too many users */
148758542Seric #endif
148858542Seric #ifdef EDEADLK
148958542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
149058542Seric #endif
149158542Seric #ifdef EISCONN
149258542Seric 	  case EISCONN:			/* Socket already connected */
149358542Seric #endif
149458542Seric #ifdef EINPROGRESS
149558542Seric 	  case EINPROGRESS:		/* Operation now in progress */
149658542Seric #endif
149758542Seric #ifdef EALREADY
149858542Seric 	  case EALREADY:		/* Operation already in progress */
149958542Seric #endif
150058542Seric #ifdef EADDRINUSE
150158542Seric 	  case EADDRINUSE:		/* Address already in use */
150258542Seric #endif
150358542Seric #ifdef EADDRNOTAVAIL
150458542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
150558542Seric #endif
150663834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
150758542Seric 	  case ENOSR:			/* Out of streams resources */
150858542Seric #endif
150958542Seric 		return TRUE;
151058542Seric 	}
151158542Seric 
151258542Seric 	/* nope, must be permanent */
151358542Seric 	return FALSE;
151458542Seric }
151558689Seric /*
151664035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
151758689Seric **
151858689Seric **	Parameters:
151958689Seric **		fd -- the file descriptor of the file.
152058689Seric **		filename -- the file name (for error messages).
152164335Seric **		ext -- the filename extension.
152258689Seric **		type -- type of the lock.  Bits can be:
152358689Seric **			LOCK_EX -- exclusive lock.
152458689Seric **			LOCK_NB -- non-blocking.
152558689Seric **
152658689Seric **	Returns:
152758689Seric **		TRUE if the lock was acquired.
152858689Seric **		FALSE otherwise.
152958689Seric */
153058689Seric 
153158689Seric bool
153264335Seric lockfile(fd, filename, ext, type)
153358689Seric 	int fd;
153458689Seric 	char *filename;
153564335Seric 	char *ext;
153658689Seric 	int type;
153758689Seric {
153864035Seric # ifndef HASFLOCK
153958689Seric 	int action;
154058689Seric 	struct flock lfd;
154164335Seric 
154264335Seric 	if (ext == NULL)
154364335Seric 		ext = "";
154464035Seric 
154563983Seric 	bzero(&lfd, sizeof lfd);
154659447Seric 	if (bitset(LOCK_UN, type))
154759447Seric 		lfd.l_type = F_UNLCK;
154859447Seric 	else if (bitset(LOCK_EX, type))
154958689Seric 		lfd.l_type = F_WRLCK;
155058689Seric 	else
155158689Seric 		lfd.l_type = F_RDLCK;
155258689Seric 
155358689Seric 	if (bitset(LOCK_NB, type))
155458689Seric 		action = F_SETLK;
155558689Seric 	else
155658689Seric 		action = F_SETLKW;
155758689Seric 
155864264Seric 	if (tTd(55, 60))
155964335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
156064335Seric 			filename, ext, action, lfd.l_type);
156164264Seric 
156258689Seric 	if (fcntl(fd, action, &lfd) >= 0)
156364264Seric 	{
156464264Seric 		if (tTd(55, 60))
156564264Seric 			printf("SUCCESS\n");
156658689Seric 		return TRUE;
156764264Seric 	}
156858689Seric 
156964264Seric 	if (tTd(55, 60))
157064264Seric 		printf("(%s) ", errstring(errno));
157164264Seric 
157264136Seric 	/*
157364136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
157464136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
157564136Seric 	**  as type "tmp" (that is, served from swap space), the
157664136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
157764136Seric 	**  Since this is fairly common during testing, we will assume
157864136Seric 	**  that this indicates that the lock is successfully grabbed.
157964136Seric 	*/
158064136Seric 
158164136Seric 	if (errno == EINVAL)
158264264Seric 	{
158364264Seric 		if (tTd(55, 60))
158464264Seric 			printf("SUCCESS\n");
158564136Seric 		return TRUE;
158664264Seric 	}
158764136Seric 
158858689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
158964378Seric 	{
159064378Seric 		int omode = -1;
159164378Seric #  ifdef F_GETFL
159264378Seric 		int oerrno = errno;
159364378Seric 
159464378Seric 		(void) fcntl(fd, F_GETFL, &omode);
159564378Seric 		errno = oerrno;
159664378Seric #  endif
159764378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
159864378Seric 			filename, ext, fd, type, omode, geteuid());
159964378Seric 	}
160058689Seric # else
160164335Seric 	if (ext == NULL)
160264335Seric 		ext = "";
160364335Seric 
160464264Seric 	if (tTd(55, 60))
160564335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
160664264Seric 
160758689Seric 	if (flock(fd, type) >= 0)
160864264Seric 	{
160964264Seric 		if (tTd(55, 60))
161064264Seric 			printf("SUCCESS\n");
161158689Seric 		return TRUE;
161264264Seric 	}
161358689Seric 
161464264Seric 	if (tTd(55, 60))
161564264Seric 		printf("(%s) ", errstring(errno));
161664264Seric 
161758689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
161864378Seric 	{
161964378Seric 		int omode = -1;
162064378Seric #  ifdef F_GETFL
162164378Seric 		int oerrno = errno;
162264378Seric 
162364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
162464378Seric 		errno = oerrno;
162564378Seric #  endif
162664378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
162764378Seric 			filename, ext, fd, type, omode, geteuid());
162864386Seric 	}
162958689Seric # endif
163064264Seric 	if (tTd(55, 60))
163164264Seric 		printf("FAIL\n");
163258689Seric 	return FALSE;
163358689Seric }
163464556Seric /*
163564556Seric **  GETCFNAME -- return the name of the .cf file.
163664556Seric **
163764556Seric **	Some systems (e.g., NeXT) determine this dynamically.
163864556Seric */
163964556Seric 
164064556Seric char *
164164556Seric getcfname()
164264556Seric {
164364556Seric 	if (ConfFile != NULL)
164464556Seric 		return ConfFile;
164564556Seric 	return _PATH_SENDMAILCF;
164664556Seric }
1647