wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / rpc_client / cli_samr.c
index a994db95f8b431d6393d9b72b18df4d33e5111fd..e2bf08de4a02c4752519e559b549d8af7b30da21 100644 (file)
@@ -5,6 +5,7 @@
    Copyright (C) Andrew Tridgell              1992-1997,2000,
    Copyright (C) Rafal Szczesniak                       2002.
    Copyright (C) Jeremy Allison                         2005.
+   Copyright (C) Guenther Deschner                      2008.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 */
 
 #include "includes.h"
+#include "rpc_client/rpc_client.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/ndr_samr_c.h"
+#include "rpc_client/cli_samr.h"
+#include "../lib/crypto/arcfour.h"
+#include "rpc_client/init_lsa.h"
 
-/* Enumerate domain groups */
+/* User change password */
 
-NTSTATUS rpccli_samr_enum_dom_groups(struct rpc_pipe_client *cli,
-                                    TALLOC_CTX *mem_ctx, 
-                                    POLICY_HND *pol, uint32 *start_idx, 
-                                    uint32 size, struct acct_info **dom_groups,
-                                    uint32 *num_dom_groups)
+NTSTATUS dcerpc_samr_chgpasswd_user(struct dcerpc_binding_handle *h,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct policy_handle *user_handle,
+                                   const char *newpassword,
+                                   const char *oldpassword,
+                                   NTSTATUS *presult)
 {
-       prs_struct qbuf, rbuf;
-       SAMR_Q_ENUM_DOM_GROUPS q;
-       SAMR_R_ENUM_DOM_GROUPS r;
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 name_idx, i;
-
-       DEBUG(10,("cli_samr_enum_dom_groups starting at index %u\n", (unsigned int)*start_idx));
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
-
-       /* Marshall data and send request */
-
-       init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
-
-       CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_GROUPS,
-               q, r,
-               qbuf, rbuf,
-               samr_io_q_enum_dom_groups,
-               samr_io_r_enum_dom_groups,
-               NT_STATUS_UNSUCCESSFUL); 
-
-       /* Return output parameters */
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result) &&
-           NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES))
-               goto done;
-
-       *num_dom_groups = r.num_entries2;
-
-       if (*num_dom_groups == 0)
-               goto done;
-
-       if (!((*dom_groups) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_groups))) {
-               result = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       memset(*dom_groups, 0, sizeof(struct acct_info) * (*num_dom_groups));
+       NTSTATUS status;
+       struct samr_Password hash1, hash2, hash3, hash4, hash5, hash6;
 
-       name_idx = 0;
+       uchar old_nt_hash[16];
+       uchar old_lm_hash[16];
+       uchar new_nt_hash[16];
+       uchar new_lm_hash[16];
 
-       for (i = 0; i < *num_dom_groups; i++) {
+       ZERO_STRUCT(old_nt_hash);
+       ZERO_STRUCT(old_lm_hash);
+       ZERO_STRUCT(new_nt_hash);
+       ZERO_STRUCT(new_lm_hash);
 
-               (*dom_groups)[i].rid = r.sam[i].rid;
+       DEBUG(10,("rpccli_samr_chgpasswd_user\n"));
 
-               if (r.sam[i].hdr_name.buffer) {
-                       unistr2_to_ascii((*dom_groups)[i].acct_name,
-                                        &r.uni_grp_name[name_idx],
-                                        sizeof((*dom_groups)[i].acct_name));
-                       name_idx++;
-               }
+       E_md4hash(oldpassword, old_nt_hash);
+       E_md4hash(newpassword, new_nt_hash);
 
-               *start_idx = r.next_idx;
-       }
+       E_deshash(oldpassword, old_lm_hash);
+       E_deshash(newpassword, new_lm_hash);
+
+       E_old_pw_hash(new_lm_hash, old_lm_hash, hash1.hash);
+       E_old_pw_hash(old_lm_hash, new_lm_hash, hash2.hash);
+       E_old_pw_hash(new_nt_hash, old_nt_hash, hash3.hash);
+       E_old_pw_hash(old_nt_hash, new_nt_hash, hash4.hash);
+       E_old_pw_hash(old_lm_hash, new_nt_hash, hash5.hash);
+       E_old_pw_hash(old_nt_hash, new_lm_hash, hash6.hash);
+
+       status = dcerpc_samr_ChangePasswordUser(h,
+                                               mem_ctx,
+                                               user_handle,
+                                               true,
+                                               &hash1,
+                                               &hash2,
+                                               true,
+                                               &hash3,
+                                               &hash4,
+                                               true,
+                                               &hash5,
+                                               true,
+                                               &hash6,
+                                               presult);
 
- done:
-       return result;
+       return status;
 }
 
