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*67421Seric static char sccsid[] = "@(#)daemon.c 8.54 (Berkeley) 06/17/94 (with daemon mode)"; 1533780Sbostic #else 16*67421Seric static char sccsid[] = "@(#)daemon.c 8.54 (Berkeley) 06/17/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 <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 824535Seric getrequests() 834535Seric { 849610Seric int t; 8553751Seric bool refusingconnections = TRUE; 8658419Seric FILE *pidf; 8764828Seric int socksize; 8866793Seric #ifdef XDEBUG 8966793Seric bool j_has_dot; 9066793Seric #endif 9146928Sbostic extern void reapchild(); 927117Seric 939610Seric /* 949610Seric ** Set up the address for the mailer. 959610Seric */ 969610Seric 9758849Seric if (DaemonAddr.sin.sin_family == 0) 9858849Seric DaemonAddr.sin.sin_family = AF_INET; 9958849Seric if (DaemonAddr.sin.sin_addr.s_addr == 0) 10058849Seric DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY; 10158849Seric if (DaemonAddr.sin.sin_port == 0) 1029610Seric { 10365169Seric register struct servent *sp; 10465169Seric 10558849Seric sp = getservbyname("smtp", "tcp"); 10658849Seric if (sp == NULL) 10758849Seric { 10858909Seric syserr("554 service \"smtp\" unknown"); 10965169Seric DaemonAddr.sin.sin_port = htons(25); 11058849Seric } 11165169Seric else 11265169Seric DaemonAddr.sin.sin_port = sp->s_port; 1139610Seric } 1149610Seric 1159610Seric /* 1169610Seric ** Try to actually open the connection. 1179610Seric */ 1189610Seric 1199610Seric if (tTd(15, 1)) 12058849Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1219610Seric 1229610Seric /* get a socket for the SMTP connection */ 12366854Seric socksize = opendaemonsocket(TRUE); 12410347Seric 12564035Seric (void) setsignal(SIGCHLD, reapchild); 12624945Seric 12758419Seric /* write the pid to the log file for posterity */ 12858419Seric pidf = fopen(PidFile, "w"); 12958419Seric if (pidf != NULL) 13058419Seric { 13163863Seric extern char *CommandLineArgs; 13263863Seric 13363863Seric /* write the process id on line 1 */ 13458419Seric fprintf(pidf, "%d\n", getpid()); 13563863Seric 13663863Seric /* line 2 contains all command line flags */ 13763863Seric fprintf(pidf, "%s\n", CommandLineArgs); 13863863Seric 13963863Seric /* flush and close */ 14058419Seric fclose(pidf); 14158419Seric } 14258419Seric 14366793Seric #ifdef XDEBUG 14466793Seric { 14566812Seric char jbuf[MAXHOSTNAMELEN]; 14658419Seric 14766812Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 14866812Seric j_has_dot = strchr(jbuf, '.') != NULL; 14966793Seric } 15066793Seric #endif 15166793Seric 1529610Seric if (tTd(15, 1)) 15310206Seric printf("getrequests: %d\n", DaemonSocket); 1549610Seric 1554631Seric for (;;) 1564631Seric { 15714875Seric register int pid; 15811147Seric auto int lotherend; 15953751Seric extern bool refuseconnections(); 16011147Seric 16114875Seric /* see if we are rejecting connections */ 16253751Seric CurrentLA = getla(); 16353751Seric if (refuseconnections()) 16436584Sbostic { 16566845Seric if (DaemonSocket >= 0) 16653751Seric { 16766845Seric /* close socket so peer will fail quickly */ 16866845Seric (void) close(DaemonSocket); 16966845Seric DaemonSocket = -1; 17053751Seric } 17166845Seric refusingconnections = TRUE; 17257385Seric setproctitle("rejecting connections: load average: %d", 17357385Seric CurrentLA); 17466845Seric sleep(15); 17553751Seric continue; 17636584Sbostic } 17714875Seric 17853751Seric if (refusingconnections) 17953751Seric { 18053751Seric /* start listening again */ 18166854Seric (void) opendaemonsocket(FALSE); 18253751Seric setproctitle("accepting connections"); 18353751Seric refusingconnections = FALSE; 18453751Seric } 18553751Seric 18666793Seric #ifdef XDEBUG 18766793Seric /* check for disaster */ 18866793Seric { 18966793Seric register STAB *s; 19066812Seric char jbuf[MAXHOSTNAMELEN]; 19166793Seric 19266812Seric expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv); 19366812Seric if ((s = stab(jbuf, ST_CLASS, ST_FIND)) == NULL || 19466793Seric !bitnset('w', s->s_class)) 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"); 2209610Seric sleep(5); 2219610Seric continue; 2225978Seric } 2234631Seric 2245978Seric /* 2255978Seric ** Create a subprocess to process the mail. 2265978Seric */ 2275978Seric 2287677Seric if (tTd(15, 2)) 2299610Seric printf("getrequests: forking (fd = %d)\n", t); 2305978Seric 2314636Seric pid = fork(); 2324636Seric if (pid < 0) 2334631Seric { 2344636Seric syserr("daemon: cannot fork"); 2354636Seric sleep(10); 2369610Seric (void) close(t); 2374636Seric continue; 2384631Seric } 2394631Seric 2404636Seric if (pid == 0) 2414631Seric { 24264086Seric char *p; 24358951Seric extern char *hostnamebyanyaddr(); 24411147Seric 2454636Seric /* 2464636Seric ** CHILD -- return to caller. 24711147Seric ** Collect verified idea of sending host. 2484636Seric ** Verify calling user id if possible here. 2494636Seric */ 2504631Seric 25164035Seric (void) setsignal(SIGCHLD, SIG_DFL); 25267171Seric (void) close(DaemonSocket); 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 27264724Seric if ((InChannel = fdopen(t, "r")) == NULL || 27364724Seric (t = dup(t)) < 0 || 27464724Seric (OutChannel = fdopen(t, "w")) == NULL) 27564724Seric { 27664724Seric syserr("cannot open SMTP server channel, fd=%d", t); 27764724Seric exit(0); 27864724Seric } 27959254Seric 28016884Seric /* should we check for illegal connection here? XXX */ 28159156Seric #ifdef XLA 28259156Seric if (!xla_host_ok(RealHostName)) 28359156Seric { 28459254Seric message("421 Too many SMTP sessions for this host"); 28559156Seric exit(0); 28659156Seric } 28759156Seric #endif 28816884Seric 2897677Seric if (tTd(15, 2)) 2905978Seric printf("getreq: returning\n"); 2914636Seric return; 2924631Seric } 2934631Seric 2947117Seric /* close the port so that others will hang (for a while) */ 2959610Seric (void) close(t); 2964631Seric } 2979886Seric /*NOTREACHED*/ 2984631Seric } 2995978Seric /* 30066845Seric ** OPENDAEMONSOCKET -- open the SMTP socket 30166845Seric ** 30266845Seric ** Deals with setting all appropriate options. DaemonAddr must 30366845Seric ** be set up in advance. 30466845Seric ** 30566845Seric ** Parameters: 30666854Seric ** firsttime -- set if this is the initial open. 30766845Seric ** 30866845Seric ** Returns: 30966845Seric ** Size in bytes of the daemon socket addr. 31066845Seric ** 31166845Seric ** Side Effects: 31266845Seric ** Leaves DaemonSocket set to the open socket. 31366845Seric ** Exits if the socket cannot be created. 31466845Seric */ 31566845Seric 31666861Seric #define MAXOPENTRIES 10 /* maximum number of tries to open connection */ 31766861Seric 31866845Seric int 31966854Seric opendaemonsocket(firsttime) 32066854Seric bool firsttime; 32166845Seric { 32266845Seric int on = 1; 32366845Seric int socksize; 32466861Seric int ntries = 0; 32566861Seric int saveerrno; 32666845Seric 32766845Seric if (tTd(15, 2)) 32866845Seric printf("opendaemonsocket()\n"); 32966845Seric 33066861Seric do 33166845Seric { 33266862Seric if (ntries > 0) 33366862Seric sleep(5); 33466861Seric if (firsttime || DaemonSocket < 0) 33566854Seric { 33666861Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 33766861Seric if (DaemonSocket < 0) 33866861Seric { 33966861Seric /* probably another daemon already */ 34066861Seric saveerrno = errno; 34166861Seric syserr("opendaemonsocket: can't create server SMTP socket"); 34266861Seric severe: 34366845Seric # ifdef LOG 34466861Seric if (LogLevel > 0) 34566861Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 34666845Seric # endif /* LOG */ 34766861Seric DaemonSocket = -1; 34866861Seric continue; 34966861Seric } 35066845Seric 35166861Seric /* turn on network debugging? */ 35266861Seric if (tTd(15, 101)) 35366861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 35466861Seric SO_DEBUG, (char *)&on, 35566861Seric sizeof on); 35666845Seric 35766861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 35866861Seric SO_REUSEADDR, (char *)&on, sizeof on); 35966861Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, 36066861Seric SO_KEEPALIVE, (char *)&on, sizeof on); 36166845Seric 36266845Seric #ifdef SO_RCVBUF 36366861Seric if (TcpRcvBufferSize > 0) 36466861Seric { 36566861Seric if (setsockopt(DaemonSocket, SOL_SOCKET, 36666861Seric SO_RCVBUF, 36766861Seric (char *) &TcpRcvBufferSize, 36866861Seric sizeof(TcpRcvBufferSize)) < 0) 36966861Seric syserr("getrequests: setsockopt(SO_RCVBUF)"); 37066861Seric } 37166845Seric #endif 37266845Seric 37366861Seric switch (DaemonAddr.sa.sa_family) 37466861Seric { 37566845Seric # ifdef NETINET 37666861Seric case AF_INET: 37766861Seric socksize = sizeof DaemonAddr.sin; 37866861Seric break; 37966845Seric # endif 38066845Seric 38166845Seric # ifdef NETISO 38266861Seric case AF_ISO: 38366861Seric socksize = sizeof DaemonAddr.siso; 38466861Seric break; 38566845Seric # endif 38666845Seric 38766861Seric default: 38866861Seric socksize = sizeof DaemonAddr; 38966861Seric break; 39066861Seric } 39166861Seric 39266861Seric if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0) 39366861Seric { 39466861Seric saveerrno = errno; 39566861Seric syserr("getrequests: cannot bind"); 39666861Seric (void) close(DaemonSocket); 39766861Seric goto severe; 39866861Seric } 39966854Seric } 40066861Seric if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0) 40166854Seric { 40266861Seric saveerrno = errno; 40366861Seric syserr("getrequests: cannot listen"); 40466854Seric (void) close(DaemonSocket); 40566854Seric goto severe; 40666854Seric } 40766861Seric return socksize; 40866861Seric } while (ntries++ < MAXOPENTRIES && transienterror(saveerrno)); 40966861Seric finis(); 41066845Seric } 41166845Seric /* 41210206Seric ** CLRDAEMON -- reset the daemon connection 41310206Seric ** 41410206Seric ** Parameters: 41510206Seric ** none. 41610206Seric ** 41710206Seric ** Returns: 41810206Seric ** none. 41910206Seric ** 42010206Seric ** Side Effects: 42110206Seric ** releases any resources used by the passive daemon. 42210206Seric */ 42310206Seric 42410206Seric clrdaemon() 42510206Seric { 42610206Seric if (DaemonSocket >= 0) 42710206Seric (void) close(DaemonSocket); 42810206Seric DaemonSocket = -1; 42910206Seric } 43010206Seric /* 43158849Seric ** SETDAEMONOPTIONS -- set options for running the daemon 43258849Seric ** 43358849Seric ** Parameters: 43458849Seric ** p -- the options line. 43558849Seric ** 43658849Seric ** Returns: 43758849Seric ** none. 43858849Seric */ 43958849Seric 44058849Seric setdaemonoptions(p) 44158849Seric register char *p; 44258849Seric { 44358873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 44458873Seric DaemonAddr.sa.sa_family = AF_INET; 44558873Seric 44658849Seric while (p != NULL) 44758849Seric { 44858849Seric register char *f; 44958849Seric register char *v; 45058849Seric 45158849Seric while (isascii(*p) && isspace(*p)) 45258849Seric p++; 45358849Seric if (*p == '\0') 45458849Seric break; 45558849Seric f = p; 45658849Seric p = strchr(p, ','); 45758849Seric if (p != NULL) 45858849Seric *p++ = '\0'; 45958849Seric v = strchr(f, '='); 46058849Seric if (v == NULL) 46158849Seric continue; 46258849Seric while (isascii(*++v) && isspace(*v)) 46358849Seric continue; 46458849Seric 46558849Seric switch (*f) 46658849Seric { 46758873Seric case 'F': /* address family */ 46858849Seric if (isascii(*v) && isdigit(*v)) 46958873Seric DaemonAddr.sa.sa_family = atoi(v); 47058873Seric #ifdef NETINET 47158873Seric else if (strcasecmp(v, "inet") == 0) 47258873Seric DaemonAddr.sa.sa_family = AF_INET; 47358873Seric #endif 47458873Seric #ifdef NETISO 47558873Seric else if (strcasecmp(v, "iso") == 0) 47658873Seric DaemonAddr.sa.sa_family = AF_ISO; 47758873Seric #endif 47858873Seric #ifdef NETNS 47958873Seric else if (strcasecmp(v, "ns") == 0) 48058873Seric DaemonAddr.sa.sa_family = AF_NS; 48158873Seric #endif 48258873Seric #ifdef NETX25 48358873Seric else if (strcasecmp(v, "x.25") == 0) 48458873Seric DaemonAddr.sa.sa_family = AF_CCITT; 48558873Seric #endif 48658849Seric else 48758873Seric syserr("554 Unknown address family %s in Family=option", v); 48858873Seric break; 48958873Seric 49058873Seric case 'A': /* address */ 49158873Seric switch (DaemonAddr.sa.sa_family) 49258849Seric { 49358873Seric #ifdef NETINET 49458873Seric case AF_INET: 49558873Seric if (isascii(*v) && isdigit(*v)) 49658873Seric DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 49758873Seric else 49858873Seric { 49958873Seric register struct netent *np; 50058849Seric 50158873Seric np = getnetbyname(v); 50258873Seric if (np == NULL) 50358873Seric syserr("554 network \"%s\" unknown", v); 50458873Seric else 50558873Seric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 50658873Seric } 50758873Seric break; 50858873Seric #endif 50958873Seric 51058873Seric default: 51158873Seric syserr("554 Address= option unsupported for family %d", 51258873Seric DaemonAddr.sa.sa_family); 51358873Seric break; 51458849Seric } 51558849Seric break; 51658849Seric 51758873Seric case 'P': /* port */ 51858873Seric switch (DaemonAddr.sa.sa_family) 51958849Seric { 52058873Seric short port; 52158849Seric 52258873Seric #ifdef NETINET 52358873Seric case AF_INET: 52458873Seric if (isascii(*v) && isdigit(*v)) 52564366Seric DaemonAddr.sin.sin_port = htons(atoi(v)); 52658849Seric else 52758873Seric { 52858873Seric register struct servent *sp; 52958873Seric 53058873Seric sp = getservbyname(v, "tcp"); 53158873Seric if (sp == NULL) 53258909Seric syserr("554 service \"%s\" unknown", v); 53358873Seric else 53458873Seric DaemonAddr.sin.sin_port = sp->s_port; 53558873Seric } 53658873Seric break; 53758873Seric #endif 53858873Seric 53958873Seric #ifdef NETISO 54058873Seric case AF_ISO: 54158873Seric /* assume two byte transport selector */ 54258873Seric if (isascii(*v) && isdigit(*v)) 54364366Seric port = htons(atoi(v)); 54458873Seric else 54558873Seric { 54658873Seric register struct servent *sp; 54758873Seric 54858873Seric sp = getservbyname(v, "tcp"); 54958873Seric if (sp == NULL) 55058909Seric syserr("554 service \"%s\" unknown", v); 55158873Seric else 55258873Seric port = sp->s_port; 55358873Seric } 55458873Seric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 55558873Seric break; 55658873Seric #endif 55758873Seric 55858873Seric default: 55958873Seric syserr("554 Port= option unsupported for family %d", 56058873Seric DaemonAddr.sa.sa_family); 56158873Seric break; 56258849Seric } 56358849Seric break; 56459783Seric 56559783Seric case 'L': /* listen queue size */ 56659783Seric ListenQueueSize = atoi(v); 56759783Seric break; 56864381Seric 56964381Seric case 'S': /* send buffer size */ 57064381Seric TcpSndBufferSize = atoi(v); 57164381Seric break; 57264381Seric 57364381Seric case 'R': /* receive buffer size */ 57464381Seric TcpRcvBufferSize = atoi(v); 57564381Seric break; 57658849Seric } 57758849Seric } 57858849Seric } 57958849Seric /* 5806039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 5816039Seric ** 5826039Seric ** Parameters: 5836039Seric ** host -- the name of the host. 5846633Seric ** port -- the port number to connect to. 58553739Seric ** mci -- a pointer to the mail connection information 58653739Seric ** structure to be filled in. 58752106Seric ** usesecureport -- if set, use a low numbered (reserved) 58852106Seric ** port to provide some rudimentary authentication. 5896039Seric ** 5906039Seric ** Returns: 5916039Seric ** An exit code telling whether the connection could be 5926039Seric ** made and if not why not. 5936039Seric ** 5946039Seric ** Side Effects: 5956039Seric ** none. 5966039Seric */ 5975978Seric 59858755Seric SOCKADDR CurHostAddr; /* address of current host */ 59958305Seric 60054967Seric int 60153739Seric makeconnection(host, port, mci, usesecureport) 6026039Seric char *host; 6037286Seric u_short port; 60454967Seric register MCI *mci; 60552106Seric bool usesecureport; 6066039Seric { 60729430Sbloom register int i, s; 60829430Sbloom register struct hostent *hp = (struct hostent *)NULL; 60958755Seric SOCKADDR addr; 61052106Seric int sav_errno; 61158755Seric int addrlen; 61266334Seric #if NAMED_BIND 61335651Seric extern int h_errno; 61435651Seric #endif 6156039Seric 6166039Seric /* 6176039Seric ** Set up the address for the mailer. 6189308Seric ** Accept "[a.b.c.d]" syntax for host name. 6196039Seric */ 6206039Seric 62166334Seric #if NAMED_BIND 62225475Smiriam h_errno = 0; 62335651Seric #endif 62425475Smiriam errno = 0; 62558864Seric bzero(&CurHostAddr, sizeof CurHostAddr); 62664334Seric SmtpPhase = mci->mci_phase = "initial connection"; 62758906Seric CurHostName = host; 62825475Smiriam 6299308Seric if (host[0] == '[') 6309308Seric { 63111147Seric long hid; 63256795Seric register char *p = strchr(host, ']'); 6339308Seric 63411147Seric if (p != NULL) 6359308Seric { 63611147Seric *p = '\0'; 63759884Seric #ifdef NETINET 63811147Seric hid = inet_addr(&host[1]); 63958360Seric if (hid == -1) 64059884Seric #endif 64158360Seric { 64258360Seric /* try it as a host name (avoid MX lookup) */ 64358360Seric hp = gethostbyname(&host[1]); 64466349Seric if (hp == NULL && p[-1] == '.') 64566349Seric { 64667265Seric #ifdef NAMED_BIND 64767265Seric int oldopts = _res.options; 64867265Seric 64967265Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 65067265Seric #endif 65166349Seric p[-1] = '\0'; 65266349Seric hp = gethostbyname(&host[1]); 65366349Seric p[-1] = '.'; 65467265Seric #ifdef NAMED_BIND 65567265Seric _res.options = oldopts; 65667265Seric #endif 65766349Seric } 65858360Seric *p = ']'; 65958360Seric goto gothostent; 66058360Seric } 66111147Seric *p = ']'; 6629308Seric } 66358360Seric if (p == NULL) 6649308Seric { 66558151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 6669308Seric return (EX_NOHOST); 6679308Seric } 66859884Seric #ifdef NETINET 66959884Seric addr.sin.sin_family = AF_INET; /*XXX*/ 67058778Seric addr.sin.sin_addr.s_addr = hid; 67159884Seric #endif 6729308Seric } 6739610Seric else 6749610Seric { 67566349Seric register char *p = &host[strlen(host) - 1]; 67666349Seric 67729430Sbloom hp = gethostbyname(host); 67866349Seric if (hp == NULL && *p == '.') 67966349Seric { 68067265Seric #ifdef NAMED_BIND 68167265Seric int oldopts = _res.options; 68267265Seric 68367265Seric _res.options &= ~(RES_DEFNAMES|RES_DNSRCH); 68467265Seric #endif 68566349Seric *p = '\0'; 68666349Seric hp = gethostbyname(host); 68766349Seric *p = '.'; 68867265Seric #ifdef NAMED_BIND 68967265Seric _res.options = oldopts; 69067265Seric #endif 69166349Seric } 69258360Seric gothostent: 69325475Smiriam if (hp == NULL) 69424945Seric { 69566334Seric #if NAMED_BIND 69625475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 69725475Smiriam return (EX_TEMPFAIL); 69825657Seric 69935651Seric /* if name server is specified, assume temp fail */ 70035651Seric if (errno == ECONNREFUSED && UseNameServer) 70135651Seric return (EX_TEMPFAIL); 70235651Seric #endif 70325475Smiriam return (EX_NOHOST); 70424945Seric } 70558778Seric addr.sa.sa_family = hp->h_addrtype; 70658778Seric switch (hp->h_addrtype) 70758778Seric { 70858778Seric #ifdef NETINET 70958778Seric case AF_INET: 71058755Seric bcopy(hp->h_addr, 71158778Seric &addr.sin.sin_addr, 712*67421Seric INADDRSZ); 71358778Seric break; 71458778Seric #endif 71558778Seric 71658778Seric default: 71758755Seric bcopy(hp->h_addr, 71858778Seric addr.sa.sa_data, 71958755Seric hp->h_length); 72058778Seric break; 72158778Seric } 72229430Sbloom i = 1; 7239610Seric } 7249610Seric 7259610Seric /* 7269610Seric ** Determine the port number. 7279610Seric */ 7289610Seric 72910011Seric if (port != 0) 73058755Seric port = htons(port); 73110011Seric else 7329610Seric { 7339610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 7349610Seric 7359610Seric if (sp == NULL) 7369610Seric { 73758909Seric syserr("554 makeconnection: service \"smtp\" unknown"); 73865169Seric port = htons(25); 7399610Seric } 74065169Seric else 74165169Seric port = sp->s_port; 7429610Seric } 7436039Seric 74458778Seric switch (addr.sa.sa_family) 74558755Seric { 74659884Seric #ifdef NETINET 74758755Seric case AF_INET: 74858778Seric addr.sin.sin_port = port; 74958755Seric addrlen = sizeof (struct sockaddr_in); 75058755Seric break; 75159884Seric #endif 75258755Seric 75358755Seric #ifdef NETISO 75458755Seric case AF_ISO: 75558755Seric /* assume two byte transport selector */ 75658755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 75758755Seric addrlen = sizeof (struct sockaddr_iso); 75858755Seric break; 75958755Seric #endif 76058755Seric 76158755Seric default: 76258778Seric syserr("Can't connect to address family %d", addr.sa.sa_family); 76358755Seric return (EX_NOHOST); 76458755Seric } 76558755Seric 7666039Seric /* 7676039Seric ** Try to actually open the connection. 7686039Seric */ 7696039Seric 77059156Seric #ifdef XLA 77159156Seric /* if too many connections, don't bother trying */ 77259156Seric if (!xla_noqueue_ok(host)) 77359156Seric return EX_TEMPFAIL; 77459156Seric #endif 77559156Seric 77657736Seric for (;;) 77752106Seric { 77857736Seric if (tTd(16, 1)) 77958755Seric printf("makeconnection (%s [%s])\n", 78058755Seric host, anynet_ntoa(&addr)); 78152106Seric 78258588Seric /* save for logging */ 78358588Seric CurHostAddr = addr; 78458588Seric 78557736Seric if (usesecureport) 78657736Seric { 78757736Seric int rport = IPPORT_RESERVED - 1; 7886039Seric 78957736Seric s = rresvport(&rport); 79057736Seric } 79157736Seric else 79257736Seric { 79357736Seric s = socket(AF_INET, SOCK_STREAM, 0); 79457736Seric } 79557736Seric if (s < 0) 79657736Seric { 79757736Seric sav_errno = errno; 79857736Seric syserr("makeconnection: no socket"); 79957736Seric goto failure; 80057736Seric } 80110347Seric 80264381Seric #ifdef SO_SNDBUF 80364381Seric if (TcpSndBufferSize > 0) 80464381Seric { 80564381Seric if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, 80664561Seric (char *) &TcpSndBufferSize, 80764381Seric sizeof(TcpSndBufferSize)) < 0) 80864381Seric syserr("makeconnection: setsockopt(SO_SNDBUF)"); 80964381Seric } 81064381Seric #endif 81164381Seric 81257736Seric if (tTd(16, 1)) 81357736Seric printf("makeconnection: fd=%d\n", s); 81457736Seric 81557736Seric /* turn on network debugging? */ 81657736Seric if (tTd(16, 101)) 81757736Seric { 81857736Seric int on = 1; 81966861Seric (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 82057736Seric (char *)&on, sizeof on); 82157736Seric } 82257736Seric if (CurEnv->e_xfp != NULL) 82357736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 82457736Seric errno = 0; /* for debugging */ 82558755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 82657736Seric break; 82757736Seric 82857736Seric /* couldn't connect.... figure out why */ 82927744Sbloom sav_errno = errno; 83027744Sbloom (void) close(s); 83129430Sbloom if (hp && hp->h_addr_list[i]) 83229430Sbloom { 83357736Seric if (tTd(16, 1)) 83458755Seric printf("Connect failed (%s); trying new address....\n", 83558755Seric errstring(sav_errno)); 83658778Seric switch (addr.sa.sa_family) 83758778Seric { 83858778Seric #ifdef NETINET 83958778Seric case AF_INET: 84058755Seric bcopy(hp->h_addr_list[i++], 84158778Seric &addr.sin.sin_addr, 842*67421Seric INADDRSZ); 84358778Seric break; 84458778Seric #endif 84558778Seric 84658778Seric default: 84758755Seric bcopy(hp->h_addr_list[i++], 84858778Seric addr.sa.sa_data, 84952106Seric hp->h_length); 85058778Seric break; 85158778Seric } 85257736Seric continue; 85329430Sbloom } 85429430Sbloom 8556039Seric /* failure, decide if temporary or not */ 8566039Seric failure: 85759254Seric #ifdef XLA 85859254Seric xla_host_end(host); 85959254Seric #endif 86058542Seric if (transienterror(sav_errno)) 86158542Seric return EX_TEMPFAIL; 86258542Seric else 86358542Seric { 86458542Seric message("%s", errstring(sav_errno)); 86558542Seric return (EX_UNAVAILABLE); 8666039Seric } 8676039Seric } 8686039Seric 8696039Seric /* connection ok, put it into canonical form */ 87064724Seric if ((mci->mci_out = fdopen(s, "w")) == NULL || 87164724Seric (s = dup(s)) < 0 || 87264725Seric (mci->mci_in = fdopen(s, "r")) == NULL) 87364724Seric { 87464724Seric syserr("cannot open SMTP client channel, fd=%d", s); 87564724Seric return EX_TEMPFAIL; 87664724Seric } 8776039Seric 87810098Seric return (EX_OK); 8796039Seric } 88010758Seric /* 88110758Seric ** MYHOSTNAME -- return the name of this host. 88210758Seric ** 88310758Seric ** Parameters: 88410758Seric ** hostbuf -- a place to return the name of this host. 88512313Seric ** size -- the size of hostbuf. 88610758Seric ** 88710758Seric ** Returns: 88810758Seric ** A list of aliases for this host. 88910758Seric ** 89010758Seric ** Side Effects: 89164338Seric ** Adds numeric codes to $=w. 89210758Seric */ 8936039Seric 89467140Seric struct hostent * 89512313Seric myhostname(hostbuf, size) 89610758Seric char hostbuf[]; 89712313Seric int size; 89810758Seric { 89958110Seric register struct hostent *hp; 90010758Seric extern struct hostent *gethostbyname(); 90110758Seric 90223120Seric if (gethostname(hostbuf, size) < 0) 90323120Seric { 90423120Seric (void) strcpy(hostbuf, "localhost"); 90523120Seric } 90611147Seric hp = gethostbyname(hostbuf); 90766853Seric if (hp == NULL) 90816877Seric { 90966853Seric syserr("!My host name (%s) does not seem to exist!", hostbuf); 91066853Seric } 91166853Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 91266853Seric hostbuf[size - 1] = '\0'; 91366853Seric 91466853Seric #if NAMED_BIND 91566853Seric /* if still no dot, try DNS directly (i.e., avoid NIS problems) */ 91666853Seric if (strchr(hostbuf, '.') == NULL) 91766853Seric { 91866853Seric extern bool getcanonname(); 91966853Seric extern int h_errno; 92066853Seric 92166853Seric /* try twice in case name server not yet started up */ 92266853Seric if (!getcanonname(hostbuf, size, TRUE) && 92366853Seric UseNameServer && 92466853Seric (h_errno != TRY_AGAIN || 92566853Seric (sleep(30), !getcanonname(hostbuf, size, TRUE)))) 92666777Seric { 92766853Seric errno = h_errno + E_DNSBASE; 92866853Seric syserr("!My host name (%s) not known to DNS", 92966853Seric hostbuf); 93066777Seric } 93166853Seric } 93266777Seric #endif 93367140Seric return (hp); 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; 115867419Seric struct in_addr 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'; 128167419Seric in_addr.s_addr = inet_addr(&name[1]); 128258110Seric 128358110Seric /* nope -- ask the name server */ 1284*67421Seric hp = gethostbyaddr((char *)&in_addr, INADDRSZ, 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, 1393*67421Seric INADDRSZ, 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