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