-/* Enumerate domain groups */
-
-NTSTATUS rpccli_samr_enum_als_groups(struct rpc_pipe_client *cli,
-                                    TALLOC_CTX *mem_ctx, 
-                                    POLICY_HND *pol, uint32 *start_idx, 
-                                    uint32 size, struct acct_info **dom_aliases,
-                                    uint32 *num_dom_aliases)
+NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct policy_handle *user_handle,
+                                   const char *newpassword,
+                                   const char *oldpassword)
 {
-       prs_struct qbuf, rbuf;
-       SAMR_Q_ENUM_DOM_ALIASES q;
-       SAMR_R_ENUM_DOM_ALIASES r;
+       NTSTATUS status;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 name_idx, i;
-
-       DEBUG(10,("cli_samr_enum_als_groups starting at index %u\n", (unsigned int)*start_idx));
-
-       ZERO_STRUCT(q);
-       ZERO_STRUCT(r);
 
-       /* Marshall data and send request */
-
-       init_samr_q_enum_dom_aliases(&q, pol, *start_idx, size);
-
-       CLI_DO_RPC(cli, mem_ctx, PI_SAMR, SAMR_ENUM_DOM_ALIASES,
-               q, r,
-               qbuf, rbuf,
-               samr_io_q_enum_dom_aliases,
-               samr_io_r_enum_dom_aliases,
-               NT_STATUS_UNSUCCESSFUL); 
-
-       /* Return output parameters */
-
-       result = r.status;
-
-       if (!NT_STATUS_IS_OK(result) &&
-           NT_STATUS_V(result) != NT_STATUS_V(STATUS_MORE_ENTRIES)) {
-               goto done;
-       }
-
-       *num_dom_aliases = r.num_entries2;
-
-       if (*num_dom_aliases == 0)
-               goto done;
-
-       if (!((*dom_aliases) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_dom_aliases))) {
-               result = NT_STATUS_NO_MEMORY;
-               goto done;
+       status = dcerpc_samr_chgpasswd_user(cli->binding_handle,
+                                           mem_ctx,
+                                           user_handle,
+                                           newpassword,
+                                           oldpassword,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       memset(*dom_aliases, 0, sizeof(struct acct_info) * *num_dom_aliases);
-
-       name_idx = 0;
-
-       for (i = 0; i < *num_dom_aliases; i++) {
-
-               (*dom_aliases)[i].rid = r.sam[i].rid;
-
-               if (r.sam[i].hdr_name.buffer) {
-                       unistr2_to_ascii((*dom_aliases)[i].acct_name,
-                                        &r.uni_grp_name[name_idx],
-                                        sizeof((*dom_aliases)[i].acct_name));
-                       name_idx++;
-               }
-
-               *start_idx = r.next_idx;
-       }
-
- done:
        return result;
 }
 
 /* User change password */
 
-NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
-                                   TALLOC_CTX *mem_ctx,
-                                   const char *username,
-                                   const char *newpassword,
-                                   const char *oldpassword)
+NTSTATUS dcerpc_samr_chgpasswd_user2(struct dcerpc_binding_handle *h,
+                                    TALLOC_CTX *mem_ctx,
+                                    const char *srv_name_slash,
+                                    const char *username,
+                                    const char *newpassword,
+                                    const char *oldpassword,
+                                    NTSTATUS *presult)
 {
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status;
        struct samr_CryptPassword new_nt_password;
        struct samr_CryptPassword new_lm_password;
        struct samr_Password old_nt_hash_enc;
        struct samr_Password old_lanman_hash_enc;
 
-       uchar old_nt_hash[16];
-       uchar old_lanman_hash[16];
-       uchar new_nt_hash[16];
-       uchar new_lanman_hash[16];
+       uint8_t old_nt_hash[16];
+       uint8_t old_lanman_hash[16];
+       uint8_t new_nt_hash[16];
+       uint8_t new_lanman_hash[16];
        struct lsa_String server, account;
-       char *srv_name_slash = NULL;
 
-       DEBUG(10,("rpccli_samr_chgpasswd_user\n"));
+       DEBUG(10,("rpccli_samr_chgpasswd_user2\n"));
 
        init_lsa_String(&server, srv_name_slash);
        init_lsa_String(&account, username);
 
-       srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
-       if (!srv_name_slash) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        /* Calculate the MD4 hash (NT compatible) of the password */
        E_md4hash(oldpassword, old_nt_hash);
        E_md4hash(newpassword, new_nt_hash);
@@ -208,7 +147,7 @@ NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
 
                encode_pw_buffer(new_lm_password.data, newpassword, STR_UNICODE);
 
-               SamOEMhash(new_lm_password.data, old_nt_hash, 516);
+               arcfour_crypt(new_lm_password.data, old_nt_hash, 516);
                E_old_pw_hash(new_nt_hash, old_lanman_hash, old_lanman_hash_enc.hash);
        } else {
                ZERO_STRUCT(new_lm_password);
@@ -217,75 +156,143 @@ NTSTATUS rpccli_samr_chgpasswd_user(struct rpc_pipe_client *cli,
 
        encode_pw_buffer(new_nt_password.data, newpassword, STR_UNICODE);
 
-       SamOEMhash(new_nt_password.data, old_nt_hash, 516);
+       arcfour_crypt(new_nt_password.data, old_nt_hash, 516);
        E_old_pw_hash(new_nt_hash, old_nt_hash, old_nt_hash_enc.hash);
 
-       result = rpccli_samr_ChangePasswordUser2(cli, mem_ctx,
+       status = dcerpc_samr_ChangePasswordUser2(h,
+                                                mem_ctx,
                                                 &server,
                                                 &account,
                                                 &new_nt_password,
                                                 &old_nt_hash_enc,
                                                 true,
                                                 &new_lm_password,
-                                                &old_lanman_hash_enc);
+                                                &old_lanman_hash_enc,
+                                                presult);
+
+       return status;
+}
+
+NTSTATUS rpccli_samr_chgpasswd_user2(struct rpc_pipe_client *cli,
+                                    TALLOC_CTX *mem_ctx,
+                                    const char *username,
+                                    const char *newpassword,
+                                    const char *oldpassword)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_samr_chgpasswd_user2(cli->binding_handle,
+                                            mem_ctx,
+                                            cli->srv_name_slash,
+                                            username,
+                                            newpassword,
+                                            oldpassword,
+                                            &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
        return result;
 }
 
 /* User change password given blobs */
 
-NTSTATUS rpccli_samr_chng_pswd_auth_crap(struct rpc_pipe_client *cli,
+NTSTATUS dcerpc_samr_chng_pswd_auth_crap(struct dcerpc_binding_handle *h,
                                         TALLOC_CTX *mem_ctx,
+                                        const char *srv_name_slash,
                                         const char *username,
                                         DATA_BLOB new_nt_password_blob,
                                         DATA_BLOB old_nt_hash_enc_blob,
                                         DATA_BLOB new_lm_password_blob,
-                                        DATA_BLOB old_lm_hash_enc_blob)
+                                        DATA_BLOB old_lm_hash_enc_blob,
+                                        NTSTATUS *presult)
 {
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status;
        struct samr_CryptPassword new_nt_password;
        struct samr_CryptPassword new_lm_password;
        struct samr_Password old_nt_hash_enc;
        struct samr_Password old_lm_hash_enc;
        struct lsa_String server, account;
-       char *srv_name_slash = NULL;
 
        DEBUG(10,("rpccli_samr_chng_pswd_auth_crap\n"));
 
-       srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
-       if (!srv_name_slash) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       ZERO_STRUCT(new_nt_password);
+       ZERO_STRUCT(new_lm_password);
+       ZERO_STRUCT(old_nt_hash_enc);
+       ZERO_STRUCT(old_lm_hash_enc);
 
        init_lsa_String(&server, srv_name_slash);
        init_lsa_String(&account, username);
 
-       memcpy(&new_nt_password.data, new_nt_password_blob.data, 516);
-       memcpy(&new_lm_password.data, new_lm_password_blob.data, 516);
-       memcpy(&old_nt_hash_enc.hash, old_nt_hash_enc_blob.data, 16);
-       memcpy(&old_lm_hash_enc.hash, old_lm_hash_enc_blob.data, 16);
+       if (new_nt_password_blob.data && new_nt_password_blob.length >= 516) {
+               memcpy(&new_nt_password.data, new_nt_password_blob.data, 516);
+       }
 
-       result = rpccli_samr_ChangePasswordUser2(cli, mem_ctx,
+       if (new_lm_password_blob.data && new_lm_password_blob.length >= 516) {
+               memcpy(&new_lm_password.data, new_lm_password_blob.data, 516);
+       }
+
+       if (old_nt_hash_enc_blob.data && old_nt_hash_enc_blob.length >= 16) {
+               memcpy(&old_nt_hash_enc.hash, old_nt_hash_enc_blob.data, 16);
+       }
+
+       if (old_lm_hash_enc_blob.data && old_lm_hash_enc_blob.length >= 16) {
+               memcpy(&old_lm_hash_enc.hash, old_lm_hash_enc_blob.data, 16);
+       }
+
+       status = dcerpc_samr_ChangePasswordUser2(h,
+                                                mem_ctx,
                                                 &server,
                                                 &account,
                                                 &new_nt_password,
                                                 &old_nt_hash_enc,
                                                 true,
                                                 &new_lm_password,
-                                                &old_lm_hash_enc);
-       return result;
+                                                &old_lm_hash_enc,
+                                                presult);
+
+       return status;
 }
 
+NTSTATUS rpccli_samr_chng_pswd_auth_crap(struct rpc_pipe_client *cli,
+                                        TALLOC_CTX *mem_ctx,
+                                        const char *username,
+                                        DATA_BLOB new_nt_password_blob,
+                                        DATA_BLOB old_nt_hash_enc_blob,
+                                        DATA_BLOB new_lm_password_blob,
+                                        DATA_BLOB old_lm_hash_enc_blob)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_samr_chng_pswd_auth_crap(cli->binding_handle,
+                                                mem_ctx,
+                                                cli->srv_name_slash,
+                                                username,
+                                                new_nt_password_blob,
+                                                old_nt_hash_enc_blob,
+                                                new_lm_password_blob,
+                                                old_lm_hash_enc_blob,
+                                                &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return result;
+}
 
 /* change password 3 */
 
-NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
-                               TALLOC_CTX *mem_ctx,
-                               const char *username,
-                               const char *newpassword,
-                               const char *oldpassword,
-                               struct samr_DomInfo1 **dominfo1,
-                               struct samr_ChangeReject **reject)
+NTSTATUS dcerpc_samr_chgpasswd_user3(struct dcerpc_binding_handle *h,
+                                    TALLOC_CTX *mem_ctx,
+                                    const char *srv_name_slash,
+                                    const char *username,
+                                    const char *newpassword,
+                                    const char *oldpassword,
+                                    struct samr_DomInfo1 **dominfo1,
+                                    struct userPwdChangeFailureInformation **reject,
+                                    NTSTATUS *presult)
 {
        NTSTATUS status;
 
@@ -294,21 +301,15 @@ NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
        struct samr_Password old_nt_hash_enc;
        struct samr_Password old_lanman_hash_enc;
 
-       uchar old_nt_hash[16];
-       uchar old_lanman_hash[16];
-       uchar new_nt_hash[16];
-       uchar new_lanman_hash[16];
+       uint8_t old_nt_hash[16];
+       uint8_t old_lanman_hash[16];
+       uint8_t new_nt_hash[16];
+       uint8_t new_lanman_hash[16];
 
        struct lsa_String server, account;
-       char *srv_name_slash = NULL;
 
        DEBUG(10,("rpccli_samr_chgpasswd_user3\n"));
 
-       srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
-       if (!srv_name_slash) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        init_lsa_String(&server, srv_name_slash);
        init_lsa_String(&account, username);
 
@@ -326,7 +327,7 @@ NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
 
                encode_pw_buffer(new_lm_password.data, newpassword, STR_UNICODE);
 
-               SamOEMhash(new_lm_password.data, old_nt_hash, 516);
+               arcfour_crypt(new_lm_password.data, old_nt_hash, 516);
                E_old_pw_hash(new_nt_hash, old_lanman_hash, old_lanman_hash_enc.hash);
        } else {
                ZERO_STRUCT(new_lm_password);
@@ -335,10 +336,11 @@ NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
 
        encode_pw_buffer(new_nt_password.data, newpassword, STR_UNICODE);
 
-       SamOEMhash(new_nt_password.data, old_nt_hash, 516);
+       arcfour_crypt(new_nt_password.data, old_nt_hash, 516);
        E_old_pw_hash(new_nt_hash, old_nt_hash, old_nt_hash_enc.hash);
 
-       status = rpccli_samr_ChangePasswordUser3(cli, mem_ctx,
+       status = dcerpc_samr_ChangePasswordUser3(h,
+                                                mem_ctx,
                                                 &server,
                                                 &account,
                                                 &new_nt_password,
@@ -348,17 +350,47 @@ NTSTATUS rpccli_samr_chgpasswd3(struct rpc_pipe_client *cli,
                                                 &old_lanman_hash_enc,
                                                 NULL,
                                                 dominfo1,
-                                                reject);
+                                                reject,
+                                                presult);
+
        return status;
 }
 
+NTSTATUS rpccli_samr_chgpasswd_user3(struct rpc_pipe_client *cli,
+                                    TALLOC_CTX *mem_ctx,
+                                    const char *username,
+                                    const char *newpassword,
+                                    const char *oldpassword,
+                                    struct samr_DomInfo1 **dominfo1,
+                                    struct userPwdChangeFailureInformation **reject)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_samr_chgpasswd_user3(cli->binding_handle,
+                                            mem_ctx,
+                                            cli->srv_name_slash,
+                                            username,
+                                            newpassword,
+                                            oldpassword,
+                                            dominfo1,
+                                            reject,
+                                            &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return result;
+}
+
 /* This function returns the bizzare set of (max_entries, max_size) required
    for the QueryDisplayInfo RPC to actually work against a domain controller
    with large (10k and higher) numbers of users.  These values were 
    obtained by inspection using ethereal and NT4 running User Manager. */
 
-void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
-                              uint32 *max_size)
+void dcerpc_get_query_dispinfo_params(int loop_count,
+                                     uint32_t *max_entries,
+                                     uint32_t *max_size)
 {
        switch(loop_count) {
        case 0:
@@ -383,3 +415,78 @@ void get_query_dispinfo_params(int loop_count, uint32 *max_entries,
                break;
        }
 }
+
+NTSTATUS dcerpc_try_samr_connects(struct dcerpc_binding_handle *h,
+                                 TALLOC_CTX *mem_ctx,
+                                 const char *srv_name_slash,
+                                 uint32_t access_mask,
+                                 struct policy_handle *connect_pol,
+                                 NTSTATUS *presult)
+{
+       NTSTATUS status;
+       union samr_ConnectInfo info_in, info_out;
+       struct samr_ConnectInfo1 info1;
+       uint32_t lvl_out = 0;
+
+       ZERO_STRUCT(info1);
+
+       info1.client_version = SAMR_CONNECT_W2K;
+       info_in.info1 = info1;
+
+       status = dcerpc_samr_Connect5(h,
+                                     mem_ctx,
+                                     srv_name_slash,
+                                     access_mask,
+                                     1,
+                                     &info_in,
+                                     &lvl_out,
+                                     &info_out,
+                                     connect_pol,
+                                     presult);
+       if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(*presult)) {
+               return status;
+       }
+
+       status = dcerpc_samr_Connect4(h,
+                                     mem_ctx,
+                                     srv_name_slash,
+                                     SAMR_CONNECT_W2K,
+                                     access_mask,
+                                     connect_pol,
+                                     presult);
+       if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(*presult)) {
+               return status;
+       }
+
+       status = dcerpc_samr_Connect2(h,
+                                     mem_ctx,
+                                     srv_name_slash,
+                                     access_mask,
+                                     connect_pol,
+                                     presult);
+
+       return status;
+}
+
+NTSTATUS rpccli_try_samr_connects(struct rpc_pipe_client *cli,
+                                 TALLOC_CTX *mem_ctx,
+                                 uint32_t access_mask,
+                                 struct policy_handle *connect_pol)
+{
+       NTSTATUS status;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       status = dcerpc_try_samr_connects(cli->binding_handle,
+                                         mem_ctx,
+                                         cli->srv_name_slash,
+                                         access_mask,
+                                         connect_pol,
+                                         &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return result;
+}
+
+/* vim: set ts=8 sw=8 noet cindent: */