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*66862Seric static char sccsid[] = "@(#)daemon.c 8.48 (Berkeley) 04/18/94 (with daemon mode)"; 1533780Sbostic #else 16*66862Seric static char sccsid[] = "@(#)daemon.c 8.48 (Berkeley) 04/18/94 (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 <arpa/nameser.h> 2759042Seric # include <resolv.h> 2859042Seric #endif 2959042Seric 304535Seric /* 314535Seric ** DAEMON.C -- routines to use when running as a daemon. 327556Seric ** 337556Seric ** This entire file is highly dependent on the 4.2 BSD 347556Seric ** interprocess communication primitives. No attempt has 357556Seric ** been made to make this file portable to Version 7, 367556Seric ** Version 6, MPX files, etc. If you should try such a 377556Seric ** thing yourself, I recommend chucking the entire file 387556Seric ** and starting from scratch. Basic semantics are: 397556Seric ** 407556Seric ** getrequests() 417556Seric ** Opens a port and initiates a connection. 427556Seric ** Returns in a child. Must set InChannel and 437556Seric ** OutChannel appropriately. 4410206Seric ** clrdaemon() 4510206Seric ** Close any open files associated with getting 4610206Seric ** the connection; this is used when running the queue, 4710206Seric ** etc., to avoid having extra file descriptors during 4810206Seric ** the queue run and to avoid confusing the network 4910206Seric ** code (if it cares). 5052106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 517556Seric ** Make a connection to the named host on the given 527556Seric ** port. Set *outfile and *infile to the files 537556Seric ** appropriate for communication. Returns zero on 547556Seric ** success, else an exit status describing the 557556Seric ** error. 5660089Seric ** host_map_lookup(map, hbuf, avp, pstat) 5756823Seric ** Convert the entry in hbuf into a canonical form. 584535Seric */ 594535Seric /* 604535Seric ** GETREQUESTS -- open mail IPC port and get requests. 614535Seric ** 624535Seric ** Parameters: 634535Seric ** none. 644535Seric ** 654535Seric ** Returns: 664535Seric ** none. 674535Seric ** 684535Seric ** Side Effects: 694535Seric ** Waits until some interesting activity occurs. When 704535Seric ** it does, a child is created to process it, and the 714535Seric ** parent waits for completion. Return from this 729886Seric ** routine is always in the child. The file pointers 739886Seric ** "InChannel" and "OutChannel" should be set to point 749886Seric ** to the communication channel. 754535Seric */ 764535Seric 7758849Seric int DaemonSocket = -1; /* fd describing socket */ 7858849Seric SOCKADDR DaemonAddr; /* socket for incoming */ 7959783Seric int ListenQueueSize = 10; /* size of listen queue */ 8064381Seric int TcpRcvBufferSize = 0; /* size of TCP receive buffer */ 8164381Seric int TcpSndBufferSize = 0; /* size of TCP send buffer */ 8216144Seric 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(); 16111147Seric 16214875Seric /* see if we are rejecting connections */ 16353751Seric CurrentLA = getla(); 16453751Seric if (refuseconnections()) 16536584Sbostic { 16666845Seric if (DaemonSocket >= 0) 16753751Seric { 16866845Seric /* close socket so peer will fail quickly */ 16966845Seric (void) close(DaemonSocket); 17066845Seric DaemonSocket = -1; 17153751Seric } 17266845Seric refusingconnections = TRUE; 17357385Seric setproctitle("rejecting connections: load average: %d", 17457385Seric CurrentLA); 17566845Seric sleep(15); 17653751Seric continue; 17736584Sbostic } 17814875Seric 17953751Seric if (refusingconnections) 18053751Seric { 18153751Seric /* start listening again */ 18266854Seric (void) opendaemonsocket(FALSE); 18353751Seric setproctitle("accepting connections"); 18453751Seric refusingconnections = FALSE; 18553751Seric } 18653751Seric 18766793Seric #ifdef XDEBUG 18866793Seric /* check for disaster */ 18966793Seric { 19066793Seric register STAB *s; 19166812Seric char jbuf[MAXHOSTNAMELEN]; 19266793Seric 19366812Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 19466812Seric if ((s = stab(jbuf, ST_CLASS, ST_FIND)) == NULL || 19566793Seric !bitnset('w', s->s_class)) 19666793Seric { 19766793Seric dumpstate("daemon lost $j"); 19866793Seric syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog"); 19966793Seric abort(); 20066793Seric } 20166812Seric else if (j_has_dot && strchr(jbuf, '.') == NULL) 20266793Seric { 20366793Seric dumpstate("daemon $j lost dot"); 20466793Seric syslog(LOG_ALERT, "daemon process $j lost dot; see syslog"); 20566793Seric abort(); 20666793Seric } 20766793Seric } 20866793Seric #endif 20966793Seric 2109610Seric /* wait for a connection */ 2119610Seric do 2129610Seric { 2139610Seric errno = 0; 21464828Seric lotherend = socksize; 21546928Sbostic t = accept(DaemonSocket, 21646928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2179610Seric } while (t < 0 && errno == EINTR); 2189610Seric if (t < 0) 2195978Seric { 2209610Seric syserr("getrequests: accept"); 2219610Seric sleep(5); 2229610Seric continue; 2235978Seric } 2244631Seric 2255978Seric /* 2265978Seric ** Create a subprocess to process the mail. 2275978Seric */ 2285978Seric 2297677Seric if (tTd(15, 2)) 2309610Seric printf("getrequests: forking (fd = %d)\n", t); 2315978Seric 2324636Seric pid = fork(); 2334636Seric if (pid < 0) 2344631Seric { 2354636Seric syserr("daemon: cannot fork"); 2364636Seric sleep(10); 2379610Seric (void) close(t); 2384636Seric continue; 2394631Seric } 2404631Seric 2414636Seric if (pid == 0) 2424631Seric { 24364086Seric char *p; 24458951Seric extern char *hostnamebyanyaddr(); 24511147Seric 2464636Seric /* 2474636Seric ** CHILD -- return to caller. 24811147Seric ** Collect verified idea of sending host. 2494636Seric ** Verify calling user id if possible here. 2504636Seric */ 2514631Seric 25264035Seric (void) setsignal(SIGCHLD, SIG_DFL); 25366017Seric DisConnected = FALSE; 25424950Seric 25566032Seric setproctitle("startup with %s", 25666032Seric anynet_ntoa(&RealHostAddr)); 25766032Seric 25811147Seric /* determine host name */ 25964086Seric p = hostnamebyanyaddr(&RealHostAddr); 26064086Seric RealHostName = newstr(p); 26166032Seric setproctitle("startup with %s", p); 26258778Seric 26355173Seric #ifdef LOG 26463842Seric if (LogLevel > 11) 26555173Seric { 26655173Seric /* log connection information */ 26755173Seric syslog(LOG_INFO, "connect from %s (%s)", 26858951Seric RealHostName, anynet_ntoa(&RealHostAddr)); 26955173Seric } 27055173Seric #endif 27155173Seric 27259254Seric (void) close(DaemonSocket); 27364724Seric if ((InChannel = fdopen(t, "r")) == NULL || 27464724Seric (t = dup(t)) < 0 || 27564724Seric (OutChannel = fdopen(t, "w")) == NULL) 27664724Seric { 27764724Seric syserr("cannot open SMTP server channel, fd=%d", t); 27864724Seric exit(0); 27964724Seric } 28059254Seric 28116884Seric /* should we check for illegal connection here? XXX */ 28259156Seric #ifdef XLA 28359156Seric if (!xla_host_ok(RealHostName)) 28459156Seric { 28559254Seric message("421 Too many SMTP sessions for this host"); 28659156Seric exit(0); 28759156Seric } 28859156Seric #endif 28916884Seric 2907677Seric if (tTd(15, 2)) 2915978Seric printf("getreq: returning\n"); 2924636Seric return; 2934631Seric } 2944631Seric 2957117Seric /* close the port so that others will hang (for a while) */ 2969610Seric (void) close(t); 2974631Seric } 2989886Seric /*NOTREACHED*/ 2994631Seric } 3005978Seric /* 30166845Seric ** OPENDAEMONSOCKET -- open the SMTP socket 30266845Seric ** 30366845Seric ** Deals with setting all appropriate options. DaemonAddr must 30466845Seric ** be set up in advance. 30566845Seric ** 30666845Seric ** Parameters: 30766854Seric ** firsttime -- set if this is the initial open. 30866845Seric ** 30966845Seric ** Returns: 31066845Seric ** Size in bytes of the daemon socket addr. 31166845Seric ** 31266845Seric ** Side Effects: 31366845Seric ** Leaves DaemonSocket set to the open socket. 31466845Seric ** Exits if the socket cannot be created. 31566845Seric */ 31666845Seric 31766861Seric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */ 31866861Seric 31966845Seric int 32066854Seric opendaemonsocket(firsttime) 32166854Seric bool firsttime; 32266845Seric { 32366845Seric int on = 1; 32466845Seric int socksize; 32566861Seric int ntries = 0; 32666861Seric int saveerrno; 32766845Seric 32866845Seric if (tTd(15, 2)) 32966845Seric printf("opendaemonsocket()\n"); 33066845Seric 33166861Seric do 33266845Seric { 333*66862Seric if (ntries > 0) 334*66862Seric sleep(5); 33566861Seric if (firsttime || DaemonSocket < 0) 33666854Seric { 33766861Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 33866861Seric if (DaemonSocket < 0) 33966861Seric { 34066861Seric /* probably another daemon already */ 34166861Seric saveerrno = errno; 34266861Seric syserr("opendaemonsocket: can't create server SMTP socket"); 34366861Seric severe: 34466845Seric # ifdef LOG 34566861Seric if (LogLevel > 0) 34666861Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 34766845Seric # endif /* LOG */ 34866861Seric DaemonSocket = -1; 34966861Seric continue; 35066861Seric } 35166845Seric 35266861Seric /* turn on network debugging? */ 35366861Seric if (tTd(15, 101)) 35466861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 35566861Seric SO_DEBUG, (char *)&on, 35666861Seric sizeof on); 35766845Seric 35866861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 35966861Seric SO_REUSEADDR, (char *)&on, sizeof on); 36066861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36166861Seric SO_KEEPALIVE, (char *)&on, sizeof on); 36266845Seric 36366845Seric #ifdef SO_RCVBUF 36466861Seric if (TcpRcvBufferSize > 0) 36566861Seric { 36666861Seric if (setsockopt(DaemonSocket, SOL_SOCKET, 36766861Seric SO_RCVBUF, 36866861Seric (char *) &TcpRcvBufferSize, 36966861Seric sizeof(TcpRcvBufferSize)) < 0) 37066861Seric syserr("getrequests: setsockopt(SO_RCVBUF)"); 37166861Seric } 37266845Seric #endif 37366845Seric 37466861Seric switch (DaemonAddr.sa.sa_family) 37566861Seric { 37666845Seric # ifdef NETINET 37766861Seric case AF_INET: 37866861Seric socksize = sizeof DaemonAddr.sin; 37966861Seric break; 38066845Seric # endif 38166845Seric 38266845Seric # ifdef NETISO 38366861Seric case AF_ISO: 38466861Seric socksize = sizeof DaemonAddr.siso; 38566861Seric break; 38666845Seric # endif 38766845Seric 38866861Seric default: 38966861Seric socksize = sizeof DaemonAddr; 39066861Seric break; 39166861Seric } 39266861Seric 39366861Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0) 39466861Seric { 39566861Seric saveerrno = errno; 39666861Seric syserr("getrequests: cannot bind"); 39766861Seric (void) close(DaemonSocket); 39866861Seric goto severe; 39966861Seric } 40066854Seric } 40166861Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0) 40266854Seric { 40366861Seric saveerrno = errno; 40466861Seric syserr("getrequests: cannot listen"); 40566854Seric (void) close(DaemonSocket); 40666854Seric goto severe; 40766854Seric } 40866861Seric return socksize; 40966861Seric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno)); 41066861Seric finis(); 41166845Seric } 41266845Seric /* 41310206Seric ** CLRDAEMON -- reset the daemon connection 41410206Seric ** 41510206Seric ** Parameters: 41610206Seric ** none. 41710206Seric ** 41810206Seric ** Returns: 41910206Seric ** none. 42010206Seric ** 42110206Seric ** Side Effects: 42210206Seric ** releases any resources used by the passive daemon. 42310206Seric */ 42410206Seric 42510206Seric clrdaemon() 42610206Seric { 42710206Seric if (DaemonSocket >= 0) 42810206Seric (void) close(DaemonSocket); 42910206Seric DaemonSocket = -1; 43010206Seric } 43110206Seric /* 43258849Seric ** SETDAEMONOPTIONS -- set options for running the daemon 43358849Seric ** 43458849Seric ** Parameters: 43558849Seric ** p -- the options line. 43658849Seric ** 43758849Seric ** Returns: 43858849Seric ** none. 43958849Seric */ 44058849Seric 44158849Seric setdaemonoptions(p) 44258849Seric register char *p; 44358849Seric { 44458873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 44558873Seric DaemonAddr.sa.sa_family = AF_INET; 44658873Seric 44758849Seric while (p != NULL) 44858849Seric { 44958849Seric register char *f; 45058849Seric register char *v; 45158849Seric 45258849Seric while (isascii(*p) && isspace(*p)) 45358849Seric p++; 45458849Seric if (*p == '\0') 45558849Seric break; 45658849Seric f = p; 45758849Seric p = strchr(p, ','); 45858849Seric if (p != NULL) 45958849Seric *p++ = '\0'; 46058849Seric v = strchr(f, '='); 46158849Seric if (v == NULL) 46258849Seric continue; 46358849Seric while (isascii(*++v) && isspace(*v)) 46458849Seric continue; 46558849Seric 46658849Seric switch (*f) 46758849Seric { 46858873Seric case 'F': /* address family */ 46958849Seric if (isascii(*v) && isdigit(*v)) 47058873Seric DaemonAddr.sa.sa_family = atoi(v); 47158873Seric #ifdef NETINET 47258873Seric else if (strcasecmp(v, "inet") == 0) 47358873Seric DaemonAddr.sa.sa_family = AF_INET; 47458873Seric #endif 47558873Seric #ifdef NETISO 47658873Seric else if (strcasecmp(v, "iso") == 0) 47758873Seric DaemonAddr.sa.sa_family = AF_ISO; 47858873Seric #endif 47958873Seric #ifdef NETNS 48058873Seric else if (strcasecmp(v, "ns") == 0) 48158873Seric DaemonAddr.sa.sa_family = AF_NS; 48258873Seric #endif 48358873Seric #ifdef NETX25 48458873Seric else if (strcasecmp(v, "x.25") == 0) 48558873Seric DaemonAddr.sa.sa_family = AF_CCITT; 48658873Seric #endif 48758849Seric else 48858873Seric syserr("554 Unknown address family %s in Family=option", v); 48958873Seric break; 49058873Seric 49158873Seric case 'A': /* address */ 49258873Seric switch (DaemonAddr.sa.sa_family) 49358849Seric { 49458873Seric #ifdef NETINET 49558873Seric case AF_INET: 49658873Seric if (isascii(*v) && isdigit(*v)) 49758873Seric DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 49858873Seric else 49958873Seric { 50058873Seric register struct netent *np; 50158849Seric 50258873Seric np = getnetbyname(v); 50358873Seric if (np == NULL) 50458873Seric syserr("554 network \"%s\" unknown", v); 50558873Seric else 50658873Seric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 50758873Seric } 50858873Seric break; 50958873Seric #endif 51058873Seric 51158873Seric default: 51258873Seric syserr("554 Address= option unsupported for family %d", 51358873Seric DaemonAddr.sa.sa_family); 51458873Seric break; 51558849Seric } 51658849Seric break; 51758849Seric 51858873Seric case 'P': /* port */ 51958873Seric switch (DaemonAddr.sa.sa_family) 52058849Seric { 52158873Seric short port; 52258849Seric 52358873Seric #ifdef NETINET 52458873Seric case AF_INET: 52558873Seric if (isascii(*v) && isdigit(*v)) 52664366Seric DaemonAddr.sin.sin_port = htons(atoi(v)); 52758849Seric else 52858873Seric { 52958873Seric register struct servent *sp; 53058873Seric 53158873Seric sp = getservbyname(v, "tcp"); 53258873Seric if (sp == NULL) 53358909Seric syserr("554 service \"%s\" unknown", v); 53458873Seric else 53558873Seric DaemonAddr.sin.sin_port = sp->s_port; 53658873Seric } 53758873Seric break; 53858873Seric #endif 53958873Seric 54058873Seric #ifdef NETISO 54158873Seric case AF_ISO: 54258873Seric /* assume two byte transport selector */ 54358873Seric if (isascii(*v) && isdigit(*v)) 54464366Seric port = htons(atoi(v)); 54558873Seric else 54658873Seric { 54758873Seric register struct servent *sp; 54858873Seric 54958873Seric sp = getservbyname(v, "tcp"); 55058873Seric if (sp == NULL) 55158909Seric syserr("554 service \"%s\" unknown", v); 55258873Seric else 55358873Seric port = sp->s_port; 55458873Seric } 55558873Seric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 55658873Seric break; 55758873Seric #endif 55858873Seric 55958873Seric default: 56058873Seric syserr("554 Port= option unsupported for family %d", 56158873Seric DaemonAddr.sa.sa_family); 56258873Seric break; 56358849Seric } 56458849Seric break; 56559783Seric 56659783Seric case 'L': /* listen queue size */ 56759783Seric ListenQueueSize = atoi(v); 56859783Seric break; 56964381Seric 57064381Seric case 'S': /* send buffer size */ 57164381Seric TcpSndBufferSize = atoi(v); 57264381Seric break; 57364381Seric 57464381Seric case 'R': /* receive buffer size */ 57564381Seric TcpRcvBufferSize = atoi(v); 57664381Seric break; 57758849Seric } 57858849Seric } 57958849Seric } 58058849Seric /* 5816039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 5826039Seric ** 5836039Seric ** Parameters: 5846039Seric ** host -- the name of the host. 5856633Seric ** port -- the port number to connect to. 58653739Seric ** mci -- a pointer to the mail connection information 58753739Seric ** structure to be filled in. 58852106Seric ** usesecureport -- if set, use a low numbered (reserved) 58952106Seric ** port to provide some rudimentary authentication. 5906039Seric ** 5916039Seric ** Returns: 5926039Seric ** An exit code telling whether the connection could be 5936039Seric ** made and if not why not. 5946039Seric ** 5956039Seric ** Side Effects: 5966039Seric ** none. 5976039Seric */ 5985978Seric 59958755Seric SOCKADDR CurHostAddr; /* address of current host */ 60058305Seric 60154967Seric int 60253739Seric makeconnection(host, port, mci, usesecureport) 6036039Seric char *host; 6047286Seric u_short port; 60554967Seric register MCI *mci; 60652106Seric bool usesecureport; 6076039Seric { 60829430Sbloom register int i, s; 60929430Sbloom register struct hostent *hp = (struct hostent *)NULL; 61058755Seric SOCKADDR addr; 61152106Seric int sav_errno; 61258755Seric int addrlen; 61366334Seric #if NAMED_BIND 61435651Seric extern int h_errno; 61535651Seric #endif 6166039Seric 6176039Seric /* 6186039Seric ** Set up the address for the mailer. 6199308Seric ** Accept "[a.b.c.d]" syntax for host name. 6206039Seric */ 6216039Seric 62266334Seric #if NAMED_BIND 62325475Smiriam h_errno = 0; 62435651Seric #endif 62525475Smiriam errno = 0; 62658864Seric bzero(&CurHostAddr, sizeof CurHostAddr); 62764334Seric SmtpPhase = mci->mci_phase = "initial connection"; 62858906Seric CurHostName = host; 62925475Smiriam 6309308Seric if (host[0] == '[') 6319308Seric { 63211147Seric long hid; 63356795Seric register char *p = strchr(host, ']'); 6349308Seric 63511147Seric if (p != NULL) 6369308Seric { 63711147Seric *p = '\0'; 63859884Seric #ifdef NETINET 63911147Seric hid = inet_addr(&host[1]); 64058360Seric if (hid == -1) 64159884Seric #endif 64258360Seric { 64358360Seric /* try it as a host name (avoid MX lookup) */ 64458360Seric hp = gethostbyname(&host[1]); 64566349Seric if (hp == NULL && p[-1] == '.') 64666349Seric { 64766349Seric p[-1] = '\0'; 64866349Seric hp = gethostbyname(&host[1]); 64966349Seric p[-1] = '.'; 65066349Seric } 65158360Seric *p = ']'; 65258360Seric goto gothostent; 65358360Seric } 65411147Seric *p = ']'; 6559308Seric } 65658360Seric if (p == NULL) 6579308Seric { 65858151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 6599308Seric return (EX_NOHOST); 6609308Seric } 66159884Seric #ifdef NETINET 66259884Seric addr.sin.sin_family = AF_INET; /*XXX*/ 66358778Seric addr.sin.sin_addr.s_addr = hid; 66459884Seric #endif 6659308Seric } 6669610Seric else 6679610Seric { 66866349Seric register char *p = &host[strlen(host) - 1]; 66966349Seric 67029430Sbloom hp = gethostbyname(host); 67166349Seric if (hp == NULL && *p == '.') 67266349Seric { 67366349Seric *p = '\0'; 67466349Seric hp = gethostbyname(host); 67566349Seric *p = '.'; 67666349Seric } 67758360Seric gothostent: 67825475Smiriam if (hp == NULL) 67924945Seric { 68066334Seric #if NAMED_BIND 68125475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 68225475Smiriam return (EX_TEMPFAIL); 68325657Seric 68435651Seric /* if name server is specified, assume temp fail */ 68535651Seric if (errno == ECONNREFUSED && UseNameServer) 68635651Seric return (EX_TEMPFAIL); 68735651Seric #endif 68825475Smiriam return (EX_NOHOST); 68924945Seric } 69058778Seric addr.sa.sa_family = hp->h_addrtype; 69158778Seric switch (hp->h_addrtype) 69258778Seric { 69358778Seric #ifdef NETINET 69458778Seric case AF_INET: 69558755Seric bcopy(hp->h_addr, 69658778Seric &addr.sin.sin_addr, 69764943Seric sizeof addr.sin.sin_addr); 69858778Seric break; 69958778Seric #endif 70058778Seric 70158778Seric default: 70258755Seric bcopy(hp->h_addr, 70358778Seric addr.sa.sa_data, 70458755Seric hp->h_length); 70558778Seric break; 70658778Seric } 70729430Sbloom i = 1; 7089610Seric } 7099610Seric 7109610Seric /* 7119610Seric ** Determine the port number. 7129610Seric */ 7139610Seric 71410011Seric if (port != 0) 71558755Seric port = htons(port); 71610011Seric else 7179610Seric { 7189610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 7199610Seric 7209610Seric if (sp == NULL) 7219610Seric { 72258909Seric syserr("554 makeconnection: service \"smtp\" unknown"); 72365169Seric port = htons(25); 7249610Seric } 72565169Seric else 72665169Seric port = sp->s_port; 7279610Seric } 7286039Seric 72958778Seric switch (addr.sa.sa_family) 73058755Seric { 73159884Seric #ifdef NETINET 73258755Seric case AF_INET: 73358778Seric addr.sin.sin_port = port; 73458755Seric addrlen = sizeof (struct sockaddr_in); 73558755Seric break; 73659884Seric #endif 73758755Seric 73858755Seric #ifdef NETISO 73958755Seric case AF_ISO: 74058755Seric /* assume two byte transport selector */ 74158755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 74258755Seric addrlen = sizeof (struct sockaddr_iso); 74358755Seric break; 74458755Seric #endif 74558755Seric 74658755Seric default: 74758778Seric syserr("Can't connect to address family %d", addr.sa.sa_family); 74858755Seric return (EX_NOHOST); 74958755Seric } 75058755Seric 7516039Seric /* 7526039Seric ** Try to actually open the connection. 7536039Seric */ 7546039Seric 75559156Seric #ifdef XLA 75659156Seric /* if too many connections, don't bother trying */ 75759156Seric if (!xla_noqueue_ok(host)) 75859156Seric return EX_TEMPFAIL; 75959156Seric #endif 76059156Seric 76157736Seric for (;;) 76252106Seric { 76357736Seric if (tTd(16, 1)) 76458755Seric printf("makeconnection (%s [%s])\n", 76558755Seric host, anynet_ntoa(&addr)); 76652106Seric 76758588Seric /* save for logging */ 76858588Seric CurHostAddr = addr; 76958588Seric 77057736Seric if (usesecureport) 77157736Seric { 77257736Seric int rport = IPPORT_RESERVED - 1; 7736039Seric 77457736Seric s = rresvport(&rport); 77557736Seric } 77657736Seric else 77757736Seric { 77857736Seric s = socket(AF_INET, SOCK_STREAM, 0); 77957736Seric } 78057736Seric if (s < 0) 78157736Seric { 78257736Seric sav_errno = errno; 78357736Seric syserr("makeconnection: no socket"); 78457736Seric goto failure; 78557736Seric } 78610347Seric 78764381Seric #ifdef SO_SNDBUF 78864381Seric if (TcpSndBufferSize > 0) 78964381Seric { 79064381Seric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 79164561Seric (char *) &TcpSndBufferSize, 79264381Seric sizeof(TcpSndBufferSize)) < 0) 79364381Seric syserr("makeconnection: setsockopt(SO_SNDBUF)"); 79464381Seric } 79564381Seric #endif 79664381Seric 79757736Seric if (tTd(16, 1)) 79857736Seric printf("makeconnection: fd=%d\n", s); 79957736Seric 80057736Seric /* turn on network debugging? */ 80157736Seric if (tTd(16, 101)) 80257736Seric { 80357736Seric int on = 1; 80466861Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 80557736Seric (char *)&on, sizeof on); 80657736Seric } 80757736Seric if (CurEnv->e_xfp != NULL) 80857736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 80957736Seric errno = 0; /* for debugging */ 81058755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 81157736Seric break; 81257736Seric 81357736Seric /* couldn't connect.... figure out why */ 81427744Sbloom sav_errno = errno; 81527744Sbloom (void) close(s); 81629430Sbloom if (hp && hp->h_addr_list[i]) 81729430Sbloom { 81857736Seric if (tTd(16, 1)) 81958755Seric printf("Connect failed (%s); trying new address....\n", 82058755Seric errstring(sav_errno)); 82158778Seric switch (addr.sa.sa_family) 82258778Seric { 82358778Seric #ifdef NETINET 82458778Seric case AF_INET: 82558755Seric bcopy(hp->h_addr_list[i++], 82658778Seric &addr.sin.sin_addr, 82764943Seric sizeof addr.sin.sin_addr); 82858778Seric break; 82958778Seric #endif 83058778Seric 83158778Seric default: 83258755Seric bcopy(hp->h_addr_list[i++], 83358778Seric addr.sa.sa_data, 83452106Seric hp->h_length); 83558778Seric break; 83658778Seric } 83757736Seric continue; 83829430Sbloom } 83929430Sbloom 8406039Seric /* failure, decide if temporary or not */ 8416039Seric failure: 84259254Seric #ifdef XLA 84359254Seric xla_host_end(host); 84459254Seric #endif 84558542Seric if (transienterror(sav_errno)) 84658542Seric return EX_TEMPFAIL; 84758542Seric else 84858542Seric { 84958542Seric message("%s", errstring(sav_errno)); 85058542Seric return (EX_UNAVAILABLE); 8516039Seric } 8526039Seric } 8536039Seric 8546039Seric /* connection ok, put it into canonical form */ 85564724Seric if ((mci->mci_out = fdopen(s, "w")) == NULL || 85664724Seric (s = dup(s)) < 0 || 85764725Seric (mci->mci_in = fdopen(s, "r")) == NULL) 85864724Seric { 85964724Seric syserr("cannot open SMTP client channel, fd=%d", s); 86064724Seric return EX_TEMPFAIL; 86164724Seric } 8626039Seric 86310098Seric return (EX_OK); 8646039Seric } 86510758Seric /* 86610758Seric ** MYHOSTNAME -- return the name of this host. 86710758Seric ** 86810758Seric ** Parameters: 86910758Seric ** hostbuf -- a place to return the name of this host. 87012313Seric ** size -- the size of hostbuf. 87110758Seric ** 87210758Seric ** Returns: 87310758Seric ** A list of aliases for this host. 87410758Seric ** 87510758Seric ** Side Effects: 87664338Seric ** Adds numeric codes to $=w. 87710758Seric */ 8786039Seric 87910758Seric char ** 88012313Seric myhostname(hostbuf, size) 88110758Seric char hostbuf[]; 88212313Seric int size; 88310758Seric { 88458110Seric register struct hostent *hp; 88510758Seric extern struct hostent *gethostbyname(); 88610758Seric 88723120Seric if (gethostname(hostbuf, size) < 0) 88823120Seric { 88923120Seric (void) strcpy(hostbuf, "localhost"); 89023120Seric } 89111147Seric hp = gethostbyname(hostbuf); 89266853Seric if (hp == NULL) 89316877Seric { 89466853Seric syserr("!My host name (%s) does not seem to exist!", hostbuf); 89566853Seric } 89666853Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 89766853Seric hostbuf[size - 1] = '\0'; 89866853Seric 89966853Seric #if NAMED_BIND 90066853Seric /* if still no dot, try DNS directly (i.e., avoid NIS problems) */ 90166853Seric if (strchr(hostbuf, '.') == NULL) 90266853Seric { 90366853Seric extern bool getcanonname(); 90466853Seric extern int h_errno; 90566853Seric 90666853Seric /* try twice in case name server not yet started up */ 90766853Seric if (!getcanonname(hostbuf, size, TRUE) && 90866853Seric UseNameServer && 90966853Seric (h_errno != TRY_AGAIN || 91066853Seric (sleep(30), !getcanonname(hostbuf, size, TRUE)))) 91166777Seric { 91266853Seric errno = h_errno + E_DNSBASE; 91366853Seric syserr("!My host name (%s) not known to DNS", 91466853Seric hostbuf); 91566777Seric } 91666853Seric } 91766777Seric #endif 91866777Seric 91966853Seric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 92066853Seric { 92166853Seric register int i; 92266853Seric 92366853Seric for (i = 0; hp->h_addr_list[i] != NULL; i++) 92458110Seric { 92566853Seric char ipbuf[100]; 92658110Seric 92766853Seric sprintf(ipbuf, "[%s]", 92866853Seric inet_ntoa(*((struct in_addr *) hp->h_addr_list[i]))); 92966853Seric setclass('w', ipbuf); 93058110Seric } 93166853Seric } 93258110Seric 93366853Seric return (hp->h_aliases); 93410758Seric } 93551315Seric /* 93658951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 93758308Seric ** 93858951Seric ** Uses RFC1413 protocol to try to get info from the other end. 93958951Seric ** 94058308Seric ** Parameters: 94158308Seric ** fd -- the descriptor 94258308Seric ** 94358308Seric ** Returns: 94458951Seric ** The user@host information associated with this descriptor. 94558308Seric */ 94658308Seric 94764927Seric #if IDENTPROTO 94858951Seric 94958951Seric static jmp_buf CtxAuthTimeout; 95058951Seric 95158951Seric static 95258951Seric authtimeout() 95358951Seric { 95458951Seric longjmp(CtxAuthTimeout, 1); 95558951Seric } 95658951Seric 95758951Seric #endif 95858951Seric 95958308Seric char * 96058951Seric getauthinfo(fd) 96158308Seric int fd; 96258308Seric { 96358951Seric int falen; 96459104Seric register char *p; 96564927Seric #if IDENTPROTO 96658951Seric SOCKADDR la; 96758951Seric int lalen; 96858951Seric register struct servent *sp; 96958951Seric int s; 97058951Seric int i; 97158951Seric EVENT *ev; 97258951Seric #endif 97358951Seric static char hbuf[MAXNAME * 2 + 2]; 97458951Seric extern char *hostnamebyanyaddr(); 97558951Seric extern char RealUserName[]; /* main.c */ 97658308Seric 97766761Seric falen = sizeof RealHostAddr; 97866761Seric if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 || 97966761Seric RealHostAddr.sa.sa_family == 0) 98058951Seric { 98158951Seric (void) sprintf(hbuf, "%s@localhost", RealUserName); 98258957Seric if (tTd(9, 1)) 98358951Seric printf("getauthinfo: %s\n", hbuf); 98458951Seric return hbuf; 98558951Seric } 98658951Seric 98766761Seric if (RealHostName == NULL) 98866761Seric { 98966761Seric /* translate that to a host name */ 99066761Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 99166761Seric } 99266761Seric 99364927Seric #if IDENTPROTO 99465831Seric if (TimeOuts.to_ident == 0) 99565831Seric goto noident; 99665831Seric 99758951Seric lalen = sizeof la; 99866761Seric if (RealHostAddr.sa.sa_family != AF_INET || 99958951Seric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 100058951Seric la.sa.sa_family != AF_INET) 100158951Seric { 100258951Seric /* no ident info */ 100358951Seric goto noident; 100458951Seric } 100558951Seric 100658951Seric /* create ident query */ 100760489Seric (void) sprintf(hbuf, "%d,%d\r\n", 100866761Seric ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port)); 100958951Seric 101058951Seric /* create local address */ 101164747Seric la.sin.sin_port = 0; 101258951Seric 101358951Seric /* create foreign address */ 101458951Seric sp = getservbyname("auth", "tcp"); 101558951Seric if (sp != NULL) 101666761Seric RealHostAddr.sin.sin_port = sp->s_port; 101758308Seric else 101866761Seric RealHostAddr.sin.sin_port = htons(113); 101958951Seric 102058951Seric s = -1; 102158951Seric if (setjmp(CtxAuthTimeout) != 0) 102258951Seric { 102358951Seric if (s >= 0) 102458951Seric (void) close(s); 102558951Seric goto noident; 102658951Seric } 102758951Seric 102858951Seric /* put a timeout around the whole thing */ 102964255Seric ev = setevent(TimeOuts.to_ident, authtimeout, 0); 103058951Seric 103164747Seric /* connect to foreign IDENT server using same address as SMTP socket */ 103258951Seric s = socket(AF_INET, SOCK_STREAM, 0); 103358951Seric if (s < 0) 103458951Seric { 103558951Seric clrevent(ev); 103658951Seric goto noident; 103758951Seric } 103864747Seric if (bind(s, &la.sa, sizeof la.sin) < 0 || 103966761Seric connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0) 104058951Seric { 104166011Seric goto closeident; 104258951Seric } 104358951Seric 104458957Seric if (tTd(9, 10)) 104558951Seric printf("getauthinfo: sent %s", hbuf); 104658951Seric 104758951Seric /* send query */ 104858951Seric if (write(s, hbuf, strlen(hbuf)) < 0) 104958951Seric goto closeident; 105058951Seric 105158951Seric /* get result */ 105258951Seric i = read(s, hbuf, sizeof hbuf); 105358951Seric (void) close(s); 105458951Seric clrevent(ev); 105558951Seric if (i <= 0) 105658951Seric goto noident; 105758951Seric if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 105858951Seric i--; 105958951Seric hbuf[++i] = '\0'; 106058951Seric 106158957Seric if (tTd(9, 3)) 106258951Seric printf("getauthinfo: got %s\n", hbuf); 106358951Seric 106458951Seric /* parse result */ 106558951Seric p = strchr(hbuf, ':'); 106658951Seric if (p == NULL) 106758951Seric { 106858951Seric /* malformed response */ 106958951Seric goto noident; 107058951Seric } 107158951Seric while (isascii(*++p) && isspace(*p)) 107258951Seric continue; 107358951Seric if (strncasecmp(p, "userid", 6) != 0) 107458951Seric { 107558951Seric /* presumably an error string */ 107658951Seric goto noident; 107758951Seric } 107858951Seric p += 6; 107958951Seric while (isascii(*p) && isspace(*p)) 108058951Seric p++; 108158951Seric if (*p++ != ':') 108258951Seric { 108358951Seric /* either useridxx or malformed response */ 108458951Seric goto noident; 108558951Seric } 108658951Seric 108758951Seric /* p now points to the OSTYPE field */ 108858951Seric p = strchr(p, ':'); 108958951Seric if (p == NULL) 109058951Seric { 109158951Seric /* malformed response */ 109258951Seric goto noident; 109358951Seric } 109458951Seric 109558957Seric /* 1413 says don't do this -- but it's broken otherwise */ 109658957Seric while (isascii(*++p) && isspace(*p)) 109758957Seric continue; 109858957Seric 109958951Seric /* p now points to the authenticated name */ 110066003Seric (void) sprintf(hbuf, "%s@%s", 110166003Seric p, RealHostName == NULL ? "localhost" : RealHostName); 110258957Seric goto finish; 110358957Seric 110466011Seric closeident: 110566011Seric (void) close(s); 110666011Seric clrevent(ev); 110766011Seric 110858957Seric #endif /* IDENTPROTO */ 110958957Seric 111058957Seric noident: 111166003Seric if (RealHostName == NULL) 111266003Seric { 111366003Seric if (tTd(9, 1)) 111466003Seric printf("getauthinfo: NULL\n"); 111566003Seric return NULL; 111666003Seric } 111758957Seric (void) strcpy(hbuf, RealHostName); 111858957Seric 111958957Seric finish: 112066003Seric if (RealHostName != NULL && RealHostName[0] != '[') 112158951Seric { 112258951Seric p = &hbuf[strlen(hbuf)]; 112358951Seric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 112458951Seric } 112558957Seric if (tTd(9, 1)) 112658951Seric printf("getauthinfo: %s\n", hbuf); 112758308Seric return hbuf; 112858308Seric } 112958308Seric /* 113060089Seric ** HOST_MAP_LOOKUP -- turn a hostname into canonical form 113153751Seric ** 113253751Seric ** Parameters: 113356823Seric ** map -- a pointer to this map (unused). 113460089Seric ** name -- the (presumably unqualified) hostname. 113560257Seric ** av -- unused -- for compatibility with other mapping 113655019Seric ** functions. 113759084Seric ** statp -- an exit status (out parameter) -- set to 113859084Seric ** EX_TEMPFAIL if the name server is unavailable. 113953751Seric ** 114053751Seric ** Returns: 114153751Seric ** The mapping, if found. 114253751Seric ** NULL if no mapping found. 114353751Seric ** 114453751Seric ** Side Effects: 114553751Seric ** Looks up the host specified in hbuf. If it is not 114653751Seric ** the canonical name for that host, return the canonical 114753751Seric ** name. 114853751Seric */ 114951315Seric 115053751Seric char * 115160257Seric host_map_lookup(map, name, av, statp) 115256823Seric MAP *map; 115360089Seric char *name; 115460257Seric char **av; 115559084Seric int *statp; 115616911Seric { 115716911Seric register struct hostent *hp; 115833932Sbostic u_long in_addr; 115956823Seric char *cp; 116058110Seric int i; 116159671Seric register STAB *s; 116260257Seric char hbuf[MAXNAME]; 116359671Seric extern struct hostent *gethostbyaddr(); 116466334Seric #if NAMED_BIND 116559671Seric extern int h_errno; 116666029Seric #endif 116716911Seric 116825574Smiriam /* 116959671Seric ** See if we have already looked up this name. If so, just 117059671Seric ** return it. 117159671Seric */ 117253751Seric 117360089Seric s = stab(name, ST_NAMECANON, ST_ENTER); 117459671Seric if (bitset(NCF_VALID, s->s_namecanon.nc_flags)) 117559671Seric { 117659986Seric if (tTd(9, 1)) 117760089Seric printf("host_map_lookup(%s) => CACHE %s\n", 117860089Seric name, s->s_namecanon.nc_cname); 117959671Seric errno = s->s_namecanon.nc_errno; 118066334Seric #if NAMED_BIND 118159671Seric h_errno = s->s_namecanon.nc_herrno; 118266029Seric #endif 118359671Seric *statp = s->s_namecanon.nc_stat; 118464797Seric if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL) 118565199Seric { 118665199Seric sprintf(hbuf, "%s: Name server timeout", 118765199Seric shortenstring(name, 33)); 118865199Seric CurEnv->e_message = newstr(hbuf); 118965199Seric } 119059671Seric return s->s_namecanon.nc_cname; 119159671Seric } 119259671Seric 119359671Seric /* 119459671Seric ** If first character is a bracket, then it is an address 119559671Seric ** lookup. Address is copied into a temporary buffer to 119660089Seric ** strip the brackets and to preserve name if address is 119759671Seric ** unknown. 119859671Seric */ 119959671Seric 120060089Seric if (*name != '[') 120153751Seric { 120255019Seric extern bool getcanonname(); 120355019Seric 120458798Seric if (tTd(9, 1)) 120560089Seric printf("host_map_lookup(%s) => ", name); 120659671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 120760089Seric (void) strcpy(hbuf, name); 120863842Seric if (getcanonname(hbuf, sizeof hbuf - 1, TRUE)) 120958796Seric { 121058796Seric if (tTd(9, 1)) 121158796Seric printf("%s\n", hbuf); 121260257Seric cp = map_rewrite(map, hbuf, strlen(hbuf), av); 121360257Seric s->s_namecanon.nc_cname = newstr(cp); 121460257Seric return cp; 121558796Seric } 121653751Seric else 121758796Seric { 121859084Seric register struct hostent *hp; 121959084Seric 122066029Seric s->s_namecanon.nc_errno = errno; 122166334Seric #if NAMED_BIND 122266029Seric s->s_namecanon.nc_herrno = h_errno; 122358796Seric if (tTd(9, 1)) 122459084Seric printf("FAIL (%d)\n", h_errno); 122559084Seric switch (h_errno) 122659084Seric { 122759084Seric case TRY_AGAIN: 122859596Seric if (UseNameServer) 122959734Seric { 123065202Seric sprintf(hbuf, "%s: Name server timeout", 123165199Seric shortenstring(name, 33)); 123265202Seric message("%s", hbuf); 123359734Seric if (CurEnv->e_message == NULL) 123465202Seric CurEnv->e_message = newstr(hbuf); 123559734Seric } 123659084Seric *statp = EX_TEMPFAIL; 123759084Seric break; 123859084Seric 123959084Seric case HOST_NOT_FOUND: 124059084Seric *statp = EX_NOHOST; 124159084Seric break; 124259084Seric 124359084Seric case NO_RECOVERY: 124459084Seric *statp = EX_SOFTWARE; 124559084Seric break; 124659084Seric 124759084Seric default: 124859084Seric *statp = EX_UNAVAILABLE; 124959084Seric break; 125059084Seric } 125166029Seric #else 125266029Seric if (tTd(9, 1)) 125366029Seric printf("FAIL\n"); 125466029Seric *statp = EX_NOHOST; 125566029Seric #endif 125659671Seric s->s_namecanon.nc_stat = *statp; 125759084Seric if (*statp != EX_TEMPFAIL || UseNameServer) 125859084Seric return NULL; 125959084Seric 126059084Seric /* 126159084Seric ** Try to look it up in /etc/hosts 126259084Seric */ 126359084Seric 126460089Seric hp = gethostbyname(name); 126559084Seric if (hp == NULL) 126659084Seric { 126759084Seric /* no dice there either */ 126859671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 126959084Seric return NULL; 127059084Seric } 127159084Seric 127259671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 127360257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 127460257Seric s->s_namecanon.nc_cname = newstr(cp); 127560257Seric return cp; 127658796Seric } 127753751Seric } 127860089Seric if ((cp = strchr(name, ']')) == NULL) 127953751Seric return (NULL); 128040994Sbostic *cp = '\0'; 128160089Seric in_addr = inet_addr(&name[1]); 128258110Seric 128358110Seric /* nope -- ask the name server */ 128433932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 128559671Seric s->s_namecanon.nc_errno = errno; 128666334Seric #if NAMED_BIND 128759671Seric s->s_namecanon.nc_herrno = h_errno; 128866029Seric #endif 128959671Seric s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */ 129033932Sbostic if (hp == NULL) 129159671Seric { 129259671Seric s->s_namecanon.nc_stat = *statp = EX_NOHOST; 129353751Seric return (NULL); 129459671Seric } 129553751Seric 129658110Seric /* found a match -- copy out */ 129760257Seric cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av); 129859671Seric s->s_namecanon.nc_stat = *statp = EX_OK; 129960257Seric s->s_namecanon.nc_cname = newstr(cp); 130060257Seric return cp; 130133932Sbostic } 130258755Seric /* 130358755Seric ** ANYNET_NTOA -- convert a network address to printable form. 130458755Seric ** 130558755Seric ** Parameters: 130658755Seric ** sap -- a pointer to a sockaddr structure. 130758755Seric ** 130858755Seric ** Returns: 130958755Seric ** A printable version of that sockaddr. 131058755Seric */ 131116911Seric 131258755Seric char * 131358755Seric anynet_ntoa(sap) 131458755Seric register SOCKADDR *sap; 131558755Seric { 131658755Seric register char *bp; 131758755Seric register char *ap; 131858755Seric int l; 131964734Seric static char buf[100]; 132058755Seric 132158798Seric /* check for null/zero family */ 132258798Seric if (sap == NULL) 132358798Seric return "NULLADDR"; 132458798Seric if (sap->sa.sa_family == 0) 132558798Seric return "0"; 132658798Seric 132764734Seric switch (sap->sa.sa_family) 132864734Seric { 132964734Seric #ifdef MAYBENEXTRELEASE /*** UNTESTED *** UNTESTED *** UNTESTED ***/ 133064821Seric #ifdef NETUNIX 133164734Seric case AF_UNIX: 133264758Seric if (sap->sunix.sun_path[0] != '\0') 133364758Seric sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path); 133464734Seric else 133564734Seric sprintf(buf, "[UNIX: localhost]"); 133664734Seric return buf; 133764734Seric #endif 133864821Seric #endif 133964734Seric 134058778Seric #ifdef NETINET 134164734Seric case AF_INET: 134258755Seric return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 134358778Seric #endif 134458755Seric 134564734Seric default: 134664734Seric /* this case is only to ensure syntactic correctness */ 134764734Seric break; 134864734Seric } 134964734Seric 135058755Seric /* unknown family -- just dump bytes */ 135158778Seric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 135258755Seric bp = &buf[strlen(buf)]; 135358778Seric ap = sap->sa.sa_data; 135458778Seric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 135558755Seric { 135658755Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 135758755Seric bp += 3; 135858755Seric } 135958755Seric *--bp = '\0'; 136058755Seric return buf; 136158755Seric } 136258951Seric /* 136358951Seric ** HOSTNAMEBYANYADDR -- return name of host based on address 136458951Seric ** 136558951Seric ** Parameters: 136658951Seric ** sap -- SOCKADDR pointer 136758951Seric ** 136858951Seric ** Returns: 136958951Seric ** text representation of host name. 137058951Seric ** 137158951Seric ** Side Effects: 137258951Seric ** none. 137358951Seric */ 137458755Seric 137558951Seric char * 137658951Seric hostnamebyanyaddr(sap) 137758951Seric register SOCKADDR *sap; 137858951Seric { 137958951Seric register struct hostent *hp; 138064734Seric int saveretry; 138158951Seric 138266334Seric #if NAMED_BIND 138359042Seric /* shorten name server timeout to avoid higher level timeouts */ 138459042Seric saveretry = _res.retry; 138559042Seric _res.retry = 3; 138659042Seric #endif /* NAMED_BIND */ 138759042Seric 138858951Seric switch (sap->sa.sa_family) 138958951Seric { 139058951Seric #ifdef NETINET 139158951Seric case AF_INET: 139258951Seric hp = gethostbyaddr((char *) &sap->sin.sin_addr, 139358951Seric sizeof sap->sin.sin_addr, 139458951Seric AF_INET); 139558951Seric break; 139658951Seric #endif 139758951Seric 139858951Seric #ifdef NETISO 139958951Seric case AF_ISO: 140058951Seric hp = gethostbyaddr((char *) &sap->siso.siso_addr, 140158951Seric sizeof sap->siso.siso_addr, 140258951Seric AF_ISO); 140358951Seric break; 140458951Seric #endif 140558951Seric 140664734Seric #ifdef MAYBENEXTRELEASE /*** UNTESTED *** UNTESTED *** UNTESTED ***/ 140764734Seric case AF_UNIX: 140864734Seric hp = NULL; 140964734Seric break; 141064734Seric #endif 141164734Seric 141258951Seric default: 141358951Seric hp = gethostbyaddr(sap->sa.sa_data, 141458951Seric sizeof sap->sa.sa_data, 141558951Seric sap->sa.sa_family); 141658951Seric break; 141758951Seric } 141858951Seric 141966334Seric #if NAMED_BIND 142059042Seric _res.retry = saveretry; 142159042Seric #endif /* NAMED_BIND */ 142259042Seric 142358951Seric if (hp != NULL) 142458951Seric return hp->h_name; 142558951Seric else 142658951Seric { 142758951Seric /* produce a dotted quad */ 142858951Seric static char buf[512]; 142958951Seric 143058951Seric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 143158951Seric return buf; 143258951Seric } 143358951Seric } 143458951Seric 143556795Seric # else /* DAEMON */ 143616911Seric /* code for systems without sophisticated networking */ 143710758Seric 143810758Seric /* 143910758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 144011297Seric ** 144111297Seric ** Can't convert to upper case here because might be a UUCP name. 144212313Seric ** 144312313Seric ** Mark, you can change this to be anything you want...... 144410758Seric */ 144510758Seric 144610758Seric char ** 144712313Seric myhostname(hostbuf, size) 144810758Seric char hostbuf[]; 144912313Seric int size; 145010758Seric { 145110758Seric register FILE *f; 145210758Seric 145310758Seric hostbuf[0] = '\0'; 145410758Seric f = fopen("/usr/include/whoami", "r"); 145510758Seric if (f != NULL) 145610758Seric { 145712313Seric (void) fgets(hostbuf, size, f); 145810758Seric fixcrlf(hostbuf, TRUE); 145910758Seric (void) fclose(f); 146010758Seric } 146110758Seric return (NULL); 146210758Seric } 146316911Seric /* 146458951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 146558308Seric ** 146658308Seric ** Parameters: 146758308Seric ** fd -- the descriptor 146858308Seric ** 146958308Seric ** Returns: 147058308Seric ** The host name associated with this descriptor, if it can 147158308Seric ** be determined. 147258308Seric ** NULL otherwise. 147358308Seric ** 147458308Seric ** Side Effects: 147558308Seric ** none 147658308Seric */ 147758308Seric 147858308Seric char * 147958951Seric getauthinfo(fd) 148058308Seric int fd; 148158308Seric { 148258308Seric return NULL; 148358308Seric } 148458308Seric /* 148516911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 148616911Seric ** 148716911Seric ** Parameters: 148856823Seric ** map -- a pointer to the database map. 148960089Seric ** name -- a buffer containing a hostname. 149053751Seric ** avp -- a pointer to a (cf file defined) argument vector. 149159084Seric ** statp -- an exit status (out parameter). 149216911Seric ** 149316911Seric ** Returns: 149453751Seric ** mapped host name 149551315Seric ** FALSE otherwise. 149616911Seric ** 149716911Seric ** Side Effects: 149860089Seric ** Looks up the host specified in name. If it is not 149916911Seric ** the canonical name for that host, replace it with 150016911Seric ** the canonical name. If the name is unknown, or it 150116911Seric ** is already the canonical name, leave it unchanged. 150216911Seric */ 150310758Seric 150416911Seric /*ARGSUSED*/ 150553751Seric char * 150660089Seric host_map_lookup(map, name, avp, statp) 150756823Seric MAP *map; 150860089Seric char *name; 150953751Seric char **avp; 151059084Seric char *statp; 151116911Seric { 151259084Seric register struct hostent *hp; 151359084Seric 151460089Seric hp = gethostbyname(name); 151559084Seric if (hp != NULL) 151659084Seric return hp->h_name; 151759084Seric *statp = EX_NOHOST; 151853751Seric return NULL; 151916911Seric } 152016911Seric 152156795Seric #endif /* DAEMON */ 1522