xref: /freebsd/contrib/sendmail/src/conf.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*
2  * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  */
13 
14 #include <sendmail.h>
15 
16 SM_RCSID("@(#)$Id: conf.c,v 8.972.2.27 2003/01/15 19:17:14 ca Exp $")
17 
18 #include <sendmail/pathnames.h>
19 
20 # include <sys/ioctl.h>
21 # include <sys/param.h>
22 
23 #include <limits.h>
24 #if NETINET || NETINET6
25 # include <arpa/inet.h>
26 #endif /* NETINET || NETINET6 */
27 #if HASULIMIT && defined(HPUX11)
28 # include <ulimit.h>
29 #endif /* HASULIMIT && defined(HPUX11) */
30 
31 static void	setupmaps __P((void));
32 static void	setupmailers __P((void));
33 static void	setupqueues __P((void));
34 static int	get_num_procs_online __P((void));
35 
36 
37 /*
38 **  CONF.C -- Sendmail Configuration Tables.
39 **
40 **	Defines the configuration of this installation.
41 **
42 **	Configuration Variables:
43 **		HdrInfo -- a table describing well-known header fields.
44 **			Each entry has the field name and some flags,
45 **			which are described in sendmail.h.
46 **
47 **	Notes:
48 **		I have tried to put almost all the reasonable
49 **		configuration information into the configuration
50 **		file read at runtime.  My intent is that anything
51 **		here is a function of the version of UNIX you
52 **		are running, or is really static -- for example
53 **		the headers are a superset of widely used
54 **		protocols.  If you find yourself playing with
55 **		this file too much, you may be making a mistake!
56 */
57 
58 
59 /*
60 **  Header info table
61 **	Final (null) entry contains the flags used for any other field.
62 **
63 **	Not all of these are actually handled specially by sendmail
64 **	at this time.  They are included as placeholders, to let
65 **	you know that "someday" I intend to have sendmail do
66 **	something with them.
67 */
68 
69 struct hdrinfo	HdrInfo[] =
70 {
71 		/* originator fields, most to least significant */
72 	{ "resent-sender",		H_FROM|H_RESENT,	NULL	},
73 	{ "resent-from",		H_FROM|H_RESENT,	NULL	},
74 	{ "resent-reply-to",		H_FROM|H_RESENT,	NULL	},
75 	{ "sender",			H_FROM,			NULL	},
76 	{ "from",			H_FROM,			NULL	},
77 	{ "reply-to",			H_FROM,			NULL	},
78 	{ "errors-to",			H_FROM|H_ERRORSTO,	NULL	},
79 	{ "full-name",			H_ACHECK,		NULL	},
80 	{ "return-receipt-to",		H_RECEIPTTO,		NULL	},
81 	{ "disposition-notification-to",	H_FROM,		NULL	},
82 
83 		/* destination fields */
84 	{ "to",				H_RCPT,			NULL	},
85 	{ "resent-to",			H_RCPT|H_RESENT,	NULL	},
86 	{ "cc",				H_RCPT,			NULL	},
87 	{ "resent-cc",			H_RCPT|H_RESENT,	NULL	},
88 	{ "bcc",			H_RCPT|H_BCC,		NULL	},
89 	{ "resent-bcc",			H_RCPT|H_BCC|H_RESENT,	NULL	},
90 	{ "apparently-to",		H_RCPT,			NULL	},
91 
92 		/* message identification and control */
93 	{ "message-id",			0,			NULL	},
94 	{ "resent-message-id",		H_RESENT,		NULL	},
95 	{ "message",			H_EOH,			NULL	},
96 	{ "text",			H_EOH,			NULL	},
97 
98 		/* date fields */
99 	{ "date",			0,			NULL	},
100 	{ "resent-date",		H_RESENT,		NULL	},
101 
102 		/* trace fields */
103 	{ "received",			H_TRACE|H_FORCE,	NULL	},
104 	{ "x400-received",		H_TRACE|H_FORCE,	NULL	},
105 	{ "via",			H_TRACE|H_FORCE,	NULL	},
106 	{ "mail-from",			H_TRACE|H_FORCE,	NULL	},
107 
108 		/* miscellaneous fields */
109 	{ "comments",			H_FORCE|H_ENCODABLE,	NULL	},
110 	{ "return-path",		H_FORCE|H_ACHECK|H_BINDLATE,	NULL	},
111 	{ "content-transfer-encoding",	H_CTE,			NULL	},
112 	{ "content-type",		H_CTYPE,		NULL	},
113 	{ "content-length",		H_ACHECK,		NULL	},
114 	{ "subject",			H_ENCODABLE,		NULL	},
115 	{ "x-authentication-warning",	H_FORCE,		NULL	},
116 
117 	{ NULL,				0,			NULL	}
118 };
119 
120 
121 
122 /*
123 **  Privacy values
124 */
125 
126 struct prival PrivacyValues[] =
127 {
128 	{ "public",		PRIV_PUBLIC		},
129 	{ "needmailhelo",	PRIV_NEEDMAILHELO	},
130 	{ "needexpnhelo",	PRIV_NEEDEXPNHELO	},
131 	{ "needvrfyhelo",	PRIV_NEEDVRFYHELO	},
132 	{ "noexpn",		PRIV_NOEXPN		},
133 	{ "novrfy",		PRIV_NOVRFY		},
134 	{ "restrictexpand",	PRIV_RESTRICTEXPAND	},
135 	{ "restrictmailq",	PRIV_RESTRICTMAILQ	},
136 	{ "restrictqrun",	PRIV_RESTRICTQRUN	},
137 	{ "noetrn",		PRIV_NOETRN		},
138 	{ "noverb",		PRIV_NOVERB		},
139 	{ "authwarnings",	PRIV_AUTHWARNINGS	},
140 	{ "noreceipts",		PRIV_NORECEIPTS		},
141 	{ "nobodyreturn",	PRIV_NOBODYRETN		},
142 	{ "goaway",		PRIV_GOAWAY		},
143 	{ NULL,			0			}
144 };
145 
146 /*
147 **  DontBlameSendmail values
148 */
149 
150 struct dbsval DontBlameSendmailValues[] =
151 {
152 	{ "safe",			DBS_SAFE			},
153 	{ "assumesafechown",		DBS_ASSUMESAFECHOWN		},
154 	{ "groupwritabledirpathsafe",	DBS_GROUPWRITABLEDIRPATHSAFE	},
155 	{ "groupwritableforwardfilesafe",
156 					DBS_GROUPWRITABLEFORWARDFILESAFE },
157 	{ "groupwritableincludefilesafe",
158 					DBS_GROUPWRITABLEINCLUDEFILESAFE },
159 	{ "groupwritablealiasfile",	DBS_GROUPWRITABLEALIASFILE	},
160 	{ "worldwritablealiasfile",	DBS_WORLDWRITABLEALIASFILE	},
161 	{ "forwardfileinunsafedirpath",	DBS_FORWARDFILEINUNSAFEDIRPATH	},
162 	{ "includefileinunsafedirpath",	DBS_INCLUDEFILEINUNSAFEDIRPATH	},
163 	{ "mapinunsafedirpath",		DBS_MAPINUNSAFEDIRPATH	},
164 	{ "linkedaliasfileinwritabledir",
165 					DBS_LINKEDALIASFILEINWRITABLEDIR },
166 	{ "linkedclassfileinwritabledir",
167 					DBS_LINKEDCLASSFILEINWRITABLEDIR },
168 	{ "linkedforwardfileinwritabledir",
169 					DBS_LINKEDFORWARDFILEINWRITABLEDIR },
170 	{ "linkedincludefileinwritabledir",
171 					DBS_LINKEDINCLUDEFILEINWRITABLEDIR },
172 	{ "linkedmapinwritabledir",	DBS_LINKEDMAPINWRITABLEDIR	},
173 	{ "linkedserviceswitchfileinwritabledir",
174 					DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR },
175 	{ "filedeliverytohardlink",	DBS_FILEDELIVERYTOHARDLINK	},
176 	{ "filedeliverytosymlink",	DBS_FILEDELIVERYTOSYMLINK	},
177 	{ "writemaptohardlink",		DBS_WRITEMAPTOHARDLINK		},
178 	{ "writemaptosymlink",		DBS_WRITEMAPTOSYMLINK		},
179 	{ "writestatstohardlink",	DBS_WRITESTATSTOHARDLINK	},
180 	{ "writestatstosymlink",	DBS_WRITESTATSTOSYMLINK		},
181 	{ "forwardfileingroupwritabledirpath",
182 					DBS_FORWARDFILEINGROUPWRITABLEDIRPATH },
183 	{ "includefileingroupwritabledirpath",
184 					DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH },
185 	{ "classfileinunsafedirpath",	DBS_CLASSFILEINUNSAFEDIRPATH	},
186 	{ "errorheaderinunsafedirpath",	DBS_ERRORHEADERINUNSAFEDIRPATH	},
187 	{ "helpfileinunsafedirpath",	DBS_HELPFILEINUNSAFEDIRPATH	},
188 	{ "forwardfileinunsafedirpathsafe",
189 					DBS_FORWARDFILEINUNSAFEDIRPATHSAFE },
190 	{ "includefileinunsafedirpathsafe",
191 					DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE },
192 	{ "runprograminunsafedirpath",	DBS_RUNPROGRAMINUNSAFEDIRPATH	},
193 	{ "runwritableprogram",		DBS_RUNWRITABLEPROGRAM		},
194 	{ "nonrootsafeaddr",		DBS_NONROOTSAFEADDR		},
195 	{ "truststickybit",		DBS_TRUSTSTICKYBIT		},
196 	{ "dontwarnforwardfileinunsafedirpath",
197 					DBS_DONTWARNFORWARDFILEINUNSAFEDIRPATH },
198 	{ "insufficiententropy",	DBS_INSUFFICIENTENTROPY },
199 	{ "groupreadablesasldbfile",	DBS_GROUPREADABLESASLDBFILE	},
200 	{ "groupwritablesasldbfile",	DBS_GROUPWRITABLESASLDBFILE	},
201 	{ "groupwritableforwardfile",	DBS_GROUPWRITABLEFORWARDFILE	},
202 	{ "groupwritableincludefile",	DBS_GROUPWRITABLEINCLUDEFILE	},
203 	{ "worldwritableforwardfile",	DBS_WORLDWRITABLEFORWARDFILE	},
204 	{ "worldwritableincludefile",	DBS_WORLDWRITABLEINCLUDEFILE	},
205 	{ "groupreadablekeyfile",	DBS_GROUPREADABLEKEYFILE	},
206 #if _FFR_GROUPREADABLEAUTHINFOFILE
207 	{ "groupreadableadefaultauthinfofile",
208 					DBS_GROUPREADABLEAUTHINFOFILE	},
209 #endif /* _FFR_GROUPREADABLEAUTHINFOFILE */
210 	{ NULL,				0				}
211 };
212 
213 /*
214 **  Miscellaneous stuff.
215 */
216 
217 int	DtableSize =	50;		/* max open files; reset in 4.2bsd */
218 /*
219 **  SETDEFAULTS -- set default values
220 **
221 **	Some of these must be initialized using direct code since they
222 **	depend on run-time values. So let's do all of them this way.
223 **
224 **	Parameters:
225 **		e -- the default envelope.
226 **
227 **	Returns:
228 **		none.
229 **
230 **	Side Effects:
231 **		Initializes a bunch of global variables to their
232 **		default values.
233 */
234 
235 #define MINUTES		* 60
236 #define HOURS		* 60 MINUTES
237 #define DAYS		* 24 HOURS
238 
239 #ifndef MAXRULERECURSION
240 # define MAXRULERECURSION	50	/* max ruleset recursion depth */
241 #endif /* ! MAXRULERECURSION */
242 
243 void
244 setdefaults(e)
245 	register ENVELOPE *e;
246 {
247 	int i;
248 	int numprocs;
249 	struct passwd *pw;
250 
251 	numprocs = get_num_procs_online();
252 	SpaceSub = ' ';				/* option B */
253 	QueueLA = 8 * numprocs;			/* option x */
254 	RefuseLA = 12 * numprocs;		/* option X */
255 	WkRecipFact = 30000L;			/* option y */
256 	WkClassFact = 1800L;			/* option z */
257 	WkTimeFact = 90000L;			/* option Z */
258 	QueueFactor = WkRecipFact * 20;		/* option q */
259 #if _FFR_QUARANTINE
260 	QueueMode = QM_NORMAL;		/* what queue items to act upon */
261 #endif /* _FFR_QUARANTINE */
262 	FileMode = (RealUid != geteuid()) ? 0644 : 0600;
263 						/* option F */
264 	QueueFileMode = (RealUid != geteuid()) ? 0644 : 0600;
265 						/* option QueueFileMode */
266 
267 	if (((pw = sm_getpwnam("mailnull")) != NULL && pw->pw_uid != 0) ||
268 	    ((pw = sm_getpwnam("sendmail")) != NULL && pw->pw_uid != 0) ||
269 	    ((pw = sm_getpwnam("daemon")) != NULL && pw->pw_uid != 0))
270 	{
271 		DefUid = pw->pw_uid;		/* option u */
272 		DefGid = pw->pw_gid;		/* option g */
273 		DefUser = newstr(pw->pw_name);
274 	}
275 	else
276 	{
277 		DefUid = 1;			/* option u */
278 		DefGid = 1;			/* option g */
279 		setdefuser();
280 	}
281 	TrustedUid = 0;
282 	if (tTd(37, 4))
283 		sm_dprintf("setdefaults: DefUser=%s, DefUid=%d, DefGid=%d\n",
284 			DefUser != NULL ? DefUser : "<1:1>",
285 			(int) DefUid, (int) DefGid);
286 	CheckpointInterval = 10;		/* option C */
287 	MaxHopCount = 25;			/* option h */
288 	set_delivery_mode(SM_FORK, e);		/* option d */
289 	e->e_errormode = EM_PRINT;		/* option e */
290 	e->e_qgrp = NOQGRP;
291 	e->e_qdir = NOQDIR;
292 	e->e_xfqgrp = NOQGRP;
293 	e->e_xfqdir = NOQDIR;
294 	e->e_ctime = curtime();
295 	SevenBitInput = false;			/* option 7 */
296 	MaxMciCache = 1;			/* option k */
297 	MciCacheTimeout = 5 MINUTES;		/* option K */
298 	LogLevel = 9;				/* option L */
299 #if MILTER
300 	MilterLogLevel = -1;
301 #endif /* MILTER */
302 	inittimeouts(NULL, false);		/* option r */
303 	PrivacyFlags = PRIV_PUBLIC;		/* option p */
304 	MeToo = true;				/* option m */
305 	SendMIMEErrors = true;			/* option f */
306 	SuperSafe = SAFE_REALLY;		/* option s */
307 	clrbitmap(DontBlameSendmail);		/* DontBlameSendmail option */
308 #if MIME8TO7
309 	MimeMode = MM_CVTMIME|MM_PASS8BIT;	/* option 8 */
310 #else /* MIME8TO7 */
311 	MimeMode = MM_PASS8BIT;
312 #endif /* MIME8TO7 */
313 	for (i = 0; i < MAXTOCLASS; i++)
314 	{
315 		TimeOuts.to_q_return[i] = 5 DAYS;	/* option T */
316 		TimeOuts.to_q_warning[i] = 0;		/* option T */
317 	}
318 	ServiceSwitchFile = "/etc/mail/service.switch";
319 	ServiceCacheMaxAge = (time_t) 10;
320 	HostsFile = _PATH_HOSTS;
321 	PidFile = newstr(_PATH_SENDMAILPID);
322 	MustQuoteChars = "@,;:\\()[].'";
323 	MciInfoTimeout = 30 MINUTES;
324 	MaxRuleRecursion = MAXRULERECURSION;
325 	MaxAliasRecursion = 10;
326 	MaxMacroRecursion = 10;
327 	ColonOkInAddr = true;
328 	DontLockReadFiles = true;
329 	DontProbeInterfaces = DPI_PROBEALL;
330 	DoubleBounceAddr = "postmaster";
331 	MaxHeadersLength = MAXHDRSLEN;
332 	MaxForwardEntries = 0;
333 	FastSplit = 1;
334 #if SASL
335 	AuthMechanisms = newstr(AUTH_MECHANISMS);
336 	MaxSLBits = INT_MAX;
337 #endif /* SASL */
338 #if STARTTLS
339 	TLS_Srv_Opts = TLS_I_SRV;
340 #endif /* STARTTLS */
341 #ifdef HESIOD_INIT
342 	HesiodContext = NULL;
343 #endif /* HESIOD_INIT */
344 #if NETINET6
345 	/* Detect if IPv6 is available at run time */
346 	i = socket(AF_INET6, SOCK_STREAM, 0);
347 	if (i >= 0)
348 	{
349 		InetMode = AF_INET6;
350 		(void) close(i);
351 	}
352 	else
353 		InetMode = AF_INET;
354 #else /* NETINET6 */
355 	InetMode = AF_INET;
356 #endif /* NETINET6 */
357 	ControlSocketName = NULL;
358 	memset(&ConnectOnlyTo, '\0', sizeof ConnectOnlyTo);
359 	DataFileBufferSize = 4096;
360 	XscriptFileBufferSize = 4096;
361 	for (i = 0; i < MAXRWSETS; i++)
362 		RuleSetNames[i] = NULL;
363 #if MILTER
364 	InputFilters[0] = NULL;
365 #endif /* MILTER */
366 #if _FFR_REJECT_LOG
367 	RejectLogInterval = 3 HOURS;
368 #endif /* _FFR_REJECT_LOG */
369 #if _FFR_REQ_DIR_FSYNC_OPT
370 	RequiresDirfsync = true;
371 #endif /* _FFR_REQ_DIR_FSYNC_OPT */
372 	setupmaps();
373 	setupqueues();
374 	setupmailers();
375 	setupheaders();
376 }
377 
378 
379 /*
380 **  SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
381 */
382 
383 void
384 setdefuser()
385 {
386 	struct passwd *defpwent;
387 	static char defuserbuf[40];
388 
389 	DefUser = defuserbuf;
390 	defpwent = sm_getpwuid(DefUid);
391 	(void) sm_strlcpy(defuserbuf,
392 			  (defpwent == NULL || defpwent->pw_name == NULL)
393 			   ? "nobody" : defpwent->pw_name,
394 			  sizeof defuserbuf);
395 	if (tTd(37, 4))
396 		sm_dprintf("setdefuser: DefUid=%d, DefUser=%s\n",
397 			   (int) DefUid, DefUser);
398 }
399 /*
400 **  SETUPQUEUES -- initialize default queues
401 **
402 **	The mqueue QUEUE structure gets filled in after readcf() but
403 **	we need something to point to now for the mailer setup,
404 **	which use "mqueue" as default queue.
405 */
406 
407 static void
408 setupqueues()
409 {
410 	char buf[100];
411 
412 	MaxRunnersPerQueue = 1;
413 	(void) sm_strlcpy(buf, "mqueue, P=/var/spool/mqueue", sizeof buf);
414 	makequeue(buf, false);
415 }
416 /*
417 **  SETUPMAILERS -- initialize default mailers
418 */
419 
420 static void
421 setupmailers()
422 {
423 	char buf[100];
424 
425 	(void) sm_strlcpy(buf, "prog, P=/bin/sh, F=lsouDq9, T=X-Unix/X-Unix/X-Unix, A=sh -c \201u",
426 			sizeof buf);
427 	makemailer(buf);
428 
429 	(void) sm_strlcpy(buf, "*file*, P=[FILE], F=lsDFMPEouq9, T=X-Unix/X-Unix/X-Unix, A=FILE \201u",
430 			sizeof buf);
431 	makemailer(buf);
432 
433 	(void) sm_strlcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE \201u",
434 			sizeof buf);
435 	makemailer(buf);
436 	initerrmailers();
437 }
438 /*
439 **  SETUPMAPS -- set up map classes
440 */
441 
442 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
443 	{ \
444 		extern bool parse __P((MAP *, char *)); \
445 		extern bool open __P((MAP *, int)); \
446 		extern void close __P((MAP *)); \
447 		extern char *lookup __P((MAP *, char *, char **, int *)); \
448 		extern void store __P((MAP *, char *, char *)); \
449 		s = stab(name, ST_MAPCLASS, ST_ENTER); \
450 		s->s_mapclass.map_cname = name; \
451 		s->s_mapclass.map_ext = ext; \
452 		s->s_mapclass.map_cflags = flags; \
453 		s->s_mapclass.map_parse = parse; \
454 		s->s_mapclass.map_open = open; \
455 		s->s_mapclass.map_close = close; \
456 		s->s_mapclass.map_lookup = lookup; \
457 		s->s_mapclass.map_store = store; \
458 	}
459 
460 static void
461 setupmaps()
462 {
463 	register STAB *s;
464 
465 #if NEWDB
466 	MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
467 		map_parseargs, hash_map_open, db_map_close,
468 		db_map_lookup, db_map_store);
469 
470 	MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
471 		map_parseargs, bt_map_open, db_map_close,
472 		db_map_lookup, db_map_store);
473 #endif /* NEWDB */
474 
475 #if NDBM
476 	MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
477 		map_parseargs, ndbm_map_open, ndbm_map_close,
478 		ndbm_map_lookup, ndbm_map_store);
479 #endif /* NDBM */
480 
481 #if NIS
482 	MAPDEF("nis", NULL, MCF_ALIASOK,
483 		map_parseargs, nis_map_open, null_map_close,
484 		nis_map_lookup, null_map_store);
485 #endif /* NIS */
486 
487 #if NISPLUS
488 	MAPDEF("nisplus", NULL, MCF_ALIASOK,
489 		map_parseargs, nisplus_map_open, null_map_close,
490 		nisplus_map_lookup, null_map_store);
491 #endif /* NISPLUS */
492 
493 #if LDAPMAP
494 	MAPDEF("ldap", NULL, MCF_ALIASOK|MCF_NOTPERSIST,
495 		ldapmap_parseargs, ldapmap_open, ldapmap_close,
496 		ldapmap_lookup, null_map_store);
497 #endif /* LDAPMAP */
498 
499 #if PH_MAP
500 	MAPDEF("ph", NULL, MCF_NOTPERSIST,
501 		ph_map_parseargs, ph_map_open, ph_map_close,
502 		ph_map_lookup, null_map_store);
503 #endif /* PH_MAP */
504 
505 #if MAP_NSD
506 	/* IRIX 6.5 nsd support */
507 	MAPDEF("nsd", NULL, MCF_ALIASOK,
508 	       map_parseargs, null_map_open, null_map_close,
509 	       nsd_map_lookup, null_map_store);
510 #endif /* MAP_NSD */
511 
512 #if HESIOD
513 	MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
514 		map_parseargs, hes_map_open, hes_map_close,
515 		hes_map_lookup, null_map_store);
516 #endif /* HESIOD */
517 
518 #if NETINFO
519 	MAPDEF("netinfo", NULL, MCF_ALIASOK,
520 		map_parseargs, ni_map_open, null_map_close,
521 		ni_map_lookup, null_map_store);
522 #endif /* NETINFO */
523 
524 #if 0
525 	MAPDEF("dns", NULL, 0,
526 		dns_map_init, null_map_open, null_map_close,
527 		dns_map_lookup, null_map_store);
528 #endif /* 0 */
529 
530 #if NAMED_BIND
531 # if DNSMAP
532 #  if _FFR_DNSMAP_ALIASABLE
533 	MAPDEF("dns", NULL, MCF_ALIASOK,
534 	       dns_map_parseargs, dns_map_open, null_map_close,
535 	       dns_map_lookup, null_map_store);
536 #  else /* _FFR_DNSMAP_ALIASABLE */
537 	MAPDEF("dns", NULL, 0,
538 	       dns_map_parseargs, dns_map_open, null_map_close,
539 	       dns_map_lookup, null_map_store);
540 #  endif /* _FFR_DNSMAP_ALIASABLE */
541 # endif /* DNSMAP */
542 #endif /* NAMED_BIND */
543 
544 #if NAMED_BIND
545 	/* best MX DNS lookup */
546 	MAPDEF("bestmx", NULL, MCF_OPTFILE,
547 		map_parseargs, null_map_open, null_map_close,
548 		bestmx_map_lookup, null_map_store);
549 #endif /* NAMED_BIND */
550 
551 	MAPDEF("host", NULL, 0,
552 		host_map_init, null_map_open, null_map_close,
553 		host_map_lookup, null_map_store);
554 
555 	MAPDEF("text", NULL, MCF_ALIASOK,
556 		map_parseargs, text_map_open, null_map_close,
557 		text_map_lookup, null_map_store);
558 
559 	MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
560 		map_parseargs, stab_map_open, null_map_close,
561 		stab_map_lookup, stab_map_store);
562 
563 	MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
564 		map_parseargs, impl_map_open, impl_map_close,
565 		impl_map_lookup, impl_map_store);
566 
567 	/* access to system passwd file */
568 	MAPDEF("user", NULL, MCF_OPTFILE,
569 		map_parseargs, user_map_open, null_map_close,
570 		user_map_lookup, null_map_store);
571 
572 	/* dequote map */
573 	MAPDEF("dequote", NULL, 0,
574 		dequote_init, null_map_open, null_map_close,
575 		dequote_map, null_map_store);
576 
577 #if MAP_REGEX
578 	MAPDEF("regex", NULL, 0,
579 		regex_map_init, null_map_open, null_map_close,
580 		regex_map_lookup, null_map_store);
581 #endif /* MAP_REGEX */
582 
583 #if USERDB
584 	/* user database */
585 	MAPDEF("userdb", ".db", 0,
586 		map_parseargs, null_map_open, null_map_close,
587 		udb_map_lookup, null_map_store);
588 #endif /* USERDB */
589 
590 	/* arbitrary programs */
591 	MAPDEF("program", NULL, MCF_ALIASOK,
592 		map_parseargs, null_map_open, null_map_close,
593 		prog_map_lookup, null_map_store);
594 
595 	/* sequenced maps */
596 	MAPDEF("sequence", NULL, MCF_ALIASOK,
597 		seq_map_parse, null_map_open, null_map_close,
598 		seq_map_lookup, seq_map_store);
599 
600 	/* switched interface to sequenced maps */
601 	MAPDEF("switch", NULL, MCF_ALIASOK,
602 		map_parseargs, switch_map_open, null_map_close,
603 		seq_map_lookup, seq_map_store);
604 
605 	/* null map lookup -- really for internal use only */
606 	MAPDEF("null", NULL, MCF_ALIASOK|MCF_OPTFILE,
607 		map_parseargs, null_map_open, null_map_close,
608 		null_map_lookup, null_map_store);
609 
610 	/* syslog map -- logs information to syslog */
611 	MAPDEF("syslog", NULL, 0,
612 		syslog_map_parseargs, null_map_open, null_map_close,
613 		syslog_map_lookup, null_map_store);
614 
615 	/* macro storage map -- rulesets can set macros */
616 	MAPDEF("macro", NULL, 0,
617 		dequote_init, null_map_open, null_map_close,
618 		macro_map_lookup, null_map_store);
619 
620 	/* arithmetic map -- add/subtract/compare */
621 	MAPDEF("arith", NULL, 0,
622 		dequote_init, null_map_open, null_map_close,
623 		arith_map_lookup, null_map_store);
624 
625 	if (tTd(38, 2))
626 	{
627 		/* bogus map -- always return tempfail */
628 		MAPDEF("bogus",	NULL, MCF_ALIASOK|MCF_OPTFILE,
629 		       map_parseargs, null_map_open, null_map_close,
630 		       bogus_map_lookup, null_map_store);
631 	}
632 }
633 
634 #undef MAPDEF
635 /*
636 **  INITHOSTMAPS -- initial host-dependent maps
637 **
638 **	This should act as an interface to any local service switch
639 **	provided by the host operating system.
640 **
641 **	Parameters:
642 **		none
643 **
644 **	Returns:
645 **		none
646 **
647 **	Side Effects:
648 **		Should define maps "host" and "users" as necessary
649 **		for this OS.  If they are not defined, they will get
650 **		a default value later.  It should check to make sure
651 **		they are not defined first, since it's possible that
652 **		the config file has provided an override.
653 */
654 
655 void
656 inithostmaps()
657 {
658 	register int i;
659 	int nmaps;
660 	char *maptype[MAXMAPSTACK];
661 	short mapreturn[MAXMAPACTIONS];
662 	char buf[MAXLINE];
663 
664 	/*
665 	**  Set up default hosts maps.
666 	*/
667 
668 #if 0
669 	nmaps = switch_map_find("hosts", maptype, mapreturn);
670 	for (i = 0; i < nmaps; i++)
671 	{
672 		if (strcmp(maptype[i], "files") == 0 &&
673 		    stab("hosts.files", ST_MAP, ST_FIND) == NULL)
674 		{
675 			(void) sm_strlcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts",
676 				sizeof buf);
677 			(void) makemapentry(buf);
678 		}
679 # if NAMED_BIND
680 		else if (strcmp(maptype[i], "dns") == 0 &&
681 			 stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
682 		{
683 			(void) sm_strlcpy(buf, "hosts.dns dns A", sizeof buf);
684 			(void) makemapentry(buf);
685 		}
686 # endif /* NAMED_BIND */
687 # if NISPLUS
688 		else if (strcmp(maptype[i], "nisplus") == 0 &&
689 			 stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
690 		{
691 			(void) sm_strlcpy(buf, "hosts.nisplus nisplus -k name -v address hosts.org_dir",
692 				sizeof buf);
693 			(void) makemapentry(buf);
694 		}
695 # endif /* NISPLUS */
696 # if NIS
697 		else if (strcmp(maptype[i], "nis") == 0 &&
698 			 stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
699 		{
700 			(void) sm_strlcpy(buf, "hosts.nis nis -k 0 -v 1 hosts.byname",
701 				sizeof buf);
702 			(void) makemapentry(buf);
703 		}
704 # endif /* NIS */
705 # if NETINFO
706 		else if (strcmp(maptype[i], "netinfo") == 0 &&
707 			 stab("hosts.netinfo", ST_MAP, ST_FIND) == NULL)
708 		{
709 			(void) sm_strlcpy(buf, "hosts.netinfo netinfo -v name /machines",
710 				sizeof buf);
711 			(void) makemapentry(buf);
712 		}
713 # endif /* NETINFO */
714 	}
715 #endif /* 0 */
716 
717 	/*
718 	**  Make sure we have a host map.
719 	*/
720 
721 	if (stab("host", ST_MAP, ST_FIND) == NULL)
722 	{
723 		/* user didn't initialize: set up host map */
724 		(void) sm_strlcpy(buf, "host host", sizeof buf);
725 #if NAMED_BIND
726 		if (ConfigLevel >= 2)
727 			(void) sm_strlcat(buf, " -a. -D", sizeof buf);
728 #endif /* NAMED_BIND */
729 		(void) makemapentry(buf);
730 	}
731 
732 	/*
733 	**  Set up default aliases maps
734 	*/
735 
736 	nmaps = switch_map_find("aliases", maptype, mapreturn);
737 	for (i = 0; i < nmaps; i++)
738 	{
739 		if (strcmp(maptype[i], "files") == 0 &&
740 		    stab("aliases.files", ST_MAP, ST_FIND) == NULL)
741 		{
742 			(void) sm_strlcpy(buf, "aliases.files null",
743 					  sizeof buf);
744 			(void) makemapentry(buf);
745 		}
746 #if NISPLUS
747 		else if (strcmp(maptype[i], "nisplus") == 0 &&
748 			 stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
749 		{
750 			(void) sm_strlcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion mail_aliases.org_dir",
751 				sizeof buf);
752 			(void) makemapentry(buf);
753 		}
754 #endif /* NISPLUS */
755 #if NIS
756 		else if (strcmp(maptype[i], "nis") == 0 &&
757 			 stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
758 		{
759 			(void) sm_strlcpy(buf, "aliases.nis nis mail.aliases",
760 				sizeof buf);
761 			(void) makemapentry(buf);
762 		}
763 #endif /* NIS */
764 #if NETINFO
765 		else if (strcmp(maptype[i], "netinfo") == 0 &&
766 			 stab("aliases.netinfo", ST_MAP, ST_FIND) == NULL)
767 		{
768 			(void) sm_strlcpy(buf, "aliases.netinfo netinfo -z, /aliases",
769 				sizeof buf);
770 			(void) makemapentry(buf);
771 		}
772 #endif /* NETINFO */
773 #if HESIOD
774 		else if (strcmp(maptype[i], "hesiod") == 0 &&
775 			 stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL)
776 		{
777 			(void) sm_strlcpy(buf, "aliases.hesiod hesiod aliases",
778 				sizeof buf);
779 			(void) makemapentry(buf);
780 		}
781 #endif /* HESIOD */
782 	}
783 	if (stab("aliases", ST_MAP, ST_FIND) == NULL)
784 	{
785 		(void) sm_strlcpy(buf, "aliases switch aliases", sizeof buf);
786 		(void) makemapentry(buf);
787 	}
788 
789 #if 0		/* "user" map class is a better choice */
790 	/*
791 	**  Set up default users maps.
792 	*/
793 
794 	nmaps = switch_map_find("passwd", maptype, mapreturn);
795 	for (i = 0; i < nmaps; i++)
796 	{
797 		if (strcmp(maptype[i], "files") == 0 &&
798 		    stab("users.files", ST_MAP, ST_FIND) == NULL)
799 		{
800 			(void) sm_strlcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd",
801 				sizeof buf);
802 			(void) makemapentry(buf);
803 		}
804 # if NISPLUS
805 		else if (strcmp(maptype[i], "nisplus") == 0 &&
806 		    stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
807 		{
808 			(void) sm_strlcpy(buf, "users.nisplus nisplus -m -kname -vhome passwd.org_dir",
809 				sizeof buf);
810 			(void) makemapentry(buf);
811 		}
812 # endif /* NISPLUS */
813 # if NIS
814 		else if (strcmp(maptype[i], "nis") == 0 &&
815 		    stab("users.nis", ST_MAP, ST_FIND) == NULL)
816 		{
817 			(void) sm_strlcpy(buf, "users.nis nis -m passwd.byname",
818 				sizeof buf);
819 			(void) makemapentry(buf);
820 		}
821 # endif /* NIS */
822 # if HESIOD
823 		else if (strcmp(maptype[i], "hesiod") == 0 &&
824 			 stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
825 		{
826 			(void) sm_strlcpy(buf, "users.hesiod hesiod", sizeof buf);
827 			(void) makemapentry(buf);
828 		}
829 # endif /* HESIOD */
830 	}
831 	if (stab("users", ST_MAP, ST_FIND) == NULL)
832 	{
833 		(void) sm_strlcpy(buf, "users switch -m passwd", sizeof buf);
834 		(void) makemapentry(buf);
835 	}
836 #endif /* 0 */
837 }
838 /*
839 **  SWITCH_MAP_FIND -- find the list of types associated with a map
840 **
841 **	This is the system-dependent interface to the service switch.
842 **
843 **	Parameters:
844 **		service -- the name of the service of interest.
845 **		maptype -- an out-array of strings containing the types
846 **			of access to use for this service.  There can
847 **			be at most MAXMAPSTACK types for a single service.
848 **		mapreturn -- an out-array of return information bitmaps
849 **			for the map.
850 **
851 **	Returns:
852 **		The number of map types filled in, or -1 for failure.
853 **
854 **	Side effects:
855 **		Preserves errno so nothing in the routine clobbers it.
856 */
857 
858 #if defined(SOLARIS) || (defined(sony_news) && defined(__svr4))
859 # define _USE_SUN_NSSWITCH_
860 #endif /* defined(SOLARIS) || (defined(sony_news) && defined(__svr4)) */
861 
862 #if _FFR_HPUX_NSSWITCH
863 # ifdef __hpux
864 #  define _USE_SUN_NSSWITCH_
865 # endif /* __hpux */
866 #endif /* _FFR_HPUX_NSSWITCH */
867 
868 #ifdef _USE_SUN_NSSWITCH_
869 # include <nsswitch.h>
870 #endif /* _USE_SUN_NSSWITCH_ */
871 
872 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
873 # define _USE_DEC_SVC_CONF_
874 #endif /* defined(ultrix) || (defined(__osf__) && defined(__alpha)) */
875 
876 #ifdef _USE_DEC_SVC_CONF_
877 # include <sys/svcinfo.h>
878 #endif /* _USE_DEC_SVC_CONF_ */
879 
880 int
881 switch_map_find(service, maptype, mapreturn)
882 	char *service;
883 	char *maptype[MAXMAPSTACK];
884 	short mapreturn[MAXMAPACTIONS];
885 {
886 	int svcno = 0;
887 	int save_errno = errno;
888 
889 #ifdef _USE_SUN_NSSWITCH_
890 	struct __nsw_switchconfig *nsw_conf;
891 	enum __nsw_parse_err pserr;
892 	struct __nsw_lookup *lk;
893 	static struct __nsw_lookup lkp0 =
894 		{ "files", {1, 0, 0, 0}, NULL, NULL };
895 	static struct __nsw_switchconfig lkp_default =
896 		{ 0, "sendmail", 3, &lkp0 };
897 
898 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
899 		mapreturn[svcno] = 0;
900 
901 	if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
902 		lk = lkp_default.lookups;
903 	else
904 		lk = nsw_conf->lookups;
905 	svcno = 0;
906 	while (lk != NULL && svcno < MAXMAPSTACK)
907 	{
908 		maptype[svcno] = lk->service_name;
909 		if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
910 			mapreturn[MA_NOTFOUND] |= 1 << svcno;
911 		if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
912 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
913 		if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
914 			mapreturn[MA_TRYAGAIN] |= 1 << svcno;
915 		svcno++;
916 		lk = lk->next;
917 	}
918 	errno = save_errno;
919 	return svcno;
920 #endif /* _USE_SUN_NSSWITCH_ */
921 
922 #ifdef _USE_DEC_SVC_CONF_
923 	struct svcinfo *svcinfo;
924 	int svc;
925 
926 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
927 		mapreturn[svcno] = 0;
928 
929 	svcinfo = getsvc();
930 	if (svcinfo == NULL)
931 		goto punt;
932 	if (strcmp(service, "hosts") == 0)
933 		svc = SVC_HOSTS;
934 	else if (strcmp(service, "aliases") == 0)
935 		svc = SVC_ALIASES;
936 	else if (strcmp(service, "passwd") == 0)
937 		svc = SVC_PASSWD;
938 	else
939 	{
940 		errno = save_errno;
941 		return -1;
942 	}
943 	for (svcno = 0; svcno < SVC_PATHSIZE && svcno < MAXMAPSTACK; svcno++)
944 	{
945 		switch (svcinfo->svcpath[svc][svcno])
946 		{
947 		  case SVC_LOCAL:
948 			maptype[svcno] = "files";
949 			break;
950 
951 		  case SVC_YP:
952 			maptype[svcno] = "nis";
953 			break;
954 
955 		  case SVC_BIND:
956 			maptype[svcno] = "dns";
957 			break;
958 
959 # ifdef SVC_HESIOD
960 		  case SVC_HESIOD:
961 			maptype[svcno] = "hesiod";
962 			break;
963 # endif /* SVC_HESIOD */
964 
965 		  case SVC_LAST:
966 			errno = save_errno;
967 			return svcno;
968 		}
969 	}
970 	errno = save_errno;
971 	return svcno;
972 #endif /* _USE_DEC_SVC_CONF_ */
973 
974 #if !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_)
975 	/*
976 	**  Fall-back mechanism.
977 	*/
978 
979 	STAB *st;
980 	static time_t servicecachetime;	/* time service switch was cached */
981 	time_t now = curtime();
982 
983 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
984 		mapreturn[svcno] = 0;
985 
986 	if ((now - servicecachetime) > (time_t) ServiceCacheMaxAge)
987 	{
988 		/* (re)read service switch */
989 		register SM_FILE_T *fp;
990 		long sff = SFF_REGONLY|SFF_OPENASROOT|SFF_NOLOCK;
991 
992 		if (!bitnset(DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR,
993 			    DontBlameSendmail))
994 			sff |= SFF_NOWLINK;
995 
996 		if (ConfigFileRead)
997 			servicecachetime = now;
998 		fp = safefopen(ServiceSwitchFile, O_RDONLY, 0, sff);
999 		if (fp != NULL)
1000 		{
1001 			char buf[MAXLINE];
1002 
1003 			while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf,
1004 					   sizeof buf) != NULL)
1005 			{
1006 				register char *p;
1007 
1008 				p = strpbrk(buf, "#\n");
1009 				if (p != NULL)
1010 					*p = '\0';
1011 				p = strpbrk(buf, " \t");
1012 				if (p != NULL)
1013 					*p++ = '\0';
1014 				if (buf[0] == '\0')
1015 					continue;
1016 				if (p == NULL)
1017 				{
1018 					sm_syslog(LOG_ERR, NOQID,
1019 						  "Bad line on %.100s: %.100s",
1020 						  ServiceSwitchFile,
1021 						  buf);
1022 					continue;
1023 				}
1024 				while (isspace(*p))
1025 					p++;
1026 				if (*p == '\0')
1027 					continue;
1028 
1029 				/*
1030 				**  Find/allocate space for this service entry.
1031 				**	Space for all of the service strings
1032 				**	are allocated at once.  This means
1033 				**	that we only have to free the first
1034 				**	one to free all of them.
1035 				*/
1036 
1037 				st = stab(buf, ST_SERVICE, ST_ENTER);
1038 				if (st->s_service[0] != NULL)
1039 					sm_free((void *) st->s_service[0]); /* XXX */
1040 				p = newstr(p);
1041 				for (svcno = 0; svcno < MAXMAPSTACK; )
1042 				{
1043 					if (*p == '\0')
1044 						break;
1045 					st->s_service[svcno++] = p;
1046 					p = strpbrk(p, " \t");
1047 					if (p == NULL)
1048 						break;
1049 					*p++ = '\0';
1050 					while (isspace(*p))
1051 						p++;
1052 				}
1053 				if (svcno < MAXMAPSTACK)
1054 					st->s_service[svcno] = NULL;
1055 			}
1056 			(void) sm_io_close(fp, SM_TIME_DEFAULT);
1057 		}
1058 	}
1059 
1060 	/* look up entry in cache */
1061 	st = stab(service, ST_SERVICE, ST_FIND);
1062 	if (st != NULL && st->s_service[0] != NULL)
1063 	{
1064 		/* extract data */
1065 		svcno = 0;
1066 		while (svcno < MAXMAPSTACK)
1067 		{
1068 			maptype[svcno] = st->s_service[svcno];
1069 			if (maptype[svcno++] == NULL)
1070 				break;
1071 		}
1072 		errno = save_errno;
1073 		return --svcno;
1074 	}
1075 #endif /* !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) */
1076 
1077 #if !defined(_USE_SUN_NSSWITCH_)
1078 	/* if the service file doesn't work, use an absolute fallback */
1079 # ifdef _USE_DEC_SVC_CONF_
1080   punt:
1081 # endif /* _USE_DEC_SVC_CONF_ */
1082 	for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
1083 		mapreturn[svcno] = 0;
1084 	svcno = 0;
1085 	if (strcmp(service, "aliases") == 0)
1086 	{
1087 		maptype[svcno++] = "files";
1088 # if defined(AUTO_NETINFO_ALIASES) && defined (NETINFO)
1089 		maptype[svcno++] = "netinfo";
1090 # endif /* defined(AUTO_NETINFO_ALIASES) && defined (NETINFO) */
1091 # ifdef AUTO_NIS_ALIASES
1092 #  if NISPLUS
1093 		maptype[svcno++] = "nisplus";
1094 #  endif /* NISPLUS */
1095 #  if NIS
1096 		maptype[svcno++] = "nis";
1097 #  endif /* NIS */
1098 # endif /* AUTO_NIS_ALIASES */
1099 		errno = save_errno;
1100 		return svcno;
1101 	}
1102 	if (strcmp(service, "hosts") == 0)
1103 	{
1104 # if NAMED_BIND
1105 		maptype[svcno++] = "dns";
1106 # else /* NAMED_BIND */
1107 #  if defined(sun) && !defined(BSD)
1108 		/* SunOS */
1109 		maptype[svcno++] = "nis";
1110 #  endif /* defined(sun) && !defined(BSD) */
1111 # endif /* NAMED_BIND */
1112 # if defined(AUTO_NETINFO_HOSTS) && defined (NETINFO)
1113 		maptype[svcno++] = "netinfo";
1114 # endif /* defined(AUTO_NETINFO_HOSTS) && defined (NETINFO) */
1115 		maptype[svcno++] = "files";
1116 		errno = save_errno;
1117 		return svcno;
1118 	}
1119 	errno = save_errno;
1120 	return -1;
1121 #endif /* !defined(_USE_SUN_NSSWITCH_) */
1122 }
1123 /*
1124 **  USERNAME -- return the user id of the logged in user.
1125 **
1126 **	Parameters:
1127 **		none.
1128 **
1129 **	Returns:
1130 **		The login name of the logged in user.
1131 **
1132 **	Side Effects:
1133 **		none.
1134 **
1135 **	Notes:
1136 **		The return value is statically allocated.
1137 */
1138 
1139 char *
1140 username()
1141 {
1142 	static char *myname = NULL;
1143 	extern char *getlogin();
1144 	register struct passwd *pw;
1145 
1146 	/* cache the result */
1147 	if (myname == NULL)
1148 	{
1149 		myname = getlogin();
1150 		if (myname == NULL || myname[0] == '\0')
1151 		{
1152 			pw = sm_getpwuid(RealUid);
1153 			if (pw != NULL)
1154 				myname = pw->pw_name;
1155 		}
1156 		else
1157 		{
1158 			uid_t uid = RealUid;
1159 
1160 			if ((pw = sm_getpwnam(myname)) == NULL ||
1161 			      (uid != 0 && uid != pw->pw_uid))
1162 			{
1163 				pw = sm_getpwuid(uid);
1164 				if (pw != NULL)
1165 					myname = pw->pw_name;
1166 			}
1167 		}
1168 		if (myname == NULL || myname[0] == '\0')
1169 		{
1170 			syserr("554 5.3.0 Who are you?");
1171 			myname = "postmaster";
1172 		}
1173 		else if (strpbrk(myname, ",;:/|\"\\") != NULL)
1174 			myname = addquotes(myname, NULL);
1175 		else
1176 			myname = sm_pstrdup_x(myname);
1177 	}
1178 	return myname;
1179 }
1180 /*
1181 **  TTYPATH -- Get the path of the user's tty
1182 **
1183 **	Returns the pathname of the user's tty.  Returns NULL if
1184 **	the user is not logged in or if s/he has write permission
1185 **	denied.
1186 **
1187 **	Parameters:
1188 **		none
1189 **
1190 **	Returns:
1191 **		pathname of the user's tty.
1192 **		NULL if not logged in or write permission denied.
1193 **
1194 **	Side Effects:
1195 **		none.
1196 **
1197 **	WARNING:
1198 **		Return value is in a local buffer.
1199 **
1200 **	Called By:
1201 **		savemail
1202 */
1203 
1204 char *
1205 ttypath()
1206 {
1207 	struct stat stbuf;
1208 	register char *pathn;
1209 	extern char *ttyname();
1210 	extern char *getlogin();
1211 
1212 	/* compute the pathname of the controlling tty */
1213 	if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
1214 	    (pathn = ttyname(0)) == NULL)
1215 	{
1216 		errno = 0;
1217 		return NULL;
1218 	}
1219 
1220 	/* see if we have write permission */
1221 	if (stat(pathn, &stbuf) < 0 || !bitset(S_IWOTH, stbuf.st_mode))
1222 	{
1223 		errno = 0;
1224 		return NULL;
1225 	}
1226 
1227 	/* see if the user is logged in */
1228 	if (getlogin() == NULL)
1229 		return NULL;
1230 
1231 	/* looks good */
1232 	return pathn;
1233 }
1234 /*
1235 **  CHECKCOMPAT -- check for From and To person compatible.
1236 **
1237 **	This routine can be supplied on a per-installation basis
1238 **	to determine whether a person is allowed to send a message.
1239 **	This allows restriction of certain types of internet
1240 **	forwarding or registration of users.
1241 **
1242 **	If the hosts are found to be incompatible, an error
1243 **	message should be given using "usrerr" and an EX_ code
1244 **	should be returned.  You can also set to->q_status to
1245 **	a DSN-style status code.
1246 **
1247 **	EF_NO_BODY_RETN can be set in e->e_flags to suppress the
1248 **	body during the return-to-sender function; this should be done
1249 **	on huge messages.  This bit may already be set by the ESMTP
1250 **	protocol.
1251 **
1252 **	Parameters:
1253 **		to -- the person being sent to.
1254 **
1255 **	Returns:
1256 **		an exit status
1257 **
1258 **	Side Effects:
1259 **		none (unless you include the usrerr stuff)
1260 */
1261 
1262 int
1263 checkcompat(to, e)
1264 	register ADDRESS *to;
1265 	register ENVELOPE *e;
1266 {
1267 	if (tTd(49, 1))
1268 		sm_dprintf("checkcompat(to=%s, from=%s)\n",
1269 			to->q_paddr, e->e_from.q_paddr);
1270 
1271 #ifdef EXAMPLE_CODE
1272 	/* this code is intended as an example only */
1273 	register STAB *s;
1274 
1275 	s = stab("arpa", ST_MAILER, ST_FIND);
1276 	if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
1277 	    to->q_mailer == s->s_mailer)
1278 	{
1279 		usrerr("553 No ARPA mail through this machine: see your system administration");
1280 		/* e->e_flags |= EF_NO_BODY_RETN; to suppress body on return */
1281 		to->q_status = "5.7.1";
1282 		return EX_UNAVAILABLE;
1283 	}
1284 #endif /* EXAMPLE_CODE */
1285 	return EX_OK;
1286 }
1287 /*
1288 **  INIT_MD -- do machine dependent initializations
1289 **
1290 **	Systems that have global modes that should be set should do
1291 **	them here rather than in main.
1292 */
1293 
1294 #ifdef _AUX_SOURCE
1295 # include <compat.h>
1296 #endif /* _AUX_SOURCE */
1297 
1298 #if SHARE_V1
1299 # include <shares.h>
1300 #endif /* SHARE_V1 */
1301 
1302 void
1303 init_md(argc, argv)
1304 	int argc;
1305 	char **argv;
1306 {
1307 #ifdef _AUX_SOURCE
1308 	setcompat(getcompat() | COMPAT_BSDPROT);
1309 #endif /* _AUX_SOURCE */
1310 
1311 #ifdef SUN_EXTENSIONS
1312 	init_md_sun();
1313 #endif /* SUN_EXTENSIONS */
1314 
1315 #if _CONVEX_SOURCE
1316 	/* keep gethostby*() from stripping the local domain name */
1317 	set_domain_trim_off();
1318 #endif /* _CONVEX_SOURCE */
1319 #ifdef __QNX__
1320 	/*
1321 	**  Due to QNX's network distributed nature, you can target a tcpip
1322 	**  stack on a different node in the qnx network; this patch lets
1323 	**  this feature work.  The __sock_locate() must be done before the
1324 	**  environment is clear.
1325 	*/
1326 	__sock_locate();
1327 #endif /* __QNX__ */
1328 #if SECUREWARE || defined(_SCO_unix_)
1329 	set_auth_parameters(argc, argv);
1330 
1331 # ifdef _SCO_unix_
1332 	/*
1333 	**  This is required for highest security levels (the kernel
1334 	**  won't let it call set*uid() or run setuid binaries without
1335 	**  it).  It may be necessary on other SECUREWARE systems.
1336 	*/
1337 
1338 	if (getluid() == -1)
1339 		setluid(0);
1340 # endif /* _SCO_unix_ */
1341 #endif /* SECUREWARE || defined(_SCO_unix_) */
1342 
1343 
1344 #ifdef VENDOR_DEFAULT
1345 	VendorCode = VENDOR_DEFAULT;
1346 #else /* VENDOR_DEFAULT */
1347 	VendorCode = VENDOR_BERKELEY;
1348 #endif /* VENDOR_DEFAULT */
1349 }
1350 /*
1351 **  INIT_VENDOR_MACROS -- vendor-dependent macro initializations
1352 **
1353 **	Called once, on startup.
1354 **
1355 **	Parameters:
1356 **		e -- the global envelope.
1357 **
1358 **	Returns:
1359 **		none.
1360 **
1361 **	Side Effects:
1362 **		vendor-dependent.
1363 */
1364 
1365 void
1366 init_vendor_macros(e)
1367 	register ENVELOPE *e;
1368 {
1369 }
1370 /*
1371 **  GETLA -- get the current load average
1372 **
1373 **	This code stolen from la.c.
1374 **
1375 **	Parameters:
1376 **		none.
1377 **
1378 **	Returns:
1379 **		The current load average as an integer.
1380 **
1381 **	Side Effects:
1382 **		none.
1383 */
1384 
1385 /* try to guess what style of load average we have */
1386 #define LA_ZERO		1	/* always return load average as zero */
1387 #define LA_INT		2	/* read kmem for avenrun; interpret as long */
1388 #define LA_FLOAT	3	/* read kmem for avenrun; interpret as float */
1389 #define LA_SUBR		4	/* call getloadavg */
1390 #define LA_MACH		5	/* MACH load averages (as on NeXT boxes) */
1391 #define LA_SHORT	6	/* read kmem for avenrun; interpret as short */
1392 #define LA_PROCSTR	7	/* read string ("1.17") from /proc/loadavg */
1393 #define LA_READKSYM	8	/* SVR4: use MIOC_READKSYM ioctl call */
1394 #define LA_DGUX		9	/* special DGUX implementation */
1395 #define LA_HPUX		10	/* special HPUX implementation */
1396 #define LA_IRIX6	11	/* special IRIX 6.2 implementation */
1397 #define LA_KSTAT	12	/* special Solaris kstat(3k) implementation */
1398 #define LA_DEVSHORT	13	/* read short from a device */
1399 #define LA_ALPHAOSF	14	/* Digital UNIX (OSF/1 on Alpha) table() call */
1400 #define LA_PSET		15	/* Solaris per-processor-set load average */
1401 
1402 /* do guesses based on general OS type */
1403 #ifndef LA_TYPE
1404 # define LA_TYPE	LA_ZERO
1405 #endif /* ! LA_TYPE */
1406 
1407 #ifndef FSHIFT
1408 # if defined(unixpc)
1409 #  define FSHIFT	5
1410 # endif /* defined(unixpc) */
1411 
1412 # if defined(__alpha) || defined(IRIX)
1413 #  define FSHIFT	10
1414 # endif /* defined(__alpha) || defined(IRIX) */
1415 
1416 #endif /* ! FSHIFT */
1417 
1418 #ifndef FSHIFT
1419 # define FSHIFT		8
1420 #endif /* ! FSHIFT */
1421 
1422 #ifndef FSCALE
1423 # define FSCALE		(1 << FSHIFT)
1424 #endif /* ! FSCALE */
1425 
1426 #ifndef LA_AVENRUN
1427 # ifdef SYSTEM5
1428 #  define LA_AVENRUN	"avenrun"
1429 # else /* SYSTEM5 */
1430 #  define LA_AVENRUN	"_avenrun"
1431 # endif /* SYSTEM5 */
1432 #endif /* ! LA_AVENRUN */
1433 
1434 /* _PATH_KMEM should be defined in <paths.h> */
1435 #ifndef _PATH_KMEM
1436 # define _PATH_KMEM	"/dev/kmem"
1437 #endif /* ! _PATH_KMEM */
1438 
1439 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
1440 
1441 # include <nlist.h>
1442 
1443 /* _PATH_UNIX should be defined in <paths.h> */
1444 # ifndef _PATH_UNIX
1445 #  if defined(SYSTEM5)
1446 #   define _PATH_UNIX	"/unix"
1447 #  else /* defined(SYSTEM5) */
1448 #   define _PATH_UNIX	"/vmunix"
1449 #  endif /* defined(SYSTEM5) */
1450 # endif /* ! _PATH_UNIX */
1451 
1452 # ifdef _AUX_SOURCE
1453 struct nlist	Nl[2];
1454 # else /* _AUX_SOURCE */
1455 struct nlist	Nl[] =
1456 {
1457 	{ LA_AVENRUN },
1458 	{ 0 },
1459 };
1460 # endif /* _AUX_SOURCE */
1461 # define X_AVENRUN	0
1462 
1463 int
1464 getla()
1465 {
1466 	int j;
1467 	static int kmem = -1;
1468 # if LA_TYPE == LA_INT
1469 	long avenrun[3];
1470 # else /* LA_TYPE == LA_INT */
1471 #  if LA_TYPE == LA_SHORT
1472 	short avenrun[3];
1473 #  else /* LA_TYPE == LA_SHORT */
1474 	double avenrun[3];
1475 #  endif /* LA_TYPE == LA_SHORT */
1476 # endif /* LA_TYPE == LA_INT */
1477 	extern int errno;
1478 	extern off_t lseek();
1479 
1480 	if (kmem < 0)
1481 	{
1482 # ifdef _AUX_SOURCE
1483 		(void) sm_strlcpy(Nl[X_AVENRUN].n_name, LA_AVENRUN,
1484 			       sizeof Nl[X_AVENRUN].n_name);
1485 		Nl[1].n_name[0] = '\0';
1486 # endif /* _AUX_SOURCE */
1487 
1488 # if defined(_AIX3) || defined(_AIX4)
1489 		if (knlist(Nl, 1, sizeof Nl[0]) < 0)
1490 # else /* defined(_AIX3) || defined(_AIX4) */
1491 		if (nlist(_PATH_UNIX, Nl) < 0)
1492 # endif /* defined(_AIX3) || defined(_AIX4) */
1493 		{
1494 			if (tTd(3, 1))
1495 				sm_dprintf("getla: nlist(%s): %s\n", _PATH_UNIX,
1496 					   sm_errstring(errno));
1497 			return -1;
1498 		}
1499 		if (Nl[X_AVENRUN].n_value == 0)
1500 		{
1501 			if (tTd(3, 1))
1502 				sm_dprintf("getla: nlist(%s, %s) ==> 0\n",
1503 					_PATH_UNIX, LA_AVENRUN);
1504 			return -1;
1505 		}
1506 # ifdef NAMELISTMASK
1507 		Nl[X_AVENRUN].n_value &= NAMELISTMASK;
1508 # endif /* NAMELISTMASK */
1509 
1510 		kmem = open(_PATH_KMEM, 0, 0);
1511 		if (kmem < 0)
1512 		{
1513 			if (tTd(3, 1))
1514 				sm_dprintf("getla: open(/dev/kmem): %s\n",
1515 					   sm_errstring(errno));
1516 			return -1;
1517 		}
1518 		if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1519 		    fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1520 		{
1521 			if (tTd(3, 1))
1522 				sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1523 					   sm_errstring(errno));
1524 			(void) close(kmem);
1525 			kmem = -1;
1526 			return -1;
1527 		}
1528 	}
1529 	if (tTd(3, 20))
1530 		sm_dprintf("getla: symbol address = %#lx\n",
1531 			(unsigned long) Nl[X_AVENRUN].n_value);
1532 	if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
1533 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
1534 	{
1535 		/* thank you Ian */
1536 		if (tTd(3, 1))
1537 			sm_dprintf("getla: lseek or read: %s\n",
1538 				   sm_errstring(errno));
1539 		return -1;
1540 	}
1541 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
1542 	if (tTd(3, 5))
1543 	{
1544 #  if LA_TYPE == LA_SHORT
1545 		sm_dprintf("getla: avenrun = %d", avenrun[0]);
1546 		if (tTd(3, 15))
1547 			sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1548 #  else /* LA_TYPE == LA_SHORT */
1549 		sm_dprintf("getla: avenrun = %ld", avenrun[0]);
1550 		if (tTd(3, 15))
1551 			sm_dprintf(", %ld, %ld", avenrun[1], avenrun[2]);
1552 #  endif /* LA_TYPE == LA_SHORT */
1553 		sm_dprintf("\n");
1554 	}
1555 	if (tTd(3, 1))
1556 		sm_dprintf("getla: %d\n",
1557 			(int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1558 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1559 # else /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) */
1560 	if (tTd(3, 5))
1561 	{
1562 		sm_dprintf("getla: avenrun = %g", avenrun[0]);
1563 		if (tTd(3, 15))
1564 			sm_dprintf(", %g, %g", avenrun[1], avenrun[2]);
1565 		sm_dprintf("\n");
1566 	}
1567 	if (tTd(3, 1))
1568 		sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1569 	return ((int) (avenrun[0] + 0.5));
1570 # endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) */
1571 }
1572 
1573 #endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) */
1574 
1575 #if LA_TYPE == LA_READKSYM
1576 
1577 # include <sys/ksym.h>
1578 
1579 int
1580 getla()
1581 {
1582 	int j;
1583 	static int kmem = -1;
1584 	long avenrun[3];
1585 	extern int errno;
1586 	struct mioc_rksym mirk;
1587 
1588 	if (kmem < 0)
1589 	{
1590 		kmem = open("/dev/kmem", 0, 0);
1591 		if (kmem < 0)
1592 		{
1593 			if (tTd(3, 1))
1594 				sm_dprintf("getla: open(/dev/kmem): %s\n",
1595 					   sm_errstring(errno));
1596 			return -1;
1597 		}
1598 		if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1599 		    fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1600 		{
1601 			if (tTd(3, 1))
1602 				sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1603 					   sm_errstring(errno));
1604 			(void) close(kmem);
1605 			kmem = -1;
1606 			return -1;
1607 		}
1608 	}
1609 	mirk.mirk_symname = LA_AVENRUN;
1610 	mirk.mirk_buf = avenrun;
1611 	mirk.mirk_buflen = sizeof(avenrun);
1612 	if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
1613 	{
1614 		if (tTd(3, 1))
1615 			sm_dprintf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
1616 				   sm_errstring(errno));
1617 		return -1;
1618 	}
1619 	if (tTd(3, 5))
1620 	{
1621 		sm_dprintf("getla: avenrun = %d", avenrun[0]);
1622 		if (tTd(3, 15))
1623 			sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1624 		sm_dprintf("\n");
1625 	}
1626 	if (tTd(3, 1))
1627 		sm_dprintf("getla: %d\n",
1628 			(int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1629 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1630 }
1631 
1632 #endif /* LA_TYPE == LA_READKSYM */
1633 
1634 #if LA_TYPE == LA_DGUX
1635 
1636 # include <sys/dg_sys_info.h>
1637 
1638 int
1639 getla()
1640 {
1641 	struct dg_sys_info_load_info load_info;
1642 
1643 	dg_sys_info((long *)&load_info,
1644 		DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
1645 
1646 	if (tTd(3, 1))
1647 		sm_dprintf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1648 
1649 	return ((int) (load_info.one_minute + 0.5));
1650 }
1651 
1652 #endif /* LA_TYPE == LA_DGUX */
1653 
1654 #if LA_TYPE == LA_HPUX
1655 
1656 /* forward declarations to keep gcc from complaining */
1657 struct pst_dynamic;
1658 struct pst_status;
1659 struct pst_static;
1660 struct pst_vminfo;
1661 struct pst_diskinfo;
1662 struct pst_processor;
1663 struct pst_lv;
1664 struct pst_swapinfo;
1665 
1666 # include <sys/param.h>
1667 # include <sys/pstat.h>
1668 
1669 int
1670 getla()
1671 {
1672 	struct pst_dynamic pstd;
1673 
1674 	if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1675 			     (size_t) 1, 0) == -1)
1676 		return 0;
1677 
1678 	if (tTd(3, 1))
1679 		sm_dprintf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1680 
1681 	return (int) (pstd.psd_avg_1_min + 0.5);
1682 }
1683 
1684 #endif /* LA_TYPE == LA_HPUX */
1685 
1686 #if LA_TYPE == LA_SUBR
1687 
1688 int
1689 getla()
1690 {
1691 	double avenrun[3];
1692 
1693 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
1694 	{
1695 		if (tTd(3, 1))
1696 			sm_dprintf("getla: getloadavg failed: %s",
1697 				   sm_errstring(errno));
1698 		return -1;
1699 	}
1700 	if (tTd(3, 1))
1701 		sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1702 	return ((int) (avenrun[0] + 0.5));
1703 }
1704 
1705 #endif /* LA_TYPE == LA_SUBR */
1706 
1707 #if LA_TYPE == LA_MACH
1708 
1709 /*
1710 **  This has been tested on NEXTSTEP release 2.1/3.X.
1711 */
1712 
1713 # if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
1714 #  include <mach/mach.h>
1715 # else /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1716 #  include <mach.h>
1717 # endif /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1718 
1719 int
1720 getla()
1721 {
1722 	processor_set_t default_set;
1723 	kern_return_t error;
1724 	unsigned int info_count;
1725 	struct processor_set_basic_info info;
1726 	host_t host;
1727 
1728 	error = processor_set_default(host_self(), &default_set);
1729 	if (error != KERN_SUCCESS)
1730 	{
1731 		if (tTd(3, 1))
1732 			sm_dprintf("getla: processor_set_default failed: %s",
1733 				   sm_errstring(errno));
1734 		return -1;
1735 	}
1736 	info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
1737 	if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
1738 			       &host, (processor_set_info_t)&info,
1739 			       &info_count) != KERN_SUCCESS)
1740 	{
1741 		if (tTd(3, 1))
1742 			sm_dprintf("getla: processor_set_info failed: %s",
1743 				   sm_errstring(errno));
1744 		return -1;
1745 	}
1746 	if (tTd(3, 1))
1747 		sm_dprintf("getla: %d\n",
1748 			(int) ((info.load_average + (LOAD_SCALE / 2)) /
1749 			       LOAD_SCALE));
1750 	return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
1751 }
1752 
1753 #endif /* LA_TYPE == LA_MACH */
1754 
1755 #if LA_TYPE == LA_PROCSTR
1756 # if SM_CONF_BROKEN_STRTOD
1757 	ERROR: This OS has most likely a broken strtod() implemenentation.
1758 	ERROR: The function is required for getla().
1759 	ERROR: Check the compilation options _LA_PROCSTR and
1760 	ERROR: _SM_CONF_BROKEN_STRTOD (without the leading _).
1761 # endif /* SM_CONF_BROKEN_STRTOD */
1762 
1763 /*
1764 **  Read /proc/loadavg for the load average.  This is assumed to be
1765 **  in a format like "0.15 0.12 0.06".
1766 **
1767 **	Initially intended for Linux.  This has been in the kernel
1768 **	since at least 0.99.15.
1769 */
1770 
1771 # ifndef _PATH_LOADAVG
1772 #  define _PATH_LOADAVG	"/proc/loadavg"
1773 # endif /* ! _PATH_LOADAVG */
1774 
1775 int
1776 getla()
1777 {
1778 	double avenrun;
1779 	register int result;
1780 	SM_FILE_T *fp;
1781 
1782 	fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_LOADAVG, SM_IO_RDONLY,
1783 			NULL);
1784 	if (fp == NULL)
1785 	{
1786 		if (tTd(3, 1))
1787 			sm_dprintf("getla: sm_io_open(%s): %s\n",
1788 				   _PATH_LOADAVG, sm_errstring(errno));
1789 		return -1;
1790 	}
1791 	result = sm_io_fscanf(fp, SM_TIME_DEFAULT, "%lf", &avenrun);
1792 	(void) sm_io_close(fp, SM_TIME_DEFAULT);
1793 	if (result != 1)
1794 	{
1795 		if (tTd(3, 1))
1796 			sm_dprintf("getla: sm_io_fscanf() = %d: %s\n",
1797 				   result, sm_errstring(errno));
1798 		return -1;
1799 	}
1800 
1801 	if (tTd(3, 1))
1802 		sm_dprintf("getla(): %.2f\n", avenrun);
1803 
1804 	return ((int) (avenrun + 0.5));
1805 }
1806 
1807 #endif /* LA_TYPE == LA_PROCSTR */
1808 
1809 #if LA_TYPE == LA_IRIX6
1810 
1811 # include <sys/sysmp.h>
1812 
1813 int
1814 getla(void)
1815 {
1816 	int j;
1817 	static int kmem = -1;
1818 	int avenrun[3];
1819 
1820 	if (kmem < 0)
1821 	{
1822 		kmem = open(_PATH_KMEM, 0, 0);
1823 		if (kmem < 0)
1824 		{
1825 			if (tTd(3, 1))
1826 				sm_dprintf("getla: open(%s): %s\n", _PATH_KMEM,
1827 					   sm_errstring(errno));
1828 			return -1;
1829 		}
1830 		if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1831 		    fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1832 		{
1833 			if (tTd(3, 1))
1834 				sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1835 					   sm_errstring(errno));
1836 			(void) close(kmem);
1837 			kmem = -1;
1838 			return -1;
1839 		}
1840 	}
1841 
1842 	if (lseek(kmem, (sysmp(MP_KERNADDR, MPKA_AVENRUN) & 0x7fffffff), SEEK_SET) == -1 ||
1843 	    read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
1844 	{
1845 		if (tTd(3, 1))
1846 			sm_dprintf("getla: lseek or read: %s\n",
1847 				   sm_errstring(errno));
1848 		return -1;
1849 	}
1850 	if (tTd(3, 5))
1851 	{
1852 		sm_dprintf("getla: avenrun = %ld", (long int) avenrun[0]);
1853 		if (tTd(3, 15))
1854 			sm_dprintf(", %ld, %ld",
1855 				(long int) avenrun[1], (long int) avenrun[2]);
1856 		sm_dprintf("\n");
1857 	}
1858 
1859 	if (tTd(3, 1))
1860 		sm_dprintf("getla: %d\n",
1861 			(int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1862 	return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1863 
1864 }
1865 #endif /* LA_TYPE == LA_IRIX6 */
1866 
1867 #if LA_TYPE == LA_KSTAT
1868 
1869 # include <kstat.h>
1870 
1871 int
1872 getla()
1873 {
1874 	static kstat_ctl_t *kc = NULL;
1875 	static kstat_t *ksp = NULL;
1876 	kstat_named_t *ksn;
1877 	int la;
1878 
1879 	if (kc == NULL)		/* if not initialized before */
1880 		kc = kstat_open();
1881 	if (kc == NULL)
1882 	{
1883 		if (tTd(3, 1))
1884 			sm_dprintf("getla: kstat_open(): %s\n",
1885 				   sm_errstring(errno));
1886 		return -1;
1887 	}
1888 	if (ksp == NULL)
1889 		ksp = kstat_lookup(kc, "unix", 0, "system_misc");
1890 	if (ksp == NULL)
1891 	{
1892 		if (tTd(3, 1))
1893 			sm_dprintf("getla: kstat_lookup(): %s\n",
1894 				   sm_errstring(errno));
1895 		return -1;
1896 	}
1897 	if (kstat_read(kc, ksp, NULL) < 0)
1898 	{
1899 		if (tTd(3, 1))
1900 			sm_dprintf("getla: kstat_read(): %s\n",
1901 				   sm_errstring(errno));
1902 		return -1;
1903 	}
1904 	ksn = (kstat_named_t *) kstat_data_lookup(ksp, "avenrun_1min");
1905 	la = ((double) ksn->value.ul + FSCALE/2) / FSCALE;
1906 	/* kstat_close(kc); /o do not close for fast access */
1907 	return la;
1908 }
1909 
1910 #endif /* LA_TYPE == LA_KSTAT */
1911 
1912 #if LA_TYPE == LA_DEVSHORT
1913 
1914 /*
1915 **  Read /dev/table/avenrun for the load average.  This should contain
1916 **  three shorts for the 1, 5, and 15 minute loads.  We only read the
1917 **  first, since that's all we care about.
1918 **
1919 **	Intended for SCO OpenServer 5.
1920 */
1921 
1922 # ifndef _PATH_AVENRUN
1923 #  define _PATH_AVENRUN	"/dev/table/avenrun"
1924 # endif /* ! _PATH_AVENRUN */
1925 
1926 int
1927 getla()
1928 {
1929 	static int afd = -1;
1930 	short avenrun;
1931 	int loadav;
1932 	int r;
1933 
1934 	errno = EBADF;
1935 
1936 	if (afd == -1 || lseek(afd, 0L, SEEK_SET) == -1)
1937 	{
1938 		if (errno != EBADF)
1939 			return -1;
1940 		afd = open(_PATH_AVENRUN, O_RDONLY|O_SYNC);
1941 		if (afd < 0)
1942 		{
1943 			sm_syslog(LOG_ERR, NOQID,
1944 				"can't open %s: %s",
1945 				_PATH_AVENRUN, sm_errstring(errno));
1946 			return -1;
1947 		}
1948 	}
1949 
1950 	r = read(afd, &avenrun, sizeof avenrun);
1951 
1952 	if (tTd(3, 5))
1953 		sm_dprintf("getla: avenrun = %d\n", avenrun);
1954 	loadav = (int) (avenrun + FSCALE/2) >> FSHIFT;
1955 	if (tTd(3, 1))
1956 		sm_dprintf("getla: %d\n", loadav);
1957 	return loadav;
1958 }
1959 
1960 #endif /* LA_TYPE == LA_DEVSHORT */
1961 
1962 #if LA_TYPE == LA_ALPHAOSF
1963 struct rtentry;
1964 struct mbuf;
1965 # include <sys/table.h>
1966 
1967 int
1968 getla()
1969 {
1970 	int ave = 0;
1971 	struct tbl_loadavg tab;
1972 
1973 	if (table(TBL_LOADAVG, 0, &tab, 1, sizeof(tab)) == -1)
1974 	{
1975 		if (tTd(3, 1))
1976 			sm_dprintf("getla: table %s\n", sm_errstring(errno));
1977 		return -1;
1978 	}
1979 
1980 	if (tTd(3, 1))
1981 		sm_dprintf("getla: scale = %d\n", tab.tl_lscale);
1982 
1983 	if (tab.tl_lscale)
1984 		ave = ((tab.tl_avenrun.l[2] + (tab.tl_lscale/2)) /
1985 		       tab.tl_lscale);
1986 	else
1987 		ave = (int) (tab.tl_avenrun.d[2] + 0.5);
1988 
1989 	if (tTd(3, 1))
1990 		sm_dprintf("getla: %d\n", ave);
1991 
1992 	return ave;
1993 }
1994 
1995 #endif /* LA_TYPE == LA_ALPHAOSF */
1996 
1997 #if LA_TYPE == LA_PSET
1998 
1999 int
2000 getla()
2001 {
2002 	double avenrun[3];
2003 
2004 	if (pset_getloadavg(PS_MYID, avenrun,
2005 			    sizeof(avenrun) / sizeof(avenrun[0])) < 0)
2006 	{
2007 		if (tTd(3, 1))
2008 			sm_dprintf("getla: pset_getloadavg failed: %s",
2009 				   sm_errstring(errno));
2010 		return -1;
2011 	}
2012 	if (tTd(3, 1))
2013 		sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
2014 	return ((int) (avenrun[0] + 0.5));
2015 }
2016 
2017 #endif /* LA_TYPE == LA_PSET */
2018 
2019 #if LA_TYPE == LA_ZERO
2020 
2021 int
2022 getla()
2023 {
2024 	if (tTd(3, 1))
2025 		sm_dprintf("getla: ZERO\n");
2026 	return 0;
2027 }
2028 
2029 #endif /* LA_TYPE == LA_ZERO */
2030 
2031 /*
2032  * Copyright 1989 Massachusetts Institute of Technology
2033  *
2034  * Permission to use, copy, modify, distribute, and sell this software and its
2035  * documentation for any purpose is hereby granted without fee, provided that
2036  * the above copyright notice appear in all copies and that both that
2037  * copyright notice and this permission notice appear in supporting
2038  * documentation, and that the name of M.I.T. not be used in advertising or
2039  * publicity pertaining to distribution of the software without specific,
2040  * written prior permission.  M.I.T. makes no representations about the
2041  * suitability of this software for any purpose.  It is provided "as is"
2042  * without express or implied warranty.
2043  *
2044  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
2045  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
2046  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2047  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
2048  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2049  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2050  *
2051  * Authors:  Many and varied...
2052  */
2053 
2054 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
2055 #ifndef lint
2056 SM_UNUSED(static char  rcsid[]) = "@(#)$OrigId: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
2057 #endif /* ! lint */
2058 
2059 #ifdef apollo
2060 # undef volatile
2061 # include <apollo/base.h>
2062 
2063 /* ARGSUSED */
2064 int getloadavg( call_data )
2065 	caddr_t call_data;	/* pointer to (double) return value */
2066 {
2067 	double *avenrun = (double *) call_data;
2068 	int i;
2069 	status_$t      st;
2070 	long loadav[3];
2071 
2072 	proc1_$get_loadav(loadav, &st);
2073 	*avenrun = loadav[0] / (double) (1 << 16);
2074 	return 0;
2075 }
2076 #endif /* apollo */
2077 /*
2078 **  SM_GETLA -- get the current load average
2079 **
2080 **	Parameters:
2081 **		none
2082 **
2083 **	Returns:
2084 **		none
2085 **
2086 **	Side Effects:
2087 **		Set CurrentLA to the current load average.
2088 **		Set {load_avg} in GlobalMacros to the current load average.
2089 */
2090 
2091 void
2092 sm_getla()
2093 {
2094 	char labuf[8];
2095 
2096 	CurrentLA = getla();
2097 	(void) sm_snprintf(labuf, sizeof labuf, "%d", CurrentLA);
2098 	macdefine(&GlobalMacros, A_TEMP, macid("{load_avg}"), labuf);
2099 }
2100 /*
2101 **  SHOULDQUEUE -- should this message be queued or sent?
2102 **
2103 **	Compares the message cost to the load average to decide.
2104 **
2105 **	Note: Do NOT change this API! It is documented in op.me
2106 **		and theoretically the user can change this function...
2107 **
2108 **	Parameters:
2109 **		pri -- the priority of the message in question.
2110 **		ct -- the message creation time (unused, but see above).
2111 **
2112 **	Returns:
2113 **		true -- if this message should be queued up for the
2114 **			time being.
2115 **		false -- if the load is low enough to send this message.
2116 **
2117 **	Side Effects:
2118 **		none.
2119 */
2120 
2121 /* ARGSUSED1 */
2122 bool
2123 shouldqueue(pri, ct)
2124 	long pri;
2125 	time_t ct;
2126 {
2127 	bool rval;
2128 
2129 	if (tTd(3, 30))
2130 		sm_dprintf("shouldqueue: CurrentLA=%d, pri=%ld: ",
2131 			CurrentLA, pri);
2132 	if (CurrentLA < QueueLA)
2133 	{
2134 		if (tTd(3, 30))
2135 			sm_dprintf("false (CurrentLA < QueueLA)\n");
2136 		return false;
2137 	}
2138 # if 0	/* this code is reported to cause oscillation around RefuseLA */
2139 	if (CurrentLA >= RefuseLA && QueueLA < RefuseLA)
2140 	{
2141 		if (tTd(3, 30))
2142 			sm_dprintf("TRUE (CurrentLA >= RefuseLA)\n");
2143 		return true;
2144 	}
2145 # endif /* 0 */
2146 	rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
2147 	if (tTd(3, 30))
2148 		sm_dprintf("%s (by calculation)\n", rval ? "true" : "false");
2149 	return rval;
2150 }
2151 /*
2152 **  REFUSECONNECTIONS -- decide if connections should be refused
2153 **
2154 **	Parameters:
2155 **		name -- daemon name (for error messages only)
2156 **		e -- the current envelope.
2157 **		d -- number of daemon
2158 **		active -- was this daemon actually active?
2159 **
2160 **	Returns:
2161 **		true if incoming SMTP connections should be refused
2162 **			(for now).
2163 **		false if we should accept new work.
2164 **
2165 **	Side Effects:
2166 **		Sets process title when it is rejecting connections.
2167 */
2168 
2169 bool
2170 refuseconnections(name, e, d, active)
2171 	char *name;
2172 	ENVELOPE *e;
2173 	int d;
2174 	bool active;
2175 {
2176 	static time_t lastconn[MAXDAEMONS];
2177 	static int conncnt[MAXDAEMONS];
2178 #if _FFR_REJECT_LOG
2179 	static time_t firstrejtime[MAXDAEMONS];
2180 	static time_t nextlogtime[MAXDAEMONS];
2181 #endif /* _FFR_REJECT_LOG */
2182 
2183 #if XLA
2184 	if (!xla_smtp_ok())
2185 		return true;
2186 #endif /* XLA */
2187 
2188 	if (ConnRateThrottle > 0)
2189 	{
2190 		time_t now;
2191 
2192 		now = curtime();
2193 		if (active)
2194 		{
2195 			if (now != lastconn[d])
2196 			{
2197 				lastconn[d] = now;
2198 				conncnt[d] = 1;
2199 			}
2200 			else if (conncnt[d]++ > ConnRateThrottle)
2201 			{
2202 #define D_MSG_CRT "deferring connections on daemon %s: %d per second"
2203 				/* sleep to flatten out connection load */
2204 				sm_setproctitle(true, e, D_MSG_CRT,
2205 						name, ConnRateThrottle);
2206 				if (LogLevel > 8)
2207 					sm_syslog(LOG_INFO, NOQID, D_MSG_CRT,
2208 						  name, ConnRateThrottle);
2209 				(void) sleep(1);
2210 			}
2211 		}
2212 		else if (now != lastconn[d])
2213 			conncnt[d] = 0;
2214 	}
2215 
2216 	sm_getla();
2217 	if (RefuseLA > 0 && CurrentLA >= RefuseLA)
2218 	{
2219 # if _FFR_REJECT_LOG
2220 		time_t now;
2221 
2222 #  define R2_MSG_LA "have been rejecting connections on daemon %s for %s"
2223 # endif /* _FFR_REJECT_LOG */
2224 # define R_MSG_LA "rejecting connections on daemon %s: load average: %d"
2225 		sm_setproctitle(true, e, R_MSG_LA, name, CurrentLA);
2226 		if (LogLevel > 8)
2227 			sm_syslog(LOG_NOTICE, NOQID, R_MSG_LA, name, CurrentLA);
2228 #if _FFR_REJECT_LOG
2229 		now = curtime();
2230 		if (firstrejtime[d] == 0)
2231 		{
2232 			firstrejtime[d] = now;
2233 			nextlogtime[d] = now + RejectLogInterval;
2234 		}
2235 		else if (nextlogtime[d] < now)
2236 		{
2237 			sm_syslog(LOG_ERR, NOQID, R2_MSG_LA, name,
2238 				  pintvl(now - firstrejtime[d], true));
2239 			nextlogtime[d] = now + RejectLogInterval;
2240 		}
2241 #endif /* _FFR_REJECT_LOG */
2242 		return true;
2243 	}
2244 #if _FFR_REJECT_LOG
2245 	else
2246 		firstrejtime[d] = 0;
2247 #endif /* _FFR_REJECT_LOG */
2248 
2249 	if (DelayLA > 0 && CurrentLA >= DelayLA)
2250 	{
2251 		time_t now;
2252 		static time_t log_delay = (time_t) 0;
2253 
2254 # define MIN_DELAY_LOG	90	/* wait before logging this again */
2255 # define D_MSG_LA "delaying connections on daemon %s: load average=%d >= %d"
2256 		/* sleep to flatten out connection load */
2257 		sm_setproctitle(true, e, D_MSG_LA, name, DelayLA);
2258 		if (LogLevel > 8 && (now = curtime()) > log_delay)
2259 		{
2260 			sm_syslog(LOG_INFO, NOQID, D_MSG_LA,
2261 				  name, CurrentLA, DelayLA);
2262 			log_delay = now + MIN_DELAY_LOG;
2263 		}
2264 		(void) sleep(1);
2265 	}
2266 
2267 	if (MaxChildren > 0 && CurChildren >= MaxChildren)
2268 	{
2269 		proc_list_probe();
2270 		if (CurChildren >= MaxChildren)
2271 		{
2272 #define R_MSG_CHILD "rejecting connections on daemon %s: %d children, max %d"
2273 			sm_setproctitle(true, e, R_MSG_CHILD,
2274 					name, CurChildren, MaxChildren);
2275 			if (LogLevel > 8)
2276 				sm_syslog(LOG_INFO, NOQID, R_MSG_CHILD,
2277 					name, CurChildren, MaxChildren);
2278 			return true;
2279 		}
2280 	}
2281 	return false;
2282 }
2283 /*
2284 **  SETPROCTITLE -- set process title for ps
2285 **
2286 **	Parameters:
2287 **		fmt -- a printf style format string.
2288 **		a, b, c -- possible parameters to fmt.
2289 **
2290 **	Returns:
2291 **		none.
2292 **
2293 **	Side Effects:
2294 **		Clobbers argv of our main procedure so ps(1) will
2295 **		display the title.
2296 */
2297 
2298 #define SPT_NONE	0	/* don't use it at all */
2299 #define SPT_REUSEARGV	1	/* cover argv with title information */
2300 #define SPT_BUILTIN	2	/* use libc builtin */
2301 #define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */
2302 #define SPT_PSSTRINGS	4	/* use PS_STRINGS->... */
2303 #define SPT_SYSMIPS	5	/* use sysmips() supported by NEWS-OS 6 */
2304 #define SPT_SCO		6	/* write kernel u. area */
2305 #define SPT_CHANGEARGV	7	/* write our own strings into argv[] */
2306 
2307 #ifndef SPT_TYPE
2308 # define SPT_TYPE	SPT_REUSEARGV
2309 #endif /* ! SPT_TYPE */
2310 
2311 
2312 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
2313 
2314 # if SPT_TYPE == SPT_PSTAT
2315 #  include <sys/pstat.h>
2316 # endif /* SPT_TYPE == SPT_PSTAT */
2317 # if SPT_TYPE == SPT_PSSTRINGS
2318 #  include <machine/vmparam.h>
2319 #  include <sys/exec.h>
2320 #  ifndef PS_STRINGS	/* hmmmm....  apparently not available after all */
2321 #   undef SPT_TYPE
2322 #   define SPT_TYPE	SPT_REUSEARGV
2323 #  else /* ! PS_STRINGS */
2324 #   ifndef NKPDE			/* FreeBSD 2.0 */
2325 #    define NKPDE 63
2326 typedef unsigned int	*pt_entry_t;
2327 #   endif /* ! NKPDE */
2328 #  endif /* ! PS_STRINGS */
2329 # endif /* SPT_TYPE == SPT_PSSTRINGS */
2330 
2331 # if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
2332 #  define SETPROC_STATIC	static
2333 # else /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2334 #  define SETPROC_STATIC
2335 # endif /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2336 
2337 # if SPT_TYPE == SPT_SYSMIPS
2338 #  include <sys/sysmips.h>
2339 #  include <sys/sysnews.h>
2340 # endif /* SPT_TYPE == SPT_SYSMIPS */
2341 
2342 # if SPT_TYPE == SPT_SCO
2343 #  include <sys/immu.h>
2344 #  include <sys/dir.h>
2345 #  include <sys/user.h>
2346 #  include <sys/fs/s5param.h>
2347 #  if PSARGSZ > MAXLINE
2348 #   define SPT_BUFSIZE	PSARGSZ
2349 #  endif /* PSARGSZ > MAXLINE */
2350 # endif /* SPT_TYPE == SPT_SCO */
2351 
2352 # ifndef SPT_PADCHAR
2353 #  define SPT_PADCHAR	' '
2354 # endif /* ! SPT_PADCHAR */
2355 
2356 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
2357 
2358 #ifndef SPT_BUFSIZE
2359 # define SPT_BUFSIZE	MAXLINE
2360 #endif /* ! SPT_BUFSIZE */
2361 
2362 #if _FFR_SPT_ALIGN
2363 
2364 /*
2365 **  It looks like the Compaq Tru64 5.1A now aligns argv and envp to
2366 **  64 bit alignment, so unless each piece of argv and envp is a multiple
2367 **  of 8 bytes (including terminating NULL), initsetproctitle() won't use
2368 **  any of the space beyond argv[0].  Be sure to set SPT_ALIGN_SIZE if
2369 **  you use this FFR.
2370 */
2371 
2372 # ifdef SPT_ALIGN_SIZE
2373 #  define SPT_ALIGN(x, align)	(((((x) + SPT_ALIGN_SIZE) >> (align)) << (align)) - 1)
2374 # else /* SPT_ALIGN_SIZE */
2375 #  define SPT_ALIGN(x, align)	(x)
2376 # endif /* SPT_ALIGN_SIZE */
2377 #else /* _FFR_SPT_ALIGN */
2378 # define SPT_ALIGN(x, align)	(x)
2379 #endif /* _FFR_SPT_ALIGN */
2380 
2381 /*
2382 **  Pointers for setproctitle.
2383 **	This allows "ps" listings to give more useful information.
2384 */
2385 
2386 static char	**Argv = NULL;		/* pointer to argument vector */
2387 static char	*LastArgv = NULL;	/* end of argv */
2388 #if SPT_TYPE != SPT_BUILTIN
2389 static void	setproctitle __P((const char *, ...));
2390 #endif /* SPT_TYPE != SPT_BUILTIN */
2391 
2392 void
2393 initsetproctitle(argc, argv, envp)
2394 	int argc;
2395 	char **argv;
2396 	char **envp;
2397 {
2398 	register int i;
2399 	int align;
2400 	extern char **environ;
2401 
2402 	/*
2403 	**  Move the environment so setproctitle can use the space at
2404 	**  the top of memory.
2405 	*/
2406 
2407 	if (envp != NULL)
2408 	{
2409 		for (i = 0; envp[i] != NULL; i++)
2410 			continue;
2411 		environ = (char **) xalloc(sizeof (char *) * (i + 1));
2412 		for (i = 0; envp[i] != NULL; i++)
2413 			environ[i] = newstr(envp[i]);
2414 		environ[i] = NULL;
2415 	}
2416 
2417 	/*
2418 	**  Save start and extent of argv for setproctitle.
2419 	*/
2420 
2421 	Argv = argv;
2422 
2423 	/*
2424 	**  Determine how much space we can use for setproctitle.
2425 	**  Use all contiguous argv and envp pointers starting at argv[0]
2426 	*/
2427 
2428 	align = -1;
2429 #if _FFR_SPT_ALIGN
2430 # ifdef SPT_ALIGN_SIZE
2431 	for (i = SPT_ALIGN_SIZE; i > 0; i >>= 1)
2432 		align++;
2433 # endif /* SPT_ALIGN_SIZE */
2434 #endif /* _FFR_SPT_ALIGN */
2435 
2436 	for (i = 0; i < argc; i++)
2437 	{
2438 		if (i == 0 || LastArgv + 1 == argv[i])
2439 			LastArgv = argv[i] + SPT_ALIGN(strlen(argv[i]), align);
2440 	}
2441 	for (i = 0; LastArgv != NULL && envp != NULL && envp[i] != NULL; i++)
2442 	{
2443 		if (LastArgv + 1 == envp[i])
2444 			LastArgv = envp[i] + SPT_ALIGN(strlen(envp[i]), align);
2445 	}
2446 }
2447 
2448 #if SPT_TYPE != SPT_BUILTIN
2449 
2450 /*VARARGS1*/
2451 static void
2452 # ifdef __STDC__
2453 setproctitle(const char *fmt, ...)
2454 # else /* __STDC__ */
2455 setproctitle(fmt, va_alist)
2456 	const char *fmt;
2457 	va_dcl
2458 # endif /* __STDC__ */
2459 {
2460 # if SPT_TYPE != SPT_NONE
2461 	register int i;
2462 	register char *p;
2463 	SETPROC_STATIC char buf[SPT_BUFSIZE];
2464 	SM_VA_LOCAL_DECL
2465 #  if SPT_TYPE == SPT_PSTAT
2466 	union pstun pst;
2467 #  endif /* SPT_TYPE == SPT_PSTAT */
2468 #  if SPT_TYPE == SPT_SCO
2469 	int j;
2470 	off_t seek_off;
2471 	static int kmem = -1;
2472 	static pid_t kmempid = -1;
2473 	struct user u;
2474 #  endif /* SPT_TYPE == SPT_SCO */
2475 
2476 	p = buf;
2477 
2478 	/* print sendmail: heading for grep */
2479 	(void) sm_strlcpy(p, "sendmail: ", SPACELEFT(buf, p));
2480 	p += strlen(p);
2481 
2482 	/* print the argument string */
2483 	SM_VA_START(ap, fmt);
2484 	(void) sm_vsnprintf(p, SPACELEFT(buf, p), fmt, ap);
2485 	SM_VA_END(ap);
2486 
2487 	i = (int) strlen(buf);
2488 	if (i < 0)
2489 		return;
2490 
2491 #  if SPT_TYPE == SPT_PSTAT
2492 	pst.pst_command = buf;
2493 	pstat(PSTAT_SETCMD, pst, i, 0, 0);
2494 #  endif /* SPT_TYPE == SPT_PSTAT */
2495 #  if SPT_TYPE == SPT_PSSTRINGS
2496 	PS_STRINGS->ps_nargvstr = 1;
2497 	PS_STRINGS->ps_argvstr = buf;
2498 #  endif /* SPT_TYPE == SPT_PSSTRINGS */
2499 #  if SPT_TYPE == SPT_SYSMIPS
2500 	sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
2501 #  endif /* SPT_TYPE == SPT_SYSMIPS */
2502 #  if SPT_TYPE == SPT_SCO
2503 	if (kmem < 0 || kmempid != CurrentPid)
2504 	{
2505 		if (kmem >= 0)
2506 			(void) close(kmem);
2507 		kmem = open(_PATH_KMEM, O_RDWR, 0);
2508 		if (kmem < 0)
2509 			return;
2510 		if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
2511 		    fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
2512 		{
2513 			(void) close(kmem);
2514 			kmem = -1;
2515 			return;
2516 		}
2517 		kmempid = CurrentPid;
2518 	}
2519 	buf[PSARGSZ - 1] = '\0';
2520 	seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
2521 	if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
2522 		(void) write(kmem, buf, PSARGSZ);
2523 #  endif /* SPT_TYPE == SPT_SCO */
2524 #  if SPT_TYPE == SPT_REUSEARGV
2525 	if (LastArgv == NULL)
2526 		return;
2527 
2528 	if (i > LastArgv - Argv[0] - 2)
2529 	{
2530 		i = LastArgv - Argv[0] - 2;
2531 		buf[i] = '\0';
2532 	}
2533 	(void) sm_strlcpy(Argv[0], buf, i + 1);
2534 	p = &Argv[0][i];
2535 	while (p < LastArgv)
2536 		*p++ = SPT_PADCHAR;
2537 	Argv[1] = NULL;
2538 #  endif /* SPT_TYPE == SPT_REUSEARGV */
2539 #  if SPT_TYPE == SPT_CHANGEARGV
2540 	Argv[0] = buf;
2541 	Argv[1] = 0;
2542 #  endif /* SPT_TYPE == SPT_CHANGEARGV */
2543 # endif /* SPT_TYPE != SPT_NONE */
2544 }
2545 
2546 #endif /* SPT_TYPE != SPT_BUILTIN */
2547 /*
2548 **  SM_SETPROCTITLE -- set process task and set process title for ps
2549 **
2550 **	Possibly set process status and call setproctitle() to
2551 **	change the ps display.
2552 **
2553 **	Parameters:
2554 **		status -- whether or not to store as process status
2555 **		e -- the current envelope.
2556 **		fmt -- a printf style format string.
2557 **		a, b, c -- possible parameters to fmt.
2558 **
2559 **	Returns:
2560 **		none.
2561 */
2562 
2563 /*VARARGS2*/
2564 void
2565 #ifdef __STDC__
2566 sm_setproctitle(bool status, ENVELOPE *e, const char *fmt, ...)
2567 #else /* __STDC__ */
2568 sm_setproctitle(status, e, fmt, va_alist)
2569 	bool status;
2570 	ENVELOPE *e;
2571 	const char *fmt;
2572 	va_dcl
2573 #endif /* __STDC__ */
2574 {
2575 	char buf[SPT_BUFSIZE];
2576 	SM_VA_LOCAL_DECL
2577 
2578 	/* print the argument string */
2579 	SM_VA_START(ap, fmt);
2580 	(void) sm_vsnprintf(buf, sizeof buf, fmt, ap);
2581 	SM_VA_END(ap);
2582 
2583 	if (status)
2584 		proc_list_set(CurrentPid, buf);
2585 
2586 	if (ProcTitlePrefix != NULL)
2587 	{
2588 		char prefix[SPT_BUFSIZE];
2589 
2590 		expand(ProcTitlePrefix, prefix, sizeof prefix, e);
2591 		setproctitle("%s: %s", prefix, buf);
2592 	}
2593 	else
2594 		setproctitle("%s", buf);
2595 }
2596 /*
2597 **  WAITFOR -- wait for a particular process id.
2598 **
2599 **	Parameters:
2600 **		pid -- process id to wait for.
2601 **
2602 **	Returns:
2603 **		status of pid.
2604 **		-1 if pid never shows up.
2605 **
2606 **	Side Effects:
2607 **		none.
2608 */
2609 
2610 int
2611 waitfor(pid)
2612 	pid_t pid;
2613 {
2614 	int st;
2615 	pid_t i;
2616 
2617 	do
2618 	{
2619 		errno = 0;
2620 		i = sm_wait(&st);
2621 		if (i > 0)
2622 			proc_list_drop(i, st, NULL);
2623 	} while ((i >= 0 || errno == EINTR) && i != pid);
2624 	if (i < 0)
2625 		return -1;
2626 	return st;
2627 }
2628 /*
2629 **  SM_WAIT -- wait
2630 **
2631 **	Parameters:
2632 **		status -- pointer to status (return value)
2633 **
2634 **	Returns:
2635 **		pid
2636 */
2637 
2638 pid_t
2639 sm_wait(status)
2640 	int *status;
2641 {
2642 # ifdef WAITUNION
2643 	union wait st;
2644 # else /* WAITUNION */
2645 	auto int st;
2646 # endif /* WAITUNION */
2647 	pid_t i;
2648 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2649 	int savesig;
2650 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2651 
2652 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2653 	savesig = sm_releasesignal(SIGCHLD);
2654 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2655 	i = wait(&st);
2656 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2657 	if (savesig > 0)
2658 		sm_blocksignal(SIGCHLD);
2659 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2660 # ifdef WAITUNION
2661 	*status = st.w_status;
2662 # else /* WAITUNION */
2663 	*status = st;
2664 # endif /* WAITUNION */
2665 	return i;
2666 }
2667 /*
2668 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
2669 **
2670 **	Parameters:
2671 **		sig -- the signal that got us here (unused).
2672 **
2673 **	Returns:
2674 **		none.
2675 **
2676 **	Side Effects:
2677 **		Picks up extant zombies.
2678 **		Control socket exits may restart/shutdown daemon.
2679 **
2680 **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
2681 **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2682 **		DOING.
2683 */
2684 
2685 /* ARGSUSED0 */
2686 SIGFUNC_DECL
2687 reapchild(sig)
2688 	int sig;
2689 {
2690 	int save_errno = errno;
2691 	int st;
2692 	pid_t pid;
2693 # if HASWAITPID
2694 	auto int status;
2695 	int count;
2696 
2697 	count = 0;
2698 	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
2699 	{
2700 		st = status;
2701 		if (count++ > 1000)
2702 			break;
2703 # else /* HASWAITPID */
2704 #  ifdef WNOHANG
2705 	union wait status;
2706 
2707 	while ((pid = wait3(&status, WNOHANG, (struct rusage *) NULL)) > 0)
2708 	{
2709 		st = status.w_status;
2710 #  else /* WNOHANG */
2711 	auto int status;
2712 
2713 	/*
2714 	**  Catch one zombie -- we will be re-invoked (we hope) if there
2715 	**  are more.  Unreliable signals probably break this, but this
2716 	**  is the "old system" situation -- waitpid or wait3 are to be
2717 	**  strongly preferred.
2718 	*/
2719 
2720 	if ((pid = wait(&status)) > 0)
2721 	{
2722 		st = status;
2723 #  endif /* WNOHANG */
2724 # endif /* HASWAITPID */
2725 		/* Drop PID and check if it was a control socket child */
2726 		proc_list_drop(pid, st, NULL);
2727 	}
2728 	FIX_SYSV_SIGNAL(sig, reapchild);
2729 	errno = save_errno;
2730 	return SIGFUNC_RETURN;
2731 }
2732 /*
2733 **  GETDTABLESIZE -- return number of file descriptors
2734 **
2735 **	Only on non-BSD systems
2736 **
2737 **	Parameters:
2738 **		none
2739 **
2740 **	Returns:
2741 **		size of file descriptor table
2742 **
2743 **	Side Effects:
2744 **		none
2745 */
2746 
2747 #ifdef SOLARIS
2748 # include <sys/resource.h>
2749 #endif /* SOLARIS */
2750 
2751 int
2752 getdtsize()
2753 {
2754 # ifdef RLIMIT_NOFILE
2755 	struct rlimit rl;
2756 
2757 	if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
2758 		return rl.rlim_cur;
2759 # endif /* RLIMIT_NOFILE */
2760 
2761 # if HASGETDTABLESIZE
2762 	return getdtablesize();
2763 # else /* HASGETDTABLESIZE */
2764 #  ifdef _SC_OPEN_MAX
2765 	return sysconf(_SC_OPEN_MAX);
2766 #  else /* _SC_OPEN_MAX */
2767 	return NOFILE;
2768 #  endif /* _SC_OPEN_MAX */
2769 # endif /* HASGETDTABLESIZE */
2770 }
2771 /*
2772 **  UNAME -- get the UUCP name of this system.
2773 */
2774 
2775 #if !HASUNAME
2776 
2777 int
2778 uname(name)
2779 	struct utsname *name;
2780 {
2781 	SM_FILE_T *file;
2782 	char *n;
2783 
2784 	name->nodename[0] = '\0';
2785 
2786 	/* try /etc/whoami -- one line with the node name */
2787 	if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, "/etc/whoami",
2788 			       SM_IO_RDONLY, NULL)) != NULL)
2789 	{
2790 		(void) sm_io_fgets(file, SM_TIME_DEFAULT, name->nodename,
2791 				   NODE_LENGTH + 1);
2792 		(void) sm_io_close(file, SM_TIME_DEFAULT);
2793 		n = strchr(name->nodename, '\n');
2794 		if (n != NULL)
2795 			*n = '\0';
2796 		if (name->nodename[0] != '\0')
2797 			return 0;
2798 	}
2799 
2800 	/* try /usr/include/whoami.h -- has a #define somewhere */
2801 	if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2802 			       "/usr/include/whoami.h", SM_IO_RDONLY, NULL))
2803 	    != NULL)
2804 	{
2805 		char buf[MAXLINE];
2806 
2807 		while (sm_io_fgets(file, SM_TIME_DEFAULT,
2808 				   buf, sizeof buf) != NULL)
2809 		{
2810 			if (sm_io_sscanf(buf, "#define sysname \"%*[^\"]\"",
2811 					NODE_LENGTH, name->nodename) > 0)
2812 				break;
2813 		}
2814 		(void) sm_io_close(file, SM_TIME_DEFAULT);
2815 		if (name->nodename[0] != '\0')
2816 			return 0;
2817 	}
2818 
2819 #  if 0
2820 	/*
2821 	**  Popen is known to have security holes.
2822 	*/
2823 
2824 	/* try uuname -l to return local name */
2825 	if ((file = popen("uuname -l", "r")) != NULL)
2826 	{
2827 		(void) sm_io_fgets(file, SM_TIME_DEFAULT, name,
2828 				   NODE_LENGTH + 1);
2829 		(void) pclose(file);
2830 		n = strchr(name, '\n');
2831 		if (n != NULL)
2832 			*n = '\0';
2833 		if (name->nodename[0] != '\0')
2834 			return 0;
2835 	}
2836 #  endif /* 0 */
2837 
2838 	return -1;
2839 }
2840 #endif /* !HASUNAME */
2841 /*
2842 **  INITGROUPS -- initialize groups
2843 **
2844 **	Stub implementation for System V style systems
2845 */
2846 
2847 #if !HASINITGROUPS
2848 
2849 initgroups(name, basegid)
2850 	char *name;
2851 	int basegid;
2852 {
2853 	return 0;
2854 }
2855 
2856 #endif /* !HASINITGROUPS */
2857 /*
2858 **  SETGROUPS -- set group list
2859 **
2860 **	Stub implementation for systems that don't have group lists
2861 */
2862 
2863 #ifndef NGROUPS_MAX
2864 
2865 int
2866 setgroups(ngroups, grouplist)
2867 	int ngroups;
2868 	GIDSET_T grouplist[];
2869 {
2870 	return 0;
2871 }
2872 
2873 #endif /* ! NGROUPS_MAX */
2874 /*
2875 **  SETSID -- set session id (for non-POSIX systems)
2876 */
2877 
2878 #if !HASSETSID
2879 
2880 pid_t
2881 setsid __P ((void))
2882 {
2883 #  ifdef TIOCNOTTY
2884 	int fd;
2885 
2886 	fd = open("/dev/tty", O_RDWR, 0);
2887 	if (fd >= 0)
2888 	{
2889 		(void) ioctl(fd, TIOCNOTTY, (char *) 0);
2890 		(void) close(fd);
2891 	}
2892 #  endif /* TIOCNOTTY */
2893 #  ifdef SYS5SETPGRP
2894 	return setpgrp();
2895 #  else /* SYS5SETPGRP */
2896 	return setpgid(0, CurrentPid);
2897 #  endif /* SYS5SETPGRP */
2898 }
2899 
2900 #endif /* !HASSETSID */
2901 /*
2902 **  FSYNC -- dummy fsync
2903 */
2904 
2905 #if NEEDFSYNC
2906 
2907 fsync(fd)
2908 	int fd;
2909 {
2910 # ifdef O_SYNC
2911 	return fcntl(fd, F_SETFL, O_SYNC);
2912 # else /* O_SYNC */
2913 	/* nothing we can do */
2914 	return 0;
2915 # endif /* O_SYNC */
2916 }
2917 
2918 #endif /* NEEDFSYNC */
2919 /*
2920 **  DGUX_INET_ADDR -- inet_addr for DG/UX
2921 **
2922 **	Data General DG/UX version of inet_addr returns a struct in_addr
2923 **	instead of a long.  This patches things.  Only needed on versions
2924 **	prior to 5.4.3.
2925 */
2926 
2927 #ifdef DGUX_5_4_2
2928 
2929 # undef inet_addr
2930 
2931 long
2932 dgux_inet_addr(host)
2933 	char *host;
2934 {
2935 	struct in_addr haddr;
2936 
2937 	haddr = inet_addr(host);
2938 	return haddr.s_addr;
2939 }
2940 
2941 #endif /* DGUX_5_4_2 */
2942 /*
2943 **  GETOPT -- for old systems or systems with bogus implementations
2944 */
2945 
2946 #if !SM_CONF_GETOPT
2947 
2948 /*
2949  * Copyright (c) 1985 Regents of the University of California.
2950  * All rights reserved.  The Berkeley software License Agreement
2951  * specifies the terms and conditions for redistribution.
2952  */
2953 
2954 
2955 /*
2956 **  this version hacked to add `atend' flag to allow state machine
2957 **  to reset if invoked by the program to scan args for a 2nd time
2958 */
2959 
2960 # if defined(LIBC_SCCS) && !defined(lint)
2961 static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
2962 # endif /* defined(LIBC_SCCS) && !defined(lint) */
2963 
2964 /*
2965 **  get option letter from argument vector
2966 */
2967 # ifdef _CONVEX_SOURCE
2968 extern int	optind, opterr, optopt;
2969 extern char	*optarg;
2970 # else /* _CONVEX_SOURCE */
2971 int	opterr = 1;		/* if error message should be printed */
2972 int	optind = 1;		/* index into parent argv vector */
2973 int	optopt = 0;		/* character checked for validity */
2974 char	*optarg = NULL;		/* argument associated with option */
2975 # endif /* _CONVEX_SOURCE */
2976 
2977 # define BADCH	(int)'?'
2978 # define EMSG	""
2979 # define tell(s)	if (opterr) \
2980 			{sm_io_fputs(smioerr, SM_TIME_DEFAULT, *nargv); \
2981 			(void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, s); \
2982 			(void) sm_io_putc(smioerr, SM_TIME_DEFAULT, optopt); \
2983 			(void) sm_io_putc(smioerr, SM_TIME_DEFAULT, '\n'); \
2984 			return BADCH;}
2985 
2986 int
2987 getopt(nargc,nargv,ostr)
2988 	int		nargc;
2989 	char *const	*nargv;
2990 	const char	*ostr;
2991 {
2992 	static char	*place = EMSG;	/* option letter processing */
2993 	static char	atend = 0;
2994 	register char	*oli = NULL;	/* option letter list index */
2995 
2996 	if (atend) {
2997 		atend = 0;
2998 		place = EMSG;
2999 	}
3000 	if(!*place) {			/* update scanning pointer */
3001 		if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
3002 			atend++;
3003 			return -1;
3004 		}
3005 		if (*place == '-') {	/* found "--" */
3006 			++optind;
3007 			atend++;
3008 			return -1;
3009 		}
3010 	}				/* option letter okay? */
3011 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
3012 		if (!*place) ++optind;
3013 		tell(": illegal option -- ");
3014 	}
3015 	if (oli && *++oli != ':') {		/* don't need argument */
3016 		optarg = NULL;
3017 		if (!*place) ++optind;
3018 	}
3019 	else {				/* need an argument */
3020 		if (*place) optarg = place;	/* no white space */
3021 		else if (nargc <= ++optind) {	/* no arg */
3022 			place = EMSG;
3023 			tell(": option requires an argument -- ");
3024 		}
3025 		else optarg = nargv[optind];	/* white space */
3026 		place = EMSG;
3027 		++optind;
3028 	}
3029 	return optopt;			/* dump back option letter */
3030 }
3031 
3032 #endif /* !SM_CONF_GETOPT */
3033 /*
3034 **  USERSHELLOK -- tell if a user's shell is ok for unrestricted use
3035 **
3036 **	Parameters:
3037 **		user -- the name of the user we are checking.
3038 **		shell -- the user's shell from /etc/passwd
3039 **
3040 **	Returns:
3041 **		true -- if it is ok to use this for unrestricted access.
3042 **		false -- if the shell is restricted.
3043 */
3044 
3045 #if !HASGETUSERSHELL
3046 
3047 # ifndef _PATH_SHELLS
3048 #  define _PATH_SHELLS	"/etc/shells"
3049 # endif /* ! _PATH_SHELLS */
3050 
3051 # if defined(_AIX3) || defined(_AIX4)
3052 #  include <userconf.h>
3053 #  if _AIX4 >= 40200
3054 #   include <userpw.h>
3055 #  endif /* _AIX4 >= 40200 */
3056 #  include <usersec.h>
3057 # endif /* defined(_AIX3) || defined(_AIX4) */
3058 
3059 static char	*DefaultUserShells[] =
3060 {
3061 	"/bin/sh",		/* standard shell */
3062 # ifdef MPE
3063 	"/SYS/PUB/CI",
3064 # else /* MPE */
3065 	"/usr/bin/sh",
3066 	"/bin/csh",		/* C shell */
3067 	"/usr/bin/csh",
3068 # endif /* MPE */
3069 # ifdef __hpux
3070 #  ifdef V4FS
3071 	"/usr/bin/rsh",		/* restricted Bourne shell */
3072 	"/usr/bin/ksh",		/* Korn shell */
3073 	"/usr/bin/rksh",	/* restricted Korn shell */
3074 	"/usr/bin/pam",
3075 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
3076 	"/usr/bin/posix/sh",
3077 #  else /* V4FS */
3078 	"/bin/rsh",		/* restricted Bourne shell */
3079 	"/bin/ksh",		/* Korn shell */
3080 	"/bin/rksh",		/* restricted Korn shell */
3081 	"/bin/pam",
3082 	"/usr/bin/keysh",	/* key shell (extended Korn shell) */
3083 	"/bin/posix/sh",
3084 	"/sbin/sh"
3085 #  endif /* V4FS */
3086 # endif /* __hpux */
3087 # if defined(_AIX3) || defined(_AIX4)
3088 	"/bin/ksh",		/* Korn shell */
3089 	"/usr/bin/ksh",
3090 	"/bin/tsh",		/* trusted shell */
3091 	"/usr/bin/tsh",
3092 	"/bin/bsh",		/* Bourne shell */
3093 	"/usr/bin/bsh",
3094 # endif /* defined(_AIX3) || defined(_AIX4) */
3095 # if defined(__svr4__) || defined(__svr5__)
3096 	"/bin/ksh",		/* Korn shell */
3097 	"/usr/bin/ksh",
3098 # endif /* defined(__svr4__) || defined(__svr5__) */
3099 # ifdef sgi
3100 	"/sbin/sh",		/* SGI's shells really live in /sbin */
3101 	"/usr/bin/sh",
3102 	"/sbin/bsh",		/* classic Bourne shell */
3103 	"/bin/bsh",
3104 	"/usr/bin/bsh",
3105 	"/sbin/csh",		/* standard csh */
3106 	"/bin/csh",
3107 	"/usr/bin/csh",
3108 	"/sbin/jsh",		/* classic Bourne shell w/ job control*/
3109 	"/bin/jsh",
3110 	"/usr/bin/jsh",
3111 	"/bin/ksh",		/* Korn shell */
3112 	"/sbin/ksh",
3113 	"/usr/bin/ksh",
3114 	"/sbin/tcsh",		/* Extended csh */
3115 	"/bin/tcsh",
3116 	"/usr/bin/tcsh",
3117 # endif /* sgi */
3118 	NULL
3119 };
3120 
3121 #endif /* !HASGETUSERSHELL */
3122 
3123 #define WILDCARD_SHELL	"/SENDMAIL/ANY/SHELL/"
3124 
3125 bool
3126 usershellok(user, shell)
3127 	char *user;
3128 	char *shell;
3129 {
3130 # if HASGETUSERSHELL
3131 	register char *p;
3132 	extern char *getusershell();
3133 
3134 	if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3135 	    ConfigLevel <= 1)
3136 		return true;
3137 
3138 	setusershell();
3139 	while ((p = getusershell()) != NULL)
3140 		if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
3141 			break;
3142 	endusershell();
3143 	return p != NULL;
3144 # else /* HASGETUSERSHELL */
3145 #  if USEGETCONFATTR
3146 	auto char *v;
3147 #  endif /* USEGETCONFATTR */
3148 	register SM_FILE_T *shellf;
3149 	char buf[MAXLINE];
3150 
3151 	if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3152 	    ConfigLevel <= 1)
3153 		return true;
3154 
3155 #  if USEGETCONFATTR
3156 	/*
3157 	**  Naturally IBM has a "better" idea.....
3158 	**
3159 	**	What a crock.  This interface isn't documented, it is
3160 	**	considered part of the security library (-ls), and it
3161 	**	only works if you are running as root (since the list
3162 	**	of valid shells is obviously a source of great concern).
3163 	**	I recommend that you do NOT define USEGETCONFATTR,
3164 	**	especially since you are going to have to set up an
3165 	**	/etc/shells anyhow to handle the cases where getconfattr
3166 	**	fails.
3167 	*/
3168 
3169 	if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
3170 	{
3171 		while (*v != '\0')
3172 		{
3173 			if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
3174 				return true;
3175 			v += strlen(v) + 1;
3176 		}
3177 		return false;
3178 	}
3179 #  endif /* USEGETCONFATTR */
3180 
3181 	shellf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_SHELLS,
3182 			    SM_IO_RDONLY, NULL);
3183 	if (shellf == NULL)
3184 	{
3185 		/* no /etc/shells; see if it is one of the std shells */
3186 		char **d;
3187 
3188 		if (errno != ENOENT && LogLevel > 3)
3189 			sm_syslog(LOG_ERR, NOQID,
3190 				  "usershellok: cannot open %s: %s",
3191 				  _PATH_SHELLS, sm_errstring(errno));
3192 
3193 		for (d = DefaultUserShells; *d != NULL; d++)
3194 		{
3195 			if (strcmp(shell, *d) == 0)
3196 				return true;
3197 		}
3198 		return false;
3199 	}
3200 
3201 	while (sm_io_fgets(shellf, SM_TIME_DEFAULT, buf, sizeof buf) != NULL)
3202 	{
3203 		register char *p, *q;
3204 
3205 		p = buf;
3206 		while (*p != '\0' && *p != '#' && *p != '/')
3207 			p++;
3208 		if (*p == '#' || *p == '\0')
3209 			continue;
3210 		q = p;
3211 		while (*p != '\0' && *p != '#' && !(isascii(*p) && isspace(*p)))
3212 			p++;
3213 		*p = '\0';
3214 		if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
3215 		{
3216 			(void) sm_io_close(shellf, SM_TIME_DEFAULT);
3217 			return true;
3218 		}
3219 	}
3220 	(void) sm_io_close(shellf, SM_TIME_DEFAULT);
3221 	return false;
3222 # endif /* HASGETUSERSHELL */
3223 }
3224 /*
3225 **  FREEDISKSPACE -- see how much free space is on the queue filesystem
3226 **
3227 **	Only implemented if you have statfs.
3228 **
3229 **	Parameters:
3230 **		dir -- the directory in question.
3231 **		bsize -- a variable into which the filesystem
3232 **			block size is stored.
3233 **
3234 **	Returns:
3235 **		The number of blocks free on the queue filesystem.
3236 **		-1 if the statfs call fails.
3237 **
3238 **	Side effects:
3239 **		Puts the filesystem block size into bsize.
3240 */
3241 
3242 /* statfs types */
3243 # define SFS_NONE	0	/* no statfs implementation */
3244 # define SFS_USTAT	1	/* use ustat */
3245 # define SFS_4ARGS	2	/* use four-argument statfs call */
3246 # define SFS_VFS	3	/* use <sys/vfs.h> implementation */
3247 # define SFS_MOUNT	4	/* use <sys/mount.h> implementation */
3248 # define SFS_STATFS	5	/* use <sys/statfs.h> implementation */
3249 # define SFS_STATVFS	6	/* use <sys/statvfs.h> implementation */
3250 
3251 # ifndef SFS_TYPE
3252 #  define SFS_TYPE	SFS_NONE
3253 # endif /* ! SFS_TYPE */
3254 
3255 # if SFS_TYPE == SFS_USTAT
3256 #  include <ustat.h>
3257 # endif /* SFS_TYPE == SFS_USTAT */
3258 # if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
3259 #  include <sys/statfs.h>
3260 # endif /* SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS */
3261 # if SFS_TYPE == SFS_VFS
3262 #  include <sys/vfs.h>
3263 # endif /* SFS_TYPE == SFS_VFS */
3264 # if SFS_TYPE == SFS_MOUNT
3265 #  include <sys/mount.h>
3266 # endif /* SFS_TYPE == SFS_MOUNT */
3267 # if SFS_TYPE == SFS_STATVFS
3268 #  include <sys/statvfs.h>
3269 # endif /* SFS_TYPE == SFS_STATVFS */
3270 
3271 long
3272 freediskspace(dir, bsize)
3273 	char *dir;
3274 	long *bsize;
3275 {
3276 # if SFS_TYPE == SFS_NONE
3277 	if (bsize != NULL)
3278 		*bsize = 4096L;
3279 
3280 	/* assume free space is plentiful */
3281 	return (long) LONG_MAX;
3282 # else /* SFS_TYPE == SFS_NONE */
3283 #  if SFS_TYPE == SFS_USTAT
3284 	struct ustat fs;
3285 	struct stat statbuf;
3286 #   define FSBLOCKSIZE	DEV_BSIZE
3287 #   define SFS_BAVAIL	f_tfree
3288 #  else /* SFS_TYPE == SFS_USTAT */
3289 #   if defined(ultrix)
3290 	struct fs_data fs;
3291 #    define SFS_BAVAIL	fd_bfreen
3292 #    define FSBLOCKSIZE	1024L
3293 #   else /* defined(ultrix) */
3294 #    if SFS_TYPE == SFS_STATVFS
3295 	struct statvfs fs;
3296 #     define FSBLOCKSIZE	fs.f_frsize
3297 #    else /* SFS_TYPE == SFS_STATVFS */
3298 	struct statfs fs;
3299 #     define FSBLOCKSIZE	fs.f_bsize
3300 #    endif /* SFS_TYPE == SFS_STATVFS */
3301 #   endif /* defined(ultrix) */
3302 #  endif /* SFS_TYPE == SFS_USTAT */
3303 #  ifndef SFS_BAVAIL
3304 #   define SFS_BAVAIL f_bavail
3305 #  endif /* ! SFS_BAVAIL */
3306 
3307 #  if SFS_TYPE == SFS_USTAT
3308 	if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
3309 #  else /* SFS_TYPE == SFS_USTAT */
3310 #   if SFS_TYPE == SFS_4ARGS
3311 	if (statfs(dir, &fs, sizeof fs, 0) == 0)
3312 #   else /* SFS_TYPE == SFS_4ARGS */
3313 #    if SFS_TYPE == SFS_STATVFS
3314 	if (statvfs(dir, &fs) == 0)
3315 #    else /* SFS_TYPE == SFS_STATVFS */
3316 #     if defined(ultrix)
3317 	if (statfs(dir, &fs) > 0)
3318 #     else /* defined(ultrix) */
3319 	if (statfs(dir, &fs) == 0)
3320 #     endif /* defined(ultrix) */
3321 #    endif /* SFS_TYPE == SFS_STATVFS */
3322 #   endif /* SFS_TYPE == SFS_4ARGS */
3323 #  endif /* SFS_TYPE == SFS_USTAT */
3324 	{
3325 		if (bsize != NULL)
3326 			*bsize = FSBLOCKSIZE;
3327 		if (fs.SFS_BAVAIL <= 0)
3328 			return 0;
3329 		else if (fs.SFS_BAVAIL > LONG_MAX)
3330 			return (long) LONG_MAX;
3331 		else
3332 			return (long) fs.SFS_BAVAIL;
3333 	}
3334 	return -1;
3335 # endif /* SFS_TYPE == SFS_NONE */
3336 }
3337 /*
3338 **  ENOUGHDISKSPACE -- is there enough free space on the queue file systems?
3339 **
3340 **	Parameters:
3341 **		msize -- the size to check against.  If zero, we don't yet
3342 **		know how big the message will be, so just check for
3343 **		a "reasonable" amount.
3344 **		e -- envelope, or NULL -- controls logging
3345 **
3346 **	Returns:
3347 **		true if in every queue group there is at least one
3348 **		queue directory whose file system contains enough free space.
3349 **		false otherwise.
3350 **
3351 **	Side Effects:
3352 **		If there is not enough disk space and e != NULL
3353 **		then sm_syslog is called.
3354 */
3355 
3356 bool
3357 enoughdiskspace(msize, e)
3358 	long msize;
3359 	ENVELOPE *e;
3360 {
3361 	int i;
3362 
3363 	if (MinBlocksFree <= 0 && msize <= 0)
3364 	{
3365 		if (tTd(4, 80))
3366 			sm_dprintf("enoughdiskspace: no threshold\n");
3367 		return true;
3368 	}
3369 
3370 	filesys_update();
3371 	for (i = 0; i < NumQueue; ++i)
3372 	{
3373 		if (pickqdir(Queue[i], msize, e) < 0)
3374 			return false;
3375 	}
3376 	return true;
3377 }
3378 /*
3379 **  TRANSIENTERROR -- tell if an error code indicates a transient failure
3380 **
3381 **	This looks at an errno value and tells if this is likely to
3382 **	go away if retried later.
3383 **
3384 **	Parameters:
3385 **		err -- the errno code to classify.
3386 **
3387 **	Returns:
3388 **		true if this is probably transient.
3389 **		false otherwise.
3390 */
3391 
3392 bool
3393 transienterror(err)
3394 	int err;
3395 {
3396 	switch (err)
3397 	{
3398 	  case EIO:			/* I/O error */
3399 	  case ENXIO:			/* Device not configured */
3400 	  case EAGAIN:			/* Resource temporarily unavailable */
3401 	  case ENOMEM:			/* Cannot allocate memory */
3402 	  case ENODEV:			/* Operation not supported by device */
3403 	  case ENFILE:			/* Too many open files in system */
3404 	  case EMFILE:			/* Too many open files */
3405 	  case ENOSPC:			/* No space left on device */
3406 	  case ETIMEDOUT:		/* Connection timed out */
3407 #ifdef ESTALE
3408 	  case ESTALE:			/* Stale NFS file handle */
3409 #endif /* ESTALE */
3410 #ifdef ENETDOWN
3411 	  case ENETDOWN:		/* Network is down */
3412 #endif /* ENETDOWN */
3413 #ifdef ENETUNREACH
3414 	  case ENETUNREACH:		/* Network is unreachable */
3415 #endif /* ENETUNREACH */
3416 #ifdef ENETRESET
3417 	  case ENETRESET:		/* Network dropped connection on reset */
3418 #endif /* ENETRESET */
3419 #ifdef ECONNABORTED
3420 	  case ECONNABORTED:		/* Software caused connection abort */
3421 #endif /* ECONNABORTED */
3422 #ifdef ECONNRESET
3423 	  case ECONNRESET:		/* Connection reset by peer */
3424 #endif /* ECONNRESET */
3425 #ifdef ENOBUFS
3426 	  case ENOBUFS:			/* No buffer space available */
3427 #endif /* ENOBUFS */
3428 #ifdef ESHUTDOWN
3429 	  case ESHUTDOWN:		/* Can't send after socket shutdown */
3430 #endif /* ESHUTDOWN */
3431 #ifdef ECONNREFUSED
3432 	  case ECONNREFUSED:		/* Connection refused */
3433 #endif /* ECONNREFUSED */
3434 #ifdef EHOSTDOWN
3435 	  case EHOSTDOWN:		/* Host is down */
3436 #endif /* EHOSTDOWN */
3437 #ifdef EHOSTUNREACH
3438 	  case EHOSTUNREACH:		/* No route to host */
3439 #endif /* EHOSTUNREACH */
3440 #ifdef EDQUOT
3441 	  case EDQUOT:			/* Disc quota exceeded */
3442 #endif /* EDQUOT */
3443 #ifdef EPROCLIM
3444 	  case EPROCLIM:		/* Too many processes */
3445 #endif /* EPROCLIM */
3446 #ifdef EUSERS
3447 	  case EUSERS:			/* Too many users */
3448 #endif /* EUSERS */
3449 #ifdef EDEADLK
3450 	  case EDEADLK:			/* Resource deadlock avoided */
3451 #endif /* EDEADLK */
3452 #ifdef EISCONN
3453 	  case EISCONN:			/* Socket already connected */
3454 #endif /* EISCONN */
3455 #ifdef EINPROGRESS
3456 	  case EINPROGRESS:		/* Operation now in progress */
3457 #endif /* EINPROGRESS */
3458 #ifdef EALREADY
3459 	  case EALREADY:		/* Operation already in progress */
3460 #endif /* EALREADY */
3461 #ifdef EADDRINUSE
3462 	  case EADDRINUSE:		/* Address already in use */
3463 #endif /* EADDRINUSE */
3464 #ifdef EADDRNOTAVAIL
3465 	  case EADDRNOTAVAIL:		/* Can't assign requested address */
3466 #endif /* EADDRNOTAVAIL */
3467 #ifdef ETXTBSY
3468 	  case ETXTBSY:			/* (Apollo) file locked */
3469 #endif /* ETXTBSY */
3470 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
3471 	  case ENOSR:			/* Out of streams resources */
3472 #endif /* defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) */
3473 #ifdef ENOLCK
3474 	  case ENOLCK:			/* No locks available */
3475 #endif /* ENOLCK */
3476 	  case E_SM_OPENTIMEOUT:	/* PSEUDO: open timed out */
3477 		return true;
3478 	}
3479 
3480 	/* nope, must be permanent */
3481 	return false;
3482 }
3483 /*
3484 **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
3485 **
3486 **	Parameters:
3487 **		fd -- the file descriptor of the file.
3488 **		filename -- the file name (for error messages).
3489 **		ext -- the filename extension.
3490 **		type -- type of the lock.  Bits can be:
3491 **			LOCK_EX -- exclusive lock.
3492 **			LOCK_NB -- non-blocking.
3493 **			LOCK_UN -- unlock.
3494 **
3495 **	Returns:
3496 **		true if the lock was acquired.
3497 **		false otherwise.
3498 */
3499 
3500 bool
3501 lockfile(fd, filename, ext, type)
3502 	int fd;
3503 	char *filename;
3504 	char *ext;
3505 	int type;
3506 {
3507 	int i;
3508 	int save_errno;
3509 # if !HASFLOCK
3510 	int action;
3511 	struct flock lfd;
3512 
3513 	if (ext == NULL)
3514 		ext = "";
3515 
3516 	memset(&lfd, '\0', sizeof lfd);
3517 	if (bitset(LOCK_UN, type))
3518 		lfd.l_type = F_UNLCK;
3519 	else if (bitset(LOCK_EX, type))
3520 		lfd.l_type = F_WRLCK;
3521 	else
3522 		lfd.l_type = F_RDLCK;
3523 
3524 	if (bitset(LOCK_NB, type))
3525 		action = F_SETLK;
3526 	else
3527 		action = F_SETLKW;
3528 
3529 	if (tTd(55, 60))
3530 		sm_dprintf("lockfile(%s%s, action=%d, type=%d): ",
3531 			filename, ext, action, lfd.l_type);
3532 
3533 	while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
3534 		continue;
3535 	if (i >= 0)
3536 	{
3537 		if (tTd(55, 60))
3538 			sm_dprintf("SUCCESS\n");
3539 		return true;
3540 	}
3541 	save_errno = errno;
3542 
3543 	if (tTd(55, 60))
3544 		sm_dprintf("(%s) ", sm_errstring(save_errno));
3545 
3546 	/*
3547 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
3548 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
3549 	**  as type "tmp" (that is, served from swap space), the
3550 	**  previous fcntl will fail with "Invalid argument" errors.
3551 	**  Since this is fairly common during testing, we will assume
3552 	**  that this indicates that the lock is successfully grabbed.
3553 	*/
3554 
3555 	if (save_errno == EINVAL)
3556 	{
3557 		if (tTd(55, 60))
3558 			sm_dprintf("SUCCESS\n");
3559 		return true;
3560 	}
3561 
3562 	if (!bitset(LOCK_NB, type) ||
3563 	    (save_errno != EACCES && save_errno != EAGAIN))
3564 	{
3565 		int omode = fcntl(fd, F_GETFL, 0);
3566 		uid_t euid = geteuid();
3567 
3568 		errno = save_errno;
3569 		syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
3570 		       filename, ext, fd, type, omode, euid);
3571 		dumpfd(fd, true, true);
3572 	}
3573 # else /* !HASFLOCK */
3574 	if (ext == NULL)
3575 		ext = "";
3576 
3577 	if (tTd(55, 60))
3578 		sm_dprintf("lockfile(%s%s, type=%o): ", filename, ext, type);
3579 
3580 	while ((i = flock(fd, type)) < 0 && errno == EINTR)
3581 		continue;
3582 	if (i >= 0)
3583 	{
3584 		if (tTd(55, 60))
3585 			sm_dprintf("SUCCESS\n");
3586 		return true;
3587 	}
3588 	save_errno = errno;
3589 
3590 	if (tTd(55, 60))
3591 		sm_dprintf("(%s) ", sm_errstring(save_errno));
3592 
3593 	if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
3594 	{
3595 		int omode = fcntl(fd, F_GETFL, 0);
3596 		uid_t euid = geteuid();
3597 
3598 		errno = save_errno;
3599 		syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
3600 			filename, ext, fd, type, omode, euid);
3601 		dumpfd(fd, true, true);
3602 	}
3603 # endif /* !HASFLOCK */
3604 	if (tTd(55, 60))
3605 		sm_dprintf("FAIL\n");
3606 	errno = save_errno;
3607 	return false;
3608 }
3609 /*
3610 **  CHOWNSAFE -- tell if chown is "safe" (executable only by root)
3611 **
3612 **	Unfortunately, given that we can't predict other systems on which
3613 **	a remote mounted (NFS) filesystem will be mounted, the answer is
3614 **	almost always that this is unsafe.
3615 **
3616 **	Note also that many operating systems have non-compliant
3617 **	implementations of the _POSIX_CHOWN_RESTRICTED variable and the
3618 **	fpathconf() routine.  According to IEEE 1003.1-1990, if
3619 **	_POSIX_CHOWN_RESTRICTED is defined and not equal to -1, then
3620 **	no non-root process can give away the file.  However, vendors
3621 **	don't take NFS into account, so a comfortable value of
3622 **	_POSIX_CHOWN_RESTRICTED tells us nothing.
3623 **
3624 **	Also, some systems (e.g., IRIX 6.2) return 1 from fpathconf()
3625 **	even on files where chown is not restricted.  Many systems get
3626 **	this wrong on NFS-based filesystems (that is, they say that chown
3627 **	is restricted [safe] on NFS filesystems where it may not be, since
3628 **	other systems can access the same filesystem and do file giveaway;
3629 **	only the NFS server knows for sure!)  Hence, it is important to
3630 **	get the value of SAFENFSPATHCONF correct -- it should be defined
3631 **	_only_ after testing (see test/t_pathconf.c) a system on an unsafe
3632 **	NFS-based filesystem to ensure that you can get meaningful results.
3633 **	If in doubt, assume unsafe!
3634 **
3635 **	You may also need to tweak IS_SAFE_CHOWN -- it should be a
3636 **	condition indicating whether the return from pathconf indicates
3637 **	that chown is safe (typically either > 0 or >= 0 -- there isn't
3638 **	even any agreement about whether a zero return means that a file
3639 **	is or is not safe).  It defaults to "> 0".
3640 **
3641 **	If the parent directory is safe (writable only by owner back
3642 **	to the root) then we can relax slightly and trust fpathconf
3643 **	in more circumstances.  This is really a crock -- if this is an
3644 **	NFS mounted filesystem then we really know nothing about the
3645 **	underlying implementation.  However, most systems pessimize and
3646 **	return an error (EINVAL or EOPNOTSUPP) on NFS filesystems, which
3647 **	we interpret as unsafe, as we should.  Thus, this heuristic gets
3648 **	us into a possible problem only on systems that have a broken
3649 **	pathconf implementation and which are also poorly configured
3650 **	(have :include: files in group- or world-writable directories).
3651 **
3652 **	Parameters:
3653 **		fd -- the file descriptor to check.
3654 **		safedir -- set if the parent directory is safe.
3655 **
3656 **	Returns:
3657 **		true -- if the chown(2) operation is "safe" -- that is,
3658 **			only root can chown the file to an arbitrary user.
3659 **		false -- if an arbitrary user can give away a file.
3660 */
3661 
3662 #ifndef IS_SAFE_CHOWN
3663 # define IS_SAFE_CHOWN	> 0
3664 #endif /* ! IS_SAFE_CHOWN */
3665 
3666 bool
3667 chownsafe(fd, safedir)
3668 	int fd;
3669 	bool safedir;
3670 {
3671 # if (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && \
3672     (defined(_PC_CHOWN_RESTRICTED) || defined(_GNU_TYPES_H))
3673 	int rval;
3674 
3675 	/* give the system administrator a chance to override */
3676 	if (bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail))
3677 		return true;
3678 
3679 	/*
3680 	**  Some systems (e.g., SunOS) seem to have the call and the
3681 	**  #define _PC_CHOWN_RESTRICTED, but don't actually implement
3682 	**  the call.  This heuristic checks for that.
3683 	*/
3684 
3685 	errno = 0;
3686 	rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
3687 #  if SAFENFSPATHCONF
3688 	return errno == 0 && rval IS_SAFE_CHOWN;
3689 #  else /* SAFENFSPATHCONF */
3690 	return safedir && errno == 0 && rval IS_SAFE_CHOWN;
3691 #  endif /* SAFENFSPATHCONF */
3692 # else /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3693 	return bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail);
3694 # endif /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3695 }
3696 /*
3697 **  RESETLIMITS -- reset system controlled resource limits
3698 **
3699 **	This is to avoid denial-of-service attacks
3700 **
3701 **	Parameters:
3702 **		none
3703 **
3704 **	Returns:
3705 **		none
3706 */
3707 
3708 #if HASSETRLIMIT
3709 # ifdef RLIMIT_NEEDS_SYS_TIME_H
3710 #  include <sys/time.h>
3711 # endif /* RLIMIT_NEEDS_SYS_TIME_H */
3712 # include <sys/resource.h>
3713 #endif /* HASSETRLIMIT */
3714 
3715 void
3716 resetlimits()
3717 {
3718 #if HASSETRLIMIT
3719 	struct rlimit lim;
3720 
3721 	lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
3722 	(void) setrlimit(RLIMIT_CPU, &lim);
3723 	(void) setrlimit(RLIMIT_FSIZE, &lim);
3724 # ifdef RLIMIT_NOFILE
3725 	lim.rlim_cur = lim.rlim_max = FD_SETSIZE;
3726 	(void) setrlimit(RLIMIT_NOFILE, &lim);
3727 # endif /* RLIMIT_NOFILE */
3728 #else /* HASSETRLIMIT */
3729 # if HASULIMIT
3730 	(void) ulimit(2, 0x3fffff);
3731 	(void) ulimit(4, FD_SETSIZE);
3732 # endif /* HASULIMIT */
3733 #endif /* HASSETRLIMIT */
3734 	errno = 0;
3735 }
3736 /*
3737 **  SETVENDOR -- process vendor code from V configuration line
3738 **
3739 **	Parameters:
3740 **		vendor -- string representation of vendor.
3741 **
3742 **	Returns:
3743 **		true -- if ok.
3744 **		false -- if vendor code could not be processed.
3745 **
3746 **	Side Effects:
3747 **		It is reasonable to set mode flags here to tweak
3748 **		processing in other parts of the code if necessary.
3749 **		For example, if you are a vendor that uses $%y to
3750 **		indicate YP lookups, you could enable that here.
3751 */
3752 
3753 bool
3754 setvendor(vendor)
3755 	char *vendor;
3756 {
3757 	if (sm_strcasecmp(vendor, "Berkeley") == 0)
3758 	{
3759 		VendorCode = VENDOR_BERKELEY;
3760 		return true;
3761 	}
3762 
3763 	/* add vendor extensions here */
3764 
3765 #ifdef SUN_EXTENSIONS
3766 	if (sm_strcasecmp(vendor, "Sun") == 0)
3767 	{
3768 		VendorCode = VENDOR_SUN;
3769 		return true;
3770 	}
3771 #endif /* SUN_EXTENSIONS */
3772 
3773 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3774 	if (sm_strcasecmp(vendor, VENDOR_NAME) == 0)
3775 	{
3776 		VendorCode = VENDOR_CODE;
3777 		return true;
3778 	}
3779 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3780 
3781 	return false;
3782 }
3783 /*
3784 **  GETVENDOR -- return vendor name based on vendor code
3785 **
3786 **	Parameters:
3787 **		vendorcode -- numeric representation of vendor.
3788 **
3789 **	Returns:
3790 **		string containing vendor name.
3791 */
3792 
3793 char *
3794 getvendor(vendorcode)
3795 	int vendorcode;
3796 {
3797 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3798 	/*
3799 	**  Can't have the same switch case twice so need to
3800 	**  handle VENDOR_CODE outside of switch.  It might
3801 	**  match one of the existing VENDOR_* codes.
3802 	*/
3803 
3804 	if (vendorcode == VENDOR_CODE)
3805 		return VENDOR_NAME;
3806 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3807 
3808 	switch (vendorcode)
3809 	{
3810 	  case VENDOR_BERKELEY:
3811 		return "Berkeley";
3812 
3813 	  case VENDOR_SUN:
3814 		return "Sun";
3815 
3816 	  case VENDOR_HP:
3817 		return "HP";
3818 
3819 	  case VENDOR_IBM:
3820 		return "IBM";
3821 
3822 	  case VENDOR_SENDMAIL:
3823 		return "Sendmail";
3824 
3825 	  default:
3826 		return "Unknown";
3827 	}
3828 }
3829 /*
3830 **  VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
3831 **
3832 **	Vendor_pre_defaults is called before reading the configuration
3833 **	file; vendor_post_defaults is called immediately after.
3834 **
3835 **	Parameters:
3836 **		e -- the global environment to initialize.
3837 **
3838 **	Returns:
3839 **		none.
3840 */
3841 
3842 #if SHARE_V1
3843 int	DefShareUid;	/* default share uid to run as -- unused??? */
3844 #endif /* SHARE_V1 */
3845 
3846 void
3847 vendor_pre_defaults(e)
3848 	ENVELOPE *e;
3849 {
3850 #if SHARE_V1
3851 	/* OTHERUID is defined in shares.h, do not be alarmed */
3852 	DefShareUid = OTHERUID;
3853 #endif /* SHARE_V1 */
3854 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3855 	sun_pre_defaults(e);
3856 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3857 #ifdef apollo
3858 	/*
3859 	**  stupid domain/os can't even open
3860 	**  /etc/mail/sendmail.cf without this
3861 	*/
3862 
3863 	setuserenv("ISP", NULL);
3864 	setuserenv("SYSTYPE", NULL);
3865 #endif /* apollo */
3866 }
3867 
3868 
3869 void
3870 vendor_post_defaults(e)
3871 	ENVELOPE *e;
3872 {
3873 #ifdef __QNX__
3874 	char *p;
3875 
3876 	/* Makes sure the SOCK environment variable remains */
3877 	if (p = getextenv("SOCK"))
3878 		setuserenv("SOCK", p);
3879 #endif /* __QNX__ */
3880 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3881 	sun_post_defaults(e);
3882 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3883 }
3884 /*
3885 **  VENDOR_DAEMON_SETUP -- special vendor setup needed for daemon mode
3886 */
3887 
3888 void
3889 vendor_daemon_setup(e)
3890 	ENVELOPE *e;
3891 {
3892 #if HASSETLOGIN
3893 	(void) setlogin(RunAsUserName);
3894 #endif /* HASSETLOGIN */
3895 #if SECUREWARE
3896 	if (getluid() != -1)
3897 	{
3898 		usrerr("Daemon cannot have LUID");
3899 		finis(false, true, EX_USAGE);
3900 	}
3901 #endif /* SECUREWARE */
3902 }
3903 /*
3904 **  VENDOR_SET_UID -- do setup for setting a user id
3905 **
3906 **	This is called when we are still root.
3907 **
3908 **	Parameters:
3909 **		uid -- the uid we are about to become.
3910 **
3911 **	Returns:
3912 **		none.
3913 */
3914 
3915 void
3916 vendor_set_uid(uid)
3917 	UID_T uid;
3918 {
3919 	/*
3920 	**  We need to setup the share groups (lnodes)
3921 	**  and add auditing information (luid's)
3922 	**  before we loose our ``root''ness.
3923 	*/
3924 #if SHARE_V1
3925 	if (setupshares(uid, syserr) != 0)
3926 		syserr("Unable to set up shares");
3927 #endif /* SHARE_V1 */
3928 #if SECUREWARE
3929 	(void) setup_secure(uid);
3930 #endif /* SECUREWARE */
3931 }
3932 /*
3933 **  VALIDATE_CONNECTION -- check connection for rationality
3934 **
3935 **	If the connection is rejected, this routine should log an
3936 **	appropriate message -- but should never issue any SMTP protocol.
3937 **
3938 **	Parameters:
3939 **		sap -- a pointer to a SOCKADDR naming the peer.
3940 **		hostname -- the name corresponding to sap.
3941 **		e -- the current envelope.
3942 **
3943 **	Returns:
3944 **		error message from rejection.
3945 **		NULL if not rejected.
3946 */
3947 
3948 #if TCPWRAPPERS
3949 # include <tcpd.h>
3950 
3951 /* tcpwrappers does no logging, but you still have to declare these -- ugh */
3952 int	allow_severity	= LOG_INFO;
3953 int	deny_severity	= LOG_NOTICE;
3954 #endif /* TCPWRAPPERS */
3955 
3956 char *
3957 validate_connection(sap, hostname, e)
3958 	SOCKADDR *sap;
3959 	char *hostname;
3960 	ENVELOPE *e;
3961 {
3962 #if TCPWRAPPERS
3963 	char *host;
3964 	char *addr;
3965 	extern int hosts_ctl();
3966 #endif /* TCPWRAPPERS */
3967 
3968 	if (tTd(48, 3))
3969 		sm_dprintf("validate_connection(%s, %s)\n",
3970 			hostname, anynet_ntoa(sap));
3971 
3972 	if (rscheck("check_relay", hostname, anynet_ntoa(sap),
3973 		    e, RSF_RMCOMM|RSF_COUNT, 3, NULL, NOQID) != EX_OK)
3974 	{
3975 		static char reject[BUFSIZ*2];
3976 		extern char MsgBuf[];
3977 
3978 		if (tTd(48, 4))
3979 			sm_dprintf("  ... validate_connection: BAD (rscheck)\n");
3980 
3981 		if (strlen(MsgBuf) >= 3)
3982 			(void) sm_strlcpy(reject, MsgBuf, sizeof reject);
3983 		else
3984 			(void) sm_strlcpy(reject, "Access denied", sizeof reject);
3985 
3986 		return reject;
3987 	}
3988 
3989 #if TCPWRAPPERS
3990 	if (hostname[0] == '[' && hostname[strlen(hostname) - 1] == ']')
3991 		host = "unknown";
3992 	else
3993 		host = hostname;
3994 	addr = anynet_ntoa(sap);
3995 
3996 # if NETINET6
3997 	/* TCP/Wrappers don't want the IPv6: protocol label */
3998 	if (addr != NULL && sm_strncasecmp(addr, "IPv6:", 5) == 0)
3999 		addr += 5;
4000 # endif /* NETINET6 */
4001 
4002 	if (!hosts_ctl("sendmail", host, addr, STRING_UNKNOWN))
4003 	{
4004 		if (tTd(48, 4))
4005 			sm_dprintf("  ... validate_connection: BAD (tcpwrappers)\n");
4006 		if (LogLevel > 3)
4007 			sm_syslog(LOG_NOTICE, e->e_id,
4008 				  "tcpwrappers (%s, %s) rejection",
4009 				  host, addr);
4010 		return "Access denied";
4011 	}
4012 #endif /* TCPWRAPPERS */
4013 	if (tTd(48, 4))
4014 		sm_dprintf("  ... validate_connection: OK\n");
4015 	return NULL;
4016 }
4017 
4018 /*
4019 **  STRTOL -- convert string to long integer
4020 **
4021 **	For systems that don't have it in the C library.
4022 **
4023 **	This is taken verbatim from the 4.4-Lite C library.
4024 */
4025 
4026 #if NEEDSTRTOL
4027 
4028 # if defined(LIBC_SCCS) && !defined(lint)
4029 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
4030 # endif /* defined(LIBC_SCCS) && !defined(lint) */
4031 
4032 /*
4033 **  Convert a string to a long integer.
4034 **
4035 **  Ignores `locale' stuff.  Assumes that the upper and lower case
4036 **  alphabets and digits are each contiguous.
4037 */
4038 
4039 long
4040 strtol(nptr, endptr, base)
4041 	const char *nptr;
4042 	char **endptr;
4043 	register int base;
4044 {
4045 	register const char *s = nptr;
4046 	register unsigned long acc;
4047 	register int c;
4048 	register unsigned long cutoff;
4049 	register int neg = 0, any, cutlim;
4050 
4051 	/*
4052 	**  Skip white space and pick up leading +/- sign if any.
4053 	**  If base is 0, allow 0x for hex and 0 for octal, else
4054 	**  assume decimal; if base is already 16, allow 0x.
4055 	*/
4056 	do {
4057 		c = *s++;
4058 	} while (isspace(c));
4059 	if (c == '-') {
4060 		neg = 1;
4061 		c = *s++;
4062 	} else if (c == '+')
4063 		c = *s++;
4064 	if ((base == 0 || base == 16) &&
4065 	    c == '0' && (*s == 'x' || *s == 'X')) {
4066 		c = s[1];
4067 		s += 2;
4068 		base = 16;
4069 	}
4070 	if (base == 0)
4071 		base = c == '0' ? 8 : 10;
4072 
4073 	/*
4074 	**  Compute the cutoff value between legal numbers and illegal
4075 	**  numbers.  That is the largest legal value, divided by the
4076 	**  base.  An input number that is greater than this value, if
4077 	**  followed by a legal input character, is too big.  One that
4078 	**  is equal to this value may be valid or not; the limit
4079 	**  between valid and invalid numbers is then based on the last
4080 	**  digit.  For instance, if the range for longs is
4081 	**  [-2147483648..2147483647] and the input base is 10,
4082 	**  cutoff will be set to 214748364 and cutlim to either
4083 	**  7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
4084 	**  a value > 214748364, or equal but the next digit is > 7 (or 8),
4085 	**  the number is too big, and we will return a range error.
4086 	**
4087 	**  Set any if any `digits' consumed; make it negative to indicate
4088 	**  overflow.
4089 	*/
4090 	cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
4091 	cutlim = cutoff % (unsigned long) base;
4092 	cutoff /= (unsigned long) base;
4093 	for (acc = 0, any = 0;; c = *s++) {
4094 		if (isdigit(c))
4095 			c -= '0';
4096 		else if (isalpha(c))
4097 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4098 		else
4099 			break;
4100 		if (c >= base)
4101 			break;
4102 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
4103 			any = -1;
4104 		else {
4105 			any = 1;
4106 			acc *= base;
4107 			acc += c;
4108 		}
4109 	}
4110 	if (any < 0) {
4111 		acc = neg ? LONG_MIN : LONG_MAX;
4112 		errno = ERANGE;
4113 	} else if (neg)
4114 		acc = -acc;
4115 	if (endptr != 0)
4116 		*endptr = (char *)(any ? s - 1 : nptr);
4117 	return acc;
4118 }
4119 
4120 #endif /* NEEDSTRTOL */
4121 /*
4122 **  STRSTR -- find first substring in string
4123 **
4124 **	Parameters:
4125 **		big -- the big (full) string.
4126 **		little -- the little (sub) string.
4127 **
4128 **	Returns:
4129 **		A pointer to the first instance of little in big.
4130 **		big if little is the null string.
4131 **		NULL if little is not contained in big.
4132 */
4133 
4134 #if NEEDSTRSTR
4135 
4136 char *
4137 strstr(big, little)
4138 	char *big;
4139 	char *little;
4140 {
4141 	register char *p = big;
4142 	int l;
4143 
4144 	if (*little == '\0')
4145 		return big;
4146 	l = strlen(little);
4147 
4148 	while ((p = strchr(p, *little)) != NULL)
4149 	{
4150 		if (strncmp(p, little, l) == 0)
4151 			return p;
4152 		p++;
4153 	}
4154 	return NULL;
4155 }
4156 
4157 #endif /* NEEDSTRSTR */
4158 /*
4159 **  SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
4160 **
4161 **	Some operating systems have wierd problems with the gethostbyXXX
4162 **	routines.  For example, Solaris versions at least through 2.3
4163 **	don't properly deliver a canonical h_name field.  This tries to
4164 **	work around these problems.
4165 **
4166 **	Support IPv6 as well as IPv4.
4167 */
4168 
4169 #if NETINET6 && NEEDSGETIPNODE
4170 
4171 # ifndef AI_DEFAULT
4172 #  define AI_DEFAULT	0	/* dummy */
4173 # endif /* ! AI_DEFAULT */
4174 # ifndef AI_ADDRCONFIG
4175 #  define AI_ADDRCONFIG	0	/* dummy */
4176 # endif /* ! AI_ADDRCONFIG */
4177 # ifndef AI_V4MAPPED
4178 #  define AI_V4MAPPED	0	/* dummy */
4179 # endif /* ! AI_V4MAPPED */
4180 # ifndef AI_ALL
4181 #  define AI_ALL	0	/* dummy */
4182 # endif /* ! AI_ALL */
4183 
4184 static struct hostent *
4185 getipnodebyname(name, family, flags, err)
4186 	char *name;
4187 	int family;
4188 	int flags;
4189 	int *err;
4190 {
4191 	bool resv6 = true;
4192 	struct hostent *h;
4193 
4194 	if (family == AF_INET6)
4195 	{
4196 		/* From RFC2133, section 6.1 */
4197 		resv6 = bitset(RES_USE_INET6, _res.options);
4198 		_res.options |= RES_USE_INET6;
4199 	}
4200 	SM_SET_H_ERRNO(0);
4201 	h = gethostbyname(name);
4202 	if (!resv6)
4203 		_res.options &= ~RES_USE_INET6;
4204 	*err = h_errno;
4205 	return h;
4206 }
4207 
4208 static struct hostent *
4209 getipnodebyaddr(addr, len, family, err)
4210 	char *addr;
4211 	int len;
4212 	int family;
4213 	int *err;
4214 {
4215 	struct hostent *h;
4216 
4217 	SM_SET_H_ERRNO(0);
4218 	h = gethostbyaddr(addr, len, family);
4219 	*err = h_errno;
4220 	return h;
4221 }
4222 
4223 void
4224 freehostent(h)
4225 	struct hostent *h;
4226 {
4227 	/*
4228 	**  Stub routine -- if they don't have getipnodeby*(),
4229 	**  they probably don't have the free routine either.
4230 	*/
4231 
4232 	return;
4233 }
4234 #endif /* NETINET6 && NEEDSGETIPNODE */
4235 
4236 struct hostent *
4237 sm_gethostbyname(name, family)
4238 	char *name;
4239 	int family;
4240 {
4241 	int save_errno;
4242 	struct hostent *h = NULL;
4243 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
4244 # if SOLARIS == 20300 || SOLARIS == 203
4245 	static struct hostent hp;
4246 	static char buf[1000];
4247 	extern struct hostent *_switch_gethostbyname_r();
4248 
4249 	if (tTd(61, 10))
4250 		sm_dprintf("_switch_gethostbyname_r(%s)... ", name);
4251 	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
4252 	save_errno = errno;
4253 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4254 	extern struct hostent *__switch_gethostbyname();
4255 
4256 	if (tTd(61, 10))
4257 		sm_dprintf("__switch_gethostbyname(%s)... ", name);
4258 	h = __switch_gethostbyname(name);
4259 	save_errno = errno;
4260 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4261 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4262 	int nmaps;
4263 # if NETINET6
4264 	int flags = AI_DEFAULT|AI_ALL;
4265 	int err;
4266 # endif /* NETINET6 */
4267 	char *maptype[MAXMAPSTACK];
4268 	short mapreturn[MAXMAPACTIONS];
4269 	char hbuf[MAXNAME];
4270 
4271 	if (tTd(61, 10))
4272 		sm_dprintf("sm_gethostbyname(%s, %d)... ", name, family);
4273 
4274 # if NETINET6
4275 #  if ADDRCONFIG_IS_BROKEN
4276 	flags &= ~AI_ADDRCONFIG;
4277 #  endif /* ADDRCONFIG_IS_BROKEN */
4278 	h = getipnodebyname(name, family, flags, &err);
4279 	SM_SET_H_ERRNO(err);
4280 # else /* NETINET6 */
4281 	h = gethostbyname(name);
4282 # endif /* NETINET6 */
4283 
4284 	save_errno = errno;
4285 	if (h == NULL)
4286 	{
4287 		if (tTd(61, 10))
4288 			sm_dprintf("failure\n");
4289 
4290 		nmaps = switch_map_find("hosts", maptype, mapreturn);
4291 		while (--nmaps >= 0)
4292 		{
4293 			if (strcmp(maptype[nmaps], "nis") == 0 ||
4294 			    strcmp(maptype[nmaps], "files") == 0)
4295 				break;
4296 		}
4297 
4298 		if (nmaps >= 0)
4299 		{
4300 			/* try short name */
4301 			if (strlen(name) > sizeof hbuf - 1)
4302 			{
4303 				errno = save_errno;
4304 				return NULL;
4305 			}
4306 			(void) sm_strlcpy(hbuf, name, sizeof hbuf);
4307 			(void) shorten_hostname(hbuf);
4308 
4309 			/* if it hasn't been shortened, there's no point */
4310 			if (strcmp(hbuf, name) != 0)
4311 			{
4312 				if (tTd(61, 10))
4313 					sm_dprintf("sm_gethostbyname(%s, %d)... ",
4314 					       hbuf, family);
4315 
4316 # if NETINET6
4317 				h = getipnodebyname(hbuf, family, flags, &err);
4318 				SM_SET_H_ERRNO(err);
4319 				save_errno = errno;
4320 # else /* NETINET6 */
4321 				h = gethostbyname(hbuf);
4322 				save_errno = errno;
4323 # endif /* NETINET6 */
4324 			}
4325 		}
4326 	}
4327 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4328 	if (tTd(61, 10))
4329 	{
4330 		if (h == NULL)
4331 			sm_dprintf("failure\n");
4332 		else
4333 		{
4334 			sm_dprintf("%s\n", h->h_name);
4335 			if (tTd(61, 11))
4336 			{
4337 #if NETINET6
4338 				struct in6_addr ia6;
4339 				char buf6[INET6_ADDRSTRLEN];
4340 #else /* NETINET6 */
4341 				struct in_addr ia;
4342 #endif /* NETINET6 */
4343 				size_t i;
4344 
4345 				if (h->h_aliases != NULL)
4346 					for (i = 0; h->h_aliases[i] != NULL;
4347 					     i++)
4348 						sm_dprintf("\talias: %s\n",
4349 							h->h_aliases[i]);
4350 				for (i = 0; h->h_addr_list[i] != NULL; i++)
4351 				{
4352 					char *addr;
4353 
4354 #if NETINET6
4355 					memmove(&ia6, h->h_addr_list[i],
4356 						IN6ADDRSZ);
4357 					addr = anynet_ntop(&ia6,
4358 							   buf6, sizeof buf6);
4359 #else /* NETINET6 */
4360 					memmove(&ia, h->h_addr_list[i],
4361 						INADDRSZ);
4362 					addr = (char *) inet_ntoa(ia);
4363 #endif /* NETINET6 */
4364 					if (addr != NULL)
4365 						sm_dprintf("\taddr: %s\n", addr);
4366 				}
4367 			}
4368 		}
4369 	}
4370 	errno = save_errno;
4371 	return h;
4372 }
4373 
4374 struct hostent *
4375 sm_gethostbyaddr(addr, len, type)
4376 	char *addr;
4377 	int len;
4378 	int type;
4379 {
4380 	struct hostent *hp;
4381 
4382 #if NETINET6
4383 	if (type == AF_INET6 &&
4384 	    IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *) addr))
4385 	{
4386 		/* Avoid reverse lookup for IPv6 unspecified address */
4387 		SM_SET_H_ERRNO(HOST_NOT_FOUND);
4388 		return NULL;
4389 	}
4390 #endif /* NETINET6 */
4391 
4392 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204)
4393 # if SOLARIS == 20300 || SOLARIS == 203
4394 	{
4395 		static struct hostent he;
4396 		static char buf[1000];
4397 		extern struct hostent *_switch_gethostbyaddr_r();
4398 
4399 		hp = _switch_gethostbyaddr_r(addr, len, type, &he,
4400 					     buf, sizeof(buf), &h_errno);
4401 	}
4402 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4403 	{
4404 		extern struct hostent *__switch_gethostbyaddr();
4405 
4406 		hp = __switch_gethostbyaddr(addr, len, type);
4407 	}
4408 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4409 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4410 # if NETINET6
4411 	{
4412 		int err;
4413 
4414 		hp = getipnodebyaddr(addr, len, type, &err);
4415 		SM_SET_H_ERRNO(err);
4416 	}
4417 # else /* NETINET6 */
4418 	hp = gethostbyaddr(addr, len, type);
4419 # endif /* NETINET6 */
4420 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4421 	return hp;
4422 }
4423 /*
4424 **  SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
4425 */
4426 
4427 struct passwd *
4428 sm_getpwnam(user)
4429 	char *user;
4430 {
4431 #ifdef _AIX4
4432 	extern struct passwd *_getpwnam_shadow(const char *, const int);
4433 
4434 	return _getpwnam_shadow(user, 0);
4435 #else /* _AIX4 */
4436 	return getpwnam(user);
4437 #endif /* _AIX4 */
4438 }
4439 
4440 struct passwd *
4441 sm_getpwuid(uid)
4442 	UID_T uid;
4443 {
4444 #if defined(_AIX4) && 0
4445 	extern struct passwd *_getpwuid_shadow(const int, const int);
4446 
4447 	return _getpwuid_shadow(uid,0);
4448 #else /* defined(_AIX4) && 0 */
4449 	return getpwuid(uid);
4450 #endif /* defined(_AIX4) && 0 */
4451 }
4452 /*
4453 **  SECUREWARE_SETUP_SECURE -- Convex SecureWare setup
4454 **
4455 **	Set up the trusted computing environment for C2 level security
4456 **	under SecureWare.
4457 **
4458 **	Parameters:
4459 **		uid -- uid of the user to initialize in the TCB
4460 **
4461 **	Returns:
4462 **		none
4463 **
4464 **	Side Effects:
4465 **		Initialized the user in the trusted computing base
4466 */
4467 
4468 #if SECUREWARE
4469 
4470 # include <sys/security.h>
4471 # include <prot.h>
4472 
4473 void
4474 secureware_setup_secure(uid)
4475 	UID_T uid;
4476 {
4477 	int rc;
4478 
4479 	if (getluid() != -1)
4480 		return;
4481 
4482 	if ((rc = set_secure_info(uid)) != SSI_GOOD_RETURN)
4483 	{
4484 		switch (rc)
4485 		{
4486 		  case SSI_NO_PRPW_ENTRY:
4487 			syserr("No protected passwd entry, uid = %d",
4488 			       (int) uid);
4489 			break;
4490 
4491 		  case SSI_LOCKED:
4492 			syserr("Account has been disabled, uid = %d",
4493 			       (int) uid);
4494 			break;
4495 
4496 		  case SSI_RETIRED:
4497 			syserr("Account has been retired, uid = %d",
4498 			       (int) uid);
4499 			break;
4500 
4501 		  case SSI_BAD_SET_LUID:
4502 			syserr("Could not set LUID, uid = %d", (int) uid);
4503 			break;
4504 
4505 		  case SSI_BAD_SET_PRIVS:
4506 			syserr("Could not set kernel privs, uid = %d",
4507 			       (int) uid);
4508 
4509 		  default:
4510 			syserr("Unknown return code (%d) from set_secure_info(%d)",
4511 				rc, (int) uid);
4512 			break;
4513 		}
4514 		finis(false, true, EX_NOPERM);
4515 	}
4516 }
4517 #endif /* SECUREWARE */
4518 /*
4519 **  ADD_HOSTNAMES -- Add a hostname to class 'w' based on IP address
4520 **
4521 **	Add hostnames to class 'w' based on the IP address read from
4522 **	the network interface.
4523 **
4524 **	Parameters:
4525 **		sa -- a pointer to a SOCKADDR containing the address
4526 **
4527 **	Returns:
4528 **		0 if successful, -1 if host lookup fails.
4529 */
4530 
4531 static int
4532 add_hostnames(sa)
4533 	SOCKADDR *sa;
4534 {
4535 	struct hostent *hp;
4536 	char **ha;
4537 	char hnb[MAXHOSTNAMELEN];
4538 
4539 	/* lookup name with IP address */
4540 	switch (sa->sa.sa_family)
4541 	{
4542 #if NETINET
4543 	  case AF_INET:
4544 		hp = sm_gethostbyaddr((char *) &sa->sin.sin_addr,
4545 				      sizeof(sa->sin.sin_addr),
4546 				      sa->sa.sa_family);
4547 		break;
4548 #endif /* NETINET */
4549 
4550 #if NETINET6
4551 	  case AF_INET6:
4552 		hp = sm_gethostbyaddr((char *) &sa->sin6.sin6_addr,
4553 				      sizeof(sa->sin6.sin6_addr),
4554 				      sa->sa.sa_family);
4555 		break;
4556 #endif /* NETINET6 */
4557 
4558 	  default:
4559 		/* Give warning about unsupported family */
4560 		if (LogLevel > 3)
4561 			sm_syslog(LOG_WARNING, NOQID,
4562 				  "Unsupported address family %d: %.100s",
4563 				  sa->sa.sa_family, anynet_ntoa(sa));
4564 		return -1;
4565 	}
4566 
4567 	if (hp == NULL)
4568 	{
4569 		int save_errno = errno;
4570 
4571 		if (LogLevel > 3 &&
4572 #if NETINET6
4573 		    !(sa->sa.sa_family == AF_INET6 &&
4574 		      IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr)) &&
4575 #endif /* NETINET6 */
4576 		    true)
4577 			sm_syslog(LOG_WARNING, NOQID,
4578 				  "gethostbyaddr(%.100s) failed: %d",
4579 				  anynet_ntoa(sa),
4580 #if NAMED_BIND
4581 				  h_errno
4582 #else /* NAMED_BIND */
4583 				  -1
4584 #endif /* NAMED_BIND */
4585 				 );
4586 		errno = save_errno;
4587 		return -1;
4588 	}
4589 
4590 	/* save its cname */
4591 	if (!wordinclass((char *) hp->h_name, 'w'))
4592 	{
4593 		setclass('w', (char *) hp->h_name);
4594 		if (tTd(0, 4))
4595 			sm_dprintf("\ta.k.a.: %s\n", hp->h_name);
4596 
4597 		if (sm_snprintf(hnb, sizeof hnb, "[%s]", hp->h_name) < sizeof hnb
4598 		    && !wordinclass((char *) hnb, 'w'))
4599 			setclass('w', hnb);
4600 	}
4601 	else
4602 	{
4603 		if (tTd(0, 43))
4604 			sm_dprintf("\ta.k.a.: %s (already in $=w)\n", hp->h_name);
4605 	}
4606 
4607 	/* save all it aliases name */
4608 	for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
4609 	{
4610 		if (!wordinclass(*ha, 'w'))
4611 		{
4612 			setclass('w', *ha);
4613 			if (tTd(0, 4))
4614 				sm_dprintf("\ta.k.a.: %s\n", *ha);
4615 			if (sm_snprintf(hnb, sizeof hnb,
4616 				     "[%s]", *ha) < sizeof hnb &&
4617 			    !wordinclass((char *) hnb, 'w'))
4618 				setclass('w', hnb);
4619 		}
4620 		else
4621 		{
4622 			if (tTd(0, 43))
4623 				sm_dprintf("\ta.k.a.: %s (already in $=w)\n",
4624 					*ha);
4625 		}
4626 	}
4627 #if NETINET6
4628 	freehostent(hp);
4629 #endif /* NETINET6 */
4630 	return 0;
4631 }
4632 /*
4633 **  LOAD_IF_NAMES -- load interface-specific names into $=w
4634 **
4635 **	Parameters:
4636 **		none.
4637 **
4638 **	Returns:
4639 **		none.
4640 **
4641 **	Side Effects:
4642 **		Loads $=w with the names of all the interfaces.
4643 */
4644 
4645 #if !NETINET
4646 # define SIOCGIFCONF_IS_BROKEN	1 /* XXX */
4647 #endif /* !NETINET */
4648 
4649 #if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
4650 struct rtentry;
4651 struct mbuf;
4652 # ifndef SUNOS403
4653 #  include <sys/time.h>
4654 # endif /* ! SUNOS403 */
4655 # if (_AIX4 >= 40300) && !defined(_NET_IF_H)
4656 #  undef __P
4657 # endif /* (_AIX4 >= 40300) && !defined(_NET_IF_H) */
4658 # include <net/if.h>
4659 #endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
4660 
4661 void
4662 load_if_names()
4663 {
4664 # if NETINET6 && defined(SIOCGLIFCONF)
4665 #  ifdef __hpux
4666 
4667     /*
4668     **  Unfortunately, HP has changed all of the structures,
4669     **  making life difficult for implementors.
4670     */
4671 
4672 #   define lifconf	if_laddrconf
4673 #   define lifc_len	iflc_len
4674 #   define lifc_buf	iflc_buf
4675 #   define lifreq	if_laddrreq
4676 #   define lifr_addr	iflr_addr
4677 #   define lifr_name	iflr_name
4678 #   define lifr_flags	iflr_flags
4679 #   define ss_family	sa_family
4680 #   undef SIOCGLIFNUM
4681 #  endif /* __hpux */
4682 
4683 	int s;
4684 	int i;
4685 	size_t len;
4686 	int numifs;
4687 	char *buf;
4688 	struct lifconf lifc;
4689 #  ifdef SIOCGLIFNUM
4690 	struct lifnum lifn;
4691 #  endif /* SIOCGLIFNUM */
4692 
4693 	s = socket(InetMode, SOCK_DGRAM, 0);
4694 	if (s == -1)
4695 		return;
4696 
4697 	/* get the list of known IP address from the kernel */
4698 #  ifdef __hpux
4699 	i = ioctl(s, SIOCGIFNUM, (char *) &numifs);
4700 #  endif /* __hpux */
4701 #  ifdef SIOCGLIFNUM
4702 	lifn.lifn_family = AF_UNSPEC;
4703 	lifn.lifn_flags = 0;
4704 	i = ioctl(s, SIOCGLIFNUM, (char *)&lifn);
4705 	numifs = lifn.lifn_count;
4706 #  endif /* SIOCGLIFNUM */
4707 
4708 #  if defined(__hpux) || defined(SIOCGLIFNUM)
4709 	if (i < 0)
4710 	{
4711 		/* can't get number of interfaces -- fall back */
4712 		if (tTd(0, 4))
4713 			sm_dprintf("SIOCGLIFNUM failed: %s\n",
4714 				   sm_errstring(errno));
4715 		numifs = -1;
4716 	}
4717 	else if (tTd(0, 42))
4718 		sm_dprintf("system has %d interfaces\n", numifs);
4719 	if (numifs < 0)
4720 #  endif /* defined(__hpux) || defined(SIOCGLIFNUM) */
4721 		numifs = MAXINTERFACES;
4722 
4723 	if (numifs <= 0)
4724 	{
4725 		(void) close(s);
4726 		return;
4727 	}
4728 
4729 	len = lifc.lifc_len = numifs * sizeof (struct lifreq);
4730 	buf = lifc.lifc_buf = xalloc(lifc.lifc_len);
4731 #  ifndef __hpux
4732 	lifc.lifc_family = AF_UNSPEC;
4733 	lifc.lifc_flags = 0;
4734 #  endif /* __hpux */
4735 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0)
4736 	{
4737 		if (tTd(0, 4))
4738 			sm_dprintf("SIOCGLIFCONF failed: %s\n",
4739 				   sm_errstring(errno));
4740 		(void) close(s);
4741 		sm_free(buf);
4742 		return;
4743 	}
4744 
4745 	/* scan the list of IP address */
4746 	if (tTd(0, 40))
4747 		sm_dprintf("scanning for interface specific names, lifc_len=%ld\n",
4748 			   (long) len);
4749 
4750 	for (i = 0; i < len && i >= 0; )
4751 	{
4752 		int flags;
4753 		struct lifreq *ifr = (struct lifreq *)&buf[i];
4754 		SOCKADDR *sa = (SOCKADDR *) &ifr->lifr_addr;
4755 		int af = ifr->lifr_addr.ss_family;
4756 		char *addr;
4757 		char *name;
4758 		struct in6_addr ia6;
4759 		struct in_addr ia;
4760 #  ifdef SIOCGLIFFLAGS
4761 		struct lifreq ifrf;
4762 #  endif /* SIOCGLIFFLAGS */
4763 		char ip_addr[256];
4764 		char buf6[INET6_ADDRSTRLEN];
4765 
4766 		/*
4767 		**  We must close and recreate the socket each time
4768 		**  since we don't know what type of socket it is now
4769 		**  (each status function may change it).
4770 		*/
4771 
4772 		(void) close(s);
4773 
4774 		s = socket(af, SOCK_DGRAM, 0);
4775 		if (s == -1)
4776 		{
4777 			sm_free(buf); /* XXX */
4778 			return;
4779 		}
4780 
4781 		/*
4782 		**  If we don't have a complete ifr structure,
4783 		**  don't try to use it.
4784 		*/
4785 
4786 		if ((len - i) < sizeof *ifr)
4787 			break;
4788 
4789 #  ifdef BSD4_4_SOCKADDR
4790 		if (sa->sa.sa_len > sizeof ifr->lifr_addr)
4791 			i += sizeof ifr->lifr_name + sa->sa.sa_len;
4792 		else
4793 #  endif /* BSD4_4_SOCKADDR */
4794 			i += sizeof *ifr;
4795 
4796 		if (tTd(0, 20))
4797 			sm_dprintf("%s\n", anynet_ntoa(sa));
4798 
4799 		if (af != AF_INET && af != AF_INET6)
4800 			continue;
4801 
4802 #  ifdef SIOCGLIFFLAGS
4803 		memset(&ifrf, '\0', sizeof(struct lifreq));
4804 		(void) sm_strlcpy(ifrf.lifr_name, ifr->lifr_name,
4805 				  sizeof(ifrf.lifr_name));
4806 		if (ioctl(s, SIOCGLIFFLAGS, (char *) &ifrf) < 0)
4807 		{
4808 			if (tTd(0, 4))
4809 				sm_dprintf("SIOCGLIFFLAGS failed: %s\n",
4810 					   sm_errstring(errno));
4811 			continue;
4812 		}
4813 
4814 		name = ifr->lifr_name;
4815 		flags = ifrf.lifr_flags;
4816 
4817 		if (tTd(0, 41))
4818 			sm_dprintf("\tflags: %lx\n", (unsigned long) flags);
4819 
4820 		if (!bitset(IFF_UP, flags))
4821 			continue;
4822 #  endif /* SIOCGLIFFLAGS */
4823 
4824 		ip_addr[0] = '\0';
4825 
4826 		/* extract IP address from the list*/
4827 		switch (af)
4828 		{
4829 		  case AF_INET6:
4830 #  ifdef __KAME__
4831 			/* convert into proper scoped address */
4832 			if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
4833 			     IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
4834 			    sa->sin6.sin6_scope_id == 0)
4835 			{
4836 				struct in6_addr *ia6p;
4837 
4838 				ia6p = &sa->sin6.sin6_addr;
4839 				sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
4840 							       ((unsigned int)ia6p->s6_addr[2] << 8));
4841 				ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
4842 			}
4843 #  endif /* __KAME__ */
4844 			ia6 = sa->sin6.sin6_addr;
4845 			if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
4846 			{
4847 				addr = anynet_ntop(&ia6, buf6, sizeof buf6);
4848 				message("WARNING: interface %s is UP with %s address",
4849 					name, addr == NULL ? "(NULL)" : addr);
4850 				continue;
4851 			}
4852 
4853 			/* save IP address in text from */
4854 			addr = anynet_ntop(&ia6, buf6, sizeof buf6);
4855 			if (addr != NULL)
4856 				(void) sm_snprintf(ip_addr, sizeof ip_addr,
4857 						   "[%.*s]",
4858 						   (int) sizeof ip_addr - 3,
4859 						   addr);
4860 			break;
4861 
4862 		  case AF_INET:
4863 			ia = sa->sin.sin_addr;
4864 			if (ia.s_addr == INADDR_ANY ||
4865 			    ia.s_addr == INADDR_NONE)
4866 			{
4867 				message("WARNING: interface %s is UP with %s address",
4868 					name, inet_ntoa(ia));
4869 				continue;
4870 			}
4871 
4872 			/* save IP address in text from */
4873 			(void) sm_snprintf(ip_addr, sizeof ip_addr, "[%.*s]",
4874 					(int) sizeof ip_addr - 3, inet_ntoa(ia));
4875 			break;
4876 		}
4877 
4878 		if (*ip_addr == '\0')
4879 			continue;
4880 
4881 		if (!wordinclass(ip_addr, 'w'))
4882 		{
4883 			setclass('w', ip_addr);
4884 			if (tTd(0, 4))
4885 				sm_dprintf("\ta.k.a.: %s\n", ip_addr);
4886 		}
4887 
4888 #  ifdef SIOCGLIFFLAGS
4889 		/* skip "loopback" interface "lo" */
4890 		if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
4891 		    bitset(IFF_LOOPBACK, flags))
4892 			continue;
4893 #  endif /* SIOCGLIFFLAGS */
4894 		(void) add_hostnames(sa);
4895 	}
4896 	sm_free(buf); /* XXX */
4897 	(void) close(s);
4898 # else /* NETINET6 && defined(SIOCGLIFCONF) */
4899 #  if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
4900 	int s;
4901 	int i;
4902 	struct ifconf ifc;
4903 	int numifs;
4904 
4905 	s = socket(AF_INET, SOCK_DGRAM, 0);
4906 	if (s == -1)
4907 		return;
4908 
4909 	/* get the list of known IP address from the kernel */
4910 #   if defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN
4911 	if (ioctl(s, SIOCGIFNUM, (char *) &numifs) < 0)
4912 	{
4913 		/* can't get number of interfaces -- fall back */
4914 		if (tTd(0, 4))
4915 			sm_dprintf("SIOCGIFNUM failed: %s\n",
4916 				   sm_errstring(errno));
4917 		numifs = -1;
4918 	}
4919 	else if (tTd(0, 42))
4920 		sm_dprintf("system has %d interfaces\n", numifs);
4921 	if (numifs < 0)
4922 #   endif /* defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN */
4923 		numifs = MAXINTERFACES;
4924 
4925 	if (numifs <= 0)
4926 	{
4927 		(void) close(s);
4928 		return;
4929 	}
4930 	ifc.ifc_len = numifs * sizeof (struct ifreq);
4931 	ifc.ifc_buf = xalloc(ifc.ifc_len);
4932 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
4933 	{
4934 		if (tTd(0, 4))
4935 			sm_dprintf("SIOCGIFCONF failed: %s\n",
4936 				   sm_errstring(errno));
4937 		(void) close(s);
4938 		return;
4939 	}
4940 
4941 	/* scan the list of IP address */
4942 	if (tTd(0, 40))
4943 		sm_dprintf("scanning for interface specific names, ifc_len=%d\n",
4944 			ifc.ifc_len);
4945 
4946 	for (i = 0; i < ifc.ifc_len && i >= 0; )
4947 	{
4948 		int af;
4949 		struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
4950 		SOCKADDR *sa = (SOCKADDR *) &ifr->ifr_addr;
4951 #   if NETINET6
4952 		char *addr;
4953 		struct in6_addr ia6;
4954 #   endif /* NETINET6 */
4955 		struct in_addr ia;
4956 #   ifdef SIOCGIFFLAGS
4957 		struct ifreq ifrf;
4958 #   endif /* SIOCGIFFLAGS */
4959 		char ip_addr[256];
4960 #   if NETINET6
4961 		char buf6[INET6_ADDRSTRLEN];
4962 #   endif /* NETINET6 */
4963 
4964 		/*
4965 		**  If we don't have a complete ifr structure,
4966 		**  don't try to use it.
4967 		*/
4968 
4969 		if ((ifc.ifc_len - i) < sizeof *ifr)
4970 			break;
4971 
4972 #   ifdef BSD4_4_SOCKADDR
4973 		if (sa->sa.sa_len > sizeof ifr->ifr_addr)
4974 			i += sizeof ifr->ifr_name + sa->sa.sa_len;
4975 		else
4976 #   endif /* BSD4_4_SOCKADDR */
4977 			i += sizeof *ifr;
4978 
4979 		if (tTd(0, 20))
4980 			sm_dprintf("%s\n", anynet_ntoa(sa));
4981 
4982 		af = ifr->ifr_addr.sa_family;
4983 		if (af != AF_INET
4984 #   if NETINET6
4985 		    && af != AF_INET6
4986 #   endif /* NETINET6 */
4987 		    )
4988 			continue;
4989 
4990 #   ifdef SIOCGIFFLAGS
4991 		memset(&ifrf, '\0', sizeof(struct ifreq));
4992 		(void) sm_strlcpy(ifrf.ifr_name, ifr->ifr_name,
4993 			       sizeof(ifrf.ifr_name));
4994 		(void) ioctl(s, SIOCGIFFLAGS, (char *) &ifrf);
4995 		if (tTd(0, 41))
4996 			sm_dprintf("\tflags: %lx\n",
4997 				(unsigned long) ifrf.ifr_flags);
4998 #    define IFRFREF ifrf
4999 #   else /* SIOCGIFFLAGS */
5000 #    define IFRFREF (*ifr)
5001 #   endif /* SIOCGIFFLAGS */
5002 
5003 		if (!bitset(IFF_UP, IFRFREF.ifr_flags))
5004 			continue;
5005 
5006 		ip_addr[0] = '\0';
5007 
5008 		/* extract IP address from the list*/
5009 		switch (af)
5010 		{
5011 		  case AF_INET:
5012 			ia = sa->sin.sin_addr;
5013 			if (ia.s_addr == INADDR_ANY ||
5014 			    ia.s_addr == INADDR_NONE)
5015 			{
5016 				message("WARNING: interface %s is UP with %s address",
5017 					ifr->ifr_name, inet_ntoa(ia));
5018 				continue;
5019 			}
5020 
5021 			/* save IP address in text from */
5022 			(void) sm_snprintf(ip_addr, sizeof ip_addr, "[%.*s]",
5023 					(int) sizeof ip_addr - 3,
5024 					inet_ntoa(ia));
5025 			break;
5026 
5027 #   if NETINET6
5028 		  case AF_INET6:
5029 #    ifdef __KAME__
5030 			/* convert into proper scoped address */
5031 			if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
5032 			     IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
5033 			    sa->sin6.sin6_scope_id == 0)
5034 			{
5035 				struct in6_addr *ia6p;
5036 
5037 				ia6p = &sa->sin6.sin6_addr;
5038 				sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
5039 							       ((unsigned int)ia6p->s6_addr[2] << 8));
5040 				ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
5041 			}
5042 #    endif /* __KAME__ */
5043 			ia6 = sa->sin6.sin6_addr;
5044 			if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
5045 			{
5046 				addr = anynet_ntop(&ia6, buf6, sizeof buf6);
5047 				message("WARNING: interface %s is UP with %s address",
5048 					ifr->ifr_name,
5049 					addr == NULL ? "(NULL)" : addr);
5050 				continue;
5051 			}
5052 
5053 			/* save IP address in text from */
5054 			addr = anynet_ntop(&ia6, buf6, sizeof buf6);
5055 			if (addr != NULL)
5056 				(void) sm_snprintf(ip_addr, sizeof ip_addr,
5057 						   "[%.*s]",
5058 						   (int) sizeof ip_addr - 3,
5059 						   addr);
5060 			break;
5061 
5062 #   endif /* NETINET6 */
5063 		}
5064 
5065 		if (ip_addr[0] == '\0')
5066 			continue;
5067 
5068 		if (!wordinclass(ip_addr, 'w'))
5069 		{
5070 			setclass('w', ip_addr);
5071 			if (tTd(0, 4))
5072 				sm_dprintf("\ta.k.a.: %s\n", ip_addr);
5073 		}
5074 
5075 		/* skip "loopback" interface "lo" */
5076 		if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
5077 		    bitset(IFF_LOOPBACK, IFRFREF.ifr_flags))
5078 			continue;
5079 
5080 		(void) add_hostnames(sa);
5081 	}
5082 	sm_free(ifc.ifc_buf); /* XXX */
5083 	(void) close(s);
5084 #   undef IFRFREF
5085 #  endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
5086 # endif /* NETINET6 && defined(SIOCGLIFCONF) */
5087 }
5088 /*
5089 **  ISLOOPBACK -- is socket address in the loopback net?
5090 **
5091 **	Parameters:
5092 **		sa -- socket address.
5093 **
5094 **	Returns:
5095 **		true -- is socket address in the loopback net?
5096 **		false -- otherwise
5097 **
5098 */
5099 
5100 bool
5101 isloopback(sa)
5102 	SOCKADDR sa;
5103 {
5104 #if NETINET6
5105 	if (IN6_IS_ADDR_LOOPBACK(&sa.sin6.sin6_addr))
5106 		return true;
5107 #else /* NETINET6 */
5108 	/* XXX how to correctly extract IN_LOOPBACKNET part? */
5109 	if (((ntohl(sa.sin.sin_addr.s_addr) & IN_CLASSA_NET)
5110 	     >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
5111 		return true;
5112 #endif /* NETINET6 */
5113 	return false;
5114 }
5115 /*
5116 **  GET_NUM_PROCS_ONLINE -- return the number of processors currently online
5117 **
5118 **	Parameters:
5119 **		none.
5120 **
5121 **	Returns:
5122 **		The number of processors online.
5123 */
5124 
5125 static int
5126 get_num_procs_online()
5127 {
5128 	int nproc = 0;
5129 
5130 #ifdef USESYSCTL
5131 # if defined(CTL_HW) && defined(HW_NCPU)
5132 	size_t sz;
5133 	int mib[2];
5134 
5135 	mib[0] = CTL_HW;
5136 	mib[1] = HW_NCPU;
5137 	sz = (size_t) sizeof nproc;
5138 	(void) sysctl(mib, 2, &nproc, &sz, NULL, 0);
5139 # endif /* defined(CTL_HW) && defined(HW_NCPU) */
5140 #else /* USESYSCTL */
5141 # ifdef _SC_NPROCESSORS_ONLN
5142 	nproc = (int) sysconf(_SC_NPROCESSORS_ONLN);
5143 # else /* _SC_NPROCESSORS_ONLN */
5144 #  ifdef __hpux
5145 #   include <sys/pstat.h>
5146 	struct pst_dynamic psd;
5147 
5148 	if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) != -1)
5149 		nproc = psd.psd_proc_cnt;
5150 #  endif /* __hpux */
5151 # endif /* _SC_NPROCESSORS_ONLN */
5152 #endif /* USESYSCTL */
5153 
5154 	if (nproc <= 0)
5155 		nproc = 1;
5156 	return nproc;
5157 }
5158 /*
5159 **  SEED_RANDOM -- seed the random number generator
5160 **
5161 **	Parameters:
5162 **		none
5163 **
5164 **	Returns:
5165 **		none
5166 */
5167 
5168 void
5169 seed_random()
5170 {
5171 #if HASSRANDOMDEV
5172 	srandomdev();
5173 #else /* HASSRANDOMDEV */
5174 	long seed;
5175 	struct timeval t;
5176 
5177 	seed = (long) CurrentPid;
5178 	if (gettimeofday(&t, NULL) >= 0)
5179 		seed += t.tv_sec + t.tv_usec;
5180 
5181 # if HASRANDOM
5182 	(void) srandom(seed);
5183 # else /* HASRANDOM */
5184 	(void) srand((unsigned int) seed);
5185 # endif /* HASRANDOM */
5186 #endif /* HASSRANDOMDEV */
5187 }
5188 /*
5189 **  SM_SYSLOG -- syslog wrapper to keep messages under SYSLOG_BUFSIZE
5190 **
5191 **	Parameters:
5192 **		level -- syslog level
5193 **		id -- envelope ID or NULL (NOQUEUE)
5194 **		fmt -- format string
5195 **		arg... -- arguments as implied by fmt.
5196 **
5197 **	Returns:
5198 **		none
5199 */
5200 
5201 /* VARARGS3 */
5202 void
5203 #ifdef __STDC__
5204 sm_syslog(int level, const char *id, const char *fmt, ...)
5205 #else /* __STDC__ */
5206 sm_syslog(level, id, fmt, va_alist)
5207 	int level;
5208 	const char *id;
5209 	const char *fmt;
5210 	va_dcl
5211 #endif /* __STDC__ */
5212 {
5213 	static char *buf = NULL;
5214 	static size_t bufsize;
5215 	char *begin, *end;
5216 	int save_errno;
5217 	int seq = 1;
5218 	int idlen;
5219 	char buf0[MAXLINE];
5220 	char *newstring;
5221 	extern int SyslogPrefixLen;
5222 	SM_VA_LOCAL_DECL
5223 
5224 	save_errno = errno;
5225 	if (id == NULL)
5226 	{
5227 		id = "NOQUEUE";
5228 		idlen = strlen(id) + SyslogPrefixLen;
5229 	}
5230 	else if (strcmp(id, NOQID) == 0)
5231 	{
5232 		id = "";
5233 		idlen = SyslogPrefixLen;
5234 	}
5235 	else
5236 		idlen = strlen(id) + SyslogPrefixLen;
5237 
5238 	if (buf == NULL)
5239 	{
5240 		buf = buf0;
5241 		bufsize = sizeof buf0;
5242 	}
5243 
5244 	for (;;)
5245 	{
5246 		int n;
5247 
5248 		/* print log message into buf */
5249 		SM_VA_START(ap, fmt);
5250 		n = sm_vsnprintf(buf, bufsize, fmt, ap);
5251 		SM_VA_END(ap);
5252 		SM_ASSERT(n > 0);
5253 		if (n < bufsize)
5254 			break;
5255 
5256 		/* String too small, redo with correct size */
5257 		bufsize = n + 1;
5258 		if (buf != buf0)
5259 		{
5260 			sm_free(buf);
5261 			buf = NULL;
5262 		}
5263 		buf = sm_malloc_x(bufsize);
5264 	}
5265 
5266 	/* clean up buf after it has been expanded with args */
5267 	newstring = str2prt(buf);
5268 	if ((strlen(newstring) + idlen + 1) < SYSLOG_BUFSIZE)
5269 	{
5270 #if LOG
5271 		if (*id == '\0')
5272 			syslog(level, "%s", newstring);
5273 		else
5274 			syslog(level, "%s: %s", id, newstring);
5275 #else /* LOG */
5276 		/*XXX should do something more sensible */
5277 		if (*id == '\0')
5278 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s\n",
5279 					     newstring);
5280 		else
5281 			(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5282 					     "%s: %s\n", id, newstring);
5283 #endif /* LOG */
5284 		if (buf == buf0)
5285 			buf = NULL;
5286 		errno = save_errno;
5287 		return;
5288 	}
5289 
5290 /*
5291 **  additional length for splitting: " ..." + 3, where 3 is magic to
5292 **  have some data for the next entry.
5293 */
5294 
5295 #define SL_SPLIT 7
5296 
5297 	begin = newstring;
5298 	idlen += 5;	/* strlen("[999]"), see below */
5299 	while (*begin != '\0' &&
5300 	       (strlen(begin) + idlen) > SYSLOG_BUFSIZE)
5301 	{
5302 		char save;
5303 
5304 		if (seq >= 999)
5305 		{
5306 			/* Too many messages */
5307 			break;
5308 		}
5309 		end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5310 		while (end > begin)
5311 		{
5312 			/* Break on comma or space */
5313 			if (*end == ',' || *end == ' ')
5314 			{
5315 				end++;	  /* Include separator */
5316 				break;
5317 			}
5318 			end--;
5319 		}
5320 		/* No separator, break midstring... */
5321 		if (end == begin)
5322 			end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5323 		save = *end;
5324 		*end = 0;
5325 #if LOG
5326 		syslog(level, "%s[%d]: %s ...", id, seq++, begin);
5327 #else /* LOG */
5328 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5329 				     "%s[%d]: %s ...\n", id, seq++, begin);
5330 #endif /* LOG */
5331 		*end = save;
5332 		begin = end;
5333 	}
5334 	if (seq >= 999)
5335 #if LOG
5336 		syslog(level, "%s[%d]: log terminated, too many parts",
5337 			id, seq);
5338 #else /* LOG */
5339 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5340 			      "%s[%d]: log terminated, too many parts\n", id, seq);
5341 #endif /* LOG */
5342 	else if (*begin != '\0')
5343 #if LOG
5344 		syslog(level, "%s[%d]: %s", id, seq, begin);
5345 #else /* LOG */
5346 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5347 				     "%s[%d]: %s\n", id, seq, begin);
5348 #endif /* LOG */
5349 	if (buf == buf0)
5350 		buf = NULL;
5351 	errno = save_errno;
5352 }
5353 /*
5354 **  HARD_SYSLOG -- call syslog repeatedly until it works
5355 **
5356 **	Needed on HP-UX, which apparently doesn't guarantee that
5357 **	syslog succeeds during interrupt handlers.
5358 */
5359 
5360 #if defined(__hpux) && !defined(HPUX11)
5361 
5362 # define MAXSYSLOGTRIES	100
5363 # undef syslog
5364 # ifdef V4FS
5365 #  define XCNST	const
5366 #  define CAST	(const char *)
5367 # else /* V4FS */
5368 #  define XCNST
5369 #  define CAST
5370 # endif /* V4FS */
5371 
5372 void
5373 # ifdef __STDC__
5374 hard_syslog(int pri, XCNST char *msg, ...)
5375 # else /* __STDC__ */
5376 hard_syslog(pri, msg, va_alist)
5377 	int pri;
5378 	XCNST char *msg;
5379 	va_dcl
5380 # endif /* __STDC__ */
5381 {
5382 	int i;
5383 	char buf[SYSLOG_BUFSIZE];
5384 	SM_VA_LOCAL_DECL
5385 
5386 	SM_VA_START(ap, msg);
5387 	(void) sm_vsnprintf(buf, sizeof buf, msg, ap);
5388 	SM_VA_END(ap);
5389 
5390 	for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, CAST "%s", buf) < 0; )
5391 		continue;
5392 }
5393 
5394 # undef CAST
5395 #endif /* defined(__hpux) && !defined(HPUX11) */
5396 #if NEEDLOCAL_HOSTNAME_LENGTH
5397 /*
5398 **  LOCAL_HOSTNAME_LENGTH
5399 **
5400 **	This is required to get sendmail to compile against BIND 4.9.x
5401 **	on Ultrix.
5402 **
5403 **	Unfortunately, a Compaq Y2K patch kit provides it without
5404 **	bumping __RES in /usr/include/resolv.h so we can't automatically
5405 **	figure out whether it is needed.
5406 */
5407 
5408 int
5409 local_hostname_length(hostname)
5410 	char *hostname;
5411 {
5412 	size_t len_host, len_domain;
5413 
5414 	if (!*_res.defdname)
5415 		res_init();
5416 	len_host = strlen(hostname);
5417 	len_domain = strlen(_res.defdname);
5418 	if (len_host > len_domain &&
5419 	    (sm_strcasecmp(hostname + len_host - len_domain,
5420 			_res.defdname) == 0) &&
5421 	    hostname[len_host - len_domain - 1] == '.')
5422 		return len_host - len_domain - 1;
5423 	else
5424 		return 0;
5425 }
5426 #endif /* NEEDLOCAL_HOSTNAME_LENGTH */
5427 
5428 #if NEEDLINK
5429 /*
5430 **  LINK -- clone a file
5431 **
5432 **	Some OS's lacks link() and hard links.  Since sendmail is using
5433 **	link() as an efficient way to clone files, this implementation
5434 **	will simply do a file copy.
5435 **
5436 **	NOTE: This link() replacement is not a generic replacement as it
5437 **	does not handle all of the semantics of the real link(2).
5438 **
5439 **	Parameters:
5440 **		source -- pathname of existing file.
5441 **		target -- pathname of link (clone) to be created.
5442 **
5443 **	Returns:
5444 **		0 -- success.
5445 **		-1 -- failure, see errno for details.
5446 */
5447 
5448 int
5449 link(source, target)
5450 	const char *source;
5451 	const char *target;
5452 {
5453 	int save_errno;
5454 	int sff;
5455 	int src = -1, dst = -1;
5456 	ssize_t readlen;
5457 	ssize_t writelen;
5458 	char buf[BUFSIZ];
5459 	struct stat st;
5460 
5461 	sff = SFF_REGONLY|SFF_OPENASROOT;
5462 	if (DontLockReadFiles)
5463 		sff |= SFF_NOLOCK;
5464 
5465 	/* Open the original file */
5466 	src = safeopen((char *)source, O_RDONLY, 0, sff);
5467 	if (src < 0)
5468 		goto fail;
5469 
5470 	/* Obtain the size and the mode */
5471 	if (fstat(src, &st) < 0)
5472 		goto fail;
5473 
5474 	/* Create the duplicate copy */
5475 	sff &= ~SFF_NOLOCK;
5476 	sff |= SFF_CREAT;
5477 	dst = safeopen((char *)target, O_CREAT|O_EXCL|O_WRONLY,
5478 		       st.st_mode, sff);
5479 	if (dst < 0)
5480 		goto fail;
5481 
5482 	/* Copy all of the bytes one buffer at a time */
5483 	while ((readlen = read(src, &buf, sizeof(buf))) > 0)
5484 	{
5485 		ssize_t left = readlen;
5486 		char *p = buf;
5487 
5488 		while (left > 0 &&
5489 		       (writelen = write(dst, p, (size_t) left)) >= 0)
5490 		{
5491 			left -= writelen;
5492 			p += writelen;
5493 		}
5494 		if (writelen < 0)
5495 			break;
5496 	}
5497 
5498 	/* Any trouble reading? */
5499 	if (readlen < 0 || writelen < 0)
5500 		goto fail;
5501 
5502 	/* Close the input file */
5503 	if (close(src) < 0)
5504 	{
5505 		src = -1;
5506 		goto fail;
5507 	}
5508 	src = -1;
5509 
5510 	/* Close the output file */
5511 	if (close(dst) < 0)
5512 	{
5513 		/* don't set dst = -1 here so we unlink the file */
5514 		goto fail;
5515 	}
5516 
5517 	/* Success */
5518 	return 0;
5519 
5520  fail:
5521 	save_errno = errno;
5522 	if (src >= 0)
5523 		(void) close(src);
5524 	if (dst >= 0)
5525 	{
5526 		(void) unlink(target);
5527 		(void) close(dst);
5528 	}
5529 	errno = save_errno;
5530 	return -1;
5531 }
5532 #endif /* NEEDLINK */
5533 
5534 /*
5535 **  Compile-Time options
5536 */
5537 
5538 char	*CompileOptions[] =
5539 {
5540 #if NAMED_BIND
5541 # if DNSMAP
5542 	"DNSMAP",
5543 # endif /* DNSMAP */
5544 #endif /* NAMED_BIND */
5545 #if EGD
5546 	"EGD",
5547 #endif /* EGD */
5548 #if HESIOD
5549 	"HESIOD",
5550 #endif /* HESIOD */
5551 #if HES_GETMAILHOST
5552 	"HES_GETMAILHOST",
5553 #endif /* HES_GETMAILHOST */
5554 #if LDAPMAP
5555 	"LDAPMAP",
5556 #endif /* LDAPMAP */
5557 #if LOG
5558 	"LOG",
5559 #endif /* LOG */
5560 #if MAP_NSD
5561 	"MAP_NSD",
5562 #endif /* MAP_NSD */
5563 #if MAP_REGEX
5564 	"MAP_REGEX",
5565 #endif /* MAP_REGEX */
5566 #if MATCHGECOS
5567 	"MATCHGECOS",
5568 #endif /* MATCHGECOS */
5569 #if MILTER
5570 	"MILTER",
5571 #endif /* MILTER */
5572 #if MIME7TO8
5573 	"MIME7TO8",
5574 #endif /* MIME7TO8 */
5575 #if MIME8TO7
5576 	"MIME8TO7",
5577 #endif /* MIME8TO7 */
5578 #if NAMED_BIND
5579 	"NAMED_BIND",
5580 #endif /* NAMED_BIND */
5581 #if NDBM
5582 	"NDBM",
5583 #endif /* NDBM */
5584 #if NETINET
5585 	"NETINET",
5586 #endif /* NETINET */
5587 #if NETINET6
5588 	"NETINET6",
5589 #endif /* NETINET6 */
5590 #if NETINFO
5591 	"NETINFO",
5592 #endif /* NETINFO */
5593 #if NETISO
5594 	"NETISO",
5595 #endif /* NETISO */
5596 #if NETNS
5597 	"NETNS",
5598 #endif /* NETNS */
5599 #if NETUNIX
5600 	"NETUNIX",
5601 #endif /* NETUNIX */
5602 #if NETX25
5603 	"NETX25",
5604 #endif /* NETX25 */
5605 #if NEWDB
5606 	"NEWDB",
5607 #endif /* NEWDB */
5608 #if NIS
5609 	"NIS",
5610 #endif /* NIS */
5611 #if NISPLUS
5612 	"NISPLUS",
5613 #endif /* NISPLUS */
5614 #if NO_DH
5615 	"NO_DH",
5616 #endif /* NO_DH */
5617 #if PH_MAP
5618 	"PH_MAP",
5619 #endif /* PH_MAP */
5620 #ifdef PICKY_HELO_CHECK
5621 	"PICKY_HELO_CHECK",
5622 #endif /* PICKY_HELO_CHECK */
5623 #if PIPELINING
5624 	"PIPELINING",
5625 #endif /* PIPELINING */
5626 #if SASL
5627 # if SASL >= 20000
5628 	"SASLv2",
5629 # else /* SASL >= 20000 */
5630 	"SASL",
5631 # endif /* SASL >= 20000 */
5632 #endif /* SASL */
5633 #if SCANF
5634 	"SCANF",
5635 #endif /* SCANF */
5636 #if SMTPDEBUG
5637 	"SMTPDEBUG",
5638 #endif /* SMTPDEBUG */
5639 #if STARTTLS
5640 	"STARTTLS",
5641 #endif /* STARTTLS */
5642 #if SUID_ROOT_FILES_OK
5643 	"SUID_ROOT_FILES_OK",
5644 #endif /* SUID_ROOT_FILES_OK */
5645 #if TCPWRAPPERS
5646 	"TCPWRAPPERS",
5647 #endif /* TCPWRAPPERS */
5648 #if TLS_NO_RSA
5649 	"TLS_NO_RSA",
5650 #endif /* TLS_NO_RSA */
5651 #if TLS_VRFY_PER_CTX
5652 	"TLS_VRFY_PER_CTX",
5653 #endif /* TLS_VRFY_PER_CTX */
5654 #if USERDB
5655 	"USERDB",
5656 #endif /* USERDB */
5657 #if USE_LDAP_INIT
5658 	"USE_LDAP_INIT",
5659 #endif /* USE_LDAP_INIT */
5660 #if XDEBUG
5661 	"XDEBUG",
5662 #endif /* XDEBUG */
5663 #if XLA
5664 	"XLA",
5665 #endif /* XLA */
5666 	NULL
5667 };
5668 
5669 
5670 /*
5671 **  OS compile options.
5672 */
5673 
5674 char	*OsCompileOptions[] =
5675 {
5676 #if ADDRCONFIG_IS_BROKEN
5677 	"ADDRCONFIG_IS_BROKEN",
5678 #endif /* ADDRCONFIG_IS_BROKEN */
5679 #ifdef AUTO_NETINFO_HOSTS
5680 	"AUTO_NETINFO_HOSTS",
5681 #endif /* AUTO_NETINFO_HOSTS */
5682 #ifdef AUTO_NIS_ALIASES
5683 	"AUTO_NIS_ALIASES",
5684 #endif /* AUTO_NIS_ALIASES */
5685 #if BROKEN_RES_SEARCH
5686 	"BROKEN_RES_SEARCH",
5687 #endif /* BROKEN_RES_SEARCH */
5688 #ifdef BSD4_4_SOCKADDR
5689 	"BSD4_4_SOCKADDR",
5690 #endif /* BSD4_4_SOCKADDR */
5691 #if BOGUS_O_EXCL
5692 	"BOGUS_O_EXCL",
5693 #endif /* BOGUS_O_EXCL */
5694 #if DEC_OSF_BROKEN_GETPWENT
5695 	"DEC_OSF_BROKEN_GETPWENT",
5696 #endif /* DEC_OSF_BROKEN_GETPWENT */
5697 #if FAST_PID_RECYCLE
5698 	"FAST_PID_RECYCLE",
5699 #endif /* FAST_PID_RECYCLE */
5700 #if HASFCHOWN
5701 	"HASFCHOWN",
5702 #endif /* HASFCHOWN */
5703 #if HASFCHMOD
5704 	"HASFCHMOD",
5705 #endif /* HASFCHMOD */
5706 #if HASFLOCK
5707 	"HASFLOCK",
5708 #endif /* HASFLOCK */
5709 #if HASGETDTABLESIZE
5710 	"HASGETDTABLESIZE",
5711 #endif /* HASGETDTABLESIZE */
5712 #if HASGETUSERSHELL
5713 	"HASGETUSERSHELL",
5714 #endif /* HASGETUSERSHELL */
5715 #if HASINITGROUPS
5716 	"HASINITGROUPS",
5717 #endif /* HASINITGROUPS */
5718 #if HASLSTAT
5719 	"HASLSTAT",
5720 #endif /* HASLSTAT */
5721 #if HASNICE
5722 	"HASNICE",
5723 #endif /* HASNICE */
5724 #if HASRANDOM
5725 	"HASRANDOM",
5726 #endif /* HASRANDOM */
5727 #if HASRRESVPORT
5728 	"HASRRESVPORT",
5729 #endif /* HASRRESVPORT */
5730 #if HASSETEGID
5731 	"HASSETEGID",
5732 #endif /* HASSETEGID */
5733 #if HASSETLOGIN
5734 	"HASSETLOGIN",
5735 #endif /* HASSETLOGIN */
5736 #if HASSETREGID
5737 	"HASSETREGID",
5738 #endif /* HASSETREGID */
5739 #if HASSETRESGID
5740 	"HASSETRESGID",
5741 #endif /* HASSETRESGID */
5742 #if HASSETREUID
5743 	"HASSETREUID",
5744 #endif /* HASSETREUID */
5745 #if HASSETRLIMIT
5746 	"HASSETRLIMIT",
5747 #endif /* HASSETRLIMIT */
5748 #if HASSETSID
5749 	"HASSETSID",
5750 #endif /* HASSETSID */
5751 #if HASSETUSERCONTEXT
5752 	"HASSETUSERCONTEXT",
5753 #endif /* HASSETUSERCONTEXT */
5754 #if HASSETVBUF
5755 	"HASSETVBUF",
5756 #endif /* HASSETVBUF */
5757 #if HAS_ST_GEN
5758 	"HAS_ST_GEN",
5759 #endif /* HAS_ST_GEN */
5760 #if HASSRANDOMDEV
5761 	"HASSRANDOMDEV",
5762 #endif /* HASSRANDOMDEV */
5763 #if HASURANDOMDEV
5764 	"HASURANDOMDEV",
5765 #endif /* HASURANDOMDEV */
5766 #if HASSTRERROR
5767 	"HASSTRERROR",
5768 #endif /* HASSTRERROR */
5769 #if HASULIMIT
5770 	"HASULIMIT",
5771 #endif /* HASULIMIT */
5772 #if HASUNAME
5773 	"HASUNAME",
5774 #endif /* HASUNAME */
5775 #if HASUNSETENV
5776 	"HASUNSETENV",
5777 #endif /* HASUNSETENV */
5778 #if HASWAITPID
5779 	"HASWAITPID",
5780 #endif /* HASWAITPID */
5781 #if IDENTPROTO
5782 	"IDENTPROTO",
5783 #endif /* IDENTPROTO */
5784 #if IP_SRCROUTE
5785 	"IP_SRCROUTE",
5786 #endif /* IP_SRCROUTE */
5787 #if O_EXLOCK && HASFLOCK && !BOGUS_O_EXCL
5788 	"LOCK_ON_OPEN",
5789 #endif /* O_EXLOCK && HASFLOCK && !BOGUS_O_EXCL */
5790 #if NEEDFSYNC
5791 	"NEEDFSYNC",
5792 #endif /* NEEDFSYNC */
5793 #if NEEDLINK
5794 	"NEEDLINK",
5795 #endif /* NEEDLINK */
5796 #if NEEDLOCAL_HOSTNAME_LENGTH
5797 	"NEEDLOCAL_HOSTNAME_LENGTH",
5798 #endif /* NEEDLOCAL_HOSTNAME_LENGTH */
5799 #if NEEDSGETIPNODE
5800 	"NEEDSGETIPNODE",
5801 #endif /* NEEDSGETIPNODE */
5802 #if NEEDSTRSTR
5803 	"NEEDSTRSTR",
5804 #endif /* NEEDSTRSTR */
5805 #if NEEDSTRTOL
5806 	"NEEDSTRTOL",
5807 #endif /* NEEDSTRTOL */
5808 #ifdef NO_GETSERVBYNAME
5809 	"NO_GETSERVBYNAME",
5810 #endif /* NO_GETSERVBYNAME */
5811 #if NOFTRUNCATE
5812 	"NOFTRUNCATE",
5813 #endif /* NOFTRUNCATE */
5814 #if REQUIRES_DIR_FSYNC
5815 	"REQUIRES_DIR_FSYNC",
5816 #endif /* REQUIRES_DIR_FSYNC */
5817 #if RLIMIT_NEEDS_SYS_TIME_H
5818 	"RLIMIT_NEEDS_SYS_TIME_H",
5819 #endif /* RLIMIT_NEEDS_SYS_TIME_H */
5820 #if SAFENFSPATHCONF
5821 	"SAFENFSPATHCONF",
5822 #endif /* SAFENFSPATHCONF */
5823 #if SECUREWARE
5824 	"SECUREWARE",
5825 #endif /* SECUREWARE */
5826 #if SHARE_V1
5827 	"SHARE_V1",
5828 #endif /* SHARE_V1 */
5829 #if SIOCGIFCONF_IS_BROKEN
5830 	"SIOCGIFCONF_IS_BROKEN",
5831 #endif /* SIOCGIFCONF_IS_BROKEN */
5832 #if SIOCGIFNUM_IS_BROKEN
5833 	"SIOCGIFNUM_IS_BROKEN",
5834 #endif /* SIOCGIFNUM_IS_BROKEN */
5835 #if SNPRINTF_IS_BROKEN
5836 	"SNPRINTF_IS_BROKEN",
5837 #endif /* SNPRINTF_IS_BROKEN */
5838 #if SO_REUSEADDR_IS_BROKEN
5839 	"SO_REUSEADDR_IS_BROKEN",
5840 #endif /* SO_REUSEADDR_IS_BROKEN */
5841 #if SYS5SETPGRP
5842 	"SYS5SETPGRP",
5843 #endif /* SYS5SETPGRP */
5844 #if SYSTEM5
5845 	"SYSTEM5",
5846 #endif /* SYSTEM5 */
5847 #if USE_DOUBLE_FORK
5848 	"USE_DOUBLE_FORK",
5849 #endif /* USE_DOUBLE_FORK */
5850 #if USE_ENVIRON
5851 	"USE_ENVIRON",
5852 #endif /* USE_ENVIRON */
5853 #if USE_SA_SIGACTION
5854 	"USE_SA_SIGACTION",
5855 #endif /* USE_SA_SIGACTION */
5856 #if USE_SIGLONGJMP
5857 	"USE_SIGLONGJMP",
5858 #endif /* USE_SIGLONGJMP */
5859 #if USEGETCONFATTR
5860 	"USEGETCONFATTR",
5861 #endif /* USEGETCONFATTR */
5862 #if USESETEUID
5863 	"USESETEUID",
5864 #endif /* USESETEUID */
5865 #ifdef USESYSCTL
5866 	"USESYSCTL",
5867 #endif /* USESYSCTL */
5868 #if USING_NETSCAPE_LDAP
5869 	"USING_NETSCAPE_LDAP",
5870 #endif /* USING_NETSCAPE_LDAP */
5871 #ifdef WAITUNION
5872 	"WAITUNION",
5873 #endif /* WAITUNION */
5874 	NULL
5875 };
5876 
5877 /*
5878 **  FFR compile options.
5879 */
5880 
5881 char	*FFRCompileOptions[] =
5882 {
5883 #if _FFR_ADAPTIVE_EOL
5884 	"_FFR_ADAPTIVE_EOL",
5885 #endif /* _FFR_ADAPTIVE_EOL */
5886 #if _FFR_ALLOW_SASLINFO
5887 	"_FFR_ALLOW_SASLINFO",
5888 #endif /* _FFR_ALLOW_SASLINFO */
5889 #if _FFR_ALLOW_S0_ERROR_4XX
5890 	"_FFR_ALLOW_S0_ERROR_4XX",
5891 #endif /* _FFR_ALLOW_S0_ERROR_4XX */
5892 #if _FFR_BESTMX_BETTER_TRUNCATION
5893 	"_FFR_BESTMX_BETTER_TRUNCATION",
5894 #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
5895 #if _FFR_CACHE_LPC
5896 /* Christophe Wolfhugel of France Telecom Oleane */
5897 	"_FFR_CACHE_LPC",
5898 #endif /* _FFR_CACHE_LPC */
5899 #if _FFR_CATCH_BROKEN_MTAS
5900 	"_FFR_CATCH_BROKEN_MTAS",
5901 #endif /* _FFR_CATCH_BROKEN_MTAS */
5902 #if _FFR_CATCH_LONG_STRINGS
5903 	"_FFR_CATCH_LONG_STRINGS",
5904 #endif /* _FFR_CATCH_LONG_STRINGS */
5905 #if _FFR_CHECK_EOM
5906 	"_FFR_CHECK_EOM",
5907 #endif /* _FFR_CHECK_EOM */
5908 #if _FFR_CHK_QUEUE
5909 	"_FFR_CHK_QUEUE",
5910 #endif /* _FFR_CHK_QUEUE */
5911 #if _FFR_CONTROL_MSTAT
5912 	"_FFR_CONTROL_MSTAT",
5913 #endif /* _FFR_CONTROL_MSTAT */
5914 #if _FFR_DAEMON_NETUNIX
5915 	"_FFR_DAEMON_NETUNIX",
5916 #endif /* _FFR_DAEMON_NETUNIX */
5917 #if _FFR_DEAL_WITH_ERROR_SSL
5918 	"_FFR_DEAL_WITH_ERROR_SSL",
5919 #endif /* _FFR_DEAL_WITH_ERROR_SSL */
5920 #if _FFR_DEPRECATE_MAILER_FLAG_I
5921 	"_FFR_DEPRECATE_MAILER_FLAG_I",
5922 #endif /* _FFR_DEPRECATE_MAILER_FLAG_I */
5923 #if _FFR_DIGUNIX_SAFECHOWN
5924 /* Problem noted by Anne Bennett of Concordia University */
5925 	"_FFR_DIGUNIX_SAFECHOWN",
5926 #endif /* _FFR_DIGUNIX_SAFECHOWN */
5927 #if _FFR_DNSMAP_ALIASABLE
5928 /* Don Lewis of TDK */
5929 	"_FFR_DNSMAP_ALIASABLE",
5930 #endif /* _FFR_DNSMAP_ALIASABLE */
5931 #if _FFR_DNSMAP_BASE
5932 	"_FFR_DNSMAP_BASE",
5933 #endif /* _FFR_DNSMAP_BASE */
5934 #if _FFR_DNSMAP_MULTI
5935 	"_FFR_DNSMAP_MULTI",
5936 # if _FFR_DNSMAP_MULTILIMIT
5937 	"_FFR_DNSMAP_MULTILIMIT",
5938 # endif /* _FFR_DNSMAP_MULTILIMIT */
5939 #endif /* _FFR_DNSMAP_MULTI */
5940 #if _FFR_DONTLOCKFILESFORREAD_OPTION
5941 	"_FFR_DONTLOCKFILESFORREAD_OPTION",
5942 #endif /* _FFR_DONTLOCKFILESFORREAD_OPTION */
5943 # if _FFR_DONT_STOP_LOOKING
5944 /* Noted by Neil Rickert of Northern Illinois University */
5945 	"_FFR_DONT_STOP_LOOKING",
5946 # endif /* _FFR_DONT_STOP_LOOKING */
5947 #if _FFR_DOTTED_USERNAMES
5948 	"_FFR_DOTTED_USERNAMES",
5949 #endif /* _FFR_DOTTED_USERNAMES */
5950 #if _FFR_DROP_TRUSTUSER_WARNING
5951 	"_FFR_DROP_TRUSTUSER_WARNING",
5952 #endif /* _FFR_DROP_TRUSTUSER_WARNING */
5953 #if _FFR_FIX_DASHT
5954 	"_FFR_FIX_DASHT",
5955 #endif /* _FFR_FIX_DASHT */
5956 #if _FFR_FORWARD_SYSERR
5957 	"_FFR_FORWARD_SYSERR",
5958 #endif /* _FFR_FORWARD_SYSERR */
5959 #if _FFR_GEN_ORCPT
5960 	"_FFR_GEN_ORCPT",
5961 #endif /* _FFR_GEN_ORCPT */
5962 #if _FFR_GROUPREADABLEAUTHINFOFILE
5963 	"_FFR_GROUPREADABLEAUTHINFOFILE",
5964 #endif /* _FFR_GROUPREADABLEAUTHINFOFILE */
5965 #if _FFR_HANDLE_ISO8859_GECOS
5966 /* Peter Eriksson of Linkopings universitet */
5967 	"_FFR_HANDLE_ISO8859_GECOS",
5968 #endif /* _FFR_HANDLE_ISO8859_GECOS */
5969 #if _FFR_HDR_TYPE
5970 	"_FFR_HDR_TYPE",
5971 #endif /* _FFR_HDR_TYPE */
5972 #if _FFR_HPUX_NSSWITCH
5973 	"_FFR_HPUX_NSSWITCH",
5974 #endif /* _FFR_HPUX_NSSWITCH */
5975 #if _FFR_IGNORE_EXT_ON_HELO
5976 	"_FFR_IGNORE_EXT_ON_HELO",
5977 #endif /* _FFR_IGNORE_EXT_ON_HELO */
5978 #if _FFR_LDAP_RECURSION
5979 /* Andrew Baucom */
5980 	"_FFR_LDAP_RECURSION",
5981 #endif /* _FFR_LDAP_RECURSION */
5982 #if _FFR_LDAP_SETVERSION
5983 	"_FFR_LDAP_SETVERSION",
5984 #endif /* _FFR_LDAP_SETVERSION */
5985 #if _FFR_LDAP_URI
5986 	"_FFR_LDAP_URI",
5987 #endif /* _FFR_LDAP_URI */
5988 #if _FFR_MAX_FORWARD_ENTRIES
5989 /* Randall S. Winchester of the University of Maryland */
5990 	"_FFR_MAX_FORWARD_ENTRIES",
5991 #endif /* _FFR_MAX_FORWARD_ENTRIES */
5992 #if MILTER
5993 # if _FFR_MILTER_421
5994 	"_FFR_MILTER_421",
5995 # endif /* _FFR_MILTER_421 */
5996 # if  _FFR_MILTER_PERDAEMON
5997 	"_FFR_MILTER_PERDAEMON",
5998 # endif /* _FFR_MILTER_PERDAEMON */
5999 #endif /* MILTER */
6000 #if _FFR_NODELAYDSN_ON_HOLD
6001 /* Steven Pitzl */
6002 	"_FFR_NODELAYDSN_ON_HOLD",
6003 #endif /* _FFR_NODELAYDSN_ON_HOLD */
6004 #if _FFR_NO_PIPE
6005 	"_FFR_NO_PIPE",
6006 #endif /* _FFR_NO_PIPE */
6007 #if _FFR_QUARANTINE
6008 	"_FFR_QUARANTINE",
6009 #endif /* _FFR_QUARANTINE */
6010 #if _FFR_QUEUEDELAY
6011 	"_FFR_QUEUEDELAY",
6012 #endif /* _FFR_QUEUEDELAY */
6013 #if _FFR_QUEUE_GROUP_SORTORDER
6014 /* XXX: Still need to actually use qgrp->qg_sortorder */
6015 	"_FFR_QUEUE_GROUP_SORTORDER",
6016 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
6017 #if _FFR_QUEUE_MACRO
6018 	"_FFR_QUEUE_MACRO",
6019 #endif /* _FFR_QUEUE_MACRO */
6020 #if _FFR_QUEUE_RUN_PARANOIA
6021 	"_FFR_QUEUE_RUN_PARANOIA",
6022 #endif /* _FFR_QUEUE_RUN_PARANOIA */
6023 #if _FFR_QUEUE_SCHED_DBG
6024 	"_FFR_QUEUE_SCHED_DBG",
6025 #endif /* _FFR_QUEUE_SCHED_DBG */
6026 #if _FFR_REDIRECTEMPTY
6027 	"_FFR_REDIRECTEMPTY",
6028 #endif /* _FFR_REDIRECTEMPTY */
6029 #if _FFR_REJECT_LOG
6030 	"_FFR_REJECT_LOG",
6031 #endif /* _FFR_REJECT_LOG */
6032 #if _FFR_REQ_DIR_FSYNC_OPT
6033 	"_FFR_REQ_DIR_FSYNC_OPT",
6034 #endif /* _FFR_REQ_DIR_FSYNC_OPT */
6035 #if _FFR_RESET_MACRO_GLOBALS
6036 	"_FFR_RESET_MACRO_GLOBALS",
6037 #endif /* _FFR_RESET_MACRO_GLOBALS */
6038 #if _FFR_RESPOND_ALL
6039 	/* in vacation */
6040 	"_FFR_RESPOND_ALL",
6041 #endif /* _FFR_RESPOND_ALL */
6042 #if _FFR_RHS
6043 	"_FFR_RHS",
6044 #endif /* _FFR_RHS */
6045 #if _FFR_SASL_OPT_M
6046 	"_FFR_SASL_OPT_M",
6047 #endif /* _FFR_SASL_OPT_M */
6048 #if _FFR_SELECT_SHM
6049 	"_FFR_SELECT_SHM",
6050 #endif /* _FFR_SELECT_SHM */
6051 #if _FFR_SHM_STATUS
6052 	"_FFR_SHM_STATUS",
6053 #endif /* _FFR_SHM_STATUS */
6054 #if _FFR_SMFI_OPENSOCKET
6055 	"_FFR_SMFI_OPENSOCKET",
6056 #endif /* _FFR_SMFI_OPENSOCKET */
6057 #if _FFR_SMTP_SSL
6058 	"_FFR_SMTP_SSL",
6059 #endif /* _FFR_SMTP_SSL */
6060 #if _FFR_SOFT_BOUNCE
6061 	"_FFR_SOFT_BOUNCE",
6062 #endif /* _FFR_SOFT_BOUNCE */
6063 #if _FFR_SPT_ALIGN
6064 /* Chris Adams of HiWAAY Informations Services */
6065 	"_FFR_SPT_ALIGN",
6066 #endif /* _FFR_SPT_ALIGN */
6067 #if _FFR_STRIPBACKSL
6068 	"_FFR_STRIPBACKSL",
6069 #endif /* _FFR_STRIPBACKSL */
6070 #if _FFR_TIMERS
6071 	"_FFR_TIMERS",
6072 #endif /* _FFR_TIMERS */
6073 #if _FFR_TLS_1
6074 	"_FFR_TLS_1",
6075 #endif /* _FFR_TLS_1 */
6076 #if _FFR_TRUSTED_QF
6077 	"_FFR_TRUSTED_QF",
6078 #endif /* _FFR_TRUSTED_QF */
6079 #if _FFR_USE_SETLOGIN
6080 /* Peter Philipp */
6081 	"_FFR_USE_SETLOGIN",
6082 #endif /* _FFR_USE_SETLOGIN */
6083 	NULL
6084 };
6085 
6086