ARRAY8Ptr getLocalAddress (void) { static int haveLocalAddress; if (!haveLocalAddress) { # if defined(IPv6) && defined(AF_INET6) struct addrinfo *ai; if (getaddrinfo(localHostname(), NULL, NULL, &ai) != 0) { XdmcpAllocARRAY8 (&localAddress, 4); localAddress.data[0] = 127; localAddress.data[1] = 0; localAddress.data[2] = 0; localAddress.data[3] = 1; } else { if (ai->ai_addr->sa_family == AF_INET) { XdmcpAllocARRAY8 (&localAddress, sizeof(struct in_addr)); memcpy(localAddress.data, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(struct in_addr)); } else if (ai->ai_addr->sa_family == AF_INET6) { XdmcpAllocARRAY8 (&localAddress, sizeof(struct in6_addr)); memcpy(localAddress.data, &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, sizeof(struct in6_addr)); } freeaddrinfo(ai); } # else struct hostent *hostent; hostent = gethostbyname (localHostname()); if (hostent != NULL) { XdmcpAllocARRAY8 (&localAddress, hostent->h_length); memmove(localAddress.data, hostent->h_addr, hostent->h_length); } else { /* Assume 127.0.0.1 */ XdmcpAllocARRAY8 (&localAddress, 4); localAddress.data[0] = 127; localAddress.data[1] = 0; localAddress.data[2] = 0; localAddress.data[3] = 1; } # endif } return &localAddress; }
void init_session_id(void) { /* Set randomly so we are unlikely to reuse id's from a previous * incarnation so we don't say "Alive" to those displays. * Start with low digits 0 to make debugging easier. */ globalSessionID = (time((Time_t *)0) & 0x7fff) * 16000; Hostname.data = (char *)localHostname(); Hostname.length = strlen(Hostname.data); }
ARRAY8Ptr getLocalAddress () { static int haveLocalAddress; if (!haveLocalAddress) { struct hostent *hostent; hostent = gethostbyname (localHostname()); XdmcpAllocARRAY8 (&localAddress, hostent->h_length); bcopy (hostent->h_addr, localAddress.data, hostent->h_length); } return &localAddress; }
CreateWellKnownSockets () { struct sockaddr_in sock_addr; char *name, *localHostname(); if (request_port == 0) return; Debug ("creating socket %d\n", request_port); xdmcpFd = socket (AF_INET, SOCK_DGRAM, 0); if (xdmcpFd == -1) { LogError (ReadCatalog(MC_LOG_SET,MC_LOG_FAIL_SOCK,MC_DEF_LOG_FAIL_SOCK), request_port); return; } name = localHostname (); registerHostname (name, strlen (name)); RegisterCloseOnFork (xdmcpFd); /* zero out the entire structure; this avoids 4.4 incompatibilities */ bzero ((char *) &sock_addr, sizeof (sock_addr)); #ifdef BSD44SOCKETS sock_addr.sin_len = sizeof(sock_addr); #endif sock_addr.sin_family = AF_INET; sock_addr.sin_port = htons ((short) request_port); sock_addr.sin_addr.s_addr = htonl (INADDR_ANY); if (bind (xdmcpFd, (struct sockaddr *)&sock_addr, sizeof (sock_addr)) == -1) { LogError (ReadCatalog(MC_LOG_SET,MC_LOG_ERR_BIND,MC_DEF_LOG_ERR_BIND), request_port, errno); close (xdmcpFd); xdmcpFd = -1; return; } WellKnownSocketsMax = xdmcpFd; FD_SET (xdmcpFd, &WellKnownSocketsMask); chooserFd = socket (AF_INET, SOCK_STREAM, 0); Debug ("Created chooser socket %d\n", chooserFd); if (chooserFd == -1) { LogError ((unsigned char *)"chooser socket creation failed, errno %d\n", errno); return; } listen (chooserFd, 5); if (chooserFd > WellKnownSocketsMax) WellKnownSocketsMax = chooserFd; FD_SET (chooserFd, &WellKnownSocketsMask); }
CreateWellKnownSockets () { struct sockaddr_in sock_addr; char *name, *localHostname(); if (request_port == 0) return; Debug ("creating socket %d\n", request_port); xdmcpFd = socket (AF_INET, SOCK_DGRAM, 0); if (xdmcpFd == -1) { LogError ("socket creation failed\n"); return; } name = localHostname (); registerHostname (name, strlen (name)); RegisterCloseOnFork (xdmcpFd); /* zero out the entire structure; this avoids 4.4 incompatibilities */ bzero ((char *) &sock_addr, sizeof (sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_port = htons ((short) request_port); sock_addr.sin_addr.s_addr = htonl (INADDR_ANY); if (bind (xdmcpFd, &sock_addr, sizeof (sock_addr)) == -1) { LogError ("error binding socket address %d\n", request_port); close (xdmcpFd); xdmcpFd = -1; return; } WellKnownSocketsMax = xdmcpFd; FD_SET (xdmcpFd, &WellKnownSocketsMask); chooserFd = socket (AF_INET, SOCK_STREAM, 0); Debug ("Created chooser socket %d\n", chooserFd); if (chooserFd == -1) { LogError ("chooser socket creation failed\n"); return; } listen (chooserFd, 5); if (chooserFd > WellKnownSocketsMax) WellKnownSocketsMax = chooserFd; FD_SET (chooserFd, &WellKnownSocketsMask); }
CreateWellKnownSockets () { struct t_bind bind_addr; struct netconfig *nconf; struct nd_hostserv service; struct nd_addrlist *servaddrs; char *name, *localHostname(); char bindbuf[15]; int it; if (request_port == 0) return; Debug ("creating UDP stream %d\n", request_port); nconf = getnetconfigent("udp"); if (!nconf) { t_error("getnetconfigent udp"); return; } xdmcpFd = t_open(nconf->nc_device, O_RDWR, NULL); if (xdmcpFd == -1) { LogError ("XDMCP stream creation failed\n"); t_error ("t_open"); return; } name = localHostname (); registerHostname (name, strlen (name)); RegisterCloseOnFork (xdmcpFd); service.h_host = HOST_SELF; sprintf(bindbuf, "%d", request_port); service.h_serv = bindbuf; netdir_getbyname(nconf, &service, &servaddrs); freenetconfigent(nconf); bind_addr.qlen = 5; bind_addr.addr.buf = servaddrs->n_addrs[0].buf; bind_addr.addr.len = servaddrs->n_addrs[0].len; it = t_bind(xdmcpFd, &bind_addr, &bind_addr); netdir_free(servaddrs, ND_ADDRLIST); if (it < 0) { LogError ("error binding STREAMS address %d\n", request_port); t_error("t_bind"); /* also goes to log file */ t_close (xdmcpFd); xdmcpFd = -1; return; } WellKnownSocketsMax = xdmcpFd; FD_SET (xdmcpFd, &WellKnownSocketsMask); chooserFd = t_open ("/dev/tcp", O_RDWR, NULL); Debug ("Created chooser fd %d\n", chooserFd); if (chooserFd == -1) { LogError ("chooser stream creation failed\n"); t_error("t_open chooser"); return; } if (chooserFd > WellKnownSocketsMax) WellKnownSocketsMax = chooserFd; FD_SET (chooserFd, &WellKnownSocketsMask); }
ARRAY8Ptr getLocalAddress(void) { static int haveLocalAddress; if (!haveLocalAddress) { #if defined(IPv6) && defined(AF_INET6) struct addrinfo *ai; if (getaddrinfo(localHostname(), 0, 0, &ai)) { XdmcpAllocARRAY8(&localAddress, 4); localAddress.data[0] = 127; localAddress.data[1] = 0; localAddress.data[2] = 0; localAddress.data[3] = 1; } else { if (ai->ai_family == AF_INET) { XdmcpAllocARRAY8(&localAddress, sizeof(struct in_addr)); memcpy(localAddress.data, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(struct in_addr)); } else /* if (ai->ai_family == AF_INET6) */ { XdmcpAllocARRAY8(&localAddress, sizeof(struct in6_addr)); memcpy(localAddress.data, &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, sizeof(struct in6_addr)); } freeaddrinfo(ai); #else struct hostent *hostent; if ((hostent = gethostbyname(localHostname()))) { XdmcpAllocARRAY8(&localAddress, hostent->h_length); memmove(localAddress.data, hostent->h_addr, hostent->h_length); #endif haveLocalAddress = True; } } return &localAddress; } void scanAccessDatabase(int force) { struct _displayAddress *da; char *cptr; int nChars, i; debug("scanAccessDatabase\n"); if (Setjmp(cnftalk.errjmp)) return; /* may memleak */ if (startConfig(GC_gXaccess, &accData->dep, force) <= 0) return; free(accData->hostList); accData->nHosts = gRecvInt(); accData->nListens = gRecvInt(); accData->nAliases = gRecvInt(); accData->nAcls = gRecvInt(); nChars = gRecvInt(); if (!(accData->hostList = (HostEntry *) Malloc(accData->nHosts * sizeof(HostEntry) + accData->nListens * sizeof(ListenEntry) + accData->nAliases * sizeof(AliasEntry) + accData->nAcls * sizeof(AclEntry) + nChars))) { closeGetter(); return; } accData->listenList = (ListenEntry *)(accData->hostList + accData->nHosts); accData->aliasList = (AliasEntry *)(accData->listenList + accData->nListens); accData->acList = (AclEntry *)(accData->aliasList + accData->nAliases); cptr = (char *)(accData->acList + accData->nAcls); for (i = 0; i < accData->nHosts; i++) { switch ((accData->hostList[i].type = gRecvInt())) { case HOST_ALIAS: accData->hostList[i].entry.aliasPattern = cptr; cptr += gRecvStrBuf(cptr); break; case HOST_PATTERN: accData->hostList[i].entry.hostPattern = cptr; cptr += gRecvStrBuf(cptr); break; case HOST_ADDRESS: da = &accData->hostList[i].entry.displayAddress; da->hostAddress.data = (unsigned char *)cptr; cptr += (da->hostAddress.length = gRecvArrBuf(cptr)); switch (gRecvInt()) { #ifdef AF_INET case AF_INET: da->connectionType = FamilyInternet; break; #endif #if defined(IPv6) && defined(AF_INET6) case AF_INET6: da->connectionType = FamilyInternet6; break; #endif #ifdef AF_DECnet case AF_DECnet: da->connectionType = FamilyDECnet; break; #endif /*#ifdef AF_UNIX case AF_UNIX: #endif*/ default: da->connectionType = FamilyLocal; break; } break; case HOST_BROADCAST: break; default: logError("Received unknown host type %d from config reader\n", accData->hostList[i].type); return; } } for (i = 0; i < accData->nListens; i++) { accData->listenList[i].iface = gRecvInt(); accData->listenList[i].mcasts = gRecvInt(); accData->listenList[i].nmcasts = gRecvInt(); } for (i = 0; i < accData->nAliases; i++) { accData->aliasList[i].name = cptr; cptr += gRecvStrBuf(cptr); accData->aliasList[i].hosts = gRecvInt(); accData->aliasList[i].nhosts = gRecvInt(); } for (i = 0; i < accData->nAcls; i++) { accData->acList[i].entries = gRecvInt(); accData->acList[i].nentries = gRecvInt(); accData->acList[i].hosts = gRecvInt(); accData->acList[i].nhosts = gRecvInt(); accData->acList[i].flags = gRecvInt(); } } /* Returns True if string is matched by pattern. Does case folding. */ static int patternMatch(const char *string, const char *pattern) { int p, s; if (!string) string = ""; for (;;) { s = *string++; switch (p = *pattern++) { case '*': if (!*pattern) return True; for (string--; *string; string++) if (patternMatch(string, pattern)) return True; return False; case '?': if (s == '\0') return False; break; case '\0': return s == '\0'; case '\\': p = *pattern++; /* fall through */ default: if (tolower(p) != tolower(s)) return False; } } }
char *NetworkAddressToHostname(CARD16 connectionType, ARRAY8Ptr connectionAddress) { switch(connectionType) { case FamilyInternet: #if defined(IPv6) && defined(AF_INET6) case FamilyInternet6: #endif { struct hostent *he; char *myDot, *name, *lname; int af_type; #if defined(IPv6) && defined(AF_INET6) char dotted[INET6_ADDRSTRLEN]; if(connectionType == FamilyInternet6) af_type = AF_INET6; else #endif af_type = AF_INET; he = gethostbyaddr((char *)connectionAddress->data, connectionAddress->length, af_type); if(he) { #if defined(IPv6) && defined(AF_INET6) struct addrinfo *ai, *nai; if(!getaddrinfo(he->h_name, NULL, NULL, &ai)) { for(nai = ai; nai; nai = nai->ai_next) { if(af_type == nai->ai_family && !memcmp(nai->ai_family == AF_INET ? (char *)&((struct sockaddr_in *)nai->ai_addr)->sin_addr : (char *)&((struct sockaddr_in6 *)nai->ai_addr)->sin6_addr, connectionAddress->data, connectionAddress->length)) { freeaddrinfo(ai); goto oki; } } freeaddrinfo(ai); #else if((he = gethostbyname(he->h_name)) && he->h_addrtype == AF_INET) { int i; for(i = 0; he->h_addr_list[i]; i++) if(!memcmp(he->h_addr_list[i], connectionAddress->data, 4)) goto oki; #endif LogError("DNS spoof attempt or misconfigured resolver.\n"); } goto gotnone; oki: if(StrDup(&name, he->h_name) && !strchr(name, '.') && (myDot = strchr(localHostname(), '.'))) { if(ASPrintf(&lname, "%s%s", name, myDot)) { #if defined(IPv6) && defined(AF_INET6) if(!getaddrinfo(lname, NULL, NULL, &ai)) { for(nai = ai; nai; nai = nai->ai_next) { if(af_type == nai->ai_family && !memcmp(nai->ai_family == AF_INET ? (char *)&((struct sockaddr_in *)nai->ai_addr)->sin_addr : (char *)&((struct sockaddr_in6 *)nai->ai_addr)->sin6_addr, connectionAddress->data, connectionAddress->length)) { freeaddrinfo(ai); free(name); return lname; } } freeaddrinfo(ai); } #else if((he = gethostbyname(lname)) && he->h_addrtype == AF_INET) { int i; for(i = 0; he->h_addr_list[i]; i++) if(!memcmp(he->h_addr_list[i], connectionAddress->data, 4)) { free(name); return lname; } } #endif free(lname); } } } else { gotnone: /* can't get name, so use emergency fallback */ #if defined(IPv6) && defined(AF_INET6) inet_ntop(af_type, connectionAddress->data, dotted, sizeof(dotted)); StrDup(&name, dotted); #else ASPrintf(&name, "%[4|'.'hhu", connectionAddress->data); #endif LogWarn("Cannot convert Internet address %s to host name\n", name); } return name; } #ifdef DNET case FamilyDECnet: break; #endif /* DNET */ default: break; } return 0; }
static char *NetworkAddressToName(CARD16 connectionType, ARRAY8Ptr connectionAddress, struct sockaddr *originalAddress, CARD16 displayNumber) { switch(connectionType) { case FamilyInternet: #if defined(IPv6) && defined(AF_INET6) case FamilyInternet6: #endif { CARD8 *data; struct hostent *hostent; char *hostname = NULL; char *name; const char *localhost; int multiHomed = 0; int type; #if defined(IPv6) && defined(AF_INET6) struct addrinfo *ai = NULL, *nai, hints; char dotted[INET6_ADDRSTRLEN]; if(connectionType == FamilyInternet6) type = AF_INET6; else #endif type = AF_INET; data = connectionAddress->data; hostent = gethostbyaddr((char *)data, connectionAddress->length, type); if(hostent) { if(sourceAddress) { #if defined(IPv6) && defined(AF_INET6) bzero(&hints, sizeof(hints)); hints.ai_flags = AI_CANONNAME; if(!getaddrinfo(hostent->h_name, NULL, &hints, &ai)) { hostname = ai->ai_canonname; for(nai = ai->ai_next; nai; nai = nai->ai_next) if(ai->ai_protocol == nai->ai_protocol && memcmp(ai->ai_addr, nai->ai_addr, ai->ai_addrlen)) multiHomed = 1; } #else hostent = gethostbyname(hostent->h_name); if(hostent && hostent->h_addrtype == AF_INET) { multiHomed = hostent->h_addr_list[1] != NULL; hostname = hostent->h_name; } #endif } else hostname = hostent->h_name; } localhost = localHostname(); /* * protect against bogus host names */ if(hostname && *hostname && *hostname != '.' && !multiHomed) { if(!strcmp(localhost, hostname)) ASPrintf(&name, "localhost:%d", displayNumber); else { if(removeDomainname) { char *localDot, *remoteDot; /* check for a common domain name. This * could reduce names by recognising common * super-domain names as well, but I don't think * this is as useful, and will confuse more * people */ if((localDot = strchr(localhost, '.')) && (remoteDot = strchr(hostname, '.'))) { /* smash the name in place; it won't * be needed later. */ if(!strcmp(localDot + 1, remoteDot + 1)) *remoteDot = '\0'; } } ASPrintf(&name, "%s:%d", hostname, displayNumber); } } else { #if defined(IPv6) && defined(AF_INET6) if(multiHomed) { if(connectionType == FamilyInternet) { data = (CARD8 *)&((struct sockaddr_in *)originalAddress)->sin_addr; } else { data = (CARD8 *)&((struct sockaddr_in6 *)originalAddress)->sin6_addr; } } inet_ntop(type, data, dotted, sizeof(dotted)); ASPrintf(&name, "%s:%d", dotted, displayNumber); #else if(multiHomed) data = (CARD8 *)&((struct sockaddr_in *)originalAddress)->sin_addr; ASPrintf(&name, "%[4|'.'hhu:%d", data, displayNumber); #endif } #if defined(IPv6) && defined(AF_INET6) if(ai) freeaddrinfo(ai); #endif return name; } #ifdef DNET case FamilyDECnet: return NULL; #endif /* DNET */ default: return NULL; } }