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