1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)conf.c 8.82 (Berkeley) 03/06/94"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include "pathnames.h" 15 # include <sys/ioctl.h> 16 # include <sys/param.h> 17 # include <netdb.h> 18 # include <pwd.h> 19 20 /* 21 ** CONF.C -- Sendmail Configuration Tables. 22 ** 23 ** Defines the configuration of this installation. 24 ** 25 ** Configuration Variables: 26 ** HdrInfo -- a table describing well-known header fields. 27 ** Each entry has the field name and some flags, 28 ** which are described in sendmail.h. 29 ** 30 ** Notes: 31 ** I have tried to put almost all the reasonable 32 ** configuration information into the configuration 33 ** file read at runtime. My intent is that anything 34 ** here is a function of the version of UNIX you 35 ** are running, or is really static -- for example 36 ** the headers are a superset of widely used 37 ** protocols. If you find yourself playing with 38 ** this file too much, you may be making a mistake! 39 */ 40 41 42 43 44 /* 45 ** Header info table 46 ** Final (null) entry contains the flags used for any other field. 47 ** 48 ** Not all of these are actually handled specially by sendmail 49 ** at this time. They are included as placeholders, to let 50 ** you know that "someday" I intend to have sendmail do 51 ** something with them. 52 */ 53 54 struct hdrinfo HdrInfo[] = 55 { 56 /* originator fields, most to least significant */ 57 "resent-sender", H_FROM|H_RESENT, 58 "resent-from", H_FROM|H_RESENT, 59 "resent-reply-to", H_FROM|H_RESENT, 60 "sender", H_FROM, 61 "from", H_FROM, 62 "reply-to", H_FROM, 63 "full-name", H_ACHECK, 64 "return-receipt-to", H_FROM|H_RECEIPTTO, 65 "errors-to", H_FROM|H_ERRORSTO, 66 67 /* destination fields */ 68 "to", H_RCPT, 69 "resent-to", H_RCPT|H_RESENT, 70 "cc", H_RCPT, 71 "resent-cc", H_RCPT|H_RESENT, 72 "bcc", H_RCPT|H_ACHECK, 73 "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 74 "apparently-to", H_RCPT, 75 76 /* message identification and control */ 77 "message-id", 0, 78 "resent-message-id", H_RESENT, 79 "message", H_EOH, 80 "text", H_EOH, 81 82 /* date fields */ 83 "date", 0, 84 "resent-date", H_RESENT, 85 86 /* trace fields */ 87 "received", H_TRACE|H_FORCE, 88 "x400-received", H_TRACE|H_FORCE, 89 "via", H_TRACE|H_FORCE, 90 "mail-from", H_TRACE|H_FORCE, 91 92 /* miscellaneous fields */ 93 "comments", H_FORCE, 94 "return-path", H_FORCE|H_ACHECK, 95 96 NULL, 0, 97 }; 98 99 100 101 /* 102 ** Location of system files/databases/etc. 103 */ 104 105 char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 106 107 108 109 /* 110 ** Privacy values 111 */ 112 113 struct prival PrivacyValues[] = 114 { 115 "public", PRIV_PUBLIC, 116 "needmailhelo", PRIV_NEEDMAILHELO, 117 "needexpnhelo", PRIV_NEEDEXPNHELO, 118 "needvrfyhelo", PRIV_NEEDVRFYHELO, 119 "noexpn", PRIV_NOEXPN, 120 "novrfy", PRIV_NOVRFY, 121 "restrictmailq", PRIV_RESTRICTMAILQ, 122 "restrictqrun", PRIV_RESTRICTQRUN, 123 "authwarnings", PRIV_AUTHWARNINGS, 124 "goaway", PRIV_GOAWAY, 125 NULL, 0, 126 }; 127 128 129 130 /* 131 ** Miscellaneous stuff. 132 */ 133 134 int DtableSize = 50; /* max open files; reset in 4.2bsd */ 135 136 137 /* 138 ** Following should be config parameters (and probably will be in 139 ** future releases). In the meantime, setting these is considered 140 ** unsupported, and is intentionally undocumented. 141 */ 142 143 #ifdef BROKENSMTPPEERS 144 bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 145 #else 146 bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 147 #endif 148 #ifdef NOLOOPBACKCHECK 149 bool CheckLoopBack = FALSE; /* set to check HELO loopback */ 150 #else 151 bool CheckLoopBack = TRUE; /* set to check HELO loopback */ 152 #endif 153 154 /* 155 ** SETDEFAULTS -- set default values 156 ** 157 ** Because of the way freezing is done, these must be initialized 158 ** using direct code. 159 ** 160 ** Parameters: 161 ** e -- the default envelope. 162 ** 163 ** Returns: 164 ** none. 165 ** 166 ** Side Effects: 167 ** Initializes a bunch of global variables to their 168 ** default values. 169 */ 170 171 #define DAYS * 24 * 60 * 60 172 173 setdefaults(e) 174 register ENVELOPE *e; 175 { 176 SpaceSub = ' '; /* option B */ 177 QueueLA = 8; /* option x */ 178 RefuseLA = 12; /* option X */ 179 WkRecipFact = 30000L; /* option y */ 180 WkClassFact = 1800L; /* option z */ 181 WkTimeFact = 90000L; /* option Z */ 182 QueueFactor = WkRecipFact * 20; /* option q */ 183 FileMode = (RealUid != geteuid()) ? 0644 : 0600; 184 /* option F */ 185 DefUid = 1; /* option u */ 186 DefGid = 1; /* option g */ 187 CheckpointInterval = 10; /* option C */ 188 MaxHopCount = 25; /* option h */ 189 e->e_sendmode = SM_FORK; /* option d */ 190 e->e_errormode = EM_PRINT; /* option e */ 191 SevenBit = FALSE; /* option 7 */ 192 MaxMciCache = 1; /* option k */ 193 MciCacheTimeout = 300; /* option K */ 194 LogLevel = 9; /* option L */ 195 settimeouts(NULL); /* option r */ 196 TimeOuts.to_q_return = 5 DAYS; /* option T */ 197 TimeOuts.to_q_warning = 0; /* option T */ 198 PrivacyFlags = 0; /* option p */ 199 setdefuser(); 200 setupmaps(); 201 setupmailers(); 202 } 203 204 205 /* 206 ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 207 */ 208 209 setdefuser() 210 { 211 struct passwd *defpwent; 212 static char defuserbuf[40]; 213 214 DefUser = defuserbuf; 215 if ((defpwent = getpwuid(DefUid)) != NULL) 216 strcpy(defuserbuf, defpwent->pw_name); 217 else 218 strcpy(defuserbuf, "nobody"); 219 } 220 /* 221 ** HOST_MAP_INIT -- initialize host class structures 222 */ 223 224 bool 225 host_map_init(map, args) 226 MAP *map; 227 char *args; 228 { 229 register char *p = args; 230 231 for (;;) 232 { 233 while (isascii(*p) && isspace(*p)) 234 p++; 235 if (*p != '-') 236 break; 237 switch (*++p) 238 { 239 case 'a': 240 map->map_app = ++p; 241 break; 242 } 243 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 244 p++; 245 if (*p != '\0') 246 *p++ = '\0'; 247 } 248 if (map->map_app != NULL) 249 map->map_app = newstr(map->map_app); 250 return TRUE; 251 } 252 /* 253 ** SETUPMAILERS -- initialize default mailers 254 */ 255 256 setupmailers() 257 { 258 char buf[100]; 259 260 strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 261 makemailer(buf); 262 263 strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 264 makemailer(buf); 265 266 strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 267 makemailer(buf); 268 } 269 /* 270 ** SETUPMAPS -- set up map classes 271 */ 272 273 #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 274 { \ 275 extern bool parse __P((MAP *, char *)); \ 276 extern bool open __P((MAP *, int)); \ 277 extern void close __P((MAP *)); \ 278 extern char *lookup __P((MAP *, char *, char **, int *)); \ 279 extern void store __P((MAP *, char *, char *)); \ 280 s = stab(name, ST_MAPCLASS, ST_ENTER); \ 281 s->s_mapclass.map_cname = name; \ 282 s->s_mapclass.map_ext = ext; \ 283 s->s_mapclass.map_cflags = flags; \ 284 s->s_mapclass.map_parse = parse; \ 285 s->s_mapclass.map_open = open; \ 286 s->s_mapclass.map_close = close; \ 287 s->s_mapclass.map_lookup = lookup; \ 288 s->s_mapclass.map_store = store; \ 289 } 290 291 setupmaps() 292 { 293 register STAB *s; 294 295 #ifdef NEWDB 296 MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 297 map_parseargs, hash_map_open, db_map_close, 298 db_map_lookup, db_map_store); 299 MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 300 map_parseargs, bt_map_open, db_map_close, 301 db_map_lookup, db_map_store); 302 #endif 303 304 #ifdef NDBM 305 MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 306 map_parseargs, ndbm_map_open, ndbm_map_close, 307 ndbm_map_lookup, ndbm_map_store); 308 #endif 309 310 #ifdef NIS 311 MAPDEF("nis", NULL, MCF_ALIASOK, 312 map_parseargs, nis_map_open, nis_map_close, 313 nis_map_lookup, nis_map_store); 314 #endif 315 316 MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 317 map_parseargs, stab_map_open, stab_map_close, 318 stab_map_lookup, stab_map_store); 319 320 MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 321 map_parseargs, impl_map_open, impl_map_close, 322 impl_map_lookup, impl_map_store); 323 324 /* host DNS lookup */ 325 MAPDEF("host", NULL, 0, 326 host_map_init, null_map_open, null_map_close, 327 host_map_lookup, null_map_store); 328 329 /* dequote map */ 330 MAPDEF("dequote", NULL, 0, 331 dequote_init, null_map_open, null_map_close, 332 dequote_map, null_map_store); 333 334 #if 0 335 # ifdef USERDB 336 /* user database */ 337 MAPDEF("udb", ".db", 0, 338 udb_map_parse, null_map_open, null_map_close, 339 udb_map_lookup, null_map_store); 340 # endif 341 #endif 342 } 343 344 #undef MAPDEF 345 /* 346 ** USERNAME -- return the user id of the logged in user. 347 ** 348 ** Parameters: 349 ** none. 350 ** 351 ** Returns: 352 ** The login name of the logged in user. 353 ** 354 ** Side Effects: 355 ** none. 356 ** 357 ** Notes: 358 ** The return value is statically allocated. 359 */ 360 361 char * 362 username() 363 { 364 static char *myname = NULL; 365 extern char *getlogin(); 366 register struct passwd *pw; 367 368 /* cache the result */ 369 if (myname == NULL) 370 { 371 myname = getlogin(); 372 if (myname == NULL || myname[0] == '\0') 373 { 374 pw = getpwuid(RealUid); 375 if (pw != NULL) 376 myname = newstr(pw->pw_name); 377 } 378 else 379 { 380 uid_t uid = RealUid; 381 382 myname = newstr(myname); 383 if ((pw = getpwnam(myname)) == NULL || 384 (uid != 0 && uid != pw->pw_uid)) 385 { 386 pw = getpwuid(uid); 387 if (pw != NULL) 388 myname = newstr(pw->pw_name); 389 } 390 } 391 if (myname == NULL || myname[0] == '\0') 392 { 393 syserr("554 Who are you?"); 394 myname = "postmaster"; 395 } 396 } 397 398 return (myname); 399 } 400 /* 401 ** TTYPATH -- Get the path of the user's tty 402 ** 403 ** Returns the pathname of the user's tty. Returns NULL if 404 ** the user is not logged in or if s/he has write permission 405 ** denied. 406 ** 407 ** Parameters: 408 ** none 409 ** 410 ** Returns: 411 ** pathname of the user's tty. 412 ** NULL if not logged in or write permission denied. 413 ** 414 ** Side Effects: 415 ** none. 416 ** 417 ** WARNING: 418 ** Return value is in a local buffer. 419 ** 420 ** Called By: 421 ** savemail 422 */ 423 424 char * 425 ttypath() 426 { 427 struct stat stbuf; 428 register char *pathn; 429 extern char *ttyname(); 430 extern char *getlogin(); 431 432 /* compute the pathname of the controlling tty */ 433 if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 434 (pathn = ttyname(0)) == NULL) 435 { 436 errno = 0; 437 return (NULL); 438 } 439 440 /* see if we have write permission */ 441 if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 442 { 443 errno = 0; 444 return (NULL); 445 } 446 447 /* see if the user is logged in */ 448 if (getlogin() == NULL) 449 return (NULL); 450 451 /* looks good */ 452 return (pathn); 453 } 454 /* 455 ** CHECKCOMPAT -- check for From and To person compatible. 456 ** 457 ** This routine can be supplied on a per-installation basis 458 ** to determine whether a person is allowed to send a message. 459 ** This allows restriction of certain types of internet 460 ** forwarding or registration of users. 461 ** 462 ** If the hosts are found to be incompatible, an error 463 ** message should be given using "usrerr" and 0 should 464 ** be returned. 465 ** 466 ** 'NoReturn' can be set to suppress the return-to-sender 467 ** function; this should be done on huge messages. 468 ** 469 ** Parameters: 470 ** to -- the person being sent to. 471 ** 472 ** Returns: 473 ** an exit status 474 ** 475 ** Side Effects: 476 ** none (unless you include the usrerr stuff) 477 */ 478 479 checkcompat(to, e) 480 register ADDRESS *to; 481 register ENVELOPE *e; 482 { 483 # ifdef lint 484 if (to == NULL) 485 to++; 486 # endif /* lint */ 487 488 if (tTd(49, 1)) 489 printf("checkcompat(to=%s, from=%s)\n", 490 to->q_paddr, e->e_from.q_paddr); 491 492 # ifdef EXAMPLE_CODE 493 /* this code is intended as an example only */ 494 register STAB *s; 495 496 s = stab("arpa", ST_MAILER, ST_FIND); 497 if (s != NULL && e->e_from.q_mailer != LocalMailer && 498 to->q_mailer == s->s_mailer) 499 { 500 usrerr("553 No ARPA mail through this machine: see your system administration"); 501 /* NoReturn = TRUE; to supress return copy */ 502 return (EX_UNAVAILABLE); 503 } 504 # endif /* EXAMPLE_CODE */ 505 return (EX_OK); 506 } 507 /* 508 ** SETSIGNAL -- set a signal handler 509 ** 510 ** This is essentially old BSD "signal(3)". 511 */ 512 513 sigfunc_t 514 setsignal(sig, handler) 515 int sig; 516 sigfunc_t handler; 517 { 518 #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 519 return signal(sig, handler); 520 #else 521 struct sigaction n, o; 522 523 bzero(&n, sizeof n); 524 n.sa_handler = handler; 525 if (sigaction(sig, &n, &o) < 0) 526 return SIG_ERR; 527 return o.sa_handler; 528 #endif 529 } 530 /* 531 ** HOLDSIGS -- arrange to hold all signals 532 ** 533 ** Parameters: 534 ** none. 535 ** 536 ** Returns: 537 ** none. 538 ** 539 ** Side Effects: 540 ** Arranges that signals are held. 541 */ 542 543 holdsigs() 544 { 545 } 546 /* 547 ** RLSESIGS -- arrange to release all signals 548 ** 549 ** This undoes the effect of holdsigs. 550 ** 551 ** Parameters: 552 ** none. 553 ** 554 ** Returns: 555 ** none. 556 ** 557 ** Side Effects: 558 ** Arranges that signals are released. 559 */ 560 561 rlsesigs() 562 { 563 } 564 /* 565 ** INIT_MD -- do machine dependent initializations 566 ** 567 ** Systems that have global modes that should be set should do 568 ** them here rather than in main. 569 */ 570 571 #ifdef _AUX_SOURCE 572 # include <compat.h> 573 #endif 574 575 init_md(argc, argv) 576 int argc; 577 char **argv; 578 { 579 #ifdef _AUX_SOURCE 580 setcompat(getcompat() | COMPAT_BSDPROT); 581 #endif 582 } 583 /* 584 ** GETLA -- get the current load average 585 ** 586 ** This code stolen from la.c. 587 ** 588 ** Parameters: 589 ** none. 590 ** 591 ** Returns: 592 ** The current load average as an integer. 593 ** 594 ** Side Effects: 595 ** none. 596 */ 597 598 /* try to guess what style of load average we have */ 599 #define LA_ZERO 1 /* always return load average as zero */ 600 #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 601 #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 602 #define LA_SUBR 4 /* call getloadavg */ 603 #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 604 #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 605 #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 606 607 /* do guesses based on general OS type */ 608 #ifndef LA_TYPE 609 # define LA_TYPE LA_ZERO 610 #endif 611 612 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 613 614 #include <nlist.h> 615 616 #ifndef LA_AVENRUN 617 # ifdef SYSTEM5 618 # define LA_AVENRUN "avenrun" 619 # else 620 # define LA_AVENRUN "_avenrun" 621 # endif 622 #endif 623 624 /* _PATH_UNIX should be defined in <paths.h> */ 625 #ifndef _PATH_UNIX 626 # if defined(SYSTEM5) 627 # define _PATH_UNIX "/unix" 628 # else 629 # define _PATH_UNIX "/vmunix" 630 # endif 631 #endif 632 633 struct nlist Nl[] = 634 { 635 { LA_AVENRUN }, 636 #define X_AVENRUN 0 637 { 0 }, 638 }; 639 640 #ifndef FSHIFT 641 # if defined(unixpc) 642 # define FSHIFT 5 643 # endif 644 645 # if defined(__alpha) 646 # define FSHIFT 10 647 # endif 648 649 # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 650 # define FSHIFT 8 651 # endif 652 #endif 653 654 #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 655 # define FSCALE (1 << FSHIFT) 656 #endif 657 658 getla() 659 { 660 static int kmem = -1; 661 #if LA_TYPE == LA_INT 662 long avenrun[3]; 663 #else 664 # if LA_TYPE == LA_SHORT 665 short avenrun[3]; 666 # else 667 double avenrun[3]; 668 # endif 669 #endif 670 extern off_t lseek(); 671 extern int errno; 672 673 if (kmem < 0) 674 { 675 kmem = open("/dev/kmem", 0, 0); 676 if (kmem < 0) 677 { 678 if (tTd(3, 1)) 679 printf("getla: open(/dev/kmem): %s\n", 680 errstring(errno)); 681 return (-1); 682 } 683 (void) fcntl(kmem, F_SETFD, 1); 684 if (nlist(_PATH_UNIX, Nl) < 0) 685 { 686 if (tTd(3, 1)) 687 printf("getla: nlist(%s): %s\n", _PATH_UNIX, 688 errstring(errno)); 689 return (-1); 690 } 691 if (Nl[X_AVENRUN].n_value == 0) 692 { 693 if (tTd(3, 1)) 694 printf("getla: nlist(%s, %s) ==> 0\n", 695 _PATH_UNIX, LA_AVENRUN); 696 return (-1); 697 } 698 } 699 if (tTd(3, 20)) 700 printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 701 if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 702 read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 703 { 704 /* thank you Ian */ 705 if (tTd(3, 1)) 706 printf("getla: lseek or read: %s\n", errstring(errno)); 707 return (-1); 708 } 709 #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 710 if (tTd(3, 5)) 711 { 712 printf("getla: avenrun = %d", avenrun[0]); 713 if (tTd(3, 15)) 714 printf(", %d, %d", avenrun[1], avenrun[2]); 715 printf("\n"); 716 } 717 if (tTd(3, 1)) 718 printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 719 return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 720 #else 721 if (tTd(3, 5)) 722 { 723 printf("getla: avenrun = %g", avenrun[0]); 724 if (tTd(3, 15)) 725 printf(", %g, %g", avenrun[1], avenrun[2]); 726 printf("\n"); 727 } 728 if (tTd(3, 1)) 729 printf("getla: %d\n", (int) (avenrun[0] +0.5)); 730 return ((int) (avenrun[0] + 0.5)); 731 #endif 732 } 733 734 #else 735 #if LA_TYPE == LA_SUBR 736 737 #ifdef DGUX 738 739 #include <sys/dg_sys_info.h> 740 741 int getla() 742 { 743 struct dg_sys_info_load_info load_info; 744 745 dg_sys_info((long *)&load_info, 746 DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 747 748 return((int) (load_info.one_minute + 0.5)); 749 } 750 751 #else 752 753 getla() 754 { 755 double avenrun[3]; 756 757 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 758 { 759 if (tTd(3, 1)) 760 perror("getla: getloadavg failed:"); 761 return (-1); 762 } 763 if (tTd(3, 1)) 764 printf("getla: %d\n", (int) (avenrun[0] +0.5)); 765 return ((int) (avenrun[0] + 0.5)); 766 } 767 768 #endif /* DGUX */ 769 #else 770 #if LA_TYPE == LA_MACH 771 772 /* 773 ** This has been tested on NEXTSTEP release 2.1/3.X. 774 */ 775 776 #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 777 # include <mach/mach.h> 778 #else 779 # include <mach.h> 780 #endif 781 782 getla() 783 { 784 processor_set_t default_set; 785 kern_return_t error; 786 unsigned int info_count; 787 struct processor_set_basic_info info; 788 host_t host; 789 790 error = processor_set_default(host_self(), &default_set); 791 if (error != KERN_SUCCESS) 792 return -1; 793 info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 794 if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 795 &host, (processor_set_info_t)&info, 796 &info_count) != KERN_SUCCESS) 797 { 798 return -1; 799 } 800 return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 801 } 802 803 804 #else 805 #if LA_TYPE == LA_PROCSTR 806 807 /* 808 ** Read /proc/loadavg for the load average. This is assumed to be 809 ** in a format like "0.15 0.12 0.06". 810 ** 811 ** Initially intended for Linux. This has been in the kernel 812 ** since at least 0.99.15. 813 */ 814 815 # ifndef _PATH_LOADAVG 816 # define _PATH_LOADAVG "/proc/loadavg" 817 # endif 818 819 int 820 getla() 821 { 822 double avenrun; 823 register int result; 824 FILE *fp; 825 826 fp = fopen(_PATH_LOADAVG, "r"); 827 if (fp == NULL) 828 { 829 if (tTd(3, 1)) 830 printf("getla: fopen(%s): %s\n", 831 _PATH_LOADAVG, errstring(errno)); 832 return -1; 833 } 834 result = fscanf(fp, "%lf", &avenrun); 835 fclose(fp); 836 if (result != 1) 837 { 838 if (tTd(3, 1)) 839 printf("getla: fscanf() = %d: %s\n", 840 result, errstring(errno)); 841 return -1; 842 } 843 844 if (tTd(3, 1)) 845 printf("getla(): %.2f\n", avenrun); 846 847 return ((int) (avenrun + 0.5)); 848 } 849 850 #else 851 852 getla() 853 { 854 if (tTd(3, 1)) 855 printf("getla: ZERO\n"); 856 return (0); 857 } 858 859 #endif 860 #endif 861 #endif 862 #endif 863 864 865 /* 866 * Copyright 1989 Massachusetts Institute of Technology 867 * 868 * Permission to use, copy, modify, distribute, and sell this software and its 869 * documentation for any purpose is hereby granted without fee, provided that 870 * the above copyright notice appear in all copies and that both that 871 * copyright notice and this permission notice appear in supporting 872 * documentation, and that the name of M.I.T. not be used in advertising or 873 * publicity pertaining to distribution of the software without specific, 874 * written prior permission. M.I.T. makes no representations about the 875 * suitability of this software for any purpose. It is provided "as is" 876 * without express or implied warranty. 877 * 878 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 879 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 880 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 881 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 882 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 883 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 884 * 885 * Authors: Many and varied... 886 */ 887 888 /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 889 #ifndef lint 890 static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 891 #endif /* !lint */ 892 893 #ifdef apollo 894 # undef volatile 895 # include <apollo/base.h> 896 897 /* ARGSUSED */ 898 int getloadavg( call_data ) 899 caddr_t call_data; /* pointer to (double) return value */ 900 { 901 double *avenrun = (double *) call_data; 902 int i; 903 status_$t st; 904 long loadav[3]; 905 proc1_$get_loadav(loadav, &st); 906 *avenrun = loadav[0] / (double) (1 << 16); 907 return(0); 908 } 909 # endif /* apollo */ 910 /* 911 ** SHOULDQUEUE -- should this message be queued or sent? 912 ** 913 ** Compares the message cost to the load average to decide. 914 ** 915 ** Parameters: 916 ** pri -- the priority of the message in question. 917 ** ctime -- the message creation time. 918 ** 919 ** Returns: 920 ** TRUE -- if this message should be queued up for the 921 ** time being. 922 ** FALSE -- if the load is low enough to send this message. 923 ** 924 ** Side Effects: 925 ** none. 926 */ 927 928 bool 929 shouldqueue(pri, ctime) 930 long pri; 931 time_t ctime; 932 { 933 if (CurrentLA < QueueLA) 934 return (FALSE); 935 if (CurrentLA >= RefuseLA) 936 return (TRUE); 937 return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 938 } 939 /* 940 ** REFUSECONNECTIONS -- decide if connections should be refused 941 ** 942 ** Parameters: 943 ** none. 944 ** 945 ** Returns: 946 ** TRUE if incoming SMTP connections should be refused 947 ** (for now). 948 ** FALSE if we should accept new work. 949 ** 950 ** Side Effects: 951 ** none. 952 */ 953 954 bool 955 refuseconnections() 956 { 957 #ifdef XLA 958 if (!xla_smtp_ok()) 959 return TRUE; 960 #endif 961 962 /* this is probably too simplistic */ 963 return (CurrentLA >= RefuseLA); 964 } 965 /* 966 ** SETPROCTITLE -- set process title for ps 967 ** 968 ** Parameters: 969 ** fmt -- a printf style format string. 970 ** a, b, c -- possible parameters to fmt. 971 ** 972 ** Returns: 973 ** none. 974 ** 975 ** Side Effects: 976 ** Clobbers argv of our main procedure so ps(1) will 977 ** display the title. 978 */ 979 980 #ifdef SETPROCTITLE 981 # ifdef HASSETPROCTITLE 982 *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 983 # endif 984 # ifdef __hpux 985 # include <sys/pstat.h> 986 # endif 987 # ifdef BSD4_4 988 # include <machine/vmparam.h> 989 # include <sys/exec.h> 990 # ifdef __bsdi__ 991 # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 992 # define PROCTITLEPAD '\0' 993 # endif 994 # ifdef PS_STRINGS 995 # define SETPROC_STATIC static 996 # endif 997 # endif 998 # ifndef SETPROC_STATIC 999 # define SETPROC_STATIC 1000 # endif 1001 #endif 1002 1003 #ifndef PROCTITLEPAD 1004 # define PROCTITLEPAD ' ' 1005 #endif 1006 1007 #ifndef HASSETPROCTITLE 1008 1009 /*VARARGS1*/ 1010 #ifdef __STDC__ 1011 setproctitle(char *fmt, ...) 1012 #else 1013 setproctitle(fmt, va_alist) 1014 char *fmt; 1015 va_dcl 1016 #endif 1017 { 1018 # ifdef SETPROCTITLE 1019 register char *p; 1020 register int i; 1021 SETPROC_STATIC char buf[MAXLINE]; 1022 VA_LOCAL_DECL 1023 # ifdef __hpux 1024 union pstun pst; 1025 # endif 1026 extern char **Argv; 1027 extern char *LastArgv; 1028 1029 p = buf; 1030 1031 /* print sendmail: heading for grep */ 1032 (void) strcpy(p, "sendmail: "); 1033 p += strlen(p); 1034 1035 /* print the argument string */ 1036 VA_START(fmt); 1037 (void) vsprintf(p, fmt, ap); 1038 VA_END; 1039 1040 i = strlen(buf); 1041 1042 # ifdef __hpux 1043 pst.pst_command = buf; 1044 pstat(PSTAT_SETCMD, pst, i, 0, 0); 1045 # else 1046 # ifdef PS_STRINGS 1047 PS_STRINGS->ps_nargvstr = 1; 1048 PS_STRINGS->ps_argvstr = buf; 1049 # else 1050 if (i > LastArgv - Argv[0] - 2) 1051 { 1052 i = LastArgv - Argv[0] - 2; 1053 buf[i] = '\0'; 1054 } 1055 (void) strcpy(Argv[0], buf); 1056 p = &Argv[0][i]; 1057 while (p < LastArgv) 1058 *p++ = PROCTITLEPAD; 1059 # endif 1060 # endif 1061 # endif /* SETPROCTITLE */ 1062 } 1063 1064 #endif 1065 /* 1066 ** REAPCHILD -- pick up the body of my child, lest it become a zombie 1067 ** 1068 ** Parameters: 1069 ** none. 1070 ** 1071 ** Returns: 1072 ** none. 1073 ** 1074 ** Side Effects: 1075 ** Picks up extant zombies. 1076 */ 1077 1078 void 1079 reapchild() 1080 { 1081 int olderrno = errno; 1082 # ifdef HASWAITPID 1083 auto int status; 1084 int count; 1085 int pid; 1086 1087 count = 0; 1088 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 1089 { 1090 if (count++ > 1000) 1091 { 1092 syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 1093 pid, status); 1094 break; 1095 } 1096 } 1097 # else 1098 # ifdef WNOHANG 1099 union wait status; 1100 1101 while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 1102 continue; 1103 # else /* WNOHANG */ 1104 auto int status; 1105 1106 while (wait(&status) > 0) 1107 continue; 1108 # endif /* WNOHANG */ 1109 # endif 1110 # ifdef SYS5SIGNALS 1111 (void) setsignal(SIGCHLD, reapchild); 1112 # endif 1113 errno = olderrno; 1114 } 1115 /* 1116 ** UNSETENV -- remove a variable from the environment 1117 ** 1118 ** Not needed on newer systems. 1119 ** 1120 ** Parameters: 1121 ** name -- the string name of the environment variable to be 1122 ** deleted from the current environment. 1123 ** 1124 ** Returns: 1125 ** none. 1126 ** 1127 ** Globals: 1128 ** environ -- a pointer to the current environment. 1129 ** 1130 ** Side Effects: 1131 ** Modifies environ. 1132 */ 1133 1134 #ifndef HASUNSETENV 1135 1136 void 1137 unsetenv(name) 1138 char *name; 1139 { 1140 extern char **environ; 1141 register char **pp; 1142 int len = strlen(name); 1143 1144 for (pp = environ; *pp != NULL; pp++) 1145 { 1146 if (strncmp(name, *pp, len) == 0 && 1147 ((*pp)[len] == '=' || (*pp)[len] == '\0')) 1148 break; 1149 } 1150 1151 for (; *pp != NULL; pp++) 1152 *pp = pp[1]; 1153 } 1154 1155 #endif 1156 /* 1157 ** GETDTABLESIZE -- return number of file descriptors 1158 ** 1159 ** Only on non-BSD systems 1160 ** 1161 ** Parameters: 1162 ** none 1163 ** 1164 ** Returns: 1165 ** size of file descriptor table 1166 ** 1167 ** Side Effects: 1168 ** none 1169 */ 1170 1171 #ifdef SOLARIS 1172 # include <sys/resource.h> 1173 #endif 1174 1175 int 1176 getdtsize() 1177 { 1178 #ifdef RLIMIT_NOFILE 1179 struct rlimit rl; 1180 1181 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 1182 return rl.rlim_cur; 1183 #endif 1184 1185 # ifdef HASGETDTABLESIZE 1186 return getdtablesize(); 1187 # else 1188 # ifdef _SC_OPEN_MAX 1189 return sysconf(_SC_OPEN_MAX); 1190 # else 1191 return NOFILE; 1192 # endif 1193 # endif 1194 } 1195 /* 1196 ** UNAME -- get the UUCP name of this system. 1197 */ 1198 1199 #ifndef HASUNAME 1200 1201 int 1202 uname(name) 1203 struct utsname *name; 1204 { 1205 FILE *file; 1206 char *n; 1207 1208 name->nodename[0] = '\0'; 1209 1210 /* try /etc/whoami -- one line with the node name */ 1211 if ((file = fopen("/etc/whoami", "r")) != NULL) 1212 { 1213 (void) fgets(name->nodename, NODE_LENGTH + 1, file); 1214 (void) fclose(file); 1215 n = strchr(name->nodename, '\n'); 1216 if (n != NULL) 1217 *n = '\0'; 1218 if (name->nodename[0] != '\0') 1219 return (0); 1220 } 1221 1222 /* try /usr/include/whoami.h -- has a #define somewhere */ 1223 if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 1224 { 1225 char buf[MAXLINE]; 1226 1227 while (fgets(buf, MAXLINE, file) != NULL) 1228 if (sscanf(buf, "#define sysname \"%*[^\"]\"", 1229 NODE_LENGTH, name->nodename) > 0) 1230 break; 1231 (void) fclose(file); 1232 if (name->nodename[0] != '\0') 1233 return (0); 1234 } 1235 1236 #ifdef TRUST_POPEN 1237 /* 1238 ** Popen is known to have security holes. 1239 */ 1240 1241 /* try uuname -l to return local name */ 1242 if ((file = popen("uuname -l", "r")) != NULL) 1243 { 1244 (void) fgets(name, NODE_LENGTH + 1, file); 1245 (void) pclose(file); 1246 n = strchr(name, '\n'); 1247 if (n != NULL) 1248 *n = '\0'; 1249 if (name->nodename[0] != '\0') 1250 return (0); 1251 } 1252 #endif 1253 1254 return (-1); 1255 } 1256 #endif /* HASUNAME */ 1257 /* 1258 ** INITGROUPS -- initialize groups 1259 ** 1260 ** Stub implementation for System V style systems 1261 */ 1262 1263 #ifndef HASINITGROUPS 1264 1265 initgroups(name, basegid) 1266 char *name; 1267 int basegid; 1268 { 1269 return 0; 1270 } 1271 1272 #endif 1273 /* 1274 ** SETSID -- set session id (for non-POSIX systems) 1275 */ 1276 1277 #ifndef HASSETSID 1278 1279 pid_t 1280 setsid __P ((void)) 1281 { 1282 #ifdef TIOCNOTTY 1283 int fd; 1284 1285 fd = open("/dev/tty", 2); 1286 if (fd >= 0) 1287 { 1288 (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 1289 (void) close(fd); 1290 } 1291 #endif /* TIOCNOTTY */ 1292 # ifdef SYS5SETPGRP 1293 return setpgrp(); 1294 # else 1295 return setpgid(0, getpid()); 1296 # endif 1297 } 1298 1299 #endif 1300 /* 1301 ** DGUX_INET_ADDR -- inet_addr for DG/UX 1302 ** 1303 ** Data General DG/UX version of inet_addr returns a struct in_addr 1304 ** instead of a long. This patches things. 1305 */ 1306 1307 #ifdef DGUX 1308 1309 #undef inet_addr 1310 1311 long 1312 dgux_inet_addr(host) 1313 char *host; 1314 { 1315 struct in_addr haddr; 1316 1317 haddr = inet_addr(host); 1318 return haddr.s_addr; 1319 } 1320 1321 #endif 1322 /* 1323 ** GETOPT -- for old systems or systems with bogus implementations 1324 */ 1325 1326 #ifdef NEEDGETOPT 1327 1328 /* 1329 * Copyright (c) 1985 Regents of the University of California. 1330 * All rights reserved. The Berkeley software License Agreement 1331 * specifies the terms and conditions for redistribution. 1332 */ 1333 1334 1335 /* 1336 ** this version hacked to add `atend' flag to allow state machine 1337 ** to reset if invoked by the program to scan args for a 2nd time 1338 */ 1339 1340 #if defined(LIBC_SCCS) && !defined(lint) 1341 static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 1342 #endif /* LIBC_SCCS and not lint */ 1343 1344 #include <stdio.h> 1345 1346 /* 1347 * get option letter from argument vector 1348 */ 1349 #ifdef _CONVEX_SOURCE 1350 extern int optind, opterr; 1351 #else 1352 int opterr = 1; /* if error message should be printed */ 1353 int optind = 1; /* index into parent argv vector */ 1354 #endif 1355 int optopt; /* character checked for validity */ 1356 char *optarg; /* argument associated with option */ 1357 1358 #define BADCH (int)'?' 1359 #define EMSG "" 1360 #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 1361 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 1362 1363 getopt(nargc,nargv,ostr) 1364 int nargc; 1365 char *const *nargv; 1366 const char *ostr; 1367 { 1368 static char *place = EMSG; /* option letter processing */ 1369 static char atend = 0; 1370 register char *oli; /* option letter list index */ 1371 1372 if (atend) { 1373 atend = 0; 1374 place = EMSG; 1375 } 1376 if(!*place) { /* update scanning pointer */ 1377 if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 1378 atend++; 1379 return(EOF); 1380 } 1381 if (*place == '-') { /* found "--" */ 1382 ++optind; 1383 atend++; 1384 return(EOF); 1385 } 1386 } /* option letter okay? */ 1387 if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 1388 if (!*place) ++optind; 1389 tell(": illegal option -- "); 1390 } 1391 if (*++oli != ':') { /* don't need argument */ 1392 optarg = NULL; 1393 if (!*place) ++optind; 1394 } 1395 else { /* need an argument */ 1396 if (*place) optarg = place; /* no white space */ 1397 else if (nargc <= ++optind) { /* no arg */ 1398 place = EMSG; 1399 tell(": option requires an argument -- "); 1400 } 1401 else optarg = nargv[optind]; /* white space */ 1402 place = EMSG; 1403 ++optind; 1404 } 1405 return(optopt); /* dump back option letter */ 1406 } 1407 1408 #endif 1409 /* 1410 ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 1411 */ 1412 1413 #ifdef NEEDVPRINTF 1414 1415 #define MAXARG 16 1416 1417 vfprintf(fp, fmt, ap) 1418 FILE * fp; 1419 char * fmt; 1420 char ** ap; 1421 { 1422 char * bp[MAXARG]; 1423 int i = 0; 1424 1425 while (*ap && i < MAXARG) 1426 bp[i++] = *ap++; 1427 fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 1428 bp[4], bp[5], bp[6], bp[7], 1429 bp[8], bp[9], bp[10], bp[11], 1430 bp[12], bp[13], bp[14], bp[15]); 1431 } 1432 1433 vsprintf(s, fmt, ap) 1434 char * s; 1435 char * fmt; 1436 char ** ap; 1437 { 1438 char * bp[MAXARG]; 1439 int i = 0; 1440 1441 while (*ap && i < MAXARG) 1442 bp[i++] = *ap++; 1443 sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 1444 bp[4], bp[5], bp[6], bp[7], 1445 bp[8], bp[9], bp[10], bp[11], 1446 bp[12], bp[13], bp[14], bp[15]); 1447 } 1448 1449 #endif 1450 /* 1451 ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 1452 ** 1453 ** Parameters: 1454 ** shell -- the user's shell from /etc/passwd 1455 ** 1456 ** Returns: 1457 ** TRUE -- if it is ok to use this for unrestricted access. 1458 ** FALSE -- if the shell is restricted. 1459 */ 1460 1461 #if !HASGETUSERSHELL 1462 1463 # ifndef _PATH_SHELLS 1464 # define _PATH_SHELLS "/etc/shells" 1465 # endif 1466 1467 char *DefaultUserShells[] = 1468 { 1469 "/bin/sh", 1470 "/usr/bin/sh", 1471 "/bin/csh", 1472 "/usr/bin/csh", 1473 #ifdef __hpux 1474 "/bin/rsh", 1475 "/bin/ksh", 1476 "/bin/rksh", 1477 "/bin/pam", 1478 "/usr/bin/keysh", 1479 "/bin/posix/sh", 1480 #endif 1481 NULL 1482 }; 1483 1484 #endif 1485 1486 #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 1487 1488 bool 1489 usershellok(shell) 1490 char *shell; 1491 { 1492 #if HASGETUSERSHELL 1493 register char *p; 1494 extern char *getusershell(); 1495 1496 setusershell(); 1497 while ((p = getusershell()) != NULL) 1498 if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 1499 break; 1500 endusershell(); 1501 return p != NULL; 1502 #else 1503 register FILE *shellf; 1504 char buf[MAXLINE]; 1505 1506 shellf = fopen(_PATH_SHELLS, "r"); 1507 if (shellf == NULL) 1508 { 1509 /* no /etc/shells; see if it is one of the std shells */ 1510 char **d; 1511 1512 for (d = DefaultUserShells; *d != NULL; d++) 1513 { 1514 if (strcmp(shell, *d) == 0) 1515 return TRUE; 1516 } 1517 return FALSE; 1518 } 1519 1520 while (fgets(buf, sizeof buf, shellf) != NULL) 1521 { 1522 register char *p, *q; 1523 1524 p = buf; 1525 while (*p != '\0' && *p != '#' && *p != '/') 1526 p++; 1527 if (*p == '#' || *p == '\0') 1528 continue; 1529 q = p; 1530 while (*p != '\0' && *p != '#' && !isspace(*p)) 1531 p++; 1532 *p = '\0'; 1533 if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 1534 { 1535 fclose(shellf); 1536 return TRUE; 1537 } 1538 } 1539 fclose(shellf); 1540 return FALSE; 1541 #endif 1542 } 1543 /* 1544 ** FREESPACE -- see how much free space is on the queue filesystem 1545 ** 1546 ** Only implemented if you have statfs. 1547 ** 1548 ** Parameters: 1549 ** dir -- the directory in question. 1550 ** bsize -- a variable into which the filesystem 1551 ** block size is stored. 1552 ** 1553 ** Returns: 1554 ** The number of bytes free on the queue filesystem. 1555 ** -1 if the statfs call fails. 1556 ** 1557 ** Side effects: 1558 ** Puts the filesystem block size into bsize. 1559 */ 1560 1561 /* statfs types */ 1562 #define SFS_NONE 0 /* no statfs implementation */ 1563 #define SFS_USTAT 1 /* use ustat */ 1564 #define SFS_4ARGS 2 /* use four-argument statfs call */ 1565 #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 1566 #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 1567 #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 1568 1569 #ifndef SFS_TYPE 1570 # define SFS_TYPE SFS_NONE 1571 #endif 1572 1573 #if SFS_TYPE == SFS_USTAT 1574 # include <ustat.h> 1575 #endif 1576 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 1577 # include <sys/statfs.h> 1578 #endif 1579 #if SFS_TYPE == SFS_VFS 1580 # include <sys/vfs.h> 1581 #endif 1582 #if SFS_TYPE == SFS_MOUNT 1583 # include <sys/mount.h> 1584 #endif 1585 1586 long 1587 freespace(dir, bsize) 1588 char *dir; 1589 long *bsize; 1590 { 1591 #if SFS_TYPE != SFS_NONE 1592 # if SFS_TYPE == SFS_USTAT 1593 struct ustat fs; 1594 struct stat statbuf; 1595 # define FSBLOCKSIZE DEV_BSIZE 1596 # define f_bavail f_tfree 1597 # else 1598 # if defined(ultrix) 1599 struct fs_data fs; 1600 # define f_bavail fd_bfreen 1601 # define FSBLOCKSIZE fs.fd_bsize 1602 # else 1603 struct statfs fs; 1604 # define FSBLOCKSIZE fs.f_bsize 1605 # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 1606 # define f_bavail f_bfree 1607 # endif 1608 # endif 1609 # endif 1610 extern int errno; 1611 1612 # if SFS_TYPE == SFS_USTAT 1613 if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 1614 # else 1615 # if SFS_TYPE == SFS_4ARGS 1616 if (statfs(dir, &fs, sizeof fs, 0) == 0) 1617 # else 1618 # if defined(ultrix) 1619 if (statfs(dir, &fs) > 0) 1620 # else 1621 if (statfs(dir, &fs) == 0) 1622 # endif 1623 # endif 1624 # endif 1625 { 1626 if (bsize != NULL) 1627 *bsize = FSBLOCKSIZE; 1628 return (fs.f_bavail); 1629 } 1630 #endif 1631 return (-1); 1632 } 1633 /* 1634 ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 1635 ** 1636 ** Only implemented if you have statfs. 1637 ** 1638 ** Parameters: 1639 ** msize -- the size to check against. If zero, we don't yet 1640 ** know how big the message will be, so just check for 1641 ** a "reasonable" amount. 1642 ** 1643 ** Returns: 1644 ** TRUE if there is enough space. 1645 ** FALSE otherwise. 1646 */ 1647 1648 bool 1649 enoughspace(msize) 1650 long msize; 1651 { 1652 long bfree, bsize; 1653 1654 if (MinBlocksFree <= 0 && msize <= 0) 1655 { 1656 if (tTd(4, 80)) 1657 printf("enoughspace: no threshold\n"); 1658 return TRUE; 1659 } 1660 1661 if ((bfree = freespace(QueueDir, &bsize)) >= 0) 1662 { 1663 if (tTd(4, 80)) 1664 printf("enoughspace: bavail=%ld, need=%ld\n", 1665 bfree, msize); 1666 1667 /* convert msize to block count */ 1668 msize = msize / bsize + 1; 1669 if (MinBlocksFree >= 0) 1670 msize += MinBlocksFree; 1671 1672 if (bfree < msize) 1673 { 1674 #ifdef LOG 1675 if (LogLevel > 0) 1676 syslog(LOG_ALERT, 1677 "%s: low on space (have %ld, %s needs %ld in %s)", 1678 CurEnv->e_id, bfree, 1679 CurHostName, msize, QueueDir); 1680 #endif 1681 return FALSE; 1682 } 1683 } 1684 else if (tTd(4, 80)) 1685 printf("enoughspace failure: min=%ld, need=%ld: %s\n", 1686 MinBlocksFree, msize, errstring(errno)); 1687 return TRUE; 1688 } 1689 /* 1690 ** TRANSIENTERROR -- tell if an error code indicates a transient failure 1691 ** 1692 ** This looks at an errno value and tells if this is likely to 1693 ** go away if retried later. 1694 ** 1695 ** Parameters: 1696 ** err -- the errno code to classify. 1697 ** 1698 ** Returns: 1699 ** TRUE if this is probably transient. 1700 ** FALSE otherwise. 1701 */ 1702 1703 bool 1704 transienterror(err) 1705 int err; 1706 { 1707 switch (err) 1708 { 1709 case EIO: /* I/O error */ 1710 case ENXIO: /* Device not configured */ 1711 case EAGAIN: /* Resource temporarily unavailable */ 1712 case ENOMEM: /* Cannot allocate memory */ 1713 case ENODEV: /* Operation not supported by device */ 1714 case ENFILE: /* Too many open files in system */ 1715 case EMFILE: /* Too many open files */ 1716 case ENOSPC: /* No space left on device */ 1717 #ifdef ETIMEDOUT 1718 case ETIMEDOUT: /* Connection timed out */ 1719 #endif 1720 #ifdef ESTALE 1721 case ESTALE: /* Stale NFS file handle */ 1722 #endif 1723 #ifdef ENETDOWN 1724 case ENETDOWN: /* Network is down */ 1725 #endif 1726 #ifdef ENETUNREACH 1727 case ENETUNREACH: /* Network is unreachable */ 1728 #endif 1729 #ifdef ENETRESET 1730 case ENETRESET: /* Network dropped connection on reset */ 1731 #endif 1732 #ifdef ECONNABORTED 1733 case ECONNABORTED: /* Software caused connection abort */ 1734 #endif 1735 #ifdef ECONNRESET 1736 case ECONNRESET: /* Connection reset by peer */ 1737 #endif 1738 #ifdef ENOBUFS 1739 case ENOBUFS: /* No buffer space available */ 1740 #endif 1741 #ifdef ESHUTDOWN 1742 case ESHUTDOWN: /* Can't send after socket shutdown */ 1743 #endif 1744 #ifdef ECONNREFUSED 1745 case ECONNREFUSED: /* Connection refused */ 1746 #endif 1747 #ifdef EHOSTDOWN 1748 case EHOSTDOWN: /* Host is down */ 1749 #endif 1750 #ifdef EHOSTUNREACH 1751 case EHOSTUNREACH: /* No route to host */ 1752 #endif 1753 #ifdef EDQUOT 1754 case EDQUOT: /* Disc quota exceeded */ 1755 #endif 1756 #ifdef EPROCLIM 1757 case EPROCLIM: /* Too many processes */ 1758 #endif 1759 #ifdef EUSERS 1760 case EUSERS: /* Too many users */ 1761 #endif 1762 #ifdef EDEADLK 1763 case EDEADLK: /* Resource deadlock avoided */ 1764 #endif 1765 #ifdef EISCONN 1766 case EISCONN: /* Socket already connected */ 1767 #endif 1768 #ifdef EINPROGRESS 1769 case EINPROGRESS: /* Operation now in progress */ 1770 #endif 1771 #ifdef EALREADY 1772 case EALREADY: /* Operation already in progress */ 1773 #endif 1774 #ifdef EADDRINUSE 1775 case EADDRINUSE: /* Address already in use */ 1776 #endif 1777 #ifdef EADDRNOTAVAIL 1778 case EADDRNOTAVAIL: /* Can't assign requested address */ 1779 #endif 1780 #ifdef ETXTBSY 1781 case ETXTBSY: /* (Apollo) file locked */ 1782 #endif 1783 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 1784 case ENOSR: /* Out of streams resources */ 1785 #endif 1786 return TRUE; 1787 } 1788 1789 /* nope, must be permanent */ 1790 return FALSE; 1791 } 1792 /* 1793 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 1794 ** 1795 ** Parameters: 1796 ** fd -- the file descriptor of the file. 1797 ** filename -- the file name (for error messages). 1798 ** ext -- the filename extension. 1799 ** type -- type of the lock. Bits can be: 1800 ** LOCK_EX -- exclusive lock. 1801 ** LOCK_NB -- non-blocking. 1802 ** 1803 ** Returns: 1804 ** TRUE if the lock was acquired. 1805 ** FALSE otherwise. 1806 */ 1807 1808 bool 1809 lockfile(fd, filename, ext, type) 1810 int fd; 1811 char *filename; 1812 char *ext; 1813 int type; 1814 { 1815 # if !HASFLOCK 1816 int action; 1817 struct flock lfd; 1818 1819 if (ext == NULL) 1820 ext = ""; 1821 1822 bzero(&lfd, sizeof lfd); 1823 if (bitset(LOCK_UN, type)) 1824 lfd.l_type = F_UNLCK; 1825 else if (bitset(LOCK_EX, type)) 1826 lfd.l_type = F_WRLCK; 1827 else 1828 lfd.l_type = F_RDLCK; 1829 1830 if (bitset(LOCK_NB, type)) 1831 action = F_SETLK; 1832 else 1833 action = F_SETLKW; 1834 1835 if (tTd(55, 60)) 1836 printf("lockfile(%s%s, action=%d, type=%d): ", 1837 filename, ext, action, lfd.l_type); 1838 1839 if (fcntl(fd, action, &lfd) >= 0) 1840 { 1841 if (tTd(55, 60)) 1842 printf("SUCCESS\n"); 1843 return TRUE; 1844 } 1845 1846 if (tTd(55, 60)) 1847 printf("(%s) ", errstring(errno)); 1848 1849 /* 1850 ** On SunOS, if you are testing using -oQ/tmp/mqueue or 1851 ** -oA/tmp/aliases or anything like that, and /tmp is mounted 1852 ** as type "tmp" (that is, served from swap space), the 1853 ** previous fcntl will fail with "Invalid argument" errors. 1854 ** Since this is fairly common during testing, we will assume 1855 ** that this indicates that the lock is successfully grabbed. 1856 */ 1857 1858 if (errno == EINVAL) 1859 { 1860 if (tTd(55, 60)) 1861 printf("SUCCESS\n"); 1862 return TRUE; 1863 } 1864 1865 if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 1866 { 1867 int omode = -1; 1868 # ifdef F_GETFL 1869 int oerrno = errno; 1870 1871 (void) fcntl(fd, F_GETFL, &omode); 1872 errno = oerrno; 1873 # endif 1874 syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1875 filename, ext, fd, type, omode, geteuid()); 1876 } 1877 # else 1878 if (ext == NULL) 1879 ext = ""; 1880 1881 if (tTd(55, 60)) 1882 printf("lockfile(%s%s, type=%o): ", filename, ext, type); 1883 1884 if (flock(fd, type) >= 0) 1885 { 1886 if (tTd(55, 60)) 1887 printf("SUCCESS\n"); 1888 return TRUE; 1889 } 1890 1891 if (tTd(55, 60)) 1892 printf("(%s) ", errstring(errno)); 1893 1894 if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 1895 { 1896 int omode = -1; 1897 # ifdef F_GETFL 1898 int oerrno = errno; 1899 1900 (void) fcntl(fd, F_GETFL, &omode); 1901 errno = oerrno; 1902 # endif 1903 syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1904 filename, ext, fd, type, omode, geteuid()); 1905 } 1906 # endif 1907 if (tTd(55, 60)) 1908 printf("FAIL\n"); 1909 return FALSE; 1910 } 1911 /* 1912 ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 1913 ** 1914 ** Parameters: 1915 ** fd -- the file descriptor to check. 1916 ** 1917 ** Returns: 1918 ** TRUE -- if only root can chown the file to an arbitrary 1919 ** user. 1920 ** FALSE -- if an arbitrary user can give away a file. 1921 */ 1922 1923 bool 1924 chownsafe(fd) 1925 int fd; 1926 { 1927 #ifdef __hpux 1928 char *s; 1929 int tfd; 1930 uid_t o_uid, o_euid; 1931 gid_t o_gid, o_egid; 1932 bool rval; 1933 struct stat stbuf; 1934 1935 o_uid = getuid(); 1936 o_euid = geteuid(); 1937 o_gid = getgid(); 1938 o_egid = getegid(); 1939 fstat(fd, &stbuf); 1940 setresuid(stbuf.st_uid, stbuf.st_uid, -1); 1941 setresgid(stbuf.st_gid, stbuf.st_gid, -1); 1942 s = tmpnam(NULL); 1943 tfd = open(s, O_RDONLY|O_CREAT, 0600); 1944 rval = fchown(tfd, DefUid, DefGid) != 0; 1945 close(tfd); 1946 unlink(s); 1947 setreuid(o_uid, o_euid); 1948 setresgid(o_gid, o_egid, -1); 1949 return rval; 1950 #else 1951 # ifdef _POSIX_CHOWN_RESTRICTED 1952 # if _POSIX_CHOWN_RESTRICTED == -1 1953 return FALSE; 1954 # else 1955 return TRUE; 1956 # endif 1957 # else 1958 # ifdef _PC_CHOWN_RESTRICTED 1959 return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 1960 # else 1961 # ifdef BSD 1962 return TRUE; 1963 # else 1964 return FALSE; 1965 # endif 1966 # endif 1967 # endif 1968 #endif 1969 } 1970 /* 1971 ** GETCFNAME -- return the name of the .cf file. 1972 ** 1973 ** Some systems (e.g., NeXT) determine this dynamically. 1974 */ 1975 1976 char * 1977 getcfname() 1978 { 1979 if (ConfFile != NULL) 1980 return ConfFile; 1981 #ifdef NETINFO 1982 { 1983 extern char *ni_propval(); 1984 char *cflocation; 1985 1986 cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 1987 if (cflocation != NULL) 1988 return cflocation; 1989 } 1990 #endif 1991 return _PATH_SENDMAILCF; 1992 } 1993 /* 1994 ** SETVENDOR -- process vendor code from V configuration line 1995 ** 1996 ** Parameters: 1997 ** vendor -- string representation of vendor. 1998 ** 1999 ** Returns: 2000 ** TRUE -- if ok. 2001 ** FALSE -- if vendor code could not be processed. 2002 ** 2003 ** Side Effects: 2004 ** It is reasonable to set mode flags here to tweak 2005 ** processing in other parts of the code if necessary. 2006 ** For example, if you are a vendor that uses $%y to 2007 ** indicate YP lookups, you could enable that here. 2008 */ 2009 2010 bool 2011 setvendor(vendor) 2012 char *vendor; 2013 { 2014 if (strcasecmp(vendor, "Berkeley") == 0) 2015 return TRUE; 2016 2017 /* add vendor extensions here */ 2018 2019 return FALSE; 2020 } 2021 /* 2022 ** STRTOL -- convert string to long integer 2023 ** 2024 ** For systems that don't have it in the C library. 2025 ** 2026 ** This is taken verbatim from the 4.4-Lite C library. 2027 */ 2028 2029 #ifdef NEEDSTRTOL 2030 2031 #if defined(LIBC_SCCS) && !defined(lint) 2032 static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 2033 #endif /* LIBC_SCCS and not lint */ 2034 2035 #include <limits.h> 2036 2037 /* 2038 * Convert a string to a long integer. 2039 * 2040 * Ignores `locale' stuff. Assumes that the upper and lower case 2041 * alphabets and digits are each contiguous. 2042 */ 2043 2044 long 2045 strtol(nptr, endptr, base) 2046 const char *nptr; 2047 char **endptr; 2048 register int base; 2049 { 2050 register const char *s = nptr; 2051 register unsigned long acc; 2052 register int c; 2053 register unsigned long cutoff; 2054 register int neg = 0, any, cutlim; 2055 2056 /* 2057 * Skip white space and pick up leading +/- sign if any. 2058 * If base is 0, allow 0x for hex and 0 for octal, else 2059 * assume decimal; if base is already 16, allow 0x. 2060 */ 2061 do { 2062 c = *s++; 2063 } while (isspace(c)); 2064 if (c == '-') { 2065 neg = 1; 2066 c = *s++; 2067 } else if (c == '+') 2068 c = *s++; 2069 if ((base == 0 || base == 16) && 2070 c == '0' && (*s == 'x' || *s == 'X')) { 2071 c = s[1]; 2072 s += 2; 2073 base = 16; 2074 } 2075 if (base == 0) 2076 base = c == '0' ? 8 : 10; 2077 2078 /* 2079 * Compute the cutoff value between legal numbers and illegal 2080 * numbers. That is the largest legal value, divided by the 2081 * base. An input number that is greater than this value, if 2082 * followed by a legal input character, is too big. One that 2083 * is equal to this value may be valid or not; the limit 2084 * between valid and invalid numbers is then based on the last 2085 * digit. For instance, if the range for longs is 2086 * [-2147483648..2147483647] and the input base is 10, 2087 * cutoff will be set to 214748364 and cutlim to either 2088 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 2089 * a value > 214748364, or equal but the next digit is > 7 (or 8), 2090 * the number is too big, and we will return a range error. 2091 * 2092 * Set any if any `digits' consumed; make it negative to indicate 2093 * overflow. 2094 */ 2095 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 2096 cutlim = cutoff % (unsigned long)base; 2097 cutoff /= (unsigned long)base; 2098 for (acc = 0, any = 0;; c = *s++) { 2099 if (isdigit(c)) 2100 c -= '0'; 2101 else if (isalpha(c)) 2102 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 2103 else 2104 break; 2105 if (c >= base) 2106 break; 2107 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 2108 any = -1; 2109 else { 2110 any = 1; 2111 acc *= base; 2112 acc += c; 2113 } 2114 } 2115 if (any < 0) { 2116 acc = neg ? LONG_MIN : LONG_MAX; 2117 errno = ERANGE; 2118 } else if (neg) 2119 acc = -acc; 2120 if (endptr != 0) 2121 *endptr = (char *)(any ? s - 1 : nptr); 2122 return (acc); 2123 } 2124 2125 #endif 2126 /* 2127 ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 2128 ** 2129 ** Solaris versions prior through 2.3 don't properly deliver a 2130 ** canonical h_name field. This tries to work around it. 2131 */ 2132 2133 #ifdef SOLARIS 2134 2135 struct hostent * 2136 solaris_gethostbyname(name) 2137 const char *name; 2138 { 2139 # ifdef SOLARIS_2_3 2140 static struct hostent hp; 2141 static char buf[1000]; 2142 extern struct hostent *_switch_gethostbyname_r(); 2143 2144 return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 2145 # else 2146 extern struct hostent *__switch_gethostbyname(); 2147 2148 return __switch_gethostbyname(name); 2149 # endif 2150 } 2151 2152 struct hostent * 2153 solaris_gethostbyaddr(addr, len, type) 2154 const char *addr; 2155 int len; 2156 int type; 2157 { 2158 # ifdef SOLARIS_2_3 2159 static struct hostent hp; 2160 static char buf[1000]; 2161 extern struct hostent *_switch_gethostbyaddr_r(); 2162 2163 return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 2164 # else 2165 extern struct hostent *__switch_gethostbyaddr(); 2166 2167 return __switch_gethostbyaddr(addr, len, type); 2168 # endif 2169 } 2170 2171 #endif 2172 /* 2173 ** NI_PROPVAL -- netinfo property value lookup routine 2174 ** 2175 ** Parameters: 2176 ** directory -- the Netinfo directory name. 2177 ** propname -- the Netinfo property name. 2178 ** 2179 ** Returns: 2180 ** NULL -- if: 2181 ** 1. the directory is not found 2182 ** 2. the property name is not found 2183 ** 3. the property contains multiple values 2184 ** 4. some error occured 2185 ** else -- the location of the config file. 2186 ** 2187 ** Notes: 2188 ** Caller should free the return value of ni_proval 2189 */ 2190 2191 #ifdef NETINFO 2192 2193 # include <netinfo/ni.h> 2194 2195 # define LOCAL_NETINFO_DOMAIN "." 2196 # define PARENT_NETINFO_DOMAIN ".." 2197 # define MAX_NI_LEVELS 256 2198 2199 char * 2200 ni_propval(directory, propname) 2201 char *directory; 2202 char *propname; 2203 { 2204 char *propval = NULL; 2205 int i; 2206 void *ni = NULL; 2207 void *lastni = NULL; 2208 ni_status nis; 2209 ni_id nid; 2210 ni_namelist ninl; 2211 2212 /* 2213 ** If the passed directory and property name are found 2214 ** in one of netinfo domains we need to search (starting 2215 ** from the local domain moving all the way back to the 2216 ** root domain) set propval to the property's value 2217 ** and return it. 2218 */ 2219 2220 for (i = 0; i < MAX_NI_LEVELS; ++i) 2221 { 2222 if (i == 0) 2223 { 2224 nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 2225 } 2226 else 2227 { 2228 if (lastni != NULL) 2229 ni_free(lastni); 2230 lastni = ni; 2231 nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 2232 } 2233 2234 /* 2235 ** Don't bother if we didn't get a handle on a 2236 ** proper domain. This is not necessarily an error. 2237 ** We would get a positive ni_status if, for instance 2238 ** we never found the directory or property and tried 2239 ** to open the parent of the root domain! 2240 */ 2241 2242 if (nis != 0) 2243 break; 2244 2245 /* 2246 ** Find the path to the server information. 2247 */ 2248 2249 if (ni_pathsearch(ni, &nid, directory) != 0) 2250 continue; 2251 2252 /* 2253 ** Find "host" information. 2254 */ 2255 2256 if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 2257 continue; 2258 2259 /* 2260 ** If there's only one name in 2261 ** the list, assume we've got 2262 ** what we want. 2263 */ 2264 2265 if (ninl.ni_namelist_len == 1) 2266 { 2267 propval = ni_name_dup(ninl.ni_namelist_val[0]); 2268 break; 2269 } 2270 } 2271 2272 /* 2273 ** Clean up. 2274 */ 2275 2276 if (ni != NULL) 2277 ni_free(ni); 2278 if (lastni != NULL && ni != lastni) 2279 ni_free(lastni); 2280 2281 return propval; 2282 } 2283 2284 #endif /* NETINFO */ 2285 /* 2286 ** HARD_SYSLOG -- call syslog repeatedly until it works 2287 ** 2288 ** Needed on HP-UX, which apparently doesn't guarantee that 2289 ** syslog succeeds during interrupt handlers. 2290 */ 2291 2292 #ifdef __hpux 2293 2294 # define MAXSYSLOGTRIES 100 2295 # undef syslog 2296 2297 # ifdef __STDC__ 2298 hard_syslog(int pri, char *msg, ...) 2299 # else 2300 hard_syslog(pri, msg, va_alist) 2301 int pri; 2302 char *msg; 2303 va_dcl 2304 # endif 2305 { 2306 int i; 2307 char buf[SYSLOG_BUFSIZE * 2]; 2308 VA_LOCAL_DECL; 2309 2310 VA_START(msg); 2311 vsprintf(buf, msg, ap); 2312 VA_END; 2313 2314 for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 2315 continue; 2316 } 2317 2318 #endif 2319