]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ndr/nbt: use ndr_dns_utils/ndr_pull_dns_string_list
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Tue, 19 May 2020 01:55:53 +0000 (13:55 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Thu, 16 Apr 2026 00:54:43 +0000 (00:54 +0000)
To retain exactly the same behaviour with regard to memory contexts
and error messages, we add an is_nbt flag.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14378

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
librpc/ndr/ndr_dns.c
librpc/ndr/ndr_dns_utils.c
librpc/ndr/ndr_dns_utils.h
librpc/ndr/ndr_nbt.c

index b36927dc599da5c658edb8346832b50fba2698ce..6822d739cbe34e19d30a0e4cfa078ac8782945cb 100644 (file)
@@ -55,7 +55,8 @@ _PUBLIC_ enum ndr_err_code ndr_pull_dns_string(struct ndr_pull *ndr,
 {
        return ndr_pull_dns_string_list(ndr,
                                        ndr_flags,
-                                       s);
+                                       s,
+                                       false);
 }
 
 
index 7a38a807e5c0cf321bb7a42b498528b4b87d8081..00b7f8ce86aaf72499456dc5d49e7054f2883b68 100644 (file)
@@ -9,19 +9,22 @@
 #define MAX_COMPONENTS 128
 
 /*
-  pull one component of a dns_string
+  pull one component of a dns/nbt string
 */
 static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
+                                           TALLOC_CTX *mem_ctx,
                                            uint8_t **component,
                                            uint32_t *offset,
-                                           uint32_t *max_offset)
+                                           uint32_t *max_offset,
+                                           const char *err_name)
 {
        uint8_t len;
        unsigned int loops = 0;
        while (loops < 5) {
                if (*offset >= ndr->data_size) {
                        return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                       "BAD DNS NAME component, bad offset");
+                                           "BAD %s NAME component, bad offset",
+                                           err_name);
                }
                len = ndr->data[*offset];
                if (len == 0) {
@@ -34,8 +37,9 @@ static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
                        /* its a label pointer */
                        if (1 + *offset >= ndr->data_size) {
                                return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                               "BAD DNS NAME component, " \
-                                               "bad label offset");
+                                                    "BAD %s NAME component, " \
+                                                    "bad label offset",
+                                                    err_name);
                        }
                        *max_offset = MAX(*max_offset, *offset + 2);
                        *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
@@ -46,16 +50,17 @@ static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
                if ((len & 0xC0) != 0) {
                        /* its a reserved length field */
                        return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                             "BAD DNS NAME component, " \
+                                             "BAD %s NAME component, " \
                                              "reserved length field: 0x%02x",
-                                             (len &0xC));
+                                             err_name, (len &0xC));
                }
                if (*offset + len + 1 > ndr->data_size) {
                        return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                             "BAD DNS NAME component, "\
-                                             "length too long");
+                                             "BAD %s NAME component, "\
+                                             "length too long",
+                                             err_name);
                }
