s3:cli_netlogon: add rpccli_{create,setup}_netlogon_creds_with_creds() helper functions
[obnox/samba/samba-obnox.git] / source3 / rpc_client / cli_netlogon.c
index 9e3c1bd30e6bd411529a7aa6ba1b8300702eb0e2..b08c10fb7830d7003f3c6bb13ec97f5155fcb1ec 100644 (file)
@@ -70,7 +70,7 @@ NTSTATUS rpccli_pre_open_netlogon_creds(void)
        global_db = db_open(talloc_autofree_context(), fname,
                            0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
                            O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2,
-                           DBWRAP_FLAG_NONE);
+                           DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS);
        if (global_db == NULL) {
                TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
@@ -124,7 +124,34 @@ NTSTATUS rpccli_create_netlogon_creds(const char *server_computer,
        return NT_STATUS_OK;
 }
 
+NTSTATUS rpccli_create_netlogon_creds_with_creds(struct cli_credentials *creds,
+                                                const char *server_computer,
+                                                struct messaging_context *msg_ctx,
+                                                TALLOC_CTX *mem_ctx,
+                                                struct netlogon_creds_cli_context **netlogon_creds)
+{
+       enum netr_SchannelType sec_chan_type;
+       const char *server_netbios_domain;
+       const char *client_account;
+
+       sec_chan_type = cli_credentials_get_secure_channel_type(creds);
+       if (sec_chan_type == SEC_CHAN_NULL) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       client_account = cli_credentials_get_username(creds);
+       server_netbios_domain = cli_credentials_get_domain(creds);
+
+       return rpccli_create_netlogon_creds(server_computer,
+                                           server_netbios_domain,
+                                           client_account,
+                                           sec_chan_type,
+                                           msg_ctx, mem_ctx,
+                                           netlogon_creds);
+}
+
 NTSTATUS rpccli_setup_netlogon_creds(struct cli_state *cli,
+                                    enum dcerpc_transport_t transport,
                                     struct netlogon_creds_cli_context *netlogon_creds,
                                     bool force_reauth,
                                     struct samr_Password current_nt_hash,
@@ -155,9 +182,10 @@ NTSTATUS rpccli_setup_netlogon_creds(struct cli_state *cli,
                TALLOC_FREE(creds);
        }
 
-       status = cli_rpc_pipe_open_noauth(cli,
-                                         &ndr_table_netlogon,
-                                         &netlogon_pipe);
+       status = cli_rpc_pipe_open_noauth_transport(cli,
+                                                   transport,
+                                                   &ndr_table_netlogon,
+                                                   &netlogon_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
                         __FUNCTION__,
@@ -193,16 +221,93 @@ NTSTATUS rpccli_setup_netlogon_creds(struct cli_state *cli,
        return NT_STATUS_OK;
 }
 
+NTSTATUS rpccli_setup_netlogon_creds_with_creds(struct cli_state *cli,
+                                               enum dcerpc_transport_t transport,
+                                               struct netlogon_creds_cli_context *netlogon_creds,
+                                               bool force_reauth,
+                                               struct cli_credentials *creds)
+{
+       struct samr_Password *current_nt_hash = NULL;
+       struct samr_Password *previous_nt_hash = NULL;
+       NTSTATUS status;
+
+       current_nt_hash = cli_credentials_get_nt_hash(creds, talloc_tos());
+       if (current_nt_hash == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = rpccli_setup_netlogon_creds(cli, transport,
+                                            netlogon_creds,
+                                            force_reauth,
+                                            *current_nt_hash,
+                                            previous_nt_hash);
+       TALLOC_FREE(current_nt_hash);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
+                                       uint16_t validation_level,
+                                       union netr_Validation *validation,
+                                       struct netr_SamInfo3 **info3_p)
+{
+       struct netr_SamInfo3 *info3;
+       NTSTATUS status;
+
+       if (validation == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       switch (validation_level) {
+       case 3:
+               if (validation->sam3 == NULL) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               info3 = talloc_move(mem_ctx, &validation->sam3);
+               break;
+       case 6:
+               if (validation->sam6 == NULL) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
+               if (info3 == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(info3);
+                       return status;
+               }
+
+               info3->sidcount = validation->sam6->sidcount;
+               info3->sids = talloc_move(info3, &validation->sam6->sids);
+               break;
+       default:
+               return NT_STATUS_BAD_VALIDATION_CLASS;
+       }
+
+       *info3_p = info3;
+
+       return NT_STATUS_OK;
+}
+
 /* Logon domain user */
 
 NTSTATUS rpccli_netlogon_password_logon(struct netlogon_creds_cli_context *creds,
                                        struct dcerpc_binding_handle *binding_handle,
+                                       TALLOC_CTX *mem_ctx,
                                        uint32_t logon_parameters,
                                        const char *domain,
                                        const char *username,
                                        const char *password,
                                        const char *workstation,
-                                       enum netr_LogonInfoClass logon_type)
+                                       enum netr_LogonInfoClass logon_type,
+                                       struct netr_SamInfo3 **info3)
 {
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
@@ -320,57 +425,19 @@ NTSTATUS rpccli_netlogon_password_logon(struct netlogon_creds_cli_context *creds
                                                  &validation,
                                                  &authoritative,
                                                  &flags);
-       TALLOC_FREE(frame);
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
                return status;
        }
 
-       return NT_STATUS_OK;
-}
-
-static NTSTATUS map_validation_to_info3(TALLOC_CTX *mem_ctx,
-                                       uint16_t validation_level,
-                                       union netr_Validation *validation,
-                                       struct netr_SamInfo3 **info3_p)
-{
-       struct netr_SamInfo3 *info3;
-       NTSTATUS status;
-
-       if (validation == NULL) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       switch (validation_level) {
-       case 3:
-               if (validation->sam3 == NULL) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-
-               info3 = talloc_move(mem_ctx, &validation->sam3);
-               break;
-       case 6:
-               if (validation->sam6 == NULL) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-
-               info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
-               if (info3 == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-               status = copy_netr_SamBaseInfo(info3, &validation->sam6->base, &info3->base);
-               if (!NT_STATUS_IS_OK(status)) {
-                       TALLOC_FREE(info3);
-                       return status;
-               }
-
-               info3->sidcount = validation->sam6->sidcount;
-               info3->sids = talloc_move(info3, &validation->sam6->sids);
-               break;
-       default:
-               return NT_STATUS_BAD_VALIDATION_CLASS;
+       status = map_validation_to_info3(mem_ctx,
+                                        validation_level, validation,
+                                        info3);
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       *info3_p = info3;
 
        return NT_STATUS_OK;
 }