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.46 (Berkeley) 05/11/93 (with daemon mode)"; 16 #else 17 static char sccsid[] = "@(#)daemon.c 6.46 (Berkeley) 05/11/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 errno = s->s_namecanon.nc_errno; 997 h_errno = s->s_namecanon.nc_herrno; 998 *statp = s->s_namecanon.nc_stat; 999 return s->s_namecanon.nc_cname; 1000 } 1001 1002 /* 1003 ** If first character is a bracket, then it is an address 1004 ** lookup. Address is copied into a temporary buffer to 1005 ** strip the brackets and to preserve hbuf if address is 1006 ** unknown. 1007 */ 1008 1009 if (*hbuf != '[') 1010 { 1011 extern bool getcanonname(); 1012 1013 if (tTd(9, 1)) 1014 printf("maphostname(%s, %d) => ", hbuf, hbsize); 1015 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1016 if (getcanonname(hbuf, hbsize)) 1017 { 1018 if (tTd(9, 1)) 1019 printf("%s\n", hbuf); 1020 s->s_namecanon.nc_cname = newstr(hbuf); 1021 return hbuf; 1022 } 1023 else 1024 { 1025 register struct hostent *hp; 1026 1027 if (tTd(9, 1)) 1028 printf("FAIL (%d)\n", h_errno); 1029 s->s_namecanon.nc_errno = errno; 1030 s->s_namecanon.nc_herrno = h_errno; 1031 switch (h_errno) 1032 { 1033 case TRY_AGAIN: 1034 if (UseNameServer) 1035 { 1036 char *msg = "Recipient domain nameserver timed out"; 1037 1038 message(msg); 1039 if (CurEnv->e_message == NULL) 1040 CurEnv->e_message = msg; 1041 } 1042 *statp = EX_TEMPFAIL; 1043 break; 1044 1045 case HOST_NOT_FOUND: 1046 *statp = EX_NOHOST; 1047 break; 1048 1049 case NO_RECOVERY: 1050 *statp = EX_SOFTWARE; 1051 break; 1052 1053 default: 1054 *statp = EX_UNAVAILABLE; 1055 break; 1056 } 1057 s->s_namecanon.nc_stat = *statp; 1058 if (*statp != EX_TEMPFAIL || UseNameServer) 1059 return NULL; 1060 1061 /* 1062 ** Try to look it up in /etc/hosts 1063 */ 1064 1065 hp = gethostbyname(hbuf); 1066 if (hp == NULL) 1067 { 1068 /* no dice there either */ 1069 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1070 return NULL; 1071 } 1072 1073 s->s_namecanon.nc_stat = *statp = EX_OK; 1074 s->s_namecanon.nc_cname = newstr(hp->h_name); 1075 return hp->h_name; 1076 } 1077 } 1078 if ((cp = strchr(hbuf, ']')) == NULL) 1079 return (NULL); 1080 *cp = '\0'; 1081 in_addr = inet_addr(&hbuf[1]); 1082 1083 /* check to see if this is one of our addresses */ 1084 for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 1085 { 1086 if (MyIpAddrs[i].s_addr == in_addr) 1087 { 1088 strncpy(hbuf, MyHostName, hbsize); 1089 hbuf[hbsize] = '\0'; 1090 return hbuf; 1091 } 1092 } 1093 1094 /* nope -- ask the name server */ 1095 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 1096 s->s_namecanon.nc_errno = errno; 1097 s->s_namecanon.nc_herrno = h_errno; 1098 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1099 if (hp == NULL) 1100 { 1101 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1102 return (NULL); 1103 } 1104 1105 /* found a match -- copy out */ 1106 s->s_namecanon.nc_cname = newstr(hp->h_name); 1107 if (strlen(hp->h_name) > hbsize) 1108 hp->h_name[hbsize] = '\0'; 1109 (void) strcpy(hbuf, hp->h_name); 1110 s->s_namecanon.nc_stat = *statp = EX_OK; 1111 return hbuf; 1112 } 1113 /* 1114 ** ANYNET_NTOA -- convert a network address to printable form. 1115 ** 1116 ** Parameters: 1117 ** sap -- a pointer to a sockaddr structure. 1118 ** 1119 ** Returns: 1120 ** A printable version of that sockaddr. 1121 */ 1122 1123 char * 1124 anynet_ntoa(sap) 1125 register SOCKADDR *sap; 1126 { 1127 register char *bp; 1128 register char *ap; 1129 int l; 1130 static char buf[80]; 1131 1132 /* check for null/zero family */ 1133 if (sap == NULL) 1134 return "NULLADDR"; 1135 if (sap->sa.sa_family == 0) 1136 return "0"; 1137 1138 #ifdef NETINET 1139 if (sap->sa.sa_family == AF_INET) 1140 { 1141 extern char *inet_ntoa(); 1142 1143 return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 1144 } 1145 #endif 1146 1147 /* unknown family -- just dump bytes */ 1148 (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1149 bp = &buf[strlen(buf)]; 1150 ap = sap->sa.sa_data; 1151 for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1152 { 1153 (void) sprintf(bp, "%02x:", *ap++ & 0377); 1154 bp += 3; 1155 } 1156 *--bp = '\0'; 1157 return buf; 1158 } 1159 /* 1160 ** HOSTNAMEBYANYADDR -- return name of host based on address 1161 ** 1162 ** Parameters: 1163 ** sap -- SOCKADDR pointer 1164 ** 1165 ** Returns: 1166 ** text representation of host name. 1167 ** 1168 ** Side Effects: 1169 ** none. 1170 */ 1171 1172 char * 1173 hostnamebyanyaddr(sap) 1174 register SOCKADDR *sap; 1175 { 1176 register struct hostent *hp; 1177 1178 #ifdef NAMED_BIND 1179 int saveretry; 1180 1181 /* shorten name server timeout to avoid higher level timeouts */ 1182 saveretry = _res.retry; 1183 _res.retry = 3; 1184 #endif /* NAMED_BIND */ 1185 1186 switch (sap->sa.sa_family) 1187 { 1188 #ifdef NETINET 1189 case AF_INET: 1190 hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1191 sizeof sap->sin.sin_addr, 1192 AF_INET); 1193 break; 1194 #endif 1195 1196 #ifdef NETISO 1197 case AF_ISO: 1198 hp = gethostbyaddr((char *) &sap->siso.siso_addr, 1199 sizeof sap->siso.siso_addr, 1200 AF_ISO); 1201 break; 1202 #endif 1203 1204 default: 1205 hp = gethostbyaddr(sap->sa.sa_data, 1206 sizeof sap->sa.sa_data, 1207 sap->sa.sa_family); 1208 break; 1209 } 1210 1211 #ifdef NAMED_BIND 1212 _res.retry = saveretry; 1213 #endif /* NAMED_BIND */ 1214 1215 if (hp != NULL) 1216 return hp->h_name; 1217 else 1218 { 1219 /* produce a dotted quad */ 1220 static char buf[512]; 1221 1222 (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 1223 return buf; 1224 } 1225 } 1226 1227 # else /* DAEMON */ 1228 /* code for systems without sophisticated networking */ 1229 1230 /* 1231 ** MYHOSTNAME -- stub version for case of no daemon code. 1232 ** 1233 ** Can't convert to upper case here because might be a UUCP name. 1234 ** 1235 ** Mark, you can change this to be anything you want...... 1236 */ 1237 1238 char ** 1239 myhostname(hostbuf, size) 1240 char hostbuf[]; 1241 int size; 1242 { 1243 register FILE *f; 1244 1245 hostbuf[0] = '\0'; 1246 f = fopen("/usr/include/whoami", "r"); 1247 if (f != NULL) 1248 { 1249 (void) fgets(hostbuf, size, f); 1250 fixcrlf(hostbuf, TRUE); 1251 (void) fclose(f); 1252 } 1253 return (NULL); 1254 } 1255 /* 1256 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1257 ** 1258 ** Parameters: 1259 ** fd -- the descriptor 1260 ** 1261 ** Returns: 1262 ** The host name associated with this descriptor, if it can 1263 ** be determined. 1264 ** NULL otherwise. 1265 ** 1266 ** Side Effects: 1267 ** none 1268 */ 1269 1270 char * 1271 getauthinfo(fd) 1272 int fd; 1273 { 1274 return NULL; 1275 } 1276 /* 1277 ** MAPHOSTNAME -- turn a hostname into canonical form 1278 ** 1279 ** Parameters: 1280 ** map -- a pointer to the database map. 1281 ** hbuf -- a buffer containing a hostname. 1282 ** hbsize -- size of hbuf. 1283 ** avp -- a pointer to a (cf file defined) argument vector. 1284 ** statp -- an exit status (out parameter). 1285 ** 1286 ** Returns: 1287 ** mapped host name 1288 ** FALSE otherwise. 1289 ** 1290 ** Side Effects: 1291 ** Looks up the host specified in hbuf. If it is not 1292 ** the canonical name for that host, replace it with 1293 ** the canonical name. If the name is unknown, or it 1294 ** is already the canonical name, leave it unchanged. 1295 */ 1296 1297 /*ARGSUSED*/ 1298 char * 1299 maphostname(map, hbuf, hbsize, avp, statp) 1300 MAP *map; 1301 char *hbuf; 1302 int hbsize; 1303 char **avp; 1304 char *statp; 1305 { 1306 register struct hostent *hp; 1307 1308 hp = gethostbyname(hbuf); 1309 if (hp != NULL) 1310 return hp->h_name; 1311 *statp = EX_NOHOST; 1312 return NULL; 1313 } 1314 1315 #endif /* DAEMON */ 1316