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.51 (Berkeley) 12/11/93"; 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 NeXT release 2.1. 773 */ 774 775 #include <mach.h> 776 777 getla() 778 { 779 processor_set_t default_set; 780 kern_return_t error; 781 unsigned int info_count; 782 struct processor_set_basic_info info; 783 host_t host; 784 785 error = processor_set_default(host_self(), &default_set); 786 if (error != KERN_SUCCESS) 787 return -1; 788 info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 789 if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 790 &host, (processor_set_info_t)&info, 791 &info_count) != KERN_SUCCESS) 792 { 793 return -1; 794 } 795 return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 796 } 797 798 799 #else 800 801 getla() 802 { 803 if (tTd(3, 1)) 804 printf("getla: ZERO\n"); 805 return (0); 806 } 807 808 #endif 809 #endif 810 #endif 811 /* 812 ** SHOULDQUEUE -- should this message be queued or sent? 813 ** 814 ** Compares the message cost to the load average to decide. 815 ** 816 ** Parameters: 817 ** pri -- the priority of the message in question. 818 ** ctime -- the message creation time. 819 ** 820 ** Returns: 821 ** TRUE -- if this message should be queued up for the 822 ** time being. 823 ** FALSE -- if the load is low enough to send this message. 824 ** 825 ** Side Effects: 826 ** none. 827 */ 828 829 bool 830 shouldqueue(pri, ctime) 831 long pri; 832 time_t ctime; 833 { 834 if (CurrentLA < QueueLA) 835 return (FALSE); 836 if (CurrentLA >= RefuseLA) 837 return (TRUE); 838 return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 839 } 840 /* 841 ** REFUSECONNECTIONS -- decide if connections should be refused 842 ** 843 ** Parameters: 844 ** none. 845 ** 846 ** Returns: 847 ** TRUE if incoming SMTP connections should be refused 848 ** (for now). 849 ** FALSE if we should accept new work. 850 ** 851 ** Side Effects: 852 ** none. 853 */ 854 855 bool 856 refuseconnections() 857 { 858 #ifdef XLA 859 if (!xla_smtp_ok()) 860 return TRUE; 861 #endif 862 863 /* this is probably too simplistic */ 864 return (CurrentLA >= RefuseLA); 865 } 866 /* 867 ** SETPROCTITLE -- set process title for ps 868 ** 869 ** Parameters: 870 ** fmt -- a printf style format string. 871 ** a, b, c -- possible parameters to fmt. 872 ** 873 ** Returns: 874 ** none. 875 ** 876 ** Side Effects: 877 ** Clobbers argv of our main procedure so ps(1) will 878 ** display the title. 879 */ 880 881 #ifdef SETPROCTITLE 882 # ifdef __hpux 883 # include <sys/pstat.h> 884 # endif 885 # ifdef BSD4_4 886 # include <machine/vmparam.h> 887 # include <sys/exec.h> 888 # ifdef __bsdi__ 889 # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 890 # define PROCTITLEPAD '\0' 891 # endif 892 # ifdef PS_STRINGS 893 # define SETPROC_STATIC static 894 # endif 895 # endif 896 # ifndef SETPROC_STATIC 897 # define SETPROC_STATIC 898 # endif 899 #endif 900 901 #ifndef PROCTITLEPAD 902 # define PROCTITLEPAD ' ' 903 #endif 904 905 /*VARARGS1*/ 906 #ifdef __STDC__ 907 setproctitle(char *fmt, ...) 908 #else 909 setproctitle(fmt, va_alist) 910 char *fmt; 911 va_dcl 912 #endif 913 { 914 # ifdef SETPROCTITLE 915 register char *p; 916 register int i; 917 SETPROC_STATIC char buf[MAXLINE]; 918 VA_LOCAL_DECL 919 # ifdef __hpux 920 union pstun pst; 921 # endif 922 extern char **Argv; 923 extern char *LastArgv; 924 925 p = buf; 926 927 /* print sendmail: heading for grep */ 928 (void) strcpy(p, "sendmail: "); 929 p += strlen(p); 930 931 /* print the argument string */ 932 VA_START(fmt); 933 (void) vsprintf(p, fmt, ap); 934 VA_END; 935 936 i = strlen(buf); 937 938 # ifdef __hpux 939 pst.pst_command = buf; 940 pstat(PSTAT_SETCMD, pst, i, 0, 0); 941 # else 942 # ifdef PS_STRINGS 943 PS_STRINGS->ps_nargvstr = 1; 944 PS_STRINGS->ps_argvstr = buf; 945 # else 946 if (i > LastArgv - Argv[0] - 2) 947 { 948 i = LastArgv - Argv[0] - 2; 949 buf[i] = '\0'; 950 } 951 (void) strcpy(Argv[0], buf); 952 p = &Argv[0][i]; 953 while (p < LastArgv) 954 *p++ = PROCTITLEPAD; 955 # endif 956 # endif 957 # endif /* SETPROCTITLE */ 958 } 959 /* 960 ** REAPCHILD -- pick up the body of my child, lest it become a zombie 961 ** 962 ** Parameters: 963 ** none. 964 ** 965 ** Returns: 966 ** none. 967 ** 968 ** Side Effects: 969 ** Picks up extant zombies. 970 */ 971 972 void 973 reapchild() 974 { 975 int olderrno = errno; 976 # ifdef HASWAITPID 977 auto int status; 978 int count; 979 int pid; 980 981 count = 0; 982 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 983 { 984 if (count++ > 1000) 985 { 986 syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 987 pid, status); 988 break; 989 } 990 } 991 # else 992 # ifdef WNOHANG 993 union wait status; 994 995 while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 996 continue; 997 # else /* WNOHANG */ 998 auto int status; 999 1000 while (wait(&status) > 0) 1001 continue; 1002 # endif /* WNOHANG */ 1003 # endif 1004 # ifdef SYS5SIGNALS 1005 (void) setsignal(SIGCHLD, reapchild); 1006 # endif 1007 errno = olderrno; 1008 } 1009 /* 1010 ** UNSETENV -- remove a variable from the environment 1011 ** 1012 ** Not needed on newer systems. 1013 ** 1014 ** Parameters: 1015 ** name -- the string name of the environment variable to be 1016 ** deleted from the current environment. 1017 ** 1018 ** Returns: 1019 ** none. 1020 ** 1021 ** Globals: 1022 ** environ -- a pointer to the current environment. 1023 ** 1024 ** Side Effects: 1025 ** Modifies environ. 1026 */ 1027 1028 #ifndef HASUNSETENV 1029 1030 void 1031 unsetenv(name) 1032 char *name; 1033 { 1034 extern char **environ; 1035 register char **pp; 1036 int len = strlen(name); 1037 1038 for (pp = environ; *pp != NULL; pp++) 1039 { 1040 if (strncmp(name, *pp, len) == 0 && 1041 ((*pp)[len] == '=' || (*pp)[len] == '\0')) 1042 break; 1043 } 1044 1045 for (; *pp != NULL; pp++) 1046 *pp = pp[1]; 1047 } 1048 1049 #endif 1050 /* 1051 ** GETDTABLESIZE -- return number of file descriptors 1052 ** 1053 ** Only on non-BSD systems 1054 ** 1055 ** Parameters: 1056 ** none 1057 ** 1058 ** Returns: 1059 ** size of file descriptor table 1060 ** 1061 ** Side Effects: 1062 ** none 1063 */ 1064 1065 #ifdef SOLARIS 1066 # include <sys/resource.h> 1067 #endif 1068 1069 int 1070 getdtsize() 1071 { 1072 #ifdef RLIMIT_NOFILE 1073 struct rlimit rl; 1074 1075 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 1076 return rl.rlim_cur; 1077 #endif 1078 1079 # ifdef HASGETDTABLESIZE 1080 return getdtablesize(); 1081 # else 1082 # ifdef _SC_OPEN_MAX 1083 return sysconf(_SC_OPEN_MAX); 1084 # else 1085 return NOFILE; 1086 # endif 1087 # endif 1088 } 1089 /* 1090 ** UNAME -- get the UUCP name of this system. 1091 */ 1092 1093 #ifndef HASUNAME 1094 1095 int 1096 uname(name) 1097 struct utsname *name; 1098 { 1099 FILE *file; 1100 char *n; 1101 1102 name->nodename[0] = '\0'; 1103 1104 /* try /etc/whoami -- one line with the node name */ 1105 if ((file = fopen("/etc/whoami", "r")) != NULL) 1106 { 1107 (void) fgets(name->nodename, NODE_LENGTH + 1, file); 1108 (void) fclose(file); 1109 n = strchr(name->nodename, '\n'); 1110 if (n != NULL) 1111 *n = '\0'; 1112 if (name->nodename[0] != '\0') 1113 return (0); 1114 } 1115 1116 /* try /usr/include/whoami.h -- has a #define somewhere */ 1117 if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 1118 { 1119 char buf[MAXLINE]; 1120 1121 while (fgets(buf, MAXLINE, file) != NULL) 1122 if (sscanf(buf, "#define sysname \"%*[^\"]\"", 1123 NODE_LENGTH, name->nodename) > 0) 1124 break; 1125 (void) fclose(file); 1126 if (name->nodename[0] != '\0') 1127 return (0); 1128 } 1129 1130 #ifdef TRUST_POPEN 1131 /* 1132 ** Popen is known to have security holes. 1133 */ 1134 1135 /* try uuname -l to return local name */ 1136 if ((file = popen("uuname -l", "r")) != NULL) 1137 { 1138 (void) fgets(name, NODE_LENGTH + 1, file); 1139 (void) pclose(file); 1140 n = strchr(name, '\n'); 1141 if (n != NULL) 1142 *n = '\0'; 1143 if (name->nodename[0] != '\0') 1144 return (0); 1145 } 1146 #endif 1147 1148 return (-1); 1149 } 1150 #endif /* HASUNAME */ 1151 /* 1152 ** INITGROUPS -- initialize groups 1153 ** 1154 ** Stub implementation for System V style systems 1155 */ 1156 1157 #ifndef HASINITGROUPS 1158 1159 initgroups(name, basegid) 1160 char *name; 1161 int basegid; 1162 { 1163 return 0; 1164 } 1165 1166 #endif 1167 /* 1168 ** SETSID -- set session id (for non-POSIX systems) 1169 */ 1170 1171 #ifndef HASSETSID 1172 1173 pid_t 1174 setsid __P ((void)) 1175 { 1176 #ifdef TIOCNOTTY 1177 int fd; 1178 1179 fd = open("/dev/tty", 2); 1180 if (fd >= 0) 1181 { 1182 (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 1183 (void) close(fd); 1184 } 1185 #endif /* TIOCNOTTY */ 1186 # ifdef SYS5SETPGRP 1187 return setpgrp(); 1188 # else 1189 return setpgid(0, getpid()); 1190 # endif 1191 } 1192 1193 #endif 1194 /* 1195 ** DGUX_INET_ADDR -- inet_addr for DG/UX 1196 ** 1197 ** Data General DG/UX version of inet_addr returns a struct in_addr 1198 ** instead of a long. This patches things. 1199 */ 1200 1201 #ifdef DGUX 1202 1203 #undef inet_addr 1204 1205 long 1206 dgux_inet_addr(host) 1207 char *host; 1208 { 1209 struct in_addr haddr; 1210 1211 haddr = inet_addr(host); 1212 return haddr.s_addr; 1213 } 1214 1215 #endif 1216 /* 1217 ** GETOPT -- for old systems or systems with bogus implementations 1218 */ 1219 1220 #ifdef NEEDGETOPT 1221 1222 /* 1223 * Copyright (c) 1985 Regents of the University of California. 1224 * All rights reserved. The Berkeley software License Agreement 1225 * specifies the terms and conditions for redistribution. 1226 */ 1227 1228 1229 /* 1230 ** this version hacked to add `atend' flag to allow state machine 1231 ** to reset if invoked by the program to scan args for a 2nd time 1232 */ 1233 1234 #if defined(LIBC_SCCS) && !defined(lint) 1235 static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 1236 #endif /* LIBC_SCCS and not lint */ 1237 1238 #include <stdio.h> 1239 1240 /* 1241 * get option letter from argument vector 1242 */ 1243 int opterr = 1, /* if error message should be printed */ 1244 optind = 1, /* index into parent argv vector */ 1245 optopt; /* character checked for validity */ 1246 char *optarg; /* argument associated with option */ 1247 1248 #define BADCH (int)'?' 1249 #define EMSG "" 1250 #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 1251 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 1252 1253 getopt(nargc,nargv,ostr) 1254 int nargc; 1255 char **nargv, 1256 *ostr; 1257 { 1258 static char *place = EMSG; /* option letter processing */ 1259 static char atend = 0; 1260 register char *oli; /* option letter list index */ 1261 1262 if (atend) { 1263 atend = 0; 1264 place = EMSG; 1265 } 1266 if(!*place) { /* update scanning pointer */ 1267 if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 1268 atend++; 1269 return(EOF); 1270 } 1271 if (*place == '-') { /* found "--" */ 1272 ++optind; 1273 atend++; 1274 return(EOF); 1275 } 1276 } /* option letter okay? */ 1277 if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 1278 if (!*place) ++optind; 1279 tell(": illegal option -- "); 1280 } 1281 if (*++oli != ':') { /* don't need argument */ 1282 optarg = NULL; 1283 if (!*place) ++optind; 1284 } 1285 else { /* need an argument */ 1286 if (*place) optarg = place; /* no white space */ 1287 else if (nargc <= ++optind) { /* no arg */ 1288 place = EMSG; 1289 tell(": option requires an argument -- "); 1290 } 1291 else optarg = nargv[optind]; /* white space */ 1292 place = EMSG; 1293 ++optind; 1294 } 1295 return(optopt); /* dump back option letter */ 1296 } 1297 1298 #endif 1299 /* 1300 ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 1301 */ 1302 1303 #ifdef NEEDVPRINTF 1304 1305 #define MAXARG 16 1306 1307 vfprintf(fp, fmt, ap) 1308 FILE * fp; 1309 char * fmt; 1310 char ** ap; 1311 { 1312 char * bp[MAXARG]; 1313 int i = 0; 1314 1315 while (*ap && i < MAXARG) 1316 bp[i++] = *ap++; 1317 fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 1318 bp[4], bp[5], bp[6], bp[7], 1319 bp[8], bp[9], bp[10], bp[11], 1320 bp[12], bp[13], bp[14], bp[15]); 1321 } 1322 1323 vsprintf(s, fmt, ap) 1324 char * s; 1325 char * fmt; 1326 char ** ap; 1327 { 1328 char * bp[MAXARG]; 1329 int i = 0; 1330 1331 while (*ap && i < MAXARG) 1332 bp[i++] = *ap++; 1333 sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 1334 bp[4], bp[5], bp[6], bp[7], 1335 bp[8], bp[9], bp[10], bp[11], 1336 bp[12], bp[13], bp[14], bp[15]); 1337 } 1338 1339 #endif 1340 /* 1341 ** FREESPACE -- see how much free space is on the queue filesystem 1342 ** 1343 ** Only implemented if you have statfs. 1344 ** 1345 ** Parameters: 1346 ** dir -- the directory in question. 1347 ** bsize -- a variable into which the filesystem 1348 ** block size is stored. 1349 ** 1350 ** Returns: 1351 ** The number of bytes free on the queue filesystem. 1352 ** -1 if the statfs call fails. 1353 ** 1354 ** Side effects: 1355 ** Puts the filesystem block size into bsize. 1356 */ 1357 1358 #ifdef HASSTATFS 1359 # undef HASUSTAT 1360 #endif 1361 1362 #if defined(HASUSTAT) 1363 # include <ustat.h> 1364 #endif 1365 1366 #ifdef HASSTATFS 1367 # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX) || defined(_AIX3) 1368 # include <sys/statfs.h> 1369 # else 1370 # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) || defined(MACH386) 1371 # include <sys/vfs.h> 1372 # else 1373 # include <sys/mount.h> 1374 # endif 1375 # endif 1376 #endif 1377 1378 long 1379 freespace(dir, bsize) 1380 char *dir; 1381 long *bsize; 1382 { 1383 #if defined(HASSTATFS) || defined(HASUSTAT) 1384 # if defined(HASUSTAT) 1385 struct ustat fs; 1386 struct stat statbuf; 1387 # define FSBLOCKSIZE DEV_BSIZE 1388 # define f_bavail f_tfree 1389 # else 1390 # if defined(ultrix) 1391 struct fs_data fs; 1392 # define f_bavail fd_bfreen 1393 # define FSBLOCKSIZE fs.fd_bsize 1394 # else 1395 struct statfs fs; 1396 # define FSBLOCKSIZE fs.f_bsize 1397 # if defined(_SCO_unix_) || defined(IRIX) 1398 # define f_bavail f_bfree 1399 # endif 1400 # endif 1401 # endif 1402 extern int errno; 1403 1404 # if defined(HASUSTAT) 1405 if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 1406 # else 1407 # if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX) 1408 if (statfs(dir, &fs, sizeof fs, 0) == 0) 1409 # else 1410 # if defined(ultrix) 1411 if (statfs(dir, &fs) > 0) 1412 # else 1413 if (statfs(dir, &fs) == 0) 1414 # endif 1415 # endif 1416 # endif 1417 { 1418 if (bsize != NULL) 1419 *bsize = FSBLOCKSIZE; 1420 return (fs.f_bavail); 1421 } 1422 #endif 1423 return (-1); 1424 } 1425 /* 1426 ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 1427 ** 1428 ** Only implemented if you have statfs. 1429 ** 1430 ** Parameters: 1431 ** msize -- the size to check against. If zero, we don't yet 1432 ** know how big the message will be, so just check for 1433 ** a "reasonable" amount. 1434 ** 1435 ** Returns: 1436 ** TRUE if there is enough space. 1437 ** FALSE otherwise. 1438 */ 1439 1440 bool 1441 enoughspace(msize) 1442 long msize; 1443 { 1444 long bfree, bsize; 1445 1446 if (MinBlocksFree <= 0 && msize <= 0) 1447 { 1448 if (tTd(4, 80)) 1449 printf("enoughspace: no threshold\n"); 1450 return TRUE; 1451 } 1452 1453 if ((bfree = freespace(QueueDir, &bsize)) >= 0) 1454 { 1455 if (tTd(4, 80)) 1456 printf("enoughspace: bavail=%ld, need=%ld\n", 1457 bfree, msize); 1458 1459 /* convert msize to block count */ 1460 msize = msize / bsize + 1; 1461 if (MinBlocksFree >= 0) 1462 msize += MinBlocksFree; 1463 1464 if (bfree < msize) 1465 { 1466 #ifdef LOG 1467 if (LogLevel > 0) 1468 syslog(LOG_ALERT, 1469 "%s: low on space (have %ld, %s needs %ld in %s)", 1470 CurEnv->e_id, bfree, 1471 CurHostName, msize, QueueDir); 1472 #endif 1473 return FALSE; 1474 } 1475 } 1476 else if (tTd(4, 80)) 1477 printf("enoughspace failure: min=%ld, need=%ld: %s\n", 1478 MinBlocksFree, msize, errstring(errno)); 1479 return TRUE; 1480 } 1481 /* 1482 ** TRANSIENTERROR -- tell if an error code indicates a transient failure 1483 ** 1484 ** This looks at an errno value and tells if this is likely to 1485 ** go away if retried later. 1486 ** 1487 ** Parameters: 1488 ** err -- the errno code to classify. 1489 ** 1490 ** Returns: 1491 ** TRUE if this is probably transient. 1492 ** FALSE otherwise. 1493 */ 1494 1495 bool 1496 transienterror(err) 1497 int err; 1498 { 1499 switch (err) 1500 { 1501 case EIO: /* I/O error */ 1502 case ENXIO: /* Device not configured */ 1503 case EAGAIN: /* Resource temporarily unavailable */ 1504 case ENOMEM: /* Cannot allocate memory */ 1505 case ENODEV: /* Operation not supported by device */ 1506 case ENFILE: /* Too many open files in system */ 1507 case EMFILE: /* Too many open files */ 1508 case ENOSPC: /* No space left on device */ 1509 #ifdef ETIMEDOUT 1510 case ETIMEDOUT: /* Connection timed out */ 1511 #endif 1512 #ifdef ESTALE 1513 case ESTALE: /* Stale NFS file handle */ 1514 #endif 1515 #ifdef ENETDOWN 1516 case ENETDOWN: /* Network is down */ 1517 #endif 1518 #ifdef ENETUNREACH 1519 case ENETUNREACH: /* Network is unreachable */ 1520 #endif 1521 #ifdef ENETRESET 1522 case ENETRESET: /* Network dropped connection on reset */ 1523 #endif 1524 #ifdef ECONNABORTED 1525 case ECONNABORTED: /* Software caused connection abort */ 1526 #endif 1527 #ifdef ECONNRESET 1528 case ECONNRESET: /* Connection reset by peer */ 1529 #endif 1530 #ifdef ENOBUFS 1531 case ENOBUFS: /* No buffer space available */ 1532 #endif 1533 #ifdef ESHUTDOWN 1534 case ESHUTDOWN: /* Can't send after socket shutdown */ 1535 #endif 1536 #ifdef ECONNREFUSED 1537 case ECONNREFUSED: /* Connection refused */ 1538 #endif 1539 #ifdef EHOSTDOWN 1540 case EHOSTDOWN: /* Host is down */ 1541 #endif 1542 #ifdef EHOSTUNREACH 1543 case EHOSTUNREACH: /* No route to host */ 1544 #endif 1545 #ifdef EDQUOT 1546 case EDQUOT: /* Disc quota exceeded */ 1547 #endif 1548 #ifdef EPROCLIM 1549 case EPROCLIM: /* Too many processes */ 1550 #endif 1551 #ifdef EUSERS 1552 case EUSERS: /* Too many users */ 1553 #endif 1554 #ifdef EDEADLK 1555 case EDEADLK: /* Resource deadlock avoided */ 1556 #endif 1557 #ifdef EISCONN 1558 case EISCONN: /* Socket already connected */ 1559 #endif 1560 #ifdef EINPROGRESS 1561 case EINPROGRESS: /* Operation now in progress */ 1562 #endif 1563 #ifdef EALREADY 1564 case EALREADY: /* Operation already in progress */ 1565 #endif 1566 #ifdef EADDRINUSE 1567 case EADDRINUSE: /* Address already in use */ 1568 #endif 1569 #ifdef EADDRNOTAVAIL 1570 case EADDRNOTAVAIL: /* Can't assign requested address */ 1571 #endif 1572 #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 1573 case ENOSR: /* Out of streams resources */ 1574 #endif 1575 return TRUE; 1576 } 1577 1578 /* nope, must be permanent */ 1579 return FALSE; 1580 } 1581 /* 1582 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 1583 ** 1584 ** Parameters: 1585 ** fd -- the file descriptor of the file. 1586 ** filename -- the file name (for error messages). 1587 ** ext -- the filename extension. 1588 ** type -- type of the lock. Bits can be: 1589 ** LOCK_EX -- exclusive lock. 1590 ** LOCK_NB -- non-blocking. 1591 ** 1592 ** Returns: 1593 ** TRUE if the lock was acquired. 1594 ** FALSE otherwise. 1595 */ 1596 1597 bool 1598 lockfile(fd, filename, ext, type) 1599 int fd; 1600 char *filename; 1601 char *ext; 1602 int type; 1603 { 1604 # ifndef HASFLOCK 1605 int action; 1606 struct flock lfd; 1607 1608 if (ext == NULL) 1609 ext = ""; 1610 1611 bzero(&lfd, sizeof lfd); 1612 if (bitset(LOCK_UN, type)) 1613 lfd.l_type = F_UNLCK; 1614 else if (bitset(LOCK_EX, type)) 1615 lfd.l_type = F_WRLCK; 1616 else 1617 lfd.l_type = F_RDLCK; 1618 1619 if (bitset(LOCK_NB, type)) 1620 action = F_SETLK; 1621 else 1622 action = F_SETLKW; 1623 1624 if (tTd(55, 60)) 1625 printf("lockfile(%s%s, action=%d, type=%d): ", 1626 filename, ext, action, lfd.l_type); 1627 1628 if (fcntl(fd, action, &lfd) >= 0) 1629 { 1630 if (tTd(55, 60)) 1631 printf("SUCCESS\n"); 1632 return TRUE; 1633 } 1634 1635 if (tTd(55, 60)) 1636 printf("(%s) ", errstring(errno)); 1637 1638 /* 1639 ** On SunOS, if you are testing using -oQ/tmp/mqueue or 1640 ** -oA/tmp/aliases or anything like that, and /tmp is mounted 1641 ** as type "tmp" (that is, served from swap space), the 1642 ** previous fcntl will fail with "Invalid argument" errors. 1643 ** Since this is fairly common during testing, we will assume 1644 ** that this indicates that the lock is successfully grabbed. 1645 */ 1646 1647 if (errno == EINVAL) 1648 { 1649 if (tTd(55, 60)) 1650 printf("SUCCESS\n"); 1651 return TRUE; 1652 } 1653 1654 if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 1655 { 1656 int omode = -1; 1657 # ifdef F_GETFL 1658 int oerrno = errno; 1659 1660 (void) fcntl(fd, F_GETFL, &omode); 1661 errno = oerrno; 1662 # endif 1663 syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1664 filename, ext, fd, type, omode, geteuid()); 1665 } 1666 # else 1667 if (ext == NULL) 1668 ext = ""; 1669 1670 if (tTd(55, 60)) 1671 printf("lockfile(%s%s, type=%o): ", filename, ext, type); 1672 1673 if (flock(fd, type) >= 0) 1674 { 1675 if (tTd(55, 60)) 1676 printf("SUCCESS\n"); 1677 return TRUE; 1678 } 1679 1680 if (tTd(55, 60)) 1681 printf("(%s) ", errstring(errno)); 1682 1683 if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 1684 { 1685 int omode = -1; 1686 # ifdef F_GETFL 1687 int oerrno = errno; 1688 1689 (void) fcntl(fd, F_GETFL, &omode); 1690 errno = oerrno; 1691 # endif 1692 syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 1693 filename, ext, fd, type, omode, geteuid()); 1694 } 1695 # endif 1696 if (tTd(55, 60)) 1697 printf("FAIL\n"); 1698 return FALSE; 1699 } 1700 /* 1701 ** GETCFNAME -- return the name of the .cf file. 1702 ** 1703 ** Some systems (e.g., NeXT) determine this dynamically. 1704 */ 1705 1706 char * 1707 getcfname() 1708 { 1709 if (ConfFile != NULL) 1710 return ConfFile; 1711 return _PATH_SENDMAILCF; 1712 } 1713 /* 1714 ** SETVENDOR -- process vendor code from V configuration line 1715 ** 1716 ** Parameters: 1717 ** vendor -- string representation of vendor. 1718 ** 1719 ** Returns: 1720 ** TRUE -- if ok. 1721 ** FALSE -- if vendor code could not be processed. 1722 ** 1723 ** Side Effects: 1724 ** It is reasonable to set mode flags here to tweak 1725 ** processing in other parts of the code if necessary. 1726 ** For example, if you are a vendor that uses $%y to 1727 ** indicate YP lookups, you could enable that here. 1728 */ 1729 1730 bool 1731 setvendor(vendor) 1732 char *vendor; 1733 { 1734 if (strcasecmp(vendor, "Berkeley") == 0) 1735 return TRUE; 1736 1737 /* add vendor extensions here */ 1738 1739 return FALSE; 1740 } 1741 /* 1742 ** STRTOL -- convert string to long integer 1743 ** 1744 ** For systems that don't have it in the C library. 1745 */ 1746 1747 #ifdef NEEDSTRTOL 1748 1749 long 1750 strtol(p, ep, b) 1751 char *p; 1752 char **ep; 1753 int b; 1754 { 1755 long l = 0; 1756 char c; 1757 char maxd; 1758 int neg = 1; 1759 1760 maxd = (b > 10) ? '9' : b + '0'; 1761 1762 if (p && *p == '-') { 1763 neg = -1; 1764 p++; 1765 } 1766 while (p && (c = *p)) { 1767 if (c >= '0' && c <= maxd) { 1768 l = l*b + *p++ - '0'; 1769 continue; 1770 } 1771 if (c >= 'A' && c <= 'Z') 1772 c -= 'A' + 'a'; 1773 c = c - 'a' + 10; 1774 if (b > c) { 1775 l = l*b + c; 1776 p++; 1777 continue; 1778 } 1779 break; 1780 } 1781 l *= neg; 1782 if (ep) 1783 *ep = p; 1784 return l; 1785 } 1786 1787 #endif 1788 /* 1789 ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 1790 ** 1791 ** Solaris versions prior through 2.3 don't properly deliver a 1792 ** canonical h_name field. This tries to work around it. 1793 */ 1794 1795 #ifdef SOLARIS 1796 1797 struct hostent * 1798 solaris_gethostbyname(name) 1799 const char *name; 1800 { 1801 # ifdef SOLARIS_2_3 1802 static struct hostent hp; 1803 static char buf[1000]; 1804 extern struct hostent *_switch_gethostbyname_r(); 1805 1806 return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 1807 # else 1808 extern struct hostent *__switch_gethostbyname(); 1809 1810 return __switch_gethostbyname(name); 1811 # endif 1812 } 1813 1814 struct hostent * 1815 solaris_gethostbyaddr(addr, len, type) 1816 const char *addr; 1817 int len; 1818 int type; 1819 { 1820 # ifdef SOLARIS_2_3 1821 static struct hostent hp; 1822 static char buf[1000]; 1823 extern struct hostent *_switch_gethostbyaddr_r(); 1824 1825 return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 1826 # else 1827 extern struct hostent *__switch_gethostbyaddr(); 1828 1829 return __switch_gethostbyaddr(addr, len, type); 1830 # endif 1831 } 1832 1833 #endif 1834