122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 642825Sbostic * %sccs.include.redist.c% 733780Sbostic */ 822700Sdist 933932Sbostic #include <errno.h> 1058153Seric #include <signal.h> 1140962Sbostic #include "sendmail.h" 124535Seric 1333780Sbostic #ifndef lint 1433780Sbostic #ifdef DAEMON 15*59156Seric static char sccsid[] = "@(#)daemon.c 6.40 (Berkeley) 04/18/93 (with daemon mode)"; 1633780Sbostic #else 17*59156Seric static char sccsid[] = "@(#)daemon.c 6.40 (Berkeley) 04/18/93 (without daemon mode)"; 1833780Sbostic #endif 1933780Sbostic #endif /* not lint */ 204535Seric 2133780Sbostic #ifdef DAEMON 2233780Sbostic 2323120Seric # include <netdb.h> 2423120Seric # include <sys/wait.h> 2523120Seric # include <sys/time.h> 265978Seric 2759042Seric #ifdef NAMED_BIND 2859042Seric # include <arpa/nameser.h> 2959042Seric # include <resolv.h> 3059042Seric #endif 3159042Seric 324535Seric /* 334535Seric ** DAEMON.C -- routines to use when running as a daemon. 347556Seric ** 357556Seric ** This entire file is highly dependent on the 4.2 BSD 367556Seric ** interprocess communication primitives. No attempt has 377556Seric ** been made to make this file portable to Version 7, 387556Seric ** Version 6, MPX files, etc. If you should try such a 397556Seric ** thing yourself, I recommend chucking the entire file 407556Seric ** and starting from scratch. Basic semantics are: 417556Seric ** 427556Seric ** getrequests() 437556Seric ** Opens a port and initiates a connection. 447556Seric ** Returns in a child. Must set InChannel and 457556Seric ** OutChannel appropriately. 4610206Seric ** clrdaemon() 4710206Seric ** Close any open files associated with getting 4810206Seric ** the connection; this is used when running the queue, 4910206Seric ** etc., to avoid having extra file descriptors during 5010206Seric ** the queue run and to avoid confusing the network 5110206Seric ** code (if it cares). 5252106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 537556Seric ** Make a connection to the named host on the given 547556Seric ** port. Set *outfile and *infile to the files 557556Seric ** appropriate for communication. Returns zero on 567556Seric ** success, else an exit status describing the 577556Seric ** error. 5856823Seric ** maphostname(map, hbuf, hbufsiz, avp) 5956823Seric ** Convert the entry in hbuf into a canonical form. 604535Seric */ 6158755Seric 6258755Seric extern char *anynet_ntoa(); 634535Seric /* 644535Seric ** GETREQUESTS -- open mail IPC port and get requests. 654535Seric ** 664535Seric ** Parameters: 674535Seric ** none. 684535Seric ** 694535Seric ** Returns: 704535Seric ** none. 714535Seric ** 724535Seric ** Side Effects: 734535Seric ** Waits until some interesting activity occurs. When 744535Seric ** it does, a child is created to process it, and the 754535Seric ** parent waits for completion. Return from this 769886Seric ** routine is always in the child. The file pointers 779886Seric ** "InChannel" and "OutChannel" should be set to point 789886Seric ** to the communication channel. 794535Seric */ 804535Seric 8158849Seric int DaemonSocket = -1; /* fd describing socket */ 8258849Seric SOCKADDR DaemonAddr; /* socket for incoming */ 8316144Seric 844535Seric getrequests() 854535Seric { 869610Seric int t; 879610Seric register struct servent *sp; 8825027Seric int on = 1; 8953751Seric bool refusingconnections = TRUE; 9058419Seric FILE *pidf; 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 { 10358849Seric sp = getservbyname("smtp", "tcp"); 10458849Seric if (sp == NULL) 10558849Seric { 10658909Seric syserr("554 service \"smtp\" unknown"); 10758849Seric goto severe; 10858849Seric } 10958849Seric DaemonAddr.sin.sin_port = sp->s_port; 1109610Seric } 1119610Seric 1129610Seric /* 1139610Seric ** Try to actually open the connection. 1149610Seric */ 1159610Seric 1169610Seric if (tTd(15, 1)) 11758849Seric printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port); 1189610Seric 1199610Seric /* get a socket for the SMTP connection */ 12059041Seric DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0); 12110206Seric if (DaemonSocket < 0) 1229610Seric { 1239610Seric /* probably another daemon already */ 1249610Seric syserr("getrequests: can't create socket"); 1259610Seric severe: 1269610Seric # ifdef LOG 1279610Seric if (LogLevel > 0) 12857663Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 12956795Seric # endif /* LOG */ 1309610Seric finis(); 1319610Seric } 13210347Seric 13310347Seric /* turn on network debugging? */ 13456328Seric if (tTd(15, 101)) 13524945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13610347Seric 13725027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 13825027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 13925027Seric 14059041Seric switch (DaemonAddr.sa.sa_family) 1419610Seric { 14259041Seric # ifdef NETINET 14359041Seric case AF_INET: 14459041Seric t = sizeof DaemonAddr.sin; 14559041Seric break; 14659041Seric # endif 14759041Seric 14859041Seric # ifdef NETISO 14959041Seric case AF_ISO: 15059041Seric t = sizeof DaemonAddr.siso; 15159041Seric break; 15259041Seric # endif 15359041Seric 15459041Seric default: 15559041Seric t = sizeof DaemonAddr; 15659041Seric break; 15759041Seric } 15859041Seric 15959041Seric if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0) 16059041Seric { 1619610Seric syserr("getrequests: cannot bind"); 16210206Seric (void) close(DaemonSocket); 1639610Seric goto severe; 1649610Seric } 1659610Seric 16624955Seric (void) signal(SIGCHLD, reapchild); 16724945Seric 16858419Seric /* write the pid to the log file for posterity */ 16958419Seric pidf = fopen(PidFile, "w"); 17058419Seric if (pidf != NULL) 17158419Seric { 17258419Seric fprintf(pidf, "%d\n", getpid()); 17358419Seric fclose(pidf); 17458419Seric } 17558419Seric 17658419Seric 1779610Seric if (tTd(15, 1)) 17810206Seric printf("getrequests: %d\n", DaemonSocket); 1799610Seric 1804631Seric for (;;) 1814631Seric { 18214875Seric register int pid; 18311147Seric auto int lotherend; 18453751Seric extern bool refuseconnections(); 18511147Seric 18614875Seric /* see if we are rejecting connections */ 18753751Seric CurrentLA = getla(); 18853751Seric if (refuseconnections()) 18936584Sbostic { 19053751Seric if (!refusingconnections) 19153751Seric { 19253751Seric /* don't queue so peer will fail quickly */ 19353751Seric (void) listen(DaemonSocket, 0); 19453751Seric refusingconnections = TRUE; 19553751Seric } 19657385Seric setproctitle("rejecting connections: load average: %d", 19757385Seric CurrentLA); 19814875Seric sleep(5); 19953751Seric continue; 20036584Sbostic } 20114875Seric 20253751Seric if (refusingconnections) 20353751Seric { 20453751Seric /* start listening again */ 20553751Seric if (listen(DaemonSocket, 10) < 0) 20653751Seric { 20753751Seric syserr("getrequests: cannot listen"); 20853751Seric (void) close(DaemonSocket); 20953751Seric goto severe; 21053751Seric } 21153751Seric setproctitle("accepting connections"); 21253751Seric refusingconnections = FALSE; 21353751Seric } 21453751Seric 2159610Seric /* wait for a connection */ 2169610Seric do 2179610Seric { 2189610Seric errno = 0; 21936230Skarels lotherend = sizeof RealHostAddr; 22046928Sbostic t = accept(DaemonSocket, 22146928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 2229610Seric } while (t < 0 && errno == EINTR); 2239610Seric if (t < 0) 2245978Seric { 2259610Seric syserr("getrequests: accept"); 2269610Seric sleep(5); 2279610Seric continue; 2285978Seric } 2294631Seric 2305978Seric /* 2315978Seric ** Create a subprocess to process the mail. 2325978Seric */ 2335978Seric 2347677Seric if (tTd(15, 2)) 2359610Seric printf("getrequests: forking (fd = %d)\n", t); 2365978Seric 2374636Seric pid = fork(); 2384636Seric if (pid < 0) 2394631Seric { 2404636Seric syserr("daemon: cannot fork"); 2414636Seric sleep(10); 2429610Seric (void) close(t); 2434636Seric continue; 2444631Seric } 2454631Seric 2464636Seric if (pid == 0) 2474631Seric { 24858951Seric extern char *hostnamebyanyaddr(); 24911147Seric 2504636Seric /* 2514636Seric ** CHILD -- return to caller. 25211147Seric ** Collect verified idea of sending host. 2534636Seric ** Verify calling user id if possible here. 2544636Seric */ 2554631Seric 25624955Seric (void) signal(SIGCHLD, SIG_DFL); 257*59156Seric OpMode = MD_SMTP; 25824950Seric 25911147Seric /* determine host name */ 26058951Seric RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr)); 26158778Seric 26255173Seric #ifdef LOG 26357977Seric if (LogLevel > 10) 26455173Seric { 26555173Seric /* log connection information */ 26655173Seric syslog(LOG_INFO, "connect from %s (%s)", 26758951Seric RealHostName, anynet_ntoa(&RealHostAddr)); 26855173Seric } 26955173Seric #endif 27055173Seric 27116884Seric /* should we check for illegal connection here? XXX */ 272*59156Seric #ifdef XLA 273*59156Seric if (!xla_host_ok(RealHostName)) 274*59156Seric { 275*59156Seric message("421 Too many sessions for this host"); 276*59156Seric exit(0); 277*59156Seric } 278*59156Seric #endif 27916884Seric 28010206Seric (void) close(DaemonSocket); 2819610Seric InChannel = fdopen(t, "r"); 28221062Seric OutChannel = fdopen(dup(t), "w"); 2837677Seric if (tTd(15, 2)) 2845978Seric printf("getreq: returning\n"); 2854636Seric return; 2864631Seric } 2874631Seric 2887117Seric /* close the port so that others will hang (for a while) */ 2899610Seric (void) close(t); 2904631Seric } 2919886Seric /*NOTREACHED*/ 2924631Seric } 2935978Seric /* 29410206Seric ** CLRDAEMON -- reset the daemon connection 29510206Seric ** 29610206Seric ** Parameters: 29710206Seric ** none. 29810206Seric ** 29910206Seric ** Returns: 30010206Seric ** none. 30110206Seric ** 30210206Seric ** Side Effects: 30310206Seric ** releases any resources used by the passive daemon. 30410206Seric */ 30510206Seric 30610206Seric clrdaemon() 30710206Seric { 30810206Seric if (DaemonSocket >= 0) 30910206Seric (void) close(DaemonSocket); 31010206Seric DaemonSocket = -1; 31110206Seric } 31210206Seric /* 31358849Seric ** SETDAEMONOPTIONS -- set options for running the daemon 31458849Seric ** 31558849Seric ** Parameters: 31658849Seric ** p -- the options line. 31758849Seric ** 31858849Seric ** Returns: 31958849Seric ** none. 32058849Seric */ 32158849Seric 32258849Seric setdaemonoptions(p) 32358849Seric register char *p; 32458849Seric { 32558873Seric if (DaemonAddr.sa.sa_family == AF_UNSPEC) 32658873Seric DaemonAddr.sa.sa_family = AF_INET; 32758873Seric 32858849Seric while (p != NULL) 32958849Seric { 33058849Seric register char *f; 33158849Seric register char *v; 33258849Seric 33358849Seric while (isascii(*p) && isspace(*p)) 33458849Seric p++; 33558849Seric if (*p == '\0') 33658849Seric break; 33758849Seric f = p; 33858849Seric p = strchr(p, ','); 33958849Seric if (p != NULL) 34058849Seric *p++ = '\0'; 34158849Seric v = strchr(f, '='); 34258849Seric if (v == NULL) 34358849Seric continue; 34458849Seric while (isascii(*++v) && isspace(*v)) 34558849Seric continue; 34658849Seric 34758849Seric switch (*f) 34858849Seric { 34958873Seric case 'F': /* address family */ 35058849Seric if (isascii(*v) && isdigit(*v)) 35158873Seric DaemonAddr.sa.sa_family = atoi(v); 35258873Seric #ifdef NETINET 35358873Seric else if (strcasecmp(v, "inet") == 0) 35458873Seric DaemonAddr.sa.sa_family = AF_INET; 35558873Seric #endif 35658873Seric #ifdef NETISO 35758873Seric else if (strcasecmp(v, "iso") == 0) 35858873Seric DaemonAddr.sa.sa_family = AF_ISO; 35958873Seric #endif 36058873Seric #ifdef NETNS 36158873Seric else if (strcasecmp(v, "ns") == 0) 36258873Seric DaemonAddr.sa.sa_family = AF_NS; 36358873Seric #endif 36458873Seric #ifdef NETX25 36558873Seric else if (strcasecmp(v, "x.25") == 0) 36658873Seric DaemonAddr.sa.sa_family = AF_CCITT; 36758873Seric #endif 36858849Seric else 36958873Seric syserr("554 Unknown address family %s in Family=option", v); 37058873Seric break; 37158873Seric 37258873Seric case 'A': /* address */ 37358873Seric switch (DaemonAddr.sa.sa_family) 37458849Seric { 37558873Seric #ifdef NETINET 37658873Seric case AF_INET: 37758873Seric if (isascii(*v) && isdigit(*v)) 37858873Seric DaemonAddr.sin.sin_addr.s_addr = inet_network(v); 37958873Seric else 38058873Seric { 38158873Seric register struct netent *np; 38258849Seric 38358873Seric np = getnetbyname(v); 38458873Seric if (np == NULL) 38558873Seric syserr("554 network \"%s\" unknown", v); 38658873Seric else 38758873Seric DaemonAddr.sin.sin_addr.s_addr = np->n_net; 38858873Seric } 38958873Seric break; 39058873Seric #endif 39158873Seric 39258873Seric default: 39358873Seric syserr("554 Address= option unsupported for family %d", 39458873Seric DaemonAddr.sa.sa_family); 39558873Seric break; 39658849Seric } 39758849Seric break; 39858849Seric 39958873Seric case 'P': /* port */ 40058873Seric switch (DaemonAddr.sa.sa_family) 40158849Seric { 40258873Seric short port; 40358849Seric 40458873Seric #ifdef NETINET 40558873Seric case AF_INET: 40658873Seric if (isascii(*v) && isdigit(*v)) 40758873Seric DaemonAddr.sin.sin_port = atoi(v); 40858849Seric else 40958873Seric { 41058873Seric register struct servent *sp; 41158873Seric 41258873Seric sp = getservbyname(v, "tcp"); 41358873Seric if (sp == NULL) 41458909Seric syserr("554 service \"%s\" unknown", v); 41558873Seric else 41658873Seric DaemonAddr.sin.sin_port = sp->s_port; 41758873Seric } 41858873Seric break; 41958873Seric #endif 42058873Seric 42158873Seric #ifdef NETISO 42258873Seric case AF_ISO: 42358873Seric /* assume two byte transport selector */ 42458873Seric if (isascii(*v) && isdigit(*v)) 42558873Seric port = atoi(v); 42658873Seric else 42758873Seric { 42858873Seric register struct servent *sp; 42958873Seric 43058873Seric sp = getservbyname(v, "tcp"); 43158873Seric if (sp == NULL) 43258909Seric syserr("554 service \"%s\" unknown", v); 43358873Seric else 43458873Seric port = sp->s_port; 43558873Seric } 43658873Seric bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2); 43758873Seric break; 43858873Seric #endif 43958873Seric 44058873Seric default: 44158873Seric syserr("554 Port= option unsupported for family %d", 44258873Seric DaemonAddr.sa.sa_family); 44358873Seric break; 44458849Seric } 44558849Seric break; 44658849Seric } 44758849Seric } 44858849Seric } 44958849Seric /* 4506039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 4516039Seric ** 4526039Seric ** Parameters: 4536039Seric ** host -- the name of the host. 4546633Seric ** port -- the port number to connect to. 45553739Seric ** mci -- a pointer to the mail connection information 45653739Seric ** structure to be filled in. 45752106Seric ** usesecureport -- if set, use a low numbered (reserved) 45852106Seric ** port to provide some rudimentary authentication. 4596039Seric ** 4606039Seric ** Returns: 4616039Seric ** An exit code telling whether the connection could be 4626039Seric ** made and if not why not. 4636039Seric ** 4646039Seric ** Side Effects: 4656039Seric ** none. 4666039Seric */ 4675978Seric 46858755Seric SOCKADDR CurHostAddr; /* address of current host */ 46958305Seric 47054967Seric int 47153739Seric makeconnection(host, port, mci, usesecureport) 4726039Seric char *host; 4737286Seric u_short port; 47454967Seric register MCI *mci; 47552106Seric bool usesecureport; 4766039Seric { 47729430Sbloom register int i, s; 47829430Sbloom register struct hostent *hp = (struct hostent *)NULL; 47958755Seric SOCKADDR addr; 48052106Seric int sav_errno; 48158755Seric int addrlen; 48235651Seric #ifdef NAMED_BIND 48335651Seric extern int h_errno; 48435651Seric #endif 4856039Seric 4866039Seric /* 4876039Seric ** Set up the address for the mailer. 4889308Seric ** Accept "[a.b.c.d]" syntax for host name. 4896039Seric */ 4906039Seric 49135651Seric #ifdef NAMED_BIND 49225475Smiriam h_errno = 0; 49335651Seric #endif 49425475Smiriam errno = 0; 49558864Seric bzero(&CurHostAddr, sizeof CurHostAddr); 49658906Seric CurHostName = host; 49725475Smiriam 4989308Seric if (host[0] == '[') 4999308Seric { 50011147Seric long hid; 50156795Seric register char *p = strchr(host, ']'); 5029308Seric 50311147Seric if (p != NULL) 5049308Seric { 50511147Seric *p = '\0'; 50611147Seric hid = inet_addr(&host[1]); 50758360Seric if (hid == -1) 50858360Seric { 50958360Seric /* try it as a host name (avoid MX lookup) */ 51058360Seric hp = gethostbyname(&host[1]); 51158360Seric *p = ']'; 51258360Seric goto gothostent; 51358360Seric } 51411147Seric *p = ']'; 5159308Seric } 51658360Seric if (p == NULL) 5179308Seric { 51858151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 5199308Seric return (EX_NOHOST); 5209308Seric } 52158778Seric addr.sin.sin_family = AF_INET; 52258778Seric addr.sin.sin_addr.s_addr = hid; 5239308Seric } 5249610Seric else 5259610Seric { 52629430Sbloom hp = gethostbyname(host); 52758360Seric gothostent: 52825475Smiriam if (hp == NULL) 52924945Seric { 53035651Seric #ifdef NAMED_BIND 53125475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 53225475Smiriam return (EX_TEMPFAIL); 53325657Seric 53435651Seric /* if name server is specified, assume temp fail */ 53535651Seric if (errno == ECONNREFUSED && UseNameServer) 53635651Seric return (EX_TEMPFAIL); 53735651Seric #endif 53825475Smiriam return (EX_NOHOST); 53924945Seric } 54058778Seric addr.sa.sa_family = hp->h_addrtype; 54158778Seric switch (hp->h_addrtype) 54258778Seric { 54358778Seric #ifdef NETINET 54458778Seric case AF_INET: 54558755Seric bcopy(hp->h_addr, 54658778Seric &addr.sin.sin_addr, 54758755Seric hp->h_length); 54858778Seric break; 54958778Seric #endif 55058778Seric 55158778Seric default: 55258755Seric bcopy(hp->h_addr, 55358778Seric addr.sa.sa_data, 55458755Seric hp->h_length); 55558778Seric break; 55658778Seric } 55729430Sbloom i = 1; 5589610Seric } 5599610Seric 5609610Seric /* 5619610Seric ** Determine the port number. 5629610Seric */ 5639610Seric 56410011Seric if (port != 0) 56558755Seric port = htons(port); 56610011Seric else 5679610Seric { 5689610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 5699610Seric 5709610Seric if (sp == NULL) 5719610Seric { 57258909Seric syserr("554 makeconnection: service \"smtp\" unknown"); 57357977Seric return (EX_OSERR); 5749610Seric } 57558755Seric port = sp->s_port; 5769610Seric } 5776039Seric 57858778Seric switch (addr.sa.sa_family) 57958755Seric { 58058755Seric case AF_INET: 58158778Seric addr.sin.sin_port = port; 58258755Seric addrlen = sizeof (struct sockaddr_in); 58358755Seric break; 58458755Seric 58558755Seric #ifdef NETISO 58658755Seric case AF_ISO: 58758755Seric /* assume two byte transport selector */ 58858755Seric bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2); 58958755Seric addrlen = sizeof (struct sockaddr_iso); 59058755Seric break; 59158755Seric #endif 59258755Seric 59358755Seric default: 59458778Seric syserr("Can't connect to address family %d", addr.sa.sa_family); 59558755Seric return (EX_NOHOST); 59658755Seric } 59758755Seric 5986039Seric /* 5996039Seric ** Try to actually open the connection. 6006039Seric */ 6016039Seric 602*59156Seric #ifdef XLA 603*59156Seric /* if too many connections, don't bother trying */ 604*59156Seric if (!xla_noqueue_ok(host)) 605*59156Seric return EX_TEMPFAIL; 606*59156Seric #endif 607*59156Seric 60857736Seric for (;;) 60952106Seric { 61057736Seric if (tTd(16, 1)) 61158755Seric printf("makeconnection (%s [%s])\n", 61258755Seric host, anynet_ntoa(&addr)); 61352106Seric 61458588Seric /* save for logging */ 61558588Seric CurHostAddr = addr; 61658588Seric 61757736Seric if (usesecureport) 61857736Seric { 61957736Seric int rport = IPPORT_RESERVED - 1; 6206039Seric 62157736Seric s = rresvport(&rport); 62257736Seric } 62357736Seric else 62457736Seric { 62557736Seric s = socket(AF_INET, SOCK_STREAM, 0); 62657736Seric } 62757736Seric if (s < 0) 62857736Seric { 62957736Seric sav_errno = errno; 63057736Seric syserr("makeconnection: no socket"); 63157736Seric goto failure; 63257736Seric } 63310347Seric 63457736Seric if (tTd(16, 1)) 63557736Seric printf("makeconnection: fd=%d\n", s); 63657736Seric 63757736Seric /* turn on network debugging? */ 63857736Seric if (tTd(16, 101)) 63957736Seric { 64057736Seric int on = 1; 64157736Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 64257736Seric (char *)&on, sizeof on); 64357736Seric } 64457736Seric if (CurEnv->e_xfp != NULL) 64557736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 64657736Seric errno = 0; /* for debugging */ 64758755Seric if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0) 64857736Seric break; 64957736Seric 65057736Seric /* couldn't connect.... figure out why */ 65127744Sbloom sav_errno = errno; 65227744Sbloom (void) close(s); 65329430Sbloom if (hp && hp->h_addr_list[i]) 65429430Sbloom { 65558755Seric extern char *errstring(); 65658755Seric 65757736Seric if (tTd(16, 1)) 65858755Seric printf("Connect failed (%s); trying new address....\n", 65958755Seric errstring(sav_errno)); 66058778Seric switch (addr.sa.sa_family) 66158778Seric { 66258778Seric #ifdef NETINET 66358778Seric case AF_INET: 66458755Seric bcopy(hp->h_addr_list[i++], 66558778Seric &addr.sin.sin_addr, 66658755Seric hp->h_length); 66758778Seric break; 66858778Seric #endif 66958778Seric 67058778Seric default: 67158755Seric bcopy(hp->h_addr_list[i++], 67258778Seric addr.sa.sa_data, 67352106Seric hp->h_length); 67458778Seric break; 67558778Seric } 67657736Seric continue; 67729430Sbloom } 67829430Sbloom 6796039Seric /* failure, decide if temporary or not */ 6806039Seric failure: 68158542Seric if (transienterror(sav_errno)) 68258542Seric return EX_TEMPFAIL; 68358542Seric else 68458542Seric { 68558542Seric extern char *errstring(); 68611147Seric 68758542Seric message("%s", errstring(sav_errno)); 688*59156Seric #ifdef XLA 689*59156Seric xla_host_end(host); 690*59156Seric #endif 69158542Seric return (EX_UNAVAILABLE); 6926039Seric } 6936039Seric } 6946039Seric 6956039Seric /* connection ok, put it into canonical form */ 69653739Seric mci->mci_out = fdopen(s, "w"); 69753739Seric mci->mci_in = fdopen(dup(s), "r"); 6986039Seric 69910098Seric return (EX_OK); 7006039Seric } 70110758Seric /* 70210758Seric ** MYHOSTNAME -- return the name of this host. 70310758Seric ** 70410758Seric ** Parameters: 70510758Seric ** hostbuf -- a place to return the name of this host. 70612313Seric ** size -- the size of hostbuf. 70710758Seric ** 70810758Seric ** Returns: 70910758Seric ** A list of aliases for this host. 71010758Seric ** 71110758Seric ** Side Effects: 71258110Seric ** Sets the MyIpAddrs buffer to a list of my IP addresses. 71310758Seric */ 7146039Seric 71558110Seric struct in_addr MyIpAddrs[MAXIPADDR + 1]; 71658110Seric 71710758Seric char ** 71812313Seric myhostname(hostbuf, size) 71910758Seric char hostbuf[]; 72012313Seric int size; 72110758Seric { 72258110Seric register struct hostent *hp; 72310758Seric extern struct hostent *gethostbyname(); 72410758Seric 72523120Seric if (gethostname(hostbuf, size) < 0) 72623120Seric { 72723120Seric (void) strcpy(hostbuf, "localhost"); 72823120Seric } 72911147Seric hp = gethostbyname(hostbuf); 73011147Seric if (hp != NULL) 73116877Seric { 73258110Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 73358110Seric hostbuf[size - 1] = '\0'; 73458110Seric 73558110Seric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 73658110Seric { 73758110Seric register int i; 73858110Seric 73958110Seric for (i = 0; i < MAXIPADDR; i++) 74058110Seric { 74158110Seric if (hp->h_addr_list[i] == NULL) 74258110Seric break; 74358110Seric MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 74458110Seric } 74558110Seric MyIpAddrs[i].s_addr = 0; 74658110Seric } 74758110Seric 74811147Seric return (hp->h_aliases); 74916877Seric } 75010758Seric else 75110758Seric return (NULL); 75210758Seric } 75351315Seric /* 75458951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 75558308Seric ** 75658951Seric ** Uses RFC1413 protocol to try to get info from the other end. 75758951Seric ** 75858308Seric ** Parameters: 75958308Seric ** fd -- the descriptor 76058308Seric ** 76158308Seric ** Returns: 76258951Seric ** The user@host information associated with this descriptor. 76358308Seric ** 76458308Seric ** Side Effects: 76558951Seric ** Sets RealHostName to the name of the host at the other end. 76658308Seric */ 76758308Seric 76858951Seric #ifdef IDENTPROTO 76958951Seric 77058951Seric static jmp_buf CtxAuthTimeout; 77158951Seric 77258951Seric static 77358951Seric authtimeout() 77458951Seric { 77558951Seric longjmp(CtxAuthTimeout, 1); 77658951Seric } 77758951Seric 77858951Seric #endif 77958951Seric 78058308Seric char * 78158951Seric getauthinfo(fd) 78258308Seric int fd; 78358308Seric { 78458951Seric SOCKADDR fa; 78558951Seric int falen; 78659104Seric register char *p; 78758951Seric #ifdef IDENTPROTO 78858951Seric SOCKADDR la; 78958951Seric int lalen; 79058951Seric register struct servent *sp; 79158951Seric int s; 79258951Seric int i; 79358951Seric EVENT *ev; 79458951Seric #endif 79558951Seric static char hbuf[MAXNAME * 2 + 2]; 79658951Seric extern char *hostnamebyanyaddr(); 79758951Seric extern char RealUserName[]; /* main.c */ 79858308Seric 79958951Seric falen = sizeof fa; 80058951Seric if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0) 80158951Seric { 80258951Seric RealHostName = "localhost"; 80358951Seric (void) sprintf(hbuf, "%s@localhost", RealUserName); 80458957Seric if (tTd(9, 1)) 80558951Seric printf("getauthinfo: %s\n", hbuf); 80658951Seric return hbuf; 80758951Seric } 80858951Seric 80958951Seric RealHostName = newstr(hostnamebyanyaddr(&fa)); 81058951Seric RealHostAddr = fa; 81158951Seric 81258951Seric #ifdef IDENTPROTO 81358951Seric lalen = sizeof la; 81458951Seric if (fa.sa.sa_family != AF_INET || 81558951Seric getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 || 81658951Seric la.sa.sa_family != AF_INET) 81758951Seric { 81858951Seric /* no ident info */ 81958951Seric goto noident; 82058951Seric } 82158951Seric 82258951Seric /* create ident query */ 82358951Seric (void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port); 82458951Seric 82558951Seric /* create local address */ 82658951Seric bzero(&la, sizeof la); 82758951Seric 82858951Seric /* create foreign address */ 82958951Seric sp = getservbyname("auth", "tcp"); 83058951Seric if (sp != NULL) 83158951Seric fa.sin.sin_port = sp->s_port; 83258308Seric else 83359097Seric fa.sin.sin_port = htons(113); 83458951Seric 83558951Seric s = -1; 83658951Seric if (setjmp(CtxAuthTimeout) != 0) 83758951Seric { 83858951Seric if (s >= 0) 83958951Seric (void) close(s); 84058951Seric goto noident; 84158951Seric } 84258951Seric 84358951Seric /* put a timeout around the whole thing */ 84458951Seric ev = setevent((time_t) 30, authtimeout, 0); 84558951Seric 84658951Seric /* connect to foreign IDENT server */ 84758951Seric s = socket(AF_INET, SOCK_STREAM, 0); 84858951Seric if (s < 0) 84958951Seric { 85058951Seric clrevent(ev); 85158951Seric goto noident; 85258951Seric } 85358951Seric if (connect(s, &fa.sa, sizeof fa.sin) < 0) 85458951Seric { 85558951Seric closeident: 85658951Seric (void) close(s); 85758951Seric clrevent(ev); 85858951Seric goto noident; 85958951Seric } 86058951Seric 86158957Seric if (tTd(9, 10)) 86258951Seric printf("getauthinfo: sent %s", hbuf); 86358951Seric 86458951Seric /* send query */ 86558951Seric if (write(s, hbuf, strlen(hbuf)) < 0) 86658951Seric goto closeident; 86758951Seric 86858951Seric /* get result */ 86958951Seric i = read(s, hbuf, sizeof hbuf); 87058951Seric (void) close(s); 87158951Seric clrevent(ev); 87258951Seric if (i <= 0) 87358951Seric goto noident; 87458951Seric if (hbuf[--i] == '\n' && hbuf[--i] == '\r') 87558951Seric i--; 87658951Seric hbuf[++i] = '\0'; 87758951Seric 87858957Seric if (tTd(9, 3)) 87958951Seric printf("getauthinfo: got %s\n", hbuf); 88058951Seric 88158951Seric /* parse result */ 88258951Seric p = strchr(hbuf, ':'); 88358951Seric if (p == NULL) 88458951Seric { 88558951Seric /* malformed response */ 88658951Seric goto noident; 88758951Seric } 88858951Seric while (isascii(*++p) && isspace(*p)) 88958951Seric continue; 89058951Seric if (strncasecmp(p, "userid", 6) != 0) 89158951Seric { 89258951Seric /* presumably an error string */ 89358951Seric goto noident; 89458951Seric } 89558951Seric p += 6; 89658951Seric while (isascii(*p) && isspace(*p)) 89758951Seric p++; 89858951Seric if (*p++ != ':') 89958951Seric { 90058951Seric /* either useridxx or malformed response */ 90158951Seric goto noident; 90258951Seric } 90358951Seric 90458951Seric /* p now points to the OSTYPE field */ 90558951Seric p = strchr(p, ':'); 90658951Seric if (p == NULL) 90758951Seric { 90858951Seric /* malformed response */ 90958951Seric goto noident; 91058951Seric } 91158951Seric 91258957Seric /* 1413 says don't do this -- but it's broken otherwise */ 91358957Seric while (isascii(*++p) && isspace(*p)) 91458957Seric continue; 91558957Seric 91658951Seric /* p now points to the authenticated name */ 91758951Seric (void) sprintf(hbuf, "%s@%s", p, RealHostName); 91858957Seric goto finish; 91958957Seric 92058957Seric #endif /* IDENTPROTO */ 92158957Seric 92258957Seric noident: 92358957Seric (void) strcpy(hbuf, RealHostName); 92458957Seric 92558957Seric finish: 92658951Seric if (RealHostName[0] != '[') 92758951Seric { 92858951Seric p = &hbuf[strlen(hbuf)]; 92958951Seric (void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr)); 93058951Seric } 93158957Seric if (tTd(9, 1)) 93258951Seric printf("getauthinfo: %s\n", hbuf); 93358308Seric return hbuf; 93458308Seric } 93558308Seric /* 93653751Seric ** MAPHOSTNAME -- turn a hostname into canonical form 93753751Seric ** 93853751Seric ** Parameters: 93956823Seric ** map -- a pointer to this map (unused). 94053751Seric ** hbuf -- a buffer containing a hostname. 94153751Seric ** hbsize -- the size of hbuf. 94255019Seric ** avp -- unused -- for compatibility with other mapping 94355019Seric ** functions. 94459084Seric ** statp -- an exit status (out parameter) -- set to 94559084Seric ** EX_TEMPFAIL if the name server is unavailable. 94653751Seric ** 94753751Seric ** Returns: 94853751Seric ** The mapping, if found. 94953751Seric ** NULL if no mapping found. 95053751Seric ** 95153751Seric ** Side Effects: 95253751Seric ** Looks up the host specified in hbuf. If it is not 95353751Seric ** the canonical name for that host, return the canonical 95453751Seric ** name. 95553751Seric */ 95651315Seric 95753751Seric char * 95859084Seric maphostname(map, hbuf, hbsize, avp, statp) 95956823Seric MAP *map; 96016911Seric char *hbuf; 96116911Seric int hbsize; 96253751Seric char **avp; 96359084Seric int *statp; 96416911Seric { 96516911Seric register struct hostent *hp; 96633932Sbostic u_long in_addr; 96756823Seric char *cp; 96858110Seric int i; 96933932Sbostic struct hostent *gethostbyaddr(); 97016911Seric 97156836Seric /* allow room for null */ 97256823Seric hbsize--; 97353751Seric 97425574Smiriam /* 97533932Sbostic * If first character is a bracket, then it is an address 97633932Sbostic * lookup. Address is copied into a temporary buffer to 97733932Sbostic * strip the brackets and to preserve hbuf if address is 97833932Sbostic * unknown. 97933932Sbostic */ 98053751Seric 98151315Seric if (*hbuf != '[') 98253751Seric { 98355019Seric extern bool getcanonname(); 98455019Seric 98558798Seric if (tTd(9, 1)) 98658798Seric printf("maphostname(%s, %d) => ", hbuf, hbsize); 98758674Seric if (getcanonname(hbuf, hbsize)) 98858796Seric { 98958796Seric if (tTd(9, 1)) 99058796Seric printf("%s\n", hbuf); 99153751Seric return hbuf; 99258796Seric } 99353751Seric else 99458796Seric { 99559084Seric register struct hostent *hp; 99659084Seric extern int h_errno; 99759084Seric 99858796Seric if (tTd(9, 1)) 99959084Seric printf("FAIL (%d)\n", h_errno); 100059084Seric switch (h_errno) 100159084Seric { 100259084Seric case TRY_AGAIN: 100359084Seric *statp = EX_TEMPFAIL; 100459084Seric break; 100559084Seric 100659084Seric case HOST_NOT_FOUND: 100759084Seric *statp = EX_NOHOST; 100859084Seric break; 100959084Seric 101059084Seric case NO_RECOVERY: 101159084Seric *statp = EX_SOFTWARE; 101259084Seric break; 101359084Seric 101459084Seric default: 101559084Seric *statp = EX_UNAVAILABLE; 101659084Seric break; 101759084Seric } 101859084Seric if (*statp != EX_TEMPFAIL || UseNameServer) 101959084Seric return NULL; 102059084Seric 102159084Seric /* 102259084Seric ** Try to look it up in /etc/hosts 102359084Seric */ 102459084Seric 102559084Seric hp = gethostbyname(hbuf); 102659084Seric if (hp == NULL) 102759084Seric { 102859084Seric /* no dice there either */ 102959084Seric *statp = EX_NOHOST; 103059084Seric return NULL; 103159084Seric } 103259084Seric 103359084Seric *statp = EX_OK; 103459084Seric return hp->h_name; 103558796Seric } 103653751Seric } 103756823Seric if ((cp = strchr(hbuf, ']')) == NULL) 103853751Seric return (NULL); 103940994Sbostic *cp = '\0'; 104056823Seric in_addr = inet_addr(&hbuf[1]); 104158110Seric 104258110Seric /* check to see if this is one of our addresses */ 104358110Seric for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 104458110Seric { 104558110Seric if (MyIpAddrs[i].s_addr == in_addr) 104658110Seric { 104758110Seric strncpy(hbuf, MyHostName, hbsize); 104858110Seric hbuf[hbsize] = '\0'; 104958110Seric return hbuf; 105058110Seric } 105158110Seric } 105258110Seric 105358110Seric /* nope -- ask the name server */ 105433932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 105533932Sbostic if (hp == NULL) 105653751Seric return (NULL); 105753751Seric 105858110Seric /* found a match -- copy out */ 105956823Seric if (strlen(hp->h_name) > hbsize) 106056823Seric hp->h_name[hbsize] = '\0'; 106153751Seric (void) strcpy(hbuf, hp->h_name); 106253751Seric return hbuf; 106333932Sbostic } 106458755Seric /* 106558755Seric ** ANYNET_NTOA -- convert a network address to printable form. 106658755Seric ** 106758755Seric ** Parameters: 106858755Seric ** sap -- a pointer to a sockaddr structure. 106958755Seric ** 107058755Seric ** Returns: 107158755Seric ** A printable version of that sockaddr. 107258755Seric */ 107316911Seric 107458755Seric char * 107558755Seric anynet_ntoa(sap) 107658755Seric register SOCKADDR *sap; 107758755Seric { 107858755Seric register char *bp; 107958755Seric register char *ap; 108058755Seric int l; 108158755Seric static char buf[80]; 108258755Seric 108358798Seric /* check for null/zero family */ 108458798Seric if (sap == NULL) 108558798Seric return "NULLADDR"; 108658798Seric if (sap->sa.sa_family == 0) 108758798Seric return "0"; 108858798Seric 108958778Seric #ifdef NETINET 109058778Seric if (sap->sa.sa_family == AF_INET) 109158755Seric { 109258755Seric extern char *inet_ntoa(); 109358755Seric 109458755Seric return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr); 109558755Seric } 109658778Seric #endif 109758755Seric 109858755Seric /* unknown family -- just dump bytes */ 109958778Seric (void) sprintf(buf, "Family %d: ", sap->sa.sa_family); 110058755Seric bp = &buf[strlen(buf)]; 110158778Seric ap = sap->sa.sa_data; 110258778Seric for (l = sizeof sap->sa.sa_data; --l >= 0; ) 110358755Seric { 110458755Seric (void) sprintf(bp, "%02x:", *ap++ & 0377); 110558755Seric bp += 3; 110658755Seric } 110758755Seric *--bp = '\0'; 110858755Seric return buf; 110958755Seric } 111058951Seric /* 111158951Seric ** HOSTNAMEBYANYADDR -- return name of host based on address 111258951Seric ** 111358951Seric ** Parameters: 111458951Seric ** sap -- SOCKADDR pointer 111558951Seric ** 111658951Seric ** Returns: 111758951Seric ** text representation of host name. 111858951Seric ** 111958951Seric ** Side Effects: 112058951Seric ** none. 112158951Seric */ 112258755Seric 112358951Seric char * 112458951Seric hostnamebyanyaddr(sap) 112558951Seric register SOCKADDR *sap; 112658951Seric { 112758951Seric register struct hostent *hp; 112858951Seric 112959042Seric #ifdef NAMED_BIND 113059042Seric int saveretry; 113159042Seric 113259042Seric /* shorten name server timeout to avoid higher level timeouts */ 113359042Seric saveretry = _res.retry; 113459042Seric _res.retry = 3; 113559042Seric #endif /* NAMED_BIND */ 113659042Seric 113758951Seric switch (sap->sa.sa_family) 113858951Seric { 113958951Seric #ifdef NETINET 114058951Seric case AF_INET: 114158951Seric hp = gethostbyaddr((char *) &sap->sin.sin_addr, 114258951Seric sizeof sap->sin.sin_addr, 114358951Seric AF_INET); 114458951Seric break; 114558951Seric #endif 114658951Seric 114758951Seric #ifdef NETISO 114858951Seric case AF_ISO: 114958951Seric hp = gethostbyaddr((char *) &sap->siso.siso_addr, 115058951Seric sizeof sap->siso.siso_addr, 115158951Seric AF_ISO); 115258951Seric break; 115358951Seric #endif 115458951Seric 115558951Seric default: 115658951Seric hp = gethostbyaddr(sap->sa.sa_data, 115758951Seric sizeof sap->sa.sa_data, 115858951Seric sap->sa.sa_family); 115958951Seric break; 116058951Seric } 116158951Seric 116259042Seric #ifdef NAMED_BIND 116359042Seric _res.retry = saveretry; 116459042Seric #endif /* NAMED_BIND */ 116559042Seric 116658951Seric if (hp != NULL) 116758951Seric return hp->h_name; 116858951Seric else 116958951Seric { 117058951Seric /* produce a dotted quad */ 117158951Seric static char buf[512]; 117258951Seric 117358951Seric (void) sprintf(buf, "[%s]", anynet_ntoa(sap)); 117458951Seric return buf; 117558951Seric } 117658951Seric } 117758951Seric 117856795Seric # else /* DAEMON */ 117916911Seric /* code for systems without sophisticated networking */ 118010758Seric 118110758Seric /* 118210758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 118311297Seric ** 118411297Seric ** Can't convert to upper case here because might be a UUCP name. 118512313Seric ** 118612313Seric ** Mark, you can change this to be anything you want...... 118710758Seric */ 118810758Seric 118910758Seric char ** 119012313Seric myhostname(hostbuf, size) 119110758Seric char hostbuf[]; 119212313Seric int size; 119310758Seric { 119410758Seric register FILE *f; 119510758Seric 119610758Seric hostbuf[0] = '\0'; 119710758Seric f = fopen("/usr/include/whoami", "r"); 119810758Seric if (f != NULL) 119910758Seric { 120012313Seric (void) fgets(hostbuf, size, f); 120110758Seric fixcrlf(hostbuf, TRUE); 120210758Seric (void) fclose(f); 120310758Seric } 120410758Seric return (NULL); 120510758Seric } 120616911Seric /* 120758951Seric ** GETAUTHINFO -- get the real host name asociated with a file descriptor 120858308Seric ** 120958308Seric ** Parameters: 121058308Seric ** fd -- the descriptor 121158308Seric ** 121258308Seric ** Returns: 121358308Seric ** The host name associated with this descriptor, if it can 121458308Seric ** be determined. 121558308Seric ** NULL otherwise. 121658308Seric ** 121758308Seric ** Side Effects: 121858308Seric ** none 121958308Seric */ 122058308Seric 122158308Seric char * 122258951Seric getauthinfo(fd) 122358308Seric int fd; 122458308Seric { 122558308Seric return NULL; 122658308Seric } 122758308Seric /* 122816911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 122916911Seric ** 123016911Seric ** Parameters: 123156823Seric ** map -- a pointer to the database map. 123216911Seric ** hbuf -- a buffer containing a hostname. 123359084Seric ** hbsize -- size of hbuf. 123453751Seric ** avp -- a pointer to a (cf file defined) argument vector. 123559084Seric ** statp -- an exit status (out parameter). 123616911Seric ** 123716911Seric ** Returns: 123853751Seric ** mapped host name 123951315Seric ** FALSE otherwise. 124016911Seric ** 124116911Seric ** Side Effects: 124216911Seric ** Looks up the host specified in hbuf. If it is not 124316911Seric ** the canonical name for that host, replace it with 124416911Seric ** the canonical name. If the name is unknown, or it 124516911Seric ** is already the canonical name, leave it unchanged. 124616911Seric */ 124710758Seric 124816911Seric /*ARGSUSED*/ 124953751Seric char * 125059084Seric maphostname(map, hbuf, hbsize, avp, statp) 125156823Seric MAP *map; 125216911Seric char *hbuf; 125316911Seric int hbsize; 125453751Seric char **avp; 125559084Seric char *statp; 125616911Seric { 125759084Seric register struct hostent *hp; 125859084Seric 125959084Seric hp = gethostbyname(hbuf); 126059084Seric if (hp != NULL) 126159084Seric return hp->h_name; 126259084Seric *statp = EX_NOHOST; 126353751Seric return NULL; 126416911Seric } 126516911Seric 126656795Seric #endif /* DAEMON */ 1267