r5917: First step in using the new cli_credentials structure. This patch
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / tools / regdiff.c
index 070516b798f4f896fc82e3a7fca84dd11141267c..7206e1e44dcc60eaffdc9921402423a1294cdb55 100644 (file)
 */
 
 #include "includes.h"
+#include "dynconfig.h"
+#include "registry.h"
+#include "lib/cmdline/popt_common.h"
 
-void writediff(REG_KEY *oldkey, REG_KEY *newkey, FILE *out)
+static void writediff(struct registry_key *oldkey, struct registry_key *newkey, FILE *out)
 {
-       int i, numvals1, numvals2, numkeys2;
-
-       numkeys2 = reg_key_num_subkeys(newkey);
-       for(i = 0; i < numkeys2; i++) {
-               REG_KEY *t1 = reg_key_get_subkey_by_index(newkey, i);
-               REG_KEY *t2 = reg_key_get_subkey_by_name(oldkey, reg_key_name(t1));
-               if(!t2) {
-                       fprintf(out, "[%s]\n", reg_key_get_path(t1));
+       int i;
+       struct registry_key *t1, *t2;
+       struct registry_value *v1, *v2;
+       WERROR error1, error2;
+       TALLOC_CTX *mem_ctx = talloc_init("writediff");
+
+       for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
+               error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
+               if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
+                       fprintf(out, "-%s\n", t1->path+1);
+               } else if(!W_ERROR_IS_OK(error2)) {
+                       DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
+               }
+       }
+
+       talloc_free(mem_ctx);
+
+       if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
+               DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
+               return;
+       }
+
+       mem_ctx = talloc_init("writediff");
+
+       for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
+               error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
+               if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
+                       fprintf(out, "\n[%s]\n", t1->path+1);
+               } else if(!W_ERROR_IS_OK(error2)) {
+                       DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
                }
                writediff(t2, t1, out);
        }
 
-       numvals2 = reg_key_num_values(newkey);
-       for(i = 0; i < numvals2; i++) {
-               REG_VAL *t1 = reg_key_get_value_by_index(newkey, i);
-               REG_VAL *t2 = reg_key_get_value_by_name(oldkey, reg_val_name(t1));
-               if(!t2 || reg_val_size(t2) != reg_val_size(t1) || memcmp(reg_val_data_blk(t1), reg_val_data_blk(t2), reg_val_size(t1))) {
-                       fprintf(out, "\"%s\"=%s:%s\n", reg_val_name(t1), str_regtype(reg_val_type(t1)), reg_val_data_string(t1));
+       talloc_free(mem_ctx);
+
+       if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
+               DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
+               return;
+       }
+
+
+       mem_ctx = talloc_init("writediff");
+
+       for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
+               error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
+               if ((W_ERROR_IS_OK(error2) && (v2->data_len != v1->data_len || memcmp(v1->data_blk, v2->data_blk, v1->data_len))) 
+                       || W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
+                       fprintf(out, "\"%s\"=%s:%s\n", v1->name, str_regtype(v1->data_type), reg_val_data_string(mem_ctx, v1));
                }
+
+               if(!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
+                       DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
+               }
+       }
+
+       talloc_free(mem_ctx);
+
+       if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
+               DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
+               return;
        }
 
-       numvals1 = reg_key_num_values(oldkey);
-       for(i = 0; i < numvals1; i++) {
-               REG_VAL *t1 = reg_key_get_value_by_index(oldkey, i);
-               if(!reg_key_get_value_by_name(newkey, reg_val_name(t1))) {
-                       fprintf(out, "\"%s\"=-\n", reg_val_name(t1));
+       mem_ctx = talloc_init("writediff");
+
+       for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
+               error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
+               if(W_ERROR_IS_OK(error2)) {
+               } else if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
+                       fprintf(out, "\"%s\"=-\n", v1->name);
+               } else {
+                       DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
                }
        }
+
+       talloc_free(mem_ctx);
+
+       if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
+               DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
+               return;
+       }
 }
 
