From 668470c9923364c6c43afbf94162b549c8baef9a Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 15 Sep 2009 07:42:54 -0700 Subject: libcli:nbt make the lmhosts parsing code and dependicies common This starts the process to have Samba4 use lmhosts. Andrew Bartlett --- source3/Makefile.in | 2 +- source3/include/proto.h | 9 ---- source3/lib/util_sock.c | 115 ---------------------------------------- source3/libsmb/namequery.c | 128 --------------------------------------------- 4 files changed, 1 insertion(+), 253 deletions(-) (limited to 'source3') diff --git a/source3/Makefile.in b/source3/Makefile.in index 1908257c93..a89bbae6ab 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -455,7 +455,7 @@ LIBNBT_OBJ = ../libcli/nbt/nbtname.o \ ../librpc/ndr/ndr_svcctl.o LIBNMB_OBJ = libsmb/unexpected.o libsmb/namecache.o libsmb/nmblib.o \ - libsmb/namequery.o libsmb/conncache.o libads/dns.o + libsmb/namequery.o ../libcli/nbt/lmhosts.o libsmb/conncache.o libads/dns.o NTERR_OBJ = libsmb/nterr.o libsmb/smberr.o DOSERR_OBJ = ../libcli/util/doserr.o diff --git a/source3/include/proto.h b/source3/include/proto.h index 5da13ca0d7..b037bf3ac0 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -1342,20 +1342,11 @@ NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx, /* The following definitions come from lib/util_sock.c */ -bool interpret_string_addr_internal(struct addrinfo **ppres, - const char *str, int flags); bool is_broadcast_addr(const struct sockaddr *pss); -bool interpret_string_addr(struct sockaddr_storage *pss, - const char *str, - int flags); -bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss, - const char *str, - int flags); bool is_loopback_ip_v4(struct in_addr ip); bool is_loopback_addr(const struct sockaddr *pss); bool is_zero_addr(const struct sockaddr *pss); void zero_ip_v4(struct in_addr *ip); -void zero_sockaddr(struct sockaddr_storage *pss); void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss, struct in_addr ip); bool same_net(const struct sockaddr *ip1, diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 6cc2e53811..08cbced1e5 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -21,121 +21,6 @@ #include "includes.h" -/******************************************************************* - Map a text hostname or IP address (IPv4 or IPv6) into a - struct sockaddr_storage. Takes a flag which allows it to - prefer an IPv4 address (needed for DC's). -******************************************************************/ - -static bool interpret_string_addr_pref(struct sockaddr_storage *pss, - const char *str, - int flags, - bool prefer_ipv4) -{ - struct addrinfo *res = NULL; -#if defined(HAVE_IPV6) - char addr[INET6_ADDRSTRLEN]; - unsigned int scope_id = 0; - - if (strchr_m(str, ':')) { - char *p = strchr_m(str, '%'); - - /* - * Cope with link-local. - * This is IP:v6:addr%ifname. - */ - - if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) { - strlcpy(addr, str, - MIN(PTR_DIFF(p,str)+1, - sizeof(addr))); - str = addr; - } - } -#endif - - zero_sockaddr(pss); - - if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) { - return false; - } - if (!res) { - return false; - } - - if (prefer_ipv4) { - struct addrinfo *p; - - for (p = res; p; p = p->ai_next) { - if (p->ai_family == AF_INET) { - memcpy(pss, p->ai_addr, p->ai_addrlen); - break; - } - } - if (p == NULL) { - /* Copy the first sockaddr. */ - memcpy(pss, res->ai_addr, res->ai_addrlen); - } - } else { - /* Copy the first sockaddr. */ - memcpy(pss, res->ai_addr, res->ai_addrlen); - } - -#if defined(HAVE_IPV6) - if (pss->ss_family == AF_INET6 && scope_id) { - struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss; - if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) && - ps6->sin6_scope_id == 0) { - ps6->sin6_scope_id = scope_id; - } - } -#endif - - freeaddrinfo(res); - return true; -} - -/******************************************************************* - Map a text hostname or IP address (IPv4 or IPv6) into a - struct sockaddr_storage. Address agnostic version. -******************************************************************/ - -bool interpret_string_addr(struct sockaddr_storage *pss, - const char *str, - int flags) -{ - return interpret_string_addr_pref(pss, - str, - flags, - false); -} - -/******************************************************************* - Map a text hostname or IP address (IPv4 or IPv6) into a - struct sockaddr_storage. Version that prefers IPv4. -******************************************************************/ - -bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss, - const char *str, - int flags) -{ - return interpret_string_addr_pref(pss, - str, - flags, - true); -} - -/******************************************************************* - Set an address to INADDR_ANY. -******************************************************************/ - -void zero_sockaddr(struct sockaddr_storage *pss) -{ - memset(pss, '\0', sizeof(*pss)); - /* Ensure we're at least a valid sockaddr-storage. */ - pss->ss_family = AF_INET; -} - /**************************************************************************** Get a port number in host byte order from a sockaddr_storage. ****************************************************************************/ diff --git a/source3/libsmb/namequery.c b/source3/libsmb/namequery.c index 1a641ac791..930f0a54f4 100644 --- a/source3/libsmb/namequery.c +++ b/source3/libsmb/namequery.c @@ -846,134 +846,6 @@ struct sockaddr_storage *name_query(int fd, return ss_list; } -/******************************************************** - Start parsing the lmhosts file. -*********************************************************/ - -XFILE *startlmhosts(const char *fname) -{ - XFILE *fp = x_fopen(fname,O_RDONLY, 0); - if (!fp) { - DEBUG(4,("startlmhosts: Can't open lmhosts file %s. " - "Error was %s\n", - fname, strerror(errno))); - return NULL; - } - return fp; -} - -/******************************************************** - Parse the next line in the lmhosts file. -*********************************************************/ - -bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type, - struct sockaddr_storage *pss) -{ - char line[1024]; - - *pp_name = NULL; - - while(!x_feof(fp) && !x_ferror(fp)) { - char *ip = NULL; - char *flags = NULL; - char *extra = NULL; - char *name = NULL; - const char *ptr; - char *ptr1 = NULL; - int count = 0; - - *name_type = -1; - - if (!fgets_slash(line,sizeof(line),fp)) { - continue; - } - - if (*line == '#') { - continue; - } - - ptr = line; - - if (next_token_talloc(ctx, &ptr, &ip, NULL)) - ++count; - if (next_token_talloc(ctx, &ptr, &name, NULL)) - ++count; - if (next_token_talloc(ctx, &ptr, &flags, NULL)) - ++count; - if (next_token_talloc(ctx, &ptr, &extra, NULL)) - ++count; - - if (count <= 0) - continue; - - if (count > 0 && count < 2) { - DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n", - line)); - continue; - } - - if (count >= 4) { - DEBUG(0,("getlmhostsent: too many columns " - "in lmhosts file (obsolete syntax)\n")); - continue; - } - - if (!flags) { - flags = talloc_strdup(ctx, ""); - if (!flags) { - continue; - } - } - - DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", - ip, name, flags)); - - if (strchr_m(flags,'G') || strchr_m(flags,'S')) { - DEBUG(0,("getlmhostsent: group flag " - "in lmhosts ignored (obsolete)\n")); - continue; - } - - if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) { - DEBUG(0,("getlmhostsent: invalid address " - "%s.\n", ip)); - } - - /* Extra feature. If the name ends in '#XX', - * where XX is a hex number, then only add that name type. */ - if((ptr1 = strchr_m(name, '#')) != NULL) { - char *endptr; - ptr1++; - - *name_type = (int)strtol(ptr1, &endptr, 16); - if(!*ptr1 || (endptr == ptr1)) { - DEBUG(0,("getlmhostsent: invalid name " - "%s containing '#'.\n", name)); - continue; - } - - *(--ptr1) = '\0'; /* Truncate at the '#' */ - } - - *pp_name = talloc_strdup(ctx, name); - if (!*pp_name) { - return false; - } - return true; - } - - return false; -} - -/******************************************************** - Finish parsing the lmhosts file. -*********************************************************/ - -void endlmhosts(XFILE *fp) -{ - x_fclose(fp); -} - /******************************************************** convert an array if struct sockaddr_storage to struct ip_service return false on failure. Port is set to PORT_NONE; -- cgit