xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision ab6c2785)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)conf.c	6.60 (Berkeley) 05/22/93";
11 #endif /* not lint */
12 
13 # include <sys/ioctl.h>
14 # include <sys/param.h>
15 # include <signal.h>
16 # include <pwd.h>
17 # include "sendmail.h"
18 # include "pathnames.h"
19 
20 /*
21 **  CONF.C -- Sendmail Configuration Tables.
22 **
23 **	Defines the configuration of this installation.
24 **
25 **	Configuration Variables:
26 **		HdrInfo -- a table describing well-known header fields.
27 **			Each entry has the field name and some flags,
28 **			which are described in sendmail.h.
29 **
30 **	Notes:
31 **		I have tried to put almost all the reasonable
32 **		configuration information into the configuration
33 **		file read at runtime.  My intent is that anything
34 **		here is a function of the version of UNIX you
35 **		are running, or is really static -- for example
36 **		the headers are a superset of widely used
37 **		protocols.  If you find yourself playing with
38 **		this file too much, you may be making a mistake!
39 */
40 
41 
42 
43 
44 /*
45 **  Header info table
46 **	Final (null) entry contains the flags used for any other field.
47 **
48 **	Not all of these are actually handled specially by sendmail
49 **	at this time.  They are included as placeholders, to let
50 **	you know that "someday" I intend to have sendmail do
51 **	something with them.
52 */
53 
54 struct hdrinfo	HdrInfo[] =
55 {
56 		/* originator fields, most to least significant  */
57 	"resent-sender",	H_FROM|H_RESENT,
58 	"resent-from",		H_FROM|H_RESENT,
59 	"resent-reply-to",	H_FROM|H_RESENT,
60 	"sender",		H_FROM,
61 	"from",			H_FROM,
62 	"reply-to",		H_FROM,
63 	"full-name",		H_ACHECK,
64 	"return-receipt-to",	H_FROM /* |H_RECEIPTTO */,
65 	"errors-to",		H_FROM|H_ERRORSTO,
66 
67 		/* destination fields */
68 	"to",			H_RCPT,
69 	"resent-to",		H_RCPT|H_RESENT,
70 	"cc",			H_RCPT,
71 	"resent-cc",		H_RCPT|H_RESENT,
72 	"bcc",			H_RCPT|H_ACHECK,
73 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
74 	"apparently-to",	H_RCPT,
75 
76 		/* message identification and control */
77 	"message-id",		0,
78 	"resent-message-id",	H_RESENT,
79 	"message",		H_EOH,
80 	"text",			H_EOH,
81 
82 		/* date fields */
83 	"date",			0,
84 	"resent-date",		H_RESENT,
85 
86 		/* trace fields */
87 	"received",		H_TRACE|H_FORCE,
88 	"x400-received",	H_TRACE|H_FORCE,
89 	"via",			H_TRACE|H_FORCE,
90 	"mail-from",		H_TRACE|H_FORCE,
91 
92 		/* miscellaneous fields */
93 	"comments",		H_FORCE,
94 	"return-path",		H_ACHECK,
95 
96 	NULL,			0,
97 };
98 
99 
100 
101 /*
102 **  Location of system files/databases/etc.
103 */
104 
105 char	*ConfFile =	_PATH_SENDMAILCF;	/* runtime configuration */
106 char	*FreezeFile =	_PATH_SENDMAILFC;	/* frozen version of above */
107 char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
108 
109 
110 
111 /*
112 **  Privacy values
113 */
114 
115 struct prival PrivacyValues[] =
116 {
117 	"public",		PRIV_PUBLIC,
118 	"needmailhelo",		PRIV_NEEDMAILHELO,
119 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
120 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
121 	"noexpn",		PRIV_NOEXPN,
122 	"novrfy",		PRIV_NOVRFY,
123 	"restrictmailq",	PRIV_RESTRMAILQ,
124 	"authwarnings",		PRIV_AUTHWARNINGS,
125 	"goaway",		PRIV_GOAWAY,
126 	NULL,			0,
127 };
128 
129 
130 
131 /*
132 **  Miscellaneous stuff.
133 */
134 
135 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
136 /*
137 **  SETDEFAULTS -- set default values
138 **
139 **	Because of the way freezing is done, these must be initialized
140 **	using direct code.
141 **
142 **	Parameters:
143 **		e -- the default envelope.
144 **
145 **	Returns:
146 **		none.
147 **
148 **	Side Effects:
149 **		Initializes a bunch of global variables to their
150 **		default values.
151 */
152 
153 #define DAYS		* 24 * 60 * 60
154 
155 setdefaults(e)
156 	register ENVELOPE *e;
157 {
158 	SpaceSub = ' ';				/* option B */
159 	QueueLA = 8;				/* option x */
160 	RefuseLA = 12;				/* option X */
161 	WkRecipFact = 30000L;			/* option y */
162 	WkClassFact = 1800L;			/* option z */
163 	WkTimeFact = 90000L;			/* option Z */
164 	QueueFactor = WkRecipFact * 20;		/* option q */
165 	FileMode = (getuid() != geteuid()) ? 0644 : 0600;
166 						/* option F */
167 	DefUid = 1;				/* option u */
168 	DefGid = 1;				/* option g */
169 	CheckpointInterval = 10;		/* option C */
170 	MaxHopCount = 25;			/* option h */
171 	e->e_sendmode = SM_FORK;		/* option d */
172 	e->e_errormode = EM_PRINT;		/* option e */
173 	SevenBit = FALSE;			/* option 7 */
174 	MaxMciCache = 1;			/* option k */
175 	MciCacheTimeout = 300;			/* option K */
176 	LogLevel = 9;				/* option L */
177 	settimeouts(NULL);			/* option r */
178 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
179 	TimeOuts.to_q_warning = 0;		/* option T */
180 	PrivacyFlags = 0;			/* option p */
181 	setdefuser();
182 	setupmaps();
183 	setupmailers();
184 }
185 
186 
187 /*
188 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
189 */
190 
191 setdefuser()
192 {
193 	struct passwd *defpwent;
194 	static char defuserbuf[40];
195 
196 	DefUser = defuserbuf;
197 	if ((defpwent = getpwuid(DefUid)) != NULL)
198 		strcpy(defuserbuf, defpwent->pw_name);
199 	else
200 		strcpy(defuserbuf, "nobody");
201 }
202 /*
203 **  HOST_MAP_INIT -- initialize host class structures
204 */
205 
206 bool
207 host_map_init(map, args)
208 	MAP *map;
209 	char *args;
210 {
211 	register char *p = args;
212 
213 	for (;;)
214 	{
215 		while (isascii(*p) && isspace(*p))
216 			p++;
217 		if (*p != '-')
218 			break;
219 		switch (*++p)
220 		{
221 		  case 'a':
222 			map->map_app = ++p;
223 			break;
224 		}
225 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
226 			p++;
227 		if (*p != '\0')
228 			*p++ = '\0';
229 	}
230 	if (map->map_app != NULL)
231 		map->map_app = newstr(map->map_app);
232 	return TRUE;
233 }
234 /*
235 **  SETUPMAILERS -- initialize default mailers
236 */
237 
238 setupmailers()
239 {
240 	char buf[100];
241 
242 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
243 	makemailer(buf);
244 
245 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
246 	makemailer(buf);
247 
248 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
249 	makemailer(buf);
250 }
251 /*
252 **  SETUPMAPS -- set up map classes
253 */
254 
255 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
256 	{ \
257 		extern bool parse __P((MAP *, char *)); \
258 		extern bool open __P((MAP *, int)); \
259 		extern void close __P((MAP *)); \
260 		extern char *lookup __P((MAP *, char *, char **, int *)); \
261 		extern void store __P((MAP *, char *, char *)); \
262 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
263 		s->s_mapclass.map_cname = name; \
264 		s->s_mapclass.map_ext = ext; \
265 		s->s_mapclass.map_cflags = flags; \
266 		s->s_mapclass.map_parse = parse; \
267 		s->s_mapclass.map_open = open; \
268 		s->s_mapclass.map_close = close; \
269 		s->s_mapclass.map_lookup = lookup; \
270 		s->s_mapclass.map_store = store; \
271 	}
272 
273 setupmaps()
274 {
275 	register STAB *s;
276 
277 #ifdef NEWDB
278 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
279 		map_parseargs, hash_map_open, db_map_close,
280 		db_map_lookup, db_map_store);
281 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
282 		map_parseargs, bt_map_open, db_map_close,
283 		db_map_lookup, db_map_store);
284 #endif
285 
286 #ifdef NDBM
287 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
288 		map_parseargs, ndbm_map_open, ndbm_map_close,
289 		ndbm_map_lookup, ndbm_map_store);
290 #endif
291 
292 #ifdef NIS
293 	MAPDEF("nis", NULL, MCF_ALIASOK,
294 		map_parseargs, nis_map_open, nis_map_close,
295 		nis_map_lookup, nis_map_store);
296 #endif
297 
298 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
299 		map_parseargs, stab_map_open, stab_map_close,
300 		stab_map_lookup, stab_map_store);
301 
302 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
303 		map_parseargs, impl_map_open, impl_map_close,
304 		impl_map_lookup, impl_map_store);
305 
306 	/* host DNS lookup */
307 	MAPDEF("host", NULL, 0,
308 		host_map_init, null_map_open, null_map_close,
309 		host_map_lookup, null_map_store);
310 
311 	/* dequote map */
312 	MAPDEF("dequote", NULL, 0,
313 		dequote_init, null_map_open, null_map_close,
314 		dequote_map, null_map_store);
315 
316 #if 0
317 # ifdef USERDB
318 	/* user database */
319 	MAPDEF("udb", ".db", 0,
320 		udb_map_parse, null_map_open, null_map_close,
321 		udb_map_lookup, null_map_store);
322 # endif
323 #endif
324 }
325 
326 #undef MAPDEF
327 /*
328 **  GETRUID -- get real user id (V7)
329 */
330 
331 getruid()
332 {
333 	if (OpMode == MD_DAEMON)
334 		return (RealUid);
335 	else
336 		return (getuid());
337 }
338 
339 
340 /*
341 **  GETRGID -- get real group id (V7).
342 */
343 
344 getrgid()
345 {
346 	if (OpMode == MD_DAEMON)
347 		return (RealGid);
348 	else
349 		return (getgid());
350 }
351 /*
352 **  USERNAME -- return the user id of the logged in user.
353 **
354 **	Parameters:
355 **		none.
356 **
357 **	Returns:
358 **		The login name of the logged in user.
359 **
360 **	Side Effects:
361 **		none.
362 **
363 **	Notes:
364 **		The return value is statically allocated.
365 */
366 
367 char *
368 username()
369 {
370 	static char *myname = NULL;
371 	extern char *getlogin();
372 	register struct passwd *pw;
373 
374 	/* cache the result */
375 	if (myname == NULL)
376 	{
377 		myname = getlogin();
378 		if (myname == NULL || myname[0] == '\0')
379 		{
380 			pw = getpwuid(getruid());
381 			if (pw != NULL)
382 				myname = newstr(pw->pw_name);
383 		}
384 		else
385 		{
386 			uid_t uid = getuid();
387 
388 			myname = newstr(myname);
389 			if ((pw = getpwnam(myname)) == NULL ||
390 			      (uid != 0 && uid != pw->pw_uid))
391 			{
392 				pw = getpwuid(uid);
393 				if (pw != NULL)
394 					myname = newstr(pw->pw_name);
395 			}
396 		}
397 		if (myname == NULL || myname[0] == '\0')
398 		{
399 			syserr("554 Who are you?");
400 			myname = "postmaster";
401 		}
402 	}
403 
404 	return (myname);
405 }
406 /*
407 **  TTYPATH -- Get the path of the user's tty
408 **
409 **	Returns the pathname of the user's tty.  Returns NULL if
410 **	the user is not logged in or if s/he has write permission
411 **	denied.
412 **
413 **	Parameters:
414 **		none
415 **
416 **	Returns:
417 **		pathname of the user's tty.
418 **		NULL if not logged in or write permission denied.
419 **
420 **	Side Effects:
421 **		none.
422 **
423 **	WARNING:
424 **		Return value is in a local buffer.
425 **
426 **	Called By:
427 **		savemail
428 */
429 
430 char *
431 ttypath()
432 {
433 	struct stat stbuf;
434 	register char *pathn;
435 	extern char *ttyname();
436 	extern char *getlogin();
437 
438 	/* compute the pathname of the controlling tty */
439 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
440 	    (pathn = ttyname(0)) == NULL)
441 	{
442 		errno = 0;
443 		return (NULL);
444 	}
445 
446 	/* see if we have write permission */
447 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
448 	{
449 		errno = 0;
450 		return (NULL);
451 	}
452 
453 	/* see if the user is logged in */
454 	if (getlogin() == NULL)
455 		return (NULL);
456 
457 	/* looks good */
458 	return (pathn);
459 }
460 /*
461 **  CHECKCOMPAT -- check for From and To person compatible.
462 **
463 **	This routine can be supplied on a per-installation basis
464 **	to determine whether a person is allowed to send a message.
465 **	This allows restriction of certain types of internet
466 **	forwarding or registration of users.
467 **
468 **	If the hosts are found to be incompatible, an error
469 **	message should be given using "usrerr" and 0 should
470 **	be returned.
471 **
472 **	'NoReturn' can be set to suppress the return-to-sender
473 **	function; this should be done on huge messages.
474 **
475 **	Parameters:
476 **		to -- the person being sent to.
477 **
478 **	Returns:
479 **		an exit status
480 **
481 **	Side Effects:
482 **		none (unless you include the usrerr stuff)
483 */
484 
485 checkcompat(to, e)
486 	register ADDRESS *to;
487 	register ENVELOPE *e;
488 {
489 # ifdef lint
490 	if (to == NULL)
491 		to++;
492 # endif lint
493 # ifdef EXAMPLE_CODE
494 	/* this code is intended as an example only */
495 	register STAB *s;
496 
497 	s = stab("arpa", ST_MAILER, ST_FIND);
498 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
499 	    to->q_mailer == s->s_mailer)
500 	{
501 		usrerr("553 No ARPA mail through this machine: see your system administration");
502 		/* NoReturn = TRUE; to supress return copy */
503 		return (EX_UNAVAILABLE);
504 	}
505 # endif /* EXAMPLE_CODE */
506 	return (EX_OK);
507 }
508 /*
509 **  HOLDSIGS -- arrange to hold all signals
510 **
511 **	Parameters:
512 **		none.
513 **
514 **	Returns:
515 **		none.
516 **
517 **	Side Effects:
518 **		Arranges that signals are held.
519 */
520 
521 holdsigs()
522 {
523 }
524 /*
525 **  RLSESIGS -- arrange to release all signals
526 **
527 **	This undoes the effect of holdsigs.
528 **
529 **	Parameters:
530 **		none.
531 **
532 **	Returns:
533 **		none.
534 **
535 **	Side Effects:
536 **		Arranges that signals are released.
537 */
538 
539 rlsesigs()
540 {
541 }
542 /*
543 **  GETLA -- get the current load average
544 **
545 **	This code stolen from la.c.
546 **
547 **	Parameters:
548 **		none.
549 **
550 **	Returns:
551 **		The current load average as an integer.
552 **
553 **	Side Effects:
554 **		none.
555 */
556 
557 /* try to guess what style of load average we have */
558 #define LA_ZERO		1	/* always return load average as zero */
559 #define LA_INT		2	/* read kmem for avenrun; interpret as int */
560 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
561 #define LA_SUBR		4	/* call getloadavg */
562 
563 #ifndef LA_TYPE
564 #  if defined(sun) && !defined(BSD)
565 #    define LA_TYPE		LA_INT
566 #  endif
567 #  if defined(mips) || defined(__alpha)
568      /* Ultrix or OSF/1 or RISC/os */
569 #    define LA_TYPE		LA_INT
570 #    define LA_AVENRUN		"avenrun"
571 #  endif
572 #  if defined(__hpux)
573 #    define LA_TYPE		LA_FLOAT
574 #    define LA_AVENRUN		"avenrun"
575 #  endif
576 
577 /* now do the guesses based on general OS type */
578 #  ifndef LA_TYPE
579 #   if defined(SYSTEM5)
580 #    define LA_TYPE		LA_INT
581 #    define LA_AVENRUN		"avenrun"
582 #   else
583 #    if defined(BSD)
584 #     define LA_TYPE		LA_SUBR
585 #    else
586 #     define LA_TYPE		LA_ZERO
587 #    endif
588 #   endif
589 #  endif
590 #endif
591 
592 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
593 
594 #include <nlist.h>
595 
596 #ifndef LA_AVENRUN
597 #define LA_AVENRUN	"_avenrun"
598 #endif
599 
600 /* _PATH_UNIX should be defined in <paths.h> */
601 #ifndef _PATH_UNIX
602 #  if defined(__hpux)
603 #    define _PATH_UNIX		"/hp-ux"
604 #  endif
605 #  if defined(mips) && !defined(ultrix)
606      /* powerful RISC/os */
607 #    define _PATH_UNIX		"/unix"
608 #  endif
609 #  if defined(SYSTEM5)
610 #    ifndef _PATH_UNIX
611 #      define _PATH_UNIX	"/unix"
612 #    endif
613 #  endif
614 #  ifndef _PATH_UNIX
615 #    define _PATH_UNIX		"/vmunix"
616 #  endif
617 #endif
618 
619 struct	nlist Nl[] =
620 {
621 	{ LA_AVENRUN },
622 #define	X_AVENRUN	0
623 	{ 0 },
624 };
625 
626 #if defined(unixpc)
627 # define FSHIFT		5
628 #endif
629 
630 #if defined(__alpha)
631 # define FSHIFT		10
632 #endif
633 
634 #if (LA_TYPE == LA_INT) && !defined(FSHIFT)
635 #  define FSHIFT	8
636 #endif
637 #if (LA_TYPE == LA_INT) && !defined(FSCALE)
638 #  define FSCALE	(1 << FSHIFT)
639 #endif
640 
641 getla()
642 {
643 	static int kmem = -1;
644 #if LA_TYPE == LA_INT
645 	long avenrun[3];
646 #else
647 	double avenrun[3];
648 #endif
649 	extern off_t lseek();
650 	extern int errno;
651 
652 	if (kmem < 0)
653 	{
654 		kmem = open("/dev/kmem", 0, 0);
655 		if (kmem < 0)
656 		{
657 			if (tTd(3, 1))
658 				printf("getla: open(/dev/kmem): %s\n",
659 					errstring(errno));
660 			return (-1);
661 		}
662 		(void) fcntl(kmem, F_SETFD, 1);
663 		if (nlist(_PATH_UNIX, Nl) < 0)
664 		{
665 			if (tTd(3, 1))
666 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
667 					errstring(errno));
668 			return (-1);
669 		}
670 		if (Nl[X_AVENRUN].n_value == 0)
671 		{
672 			if (tTd(3, 1))
673 				printf("getla: nlist(%s, %s) ==> 0\n",
674 					_PATH_UNIX, LA_AVENRUN);
675 			return (-1);
676 		}
677 	}
678 	if (tTd(3, 20))
679 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
680 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
681 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
682 	{
683 		/* thank you Ian */
684 		if (tTd(3, 1))
685 			printf("getla: lseek or read: %s\n", errstring(errno));
686 		return (-1);
687 	}
688 #if LA_TYPE == LA_INT
689 	if (tTd(3, 5))
690 	{
691 		printf("getla: avenrun = %d", avenrun[0]);
692 		if (tTd(3, 15))
693 			printf(", %d, %d", avenrun[1], avenrun[2]);
694 		printf("\n");
695 	}
696 	if (tTd(3, 1))
697 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
698 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
699 #else
700 	if (tTd(3, 5))
701 	{
702 		printf("getla: avenrun = %g", avenrun[0]);
703 		if (tTd(3, 15))
704 			printf(", %g, %g", avenrun[1], avenrun[2]);
705 		printf("\n");
706 	}
707 	if (tTd(3, 1))
708 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
709 	return ((int) (avenrun[0] + 0.5));
710 #endif
711 }
712 
713 #else
714 #if LA_TYPE == LA_SUBR
715 
716 getla()
717 {
718 	double avenrun[3];
719 
720 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
721 	{
722 		if (tTd(3, 1))
723 			perror("getla: getloadavg failed:");
724 		return (-1);
725 	}
726 	if (tTd(3, 1))
727 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
728 	return ((int) (avenrun[0] + 0.5));
729 }
730 
731 #else
732 
733 getla()
734 {
735 	if (tTd(3, 1))
736 		printf("getla: ZERO\n");
737 	return (0);
738 }
739 
740 #endif
741 #endif
742 /*
743 **  SHOULDQUEUE -- should this message be queued or sent?
744 **
745 **	Compares the message cost to the load average to decide.
746 **
747 **	Parameters:
748 **		pri -- the priority of the message in question.
749 **		ctime -- the message creation time.
750 **
751 **	Returns:
752 **		TRUE -- if this message should be queued up for the
753 **			time being.
754 **		FALSE -- if the load is low enough to send this message.
755 **
756 **	Side Effects:
757 **		none.
758 */
759 
760 bool
761 shouldqueue(pri, ctime)
762 	long pri;
763 	time_t ctime;
764 {
765 	if (CurrentLA < QueueLA)
766 		return (FALSE);
767 	if (CurrentLA >= RefuseLA)
768 		return (TRUE);
769 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
770 }
771 /*
772 **  REFUSECONNECTIONS -- decide if connections should be refused
773 **
774 **	Parameters:
775 **		none.
776 **
777 **	Returns:
778 **		TRUE if incoming SMTP connections should be refused
779 **			(for now).
780 **		FALSE if we should accept new work.
781 **
782 **	Side Effects:
783 **		none.
784 */
785 
786 bool
787 refuseconnections()
788 {
789 #ifdef XLA
790 	if (!xla_smtp_ok())
791 		return TRUE;
792 #endif
793 
794 	/* this is probably too simplistic */
795 	return (CurrentLA >= RefuseLA);
796 }
797 /*
798 **  SETPROCTITLE -- set process title for ps
799 **
800 **	Parameters:
801 **		fmt -- a printf style format string.
802 **		a, b, c -- possible parameters to fmt.
803 **
804 **	Returns:
805 **		none.
806 **
807 **	Side Effects:
808 **		Clobbers argv of our main procedure so ps(1) will
809 **		display the title.
810 */
811 
812 #ifdef SETPROCTITLE
813 # ifdef __hpux
814 #  include <sys/pstat.h>
815 # endif
816 # ifdef BSD4_4
817 #  include <machine/vmparam.h>
818 #  include <sys/exec.h>
819 #  ifdef PS_STRINGS
820 #   define SETPROC_STATIC static
821 #  endif
822 # endif
823 # ifndef SETPROC_STATIC
824 #  define SETPROC_STATIC
825 # endif
826 #endif
827 
828 /*VARARGS1*/
829 #ifdef __STDC__
830 setproctitle(char *fmt, ...)
831 #else
832 setproctitle(fmt, va_alist)
833 	char *fmt;
834 	va_dcl
835 #endif
836 {
837 # ifdef SETPROCTITLE
838 	register char *p;
839 	register int i;
840 	SETPROC_STATIC char buf[MAXLINE];
841 	VA_LOCAL_DECL
842 #  ifdef __hpux
843 	union pstun pst;
844 #  endif
845 	extern char **Argv;
846 	extern char *LastArgv;
847 
848 	p = buf;
849 
850 	/* print sendmail: heading for grep */
851 	(void) strcpy(p, "sendmail: ");
852 	p += strlen(p);
853 
854 	/* print the argument string */
855 	VA_START(fmt);
856 	(void) vsprintf(p, fmt, ap);
857 	VA_END;
858 
859 	i = strlen(buf);
860 
861 #  ifdef __hpux
862 	pst.pst_command = buf;
863 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
864 #  else
865 #   ifdef PS_STRINGS
866 	PS_STRINGS->ps_nargvstr = 1;
867 	PS_STRINGS->ps_argvstr = buf;
868 #   else
869 	if (i > LastArgv - Argv[0] - 2)
870 	{
871 		i = LastArgv - Argv[0] - 2;
872 		buf[i] = '\0';
873 	}
874 	(void) strcpy(Argv[0], buf);
875 	p = &Argv[0][i];
876 	while (p < LastArgv)
877 		*p++ = ' ';
878 #   endif
879 #  endif
880 # endif /* SETPROCTITLE */
881 }
882 /*
883 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
884 **
885 **	Parameters:
886 **		none.
887 **
888 **	Returns:
889 **		none.
890 **
891 **	Side Effects:
892 **		Picks up extant zombies.
893 */
894 
895 # include <sys/wait.h>
896 
897 void
898 reapchild()
899 {
900 # ifdef WIFEXITED
901 	auto int status;
902 
903 	while (waitpid(-1, &status, WNOHANG) > 0)
904 		continue;
905 # else
906 # ifdef WNOHANG
907 	union wait status;
908 
909 	while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0)
910 		continue;
911 # else /* WNOHANG */
912 	auto int status;
913 
914 	while (wait(&status) > 0)
915 		continue;
916 # endif /* WNOHANG */
917 # endif
918 # ifdef SYSTEM5
919 	(void) signal(SIGCHLD, reapchild);
920 # endif
921 }
922 /*
923 **  UNSETENV -- remove a variable from the environment
924 **
925 **	Not needed on newer systems.
926 **
927 **	Parameters:
928 **		name -- the string name of the environment variable to be
929 **			deleted from the current environment.
930 **
931 **	Returns:
932 **		none.
933 **
934 **	Globals:
935 **		environ -- a pointer to the current environment.
936 **
937 **	Side Effects:
938 **		Modifies environ.
939 */
940 
941 #ifdef UNSETENV
942 
943 void
944 unsetenv(name)
945 	char *name;
946 {
947 	extern char **environ;
948 	register char **pp;
949 	int len = strlen(name);
950 
951 	for (pp = environ; *pp != NULL; pp++)
952 	{
953 		if (strncmp(name, *pp, len) == 0 &&
954 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
955 			break;
956 	}
957 
958 	for (; *pp != NULL; pp++)
959 		*pp = pp[1];
960 }
961 
962 #endif /* UNSETENV */
963 /*
964 **  GETDTABLESIZE -- return number of file descriptors
965 **
966 **	Only on non-BSD systems
967 **
968 **	Parameters:
969 **		none
970 **
971 **	Returns:
972 **		size of file descriptor table
973 **
974 **	Side Effects:
975 **		none
976 */
977 
978 #ifdef SYSTEM5
979 
980 int
981 getdtablesize()
982 {
983 # ifdef _SC_OPEN_MAX
984 	return sysconf(_SC_OPEN_MAX);
985 # else
986 	return NOFILE;
987 # endif
988 }
989 
990 #endif
991 /*
992 **  UNAME -- get the UUCP name of this system.
993 */
994 
995 #ifndef HASUNAME
996 
997 int
998 uname(name)
999 	struct utsname *name;
1000 {
1001 	FILE *file;
1002 	char *n;
1003 
1004 	name->nodename[0] = '\0';
1005 
1006 	/* try /etc/whoami -- one line with the node name */
1007 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1008 	{
1009 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1010 		(void) fclose(file);
1011 		n = strchr(name->nodename, '\n');
1012 		if (n != NULL)
1013 			*n = '\0';
1014 		if (name->nodename[0] != '\0')
1015 			return (0);
1016 	}
1017 
1018 	/* try /usr/include/whoami.h -- has a #define somewhere */
1019 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1020 	{
1021 		char buf[MAXLINE];
1022 
1023 		while (fgets(buf, MAXLINE, file) != NULL)
1024 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1025 					NODE_LENGTH, name->nodename) > 0)
1026 				break;
1027 		(void) fclose(file);
1028 		if (name->nodename[0] != '\0')
1029 			return (0);
1030 	}
1031 
1032 #ifdef TRUST_POPEN
1033 	/*
1034 	**  Popen is known to have security holes.
1035 	*/
1036 
1037 	/* try uuname -l to return local name */
1038 	if ((file = popen("uuname -l", "r")) != NULL)
1039 	{
1040 		(void) fgets(name, NODE_LENGTH + 1, file);
1041 		(void) pclose(file);
1042 		n = strchr(name, '\n');
1043 		if (n != NULL)
1044 			*n = '\0';
1045 		if (name->nodename[0] != '\0')
1046 			return (0);
1047 	}
1048 #endif
1049 
1050 	return (-1);
1051 }
1052 #endif /* HASUNAME */
1053 /*
1054 **  INITGROUPS -- initialize groups
1055 **
1056 **	Stub implementation for System V style systems
1057 */
1058 
1059 #ifndef HASINITGROUPS
1060 # if !defined(SYSTEM5) || defined(__hpux)
1061 #  define HASINITGROUPS
1062 # endif
1063 #endif
1064 
1065 #ifndef HASINITGROUPS
1066 
1067 initgroups(name, basegid)
1068 	char *name;
1069 	int basegid;
1070 {
1071 	return 0;
1072 }
1073 
1074 #endif
1075 /*
1076 **  SETSID -- set session id (for non-POSIX systems)
1077 */
1078 
1079 #ifndef HASSETSID
1080 
1081 setsid()
1082 {
1083 # ifdef SYSTEM5
1084 	setpgrp();
1085 # endif
1086 }
1087 
1088 #endif
1089 /*
1090 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1091 **
1092 **	Only implemented if you have statfs.
1093 **
1094 **	Parameters:
1095 **		msize -- the size to check against.  If zero, we don't yet
1096 **			know how big the message will be, so just check for
1097 **			a "reasonable" amount.
1098 **
1099 **	Returns:
1100 **		TRUE if there is enough space.
1101 **		FALSE otherwise.
1102 */
1103 
1104 #ifndef HASSTATFS
1105 # if defined(BSD4_4) || defined(__osf__)
1106 #  define HASSTATFS
1107 # endif
1108 #endif
1109 
1110 #ifdef HASSTATFS
1111 # undef HASUSTAT
1112 #endif
1113 
1114 #if defined(HASUSTAT)
1115 # include <ustat.h>
1116 #endif
1117 
1118 #ifdef HASSTATFS
1119 # if defined(sgi) || defined(apollo)
1120 #  include <sys/statfs.h>
1121 # else
1122 #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
1123 #   include <sys/vfs.h>
1124 #  else
1125 #   include <sys/mount.h>
1126 #  endif
1127 # endif
1128 #endif
1129 
1130 bool
1131 enoughspace(msize)
1132 	long msize;
1133 {
1134 #if defined(HASSTATFS) || defined(HASUSTAT)
1135 # if defined(HASUSTAT)
1136 	struct ustat fs;
1137 	struct stat statbuf;
1138 #  define FSBLOCKSIZE	DEV_BSIZE
1139 #  define f_bavail	f_tfree
1140 # else
1141 #  if defined(ultrix)
1142 	struct fs_data fs;
1143 #   define f_bavail	fd_bfreen
1144 #   define FSBLOCKSIZE	fs.fd_bsize
1145 #  else
1146 	struct statfs fs;
1147 #   define FSBLOCKSIZE	fs.f_bsize
1148 #  endif
1149 # endif
1150 	long blocksneeded;
1151 	extern int errno;
1152 
1153 	if (MinBlocksFree <= 0 && msize <= 0)
1154 	{
1155 		if (tTd(4, 80))
1156 			printf("enoughspace: no threshold\n");
1157 		return TRUE;
1158 	}
1159 
1160 # if defined(HASUSTAT)
1161 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1162 # else
1163 #  if defined(sgi) || defined(apollo)
1164 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
1165 #  else
1166 #   if defined(ultrix)
1167 	if (statfs(QueueDir, &fs) > 0)
1168 #   else
1169 	if (statfs(QueueDir, &fs) == 0)
1170 #   endif
1171 #  endif
1172 # endif
1173 	{
1174 		if (tTd(4, 80))
1175 			printf("enoughspace: bavail=%ld, need=%ld\n",
1176 				fs.f_bavail, msize);
1177 
1178 		/* convert msize to block count */
1179 		msize = msize / FSBLOCKSIZE + 1;
1180 		if (MinBlocksFree >= 0)
1181 			msize += MinBlocksFree;
1182 
1183 		if (fs.f_bavail < msize)
1184 		{
1185 #ifdef LOG
1186 			if (LogLevel > 0)
1187 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
1188 					QueueDir, fs.f_bavail, msize);
1189 #endif
1190 			return FALSE;
1191 		}
1192 	}
1193 	else if (tTd(4, 80))
1194 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1195 			MinBlocksFree, msize, errstring(errno));
1196 #endif
1197 	return TRUE;
1198 }
1199 /*
1200 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1201 **
1202 **	This looks at an errno value and tells if this is likely to
1203 **	go away if retried later.
1204 **
1205 **	Parameters:
1206 **		err -- the errno code to classify.
1207 **
1208 **	Returns:
1209 **		TRUE if this is probably transient.
1210 **		FALSE otherwise.
1211 */
1212 
1213 bool
1214 transienterror(err)
1215 	int err;
1216 {
1217 	switch (err)
1218 	{
1219 	  case EIO:			/* I/O error */
1220 	  case ENXIO:			/* Device not configured */
1221 	  case EAGAIN:			/* Resource temporarily unavailable */
1222 	  case ENOMEM:			/* Cannot allocate memory */
1223 	  case ENODEV:			/* Operation not supported by device */
1224 	  case ENFILE:			/* Too many open files in system */
1225 	  case EMFILE:			/* Too many open files */
1226 	  case ENOSPC:			/* No space left on device */
1227 #ifdef ETIMEDOUT
1228 	  case ETIMEDOUT:		/* Connection timed out */
1229 #endif
1230 #ifdef ESTALE
1231 	  case ESTALE:			/* Stale NFS file handle */
1232 #endif
1233 #ifdef ENETDOWN
1234 	  case ENETDOWN:		/* Network is down */
1235 #endif
1236 #ifdef ENETUNREACH
1237 	  case ENETUNREACH:		/* Network is unreachable */
1238 #endif
1239 #ifdef ENETRESET
1240 	  case ENETRESET:		/* Network dropped connection on reset */
1241 #endif
1242 #ifdef ECONNABORTED
1243 	  case ECONNABORTED:		/* Software caused connection abort */
1244 #endif
1245 #ifdef ECONNRESET
1246 	  case ECONNRESET:		/* Connection reset by peer */
1247 #endif
1248 #ifdef ENOBUFS
1249 	  case ENOBUFS:			/* No buffer space available */
1250 #endif
1251 #ifdef ESHUTDOWN
1252 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1253 #endif
1254 #ifdef ECONNREFUSED
1255 	  case ECONNREFUSED:		/* Connection refused */
1256 #endif
1257 #ifdef EHOSTDOWN
1258 	  case EHOSTDOWN:		/* Host is down */
1259 #endif
1260 #ifdef EHOSTUNREACH
1261 	  case EHOSTUNREACH:		/* No route to host */
1262 #endif
1263 #ifdef EDQUOT
1264 	  case EDQUOT:			/* Disc quota exceeded */
1265 #endif
1266 #ifdef EPROCLIM
1267 	  case EPROCLIM:		/* Too many processes */
1268 #endif
1269 #ifdef EUSERS
1270 	  case EUSERS:			/* Too many users */
1271 #endif
1272 #ifdef EDEADLK
1273 	  case EDEADLK:			/* Resource deadlock avoided */
1274 #endif
1275 #ifdef EISCONN
1276 	  case EISCONN:			/* Socket already connected */
1277 #endif
1278 #ifdef EINPROGRESS
1279 	  case EINPROGRESS:		/* Operation now in progress */
1280 #endif
1281 #ifdef EALREADY
1282 	  case EALREADY:		/* Operation already in progress */
1283 #endif
1284 #ifdef EADDRINUSE
1285 	  case EADDRINUSE:		/* Address already in use */
1286 #endif
1287 #ifdef EADDRNOTAVAIL
1288 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1289 #endif
1290 #ifdef ENOSR
1291 	  case ENOSR:			/* Out of streams resources */
1292 #endif
1293 		return TRUE;
1294 	}
1295 
1296 	/* nope, must be permanent */
1297 	return FALSE;
1298 }
1299 /*
1300 **  LOCKFILE -- lock a file using flock or (shudder) lockf
1301 **
1302 **	Parameters:
1303 **		fd -- the file descriptor of the file.
1304 **		filename -- the file name (for error messages).
1305 **		type -- type of the lock.  Bits can be:
1306 **			LOCK_EX -- exclusive lock.
1307 **			LOCK_NB -- non-blocking.
1308 **
1309 **	Returns:
1310 **		TRUE if the lock was acquired.
1311 **		FALSE otherwise.
1312 */
1313 
1314 bool
1315 lockfile(fd, filename, type)
1316 	int fd;
1317 	char *filename;
1318 	int type;
1319 {
1320 # ifdef LOCKF
1321 	int action;
1322 	struct flock lfd;
1323 
1324 	if (bitset(LOCK_UN, type))
1325 		lfd.l_type = F_UNLCK;
1326 	else if (bitset(LOCK_EX, type))
1327 		lfd.l_type = F_WRLCK;
1328 	else
1329 		lfd.l_type = F_RDLCK;
1330 
1331 	if (bitset(LOCK_NB, type))
1332 		action = F_SETLK;
1333 	else
1334 		action = F_SETLKW;
1335 
1336 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1337 
1338 	if (fcntl(fd, action, &lfd) >= 0)
1339 		return TRUE;
1340 
1341 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1342 		syserr("cannot lockf(%s, %o)", filename, type);
1343 # else
1344 	if (flock(fd, type) >= 0)
1345 		return TRUE;
1346 
1347 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1348 		syserr("cannot flock(%s, %o)", filename, type);
1349 # endif
1350 	return FALSE;
1351 }
1352