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