r15274: Drop default EXT_LIB_ prefix for external libraries. Fixes issues with local
[ira/wip.git] / source / utils / ndrdump.c
index 826f0c6c6e812fbccf55c54c2e16ab13db781f01..117029b6bc575d6938a0d0c673286d91f5c3ed15 100644 (file)
 #include "includes.h"
 #include "lib/cmdline/popt_common.h"
 #include "system/iconv.h"
-#include "librpc/gen_ndr/tables.h"
-
-static const struct dcerpc_interface_table *find_pipe(const char *pipe_name)
-{
-       int i;
-       for (i=0;dcerpc_pipes[i];i++) {
-               if (strcmp(dcerpc_pipes[i]->name, pipe_name) == 0) {
-                       break;
-               }
-       }
-       if (!dcerpc_pipes[i]) {
-               printf("pipe '%s' not in table\n", pipe_name);
-               exit(1);
-       }
-       return dcerpc_pipes[i];
-}
+#include "system/filesys.h"
+#include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/dcerpc_table.h"
 
 static const struct dcerpc_interface_call *find_function(
        const struct dcerpc_interface_table *p,
@@ -62,14 +49,14 @@ static const struct dcerpc_interface_call *find_function(
 
 static void show_pipes(void)
 {
-       int i;
+       const struct dcerpc_interface_list *l;
        printf("\nYou must specify a pipe\n");
        printf("known pipes are:\n");
-       for (i=0;dcerpc_pipes[i];i++) {
-               if(dcerpc_pipes[i]->helpstring) {
-                       printf("\t%s - %s\n", dcerpc_pipes[i]->name, dcerpc_pipes[i]->helpstring);
+       for (l=librpc_dcerpc_pipes();l;l=l->next) {
+               if(l->table->helpstring) {
+                       printf("\t%s - %s\n", l->table->name, l->table->helpstring);
                } else {
-                       printf("\t%s\n", dcerpc_pipes[i]->name);
+                       printf("\t%s\n", l->table->name);
                }
        }
        exit(1);
@@ -86,38 +73,95 @@ static void show_functions(const struct dcerpc_interface_table *p)
        exit(1);
 }
 
- int main(int argc, const char *argv[])
+static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
+{
+       int num_read, total_len = 0;
+       char buf[255];
+       char *result = NULL;
+
+       while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
+
+               if (result) {
+                       result = (char *) talloc_realloc(
+                               mem_ctx, result, char *, total_len + num_read);
+               } else {
+                       result = talloc_size(mem_ctx, num_read);
+               }
+
+               memcpy(result + total_len, buf, num_read);
+
+               total_len += num_read;
+       }
+
+       if (size)
+               *size = total_len;
+
+       return result;
+}
+
+const struct dcerpc_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
 {
        const struct dcerpc_interface_table *p;
+       void *handle;
+       char *symbol;
+
+       handle = dlopen(plugin, RTLD_NOW);
+       if (handle == NULL) {
+               printf("%s: Unable to open: %s\n", plugin, dlerror());
+               return NULL;
+       }
+
+       symbol = talloc_asprintf(NULL, "dcerpc_table_%s", pipe_name);
+       p = dlsym(handle, symbol);
+
+       if (!p) {
+               printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
+               talloc_free(symbol);
+               return NULL;
+       }
+
+       talloc_free(symbol);
+       
+       return p;
+}
+
+ int main(int argc, const char *argv[])
+{
+       const struct dcerpc_interface_table *p = NULL;
        const struct dcerpc_interface_call *f;
        const char *pipe_name, *function, *inout, *filename;
-       char *data;
+       uint8_t *data;
        size_t size;
        DATA_BLOB blob;
-       struct ndr_pull *ndr;
+       struct ndr_pull *ndr_pull;
+       struct ndr_print *ndr_print;
        TALLOC_CTX *mem_ctx;
        int flags;
        poptContext pc;
        NTSTATUS status;
        void *st;
+       void *v_st;
        const char *ctx_filename = NULL;
+       const char *plugin = NULL;
+       BOOL validate = False;
+       BOOL dumpdata = False;
        int opt;
-       struct ndr_print *pr;
        struct poptOption long_options[] = {
                {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
+               {"validate", 0, POPT_ARG_NONE, &validate, 0, "try to validate the data", NULL },        
+               {"dump-data", 0, POPT_ARG_NONE, &dumpdata, 0, "dump the hex data", NULL },      
+               {"load-dso", 'l', POPT_ARG_STRING, &plugin, 0, "load from shared object file", NULL },
+               POPT_COMMON_SAMBA
                POPT_AUTOHELP
                POPT_TABLEEND
        };
 
-       ndrdump_init_subsystems;
-
-       DEBUGLEVEL = 10;
-
-       setup_logging("ndrdump", DEBUG_STDOUT);
+       dcerpc_table_init();
 
        pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
        
-       poptSetOtherOptionHelp(pc, "<pipe> <function> <inout> <filename>");
+       poptSetOtherOptionHelp(
+               pc, "<pipe|uuid> <function> <inout> [<filename>]");
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
        }
@@ -130,13 +174,34 @@ static void show_functions(const struct dcerpc_interface_table *p)
                exit(1);
        }
 
-       p = find_pipe(pipe_name);
+       if (plugin != NULL) {
+               p = load_iface_from_plugin(plugin, pipe_name);
+       }
+
+       if (!p) {
+               p = idl_iface_by_name(pipe_name);
+       }
+
+       if (!p) {
+               struct GUID uuid;
+
+               status = GUID_from_string(pipe_name, &uuid);
+
+               if (NT_STATUS_IS_OK(status)) {
+                       p = idl_iface_by_uuid(&uuid);
+               }
+       }
+
+       if (!p) {
+               printf("Unknown pipe or UUID '%s'\n", pipe_name);
+               exit(1);
+       }
 
        function = poptGetArg(pc);
        inout = poptGetArg(pc);
        filename = poptGetArg(pc);
 
-       if (!function || !inout || !filename) {
+       if (!function || !inout) {
                poptPrintUsage(pc, stderr, 0);
                show_functions(p);
                exit(1);
@@ -157,9 +222,15 @@ static void show_functions(const struct dcerpc_interface_table *p)
 
        mem_ctx = talloc_init("ndrdump");
 
-       st = talloc_zero(mem_ctx, f->struct_size);
+       st = talloc_zero_size(mem_ctx, f->struct_size);
        if (!st) {
-               printf("Unable to allocate %d bytes\n", f->struct_size);
+               printf("Unable to allocate %d bytes\n", (int)f->struct_size);
+               exit(1);
+       }
+
+       v_st = talloc_zero_size(mem_ctx, f->struct_size);
+       if (!v_st) {
+               printf("Unable to allocate %d bytes\n", (int)f->struct_size);
                exit(1);
        }
 
@@ -169,7 +240,7 @@ static void show_functions(const struct dcerpc_interface_table *p)
                        exit(1);
                }
                        
-               data = file_load(ctx_filename, &size);
+               data = (uint8_t *)file_load(ctx_filename, &size, mem_ctx);
                if (!data) {
                        perror(ctx_filename);
                        exit(1);
@@ -178,59 +249,120 @@ static void show_functions(const struct dcerpc_interface_table *p)
                blob.data = data;
                blob.length = size;
 
-               ndr = ndr_pull_init_blob(&blob, mem_ctx);
+               ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
+               ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
 
-               status = f->ndr_pull(ndr, NDR_IN, st);
+               status = f->ndr_pull(ndr_pull, NDR_IN, st);
 
-               if (ndr->offset != ndr->data_size) {
-                       printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
+               if (ndr_pull->offset != ndr_pull->data_size) {
+                       printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - ndr_pull->offset);
                }
 
                if (!NT_STATUS_IS_OK(status)) {
                        printf("pull for context file returned %s\n", nt_errstr(status));
                        exit(1);
                }
+               memcpy(v_st, st, f->struct_size);
        } 
 
-       data = file_load(filename, &size);
+       if (filename)
+               data = (uint8_t *)file_load(filename, &size, mem_ctx);
+       else
+               data = (uint8_t *)stdin_load(mem_ctx, &size);
+
        if (!data) {
-               perror(filename);
+               if (filename)
+                       perror(filename);
+               else
+                       perror("stdin");
                exit(1);
        }
 
        blob.data = data;
        blob.length = size;
 
-       ndr = ndr_pull_init_blob(&blob, mem_ctx);
-
-       if (flags == NDR_OUT) {
-               ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
-       }
+       ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
+       ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
 
-       status = f->ndr_pull(ndr, flags, st);
+       status = f->ndr_pull(ndr_pull, flags, st);
 
        printf("pull returned %s\n", nt_errstr(status));
 
-       if (ndr->offset != ndr->data_size) {
-               printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
-               dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
+       if (ndr_pull->offset != ndr_pull->data_size) {
+               printf("WARNING! %d unread bytes\n", ndr_pull->data_size - ndr_pull->offset);
+               dump_data(0, ndr_pull->data+ndr_pull->offset, ndr_pull->data_size - ndr_pull->offset);
+       }
+
+       if (dumpdata) {
+               printf("%d bytes consumed\n", ndr_pull->offset);
+               dump_data(0, blob.data, blob.length);
        }
 
-       pr = talloc_p(NULL, struct ndr_print);
-       pr->print = ndr_print_debug_helper;
-       pr->depth = 1;
-       f->ndr_print(pr, function, flags, st);
+       ndr_print = talloc_zero(mem_ctx, struct ndr_print);
+       ndr_print->print = ndr_print_debug_helper;
+       ndr_print->depth = 1;
+       f->ndr_print(ndr_print, function, flags, st);
 
-       if (!NT_STATUS_IS_OK(status) ||
-           ndr->offset != ndr->data_size) {
+       if (!NT_STATUS_IS_OK(status)) {
                printf("dump FAILED\n");
                exit(1);
        }
 
+       if (validate) {
+               DATA_BLOB v_blob;
+               struct ndr_push *ndr_v_push;
+               struct ndr_pull *ndr_v_pull;
+               struct ndr_print *ndr_v_print;
+
+               ndr_v_push = ndr_push_init_ctx(mem_ctx);
+               
+               status = f->ndr_push(ndr_v_push, flags, st);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("validate push FAILED\n");
+                       exit(1);
+               }
+
+               v_blob = ndr_push_blob(ndr_v_push);
+
+               if (dumpdata) {
+                       printf("%ld bytes generated (validate)\n", (long)v_blob.length);
+                       dump_data(0, v_blob.data, v_blob.length);
+               }
+
+               ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
+               ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
+
+               status = f->ndr_pull(ndr_v_pull, flags, v_st);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("validate pull FAILED\n");
+                       exit(1);
+               }
+
+               printf("pull returned %s\n", nt_errstr(status));
+
+               if (ndr_v_pull->offset != ndr_v_pull->data_size) {
+                       printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
+                       dump_data(0, ndr_v_pull->data+ndr_v_pull->offset, ndr_v_pull->data_size - ndr_v_pull->offset);
+               }
+
+               ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
+               ndr_v_print->print = ndr_print_debug_helper;
+               ndr_v_print->depth = 1;
+               f->ndr_print(ndr_v_print, function, flags, v_st);
+
+               if (blob.length != v_blob.length) {
+                       printf("WARNING! orig bytes:%ld validated pushed bytes:%ld\n", (long)blob.length, (long)v_blob.length);
+               }
+
+               if (ndr_pull->offset != ndr_v_pull->offset) {
+                       printf("WARNING! orig pulled bytes:%d validated pulled bytes:%d\n", ndr_pull->offset, ndr_v_pull->offset);
+               }
+       }
+
        printf("dump OK\n");
 
-       talloc_free(pr);
-       
+       talloc_free(mem_ctx);
+
        poptFreeContext(pc);
        
        return 0;