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