xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64718)
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.41 (Berkeley) 10/15/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 <pwd.h>
18 
19 /*
20 **  CONF.C -- Sendmail Configuration Tables.
21 **
22 **	Defines the configuration of this installation.
23 **
24 **	Configuration Variables:
25 **		HdrInfo -- a table describing well-known header fields.
26 **			Each entry has the field name and some flags,
27 **			which are described in sendmail.h.
28 **
29 **	Notes:
30 **		I have tried to put almost all the reasonable
31 **		configuration information into the configuration
32 **		file read at runtime.  My intent is that anything
33 **		here is a function of the version of UNIX you
34 **		are running, or is really static -- for example
35 **		the headers are a superset of widely used
36 **		protocols.  If you find yourself playing with
37 **		this file too much, you may be making a mistake!
38 */
39 
40 
41 
42 
43 /*
44 **  Header info table
45 **	Final (null) entry contains the flags used for any other field.
46 **
47 **	Not all of these are actually handled specially by sendmail
48 **	at this time.  They are included as placeholders, to let
49 **	you know that "someday" I intend to have sendmail do
50 **	something with them.
51 */
52 
53 struct hdrinfo	HdrInfo[] =
54 {
55 		/* originator fields, most to least significant  */
56 	"resent-sender",	H_FROM|H_RESENT,
57 	"resent-from",		H_FROM|H_RESENT,
58 	"resent-reply-to",	H_FROM|H_RESENT,
59 	"sender",		H_FROM,
60 	"from",			H_FROM,
61 	"reply-to",		H_FROM,
62 	"full-name",		H_ACHECK,
63 	"return-receipt-to",	H_FROM|H_RECEIPTTO,
64 	"errors-to",		H_FROM|H_ERRORSTO,
65 
66 		/* destination fields */
67 	"to",			H_RCPT,
68 	"resent-to",		H_RCPT|H_RESENT,
69 	"cc",			H_RCPT,
70 	"resent-cc",		H_RCPT|H_RESENT,
71 	"bcc",			H_RCPT|H_ACHECK,
72 	"resent-bcc",		H_RCPT|H_ACHECK|H_RESENT,
73 	"apparently-to",	H_RCPT,
74 
75 		/* message identification and control */
76 	"message-id",		0,
77 	"resent-message-id",	H_RESENT,
78 	"message",		H_EOH,
79 	"text",			H_EOH,
80 
81 		/* date fields */
82 	"date",			0,
83 	"resent-date",		H_RESENT,
84 
85 		/* trace fields */
86 	"received",		H_TRACE|H_FORCE,
87 	"x400-received",	H_TRACE|H_FORCE,
88 	"via",			H_TRACE|H_FORCE,
89 	"mail-from",		H_TRACE|H_FORCE,
90 
91 		/* miscellaneous fields */
92 	"comments",		H_FORCE,
93 	"return-path",		H_FORCE|H_ACHECK,
94 
95 	NULL,			0,
96 };
97 
98 
99 
100 /*
101 **  Location of system files/databases/etc.
102 */
103 
104 char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
105 
106 
107 
108 /*
109 **  Privacy values
110 */
111 
112 struct prival PrivacyValues[] =
113 {
114 	"public",		PRIV_PUBLIC,
115 	"needmailhelo",		PRIV_NEEDMAILHELO,
116 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
117 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
118 	"noexpn",		PRIV_NOEXPN,
119 	"novrfy",		PRIV_NOVRFY,
120 	"restrictmailq",	PRIV_RESTRICTMAILQ,
121 	"restrictqrun",		PRIV_RESTRICTQRUN,
122 	"authwarnings",		PRIV_AUTHWARNINGS,
123 	"goaway",		PRIV_GOAWAY,
124 	NULL,			0,
125 };
126 
127 
128 
129 /*
130 **  Miscellaneous stuff.
131 */
132 
133 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
134 
135 
136 /*
137 **  Following should be config parameters (and probably will be in
138 **  future releases).  In the meantime, setting these is considered
139 **  unsupported, and is intentionally undocumented.
140 */
141 
142 #ifdef BROKENSMTPPEERS
143 bool	BrokenSmtpPeers = TRUE;		/* set if you have broken SMTP peers */
144 #else
145 bool	BrokenSmtpPeers = FALSE;	/* set if you have broken SMTP peers */
146 #endif
147 #ifdef NOLOOPBACKCHECK
148 bool	CheckLoopBack = FALSE;		/* set to check HELO loopback */
149 #else
150 bool	CheckLoopBack = TRUE;		/* set to check HELO loopback */
151 #endif
152 
153 /*
154 **  SETDEFAULTS -- set default values
155 **
156 **	Because of the way freezing is done, these must be initialized
157 **	using direct code.
158 **
159 **	Parameters:
160 **		e -- the default envelope.
161 **
162 **	Returns:
163 **		none.
164 **
165 **	Side Effects:
166 **		Initializes a bunch of global variables to their
167 **		default values.
168 */
169 
170 #define DAYS		* 24 * 60 * 60
171 
172 setdefaults(e)
173 	register ENVELOPE *e;
174 {
175 	SpaceSub = ' ';				/* option B */
176 	QueueLA = 8;				/* option x */
177 	RefuseLA = 12;				/* option X */
178 	WkRecipFact = 30000L;			/* option y */
179 	WkClassFact = 1800L;			/* option z */
180 	WkTimeFact = 90000L;			/* option Z */
181 	QueueFactor = WkRecipFact * 20;		/* option q */
182 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
183 						/* option F */
184 	DefUid = 1;				/* option u */
185 	DefGid = 1;				/* option g */
186 	CheckpointInterval = 10;		/* option C */
187 	MaxHopCount = 25;			/* option h */
188 	e->e_sendmode = SM_FORK;		/* option d */
189 	e->e_errormode = EM_PRINT;		/* option e */
190 	SevenBit = FALSE;			/* option 7 */
191 	MaxMciCache = 1;			/* option k */
192 	MciCacheTimeout = 300;			/* option K */
193 	LogLevel = 9;				/* option L */
194 	settimeouts(NULL);			/* option r */
195 	TimeOuts.to_q_return = 5 DAYS;		/* option T */
196 	TimeOuts.to_q_warning = 0;		/* option T */
197 	PrivacyFlags = 0;			/* option p */
198 	setdefuser();
199 	setupmaps();
200 	setupmailers();
201 }
202 
203 
204 /*
205 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
206 */
207 
208 setdefuser()
209 {
210 	struct passwd *defpwent;
211 	static char defuserbuf[40];
212 
213 	DefUser = defuserbuf;
214 	if ((defpwent = getpwuid(DefUid)) != NULL)
215 		strcpy(defuserbuf, defpwent->pw_name);
216 	else
217 		strcpy(defuserbuf, "nobody");
218 }
219 /*
220 **  HOST_MAP_INIT -- initialize host class structures
221 */
222 
223 bool
224 host_map_init(map, args)
225 	MAP *map;
226 	char *args;
227 {
228 	register char *p = args;
229 
230 	for (;;)
231 	{
232 		while (isascii(*p) && isspace(*p))
233 			p++;
234 		if (*p != '-')
235 			break;
236 		switch (*++p)
237 		{
238 		  case 'a':
239 			map->map_app = ++p;
240 			break;
241 		}
242 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
243 			p++;
244 		if (*p != '\0')
245 			*p++ = '\0';
246 	}
247 	if (map->map_app != NULL)
248 		map->map_app = newstr(map->map_app);
249 	return TRUE;
250 }
251 /*
252 **  SETUPMAILERS -- initialize default mailers
253 */
254 
255 setupmailers()
256 {
257 	char buf[100];
258 
259 	strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u");
260 	makemailer(buf);
261 
262 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE");
263 	makemailer(buf);
264 
265 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
266 	makemailer(buf);
267 }
268 /*
269 **  SETUPMAPS -- set up map classes
270 */
271 
272 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
273 	{ \
274 		extern bool parse __P((MAP *, char *)); \
275 		extern bool open __P((MAP *, int)); \
276 		extern void close __P((MAP *)); \
277 		extern char *lookup __P((MAP *, char *, char **, int *)); \
278 		extern void store __P((MAP *, char *, char *)); \
279 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
280 		s->s_mapclass.map_cname = name; \
281 		s->s_mapclass.map_ext = ext; \
282 		s->s_mapclass.map_cflags = flags; \
283 		s->s_mapclass.map_parse = parse; \
284 		s->s_mapclass.map_open = open; \
285 		s->s_mapclass.map_close = close; \
286 		s->s_mapclass.map_lookup = lookup; \
287 		s->s_mapclass.map_store = store; \
288 	}
289 
290 setupmaps()
291 {
292 	register STAB *s;
293 
294 #ifdef NEWDB
295 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
296 		map_parseargs, hash_map_open, db_map_close,
297 		db_map_lookup, db_map_store);
298 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
299 		map_parseargs, bt_map_open, db_map_close,
300 		db_map_lookup, db_map_store);
301 #endif
302 
303 #ifdef NDBM
304 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
305 		map_parseargs, ndbm_map_open, ndbm_map_close,
306 		ndbm_map_lookup, ndbm_map_store);
307 #endif
308 
309 #ifdef NIS
310 	MAPDEF("nis", NULL, MCF_ALIASOK,
311 		map_parseargs, nis_map_open, nis_map_close,
312 		nis_map_lookup, nis_map_store);
313 #endif
314 
315 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
316 		map_parseargs, stab_map_open, stab_map_close,
317 		stab_map_lookup, stab_map_store);
318 
319 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
320 		map_parseargs, impl_map_open, impl_map_close,
321 		impl_map_lookup, impl_map_store);
322 
323 	/* host DNS lookup */
324 	MAPDEF("host", NULL, 0,
325 		host_map_init, null_map_open, null_map_close,
326 		host_map_lookup, null_map_store);
327 
328 	/* dequote map */
329 	MAPDEF("dequote", NULL, 0,
330 		dequote_init, null_map_open, null_map_close,
331 		dequote_map, null_map_store);
332 
333 #if 0
334 # ifdef USERDB
335 	/* user database */
336 	MAPDEF("udb", ".db", 0,
337 		udb_map_parse, null_map_open, null_map_close,
338 		udb_map_lookup, null_map_store);
339 # endif
340 #endif
341 }
342 
343 #undef MAPDEF
344 /*
345 **  USERNAME -- return the user id of the logged in user.
346 **
347 **	Parameters:
348 **		none.
349 **
350 **	Returns:
351 **		The login name of the logged in user.
352 **
353 **	Side Effects:
354 **		none.
355 **
356 **	Notes:
357 **		The return value is statically allocated.
358 */
359 
360 char *
361 username()
362 {
363 	static char *myname = NULL;
364 	extern char *getlogin();
365 	register struct passwd *pw;
366 
367 	/* cache the result */
368 	if (myname == NULL)
369 	{
370 		myname = getlogin();
371 		if (myname == NULL || myname[0] == '\0')
372 		{
373 			pw = getpwuid(RealUid);
374 			if (pw != NULL)
375 				myname = newstr(pw->pw_name);
376 		}
377 		else
378 		{
379 			uid_t uid = RealUid;
380 
381 			myname = newstr(myname);
382 			if ((pw = getpwnam(myname)) == NULL ||
383 			      (uid != 0 && uid != pw->pw_uid))
384 			{
385 				pw = getpwuid(uid);
386 				if (pw != NULL)
387 					myname = newstr(pw->pw_name);
388 			}
389 		}
390 		if (myname == NULL || myname[0] == '\0')
391 		{
392 			syserr("554 Who are you?");
393 			myname = "postmaster";
394 		}
395 	}
396 
397 	return (myname);
398 }
399 /*
400 **  TTYPATH -- Get the path of the user's tty
401 **
402 **	Returns the pathname of the user's tty.  Returns NULL if
403 **	the user is not logged in or if s/he has write permission
404 **	denied.
405 **
406 **	Parameters:
407 **		none
408 **
409 **	Returns:
410 **		pathname of the user's tty.
411 **		NULL if not logged in or write permission denied.
412 **
413 **	Side Effects:
414 **		none.
415 **
416 **	WARNING:
417 **		Return value is in a local buffer.
418 **
419 **	Called By:
420 **		savemail
421 */
422 
423 char *
424 ttypath()
425 {
426 	struct stat stbuf;
427 	register char *pathn;
428 	extern char *ttyname();
429 	extern char *getlogin();
430 
431 	/* compute the pathname of the controlling tty */
432 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
433 	    (pathn = ttyname(0)) == NULL)
434 	{
435 		errno = 0;
436 		return (NULL);
437 	}
438 
439 	/* see if we have write permission */
440 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
441 	{
442 		errno = 0;
443 		return (NULL);
444 	}
445 
446 	/* see if the user is logged in */
447 	if (getlogin() == NULL)
448 		return (NULL);
449 
450 	/* looks good */
451 	return (pathn);
452 }
453 /*
454 **  CHECKCOMPAT -- check for From and To person compatible.
455 **
456 **	This routine can be supplied on a per-installation basis
457 **	to determine whether a person is allowed to send a message.
458 **	This allows restriction of certain types of internet
459 **	forwarding or registration of users.
460 **
461 **	If the hosts are found to be incompatible, an error
462 **	message should be given using "usrerr" and 0 should
463 **	be returned.
464 **
465 **	'NoReturn' can be set to suppress the return-to-sender
466 **	function; this should be done on huge messages.
467 **
468 **	Parameters:
469 **		to -- the person being sent to.
470 **
471 **	Returns:
472 **		an exit status
473 **
474 **	Side Effects:
475 **		none (unless you include the usrerr stuff)
476 */
477 
478 checkcompat(to, e)
479 	register ADDRESS *to;
480 	register ENVELOPE *e;
481 {
482 # ifdef lint
483 	if (to == NULL)
484 		to++;
485 # endif /* lint */
486 
487 	if (tTd(49, 1))
488 		printf("checkcompat(to=%s, from=%s)\n",
489 			to->q_paddr, e->e_from.q_paddr);
490 
491 # ifdef EXAMPLE_CODE
492 	/* this code is intended as an example only */
493 	register STAB *s;
494 
495 	s = stab("arpa", ST_MAILER, ST_FIND);
496 	if (s != NULL && e->e_from.q_mailer != LocalMailer &&
497 	    to->q_mailer == s->s_mailer)
498 	{
499 		usrerr("553 No ARPA mail through this machine: see your system administration");
500 		/* NoReturn = TRUE; to supress return copy */
501 		return (EX_UNAVAILABLE);
502 	}
503 # endif /* EXAMPLE_CODE */
504 	return (EX_OK);
505 }
506 /*
507 **  SETSIGNAL -- set a signal handler
508 **
509 **	This is essentially old BSD "signal(3)".
510 */
511 
512 sigfunc_t
513 setsignal(sig, handler)
514 	int sig;
515 	sigfunc_t handler;
516 {
517 #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
518 	return signal(sig, handler);
519 #else
520 	struct sigaction n, o;
521 
522 	bzero(&n, sizeof n);
523 	n.sa_handler = handler;
524 	if (sigaction(sig, &n, &o) < 0)
525 		return SIG_ERR;
526 	return o.sa_handler;
527 #endif
528 }
529 /*
530 **  HOLDSIGS -- arrange to hold all signals
531 **
532 **	Parameters:
533 **		none.
534 **
535 **	Returns:
536 **		none.
537 **
538 **	Side Effects:
539 **		Arranges that signals are held.
540 */
541 
542 holdsigs()
543 {
544 }
545 /*
546 **  RLSESIGS -- arrange to release all signals
547 **
548 **	This undoes the effect of holdsigs.
549 **
550 **	Parameters:
551 **		none.
552 **
553 **	Returns:
554 **		none.
555 **
556 **	Side Effects:
557 **		Arranges that signals are released.
558 */
559 
560 rlsesigs()
561 {
562 }
563 /*
564 **  INIT_MD -- do machine dependent initializations
565 **
566 **	Systems that have global modes that should be set should do
567 **	them here rather than in main.
568 */
569 
570 #ifdef _AUX_SOURCE
571 # include	<compat.h>
572 #endif
573 
574 init_md()
575 {
576 #ifdef _AUX_SOURCE
577 	setcompat(getcompat() | COMPAT_BSDPROT);
578 #endif
579 }
580 /*
581 **  GETLA -- get the current load average
582 **
583 **	This code stolen from la.c.
584 **
585 **	Parameters:
586 **		none.
587 **
588 **	Returns:
589 **		The current load average as an integer.
590 **
591 **	Side Effects:
592 **		none.
593 */
594 
595 /* try to guess what style of load average we have */
596 #define LA_ZERO		1	/* always return load average as zero */
597 #define LA_INT		2	/* read kmem for avenrun; interpret as long */
598 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
599 #define LA_SUBR		4	/* call getloadavg */
600 #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
601 #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
602 
603 /* do guesses based on general OS type */
604 #ifndef LA_TYPE
605 # define LA_TYPE	LA_ZERO
606 #endif
607 
608 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
609 
610 #include <nlist.h>
611 
612 #ifndef LA_AVENRUN
613 # ifdef SYSTEM5
614 #  define LA_AVENRUN	"avenrun"
615 # else
616 #  define LA_AVENRUN	"_avenrun"
617 # endif
618 #endif
619 
620 /* _PATH_UNIX should be defined in <paths.h> */
621 #ifndef _PATH_UNIX
622 # if defined(SYSTEM5)
623 #  define _PATH_UNIX	"/unix"
624 # else
625 #  define _PATH_UNIX	"/vmunix"
626 # endif
627 #endif
628 
629 struct	nlist Nl[] =
630 {
631 	{ LA_AVENRUN },
632 #define	X_AVENRUN	0
633 	{ 0 },
634 };
635 
636 #ifndef FSHIFT
637 # if defined(unixpc)
638 #  define FSHIFT	5
639 # endif
640 
641 # if defined(__alpha)
642 #  define FSHIFT	10
643 # endif
644 
645 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
646 #  define FSHIFT	8
647 # endif
648 #endif
649 
650 #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE)
651 #  define FSCALE	(1 << FSHIFT)
652 #endif
653 
654 getla()
655 {
656 	static int kmem = -1;
657 #if LA_TYPE == LA_INT
658 	long avenrun[3];
659 #else
660 # if LA_TYPE == LA_SHORT
661 	short avenrun[3];
662 # else
663 	double avenrun[3];
664 # endif
665 #endif
666 	extern off_t lseek();
667 	extern int errno;
668 
669 	if (kmem < 0)
670 	{
671 		kmem = open("/dev/kmem", 0, 0);
672 		if (kmem < 0)
673 		{
674 			if (tTd(3, 1))
675 				printf("getla: open(/dev/kmem): %s\n",
676 					errstring(errno));
677 			return (-1);
678 		}
679 		(void) fcntl(kmem, F_SETFD, 1);
680 		if (nlist(_PATH_UNIX, Nl) < 0)
681 		{
682 			if (tTd(3, 1))
683 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
684 					errstring(errno));
685 			return (-1);
686 		}
687 		if (Nl[X_AVENRUN].n_value == 0)
688 		{
689 			if (tTd(3, 1))
690 				printf("getla: nlist(%s, %s) ==> 0\n",
691 					_PATH_UNIX, LA_AVENRUN);
692 			return (-1);
693 		}
694 	}
695 	if (tTd(3, 20))
696 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
697 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
698 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
699 	{
700 		/* thank you Ian */
701 		if (tTd(3, 1))
702 			printf("getla: lseek or read: %s\n", errstring(errno));
703 		return (-1);
704 	}
705 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
706 	if (tTd(3, 5))
707 	{
708 		printf("getla: avenrun = %d", avenrun[0]);
709 		if (tTd(3, 15))
710 			printf(", %d, %d", avenrun[1], avenrun[2]);
711 		printf("\n");
712 	}
713 	if (tTd(3, 1))
714 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
715 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
716 #else
717 	if (tTd(3, 5))
718 	{
719 		printf("getla: avenrun = %g", avenrun[0]);
720 		if (tTd(3, 15))
721 			printf(", %g, %g", avenrun[1], avenrun[2]);
722 		printf("\n");
723 	}
724 	if (tTd(3, 1))
725 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
726 	return ((int) (avenrun[0] + 0.5));
727 #endif
728 }
729 
730 #else
731 #if LA_TYPE == LA_SUBR
732 
733 #ifdef DGUX
734 
735 #include <sys/dg_sys_info.h>
736 
737 int getla()
738 {
739 	struct dg_sys_info_load_info load_info;
740 
741 	dg_sys_info((long *)&load_info,
742 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
743 
744 	return((int) (load_info.one_minute + 0.5));
745 }
746 
747 #else
748 
749 getla()
750 {
751 	double avenrun[3];
752 
753 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
754 	{
755 		if (tTd(3, 1))
756 			perror("getla: getloadavg failed:");
757 		return (-1);
758 	}
759 	if (tTd(3, 1))
760 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
761 	return ((int) (avenrun[0] + 0.5));
762 }
763 
764 #endif /* DGUX */
765 #else
766 #if LA_TYPE == LA_MACH
767 
768 /*
769 **  This has been tested on NeXT release 2.1.
770 */
771 
772 #include <mach.h>
773 
774 getla()
775 {
776 	processor_set_t default_set;
777 	kern_return_t error;
778 	unsigned int info_count;
779 	struct processor_set_basic_info info;
780 	host_t host;
781 
782 	error = processor_set_default(host_self(), &default_set);
783 	if (error != KERN_SUCCESS)
784 		return -1;
785 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
786 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
787 			       &host, (processor_set_info_t)&info,
788 			       &info_count) != KERN_SUCCESS)
789 	{
790 		return -1;
791 	}
792 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
793 }
794 
795 
796 #else
797 
798 getla()
799 {
800 	if (tTd(3, 1))
801 		printf("getla: ZERO\n");
802 	return (0);
803 }
804 
805 #endif
806 #endif
807 #endif
808 /*
809 **  SHOULDQUEUE -- should this message be queued or sent?
810 **
811 **	Compares the message cost to the load average to decide.
812 **
813 **	Parameters:
814 **		pri -- the priority of the message in question.
815 **		ctime -- the message creation time.
816 **
817 **	Returns:
818 **		TRUE -- if this message should be queued up for the
819 **			time being.
820 **		FALSE -- if the load is low enough to send this message.
821 **
822 **	Side Effects:
823 **		none.
824 */
825 
826 bool
827 shouldqueue(pri, ctime)
828 	long pri;
829 	time_t ctime;
830 {
831 	if (CurrentLA < QueueLA)
832 		return (FALSE);
833 	if (CurrentLA >= RefuseLA)
834 		return (TRUE);
835 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
836 }
837 /*
838 **  REFUSECONNECTIONS -- decide if connections should be refused
839 **
840 **	Parameters:
841 **		none.
842 **
843 **	Returns:
844 **		TRUE if incoming SMTP connections should be refused
845 **			(for now).
846 **		FALSE if we should accept new work.
847 **
848 **	Side Effects:
849 **		none.
850 */
851 
852 bool
853 refuseconnections()
854 {
855 #ifdef XLA
856 	if (!xla_smtp_ok())
857 		return TRUE;
858 #endif
859 
860 	/* this is probably too simplistic */
861 	return (CurrentLA >= RefuseLA);
862 }
863 /*
864 **  SETPROCTITLE -- set process title for ps
865 **
866 **	Parameters:
867 **		fmt -- a printf style format string.
868 **		a, b, c -- possible parameters to fmt.
869 **
870 **	Returns:
871 **		none.
872 **
873 **	Side Effects:
874 **		Clobbers argv of our main procedure so ps(1) will
875 **		display the title.
876 */
877 
878 #ifdef SETPROCTITLE
879 # ifdef __hpux
880 #  include <sys/pstat.h>
881 # endif
882 # ifdef BSD4_4
883 #  include <machine/vmparam.h>
884 #  include <sys/exec.h>
885 #  ifdef __bsdi__
886 #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
887 #  endif
888 #  ifdef PS_STRINGS
889 #   define SETPROC_STATIC static
890 #  endif
891 # endif
892 # ifndef SETPROC_STATIC
893 #  define SETPROC_STATIC
894 # endif
895 #endif
896 
897 /*VARARGS1*/
898 #ifdef __STDC__
899 setproctitle(char *fmt, ...)
900 #else
901 setproctitle(fmt, va_alist)
902 	char *fmt;
903 	va_dcl
904 #endif
905 {
906 # ifdef SETPROCTITLE
907 	register char *p;
908 	register int i;
909 	SETPROC_STATIC char buf[MAXLINE];
910 	VA_LOCAL_DECL
911 #  ifdef __hpux
912 	union pstun pst;
913 #  endif
914 	extern char **Argv;
915 	extern char *LastArgv;
916 
917 	p = buf;
918 
919 	/* print sendmail: heading for grep */
920 	(void) strcpy(p, "sendmail: ");
921 	p += strlen(p);
922 
923 	/* print the argument string */
924 	VA_START(fmt);
925 	(void) vsprintf(p, fmt, ap);
926 	VA_END;
927 
928 	i = strlen(buf);
929 
930 #  ifdef __hpux
931 	pst.pst_command = buf;
932 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
933 #  else
934 #   ifdef PS_STRINGS
935 	PS_STRINGS->ps_nargvstr = 1;
936 	PS_STRINGS->ps_argvstr = buf;
937 #   else
938 	if (i > LastArgv - Argv[0] - 2)
939 	{
940 		i = LastArgv - Argv[0] - 2;
941 		buf[i] = '\0';
942 	}
943 	(void) strcpy(Argv[0], buf);
944 	p = &Argv[0][i];
945 	while (p < LastArgv)
946 		*p++ = ' ';
947 #   endif
948 #  endif
949 # endif /* SETPROCTITLE */
950 }
951 /*
952 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
953 **
954 **	Parameters:
955 **		none.
956 **
957 **	Returns:
958 **		none.
959 **
960 **	Side Effects:
961 **		Picks up extant zombies.
962 */
963 
964 void
965 reapchild()
966 {
967 # ifdef HASWAITPID
968 	auto int status;
969 	int count;
970 	int pid;
971 
972 	count = 0;
973 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
974 	{
975 		if (count++ > 1000)
976 		{
977 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
978 				pid, status);
979 			break;
980 		}
981 	}
982 # else
983 # ifdef WNOHANG
984 	union wait status;
985 
986 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
987 		continue;
988 # else /* WNOHANG */
989 	auto int status;
990 
991 	while (wait(&status) > 0)
992 		continue;
993 # endif /* WNOHANG */
994 # endif
995 # ifdef SYS5SIGNALS
996 	(void) setsignal(SIGCHLD, reapchild);
997 # endif
998 }
999 /*
1000 **  UNSETENV -- remove a variable from the environment
1001 **
1002 **	Not needed on newer systems.
1003 **
1004 **	Parameters:
1005 **		name -- the string name of the environment variable to be
1006 **			deleted from the current environment.
1007 **
1008 **	Returns:
1009 **		none.
1010 **
1011 **	Globals:
1012 **		environ -- a pointer to the current environment.
1013 **
1014 **	Side Effects:
1015 **		Modifies environ.
1016 */
1017 
1018 #ifndef HASUNSETENV
1019 
1020 void
1021 unsetenv(name)
1022 	char *name;
1023 {
1024 	extern char **environ;
1025 	register char **pp;
1026 	int len = strlen(name);
1027 
1028 	for (pp = environ; *pp != NULL; pp++)
1029 	{
1030 		if (strncmp(name, *pp, len) == 0 &&
1031 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1032 			break;
1033 	}
1034 
1035 	for (; *pp != NULL; pp++)
1036 		*pp = pp[1];
1037 }
1038 
1039 #endif
1040 /*
1041 **  GETDTABLESIZE -- return number of file descriptors
1042 **
1043 **	Only on non-BSD systems
1044 **
1045 **	Parameters:
1046 **		none
1047 **
1048 **	Returns:
1049 **		size of file descriptor table
1050 **
1051 **	Side Effects:
1052 **		none
1053 */
1054 
1055 #ifdef SOLARIS
1056 # include <sys/resource.h>
1057 #endif
1058 
1059 int
1060 getdtsize()
1061 {
1062 #ifdef RLIMIT_NOFILE
1063 	struct rlimit rl;
1064 
1065 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1066 		return rl.rlim_cur;
1067 #endif
1068 
1069 # ifdef HASGETDTABLESIZE
1070 	return getdtablesize();
1071 # else
1072 #  ifdef _SC_OPEN_MAX
1073 	return sysconf(_SC_OPEN_MAX);
1074 #  else
1075 	return NOFILE;
1076 #  endif
1077 # endif
1078 }
1079 /*
1080 **  UNAME -- get the UUCP name of this system.
1081 */
1082 
1083 #ifndef HASUNAME
1084 
1085 int
1086 uname(name)
1087 	struct utsname *name;
1088 {
1089 	FILE *file;
1090 	char *n;
1091 
1092 	name->nodename[0] = '\0';
1093 
1094 	/* try /etc/whoami -- one line with the node name */
1095 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1096 	{
1097 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1098 		(void) fclose(file);
1099 		n = strchr(name->nodename, '\n');
1100 		if (n != NULL)
1101 			*n = '\0';
1102 		if (name->nodename[0] != '\0')
1103 			return (0);
1104 	}
1105 
1106 	/* try /usr/include/whoami.h -- has a #define somewhere */
1107 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1108 	{
1109 		char buf[MAXLINE];
1110 
1111 		while (fgets(buf, MAXLINE, file) != NULL)
1112 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1113 					NODE_LENGTH, name->nodename) > 0)
1114 				break;
1115 		(void) fclose(file);
1116 		if (name->nodename[0] != '\0')
1117 			return (0);
1118 	}
1119 
1120 #ifdef TRUST_POPEN
1121 	/*
1122 	**  Popen is known to have security holes.
1123 	*/
1124 
1125 	/* try uuname -l to return local name */
1126 	if ((file = popen("uuname -l", "r")) != NULL)
1127 	{
1128 		(void) fgets(name, NODE_LENGTH + 1, file);
1129 		(void) pclose(file);
1130 		n = strchr(name, '\n');
1131 		if (n != NULL)
1132 			*n = '\0';
1133 		if (name->nodename[0] != '\0')
1134 			return (0);
1135 	}
1136 #endif
1137 
1138 	return (-1);
1139 }
1140 #endif /* HASUNAME */
1141 /*
1142 **  INITGROUPS -- initialize groups
1143 **
1144 **	Stub implementation for System V style systems
1145 */
1146 
1147 #ifndef HASINITGROUPS
1148 
1149 initgroups(name, basegid)
1150 	char *name;
1151 	int basegid;
1152 {
1153 	return 0;
1154 }
1155 
1156 #endif
1157 /*
1158 **  SETSID -- set session id (for non-POSIX systems)
1159 */
1160 
1161 #ifndef HASSETSID
1162 
1163 pid_t
1164 setsid __P ((void))
1165 {
1166 #ifdef TIOCNOTTY
1167 	int fd;
1168 
1169 	fd = open("/dev/tty", 2);
1170 	if (fd >= 0)
1171 	{
1172 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1173 		(void) close(fd);
1174 	}
1175 #endif /* TIOCNOTTY */
1176 # ifdef SYS5SETPGRP
1177 	return setpgrp();
1178 # else
1179 	return setpgid(0, getpid());
1180 # endif
1181 }
1182 
1183 #endif
1184 /*
1185 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1186 **
1187 **	Data General DG/UX version of inet_addr returns a struct in_addr
1188 **	instead of a long.  This patches things.
1189 */
1190 
1191 #ifdef DGUX
1192 
1193 #undef inet_addr
1194 
1195 long
1196 dgux_inet_addr(host)
1197 	char *host;
1198 {
1199 	struct in_addr haddr;
1200 
1201 	haddr = inet_addr(host);
1202 	return haddr.s_addr;
1203 }
1204 
1205 #endif
1206 /*
1207 **  GETOPT -- for old systems or systems with bogus implementations
1208 */
1209 
1210 #ifdef NEEDGETOPT
1211 
1212 /*
1213  * Copyright (c) 1985 Regents of the University of California.
1214  * All rights reserved.  The Berkeley software License Agreement
1215  * specifies the terms and conditions for redistribution.
1216  */
1217 
1218 
1219 /*
1220 ** this version hacked to add `atend' flag to allow state machine
1221 ** to reset if invoked by the program to scan args for a 2nd time
1222 */
1223 
1224 #if defined(LIBC_SCCS) && !defined(lint)
1225 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1226 #endif /* LIBC_SCCS and not lint */
1227 
1228 #include <stdio.h>
1229 
1230 /*
1231  * get option letter from argument vector
1232  */
1233 int	opterr = 1,		/* if error message should be printed */
1234 	optind = 1,		/* index into parent argv vector */
1235 	optopt;			/* character checked for validity */
1236 char	*optarg;		/* argument associated with option */
1237 
1238 #define BADCH	(int)'?'
1239 #define EMSG	""
1240 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1241 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1242 
1243 getopt(nargc,nargv,ostr)
1244 int	nargc;
1245 char	**nargv,
1246 	*ostr;
1247 {
1248 	static char	*place = EMSG;	/* option letter processing */
1249 	static char	atend = 0;
1250 	register char	*oli;		/* option letter list index */
1251 
1252 	if (atend) {
1253 		atend = 0;
1254 		place = EMSG;
1255 	}
1256 	if(!*place) {			/* update scanning pointer */
1257 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1258 			atend++;
1259 			return(EOF);
1260 		}
1261 		if (*place == '-') {	/* found "--" */
1262 			++optind;
1263 			atend++;
1264 			return(EOF);
1265 		}
1266 	}				/* option letter okay? */
1267 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1268 		if (!*place) ++optind;
1269 		tell(": illegal option -- ");
1270 	}
1271 	if (*++oli != ':') {		/* don't need argument */
1272 		optarg = NULL;
1273 		if (!*place) ++optind;
1274 	}
1275 	else {				/* need an argument */
1276 		if (*place) optarg = place;	/* no white space */
1277 		else if (nargc <= ++optind) {	/* no arg */
1278 			place = EMSG;
1279 			tell(": option requires an argument -- ");
1280 		}
1281 	 	else optarg = nargv[optind];	/* white space */
1282 		place = EMSG;
1283 		++optind;
1284 	}
1285 	return(optopt);			/* dump back option letter */
1286 }
1287 
1288 #endif
1289 /*
1290 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1291 */
1292 
1293 #ifdef NEEDVPRINTF
1294 
1295 #define MAXARG	16
1296 
1297 vfprintf(fp, fmt, ap)
1298 	FILE *	fp;
1299 	char *	fmt;
1300 	char **	ap;
1301 {
1302 	char *	bp[MAXARG];
1303 	int	i = 0;
1304 
1305 	while (*ap && i < MAXARG)
1306 		bp[i++] = *ap++;
1307 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1308 			 bp[4], bp[5], bp[6], bp[7],
1309 			 bp[8], bp[9], bp[10], bp[11],
1310 			 bp[12], bp[13], bp[14], bp[15]);
1311 }
1312 
1313 vsprintf(s, fmt, ap)
1314 	char *	s;
1315 	char *	fmt;
1316 	char **	ap;
1317 {
1318 	char *	bp[MAXARG];
1319 	int	i = 0;
1320 
1321 	while (*ap && i < MAXARG)
1322 		bp[i++] = *ap++;
1323 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1324 			bp[4], bp[5], bp[6], bp[7],
1325 			bp[8], bp[9], bp[10], bp[11],
1326 			bp[12], bp[13], bp[14], bp[15]);
1327 }
1328 
1329 #endif
1330 /*
1331 **  FREESPACE -- see how much free space is on the queue filesystem
1332 **
1333 **	Only implemented if you have statfs.
1334 **
1335 **	Parameters:
1336 **		dir -- the directory in question.
1337 **		bsize -- a variable into which the filesystem
1338 **			block size is stored.
1339 **
1340 **	Returns:
1341 **		The number of bytes free on the queue filesystem.
1342 **		-1 if the statfs call fails.
1343 **
1344 **	Side effects:
1345 **		Puts the filesystem block size into bsize.
1346 */
1347 
1348 #ifdef HASSTATFS
1349 # undef HASUSTAT
1350 #endif
1351 
1352 #if defined(HASUSTAT)
1353 # include <ustat.h>
1354 #endif
1355 
1356 #ifdef HASSTATFS
1357 # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX)
1358 #  include <sys/statfs.h>
1359 # else
1360 #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
1361 #   include <sys/vfs.h>
1362 #  else
1363 #   include <sys/mount.h>
1364 #  endif
1365 # endif
1366 #endif
1367 
1368 long
1369 freespace(dir, bsize)
1370 	char *dir;
1371 	long *bsize;
1372 {
1373 #if defined(HASSTATFS) || defined(HASUSTAT)
1374 # if defined(HASUSTAT)
1375 	struct ustat fs;
1376 	struct stat statbuf;
1377 #  define FSBLOCKSIZE	DEV_BSIZE
1378 #  define f_bavail	f_tfree
1379 # else
1380 #  if defined(ultrix)
1381 	struct fs_data fs;
1382 #   define f_bavail	fd_bfreen
1383 #   define FSBLOCKSIZE	fs.fd_bsize
1384 #  else
1385 	struct statfs fs;
1386 #   define FSBLOCKSIZE	fs.f_bsize
1387 #   if defined(_SCO_unix_) || defined(IRIX)
1388 #    define f_bavail f_bfree
1389 #   endif
1390 #  endif
1391 # endif
1392 	extern int errno;
1393 
1394 # if defined(HASUSTAT)
1395 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1396 # else
1397 #  if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX)
1398 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
1399 #  else
1400 #   if defined(ultrix)
1401 	if (statfs(dir, &fs) > 0)
1402 #   else
1403 	if (statfs(dir, &fs) == 0)
1404 #   endif
1405 #  endif
1406 # endif
1407 	{
1408 		if (bsize != NULL)
1409 			*bsize = FSBLOCKSIZE;
1410 		return (fs.f_bavail);
1411 	}
1412 #endif
1413 	return (-1);
1414 }
1415 /*
1416 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1417 **
1418 **	Only implemented if you have statfs.
1419 **
1420 **	Parameters:
1421 **		msize -- the size to check against.  If zero, we don't yet
1422 **		know how big the message will be, so just check for
1423 **		a "reasonable" amount.
1424 **
1425 **	Returns:
1426 **		TRUE if there is enough space.
1427 **		FALSE otherwise.
1428 */
1429 
1430 bool
1431 enoughspace(msize)
1432 	long msize;
1433 {
1434 	long bfree, bsize;
1435 
1436 	if (MinBlocksFree <= 0 && msize <= 0)
1437 	{
1438 		if (tTd(4, 80))
1439 			printf("enoughspace: no threshold\n");
1440 		return TRUE;
1441 	}
1442 
1443 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
1444 	{
1445 		if (tTd(4, 80))
1446 			printf("enoughspace: bavail=%ld, need=%ld\n",
1447 				bfree, msize);
1448 
1449 		/* convert msize to block count */
1450 		msize = msize / bsize + 1;
1451 		if (MinBlocksFree >= 0)
1452 			msize += MinBlocksFree;
1453 
1454 		if (bfree < msize)
1455 		{
1456 #ifdef LOG
1457 			if (LogLevel > 0)
1458 				syslog(LOG_ALERT,
1459 					"%s: low on space (have %ld, %s needs %ld in %s)",
1460 					CurEnv->e_id, bfree,
1461 					CurHostName, msize, QueueDir);
1462 #endif
1463 			return FALSE;
1464 		}
1465 	}
1466 	else if (tTd(4, 80))
1467 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1468 			MinBlocksFree, msize, errstring(errno));
1469 	return TRUE;
1470 }
1471 /*
1472 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1473 **
1474 **	This looks at an errno value and tells if this is likely to
1475 **	go away if retried later.
1476 **
1477 **	Parameters:
1478 **		err -- the errno code to classify.
1479 **
1480 **	Returns:
1481 **		TRUE if this is probably transient.
1482 **		FALSE otherwise.
1483 */
1484 
1485 bool
1486 transienterror(err)
1487 	int err;
1488 {
1489 	switch (err)
1490 	{
1491 	  case EIO:			/* I/O error */
1492 	  case ENXIO:			/* Device not configured */
1493 	  case EAGAIN:			/* Resource temporarily unavailable */
1494 	  case ENOMEM:			/* Cannot allocate memory */
1495 	  case ENODEV:			/* Operation not supported by device */
1496 	  case ENFILE:			/* Too many open files in system */
1497 	  case EMFILE:			/* Too many open files */
1498 	  case ENOSPC:			/* No space left on device */
1499 #ifdef ETIMEDOUT
1500 	  case ETIMEDOUT:		/* Connection timed out */
1501 #endif
1502 #ifdef ESTALE
1503 	  case ESTALE:			/* Stale NFS file handle */
1504 #endif
1505 #ifdef ENETDOWN
1506 	  case ENETDOWN:		/* Network is down */
1507 #endif
1508 #ifdef ENETUNREACH
1509 	  case ENETUNREACH:		/* Network is unreachable */
1510 #endif
1511 #ifdef ENETRESET
1512 	  case ENETRESET:		/* Network dropped connection on reset */
1513 #endif
1514 #ifdef ECONNABORTED
1515 	  case ECONNABORTED:		/* Software caused connection abort */
1516 #endif
1517 #ifdef ECONNRESET
1518 	  case ECONNRESET:		/* Connection reset by peer */
1519 #endif
1520 #ifdef ENOBUFS
1521 	  case ENOBUFS:			/* No buffer space available */
1522 #endif
1523 #ifdef ESHUTDOWN
1524 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1525 #endif
1526 #ifdef ECONNREFUSED
1527 	  case ECONNREFUSED:		/* Connection refused */
1528 #endif
1529 #ifdef EHOSTDOWN
1530 	  case EHOSTDOWN:		/* Host is down */
1531 #endif
1532 #ifdef EHOSTUNREACH
1533 	  case EHOSTUNREACH:		/* No route to host */
1534 #endif
1535 #ifdef EDQUOT
1536 	  case EDQUOT:			/* Disc quota exceeded */
1537 #endif
1538 #ifdef EPROCLIM
1539 	  case EPROCLIM:		/* Too many processes */
1540 #endif
1541 #ifdef EUSERS
1542 	  case EUSERS:			/* Too many users */
1543 #endif
1544 #ifdef EDEADLK
1545 	  case EDEADLK:			/* Resource deadlock avoided */
1546 #endif
1547 #ifdef EISCONN
1548 	  case EISCONN:			/* Socket already connected */
1549 #endif
1550 #ifdef EINPROGRESS
1551 	  case EINPROGRESS:		/* Operation now in progress */
1552 #endif
1553 #ifdef EALREADY
1554 	  case EALREADY:		/* Operation already in progress */
1555 #endif
1556 #ifdef EADDRINUSE
1557 	  case EADDRINUSE:		/* Address already in use */
1558 #endif
1559 #ifdef EADDRNOTAVAIL
1560 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1561 #endif
1562 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1563 	  case ENOSR:			/* Out of streams resources */
1564 #endif
1565 		return TRUE;
1566 	}
1567 
1568 	/* nope, must be permanent */
1569 	return FALSE;
1570 }
1571 /*
1572 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1573 **
1574 **	Parameters:
1575 **		fd -- the file descriptor of the file.
1576 **		filename -- the file name (for error messages).
1577 **		ext -- the filename extension.
1578 **		type -- type of the lock.  Bits can be:
1579 **			LOCK_EX -- exclusive lock.
1580 **			LOCK_NB -- non-blocking.
1581 **
1582 **	Returns:
1583 **		TRUE if the lock was acquired.
1584 **		FALSE otherwise.
1585 */
1586 
1587 bool
1588 lockfile(fd, filename, ext, type)
1589 	int fd;
1590 	char *filename;
1591 	char *ext;
1592 	int type;
1593 {
1594 # ifndef HASFLOCK
1595 	int action;
1596 	struct flock lfd;
1597 
1598 	if (ext == NULL)
1599 		ext = "";
1600 
1601 	bzero(&lfd, sizeof lfd);
1602 	if (bitset(LOCK_UN, type))
1603 		lfd.l_type = F_UNLCK;
1604 	else if (bitset(LOCK_EX, type))
1605 		lfd.l_type = F_WRLCK;
1606 	else
1607 		lfd.l_type = F_RDLCK;
1608 
1609 	if (bitset(LOCK_NB, type))
1610 		action = F_SETLK;
1611 	else
1612 		action = F_SETLKW;
1613 
1614 	if (tTd(55, 60))
1615 		printf("lockfile(%s%s, action=%d, type=%d): ",
1616 			filename, ext, action, lfd.l_type);
1617 
1618 	if (fcntl(fd, action, &lfd) >= 0)
1619 	{
1620 		if (tTd(55, 60))
1621 			printf("SUCCESS\n");
1622 		return TRUE;
1623 	}
1624 
1625 	if (tTd(55, 60))
1626 		printf("(%s) ", errstring(errno));
1627 
1628 	/*
1629 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
1630 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
1631 	**  as type "tmp" (that is, served from swap space), the
1632 	**  previous fcntl will fail with "Invalid argument" errors.
1633 	**  Since this is fairly common during testing, we will assume
1634 	**  that this indicates that the lock is successfully grabbed.
1635 	*/
1636 
1637 	if (errno == EINVAL)
1638 	{
1639 		if (tTd(55, 60))
1640 			printf("SUCCESS\n");
1641 		return TRUE;
1642 	}
1643 
1644 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1645 	{
1646 		int omode = -1;
1647 #  ifdef F_GETFL
1648 		int oerrno = errno;
1649 
1650 		(void) fcntl(fd, F_GETFL, &omode);
1651 		errno = oerrno;
1652 #  endif
1653 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1654 			filename, ext, fd, type, omode, geteuid());
1655 	}
1656 # else
1657 	if (ext == NULL)
1658 		ext = "";
1659 
1660 	if (tTd(55, 60))
1661 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
1662 
1663 	if (flock(fd, type) >= 0)
1664 	{
1665 		if (tTd(55, 60))
1666 			printf("SUCCESS\n");
1667 		return TRUE;
1668 	}
1669 
1670 	if (tTd(55, 60))
1671 		printf("(%s) ", errstring(errno));
1672 
1673 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1674 	{
1675 		int omode = -1;
1676 #  ifdef F_GETFL
1677 		int oerrno = errno;
1678 
1679 		(void) fcntl(fd, F_GETFL, &omode);
1680 		errno = oerrno;
1681 #  endif
1682 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1683 			filename, ext, fd, type, omode, geteuid());
1684 	}
1685 # endif
1686 	if (tTd(55, 60))
1687 		printf("FAIL\n");
1688 	return FALSE;
1689 }
1690 /*
1691 **  GETCFNAME -- return the name of the .cf file.
1692 **
1693 **	Some systems (e.g., NeXT) determine this dynamically.
1694 */
1695 
1696 char *
1697 getcfname()
1698 {
1699 	if (ConfFile != NULL)
1700 		return ConfFile;
1701 	return _PATH_SENDMAILCF;
1702 }
1703 /*
1704 **  SETVENDOR -- process vendor code from V configuration line
1705 **
1706 **	Parameters:
1707 **		vendor -- string representation of vendor.
1708 **
1709 **	Returns:
1710 **		TRUE -- if ok.
1711 **		FALSE -- if vendor code could not be processed.
1712 */
1713 
1714 bool
1715 setvendor(vendor)
1716 	char *vendor;
1717 {
1718 	return (strcasecmp(vendor, "Berkeley") == 0);
1719 }
1720