-               *component = (uint8_t*)talloc_strndup(ndr,
+               *component = (uint8_t*)talloc_strndup(mem_ctx,
                                (const char *)&ndr->data[1 + *offset], len);
                NDR_ERR_HAVE_NO_MEMORY(*component);
                *offset += len + 1;
@@ -65,20 +70,32 @@ static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
 
        /* too many pointers */
        return ndr_pull_error(ndr, NDR_ERR_STRING,
-                             "BAD DNS NAME component, too many pointers");
+                             "BAD %s NAME component, too many pointers",
+                             err_name);
 }
 
 /**
-  pull a dns_string from the wire
+  pull a dns/nbt string from the wire
 */
 enum ndr_err_code ndr_pull_dns_string_list(struct ndr_pull *ndr,
                                           ndr_flags_type ndr_flags,
-                                          const char **s)
+                                          const char **s,
+                                          bool is_nbt)
 {
        uint32_t offset = ndr->offset;
        uint32_t max_offset = offset;
        unsigned num_components;
        char *name;
+       const char *err_name = NULL;
+       TALLOC_CTX *mem_ctx = NULL;
+
+       if (is_nbt) {
+               err_name = "NBT";
+               mem_ctx = ndr->current_mem_ctx;
+       } else {
+               err_name = "DNS";
+               mem_ctx = ndr;
+       }
 
        if (!(ndr_flags & NDR_SCALARS)) {
                return NDR_ERR_SUCCESS;
@@ -87,12 +104,18 @@ enum ndr_err_code ndr_pull_dns_string_list(struct ndr_pull *ndr,
        name = talloc_strdup(ndr->current_mem_ctx, "");
 
        /* break up name into a list of components */
-       for (num_components=0; num_components<MAX_COMPONENTS;
+       for (num_components = 0;
+            num_components < MAX_COMPONENTS;
             num_components++) {
                uint8_t *component = NULL;
-               NDR_CHECK(ndr_pull_component(ndr, &component, &offset,
-                                            &max_offset));
-               if (component == NULL) break;
+               NDR_CHECK(ndr_pull_component(ndr,
+                                            mem_ctx,
+                                            &component, &offset,
+                                            &max_offset,
+                                            err_name));
+               if (component == NULL) {
+                       break;
+               }
                if (num_components > 0) {
                        name = talloc_asprintf_append_buffer(name, ".%s",
                                                             component);
@@ -104,7 +127,8 @@ enum ndr_err_code ndr_pull_dns_string_list(struct ndr_pull *ndr,
        }
        if (num_components == MAX_COMPONENTS) {
                return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                     "BAD DNS NAME too many components");
+                                     "BAD %s NAME too many components",
+                                     err_name);
        }
 
        (*s) = name;
index f75a628852cae39e883e667b4faf2210696f5fbf..8b9780feb3ed060fb2838704ca642252eefc0eb4 100644 (file)
@@ -1,7 +1,8 @@
 
 enum ndr_err_code ndr_pull_dns_string_list(struct ndr_pull *ndr,
                                           ndr_flags_type ndr_flags,
-                                          const char **s);
+                                          const char **s,
+                                          bool is_nbt);
 
 enum ndr_err_code ndr_push_dns_string_list(struct ndr_push *ndr,
                                           struct ndr_token_list *string_list,
index 6f54198ffbce89691dfd42b6590d8fd353abe708..f9c32c13304bc4477cb2e9b02b15991063760847 100644 (file)
@@ -28,9 +28,6 @@
 #include "ndr_dns_utils.h"
 
 
-/* don't allow an unlimited number of name components */
-#define MAX_COMPONENTS 128
-
 /**
   print a nbt string
 */
@@ -39,103 +36,15 @@ _PUBLIC_ void ndr_print_nbt_string(struct ndr_print *ndr, const char *name, cons
        ndr_print_string(ndr, name, s);
 }
 
-/*
-  pull one component of a nbt_string
-*/
-static enum ndr_err_code ndr_pull_component(struct ndr_pull *ndr,
-                                           uint8_t **component,
-                                           uint32_t *offset,
-                                           uint32_t *max_offset)
-{
-       uint8_t len;
-       unsigned int loops = 0;
-       while (loops < 5) {
-               if (*offset >= ndr->data_size) {
-                       return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                             "BAD NBT NAME component");
-               }
-               len = ndr->data[*offset];
-               if (len == 0) {
-                       *offset += 1;
-                       *max_offset = MAX(*max_offset, *offset);
-                       *component = NULL;
-                       return NDR_ERR_SUCCESS;
-               }
-               if ((len & 0xC0) == 0xC0) {
-                       /* its a label pointer */
-                       if (1 + *offset >= ndr->data_size) {
-                               return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                                     "BAD NBT NAME component");
-                       }
-                       *max_offset = MAX(*max_offset, *offset + 2);
-                       *offset = ((len&0x3F)<<8) | ndr->data[1 + *offset];
-                       *max_offset = MAX(*max_offset, *offset);
-                       loops++;
-                       continue;
-               }
-               if ((len & 0xC0) != 0) {
-                       /* its a reserved length field */
-                       return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                             "BAD NBT NAME component");
-               }
-               if (*offset + len + 1 > ndr->data_size) {
-                       return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                             "BAD NBT NAME component");
-               }
-               *component = (uint8_t*)talloc_strndup(
-                       ndr->current_mem_ctx,
-                       (const char *)&ndr->data[1 + *offset], len);
-               NDR_ERR_HAVE_NO_MEMORY(*component);
-               *offset += len + 1;
-               *max_offset = MAX(*max_offset, *offset);
-               return NDR_ERR_SUCCESS;
-       }
-
-       /* too many pointers */
-       return ndr_pull_error(ndr, NDR_ERR_STRING, "BAD NBT NAME component");
-}
-
 /**
   pull a nbt_string from the wire
 */
 _PUBLIC_ enum ndr_err_code ndr_pull_nbt_string(struct ndr_pull *ndr, ndr_flags_type ndr_flags, const char **s)
 {
-       uint32_t offset = ndr->offset;
-       uint32_t max_offset = offset;
-       unsigned num_components;
-       char *name;
-
-       if (!(ndr_flags & NDR_SCALARS)) {
-               return NDR_ERR_SUCCESS;
-       }
-
-       name = NULL;
-
-       /* break up name into a list of components */
-       for (num_components=0;num_components<MAX_COMPONENTS;num_components++) {
-               uint8_t *component = NULL;
-               NDR_CHECK(ndr_pull_component(ndr, &component, &offset, &max_offset));
-               if (component == NULL) break;
-               if (name) {
-                       name = talloc_asprintf_append_buffer(name, ".%s", component);
-                       NDR_ERR_HAVE_NO_MEMORY(name);
-               } else {
-                       name = (char *)component;
-               }
-       }
-       if (num_components == MAX_COMPONENTS) {
-               return ndr_pull_error(ndr, NDR_ERR_STRING,
-                                     "BAD NBT NAME too many components");
-       }
-       if (num_components == 0) {
-               name = talloc_strdup(ndr->current_mem_ctx, "");
-               NDR_ERR_HAVE_NO_MEMORY(name);
-       }
-
-       (*s) = name;
-       ndr->offset = max_offset;
-
-       return NDR_ERR_SUCCESS;
+       return ndr_pull_dns_string_list(ndr,
+                                       ndr_flags,
+                                       s,
+                                       true);
 }
 
 /**