-int main (int argc, char **argv)
+ int main(int argc, char **argv)
 {
-       uint32  setparms, checkparms;
        int opt;
        poptContext pc;
-       REG_KEY *root;
-       const char *backend1 = NULL, *backend2 = NULL;
-       const char *location2;
        char *outputfile = NULL;
        FILE *fd = stdout;
-       REG_HANDLE *h2;
-       REG_KEY *root1 = NULL, *root2;
+       struct registry_context *h1 = NULL, *h2 = NULL;
        int from_null = 0;
-       int fullpath = 0, no_values = 0;
+       int i;
+       WERROR error, error2;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               {"backend", 'b', POPT_ARG_STRING, NULL, 'b', "backend to use", NULL},
+               POPT_COMMON_CREDENTIALS
                {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
-               {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL" },
+               {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL },
+               {"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL },
+               {"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL },
                POPT_TABLEEND
        };
 
+       regdiff_init_subsystems;
+
+       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
+               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
+       }
+
+
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
-       
+
        while((opt = poptGetNextOpt(pc)) != -1) {
+               error = WERR_OK;
                switch(opt)     {
-                       case 'b':
-                               if(!backend1 && !from_null) backend1 = poptGetOptArg(pc);
-                               else if(!backend2) backend2 = poptGetOptArg(pc);
-                               break;
+               case 'L':
+                       if (!h1 && !from_null) error = reg_open_local(&h1);
+                       else if (!h2) error = reg_open_local(&h2);
+                       break;
+               case 'R':
+                       if (!h1 && !from_null) error = reg_open_remote(&h1, cli_credentials_get_username(cmdline_credentials), cli_credentials_get_password(cmdline_credentials), poptGetOptArg(pc));
+                       else if (!h2) error = reg_open_remote(&h2, cli_credentials_get_username(cmdline_credentials), cli_credentials_get_password(cmdline_credentials), poptGetOptArg(pc));
+                       break;
                }
-       }
-       setup_logging(argv[0], True);
 
-       if(!from_null) {
-               REG_HANDLE *h1;
-               const char *location1;
-               location1 = poptGetArg(pc);
-               if(!location1) {
-                       poptPrintUsage(pc, stderr, 0);
+               if (!W_ERROR_IS_OK(error)) {
+                       fprintf(stderr, "Error: %s\n", win_errstr(error));
                        return 1;
                }
-
-               if(!backend1) backend1 = "dir";
-
-               h1 = reg_open(backend1, location1, True);
-               if(!h1) {
-                       fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location1, backend1);
-                       return 1;
-               }
-
-               root1 = reg_get_root(h1);
-       }
-
-       location2 = poptGetArg(pc);
-       if(!location2) {
-               poptPrintUsage(pc, stderr, 0);
-               return 2;
-       }
-
-       if(!backend2) backend2 = "dir";
-       
-       h2 = reg_open(backend2, location2, True);
-       if(!h2) {
-               fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location2, backend2);
-               return 1;
-       }
-       
-       root2 = reg_get_root(h2);
-       if(!root2) {
-               fprintf(stderr, "Can't open root key for '%s:%s'\n", backend2, location2);
-               return 1;
        }
+       setup_logging(argv[0], True);
 
        poptFreeContext(pc);
 
@@ -138,11 +169,28 @@ int main (int argc, char **argv)
        }
 
        fprintf(fd, "REGEDIT4\n\n");
-       fprintf(fd, "; Generated using regdiff\n");
+       fprintf(fd, "; Generated using regdiff, part of Samba\n");
 
-       writediff(root1, root2, fd); 
+       error2 = error = WERR_OK; 
+
+       for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
+               struct registry_key *r1, *r2;
+               error = reg_get_predefined_key(h1, i, &r1);
+               if (!W_ERROR_IS_OK(error)) {
+                       DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
+                       continue;
+               }
+               
+               error = reg_get_predefined_key(h2, i, &r2);
+               if (!W_ERROR_IS_OK(error)) {
+                       DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
+                       continue;
+               }
+
+               writediff(r1, r2, fd); 
+       }
 
        fclose(fd);
-       
+
        return 0;
 }