xref: /csrg-svn/usr.sbin/sendmail/src/conf.c (revision 64705)
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.40 (Berkeley) 10/08/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 getla()
734 {
735 	double avenrun[3];
736 
737 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
738 	{
739 		if (tTd(3, 1))
740 			perror("getla: getloadavg failed:");
741 		return (-1);
742 	}
743 	if (tTd(3, 1))
744 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
745 	return ((int) (avenrun[0] + 0.5));
746 }
747 
748 #else
749 #if LA_TYPE == LA_MACH
750 
751 /*
752 **  This has been tested on NeXT release 2.1.
753 */
754 
755 #include <mach.h>
756 
757 getla()
758 {
759 	processor_set_t default_set;
760 	kern_return_t error;
761 	unsigned int info_count;
762 	struct processor_set_basic_info info;
763 	host_t host;
764 
765 	error = processor_set_default(host_self(), &default_set);
766 	if (error != KERN_SUCCESS)
767 		return -1;
768 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
769 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
770 			       &host, (processor_set_info_t)&info,
771 			       &info_count) != KERN_SUCCESS)
772 	{
773 		return -1;
774 	}
775 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
776 }
777 
778 
779 #else
780 
781 getla()
782 {
783 	if (tTd(3, 1))
784 		printf("getla: ZERO\n");
785 	return (0);
786 }
787 
788 #endif
789 #endif
790 #endif
791 /*
792 **  SHOULDQUEUE -- should this message be queued or sent?
793 **
794 **	Compares the message cost to the load average to decide.
795 **
796 **	Parameters:
797 **		pri -- the priority of the message in question.
798 **		ctime -- the message creation time.
799 **
800 **	Returns:
801 **		TRUE -- if this message should be queued up for the
802 **			time being.
803 **		FALSE -- if the load is low enough to send this message.
804 **
805 **	Side Effects:
806 **		none.
807 */
808 
809 bool
810 shouldqueue(pri, ctime)
811 	long pri;
812 	time_t ctime;
813 {
814 	if (CurrentLA < QueueLA)
815 		return (FALSE);
816 	if (CurrentLA >= RefuseLA)
817 		return (TRUE);
818 	return (pri > (QueueFactor / (CurrentLA - QueueLA + 1)));
819 }
820 /*
821 **  REFUSECONNECTIONS -- decide if connections should be refused
822 **
823 **	Parameters:
824 **		none.
825 **
826 **	Returns:
827 **		TRUE if incoming SMTP connections should be refused
828 **			(for now).
829 **		FALSE if we should accept new work.
830 **
831 **	Side Effects:
832 **		none.
833 */
834 
835 bool
836 refuseconnections()
837 {
838 #ifdef XLA
839 	if (!xla_smtp_ok())
840 		return TRUE;
841 #endif
842 
843 	/* this is probably too simplistic */
844 	return (CurrentLA >= RefuseLA);
845 }
846 /*
847 **  SETPROCTITLE -- set process title for ps
848 **
849 **	Parameters:
850 **		fmt -- a printf style format string.
851 **		a, b, c -- possible parameters to fmt.
852 **
853 **	Returns:
854 **		none.
855 **
856 **	Side Effects:
857 **		Clobbers argv of our main procedure so ps(1) will
858 **		display the title.
859 */
860 
861 #ifdef SETPROCTITLE
862 # ifdef __hpux
863 #  include <sys/pstat.h>
864 # endif
865 # ifdef BSD4_4
866 #  include <machine/vmparam.h>
867 #  include <sys/exec.h>
868 #  ifdef __bsdi__
869 #   undef PS_STRINGS	/* BSDI 1.0 doesn't do PS_STRINGS as we expect */
870 #  endif
871 #  ifdef PS_STRINGS
872 #   define SETPROC_STATIC static
873 #  endif
874 # endif
875 # ifndef SETPROC_STATIC
876 #  define SETPROC_STATIC
877 # endif
878 #endif
879 
880 /*VARARGS1*/
881 #ifdef __STDC__
882 setproctitle(char *fmt, ...)
883 #else
884 setproctitle(fmt, va_alist)
885 	char *fmt;
886 	va_dcl
887 #endif
888 {
889 # ifdef SETPROCTITLE
890 	register char *p;
891 	register int i;
892 	SETPROC_STATIC char buf[MAXLINE];
893 	VA_LOCAL_DECL
894 #  ifdef __hpux
895 	union pstun pst;
896 #  endif
897 	extern char **Argv;
898 	extern char *LastArgv;
899 
900 	p = buf;
901 
902 	/* print sendmail: heading for grep */
903 	(void) strcpy(p, "sendmail: ");
904 	p += strlen(p);
905 
906 	/* print the argument string */
907 	VA_START(fmt);
908 	(void) vsprintf(p, fmt, ap);
909 	VA_END;
910 
911 	i = strlen(buf);
912 
913 #  ifdef __hpux
914 	pst.pst_command = buf;
915 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
916 #  else
917 #   ifdef PS_STRINGS
918 	PS_STRINGS->ps_nargvstr = 1;
919 	PS_STRINGS->ps_argvstr = buf;
920 #   else
921 	if (i > LastArgv - Argv[0] - 2)
922 	{
923 		i = LastArgv - Argv[0] - 2;
924 		buf[i] = '\0';
925 	}
926 	(void) strcpy(Argv[0], buf);
927 	p = &Argv[0][i];
928 	while (p < LastArgv)
929 		*p++ = ' ';
930 #   endif
931 #  endif
932 # endif /* SETPROCTITLE */
933 }
934 /*
935 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
936 **
937 **	Parameters:
938 **		none.
939 **
940 **	Returns:
941 **		none.
942 **
943 **	Side Effects:
944 **		Picks up extant zombies.
945 */
946 
947 void
948 reapchild()
949 {
950 # ifdef HASWAITPID
951 	auto int status;
952 	int count;
953 	int pid;
954 
955 	count = 0;
956 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
957 	{
958 		if (count++ > 1000)
959 		{
960 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
961 				pid, status);
962 			break;
963 		}
964 	}
965 # else
966 # ifdef WNOHANG
967 	union wait status;
968 
969 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
970 		continue;
971 # else /* WNOHANG */
972 	auto int status;
973 
974 	while (wait(&status) > 0)
975 		continue;
976 # endif /* WNOHANG */
977 # endif
978 # ifdef SYS5SIGNALS
979 	(void) setsignal(SIGCHLD, reapchild);
980 # endif
981 }
982 /*
983 **  UNSETENV -- remove a variable from the environment
984 **
985 **	Not needed on newer systems.
986 **
987 **	Parameters:
988 **		name -- the string name of the environment variable to be
989 **			deleted from the current environment.
990 **
991 **	Returns:
992 **		none.
993 **
994 **	Globals:
995 **		environ -- a pointer to the current environment.
996 **
997 **	Side Effects:
998 **		Modifies environ.
999 */
1000 
1001 #ifndef HASUNSETENV
1002 
1003 void
1004 unsetenv(name)
1005 	char *name;
1006 {
1007 	extern char **environ;
1008 	register char **pp;
1009 	int len = strlen(name);
1010 
1011 	for (pp = environ; *pp != NULL; pp++)
1012 	{
1013 		if (strncmp(name, *pp, len) == 0 &&
1014 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1015 			break;
1016 	}
1017 
1018 	for (; *pp != NULL; pp++)
1019 		*pp = pp[1];
1020 }
1021 
1022 #endif
1023 /*
1024 **  GETDTABLESIZE -- return number of file descriptors
1025 **
1026 **	Only on non-BSD systems
1027 **
1028 **	Parameters:
1029 **		none
1030 **
1031 **	Returns:
1032 **		size of file descriptor table
1033 **
1034 **	Side Effects:
1035 **		none
1036 */
1037 
1038 #ifdef SOLARIS
1039 # include <sys/resource.h>
1040 #endif
1041 
1042 int
1043 getdtsize()
1044 {
1045 #ifdef RLIMIT_NOFILE
1046 	struct rlimit rl;
1047 
1048 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1049 		return rl.rlim_cur;
1050 #endif
1051 
1052 # ifdef HASGETDTABLESIZE
1053 	return getdtablesize();
1054 # else
1055 #  ifdef _SC_OPEN_MAX
1056 	return sysconf(_SC_OPEN_MAX);
1057 #  else
1058 	return NOFILE;
1059 #  endif
1060 # endif
1061 }
1062 /*
1063 **  UNAME -- get the UUCP name of this system.
1064 */
1065 
1066 #ifndef HASUNAME
1067 
1068 int
1069 uname(name)
1070 	struct utsname *name;
1071 {
1072 	FILE *file;
1073 	char *n;
1074 
1075 	name->nodename[0] = '\0';
1076 
1077 	/* try /etc/whoami -- one line with the node name */
1078 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1079 	{
1080 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1081 		(void) fclose(file);
1082 		n = strchr(name->nodename, '\n');
1083 		if (n != NULL)
1084 			*n = '\0';
1085 		if (name->nodename[0] != '\0')
1086 			return (0);
1087 	}
1088 
1089 	/* try /usr/include/whoami.h -- has a #define somewhere */
1090 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1091 	{
1092 		char buf[MAXLINE];
1093 
1094 		while (fgets(buf, MAXLINE, file) != NULL)
1095 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1096 					NODE_LENGTH, name->nodename) > 0)
1097 				break;
1098 		(void) fclose(file);
1099 		if (name->nodename[0] != '\0')
1100 			return (0);
1101 	}
1102 
1103 #ifdef TRUST_POPEN
1104 	/*
1105 	**  Popen is known to have security holes.
1106 	*/
1107 
1108 	/* try uuname -l to return local name */
1109 	if ((file = popen("uuname -l", "r")) != NULL)
1110 	{
1111 		(void) fgets(name, NODE_LENGTH + 1, file);
1112 		(void) pclose(file);
1113 		n = strchr(name, '\n');
1114 		if (n != NULL)
1115 			*n = '\0';
1116 		if (name->nodename[0] != '\0')
1117 			return (0);
1118 	}
1119 #endif
1120 
1121 	return (-1);
1122 }
1123 #endif /* HASUNAME */
1124 /*
1125 **  INITGROUPS -- initialize groups
1126 **
1127 **	Stub implementation for System V style systems
1128 */
1129 
1130 #ifndef HASINITGROUPS
1131 
1132 initgroups(name, basegid)
1133 	char *name;
1134 	int basegid;
1135 {
1136 	return 0;
1137 }
1138 
1139 #endif
1140 /*
1141 **  SETSID -- set session id (for non-POSIX systems)
1142 */
1143 
1144 #ifndef HASSETSID
1145 
1146 pid_t
1147 setsid __P ((void))
1148 {
1149 #ifdef TIOCNOTTY
1150 	int fd;
1151 
1152 	fd = open("/dev/tty", 2);
1153 	if (fd >= 0)
1154 	{
1155 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1156 		(void) close(fd);
1157 	}
1158 #endif /* TIOCNOTTY */
1159 # ifdef SYS5SETPGRP
1160 	return setpgrp();
1161 # else
1162 	return setpgid(0, getpid());
1163 # endif
1164 }
1165 
1166 #endif
1167 /*
1168 **  GETOPT -- for old systems or systems with bogus implementations
1169 */
1170 
1171 #ifdef NEEDGETOPT
1172 
1173 /*
1174  * Copyright (c) 1985 Regents of the University of California.
1175  * All rights reserved.  The Berkeley software License Agreement
1176  * specifies the terms and conditions for redistribution.
1177  */
1178 
1179 
1180 /*
1181 ** this version hacked to add `atend' flag to allow state machine
1182 ** to reset if invoked by the program to scan args for a 2nd time
1183 */
1184 
1185 #if defined(LIBC_SCCS) && !defined(lint)
1186 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1187 #endif /* LIBC_SCCS and not lint */
1188 
1189 #include <stdio.h>
1190 
1191 /*
1192  * get option letter from argument vector
1193  */
1194 int	opterr = 1,		/* if error message should be printed */
1195 	optind = 1,		/* index into parent argv vector */
1196 	optopt;			/* character checked for validity */
1197 char	*optarg;		/* argument associated with option */
1198 
1199 #define BADCH	(int)'?'
1200 #define EMSG	""
1201 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1202 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1203 
1204 getopt(nargc,nargv,ostr)
1205 int	nargc;
1206 char	**nargv,
1207 	*ostr;
1208 {
1209 	static char	*place = EMSG;	/* option letter processing */
1210 	static char	atend = 0;
1211 	register char	*oli;		/* option letter list index */
1212 
1213 	if (atend) {
1214 		atend = 0;
1215 		place = EMSG;
1216 	}
1217 	if(!*place) {			/* update scanning pointer */
1218 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1219 			atend++;
1220 			return(EOF);
1221 		}
1222 		if (*place == '-') {	/* found "--" */
1223 			++optind;
1224 			atend++;
1225 			return(EOF);
1226 		}
1227 	}				/* option letter okay? */
1228 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1229 		if (!*place) ++optind;
1230 		tell(": illegal option -- ");
1231 	}
1232 	if (*++oli != ':') {		/* don't need argument */
1233 		optarg = NULL;
1234 		if (!*place) ++optind;
1235 	}
1236 	else {				/* need an argument */
1237 		if (*place) optarg = place;	/* no white space */
1238 		else if (nargc <= ++optind) {	/* no arg */
1239 			place = EMSG;
1240 			tell(": option requires an argument -- ");
1241 		}
1242 	 	else optarg = nargv[optind];	/* white space */
1243 		place = EMSG;
1244 		++optind;
1245 	}
1246 	return(optopt);			/* dump back option letter */
1247 }
1248 
1249 #endif
1250 /*
1251 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1252 */
1253 
1254 #ifdef NEEDVPRINTF
1255 
1256 #define MAXARG	16
1257 
1258 vfprintf(fp, fmt, ap)
1259 	FILE *	fp;
1260 	char *	fmt;
1261 	char **	ap;
1262 {
1263 	char *	bp[MAXARG];
1264 	int	i = 0;
1265 
1266 	while (*ap && i < MAXARG)
1267 		bp[i++] = *ap++;
1268 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1269 			 bp[4], bp[5], bp[6], bp[7],
1270 			 bp[8], bp[9], bp[10], bp[11],
1271 			 bp[12], bp[13], bp[14], bp[15]);
1272 }
1273 
1274 vsprintf(s, fmt, ap)
1275 	char *	s;
1276 	char *	fmt;
1277 	char **	ap;
1278 {
1279 	char *	bp[MAXARG];
1280 	int	i = 0;
1281 
1282 	while (*ap && i < MAXARG)
1283 		bp[i++] = *ap++;
1284 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1285 			bp[4], bp[5], bp[6], bp[7],
1286 			bp[8], bp[9], bp[10], bp[11],
1287 			bp[12], bp[13], bp[14], bp[15]);
1288 }
1289 
1290 #endif
1291 /*
1292 **  FREESPACE -- see how much free space is on the queue filesystem
1293 **
1294 **	Only implemented if you have statfs.
1295 **
1296 **	Parameters:
1297 **		dir -- the directory in question.
1298 **		bsize -- a variable into which the filesystem
1299 **			block size is stored.
1300 **
1301 **	Returns:
1302 **		The number of bytes free on the queue filesystem.
1303 **		-1 if the statfs call fails.
1304 **
1305 **	Side effects:
1306 **		Puts the filesystem block size into bsize.
1307 */
1308 
1309 #ifdef HASSTATFS
1310 # undef HASUSTAT
1311 #endif
1312 
1313 #if defined(HASUSTAT)
1314 # include <ustat.h>
1315 #endif
1316 
1317 #ifdef HASSTATFS
1318 # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV)
1319 #  include <sys/statfs.h>
1320 # else
1321 #  if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE)
1322 #   include <sys/vfs.h>
1323 #  else
1324 #   include <sys/mount.h>
1325 #  endif
1326 # endif
1327 #endif
1328 
1329 long
1330 freespace(dir, bsize)
1331 	char *dir;
1332 	long *bsize;
1333 {
1334 #if defined(HASSTATFS) || defined(HASUSTAT)
1335 # if defined(HASUSTAT)
1336 	struct ustat fs;
1337 	struct stat statbuf;
1338 #  define FSBLOCKSIZE	DEV_BSIZE
1339 #  define f_bavail	f_tfree
1340 # else
1341 #  if defined(ultrix)
1342 	struct fs_data fs;
1343 #   define f_bavail	fd_bfreen
1344 #   define FSBLOCKSIZE	fs.fd_bsize
1345 #  else
1346 	struct statfs fs;
1347 #   define FSBLOCKSIZE	fs.f_bsize
1348 #   if defined(_SCO_unix_) || defined(IRIX)
1349 #    define f_bavail f_bfree
1350 #   endif
1351 #  endif
1352 # endif
1353 	extern int errno;
1354 
1355 # if defined(HASUSTAT)
1356 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
1357 # else
1358 #  if defined(IRIX) || defined(apollo) || defined(UMAXV)
1359 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
1360 #  else
1361 #   if defined(ultrix)
1362 	if (statfs(dir, &fs) > 0)
1363 #   else
1364 	if (statfs(dir, &fs) == 0)
1365 #   endif
1366 #  endif
1367 # endif
1368 	{
1369 		if (bsize != NULL)
1370 			*bsize = FSBLOCKSIZE;
1371 		return (fs.f_bavail);
1372 	}
1373 #endif
1374 	return (-1);
1375 }
1376 /*
1377 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
1378 **
1379 **	Only implemented if you have statfs.
1380 **
1381 **	Parameters:
1382 **		msize -- the size to check against.  If zero, we don't yet
1383 **		know how big the message will be, so just check for
1384 **		a "reasonable" amount.
1385 **
1386 **	Returns:
1387 **		TRUE if there is enough space.
1388 **		FALSE otherwise.
1389 */
1390 
1391 bool
1392 enoughspace(msize)
1393 	long msize;
1394 {
1395 	long bfree, bsize;
1396 
1397 	if (MinBlocksFree <= 0 && msize <= 0)
1398 	{
1399 		if (tTd(4, 80))
1400 			printf("enoughspace: no threshold\n");
1401 		return TRUE;
1402 	}
1403 
1404 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
1405 	{
1406 		if (tTd(4, 80))
1407 			printf("enoughspace: bavail=%ld, need=%ld\n",
1408 				bfree, msize);
1409 
1410 		/* convert msize to block count */
1411 		msize = msize / bsize + 1;
1412 		if (MinBlocksFree >= 0)
1413 			msize += MinBlocksFree;
1414 
1415 		if (bfree < msize)
1416 		{
1417 #ifdef LOG
1418 			if (LogLevel > 0)
1419 				syslog(LOG_ALERT,
1420 					"%s: low on space (have %ld, %s needs %ld in %s)",
1421 					CurEnv->e_id, bfree,
1422 					CurHostName, msize, QueueDir);
1423 #endif
1424 			return FALSE;
1425 		}
1426 	}
1427 	else if (tTd(4, 80))
1428 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
1429 			MinBlocksFree, msize, errstring(errno));
1430 	return TRUE;
1431 }
1432 /*
1433 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
1434 **
1435 **	This looks at an errno value and tells if this is likely to
1436 **	go away if retried later.
1437 **
1438 **	Parameters:
1439 **		err -- the errno code to classify.
1440 **
1441 **	Returns:
1442 **		TRUE if this is probably transient.
1443 **		FALSE otherwise.
1444 */
1445 
1446 bool
1447 transienterror(err)
1448 	int err;
1449 {
1450 	switch (err)
1451 	{
1452 	  case EIO:			/* I/O error */
1453 	  case ENXIO:			/* Device not configured */
1454 	  case EAGAIN:			/* Resource temporarily unavailable */
1455 	  case ENOMEM:			/* Cannot allocate memory */
1456 	  case ENODEV:			/* Operation not supported by device */
1457 	  case ENFILE:			/* Too many open files in system */
1458 	  case EMFILE:			/* Too many open files */
1459 	  case ENOSPC:			/* No space left on device */
1460 #ifdef ETIMEDOUT
1461 	  case ETIMEDOUT:		/* Connection timed out */
1462 #endif
1463 #ifdef ESTALE
1464 	  case ESTALE:			/* Stale NFS file handle */
1465 #endif
1466 #ifdef ENETDOWN
1467 	  case ENETDOWN:		/* Network is down */
1468 #endif
1469 #ifdef ENETUNREACH
1470 	  case ENETUNREACH:		/* Network is unreachable */
1471 #endif
1472 #ifdef ENETRESET
1473 	  case ENETRESET:		/* Network dropped connection on reset */
1474 #endif
1475 #ifdef ECONNABORTED
1476 	  case ECONNABORTED:		/* Software caused connection abort */
1477 #endif
1478 #ifdef ECONNRESET
1479 	  case ECONNRESET:		/* Connection reset by peer */
1480 #endif
1481 #ifdef ENOBUFS
1482 	  case ENOBUFS:			/* No buffer space available */
1483 #endif
1484 #ifdef ESHUTDOWN
1485 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
1486 #endif
1487 #ifdef ECONNREFUSED
1488 	  case ECONNREFUSED:		/* Connection refused */
1489 #endif
1490 #ifdef EHOSTDOWN
1491 	  case EHOSTDOWN:		/* Host is down */
1492 #endif
1493 #ifdef EHOSTUNREACH
1494 	  case EHOSTUNREACH:		/* No route to host */
1495 #endif
1496 #ifdef EDQUOT
1497 	  case EDQUOT:			/* Disc quota exceeded */
1498 #endif
1499 #ifdef EPROCLIM
1500 	  case EPROCLIM:		/* Too many processes */
1501 #endif
1502 #ifdef EUSERS
1503 	  case EUSERS:			/* Too many users */
1504 #endif
1505 #ifdef EDEADLK
1506 	  case EDEADLK:			/* Resource deadlock avoided */
1507 #endif
1508 #ifdef EISCONN
1509 	  case EISCONN:			/* Socket already connected */
1510 #endif
1511 #ifdef EINPROGRESS
1512 	  case EINPROGRESS:		/* Operation now in progress */
1513 #endif
1514 #ifdef EALREADY
1515 	  case EALREADY:		/* Operation already in progress */
1516 #endif
1517 #ifdef EADDRINUSE
1518 	  case EADDRINUSE:		/* Address already in use */
1519 #endif
1520 #ifdef EADDRNOTAVAIL
1521 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
1522 #endif
1523 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
1524 	  case ENOSR:			/* Out of streams resources */
1525 #endif
1526 		return TRUE;
1527 	}
1528 
1529 	/* nope, must be permanent */
1530 	return FALSE;
1531 }
1532 /*
1533 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
1534 **
1535 **	Parameters:
1536 **		fd -- the file descriptor of the file.
1537 **		filename -- the file name (for error messages).
1538 **		ext -- the filename extension.
1539 **		type -- type of the lock.  Bits can be:
1540 **			LOCK_EX -- exclusive lock.
1541 **			LOCK_NB -- non-blocking.
1542 **
1543 **	Returns:
1544 **		TRUE if the lock was acquired.
1545 **		FALSE otherwise.
1546 */
1547 
1548 bool
1549 lockfile(fd, filename, ext, type)
1550 	int fd;
1551 	char *filename;
1552 	char *ext;
1553 	int type;
1554 {
1555 # ifndef HASFLOCK
1556 	int action;
1557 	struct flock lfd;
1558 
1559 	if (ext == NULL)
1560 		ext = "";
1561 
1562 	bzero(&lfd, sizeof lfd);
1563 	if (bitset(LOCK_UN, type))
1564 		lfd.l_type = F_UNLCK;
1565 	else if (bitset(LOCK_EX, type))
1566 		lfd.l_type = F_WRLCK;
1567 	else
1568 		lfd.l_type = F_RDLCK;
1569 
1570 	if (bitset(LOCK_NB, type))
1571 		action = F_SETLK;
1572 	else
1573 		action = F_SETLKW;
1574 
1575 	if (tTd(55, 60))
1576 		printf("lockfile(%s%s, action=%d, type=%d): ",
1577 			filename, ext, action, lfd.l_type);
1578 
1579 	if (fcntl(fd, action, &lfd) >= 0)
1580 	{
1581 		if (tTd(55, 60))
1582 			printf("SUCCESS\n");
1583 		return TRUE;
1584 	}
1585 
1586 	if (tTd(55, 60))
1587 		printf("(%s) ", errstring(errno));
1588 
1589 	/*
1590 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
1591 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
1592 	**  as type "tmp" (that is, served from swap space), the
1593 	**  previous fcntl will fail with "Invalid argument" errors.
1594 	**  Since this is fairly common during testing, we will assume
1595 	**  that this indicates that the lock is successfully grabbed.
1596 	*/
1597 
1598 	if (errno == EINVAL)
1599 	{
1600 		if (tTd(55, 60))
1601 			printf("SUCCESS\n");
1602 		return TRUE;
1603 	}
1604 
1605 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
1606 	{
1607 		int omode = -1;
1608 #  ifdef F_GETFL
1609 		int oerrno = errno;
1610 
1611 		(void) fcntl(fd, F_GETFL, &omode);
1612 		errno = oerrno;
1613 #  endif
1614 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1615 			filename, ext, fd, type, omode, geteuid());
1616 	}
1617 # else
1618 	if (ext == NULL)
1619 		ext = "";
1620 
1621 	if (tTd(55, 60))
1622 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
1623 
1624 	if (flock(fd, type) >= 0)
1625 	{
1626 		if (tTd(55, 60))
1627 			printf("SUCCESS\n");
1628 		return TRUE;
1629 	}
1630 
1631 	if (tTd(55, 60))
1632 		printf("(%s) ", errstring(errno));
1633 
1634 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
1635 	{
1636 		int omode = -1;
1637 #  ifdef F_GETFL
1638 		int oerrno = errno;
1639 
1640 		(void) fcntl(fd, F_GETFL, &omode);
1641 		errno = oerrno;
1642 #  endif
1643 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
1644 			filename, ext, fd, type, omode, geteuid());
1645 	}
1646 # endif
1647 	if (tTd(55, 60))
1648 		printf("FAIL\n");
1649 	return FALSE;
1650 }
1651 /*
1652 **  GETCFNAME -- return the name of the .cf file.
1653 **
1654 **	Some systems (e.g., NeXT) determine this dynamically.
1655 */
1656 
1657 char *
1658 getcfname()
1659 {
1660 	if (ConfFile != NULL)
1661 		return ConfFile;
1662 	return _PATH_SENDMAILCF;
1663 }
1664