ldb: detect eof on ldif files
[samba.git] / source4 / lib / ldb / tools / ldbedit.c
index ecdb4d7f621b855ca45bdd0a0b7f0825028c6feb..aaf6d80352d804b8a652f51786799cbd38b4cc89 100644 (file)
@@ -281,6 +281,21 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1,
                msgs2[count2++] = ldif->msg;
        }
 
+       /* the feof() test works here, even for the last line of the
+        * file, as we parse ldif files character by character, and
+        * feof() is only true if we have failed to read a character
+        * from the file. So if the last line is bad, we don't get
+        * feof() set, so we know the record was bad. Only if we
+        * attempt to go to the next record will we get feof() and
+        * thus consider that the ldif has ended without errors
+        */
+       if (!feof(f)) {
+               fprintf(stderr, "Error parsing ldif - aborting\n");
+               fclose(f);
+               unlink(file_template);
+               return -1;
+       }
+
        fclose(f);
        unlink(file_template);
 
@@ -291,7 +306,7 @@ static void usage(struct ldb_context *ldb)
 {
        printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
        ldb_cmdline_help(ldb, "ldbedit", stdout);
-       exit(1);
+       exit(LDB_ERR_OPERATIONS_ERROR);
 }
 
 int main(int argc, const char **argv)
@@ -306,6 +321,9 @@ int main(int argc, const char **argv)
        struct ldb_control **req_ctrls;
 
        ldb = ldb_init(mem_ctx, NULL);
+       if (ldb == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        options = ldb_cmdline_process(ldb, argc, argv, usage);
 
@@ -323,40 +341,32 @@ int main(int argc, const char **argv)
 
        if (options->basedn != NULL) {
                basedn = ldb_dn_new(ldb, ldb, options->basedn);
-               if ( ! ldb_dn_validate(basedn)) {
-                       printf("Invalid Base DN format\n");
-                       exit(1);
+               if (basedn == NULL) {
+                       return LDB_ERR_OPERATIONS_ERROR;
                }
        }
 
        req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
        if (options->controls != NULL &&  req_ctrls== NULL) {
                printf("parsing controls failed: %s\n", ldb_errstring(ldb));
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        ret = ldb_search_ctrl(ldb, ldb, &result, basedn, options->scope, attrs, req_ctrls, "%s", expression);
        if (ret != LDB_SUCCESS) {
                printf("search failed - %s\n", ldb_errstring(ldb));
-               exit(1);
+               return ret;
        }
 
        if (result->count == 0) {
                printf("no matching records - cannot edit\n");
-               return 0;
+               talloc_free(mem_ctx);
+               return LDB_SUCCESS;
        }
 
-       do_edit(ldb, result->msgs, result->count, options->editor);
-
-       if (result) {
-               ret = talloc_free(result);
-               if (ret == -1) {
-                       fprintf(stderr, "talloc_free failed\n");
-                       exit(1);
-               }
-       }
+       ret = do_edit(ldb, result->msgs, result->count, options->editor);
 
        talloc_free(mem_ctx);
 
-       return 0;
+       return ret == 0 ? LDB_SUCCESS : LDB_ERR_OPERATIONS_ERROR;
 }