r19831: Big ldb_dn optimization and interfaces enhancement patch
[kamenim/samba.git] / source4 / lib / ldb / tools / ldbedit.c
index 24fcf1a9699ebee511e41ab79eff8f3703c88038..429febb75b04775fe90fda7337afbe4852440fcd 100644 (file)
@@ -91,11 +91,11 @@ static int modify_record(struct ldb_context *ldb,
 static struct ldb_message *msg_find(struct ldb_context *ldb,
                                    struct ldb_message **msgs,
                                    int count,
-                                   const struct ldb_dn *dn)
+                                   struct ldb_dn *dn)
 {
        int i;
        for (i=0;i<count;i++) {
-               if (ldb_dn_compare(ldb, dn, msgs[i]->dn) == 0) {
+               if (ldb_dn_compare(dn, msgs[i]->dn) == 0) {
                        return msgs[i];
                }
        }
@@ -189,7 +189,7 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
 {
        int fd, ret;
        FILE *f;
-       char template[] = "/tmp/ldbedit.XXXXXX";
+       char file_template[] = "/tmp/ldbedit.XXXXXX";
        char *cmd;
        struct ldb_ldif *ldif;
        struct ldb_message **msgs2 = NULL;
@@ -197,10 +197,10 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
 
        /* write out the original set of messages to a temporary
           file */
-       fd = mkstemp(template);
+       fd = mkstemp(file_template);
 
        if (fd == -1) {
-               perror(template);
+               perror(file_template);
                return -1;
        }
 
@@ -209,7 +209,7 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
        if (!f) {
                perror("fopen");
                close(fd);
-               unlink(template);
+               unlink(file_template);
                return -1;
        }
 
@@ -219,28 +219,28 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
 
        fclose(f);
 
-       asprintf(&cmd, "%s %s", editor, template);
+       cmd = talloc_asprintf(ldb, "%s %s", editor, file_template);
 
        if (!cmd) {
-               unlink(template);
+               unlink(file_template);
                fprintf(stderr, "out of memory\n");
                return -1;
        }
 
        /* run the editor */
        ret = system(cmd);
-       free(cmd);
+       talloc_free(cmd);
 
        if (ret != 0) {
-               unlink(template);
+               unlink(file_template);
                fprintf(stderr, "edit with %s failed\n", editor);
                return -1;
        }
 
        /* read the resulting ldif into msgs2 */
-       f = fopen(template, "r");
+       f = fopen(file_template, "r");
        if (!f) {
-               perror(template);
+               perror(file_template);
                return -1;
        }
 
@@ -254,7 +254,7 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
        }
 
        fclose(f);
-       unlink(template);
+       unlink(file_template);
 
        return merge_edits(ldb, msgs1, count1, msgs2, count2);
 }
@@ -272,15 +272,17 @@ static void usage(void)
        exit(1);
 }
 
- int main(int argc, const char **argv)
+int main(int argc, const char **argv)
 {
        struct ldb_context *ldb;
        struct ldb_result *result = NULL;
        struct ldb_dn *basedn = NULL;
        int ret;
-       const char *expression = "(|(objectclass=*)(distinguishedName=*))";
+       const char *expression = "(|(objectClass=*)(distinguishedName=*))";
        const char * const * attrs = NULL;
 
+       ldb_global_init();
+
        ldb = ldb_init(NULL);
 
        options = ldb_cmdline_process(ldb, argc, argv, usage);
@@ -298,8 +300,8 @@ static void usage(void)
        }
 
        if (options->basedn != NULL) {
-               basedn = ldb_dn_explode(ldb, options->basedn);
-               if (basedn == NULL) {
+               basedn = ldb_dn_new(ldb, ldb, options->basedn);
+               if ( ! ldb_dn_validate(basedn)) {
                        printf("Invalid Base DN format\n");
                        exit(1);
                }