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.1.1 (Berkeley) 02/28/95"; 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 if (shell == NULL || shell[0] == '\0') 1521 return TRUE; 1522 1523 setusershell(); 1524 while ((p = getusershell()) != NULL) 1525 if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 1526 break; 1527 endusershell(); 1528 return p != NULL; 1529 #else 1530 register FILE *shellf; 1531 char buf[MAXLINE]; 1532 1533 if (shell == NULL || shell[0] == '\0') 1534 return TRUE; 1535 1536 shellf = fopen(_PATH_SHELLS, "r"); 1537 if (shellf == NULL) 1538 { 1539 /* no /etc/shells; see if it is one of the std shells */ 1540 char **d; 1541 1542 for (d = DefaultUserShells; *d != NULL; d++) 1543 { 1544 if (strcmp(shell, *d) == 0) 1545 return TRUE; 1546 } 1547 return FALSE; 1548 } 1549 1550 while (fgets(buf, sizeof buf, shellf) != NULL) 1551 { 1552 register char *p, *q; 1553 1554 p = buf; 1555 while (*p != '\0' && *p != '#' && *p != '/') 1556 p++; 1557 if (*p == '#' || *p == '\0') 1558 continue; 1559 q = p; 1560 while (*p != '\0' && *p != '#' && !isspace(*p)) 1561 p++; 1562 *p = '\0'; 1563 if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 1564 { 1565 fclose(shellf); 1566 return TRUE; 1567 } 1568 } 1569 fclose(shellf); 1570 return FALSE; 1571 #endif 1572 } 1573 /* 1574 ** FREESPACE -- see how much free space is on the queue filesystem 1575 ** 1576 ** Only implemented if you have statfs. 1577 ** 1578 ** Parameters: 1579 ** dir -- the directory in question. 1580 ** bsize -- a variable into which the filesystem 1581 ** block size is stored. 1582 ** 1583 ** Returns: 1584 ** The number of bytes free on the queue filesystem. 1585 ** -1 if the statfs call fails. 1586 ** 1587 ** Side effects: 1588 ** Puts the filesystem block size into bsize. 1589 */ 1590 1591 /* statfs types */ 1592 #define SFS_NONE 0 /* no statfs implementation */ 1593 #define SFS_USTAT 1 /* use ustat */ 1594 #define SFS_4ARGS 2 /* use four-argument statfs call */ 1595 #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 1596 #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 1597 #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 1598 #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 1599 1600 #ifndef SFS_TYPE 1601 # define SFS_TYPE SFS_NONE 1602 #endif 1603 1604 #if SFS_TYPE == SFS_USTAT 1605 # include <ustat.h> 1606 #endif 1607 #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 1608 # include <sys/statfs.h> 1609 #endif 1610 #if SFS_TYPE == SFS_VFS 1611 # include <sys/vfs.h> 1612 #endif 1613 #if SFS_TYPE == SFS_MOUNT 1614 # include <sys/mount.h> 1615 #endif 1616 #if SFS_TYPE == SFS_STATVFS 1617 # include <sys/statvfs.h> 1618 #endif 1619 1620 long 1621 freespace(dir, bsize) 1622 char *dir; 1623 long *bsize; 1624 { 1625 #if SFS_TYPE != SFS_NONE 1626 # if SFS_TYPE == SFS_USTAT 1627 struct ustat fs; 1628 struct stat statbuf; 1629 # define FSBLOCKSIZE DEV_BSIZE 1630 # define f_bavail f_tfree 1631 # else 1632 # if defined(ultrix) 1633 struct fs_data fs; 1634 # define f_bavail fd_bfreen 1635 # define FSBLOCKSIZE fs.fd_bsize 1636 # else 1637 # if SFS_TYPE == SFS_STATVFS 1638 struct statvfs fs; 1639 # define FSBLOCKSIZE fs.f_bsize 1640 # else 1641 struct statfs fs; 1642 # define FSBLOCKSIZE fs.f_bsize 1643 # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 1644 # define f_bavail f_bfree 1645 # endif 1646 # endif 1647 # endif 1648 # endif 1649 extern int errno; 1650 1651 # if SFS_TYPE == SFS_USTAT 1652 if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 1653 # else 1654 # if SFS_TYPE == SFS_4ARGS 1655 if (statfs(dir, &fs, sizeof fs, 0) == 0) 1656 # else 1657 # if defined(ultrix) 1658 if (statfs(dir, &fs) > 0) 1659 # else 1660 if (statfs(dir, &fs) == 0) 1661 # endif 1662 # endif 1663 # endif 1664 { 1665 if (bsize != NULL) 1666 *bsize = FSBLOCKSIZE; 1667 return (fs.f_bavail); 1668 } 1669 #endif 1670 return (-1); 1671 } 1672 /* 1673 ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 1674 ** 1675 ** Only implemented if you have statfs. 1676 ** 1677 ** Parameters: 1678 ** msize -- the size to check against. If zero, we don't yet 1679 ** know how big the message will be, so just check for 1680 ** a "reasonable" amount. 1681 ** 1682 ** Returns: 1683 ** TRUE if there is enough space. 1684 ** FALSE otherwise. 1685 */ 1686 1687 bool 1688 enoughspace(msize) 1689 long msize; 1690 { 1691 long bfree, bsize; 1692 1693 if (MinBlocksFree <= 0 && msize <= 0) 1694 { 1695 if (tTd(4, 80)) 1696 printf("enoughspace: no threshold\n"); 1697 return TRUE; 1698 } 1699 1700 if ((bfree = freespace(QueueDir, &bsize)) >= 0) 1701 { 1702 if (tTd(4, 80)) 1703 printf("enoughspace: bavail=%ld, need=%ld\n", 1704 bfree, msize); 1705 1706 /* convert msize to block count */ 1707 msize = msize / bsize + 1; 1708 if (MinBlocksFree >= 0) 1709 msize += MinBlocksFree; 1710 1711 if (bfree < msize) 1712 { 1713 #ifdef LOG 1714 if (LogLevel > 0) 1715 syslog(LOG_ALERT, 1716 "%s: low on space (have %ld, %s needs %ld in %s)", 1717 CurEnv->e_id, bfree, 1718 CurHostName, msize, QueueDir); 1719 #endif 1720 return FALSE; 1721 } 1722 } 1723 else if (tTd(4, 80)) 1724 printf("enoughspace failure: min=%ld, need=%ld: %s\n", 1725 MinBlocksFree, msize, errstring(errno)); 1726 return TRUE; 1727 } 1728 /* 1729 ** TRANSIENTERROR -- tell if an error code indicates a transient failure 1730 ** 1731 ** This looks at an errno value and tells if this is likely to 1732 ** go away if retried later. 1733 ** 1734 ** Parameters: 1735 ** err -- the errno code to classify. 1736 ** 1737 ** Returns: 1738 ** TRUE if this is probably transient. 1739 ** FALSE otherwise. 1740 */ 1741 1742 bool 1743 transienterror(err) 1744 int err; 1745 { 1746 switch (err) 1747 { 1748 case EIO: /* I/O error */ 1749 case ENXIO: /* Device not configured */ 1750 case EAGAIN: /* Resource temporarily unavailable */ 1751 case ENOMEM: /* Cannot allocate memory */ 1752 case ENODEV: /* Operation not supported by device */ 1753 case ENFILE: /* Too many open files in system */ 1754 case EMFILE: /* Too many open files */ 1755 case ENOSPC: /* No space left on device */ 1756 #ifdef ETIMEDOUT 1757 case ETIMEDOUT: /* Connection timed out */ 1758 #endif 1759 #ifdef ESTALE 1760 case ESTALE: /* Stale NFS file handle */ 1761 #endif 1762 #ifdef ENETDOWN 1763 case ENETDOWN: /* Network is down */ 1764 #endif 1765 #ifdef ENETUNREACH 1766 case ENETUNREACH: /* Network is unreachable */ 1767 #endif 1768 #ifdef ENETRESET 1769 case ENETRESET: /* Network dropped connection on reset */ 1770 #endif 1771 #ifdef ECONNABORTED 1772 case ECONNABORTED: /* Software caused connection abort */ 1773 #endif 1774 #ifdef ECONNRESET 1775 case ECONNRESET: /* Connection reset by peer */ 1776 #endif 1777 #ifdef ENOBUFS 1778 case ENOBUFS: /* No buffer space available */ 1779 #endif 1780 #ifdef ESHUTDOWN 1781 case ESHUTDOWN: /* Can't send after socket shutdown */ 1782 #endif 1783 #ifdef ECONNREFUSED 1784 case ECONNREFUSED: /* Connection refused */ 1785 #endif 1786 #ifdef EHOSTDOWN 1787 case EHOSTDOWN: /* Host is down */ 1788 #endif 1789 #ifdef EHOSTUNREACH 1790 case EHOSTUNREACH: /* No route to host */ 1791 #endif 1792 #ifdef EDQUOT 1793 case EDQUOT: /* Disc quota exceeded */ 1794 #endif 1795 #ifdef EPROCLIM 1796 case EPROCLIM: /* Too many processes */ 1797 #endif 1798 #ifdef EUSERS 1799 case EUSERS: /* Too many users */ 1800 #endif 1801 #ifdef EDEADLK 1802 case EDEADLK: /* Resource deadlock avoided */ 1803 #endif 1804 #ifdef EISCONN 1805 case EISCONN: /* Socket already connected */ 1806 #endif 1807 #ifdef EINPROGRESS 1808 case EINPROGRESS: /* Operation now in progress */ 1809 #endif 1810 #ifdef EALREADY 1811 case EALREADY: /* Operation already in progress */ 1812 #endif 1813 #ifdef EADDRINUSE 1814 case EADDRINUSE: /* Address already in use */ 1815 #endif 1816 #ifdef EADDRNOTAVAIL 1817 case EADDRNOTAVAIL: /* Can't assign requested address */ 1818 #endif 1819 #ifdef ETXTBSY 1820 case ETXTBSY: /* (Apollo) file locked */ 1821 #endif 1822 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 1823 case ENOSR: /* Out of streams resources */ 1824 #endif 1825 return TRUE; 1826 } 1827 1828 /* nope, must be permanent */ 1829 return FALSE; 1830 } 1831 /* 1832 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 1833 ** 1834 ** Parameters: 1835 ** fd -- the file descriptor of the file. 1836 ** filename -- the file name (for error messages). 1837 ** ext -- the filename extension. 1838 ** type -- type of the lock. Bits can be: 1839 ** LOCK_EX -- exclusive lock. 1840 ** LOCK_NB -- non-blocking. 1841 ** 1842 ** Returns: 1843 ** TRUE if the lock was acquired. 1844 ** FALSE otherwise. 1845 */ 1846 1847 bool 1848 lockfile(fd, filename, ext, type) 1849 int fd; 1850 char *filename; 1851 char *ext; 1852 int type; 1853 { 1854 # if !HASFLOCK 1855 int action; 1856 struct flock lfd; 1857 1858 if (ext == NULL) 1859 ext = ""; 1860 1861 bzero(&lfd, sizeof lfd); 1862 if (bitset(LOCK_UN, type)) 1863 lfd.l_type = F_UNLCK; 1864 else if (bitset(LOCK_EX, type)) 1865 lfd.l_type = F_WRLCK; 1866 else 1867 lfd.l_type = F_RDLCK; 1868 1869 if (bitset(LOCK_NB, type)) 1870 action = F_SETLK; 1871 else 1872 action = F_SETLKW; 1873 1874 if (tTd(55, 60)) 1875 printf("lockfile(%s%s, action=%d, type=%d): ", 1876 filename, ext, action, lfd.l_type); 1877 1878 if (fcntl(fd, action, &lfd) >= 0) 1879 { 1880 if (tTd(55, 60)) 1881 printf("SUCCESS\n"); 1882 return TRUE; 1883 } 1884 1885 if (tTd(55, 60)) 1886 printf("(%s) ", errstring(errno)); 1887 1888 /* 1889 ** On SunOS, if you are testing using -oQ/tmp/mqueue or 1890 ** -oA/tmp/aliases or anything like that, and /tmp is mounted 1891 ** as type "tmp" (that is, served from swap space), the 1892 ** previous fcntl will fail with "Invalid argument" errors. 1893 ** Since this is fairly common during testing, we will assume 1894 ** that this indicates that the lock is successfully grabbed. 1895 */ 1896 1897 if (errno == EINVAL) 1898 { 1899 if (tTd(55, 60)) 1900 printf("SUCCESS\n"); 1901 return TRUE; 1902 } 1903 1904 if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 1905 { 1906 int omode = -1; 1907 # ifdef F_GETFL 1908 int oerrno = errno; 1909 1910 (void) fcntl(fd, F_GETFL, &omode); 1911 errno = oerrno; 1912 # endif 1913 syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1914 filename, ext, fd, type, omode, geteuid()); 1915 } 1916 # else 1917 if (ext == NULL) 1918 ext = ""; 1919 1920 if (tTd(55, 60)) 1921 printf("lockfile(%s%s, type=%o): ", filename, ext, type); 1922 1923 if (flock(fd, type) >= 0) 1924 { 1925 if (tTd(55, 60)) 1926 printf("SUCCESS\n"); 1927 return TRUE; 1928 } 1929 1930 if (tTd(55, 60)) 1931 printf("(%s) ", errstring(errno)); 1932 1933 if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 1934 { 1935 int omode = -1; 1936 # ifdef F_GETFL 1937 int oerrno = errno; 1938 1939 (void) fcntl(fd, F_GETFL, &omode); 1940 errno = oerrno; 1941 # endif 1942 syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1943 filename, ext, fd, type, omode, geteuid()); 1944 } 1945 # endif 1946 if (tTd(55, 60)) 1947 printf("FAIL\n"); 1948 return FALSE; 1949 } 1950 /* 1951 ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 1952 ** 1953 ** Parameters: 1954 ** fd -- the file descriptor to check. 1955 ** 1956 ** Returns: 1957 ** TRUE -- if only root can chown the file to an arbitrary 1958 ** user. 1959 ** FALSE -- if an arbitrary user can give away a file. 1960 */ 1961 1962 bool 1963 chownsafe(fd) 1964 int fd; 1965 { 1966 #ifdef __hpux 1967 char *s; 1968 int tfd; 1969 uid_t o_uid, o_euid; 1970 gid_t o_gid, o_egid; 1971 bool rval; 1972 struct stat stbuf; 1973 1974 o_uid = getuid(); 1975 o_euid = geteuid(); 1976 o_gid = getgid(); 1977 o_egid = getegid(); 1978 fstat(fd, &stbuf); 1979 setresuid(stbuf.st_uid, stbuf.st_uid, -1); 1980 setresgid(stbuf.st_gid, stbuf.st_gid, -1); 1981 s = tmpnam(NULL); 1982 tfd = open(s, O_RDONLY|O_CREAT, 0600); 1983 rval = fchown(tfd, DefUid, DefGid) != 0; 1984 close(tfd); 1985 unlink(s); 1986 setreuid(o_uid, o_euid); 1987 setresgid(o_gid, o_egid, -1); 1988 return rval; 1989 #else 1990 # ifdef _POSIX_CHOWN_RESTRICTED 1991 # if _POSIX_CHOWN_RESTRICTED == -1 1992 return FALSE; 1993 # else 1994 return TRUE; 1995 # endif 1996 # else 1997 # ifdef _PC_CHOWN_RESTRICTED 1998 return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 1999 # else 2000 # ifdef BSD 2001 return TRUE; 2002 # else 2003 return FALSE; 2004 # endif 2005 # endif 2006 # endif 2007 #endif 2008 } 2009 /* 2010 ** GETCFNAME -- return the name of the .cf file. 2011 ** 2012 ** Some systems (e.g., NeXT) determine this dynamically. 2013 */ 2014 2015 char * 2016 getcfname() 2017 { 2018 if (ConfFile != NULL) 2019 return ConfFile; 2020 #ifdef NETINFO 2021 { 2022 extern char *ni_propval(); 2023 char *cflocation; 2024 2025 cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 2026 if (cflocation != NULL) 2027 return cflocation; 2028 } 2029 #endif 2030 return _PATH_SENDMAILCF; 2031 } 2032 /* 2033 ** SETVENDOR -- process vendor code from V configuration line 2034 ** 2035 ** Parameters: 2036 ** vendor -- string representation of vendor. 2037 ** 2038 ** Returns: 2039 ** TRUE -- if ok. 2040 ** FALSE -- if vendor code could not be processed. 2041 ** 2042 ** Side Effects: 2043 ** It is reasonable to set mode flags here to tweak 2044 ** processing in other parts of the code if necessary. 2045 ** For example, if you are a vendor that uses $%y to 2046 ** indicate YP lookups, you could enable that here. 2047 */ 2048 2049 bool 2050 setvendor(vendor) 2051 char *vendor; 2052 { 2053 if (strcasecmp(vendor, "Berkeley") == 0) 2054 return TRUE; 2055 2056 /* add vendor extensions here */ 2057 2058 return FALSE; 2059 } 2060 /* 2061 ** STRTOL -- convert string to long integer 2062 ** 2063 ** For systems that don't have it in the C library. 2064 ** 2065 ** This is taken verbatim from the 4.4-Lite C library. 2066 */ 2067 2068 #ifdef NEEDSTRTOL 2069 2070 #if defined(LIBC_SCCS) && !defined(lint) 2071 static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 2072 #endif /* LIBC_SCCS and not lint */ 2073 2074 #include <limits.h> 2075 2076 /* 2077 * Convert a string to a long integer. 2078 * 2079 * Ignores `locale' stuff. Assumes that the upper and lower case 2080 * alphabets and digits are each contiguous. 2081 */ 2082 2083 long 2084 strtol(nptr, endptr, base) 2085 const char *nptr; 2086 char **endptr; 2087 register int base; 2088 { 2089 register const char *s = nptr; 2090 register unsigned long acc; 2091 register int c; 2092 register unsigned long cutoff; 2093 register int neg = 0, any, cutlim; 2094 2095 /* 2096 * Skip white space and pick up leading +/- sign if any. 2097 * If base is 0, allow 0x for hex and 0 for octal, else 2098 * assume decimal; if base is already 16, allow 0x. 2099 */ 2100 do { 2101 c = *s++; 2102 } while (isspace(c)); 2103 if (c == '-') { 2104 neg = 1; 2105 c = *s++; 2106 } else if (c == '+') 2107 c = *s++; 2108 if ((base == 0 || base == 16) && 2109 c == '0' && (*s == 'x' || *s == 'X')) { 2110 c = s[1]; 2111 s += 2; 2112 base = 16; 2113 } 2114 if (base == 0) 2115 base = c == '0' ? 8 : 10; 2116 2117 /* 2118 * Compute the cutoff value between legal numbers and illegal 2119 * numbers. That is the largest legal value, divided by the 2120 * base. An input number that is greater than this value, if 2121 * followed by a legal input character, is too big. One that 2122 * is equal to this value may be valid or not; the limit 2123 * between valid and invalid numbers is then based on the last 2124 * digit. For instance, if the range for longs is 2125 * [-2147483648..2147483647] and the input base is 10, 2126 * cutoff will be set to 214748364 and cutlim to either 2127 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 2128 * a value > 214748364, or equal but the next digit is > 7 (or 8), 2129 * the number is too big, and we will return a range error. 2130 * 2131 * Set any if any `digits' consumed; make it negative to indicate 2132 * overflow. 2133 */ 2134 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 2135 cutlim = cutoff % (unsigned long)base; 2136 cutoff /= (unsigned long)base; 2137 for (acc = 0, any = 0;; c = *s++) { 2138 if (isdigit(c)) 2139 c -= '0'; 2140 else if (isalpha(c)) 2141 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 2142 else 2143 break; 2144 if (c >= base) 2145 break; 2146 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 2147 any = -1; 2148 else { 2149 any = 1; 2150 acc *= base; 2151 acc += c; 2152 } 2153 } 2154 if (any < 0) { 2155 acc = neg ? LONG_MIN : LONG_MAX; 2156 errno = ERANGE; 2157 } else if (neg) 2158 acc = -acc; 2159 if (endptr != 0) 2160 *endptr = (char *)(any ? s - 1 : nptr); 2161 return (acc); 2162 } 2163 2164 #endif 2165 /* 2166 ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 2167 ** 2168 ** Solaris versions prior through 2.3 don't properly deliver a 2169 ** canonical h_name field. This tries to work around it. 2170 */ 2171 2172 #ifdef SOLARIS 2173 2174 struct hostent * 2175 solaris_gethostbyname(name) 2176 const char *name; 2177 { 2178 # ifdef SOLARIS_2_3 2179 static struct hostent hp; 2180 static char buf[1000]; 2181 extern struct hostent *_switch_gethostbyname_r(); 2182 2183 return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 2184 # else 2185 extern struct hostent *__switch_gethostbyname(); 2186 2187 return __switch_gethostbyname(name); 2188 # endif 2189 } 2190 2191 struct hostent * 2192 solaris_gethostbyaddr(addr, len, type) 2193 const char *addr; 2194 int len; 2195 int type; 2196 { 2197 # ifdef SOLARIS_2_3 2198 static struct hostent hp; 2199 static char buf[1000]; 2200 extern struct hostent *_switch_gethostbyaddr_r(); 2201 2202 return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 2203 # else 2204 extern struct hostent *__switch_gethostbyaddr(); 2205 2206 return __switch_gethostbyaddr(addr, len, type); 2207 # endif 2208 } 2209 2210 #endif 2211 /* 2212 ** NI_PROPVAL -- netinfo property value lookup routine 2213 ** 2214 ** Parameters: 2215 ** directory -- the Netinfo directory name. 2216 ** propname -- the Netinfo property name. 2217 ** 2218 ** Returns: 2219 ** NULL -- if: 2220 ** 1. the directory is not found 2221 ** 2. the property name is not found 2222 ** 3. the property contains multiple values 2223 ** 4. some error occured 2224 ** else -- the location of the config file. 2225 ** 2226 ** Notes: 2227 ** Caller should free the return value of ni_proval 2228 */ 2229 2230 #ifdef NETINFO 2231 2232 # include <netinfo/ni.h> 2233 2234 # define LOCAL_NETINFO_DOMAIN "." 2235 # define PARENT_NETINFO_DOMAIN ".." 2236 # define MAX_NI_LEVELS 256 2237 2238 char * 2239 ni_propval(directory, propname) 2240 char *directory; 2241 char *propname; 2242 { 2243 char *propval = NULL; 2244 int i; 2245 void *ni = NULL; 2246 void *lastni = NULL; 2247 ni_status nis; 2248 ni_id nid; 2249 ni_namelist ninl; 2250 2251 /* 2252 ** If the passed directory and property name are found 2253 ** in one of netinfo domains we need to search (starting 2254 ** from the local domain moving all the way back to the 2255 ** root domain) set propval to the property's value 2256 ** and return it. 2257 */ 2258 2259 for (i = 0; i < MAX_NI_LEVELS; ++i) 2260 { 2261 if (i == 0) 2262 { 2263 nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 2264 } 2265 else 2266 { 2267 if (lastni != NULL) 2268 ni_free(lastni); 2269 lastni = ni; 2270 nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 2271 } 2272 2273 /* 2274 ** Don't bother if we didn't get a handle on a 2275 ** proper domain. This is not necessarily an error. 2276 ** We would get a positive ni_status if, for instance 2277 ** we never found the directory or property and tried 2278 ** to open the parent of the root domain! 2279 */ 2280 2281 if (nis != 0) 2282 break; 2283 2284 /* 2285 ** Find the path to the server information. 2286 */ 2287 2288 if (ni_pathsearch(ni, &nid, directory) != 0) 2289 continue; 2290 2291 /* 2292 ** Find "host" information. 2293 */ 2294 2295 if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 2296 continue; 2297 2298 /* 2299 ** If there's only one name in 2300 ** the list, assume we've got 2301 ** what we want. 2302 */ 2303 2304 if (ninl.ni_namelist_len == 1) 2305 { 2306 propval = ni_name_dup(ninl.ni_namelist_val[0]); 2307 break; 2308 } 2309 } 2310 2311 /* 2312 ** Clean up. 2313 */ 2314 2315 if (ni != NULL) 2316 ni_free(ni); 2317 if (lastni != NULL && ni != lastni) 2318 ni_free(lastni); 2319 2320 return propval; 2321 } 2322 2323 #endif /* NETINFO */ 2324 /* 2325 ** HARD_SYSLOG -- call syslog repeatedly until it works 2326 ** 2327 ** Needed on HP-UX, which apparently doesn't guarantee that 2328 ** syslog succeeds during interrupt handlers. 2329 */ 2330 2331 #ifdef __hpux 2332 2333 # define MAXSYSLOGTRIES 100 2334 # undef syslog 2335 2336 # ifdef __STDC__ 2337 hard_syslog(int pri, char *msg, ...) 2338 # else 2339 hard_syslog(pri, msg, va_alist) 2340 int pri; 2341 char *msg; 2342 va_dcl 2343 # endif 2344 { 2345 int i; 2346 char buf[SYSLOG_BUFSIZE * 2]; 2347 VA_LOCAL_DECL; 2348 2349 VA_START(msg); 2350 vsprintf(buf, msg, ap); 2351 VA_END; 2352 2353 for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 2354 continue; 2355 } 2356 2357 #endif 2358