s3-dcerpc: Use dcerpc_AuthType in pipe_auth_data
[kai/samba.git] / source3 / rpc_server / srv_netlog_nt.c
index a039b08869294d2a5d3f75c710720df843cff19c..56b5027958a8ef966cff11fe2cf8e817bb83f5e2 100644 (file)
 #include "includes.h"
 #include "../libcli/auth/schannel.h"
 #include "../librpc/gen_ndr/srv_netlogon.h"
+#include "../librpc/gen_ndr/srv_samr.h"
+#include "../librpc/gen_ndr/srv_lsa.h"
+#include "../librpc/gen_ndr/cli_samr.h"
+#include "../librpc/gen_ndr/cli_lsa.h"
+#include "rpc_client/cli_lsarpc.h"
 #include "librpc/gen_ndr/messaging.h"
 #include "../lib/crypto/md4.h"
 
@@ -44,7 +49,7 @@ struct netlogon_server_pipe_state {
  _netr_LogonControl
  *************************************************************************/
 
-WERROR _netr_LogonControl(pipes_struct *p,
+WERROR _netr_LogonControl(struct pipes_struct *p,
                          struct netr_LogonControl *r)
 {
        struct netr_LogonControl2Ex l;
@@ -82,7 +87,7 @@ static void send_sync_message(void)
  _netr_LogonControl2
  *************************************************************************/
 
-WERROR _netr_LogonControl2(pipes_struct *p,
+WERROR _netr_LogonControl2(struct pipes_struct *p,
                           struct netr_LogonControl2 *r)
 {
        struct netr_LogonControl2Ex l;
@@ -162,7 +167,7 @@ static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
  _netr_LogonControl2Ex
 ****************************************************************/
 
-WERROR _netr_LogonControl2Ex(pipes_struct *p,
+WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
                             struct netr_LogonControl2Ex *r)
 {
        uint32_t flags = 0x0;
@@ -180,7 +185,7 @@ WERROR _netr_LogonControl2Ex(pipes_struct *p,
        const char *fn;
        uint32_t acct_ctrl;
 
-       switch (p->hdr_req.opnum) {
+       switch (p->opnum) {
        case NDR_NETR_LOGONCONTROL:
                fn = "_netr_LogonControl";
                break;
@@ -194,7 +199,7 @@ WERROR _netr_LogonControl2Ex(pipes_struct *p,
                return WERR_INVALID_PARAM;
        }
 
-       acct_ctrl = pdb_get_acct_ctrl(p->server_info->sam_account);
+       acct_ctrl = p->server_info->info3->base.acct_flags;
 
        switch (r->in.function_code) {
        case NETLOGON_CONTROL_TC_VERIFY:
@@ -384,44 +389,75 @@ WERROR _netr_LogonControl2Ex(pipes_struct *p,
  _netr_NetrEnumerateTrustedDomains
  *************************************************************************/
 
-WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
-                                        struct netr_NetrEnumerateTrustedDomains *r)
+NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
+                                          struct netr_NetrEnumerateTrustedDomains *r)
 {
        NTSTATUS status;
        DATA_BLOB blob;
-       struct trustdom_info **domains;
-       uint32_t num_domains;
-       const char **trusted_domains;
+       int num_domains = 0;
+       const char **trusted_domains = NULL;
+       struct lsa_DomainList domain_list;
+       struct rpc_pipe_client *cli = NULL;
+       struct policy_handle pol;
+       uint32_t enum_ctx = 0;
        int i;
+       uint32_t max_size = (uint32_t)-1;
 
        DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
 
-       /* set up the Trusted Domain List response */
-
-       become_root();
-       status = pdb_enum_trusteddoms(p->mem_ctx, &num_domains, &domains);
-       unbecome_root();
-
+       status = rpc_pipe_open_internal(p->mem_ctx, &ndr_table_lsarpc.syntax_id,
+                                       p->server_info,
+                                       &cli);
        if (!NT_STATUS_IS_OK(status)) {
-               return ntstatus_to_werror(status);
+               return status;
        }
 
-       trusted_domains = talloc_zero_array(p->mem_ctx, const char *, num_domains + 1);
-       if (!trusted_domains) {
-               return WERR_NOMEM;
+       status = rpccli_lsa_open_policy2(cli, p->mem_ctx,
+                                        true,
+                                        LSA_POLICY_VIEW_LOCAL_INFORMATION,
+                                        &pol);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       for (i = 0; i < num_domains; i++) {
-               trusted_domains[i] = talloc_strdup(trusted_domains, domains[i]->name);
-               if (!trusted_domains[i]) {
-                       TALLOC_FREE(trusted_domains);
-                       return WERR_NOMEM;
+       do {
+               /* Lookup list of trusted domains */
+
+               status = rpccli_lsa_EnumTrustDom(cli, p->mem_ctx,
+                                                &pol,
+                                                &enum_ctx,
+                                                &domain_list,
+                                                max_size);
+               if (!NT_STATUS_IS_OK(status) &&
+                   !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) &&
+                   !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
+                       goto out;
+               }
+
+               for (i = 0; i < domain_list.count; i++) {
+                       if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
+                                                &trusted_domains, &num_domains)) {
+                               status = NT_STATUS_NO_MEMORY;
+                               goto out;
+                       }
+               }
+       } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
+
+       if (num_domains > 0) {
+               /* multi sz terminate */
+               trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
+               if (trusted_domains == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
                }
+
+               trusted_domains[num_domains] = NULL;
        }
 
        if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
                TALLOC_FREE(trusted_domains);
-               return WERR_NOMEM;
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
 
        r->out.trusted_domains_blob->data = blob.data;
@@ -429,7 +465,118 @@ WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
 
        DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
 
-       return WERR_OK;
+       status = NT_STATUS_OK;
+
+ out:
+       if (cli && is_valid_policy_hnd(&pol)) {
+               rpccli_lsa_Close(cli, p->mem_ctx, &pol);
+       }
+
+       return status;
+}
+
+/*************************************************************************
+ *************************************************************************/
+
+static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
+                                         struct rpc_pipe_client *cli,
+                                         const char *account_name,
+                                         uint32_t access_mask,
+                                         struct dom_sid2 **domain_sid_p,
+                                         uint32_t *user_rid_p,
+                                         struct policy_handle *user_handle)
+{
+       NTSTATUS status;
+       struct policy_handle connect_handle, domain_handle;
+       struct lsa_String domain_name;
+       struct dom_sid2 *domain_sid;
+       struct lsa_String names;
+       struct samr_Ids rids;
+       struct samr_Ids types;
+       uint32_t rid;
+
+       status = rpccli_samr_Connect2(cli, mem_ctx,
+                                     global_myname(),
+                                     SAMR_ACCESS_CONNECT_TO_SERVER |
+                                     SAMR_ACCESS_ENUM_DOMAINS |
+                                     SAMR_ACCESS_LOOKUP_DOMAIN,
+                                     &connect_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       init_lsa_String(&domain_name, get_global_sam_name());
+
+       status = rpccli_samr_LookupDomain(cli, mem_ctx,
+                                         &connect_handle,
+                                         &domain_name,
+                                         &domain_sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       status = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_handle,
+                                       SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
+                                       domain_sid,
+                                       &domain_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       init_lsa_String(&names, account_name);
+
+       status = rpccli_samr_LookupNames(cli, mem_ctx,
+                                        &domain_handle,
+                                        1,
+                                        &names,
+                                        &rids,
+                                        &types);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       if (rids.count != 1) {
+               status = NT_STATUS_NO_SUCH_USER;
+               goto out;
+       }
+       if (rids.count != types.count) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto out;
+       }
+       if (types.ids[0] != SID_NAME_USER) {
+               status = NT_STATUS_NO_SUCH_USER;
+               goto out;
+       }
+
+       rid = rids.ids[0];
+
+       status = rpccli_samr_OpenUser(cli, mem_ctx,
+                                     &domain_handle,
+                                     access_mask,
+                                     rid,
+                                     user_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       if (user_rid_p) {
+               *user_rid_p = rid;
+       }
+
+       if (domain_sid_p) {
+               *domain_sid_p = domain_sid;
+       }
+
+ out:
+       if (cli && is_valid_policy_hnd(&domain_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &domain_handle);
+       }
+       if (cli && is_valid_policy_hnd(&connect_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_handle);
+       }
+
+       return status;
 }
 
 /******************************************************************
@@ -439,11 +586,15 @@ WERROR _netr_NetrEnumerateTrustedDomains(pipes_struct *p,
 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
                          enum netr_SchannelType sec_chan_type, struct dom_sid *sid)
 {
-       struct samu *sampass = NULL;
-       const uint8 *pass;
-       bool ret;
-       uint32 acct_ctrl;
-
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx;
+       struct rpc_pipe_client *cli = NULL;
+       struct policy_handle user_handle;
+       uint32_t user_rid;
+       struct dom_sid *domain_sid;
+       uint32_t acct_ctrl;
+       union samr_UserInfo *info;
+       struct auth_serversupplied_info *server_info;
 #if 0
        char addr[INET6_ADDRSTRLEN];
 
@@ -464,26 +615,50 @@ static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
        }
 #endif /* 0 */
 
-       if ( !(sampass = samu_new( NULL )) ) {
-               return NT_STATUS_NO_MEMORY;
+       mem_ctx = talloc_new(talloc_tos());
+       if (mem_ctx == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
+
+       status = make_server_info_system(mem_ctx, &server_info);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       ZERO_STRUCT(user_handle);
+
+       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
+                                       server_info,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       /* JRA. This is ok as it is only used for generating the challenge. */
        become_root();
-       ret = pdb_getsampwnam(sampass, mach_acct);
+       status = samr_find_machine_account(mem_ctx, cli, mach_acct,
+                                          SEC_FLAG_MAXIMUM_ALLOWED,
+                                          &domain_sid, &user_rid,
+                                          &user_handle);
        unbecome_root();
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
-       if (!ret) {
-               DEBUG(0,("get_md4pw: Workstation %s: no account in domain\n", mach_acct));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_ACCESS_DENIED;
+       status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
+                                           &user_handle,
+                                           UserControlInformation,
+                                           &info);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       acct_ctrl = pdb_get_acct_ctrl(sampass);
+       acct_ctrl = info->info16.acct_flags;
+
        if (acct_ctrl & ACB_DISABLED) {
                DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_ACCOUNT_DISABLED;
+               status = NT_STATUS_ACCOUNT_DISABLED;
+               goto out;
        }
 
        if (!(acct_ctrl & ACB_SVRTRUST) &&
@@ -491,8 +666,8 @@ static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
            !(acct_ctrl & ACB_DOMTRUST))
        {
                DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+               status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+               goto out;
        }
 
        switch (sec_chan_type) {
@@ -500,53 +675,65 @@ static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
                        if (!(acct_ctrl & ACB_SVRTRUST)) {
                                DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
                                         "but not a server trust account\n", mach_acct));
-                               TALLOC_FREE(sampass);
-                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               goto out;
                        }
                        break;
                case SEC_CHAN_WKSTA:
                        if (!(acct_ctrl & ACB_WSTRUST)) {
                                DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
                                         "but not a workstation trust account\n", mach_acct));
-                               TALLOC_FREE(sampass);
-                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               goto out;
                        }
                        break;
                case SEC_CHAN_DOMAIN:
                        if (!(acct_ctrl & ACB_DOMTRUST)) {
                                DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
                                         "but not a interdomain trust account\n", mach_acct));
-                               TALLOC_FREE(sampass);
-                               return NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
+                               goto out;
                        }
                        break;
                default:
                        break;
        }
 
-       if ((pass = pdb_get_nt_passwd(sampass)) == NULL) {
+       become_root();
+       status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
+                                           &user_handle,
+                                           UserInternal1Information,
+                                           &info);
+       unbecome_root();
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+       if (info->info18.nt_pwd_active == 0) {
                DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
-               TALLOC_FREE(sampass);
-               return NT_STATUS_LOGON_FAILURE;
+               status = NT_STATUS_LOGON_FAILURE;
+               goto out;
        }
 
-       memcpy(md4pw->hash, pass, 16);
-       dump_data(5, md4pw->hash, 16);
+       /* samr gives out nthash unencrypted (!) */
+       memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
 
-       sid_copy(sid, pdb_get_user_sid(sampass));
+       sid_compose(sid, domain_sid, user_rid);
 
-       TALLOC_FREE(sampass);
-
-       return NT_STATUS_OK;
+ out:
+       if (cli && is_valid_policy_hnd(&user_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &user_handle);
+       }
 
+       talloc_free(mem_ctx);
 
+       return status;
 }
 
 /*************************************************************************
  _netr_ServerReqChallenge
  *************************************************************************/
 
-NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
+NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
                                  struct netr_ServerReqChallenge *r)
 {
        struct netlogon_server_pipe_state *pipe_state =
@@ -578,7 +765,7 @@ NTSTATUS _netr_ServerReqChallenge(pipes_struct *p,
  Create the initial credentials.
  *************************************************************************/
 
-NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
+NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
                                  struct netr_ServerAuthenticate *r)
 {
        struct netr_ServerAuthenticate3 a;
@@ -604,7 +791,7 @@ NTSTATUS _netr_ServerAuthenticate(pipes_struct *p,
  _netr_ServerAuthenticate3
  *************************************************************************/
 
-NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
+NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
                                   struct netr_ServerAuthenticate3 *r)
 {
        NTSTATUS status;
@@ -649,7 +836,7 @@ NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
                srv_flgs |= NETLOGON_NEG_SCHANNEL;
        }
 
-       switch (p->hdr_req.opnum) {
+       switch (p->opnum) {
                case NDR_NETR_SERVERAUTHENTICATE:
                        fn = "_netr_ServerAuthenticate";
                        break;
@@ -747,7 +934,7 @@ NTSTATUS _netr_ServerAuthenticate3(pipes_struct *p,
  _netr_ServerAuthenticate2
  *************************************************************************/
 
-NTSTATUS _netr_ServerAuthenticate2(pipes_struct *p,
+NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
                                   struct netr_ServerAuthenticate2 *r)
 {
        struct netr_ServerAuthenticate3 a;
@@ -774,7 +961,7 @@ static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
                                        const char *computer_name,
                                        bool integrity, bool privacy)
 {
-       if (auth_info && auth_info->auth_type == PIPE_AUTH_TYPE_SCHANNEL) {
+       if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
                if (!privacy && !integrity) {
                        return NT_STATUS_OK;
                }
@@ -800,7 +987,7 @@ static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
 /*************************************************************************
  *************************************************************************/
 
-static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
+static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
                                             TALLOC_CTX *mem_ctx,
                                             const char *computer_name,
                                             struct netr_Authenticator *received_authenticator,
@@ -829,114 +1016,82 @@ static NTSTATUS netr_creds_server_step_check(pipes_struct *p,
 /*************************************************************************
  *************************************************************************/
 
-static NTSTATUS netr_find_machine_account(TALLOC_CTX *mem_ctx,
-                                         const char *account_name,
-                                         struct samu **sampassp)
+static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
+                                                 struct auth_serversupplied_info *server_info,
+                                                 const char *account_name,
+                                                 struct samr_Password *nt_hash)
 {
-       struct samu *sampass;
-       bool ret = false;
+       NTSTATUS status;
+       struct rpc_pipe_client *cli = NULL;
+       struct policy_handle user_handle;
        uint32_t acct_ctrl;
+       union samr_UserInfo *info;
+       struct samr_UserInfo18 info18;
+       DATA_BLOB in,out;
 
-       sampass = samu_new(mem_ctx);
-       if (!sampass) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       ZERO_STRUCT(user_handle);
 
-       become_root();
-       ret = pdb_getsampwnam(sampass, account_name);
-       unbecome_root();
+       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
+                                       server_info,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
-       if (!ret) {
-               TALLOC_FREE(sampass);
-               return NT_STATUS_ACCESS_DENIED;
+       status = samr_find_machine_account(mem_ctx, cli, account_name,
+                                          SEC_FLAG_MAXIMUM_ALLOWED,
+                                          NULL, NULL,
+                                          &user_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       /* Ensure the account exists and is a machine account. */
+       status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
+                                           &user_handle,
+                                           UserControlInformation,
+                                           &info);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
-       acct_ctrl = pdb_get_acct_ctrl(sampass);
+       acct_ctrl = info->info16.acct_flags;
 
        if (!(acct_ctrl & ACB_WSTRUST ||
              acct_ctrl & ACB_SVRTRUST ||
              acct_ctrl & ACB_DOMTRUST)) {
-               TALLOC_FREE(sampass);
-               return NT_STATUS_NO_SUCH_USER;
+               status = NT_STATUS_NO_SUCH_USER;
+               goto out;
        }
 
        if (acct_ctrl & ACB_DISABLED) {
-               TALLOC_FREE(sampass);
-               return NT_STATUS_ACCOUNT_DISABLED;
+               status = NT_STATUS_ACCOUNT_DISABLED;
+               goto out;
        }
 
-       *sampassp = sampass;
-
-       return NT_STATUS_OK;
-}
-
-/*************************************************************************
- *************************************************************************/
-
-static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
-                                                 struct samu *sampass,
-                                                 DATA_BLOB *plaintext_blob,
-                                                 struct samr_Password *nt_hash,
-                                                 struct samr_Password *lm_hash)
-{
-       NTSTATUS status;
-       const uchar *old_pw;
-       const char *plaintext = NULL;
-       size_t plaintext_len;
-       struct samr_Password nt_hash_local;
+       ZERO_STRUCT(info18);
 
-       if (!sampass) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
+       in = data_blob_const(nt_hash->hash, 16);
+       out = data_blob_talloc_zero(mem_ctx, 16);
+       sess_crypt_blob(&out, &in, &server_info->user_session_key, true);
+       memcpy(info18.nt_pwd.hash, out.data, out.length);
 
-       if (plaintext_blob) {
-               if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
-                                          plaintext_blob->data, plaintext_blob->length,
-                                          &plaintext, &plaintext_len, false))
-               {
-                       plaintext = NULL;
-                       mdfour(nt_hash_local.hash, plaintext_blob->data, plaintext_blob->length);
-                       nt_hash = &nt_hash_local;
-               }
-       }
+       info18.nt_pwd_active = true;
 
-       if (plaintext) {
-               if (!pdb_set_plaintext_passwd(sampass, plaintext)) {
-                       return NT_STATUS_ACCESS_DENIED;
-               }
+       info->info18 = info18;
 
-               goto done;
+       status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
+                                         &user_handle,
+                                         UserInternal1Information,
+                                         info);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       if (nt_hash) {
-               old_pw = pdb_get_nt_passwd(sampass);
-
-               if (old_pw && memcmp(nt_hash->hash, old_pw, 16) == 0) {
-                       /* Avoid backend modificiations and other fun if the
-                          client changed the password to the *same thing* */
-               } else {
-                       /* LM password should be NULL for machines */
-                       if (!pdb_set_lanman_passwd(sampass, NULL, PDB_CHANGED)) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
-                       if (!pdb_set_nt_passwd(sampass, nt_hash->hash, PDB_CHANGED)) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
-
-                       if (!pdb_set_pass_last_set_time(sampass, time(NULL), PDB_CHANGED)) {
-                               /* Not quite sure what this one qualifies as, but this will do */
-                               return NT_STATUS_UNSUCCESSFUL;
-                       }
-               }
+ out:
+       if (cli && is_valid_policy_hnd(&user_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &user_handle);
        }
 
- done:
-       become_root();
-       status = pdb_update_sam_account(sampass);
-       unbecome_root();
-
        return status;
 }
 
@@ -944,11 +1099,10 @@ static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
  _netr_ServerPasswordSet
  *************************************************************************/
 
-NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
+NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
                                 struct netr_ServerPasswordSet *r)
 {
        NTSTATUS status = NT_STATUS_OK;
-       struct samu *sampass=NULL;
        int i;
        struct netlogon_creds_CredentialState *creds;
 
@@ -980,19 +1134,10 @@ NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
                DEBUG(100,("%02X ", r->in.new_password->hash[i]));
        DEBUG(100,("\n"));
 
-       status = netr_find_machine_account(p->mem_ctx,
-                                          creds->account_name,
-                                          &sampass);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = netr_set_machine_account_password(sampass,
-                                                  sampass,
-                                                  NULL,
-                                                  r->in.new_password,
-                                                  NULL);
-       TALLOC_FREE(sampass);
+       status = netr_set_machine_account_password(p->mem_ctx,
+                                                  p->server_info,
+                                                  creds->account_name,
+                                                  r->in.new_password);
        return status;
 }
 
@@ -1000,12 +1145,11 @@ NTSTATUS _netr_ServerPasswordSet(pipes_struct *p,
  _netr_ServerPasswordSet2
 ****************************************************************/
 
-NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
+NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
                                  struct netr_ServerPasswordSet2 *r)
 {
        NTSTATUS status;
        struct netlogon_creds_CredentialState *creds;
-       struct samu *sampass;
        DATA_BLOB plaintext;
        struct samr_CryptPassword password_buf;
        struct samr_Password nt_hash;
@@ -1036,19 +1180,10 @@ NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
 
        mdfour(nt_hash.hash, plaintext.data, plaintext.length);
 
-       status = netr_find_machine_account(p->mem_ctx,
-                                          creds->account_name,
-                                          &sampass);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = netr_set_machine_account_password(sampass,
-                                                  sampass,
-                                                  NULL,
-                                                  &nt_hash,
-                                                  NULL);
-       TALLOC_FREE(sampass);
+       status = netr_set_machine_account_password(p->mem_ctx,
+                                                  p->server_info,
+                                                  creds->account_name,
+                                                  &nt_hash);
        return status;
 }
 
@@ -1056,7 +1191,7 @@ NTSTATUS _netr_ServerPasswordSet2(pipes_struct *p,
  _netr_LogonSamLogoff
  *************************************************************************/
 
-NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
+NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
                              struct netr_LogonSamLogoff *r)
 {
        NTSTATUS status;
@@ -1077,7 +1212,7 @@ NTSTATUS _netr_LogonSamLogoff(pipes_struct *p,
  _netr_LogonSamLogon_base
  *************************************************************************/
 
-static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
+static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
                                         struct netr_LogonSamLogonEx *r,
                                         struct netlogon_creds_CredentialState *creds)
 {
@@ -1091,7 +1226,7 @@ static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
        bool process_creds = true;
        const char *fn;
 
-       switch (p->hdr_req.opnum) {
+       switch (p->opnum) {
                case NDR_NETR_LOGONSAMLOGON:
                        process_creds = true;
                        fn = "_netr_LogonSamLogon";
@@ -1245,7 +1380,7 @@ static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
                        user_info, &server_info);
        }
 
-       (auth_context->free)(&auth_context);
+       TALLOC_FREE(auth_context);
        free_user_info(&user_info);
 
        DEBUG(5,("%s: check_password returned status %s\n",
@@ -1284,7 +1419,7 @@ static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
                memcpy(pipe_session_key, creds->session_key, 16);
        } else {
                /* Get the pipe session key from the schannel. */
-               if ((p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL)
+               if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
                    || (p->auth.a_u.schannel_auth == NULL)) {
                        return NT_STATUS_INVALID_HANDLE;
                }
@@ -1315,7 +1450,7 @@ static NTSTATUS _netr_LogonSamLogon_base(pipes_struct *p,
  _netr_LogonSamLogonWithFlags
 ****************************************************************/
 
-NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
+NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
                                      struct netr_LogonSamLogonWithFlags *r)
 {
        NTSTATUS status;
@@ -1355,7 +1490,7 @@ NTSTATUS _netr_LogonSamLogonWithFlags(pipes_struct *p,
  _netr_LogonSamLogon
  *************************************************************************/
 
-NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
+NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
                             struct netr_LogonSamLogon *r)
 {
        NTSTATUS status;
@@ -1385,7 +1520,7 @@ NTSTATUS _netr_LogonSamLogon(pipes_struct *p,
  - no credential chaining. Map into net sam logon.
  *************************************************************************/
 
-NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
+NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
                               struct netr_LogonSamLogonEx *r)
 {
        NTSTATUS status;
@@ -1400,7 +1535,7 @@ NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
        }
 
        /* Only allow this if the pipe is protected. */
-       if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
+       if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
                DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
                        get_remote_machine_name() ));
                return NT_STATUS_INVALID_PARAMETER;
@@ -1416,7 +1551,7 @@ NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
  _ds_enum_dom_trusts
  *************************************************************************/
 #if 0  /* JERRY -- not correct */
- NTSTATUS _ds_enum_dom_trusts(pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
+ NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
                             DS_R_ENUM_DOM_TRUSTS *r_u)
 {
        NTSTATUS status = NT_STATUS_OK;
@@ -1435,7 +1570,7 @@ NTSTATUS _netr_LogonSamLogonEx(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_LogonUasLogon(pipes_struct *p,
+WERROR _netr_LogonUasLogon(struct pipes_struct *p,
                           struct netr_LogonUasLogon *r)
 {
        p->rng_fault_state = true;
@@ -1445,7 +1580,7 @@ WERROR _netr_LogonUasLogon(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_LogonUasLogoff(pipes_struct *p,
+WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
                            struct netr_LogonUasLogoff *r)
 {
        p->rng_fault_state = true;
@@ -1455,7 +1590,7 @@ WERROR _netr_LogonUasLogoff(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
+NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
                              struct netr_DatabaseDeltas *r)
 {
        p->rng_fault_state = true;
@@ -1465,7 +1600,7 @@ NTSTATUS _netr_DatabaseDeltas(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_DatabaseSync(pipes_struct *p,
+NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
                            struct netr_DatabaseSync *r)
 {
        p->rng_fault_state = true;
@@ -1475,7 +1610,7 @@ NTSTATUS _netr_DatabaseSync(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_AccountDeltas(pipes_struct *p,
+NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
                             struct netr_AccountDeltas *r)
 {
        p->rng_fault_state = true;
@@ -1485,7 +1620,7 @@ NTSTATUS _netr_AccountDeltas(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_AccountSync(pipes_struct *p,
+NTSTATUS _netr_AccountSync(struct pipes_struct *p,
                           struct netr_AccountSync *r)
 {
        p->rng_fault_state = true;
@@ -1536,7 +1671,7 @@ static bool wb_getdcname(TALLOC_CTX *mem_ctx,
  _netr_GetDcName
 ****************************************************************/
 
-WERROR _netr_GetDcName(pipes_struct *p,
+WERROR _netr_GetDcName(struct pipes_struct *p,
                       struct netr_GetDcName *r)
 {
        NTSTATUS status;
@@ -1582,7 +1717,7 @@ WERROR _netr_GetDcName(pipes_struct *p,
  _netr_GetAnyDCName
 ****************************************************************/
 
-WERROR _netr_GetAnyDCName(pipes_struct *p,
+WERROR _netr_GetAnyDCName(struct pipes_struct *p,
                          struct netr_GetAnyDCName *r)
 {
        NTSTATUS status;
@@ -1626,7 +1761,7 @@ WERROR _netr_GetAnyDCName(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
+NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
                             struct netr_DatabaseSync2 *r)
 {
        p->rng_fault_state = true;
@@ -1636,7 +1771,7 @@ NTSTATUS _netr_DatabaseSync2(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
+NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
                            struct netr_DatabaseRedo *r)
 {
        p->rng_fault_state = true;
@@ -1646,7 +1781,7 @@ NTSTATUS _netr_DatabaseRedo(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRGetDCName(pipes_struct *p,
+WERROR _netr_DsRGetDCName(struct pipes_struct *p,
                          struct netr_DsRGetDCName *r)
 {
        p->rng_fault_state = true;
@@ -1656,7 +1791,7 @@ WERROR _netr_DsRGetDCName(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
+NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
                                    struct netr_LogonGetCapabilities *r)
 {
        return NT_STATUS_NOT_IMPLEMENTED;
@@ -1665,7 +1800,7 @@ NTSTATUS _netr_LogonGetCapabilities(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
+WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
                                     struct netr_NETRLOGONSETSERVICEBITS *r)
 {
        p->rng_fault_state = true;
@@ -1675,7 +1810,7 @@ WERROR _netr_NETRLOGONSETSERVICEBITS(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_LogonGetTrustRid(pipes_struct *p,
+WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
                              struct netr_LogonGetTrustRid *r)
 {
        p->rng_fault_state = true;
@@ -1685,7 +1820,7 @@ WERROR _netr_LogonGetTrustRid(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
+WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
                                          struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
 {
        p->rng_fault_state = true;
@@ -1695,7 +1830,7 @@ WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
+WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
                                          struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
 {
        p->rng_fault_state = true;
@@ -1705,7 +1840,7 @@ WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
+WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
                            struct netr_DsRGetDCNameEx *r)
 {
        p->rng_fault_state = true;
@@ -1715,7 +1850,7 @@ WERROR _netr_DsRGetDCNameEx(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRGetSiteName(pipes_struct *p,
+WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
                            struct netr_DsRGetSiteName *r)
 {
        p->rng_fault_state = true;
@@ -1725,7 +1860,7 @@ WERROR _netr_DsRGetSiteName(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
+NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
                                  struct netr_LogonGetDomainInfo *r)
 {
        p->rng_fault_state = true;
@@ -1735,7 +1870,7 @@ NTSTATUS _netr_LogonGetDomainInfo(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_ServerPasswordGet(pipes_struct *p,
+WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
                               struct netr_ServerPasswordGet *r)
 {
        p->rng_fault_state = true;
@@ -1745,7 +1880,7 @@ WERROR _netr_ServerPasswordGet(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
+WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
                                struct netr_NETRLOGONSENDTOSAM *r)
 {
        p->rng_fault_state = true;
@@ -1755,7 +1890,7 @@ WERROR _netr_NETRLOGONSENDTOSAM(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
+WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
                                    struct netr_DsRAddressToSitenamesW *r)
 {
        p->rng_fault_state = true;
@@ -1765,7 +1900,7 @@ WERROR _netr_DsRAddressToSitenamesW(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
+WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
                             struct netr_DsRGetDCNameEx2 *r)
 {
        p->rng_fault_state = true;
@@ -1775,7 +1910,7 @@ WERROR _netr_DsRGetDCNameEx2(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
+WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
                                                 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
 {
        p->rng_fault_state = true;
@@ -1785,7 +1920,7 @@ WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
+WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
                                           struct netr_NetrEnumerateTrustedDomainsEx *r)
 {
        p->rng_fault_state = true;
@@ -1795,7 +1930,7 @@ WERROR _netr_NetrEnumerateTrustedDomainsEx(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
+WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
                                      struct netr_DsRAddressToSitenamesExW *r)
 {
        p->rng_fault_state = true;
@@ -1805,7 +1940,7 @@ WERROR _netr_DsRAddressToSitenamesExW(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
+WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
                                   struct netr_DsrGetDcSiteCoverageW *r)
 {
        p->rng_fault_state = true;
@@ -1815,7 +1950,7 @@ WERROR _netr_DsrGetDcSiteCoverageW(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
+WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
                                      struct netr_DsrEnumerateDomainTrusts *r)
 {
        p->rng_fault_state = true;
@@ -1825,7 +1960,7 @@ WERROR _netr_DsrEnumerateDomainTrusts(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
+WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
                                         struct netr_DsrDeregisterDNSHostRecords *r)
 {
        p->rng_fault_state = true;
@@ -1835,7 +1970,7 @@ WERROR _netr_DsrDeregisterDNSHostRecords(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
+NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
                                       struct netr_ServerTrustPasswordsGet *r)
 {
        p->rng_fault_state = true;
@@ -1845,7 +1980,7 @@ NTSTATUS _netr_ServerTrustPasswordsGet(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
+WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
                                          struct netr_DsRGetForestTrustInformation *r)
 {
        p->rng_fault_state = true;
@@ -1855,7 +1990,7 @@ WERROR _netr_DsRGetForestTrustInformation(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_GetForestTrustInformation(pipes_struct *p,
+NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
                                         struct netr_GetForestTrustInformation *r)
 {
        p->rng_fault_state = true;
@@ -1865,7 +2000,7 @@ NTSTATUS _netr_GetForestTrustInformation(pipes_struct *p,
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS _netr_ServerGetTrustInfo(pipes_struct *p,
+NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
                                  struct netr_ServerGetTrustInfo *r)
 {
        p->rng_fault_state = true;