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