1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #include <errno.h> 10 #include <signal.h> 11 #include "sendmail.h" 12 13 #ifndef lint 14 #ifdef DAEMON 15 static char sccsid[] = "@(#)daemon.c 6.45 (Berkeley) 05/07/93 (with daemon mode)"; 16 #else 17 static char sccsid[] = "@(#)daemon.c 6.45 (Berkeley) 05/07/93 (without daemon mode)"; 18 #endif 19 #endif /* not lint */ 20 21 #ifdef DAEMON 22 23 # include <netdb.h> 24 # include <sys/wait.h> 25 # include <sys/time.h> 26 27 #ifdef NAMED_BIND 28 # include <arpa/nameser.h> 29 # include <resolv.h> 30 #endif 31 32 /* 33 ** DAEMON.C -- routines to use when running as a daemon. 34 ** 35 ** This entire file is highly dependent on the 4.2 BSD 36 ** interprocess communication primitives. No attempt has 37 ** been made to make this file portable to Version 7, 38 ** Version 6, MPX files, etc. If you should try such a 39 ** thing yourself, I recommend chucking the entire file 40 ** and starting from scratch. Basic semantics are: 41 ** 42 ** getrequests() 43 ** Opens a port and initiates a connection. 44 ** Returns in a child. Must set InChannel and 45 ** OutChannel appropriately. 46 ** clrdaemon() 47 ** Close any open files associated with getting 48 ** the connection; this is used when running the queue, 49 ** etc., to avoid having extra file descriptors during 50 ** the queue run and to avoid confusing the network 51 ** code (if it cares). 52 ** makeconnection(host, port, outfile, infile, usesecureport) 53 ** Make a connection to the named host on the given 54 ** port. Set *outfile and *infile to the files 55 ** appropriate for communication. Returns zero on 56 ** success, else an exit status describing the 57 ** error. 58 ** maphostname(map, hbuf, hbufsiz, avp) 59 ** Convert the entry in hbuf into a canonical form. 60 */ 61 62 extern char *anynet_ntoa(); 63 /* 64 ** GETREQUESTS -- open mail IPC port and get requests. 65 ** 66 ** Parameters: 67 ** none. 68 ** 69 ** Returns: 70 ** none. 71 ** 72 ** Side Effects: 73 ** Waits until some interesting activity occurs. When 74 ** it does, a child is created to process it, and the 75 ** parent waits for completion. Return from this 76 ** routine is always in the child. The file pointers 77 ** "InChannel" and "OutChannel" should be set to point 78 ** to the communication channel. 79 */ 80 81 int DaemonSocket = -1; /* fd describing socket */ 82 SOCKADDR DaemonAddr; /* socket for incoming */ 83 int ListenQueueSize = 10; /* size of listen queue */ 84 85 getrequests() 86 { 87 int t; 88 register struct servent *sp; 89 int on = 1; 90 bool refusingconnections = TRUE; 91 FILE *pidf; 92 extern void reapchild(); 93 94 /* 95 ** Set up the address for the mailer. 96 */ 97 98 if (DaemonAddr.sin.sin_family == 0) 99 DaemonAddr.sin.sin_family = AF_INET; 100 if (DaemonAddr.sin.sin_addr.s_addr == 0) 101 DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 102 if (DaemonAddr.sin.sin_port == 0) 103 { 104 sp = getservbyname("smtp", "tcp"); 105 if (sp == NULL) 106 { 107 syserr("554 service \"smtp\" unknown"); 108 goto severe; 109 } 110 DaemonAddr.sin.sin_port = sp->s_port; 111 } 112 113 /* 114 ** Try to actually open the connection. 115 */ 116 117 if (tTd(15, 1)) 118 printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 119 120 /* get a socket for the SMTP connection */ 121 DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 122 if (DaemonSocket < 0) 123 { 124 /* probably another daemon already */ 125 syserr("getrequests: can't create socket"); 126 severe: 127 # ifdef LOG 128 if (LogLevel > 0) 129 syslog(LOG_ALERT, "problem creating SMTP socket"); 130 # endif /* LOG */ 131 finis(); 132 } 133 134 /* turn on network debugging? */ 135 if (tTd(15, 101)) 136 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 137 138 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 139 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 140 141 switch (DaemonAddr.sa.sa_family) 142 { 143 # ifdef NETINET 144 case AF_INET: 145 t = sizeof DaemonAddr.sin; 146 break; 147 # endif 148 149 # ifdef NETISO 150 case AF_ISO: 151 t = sizeof DaemonAddr.siso; 152 break; 153 # endif 154 155 default: 156 t = sizeof DaemonAddr; 157 break; 158 } 159 160 if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0) 161 { 162 syserr("getrequests: cannot bind"); 163 (void) close(DaemonSocket); 164 goto severe; 165 } 166 167 (void) signal(SIGCHLD, reapchild); 168 169 /* write the pid to the log file for posterity */ 170 pidf = fopen(PidFile, "w"); 171 if (pidf != NULL) 172 { 173 fprintf(pidf, "%d\n", getpid()); 174 fclose(pidf); 175 } 176 177 178 if (tTd(15, 1)) 179 printf("getrequests: %d\n", DaemonSocket); 180 181 for (;;) 182 { 183 register int pid; 184 auto int lotherend; 185 extern bool refuseconnections(); 186 187 /* see if we are rejecting connections */ 188 CurrentLA = getla(); 189 if (refuseconnections()) 190 { 191 if (!refusingconnections) 192 { 193 /* don't queue so peer will fail quickly */ 194 (void) listen(DaemonSocket, 0); 195 refusingconnections = TRUE; 196 } 197 setproctitle("rejecting connections: load average: %d", 198 CurrentLA); 199 sleep(5); 200 continue; 201 } 202 203 if (refusingconnections) 204 { 205 /* start listening again */ 206 if (listen(DaemonSocket, ListenQueueSize) < 0) 207 { 208 syserr("getrequests: cannot listen"); 209 (void) close(DaemonSocket); 210 goto severe; 211 } 212 setproctitle("accepting connections"); 213 refusingconnections = FALSE; 214 } 215 216 /* wait for a connection */ 217 do 218 { 219 errno = 0; 220 lotherend = sizeof RealHostAddr; 221 t = accept(DaemonSocket, 222 (struct sockaddr *)&RealHostAddr, &lotherend); 223 } while (t < 0 && errno == EINTR); 224 if (t < 0) 225 { 226 syserr("getrequests: accept"); 227 sleep(5); 228 continue; 229 } 230 231 /* 232 ** Create a subprocess to process the mail. 233 */ 234 235 if (tTd(15, 2)) 236 printf("getrequests: forking (fd = %d)\n", t); 237 238 pid = fork(); 239 if (pid < 0) 240 { 241 syserr("daemon: cannot fork"); 242 sleep(10); 243 (void) close(t); 244 continue; 245 } 246 247 if (pid == 0) 248 { 249 extern char *hostnamebyanyaddr(); 250 251 /* 252 ** CHILD -- return to caller. 253 ** Collect verified idea of sending host. 254 ** Verify calling user id if possible here. 255 */ 256 257 (void) signal(SIGCHLD, SIG_DFL); 258 OpMode = MD_SMTP; 259 260 /* determine host name */ 261 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 262 263 #ifdef LOG 264 if (LogLevel > 10) 265 { 266 /* log connection information */ 267 syslog(LOG_INFO, "connect from %s (%s)", 268 RealHostName, anynet_ntoa(&RealHostAddr)); 269 } 270 #endif 271 272 (void) close(DaemonSocket); 273 InChannel = fdopen(t, "r"); 274 OutChannel = fdopen(dup(t), "w"); 275 276 /* should we check for illegal connection here? XXX */ 277 #ifdef XLA 278 if (!xla_host_ok(RealHostName)) 279 { 280 message("421 Too many SMTP sessions for this host"); 281 exit(0); 282 } 283 #endif 284 285 if (tTd(15, 2)) 286 printf("getreq: returning\n"); 287 return; 288 } 289 290 /* close the port so that others will hang (for a while) */ 291 (void) close(t); 292 } 293 /*NOTREACHED*/ 294 } 295 /* 296 ** CLRDAEMON -- reset the daemon connection 297 ** 298 ** Parameters: 299 ** none. 300 ** 301 ** Returns: 302 ** none. 303 ** 304 ** Side Effects: 305 ** releases any resources used by the passive daemon. 306 */ 307 308 clrdaemon() 309 { 310 if (DaemonSocket >= 0) 311 (void) close(DaemonSocket); 312 DaemonSocket = -1; 313 } 314 /* 315 ** SETDAEMONOPTIONS -- set options for running the daemon 316 ** 317 ** Parameters: 318 ** p -- the options line. 319 ** 320 ** Returns: 321 ** none. 322 */ 323 324 setdaemonoptions(p) 325 register char *p; 326 { 327 if (DaemonAddr.sa.sa_family == AF_UNSPEC) 328 DaemonAddr.sa.sa_family = AF_INET; 329 330 while (p != NULL) 331 { 332 register char *f; 333 register char *v; 334 335 while (isascii(*p) && isspace(*p)) 336 p++; 337 if (*p == '\0') 338 break; 339 f = p; 340 p = strchr(p, ','); 341 if (p != NULL) 342 *p++ = '\0'; 343 v = strchr(f, '='); 344 if (v == NULL) 345 continue; 346 while (isascii(*++v) && isspace(*v)) 347 continue; 348 349 switch (*f) 350 { 351 case 'F': /* address family */ 352 if (isascii(*v) && isdigit(*v)) 353 DaemonAddr.sa.sa_family = atoi(v); 354 #ifdef NETINET 355 else if (strcasecmp(v, "inet") == 0) 356 DaemonAddr.sa.sa_family = AF_INET; 357 #endif 358 #ifdef NETISO 359 else if (strcasecmp(v, "iso") == 0) 360 DaemonAddr.sa.sa_family = AF_ISO; 361 #endif 362 #ifdef NETNS 363 else if (strcasecmp(v, "ns") == 0) 364 DaemonAddr.sa.sa_family = AF_NS; 365 #endif 366 #ifdef NETX25 367 else if (strcasecmp(v, "x.25") == 0) 368 DaemonAddr.sa.sa_family = AF_CCITT; 369 #endif 370 else 371 syserr("554 Unknown address family %s in Family=option", v); 372 break; 373 374 case 'A': /* address */ 375 switch (DaemonAddr.sa.sa_family) 376 { 377 #ifdef NETINET 378 case AF_INET: 379 if (isascii(*v) && isdigit(*v)) 380 DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 381 else 382 { 383 register struct netent *np; 384 385 np = getnetbyname(v); 386 if (np == NULL) 387 syserr("554 network \"%s\" unknown", v); 388 else 389 DaemonAddr.sin.sin_addr.s_addr = np->n_net; 390 } 391 break; 392 #endif 393 394 default: 395 syserr("554 Address= option unsupported for family %d", 396 DaemonAddr.sa.sa_family); 397 break; 398 } 399 break; 400 401 case 'P': /* port */ 402 switch (DaemonAddr.sa.sa_family) 403 { 404 short port; 405 406 #ifdef NETINET 407 case AF_INET: 408 if (isascii(*v) && isdigit(*v)) 409 DaemonAddr.sin.sin_port = atoi(v); 410 else 411 { 412 register struct servent *sp; 413 414 sp = getservbyname(v, "tcp"); 415 if (sp == NULL) 416 syserr("554 service \"%s\" unknown", v); 417 else 418 DaemonAddr.sin.sin_port = sp->s_port; 419 } 420 break; 421 #endif 422 423 #ifdef NETISO 424 case AF_ISO: 425 /* assume two byte transport selector */ 426 if (isascii(*v) && isdigit(*v)) 427 port = atoi(v); 428 else 429 { 430 register struct servent *sp; 431 432 sp = getservbyname(v, "tcp"); 433 if (sp == NULL) 434 syserr("554 service \"%s\" unknown", v); 435 else 436 port = sp->s_port; 437 } 438 bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 439 break; 440 #endif 441 442 default: 443 syserr("554 Port= option unsupported for family %d", 444 DaemonAddr.sa.sa_family); 445 break; 446 } 447 break; 448 449 case 'L': /* listen queue size */ 450 ListenQueueSize = atoi(v); 451 break; 452 } 453 } 454 } 455 /* 456 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 457 ** 458 ** Parameters: 459 ** host -- the name of the host. 460 ** port -- the port number to connect to. 461 ** mci -- a pointer to the mail connection information 462 ** structure to be filled in. 463 ** usesecureport -- if set, use a low numbered (reserved) 464 ** port to provide some rudimentary authentication. 465 ** 466 ** Returns: 467 ** An exit code telling whether the connection could be 468 ** made and if not why not. 469 ** 470 ** Side Effects: 471 ** none. 472 */ 473 474 SOCKADDR CurHostAddr; /* address of current host */ 475 476 int 477 makeconnection(host, port, mci, usesecureport) 478 char *host; 479 u_short port; 480 register MCI *mci; 481 bool usesecureport; 482 { 483 register int i, s; 484 register struct hostent *hp = (struct hostent *)NULL; 485 SOCKADDR addr; 486 int sav_errno; 487 int addrlen; 488 #ifdef NAMED_BIND 489 extern int h_errno; 490 #endif 491 492 /* 493 ** Set up the address for the mailer. 494 ** Accept "[a.b.c.d]" syntax for host name. 495 */ 496 497 #ifdef NAMED_BIND 498 h_errno = 0; 499 #endif 500 errno = 0; 501 bzero(&CurHostAddr, sizeof CurHostAddr); 502 CurHostName = host; 503 504 if (host[0] == '[') 505 { 506 long hid; 507 register char *p = strchr(host, ']'); 508 509 if (p != NULL) 510 { 511 *p = '\0'; 512 hid = inet_addr(&host[1]); 513 if (hid == -1) 514 { 515 /* try it as a host name (avoid MX lookup) */ 516 hp = gethostbyname(&host[1]); 517 *p = ']'; 518 goto gothostent; 519 } 520 *p = ']'; 521 } 522 if (p == NULL) 523 { 524 usrerr("553 Invalid numeric domain spec \"%s\"", host); 525 return (EX_NOHOST); 526 } 527 addr.sin.sin_family = AF_INET; 528 addr.sin.sin_addr.s_addr = hid; 529 } 530 else 531 { 532 hp = gethostbyname(host); 533 gothostent: 534 if (hp == NULL) 535 { 536 #ifdef NAMED_BIND 537 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 538 return (EX_TEMPFAIL); 539 540 /* if name server is specified, assume temp fail */ 541 if (errno == ECONNREFUSED && UseNameServer) 542 return (EX_TEMPFAIL); 543 #endif 544 return (EX_NOHOST); 545 } 546 addr.sa.sa_family = hp->h_addrtype; 547 switch (hp->h_addrtype) 548 { 549 #ifdef NETINET 550 case AF_INET: 551 bcopy(hp->h_addr, 552 &addr.sin.sin_addr, 553 hp->h_length); 554 break; 555 #endif 556 557 default: 558 bcopy(hp->h_addr, 559 addr.sa.sa_data, 560 hp->h_length); 561 break; 562 } 563 i = 1; 564 } 565 566 /* 567 ** Determine the port number. 568 */ 569 570 if (port != 0) 571 port = htons(port); 572 else 573 { 574 register struct servent *sp = getservbyname("smtp", "tcp"); 575 576 if (sp == NULL) 577 { 578 syserr("554 makeconnection: service \"smtp\" unknown"); 579 return (EX_OSERR); 580 } 581 port = sp->s_port; 582 } 583 584 switch (addr.sa.sa_family) 585 { 586 case AF_INET: 587 addr.sin.sin_port = port; 588 addrlen = sizeof (struct sockaddr_in); 589 break; 590 591 #ifdef NETISO 592 case AF_ISO: 593 /* assume two byte transport selector */ 594 bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 595 addrlen = sizeof (struct sockaddr_iso); 596 break; 597 #endif 598 599 default: 600 syserr("Can't connect to address family %d", addr.sa.sa_family); 601 return (EX_NOHOST); 602 } 603 604 /* 605 ** Try to actually open the connection. 606 */ 607 608 #ifdef XLA 609 /* if too many connections, don't bother trying */ 610 if (!xla_noqueue_ok(host)) 611 return EX_TEMPFAIL; 612 #endif 613 614 for (;;) 615 { 616 if (tTd(16, 1)) 617 printf("makeconnection (%s [%s])\n", 618 host, anynet_ntoa(&addr)); 619 620 /* save for logging */ 621 CurHostAddr = addr; 622 623 if (usesecureport) 624 { 625 int rport = IPPORT_RESERVED - 1; 626 627 s = rresvport(&rport); 628 } 629 else 630 { 631 s = socket(AF_INET, SOCK_STREAM, 0); 632 } 633 if (s < 0) 634 { 635 sav_errno = errno; 636 syserr("makeconnection: no socket"); 637 goto failure; 638 } 639 640 if (tTd(16, 1)) 641 printf("makeconnection: fd=%d\n", s); 642 643 /* turn on network debugging? */ 644 if (tTd(16, 101)) 645 { 646 int on = 1; 647 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 648 (char *)&on, sizeof on); 649 } 650 if (CurEnv->e_xfp != NULL) 651 (void) fflush(CurEnv->e_xfp); /* for debugging */ 652 errno = 0; /* for debugging */ 653 if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 654 break; 655 656 /* couldn't connect.... figure out why */ 657 sav_errno = errno; 658 (void) close(s); 659 if (hp && hp->h_addr_list[i]) 660 { 661 extern char *errstring(); 662 663 if (tTd(16, 1)) 664 printf("Connect failed (%s); trying new address....\n", 665 errstring(sav_errno)); 666 switch (addr.sa.sa_family) 667 { 668 #ifdef NETINET 669 case AF_INET: 670 bcopy(hp->h_addr_list[i++], 671 &addr.sin.sin_addr, 672 hp->h_length); 673 break; 674 #endif 675 676 default: 677 bcopy(hp->h_addr_list[i++], 678 addr.sa.sa_data, 679 hp->h_length); 680 break; 681 } 682 continue; 683 } 684 685 /* failure, decide if temporary or not */ 686 failure: 687 #ifdef XLA 688 xla_host_end(host); 689 #endif 690 if (transienterror(sav_errno)) 691 return EX_TEMPFAIL; 692 else 693 { 694 extern char *errstring(); 695 696 message("%s", errstring(sav_errno)); 697 return (EX_UNAVAILABLE); 698 } 699 } 700 701 /* connection ok, put it into canonical form */ 702 mci->mci_out = fdopen(s, "w"); 703 mci->mci_in = fdopen(dup(s), "r"); 704 705 return (EX_OK); 706 } 707 /* 708 ** MYHOSTNAME -- return the name of this host. 709 ** 710 ** Parameters: 711 ** hostbuf -- a place to return the name of this host. 712 ** size -- the size of hostbuf. 713 ** 714 ** Returns: 715 ** A list of aliases for this host. 716 ** 717 ** Side Effects: 718 ** Sets the MyIpAddrs buffer to a list of my IP addresses. 719 */ 720 721 struct in_addr MyIpAddrs[MAXIPADDR + 1]; 722 723 char ** 724 myhostname(hostbuf, size) 725 char hostbuf[]; 726 int size; 727 { 728 register struct hostent *hp; 729 extern struct hostent *gethostbyname(); 730 731 if (gethostname(hostbuf, size) < 0) 732 { 733 (void) strcpy(hostbuf, "localhost"); 734 } 735 hp = gethostbyname(hostbuf); 736 if (hp != NULL) 737 { 738 (void) strncpy(hostbuf, hp->h_name, size - 1); 739 hostbuf[size - 1] = '\0'; 740 741 if (hp->h_addrtype == AF_INET && hp->h_length == 4) 742 { 743 register int i; 744 745 for (i = 0; i < MAXIPADDR; i++) 746 { 747 if (hp->h_addr_list[i] == NULL) 748 break; 749 MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 750 } 751 MyIpAddrs[i].s_addr = 0; 752 } 753 754 return (hp->h_aliases); 755 } 756 else 757 return (NULL); 758 } 759 /* 760 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 761 ** 762 ** Uses RFC1413 protocol to try to get info from the other end. 763 ** 764 ** Parameters: 765 ** fd -- the descriptor 766 ** 767 ** Returns: 768 ** The user@host information associated with this descriptor. 769 ** 770 ** Side Effects: 771 ** Sets RealHostName to the name of the host at the other end. 772 */ 773 774 #ifdef IDENTPROTO 775 776 static jmp_buf CtxAuthTimeout; 777 778 static 779 authtimeout() 780 { 781 longjmp(CtxAuthTimeout, 1); 782 } 783 784 #endif 785 786 char * 787 getauthinfo(fd) 788 int fd; 789 { 790 SOCKADDR fa; 791 int falen; 792 register char *p; 793 #ifdef IDENTPROTO 794 SOCKADDR la; 795 int lalen; 796 register struct servent *sp; 797 int s; 798 int i; 799 EVENT *ev; 800 #endif 801 static char hbuf[MAXNAME * 2 + 2]; 802 extern char *hostnamebyanyaddr(); 803 extern char RealUserName[]; /* main.c */ 804 805 falen = sizeof fa; 806 if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0) 807 { 808 RealHostName = "localhost"; 809 (void) sprintf(hbuf, "%s@localhost", RealUserName); 810 if (tTd(9, 1)) 811 printf("getauthinfo: %s\n", hbuf); 812 return hbuf; 813 } 814 815 RealHostName = newstr(hostnamebyanyaddr(&fa)); 816 RealHostAddr = fa; 817 818 #ifdef IDENTPROTO 819 lalen = sizeof la; 820 if (fa.sa.sa_family != AF_INET || 821 getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 822 la.sa.sa_family != AF_INET) 823 { 824 /* no ident info */ 825 goto noident; 826 } 827 828 /* create ident query */ 829 (void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port); 830 831 /* create local address */ 832 bzero(&la, sizeof la); 833 834 /* create foreign address */ 835 sp = getservbyname("auth", "tcp"); 836 if (sp != NULL) 837 fa.sin.sin_port = sp->s_port; 838 else 839 fa.sin.sin_port = htons(113); 840 841 s = -1; 842 if (setjmp(CtxAuthTimeout) != 0) 843 { 844 if (s >= 0) 845 (void) close(s); 846 goto noident; 847 } 848 849 /* put a timeout around the whole thing */ 850 ev = setevent((time_t) 30, authtimeout, 0); 851 852 /* connect to foreign IDENT server */ 853 s = socket(AF_INET, SOCK_STREAM, 0); 854 if (s < 0) 855 { 856 clrevent(ev); 857 goto noident; 858 } 859 if (connect(s, &fa.sa, sizeof fa.sin) < 0) 860 { 861 closeident: 862 (void) close(s); 863 clrevent(ev); 864 goto noident; 865 } 866 867 if (tTd(9, 10)) 868 printf("getauthinfo: sent %s", hbuf); 869 870 /* send query */ 871 if (write(s, hbuf, strlen(hbuf)) < 0) 872 goto closeident; 873 874 /* get result */ 875 i = read(s, hbuf, sizeof hbuf); 876 (void) close(s); 877 clrevent(ev); 878 if (i <= 0) 879 goto noident; 880 if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 881 i--; 882 hbuf[++i] = '\0'; 883 884 if (tTd(9, 3)) 885 printf("getauthinfo: got %s\n", hbuf); 886 887 /* parse result */ 888 p = strchr(hbuf, ':'); 889 if (p == NULL) 890 { 891 /* malformed response */ 892 goto noident; 893 } 894 while (isascii(*++p) && isspace(*p)) 895 continue; 896 if (strncasecmp(p, "userid", 6) != 0) 897 { 898 /* presumably an error string */ 899 goto noident; 900 } 901 p += 6; 902 while (isascii(*p) && isspace(*p)) 903 p++; 904 if (*p++ != ':') 905 { 906 /* either useridxx or malformed response */ 907 goto noident; 908 } 909 910 /* p now points to the OSTYPE field */ 911 p = strchr(p, ':'); 912 if (p == NULL) 913 { 914 /* malformed response */ 915 goto noident; 916 } 917 918 /* 1413 says don't do this -- but it's broken otherwise */ 919 while (isascii(*++p) && isspace(*p)) 920 continue; 921 922 /* p now points to the authenticated name */ 923 (void) sprintf(hbuf, "%s@%s", p, RealHostName); 924 goto finish; 925 926 #endif /* IDENTPROTO */ 927 928 noident: 929 (void) strcpy(hbuf, RealHostName); 930 931 finish: 932 if (RealHostName[0] != '[') 933 { 934 p = &hbuf[strlen(hbuf)]; 935 (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 936 } 937 if (tTd(9, 1)) 938 printf("getauthinfo: %s\n", hbuf); 939 return hbuf; 940 } 941 /* 942 ** MAPHOSTNAME -- turn a hostname into canonical form 943 ** 944 ** Parameters: 945 ** map -- a pointer to this map (unused). 946 ** hbuf -- a buffer containing a hostname. 947 ** hbsize -- the size of hbuf. 948 ** avp -- unused -- for compatibility with other mapping 949 ** functions. 950 ** statp -- an exit status (out parameter) -- set to 951 ** EX_TEMPFAIL if the name server is unavailable. 952 ** 953 ** Returns: 954 ** The mapping, if found. 955 ** NULL if no mapping found. 956 ** 957 ** Side Effects: 958 ** Looks up the host specified in hbuf. If it is not 959 ** the canonical name for that host, return the canonical 960 ** name. 961 */ 962 963 char * 964 maphostname(map, hbuf, hbsize, avp, statp) 965 MAP *map; 966 char *hbuf; 967 int hbsize; 968 char **avp; 969 int *statp; 970 { 971 register struct hostent *hp; 972 u_long in_addr; 973 char *cp; 974 int i; 975 register STAB *s; 976 extern struct hostent *gethostbyaddr(); 977 extern int h_errno; 978 979 /* allow room for null */ 980 hbsize--; 981 982 /* 983 ** See if we have already looked up this name. If so, just 984 ** return it. 985 */ 986 987 s = stab(hbuf, ST_NAMECANON, ST_ENTER); 988 if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 989 { 990 errno = s->s_namecanon.nc_errno; 991 h_errno = s->s_namecanon.nc_herrno; 992 *statp = s->s_namecanon.nc_stat; 993 return s->s_namecanon.nc_cname; 994 } 995 996 /* 997 ** If first character is a bracket, then it is an address 998 ** lookup. Address is copied into a temporary buffer to 999 ** strip the brackets and to preserve hbuf if address is 1000 ** unknown. 1001 */ 1002 1003 if (*hbuf != '[') 1004 { 1005 extern bool getcanonname(); 1006 1007 if (tTd(9, 1)) 1008 printf("maphostname(%s, %d) => ", hbuf, hbsize); 1009 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1010 if (getcanonname(hbuf, hbsize)) 1011 { 1012 if (tTd(9, 1)) 1013 printf("%s\n", hbuf); 1014 s->s_namecanon.nc_cname = newstr(hbuf); 1015 return hbuf; 1016 } 1017 else 1018 { 1019 register struct hostent *hp; 1020 1021 if (tTd(9, 1)) 1022 printf("FAIL (%d)\n", h_errno); 1023 s->s_namecanon.nc_errno = errno; 1024 s->s_namecanon.nc_herrno = h_errno; 1025 switch (h_errno) 1026 { 1027 case TRY_AGAIN: 1028 if (UseNameServer) 1029 { 1030 char *msg = "Recipient domain nameserver timed out"; 1031 1032 message(msg); 1033 if (CurEnv->e_message == NULL) 1034 CurEnv->e_message = msg; 1035 } 1036 *statp = EX_TEMPFAIL; 1037 break; 1038 1039 case HOST_NOT_FOUND: 1040 *statp = EX_NOHOST; 1041 break; 1042 1043 case NO_RECOVERY: 1044 *statp = EX_SOFTWARE; 1045 break; 1046 1047 default: 1048 *statp = EX_UNAVAILABLE; 1049 break; 1050 } 1051 s->s_namecanon.nc_stat = *statp; 1052 if (*statp != EX_TEMPFAIL || UseNameServer) 1053 return NULL; 1054 1055 /* 1056 ** Try to look it up in /etc/hosts 1057 */ 1058 1059 hp = gethostbyname(hbuf); 1060 if (hp == NULL) 1061 { 1062 /* no dice there either */ 1063 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1064 return NULL; 1065 } 1066 1067 s->s_namecanon.nc_stat = *statp = EX_OK; 1068 s->s_namecanon.nc_cname = newstr(hp->h_name); 1069 return hp->h_name; 1070 } 1071 } 1072 if ((cp = strchr(hbuf, ']')) == NULL) 1073 return (NULL); 1074 *cp = '\0'; 1075 in_addr = inet_addr(&hbuf[1]); 1076 1077 /* check to see if this is one of our addresses */ 1078 for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 1079 { 1080 if (MyIpAddrs[i].s_addr == in_addr) 1081 { 1082 strncpy(hbuf, MyHostName, hbsize); 1083 hbuf[hbsize] = '\0'; 1084 return hbuf; 1085 } 1086 } 1087 1088 /* nope -- ask the name server */ 1089 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 1090 s->s_namecanon.nc_errno = errno; 1091 s->s_namecanon.nc_herrno = h_errno; 1092 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1093 if (hp == NULL) 1094 { 1095 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1096 return (NULL); 1097 } 1098 1099 /* found a match -- copy out */ 1100 s->s_namecanon.nc_cname = newstr(hp->h_name); 1101 if (strlen(hp->h_name) > hbsize) 1102 hp->h_name[hbsize] = '\0'; 1103 (void) strcpy(hbuf, hp->h_name); 1104 s->s_namecanon.nc_stat = *statp = EX_OK; 1105 return hbuf; 1106 } 1107 /* 1108 ** ANYNET_NTOA -- convert a network address to printable form. 1109 ** 1110 ** Parameters: 1111 ** sap -- a pointer to a sockaddr structure. 1112 ** 1113 ** Returns: 1114 ** A printable version of that sockaddr. 1115 */ 1116 1117 char * 1118 anynet_ntoa(sap) 1119 register SOCKADDR *sap; 1120 { 1121 register char *bp; 1122 register char *ap; 1123 int l; 1124 static char buf[80]; 1125 1126 /* check for null/zero family */ 1127 if (sap == NULL) 1128 return "NULLADDR"; 1129 if (sap->sa.sa_family == 0) 1130 return "0"; 1131 1132 #ifdef NETINET 1133 if (sap->sa.sa_family == AF_INET) 1134 { 1135 extern char *inet_ntoa(); 1136 1137 return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 1138 } 1139 #endif 1140 1141 /* unknown family -- just dump bytes */ 1142 (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1143 bp = &buf[strlen(buf)]; 1144 ap = sap->sa.sa_data; 1145 for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1146 { 1147 (void) sprintf(bp, "%02x:", *ap++ & 0377); 1148 bp += 3; 1149 } 1150 *--bp = '\0'; 1151 return buf; 1152 } 1153 /* 1154 ** HOSTNAMEBYANYADDR -- return name of host based on address 1155 ** 1156 ** Parameters: 1157 ** sap -- SOCKADDR pointer 1158 ** 1159 ** Returns: 1160 ** text representation of host name. 1161 ** 1162 ** Side Effects: 1163 ** none. 1164 */ 1165 1166 char * 1167 hostnamebyanyaddr(sap) 1168 register SOCKADDR *sap; 1169 { 1170 register struct hostent *hp; 1171 1172 #ifdef NAMED_BIND 1173 int saveretry; 1174 1175 /* shorten name server timeout to avoid higher level timeouts */ 1176 saveretry = _res.retry; 1177 _res.retry = 3; 1178 #endif /* NAMED_BIND */ 1179 1180 switch (sap->sa.sa_family) 1181 { 1182 #ifdef NETINET 1183 case AF_INET: 1184 hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1185 sizeof sap->sin.sin_addr, 1186 AF_INET); 1187 break; 1188 #endif 1189 1190 #ifdef NETISO 1191 case AF_ISO: 1192 hp = gethostbyaddr((char *) &sap->siso.siso_addr, 1193 sizeof sap->siso.siso_addr, 1194 AF_ISO); 1195 break; 1196 #endif 1197 1198 default: 1199 hp = gethostbyaddr(sap->sa.sa_data, 1200 sizeof sap->sa.sa_data, 1201 sap->sa.sa_family); 1202 break; 1203 } 1204 1205 #ifdef NAMED_BIND 1206 _res.retry = saveretry; 1207 #endif /* NAMED_BIND */ 1208 1209 if (hp != NULL) 1210 return hp->h_name; 1211 else 1212 { 1213 /* produce a dotted quad */ 1214 static char buf[512]; 1215 1216 (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 1217 return buf; 1218 } 1219 } 1220 1221 # else /* DAEMON */ 1222 /* code for systems without sophisticated networking */ 1223 1224 /* 1225 ** MYHOSTNAME -- stub version for case of no daemon code. 1226 ** 1227 ** Can't convert to upper case here because might be a UUCP name. 1228 ** 1229 ** Mark, you can change this to be anything you want...... 1230 */ 1231 1232 char ** 1233 myhostname(hostbuf, size) 1234 char hostbuf[]; 1235 int size; 1236 { 1237 register FILE *f; 1238 1239 hostbuf[0] = '\0'; 1240 f = fopen("/usr/include/whoami", "r"); 1241 if (f != NULL) 1242 { 1243 (void) fgets(hostbuf, size, f); 1244 fixcrlf(hostbuf, TRUE); 1245 (void) fclose(f); 1246 } 1247 return (NULL); 1248 } 1249 /* 1250 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1251 ** 1252 ** Parameters: 1253 ** fd -- the descriptor 1254 ** 1255 ** Returns: 1256 ** The host name associated with this descriptor, if it can 1257 ** be determined. 1258 ** NULL otherwise. 1259 ** 1260 ** Side Effects: 1261 ** none 1262 */ 1263 1264 char * 1265 getauthinfo(fd) 1266 int fd; 1267 { 1268 return NULL; 1269 } 1270 /* 1271 ** MAPHOSTNAME -- turn a hostname into canonical form 1272 ** 1273 ** Parameters: 1274 ** map -- a pointer to the database map. 1275 ** hbuf -- a buffer containing a hostname. 1276 ** hbsize -- size of hbuf. 1277 ** avp -- a pointer to a (cf file defined) argument vector. 1278 ** statp -- an exit status (out parameter). 1279 ** 1280 ** Returns: 1281 ** mapped host name 1282 ** FALSE otherwise. 1283 ** 1284 ** Side Effects: 1285 ** Looks up the host specified in hbuf. If it is not 1286 ** the canonical name for that host, replace it with 1287 ** the canonical name. If the name is unknown, or it 1288 ** is already the canonical name, leave it unchanged. 1289 */ 1290 1291 /*ARGSUSED*/ 1292 char * 1293 maphostname(map, hbuf, hbsize, avp, statp) 1294 MAP *map; 1295 char *hbuf; 1296 int hbsize; 1297 char **avp; 1298 char *statp; 1299 { 1300 register struct hostent *hp; 1301 1302 hp = gethostbyname(hbuf); 1303 if (hp != NULL) 1304 return hp->h_name; 1305 *statp = EX_NOHOST; 1306 return NULL; 1307 } 1308 1309 #endif /* DAEMON */ 1310