libcli/nbt Move more of lmhosts lookup into common code
authorAndrew Bartlett <abartlet@samba.org>
Tue, 3 Nov 2009 03:15:07 +0000 (14:15 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 4 Nov 2009 03:58:25 +0000 (14:58 +1100)
This aims to eventually share this with Samba4.

Andrew Bartlett

libcli/nbt/libnbt.h
libcli/nbt/lmhosts.c
source3/libsmb/namequery.c

index 2abcb5697130663ebb87d8a730f296216331683b..6d6a4a4696ba7cb27227a22f87bf29cc9981857f 100644 (file)
@@ -360,4 +360,10 @@ bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type,
                struct sockaddr_storage *pss);
 void endlmhosts(XFILE *fp);
 
+NTSTATUS resolve_lmhosts_file_as_sockaddr(const char *lmhosts_file, 
+                                         const char *name, int name_type,
+                                         TALLOC_CTX *mem_ctx, 
+                                         struct sockaddr_storage **return_iplist,
+                                         int *return_count);
+
 #endif /* __LIBNBT_H__ */
index 11703a27e80c3b90d04eb1b6c4145ecb414f8e28..317ccc556c73e06424561a0ca10908ca468c3569 100644 (file)
@@ -155,3 +155,82 @@ void endlmhosts(XFILE *fp)
        x_fclose(fp);
 }
 
+/********************************************************
+ Resolve via "lmhosts" method.
+*********************************************************/
+
+NTSTATUS resolve_lmhosts_file_as_sockaddr(const char *lmhosts_file, 
+                                         const char *name, int name_type,
+                                         TALLOC_CTX *mem_ctx, 
+                                         struct sockaddr_storage **return_iplist,
+                                         int *return_count)
+{
+       /*
+        * "lmhosts" means parse the local lmhosts file.
+        */
+
+       XFILE *fp;
+       char *lmhost_name = NULL;
+       int name_type2;
+       struct sockaddr_storage return_ss;
+       NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
+       TALLOC_CTX *ctx = NULL;
+
+       *return_iplist = NULL;
+       *return_count = 0;
+
+       DEBUG(3,("resolve_lmhosts: "
+               "Attempting lmhosts lookup for name %s<0x%x>\n",
+               name, name_type));
+
+       fp = startlmhosts(lmhosts_file);
+
+       if ( fp == NULL )
+               return NT_STATUS_NO_SUCH_FILE;
+
+       ctx = talloc_new(mem_ctx);
+       if (!ctx) {
+               endlmhosts(fp);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       while (getlmhostsent(ctx, fp, &lmhost_name, &name_type2, &return_ss)) {
+
+               if (!strequal(name, lmhost_name)) {
+                       TALLOC_FREE(lmhost_name);
+                       continue;
+               }
+
+               if ((name_type2 != -1) && (name_type != name_type2)) {
+                       TALLOC_FREE(lmhost_name);
+                       continue;
+               }
+               
+               *return_iplist = talloc_realloc(ctx, (*return_iplist), 
+                                               struct sockaddr_storage,
+                                               (*return_count)+1);
+
+               if ((*return_iplist) == NULL) {
+                       TALLOC_FREE(ctx);
+                       endlmhosts(fp);
+                       DEBUG(3,("resolve_lmhosts: talloc_realloc fail !\n"));
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               (*return_iplist)[*return_count] = return_ss;
+               *return_count += 1;
+
+               /* we found something */
+               status = NT_STATUS_OK;
+
+               /* Multiple names only for DC lookup */
+               if (name_type != 0x1c)
+                       break;
+       }
+
+       talloc_steal(mem_ctx, *return_iplist);
+       TALLOC_FREE(ctx);
+       endlmhosts(fp);
+       return status;
+}
+
index 930f0a54f4cffab1986736fa9aa3f9046229d856..a6fc612a0f425d77a64ef0f4b3439aa582d0c7f4 100644 (file)
@@ -1101,11 +1101,7 @@ static NTSTATUS resolve_lmhosts(const char *name, int name_type,
        /*
         * "lmhosts" means parse the local lmhosts file.
         */
-
-       XFILE *fp;
-       char *lmhost_name = NULL;
-       int name_type2;
-       struct sockaddr_storage return_ss;
+       struct sockaddr_storage *ss_list;
        NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
        TALLOC_CTX *ctx = NULL;
 
@@ -1116,54 +1112,28 @@ static NTSTATUS resolve_lmhosts(const char *name, int name_type,
                "Attempting lmhosts lookup for name %s<0x%x>\n",
                name, name_type));
 
-       fp = startlmhosts(get_dyn_LMHOSTSFILE());
-
-       if ( fp == NULL )
-               return NT_STATUS_NO_SUCH_FILE;
-
        ctx = talloc_init("resolve_lmhosts");
        if (!ctx) {
-               endlmhosts(fp);
                return NT_STATUS_NO_MEMORY;
        }
 
-       while (getlmhostsent(ctx, fp, &lmhost_name, &name_type2, &return_ss)) {
-
-               if (!strequal(name, lmhost_name)) {
-                       TALLOC_FREE(lmhost_name);
-                       continue;
-               }
-
-               if ((name_type2 != -1) && (name_type != name_type2)) {
-                       TALLOC_FREE(lmhost_name);
-                       continue;
-               }
-
-               *return_iplist = SMB_REALLOC_ARRAY((*return_iplist),
-                                       struct ip_service,
-                                       (*return_count)+1);
-
-               if ((*return_iplist) == NULL) {
-                       TALLOC_FREE(ctx);
-                       endlmhosts(fp);
-                       DEBUG(3,("resolve_lmhosts: malloc fail !\n"));
+       status = resolve_lmhosts_file_as_sockaddr(get_dyn_LMHOSTSFILE(), 
+                                                 name, name_type, 
+                                                 ctx, 
+                                                 &ss_list, 
+                                                 return_count);
+       if (NT_STATUS_IS_OK(status)) {
+               if (convert_ss2service(return_iplist, 
+                                      ss_list,
+                                      *return_count)) {
+                       talloc_free(ctx);
+                       return NT_STATUS_OK;
+               } else {
+                       talloc_free(ctx);
                        return NT_STATUS_NO_MEMORY;
                }
-
-               (*return_iplist)[*return_count].ss = return_ss;
-               (*return_iplist)[*return_count].port = PORT_NONE;
-               *return_count += 1;
-
-               /* we found something */
-               status = NT_STATUS_OK;
-
-               /* Multiple names only for DC lookup */
-               if (name_type != 0x1c)
-                       break;
        }
-
-       TALLOC_FREE(ctx);
-       endlmhosts(fp);
+       talloc_free(ctx);
        return status;
 }