122700Sdist /* 234920Sbostic * Copyright (c) 1983 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*68462Seric static char sccsid[] = "@(#)daemon.c 8.71 (Berkeley) 02/28/95 (with daemon mode)"; 1533780Sbostic #else 16*68462Seric static char sccsid[] = "@(#)daemon.c 8.71 (Berkeley) 02/28/95 (without daemon mode)"; 1733780Sbostic #endif 1833780Sbostic #endif /* not lint */ 194535Seric 2033780Sbostic #ifdef DAEMON 2133780Sbostic 2223120Seric # include <netdb.h> 2364338Seric # include <arpa/inet.h> 245978Seric 2566334Seric #if NAMED_BIND 2659042Seric # include <resolv.h> 2759042Seric #endif 2859042Seric 294535Seric /* 304535Seric ** DAEMON.C -- routines to use when running as a daemon. 317556Seric ** 327556Seric ** This entire file is highly dependent on the 4.2 BSD 337556Seric ** interprocess communication primitives. No attempt has 347556Seric ** been made to make this file portable to Version 7, 357556Seric ** Version 6, MPX files, etc. If you should try such a 367556Seric ** thing yourself, I recommend chucking the entire file 377556Seric ** and starting from scratch. Basic semantics are: 387556Seric ** 397556Seric ** getrequests() 407556Seric ** Opens a port and initiates a connection. 417556Seric ** Returns in a child. Must set InChannel and 427556Seric ** OutChannel appropriately. 4310206Seric ** clrdaemon() 4410206Seric ** Close any open files associated with getting 4510206Seric ** the connection; this is used when running the queue, 4610206Seric ** etc., to avoid having extra file descriptors during 4710206Seric ** the queue run and to avoid confusing the network 4810206Seric ** code (if it cares). 4952106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 507556Seric ** Make a connection to the named host on the given 517556Seric ** port. Set *outfile and *infile to the files 527556Seric ** appropriate for communication. Returns zero on 537556Seric ** success, else an exit status describing the 547556Seric ** error. 5560089Seric ** host_map_lookup(map, hbuf, avp, pstat) 5656823Seric ** Convert the entry in hbuf into a canonical form. 574535Seric */ 584535Seric /* 594535Seric ** GETREQUESTS -- open mail IPC port and get requests. 604535Seric ** 614535Seric ** Parameters: 624535Seric ** none. 634535Seric ** 644535Seric ** Returns: 654535Seric ** none. 664535Seric ** 674535Seric ** Side Effects: 684535Seric ** Waits until some interesting activity occurs. When 694535Seric ** it does, a child is created to process it, and the 704535Seric ** parent waits for completion. Return from this 719886Seric ** routine is always in the child. The file pointers 729886Seric ** "InChannel" and "OutChannel" should be set to point 739886Seric ** to the communication channel. 744535Seric */ 754535Seric 7658849Seric int DaemonSocket = -1; /* fd describing socket */ 7758849Seric SOCKADDR DaemonAddr; /* socket for incoming */ 7859783Seric int ListenQueueSize = 10; /* size of listen queue */ 7964381Seric int TcpRcvBufferSize = 0; /* size of TCP receive buffer */ 8064381Seric int TcpSndBufferSize = 0; /* size of TCP send buffer */ 8116144Seric 82*68462Seric void 834535Seric getrequests() 844535Seric { 859610Seric int t; 8653751Seric bool refusingconnections = TRUE; 8758419Seric FILE *pidf; 8864828Seric int socksize; 8966793Seric #ifdef XDEBUG 9066793Seric bool j_has_dot; 9166793Seric #endif 9246928Sbostic extern void reapchild(); 937117Seric 949610Seric /* 959610Seric ** Set up the address for the mailer. 969610Seric */ 979610Seric 9858849Seric if (DaemonAddr.sin.sin_family == 0) 9958849Seric DaemonAddr.sin.sin_family = AF_INET; 10058849Seric if (DaemonAddr.sin.sin_addr.s_addr == 0) 10158849Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 10258849Seric if (DaemonAddr.sin.sin_port == 0) 1039610Seric { 10465169Seric register struct servent *sp; 10565169Seric 10658849Seric sp = getservbyname("smtp", "tcp"); 10758849Seric if (sp == NULL) 10858849Seric { 10958909Seric syserr("554 service \"smtp\" unknown"); 11065169Seric DaemonAddr.sin.sin_port = htons(25); 11158849Seric } 11265169Seric else 11365169Seric DaemonAddr.sin.sin_port = sp->s_port; 1149610Seric } 1159610Seric 1169610Seric /* 1179610Seric ** Try to actually open the connection. 1189610Seric */ 1199610Seric 1209610Seric if (tTd(15, 1)) 12158849Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1229610Seric 1239610Seric /* get a socket for the SMTP connection */ 12466854Seric socksize = opendaemonsocket(TRUE); 12510347Seric 12664035Seric (void) setsignal(SIGCHLD, reapchild); 12724945Seric 12858419Seric /* write the pid to the log file for posterity */ 12958419Seric pidf = fopen(PidFile, "w"); 13058419Seric if (pidf != NULL) 13158419Seric { 13263863Seric extern char *CommandLineArgs; 13363863Seric 13463863Seric /* write the process id on line 1 */ 13558419Seric fprintf(pidf, "%d\n", getpid()); 13663863Seric 13763863Seric /* line 2 contains all command line flags */ 13863863Seric fprintf(pidf, "%s\n", CommandLineArgs); 13963863Seric 14063863Seric /* flush and close */ 14158419Seric fclose(pidf); 14258419Seric } 14358419Seric 14466793Seric #ifdef XDEBUG 14566793Seric { 14666812Seric char jbuf[MAXHOSTNAMELEN]; 14758419Seric 14866812Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 14966812Seric j_has_dot = strchr(jbuf, '.') != NULL; 15066793Seric } 15166793Seric #endif 15266793Seric 1539610Seric if (tTd(15, 1)) 15410206Seric printf("getrequests: %d\n", DaemonSocket); 1559610Seric 1564631Seric for (;;) 1574631Seric { 15814875Seric register int pid; 15911147Seric auto int lotherend; 16053751Seric extern bool refuseconnections(); 161*68462Seric extern int getla(); 16211147Seric 16314875Seric /* see if we are rejecting connections */ 16453751Seric CurrentLA = getla(); 16553751Seric if (refuseconnections()) 16636584Sbostic { 16766845Seric if (DaemonSocket >= 0) 16853751Seric { 16966845Seric /* close socket so peer will fail quickly */ 17066845Seric (void) close(DaemonSocket); 17166845Seric DaemonSocket = -1; 17253751Seric } 17366845Seric refusingconnections = TRUE; 17457385Seric setproctitle("rejecting connections: load average: %d", 17557385Seric CurrentLA); 17666845Seric sleep(15); 17753751Seric continue; 17836584Sbostic } 17914875Seric 180*68462Seric /* arrange to (re)open the socket if necessary */ 18153751Seric if (refusingconnections) 18253751Seric { 18367690Seric (void) opendaemonsocket(FALSE); 18453751Seric setproctitle("accepting connections"); 18553751Seric refusingconnections = FALSE; 18653751Seric } 18753751Seric 18866793Seric #ifdef XDEBUG 18966793Seric /* check for disaster */ 19066793Seric { 19166812Seric char jbuf[MAXHOSTNAMELEN]; 19266793Seric 19366812Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 194*68462Seric if (!wordinclass(jbuf, 'w')) 19566793Seric { 19666793Seric dumpstate("daemon lost $j"); 19766793Seric syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog"); 19866793Seric abort(); 19966793Seric } 20066812Seric else if (j_has_dot && strchr(jbuf, '.') == NULL) 20166793Seric { 20266793Seric dumpstate("daemon $j lost dot"); 20366793Seric syslog(LOG_ALERT, "daemon process $j lost dot; see syslog"); 20466793Seric abort(); 20566793Seric } 20666793Seric } 20766793Seric #endif 20866793Seric 2099610Seric /* wait for a connection */ 2109610Seric do 2119610Seric { 2129610Seric errno = 0; 21364828Seric lotherend = socksize; 21446928Sbostic t = accept(DaemonSocket, 21546928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2169610Seric } while (t < 0 && errno == EINTR); 2179610Seric if (t < 0) 2185978Seric { 2199610Seric syserr("getrequests: accept"); 220*68462Seric 221*68462Seric /* arrange to re-open the socket next time around */ 222*68462Seric (void) close(DaemonSocket); 223*68462Seric DaemonSocket = -1; 2249610Seric sleep(5); 2259610Seric continue; 2265978Seric } 2274631Seric 2285978Seric /* 2295978Seric ** Create a subprocess to process the mail. 2305978Seric */ 2315978Seric 2327677Seric if (tTd(15, 2)) 2339610Seric printf("getrequests: forking (fd = %d)\n", t); 2345978Seric 2354636Seric pid = fork(); 2364636Seric if (pid < 0) 2374631Seric { 2384636Seric syserr("daemon: cannot fork"); 2394636Seric sleep(10); 2409610Seric (void) close(t); 2414636Seric continue; 2424631Seric } 2434631Seric 2444636Seric if (pid == 0) 2454631Seric { 24664086Seric char *p; 24758951Seric extern char *hostnamebyanyaddr(); 248*68462Seric extern void intsig(); 24911147Seric 2504636Seric /* 2514636Seric ** CHILD -- return to caller. 25211147Seric ** Collect verified idea of sending host. 2534636Seric ** Verify calling user id if possible here. 2544636Seric */ 2554631Seric 25664035Seric (void) setsignal(SIGCHLD, SIG_DFL); 257*68462Seric (void) setsignal(SIGHUP, intsig); 258*68462Seric (void) close(DaemonSocket); 25966017Seric DisConnected = FALSE; 26024950Seric 26166032Seric setproctitle("startup with %s", 26266032Seric anynet_ntoa(&RealHostAddr)); 26366032Seric 26411147Seric /* determine host name */ 26564086Seric p = hostnamebyanyaddr(&RealHostAddr); 26664086Seric RealHostName = newstr(p); 26766032Seric setproctitle("startup with %s", p); 26858778Seric 26955173Seric #ifdef LOG 27063842Seric if (LogLevel > 11) 27155173Seric { 27255173Seric /* log connection information */ 27355173Seric syslog(LOG_INFO, "connect from %s (%s)", 27458951Seric RealHostName, anynet_ntoa(&RealHostAddr)); 27555173Seric } 27655173Seric #endif 27755173Seric 27864724Seric if ((InChannel = fdopen(t, "r")) == NULL || 27964724Seric (t = dup(t)) < 0 || 28064724Seric (OutChannel = fdopen(t, "w")) == NULL) 28164724Seric { 28264724Seric syserr("cannot open SMTP server channel, fd=%d", t); 28364724Seric exit(0); 28464724Seric } 28559254Seric 28616884Seric /* should we check for illegal connection here? XXX */ 28759156Seric #ifdef XLA 28859156Seric if (!xla_host_ok(RealHostName)) 28959156Seric { 29059254Seric message("421 Too many SMTP sessions for this host"); 29159156Seric exit(0); 29259156Seric } 29359156Seric #endif 29416884Seric 2957677Seric if (tTd(15, 2)) 2965978Seric printf("getreq: returning\n"); 2974636Seric return; 2984631Seric } 2994631Seric 3007117Seric /* close the port so that others will hang (for a while) */ 3019610Seric (void) close(t); 3024631Seric } 3039886Seric /*NOTREACHED*/ 3044631Seric } 3055978Seric /* 30666845Seric ** OPENDAEMONSOCKET -- open the SMTP socket 30766845Seric ** 30866845Seric ** Deals with setting all appropriate options. DaemonAddr must 30966845Seric ** be set up in advance. 31066845Seric ** 31166845Seric ** Parameters: 31266854Seric ** firsttime -- set if this is the initial open. 31366845Seric ** 31466845Seric ** Returns: 31566845Seric ** Size in bytes of the daemon socket addr. 31666845Seric ** 31766845Seric ** Side Effects: 31866845Seric ** Leaves DaemonSocket set to the open socket. 31966845Seric ** Exits if the socket cannot be created. 32066845Seric */ 32166845Seric 32266861Seric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */ 32366861Seric 32466845Seric int 32566854Seric opendaemonsocket(firsttime) 32666854Seric bool firsttime; 32766845Seric { 32866845Seric int on = 1; 329*68462Seric int socksize = 0; 33066861Seric int ntries = 0; 33166861Seric int saveerrno; 33266845Seric 33366845Seric if (tTd(15, 2)) 33466845Seric printf("opendaemonsocket()\n"); 33566845Seric 33666861Seric do 33766845Seric { 33866862Seric if (ntries > 0) 33966862Seric sleep(5); 34066861Seric if (firsttime || DaemonSocket < 0) 34166854Seric { 34266861Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 34366861Seric if (DaemonSocket < 0) 34466861Seric { 34566861Seric /* probably another daemon already */ 34666861Seric saveerrno = errno; 34766861Seric syserr("opendaemonsocket: can't create server SMTP socket"); 34866861Seric severe: 34966845Seric # ifdef LOG 35066861Seric if (LogLevel > 0) 35166861Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 35266845Seric # endif /* LOG */ 35366861Seric DaemonSocket = -1; 35466861Seric continue; 35566861Seric } 35666845Seric 35766861Seric /* turn on network debugging? */ 35866861Seric if (tTd(15, 101)) 35966861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36066861Seric SO_DEBUG, (char *)&on, 36166861Seric sizeof on); 36266845Seric 36366861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36466861Seric SO_REUSEADDR, (char *)&on, sizeof on); 36566861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36666861Seric SO_KEEPALIVE, (char *)&on, sizeof on); 36766845Seric 36866845Seric #ifdef SO_RCVBUF 36966861Seric if (TcpRcvBufferSize > 0) 37066861Seric { 37166861Seric if (setsockopt(DaemonSocket, SOL_SOCKET, 37266861Seric SO_RCVBUF, 37366861Seric (char *) &TcpRcvBufferSize, 37466861Seric sizeof(TcpRcvBufferSize)) < 0) 37566861Seric syserr("getrequests: setsockopt(SO_RCVBUF)"); 37666861Seric } 37766845Seric #endif 37866845Seric 37966861Seric switch (DaemonAddr.sa.sa_family) 38066861Seric { 38166845Seric # ifdef NETINET 38266861Seric case AF_INET: 38366861Seric socksize = sizeof DaemonAddr.sin; 38466861Seric break; 38566845Seric # endif 38666845Seric 38766845Seric # ifdef NETISO 38866861Seric case AF_ISO: 38966861Seric socksize = sizeof DaemonAddr.siso; 39066861Seric break; 39166845Seric # endif 39266845Seric 39366861Seric default: 39466861Seric socksize = sizeof DaemonAddr; 39566861Seric break; 39666861Seric } 39766861Seric 39866861Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0) 39966861Seric { 40066861Seric saveerrno = errno; 40166861Seric syserr("getrequests: cannot bind"); 40266861Seric (void) close(DaemonSocket); 40366861Seric goto severe; 40466861Seric } 40566854Seric } 40666861Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0) 40766854Seric { 40866861Seric saveerrno = errno; 40966861Seric syserr("getrequests: cannot listen"); 41066854Seric (void) close(DaemonSocket); 41166854Seric goto severe; 41266854Seric } 41366861Seric return socksize; 41466861Seric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno)); 415*68462Seric syserr("!opendaemonsocket: server SMTP socket wedged: exiting"); 41666861Seric finis(); 41766845Seric } 41866845Seric /* 41910206Seric ** CLRDAEMON -- reset the daemon connection 42010206Seric ** 42110206Seric ** Parameters: 42210206Seric ** none. 42310206Seric ** 42410206Seric ** Returns: 42510206Seric ** none. 42610206Seric ** 42710206Seric ** Side Effects: 42810206Seric ** releases any resources used by the passive daemon. 42910206Seric */ 43010206Seric 431*68462Seric void 43210206Seric clrdaemon() 43310206Seric { 43410206Seric if (DaemonSocket >= 0) 43510206Seric (void) close(DaemonSocket); 43610206Seric DaemonSocket = -1; 43710206Seric } 43810206Seric /* 43958849Seric ** SETDAEMONOPTIONS -- set options for running the daemon 44058849Seric ** 44158849Seric ** Parameters: 44258849Seric ** p -- the options line. 44358849Seric ** 44458849Seric ** Returns: 44558849Seric ** none. 44658849Seric */ 44758849Seric 448*68462Seric void 44958849Seric setdaemonoptions(p) 45058849Seric register char *p; 45158849Seric { 45258873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 45358873Seric DaemonAddr.sa.sa_family = AF_INET; 45458873Seric 45558849Seric while (p != NULL) 45658849Seric { 45758849Seric register char *f; 45858849Seric register char *v; 45958849Seric 46058849Seric while (isascii(*p) && isspace(*p)) 46158849Seric p++; 46258849Seric if (*p == '\0') 46358849Seric break; 46458849Seric f = p; 46558849Seric p = strchr(p, ','); 46658849Seric if (p != NULL) 46758849Seric *p++ = '\0'; 46858849Seric v = strchr(f, '='); 46958849Seric if (v == NULL) 47058849Seric continue; 47158849Seric while (isascii(*++v) && isspace(*v)) 47258849Seric continue; 47358849Seric 47458849Seric switch (*f) 47558849Seric { 47658873Seric case 'F': /* address family */ 47758849Seric if (isascii(*v) && isdigit(*v)) 47858873Seric DaemonAddr.sa.sa_family = atoi(v); 47958873Seric #ifdef NETINET 48058873Seric else if (strcasecmp(v, "inet") == 0) 48158873Seric DaemonAddr.sa.sa_family = AF_INET; 48258873Seric #endif 48358873Seric #ifdef NETISO 48458873Seric else if (strcasecmp(v, "iso") == 0) 48558873Seric DaemonAddr.sa.sa_family = AF_ISO; 48658873Seric #endif 48758873Seric #ifdef NETNS 48858873Seric else if (strcasecmp(v, "ns") == 0) 48958873Seric DaemonAddr.sa.sa_family = AF_NS; 49058873Seric #endif 49158873Seric #ifdef 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 { 50258873Seric #ifdef NETINET 50358873Seric case AF_INET: 50458873Seric if (isascii(*v) && isdigit(*v)) 505*68462Seric 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 53158873Seric #ifdef 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 54858873Seric #ifdef 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; 58558849Seric } 58658849Seric } 58758849Seric } 58858849Seric /* 5896039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 5906039Seric ** 5916039Seric ** Parameters: 5926039Seric ** host -- the name of the host. 5936633Seric ** port -- the port number to connect to. 59453739Seric ** mci -- a pointer to the mail connection information 59553739Seric ** structure to be filled in. 59652106Seric ** usesecureport -- if set, use a low numbered (reserved) 59752106Seric ** port to provide some rudimentary authentication. 5986039Seric ** 5996039Seric ** Returns: 6006039Seric ** An exit code telling whether the connection could be 6016039Seric ** made and if not why not. 6026039Seric ** 6036039Seric ** Side Effects: 6046039Seric ** none. 6056039Seric */ 6065978Seric 60758755Seric SOCKADDR CurHostAddr; /* address of current host */ 60858305Seric 60954967Seric int 61053739Seric makeconnection(host, port, mci, usesecureport) 6116039Seric char *host; 6127286Seric u_short port; 61354967Seric register MCI *mci; 61452106Seric bool usesecureport; 6156039Seric { 616*68462Seric register int i = 0; 617*68462Seric register int s; 61829430Sbloom register struct hostent *hp = (struct hostent *)NULL; 61958755Seric SOCKADDR addr; 62052106Seric int sav_errno; 62158755Seric int addrlen; 622*68462Seric bool firstconnect; 62366334Seric #if NAMED_BIND 62435651Seric extern int h_errno; 62535651Seric #endif 6266039Seric 6276039Seric /* 6286039Seric ** Set up the address for the mailer. 6299308Seric ** Accept "[a.b.c.d]" syntax for host name. 6306039Seric */ 6316039Seric 63266334Seric #if NAMED_BIND 63325475Smiriam h_errno = 0; 63435651Seric #endif 63525475Smiriam errno = 0; 63658864Seric bzero(&CurHostAddr, sizeof CurHostAddr); 63764334Seric SmtpPhase = mci->mci_phase = "initial connection"; 63858906Seric CurHostName = host; 63925475Smiriam 6409308Seric if (host[0] == '[') 6419308Seric { 64211147Seric long hid; 64356795Seric register char *p = strchr(host, ']'); 6449308Seric 64511147Seric if (p != NULL) 6469308Seric { 64711147Seric *p = '\0'; 64859884Seric #ifdef NETINET 64911147Seric hid = inet_addr(&host[1]); 65058360Seric if (hid == -1) 65159884Seric #endif 65258360Seric { 65358360Seric /* try it as a host name (avoid MX lookup) */ 65458360Seric hp = gethostbyname(&host[1]); 65566349Seric if (hp == NULL && p[-1] == '.') 65666349Seric { 657*68462Seric #if NAMED_BIND 658*68462Seric int oldopts = _res.options; 659*68462Seric 660*68462Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 661*68462Seric #endif 66266349Seric p[-1] = '\0'; 66366349Seric hp = gethostbyname(&host[1]); 66466349Seric p[-1] = '.'; 665*68462Seric #if NAMED_BIND 666*68462Seric _res.options = oldopts; 667*68462Seric #endif 66866349Seric } 66958360Seric *p = ']'; 67058360Seric goto gothostent; 67158360Seric } 67211147Seric *p = ']'; 6739308Seric } 67458360Seric if (p == NULL) 6759308Seric { 67658151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 6779308Seric return (EX_NOHOST); 6789308Seric } 67959884Seric #ifdef NETINET 68059884Seric addr.sin.sin_family = AF_INET; /*XXX*/ 68158778Seric addr.sin.sin_addr.s_addr = hid; 68259884Seric #endif 6839308Seric } 6849610Seric else 6859610Seric { 68666349Seric register char *p = &host[strlen(host) - 1]; 68766349Seric 68829430Sbloom hp = gethostbyname(host); 68966349Seric if (hp == NULL && *p == '.') 69066349Seric { 691*68462Seric #if NAMED_BIND 692*68462Seric int oldopts = _res.options; 693*68462Seric 694*68462Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 695*68462Seric #endif 69666349Seric *p = '\0'; 69766349Seric hp = gethostbyname(host); 69866349Seric *p = '.'; 699*68462Seric #if NAMED_BIND 700*68462Seric _res.options = oldopts; 701*68462Seric #endif 70266349Seric } 70358360Seric gothostent: 70425475Smiriam if (hp == NULL) 70524945Seric { 70666334Seric #if NAMED_BIND 707*68462Seric /* check for name server timeouts */ 708*68462Seric if (errno == ETIMEDOUT || h_errno == TRY_AGAIN || 709*68462Seric (errno == ECONNREFUSED && UseNameServer)) 710*68462Seric { 711*68462Seric mci->mci_status = "4.4.3"; 71225475Smiriam return (EX_TEMPFAIL); 713*68462Seric } 71435651Seric #endif 71525475Smiriam return (EX_NOHOST); 71624945Seric } 71758778Seric addr.sa.sa_family = hp->h_addrtype; 71858778Seric switch (hp->h_addrtype) 71958778Seric { 72058778Seric #ifdef NETINET 72158778Seric case AF_INET: 72258755Seric bcopy(hp->h_addr, 72358778Seric &addr.sin.sin_addr, 724*68462Seric INADDRSZ); 72558778Seric break; 72658778Seric #endif 72758778Seric 72858778Seric default: 72958755Seric bcopy(hp->h_addr, 73058778Seric addr.sa.sa_data, 73158755Seric hp->h_length); 73258778Seric break; 73358778Seric } 73429430Sbloom i = 1; 7359610Seric } 7369610Seric 7379610Seric /* 7389610Seric ** Determine the port number. 7399610Seric */ 7409610Seric 74110011Seric if (port != 0) 74258755Seric port = htons(port); 74310011Seric else 7449610Seric { 7459610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 7469610Seric 7479610Seric if (sp == NULL) 7489610Seric { 74958909Seric syserr("554 makeconnection: service \"smtp\" unknown"); 75065169Seric port = htons(25); 7519610Seric } 75265169Seric else 75365169Seric port = sp->s_port; 7549610Seric } 7556039Seric 75658778Seric switch (addr.sa.sa_family) 75758755Seric { 75859884Seric #ifdef NETINET 75958755Seric case AF_INET: 76058778Seric addr.sin.sin_port = port; 76158755Seric addrlen = sizeof (struct sockaddr_in); 76258755Seric break; 76359884Seric #endif 76458755Seric 76558755Seric #ifdef NETISO 76658755Seric case AF_ISO: 76758755Seric /* assume two byte transport selector */ 76858755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 76958755Seric addrlen = sizeof (struct sockaddr_iso); 77058755Seric break; 77158755Seric #endif 77258755Seric 77358755Seric default: 77458778Seric syserr("Can't connect to address family %d", addr.sa.sa_family); 77558755Seric return (EX_NOHOST); 77658755Seric } 77758755Seric 7786039Seric /* 7796039Seric ** Try to actually open the connection. 7806039Seric */ 7816039Seric 78259156Seric #ifdef XLA 78359156Seric /* if too many connections, don't bother trying */ 78459156Seric if (!xla_noqueue_ok(host)) 78559156Seric return EX_TEMPFAIL; 78659156Seric #endif 78759156Seric 788*68462Seric firstconnect = TRUE; 78957736Seric for (;;) 79052106Seric { 79157736Seric if (tTd(16, 1)) 79258755Seric printf("makeconnection (%s [%s])\n", 79358755Seric host, anynet_ntoa(&addr)); 79452106Seric 79558588Seric /* save for logging */ 79658588Seric CurHostAddr = addr; 79758588Seric 79857736Seric if (usesecureport) 79957736Seric { 80057736Seric int rport = IPPORT_RESERVED - 1; 8016039Seric 80257736Seric s = rresvport(&rport); 80357736Seric } 80457736Seric else 80557736Seric { 80657736Seric s = socket(AF_INET, SOCK_STREAM, 0); 80757736Seric } 80857736Seric if (s < 0) 80957736Seric { 81057736Seric sav_errno = errno; 81157736Seric syserr("makeconnection: no socket"); 81257736Seric goto failure; 81357736Seric } 81410347Seric 81564381Seric #ifdef SO_SNDBUF 81664381Seric if (TcpSndBufferSize > 0) 81764381Seric { 81864381Seric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 81964561Seric (char *) &TcpSndBufferSize, 82064381Seric sizeof(TcpSndBufferSize)) < 0) 82164381Seric syserr("makeconnection: setsockopt(SO_SNDBUF)"); 82264381Seric } 82364381Seric #endif 82464381Seric 82557736Seric if (tTd(16, 1)) 82657736Seric printf("makeconnection: fd=%d\n", s); 82757736Seric 82857736Seric /* turn on network debugging? */ 82957736Seric if (tTd(16, 101)) 83057736Seric { 83157736Seric int on = 1; 83266861Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 83357736Seric (char *)&on, sizeof on); 83457736Seric } 83557736Seric if (CurEnv->e_xfp != NULL) 83657736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 83757736Seric errno = 0; /* for debugging */ 83858755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 83957736Seric break; 84057736Seric 841*68462Seric /* if running demand-dialed connection, try again */ 842*68462Seric if (DialDelay > 0 && firstconnect) 843*68462Seric { 844*68462Seric if (tTd(16, 1)) 845*68462Seric printf("Connect failed (%s); trying again...\n", 846*68462Seric errstring(sav_errno)); 847*68462Seric firstconnect = FALSE; 848*68462Seric sleep(DialDelay); 849*68462Seric continue; 850*68462Seric } 851*68462Seric 85257736Seric /* couldn't connect.... figure out why */ 85327744Sbloom sav_errno = errno; 85427744Sbloom (void) close(s); 855*68462Seric if (hp != NULL && hp->h_addr_list[i]) 85629430Sbloom { 85757736Seric if (tTd(16, 1)) 85858755Seric printf("Connect failed (%s); trying new address....\n", 85958755Seric errstring(sav_errno)); 86058778Seric switch (addr.sa.sa_family) 86158778Seric { 86258778Seric #ifdef NETINET 86358778Seric case AF_INET: 86458755Seric bcopy(hp->h_addr_list[i++], 86558778Seric &addr.sin.sin_addr, 866*68462Seric INADDRSZ); 86758778Seric break; 86858778Seric #endif 86958778Seric 87058778Seric default: 87158755Seric bcopy(hp->h_addr_list[i++], 87258778Seric addr.sa.sa_data, 87352106Seric hp->h_length); 87458778Seric break; 87558778Seric } 87657736Seric continue; 87729430Sbloom } 87829430Sbloom 8796039Seric /* failure, decide if temporary or not */ 8806039Seric failure: 88159254Seric #ifdef XLA 88259254Seric xla_host_end(host); 88359254Seric #endif 88458542Seric if (transienterror(sav_errno)) 88558542Seric return EX_TEMPFAIL; 88658542Seric else 88758542Seric { 88858542Seric message("%s", errstring(sav_errno)); 88958542Seric return (EX_UNAVAILABLE); 8906039Seric } 8916039Seric } 8926039Seric 8936039Seric /* connection ok, put it into canonical form */ 89464724Seric if ((mci->mci_out = fdopen(s, "w")) == NULL || 89564724Seric (s = dup(s)) < 0 || 89664725Seric (mci->mci_in = fdopen(s, "r")) == NULL) 89764724Seric { 89864724Seric syserr("cannot open SMTP client channel, fd=%d", s); 89964724Seric return EX_TEMPFAIL; 90064724Seric } 9016039Seric 90210098Seric return (EX_OK); 9036039Seric } 90410758Seric /* 90510758Seric ** MYHOSTNAME -- return the name of this host. 90610758Seric ** 90710758Seric ** Parameters: 90810758Seric ** hostbuf -- a place to return the name of this host. 90912313Seric ** size -- the size of hostbuf. 91010758Seric ** 91110758Seric ** Returns: 91210758Seric ** A list of aliases for this host. 91310758Seric ** 91410758Seric ** Side Effects: 91564338Seric ** Adds numeric codes to $=w. 91610758Seric */ 9176039Seric 918*68462Seric struct hostent * 91912313Seric myhostname(hostbuf, size) 92010758Seric char hostbuf[]; 92112313Seric int size; 92210758Seric { 92358110Seric register struct hostent *hp; 92410758Seric extern struct hostent *gethostbyname(); 925*68462Seric extern bool getcanonname(); 926*68462Seric extern int h_errno; 92710758Seric 92823120Seric if (gethostname(hostbuf, size) < 0) 92923120Seric { 93023120Seric (void) strcpy(hostbuf, "localhost"); 93123120Seric } 93211147Seric hp = gethostbyname(hostbuf); 93366853Seric if (hp == NULL) 934*68462Seric return NULL; 935*68462Seric if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL) 93667448Seric { 937*68462Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 938*68462Seric hostbuf[size - 1] = '\0'; 93967448Seric } 94066853Seric 94166853Seric #if NAMED_BIND 942*68462Seric /* 943*68462Seric ** If still no dot, try DNS directly (i.e., avoid NIS problems). 944*68462Seric ** This ought to be driven from the configuration file, but 945*68462Seric ** we are called before the configuration is read. We could 946*68462Seric ** check for an /etc/resolv.conf file, but that isn't required. 947*68462Seric ** All in all, a bit of a mess. 948*68462Seric */ 949*68462Seric 950*68462Seric if (strchr(hostbuf, '.') == NULL && 951*68462Seric !getcanonname(hostbuf, size, TRUE) && 952*68462Seric h_errno == TRY_AGAIN) 95368457Seric { 95466853Seric /* try twice in case name server not yet started up */ 955*68462Seric message("My unqualifed host name (%s) unknown to DNS; sleeping for retry", 956*68462Seric hostbuf); 957*68462Seric sleep(60); 958*68462Seric if (!getcanonname(hostbuf, size, TRUE)) 95966853Seric errno = h_errno + E_DNSBASE; 96066853Seric } 96166777Seric #endif 962*68462Seric return (hp); 96310758Seric } 96451315Seric /* 96558951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 96658308Seric ** 96758951Seric ** Uses RFC1413 protocol to try to get info from the other end. 96858951Seric ** 96958308Seric ** Parameters: 97058308Seric ** fd -- the descriptor 97158308Seric ** 97258308Seric ** Returns: 97358951Seric ** The user@host information associated with this descriptor. 97458308Seric */ 97558308Seric 97658951Seric static jmp_buf CtxAuthTimeout; 97758951Seric 978*68462Seric static void 97958951Seric authtimeout() 98058951Seric { 98158951Seric longjmp(CtxAuthTimeout, 1); 98258951Seric } 98358951Seric 98458308Seric char * 98558951Seric getauthinfo(fd) 98658308Seric int fd; 98758308Seric { 98858951Seric int falen; 98959104Seric register char *p; 99058951Seric SOCKADDR la; 99158951Seric int lalen; 99258951Seric register struct servent *sp; 99358951Seric int s; 99458951Seric int i; 99558951Seric EVENT *ev; 99668444Seric int nleft; 997*68462Seric char ibuf[MAXNAME + 1]; 99858951Seric static char hbuf[MAXNAME * 2 + 2]; 99958951Seric extern char *hostnamebyanyaddr(); 100058951Seric extern char RealUserName[]; /* main.c */ 100158308Seric 100266761Seric falen = sizeof RealHostAddr; 1003*68462Seric if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 || 1004*68462Seric falen <= 0 || RealHostAddr.sa.sa_family == 0) 100558951Seric { 100658951Seric (void) sprintf(hbuf, "%s@localhost", RealUserName); 100758957Seric if (tTd(9, 1)) 100858951Seric printf("getauthinfo: %s\n", hbuf); 100958951Seric return hbuf; 101058951Seric } 101158951Seric 101266761Seric if (RealHostName == NULL) 101366761Seric { 101466761Seric /* translate that to a host name */ 101566761Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 101666761Seric } 101766761Seric 101865831Seric if (TimeOuts.to_ident == 0) 101965831Seric goto noident; 102065831Seric 102158951Seric lalen = sizeof la; 102266761Seric if (RealHostAddr.sa.sa_family != AF_INET || 102358951Seric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 102458951Seric la.sa.sa_family != AF_INET) 102558951Seric { 102658951Seric /* no ident info */ 102758951Seric goto noident; 102858951Seric } 102958951Seric 103058951Seric /* create ident query */ 103168457Seric (void) sprintf(ibuf, "%d,%d\r\n", 103266761Seric ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); 103358951Seric 103458951Seric /* create local address */ 103564747Seric la.sin.sin_port = 0; 103658951Seric 103758951Seric /* create foreign address */ 103858951Seric sp = getservbyname("auth", "tcp"); 103958951Seric if (sp != NULL) 104066761Seric RealHostAddr.sin.sin_port = sp->s_port; 104158308Seric else 104266761Seric RealHostAddr.sin.sin_port = htons(113); 104358951Seric 104458951Seric s = -1; 104558951Seric if (setjmp(CtxAuthTimeout) != 0) 104658951Seric { 104758951Seric if (s >= 0) 104858951Seric (void) close(s); 104958951Seric goto noident; 105058951Seric } 105158951Seric 105258951Seric /* put a timeout around the whole thing */ 105364255Seric ev = setevent(TimeOuts.to_ident, authtimeout, 0); 105458951Seric 105564747Seric /* connect to foreign IDENT server using same address as SMTP socket */ 105658951Seric s = socket(AF_INET, SOCK_STREAM, 0); 105758951Seric if (s < 0) 105858951Seric { 105958951Seric clrevent(ev); 106058951Seric goto noident; 106158951Seric } 106264747Seric if (bind(s, &la.sa, sizeof la.sin) < 0 || 106366761Seric connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) 106458951Seric { 106566011Seric goto closeident; 106658951Seric } 106758951Seric 106858957Seric if (tTd(9, 10)) 106968457Seric printf("getauthinfo: sent %s", ibuf); 107058951Seric 107158951Seric /* send query */ 107268457Seric if (write(s, ibuf, strlen(ibuf)) < 0) 107358951Seric goto closeident; 107458951Seric 107558951Seric /* get result */ 107668457Seric p = &ibuf[0]; 1077*68462Seric nleft = sizeof(ibuf); 107868444Seric while ((i = read(s, p, nleft)) > 0) 107968444Seric { 108068444Seric p += i; 108168444Seric nleft -= i; 108268444Seric } 108358951Seric (void) close(s); 108458951Seric clrevent(ev); 108568457Seric if (i < 0 || p == &ibuf[0]) 108658951Seric goto noident; 108758951Seric 108868444Seric if (*--p == '\n' && *--p == '\r') 108968444Seric p--; 109068444Seric *++p = '\0'; 109168444Seric 109258957Seric if (tTd(9, 3)) 109368457Seric printf("getauthinfo: got %s\n", ibuf); 109458951Seric 109558951Seric /* parse result */ 109668457Seric p = strchr(ibuf, ':'); 109758951Seric if (p == NULL) 109858951Seric { 109958951Seric /* malformed response */ 110058951Seric goto noident; 110158951Seric } 110258951Seric while (isascii(*++p) && isspace(*p)) 110358951Seric continue; 110458951Seric if (strncasecmp(p, "userid", 6) != 0) 110558951Seric { 110658951Seric /* presumably an error string */ 110758951Seric goto noident; 110858951Seric } 110958951Seric p += 6; 111058951Seric while (isascii(*p) && isspace(*p)) 111158951Seric p++; 111258951Seric if (*p++ != ':') 111358951Seric { 111458951Seric /* either useridxx or malformed response */ 111558951Seric goto noident; 111658951Seric } 111758951Seric 111858951Seric /* p now points to the OSTYPE field */ 111958951Seric p = strchr(p, ':'); 112058951Seric if (p == NULL) 112158951Seric { 112258951Seric /* malformed response */ 112358951Seric goto noident; 112458951Seric } 112558951Seric 112658957Seric /* 1413 says don't do this -- but it's broken otherwise */ 112758957Seric while (isascii(*++p) && isspace(*p)) 112858957Seric continue; 112958957Seric 113067935Seric /* p now points to the authenticated name -- copy carefully */ 113168457Seric cleanstrcpy(hbuf, p, MAXNAME); 113267935Seric hbuf[i++] = '@'; 113367935Seric strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName); 113458957Seric goto finish; 113558957Seric 113666011Seric closeident: 113766011Seric (void) close(s); 113866011Seric clrevent(ev); 113966011Seric 114058957Seric noident: 114166003Seric if (RealHostName == NULL) 114266003Seric { 114366003Seric if (tTd(9, 1)) 114466003Seric printf("getauthinfo: NULL\n"); 114566003Seric return NULL; 114666003Seric } 114758957Seric (void) strcpy(hbuf, RealHostName); 114858957Seric 114958957Seric finish: 115066003Seric if (RealHostName != NULL && RealHostName[0] != '[') 115158951Seric { 115258951Seric p = &hbuf[strlen(hbuf)]; 115358951Seric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 115458951Seric } 115558957Seric if (tTd(9, 1)) 115658951Seric printf("getauthinfo: %s\n", hbuf); 115758308Seric return hbuf; 115858308Seric } 115958308Seric /* 116060089Seric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 116153751Seric ** 116253751Seric ** Parameters: 116356823Seric ** map -- a pointer to this map (unused). 116460089Seric ** name -- the (presumably unqualified) hostname. 116560257Seric ** av -- unused -- for compatibility with other mapping 116655019Seric ** functions. 116759084Seric ** statp -- an exit status (out parameter) -- set to 116859084Seric ** EX_TEMPFAIL if the name server is unavailable. 116953751Seric ** 117053751Seric ** Returns: 117153751Seric ** The mapping, if found. 117253751Seric ** NULL if no mapping found. 117353751Seric ** 117453751Seric ** Side Effects: 117553751Seric ** Looks up the host specified in hbuf. If it is not 117653751Seric ** the canonical name for that host, return the canonical 117753751Seric ** name. 117853751Seric */ 117951315Seric 118053751Seric char * 118160257Seric host_map_lookup(map, name, av, statp) 118256823Seric MAP *map; 118360089Seric char *name; 118460257Seric char **av; 118559084Seric int *statp; 118616911Seric { 118716911Seric register struct hostent *hp; 1188*68462Seric struct in_addr in_addr; 118956823Seric char *cp; 119059671Seric register STAB *s; 119160257Seric char hbuf[MAXNAME]; 119259671Seric extern struct hostent *gethostbyaddr(); 119366334Seric #if NAMED_BIND 119459671Seric extern int h_errno; 119566029Seric #endif 119616911Seric 119725574Smiriam /* 119859671Seric ** See if we have already looked up this name. If so, just 119959671Seric ** return it. 120059671Seric */ 120153751Seric 120260089Seric s = stab(name, ST_NAMECANON, ST_ENTER); 120359671Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 120459671Seric { 120559986Seric if (tTd(9, 1)) 120660089Seric printf("host_map_lookup(%s) => CACHE %s\n", 120760089Seric name, s->s_namecanon.nc_cname); 120859671Seric errno = s->s_namecanon.nc_errno; 120966334Seric #if NAMED_BIND 121059671Seric h_errno = s->s_namecanon.nc_herrno; 121166029Seric #endif 121259671Seric *statp = s->s_namecanon.nc_stat; 121364797Seric if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL) 121465199Seric { 121565199Seric sprintf(hbuf, "%s: Name server timeout", 121665199Seric shortenstring(name, 33)); 121765199Seric CurEnv->e_message = newstr(hbuf); 121865199Seric } 121959671Seric return s->s_namecanon.nc_cname; 122059671Seric } 122159671Seric 122259671Seric /* 122359671Seric ** If first character is a bracket, then it is an address 122459671Seric ** lookup. Address is copied into a temporary buffer to 122560089Seric ** strip the brackets and to preserve name if address is 122659671Seric ** unknown. 122759671Seric */ 122859671Seric 122960089Seric if (*name != '[') 123053751Seric { 123155019Seric extern bool getcanonname(); 123255019Seric 123358798Seric if (tTd(9, 1)) 123460089Seric printf("host_map_lookup(%s) => ", name); 123559671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 123660089Seric (void) strcpy(hbuf, name); 123763842Seric if (getcanonname(hbuf, sizeof hbuf - 1, TRUE)) 123858796Seric { 123958796Seric if (tTd(9, 1)) 124058796Seric printf("%s\n", hbuf); 124160257Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av); 124260257Seric s->s_namecanon.nc_cname = newstr(cp); 124360257Seric return cp; 124458796Seric } 124553751Seric else 124658796Seric { 124759084Seric register struct hostent *hp; 124859084Seric 124966029Seric s->s_namecanon.nc_errno = errno; 125066334Seric #if NAMED_BIND 125166029Seric s->s_namecanon.nc_herrno = h_errno; 125258796Seric if (tTd(9, 1)) 125359084Seric printf("FAIL (%d)\n", h_errno); 125459084Seric switch (h_errno) 125559084Seric { 125659084Seric case TRY_AGAIN: 125759596Seric if (UseNameServer) 125859734Seric { 125965202Seric sprintf(hbuf, "%s: Name server timeout", 126065199Seric shortenstring(name, 33)); 126165202Seric message("%s", hbuf); 126259734Seric if (CurEnv->e_message == NULL) 126365202Seric CurEnv->e_message = newstr(hbuf); 126459734Seric } 126559084Seric *statp = EX_TEMPFAIL; 126659084Seric break; 126759084Seric 126859084Seric case HOST_NOT_FOUND: 126959084Seric *statp = EX_NOHOST; 127059084Seric break; 127159084Seric 127259084Seric case NO_RECOVERY: 127359084Seric *statp = EX_SOFTWARE; 127459084Seric break; 127559084Seric 127659084Seric default: 127759084Seric *statp = EX_UNAVAILABLE; 127859084Seric break; 127959084Seric } 128066029Seric #else 128166029Seric if (tTd(9, 1)) 128266029Seric printf("FAIL\n"); 128366029Seric *statp = EX_NOHOST; 128466029Seric #endif 128559671Seric s->s_namecanon.nc_stat = *statp; 1286*68462Seric if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) || 1287*68462Seric UseNameServer) 128859084Seric return NULL; 128959084Seric 129059084Seric /* 129159084Seric ** Try to look it up in /etc/hosts 129259084Seric */ 129359084Seric 129460089Seric hp = gethostbyname(name); 129559084Seric if (hp == NULL) 129659084Seric { 129759084Seric /* no dice there either */ 129859671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 129959084Seric return NULL; 130059084Seric } 130159084Seric 130259671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 130360257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 130460257Seric s->s_namecanon.nc_cname = newstr(cp); 130560257Seric return cp; 130658796Seric } 130753751Seric } 130860089Seric if ((cp = strchr(name, ']')) == NULL) 130953751Seric return (NULL); 131040994Sbostic *cp = '\0'; 1311*68462Seric in_addr.s_addr = inet_addr(&name[1]); 131258110Seric 131358110Seric /* nope -- ask the name server */ 1314*68462Seric hp = gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET); 131559671Seric s->s_namecanon.nc_errno = errno; 131666334Seric #if NAMED_BIND 131759671Seric s->s_namecanon.nc_herrno = h_errno; 131866029Seric #endif 131959671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 132033932Sbostic if (hp == NULL) 132159671Seric { 132259671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 132353751Seric return (NULL); 132459671Seric } 132553751Seric 132658110Seric /* found a match -- copy out */ 132760257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 132859671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 132960257Seric s->s_namecanon.nc_cname = newstr(cp); 133060257Seric return cp; 133133932Sbostic } 133258755Seric /* 133358755Seric ** ANYNET_NTOA -- convert a network address to printable form. 133458755Seric ** 133558755Seric ** Parameters: 133658755Seric ** sap -- a pointer to a sockaddr structure. 133758755Seric ** 133858755Seric ** Returns: 133958755Seric ** A printable version of that sockaddr. 134058755Seric */ 134116911Seric 134258755Seric char * 134358755Seric anynet_ntoa(sap) 134458755Seric register SOCKADDR *sap; 134558755Seric { 134658755Seric register char *bp; 134758755Seric register char *ap; 134858755Seric int l; 134964734Seric static char buf[100]; 135058755Seric 135158798Seric /* check for null/zero family */ 135258798Seric if (sap == NULL) 135358798Seric return "NULLADDR"; 135458798Seric if (sap->sa.sa_family == 0) 135558798Seric return "0"; 135658798Seric 135764734Seric switch (sap->sa.sa_family) 135864734Seric { 135964821Seric #ifdef NETUNIX 136064734Seric case AF_UNIX: 136164758Seric if (sap->sunix.sun_path[0] != '\0') 136264758Seric sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); 136364734Seric else 136464734Seric sprintf(buf, "[UNIX: localhost]"); 136564734Seric return buf; 136664734Seric #endif 136764734Seric 136858778Seric #ifdef NETINET 136964734Seric case AF_INET: 137058755Seric return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 137158778Seric #endif 137258755Seric 137364734Seric default: 137464734Seric /* this case is only to ensure syntactic correctness */ 137564734Seric break; 137664734Seric } 137764734Seric 137858755Seric /* unknown family -- just dump bytes */ 137958778Seric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 138058755Seric bp = &buf[strlen(buf)]; 138158778Seric ap = sap->sa.sa_data; 138258778Seric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 138358755Seric { 138458755Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 138558755Seric bp += 3; 138658755Seric } 138758755Seric *--bp = '\0'; 138858755Seric return buf; 138958755Seric } 139058951Seric /* 139158951Seric ** HOSTNAMEBYANYADDR -- return name of host based on address 139258951Seric ** 139358951Seric ** Parameters: 139458951Seric ** sap -- SOCKADDR pointer 139558951Seric ** 139658951Seric ** Returns: 139758951Seric ** text representation of host name. 139858951Seric ** 139958951Seric ** Side Effects: 140058951Seric ** none. 140158951Seric */ 140258755Seric 140358951Seric char * 140458951Seric hostnamebyanyaddr(sap) 140558951Seric register SOCKADDR *sap; 140658951Seric { 140758951Seric register struct hostent *hp; 140864734Seric int saveretry; 140958951Seric 141066334Seric #if NAMED_BIND 141159042Seric /* shorten name server timeout to avoid higher level timeouts */ 141259042Seric saveretry = _res.retry; 141359042Seric _res.retry = 3; 141459042Seric #endif /* NAMED_BIND */ 141559042Seric 141658951Seric switch (sap->sa.sa_family) 141758951Seric { 141858951Seric #ifdef NETINET 141958951Seric case AF_INET: 142058951Seric hp = gethostbyaddr((char *) &sap->sin.sin_addr, 1421*68462Seric INADDRSZ, 142258951Seric AF_INET); 142358951Seric break; 142458951Seric #endif 142558951Seric 142658951Seric #ifdef NETISO 142758951Seric case AF_ISO: 142858951Seric hp = gethostbyaddr((char *) &sap->siso.siso_addr, 142958951Seric sizeof sap->siso.siso_addr, 143058951Seric AF_ISO); 143158951Seric break; 143258951Seric #endif 143358951Seric 143464734Seric case AF_UNIX: 143564734Seric hp = NULL; 143664734Seric break; 143764734Seric 143858951Seric default: 143958951Seric hp = gethostbyaddr(sap->sa.sa_data, 144058951Seric sizeof sap->sa.sa_data, 144158951Seric sap->sa.sa_family); 144258951Seric break; 144358951Seric } 144458951Seric 144566334Seric #if NAMED_BIND 144659042Seric _res.retry = saveretry; 144759042Seric #endif /* NAMED_BIND */ 144859042Seric 144958951Seric if (hp != NULL) 145058951Seric return hp->h_name; 145158951Seric else 145258951Seric { 145358951Seric /* produce a dotted quad */ 145458951Seric static char buf[512]; 145558951Seric 145658951Seric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 145758951Seric return buf; 145858951Seric } 145958951Seric } 146058951Seric 146156795Seric # else /* DAEMON */ 146216911Seric /* code for systems without sophisticated networking */ 146310758Seric 146410758Seric /* 146510758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 146611297Seric ** 146711297Seric ** Can't convert to upper case here because might be a UUCP name. 146812313Seric ** 146912313Seric ** Mark, you can change this to be anything you want...... 147010758Seric */ 147110758Seric 147210758Seric char ** 147312313Seric myhostname(hostbuf, size) 147410758Seric char hostbuf[]; 147512313Seric int size; 147610758Seric { 147710758Seric register FILE *f; 147810758Seric 147910758Seric hostbuf[0] = '\0'; 148010758Seric f = fopen("/usr/include/whoami", "r"); 148110758Seric if (f != NULL) 148210758Seric { 148312313Seric (void) fgets(hostbuf, size, f); 148410758Seric fixcrlf(hostbuf, TRUE); 148510758Seric (void) fclose(f); 148610758Seric } 148710758Seric return (NULL); 148810758Seric } 148916911Seric /* 149058951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 149158308Seric ** 149258308Seric ** Parameters: 149358308Seric ** fd -- the descriptor 149458308Seric ** 149558308Seric ** Returns: 149658308Seric ** The host name associated with this descriptor, if it can 149758308Seric ** be determined. 149858308Seric ** NULL otherwise. 149958308Seric ** 150058308Seric ** Side Effects: 150158308Seric ** none 150258308Seric */ 150358308Seric 150458308Seric char * 150558951Seric getauthinfo(fd) 150658308Seric int fd; 150758308Seric { 150858308Seric return NULL; 150958308Seric } 151058308Seric /* 151116911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 151216911Seric ** 151316911Seric ** Parameters: 151456823Seric ** map -- a pointer to the database map. 151560089Seric ** name -- a buffer containing a hostname. 151653751Seric ** avp -- a pointer to a (cf file defined) argument vector. 151759084Seric ** statp -- an exit status (out parameter). 151816911Seric ** 151916911Seric ** Returns: 152053751Seric ** mapped host name 152151315Seric ** FALSE otherwise. 152216911Seric ** 152316911Seric ** Side Effects: 152460089Seric ** Looks up the host specified in name. If it is not 152516911Seric ** the canonical name for that host, replace it with 152616911Seric ** the canonical name. If the name is unknown, or it 152716911Seric ** is already the canonical name, leave it unchanged. 152816911Seric */ 152910758Seric 153016911Seric /*ARGSUSED*/ 153153751Seric char * 153260089Seric host_map_lookup(map, name, avp, statp) 153356823Seric MAP *map; 153460089Seric char *name; 153553751Seric char **avp; 153659084Seric char *statp; 153716911Seric { 153859084Seric register struct hostent *hp; 153959084Seric 154060089Seric hp = gethostbyname(name); 154159084Seric if (hp != NULL) 154259084Seric return hp->h_name; 154359084Seric *statp = EX_NOHOST; 154453751Seric return NULL; 154516911Seric } 154616911Seric 154756795Seric #endif /* DAEMON */ 1548