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*58360Seric static char sccsid[] = "@(#)daemon.c 6.12 (Berkeley) 03/02/93 (with daemon mode)"; 1633780Sbostic #else 17*58360Seric static char sccsid[] = "@(#)daemon.c 6.12 (Berkeley) 03/02/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; 8252106Seric struct sockaddr_in srvraddr; 8346928Sbostic extern void reapchild(); 847117Seric 859610Seric /* 869610Seric ** Set up the address for the mailer. 879610Seric */ 889610Seric 899610Seric sp = getservbyname("smtp", "tcp"); 909610Seric if (sp == NULL) 919610Seric { 9258151Seric syserr("554 server \"smtp\" unknown"); 9310167Seric goto severe; 949610Seric } 9552106Seric srvraddr.sin_family = AF_INET; 9652106Seric srvraddr.sin_addr.s_addr = INADDR_ANY; 9752106Seric srvraddr.sin_port = sp->s_port; 989610Seric 999610Seric /* 1009610Seric ** Try to actually open the connection. 1019610Seric */ 1029610Seric 1039610Seric if (tTd(15, 1)) 10452106Seric printf("getrequests: port 0x%x\n", srvraddr.sin_port); 1059610Seric 1069610Seric /* get a socket for the SMTP connection */ 10723120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 10810206Seric if (DaemonSocket < 0) 1099610Seric { 1109610Seric /* probably another daemon already */ 1119610Seric syserr("getrequests: can't create socket"); 1129610Seric severe: 1139610Seric # ifdef LOG 1149610Seric if (LogLevel > 0) 11557663Seric syslog(LOG_ALERT, "problem creating SMTP socket"); 11656795Seric # endif /* LOG */ 1179610Seric finis(); 1189610Seric } 11910347Seric 12010347Seric /* turn on network debugging? */ 12156328Seric if (tTd(15, 101)) 12224945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12310347Seric 12425027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 12525027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 12625027Seric 12752106Seric if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0) 1289610Seric { 1299610Seric syserr("getrequests: cannot bind"); 13010206Seric (void) close(DaemonSocket); 1319610Seric goto severe; 1329610Seric } 1339610Seric 13424955Seric (void) signal(SIGCHLD, reapchild); 13524945Seric 1369610Seric if (tTd(15, 1)) 13710206Seric printf("getrequests: %d\n", DaemonSocket); 1389610Seric 1394631Seric for (;;) 1404631Seric { 14114875Seric register int pid; 14211147Seric auto int lotherend; 14353751Seric extern bool refuseconnections(); 14411147Seric 14514875Seric /* see if we are rejecting connections */ 14653751Seric CurrentLA = getla(); 14753751Seric if (refuseconnections()) 14836584Sbostic { 14953751Seric if (!refusingconnections) 15053751Seric { 15153751Seric /* don't queue so peer will fail quickly */ 15253751Seric (void) listen(DaemonSocket, 0); 15353751Seric refusingconnections = TRUE; 15453751Seric } 15557385Seric setproctitle("rejecting connections: load average: %d", 15657385Seric CurrentLA); 15714875Seric sleep(5); 15853751Seric continue; 15936584Sbostic } 16014875Seric 16153751Seric if (refusingconnections) 16253751Seric { 16353751Seric /* start listening again */ 16453751Seric if (listen(DaemonSocket, 10) < 0) 16553751Seric { 16653751Seric syserr("getrequests: cannot listen"); 16753751Seric (void) close(DaemonSocket); 16853751Seric goto severe; 16953751Seric } 17053751Seric setproctitle("accepting connections"); 17153751Seric refusingconnections = FALSE; 17253751Seric } 17353751Seric 1749610Seric /* wait for a connection */ 1759610Seric do 1769610Seric { 1779610Seric errno = 0; 17836230Skarels lotherend = sizeof RealHostAddr; 17946928Sbostic t = accept(DaemonSocket, 18046928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1819610Seric } while (t < 0 && errno == EINTR); 1829610Seric if (t < 0) 1835978Seric { 1849610Seric syserr("getrequests: accept"); 1859610Seric sleep(5); 1869610Seric continue; 1875978Seric } 1884631Seric 1895978Seric /* 1905978Seric ** Create a subprocess to process the mail. 1915978Seric */ 1925978Seric 1937677Seric if (tTd(15, 2)) 1949610Seric printf("getrequests: forking (fd = %d)\n", t); 1955978Seric 1964636Seric pid = fork(); 1974636Seric if (pid < 0) 1984631Seric { 1994636Seric syserr("daemon: cannot fork"); 2004636Seric sleep(10); 2019610Seric (void) close(t); 2024636Seric continue; 2034631Seric } 2044631Seric 2054636Seric if (pid == 0) 2064631Seric { 20711147Seric extern struct hostent *gethostbyaddr(); 20811147Seric register struct hostent *hp; 20911147Seric char buf[MAXNAME]; 21057135Seric extern char *inet_ntoa(); 21111147Seric 2124636Seric /* 2134636Seric ** CHILD -- return to caller. 21411147Seric ** Collect verified idea of sending host. 2154636Seric ** Verify calling user id if possible here. 2164636Seric */ 2174631Seric 21824955Seric (void) signal(SIGCHLD, SIG_DFL); 21924950Seric 22011147Seric /* determine host name */ 22136230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 22211147Seric if (hp != NULL) 22323104Seric (void) strcpy(buf, hp->h_name); 22411147Seric else 22516884Seric { 22616884Seric /* produce a dotted quad */ 22716884Seric (void) sprintf(buf, "[%s]", 22836230Skarels inet_ntoa(RealHostAddr.sin_addr)); 22916884Seric } 23016884Seric 23155173Seric #ifdef LOG 23257977Seric if (LogLevel > 10) 23355173Seric { 23455173Seric /* log connection information */ 23555173Seric syslog(LOG_INFO, "connect from %s (%s)", 23655173Seric buf, inet_ntoa(RealHostAddr.sin_addr)); 23755173Seric } 23855173Seric #endif 23955173Seric 24016884Seric /* should we check for illegal connection here? XXX */ 24116884Seric 24211147Seric RealHostName = newstr(buf); 24311147Seric 24410206Seric (void) close(DaemonSocket); 2459610Seric InChannel = fdopen(t, "r"); 24621062Seric OutChannel = fdopen(dup(t), "w"); 2477677Seric if (tTd(15, 2)) 2485978Seric printf("getreq: returning\n"); 2497876Seric # ifdef LOG 2507876Seric if (LogLevel > 11) 2517876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 25256795Seric # endif /* LOG */ 2534636Seric return; 2544631Seric } 2554631Seric 2567117Seric /* close the port so that others will hang (for a while) */ 2579610Seric (void) close(t); 2584631Seric } 2599886Seric /*NOTREACHED*/ 2604631Seric } 2615978Seric /* 26210206Seric ** CLRDAEMON -- reset the daemon connection 26310206Seric ** 26410206Seric ** Parameters: 26510206Seric ** none. 26610206Seric ** 26710206Seric ** Returns: 26810206Seric ** none. 26910206Seric ** 27010206Seric ** Side Effects: 27110206Seric ** releases any resources used by the passive daemon. 27210206Seric */ 27310206Seric 27410206Seric clrdaemon() 27510206Seric { 27610206Seric if (DaemonSocket >= 0) 27710206Seric (void) close(DaemonSocket); 27810206Seric DaemonSocket = -1; 27910206Seric } 28010206Seric /* 2816039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2826039Seric ** 2836039Seric ** Parameters: 2846039Seric ** host -- the name of the host. 2856633Seric ** port -- the port number to connect to. 28653739Seric ** mci -- a pointer to the mail connection information 28753739Seric ** structure to be filled in. 28852106Seric ** usesecureport -- if set, use a low numbered (reserved) 28952106Seric ** port to provide some rudimentary authentication. 2906039Seric ** 2916039Seric ** Returns: 2926039Seric ** An exit code telling whether the connection could be 2936039Seric ** made and if not why not. 2946039Seric ** 2956039Seric ** Side Effects: 2966039Seric ** none. 2976039Seric */ 2985978Seric 29958305Seric struct sockaddr_in CurHostAddr; /* address of current host */ 30058305Seric 30154967Seric int 30253739Seric makeconnection(host, port, mci, usesecureport) 3036039Seric char *host; 3047286Seric u_short port; 30554967Seric register MCI *mci; 30652106Seric bool usesecureport; 3076039Seric { 30829430Sbloom register int i, s; 30929430Sbloom register struct hostent *hp = (struct hostent *)NULL; 31052106Seric struct sockaddr_in addr; 31152106Seric int sav_errno; 31229430Sbloom extern char *inet_ntoa(); 31335651Seric #ifdef NAMED_BIND 31435651Seric extern int h_errno; 31535651Seric #endif 3166039Seric 3176039Seric /* 3186039Seric ** Set up the address for the mailer. 3199308Seric ** Accept "[a.b.c.d]" syntax for host name. 3206039Seric */ 3216039Seric 32235651Seric #ifdef NAMED_BIND 32325475Smiriam h_errno = 0; 32435651Seric #endif 32525475Smiriam errno = 0; 32625475Smiriam 3279308Seric if (host[0] == '[') 3289308Seric { 32911147Seric long hid; 33056795Seric register char *p = strchr(host, ']'); 3319308Seric 33211147Seric if (p != NULL) 3339308Seric { 33411147Seric *p = '\0'; 33511147Seric hid = inet_addr(&host[1]); 336*58360Seric if (hid == -1) 337*58360Seric { 338*58360Seric /* try it as a host name (avoid MX lookup) */ 339*58360Seric hp = gethostbyname(&host[1]); 340*58360Seric *p = ']'; 341*58360Seric goto gothostent; 342*58360Seric } 34311147Seric *p = ']'; 3449308Seric } 345*58360Seric if (p == NULL) 3469308Seric { 34758151Seric usrerr("553 Invalid numeric domain spec \"%s\"", host); 3489308Seric return (EX_NOHOST); 3499308Seric } 35052106Seric addr.sin_addr.s_addr = hid; 3519308Seric } 3529610Seric else 3539610Seric { 35429430Sbloom hp = gethostbyname(host); 355*58360Seric gothostent: 35625475Smiriam if (hp == NULL) 35724945Seric { 35835651Seric #ifdef NAMED_BIND 35925475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 36025475Smiriam return (EX_TEMPFAIL); 36125657Seric 36235651Seric /* if name server is specified, assume temp fail */ 36335651Seric if (errno == ECONNREFUSED && UseNameServer) 36435651Seric return (EX_TEMPFAIL); 36535651Seric #endif 36625475Smiriam return (EX_NOHOST); 36724945Seric } 36852106Seric bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 36929430Sbloom i = 1; 3709610Seric } 3719610Seric 3729610Seric /* 3739610Seric ** Determine the port number. 3749610Seric */ 3759610Seric 37610011Seric if (port != 0) 37752106Seric addr.sin_port = htons(port); 37810011Seric else 3799610Seric { 3809610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3819610Seric 3829610Seric if (sp == NULL) 3839610Seric { 38458151Seric syserr("554 makeconnection: server \"smtp\" unknown"); 38557977Seric return (EX_OSERR); 3869610Seric } 38752106Seric addr.sin_port = sp->s_port; 3889610Seric } 3896039Seric 3906039Seric /* 3916039Seric ** Try to actually open the connection. 3926039Seric */ 3936039Seric 39457736Seric for (;;) 39552106Seric { 39657736Seric if (tTd(16, 1)) 39757736Seric printf("makeconnection (%s [%s])\n", host, 39857736Seric inet_ntoa(addr.sin_addr)); 39952106Seric 40057736Seric if (usesecureport) 40157736Seric { 40257736Seric int rport = IPPORT_RESERVED - 1; 4036039Seric 40457736Seric s = rresvport(&rport); 40557736Seric } 40657736Seric else 40757736Seric { 40857736Seric s = socket(AF_INET, SOCK_STREAM, 0); 40957736Seric } 41057736Seric if (s < 0) 41157736Seric { 41257736Seric sav_errno = errno; 41357736Seric syserr("makeconnection: no socket"); 41457736Seric goto failure; 41557736Seric } 41610347Seric 41757736Seric if (tTd(16, 1)) 41857736Seric printf("makeconnection: fd=%d\n", s); 41957736Seric 42057736Seric /* turn on network debugging? */ 42157736Seric if (tTd(16, 101)) 42257736Seric { 42357736Seric int on = 1; 42457736Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 42557736Seric (char *)&on, sizeof on); 42657736Seric } 42757736Seric if (CurEnv->e_xfp != NULL) 42857736Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 42957736Seric errno = 0; /* for debugging */ 43057736Seric addr.sin_family = AF_INET; 43157736Seric if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0) 43257736Seric break; 43357736Seric 43457736Seric /* couldn't connect.... figure out why */ 43527744Sbloom sav_errno = errno; 43627744Sbloom (void) close(s); 43729430Sbloom if (hp && hp->h_addr_list[i]) 43829430Sbloom { 43957736Seric if (tTd(16, 1)) 44057736Seric printf("Connect failed; trying new address....\n"); 44152106Seric bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 44252106Seric hp->h_length); 44357736Seric continue; 44429430Sbloom } 44529430Sbloom 4466039Seric /* failure, decide if temporary or not */ 4476039Seric failure: 44827744Sbloom switch (sav_errno) 4496039Seric { 4506039Seric case EISCONN: 4516039Seric case ETIMEDOUT: 4526897Seric case EINPROGRESS: 4536897Seric case EALREADY: 4546897Seric case EADDRINUSE: 45510123Seric case EHOSTDOWN: 4566897Seric case ENETDOWN: 4576897Seric case ENETRESET: 4586897Seric case ENOBUFS: 4597204Seric case ECONNREFUSED: 46011546Seric case ECONNRESET: 46110081Seric case EHOSTUNREACH: 46210098Seric case ENETUNREACH: 46351995Seric #ifdef ENOSR 46451995Seric case ENOSR: 46551995Seric #endif 4666039Seric /* there are others, I'm sure..... */ 4676039Seric return (EX_TEMPFAIL); 4686039Seric 46911147Seric case EPERM: 47011147Seric /* why is this happening? */ 47111147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 47252106Seric addr.sin_addr.s_addr, addr.sin_port); 47314383Seric return (EX_TEMPFAIL); 47411147Seric 4756039Seric default: 47640932Srick { 47740932Srick extern char *errstring(); 47840932Srick 47958151Seric message("%s", errstring(sav_errno)); 48040932Srick return (EX_UNAVAILABLE); 48140932Srick } 4826039Seric } 4836039Seric } 4846039Seric 4856039Seric /* connection ok, put it into canonical form */ 48653739Seric mci->mci_out = fdopen(s, "w"); 48753739Seric mci->mci_in = fdopen(dup(s), "r"); 4886039Seric 48958305Seric /* save for logging */ 49058305Seric CurHostAddr = addr; 49158305Seric 49210098Seric return (EX_OK); 4936039Seric } 49410758Seric /* 49510758Seric ** MYHOSTNAME -- return the name of this host. 49610758Seric ** 49710758Seric ** Parameters: 49810758Seric ** hostbuf -- a place to return the name of this host. 49912313Seric ** size -- the size of hostbuf. 50010758Seric ** 50110758Seric ** Returns: 50210758Seric ** A list of aliases for this host. 50310758Seric ** 50410758Seric ** Side Effects: 50558110Seric ** Sets the MyIpAddrs buffer to a list of my IP addresses. 50610758Seric */ 5076039Seric 50858110Seric struct in_addr MyIpAddrs[MAXIPADDR + 1]; 50958110Seric 51010758Seric char ** 51112313Seric myhostname(hostbuf, size) 51210758Seric char hostbuf[]; 51312313Seric int size; 51410758Seric { 51558110Seric register struct hostent *hp; 51610758Seric extern struct hostent *gethostbyname(); 51710758Seric 51823120Seric if (gethostname(hostbuf, size) < 0) 51923120Seric { 52023120Seric (void) strcpy(hostbuf, "localhost"); 52123120Seric } 52211147Seric hp = gethostbyname(hostbuf); 52311147Seric if (hp != NULL) 52416877Seric { 52558110Seric (void) strncpy(hostbuf, hp->h_name, size - 1); 52658110Seric hostbuf[size - 1] = '\0'; 52758110Seric 52858110Seric if (hp->h_addrtype == AF_INET && hp->h_length == 4) 52958110Seric { 53058110Seric register int i; 53158110Seric 53258110Seric for (i = 0; i < MAXIPADDR; i++) 53358110Seric { 53458110Seric if (hp->h_addr_list[i] == NULL) 53558110Seric break; 53658110Seric MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 53758110Seric } 53858110Seric MyIpAddrs[i].s_addr = 0; 53958110Seric } 54058110Seric 54111147Seric return (hp->h_aliases); 54216877Seric } 54310758Seric else 54410758Seric return (NULL); 54510758Seric } 54651315Seric /* 54758308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 54858308Seric ** 54958308Seric ** Parameters: 55058308Seric ** fd -- the descriptor 55158308Seric ** 55258308Seric ** Returns: 55358308Seric ** The host name associated with this descriptor, if it can 55458308Seric ** be determined. 55558308Seric ** NULL otherwise. 55658308Seric ** 55758308Seric ** Side Effects: 55858308Seric ** none 55958308Seric */ 56058308Seric 56158308Seric char * 56258308Seric getrealhostname(fd) 56358308Seric int fd; 56458308Seric { 56558308Seric register struct hostent *hp; 56658308Seric struct sockaddr_in sin; 56758308Seric int sinlen; 56858308Seric char hbuf[MAXNAME]; 56958308Seric extern struct hostent *gethostbyaddr(); 57058308Seric extern char *inet_ntoa(); 57158308Seric 57258308Seric if (getsockname(fd, (struct sockaddr *) &sin, &sinlen) < 0) 57358308Seric return NULL; 57458308Seric hp = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, 57558308Seric sin.sin_family); 57658308Seric if (hp != NULL) 57758308Seric (void) strcpy(hbuf, hp->h_name); 57858308Seric else 57958308Seric (void) sprintf(hbuf, "[%s]", inet_ntoa(sin.sin_addr)); 58058308Seric return hbuf; 58158308Seric } 58258308Seric /* 58353751Seric ** MAPHOSTNAME -- turn a hostname into canonical form 58453751Seric ** 58553751Seric ** Parameters: 58656823Seric ** map -- a pointer to this map (unused). 58753751Seric ** hbuf -- a buffer containing a hostname. 58853751Seric ** hbsize -- the size of hbuf. 58955019Seric ** avp -- unused -- for compatibility with other mapping 59055019Seric ** functions. 59153751Seric ** 59253751Seric ** Returns: 59353751Seric ** The mapping, if found. 59453751Seric ** NULL if no mapping found. 59553751Seric ** 59653751Seric ** Side Effects: 59753751Seric ** Looks up the host specified in hbuf. If it is not 59853751Seric ** the canonical name for that host, return the canonical 59953751Seric ** name. 60053751Seric */ 60151315Seric 60253751Seric char * 60356823Seric maphostname(map, hbuf, hbsize, avp) 60456823Seric MAP *map; 60516911Seric char *hbuf; 60616911Seric int hbsize; 60753751Seric char **avp; 60816911Seric { 60916911Seric register struct hostent *hp; 61033932Sbostic u_long in_addr; 61156823Seric char *cp; 61258110Seric int i; 61333932Sbostic struct hostent *gethostbyaddr(); 61416911Seric 61556836Seric /* allow room for null */ 61656823Seric hbsize--; 61753751Seric 61825574Smiriam /* 61933932Sbostic * If first character is a bracket, then it is an address 62033932Sbostic * lookup. Address is copied into a temporary buffer to 62133932Sbostic * strip the brackets and to preserve hbuf if address is 62233932Sbostic * unknown. 62333932Sbostic */ 62453751Seric 62551315Seric if (*hbuf != '[') 62653751Seric { 62755019Seric extern bool getcanonname(); 62855019Seric 62953751Seric if (getcanonname(hbuf, hbsize)) 63053751Seric return hbuf; 63153751Seric else 63253751Seric return NULL; 63353751Seric } 63456823Seric if ((cp = strchr(hbuf, ']')) == NULL) 63553751Seric return (NULL); 63640994Sbostic *cp = '\0'; 63756823Seric in_addr = inet_addr(&hbuf[1]); 63858110Seric 63958110Seric /* check to see if this is one of our addresses */ 64058110Seric for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 64158110Seric { 64258110Seric if (MyIpAddrs[i].s_addr == in_addr) 64358110Seric { 64458110Seric strncpy(hbuf, MyHostName, hbsize); 64558110Seric hbuf[hbsize] = '\0'; 64658110Seric return hbuf; 64758110Seric } 64858110Seric } 64958110Seric 65058110Seric /* nope -- ask the name server */ 65133932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 65233932Sbostic if (hp == NULL) 65353751Seric return (NULL); 65453751Seric 65558110Seric /* found a match -- copy out */ 65656823Seric if (strlen(hp->h_name) > hbsize) 65756823Seric hp->h_name[hbsize] = '\0'; 65853751Seric (void) strcpy(hbuf, hp->h_name); 65953751Seric return hbuf; 66033932Sbostic } 66116911Seric 66256795Seric # else /* DAEMON */ 66316911Seric /* code for systems without sophisticated networking */ 66410758Seric 66510758Seric /* 66610758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 66711297Seric ** 66811297Seric ** Can't convert to upper case here because might be a UUCP name. 66912313Seric ** 67012313Seric ** Mark, you can change this to be anything you want...... 67110758Seric */ 67210758Seric 67310758Seric char ** 67412313Seric myhostname(hostbuf, size) 67510758Seric char hostbuf[]; 67612313Seric int size; 67710758Seric { 67810758Seric register FILE *f; 67910758Seric 68010758Seric hostbuf[0] = '\0'; 68110758Seric f = fopen("/usr/include/whoami", "r"); 68210758Seric if (f != NULL) 68310758Seric { 68412313Seric (void) fgets(hostbuf, size, f); 68510758Seric fixcrlf(hostbuf, TRUE); 68610758Seric (void) fclose(f); 68710758Seric } 68810758Seric return (NULL); 68910758Seric } 69016911Seric /* 69158308Seric ** GETREALHOSTNAME -- get the real host name asociated with a file descriptor 69258308Seric ** 69358308Seric ** Parameters: 69458308Seric ** fd -- the descriptor 69558308Seric ** 69658308Seric ** Returns: 69758308Seric ** The host name associated with this descriptor, if it can 69858308Seric ** be determined. 69958308Seric ** NULL otherwise. 70058308Seric ** 70158308Seric ** Side Effects: 70258308Seric ** none 70358308Seric */ 70458308Seric 70558308Seric char * 70658308Seric getrealhostname(fd) 70758308Seric int fd; 70858308Seric { 70958308Seric return NULL; 71058308Seric } 71158308Seric /* 71216911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 71316911Seric ** 71416911Seric ** Parameters: 71556823Seric ** map -- a pointer to the database map. 71616911Seric ** hbuf -- a buffer containing a hostname. 71753751Seric ** avp -- a pointer to a (cf file defined) argument vector. 71816911Seric ** 71916911Seric ** Returns: 72053751Seric ** mapped host name 72151315Seric ** FALSE otherwise. 72216911Seric ** 72316911Seric ** Side Effects: 72416911Seric ** Looks up the host specified in hbuf. If it is not 72516911Seric ** the canonical name for that host, replace it with 72616911Seric ** the canonical name. If the name is unknown, or it 72716911Seric ** is already the canonical name, leave it unchanged. 72816911Seric */ 72910758Seric 73016911Seric /*ARGSUSED*/ 73153751Seric char * 73256823Seric maphostname(map, hbuf, hbsize, avp) 73356823Seric MAP *map; 73416911Seric char *hbuf; 73516911Seric int hbsize; 73653751Seric char **avp; 73716911Seric { 73853751Seric return NULL; 73916911Seric } 74016911Seric 74156795Seric #endif /* DAEMON */ 742