xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 63937)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)conf.c	8.5 (Berkeley) 07/19/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include "pathnames.h"
15 # include <sys/ioctl.h>
16 # include <sys/param.h>
17 # include <signal.h>
18 # include <pwd.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_FORCE|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 = (RealUid != 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 **  USERNAME -- return the user id of the logged in user.
329 **
330 **	Parameters:
331 **		none.
332 **
333 **	Returns:
334 **		The login name of the logged in user.
335 **
336 **	Side Effects:
337 **		none.
338 **
339 **	Notes:
340 **		The return value is statically allocated.
341 */
342 
343 char *
344 username()
345 {
346 	static char *myname = NULL;
347 	extern char *getlogin();
348 	register struct passwd *pw;
349 
350 	/* cache the result */
351 	if (myname == NULL)
352 	{
353 		myname = getlogin();
354 		if (myname == NULL || myname[0] == '\0')
355 		{
356 			pw = getpwuid(RealUid);
357 			if (pw != NULL)
358 				myname = newstr(pw->pw_name);
359 		}
360 		else
361 		{
362 			uid_t uid = RealUid;
363 
364 			myname = newstr(myname);
365 			if ((pw = getpwnam(myname)) == NULL ||
366 			      (uid != 0 && uid != pw->pw_uid))
367 			{
368 				pw = getpwuid(uid);
369 				if (pw != NULL)
370 					myname = newstr(pw->pw_name);
371 			}
372 		}
373 		if (myname == NULL || myname[0] == '\0')
374 		{
375 			syserr("554 Who are you?");
376 			myname = "postmaster";
377 		}
378 	}
379 
380 	return (myname);
381 }
382 /*
383 **  TTYPATH -- Get the path of the user's tty
384 **
385 **	Returns the pathname of the user's tty.  Returns NULL if
386 **	the user is not logged in or if s/he has write permission
387 **	denied.
388 **
389 **	Parameters:
390 **		none
391 **
392 **	Returns:
393 **		pathname of the user's tty.
394 **		NULL if not logged in or write permission denied.
395 **
396 **	Side Effects:
397 **		none.
398 **
399 **	WARNING:
400 **		Return value is in a local buffer.
401 **
402 **	Called By:
403 **		savemail
404 */
405 
406 char *
407 ttypath()
408 {
409 	struct stat stbuf;
410 	register char *pathn;
411 	extern char *ttyname();
412 	extern char *getlogin();
413 
414 	/* compute the pathname of the controlling tty */
415 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
416 	    (pathn = ttyname(0)) == NULL)
417 	{
418 		errno = 0;
419 		return (NULL);
420 	}
421 
422 	/* see if we have write permission */
423 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
424 	{
425 		errno = 0;
426 		return (NULL);
427 	}
428 
429 	/* see if the user is logged in */
430 	if (getlogin() == NULL)
431 		return (NULL);
432 
433 	/* looks good */
434 	return (pathn);
435 }
436 /*
437 **  CHECKCOMPAT -- check for From and To person compatible.
438 **
439 **	This routine can be supplied on a per-installation basis
440 **	to determine whether a person is allowed to send a message.
441 **	This allows restriction of certain types of internet
442 **	forwarding or registration of users.
443 **
444 **	If the hosts are found to be incompatible, an error
445 **	message should be given using "usrerr" and 0 should
446 **	be returned.
447 **
448 **	'NoReturn' can be set to suppress the return-to-sender
449 **	function; this should be done on huge messages.
450 **
451 **	Parameters:
452 **		to -- the person being sent to.
453 **
454 **	Returns:
455 **		an exit status
456 **
457 **	Side Effects:
458 **		none (unless you include the usrerr stuff)
459 */
460 
461 checkcompat(to, e)
462 	register ADDRESS *to;
463 	register ENVELOPE *e;
464 {
465 # ifdef lint
466 	if (to == NULL)
467 		to++;
468 # endif /* lint */
469 # ifdef EXAMPLE_CODE
470 	/* this code is intended as an example only */
471 	register STAB *s;
472 
473 	s = stab("arpa", ST_MAILER, ST_FIND);
474 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
475 	    to->q_mailer == s->s_mailer)
476 	{
477 		usrerr("553 No ARPA mail through this machine: see your system administration");
478 		/* NoReturn = TRUE; to supress return copy */
479 		return (EX_UNAVAILABLE);
480 	}
481 # endif /* EXAMPLE_CODE */
482 	return (EX_OK);
483 }
484 /*
485 **  HOLDSIGS -- arrange to hold all signals
486 **
487 **	Parameters:
488 **		none.
489 **
490 **	Returns:
491 **		none.
492 **
493 **	Side Effects:
494 **		Arranges that signals are held.
495 */
496 
497 holdsigs()
498 {
499 }
500 /*
501 **  RLSESIGS -- arrange to release all signals
502 **
503 **	This undoes the effect of holdsigs.
504 **
505 **	Parameters:
506 **		none.
507 **
508 **	Returns:
509 **		none.
510 **
511 **	Side Effects:
512 **		Arranges that signals are released.
513 */
514 
515 rlsesigs()
516 {
517 }
518 /*
519 **  GETLA -- get the current load average
520 **
521 **	This code stolen from la.c.
522 **
523 **	Parameters:
524 **		none.
525 **
526 **	Returns:
527 **		The current load average as an integer.
528 **
529 **	Side Effects:
530 **		none.
531 */
532 
533 /* try to guess what style of load average we have */
534 #define LA_ZERO		1	/* always return load average as zero */
535 #define LA_INT		2	/* read kmem for avenrun; interpret as int */
536 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
537 #define LA_SUBR		4	/* call getloadavg */
538 
539 #ifndef LA_TYPE
540 #  if defined(sun) && !defined(BSD)
541 #    define LA_TYPE		LA_INT
542 #  endif
543 #  if defined(mips) || defined(__alpha)
544      /* Ultrix or OSF/1 or RISC/os */
545 #    define LA_TYPE		LA_INT
546 #    define LA_AVENRUN		"avenrun"
547 #  endif
548 #  if defined(__hpux)
549 #    define LA_TYPE		LA_FLOAT
550 #    define LA_AVENRUN		"avenrun"
551 #  endif
552 #  if defined(__NeXT__)
553 #    define LA_TYPE		LA_ZERO
554 #  endif
555 
556 /* now do the guesses based on general OS type */
557 #  ifndef LA_TYPE
558 #   if defined(SYSTEM5)
559 #    define LA_TYPE		LA_INT
560 #    define LA_AVENRUN		"avenrun"
561 #   else
562 #    if defined(BSD)
563 #     define LA_TYPE		LA_SUBR
564 #    else
565 #     define LA_TYPE		LA_ZERO
566 #    endif
567 #   endif
568 #  endif
569 #endif
570 
571 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT)
572 
573 #include <nlist.h>
574 
575 #ifndef LA_AVENRUN
576 #define LA_AVENRUN	"_avenrun"
577 #endif
578 
579 /* _PATH_UNIX should be defined in <paths.h> */
580 #ifndef _PATH_UNIX
581 #  if defined(__hpux)
582 #    define _PATH_UNIX		"/hp-ux"
583 #  endif
584 #  if defined(mips) && !defined(ultrix)
585      /* powerful RISC/os */
586 #    define _PATH_UNIX		"/unix"
587 #  endif
588 #  if defined(Solaris2)
589      /* Solaris 2 */
590 #    define _PATH_UNIX		"/kernel/unix"
591 #  endif
592 #  if defined(SYSTEM5)
593 #    ifndef _PATH_UNIX
594 #      define _PATH_UNIX	"/unix"
595 #    endif
596 #  endif
597 #  ifndef _PATH_UNIX
598 #    define _PATH_UNIX		"/vmunix"
599 #  endif
600 #endif
601 
602 struct	nlist Nl[] =
603 {
604 	{ LA_AVENRUN },
605 #define	X_AVENRUN	0
606 	{ 0 },
607 };
608 
609 #ifndef FSHIFT
610 # if defined(unixpc)
611 #  define FSHIFT	5
612 # endif
613 
614 # if defined(__alpha)
615 #  define FSHIFT	10
616 # endif
617 
618 # if (LA_TYPE == LA_INT)
619 #  define FSHIFT	8
620 # endif
621 #endif
622 
623 #if (LA_TYPE == LA_INT) && !defined(FSCALE)
624 #  define FSCALE	(1 << FSHIFT)
625 #endif
626 
627 getla()
628 {
629 	static int kmem = -1;
630 #if LA_TYPE == LA_INT
631 	long avenrun[3];
632 #else
633 	double avenrun[3];
634 #endif
635 	extern off_t lseek();
636 	extern int errno;
637 
638 	if (kmem < 0)
639 	{
640 		kmem = open("/dev/kmem", 0, 0);
641 		if (kmem < 0)
642 		{
643 			if (tTd(3, 1))
644 				printf("getla: open(/dev/kmem): %s\n",
645 					errstring(errno));
646 			return (-1);
647 		}
648 		(void) fcntl(kmem, F_SETFD, 1);
649 		if (nlist(_PATH_UNIX, Nl) < 0)
650 		{
651 			if (tTd(3, 1))
652 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
653 					errstring(errno));
654 			return (-1);
655 		}
656 		if (Nl[X_AVENRUN].n_value == 0)
657 		{
658 			if (tTd(3, 1))
659 				printf("getla: nlist(%s, %s) ==> 0\n",
660 					_PATH_UNIX, LA_AVENRUN);
661 			return (-1);
662 		}
663 	}
664 	if (tTd(3, 20))
665 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
666 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
667 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
668 	{
669 		/* thank you Ian */
670 		if (tTd(3, 1))
671 			printf("getla: lseek or read: %s\n", errstring(errno));
672 		return (-1);
673 	}
674 #if LA_TYPE == LA_INT
675 	if (tTd(3, 5))
676 	{
677 		printf("getla: avenrun = %d", avenrun[0]);
678 		if (tTd(3, 15))
679 			printf(", %d, %d", avenrun[1], avenrun[2]);
680 		printf("\n");
681 	}
682 	if (tTd(3, 1))
683 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
684 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
685 #else
686 	if (tTd(3, 5))
687 	{
688 		printf("getla: avenrun = %g", avenrun[0]);
689 		if (tTd(3, 15))
690 			printf(", %g, %g", avenrun[1], avenrun[2]);
691 		printf("\n");
692 	}
693 	if (tTd(3, 1))
694 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
695 	return ((int) (avenrun[0] + 0.5));
696 #endif
697 }
698 
699 #else
700 #if LA_TYPE == LA_SUBR
701 
702 getla()
703 {
704 	double avenrun[3];
705 
706 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
707 	{
708 		if (tTd(3, 1))
709 			perror("getla: getloadavg failed:");
710 		return (-1);
711 	}
712 	if (tTd(3, 1))
713 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
714 	return ((int) (avenrun[0] + 0.5));
715 }
716 
717 #else
718 
719 getla()
720 {
721 	if (tTd(3, 1))
722 		printf("getla: ZERO\n");
723 	return (0);
724 }
725 
726 #endif
727 #endif
728 /*
729 **  SHOULDQUEUE -- should this message be queued or sent?
730 **
731 **	Compares the message cost to the load average to decide.
732 **
733 **	Parameters:
734 **		pri -- the priority of the message in question.
735 **		ctime -- the message creation time.
736 **
737 **	Returns:
738 **		TRUE -- if this message should be queued up for the
739 **			time being.
740 **		FALSE -- if the load is low enough to send this message.
741 **
742 **	Side Effects:
743 **		none.
744 */
745 
746 bool
747 shouldqueue(pri, ctime)
748 	long pri;
749 	time_t ctime;
750 {
751 	if (CurrentLA < QueueLA)
752 		return (FALSE);
753 	if (CurrentLA >= RefuseLA)
754 		return (TRUE);
755 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
756 }
757 /*
758 **  REFUSECONNECTIONS -- decide if connections should be refused
759 **
760 **	Parameters:
761 **		none.
762 **
763 **	Returns:
764 **		TRUE if incoming SMTP connections should be refused
765 **			(for now).
766 **		FALSE if we should accept new work.
767 **
768 **	Side Effects:
769 **		none.
770 */
771 
772 bool
773 refuseconnections()
774 {
775 #ifdef XLA
776 	if (!xla_smtp_ok())
777 		return TRUE;
778 #endif
779 
780 	/* this is probably too simplistic */
781 	return (CurrentLA >= RefuseLA);
782 }
783 /*
784 **  SETPROCTITLE -- set process title for ps
785 **
786 **	Parameters:
787 **		fmt -- a printf style format string.
788 **		a, b, c -- possible parameters to fmt.
789 **
790 **	Returns:
791 **		none.
792 **
793 **	Side Effects:
794 **		Clobbers argv of our main procedure so ps(1) will
795 **		display the title.
796 */
797 
798 #ifdef SETPROCTITLE
799 # ifdef __hpux
800 #  include <sys/pstat.h>
801 # endif
802 # ifdef BSD4_4
803 #  include <machine/vmparam.h>
804 #  include <sys/exec.h>
805 #  ifdef PS_STRINGS
806 #   define SETPROC_STATIC static
807 #  endif
808 # endif
809 # ifndef SETPROC_STATIC
810 #  define SETPROC_STATIC
811 # endif
812 #endif
813 
814 /*VARARGS1*/
815 #ifdef __STDC__
816 setproctitle(char *fmt, ...)
817 #else
818 setproctitle(fmt, va_alist)
819 	char *fmt;
820 	va_dcl
821 #endif
822 {
823 # ifdef SETPROCTITLE
824 	register char *p;
825 	register int i;
826 	SETPROC_STATIC char buf[MAXLINE];
827 	VA_LOCAL_DECL
828 #  ifdef __hpux
829 	union pstun pst;
830 #  endif
831 	extern char **Argv;
832 	extern char *LastArgv;
833 
834 	p = buf;
835 
836 	/* print sendmail: heading for grep */
837 	(void) strcpy(p, "sendmail: ");
838 	p += strlen(p);
839 
840 	/* print the argument string */
841 	VA_START(fmt);
842 	(void) vsprintf(p, fmt, ap);
843 	VA_END;
844 
845 	i = strlen(buf);
846 
847 #  ifdef __hpux
848 	pst.pst_command = buf;
849 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
850 #  else
851 #   ifdef PS_STRINGS
852 	PS_STRINGS->ps_nargvstr = 1;
853 	PS_STRINGS->ps_argvstr = buf;
854 #   else
855 	if (i > LastArgv - Argv[0] - 2)
856 	{
857 		i = LastArgv - Argv[0] - 2;
858 		buf[i] = '\0';
859 	}
860 	(void) strcpy(Argv[0], buf);
861 	p = &Argv[0][i];
862 	while (p < LastArgv)
863 		*p++ = ' ';
864 #   endif
865 #  endif
866 # endif /* SETPROCTITLE */
867 }
868 /*
869 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
870 **
871 **	Parameters:
872 **		none.
873 **
874 **	Returns:
875 **		none.
876 **
877 **	Side Effects:
878 **		Picks up extant zombies.
879 */
880 
881 # include <sys/wait.h>
882 
883 void
884 reapchild()
885 {
886 # if defined(WIFEXITED) && !defined(__NeXT__)
887 	auto int status;
888 	int count;
889 	int pid;
890 
891 	count = 0;
892 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
893 	{
894 		if (count++ > 1000)
895 		{
896 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
897 				pid, status);
898 			break;
899 		}
900 	}
901 # else
902 # ifdef WNOHANG
903 	union wait status;
904 
905 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
906 		continue;
907 # else /* WNOHANG */
908 	auto int status;
909 
910 	while (wait(&status) > 0)
911 		continue;
912 # endif /* WNOHANG */
913 # endif
914 # ifdef SYSTEM5
915 	(void) signal(SIGCHLD, reapchild);
916 # endif
917 }
918 /*
919 **  UNSETENV -- remove a variable from the environment
920 **
921 **	Not needed on newer systems.
922 **
923 **	Parameters:
924 **		name -- the string name of the environment variable to be
925 **			deleted from the current environment.
926 **
927 **	Returns:
928 **		none.
929 **
930 **	Globals:
931 **		environ -- a pointer to the current environment.
932 **
933 **	Side Effects:
934 **		Modifies environ.
935 */
936 
937 #ifdef UNSETENV
938 
939 void
940 unsetenv(name)
941 	char *name;
942 {
943 	extern char **environ;
944 	register char **pp;
945 	int len = strlen(name);
946 
947 	for (pp = environ; *pp != NULL; pp++)
948 	{
949 		if (strncmp(name, *pp, len) == 0 &&
950 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
951 			break;
952 	}
953 
954 	for (; *pp != NULL; pp++)
955 		*pp = pp[1];
956 }
957 
958 #endif /* UNSETENV */
959 /*
960 **  GETDTABLESIZE -- return number of file descriptors
961 **
962 **	Only on non-BSD systems
963 **
964 **	Parameters:
965 **		none
966 **
967 **	Returns:
968 **		size of file descriptor table
969 **
970 **	Side Effects:
971 **		none
972 */
973 
974 #ifdef SOLARIS
975 # include <sys/resource.h>
976 #endif
977 
978 int
979 getdtsize()
980 {
981 #ifdef RLIMIT_NOFILE
982 	struct rlimit rl;
983 
984 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
985 		return rl.rlim_cur;
986 #endif
987 
988 # ifdef _SC_OPEN_MAX
989 	return sysconf(_SC_OPEN_MAX);
990 # else
991 #  ifdef HASGETDTABLESIZE
992 	return getdtablesize();
993 #  else
994 	return NOFILE;
995 #  endif
996 # endif
997 }
998 /*
999 **  UNAME -- get the UUCP name of this system.
1000 */
1001 
1002 #ifndef HASUNAME
1003 
1004 int
1005 uname(name)
1006 	struct utsname *name;
1007 {
1008 	FILE *file;
1009 	char *n;
1010 
1011 	name->nodename[0] = '\0';
1012 
1013 	/* try /etc/whoami -- one line with the node name */
1014 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1015 	{
1016 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1017 		(void) fclose(file);
1018 		n = strchr(name->nodename, '\n');
1019 		if (n != NULL)
1020 			*n = '\0';
1021 		if (name->nodename[0] != '\0')
1022 			return (0);
1023 	}
1024 
1025 	/* try /usr/include/whoami.h -- has a #define somewhere */
1026 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1027 	{
1028 		char buf[MAXLINE];
1029 
1030 		while (fgets(buf, MAXLINE, file) != NULL)
1031 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1032 					NODE_LENGTH, name->nodename) > 0)
1033 				break;
1034 		(void) fclose(file);
1035 		if (name->nodename[0] != '\0')
1036 			return (0);
1037 	}
1038 
1039 #ifdef TRUST_POPEN
1040 	/*
1041 	**  Popen is known to have security holes.
1042 	*/
1043 
1044 	/* try uuname -l to return local name */
1045 	if ((file = popen("uuname -l", "r")) != NULL)
1046 	{
1047 		(void) fgets(name, NODE_LENGTH + 1, file);
1048 		(void) pclose(file);
1049 		n = strchr(name, '\n');
1050 		if (n != NULL)
1051 			*n = '\0';
1052 		if (name->nodename[0] != '\0')
1053 			return (0);
1054 	}
1055 #endif
1056 
1057 	return (-1);
1058 }
1059 #endif /* HASUNAME */
1060 /*
1061 **  INITGROUPS -- initialize groups
1062 **
1063 **	Stub implementation for System V style systems
1064 */
1065 
1066 #ifndef HASINITGROUPS
1067 # if !defined(SYSTEM5) || defined(__hpux)
1068 #  define HASINITGROUPS
1069 # endif
1070 #endif
1071 
1072 #ifndef HASINITGROUPS
1073 
1074 initgroups(name, basegid)
1075 	char *name;
1076 	int basegid;
1077 {
1078 	return 0;
1079 }
1080 
1081 #endif
1082 /*
1083 **  SETSID -- set session id (for non-POSIX systems)
1084 */
1085 
1086 #ifndef HASSETSID
1087 
1088 pid_t
1089 setsid __P ((void))
1090 {
1091 # ifdef SYSTEM5
1092 	return setpgrp();
1093 # else
1094 	return 0;
1095 # endif
1096 }
1097 
1098 #endif
1099 /*
1100 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1101 **
1102 **	Only implemented if you have statfs.
1103 **
1104 **	Parameters:
1105 **		msize -- the size to check against.  If zero, we don't yet
1106 **			know how big the message will be, so just check for
1107 **			a "reasonable" amount.
1108 **
1109 **	Returns:
1110 **		TRUE if there is enough space.
1111 **		FALSE otherwise.
1112 */
1113 
1114 #ifndef HASSTATFS
1115 # if defined(BSD4_4) || defined(__osf__)
1116 #  define HASSTATFS
1117 # endif
1118 #endif
1119 
1120 #ifdef HASSTATFS
1121 # undef HASUSTAT
1122 #endif
1123 
1124 #if defined(HASUSTAT)
1125 # include <ustat.h>
1126 #endif
1127 
1128 #ifdef HASSTATFS
1129 # if defined(sgi) || defined(apollo)
1130 #  include <sys/statfs.h>
1131 # else
1132 #  if (defined(sun) && !defined(BSD)) || defined(__hpux)
1133 #   include <sys/vfs.h>
1134 #  else
1135 #   include <sys/mount.h>
1136 #  endif
1137 # endif
1138 #endif
1139 
1140 bool
1141 enoughspace(msize)
1142 	long msize;
1143 {
1144 #if defined(HASSTATFS) || defined(HASUSTAT)
1145 # if defined(HASUSTAT)
1146 	struct ustat fs;
1147 	struct stat statbuf;
1148 #  define FSBLOCKSIZE	DEV_BSIZE
1149 #  define f_bavail	f_tfree
1150 # else
1151 #  if defined(ultrix)
1152 	struct fs_data fs;
1153 #   define f_bavail	fd_bfreen
1154 #   define FSBLOCKSIZE	fs.fd_bsize
1155 #  else
1156 	struct statfs fs;
1157 #   define FSBLOCKSIZE	fs.f_bsize
1158 #  endif
1159 # endif
1160 	extern int errno;
1161 
1162 	if (MinBlocksFree <= 0 && msize <= 0)
1163 	{
1164 		if (tTd(4, 80))
1165 			printf("enoughspace: no threshold\n");
1166 		return TRUE;
1167 	}
1168 
1169 # if defined(HASUSTAT)
1170 	if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1171 # else
1172 #  if defined(sgi) || defined(apollo)
1173 	if (statfs(QueueDir, &fs, sizeof fs, 0) == 0)
1174 #  else
1175 #   if defined(ultrix)
1176 	if (statfs(QueueDir, &fs) > 0)
1177 #   else
1178 	if (statfs(QueueDir, &fs) == 0)
1179 #   endif
1180 #  endif
1181 # endif
1182 	{
1183 		if (tTd(4, 80))
1184 			printf("enoughspace: bavail=%ld, need=%ld\n",
1185 				fs.f_bavail, msize);
1186 
1187 		/* convert msize to block count */
1188 		msize = msize / FSBLOCKSIZE + 1;
1189 		if (MinBlocksFree >= 0)
1190 			msize += MinBlocksFree;
1191 
1192 		if (fs.f_bavail < msize)
1193 		{
1194 #ifdef LOG
1195 			if (LogLevel > 0)
1196 				syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)",
1197 					QueueDir, fs.f_bavail, msize);
1198 #endif
1199 			return FALSE;
1200 		}
1201 	}
1202 	else if (tTd(4, 80))
1203 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1204 			MinBlocksFree, msize, errstring(errno));
1205 #endif
1206 	return TRUE;
1207 }
1208 /*
1209 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1210 **
1211 **	This looks at an errno value and tells if this is likely to
1212 **	go away if retried later.
1213 **
1214 **	Parameters:
1215 **		err -- the errno code to classify.
1216 **
1217 **	Returns:
1218 **		TRUE if this is probably transient.
1219 **		FALSE otherwise.
1220 */
1221 
1222 bool
1223 transienterror(err)
1224 	int err;
1225 {
1226 	switch (err)
1227 	{
1228 	  case EIO:			/* I/O error */
1229 	  case ENXIO:			/* Device not configured */
1230 	  case EAGAIN:			/* Resource temporarily unavailable */
1231 	  case ENOMEM:			/* Cannot allocate memory */
1232 	  case ENODEV:			/* Operation not supported by device */
1233 	  case ENFILE:			/* Too many open files in system */
1234 	  case EMFILE:			/* Too many open files */
1235 	  case ENOSPC:			/* No space left on device */
1236 #ifdef ETIMEDOUT
1237 	  case ETIMEDOUT:		/* Connection timed out */
1238 #endif
1239 #ifdef ESTALE
1240 	  case ESTALE:			/* Stale NFS file handle */
1241 #endif
1242 #ifdef ENETDOWN
1243 	  case ENETDOWN:		/* Network is down */
1244 #endif
1245 #ifdef ENETUNREACH
1246 	  case ENETUNREACH:		/* Network is unreachable */
1247 #endif
1248 #ifdef ENETRESET
1249 	  case ENETRESET:		/* Network dropped connection on reset */
1250 #endif
1251 #ifdef ECONNABORTED
1252 	  case ECONNABORTED:		/* Software caused connection abort */
1253 #endif
1254 #ifdef ECONNRESET
1255 	  case ECONNRESET:		/* Connection reset by peer */
1256 #endif
1257 #ifdef ENOBUFS
1258 	  case ENOBUFS:			/* No buffer space available */
1259 #endif
1260 #ifdef ESHUTDOWN
1261 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1262 #endif
1263 #ifdef ECONNREFUSED
1264 	  case ECONNREFUSED:		/* Connection refused */
1265 #endif
1266 #ifdef EHOSTDOWN
1267 	  case EHOSTDOWN:		/* Host is down */
1268 #endif
1269 #ifdef EHOSTUNREACH
1270 	  case EHOSTUNREACH:		/* No route to host */
1271 #endif
1272 #ifdef EDQUOT
1273 	  case EDQUOT:			/* Disc quota exceeded */
1274 #endif
1275 #ifdef EPROCLIM
1276 	  case EPROCLIM:		/* Too many processes */
1277 #endif
1278 #ifdef EUSERS
1279 	  case EUSERS:			/* Too many users */
1280 #endif
1281 #ifdef EDEADLK
1282 	  case EDEADLK:			/* Resource deadlock avoided */
1283 #endif
1284 #ifdef EISCONN
1285 	  case EISCONN:			/* Socket already connected */
1286 #endif
1287 #ifdef EINPROGRESS
1288 	  case EINPROGRESS:		/* Operation now in progress */
1289 #endif
1290 #ifdef EALREADY
1291 	  case EALREADY:		/* Operation already in progress */
1292 #endif
1293 #ifdef EADDRINUSE
1294 	  case EADDRINUSE:		/* Address already in use */
1295 #endif
1296 #ifdef EADDRNOTAVAIL
1297 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1298 #endif
1299 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1300 	  case ENOSR:			/* Out of streams resources */
1301 #endif
1302 		return TRUE;
1303 	}
1304 
1305 	/* nope, must be permanent */
1306 	return FALSE;
1307 }
1308 /*
1309 **  LOCKFILE -- lock a file using flock or (shudder) lockf
1310 **
1311 **	Parameters:
1312 **		fd -- the file descriptor of the file.
1313 **		filename -- the file name (for error messages).
1314 **		type -- type of the lock.  Bits can be:
1315 **			LOCK_EX -- exclusive lock.
1316 **			LOCK_NB -- non-blocking.
1317 **
1318 **	Returns:
1319 **		TRUE if the lock was acquired.
1320 **		FALSE otherwise.
1321 */
1322 
1323 bool
1324 lockfile(fd, filename, type)
1325 	int fd;
1326 	char *filename;
1327 	int type;
1328 {
1329 # ifdef LOCKF
1330 	int action;
1331 	struct flock lfd;
1332 
1333 	if (bitset(LOCK_UN, type))
1334 		lfd.l_type = F_UNLCK;
1335 	else if (bitset(LOCK_EX, type))
1336 		lfd.l_type = F_WRLCK;
1337 	else
1338 		lfd.l_type = F_RDLCK;
1339 
1340 	if (bitset(LOCK_NB, type))
1341 		action = F_SETLK;
1342 	else
1343 		action = F_SETLKW;
1344 
1345 	lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1346 
1347 	if (fcntl(fd, action, &lfd) >= 0)
1348 		return TRUE;
1349 
1350 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1351 		syserr("cannot lockf(%s, %o)", filename, type);
1352 # else
1353 	if (flock(fd, type) >= 0)
1354 		return TRUE;
1355 
1356 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1357 		syserr("cannot flock(%s, %o)", filename, type);
1358 # endif
1359 	return FALSE;
1360 }
1361