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