xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64705)
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*64705Seric static char sccsid[] = "@(#)conf.c	8.40 (Berkeley) 10/08/93";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1736928Sbostic # include <pwd.h>
18404Seric 
19294Seric /*
203309Seric **  CONF.C -- Sendmail Configuration Tables.
21294Seric **
22294Seric **	Defines the configuration of this installation.
23294Seric **
241388Seric **	Configuration Variables:
252897Seric **		HdrInfo -- a table describing well-known header fields.
262897Seric **			Each entry has the field name and some flags,
274147Seric **			which are described in sendmail.h.
284093Seric **
294093Seric **	Notes:
304093Seric **		I have tried to put almost all the reasonable
314093Seric **		configuration information into the configuration
324093Seric **		file read at runtime.  My intent is that anything
334093Seric **		here is a function of the version of UNIX you
344093Seric **		are running, or is really static -- for example
354093Seric **		the headers are a superset of widely used
364093Seric **		protocols.  If you find yourself playing with
374093Seric **		this file too much, you may be making a mistake!
38294Seric */
39294Seric 
40294Seric 
41294Seric 
42294Seric 
434437Seric /*
442897Seric **  Header info table
453057Seric **	Final (null) entry contains the flags used for any other field.
464147Seric **
474147Seric **	Not all of these are actually handled specially by sendmail
484147Seric **	at this time.  They are included as placeholders, to let
494147Seric **	you know that "someday" I intend to have sendmail do
504147Seric **	something with them.
512897Seric */
522897Seric 
532897Seric struct hdrinfo	HdrInfo[] =
542897Seric {
558060Seric 		/* originator fields, most to least significant  */
5611417Seric 	"resent-sender",	H_FROM|H_RESENT,
5711417Seric 	"resent-from",		H_FROM|H_RESENT,
5825686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
599055Seric 	"sender",		H_FROM,
609055Seric 	"from",			H_FROM,
6125686Seric 	"reply-to",		H_FROM,
629055Seric 	"full-name",		H_ACHECK,
6364682Seric 	"return-receipt-to",	H_FROM|H_RECEIPTTO,
6457359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6558796Seric 
668060Seric 		/* destination fields */
679055Seric 	"to",			H_RCPT,
6811417Seric 	"resent-to",		H_RCPT|H_RESENT,
699055Seric 	"cc",			H_RCPT,
7011417Seric 	"resent-cc",		H_RCPT|H_RESENT,
719055Seric 	"bcc",			H_RCPT|H_ACHECK,
7211417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7356215Seric 	"apparently-to",	H_RCPT,
7458796Seric 
758060Seric 		/* message identification and control */
7611417Seric 	"message-id",		0,
7711417Seric 	"resent-message-id",	H_RESENT,
789055Seric 	"message",		H_EOH,
799055Seric 	"text",			H_EOH,
8058796Seric 
8111417Seric 		/* date fields */
8211417Seric 	"date",			0,
8311417Seric 	"resent-date",		H_RESENT,
8458796Seric 
858060Seric 		/* trace fields */
869055Seric 	"received",		H_TRACE|H_FORCE,
8759318Seric 	"x400-received",	H_TRACE|H_FORCE,
889055Seric 	"via",			H_TRACE|H_FORCE,
899055Seric 	"mail-from",		H_TRACE|H_FORCE,
908060Seric 
9158796Seric 		/* miscellaneous fields */
9258796Seric 	"comments",		H_FORCE,
9363753Seric 	"return-path",		H_FORCE|H_ACHECK,
9458796Seric 
959055Seric 	NULL,			0,
962897Seric };
974166Seric 
984166Seric 
994166Seric 
1004282Seric /*
1014282Seric **  Location of system files/databases/etc.
1024282Seric */
1034282Seric 
10458082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1059039Seric 
1069064Seric 
1079064Seric 
1089039Seric /*
10958082Seric **  Privacy values
11058082Seric */
11158082Seric 
11258082Seric struct prival PrivacyValues[] =
11358082Seric {
11458082Seric 	"public",		PRIV_PUBLIC,
11558082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11658114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11758082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11858082Seric 	"noexpn",		PRIV_NOEXPN,
11958082Seric 	"novrfy",		PRIV_NOVRFY,
12064333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12164333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12258789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12358082Seric 	"goaway",		PRIV_GOAWAY,
12458789Seric 	NULL,			0,
12558082Seric };
12658082Seric 
12758082Seric 
12858082Seric 
12958082Seric /*
13024943Seric **  Miscellaneous stuff.
1319039Seric */
1329039Seric 
13324943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13464644Seric 
13564644Seric 
13664644Seric /*
13764644Seric **  Following should be config parameters (and probably will be in
13864644Seric **  future releases).  In the meantime, setting these is considered
13964644Seric **  unsupported, and is intentionally undocumented.
14064644Seric */
14164644Seric 
14264644Seric #ifdef BROKENSMTPPEERS
14364644Seric bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
14464644Seric #else
14564644Seric bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
14664644Seric #endif
14764644Seric #ifdef NOLOOPBACKCHECK
14864644Seric bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
14964644Seric #else
15064644Seric bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
15164644Seric #endif
15264644Seric 
15324943Seric /*
15424943Seric **  SETDEFAULTS -- set default values
15524943Seric **
15624943Seric **	Because of the way freezing is done, these must be initialized
15724943Seric **	using direct code.
15824943Seric **
15924943Seric **	Parameters:
16058734Seric **		e -- the default envelope.
16124943Seric **
16224943Seric **	Returns:
16324943Seric **		none.
16424943Seric **
16524943Seric **	Side Effects:
16624943Seric **		Initializes a bunch of global variables to their
16724943Seric **		default values.
16824943Seric */
16924943Seric 
17058737Seric #define DAYS		* 24 * 60 * 60
17158737Seric 
17258734Seric setdefaults(e)
17358734Seric 	register ENVELOPE *e;
17424943Seric {
17557438Seric 	SpaceSub = ' ';				/* option B */
17657438Seric 	QueueLA = 8;				/* option x */
17757438Seric 	RefuseLA = 12;				/* option X */
17857438Seric 	WkRecipFact = 30000L;			/* option y */
17957438Seric 	WkClassFact = 1800L;			/* option z */
18057438Seric 	WkTimeFact = 90000L;			/* option Z */
18157438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
18263787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
18357438Seric 						/* option F */
18457438Seric 	DefUid = 1;				/* option u */
18557438Seric 	DefGid = 1;				/* option g */
18657438Seric 	CheckpointInterval = 10;		/* option C */
18757438Seric 	MaxHopCount = 25;			/* option h */
18858734Seric 	e->e_sendmode = SM_FORK;		/* option d */
18958734Seric 	e->e_errormode = EM_PRINT;		/* option e */
19059709Seric 	SevenBit = FALSE;			/* option 7 */
19157438Seric 	MaxMciCache = 1;			/* option k */
19257438Seric 	MciCacheTimeout = 300;			/* option K */
19357438Seric 	LogLevel = 9;				/* option L */
19458112Seric 	settimeouts(NULL);			/* option r */
19558737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
19658737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
19758853Seric 	PrivacyFlags = 0;			/* option p */
19840973Sbostic 	setdefuser();
19953654Seric 	setupmaps();
20057402Seric 	setupmailers();
20124943Seric }
202294Seric 
20340973Sbostic 
2044326Seric /*
20540973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20640973Sbostic */
20740973Sbostic 
20840973Sbostic setdefuser()
20940973Sbostic {
21040973Sbostic 	struct passwd *defpwent;
21157386Seric 	static char defuserbuf[40];
21240973Sbostic 
21357386Seric 	DefUser = defuserbuf;
21440973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
21557386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21640973Sbostic 	else
21757386Seric 		strcpy(defuserbuf, "nobody");
21840973Sbostic }
21953654Seric /*
22056836Seric **  HOST_MAP_INIT -- initialize host class structures
22156836Seric */
22256836Seric 
22356836Seric bool
22460219Seric host_map_init(map, args)
22556836Seric 	MAP *map;
22656836Seric 	char *args;
22756836Seric {
22856836Seric 	register char *p = args;
22956836Seric 
23056836Seric 	for (;;)
23156836Seric 	{
23258050Seric 		while (isascii(*p) && isspace(*p))
23356836Seric 			p++;
23456836Seric 		if (*p != '-')
23556836Seric 			break;
23656836Seric 		switch (*++p)
23756836Seric 		{
23856836Seric 		  case 'a':
23956836Seric 			map->map_app = ++p;
24056836Seric 			break;
24156836Seric 		}
24258050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24356836Seric 			p++;
24456836Seric 		if (*p != '\0')
24556836Seric 			*p++ = '\0';
24656836Seric 	}
24756836Seric 	if (map->map_app != NULL)
24856836Seric 		map->map_app = newstr(map->map_app);
24956836Seric 	return TRUE;
25056836Seric }
25157402Seric /*
25257402Seric **  SETUPMAILERS -- initialize default mailers
25357402Seric */
25456836Seric 
25557402Seric setupmailers()
25657402Seric {
25757402Seric 	char buf[100];
25857402Seric 
25957403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
26057403Seric 	makemailer(buf);
26157403Seric 
26259883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
26357402Seric 	makemailer(buf);
26457402Seric 
26557402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26657402Seric 	makemailer(buf);
26757402Seric }
26856836Seric /*
26960207Seric **  SETUPMAPS -- set up map classes
27060207Seric */
27160207Seric 
27260207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27360207Seric 	{ \
27460207Seric 		extern bool parse __P((MAP *, char *)); \
27560207Seric 		extern bool open __P((MAP *, int)); \
27660207Seric 		extern void close __P((MAP *)); \
27760207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
27860207Seric 		extern void store __P((MAP *, char *, char *)); \
27960207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28060207Seric 		s->s_mapclass.map_cname = name; \
28160207Seric 		s->s_mapclass.map_ext = ext; \
28260207Seric 		s->s_mapclass.map_cflags = flags; \
28360207Seric 		s->s_mapclass.map_parse = parse; \
28460207Seric 		s->s_mapclass.map_open = open; \
28560207Seric 		s->s_mapclass.map_close = close; \
28660207Seric 		s->s_mapclass.map_lookup = lookup; \
28760207Seric 		s->s_mapclass.map_store = store; \
28860207Seric 	}
28960207Seric 
29060207Seric setupmaps()
29160207Seric {
29260207Seric 	register STAB *s;
29360207Seric 
29460207Seric #ifdef NEWDB
29560207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29660207Seric 		map_parseargs, hash_map_open, db_map_close,
29760207Seric 		db_map_lookup, db_map_store);
29860207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29960207Seric 		map_parseargs, bt_map_open, db_map_close,
30060207Seric 		db_map_lookup, db_map_store);
30160207Seric #endif
30260207Seric 
30360207Seric #ifdef NDBM
30460207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30560207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30660207Seric 		ndbm_map_lookup, ndbm_map_store);
30760207Seric #endif
30860207Seric 
30960207Seric #ifdef NIS
31060207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31160207Seric 		map_parseargs, nis_map_open, nis_map_close,
31260207Seric 		nis_map_lookup, nis_map_store);
31360207Seric #endif
31460207Seric 
31560207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
31660207Seric 		map_parseargs, stab_map_open, stab_map_close,
31760207Seric 		stab_map_lookup, stab_map_store);
31860207Seric 
31960207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
32060207Seric 		map_parseargs, impl_map_open, impl_map_close,
32160207Seric 		impl_map_lookup, impl_map_store);
32260207Seric 
32360207Seric 	/* host DNS lookup */
32460207Seric 	MAPDEF("host", NULL, 0,
32560207Seric 		host_map_init, null_map_open, null_map_close,
32660207Seric 		host_map_lookup, null_map_store);
32760207Seric 
32860207Seric 	/* dequote map */
32960207Seric 	MAPDEF("dequote", NULL, 0,
33060207Seric 		dequote_init, null_map_open, null_map_close,
33160207Seric 		dequote_map, null_map_store);
33260207Seric 
33360207Seric #if 0
33460207Seric # ifdef USERDB
33560207Seric 	/* user database */
33660207Seric 	MAPDEF("udb", ".db", 0,
33760207Seric 		udb_map_parse, null_map_open, null_map_close,
33860207Seric 		udb_map_lookup, null_map_store);
33960207Seric # endif
34060207Seric #endif
34160207Seric }
34260207Seric 
34360207Seric #undef MAPDEF
34460207Seric /*
3459369Seric **  USERNAME -- return the user id of the logged in user.
3469369Seric **
3479369Seric **	Parameters:
3489369Seric **		none.
3499369Seric **
3509369Seric **	Returns:
3519369Seric **		The login name of the logged in user.
3529369Seric **
3539369Seric **	Side Effects:
3549369Seric **		none.
3559369Seric **
3569369Seric **	Notes:
3579369Seric **		The return value is statically allocated.
3589369Seric */
3599369Seric 
3609369Seric char *
3619369Seric username()
3629369Seric {
36317469Seric 	static char *myname = NULL;
3649369Seric 	extern char *getlogin();
36519904Smiriam 	register struct passwd *pw;
3669369Seric 
36717469Seric 	/* cache the result */
36817469Seric 	if (myname == NULL)
36917469Seric 	{
37017469Seric 		myname = getlogin();
37117469Seric 		if (myname == NULL || myname[0] == '\0')
37217469Seric 		{
37363787Seric 			pw = getpwuid(RealUid);
37417469Seric 			if (pw != NULL)
37540993Sbostic 				myname = newstr(pw->pw_name);
37617469Seric 		}
37719904Smiriam 		else
37819904Smiriam 		{
37963787Seric 			uid_t uid = RealUid;
38019873Smiriam 
38140993Sbostic 			myname = newstr(myname);
38240993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
38358736Seric 			      (uid != 0 && uid != pw->pw_uid))
38419904Smiriam 			{
38558736Seric 				pw = getpwuid(uid);
38624945Seric 				if (pw != NULL)
38740993Sbostic 					myname = newstr(pw->pw_name);
38819873Smiriam 			}
38919873Smiriam 		}
39017469Seric 		if (myname == NULL || myname[0] == '\0')
39117469Seric 		{
39258151Seric 			syserr("554 Who are you?");
39317469Seric 			myname = "postmaster";
39417469Seric 		}
39517469Seric 	}
39617469Seric 
39717469Seric 	return (myname);
3989369Seric }
3999369Seric /*
4004190Seric **  TTYPATH -- Get the path of the user's tty
401294Seric **
402294Seric **	Returns the pathname of the user's tty.  Returns NULL if
403294Seric **	the user is not logged in or if s/he has write permission
404294Seric **	denied.
405294Seric **
406294Seric **	Parameters:
407294Seric **		none
408294Seric **
409294Seric **	Returns:
410294Seric **		pathname of the user's tty.
411294Seric **		NULL if not logged in or write permission denied.
412294Seric **
413294Seric **	Side Effects:
414294Seric **		none.
415294Seric **
416294Seric **	WARNING:
417294Seric **		Return value is in a local buffer.
418294Seric **
419294Seric **	Called By:
420294Seric **		savemail
421294Seric */
422294Seric 
423294Seric char *
424294Seric ttypath()
425294Seric {
426294Seric 	struct stat stbuf;
427294Seric 	register char *pathn;
428294Seric 	extern char *ttyname();
4294081Seric 	extern char *getlogin();
430294Seric 
431294Seric 	/* compute the pathname of the controlling tty */
4329369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4339369Seric 	    (pathn = ttyname(0)) == NULL)
434294Seric 	{
435294Seric 		errno = 0;
436294Seric 		return (NULL);
437294Seric 	}
438294Seric 
439294Seric 	/* see if we have write permission */
4402967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
441294Seric 	{
442294Seric 		errno = 0;
443294Seric 		return (NULL);
444294Seric 	}
445294Seric 
446294Seric 	/* see if the user is logged in */
447294Seric 	if (getlogin() == NULL)
448294Seric 		return (NULL);
449294Seric 
450294Seric 	/* looks good */
451294Seric 	return (pathn);
452294Seric }
4532967Seric /*
4542967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4552967Seric **
4562967Seric **	This routine can be supplied on a per-installation basis
4572967Seric **	to determine whether a person is allowed to send a message.
4582967Seric **	This allows restriction of certain types of internet
4592967Seric **	forwarding or registration of users.
4602967Seric **
4612967Seric **	If the hosts are found to be incompatible, an error
46257454Seric **	message should be given using "usrerr" and 0 should
4632967Seric **	be returned.
4642967Seric **
4654288Seric **	'NoReturn' can be set to suppress the return-to-sender
4664288Seric **	function; this should be done on huge messages.
4674288Seric **
4682967Seric **	Parameters:
4692967Seric **		to -- the person being sent to.
4702967Seric **
4712967Seric **	Returns:
47257459Seric **		an exit status
4732967Seric **
4742967Seric **	Side Effects:
4752967Seric **		none (unless you include the usrerr stuff)
4762967Seric */
4772967Seric 
47855012Seric checkcompat(to, e)
4792967Seric 	register ADDRESS *to;
48055012Seric 	register ENVELOPE *e;
4812967Seric {
48212133Seric # ifdef lint
48312133Seric 	if (to == NULL)
48412133Seric 		to++;
48563834Seric # endif /* lint */
48664402Seric 
48764402Seric 	if (tTd(49, 1))
48864402Seric 		printf("checkcompat(to=%s, from=%s)\n",
48964402Seric 			to->q_paddr, e->e_from.q_paddr);
49064402Seric 
49110698Seric # ifdef EXAMPLE_CODE
49210698Seric 	/* this code is intended as an example only */
4934437Seric 	register STAB *s;
4944437Seric 
4954437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49655012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4979369Seric 	    to->q_mailer == s->s_mailer)
4984437Seric 	{
49958151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50010698Seric 		/* NoReturn = TRUE; to supress return copy */
50157459Seric 		return (EX_UNAVAILABLE);
5024437Seric 	}
50356795Seric # endif /* EXAMPLE_CODE */
50457459Seric 	return (EX_OK);
5052967Seric }
5069369Seric /*
50764035Seric **  SETSIGNAL -- set a signal handler
50864035Seric **
50964035Seric **	This is essentially old BSD "signal(3)".
51064035Seric */
51164035Seric 
51264561Seric sigfunc_t
51364035Seric setsignal(sig, handler)
51464035Seric 	int sig;
51564561Seric 	sigfunc_t handler;
51664035Seric {
51764380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
51864035Seric 	return signal(sig, handler);
51964035Seric #else
52064035Seric 	struct sigaction n, o;
52164035Seric 
52264035Seric 	bzero(&n, sizeof n);
52364035Seric 	n.sa_handler = handler;
52464035Seric 	if (sigaction(sig, &n, &o) < 0)
52564035Seric 		return SIG_ERR;
52664035Seric 	return o.sa_handler;
52764035Seric #endif
52864035Seric }
52964035Seric /*
5309369Seric **  HOLDSIGS -- arrange to hold all signals
5319369Seric **
5329369Seric **	Parameters:
5339369Seric **		none.
5349369Seric **
5359369Seric **	Returns:
5369369Seric **		none.
5379369Seric **
5389369Seric **	Side Effects:
5399369Seric **		Arranges that signals are held.
5409369Seric */
5419369Seric 
5429369Seric holdsigs()
5439369Seric {
5449369Seric }
5459369Seric /*
5469369Seric **  RLSESIGS -- arrange to release all signals
5479369Seric **
5489369Seric **	This undoes the effect of holdsigs.
5499369Seric **
5509369Seric **	Parameters:
5519369Seric **		none.
5529369Seric **
5539369Seric **	Returns:
5549369Seric **		none.
5559369Seric **
5569369Seric **	Side Effects:
5579369Seric **		Arranges that signals are released.
5589369Seric */
5599369Seric 
5609369Seric rlsesigs()
5619369Seric {
5629369Seric }
56314872Seric /*
564*64705Seric **  INIT_MD -- do machine dependent initializations
565*64705Seric **
566*64705Seric **	Systems that have global modes that should be set should do
567*64705Seric **	them here rather than in main.
568*64705Seric */
569*64705Seric 
570*64705Seric #ifdef _AUX_SOURCE
571*64705Seric # include	<compat.h>
572*64705Seric #endif
573*64705Seric 
574*64705Seric init_md()
575*64705Seric {
576*64705Seric #ifdef _AUX_SOURCE
577*64705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
578*64705Seric #endif
579*64705Seric }
580*64705Seric /*
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 
73351773Seric getla()
73451773Seric {
73551920Seric 	double avenrun[3];
73651920Seric 
73751920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
73857736Seric 	{
73957736Seric 		if (tTd(3, 1))
74057736Seric 			perror("getla: getloadavg failed:");
74151920Seric 		return (-1);
74257736Seric 	}
74357736Seric 	if (tTd(3, 1))
74457736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
74551920Seric 	return ((int) (avenrun[0] + 0.5));
74651773Seric }
74751773Seric 
74851773Seric #else
74964295Seric #if LA_TYPE == LA_MACH
75051773Seric 
75164295Seric /*
75264295Seric **  This has been tested on NeXT release 2.1.
75364295Seric */
75464295Seric 
75564295Seric #include <mach.h>
75664295Seric 
75751773Seric getla()
75851773Seric {
75964295Seric 	processor_set_t default_set;
76064295Seric 	kern_return_t error;
76164295Seric 	unsigned int info_count;
76264295Seric 	struct processor_set_basic_info info;
76364295Seric 	host_t host;
76464295Seric 
76564295Seric 	error = processor_set_default(host_self(), &default_set);
76664295Seric 	if (error != KERN_SUCCESS)
76764295Seric 		return -1;
76864295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
76964295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
77064295Seric 			       &host, (processor_set_info_t)&info,
77164295Seric 			       &info_count) != KERN_SUCCESS)
77264295Seric 	{
77364295Seric 		return -1;
77464295Seric 	}
77564295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
77664295Seric }
77764295Seric 
77864295Seric 
77964295Seric #else
78064295Seric 
78164295Seric getla()
78264295Seric {
78357736Seric 	if (tTd(3, 1))
78457736Seric 		printf("getla: ZERO\n");
78551920Seric 	return (0);
78651773Seric }
78751773Seric 
78851773Seric #endif
78951773Seric #endif
79064295Seric #endif
79124943Seric /*
79224943Seric **  SHOULDQUEUE -- should this message be queued or sent?
79324943Seric **
79424943Seric **	Compares the message cost to the load average to decide.
79524943Seric **
79624943Seric **	Parameters:
79724943Seric **		pri -- the priority of the message in question.
79857438Seric **		ctime -- the message creation time.
79924943Seric **
80024943Seric **	Returns:
80124943Seric **		TRUE -- if this message should be queued up for the
80224943Seric **			time being.
80324943Seric **		FALSE -- if the load is low enough to send this message.
80424943Seric **
80524943Seric **	Side Effects:
80624943Seric **		none.
80724943Seric */
80824943Seric 
80924943Seric bool
81057438Seric shouldqueue(pri, ctime)
81124943Seric 	long pri;
81257438Seric 	time_t ctime;
81324943Seric {
81451920Seric 	if (CurrentLA < QueueLA)
81524943Seric 		return (FALSE);
81658132Seric 	if (CurrentLA >= RefuseLA)
81758132Seric 		return (TRUE);
81851920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
81924943Seric }
82024943Seric /*
82153037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
82253037Seric **
82353037Seric **	Parameters:
82453037Seric **		none.
82553037Seric **
82653037Seric **	Returns:
82753037Seric **		TRUE if incoming SMTP connections should be refused
82853037Seric **			(for now).
82953037Seric **		FALSE if we should accept new work.
83053037Seric **
83153037Seric **	Side Effects:
83253037Seric **		none.
83353037Seric */
83453037Seric 
83553037Seric bool
83653037Seric refuseconnections()
83753037Seric {
83859156Seric #ifdef XLA
83959156Seric 	if (!xla_smtp_ok())
84059156Seric 		return TRUE;
84159156Seric #endif
84259156Seric 
84353037Seric 	/* this is probably too simplistic */
84458132Seric 	return (CurrentLA >= RefuseLA);
84553037Seric }
84653037Seric /*
84724943Seric **  SETPROCTITLE -- set process title for ps
84824943Seric **
84924943Seric **	Parameters:
85058674Seric **		fmt -- a printf style format string.
85158674Seric **		a, b, c -- possible parameters to fmt.
85224943Seric **
85324943Seric **	Returns:
85424943Seric **		none.
85524943Seric **
85624943Seric **	Side Effects:
85724943Seric **		Clobbers argv of our main procedure so ps(1) will
85824943Seric **		display the title.
85924943Seric */
86024943Seric 
86158689Seric #ifdef SETPROCTITLE
86258689Seric # ifdef __hpux
86358689Seric #  include <sys/pstat.h>
86458689Seric # endif
86559732Seric # ifdef BSD4_4
86659732Seric #  include <machine/vmparam.h>
86759732Seric #  include <sys/exec.h>
86864247Seric #  ifdef __bsdi__
86964247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
87064247Seric #  endif
87159992Seric #  ifdef PS_STRINGS
87259992Seric #   define SETPROC_STATIC static
87359992Seric #  endif
87459732Seric # endif
87559732Seric # ifndef SETPROC_STATIC
87659732Seric #  define SETPROC_STATIC
87759732Seric # endif
87858689Seric #endif
87958689Seric 
88024943Seric /*VARARGS1*/
88157642Seric #ifdef __STDC__
88257642Seric setproctitle(char *fmt, ...)
88357642Seric #else
88457642Seric setproctitle(fmt, va_alist)
88524943Seric 	char *fmt;
88657642Seric 	va_dcl
88757642Seric #endif
88824943Seric {
88924943Seric # ifdef SETPROCTITLE
89024943Seric 	register char *p;
89125049Seric 	register int i;
89259732Seric 	SETPROC_STATIC char buf[MAXLINE];
89356852Seric 	VA_LOCAL_DECL
89458689Seric #  ifdef __hpux
89558689Seric 	union pstun pst;
89658689Seric #  endif
89724943Seric 	extern char **Argv;
89824943Seric 	extern char *LastArgv;
89924943Seric 
90058674Seric 	p = buf;
90124943Seric 
90258674Seric 	/* print sendmail: heading for grep */
90358674Seric 	(void) strcpy(p, "sendmail: ");
90458674Seric 	p += strlen(p);
90524943Seric 
90658674Seric 	/* print the argument string */
90758674Seric 	VA_START(fmt);
90858674Seric 	(void) vsprintf(p, fmt, ap);
90956852Seric 	VA_END;
91054996Seric 
91158674Seric 	i = strlen(buf);
91258689Seric 
91358689Seric #  ifdef __hpux
91458689Seric 	pst.pst_command = buf;
91558689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
91658689Seric #  else
91759992Seric #   ifdef PS_STRINGS
91859732Seric 	PS_STRINGS->ps_nargvstr = 1;
91959732Seric 	PS_STRINGS->ps_argvstr = buf;
92059732Seric #   else
92154996Seric 	if (i > LastArgv - Argv[0] - 2)
92225049Seric 	{
92354996Seric 		i = LastArgv - Argv[0] - 2;
92458674Seric 		buf[i] = '\0';
92525049Seric 	}
92658674Seric 	(void) strcpy(Argv[0], buf);
92754997Seric 	p = &Argv[0][i];
92824943Seric 	while (p < LastArgv)
92924943Seric 		*p++ = ' ';
93059732Seric #   endif
93158689Seric #  endif
93256795Seric # endif /* SETPROCTITLE */
93324943Seric }
93425698Seric /*
93525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
93625698Seric **
93725698Seric **	Parameters:
93825698Seric **		none.
93925698Seric **
94025698Seric **	Returns:
94125698Seric **		none.
94225698Seric **
94325698Seric **	Side Effects:
94425698Seric **		Picks up extant zombies.
94525698Seric */
94625698Seric 
94746928Sbostic void
94825698Seric reapchild()
94925698Seric {
95063966Seric # ifdef HASWAITPID
95160219Seric 	auto int status;
95260560Seric 	int count;
95360560Seric 	int pid;
95460219Seric 
95560560Seric 	count = 0;
95660560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
95760560Seric 	{
95860560Seric 		if (count++ > 1000)
95960560Seric 		{
96060560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
96160560Seric 				pid, status);
96260560Seric 			break;
96360560Seric 		}
96460560Seric 	}
96560219Seric # else
96625698Seric # ifdef WNOHANG
96725698Seric 	union wait status;
96825698Seric 
96963753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
97025698Seric 		continue;
97156795Seric # else /* WNOHANG */
97225698Seric 	auto int status;
97325698Seric 
97460219Seric 	while (wait(&status) > 0)
97525698Seric 		continue;
97656795Seric # endif /* WNOHANG */
97760219Seric # endif
97864035Seric # ifdef SYS5SIGNALS
97964035Seric 	(void) setsignal(SIGCHLD, reapchild);
98058061Seric # endif
98125698Seric }
98255418Seric /*
98355418Seric **  UNSETENV -- remove a variable from the environment
98455418Seric **
98555418Seric **	Not needed on newer systems.
98655418Seric **
98755418Seric **	Parameters:
98855418Seric **		name -- the string name of the environment variable to be
98955418Seric **			deleted from the current environment.
99055418Seric **
99155418Seric **	Returns:
99255418Seric **		none.
99355418Seric **
99455418Seric **	Globals:
99555418Seric **		environ -- a pointer to the current environment.
99655418Seric **
99755418Seric **	Side Effects:
99855418Seric **		Modifies environ.
99955418Seric */
100055418Seric 
100163962Seric #ifndef HASUNSETENV
100255418Seric 
100355418Seric void
100455418Seric unsetenv(name)
100555418Seric 	char *name;
100655418Seric {
100755418Seric 	extern char **environ;
100855418Seric 	register char **pp;
100955418Seric 	int len = strlen(name);
101055418Seric 
101155418Seric 	for (pp = environ; *pp != NULL; pp++)
101255418Seric 	{
101355418Seric 		if (strncmp(name, *pp, len) == 0 &&
101455418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
101555418Seric 			break;
101655418Seric 	}
101755418Seric 
101855418Seric 	for (; *pp != NULL; pp++)
101955418Seric 		*pp = pp[1];
102055418Seric }
102155418Seric 
102263962Seric #endif
102356215Seric /*
102456215Seric **  GETDTABLESIZE -- return number of file descriptors
102556215Seric **
102656215Seric **	Only on non-BSD systems
102756215Seric **
102856215Seric **	Parameters:
102956215Seric **		none
103056215Seric **
103156215Seric **	Returns:
103256215Seric **		size of file descriptor table
103356215Seric **
103456215Seric **	Side Effects:
103556215Seric **		none
103656215Seric */
103756215Seric 
103863787Seric #ifdef SOLARIS
103963787Seric # include <sys/resource.h>
104063787Seric #endif
104156215Seric 
104256215Seric int
104363787Seric getdtsize()
104456215Seric {
104563787Seric #ifdef RLIMIT_NOFILE
104663787Seric 	struct rlimit rl;
104763787Seric 
104863787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
104963787Seric 		return rl.rlim_cur;
105063787Seric #endif
105163787Seric 
105264031Seric # ifdef HASGETDTABLESIZE
105364031Seric 	return getdtablesize();
105464031Seric # else
105564031Seric #  ifdef _SC_OPEN_MAX
105658689Seric 	return sysconf(_SC_OPEN_MAX);
105763787Seric #  else
105856215Seric 	return NOFILE;
105963787Seric #  endif
106058689Seric # endif
106156215Seric }
106257631Seric /*
106357631Seric **  UNAME -- get the UUCP name of this system.
106457631Seric */
106557631Seric 
106657943Seric #ifndef HASUNAME
106757631Seric 
106857631Seric int
106957631Seric uname(name)
107057631Seric 	struct utsname *name;
107157631Seric {
107257631Seric 	FILE *file;
107357631Seric 	char *n;
107457631Seric 
107557631Seric 	name->nodename[0] = '\0';
107657631Seric 
107757661Seric 	/* try /etc/whoami -- one line with the node name */
107857631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
107957631Seric 	{
108057661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
108157631Seric 		(void) fclose(file);
108257661Seric 		n = strchr(name->nodename, '\n');
108357631Seric 		if (n != NULL)
108457631Seric 			*n = '\0';
108557631Seric 		if (name->nodename[0] != '\0')
108657631Seric 			return (0);
108757631Seric 	}
108857631Seric 
108957661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
109057631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
109157631Seric 	{
109257631Seric 		char buf[MAXLINE];
109357631Seric 
109457631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
109557631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
109657631Seric 					NODE_LENGTH, name->nodename) > 0)
109757631Seric 				break;
109857631Seric 		(void) fclose(file);
109957631Seric 		if (name->nodename[0] != '\0')
110057631Seric 			return (0);
110157631Seric 	}
110257631Seric 
110357631Seric #ifdef TRUST_POPEN
110457631Seric 	/*
110557631Seric 	**  Popen is known to have security holes.
110657631Seric 	*/
110757631Seric 
110857661Seric 	/* try uuname -l to return local name */
110957631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
111057631Seric 	{
111157661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
111257631Seric 		(void) pclose(file);
111357661Seric 		n = strchr(name, '\n');
111457631Seric 		if (n != NULL)
111557631Seric 			*n = '\0';
111657661Seric 		if (name->nodename[0] != '\0')
111757631Seric 			return (0);
111857631Seric 	}
111957631Seric #endif
112057631Seric 
112157631Seric 	return (-1);
112257631Seric }
112357943Seric #endif /* HASUNAME */
112458068Seric /*
112558068Seric **  INITGROUPS -- initialize groups
112658068Seric **
112758068Seric **	Stub implementation for System V style systems
112858068Seric */
112958068Seric 
113058068Seric #ifndef HASINITGROUPS
113158068Seric 
113258068Seric initgroups(name, basegid)
113358068Seric 	char *name;
113458068Seric 	int basegid;
113558068Seric {
113658068Seric 	return 0;
113758068Seric }
113858068Seric 
113958068Seric #endif
114058082Seric /*
114159289Seric **  SETSID -- set session id (for non-POSIX systems)
114259289Seric */
114359289Seric 
114459289Seric #ifndef HASSETSID
114559289Seric 
114663753Seric pid_t
114763753Seric setsid __P ((void))
114859289Seric {
114964155Seric #ifdef TIOCNOTTY
115064155Seric 	int fd;
115164155Seric 
115264155Seric 	fd = open("/dev/tty", 2);
115364155Seric 	if (fd >= 0)
115464155Seric 	{
115564155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
115664155Seric 		(void) close(fd);
115764155Seric 	}
115864155Seric #endif /* TIOCNOTTY */
1159*64705Seric # ifdef SYS5SETPGRP
116063753Seric 	return setpgrp();
116163753Seric # else
116264155Seric 	return setpgid(0, getpid());
116359289Seric # endif
116459289Seric }
116559289Seric 
116659289Seric #endif
116759289Seric /*
116863969Seric **  GETOPT -- for old systems or systems with bogus implementations
116963969Seric */
117063969Seric 
117163969Seric #ifdef NEEDGETOPT
117263969Seric 
117363969Seric /*
117463969Seric  * Copyright (c) 1985 Regents of the University of California.
117563969Seric  * All rights reserved.  The Berkeley software License Agreement
117663969Seric  * specifies the terms and conditions for redistribution.
117763969Seric  */
117863969Seric 
117963969Seric 
118063969Seric /*
118163969Seric ** this version hacked to add `atend' flag to allow state machine
118263969Seric ** to reset if invoked by the program to scan args for a 2nd time
118363969Seric */
118463969Seric 
118563969Seric #if defined(LIBC_SCCS) && !defined(lint)
118663969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
118764072Seric #endif /* LIBC_SCCS and not lint */
118863969Seric 
118963969Seric #include <stdio.h>
119063969Seric 
119163969Seric /*
119263969Seric  * get option letter from argument vector
119363969Seric  */
119463969Seric int	opterr = 1,		/* if error message should be printed */
119563969Seric 	optind = 1,		/* index into parent argv vector */
119663969Seric 	optopt;			/* character checked for validity */
119763969Seric char	*optarg;		/* argument associated with option */
119863969Seric 
119963969Seric #define BADCH	(int)'?'
120063969Seric #define EMSG	""
120163969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
120263969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
120363969Seric 
120463969Seric getopt(nargc,nargv,ostr)
120563969Seric int	nargc;
120663969Seric char	**nargv,
120763969Seric 	*ostr;
120863969Seric {
120963969Seric 	static char	*place = EMSG;	/* option letter processing */
121063969Seric 	static char	atend = 0;
121163969Seric 	register char	*oli;		/* option letter list index */
121263969Seric 
121363969Seric 	if (atend) {
121463969Seric 		atend = 0;
121563969Seric 		place = EMSG;
121663969Seric 	}
121763969Seric 	if(!*place) {			/* update scanning pointer */
121864136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
121963969Seric 			atend++;
122063969Seric 			return(EOF);
122163969Seric 		}
122263969Seric 		if (*place == '-') {	/* found "--" */
122363969Seric 			++optind;
122463969Seric 			atend++;
122563969Seric 			return(EOF);
122663969Seric 		}
122763969Seric 	}				/* option letter okay? */
122864125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
122964136Seric 		if (!*place) ++optind;
123063969Seric 		tell(": illegal option -- ");
123163969Seric 	}
123263969Seric 	if (*++oli != ':') {		/* don't need argument */
123363969Seric 		optarg = NULL;
123463969Seric 		if (!*place) ++optind;
123563969Seric 	}
123663969Seric 	else {				/* need an argument */
123763969Seric 		if (*place) optarg = place;	/* no white space */
123863969Seric 		else if (nargc <= ++optind) {	/* no arg */
123963969Seric 			place = EMSG;
124063969Seric 			tell(": option requires an argument -- ");
124163969Seric 		}
124263969Seric 	 	else optarg = nargv[optind];	/* white space */
124363969Seric 		place = EMSG;
124463969Seric 		++optind;
124563969Seric 	}
124663969Seric 	return(optopt);			/* dump back option letter */
124763969Seric }
124863969Seric 
124963969Seric #endif
125063969Seric /*
125163969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
125263969Seric */
125363969Seric 
125463969Seric #ifdef NEEDVPRINTF
125563969Seric 
125663969Seric #define MAXARG	16
125763969Seric 
125863969Seric vfprintf(fp, fmt, ap)
125963969Seric 	FILE *	fp;
126063969Seric 	char *	fmt;
126163969Seric 	char **	ap;
126263969Seric {
126363969Seric 	char *	bp[MAXARG];
126463969Seric 	int	i = 0;
126563969Seric 
126663969Seric 	while (*ap && i < MAXARG)
126763969Seric 		bp[i++] = *ap++;
126863969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
126963969Seric 			 bp[4], bp[5], bp[6], bp[7],
127063969Seric 			 bp[8], bp[9], bp[10], bp[11],
127163969Seric 			 bp[12], bp[13], bp[14], bp[15]);
127263969Seric }
127363969Seric 
127463969Seric vsprintf(s, fmt, ap)
127563969Seric 	char *	s;
127663969Seric 	char *	fmt;
127763969Seric 	char **	ap;
127863969Seric {
127963969Seric 	char *	bp[MAXARG];
128063969Seric 	int	i = 0;
128163969Seric 
128263969Seric 	while (*ap && i < MAXARG)
128363969Seric 		bp[i++] = *ap++;
128463969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
128563969Seric 			bp[4], bp[5], bp[6], bp[7],
128663969Seric 			bp[8], bp[9], bp[10], bp[11],
128763969Seric 			bp[12], bp[13], bp[14], bp[15]);
128863969Seric }
128963969Seric 
129063969Seric #endif
129163969Seric /*
129264311Seric **  FREESPACE -- see how much free space is on the queue filesystem
129358082Seric **
129458082Seric **	Only implemented if you have statfs.
129558082Seric **
129658082Seric **	Parameters:
129764311Seric **		dir -- the directory in question.
129864311Seric **		bsize -- a variable into which the filesystem
129964311Seric **			block size is stored.
130058082Seric **
130158082Seric **	Returns:
130264311Seric **		The number of bytes free on the queue filesystem.
130364311Seric **		-1 if the statfs call fails.
130464311Seric **
130564311Seric **	Side effects:
130664311Seric **		Puts the filesystem block size into bsize.
130758082Seric */
130858082Seric 
130958082Seric #ifdef HASSTATFS
131058157Seric # undef HASUSTAT
131158157Seric #endif
131258157Seric 
131358157Seric #if defined(HASUSTAT)
131458157Seric # include <ustat.h>
131558157Seric #endif
131658157Seric 
131758157Seric #ifdef HASSTATFS
1318*64705Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV)
131958133Seric #  include <sys/statfs.h>
132058133Seric # else
132164380Seric #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
132258133Seric #   include <sys/vfs.h>
132358133Seric #  else
132458157Seric #   include <sys/mount.h>
132558133Seric #  endif
132658133Seric # endif
132758082Seric #endif
132858082Seric 
132964311Seric long
133064311Seric freespace(dir, bsize)
133164311Seric 	char *dir;
133264311Seric 	long *bsize;
133358082Seric {
133458160Seric #if defined(HASSTATFS) || defined(HASUSTAT)
133558157Seric # if defined(HASUSTAT)
133658153Seric 	struct ustat fs;
133758153Seric 	struct stat statbuf;
133858366Seric #  define FSBLOCKSIZE	DEV_BSIZE
133958157Seric #  define f_bavail	f_tfree
134058157Seric # else
134158157Seric #  if defined(ultrix)
134258157Seric 	struct fs_data fs;
134358157Seric #   define f_bavail	fd_bfreen
134458366Seric #   define FSBLOCKSIZE	fs.fd_bsize
134558153Seric #  else
134658082Seric 	struct statfs fs;
134758366Seric #   define FSBLOCKSIZE	fs.f_bsize
134864336Seric #   if defined(_SCO_unix_) || defined(IRIX)
134963987Seric #    define f_bavail f_bfree
135063987Seric #   endif
135158153Seric #  endif
135258133Seric # endif
135358082Seric 	extern int errno;
135458082Seric 
135558157Seric # if defined(HASUSTAT)
135664311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
135758157Seric # else
1358*64705Seric #  if defined(IRIX) || defined(apollo) || defined(UMAXV)
135964311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
136058157Seric #  else
136158157Seric #   if defined(ultrix)
136264311Seric 	if (statfs(dir, &fs) > 0)
136358153Seric #   else
136464311Seric 	if (statfs(dir, &fs) == 0)
136558153Seric #   endif
136658133Seric #  endif
136758133Seric # endif
136858133Seric 	{
136964311Seric 		if (bsize != NULL)
137064311Seric 			*bsize = FSBLOCKSIZE;
137164311Seric 		return (fs.f_bavail);
137264311Seric 	}
137364337Seric #endif
137464311Seric 	return (-1);
137564311Seric }
137664311Seric /*
137764311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
137864311Seric **
137964311Seric **	Only implemented if you have statfs.
138064311Seric **
138164311Seric **	Parameters:
138264311Seric **		msize -- the size to check against.  If zero, we don't yet
138364311Seric **		know how big the message will be, so just check for
138464311Seric **		a "reasonable" amount.
138564311Seric **
138664311Seric **	Returns:
138764311Seric **		TRUE if there is enough space.
138864311Seric **		FALSE otherwise.
138964311Seric */
139064311Seric 
139164311Seric bool
139264311Seric enoughspace(msize)
139364311Seric 	long msize;
139464311Seric {
139564311Seric 	long bfree, bsize;
139664311Seric 
139764311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
139864311Seric 	{
139958133Seric 		if (tTd(4, 80))
140064311Seric 			printf("enoughspace: no threshold\n");
140164311Seric 		return TRUE;
140264311Seric 	}
140364311Seric 
140464311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
140564311Seric 	{
140664311Seric 		if (tTd(4, 80))
140758333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
140864311Seric 				bfree, msize);
140958333Seric 
141058333Seric 		/* convert msize to block count */
141164311Seric 		msize = msize / bsize + 1;
141258333Seric 		if (MinBlocksFree >= 0)
141358333Seric 			msize += MinBlocksFree;
141458333Seric 
141564311Seric 		if (bfree < msize)
141658090Seric 		{
141758090Seric #ifdef LOG
141858090Seric 			if (LogLevel > 0)
141964254Seric 				syslog(LOG_ALERT,
142064254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
142164311Seric 					CurEnv->e_id, bfree,
142264254Seric 					CurHostName, msize, QueueDir);
142358090Seric #endif
142458082Seric 			return FALSE;
142558090Seric 		}
142658082Seric 	}
142758082Seric 	else if (tTd(4, 80))
142858333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
142958333Seric 			MinBlocksFree, msize, errstring(errno));
143058082Seric 	return TRUE;
143158082Seric }
143258542Seric /*
143358542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
143458542Seric **
143558542Seric **	This looks at an errno value and tells if this is likely to
143658542Seric **	go away if retried later.
143758542Seric **
143858542Seric **	Parameters:
143958542Seric **		err -- the errno code to classify.
144058542Seric **
144158542Seric **	Returns:
144258542Seric **		TRUE if this is probably transient.
144358542Seric **		FALSE otherwise.
144458542Seric */
144558542Seric 
144658542Seric bool
144758542Seric transienterror(err)
144858542Seric 	int err;
144958542Seric {
145058542Seric 	switch (err)
145158542Seric 	{
145258542Seric 	  case EIO:			/* I/O error */
145358542Seric 	  case ENXIO:			/* Device not configured */
145458542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
145558542Seric 	  case ENOMEM:			/* Cannot allocate memory */
145658542Seric 	  case ENODEV:			/* Operation not supported by device */
145758542Seric 	  case ENFILE:			/* Too many open files in system */
145858542Seric 	  case EMFILE:			/* Too many open files */
145958542Seric 	  case ENOSPC:			/* No space left on device */
146058542Seric #ifdef ETIMEDOUT
146158542Seric 	  case ETIMEDOUT:		/* Connection timed out */
146258542Seric #endif
146358542Seric #ifdef ESTALE
146458542Seric 	  case ESTALE:			/* Stale NFS file handle */
146558542Seric #endif
146658542Seric #ifdef ENETDOWN
146758542Seric 	  case ENETDOWN:		/* Network is down */
146858542Seric #endif
146958542Seric #ifdef ENETUNREACH
147058542Seric 	  case ENETUNREACH:		/* Network is unreachable */
147158542Seric #endif
147258542Seric #ifdef ENETRESET
147358542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
147458542Seric #endif
147558542Seric #ifdef ECONNABORTED
147658542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
147758542Seric #endif
147858542Seric #ifdef ECONNRESET
147958542Seric 	  case ECONNRESET:		/* Connection reset by peer */
148058542Seric #endif
148158542Seric #ifdef ENOBUFS
148258542Seric 	  case ENOBUFS:			/* No buffer space available */
148358542Seric #endif
148458542Seric #ifdef ESHUTDOWN
148558542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
148658542Seric #endif
148758542Seric #ifdef ECONNREFUSED
148858542Seric 	  case ECONNREFUSED:		/* Connection refused */
148958542Seric #endif
149058542Seric #ifdef EHOSTDOWN
149158542Seric 	  case EHOSTDOWN:		/* Host is down */
149258542Seric #endif
149358542Seric #ifdef EHOSTUNREACH
149458542Seric 	  case EHOSTUNREACH:		/* No route to host */
149558542Seric #endif
149658542Seric #ifdef EDQUOT
149758542Seric 	  case EDQUOT:			/* Disc quota exceeded */
149858542Seric #endif
149958542Seric #ifdef EPROCLIM
150058542Seric 	  case EPROCLIM:		/* Too many processes */
150158542Seric #endif
150258542Seric #ifdef EUSERS
150358542Seric 	  case EUSERS:			/* Too many users */
150458542Seric #endif
150558542Seric #ifdef EDEADLK
150658542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
150758542Seric #endif
150858542Seric #ifdef EISCONN
150958542Seric 	  case EISCONN:			/* Socket already connected */
151058542Seric #endif
151158542Seric #ifdef EINPROGRESS
151258542Seric 	  case EINPROGRESS:		/* Operation now in progress */
151358542Seric #endif
151458542Seric #ifdef EALREADY
151558542Seric 	  case EALREADY:		/* Operation already in progress */
151658542Seric #endif
151758542Seric #ifdef EADDRINUSE
151858542Seric 	  case EADDRINUSE:		/* Address already in use */
151958542Seric #endif
152058542Seric #ifdef EADDRNOTAVAIL
152158542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
152258542Seric #endif
152363834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
152458542Seric 	  case ENOSR:			/* Out of streams resources */
152558542Seric #endif
152658542Seric 		return TRUE;
152758542Seric 	}
152858542Seric 
152958542Seric 	/* nope, must be permanent */
153058542Seric 	return FALSE;
153158542Seric }
153258689Seric /*
153364035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
153458689Seric **
153558689Seric **	Parameters:
153658689Seric **		fd -- the file descriptor of the file.
153758689Seric **		filename -- the file name (for error messages).
153864335Seric **		ext -- the filename extension.
153958689Seric **		type -- type of the lock.  Bits can be:
154058689Seric **			LOCK_EX -- exclusive lock.
154158689Seric **			LOCK_NB -- non-blocking.
154258689Seric **
154358689Seric **	Returns:
154458689Seric **		TRUE if the lock was acquired.
154558689Seric **		FALSE otherwise.
154658689Seric */
154758689Seric 
154858689Seric bool
154964335Seric lockfile(fd, filename, ext, type)
155058689Seric 	int fd;
155158689Seric 	char *filename;
155264335Seric 	char *ext;
155358689Seric 	int type;
155458689Seric {
155564035Seric # ifndef HASFLOCK
155658689Seric 	int action;
155758689Seric 	struct flock lfd;
155864335Seric 
155964335Seric 	if (ext == NULL)
156064335Seric 		ext = "";
156164035Seric 
156263983Seric 	bzero(&lfd, sizeof lfd);
156359447Seric 	if (bitset(LOCK_UN, type))
156459447Seric 		lfd.l_type = F_UNLCK;
156559447Seric 	else if (bitset(LOCK_EX, type))
156658689Seric 		lfd.l_type = F_WRLCK;
156758689Seric 	else
156858689Seric 		lfd.l_type = F_RDLCK;
156958689Seric 
157058689Seric 	if (bitset(LOCK_NB, type))
157158689Seric 		action = F_SETLK;
157258689Seric 	else
157358689Seric 		action = F_SETLKW;
157458689Seric 
157564264Seric 	if (tTd(55, 60))
157664335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
157764335Seric 			filename, ext, action, lfd.l_type);
157864264Seric 
157958689Seric 	if (fcntl(fd, action, &lfd) >= 0)
158064264Seric 	{
158164264Seric 		if (tTd(55, 60))
158264264Seric 			printf("SUCCESS\n");
158358689Seric 		return TRUE;
158464264Seric 	}
158558689Seric 
158664264Seric 	if (tTd(55, 60))
158764264Seric 		printf("(%s) ", errstring(errno));
158864264Seric 
158964136Seric 	/*
159064136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
159164136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
159264136Seric 	**  as type "tmp" (that is, served from swap space), the
159364136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
159464136Seric 	**  Since this is fairly common during testing, we will assume
159564136Seric 	**  that this indicates that the lock is successfully grabbed.
159664136Seric 	*/
159764136Seric 
159864136Seric 	if (errno == EINVAL)
159964264Seric 	{
160064264Seric 		if (tTd(55, 60))
160164264Seric 			printf("SUCCESS\n");
160264136Seric 		return TRUE;
160364264Seric 	}
160464136Seric 
160558689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
160664378Seric 	{
160764378Seric 		int omode = -1;
160864378Seric #  ifdef F_GETFL
160964378Seric 		int oerrno = errno;
161064378Seric 
161164378Seric 		(void) fcntl(fd, F_GETFL, &omode);
161264378Seric 		errno = oerrno;
161364378Seric #  endif
161464378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
161564378Seric 			filename, ext, fd, type, omode, geteuid());
161664378Seric 	}
161758689Seric # else
161864335Seric 	if (ext == NULL)
161964335Seric 		ext = "";
162064335Seric 
162164264Seric 	if (tTd(55, 60))
162264335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
162364264Seric 
162458689Seric 	if (flock(fd, type) >= 0)
162564264Seric 	{
162664264Seric 		if (tTd(55, 60))
162764264Seric 			printf("SUCCESS\n");
162858689Seric 		return TRUE;
162964264Seric 	}
163058689Seric 
163164264Seric 	if (tTd(55, 60))
163264264Seric 		printf("(%s) ", errstring(errno));
163364264Seric 
163458689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
163564378Seric 	{
163664378Seric 		int omode = -1;
163764378Seric #  ifdef F_GETFL
163864378Seric 		int oerrno = errno;
163964378Seric 
164064378Seric 		(void) fcntl(fd, F_GETFL, &omode);
164164378Seric 		errno = oerrno;
164264378Seric #  endif
164364378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
164464378Seric 			filename, ext, fd, type, omode, geteuid());
164564386Seric 	}
164658689Seric # endif
164764264Seric 	if (tTd(55, 60))
164864264Seric 		printf("FAIL\n");
164958689Seric 	return FALSE;
165058689Seric }
165164556Seric /*
165264556Seric **  GETCFNAME -- return the name of the .cf file.
165364556Seric **
165464556Seric **	Some systems (e.g., NeXT) determine this dynamically.
165564556Seric */
165664556Seric 
165764556Seric char *
165864556Seric getcfname()
165964556Seric {
166064556Seric 	if (ConfFile != NULL)
166164556Seric 		return ConfFile;
166264556Seric 	return _PATH_SENDMAILCF;
166364556Seric }
1664