xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 51305)
122698Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822698Sdist 
922698Sdist #ifndef lint
10*51305Seric static char sccsid[] = "@(#)conf.c	5.29 (Berkeley) 10/03/91";
1133728Sbostic #endif /* not lint */
1222698Sdist 
1314881Seric # include <sys/ioctl.h>
1424943Seric # include <sys/param.h>
1536928Sbostic # include <pwd.h>
163309Seric # include "sendmail.h"
1740980Sbostic # include "pathnames.h"
18404Seric 
19294Seric /*
203309Seric **  CONF.C -- Sendmail Configuration Tables.
21294Seric **
22294Seric **	Defines the configuration of this installation.
23294Seric **
241388Seric **	Compilation Flags:
2514872Seric **		VMUNIX -- running on a Berkeley UNIX system.
26294Seric **
271388Seric **	Configuration Variables:
282897Seric **		HdrInfo -- a table describing well-known header fields.
292897Seric **			Each entry has the field name and some flags,
304147Seric **			which are described in sendmail.h.
314093Seric **
324093Seric **	Notes:
334093Seric **		I have tried to put almost all the reasonable
344093Seric **		configuration information into the configuration
354093Seric **		file read at runtime.  My intent is that anything
364093Seric **		here is a function of the version of UNIX you
374093Seric **		are running, or is really static -- for example
384093Seric **		the headers are a superset of widely used
394093Seric **		protocols.  If you find yourself playing with
404093Seric **		this file too much, you may be making a mistake!
41294Seric */
42294Seric 
43294Seric 
44294Seric 
45294Seric 
464437Seric /*
472897Seric **  Header info table
483057Seric **	Final (null) entry contains the flags used for any other field.
494147Seric **
504147Seric **	Not all of these are actually handled specially by sendmail
514147Seric **	at this time.  They are included as placeholders, to let
524147Seric **	you know that "someday" I intend to have sendmail do
534147Seric **	something with them.
542897Seric */
552897Seric 
562897Seric struct hdrinfo	HdrInfo[] =
572897Seric {
588060Seric 		/* originator fields, most to least significant  */
5911417Seric 	"resent-sender",	H_FROM|H_RESENT,
6011417Seric 	"resent-from",		H_FROM|H_RESENT,
6125686Seric 	"resent-reply-to",	H_FROM|H_RESENT,
629055Seric 	"sender",		H_FROM,
639055Seric 	"from",			H_FROM,
6425686Seric 	"reply-to",		H_FROM,
659055Seric 	"full-name",		H_ACHECK,
669055Seric 	"return-receipt-to",	H_FROM,
679055Seric 	"errors-to",		H_FROM,
688060Seric 		/* destination fields */
699055Seric 	"to",			H_RCPT,
7011417Seric 	"resent-to",		H_RCPT|H_RESENT,
719055Seric 	"cc",			H_RCPT,
7211417Seric 	"resent-cc",		H_RCPT|H_RESENT,
739055Seric 	"bcc",			H_RCPT|H_ACHECK,
7411417Seric 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
758060Seric 		/* message identification and control */
7611417Seric 	"message-id",		0,
7711417Seric 	"resent-message-id",	H_RESENT,
789055Seric 	"message",		H_EOH,
799055Seric 	"text",			H_EOH,
8011417Seric 		/* date fields */
8111417Seric 	"date",			0,
8211417Seric 	"resent-date",		H_RESENT,
838060Seric 		/* trace fields */
849055Seric 	"received",		H_TRACE|H_FORCE,
859055Seric 	"via",			H_TRACE|H_FORCE,
869055Seric 	"mail-from",		H_TRACE|H_FORCE,
878060Seric 
889055Seric 	NULL,			0,
892897Seric };
904166Seric 
914166Seric 
924166Seric /*
934166Seric **  ARPANET error message numbers.
944166Seric */
954166Seric 
967956Seric char	Arpa_Info[] =		"050";	/* arbitrary info */
977956Seric char	Arpa_TSyserr[] =	"451";	/* some (transient) system error */
987956Seric char	Arpa_PSyserr[] =	"554";	/* some (permanent) system error */
997956Seric char	Arpa_Usrerr[] =		"554";	/* some (fatal) user error */
1004282Seric 
1014282Seric 
1024282Seric 
1034282Seric /*
1044282Seric **  Location of system files/databases/etc.
1054282Seric */
1064282Seric 
10740980Sbostic char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
10840980Sbostic char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
1099039Seric 
1109064Seric 
1119064Seric 
1129039Seric /*
11324943Seric **  Miscellaneous stuff.
1149039Seric */
1159039Seric 
11624943Seric int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
11740938Srick extern int la;				/* load average */
11824943Seric /*
11924943Seric **  SETDEFAULTS -- set default values
12024943Seric **
12124943Seric **	Because of the way freezing is done, these must be initialized
12224943Seric **	using direct code.
12324943Seric **
12424943Seric **	Parameters:
12524943Seric **		none.
12624943Seric **
12724943Seric **	Returns:
12824943Seric **		none.
12924943Seric **
13024943Seric **	Side Effects:
13124943Seric **		Initializes a bunch of global variables to their
13224943Seric **		default values.
13324943Seric */
13424943Seric 
13524943Seric setdefaults()
13624943Seric {
13724943Seric 	QueueLA = 8;
13824943Seric 	QueueFactor = 10000;
13924943Seric 	RefuseLA = 12;
14024943Seric 	SpaceSub = ' ';
14124981Seric 	WkRecipFact = 1000;
14224981Seric 	WkClassFact = 1800;
14325812Seric 	WkTimeFact = 9000;
14424981Seric 	FileMode = 0644;
14524981Seric 	DefUid = 1;
14624981Seric 	DefGid = 1;
14747284Seric 	CheckpointInterval = 10;
148*51305Seric 	MaxHopCount = MAXHOP;
14940973Sbostic 	setdefuser();
15024943Seric }
151294Seric 
15240973Sbostic 
1534326Seric /*
15440973Sbostic **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
15540973Sbostic */
15640973Sbostic 
15740973Sbostic setdefuser()
15840973Sbostic {
15940973Sbostic 	struct passwd *defpwent;
16040973Sbostic 
16140973Sbostic 	if (DefUser != NULL)
16240973Sbostic 		free(DefUser);
16340973Sbostic 	if ((defpwent = getpwuid(DefUid)) != NULL)
16440973Sbostic 		DefUser = newstr(defpwent->pw_name);
16540973Sbostic 	else
16640973Sbostic 		DefUser = newstr("nobody");
16740973Sbostic }
16840973Sbostic 
16940973Sbostic 
17040973Sbostic /*
1714326Seric **  GETRUID -- get real user id (V7)
1724326Seric */
1734326Seric 
1744326Seric getruid()
1754326Seric {
1769274Seric 	if (OpMode == MD_DAEMON)
1774536Seric 		return (RealUid);
1784536Seric 	else
1794536Seric 		return (getuid());
1804326Seric }
1814326Seric 
1824326Seric 
1834326Seric /*
1844326Seric **  GETRGID -- get real group id (V7).
1854326Seric */
1864326Seric 
1874326Seric getrgid()
1884326Seric {
1899274Seric 	if (OpMode == MD_DAEMON)
1904536Seric 		return (RealGid);
1914536Seric 	else
1924536Seric 		return (getgid());
1934326Seric }
1944326Seric 
19533936Sbostic /*
1969369Seric **  USERNAME -- return the user id of the logged in user.
1979369Seric **
1989369Seric **	Parameters:
1999369Seric **		none.
2009369Seric **
2019369Seric **	Returns:
2029369Seric **		The login name of the logged in user.
2039369Seric **
2049369Seric **	Side Effects:
2059369Seric **		none.
2069369Seric **
2079369Seric **	Notes:
2089369Seric **		The return value is statically allocated.
2099369Seric */
2109369Seric 
2119369Seric char *
2129369Seric username()
2139369Seric {
21417469Seric 	static char *myname = NULL;
2159369Seric 	extern char *getlogin();
21619904Smiriam 	register struct passwd *pw;
2179369Seric 
21817469Seric 	/* cache the result */
21917469Seric 	if (myname == NULL)
22017469Seric 	{
22117469Seric 		myname = getlogin();
22217469Seric 		if (myname == NULL || myname[0] == '\0')
22317469Seric 		{
22417469Seric 
22517469Seric 			pw = getpwuid(getruid());
22617469Seric 			if (pw != NULL)
22740993Sbostic 				myname = newstr(pw->pw_name);
22817469Seric 		}
22919904Smiriam 		else
23019904Smiriam 		{
23119873Smiriam 
23240993Sbostic 			myname = newstr(myname);
23340993Sbostic 			if ((pw = getpwnam(myname)) == NULL ||
23440993Sbostic 			      getuid() != pw->pw_uid)
23519904Smiriam 			{
23619873Smiriam 				pw = getpwuid(getuid());
23724945Seric 				if (pw != NULL)
23840993Sbostic 					myname = newstr(pw->pw_name);
23919873Smiriam 			}
24019873Smiriam 		}
24117469Seric 		if (myname == NULL || myname[0] == '\0')
24217469Seric 		{
24317469Seric 			syserr("Who are you?");
24417469Seric 			myname = "postmaster";
24517469Seric 		}
24617469Seric 	}
24717469Seric 
24817469Seric 	return (myname);
2499369Seric }
2509369Seric /*
2514190Seric **  TTYPATH -- Get the path of the user's tty
252294Seric **
253294Seric **	Returns the pathname of the user's tty.  Returns NULL if
254294Seric **	the user is not logged in or if s/he has write permission
255294Seric **	denied.
256294Seric **
257294Seric **	Parameters:
258294Seric **		none
259294Seric **
260294Seric **	Returns:
261294Seric **		pathname of the user's tty.
262294Seric **		NULL if not logged in or write permission denied.
263294Seric **
264294Seric **	Side Effects:
265294Seric **		none.
266294Seric **
267294Seric **	WARNING:
268294Seric **		Return value is in a local buffer.
269294Seric **
270294Seric **	Called By:
271294Seric **		savemail
272294Seric */
273294Seric 
274294Seric # include <sys/stat.h>
275294Seric 
276294Seric char *
277294Seric ttypath()
278294Seric {
279294Seric 	struct stat stbuf;
280294Seric 	register char *pathn;
281294Seric 	extern char *ttyname();
2824081Seric 	extern char *getlogin();
283294Seric 
284294Seric 	/* compute the pathname of the controlling tty */
2859369Seric 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
2869369Seric 	    (pathn = ttyname(0)) == NULL)
287294Seric 	{
288294Seric 		errno = 0;
289294Seric 		return (NULL);
290294Seric 	}
291294Seric 
292294Seric 	/* see if we have write permission */
2932967Seric 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
294294Seric 	{
295294Seric 		errno = 0;
296294Seric 		return (NULL);
297294Seric 	}
298294Seric 
299294Seric 	/* see if the user is logged in */
300294Seric 	if (getlogin() == NULL)
301294Seric 		return (NULL);
302294Seric 
303294Seric 	/* looks good */
304294Seric 	return (pathn);
305294Seric }
3062967Seric /*
3072967Seric **  CHECKCOMPAT -- check for From and To person compatible.
3082967Seric **
3092967Seric **	This routine can be supplied on a per-installation basis
3102967Seric **	to determine whether a person is allowed to send a message.
3112967Seric **	This allows restriction of certain types of internet
3122967Seric **	forwarding or registration of users.
3132967Seric **
3142967Seric **	If the hosts are found to be incompatible, an error
3152967Seric **	message should be given using "usrerr" and FALSE should
3162967Seric **	be returned.
3172967Seric **
3184288Seric **	'NoReturn' can be set to suppress the return-to-sender
3194288Seric **	function; this should be done on huge messages.
3204288Seric **
3212967Seric **	Parameters:
3222967Seric **		to -- the person being sent to.
3232967Seric **
3242967Seric **	Returns:
3252967Seric **		TRUE -- ok to send.
3262967Seric **		FALSE -- not ok.
3272967Seric **
3282967Seric **	Side Effects:
3292967Seric **		none (unless you include the usrerr stuff)
3302967Seric */
3312967Seric 
3322967Seric bool
3332967Seric checkcompat(to)
3342967Seric 	register ADDRESS *to;
3352967Seric {
33612133Seric # ifdef lint
33712133Seric 	if (to == NULL)
33812133Seric 		to++;
33912133Seric # endif lint
34010698Seric # ifdef EXAMPLE_CODE
34110698Seric 	/* this code is intended as an example only */
3424437Seric 	register STAB *s;
3434437Seric 
3444437Seric 	s = stab("arpa", ST_MAILER, ST_FIND);
3459369Seric 	if (s != NULL && CurEnv->e_from.q_mailer != LocalMailer &&
3469369Seric 	    to->q_mailer == s->s_mailer)
3474437Seric 	{
3484437Seric 		usrerr("No ARPA mail through this machine: see your system administration");
34910698Seric 		/* NoReturn = TRUE; to supress return copy */
3504437Seric 		return (FALSE);
3514437Seric 	}
35210698Seric # endif EXAMPLE_CODE
3532967Seric 	return (TRUE);
3542967Seric }
3559369Seric /*
3569369Seric **  HOLDSIGS -- arrange to hold all signals
3579369Seric **
3589369Seric **	Parameters:
3599369Seric **		none.
3609369Seric **
3619369Seric **	Returns:
3629369Seric **		none.
3639369Seric **
3649369Seric **	Side Effects:
3659369Seric **		Arranges that signals are held.
3669369Seric */
3679369Seric 
3689369Seric holdsigs()
3699369Seric {
3709369Seric }
3719369Seric /*
3729369Seric **  RLSESIGS -- arrange to release all signals
3739369Seric **
3749369Seric **	This undoes the effect of holdsigs.
3759369Seric **
3769369Seric **	Parameters:
3779369Seric **		none.
3789369Seric **
3799369Seric **	Returns:
3809369Seric **		none.
3819369Seric **
3829369Seric **	Side Effects:
3839369Seric **		Arranges that signals are released.
3849369Seric */
3859369Seric 
3869369Seric rlsesigs()
3879369Seric {
3889369Seric }
38914872Seric /*
39014872Seric **  GETLA -- get the current load average
39114872Seric **
39214881Seric **	This code stolen from la.c.
39314881Seric **
39414872Seric **	Parameters:
39514872Seric **		none.
39614872Seric **
39714872Seric **	Returns:
39814872Seric **		The current load average as an integer.
39914872Seric **
40014872Seric **	Side Effects:
40114872Seric **		none.
40214872Seric */
40314872Seric 
40438196Smckusick #ifndef sun
40514872Seric 
40638196Smckusick getla()
40738196Smckusick {
40838196Smckusick 	double avenrun[3];
40938196Smckusick 
41038196Smckusick 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
41138196Smckusick 		return (0);
41238196Smckusick 	return ((int) (avenrun[0] + 0.5));
41338196Smckusick }
41438196Smckusick 
41538196Smckusick #else /* sun */
41638196Smckusick 
41714872Seric #include <nlist.h>
41814872Seric 
41914872Seric struct	nlist Nl[] =
42014872Seric {
42114872Seric 	{ "_avenrun" },
42214872Seric #define	X_AVENRUN	0
42314872Seric 	{ 0 },
42414872Seric };
42514872Seric 
42640930Srick 
42740930Srick extern int la;
42840930Srick 
42914872Seric getla()
43014872Seric {
43114872Seric 	static int kmem = -1;
43224943Seric 	long avenrun[3];
43325615Seric 	extern off_t lseek();
43414872Seric 
43514872Seric 	if (kmem < 0)
43614872Seric 	{
43724945Seric 		kmem = open("/dev/kmem", 0, 0);
43814872Seric 		if (kmem < 0)
43914872Seric 			return (-1);
44023118Seric 		(void) ioctl(kmem, (int) FIOCLEX, (char *) 0);
44114872Seric 		nlist("/vmunix", Nl);
44214872Seric 		if (Nl[0].n_type == 0)
44314872Seric 			return (-1);
44414872Seric 	}
44524945Seric 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
44623118Seric 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
44719967Seric 	{
44819967Seric 		/* thank you Ian */
44919967Seric 		return (-1);
45019967Seric 	}
45124943Seric 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
45214872Seric }
45314872Seric 
45438196Smckusick #endif /* sun */
45524943Seric /*
45624943Seric **  SHOULDQUEUE -- should this message be queued or sent?
45724943Seric **
45824943Seric **	Compares the message cost to the load average to decide.
45924943Seric **
46024943Seric **	Parameters:
46124943Seric **		pri -- the priority of the message in question.
46224943Seric **
46324943Seric **	Returns:
46424943Seric **		TRUE -- if this message should be queued up for the
46524943Seric **			time being.
46624943Seric **		FALSE -- if the load is low enough to send this message.
46724943Seric **
46824943Seric **	Side Effects:
46924943Seric **		none.
47024943Seric */
47124943Seric 
47224943Seric bool
47324943Seric shouldqueue(pri)
47424943Seric 	long pri;
47524943Seric {
47624943Seric 	if (la < QueueLA)
47724943Seric 		return (FALSE);
47824943Seric 	return (pri > (QueueFactor / (la - QueueLA + 1)));
47924943Seric }
48024943Seric /*
48124943Seric **  SETPROCTITLE -- set process title for ps
48224943Seric **
48324943Seric **	Parameters:
48424943Seric **		fmt -- a printf style format string.
48524943Seric **		a, b, c -- possible parameters to fmt.
48624943Seric **
48724943Seric **	Returns:
48824943Seric **		none.
48924943Seric **
49024943Seric **	Side Effects:
49124943Seric **		Clobbers argv of our main procedure so ps(1) will
49224943Seric **		display the title.
49324943Seric */
49424943Seric 
49524943Seric /*VARARGS1*/
49624943Seric setproctitle(fmt, a, b, c)
49724943Seric 	char *fmt;
49824943Seric {
49924943Seric # ifdef SETPROCTITLE
50024943Seric 	register char *p;
50125049Seric 	register int i;
50224943Seric 	extern char **Argv;
50324943Seric 	extern char *LastArgv;
50425049Seric 	char buf[MAXLINE];
50524943Seric 
50625049Seric 	(void) sprintf(buf, fmt, a, b, c);
50724943Seric 
50824943Seric 	/* make ps print "(sendmail)" */
50925049Seric 	p = Argv[0];
51024943Seric 	*p++ = '-';
51124943Seric 
51225049Seric 	i = strlen(buf);
51325049Seric 	if (i > LastArgv - p - 2)
51425049Seric 	{
51525049Seric 		i = LastArgv - p - 2;
51625049Seric 		buf[i] = '\0';
51725049Seric 	}
51825049Seric 	(void) strcpy(p, buf);
51925049Seric 	p += i;
52024943Seric 	while (p < LastArgv)
52124943Seric 		*p++ = ' ';
52224943Seric # endif SETPROCTITLE
52324943Seric }
52425698Seric /*
52525698Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
52625698Seric **
52725698Seric **	Parameters:
52825698Seric **		none.
52925698Seric **
53025698Seric **	Returns:
53125698Seric **		none.
53225698Seric **
53325698Seric **	Side Effects:
53425698Seric **		Picks up extant zombies.
53525698Seric */
53625698Seric 
53725698Seric # ifdef VMUNIX
53825698Seric # include <sys/wait.h>
53925698Seric # endif VMUNIX
54025698Seric 
54146928Sbostic void
54225698Seric reapchild()
54325698Seric {
54425698Seric # ifdef WNOHANG
54525698Seric 	union wait status;
54625698Seric 
54746928Sbostic 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
54825698Seric 		continue;
54925698Seric # else WNOHANG
55025698Seric 	auto int status;
55125698Seric 
55246928Sbostic 	while (wait((int *)&status) > 0)
55325698Seric 		continue;
55425698Seric # endif WNOHANG
55525698Seric }
556