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