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