xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision 4c0050d5)
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.129 (Berkeley) 02/10/95";
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 <netdb.h>
18 # include <pwd.h>
19 
20 /*
21 **  CONF.C -- Sendmail Configuration Tables.
22 **
23 **	Defines the configuration of this installation.
24 **
25 **	Configuration Variables:
26 **		HdrInfo -- a table describing well-known header fields.
27 **			Each entry has the field name and some flags,
28 **			which are described in sendmail.h.
29 **
30 **	Notes:
31 **		I have tried to put almost all the reasonable
32 **		configuration information into the configuration
33 **		file read at runtime.  My intent is that anything
34 **		here is a function of the version of UNIX you
35 **		are running, or is really static -- for example
36 **		the headers are a superset of widely used
37 **		protocols.  If you find yourself playing with
38 **		this file too much, you may be making a mistake!
39 */
40 
41 
42 
43 
44 /*
45 **  Header info table
46 **	Final (null) entry contains the flags used for any other field.
47 **
48 **	Not all of these are actually handled specially by sendmail
49 **	at this time.  They are included as placeholders, to let
50 **	you know that "someday" I intend to have sendmail do
51 **	something with them.
52 */
53 
54 struct hdrinfo	HdrInfo[] =
55 {
56 		/* originator fields, most to least significant  */
57 	"resent-sender",		H_FROM|H_RESENT,
58 	"resent-from",			H_FROM|H_RESENT,
59 	"resent-reply-to",		H_FROM|H_RESENT,
60 	"sender",			H_FROM,
61 	"from",				H_FROM,
62 	"reply-to",			H_FROM,
63 	"full-name",			H_ACHECK,
64 	"return-receipt-to",		H_FROM|H_RECEIPTTO,
65 	"errors-to",			H_FROM|H_ERRORSTO,
66 
67 		/* destination fields */
68 	"to",				H_RCPT,
69 	"resent-to",			H_RCPT|H_RESENT,
70 	"cc",				H_RCPT,
71 	"resent-cc",			H_RCPT|H_RESENT,
72 	"bcc",				H_RCPT|H_ACHECK,
73 	"resent-bcc",			H_RCPT|H_ACHECK|H_RESENT,
74 	"apparently-to",		H_RCPT,
75 
76 		/* message identification and control */
77 	"message-id",			0,
78 	"resent-message-id",		H_RESENT,
79 	"message",			H_EOH,
80 	"text",				H_EOH,
81 
82 		/* date fields */
83 	"date",				0,
84 	"resent-date",			H_RESENT,
85 
86 		/* trace fields */
87 	"received",			H_TRACE|H_FORCE,
88 	"x400-received",		H_TRACE|H_FORCE,
89 	"via",				H_TRACE|H_FORCE,
90 	"mail-from",			H_TRACE|H_FORCE,
91 
92 		/* miscellaneous fields */
93 	"comments",			H_FORCE,
94 	"return-path",			H_FORCE|H_ACHECK,
95 	"content-transfer-encoding",	H_CTE,
96 	"content-type",			H_CTYPE,
97 
98 	NULL,			0,
99 };
100 
101 
102 
103 /*
104 **  Location of system files/databases/etc.
105 */
106 
107 char	*PidFile =	_PATH_SENDMAILPID;	/* stores daemon proc id */
108 
109 
110 
111 /*
112 **  Privacy values
113 */
114 
115 struct prival PrivacyValues[] =
116 {
117 	"public",		PRIV_PUBLIC,
118 	"needmailhelo",		PRIV_NEEDMAILHELO,
119 	"needexpnhelo",		PRIV_NEEDEXPNHELO,
120 	"needvrfyhelo",		PRIV_NEEDVRFYHELO,
121 	"noexpn",		PRIV_NOEXPN,
122 	"novrfy",		PRIV_NOVRFY,
123 	"restrictmailq",	PRIV_RESTRICTMAILQ,
124 	"restrictqrun",		PRIV_RESTRICTQRUN,
125 	"authwarnings",		PRIV_AUTHWARNINGS,
126 	"noreceipts",		PRIV_NORECEIPTS,
127 	"goaway",		PRIV_GOAWAY,
128 	NULL,			0,
129 };
130 
131 
132 
133 /*
134 **  Miscellaneous stuff.
135 */
136 
137 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
138 /*
139 **  SETDEFAULTS -- set default values
140 **
141 **	Because of the way freezing is done, these must be initialized
142 **	using direct code.
143 **
144 **	Parameters:
145 **		e -- the default envelope.
146 **
147 **	Returns:
148 **		none.
149 **
150 **	Side Effects:
151 **		Initializes a bunch of global variables to their
152 **		default values.
153 */
154 
155 #define DAYS		* 24 * 60 * 60
156 
157 setdefaults(e)
158 	register ENVELOPE *e;
159 {
160 	int i;
161 
162 	SpaceSub = ' ';				/* option B */
163 	QueueLA = 8;				/* option x */
164 	RefuseLA = 12;				/* option X */
165 	WkRecipFact = 30000L;			/* option y */
166 	WkClassFact = 1800L;			/* option z */
167 	WkTimeFact = 90000L;			/* option Z */
168 	QueueFactor = WkRecipFact * 20;		/* option q */
169 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
170 						/* option F */
171 	DefUid = 1;				/* option u */
172 	DefGid = 1;				/* option g */
173 	CheckpointInterval = 10;		/* option C */
174 	MaxHopCount = 25;			/* option h */
175 	e->e_sendmode = SM_FORK;		/* option d */
176 	e->e_errormode = EM_PRINT;		/* option e */
177 	SevenBitInput = FALSE;			/* option 7 */
178 	MaxMciCache = 1;			/* option k */
179 	MciCacheTimeout = 300;			/* option K */
180 	LogLevel = 9;				/* option L */
181 	inittimeouts(NULL);			/* option r */
182 	PrivacyFlags = 0;			/* option p */
183 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
184 	for (i = 0; i < MAXTOCLASS; i++)
185 	{
186 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
187 		TimeOuts.to_q_warning[i] = 0;		/* option T */
188 	}
189 	ServiceSwitchFile = "/etc/service.switch";
190 	setdefuser();
191 	setupmaps();
192 	setupmailers();
193 }
194 
195 
196 /*
197 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
198 */
199 
200 setdefuser()
201 {
202 	struct passwd *defpwent;
203 	static char defuserbuf[40];
204 
205 	DefUser = defuserbuf;
206 	if ((defpwent = getpwuid(DefUid)) != NULL)
207 		strcpy(defuserbuf, defpwent->pw_name);
208 	else
209 		strcpy(defuserbuf, "nobody");
210 }
211 /*
212 **  HOST_MAP_INIT -- initialize host class structures
213 */
214 
215 bool	host_map_init __P((MAP *map, char *args));
216 
217 bool
218 host_map_init(map, args)
219 	MAP *map;
220 	char *args;
221 {
222 	register char *p = args;
223 
224 	for (;;)
225 	{
226 		while (isascii(*p) && isspace(*p))
227 			p++;
228 		if (*p != '-')
229 			break;
230 		switch (*++p)
231 		{
232 		  case 'a':
233 			map->map_app = ++p;
234 			break;
235 		}
236 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
237 			p++;
238 		if (*p != '\0')
239 			*p++ = '\0';
240 	}
241 	if (map->map_app != NULL)
242 		map->map_app = newstr(map->map_app);
243 	return TRUE;
244 }
245 /*
246 **  SETUPMAILERS -- initialize default mailers
247 */
248 
249 setupmailers()
250 {
251 	char buf[100];
252 
253 	strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u");
254 	makemailer(buf);
255 
256 	strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE");
257 	makemailer(buf);
258 
259 	strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE");
260 	makemailer(buf);
261 }
262 /*
263 **  SETUPMAPS -- set up map classes
264 */
265 
266 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
267 	{ \
268 		extern bool parse __P((MAP *, char *)); \
269 		extern bool open __P((MAP *, int)); \
270 		extern void close __P((MAP *)); \
271 		extern char *lookup __P((MAP *, char *, char **, int *)); \
272 		extern void store __P((MAP *, char *, char *)); \
273 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
274 		s->s_mapclass.map_cname = name; \
275 		s->s_mapclass.map_ext = ext; \
276 		s->s_mapclass.map_cflags = flags; \
277 		s->s_mapclass.map_parse = parse; \
278 		s->s_mapclass.map_open = open; \
279 		s->s_mapclass.map_close = close; \
280 		s->s_mapclass.map_lookup = lookup; \
281 		s->s_mapclass.map_store = store; \
282 	}
283 
284 setupmaps()
285 {
286 	register STAB *s;
287 
288 #ifdef NEWDB
289 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
290 		map_parseargs, hash_map_open, db_map_close,
291 		db_map_lookup, db_map_store);
292 
293 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
294 		map_parseargs, bt_map_open, db_map_close,
295 		db_map_lookup, db_map_store);
296 #endif
297 
298 #ifdef NDBM
299 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
300 		map_parseargs, ndbm_map_open, ndbm_map_close,
301 		ndbm_map_lookup, ndbm_map_store);
302 #endif
303 
304 #ifdef NIS
305 	MAPDEF("nis", NULL, MCF_ALIASOK,
306 		map_parseargs, nis_map_open, null_map_close,
307 		nis_map_lookup, null_map_store);
308 #endif
309 
310 #ifdef NISPLUS
311 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
312 		map_parseargs, nisplus_map_open, null_map_close,
313 		nisplus_map_lookup, null_map_store);
314 #endif
315 
316 #ifdef HESIOD
317 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
318 		map_parseargs, null_map_open, null_map_close,
319 		hes_map_lookup, null_map_store);
320 #endif
321 
322 #ifdef NETINFO
323 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
324 		map_parseargs, ni_map_open, null_map_close,
325 		ni_map_lookup, null_map_store);
326 #endif
327 
328 #if 0
329 	MAPDEF("dns", NULL, 0,
330 		dns_map_init, null_map_open, null_map_close,
331 		dns_map_lookup, null_map_store);
332 #endif
333 
334 #if NAMED_BIND
335 	/* best MX DNS lookup */
336 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
337 		map_parseargs, null_map_open, null_map_close,
338 		bestmx_map_lookup, null_map_store);
339 #endif
340 
341 	MAPDEF("host", NULL, 0,
342 		host_map_init, null_map_open, null_map_close,
343 		host_map_lookup, null_map_store);
344 
345 	MAPDEF("text", NULL, MCF_ALIASOK,
346 		map_parseargs, text_map_open, null_map_close,
347 		text_map_lookup, null_map_store);
348 
349 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
350 		map_parseargs, stab_map_open, null_map_close,
351 		stab_map_lookup, stab_map_store);
352 
353 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
354 		map_parseargs, impl_map_open, impl_map_close,
355 		impl_map_lookup, impl_map_store);
356 
357 	/* access to system passwd file */
358 	MAPDEF("user", NULL, MCF_OPTFILE,
359 		map_parseargs, user_map_open, null_map_close,
360 		user_map_lookup, null_map_store);
361 
362 	/* dequote map */
363 	MAPDEF("dequote", NULL, 0,
364 		dequote_init, null_map_open, null_map_close,
365 		dequote_map, null_map_store);
366 
367 #if 0
368 # ifdef USERDB
369 	/* user database */
370 	MAPDEF("udb", ".db", 0,
371 		udb_map_parse, null_map_open, null_map_close,
372 		udb_map_lookup, null_map_store);
373 # endif
374 #endif
375 
376 	/* sequenced maps */
377 	MAPDEF("sequence", NULL, MCF_ALIASOK,
378 		seq_map_parse, null_map_open, null_map_close,
379 		seq_map_lookup, seq_map_store);
380 
381 	/* switched interface to sequenced maps */
382 	MAPDEF("switch", NULL, MCF_ALIASOK,
383 		map_parseargs, switch_map_open, null_map_close,
384 		seq_map_lookup, seq_map_store);
385 }
386 
387 #undef MAPDEF
388 /*
389 **  INITHOSTMAPS -- initial host-dependent maps
390 **
391 **	This should act as an interface to any local service switch
392 **	provided by the host operating system.
393 **
394 **	Parameters:
395 **		none
396 **
397 **	Returns:
398 **		none
399 **
400 **	Side Effects:
401 **		Should define maps "host" and "users" as necessary
402 **		for this OS.  If they are not defined, they will get
403 **		a default value later.  It should check to make sure
404 **		they are not defined first, since it's possible that
405 **		the config file has provided an override.
406 */
407 
408 void
409 inithostmaps()
410 {
411 	register int i;
412 	int nmaps;
413 	char *maptype[MAXMAPSTACK];
414 	short mapreturn[MAXMAPACTIONS];
415 	char buf[MAXLINE];
416 
417 	/*
418 	**  Set up default hosts maps.
419 	*/
420 
421 #if 0
422 	nmaps = switch_map_find("hosts", maptype, mapreturn);
423 	for (i = 0; i < nmaps; i++)
424 	{
425 		if (strcmp(maptype[i], "files") == 0 &&
426 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
427 		{
428 			strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts");
429 			makemapentry(buf);
430 		}
431 #if NAMED_BIND
432 		else if (strcmp(maptype[i], "dns") == 0 &&
433 		    stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
434 		{
435 			strcpy(buf, "hosts.dns dns A");
436 			makemapentry(buf);
437 		}
438 #endif
439 #ifdef NISPLUS
440 		else if (strcmp(maptype[i], "nisplus") == 0 &&
441 		    stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
442 		{
443 			strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir");
444 			makemapentry(buf);
445 		}
446 #endif
447 #ifdef NIS
448 		else if (strcmp(maptype[i], "nis") == 0 &&
449 		    stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
450 		{
451 			strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname");
452 			makemapentry(buf);
453 		}
454 #endif
455 	}
456 #endif
457 
458 	/*
459 	**  Make sure we have a host map.
460 	*/
461 
462 	if (stab("host", ST_MAP, ST_FIND) == NULL)
463 	{
464 		/* user didn't initialize: set up host map */
465 		strcpy(buf, "host host");
466 #if NAMED_BIND
467 		if (ConfigLevel >= 2)
468 			strcat(buf, " -a.");
469 #endif
470 		makemapentry(buf);
471 	}
472 
473 	/*
474 	**  Set up default aliases maps
475 	*/
476 
477 	nmaps = switch_map_find("aliases", maptype, mapreturn);
478 	for (i = 0; i < nmaps; i++)
479 	{
480 		if (strcmp(maptype[i], "files") == 0 &&
481 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
482 		{
483 			strcpy(buf, "aliases.files implicit /etc/aliases");
484 			makemapentry(buf);
485 		}
486 #ifdef NISPLUS
487 		else if (strcmp(maptype[i], "nisplus") == 0 &&
488 		    stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
489 		{
490 			strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir");
491 			makemapentry(buf);
492 		}
493 #endif
494 #ifdef NIS
495 		else if (strcmp(maptype[i], "nis") == 0 &&
496 		    stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
497 		{
498 			strcpy(buf, "aliases.nis nis -d mail.aliases");
499 			makemapentry(buf);
500 		}
501 #endif
502 	}
503 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
504 	{
505 		strcpy(buf, "aliases switch aliases");
506 		makemapentry(buf);
507 	}
508 	strcpy(buf, "switch:aliases");
509 	setalias(buf);
510 
511 #if 0		/* "user" map class is a better choice */
512 	/*
513 	**  Set up default users maps.
514 	*/
515 
516 	nmaps = switch_map_find("passwd", maptype, mapreturn);
517 	for (i = 0; i < nmaps; i++)
518 	{
519 		if (strcmp(maptype[i], "files") == 0 &&
520 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
521 		{
522 			strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd");
523 			makemapentry(buf);
524 		}
525 #ifdef NISPLUS
526 		else if (strcmp(maptype[i], "nisplus") == 0 &&
527 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
528 		{
529 			strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir");
530 			makemapentry(buf);
531 		}
532 #endif
533 #ifdef NIS
534 		else if (strcmp(maptype[i], "nis") == 0 &&
535 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
536 		{
537 			strcpy(buf, "users.nis nis -m -d passwd.byname");
538 			makemapentry(buf);
539 		}
540 #endif
541 #ifdef HESIOD
542 		else if (strcmp(maptype[i], "hesiod") == 0) &&
543 		    stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
544 		{
545 			strcpy(buf, "users.hesiod hesiod");
546 			makemapentry(buf);
547 		}
548 #endif
549 	}
550 	if (stab("users", ST_MAP, ST_FIND) == NULL)
551 	{
552 		strcpy(buf, "users switch -m passwd");
553 		makemapentry(buf);
554 	}
555 #endif
556 }
557 /*
558 **  SWITCH_MAP_FIND -- find the list of types associated with a map
559 **
560 **	This is the system-dependent interface to the service switch.
561 **
562 **	Parameters:
563 **		service -- the name of the service of interest.
564 **		maptype -- an out-array of strings containing the types
565 **			of access to use for this service.  There can
566 **			be at most MAXMAPSTACK types for a single service.
567 **		mapreturn -- an out-array of return information bitmaps
568 **			for the map.
569 **
570 **	Returns:
571 **		The number of map types filled in, or -1 for failure.
572 */
573 
574 #ifdef SOLARIS
575 # include <nsswitch.h>
576 #endif
577 
578 #if defined(ultrix) || defined(__osf__)
579 # include <sys/svcinfo.h>
580 #endif
581 
582 int
583 switch_map_find(service, maptype, mapreturn)
584 	char *service;
585 	char *maptype[MAXMAPSTACK];
586 	short mapreturn[MAXMAPACTIONS];
587 {
588 	register FILE *fp;
589 	int svcno;
590 	static char buf[MAXLINE];
591 
592 #ifdef SOLARIS
593 	struct __nsw_switchconfig *nsw_conf;
594 	enum __nsw_parse_err pserr;
595 	struct __nsw_lookup *lk;
596 	int nsw_rc;
597 	static struct __nsw_lookup lkp0 =
598 		{ "files", {1, 0, 0, 0}, NULL, NULL };
599 	static struct __nsw_switchconfig lkp_default =
600 		{ 0, "sendmail", 3, &lkp0 };
601 
602 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
603 		lk = lkp_default.lookups;
604 	else
605 		lk = nsw_conf->lookups;
606 	svcno = 0;
607 	while (lk != NULL)
608 	{
609 		maptype[svcno] = lk->service_name;
610 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
611 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
612 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
613 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
614 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
615 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
616 		svcno++;
617 		lk = lk->next;
618 	}
619 	return svcno;
620 #endif
621 
622 #if defined(ultrix) || defined(__osf__)
623 	struct svcinfo *svcinfo;
624 	int svc;
625 
626 	svcinfo = getsvc();
627 	if (svcinfo == NULL)
628 		goto punt;
629 	if (strcmp(service, "hosts") == 0)
630 		svc = SVC_HOSTS;
631 	else if (strcmp(service, "aliases") == 0)
632 		svc = SVC_ALIASES;
633 	else if (strcmp(service, "passwd") == 0)
634 		svc = SVC_PASSWD;
635 	else
636 		return -1;
637 	for (svcno = 0; svcno < SVC_PATHSIZE; svcno++)
638 	{
639 		switch (svcinfo->svcpath[svc][svcno])
640 		{
641 		  case SVC_LOCAL:
642 			maptype[svcno] = "files";
643 			break;
644 
645 		  case SVC_YP:
646 			maptype[svcno] = "nis";
647 			break;
648 
649 		  case SVC_BIND:
650 			maptype[svcno] = "dns";
651 			break;
652 
653 #ifdef SVC_HESIOD
654 		  case SVC_HESIOD:
655 			maptype[svcno] = "hesiod";
656 			break;
657 #endif
658 
659 		  case SVC_LAST:
660 			return svcno;
661 		}
662 	}
663 	return svcno;
664 #endif
665 
666 #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__)
667 	/*
668 	**  Fall-back mechanism.
669 	*/
670 
671 	svcno = 0;
672 	fp = fopen(ServiceSwitchFile, "r");
673 	if (fp != NULL)
674 	{
675 		while (fgets(buf, sizeof buf, fp) != NULL)
676 		{
677 			register char *p;
678 
679 			p = strpbrk(buf, "#\n");
680 			if (p != NULL)
681 				*p = '\0';
682 			p = strpbrk(buf, " \t");
683 			if (p != NULL)
684 				*p++ = '\0';
685 			if (strcmp(buf, service) != 0)
686 				continue;
687 
688 			/* got the right service -- extract data */
689 			do
690 			{
691 				while (isspace(*p))
692 					p++;
693 				if (*p == '\0')
694 					break;
695 				maptype[svcno++] = p;
696 				p = strpbrk(p, " \t");
697 				if (p != NULL)
698 					*p++ = '\0';
699 			} while (p != NULL);
700 			break;
701 		}
702 		fclose(fp);
703 		return svcno;
704 	}
705 #endif
706 
707 	/* if the service file doesn't work, use an absolute fallback */
708   punt:
709 	if (strcmp(service, "aliases") == 0)
710 	{
711 		maptype[0] = "files";
712 		return 1;
713 	}
714 	if (strcmp(service, "hosts") == 0)
715 	{
716 # if NAMED_BIND
717 		maptype[svcno++] = "dns";
718 # else
719 #  if defined(sun) && !defined(BSD) && !defined(SOLARIS)
720 		/* SunOS */
721 		maptype[svcno++] = "nis";
722 #  endif
723 # endif
724 		maptype[svcno++] = "files";
725 		return svcno;
726 	}
727 	return -1;
728 }
729 /*
730 **  USERNAME -- return the user id of the logged in user.
731 **
732 **	Parameters:
733 **		none.
734 **
735 **	Returns:
736 **		The login name of the logged in user.
737 **
738 **	Side Effects:
739 **		none.
740 **
741 **	Notes:
742 **		The return value is statically allocated.
743 */
744 
745 char *
746 username()
747 {
748 	static char *myname = NULL;
749 	extern char *getlogin();
750 	register struct passwd *pw;
751 
752 	/* cache the result */
753 	if (myname == NULL)
754 	{
755 		myname = getlogin();
756 		if (myname == NULL || myname[0] == '\0')
757 		{
758 			pw = getpwuid(RealUid);
759 			if (pw != NULL)
760 				myname = newstr(pw->pw_name);
761 		}
762 		else
763 		{
764 			uid_t uid = RealUid;
765 
766 			myname = newstr(myname);
767 			if ((pw = getpwnam(myname)) == NULL ||
768 			      (uid != 0 && uid != pw->pw_uid))
769 			{
770 				pw = getpwuid(uid);
771 				if (pw != NULL)
772 					myname = newstr(pw->pw_name);
773 			}
774 		}
775 		if (myname == NULL || myname[0] == '\0')
776 		{
777 			syserr("554 Who are you?");
778 			myname = "postmaster";
779 		}
780 	}
781 
782 	return (myname);
783 }
784 /*
785 **  TTYPATH -- Get the path of the user's tty
786 **
787 **	Returns the pathname of the user's tty.  Returns NULL if
788 **	the user is not logged in or if s/he has write permission
789 **	denied.
790 **
791 **	Parameters:
792 **		none
793 **
794 **	Returns:
795 **		pathname of the user's tty.
796 **		NULL if not logged in or write permission denied.
797 **
798 **	Side Effects:
799 **		none.
800 **
801 **	WARNING:
802 **		Return value is in a local buffer.
803 **
804 **	Called By:
805 **		savemail
806 */
807 
808 char *
809 ttypath()
810 {
811 	struct stat stbuf;
812 	register char *pathn;
813 	extern char *ttyname();
814 	extern char *getlogin();
815 
816 	/* compute the pathname of the controlling tty */
817 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
818 	    (pathn = ttyname(0)) == NULL)
819 	{
820 		errno = 0;
821 		return (NULL);
822 	}
823 
824 	/* see if we have write permission */
825 	if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode))
826 	{
827 		errno = 0;
828 		return (NULL);
829 	}
830 
831 	/* see if the user is logged in */
832 	if (getlogin() == NULL)
833 		return (NULL);
834 
835 	/* looks good */
836 	return (pathn);
837 }
838 /*
839 **  CHECKCOMPAT -- check for From and To person compatible.
840 **
841 **	This routine can be supplied on a per-installation basis
842 **	to determine whether a person is allowed to send a message.
843 **	This allows restriction of certain types of internet
844 **	forwarding or registration of users.
845 **
846 **	If the hosts are found to be incompatible, an error
847 **	message should be given using "usrerr" and 0 should
848 **	be returned.
849 **
850 **	EF_NORETURN can be set in e->e_flags to suppress the return-to-sender
851 **	function; this should be done on huge messages.
852 **
853 **	Parameters:
854 **		to -- the person being sent to.
855 **
856 **	Returns:
857 **		an exit status
858 **
859 **	Side Effects:
860 **		none (unless you include the usrerr stuff)
861 */
862 
863 checkcompat(to, e)
864 	register ADDRESS *to;
865 	register ENVELOPE *e;
866 {
867 # ifdef lint
868 	if (to == NULL)
869 		to++;
870 # endif /* lint */
871 
872 	if (tTd(49, 1))
873 		printf("checkcompat(to=%s, from=%s)\n",
874 			to->q_paddr, e->e_from.q_paddr);
875 
876 # ifdef EXAMPLE_CODE
877 	/* this code is intended as an example only */
878 	register STAB *s;
879 
880 	s = stab("arpa", ST_MAILER, ST_FIND);
881 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
882 	    to->q_mailer == s->s_mailer)
883 	{
884 		usrerr("553 No ARPA mail through this machine: see your system administration");
885 		/* e->e_flags |= EF_NORETURN; to supress return copy */
886 		return (EX_UNAVAILABLE);
887 	}
888 # endif /* EXAMPLE_CODE */
889 	return (EX_OK);
890 }
891 /*
892 **  SETSIGNAL -- set a signal handler
893 **
894 **	This is essentially old BSD "signal(3)".
895 */
896 
897 sigfunc_t
898 setsignal(sig, handler)
899 	int sig;
900 	sigfunc_t handler;
901 {
902 #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE)
903 	return signal(sig, handler);
904 #else
905 	struct sigaction n, o;
906 
907 	bzero(&n, sizeof n);
908 	n.sa_handler = handler;
909 # ifdef SA_RESTART
910 	n.sa_flags = SA_RESTART;
911 # endif
912 	if (sigaction(sig, &n, &o) < 0)
913 		return SIG_ERR;
914 	return o.sa_handler;
915 #endif
916 }
917 /*
918 **  HOLDSIGS -- arrange to hold all signals
919 **
920 **	Parameters:
921 **		none.
922 **
923 **	Returns:
924 **		none.
925 **
926 **	Side Effects:
927 **		Arranges that signals are held.
928 */
929 
930 holdsigs()
931 {
932 }
933 /*
934 **  RLSESIGS -- arrange to release all signals
935 **
936 **	This undoes the effect of holdsigs.
937 **
938 **	Parameters:
939 **		none.
940 **
941 **	Returns:
942 **		none.
943 **
944 **	Side Effects:
945 **		Arranges that signals are released.
946 */
947 
948 rlsesigs()
949 {
950 }
951 /*
952 **  INIT_MD -- do machine dependent initializations
953 **
954 **	Systems that have global modes that should be set should do
955 **	them here rather than in main.
956 */
957 
958 #ifdef _AUX_SOURCE
959 # include	<compat.h>
960 #endif
961 
962 init_md(argc, argv)
963 	int argc;
964 	char **argv;
965 {
966 #ifdef _AUX_SOURCE
967 	setcompat(getcompat() | COMPAT_BSDPROT);
968 #endif
969 
970 #ifdef VENDOR_DEFAULT
971 	VendorCode = VENDOR_DEFAULT;
972 #else
973 	VendorCode = VENDOR_BERKELEY;
974 #endif
975 }
976 /*
977 **  GETLA -- get the current load average
978 **
979 **	This code stolen from la.c.
980 **
981 **	Parameters:
982 **		none.
983 **
984 **	Returns:
985 **		The current load average as an integer.
986 **
987 **	Side Effects:
988 **		none.
989 */
990 
991 /* try to guess what style of load average we have */
992 #define LA_ZERO		1	/* always return load average as zero */
993 #define LA_INT		2	/* read kmem for avenrun; interpret as long */
994 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
995 #define LA_SUBR		4	/* call getloadavg */
996 #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
997 #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
998 #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
999 
1000 /* do guesses based on general OS type */
1001 #ifndef LA_TYPE
1002 # define LA_TYPE	LA_ZERO
1003 #endif
1004 
1005 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
1006 
1007 #include <nlist.h>
1008 
1009 #ifdef IRIX64
1010 # define nlist		nlist64
1011 #endif
1012 
1013 #ifndef LA_AVENRUN
1014 # ifdef SYSTEM5
1015 #  define LA_AVENRUN	"avenrun"
1016 # else
1017 #  define LA_AVENRUN	"_avenrun"
1018 # endif
1019 #endif
1020 
1021 /* _PATH_UNIX should be defined in <paths.h> */
1022 #ifndef _PATH_UNIX
1023 # if defined(SYSTEM5)
1024 #  define _PATH_UNIX	"/unix"
1025 # else
1026 #  define _PATH_UNIX	"/vmunix"
1027 # endif
1028 #endif
1029 
1030 struct	nlist Nl[] =
1031 {
1032 	{ LA_AVENRUN },
1033 #define	X_AVENRUN	0
1034 	{ 0 },
1035 };
1036 
1037 #ifndef FSHIFT
1038 # if defined(unixpc)
1039 #  define FSHIFT	5
1040 # endif
1041 
1042 # if defined(__alpha) || defined(IRIX)
1043 #  define FSHIFT	10
1044 # endif
1045 
1046 # if defined(_AIX3)
1047 #  define FSHIFT	16
1048 # endif
1049 #endif
1050 
1051 #ifndef FSHIFT
1052 # define FSHIFT		8
1053 #endif
1054 
1055 #ifndef FSCALE
1056 # define FSCALE		(1 << FSHIFT)
1057 #endif
1058 
1059 getla()
1060 {
1061 	static int kmem = -1;
1062 #if LA_TYPE == LA_INT
1063 	long avenrun[3];
1064 #else
1065 # if LA_TYPE == LA_SHORT
1066 	short avenrun[3];
1067 # else
1068 	double avenrun[3];
1069 # endif
1070 #endif
1071 	extern off_t lseek();
1072 	extern int errno;
1073 
1074 	if (kmem < 0)
1075 	{
1076 		kmem = open("/dev/kmem", 0, 0);
1077 		if (kmem < 0)
1078 		{
1079 			if (tTd(3, 1))
1080 				printf("getla: open(/dev/kmem): %s\n",
1081 					errstring(errno));
1082 			return (-1);
1083 		}
1084 		(void) fcntl(kmem, F_SETFD, 1);
1085 #ifdef _AIX3
1086 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
1087 #else
1088 		if (nlist(_PATH_UNIX, Nl) < 0)
1089 #endif
1090 		{
1091 			if (tTd(3, 1))
1092 				printf("getla: nlist(%s): %s\n", _PATH_UNIX,
1093 					errstring(errno));
1094 			return (-1);
1095 		}
1096 		if (Nl[X_AVENRUN].n_value == 0)
1097 		{
1098 			if (tTd(3, 1))
1099 				printf("getla: nlist(%s, %s) ==> 0\n",
1100 					_PATH_UNIX, LA_AVENRUN);
1101 			return (-1);
1102 		}
1103 #ifdef NAMELISTMASK
1104 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
1105 #endif
1106 	}
1107 	if (tTd(3, 20))
1108 		printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value);
1109 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
1110 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
1111 	{
1112 		/* thank you Ian */
1113 		if (tTd(3, 1))
1114 			printf("getla: lseek or read: %s\n", errstring(errno));
1115 		return (-1);
1116 	}
1117 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
1118 	if (tTd(3, 5))
1119 	{
1120 		printf("getla: avenrun = %d", avenrun[0]);
1121 		if (tTd(3, 15))
1122 			printf(", %d, %d", avenrun[1], avenrun[2]);
1123 		printf("\n");
1124 	}
1125 	if (tTd(3, 1))
1126 		printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1127 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1128 #else
1129 	if (tTd(3, 5))
1130 	{
1131 		printf("getla: avenrun = %g", avenrun[0]);
1132 		if (tTd(3, 15))
1133 			printf(", %g, %g", avenrun[1], avenrun[2]);
1134 		printf("\n");
1135 	}
1136 	if (tTd(3, 1))
1137 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
1138 	return ((int) (avenrun[0] + 0.5));
1139 #endif
1140 }
1141 
1142 #else
1143 #if LA_TYPE == LA_SUBR
1144 
1145 #ifdef DGUX
1146 
1147 #include <sys/dg_sys_info.h>
1148 
1149 int getla()
1150 {
1151 	struct dg_sys_info_load_info load_info;
1152 
1153 	dg_sys_info((long *)&load_info,
1154 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
1155 
1156         if (tTd(3, 1))
1157                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1158 
1159 	return((int) (load_info.one_minute + 0.5));
1160 }
1161 
1162 #else
1163 
1164 getla()
1165 {
1166 	double avenrun[3];
1167 
1168 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
1169 	{
1170 		if (tTd(3, 1))
1171 			perror("getla: getloadavg failed:");
1172 		return (-1);
1173 	}
1174 	if (tTd(3, 1))
1175 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
1176 	return ((int) (avenrun[0] + 0.5));
1177 }
1178 
1179 #endif /* DGUX */
1180 #else
1181 #if LA_TYPE == LA_MACH
1182 
1183 /*
1184 **  This has been tested on NEXTSTEP release 2.1/3.X.
1185 */
1186 
1187 #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
1188 # include <mach/mach.h>
1189 #else
1190 # include <mach.h>
1191 #endif
1192 
1193 getla()
1194 {
1195 	processor_set_t default_set;
1196 	kern_return_t error;
1197 	unsigned int info_count;
1198 	struct processor_set_basic_info info;
1199 	host_t host;
1200 
1201 	error = processor_set_default(host_self(), &default_set);
1202 	if (error != KERN_SUCCESS)
1203 		return -1;
1204 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
1205 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
1206 			       &host, (processor_set_info_t)&info,
1207 			       &info_count) != KERN_SUCCESS)
1208 	{
1209 		return -1;
1210 	}
1211 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
1212 }
1213 
1214 
1215 #else
1216 #if LA_TYPE == LA_PROCSTR
1217 
1218 /*
1219 **  Read /proc/loadavg for the load average.  This is assumed to be
1220 **  in a format like "0.15 0.12 0.06".
1221 **
1222 **	Initially intended for Linux.  This has been in the kernel
1223 **	since at least 0.99.15.
1224 */
1225 
1226 # ifndef _PATH_LOADAVG
1227 #  define _PATH_LOADAVG	"/proc/loadavg"
1228 # endif
1229 
1230 int
1231 getla()
1232 {
1233 	double avenrun;
1234 	register int result;
1235 	FILE *fp;
1236 
1237 	fp = fopen(_PATH_LOADAVG, "r");
1238 	if (fp == NULL)
1239 	{
1240 		if (tTd(3, 1))
1241 			printf("getla: fopen(%s): %s\n",
1242 				_PATH_LOADAVG, errstring(errno));
1243 		return -1;
1244 	}
1245 	result = fscanf(fp, "%lf", &avenrun);
1246 	fclose(fp);
1247 	if (result != 1)
1248 	{
1249 		if (tTd(3, 1))
1250 			printf("getla: fscanf() = %d: %s\n",
1251 				result, errstring(errno));
1252 		return -1;
1253 	}
1254 
1255 	if (tTd(3, 1))
1256 		printf("getla(): %.2f\n", avenrun);
1257 
1258 	return ((int) (avenrun + 0.5));
1259 }
1260 
1261 #else
1262 
1263 getla()
1264 {
1265 	if (tTd(3, 1))
1266 		printf("getla: ZERO\n");
1267 	return (0);
1268 }
1269 
1270 #endif
1271 #endif
1272 #endif
1273 #endif
1274 
1275 
1276 /*
1277  * Copyright 1989 Massachusetts Institute of Technology
1278  *
1279  * Permission to use, copy, modify, distribute, and sell this software and its
1280  * documentation for any purpose is hereby granted without fee, provided that
1281  * the above copyright notice appear in all copies and that both that
1282  * copyright notice and this permission notice appear in supporting
1283  * documentation, and that the name of M.I.T. not be used in advertising or
1284  * publicity pertaining to distribution of the software without specific,
1285  * written prior permission.  M.I.T. makes no representations about the
1286  * suitability of this software for any purpose.  It is provided "as is"
1287  * without express or implied warranty.
1288  *
1289  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
1290  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
1291  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1292  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1293  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1294  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1295  *
1296  * Authors:  Many and varied...
1297  */
1298 
1299 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
1300 #ifndef lint
1301 static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
1302 #endif /* !lint */
1303 
1304 #ifdef apollo
1305 # undef volatile
1306 #    include <apollo/base.h>
1307 
1308 /* ARGSUSED */
1309 int getloadavg( call_data )
1310      caddr_t	call_data;	/* pointer to (double) return value */
1311 {
1312      double *avenrun = (double *) call_data;
1313      int i;
1314      status_$t      st;
1315      long loadav[3];
1316      proc1_$get_loadav(loadav, &st);
1317      *avenrun = loadav[0] / (double) (1 << 16);
1318      return(0);
1319 }
1320 #   endif /* apollo */
1321 /*
1322 **  SHOULDQUEUE -- should this message be queued or sent?
1323 **
1324 **	Compares the message cost to the load average to decide.
1325 **
1326 **	Parameters:
1327 **		pri -- the priority of the message in question.
1328 **		ctime -- the message creation time.
1329 **
1330 **	Returns:
1331 **		TRUE -- if this message should be queued up for the
1332 **			time being.
1333 **		FALSE -- if the load is low enough to send this message.
1334 **
1335 **	Side Effects:
1336 **		none.
1337 */
1338 
1339 bool
1340 shouldqueue(pri, ctime)
1341 	long pri;
1342 	time_t ctime;
1343 {
1344 	bool rval;
1345 
1346 	if (tTd(3, 30))
1347 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
1348 	if (CurrentLA < QueueLA)
1349 	{
1350 		if (tTd(3, 30))
1351 			printf("FALSE (CurrentLA < QueueLA)\n");
1352 		return (FALSE);
1353 	}
1354 	if (CurrentLA >= RefuseLA)
1355 	{
1356 		if (tTd(3, 30))
1357 			printf("TRUE (CurrentLA >= RefuseLA)\n");
1358 		return (TRUE);
1359 	}
1360 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
1361 	if (tTd(3, 30))
1362 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
1363 	return rval;
1364 }
1365 /*
1366 **  REFUSECONNECTIONS -- decide if connections should be refused
1367 **
1368 **	Parameters:
1369 **		none.
1370 **
1371 **	Returns:
1372 **		TRUE if incoming SMTP connections should be refused
1373 **			(for now).
1374 **		FALSE if we should accept new work.
1375 **
1376 **	Side Effects:
1377 **		none.
1378 */
1379 
1380 bool
1381 refuseconnections()
1382 {
1383 	extern bool enoughspace();
1384 
1385 #ifdef XLA
1386 	if (!xla_smtp_ok())
1387 		return TRUE;
1388 #endif
1389 
1390 	/* this is probably too simplistic */
1391 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
1392 }
1393 /*
1394 **  SETPROCTITLE -- set process title for ps
1395 **
1396 **	Parameters:
1397 **		fmt -- a printf style format string.
1398 **		a, b, c -- possible parameters to fmt.
1399 **
1400 **	Returns:
1401 **		none.
1402 **
1403 **	Side Effects:
1404 **		Clobbers argv of our main procedure so ps(1) will
1405 **		display the title.
1406 */
1407 
1408 #define SPT_NONE	0	/* don't use it at all */
1409 #define SPT_REUSEARGV	1	/* cover argv with title information */
1410 #define SPT_BUILTIN	2	/* use libc builtin */
1411 #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
1412 #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
1413 #define SPT_WRITEUDOT	5	/* write u. area in kmem */
1414 
1415 #ifndef SPT_TYPE
1416 # define SPT_TYPE	SPT_REUSEARGV
1417 #endif
1418 
1419 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
1420 
1421 # if SPT_TYPE == SPT_PSTAT
1422 #  include <sys/pstat.h>
1423 # endif
1424 # if SPT_TYPE == SPT_PSSTRINGS
1425 #  include <machine/vmparam.h>
1426 #  include <sys/exec.h>
1427 #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
1428 #   undef SPT_TYPE
1429 #   define SPT_TYPE	SPT_REUSEARGV
1430 #  endif
1431 # endif
1432 
1433 # if SPT_TYPE == SPT_PSSTRINGS
1434 #  define SETPROC_STATIC	static
1435 # else
1436 #  define SETPROC_STATIC
1437 # endif
1438 
1439 # ifndef SPT_PADCHAR
1440 #  define SPT_PADCHAR	' '
1441 # endif
1442 
1443 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
1444 
1445 #if SPT_TYPE != SPT_BUILTIN
1446 
1447 /*VARARGS1*/
1448 # ifdef __STDC__
1449 setproctitle(char *fmt, ...)
1450 # else
1451 setproctitle(fmt, va_alist)
1452 	char *fmt;
1453 	va_dcl
1454 # endif
1455 {
1456 # if SPT_TYPE != SPT_NONE
1457 	register char *p;
1458 	register int i;
1459 	SETPROC_STATIC char buf[MAXLINE];
1460 	VA_LOCAL_DECL
1461 #  if SPT_TYPE == SPT_PSTAT
1462 	union pstun pst;
1463 #  endif
1464 	extern char **Argv;
1465 	extern char *LastArgv;
1466 
1467 	p = buf;
1468 
1469 	/* print sendmail: heading for grep */
1470 	(void) strcpy(p, "sendmail: ");
1471 	p += strlen(p);
1472 
1473 	/* print the argument string */
1474 	VA_START(fmt);
1475 	(void) vsprintf(p, fmt, ap);
1476 	VA_END;
1477 
1478 	i = strlen(buf);
1479 
1480 #  if SPT_TYPE == SPT_PSTAT
1481 	pst.pst_command = buf;
1482 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
1483 #  else
1484 #   if SPT_TYPE == SPT_PSSTRINGS
1485 	PS_STRINGS->ps_nargvstr = 1;
1486 	PS_STRINGS->ps_argvstr = buf;
1487 #   else
1488 	if (i > LastArgv - Argv[0] - 2)
1489 	{
1490 		i = LastArgv - Argv[0] - 2;
1491 		buf[i] = '\0';
1492 	}
1493 	(void) strcpy(Argv[0], buf);
1494 	p = &Argv[0][i];
1495 	while (p < LastArgv)
1496 		*p++ = SPT_PADCHAR;
1497 	Argv[1] = NULL;
1498 #   endif /* SPT_TYPE == SPT_PSSTRINGS */
1499 #  endif /* SPT_TYPE == SPT_PSTAT */
1500 # endif /* SPT_TYPE != SPT_NONE */
1501 }
1502 
1503 #endif /* SPT_TYPE != SPT_BUILTIN */
1504 /*
1505 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
1506 **
1507 **	Parameters:
1508 **		none.
1509 **
1510 **	Returns:
1511 **		none.
1512 **
1513 **	Side Effects:
1514 **		Picks up extant zombies.
1515 */
1516 
1517 void
1518 reapchild()
1519 {
1520 	int olderrno = errno;
1521 # ifdef HASWAITPID
1522 	auto int status;
1523 	int count;
1524 	int pid;
1525 
1526 	count = 0;
1527 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1528 	{
1529 		if (count++ > 1000)
1530 		{
1531 #ifdef LOG
1532 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
1533 				pid, status);
1534 #endif
1535 			break;
1536 		}
1537 	}
1538 # else
1539 # ifdef WNOHANG
1540 	union wait status;
1541 
1542 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
1543 		continue;
1544 # else /* WNOHANG */
1545 	auto int status;
1546 
1547 	while (wait(&status) > 0)
1548 		continue;
1549 # endif /* WNOHANG */
1550 # endif
1551 # ifdef SYS5SIGNALS
1552 	(void) setsignal(SIGCHLD, reapchild);
1553 # endif
1554 	errno = olderrno;
1555 }
1556 /*
1557 **  UNSETENV -- remove a variable from the environment
1558 **
1559 **	Not needed on newer systems.
1560 **
1561 **	Parameters:
1562 **		name -- the string name of the environment variable to be
1563 **			deleted from the current environment.
1564 **
1565 **	Returns:
1566 **		none.
1567 **
1568 **	Globals:
1569 **		environ -- a pointer to the current environment.
1570 **
1571 **	Side Effects:
1572 **		Modifies environ.
1573 */
1574 
1575 #ifndef HASUNSETENV
1576 
1577 void
1578 unsetenv(name)
1579 	char *name;
1580 {
1581 	extern char **environ;
1582 	register char **pp;
1583 	int len = strlen(name);
1584 
1585 	for (pp = environ; *pp != NULL; pp++)
1586 	{
1587 		if (strncmp(name, *pp, len) == 0 &&
1588 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1589 			break;
1590 	}
1591 
1592 	for (; *pp != NULL; pp++)
1593 		*pp = pp[1];
1594 }
1595 
1596 #endif
1597 /*
1598 **  GETDTABLESIZE -- return number of file descriptors
1599 **
1600 **	Only on non-BSD systems
1601 **
1602 **	Parameters:
1603 **		none
1604 **
1605 **	Returns:
1606 **		size of file descriptor table
1607 **
1608 **	Side Effects:
1609 **		none
1610 */
1611 
1612 #ifdef SOLARIS
1613 # include <sys/resource.h>
1614 #endif
1615 
1616 int
1617 getdtsize()
1618 {
1619 #ifdef RLIMIT_NOFILE
1620 	struct rlimit rl;
1621 
1622 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1623 		return rl.rlim_cur;
1624 #endif
1625 
1626 # ifdef HASGETDTABLESIZE
1627 	return getdtablesize();
1628 # else
1629 #  ifdef _SC_OPEN_MAX
1630 	return sysconf(_SC_OPEN_MAX);
1631 #  else
1632 	return NOFILE;
1633 #  endif
1634 # endif
1635 }
1636 /*
1637 **  UNAME -- get the UUCP name of this system.
1638 */
1639 
1640 #ifndef HASUNAME
1641 
1642 int
1643 uname(name)
1644 	struct utsname *name;
1645 {
1646 	FILE *file;
1647 	char *n;
1648 
1649 	name->nodename[0] = '\0';
1650 
1651 	/* try /etc/whoami -- one line with the node name */
1652 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1653 	{
1654 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1655 		(void) fclose(file);
1656 		n = strchr(name->nodename, '\n');
1657 		if (n != NULL)
1658 			*n = '\0';
1659 		if (name->nodename[0] != '\0')
1660 			return (0);
1661 	}
1662 
1663 	/* try /usr/include/whoami.h -- has a #define somewhere */
1664 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1665 	{
1666 		char buf[MAXLINE];
1667 
1668 		while (fgets(buf, MAXLINE, file) != NULL)
1669 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1670 					NODE_LENGTH, name->nodename) > 0)
1671 				break;
1672 		(void) fclose(file);
1673 		if (name->nodename[0] != '\0')
1674 			return (0);
1675 	}
1676 
1677 #ifdef TRUST_POPEN
1678 	/*
1679 	**  Popen is known to have security holes.
1680 	*/
1681 
1682 	/* try uuname -l to return local name */
1683 	if ((file = popen("uuname -l", "r")) != NULL)
1684 	{
1685 		(void) fgets(name, NODE_LENGTH + 1, file);
1686 		(void) pclose(file);
1687 		n = strchr(name, '\n');
1688 		if (n != NULL)
1689 			*n = '\0';
1690 		if (name->nodename[0] != '\0')
1691 			return (0);
1692 	}
1693 #endif
1694 
1695 	return (-1);
1696 }
1697 #endif /* HASUNAME */
1698 /*
1699 **  INITGROUPS -- initialize groups
1700 **
1701 **	Stub implementation for System V style systems
1702 */
1703 
1704 #ifndef HASINITGROUPS
1705 
1706 initgroups(name, basegid)
1707 	char *name;
1708 	int basegid;
1709 {
1710 	return 0;
1711 }
1712 
1713 #endif
1714 /*
1715 **  SETSID -- set session id (for non-POSIX systems)
1716 */
1717 
1718 #ifndef HASSETSID
1719 
1720 pid_t
1721 setsid __P ((void))
1722 {
1723 #ifdef TIOCNOTTY
1724 	int fd;
1725 
1726 	fd = open("/dev/tty", O_RDWR, 0);
1727 	if (fd >= 0)
1728 	{
1729 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1730 		(void) close(fd);
1731 	}
1732 #endif /* TIOCNOTTY */
1733 # ifdef SYS5SETPGRP
1734 	return setpgrp();
1735 # else
1736 	return setpgid(0, getpid());
1737 # endif
1738 }
1739 
1740 #endif
1741 /*
1742 **  FSYNC -- dummy fsync
1743 */
1744 
1745 #ifdef NEEDFSYNC
1746 
1747 fsync(fd)
1748 	int fd;
1749 {
1750 # ifdef O_SYNC
1751 	return fcntl(fd, F_SETFL, O_SYNC);
1752 # else
1753 	/* nothing we can do */
1754 	return 0;
1755 # endif
1756 }
1757 
1758 #endif
1759 /*
1760 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1761 **
1762 **	Data General DG/UX version of inet_addr returns a struct in_addr
1763 **	instead of a long.  This patches things.  Only needed on versions
1764 **	prior to 5.4.3.
1765 */
1766 
1767 #ifdef DGUX_5_4_2
1768 
1769 #undef inet_addr
1770 
1771 long
1772 dgux_inet_addr(host)
1773 	char *host;
1774 {
1775 	struct in_addr haddr;
1776 
1777 	haddr = inet_addr(host);
1778 	return haddr.s_addr;
1779 }
1780 
1781 #endif
1782 /*
1783 **  GETOPT -- for old systems or systems with bogus implementations
1784 */
1785 
1786 #ifdef NEEDGETOPT
1787 
1788 /*
1789  * Copyright (c) 1985 Regents of the University of California.
1790  * All rights reserved.  The Berkeley software License Agreement
1791  * specifies the terms and conditions for redistribution.
1792  */
1793 
1794 
1795 /*
1796 ** this version hacked to add `atend' flag to allow state machine
1797 ** to reset if invoked by the program to scan args for a 2nd time
1798 */
1799 
1800 #if defined(LIBC_SCCS) && !defined(lint)
1801 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1802 #endif /* LIBC_SCCS and not lint */
1803 
1804 #include <stdio.h>
1805 
1806 /*
1807  * get option letter from argument vector
1808  */
1809 #ifdef _CONVEX_SOURCE
1810 extern int	optind, opterr;
1811 #else
1812 int	opterr = 1;		/* if error message should be printed */
1813 int	optind = 1;		/* index into parent argv vector */
1814 #endif
1815 int	optopt;			/* character checked for validity */
1816 char	*optarg;		/* argument associated with option */
1817 
1818 #define BADCH	(int)'?'
1819 #define EMSG	""
1820 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1821 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1822 
1823 getopt(nargc,nargv,ostr)
1824 	int		nargc;
1825 	char *const	*nargv;
1826 	const char	*ostr;
1827 {
1828 	static char	*place = EMSG;	/* option letter processing */
1829 	static char	atend = 0;
1830 	register char	*oli;		/* option letter list index */
1831 
1832 	if (atend) {
1833 		atend = 0;
1834 		place = EMSG;
1835 	}
1836 	if(!*place) {			/* update scanning pointer */
1837 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1838 			atend++;
1839 			return(EOF);
1840 		}
1841 		if (*place == '-') {	/* found "--" */
1842 			++optind;
1843 			atend++;
1844 			return(EOF);
1845 		}
1846 	}				/* option letter okay? */
1847 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1848 		if (!*place) ++optind;
1849 		tell(": illegal option -- ");
1850 	}
1851 	if (*++oli != ':') {		/* don't need argument */
1852 		optarg = NULL;
1853 		if (!*place) ++optind;
1854 	}
1855 	else {				/* need an argument */
1856 		if (*place) optarg = place;	/* no white space */
1857 		else if (nargc <= ++optind) {	/* no arg */
1858 			place = EMSG;
1859 			tell(": option requires an argument -- ");
1860 		}
1861 	 	else optarg = nargv[optind];	/* white space */
1862 		place = EMSG;
1863 		++optind;
1864 	}
1865 	return(optopt);			/* dump back option letter */
1866 }
1867 
1868 #endif
1869 /*
1870 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1871 */
1872 
1873 #ifdef NEEDVPRINTF
1874 
1875 #define MAXARG	16
1876 
1877 vfprintf(fp, fmt, ap)
1878 	FILE *	fp;
1879 	char *	fmt;
1880 	char **	ap;
1881 {
1882 	char *	bp[MAXARG];
1883 	int	i = 0;
1884 
1885 	while (*ap && i < MAXARG)
1886 		bp[i++] = *ap++;
1887 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1888 			 bp[4], bp[5], bp[6], bp[7],
1889 			 bp[8], bp[9], bp[10], bp[11],
1890 			 bp[12], bp[13], bp[14], bp[15]);
1891 }
1892 
1893 vsprintf(s, fmt, ap)
1894 	char *	s;
1895 	char *	fmt;
1896 	char **	ap;
1897 {
1898 	char *	bp[MAXARG];
1899 	int	i = 0;
1900 
1901 	while (*ap && i < MAXARG)
1902 		bp[i++] = *ap++;
1903 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1904 			bp[4], bp[5], bp[6], bp[7],
1905 			bp[8], bp[9], bp[10], bp[11],
1906 			bp[12], bp[13], bp[14], bp[15]);
1907 }
1908 
1909 #endif
1910 /*
1911 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
1912 **
1913 **	Parameters:
1914 **		shell -- the user's shell from /etc/passwd
1915 **
1916 **	Returns:
1917 **		TRUE -- if it is ok to use this for unrestricted access.
1918 **		FALSE -- if the shell is restricted.
1919 */
1920 
1921 #if !HASGETUSERSHELL
1922 
1923 # ifndef _PATH_SHELLS
1924 #  define _PATH_SHELLS	"/etc/shells"
1925 # endif
1926 
1927 char	*DefaultUserShells[] =
1928 {
1929 	"/bin/sh",		/* standard shell */
1930 	"/usr/bin/sh",
1931 	"/bin/csh",		/* C shell */
1932 	"/usr/bin/csh",
1933 #ifdef __hpux
1934 	"/bin/rsh",		/* restricted Bourne shell */
1935 	"/bin/ksh",		/* Korn shell */
1936 	"/bin/rksh",		/* restricted Korn shell */
1937 	"/bin/pam",
1938 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
1939 	"/bin/posix/sh",
1940 #endif
1941 #ifdef _AIX3
1942 	"/bin/ksh",		/* Korn shell */
1943 	"/usr/bin/ksh",
1944 	"/bin/tsh",		/* trusted shell */
1945 	"/usr/bin/tsh",
1946 	"/bin/bsh",		/* Bourne shell */
1947 	"/usr/bin/bsh",
1948 #endif
1949 	NULL
1950 };
1951 
1952 #endif
1953 
1954 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
1955 
1956 bool
1957 usershellok(shell)
1958 	char *shell;
1959 {
1960 #if HASGETUSERSHELL
1961 	register char *p;
1962 	extern char *getusershell();
1963 
1964 	setusershell();
1965 	while ((p = getusershell()) != NULL)
1966 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
1967 			break;
1968 	endusershell();
1969 	return p != NULL;
1970 #else
1971 	register FILE *shellf;
1972 	char buf[MAXLINE];
1973 
1974 	shellf = fopen(_PATH_SHELLS, "r");
1975 	if (shellf == NULL)
1976 	{
1977 		/* no /etc/shells; see if it is one of the std shells */
1978 		char **d;
1979 
1980 		for (d = DefaultUserShells; *d != NULL; d++)
1981 		{
1982 			if (strcmp(shell, *d) == 0)
1983 				return TRUE;
1984 		}
1985 		return FALSE;
1986 	}
1987 
1988 	while (fgets(buf, sizeof buf, shellf) != NULL)
1989 	{
1990 		register char *p, *q;
1991 
1992 		p = buf;
1993 		while (*p != '\0' && *p != '#' && *p != '/')
1994 			p++;
1995 		if (*p == '#' || *p == '\0')
1996 			continue;
1997 		q = p;
1998 		while (*p != '\0' && *p != '#' && !isspace(*p))
1999 			p++;
2000 		*p = '\0';
2001 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
2002 		{
2003 			fclose(shellf);
2004 			return TRUE;
2005 		}
2006 	}
2007 	fclose(shellf);
2008 	return FALSE;
2009 #endif
2010 }
2011 /*
2012 **  FREESPACE -- see how much free space is on the queue filesystem
2013 **
2014 **	Only implemented if you have statfs.
2015 **
2016 **	Parameters:
2017 **		dir -- the directory in question.
2018 **		bsize -- a variable into which the filesystem
2019 **			block size is stored.
2020 **
2021 **	Returns:
2022 **		The number of bytes free on the queue filesystem.
2023 **		-1 if the statfs call fails.
2024 **
2025 **	Side effects:
2026 **		Puts the filesystem block size into bsize.
2027 */
2028 
2029 /* statfs types */
2030 #define SFS_NONE	0	/* no statfs implementation */
2031 #define SFS_USTAT	1	/* use ustat */
2032 #define SFS_4ARGS	2	/* use four-argument statfs call */
2033 #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
2034 #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
2035 #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
2036 #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
2037 
2038 #ifndef SFS_TYPE
2039 # define SFS_TYPE	SFS_NONE
2040 #endif
2041 
2042 #if SFS_TYPE == SFS_USTAT
2043 # include <ustat.h>
2044 #endif
2045 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
2046 # include <sys/statfs.h>
2047 #endif
2048 #if SFS_TYPE == SFS_VFS
2049 # include <sys/vfs.h>
2050 #endif
2051 #if SFS_TYPE == SFS_MOUNT
2052 # include <sys/mount.h>
2053 #endif
2054 #if SFS_TYPE == SFS_STATVFS
2055 # include <sys/statvfs.h>
2056 #endif
2057 
2058 long
2059 freespace(dir, bsize)
2060 	char *dir;
2061 	long *bsize;
2062 {
2063 #if SFS_TYPE != SFS_NONE
2064 # if SFS_TYPE == SFS_USTAT
2065 	struct ustat fs;
2066 	struct stat statbuf;
2067 #  define FSBLOCKSIZE	DEV_BSIZE
2068 #  define f_bavail	f_tfree
2069 # else
2070 #  if defined(ultrix)
2071 	struct fs_data fs;
2072 #   define f_bavail	fd_bfreen
2073 #   define FSBLOCKSIZE	1024L
2074 #  else
2075 #   if SFS_TYPE == SFS_STATVFS
2076 	struct statvfs fs;
2077 #    define FSBLOCKSIZE	fs.f_frsize
2078 #   else
2079 	struct statfs fs;
2080 #    define FSBLOCKSIZE	fs.f_bsize
2081 #    if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) || defined(ALTOS_SYS_V) || defined(_UTS)
2082 #     define f_bavail f_bfree
2083 #    endif
2084 #   endif
2085 #  endif
2086 # endif
2087 	extern int errno;
2088 
2089 # if SFS_TYPE == SFS_USTAT
2090 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
2091 # else
2092 #  if SFS_TYPE == SFS_4ARGS
2093 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
2094 #  else
2095 #   if SFS_TYPE == SFS_STATVFS
2096 	if (statvfs(dir, &fs) == 0)
2097 #   else
2098 #    if defined(ultrix)
2099 	if (statfs(dir, &fs) > 0)
2100 #    else
2101 	if (statfs(dir, &fs) == 0)
2102 #    endif
2103 #   endif
2104 #  endif
2105 # endif
2106 	{
2107 		if (bsize != NULL)
2108 			*bsize = FSBLOCKSIZE;
2109 		return (fs.f_bavail);
2110 	}
2111 #endif
2112 	return (-1);
2113 }
2114 /*
2115 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
2116 **
2117 **	Only implemented if you have statfs.
2118 **
2119 **	Parameters:
2120 **		msize -- the size to check against.  If zero, we don't yet
2121 **		know how big the message will be, so just check for
2122 **		a "reasonable" amount.
2123 **
2124 **	Returns:
2125 **		TRUE if there is enough space.
2126 **		FALSE otherwise.
2127 */
2128 
2129 bool
2130 enoughspace(msize)
2131 	long msize;
2132 {
2133 	long bfree, bsize;
2134 
2135 	if (MinBlocksFree <= 0 && msize <= 0)
2136 	{
2137 		if (tTd(4, 80))
2138 			printf("enoughspace: no threshold\n");
2139 		return TRUE;
2140 	}
2141 
2142 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
2143 	{
2144 		if (tTd(4, 80))
2145 			printf("enoughspace: bavail=%ld, need=%ld\n",
2146 				bfree, msize);
2147 
2148 		/* convert msize to block count */
2149 		msize = msize / bsize + 1;
2150 		if (MinBlocksFree >= 0)
2151 			msize += MinBlocksFree;
2152 
2153 		if (bfree < msize)
2154 		{
2155 #ifdef LOG
2156 			if (LogLevel > 0)
2157 				syslog(LOG_ALERT,
2158 					"%s: low on space (have %ld, %s needs %ld in %s)",
2159 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
2160 					bfree,
2161 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
2162 					msize, QueueDir);
2163 #endif
2164 			return FALSE;
2165 		}
2166 	}
2167 	else if (tTd(4, 80))
2168 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
2169 			MinBlocksFree, msize, errstring(errno));
2170 	return TRUE;
2171 }
2172 /*
2173 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
2174 **
2175 **	This looks at an errno value and tells if this is likely to
2176 **	go away if retried later.
2177 **
2178 **	Parameters:
2179 **		err -- the errno code to classify.
2180 **
2181 **	Returns:
2182 **		TRUE if this is probably transient.
2183 **		FALSE otherwise.
2184 */
2185 
2186 bool
2187 transienterror(err)
2188 	int err;
2189 {
2190 	switch (err)
2191 	{
2192 	  case EIO:			/* I/O error */
2193 	  case ENXIO:			/* Device not configured */
2194 	  case EAGAIN:			/* Resource temporarily unavailable */
2195 	  case ENOMEM:			/* Cannot allocate memory */
2196 	  case ENODEV:			/* Operation not supported by device */
2197 	  case ENFILE:			/* Too many open files in system */
2198 	  case EMFILE:			/* Too many open files */
2199 	  case ENOSPC:			/* No space left on device */
2200 #ifdef ETIMEDOUT
2201 	  case ETIMEDOUT:		/* Connection timed out */
2202 #endif
2203 #ifdef ESTALE
2204 	  case ESTALE:			/* Stale NFS file handle */
2205 #endif
2206 #ifdef ENETDOWN
2207 	  case ENETDOWN:		/* Network is down */
2208 #endif
2209 #ifdef ENETUNREACH
2210 	  case ENETUNREACH:		/* Network is unreachable */
2211 #endif
2212 #ifdef ENETRESET
2213 	  case ENETRESET:		/* Network dropped connection on reset */
2214 #endif
2215 #ifdef ECONNABORTED
2216 	  case ECONNABORTED:		/* Software caused connection abort */
2217 #endif
2218 #ifdef ECONNRESET
2219 	  case ECONNRESET:		/* Connection reset by peer */
2220 #endif
2221 #ifdef ENOBUFS
2222 	  case ENOBUFS:			/* No buffer space available */
2223 #endif
2224 #ifdef ESHUTDOWN
2225 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
2226 #endif
2227 #ifdef ECONNREFUSED
2228 	  case ECONNREFUSED:		/* Connection refused */
2229 #endif
2230 #ifdef EHOSTDOWN
2231 	  case EHOSTDOWN:		/* Host is down */
2232 #endif
2233 #ifdef EHOSTUNREACH
2234 	  case EHOSTUNREACH:		/* No route to host */
2235 #endif
2236 #ifdef EDQUOT
2237 	  case EDQUOT:			/* Disc quota exceeded */
2238 #endif
2239 #ifdef EPROCLIM
2240 	  case EPROCLIM:		/* Too many processes */
2241 #endif
2242 #ifdef EUSERS
2243 	  case EUSERS:			/* Too many users */
2244 #endif
2245 #ifdef EDEADLK
2246 	  case EDEADLK:			/* Resource deadlock avoided */
2247 #endif
2248 #ifdef EISCONN
2249 	  case EISCONN:			/* Socket already connected */
2250 #endif
2251 #ifdef EINPROGRESS
2252 	  case EINPROGRESS:		/* Operation now in progress */
2253 #endif
2254 #ifdef EALREADY
2255 	  case EALREADY:		/* Operation already in progress */
2256 #endif
2257 #ifdef EADDRINUSE
2258 	  case EADDRINUSE:		/* Address already in use */
2259 #endif
2260 #ifdef EADDRNOTAVAIL
2261 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
2262 #endif
2263 #ifdef ETXTBSY
2264 	  case ETXTBSY:			/* (Apollo) file locked */
2265 #endif
2266 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
2267 	  case ENOSR:			/* Out of streams resources */
2268 #endif
2269 		return TRUE;
2270 	}
2271 
2272 	/* nope, must be permanent */
2273 	return FALSE;
2274 }
2275 /*
2276 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
2277 **
2278 **	Parameters:
2279 **		fd -- the file descriptor of the file.
2280 **		filename -- the file name (for error messages).
2281 **		ext -- the filename extension.
2282 **		type -- type of the lock.  Bits can be:
2283 **			LOCK_EX -- exclusive lock.
2284 **			LOCK_NB -- non-blocking.
2285 **
2286 **	Returns:
2287 **		TRUE if the lock was acquired.
2288 **		FALSE otherwise.
2289 */
2290 
2291 bool
2292 lockfile(fd, filename, ext, type)
2293 	int fd;
2294 	char *filename;
2295 	char *ext;
2296 	int type;
2297 {
2298 # if !HASFLOCK
2299 	int action;
2300 	struct flock lfd;
2301 
2302 	if (ext == NULL)
2303 		ext = "";
2304 
2305 	bzero(&lfd, sizeof lfd);
2306 	if (bitset(LOCK_UN, type))
2307 		lfd.l_type = F_UNLCK;
2308 	else if (bitset(LOCK_EX, type))
2309 		lfd.l_type = F_WRLCK;
2310 	else
2311 		lfd.l_type = F_RDLCK;
2312 
2313 	if (bitset(LOCK_NB, type))
2314 		action = F_SETLK;
2315 	else
2316 		action = F_SETLKW;
2317 
2318 	if (tTd(55, 60))
2319 		printf("lockfile(%s%s, action=%d, type=%d): ",
2320 			filename, ext, action, lfd.l_type);
2321 
2322 	if (fcntl(fd, action, &lfd) >= 0)
2323 	{
2324 		if (tTd(55, 60))
2325 			printf("SUCCESS\n");
2326 		return TRUE;
2327 	}
2328 
2329 	if (tTd(55, 60))
2330 		printf("(%s) ", errstring(errno));
2331 
2332 	/*
2333 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
2334 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
2335 	**  as type "tmp" (that is, served from swap space), the
2336 	**  previous fcntl will fail with "Invalid argument" errors.
2337 	**  Since this is fairly common during testing, we will assume
2338 	**  that this indicates that the lock is successfully grabbed.
2339 	*/
2340 
2341 	if (errno == EINVAL)
2342 	{
2343 		if (tTd(55, 60))
2344 			printf("SUCCESS\n");
2345 		return TRUE;
2346 	}
2347 
2348 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
2349 	{
2350 		int omode = -1;
2351 #  ifdef F_GETFL
2352 		int oerrno = errno;
2353 
2354 		(void) fcntl(fd, F_GETFL, &omode);
2355 		errno = oerrno;
2356 #  endif
2357 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2358 			filename, ext, fd, type, omode, geteuid());
2359 	}
2360 # else
2361 	if (ext == NULL)
2362 		ext = "";
2363 
2364 	if (tTd(55, 60))
2365 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
2366 
2367 	if (flock(fd, type) >= 0)
2368 	{
2369 		if (tTd(55, 60))
2370 			printf("SUCCESS\n");
2371 		return TRUE;
2372 	}
2373 
2374 	if (tTd(55, 60))
2375 		printf("(%s) ", errstring(errno));
2376 
2377 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
2378 	{
2379 		int omode = -1;
2380 #  ifdef F_GETFL
2381 		int oerrno = errno;
2382 
2383 		(void) fcntl(fd, F_GETFL, &omode);
2384 		errno = oerrno;
2385 #  endif
2386 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2387 			filename, ext, fd, type, omode, geteuid());
2388 	}
2389 # endif
2390 	if (tTd(55, 60))
2391 		printf("FAIL\n");
2392 	return FALSE;
2393 }
2394 /*
2395 **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
2396 **
2397 **	Parameters:
2398 **		fd -- the file descriptor to check.
2399 **
2400 **	Returns:
2401 **		TRUE -- if only root can chown the file to an arbitrary
2402 **			user.
2403 **		FALSE -- if an arbitrary user can give away a file.
2404 */
2405 
2406 bool
2407 chownsafe(fd)
2408 	int fd;
2409 {
2410 #ifdef __hpux
2411 	char *s;
2412 	int tfd;
2413 	uid_t o_uid, o_euid;
2414 	gid_t o_gid, o_egid;
2415 	bool rval;
2416 	struct stat stbuf;
2417 
2418 	o_uid = getuid();
2419 	o_euid = geteuid();
2420 	o_gid = getgid();
2421 	o_egid = getegid();
2422 	fstat(fd, &stbuf);
2423 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
2424 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
2425 	s = tmpnam(NULL);
2426 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
2427 	rval = fchown(tfd, DefUid, DefGid) != 0;
2428 	close(tfd);
2429 	unlink(s);
2430 	setreuid(o_uid, o_euid);
2431 	setresgid(o_gid, o_egid, -1);
2432 	return rval;
2433 #else
2434 # ifdef _POSIX_CHOWN_RESTRICTED
2435 #  if _POSIX_CHOWN_RESTRICTED == -1
2436 	return FALSE;
2437 #  else
2438 	return TRUE;
2439 #  endif
2440 # else
2441 #  ifdef _PC_CHOWN_RESTRICTED
2442 	int rval;
2443 
2444 	/*
2445 	**  Some systems (e.g., SunOS) seem to have the call and the
2446 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
2447 	**  the call.  This heuristic checks for that.
2448 	*/
2449 
2450 	errno = 0;
2451 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
2452 	if (errno == 0)
2453 		return rval > 0;
2454 #  endif
2455 #  ifdef BSD
2456 	return TRUE;
2457 #  else
2458 	return FALSE;
2459 #  endif
2460 # endif
2461 #endif
2462 }
2463 /*
2464 **  RESETLIMITS -- reset system controlled resource limits
2465 **
2466 **	This is to avoid denial-of-service attacks
2467 **
2468 **	Parameters:
2469 **		none
2470 **
2471 **	Returns:
2472 **		none
2473 */
2474 
2475 #if HASSETRLIMIT
2476 # include <sys/resource.h>
2477 #endif
2478 
2479 resetlimits()
2480 {
2481 #if HASSETRLIMIT
2482 	struct rlimit lim;
2483 
2484 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
2485 	(void) setrlimit(RLIMIT_CPU, &lim);
2486 	(void) setrlimit(RLIMIT_FSIZE, &lim);
2487 #else
2488 # if HASULIMIT
2489 	(void) ulimit(2, 0x3fffff);
2490 # endif
2491 #endif
2492 }
2493 /*
2494 **  GETCFNAME -- return the name of the .cf file.
2495 **
2496 **	Some systems (e.g., NeXT) determine this dynamically.
2497 */
2498 
2499 char *
2500 getcfname()
2501 {
2502 	if (ConfFile != NULL)
2503 		return ConfFile;
2504 #ifdef NETINFO
2505 	{
2506 		extern char *ni_propval();
2507 		char *cflocation;
2508 
2509 		cflocation = ni_propval("/locations", NULL, "sendmail",
2510 					"sendmail.cf", '\0');
2511 		if (cflocation != NULL)
2512 			return cflocation;
2513 	}
2514 #endif
2515 	return _PATH_SENDMAILCF;
2516 }
2517 /*
2518 **  SETVENDOR -- process vendor code from V configuration line
2519 **
2520 **	Parameters:
2521 **		vendor -- string representation of vendor.
2522 **
2523 **	Returns:
2524 **		TRUE -- if ok.
2525 **		FALSE -- if vendor code could not be processed.
2526 **
2527 **	Side Effects:
2528 **		It is reasonable to set mode flags here to tweak
2529 **		processing in other parts of the code if necessary.
2530 **		For example, if you are a vendor that uses $%y to
2531 **		indicate YP lookups, you could enable that here.
2532 */
2533 
2534 bool
2535 setvendor(vendor)
2536 	char *vendor;
2537 {
2538 	if (strcasecmp(vendor, "Berkeley") == 0)
2539 	{
2540 		VendorCode = VENDOR_BERKELEY;
2541 		return TRUE;
2542 	}
2543 
2544 	/* add vendor extensions here */
2545 
2546 #ifdef SUN_EXTENSIONS
2547 	if (strcasecmp(vendor, "Sun") == 0)
2548 	{
2549 		VendorCode = VENDOR_SUN;
2550 		return TRUE;
2551 	}
2552 #endif
2553 
2554 	return FALSE;
2555 }
2556 /*
2557 **  STRTOL -- convert string to long integer
2558 **
2559 **	For systems that don't have it in the C library.
2560 **
2561 **	This is taken verbatim from the 4.4-Lite C library.
2562 */
2563 
2564 #ifdef NEEDSTRTOL
2565 
2566 #if defined(LIBC_SCCS) && !defined(lint)
2567 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
2568 #endif /* LIBC_SCCS and not lint */
2569 
2570 #include <limits.h>
2571 
2572 /*
2573  * Convert a string to a long integer.
2574  *
2575  * Ignores `locale' stuff.  Assumes that the upper and lower case
2576  * alphabets and digits are each contiguous.
2577  */
2578 
2579 long
2580 strtol(nptr, endptr, base)
2581 	const char *nptr;
2582 	char **endptr;
2583 	register int base;
2584 {
2585 	register const char *s = nptr;
2586 	register unsigned long acc;
2587 	register int c;
2588 	register unsigned long cutoff;
2589 	register int neg = 0, any, cutlim;
2590 
2591 	/*
2592 	 * Skip white space and pick up leading +/- sign if any.
2593 	 * If base is 0, allow 0x for hex and 0 for octal, else
2594 	 * assume decimal; if base is already 16, allow 0x.
2595 	 */
2596 	do {
2597 		c = *s++;
2598 	} while (isspace(c));
2599 	if (c == '-') {
2600 		neg = 1;
2601 		c = *s++;
2602 	} else if (c == '+')
2603 		c = *s++;
2604 	if ((base == 0 || base == 16) &&
2605 	    c == '0' && (*s == 'x' || *s == 'X')) {
2606 		c = s[1];
2607 		s += 2;
2608 		base = 16;
2609 	}
2610 	if (base == 0)
2611 		base = c == '0' ? 8 : 10;
2612 
2613 	/*
2614 	 * Compute the cutoff value between legal numbers and illegal
2615 	 * numbers.  That is the largest legal value, divided by the
2616 	 * base.  An input number that is greater than this value, if
2617 	 * followed by a legal input character, is too big.  One that
2618 	 * is equal to this value may be valid or not; the limit
2619 	 * between valid and invalid numbers is then based on the last
2620 	 * digit.  For instance, if the range for longs is
2621 	 * [-2147483648..2147483647] and the input base is 10,
2622 	 * cutoff will be set to 214748364 and cutlim to either
2623 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
2624 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
2625 	 * the number is too big, and we will return a range error.
2626 	 *
2627 	 * Set any if any `digits' consumed; make it negative to indicate
2628 	 * overflow.
2629 	 */
2630 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
2631 	cutlim = cutoff % (unsigned long)base;
2632 	cutoff /= (unsigned long)base;
2633 	for (acc = 0, any = 0;; c = *s++) {
2634 		if (isdigit(c))
2635 			c -= '0';
2636 		else if (isalpha(c))
2637 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
2638 		else
2639 			break;
2640 		if (c >= base)
2641 			break;
2642 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
2643 			any = -1;
2644 		else {
2645 			any = 1;
2646 			acc *= base;
2647 			acc += c;
2648 		}
2649 	}
2650 	if (any < 0) {
2651 		acc = neg ? LONG_MIN : LONG_MAX;
2652 		errno = ERANGE;
2653 	} else if (neg)
2654 		acc = -acc;
2655 	if (endptr != 0)
2656 		*endptr = (char *)(any ? s - 1 : nptr);
2657 	return (acc);
2658 }
2659 
2660 #endif
2661 /*
2662 **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
2663 **
2664 **	Solaris versions at least through 2.3 don't properly deliver a
2665 **	canonical h_name field.  This tries to work around it.
2666 */
2667 
2668 #ifdef SOLARIS
2669 
2670 extern int	h_errno;
2671 
2672 struct hostent *
2673 solaris_gethostbyname(name)
2674 	const char *name;
2675 {
2676 # ifdef SOLARIS_2_3
2677 	static struct hostent hp;
2678 	static char buf[1000];
2679 	extern struct hostent *_switch_gethostbyname_r();
2680 
2681 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
2682 # else
2683 	extern struct hostent *__switch_gethostbyname();
2684 
2685 	return __switch_gethostbyname(name);
2686 # endif
2687 }
2688 
2689 struct hostent *
2690 solaris_gethostbyaddr(addr, len, type)
2691 	const char *addr;
2692 	int len;
2693 	int type;
2694 {
2695 # ifdef SOLARIS_2_3
2696 	static struct hostent hp;
2697 	static char buf[1000];
2698 	extern struct hostent *_switch_gethostbyaddr_r();
2699 
2700 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
2701 # else
2702 	extern struct hostent *__switch_gethostbyaddr();
2703 
2704 	return __switch_gethostbyaddr(addr, len, type);
2705 # endif
2706 }
2707 
2708 #endif
2709 /*
2710 **  NI_PROPVAL -- netinfo property value lookup routine
2711 **
2712 **	Parameters:
2713 **		keydir -- the Netinfo directory name in which to search
2714 **			for the key.
2715 **		keyprop -- the name of the property in which to find the
2716 **			property we are interested.  Defaults to "name".
2717 **		keyval -- the value for which we are really searching.
2718 **		valprop -- the property name for the value in which we
2719 **			are interested.
2720 **		sepchar -- if non-nil, this can be multiple-valued, and
2721 **			we should return a string separated by this
2722 **			character.
2723 **
2724 **	Returns:
2725 **		NULL -- if:
2726 **			1. the directory is not found
2727 **			2. the property name is not found
2728 **			3. the property contains multiple values
2729 **			4. some error occured
2730 **		else -- the location of the config file.
2731 **
2732 **	Example:
2733 **		To search for an alias value, use:
2734 **		  ni_propval("/aliases", "name", aliasname, "members", ',')
2735 **
2736 **	Notes:
2737 **      	Caller should free the return value of ni_proval
2738 */
2739 
2740 #ifdef NETINFO
2741 
2742 # include <netinfo/ni.h>
2743 
2744 # define LOCAL_NETINFO_DOMAIN    "."
2745 # define PARENT_NETINFO_DOMAIN   ".."
2746 # define MAX_NI_LEVELS           256
2747 
2748 char *
2749 ni_propval(keydir, keyprop, keyval, valprop, sepchar)
2750 	char *keydir;
2751 	char *keyprop;
2752 	char *keyval;
2753 	char *valprop;
2754 	char sepchar;
2755 {
2756 	char *propval = NULL;
2757 	int i;
2758 	int j, alen;
2759 	void *ni = NULL;
2760 	void *lastni = NULL;
2761 	ni_status nis;
2762 	ni_id nid;
2763 	ni_namelist ninl;
2764 	register char *p;
2765 	char keybuf[1024];
2766 
2767 	/*
2768 	**  Create the full key from the two parts.
2769 	**
2770 	**	Note that directory can end with, e.g., "name=" to specify
2771 	**	an alternate search property.
2772 	*/
2773 
2774 	i = strlen(keydir) + strlen(keyval) + 2;
2775 	if (keyprop != NULL)
2776 		i += strlen(keyprop) + 1;
2777 	if (i > sizeof keybuf)
2778 		return NULL;
2779 	strcpy(keybuf, keydir);
2780 	strcat(keybuf, "/");
2781 	if (keyprop != NULL)
2782 	{
2783 		strcat(keybuf, keyprop);
2784 		strcat(keybuf, "=");
2785 	}
2786 	strcat(keybuf, keyval);
2787 
2788 	/*
2789 	**  If the passed directory and property name are found
2790 	**  in one of netinfo domains we need to search (starting
2791 	**  from the local domain moving all the way back to the
2792 	**  root domain) set propval to the property's value
2793 	**  and return it.
2794 	*/
2795 
2796 	for (i = 0; i < MAX_NI_LEVELS; ++i)
2797 	{
2798 		if (i == 0)
2799 		{
2800 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
2801 		}
2802 		else
2803 		{
2804 			if (lastni != NULL)
2805 				ni_free(lastni);
2806 			lastni = ni;
2807 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
2808 		}
2809 
2810 		/*
2811 		**  Don't bother if we didn't get a handle on a
2812 		**  proper domain.  This is not necessarily an error.
2813 		**  We would get a positive ni_status if, for instance
2814 		**  we never found the directory or property and tried
2815 		**  to open the parent of the root domain!
2816 		*/
2817 
2818 		if (nis != 0)
2819 			break;
2820 
2821 		/*
2822 		**  Find the path to the server information.
2823 		*/
2824 
2825 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
2826 			continue;
2827 
2828 		/*
2829 		**  Find associated value information.
2830 		*/
2831 
2832 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
2833 			continue;
2834 
2835 		/*
2836 		**  See if we have an acceptable number of values.
2837 		*/
2838 
2839 		if (ninl.ni_namelist_len <= 0)
2840 			continue;
2841 
2842 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
2843 		{
2844 			ni_namelist_free(&ninl);
2845 			continue;
2846 		}
2847 
2848 		/*
2849 		**  Calculate number of bytes needed and build result
2850 		*/
2851 
2852 		alen = 1;
2853 		for (j = 0; j < ninl.ni_namelist_len; j++)
2854 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
2855 		propval = p = xalloc(alen);
2856 		for (j = 0; j < ninl.ni_namelist_len; j++)
2857 		{
2858 			strcpy(p, ninl.ni_namelist_val[j]);
2859 			p += strlen(p);
2860 			*p++ = sepchar;
2861 		}
2862 		*--p = '\0';
2863 
2864 		ni_namelist_free(&ninl);
2865 	}
2866 
2867 	/*
2868 	**  Clean up.
2869 	*/
2870 
2871 	if (ni != NULL)
2872 		ni_free(ni);
2873 	if (lastni != NULL && ni != lastni)
2874 		ni_free(lastni);
2875 
2876 	return propval;
2877 }
2878 
2879 #endif /* NETINFO */
2880 /*
2881 **  HARD_SYSLOG -- call syslog repeatedly until it works
2882 **
2883 **	Needed on HP-UX, which apparently doesn't guarantee that
2884 **	syslog succeeds during interrupt handlers.
2885 */
2886 
2887 #ifdef __hpux
2888 
2889 # define MAXSYSLOGTRIES	100
2890 # undef syslog
2891 
2892 # ifdef __STDC__
2893 hard_syslog(int pri, char *msg, ...)
2894 # else
2895 hard_syslog(pri, msg, va_alist)
2896 	int pri;
2897 	char *msg;
2898 	va_dcl
2899 # endif
2900 {
2901 	int i;
2902 	char buf[SYSLOG_BUFSIZE * 2];
2903 	VA_LOCAL_DECL;
2904 
2905 	VA_START(msg);
2906 	vsprintf(buf, msg, ap);
2907 	VA_END;
2908 
2909 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
2910 		continue;
2911 }
2912 
2913 #endif
2914