net_vampire: add error and result_message to samsync_context.
authorGünther Deschner <gd@samba.org>
Tue, 17 Jun 2008 17:49:58 +0000 (19:49 +0200)
committerGünther Deschner <gd@samba.org>
Tue, 17 Jun 2008 17:55:16 +0000 (19:55 +0200)
Guenther
(This used to be commit e0b117200441f842fbc11cc817ab2cde4d63a22e)

source3/libnet/libnet_samsync.c
source3/libnet/libnet_samsync.h
source3/libnet/libnet_samsync_ldif.c
source3/utils/net_rpc_samsync.c

index b5632aed6937fe8dde4da171fc7adf1b12af2b7b..c86c5c12e11e0f92905faaae309f2fbd527cffb2 100644 (file)
@@ -222,6 +222,25 @@ NTSTATUS samsync_init_context(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
+/**
+ * samsync_database_str
+ */
+
+static const char *samsync_database_str(enum netr_SamDatabaseID database_id)
+{
+
+       switch (database_id) {
+               case SAM_DATABASE_DOMAIN:
+                       return "DOMAIN";
+               case SAM_DATABASE_BUILTIN:
+                       return "BUILTIN";
+               case SAM_DATABASE_PRIVS:
+                       return "PRIVS";
+               default:
+                       return "unknown";
+       }
+}
+
 /**
  * samsync_debug_str
  */
@@ -231,7 +250,6 @@ static const char *samsync_debug_str(TALLOC_CTX *mem_ctx,
                                     enum netr_SamDatabaseID database_id)
 {
        const char *action = NULL;
-       const char *str = NULL;
 
        switch (mode) {
                case NET_SAMSYNC_MODE_DUMP:
@@ -248,26 +266,8 @@ static const char *samsync_debug_str(TALLOC_CTX *mem_ctx,
                        break;
        }
 
-       switch (database_id) {
-               case SAM_DATABASE_DOMAIN:
-                       str = talloc_asprintf(mem_ctx, "%s DOMAIN database",
-                               action);
-                       break;
-               case SAM_DATABASE_BUILTIN:
-                       str = talloc_asprintf(mem_ctx, "%s BUILTIN database",
-                               action);
-                       break;
-               case SAM_DATABASE_PRIVS:
-                       str = talloc_asprintf(mem_ctx, "%s PRIVS database",
-                               action);
-                       break;
-               default:
-                       str = talloc_asprintf(mem_ctx, "%s unknown database type %u",
-                               action, database_id);
-                       break;
-       }
-
-       return str;
+       return talloc_asprintf(mem_ctx, "%s %s database",
+                               action, samsync_database_str(database_id));
 }
 
 /**
@@ -303,6 +303,7 @@ NTSTATUS samsync_process_database(struct rpc_pipe_client *pipe_hnd,
 
        do {
                struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
+               NTSTATUS callback_status;
 
                netlogon_creds_client_step(pipe_hnd->dc, &credential);
 
@@ -340,7 +341,11 @@ NTSTATUS samsync_process_database(struct rpc_pipe_client *pipe_hnd,
                                        delta_enum_array);
 
                /* Process results */
-               callback_fn(mem_ctx, database_id, delta_enum_array, result, ctx);
+               callback_status = callback_fn(mem_ctx, database_id, delta_enum_array, result, ctx);
+               if (!NT_STATUS_IS_OK(callback_status)) {
+                       result = callback_status;
+                       goto out;
+               }
 
                TALLOC_FREE(delta_enum_array);
 
@@ -349,6 +354,23 @@ NTSTATUS samsync_process_database(struct rpc_pipe_client *pipe_hnd,
 
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
+ out:
+       if (NT_STATUS_IS_ERR(result) && !ctx->error_message) {
+
+               ctx->error_message = talloc_asprintf(ctx,
+                       "Failed to fetch %s database: %s",
+                       samsync_database_str(database_id),
+                       nt_errstr(result));
+
+               if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) {
+
+                       ctx->error_message =
+                               talloc_asprintf_append(ctx->error_message,
+                                       "\nPerhaps %s is a Windows native mode domain?",
+                                       ctx->domain_name);
+               }
+       }
+
        talloc_destroy(mem_ctx);
 
        return result;
index 1e3995614fc12f66c9744731d02540c0480691ce..bd64c24d93383195f498d2386096fce1eb353e4f 100644 (file)
@@ -30,6 +30,10 @@ struct samsync_context {
        const char *domain_sid_str;
        const char *domain_name;
        const char *output_filename;
+
+       char *result_message;
+       char *error_message;
+
        void *private_data;
 };
 
index 86de2ab253ff44808d1de4af4cd17a292190f1bf..60acb7db40b11e3b850c889a8f3351bda58f59f4 100644 (file)
@@ -1186,7 +1186,7 @@ NTSTATUS fetch_sam_entries_ldif(TALLOC_CTX *mem_ctx,
        for (i = 0; i < r->num_deltas; i++) {
                status = fetch_sam_entry_ldif(mem_ctx, database_id,
                                              &r->delta_enum[i], ctx,
-                                             &g_index, &a_index);
+                                             &a_index, &g_index);
                if (!NT_STATUS_IS_OK(status)) {
                        goto failed;
                }
@@ -1195,6 +1195,11 @@ NTSTATUS fetch_sam_entries_ldif(TALLOC_CTX *mem_ctx,
        /* This was the last query */
        if (NT_STATUS_IS_OK(result)) {
                ldif_write_output(database_id, ldif_ctx);
+               if (ldif_ctx->ldif_file != stdout) {
+                       ctx->result_message = talloc_asprintf(mem_ctx,
+                               "Vampired %d accounts and %d groups to %s",
+                               a_index, g_index, ctx->output_filename);
+               }
                ldif_free_context(ldif_ctx);
                ctx->private_data = NULL;
        }
index 797598c48e946e6b74f635a53d19a4f7e872b297..5161bb3ef5a11a8147f91e16a96818eff3ebad1c 100644 (file)
@@ -1199,29 +1199,33 @@ NTSTATUS rpc_vampire_internals(struct net_context *c,
        /* fetch domain */
        result = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
                                          fetch_sam_entries, ctx);
-       if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, "Failed to fetch domain database: %s\n",
-                         nt_errstr(result));
-               if (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED))
-                       d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
-                                 "native mode domain?\n", domain_name);
+
+       if (!NT_STATUS_IS_OK(result) && ctx->error_message) {
+               d_fprintf(stderr, "%s\n", ctx->error_message);
                goto fail;
        }
 
+       if (ctx->result_message) {
+               d_fprintf(stdout, "%s\n", ctx->result_message);
+       }
+
        /* fetch builtin */
        ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
        ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
        result = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
                                          fetch_sam_entries, ctx);
