122700Sdist /* 268839Seric * Copyright (c) 1983, 1995 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533780Sbostic * 642825Sbostic * %sccs.include.redist.c% 733780Sbostic */ 822700Sdist 933932Sbostic #include <errno.h> 1040962Sbostic #include "sendmail.h" 114535Seric 1233780Sbostic #ifndef lint 1333780Sbostic #ifdef DAEMON 14*69881Seric static char sccsid[] = "@(#)daemon.c 8.102 (Berkeley) 06/13/95 (with daemon mode)"; 1533780Sbostic #else 16*69881Seric static char sccsid[] = "@(#)daemon.c 8.102 (Berkeley) 06/13/95 (without daemon mode)"; 1733780Sbostic #endif 1833780Sbostic #endif /* not lint */ 194535Seric 2033780Sbostic #ifdef DAEMON 2133780Sbostic 2264338Seric # include <arpa/inet.h> 235978Seric 2466334Seric #if NAMED_BIND 2559042Seric # include <resolv.h> 2659042Seric #endif 2759042Seric 2869601Seric #if IP_SRCROUTE 2969601Seric # include <netinet/in_systm.h> 3069601Seric # include <netinet/ip.h> 3169601Seric # include <netinet/ip_var.h> 3269601Seric #endif 3369601Seric 344535Seric /* 354535Seric ** DAEMON.C -- routines to use when running as a daemon. 367556Seric ** 377556Seric ** This entire file is highly dependent on the 4.2 BSD 387556Seric ** interprocess communication primitives. No attempt has 397556Seric ** been made to make this file portable to Version 7, 407556Seric ** Version 6, MPX files, etc. If you should try such a 417556Seric ** thing yourself, I recommend chucking the entire file 427556Seric ** and starting from scratch. Basic semantics are: 437556Seric ** 447556Seric ** getrequests() 457556Seric ** Opens a port and initiates a connection. 467556Seric ** Returns in a child. Must set InChannel and 477556Seric ** OutChannel appropriately. 4810206Seric ** clrdaemon() 4910206Seric ** Close any open files associated with getting 5010206Seric ** the connection; this is used when running the queue, 5110206Seric ** etc., to avoid having extra file descriptors during 5210206Seric ** the queue run and to avoid confusing the network 5310206Seric ** code (if it cares). 5452106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 557556Seric ** Make a connection to the named host on the given 567556Seric ** port. Set *outfile and *infile to the files 577556Seric ** appropriate for communication. Returns zero on 587556Seric ** success, else an exit status describing the 597556Seric ** error. 6060089Seric ** host_map_lookup(map, hbuf, avp, pstat) 6156823Seric ** Convert the entry in hbuf into a canonical form. 624535Seric */ 634535Seric /* 644535Seric ** GETREQUESTS -- open mail IPC port and get requests. 654535Seric ** 664535Seric ** Parameters: 674535Seric ** none. 684535Seric ** 694535Seric ** Returns: 704535Seric ** none. 714535Seric ** 724535Seric ** Side Effects: 734535Seric ** Waits until some interesting activity occurs. When 744535Seric ** it does, a child is created to process it, and the 754535Seric ** parent waits for completion. Return from this 769886Seric ** routine is always in the child. The file pointers 779886Seric ** "InChannel" and "OutChannel" should be set to point 789886Seric ** to the communication channel. 794535Seric */ 804535Seric 8158849Seric int DaemonSocket = -1; /* fd describing socket */ 8258849Seric SOCKADDR DaemonAddr; /* socket for incoming */ 8359783Seric int ListenQueueSize = 10; /* size of listen queue */ 8464381Seric int TcpRcvBufferSize = 0; /* size of TCP receive buffer */ 8564381Seric int TcpSndBufferSize = 0; /* size of TCP send buffer */ 8616144Seric 8768693Seric void 884535Seric getrequests() 894535Seric { 909610Seric int t; 9153751Seric bool refusingconnections = TRUE; 9258419Seric FILE *pidf; 9364828Seric int socksize; 94*69881Seric #if XDEBUG 9566793Seric bool j_has_dot; 9666793Seric #endif 9746928Sbostic extern void reapchild(); 987117Seric 999610Seric /* 1009610Seric ** Set up the address for the mailer. 1019610Seric */ 1029610Seric 10358849Seric if (DaemonAddr.sin.sin_family == 0) 10458849Seric DaemonAddr.sin.sin_family = AF_INET; 10558849Seric if (DaemonAddr.sin.sin_addr.s_addr == 0) 10658849Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 10758849Seric if (DaemonAddr.sin.sin_port == 0) 1089610Seric { 10965169Seric register struct servent *sp; 11065169Seric 11158849Seric sp = getservbyname("smtp", "tcp"); 11258849Seric if (sp == NULL) 11358849Seric { 11458909Seric syserr("554 service \"smtp\" unknown"); 11565169Seric DaemonAddr.sin.sin_port = htons(25); 11658849Seric } 11765169Seric else 11865169Seric DaemonAddr.sin.sin_port = sp->s_port; 1199610Seric } 1209610Seric 1219610Seric /* 1229610Seric ** Try to actually open the connection. 1239610Seric */ 1249610Seric 1259610Seric if (tTd(15, 1)) 12658849Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1279610Seric 1289610Seric /* get a socket for the SMTP connection */ 12966854Seric socksize = opendaemonsocket(TRUE); 13010347Seric 13164035Seric (void) setsignal(SIGCHLD, reapchild); 13224945Seric 13358419Seric /* write the pid to the log file for posterity */ 13458419Seric pidf = fopen(PidFile, "w"); 13558419Seric if (pidf != NULL) 13658419Seric { 13763863Seric extern char *CommandLineArgs; 13863863Seric 13963863Seric /* write the process id on line 1 */ 14058419Seric fprintf(pidf, "%d\n", getpid()); 14163863Seric 14263863Seric /* line 2 contains all command line flags */ 14363863Seric fprintf(pidf, "%s\n", CommandLineArgs); 14463863Seric 14563863Seric /* flush and close */ 14658419Seric fclose(pidf); 14758419Seric } 14858419Seric 149*69881Seric #if XDEBUG 15066793Seric { 15166812Seric char jbuf[MAXHOSTNAMELEN]; 15258419Seric 15368693Seric expand("\201j", jbuf, sizeof jbuf, CurEnv); 15466812Seric j_has_dot = strchr(jbuf, '.') != NULL; 15566793Seric } 15666793Seric #endif 15766793Seric 1589610Seric if (tTd(15, 1)) 15910206Seric printf("getrequests: %d\n", DaemonSocket); 1609610Seric 1614631Seric for (;;) 1624631Seric { 16314875Seric register int pid; 16411147Seric auto int lotherend; 16553751Seric extern bool refuseconnections(); 16668693Seric extern int getla(); 16711147Seric 16814875Seric /* see if we are rejecting connections */ 16953751Seric CurrentLA = getla(); 17053751Seric if (refuseconnections()) 17136584Sbostic { 17266845Seric if (DaemonSocket >= 0) 17353751Seric { 17466845Seric /* close socket so peer will fail quickly */ 17566845Seric (void) close(DaemonSocket); 17666845Seric DaemonSocket = -1; 17753751Seric } 17866845Seric refusingconnections = TRUE; 17966845Seric sleep(15); 18053751Seric continue; 18136584Sbostic } 18214875Seric 18368693Seric /* arrange to (re)open the socket if necessary */ 18453751Seric if (refusingconnections) 18553751Seric { 18667690Seric (void) opendaemonsocket(FALSE); 18753751Seric refusingconnections = FALSE; 18853751Seric } 18953751Seric 190*69881Seric #if XDEBUG 19166793Seric /* check for disaster */ 19266793Seric { 19366812Seric char jbuf[MAXHOSTNAMELEN]; 19466793Seric 19568693Seric expand("\201j", jbuf, sizeof jbuf, CurEnv); 19668693Seric if (!wordinclass(jbuf, 'w')) 19766793Seric { 19866793Seric dumpstate("daemon lost $j"); 19966793Seric syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog"); 20066793Seric abort(); 20166793Seric } 20266812Seric else if (j_has_dot && strchr(jbuf, '.') == NULL) 20366793Seric { 20466793Seric dumpstate("daemon $j lost dot"); 20566793Seric syslog(LOG_ALERT, "daemon process $j lost dot; see syslog"); 20666793Seric abort(); 20766793Seric } 20866793Seric } 20966793Seric #endif 21066793Seric 2119610Seric /* wait for a connection */ 21269781Seric setproctitle("accepting connections"); 2139610Seric do 2149610Seric { 2159610Seric errno = 0; 21664828Seric lotherend = socksize; 21746928Sbostic t = accept(DaemonSocket, 21846928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2199610Seric } while (t < 0 && errno == EINTR); 2209610Seric if (t < 0) 2215978Seric { 2229610Seric syserr("getrequests: accept"); 22368693Seric 22468693Seric /* arrange to re-open the socket next time around */ 22568693Seric (void) close(DaemonSocket); 22668693Seric DaemonSocket = -1; 2279610Seric sleep(5); 2289610Seric continue; 2295978Seric } 2304631Seric 2315978Seric /* 2325978Seric ** Create a subprocess to process the mail. 2335978Seric */ 2345978Seric 2357677Seric if (tTd(15, 2)) 2369610Seric printf("getrequests: forking (fd = %d)\n", t); 2375978Seric 2384636Seric pid = fork(); 2394636Seric if (pid < 0) 2404631Seric { 2414636Seric syserr("daemon: cannot fork"); 2424636Seric sleep(10); 2439610Seric (void) close(t); 2444636Seric continue; 2454631Seric } 2464631Seric 2474636Seric if (pid == 0) 2484631Seric { 24964086Seric char *p; 25058951Seric extern char *hostnamebyanyaddr(); 25168693Seric extern void intsig(); 25211147Seric 2534636Seric /* 2544636Seric ** CHILD -- return to caller. 25511147Seric ** Collect verified idea of sending host. 2564636Seric ** Verify calling user id if possible here. 2574636Seric */ 2584631Seric 25964035Seric (void) setsignal(SIGCHLD, SIG_DFL); 26068693Seric (void) setsignal(SIGHUP, intsig); 26168693Seric (void) close(DaemonSocket); 26266017Seric DisConnected = FALSE; 26324950Seric 26466032Seric setproctitle("startup with %s", 26566032Seric anynet_ntoa(&RealHostAddr)); 26666032Seric 26711147Seric /* determine host name */ 26864086Seric p = hostnamebyanyaddr(&RealHostAddr); 26969471Seric if (strlen(p) > MAXNAME) 27069471Seric p[MAXNAME] = '\0'; 27164086Seric RealHostName = newstr(p); 27266032Seric setproctitle("startup with %s", p); 27358778Seric 27464724Seric if ((InChannel = fdopen(t, "r")) == NULL || 27564724Seric (t = dup(t)) < 0 || 27664724Seric (OutChannel = fdopen(t, "w")) == NULL) 27764724Seric { 27864724Seric syserr("cannot open SMTP server channel, fd=%d", t); 27964724Seric exit(0); 28064724Seric } 28159254Seric 28216884Seric /* should we check for illegal connection here? XXX */ 28359156Seric #ifdef XLA 28459156Seric if (!xla_host_ok(RealHostName)) 28559156Seric { 28659254Seric message("421 Too many SMTP sessions for this host"); 28759156Seric exit(0); 28859156Seric } 28959156Seric #endif 29016884Seric 2917677Seric if (tTd(15, 2)) 2925978Seric printf("getreq: returning\n"); 2934636Seric return; 2944631Seric } 2954631Seric 29669839Seric CurChildren++; 29769839Seric 2987117Seric /* close the port so that others will hang (for a while) */ 2999610Seric (void) close(t); 3004631Seric } 3019886Seric /*NOTREACHED*/ 3024631Seric } 3035978Seric /* 30466845Seric ** OPENDAEMONSOCKET -- open the SMTP socket 30566845Seric ** 30666845Seric ** Deals with setting all appropriate options. DaemonAddr must 30766845Seric ** be set up in advance. 30866845Seric ** 30966845Seric ** Parameters: 31066854Seric ** firsttime -- set if this is the initial open. 31166845Seric ** 31266845Seric ** Returns: 31366845Seric ** Size in bytes of the daemon socket addr. 31466845Seric ** 31566845Seric ** Side Effects: 31666845Seric ** Leaves DaemonSocket set to the open socket. 31766845Seric ** Exits if the socket cannot be created. 31866845Seric */ 31966845Seric 32066861Seric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */ 32166861Seric 32266845Seric int 32366854Seric opendaemonsocket(firsttime) 32466854Seric bool firsttime; 32566845Seric { 32666845Seric int on = 1; 32768693Seric int socksize = 0; 32866861Seric int ntries = 0; 32966861Seric int saveerrno; 33066845Seric 33166845Seric if (tTd(15, 2)) 33266845Seric printf("opendaemonsocket()\n"); 33366845Seric 33466861Seric do 33566845Seric { 33666862Seric if (ntries > 0) 33766862Seric sleep(5); 33866861Seric if (firsttime || DaemonSocket < 0) 33966854Seric { 34066861Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 34166861Seric if (DaemonSocket < 0) 34266861Seric { 34366861Seric /* probably another daemon already */ 34466861Seric saveerrno = errno; 34566861Seric syserr("opendaemonsocket: can't create server SMTP socket"); 34666861Seric severe: 34766845Seric # ifdef LOG 34866861Seric if (LogLevel > 0) 34966861Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 35066845Seric # endif /* LOG */ 35166861Seric DaemonSocket = -1; 35266861Seric continue; 35366861Seric } 35466845Seric 35566861Seric /* turn on network debugging? */ 35666861Seric if (tTd(15, 101)) 35766861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 35866861Seric SO_DEBUG, (char *)&on, 35966861Seric sizeof on); 36066845Seric 36166861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36266861Seric SO_REUSEADDR, (char *)&on, sizeof on); 36366861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36466861Seric SO_KEEPALIVE, (char *)&on, sizeof on); 36566845Seric 36666845Seric #ifdef SO_RCVBUF 36766861Seric if (TcpRcvBufferSize > 0) 36866861Seric { 36966861Seric if (setsockopt(DaemonSocket, SOL_SOCKET, 37066861Seric SO_RCVBUF, 37166861Seric (char *) &TcpRcvBufferSize, 37266861Seric sizeof(TcpRcvBufferSize)) < 0) 37366861Seric syserr("getrequests: setsockopt(SO_RCVBUF)"); 37466861Seric } 37566845Seric #endif 37666845Seric 37766861Seric switch (DaemonAddr.sa.sa_family) 37866861Seric { 379*69881Seric # if NETINET 38066861Seric case AF_INET: 38166861Seric socksize = sizeof DaemonAddr.sin; 38266861Seric break; 38366845Seric # endif 38466845Seric 385*69881Seric # if NETISO 38666861Seric case AF_ISO: 38766861Seric socksize = sizeof DaemonAddr.siso; 38866861Seric break; 38966845Seric # endif 39066845Seric 39166861Seric default: 39266861Seric socksize = sizeof DaemonAddr; 39366861Seric break; 39466861Seric } 39566861Seric 39666861Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0) 39766861Seric { 39866861Seric saveerrno = errno; 39966861Seric syserr("getrequests: cannot bind"); 40066861Seric (void) close(DaemonSocket); 40166861Seric goto severe; 40266861Seric } 40366854Seric } 40466861Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0) 40566854Seric { 40666861Seric saveerrno = errno; 40766861Seric syserr("getrequests: cannot listen"); 40866854Seric (void) close(DaemonSocket); 40966854Seric goto severe; 41066854Seric } 41166861Seric return socksize; 41266861Seric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno)); 41368693Seric syserr("!opendaemonsocket: server SMTP socket wedged: exiting"); 41466861Seric finis(); 41566845Seric } 41666845Seric /* 41710206Seric ** CLRDAEMON -- reset the daemon connection 41810206Seric ** 41910206Seric ** Parameters: 42010206Seric ** none. 42110206Seric ** 42210206Seric ** Returns: 42310206Seric ** none. 42410206Seric ** 42510206Seric ** Side Effects: 42610206Seric ** releases any resources used by the passive daemon. 42710206Seric */ 42810206Seric 42968693Seric void 43010206Seric clrdaemon() 43110206Seric { 43210206Seric if (DaemonSocket >= 0) 43310206Seric (void) close(DaemonSocket); 43410206Seric DaemonSocket = -1; 43510206Seric } 43610206Seric /* 43758849Seric ** SETDAEMONOPTIONS -- set options for running the daemon 43858849Seric ** 43958849Seric ** Parameters: 44058849Seric ** p -- the options line. 44158849Seric ** 44258849Seric ** Returns: 44358849Seric ** none. 44458849Seric */ 44558849Seric 44668693Seric void 44758849Seric setdaemonoptions(p) 44858849Seric register char *p; 44958849Seric { 45058873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 45158873Seric DaemonAddr.sa.sa_family = AF_INET; 45258873Seric 45358849Seric while (p != NULL) 45458849Seric { 45558849Seric register char *f; 45658849Seric register char *v; 45758849Seric 45858849Seric while (isascii(*p) && isspace(*p)) 45958849Seric p++; 46058849Seric if (*p == '\0') 46158849Seric break; 46258849Seric f = p; 46358849Seric p = strchr(p, ','); 46458849Seric if (p != NULL) 46558849Seric *p++ = '\0'; 46658849Seric v = strchr(f, '='); 46758849Seric if (v == NULL) 46858849Seric continue; 46958849Seric while (isascii(*++v) && isspace(*v)) 47058849Seric continue; 47169397Seric if (isascii(*f) && isupper(*f)) 47269397Seric *f = tolower(*f); 47358849Seric 47458849Seric switch (*f) 47558849Seric { 47658873Seric case 'F': /* address family */ 47758849Seric if (isascii(*v) && isdigit(*v)) 47858873Seric DaemonAddr.sa.sa_family = atoi(v); 479*69881Seric #if NETINET 48058873Seric else if (strcasecmp(v, "inet") == 0) 48158873Seric DaemonAddr.sa.sa_family = AF_INET; 48258873Seric #endif 483*69881Seric #if NETISO 48458873Seric else if (strcasecmp(v, "iso") == 0) 48558873Seric DaemonAddr.sa.sa_family = AF_ISO; 48658873Seric #endif 487*69881Seric #if NETNS 48858873Seric else if (strcasecmp(v, "ns") == 0) 48958873Seric DaemonAddr.sa.sa_family = AF_NS; 49058873Seric #endif 491*69881Seric #if NETX25 49258873Seric else if (strcasecmp(v, "x.25") == 0) 49358873Seric DaemonAddr.sa.sa_family = AF_CCITT; 49458873Seric #endif 49558849Seric else 49658873Seric syserr("554 Unknown address family %s in Family=option", v); 49758873Seric break; 49858873Seric 49958873Seric case 'A': /* address */ 50058873Seric switch (DaemonAddr.sa.sa_family) 50158849Seric { 502*69881Seric #if NETINET 50358873Seric case AF_INET: 50458873Seric if (isascii(*v) && isdigit(*v)) 50568693Seric DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v)); 50658873Seric else 50758873Seric { 50858873Seric register struct netent *np; 50958849Seric 51058873Seric np = getnetbyname(v); 51158873Seric if (np == NULL) 51258873Seric syserr("554 network \"%s\" unknown", v); 51358873Seric else 51458873Seric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 51558873Seric } 51658873Seric break; 51758873Seric #endif 51858873Seric 51958873Seric default: 52058873Seric syserr("554 Address= option unsupported for family %d", 52158873Seric DaemonAddr.sa.sa_family); 52258873Seric break; 52358849Seric } 52458849Seric break; 52558849Seric 52658873Seric case 'P': /* port */ 52758873Seric switch (DaemonAddr.sa.sa_family) 52858849Seric { 52958873Seric short port; 53058849Seric 531*69881Seric #if NETINET 53258873Seric case AF_INET: 53358873Seric if (isascii(*v) && isdigit(*v)) 53464366Seric DaemonAddr.sin.sin_port = htons(atoi(v)); 53558849Seric else 53658873Seric { 53758873Seric register struct servent *sp; 53858873Seric 53958873Seric sp = getservbyname(v, "tcp"); 54058873Seric if (sp == NULL) 54158909Seric syserr("554 service \"%s\" unknown", v); 54258873Seric else 54358873Seric DaemonAddr.sin.sin_port = sp->s_port; 54458873Seric } 54558873Seric break; 54658873Seric #endif 54758873Seric 548*69881Seric #if NETISO 54958873Seric case AF_ISO: 55058873Seric /* assume two byte transport selector */ 55158873Seric if (isascii(*v) && isdigit(*v)) 55264366Seric port = htons(atoi(v)); 55358873Seric else 55458873Seric { 55558873Seric register struct servent *sp; 55658873Seric 55758873Seric sp = getservbyname(v, "tcp"); 55858873Seric if (sp == NULL) 55958909Seric syserr("554 service \"%s\" unknown", v); 56058873Seric else 56158873Seric port = sp->s_port; 56258873Seric } 56358873Seric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 56458873Seric break; 56558873Seric #endif 56658873Seric 56758873Seric default: 56858873Seric syserr("554 Port= option unsupported for family %d", 56958873Seric DaemonAddr.sa.sa_family); 57058873Seric break; 57158849Seric } 57258849Seric break; 57359783Seric 57459783Seric case 'L': /* listen queue size */ 57559783Seric ListenQueueSize = atoi(v); 57659783Seric break; 57764381Seric 57864381Seric case 'S': /* send buffer size */ 57964381Seric TcpSndBufferSize = atoi(v); 58064381Seric break; 58164381Seric 58264381Seric case 'R': /* receive buffer size */ 58364381Seric TcpRcvBufferSize = atoi(v); 58464381Seric break; 58569397Seric 58669397Seric default: 58769397Seric syserr("554 DaemonPortOptions parameter \"%s\" unknown", f); 58858849Seric } 58958849Seric } 59058849Seric } 59158849Seric /* 5926039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 5936039Seric ** 5946039Seric ** Parameters: 5956039Seric ** host -- the name of the host. 5966633Seric ** port -- the port number to connect to. 59753739Seric ** mci -- a pointer to the mail connection information 59853739Seric ** structure to be filled in. 59952106Seric ** usesecureport -- if set, use a low numbered (reserved) 60052106Seric ** port to provide some rudimentary authentication. 6016039Seric ** 6026039Seric ** Returns: 6036039Seric ** An exit code telling whether the connection could be 6046039Seric ** made and if not why not. 6056039Seric ** 6066039Seric ** Side Effects: 6076039Seric ** none. 6086039Seric */ 6095978Seric 61058755Seric SOCKADDR CurHostAddr; /* address of current host */ 61158305Seric 61254967Seric int 61353739Seric makeconnection(host, port, mci, usesecureport) 6146039Seric char *host; 6157286Seric u_short port; 61654967Seric register MCI *mci; 61752106Seric bool usesecureport; 6186039Seric { 61968693Seric register int i = 0; 62068693Seric register int s; 62129430Sbloom register struct hostent *hp = (struct hostent *)NULL; 62258755Seric SOCKADDR addr; 62352106Seric int sav_errno; 62458755Seric int addrlen; 62568693Seric bool firstconnect; 62666334Seric #if NAMED_BIND 62735651Seric extern int h_errno; 62835651Seric #endif 6296039Seric 6306039Seric /* 6316039Seric ** Set up the address for the mailer. 6329308Seric ** Accept "[a.b.c.d]" syntax for host name. 6336039Seric */ 6346039Seric 63566334Seric #if NAMED_BIND 63625475Smiriam h_errno = 0; 63735651Seric #endif 63825475Smiriam errno = 0; 63958864Seric bzero(&CurHostAddr, sizeof CurHostAddr); 64064334Seric SmtpPhase = mci->mci_phase = "initial connection"; 64158906Seric CurHostName = host; 64225475Smiriam 6439308Seric if (host[0] == '[') 6449308Seric { 64511147Seric long hid; 64656795Seric register char *p = strchr(host, ']'); 6479308Seric 64811147Seric if (p != NULL) 6499308Seric { 65011147Seric *p = '\0'; 651*69881Seric #if NETINET 65211147Seric hid = inet_addr(&host[1]); 65358360Seric if (hid == -1) 65459884Seric #endif 65558360Seric { 65658360Seric /* try it as a host name (avoid MX lookup) */ 65768693Seric hp = sm_gethostbyname(&host[1]); 65866349Seric if (hp == NULL && p[-1] == '.') 65966349Seric { 66068693Seric #if NAMED_BIND 66168693Seric int oldopts = _res.options; 66268693Seric 66368693Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 66468693Seric #endif 66566349Seric p[-1] = '\0'; 66668693Seric hp = sm_gethostbyname(&host[1]); 66766349Seric p[-1] = '.'; 66868693Seric #if NAMED_BIND 66968693Seric _res.options = oldopts; 67068693Seric #endif 67166349Seric } 67258360Seric *p = ']'; 67358360Seric goto gothostent; 67458360Seric } 67511147Seric *p = ']'; 6769308Seric } 67758360Seric if (p == NULL) 6789308Seric { 67958151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 68068857Seric mci->mci_status = "5.1.2"; 6819308Seric return (EX_NOHOST); 6829308Seric } 683*69881Seric #if NETINET 68459884Seric addr.sin.sin_family = AF_INET; /*XXX*/ 68558778Seric addr.sin.sin_addr.s_addr = hid; 68659884Seric #endif 6879308Seric } 6889610Seric else 6899610Seric { 69066349Seric register char *p = &host[strlen(host) - 1]; 69166349Seric 69268693Seric hp = sm_gethostbyname(host); 69366349Seric if (hp == NULL && *p == '.') 69466349Seric { 69568693Seric #if NAMED_BIND 69668693Seric int oldopts = _res.options; 69768693Seric 69868693Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 69968693Seric #endif 70066349Seric *p = '\0'; 70168693Seric hp = sm_gethostbyname(host); 70266349Seric *p = '.'; 70368693Seric #if NAMED_BIND 70468693Seric _res.options = oldopts; 70568693Seric #endif 70666349Seric } 70758360Seric gothostent: 70825475Smiriam if (hp == NULL) 70924945Seric { 71066334Seric #if NAMED_BIND 71168693Seric /* check for name server timeouts */ 71268693Seric if (errno == ETIMEDOUT || h_errno == TRY_AGAIN || 71368693Seric (errno == ECONNREFUSED && UseNameServer)) 71468693Seric { 71568693Seric mci->mci_status = "4.4.3"; 71625475Smiriam return (EX_TEMPFAIL); 71768693Seric } 71835651Seric #endif 71925475Smiriam return (EX_NOHOST); 72024945Seric } 72158778Seric addr.sa.sa_family = hp->h_addrtype; 72258778Seric switch (hp->h_addrtype) 72358778Seric { 724*69881Seric #if NETINET 72558778Seric case AF_INET: 72658755Seric bcopy(hp->h_addr, 72758778Seric &addr.sin.sin_addr, 72868693Seric INADDRSZ); 72958778Seric break; 73058778Seric #endif 73158778Seric 73258778Seric default: 73358755Seric bcopy(hp->h_addr, 73458778Seric addr.sa.sa_data, 73558755Seric hp->h_length); 73658778Seric break; 73758778Seric } 73829430Sbloom i = 1; 7399610Seric } 7409610Seric 7419610Seric /* 7429610Seric ** Determine the port number. 7439610Seric */ 7449610Seric 74569840Seric if (port == 0) 7469610Seric { 7479610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 7489610Seric 7499610Seric if (sp == NULL) 7509610Seric { 75168745Seric #ifdef LOG 75268745Seric if (LogLevel > 2) 75368745Seric syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown"); 75468745Seric #endif 75565169Seric port = htons(25); 7569610Seric } 75765169Seric else 75865169Seric port = sp->s_port; 7599610Seric } 7606039Seric 76158778Seric switch (addr.sa.sa_family) 76258755Seric { 763*69881Seric #if NETINET 76458755Seric case AF_INET: 76558778Seric addr.sin.sin_port = port; 76658755Seric addrlen = sizeof (struct sockaddr_in); 76758755Seric break; 76859884Seric #endif 76958755Seric 770*69881Seric #if NETISO 77158755Seric case AF_ISO: 77258755Seric /* assume two byte transport selector */ 77358755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 77458755Seric addrlen = sizeof (struct sockaddr_iso); 77558755Seric break; 77658755Seric #endif 77758755Seric 77858755Seric default: 77958778Seric syserr("Can't connect to address family %d", addr.sa.sa_family); 78058755Seric return (EX_NOHOST); 78158755Seric } 78258755Seric 7836039Seric /* 7846039Seric ** Try to actually open the connection. 7856039Seric */ 7866039Seric 78759156Seric #ifdef XLA 78859156Seric /* if too many connections, don't bother trying */ 78959156Seric if (!xla_noqueue_ok(host)) 79059156Seric return EX_TEMPFAIL; 79159156Seric #endif 79259156Seric 79368693Seric firstconnect = TRUE; 79457736Seric for (;;) 79552106Seric { 79657736Seric if (tTd(16, 1)) 79758755Seric printf("makeconnection (%s [%s])\n", 79858755Seric host, anynet_ntoa(&addr)); 79952106Seric 80058588Seric /* save for logging */ 80158588Seric CurHostAddr = addr; 80258588Seric 80357736Seric if (usesecureport) 80457736Seric { 80557736Seric int rport = IPPORT_RESERVED - 1; 8066039Seric 80757736Seric s = rresvport(&rport); 80857736Seric } 80957736Seric else 81057736Seric { 81157736Seric s = socket(AF_INET, SOCK_STREAM, 0); 81257736Seric } 81357736Seric if (s < 0) 81457736Seric { 81557736Seric sav_errno = errno; 81657736Seric syserr("makeconnection: no socket"); 81757736Seric goto failure; 81857736Seric } 81910347Seric 82064381Seric #ifdef SO_SNDBUF 82164381Seric if (TcpSndBufferSize > 0) 82264381Seric { 82364381Seric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 82464561Seric (char *) &TcpSndBufferSize, 82564381Seric sizeof(TcpSndBufferSize)) < 0) 82664381Seric syserr("makeconnection: setsockopt(SO_SNDBUF)"); 82764381Seric } 82864381Seric #endif 82964381Seric 83057736Seric if (tTd(16, 1)) 83157736Seric printf("makeconnection: fd=%d\n", s); 83257736Seric 83357736Seric /* turn on network debugging? */ 83457736Seric if (tTd(16, 101)) 83557736Seric { 83657736Seric int on = 1; 83766861Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 83857736Seric (char *)&on, sizeof on); 83957736Seric } 84057736Seric if (CurEnv->e_xfp != NULL) 84157736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 84257736Seric errno = 0; /* for debugging */ 84358755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 84457736Seric break; 84557736Seric 84668693Seric /* if running demand-dialed connection, try again */ 84768693Seric if (DialDelay > 0 && firstconnect) 84868693Seric { 84968693Seric if (tTd(16, 1)) 85068693Seric printf("Connect failed (%s); trying again...\n", 85168693Seric errstring(sav_errno)); 85268693Seric firstconnect = FALSE; 85368693Seric sleep(DialDelay); 85468693Seric continue; 85568693Seric } 85668693Seric 85757736Seric /* couldn't connect.... figure out why */ 85827744Sbloom sav_errno = errno; 85927744Sbloom (void) close(s); 86068693Seric if (hp != NULL && hp->h_addr_list[i]) 86129430Sbloom { 86257736Seric if (tTd(16, 1)) 86358755Seric printf("Connect failed (%s); trying new address....\n", 86458755Seric errstring(sav_errno)); 86558778Seric switch (addr.sa.sa_family) 86658778Seric { 867*69881Seric #if NETINET 86858778Seric case AF_INET: 86958755Seric bcopy(hp->h_addr_list[i++], 87058778Seric &addr.sin.sin_addr, 87168693Seric INADDRSZ); 87258778Seric break; 87358778Seric #endif 87458778Seric 87558778Seric default: 87658755Seric bcopy(hp->h_addr_list[i++], 87758778Seric addr.sa.sa_data, 87852106Seric hp->h_length); 87958778Seric break; 88058778Seric } 88157736Seric continue; 88229430Sbloom } 88329430Sbloom 8846039Seric /* failure, decide if temporary or not */ 8856039Seric failure: 88659254Seric #ifdef XLA 88759254Seric xla_host_end(host); 88859254Seric #endif 88958542Seric if (transienterror(sav_errno)) 89058542Seric return EX_TEMPFAIL; 89158542Seric else 89258542Seric { 89358542Seric message("%s", errstring(sav_errno)); 89458542Seric return (EX_UNAVAILABLE); 8956039Seric } 8966039Seric } 8976039Seric 8986039Seric /* connection ok, put it into canonical form */ 89964724Seric if ((mci->mci_out = fdopen(s, "w")) == NULL || 90064724Seric (s = dup(s)) < 0 || 90164725Seric (mci->mci_in = fdopen(s, "r")) == NULL) 90264724Seric { 90364724Seric syserr("cannot open SMTP client channel, fd=%d", s); 90464724Seric return EX_TEMPFAIL; 90564724Seric } 9066039Seric 90710098Seric return (EX_OK); 9086039Seric } 90910758Seric /* 91010758Seric ** MYHOSTNAME -- return the name of this host. 91110758Seric ** 91210758Seric ** Parameters: 91310758Seric ** hostbuf -- a place to return the name of this host. 91412313Seric ** size -- the size of hostbuf. 91510758Seric ** 91610758Seric ** Returns: 91710758Seric ** A list of aliases for this host. 91810758Seric ** 91910758Seric ** Side Effects: 92064338Seric ** Adds numeric codes to $=w. 92110758Seric */ 9226039Seric 92368693Seric struct hostent * 92412313Seric myhostname(hostbuf, size) 92510758Seric char hostbuf[]; 92612313Seric int size; 92710758Seric { 92858110Seric register struct hostent *hp; 92968693Seric extern bool getcanonname(); 93068693Seric extern int h_errno; 93110758Seric 93223120Seric if (gethostname(hostbuf, size) < 0) 93323120Seric { 93423120Seric (void) strcpy(hostbuf, "localhost"); 93523120Seric } 93668693Seric hp = sm_gethostbyname(hostbuf); 93766853Seric if (hp == NULL) 93868693Seric return NULL; 93968693Seric if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL) 94067448Seric { 94168693Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 94268693Seric hostbuf[size - 1] = '\0'; 94367448Seric } 94466853Seric 94568693Seric /* 94669877Seric ** If there is still no dot in the name, try looking for a 94769877Seric ** dotted alias. 94868693Seric */ 94968693Seric 95069818Seric if (strchr(hostbuf, '.') == NULL) 95168612Seric { 95269877Seric char **ha; 95369818Seric 95469877Seric for (ha = hp->h_aliases; *ha != NULL; ha++) 95569877Seric { 95669877Seric if (strchr(*ha, '.') != NULL) 95769877Seric { 95869877Seric (void) strncpy(hostbuf, *ha, size - 1); 95969877Seric hostbuf[size - 1] = '\0'; 96069818Seric break; 96169877Seric } 96269818Seric } 96366853Seric } 96469877Seric 96569877Seric /* 96669877Seric ** If _still_ no dot, wait for a while and try again -- it is 96769877Seric ** possible that some service is starting up. This can result 96869877Seric ** in excessive delays if the system is badly configured, but 96969877Seric ** there really isn't a way around that, particularly given that 97069877Seric ** the config file hasn't been read at this point. 97169877Seric ** All in all, a bit of a mess. 97269877Seric */ 97369877Seric 97469877Seric if (strchr(hostbuf, '.') == NULL && 97569877Seric !getcanonname(hostbuf, size, TRUE)) 97669877Seric { 97769877Seric message("My unqualifed host name (%s) unknown; sleeping for retry", 97869877Seric hostbuf); 97969877Seric sleep(60); 98069877Seric (void) getcanonname(hostbuf, size, TRUE); 98169877Seric } 98268693Seric return (hp); 98310758Seric } 98451315Seric /* 98558951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 98658308Seric ** 98758951Seric ** Uses RFC1413 protocol to try to get info from the other end. 98858951Seric ** 98958308Seric ** Parameters: 99058308Seric ** fd -- the descriptor 99158308Seric ** 99258308Seric ** Returns: 99358951Seric ** The user@host information associated with this descriptor. 99458308Seric */ 99558308Seric 99658951Seric static jmp_buf CtxAuthTimeout; 99758951Seric 99868693Seric static void 99958951Seric authtimeout() 100058951Seric { 100158951Seric longjmp(CtxAuthTimeout, 1); 100258951Seric } 100358951Seric 100458308Seric char * 100558951Seric getauthinfo(fd) 100658308Seric int fd; 100758308Seric { 100858951Seric int falen; 100959104Seric register char *p; 101058951Seric SOCKADDR la; 101158951Seric int lalen; 101258951Seric register struct servent *sp; 101358951Seric int s; 101458951Seric int i; 101558951Seric EVENT *ev; 101668444Seric int nleft; 101768462Seric char ibuf[MAXNAME + 1]; 101858951Seric static char hbuf[MAXNAME * 2 + 2]; 101958951Seric extern char *hostnamebyanyaddr(); 102058308Seric 102166761Seric falen = sizeof RealHostAddr; 102268693Seric if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 || 102368693Seric falen <= 0 || RealHostAddr.sa.sa_family == 0) 102458951Seric { 102558951Seric (void) sprintf(hbuf, "%s@localhost", RealUserName); 102658957Seric if (tTd(9, 1)) 102758951Seric printf("getauthinfo: %s\n", hbuf); 102858951Seric return hbuf; 102958951Seric } 103058951Seric 103166761Seric if (RealHostName == NULL) 103266761Seric { 103366761Seric /* translate that to a host name */ 103466761Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 103566761Seric } 103666761Seric 103765831Seric if (TimeOuts.to_ident == 0) 103865831Seric goto noident; 103965831Seric 104058951Seric lalen = sizeof la; 104166761Seric if (RealHostAddr.sa.sa_family != AF_INET || 104258951Seric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 104358951Seric la.sa.sa_family != AF_INET) 104458951Seric { 104558951Seric /* no ident info */ 104658951Seric goto noident; 104758951Seric } 104858951Seric 104958951Seric /* create ident query */ 105068457Seric (void) sprintf(ibuf, "%d,%d\r\n", 105166761Seric ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); 105258951Seric 105358951Seric /* create local address */ 105464747Seric la.sin.sin_port = 0; 105558951Seric 105658951Seric /* create foreign address */ 105758951Seric sp = getservbyname("auth", "tcp"); 105858951Seric if (sp != NULL) 105966761Seric RealHostAddr.sin.sin_port = sp->s_port; 106058308Seric else 106166761Seric RealHostAddr.sin.sin_port = htons(113); 106258951Seric 106358951Seric s = -1; 106458951Seric if (setjmp(CtxAuthTimeout) != 0) 106558951Seric { 106658951Seric if (s >= 0) 106758951Seric (void) close(s); 106858951Seric goto noident; 106958951Seric } 107058951Seric 107158951Seric /* put a timeout around the whole thing */ 107264255Seric ev = setevent(TimeOuts.to_ident, authtimeout, 0); 107358951Seric 107464747Seric /* connect to foreign IDENT server using same address as SMTP socket */ 107558951Seric s = socket(AF_INET, SOCK_STREAM, 0); 107658951Seric if (s < 0) 107758951Seric { 107858951Seric clrevent(ev); 107958951Seric goto noident; 108058951Seric } 108164747Seric if (bind(s, &la.sa, sizeof la.sin) < 0 || 108266761Seric connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) 108358951Seric { 108466011Seric goto closeident; 108558951Seric } 108658951Seric 108758957Seric if (tTd(9, 10)) 108868457Seric printf("getauthinfo: sent %s", ibuf); 108958951Seric 109058951Seric /* send query */ 109168457Seric if (write(s, ibuf, strlen(ibuf)) < 0) 109258951Seric goto closeident; 109358951Seric 109458951Seric /* get result */ 109568457Seric p = &ibuf[0]; 109668525Seric nleft = sizeof ibuf - 1; 109768444Seric while ((i = read(s, p, nleft)) > 0) 109868444Seric { 109968444Seric p += i; 110068444Seric nleft -= i; 110168444Seric } 110258951Seric (void) close(s); 110358951Seric clrevent(ev); 110468457Seric if (i < 0 || p == &ibuf[0]) 110558951Seric goto noident; 110658951Seric 110768444Seric if (*--p == '\n' && *--p == '\r') 110868444Seric p--; 110968444Seric *++p = '\0'; 111068444Seric 111158957Seric if (tTd(9, 3)) 111268457Seric printf("getauthinfo: got %s\n", ibuf); 111358951Seric 111458951Seric /* parse result */ 111568457Seric p = strchr(ibuf, ':'); 111658951Seric if (p == NULL) 111758951Seric { 111858951Seric /* malformed response */ 111958951Seric goto noident; 112058951Seric } 112158951Seric while (isascii(*++p) && isspace(*p)) 112258951Seric continue; 112358951Seric if (strncasecmp(p, "userid", 6) != 0) 112458951Seric { 112558951Seric /* presumably an error string */ 112658951Seric goto noident; 112758951Seric } 112858951Seric p += 6; 112958951Seric while (isascii(*p) && isspace(*p)) 113058951Seric p++; 113158951Seric if (*p++ != ':') 113258951Seric { 113358951Seric /* either useridxx or malformed response */ 113458951Seric goto noident; 113558951Seric } 113658951Seric 113758951Seric /* p now points to the OSTYPE field */ 113868693Seric while (isascii(*p) && isspace(*p)) 113968693Seric p++; 114068693Seric if (strncasecmp(p, "other", 5) == 0 && 114168693Seric (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0')) 114268693Seric { 114368693Seric /* not useful information */ 114468693Seric goto noident; 114568693Seric } 114658951Seric p = strchr(p, ':'); 114758951Seric if (p == NULL) 114858951Seric { 114958951Seric /* malformed response */ 115058951Seric goto noident; 115158951Seric } 115258951Seric 115358957Seric /* 1413 says don't do this -- but it's broken otherwise */ 115458957Seric while (isascii(*++p) && isspace(*p)) 115558957Seric continue; 115658957Seric 115767935Seric /* p now points to the authenticated name -- copy carefully */ 115868457Seric cleanstrcpy(hbuf, p, MAXNAME); 115968875Seric i = strlen(hbuf); 116067935Seric hbuf[i++] = '@'; 116167935Seric strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName); 116269601Seric goto postident; 116358957Seric 116466011Seric closeident: 116566011Seric (void) close(s); 116666011Seric clrevent(ev); 116766011Seric 116858957Seric noident: 116966003Seric if (RealHostName == NULL) 117066003Seric { 117166003Seric if (tTd(9, 1)) 117266003Seric printf("getauthinfo: NULL\n"); 117366003Seric return NULL; 117466003Seric } 117558957Seric (void) strcpy(hbuf, RealHostName); 117658957Seric 117769601Seric postident: 117869601Seric #if IP_SRCROUTE 117969601Seric /* 118069601Seric ** Extract IP source routing information. 118169601Seric ** 118269601Seric ** Format of output for a connection from site a through b 118369601Seric ** through c to d: 118469601Seric ** loose: @site-c@site-b:site-a 118569601Seric ** strict: !@site-c@site-b:site-a 118669601Seric ** 118769601Seric ** o - pointer within ipopt_list structure. 118869601Seric ** q - pointer within ls/ss rr route data 118969601Seric ** p - pointer to hbuf 119069601Seric */ 119169601Seric 119269601Seric if (RealHostAddr.sa.sa_family == AF_INET) 119369601Seric { 119469601Seric int ipoptlen, j; 119569637Seric u_char *q; 119669601Seric u_char *o; 119769601Seric struct in_addr addr; 119869601Seric struct ipoption ipopt; 119969601Seric 120069601Seric ipoptlen = sizeof ipopt; 120169601Seric if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS, 120269601Seric (char *) &ipopt, &ipoptlen) < 0) 120369601Seric goto noipsr; 120469601Seric if (ipoptlen == 0) 120569601Seric goto noipsr; 120669637Seric o = (u_char *) ipopt.ipopt_list; 120769731Seric while (o != NULL && o < (u_char *) &ipopt + ipoptlen) 120869601Seric { 120969601Seric switch (*o) 121069601Seric { 121169601Seric case IPOPT_EOL: 121269601Seric o = NULL; 121369601Seric break; 121469601Seric 121569601Seric case IPOPT_NOP: 121669601Seric o++; 121769601Seric break; 121869601Seric 121969601Seric case IPOPT_SSRR: 122069601Seric case IPOPT_LSRR: 122169601Seric p = &hbuf[strlen(hbuf)]; 122269601Seric sprintf(p, " [%s@%s", 122369601Seric *o == IPOPT_SSRR ? "!" : "", 122469601Seric inet_ntoa(ipopt.ipopt_dst)); 122569601Seric p += strlen(p); 122669601Seric 122769601Seric /* o[1] is option length */ 122869601Seric j = *++o / sizeof(struct in_addr) - 1; 122969601Seric 123069601Seric /* q skips length and router pointer to data */ 123169601Seric q = o + 2; 123269601Seric for ( ; j >= 0; j--) 123369601Seric { 123469601Seric memcpy(&addr, q, sizeof(addr)); 123569637Seric sprintf(p, "%c%s", 123669601Seric j ? '@' : ':', 123769601Seric inet_ntoa(addr)); 123869637Seric p += strlen(p); 123969637Seric q += sizeof(struct in_addr); 124069601Seric } 124169601Seric o += *o; 124269601Seric break; 124369601Seric 124469601Seric default: 124569601Seric /* Skip over option */ 124669601Seric o += o[1]; 124769601Seric break; 124869601Seric } 124969601Seric } 125069601Seric strcat(hbuf,"]"); 125169601Seric goto postipsr; 125269601Seric } 125369601Seric #endif 125469601Seric 125569601Seric noipsr: 125666003Seric if (RealHostName != NULL && RealHostName[0] != '[') 125758951Seric { 125858951Seric p = &hbuf[strlen(hbuf)]; 125958951Seric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 126058951Seric } 126169601Seric 126269601Seric postipsr: 126358957Seric if (tTd(9, 1)) 126458951Seric printf("getauthinfo: %s\n", hbuf); 126558308Seric return hbuf; 126658308Seric } 126758308Seric /* 126860089Seric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 126953751Seric ** 127053751Seric ** Parameters: 127156823Seric ** map -- a pointer to this map (unused). 127260089Seric ** name -- the (presumably unqualified) hostname. 127360257Seric ** av -- unused -- for compatibility with other mapping 127455019Seric ** functions. 127559084Seric ** statp -- an exit status (out parameter) -- set to 127659084Seric ** EX_TEMPFAIL if the name server is unavailable. 127753751Seric ** 127853751Seric ** Returns: 127953751Seric ** The mapping, if found. 128053751Seric ** NULL if no mapping found. 128153751Seric ** 128253751Seric ** Side Effects: 128353751Seric ** Looks up the host specified in hbuf. If it is not 128453751Seric ** the canonical name for that host, return the canonical 128553751Seric ** name. 128653751Seric */ 128751315Seric 128853751Seric char * 128960257Seric host_map_lookup(map, name, av, statp) 129056823Seric MAP *map; 129160089Seric char *name; 129260257Seric char **av; 129359084Seric int *statp; 129416911Seric { 129516911Seric register struct hostent *hp; 129668693Seric struct in_addr in_addr; 129756823Seric char *cp; 129859671Seric register STAB *s; 129968693Seric char hbuf[MAXNAME + 1]; 130066334Seric #if NAMED_BIND 130159671Seric extern int h_errno; 130266029Seric #endif 130316911Seric 130425574Smiriam /* 130559671Seric ** See if we have already looked up this name. If so, just 130659671Seric ** return it. 130759671Seric */ 130853751Seric 130960089Seric s = stab(name, ST_NAMECANON, ST_ENTER); 131059671Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 131159671Seric { 131259986Seric if (tTd(9, 1)) 131360089Seric printf("host_map_lookup(%s) => CACHE %s\n", 131469511Seric name, 131569511Seric s->s_namecanon.nc_cname == NULL 131669511Seric ? "NULL" 131769512Seric : s->s_namecanon.nc_cname); 131859671Seric errno = s->s_namecanon.nc_errno; 131966334Seric #if NAMED_BIND 132059671Seric h_errno = s->s_namecanon.nc_herrno; 132166029Seric #endif 132259671Seric *statp = s->s_namecanon.nc_stat; 132368817Seric if (*statp == EX_TEMPFAIL) 132465199Seric { 132568857Seric CurEnv->e_status = "4.4.3"; 132668817Seric usrerr("451 %s: Name server timeout", 132765199Seric shortenstring(name, 33)); 132865199Seric } 132959671Seric return s->s_namecanon.nc_cname; 133059671Seric } 133159671Seric 133259671Seric /* 133359671Seric ** If first character is a bracket, then it is an address 133459671Seric ** lookup. Address is copied into a temporary buffer to 133560089Seric ** strip the brackets and to preserve name if address is 133659671Seric ** unknown. 133759671Seric */ 133859671Seric 133960089Seric if (*name != '[') 134053751Seric { 134155019Seric extern bool getcanonname(); 134255019Seric 134358798Seric if (tTd(9, 1)) 134460089Seric printf("host_map_lookup(%s) => ", name); 134559671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 134668693Seric if (strlen(name) < sizeof hbuf) 134768693Seric (void) strcpy(hbuf, name); 134868693Seric else 134968693Seric { 135068693Seric bcopy(name, hbuf, sizeof hbuf - 1); 135168693Seric hbuf[sizeof hbuf - 1] = '\0'; 135268693Seric } 135368759Seric if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon)) 135458796Seric { 135558796Seric if (tTd(9, 1)) 135658796Seric printf("%s\n", hbuf); 135760257Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av); 135860257Seric s->s_namecanon.nc_cname = newstr(cp); 135960257Seric return cp; 136058796Seric } 136153751Seric else 136258796Seric { 136359084Seric register struct hostent *hp; 136459084Seric 136566029Seric s->s_namecanon.nc_errno = errno; 136666334Seric #if NAMED_BIND 136766029Seric s->s_namecanon.nc_herrno = h_errno; 136858796Seric if (tTd(9, 1)) 136959084Seric printf("FAIL (%d)\n", h_errno); 137059084Seric switch (h_errno) 137159084Seric { 137259084Seric case TRY_AGAIN: 137359596Seric if (UseNameServer) 137459734Seric { 137568857Seric CurEnv->e_status = "4.4.3"; 137668817Seric usrerr("451 %s: Name server timeout", 137765199Seric shortenstring(name, 33)); 137859734Seric } 137959084Seric *statp = EX_TEMPFAIL; 138059084Seric break; 138159084Seric 138259084Seric case HOST_NOT_FOUND: 138368881Seric case NO_DATA: 138459084Seric *statp = EX_NOHOST; 138559084Seric break; 138659084Seric 138759084Seric case NO_RECOVERY: 138859084Seric *statp = EX_SOFTWARE; 138959084Seric break; 139059084Seric 139159084Seric default: 139259084Seric *statp = EX_UNAVAILABLE; 139359084Seric break; 139459084Seric } 139566029Seric #else 139666029Seric if (tTd(9, 1)) 139766029Seric printf("FAIL\n"); 139866029Seric *statp = EX_NOHOST; 139966029Seric #endif 140059671Seric s->s_namecanon.nc_stat = *statp; 140168693Seric if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) || 140268693Seric UseNameServer) 140359084Seric return NULL; 140459084Seric 140559084Seric /* 140659084Seric ** Try to look it up in /etc/hosts 140759084Seric */ 140859084Seric 140968693Seric hp = sm_gethostbyname(name); 141059084Seric if (hp == NULL) 141159084Seric { 141259084Seric /* no dice there either */ 141359671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 141459084Seric return NULL; 141559084Seric } 141659084Seric 141759671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 141860257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 141960257Seric s->s_namecanon.nc_cname = newstr(cp); 142060257Seric return cp; 142158796Seric } 142253751Seric } 142360089Seric if ((cp = strchr(name, ']')) == NULL) 142453751Seric return (NULL); 142540994Sbostic *cp = '\0'; 142668693Seric in_addr.s_addr = inet_addr(&name[1]); 142758110Seric 142858110Seric /* nope -- ask the name server */ 142968693Seric hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET); 143059671Seric s->s_namecanon.nc_errno = errno; 143166334Seric #if NAMED_BIND 143259671Seric s->s_namecanon.nc_herrno = h_errno; 143366029Seric #endif 143459671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 143533932Sbostic if (hp == NULL) 143659671Seric { 143759671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 143853751Seric return (NULL); 143959671Seric } 144053751Seric 144158110Seric /* found a match -- copy out */ 144260257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 144359671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 144460257Seric s->s_namecanon.nc_cname = newstr(cp); 144560257Seric return cp; 144633932Sbostic } 144758755Seric /* 144858755Seric ** ANYNET_NTOA -- convert a network address to printable form. 144958755Seric ** 145058755Seric ** Parameters: 145158755Seric ** sap -- a pointer to a sockaddr structure. 145258755Seric ** 145358755Seric ** Returns: 145458755Seric ** A printable version of that sockaddr. 145558755Seric */ 145616911Seric 1457*69881Seric #if NETLINK 145869516Seric # include <net/if_dl.h> 145969516Seric #endif 146069516Seric 146158755Seric char * 146258755Seric anynet_ntoa(sap) 146358755Seric register SOCKADDR *sap; 146458755Seric { 146558755Seric register char *bp; 146658755Seric register char *ap; 146758755Seric int l; 146864734Seric static char buf[100]; 146958755Seric 147058798Seric /* check for null/zero family */ 147158798Seric if (sap == NULL) 147258798Seric return "NULLADDR"; 147358798Seric if (sap->sa.sa_family == 0) 147458798Seric return "0"; 147558798Seric 147664734Seric switch (sap->sa.sa_family) 147764734Seric { 1478*69881Seric #if NETUNIX 147964734Seric case AF_UNIX: 148064758Seric if (sap->sunix.sun_path[0] != '\0') 148164758Seric sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); 148264734Seric else 148364734Seric sprintf(buf, "[UNIX: localhost]"); 148464734Seric return buf; 148564734Seric #endif 148664734Seric 1487*69881Seric #if NETINET 148864734Seric case AF_INET: 148968776Seric return inet_ntoa(sap->sin.sin_addr); 149058778Seric #endif 149158755Seric 1492*69881Seric #if NETLINK 149369516Seric case AF_LINK: 149469516Seric sprintf(buf, "[LINK: %s]", 149569516Seric link_ntoa((struct sockaddr_dl *) &sap->sa)); 149669516Seric return buf; 149769516Seric #endif 149864734Seric default: 149969516Seric /* this case is needed when nothing is #defined */ 150069516Seric /* in order to keep the switch syntactically correct */ 150169516Seric break; 150264734Seric } 150364734Seric 150458755Seric /* unknown family -- just dump bytes */ 150558778Seric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 150658755Seric bp = &buf[strlen(buf)]; 150758778Seric ap = sap->sa.sa_data; 150858778Seric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 150958755Seric { 151058755Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 151158755Seric bp += 3; 151258755Seric } 151358755Seric *--bp = '\0'; 151458755Seric return buf; 151558755Seric } 151658951Seric /* 151758951Seric ** HOSTNAMEBYANYADDR -- return name of host based on address 151858951Seric ** 151958951Seric ** Parameters: 152058951Seric ** sap -- SOCKADDR pointer 152158951Seric ** 152258951Seric ** Returns: 152358951Seric ** text representation of host name. 152458951Seric ** 152558951Seric ** Side Effects: 152658951Seric ** none. 152758951Seric */ 152858755Seric 152958951Seric char * 153058951Seric hostnamebyanyaddr(sap) 153158951Seric register SOCKADDR *sap; 153258951Seric { 153358951Seric register struct hostent *hp; 153464734Seric int saveretry; 153558951Seric 153666334Seric #if NAMED_BIND 153759042Seric /* shorten name server timeout to avoid higher level timeouts */ 153859042Seric saveretry = _res.retry; 153959042Seric _res.retry = 3; 154059042Seric #endif /* NAMED_BIND */ 154159042Seric 154258951Seric switch (sap->sa.sa_family) 154358951Seric { 1544*69881Seric #if NETINET 154558951Seric case AF_INET: 154668693Seric hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr, 154768693Seric INADDRSZ, 154858951Seric AF_INET); 154958951Seric break; 155058951Seric #endif 155158951Seric 1552*69881Seric #if NETISO 155358951Seric case AF_ISO: 155468693Seric hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr, 155558951Seric sizeof sap->siso.siso_addr, 155658951Seric AF_ISO); 155758951Seric break; 155858951Seric #endif 155958951Seric 156064734Seric case AF_UNIX: 156164734Seric hp = NULL; 156264734Seric break; 156364734Seric 156458951Seric default: 156568693Seric hp = sm_gethostbyaddr(sap->sa.sa_data, 156658951Seric sizeof sap->sa.sa_data, 156758951Seric sap->sa.sa_family); 156858951Seric break; 156958951Seric } 157058951Seric 157166334Seric #if NAMED_BIND 157259042Seric _res.retry = saveretry; 157359042Seric #endif /* NAMED_BIND */ 157459042Seric 157558951Seric if (hp != NULL) 157658951Seric return hp->h_name; 157758951Seric else 157858951Seric { 157958951Seric /* produce a dotted quad */ 158058951Seric static char buf[512]; 158158951Seric 158258951Seric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 158358951Seric return buf; 158458951Seric } 158558951Seric } 158658951Seric 158756795Seric # else /* DAEMON */ 158816911Seric /* code for systems without sophisticated networking */ 158910758Seric 159010758Seric /* 159110758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 159211297Seric ** 159311297Seric ** Can't convert to upper case here because might be a UUCP name. 159412313Seric ** 159512313Seric ** Mark, you can change this to be anything you want...... 159610758Seric */ 159710758Seric 159810758Seric char ** 159912313Seric myhostname(hostbuf, size) 160010758Seric char hostbuf[]; 160112313Seric int size; 160210758Seric { 160310758Seric register FILE *f; 160410758Seric 160510758Seric hostbuf[0] = '\0'; 160610758Seric f = fopen("/usr/include/whoami", "r"); 160710758Seric if (f != NULL) 160810758Seric { 160912313Seric (void) fgets(hostbuf, size, f); 161010758Seric fixcrlf(hostbuf, TRUE); 161110758Seric (void) fclose(f); 161210758Seric } 161310758Seric return (NULL); 161410758Seric } 161516911Seric /* 161658951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 161758308Seric ** 161858308Seric ** Parameters: 161958308Seric ** fd -- the descriptor 162058308Seric ** 162158308Seric ** Returns: 162258308Seric ** The host name associated with this descriptor, if it can 162358308Seric ** be determined. 162458308Seric ** NULL otherwise. 162558308Seric ** 162658308Seric ** Side Effects: 162758308Seric ** none 162858308Seric */ 162958308Seric 163058308Seric char * 163158951Seric getauthinfo(fd) 163258308Seric int fd; 163358308Seric { 163458308Seric return NULL; 163558308Seric } 163658308Seric /* 163716911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 163816911Seric ** 163916911Seric ** Parameters: 164056823Seric ** map -- a pointer to the database map. 164160089Seric ** name -- a buffer containing a hostname. 164253751Seric ** avp -- a pointer to a (cf file defined) argument vector. 164359084Seric ** statp -- an exit status (out parameter). 164416911Seric ** 164516911Seric ** Returns: 164653751Seric ** mapped host name 164751315Seric ** FALSE otherwise. 164816911Seric ** 164916911Seric ** Side Effects: 165060089Seric ** Looks up the host specified in name. If it is not 165116911Seric ** the canonical name for that host, replace it with 165216911Seric ** the canonical name. If the name is unknown, or it 165316911Seric ** is already the canonical name, leave it unchanged. 165416911Seric */ 165510758Seric 165616911Seric /*ARGSUSED*/ 165753751Seric char * 165860089Seric host_map_lookup(map, name, avp, statp) 165956823Seric MAP *map; 166060089Seric char *name; 166153751Seric char **avp; 166259084Seric char *statp; 166316911Seric { 166459084Seric register struct hostent *hp; 166559084Seric 166668693Seric hp = sm_gethostbyname(name); 166759084Seric if (hp != NULL) 166859084Seric return hp->h_name; 166959084Seric *statp = EX_NOHOST; 167053751Seric return NULL; 167116911Seric } 167216911Seric 167356795Seric #endif /* DAEMON */ 1674