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