xref: /original-bsd/usr.sbin/sendmail/src/conf.c (revision 9a35f7df)
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.174 (Berkeley) 05/24/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=X-Unix, A=sh -c $u");
266 	makemailer(buf);
267 
268 	strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=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 #ifdef 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 void
1734 putenv(env)
1735 	char *env;
1736 {
1737 	char *p;
1738 	int l;
1739 	char nbuf[100];
1740 
1741 	p = strchr(env, '=');
1742 	if (p == NULL)
1743 		return;
1744 	l = p - env;
1745 	if (l > sizeof nbuf - 1)
1746 		l = sizeof nbuf - 1;
1747 	bcopy(env, nbuf, l);
1748 	nbuf[l] = '\0';
1749 	setenv(nbuf, ++p, 1);
1750 }
1751 
1752 #endif
1753 /*
1754 **  UNSETENV -- remove a variable from the environment
1755 **
1756 **	Not needed on newer systems.
1757 **
1758 **	Parameters:
1759 **		name -- the string name of the environment variable to be
1760 **			deleted from the current environment.
1761 **
1762 **	Returns:
1763 **		none.
1764 **
1765 **	Globals:
1766 **		environ -- a pointer to the current environment.
1767 **
1768 **	Side Effects:
1769 **		Modifies environ.
1770 */
1771 
1772 #ifndef HASUNSETENV
1773 
1774 void
1775 unsetenv(name)
1776 	char *name;
1777 {
1778 	extern char **environ;
1779 	register char **pp;
1780 	int len = strlen(name);
1781 
1782 	for (pp = environ; *pp != NULL; pp++)
1783 	{
1784 		if (strncmp(name, *pp, len) == 0 &&
1785 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1786 			break;
1787 	}
1788 
1789 	for (; *pp != NULL; pp++)
1790 		*pp = pp[1];
1791 }
1792 
1793 #endif
1794 /*
1795 **  GETDTABLESIZE -- return number of file descriptors
1796 **
1797 **	Only on non-BSD systems
1798 **
1799 **	Parameters:
1800 **		none
1801 **
1802 **	Returns:
1803 **		size of file descriptor table
1804 **
1805 **	Side Effects:
1806 **		none
1807 */
1808 
1809 #ifdef SOLARIS
1810 # include <sys/resource.h>
1811 #endif
1812 
1813 int
1814 getdtsize()
1815 {
1816 #ifdef RLIMIT_NOFILE
1817 	struct rlimit rl;
1818 
1819 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
1820 		return rl.rlim_cur;
1821 #endif
1822 
1823 # ifdef HASGETDTABLESIZE
1824 	return getdtablesize();
1825 # else
1826 #  ifdef _SC_OPEN_MAX
1827 	return sysconf(_SC_OPEN_MAX);
1828 #  else
1829 	return NOFILE;
1830 #  endif
1831 # endif
1832 }
1833 /*
1834 **  UNAME -- get the UUCP name of this system.
1835 */
1836 
1837 #ifndef HASUNAME
1838 
1839 int
1840 uname(name)
1841 	struct utsname *name;
1842 {
1843 	FILE *file;
1844 	char *n;
1845 
1846 	name->nodename[0] = '\0';
1847 
1848 	/* try /etc/whoami -- one line with the node name */
1849 	if ((file = fopen("/etc/whoami", "r")) != NULL)
1850 	{
1851 		(void) fgets(name->nodename, NODE_LENGTH + 1, file);
1852 		(void) fclose(file);
1853 		n = strchr(name->nodename, '\n');
1854 		if (n != NULL)
1855 			*n = '\0';
1856 		if (name->nodename[0] != '\0')
1857 			return (0);
1858 	}
1859 
1860 	/* try /usr/include/whoami.h -- has a #define somewhere */
1861 	if ((file = fopen("/usr/include/whoami.h", "r")) != NULL)
1862 	{
1863 		char buf[MAXLINE];
1864 
1865 		while (fgets(buf, MAXLINE, file) != NULL)
1866 			if (sscanf(buf, "#define sysname \"%*[^\"]\"",
1867 					NODE_LENGTH, name->nodename) > 0)
1868 				break;
1869 		(void) fclose(file);
1870 		if (name->nodename[0] != '\0')
1871 			return (0);
1872 	}
1873 
1874 #ifdef TRUST_POPEN
1875 	/*
1876 	**  Popen is known to have security holes.
1877 	*/
1878 
1879 	/* try uuname -l to return local name */
1880 	if ((file = popen("uuname -l", "r")) != NULL)
1881 	{
1882 		(void) fgets(name, NODE_LENGTH + 1, file);
1883 		(void) pclose(file);
1884 		n = strchr(name, '\n');
1885 		if (n != NULL)
1886 			*n = '\0';
1887 		if (name->nodename[0] != '\0')
1888 			return (0);
1889 	}
1890 #endif
1891 
1892 	return (-1);
1893 }
1894 #endif /* HASUNAME */
1895 /*
1896 **  INITGROUPS -- initialize groups
1897 **
1898 **	Stub implementation for System V style systems
1899 */
1900 
1901 #ifndef HASINITGROUPS
1902 
1903 initgroups(name, basegid)
1904 	char *name;
1905 	int basegid;
1906 {
1907 	return 0;
1908 }
1909 
1910 #endif
1911 /*
1912 **  SETSID -- set session id (for non-POSIX systems)
1913 */
1914 
1915 #ifndef HASSETSID
1916 
1917 pid_t
1918 setsid __P ((void))
1919 {
1920 #ifdef TIOCNOTTY
1921 	int fd;
1922 
1923 	fd = open("/dev/tty", O_RDWR, 0);
1924 	if (fd >= 0)
1925 	{
1926 		(void) ioctl(fd, (int) TIOCNOTTY, (char *) 0);
1927 		(void) close(fd);
1928 	}
1929 #endif /* TIOCNOTTY */
1930 # ifdef SYS5SETPGRP
1931 	return setpgrp();
1932 # else
1933 	return setpgid(0, getpid());
1934 # endif
1935 }
1936 
1937 #endif
1938 /*
1939 **  FSYNC -- dummy fsync
1940 */
1941 
1942 #ifdef NEEDFSYNC
1943 
1944 fsync(fd)
1945 	int fd;
1946 {
1947 # ifdef O_SYNC
1948 	return fcntl(fd, F_SETFL, O_SYNC);
1949 # else
1950 	/* nothing we can do */
1951 	return 0;
1952 # endif
1953 }
1954 
1955 #endif
1956 /*
1957 **  DGUX_INET_ADDR -- inet_addr for DG/UX
1958 **
1959 **	Data General DG/UX version of inet_addr returns a struct in_addr
1960 **	instead of a long.  This patches things.  Only needed on versions
1961 **	prior to 5.4.3.
1962 */
1963 
1964 #ifdef DGUX_5_4_2
1965 
1966 #undef inet_addr
1967 
1968 long
1969 dgux_inet_addr(host)
1970 	char *host;
1971 {
1972 	struct in_addr haddr;
1973 
1974 	haddr = inet_addr(host);
1975 	return haddr.s_addr;
1976 }
1977 
1978 #endif
1979 /*
1980 **  GETOPT -- for old systems or systems with bogus implementations
1981 */
1982 
1983 #ifdef NEEDGETOPT
1984 
1985 /*
1986  * Copyright (c) 1985 Regents of the University of California.
1987  * All rights reserved.  The Berkeley software License Agreement
1988  * specifies the terms and conditions for redistribution.
1989  */
1990 
1991 
1992 /*
1993 ** this version hacked to add `atend' flag to allow state machine
1994 ** to reset if invoked by the program to scan args for a 2nd time
1995 */
1996 
1997 #if defined(LIBC_SCCS) && !defined(lint)
1998 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
1999 #endif /* LIBC_SCCS and not lint */
2000 
2001 #include <stdio.h>
2002 
2003 /*
2004  * get option letter from argument vector
2005  */
2006 #ifdef _CONVEX_SOURCE
2007 extern int	optind, opterr, optopt;
2008 extern char	*optarg;
2009 #else
2010 int	opterr = 1;		/* if error message should be printed */
2011 int	optind = 1;		/* index into parent argv vector */
2012 int	optopt = 0;		/* character checked for validity */
2013 char	*optarg = NULL;		/* argument associated with option */
2014 #endif
2015 
2016 #define BADCH	(int)'?'
2017 #define EMSG	""
2018 #define tell(s)	if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
2019 		fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
2020 
2021 getopt(nargc,nargv,ostr)
2022 	int		nargc;
2023 	char *const	*nargv;
2024 	const char	*ostr;
2025 {
2026 	static char	*place = EMSG;	/* option letter processing */
2027 	static char	atend = 0;
2028 	register char	*oli;		/* option letter list index */
2029 
2030 	if (atend) {
2031 		atend = 0;
2032 		place = EMSG;
2033 	}
2034 	if(!*place) {			/* update scanning pointer */
2035 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
2036 			atend++;
2037 			return(EOF);
2038 		}
2039 		if (*place == '-') {	/* found "--" */
2040 			++optind;
2041 			atend++;
2042 			return(EOF);
2043 		}
2044 	}				/* option letter okay? */
2045 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
2046 		if (!*place) ++optind;
2047 		tell(": illegal option -- ");
2048 	}
2049 	if (*++oli != ':') {		/* don't need argument */
2050 		optarg = NULL;
2051 		if (!*place) ++optind;
2052 	}
2053 	else {				/* need an argument */
2054 		if (*place) optarg = place;	/* no white space */
2055 		else if (nargc <= ++optind) {	/* no arg */
2056 			place = EMSG;
2057 			tell(": option requires an argument -- ");
2058 		}
2059 	 	else optarg = nargv[optind];	/* white space */
2060 		place = EMSG;
2061 		++optind;
2062 	}
2063 	return(optopt);			/* dump back option letter */
2064 }
2065 
2066 #endif
2067 /*
2068 **  VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version
2069 */
2070 
2071 #ifdef NEEDVPRINTF
2072 
2073 #define MAXARG	16
2074 
2075 vfprintf(fp, fmt, ap)
2076 	FILE *	fp;
2077 	char *	fmt;
2078 	char **	ap;
2079 {
2080 	char *	bp[MAXARG];
2081 	int	i = 0;
2082 
2083 	while (*ap && i < MAXARG)
2084 		bp[i++] = *ap++;
2085 	fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3],
2086 			 bp[4], bp[5], bp[6], bp[7],
2087 			 bp[8], bp[9], bp[10], bp[11],
2088 			 bp[12], bp[13], bp[14], bp[15]);
2089 }
2090 
2091 vsprintf(s, fmt, ap)
2092 	char *	s;
2093 	char *	fmt;
2094 	char **	ap;
2095 {
2096 	char *	bp[MAXARG];
2097 	int	i = 0;
2098 
2099 	while (*ap && i < MAXARG)
2100 		bp[i++] = *ap++;
2101 	sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3],
2102 			bp[4], bp[5], bp[6], bp[7],
2103 			bp[8], bp[9], bp[10], bp[11],
2104 			bp[12], bp[13], bp[14], bp[15]);
2105 }
2106 
2107 #endif
2108 /*
2109 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
2110 **
2111 **	Parameters:
2112 **		shell -- the user's shell from /etc/passwd
2113 **
2114 **	Returns:
2115 **		TRUE -- if it is ok to use this for unrestricted access.
2116 **		FALSE -- if the shell is restricted.
2117 */
2118 
2119 #if !HASGETUSERSHELL
2120 
2121 # ifndef _PATH_SHELLS
2122 #  define _PATH_SHELLS	"/etc/shells"
2123 # endif
2124 
2125 char	*DefaultUserShells[] =
2126 {
2127 	"/bin/sh",		/* standard shell */
2128 	"/usr/bin/sh",
2129 	"/bin/csh",		/* C shell */
2130 	"/usr/bin/csh",
2131 #ifdef __hpux
2132 # ifdef V4FS
2133 	"/usr/bin/rsh",		/* restricted Bourne shell */
2134 	"/usr/bin/ksh",		/* Korn shell */
2135 	"/usr/bin/rksh",	/* restricted Korn shell */
2136 	"/usr/bin/pam",
2137 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
2138 	"/usr/bin/posix/sh",
2139 # else
2140 	"/bin/rsh",		/* restricted Bourne shell */
2141 	"/bin/ksh",		/* Korn shell */
2142 	"/bin/rksh",		/* restricted Korn shell */
2143 	"/bin/pam",
2144 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
2145 	"/bin/posix/sh",
2146 # endif
2147 #endif
2148 #ifdef _AIX3
2149 	"/bin/ksh",		/* Korn shell */
2150 	"/usr/bin/ksh",
2151 	"/bin/tsh",		/* trusted shell */
2152 	"/usr/bin/tsh",
2153 	"/bin/bsh",		/* Bourne shell */
2154 	"/usr/bin/bsh",
2155 #endif
2156 	NULL
2157 };
2158 
2159 #endif
2160 
2161 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
2162 
2163 bool
2164 usershellok(shell)
2165 	char *shell;
2166 {
2167 #if HASGETUSERSHELL
2168 	register char *p;
2169 	extern char *getusershell();
2170 
2171 	if (shell == NULL || shell[0] == '\0' || ConfigLevel <= 1)
2172 		return TRUE;
2173 
2174 	setusershell();
2175 	while ((p = getusershell()) != NULL)
2176 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
2177 			break;
2178 	endusershell();
2179 	return p != NULL;
2180 #else
2181 	register FILE *shellf;
2182 	char buf[MAXLINE];
2183 
2184 	if (shell == NULL || shell[0] == '\0')
2185 		return TRUE;
2186 
2187 	shellf = fopen(_PATH_SHELLS, "r");
2188 	if (shellf == NULL)
2189 	{
2190 		/* no /etc/shells; see if it is one of the std shells */
2191 		char **d;
2192 
2193 		for (d = DefaultUserShells; *d != NULL; d++)
2194 		{
2195 			if (strcmp(shell, *d) == 0)
2196 				return TRUE;
2197 		}
2198 		return FALSE;
2199 	}
2200 
2201 	while (fgets(buf, sizeof buf, shellf) != NULL)
2202 	{
2203 		register char *p, *q;
2204 
2205 		p = buf;
2206 		while (*p != '\0' && *p != '#' && *p != '/')
2207 			p++;
2208 		if (*p == '#' || *p == '\0')
2209 			continue;
2210 		q = p;
2211 		while (*p != '\0' && *p != '#' && !isspace(*p))
2212 			p++;
2213 		*p = '\0';
2214 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
2215 		{
2216 			fclose(shellf);
2217 			return TRUE;
2218 		}
2219 	}
2220 	fclose(shellf);
2221 	return FALSE;
2222 #endif
2223 }
2224 /*
2225 **  FREESPACE -- see how much free space is on the queue filesystem
2226 **
2227 **	Only implemented if you have statfs.
2228 **
2229 **	Parameters:
2230 **		dir -- the directory in question.
2231 **		bsize -- a variable into which the filesystem
2232 **			block size is stored.
2233 **
2234 **	Returns:
2235 **		The number of bytes free on the queue filesystem.
2236 **		-1 if the statfs call fails.
2237 **
2238 **	Side effects:
2239 **		Puts the filesystem block size into bsize.
2240 */
2241 
2242 /* statfs types */
2243 #define SFS_NONE	0	/* no statfs implementation */
2244 #define SFS_USTAT	1	/* use ustat */
2245 #define SFS_4ARGS	2	/* use four-argument statfs call */
2246 #define SFS_VFS		3	/* use <sys/vfs.h> implementation */
2247 #define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
2248 #define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
2249 #define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
2250 
2251 #ifndef SFS_TYPE
2252 # define SFS_TYPE	SFS_NONE
2253 #endif
2254 
2255 #if SFS_TYPE == SFS_USTAT
2256 # include <ustat.h>
2257 #endif
2258 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
2259 # include <sys/statfs.h>
2260 #endif
2261 #if SFS_TYPE == SFS_VFS
2262 # include <sys/vfs.h>
2263 #endif
2264 #if SFS_TYPE == SFS_MOUNT
2265 # include <sys/mount.h>
2266 #endif
2267 #if SFS_TYPE == SFS_STATVFS
2268 # include <sys/statvfs.h>
2269 #endif
2270 
2271 long
2272 freespace(dir, bsize)
2273 	char *dir;
2274 	long *bsize;
2275 {
2276 #if SFS_TYPE != SFS_NONE
2277 # if SFS_TYPE == SFS_USTAT
2278 	struct ustat fs;
2279 	struct stat statbuf;
2280 #  define FSBLOCKSIZE	DEV_BSIZE
2281 #  define SFS_BAVAIL	f_tfree
2282 # else
2283 #  if defined(ultrix)
2284 	struct fs_data fs;
2285 #   define SFS_BAVAIL	fd_bfreen
2286 #   define FSBLOCKSIZE	1024L
2287 #  else
2288 #   if SFS_TYPE == SFS_STATVFS
2289 	struct statvfs fs;
2290 #    define FSBLOCKSIZE	fs.f_frsize
2291 #   else
2292 	struct statfs fs;
2293 #    define FSBLOCKSIZE	fs.f_bsize
2294 #   endif
2295 #  endif
2296 # endif
2297 # ifndef SFS_BAVAIL
2298 #  define SFS_BAVAIL f_bavail
2299 # endif
2300 
2301 # if SFS_TYPE == SFS_USTAT
2302 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
2303 # else
2304 #  if SFS_TYPE == SFS_4ARGS
2305 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
2306 #  else
2307 #   if SFS_TYPE == SFS_STATVFS
2308 	if (statvfs(dir, &fs) == 0)
2309 #   else
2310 #    if defined(ultrix)
2311 	if (statfs(dir, &fs) > 0)
2312 #    else
2313 	if (statfs(dir, &fs) == 0)
2314 #    endif
2315 #   endif
2316 #  endif
2317 # endif
2318 	{
2319 		if (bsize != NULL)
2320 			*bsize = FSBLOCKSIZE;
2321 		return (fs.SFS_BAVAIL);
2322 	}
2323 #endif
2324 	return (-1);
2325 }
2326 /*
2327 **  ENOUGHSPACE -- check to see if there is enough free space on the queue fs
2328 **
2329 **	Only implemented if you have statfs.
2330 **
2331 **	Parameters:
2332 **		msize -- the size to check against.  If zero, we don't yet
2333 **		know how big the message will be, so just check for
2334 **		a "reasonable" amount.
2335 **
2336 **	Returns:
2337 **		TRUE if there is enough space.
2338 **		FALSE otherwise.
2339 */
2340 
2341 bool
2342 enoughspace(msize)
2343 	long msize;
2344 {
2345 	long bfree, bsize;
2346 
2347 	if (MinBlocksFree <= 0 && msize <= 0)
2348 	{
2349 		if (tTd(4, 80))
2350 			printf("enoughspace: no threshold\n");
2351 		return TRUE;
2352 	}
2353 
2354 	if ((bfree = freespace(QueueDir, &bsize)) >= 0)
2355 	{
2356 		if (tTd(4, 80))
2357 			printf("enoughspace: bavail=%ld, need=%ld\n",
2358 				bfree, msize);
2359 
2360 		/* convert msize to block count */
2361 		msize = msize / bsize + 1;
2362 		if (MinBlocksFree >= 0)
2363 			msize += MinBlocksFree;
2364 
2365 		if (bfree < msize)
2366 		{
2367 #ifdef LOG
2368 			if (LogLevel > 0)
2369 				syslog(LOG_ALERT,
2370 					"%s: low on space (have %ld, %s needs %ld in %s)",
2371 					CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id,
2372 					bfree,
2373 					CurHostName == NULL ? "SMTP-DAEMON" : CurHostName,
2374 					msize, QueueDir);
2375 #endif
2376 			return FALSE;
2377 		}
2378 	}
2379 	else if (tTd(4, 80))
2380 		printf("enoughspace failure: min=%ld, need=%ld: %s\n",
2381 			MinBlocksFree, msize, errstring(errno));
2382 	return TRUE;
2383 }
2384 /*
2385 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
2386 **
2387 **	This looks at an errno value and tells if this is likely to
2388 **	go away if retried later.
2389 **
2390 **	Parameters:
2391 **		err -- the errno code to classify.
2392 **
2393 **	Returns:
2394 **		TRUE if this is probably transient.
2395 **		FALSE otherwise.
2396 */
2397 
2398 bool
2399 transienterror(err)
2400 	int err;
2401 {
2402 	switch (err)
2403 	{
2404 	  case EIO:			/* I/O error */
2405 	  case ENXIO:			/* Device not configured */
2406 	  case EAGAIN:			/* Resource temporarily unavailable */
2407 	  case ENOMEM:			/* Cannot allocate memory */
2408 	  case ENODEV:			/* Operation not supported by device */
2409 	  case ENFILE:			/* Too many open files in system */
2410 	  case EMFILE:			/* Too many open files */
2411 	  case ENOSPC:			/* No space left on device */
2412 #ifdef ETIMEDOUT
2413 	  case ETIMEDOUT:		/* Connection timed out */
2414 #endif
2415 #ifdef ESTALE
2416 	  case ESTALE:			/* Stale NFS file handle */
2417 #endif
2418 #ifdef ENETDOWN
2419 	  case ENETDOWN:		/* Network is down */
2420 #endif
2421 #ifdef ENETUNREACH
2422 	  case ENETUNREACH:		/* Network is unreachable */
2423 #endif
2424 #ifdef ENETRESET
2425 	  case ENETRESET:		/* Network dropped connection on reset */
2426 #endif
2427 #ifdef ECONNABORTED
2428 	  case ECONNABORTED:		/* Software caused connection abort */
2429 #endif
2430 #ifdef ECONNRESET
2431 	  case ECONNRESET:		/* Connection reset by peer */
2432 #endif
2433 #ifdef ENOBUFS
2434 	  case ENOBUFS:			/* No buffer space available */
2435 #endif
2436 #ifdef ESHUTDOWN
2437 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
2438 #endif
2439 #ifdef ECONNREFUSED
2440 	  case ECONNREFUSED:		/* Connection refused */
2441 #endif
2442 #ifdef EHOSTDOWN
2443 	  case EHOSTDOWN:		/* Host is down */
2444 #endif
2445 #ifdef EHOSTUNREACH
2446 	  case EHOSTUNREACH:		/* No route to host */
2447 #endif
2448 #ifdef EDQUOT
2449 	  case EDQUOT:			/* Disc quota exceeded */
2450 #endif
2451 #ifdef EPROCLIM
2452 	  case EPROCLIM:		/* Too many processes */
2453 #endif
2454 #ifdef EUSERS
2455 	  case EUSERS:			/* Too many users */
2456 #endif
2457 #ifdef EDEADLK
2458 	  case EDEADLK:			/* Resource deadlock avoided */
2459 #endif
2460 #ifdef EISCONN
2461 	  case EISCONN:			/* Socket already connected */
2462 #endif
2463 #ifdef EINPROGRESS
2464 	  case EINPROGRESS:		/* Operation now in progress */
2465 #endif
2466 #ifdef EALREADY
2467 	  case EALREADY:		/* Operation already in progress */
2468 #endif
2469 #ifdef EADDRINUSE
2470 	  case EADDRINUSE:		/* Address already in use */
2471 #endif
2472 #ifdef EADDRNOTAVAIL
2473 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
2474 #endif
2475 #ifdef ETXTBSY
2476 	  case ETXTBSY:			/* (Apollo) file locked */
2477 #endif
2478 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
2479 	  case ENOSR:			/* Out of streams resources */
2480 #endif
2481 		return TRUE;
2482 	}
2483 
2484 	/* nope, must be permanent */
2485 	return FALSE;
2486 }
2487 /*
2488 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
2489 **
2490 **	Parameters:
2491 **		fd -- the file descriptor of the file.
2492 **		filename -- the file name (for error messages).
2493 **		ext -- the filename extension.
2494 **		type -- type of the lock.  Bits can be:
2495 **			LOCK_EX -- exclusive lock.
2496 **			LOCK_NB -- non-blocking.
2497 **
2498 **	Returns:
2499 **		TRUE if the lock was acquired.
2500 **		FALSE otherwise.
2501 */
2502 
2503 bool
2504 lockfile(fd, filename, ext, type)
2505 	int fd;
2506 	char *filename;
2507 	char *ext;
2508 	int type;
2509 {
2510 # if !HASFLOCK
2511 	int action;
2512 	struct flock lfd;
2513 
2514 	if (ext == NULL)
2515 		ext = "";
2516 
2517 	bzero(&lfd, sizeof lfd);
2518 	if (bitset(LOCK_UN, type))
2519 		lfd.l_type = F_UNLCK;
2520 	else if (bitset(LOCK_EX, type))
2521 		lfd.l_type = F_WRLCK;
2522 	else
2523 		lfd.l_type = F_RDLCK;
2524 
2525 	if (bitset(LOCK_NB, type))
2526 		action = F_SETLK;
2527 	else
2528 		action = F_SETLKW;
2529 
2530 	if (tTd(55, 60))
2531 		printf("lockfile(%s%s, action=%d, type=%d): ",
2532 			filename, ext, action, lfd.l_type);
2533 
2534 	if (fcntl(fd, action, &lfd) >= 0)
2535 	{
2536 		if (tTd(55, 60))
2537 			printf("SUCCESS\n");
2538 		return TRUE;
2539 	}
2540 
2541 	if (tTd(55, 60))
2542 		printf("(%s) ", errstring(errno));
2543 
2544 	/*
2545 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
2546 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
2547 	**  as type "tmp" (that is, served from swap space), the
2548 	**  previous fcntl will fail with "Invalid argument" errors.
2549 	**  Since this is fairly common during testing, we will assume
2550 	**  that this indicates that the lock is successfully grabbed.
2551 	*/
2552 
2553 	if (errno == EINVAL)
2554 	{
2555 		if (tTd(55, 60))
2556 			printf("SUCCESS\n");
2557 		return TRUE;
2558 	}
2559 
2560 	if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
2561 	{
2562 		int omode = -1;
2563 #  ifdef F_GETFL
2564 		int oerrno = errno;
2565 
2566 		(void) fcntl(fd, F_GETFL, &omode);
2567 		errno = oerrno;
2568 #  endif
2569 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2570 			filename, ext, fd, type, omode, geteuid());
2571 	}
2572 # else
2573 	if (ext == NULL)
2574 		ext = "";
2575 
2576 	if (tTd(55, 60))
2577 		printf("lockfile(%s%s, type=%o): ", filename, ext, type);
2578 
2579 	if (flock(fd, type) >= 0)
2580 	{
2581 		if (tTd(55, 60))
2582 			printf("SUCCESS\n");
2583 		return TRUE;
2584 	}
2585 
2586 	if (tTd(55, 60))
2587 		printf("(%s) ", errstring(errno));
2588 
2589 	if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
2590 	{
2591 		int omode = -1;
2592 #  ifdef F_GETFL
2593 		int oerrno = errno;
2594 
2595 		(void) fcntl(fd, F_GETFL, &omode);
2596 		errno = oerrno;
2597 #  endif
2598 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
2599 			filename, ext, fd, type, omode, geteuid());
2600 	}
2601 # endif
2602 	if (tTd(55, 60))
2603 		printf("FAIL\n");
2604 	return FALSE;
2605 }
2606 /*
2607 **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
2608 **
2609 **	Parameters:
2610 **		fd -- the file descriptor to check.
2611 **
2612 **	Returns:
2613 **		TRUE -- if only root can chown the file to an arbitrary
2614 **			user.
2615 **		FALSE -- if an arbitrary user can give away a file.
2616 */
2617 
2618 bool
2619 chownsafe(fd)
2620 	int fd;
2621 {
2622 #ifdef __hpux
2623 	char *s;
2624 	int tfd;
2625 	uid_t o_uid, o_euid;
2626 	gid_t o_gid, o_egid;
2627 	bool rval;
2628 	struct stat stbuf;
2629 
2630 	o_uid = getuid();
2631 	o_euid = geteuid();
2632 	o_gid = getgid();
2633 	o_egid = getegid();
2634 	fstat(fd, &stbuf);
2635 	setresuid(stbuf.st_uid, stbuf.st_uid, -1);
2636 	setresgid(stbuf.st_gid, stbuf.st_gid, -1);
2637 	s = tmpnam(NULL);
2638 	tfd = open(s, O_RDONLY|O_CREAT, 0600);
2639 	rval = fchown(tfd, DefUid, DefGid) != 0;
2640 	close(tfd);
2641 	unlink(s);
2642 	setresuid(o_uid, o_euid, -1);
2643 	setresgid(o_gid, o_egid, -1);
2644 	return rval;
2645 #else
2646 # ifdef _POSIX_CHOWN_RESTRICTED
2647 #  if _POSIX_CHOWN_RESTRICTED == -1
2648 	return FALSE;
2649 #  else
2650 	return TRUE;
2651 #  endif
2652 # else
2653 #  ifdef _PC_CHOWN_RESTRICTED
2654 	int rval;
2655 
2656 	/*
2657 	**  Some systems (e.g., SunOS) seem to have the call and the
2658 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
2659 	**  the call.  This heuristic checks for that.
2660 	*/
2661 
2662 	errno = 0;
2663 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
2664 	if (errno == 0)
2665 		return rval > 0;
2666 #  endif
2667 #  ifdef BSD
2668 	return TRUE;
2669 #  else
2670 	return FALSE;
2671 #  endif
2672 # endif
2673 #endif
2674 }
2675 /*
2676 **  RESETLIMITS -- reset system controlled resource limits
2677 **
2678 **	This is to avoid denial-of-service attacks
2679 **
2680 **	Parameters:
2681 **		none
2682 **
2683 **	Returns:
2684 **		none
2685 */
2686 
2687 #if HASSETRLIMIT
2688 # include <sys/resource.h>
2689 #endif
2690 
2691 void
2692 resetlimits()
2693 {
2694 #if HASSETRLIMIT
2695 	struct rlimit lim;
2696 
2697 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
2698 	(void) setrlimit(RLIMIT_CPU, &lim);
2699 	(void) setrlimit(RLIMIT_FSIZE, &lim);
2700 #else
2701 # if HASULIMIT
2702 	(void) ulimit(2, 0x3fffff);
2703 # endif
2704 #endif
2705 }
2706 /*
2707 **  GETCFNAME -- return the name of the .cf file.
2708 **
2709 **	Some systems (e.g., NeXT) determine this dynamically.
2710 */
2711 
2712 char *
2713 getcfname()
2714 {
2715 	int i;
2716 	static char cbuf[200];
2717 
2718 	if (ConfFile != NULL)
2719 		return ConfFile;
2720 #ifdef NETINFO
2721 	{
2722 		extern char *ni_propval();
2723 		char *cflocation;
2724 
2725 		cflocation = ni_propval("/locations", NULL, "sendmail",
2726 					"sendmail.cf", '\0');
2727 		if (cflocation != NULL)
2728 			return cflocation;
2729 	}
2730 #endif
2731 
2732 #ifdef TRY_VERSIONED_CF_NAME
2733 	/*
2734 	**  Try sendmail.8.6.12.cf, then sendmail.8.6.cf, then
2735 	**  sendmail.8.cf, and finally sendmail.cf.
2736 	**
2737 	**	I suppose it should really try a search path here --
2738 	**	e.g., /etc/sendmail.cf, /etc/mail/sendmail.cf,
2739 	**	/usr/lib/sendmail.cf, and so forth.
2740 	*/
2741 
2742 	strcpy(cbuf, _PATH_SENDMAILCF);
2743 	i = strlen(cbuf);
2744 	if (strcmp(&cbuf[i - 3], ".cf") == 0)
2745 	{
2746 		char *p;
2747 		extern char Version[];
2748 
2749 		strcpy(&cbuf[i - 2], Version);
2750 		p = strchr(&cbuf[i - 2], '/');
2751 		if (p != NULL)
2752 			*p = '\0';
2753 		p = &cbuf[strlen(cbuf)];
2754 		do
2755 		{
2756 			int fd;
2757 
2758 			strcpy(p, ".cf");
2759 			if ((fd = open(cbuf, O_RDONLY, 0)) >= 0)
2760 			{
2761 				close(fd);
2762 				return cbuf;
2763 			}
2764 			*p = '\0';
2765 		} while ((p = strrchr(&cbuf[i - 2], '.')) != NULL);
2766 	}
2767 #endif
2768 	return _PATH_SENDMAILCF;
2769 }
2770 /*
2771 **  SETVENDOR -- process vendor code from V configuration line
2772 **
2773 **	Parameters:
2774 **		vendor -- string representation of vendor.
2775 **
2776 **	Returns:
2777 **		TRUE -- if ok.
2778 **		FALSE -- if vendor code could not be processed.
2779 **
2780 **	Side Effects:
2781 **		It is reasonable to set mode flags here to tweak
2782 **		processing in other parts of the code if necessary.
2783 **		For example, if you are a vendor that uses $%y to
2784 **		indicate YP lookups, you could enable that here.
2785 */
2786 
2787 bool
2788 setvendor(vendor)
2789 	char *vendor;
2790 {
2791 	if (strcasecmp(vendor, "Berkeley") == 0)
2792 	{
2793 		VendorCode = VENDOR_BERKELEY;
2794 		return TRUE;
2795 	}
2796 
2797 	/* add vendor extensions here */
2798 
2799 #ifdef SUN_EXTENSIONS
2800 	if (strcasecmp(vendor, "Sun") == 0)
2801 	{
2802 		VendorCode = VENDOR_SUN;
2803 		return TRUE;
2804 	}
2805 #endif
2806 
2807 	return FALSE;
2808 }
2809 /*
2810 **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
2811 **
2812 **	Vendor_pre_defaults is called before reading the configuration
2813 **	file; vendor_post_defaults is called immediately after.
2814 **
2815 **	Parameters:
2816 **		e -- the global environment to initialize.
2817 **
2818 **	Returns:
2819 **		none.
2820 */
2821 
2822 void
2823 vendor_pre_defaults(e)
2824 	ENVELOPE *e;
2825 {
2826 }
2827 
2828 
2829 void
2830 vendor_post_defaults(e)
2831 	ENVELOPE *e;
2832 {
2833 }
2834 /*
2835 **  STRTOL -- convert string to long integer
2836 **
2837 **	For systems that don't have it in the C library.
2838 **
2839 **	This is taken verbatim from the 4.4-Lite C library.
2840 */
2841 
2842 #ifdef NEEDSTRTOL
2843 
2844 #if defined(LIBC_SCCS) && !defined(lint)
2845 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
2846 #endif /* LIBC_SCCS and not lint */
2847 
2848 #include <limits.h>
2849 
2850 /*
2851  * Convert a string to a long integer.
2852  *
2853  * Ignores `locale' stuff.  Assumes that the upper and lower case
2854  * alphabets and digits are each contiguous.
2855  */
2856 
2857 long
2858 strtol(nptr, endptr, base)
2859 	const char *nptr;
2860 	char **endptr;
2861 	register int base;
2862 {
2863 	register const char *s = nptr;
2864 	register unsigned long acc;
2865 	register int c;
2866 	register unsigned long cutoff;
2867 	register int neg = 0, any, cutlim;
2868 
2869 	/*
2870 	 * Skip white space and pick up leading +/- sign if any.
2871 	 * If base is 0, allow 0x for hex and 0 for octal, else
2872 	 * assume decimal; if base is already 16, allow 0x.
2873 	 */
2874 	do {
2875 		c = *s++;
2876 	} while (isspace(c));
2877 	if (c == '-') {
2878 		neg = 1;
2879 		c = *s++;
2880 	} else if (c == '+')
2881 		c = *s++;
2882 	if ((base == 0 || base == 16) &&
2883 	    c == '0' && (*s == 'x' || *s == 'X')) {
2884 		c = s[1];
2885 		s += 2;
2886 		base = 16;
2887 	}
2888 	if (base == 0)
2889 		base = c == '0' ? 8 : 10;
2890 
2891 	/*
2892 	 * Compute the cutoff value between legal numbers and illegal
2893 	 * numbers.  That is the largest legal value, divided by the
2894 	 * base.  An input number that is greater than this value, if
2895 	 * followed by a legal input character, is too big.  One that
2896 	 * is equal to this value may be valid or not; the limit
2897 	 * between valid and invalid numbers is then based on the last
2898 	 * digit.  For instance, if the range for longs is
2899 	 * [-2147483648..2147483647] and the input base is 10,
2900 	 * cutoff will be set to 214748364 and cutlim to either
2901 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
2902 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
2903 	 * the number is too big, and we will return a range error.
2904 	 *
2905 	 * Set any if any `digits' consumed; make it negative to indicate
2906 	 * overflow.
2907 	 */
2908 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
2909 	cutlim = cutoff % (unsigned long)base;
2910 	cutoff /= (unsigned long)base;
2911 	for (acc = 0, any = 0;; c = *s++) {
2912 		if (isdigit(c))
2913 			c -= '0';
2914 		else if (isalpha(c))
2915 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
2916 		else
2917 			break;
2918 		if (c >= base)
2919 			break;
2920 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
2921 			any = -1;
2922 		else {
2923 			any = 1;
2924 			acc *= base;
2925 			acc += c;
2926 		}
2927 	}
2928 	if (any < 0) {
2929 		acc = neg ? LONG_MIN : LONG_MAX;
2930 		errno = ERANGE;
2931 	} else if (neg)
2932 		acc = -acc;
2933 	if (endptr != 0)
2934 		*endptr = (char *)(any ? s - 1 : nptr);
2935 	return (acc);
2936 }
2937 
2938 #endif
2939 /*
2940 **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
2941 **
2942 **	Some operating systems have wierd problems with the gethostbyXXX
2943 **	routines.  For example, Solaris versions at least through 2.3
2944 **	don't properly deliver a canonical h_name field.  This tries to
2945 **	work around these problems.
2946 */
2947 
2948 struct hostent *
2949 sm_gethostbyname(name)
2950 	char *name;
2951 {
2952 #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4)
2953 	extern int h_errno;
2954 
2955 # if SOLARIS == 203
2956 	static struct hostent hp;
2957 	static char buf[1000];
2958 	extern struct hostent *_switch_gethostbyname_r();
2959 
2960 	return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
2961 # else
2962 	extern struct hostent *__switch_gethostbyname();
2963 
2964 	return __switch_gethostbyname(name);
2965 # endif
2966 #else
2967 	return gethostbyname(name);
2968 #endif
2969 }
2970 
2971 struct hostent *
2972 sm_gethostbyaddr(addr, len, type)
2973 	char *addr;
2974 	int len;
2975 	int type;
2976 {
2977 #if defined(SOLARIS) && SOLARIS < 204
2978 	extern int h_errno;
2979 
2980 # if SOLARIS == 203
2981 	static struct hostent hp;
2982 	static char buf[1000];
2983 	extern struct hostent *_switch_gethostbyaddr_r();
2984 
2985 	return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno);
2986 # else
2987 	extern struct hostent *__switch_gethostbyaddr();
2988 
2989 	return __switch_gethostbyaddr(addr, len, type);
2990 # endif
2991 #else
2992 	return gethostbyaddr(addr, len, type);
2993 #endif
2994 }
2995 /*
2996 **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
2997 */
2998 
2999 struct passwd *
3000 sm_getpwnam(user)
3001 	char *user;
3002 {
3003 	return getpwnam(user);
3004 }
3005 
3006 struct passwd *
3007 sm_getpwuid(uid)
3008 	uid_t uid;
3009 {
3010 	return getpwuid(uid);
3011 }
3012 /*
3013 **  LOAD_IF_NAMES -- load interface-specific names into $=w
3014 **
3015 **	Parameters:
3016 **		none.
3017 **
3018 **	Returns:
3019 **		none.
3020 **
3021 **	Side Effects:
3022 **		Loads $=w with the names of all the interfaces.
3023 */
3024 
3025 #ifdef SIOCGIFCONF
3026 struct rtentry;
3027 struct mbuf;
3028 # include <arpa/inet.h>
3029 # include <sys/time.h>
3030 # include <net/if.h>
3031 #endif
3032 
3033 void
3034 load_if_names()
3035 {
3036 #ifdef SIOCGIFCONF
3037 	int s;
3038 	int i;
3039         struct ifconf ifc;
3040 	char interfacebuf[1024];
3041 
3042 	s = socket(AF_INET, SOCK_DGRAM, 0);
3043 	if (s == -1)
3044 		return;
3045 
3046 	/* get the list of known IP address from the kernel */
3047         ifc.ifc_buf = interfacebuf;
3048         ifc.ifc_len = sizeof interfacebuf;
3049 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
3050 	{
3051 		if (tTd(0, 4))
3052 			printf("SIOGIFCONF failed: %s\n", errstring(errno));
3053 		close(s);
3054 		return;
3055 	}
3056 	close(s);
3057 
3058 	/* scan the list of IP address */
3059 	if (tTd(0, 4))
3060 		printf("scanning for interface specific names, ifc_len=%d\n",
3061 			ifc.ifc_len);
3062 
3063 	for (i = 0; i < ifc.ifc_len; )
3064 	{
3065 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
3066 		struct sockaddr *sa = &ifr->ifr_addr;
3067 		struct in_addr ia;
3068 		struct hostent *hp;
3069 		char ip_addr[256];
3070 		extern char *inet_ntoa();
3071 		extern struct hostent *gethostbyaddr();
3072 
3073 #ifdef BSD4_4_SOCKADDR
3074 		if (sa->sa_len > sizeof ifr->ifr_addr)
3075 			i += sizeof ifr->ifr_name + sa->sa_len;
3076 		else
3077 #endif
3078 			i += sizeof *ifr;
3079 
3080 		if (tTd(0, 20))
3081 			printf("%s\n", anynet_ntoa((SOCKADDR *) sa));
3082 
3083 		if (ifr->ifr_addr.sa_family != AF_INET)
3084 			continue;
3085 
3086 		/* extract IP address from the list*/
3087 		ia = (((struct sockaddr_in *) sa)->sin_addr);
3088 
3089 		/* save IP address in text from */
3090 		(void) sprintf(ip_addr, "[%s]",
3091 			inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
3092 		if (!wordinclass(ip_addr, 'w'))
3093 		{
3094 			setclass('w', ip_addr);
3095 			if (tTd(0, 4))
3096 				printf("\ta.k.a.: %s\n", ip_addr);
3097 		}
3098 
3099 		/* skip "loopback" interface "lo" */
3100 		if (strcmp("lo0", ifr->ifr_name) == 0)
3101 			continue;
3102 
3103 		/* lookup name with IP address */
3104 		hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET);
3105 		if (hp == NULL)
3106 		{
3107 			syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n",
3108 				inet_ntoa(ia));
3109 			continue;
3110 		}
3111 
3112 		/* save its cname */
3113 		if (!wordinclass(hp->h_name, 'w'))
3114 		{
3115 			setclass('w', hp->h_name);
3116 			if (tTd(0, 4))
3117 				printf("\ta.k.a.: %s\n", hp->h_name);
3118 		}
3119 
3120 		/* save all it aliases name */
3121 		while (*hp->h_aliases)
3122 		{
3123 			if (!wordinclass(*hp->h_aliases, 'w'))
3124 			{
3125 				setclass('w', *hp->h_aliases);
3126 				if (tTd(0, 4))
3127 				printf("\ta.k.a.: %s\n", *hp->h_aliases);
3128 			}
3129 			hp->h_aliases++;
3130 		}
3131 	}
3132 #endif
3133 }
3134 /*
3135 **  NI_PROPVAL -- netinfo property value lookup routine
3136 **
3137 **	Parameters:
3138 **		keydir -- the Netinfo directory name in which to search
3139 **			for the key.
3140 **		keyprop -- the name of the property in which to find the
3141 **			property we are interested.  Defaults to "name".
3142 **		keyval -- the value for which we are really searching.
3143 **		valprop -- the property name for the value in which we
3144 **			are interested.
3145 **		sepchar -- if non-nil, this can be multiple-valued, and
3146 **			we should return a string separated by this
3147 **			character.
3148 **
3149 **	Returns:
3150 **		NULL -- if:
3151 **			1. the directory is not found
3152 **			2. the property name is not found
3153 **			3. the property contains multiple values
3154 **			4. some error occured
3155 **		else -- the location of the config file.
3156 **
3157 **	Example:
3158 **		To search for an alias value, use:
3159 **		  ni_propval("/aliases", "name", aliasname, "members", ',')
3160 **
3161 **	Notes:
3162 **      	Caller should free the return value of ni_proval
3163 */
3164 
3165 #ifdef NETINFO
3166 
3167 # include <netinfo/ni.h>
3168 
3169 # define LOCAL_NETINFO_DOMAIN    "."
3170 # define PARENT_NETINFO_DOMAIN   ".."
3171 # define MAX_NI_LEVELS           256
3172 
3173 char *
3174 ni_propval(keydir, keyprop, keyval, valprop, sepchar)
3175 	char *keydir;
3176 	char *keyprop;
3177 	char *keyval;
3178 	char *valprop;
3179 	char sepchar;
3180 {
3181 	char *propval = NULL;
3182 	int i;
3183 	int j, alen;
3184 	void *ni = NULL;
3185 	void *lastni = NULL;
3186 	ni_status nis;
3187 	ni_id nid;
3188 	ni_namelist ninl;
3189 	register char *p;
3190 	char keybuf[1024];
3191 
3192 	/*
3193 	**  Create the full key from the two parts.
3194 	**
3195 	**	Note that directory can end with, e.g., "name=" to specify
3196 	**	an alternate search property.
3197 	*/
3198 
3199 	i = strlen(keydir) + strlen(keyval) + 2;
3200 	if (keyprop != NULL)
3201 		i += strlen(keyprop) + 1;
3202 	if (i > sizeof keybuf)
3203 		return NULL;
3204 	strcpy(keybuf, keydir);
3205 	strcat(keybuf, "/");
3206 	if (keyprop != NULL)
3207 	{
3208 		strcat(keybuf, keyprop);
3209 		strcat(keybuf, "=");
3210 	}
3211 	strcat(keybuf, keyval);
3212 
3213 	/*
3214 	**  If the passed directory and property name are found
3215 	**  in one of netinfo domains we need to search (starting
3216 	**  from the local domain moving all the way back to the
3217 	**  root domain) set propval to the property's value
3218 	**  and return it.
3219 	*/
3220 
3221 	for (i = 0; i < MAX_NI_LEVELS; ++i)
3222 	{
3223 		if (i == 0)
3224 		{
3225 			nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
3226 		}
3227 		else
3228 		{
3229 			if (lastni != NULL)
3230 				ni_free(lastni);
3231 			lastni = ni;
3232 			nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
3233 		}
3234 
3235 		/*
3236 		**  Don't bother if we didn't get a handle on a
3237 		**  proper domain.  This is not necessarily an error.
3238 		**  We would get a positive ni_status if, for instance
3239 		**  we never found the directory or property and tried
3240 		**  to open the parent of the root domain!
3241 		*/
3242 
3243 		if (nis != 0)
3244 			break;
3245 
3246 		/*
3247 		**  Find the path to the server information.
3248 		*/
3249 
3250 		if (ni_pathsearch(ni, &nid, keybuf) != 0)
3251 			continue;
3252 
3253 		/*
3254 		**  Find associated value information.
3255 		*/
3256 
3257 		if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
3258 			continue;
3259 
3260 		/*
3261 		**  See if we have an acceptable number of values.
3262 		*/
3263 
3264 		if (ninl.ni_namelist_len <= 0)
3265 			continue;
3266 
3267 		if (sepchar == '\0' && ninl.ni_namelist_len > 1)
3268 		{
3269 			ni_namelist_free(&ninl);
3270 			continue;
3271 		}
3272 
3273 		/*
3274 		**  Calculate number of bytes needed and build result
3275 		*/
3276 
3277 		alen = 1;
3278 		for (j = 0; j < ninl.ni_namelist_len; j++)
3279 			alen += strlen(ninl.ni_namelist_val[j]) + 1;
3280 		propval = p = xalloc(alen);
3281 		for (j = 0; j < ninl.ni_namelist_len; j++)
3282 		{
3283 			strcpy(p, ninl.ni_namelist_val[j]);
3284 			p += strlen(p);
3285 			*p++ = sepchar;
3286 		}
3287 		*--p = '\0';
3288 
3289 		ni_namelist_free(&ninl);
3290 	}
3291 
3292 	/*
3293 	**  Clean up.
3294 	*/
3295 
3296 	if (ni != NULL)
3297 		ni_free(ni);
3298 	if (lastni != NULL && ni != lastni)
3299 		ni_free(lastni);
3300 
3301 	return propval;
3302 }
3303 
3304 #endif /* NETINFO */
3305 /*
3306 **  HARD_SYSLOG -- call syslog repeatedly until it works
3307 **
3308 **	Needed on HP-UX, which apparently doesn't guarantee that
3309 **	syslog succeeds during interrupt handlers.
3310 */
3311 
3312 #ifdef __hpux
3313 
3314 # define MAXSYSLOGTRIES	100
3315 # undef syslog
3316 
3317 # ifdef __STDC__
3318 hard_syslog(int pri, char *msg, ...)
3319 # else
3320 hard_syslog(pri, msg, va_alist)
3321 	int pri;
3322 	char *msg;
3323 	va_dcl
3324 # endif
3325 {
3326 	int i;
3327 	char buf[SYSLOG_BUFSIZE * 2];
3328 	VA_LOCAL_DECL;
3329 
3330 	VA_START(msg);
3331 	vsprintf(buf, msg, ap);
3332 	VA_END;
3333 
3334 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; )
3335 		continue;
3336 }
3337 
3338 #endif
3339 /*
3340 **  LOCAL_HOSTNAME_LENGTH
3341 **
3342 **	This is required to get sendmail to compile against BIND 4.9.x
3343 **	on Ultrix.
3344 */
3345 
3346 #if defined(ultrix) && NAMED_BIND
3347 
3348 # include <resolv.h>
3349 # if __RES >= 19931104
3350 
3351 int
3352 local_hostname_length(hostname)
3353 	char *hostname;
3354 {
3355 	int len_host, len_domain;
3356 
3357 	if (!*_res.defdname)
3358 		res_init();
3359 	len_host = strlen(hostname);
3360 	len_domain = strlen(_res.defdname);
3361 	if (len_host > len_domain &&
3362 	    (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) &&
3363 	    hostname[len_host - len_domain - 1] == '.')
3364 		return len_host - len_domain - 1;
3365 	else
3366 		return 0;
3367 }
3368 
3369 # endif
3370 #endif
3371 /*
3372 **  Compile-Time options
3373 */
3374 
3375 char	*CompileOptions[] =
3376 {
3377 #if HESIOD
3378 	"HESIOD",
3379 #endif
3380 #if LOG
3381 	"LOG",
3382 #endif
3383 #if MATCHGECOS
3384 	"MATCHGECOS",
3385 #endif
3386 #if NAMED_BIND
3387 	"NAMED_BIND",
3388 #endif
3389 #if NDBM
3390 	"NDBM",
3391 #endif
3392 #if NETINET
3393 	"NETINET",
3394 #endif
3395 #if NETINFO
3396 	"NETINFO",
3397 #endif
3398 #if NETISO
3399 	"NETISO",
3400 #endif
3401 #if NETNS
3402 	"NETNS",
3403 #endif
3404 #if NETUNIX
3405 	"NETUNIX",
3406 #endif
3407 #if NETX25
3408 	"NETX25",
3409 #endif
3410 #if NEWDB
3411 	"NEWDB",
3412 #endif
3413 #if NIS
3414 	"NIS",
3415 #endif
3416 #if NISPLUS
3417 	"NISPLUS",
3418 #endif
3419 #if SCANF
3420 	"SCANF",
3421 #endif
3422 #if SUID_ROOT_FILES_OK
3423 	"SUID_ROOT_FILES_OK",
3424 #endif
3425 #if USERDB
3426 	"USERDB",
3427 #endif
3428 #if XDEBUG
3429 	"XDEBUG",
3430 #endif
3431 #if XLA
3432 	"XLA",
3433 #endif
3434 	NULL
3435 };
3436