xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision 995e0890)
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.131 (Berkeley) 02/19/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
1150 getla()
1151 {
1152 	struct dg_sys_info_load_info load_info;
1153 
1154 	dg_sys_info((long *)&load_info,
1155 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
1156 
1157         if (tTd(3, 1))
1158                 printf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1159 
1160 	return((int) (load_info.one_minute + 0.5));
1161 }
1162 
1163 #else
1164 # ifdef __hpux
1165 
1166 #  include <sys/param.h>
1167 #  include <sys/pstat.h>
1168 
1169 int
1170 getla()
1171 {
1172 	struct pst_dynamic pstd;
1173 
1174 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1175 			     (size_t) 1 ,0) == -1)
1176 		return 0;
1177 
1178         if (tTd(3, 1))
1179                 printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1180 
1181 	return (int) (pstd.psd_avg_1_min + 0.5);
1182 }
1183 
1184 # else
1185 
1186 int
1187 getla()
1188 {
1189 	double avenrun[3];
1190 
1191 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
1192 	{
1193 		if (tTd(3, 1))
1194 			perror("getla: getloadavg failed:");
1195 		return (-1);
1196 	}
1197 	if (tTd(3, 1))
1198 		printf("getla: %d\n", (int) (avenrun[0] +0.5));
1199 	return ((int) (avenrun[0] + 0.5));
1200 }
1201 
1202 # endif /* __hpux */
1203 #endif /* DGUX */
1204 #else
1205 #if LA_TYPE == LA_MACH
1206 
1207 /*
1208 **  This has been tested on NEXTSTEP release 2.1/3.X.
1209 */
1210 
1211 #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
1212 # include <mach/mach.h>
1213 #else
1214 # include <mach.h>
1215 #endif
1216 
1217 getla()
1218 {
1219 	processor_set_t default_set;
1220 	kern_return_t error;
1221 	unsigned int info_count;
1222 	struct processor_set_basic_info info;
1223 	host_t host;
1224 
1225 	error = processor_set_default(host_self(), &default_set);
1226 	if (error != KERN_SUCCESS)
1227 		return -1;
1228 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
1229 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
1230 			       &host, (processor_set_info_t)&info,
1231 			       &info_count) != KERN_SUCCESS)
1232 	{
1233 		return -1;
1234 	}
1235 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
1236 }
1237 
1238 
1239 #else
1240 #if LA_TYPE == LA_PROCSTR
1241 
1242 /*
1243 **  Read /proc/loadavg for the load average.  This is assumed to be
1244 **  in a format like "0.15 0.12 0.06".
1245 **
1246 **	Initially intended for Linux.  This has been in the kernel
1247 **	since at least 0.99.15.
1248 */
1249 
1250 # ifndef _PATH_LOADAVG
1251 #  define _PATH_LOADAVG	"/proc/loadavg"
1252 # endif
1253 
1254 int
1255 getla()
1256 {
1257 	double avenrun;
1258 	register int result;
1259 	FILE *fp;
1260 
1261 	fp = fopen(_PATH_LOADAVG, "r");
1262 	if (fp == NULL)
1263 	{
1264 		if (tTd(3, 1))
1265 			printf("getla: fopen(%s): %s\n",
1266 				_PATH_LOADAVG, errstring(errno));
1267 		return -1;
1268 	}
1269 	result = fscanf(fp, "%lf", &avenrun);
1270 	fclose(fp);
1271 	if (result != 1)
1272 	{
1273 		if (tTd(3, 1))
1274 			printf("getla: fscanf() = %d: %s\n",
1275 				result, errstring(errno));
1276 		return -1;
1277 	}
1278 
1279 	if (tTd(3, 1))
1280 		printf("getla(): %.2f\n", avenrun);
1281 
1282 	return ((int) (avenrun + 0.5));
1283 }
1284 
1285 #else
1286 
1287 getla()
1288 {
1289 	if (tTd(3, 1))
1290 		printf("getla: ZERO\n");
1291 	return (0);
1292 }
1293 
1294 #endif
1295 #endif
1296 #endif
1297 #endif
1298 
1299 
1300 /*
1301  * Copyright 1989 Massachusetts Institute of Technology
1302  *
1303  * Permission to use, copy, modify, distribute, and sell this software and its
1304  * documentation for any purpose is hereby granted without fee, provided that
1305  * the above copyright notice appear in all copies and that both that
1306  * copyright notice and this permission notice appear in supporting
1307  * documentation, and that the name of M.I.T. not be used in advertising or
1308  * publicity pertaining to distribution of the software without specific,
1309  * written prior permission.  M.I.T. makes no representations about the
1310  * suitability of this software for any purpose.  It is provided "as is"
1311  * without express or implied warranty.
1312  *
1313  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
1314  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
1315  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1316  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1317  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1318  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1319  *
1320  * Authors:  Many and varied...
1321  */
1322 
1323 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
1324 #ifndef lint
1325 static char  rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
1326 #endif /* !lint */
1327 
1328 #ifdef apollo
1329 # undef volatile
1330 #    include <apollo/base.h>
1331 
1332 /* ARGSUSED */
1333 int getloadavg( call_data )
1334      caddr_t	call_data;	/* pointer to (double) return value */
1335 {
1336      double *avenrun = (double *) call_data;
1337      int i;
1338      status_$t      st;
1339      long loadav[3];
1340      proc1_$get_loadav(loadav, &st);
1341      *avenrun = loadav[0] / (double) (1 << 16);
1342      return(0);
1343 }
1344 #   endif /* apollo */
1345 /*
1346 **  SHOULDQUEUE -- should this message be queued or sent?
1347 **
1348 **	Compares the message cost to the load average to decide.
1349 **
1350 **	Parameters:
1351 **		pri -- the priority of the message in question.
1352 **		ctime -- the message creation time.
1353 **
1354 **	Returns:
1355 **		TRUE -- if this message should be queued up for the
1356 **			time being.
1357 **		FALSE -- if the load is low enough to send this message.
1358 **
1359 **	Side Effects:
1360 **		none.
1361 */
1362 
1363 bool
1364 shouldqueue(pri, ctime)
1365 	long pri;
1366 	time_t ctime;
1367 {
1368 	bool rval;
1369 
1370 	if (tTd(3, 30))
1371 		printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri);
1372 	if (CurrentLA < QueueLA)
1373 	{
1374 		if (tTd(3, 30))
1375 			printf("FALSE (CurrentLA < QueueLA)\n");
1376 		return (FALSE);
1377 	}
1378 	if (CurrentLA >= RefuseLA)
1379 	{
1380 		if (tTd(3, 30))
1381 			printf("TRUE (CurrentLA >= RefuseLA)\n");
1382 		return (TRUE);
1383 	}
1384 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
1385 	if (tTd(3, 30))
1386 		printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE");
1387 	return rval;
1388 }
1389 /*
1390 **  REFUSECONNECTIONS -- decide if connections should be refused
1391 **
1392 **	Parameters:
1393 **		none.
1394 **
1395 **	Returns:
1396 **		TRUE if incoming SMTP connections should be refused
1397 **			(for now).
1398 **		FALSE if we should accept new work.
1399 **
1400 **	Side Effects:
1401 **		none.
1402 */
1403 
1404 bool
1405 refuseconnections()
1406 {
1407 	extern bool enoughspace();
1408 
1409 #ifdef XLA
1410 	if (!xla_smtp_ok())
1411 		return TRUE;
1412 #endif
1413 
1414 	/* this is probably too simplistic */
1415 	return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1);
1416 }
1417 /*
1418 **  SETPROCTITLE -- set process title for ps
1419 **
1420 **	Parameters:
1421 **		fmt -- a printf style format string.
1422 **		a, b, c -- possible parameters to fmt.
1423 **
1424 **	Returns:
1425 **		none.
1426 **
1427 **	Side Effects:
1428 **		Clobbers argv of our main procedure so ps(1) will
1429 **		display the title.
1430 */
1431 
1432 #define SPT_NONE	0	/* don't use it at all */
1433 #define SPT_REUSEARGV	1	/* cover argv with title information */
1434 #define SPT_BUILTIN	2	/* use libc builtin */
1435 #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
1436 #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
1437 #define SPT_WRITEUDOT	5	/* write u. area in kmem */
1438 
1439 #ifndef SPT_TYPE
1440 # define SPT_TYPE	SPT_REUSEARGV
1441 #endif
1442 
1443 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
1444 
1445 # if SPT_TYPE == SPT_PSTAT
1446 #  include <sys/pstat.h>
1447 # endif
1448 # if SPT_TYPE == SPT_PSSTRINGS
1449 #  include <machine/vmparam.h>
1450 #  include <sys/exec.h>
1451 #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
1452 #   undef SPT_TYPE
1453 #   define SPT_TYPE	SPT_REUSEARGV
1454 #  endif
1455 # endif
1456 
1457 # if SPT_TYPE == SPT_PSSTRINGS
1458 #  define SETPROC_STATIC	static
1459 # else
1460 #  define SETPROC_STATIC
1461 # endif
1462 
1463 # ifndef SPT_PADCHAR
1464 #  define SPT_PADCHAR	' '
1465 # endif
1466 
1467 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
1468 
1469 #if SPT_TYPE != SPT_BUILTIN
1470 
1471 /*VARARGS1*/
1472 # ifdef __STDC__
1473 setproctitle(char *fmt, ...)
1474 # else
1475 setproctitle(fmt, va_alist)
1476 	char *fmt;
1477 	va_dcl
1478 # endif
1479 {
1480 # if SPT_TYPE != SPT_NONE
1481 	register char *p;
1482 	register int i;
1483 	SETPROC_STATIC char buf[MAXLINE];
1484 	VA_LOCAL_DECL
1485 #  if SPT_TYPE == SPT_PSTAT
1486 	union pstun pst;
1487 #  endif
1488 	extern char **Argv;
1489 	extern char *LastArgv;
1490 
1491 	p = buf;
1492 
1493 	/* print sendmail: heading for grep */
1494 	(void) strcpy(p, "sendmail: ");
1495 	p += strlen(p);
1496 
1497 	/* print the argument string */
1498 	VA_START(fmt);
1499 	(void) vsprintf(p, fmt, ap);
1500 	VA_END;
1501 
1502 	i = strlen(buf);
1503 
1504 #  if SPT_TYPE == SPT_PSTAT
1505 	pst.pst_command = buf;
1506 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
1507 #  else
1508 #   if SPT_TYPE == SPT_PSSTRINGS
1509 	PS_STRINGS->ps_nargvstr = 1;
1510 	PS_STRINGS->ps_argvstr = buf;
1511 #   else
1512 	if (i > LastArgv - Argv[0] - 2)
1513 	{
1514 		i = LastArgv - Argv[0] - 2;
1515 		buf[i] = '\0';
1516 	}
1517 	(void) strcpy(Argv[0], buf);
1518 	p = &Argv[0][i];
1519 	while (p < LastArgv)
1520 		*p++ = SPT_PADCHAR;
1521 	Argv[1] = NULL;
1522 #   endif /* SPT_TYPE == SPT_PSSTRINGS */
1523 #  endif /* SPT_TYPE == SPT_PSTAT */
1524 # endif /* SPT_TYPE != SPT_NONE */
1525 }
1526 
1527 #endif /* SPT_TYPE != SPT_BUILTIN */
1528 /*
1529 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
1530 **
1531 **	Parameters:
1532 **		sig -- the signal that got us here (unused).
1533 **
1534 **	Returns:
1535 **		none.
1536 **
1537 **	Side Effects:
1538 **		Picks up extant zombies.
1539 */
1540 
1541 void
1542 reapchild(sig)
1543 	int sig;
1544 {
1545 	int olderrno = errno;
1546 # ifdef HASWAITPID
1547 	auto int status;
1548 	int count;
1549 	int pid;
1550 
1551 	count = 0;
1552 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1553 	{
1554 		if (count++ > 1000)
1555 		{
1556 #ifdef LOG
1557 			syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x",
1558 				pid, status);
1559 #endif
1560 			break;
1561 		}
1562 	}
1563 # else
1564 # ifdef WNOHANG
1565 	union wait status;
1566 
1567 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
1568 		continue;
1569 # else /* WNOHANG */
1570 	auto int status;
1571 
1572 	while (wait(&status) > 0)
1573 		continue;
1574 # endif /* WNOHANG */
1575 # endif
1576 # ifdef SYS5SIGNALS
1577 	(void) setsignal(SIGCHLD, reapchild);
1578 # endif
1579 	errno = olderrno;
1580 }
1581 /*
1582 **  UNSETENV -- remove a variable from the environment
1583 **
1584 **	Not needed on newer systems.
1585 **
1586 **	Parameters:
1587 **		name -- the string name of the environment variable to be
1588 **			deleted from the current environment.
1589 **
1590 **	Returns:
1591 **		none.
1592 **
1593 **	Globals:
1594 **		environ -- a pointer to the current environment.
1595 **
1596 **	Side Effects:
1597 **		Modifies environ.
1598 */
1599 
1600 #ifndef HASUNSETENV
1601 
1602 void
1603 unsetenv(name)
1604 	char *name;
1605 {
1606 	extern char **environ;
1607 	register char **pp;
1608 	int len = strlen(name);
1609 
1610 	for (pp = environ; *pp != NULL; pp++)
1611 	{
1612 		if (strncmp(name, *pp, len) == 0 &&
1613 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1614 			break;
1615 	}
1616 
1617 	for (; *pp != NULL; pp++)
1618 		*pp = pp[1];
1619 }
1620 
1621 #endif
1622 /*
1623 **  GETDTABLESIZE -- return number of file descriptors
1624 **
1625 **	Only on non-BSD systems
1626 **
1627 **	Parameters:
1628 **		none
1629 **
1630 **	Returns:
1631 **		size of file descriptor table
1632 **
1633 **	Side Effects:
1634 **		none
1635 */
1636 
1637 #ifdef SOLARIS
1638 # include <sys/resource.h>
1639 #endif
1640 
1641 int
1642 getdtsize()
1643 {
1644 #ifdef RLIMIT_NOFILE
1645 	struct rlimit rl;
1646 
1647 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1648 		return rl.rlim_cur;
1649 #endif
1650 
1651 # ifdef HASGETDTABLESIZE
1652 	return getdtablesize();
1653 # else
1654 #  ifdef _SC_OPEN_MAX
1655 	return sysconf(_SC_OPEN_MAX);
1656 #  else
1657 	return NOFILE;
1658 #  endif
1659 # endif
1660 }
1661 /*
1662 **  UNAME -- get the UUCP name of this system.
1663 */
1664 
1665 #ifndef HASUNAME
1666 
1667 int
1668 uname(name)
1669 	struct utsname *name;
1670 {
1671 	FILE *file;
1672 	char *n;
1673 
1674 	name->nodename[0] = '\0';
1675 
1676 	/* try /etc/whoami -- one line with the node name */
1677 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1678 	{
1679 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1680 		(void) fclose(file);
1681 		n = strchr(name->nodename, '\n');
1682 		if (n != NULL)
1683 			*n = '\0';
1684 		if (name->nodename[0] != '\0')
1685 			return (0);
1686 	}
1687 
1688 	/* try /usr/include/whoami.h -- has a #define somewhere */
1689 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1690 	{
1691 		char buf[MAXLINE];
1692 
1693 		while (fgets(buf, MAXLINE, file) != NULL)
1694 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1695 					NODE_LENGTH, name->nodename) > 0)
1696 				break;
1697 		(void) fclose(file);
1698 		if (name->nodename[0] != '\0')
1699 			return (0);
1700 	}
1701 
1702 #ifdef TRUST_POPEN
1703 	/*
1704 	**  Popen is known to have security holes.
1705 	*/
1706 
1707 	/* try uuname -l to return local name */
1708 	if ((file = popen("uuname -l", "r")) != NULL)
1709 	{
1710 		(void) fgets(name, NODE_LENGTH + 1, file);
1711 		(void) pclose(file);
1712 		n = strchr(name, '\n');
1713 		if (n != NULL)
1714 			*n = '\0';
1715 		if (name->nodename[0] != '\0')
1716 			return (0);
1717 	}
1718 #endif
1719 
1720 	return (-1);
1721 }
1722 #endif /* HASUNAME */
1723 /*
1724 **  INITGROUPS -- initialize groups
1725 **
1726 **	Stub implementation for System V style systems
1727 */
1728 
1729 #ifndef HASINITGROUPS
1730 
1731 initgroups(name, basegid)
1732 	char *name;
1733 	int basegid;
1734 {
1735 	return 0;
1736 }
1737 
1738 #endif
1739 /*
1740 **  SETSID -- set session id (for non-POSIX systems)
1741 */
1742 
1743 #ifndef HASSETSID
1744 
1745 pid_t
1746 setsid __P ((void))
1747 {
1748 #ifdef TIOCNOTTY
1749 	int fd;
1750 
1751 	fd = open("/dev/tty", O_RDWR, 0);
1752 	if (fd >= 0)
1753 	{
1754 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1755 		(void) close(fd);
1756 	}
1757 #endif /* TIOCNOTTY */
1758 # ifdef SYS5SETPGRP
1759 	return setpgrp();
1760 # else
1761 	return setpgid(0, getpid());
1762 # endif
1763 }
1764 
1765 #endif
1766 /*
1767 **  FSYNC -- dummy fsync
1768 */
1769 
1770 #ifdef NEEDFSYNC
1771 
1772 fsync(fd)
1773 	int fd;
1774 {
1775 # ifdef O_SYNC
1776 	return fcntl(fd, F_SETFL, O_SYNC);
1777 # else
1778 	/* nothing we can do */
1779 	return 0;
1780 # endif
1781 }
1782 
1783 #endif
1784 /*
1785 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1786 **
1787 **	Data General DG/UX version of inet_addr returns a struct in_addr
1788 **	instead of a long.  This patches things.  Only needed on versions
1789 **	prior to 5.4.3.
1790 */
1791 
1792 #ifdef DGUX_5_4_2
1793 
1794 #undef inet_addr
1795 
1796 long
1797 dgux_inet_addr(host)
1798 	char *host;
1799 {
1800 	struct in_addr haddr;
1801 
1802 	haddr = inet_addr(host);
1803 	return haddr.s_addr;
1804 }
1805 
1806 #endif
1807 /*
1808 **  GETOPT -- for old systems or systems with bogus implementations
1809 */
1810 
1811 #ifdef NEEDGETOPT
1812 
1813 /*
1814  * Copyright (c) 1985 Regents of the University of California.
1815  * All rights reserved.  The Berkeley software License Agreement
1816  * specifies the terms and conditions for redistribution.
1817  */
1818 
1819 
1820 /*
1821 ** this version hacked to add `atend' flag to allow state machine
1822 ** to reset if invoked by the program to scan args for a 2nd time
1823 */
1824 
1825 #if defined(LIBC_SCCS) && !defined(lint)
1826 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1827 #endif /* LIBC_SCCS and not lint */
1828 
1829 #include <stdio.h>
1830 
1831 /*
1832  * get option letter from argument vector
1833  */
1834 #ifdef _CONVEX_SOURCE
1835 extern int	optind, opterr;
1836 #else
1837 int	opterr = 1;		/* if error message should be printed */
1838 int	optind = 1;		/* index into parent argv vector */
1839 #endif
1840 int	optopt;			/* character checked for validity */
1841 char	*optarg;		/* argument associated with option */
1842 
1843 #define BADCH	(int)'?'
1844 #define EMSG	""
1845 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
1846 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
1847 
1848 getopt(nargc,nargv,ostr)
1849 	int		nargc;
1850 	char *const	*nargv;
1851 	const char	*ostr;
1852 {
1853 	static char	*place = EMSG;	/* option letter processing */
1854 	static char	atend = 0;
1855 	register char	*oli;		/* option letter list index */
1856 
1857 	if (atend) {
1858 		atend = 0;
1859 		place = EMSG;
1860 	}
1861 	if(!*place) {			/* update scanning pointer */
1862 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
1863 			atend++;
1864 			return(EOF);
1865 		}
1866 		if (*place == '-') {	/* found "--" */
1867 			++optind;
1868 			atend++;
1869 			return(EOF);
1870 		}
1871 	}				/* option letter okay? */
1872 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
1873 		if (!*place) ++optind;
1874 		tell(": illegal option -- ");
1875 	}
1876 	if (*++oli != ':') {		/* don't need argument */
1877 		optarg = NULL;
1878 		if (!*place) ++optind;
1879 	}
1880 	else {				/* need an argument */
1881 		if (*place) optarg = place;	/* no white space */
1882 		else if (nargc <= ++optind) {	/* no arg */
1883 			place = EMSG;
1884 			tell(": option requires an argument -- ");
1885 		}
1886 	 	else optarg = nargv[optind];	/* white space */
1887 		place = EMSG;
1888 		++optind;
1889 	}
1890 	return(optopt);			/* dump back option letter */
1891 }
1892 
1893 #endif
1894 /*
1895 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
1896 */
1897 
1898 #ifdef NEEDVPRINTF
1899 
1900 #define MAXARG	16
1901 
1902 vfprintf(fp, fmt, ap)
1903 	FILE *	fp;
1904 	char *	fmt;
1905 	char **	ap;
1906 {
1907 	char *	bp[MAXARG];
1908 	int	i = 0;
1909 
1910 	while (*ap && i < MAXARG)
1911 		bp[i++] = *ap++;
1912 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
1913 			 bp[4], bp[5], bp[6], bp[7],
1914 			 bp[8], bp[9], bp[10], bp[11],
1915 			 bp[12], bp[13], bp[14], bp[15]);
1916 }
1917 
1918 vsprintf(s, fmt, ap)
1919 	char *	s;
1920 	char *	fmt;
1921 	char **	ap;
1922 {
1923 	char *	bp[MAXARG];
1924 	int	i = 0;
1925 
1926 	while (*ap && i < MAXARG)
1927 		bp[i++] = *ap++;
1928 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
1929 			bp[4], bp[5], bp[6], bp[7],
1930 			bp[8], bp[9], bp[10], bp[11],
1931 			bp[12], bp[13], bp[14], bp[15]);
1932 }
1933 
1934 #endif
1935 /*
1936 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
1937 **
1938 **	Parameters:
1939 **		shell -- the user's shell from /etc/passwd
1940 **
1941 **	Returns:
1942 **		TRUE -- if it is ok to use this for unrestricted access.
1943 **		FALSE -- if the shell is restricted.
1944 */
1945 
1946 #if !HASGETUSERSHELL
1947 
1948 # ifndef _PATH_SHELLS
1949 #  define _PATH_SHELLS	"/etc/shells"
1950 # endif
1951 
1952 char	*DefaultUserShells[] =
1953 {
1954 	"/bin/sh",		/* standard shell */
1955 	"/usr/bin/sh",
1956 	"/bin/csh",		/* C shell */
1957 	"/usr/bin/csh",
1958 #ifdef __hpux
1959 	"/bin/rsh",		/* restricted Bourne shell */
1960 	"/bin/ksh",		/* Korn shell */
1961 	"/bin/rksh",		/* restricted Korn shell */
1962 	"/bin/pam",
1963 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
1964 	"/bin/posix/sh",
1965 #endif
1966 #ifdef _AIX3
1967 	"/bin/ksh",		/* Korn shell */
1968 	"/usr/bin/ksh",
1969 	"/bin/tsh",		/* trusted shell */
1970 	"/usr/bin/tsh",
1971 	"/bin/bsh",		/* Bourne shell */
1972 	"/usr/bin/bsh",
1973 #endif
1974 	NULL
1975 };
1976 
1977 #endif
1978 
1979 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
1980 
1981 bool
1982 usershellok(shell)
1983 	char *shell;
1984 {
1985 #if HASGETUSERSHELL
1986 	register char *p;
1987 	extern char *getusershell();
1988 
1989 	setusershell();
1990 	while ((p = getusershell()) != NULL)
1991 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
1992 			break;
1993 	endusershell();
1994 	return p != NULL;
1995 #else
1996 	register FILE *shellf;
1997 	char buf[MAXLINE];
1998 
1999 	shellf = fopen(_PATH_SHELLS, "r");
2000 	if (shellf == NULL)
2001 	{
2002 		/* no /etc/shells; see if it is one of the std shells */
2003 		char **d;
2004 
2005 		for (d = DefaultUserShells; *d != NULL; d++)
2006 		{
2007 			if (strcmp(shell, *d) == 0)
2008 				return TRUE;
2009 		}
2010 		return FALSE;
2011 	}
2012 
2013 	while (fgets(buf, sizeof buf, shellf) != NULL)
2014 	{
2015 		register char *p, *q;
2016 
2017 		p = buf;
2018 		while (*p != '\0' && *p != '#' && *p != '/')
2019 			p++;
2020 		if (*p == '#' || *p == '\0')
2021 			continue;
2022 		q = p;
2023 		while (*p != '\0' && *p != '#' && !isspace(*p))
2024 			p++;
2025 		*p = '\0';
2026 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
2027 		{
2028 			fclose(shellf);
2029 			return TRUE;
2030 		}
2031 	}
2032 	fclose(shellf);
2033 	return FALSE;
2034 #endif
2035 }
2036 /*
2037 **  FREESPACE -- see how much free space is on the queue filesystem
2038 **
2039 **	Only implemented if you have statfs.
2040 **
2041 **	Parameters:
2042 **		dir -- the directory in question.
2043 **		bsize -- a variable into which the filesystem
2044 **			block size is stored.
2045 **
2046 **	Returns:
2047 **		The number of bytes free on the queue filesystem.
2048 **		-1 if the statfs call fails.
2049 **
2050 **	Side effects:
2051 **		Puts the filesystem block size into bsize.
2052 */
2053 
2054 /* statfs types */
2055 #define SFS_NONE	0	/* no statfs implementation */
2056 #define SFS_USTAT	1	/* use ustat */
2057 #define SFS_4ARGS	2	/* use four-argument statfs call */
2058 #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
2059 #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
2060 #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
2061 #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
2062 
2063 #ifndef SFS_TYPE
2064 # define SFS_TYPE	SFS_NONE
2065 #endif
2066 
2067 #if SFS_TYPE == SFS_USTAT
2068 # include <ustat.h>
2069 #endif
2070 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
2071 # include <sys/statfs.h>
2072 #endif
2073 #if SFS_TYPE == SFS_VFS
2074 # include <sys/vfs.h>
2075 #endif
2076 #if SFS_TYPE == SFS_MOUNT
2077 # include <sys/mount.h>
2078 #endif
2079 #if SFS_TYPE == SFS_STATVFS
2080 # include <sys/statvfs.h>
2081 #endif
2082 
2083 long
2084 freespace(dir, bsize)
2085 	char *dir;
2086 	long *bsize;
2087 {
2088 #if SFS_TYPE != SFS_NONE
2089 # if SFS_TYPE == SFS_USTAT
2090 	struct ustat fs;
2091 	struct stat statbuf;
2092 #  define FSBLOCKSIZE	DEV_BSIZE
2093 #  define f_bavail	f_tfree
2094 # else
2095 #  if defined(ultrix)
2096 	struct fs_data fs;
2097 #   define f_bavail	fd_bfreen
2098 #   define FSBLOCKSIZE	1024L
2099 #  else
2100 #   if SFS_TYPE == SFS_STATVFS
2101 	struct statvfs fs;
2102 #    define FSBLOCKSIZE	fs.f_frsize
2103 #   else
2104 	struct statfs fs;
2105 #    define FSBLOCKSIZE	fs.f_bsize
2106 #    if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) || defined(ALTOS_SYS_V) || defined(_UTS) || defined(_CRAYCOM)
2107 #     define f_bavail f_bfree
2108 #    endif
2109 #   endif
2110 #  endif
2111 # endif
2112 	extern int errno;
2113 
2114 # if SFS_TYPE == SFS_USTAT
2115 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
2116 # else
2117 #  if SFS_TYPE == SFS_4ARGS
2118 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
2119 #  else
2120 #   if SFS_TYPE == SFS_STATVFS
2121 	if (statvfs(dir, &fs) == 0)
2122 #   else
2123 #    if defined(ultrix)
2124 	if (statfs(dir, &fs) > 0)
2125 #    else
2126 	if (statfs(dir, &fs) == 0)
2127 #    endif
2128 #   endif
2129 #  endif
2130 # endif
2131 	{
2132 		if (bsize != NULL)
2133 			*bsize = FSBLOCKSIZE;
2134 		return (fs.f_bavail);
2135 	}
2136 #endif
2137 	return (-1);
2138 }
2139 /*
2140 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
2141 **
2142 **	Only implemented if you have statfs.
2143 **
2144 **	Parameters:
2145 **		msize -- the size to check against.  If zero, we don't yet
2146 **		know how big the message will be, so just check for
2147 **		a "reasonable" amount.
2148 **
2149 **	Returns:
2150 **		TRUE if there is enough space.
2151 **		FALSE otherwise.
2152 */
2153 
2154 bool
2155 enoughspace(msize)
2156 	long msize;
2157 {
2158 	long bfree, bsize;
2159 
2160 	if (MinBlocksFree <= 0 && msize <= 0)
2161 	{
2162 		if (tTd(4, 80))
2163 			printf("enoughspace: no threshold\n");
2164 		return TRUE;
2165 	}
2166 
2167 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
2168 	{
2169 		if (tTd(4, 80))
2170 			printf("enoughspace: bavail=%ld, need=%ld\n",
2171 				bfree, msize);
2172 
2173 		/* convert msize to block count */
2174 		msize = msize / bsize + 1;
2175 		if (MinBlocksFree >= 0)
2176 			msize += MinBlocksFree;
2177 
2178 		if (bfree < msize)
2179 		{
2180 #ifdef LOG
2181 			if (LogLevel > 0)
2182 				syslog(LOG_ALERT,
2183 					"%s: low on space (have %ld, %s needs %ld in %s)",
2184 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
2185 					bfree,
2186 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
2187 					msize, QueueDir);
2188 #endif
2189 			return FALSE;
2190 		}
2191 	}
2192 	else if (tTd(4, 80))
2193 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
2194 			MinBlocksFree, msize, errstring(errno));
2195 	return TRUE;
2196 }
2197 /*
2198 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
2199 **
2200 **	This looks at an errno value and tells if this is likely to
2201 **	go away if retried later.
2202 **
2203 **	Parameters:
2204 **		err -- the errno code to classify.
2205 **
2206 **	Returns:
2207 **		TRUE if this is probably transient.
2208 **		FALSE otherwise.
2209 */
2210 
2211 bool
2212 transienterror(err)
2213 	int err;
2214 {
2215 	switch (err)
2216 	{
2217 	  case EIO:			/* I/O error */
2218 	  case ENXIO:			/* Device not configured */
2219 	  case EAGAIN:			/* Resource temporarily unavailable */
2220 	  case ENOMEM:			/* Cannot allocate memory */
2221 	  case ENODEV:			/* Operation not supported by device */
2222 	  case ENFILE:			/* Too many open files in system */
2223 	  case EMFILE:			/* Too many open files */
2224 	  case ENOSPC:			/* No space left on device */
2225 #ifdef ETIMEDOUT
2226 	  case ETIMEDOUT:		/* Connection timed out */
2227 #endif
2228 #ifdef ESTALE
2229 	  case ESTALE:			/* Stale NFS file handle */
2230 #endif
2231 #ifdef ENETDOWN
2232 	  case ENETDOWN:		/* Network is down */
2233 #endif
2234 #ifdef ENETUNREACH
2235 	  case ENETUNREACH:		/* Network is unreachable */
2236 #endif
2237 #ifdef ENETRESET
2238 	  case ENETRESET:		/* Network dropped connection on reset */
2239 #endif
2240 #ifdef ECONNABORTED
2241 	  case ECONNABORTED:		/* Software caused connection abort */
2242 #endif
2243 #ifdef ECONNRESET
2244 	  case ECONNRESET:		/* Connection reset by peer */
2245 #endif
2246 #ifdef ENOBUFS
2247 	  case ENOBUFS:			/* No buffer space available */
2248 #endif
2249 #ifdef ESHUTDOWN
2250 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
2251 #endif
2252 #ifdef ECONNREFUSED
2253 	  case ECONNREFUSED:		/* Connection refused */
2254 #endif
2255 #ifdef EHOSTDOWN
2256 	  case EHOSTDOWN:		/* Host is down */
2257 #endif
2258 #ifdef EHOSTUNREACH
2259 	  case EHOSTUNREACH:		/* No route to host */
2260 #endif
2261 #ifdef EDQUOT
2262 	  case EDQUOT:			/* Disc quota exceeded */
2263 #endif
2264 #ifdef EPROCLIM
2265 	  case EPROCLIM:		/* Too many processes */
2266 #endif
2267 #ifdef EUSERS
2268 	  case EUSERS:			/* Too many users */
2269 #endif
2270 #ifdef EDEADLK
2271 	  case EDEADLK:			/* Resource deadlock avoided */
2272 #endif
2273 #ifdef EISCONN
2274 	  case EISCONN:			/* Socket already connected */
2275 #endif
2276 #ifdef EINPROGRESS
2277 	  case EINPROGRESS:		/* Operation now in progress */
2278 #endif
2279 #ifdef EALREADY
2280 	  case EALREADY:		/* Operation already in progress */
2281 #endif
2282 #ifdef EADDRINUSE
2283 	  case EADDRINUSE:		/* Address already in use */
2284 #endif
2285 #ifdef EADDRNOTAVAIL
2286 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
2287 #endif
2288 #ifdef ETXTBSY
2289 	  case ETXTBSY:			/* (Apollo) file locked */
2290 #endif
2291 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
2292 	  case ENOSR:			/* Out of streams resources */
2293 #endif
2294 		return TRUE;
2295 	}
2296 
2297 	/* nope, must be permanent */
2298 	return FALSE;
2299 }
2300 /*
2301 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
2302 **
2303 **	Parameters:
2304 **		fd -- the file descriptor of the file.
2305 **		filename -- the file name (for error messages).
2306 **		ext -- the filename extension.
2307 **		type -- type of the lock.  Bits can be:
2308 **			LOCK_EX -- exclusive lock.
2309 **			LOCK_NB -- non-blocking.
2310 **
2311 **	Returns:
2312 **		TRUE if the lock was acquired.
2313 **		FALSE otherwise.
2314 */
2315 
2316 bool
2317 lockfile(fd, filename, ext, type)
2318 	int fd;
2319 	char *filename;
2320 	char *ext;
2321 	int type;
2322 {
2323 # if !HASFLOCK
2324 	int action;
2325 	struct flock lfd;
2326 
2327 	if (ext == NULL)
2328 		ext = "";
2329 
2330 	bzero(&lfd, sizeof lfd);
2331 	if (bitset(LOCK_UN, type))
2332 		lfd.l_type = F_UNLCK;
2333 	else if (bitset(LOCK_EX, type))
2334 		lfd.l_type = F_WRLCK;
2335 	else
2336 		lfd.l_type = F_RDLCK;
2337 
2338 	if (bitset(LOCK_NB, type))
2339 		action = F_SETLK;
2340 	else
2341 		action = F_SETLKW;
2342 
2343 	if (tTd(55, 60))
2344 		printf("lockfile(%s%s, action=%d, type=%d): ",
2345 			filename, ext, action, lfd.l_type);
2346 
2347 	if (fcntl(fd, action, &lfd) >= 0)
2348 	{
2349 		if (tTd(55, 60))
2350 			printf("SUCCESS\n");
2351 		return TRUE;
2352 	}
2353 
2354 	if (tTd(55, 60))
2355 		printf("(%s) ", errstring(errno));
2356 
2357 	/*
2358 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
2359 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
2360 	**  as type "tmp" (that is, served from swap space), the
2361 	**  previous fcntl will fail with "Invalid argument" errors.
2362 	**  Since this is fairly common during testing, we will assume
2363 	**  that this indicates that the lock is successfully grabbed.
2364 	*/
2365 
2366 	if (errno == EINVAL)
2367 	{
2368 		if (tTd(55, 60))
2369 			printf("SUCCESS\n");
2370 		return TRUE;
2371 	}
2372 
2373 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
2374 	{
2375 		int omode = -1;
2376 #  ifdef F_GETFL
2377 		int oerrno = errno;
2378 
2379 		(void) fcntl(fd, F_GETFL, &omode);
2380 		errno = oerrno;
2381 #  endif
2382 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2383 			filename, ext, fd, type, omode, geteuid());
2384 	}
2385 # else
2386 	if (ext == NULL)
2387 		ext = "";
2388 
2389 	if (tTd(55, 60))
2390 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
2391 
2392 	if (flock(fd, type) >= 0)
2393 	{
2394 		if (tTd(55, 60))
2395 			printf("SUCCESS\n");
2396 		return TRUE;
2397 	}
2398 
2399 	if (tTd(55, 60))
2400 		printf("(%s) ", errstring(errno));
2401 
2402 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
2403 	{
2404 		int omode = -1;
2405 #  ifdef F_GETFL
2406 		int oerrno = errno;
2407 
2408 		(void) fcntl(fd, F_GETFL, &omode);
2409 		errno = oerrno;
2410 #  endif
2411 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2412 			filename, ext, fd, type, omode, geteuid());
2413 	}
2414 # endif
2415 	if (tTd(55, 60))
2416 		printf("FAIL\n");
2417 	return FALSE;
2418 }
2419 /*
2420 **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
2421 **
2422 **	Parameters:
2423 **		fd -- the file descriptor to check.
2424 **
2425 **	Returns:
2426 **		TRUE -- if only root can chown the file to an arbitrary
2427 **			user.
2428 **		FALSE -- if an arbitrary user can give away a file.
2429 */
2430 
2431 bool
2432 chownsafe(fd)
2433 	int fd;
2434 {
2435 #ifdef __hpux
2436 	char *s;
2437 	int tfd;
2438 	uid_t o_uid, o_euid;
2439 	gid_t o_gid, o_egid;
2440 	bool rval;
2441 	struct stat stbuf;
2442 
2443 	o_uid = getuid();
2444 	o_euid = geteuid();
2445 	o_gid = getgid();
2446 	o_egid = getegid();
2447 	fstat(fd, &stbuf);
2448 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
2449 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
2450 	s = tmpnam(NULL);
2451 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
2452 	rval = fchown(tfd, DefUid, DefGid) != 0;
2453 	close(tfd);
2454 	unlink(s);
2455 	setreuid(o_uid, o_euid);
2456 	setresgid(o_gid, o_egid, -1);
2457 	return rval;
2458 #else
2459 # ifdef _POSIX_CHOWN_RESTRICTED
2460 #  if _POSIX_CHOWN_RESTRICTED == -1
2461 	return FALSE;
2462 #  else
2463 	return TRUE;
2464 #  endif
2465 # else
2466 #  ifdef _PC_CHOWN_RESTRICTED
2467 	int rval;
2468 
2469 	/*
2470 	**  Some systems (e.g., SunOS) seem to have the call and the
2471 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
2472 	**  the call.  This heuristic checks for that.
2473 	*/
2474 
2475 	errno = 0;
2476 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
2477 	if (errno == 0)
2478 		return rval > 0;
2479 #  endif
2480 #  ifdef BSD
2481 	return TRUE;
2482 #  else
2483 	return FALSE;
2484 #  endif
2485 # endif
2486 #endif
2487 }
2488 /*
2489 **  RESETLIMITS -- reset system controlled resource limits
2490 **
2491 **	This is to avoid denial-of-service attacks
2492 **
2493 **	Parameters:
2494 **		none
2495 **
2496 **	Returns:
2497 **		none
2498 */
2499 
2500 #if HASSETRLIMIT
2501 # include <sys/resource.h>
2502 #endif
2503 
2504 resetlimits()
2505 {
2506 #if HASSETRLIMIT
2507 	struct rlimit lim;
2508 
2509 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
2510 	(void) setrlimit(RLIMIT_CPU, &lim);
2511 	(void) setrlimit(RLIMIT_FSIZE, &lim);
2512 #else
2513 # if HASULIMIT
2514 	(void) ulimit(2, 0x3fffff);
2515 # endif
2516 #endif
2517 }
2518 /*
2519 **  GETCFNAME -- return the name of the .cf file.
2520 **
2521 **	Some systems (e.g., NeXT) determine this dynamically.
2522 */
2523 
2524 char *
2525 getcfname()
2526 {
2527 	if (ConfFile != NULL)
2528 		return ConfFile;
2529 #ifdef NETINFO
2530 	{
2531 		extern char *ni_propval();
2532 		char *cflocation;
2533 
2534 		cflocation = ni_propval("/locations", NULL, "sendmail",
2535 					"sendmail.cf", '\0');
2536 		if (cflocation != NULL)
2537 			return cflocation;
2538 	}
2539 #endif
2540 	return _PATH_SENDMAILCF;
2541 }
2542 /*
2543 **  SETVENDOR -- process vendor code from V configuration line
2544 **
2545 **	Parameters:
2546 **		vendor -- string representation of vendor.
2547 **
2548 **	Returns:
2549 **		TRUE -- if ok.
2550 **		FALSE -- if vendor code could not be processed.
2551 **
2552 **	Side Effects:
2553 **		It is reasonable to set mode flags here to tweak
2554 **		processing in other parts of the code if necessary.
2555 **		For example, if you are a vendor that uses $%y to
2556 **		indicate YP lookups, you could enable that here.
2557 */
2558 
2559 bool
2560 setvendor(vendor)
2561 	char *vendor;
2562 {
2563 	if (strcasecmp(vendor, "Berkeley") == 0)
2564 	{
2565 		VendorCode = VENDOR_BERKELEY;
2566 		return TRUE;
2567 	}
2568 
2569 	/* add vendor extensions here */
2570 
2571 #ifdef SUN_EXTENSIONS
2572 	if (strcasecmp(vendor, "Sun") == 0)
2573 	{
2574 		VendorCode = VENDOR_SUN;
2575 		return TRUE;
2576 	}
2577 #endif
2578 
2579 	return FALSE;
2580 }
2581 /*
2582 **  STRTOL -- convert string to long integer
2583 **
2584 **	For systems that don't have it in the C library.
2585 **
2586 **	This is taken verbatim from the 4.4-Lite C library.
2587 */
2588 
2589 #ifdef NEEDSTRTOL
2590 
2591 #if defined(LIBC_SCCS) && !defined(lint)
2592 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
2593 #endif /* LIBC_SCCS and not lint */
2594 
2595 #include <limits.h>
2596 
2597 /*
2598  * Convert a string to a long integer.
2599  *
2600  * Ignores `locale' stuff.  Assumes that the upper and lower case
2601  * alphabets and digits are each contiguous.
2602  */
2603 
2604 long
2605 strtol(nptr, endptr, base)
2606 	const char *nptr;
2607 	char **endptr;
2608 	register int base;
2609 {
2610 	register const char *s = nptr;
2611 	register unsigned long acc;
2612 	register int c;
2613 	register unsigned long cutoff;
2614 	register int neg = 0, any, cutlim;
2615 
2616 	/*
2617 	 * Skip white space and pick up leading +/- sign if any.
2618 	 * If base is 0, allow 0x for hex and 0 for octal, else
2619 	 * assume decimal; if base is already 16, allow 0x.
2620 	 */
2621 	do {
2622 		c = *s++;
2623 	} while (isspace(c));
2624 	if (c == '-') {
2625 		neg = 1;
2626 		c = *s++;
2627 	} else if (c == '+')
2628 		c = *s++;
2629 	if ((base == 0 || base == 16) &&
2630 	    c == '0' && (*s == 'x' || *s == 'X')) {
2631 		c = s[1];
2632 		s += 2;
2633 		base = 16;
2634 	}
2635 	if (base == 0)
2636 		base = c == '0' ? 8 : 10;
2637 
2638 	/*
2639 	 * Compute the cutoff value between legal numbers and illegal
2640 	 * numbers.  That is the largest legal value, divided by the
2641 	 * base.  An input number that is greater than this value, if
2642 	 * followed by a legal input character, is too big.  One that
2643 	 * is equal to this value may be valid or not; the limit
2644 	 * between valid and invalid numbers is then based on the last
2645 	 * digit.  For instance, if the range for longs is
2646 	 * [-2147483648..2147483647] and the input base is 10,
2647 	 * cutoff will be set to 214748364 and cutlim to either
2648 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
2649 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
2650 	 * the number is too big, and we will return a range error.
2651 	 *
2652 	 * Set any if any `digits' consumed; make it negative to indicate
2653 	 * overflow.
2654 	 */
2655 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
2656 	cutlim = cutoff % (unsigned long)base;
2657 	cutoff /= (unsigned long)base;
2658 	for (acc = 0, any = 0;; c = *s++) {
2659 		if (isdigit(c))
2660 			c -= '0';
2661 		else if (isalpha(c))
2662 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
2663 		else
2664 			break;
2665 		if (c >= base)
2666 			break;
2667 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
2668 			any = -1;
2669 		else {
2670 			any = 1;
2671 			acc *= base;
2672 			acc += c;
2673 		}
2674 	}
2675 	if (any < 0) {
2676 		acc = neg ? LONG_MIN : LONG_MAX;
2677 		errno = ERANGE;
2678 	} else if (neg)
2679 		acc = -acc;
2680 	if (endptr != 0)
2681 		*endptr = (char *)(any ? s - 1 : nptr);
2682 	return (acc);
2683 }
2684 
2685 #endif
2686 /*
2687 **  SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
2688 **
2689 **	Solaris versions at least through 2.3 don't properly deliver a
2690 **	canonical h_name field.  This tries to work around it.
2691 */
2692 
2693 #ifdef SOLARIS
2694 
2695 extern int	h_errno;
2696 
2697 struct hostent *
2698 solaris_gethostbyname(name)
2699 	const char *name;
2700 {
2701 # ifdef SOLARIS_2_3
2702 	static struct hostent hp;
2703 	static char buf[1000];
2704 	extern struct hostent *_switch_gethostbyname_r();
2705 
2706 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
2707 # else
2708 	extern struct hostent *__switch_gethostbyname();
2709 
2710 	return __switch_gethostbyname(name);
2711 # endif
2712 }
2713 
2714 struct hostent *
2715 solaris_gethostbyaddr(addr, len, type)
2716 	const char *addr;
2717 	int len;
2718 	int type;
2719 {
2720 # ifdef SOLARIS_2_3
2721 	static struct hostent hp;
2722 	static char buf[1000];
2723 	extern struct hostent *_switch_gethostbyaddr_r();
2724 
2725 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
2726 # else
2727 	extern struct hostent *__switch_gethostbyaddr();
2728 
2729 	return __switch_gethostbyaddr(addr, len, type);
2730 # endif
2731 }
2732 
2733 #endif
2734 /*
2735 **  NI_PROPVAL -- netinfo property value lookup routine
2736 **
2737 **	Parameters:
2738 **		keydir -- the Netinfo directory name in which to search
2739 **			for the key.
2740 **		keyprop -- the name of the property in which to find the
2741 **			property we are interested.  Defaults to "name".
2742 **		keyval -- the value for which we are really searching.
2743 **		valprop -- the property name for the value in which we
2744 **			are interested.
2745 **		sepchar -- if non-nil, this can be multiple-valued, and
2746 **			we should return a string separated by this
2747 **			character.
2748 **
2749 **	Returns:
2750 **		NULL -- if:
2751 **			1. the directory is not found
2752 **			2. the property name is not found
2753 **			3. the property contains multiple values
2754 **			4. some error occured
2755 **		else -- the location of the config file.
2756 **
2757 **	Example:
2758 **		To search for an alias value, use:
2759 **		  ni_propval("/aliases", "name", aliasname, "members", ',')
2760 **
2761 **	Notes:
2762 **      	Caller should free the return value of ni_proval
2763 */
2764 
2765 #ifdef NETINFO
2766 
2767 # include <netinfo/ni.h>
2768 
2769 # define LOCAL_NETINFO_DOMAIN    "."
2770 # define PARENT_NETINFO_DOMAIN   ".."
2771 # define MAX_NI_LEVELS           256
2772 
2773 char *
2774 ni_propval(keydir, keyprop, keyval, valprop, sepchar)
2775 	char *keydir;
2776 	char *keyprop;
2777 	char *keyval;
2778 	char *valprop;
2779 	char sepchar;
2780 {
2781 	char *propval = NULL;
2782 	int i;
2783 	int j, alen;
2784 	void *ni = NULL;
2785 	void *lastni = NULL;
2786 	ni_status nis;
2787 	ni_id nid;
2788 	ni_namelist ninl;
2789 	register char *p;
2790 	char keybuf[1024];
2791 
2792 	/*
2793 	**  Create the full key from the two parts.
2794 	**
2795 	**	Note that directory can end with, e.g., "name=" to specify
2796 	**	an alternate search property.
2797 	*/
2798 
2799 	i = strlen(keydir) + strlen(keyval) + 2;
2800 	if (keyprop != NULL)
2801 		i += strlen(keyprop) + 1;
2802 	if (i > sizeof keybuf)
2803 		return NULL;
2804 	strcpy(keybuf, keydir);
2805 	strcat(keybuf, "/");
2806 	if (keyprop != NULL)
2807 	{
2808 		strcat(keybuf, keyprop);
2809 		strcat(keybuf, "=");
2810 	}
2811 	strcat(keybuf, keyval);
2812 
2813 	/*
2814 	**  If the passed directory and property name are found
2815 	**  in one of netinfo domains we need to search (starting
2816 	**  from the local domain moving all the way back to the
2817 	**  root domain) set propval to the property's value
2818 	**  and return it.
2819 	*/
2820 
2821 	for (i = 0; i < MAX_NI_LEVELS; ++i)
2822 	{
2823 		if (i == 0)
2824 		{
2825 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
2826 		}
2827 		else
2828 		{
2829 			if (lastni != NULL)
2830 				ni_free(lastni);
2831 			lastni = ni;
2832 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
2833 		}
2834 
2835 		/*
2836 		**  Don't bother if we didn't get a handle on a
2837 		**  proper domain.  This is not necessarily an error.
2838 		**  We would get a positive ni_status if, for instance
2839 		**  we never found the directory or property and tried
2840 		**  to open the parent of the root domain!
2841 		*/
2842 
2843 		if (nis != 0)
2844 			break;
2845 
2846 		/*
2847 		**  Find the path to the server information.
2848 		*/
2849 
2850 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
2851 			continue;
2852 
2853 		/*
2854 		**  Find associated value information.
2855 		*/
2856 
2857 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
2858 			continue;
2859 
2860 		/*
2861 		**  See if we have an acceptable number of values.
2862 		*/
2863 
2864 		if (ninl.ni_namelist_len <= 0)
2865 			continue;
2866 
2867 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
2868 		{
2869 			ni_namelist_free(&ninl);
2870 			continue;
2871 		}
2872 
2873 		/*
2874 		**  Calculate number of bytes needed and build result
2875 		*/
2876 
2877 		alen = 1;
2878 		for (j = 0; j < ninl.ni_namelist_len; j++)
2879 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
2880 		propval = p = xalloc(alen);
2881 		for (j = 0; j < ninl.ni_namelist_len; j++)
2882 		{
2883 			strcpy(p, ninl.ni_namelist_val[j]);
2884 			p += strlen(p);
2885 			*p++ = sepchar;
2886 		}
2887 		*--p = '\0';
2888 
2889 		ni_namelist_free(&ninl);
2890 	}
2891 
2892 	/*
2893 	**  Clean up.
2894 	*/
2895 
2896 	if (ni != NULL)
2897 		ni_free(ni);
2898 	if (lastni != NULL && ni != lastni)
2899 		ni_free(lastni);
2900 
2901 	return propval;
2902 }
2903 
2904 #endif /* NETINFO */
2905 /*
2906 **  HARD_SYSLOG -- call syslog repeatedly until it works
2907 **
2908 **	Needed on HP-UX, which apparently doesn't guarantee that
2909 **	syslog succeeds during interrupt handlers.
2910 */
2911 
2912 #ifdef __hpux
2913 
2914 # define MAXSYSLOGTRIES	100
2915 # undef syslog
2916 
2917 # ifdef __STDC__
2918 hard_syslog(int pri, char *msg, ...)
2919 # else
2920 hard_syslog(pri, msg, va_alist)
2921 	int pri;
2922 	char *msg;
2923 	va_dcl
2924 # endif
2925 {
2926 	int i;
2927 	char buf[SYSLOG_BUFSIZE * 2];
2928 	VA_LOCAL_DECL;
2929 
2930 	VA_START(msg);
2931 	vsprintf(buf, msg, ap);
2932 	VA_END;
2933 
2934 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
2935 		continue;
2936 }
2937 
2938 #endif
2939