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.47 (Berkeley) 05/13/93 (with daemon mode)"; 16 #else 17 static char sccsid[] = "@(#)daemon.c 6.47 (Berkeley) 05/13/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 #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 extern char *errstring(); 668 669 if (tTd(16, 1)) 670 printf("Connect failed (%s); trying new address....\n", 671 errstring(sav_errno)); 672 switch (addr.sa.sa_family) 673 { 674 #ifdef NETINET 675 case AF_INET: 676 bcopy(hp->h_addr_list[i++], 677 &addr.sin.sin_addr, 678 hp->h_length); 679 break; 680 #endif 681 682 default: 683 bcopy(hp->h_addr_list[i++], 684 addr.sa.sa_data, 685 hp->h_length); 686 break; 687 } 688 continue; 689 } 690 691 /* failure, decide if temporary or not */ 692 failure: 693 #ifdef XLA 694 xla_host_end(host); 695 #endif 696 if (transienterror(sav_errno)) 697 return EX_TEMPFAIL; 698 else 699 { 700 extern char *errstring(); 701 702 message("%s", errstring(sav_errno)); 703 return (EX_UNAVAILABLE); 704 } 705 } 706 707 /* connection ok, put it into canonical form */ 708 mci->mci_out = fdopen(s, "w"); 709 mci->mci_in = fdopen(dup(s), "r"); 710 711 return (EX_OK); 712 } 713 /* 714 ** MYHOSTNAME -- return the name of this host. 715 ** 716 ** Parameters: 717 ** hostbuf -- a place to return the name of this host. 718 ** size -- the size of hostbuf. 719 ** 720 ** Returns: 721 ** A list of aliases for this host. 722 ** 723 ** Side Effects: 724 ** Sets the MyIpAddrs buffer to a list of my IP addresses. 725 */ 726 727 struct in_addr MyIpAddrs[MAXIPADDR + 1]; 728 729 char ** 730 myhostname(hostbuf, size) 731 char hostbuf[]; 732 int size; 733 { 734 register struct hostent *hp; 735 extern struct hostent *gethostbyname(); 736 737 if (gethostname(hostbuf, size) < 0) 738 { 739 (void) strcpy(hostbuf, "localhost"); 740 } 741 hp = gethostbyname(hostbuf); 742 if (hp != NULL) 743 { 744 (void) strncpy(hostbuf, hp->h_name, size - 1); 745 hostbuf[size - 1] = '\0'; 746 747 if (hp->h_addrtype == AF_INET && hp->h_length == 4) 748 { 749 register int i; 750 751 for (i = 0; i < MAXIPADDR; i++) 752 { 753 if (hp->h_addr_list[i] == NULL) 754 break; 755 MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 756 } 757 MyIpAddrs[i].s_addr = 0; 758 } 759 760 return (hp->h_aliases); 761 } 762 else 763 return (NULL); 764 } 765 /* 766 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 767 ** 768 ** Uses RFC1413 protocol to try to get info from the other end. 769 ** 770 ** Parameters: 771 ** fd -- the descriptor 772 ** 773 ** Returns: 774 ** The user@host information associated with this descriptor. 775 ** 776 ** Side Effects: 777 ** Sets RealHostName to the name of the host at the other end. 778 */ 779 780 #ifdef IDENTPROTO 781 782 static jmp_buf CtxAuthTimeout; 783 784 static 785 authtimeout() 786 { 787 longjmp(CtxAuthTimeout, 1); 788 } 789 790 #endif 791 792 char * 793 getauthinfo(fd) 794 int fd; 795 { 796 SOCKADDR fa; 797 int falen; 798 register char *p; 799 #ifdef IDENTPROTO 800 SOCKADDR la; 801 int lalen; 802 register struct servent *sp; 803 int s; 804 int i; 805 EVENT *ev; 806 #endif 807 static char hbuf[MAXNAME * 2 + 2]; 808 extern char *hostnamebyanyaddr(); 809 extern char RealUserName[]; /* main.c */ 810 811 falen = sizeof fa; 812 if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0) 813 { 814 RealHostName = "localhost"; 815 (void) sprintf(hbuf, "%s@localhost", RealUserName); 816 if (tTd(9, 1)) 817 printf("getauthinfo: %s\n", hbuf); 818 return hbuf; 819 } 820 821 RealHostName = newstr(hostnamebyanyaddr(&fa)); 822 RealHostAddr = fa; 823 824 #ifdef IDENTPROTO 825 lalen = sizeof la; 826 if (fa.sa.sa_family != AF_INET || 827 getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 828 la.sa.sa_family != AF_INET) 829 { 830 /* no ident info */ 831 goto noident; 832 } 833 834 /* create ident query */ 835 (void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port); 836 837 /* create local address */ 838 bzero(&la, sizeof la); 839 840 /* create foreign address */ 841 sp = getservbyname("auth", "tcp"); 842 if (sp != NULL) 843 fa.sin.sin_port = sp->s_port; 844 else 845 fa.sin.sin_port = htons(113); 846 847 s = -1; 848 if (setjmp(CtxAuthTimeout) != 0) 849 { 850 if (s >= 0) 851 (void) close(s); 852 goto noident; 853 } 854 855 /* put a timeout around the whole thing */ 856 ev = setevent((time_t) 30, authtimeout, 0); 857 858 /* connect to foreign IDENT server */ 859 s = socket(AF_INET, SOCK_STREAM, 0); 860 if (s < 0) 861 { 862 clrevent(ev); 863 goto noident; 864 } 865 if (connect(s, &fa.sa, sizeof fa.sin) < 0) 866 { 867 closeident: 868 (void) close(s); 869 clrevent(ev); 870 goto noident; 871 } 872 873 if (tTd(9, 10)) 874 printf("getauthinfo: sent %s", hbuf); 875 876 /* send query */ 877 if (write(s, hbuf, strlen(hbuf)) < 0) 878 goto closeident; 879 880 /* get result */ 881 i = read(s, hbuf, sizeof hbuf); 882 (void) close(s); 883 clrevent(ev); 884 if (i <= 0) 885 goto noident; 886 if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 887 i--; 888 hbuf[++i] = '\0'; 889 890 if (tTd(9, 3)) 891 printf("getauthinfo: got %s\n", hbuf); 892 893 /* parse result */ 894 p = strchr(hbuf, ':'); 895 if (p == NULL) 896 { 897 /* malformed response */ 898 goto noident; 899 } 900 while (isascii(*++p) && isspace(*p)) 901 continue; 902 if (strncasecmp(p, "userid", 6) != 0) 903 { 904 /* presumably an error string */ 905 goto noident; 906 } 907 p += 6; 908 while (isascii(*p) && isspace(*p)) 909 p++; 910 if (*p++ != ':') 911 { 912 /* either useridxx or malformed response */ 913 goto noident; 914 } 915 916 /* p now points to the OSTYPE field */ 917 p = strchr(p, ':'); 918 if (p == NULL) 919 { 920 /* malformed response */ 921 goto noident; 922 } 923 924 /* 1413 says don't do this -- but it's broken otherwise */ 925 while (isascii(*++p) && isspace(*p)) 926 continue; 927 928 /* p now points to the authenticated name */ 929 (void) sprintf(hbuf, "%s@%s", p, RealHostName); 930 goto finish; 931 932 #endif /* IDENTPROTO */ 933 934 noident: 935 (void) strcpy(hbuf, RealHostName); 936 937 finish: 938 if (RealHostName[0] != '[') 939 { 940 p = &hbuf[strlen(hbuf)]; 941 (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 942 } 943 if (tTd(9, 1)) 944 printf("getauthinfo: %s\n", hbuf); 945 return hbuf; 946 } 947 /* 948 ** MAPHOSTNAME -- turn a hostname into canonical form 949 ** 950 ** Parameters: 951 ** map -- a pointer to this map (unused). 952 ** hbuf -- a buffer containing a hostname. 953 ** hbsize -- the size of hbuf. 954 ** avp -- unused -- for compatibility with other mapping 955 ** functions. 956 ** statp -- an exit status (out parameter) -- set to 957 ** EX_TEMPFAIL if the name server is unavailable. 958 ** 959 ** Returns: 960 ** The mapping, if found. 961 ** NULL if no mapping found. 962 ** 963 ** Side Effects: 964 ** Looks up the host specified in hbuf. If it is not 965 ** the canonical name for that host, return the canonical 966 ** name. 967 */ 968 969 char * 970 maphostname(map, hbuf, hbsize, avp, statp) 971 MAP *map; 972 char *hbuf; 973 int hbsize; 974 char **avp; 975 int *statp; 976 { 977 register struct hostent *hp; 978 u_long in_addr; 979 char *cp; 980 int i; 981 register STAB *s; 982 extern struct hostent *gethostbyaddr(); 983 extern int h_errno; 984 985 /* allow room for null */ 986 hbsize--; 987 988 /* 989 ** See if we have already looked up this name. If so, just 990 ** return it. 991 */ 992 993 s = stab(hbuf, ST_NAMECANON, ST_ENTER); 994 if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 995 { 996 if (tTd(9, 1)) 997 printf("maphostname(%s, %d) => CACHE %s\n", 998 hbuf, hbsize, s->s_namecanon.nc_cname); 999 errno = s->s_namecanon.nc_errno; 1000 h_errno = s->s_namecanon.nc_herrno; 1001 *statp = s->s_namecanon.nc_stat; 1002 return s->s_namecanon.nc_cname; 1003 } 1004 1005 /* 1006 ** If first character is a bracket, then it is an address 1007 ** lookup. Address is copied into a temporary buffer to 1008 ** strip the brackets and to preserve hbuf if address is 1009 ** unknown. 1010 */ 1011 1012 if (*hbuf != '[') 1013 { 1014 extern bool getcanonname(); 1015 1016 if (tTd(9, 1)) 1017 printf("maphostname(%s, %d) => ", hbuf, hbsize); 1018 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1019 if (getcanonname(hbuf, hbsize)) 1020 { 1021 if (tTd(9, 1)) 1022 printf("%s\n", hbuf); 1023 s->s_namecanon.nc_cname = newstr(hbuf); 1024 return hbuf; 1025 } 1026 else 1027 { 1028 register struct hostent *hp; 1029 1030 if (tTd(9, 1)) 1031 printf("FAIL (%d)\n", h_errno); 1032 s->s_namecanon.nc_errno = errno; 1033 s->s_namecanon.nc_herrno = h_errno; 1034 switch (h_errno) 1035 { 1036 case TRY_AGAIN: 1037 if (UseNameServer) 1038 { 1039 char *msg = "Recipient domain nameserver timed out"; 1040 1041 message(msg); 1042 if (CurEnv->e_message == NULL) 1043 CurEnv->e_message = msg; 1044 } 1045 *statp = EX_TEMPFAIL; 1046 break; 1047 1048 case HOST_NOT_FOUND: 1049 *statp = EX_NOHOST; 1050 break; 1051 1052 case NO_RECOVERY: 1053 *statp = EX_SOFTWARE; 1054 break; 1055 1056 default: 1057 *statp = EX_UNAVAILABLE; 1058 break; 1059 } 1060 s->s_namecanon.nc_stat = *statp; 1061 if (*statp != EX_TEMPFAIL || UseNameServer) 1062 return NULL; 1063 1064 /* 1065 ** Try to look it up in /etc/hosts 1066 */ 1067 1068 hp = gethostbyname(hbuf); 1069 if (hp == NULL) 1070 { 1071 /* no dice there either */ 1072 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1073 return NULL; 1074 } 1075 1076 s->s_namecanon.nc_stat = *statp = EX_OK; 1077 s->s_namecanon.nc_cname = newstr(hp->h_name); 1078 return hp->h_name; 1079 } 1080 } 1081 if ((cp = strchr(hbuf, ']')) == NULL) 1082 return (NULL); 1083 *cp = '\0'; 1084 in_addr = inet_addr(&hbuf[1]); 1085 1086 /* check to see if this is one of our addresses */ 1087 for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 1088 { 1089 if (MyIpAddrs[i].s_addr == in_addr) 1090 { 1091 strncpy(hbuf, MyHostName, hbsize); 1092 hbuf[hbsize] = '\0'; 1093 return hbuf; 1094 } 1095 } 1096 1097 /* nope -- ask the name server */ 1098 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 1099 s->s_namecanon.nc_errno = errno; 1100 s->s_namecanon.nc_herrno = h_errno; 1101 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1102 if (hp == NULL) 1103 { 1104 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1105 return (NULL); 1106 } 1107 1108 /* found a match -- copy out */ 1109 s->s_namecanon.nc_cname = newstr(hp->h_name); 1110 if (strlen(hp->h_name) > hbsize) 1111 hp->h_name[hbsize] = '\0'; 1112 (void) strcpy(hbuf, hp->h_name); 1113 s->s_namecanon.nc_stat = *statp = EX_OK; 1114 return hbuf; 1115 } 1116 /* 1117 ** ANYNET_NTOA -- convert a network address to printable form. 1118 ** 1119 ** Parameters: 1120 ** sap -- a pointer to a sockaddr structure. 1121 ** 1122 ** Returns: 1123 ** A printable version of that sockaddr. 1124 */ 1125 1126 char * 1127 anynet_ntoa(sap) 1128 register SOCKADDR *sap; 1129 { 1130 register char *bp; 1131 register char *ap; 1132 int l; 1133 static char buf[80]; 1134 1135 /* check for null/zero family */ 1136 if (sap == NULL) 1137 return "NULLADDR"; 1138 if (sap->sa.sa_family == 0) 1139 return "0"; 1140 1141 #ifdef NETINET 1142 if (sap->sa.sa_family == AF_INET) 1143 { 1144 extern char *inet_ntoa(); 1145 1146 return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 1147 } 1148 #endif 1149 1150 /* unknown family -- just dump bytes */ 1151 (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1152 bp = &buf[strlen(buf)]; 1153 ap = sap->sa.sa_data; 1154 for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1155 { 1156 (void) sprintf(bp, "%02x:", *ap++ & 0377); 1157 bp += 3; 1158 } 1159 *--bp = '\0'; 1160 return buf; 1161 } 1162 /* 1163 ** HOSTNAMEBYANYADDR -- return name of host based on address 1164 ** 1165 ** Parameters: 1166 ** sap -- SOCKADDR pointer 1167 ** 1168 ** Returns: 1169 ** text representation of host name. 1170 ** 1171 ** Side Effects: 1172 ** none. 1173 */ 1174 1175 char * 1176 hostnamebyanyaddr(sap) 1177 register SOCKADDR *sap; 1178 { 1179 register struct hostent *hp; 1180 1181 #ifdef NAMED_BIND 1182 int saveretry; 1183 1184 /* shorten name server timeout to avoid higher level timeouts */ 1185 saveretry = _res.retry; 1186 _res.retry = 3; 1187 #endif /* NAMED_BIND */ 1188 1189 switch (sap->sa.sa_family) 1190 { 1191 #ifdef NETINET 1192 case AF_INET: 1193 hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1194 sizeof sap->sin.sin_addr, 1195 AF_INET); 1196 break; 1197 #endif 1198 1199 #ifdef NETISO 1200 case AF_ISO: 1201 hp = gethostbyaddr((char *) &sap->siso.siso_addr, 1202 sizeof sap->siso.siso_addr, 1203 AF_ISO); 1204 break; 1205 #endif 1206 1207 default: 1208 hp = gethostbyaddr(sap->sa.sa_data, 1209 sizeof sap->sa.sa_data, 1210 sap->sa.sa_family); 1211 break; 1212 } 1213 1214 #ifdef NAMED_BIND 1215 _res.retry = saveretry; 1216 #endif /* NAMED_BIND */ 1217 1218 if (hp != NULL) 1219 return hp->h_name; 1220 else 1221 { 1222 /* produce a dotted quad */ 1223 static char buf[512]; 1224 1225 (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 1226 return buf; 1227 } 1228 } 1229 1230 # else /* DAEMON */ 1231 /* code for systems without sophisticated networking */ 1232 1233 /* 1234 ** MYHOSTNAME -- stub version for case of no daemon code. 1235 ** 1236 ** Can't convert to upper case here because might be a UUCP name. 1237 ** 1238 ** Mark, you can change this to be anything you want...... 1239 */ 1240 1241 char ** 1242 myhostname(hostbuf, size) 1243 char hostbuf[]; 1244 int size; 1245 { 1246 register FILE *f; 1247 1248 hostbuf[0] = '\0'; 1249 f = fopen("/usr/include/whoami", "r"); 1250 if (f != NULL) 1251 { 1252 (void) fgets(hostbuf, size, f); 1253 fixcrlf(hostbuf, TRUE); 1254 (void) fclose(f); 1255 } 1256 return (NULL); 1257 } 1258 /* 1259 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1260 ** 1261 ** Parameters: 1262 ** fd -- the descriptor 1263 ** 1264 ** Returns: 1265 ** The host name associated with this descriptor, if it can 1266 ** be determined. 1267 ** NULL otherwise. 1268 ** 1269 ** Side Effects: 1270 ** none 1271 */ 1272 1273 char * 1274 getauthinfo(fd) 1275 int fd; 1276 { 1277 return NULL; 1278 } 1279 /* 1280 ** MAPHOSTNAME -- turn a hostname into canonical form 1281 ** 1282 ** Parameters: 1283 ** map -- a pointer to the database map. 1284 ** hbuf -- a buffer containing a hostname. 1285 ** hbsize -- size of hbuf. 1286 ** avp -- a pointer to a (cf file defined) argument vector. 1287 ** statp -- an exit status (out parameter). 1288 ** 1289 ** Returns: 1290 ** mapped host name 1291 ** FALSE otherwise. 1292 ** 1293 ** Side Effects: 1294 ** Looks up the host specified in hbuf. If it is not 1295 ** the canonical name for that host, replace it with 1296 ** the canonical name. If the name is unknown, or it 1297 ** is already the canonical name, leave it unchanged. 1298 */ 1299 1300 /*ARGSUSED*/ 1301 char * 1302 maphostname(map, hbuf, hbsize, avp, statp) 1303 MAP *map; 1304 char *hbuf; 1305 int hbsize; 1306 char **avp; 1307 char *statp; 1308 { 1309 register struct hostent *hp; 1310 1311 hp = gethostbyname(hbuf); 1312 if (hp != NULL) 1313 return hp->h_name; 1314 *statp = EX_NOHOST; 1315 return NULL; 1316 } 1317 1318 #endif /* DAEMON */ 1319