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*58542Seric static char sccsid[] = "@(#)daemon.c 6.15 (Berkeley) 03/07/93 (with daemon mode)"; 1633780Sbostic #else 17*58542Seric static char sccsid[] = "@(#)daemon.c 6.15 (Berkeley) 03/07/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; 8258419Seric 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 13758419Seric /* write the pid to the log file for posterity */ 13858419Seric pidf = fopen(PidFile, "w"); 13958419Seric if (pidf != NULL) 14058419Seric { 14158419Seric fprintf(pidf, "%d\n", getpid()); 14258419Seric fclose(pidf); 14358419Seric } 14458419Seric 14558419Seric 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: 458*58542Seric if (transienterror(sav_errno)) 459*58542Seric return EX_TEMPFAIL; 460*58542Seric else if (sav_errno == EPERM) 4616039Seric { 46211147Seric /* why is this happening? */ 46311147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 46452106Seric addr.sin_addr.s_addr, addr.sin_port); 46514383Seric return (EX_TEMPFAIL); 466*58542Seric } 467*58542Seric else 468*58542Seric { 469*58542Seric extern char *errstring(); 47011147Seric 471*58542Seric message("%s", errstring(sav_errno)); 472*58542Seric return (EX_UNAVAILABLE); 4736039Seric } 4746039Seric } 4756039Seric 4766039Seric /* connection ok, put it into canonical form */ 47753739Seric mci->mci_out = fdopen(s, "w"); 47853739Seric mci->mci_in = fdopen(dup(s), "r"); 4796039Seric 48058305Seric /* save for logging */ 48158305Seric CurHostAddr = addr; 48258305Seric 48310098Seric return (EX_OK); 4846039Seric } 48510758Seric /* 48610758Seric ** MYHOSTNAME -- return the name of this host. 48710758Seric ** 48810758Seric ** Parameters: 48910758Seric ** hostbuf -- a place to return the name of this host. 49012313Seric ** size -- the size of hostbuf. 49110758Seric ** 49210758Seric ** Returns: 49310758Seric ** A list of aliases for this host. 49410758Seric ** 49510758Seric ** Side Effects: 49658110Seric ** Sets the MyIpAddrs buffer to a list of my IP addresses. 49710758Seric */ 4986039Seric 49958110Seric struct in_addr MyIpAddrs[MAXIPADDR + 1]; 50058110Seric 50110758Seric char ** 50212313Seric myhostname(hostbuf, size) 50310758Seric char hostbuf[]; 50412313Seric int size; 50510758Seric { 50658110Seric register struct hostent *hp; 50710758Seric extern struct hostent *gethostbyname(); 50810758Seric 50923120Seric if (gethostname(hostbuf, size) < 0) 51023120Seric { 51123120Seric (void) strcpy(hostbuf, "localhost"); 51223120Seric } 51311147Seric hp = gethostbyname(hostbuf); 51411147Seric if (hp != NULL) 51516877Seric { 51658110Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 51758110Seric hostbuf[size - 1] = '\0'; 51858110Seric 51958110Seric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 52058110Seric { 52158110Seric register int i; 52258110Seric 52358110Seric for (i = 0; i < MAXIPADDR; i++) 52458110Seric { 52558110Seric if (hp->h_addr_list[i] == NULL) 52658110Seric break; 52758110Seric MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 52858110Seric } 52958110Seric MyIpAddrs[i].s_addr = 0; 53058110Seric } 53158110Seric 53211147Seric return (hp->h_aliases); 53316877Seric } 53410758Seric else 53510758Seric return (NULL); 53610758Seric } 53751315Seric /* 53858308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 53958308Seric ** 54058308Seric ** Parameters: 54158308Seric ** fd -- the descriptor 54258308Seric ** 54358308Seric ** Returns: 54458308Seric ** The host name associated with this descriptor, if it can 54558308Seric ** be determined. 54658308Seric ** NULL otherwise. 54758308Seric ** 54858308Seric ** Side Effects: 54958308Seric ** none 55058308Seric */ 55158308Seric 55258308Seric char * 55358308Seric getrealhostname(fd) 55458308Seric int fd; 55558308Seric { 55658308Seric register struct hostent *hp; 55758308Seric struct sockaddr_in sin; 55858308Seric int sinlen; 55958308Seric char hbuf[MAXNAME]; 56058308Seric extern struct hostent *gethostbyaddr(); 56158308Seric extern char *inet_ntoa(); 56258308Seric 56358501Seric if (getsockname(fd, (struct sockaddr *) &sin, &sinlen) < 0 || 56458501Seric sinlen <= 0) 56558308Seric return NULL; 56658308Seric hp = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, 56758308Seric sin.sin_family); 56858308Seric if (hp != NULL) 56958308Seric (void) strcpy(hbuf, hp->h_name); 57058308Seric else 57158308Seric (void) sprintf(hbuf, "[%s]", inet_ntoa(sin.sin_addr)); 57258308Seric return hbuf; 57358308Seric } 57458308Seric /* 57553751Seric ** MAPHOSTNAME -- turn a hostname into canonical form 57653751Seric ** 57753751Seric ** Parameters: 57856823Seric ** map -- a pointer to this map (unused). 57953751Seric ** hbuf -- a buffer containing a hostname. 58053751Seric ** hbsize -- the size of hbuf. 58155019Seric ** avp -- unused -- for compatibility with other mapping 58255019Seric ** functions. 58353751Seric ** 58453751Seric ** Returns: 58553751Seric ** The mapping, if found. 58653751Seric ** NULL if no mapping found. 58753751Seric ** 58853751Seric ** Side Effects: 58953751Seric ** Looks up the host specified in hbuf. If it is not 59053751Seric ** the canonical name for that host, return the canonical 59153751Seric ** name. 59253751Seric */ 59351315Seric 59453751Seric char * 59556823Seric maphostname(map, hbuf, hbsize, avp) 59656823Seric MAP *map; 59716911Seric char *hbuf; 59816911Seric int hbsize; 59953751Seric char **avp; 60016911Seric { 60116911Seric register struct hostent *hp; 60233932Sbostic u_long in_addr; 60356823Seric char *cp; 60458110Seric int i; 60533932Sbostic struct hostent *gethostbyaddr(); 60616911Seric 60756836Seric /* allow room for null */ 60856823Seric hbsize--; 60953751Seric 61025574Smiriam /* 61133932Sbostic * If first character is a bracket, then it is an address 61233932Sbostic * lookup. Address is copied into a temporary buffer to 61333932Sbostic * strip the brackets and to preserve hbuf if address is 61433932Sbostic * unknown. 61533932Sbostic */ 61653751Seric 61751315Seric if (*hbuf != '[') 61853751Seric { 61955019Seric extern bool getcanonname(); 62055019Seric 62153751Seric if (getcanonname(hbuf, hbsize)) 62253751Seric return hbuf; 62353751Seric else 62453751Seric return NULL; 62553751Seric } 62656823Seric if ((cp = strchr(hbuf, ']')) == NULL) 62753751Seric return (NULL); 62840994Sbostic *cp = '\0'; 62956823Seric in_addr = inet_addr(&hbuf[1]); 63058110Seric 63158110Seric /* check to see if this is one of our addresses */ 63258110Seric for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 63358110Seric { 63458110Seric if (MyIpAddrs[i].s_addr == in_addr) 63558110Seric { 63658110Seric strncpy(hbuf, MyHostName, hbsize); 63758110Seric hbuf[hbsize] = '\0'; 63858110Seric return hbuf; 63958110Seric } 64058110Seric } 64158110Seric 64258110Seric /* nope -- ask the name server */ 64333932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 64433932Sbostic if (hp == NULL) 64553751Seric return (NULL); 64653751Seric 64758110Seric /* found a match -- copy out */ 64856823Seric if (strlen(hp->h_name) > hbsize) 64956823Seric hp->h_name[hbsize] = '\0'; 65053751Seric (void) strcpy(hbuf, hp->h_name); 65153751Seric return hbuf; 65233932Sbostic } 65316911Seric 65456795Seric # else /* DAEMON */ 65516911Seric /* code for systems without sophisticated networking */ 65610758Seric 65710758Seric /* 65810758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 65911297Seric ** 66011297Seric ** Can't convert to upper case here because might be a UUCP name. 66112313Seric ** 66212313Seric ** Mark, you can change this to be anything you want...... 66310758Seric */ 66410758Seric 66510758Seric char ** 66612313Seric myhostname(hostbuf, size) 66710758Seric char hostbuf[]; 66812313Seric int size; 66910758Seric { 67010758Seric register FILE *f; 67110758Seric 67210758Seric hostbuf[0] = '\0'; 67310758Seric f = fopen("/usr/include/whoami", "r"); 67410758Seric if (f != NULL) 67510758Seric { 67612313Seric (void) fgets(hostbuf, size, f); 67710758Seric fixcrlf(hostbuf, TRUE); 67810758Seric (void) fclose(f); 67910758Seric } 68010758Seric return (NULL); 68110758Seric } 68216911Seric /* 68358308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 68458308Seric ** 68558308Seric ** Parameters: 68658308Seric ** fd -- the descriptor 68758308Seric ** 68858308Seric ** Returns: 68958308Seric ** The host name associated with this descriptor, if it can 69058308Seric ** be determined. 69158308Seric ** NULL otherwise. 69258308Seric ** 69358308Seric ** Side Effects: 69458308Seric ** none 69558308Seric */ 69658308Seric 69758308Seric char * 69858308Seric getrealhostname(fd) 69958308Seric int fd; 70058308Seric { 70158308Seric return NULL; 70258308Seric } 70358308Seric /* 70416911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 70516911Seric ** 70616911Seric ** Parameters: 70756823Seric ** map -- a pointer to the database map. 70816911Seric ** hbuf -- a buffer containing a hostname. 70953751Seric ** avp -- a pointer to a (cf file defined) argument vector. 71016911Seric ** 71116911Seric ** Returns: 71253751Seric ** mapped host name 71351315Seric ** FALSE otherwise. 71416911Seric ** 71516911Seric ** Side Effects: 71616911Seric ** Looks up the host specified in hbuf. If it is not 71716911Seric ** the canonical name for that host, replace it with 71816911Seric ** the canonical name. If the name is unknown, or it 71916911Seric ** is already the canonical name, leave it unchanged. 72016911Seric */ 72110758Seric 72216911Seric /*ARGSUSED*/ 72353751Seric char * 72456823Seric maphostname(map, hbuf, hbsize, avp) 72556823Seric MAP *map; 72616911Seric char *hbuf; 72716911Seric int hbsize; 72853751Seric char **avp; 72916911Seric { 73053751Seric return NULL; 73116911Seric } 73216911Seric 73356795Seric #endif /* DAEMON */ 734