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