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*58419Seric static char sccsid[] = "@(#)daemon.c 6.13 (Berkeley) 03/03/93 (with daemon mode)"; 1633780Sbostic #else 17*58419Seric static char sccsid[] = "@(#)daemon.c 6.13 (Berkeley) 03/03/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 274535Seric /* 284535Seric ** DAEMON.C -- routines to use when running as a daemon. 297556Seric ** 307556Seric ** This entire file is highly dependent on the 4.2 BSD 317556Seric ** interprocess communication primitives. No attempt has 327556Seric ** been made to make this file portable to Version 7, 337556Seric ** Version 6, MPX files, etc. If you should try such a 347556Seric ** thing yourself, I recommend chucking the entire file 357556Seric ** and starting from scratch. Basic semantics are: 367556Seric ** 377556Seric ** getrequests() 387556Seric ** Opens a port and initiates a connection. 397556Seric ** Returns in a child. Must set InChannel and 407556Seric ** OutChannel appropriately. 4110206Seric ** clrdaemon() 4210206Seric ** Close any open files associated with getting 4310206Seric ** the connection; this is used when running the queue, 4410206Seric ** etc., to avoid having extra file descriptors during 4510206Seric ** the queue run and to avoid confusing the network 4610206Seric ** code (if it cares). 4752106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 487556Seric ** Make a connection to the named host on the given 497556Seric ** port. Set *outfile and *infile to the files 507556Seric ** appropriate for communication. Returns zero on 517556Seric ** success, else an exit status describing the 527556Seric ** error. 5356823Seric ** maphostname(map, hbuf, hbufsiz, avp) 5456823Seric ** Convert the entry in hbuf into a canonical form. 554535Seric */ 564535Seric /* 574535Seric ** GETREQUESTS -- open mail IPC port and get requests. 584535Seric ** 594535Seric ** Parameters: 604535Seric ** none. 614535Seric ** 624535Seric ** Returns: 634535Seric ** none. 644535Seric ** 654535Seric ** Side Effects: 664535Seric ** Waits until some interesting activity occurs. When 674535Seric ** it does, a child is created to process it, and the 684535Seric ** parent waits for completion. Return from this 699886Seric ** routine is always in the child. The file pointers 709886Seric ** "InChannel" and "OutChannel" should be set to point 719886Seric ** to the communication channel. 724535Seric */ 734535Seric 7416144Seric int DaemonSocket = -1; /* fd describing socket */ 7516144Seric 764535Seric getrequests() 774535Seric { 789610Seric int t; 799610Seric register struct servent *sp; 8025027Seric int on = 1; 8153751Seric bool refusingconnections = TRUE; 82*58419Seric FILE *pidf; 8352106Seric struct sockaddr_in srvraddr; 8446928Sbostic extern void reapchild(); 857117Seric 869610Seric /* 879610Seric ** Set up the address for the mailer. 889610Seric */ 899610Seric 909610Seric sp = getservbyname("smtp", "tcp"); 919610Seric if (sp == NULL) 929610Seric { 9358151Seric syserr("554 server \"smtp\" unknown"); 9410167Seric goto severe; 959610Seric } 9652106Seric srvraddr.sin_family = AF_INET; 9752106Seric srvraddr.sin_addr.s_addr = INADDR_ANY; 9852106Seric srvraddr.sin_port = sp->s_port; 999610Seric 1009610Seric /* 1019610Seric ** Try to actually open the connection. 1029610Seric */ 1039610Seric 1049610Seric if (tTd(15, 1)) 10552106Seric printf("getrequests: port 0x%x\n", srvraddr.sin_port); 1069610Seric 1079610Seric /* get a socket for the SMTP connection */ 10823120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 10910206Seric if (DaemonSocket < 0) 1109610Seric { 1119610Seric /* probably another daemon already */ 1129610Seric syserr("getrequests: can't create socket"); 1139610Seric severe: 1149610Seric # ifdef LOG 1159610Seric if (LogLevel > 0) 11657663Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 11756795Seric # endif /* LOG */ 1189610Seric finis(); 1199610Seric } 12010347Seric 12110347Seric /* turn on network debugging? */ 12256328Seric if (tTd(15, 101)) 12324945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12410347Seric 12525027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 12625027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 12725027Seric 12852106Seric if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0) 1299610Seric { 1309610Seric syserr("getrequests: cannot bind"); 13110206Seric (void) close(DaemonSocket); 1329610Seric goto severe; 1339610Seric } 1349610Seric 13524955Seric (void) signal(SIGCHLD, reapchild); 13624945Seric 137*58419Seric /* write the pid to the log file for posterity */ 138*58419Seric pidf = fopen(PidFile, "w"); 139*58419Seric if (pidf != NULL) 140*58419Seric { 141*58419Seric fprintf(pidf, "%d\n", getpid()); 142*58419Seric fclose(pidf); 143*58419Seric } 144*58419Seric 145*58419Seric 1469610Seric if (tTd(15, 1)) 14710206Seric printf("getrequests: %d\n", DaemonSocket); 1489610Seric 1494631Seric for (;;) 1504631Seric { 15114875Seric register int pid; 15211147Seric auto int lotherend; 15353751Seric extern bool refuseconnections(); 15411147Seric 15514875Seric /* see if we are rejecting connections */ 15653751Seric CurrentLA = getla(); 15753751Seric if (refuseconnections()) 15836584Sbostic { 15953751Seric if (!refusingconnections) 16053751Seric { 16153751Seric /* don't queue so peer will fail quickly */ 16253751Seric (void) listen(DaemonSocket, 0); 16353751Seric refusingconnections = TRUE; 16453751Seric } 16557385Seric setproctitle("rejecting connections: load average: %d", 16657385Seric CurrentLA); 16714875Seric sleep(5); 16853751Seric continue; 16936584Sbostic } 17014875Seric 17153751Seric if (refusingconnections) 17253751Seric { 17353751Seric /* start listening again */ 17453751Seric if (listen(DaemonSocket, 10) < 0) 17553751Seric { 17653751Seric syserr("getrequests: cannot listen"); 17753751Seric (void) close(DaemonSocket); 17853751Seric goto severe; 17953751Seric } 18053751Seric setproctitle("accepting connections"); 18153751Seric refusingconnections = FALSE; 18253751Seric } 18353751Seric 1849610Seric /* wait for a connection */ 1859610Seric do 1869610Seric { 1879610Seric errno = 0; 18836230Skarels lotherend = sizeof RealHostAddr; 18946928Sbostic t = accept(DaemonSocket, 19046928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1919610Seric } while (t < 0 && errno == EINTR); 1929610Seric if (t < 0) 1935978Seric { 1949610Seric syserr("getrequests: accept"); 1959610Seric sleep(5); 1969610Seric continue; 1975978Seric } 1984631Seric 1995978Seric /* 2005978Seric ** Create a subprocess to process the mail. 2015978Seric */ 2025978Seric 2037677Seric if (tTd(15, 2)) 2049610Seric printf("getrequests: forking (fd = %d)\n", t); 2055978Seric 2064636Seric pid = fork(); 2074636Seric if (pid < 0) 2084631Seric { 2094636Seric syserr("daemon: cannot fork"); 2104636Seric sleep(10); 2119610Seric (void) close(t); 2124636Seric continue; 2134631Seric } 2144631Seric 2154636Seric if (pid == 0) 2164631Seric { 21711147Seric extern struct hostent *gethostbyaddr(); 21811147Seric register struct hostent *hp; 21911147Seric char buf[MAXNAME]; 22057135Seric extern char *inet_ntoa(); 22111147Seric 2224636Seric /* 2234636Seric ** CHILD -- return to caller. 22411147Seric ** Collect verified idea of sending host. 2254636Seric ** Verify calling user id if possible here. 2264636Seric */ 2274631Seric 22824955Seric (void) signal(SIGCHLD, SIG_DFL); 22924950Seric 23011147Seric /* determine host name */ 23136230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 23211147Seric if (hp != NULL) 23323104Seric (void) strcpy(buf, hp->h_name); 23411147Seric else 23516884Seric { 23616884Seric /* produce a dotted quad */ 23716884Seric (void) sprintf(buf, "[%s]", 23836230Skarels inet_ntoa(RealHostAddr.sin_addr)); 23916884Seric } 24016884Seric 24155173Seric #ifdef LOG 24257977Seric if (LogLevel > 10) 24355173Seric { 24455173Seric /* log connection information */ 24555173Seric syslog(LOG_INFO, "connect from %s (%s)", 24655173Seric buf, inet_ntoa(RealHostAddr.sin_addr)); 24755173Seric } 24855173Seric #endif 24955173Seric 25016884Seric /* should we check for illegal connection here? XXX */ 25116884Seric 25211147Seric RealHostName = newstr(buf); 25311147Seric 25410206Seric (void) close(DaemonSocket); 2559610Seric InChannel = fdopen(t, "r"); 25621062Seric OutChannel = fdopen(dup(t), "w"); 2577677Seric if (tTd(15, 2)) 2585978Seric printf("getreq: returning\n"); 2597876Seric # ifdef LOG 2607876Seric if (LogLevel > 11) 2617876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 26256795Seric # endif /* LOG */ 2634636Seric return; 2644631Seric } 2654631Seric 2667117Seric /* close the port so that others will hang (for a while) */ 2679610Seric (void) close(t); 2684631Seric } 2699886Seric /*NOTREACHED*/ 2704631Seric } 2715978Seric /* 27210206Seric ** CLRDAEMON -- reset the daemon connection 27310206Seric ** 27410206Seric ** Parameters: 27510206Seric ** none. 27610206Seric ** 27710206Seric ** Returns: 27810206Seric ** none. 27910206Seric ** 28010206Seric ** Side Effects: 28110206Seric ** releases any resources used by the passive daemon. 28210206Seric */ 28310206Seric 28410206Seric clrdaemon() 28510206Seric { 28610206Seric if (DaemonSocket >= 0) 28710206Seric (void) close(DaemonSocket); 28810206Seric DaemonSocket = -1; 28910206Seric } 29010206Seric /* 2916039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2926039Seric ** 2936039Seric ** Parameters: 2946039Seric ** host -- the name of the host. 2956633Seric ** port -- the port number to connect to. 29653739Seric ** mci -- a pointer to the mail connection information 29753739Seric ** structure to be filled in. 29852106Seric ** usesecureport -- if set, use a low numbered (reserved) 29952106Seric ** port to provide some rudimentary authentication. 3006039Seric ** 3016039Seric ** Returns: 3026039Seric ** An exit code telling whether the connection could be 3036039Seric ** made and if not why not. 3046039Seric ** 3056039Seric ** Side Effects: 3066039Seric ** none. 3076039Seric */ 3085978Seric 30958305Seric struct sockaddr_in CurHostAddr; /* address of current host */ 31058305Seric 31154967Seric int 31253739Seric makeconnection(host, port, mci, usesecureport) 3136039Seric char *host; 3147286Seric u_short port; 31554967Seric register MCI *mci; 31652106Seric bool usesecureport; 3176039Seric { 31829430Sbloom register int i, s; 31929430Sbloom register struct hostent *hp = (struct hostent *)NULL; 32052106Seric struct sockaddr_in addr; 32152106Seric int sav_errno; 32229430Sbloom extern char *inet_ntoa(); 32335651Seric #ifdef NAMED_BIND 32435651Seric extern int h_errno; 32535651Seric #endif 3266039Seric 3276039Seric /* 3286039Seric ** Set up the address for the mailer. 3299308Seric ** Accept "[a.b.c.d]" syntax for host name. 3306039Seric */ 3316039Seric 33235651Seric #ifdef NAMED_BIND 33325475Smiriam h_errno = 0; 33435651Seric #endif 33525475Smiriam errno = 0; 33625475Smiriam 3379308Seric if (host[0] == '[') 3389308Seric { 33911147Seric long hid; 34056795Seric register char *p = strchr(host, ']'); 3419308Seric 34211147Seric if (p != NULL) 3439308Seric { 34411147Seric *p = '\0'; 34511147Seric hid = inet_addr(&host[1]); 34658360Seric if (hid == -1) 34758360Seric { 34858360Seric /* try it as a host name (avoid MX lookup) */ 34958360Seric hp = gethostbyname(&host[1]); 35058360Seric *p = ']'; 35158360Seric goto gothostent; 35258360Seric } 35311147Seric *p = ']'; 3549308Seric } 35558360Seric if (p == NULL) 3569308Seric { 35758151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 3589308Seric return (EX_NOHOST); 3599308Seric } 36052106Seric addr.sin_addr.s_addr = hid; 3619308Seric } 3629610Seric else 3639610Seric { 36429430Sbloom hp = gethostbyname(host); 36558360Seric gothostent: 36625475Smiriam if (hp == NULL) 36724945Seric { 36835651Seric #ifdef NAMED_BIND 36925475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 37025475Smiriam return (EX_TEMPFAIL); 37125657Seric 37235651Seric /* if name server is specified, assume temp fail */ 37335651Seric if (errno == ECONNREFUSED && UseNameServer) 37435651Seric return (EX_TEMPFAIL); 37535651Seric #endif 37625475Smiriam return (EX_NOHOST); 37724945Seric } 37852106Seric bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 37929430Sbloom i = 1; 3809610Seric } 3819610Seric 3829610Seric /* 3839610Seric ** Determine the port number. 3849610Seric */ 3859610Seric 38610011Seric if (port != 0) 38752106Seric addr.sin_port = htons(port); 38810011Seric else 3899610Seric { 3909610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3919610Seric 3929610Seric if (sp == NULL) 3939610Seric { 39458151Seric syserr("554 makeconnection: server \"smtp\" unknown"); 39557977Seric return (EX_OSERR); 3969610Seric } 39752106Seric addr.sin_port = sp->s_port; 3989610Seric } 3996039Seric 4006039Seric /* 4016039Seric ** Try to actually open the connection. 4026039Seric */ 4036039Seric 40457736Seric for (;;) 40552106Seric { 40657736Seric if (tTd(16, 1)) 40757736Seric printf("makeconnection (%s [%s])\n", host, 40857736Seric inet_ntoa(addr.sin_addr)); 40952106Seric 41057736Seric if (usesecureport) 41157736Seric { 41257736Seric int rport = IPPORT_RESERVED - 1; 4136039Seric 41457736Seric s = rresvport(&rport); 41557736Seric } 41657736Seric else 41757736Seric { 41857736Seric s = socket(AF_INET, SOCK_STREAM, 0); 41957736Seric } 42057736Seric if (s < 0) 42157736Seric { 42257736Seric sav_errno = errno; 42357736Seric syserr("makeconnection: no socket"); 42457736Seric goto failure; 42557736Seric } 42610347Seric 42757736Seric if (tTd(16, 1)) 42857736Seric printf("makeconnection: fd=%d\n", s); 42957736Seric 43057736Seric /* turn on network debugging? */ 43157736Seric if (tTd(16, 101)) 43257736Seric { 43357736Seric int on = 1; 43457736Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 43557736Seric (char *)&on, sizeof on); 43657736Seric } 43757736Seric if (CurEnv->e_xfp != NULL) 43857736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 43957736Seric errno = 0; /* for debugging */ 44057736Seric addr.sin_family = AF_INET; 44157736Seric if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0) 44257736Seric break; 44357736Seric 44457736Seric /* couldn't connect.... figure out why */ 44527744Sbloom sav_errno = errno; 44627744Sbloom (void) close(s); 44729430Sbloom if (hp && hp->h_addr_list[i]) 44829430Sbloom { 44957736Seric if (tTd(16, 1)) 45057736Seric printf("Connect failed; trying new address....\n"); 45152106Seric bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 45252106Seric hp->h_length); 45357736Seric continue; 45429430Sbloom } 45529430Sbloom 4566039Seric /* failure, decide if temporary or not */ 4576039Seric failure: 45827744Sbloom switch (sav_errno) 4596039Seric { 4606039Seric case EISCONN: 4616039Seric case ETIMEDOUT: 4626897Seric case EINPROGRESS: 4636897Seric case EALREADY: 4646897Seric case EADDRINUSE: 46510123Seric case EHOSTDOWN: 4666897Seric case ENETDOWN: 4676897Seric case ENETRESET: 4686897Seric case ENOBUFS: 4697204Seric case ECONNREFUSED: 47011546Seric case ECONNRESET: 47110081Seric case EHOSTUNREACH: 47210098Seric case ENETUNREACH: 47351995Seric #ifdef ENOSR 47451995Seric case ENOSR: 47551995Seric #endif 4766039Seric /* there are others, I'm sure..... */ 4776039Seric return (EX_TEMPFAIL); 4786039Seric 47911147Seric case EPERM: 48011147Seric /* why is this happening? */ 48111147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 48252106Seric addr.sin_addr.s_addr, addr.sin_port); 48314383Seric return (EX_TEMPFAIL); 48411147Seric 4856039Seric default: 48640932Srick { 48740932Srick extern char *errstring(); 48840932Srick 48958151Seric message("%s", errstring(sav_errno)); 49040932Srick return (EX_UNAVAILABLE); 49140932Srick } 4926039Seric } 4936039Seric } 4946039Seric 4956039Seric /* connection ok, put it into canonical form */ 49653739Seric mci->mci_out = fdopen(s, "w"); 49753739Seric mci->mci_in = fdopen(dup(s), "r"); 4986039Seric 49958305Seric /* save for logging */ 50058305Seric CurHostAddr = addr; 50158305Seric 50210098Seric return (EX_OK); 5036039Seric } 50410758Seric /* 50510758Seric ** MYHOSTNAME -- return the name of this host. 50610758Seric ** 50710758Seric ** Parameters: 50810758Seric ** hostbuf -- a place to return the name of this host. 50912313Seric ** size -- the size of hostbuf. 51010758Seric ** 51110758Seric ** Returns: 51210758Seric ** A list of aliases for this host. 51310758Seric ** 51410758Seric ** Side Effects: 51558110Seric ** Sets the MyIpAddrs buffer to a list of my IP addresses. 51610758Seric */ 5176039Seric 51858110Seric struct in_addr MyIpAddrs[MAXIPADDR + 1]; 51958110Seric 52010758Seric char ** 52112313Seric myhostname(hostbuf, size) 52210758Seric char hostbuf[]; 52312313Seric int size; 52410758Seric { 52558110Seric register struct hostent *hp; 52610758Seric extern struct hostent *gethostbyname(); 52710758Seric 52823120Seric if (gethostname(hostbuf, size) < 0) 52923120Seric { 53023120Seric (void) strcpy(hostbuf, "localhost"); 53123120Seric } 53211147Seric hp = gethostbyname(hostbuf); 53311147Seric if (hp != NULL) 53416877Seric { 53558110Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 53658110Seric hostbuf[size - 1] = '\0'; 53758110Seric 53858110Seric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 53958110Seric { 54058110Seric register int i; 54158110Seric 54258110Seric for (i = 0; i < MAXIPADDR; i++) 54358110Seric { 54458110Seric if (hp->h_addr_list[i] == NULL) 54558110Seric break; 54658110Seric MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 54758110Seric } 54858110Seric MyIpAddrs[i].s_addr = 0; 54958110Seric } 55058110Seric 55111147Seric return (hp->h_aliases); 55216877Seric } 55310758Seric else 55410758Seric return (NULL); 55510758Seric } 55651315Seric /* 55758308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 55858308Seric ** 55958308Seric ** Parameters: 56058308Seric ** fd -- the descriptor 56158308Seric ** 56258308Seric ** Returns: 56358308Seric ** The host name associated with this descriptor, if it can 56458308Seric ** be determined. 56558308Seric ** NULL otherwise. 56658308Seric ** 56758308Seric ** Side Effects: 56858308Seric ** none 56958308Seric */ 57058308Seric 57158308Seric char * 57258308Seric getrealhostname(fd) 57358308Seric int fd; 57458308Seric { 57558308Seric register struct hostent *hp; 57658308Seric struct sockaddr_in sin; 57758308Seric int sinlen; 57858308Seric char hbuf[MAXNAME]; 57958308Seric extern struct hostent *gethostbyaddr(); 58058308Seric extern char *inet_ntoa(); 58158308Seric 58258308Seric if (getsockname(fd, (struct sockaddr *) &sin, &sinlen) < 0) 58358308Seric return NULL; 58458308Seric hp = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, 58558308Seric sin.sin_family); 58658308Seric if (hp != NULL) 58758308Seric (void) strcpy(hbuf, hp->h_name); 58858308Seric else 58958308Seric (void) sprintf(hbuf, "[%s]", inet_ntoa(sin.sin_addr)); 59058308Seric return hbuf; 59158308Seric } 59258308Seric /* 59353751Seric ** MAPHOSTNAME -- turn a hostname into canonical form 59453751Seric ** 59553751Seric ** Parameters: 59656823Seric ** map -- a pointer to this map (unused). 59753751Seric ** hbuf -- a buffer containing a hostname. 59853751Seric ** hbsize -- the size of hbuf. 59955019Seric ** avp -- unused -- for compatibility with other mapping 60055019Seric ** functions. 60153751Seric ** 60253751Seric ** Returns: 60353751Seric ** The mapping, if found. 60453751Seric ** NULL if no mapping found. 60553751Seric ** 60653751Seric ** Side Effects: 60753751Seric ** Looks up the host specified in hbuf. If it is not 60853751Seric ** the canonical name for that host, return the canonical 60953751Seric ** name. 61053751Seric */ 61151315Seric 61253751Seric char * 61356823Seric maphostname(map, hbuf, hbsize, avp) 61456823Seric MAP *map; 61516911Seric char *hbuf; 61616911Seric int hbsize; 61753751Seric char **avp; 61816911Seric { 61916911Seric register struct hostent *hp; 62033932Sbostic u_long in_addr; 62156823Seric char *cp; 62258110Seric int i; 62333932Sbostic struct hostent *gethostbyaddr(); 62416911Seric 62556836Seric /* allow room for null */ 62656823Seric hbsize--; 62753751Seric 62825574Smiriam /* 62933932Sbostic * If first character is a bracket, then it is an address 63033932Sbostic * lookup. Address is copied into a temporary buffer to 63133932Sbostic * strip the brackets and to preserve hbuf if address is 63233932Sbostic * unknown. 63333932Sbostic */ 63453751Seric 63551315Seric if (*hbuf != '[') 63653751Seric { 63755019Seric extern bool getcanonname(); 63855019Seric 63953751Seric if (getcanonname(hbuf, hbsize)) 64053751Seric return hbuf; 64153751Seric else 64253751Seric return NULL; 64353751Seric } 64456823Seric if ((cp = strchr(hbuf, ']')) == NULL) 64553751Seric return (NULL); 64640994Sbostic *cp = '\0'; 64756823Seric in_addr = inet_addr(&hbuf[1]); 64858110Seric 64958110Seric /* check to see if this is one of our addresses */ 65058110Seric for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 65158110Seric { 65258110Seric if (MyIpAddrs[i].s_addr == in_addr) 65358110Seric { 65458110Seric strncpy(hbuf, MyHostName, hbsize); 65558110Seric hbuf[hbsize] = '\0'; 65658110Seric return hbuf; 65758110Seric } 65858110Seric } 65958110Seric 66058110Seric /* nope -- ask the name server */ 66133932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 66233932Sbostic if (hp == NULL) 66353751Seric return (NULL); 66453751Seric 66558110Seric /* found a match -- copy out */ 66656823Seric if (strlen(hp->h_name) > hbsize) 66756823Seric hp->h_name[hbsize] = '\0'; 66853751Seric (void) strcpy(hbuf, hp->h_name); 66953751Seric return hbuf; 67033932Sbostic } 67116911Seric 67256795Seric # else /* DAEMON */ 67316911Seric /* code for systems without sophisticated networking */ 67410758Seric 67510758Seric /* 67610758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 67711297Seric ** 67811297Seric ** Can't convert to upper case here because might be a UUCP name. 67912313Seric ** 68012313Seric ** Mark, you can change this to be anything you want...... 68110758Seric */ 68210758Seric 68310758Seric char ** 68412313Seric myhostname(hostbuf, size) 68510758Seric char hostbuf[]; 68612313Seric int size; 68710758Seric { 68810758Seric register FILE *f; 68910758Seric 69010758Seric hostbuf[0] = '\0'; 69110758Seric f = fopen("/usr/include/whoami", "r"); 69210758Seric if (f != NULL) 69310758Seric { 69412313Seric (void) fgets(hostbuf, size, f); 69510758Seric fixcrlf(hostbuf, TRUE); 69610758Seric (void) fclose(f); 69710758Seric } 69810758Seric return (NULL); 69910758Seric } 70016911Seric /* 70158308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 70258308Seric ** 70358308Seric ** Parameters: 70458308Seric ** fd -- the descriptor 70558308Seric ** 70658308Seric ** Returns: 70758308Seric ** The host name associated with this descriptor, if it can 70858308Seric ** be determined. 70958308Seric ** NULL otherwise. 71058308Seric ** 71158308Seric ** Side Effects: 71258308Seric ** none 71358308Seric */ 71458308Seric 71558308Seric char * 71658308Seric getrealhostname(fd) 71758308Seric int fd; 71858308Seric { 71958308Seric return NULL; 72058308Seric } 72158308Seric /* 72216911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 72316911Seric ** 72416911Seric ** Parameters: 72556823Seric ** map -- a pointer to the database map. 72616911Seric ** hbuf -- a buffer containing a hostname. 72753751Seric ** avp -- a pointer to a (cf file defined) argument vector. 72816911Seric ** 72916911Seric ** Returns: 73053751Seric ** mapped host name 73151315Seric ** FALSE otherwise. 73216911Seric ** 73316911Seric ** Side Effects: 73416911Seric ** Looks up the host specified in hbuf. If it is not 73516911Seric ** the canonical name for that host, replace it with 73616911Seric ** the canonical name. If the name is unknown, or it 73716911Seric ** is already the canonical name, leave it unchanged. 73816911Seric */ 73910758Seric 74016911Seric /*ARGSUSED*/ 74153751Seric char * 74256823Seric maphostname(map, hbuf, hbsize, avp) 74356823Seric MAP *map; 74416911Seric char *hbuf; 74516911Seric int hbsize; 74653751Seric char **avp; 74716911Seric { 74853751Seric return NULL; 74916911Seric } 75016911Seric 75156795Seric #endif /* DAEMON */ 752