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