2 * Copyright (c) 1998-2013 Proofpoint, Inc. and its suppliers.
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.
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.
16 SM_RCSID("@(#)$Id: conf.c,v 8.1192 2014-01-27 18:23:21 ca Exp $")
18 #include <sm/sendmail.h>
19 #include <sendmail/pathnames.h>
29 /* for the IPv6 device lookup */
30 # define _SOCKADDR_LEN
32 # endif /* NETINET6 */
35 # include <sys/ioctl.h>
36 # include <sys/param.h>
39 #if NETINET || NETINET6
40 # include <arpa/inet.h>
41 #endif /* NETINET || NETINET6 */
42 #if HASULIMIT && defined(HPUX11)
44 #endif /* HASULIMIT && defined(HPUX11) */
46 static void setupmaps __P((void));
47 static void setupmailers __P((void));
48 static void setupqueues __P((void));
49 static int get_num_procs_online __P((void));
50 static int add_hostnames __P((SOCKADDR *));
52 #if NETINET6 && NEEDSGETIPNODE
53 static struct hostent *sm_getipnodebyname __P((const char *, int, int, int *));
54 static struct hostent *sm_getipnodebyaddr __P((const void *, size_t, int, int *));
55 #else /* NETINET6 && NEEDSGETIPNODE */
56 #define sm_getipnodebyname getipnodebyname
57 #define sm_getipnodebyaddr getipnodebyaddr
58 #endif /* NETINET6 && NEEDSGETIPNODE */
62 ** CONF.C -- Sendmail Configuration Tables.
64 ** Defines the configuration of this installation.
66 ** Configuration Variables:
67 ** HdrInfo -- a table describing well-known header fields.
68 ** Each entry has the field name and some flags,
69 ** which are described in sendmail.h.
72 ** I have tried to put almost all the reasonable
73 ** configuration information into the configuration
74 ** file read at runtime. My intent is that anything
75 ** here is a function of the version of UNIX you
76 ** are running, or is really static -- for example
77 ** the headers are a superset of widely used
78 ** protocols. If you find yourself playing with
79 ** this file too much, you may be making a mistake!
85 ** Final (null) entry contains the flags used for any other field.
87 ** Not all of these are actually handled specially by sendmail
88 ** at this time. They are included as placeholders, to let
89 ** you know that "someday" I intend to have sendmail do
90 ** something with them.
93 struct hdrinfo HdrInfo[] =
95 /* originator fields, most to least significant */
96 { "resent-sender", H_FROM|H_RESENT, NULL },
97 { "resent-from", H_FROM|H_RESENT, NULL },
98 { "resent-reply-to", H_FROM|H_RESENT, NULL },
99 { "sender", H_FROM, NULL },
100 { "from", H_FROM, NULL },
101 { "reply-to", H_FROM, NULL },
102 { "errors-to", H_FROM|H_ERRORSTO, NULL },
103 { "full-name", H_ACHECK, NULL },
104 { "return-receipt-to", H_RECEIPTTO, NULL },
105 { "delivery-receipt-to", H_RECEIPTTO, NULL },
106 { "disposition-notification-to", H_FROM, NULL },
108 /* destination fields */
109 { "to", H_RCPT, NULL },
110 { "resent-to", H_RCPT|H_RESENT, NULL },
111 { "cc", H_RCPT, NULL },
112 { "resent-cc", H_RCPT|H_RESENT, NULL },
113 { "bcc", H_RCPT|H_BCC, NULL },
114 { "resent-bcc", H_RCPT|H_BCC|H_RESENT, NULL },
115 { "apparently-to", H_RCPT, NULL },
117 /* message identification and control */
118 { "message-id", 0, NULL },
119 { "resent-message-id", H_RESENT, NULL },
120 { "message", H_EOH, NULL },
121 { "text", H_EOH, NULL },
125 { "resent-date", H_RESENT, NULL },
128 { "received", H_TRACE|H_FORCE, NULL },
129 { "x400-received", H_TRACE|H_FORCE, NULL },
130 { "via", H_TRACE|H_FORCE, NULL },
131 { "mail-from", H_TRACE|H_FORCE, NULL },
133 /* miscellaneous fields */
134 { "comments", H_FORCE|H_ENCODABLE, NULL },
135 { "return-path", H_FORCE|H_ACHECK|H_BINDLATE, NULL },
136 { "content-transfer-encoding", H_CTE, NULL },
137 { "content-type", H_CTYPE, NULL },
138 { "content-length", H_ACHECK, NULL },
139 { "subject", H_ENCODABLE, NULL },
140 { "x-authentication-warning", H_FORCE, NULL },
151 struct prival PrivacyValues[] =
153 { "public", PRIV_PUBLIC },
154 { "needmailhelo", PRIV_NEEDMAILHELO },
155 { "needexpnhelo", PRIV_NEEDEXPNHELO },
156 { "needvrfyhelo", PRIV_NEEDVRFYHELO },
157 { "noexpn", PRIV_NOEXPN },
158 { "novrfy", PRIV_NOVRFY },
159 { "restrictexpand", PRIV_RESTRICTEXPAND },
160 { "restrictmailq", PRIV_RESTRICTMAILQ },
161 { "restrictqrun", PRIV_RESTRICTQRUN },
162 { "noetrn", PRIV_NOETRN },
163 { "noverb", PRIV_NOVERB },
164 { "authwarnings", PRIV_AUTHWARNINGS },
165 { "noreceipts", PRIV_NORECEIPTS },
166 { "nobodyreturn", PRIV_NOBODYRETN },
167 { "goaway", PRIV_GOAWAY },
168 { "noactualrecipient", PRIV_NOACTUALRECIPIENT },
173 ** DontBlameSendmail values
176 struct dbsval DontBlameSendmailValues[] =
178 { "safe", DBS_SAFE },
179 { "assumesafechown", DBS_ASSUMESAFECHOWN },
180 { "groupwritabledirpathsafe", DBS_GROUPWRITABLEDIRPATHSAFE },
181 { "groupwritableforwardfilesafe",
182 DBS_GROUPWRITABLEFORWARDFILESAFE },
183 { "groupwritableincludefilesafe",
184 DBS_GROUPWRITABLEINCLUDEFILESAFE },
185 { "groupwritablealiasfile", DBS_GROUPWRITABLEALIASFILE },
186 { "worldwritablealiasfile", DBS_WORLDWRITABLEALIASFILE },
187 { "forwardfileinunsafedirpath", DBS_FORWARDFILEINUNSAFEDIRPATH },
188 { "includefileinunsafedirpath", DBS_INCLUDEFILEINUNSAFEDIRPATH },
189 { "mapinunsafedirpath", DBS_MAPINUNSAFEDIRPATH },
190 { "linkedaliasfileinwritabledir",
191 DBS_LINKEDALIASFILEINWRITABLEDIR },
192 { "linkedclassfileinwritabledir",
193 DBS_LINKEDCLASSFILEINWRITABLEDIR },
194 { "linkedforwardfileinwritabledir",
195 DBS_LINKEDFORWARDFILEINWRITABLEDIR },
196 { "linkedincludefileinwritabledir",
197 DBS_LINKEDINCLUDEFILEINWRITABLEDIR },
198 { "linkedmapinwritabledir", DBS_LINKEDMAPINWRITABLEDIR },
199 { "linkedserviceswitchfileinwritabledir",
200 DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR },
201 { "filedeliverytohardlink", DBS_FILEDELIVERYTOHARDLINK },
202 { "filedeliverytosymlink", DBS_FILEDELIVERYTOSYMLINK },
203 { "writemaptohardlink", DBS_WRITEMAPTOHARDLINK },
204 { "writemaptosymlink", DBS_WRITEMAPTOSYMLINK },
205 { "writestatstohardlink", DBS_WRITESTATSTOHARDLINK },
206 { "writestatstosymlink", DBS_WRITESTATSTOSYMLINK },
207 { "forwardfileingroupwritabledirpath",
208 DBS_FORWARDFILEINGROUPWRITABLEDIRPATH },
209 { "includefileingroupwritabledirpath",
210 DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH },
211 { "classfileinunsafedirpath", DBS_CLASSFILEINUNSAFEDIRPATH },
212 { "errorheaderinunsafedirpath", DBS_ERRORHEADERINUNSAFEDIRPATH },
213 { "helpfileinunsafedirpath", DBS_HELPFILEINUNSAFEDIRPATH },
214 { "forwardfileinunsafedirpathsafe",
215 DBS_FORWARDFILEINUNSAFEDIRPATHSAFE },
216 { "includefileinunsafedirpathsafe",
217 DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE },
218 { "runprograminunsafedirpath", DBS_RUNPROGRAMINUNSAFEDIRPATH },
219 { "runwritableprogram", DBS_RUNWRITABLEPROGRAM },
220 { "nonrootsafeaddr", DBS_NONROOTSAFEADDR },
221 { "truststickybit", DBS_TRUSTSTICKYBIT },
222 { "dontwarnforwardfileinunsafedirpath",
223 DBS_DONTWARNFORWARDFILEINUNSAFEDIRPATH },
224 { "insufficiententropy", DBS_INSUFFICIENTENTROPY },
225 { "groupreadablesasldbfile", DBS_GROUPREADABLESASLDBFILE },
226 { "groupwritablesasldbfile", DBS_GROUPWRITABLESASLDBFILE },
227 { "groupwritableforwardfile", DBS_GROUPWRITABLEFORWARDFILE },
228 { "groupwritableincludefile", DBS_GROUPWRITABLEINCLUDEFILE },
229 { "worldwritableforwardfile", DBS_WORLDWRITABLEFORWARDFILE },
230 { "worldwritableincludefile", DBS_WORLDWRITABLEINCLUDEFILE },
231 { "groupreadablekeyfile", DBS_GROUPREADABLEKEYFILE },
232 { "groupreadabledefaultauthinfofile",
233 DBS_GROUPREADABLEAUTHINFOFILE },
238 ** Miscellaneous stuff.
241 int DtableSize = 50; /* max open files; reset in 4.2bsd */
243 ** SETDEFAULTS -- set default values
245 ** Some of these must be initialized using direct code since they
246 ** depend on run-time values. So let's do all of them this way.
249 ** e -- the default envelope.
255 ** Initializes a bunch of global variables to their
260 #define HOURS * 60 MINUTES
261 #define DAYS * 24 HOURS
263 #ifndef MAXRULERECURSION
264 # define MAXRULERECURSION 50 /* max ruleset recursion depth */
265 #endif /* ! MAXRULERECURSION */
269 register ENVELOPE *e;
275 numprocs = get_num_procs_online();
276 SpaceSub = ' '; /* option B */
277 QueueLA = 8 * numprocs; /* option x */
278 RefuseLA = 12 * numprocs; /* option X */
279 WkRecipFact = 30000L; /* option y */
280 WkClassFact = 1800L; /* option z */
281 WkTimeFact = 90000L; /* option Z */
282 QueueFactor = WkRecipFact * 20; /* option q */
283 QueueMode = QM_NORMAL; /* what queue items to act upon */
284 FileMode = (RealUid != geteuid()) ? 0644 : 0600;
286 QueueFileMode = (RealUid != geteuid()) ? 0644 : 0600;
287 /* option QueueFileMode */
289 if (((pw = sm_getpwnam("mailnull")) != NULL && pw->pw_uid != 0) ||
290 ((pw = sm_getpwnam("sendmail")) != NULL && pw->pw_uid != 0) ||
291 ((pw = sm_getpwnam("daemon")) != NULL && pw->pw_uid != 0))
293 DefUid = pw->pw_uid; /* option u */
294 DefGid = pw->pw_gid; /* option g */
295 DefUser = newstr(pw->pw_name);
299 DefUid = 1; /* option u */
300 DefGid = 1; /* option g */
305 sm_dprintf("setdefaults: DefUser=%s, DefUid=%ld, DefGid=%ld\n",
306 DefUser != NULL ? DefUser : "<1:1>",
307 (long) DefUid, (long) DefGid);
308 CheckpointInterval = 10; /* option C */
309 MaxHopCount = 25; /* option h */
310 set_delivery_mode(SM_FORK, e); /* option d */
311 e->e_errormode = EM_PRINT; /* option e */
314 e->e_xfqgrp = NOQGRP;
315 e->e_xfqdir = NOQDIR;
316 e->e_ctime = curtime();
317 SevenBitInput = false; /* option 7 */
318 MaxMciCache = 1; /* option k */
319 MciCacheTimeout = 5 MINUTES; /* option K */
320 LogLevel = 9; /* option L */
324 inittimeouts(NULL, false); /* option r */
325 PrivacyFlags = PRIV_PUBLIC; /* option p */
326 MeToo = true; /* option m */
327 SendMIMEErrors = true; /* option f */
328 SuperSafe = SAFE_REALLY; /* option s */
329 clrbitmap(DontBlameSendmail); /* DontBlameSendmail option */
331 MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */
333 MimeMode = MM_PASS8BIT;
334 #endif /* MIME8TO7 */
335 for (i = 0; i < MAXTOCLASS; i++)
337 TimeOuts.to_q_return[i] = 5 DAYS; /* option T */
338 TimeOuts.to_q_warning[i] = 0; /* option T */
340 ServiceSwitchFile = "/etc/mail/service.switch";
341 ServiceCacheMaxAge = (time_t) 10;
342 HostsFile = _PATH_HOSTS;
343 PidFile = newstr(_PATH_SENDMAILPID);
344 MustQuoteChars = "@,;:\\()[].'";
345 MciInfoTimeout = 30 MINUTES;
346 MaxRuleRecursion = MAXRULERECURSION;
347 MaxAliasRecursion = 10;
348 MaxMacroRecursion = 10;
349 ColonOkInAddr = true;
350 DontLockReadFiles = true;
351 DontProbeInterfaces = DPI_PROBEALL;
352 DoubleBounceAddr = "postmaster";
353 MaxHeadersLength = MAXHDRSLEN;
354 MaxMimeHeaderLength = MAXLINE;
355 MaxMimeFieldLength = MaxMimeHeaderLength / 2;
356 MaxForwardEntries = 0;
358 MaxNOOPCommands = MAXNOOPCOMMANDS;
360 AuthMechanisms = newstr(AUTH_MECHANISMS);
365 TLS_Srv_Opts = TLS_I_SRV;
366 if (NULL == EVP_digest)
367 EVP_digest = EVP_md5();
368 #endif /* STARTTLS */
370 HesiodContext = NULL;
371 #endif /* HESIOD_INIT */
373 /* Detect if IPv6 is available at run time */
374 i = socket(AF_INET6, SOCK_STREAM, 0);
383 UseCompressedIPv6Addresses = true;
387 #endif /* NETINET6 */
388 ControlSocketName = NULL;
389 memset(&ConnectOnlyTo, '\0', sizeof(ConnectOnlyTo));
390 DataFileBufferSize = 4096;
391 XscriptFileBufferSize = 4096;
392 for (i = 0; i < MAXRWSETS; i++)
393 RuleSetNames[i] = NULL;
395 InputFilters[0] = NULL;
397 RejectLogInterval = 3 HOURS;
398 #if REQUIRES_DIR_FSYNC
399 RequiresDirfsync = true;
400 #endif /* REQUIRES_DIR_FSYNC */
401 #if _FFR_RCPTTHROTDELAY
402 BadRcptThrottleDelay = 1;
403 #endif /* _FFR_RCPTTHROTDELAY */
404 ConnectionRateWindowSize = 60;
405 #if _FFR_BOUNCE_QUEUE
406 BounceQueue = NOQGRP;
407 #endif /* _FFR_BOUNCE_QUEUE */
416 ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
422 struct passwd *defpwent;
423 static char defuserbuf[40];
425 DefUser = defuserbuf;
426 defpwent = sm_getpwuid(DefUid);
427 (void) sm_strlcpy(defuserbuf,
428 (defpwent == NULL || defpwent->pw_name == NULL)
429 ? "nobody" : defpwent->pw_name,
432 sm_dprintf("setdefuser: DefUid=%ld, DefUser=%s\n",
433 (long) DefUid, DefUser);
436 ** SETUPQUEUES -- initialize default queues
438 ** The mqueue QUEUE structure gets filled in after readcf() but
439 ** we need something to point to now for the mailer setup,
440 ** which use "mqueue" as default queue.
448 MaxRunnersPerQueue = 1;
449 (void) sm_strlcpy(buf, "mqueue, P=/var/spool/mqueue", sizeof(buf));
450 makequeue(buf, false);
453 ** SETUPMAILERS -- initialize default mailers
461 (void) sm_strlcpy(buf, "prog, P=/bin/sh, F=lsouDq9, T=X-Unix/X-Unix/X-Unix, A=sh -c \201u",
465 (void) sm_strlcpy(buf, "*file*, P=[FILE], F=lsDFMPEouq9, T=X-Unix/X-Unix/X-Unix, A=FILE \201u",
469 (void) sm_strlcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE \201u",
475 ** SETUPMAPS -- set up map classes
478 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
480 extern bool parse __P((MAP *, char *)); \
481 extern bool open __P((MAP *, int)); \
482 extern void close __P((MAP *)); \
483 extern char *lookup __P((MAP *, char *, char **, int *)); \
484 extern void store __P((MAP *, char *, char *)); \
485 s = stab(name, ST_MAPCLASS, ST_ENTER); \
486 s->s_mapclass.map_cname = name; \
487 s->s_mapclass.map_ext = ext; \
488 s->s_mapclass.map_cflags = flags; \
489 s->s_mapclass.map_parse = parse; \
490 s->s_mapclass.map_open = open; \
491 s->s_mapclass.map_close = close; \
492 s->s_mapclass.map_lookup = lookup; \
493 s->s_mapclass.map_store = store; \
502 # if DB_VERSION_MAJOR > 1
503 int major_v, minor_v, patch_v;
505 (void) db_version(&major_v, &minor_v, &patch_v);
506 if (major_v != DB_VERSION_MAJOR || minor_v != DB_VERSION_MINOR)
509 syserr("Berkeley DB version mismatch: compiled against %d.%d.%d, run-time linked against %d.%d.%d",
510 DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,
511 major_v, minor_v, patch_v);
513 # endif /* DB_VERSION_MAJOR > 1 */
515 MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
516 map_parseargs, hash_map_open, db_map_close,
517 db_map_lookup, db_map_store);
519 MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
520 map_parseargs, bt_map_open, db_map_close,
521 db_map_lookup, db_map_store);
525 MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
526 map_parseargs, ndbm_map_open, ndbm_map_close,
527 ndbm_map_lookup, ndbm_map_store);
531 MAPDEF("nis", NULL, MCF_ALIASOK,
532 map_parseargs, nis_map_open, null_map_close,
533 nis_map_lookup, null_map_store);
537 MAPDEF("nisplus", NULL, MCF_ALIASOK,
538 map_parseargs, nisplus_map_open, null_map_close,
539 nisplus_map_lookup, null_map_store);
543 MAPDEF("ldap", NULL, MCF_ALIASOK|MCF_NOTPERSIST,
544 ldapmap_parseargs, ldapmap_open, ldapmap_close,
545 ldapmap_lookup, null_map_store);
549 MAPDEF("ph", NULL, MCF_NOTPERSIST,
550 ph_map_parseargs, ph_map_open, ph_map_close,
551 ph_map_lookup, null_map_store);
555 /* IRIX 6.5 nsd support */
556 MAPDEF("nsd", NULL, MCF_ALIASOK,
557 map_parseargs, null_map_open, null_map_close,
558 nsd_map_lookup, null_map_store);
562 MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
563 map_parseargs, hes_map_open, hes_map_close,
564 hes_map_lookup, null_map_store);
568 MAPDEF("netinfo", NULL, MCF_ALIASOK,
569 map_parseargs, ni_map_open, null_map_close,
570 ni_map_lookup, null_map_store);
574 MAPDEF("dns", NULL, 0,
575 dns_map_init, null_map_open, null_map_close,
576 dns_map_lookup, null_map_store);
581 # if _FFR_DNSMAP_ALIASABLE
582 MAPDEF("dns", NULL, MCF_ALIASOK,
583 dns_map_parseargs, dns_map_open, null_map_close,
584 dns_map_lookup, null_map_store);
585 # else /* _FFR_DNSMAP_ALIASABLE */
586 MAPDEF("dns", NULL, 0,
587 dns_map_parseargs, dns_map_open, null_map_close,
588 dns_map_lookup, null_map_store);
589 # endif /* _FFR_DNSMAP_ALIASABLE */
591 #endif /* NAMED_BIND */
594 /* best MX DNS lookup */
595 MAPDEF("bestmx", NULL, MCF_OPTFILE,
596 map_parseargs, null_map_open, null_map_close,
597 bestmx_map_lookup, null_map_store);
598 #endif /* NAMED_BIND */
600 MAPDEF("host", NULL, 0,
601 host_map_init, null_map_open, null_map_close,
602 host_map_lookup, null_map_store);
604 MAPDEF("text", NULL, MCF_ALIASOK,
605 map_parseargs, text_map_open, null_map_close,
606 text_map_lookup, null_map_store);
608 MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
609 map_parseargs, stab_map_open, null_map_close,
610 stab_map_lookup, stab_map_store);
612 MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
613 map_parseargs, impl_map_open, impl_map_close,
614 impl_map_lookup, impl_map_store);
616 /* access to system passwd file */
617 MAPDEF("user", NULL, MCF_OPTFILE,
618 map_parseargs, user_map_open, null_map_close,
619 user_map_lookup, null_map_store);
622 MAPDEF("dequote", NULL, 0,
623 dequote_init, null_map_open, null_map_close,
624 dequote_map, null_map_store);
627 MAPDEF("regex", NULL, 0,
628 regex_map_init, null_map_open, null_map_close,
629 regex_map_lookup, null_map_store);
630 #endif /* MAP_REGEX */
634 MAPDEF("userdb", ".db", 0,
635 map_parseargs, null_map_open, null_map_close,
636 udb_map_lookup, null_map_store);
639 /* arbitrary programs */
640 MAPDEF("program", NULL, MCF_ALIASOK,
641 map_parseargs, null_map_open, null_map_close,
642 prog_map_lookup, null_map_store);
645 MAPDEF("sequence", NULL, MCF_ALIASOK,
646 seq_map_parse, null_map_open, null_map_close,
647 seq_map_lookup, seq_map_store);
649 /* switched interface to sequenced maps */
650 MAPDEF("switch", NULL, MCF_ALIASOK,
651 map_parseargs, switch_map_open, null_map_close,
652 seq_map_lookup, seq_map_store);
654 /* null map lookup -- really for internal use only */
655 MAPDEF("null", NULL, MCF_ALIASOK|MCF_OPTFILE,
656 map_parseargs, null_map_open, null_map_close,
657 null_map_lookup, null_map_store);
659 /* syslog map -- logs information to syslog */
660 MAPDEF("syslog", NULL, 0,
661 syslog_map_parseargs, null_map_open, null_map_close,
662 syslog_map_lookup, null_map_store);
664 /* macro storage map -- rulesets can set macros */
665 MAPDEF("macro", NULL, 0,
666 dequote_init, null_map_open, null_map_close,
667 macro_map_lookup, null_map_store);
669 /* arithmetic map -- add/subtract/compare */
670 MAPDEF("arith", NULL, 0,
671 dequote_init, null_map_open, null_map_close,
672 arith_map_lookup, null_map_store);
674 /* "arpa" map -- IP -> arpa */
675 MAPDEF("arpa", NULL, 0,
676 dequote_init, null_map_open, null_map_close,
677 arpa_map_lookup, null_map_store);
680 /* arbitrary daemons */
681 MAPDEF("socket", NULL, MCF_ALIASOK,
682 map_parseargs, socket_map_open, socket_map_close,
683 socket_map_lookup, null_map_store);
684 #endif /* SOCKETMAP */
687 /* dprintf map -- logs information to syslog */
688 MAPDEF("dprintf", NULL, 0,
689 dprintf_map_parseargs, null_map_open, null_map_close,
690 dprintf_map_lookup, null_map_store);
691 #endif /* _FFR_DPRINTF_MAP */
695 /* bogus map -- always return tempfail */
696 MAPDEF("bogus", NULL, MCF_ALIASOK|MCF_OPTFILE,
697 map_parseargs, null_map_open, null_map_close,
698 bogus_map_lookup, null_map_store);
704 ** INITHOSTMAPS -- initial host-dependent maps
706 ** This should act as an interface to any local service switch
707 ** provided by the host operating system.
716 ** Should define maps "host" and "users" as necessary
717 ** for this OS. If they are not defined, they will get
718 ** a default value later. It should check to make sure
719 ** they are not defined first, since it's possible that
720 ** the config file has provided an override.
728 char *maptype[MAXMAPSTACK];
729 short mapreturn[MAXMAPACTIONS];
733 ** Make sure we have a host map.
736 if (stab("host", ST_MAP, ST_FIND) == NULL)
738 /* user didn't initialize: set up host map */
739 (void) sm_strlcpy(buf, "host host", sizeof(buf));
741 if (ConfigLevel >= 2)
742 (void) sm_strlcat(buf, " -a. -D", sizeof(buf));
743 #endif /* NAMED_BIND */
744 (void) makemapentry(buf);
748 ** Set up default aliases maps
751 nmaps = switch_map_find("aliases", maptype, mapreturn);
752 for (i = 0; i < nmaps; i++)
754 if (strcmp(maptype[i], "files") == 0 &&
755 stab("aliases.files", ST_MAP, ST_FIND) == NULL)
757 (void) sm_strlcpy(buf, "aliases.files null",
759 (void) makemapentry(buf);
762 else if (strcmp(maptype[i], "nisplus") == 0 &&
763 stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
765 (void) sm_strlcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion mail_aliases.org_dir",
767 (void) makemapentry(buf);
771 else if (strcmp(maptype[i], "nis") == 0 &&
772 stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
774 (void) sm_strlcpy(buf, "aliases.nis nis mail.aliases",
776 (void) makemapentry(buf);
780 else if (strcmp(maptype[i], "netinfo") == 0 &&
781 stab("aliases.netinfo", ST_MAP, ST_FIND) == NULL)
783 (void) sm_strlcpy(buf, "aliases.netinfo netinfo -z, /aliases",
785 (void) makemapentry(buf);
789 else if (strcmp(maptype[i], "hesiod") == 0 &&
790 stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL)
792 (void) sm_strlcpy(buf, "aliases.hesiod hesiod aliases",
794 (void) makemapentry(buf);
797 #if LDAPMAP && defined(SUN_EXTENSIONS) && \
798 defined(SUN_SIMPLIFIED_LDAP) && HASLDAPGETALIASBYNAME
799 else if (strcmp(maptype[i], "ldap") == 0 &&
800 stab("aliases.ldap", ST_MAP, ST_FIND) == NULL)
802 (void) sm_strlcpy(buf, "aliases.ldap ldap -b . -h localhost -k mail=%0 -v mailgroup",
804 (void) makemapentry(buf);
806 #endif /* LDAPMAP && defined(SUN_EXTENSIONS) && ... */
808 if (stab("aliases", ST_MAP, ST_FIND) == NULL)
810 (void) sm_strlcpy(buf, "aliases switch aliases", sizeof(buf));
811 (void) makemapentry(buf);
816 ** SWITCH_MAP_FIND -- find the list of types associated with a map
818 ** This is the system-dependent interface to the service switch.
821 ** service -- the name of the service of interest.
822 ** maptype -- an out-array of strings containing the types
823 ** of access to use for this service. There can
824 ** be at most MAXMAPSTACK types for a single service.
825 ** mapreturn -- an out-array of return information bitmaps
829 ** The number of map types filled in, or -1 for failure.
832 ** Preserves errno so nothing in the routine clobbers it.
835 #if defined(SOLARIS) || (defined(sony_news) && defined(__svr4))
836 # define _USE_SUN_NSSWITCH_
837 #endif /* defined(SOLARIS) || (defined(sony_news) && defined(__svr4)) */
839 #if _FFR_HPUX_NSSWITCH
841 # define _USE_SUN_NSSWITCH_
843 #endif /* _FFR_HPUX_NSSWITCH */
845 #ifdef _USE_SUN_NSSWITCH_
846 # include <nsswitch.h>
847 #endif /* _USE_SUN_NSSWITCH_ */
849 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
850 # define _USE_DEC_SVC_CONF_
851 #endif /* defined(ultrix) || (defined(__osf__) && defined(__alpha)) */
853 #ifdef _USE_DEC_SVC_CONF_
854 # include <sys/svcinfo.h>
855 #endif /* _USE_DEC_SVC_CONF_ */
858 switch_map_find(service, maptype, mapreturn)
860 char *maptype[MAXMAPSTACK];
861 short mapreturn[MAXMAPACTIONS];
864 int save_errno = errno;
866 #ifdef _USE_SUN_NSSWITCH_
867 struct __nsw_switchconfig *nsw_conf;
868 enum __nsw_parse_err pserr;
869 struct __nsw_lookup *lk;
870 static struct __nsw_lookup lkp0 =
871 { "files", {1, 0, 0, 0}, NULL, NULL };
872 static struct __nsw_switchconfig lkp_default =
873 { 0, "sendmail", 3, &lkp0 };
875 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
876 mapreturn[svcno] = 0;
878 if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
879 lk = lkp_default.lookups;
881 lk = nsw_conf->lookups;
883 while (lk != NULL && svcno < MAXMAPSTACK)
885 maptype[svcno] = lk->service_name;
886 if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
887 mapreturn[MA_NOTFOUND] |= 1 << svcno;
888 if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
889 mapreturn[MA_TRYAGAIN] |= 1 << svcno;
890 if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
891 mapreturn[MA_TRYAGAIN] |= 1 << svcno;
897 #endif /* _USE_SUN_NSSWITCH_ */
899 #ifdef _USE_DEC_SVC_CONF_
900 struct svcinfo *svcinfo;
903 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
904 mapreturn[svcno] = 0;
909 if (strcmp(service, "hosts") == 0)
911 else if (strcmp(service, "aliases") == 0)
913 else if (strcmp(service, "passwd") == 0)
920 for (svcno = 0; svcno < SVC_PATHSIZE && svcno < MAXMAPSTACK; svcno++)
922 switch (svcinfo->svcpath[svc][svcno])
925 maptype[svcno] = "files";
929 maptype[svcno] = "nis";
933 maptype[svcno] = "dns";
938 maptype[svcno] = "hesiod";
940 # endif /* SVC_HESIOD */
949 #endif /* _USE_DEC_SVC_CONF_ */
951 #if !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_)
953 ** Fall-back mechanism.
957 static time_t servicecachetime; /* time service switch was cached */
958 time_t now = curtime();
960 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
961 mapreturn[svcno] = 0;
963 if ((now - servicecachetime) > (time_t) ServiceCacheMaxAge)
965 /* (re)read service switch */
966 register SM_FILE_T *fp;
967 long sff = SFF_REGONLY|SFF_OPENASROOT|SFF_NOLOCK;
969 if (!bitnset(DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR,
974 servicecachetime = now;
975 fp = safefopen(ServiceSwitchFile, O_RDONLY, 0, sff);
980 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf,
985 p = strpbrk(buf, "#\n");
988 #ifndef SM_NSSWITCH_DELIMS
989 # define SM_NSSWITCH_DELIMS " \t"
990 #endif /* SM_NSSWITCH_DELIMS */
991 p = strpbrk(buf, SM_NSSWITCH_DELIMS);
998 sm_syslog(LOG_ERR, NOQID,
999 "Bad line on %.100s: %.100s",
1004 while (isascii(*p) && isspace(*p))
1010 ** Find/allocate space for this service entry.
1011 ** Space for all of the service strings
1012 ** are allocated at once. This means
1013 ** that we only have to free the first
1014 ** one to free all of them.
1017 st = stab(buf, ST_SERVICE, ST_ENTER);
1018 if (st->s_service[0] != NULL)
1019 sm_free((void *) st->s_service[0]); /* XXX */
1021 for (svcno = 0; svcno < MAXMAPSTACK; )
1025 st->s_service[svcno++] = p;
1026 p = strpbrk(p, " \t");
1030 while (isascii(*p) && isspace(*p))
1033 if (svcno < MAXMAPSTACK)
1034 st->s_service[svcno] = NULL;
1036 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1040 /* look up entry in cache */
1041 st = stab(service, ST_SERVICE, ST_FIND);
1042 if (st != NULL && st->s_service[0] != NULL)
1046 while (svcno < MAXMAPSTACK)
1048 maptype[svcno] = st->s_service[svcno];
1049 if (maptype[svcno++] == NULL)
1055 #endif /* !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) */
1057 #if !defined(_USE_SUN_NSSWITCH_)
1058 /* if the service file doesn't work, use an absolute fallback */
1059 # ifdef _USE_DEC_SVC_CONF_
1061 # endif /* _USE_DEC_SVC_CONF_ */
1062 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
1063 mapreturn[svcno] = 0;
1065 if (strcmp(service, "aliases") == 0)
1067 maptype[svcno++] = "files";
1068 # if defined(AUTO_NETINFO_ALIASES) && defined (NETINFO)
1069 maptype[svcno++] = "netinfo";
1070 # endif /* defined(AUTO_NETINFO_ALIASES) && defined (NETINFO) */
1071 # ifdef AUTO_NIS_ALIASES
1073 maptype[svcno++] = "nisplus";
1074 # endif /* NISPLUS */
1076 maptype[svcno++] = "nis";
1078 # endif /* AUTO_NIS_ALIASES */
1082 if (strcmp(service, "hosts") == 0)
1085 maptype[svcno++] = "dns";
1086 # else /* NAMED_BIND */
1087 # if defined(sun) && !defined(BSD)
1089 maptype[svcno++] = "nis";
1090 # endif /* defined(sun) && !defined(BSD) */
1091 # endif /* NAMED_BIND */
1092 # if defined(AUTO_NETINFO_HOSTS) && defined (NETINFO)
1093 maptype[svcno++] = "netinfo";
1094 # endif /* defined(AUTO_NETINFO_HOSTS) && defined (NETINFO) */
1095 maptype[svcno++] = "files";
1101 #endif /* !defined(_USE_SUN_NSSWITCH_) */
1104 ** USERNAME -- return the user id of the logged in user.
1110 ** The login name of the logged in user.
1116 ** The return value is statically allocated.
1122 static char *myname = NULL;
1123 extern char *getlogin();
1124 register struct passwd *pw;
1126 /* cache the result */
1129 myname = getlogin();
1130 if (myname == NULL || myname[0] == '\0')
1132 pw = sm_getpwuid(RealUid);
1134 myname = pw->pw_name;
1138 uid_t uid = RealUid;
1140 if ((pw = sm_getpwnam(myname)) == NULL ||
1141 (uid != 0 && uid != pw->pw_uid))
1143 pw = sm_getpwuid(uid);
1145 myname = pw->pw_name;
1148 if (myname == NULL || myname[0] == '\0')
1150 syserr("554 5.3.0 Who are you?");
1151 myname = "postmaster";
1153 else if (strpbrk(myname, ",;:/|\"\\") != NULL)
1154 myname = addquotes(myname, NULL);
1156 myname = sm_pstrdup_x(myname);
1161 ** TTYPATH -- Get the path of the user's tty
1163 ** Returns the pathname of the user's tty. Returns NULL if
1164 ** the user is not logged in or if s/he has write permission
1171 ** pathname of the user's tty.
1172 ** NULL if not logged in or write permission denied.
1178 ** Return value is in a local buffer.
1188 register char *pathn;
1189 extern char *ttyname();
1190 extern char *getlogin();
1192 /* compute the pathname of the controlling tty */
1193 if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
1194 (pathn = ttyname(0)) == NULL)
1200 /* see if we have write permission */
1201 if (stat(pathn, &stbuf) < 0 || !bitset(S_IWOTH, stbuf.st_mode))
1207 /* see if the user is logged in */
1208 if (getlogin() == NULL)
1215 ** CHECKCOMPAT -- check for From and To person compatible.
1217 ** This routine can be supplied on a per-installation basis
1218 ** to determine whether a person is allowed to send a message.
1219 ** This allows restriction of certain types of internet
1220 ** forwarding or registration of users.
1222 ** If the hosts are found to be incompatible, an error
1223 ** message should be given using "usrerr" and an EX_ code
1224 ** should be returned. You can also set to->q_status to
1225 ** a DSN-style status code.
1227 ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the
1228 ** body during the return-to-sender function; this should be done
1229 ** on huge messages. This bit may already be set by the ESMTP
1233 ** to -- the person being sent to.
1239 ** none (unless you include the usrerr stuff)
1244 register ADDRESS *to;
1245 register ENVELOPE *e;
1248 sm_dprintf("checkcompat(to=%s, from=%s)\n",
1249 to->q_paddr, e->e_from.q_paddr);
1252 /* this code is intended as an example only */
1255 s = stab("arpa", ST_MAILER, ST_FIND);
1256 if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
1257 to->q_mailer == s->s_mailer)
1259 usrerr("553 No ARPA mail through this machine: see your system administration");
1260 /* e->e_flags |= EF_NO_BODY_RETN; to suppress body on return */
1261 to->q_status = "5.7.1";
1262 return EX_UNAVAILABLE;
1264 #endif /* EXAMPLE_CODE */
1268 #ifdef SUN_EXTENSIONS
1274 /* Check for large file descriptor */
1275 if (fstat(fileno(stdin), &sbuf) < 0)
1277 if (errno == EOVERFLOW)
1284 #endif /* SUN_EXTENSIONS */
1287 ** INIT_MD -- do machine dependent initializations
1289 ** Systems that have global modes that should be set should do
1290 ** them here rather than in main.
1294 # include <compat.h>
1295 #endif /* _AUX_SOURCE */
1298 # include <shares.h>
1299 #endif /* SHARE_V1 */
1307 setcompat(getcompat() | COMPAT_BSDPROT);
1308 #endif /* _AUX_SOURCE */
1310 #ifdef SUN_EXTENSIONS
1312 #endif /* SUN_EXTENSIONS */
1315 /* keep gethostby*() from stripping the local domain name */
1316 set_domain_trim_off();
1317 #endif /* _CONVEX_SOURCE */
1318 #if defined(__QNX__) && !defined(__QNXNTO__)
1320 ** Due to QNX's network distributed nature, you can target a tcpip
1321 ** stack on a different node in the qnx network; this patch lets
1322 ** this feature work. The __sock_locate() must be done before the
1323 ** environment is clear.
1326 #endif /* __QNX__ */
1327 #if SECUREWARE || defined(_SCO_unix_)
1328 set_auth_parameters(argc, argv);
1332 ** This is required for highest security levels (the kernel
1333 ** won't let it call set*uid() or run setuid binaries without
1334 ** it). It may be necessary on other SECUREWARE systems.
1337 if (getluid() == -1)
1339 # endif /* _SCO_unix_ */
1340 #endif /* SECUREWARE || defined(_SCO_unix_) */
1343 #ifdef VENDOR_DEFAULT
1344 VendorCode = VENDOR_DEFAULT;
1345 #else /* VENDOR_DEFAULT */
1346 VendorCode = VENDOR_BERKELEY;
1347 #endif /* VENDOR_DEFAULT */
1350 ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations
1352 ** Called once, on startup.
1355 ** e -- the global envelope.
1361 ** vendor-dependent.
1365 init_vendor_macros(e)
1366 register ENVELOPE *e;
1370 ** GETLA -- get the current load average
1372 ** This code stolen from la.c.
1378 ** The current load average as an integer.
1384 /* try to guess what style of load average we have */
1385 #define LA_ZERO 1 /* always return load average as zero */
1386 #define LA_INT 2 /* read kmem for avenrun; interpret as long */
1387 #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */
1388 #define LA_SUBR 4 /* call getloadavg */
1389 #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */
1390 #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */
1391 #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */
1392 #define LA_READKSYM 8 /* SVR4: use MIOC_READKSYM ioctl call */
1393 #define LA_DGUX 9 /* special DGUX implementation */
1394 #define LA_HPUX 10 /* special HPUX implementation */
1395 #define LA_IRIX6 11 /* special IRIX 6.2 implementation */
1396 #define LA_KSTAT 12 /* special Solaris kstat(3k) implementation */
1397 #define LA_DEVSHORT 13 /* read short from a device */
1398 #define LA_ALPHAOSF 14 /* Digital UNIX (OSF/1 on Alpha) table() call */
1399 #define LA_PSET 15 /* Solaris per-processor-set load average */
1400 #define LA_LONGLONG 17 /* read kmem for avenrun; interpret as long long */
1402 /* do guesses based on general OS type */
1404 # define LA_TYPE LA_ZERO
1405 #endif /* ! LA_TYPE */
1408 # if defined(unixpc)
1410 # endif /* defined(unixpc) */
1412 # if defined(__alpha) || defined(IRIX)
1414 # endif /* defined(__alpha) || defined(IRIX) */
1416 #endif /* ! FSHIFT */
1420 #endif /* ! FSHIFT */
1423 # define FSCALE (1 << FSHIFT)
1424 #endif /* ! FSCALE */
1428 # define LA_AVENRUN "avenrun"
1429 # else /* SYSTEM5 */
1430 # define LA_AVENRUN "_avenrun"
1431 # endif /* SYSTEM5 */
1432 #endif /* ! LA_AVENRUN */
1434 /* _PATH_KMEM should be defined in <paths.h> */
1436 # define _PATH_KMEM "/dev/kmem"
1437 #endif /* ! _PATH_KMEM */
1439 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) || (LA_TYPE == LA_LONGLONG)
1443 /* _PATH_UNIX should be defined in <paths.h> */
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 */
1454 # else /* _AUX_SOURCE */
1460 # endif /* _AUX_SOURCE */
1461 # define X_AVENRUN 0
1467 static int kmem = -1;
1468 # if LA_TYPE == LA_INT
1470 # else /* LA_TYPE == LA_INT */
1471 # if LA_TYPE == LA_SHORT
1474 # if LA_TYPE == LA_LONGLONG
1475 long long avenrun[3];
1476 # else /* LA_TYPE == LA_LONGLONG */
1478 # endif /* LA_TYPE == LA_LONGLONG */
1479 # endif /* LA_TYPE == LA_SHORT */
1480 # endif /* LA_TYPE == LA_INT */
1481 extern off_t lseek();
1486 (void) sm_strlcpy(Nl[X_AVENRUN].n_name, LA_AVENRUN,
1487 sizeof(Nl[X_AVENRUN].n_name));
1488 Nl[1].n_name[0] = '\0';
1489 # endif /* _AUX_SOURCE */
1491 # if defined(_AIX3) || defined(_AIX4)
1492 if (knlist(Nl, 1, sizeof(Nl[0])) < 0)
1493 # else /* defined(_AIX3) || defined(_AIX4) */
1494 if (nlist(_PATH_UNIX, Nl) < 0)
1495 # endif /* defined(_AIX3) || defined(_AIX4) */
1498 sm_dprintf("getla: nlist(%s): %s\n", _PATH_UNIX,
1499 sm_errstring(errno));
1502 if (Nl[X_AVENRUN].n_value == 0)
1505 sm_dprintf("getla: nlist(%s, %s) ==> 0\n",
1506 _PATH_UNIX, LA_AVENRUN);
1509 # ifdef NAMELISTMASK
1510 Nl[X_AVENRUN].n_value &= NAMELISTMASK;
1511 # endif /* NAMELISTMASK */
1513 kmem = open(_PATH_KMEM, 0, 0);
1517 sm_dprintf("getla: open(/dev/kmem): %s\n",
1518 sm_errstring(errno));
1521 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1522 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1525 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1526 sm_errstring(errno));
1533 sm_dprintf("getla: symbol address = %#lx\n",
1534 (unsigned long) Nl[X_AVENRUN].n_value);
1535 if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
1536 read(kmem, (char *) avenrun, sizeof(avenrun)) != sizeof(avenrun))
1540 sm_dprintf("getla: lseek or read: %s\n",
1541 sm_errstring(errno));
1544 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) || (LA_TYPE == LA_LONGLONG)
1547 # if LA_TYPE == LA_SHORT
1548 sm_dprintf("getla: avenrun = %d", avenrun[0]);
1550 sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1551 # else /* LA_TYPE == LA_SHORT */
1552 # if LA_TYPE == LA_LONGLONG
1553 sm_dprintf("getla: avenrun = %lld", avenrun[0]);
1555 sm_dprintf(", %lld, %lld", avenrun[1], avenrun[2]);
1556 # else /* LA_TYPE == LA_LONGLONG */
1557 sm_dprintf("getla: avenrun = %ld", avenrun[0]);
1559 sm_dprintf(", %ld, %ld", avenrun[1], avenrun[2]);
1560 # endif /* LA_TYPE == LA_LONGLONG */
1561 # endif /* LA_TYPE == LA_SHORT */
1565 sm_dprintf("getla: %d\n",
1566 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1567 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1568 # else /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) || (LA_TYPE == LA_LONGLONG) */
1571 sm_dprintf("getla: avenrun = %g", avenrun[0]);
1573 sm_dprintf(", %g, %g", avenrun[1], avenrun[2]);
1577 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1578 return ((int) (avenrun[0] + 0.5));
1579 # endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) || (LA_TYPE == LA_LONGLONG) */
1582 #endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) || (LA_TYPE == LA_LONGLONG) */
1584 #if LA_TYPE == LA_READKSYM
1586 # include <sys/ksym.h>
1592 static int kmem = -1;
1594 struct mioc_rksym mirk;
1598 kmem = open("/dev/kmem", 0, 0);
1602 sm_dprintf("getla: open(/dev/kmem): %s\n",
1603 sm_errstring(errno));
1606 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1607 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1610 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1611 sm_errstring(errno));
1617 mirk.mirk_symname = LA_AVENRUN;
1618 mirk.mirk_buf = avenrun;
1619 mirk.mirk_buflen = sizeof(avenrun);
1620 if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
1623 sm_dprintf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
1624 sm_errstring(errno));
1629 sm_dprintf("getla: avenrun = %d", avenrun[0]);
1631 sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1635 sm_dprintf("getla: %d\n",
1636 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1637 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1640 #endif /* LA_TYPE == LA_READKSYM */
1642 #if LA_TYPE == LA_DGUX
1644 # include <sys/dg_sys_info.h>
1649 struct dg_sys_info_load_info load_info;
1651 dg_sys_info((long *)&load_info,
1652 DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
1655 sm_dprintf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1657 return ((int) (load_info.one_minute + 0.5));
1660 #endif /* LA_TYPE == LA_DGUX */
1662 #if LA_TYPE == LA_HPUX
1664 /* forward declarations to keep gcc from complaining */
1669 struct pst_diskinfo;
1670 struct pst_processor;
1672 struct pst_swapinfo;
1674 # include <sys/param.h>
1675 # include <sys/pstat.h>
1680 struct pst_dynamic pstd;
1682 if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1683 (size_t) 1, 0) == -1)
1687 sm_dprintf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1689 return (int) (pstd.psd_avg_1_min + 0.5);
1692 #endif /* LA_TYPE == LA_HPUX */
1694 #if LA_TYPE == LA_SUBR
1701 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
1704 sm_dprintf("getla: getloadavg failed: %s",
1705 sm_errstring(errno));
1709 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1710 return ((int) (avenrun[0] + 0.5));
1713 #endif /* LA_TYPE == LA_SUBR */
1715 #if LA_TYPE == LA_MACH
1718 ** This has been tested on NEXTSTEP release 2.1/3.X.
1721 # if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
1722 # include <mach/mach.h>
1723 # else /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1725 # endif /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1730 processor_set_t default_set;
1731 kern_return_t error;
1732 unsigned int info_count;
1733 struct processor_set_basic_info info;
1736 error = processor_set_default(host_self(), &default_set);
1737 if (error != KERN_SUCCESS)
1740 sm_dprintf("getla: processor_set_default failed: %s",
1741 sm_errstring(errno));
1744 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
1745 if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
1746 &host, (processor_set_info_t)&info,
1747 &info_count) != KERN_SUCCESS)
1750 sm_dprintf("getla: processor_set_info failed: %s",
1751 sm_errstring(errno));
1755 sm_dprintf("getla: %d\n",
1756 (int) ((info.load_average + (LOAD_SCALE / 2)) /
1758 return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
1761 #endif /* LA_TYPE == LA_MACH */
1763 #if LA_TYPE == LA_PROCSTR
1764 # if SM_CONF_BROKEN_STRTOD
1765 ERROR: This OS has most likely a broken strtod() implemenentation.
1766 ERROR: The function is required for getla().
1767 ERROR: Check the compilation options _LA_PROCSTR and
1768 ERROR: _SM_CONF_BROKEN_STRTOD (without the leading _).
1769 # endif /* SM_CONF_BROKEN_STRTOD */
1772 ** Read /proc/loadavg for the load average. This is assumed to be
1773 ** in a format like "0.15 0.12 0.06".
1775 ** Initially intended for Linux. This has been in the kernel
1776 ** since at least 0.99.15.
1779 # ifndef _PATH_LOADAVG
1780 # define _PATH_LOADAVG "/proc/loadavg"
1781 # endif /* ! _PATH_LOADAVG */
1787 register int result;
1790 fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_LOADAVG, SM_IO_RDONLY,
1795 sm_dprintf("getla: sm_io_open(%s): %s\n",
1796 _PATH_LOADAVG, sm_errstring(errno));
1799 result = sm_io_fscanf(fp, SM_TIME_DEFAULT, "%lf", &avenrun);
1800 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1804 sm_dprintf("getla: sm_io_fscanf() = %d: %s\n",
1805 result, sm_errstring(errno));
1810 sm_dprintf("getla(): %.2f\n", avenrun);
1812 return ((int) (avenrun + 0.5));
1815 #endif /* LA_TYPE == LA_PROCSTR */
1817 #if LA_TYPE == LA_IRIX6
1819 # include <sys/sysmp.h>
1822 # define CAST_SYSMP(x) (x)
1823 # else /* _UNICOSMP */
1824 # define CAST_SYSMP(x) ((x) & 0x7fffffff)
1825 # endif /* _UNICOSMP */
1831 static int kmem = -1;
1836 kmem = open(_PATH_KMEM, 0, 0);
1840 sm_dprintf("getla: open(%s): %s\n", _PATH_KMEM,
1841 sm_errstring(errno));
1844 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1845 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1848 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1849 sm_errstring(errno));
1856 if (lseek(kmem, CAST_SYSMP(sysmp(MP_KERNADDR, MPKA_AVENRUN)), SEEK_SET)
1858 read(kmem, (char *) avenrun, sizeof(avenrun)) != sizeof(avenrun))
1861 sm_dprintf("getla: lseek or read: %s\n",
1862 sm_errstring(errno));
1867 sm_dprintf("getla: avenrun = %ld", (long int) avenrun[0]);
1869 sm_dprintf(", %ld, %ld",
1870 (long int) avenrun[1], (long int) avenrun[2]);
1875 sm_dprintf("getla: %d\n",
1876 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1877 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1880 #endif /* LA_TYPE == LA_IRIX6 */
1882 #if LA_TYPE == LA_KSTAT
1889 static kstat_ctl_t *kc = NULL;
1890 static kstat_t *ksp = NULL;
1894 if (kc == NULL) /* if not initialized before */
1899 sm_dprintf("getla: kstat_open(): %s\n",
1900 sm_errstring(errno));
1904 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
1908 sm_dprintf("getla: kstat_lookup(): %s\n",
1909 sm_errstring(errno));
1912 if (kstat_read(kc, ksp, NULL) < 0)
1915 sm_dprintf("getla: kstat_read(): %s\n",
1916 sm_errstring(errno));
1919 ksn = (kstat_named_t *) kstat_data_lookup(ksp, "avenrun_1min");
1920 la = ((double) ksn->value.ul + FSCALE/2) / FSCALE;
1921 /* kstat_close(kc); /o do not close for fast access */
1925 #endif /* LA_TYPE == LA_KSTAT */
1927 #if LA_TYPE == LA_DEVSHORT
1930 ** Read /dev/table/avenrun for the load average. This should contain
1931 ** three shorts for the 1, 5, and 15 minute loads. We only read the
1932 ** first, since that's all we care about.
1934 ** Intended for SCO OpenServer 5.
1937 # ifndef _PATH_AVENRUN
1938 # define _PATH_AVENRUN "/dev/table/avenrun"
1939 # endif /* ! _PATH_AVENRUN */
1944 static int afd = -1;
1951 if (afd == -1 || lseek(afd, 0L, SEEK_SET) == -1)
1955 afd = open(_PATH_AVENRUN, O_RDONLY|O_SYNC);
1958 sm_syslog(LOG_ERR, NOQID,
1959 "can't open %s: %s",
1960 _PATH_AVENRUN, sm_errstring(errno));
1965 r = read(afd, &avenrun, sizeof(avenrun));
1966 if (r != sizeof(avenrun))
1968 sm_syslog(LOG_ERR, NOQID,
1969 "can't read %s: %s", _PATH_AVENRUN,
1970 r == -1 ? sm_errstring(errno) : "short read");
1975 sm_dprintf("getla: avenrun = %d\n", avenrun);
1976 loadav = (int) (avenrun + FSCALE/2) >> FSHIFT;
1978 sm_dprintf("getla: %d\n", loadav);
1982 #endif /* LA_TYPE == LA_DEVSHORT */
1984 #if LA_TYPE == LA_ALPHAOSF
1987 # include <sys/table.h>
1993 struct tbl_loadavg tab;
1995 if (table(TBL_LOADAVG, 0, &tab, 1, sizeof(tab)) == -1)
1998 sm_dprintf("getla: table %s\n", sm_errstring(errno));
2003 sm_dprintf("getla: scale = %d\n", tab.tl_lscale);
2006 ave = ((tab.tl_avenrun.l[2] + (tab.tl_lscale/2)) /
2009 ave = (int) (tab.tl_avenrun.d[2] + 0.5);
2012 sm_dprintf("getla: %d\n", ave);
2017 #endif /* LA_TYPE == LA_ALPHAOSF */
2019 #if LA_TYPE == LA_PSET
2026 if (pset_getloadavg(PS_MYID, avenrun,
2027 sizeof(avenrun) / sizeof(avenrun[0])) < 0)
2030 sm_dprintf("getla: pset_getloadavg failed: %s",
2031 sm_errstring(errno));
2035 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
2036 return ((int) (avenrun[0] + 0.5));
2039 #endif /* LA_TYPE == LA_PSET */
2041 #if LA_TYPE == LA_ZERO
2047 sm_dprintf("getla: ZERO\n");
2051 #endif /* LA_TYPE == LA_ZERO */
2054 * Copyright 1989 Massachusetts Institute of Technology
2056 * Permission to use, copy, modify, distribute, and sell this software and its
2057 * documentation for any purpose is hereby granted without fee, provided that
2058 * the above copyright notice appear in all copies and that both that
2059 * copyright notice and this permission notice appear in supporting
2060 * documentation, and that the name of M.I.T. not be used in advertising or
2061 * publicity pertaining to distribution of the software without specific,
2062 * written prior permission. M.I.T. makes no representations about the
2063 * suitability of this software for any purpose. It is provided "as is"
2064 * without express or implied warranty.
2066 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
2067 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
2068 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2069 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
2070 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2071 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2073 * Authors: Many and varied...
2076 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
2078 SM_UNUSED(static char rcsid[]) = "@(#)$OrigId: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
2083 # include <apollo/base.h>
2086 int getloadavg( call_data )
2087 caddr_t call_data; /* pointer to (double) return value */
2089 double *avenrun = (double *) call_data;
2094 proc1_$get_loadav(loadav, &st);
2095 *avenrun = loadav[0] / (double) (1 << 16);
2100 ** SM_GETLA -- get the current load average
2109 ** Set CurrentLA to the current load average.
2110 ** Set {load_avg} in GlobalMacros to the current load average.
2118 CurrentLA = getla();
2119 (void) sm_snprintf(labuf, sizeof(labuf), "%d", CurrentLA);
2120 macdefine(&GlobalMacros, A_TEMP, macid("{load_avg}"), labuf);
2123 ** SHOULDQUEUE -- should this message be queued or sent?
2125 ** Compares the message cost to the load average to decide.
2127 ** Note: Do NOT change this API! It is documented in op.me
2128 ** and theoretically the user can change this function...
2131 ** pri -- the priority of the message in question.
2132 ** ct -- the message creation time (unused, but see above).
2135 ** true -- if this message should be queued up for the
2137 ** false -- if the load is low enough to send this message.
2145 shouldqueue(pri, ct)
2152 #endif /* _FFR_MEMSTAT */
2155 sm_dprintf("shouldqueue: CurrentLA=%d, pri=%ld: ",
2159 if (QueueLowMem > 0 &&
2160 sm_memstat_get(MemoryResource, &memfree) >= 0 &&
2161 memfree < QueueLowMem)
2164 sm_dprintf("true (memfree=%ld < QueueLowMem=%ld)\n",
2165 memfree, QueueLowMem);
2168 #endif /* _FFR_MEMSTAT */
2169 if (CurrentLA < QueueLA)
2172 sm_dprintf("false (CurrentLA < QueueLA)\n");
2175 rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
2177 sm_dprintf("%s (by calculation)\n", rval ? "true" : "false");
2182 ** REFUSECONNECTIONS -- decide if connections should be refused
2185 ** e -- the current envelope.
2186 ** dn -- number of daemon.
2187 ** active -- was this daemon actually active?
2190 ** true if incoming SMTP connections should be refused
2192 ** false if we should accept new work.
2195 ** Sets process title when it is rejecting connections.
2199 refuseconnections(e, dn, active)
2204 static time_t lastconn[MAXDAEMONS];
2205 static int conncnt[MAXDAEMONS];
2206 static time_t firstrejtime[MAXDAEMONS];
2207 static time_t nextlogtime[MAXDAEMONS];
2211 #endif /* _FFR_MEMSTAT */
2219 SM_ASSERT(dn < MAXDAEMONS);
2220 if (ConnRateThrottle > 0)
2227 if (now != lastconn[dn])
2232 else if (conncnt[dn]++ > ConnRateThrottle)
2234 #define D_MSG_CRT "deferring connections on daemon %s: %d per second"
2235 /* sleep to flatten out connection load */
2236 sm_setproctitle(true, e, D_MSG_CRT,
2240 sm_syslog(LOG_INFO, NOQID, D_MSG_CRT,
2246 else if (now != lastconn[dn])
2252 if (RefuseLowMem > 0 &&
2253 sm_memstat_get(MemoryResource, &memfree) >= 0 &&
2254 memfree < RefuseLowMem)
2256 # define R_MSG_LM "rejecting connections on daemon %s: free memory: %ld"
2257 sm_setproctitle(true, e, R_MSG_LM, Daemons[dn].d_name, memfree);
2259 sm_syslog(LOG_NOTICE, NOQID, R_MSG_LM,
2260 Daemons[dn].d_name, memfree);
2263 #endif /* _FFR_MEMSTAT */
2265 limit = (Daemons[dn].d_refuseLA != DPO_NOTSET) ?
2266 Daemons[dn].d_refuseLA : RefuseLA;
2267 if (limit > 0 && CurrentLA >= limit)
2271 # define R_MSG_LA "rejecting connections on daemon %s: load average: %d"
2272 # define R2_MSG_LA "have been rejecting connections on daemon %s for %s"
2273 sm_setproctitle(true, e, R_MSG_LA, Daemons[dn].d_name,
2276 sm_syslog(LOG_NOTICE, NOQID, R_MSG_LA,
2277 Daemons[dn].d_name, CurrentLA);
2279 if (firstrejtime[dn] == 0)
2281 firstrejtime[dn] = now;
2282 nextlogtime[dn] = now + RejectLogInterval;
2284 else if (nextlogtime[dn] < now)
2286 sm_syslog(LOG_ERR, NOQID, R2_MSG_LA, Daemons[dn].d_name,
2287 pintvl(now - firstrejtime[dn], true));
2288 nextlogtime[dn] = now + RejectLogInterval;
2293 firstrejtime[dn] = 0;
2295 limit = (Daemons[dn].d_delayLA != DPO_NOTSET) ?
2296 Daemons[dn].d_delayLA : DelayLA;
2297 if (limit > 0 && CurrentLA >= limit)
2300 static time_t log_delay = (time_t) 0;
2302 # define MIN_DELAY_LOG 90 /* wait before logging this again */
2303 # define D_MSG_LA "delaying connections on daemon %s: load average=%d >= %d"
2304 /* sleep to flatten out connection load */
2305 sm_setproctitle(true, e, D_MSG_LA, Daemons[dn].d_name,
2307 if (LogLevel > 8 && (now = curtime()) > log_delay)
2309 sm_syslog(LOG_INFO, NOQID, D_MSG_LA,
2310 Daemons[dn].d_name, CurrentLA, limit);
2311 log_delay = now + MIN_DELAY_LOG;
2316 limit = (Daemons[dn].d_maxchildren != DPO_NOTSET) ?
2317 Daemons[dn].d_maxchildren : MaxChildren;
2318 if (limit > 0 && CurChildren >= limit)
2321 if (CurChildren >= limit)
2323 #define R_MSG_CHILD "rejecting connections on daemon %s: %d children, max %d"
2324 sm_setproctitle(true, e, R_MSG_CHILD,
2325 Daemons[dn].d_name, CurChildren,
2328 sm_syslog(LOG_INFO, NOQID, R_MSG_CHILD,
2329 Daemons[dn].d_name, CurChildren,
2338 ** SETPROCTITLE -- set process title for ps
2341 ** fmt -- a printf style format string.
2342 ** a, b, c -- possible parameters to fmt.
2348 ** Clobbers argv of our main procedure so ps(1) will
2349 ** display the title.
2352 #define SPT_NONE 0 /* don't use it at all */
2353 #define SPT_REUSEARGV 1 /* cover argv with title information */
2354 #define SPT_BUILTIN 2 /* use libc builtin */
2355 #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */
2356 #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */
2357 #define SPT_SYSMIPS 5 /* use sysmips() supported by NEWS-OS 6 */
2358 #define SPT_SCO 6 /* write kernel u. area */
2359 #define SPT_CHANGEARGV 7 /* write our own strings into argv[] */
2362 # define SPT_TYPE SPT_REUSEARGV
2363 #endif /* ! SPT_TYPE */
2366 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
2368 # if SPT_TYPE == SPT_PSTAT
2369 # include <sys/pstat.h>
2370 # endif /* SPT_TYPE == SPT_PSTAT */
2371 # if SPT_TYPE == SPT_PSSTRINGS
2372 # include <machine/vmparam.h>
2373 # include <sys/exec.h>
2374 # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */
2376 # define SPT_TYPE SPT_REUSEARGV
2377 # else /* ! PS_STRINGS */
2378 # ifndef NKPDE /* FreeBSD 2.0 */
2380 typedef unsigned int *pt_entry_t;
2381 # endif /* ! NKPDE */
2382 # endif /* ! PS_STRINGS */
2383 # endif /* SPT_TYPE == SPT_PSSTRINGS */
2385 # if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
2386 # define SETPROC_STATIC static
2387 # else /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2388 # define SETPROC_STATIC
2389 # endif /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2391 # if SPT_TYPE == SPT_SYSMIPS
2392 # include <sys/sysmips.h>
2393 # include <sys/sysnews.h>
2394 # endif /* SPT_TYPE == SPT_SYSMIPS */
2396 # if SPT_TYPE == SPT_SCO
2397 # include <sys/immu.h>
2398 # include <sys/dir.h>
2399 # include <sys/user.h>
2400 # include <sys/fs/s5param.h>
2401 # if PSARGSZ > MAXLINE
2402 # define SPT_BUFSIZE PSARGSZ
2403 # endif /* PSARGSZ > MAXLINE */
2404 # endif /* SPT_TYPE == SPT_SCO */
2406 # ifndef SPT_PADCHAR
2407 # define SPT_PADCHAR ' '
2408 # endif /* ! SPT_PADCHAR */
2410 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
2413 # define SPT_BUFSIZE MAXLINE
2414 #endif /* ! SPT_BUFSIZE */
2419 ** It looks like the Compaq Tru64 5.1A now aligns argv and envp to
2420 ** 64 bit alignment, so unless each piece of argv and envp is a multiple
2421 ** of 8 bytes (including terminating NULL), initsetproctitle() won't use
2422 ** any of the space beyond argv[0]. Be sure to set SPT_ALIGN_SIZE if
2423 ** you use this FFR.
2426 # ifdef SPT_ALIGN_SIZE
2427 # define SPT_ALIGN(x, align) (((((x) + SPT_ALIGN_SIZE) >> (align)) << (align)) - 1)
2428 # else /* SPT_ALIGN_SIZE */
2429 # define SPT_ALIGN(x, align) (x)
2430 # endif /* SPT_ALIGN_SIZE */
2431 #else /* _FFR_SPT_ALIGN */
2432 # define SPT_ALIGN(x, align) (x)
2433 #endif /* _FFR_SPT_ALIGN */
2436 ** Pointers for setproctitle.
2437 ** This allows "ps" listings to give more useful information.
2440 static char **Argv = NULL; /* pointer to argument vector */
2441 static char *LastArgv = NULL; /* end of argv */
2442 #if SPT_TYPE != SPT_BUILTIN
2443 static void setproctitle __P((const char *, ...));
2444 #endif /* SPT_TYPE != SPT_BUILTIN */
2447 initsetproctitle(argc, argv, envp)
2454 extern char **environ;
2457 ** Move the environment so setproctitle can use the space at
2458 ** the top of memory.
2463 for (i = 0; envp[i] != NULL; i++)
2465 environ = (char **) xalloc(sizeof(char *) * (i + 1));
2466 for (i = 0; envp[i] != NULL; i++)
2467 environ[i] = newstr(envp[i]);
2472 ** Save start and extent of argv for setproctitle.
2478 ** Determine how much space we can use for setproctitle.
2479 ** Use all contiguous argv and envp pointers starting at argv[0]
2484 # ifdef SPT_ALIGN_SIZE
2485 for (i = SPT_ALIGN_SIZE; i > 0; i >>= 1)
2487 # endif /* SPT_ALIGN_SIZE */
2488 # endif /* _FFR_SPT_ALIGN */
2490 for (i = 0; i < argc; i++)
2492 if (i == 0 || LastArgv + 1 == argv[i])
2493 LastArgv = argv[i] + SPT_ALIGN(strlen(argv[i]), align);
2495 for (i = 0; LastArgv != NULL && envp != NULL && envp[i] != NULL; i++)
2497 if (LastArgv + 1 == envp[i])
2498 LastArgv = envp[i] + SPT_ALIGN(strlen(envp[i]), align);
2502 #if SPT_TYPE != SPT_BUILTIN
2507 setproctitle(const char *fmt, ...)
2508 # else /* __STDC__ */
2509 setproctitle(fmt, va_alist)
2512 # endif /* __STDC__ */
2514 # if SPT_TYPE != SPT_NONE
2517 SETPROC_STATIC char buf[SPT_BUFSIZE];
2519 # if SPT_TYPE == SPT_PSTAT
2521 # endif /* SPT_TYPE == SPT_PSTAT */
2522 # if SPT_TYPE == SPT_SCO
2525 static int kmem = -1;
2526 static pid_t kmempid = -1;
2528 # endif /* SPT_TYPE == SPT_SCO */
2532 /* print sendmail: heading for grep */
2533 (void) sm_strlcpy(p, "sendmail: ", SPACELEFT(buf, p));
2536 /* print the argument string */
2537 SM_VA_START(ap, fmt);
2538 (void) sm_vsnprintf(p, SPACELEFT(buf, p), fmt, ap);
2541 i = (int) strlen(buf);
2545 # if SPT_TYPE == SPT_PSTAT
2546 pst.pst_command = buf;
2547 pstat(PSTAT_SETCMD, pst, i, 0, 0);
2548 # endif /* SPT_TYPE == SPT_PSTAT */
2549 # if SPT_TYPE == SPT_PSSTRINGS
2550 PS_STRINGS->ps_nargvstr = 1;
2551 PS_STRINGS->ps_argvstr = buf;
2552 # endif /* SPT_TYPE == SPT_PSSTRINGS */
2553 # if SPT_TYPE == SPT_SYSMIPS
2554 sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
2555 # endif /* SPT_TYPE == SPT_SYSMIPS */
2556 # if SPT_TYPE == SPT_SCO
2557 if (kmem < 0 || kmempid != CurrentPid)
2561 kmem = open(_PATH_KMEM, O_RDWR, 0);
2564 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
2565 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
2571 kmempid = CurrentPid;
2573 buf[PSARGSZ - 1] = '\0';
2574 seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
2575 if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
2576 (void) write(kmem, buf, PSARGSZ);
2577 # endif /* SPT_TYPE == SPT_SCO */
2578 # if SPT_TYPE == SPT_REUSEARGV
2579 if (LastArgv == NULL)
2582 if (i > LastArgv - Argv[0] - 2)
2584 i = LastArgv - Argv[0] - 2;
2587 (void) sm_strlcpy(Argv[0], buf, i + 1);
2589 while (p < LastArgv)
2592 # endif /* SPT_TYPE == SPT_REUSEARGV */
2593 # if SPT_TYPE == SPT_CHANGEARGV
2596 # endif /* SPT_TYPE == SPT_CHANGEARGV */
2597 # endif /* SPT_TYPE != SPT_NONE */
2600 #endif /* SPT_TYPE != SPT_BUILTIN */
2602 ** SM_SETPROCTITLE -- set process task and set process title for ps
2604 ** Possibly set process status and call setproctitle() to
2605 ** change the ps display.
2608 ** status -- whether or not to store as process status
2609 ** e -- the current envelope.
2610 ** fmt -- a printf style format string.
2611 ** a, b, c -- possible parameters to fmt.
2620 sm_setproctitle(bool status, ENVELOPE *e, const char *fmt, ...)
2621 #else /* __STDC__ */
2622 sm_setproctitle(status, e, fmt, va_alist)
2627 #endif /* __STDC__ */
2629 char buf[SPT_BUFSIZE];
2632 /* print the argument string */
2633 SM_VA_START(ap, fmt);
2634 (void) sm_vsnprintf(buf, sizeof(buf), fmt, ap);
2638 proc_list_set(CurrentPid, buf);
2640 if (ProcTitlePrefix != NULL)
2642 char prefix[SPT_BUFSIZE];
2644 expand(ProcTitlePrefix, prefix, sizeof(prefix), e);
2645 setproctitle("%s: %s", prefix, buf);
2648 setproctitle("%s", buf);
2651 ** WAITFOR -- wait for a particular process id.
2654 ** pid -- process id to wait for.
2658 ** -1 if pid never shows up.
2676 proc_list_drop(i, st, NULL);
2677 } while ((i >= 0 || errno == EINTR) && i != pid);
2686 ** status -- pointer to status (return value)
2698 # else /* WAITUNION */
2700 # endif /* WAITUNION */
2702 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2704 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2706 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2707 savesig = sm_releasesignal(SIGCHLD);
2708 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2710 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2712 sm_blocksignal(SIGCHLD);
2713 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2715 *status = st.w_status;
2716 # else /* WAITUNION */
2718 # endif /* WAITUNION */
2722 ** REAPCHILD -- pick up the body of my child, lest it become a zombie
2725 ** sig -- the signal that got us here (unused).
2731 ** Picks up extant zombies.
2732 ** Control socket exits may restart/shutdown daemon.
2734 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2735 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2744 int save_errno = errno;
2752 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
2757 # else /* HASWAITPID */
2761 while ((pid = wait3(&status, WNOHANG, (struct rusage *) NULL)) > 0)
2763 st = status.w_status;
2764 # else /* WNOHANG */
2768 ** Catch one zombie -- we will be re-invoked (we hope) if there
2769 ** are more. Unreliable signals probably break this, but this
2770 ** is the "old system" situation -- waitpid or wait3 are to be
2771 ** strongly preferred.
2774 if ((pid = wait(&status)) > 0)
2777 # endif /* WNOHANG */
2778 # endif /* HASWAITPID */
2779 /* Drop PID and check if it was a control socket child */
2780 proc_list_drop(pid, st, NULL);
2782 FIX_SYSV_SIGNAL(sig, reapchild);
2784 return SIGFUNC_RETURN;
2787 ** GETDTSIZE -- return number of file descriptors
2789 ** Only on non-BSD systems
2795 ** size of file descriptor table
2802 # include <sys/resource.h>
2803 #endif /* SOLARIS */
2808 # ifdef RLIMIT_NOFILE
2811 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
2813 # endif /* RLIMIT_NOFILE */
2815 # if HASGETDTABLESIZE
2816 return getdtablesize();
2817 # else /* HASGETDTABLESIZE */
2818 # ifdef _SC_OPEN_MAX
2819 return sysconf(_SC_OPEN_MAX);
2820 # else /* _SC_OPEN_MAX */
2822 # endif /* _SC_OPEN_MAX */
2823 # endif /* HASGETDTABLESIZE */
2826 ** UNAME -- get the UUCP name of this system.
2833 struct utsname *name;
2838 name->nodename[0] = '\0';
2840 /* try /etc/whoami -- one line with the node name */
2841 if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, "/etc/whoami",
2842 SM_IO_RDONLY, NULL)) != NULL)
2844 (void) sm_io_fgets(file, SM_TIME_DEFAULT, name->nodename,
2846 (void) sm_io_close(file, SM_TIME_DEFAULT);
2847 n = strchr(name->nodename, '\n');
2850 if (name->nodename[0] != '\0')
2854 /* try /usr/include/whoami.h -- has a #define somewhere */
2855 if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2856 "/usr/include/whoami.h", SM_IO_RDONLY, NULL))
2861 while (sm_io_fgets(file, SM_TIME_DEFAULT,
2862 buf, sizeof(buf)) >= 0)
2864 if (sm_io_sscanf(buf, "#define sysname \"%*[^\"]\"",
2865 NODE_LENGTH, name->nodename) > 0)
2868 (void) sm_io_close(file, SM_TIME_DEFAULT);
2869 if (name->nodename[0] != '\0')
2875 #endif /* !HASUNAME */
2877 ** INITGROUPS -- initialize groups
2879 ** Stub implementation for System V style systems
2884 initgroups(name, basegid)
2891 #endif /* !HASINITGROUPS */
2893 ** SETGROUPS -- set group list
2895 ** Stub implementation for systems that don't have group lists
2901 setgroups(ngroups, grouplist)
2903 GIDSET_T grouplist[];
2908 #endif /* ! NGROUPS_MAX */
2910 ** SETSID -- set session id (for non-POSIX systems)
2921 fd = open("/dev/tty", O_RDWR, 0);
2924 (void) ioctl(fd, TIOCNOTTY, (char *) 0);
2927 # endif /* TIOCNOTTY */
2930 # else /* SYS5SETPGRP */
2931 return setpgid(0, CurrentPid);
2932 # endif /* SYS5SETPGRP */
2935 #endif /* !HASSETSID */
2937 ** FSYNC -- dummy fsync
2946 return fcntl(fd, F_SETFL, O_SYNC);
2948 /* nothing we can do */
2950 # endif /* O_SYNC */
2953 #endif /* NEEDFSYNC */
2955 ** DGUX_INET_ADDR -- inet_addr for DG/UX
2957 ** Data General DG/UX version of inet_addr returns a struct in_addr
2958 ** instead of a long. This patches things. Only needed on versions
2967 dgux_inet_addr(host)
2970 struct in_addr haddr;
2972 haddr = inet_addr(host);
2973 return haddr.s_addr;
2976 #endif /* DGUX_5_4_2 */
2978 ** GETOPT -- for old systems or systems with bogus implementations
2984 * Copyright (c) 1985 Regents of the University of California.
2985 * All rights reserved. The Berkeley software License Agreement
2986 * specifies the terms and conditions for redistribution.
2991 ** this version hacked to add `atend' flag to allow state machine
2992 ** to reset if invoked by the program to scan args for a 2nd time
2995 # if defined(LIBC_SCCS) && !defined(lint)
2996 static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86";
2997 # endif /* defined(LIBC_SCCS) && !defined(lint) */
3000 ** get option letter from argument vector
3002 # ifdef _CONVEX_SOURCE
3003 extern int optind, opterr, optopt;
3004 extern char *optarg;
3005 # else /* _CONVEX_SOURCE */
3006 int opterr = 1; /* if error message should be printed */
3007 int optind = 1; /* index into parent argv vector */
3008 int optopt = 0; /* character checked for validity */
3009 char *optarg = NULL; /* argument associated with option */
3010 # endif /* _CONVEX_SOURCE */
3012 # define BADCH (int)'?'
3014 # define tell(s) if (opterr) \
3015 {sm_io_fputs(smioerr, SM_TIME_DEFAULT, *nargv); \
3016 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, s); \
3017 (void) sm_io_putc(smioerr, SM_TIME_DEFAULT, optopt); \
3018 (void) sm_io_putc(smioerr, SM_TIME_DEFAULT, '\n'); \
3022 getopt(nargc,nargv,ostr)
3027 static char *place = EMSG; /* option letter processing */
3028 static char atend = 0;
3029 register char *oli = NULL; /* option letter list index */
3035 if(!*place) { /* update scanning pointer */
3036 if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
3040 if (*place == '-') { /* found "--" */
3045 } /* option letter okay? */
3046 if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
3047 if (!*place) ++optind;
3048 tell(": illegal option -- ");
3050 if (oli && *++oli != ':') { /* don't need argument */
3052 if (!*place) ++optind;
3054 else { /* need an argument */
3055 if (*place) optarg = place; /* no white space */
3056 else if (nargc <= ++optind) { /* no arg */
3058 tell(": option requires an argument -- ");
3060 else optarg = nargv[optind]; /* white space */
3064 return optopt; /* dump back option letter */
3067 #endif /* !SM_CONF_GETOPT */
3069 ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use
3072 ** user -- the name of the user we are checking.
3073 ** shell -- the user's shell from /etc/passwd
3076 ** true -- if it is ok to use this for unrestricted access.
3077 ** false -- if the shell is restricted.
3080 #if !HASGETUSERSHELL
3082 # ifndef _PATH_SHELLS
3083 # define _PATH_SHELLS "/etc/shells"
3084 # endif /* ! _PATH_SHELLS */
3086 # if defined(_AIX3) || defined(_AIX4)
3087 # include <userconf.h>
3089 # include <userpw.h>
3090 # endif /* _AIX4 >= 40200 */
3091 # include <usersec.h>
3092 # endif /* defined(_AIX3) || defined(_AIX4) */
3094 static char *DefaultUserShells[] =
3096 "/bin/sh", /* standard shell */
3101 "/bin/csh", /* C shell */
3106 "/usr/bin/rsh", /* restricted Bourne shell */
3107 "/usr/bin/ksh", /* Korn shell */
3108 "/usr/bin/rksh", /* restricted Korn shell */
3110 "/usr/bin/keysh", /* key shell (extended Korn shell) */
3111 "/usr/bin/posix/sh",
3113 "/bin/rsh", /* restricted Bourne shell */
3114 "/bin/ksh", /* Korn shell */
3115 "/bin/rksh", /* restricted Korn shell */
3117 "/usr/bin/keysh", /* key shell (extended Korn shell) */
3121 # endif /* __hpux */
3122 # if defined(_AIX3) || defined(_AIX4)
3123 "/bin/ksh", /* Korn shell */
3125 "/bin/tsh", /* trusted shell */
3127 "/bin/bsh", /* Bourne shell */
3129 # endif /* defined(_AIX3) || defined(_AIX4) */
3130 # if defined(__svr4__) || defined(__svr5__)
3131 "/bin/ksh", /* Korn shell */
3133 # endif /* defined(__svr4__) || defined(__svr5__) */
3135 "/sbin/sh", /* SGI's shells really live in /sbin */
3137 "/sbin/bsh", /* classic Bourne shell */
3140 "/sbin/csh", /* standard csh */
3143 "/sbin/jsh", /* classic Bourne shell w/ job control*/
3146 "/bin/ksh", /* Korn shell */
3149 "/sbin/tcsh", /* Extended csh */
3156 #endif /* !HASGETUSERSHELL */
3158 #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/"
3161 usershellok(user, shell)
3165 # if HASGETUSERSHELL
3167 extern char *getusershell();
3169 if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3174 while ((p = getusershell()) != NULL)
3175 if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
3179 # else /* HASGETUSERSHELL */
3182 # endif /* USEGETCONFATTR */
3183 register SM_FILE_T *shellf;
3186 if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3192 ** Naturally IBM has a "better" idea.....
3194 ** What a crock. This interface isn't documented, it is
3195 ** considered part of the security library (-ls), and it
3196 ** only works if you are running as root (since the list
3197 ** of valid shells is obviously a source of great concern).
3198 ** I recommend that you do NOT define USEGETCONFATTR,
3199 ** especially since you are going to have to set up an
3200 ** /etc/shells anyhow to handle the cases where getconfattr
3204 if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
3208 if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
3214 # endif /* USEGETCONFATTR */
3216 shellf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_SHELLS,
3217 SM_IO_RDONLY, NULL);
3220 /* no /etc/shells; see if it is one of the std shells */
3223 if (errno != ENOENT && LogLevel > 3)
3224 sm_syslog(LOG_ERR, NOQID,
3225 "usershellok: cannot open %s: %s",
3226 _PATH_SHELLS, sm_errstring(errno));
3228 for (d = DefaultUserShells; *d != NULL; d++)
3230 if (strcmp(shell, *d) == 0)
3236 while (sm_io_fgets(shellf, SM_TIME_DEFAULT, buf, sizeof(buf)) >= 0)
3238 register char *p, *q;
3241 while (*p != '\0' && *p != '#' && *p != '/')
3243 if (*p == '#' || *p == '\0')
3246 while (*p != '\0' && *p != '#' && !(isascii(*p) && isspace(*p)))
3249 if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
3251 (void) sm_io_close(shellf, SM_TIME_DEFAULT);
3255 (void) sm_io_close(shellf, SM_TIME_DEFAULT);
3257 # endif /* HASGETUSERSHELL */
3260 ** FREEDISKSPACE -- see how much free space is on the queue filesystem
3262 ** Only implemented if you have statfs.
3265 ** dir -- the directory in question.
3266 ** bsize -- a variable into which the filesystem
3267 ** block size is stored.
3270 ** The number of blocks free on the queue filesystem.
3271 ** -1 if the statfs call fails.
3274 ** Puts the filesystem block size into bsize.
3278 # define SFS_NONE 0 /* no statfs implementation */
3279 # define SFS_USTAT 1 /* use ustat */
3280 # define SFS_4ARGS 2 /* use four-argument statfs call */
3281 # define SFS_VFS 3 /* use <sys/vfs.h> implementation */
3282 # define SFS_MOUNT 4 /* use <sys/mount.h> implementation */
3283 # define SFS_STATFS 5 /* use <sys/statfs.h> implementation */
3284 # define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */
3287 # define SFS_TYPE SFS_NONE
3288 # endif /* ! SFS_TYPE */
3290 # if SFS_TYPE == SFS_USTAT
3292 # endif /* SFS_TYPE == SFS_USTAT */
3293 # if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
3294 # include <sys/statfs.h>
3295 # endif /* SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS */
3296 # if SFS_TYPE == SFS_VFS
3297 # include <sys/vfs.h>
3298 # endif /* SFS_TYPE == SFS_VFS */
3299 # if SFS_TYPE == SFS_MOUNT
3300 # include <sys/mount.h>
3301 # endif /* SFS_TYPE == SFS_MOUNT */
3302 # if SFS_TYPE == SFS_STATVFS
3303 # include <sys/statvfs.h>
3304 # endif /* SFS_TYPE == SFS_STATVFS */
3307 freediskspace(dir, bsize)
3311 # if SFS_TYPE == SFS_NONE
3315 /* assume free space is plentiful */
3316 return (long) LONG_MAX;
3317 # else /* SFS_TYPE == SFS_NONE */
3318 # if SFS_TYPE == SFS_USTAT
3320 struct stat statbuf;
3321 # define FSBLOCKSIZE DEV_BSIZE
3322 # define SFS_BAVAIL f_tfree
3323 # else /* SFS_TYPE == SFS_USTAT */
3324 # if defined(ultrix)
3326 # define SFS_BAVAIL fd_bfreen
3327 # define FSBLOCKSIZE 1024L
3328 # else /* defined(ultrix) */
3329 # if SFS_TYPE == SFS_STATVFS
3331 # define FSBLOCKSIZE fs.f_frsize
3332 # else /* SFS_TYPE == SFS_STATVFS */
3334 # define FSBLOCKSIZE fs.f_bsize
3335 # endif /* SFS_TYPE == SFS_STATVFS */
3336 # endif /* defined(ultrix) */
3337 # endif /* SFS_TYPE == SFS_USTAT */
3339 # define SFS_BAVAIL f_bavail
3340 # endif /* ! SFS_BAVAIL */
3342 # if SFS_TYPE == SFS_USTAT
3343 if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
3344 # else /* SFS_TYPE == SFS_USTAT */
3345 # if SFS_TYPE == SFS_4ARGS
3346 if (statfs(dir, &fs, sizeof(fs), 0) == 0)
3347 # else /* SFS_TYPE == SFS_4ARGS */
3348 # if SFS_TYPE == SFS_STATVFS
3349 if (statvfs(dir, &fs) == 0)
3350 # else /* SFS_TYPE == SFS_STATVFS */
3351 # if defined(ultrix)
3352 if (statfs(dir, &fs) > 0)
3353 # else /* defined(ultrix) */
3354 if (statfs(dir, &fs) == 0)
3355 # endif /* defined(ultrix) */
3356 # endif /* SFS_TYPE == SFS_STATVFS */
3357 # endif /* SFS_TYPE == SFS_4ARGS */
3358 # endif /* SFS_TYPE == SFS_USTAT */
3361 *bsize = FSBLOCKSIZE;
3362 if (fs.SFS_BAVAIL <= 0)
3364 else if (fs.SFS_BAVAIL > LONG_MAX)
3365 return (long) LONG_MAX;
3367 return (long) fs.SFS_BAVAIL;
3370 # endif /* SFS_TYPE == SFS_NONE */
3373 ** ENOUGHDISKSPACE -- is there enough free space on the queue file systems?
3376 ** msize -- the size to check against. If zero, we don't yet
3377 ** know how big the message will be, so just check for
3378 ** a "reasonable" amount.
3379 ** e -- envelope, or NULL -- controls logging
3382 ** true if in every queue group there is at least one
3383 ** queue directory whose file system contains enough free space.
3387 ** If there is not enough disk space and e != NULL
3388 ** then sm_syslog is called.
3392 enoughdiskspace(msize, e)
3401 #endif /* _FFR_TESTS */
3402 if (MinBlocksFree <= 0 && msize <= 0)
3405 sm_dprintf("enoughdiskspace: no threshold\n");
3410 for (i = 0; i < NumQueue; ++i)
3412 if (pickqdir(Queue[i], msize, e) < 0)
3418 ** TRANSIENTERROR -- tell if an error code indicates a transient failure
3420 ** This looks at an errno value and tells if this is likely to
3421 ** go away if retried later.
3424 ** err -- the errno code to classify.
3427 ** true if this is probably transient.
3437 case EIO: /* I/O error */
3438 case ENXIO: /* Device not configured */
3439 case EAGAIN: /* Resource temporarily unavailable */
3440 case ENOMEM: /* Cannot allocate memory */
3441 case ENODEV: /* Operation not supported by device */
3442 case ENFILE: /* Too many open files in system */
3443 case EMFILE: /* Too many open files */
3444 case ENOSPC: /* No space left on device */
3445 case ETIMEDOUT: /* Connection timed out */
3447 case ESTALE: /* Stale NFS file handle */
3450 case ENETDOWN: /* Network is down */
3451 #endif /* ENETDOWN */
3453 case ENETUNREACH: /* Network is unreachable */
3454 #endif /* ENETUNREACH */
3456 case ENETRESET: /* Network dropped connection on reset */
3457 #endif /* ENETRESET */
3459 case ECONNABORTED: /* Software caused connection abort */
3460 #endif /* ECONNABORTED */
3462 case ECONNRESET: /* Connection reset by peer */
3463 #endif /* ECONNRESET */
3465 case ENOBUFS: /* No buffer space available */
3466 #endif /* ENOBUFS */
3468 case ESHUTDOWN: /* Can't send after socket shutdown */
3469 #endif /* ESHUTDOWN */
3471 case ECONNREFUSED: /* Connection refused */
3472 #endif /* ECONNREFUSED */
3474 case EHOSTDOWN: /* Host is down */
3475 #endif /* EHOSTDOWN */
3477 case EHOSTUNREACH: /* No route to host */
3478 #endif /* EHOSTUNREACH */
3480 case EDQUOT: /* Disc quota exceeded */
3483 case EPROCLIM: /* Too many processes */
3484 #endif /* EPROCLIM */
3486 case EUSERS: /* Too many users */
3489 case EDEADLK: /* Resource deadlock avoided */
3490 #endif /* EDEADLK */
3492 case EISCONN: /* Socket already connected */
3493 #endif /* EISCONN */
3495 case EINPROGRESS: /* Operation now in progress */
3496 #endif /* EINPROGRESS */
3498 case EALREADY: /* Operation already in progress */
3499 #endif /* EALREADY */
3501 case EADDRINUSE: /* Address already in use */
3502 #endif /* EADDRINUSE */
3503 #ifdef EADDRNOTAVAIL
3504 case EADDRNOTAVAIL: /* Can't assign requested address */
3505 #endif /* EADDRNOTAVAIL */
3507 case ETXTBSY: /* (Apollo) file locked */
3508 #endif /* ETXTBSY */
3509 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
3510 case ENOSR: /* Out of streams resources */
3511 #endif /* defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) */
3513 case ENOLCK: /* No locks available */
3515 case E_SM_OPENTIMEOUT: /* PSEUDO: open timed out */
3519 /* nope, must be permanent */
3523 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking
3526 ** fd -- the file descriptor of the file.
3527 ** filename -- the file name (for error messages).
3528 ** ext -- the filename extension.
3529 ** type -- type of the lock. Bits can be:
3530 ** LOCK_EX -- exclusive lock.
3531 ** LOCK_NB -- non-blocking.
3532 ** LOCK_UN -- unlock.
3535 ** true if the lock was acquired.
3540 lockfile(fd, filename, ext, type)
3555 memset(&lfd, '\0', sizeof(lfd));
3556 if (bitset(LOCK_UN, type))
3557 lfd.l_type = F_UNLCK;
3558 else if (bitset(LOCK_EX, type))
3559 lfd.l_type = F_WRLCK;
3561 lfd.l_type = F_RDLCK;
3563 if (bitset(LOCK_NB, type))
3569 sm_dprintf("lockfile(%s%s, action=%d, type=%d): ",
3570 filename, ext, action, lfd.l_type);
3572 while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
3577 sm_dprintf("SUCCESS\n");
3583 sm_dprintf("(%s) ", sm_errstring(save_errno));
3586 ** On SunOS, if you are testing using -oQ/tmp/mqueue or
3587 ** -oA/tmp/aliases or anything like that, and /tmp is mounted
3588 ** as type "tmp" (that is, served from swap space), the
3589 ** previous fcntl will fail with "Invalid argument" errors.
3590 ** Since this is fairly common during testing, we will assume
3591 ** that this indicates that the lock is successfully grabbed.
3594 if (save_errno == EINVAL)
3597 sm_dprintf("SUCCESS\n");
3601 if (!bitset(LOCK_NB, type) ||
3602 (save_errno != EACCES && save_errno != EAGAIN))
3604 int omode = fcntl(fd, F_GETFL, 0);
3605 uid_t euid = geteuid();
3608 syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%ld)",
3609 filename, ext, fd, type, omode, (long) euid);
3610 dumpfd(fd, true, true);
3612 # else /* !HASFLOCK */
3617 sm_dprintf("lockfile(%s%s, type=%o): ", filename, ext, type);
3619 while ((i = flock(fd, type)) < 0 && errno == EINTR)
3624 sm_dprintf("SUCCESS\n");
3630 sm_dprintf("(%s) ", sm_errstring(save_errno));
3632 if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
3634 int omode = fcntl(fd, F_GETFL, 0);
3635 uid_t euid = geteuid();
3638 syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%ld)",
3639 filename, ext, fd, type, omode, (long) euid);
3640 dumpfd(fd, true, true);
3642 # endif /* !HASFLOCK */
3644 sm_dprintf("FAIL\n");
3649 ** CHOWNSAFE -- tell if chown is "safe" (executable only by root)
3651 ** Unfortunately, given that we can't predict other systems on which
3652 ** a remote mounted (NFS) filesystem will be mounted, the answer is
3653 ** almost always that this is unsafe.
3655 ** Note also that many operating systems have non-compliant
3656 ** implementations of the _POSIX_CHOWN_RESTRICTED variable and the
3657 ** fpathconf() routine. According to IEEE 1003.1-1990, if
3658 ** _POSIX_CHOWN_RESTRICTED is defined and not equal to -1, then
3659 ** no non-root process can give away the file. However, vendors
3660 ** don't take NFS into account, so a comfortable value of
3661 ** _POSIX_CHOWN_RESTRICTED tells us nothing.
3663 ** Also, some systems (e.g., IRIX 6.2) return 1 from fpathconf()
3664 ** even on files where chown is not restricted. Many systems get
3665 ** this wrong on NFS-based filesystems (that is, they say that chown
3666 ** is restricted [safe] on NFS filesystems where it may not be, since
3667 ** other systems can access the same filesystem and do file giveaway;
3668 ** only the NFS server knows for sure!) Hence, it is important to
3669 ** get the value of SAFENFSPATHCONF correct -- it should be defined
3670 ** _only_ after testing (see test/t_pathconf.c) a system on an unsafe
3671 ** NFS-based filesystem to ensure that you can get meaningful results.
3672 ** If in doubt, assume unsafe!
3674 ** You may also need to tweak IS_SAFE_CHOWN -- it should be a
3675 ** condition indicating whether the return from pathconf indicates
3676 ** that chown is safe (typically either > 0 or >= 0 -- there isn't
3677 ** even any agreement about whether a zero return means that a file
3678 ** is or is not safe). It defaults to "> 0".
3680 ** If the parent directory is safe (writable only by owner back
3681 ** to the root) then we can relax slightly and trust fpathconf
3682 ** in more circumstances. This is really a crock -- if this is an
3683 ** NFS mounted filesystem then we really know nothing about the
3684 ** underlying implementation. However, most systems pessimize and
3685 ** return an error (EINVAL or EOPNOTSUPP) on NFS filesystems, which
3686 ** we interpret as unsafe, as we should. Thus, this heuristic gets
3687 ** us into a possible problem only on systems that have a broken
3688 ** pathconf implementation and which are also poorly configured
3689 ** (have :include: files in group- or world-writable directories).
3692 ** fd -- the file descriptor to check.
3693 ** safedir -- set if the parent directory is safe.
3696 ** true -- if the chown(2) operation is "safe" -- that is,
3697 ** only root can chown the file to an arbitrary user.
3698 ** false -- if an arbitrary user can give away a file.
3701 #ifndef IS_SAFE_CHOWN
3702 # define IS_SAFE_CHOWN > 0
3703 #endif /* ! IS_SAFE_CHOWN */
3706 chownsafe(fd, safedir)
3710 # if (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && \
3711 (defined(_PC_CHOWN_RESTRICTED) || defined(_GNU_TYPES_H))
3714 /* give the system administrator a chance to override */
3715 if (bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail))
3719 ** Some systems (e.g., SunOS) seem to have the call and the
3720 ** #define _PC_CHOWN_RESTRICTED, but don't actually implement
3721 ** the call. This heuristic checks for that.
3725 rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
3726 # if SAFENFSPATHCONF
3727 return errno == 0 && rval IS_SAFE_CHOWN;
3728 # else /* SAFENFSPATHCONF */
3729 return safedir && errno == 0 && rval IS_SAFE_CHOWN;
3730 # endif /* SAFENFSPATHCONF */
3731 # else /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3732 return bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail);
3733 # endif /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3736 ** RESETLIMITS -- reset system controlled resource limits
3738 ** This is to avoid denial-of-service attacks
3748 # ifdef RLIMIT_NEEDS_SYS_TIME_H
3749 # include <sm/time.h>
3750 # endif /* RLIMIT_NEEDS_SYS_TIME_H */
3751 # include <sys/resource.h>
3752 #endif /* HASSETRLIMIT */
3760 lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
3761 (void) setrlimit(RLIMIT_CPU, &lim);
3762 (void) setrlimit(RLIMIT_FSIZE, &lim);
3763 # ifdef RLIMIT_NOFILE
3764 lim.rlim_cur = lim.rlim_max = FD_SETSIZE;
3765 (void) setrlimit(RLIMIT_NOFILE, &lim);
3766 # endif /* RLIMIT_NOFILE */
3767 #else /* HASSETRLIMIT */
3769 (void) ulimit(2, 0x3fffff);
3770 (void) ulimit(4, FD_SETSIZE);
3771 # endif /* HASULIMIT */
3772 #endif /* HASSETRLIMIT */
3776 ** SETVENDOR -- process vendor code from V configuration line
3779 ** vendor -- string representation of vendor.
3783 ** false -- if vendor code could not be processed.
3786 ** It is reasonable to set mode flags here to tweak
3787 ** processing in other parts of the code if necessary.
3788 ** For example, if you are a vendor that uses $%y to
3789 ** indicate YP lookups, you could enable that here.
3796 if (sm_strcasecmp(vendor, "Berkeley") == 0)
3798 VendorCode = VENDOR_BERKELEY;
3802 /* add vendor extensions here */
3804 #ifdef SUN_EXTENSIONS
3805 if (sm_strcasecmp(vendor, "Sun") == 0)
3807 VendorCode = VENDOR_SUN;
3810 #endif /* SUN_EXTENSIONS */
3812 if (sm_strcasecmp(vendor, "Digital") == 0)
3814 VendorCode = VENDOR_DEC;
3819 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3820 if (sm_strcasecmp(vendor, VENDOR_NAME) == 0)
3822 VendorCode = VENDOR_CODE;
3825 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3830 ** GETVENDOR -- return vendor name based on vendor code
3833 ** vendorcode -- numeric representation of vendor.
3836 ** string containing vendor name.
3840 getvendor(vendorcode)
3843 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3845 ** Can't have the same switch case twice so need to
3846 ** handle VENDOR_CODE outside of switch. It might
3847 ** match one of the existing VENDOR_* codes.
3850 if (vendorcode == VENDOR_CODE)
3852 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3856 case VENDOR_BERKELEY:
3868 case VENDOR_SENDMAIL:
3876 ** VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
3878 ** Vendor_pre_defaults is called before reading the configuration
3879 ** file; vendor_post_defaults is called immediately after.
3882 ** e -- the global environment to initialize.
3889 int DefShareUid; /* default share uid to run as -- unused??? */
3890 #endif /* SHARE_V1 */
3893 vendor_pre_defaults(e)
3897 /* OTHERUID is defined in shares.h, do not be alarmed */
3898 DefShareUid = OTHERUID;
3899 #endif /* SHARE_V1 */
3900 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3901 sun_pre_defaults(e);
3902 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3905 ** stupid domain/os can't even open
3906 ** /etc/mail/sendmail.cf without this
3909 sm_setuserenv("ISP", NULL);
3910 sm_setuserenv("SYSTYPE", NULL);
3916 vendor_post_defaults(e)
3920 /* Makes sure the SOCK environment variable remains */
3921 sm_setuserenv("SOCK", NULL);
3922 #endif /* __QNX__ */
3923 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3924 sun_post_defaults(e);
3925 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3928 ** VENDOR_DAEMON_SETUP -- special vendor setup needed for daemon mode
3932 vendor_daemon_setup(e)
3936 (void) setlogin(RunAsUserName);
3937 #endif /* HASSETLOGIN */
3939 if (getluid() != -1)
3941 usrerr("Daemon cannot have LUID");
3942 finis(false, true, EX_USAGE);
3944 #endif /* SECUREWARE */
3947 ** VENDOR_SET_UID -- do setup for setting a user id
3949 ** This is called when we are still root.
3952 ** uid -- the uid we are about to become.
3963 ** We need to setup the share groups (lnodes)
3964 ** and add auditing information (luid's)
3965 ** before we loose our ``root''ness.
3968 if (setupshares(uid, syserr) != 0)
3969 syserr("Unable to set up shares");
3970 #endif /* SHARE_V1 */
3972 (void) setup_secure(uid);
3973 #endif /* SECUREWARE */
3976 ** VALIDATE_CONNECTION -- check connection for rationality
3978 ** If the connection is rejected, this routine should log an
3979 ** appropriate message -- but should never issue any SMTP protocol.
3982 ** sap -- a pointer to a SOCKADDR naming the peer.
3983 ** hostname -- the name corresponding to sap.
3984 ** e -- the current envelope.
3987 ** error message from rejection.
3988 ** NULL if not rejected.
3994 /* tcpwrappers does no logging, but you still have to declare these -- ugh */
3995 int allow_severity = LOG_INFO;
3996 int deny_severity = LOG_NOTICE;
3997 #endif /* TCPWRAPPERS */
4000 validate_connection(sap, hostname, e)
4008 extern int hosts_ctl();
4009 #endif /* TCPWRAPPERS */
4012 sm_dprintf("validate_connection(%s, %s)\n",
4013 hostname, anynet_ntoa(sap));
4015 connection_rate_check(sap, e);
4016 if (rscheck("check_relay", hostname, anynet_ntoa(sap), e,
4017 RSF_RMCOMM|RSF_COUNT, 3, NULL, NOQID, NULL, NULL) != EX_OK)
4019 static char reject[BUFSIZ*2];
4020 extern char MsgBuf[];
4023 sm_dprintf(" ... validate_connection: BAD (rscheck)\n");
4025 if (strlen(MsgBuf) >= 3)
4026 (void) sm_strlcpy(reject, MsgBuf, sizeof(reject));
4028 (void) sm_strlcpy(reject, "Access denied", sizeof(reject));
4034 if (hostname[0] == '[' && hostname[strlen(hostname) - 1] == ']')
4038 addr = anynet_ntoa(sap);
4041 /* TCP/Wrappers don't want the IPv6: protocol label */
4042 if (addr != NULL && sm_strncasecmp(addr, "IPv6:", 5) == 0)
4044 # endif /* NETINET6 */
4046 if (!hosts_ctl("sendmail", host, addr, STRING_UNKNOWN))
4049 sm_dprintf(" ... validate_connection: BAD (tcpwrappers)\n");
4051 sm_syslog(LOG_NOTICE, e->e_id,
4052 "tcpwrappers (%s, %s) rejection",
4054 return "Access denied";
4056 #endif /* TCPWRAPPERS */
4058 sm_dprintf(" ... validate_connection: OK\n");
4063 ** STRTOL -- convert string to long integer
4065 ** For systems that don't have it in the C library.
4067 ** This is taken verbatim from the 4.4-Lite C library.
4072 # if defined(LIBC_SCCS) && !defined(lint)
4073 static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
4074 # endif /* defined(LIBC_SCCS) && !defined(lint) */
4077 ** Convert a string to a long integer.
4079 ** Ignores `locale' stuff. Assumes that the upper and lower case
4080 ** alphabets and digits are each contiguous.
4084 strtol(nptr, endptr, base)
4089 register const char *s = nptr;
4090 register unsigned long acc;
4092 register unsigned long cutoff;
4093 register int neg = 0, any, cutlim;
4096 ** Skip white space and pick up leading +/- sign if any.
4097 ** If base is 0, allow 0x for hex and 0 for octal, else
4098 ** assume decimal; if base is already 16, allow 0x.
4102 } while (isascii(c) && isspace(c));
4106 } else if (c == '+')
4108 if ((base == 0 || base == 16) &&
4109 c == '0' && (*s == 'x' || *s == 'X')) {
4115 base = c == '0' ? 8 : 10;
4118 ** Compute the cutoff value between legal numbers and illegal
4119 ** numbers. That is the largest legal value, divided by the
4120 ** base. An input number that is greater than this value, if
4121 ** followed by a legal input character, is too big. One that
4122 ** is equal to this value may be valid or not; the limit
4123 ** between valid and invalid numbers is then based on the last
4124 ** digit. For instance, if the range for longs is
4125 ** [-2147483648..2147483647] and the input base is 10,
4126 ** cutoff will be set to 214748364 and cutlim to either
4127 ** 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
4128 ** a value > 214748364, or equal but the next digit is > 7 (or 8),
4129 ** the number is too big, and we will return a range error.
4131 ** Set any if any `digits' consumed; make it negative to indicate
4134 cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
4135 cutlim = cutoff % (unsigned long) base;
4136 cutoff /= (unsigned long) base;
4137 for (acc = 0, any = 0;; c = *s++) {
4138 if (isascii(c) && isdigit(c))
4140 else if (isascii(c) && isalpha(c))
4141 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4146 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
4155 acc = neg ? LONG_MIN : LONG_MAX;
4160 *endptr = (char *)(any ? s - 1 : nptr);
4164 #endif /* NEEDSTRTOL */
4166 ** STRSTR -- find first substring in string
4169 ** big -- the big (full) string.
4170 ** little -- the little (sub) string.
4173 ** A pointer to the first instance of little in big.
4174 ** big if little is the null string.
4175 ** NULL if little is not contained in big.
4185 register char *p = big;
4188 if (*little == '\0')
4192 while ((p = strchr(p, *little)) != NULL)
4194 if (strncmp(p, little, l) == 0)
4201 #endif /* NEEDSTRSTR */
4203 ** SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
4205 ** Some operating systems have weird problems with the gethostbyXXX
4206 ** routines. For example, Solaris versions at least through 2.3
4207 ** don't properly deliver a canonical h_name field. This tries to
4208 ** work around these problems.
4210 ** Support IPv6 as well as IPv4.
4213 #if NETINET6 && NEEDSGETIPNODE
4216 # define AI_DEFAULT 0 /* dummy */
4217 # endif /* ! AI_DEFAULT */
4218 # ifndef AI_ADDRCONFIG
4219 # define AI_ADDRCONFIG 0 /* dummy */
4220 # endif /* ! AI_ADDRCONFIG */
4221 # ifndef AI_V4MAPPED
4222 # define AI_V4MAPPED 0 /* dummy */
4223 # endif /* ! AI_V4MAPPED */
4225 # define AI_ALL 0 /* dummy */
4226 # endif /* ! AI_ALL */
4228 static struct hostent *
4229 sm_getipnodebyname(name, family, flags, err)
4236 # if HAS_GETHOSTBYNAME2
4238 h = gethostbyname2(name, family);
4243 # else /* HAS_GETHOSTBYNAME2 */
4246 if (family == AF_INET6)
4248 /* From RFC2133, section 6.1 */
4249 resv6 = bitset(RES_USE_INET6, _res.options);
4250 _res.options |= RES_USE_INET6;
4253 h = gethostbyname(name);
4255 _res.options &= ~RES_USE_INET6;
4257 /* the function is supposed to return only the requested family */
4258 if (h != NULL && h->h_addrtype != family)
4262 # endif /* NETINET6 */
4269 # endif /* HAS_GETHOSTBYNAME2 */
4272 static struct hostent *
4273 sm_getipnodebyaddr(addr, len, family, err)
4282 h = gethostbyaddr(addr, len, family);
4292 ** Stub routine -- if they don't have getipnodeby*(),
4293 ** they probably don't have the free routine either.
4298 #endif /* NETINET6 && NEEDSGETIPNODE */
4301 sm_gethostbyname(name, family)
4306 struct hostent *h = NULL;
4307 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
4308 # if SOLARIS == 20300 || SOLARIS == 203
4309 static struct hostent hp;
4310 static char buf[1000];
4311 extern struct hostent *_switch_gethostbyname_r();
4314 sm_dprintf("_switch_gethostbyname_r(%s)... ", name);
4315 h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
4317 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4318 extern struct hostent *__switch_gethostbyname();
4321 sm_dprintf("__switch_gethostbyname(%s)... ", name);
4322 h = __switch_gethostbyname(name);
4324 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4325 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4328 # ifndef SM_IPNODEBYNAME_FLAGS
4329 /* For IPv4-mapped addresses, use: AI_DEFAULT|AI_ALL */
4330 # define SM_IPNODEBYNAME_FLAGS AI_ADDRCONFIG
4331 # endif /* SM_IPNODEBYNAME_FLAGS */
4333 int flags = SM_IPNODEBYNAME_FLAGS;
4335 # endif /* NETINET6 */
4336 char *maptype[MAXMAPSTACK];
4337 short mapreturn[MAXMAPACTIONS];
4341 sm_dprintf("sm_gethostbyname(%s, %d)... ", name, family);
4344 # if ADDRCONFIG_IS_BROKEN
4345 flags &= ~AI_ADDRCONFIG;
4346 # endif /* ADDRCONFIG_IS_BROKEN */
4347 h = sm_getipnodebyname(name, family, flags, &err);
4348 SM_SET_H_ERRNO(err);
4349 # else /* NETINET6 */
4350 h = gethostbyname(name);
4351 # endif /* NETINET6 */
4357 sm_dprintf("failure\n");
4359 nmaps = switch_map_find("hosts", maptype, mapreturn);
4360 while (--nmaps >= 0)
4362 if (strcmp(maptype[nmaps], "nis") == 0 ||
4363 strcmp(maptype[nmaps], "files") == 0)
4369 /* try short name */
4370 if (strlen(name) > sizeof(hbuf) - 1)
4375 (void) sm_strlcpy(hbuf, name, sizeof(hbuf));
4376 (void) shorten_hostname(hbuf);
4378 /* if it hasn't been shortened, there's no point */
4379 if (strcmp(hbuf, name) != 0)
4382 sm_dprintf("sm_gethostbyname(%s, %d)... ",
4386 h = sm_getipnodebyname(hbuf, family, flags, &err);
4387 SM_SET_H_ERRNO(err);
4389 # else /* NETINET6 */
4390 h = gethostbyname(hbuf);
4392 # endif /* NETINET6 */
4396 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4398 /* the function is supposed to return only the requested family */
4399 if (h != NULL && h->h_addrtype != family)
4403 # endif /* NETINET6 */
4405 SM_SET_H_ERRNO(NO_DATA);
4411 sm_dprintf("failure\n");
4414 sm_dprintf("%s\n", h->h_name);
4420 struct in6_addr ia6;
4421 char buf6[INET6_ADDRSTRLEN];
4422 #endif /* NETINET6 */
4424 if (h->h_aliases != NULL)
4425 for (i = 0; h->h_aliases[i] != NULL;
4427 sm_dprintf("\talias: %s\n",
4429 for (i = 0; h->h_addr_list[i] != NULL; i++)
4435 if (h->h_addrtype == AF_INET6)
4437 memmove(&ia6, h->h_addr_list[i],
4439 addr = anynet_ntop(&ia6,
4440 buf6, sizeof(buf6));
4443 #endif /* NETINET6 */
4444 /* "else" in #if code above */
4446 memmove(&ia, h->h_addr_list[i],
4448 addr = (char *) inet_ntoa(ia);
4451 sm_dprintf("\taddr: %s\n", addr);
4461 sm_gethostbyaddr(addr, len, type)
4469 if (type == AF_INET6 &&
4470 IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *) addr))
4472 /* Avoid reverse lookup for IPv6 unspecified address */
4473 SM_SET_H_ERRNO(HOST_NOT_FOUND);
4476 #endif /* NETINET6 */
4478 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204)
4479 # if SOLARIS == 20300 || SOLARIS == 203
4481 static struct hostent he;
4482 static char buf[1000];
4483 extern struct hostent *_switch_gethostbyaddr_r();
4485 hp = _switch_gethostbyaddr_r(addr, len, type, &he,
4486 buf, sizeof(buf), &h_errno);
4488 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4490 extern struct hostent *__switch_gethostbyaddr();
4492 hp = __switch_gethostbyaddr(addr, len, type);
4494 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4495 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4500 hp = sm_getipnodebyaddr(addr, len, type, &err);
4501 SM_SET_H_ERRNO(err);
4503 # else /* NETINET6 */
4504 hp = gethostbyaddr(addr, len, type);
4505 # endif /* NETINET6 */
4506 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4510 ** SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
4518 extern struct passwd *_getpwnam_shadow(const char *, const int);
4520 return _getpwnam_shadow(user, 0);
4522 return getpwnam(user);
4530 #if defined(_AIX4) && 0
4531 extern struct passwd *_getpwuid_shadow(const int, const int);
4533 return _getpwuid_shadow(uid,0);
4534 #else /* defined(_AIX4) && 0 */
4535 return getpwuid(uid);
4536 #endif /* defined(_AIX4) && 0 */
4539 ** SECUREWARE_SETUP_SECURE -- Convex SecureWare setup
4541 ** Set up the trusted computing environment for C2 level security
4542 ** under SecureWare.
4545 ** uid -- uid of the user to initialize in the TCB
4551 ** Initialized the user in the trusted computing base
4556 # include <sys/security.h>
4560 secureware_setup_secure(uid)
4565 if (getluid() != -1)
4568 if ((rc = set_secure_info(uid)) != SSI_GOOD_RETURN)
4572 case SSI_NO_PRPW_ENTRY:
4573 syserr("No protected passwd entry, uid = %d",
4578 syserr("Account has been disabled, uid = %d",
4583 syserr("Account has been retired, uid = %d",
4587 case SSI_BAD_SET_LUID:
4588 syserr("Could not set LUID, uid = %d", (int) uid);
4591 case SSI_BAD_SET_PRIVS:
4592 syserr("Could not set kernel privs, uid = %d",
4596 syserr("Unknown return code (%d) from set_secure_info(%d)",
4600 finis(false, true, EX_NOPERM);
4603 #endif /* SECUREWARE */
4605 ** ADD_HOSTNAMES -- Add a hostname to class 'w' based on IP address
4607 ** Add hostnames to class 'w' based on the IP address read from
4608 ** the network interface.
4611 ** sa -- a pointer to a SOCKADDR containing the address
4614 ** 0 if successful, -1 if host lookup fails.
4623 char hnb[MAXHOSTNAMELEN];
4625 /* lookup name with IP address */
4626 switch (sa->sa.sa_family)
4630 hp = sm_gethostbyaddr((char *) &sa->sin.sin_addr,
4631 sizeof(sa->sin.sin_addr),
4634 #endif /* NETINET */
4638 hp = sm_gethostbyaddr((char *) &sa->sin6.sin6_addr,
4639 sizeof(sa->sin6.sin6_addr),
4642 #endif /* NETINET6 */
4645 /* Give warning about unsupported family */
4647 sm_syslog(LOG_WARNING, NOQID,
4648 "Unsupported address family %d: %.100s",
4649 sa->sa.sa_family, anynet_ntoa(sa));
4655 int save_errno = errno;
4658 #if NETINET && defined(IN_LINKLOCAL)
4659 !(sa->sa.sa_family == AF_INET &&
4660 IN_LINKLOCAL(ntohl(sa->sin.sin_addr.s_addr))) &&
4661 #endif /* NETINET && defined(IN_LINKLOCAL) */
4663 !(sa->sa.sa_family == AF_INET6 &&
4664 IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr)) &&
4665 #endif /* NETINET6 */
4667 sm_syslog(LOG_WARNING, NOQID,
4668 "gethostbyaddr(%.100s) failed: %d",
4672 #else /* NAMED_BIND */
4674 #endif /* NAMED_BIND */
4680 /* save its cname */
4681 if (!wordinclass((char *) hp->h_name, 'w'))
4683 setclass('w', (char *) hp->h_name);
4685 sm_dprintf("\ta.k.a.: %s\n", hp->h_name);
4687 if (sm_snprintf(hnb, sizeof(hnb), "[%s]", hp->h_name) <
4689 && !wordinclass((char *) hnb, 'w'))
4695 sm_dprintf("\ta.k.a.: %s (already in $=w)\n", hp->h_name);
4698 /* save all it aliases name */
4699 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
4701 if (!wordinclass(*ha, 'w'))
4705 sm_dprintf("\ta.k.a.: %s\n", *ha);
4706 if (sm_snprintf(hnb, sizeof(hnb),
4707 "[%s]", *ha) < sizeof(hnb) &&
4708 !wordinclass((char *) hnb, 'w'))
4714 sm_dprintf("\ta.k.a.: %s (already in $=w)\n",
4720 #endif /* NETINET6 */
4724 ** LOAD_IF_NAMES -- load interface-specific names into $=w
4733 ** Loads $=w with the names of all the interfaces.
4737 # define SIOCGIFCONF_IS_BROKEN 1 /* XXX */
4738 #endif /* !NETINET */
4740 #if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
4744 # include <sm/time.h>
4745 # endif /* ! SUNOS403 */
4746 # if (_AIX4 >= 40300) && !defined(_NET_IF_H)
4748 # endif /* (_AIX4 >= 40300) && !defined(_NET_IF_H) */
4749 # include <net/if.h>
4750 #endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
4755 # if NETINET6 && defined(SIOCGLIFCONF)
4759 ** Unfortunately, HP has changed all of the structures,
4760 ** making life difficult for implementors.
4763 # define lifconf if_laddrconf
4764 # define lifc_len iflc_len
4765 # define lifc_buf iflc_buf
4766 # define lifreq if_laddrreq
4767 # define lifr_addr iflr_addr
4768 # define lifr_name iflr_name
4769 # define lifr_flags iflr_flags
4770 # define ss_family sa_family
4772 # endif /* __hpux */
4779 struct lifconf lifc;
4782 # endif /* SIOCGLIFNUM */
4784 s = socket(InetMode, SOCK_DGRAM, 0);
4788 /* get the list of known IP address from the kernel */
4790 i = ioctl(s, SIOCGIFNUM, (char *) &numifs);
4791 # endif /* __hpux */
4793 lifn.lifn_family = AF_UNSPEC;
4794 lifn.lifn_flags = 0;
4795 i = ioctl(s, SIOCGLIFNUM, (char *)&lifn);
4796 numifs = lifn.lifn_count;
4797 # endif /* SIOCGLIFNUM */
4799 # if defined(__hpux) || defined(SIOCGLIFNUM)
4802 /* can't get number of interfaces -- fall back */
4804 sm_dprintf("SIOCGLIFNUM failed: %s\n",
4805 sm_errstring(errno));
4808 else if (tTd(0, 42))
4809 sm_dprintf("system has %d interfaces\n", numifs);
4811 # endif /* defined(__hpux) || defined(SIOCGLIFNUM) */
4812 numifs = MAXINTERFACES;
4820 len = lifc.lifc_len = numifs * sizeof(struct lifreq);
4821 buf = lifc.lifc_buf = xalloc(lifc.lifc_len);
4823 lifc.lifc_family = AF_UNSPEC;
4824 lifc.lifc_flags = 0;
4825 # endif /* ! __hpux */
4826 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0)
4829 sm_dprintf("SIOCGLIFCONF failed: %s\n",
4830 sm_errstring(errno));
4836 /* scan the list of IP address */
4838 sm_dprintf("scanning for interface specific names, lifc_len=%ld\n",
4841 for (i = 0; i < len && i >= 0; )
4844 struct lifreq *ifr = (struct lifreq *)&buf[i];
4845 SOCKADDR *sa = (SOCKADDR *) &ifr->lifr_addr;
4846 int af = ifr->lifr_addr.ss_family;
4849 struct in6_addr ia6;
4851 # ifdef SIOCGLIFFLAGS
4853 # endif /* SIOCGLIFFLAGS */
4855 char buf6[INET6_ADDRSTRLEN];
4858 ** We must close and recreate the socket each time
4859 ** since we don't know what type of socket it is now
4860 ** (each status function may change it).
4865 s = socket(af, SOCK_DGRAM, 0);
4868 sm_free(buf); /* XXX */
4873 ** If we don't have a complete ifr structure,
4874 ** don't try to use it.
4877 if ((len - i) < sizeof(*ifr))
4880 # ifdef BSD4_4_SOCKADDR
4881 if (sa->sa.sa_len > sizeof(ifr->lifr_addr))
4882 i += sizeof(ifr->lifr_name) + sa->sa.sa_len;
4884 # endif /* BSD4_4_SOCKADDR */
4886 /* fix for IPv6 size differences */
4887 i += sizeof(ifr->ifr_name) +
4888 max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
4894 sm_dprintf("%s\n", anynet_ntoa(sa));
4896 if (af != AF_INET && af != AF_INET6)
4899 # ifdef SIOCGLIFFLAGS
4900 memset(&ifrf, '\0', sizeof(struct lifreq));
4901 (void) sm_strlcpy(ifrf.lifr_name, ifr->lifr_name,
4902 sizeof(ifrf.lifr_name));
4903 if (ioctl(s, SIOCGLIFFLAGS, (char *) &ifrf) < 0)
4906 sm_dprintf("SIOCGLIFFLAGS failed: %s\n",
4907 sm_errstring(errno));
4911 name = ifr->lifr_name;
4912 flags = ifrf.lifr_flags;
4915 sm_dprintf("\tflags: %lx\n", (unsigned long) flags);
4917 if (!bitset(IFF_UP, flags))
4919 # endif /* SIOCGLIFFLAGS */
4923 /* extract IP address from the list*/
4927 SETV6LOOPBACKADDRFOUND(*sa);
4929 /* convert into proper scoped address */
4930 if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
4931 IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
4932 sa->sin6.sin6_scope_id == 0)
4934 struct in6_addr *ia6p;
4936 ia6p = &sa->sin6.sin6_addr;
4937 sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
4938 ((unsigned int)ia6p->s6_addr[2] << 8));
4939 ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
4941 # endif /* __KAME__ */
4942 ia6 = sa->sin6.sin6_addr;
4943 if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
4945 addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
4946 message("WARNING: interface %s is UP with %s address",
4947 name, addr == NULL ? "(NULL)" : addr);
4951 /* save IP address in text from */
4952 addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
4954 (void) sm_snprintf(ip_addr, sizeof(ip_addr),
4956 (int) sizeof(ip_addr) - 3,
4961 ia = sa->sin.sin_addr;
4962 if (ia.s_addr == INADDR_ANY ||
4963 ia.s_addr == INADDR_NONE)
4965 message("WARNING: interface %s is UP with %s address",
4966 name, inet_ntoa(ia));
4970 /* save IP address in text from */
4971 (void) sm_snprintf(ip_addr, sizeof(ip_addr), "[%.*s]",
4972 (int) sizeof(ip_addr) - 3, inet_ntoa(ia));
4976 if (*ip_addr == '\0')
4979 if (!wordinclass(ip_addr, 'w'))
4981 setclass('w', ip_addr);
4983 sm_dprintf("\ta.k.a.: %s\n", ip_addr);
4986 # ifdef SIOCGLIFFLAGS
4987 /* skip "loopback" interface "lo" */
4988 if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
4989 bitset(IFF_LOOPBACK, flags))
4991 # endif /* SIOCGLIFFLAGS */
4992 (void) add_hostnames(sa);
4994 sm_free(buf); /* XXX */
4996 # else /* NETINET6 && defined(SIOCGLIFCONF) */
4997 # if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
5003 s = socket(AF_INET, SOCK_DGRAM, 0);
5007 /* get the list of known IP address from the kernel */
5008 # if defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN
5009 if (ioctl(s, SIOCGIFNUM, (char *) &numifs) < 0)
5011 /* can't get number of interfaces -- fall back */
5013 sm_dprintf("SIOCGIFNUM failed: %s\n",
5014 sm_errstring(errno));
5017 else if (tTd(0, 42))
5018 sm_dprintf("system has %d interfaces\n", numifs);
5020 # endif /* defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN */
5021 numifs = MAXINTERFACES;
5028 ifc.ifc_len = numifs * sizeof(struct ifreq);
5029 ifc.ifc_buf = xalloc(ifc.ifc_len);
5030 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
5033 sm_dprintf("SIOCGIFCONF failed: %s\n",
5034 sm_errstring(errno));
5039 /* scan the list of IP address */
5041 sm_dprintf("scanning for interface specific names, ifc_len=%d\n",
5044 for (i = 0; i < ifc.ifc_len && i >= 0; )
5047 struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
5048 SOCKADDR *sa = (SOCKADDR *) &ifr->ifr_addr;
5051 struct in6_addr ia6;
5052 # endif /* NETINET6 */
5054 # ifdef SIOCGIFFLAGS
5056 # endif /* SIOCGIFFLAGS */
5059 char buf6[INET6_ADDRSTRLEN];
5060 # endif /* NETINET6 */
5063 ** If we don't have a complete ifr structure,
5064 ** don't try to use it.
5067 if ((ifc.ifc_len - i) < sizeof(*ifr))
5070 # ifdef BSD4_4_SOCKADDR
5071 if (sa->sa.sa_len > sizeof(ifr->ifr_addr))
5072 i += sizeof(ifr->ifr_name) + sa->sa.sa_len;
5074 # endif /* BSD4_4_SOCKADDR */
5078 sm_dprintf("%s\n", anynet_ntoa(sa));
5080 af = ifr->ifr_addr.sa_family;
5084 # endif /* NETINET6 */
5088 # ifdef SIOCGIFFLAGS
5089 memset(&ifrf, '\0', sizeof(struct ifreq));
5090 (void) sm_strlcpy(ifrf.ifr_name, ifr->ifr_name,
5091 sizeof(ifrf.ifr_name));
5092 (void) ioctl(s, SIOCGIFFLAGS, (char *) &ifrf);
5094 sm_dprintf("\tflags: %lx\n",
5095 (unsigned long) ifrf.ifr_flags);
5096 # define IFRFREF ifrf
5097 # else /* SIOCGIFFLAGS */
5098 # define IFRFREF (*ifr)
5099 # endif /* SIOCGIFFLAGS */
5101 if (!bitset(IFF_UP, IFRFREF.ifr_flags))
5106 /* extract IP address from the list*/
5110 ia = sa->sin.sin_addr;
5111 if (ia.s_addr == INADDR_ANY ||
5112 ia.s_addr == INADDR_NONE)
5114 message("WARNING: interface %s is UP with %s address",
5115 ifr->ifr_name, inet_ntoa(ia));
5119 /* save IP address in text from */
5120 (void) sm_snprintf(ip_addr, sizeof(ip_addr), "[%.*s]",
5121 (int) sizeof(ip_addr) - 3,
5127 SETV6LOOPBACKADDRFOUND(*sa);
5129 /* convert into proper scoped address */
5130 if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
5131 IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
5132 sa->sin6.sin6_scope_id == 0)
5134 struct in6_addr *ia6p;
5136 ia6p = &sa->sin6.sin6_addr;
5137 sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
5138 ((unsigned int)ia6p->s6_addr[2] << 8));
5139 ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
5141 # endif /* __KAME__ */
5142 ia6 = sa->sin6.sin6_addr;
5143 if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
5145 addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
5146 message("WARNING: interface %s is UP with %s address",
5148 addr == NULL ? "(NULL)" : addr);
5152 /* save IP address in text from */
5153 addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
5155 (void) sm_snprintf(ip_addr, sizeof(ip_addr),
5157 (int) sizeof(ip_addr) - 3,
5161 # endif /* NETINET6 */
5164 if (ip_addr[0] == '\0')
5167 if (!wordinclass(ip_addr, 'w'))
5169 setclass('w', ip_addr);
5171 sm_dprintf("\ta.k.a.: %s\n", ip_addr);
5174 /* skip "loopback" interface "lo" */
5175 if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
5176 bitset(IFF_LOOPBACK, IFRFREF.ifr_flags))
5179 (void) add_hostnames(sa);
5181 sm_free(ifc.ifc_buf); /* XXX */
5184 # endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
5185 # endif /* NETINET6 && defined(SIOCGLIFCONF) */
5188 ** ISLOOPBACK -- is socket address in the loopback net?
5191 ** sa -- socket address.
5194 ** true -- is socket address in the loopback net?
5195 ** false -- otherwise
5204 if (IN6_IS_ADDR_LOOPBACK(&sa.sin6.sin6_addr))
5206 #else /* NETINET6 */
5207 /* XXX how to correctly extract IN_LOOPBACKNET part? */
5208 if (((ntohl(sa.sin.sin_addr.s_addr) & IN_CLASSA_NET)
5209 >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
5211 #endif /* NETINET6 */
5215 ** GET_NUM_PROCS_ONLINE -- return the number of processors currently online
5221 ** The number of processors online.
5225 get_num_procs_online()
5230 # if defined(CTL_HW) && defined(HW_NCPU)
5236 sz = (size_t) sizeof(nproc);
5237 (void) sysctl(mib, 2, &nproc, &sz, NULL, 0);
5238 # endif /* defined(CTL_HW) && defined(HW_NCPU) */
5239 #else /* USESYSCTL */
5240 # ifdef _SC_NPROCESSORS_ONLN
5241 nproc = (int) sysconf(_SC_NPROCESSORS_ONLN);
5242 # else /* _SC_NPROCESSORS_ONLN */
5244 # include <sys/pstat.h>
5245 struct pst_dynamic psd;
5247 if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) != -1)
5248 nproc = psd.psd_proc_cnt;
5249 # endif /* __hpux */
5250 # endif /* _SC_NPROCESSORS_ONLN */
5251 #endif /* USESYSCTL */
5258 ** SM_CLOSEFROM -- close file descriptors
5261 ** lowest -- first fd to close
5262 ** highest -- last fd + 1 to close
5269 sm_closefrom(lowest, highest)
5270 int lowest, highest;
5274 #else /* HASCLOSEFROM */
5277 for (i = lowest; i < highest; i++)
5279 #endif /* HASCLOSEFROM */
5283 ** CLOSEFD_WALK -- walk fd's arranging to close them
5284 ** Callback for fdwalk()
5287 ** lowest -- first fd to arrange to be closed
5288 ** fd -- fd to arrange to be closed
5295 closefd_walk(lowest, fd)
5299 if (fd >= *(int *)lowest)
5300 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
5303 #endif /* HASFDWALK */
5305 ** SM_CLOSE_ON_EXEC -- arrange for file descriptors to be closed
5308 ** lowest -- first fd to arrange to be closed
5309 ** highest -- last fd + 1 to arrange to be closed
5316 sm_close_on_exec(lowest, highest)
5317 int lowest, highest;
5320 (void) fdwalk(closefd_walk, &lowest);
5321 #else /* HASFDWALK */
5324 for (i = lowest; i < highest; i++)
5326 if ((j = fcntl(i, F_GETFD, 0)) != -1)
5327 (void) fcntl(i, F_SETFD, j | FD_CLOEXEC);
5329 #endif /* HASFDWALK */
5332 ** SEED_RANDOM -- seed the random number generator
5346 #else /* HASSRANDOMDEV */
5350 seed = (long) CurrentPid;
5351 if (gettimeofday(&t, NULL) >= 0)
5352 seed += t.tv_sec + t.tv_usec;
5355 (void) srandom(seed);
5356 # else /* HASRANDOM */
5357 (void) srand((unsigned int) seed);
5358 # endif /* HASRANDOM */
5359 #endif /* HASSRANDOMDEV */
5362 ** SM_SYSLOG -- syslog wrapper to keep messages under SYSLOG_BUFSIZE
5365 ** level -- syslog level
5366 ** id -- envelope ID or NULL (NOQUEUE)
5367 ** fmt -- format string
5368 ** arg... -- arguments as implied by fmt.
5377 sm_syslog(int level, const char *id, const char *fmt, ...)
5378 #else /* __STDC__ */
5379 sm_syslog(level, id, fmt, va_alist)
5384 #endif /* __STDC__ */
5394 extern int SyslogPrefixLen;
5400 idlen = strlen(id) + SyslogPrefixLen;
5403 bufsize = sizeof(buf0);
5409 /* print log message into buf */
5410 SM_VA_START(ap, fmt);
5411 n = sm_vsnprintf(buf, bufsize, fmt, ap);
5417 /* String too small, redo with correct size */
5424 buf = sm_malloc_x(bufsize);
5427 /* clean up buf after it has been expanded with args */
5428 newstring = str2prt(buf);
5429 if ((strlen(newstring) + idlen + 1) < SYSLOG_BUFSIZE)
5438 gettimeofday(&tv, NULL);
5439 sm_dprintf("%ld.%06ld %s\n", (long) tv.tv_sec,
5440 (long) tv.tv_usec, newstring);
5442 else if (tTd(89, 8))
5443 sm_dprintf("%s\n", newstring);
5445 syslog(level, "%s", newstring);
5453 gettimeofday(&tv, NULL);
5454 sm_dprintf("%ld.%06ld %s: %s\n", (long) tv.tv_sec,
5455 (long) tv.tv_usec, id, newstring);
5457 else if (tTd(89, 8))
5458 sm_dprintf("%s: %s\n", id, newstring);
5460 syslog(level, "%s: %s", id, newstring);
5463 /*XXX should do something more sensible */
5465 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s\n",
5468 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5469 "%s: %s\n", id, newstring);
5478 ** additional length for splitting: " ..." + 3, where 3 is magic to
5479 ** have some data for the next entry.
5485 idlen += 5; /* strlen("[999]"), see below */
5486 while (*begin != '\0' &&
5487 (strlen(begin) + idlen) > SYSLOG_BUFSIZE)
5493 /* Too many messages */
5496 end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5499 /* Break on comma or space */
5500 if (*end == ',' || *end == ' ')
5502 end++; /* Include separator */
5507 /* No separator, break midstring... */
5509 end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5514 sm_dprintf("%s[%d]: %s ...\n", id, seq++, begin);
5516 syslog(level, "%s[%d]: %s ...", id, seq++, begin);
5518 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5519 "%s[%d]: %s ...\n", id, seq++, begin);
5528 sm_dprintf("%s[%d]: log terminated, too many parts\n",
5531 syslog(level, "%s[%d]: log terminated, too many parts",
5534 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5535 "%s[%d]: log terminated, too many parts\n", id, seq);
5538 else if (*begin != '\0')
5542 sm_dprintf("%s[%d]: %s\n", id, seq, begin);
5544 syslog(level, "%s[%d]: %s", id, seq, begin);
5546 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5547 "%s[%d]: %s\n", id, seq, begin);
5555 ** HARD_SYSLOG -- call syslog repeatedly until it works
5557 ** Needed on HP-UX, which apparently doesn't guarantee that
5558 ** syslog succeeds during interrupt handlers.
5561 #if defined(__hpux) && !defined(HPUX11)
5563 # define MAXSYSLOGTRIES 100
5566 # define XCNST const
5567 # define CAST (const char *)
5575 hard_syslog(int pri, XCNST char *msg, ...)
5576 # else /* __STDC__ */
5577 hard_syslog(pri, msg, va_alist)
5581 # endif /* __STDC__ */
5584 char buf[SYSLOG_BUFSIZE];
5587 SM_VA_START(ap, msg);
5588 (void) sm_vsnprintf(buf, sizeof(buf), msg, ap);
5591 for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, CAST "%s", buf) < 0; )
5596 #endif /* defined(__hpux) && !defined(HPUX11) */
5597 #if NEEDLOCAL_HOSTNAME_LENGTH
5599 ** LOCAL_HOSTNAME_LENGTH
5601 ** This is required to get sendmail to compile against BIND 4.9.x
5604 ** Unfortunately, a Compaq Y2K patch kit provides it without
5605 ** bumping __RES in /usr/include/resolv.h so we can't automatically
5606 ** figure out whether it is needed.
5610 local_hostname_length(hostname)
5613 size_t len_host, len_domain;
5615 if (!*_res.defdname)
5617 len_host = strlen(hostname);
5618 len_domain = strlen(_res.defdname);
5619 if (len_host > len_domain &&
5620 (sm_strcasecmp(hostname + len_host - len_domain,
5621 _res.defdname) == 0) &&
5622 hostname[len_host - len_domain - 1] == '.')
5623 return len_host - len_domain - 1;
5627 #endif /* NEEDLOCAL_HOSTNAME_LENGTH */
5631 ** LINK -- clone a file
5633 ** Some OS's lacks link() and hard links. Since sendmail is using
5634 ** link() as an efficient way to clone files, this implementation
5635 ** will simply do a file copy.
5637 ** NOTE: This link() replacement is not a generic replacement as it
5638 ** does not handle all of the semantics of the real link(2).
5641 ** source -- pathname of existing file.
5642 ** target -- pathname of link (clone) to be created.
5646 ** -1 -- failure, see errno for details.
5650 link(source, target)
5656 int src = -1, dst = -1;
5662 sff = SFF_REGONLY|SFF_OPENASROOT;
5663 if (DontLockReadFiles)
5666 /* Open the original file */
5667 src = safeopen((char *)source, O_RDONLY, 0, sff);
5671 /* Obtain the size and the mode */
5672 if (fstat(src, &st) < 0)
5675 /* Create the duplicate copy */
5678 dst = safeopen((char *)target, O_CREAT|O_EXCL|O_WRONLY,
5683 /* Copy all of the bytes one buffer at a time */
5684 while ((readlen = read(src, &buf, sizeof(buf))) > 0)
5686 ssize_t left = readlen;
5690 (writelen = write(dst, p, (size_t) left)) >= 0)
5699 /* Any trouble reading? */
5700 if (readlen < 0 || writelen < 0)
5703 /* Close the input file */
5711 /* Close the output file */
5714 /* don't set dst = -1 here so we unlink the file */
5727 (void) unlink(target);
5733 #endif /* NEEDLINK */
5736 ** Compile-Time options
5739 char *CompileOptions[] =
5755 #if HESIOD_ALLOW_NUMERIC_LOGIN
5756 "HESIOD_ALLOW_NUMERIC_LOGIN",
5762 /* Use uncompressed IPv6 address format (no "::") by default */
5837 #ifdef PICKY_HELO_CHECK
5846 # else /* SASL >= 20000 */
5853 #if SM_LDAP_ERROR_ON_MISSING_ARGS
5854 "SM_LDAP_ERROR_ON_MISSING_ARGS",
5865 #if SUID_ROOT_FILES_OK
5866 "SUID_ROOT_FILES_OK",
5874 #if TLS_VRFY_PER_CTX
5897 ** OS compile options.
5900 char *OsCompileOptions[] =
5902 #if ADDRCONFIG_IS_BROKEN
5903 "ADDRCONFIG_IS_BROKEN",
5905 #ifdef AUTO_NETINFO_HOSTS
5906 "AUTO_NETINFO_HOSTS",
5908 #ifdef AUTO_NIS_ALIASES
5911 #if BROKEN_RES_SEARCH
5912 "BROKEN_RES_SEARCH",
5914 #ifdef BSD4_4_SOCKADDR
5920 #if DEC_OSF_BROKEN_GETPWENT
5921 "DEC_OSF_BROKEN_GETPWENT",
5923 #if FAST_PID_RECYCLE
5941 #if HASGETDTABLESIZE
5950 #if HASLDAPGETALIASBYNAME
5951 "HASLDAPGETALIASBYNAME",
5986 #if HASSETUSERCONTEXT
5987 "HASSETUSERCONTEXT",
6025 #if O_EXLOCK && HASFLOCK && !BOGUS_O_EXCL
6037 #if NEEDLOCAL_HOSTNAME_LENGTH
6038 "NEEDLOCAL_HOSTNAME_LENGTH",
6049 #ifdef NO_GETSERVBYNAME
6055 #if REQUIRES_DIR_FSYNC
6056 "REQUIRES_DIR_FSYNC",
6058 #if RLIMIT_NEEDS_SYS_TIME_H
6059 "RLIMIT_NEEDS_SYS_TIME_H",
6067 #if SFS_TYPE == SFS_4ARGS
6069 #elif SFS_TYPE == SFS_MOUNT
6071 #elif SFS_TYPE == SFS_NONE
6073 #elif SFS_TYPE == SFS_NT
6075 #elif SFS_TYPE == SFS_STATFS
6077 #elif SFS_TYPE == SFS_STATVFS
6079 #elif SFS_TYPE == SFS_USTAT
6081 #elif SFS_TYPE == SFS_VFS
6087 #if SIOCGIFCONF_IS_BROKEN
6088 "SIOCGIFCONF_IS_BROKEN",
6090 #if SIOCGIFNUM_IS_BROKEN
6091 "SIOCGIFNUM_IS_BROKEN",
6093 #if SNPRINTF_IS_BROKEN
6094 "SNPRINTF_IS_BROKEN",
6096 #if SO_REUSEADDR_IS_BROKEN
6097 "SO_REUSEADDR_IS_BROKEN",
6111 #if USE_SA_SIGACTION
6126 #if USE_OPENSSL_ENGINE
6127 "USE_OPENSSL_ENGINE",
6129 #if USING_NETSCAPE_LDAP
6130 "USING_NETSCAPE_LDAP",
6139 ** FFR compile options.
6142 char *FFRCompileOptions[] =
6147 #if _FFR_ADDR_TYPE_MODES
6148 /* more info in {addr_type}, requires m4 changes! */
6149 "_FFR_ADDR_TYPE_MODES",
6151 #if _FFR_ALIAS_DETAIL
6152 /* try to handle +detail for aliases */
6153 "_FFR_ALIAS_DETAIL",
6155 #if _FFR_ALLOW_SASLINFO
6156 /* DefaultAuthInfo can be specified by user. */
6157 /* DefaultAuthInfo doesn't really work in 8.13 anymore. */
6158 "_FFR_ALLOW_SASLINFO",
6160 #if _FFR_BADRCPT_SHUTDOWN
6161 /* shut down connection (421) if there are too many bad RCPTs */
6162 "_FFR_BADRCPT_SHUTDOWN",
6164 #if _FFR_BESTMX_BETTER_TRUNCATION
6165 /* Better truncation of list of MX records for dns map. */
6166 "_FFR_BESTMX_BETTER_TRUNCATION",
6168 #if _FFR_BOUNCE_QUEUE
6169 /* Separate, unprocessed queue for DSNs */
6170 /* John Gardiner Myers of Proofpoint */
6171 "_FFR_BOUNCE_QUEUE",
6173 #if _FFR_CATCH_BROKEN_MTAS
6174 /* Deal with MTAs that send a reply during the DATA phase. */
6175 "_FFR_CATCH_BROKEN_MTAS",
6178 /* Stricter checks about queue directory permissions. */
6181 #if _FFR_CLIENT_SIZE
6182 /* Don't try to send mail if its size exceeds SIZE= of server. */
6186 /* CRLPath; needs documentation; Al Smith */
6190 /* deliver first TA in background, then queue */
6193 #if _FFR_DIGUNIX_SAFECHOWN
6194 /* Properly set SAFECHOWN (include/sm/conf.h) for Digital UNIX */
6195 /* Problem noted by Anne Bennett of Concordia University */
6196 "_FFR_DIGUNIX_SAFECHOWN",
6198 #if _FFR_DNSMAP_ALIASABLE
6199 /* Allow dns map type to be used for aliases. */
6200 /* Don Lewis of TDK */
6201 "_FFR_DNSMAP_ALIASABLE",
6203 #if _FFR_DONTLOCKFILESFORREAD_OPTION
6204 /* Enable DontLockFilesForRead option. */
6205 "_FFR_DONTLOCKFILESFORREAD_OPTION",
6207 #if _FFR_DOTTED_USERNAMES
6208 /* Allow usernames with '.' */
6209 "_FFR_DOTTED_USERNAMES",
6213 ** Make DaemonPortOptions case sensitive.
6214 ** For some unknown reasons the code converted every option
6215 ** to uppercase (first letter only, as that's the only one that
6216 ** is actually checked). This prevented all new lower case options
6218 ** The documentation doesn't say anything about case (in)sensitivity,
6219 ** which means it should be case sensitive by default,
6220 ** but it's not a good idea to change this within a patch release,
6221 ** so let's delay this to 8.15.
6226 #if _FFR_DPRINTF_MAP
6227 /* dprintf map for logging */
6230 #if _FFR_DROP_TRUSTUSER_WARNING
6232 ** Don't issue this warning:
6233 ** "readcf: option TrustedUser may cause problems on systems
6234 ** which do not support fchown() if UseMSP is not set.
6237 "_FFR_DROP_TRUSTUSER_WARNING",
6239 #if _FFR_EIGHT_BIT_ADDR_OK
6240 /* EightBitAddrOK: allow 8-bit e-mail addresses */
6241 "_FFR_EIGHT_BIT_ADDR_OK",
6243 #if _FFR_EXTRA_MAP_CHECK
6244 /* perform extra checks on $( $) in R lines */
6245 "_FFR_EXTRA_MAP_CHECK",
6247 #if _FFR_GETHBN_ExFILE
6249 ** According to Motonori Nakamura some gethostbyname()
6250 ** implementations (TurboLinux?) may (temporarily) fail
6251 ** due to a lack of file descriptors. Enabling this FFR
6252 ** will check errno for EMFILE and ENFILE and in case of a match
6253 ** cause a temporary error instead of a permanent error.
6254 ** The right solution is of course to file a bug against those
6255 ** systems such that they actually set h_errno = TRY_AGAIN.
6258 "_FFR_GETHBN_ExFILE",
6261 /* FIPSMode (if supported by OpenSSL library) */
6266 ** If using -t, force not sending to argv recipients, even
6267 ** if they are mentioned in the headers.
6272 #if _FFR_FORWARD_SYSERR
6273 /* Cause a "syserr" if forward file isn't "safe". */
6274 "_FFR_FORWARD_SYSERR",
6277 /* Generate a ORCPT DSN arg if not already provided */
6280 #if _FFR_HANDLE_ISO8859_GECOS
6282 ** Allow ISO 8859 characters in GECOS field: replace them
6283 ** with ASCII "equivalent".
6286 /* Peter Eriksson of Linkopings universitet */
6287 "_FFR_HANDLE_ISO8859_GECOS",
6289 #if _FFR_HANDLE_HDR_RW_TEMPFAIL
6291 ** Temporary header rewriting problems from remotename() etc
6292 ** are not "sticky" for mci (e.g., during queue runs).
6295 "_FFR_HANDLE_HDR_RW_TEMPFAIL",
6297 #if _FFR_HPUX_NSSWITCH
6298 /* Use nsswitch on HP-UX */
6299 "_FFR_HPUX_NSSWITCH",
6301 #if _FFR_IGNORE_BOGUS_ADDR
6302 /* Ignore addresses for which prescan() failed */
6303 "_FFR_IGNORE_BOGUS_ADDR",
6305 #if _FFR_IGNORE_EXT_ON_HELO
6306 /* Ignore extensions offered in response to HELO */
6307 "_FFR_IGNORE_EXT_ON_HELO",
6310 /* Set MAXHOSTNAMELEN to 256 (Linux) */
6313 #if _FFR_LOCAL_DAEMON
6314 /* Local daemon mode (-bl) which only accepts loopback connections */
6315 "_FFR_LOCAL_DAEMON",
6318 /* log some TLS/AUTH info in from= too */
6322 /* log some TLS info in to= too */
6331 #if _FFR_MAXDATASIZE
6333 ** It is possible that a header is larger than MILTER_CHUNK_SIZE,
6334 ** hence this shouldn't be used as limit for milter communication.
6335 ** see also libmilter/comm.c
6336 ** Gurusamy Sarathy of ActiveState
6341 #if _FFR_MAX_FORWARD_ENTRIES
6342 /* Try to limit number of .forward entries */
6343 /* (doesn't work) */
6344 /* Randall S. Winchester of the University of Maryland */
6345 "_FFR_MAX_FORWARD_ENTRIES",
6347 #if _FFR_MAX_SLEEP_TIME
6348 /* Limit sleep(2) time in libsm/clock.c */
6349 "_FFR_MAX_SLEEP_TIME",
6351 #if _FFR_MDS_NEGOTIATE
6352 /* MaxDataSize negotation with libmilter */
6353 "_FFR_MDS_NEGOTIATE",
6356 /* Check free memory */
6359 #if _FFR_MILTER_CHECK
6360 "_FFR_MILTER_CHECK",
6362 #if _FFR_MILTER_CONNECT_REPLYCODE
6363 /* milter: propagate replycode returned by connect commands */
6364 /* John Gardiner Myers of Proofpoint */
6365 "_FFR_MILTER_CONNECT_REPLYCODE ",
6367 #if _FFR_MILTER_CONVERT_ALL_LF_TO_CRLF
6369 ** milter_body() uses the same conversion algorithm as putbody()
6370 ** to translate the "local" df format (\n) to SMTP format (\r\n).
6371 ** However, putbody() and mime8to7() use different conversion
6373 ** If the input date does not follow the SMTP standard
6374 ** (e.g., if it has "naked \r"s), then the output from putbody()
6375 ** and mime8to7() will most likely be different.
6376 ** By turning on this FFR milter_body() will try to "imitate"
6378 ** Note: there is no (simple) way to deal with both conversions
6379 ** in a consistent manner. Moreover, as the "GiGo" principle applies,
6380 ** it's not really worth to fix it.
6383 "_FFR_MILTER_CONVERT_ALL_LF_TO_CRLF",
6385 #if _FFR_MILTER_CHECK_REJECTIONS_TOO
6387 ** Also send RCPTs that are rejected by check_rcpt to a milter
6388 ** (if requested during option negotiation).
6391 "_FFR_MILTER_CHECK_REJECTIONS_TOO",
6393 #if _FFR_MILTER_ENHSC
6394 /* extract enhanced status code from milter replies for dsn= logging */
6395 "_FFR_MILTER_ENHSC",
6397 #if _FFR_MIME7TO8_OLD
6398 /* Old mime7to8 code, the new is broken for at least one example. */
6399 "_FFR_MIME7TO8_OLD",
6401 #if _FFR_MORE_MACROS
6402 /* allow more long macro names ("unprintable" characters). */
6406 /* allow to override "Message accepted for delivery" */
6409 #if _FFR_NODELAYDSN_ON_HOLD
6410 /* Do not issue a DELAY DSN for mailers that use the hold flag. */
6412 "_FFR_NODELAYDSN_ON_HOLD",
6415 /* Disable PIPELINING, delay client if used. */
6418 #if _FFR_LDAP_NETWORK_TIMEOUT
6419 /* set LDAP_OPT_NETWORK_TIMEOUT if available (-c) */
6420 "_FFR_LDAP_NETWORK_TIMEOUT",
6423 /* log ntries=, from Nik Clayton of FreeBSD */
6427 /* "proxy" (synchronous) delivery mode */
6430 #if _FFR_QF_PARANOIA
6433 #if _FFR_QUEUE_GROUP_SORTORDER
6434 /* Allow QueueSortOrder per queue group. */
6435 /* XXX: Still need to actually use qgrp->qg_sortorder */
6436 "_FFR_QUEUE_GROUP_SORTORDER",
6438 #if _FFR_QUEUE_MACRO
6439 /* Define {queue} macro. */
6442 #if _FFR_QUEUE_RUN_PARANOIA
6443 /* Additional checks when doing queue runs; interval of checks */
6444 "_FFR_QUEUE_RUN_PARANOIA",
6446 #if _FFR_QUEUE_SCHED_DBG
6447 /* Debug output for the queue scheduler. */
6448 "_FFR_QUEUE_SCHED_DBG",
6453 #if _FFR_RCPTTHROTDELAY
6454 /* configurable delay for BadRcptThrottle */
6455 "_FFR_RCPTTHROTDELAY",
6457 #if _FFR_REDIRECTEMPTY
6459 ** envelope <> can't be sent to mailing lists, only owner-
6460 ** send spam of this type to owner- of the list
6461 ** ---- to stop spam from going to mailing lists.
6464 "_FFR_REDIRECTEMPTY",
6466 #if _FFR_REJECT_NUL_BYTE
6467 /* reject NUL bytes in body */
6468 "_FFR_REJECT_NUL_BYTE",
6470 #if _FFR_RESET_MACRO_GLOBALS
6471 /* Allow macro 'j' to be set dynamically via rulesets. */
6472 "_FFR_RESET_MACRO_GLOBALS",
6475 /* Random shuffle for queue sorting. */
6480 ** allow -qGqueue_group -qp to work, i.e.,
6481 ** restrict a persistent queue runner to a queue group.
6487 /* session id (for logging) */
6491 /* Donated code (unused). */
6494 #if _FFR_LDAP_SINGLEDN
6496 ** The LDAP database map code in Sendmail 8.12.10, when
6497 ** given the -1 switch, would match only a single DN,
6498 ** but was able to return multiple attributes for that
6499 ** DN. In Sendmail 8.13 this "bug" was corrected to
6500 ** only return if exactly one attribute matched.
6502 ** Unfortunately, our configuration uses the former
6503 ** behaviour. Attached is a relatively simple patch
6504 ** to 8.13.4 which adds a -2 switch (for lack of a
6505 ** better option) which returns the single dn/multiple
6508 ** Jeffrey T. Eaton, Carnegie-Mellon University
6511 "_FFR_LDAP_SINGLEDN",
6513 #if _FFR_SKIP_DOMAINS
6514 /* process every N'th domain instead of every N'th message */
6515 "_FFR_SKIP_DOMAINS",
6517 #if _FFR_SLEEP_USE_SELECT
6518 /* Use select(2) in libsm/clock.c to emulate sleep(2) */
6519 "_FFR_SLEEP_USE_SELECT ",
6523 ** It looks like the Compaq Tru64 5.1A now aligns argv and envp to 64
6524 ** bit alignment, so unless each piece of argv and envp is a multiple
6525 ** of 8 bytes (including terminating NULL), initsetproctitle() won't
6526 ** use any of the space beyond argv[0]. Be sure to set SPT_ALIGN_SIZE
6527 ** if you use this FFR.
6530 /* Chris Adams of HiWAAY Informations Services */
6533 #if _FFR_SS_PER_DAEMON
6534 /* SuperSafe per DaemonPortOptions: 'T' (better letter?) */
6535 "_FFR_SS_PER_DAEMON",
6538 /* enable some test code */
6542 /* Donated code (unused). */
6548 #if _FFR_TLS_USE_CERTIFICATE_CHAIN_FILE
6550 ** Use SSL_CTX_use_certificate_chain_file()
6551 ** instead of SSL_CTX_use_certificate_file()
6554 "_FFR_TLS_USE_CERTIFICATE_CHAIN_FILE",
6556 #if _FFR_TLS_SE_OPTS
6557 /* TLS session options */
6562 ** If we don't own the file mark it as unsafe.
6563 ** However, allow TrustedUser to own it as well
6564 ** in case TrustedUser manipulates the queue.
6569 #if _FFR_USE_GETPWNAM_ERRNO
6571 ** See libsm/mbdb.c: only enable this on OSs
6572 ** that implement the correct (POSIX) semantics.
6573 ** This will need to become an OS-specific #if enabled
6574 ** in one of the headers files under include/sm/os/ .
6577 "_FFR_USE_GETPWNAM_ERRNO",
6579 #if _FFR_USE_SEM_LOCKING
6580 "_FFR_USE_SEM_LOCKING",
6582 #if _FFR_USE_SETLOGIN
6583 /* Use setlogin() */
6585 "_FFR_USE_SETLOGIN",