xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 66002)
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*66002Seric static char sccsid[] = "@(#)conf.c	8.72 (Berkeley) 02/05/94";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1363937Seric # include "sendmail.h"
1463937Seric # include "pathnames.h"
1514881Seric # include <sys/ioctl.h>
1658082Seric # include <sys/param.h>
1765018Seric # include <netdb.h>
1836928Sbostic # include <pwd.h>
19404Seric 
20294Seric /*
213309Seric **  CONF.C -- Sendmail Configuration Tables.
22294Seric **
23294Seric **	Defines the configuration of this installation.
24294Seric **
251388Seric **	Configuration Variables:
262897Seric **		HdrInfo -- a table describing well-known header fields.
272897Seric **			Each entry has the field name and some flags,
284147Seric **			which are described in sendmail.h.
294093Seric **
304093Seric **	Notes:
314093Seric **		I have tried to put almost all the reasonable
324093Seric **		configuration information into the configuration
334093Seric **		file read at runtime.  My intent is that anything
344093Seric **		here is a function of the version of UNIX you
354093Seric **		are running, or is really static -- for example
364093Seric **		the headers are a superset of widely used
374093Seric **		protocols.  If you find yourself playing with
384093Seric **		this file too much, you may be making a mistake!
39294Seric */
40294Seric 
41294Seric 
42294Seric 
43294Seric 
444437Seric /*
452897Seric **  Header info table
463057Seric **	Final (null) entry contains the flags used for any other field.
474147Seric **
484147Seric **	Not all of these are actually handled specially by sendmail
494147Seric **	at this time.  They are included as placeholders, to let
504147Seric **	you know that "someday" I intend to have sendmail do
514147Seric **	something with them.
522897Seric */
532897Seric 
542897Seric struct hdrinfo	HdrInfo[] =
552897Seric {
568060Seric 		/* originator fields, most to least significant  */
5711417Seric 	"resent-sender",	H_FROM|H_RESENT,
5811417Seric 	"resent-from",		H_FROM|H_RESENT,
5925686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
609055Seric 	"sender",		H_FROM,
619055Seric 	"from",			H_FROM,
6225686Seric 	"reply-to",		H_FROM,
639055Seric 	"full-name",		H_ACHECK,
6464682Seric 	"return-receipt-to",	H_FROM|H_RECEIPTTO,
6557359Seric 	"errors-to",		H_FROM|H_ERRORSTO,
6658796Seric 
678060Seric 		/* destination fields */
689055Seric 	"to",			H_RCPT,
6911417Seric 	"resent-to",		H_RCPT|H_RESENT,
709055Seric 	"cc",			H_RCPT,
7111417Seric 	"resent-cc",		H_RCPT|H_RESENT,
729055Seric 	"bcc",			H_RCPT|H_ACHECK,
7311417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
7456215Seric 	"apparently-to",	H_RCPT,
7558796Seric 
768060Seric 		/* message identification and control */
7711417Seric 	"message-id",		0,
7811417Seric 	"resent-message-id",	H_RESENT,
799055Seric 	"message",		H_EOH,
809055Seric 	"text",			H_EOH,
8158796Seric 
8211417Seric 		/* date fields */
8311417Seric 	"date",			0,
8411417Seric 	"resent-date",		H_RESENT,
8558796Seric 
868060Seric 		/* trace fields */
879055Seric 	"received",		H_TRACE|H_FORCE,
8859318Seric 	"x400-received",	H_TRACE|H_FORCE,
899055Seric 	"via",			H_TRACE|H_FORCE,
909055Seric 	"mail-from",		H_TRACE|H_FORCE,
918060Seric 
9258796Seric 		/* miscellaneous fields */
9358796Seric 	"comments",		H_FORCE,
9463753Seric 	"return-path",		H_FORCE|H_ACHECK,
9558796Seric 
969055Seric 	NULL,			0,
972897Seric };
984166Seric 
994166Seric 
1004166Seric 
1014282Seric /*
1024282Seric **  Location of system files/databases/etc.
1034282Seric */
1044282Seric 
10558082Seric char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
1069039Seric 
1079064Seric 
1089064Seric 
1099039Seric /*
11058082Seric **  Privacy values
11158082Seric */
11258082Seric 
11358082Seric struct prival PrivacyValues[] =
11458082Seric {
11558082Seric 	"public",		PRIV_PUBLIC,
11658082Seric 	"needmailhelo",		PRIV_NEEDMAILHELO,
11758114Seric 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
11858082Seric 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
11958082Seric 	"noexpn",		PRIV_NOEXPN,
12058082Seric 	"novrfy",		PRIV_NOVRFY,
12164333Seric 	"restrictmailq",	PRIV_RESTRICTMAILQ,
12264333Seric 	"restrictqrun",		PRIV_RESTRICTQRUN,
12358789Seric 	"authwarnings",		PRIV_AUTHWARNINGS,
12458082Seric 	"goaway",		PRIV_GOAWAY,
12558789Seric 	NULL,			0,
12658082Seric };
12758082Seric 
12858082Seric 
12958082Seric 
13058082Seric /*
13124943Seric **  Miscellaneous stuff.
1329039Seric */
1339039Seric 
13424943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
13564644Seric 
13664644Seric 
13764644Seric /*
13864644Seric **  Following should be config parameters (and probably will be in
13964644Seric **  future releases).  In the meantime, setting these is considered
14064644Seric **  unsupported, and is intentionally undocumented.
14164644Seric */
14264644Seric 
14364644Seric #ifdef BROKENSMTPPEERS
14464644Seric bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
14564644Seric #else
14664644Seric bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
14764644Seric #endif
14864644Seric #ifdef NOLOOPBACKCHECK
14964644Seric bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
15064644Seric #else
15164644Seric bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
15264644Seric #endif
15364644Seric 
15424943Seric /*
15524943Seric **  SETDEFAULTS -- set default values
15624943Seric **
15724943Seric **	Because of the way freezing is done, these must be initialized
15824943Seric **	using direct code.
15924943Seric **
16024943Seric **	Parameters:
16158734Seric **		e -- the default envelope.
16224943Seric **
16324943Seric **	Returns:
16424943Seric **		none.
16524943Seric **
16624943Seric **	Side Effects:
16724943Seric **		Initializes a bunch of global variables to their
16824943Seric **		default values.
16924943Seric */
17024943Seric 
17158737Seric #define DAYS		* 24 * 60 * 60
17258737Seric 
17358734Seric setdefaults(e)
17458734Seric 	register ENVELOPE *e;
17524943Seric {
17657438Seric 	SpaceSub = ' ';				/* option B */
17757438Seric 	QueueLA = 8;				/* option x */
17857438Seric 	RefuseLA = 12;				/* option X */
17957438Seric 	WkRecipFact = 30000L;			/* option y */
18057438Seric 	WkClassFact = 1800L;			/* option z */
18157438Seric 	WkTimeFact = 90000L;			/* option Z */
18257438Seric 	QueueFactor = WkRecipFact * 20;		/* option q */
18363787Seric 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
18457438Seric 						/* option F */
18557438Seric 	DefUid = 1;				/* option u */
18657438Seric 	DefGid = 1;				/* option g */
18757438Seric 	CheckpointInterval = 10;		/* option C */
18857438Seric 	MaxHopCount = 25;			/* option h */
18958734Seric 	e->e_sendmode = SM_FORK;		/* option d */
19058734Seric 	e->e_errormode = EM_PRINT;		/* option e */
19159709Seric 	SevenBit = FALSE;			/* option 7 */
19257438Seric 	MaxMciCache = 1;			/* option k */
19357438Seric 	MciCacheTimeout = 300;			/* option K */
19457438Seric 	LogLevel = 9;				/* option L */
19558112Seric 	settimeouts(NULL);			/* option r */
19658737Seric 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
19758737Seric 	TimeOuts.to_q_warning = 0;		/* option T */
19858853Seric 	PrivacyFlags = 0;			/* option p */
19940973Sbostic 	setdefuser();
20053654Seric 	setupmaps();
20157402Seric 	setupmailers();
20224943Seric }
203294Seric 
20440973Sbostic 
2054326Seric /*
20640973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
20740973Sbostic */
20840973Sbostic 
20940973Sbostic setdefuser()
21040973Sbostic {
21140973Sbostic 	struct passwd *defpwent;
21257386Seric 	static char defuserbuf[40];
21340973Sbostic 
21457386Seric 	DefUser = defuserbuf;
21540973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
21657386Seric 		strcpy(defuserbuf, defpwent->pw_name);
21740973Sbostic 	else
21857386Seric 		strcpy(defuserbuf, "nobody");
21940973Sbostic }
22053654Seric /*
22156836Seric **  HOST_MAP_INIT -- initialize host class structures
22256836Seric */
22356836Seric 
22456836Seric bool
22560219Seric host_map_init(map, args)
22656836Seric 	MAP *map;
22756836Seric 	char *args;
22856836Seric {
22956836Seric 	register char *p = args;
23056836Seric 
23156836Seric 	for (;;)
23256836Seric 	{
23358050Seric 		while (isascii(*p) && isspace(*p))
23456836Seric 			p++;
23556836Seric 		if (*p != '-')
23656836Seric 			break;
23756836Seric 		switch (*++p)
23856836Seric 		{
23956836Seric 		  case 'a':
24056836Seric 			map->map_app = ++p;
24156836Seric 			break;
24256836Seric 		}
24358050Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
24456836Seric 			p++;
24556836Seric 		if (*p != '\0')
24656836Seric 			*p++ = '\0';
24756836Seric 	}
24856836Seric 	if (map->map_app != NULL)
24956836Seric 		map->map_app = newstr(map->map_app);
25056836Seric 	return TRUE;
25156836Seric }
25257402Seric /*
25357402Seric **  SETUPMAILERS -- initialize default mailers
25457402Seric */
25556836Seric 
25657402Seric setupmailers()
25757402Seric {
25857402Seric 	char buf[100];
25957402Seric 
26057403Seric 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
26157403Seric 	makemailer(buf);
26257403Seric 
26359883Seric 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
26457402Seric 	makemailer(buf);
26557402Seric 
26657402Seric 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
26757402Seric 	makemailer(buf);
26857402Seric }
26956836Seric /*
27060207Seric **  SETUPMAPS -- set up map classes
27160207Seric */
27260207Seric 
27360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
27460207Seric 	{ \
27560207Seric 		extern bool parse __P((MAP *, char *)); \
27660207Seric 		extern bool open __P((MAP *, int)); \
27760207Seric 		extern void close __P((MAP *)); \
27860207Seric 		extern char *lookup __P((MAP *, char *, char **, int *)); \
27960207Seric 		extern void store __P((MAP *, char *, char *)); \
28060207Seric 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
28160207Seric 		s->s_mapclass.map_cname = name; \
28260207Seric 		s->s_mapclass.map_ext = ext; \
28360207Seric 		s->s_mapclass.map_cflags = flags; \
28460207Seric 		s->s_mapclass.map_parse = parse; \
28560207Seric 		s->s_mapclass.map_open = open; \
28660207Seric 		s->s_mapclass.map_close = close; \
28760207Seric 		s->s_mapclass.map_lookup = lookup; \
28860207Seric 		s->s_mapclass.map_store = store; \
28960207Seric 	}
29060207Seric 
29160207Seric setupmaps()
29260207Seric {
29360207Seric 	register STAB *s;
29460207Seric 
29560207Seric #ifdef NEWDB
29660207Seric 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
29760207Seric 		map_parseargs, hash_map_open, db_map_close,
29860207Seric 		db_map_lookup, db_map_store);
29960207Seric 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
30060207Seric 		map_parseargs, bt_map_open, db_map_close,
30160207Seric 		db_map_lookup, db_map_store);
30260207Seric #endif
30360207Seric 
30460207Seric #ifdef NDBM
30560207Seric 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
30660207Seric 		map_parseargs, ndbm_map_open, ndbm_map_close,
30760207Seric 		ndbm_map_lookup, ndbm_map_store);
30860207Seric #endif
30960207Seric 
31060207Seric #ifdef NIS
31160207Seric 	MAPDEF("nis", NULL, MCF_ALIASOK,
31260207Seric 		map_parseargs, nis_map_open, nis_map_close,
31360207Seric 		nis_map_lookup, nis_map_store);
31460207Seric #endif
31560207Seric 
31660207Seric 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
31760207Seric 		map_parseargs, stab_map_open, stab_map_close,
31860207Seric 		stab_map_lookup, stab_map_store);
31960207Seric 
32060207Seric 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
32160207Seric 		map_parseargs, impl_map_open, impl_map_close,
32260207Seric 		impl_map_lookup, impl_map_store);
32360207Seric 
32460207Seric 	/* host DNS lookup */
32560207Seric 	MAPDEF("host", NULL, 0,
32660207Seric 		host_map_init, null_map_open, null_map_close,
32760207Seric 		host_map_lookup, null_map_store);
32860207Seric 
32960207Seric 	/* dequote map */
33060207Seric 	MAPDEF("dequote", NULL, 0,
33160207Seric 		dequote_init, null_map_open, null_map_close,
33260207Seric 		dequote_map, null_map_store);
33360207Seric 
33460207Seric #if 0
33560207Seric # ifdef USERDB
33660207Seric 	/* user database */
33760207Seric 	MAPDEF("udb", ".db", 0,
33860207Seric 		udb_map_parse, null_map_open, null_map_close,
33960207Seric 		udb_map_lookup, null_map_store);
34060207Seric # endif
34160207Seric #endif
34260207Seric }
34360207Seric 
34460207Seric #undef MAPDEF
34560207Seric /*
3469369Seric **  USERNAME -- return the user id of the logged in user.
3479369Seric **
3489369Seric **	Parameters:
3499369Seric **		none.
3509369Seric **
3519369Seric **	Returns:
3529369Seric **		The login name of the logged in user.
3539369Seric **
3549369Seric **	Side Effects:
3559369Seric **		none.
3569369Seric **
3579369Seric **	Notes:
3589369Seric **		The return value is statically allocated.
3599369Seric */
3609369Seric 
3619369Seric char *
3629369Seric username()
3639369Seric {
36417469Seric 	static char *myname = NULL;
3659369Seric 	extern char *getlogin();
36619904Smiriam 	register struct passwd *pw;
3679369Seric 
36817469Seric 	/* cache the result */
36917469Seric 	if (myname == NULL)
37017469Seric 	{
37117469Seric 		myname = getlogin();
37217469Seric 		if (myname == NULL || myname[0] == '\0')
37317469Seric 		{
37463787Seric 			pw = getpwuid(RealUid);
37517469Seric 			if (pw != NULL)
37640993Sbostic 				myname = newstr(pw->pw_name);
37717469Seric 		}
37819904Smiriam 		else
37919904Smiriam 		{
38063787Seric 			uid_t uid = RealUid;
38119873Smiriam 
38240993Sbostic 			myname = newstr(myname);
38340993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
38458736Seric 			      (uid != 0 && uid != pw->pw_uid))
38519904Smiriam 			{
38658736Seric 				pw = getpwuid(uid);
38724945Seric 				if (pw != NULL)
38840993Sbostic 					myname = newstr(pw->pw_name);
38919873Smiriam 			}
39019873Smiriam 		}
39117469Seric 		if (myname == NULL || myname[0] == '\0')
39217469Seric 		{
39358151Seric 			syserr("554 Who are you?");
39417469Seric 			myname = "postmaster";
39517469Seric 		}
39617469Seric 	}
39717469Seric 
39817469Seric 	return (myname);
3999369Seric }
4009369Seric /*
4014190Seric **  TTYPATH -- Get the path of the user's tty
402294Seric **
403294Seric **	Returns the pathname of the user's tty.  Returns NULL if
404294Seric **	the user is not logged in or if s/he has write permission
405294Seric **	denied.
406294Seric **
407294Seric **	Parameters:
408294Seric **		none
409294Seric **
410294Seric **	Returns:
411294Seric **		pathname of the user's tty.
412294Seric **		NULL if not logged in or write permission denied.
413294Seric **
414294Seric **	Side Effects:
415294Seric **		none.
416294Seric **
417294Seric **	WARNING:
418294Seric **		Return value is in a local buffer.
419294Seric **
420294Seric **	Called By:
421294Seric **		savemail
422294Seric */
423294Seric 
424294Seric char *
425294Seric ttypath()
426294Seric {
427294Seric 	struct stat stbuf;
428294Seric 	register char *pathn;
429294Seric 	extern char *ttyname();
4304081Seric 	extern char *getlogin();
431294Seric 
432294Seric 	/* compute the pathname of the controlling tty */
4339369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
4349369Seric 	    (pathn = ttyname(0)) == NULL)
435294Seric 	{
436294Seric 		errno = 0;
437294Seric 		return (NULL);
438294Seric 	}
439294Seric 
440294Seric 	/* see if we have write permission */
4412967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
442294Seric 	{
443294Seric 		errno = 0;
444294Seric 		return (NULL);
445294Seric 	}
446294Seric 
447294Seric 	/* see if the user is logged in */
448294Seric 	if (getlogin() == NULL)
449294Seric 		return (NULL);
450294Seric 
451294Seric 	/* looks good */
452294Seric 	return (pathn);
453294Seric }
4542967Seric /*
4552967Seric **  CHECKCOMPAT -- check for From and To person compatible.
4562967Seric **
4572967Seric **	This routine can be supplied on a per-installation basis
4582967Seric **	to determine whether a person is allowed to send a message.
4592967Seric **	This allows restriction of certain types of internet
4602967Seric **	forwarding or registration of users.
4612967Seric **
4622967Seric **	If the hosts are found to be incompatible, an error
46357454Seric **	message should be given using "usrerr" and 0 should
4642967Seric **	be returned.
4652967Seric **
4664288Seric **	'NoReturn' can be set to suppress the return-to-sender
4674288Seric **	function; this should be done on huge messages.
4684288Seric **
4692967Seric **	Parameters:
4702967Seric **		to -- the person being sent to.
4712967Seric **
4722967Seric **	Returns:
47357459Seric **		an exit status
4742967Seric **
4752967Seric **	Side Effects:
4762967Seric **		none (unless you include the usrerr stuff)
4772967Seric */
4782967Seric 
47955012Seric checkcompat(to, e)
4802967Seric 	register ADDRESS *to;
48155012Seric 	register ENVELOPE *e;
4822967Seric {
48312133Seric # ifdef lint
48412133Seric 	if (to == NULL)
48512133Seric 		to++;
48663834Seric # endif /* lint */
48764402Seric 
48864402Seric 	if (tTd(49, 1))
48964402Seric 		printf("checkcompat(to=%s, from=%s)\n",
49064402Seric 			to->q_paddr, e->e_from.q_paddr);
49164402Seric 
49210698Seric # ifdef EXAMPLE_CODE
49310698Seric 	/* this code is intended as an example only */
4944437Seric 	register STAB *s;
4954437Seric 
4964437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
49755012Seric 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
4989369Seric 	    to->q_mailer == s->s_mailer)
4994437Seric 	{
50058151Seric 		usrerr("553 No ARPA mail through this machine: see your system administration");
50110698Seric 		/* NoReturn = TRUE; to supress return copy */
50257459Seric 		return (EX_UNAVAILABLE);
5034437Seric 	}
50456795Seric # endif /* EXAMPLE_CODE */
50557459Seric 	return (EX_OK);
5062967Seric }
5079369Seric /*
50864035Seric **  SETSIGNAL -- set a signal handler
50964035Seric **
51064035Seric **	This is essentially old BSD "signal(3)".
51164035Seric */
51264035Seric 
51364561Seric sigfunc_t
51464035Seric setsignal(sig, handler)
51564035Seric 	int sig;
51664561Seric 	sigfunc_t handler;
51764035Seric {
51864380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
51964035Seric 	return signal(sig, handler);
52064035Seric #else
52164035Seric 	struct sigaction n, o;
52264035Seric 
52364035Seric 	bzero(&n, sizeof n);
52464035Seric 	n.sa_handler = handler;
52564035Seric 	if (sigaction(sig, &n, &o) < 0)
52664035Seric 		return SIG_ERR;
52764035Seric 	return o.sa_handler;
52864035Seric #endif
52964035Seric }
53064035Seric /*
5319369Seric **  HOLDSIGS -- arrange to hold all signals
5329369Seric **
5339369Seric **	Parameters:
5349369Seric **		none.
5359369Seric **
5369369Seric **	Returns:
5379369Seric **		none.
5389369Seric **
5399369Seric **	Side Effects:
5409369Seric **		Arranges that signals are held.
5419369Seric */
5429369Seric 
5439369Seric holdsigs()
5449369Seric {
5459369Seric }
5469369Seric /*
5479369Seric **  RLSESIGS -- arrange to release all signals
5489369Seric **
5499369Seric **	This undoes the effect of holdsigs.
5509369Seric **
5519369Seric **	Parameters:
5529369Seric **		none.
5539369Seric **
5549369Seric **	Returns:
5559369Seric **		none.
5569369Seric **
5579369Seric **	Side Effects:
5589369Seric **		Arranges that signals are released.
5599369Seric */
5609369Seric 
5619369Seric rlsesigs()
5629369Seric {
5639369Seric }
56414872Seric /*
56564705Seric **  INIT_MD -- do machine dependent initializations
56664705Seric **
56764705Seric **	Systems that have global modes that should be set should do
56864705Seric **	them here rather than in main.
56964705Seric */
57064705Seric 
57164705Seric #ifdef _AUX_SOURCE
57264705Seric # include	<compat.h>
57364705Seric #endif
57464705Seric 
57564825Seric init_md(argc, argv)
57664825Seric 	int argc;
57764825Seric 	char **argv;
57864705Seric {
57964705Seric #ifdef _AUX_SOURCE
58064705Seric 	setcompat(getcompat() | COMPAT_BSDPROT);
58164705Seric #endif
58264705Seric }
58364705Seric /*
58414872Seric **  GETLA -- get the current load average
58514872Seric **
58614881Seric **	This code stolen from la.c.
58714881Seric **
58814872Seric **	Parameters:
58914872Seric **		none.
59014872Seric **
59114872Seric **	Returns:
59214872Seric **		The current load average as an integer.
59314872Seric **
59414872Seric **	Side Effects:
59514872Seric **		none.
59614872Seric */
59714872Seric 
59851920Seric /* try to guess what style of load average we have */
59951920Seric #define LA_ZERO		1	/* always return load average as zero */
60064360Seric #define LA_INT		2	/* read kmem for avenrun; interpret as long */
60151920Seric #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
60251920Seric #define LA_SUBR		4	/* call getloadavg */
60364295Seric #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
60464360Seric #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
60514872Seric 
60663962Seric /* do guesses based on general OS type */
60751920Seric #ifndef LA_TYPE
60863962Seric # define LA_TYPE	LA_ZERO
60951920Seric #endif
61051920Seric 
61164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
61251920Seric 
61314872Seric #include <nlist.h>
61414872Seric 
61551920Seric #ifndef LA_AVENRUN
61663962Seric # ifdef SYSTEM5
61763962Seric #  define LA_AVENRUN	"avenrun"
61863962Seric # else
61963962Seric #  define LA_AVENRUN	"_avenrun"
62063962Seric # endif
62151920Seric #endif
62251920Seric 
62351920Seric /* _PATH_UNIX should be defined in <paths.h> */
62451920Seric #ifndef _PATH_UNIX
62563962Seric # if defined(SYSTEM5)
62663962Seric #  define _PATH_UNIX	"/unix"
62763962Seric # else
62863962Seric #  define _PATH_UNIX	"/vmunix"
62963962Seric # endif
63051920Seric #endif
63151920Seric 
63214872Seric struct	nlist Nl[] =
63314872Seric {
63451920Seric 	{ LA_AVENRUN },
63514872Seric #define	X_AVENRUN	0
63614872Seric 	{ 0 },
63714872Seric };
63814872Seric 
63960583Seric #ifndef FSHIFT
64060583Seric # if defined(unixpc)
64160583Seric #  define FSHIFT	5
64260583Seric # endif
64357736Seric 
64460583Seric # if defined(__alpha)
64560583Seric #  define FSHIFT	10
64660583Seric # endif
64757977Seric 
64864360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
64951920Seric #  define FSHIFT	8
65060583Seric # endif
65157736Seric #endif
65260583Seric 
65364360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
65451920Seric #  define FSCALE	(1 << FSHIFT)
65551920Seric #endif
65640930Srick 
65714872Seric getla()
65814872Seric {
65914872Seric 	static int kmem = -1;
66051920Seric #if LA_TYPE == LA_INT
66124943Seric 	long avenrun[3];
66251920Seric #else
66364360Seric # if LA_TYPE == LA_SHORT
66464360Seric 	short avenrun[3];
66564360Seric # else
66651920Seric 	double avenrun[3];
66764360Seric # endif
66851920Seric #endif
66925615Seric 	extern off_t lseek();
67057736Seric 	extern int errno;
67114872Seric 
67214872Seric 	if (kmem < 0)
67314872Seric 	{
67424945Seric 		kmem = open("/dev/kmem", 0, 0);
67514872Seric 		if (kmem < 0)
67657736Seric 		{
67757736Seric 			if (tTd(3, 1))
67857736Seric 				printf("getla: open(/dev/kmem): %s\n",
67957736Seric 					errstring(errno));
68014872Seric 			return (-1);
68157736Seric 		}
68251920Seric 		(void) fcntl(kmem, F_SETFD, 1);
68357736Seric 		if (nlist(_PATH_UNIX, Nl) < 0)
68457736Seric 		{
68557736Seric 			if (tTd(3, 1))
68657736Seric 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
68757736Seric 					errstring(errno));
68814872Seric 			return (-1);
68957736Seric 		}
69059253Seric 		if (Nl[X_AVENRUN].n_value == 0)
69159253Seric 		{
69259253Seric 			if (tTd(3, 1))
69359253Seric 				printf("getla: nlist(%s, %s) ==> 0\n",
69459253Seric 					_PATH_UNIX, LA_AVENRUN);
69559253Seric 			return (-1);
69659253Seric 		}
69714872Seric 	}
69857736Seric 	if (tTd(3, 20))
69957736Seric 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
70024945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
70123118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
70219967Seric 	{
70319967Seric 		/* thank you Ian */
70457736Seric 		if (tTd(3, 1))
70557736Seric 			printf("getla: lseek or read: %s\n", errstring(errno));
70619967Seric 		return (-1);
70719967Seric 	}
70864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
70957736Seric 	if (tTd(3, 5))
71057736Seric 	{
71157736Seric 		printf("getla: avenrun = %d", avenrun[0]);
71257736Seric 		if (tTd(3, 15))
71357736Seric 			printf(", %d, %d", avenrun[1], avenrun[2]);
71457736Seric 		printf("\n");
71557736Seric 	}
71657736Seric 	if (tTd(3, 1))
71757736Seric 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71824943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
71951920Seric #else
72057736Seric 	if (tTd(3, 5))
72157736Seric 	{
72257736Seric 		printf("getla: avenrun = %g", avenrun[0]);
72357736Seric 		if (tTd(3, 15))
72457736Seric 			printf(", %g, %g", avenrun[1], avenrun[2]);
72557736Seric 		printf("\n");
72657736Seric 	}
72757736Seric 	if (tTd(3, 1))
72857736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
72951920Seric 	return ((int) (avenrun[0] + 0.5));
73051920Seric #endif
73114872Seric }
73214872Seric 
73351773Seric #else
73451920Seric #if LA_TYPE == LA_SUBR
73551773Seric 
73664718Seric #ifdef DGUX
73764718Seric 
73864718Seric #include <sys/dg_sys_info.h>
73964718Seric 
74064718Seric int getla()
74164718Seric {
74264718Seric 	struct dg_sys_info_load_info load_info;
74364718Seric 
74464718Seric 	dg_sys_info((long *)&load_info,
74564718Seric 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
74664718Seric 
74764718Seric 	return((int) (load_info.one_minute + 0.5));
74864718Seric }
74964718Seric 
75064718Seric #else
75164718Seric 
75251773Seric getla()
75351773Seric {
75451920Seric 	double avenrun[3];
75551920Seric 
75651920Seric 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
75757736Seric 	{
75857736Seric 		if (tTd(3, 1))
75957736Seric 			perror("getla: getloadavg failed:");
76051920Seric 		return (-1);
76157736Seric 	}
76257736Seric 	if (tTd(3, 1))
76357736Seric 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
76451920Seric 	return ((int) (avenrun[0] + 0.5));
76551773Seric }
76651773Seric 
76764718Seric #endif /* DGUX */
76851773Seric #else
76964295Seric #if LA_TYPE == LA_MACH
77051773Seric 
77164295Seric /*
77265173Seric **  This has been tested on NEXTSTEP release 2.1/3.X.
77364295Seric */
77464295Seric 
77565173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
77665173Seric # include <mach/mach.h>
77765173Seric #else
77865173Seric # include <mach.h>
77965173Seric #endif
78064295Seric 
78151773Seric getla()
78251773Seric {
78364295Seric 	processor_set_t default_set;
78464295Seric 	kern_return_t error;
78564295Seric 	unsigned int info_count;
78664295Seric 	struct processor_set_basic_info info;
78764295Seric 	host_t host;
78864295Seric 
78964295Seric 	error = processor_set_default(host_self(), &default_set);
79064295Seric 	if (error != KERN_SUCCESS)
79164295Seric 		return -1;
79264295Seric 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
79364295Seric 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
79464295Seric 			       &host, (processor_set_info_t)&info,
79564295Seric 			       &info_count) != KERN_SUCCESS)
79664295Seric 	{
79764295Seric 		return -1;
79864295Seric 	}
79964295Seric 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
80064295Seric }
80164295Seric 
80264295Seric 
80364295Seric #else
80464295Seric 
80564295Seric getla()
80664295Seric {
80757736Seric 	if (tTd(3, 1))
80857736Seric 		printf("getla: ZERO\n");
80951920Seric 	return (0);
81051773Seric }
81151773Seric 
81251773Seric #endif
81351773Seric #endif
81464295Seric #endif
81524943Seric /*
81624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
81724943Seric **
81824943Seric **	Compares the message cost to the load average to decide.
81924943Seric **
82024943Seric **	Parameters:
82124943Seric **		pri -- the priority of the message in question.
82257438Seric **		ctime -- the message creation time.
82324943Seric **
82424943Seric **	Returns:
82524943Seric **		TRUE -- if this message should be queued up for the
82624943Seric **			time being.
82724943Seric **		FALSE -- if the load is low enough to send this message.
82824943Seric **
82924943Seric **	Side Effects:
83024943Seric **		none.
83124943Seric */
83224943Seric 
83324943Seric bool
83457438Seric shouldqueue(pri, ctime)
83524943Seric 	long pri;
83657438Seric 	time_t ctime;
83724943Seric {
83851920Seric 	if (CurrentLA < QueueLA)
83924943Seric 		return (FALSE);
84058132Seric 	if (CurrentLA >= RefuseLA)
84158132Seric 		return (TRUE);
84251920Seric 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
84324943Seric }
84424943Seric /*
84553037Seric **  REFUSECONNECTIONS -- decide if connections should be refused
84653037Seric **
84753037Seric **	Parameters:
84853037Seric **		none.
84953037Seric **
85053037Seric **	Returns:
85153037Seric **		TRUE if incoming SMTP connections should be refused
85253037Seric **			(for now).
85353037Seric **		FALSE if we should accept new work.
85453037Seric **
85553037Seric **	Side Effects:
85653037Seric **		none.
85753037Seric */
85853037Seric 
85953037Seric bool
86053037Seric refuseconnections()
86153037Seric {
86259156Seric #ifdef XLA
86359156Seric 	if (!xla_smtp_ok())
86459156Seric 		return TRUE;
86559156Seric #endif
86659156Seric 
86753037Seric 	/* this is probably too simplistic */
86858132Seric 	return (CurrentLA >= RefuseLA);
86953037Seric }
87053037Seric /*
87124943Seric **  SETPROCTITLE -- set process title for ps
87224943Seric **
87324943Seric **	Parameters:
87458674Seric **		fmt -- a printf style format string.
87558674Seric **		a, b, c -- possible parameters to fmt.
87624943Seric **
87724943Seric **	Returns:
87824943Seric **		none.
87924943Seric **
88024943Seric **	Side Effects:
88124943Seric **		Clobbers argv of our main procedure so ps(1) will
88224943Seric **		display the title.
88324943Seric */
88424943Seric 
88558689Seric #ifdef SETPROCTITLE
886*66002Seric # ifdef HASSETPROCTITLE
887*66002Seric    *** ERROR ***  Cannot have both SETPROCTITLE and HASSETPROCTITLE defined
888*66002Seric # endif
88958689Seric # ifdef __hpux
89058689Seric #  include <sys/pstat.h>
89158689Seric # endif
89259732Seric # ifdef BSD4_4
89359732Seric #  include <machine/vmparam.h>
89459732Seric #  include <sys/exec.h>
89564247Seric #  ifdef __bsdi__
89664247Seric #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
89764843Seric #   define PROCTITLEPAD	'\0'
89864247Seric #  endif
89959992Seric #  ifdef PS_STRINGS
90059992Seric #   define SETPROC_STATIC static
90159992Seric #  endif
90259732Seric # endif
90359732Seric # ifndef SETPROC_STATIC
90459732Seric #  define SETPROC_STATIC
90559732Seric # endif
90658689Seric #endif
90758689Seric 
90864843Seric #ifndef PROCTITLEPAD
90964843Seric # define PROCTITLEPAD	' '
91064843Seric #endif
91164843Seric 
91265982Seric #ifndef HASSETPROCTITLE
91365982Seric 
91424943Seric /*VARARGS1*/
91557642Seric #ifdef __STDC__
91657642Seric setproctitle(char *fmt, ...)
91757642Seric #else
91857642Seric setproctitle(fmt, va_alist)
91924943Seric 	char *fmt;
92057642Seric 	va_dcl
92157642Seric #endif
92224943Seric {
92324943Seric # ifdef SETPROCTITLE
92424943Seric 	register char *p;
92525049Seric 	register int i;
92659732Seric 	SETPROC_STATIC char buf[MAXLINE];
92756852Seric 	VA_LOCAL_DECL
92858689Seric #  ifdef __hpux
92958689Seric 	union pstun pst;
93058689Seric #  endif
93124943Seric 	extern char **Argv;
93224943Seric 	extern char *LastArgv;
93324943Seric 
93458674Seric 	p = buf;
93524943Seric 
93658674Seric 	/* print sendmail: heading for grep */
93758674Seric 	(void) strcpy(p, "sendmail: ");
93858674Seric 	p += strlen(p);
93924943Seric 
94058674Seric 	/* print the argument string */
94158674Seric 	VA_START(fmt);
94258674Seric 	(void) vsprintf(p, fmt, ap);
94356852Seric 	VA_END;
94454996Seric 
94558674Seric 	i = strlen(buf);
94658689Seric 
94758689Seric #  ifdef __hpux
94858689Seric 	pst.pst_command = buf;
94958689Seric 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
95058689Seric #  else
95159992Seric #   ifdef PS_STRINGS
95259732Seric 	PS_STRINGS->ps_nargvstr = 1;
95359732Seric 	PS_STRINGS->ps_argvstr = buf;
95459732Seric #   else
95554996Seric 	if (i > LastArgv - Argv[0] - 2)
95625049Seric 	{
95754996Seric 		i = LastArgv - Argv[0] - 2;
95858674Seric 		buf[i] = '\0';
95925049Seric 	}
96058674Seric 	(void) strcpy(Argv[0], buf);
96154997Seric 	p = &Argv[0][i];
96224943Seric 	while (p < LastArgv)
96364843Seric 		*p++ = PROCTITLEPAD;
96459732Seric #   endif
96558689Seric #  endif
96656795Seric # endif /* SETPROCTITLE */
96724943Seric }
96865982Seric 
96965982Seric #endif
97025698Seric /*
97125698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
97225698Seric **
97325698Seric **	Parameters:
97425698Seric **		none.
97525698Seric **
97625698Seric **	Returns:
97725698Seric **		none.
97825698Seric **
97925698Seric **	Side Effects:
98025698Seric **		Picks up extant zombies.
98125698Seric */
98225698Seric 
98346928Sbostic void
98425698Seric reapchild()
98525698Seric {
98664738Seric 	int olderrno = errno;
98763966Seric # ifdef HASWAITPID
98860219Seric 	auto int status;
98960560Seric 	int count;
99060560Seric 	int pid;
99160219Seric 
99260560Seric 	count = 0;
99360560Seric 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
99460560Seric 	{
99560560Seric 		if (count++ > 1000)
99660560Seric 		{
99760560Seric 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
99860560Seric 				pid, status);
99960560Seric 			break;
100060560Seric 		}
100160560Seric 	}
100260219Seric # else
100325698Seric # ifdef WNOHANG
100425698Seric 	union wait status;
100525698Seric 
100663753Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
100725698Seric 		continue;
100856795Seric # else /* WNOHANG */
100925698Seric 	auto int status;
101025698Seric 
101160219Seric 	while (wait(&status) > 0)
101225698Seric 		continue;
101356795Seric # endif /* WNOHANG */
101460219Seric # endif
101564035Seric # ifdef SYS5SIGNALS
101664035Seric 	(void) setsignal(SIGCHLD, reapchild);
101758061Seric # endif
101864738Seric 	errno = olderrno;
101925698Seric }
102055418Seric /*
102155418Seric **  UNSETENV -- remove a variable from the environment
102255418Seric **
102355418Seric **	Not needed on newer systems.
102455418Seric **
102555418Seric **	Parameters:
102655418Seric **		name -- the string name of the environment variable to be
102755418Seric **			deleted from the current environment.
102855418Seric **
102955418Seric **	Returns:
103055418Seric **		none.
103155418Seric **
103255418Seric **	Globals:
103355418Seric **		environ -- a pointer to the current environment.
103455418Seric **
103555418Seric **	Side Effects:
103655418Seric **		Modifies environ.
103755418Seric */
103855418Seric 
103963962Seric #ifndef HASUNSETENV
104055418Seric 
104155418Seric void
104255418Seric unsetenv(name)
104355418Seric 	char *name;
104455418Seric {
104555418Seric 	extern char **environ;
104655418Seric 	register char **pp;
104755418Seric 	int len = strlen(name);
104855418Seric 
104955418Seric 	for (pp = environ; *pp != NULL; pp++)
105055418Seric 	{
105155418Seric 		if (strncmp(name, *pp, len) == 0 &&
105255418Seric 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
105355418Seric 			break;
105455418Seric 	}
105555418Seric 
105655418Seric 	for (; *pp != NULL; pp++)
105755418Seric 		*pp = pp[1];
105855418Seric }
105955418Seric 
106063962Seric #endif
106156215Seric /*
106256215Seric **  GETDTABLESIZE -- return number of file descriptors
106356215Seric **
106456215Seric **	Only on non-BSD systems
106556215Seric **
106656215Seric **	Parameters:
106756215Seric **		none
106856215Seric **
106956215Seric **	Returns:
107056215Seric **		size of file descriptor table
107156215Seric **
107256215Seric **	Side Effects:
107356215Seric **		none
107456215Seric */
107556215Seric 
107663787Seric #ifdef SOLARIS
107763787Seric # include <sys/resource.h>
107863787Seric #endif
107956215Seric 
108056215Seric int
108163787Seric getdtsize()
108256215Seric {
108363787Seric #ifdef RLIMIT_NOFILE
108463787Seric 	struct rlimit rl;
108563787Seric 
108663787Seric 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
108763787Seric 		return rl.rlim_cur;
108863787Seric #endif
108963787Seric 
109064031Seric # ifdef HASGETDTABLESIZE
109164031Seric 	return getdtablesize();
109264031Seric # else
109364031Seric #  ifdef _SC_OPEN_MAX
109458689Seric 	return sysconf(_SC_OPEN_MAX);
109563787Seric #  else
109656215Seric 	return NOFILE;
109763787Seric #  endif
109858689Seric # endif
109956215Seric }
110057631Seric /*
110157631Seric **  UNAME -- get the UUCP name of this system.
110257631Seric */
110357631Seric 
110457943Seric #ifndef HASUNAME
110557631Seric 
110657631Seric int
110757631Seric uname(name)
110857631Seric 	struct utsname *name;
110957631Seric {
111057631Seric 	FILE *file;
111157631Seric 	char *n;
111257631Seric 
111357631Seric 	name->nodename[0] = '\0';
111457631Seric 
111557661Seric 	/* try /etc/whoami -- one line with the node name */
111657631Seric 	if ((file = fopen("/etc/whoami", "r")) != NULL)
111757631Seric 	{
111857661Seric 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
111957631Seric 		(void) fclose(file);
112057661Seric 		n = strchr(name->nodename, '\n');
112157631Seric 		if (n != NULL)
112257631Seric 			*n = '\0';
112357631Seric 		if (name->nodename[0] != '\0')
112457631Seric 			return (0);
112557631Seric 	}
112657631Seric 
112757661Seric 	/* try /usr/include/whoami.h -- has a #define somewhere */
112857631Seric 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
112957631Seric 	{
113057631Seric 		char buf[MAXLINE];
113157631Seric 
113257631Seric 		while (fgets(buf, MAXLINE, file) != NULL)
113357631Seric 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
113457631Seric 					NODE_LENGTH, name->nodename) > 0)
113557631Seric 				break;
113657631Seric 		(void) fclose(file);
113757631Seric 		if (name->nodename[0] != '\0')
113857631Seric 			return (0);
113957631Seric 	}
114057631Seric 
114157631Seric #ifdef TRUST_POPEN
114257631Seric 	/*
114357631Seric 	**  Popen is known to have security holes.
114457631Seric 	*/
114557631Seric 
114657661Seric 	/* try uuname -l to return local name */
114757631Seric 	if ((file = popen("uuname -l", "r")) != NULL)
114857631Seric 	{
114957661Seric 		(void) fgets(name, NODE_LENGTH + 1, file);
115057631Seric 		(void) pclose(file);
115157661Seric 		n = strchr(name, '\n');
115257631Seric 		if (n != NULL)
115357631Seric 			*n = '\0';
115457661Seric 		if (name->nodename[0] != '\0')
115557631Seric 			return (0);
115657631Seric 	}
115757631Seric #endif
115857631Seric 
115957631Seric 	return (-1);
116057631Seric }
116157943Seric #endif /* HASUNAME */
116258068Seric /*
116358068Seric **  INITGROUPS -- initialize groups
116458068Seric **
116558068Seric **	Stub implementation for System V style systems
116658068Seric */
116758068Seric 
116858068Seric #ifndef HASINITGROUPS
116958068Seric 
117058068Seric initgroups(name, basegid)
117158068Seric 	char *name;
117258068Seric 	int basegid;
117358068Seric {
117458068Seric 	return 0;
117558068Seric }
117658068Seric 
117758068Seric #endif
117858082Seric /*
117959289Seric **  SETSID -- set session id (for non-POSIX systems)
118059289Seric */
118159289Seric 
118259289Seric #ifndef HASSETSID
118359289Seric 
118463753Seric pid_t
118563753Seric setsid __P ((void))
118659289Seric {
118764155Seric #ifdef TIOCNOTTY
118864155Seric 	int fd;
118964155Seric 
119064155Seric 	fd = open("/dev/tty", 2);
119164155Seric 	if (fd >= 0)
119264155Seric 	{
119364155Seric 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
119464155Seric 		(void) close(fd);
119564155Seric 	}
119664155Seric #endif /* TIOCNOTTY */
119764705Seric # ifdef SYS5SETPGRP
119863753Seric 	return setpgrp();
119963753Seric # else
120064155Seric 	return setpgid(0, getpid());
120159289Seric # endif
120259289Seric }
120359289Seric 
120459289Seric #endif
120559289Seric /*
120664718Seric **  DGUX_INET_ADDR -- inet_addr for DG/UX
120764718Seric **
120864718Seric **	Data General DG/UX version of inet_addr returns a struct in_addr
120964718Seric **	instead of a long.  This patches things.
121064718Seric */
121164718Seric 
121264718Seric #ifdef DGUX
121364718Seric 
121464718Seric #undef inet_addr
121564718Seric 
121664718Seric long
121764718Seric dgux_inet_addr(host)
121864718Seric 	char *host;
121964718Seric {
122064718Seric 	struct in_addr haddr;
122164718Seric 
122264718Seric 	haddr = inet_addr(host);
122364718Seric 	return haddr.s_addr;
122464718Seric }
122564718Seric 
122664718Seric #endif
122764718Seric /*
122863969Seric **  GETOPT -- for old systems or systems with bogus implementations
122963969Seric */
123063969Seric 
123163969Seric #ifdef NEEDGETOPT
123263969Seric 
123363969Seric /*
123463969Seric  * Copyright (c) 1985 Regents of the University of California.
123563969Seric  * All rights reserved.  The Berkeley software License Agreement
123663969Seric  * specifies the terms and conditions for redistribution.
123763969Seric  */
123863969Seric 
123963969Seric 
124063969Seric /*
124163969Seric ** this version hacked to add `atend' flag to allow state machine
124263969Seric ** to reset if invoked by the program to scan args for a 2nd time
124363969Seric */
124463969Seric 
124563969Seric #if defined(LIBC_SCCS) && !defined(lint)
124663969Seric static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
124764072Seric #endif /* LIBC_SCCS and not lint */
124863969Seric 
124963969Seric #include <stdio.h>
125063969Seric 
125163969Seric /*
125263969Seric  * get option letter from argument vector
125363969Seric  */
125465352Seric #ifdef _CONVEX_SOURCE
125565352Seric extern int	optind, opterr;
125665352Seric #else
125765352Seric int	opterr = 1;		/* if error message should be printed */
125865352Seric int	optind = 1;		/* index into parent argv vector */
125965352Seric #endif
126065352Seric int	optopt;			/* character checked for validity */
126163969Seric char	*optarg;		/* argument associated with option */
126263969Seric 
126363969Seric #define BADCH	(int)'?'
126463969Seric #define EMSG	""
126563969Seric #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
126663969Seric 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
126763969Seric 
126863969Seric getopt(nargc,nargv,ostr)
126965160Seric 	int		nargc;
127065160Seric 	char *const	*nargv;
127165160Seric 	const char	*ostr;
127263969Seric {
127363969Seric 	static char	*place = EMSG;	/* option letter processing */
127463969Seric 	static char	atend = 0;
127563969Seric 	register char	*oli;		/* option letter list index */
127663969Seric 
127763969Seric 	if (atend) {
127863969Seric 		atend = 0;
127963969Seric 		place = EMSG;
128063969Seric 	}
128163969Seric 	if(!*place) {			/* update scanning pointer */
128264136Seric 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
128363969Seric 			atend++;
128463969Seric 			return(EOF);
128563969Seric 		}
128663969Seric 		if (*place == '-') {	/* found "--" */
128763969Seric 			++optind;
128863969Seric 			atend++;
128963969Seric 			return(EOF);
129063969Seric 		}
129163969Seric 	}				/* option letter okay? */
129264125Seric 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
129364136Seric 		if (!*place) ++optind;
129463969Seric 		tell(": illegal option -- ");
129563969Seric 	}
129663969Seric 	if (*++oli != ':') {		/* don't need argument */
129763969Seric 		optarg = NULL;
129863969Seric 		if (!*place) ++optind;
129963969Seric 	}
130063969Seric 	else {				/* need an argument */
130163969Seric 		if (*place) optarg = place;	/* no white space */
130263969Seric 		else if (nargc <= ++optind) {	/* no arg */
130363969Seric 			place = EMSG;
130463969Seric 			tell(": option requires an argument -- ");
130563969Seric 		}
130663969Seric 	 	else optarg = nargv[optind];	/* white space */
130763969Seric 		place = EMSG;
130863969Seric 		++optind;
130963969Seric 	}
131063969Seric 	return(optopt);			/* dump back option letter */
131163969Seric }
131263969Seric 
131363969Seric #endif
131463969Seric /*
131563969Seric **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
131663969Seric */
131763969Seric 
131863969Seric #ifdef NEEDVPRINTF
131963969Seric 
132063969Seric #define MAXARG	16
132163969Seric 
132263969Seric vfprintf(fp, fmt, ap)
132363969Seric 	FILE *	fp;
132463969Seric 	char *	fmt;
132563969Seric 	char **	ap;
132663969Seric {
132763969Seric 	char *	bp[MAXARG];
132863969Seric 	int	i = 0;
132963969Seric 
133063969Seric 	while (*ap && i < MAXARG)
133163969Seric 		bp[i++] = *ap++;
133263969Seric 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
133363969Seric 			 bp[4], bp[5], bp[6], bp[7],
133463969Seric 			 bp[8], bp[9], bp[10], bp[11],
133563969Seric 			 bp[12], bp[13], bp[14], bp[15]);
133663969Seric }
133763969Seric 
133863969Seric vsprintf(s, fmt, ap)
133963969Seric 	char *	s;
134063969Seric 	char *	fmt;
134163969Seric 	char **	ap;
134263969Seric {
134363969Seric 	char *	bp[MAXARG];
134463969Seric 	int	i = 0;
134563969Seric 
134663969Seric 	while (*ap && i < MAXARG)
134763969Seric 		bp[i++] = *ap++;
134863969Seric 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
134963969Seric 			bp[4], bp[5], bp[6], bp[7],
135063969Seric 			bp[8], bp[9], bp[10], bp[11],
135163969Seric 			bp[12], bp[13], bp[14], bp[15]);
135263969Seric }
135363969Seric 
135463969Seric #endif
135563969Seric /*
135665220Seric **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
135765208Seric **
135865211Seric **	Parameters:
135965211Seric **		shell -- the user's shell from /etc/passwd
136065211Seric **
136165211Seric **	Returns:
136265211Seric **		TRUE -- if it is ok to use this for unrestricted access.
136365211Seric **		FALSE -- if the shell is restricted.
136465208Seric */
136565208Seric 
136665587Seric #if !HASGETUSERSHELL
136765587Seric 
136865587Seric # ifndef _PATH_SHELLS
136965587Seric #  define _PATH_SHELLS	"/etc/shells"
137065587Seric # endif
137165587Seric 
137265587Seric char	*DefaultUserShells[] =
137365587Seric {
137465587Seric 	"/bin/sh",
137565587Seric 	"/usr/bin/sh",
137665587Seric 	"/bin/csh",
137765587Seric 	"/usr/bin/csh",
137865587Seric #ifdef __hpux
137965587Seric 	"/bin/rsh",
138065587Seric 	"/bin/ksh",
138165587Seric 	"/bin/rksh",
138265587Seric 	"/bin/pam",
138365587Seric 	"/usr/bin/keysh",
138465587Seric 	"/bin/posix/sh",
138565211Seric #endif
138665587Seric 	NULL
138765587Seric };
138865587Seric 
138965208Seric #endif
139065208Seric 
139165747Seric #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
139265747Seric 
139365211Seric bool
139465211Seric usershellok(shell)
139565211Seric 	char *shell;
139665208Seric {
139765211Seric #if HASGETUSERSHELL
139865211Seric 	register char *p;
139965211Seric 	extern char *getusershell();
140065208Seric 
140165211Seric 	setusershell();
140265211Seric 	while ((p = getusershell()) != NULL)
140365747Seric 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
140465211Seric 			break;
140565211Seric 	endusershell();
140665211Seric 	return p != NULL;
140765211Seric #else
140865211Seric 	register FILE *shellf;
140965211Seric 	char buf[MAXLINE];
141065208Seric 
141165211Seric 	shellf = fopen(_PATH_SHELLS, "r");
141265211Seric 	if (shellf == NULL)
141365211Seric 	{
141465211Seric 		/* no /etc/shells; see if it is one of the std shells */
141565587Seric 		char **d;
141665587Seric 
141765587Seric 		for (d = DefaultUserShells; *d != NULL; d++)
141865587Seric 		{
141965587Seric 			if (strcmp(shell, *d) == 0)
142065587Seric 				return TRUE;
142165587Seric 		}
142265587Seric 		return FALSE;
142365211Seric 	}
142465208Seric 
142565211Seric 	while (fgets(buf, sizeof buf, shellf) != NULL)
142665211Seric 	{
142765211Seric 		register char *p, *q;
142865208Seric 
142965211Seric 		p = buf;
143065211Seric 		while (*p != '\0' && *p != '#' && *p != '/')
143165211Seric 			p++;
143265211Seric 		if (*p == '#' || *p == '\0')
143365208Seric 			continue;
143465211Seric 		q = p;
143565211Seric 		while (*p != '\0' && *p != '#' && !isspace(*p))
143665211Seric 			p++;
143765211Seric 		*p = '\0';
143865747Seric 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
143965211Seric 		{
144065211Seric 			fclose(shellf);
144165211Seric 			return TRUE;
144265211Seric 		}
144365208Seric 	}
144465211Seric 	fclose(shellf);
144565211Seric 	return FALSE;
144665211Seric #endif
144765208Seric }
144865208Seric /*
144964311Seric **  FREESPACE -- see how much free space is on the queue filesystem
145058082Seric **
145158082Seric **	Only implemented if you have statfs.
145258082Seric **
145358082Seric **	Parameters:
145464311Seric **		dir -- the directory in question.
145564311Seric **		bsize -- a variable into which the filesystem
145664311Seric **			block size is stored.
145758082Seric **
145858082Seric **	Returns:
145964311Seric **		The number of bytes free on the queue filesystem.
146064311Seric **		-1 if the statfs call fails.
146164311Seric **
146264311Seric **	Side effects:
146364311Seric **		Puts the filesystem block size into bsize.
146458082Seric */
146558082Seric 
146665749Seric /* statfs types */
146765749Seric #define SFS_NONE	0	/* no statfs implementation */
146865749Seric #define SFS_USTAT	1	/* use ustat */
146965749Seric #define SFS_4ARGS	2	/* use four-argument statfs call */
147065749Seric #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
147165749Seric #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
147265749Seric #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
147365749Seric 
147465749Seric #ifndef SFS_TYPE
147565749Seric # define SFS_TYPE	SFS_NONE
147658157Seric #endif
147758157Seric 
147865749Seric #if SFS_TYPE == SFS_USTAT
147958157Seric # include <ustat.h>
148058157Seric #endif
148165749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
148265749Seric # include <sys/statfs.h>
148358082Seric #endif
148465749Seric #if SFS_TYPE == SFS_VFS
148565749Seric # include <sys/vfs.h>
148665749Seric #endif
148765749Seric #if SFS_TYPE == SFS_MOUNT
148865749Seric # include <sys/mount.h>
148965749Seric #endif
149058082Seric 
149164311Seric long
149264311Seric freespace(dir, bsize)
149364311Seric 	char *dir;
149464311Seric 	long *bsize;
149558082Seric {
149665749Seric #if SFS_TYPE != SFS_NONE
149765749Seric # if SFS_TYPE == SFS_USTAT
149858153Seric 	struct ustat fs;
149958153Seric 	struct stat statbuf;
150058366Seric #  define FSBLOCKSIZE	DEV_BSIZE
150158157Seric #  define f_bavail	f_tfree
150258157Seric # else
150358157Seric #  if defined(ultrix)
150458157Seric 	struct fs_data fs;
150558157Seric #   define f_bavail	fd_bfreen
150658366Seric #   define FSBLOCKSIZE	fs.fd_bsize
150758153Seric #  else
150858082Seric 	struct statfs fs;
150958366Seric #   define FSBLOCKSIZE	fs.f_bsize
151065820Seric #   if defined(_SCO_unix_) || defined(IRIX) || defined(apollo)
151163987Seric #    define f_bavail f_bfree
151263987Seric #   endif
151358153Seric #  endif
151458133Seric # endif
151558082Seric 	extern int errno;
151658082Seric 
151765749Seric # if SFS_TYPE == SFS_USTAT
151864311Seric 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
151958157Seric # else
152065749Seric #  if SFS_TYPE == SFS_4ARGS
152164311Seric 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
152258157Seric #  else
152358157Seric #   if defined(ultrix)
152464311Seric 	if (statfs(dir, &fs) > 0)
152558153Seric #   else
152664311Seric 	if (statfs(dir, &fs) == 0)
152758153Seric #   endif
152858133Seric #  endif
152958133Seric # endif
153058133Seric 	{
153164311Seric 		if (bsize != NULL)
153264311Seric 			*bsize = FSBLOCKSIZE;
153364311Seric 		return (fs.f_bavail);
153464311Seric 	}
153564337Seric #endif
153664311Seric 	return (-1);
153764311Seric }
153864311Seric /*
153964311Seric **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
154064311Seric **
154164311Seric **	Only implemented if you have statfs.
154264311Seric **
154364311Seric **	Parameters:
154464311Seric **		msize -- the size to check against.  If zero, we don't yet
154564311Seric **		know how big the message will be, so just check for
154664311Seric **		a "reasonable" amount.
154764311Seric **
154864311Seric **	Returns:
154964311Seric **		TRUE if there is enough space.
155064311Seric **		FALSE otherwise.
155164311Seric */
155264311Seric 
155364311Seric bool
155464311Seric enoughspace(msize)
155564311Seric 	long msize;
155664311Seric {
155764311Seric 	long bfree, bsize;
155864311Seric 
155964311Seric 	if (MinBlocksFree <= 0 && msize <= 0)
156064311Seric 	{
156158133Seric 		if (tTd(4, 80))
156264311Seric 			printf("enoughspace: no threshold\n");
156364311Seric 		return TRUE;
156464311Seric 	}
156564311Seric 
156664311Seric 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
156764311Seric 	{
156864311Seric 		if (tTd(4, 80))
156958333Seric 			printf("enoughspace: bavail=%ld, need=%ld\n",
157064311Seric 				bfree, msize);
157158333Seric 
157258333Seric 		/* convert msize to block count */
157364311Seric 		msize = msize / bsize + 1;
157458333Seric 		if (MinBlocksFree >= 0)
157558333Seric 			msize += MinBlocksFree;
157658333Seric 
157764311Seric 		if (bfree < msize)
157858090Seric 		{
157958090Seric #ifdef LOG
158058090Seric 			if (LogLevel > 0)
158164254Seric 				syslog(LOG_ALERT,
158264254Seric 					"%s: low on space (have %ld, %s needs %ld in %s)",
158364311Seric 					CurEnv->e_id, bfree,
158464254Seric 					CurHostName, msize, QueueDir);
158558090Seric #endif
158658082Seric 			return FALSE;
158758090Seric 		}
158858082Seric 	}
158958082Seric 	else if (tTd(4, 80))
159058333Seric 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
159158333Seric 			MinBlocksFree, msize, errstring(errno));
159258082Seric 	return TRUE;
159358082Seric }
159458542Seric /*
159558542Seric **  TRANSIENTERROR -- tell if an error code indicates a transient failure
159658542Seric **
159758542Seric **	This looks at an errno value and tells if this is likely to
159858542Seric **	go away if retried later.
159958542Seric **
160058542Seric **	Parameters:
160158542Seric **		err -- the errno code to classify.
160258542Seric **
160358542Seric **	Returns:
160458542Seric **		TRUE if this is probably transient.
160558542Seric **		FALSE otherwise.
160658542Seric */
160758542Seric 
160858542Seric bool
160958542Seric transienterror(err)
161058542Seric 	int err;
161158542Seric {
161258542Seric 	switch (err)
161358542Seric 	{
161458542Seric 	  case EIO:			/* I/O error */
161558542Seric 	  case ENXIO:			/* Device not configured */
161658542Seric 	  case EAGAIN:			/* Resource temporarily unavailable */
161758542Seric 	  case ENOMEM:			/* Cannot allocate memory */
161858542Seric 	  case ENODEV:			/* Operation not supported by device */
161958542Seric 	  case ENFILE:			/* Too many open files in system */
162058542Seric 	  case EMFILE:			/* Too many open files */
162158542Seric 	  case ENOSPC:			/* No space left on device */
162258542Seric #ifdef ETIMEDOUT
162358542Seric 	  case ETIMEDOUT:		/* Connection timed out */
162458542Seric #endif
162558542Seric #ifdef ESTALE
162658542Seric 	  case ESTALE:			/* Stale NFS file handle */
162758542Seric #endif
162858542Seric #ifdef ENETDOWN
162958542Seric 	  case ENETDOWN:		/* Network is down */
163058542Seric #endif
163158542Seric #ifdef ENETUNREACH
163258542Seric 	  case ENETUNREACH:		/* Network is unreachable */
163358542Seric #endif
163458542Seric #ifdef ENETRESET
163558542Seric 	  case ENETRESET:		/* Network dropped connection on reset */
163658542Seric #endif
163758542Seric #ifdef ECONNABORTED
163858542Seric 	  case ECONNABORTED:		/* Software caused connection abort */
163958542Seric #endif
164058542Seric #ifdef ECONNRESET
164158542Seric 	  case ECONNRESET:		/* Connection reset by peer */
164258542Seric #endif
164358542Seric #ifdef ENOBUFS
164458542Seric 	  case ENOBUFS:			/* No buffer space available */
164558542Seric #endif
164658542Seric #ifdef ESHUTDOWN
164758542Seric 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
164858542Seric #endif
164958542Seric #ifdef ECONNREFUSED
165058542Seric 	  case ECONNREFUSED:		/* Connection refused */
165158542Seric #endif
165258542Seric #ifdef EHOSTDOWN
165358542Seric 	  case EHOSTDOWN:		/* Host is down */
165458542Seric #endif
165558542Seric #ifdef EHOSTUNREACH
165658542Seric 	  case EHOSTUNREACH:		/* No route to host */
165758542Seric #endif
165858542Seric #ifdef EDQUOT
165958542Seric 	  case EDQUOT:			/* Disc quota exceeded */
166058542Seric #endif
166158542Seric #ifdef EPROCLIM
166258542Seric 	  case EPROCLIM:		/* Too many processes */
166358542Seric #endif
166458542Seric #ifdef EUSERS
166558542Seric 	  case EUSERS:			/* Too many users */
166658542Seric #endif
166758542Seric #ifdef EDEADLK
166858542Seric 	  case EDEADLK:			/* Resource deadlock avoided */
166958542Seric #endif
167058542Seric #ifdef EISCONN
167158542Seric 	  case EISCONN:			/* Socket already connected */
167258542Seric #endif
167358542Seric #ifdef EINPROGRESS
167458542Seric 	  case EINPROGRESS:		/* Operation now in progress */
167558542Seric #endif
167658542Seric #ifdef EALREADY
167758542Seric 	  case EALREADY:		/* Operation already in progress */
167858542Seric #endif
167958542Seric #ifdef EADDRINUSE
168058542Seric 	  case EADDRINUSE:		/* Address already in use */
168158542Seric #endif
168258542Seric #ifdef EADDRNOTAVAIL
168358542Seric 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
168458542Seric #endif
168563834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
168658542Seric 	  case ENOSR:			/* Out of streams resources */
168758542Seric #endif
168858542Seric 		return TRUE;
168958542Seric 	}
169058542Seric 
169158542Seric 	/* nope, must be permanent */
169258542Seric 	return FALSE;
169358542Seric }
169458689Seric /*
169564035Seric **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
169658689Seric **
169758689Seric **	Parameters:
169858689Seric **		fd -- the file descriptor of the file.
169958689Seric **		filename -- the file name (for error messages).
170064335Seric **		ext -- the filename extension.
170158689Seric **		type -- type of the lock.  Bits can be:
170258689Seric **			LOCK_EX -- exclusive lock.
170358689Seric **			LOCK_NB -- non-blocking.
170458689Seric **
170558689Seric **	Returns:
170658689Seric **		TRUE if the lock was acquired.
170758689Seric **		FALSE otherwise.
170858689Seric */
170958689Seric 
171058689Seric bool
171164335Seric lockfile(fd, filename, ext, type)
171258689Seric 	int fd;
171358689Seric 	char *filename;
171464335Seric 	char *ext;
171558689Seric 	int type;
171658689Seric {
171765830Seric # if !HASFLOCK
171858689Seric 	int action;
171958689Seric 	struct flock lfd;
172064335Seric 
172164335Seric 	if (ext == NULL)
172264335Seric 		ext = "";
172364035Seric 
172463983Seric 	bzero(&lfd, sizeof lfd);
172559447Seric 	if (bitset(LOCK_UN, type))
172659447Seric 		lfd.l_type = F_UNLCK;
172759447Seric 	else if (bitset(LOCK_EX, type))
172858689Seric 		lfd.l_type = F_WRLCK;
172958689Seric 	else
173058689Seric 		lfd.l_type = F_RDLCK;
173158689Seric 
173258689Seric 	if (bitset(LOCK_NB, type))
173358689Seric 		action = F_SETLK;
173458689Seric 	else
173558689Seric 		action = F_SETLKW;
173658689Seric 
173764264Seric 	if (tTd(55, 60))
173864335Seric 		printf("lockfile(%s%s, action=%d, type=%d): ",
173964335Seric 			filename, ext, action, lfd.l_type);
174064264Seric 
174158689Seric 	if (fcntl(fd, action, &lfd) >= 0)
174264264Seric 	{
174364264Seric 		if (tTd(55, 60))
174464264Seric 			printf("SUCCESS\n");
174558689Seric 		return TRUE;
174664264Seric 	}
174758689Seric 
174864264Seric 	if (tTd(55, 60))
174964264Seric 		printf("(%s) ", errstring(errno));
175064264Seric 
175164136Seric 	/*
175264136Seric 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
175364136Seric 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
175464136Seric 	**  as type "tmp" (that is, served from swap space), the
175564136Seric 	**  previous fcntl will fail with "Invalid argument" errors.
175664136Seric 	**  Since this is fairly common during testing, we will assume
175764136Seric 	**  that this indicates that the lock is successfully grabbed.
175864136Seric 	*/
175964136Seric 
176064136Seric 	if (errno == EINVAL)
176164264Seric 	{
176264264Seric 		if (tTd(55, 60))
176364264Seric 			printf("SUCCESS\n");
176464136Seric 		return TRUE;
176564264Seric 	}
176664136Seric 
176758689Seric 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
176864378Seric 	{
176964378Seric 		int omode = -1;
177064378Seric #  ifdef F_GETFL
177164378Seric 		int oerrno = errno;
177264378Seric 
177364378Seric 		(void) fcntl(fd, F_GETFL, &omode);
177464378Seric 		errno = oerrno;
177564378Seric #  endif
177664378Seric 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
177764378Seric 			filename, ext, fd, type, omode, geteuid());
177864378Seric 	}
177958689Seric # else
178064335Seric 	if (ext == NULL)
178164335Seric 		ext = "";
178264335Seric 
178364264Seric 	if (tTd(55, 60))
178464335Seric 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
178564264Seric 
178658689Seric 	if (flock(fd, type) >= 0)
178764264Seric 	{
178864264Seric 		if (tTd(55, 60))
178964264Seric 			printf("SUCCESS\n");
179058689Seric 		return TRUE;
179164264Seric 	}
179258689Seric 
179364264Seric 	if (tTd(55, 60))
179464264Seric 		printf("(%s) ", errstring(errno));
179564264Seric 
179658689Seric 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
179764378Seric 	{
179864378Seric 		int omode = -1;
179964378Seric #  ifdef F_GETFL
180064378Seric 		int oerrno = errno;
180164378Seric 
180264378Seric 		(void) fcntl(fd, F_GETFL, &omode);
180364378Seric 		errno = oerrno;
180464378Seric #  endif
180564378Seric 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
180664378Seric 			filename, ext, fd, type, omode, geteuid());
180764386Seric 	}
180858689Seric # endif
180964264Seric 	if (tTd(55, 60))
181064264Seric 		printf("FAIL\n");
181158689Seric 	return FALSE;
181258689Seric }
181364556Seric /*
181465948Seric **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
181565948Seric **
181665948Seric **	Parameters:
181765948Seric **		fd -- the file descriptor to check.
181865948Seric **
181965948Seric **	Returns:
182065948Seric **		TRUE -- if only root can chown the file to an arbitrary
182165948Seric **			user.
182265948Seric **		FALSE -- if an arbitrary user can give away a file.
182365948Seric */
182465948Seric 
182565948Seric bool
182665948Seric chownsafe(fd)
182765948Seric 	int fd;
182865948Seric {
182965950Seric #ifdef __hpux
183065948Seric 	char *s;
183165948Seric 	int tfd;
183265948Seric 	uid_t o_uid, o_euid;
183365948Seric 	gid_t o_gid, o_egid;
183465948Seric 	bool rval;
183565948Seric 	struct stat stbuf;
183665948Seric 
183765948Seric 	o_uid = getuid();
183865948Seric 	o_euid = geteuid();
183965948Seric 	o_gid = getgid();
184065948Seric 	o_egid = getegid();
184165959Seric 	fstat(fd, &stbuf);
184265948Seric 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
184365948Seric 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
184465948Seric 	s = tmpnam(NULL);
184565948Seric 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
184665974Seric 	rval = fchown(tfd, DefUid, DefGid) != 0;
184765948Seric 	close(tfd);
184865948Seric 	unlink(s);
184965948Seric 	setreuid(o_uid, o_euid);
185065948Seric 	setresgid(o_gid, o_egid, -1);
185165948Seric 	return rval;
185265948Seric #else
185365948Seric # ifdef _PC_CHOWN_RESTRICTED
185465948Seric 	return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0;
185565948Seric # else
185665948Seric #  ifdef BSD
185765948Seric 	return TRUE;
185865948Seric #  else
185965948Seric 	return FALSE;
186065948Seric #  endif
186165948Seric # endif
186265948Seric #endif
186365948Seric }
186465948Seric /*
186564556Seric **  GETCFNAME -- return the name of the .cf file.
186664556Seric **
186764556Seric **	Some systems (e.g., NeXT) determine this dynamically.
186864556Seric */
186964556Seric 
187064556Seric char *
187164556Seric getcfname()
187264556Seric {
187364556Seric 	if (ConfFile != NULL)
187464556Seric 		return ConfFile;
187565214Seric #ifdef NETINFO
187665214Seric 	{
187765214Seric 		extern char *ni_propval();
187865214Seric 		char *cflocation;
187965214Seric 
188065214Seric 		cflocation = ni_propval("/locations/sendmail", "sendmail.cf");
188165214Seric 		if (cflocation != NULL)
188265214Seric 			return cflocation;
188365214Seric 	}
188465214Seric #endif
188564556Seric 	return _PATH_SENDMAILCF;
188664556Seric }
188764718Seric /*
188864718Seric **  SETVENDOR -- process vendor code from V configuration line
188964718Seric **
189064718Seric **	Parameters:
189164718Seric **		vendor -- string representation of vendor.
189264718Seric **
189364718Seric **	Returns:
189464718Seric **		TRUE -- if ok.
189564718Seric **		FALSE -- if vendor code could not be processed.
189664926Seric **
189764926Seric **	Side Effects:
189864926Seric **		It is reasonable to set mode flags here to tweak
189964926Seric **		processing in other parts of the code if necessary.
190064926Seric **		For example, if you are a vendor that uses $%y to
190164926Seric **		indicate YP lookups, you could enable that here.
190264718Seric */
190364718Seric 
190464718Seric bool
190564718Seric setvendor(vendor)
190664718Seric 	char *vendor;
190764718Seric {
190864926Seric 	if (strcasecmp(vendor, "Berkeley") == 0)
190964926Seric 		return TRUE;
191064926Seric 
191164926Seric 	/* add vendor extensions here */
191264926Seric 
191364926Seric 	return FALSE;
191464718Seric }
191564816Seric /*
191664816Seric **  STRTOL -- convert string to long integer
191764816Seric **
191864816Seric **	For systems that don't have it in the C library.
191964816Seric */
192064816Seric 
192164816Seric #ifdef NEEDSTRTOL
192264816Seric 
192364816Seric long
192464816Seric strtol(p, ep, b)
192564816Seric 	char *p;
192664816Seric 	char **ep;
192764816Seric 	int b;
192864816Seric {
192964816Seric 	long l = 0;
193064816Seric 	char c;
193164816Seric 	char maxd;
193264816Seric 	int neg = 1;
193364816Seric 
193464816Seric 	maxd = (b > 10) ? '9' : b + '0';
193564816Seric 
193664816Seric 	if (p && *p == '-') {
193764816Seric 		neg = -1;
193864816Seric 		p++;
193964816Seric 	}
194064816Seric 	while (p && (c = *p)) {
194164816Seric 		if (c >= '0' && c <= maxd) {
194264816Seric 			l = l*b + *p++ - '0';
194364816Seric 			continue;
194464816Seric 		}
194564816Seric 		if (c >= 'A' && c <= 'Z')
194664816Seric 			c -= 'A' + 'a';
194764816Seric 		c = c - 'a' + 10;
194864816Seric 		if (b > c) {
194964816Seric 			l = l*b + c;
195064816Seric 			p++;
195164816Seric 			continue;
195264816Seric 		}
195364816Seric 		break;
195464816Seric 	}
195564816Seric 	l *= neg;
195664816Seric 	if (ep)
195764816Seric 		*ep = p;
195864816Seric 	return l;
195964816Seric }
196064816Seric 
196164816Seric #endif
196264841Seric /*
196364841Seric **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
196464841Seric **
196564841Seric **	Solaris versions prior through 2.3 don't properly deliver a
196664841Seric **	canonical h_name field.  This tries to work around it.
196764841Seric */
196864841Seric 
196964841Seric #ifdef SOLARIS
197064841Seric 
197164841Seric struct hostent *
197264841Seric solaris_gethostbyname(name)
197365094Seric 	const char *name;
197464841Seric {
197564841Seric # ifdef SOLARIS_2_3
197664841Seric 	static struct hostent hp;
197764841Seric 	static char buf[1000];
197864841Seric 	extern struct hostent *_switch_gethostbyname_r();
197964841Seric 
198064841Seric 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
198164841Seric # else
198265009Seric 	extern struct hostent *__switch_gethostbyname();
198365009Seric 
198464841Seric 	return __switch_gethostbyname(name);
198564841Seric # endif
198664841Seric }
198764841Seric 
198864841Seric struct hostent *
198964841Seric solaris_gethostbyaddr(addr, len, type)
199065094Seric 	const char *addr;
199164841Seric 	int len;
199264841Seric 	int type;
199364841Seric {
199464841Seric # ifdef SOLARIS_2_3
199564841Seric 	static struct hostent hp;
199664841Seric 	static char buf[1000];
199764841Seric 	extern struct hostent *_switch_gethostbyaddr_r();
199864841Seric 
199964841Seric 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
200064841Seric # else
200165009Seric 	extern struct hostent *__switch_gethostbyaddr();
200265009Seric 
200364841Seric 	return __switch_gethostbyaddr(addr, len, type);
200464841Seric # endif
200564841Seric }
200664841Seric 
200764841Seric #endif
200865214Seric /*
200965214Seric **  NI_PROPVAL -- netinfo property value lookup routine
201065214Seric **
201165214Seric **	Parameters:
201265214Seric **		directory -- the Netinfo directory name.
201365214Seric **		propname -- the Netinfo property name.
201465214Seric **
201565214Seric **	Returns:
201665214Seric **		NULL -- if:
201765214Seric **			1. the directory is not found
201865214Seric **			2. the property name is not found
201965214Seric **			3. the property contains multiple values
202065214Seric **			4. some error occured
202165214Seric **		else -- the location of the config file.
202265214Seric **
202365214Seric **	Notes:
202465214Seric **      	Caller should free the return value of ni_proval
202565214Seric */
202665214Seric 
202765214Seric #ifdef NETINFO
202865214Seric 
202965214Seric # include <netinfo/ni.h>
203065214Seric 
203165214Seric # define LOCAL_NETINFO_DOMAIN    "."
203265214Seric # define PARENT_NETINFO_DOMAIN   ".."
203365214Seric # define MAX_NI_LEVELS           256
203465214Seric 
203565214Seric char *
203665214Seric ni_propval(directory, propname)
203765214Seric 	char *directory;
203865214Seric 	char *propname;
203965214Seric {
204065820Seric 	char *propval = NULL;
204165214Seric 	int i;
204265214Seric 	void *ni = NULL;
204365214Seric 	void *lastni = NULL;
204465214Seric 	ni_status nis;
204565214Seric 	ni_id nid;
204665214Seric 	ni_namelist ninl;
204765214Seric 
204865214Seric 	/*
204965214Seric 	**  If the passed directory and property name are found
205065214Seric 	**  in one of netinfo domains we need to search (starting
205165214Seric 	**  from the local domain moving all the way back to the
205265214Seric 	**  root domain) set propval to the property's value
205365214Seric 	**  and return it.
205465214Seric 	*/
205565214Seric 
205665214Seric 	for (i = 0; i < MAX_NI_LEVELS; ++i)
205765214Seric 	{
205865214Seric 		if (i == 0)
205965214Seric 		{
206065214Seric 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
206165214Seric 		}
206265214Seric 		else
206365214Seric 		{
206465214Seric 			if (lastni != NULL)
206565214Seric 				ni_free(lastni);
206665214Seric 			lastni = ni;
206765214Seric 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
206865214Seric 		}
206965214Seric 
207065214Seric 		/*
207165214Seric 		**  Don't bother if we didn't get a handle on a
207265214Seric 		**  proper domain.  This is not necessarily an error.
207365214Seric 		**  We would get a positive ni_status if, for instance
207465214Seric 		**  we never found the directory or property and tried
207565214Seric 		**  to open the parent of the root domain!
207665214Seric 		*/
207765214Seric 
207865214Seric 		if (nis != 0)
207965214Seric 			break;
208065214Seric 
208165214Seric 		/*
208265214Seric 		**  Find the path to the server information.
208365214Seric 		*/
208465214Seric 
208565214Seric 		if (ni_pathsearch(ni, &nid, directory) != 0)
208665214Seric 			continue;
208765214Seric 
208865214Seric 		/*
208965214Seric 		**  Find "host" information.
209065214Seric 		*/
209165214Seric 
209265214Seric 		if (ni_lookupprop(ni, &nid, propname, &ninl) != 0)
209365214Seric 			continue;
209465214Seric 
209565214Seric 		/*
209665214Seric 		**  If there's only one name in
209765214Seric 		**  the list, assume we've got
209865214Seric 		**  what we want.
209965214Seric 		*/
210065214Seric 
210165214Seric 		if (ninl.ni_namelist_len == 1)
210265214Seric 		{
210365214Seric 			propval = ni_name_dup(ninl.ni_namelist_val[0]);
210465214Seric 			break;
210565214Seric 		}
210665214Seric 	}
210765214Seric 
210865214Seric 	/*
210965214Seric 	**  Clean up.
211065214Seric 	*/
211165214Seric 
211265214Seric 	if (ni != NULL)
211365214Seric 		ni_free(ni);
211465214Seric 	if (lastni != NULL && ni != lastni)
211565214Seric 		ni_free(lastni);
211665214Seric 
211765214Seric 	return propval;
211865214Seric }
211965214Seric 
212065214Seric #endif /* NETINFO */
212165504Seric /*
212265504Seric **  HARD_SYSLOG -- call syslog repeatedly until it works
212365504Seric **
212465504Seric **	Needed on HP-UX, which apparently doesn't guarantee that
212565504Seric **	syslog succeeds during interrupt handlers.
212665504Seric */
212765504Seric 
212865504Seric #ifdef __hpux
212965504Seric 
213065504Seric # define MAXSYSLOGTRIES	100
213165504Seric # undef syslog
213265504Seric 
213365504Seric # ifdef __STDC__
213465504Seric hard_syslog(int pri, char *msg, ...)
213565504Seric # else
213665504Seric hard_syslog(pri, msg, va_alist)
213765504Seric 	int pri;
213865504Seric 	char *msg;
213965504Seric 	va_dcl
214065504Seric # endif
214165504Seric {
214265504Seric 	int i;
214365504Seric 	char buf[SYSLOG_BUFSIZE * 2];
214465504Seric 	VA_LOCAL_DECL;
214565504Seric 
214665504Seric 	VA_START(msg);
214765504Seric 	vsprintf(buf, msg, ap);
214865504Seric 	VA_END;
214965504Seric 
215065504Seric 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
215165504Seric 		continue;
215265504Seric }
215365504Seric 
215465504Seric #endif
2155