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.50 (Berkeley) 05/24/93 (with daemon mode)"; 16 #else 17 static char sccsid[] = "@(#)daemon.c 6.50 (Berkeley) 05/24/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 ** host_map_lookup(map, hbuf, avp, pstat) 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 #ifdef NETINET 513 hid = inet_addr(&host[1]); 514 if (hid == -1) 515 #endif 516 { 517 /* try it as a host name (avoid MX lookup) */ 518 hp = gethostbyname(&host[1]); 519 *p = ']'; 520 goto gothostent; 521 } 522 *p = ']'; 523 } 524 if (p == NULL) 525 { 526 usrerr("553 Invalid numeric domain spec \"%s\"", host); 527 return (EX_NOHOST); 528 } 529 #ifdef NETINET 530 addr.sin.sin_family = AF_INET; /*XXX*/ 531 addr.sin.sin_addr.s_addr = hid; 532 #endif 533 } 534 else 535 { 536 hp = gethostbyname(host); 537 gothostent: 538 if (hp == NULL) 539 { 540 #ifdef NAMED_BIND 541 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 542 return (EX_TEMPFAIL); 543 544 /* if name server is specified, assume temp fail */ 545 if (errno == ECONNREFUSED && UseNameServer) 546 return (EX_TEMPFAIL); 547 #endif 548 return (EX_NOHOST); 549 } 550 addr.sa.sa_family = hp->h_addrtype; 551 switch (hp->h_addrtype) 552 { 553 #ifdef NETINET 554 case AF_INET: 555 bcopy(hp->h_addr, 556 &addr.sin.sin_addr, 557 hp->h_length); 558 break; 559 #endif 560 561 default: 562 bcopy(hp->h_addr, 563 addr.sa.sa_data, 564 hp->h_length); 565 break; 566 } 567 i = 1; 568 } 569 570 /* 571 ** Determine the port number. 572 */ 573 574 if (port != 0) 575 port = htons(port); 576 else 577 { 578 register struct servent *sp = getservbyname("smtp", "tcp"); 579 580 if (sp == NULL) 581 { 582 syserr("554 makeconnection: service \"smtp\" unknown"); 583 return (EX_OSERR); 584 } 585 port = sp->s_port; 586 } 587 588 switch (addr.sa.sa_family) 589 { 590 #ifdef NETINET 591 case AF_INET: 592 addr.sin.sin_port = port; 593 addrlen = sizeof (struct sockaddr_in); 594 break; 595 #endif 596 597 #ifdef NETISO 598 case AF_ISO: 599 /* assume two byte transport selector */ 600 bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 601 addrlen = sizeof (struct sockaddr_iso); 602 break; 603 #endif 604 605 default: 606 syserr("Can't connect to address family %d", addr.sa.sa_family); 607 return (EX_NOHOST); 608 } 609 610 /* 611 ** Try to actually open the connection. 612 */ 613 614 #ifdef XLA 615 /* if too many connections, don't bother trying */ 616 if (!xla_noqueue_ok(host)) 617 return EX_TEMPFAIL; 618 #endif 619 620 for (;;) 621 { 622 if (tTd(16, 1)) 623 printf("makeconnection (%s [%s])\n", 624 host, anynet_ntoa(&addr)); 625 626 /* save for logging */ 627 CurHostAddr = addr; 628 629 if (usesecureport) 630 { 631 int rport = IPPORT_RESERVED - 1; 632 633 s = rresvport(&rport); 634 } 635 else 636 { 637 s = socket(AF_INET, SOCK_STREAM, 0); 638 } 639 if (s < 0) 640 { 641 sav_errno = errno; 642 syserr("makeconnection: no socket"); 643 goto failure; 644 } 645 646 if (tTd(16, 1)) 647 printf("makeconnection: fd=%d\n", s); 648 649 /* turn on network debugging? */ 650 if (tTd(16, 101)) 651 { 652 int on = 1; 653 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 654 (char *)&on, sizeof on); 655 } 656 if (CurEnv->e_xfp != NULL) 657 (void) fflush(CurEnv->e_xfp); /* for debugging */ 658 errno = 0; /* for debugging */ 659 if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 660 break; 661 662 /* couldn't connect.... figure out why */ 663 sav_errno = errno; 664 (void) close(s); 665 if (hp && hp->h_addr_list[i]) 666 { 667 if (tTd(16, 1)) 668 printf("Connect failed (%s); trying new address....\n", 669 errstring(sav_errno)); 670 switch (addr.sa.sa_family) 671 { 672 #ifdef NETINET 673 case AF_INET: 674 bcopy(hp->h_addr_list[i++], 675 &addr.sin.sin_addr, 676 hp->h_length); 677 break; 678 #endif 679 680 default: 681 bcopy(hp->h_addr_list[i++], 682 addr.sa.sa_data, 683 hp->h_length); 684 break; 685 } 686 continue; 687 } 688 689 /* failure, decide if temporary or not */ 690 failure: 691 #ifdef XLA 692 xla_host_end(host); 693 #endif 694 if (transienterror(sav_errno)) 695 return EX_TEMPFAIL; 696 else 697 { 698 message("%s", errstring(sav_errno)); 699 return (EX_UNAVAILABLE); 700 } 701 } 702 703 /* connection ok, put it into canonical form */ 704 mci->mci_out = fdopen(s, "w"); 705 mci->mci_in = fdopen(dup(s), "r"); 706 707 return (EX_OK); 708 } 709 /* 710 ** MYHOSTNAME -- return the name of this host. 711 ** 712 ** Parameters: 713 ** hostbuf -- a place to return the name of this host. 714 ** size -- the size of hostbuf. 715 ** 716 ** Returns: 717 ** A list of aliases for this host. 718 ** 719 ** Side Effects: 720 ** Sets the MyIpAddrs buffer to a list of my IP addresses. 721 */ 722 723 struct in_addr MyIpAddrs[MAXIPADDR + 1]; 724 725 char ** 726 myhostname(hostbuf, size) 727 char hostbuf[]; 728 int size; 729 { 730 register struct hostent *hp; 731 extern struct hostent *gethostbyname(); 732 733 if (gethostname(hostbuf, size) < 0) 734 { 735 (void) strcpy(hostbuf, "localhost"); 736 } 737 hp = gethostbyname(hostbuf); 738 if (hp != NULL) 739 { 740 (void) strncpy(hostbuf, hp->h_name, size - 1); 741 hostbuf[size - 1] = '\0'; 742 743 if (hp->h_addrtype == AF_INET && hp->h_length == 4) 744 { 745 register int i; 746 747 for (i = 0; i < MAXIPADDR; i++) 748 { 749 if (hp->h_addr_list[i] == NULL) 750 break; 751 MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 752 } 753 MyIpAddrs[i].s_addr = 0; 754 } 755 756 return (hp->h_aliases); 757 } 758 else 759 return (NULL); 760 } 761 /* 762 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 763 ** 764 ** Uses RFC1413 protocol to try to get info from the other end. 765 ** 766 ** Parameters: 767 ** fd -- the descriptor 768 ** 769 ** Returns: 770 ** The user@host information associated with this descriptor. 771 ** 772 ** Side Effects: 773 ** Sets RealHostName to the name of the host at the other end. 774 */ 775 776 #ifdef IDENTPROTO 777 778 static jmp_buf CtxAuthTimeout; 779 780 static 781 authtimeout() 782 { 783 longjmp(CtxAuthTimeout, 1); 784 } 785 786 #endif 787 788 char * 789 getauthinfo(fd) 790 int fd; 791 { 792 SOCKADDR fa; 793 int falen; 794 register char *p; 795 #ifdef IDENTPROTO 796 SOCKADDR la; 797 int lalen; 798 register struct servent *sp; 799 int s; 800 int i; 801 EVENT *ev; 802 #endif 803 static char hbuf[MAXNAME * 2 + 2]; 804 extern char *hostnamebyanyaddr(); 805 extern char RealUserName[]; /* main.c */ 806 807 falen = sizeof fa; 808 if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0) 809 { 810 RealHostName = "localhost"; 811 (void) sprintf(hbuf, "%s@localhost", RealUserName); 812 if (tTd(9, 1)) 813 printf("getauthinfo: %s\n", hbuf); 814 return hbuf; 815 } 816 817 RealHostName = newstr(hostnamebyanyaddr(&fa)); 818 RealHostAddr = fa; 819 820 #ifdef IDENTPROTO 821 lalen = sizeof la; 822 if (fa.sa.sa_family != AF_INET || 823 getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 824 la.sa.sa_family != AF_INET) 825 { 826 /* no ident info */ 827 goto noident; 828 } 829 830 /* create ident query */ 831 (void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port); 832 833 /* create local address */ 834 bzero(&la, sizeof la); 835 836 /* create foreign address */ 837 sp = getservbyname("auth", "tcp"); 838 if (sp != NULL) 839 fa.sin.sin_port = sp->s_port; 840 else 841 fa.sin.sin_port = htons(113); 842 843 s = -1; 844 if (setjmp(CtxAuthTimeout) != 0) 845 { 846 if (s >= 0) 847 (void) close(s); 848 goto noident; 849 } 850 851 /* put a timeout around the whole thing */ 852 ev = setevent((time_t) 30, authtimeout, 0); 853 854 /* connect to foreign IDENT server */ 855 s = socket(AF_INET, SOCK_STREAM, 0); 856 if (s < 0) 857 { 858 clrevent(ev); 859 goto noident; 860 } 861 if (connect(s, &fa.sa, sizeof fa.sin) < 0) 862 { 863 closeident: 864 (void) close(s); 865 clrevent(ev); 866 goto noident; 867 } 868 869 if (tTd(9, 10)) 870 printf("getauthinfo: sent %s", hbuf); 871 872 /* send query */ 873 if (write(s, hbuf, strlen(hbuf)) < 0) 874 goto closeident; 875 876 /* get result */ 877 i = read(s, hbuf, sizeof hbuf); 878 (void) close(s); 879 clrevent(ev); 880 if (i <= 0) 881 goto noident; 882 if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 883 i--; 884 hbuf[++i] = '\0'; 885 886 if (tTd(9, 3)) 887 printf("getauthinfo: got %s\n", hbuf); 888 889 /* parse result */ 890 p = strchr(hbuf, ':'); 891 if (p == NULL) 892 { 893 /* malformed response */ 894 goto noident; 895 } 896 while (isascii(*++p) && isspace(*p)) 897 continue; 898 if (strncasecmp(p, "userid", 6) != 0) 899 { 900 /* presumably an error string */ 901 goto noident; 902 } 903 p += 6; 904 while (isascii(*p) && isspace(*p)) 905 p++; 906 if (*p++ != ':') 907 { 908 /* either useridxx or malformed response */ 909 goto noident; 910 } 911 912 /* p now points to the OSTYPE field */ 913 p = strchr(p, ':'); 914 if (p == NULL) 915 { 916 /* malformed response */ 917 goto noident; 918 } 919 920 /* 1413 says don't do this -- but it's broken otherwise */ 921 while (isascii(*++p) && isspace(*p)) 922 continue; 923 924 /* p now points to the authenticated name */ 925 (void) sprintf(hbuf, "%s@%s", p, RealHostName); 926 goto finish; 927 928 #endif /* IDENTPROTO */ 929 930 noident: 931 (void) strcpy(hbuf, RealHostName); 932 933 finish: 934 if (RealHostName[0] != '[') 935 { 936 p = &hbuf[strlen(hbuf)]; 937 (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 938 } 939 if (tTd(9, 1)) 940 printf("getauthinfo: %s\n", hbuf); 941 return hbuf; 942 } 943 /* 944 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 945 ** 946 ** Parameters: 947 ** map -- a pointer to this map (unused). 948 ** name -- the (presumably unqualified) hostname. 949 ** av -- unused -- for compatibility with other mapping 950 ** functions. 951 ** statp -- an exit status (out parameter) -- set to 952 ** EX_TEMPFAIL if the name server is unavailable. 953 ** 954 ** Returns: 955 ** The mapping, if found. 956 ** NULL if no mapping found. 957 ** 958 ** Side Effects: 959 ** Looks up the host specified in hbuf. If it is not 960 ** the canonical name for that host, return the canonical 961 ** name. 962 */ 963 964 char * 965 host_map_lookup(map, name, av, statp) 966 MAP *map; 967 char *name; 968 char **av; 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 char hbuf[MAXNAME]; 977 extern struct hostent *gethostbyaddr(); 978 extern int h_errno; 979 extern char *map_rewrite(); 980 981 /* 982 ** See if we have already looked up this name. If so, just 983 ** return it. 984 */ 985 986 s = stab(name, ST_NAMECANON, ST_ENTER); 987 if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 988 { 989 if (tTd(9, 1)) 990 printf("host_map_lookup(%s) => CACHE %s\n", 991 name, s->s_namecanon.nc_cname); 992 errno = s->s_namecanon.nc_errno; 993 h_errno = s->s_namecanon.nc_herrno; 994 *statp = s->s_namecanon.nc_stat; 995 return s->s_namecanon.nc_cname; 996 } 997 998 /* 999 ** If first character is a bracket, then it is an address 1000 ** lookup. Address is copied into a temporary buffer to 1001 ** strip the brackets and to preserve name if address is 1002 ** unknown. 1003 */ 1004 1005 if (*name != '[') 1006 { 1007 extern bool getcanonname(); 1008 1009 if (tTd(9, 1)) 1010 printf("host_map_lookup(%s) => ", name); 1011 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1012 (void) strcpy(hbuf, name); 1013 if (getcanonname(hbuf, sizeof hbuf - 1)) 1014 { 1015 if (tTd(9, 1)) 1016 printf("%s\n", hbuf); 1017 cp = map_rewrite(map, hbuf, strlen(hbuf), av); 1018 s->s_namecanon.nc_cname = newstr(cp); 1019 return cp; 1020 } 1021 else 1022 { 1023 register struct hostent *hp; 1024 1025 if (tTd(9, 1)) 1026 printf("FAIL (%d)\n", h_errno); 1027 s->s_namecanon.nc_errno = errno; 1028 s->s_namecanon.nc_herrno = h_errno; 1029 switch (h_errno) 1030 { 1031 case TRY_AGAIN: 1032 if (UseNameServer) 1033 { 1034 char *msg = "Recipient domain nameserver timed out"; 1035 1036 message(msg); 1037 if (CurEnv->e_message == NULL) 1038 CurEnv->e_message = newstr(msg); 1039 } 1040 *statp = EX_TEMPFAIL; 1041 break; 1042 1043 case HOST_NOT_FOUND: 1044 *statp = EX_NOHOST; 1045 break; 1046 1047 case NO_RECOVERY: 1048 *statp = EX_SOFTWARE; 1049 break; 1050 1051 default: 1052 *statp = EX_UNAVAILABLE; 1053 break; 1054 } 1055 s->s_namecanon.nc_stat = *statp; 1056 if (*statp != EX_TEMPFAIL || UseNameServer) 1057 return NULL; 1058 1059 /* 1060 ** Try to look it up in /etc/hosts 1061 */ 1062 1063 hp = gethostbyname(name); 1064 if (hp == NULL) 1065 { 1066 /* no dice there either */ 1067 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1068 return NULL; 1069 } 1070 1071 s->s_namecanon.nc_stat = *statp = EX_OK; 1072 cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1073 s->s_namecanon.nc_cname = newstr(cp); 1074 return cp; 1075 } 1076 } 1077 if ((cp = strchr(name, ']')) == NULL) 1078 return (NULL); 1079 *cp = '\0'; 1080 in_addr = inet_addr(&name[1]); 1081 1082 /* check to see if this is one of our addresses */ 1083 for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 1084 { 1085 if (MyIpAddrs[i].s_addr == in_addr) 1086 { 1087 return map_rewrite(map, MyHostName, strlen(MyHostName), av); 1088 } 1089 } 1090 1091 /* nope -- ask the name server */ 1092 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 1093 s->s_namecanon.nc_errno = errno; 1094 s->s_namecanon.nc_herrno = h_errno; 1095 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1096 if (hp == NULL) 1097 { 1098 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1099 return (NULL); 1100 } 1101 1102 /* found a match -- copy out */ 1103 cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1104 s->s_namecanon.nc_stat = *statp = EX_OK; 1105 s->s_namecanon.nc_cname = newstr(cp); 1106 return cp; 1107 } 1108 /* 1109 ** ANYNET_NTOA -- convert a network address to printable form. 1110 ** 1111 ** Parameters: 1112 ** sap -- a pointer to a sockaddr structure. 1113 ** 1114 ** Returns: 1115 ** A printable version of that sockaddr. 1116 */ 1117 1118 char * 1119 anynet_ntoa(sap) 1120 register SOCKADDR *sap; 1121 { 1122 register char *bp; 1123 register char *ap; 1124 int l; 1125 static char buf[80]; 1126 1127 /* check for null/zero family */ 1128 if (sap == NULL) 1129 return "NULLADDR"; 1130 if (sap->sa.sa_family == 0) 1131 return "0"; 1132 1133 #ifdef NETINET 1134 if (sap->sa.sa_family == AF_INET) 1135 { 1136 extern char *inet_ntoa(); 1137 1138 return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 1139 } 1140 #endif 1141 1142 /* unknown family -- just dump bytes */ 1143 (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1144 bp = &buf[strlen(buf)]; 1145 ap = sap->sa.sa_data; 1146 for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1147 { 1148 (void) sprintf(bp, "%02x:", *ap++ & 0377); 1149 bp += 3; 1150 } 1151 *--bp = '\0'; 1152 return buf; 1153 } 1154 /* 1155 ** HOSTNAMEBYANYADDR -- return name of host based on address 1156 ** 1157 ** Parameters: 1158 ** sap -- SOCKADDR pointer 1159 ** 1160 ** Returns: 1161 ** text representation of host name. 1162 ** 1163 ** Side Effects: 1164 ** none. 1165 */ 1166 1167 char * 1168 hostnamebyanyaddr(sap) 1169 register SOCKADDR *sap; 1170 { 1171 register struct hostent *hp; 1172 1173 #ifdef NAMED_BIND 1174 int saveretry; 1175 1176 /* shorten name server timeout to avoid higher level timeouts */ 1177 saveretry = _res.retry; 1178 _res.retry = 3; 1179 #endif /* NAMED_BIND */ 1180 1181 switch (sap->sa.sa_family) 1182 { 1183 #ifdef NETINET 1184 case AF_INET: 1185 hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1186 sizeof sap->sin.sin_addr, 1187 AF_INET); 1188 break; 1189 #endif 1190 1191 #ifdef NETISO 1192 case AF_ISO: 1193 hp = gethostbyaddr((char *) &sap->siso.siso_addr, 1194 sizeof sap->siso.siso_addr, 1195 AF_ISO); 1196 break; 1197 #endif 1198 1199 default: 1200 hp = gethostbyaddr(sap->sa.sa_data, 1201 sizeof sap->sa.sa_data, 1202 sap->sa.sa_family); 1203 break; 1204 } 1205 1206 #ifdef NAMED_BIND 1207 _res.retry = saveretry; 1208 #endif /* NAMED_BIND */ 1209 1210 if (hp != NULL) 1211 return hp->h_name; 1212 else 1213 { 1214 /* produce a dotted quad */ 1215 static char buf[512]; 1216 1217 (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 1218 return buf; 1219 } 1220 } 1221 1222 # else /* DAEMON */ 1223 /* code for systems without sophisticated networking */ 1224 1225 /* 1226 ** MYHOSTNAME -- stub version for case of no daemon code. 1227 ** 1228 ** Can't convert to upper case here because might be a UUCP name. 1229 ** 1230 ** Mark, you can change this to be anything you want...... 1231 */ 1232 1233 char ** 1234 myhostname(hostbuf, size) 1235 char hostbuf[]; 1236 int size; 1237 { 1238 register FILE *f; 1239 1240 hostbuf[0] = '\0'; 1241 f = fopen("/usr/include/whoami", "r"); 1242 if (f != NULL) 1243 { 1244 (void) fgets(hostbuf, size, f); 1245 fixcrlf(hostbuf, TRUE); 1246 (void) fclose(f); 1247 } 1248 return (NULL); 1249 } 1250 /* 1251 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1252 ** 1253 ** Parameters: 1254 ** fd -- the descriptor 1255 ** 1256 ** Returns: 1257 ** The host name associated with this descriptor, if it can 1258 ** be determined. 1259 ** NULL otherwise. 1260 ** 1261 ** Side Effects: 1262 ** none 1263 */ 1264 1265 char * 1266 getauthinfo(fd) 1267 int fd; 1268 { 1269 return NULL; 1270 } 1271 /* 1272 ** MAPHOSTNAME -- turn a hostname into canonical form 1273 ** 1274 ** Parameters: 1275 ** map -- a pointer to the database map. 1276 ** name -- a buffer containing a hostname. 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 name. 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 host_map_lookup(map, name, avp, statp) 1294 MAP *map; 1295 char *name; 1296 char **avp; 1297 char *statp; 1298 { 1299 register struct hostent *hp; 1300 1301 hp = gethostbyname(name); 1302 if (hp != NULL) 1303 return hp->h_name; 1304 *statp = EX_NOHOST; 1305 return NULL; 1306 } 1307 1308 #endif /* DAEMON */ 1309