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