-       if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
-                         nt_errstr(result));
+
+       if (!NT_STATUS_IS_OK(result) && ctx->error_message) {
+               d_fprintf(stderr, "%s\n", ctx->error_message);
                goto fail;
        }
 
-       TALLOC_FREE(ctx);
+       if (ctx->result_message) {
+               d_fprintf(stdout, "%s\n", ctx->result_message);
+       }
 
  fail:
+       TALLOC_FREE(ctx);
        return result;
 }
 
@@ -1253,29 +1257,33 @@ NTSTATUS rpc_vampire_ldif_internals(struct net_context *c,
        /* fetch domain */
        status = samsync_process_database(pipe_hnd, SAM_DATABASE_DOMAIN,
                                          fetch_sam_entries_ldif, ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "Failed to fetch domain database: %s\n",
-                         nt_errstr(status));
-               if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED))
-                       d_fprintf(stderr, "Perhaps %s is a Windows 2000 "
-                                 "native mode domain?\n", domain_name);
+
+       if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
+               d_fprintf(stderr, "%s\n", ctx->error_message);
                goto fail;
        }
 
+       if (ctx->result_message) {
+               d_fprintf(stdout, "%s\n", ctx->result_message);
+       }
+
        /* fetch builtin */
        ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
        ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
        status = samsync_process_database(pipe_hnd, SAM_DATABASE_BUILTIN,
                                          fetch_sam_entries_ldif, ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "Failed to fetch builtin database: %s\n",
-                         nt_errstr(status));
+
+       if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
+               d_fprintf(stderr, "%s\n", ctx->error_message);
                goto fail;
        }
 
-       TALLOC_FREE(ctx);
+       if (ctx->result_message) {
+               d_fprintf(stdout, "%s\n", ctx->result_message);
+       }
 
  fail:
+       TALLOC_FREE(ctx);
        return status;
 }