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