1 /* 2 * Copyright (c) 1983, 1995 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.84 (Berkeley) 04/25/95 (with daemon mode)"; 15 #else 16 static char sccsid[] = "@(#)daemon.c 8.84 (Berkeley) 04/25/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 mci->mci_status = "5.1.2"; 677 return (EX_NOHOST); 678 } 679 #ifdef NETINET 680 addr.sin.sin_family = AF_INET; /*XXX*/ 681 addr.sin.sin_addr.s_addr = hid; 682 #endif 683 } 684 else 685 { 686 register char *p = &host[strlen(host) - 1]; 687 688 hp = sm_gethostbyname(host); 689 if (hp == NULL && *p == '.') 690 { 691 #if NAMED_BIND 692 int oldopts = _res.options; 693 694 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 695 #endif 696 *p = '\0'; 697 hp = sm_gethostbyname(host); 698 *p = '.'; 699 #if NAMED_BIND 700 _res.options = oldopts; 701 #endif 702 } 703 gothostent: 704 if (hp == NULL) 705 { 706 #if NAMED_BIND 707 /* check for name server timeouts */ 708 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN || 709 (errno == ECONNREFUSED && UseNameServer)) 710 { 711 mci->mci_status = "4.4.3"; 712 return (EX_TEMPFAIL); 713 } 714 #endif 715 return (EX_NOHOST); 716 } 717 addr.sa.sa_family = hp->h_addrtype; 718 switch (hp->h_addrtype) 719 { 720 #ifdef NETINET 721 case AF_INET: 722 bcopy(hp->h_addr, 723 &addr.sin.sin_addr, 724 INADDRSZ); 725 break; 726 #endif 727 728 default: 729 bcopy(hp->h_addr, 730 addr.sa.sa_data, 731 hp->h_length); 732 break; 733 } 734 i = 1; 735 } 736 737 /* 738 ** Determine the port number. 739 */ 740 741 if (port != 0) 742 port = htons(port); 743 else 744 { 745 register struct servent *sp = getservbyname("smtp", "tcp"); 746 747 if (sp == NULL) 748 { 749 #ifdef LOG 750 if (LogLevel > 2) 751 syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown"); 752 #endif 753 port = htons(25); 754 } 755 else 756 port = sp->s_port; 757 } 758 759 switch (addr.sa.sa_family) 760 { 761 #ifdef NETINET 762 case AF_INET: 763 addr.sin.sin_port = port; 764 addrlen = sizeof (struct sockaddr_in); 765 break; 766 #endif 767 768 #ifdef NETISO 769 case AF_ISO: 770 /* assume two byte transport selector */ 771 bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 772 addrlen = sizeof (struct sockaddr_iso); 773 break; 774 #endif 775 776 default: 777 syserr("Can't connect to address family %d", addr.sa.sa_family); 778 return (EX_NOHOST); 779 } 780 781 /* 782 ** Try to actually open the connection. 783 */ 784 785 #ifdef XLA 786 /* if too many connections, don't bother trying */ 787 if (!xla_noqueue_ok(host)) 788 return EX_TEMPFAIL; 789 #endif 790 791 firstconnect = TRUE; 792 for (;;) 793 { 794 if (tTd(16, 1)) 795 printf("makeconnection (%s [%s])\n", 796 host, anynet_ntoa(&addr)); 797 798 /* save for logging */ 799 CurHostAddr = addr; 800 801 if (usesecureport) 802 { 803 int rport = IPPORT_RESERVED - 1; 804 805 s = rresvport(&rport); 806 } 807 else 808 { 809 s = socket(AF_INET, SOCK_STREAM, 0); 810 } 811 if (s < 0) 812 { 813 sav_errno = errno; 814 syserr("makeconnection: no socket"); 815 goto failure; 816 } 817 818 #ifdef SO_SNDBUF 819 if (TcpSndBufferSize > 0) 820 { 821 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 822 (char *) &TcpSndBufferSize, 823 sizeof(TcpSndBufferSize)) < 0) 824 syserr("makeconnection: setsockopt(SO_SNDBUF)"); 825 } 826 #endif 827 828 if (tTd(16, 1)) 829 printf("makeconnection: fd=%d\n", s); 830 831 /* turn on network debugging? */ 832 if (tTd(16, 101)) 833 { 834 int on = 1; 835 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 836 (char *)&on, sizeof on); 837 } 838 if (CurEnv->e_xfp != NULL) 839 (void) fflush(CurEnv->e_xfp); /* for debugging */ 840 errno = 0; /* for debugging */ 841 if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 842 break; 843 844 /* if running demand-dialed connection, try again */ 845 if (DialDelay > 0 && firstconnect) 846 { 847 if (tTd(16, 1)) 848 printf("Connect failed (%s); trying again...\n", 849 errstring(sav_errno)); 850 firstconnect = FALSE; 851 sleep(DialDelay); 852 continue; 853 } 854 855 /* couldn't connect.... figure out why */ 856 sav_errno = errno; 857 (void) close(s); 858 if (hp != NULL && hp->h_addr_list[i]) 859 { 860 if (tTd(16, 1)) 861 printf("Connect failed (%s); trying new address....\n", 862 errstring(sav_errno)); 863 switch (addr.sa.sa_family) 864 { 865 #ifdef NETINET 866 case AF_INET: 867 bcopy(hp->h_addr_list[i++], 868 &addr.sin.sin_addr, 869 INADDRSZ); 870 break; 871 #endif 872 873 default: 874 bcopy(hp->h_addr_list[i++], 875 addr.sa.sa_data, 876 hp->h_length); 877 break; 878 } 879 continue; 880 } 881 882 /* failure, decide if temporary or not */ 883 failure: 884 #ifdef XLA 885 xla_host_end(host); 886 #endif 887 if (transienterror(sav_errno)) 888 return EX_TEMPFAIL; 889 else 890 { 891 message("%s", errstring(sav_errno)); 892 return (EX_UNAVAILABLE); 893 } 894 } 895 896 /* connection ok, put it into canonical form */ 897 if ((mci->mci_out = fdopen(s, "w")) == NULL || 898 (s = dup(s)) < 0 || 899 (mci->mci_in = fdopen(s, "r")) == NULL) 900 { 901 syserr("cannot open SMTP client channel, fd=%d", s); 902 return EX_TEMPFAIL; 903 } 904 905 return (EX_OK); 906 } 907 /* 908 ** MYHOSTNAME -- return the name of this host. 909 ** 910 ** Parameters: 911 ** hostbuf -- a place to return the name of this host. 912 ** size -- the size of hostbuf. 913 ** 914 ** Returns: 915 ** A list of aliases for this host. 916 ** 917 ** Side Effects: 918 ** Adds numeric codes to $=w. 919 */ 920 921 struct hostent * 922 myhostname(hostbuf, size) 923 char hostbuf[]; 924 int size; 925 { 926 register struct hostent *hp; 927 extern bool getcanonname(); 928 extern int h_errno; 929 930 if (gethostname(hostbuf, size) < 0) 931 { 932 (void) strcpy(hostbuf, "localhost"); 933 } 934 hp = sm_gethostbyname(hostbuf); 935 if (hp == NULL) 936 return NULL; 937 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL) 938 { 939 (void) strncpy(hostbuf, hp->h_name, size - 1); 940 hostbuf[size - 1] = '\0'; 941 } 942 943 #if NAMED_BIND 944 /* 945 ** If still no dot, try DNS directly (i.e., avoid NIS problems). 946 ** This ought to be driven from the configuration file, but 947 ** we are called before the configuration is read. We could 948 ** check for an /etc/resolv.conf file, but that isn't required. 949 ** All in all, a bit of a mess. 950 */ 951 952 if (strchr(hostbuf, '.') == NULL && 953 !getcanonname(hostbuf, size, TRUE) && 954 h_errno == TRY_AGAIN) 955 { 956 /* try twice in case name server not yet started up */ 957 message("My unqualifed host name (%s) unknown to DNS; sleeping for retry", 958 hostbuf); 959 sleep(60); 960 if (!getcanonname(hostbuf, size, TRUE)) 961 errno = h_errno + E_DNSBASE; 962 } 963 #endif 964 return (hp); 965 } 966 /* 967 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 968 ** 969 ** Uses RFC1413 protocol to try to get info from the other end. 970 ** 971 ** Parameters: 972 ** fd -- the descriptor 973 ** 974 ** Returns: 975 ** The user@host information associated with this descriptor. 976 */ 977 978 static jmp_buf CtxAuthTimeout; 979 980 static void 981 authtimeout() 982 { 983 longjmp(CtxAuthTimeout, 1); 984 } 985 986 char * 987 getauthinfo(fd) 988 int fd; 989 { 990 int falen; 991 register char *p; 992 SOCKADDR la; 993 int lalen; 994 register struct servent *sp; 995 int s; 996 int i; 997 EVENT *ev; 998 int nleft; 999 char ibuf[MAXNAME + 1]; 1000 static char hbuf[MAXNAME * 2 + 2]; 1001 extern char *hostnamebyanyaddr(); 1002 extern char RealUserName[]; /* main.c */ 1003 1004 falen = sizeof RealHostAddr; 1005 if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 || 1006 falen <= 0 || RealHostAddr.sa.sa_family == 0) 1007 { 1008 (void) sprintf(hbuf, "%s@localhost", RealUserName); 1009 if (tTd(9, 1)) 1010 printf("getauthinfo: %s\n", hbuf); 1011 return hbuf; 1012 } 1013 1014 if (RealHostName == NULL) 1015 { 1016 /* translate that to a host name */ 1017 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 1018 } 1019 1020 if (TimeOuts.to_ident == 0) 1021 goto noident; 1022 1023 lalen = sizeof la; 1024 if (RealHostAddr.sa.sa_family != AF_INET || 1025 getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 1026 la.sa.sa_family != AF_INET) 1027 { 1028 /* no ident info */ 1029 goto noident; 1030 } 1031 1032 /* create ident query */ 1033 (void) sprintf(ibuf, "%d,%d\r\n", 1034 ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); 1035 1036 /* create local address */ 1037 la.sin.sin_port = 0; 1038 1039 /* create foreign address */ 1040 sp = getservbyname("auth", "tcp"); 1041 if (sp != NULL) 1042 RealHostAddr.sin.sin_port = sp->s_port; 1043 else 1044 RealHostAddr.sin.sin_port = htons(113); 1045 1046 s = -1; 1047 if (setjmp(CtxAuthTimeout) != 0) 1048 { 1049 if (s >= 0) 1050 (void) close(s); 1051 goto noident; 1052 } 1053 1054 /* put a timeout around the whole thing */ 1055 ev = setevent(TimeOuts.to_ident, authtimeout, 0); 1056 1057 /* connect to foreign IDENT server using same address as SMTP socket */ 1058 s = socket(AF_INET, SOCK_STREAM, 0); 1059 if (s < 0) 1060 { 1061 clrevent(ev); 1062 goto noident; 1063 } 1064 if (bind(s, &la.sa, sizeof la.sin) < 0 || 1065 connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) 1066 { 1067 goto closeident; 1068 } 1069 1070 if (tTd(9, 10)) 1071 printf("getauthinfo: sent %s", ibuf); 1072 1073 /* send query */ 1074 if (write(s, ibuf, strlen(ibuf)) < 0) 1075 goto closeident; 1076 1077 /* get result */ 1078 p = &ibuf[0]; 1079 nleft = sizeof ibuf - 1; 1080 while ((i = read(s, p, nleft)) > 0) 1081 { 1082 p += i; 1083 nleft -= i; 1084 } 1085 (void) close(s); 1086 clrevent(ev); 1087 if (i < 0 || p == &ibuf[0]) 1088 goto noident; 1089 1090 if (*--p == '\n' && *--p == '\r') 1091 p--; 1092 *++p = '\0'; 1093 1094 if (tTd(9, 3)) 1095 printf("getauthinfo: got %s\n", ibuf); 1096 1097 /* parse result */ 1098 p = strchr(ibuf, ':'); 1099 if (p == NULL) 1100 { 1101 /* malformed response */ 1102 goto noident; 1103 } 1104 while (isascii(*++p) && isspace(*p)) 1105 continue; 1106 if (strncasecmp(p, "userid", 6) != 0) 1107 { 1108 /* presumably an error string */ 1109 goto noident; 1110 } 1111 p += 6; 1112 while (isascii(*p) && isspace(*p)) 1113 p++; 1114 if (*p++ != ':') 1115 { 1116 /* either useridxx or malformed response */ 1117 goto noident; 1118 } 1119 1120 /* p now points to the OSTYPE field */ 1121 while (isascii(*p) && isspace(*p)) 1122 p++; 1123 if (strncasecmp(p, "other", 5) == 0 && 1124 (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0')) 1125 { 1126 /* not useful information */ 1127 goto noident; 1128 } 1129 p = strchr(p, ':'); 1130 if (p == NULL) 1131 { 1132 /* malformed response */ 1133 goto noident; 1134 } 1135 1136 /* 1413 says don't do this -- but it's broken otherwise */ 1137 while (isascii(*++p) && isspace(*p)) 1138 continue; 1139 1140 /* p now points to the authenticated name -- copy carefully */ 1141 cleanstrcpy(hbuf, p, MAXNAME); 1142 i = strlen(hbuf); 1143 hbuf[i++] = '@'; 1144 strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName); 1145 goto finish; 1146 1147 closeident: 1148 (void) close(s); 1149 clrevent(ev); 1150 1151 noident: 1152 if (RealHostName == NULL) 1153 { 1154 if (tTd(9, 1)) 1155 printf("getauthinfo: NULL\n"); 1156 return NULL; 1157 } 1158 (void) strcpy(hbuf, RealHostName); 1159 1160 finish: 1161 if (RealHostName != NULL && RealHostName[0] != '[') 1162 { 1163 p = &hbuf[strlen(hbuf)]; 1164 (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 1165 } 1166 if (tTd(9, 1)) 1167 printf("getauthinfo: %s\n", hbuf); 1168 return hbuf; 1169 } 1170 /* 1171 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 1172 ** 1173 ** Parameters: 1174 ** map -- a pointer to this map (unused). 1175 ** name -- the (presumably unqualified) hostname. 1176 ** av -- unused -- for compatibility with other mapping 1177 ** functions. 1178 ** statp -- an exit status (out parameter) -- set to 1179 ** EX_TEMPFAIL if the name server is unavailable. 1180 ** 1181 ** Returns: 1182 ** The mapping, if found. 1183 ** NULL if no mapping found. 1184 ** 1185 ** Side Effects: 1186 ** Looks up the host specified in hbuf. If it is not 1187 ** the canonical name for that host, return the canonical 1188 ** name. 1189 */ 1190 1191 char * 1192 host_map_lookup(map, name, av, statp) 1193 MAP *map; 1194 char *name; 1195 char **av; 1196 int *statp; 1197 { 1198 register struct hostent *hp; 1199 struct in_addr in_addr; 1200 char *cp; 1201 register STAB *s; 1202 char hbuf[MAXNAME + 1]; 1203 #if NAMED_BIND 1204 extern int h_errno; 1205 #endif 1206 1207 /* 1208 ** See if we have already looked up this name. If so, just 1209 ** return it. 1210 */ 1211 1212 s = stab(name, ST_NAMECANON, ST_ENTER); 1213 if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 1214 { 1215 if (tTd(9, 1)) 1216 printf("host_map_lookup(%s) => CACHE %s\n", 1217 name, s->s_namecanon.nc_cname); 1218 errno = s->s_namecanon.nc_errno; 1219 #if NAMED_BIND 1220 h_errno = s->s_namecanon.nc_herrno; 1221 #endif 1222 *statp = s->s_namecanon.nc_stat; 1223 if (*statp == EX_TEMPFAIL) 1224 { 1225 CurEnv->e_status = "4.4.3"; 1226 usrerr("451 %s: Name server timeout", 1227 shortenstring(name, 33)); 1228 } 1229 return s->s_namecanon.nc_cname; 1230 } 1231 1232 /* 1233 ** If first character is a bracket, then it is an address 1234 ** lookup. Address is copied into a temporary buffer to 1235 ** strip the brackets and to preserve name if address is 1236 ** unknown. 1237 */ 1238 1239 if (*name != '[') 1240 { 1241 extern bool getcanonname(); 1242 1243 if (tTd(9, 1)) 1244 printf("host_map_lookup(%s) => ", name); 1245 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1246 if (strlen(name) < sizeof hbuf) 1247 (void) strcpy(hbuf, name); 1248 else 1249 { 1250 bcopy(name, hbuf, sizeof hbuf - 1); 1251 hbuf[sizeof hbuf - 1] = '\0'; 1252 } 1253 if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon)) 1254 { 1255 if (tTd(9, 1)) 1256 printf("%s\n", hbuf); 1257 cp = map_rewrite(map, hbuf, strlen(hbuf), av); 1258 s->s_namecanon.nc_cname = newstr(cp); 1259 return cp; 1260 } 1261 else 1262 { 1263 register struct hostent *hp; 1264 1265 s->s_namecanon.nc_errno = errno; 1266 #if NAMED_BIND 1267 s->s_namecanon.nc_herrno = h_errno; 1268 if (tTd(9, 1)) 1269 printf("FAIL (%d)\n", h_errno); 1270 switch (h_errno) 1271 { 1272 case TRY_AGAIN: 1273 if (UseNameServer) 1274 { 1275 CurEnv->e_status = "4.4.3"; 1276 usrerr("451 %s: Name server timeout", 1277 shortenstring(name, 33)); 1278 } 1279 *statp = EX_TEMPFAIL; 1280 break; 1281 1282 case HOST_NOT_FOUND: 1283 case NO_DATA: 1284 *statp = EX_NOHOST; 1285 break; 1286 1287 case NO_RECOVERY: 1288 *statp = EX_SOFTWARE; 1289 break; 1290 1291 default: 1292 *statp = EX_UNAVAILABLE; 1293 break; 1294 } 1295 #else 1296 if (tTd(9, 1)) 1297 printf("FAIL\n"); 1298 *statp = EX_NOHOST; 1299 #endif 1300 s->s_namecanon.nc_stat = *statp; 1301 if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) || 1302 UseNameServer) 1303 return NULL; 1304 1305 /* 1306 ** Try to look it up in /etc/hosts 1307 */ 1308 1309 hp = sm_gethostbyname(name); 1310 if (hp == NULL) 1311 { 1312 /* no dice there either */ 1313 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1314 return NULL; 1315 } 1316 1317 s->s_namecanon.nc_stat = *statp = EX_OK; 1318 cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1319 s->s_namecanon.nc_cname = newstr(cp); 1320 return cp; 1321 } 1322 } 1323 if ((cp = strchr(name, ']')) == NULL) 1324 return (NULL); 1325 *cp = '\0'; 1326 in_addr.s_addr = inet_addr(&name[1]); 1327 1328 /* nope -- ask the name server */ 1329 hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET); 1330 s->s_namecanon.nc_errno = errno; 1331 #if NAMED_BIND 1332 s->s_namecanon.nc_herrno = h_errno; 1333 #endif 1334 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 1335 if (hp == NULL) 1336 { 1337 s->s_namecanon.nc_stat = *statp = EX_NOHOST; 1338 return (NULL); 1339 } 1340 1341 /* found a match -- copy out */ 1342 cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 1343 s->s_namecanon.nc_stat = *statp = EX_OK; 1344 s->s_namecanon.nc_cname = newstr(cp); 1345 return cp; 1346 } 1347 /* 1348 ** ANYNET_NTOA -- convert a network address to printable form. 1349 ** 1350 ** Parameters: 1351 ** sap -- a pointer to a sockaddr structure. 1352 ** 1353 ** Returns: 1354 ** A printable version of that sockaddr. 1355 */ 1356 1357 char * 1358 anynet_ntoa(sap) 1359 register SOCKADDR *sap; 1360 { 1361 register char *bp; 1362 register char *ap; 1363 int l; 1364 static char buf[100]; 1365 1366 /* check for null/zero family */ 1367 if (sap == NULL) 1368 return "NULLADDR"; 1369 if (sap->sa.sa_family == 0) 1370 return "0"; 1371 1372 switch (sap->sa.sa_family) 1373 { 1374 #ifdef NETUNIX 1375 case AF_UNIX: 1376 if (sap->sunix.sun_path[0] != '\0') 1377 sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); 1378 else 1379 sprintf(buf, "[UNIX: localhost]"); 1380 return buf; 1381 #endif 1382 1383 #ifdef NETINET 1384 case AF_INET: 1385 return inet_ntoa(sap->sin.sin_addr); 1386 #endif 1387 1388 default: 1389 /* this case is only to ensure syntactic correctness */ 1390 break; 1391 } 1392 1393 /* unknown family -- just dump bytes */ 1394 (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 1395 bp = &buf[strlen(buf)]; 1396 ap = sap->sa.sa_data; 1397 for (l = sizeof sap->sa.sa_data; --l >= 0; ) 1398 { 1399 (void) sprintf(bp, "%02x:", *ap++ & 0377); 1400 bp += 3; 1401 } 1402 *--bp = '\0'; 1403 return buf; 1404 } 1405 /* 1406 ** HOSTNAMEBYANYADDR -- return name of host based on address 1407 ** 1408 ** Parameters: 1409 ** sap -- SOCKADDR pointer 1410 ** 1411 ** Returns: 1412 ** text representation of host name. 1413 ** 1414 ** Side Effects: 1415 ** none. 1416 */ 1417 1418 char * 1419 hostnamebyanyaddr(sap) 1420 register SOCKADDR *sap; 1421 { 1422 register struct hostent *hp; 1423 int saveretry; 1424 1425 #if NAMED_BIND 1426 /* shorten name server timeout to avoid higher level timeouts */ 1427 saveretry = _res.retry; 1428 _res.retry = 3; 1429 #endif /* NAMED_BIND */ 1430 1431 switch (sap->sa.sa_family) 1432 { 1433 #ifdef NETINET 1434 case AF_INET: 1435 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr, 1436 INADDRSZ, 1437 AF_INET); 1438 break; 1439 #endif 1440 1441 #ifdef NETISO 1442 case AF_ISO: 1443 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr, 1444 sizeof sap->siso.siso_addr, 1445 AF_ISO); 1446 break; 1447 #endif 1448 1449 case AF_UNIX: 1450 hp = NULL; 1451 break; 1452 1453 default: 1454 hp = sm_gethostbyaddr(sap->sa.sa_data, 1455 sizeof sap->sa.sa_data, 1456 sap->sa.sa_family); 1457 break; 1458 } 1459 1460 #if NAMED_BIND 1461 _res.retry = saveretry; 1462 #endif /* NAMED_BIND */ 1463 1464 if (hp != NULL) 1465 return hp->h_name; 1466 else 1467 { 1468 /* produce a dotted quad */ 1469 static char buf[512]; 1470 1471 (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 1472 return buf; 1473 } 1474 } 1475 1476 # else /* DAEMON */ 1477 /* code for systems without sophisticated networking */ 1478 1479 /* 1480 ** MYHOSTNAME -- stub version for case of no daemon code. 1481 ** 1482 ** Can't convert to upper case here because might be a UUCP name. 1483 ** 1484 ** Mark, you can change this to be anything you want...... 1485 */ 1486 1487 char ** 1488 myhostname(hostbuf, size) 1489 char hostbuf[]; 1490 int size; 1491 { 1492 register FILE *f; 1493 1494 hostbuf[0] = '\0'; 1495 f = fopen("/usr/include/whoami", "r"); 1496 if (f != NULL) 1497 { 1498 (void) fgets(hostbuf, size, f); 1499 fixcrlf(hostbuf, TRUE); 1500 (void) fclose(f); 1501 } 1502 return (NULL); 1503 } 1504 /* 1505 ** GETAUTHINFO -- get the real host name asociated with a file descriptor 1506 ** 1507 ** Parameters: 1508 ** fd -- the descriptor 1509 ** 1510 ** Returns: 1511 ** The host name associated with this descriptor, if it can 1512 ** be determined. 1513 ** NULL otherwise. 1514 ** 1515 ** Side Effects: 1516 ** none 1517 */ 1518 1519 char * 1520 getauthinfo(fd) 1521 int fd; 1522 { 1523 return NULL; 1524 } 1525 /* 1526 ** MAPHOSTNAME -- turn a hostname into canonical form 1527 ** 1528 ** Parameters: 1529 ** map -- a pointer to the database map. 1530 ** name -- a buffer containing a hostname. 1531 ** avp -- a pointer to a (cf file defined) argument vector. 1532 ** statp -- an exit status (out parameter). 1533 ** 1534 ** Returns: 1535 ** mapped host name 1536 ** FALSE otherwise. 1537 ** 1538 ** Side Effects: 1539 ** Looks up the host specified in name. If it is not 1540 ** the canonical name for that host, replace it with 1541 ** the canonical name. If the name is unknown, or it 1542 ** is already the canonical name, leave it unchanged. 1543 */ 1544 1545 /*ARGSUSED*/ 1546 char * 1547 host_map_lookup(map, name, avp, statp) 1548 MAP *map; 1549 char *name; 1550 char **avp; 1551 char *statp; 1552 { 1553 register struct hostent *hp; 1554 1555 hp = sm_gethostbyname(name); 1556 if (hp != NULL) 1557 return hp->h_name; 1558 *statp = EX_NOHOST; 1559 return NULL; 1560 } 1561 1562 #endif /* DAEMON */ 1563