2 * Copyright (c) 1998-2003 Sendmail, 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.
12 * $FreeBSD: src/contrib/sendmail/src/conf.c,v 1.5.2.15 2003/10/30 22:31:43 gshapiro Exp $
14 * $DragonFly: src/contrib/sendmail/src/Attic/conf.c,v 1.4 2005/04/29 10:04:45 joerg Exp $
19 SM_RCSID("@(#)$Id: conf.c,v 8.972.2.50 2003/09/03 21:37:03 ca Exp $")
21 #include <sendmail/pathnames.h>
26 # include <sys/ioctl.h>
27 # include <sys/param.h>
31 #if NETINET || NETINET6
32 # include <arpa/inet.h>
33 #endif /* NETINET || NETINET6 */
34 #if HASULIMIT && defined(HPUX11)
36 #endif /* HASULIMIT && defined(HPUX11) */
38 static void setupmaps __P((void));
39 static void setupmailers __P((void));
40 static void setupqueues __P((void));
41 static int get_num_procs_online __P((void));
45 ** CONF.C -- Sendmail Configuration Tables.
47 ** Defines the configuration of this installation.
49 ** Configuration Variables:
50 ** HdrInfo -- a table describing well-known header fields.
51 ** Each entry has the field name and some flags,
52 ** which are described in sendmail.h.
55 ** I have tried to put almost all the reasonable
56 ** configuration information into the configuration
57 ** file read at runtime. My intent is that anything
58 ** here is a function of the version of UNIX you
59 ** are running, or is really static -- for example
60 ** the headers are a superset of widely used
61 ** protocols. If you find yourself playing with
62 ** this file too much, you may be making a mistake!
68 ** Final (null) entry contains the flags used for any other field.
70 ** Not all of these are actually handled specially by sendmail
71 ** at this time. They are included as placeholders, to let
72 ** you know that "someday" I intend to have sendmail do
73 ** something with them.
76 struct hdrinfo HdrInfo[] =
78 /* originator fields, most to least significant */
79 { "resent-sender", H_FROM|H_RESENT, NULL },
80 { "resent-from", H_FROM|H_RESENT, NULL },
81 { "resent-reply-to", H_FROM|H_RESENT, NULL },
82 { "sender", H_FROM, NULL },
83 { "from", H_FROM, NULL },
84 { "reply-to", H_FROM, NULL },
85 { "errors-to", H_FROM|H_ERRORSTO, NULL },
86 { "full-name", H_ACHECK, NULL },
87 { "return-receipt-to", H_RECEIPTTO, NULL },
88 { "disposition-notification-to", H_FROM, NULL },
90 /* destination fields */
91 { "to", H_RCPT, NULL },
92 { "resent-to", H_RCPT|H_RESENT, NULL },
93 { "cc", H_RCPT, NULL },
94 { "resent-cc", H_RCPT|H_RESENT, NULL },
95 { "bcc", H_RCPT|H_BCC, NULL },
96 { "resent-bcc", H_RCPT|H_BCC|H_RESENT, NULL },
97 { "apparently-to", H_RCPT, NULL },
99 /* message identification and control */
100 { "message-id", 0, NULL },
101 { "resent-message-id", H_RESENT, NULL },
102 { "message", H_EOH, NULL },
103 { "text", H_EOH, NULL },
107 { "resent-date", H_RESENT, NULL },
110 { "received", H_TRACE|H_FORCE, NULL },
111 { "x400-received", H_TRACE|H_FORCE, NULL },
112 { "via", H_TRACE|H_FORCE, NULL },
113 { "mail-from", H_TRACE|H_FORCE, NULL },
115 /* miscellaneous fields */
116 { "comments", H_FORCE|H_ENCODABLE, NULL },
117 { "return-path", H_FORCE|H_ACHECK|H_BINDLATE, NULL },
118 { "content-transfer-encoding", H_CTE, NULL },
119 { "content-type", H_CTYPE, NULL },
120 { "content-length", H_ACHECK, NULL },
121 { "subject", H_ENCODABLE, NULL },
122 { "x-authentication-warning", H_FORCE, NULL },
133 struct prival PrivacyValues[] =
135 { "public", PRIV_PUBLIC },
136 { "needmailhelo", PRIV_NEEDMAILHELO },
137 { "needexpnhelo", PRIV_NEEDEXPNHELO },
138 { "needvrfyhelo", PRIV_NEEDVRFYHELO },
139 { "noexpn", PRIV_NOEXPN },
140 { "novrfy", PRIV_NOVRFY },
141 { "restrictexpand", PRIV_RESTRICTEXPAND },
142 { "restrictmailq", PRIV_RESTRICTMAILQ },
143 { "restrictqrun", PRIV_RESTRICTQRUN },
144 { "noetrn", PRIV_NOETRN },
145 { "noverb", PRIV_NOVERB },
146 { "authwarnings", PRIV_AUTHWARNINGS },
147 { "noreceipts", PRIV_NORECEIPTS },
148 { "nobodyreturn", PRIV_NOBODYRETN },
149 { "goaway", PRIV_GOAWAY },
154 ** DontBlameSendmail values
157 struct dbsval DontBlameSendmailValues[] =
159 { "safe", DBS_SAFE },
160 { "assumesafechown", DBS_ASSUMESAFECHOWN },
161 { "groupwritabledirpathsafe", DBS_GROUPWRITABLEDIRPATHSAFE },
162 { "groupwritableforwardfilesafe",
163 DBS_GROUPWRITABLEFORWARDFILESAFE },
164 { "groupwritableincludefilesafe",
165 DBS_GROUPWRITABLEINCLUDEFILESAFE },
166 { "groupwritablealiasfile", DBS_GROUPWRITABLEALIASFILE },
167 { "worldwritablealiasfile", DBS_WORLDWRITABLEALIASFILE },
168 { "forwardfileinunsafedirpath", DBS_FORWARDFILEINUNSAFEDIRPATH },
169 { "includefileinunsafedirpath", DBS_INCLUDEFILEINUNSAFEDIRPATH },
170 { "mapinunsafedirpath", DBS_MAPINUNSAFEDIRPATH },
171 { "linkedaliasfileinwritabledir",
172 DBS_LINKEDALIASFILEINWRITABLEDIR },
173 { "linkedclassfileinwritabledir",
174 DBS_LINKEDCLASSFILEINWRITABLEDIR },
175 { "linkedforwardfileinwritabledir",
176 DBS_LINKEDFORWARDFILEINWRITABLEDIR },
177 { "linkedincludefileinwritabledir",
178 DBS_LINKEDINCLUDEFILEINWRITABLEDIR },
179 { "linkedmapinwritabledir", DBS_LINKEDMAPINWRITABLEDIR },
180 { "linkedserviceswitchfileinwritabledir",
181 DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR },
182 { "filedeliverytohardlink", DBS_FILEDELIVERYTOHARDLINK },
183 { "filedeliverytosymlink", DBS_FILEDELIVERYTOSYMLINK },
184 { "writemaptohardlink", DBS_WRITEMAPTOHARDLINK },
185 { "writemaptosymlink", DBS_WRITEMAPTOSYMLINK },
186 { "writestatstohardlink", DBS_WRITESTATSTOHARDLINK },
187 { "writestatstosymlink", DBS_WRITESTATSTOSYMLINK },
188 { "forwardfileingroupwritabledirpath",
189 DBS_FORWARDFILEINGROUPWRITABLEDIRPATH },
190 { "includefileingroupwritabledirpath",
191 DBS_INCLUDEFILEINGROUPWRITABLEDIRPATH },
192 { "classfileinunsafedirpath", DBS_CLASSFILEINUNSAFEDIRPATH },
193 { "errorheaderinunsafedirpath", DBS_ERRORHEADERINUNSAFEDIRPATH },
194 { "helpfileinunsafedirpath", DBS_HELPFILEINUNSAFEDIRPATH },
195 { "forwardfileinunsafedirpathsafe",
196 DBS_FORWARDFILEINUNSAFEDIRPATHSAFE },
197 { "includefileinunsafedirpathsafe",
198 DBS_INCLUDEFILEINUNSAFEDIRPATHSAFE },
199 { "runprograminunsafedirpath", DBS_RUNPROGRAMINUNSAFEDIRPATH },
200 { "runwritableprogram", DBS_RUNWRITABLEPROGRAM },
201 { "nonrootsafeaddr", DBS_NONROOTSAFEADDR },
202 { "truststickybit", DBS_TRUSTSTICKYBIT },
203 { "dontwarnforwardfileinunsafedirpath",
204 DBS_DONTWARNFORWARDFILEINUNSAFEDIRPATH },
205 { "insufficiententropy", DBS_INSUFFICIENTENTROPY },
206 { "groupreadablesasldbfile", DBS_GROUPREADABLESASLDBFILE },
207 { "groupwritablesasldbfile", DBS_GROUPWRITABLESASLDBFILE },
208 { "groupwritableforwardfile", DBS_GROUPWRITABLEFORWARDFILE },
209 { "groupwritableincludefile", DBS_GROUPWRITABLEINCLUDEFILE },
210 { "worldwritableforwardfile", DBS_WORLDWRITABLEFORWARDFILE },
211 { "worldwritableincludefile", DBS_WORLDWRITABLEINCLUDEFILE },
212 { "groupreadablekeyfile", DBS_GROUPREADABLEKEYFILE },
213 #if _FFR_GROUPREADABLEAUTHINFOFILE
214 { "groupreadableadefaultauthinfofile",
215 DBS_GROUPREADABLEAUTHINFOFILE },
216 #endif /* _FFR_GROUPREADABLEAUTHINFOFILE */
221 ** Miscellaneous stuff.
224 int DtableSize = 50; /* max open files; reset in 4.2bsd */
226 ** SETDEFAULTS -- set default values
228 ** Some of these must be initialized using direct code since they
229 ** depend on run-time values. So let's do all of them this way.
232 ** e -- the default envelope.
238 ** Initializes a bunch of global variables to their
243 #define HOURS * 60 MINUTES
244 #define DAYS * 24 HOURS
246 #ifndef MAXRULERECURSION
247 # define MAXRULERECURSION 50 /* max ruleset recursion depth */
248 #endif /* ! MAXRULERECURSION */
252 register ENVELOPE *e;
258 numprocs = get_num_procs_online();
259 SpaceSub = ' '; /* option B */
260 QueueLA = 8 * numprocs; /* option x */
261 RefuseLA = 12 * numprocs; /* option X */
262 WkRecipFact = 30000L; /* option y */
263 WkClassFact = 1800L; /* option z */
264 WkTimeFact = 90000L; /* option Z */
265 QueueFactor = WkRecipFact * 20; /* option q */
267 QueueMode = QM_NORMAL; /* what queue items to act upon */
268 #endif /* _FFR_QUARANTINE */
269 FileMode = (RealUid != geteuid()) ? 0644 : 0600;
271 QueueFileMode = (RealUid != geteuid()) ? 0644 : 0600;
272 /* option QueueFileMode */
274 if (((pw = sm_getpwnam("mailnull")) != NULL && pw->pw_uid != 0) ||
275 ((pw = sm_getpwnam("sendmail")) != NULL && pw->pw_uid != 0) ||
276 ((pw = sm_getpwnam("daemon")) != NULL && pw->pw_uid != 0))
278 DefUid = pw->pw_uid; /* option u */
279 DefGid = pw->pw_gid; /* option g */
280 DefUser = newstr(pw->pw_name);
284 DefUid = 1; /* option u */
285 DefGid = 1; /* option g */
290 sm_dprintf("setdefaults: DefUser=%s, DefUid=%d, DefGid=%d\n",
291 DefUser != NULL ? DefUser : "<1:1>",
292 (int) DefUid, (int) DefGid);
293 CheckpointInterval = 10; /* option C */
294 MaxHopCount = 25; /* option h */
295 set_delivery_mode(SM_FORK, e); /* option d */
296 e->e_errormode = EM_PRINT; /* option e */
299 e->e_xfqgrp = NOQGRP;
300 e->e_xfqdir = NOQDIR;
301 e->e_ctime = curtime();
302 SevenBitInput = false; /* option 7 */
303 MaxMciCache = 1; /* option k */
304 MciCacheTimeout = 5 MINUTES; /* option K */
305 LogLevel = 9; /* option L */
309 inittimeouts(NULL, false); /* option r */
310 PrivacyFlags = PRIV_PUBLIC; /* option p */
311 MeToo = true; /* option m */
312 SendMIMEErrors = true; /* option f */
313 SuperSafe = SAFE_REALLY; /* option s */
314 clrbitmap(DontBlameSendmail); /* DontBlameSendmail option */
316 MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */
318 MimeMode = MM_PASS8BIT;
319 #endif /* MIME8TO7 */
320 for (i = 0; i < MAXTOCLASS; i++)
322 TimeOuts.to_q_return[i] = 5 DAYS; /* option T */
323 TimeOuts.to_q_warning[i] = 0; /* option T */
325 ServiceSwitchFile = "/etc/mail/service.switch";
326 ServiceCacheMaxAge = (time_t) 10;
327 HostsFile = _PATH_HOSTS;
328 PidFile = newstr(_PATH_SENDMAILPID);
329 MustQuoteChars = "@,;:\\()[].'";
330 MciInfoTimeout = 30 MINUTES;
331 MaxRuleRecursion = MAXRULERECURSION;
332 MaxAliasRecursion = 10;
333 MaxMacroRecursion = 10;
334 ColonOkInAddr = true;
335 DontLockReadFiles = true;
336 DontProbeInterfaces = DPI_PROBEALL;
337 DoubleBounceAddr = "postmaster";
338 MaxHeadersLength = MAXHDRSLEN;
339 MaxMimeHeaderLength = MAXLINE;
340 MaxMimeFieldLength = MaxMimeHeaderLength / 2;
341 MaxForwardEntries = 0;
344 AuthMechanisms = newstr(AUTH_MECHANISMS);
348 TLS_Srv_Opts = TLS_I_SRV;
349 #endif /* STARTTLS */
351 HesiodContext = NULL;
352 #endif /* HESIOD_INIT */
354 /* Detect if IPv6 is available at run time */
355 i = socket(AF_INET6, SOCK_STREAM, 0);
365 #endif /* NETINET6 */
366 ControlSocketName = NULL;
367 memset(&ConnectOnlyTo, '\0', sizeof ConnectOnlyTo);
368 DataFileBufferSize = 4096;
369 XscriptFileBufferSize = 4096;
370 for (i = 0; i < MAXRWSETS; i++)
371 RuleSetNames[i] = NULL;
373 InputFilters[0] = NULL;
376 RejectLogInterval = 3 HOURS;
377 #endif /* _FFR_REJECT_LOG */
378 #if _FFR_REQ_DIR_FSYNC_OPT
379 RequiresDirfsync = true;
380 #endif /* _FFR_REQ_DIR_FSYNC_OPT */
389 ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups())
395 struct passwd *defpwent;
396 static char defuserbuf[40];
398 DefUser = defuserbuf;
399 defpwent = sm_getpwuid(DefUid);
400 (void) sm_strlcpy(defuserbuf,
401 (defpwent == NULL || defpwent->pw_name == NULL)
402 ? "nobody" : defpwent->pw_name,
405 sm_dprintf("setdefuser: DefUid=%d, DefUser=%s\n",
406 (int) DefUid, DefUser);
409 ** SETUPQUEUES -- initialize default queues
411 ** The mqueue QUEUE structure gets filled in after readcf() but
412 ** we need something to point to now for the mailer setup,
413 ** which use "mqueue" as default queue.
421 MaxRunnersPerQueue = 1;
422 (void) sm_strlcpy(buf, "mqueue, P=/var/spool/mqueue", sizeof buf);
423 makequeue(buf, false);
426 ** SETUPMAILERS -- initialize default mailers
434 (void) sm_strlcpy(buf, "prog, P=/bin/sh, F=lsouDq9, T=X-Unix/X-Unix/X-Unix, A=sh -c \201u",
438 (void) sm_strlcpy(buf, "*file*, P=[FILE], F=lsDFMPEouq9, T=X-Unix/X-Unix/X-Unix, A=FILE \201u",
442 (void) sm_strlcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE \201u",
448 ** SETUPMAPS -- set up map classes
451 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \
453 extern bool parse __P((MAP *, char *)); \
454 extern bool open __P((MAP *, int)); \
455 extern void close __P((MAP *)); \
456 extern char *lookup __P((MAP *, char *, char **, int *)); \
457 extern void store __P((MAP *, char *, char *)); \
458 s = stab(name, ST_MAPCLASS, ST_ENTER); \
459 s->s_mapclass.map_cname = name; \
460 s->s_mapclass.map_ext = ext; \
461 s->s_mapclass.map_cflags = flags; \
462 s->s_mapclass.map_parse = parse; \
463 s->s_mapclass.map_open = open; \
464 s->s_mapclass.map_close = close; \
465 s->s_mapclass.map_lookup = lookup; \
466 s->s_mapclass.map_store = store; \
475 # if DB_VERSION_MAJOR > 1
476 int major_v, minor_v, patch_v;
478 (void) db_version(&major_v, &minor_v, &patch_v);
479 if (major_v != DB_VERSION_MAJOR || minor_v != DB_VERSION_MINOR)
482 syserr("Berkeley DB version mismatch: compiled against %d.%d.%d, run-time linked against %d.%d.%d",
483 DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,
484 major_v, minor_v, patch_v);
486 # endif /* DB_VERSION_MAJOR > 1 */
488 MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
489 map_parseargs, hash_map_open, db_map_close,
490 db_map_lookup, db_map_store);
492 MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE,
493 map_parseargs, bt_map_open, db_map_close,
494 db_map_lookup, db_map_store);
498 MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE,
499 map_parseargs, ndbm_map_open, ndbm_map_close,
500 ndbm_map_lookup, ndbm_map_store);
504 MAPDEF("nis", NULL, MCF_ALIASOK,
505 map_parseargs, nis_map_open, null_map_close,
506 nis_map_lookup, null_map_store);
510 MAPDEF("nisplus", NULL, MCF_ALIASOK,
511 map_parseargs, nisplus_map_open, null_map_close,
512 nisplus_map_lookup, null_map_store);
516 MAPDEF("ldap", NULL, MCF_ALIASOK|MCF_NOTPERSIST,
517 ldapmap_parseargs, ldapmap_open, ldapmap_close,
518 ldapmap_lookup, null_map_store);
522 MAPDEF("ph", NULL, MCF_NOTPERSIST,
523 ph_map_parseargs, ph_map_open, ph_map_close,
524 ph_map_lookup, null_map_store);
528 /* IRIX 6.5 nsd support */
529 MAPDEF("nsd", NULL, MCF_ALIASOK,
530 map_parseargs, null_map_open, null_map_close,
531 nsd_map_lookup, null_map_store);
535 MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY,
536 map_parseargs, hes_map_open, hes_map_close,
537 hes_map_lookup, null_map_store);
541 MAPDEF("netinfo", NULL, MCF_ALIASOK,
542 map_parseargs, ni_map_open, null_map_close,
543 ni_map_lookup, null_map_store);
547 MAPDEF("dns", NULL, 0,
548 dns_map_init, null_map_open, null_map_close,
549 dns_map_lookup, null_map_store);
554 # if _FFR_DNSMAP_ALIASABLE
555 MAPDEF("dns", NULL, MCF_ALIASOK,
556 dns_map_parseargs, dns_map_open, null_map_close,
557 dns_map_lookup, null_map_store);
558 # else /* _FFR_DNSMAP_ALIASABLE */
559 MAPDEF("dns", NULL, 0,
560 dns_map_parseargs, dns_map_open, null_map_close,
561 dns_map_lookup, null_map_store);
562 # endif /* _FFR_DNSMAP_ALIASABLE */
564 #endif /* NAMED_BIND */
567 /* best MX DNS lookup */
568 MAPDEF("bestmx", NULL, MCF_OPTFILE,
569 map_parseargs, null_map_open, null_map_close,
570 bestmx_map_lookup, null_map_store);
571 #endif /* NAMED_BIND */
573 MAPDEF("host", NULL, 0,
574 host_map_init, null_map_open, null_map_close,
575 host_map_lookup, null_map_store);
577 MAPDEF("text", NULL, MCF_ALIASOK,
578 map_parseargs, text_map_open, null_map_close,
579 text_map_lookup, null_map_store);
581 MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY,
582 map_parseargs, stab_map_open, null_map_close,
583 stab_map_lookup, stab_map_store);
585 MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE,
586 map_parseargs, impl_map_open, impl_map_close,
587 impl_map_lookup, impl_map_store);
589 /* access to system passwd file */
590 MAPDEF("user", NULL, MCF_OPTFILE,
591 map_parseargs, user_map_open, null_map_close,
592 user_map_lookup, null_map_store);
595 MAPDEF("dequote", NULL, 0,
596 dequote_init, null_map_open, null_map_close,
597 dequote_map, null_map_store);
600 MAPDEF("regex", NULL, 0,
601 regex_map_init, null_map_open, null_map_close,
602 regex_map_lookup, null_map_store);
603 #endif /* MAP_REGEX */
607 MAPDEF("userdb", ".db", 0,
608 map_parseargs, null_map_open, null_map_close,
609 udb_map_lookup, null_map_store);
612 /* arbitrary programs */
613 MAPDEF("program", NULL, MCF_ALIASOK,
614 map_parseargs, null_map_open, null_map_close,
615 prog_map_lookup, null_map_store);
618 MAPDEF("sequence", NULL, MCF_ALIASOK,
619 seq_map_parse, null_map_open, null_map_close,
620 seq_map_lookup, seq_map_store);
622 /* switched interface to sequenced maps */
623 MAPDEF("switch", NULL, MCF_ALIASOK,
624 map_parseargs, switch_map_open, null_map_close,
625 seq_map_lookup, seq_map_store);
627 /* null map lookup -- really for internal use only */
628 MAPDEF("null", NULL, MCF_ALIASOK|MCF_OPTFILE,
629 map_parseargs, null_map_open, null_map_close,
630 null_map_lookup, null_map_store);
632 /* syslog map -- logs information to syslog */
633 MAPDEF("syslog", NULL, 0,
634 syslog_map_parseargs, null_map_open, null_map_close,
635 syslog_map_lookup, null_map_store);
637 /* macro storage map -- rulesets can set macros */
638 MAPDEF("macro", NULL, 0,
639 dequote_init, null_map_open, null_map_close,
640 macro_map_lookup, null_map_store);
642 /* arithmetic map -- add/subtract/compare */
643 MAPDEF("arith", NULL, 0,
644 dequote_init, null_map_open, null_map_close,
645 arith_map_lookup, null_map_store);
649 /* bogus map -- always return tempfail */
650 MAPDEF("bogus", NULL, MCF_ALIASOK|MCF_OPTFILE,
651 map_parseargs, null_map_open, null_map_close,
652 bogus_map_lookup, null_map_store);
658 ** INITHOSTMAPS -- initial host-dependent maps
660 ** This should act as an interface to any local service switch
661 ** provided by the host operating system.
670 ** Should define maps "host" and "users" as necessary
671 ** for this OS. If they are not defined, they will get
672 ** a default value later. It should check to make sure
673 ** they are not defined first, since it's possible that
674 ** the config file has provided an override.
682 char *maptype[MAXMAPSTACK];
683 short mapreturn[MAXMAPACTIONS];
687 ** Set up default hosts maps.
691 nmaps = switch_map_find("hosts", maptype, mapreturn);
692 for (i = 0; i < nmaps; i++)
694 if (strcmp(maptype[i], "files") == 0 &&
695 stab("hosts.files", ST_MAP, ST_FIND) == NULL)
697 (void) sm_strlcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts",
699 (void) makemapentry(buf);
702 else if (strcmp(maptype[i], "dns") == 0 &&
703 stab("hosts.dns", ST_MAP, ST_FIND) == NULL)
705 (void) sm_strlcpy(buf, "hosts.dns dns A", sizeof buf);
706 (void) makemapentry(buf);
708 # endif /* NAMED_BIND */
710 else if (strcmp(maptype[i], "nisplus") == 0 &&
711 stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL)
713 (void) sm_strlcpy(buf, "hosts.nisplus nisplus -k name -v address hosts.org_dir",
715 (void) makemapentry(buf);
717 # endif /* NISPLUS */
719 else if (strcmp(maptype[i], "nis") == 0 &&
720 stab("hosts.nis", ST_MAP, ST_FIND) == NULL)
722 (void) sm_strlcpy(buf, "hosts.nis nis -k 0 -v 1 hosts.byname",
724 (void) makemapentry(buf);
728 else if (strcmp(maptype[i], "netinfo") == 0 &&
729 stab("hosts.netinfo", ST_MAP, ST_FIND) == NULL)
731 (void) sm_strlcpy(buf, "hosts.netinfo netinfo -v name /machines",
733 (void) makemapentry(buf);
735 # endif /* NETINFO */
740 ** Make sure we have a host map.
743 if (stab("host", ST_MAP, ST_FIND) == NULL)
745 /* user didn't initialize: set up host map */
746 (void) sm_strlcpy(buf, "host host", sizeof buf);
748 if (ConfigLevel >= 2)
749 (void) sm_strlcat(buf, " -a. -D", sizeof buf);
750 #endif /* NAMED_BIND */
751 (void) makemapentry(buf);
755 ** Set up default aliases maps
758 nmaps = switch_map_find("aliases", maptype, mapreturn);
759 for (i = 0; i < nmaps; i++)
761 if (strcmp(maptype[i], "files") == 0 &&
762 stab("aliases.files", ST_MAP, ST_FIND) == NULL)
764 (void) sm_strlcpy(buf, "aliases.files null",
766 (void) makemapentry(buf);
769 else if (strcmp(maptype[i], "nisplus") == 0 &&
770 stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL)
772 (void) sm_strlcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion mail_aliases.org_dir",
774 (void) makemapentry(buf);
778 else if (strcmp(maptype[i], "nis") == 0 &&
779 stab("aliases.nis", ST_MAP, ST_FIND) == NULL)
781 (void) sm_strlcpy(buf, "aliases.nis nis mail.aliases",
783 (void) makemapentry(buf);
787 else if (strcmp(maptype[i], "netinfo") == 0 &&
788 stab("aliases.netinfo", ST_MAP, ST_FIND) == NULL)
790 (void) sm_strlcpy(buf, "aliases.netinfo netinfo -z, /aliases",
792 (void) makemapentry(buf);
796 else if (strcmp(maptype[i], "hesiod") == 0 &&
797 stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL)
799 (void) sm_strlcpy(buf, "aliases.hesiod hesiod aliases",
801 (void) makemapentry(buf);
805 if (stab("aliases", ST_MAP, ST_FIND) == NULL)
807 (void) sm_strlcpy(buf, "aliases switch aliases", sizeof buf);
808 (void) makemapentry(buf);
811 #if 0 /* "user" map class is a better choice */
813 ** Set up default users maps.
816 nmaps = switch_map_find("passwd", maptype, mapreturn);
817 for (i = 0; i < nmaps; i++)
819 if (strcmp(maptype[i], "files") == 0 &&
820 stab("users.files", ST_MAP, ST_FIND) == NULL)
822 (void) sm_strlcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd",
824 (void) makemapentry(buf);
827 else if (strcmp(maptype[i], "nisplus") == 0 &&
828 stab("users.nisplus", ST_MAP, ST_FIND) == NULL)
830 (void) sm_strlcpy(buf, "users.nisplus nisplus -m -kname -vhome passwd.org_dir",
832 (void) makemapentry(buf);
834 # endif /* NISPLUS */
836 else if (strcmp(maptype[i], "nis") == 0 &&
837 stab("users.nis", ST_MAP, ST_FIND) == NULL)
839 (void) sm_strlcpy(buf, "users.nis nis -m passwd.byname",
841 (void) makemapentry(buf);
845 else if (strcmp(maptype[i], "hesiod") == 0 &&
846 stab("users.hesiod", ST_MAP, ST_FIND) == NULL)
848 (void) sm_strlcpy(buf, "users.hesiod hesiod", sizeof buf);
849 (void) makemapentry(buf);
853 if (stab("users", ST_MAP, ST_FIND) == NULL)
855 (void) sm_strlcpy(buf, "users switch -m passwd", sizeof buf);
856 (void) makemapentry(buf);
861 ** SWITCH_MAP_FIND -- find the list of types associated with a map
863 ** This is the system-dependent interface to the service switch.
866 ** service -- the name of the service of interest.
867 ** maptype -- an out-array of strings containing the types
868 ** of access to use for this service. There can
869 ** be at most MAXMAPSTACK types for a single service.
870 ** mapreturn -- an out-array of return information bitmaps
874 ** The number of map types filled in, or -1 for failure.
877 ** Preserves errno so nothing in the routine clobbers it.
880 #if defined(SOLARIS) || (defined(sony_news) && defined(__svr4))
881 # define _USE_SUN_NSSWITCH_
882 #endif /* defined(SOLARIS) || (defined(sony_news) && defined(__svr4)) */
884 #if _FFR_HPUX_NSSWITCH
886 # define _USE_SUN_NSSWITCH_
888 #endif /* _FFR_HPUX_NSSWITCH */
890 #ifdef _USE_SUN_NSSWITCH_
891 # include <nsswitch.h>
892 #endif /* _USE_SUN_NSSWITCH_ */
894 #if defined(ultrix) || (defined(__osf__) && defined(__alpha))
895 # define _USE_DEC_SVC_CONF_
896 #endif /* defined(ultrix) || (defined(__osf__) && defined(__alpha)) */
898 #ifdef _USE_DEC_SVC_CONF_
899 # include <sys/svcinfo.h>
900 #endif /* _USE_DEC_SVC_CONF_ */
903 switch_map_find(service, maptype, mapreturn)
905 char *maptype[MAXMAPSTACK];
906 short mapreturn[MAXMAPACTIONS];
909 int save_errno = errno;
911 #ifdef _USE_SUN_NSSWITCH_
912 struct __nsw_switchconfig *nsw_conf;
913 enum __nsw_parse_err pserr;
914 struct __nsw_lookup *lk;
915 static struct __nsw_lookup lkp0 =
916 { "files", {1, 0, 0, 0}, NULL, NULL };
917 static struct __nsw_switchconfig lkp_default =
918 { 0, "sendmail", 3, &lkp0 };
920 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
921 mapreturn[svcno] = 0;
923 if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL)
924 lk = lkp_default.lookups;
926 lk = nsw_conf->lookups;
928 while (lk != NULL && svcno < MAXMAPSTACK)
930 maptype[svcno] = lk->service_name;
931 if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN)
932 mapreturn[MA_NOTFOUND] |= 1 << svcno;
933 if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN)
934 mapreturn[MA_TRYAGAIN] |= 1 << svcno;
935 if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN)
936 mapreturn[MA_TRYAGAIN] |= 1 << svcno;
942 #endif /* _USE_SUN_NSSWITCH_ */
944 #ifdef _USE_DEC_SVC_CONF_
945 struct svcinfo *svcinfo;
948 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
949 mapreturn[svcno] = 0;
954 if (strcmp(service, "hosts") == 0)
956 else if (strcmp(service, "aliases") == 0)
958 else if (strcmp(service, "passwd") == 0)
965 for (svcno = 0; svcno < SVC_PATHSIZE && svcno < MAXMAPSTACK; svcno++)
967 switch (svcinfo->svcpath[svc][svcno])
970 maptype[svcno] = "files";
974 maptype[svcno] = "nis";
978 maptype[svcno] = "dns";
983 maptype[svcno] = "hesiod";
985 # endif /* SVC_HESIOD */
994 #endif /* _USE_DEC_SVC_CONF_ */
996 #if !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_)
998 ** Fall-back mechanism.
1002 static time_t servicecachetime; /* time service switch was cached */
1003 time_t now = curtime();
1005 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
1006 mapreturn[svcno] = 0;
1008 if ((now - servicecachetime) > (time_t) ServiceCacheMaxAge)
1010 /* (re)read service switch */
1011 register SM_FILE_T *fp;
1012 long sff = SFF_REGONLY|SFF_OPENASROOT|SFF_NOLOCK;
1014 if (!bitnset(DBS_LINKEDSERVICESWITCHFILEINWRITABLEDIR,
1019 servicecachetime = now;
1020 fp = safefopen(ServiceSwitchFile, O_RDONLY, 0, sff);
1025 while (sm_io_fgets(fp, SM_TIME_DEFAULT, buf,
1026 sizeof buf) != NULL)
1030 p = strpbrk(buf, "#\n");
1033 p = strpbrk(buf, " \t");
1040 sm_syslog(LOG_ERR, NOQID,
1041 "Bad line on %.100s: %.100s",
1052 ** Find/allocate space for this service entry.
1053 ** Space for all of the service strings
1054 ** are allocated at once. This means
1055 ** that we only have to free the first
1056 ** one to free all of them.
1059 st = stab(buf, ST_SERVICE, ST_ENTER);
1060 if (st->s_service[0] != NULL)
1061 sm_free((void *) st->s_service[0]); /* XXX */
1063 for (svcno = 0; svcno < MAXMAPSTACK; )
1067 st->s_service[svcno++] = p;
1068 p = strpbrk(p, " \t");
1075 if (svcno < MAXMAPSTACK)
1076 st->s_service[svcno] = NULL;
1078 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1082 /* look up entry in cache */
1083 st = stab(service, ST_SERVICE, ST_FIND);
1084 if (st != NULL && st->s_service[0] != NULL)
1088 while (svcno < MAXMAPSTACK)
1090 maptype[svcno] = st->s_service[svcno];
1091 if (maptype[svcno++] == NULL)
1097 #endif /* !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) */
1099 #if !defined(_USE_SUN_NSSWITCH_)
1100 /* if the service file doesn't work, use an absolute fallback */
1101 # ifdef _USE_DEC_SVC_CONF_
1103 # endif /* _USE_DEC_SVC_CONF_ */
1104 for (svcno = 0; svcno < MAXMAPACTIONS; svcno++)
1105 mapreturn[svcno] = 0;
1107 if (strcmp(service, "aliases") == 0)
1109 maptype[svcno++] = "files";
1110 # if defined(AUTO_NETINFO_ALIASES) && defined (NETINFO)
1111 maptype[svcno++] = "netinfo";
1112 # endif /* defined(AUTO_NETINFO_ALIASES) && defined (NETINFO) */
1113 # ifdef AUTO_NIS_ALIASES
1115 maptype[svcno++] = "nisplus";
1116 # endif /* NISPLUS */
1118 maptype[svcno++] = "nis";
1120 # endif /* AUTO_NIS_ALIASES */
1124 if (strcmp(service, "hosts") == 0)
1127 maptype[svcno++] = "dns";
1128 # else /* NAMED_BIND */
1129 # if defined(sun) && !defined(BSD)
1131 maptype[svcno++] = "nis";
1132 # endif /* defined(sun) && !defined(BSD) */
1133 # endif /* NAMED_BIND */
1134 # if defined(AUTO_NETINFO_HOSTS) && defined (NETINFO)
1135 maptype[svcno++] = "netinfo";
1136 # endif /* defined(AUTO_NETINFO_HOSTS) && defined (NETINFO) */
1137 maptype[svcno++] = "files";
1143 #endif /* !defined(_USE_SUN_NSSWITCH_) */
1146 ** USERNAME -- return the user id of the logged in user.
1152 ** The login name of the logged in user.
1158 ** The return value is statically allocated.
1164 static char *myname = NULL;
1165 extern char *getlogin();
1166 register struct passwd *pw;
1168 /* cache the result */
1171 myname = getlogin();
1172 if (myname == NULL || myname[0] == '\0')
1174 pw = sm_getpwuid(RealUid);
1176 myname = pw->pw_name;
1180 uid_t uid = RealUid;
1182 if ((pw = sm_getpwnam(myname)) == NULL ||
1183 (uid != 0 && uid != pw->pw_uid))
1185 pw = sm_getpwuid(uid);
1187 myname = pw->pw_name;
1190 if (myname == NULL || myname[0] == '\0')
1192 syserr("554 5.3.0 Who are you?");
1193 myname = "postmaster";
1195 else if (strpbrk(myname, ",;:/|\"\\") != NULL)
1196 myname = addquotes(myname, NULL);
1198 myname = sm_pstrdup_x(myname);
1203 ** TTYPATH -- Get the path of the user's tty
1205 ** Returns the pathname of the user's tty. Returns NULL if
1206 ** the user is not logged in or if s/he has write permission
1213 ** pathname of the user's tty.
1214 ** NULL if not logged in or write permission denied.
1220 ** Return value is in a local buffer.
1230 register char *pathn;
1231 extern char *ttyname();
1232 extern char *getlogin();
1234 /* compute the pathname of the controlling tty */
1235 if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL &&
1236 (pathn = ttyname(0)) == NULL)
1242 /* see if we have write permission */
1243 if (stat(pathn, &stbuf) < 0 || !bitset(S_IWOTH, stbuf.st_mode))
1249 /* see if the user is logged in */
1250 if (getlogin() == NULL)
1257 ** CHECKCOMPAT -- check for From and To person compatible.
1259 ** This routine can be supplied on a per-installation basis
1260 ** to determine whether a person is allowed to send a message.
1261 ** This allows restriction of certain types of internet
1262 ** forwarding or registration of users.
1264 ** If the hosts are found to be incompatible, an error
1265 ** message should be given using "usrerr" and an EX_ code
1266 ** should be returned. You can also set to->q_status to
1267 ** a DSN-style status code.
1269 ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the
1270 ** body during the return-to-sender function; this should be done
1271 ** on huge messages. This bit may already be set by the ESMTP
1275 ** to -- the person being sent to.
1281 ** none (unless you include the usrerr stuff)
1286 register ADDRESS *to;
1287 register ENVELOPE *e;
1290 sm_dprintf("checkcompat(to=%s, from=%s)\n",
1291 to->q_paddr, e->e_from.q_paddr);
1294 /* this code is intended as an example only */
1297 s = stab("arpa", ST_MAILER, ST_FIND);
1298 if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 &&
1299 to->q_mailer == s->s_mailer)
1301 usrerr("553 No ARPA mail through this machine: see your system administration");
1302 /* e->e_flags |= EF_NO_BODY_RETN; to suppress body on return */
1303 to->q_status = "5.7.1";
1304 return EX_UNAVAILABLE;
1306 #endif /* EXAMPLE_CODE */
1310 ** INIT_MD -- do machine dependent initializations
1312 ** Systems that have global modes that should be set should do
1313 ** them here rather than in main.
1317 # include <compat.h>
1318 #endif /* _AUX_SOURCE */
1321 # include <shares.h>
1322 #endif /* SHARE_V1 */
1330 setcompat(getcompat() | COMPAT_BSDPROT);
1331 #endif /* _AUX_SOURCE */
1333 #ifdef SUN_EXTENSIONS
1335 #endif /* SUN_EXTENSIONS */
1338 /* keep gethostby*() from stripping the local domain name */
1339 set_domain_trim_off();
1340 #endif /* _CONVEX_SOURCE */
1343 ** Due to QNX's network distributed nature, you can target a tcpip
1344 ** stack on a different node in the qnx network; this patch lets
1345 ** this feature work. The __sock_locate() must be done before the
1346 ** environment is clear.
1349 #endif /* __QNX__ */
1350 #if SECUREWARE || defined(_SCO_unix_)
1351 set_auth_parameters(argc, argv);
1355 ** This is required for highest security levels (the kernel
1356 ** won't let it call set*uid() or run setuid binaries without
1357 ** it). It may be necessary on other SECUREWARE systems.
1360 if (getluid() == -1)
1362 # endif /* _SCO_unix_ */
1363 #endif /* SECUREWARE || defined(_SCO_unix_) */
1366 #ifdef VENDOR_DEFAULT
1367 VendorCode = VENDOR_DEFAULT;
1368 #else /* VENDOR_DEFAULT */
1369 VendorCode = VENDOR_BERKELEY;
1370 #endif /* VENDOR_DEFAULT */
1373 ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations
1375 ** Called once, on startup.
1378 ** e -- the global envelope.
1384 ** vendor-dependent.
1388 init_vendor_macros(e)
1389 register ENVELOPE *e;
1393 ** GETLA -- get the current load average
1395 ** This code stolen from la.c.
1401 ** The current load average as an integer.
1407 /* try to guess what style of load average we have */
1408 #define LA_ZERO 1 /* always return load average as zero */
1409 #define LA_INT 2 /* read kmem for avenrun; interpret as long */
1410 #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */
1411 #define LA_SUBR 4 /* call getloadavg */
1412 #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */
1413 #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */
1414 #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */
1415 #define LA_READKSYM 8 /* SVR4: use MIOC_READKSYM ioctl call */
1416 #define LA_DGUX 9 /* special DGUX implementation */
1417 #define LA_HPUX 10 /* special HPUX implementation */
1418 #define LA_IRIX6 11 /* special IRIX 6.2 implementation */
1419 #define LA_KSTAT 12 /* special Solaris kstat(3k) implementation */
1420 #define LA_DEVSHORT 13 /* read short from a device */
1421 #define LA_ALPHAOSF 14 /* Digital UNIX (OSF/1 on Alpha) table() call */
1422 #define LA_PSET 15 /* Solaris per-processor-set load average */
1424 /* do guesses based on general OS type */
1426 # define LA_TYPE LA_ZERO
1427 #endif /* ! LA_TYPE */
1430 # if defined(unixpc)
1432 # endif /* defined(unixpc) */
1434 # if defined(__alpha) || defined(IRIX)
1436 # endif /* defined(__alpha) || defined(IRIX) */
1438 #endif /* ! FSHIFT */
1442 #endif /* ! FSHIFT */
1445 # define FSCALE (1 << FSHIFT)
1446 #endif /* ! FSCALE */
1450 # define LA_AVENRUN "avenrun"
1451 # else /* SYSTEM5 */
1452 # define LA_AVENRUN "_avenrun"
1453 # endif /* SYSTEM5 */
1454 #endif /* ! LA_AVENRUN */
1456 /* _PATH_KMEM should be defined in <paths.h> */
1458 # define _PATH_KMEM "/dev/kmem"
1459 #endif /* ! _PATH_KMEM */
1461 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT)
1465 /* _PATH_UNIX should be defined in <paths.h> */
1467 # if defined(SYSTEM5)
1468 # define _PATH_UNIX "/unix"
1469 # else /* defined(SYSTEM5) */
1470 # define _PATH_UNIX "/vmunix"
1471 # endif /* defined(SYSTEM5) */
1472 # endif /* ! _PATH_UNIX */
1476 # else /* _AUX_SOURCE */
1482 # endif /* _AUX_SOURCE */
1483 # define X_AVENRUN 0
1489 static int kmem = -1;
1490 # if LA_TYPE == LA_INT
1492 # else /* LA_TYPE == LA_INT */
1493 # if LA_TYPE == LA_SHORT
1495 # else /* LA_TYPE == LA_SHORT */
1497 # endif /* LA_TYPE == LA_SHORT */
1498 # endif /* LA_TYPE == LA_INT */
1499 extern off_t lseek();
1504 (void) sm_strlcpy(Nl[X_AVENRUN].n_name, LA_AVENRUN,
1505 sizeof Nl[X_AVENRUN].n_name);
1506 Nl[1].n_name[0] = '\0';
1507 # endif /* _AUX_SOURCE */
1509 # if defined(_AIX3) || defined(_AIX4)
1510 if (knlist(Nl, 1, sizeof Nl[0]) < 0)
1511 # else /* defined(_AIX3) || defined(_AIX4) */
1512 if (nlist(_PATH_UNIX, Nl) < 0)
1513 # endif /* defined(_AIX3) || defined(_AIX4) */
1516 sm_dprintf("getla: nlist(%s): %s\n", _PATH_UNIX,
1517 sm_errstring(errno));
1520 if (Nl[X_AVENRUN].n_value == 0)
1523 sm_dprintf("getla: nlist(%s, %s) ==> 0\n",
1524 _PATH_UNIX, LA_AVENRUN);
1527 # ifdef NAMELISTMASK
1528 Nl[X_AVENRUN].n_value &= NAMELISTMASK;
1529 # endif /* NAMELISTMASK */
1531 kmem = open(_PATH_KMEM, 0, 0);
1535 sm_dprintf("getla: open(/dev/kmem): %s\n",
1536 sm_errstring(errno));
1539 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1540 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1543 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1544 sm_errstring(errno));
1551 sm_dprintf("getla: symbol address = %#lx\n",
1552 (unsigned long) Nl[X_AVENRUN].n_value);
1553 if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 ||
1554 read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
1558 sm_dprintf("getla: lseek or read: %s\n",
1559 sm_errstring(errno));
1562 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)
1565 # if LA_TYPE == LA_SHORT
1566 sm_dprintf("getla: avenrun = %d", avenrun[0]);
1568 sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1569 # else /* LA_TYPE == LA_SHORT */
1570 sm_dprintf("getla: avenrun = %ld", avenrun[0]);
1572 sm_dprintf(", %ld, %ld", avenrun[1], avenrun[2]);
1573 # endif /* LA_TYPE == LA_SHORT */
1577 sm_dprintf("getla: %d\n",
1578 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1579 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1580 # else /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) */
1583 sm_dprintf("getla: avenrun = %g", avenrun[0]);
1585 sm_dprintf(", %g, %g", avenrun[1], avenrun[2]);
1589 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1590 return ((int) (avenrun[0] + 0.5));
1591 # endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) */
1594 #endif /* (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) */
1596 #if LA_TYPE == LA_READKSYM
1598 # include <sys/ksym.h>
1604 static int kmem = -1;
1606 struct mioc_rksym mirk;
1610 kmem = open("/dev/kmem", 0, 0);
1614 sm_dprintf("getla: open(/dev/kmem): %s\n",
1615 sm_errstring(errno));
1618 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1619 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1622 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1623 sm_errstring(errno));
1629 mirk.mirk_symname = LA_AVENRUN;
1630 mirk.mirk_buf = avenrun;
1631 mirk.mirk_buflen = sizeof(avenrun);
1632 if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0)
1635 sm_dprintf("getla: ioctl(MIOC_READKSYM) failed: %s\n",
1636 sm_errstring(errno));
1641 sm_dprintf("getla: avenrun = %d", avenrun[0]);
1643 sm_dprintf(", %d, %d", avenrun[1], avenrun[2]);
1647 sm_dprintf("getla: %d\n",
1648 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1649 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1652 #endif /* LA_TYPE == LA_READKSYM */
1654 #if LA_TYPE == LA_DGUX
1656 # include <sys/dg_sys_info.h>
1661 struct dg_sys_info_load_info load_info;
1663 dg_sys_info((long *)&load_info,
1664 DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0);
1667 sm_dprintf("getla: %d\n", (int) (load_info.one_minute + 0.5));
1669 return ((int) (load_info.one_minute + 0.5));
1672 #endif /* LA_TYPE == LA_DGUX */
1674 #if LA_TYPE == LA_HPUX
1676 /* forward declarations to keep gcc from complaining */
1681 struct pst_diskinfo;
1682 struct pst_processor;
1684 struct pst_swapinfo;
1686 # include <sys/param.h>
1687 # include <sys/pstat.h>
1692 struct pst_dynamic pstd;
1694 if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic),
1695 (size_t) 1, 0) == -1)
1699 sm_dprintf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5));
1701 return (int) (pstd.psd_avg_1_min + 0.5);
1704 #endif /* LA_TYPE == LA_HPUX */
1706 #if LA_TYPE == LA_SUBR
1713 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0)
1716 sm_dprintf("getla: getloadavg failed: %s",
1717 sm_errstring(errno));
1721 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
1722 return ((int) (avenrun[0] + 0.5));
1725 #endif /* LA_TYPE == LA_SUBR */
1727 #if LA_TYPE == LA_MACH
1730 ** This has been tested on NEXTSTEP release 2.1/3.X.
1733 # if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0
1734 # include <mach/mach.h>
1735 # else /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1737 # endif /* defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 */
1742 processor_set_t default_set;
1743 kern_return_t error;
1744 unsigned int info_count;
1745 struct processor_set_basic_info info;
1748 error = processor_set_default(host_self(), &default_set);
1749 if (error != KERN_SUCCESS)
1752 sm_dprintf("getla: processor_set_default failed: %s",
1753 sm_errstring(errno));
1756 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
1757 if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
1758 &host, (processor_set_info_t)&info,
1759 &info_count) != KERN_SUCCESS)
1762 sm_dprintf("getla: processor_set_info failed: %s",
1763 sm_errstring(errno));
1767 sm_dprintf("getla: %d\n",
1768 (int) ((info.load_average + (LOAD_SCALE / 2)) /
1770 return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE;
1773 #endif /* LA_TYPE == LA_MACH */
1775 #if LA_TYPE == LA_PROCSTR
1776 # if SM_CONF_BROKEN_STRTOD
1777 ERROR: This OS has most likely a broken strtod() implemenentation.
1778 ERROR: The function is required for getla().
1779 ERROR: Check the compilation options _LA_PROCSTR and
1780 ERROR: _SM_CONF_BROKEN_STRTOD (without the leading _).
1781 # endif /* SM_CONF_BROKEN_STRTOD */
1784 ** Read /proc/loadavg for the load average. This is assumed to be
1785 ** in a format like "0.15 0.12 0.06".
1787 ** Initially intended for Linux. This has been in the kernel
1788 ** since at least 0.99.15.
1791 # ifndef _PATH_LOADAVG
1792 # define _PATH_LOADAVG "/proc/loadavg"
1793 # endif /* ! _PATH_LOADAVG */
1799 register int result;
1802 fp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_LOADAVG, SM_IO_RDONLY,
1807 sm_dprintf("getla: sm_io_open(%s): %s\n",
1808 _PATH_LOADAVG, sm_errstring(errno));
1811 result = sm_io_fscanf(fp, SM_TIME_DEFAULT, "%lf", &avenrun);
1812 (void) sm_io_close(fp, SM_TIME_DEFAULT);
1816 sm_dprintf("getla: sm_io_fscanf() = %d: %s\n",
1817 result, sm_errstring(errno));
1822 sm_dprintf("getla(): %.2f\n", avenrun);
1824 return ((int) (avenrun + 0.5));
1827 #endif /* LA_TYPE == LA_PROCSTR */
1829 #if LA_TYPE == LA_IRIX6
1831 # include <sys/sysmp.h>
1837 static int kmem = -1;
1842 kmem = open(_PATH_KMEM, 0, 0);
1846 sm_dprintf("getla: open(%s): %s\n", _PATH_KMEM,
1847 sm_errstring(errno));
1850 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
1851 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
1854 sm_dprintf("getla: fcntl(/dev/kmem, FD_CLOEXEC): %s\n",
1855 sm_errstring(errno));
1862 if (lseek(kmem, (sysmp(MP_KERNADDR, MPKA_AVENRUN) & 0x7fffffff), SEEK_SET) == -1 ||
1863 read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
1866 sm_dprintf("getla: lseek or read: %s\n",
1867 sm_errstring(errno));
1872 sm_dprintf("getla: avenrun = %ld", (long int) avenrun[0]);
1874 sm_dprintf(", %ld, %ld",
1875 (long int) avenrun[1], (long int) avenrun[2]);
1880 sm_dprintf("getla: %d\n",
1881 (int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1882 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
1885 #endif /* LA_TYPE == LA_IRIX6 */
1887 #if LA_TYPE == LA_KSTAT
1894 static kstat_ctl_t *kc = NULL;
1895 static kstat_t *ksp = NULL;
1899 if (kc == NULL) /* if not initialized before */
1904 sm_dprintf("getla: kstat_open(): %s\n",
1905 sm_errstring(errno));
1909 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
1913 sm_dprintf("getla: kstat_lookup(): %s\n",
1914 sm_errstring(errno));
1917 if (kstat_read(kc, ksp, NULL) < 0)
1920 sm_dprintf("getla: kstat_read(): %s\n",
1921 sm_errstring(errno));
1924 ksn = (kstat_named_t *) kstat_data_lookup(ksp, "avenrun_1min");
1925 la = ((double) ksn->value.ul + FSCALE/2) / FSCALE;
1926 /* kstat_close(kc); /o do not close for fast access */
1930 #endif /* LA_TYPE == LA_KSTAT */
1932 #if LA_TYPE == LA_DEVSHORT
1935 ** Read /dev/table/avenrun for the load average. This should contain
1936 ** three shorts for the 1, 5, and 15 minute loads. We only read the
1937 ** first, since that's all we care about.
1939 ** Intended for SCO OpenServer 5.
1942 # ifndef _PATH_AVENRUN
1943 # define _PATH_AVENRUN "/dev/table/avenrun"
1944 # endif /* ! _PATH_AVENRUN */
1949 static int afd = -1;
1956 if (afd == -1 || lseek(afd, 0L, SEEK_SET) == -1)
1960 afd = open(_PATH_AVENRUN, O_RDONLY|O_SYNC);
1963 sm_syslog(LOG_ERR, NOQID,
1964 "can't open %s: %s",
1965 _PATH_AVENRUN, sm_errstring(errno));
1970 r = read(afd, &avenrun, sizeof avenrun);
1973 sm_dprintf("getla: avenrun = %d\n", avenrun);
1974 loadav = (int) (avenrun + FSCALE/2) >> FSHIFT;
1976 sm_dprintf("getla: %d\n", loadav);
1980 #endif /* LA_TYPE == LA_DEVSHORT */
1982 #if LA_TYPE == LA_ALPHAOSF
1985 # include <sys/table.h>
1991 struct tbl_loadavg tab;
1993 if (table(TBL_LOADAVG, 0, &tab, 1, sizeof(tab)) == -1)
1996 sm_dprintf("getla: table %s\n", sm_errstring(errno));
2001 sm_dprintf("getla: scale = %d\n", tab.tl_lscale);
2004 ave = ((tab.tl_avenrun.l[2] + (tab.tl_lscale/2)) /
2007 ave = (int) (tab.tl_avenrun.d[2] + 0.5);
2010 sm_dprintf("getla: %d\n", ave);
2015 #endif /* LA_TYPE == LA_ALPHAOSF */
2017 #if LA_TYPE == LA_PSET
2024 if (pset_getloadavg(PS_MYID, avenrun,
2025 sizeof(avenrun) / sizeof(avenrun[0])) < 0)
2028 sm_dprintf("getla: pset_getloadavg failed: %s",
2029 sm_errstring(errno));
2033 sm_dprintf("getla: %d\n", (int) (avenrun[0] +0.5));
2034 return ((int) (avenrun[0] + 0.5));
2037 #endif /* LA_TYPE == LA_PSET */
2039 #if LA_TYPE == LA_ZERO
2045 sm_dprintf("getla: ZERO\n");
2049 #endif /* LA_TYPE == LA_ZERO */
2052 * Copyright 1989 Massachusetts Institute of Technology
2054 * Permission to use, copy, modify, distribute, and sell this software and its
2055 * documentation for any purpose is hereby granted without fee, provided that
2056 * the above copyright notice appear in all copies and that both that
2057 * copyright notice and this permission notice appear in supporting
2058 * documentation, and that the name of M.I.T. not be used in advertising or
2059 * publicity pertaining to distribution of the software without specific,
2060 * written prior permission. M.I.T. makes no representations about the
2061 * suitability of this software for any purpose. It is provided "as is"
2062 * without express or implied warranty.
2064 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
2065 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
2066 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2067 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
2068 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2069 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2071 * Authors: Many and varied...
2074 /* Non Apollo stuff removed by Don Lewis 11/15/93 */
2076 SM_UNUSED(static char rcsid[]) = "@(#)$OrigId: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $";
2081 # include <apollo/base.h>
2084 int getloadavg( call_data )
2085 caddr_t call_data; /* pointer to (double) return value */
2087 double *avenrun = (double *) call_data;
2092 proc1_$get_loadav(loadav, &st);
2093 *avenrun = loadav[0] / (double) (1 << 16);
2098 ** SM_GETLA -- get the current load average
2107 ** Set CurrentLA to the current load average.
2108 ** Set {load_avg} in GlobalMacros to the current load average.
2116 CurrentLA = getla();
2117 (void) sm_snprintf(labuf, sizeof labuf, "%d", CurrentLA);
2118 macdefine(&GlobalMacros, A_TEMP, macid("{load_avg}"), labuf);
2121 ** SHOULDQUEUE -- should this message be queued or sent?
2123 ** Compares the message cost to the load average to decide.
2125 ** Note: Do NOT change this API! It is documented in op.me
2126 ** and theoretically the user can change this function...
2129 ** pri -- the priority of the message in question.
2130 ** ct -- the message creation time (unused, but see above).
2133 ** true -- if this message should be queued up for the
2135 ** false -- if the load is low enough to send this message.
2143 shouldqueue(pri, ct)
2150 sm_dprintf("shouldqueue: CurrentLA=%d, pri=%ld: ",
2152 if (CurrentLA < QueueLA)
2155 sm_dprintf("false (CurrentLA < QueueLA)\n");
2158 # if 0 /* this code is reported to cause oscillation around RefuseLA */
2159 if (CurrentLA >= RefuseLA && QueueLA < RefuseLA)
2162 sm_dprintf("TRUE (CurrentLA >= RefuseLA)\n");
2166 rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1));
2168 sm_dprintf("%s (by calculation)\n", rval ? "true" : "false");
2172 ** REFUSECONNECTIONS -- decide if connections should be refused
2175 ** name -- daemon name (for error messages only)
2176 ** e -- the current envelope.
2177 ** d -- number of daemon
2178 ** active -- was this daemon actually active?
2181 ** true if incoming SMTP connections should be refused
2183 ** false if we should accept new work.
2186 ** Sets process title when it is rejecting connections.
2190 refuseconnections(name, e, d, active)
2196 static time_t lastconn[MAXDAEMONS];
2197 static int conncnt[MAXDAEMONS];
2199 static time_t firstrejtime[MAXDAEMONS];
2200 static time_t nextlogtime[MAXDAEMONS];
2201 #endif /* _FFR_REJECT_LOG */
2208 if (ConnRateThrottle > 0)
2215 if (now != lastconn[d])
2220 else if (conncnt[d]++ > ConnRateThrottle)
2222 #define D_MSG_CRT "deferring connections on daemon %s: %d per second"
2223 /* sleep to flatten out connection load */
2224 sm_setproctitle(true, e, D_MSG_CRT,
2225 name, ConnRateThrottle);
2227 sm_syslog(LOG_INFO, NOQID, D_MSG_CRT,
2228 name, ConnRateThrottle);
2232 else if (now != lastconn[d])
2237 if (RefuseLA > 0 && CurrentLA >= RefuseLA)
2239 # if _FFR_REJECT_LOG
2242 # define R2_MSG_LA "have been rejecting connections on daemon %s for %s"
2243 # endif /* _FFR_REJECT_LOG */
2244 # define R_MSG_LA "rejecting connections on daemon %s: load average: %d"
2245 sm_setproctitle(true, e, R_MSG_LA, name, CurrentLA);
2247 sm_syslog(LOG_NOTICE, NOQID, R_MSG_LA, name, CurrentLA);
2248 # if _FFR_REJECT_LOG
2250 if (firstrejtime[d] == 0)
2252 firstrejtime[d] = now;
2253 nextlogtime[d] = now + RejectLogInterval;
2255 else if (nextlogtime[d] < now)
2257 sm_syslog(LOG_ERR, NOQID, R2_MSG_LA, name,
2258 pintvl(now - firstrejtime[d], true));
2259 nextlogtime[d] = now + RejectLogInterval;
2261 # endif /* _FFR_REJECT_LOG */
2264 # if _FFR_REJECT_LOG
2266 firstrejtime[d] = 0;
2267 # endif /* _FFR_REJECT_LOG */
2269 if (DelayLA > 0 && CurrentLA >= DelayLA)
2272 static time_t log_delay = (time_t) 0;
2274 # define MIN_DELAY_LOG 90 /* wait before logging this again */
2275 # define D_MSG_LA "delaying connections on daemon %s: load average=%d >= %d"
2276 /* sleep to flatten out connection load */
2277 sm_setproctitle(true, e, D_MSG_LA, name, DelayLA);
2278 if (LogLevel > 8 && (now = curtime()) > log_delay)
2280 sm_syslog(LOG_INFO, NOQID, D_MSG_LA,
2281 name, CurrentLA, DelayLA);
2282 log_delay = now + MIN_DELAY_LOG;
2287 if (MaxChildren > 0 && CurChildren >= MaxChildren)
2290 if (CurChildren >= MaxChildren)
2292 #define R_MSG_CHILD "rejecting connections on daemon %s: %d children, max %d"
2293 sm_setproctitle(true, e, R_MSG_CHILD,
2294 name, CurChildren, MaxChildren);
2296 sm_syslog(LOG_INFO, NOQID, R_MSG_CHILD,
2297 name, CurChildren, MaxChildren);
2304 ** SETPROCTITLE -- set process title for ps
2307 ** fmt -- a printf style format string.
2308 ** a, b, c -- possible parameters to fmt.
2314 ** Clobbers argv of our main procedure so ps(1) will
2315 ** display the title.
2318 #define SPT_NONE 0 /* don't use it at all */
2319 #define SPT_REUSEARGV 1 /* cover argv with title information */
2320 #define SPT_BUILTIN 2 /* use libc builtin */
2321 #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */
2322 #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */
2323 #define SPT_SYSMIPS 5 /* use sysmips() supported by NEWS-OS 6 */
2324 #define SPT_SCO 6 /* write kernel u. area */
2325 #define SPT_CHANGEARGV 7 /* write our own strings into argv[] */
2328 # define SPT_TYPE SPT_REUSEARGV
2329 #endif /* ! SPT_TYPE */
2332 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
2334 # if SPT_TYPE == SPT_PSTAT
2335 # include <sys/pstat.h>
2336 # endif /* SPT_TYPE == SPT_PSTAT */
2337 # if SPT_TYPE == SPT_PSSTRINGS
2338 # include <machine/vmparam.h>
2339 # include <sys/exec.h>
2340 # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */
2342 # define SPT_TYPE SPT_REUSEARGV
2343 # else /* ! PS_STRINGS */
2344 # ifndef NKPDE /* FreeBSD 2.0 */
2346 typedef unsigned int *pt_entry_t;
2347 # endif /* ! NKPDE */
2348 # endif /* ! PS_STRINGS */
2349 # endif /* SPT_TYPE == SPT_PSSTRINGS */
2351 # if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
2352 # define SETPROC_STATIC static
2353 # else /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2354 # define SETPROC_STATIC
2355 # endif /* SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV */
2357 # if SPT_TYPE == SPT_SYSMIPS
2358 # include <sys/sysmips.h>
2359 # include <sys/sysnews.h>
2360 # endif /* SPT_TYPE == SPT_SYSMIPS */
2362 # if SPT_TYPE == SPT_SCO
2363 # include <sys/immu.h>
2364 # include <sys/dir.h>
2365 # include <sys/user.h>
2366 # include <sys/fs/s5param.h>
2367 # if PSARGSZ > MAXLINE
2368 # define SPT_BUFSIZE PSARGSZ
2369 # endif /* PSARGSZ > MAXLINE */
2370 # endif /* SPT_TYPE == SPT_SCO */
2372 # ifndef SPT_PADCHAR
2373 # define SPT_PADCHAR ' '
2374 # endif /* ! SPT_PADCHAR */
2376 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
2379 # define SPT_BUFSIZE MAXLINE
2380 #endif /* ! SPT_BUFSIZE */
2385 ** It looks like the Compaq Tru64 5.1A now aligns argv and envp to
2386 ** 64 bit alignment, so unless each piece of argv and envp is a multiple
2387 ** of 8 bytes (including terminating NULL), initsetproctitle() won't use
2388 ** any of the space beyond argv[0]. Be sure to set SPT_ALIGN_SIZE if
2389 ** you use this FFR.
2392 # ifdef SPT_ALIGN_SIZE
2393 # define SPT_ALIGN(x, align) (((((x) + SPT_ALIGN_SIZE) >> (align)) << (align)) - 1)
2394 # else /* SPT_ALIGN_SIZE */
2395 # define SPT_ALIGN(x, align) (x)
2396 # endif /* SPT_ALIGN_SIZE */
2397 #else /* _FFR_SPT_ALIGN */
2398 # define SPT_ALIGN(x, align) (x)
2399 #endif /* _FFR_SPT_ALIGN */
2402 ** Pointers for setproctitle.
2403 ** This allows "ps" listings to give more useful information.
2406 static char **Argv = NULL; /* pointer to argument vector */
2407 static char *LastArgv = NULL; /* end of argv */
2408 #if SPT_TYPE != SPT_BUILTIN
2409 static void setproctitle __P((const char *, ...));
2410 #endif /* SPT_TYPE != SPT_BUILTIN */
2413 initsetproctitle(argc, argv, envp)
2420 extern char **environ;
2423 ** Move the environment so setproctitle can use the space at
2424 ** the top of memory.
2429 for (i = 0; envp[i] != NULL; i++)
2431 environ = (char **) xalloc(sizeof (char *) * (i + 1));
2432 for (i = 0; envp[i] != NULL; i++)
2433 environ[i] = newstr(envp[i]);
2438 ** Save start and extent of argv for setproctitle.
2444 ** Determine how much space we can use for setproctitle.
2445 ** Use all contiguous argv and envp pointers starting at argv[0]
2450 # ifdef SPT_ALIGN_SIZE
2451 for (i = SPT_ALIGN_SIZE; i > 0; i >>= 1)
2453 # endif /* SPT_ALIGN_SIZE */
2454 # endif /* _FFR_SPT_ALIGN */
2456 for (i = 0; i < argc; i++)
2458 if (i == 0 || LastArgv + 1 == argv[i])
2459 LastArgv = argv[i] + SPT_ALIGN(strlen(argv[i]), align);
2461 for (i = 0; LastArgv != NULL && envp != NULL && envp[i] != NULL; i++)
2463 if (LastArgv + 1 == envp[i])
2464 LastArgv = envp[i] + SPT_ALIGN(strlen(envp[i]), align);
2468 #if SPT_TYPE != SPT_BUILTIN
2473 setproctitle(const char *fmt, ...)
2474 # else /* __STDC__ */
2475 setproctitle(fmt, va_alist)
2478 # endif /* __STDC__ */
2480 # if SPT_TYPE != SPT_NONE
2483 SETPROC_STATIC char buf[SPT_BUFSIZE];
2485 # if SPT_TYPE == SPT_PSTAT
2487 # endif /* SPT_TYPE == SPT_PSTAT */
2488 # if SPT_TYPE == SPT_SCO
2491 static int kmem = -1;
2492 static pid_t kmempid = -1;
2494 # endif /* SPT_TYPE == SPT_SCO */
2498 /* print sendmail: heading for grep */
2499 (void) sm_strlcpy(p, "sendmail: ", SPACELEFT(buf, p));
2502 /* print the argument string */
2503 SM_VA_START(ap, fmt);
2504 (void) sm_vsnprintf(p, SPACELEFT(buf, p), fmt, ap);
2507 i = (int) strlen(buf);
2511 # if SPT_TYPE == SPT_PSTAT
2512 pst.pst_command = buf;
2513 pstat(PSTAT_SETCMD, pst, i, 0, 0);
2514 # endif /* SPT_TYPE == SPT_PSTAT */
2515 # if SPT_TYPE == SPT_PSSTRINGS
2516 PS_STRINGS->ps_nargvstr = 1;
2517 PS_STRINGS->ps_argvstr = buf;
2518 # endif /* SPT_TYPE == SPT_PSSTRINGS */
2519 # if SPT_TYPE == SPT_SYSMIPS
2520 sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
2521 # endif /* SPT_TYPE == SPT_SYSMIPS */
2522 # if SPT_TYPE == SPT_SCO
2523 if (kmem < 0 || kmempid != CurrentPid)
2527 kmem = open(_PATH_KMEM, O_RDWR, 0);
2530 if ((j = fcntl(kmem, F_GETFD, 0)) < 0 ||
2531 fcntl(kmem, F_SETFD, j | FD_CLOEXEC) < 0)
2537 kmempid = CurrentPid;
2539 buf[PSARGSZ - 1] = '\0';
2540 seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
2541 if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
2542 (void) write(kmem, buf, PSARGSZ);
2543 # endif /* SPT_TYPE == SPT_SCO */
2544 # if SPT_TYPE == SPT_REUSEARGV
2545 if (LastArgv == NULL)
2548 if (i > LastArgv - Argv[0] - 2)
2550 i = LastArgv - Argv[0] - 2;
2553 (void) sm_strlcpy(Argv[0], buf, i + 1);
2555 while (p < LastArgv)
2558 # endif /* SPT_TYPE == SPT_REUSEARGV */
2559 # if SPT_TYPE == SPT_CHANGEARGV
2562 # endif /* SPT_TYPE == SPT_CHANGEARGV */
2563 # endif /* SPT_TYPE != SPT_NONE */
2566 #endif /* SPT_TYPE != SPT_BUILTIN */
2568 ** SM_SETPROCTITLE -- set process task and set process title for ps
2570 ** Possibly set process status and call setproctitle() to
2571 ** change the ps display.
2574 ** status -- whether or not to store as process status
2575 ** e -- the current envelope.
2576 ** fmt -- a printf style format string.
2577 ** a, b, c -- possible parameters to fmt.
2586 sm_setproctitle(bool status, ENVELOPE *e, const char *fmt, ...)
2587 #else /* __STDC__ */
2588 sm_setproctitle(status, e, fmt, va_alist)
2593 #endif /* __STDC__ */
2595 char buf[SPT_BUFSIZE];
2598 /* print the argument string */
2599 SM_VA_START(ap, fmt);
2600 (void) sm_vsnprintf(buf, sizeof buf, fmt, ap);
2604 proc_list_set(CurrentPid, buf);
2606 if (ProcTitlePrefix != NULL)
2608 char prefix[SPT_BUFSIZE];
2610 expand(ProcTitlePrefix, prefix, sizeof prefix, e);
2611 setproctitle("%s: %s", prefix, buf);
2614 setproctitle("%s", buf);
2617 ** WAITFOR -- wait for a particular process id.
2620 ** pid -- process id to wait for.
2624 ** -1 if pid never shows up.
2642 proc_list_drop(i, st, NULL);
2643 } while ((i >= 0 || errno == EINTR) && i != pid);
2652 ** status -- pointer to status (return value)
2664 # else /* WAITUNION */
2666 # endif /* WAITUNION */
2668 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2670 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2672 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2673 savesig = sm_releasesignal(SIGCHLD);
2674 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2676 # if defined(ISC_UNIX) || defined(_SCO_unix_)
2678 sm_blocksignal(SIGCHLD);
2679 # endif /* defined(ISC_UNIX) || defined(_SCO_unix_) */
2681 *status = st.w_status;
2682 # else /* WAITUNION */
2684 # endif /* WAITUNION */
2688 ** REAPCHILD -- pick up the body of my child, lest it become a zombie
2691 ** sig -- the signal that got us here (unused).
2697 ** Picks up extant zombies.
2698 ** Control socket exits may restart/shutdown daemon.
2700 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2701 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2710 int save_errno = errno;
2718 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
2723 # else /* HASWAITPID */
2727 while ((pid = wait3(&status, WNOHANG, (struct rusage *) NULL)) > 0)
2729 st = status.w_status;
2730 # else /* WNOHANG */
2734 ** Catch one zombie -- we will be re-invoked (we hope) if there
2735 ** are more. Unreliable signals probably break this, but this
2736 ** is the "old system" situation -- waitpid or wait3 are to be
2737 ** strongly preferred.
2740 if ((pid = wait(&status)) > 0)
2743 # endif /* WNOHANG */
2744 # endif /* HASWAITPID */
2745 /* Drop PID and check if it was a control socket child */
2746 proc_list_drop(pid, st, NULL);
2748 FIX_SYSV_SIGNAL(sig, reapchild);
2750 return SIGFUNC_RETURN;
2753 ** GETDTABLESIZE -- return number of file descriptors
2755 ** Only on non-BSD systems
2761 ** size of file descriptor table
2768 # include <sys/resource.h>
2769 #endif /* SOLARIS */
2774 # ifdef RLIMIT_NOFILE
2777 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
2779 # endif /* RLIMIT_NOFILE */
2781 # if HASGETDTABLESIZE
2782 return getdtablesize();
2783 # else /* HASGETDTABLESIZE */
2784 # ifdef _SC_OPEN_MAX
2785 return sysconf(_SC_OPEN_MAX);
2786 # else /* _SC_OPEN_MAX */
2788 # endif /* _SC_OPEN_MAX */
2789 # endif /* HASGETDTABLESIZE */
2792 ** UNAME -- get the UUCP name of this system.
2799 struct utsname *name;
2804 name->nodename[0] = '\0';
2806 /* try /etc/whoami -- one line with the node name */
2807 if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, "/etc/whoami",
2808 SM_IO_RDONLY, NULL)) != NULL)
2810 (void) sm_io_fgets(file, SM_TIME_DEFAULT, name->nodename,
2812 (void) sm_io_close(file, SM_TIME_DEFAULT);
2813 n = strchr(name->nodename, '\n');
2816 if (name->nodename[0] != '\0')
2820 /* try /usr/include/whoami.h -- has a #define somewhere */
2821 if ((file = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
2822 "/usr/include/whoami.h", SM_IO_RDONLY, NULL))
2827 while (sm_io_fgets(file, SM_TIME_DEFAULT,
2828 buf, sizeof buf) != NULL)
2830 if (sm_io_sscanf(buf, "#define sysname \"%*[^\"]\"",
2831 NODE_LENGTH, name->nodename) > 0)
2834 (void) sm_io_close(file, SM_TIME_DEFAULT);
2835 if (name->nodename[0] != '\0')
2841 #endif /* !HASUNAME */
2843 ** INITGROUPS -- initialize groups
2845 ** Stub implementation for System V style systems
2850 initgroups(name, basegid)
2857 #endif /* !HASINITGROUPS */
2859 ** SETGROUPS -- set group list
2861 ** Stub implementation for systems that don't have group lists
2867 setgroups(ngroups, grouplist)
2869 GIDSET_T grouplist[];
2874 #endif /* ! NGROUPS_MAX */
2876 ** SETSID -- set session id (for non-POSIX systems)
2887 fd = open("/dev/tty", O_RDWR, 0);
2890 (void) ioctl(fd, TIOCNOTTY, (char *) 0);
2893 # endif /* TIOCNOTTY */
2896 # else /* SYS5SETPGRP */
2897 return setpgid(0, CurrentPid);
2898 # endif /* SYS5SETPGRP */
2901 #endif /* !HASSETSID */
2903 ** FSYNC -- dummy fsync
2912 return fcntl(fd, F_SETFL, O_SYNC);
2914 /* nothing we can do */
2916 # endif /* O_SYNC */
2919 #endif /* NEEDFSYNC */
2921 ** DGUX_INET_ADDR -- inet_addr for DG/UX
2923 ** Data General DG/UX version of inet_addr returns a struct in_addr
2924 ** instead of a long. This patches things. Only needed on versions
2933 dgux_inet_addr(host)
2936 struct in_addr haddr;
2938 haddr = inet_addr(host);
2939 return haddr.s_addr;
2942 #endif /* DGUX_5_4_2 */
2944 ** GETOPT -- for old systems or systems with bogus implementations
2950 * Copyright (c) 1985 Regents of the University of California.
2951 * All rights reserved. The Berkeley software License Agreement
2952 * specifies the terms and conditions for redistribution.
2957 ** this version hacked to add `atend' flag to allow state machine
2958 ** to reset if invoked by the program to scan args for a 2nd time
2961 # if defined(LIBC_SCCS) && !defined(lint)
2962 static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86";
2963 # endif /* defined(LIBC_SCCS) && !defined(lint) */
2966 ** get option letter from argument vector
2968 # ifdef _CONVEX_SOURCE
2969 extern int optind, opterr, optopt;
2970 extern char *optarg;
2971 # else /* _CONVEX_SOURCE */
2972 int opterr = 1; /* if error message should be printed */
2973 int optind = 1; /* index into parent argv vector */
2974 int optopt = 0; /* character checked for validity */
2975 char *optarg = NULL; /* argument associated with option */
2976 # endif /* _CONVEX_SOURCE */
2978 # define BADCH (int)'?'
2980 # define tell(s) if (opterr) \
2981 {sm_io_fputs(smioerr, SM_TIME_DEFAULT, *nargv); \
2982 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, s); \
2983 (void) sm_io_putc(smioerr, SM_TIME_DEFAULT, optopt); \
2984 (void) sm_io_putc(smioerr, SM_TIME_DEFAULT, '\n'); \
2988 getopt(nargc,nargv,ostr)
2993 static char *place = EMSG; /* option letter processing */
2994 static char atend = 0;
2995 register char *oli = NULL; /* option letter list index */
3001 if(!*place) { /* update scanning pointer */
3002 if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) {
3006 if (*place == '-') { /* found "--" */
3011 } /* option letter okay? */
3012 if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
3013 if (!*place) ++optind;
3014 tell(": illegal option -- ");
3016 if (oli && *++oli != ':') { /* don't need argument */
3018 if (!*place) ++optind;
3020 else { /* need an argument */
3021 if (*place) optarg = place; /* no white space */
3022 else if (nargc <= ++optind) { /* no arg */
3024 tell(": option requires an argument -- ");
3026 else optarg = nargv[optind]; /* white space */
3030 return optopt; /* dump back option letter */
3033 #endif /* !SM_CONF_GETOPT */
3035 ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use
3038 ** user -- the name of the user we are checking.
3039 ** shell -- the user's shell from /etc/passwd
3042 ** true -- if it is ok to use this for unrestricted access.
3043 ** false -- if the shell is restricted.
3046 #if !HASGETUSERSHELL
3048 # ifndef _PATH_SHELLS
3049 # define _PATH_SHELLS "/etc/shells"
3050 # endif /* ! _PATH_SHELLS */
3052 # if defined(_AIX3) || defined(_AIX4)
3053 # include <userconf.h>
3055 # include <userpw.h>
3056 # endif /* _AIX4 >= 40200 */
3057 # include <usersec.h>
3058 # endif /* defined(_AIX3) || defined(_AIX4) */
3060 static char *DefaultUserShells[] =
3062 "/bin/sh", /* standard shell */
3067 "/bin/csh", /* C shell */
3072 "/usr/bin/rsh", /* restricted Bourne shell */
3073 "/usr/bin/ksh", /* Korn shell */
3074 "/usr/bin/rksh", /* restricted Korn shell */
3076 "/usr/bin/keysh", /* key shell (extended Korn shell) */
3077 "/usr/bin/posix/sh",
3079 "/bin/rsh", /* restricted Bourne shell */
3080 "/bin/ksh", /* Korn shell */
3081 "/bin/rksh", /* restricted Korn shell */
3083 "/usr/bin/keysh", /* key shell (extended Korn shell) */
3087 # endif /* __hpux */
3088 # if defined(_AIX3) || defined(_AIX4)
3089 "/bin/ksh", /* Korn shell */
3091 "/bin/tsh", /* trusted shell */
3093 "/bin/bsh", /* Bourne shell */
3095 # endif /* defined(_AIX3) || defined(_AIX4) */
3096 # if defined(__svr4__) || defined(__svr5__)
3097 "/bin/ksh", /* Korn shell */
3099 # endif /* defined(__svr4__) || defined(__svr5__) */
3101 "/sbin/sh", /* SGI's shells really live in /sbin */
3103 "/sbin/bsh", /* classic Bourne shell */
3106 "/sbin/csh", /* standard csh */
3109 "/sbin/jsh", /* classic Bourne shell w/ job control*/
3112 "/bin/ksh", /* Korn shell */
3115 "/sbin/tcsh", /* Extended csh */
3122 #endif /* !HASGETUSERSHELL */
3124 #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/"
3127 usershellok(user, shell)
3131 # if HASGETUSERSHELL
3133 extern char *getusershell();
3135 if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3140 while ((p = getusershell()) != NULL)
3141 if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0)
3145 # else /* HASGETUSERSHELL */
3148 # endif /* USEGETCONFATTR */
3149 register SM_FILE_T *shellf;
3152 if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') ||
3158 ** Naturally IBM has a "better" idea.....
3160 ** What a crock. This interface isn't documented, it is
3161 ** considered part of the security library (-ls), and it
3162 ** only works if you are running as root (since the list
3163 ** of valid shells is obviously a source of great concern).
3164 ** I recommend that you do NOT define USEGETCONFATTR,
3165 ** especially since you are going to have to set up an
3166 ** /etc/shells anyhow to handle the cases where getconfattr
3170 if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL)
3174 if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0)
3180 # endif /* USEGETCONFATTR */
3182 shellf = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, _PATH_SHELLS,
3183 SM_IO_RDONLY, NULL);
3186 /* no /etc/shells; see if it is one of the std shells */
3189 if (errno != ENOENT && LogLevel > 3)
3190 sm_syslog(LOG_ERR, NOQID,
3191 "usershellok: cannot open %s: %s",
3192 _PATH_SHELLS, sm_errstring(errno));
3194 for (d = DefaultUserShells; *d != NULL; d++)
3196 if (strcmp(shell, *d) == 0)
3202 while (sm_io_fgets(shellf, SM_TIME_DEFAULT, buf, sizeof buf) != NULL)
3204 register char *p, *q;
3207 while (*p != '\0' && *p != '#' && *p != '/')
3209 if (*p == '#' || *p == '\0')
3212 while (*p != '\0' && *p != '#' && !(isascii(*p) && isspace(*p)))
3215 if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0)
3217 (void) sm_io_close(shellf, SM_TIME_DEFAULT);
3221 (void) sm_io_close(shellf, SM_TIME_DEFAULT);
3223 # endif /* HASGETUSERSHELL */
3226 ** FREEDISKSPACE -- see how much free space is on the queue filesystem
3228 ** Only implemented if you have statfs.
3231 ** dir -- the directory in question.
3232 ** bsize -- a variable into which the filesystem
3233 ** block size is stored.
3236 ** The number of blocks free on the queue filesystem.
3237 ** -1 if the statfs call fails.
3240 ** Puts the filesystem block size into bsize.
3244 # define SFS_NONE 0 /* no statfs implementation */
3245 # define SFS_USTAT 1 /* use ustat */
3246 # define SFS_4ARGS 2 /* use four-argument statfs call */
3247 # define SFS_VFS 3 /* use <sys/vfs.h> implementation */
3248 # define SFS_MOUNT 4 /* use <sys/mount.h> implementation */
3249 # define SFS_STATFS 5 /* use <sys/statfs.h> implementation */
3250 # define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */
3253 # define SFS_TYPE SFS_NONE
3254 # endif /* ! SFS_TYPE */
3256 # if SFS_TYPE == SFS_USTAT
3258 # endif /* SFS_TYPE == SFS_USTAT */
3259 # if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS
3260 # include <sys/statfs.h>
3261 # endif /* SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS */
3262 # if SFS_TYPE == SFS_VFS
3263 # include <sys/vfs.h>
3264 # endif /* SFS_TYPE == SFS_VFS */
3265 # if SFS_TYPE == SFS_MOUNT
3266 # include <sys/mount.h>
3267 # endif /* SFS_TYPE == SFS_MOUNT */
3268 # if SFS_TYPE == SFS_STATVFS
3269 # include <sys/statvfs.h>
3270 # endif /* SFS_TYPE == SFS_STATVFS */
3273 freediskspace(dir, bsize)
3277 # if SFS_TYPE == SFS_NONE
3281 /* assume free space is plentiful */
3282 return (long) LONG_MAX;
3283 # else /* SFS_TYPE == SFS_NONE */
3284 # if SFS_TYPE == SFS_USTAT
3286 struct stat statbuf;
3287 # define FSBLOCKSIZE DEV_BSIZE
3288 # define SFS_BAVAIL f_tfree
3289 # else /* SFS_TYPE == SFS_USTAT */
3290 # if defined(ultrix)
3292 # define SFS_BAVAIL fd_bfreen
3293 # define FSBLOCKSIZE 1024L
3294 # else /* defined(ultrix) */
3295 # if SFS_TYPE == SFS_STATVFS
3297 # define FSBLOCKSIZE fs.f_frsize
3298 # else /* SFS_TYPE == SFS_STATVFS */
3300 # define FSBLOCKSIZE fs.f_bsize
3301 # endif /* SFS_TYPE == SFS_STATVFS */
3302 # endif /* defined(ultrix) */
3303 # endif /* SFS_TYPE == SFS_USTAT */
3305 # define SFS_BAVAIL f_bavail
3306 # endif /* ! SFS_BAVAIL */
3308 # if SFS_TYPE == SFS_USTAT
3309 if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0)
3310 # else /* SFS_TYPE == SFS_USTAT */
3311 # if SFS_TYPE == SFS_4ARGS
3312 if (statfs(dir, &fs, sizeof fs, 0) == 0)
3313 # else /* SFS_TYPE == SFS_4ARGS */
3314 # if SFS_TYPE == SFS_STATVFS
3315 if (statvfs(dir, &fs) == 0)
3316 # else /* SFS_TYPE == SFS_STATVFS */
3317 # if defined(ultrix)
3318 if (statfs(dir, &fs) > 0)
3319 # else /* defined(ultrix) */
3320 if (statfs(dir, &fs) == 0)
3321 # endif /* defined(ultrix) */
3322 # endif /* SFS_TYPE == SFS_STATVFS */
3323 # endif /* SFS_TYPE == SFS_4ARGS */
3324 # endif /* SFS_TYPE == SFS_USTAT */
3327 *bsize = FSBLOCKSIZE;
3328 if (fs.SFS_BAVAIL <= 0)
3330 else if (fs.SFS_BAVAIL > LONG_MAX)
3331 return (long) LONG_MAX;
3333 return (long) fs.SFS_BAVAIL;
3336 # endif /* SFS_TYPE == SFS_NONE */
3339 ** ENOUGHDISKSPACE -- is there enough free space on the queue file systems?
3342 ** msize -- the size to check against. If zero, we don't yet
3343 ** know how big the message will be, so just check for
3344 ** a "reasonable" amount.
3345 ** e -- envelope, or NULL -- controls logging
3348 ** true if in every queue group there is at least one
3349 ** queue directory whose file system contains enough free space.
3353 ** If there is not enough disk space and e != NULL
3354 ** then sm_syslog is called.
3358 enoughdiskspace(msize, e)
3364 if (MinBlocksFree <= 0 && msize <= 0)
3367 sm_dprintf("enoughdiskspace: no threshold\n");
3372 for (i = 0; i < NumQueue; ++i)
3374 if (pickqdir(Queue[i], msize, e) < 0)
3380 ** TRANSIENTERROR -- tell if an error code indicates a transient failure
3382 ** This looks at an errno value and tells if this is likely to
3383 ** go away if retried later.
3386 ** err -- the errno code to classify.
3389 ** true if this is probably transient.
3399 case EIO: /* I/O error */
3400 case ENXIO: /* Device not configured */
3401 case EAGAIN: /* Resource temporarily unavailable */
3402 case ENOMEM: /* Cannot allocate memory */
3403 case ENODEV: /* Operation not supported by device */
3404 case ENFILE: /* Too many open files in system */
3405 case EMFILE: /* Too many open files */
3406 case ENOSPC: /* No space left on device */
3407 case ETIMEDOUT: /* Connection timed out */
3409 case ESTALE: /* Stale NFS file handle */
3412 case ENETDOWN: /* Network is down */
3413 #endif /* ENETDOWN */
3415 case ENETUNREACH: /* Network is unreachable */
3416 #endif /* ENETUNREACH */
3418 case ENETRESET: /* Network dropped connection on reset */
3419 #endif /* ENETRESET */
3421 case ECONNABORTED: /* Software caused connection abort */
3422 #endif /* ECONNABORTED */
3424 case ECONNRESET: /* Connection reset by peer */
3425 #endif /* ECONNRESET */
3427 case ENOBUFS: /* No buffer space available */
3428 #endif /* ENOBUFS */
3430 case ESHUTDOWN: /* Can't send after socket shutdown */
3431 #endif /* ESHUTDOWN */
3433 case ECONNREFUSED: /* Connection refused */
3434 #endif /* ECONNREFUSED */
3436 case EHOSTDOWN: /* Host is down */
3437 #endif /* EHOSTDOWN */
3439 case EHOSTUNREACH: /* No route to host */
3440 #endif /* EHOSTUNREACH */
3442 case EDQUOT: /* Disc quota exceeded */
3445 case EPROCLIM: /* Too many processes */
3446 #endif /* EPROCLIM */
3448 case EUSERS: /* Too many users */
3451 case EDEADLK: /* Resource deadlock avoided */
3452 #endif /* EDEADLK */
3454 case EISCONN: /* Socket already connected */
3455 #endif /* EISCONN */
3457 case EINPROGRESS: /* Operation now in progress */
3458 #endif /* EINPROGRESS */
3460 case EALREADY: /* Operation already in progress */
3461 #endif /* EALREADY */
3463 case EADDRINUSE: /* Address already in use */
3464 #endif /* EADDRINUSE */
3465 #ifdef EADDRNOTAVAIL
3466 case EADDRNOTAVAIL: /* Can't assign requested address */
3467 #endif /* EADDRNOTAVAIL */
3469 case ETXTBSY: /* (Apollo) file locked */
3470 #endif /* ETXTBSY */
3471 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR))
3472 case ENOSR: /* Out of streams resources */
3473 #endif /* defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) */
3475 case ENOLCK: /* No locks available */
3477 case E_SM_OPENTIMEOUT: /* PSEUDO: open timed out */
3481 /* nope, must be permanent */
3485 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking
3488 ** fd -- the file descriptor of the file.
3489 ** filename -- the file name (for error messages).
3490 ** ext -- the filename extension.
3491 ** type -- type of the lock. Bits can be:
3492 ** LOCK_EX -- exclusive lock.
3493 ** LOCK_NB -- non-blocking.
3494 ** LOCK_UN -- unlock.
3497 ** true if the lock was acquired.
3502 lockfile(fd, filename, ext, type)
3517 memset(&lfd, '\0', sizeof lfd);
3518 if (bitset(LOCK_UN, type))
3519 lfd.l_type = F_UNLCK;
3520 else if (bitset(LOCK_EX, type))
3521 lfd.l_type = F_WRLCK;
3523 lfd.l_type = F_RDLCK;
3525 if (bitset(LOCK_NB, type))
3531 sm_dprintf("lockfile(%s%s, action=%d, type=%d): ",
3532 filename, ext, action, lfd.l_type);
3534 while ((i = fcntl(fd, action, &lfd)) < 0 && errno == EINTR)
3539 sm_dprintf("SUCCESS\n");
3545 sm_dprintf("(%s) ", sm_errstring(save_errno));
3548 ** On SunOS, if you are testing using -oQ/tmp/mqueue or
3549 ** -oA/tmp/aliases or anything like that, and /tmp is mounted
3550 ** as type "tmp" (that is, served from swap space), the
3551 ** previous fcntl will fail with "Invalid argument" errors.
3552 ** Since this is fairly common during testing, we will assume
3553 ** that this indicates that the lock is successfully grabbed.
3556 if (save_errno == EINVAL)
3559 sm_dprintf("SUCCESS\n");
3563 if (!bitset(LOCK_NB, type) ||
3564 (save_errno != EACCES && save_errno != EAGAIN))
3566 int omode = fcntl(fd, F_GETFL, 0);
3567 uid_t euid = geteuid();
3570 syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
3571 filename, ext, fd, type, omode, euid);
3572 dumpfd(fd, true, true);
3574 # else /* !HASFLOCK */
3579 sm_dprintf("lockfile(%s%s, type=%o): ", filename, ext, type);
3581 while ((i = flock(fd, type)) < 0 && errno == EINTR)
3586 sm_dprintf("SUCCESS\n");
3592 sm_dprintf("(%s) ", sm_errstring(save_errno));
3594 if (!bitset(LOCK_NB, type) || save_errno != EWOULDBLOCK)
3596 int omode = fcntl(fd, F_GETFL, 0);
3597 uid_t euid = geteuid();
3600 syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)",
3601 filename, ext, fd, type, omode, euid);
3602 dumpfd(fd, true, true);
3604 # endif /* !HASFLOCK */
3606 sm_dprintf("FAIL\n");
3611 ** CHOWNSAFE -- tell if chown is "safe" (executable only by root)
3613 ** Unfortunately, given that we can't predict other systems on which
3614 ** a remote mounted (NFS) filesystem will be mounted, the answer is
3615 ** almost always that this is unsafe.
3617 ** Note also that many operating systems have non-compliant
3618 ** implementations of the _POSIX_CHOWN_RESTRICTED variable and the
3619 ** fpathconf() routine. According to IEEE 1003.1-1990, if
3620 ** _POSIX_CHOWN_RESTRICTED is defined and not equal to -1, then
3621 ** no non-root process can give away the file. However, vendors
3622 ** don't take NFS into account, so a comfortable value of
3623 ** _POSIX_CHOWN_RESTRICTED tells us nothing.
3625 ** Also, some systems (e.g., IRIX 6.2) return 1 from fpathconf()
3626 ** even on files where chown is not restricted. Many systems get
3627 ** this wrong on NFS-based filesystems (that is, they say that chown
3628 ** is restricted [safe] on NFS filesystems where it may not be, since
3629 ** other systems can access the same filesystem and do file giveaway;
3630 ** only the NFS server knows for sure!) Hence, it is important to
3631 ** get the value of SAFENFSPATHCONF correct -- it should be defined
3632 ** _only_ after testing (see test/t_pathconf.c) a system on an unsafe
3633 ** NFS-based filesystem to ensure that you can get meaningful results.
3634 ** If in doubt, assume unsafe!
3636 ** You may also need to tweak IS_SAFE_CHOWN -- it should be a
3637 ** condition indicating whether the return from pathconf indicates
3638 ** that chown is safe (typically either > 0 or >= 0 -- there isn't
3639 ** even any agreement about whether a zero return means that a file
3640 ** is or is not safe). It defaults to "> 0".
3642 ** If the parent directory is safe (writable only by owner back
3643 ** to the root) then we can relax slightly and trust fpathconf
3644 ** in more circumstances. This is really a crock -- if this is an
3645 ** NFS mounted filesystem then we really know nothing about the
3646 ** underlying implementation. However, most systems pessimize and
3647 ** return an error (EINVAL or EOPNOTSUPP) on NFS filesystems, which
3648 ** we interpret as unsafe, as we should. Thus, this heuristic gets
3649 ** us into a possible problem only on systems that have a broken
3650 ** pathconf implementation and which are also poorly configured
3651 ** (have :include: files in group- or world-writable directories).
3654 ** fd -- the file descriptor to check.
3655 ** safedir -- set if the parent directory is safe.
3658 ** true -- if the chown(2) operation is "safe" -- that is,
3659 ** only root can chown the file to an arbitrary user.
3660 ** false -- if an arbitrary user can give away a file.
3663 #ifndef IS_SAFE_CHOWN
3664 # define IS_SAFE_CHOWN > 0
3665 #endif /* ! IS_SAFE_CHOWN */
3668 chownsafe(fd, safedir)
3672 # if (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && \
3673 (defined(_PC_CHOWN_RESTRICTED) || defined(_GNU_TYPES_H))
3676 /* give the system administrator a chance to override */
3677 if (bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail))
3681 ** Some systems (e.g., SunOS) seem to have the call and the
3682 ** #define _PC_CHOWN_RESTRICTED, but don't actually implement
3683 ** the call. This heuristic checks for that.
3687 rval = fpathconf(fd, _PC_CHOWN_RESTRICTED);
3688 # if SAFENFSPATHCONF
3689 return errno == 0 && rval IS_SAFE_CHOWN;
3690 # else /* SAFENFSPATHCONF */
3691 return safedir && errno == 0 && rval IS_SAFE_CHOWN;
3692 # endif /* SAFENFSPATHCONF */
3693 # else /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3694 return bitnset(DBS_ASSUMESAFECHOWN, DontBlameSendmail);
3695 # endif /* (!defined(_POSIX_CHOWN_RESTRICTED) || _POSIX_CHOWN_RESTRICTED != -1) && ... */
3698 ** RESETLIMITS -- reset system controlled resource limits
3700 ** This is to avoid denial-of-service attacks
3710 # ifdef RLIMIT_NEEDS_SYS_TIME_H
3711 # include <sys/time.h>
3712 # endif /* RLIMIT_NEEDS_SYS_TIME_H */
3713 # include <sys/resource.h>
3714 #endif /* HASSETRLIMIT */
3722 lim.rlim_cur = lim.rlim_max = RLIM_INFINITY;
3723 (void) setrlimit(RLIMIT_CPU, &lim);
3724 (void) setrlimit(RLIMIT_FSIZE, &lim);
3725 # ifdef RLIMIT_NOFILE
3726 lim.rlim_cur = lim.rlim_max = FD_SETSIZE;
3727 (void) setrlimit(RLIMIT_NOFILE, &lim);
3728 # endif /* RLIMIT_NOFILE */
3729 #else /* HASSETRLIMIT */
3731 (void) ulimit(2, 0x3fffff);
3732 (void) ulimit(4, FD_SETSIZE);
3733 # endif /* HASULIMIT */
3734 #endif /* HASSETRLIMIT */
3738 ** SETVENDOR -- process vendor code from V configuration line
3741 ** vendor -- string representation of vendor.
3745 ** false -- if vendor code could not be processed.
3748 ** It is reasonable to set mode flags here to tweak
3749 ** processing in other parts of the code if necessary.
3750 ** For example, if you are a vendor that uses $%y to
3751 ** indicate YP lookups, you could enable that here.
3758 if (sm_strcasecmp(vendor, "Berkeley") == 0)
3760 VendorCode = VENDOR_BERKELEY;
3764 /* add vendor extensions here */
3766 #ifdef SUN_EXTENSIONS
3767 if (sm_strcasecmp(vendor, "Sun") == 0)
3769 VendorCode = VENDOR_SUN;
3772 #endif /* SUN_EXTENSIONS */
3774 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3775 if (sm_strcasecmp(vendor, VENDOR_NAME) == 0)
3777 VendorCode = VENDOR_CODE;
3780 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3785 ** GETVENDOR -- return vendor name based on vendor code
3788 ** vendorcode -- numeric representation of vendor.
3791 ** string containing vendor name.
3795 getvendor(vendorcode)
3798 #if defined(VENDOR_NAME) && defined(VENDOR_CODE)
3800 ** Can't have the same switch case twice so need to
3801 ** handle VENDOR_CODE outside of switch. It might
3802 ** match one of the existing VENDOR_* codes.
3805 if (vendorcode == VENDOR_CODE)
3807 #endif /* defined(VENDOR_NAME) && defined(VENDOR_CODE) */
3811 case VENDOR_BERKELEY:
3823 case VENDOR_SENDMAIL:
3831 ** VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults
3833 ** Vendor_pre_defaults is called before reading the configuration
3834 ** file; vendor_post_defaults is called immediately after.
3837 ** e -- the global environment to initialize.
3844 int DefShareUid; /* default share uid to run as -- unused??? */
3845 #endif /* SHARE_V1 */
3848 vendor_pre_defaults(e)
3852 /* OTHERUID is defined in shares.h, do not be alarmed */
3853 DefShareUid = OTHERUID;
3854 #endif /* SHARE_V1 */
3855 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3856 sun_pre_defaults(e);
3857 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3860 ** stupid domain/os can't even open
3861 ** /etc/mail/sendmail.cf without this
3864 setuserenv("ISP", NULL);
3865 setuserenv("SYSTYPE", NULL);
3871 vendor_post_defaults(e)
3877 /* Makes sure the SOCK environment variable remains */
3878 if (p = getextenv("SOCK"))
3879 setuserenv("SOCK", p);
3880 #endif /* __QNX__ */
3881 #if defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES)
3882 sun_post_defaults(e);
3883 #endif /* defined(SUN_EXTENSIONS) && defined(SUN_DEFAULT_VALUES) */
3886 ** VENDOR_DAEMON_SETUP -- special vendor setup needed for daemon mode
3890 vendor_daemon_setup(e)
3894 (void) setlogin(RunAsUserName);
3895 #endif /* HASSETLOGIN */
3897 if (getluid() != -1)
3899 usrerr("Daemon cannot have LUID");
3900 finis(false, true, EX_USAGE);
3902 #endif /* SECUREWARE */
3905 ** VENDOR_SET_UID -- do setup for setting a user id
3907 ** This is called when we are still root.
3910 ** uid -- the uid we are about to become.
3921 ** We need to setup the share groups (lnodes)
3922 ** and add auditing information (luid's)
3923 ** before we loose our ``root''ness.
3926 if (setupshares(uid, syserr) != 0)
3927 syserr("Unable to set up shares");
3928 #endif /* SHARE_V1 */
3930 (void) setup_secure(uid);
3931 #endif /* SECUREWARE */
3934 ** VALIDATE_CONNECTION -- check connection for rationality
3936 ** If the connection is rejected, this routine should log an
3937 ** appropriate message -- but should never issue any SMTP protocol.
3940 ** sap -- a pointer to a SOCKADDR naming the peer.
3941 ** hostname -- the name corresponding to sap.
3942 ** e -- the current envelope.
3945 ** error message from rejection.
3946 ** NULL if not rejected.
3952 /* tcpwrappers does no logging, but you still have to declare these -- ugh */
3953 int allow_severity = LOG_INFO;
3954 int deny_severity = LOG_NOTICE;
3955 #endif /* TCPWRAPPERS */
3958 validate_connection(sap, hostname, e)
3966 extern int hosts_ctl();
3967 #endif /* TCPWRAPPERS */
3970 sm_dprintf("validate_connection(%s, %s)\n",
3971 hostname, anynet_ntoa(sap));
3973 if (rscheck("check_relay", hostname, anynet_ntoa(sap),
3974 e, RSF_RMCOMM|RSF_COUNT, 3, NULL, NOQID) != EX_OK)
3976 static char reject[BUFSIZ*2];
3977 extern char MsgBuf[];
3980 sm_dprintf(" ... validate_connection: BAD (rscheck)\n");
3982 if (strlen(MsgBuf) >= 3)
3983 (void) sm_strlcpy(reject, MsgBuf, sizeof reject);
3985 (void) sm_strlcpy(reject, "Access denied", sizeof reject);
3991 if (hostname[0] == '[' && hostname[strlen(hostname) - 1] == ']')
3995 addr = anynet_ntoa(sap);
3998 /* TCP/Wrappers don't want the IPv6: protocol label */
3999 if (addr != NULL && sm_strncasecmp(addr, "IPv6:", 5) == 0)
4001 # endif /* NETINET6 */
4003 if (!hosts_ctl("sendmail", host, addr, STRING_UNKNOWN))
4006 sm_dprintf(" ... validate_connection: BAD (tcpwrappers)\n");
4008 sm_syslog(LOG_NOTICE, e->e_id,
4009 "tcpwrappers (%s, %s) rejection",
4011 return "Access denied";
4013 #endif /* TCPWRAPPERS */
4015 sm_dprintf(" ... validate_connection: OK\n");
4020 ** STRTOL -- convert string to long integer
4022 ** For systems that don't have it in the C library.
4024 ** This is taken verbatim from the 4.4-Lite C library.
4029 # if defined(LIBC_SCCS) && !defined(lint)
4030 static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
4031 # endif /* defined(LIBC_SCCS) && !defined(lint) */
4034 ** Convert a string to a long integer.
4036 ** Ignores `locale' stuff. Assumes that the upper and lower case
4037 ** alphabets and digits are each contiguous.
4041 strtol(nptr, endptr, base)
4046 register const char *s = nptr;
4047 register unsigned long acc;
4049 register unsigned long cutoff;
4050 register int neg = 0, any, cutlim;
4053 ** Skip white space and pick up leading +/- sign if any.
4054 ** If base is 0, allow 0x for hex and 0 for octal, else
4055 ** assume decimal; if base is already 16, allow 0x.
4059 } while (isspace(c));
4063 } else if (c == '+')
4065 if ((base == 0 || base == 16) &&
4066 c == '0' && (*s == 'x' || *s == 'X')) {
4072 base = c == '0' ? 8 : 10;
4075 ** Compute the cutoff value between legal numbers and illegal
4076 ** numbers. That is the largest legal value, divided by the
4077 ** base. An input number that is greater than this value, if
4078 ** followed by a legal input character, is too big. One that
4079 ** is equal to this value may be valid or not; the limit
4080 ** between valid and invalid numbers is then based on the last
4081 ** digit. For instance, if the range for longs is
4082 ** [-2147483648..2147483647] and the input base is 10,
4083 ** cutoff will be set to 214748364 and cutlim to either
4084 ** 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
4085 ** a value > 214748364, or equal but the next digit is > 7 (or 8),
4086 ** the number is too big, and we will return a range error.
4088 ** Set any if any `digits' consumed; make it negative to indicate
4091 cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
4092 cutlim = cutoff % (unsigned long) base;
4093 cutoff /= (unsigned long) base;
4094 for (acc = 0, any = 0;; c = *s++) {
4097 else if (isalpha(c))
4098 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4103 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
4112 acc = neg ? LONG_MIN : LONG_MAX;
4117 *endptr = (char *)(any ? s - 1 : nptr);
4121 #endif /* NEEDSTRTOL */
4123 ** STRSTR -- find first substring in string
4126 ** big -- the big (full) string.
4127 ** little -- the little (sub) string.
4130 ** A pointer to the first instance of little in big.
4131 ** big if little is the null string.
4132 ** NULL if little is not contained in big.
4142 register char *p = big;
4145 if (*little == '\0')
4149 while ((p = strchr(p, *little)) != NULL)
4151 if (strncmp(p, little, l) == 0)
4158 #endif /* NEEDSTRSTR */
4160 ** SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
4162 ** Some operating systems have wierd problems with the gethostbyXXX
4163 ** routines. For example, Solaris versions at least through 2.3
4164 ** don't properly deliver a canonical h_name field. This tries to
4165 ** work around these problems.
4167 ** Support IPv6 as well as IPv4.
4170 #if NETINET6 && NEEDSGETIPNODE
4173 # define AI_DEFAULT 0 /* dummy */
4174 # endif /* ! AI_DEFAULT */
4175 # ifndef AI_ADDRCONFIG
4176 # define AI_ADDRCONFIG 0 /* dummy */
4177 # endif /* ! AI_ADDRCONFIG */
4178 # ifndef AI_V4MAPPED
4179 # define AI_V4MAPPED 0 /* dummy */
4180 # endif /* ! AI_V4MAPPED */
4182 # define AI_ALL 0 /* dummy */
4183 # endif /* ! AI_ALL */
4185 static struct hostent *
4186 getipnodebyname(name, family, flags, err)
4195 if (family == AF_INET6)
4197 /* From RFC2133, section 6.1 */
4198 resv6 = bitset(RES_USE_INET6, _res.options);
4199 _res.options |= RES_USE_INET6;
4202 h = gethostbyname(name);
4204 _res.options &= ~RES_USE_INET6;
4209 static struct hostent *
4210 getipnodebyaddr(addr, len, family, err)
4219 h = gethostbyaddr(addr, len, family);
4229 ** Stub routine -- if they don't have getipnodeby*(),
4230 ** they probably don't have the free routine either.
4235 #endif /* NETINET6 && NEEDSGETIPNODE */
4238 sm_gethostbyname(name, family)
4243 struct hostent *h = NULL;
4244 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
4245 # if SOLARIS == 20300 || SOLARIS == 203
4246 static struct hostent hp;
4247 static char buf[1000];
4248 extern struct hostent *_switch_gethostbyname_r();
4251 sm_dprintf("_switch_gethostbyname_r(%s)... ", name);
4252 h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
4254 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4255 extern struct hostent *__switch_gethostbyname();
4258 sm_dprintf("__switch_gethostbyname(%s)... ", name);
4259 h = __switch_gethostbyname(name);
4261 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4262 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4265 int flags = AI_DEFAULT|AI_ALL;
4267 # endif /* NETINET6 */
4268 char *maptype[MAXMAPSTACK];
4269 short mapreturn[MAXMAPACTIONS];
4273 sm_dprintf("sm_gethostbyname(%s, %d)... ", name, family);
4276 # if ADDRCONFIG_IS_BROKEN
4277 flags &= ~AI_ADDRCONFIG;
4278 # endif /* ADDRCONFIG_IS_BROKEN */
4279 h = getipnodebyname(name, family, flags, &err);
4280 SM_SET_H_ERRNO(err);
4281 # else /* NETINET6 */
4282 h = gethostbyname(name);
4283 # endif /* NETINET6 */
4289 sm_dprintf("failure\n");
4291 nmaps = switch_map_find("hosts", maptype, mapreturn);
4292 while (--nmaps >= 0)
4294 if (strcmp(maptype[nmaps], "nis") == 0 ||
4295 strcmp(maptype[nmaps], "files") == 0)
4301 /* try short name */
4302 if (strlen(name) > sizeof hbuf - 1)
4307 (void) sm_strlcpy(hbuf, name, sizeof hbuf);
4308 (void) shorten_hostname(hbuf);
4310 /* if it hasn't been shortened, there's no point */
4311 if (strcmp(hbuf, name) != 0)
4314 sm_dprintf("sm_gethostbyname(%s, %d)... ",
4318 h = getipnodebyname(hbuf, family, flags, &err);
4319 SM_SET_H_ERRNO(err);
4321 # else /* NETINET6 */
4322 h = gethostbyname(hbuf);
4324 # endif /* NETINET6 */
4328 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
4332 sm_dprintf("failure\n");
4335 sm_dprintf("%s\n", h->h_name);
4339 struct in6_addr ia6;
4340 char buf6[INET6_ADDRSTRLEN];
4341 #else /* NETINET6 */
4343 #endif /* NETINET6 */
4346 if (h->h_aliases != NULL)
4347 for (i = 0; h->h_aliases[i] != NULL;
4349 sm_dprintf("\talias: %s\n",
4351 for (i = 0; h->h_addr_list[i] != NULL; i++)
4356 memmove(&ia6, h->h_addr_list[i],
4358 addr = anynet_ntop(&ia6,
4360 #else /* NETINET6 */
4361 memmove(&ia, h->h_addr_list[i],
4363 addr = (char *) inet_ntoa(ia);
4364 #endif /* NETINET6 */
4366 sm_dprintf("\taddr: %s\n", addr);
4376 sm_gethostbyaddr(addr, len, type)
4384 if (type == AF_INET6 &&
4385 IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *) addr))
4387 /* Avoid reverse lookup for IPv6 unspecified address */
4388 SM_SET_H_ERRNO(HOST_NOT_FOUND);
4391 #endif /* NETINET6 */
4393 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204)
4394 # if SOLARIS == 20300 || SOLARIS == 203
4396 static struct hostent he;
4397 static char buf[1000];
4398 extern struct hostent *_switch_gethostbyaddr_r();
4400 hp = _switch_gethostbyaddr_r(addr, len, type, &he,
4401 buf, sizeof(buf), &h_errno);
4403 # else /* SOLARIS == 20300 || SOLARIS == 203 */
4405 extern struct hostent *__switch_gethostbyaddr();
4407 hp = __switch_gethostbyaddr(addr, len, type);
4409 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
4410 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4415 hp = getipnodebyaddr(addr, len, type, &err);
4416 SM_SET_H_ERRNO(err);
4418 # else /* NETINET6 */
4419 hp = gethostbyaddr(addr, len, type);
4420 # endif /* NETINET6 */
4421 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) */
4425 ** SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid
4433 extern struct passwd *_getpwnam_shadow(const char *, const int);
4435 return _getpwnam_shadow(user, 0);
4437 return getpwnam(user);
4445 #if defined(_AIX4) && 0
4446 extern struct passwd *_getpwuid_shadow(const int, const int);
4448 return _getpwuid_shadow(uid,0);
4449 #else /* defined(_AIX4) && 0 */
4450 return getpwuid(uid);
4451 #endif /* defined(_AIX4) && 0 */
4454 ** SECUREWARE_SETUP_SECURE -- Convex SecureWare setup
4456 ** Set up the trusted computing environment for C2 level security
4457 ** under SecureWare.
4460 ** uid -- uid of the user to initialize in the TCB
4466 ** Initialized the user in the trusted computing base
4471 # include <sys/security.h>
4475 secureware_setup_secure(uid)
4480 if (getluid() != -1)
4483 if ((rc = set_secure_info(uid)) != SSI_GOOD_RETURN)
4487 case SSI_NO_PRPW_ENTRY:
4488 syserr("No protected passwd entry, uid = %d",
4493 syserr("Account has been disabled, uid = %d",
4498 syserr("Account has been retired, uid = %d",
4502 case SSI_BAD_SET_LUID:
4503 syserr("Could not set LUID, uid = %d", (int) uid);
4506 case SSI_BAD_SET_PRIVS:
4507 syserr("Could not set kernel privs, uid = %d",
4511 syserr("Unknown return code (%d) from set_secure_info(%d)",
4515 finis(false, true, EX_NOPERM);
4518 #endif /* SECUREWARE */
4520 ** ADD_HOSTNAMES -- Add a hostname to class 'w' based on IP address
4522 ** Add hostnames to class 'w' based on the IP address read from
4523 ** the network interface.
4526 ** sa -- a pointer to a SOCKADDR containing the address
4529 ** 0 if successful, -1 if host lookup fails.
4538 char hnb[MAXHOSTNAMELEN];
4540 /* lookup name with IP address */
4541 switch (sa->sa.sa_family)
4545 hp = sm_gethostbyaddr((char *) &sa->sin.sin_addr,
4546 sizeof(sa->sin.sin_addr),
4549 #endif /* NETINET */
4553 hp = sm_gethostbyaddr((char *) &sa->sin6.sin6_addr,
4554 sizeof(sa->sin6.sin6_addr),
4557 #endif /* NETINET6 */
4560 /* Give warning about unsupported family */
4562 sm_syslog(LOG_WARNING, NOQID,
4563 "Unsupported address family %d: %.100s",
4564 sa->sa.sa_family, anynet_ntoa(sa));
4570 int save_errno = errno;
4574 !(sa->sa.sa_family == AF_INET6 &&
4575 IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr)) &&
4576 #endif /* NETINET6 */
4578 sm_syslog(LOG_WARNING, NOQID,
4579 "gethostbyaddr(%.100s) failed: %d",
4583 #else /* NAMED_BIND */
4585 #endif /* NAMED_BIND */
4591 /* save its cname */
4592 if (!wordinclass((char *) hp->h_name, 'w'))
4594 setclass('w', (char *) hp->h_name);
4596 sm_dprintf("\ta.k.a.: %s\n", hp->h_name);
4598 if (sm_snprintf(hnb, sizeof hnb, "[%s]", hp->h_name) < sizeof hnb
4599 && !wordinclass((char *) hnb, 'w'))
4605 sm_dprintf("\ta.k.a.: %s (already in $=w)\n", hp->h_name);
4608 /* save all it aliases name */
4609 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
4611 if (!wordinclass(*ha, 'w'))
4615 sm_dprintf("\ta.k.a.: %s\n", *ha);
4616 if (sm_snprintf(hnb, sizeof hnb,
4617 "[%s]", *ha) < sizeof hnb &&
4618 !wordinclass((char *) hnb, 'w'))
4624 sm_dprintf("\ta.k.a.: %s (already in $=w)\n",
4630 #endif /* NETINET6 */
4634 ** LOAD_IF_NAMES -- load interface-specific names into $=w
4643 ** Loads $=w with the names of all the interfaces.
4647 # define SIOCGIFCONF_IS_BROKEN 1 /* XXX */
4648 #endif /* !NETINET */
4650 #if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
4654 # include <sys/time.h>
4655 # endif /* ! SUNOS403 */
4656 # if (_AIX4 >= 40300) && !defined(_NET_IF_H)
4658 # endif /* (_AIX4 >= 40300) && !defined(_NET_IF_H) */
4659 # include <net/if.h>
4660 #endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
4665 # if NETINET6 && defined(SIOCGLIFCONF)
4669 ** Unfortunately, HP has changed all of the structures,
4670 ** making life difficult for implementors.
4673 # define lifconf if_laddrconf
4674 # define lifc_len iflc_len
4675 # define lifc_buf iflc_buf
4676 # define lifreq if_laddrreq
4677 # define lifr_addr iflr_addr
4678 # define lifr_name iflr_name
4679 # define lifr_flags iflr_flags
4680 # define ss_family sa_family
4682 # endif /* __hpux */
4689 struct lifconf lifc;
4692 # endif /* SIOCGLIFNUM */
4694 s = socket(InetMode, SOCK_DGRAM, 0);
4698 /* get the list of known IP address from the kernel */
4700 i = ioctl(s, SIOCGIFNUM, (char *) &numifs);
4701 # endif /* __hpux */
4703 lifn.lifn_family = AF_UNSPEC;
4704 lifn.lifn_flags = 0;
4705 i = ioctl(s, SIOCGLIFNUM, (char *)&lifn);
4706 numifs = lifn.lifn_count;
4707 # endif /* SIOCGLIFNUM */
4709 # if defined(__hpux) || defined(SIOCGLIFNUM)
4712 /* can't get number of interfaces -- fall back */
4714 sm_dprintf("SIOCGLIFNUM failed: %s\n",
4715 sm_errstring(errno));
4718 else if (tTd(0, 42))
4719 sm_dprintf("system has %d interfaces\n", numifs);
4721 # endif /* defined(__hpux) || defined(SIOCGLIFNUM) */
4722 numifs = MAXINTERFACES;
4730 len = lifc.lifc_len = numifs * sizeof (struct lifreq);
4731 buf = lifc.lifc_buf = xalloc(lifc.lifc_len);
4733 lifc.lifc_family = AF_UNSPEC;
4734 lifc.lifc_flags = 0;
4735 # endif /* ! __hpux */
4736 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0)
4739 sm_dprintf("SIOCGLIFCONF failed: %s\n",
4740 sm_errstring(errno));
4746 /* scan the list of IP address */
4748 sm_dprintf("scanning for interface specific names, lifc_len=%ld\n",
4751 for (i = 0; i < len && i >= 0; )
4754 struct lifreq *ifr = (struct lifreq *)&buf[i];
4755 SOCKADDR *sa = (SOCKADDR *) &ifr->lifr_addr;
4756 int af = ifr->lifr_addr.ss_family;
4759 struct in6_addr ia6;
4761 # ifdef SIOCGLIFFLAGS
4763 # endif /* SIOCGLIFFLAGS */
4765 char buf6[INET6_ADDRSTRLEN];
4768 ** We must close and recreate the socket each time
4769 ** since we don't know what type of socket it is now
4770 ** (each status function may change it).
4775 s = socket(af, SOCK_DGRAM, 0);
4778 sm_free(buf); /* XXX */
4783 ** If we don't have a complete ifr structure,
4784 ** don't try to use it.
4787 if ((len - i) < sizeof *ifr)
4790 # ifdef BSD4_4_SOCKADDR
4791 if (sa->sa.sa_len > sizeof ifr->lifr_addr)
4792 i += sizeof ifr->lifr_name + sa->sa.sa_len;
4794 # endif /* BSD4_4_SOCKADDR */
4798 sm_dprintf("%s\n", anynet_ntoa(sa));
4800 if (af != AF_INET && af != AF_INET6)
4803 # ifdef SIOCGLIFFLAGS
4804 memset(&ifrf, '\0', sizeof(struct lifreq));
4805 (void) sm_strlcpy(ifrf.lifr_name, ifr->lifr_name,
4806 sizeof(ifrf.lifr_name));
4807 if (ioctl(s, SIOCGLIFFLAGS, (char *) &ifrf) < 0)
4810 sm_dprintf("SIOCGLIFFLAGS failed: %s\n",
4811 sm_errstring(errno));
4815 name = ifr->lifr_name;
4816 flags = ifrf.lifr_flags;
4819 sm_dprintf("\tflags: %lx\n", (unsigned long) flags);
4821 if (!bitset(IFF_UP, flags))
4823 # endif /* SIOCGLIFFLAGS */
4827 /* extract IP address from the list*/
4832 /* convert into proper scoped address */
4833 if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
4834 IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
4835 sa->sin6.sin6_scope_id == 0)
4837 struct in6_addr *ia6p;
4839 ia6p = &sa->sin6.sin6_addr;
4840 sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
4841 ((unsigned int)ia6p->s6_addr[2] << 8));
4842 ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
4844 # endif /* __KAME__ */
4845 ia6 = sa->sin6.sin6_addr;
4846 if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
4848 addr = anynet_ntop(&ia6, buf6, sizeof buf6);
4849 message("WARNING: interface %s is UP with %s address",
4850 name, addr == NULL ? "(NULL)" : addr);
4854 /* save IP address in text from */
4855 addr = anynet_ntop(&ia6, buf6, sizeof buf6);
4857 (void) sm_snprintf(ip_addr, sizeof ip_addr,
4859 (int) sizeof ip_addr - 3,
4864 ia = sa->sin.sin_addr;
4865 if (ia.s_addr == INADDR_ANY ||
4866 ia.s_addr == INADDR_NONE)
4868 message("WARNING: interface %s is UP with %s address",
4869 name, inet_ntoa(ia));
4873 /* save IP address in text from */
4874 (void) sm_snprintf(ip_addr, sizeof ip_addr, "[%.*s]",
4875 (int) sizeof ip_addr - 3, inet_ntoa(ia));
4879 if (*ip_addr == '\0')
4882 if (!wordinclass(ip_addr, 'w'))
4884 setclass('w', ip_addr);
4886 sm_dprintf("\ta.k.a.: %s\n", ip_addr);
4889 # ifdef SIOCGLIFFLAGS
4890 /* skip "loopback" interface "lo" */
4891 if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
4892 bitset(IFF_LOOPBACK, flags))
4894 # endif /* SIOCGLIFFLAGS */
4895 (void) add_hostnames(sa);
4897 sm_free(buf); /* XXX */
4899 # else /* NETINET6 && defined(SIOCGLIFCONF) */
4900 # if defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN
4906 s = socket(AF_INET, SOCK_DGRAM, 0);
4910 /* get the list of known IP address from the kernel */
4911 # if defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN
4912 if (ioctl(s, SIOCGIFNUM, (char *) &numifs) < 0)
4914 /* can't get number of interfaces -- fall back */
4916 sm_dprintf("SIOCGIFNUM failed: %s\n",
4917 sm_errstring(errno));
4920 else if (tTd(0, 42))
4921 sm_dprintf("system has %d interfaces\n", numifs);
4923 # endif /* defined(SIOCGIFNUM) && !SIOCGIFNUM_IS_BROKEN */
4924 numifs = MAXINTERFACES;
4931 ifc.ifc_len = numifs * sizeof (struct ifreq);
4932 ifc.ifc_buf = xalloc(ifc.ifc_len);
4933 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
4936 sm_dprintf("SIOCGIFCONF failed: %s\n",
4937 sm_errstring(errno));
4942 /* scan the list of IP address */
4944 sm_dprintf("scanning for interface specific names, ifc_len=%d\n",
4947 for (i = 0; i < ifc.ifc_len && i >= 0; )
4950 struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i];
4951 SOCKADDR *sa = (SOCKADDR *) &ifr->ifr_addr;
4954 struct in6_addr ia6;
4955 # endif /* NETINET6 */
4957 # ifdef SIOCGIFFLAGS
4959 # endif /* SIOCGIFFLAGS */
4962 char buf6[INET6_ADDRSTRLEN];
4963 # endif /* NETINET6 */
4966 ** If we don't have a complete ifr structure,
4967 ** don't try to use it.
4970 if ((ifc.ifc_len - i) < sizeof *ifr)
4973 # ifdef BSD4_4_SOCKADDR
4974 if (sa->sa.sa_len > sizeof ifr->ifr_addr)
4975 i += sizeof ifr->ifr_name + sa->sa.sa_len;
4977 # endif /* BSD4_4_SOCKADDR */
4981 sm_dprintf("%s\n", anynet_ntoa(sa));
4983 af = ifr->ifr_addr.sa_family;
4987 # endif /* NETINET6 */
4991 # ifdef SIOCGIFFLAGS
4992 memset(&ifrf, '\0', sizeof(struct ifreq));
4993 (void) sm_strlcpy(ifrf.ifr_name, ifr->ifr_name,
4994 sizeof(ifrf.ifr_name));
4995 (void) ioctl(s, SIOCGIFFLAGS, (char *) &ifrf);
4997 sm_dprintf("\tflags: %lx\n",
4998 (unsigned long) ifrf.ifr_flags);
4999 # define IFRFREF ifrf
5000 # else /* SIOCGIFFLAGS */
5001 # define IFRFREF (*ifr)
5002 # endif /* SIOCGIFFLAGS */
5004 if (!bitset(IFF_UP, IFRFREF.ifr_flags))
5009 /* extract IP address from the list*/
5013 ia = sa->sin.sin_addr;
5014 if (ia.s_addr == INADDR_ANY ||
5015 ia.s_addr == INADDR_NONE)
5017 message("WARNING: interface %s is UP with %s address",
5018 ifr->ifr_name, inet_ntoa(ia));
5022 /* save IP address in text from */
5023 (void) sm_snprintf(ip_addr, sizeof ip_addr, "[%.*s]",
5024 (int) sizeof ip_addr - 3,
5031 /* convert into proper scoped address */
5032 if ((IN6_IS_ADDR_LINKLOCAL(&sa->sin6.sin6_addr) ||
5033 IN6_IS_ADDR_SITELOCAL(&sa->sin6.sin6_addr)) &&
5034 sa->sin6.sin6_scope_id == 0)
5036 struct in6_addr *ia6p;
5038 ia6p = &sa->sin6.sin6_addr;
5039 sa->sin6.sin6_scope_id = ntohs(ia6p->s6_addr[3] |
5040 ((unsigned int)ia6p->s6_addr[2] << 8));
5041 ia6p->s6_addr[2] = ia6p->s6_addr[3] = 0;
5043 # endif /* __KAME__ */
5044 ia6 = sa->sin6.sin6_addr;
5045 if (IN6_IS_ADDR_UNSPECIFIED(&ia6))
5047 addr = anynet_ntop(&ia6, buf6, sizeof buf6);
5048 message("WARNING: interface %s is UP with %s address",
5050 addr == NULL ? "(NULL)" : addr);
5054 /* save IP address in text from */
5055 addr = anynet_ntop(&ia6, buf6, sizeof buf6);
5057 (void) sm_snprintf(ip_addr, sizeof ip_addr,
5059 (int) sizeof ip_addr - 3,
5063 # endif /* NETINET6 */
5066 if (ip_addr[0] == '\0')
5069 if (!wordinclass(ip_addr, 'w'))
5071 setclass('w', ip_addr);
5073 sm_dprintf("\ta.k.a.: %s\n", ip_addr);
5076 /* skip "loopback" interface "lo" */
5077 if (DontProbeInterfaces == DPI_SKIPLOOPBACK &&
5078 bitset(IFF_LOOPBACK, IFRFREF.ifr_flags))
5081 (void) add_hostnames(sa);
5083 sm_free(ifc.ifc_buf); /* XXX */
5086 # endif /* defined(SIOCGIFCONF) && !SIOCGIFCONF_IS_BROKEN */
5087 # endif /* NETINET6 && defined(SIOCGLIFCONF) */
5090 ** ISLOOPBACK -- is socket address in the loopback net?
5093 ** sa -- socket address.
5096 ** true -- is socket address in the loopback net?
5097 ** false -- otherwise
5106 if (IN6_IS_ADDR_LOOPBACK(&sa.sin6.sin6_addr))
5108 #else /* NETINET6 */
5109 /* XXX how to correctly extract IN_LOOPBACKNET part? */
5110 if (((ntohl(sa.sin.sin_addr.s_addr) & IN_CLASSA_NET)
5111 >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
5113 #endif /* NETINET6 */
5117 ** GET_NUM_PROCS_ONLINE -- return the number of processors currently online
5123 ** The number of processors online.
5127 get_num_procs_online()
5132 # if defined(CTL_HW) && defined(HW_NCPU)
5138 sz = (size_t) sizeof nproc;
5139 (void) sysctl(mib, 2, &nproc, &sz, NULL, 0);
5140 # endif /* defined(CTL_HW) && defined(HW_NCPU) */
5141 #else /* USESYSCTL */
5142 # ifdef _SC_NPROCESSORS_ONLN
5143 nproc = (int) sysconf(_SC_NPROCESSORS_ONLN);
5144 # else /* _SC_NPROCESSORS_ONLN */
5146 # include <sys/pstat.h>
5147 struct pst_dynamic psd;
5149 if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) != -1)
5150 nproc = psd.psd_proc_cnt;
5151 # endif /* __hpux */
5152 # endif /* _SC_NPROCESSORS_ONLN */
5153 #endif /* USESYSCTL */
5160 ** SEED_RANDOM -- seed the random number generator
5174 #else /* HASSRANDOMDEV */
5178 seed = (long) CurrentPid;
5179 if (gettimeofday(&t, NULL) >= 0)
5180 seed += t.tv_sec + t.tv_usec;
5183 (void) srandom(seed);
5184 # else /* HASRANDOM */
5185 (void) srand((unsigned int) seed);
5186 # endif /* HASRANDOM */
5187 #endif /* HASSRANDOMDEV */
5190 ** SM_SYSLOG -- syslog wrapper to keep messages under SYSLOG_BUFSIZE
5193 ** level -- syslog level
5194 ** id -- envelope ID or NULL (NOQUEUE)
5195 ** fmt -- format string
5196 ** arg... -- arguments as implied by fmt.
5205 sm_syslog(int level, const char *id, const char *fmt, ...)
5206 #else /* __STDC__ */
5207 sm_syslog(level, id, fmt, va_alist)
5212 #endif /* __STDC__ */
5214 static char *buf = NULL;
5215 static size_t bufsize;
5222 extern int SyslogPrefixLen;
5229 idlen = strlen(id) + SyslogPrefixLen;
5231 else if (strcmp(id, NOQID) == 0)
5234 idlen = SyslogPrefixLen;
5237 idlen = strlen(id) + SyslogPrefixLen;
5242 bufsize = sizeof buf0;
5249 /* print log message into buf */
5250 SM_VA_START(ap, fmt);
5251 n = sm_vsnprintf(buf, bufsize, fmt, ap);
5257 /* String too small, redo with correct size */
5264 buf = sm_malloc_x(bufsize);
5267 /* clean up buf after it has been expanded with args */
5268 newstring = str2prt(buf);
5269 if ((strlen(newstring) + idlen + 1) < SYSLOG_BUFSIZE)
5273 syslog(level, "%s", newstring);
5275 syslog(level, "%s: %s", id, newstring);
5277 /*XXX should do something more sensible */
5279 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s\n",
5282 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5283 "%s: %s\n", id, newstring);
5292 ** additional length for splitting: " ..." + 3, where 3 is magic to
5293 ** have some data for the next entry.
5299 idlen += 5; /* strlen("[999]"), see below */
5300 while (*begin != '\0' &&
5301 (strlen(begin) + idlen) > SYSLOG_BUFSIZE)
5307 /* Too many messages */
5310 end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5313 /* Break on comma or space */
5314 if (*end == ',' || *end == ' ')
5316 end++; /* Include separator */
5321 /* No separator, break midstring... */
5323 end = begin + SYSLOG_BUFSIZE - idlen - SL_SPLIT;
5327 syslog(level, "%s[%d]: %s ...", id, seq++, begin);
5329 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5330 "%s[%d]: %s ...\n", id, seq++, begin);
5337 syslog(level, "%s[%d]: log terminated, too many parts",
5340 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5341 "%s[%d]: log terminated, too many parts\n", id, seq);
5343 else if (*begin != '\0')
5345 syslog(level, "%s[%d]: %s", id, seq, begin);
5347 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
5348 "%s[%d]: %s\n", id, seq, begin);
5355 ** HARD_SYSLOG -- call syslog repeatedly until it works
5357 ** Needed on HP-UX, which apparently doesn't guarantee that
5358 ** syslog succeeds during interrupt handlers.
5361 #if defined(__hpux) && !defined(HPUX11)
5363 # define MAXSYSLOGTRIES 100
5366 # define XCNST const
5367 # define CAST (const char *)
5375 hard_syslog(int pri, XCNST char *msg, ...)
5376 # else /* __STDC__ */
5377 hard_syslog(pri, msg, va_alist)
5381 # endif /* __STDC__ */
5384 char buf[SYSLOG_BUFSIZE];
5387 SM_VA_START(ap, msg);
5388 (void) sm_vsnprintf(buf, sizeof buf, msg, ap);
5391 for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, CAST "%s", buf) < 0; )
5396 #endif /* defined(__hpux) && !defined(HPUX11) */
5397 #if NEEDLOCAL_HOSTNAME_LENGTH
5399 ** LOCAL_HOSTNAME_LENGTH
5401 ** This is required to get sendmail to compile against BIND 4.9.x
5404 ** Unfortunately, a Compaq Y2K patch kit provides it without
5405 ** bumping __RES in /usr/include/resolv.h so we can't automatically
5406 ** figure out whether it is needed.
5410 local_hostname_length(hostname)
5413 size_t len_host, len_domain;
5415 if (!*_res.defdname)
5417 len_host = strlen(hostname);
5418 len_domain = strlen(_res.defdname);
5419 if (len_host > len_domain &&
5420 (sm_strcasecmp(hostname + len_host - len_domain,
5421 _res.defdname) == 0) &&
5422 hostname[len_host - len_domain - 1] == '.')
5423 return len_host - len_domain - 1;
5427 #endif /* NEEDLOCAL_HOSTNAME_LENGTH */
5431 ** LINK -- clone a file
5433 ** Some OS's lacks link() and hard links. Since sendmail is using
5434 ** link() as an efficient way to clone files, this implementation
5435 ** will simply do a file copy.
5437 ** NOTE: This link() replacement is not a generic replacement as it
5438 ** does not handle all of the semantics of the real link(2).
5441 ** source -- pathname of existing file.
5442 ** target -- pathname of link (clone) to be created.
5446 ** -1 -- failure, see errno for details.
5450 link(source, target)
5456 int src = -1, dst = -1;
5462 sff = SFF_REGONLY|SFF_OPENASROOT;
5463 if (DontLockReadFiles)
5466 /* Open the original file */
5467 src = safeopen((char *)source, O_RDONLY, 0, sff);
5471 /* Obtain the size and the mode */
5472 if (fstat(src, &st) < 0)
5475 /* Create the duplicate copy */
5478 dst = safeopen((char *)target, O_CREAT|O_EXCL|O_WRONLY,
5483 /* Copy all of the bytes one buffer at a time */
5484 while ((readlen = read(src, &buf, sizeof(buf))) > 0)
5486 ssize_t left = readlen;
5490 (writelen = write(dst, p, (size_t) left)) >= 0)
5499 /* Any trouble reading? */
5500 if (readlen < 0 || writelen < 0)
5503 /* Close the input file */
5511 /* Close the output file */
5514 /* don't set dst = -1 here so we unlink the file */
5527 (void) unlink(target);
5533 #endif /* NEEDLINK */
5536 ** Compile-Time options
5539 char *CompileOptions[] =
5544 # endif /* DNSMAP */
5545 #endif /* NAMED_BIND */
5554 #endif /* HES_GETMAILHOST */
5557 #endif /* LDAPMAP */
5563 #endif /* MAP_NSD */
5566 #endif /* MAP_REGEX */
5569 #endif /* MATCHGECOS */
5575 #endif /* MIME7TO8 */
5578 #endif /* MIME8TO7 */
5581 #endif /* NAMED_BIND */
5587 #endif /* NETINET */
5590 #endif /* NETINET6 */
5593 #endif /* NETINFO */
5602 #endif /* NETUNIX */
5614 #endif /* NISPLUS */
5621 #ifdef PICKY_HELO_CHECK
5623 #endif /* PICKY_HELO_CHECK */
5626 #endif /* PIPELINING */
5630 # else /* SASL >= 20000 */
5632 # endif /* SASL >= 20000 */
5639 #endif /* SMTPDEBUG */
5642 #endif /* STARTTLS */
5643 #if SUID_ROOT_FILES_OK
5644 "SUID_ROOT_FILES_OK",
5645 #endif /* SUID_ROOT_FILES_OK */
5648 #endif /* TCPWRAPPERS */
5651 #endif /* TLS_NO_RSA */
5652 #if TLS_VRFY_PER_CTX
5654 #endif /* TLS_VRFY_PER_CTX */
5660 #endif /* USE_LDAP_INIT */
5672 ** OS compile options.
5675 char *OsCompileOptions[] =
5677 #if ADDRCONFIG_IS_BROKEN
5678 "ADDRCONFIG_IS_BROKEN",
5679 #endif /* ADDRCONFIG_IS_BROKEN */
5682 #endif /* ALLOW_255 */
5683 #ifdef AUTO_NETINFO_HOSTS
5684 "AUTO_NETINFO_HOSTS",
5685 #endif /* AUTO_NETINFO_HOSTS */
5686 #ifdef AUTO_NIS_ALIASES
5688 #endif /* AUTO_NIS_ALIASES */
5689 #if BROKEN_RES_SEARCH
5690 "BROKEN_RES_SEARCH",
5691 #endif /* BROKEN_RES_SEARCH */
5692 #ifdef BSD4_4_SOCKADDR
5694 #endif /* BSD4_4_SOCKADDR */
5697 #endif /* BOGUS_O_EXCL */
5698 #if DEC_OSF_BROKEN_GETPWENT
5699 "DEC_OSF_BROKEN_GETPWENT",
5700 #endif /* DEC_OSF_BROKEN_GETPWENT */
5701 #if FAST_PID_RECYCLE
5703 #endif /* FAST_PID_RECYCLE */
5706 #endif /* HASFCHOWN */
5709 #endif /* HASFCHMOD */
5712 #endif /* HASFLOCK */
5713 #if HASGETDTABLESIZE
5715 #endif /* HASGETDTABLESIZE */
5718 #endif /* HASGETUSERSHELL */
5721 #endif /* HASINITGROUPS */
5724 #endif /* HASLSTAT */
5727 #endif /* HASNICE */
5730 #endif /* HASRANDOM */
5733 #endif /* HASRRESVPORT */
5736 #endif /* HASSETEGID */
5739 #endif /* HASSETLOGIN */
5742 #endif /* HASSETREGID */
5745 #endif /* HASSETRESGID */
5748 #endif /* HASSETREUID */
5751 #endif /* HASSETRLIMIT */
5754 #endif /* HASSETSID */
5755 #if HASSETUSERCONTEXT
5756 "HASSETUSERCONTEXT",
5757 #endif /* HASSETUSERCONTEXT */
5760 #endif /* HASSETVBUF */
5763 #endif /* HAS_ST_GEN */
5766 #endif /* HASSRANDOMDEV */
5769 #endif /* HASURANDOMDEV */
5772 #endif /* HASSTRERROR */
5775 #endif /* HASULIMIT */
5778 #endif /* HASUNAME */
5781 #endif /* HASUNSETENV */
5784 #endif /* HASWAITPID */
5787 #endif /* IDENTPROTO */
5790 #endif /* IP_SRCROUTE */
5791 #if O_EXLOCK && HASFLOCK && !BOGUS_O_EXCL
5793 #endif /* O_EXLOCK && HASFLOCK && !BOGUS_O_EXCL */
5796 #endif /* NEEDFSYNC */
5799 #endif /* NEEDLINK */
5800 #if NEEDLOCAL_HOSTNAME_LENGTH
5801 "NEEDLOCAL_HOSTNAME_LENGTH",
5802 #endif /* NEEDLOCAL_HOSTNAME_LENGTH */
5805 #endif /* NEEDSGETIPNODE */
5808 #endif /* NEEDSTRSTR */
5811 #endif /* NEEDSTRTOL */
5812 #ifdef NO_GETSERVBYNAME
5814 #endif /* NO_GETSERVBYNAME */
5817 #endif /* NOFTRUNCATE */
5818 #if REQUIRES_DIR_FSYNC
5819 "REQUIRES_DIR_FSYNC",
5820 #endif /* REQUIRES_DIR_FSYNC */
5821 #if RLIMIT_NEEDS_SYS_TIME_H
5822 "RLIMIT_NEEDS_SYS_TIME_H",
5823 #endif /* RLIMIT_NEEDS_SYS_TIME_H */
5826 #endif /* SAFENFSPATHCONF */
5829 #endif /* SECUREWARE */
5832 #endif /* SHARE_V1 */
5833 #if SIOCGIFCONF_IS_BROKEN
5834 "SIOCGIFCONF_IS_BROKEN",
5835 #endif /* SIOCGIFCONF_IS_BROKEN */
5836 #if SIOCGIFNUM_IS_BROKEN
5837 "SIOCGIFNUM_IS_BROKEN",
5838 #endif /* SIOCGIFNUM_IS_BROKEN */
5839 #if SNPRINTF_IS_BROKEN
5840 "SNPRINTF_IS_BROKEN",
5841 #endif /* SNPRINTF_IS_BROKEN */
5842 #if SO_REUSEADDR_IS_BROKEN
5843 "SO_REUSEADDR_IS_BROKEN",
5844 #endif /* SO_REUSEADDR_IS_BROKEN */
5847 #endif /* SYS5SETPGRP */
5850 #endif /* SYSTEM5 */
5853 #endif /* USE_DOUBLE_FORK */
5856 #endif /* USE_ENVIRON */
5857 #if USE_SA_SIGACTION
5859 #endif /* USE_SA_SIGACTION */
5862 #endif /* USE_SIGLONGJMP */
5865 #endif /* USEGETCONFATTR */
5868 #endif /* USESETEUID */
5871 #endif /* USESYSCTL */
5872 #if USING_NETSCAPE_LDAP
5873 "USING_NETSCAPE_LDAP",
5874 #endif /* USING_NETSCAPE_LDAP */
5877 #endif /* WAITUNION */
5882 ** FFR compile options.
5885 char *FFRCompileOptions[] =
5887 #if _FFR_ADAPTIVE_EOL
5888 /* tries to be smart about \r\n versus \n from broken clients */
5889 /* known to be broken, do not use */
5890 "_FFR_ADAPTIVE_EOL",
5891 #endif /* _FFR_ADAPTIVE_EOL */
5892 #if _FFR_ALLOW_SASLINFO
5893 /* DefaultAuthInfo can be specified by user. */
5894 /* DefaultAuthInfo doesn't really work in 8.12 anymore. */
5895 "_FFR_ALLOW_SASLINFO",
5896 #endif /* _FFR_ALLOW_SASLINFO */
5897 #if _FFR_ALLOW_S0_ERROR_4XX
5898 /* Allow for tempfail from S0 (ruleset 0). */
5899 "_FFR_ALLOW_S0_ERROR_4XX",
5900 #endif /* _FFR_ALLOW_S0_ERROR_4XX */
5901 #if _FFR_BESTMX_BETTER_TRUNCATION
5902 /* Better truncation of list of MX records for dns map. */
5903 "_FFR_BESTMX_BETTER_TRUNCATION",
5904 #endif /* _FFR_BESTMX_BETTER_TRUNCATION */
5905 #if _FFR_BLOCK_PROXIES
5907 ** Try to deal with open HTTP proxies that are used to send spam
5908 ** by recognizing some commands from them.
5911 "_FFR_BLOCK_PROXIES",
5912 #endif /* _FFR_BLOCK_PROXIES */
5914 /* Cache connections to LCP based mailers */
5915 /* Christophe Wolfhugel of France Telecom Oleane */
5917 #endif /* _FFR_CACHE_LPC */
5918 #if _FFR_CATCH_BROKEN_MTAS
5919 /* Deal with MTAs that send a reply during the DATA phase. */
5920 "_FFR_CATCH_BROKEN_MTAS",
5921 #endif /* _FFR_CATCH_BROKEN_MTAS */
5922 #if _FFR_CATCH_LONG_STRINGS
5923 /* Report long address strings instead of silently ignoring them. */
5924 "_FFR_CATCH_LONG_STRINGS",
5925 #endif /* _FFR_CATCH_LONG_STRINGS */
5927 /* Enable check_eom ruleset */
5929 #endif /* _FFR_CHECK_EOM */
5931 /* Stricter checks about queue directory permissions. */
5933 #endif /* _FFR_CHK_QUEUE */
5934 #if _FFR_CLIENT_SIZE
5935 /* Don't try to send mail if its size exceeds SIZE= of server. */
5937 #endif /* _FFR_CLIENT_SIZE */
5938 #if _FFR_CONTROL_MSTAT
5939 /* Extended daemon status. */
5940 "_FFR_CONTROL_MSTAT",
5941 #endif /* _FFR_CONTROL_MSTAT */
5942 #if _FFR_DAEMON_NETUNIX
5943 /* Allow local (not just TCP) socket connection to server. */
5944 "_FFR_DAEMON_NETUNIX",
5945 #endif /* _FFR_DAEMON_NETUNIX */
5946 #if _FFR_DEAL_WITH_ERROR_SSL
5947 /* Deal with SSL errors by recognizing them as EOF. */
5948 "_FFR_DEAL_WITH_ERROR_SSL",
5949 #endif /* _FFR_DEAL_WITH_ERROR_SSL */
5950 #if _FFR_DEPRECATE_MAILER_FLAG_I
5951 /* What it says :-) */
5952 "_FFR_DEPRECATE_MAILER_FLAG_I",
5953 #endif /* _FFR_DEPRECATE_MAILER_FLAG_I */
5954 #if _FFR_DIGUNIX_SAFECHOWN
5955 /* Properly set SAFECHOWN (include/sm/conf.h) for Digital UNIX */
5956 /* Problem noted by Anne Bennett of Concordia University */
5957 "_FFR_DIGUNIX_SAFECHOWN",
5958 #endif /* _FFR_DIGUNIX_SAFECHOWN */
5959 #if _FFR_DNSMAP_ALIASABLE
5960 /* Allow dns map type to be used for aliases. */
5961 /* Don Lewis of TDK */
5962 "_FFR_DNSMAP_ALIASABLE",
5963 #endif /* _FFR_DNSMAP_ALIASABLE */
5964 #if _FFR_DNSMAP_BASE
5965 /* Specify a "base" domain for DNS lookups. */
5967 #endif /* _FFR_DNSMAP_BASE */
5968 #if _FFR_DNSMAP_MULTI
5969 /* Allow multiple return values for DNS map. */
5970 "_FFR_DNSMAP_MULTI",
5971 # if _FFR_DNSMAP_MULTILIMIT
5972 /* Limit number of return values for DNS map. */
5973 "_FFR_DNSMAP_MULTILIMIT",
5974 # endif /* _FFR_DNSMAP_MULTILIMIT */
5975 #endif /* _FFR_DNSMAP_MULTI */
5976 #if _FFR_DONTLOCKFILESFORREAD_OPTION
5977 /* Enable DontLockFilesForRead option. */
5978 "_FFR_DONTLOCKFILESFORREAD_OPTION",
5979 #endif /* _FFR_DONTLOCKFILESFORREAD_OPTION */
5980 #if _FFR_DONT_STOP_LOOKING
5981 /* Continue with DNS lookups on ECONNREFUSED and TRY_AGAIN. */
5982 /* Noted by Neil Rickert of Northern Illinois University */
5983 "_FFR_DONT_STOP_LOOKING",
5984 #endif /* _FFR_DONT_STOP_LOOKING */
5985 #if _FFR_DOTTED_USERNAMES
5986 /* Allow usernames with '.' */
5987 "_FFR_DOTTED_USERNAMES",
5988 #endif /* _FFR_DOTTED_USERNAMES */
5989 #if _FFR_DROP_TRUSTUSER_WARNING
5991 ** Don't issue this warning:
5992 ** "readcf: option TrustedUser may cause problems on systems
5993 ** which do not support fchown() if UseMSP is not set.
5996 "_FFR_DROP_TRUSTUSER_WARNING",
5997 #endif /* _FFR_DROP_TRUSTUSER_WARNING */
5998 #if _FFR_EXTRA_MAP_CHECK
5999 /* perform extra checks on $( $) in R lines */
6000 "_FFR_EXTRA_MAP_CHECK",
6001 #endif /* _FFR_EXTRA_MAP_CHECK */
6004 ** If using -t, force not sending to argv recipients, even
6005 ** if they are mentioned in the headers.
6009 #endif /* _FFR_FIX_DASHT */
6010 #if _FFR_FORWARD_SYSERR
6011 /* Cause a "syserr" if forward file isn't "safe". */
6012 "_FFR_FORWARD_SYSERR",
6013 #endif /* _FFR_FORWARD_SYSERR */
6015 /* Generate a ORCPT DSN arg if not already provided */
6017 #endif /* _FFR_GEN_ORCPT */
6018 #if _FFR_GROUPREADABLEAUTHINFOFILE
6019 /* Allow group readable DefaultAuthInfo file. */
6020 "_FFR_GROUPREADABLEAUTHINFOFILE",
6021 #endif /* _FFR_GROUPREADABLEAUTHINFOFILE */
6022 #if _FFR_HANDLE_ISO8859_GECOS
6024 ** Allow ISO 8859 characters in GECOS field: replace them
6025 ** ith ASCII "equivalent".
6028 /* Peter Eriksson of Linkopings universitet */
6029 "_FFR_HANDLE_ISO8859_GECOS",
6030 #endif /* _FFR_HANDLE_ISO8859_GECOS */
6032 /* Set 'h' in {addr_type} for headers. */
6034 #endif /* _FFR_HDR_TYPE */
6035 #if _FFR_HPUX_NSSWITCH
6036 /* Use nsswitch on HP-UX */
6037 "_FFR_HPUX_NSSWITCH",
6038 #endif /* _FFR_HPUX_NSSWITCH */
6039 #if _FFR_IGNORE_BOGUS_ADDR
6040 /* Ignore addresses for which prescan() failed */
6041 "_FFR_IGNORE_BOGUS_ADDR",
6042 #endif /* _FFR_IGNORE_BOGUS_ADDR */
6043 #if _FFR_IGNORE_EXT_ON_HELO
6044 /* Ignore extensions offered in response to HELO */
6045 "_FFR_IGNORE_EXT_ON_HELO",
6046 #endif /* _FFR_IGNORE_EXT_ON_HELO */
6047 #if _FFR_LDAP_RECURSION
6048 /* Support LDAP recursion in LDAP responses */
6050 "_FFR_LDAP_RECURSION",
6051 #endif /* _FFR_LDAP_RECURSION */
6052 #if _FFR_LDAP_SETVERSION
6053 /* New LDAP map option for setting LDAP protocol version */
6054 "_FFR_LDAP_SETVERSION",
6055 #endif /* _FFR_LDAP_SETVERSION */
6057 /* Support LDAP URI form of specifying host/port (and allows ldaps) */
6059 #endif /* _FFR_LDAP_URI */
6060 #if _FFR_MAX_FORWARD_ENTRIES
6061 /* Try to limit number of .forward entries */
6062 /* (doesn't work) */
6063 /* Randall S. Winchester of the University of Maryland */
6064 "_FFR_MAX_FORWARD_ENTRIES",
6065 #endif /* _FFR_MAX_FORWARD_ENTRIES */
6066 #if _FFR_MAX_SLEEP_TIME
6067 /* Limit sleep(2) time in libsm/clock.c */
6068 "_FFR_MAX_SLEEP_TIME",
6069 #endif /* _FFR_MAX_SLEEP_TIME */
6071 # if _FFR_MILTER_421
6072 /* If a filter returns 421, close the SMTP connection */
6074 # endif /* _FFR_MILTER_421 */
6075 # if _FFR_MILTER_PERDAEMON
6076 /* Per DaemonPortOptions InputMailFilter lists */
6077 "_FFR_MILTER_PERDAEMON",
6078 # endif /* _FFR_MILTER_PERDAEMON */
6080 #if _FFR_NODELAYDSN_ON_HOLD
6081 /* Do not issue a DELAY DSN for mailers that use the hold flag. */
6083 "_FFR_NODELAYDSN_ON_HOLD",
6084 #endif /* _FFR_NODELAYDSN_ON_HOLD */
6086 /* Disable PIPELINING, delay client if used. */
6088 #endif /* _FFR_NO_PIPE */
6090 /* Quarantine items in the queue */
6092 #endif /* _FFR_QUARANTINE */
6094 /* Exponential queue delay; disabled in 8.13 since it isn't used. */
6096 #endif /* _FFR_QUEUEDELAY */
6097 #if _FFR_QUEUE_GROUP_SORTORDER
6098 /* Allow QueueSortOrder per queue group. */
6099 /* XXX: Still need to actually use qgrp->qg_sortorder */
6100 "_FFR_QUEUE_GROUP_SORTORDER",
6101 #endif /* _FFR_QUEUE_GROUP_SORTORDER */
6102 #if _FFR_QUEUE_MACRO
6103 /* Define {queue} macro. */
6105 #endif /* _FFR_QUEUE_MACRO */
6106 #if _FFR_QUEUERETURN_DSN
6108 ** Provide an option for different Timeout.queue{warn,return} for
6109 ** DSN messages. These days, queues are filled with bounces for
6110 ** spam that will never make it to the sender and therefore slow
6111 ** down queue runs until they timeout.
6114 "_FFR_QUEUERETURN_DSN",
6115 #endif /* _FFR_QUEUERETURN_DSN */
6116 #if _FFR_QUEUE_RUN_PARANOIA
6117 /* Additional checks when doing queue runs. */
6118 "_FFR_QUEUE_RUN_PARANOIA",
6119 #endif /* _FFR_QUEUE_RUN_PARANOIA */
6120 #if _FFR_QUEUE_SCHED_DBG
6121 /* Debug output for the queue scheduler. */
6122 "_FFR_QUEUE_SCHED_DBG",
6123 #endif /* _FFR_QUEUE_SCHED_DBG */
6124 #if _FFR_REDIRECTEMPTY
6126 ** envelope <> can't be sent to mailing lists, only owner-
6127 ** send spam of this type to owner- of the list
6128 ** ---- to stop spam from going to mailing lists.
6131 "_FFR_REDIRECTEMPTY",
6132 #endif /* _FFR_REDIRECTEMPTY */
6134 /* Log when we start/stop rejecting connections due to load, etc */
6136 #endif /* _FFR_REJECT_LOG */
6137 #if _FFR_REQ_DIR_FSYNC_OPT
6138 /* Add cf option to fsync() directories */
6139 "_FFR_REQ_DIR_FSYNC_OPT",
6140 #endif /* _FFR_REQ_DIR_FSYNC_OPT */
6141 #if _FFR_RESET_MACRO_GLOBALS
6142 /* Allow macro 'j' to be set dynamically via rulesets. */
6143 "_FFR_RESET_MACRO_GLOBALS",
6144 #endif /* _FFR_RESET_MACRO_GLOBALS */
6145 #if _FFR_RESPOND_ALL
6146 /* in vacation: respond to every message, not just once per interval */
6148 #endif /* _FFR_RESPOND_ALL */
6150 /* Random shuffle for queue sorting. */
6152 #endif /* _FFR_RHS */
6154 /* Support SASL's SASL_SEC_MUTUAL_AUTH option */
6156 #endif /* _FFR_SASL_OPT_M */
6158 /* Auto-select of shared memory key */
6160 #endif /* _FFR_SELECT_SHM */
6162 /* Donated code (unused). */
6164 #endif /* _FFR_SHM_STATUS */
6165 #if _FFR_SLEEP_USE_SELECT
6166 /* Use select(2) in libsm/clock.c to emulate sleep(2) */
6167 "_FFR_SLEEP_USE_SELECT ",
6168 #endif /* _FFR_SLEEP_USE_SELECT */
6169 #if _FFR_SMFI_OPENSOCKET
6170 /* libmilter: smfi_opensocket() to force the socket open early */
6171 "_FFR_SMFI_OPENSOCKET",
6172 #endif /* _FFR_SMFI_OPENSOCKET */
6174 /* Support for smtps (SMTP over SSL) */
6176 #endif /* _FFR_SMTP_SSL */
6177 #if _FFR_SOFT_BOUNCE
6178 /* Turn all errors into temporary errors. */
6180 #endif /* _FFR_SOFT_BOUNCE */
6183 ** It looks like the Compaq Tru64 5.1A now aligns argv and envp to 64
6184 ** bit alignment, so unless each piece of argv and envp is a multiple
6185 ** of 8 bytes (including terminating NULL), initsetproctitle() won't
6186 ** use any of the space beyond argv[0]. Be sure to set SPT_ALIGN_SIZE
6187 ** if you use this FFR.
6190 /* Chris Adams of HiWAAY Informations Services */
6192 #endif /* _FFR_SPT_ALIGN */
6193 #if _FFR_STRIPBACKSL
6195 ** Strip backslash from addresses (so sender doesn't
6196 ** decide to ignore forward)
6200 #endif /* _FFR_STRIPBACKSL */
6202 /* Donated code (unused). */
6204 #endif /* _FFR_TIMERS */
6206 /* More STARTTLS options, e.g., secondary certs. */
6208 #endif /* _FFR_TLS_1 */
6211 ** If we don't own the file mark it as unsafe.
6212 ** However, allow TrustedUser to own it as well
6213 ** in case TrustedUser manipulates the queue.
6217 #endif /* _FFR_TRUSTED_QF */
6218 #if _FFR_USE_SETLOGIN
6219 /* Use setlogin() */
6221 "_FFR_USE_SETLOGIN",
6222 #endif /* _FFR_USE_SETLOGIN */