s3-spoolss: fix rpcclient after setprinter IDL fixes.
[ira/wip.git] / source3 / rpcclient / cmd_samr.c
index bec7227e83f1800449975def9a8698c780ee177c..e21a57fba7a3137cf9693d7bbda265d25862fd29 100644 (file)
@@ -6,6 +6,7 @@
    Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
    Copyright (C) Elrond                            2000,
    Copyright (C) Tim Potter                        2000
+   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 "rpcclient.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/cli_samr.h"
 
 extern DOM_SID domain_sid;
 
-static void init_lsa_String(struct lsa_String *name, const char *s)
+/****************************************************************************
+ display samr_user_info_7 structure
+ ****************************************************************************/
+static void display_samr_user_info_7(struct samr_UserInfo7 *r)
 {
-       name->string = s;
+       printf("\tUser Name   :\t%s\n", r->account_name.string);
 }
 
 /****************************************************************************
- display sam_user_info_7 structure
+ display samr_user_info_9 structure
  ****************************************************************************/
-static void display_sam_user_info_7(SAM_USER_INFO_7 *usr)
+static void display_samr_user_info_9(struct samr_UserInfo9 *r)
 {
-       fstring temp;
-
-       unistr2_to_ascii(temp, &usr->uni_name, sizeof(temp));
-       printf("\tUser Name   :\t%s\n", temp);
+       printf("\tPrimary group RID   :\tox%x\n", r->primary_gid);
 }
 
 /****************************************************************************
- display sam_user_info_9 structure
+ display samr_user_info_16 structure
  ****************************************************************************/
-static void display_sam_user_info_9(SAM_USER_INFO_9 *usr)
+static void display_samr_user_info_16(struct samr_UserInfo16 *r)
 {
-       printf("\tPrimary group RID   :\tox%x\n", usr->rid_group);
+       printf("\tAcct Flags   :\tox%x\n", r->acct_flags);
 }
 
 /****************************************************************************
- display sam_user_info_16 structure
+ display samr_user_info_20 structure
  ****************************************************************************/
-static void display_sam_user_info_16(SAM_USER_INFO_16 *usr)
+static void display_samr_user_info_20(struct samr_UserInfo20 *r)
 {
-       printf("\tAcct Flags   :\tox%x\n", usr->acb_info);
+       printf("\tRemote Dial :\n");
+       dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
 }
 
+
 /****************************************************************************
- display sam_user_info_21 structure
+ display samr_user_info_21 structure
  ****************************************************************************/
-static void display_sam_user_info_21(SAM_USER_INFO_21 *usr)
+static void display_samr_user_info_21(struct samr_UserInfo21 *r)
 {
-       fstring temp;
-
-       unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp));
-       printf("\tUser Name   :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp));
-       printf("\tFull Name   :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp));
-       printf("\tHome Drive  :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp));
-       printf("\tDir Drive   :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp));
-       printf("\tProfile Path:\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp));
-       printf("\tLogon Script:\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp));
-       printf("\tDescription :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp));
-       printf("\tWorkstations:\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_comment, sizeof(temp));
-       printf("\tUnknown Str :\t%s\n", temp);
-
-       unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp));
-       printf("\tRemote Dial :\t%s\n", temp);
+       printf("\tUser Name   :\t%s\n", r->account_name.string);
+       printf("\tFull Name   :\t%s\n", r->full_name.string);
+       printf("\tHome Drive  :\t%s\n", r->home_directory.string);
+       printf("\tDir Drive   :\t%s\n", r->home_drive.string);
+       printf("\tProfile Path:\t%s\n", r->profile_path.string);
+       printf("\tLogon Script:\t%s\n", r->logon_script.string);
+       printf("\tDescription :\t%s\n", r->description.string);
+       printf("\tWorkstations:\t%s\n", r->workstations.string);
+       printf("\tComment     :\t%s\n", r->comment.string);
+       printf("\tRemote Dial :\n");
+       dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
 
        printf("\tLogon Time               :\t%s\n",
-              http_timestring(nt_time_to_unix(usr->logon_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->last_logon)));
        printf("\tLogoff Time              :\t%s\n",
-              http_timestring(nt_time_to_unix(usr->logoff_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->last_logoff)));
        printf("\tKickoff Time             :\t%s\n",
-              http_timestring(nt_time_to_unix(usr->kickoff_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->acct_expiry)));
        printf("\tPassword last set Time   :\t%s\n",
-              http_timestring(nt_time_to_unix(usr->pass_last_set_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->last_password_change)));
        printf("\tPassword can change Time :\t%s\n",
-              http_timestring(nt_time_to_unix(usr->pass_can_change_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->allow_password_change)));
        printf("\tPassword must change Time:\t%s\n",
-              http_timestring(nt_time_to_unix(usr->pass_must_change_time)));
+              http_timestring(talloc_tos(), nt_time_to_unix(r->force_password_change)));
 
        printf("\tunknown_2[0..31]...\n"); /* user passwords? */
 
-       printf("\tuser_rid :\t0x%x\n"  , usr->user_rid ); /* User ID */
-       printf("\tgroup_rid:\t0x%x\n"  , usr->group_rid); /* Group ID */
-       printf("\tacb_info :\t0x%08x\n", usr->acb_info ); /* Account Control Info */
+       printf("\tuser_rid :\t0x%x\n"  , r->rid); /* User ID */
+       printf("\tgroup_rid:\t0x%x\n"  , r->primary_gid); /* Group ID */
+       printf("\tacb_info :\t0x%08x\n", r->acct_flags); /* Account Control Info */
 
-       printf("\tfields_present:\t0x%08x\n", usr->fields_present); /* 0x00ff ffff */
-       printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */
-       printf("\tbad_password_count:\t0x%08x\n", usr->bad_password_count);
-       printf("\tlogon_count:\t0x%08x\n", usr->logon_count);
+       printf("\tfields_present:\t0x%08x\n", r->fields_present); /* 0x00ff ffff */
+       printf("\tlogon_divs:\t%d\n", r->logon_hours.units_per_week); /* 0x0000 00a8 which is 168 which is num hrs in a week */
+       printf("\tbad_password_count:\t0x%08x\n", r->bad_password_count);
+       printf("\tlogon_count:\t0x%08x\n", r->logon_count);
 
        printf("\tpadding1[0..7]...\n");
 
-       if (usr->ptr_logon_hrs) {
-               printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len);
+       if (r->logon_hours.bits) {
+               printf("\tlogon_hrs[0..%d]...\n", r->logon_hours.units_per_week/8);
        }
 }
 
@@ -163,24 +148,24 @@ static void display_sam_dom_info_1(struct samr_DomInfo1 *info1)
                display_time(info1->min_password_age));
 }
 
-static void display_sam_dom_info_2(struct samr_DomInfo2 *info2)
+static void display_sam_dom_info_2(struct samr_DomGeneralInformation *general)
 {
-       printf("Domain:\t\t%s\n", info2->domain_name.string);
-       printf("Server:\t\t%s\n", info2->primary.string);
-       printf("Comment:\t%s\n", info2->comment.string);
+       printf("Domain:\t\t%s\n", general->domain_name.string);
+       printf("Server:\t\t%s\n", general->primary.string);
+       printf("Comment:\t%s\n", general->oem_information.string);
 
-       printf("Total Users:\t%d\n", info2->num_users);
-       printf("Total Groups:\t%d\n", info2->num_groups);
-       printf("Total Aliases:\t%d\n", info2->num_aliases);
+       printf("Total Users:\t%d\n", general->num_users);
+       printf("Total Groups:\t%d\n", general->num_groups);
+       printf("Total Aliases:\t%d\n", general->num_aliases);
 
-       printf("Sequence No:\t%llu\n", (unsigned long long)info2->sequence_num);
+       printf("Sequence No:\t%llu\n", (unsigned long long)general->sequence_num);
 
        printf("Force Logoff:\t%d\n",
-               (int)nt_time_to_unix_abs(&info2->force_logoff_time));
+               (int)nt_time_to_unix_abs(&general->force_logoff_time));
 
-       printf("Unknown 2:\t0x%x\n", info2->unknown2);
-       printf("Server Role:\t%s\n", server_role_str(info2->role));
-       printf("Unknown 3:\t0x%x\n", info2->unknown3);
+       printf("Domain Server State:\t0x%x\n", general->domain_server_state);
+       printf("Server Role:\t%s\n", server_role_str(general->role));
+       printf("Unknown 3:\t0x%x\n", general->unknown3);
 }
 
 static void display_sam_dom_info_3(struct samr_DomInfo3 *info3)
@@ -189,9 +174,9 @@ static void display_sam_dom_info_3(struct samr_DomInfo3 *info3)
                (int)nt_time_to_unix_abs(&info3->force_logoff_time));
 }
 
-static void display_sam_dom_info_4(struct samr_DomInfo4 *info4)
+static void display_sam_dom_info_4(struct samr_DomOEMInformation *oem)
 {
-       printf("Comment:\t%s\n", info4->comment.string);
+       printf("Comment:\t%s\n", oem->oem_information.string);
 }
 
 static void display_sam_dom_info_5(struct samr_DomInfo5 *info5)
@@ -213,12 +198,12 @@ static void display_sam_dom_info_8(struct samr_DomInfo8 *info8)
 {
        printf("Sequence No:\t%llu\n", (unsigned long long)info8->sequence_num);
        printf("Domain Create Time:\t%s\n",
-               http_timestring(nt_time_to_unix(info8->domain_create_time)));
+               http_timestring(talloc_tos(), nt_time_to_unix(info8->domain_create_time)));
 }
 
 static void display_sam_dom_info_9(struct samr_DomInfo9 *info9)
 {
-       printf("unknown:\t%d (0x%08x)\n", info9->unknown, info9->unknown);
+       printf("Domain Server State:\t0x%x\n", info9->domain_server_state);
 }
 
 static void display_sam_dom_info_12(struct samr_DomInfo12 *info12)
@@ -235,17 +220,16 @@ static void display_sam_dom_info_13(struct samr_DomInfo13 *info13)
 {
        printf("Sequence No:\t%llu\n", (unsigned long long)info13->sequence_num);
        printf("Domain Create Time:\t%s\n",
-               http_timestring(nt_time_to_unix(info13->domain_create_time)));
-       printf("Unknown1:\t%d\n", info13->unknown1);
-       printf("Unknown2:\t%d\n", info13->unknown2);
-
+               http_timestring(talloc_tos(), nt_time_to_unix(info13->domain_create_time)));
+       printf("Sequence No at last promotion:\t%llu\n",
+               (unsigned long long)info13->modified_count_at_last_promotion);
 }
 
 static void display_sam_info_1(struct samr_DispEntryGeneral *r)
 {
        printf("index: 0x%x ", r->idx);
        printf("RID: 0x%x ", r->rid);
-       printf("acb: 0x%x ", r->acct_flags);
+       printf("acb: 0x%08x ", r->acct_flags);
        printf("Account: %s\t", r->account_name.string);
        printf("Name: %s\t", r->full_name.string);
        printf("Desc: %s\n", r->description.string);
@@ -255,7 +239,7 @@ static void display_sam_info_2(struct samr_DispEntryFull *r)
 {
        printf("index: 0x%x ", r->idx);
        printf("RID: 0x%x ", r->rid);
-       printf("acb: 0x%x ", r->acct_flags);
+       printf("acb: 0x%08x ", r->acct_flags);
        printf("Account: %s\t", r->account_name.string);
        printf("Desc: %s\n", r->description.string);
 }
@@ -264,7 +248,7 @@ static void display_sam_info_3(struct samr_DispEntryFullGroup *r)
 {
        printf("index: 0x%x ", r->idx);
        printf("RID: 0x%x ", r->rid);
-       printf("acb: 0x%x ", r->acct_flags);
+       printf("acb: 0x%08x ", r->acct_flags);
        printf("Account: %s\t", r->account_name.string);
        printf("Desc: %s\n", r->description.string);
 }
@@ -282,25 +266,32 @@ static void display_sam_info_5(struct samr_DispEntryAscii *r)
 }
 
 /****************************************************************************
- Try samr_connect4 first, then samr_connect2 if it fails
  ****************************************************************************/
-static NTSTATUS try_samr_connects(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
-                                 uint32 access_mask, POLICY_HND *connect_pol)
+
+static NTSTATUS get_domain_handle(struct rpc_pipe_client *cli,
+                                 TALLOC_CTX *mem_ctx,
+                                 const char *sam,
+                                 struct policy_handle *connect_pol,
+                                 uint32_t access_mask,
+                                 struct dom_sid *_domain_sid,
+                                 struct policy_handle *domain_pol)
 {
-       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
-       result = rpccli_samr_Connect4(cli, mem_ctx,
-                                     cli->cli->desthost,
-                                     SAMR_CONNECT_W2K,
-                                     access_mask,
-                                     connect_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               result = rpccli_samr_Connect2(cli, mem_ctx,
-                                             cli->cli->desthost,
+       if (StrCaseCmp(sam, "domain") == 0) {
+               return rpccli_samr_OpenDomain(cli, mem_ctx,
+                                             connect_pol,
+                                             access_mask,
+                                             _domain_sid,
+                                             domain_pol);
+       } else if (StrCaseCmp(sam, "builtin") == 0) {
+               return rpccli_samr_OpenDomain(cli, mem_ctx,
+                                             connect_pol,
                                              access_mask,
-                                             connect_pol);
+                                             CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
+                                             domain_pol);
        }
-       return result;
+
+       return NT_STATUS_INVALID_PARAMETER;
 }
 
 /**********************************************************************
@@ -310,12 +301,11 @@ static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
                                     TALLOC_CTX *mem_ctx,
                                     int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, user_pol;
+       struct policy_handle connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 info_level = 21;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
-       SAM_USERINFO_CTR *user_ctr;
-       fstring server;
+       union samr_UserInfo *info = NULL;
        uint32 user_rid = 0;
 
        if ((argc < 2) || (argc > 4)) {
@@ -332,11 +322,9 @@ static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
                sscanf(argv[3], "%x", &access_mask);
 
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -384,26 +372,29 @@ static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       ZERO_STRUCT(user_ctr);
-
-       result = rpccli_samr_query_userinfo(cli, mem_ctx, &user_pol,
-                                        info_level, &user_ctr);
+       result = rpccli_samr_QueryUserInfo(cli, mem_ctx,
+                                          &user_pol,
+                                          info_level,
+                                          &info);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       switch (user_ctr->switch_value) {
+       switch (info_level) {
        case 7:
-               display_sam_user_info_7(user_ctr->info.id7);
+               display_samr_user_info_7(&info->info7);
                break;
        case 9:
-               display_sam_user_info_9(user_ctr->info.id9);
+               display_samr_user_info_9(&info->info9);
                break;
        case 16:
-               display_sam_user_info_16(user_ctr->info.id16);
+               display_samr_user_info_16(&info->info16);
+               break;
+       case 20:
+               display_samr_user_info_20(&info->info20);
                break;
        case 21:
-               display_sam_user_info_21(user_ctr->info.id21);
+               display_samr_user_info_21(&info->info21);
                break;
        default:
                printf("Unsupported infolevel: %d\n", info_level);
@@ -498,12 +489,11 @@ static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
                                      TALLOC_CTX *mem_ctx,
                                      int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, group_pol;
+       struct policy_handle connect_pol, domain_pol, group_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        enum samr_GroupInfoEnum info_level = GROUPINFOALL;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        union samr_GroupInfo *group_info = NULL;
-       fstring                 server;
        uint32 group_rid;
 
        if ((argc < 2) || (argc > 4)) {
@@ -519,11 +509,9 @@ static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
        if (argc > 3)
                sscanf(argv[3], "%x", &access_mask);
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -569,14 +557,13 @@ static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
                                           TALLOC_CTX *mem_ctx,
                                           int argc, const char **argv)
 {
-       POLICY_HND              connect_pol,
+       struct policy_handle            connect_pol,
                                domain_pol,
                                user_pol;
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
        uint32                  user_rid;
        uint32                  access_mask = MAXIMUM_ALLOWED_ACCESS;
        int                     i;
-       fstring                 server;
        struct samr_RidWithAttributeArray *rid_array = NULL;
 
        if ((argc < 2) || (argc > 3)) {
@@ -589,11 +576,9 @@ static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
        if (argc > 2)
                sscanf(argv[2], "%x", &access_mask);
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -641,13 +626,12 @@ static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
                                           TALLOC_CTX *mem_ctx,
                                           int argc, const char **argv)
 {
-       POLICY_HND              connect_pol, domain_pol;
+       struct policy_handle            connect_pol, domain_pol;
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
        DOM_SID                *sids;
        size_t                     num_sids;
        uint32                  access_mask = MAXIMUM_ALLOWED_ACCESS;
        int                     i;
-       fstring                 server;
        struct lsa_SidArray sid_array;
        struct samr_Ids alias_rids;
 
@@ -688,30 +672,18 @@ static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
 
        sid_array.num_sids = num_sids;
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               access_mask,
-                                               &domain_sid, &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               access_mask,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else {
-               printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
-               return NT_STATUS_INVALID_PARAMETER;
-       }
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  access_mask,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -739,12 +711,11 @@ static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
                                         TALLOC_CTX *mem_ctx,
                                         int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, group_pol;
+       struct policy_handle connect_pol, domain_pol, group_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 group_rid;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        int i;
-       fstring                 server;
        unsigned int old_timeout;
        struct samr_RidTypeArray *rids = NULL;
 
@@ -758,11 +729,9 @@ static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
        if (argc > 2)
                sscanf(argv[2], "%x", &access_mask);
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -786,13 +755,14 @@ static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
                goto done;
 
        /* Make sure to wait for our DC's reply */
-       old_timeout = cli_set_timeout(cli->cli, MAX(cli->cli->timeout,30000)); /* 30 seconds. */
+       old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
+       rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
 
        result = rpccli_samr_QueryGroupMember(cli, mem_ctx,
                                              &group_pol,
                                              &rids);
 
-       cli_set_timeout(cli->cli, old_timeout);
+       rpccli_set_timeout(cli, old_timeout);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -815,74 +785,79 @@ static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
                                        TALLOC_CTX *mem_ctx,
                                        int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 start_idx, size, num_dom_users, i;
-       char **dom_users;
-       uint32 *dom_rids;
+       uint32 start_idx, num_dom_users, i;
+       struct samr_SamArray *dom_users = NULL;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        uint32 acb_mask = ACB_NORMAL;
-       bool got_connect_pol = False, got_domain_pol = False;
+       uint32_t size = 0xffff;
 
-       if ((argc < 1) || (argc > 3)) {
-               printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
+       if ((argc < 1) || (argc > 4)) {
+               printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (argc > 1)
+       if (argc > 1) {
                sscanf(argv[1], "%x", &access_mask);
+       }
 
-       if (argc > 2)
+       if (argc > 2) {
                sscanf(argv[2], "%x", &acb_mask);
+       }
+
+       if (argc > 3) {
+               sscanf(argv[3], "%x", &size);
+       }
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                       &connect_pol,
-                                       access_mask,
-                                       &domain_sid,
-                                       &domain_pol);
-
+       result = get_domain_handle(cli, mem_ctx, "domain",
+                                  &connect_pol,
+                                  access_mask,
+                                  &domain_sid,
+                                  &domain_pol);
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_domain_pol = True;
-
        /* Enumerate domain users */
 
        start_idx = 0;
-       size = 0xffff;
 
        do {
-               result = rpccli_samr_enum_dom_users(
-                       cli, mem_ctx, &domain_pol, &start_idx, acb_mask,
-                       size, &dom_users, &dom_rids, &num_dom_users);
+               result = rpccli_samr_EnumDomainUsers(cli, mem_ctx,
+                                                    &domain_pol,
+                                                    &start_idx,
+                                                    acb_mask,
+                                                    &dom_users,
+                                                    size,
+                                                    &num_dom_users);
 
                if (NT_STATUS_IS_OK(result) ||
                    NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 
                        for (i = 0; i < num_dom_users; i++)
                                printf("user:[%s] rid:[0x%x]\n",
-                                      dom_users[i], dom_rids[i]);
+                                      dom_users->entries[i].name.string,
+                                      dom_users->entries[i].idx);
                }
 
        } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 
  done:
-       if (got_domain_pol)
+       if (is_valid_policy_hnd(&domain_pol))
                rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 
-       if (got_connect_pol)
+       if (is_valid_policy_hnd(&connect_pol))
                rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 
        return result;
@@ -894,70 +869,72 @@ static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 start_idx, size, num_dom_groups, i;
+       uint32 start_idx, num_dom_groups, i;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
-       struct acct_info *dom_groups;
-       bool got_connect_pol = False, got_domain_pol = False;
+       struct samr_SamArray *dom_groups = NULL;
+       uint32_t size = 0xffff;
 
-       if ((argc < 1) || (argc > 2)) {
-               printf("Usage: %s [access_mask]\n", argv[0]);
+       if ((argc < 1) || (argc > 3)) {
+               printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (argc > 1)
+       if (argc > 1) {
                sscanf(argv[1], "%x", &access_mask);
+       }
+
+       if (argc > 2) {
+               sscanf(argv[2], "%x", &size);
+       }
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
-       result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                       &connect_pol,
-                                       access_mask,
-                                       &domain_sid,
-                                       &domain_pol);
-
+       result = get_domain_handle(cli, mem_ctx, "domain",
+                                  &connect_pol,
+                                  access_mask,
+                                  &domain_sid,
+                                  &domain_pol);
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_domain_pol = True;
-
        /* Enumerate domain groups */
 
        start_idx = 0;
-       size = 0xffff;
 
        do {
-               result = rpccli_samr_enum_dom_groups(
-                       cli, mem_ctx, &domain_pol, &start_idx, size,
-                       &dom_groups, &num_dom_groups);
-
+               result = rpccli_samr_EnumDomainGroups(cli, mem_ctx,
+                                                     &domain_pol,
+                                                     &start_idx,
+                                                     &dom_groups,
+                                                     size,
+                                                     &num_dom_groups);
                if (NT_STATUS_IS_OK(result) ||
                    NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 
                        for (i = 0; i < num_dom_groups; i++)
                                printf("group:[%s] rid:[0x%x]\n",
-                                      dom_groups[i].acct_name,
-                                      dom_groups[i].rid);
+                                      dom_groups->entries[i].name.string,
+                                      dom_groups->entries[i].idx);
                }
 
        } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 
  done:
-       if (got_domain_pol)
+       if (is_valid_policy_hnd(&domain_pol))
                rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 
-       if (got_connect_pol)
+       if (is_valid_policy_hnd(&connect_pol))
                rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 
        return result;
@@ -969,90 +946,148 @@ static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 start_idx, size, num_als_groups, i;
+       uint32 start_idx, num_als_groups, i;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
-       struct acct_info *als_groups;
-       bool got_connect_pol = False, got_domain_pol = False;
+       struct samr_SamArray *als_groups = NULL;
+       uint32_t size = 0xffff;
 
-       if ((argc < 2) || (argc > 3)) {
-               printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
+       if ((argc < 2) || (argc > 4)) {
+               printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (argc > 2)
+       if (argc > 2) {
                sscanf(argv[2], "%x", &access_mask);
+       }
+
+       if (argc > 3) {
+               sscanf(argv[3], "%x", &size);
+       }
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               access_mask,
-                                               &domain_sid,
-                                               &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               access_mask,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else
-               return NT_STATUS_OK;
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  access_mask,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       got_domain_pol = True;
-
        /* Enumerate alias groups */
 
        start_idx = 0;
-       size = 0xffff;          /* Number of groups to retrieve */
 
        do {
-               result = rpccli_samr_enum_als_groups(
-                       cli, mem_ctx, &domain_pol, &start_idx, size,
-                       &als_groups, &num_als_groups);
+               result = rpccli_samr_EnumDomainAliases(cli, mem_ctx,
+                                                      &domain_pol,
+                                                      &start_idx,
+                                                      &als_groups,
+                                                      size,
+                                                      &num_als_groups);
 
                if (NT_STATUS_IS_OK(result) ||
                    NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 
                        for (i = 0; i < num_als_groups; i++)
                                printf("group:[%s] rid:[0x%x]\n",
-                                      als_groups[i].acct_name,
-                                      als_groups[i].rid);
+                                      als_groups->entries[i].name.string,
+                                      als_groups->entries[i].idx);
                }
        } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 
  done:
-       if (got_domain_pol)
+       if (is_valid_policy_hnd(&domain_pol))
                rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 
-       if (got_connect_pol)
+       if (is_valid_policy_hnd(&connect_pol))
                rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 
        return result;
 }
 
+/* Enumerate domains */
+
+static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
+                                     TALLOC_CTX *mem_ctx,
+                                     int argc, const char **argv)
+{
+       struct policy_handle connect_pol;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       uint32 start_idx, size, num_entries, i;
+       uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
+       struct samr_SamArray *sam = NULL;
+
+       if ((argc < 1) || (argc > 2)) {
+               printf("Usage: %s [access mask]\n", argv[0]);
+               return NT_STATUS_OK;
+       }
+
+       if (argc > 1) {
+               sscanf(argv[1], "%x", &access_mask);
+       }
+
+       /* Get sam policy handle */
+
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         access_mask,
+                                         &connect_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       /* Enumerate alias groups */
+
+       start_idx = 0;
+       size = 0xffff;
+
+       do {
+               result = rpccli_samr_EnumDomains(cli, mem_ctx,
+                                                &connect_pol,
+                                                &start_idx,
+                                                &sam,
+                                                size,
+                                                &num_entries);
+
+               if (NT_STATUS_IS_OK(result) ||
+                   NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
+
+                       for (i = 0; i < num_entries; i++)
+                               printf("name:[%s] idx:[0x%x]\n",
+                                      sam->entries[i].name.string,
+                                      sam->entries[i].idx);
+               }
+       } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
+
+ done:
+       if (is_valid_policy_hnd(&connect_pol)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_pol);
+       }
+
+       return result;
+}
+
+
 /* Query alias membership */
 
 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
                                         TALLOC_CTX *mem_ctx,
                                         int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, alias_pol;
+       struct policy_handle connect_pol, domain_pol, alias_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 alias_rid, i;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -1070,28 +1105,20 @@ static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
 
        /* Open SAMR handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
        /* Open handle on domain */
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               &domain_sid,
-                                               &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else
-               return NT_STATUS_OK;
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  MAXIMUM_ALLOWED_ACCESS,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1133,7 +1160,7 @@ static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
                                         TALLOC_CTX *mem_ctx,
                                         int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, alias_pol;
+       struct policy_handle connect_pol, domain_pol, alias_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32_t alias_rid;
        uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
@@ -1148,19 +1175,19 @@ static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
 
        sscanf(argv[2], "%i", &alias_rid);
 
-       if (argc > 3) {
+       if (argc > 2) {
                level = atoi(argv[3]);
        }
 
-       if (argc > 4) {
+       if (argc > 3) {
                sscanf(argv[4], "%x", &access_mask);
        }
 
        /* Open SAMR handle */
 
-       result = try_samr_connects(cli, mem_ctx,
-                                  SEC_FLAG_MAXIMUM_ALLOWED,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         SEC_FLAG_MAXIMUM_ALLOWED,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
@@ -1168,25 +1195,11 @@ static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
 
        /* Open handle on domain */
 
-       if (strequal(argv[1], "domain")) {
-
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               SEC_FLAG_MAXIMUM_ALLOWED,
-                                               &domain_sid,
-                                               &domain_pol);
-
-       } else if (strequal(argv[1], "builtin")) {
-
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               SEC_FLAG_MAXIMUM_ALLOWED,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-
-       } else {
-               return NT_STATUS_OK;
-       }
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  SEC_FLAG_MAXIMUM_ALLOWED,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
@@ -1242,7 +1255,7 @@ static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
                                      TALLOC_CTX *mem_ctx,
                                      int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, alias_pol;
+       struct policy_handle connect_pol, domain_pol, alias_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 alias_rid;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -1256,28 +1269,20 @@ static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
 
        /* Open SAMR handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
        /* Open handle on domain */
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               &domain_sid,
-                                               &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else
-               return NT_STATUS_INVALID_PARAMETER;
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  MAXIMUM_ALLOWED_ACCESS,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1326,13 +1331,14 @@ static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
 
 /* Query display info */
 
-static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
-                                        TALLOC_CTX *mem_ctx,
-                                        int argc, const char **argv)
+static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
+                                                TALLOC_CTX *mem_ctx,
+                                                int argc, const char **argv,
+                                                uint32_t opcode)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries, i;
+       uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        uint32 info_level = 1;
        union samr_DispInfo info;
@@ -1366,8 +1372,9 @@ static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1391,15 +1398,51 @@ static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
                        get_query_dispinfo_params(
                                loop_count, &max_entries, &max_size);
 
-               result = rpccli_samr_QueryDisplayInfo(cli, mem_ctx,
-                                                     &domain_pol,
-                                                     info_level,
-                                                     start_idx,
-                                                     max_entries,
-                                                     max_size,
-                                                     &total_size,
-                                                     &returned_size,
-                                                     &info);
+               switch (opcode) {
+               case NDR_SAMR_QUERYDISPLAYINFO:
+                       result = rpccli_samr_QueryDisplayInfo(cli, mem_ctx,
+                                                             &domain_pol,
+                                                             info_level,
+                                                             start_idx,
+                                                             max_entries,
+                                                             max_size,
+                                                             &total_size,
+                                                             &returned_size,
+                                                             &info);
+                       break;
+               case NDR_SAMR_QUERYDISPLAYINFO2:
+                       result = rpccli_samr_QueryDisplayInfo2(cli, mem_ctx,
+                                                              &domain_pol,
+                                                              info_level,
+                                                              start_idx,
+                                                              max_entries,
+                                                              max_size,
+                                                              &total_size,
+                                                              &returned_size,
+                                                              &info);
+
+                       break;
+               case NDR_SAMR_QUERYDISPLAYINFO3:
+                       result = rpccli_samr_QueryDisplayInfo3(cli, mem_ctx,
+                                                              &domain_pol,
+                                                              info_level,
+                                                              start_idx,
+                                                              max_entries,
+                                                              max_size,
+                                                              &total_size,
+                                                              &returned_size,
+                                                              &info);
+
+                       break;
+               default:
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               if (!NT_STATUS_IS_OK(result) &&
+                   !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
+                   !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
+                       break;
+               }
 
                loop_count++;
 
@@ -1425,9 +1468,6 @@ static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
 
                start_idx += num_entries;
 
-               if (NT_STATUS_IS_ERR(result))
-                       break;
-
                if (num_entries == 0)
                        break;
 
@@ -1458,13 +1498,37 @@ static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
        return result;
 }
 
+static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc, const char **argv)
+{
+       return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
+                                               NDR_SAMR_QUERYDISPLAYINFO);
+}
+
+static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, const char **argv)
+{
+       return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
+                                               NDR_SAMR_QUERYDISPLAYINFO2);
+}
+
+static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, const char **argv)
+{
+       return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
+                                               NDR_SAMR_QUERYDISPLAYINFO3);
+}
+
 /* Query domain info */
 
 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
                                        TALLOC_CTX *mem_ctx,
                                        int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 switch_level = 2;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -1483,8 +1547,9 @@ static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1517,13 +1582,13 @@ static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
                display_sam_dom_info_1(&info->info1);
                break;
        case 2:
-               display_sam_dom_info_2(&info->info2);
+               display_sam_dom_info_2(&info->general);
                break;
        case 3:
                display_sam_dom_info_3(&info->info3);
                break;
        case 4:
-               display_sam_dom_info_4(&info->info4);
+               display_sam_dom_info_4(&info->oem);
                break;
        case 5:
                display_sam_dom_info_5(&info->info5);
@@ -1566,7 +1631,7 @@ static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
                                          TALLOC_CTX *mem_ctx,
                                          int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, user_pol;
+       struct policy_handle connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        struct lsa_String acct_name;
        uint32 acb_info;
@@ -1586,8 +1651,9 @@ static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1643,7 +1709,7 @@ static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
                                           TALLOC_CTX *mem_ctx,
                                           int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, group_pol;
+       struct policy_handle connect_pol, domain_pol, group_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        struct lsa_String grp_name;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -1661,8 +1727,9 @@ static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1708,7 +1775,7 @@ static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
                                           TALLOC_CTX *mem_ctx,
                                           int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, alias_pol;
+       struct policy_handle connect_pol, domain_pol, alias_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        struct lsa_String alias_name;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -1726,8 +1793,9 @@ static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1775,7 +1843,7 @@ static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
                                       int argc, const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        uint32 num_names;
        struct samr_Ids rids, name_types;
        int i;
@@ -1790,26 +1858,18 @@ static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
 
        /* Get sam policy and domain handles */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               &domain_sid,
-                                               &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else
-               return NT_STATUS_OK;
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  MAXIMUM_ALLOWED_ACCESS,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1858,8 +1918,8 @@ static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
                                      int argc, const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       POLICY_HND connect_pol, domain_pol;
-       uint32_t num_rids, num_names, *rids;
+       struct policy_handle connect_pol, domain_pol;
+       uint32_t num_rids, *rids;
        struct lsa_Strings names;
        struct samr_Ids types;
 
@@ -1872,26 +1932,18 @@ static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
 
        /* Get sam policy and domain handles */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
 
-       if (StrCaseCmp(argv[1], "domain")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               &domain_sid,
-                                               &domain_pol);
-       else if (StrCaseCmp(argv[1], "builtin")==0)
-               result = rpccli_samr_OpenDomain(cli, mem_ctx,
-                                               &connect_pol,
-                                               MAXIMUM_ALLOWED_ACCESS,
-                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
-                                               &domain_pol);
-       else
-               return NT_STATUS_OK;
+       result = get_domain_handle(cli, mem_ctx, argv[1],
+                                  &connect_pol,
+                                  MAXIMUM_ALLOWED_ACCESS,
+                                  &domain_sid,
+                                  &domain_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -1923,7 +1975,7 @@ static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
 
        /* Display results */
 
-       for (i = 0; i < num_names; i++) {
+       for (i = 0; i < num_rids; i++) {
                printf("rid 0x%x: %s (%d)\n",
                        rids[i], names.names[i].string, types.ids[i]);
        }
@@ -1941,7 +1993,7 @@ static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
                                          int argc, const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       POLICY_HND connect_pol, domain_pol, group_pol;
+       struct policy_handle connect_pol, domain_pol, group_pol;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 
        if ((argc < 2) || (argc > 3)) {
@@ -1954,8 +2006,9 @@ static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
 
        /* Get sam policy and domain handles */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2021,7 +2074,7 @@ static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
                                          int argc, const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       POLICY_HND connect_pol, domain_pol, user_pol;
+       struct policy_handle connect_pol, domain_pol, user_pol;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 
        if ((argc < 2) || (argc > 3)) {
@@ -2034,8 +2087,9 @@ static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
 
        /* Get sam policy and domain handles */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2102,10 +2156,9 @@ static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
                                     TALLOC_CTX *mem_ctx,
                                     int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol, user_pol, *pol;
+       struct policy_handle connect_pol, domain_pol, user_pol, *pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 sec_info = DACL_SECURITY_INFORMATION;
-       fstring server;
        uint32 user_rid = 0;
        TALLOC_CTX *ctx = NULL;
        SEC_DESC_BUF *sec_desc_buf=NULL;
@@ -2131,10 +2184,9 @@ static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
                sec_info = atoi(argv[2]);
        }
 
-       slprintf(server, sizeof(fstring)-1, "\\\\%s", cli->cli->desthost);
-       strupper_m(server);
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2194,7 +2246,7 @@ static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
                                           int argc, const char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       POLICY_HND connect_pol, domain_pol, user_pol;
+       struct policy_handle connect_pol, domain_pol, user_pol;
        struct samr_PwInfo info;
        uint32_t rid;
 
@@ -2205,8 +2257,9 @@ static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
 
        sscanf(argv[1], "%i", &rid);
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
@@ -2279,7 +2332,7 @@ static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
                                       TALLOC_CTX *mem_ctx,
                                       int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        fstring sid_string;
@@ -2293,7 +2346,9 @@ static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
 
        init_lsa_String(&domain_name, argv[1]);
 
-       result = try_samr_connects(cli, mem_ctx, access_mask, &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         access_mask,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2324,13 +2379,105 @@ done:
        return result;
 }
 
+/* Change user password */
+
+static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
+                                  TALLOC_CTX *mem_ctx,
+                                  int argc, const char **argv)
+{
+       struct policy_handle connect_pol, domain_pol, user_pol;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       const char *user, *oldpass, *newpass;
+       uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
+       struct samr_Ids rids, types;
+       struct lsa_String lsa_acct_name;
+
+       if (argc < 3) {
+               printf("Usage: %s username oldpass newpass\n", argv[0]);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       user = argv[1];
+       oldpass = argv[2];
+       newpass = argv[3];
+
+       /* Get sam policy handle */
+
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       /* Get domain policy handle */
+
+       result = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_pol,
+                                       access_mask,
+                                       &domain_sid,
+                                       &domain_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       init_lsa_String(&lsa_acct_name, user);
+
+       result = rpccli_samr_LookupNames(cli, mem_ctx,
+                                        &domain_pol,
+                                        1,
+                                        &lsa_acct_name,
+                                        &rids,
+                                        &types);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       result = rpccli_samr_OpenUser(cli, mem_ctx,
+                                     &domain_pol,
+                                     access_mask,
+                                     rids.ids[0],
+                                     &user_pol);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+       /* Change user password */
+       result = rpccli_samr_chgpasswd_user(cli, mem_ctx,
+                                           &user_pol,
+                                           newpass,
+                                           oldpass);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+ done:
+       if (is_valid_policy_hnd(&user_pol)) {
+               rpccli_samr_Close(cli, mem_ctx, &user_pol);
+       }
+       if (is_valid_policy_hnd(&domain_pol)) {
+               rpccli_samr_Close(cli, mem_ctx, &domain_pol);
+       }
+       if (is_valid_policy_hnd(&connect_pol)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_pol);
+       }
+
+       return result;
+}
+
+
 /* Change user password */
 
 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
                                    TALLOC_CTX *mem_ctx,
                                    int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        const char *user, *oldpass, *newpass;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
@@ -2346,8 +2493,9 @@ static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2364,7 +2512,7 @@ static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
                goto done;
 
        /* Change user password */
-       result = rpccli_samr_chgpasswd_user(cli, mem_ctx, user, newpass, oldpass);
+       result = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2386,12 +2534,12 @@ static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
                                    TALLOC_CTX *mem_ctx,
                                    int argc, const char **argv)
 {
-       POLICY_HND connect_pol, domain_pol;
+       struct policy_handle connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        const char *user, *oldpass, *newpass;
        uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
        struct samr_DomInfo1 *info = NULL;
-       struct samr_ChangeReject *reject = NULL;
+       struct userPwdChangeFailureInformation *reject = NULL;
 
        if (argc < 3) {
                printf("Usage: %s username oldpass newpass\n", argv[0]);
@@ -2404,8 +2552,9 @@ static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
 
        /* Get sam policy handle */
 
-       result = try_samr_connects(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
-                                  &connect_pol);
+       result = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
 
        if (!NT_STATUS_IS_OK(result))
                goto done;
@@ -2422,33 +2571,30 @@ static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
                goto done;
 
        /* Change user password */
-       result = rpccli_samr_chgpasswd3(cli, mem_ctx,
-                                       user,
-                                       newpass,
-                                       oldpass,
-                                       &info,
-                                       &reject);
+       result = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
+                                            user,
+                                            newpass,
+                                            oldpass,
+                                            &info,
+                                            &reject);
 
        if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
 
                display_sam_dom_info_1(info);
 
-               switch (reject->reason) {
-                       case SAMR_REJECT_TOO_SHORT:
-                               d_printf("SAMR_REJECT_TOO_SHORT\n");
+               switch (reject->extendedFailureReason) {
+                       case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
+                               d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
                                break;
-                       case SAMR_REJECT_IN_HISTORY:
-                               d_printf("SAMR_REJECT_IN_HISTORY\n");
+                       case SAM_PWD_CHANGE_PWD_IN_HISTORY:
+                               d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
                                break;
-                       case SAMR_REJECT_COMPLEXITY:
-                               d_printf("SAMR_REJECT_COMPLEXITY\n");
-                               break;
-                       case SAMR_REJECT_OTHER:
-                               d_printf("SAMR_REJECT_OTHER\n");
+                       case SAM_PWD_CHANGE_NOT_COMPLEX:
+                               d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
                                break;
                        default:
                                d_printf("unknown reject reason: %d\n",
-                                       reject->reason);
+                                       reject->extendedFailureReason);
                                break;
                }
        }
@@ -2466,39 +2612,345 @@ static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
        return result;
 }
 
+static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, const char **argv,
+                                        int opcode)
+{
+       struct policy_handle connect_pol, domain_pol, user_pol;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       const char *user, *param;
+       uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
+       uint32_t level;
+       uint32_t user_rid;
+       union samr_UserInfo info;
+       struct samr_CryptPassword pwd_buf;
+       struct samr_CryptPasswordEx pwd_buf_ex;
+       uint8_t nt_hash[16];
+       uint8_t lm_hash[16];
+       DATA_BLOB session_key;
+       uint8_t password_expired = 0;
+
+       if (argc < 4) {
+               printf("Usage: %s username level password [password_expired]\n",
+                       argv[0]);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       user = argv[1];
+       level = atoi(argv[2]);
+       param = argv[3];
+
+       if (argc >= 5) {
+               password_expired = atoi(argv[4]);
+       }
+
+       status = cli_get_session_key(mem_ctx, cli, &session_key);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       init_samr_CryptPassword(param, &session_key, &pwd_buf);
+       init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
+       nt_lm_owf_gen(param, nt_hash, lm_hash);
+
+       switch (level) {
+       case 18:
+               {
+                       DATA_BLOB in,out;
+                       in = data_blob_const(nt_hash, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+                       sess_crypt_blob(&out, &in, &session_key, true);
+                       memcpy(nt_hash, out.data, out.length);
+               }
+               {
+                       DATA_BLOB in,out;
+                       in = data_blob_const(lm_hash, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+                       sess_crypt_blob(&out, &in, &session_key, true);
+                       memcpy(lm_hash, out.data, out.length);
+               }
+
+               memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
+               memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
+               info.info18.nt_pwd_active       = true;
+               info.info18.lm_pwd_active       = true;
+               info.info18.password_expired    = password_expired;
+
+               break;
+       case 21:
+               ZERO_STRUCT(info.info21);
+
+               info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
+                                            SAMR_FIELD_LM_PASSWORD_PRESENT;
+               if (argc >= 5) {
+                       info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
+                       info.info21.password_expired = password_expired;
+               }
+
+               info.info21.lm_password_set = true;
+               info.info21.lm_owf_password.length = 16;
+               info.info21.lm_owf_password.size = 16;
+
+               info.info21.nt_password_set = true;
+               info.info21.nt_owf_password.length = 16;
+               info.info21.nt_owf_password.size = 16;
+
+               {
+                       DATA_BLOB in,out;
+                       in = data_blob_const(nt_hash, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+                       sess_crypt_blob(&out, &in, &session_key, true);
+                       info.info21.nt_owf_password.array =
+                               (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
+               }
+               {
+                       DATA_BLOB in,out;
+                       in = data_blob_const(lm_hash, 16);
+                       out = data_blob_talloc_zero(mem_ctx, 16);
+                       sess_crypt_blob(&out, &in, &session_key, true);
+                       info.info21.lm_owf_password.array =
+                               (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
+               }
+
+               break;
+       case 23:
+               ZERO_STRUCT(info.info23);
+
+               info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
+                                                 SAMR_FIELD_LM_PASSWORD_PRESENT;
+               if (argc >= 5) {
+                       info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
+                       info.info23.info.password_expired = password_expired;
+               }
+
+               info.info23.password = pwd_buf;
+
+               break;
+       case 24:
+               info.info24.password            = pwd_buf;
+               info.info24.password_expired    = password_expired;
+
+               break;
+       case 25:
+               ZERO_STRUCT(info.info25);
+
+               info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
+                                                 SAMR_FIELD_LM_PASSWORD_PRESENT;
+               if (argc >= 5) {
+                       info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
+                       info.info25.info.password_expired = password_expired;
+               }
+
+               info.info25.password = pwd_buf_ex;
+
+               break;
+       case 26:
+               info.info26.password            = pwd_buf_ex;
+               info.info26.password_expired    = password_expired;
+
+               break;
+       default:
+               return NT_STATUS_INVALID_INFO_CLASS;
+       }
+
+       /* Get sam policy handle */
+
+       status = rpccli_try_samr_connects(cli, mem_ctx,
+                                         MAXIMUM_ALLOWED_ACCESS,
+                                         &connect_pol);
+
+       if (!NT_STATUS_IS_OK(status))
+               goto done;
+
+       /* Get domain policy handle */
+
+       status = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_pol,
+                                       access_mask,
+                                       &domain_sid,
+                                       &domain_pol);
+
+       if (!NT_STATUS_IS_OK(status))
+               goto done;
+
+       user_rid = strtol(user, NULL, 0);
+       if (user_rid) {
+               status = rpccli_samr_OpenUser(cli, mem_ctx,
+                                             &domain_pol,
+                                             access_mask,
+                                             user_rid,
+                                             &user_pol);
+       }
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
+           (user_rid == 0)) {
+
+               /* Probably this was a user name, try lookupnames */
+               struct samr_Ids rids, types;
+               struct lsa_String lsa_acct_name;
+
+               init_lsa_String(&lsa_acct_name, user);
+
+               status = rpccli_samr_LookupNames(cli, mem_ctx,
+                                                &domain_pol,
+                                                1,
+                                                &lsa_acct_name,
+                                                &rids,
+                                                &types);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = rpccli_samr_OpenUser(cli, mem_ctx,
+                                             &domain_pol,
+                                             access_mask,
+                                             rids.ids[0],
+                                             &user_pol);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       switch (opcode) {
+       case NDR_SAMR_SETUSERINFO:
+               status = rpccli_samr_SetUserInfo(cli, mem_ctx,
+                                                &user_pol,
+                                                level,
+                                                &info);
+               break;
+       case NDR_SAMR_SETUSERINFO2:
+               status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
+                                                 &user_pol,
+                                                 level,
+                                                 &info);
+               break;
+       default:
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+ done:
+       return status;
+}
+
+static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
+                                    TALLOC_CTX *mem_ctx,
+                                    int argc, const char **argv)
+{
+       return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
+                                       NDR_SAMR_SETUSERINFO);
+}
+
+static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
+                                     TALLOC_CTX *mem_ctx,
+                                     int argc, const char **argv)
+{
+       return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
+                                       NDR_SAMR_SETUSERINFO2);
+}
+
+static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, const char **argv)
+{
+       NTSTATUS status;
+       struct policy_handle connect_handle;
+       struct policy_handle domain_handle;
+       uint16_t level = 1;
+       struct lsa_String name;
+       uint32_t idx = 0;
+
+       if (argc < 2 || argc > 3) {
+               printf("Usage: %s name level\n", argv[0]);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       init_lsa_String(&name, argv[1]);
+
+       if (argc == 3) {
+               level = atoi(argv[2]);
+       }
+
+       status = rpccli_try_samr_connects(cli, mem_ctx,
+                                         SEC_FLAG_MAXIMUM_ALLOWED,
+                                         &connect_handle);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+
+       status = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_handle,
+                                       SEC_FLAG_MAXIMUM_ALLOWED,
+                                       &domain_sid,
+                                       &domain_handle);
+
+       if (!NT_STATUS_IS_OK(status))
+               goto done;
+
+
+       status = rpccli_samr_GetDisplayEnumerationIndex(cli, mem_ctx,
+                                                       &domain_handle,
+                                                       level,
+                                                       &name,
+                                                       &idx);
+
+       if (NT_STATUS_IS_OK(status) ||
+           NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+               printf("idx: %d (0x%08x)\n", idx, idx);
+       }
+ done:
+
+       if (is_valid_policy_hnd(&domain_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &domain_handle);
+       }
+       if (is_valid_policy_hnd(&connect_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_handle);
+       }
+
+       return status;
+
+}
 /* List of commands exported by this module */
 
 struct cmd_set samr_commands[] = {
 
        { "SAMR" },
 
-       { "queryuser",  RPC_RTYPE_NTSTATUS, cmd_samr_query_user,                NULL, PI_SAMR, NULL,    "Query user info",         "" },
-       { "querygroup",         RPC_RTYPE_NTSTATUS, cmd_samr_query_group,               NULL, PI_SAMR, NULL,    "Query group info",        "" },
-       { "queryusergroups",    RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups,  NULL, PI_SAMR, NULL,    "Query user groups",       "" },
-       { "queryuseraliases",   RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases,         NULL, PI_SAMR, NULL,    "Query user aliases",      "" },
-       { "querygroupmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem,    NULL, PI_SAMR, NULL,    "Query group membership",  "" },
-       { "queryaliasmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem,    NULL, PI_SAMR, NULL,    "Query alias membership",  "" },
-       { "queryaliasinfo",     RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo,   NULL, PI_SAMR, NULL,    "Query alias info",       "" },
-       { "deletealias",        RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias,      NULL, PI_SAMR, NULL,    "Delete an alias",  "" },
-       { "querydispinfo",      RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo,    NULL, PI_SAMR, NULL,    "Query display info",      "" },
-       { "querydominfo",       RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo,     NULL, PI_SAMR, NULL,    "Query domain info",       "" },
-       { "enumdomusers",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users,       NULL, PI_SAMR, NULL,  "Enumerate domain users", "" },
-       { "enumdomgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups,       NULL, PI_SAMR, NULL,        "Enumerate domain groups", "" },
-       { "enumalsgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups,       NULL, PI_SAMR, NULL,        "Enumerate alias groups",  "" },
-
-       { "createdomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user,       NULL, PI_SAMR, NULL,        "Create domain user",      "" },
-       { "createdomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group,      NULL, PI_SAMR, NULL,        "Create domain group",     "" },
-       { "createdomalias",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias,      NULL, PI_SAMR, NULL,        "Create domain alias",     "" },
-       { "samlookupnames",     RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names,          NULL, PI_SAMR, NULL,        "Look up names",           "" },
-       { "samlookuprids",      RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids,           NULL, PI_SAMR, NULL,        "Look up names",           "" },
-       { "deletedomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group,      NULL, PI_SAMR, NULL,        "Delete domain group",     "" },
-       { "deletedomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user,       NULL, PI_SAMR, NULL,        "Delete domain user",      "" },
-       { "samquerysecobj",     RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj,         NULL, PI_SAMR, NULL, "Query SAMR security object",   "" },
-       { "getdompwinfo",       RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo,        NULL, PI_SAMR, NULL, "Retrieve domain password info", "" },
-       { "getusrdompwinfo",    RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo,     NULL, PI_SAMR, NULL, "Retrieve user domain password info", "" },
-
-       { "lookupdomain",       RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain,         NULL, PI_SAMR, NULL, "Lookup Domain Name", "" },
-       { "chgpasswd2",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2,            NULL, PI_SAMR, NULL, "Change user password", "" },
-       { "chgpasswd3",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3,            NULL, PI_SAMR, NULL, "Change user password", "" },
+       { "queryuser",  RPC_RTYPE_NTSTATUS, cmd_samr_query_user,                NULL, &ndr_table_samr.syntax_id, NULL,  "Query user info",         "" },
+       { "querygroup",         RPC_RTYPE_NTSTATUS, cmd_samr_query_group,               NULL, &ndr_table_samr.syntax_id, NULL,  "Query group info",        "" },
+       { "queryusergroups",    RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups,  NULL, &ndr_table_samr.syntax_id, NULL,  "Query user groups",       "" },
+       { "queryuseraliases",   RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases,         NULL, &ndr_table_samr.syntax_id, NULL,  "Query user aliases",      "" },
+       { "querygroupmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query group membership",  "" },
+       { "queryaliasmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias membership",  "" },
+       { "queryaliasinfo",     RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias info",       "" },
+       { "deletealias",        RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias,      NULL, &ndr_table_samr.syntax_id, NULL,  "Delete an alias",  "" },
+       { "querydispinfo",      RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
+       { "querydispinfo2",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
+       { "querydispinfo3",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
+       { "querydominfo",       RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo,     NULL, &ndr_table_samr.syntax_id, NULL,  "Query domain info",       "" },
+       { "enumdomusers",       RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users,       NULL, &ndr_table_samr.syntax_id, NULL,       "Enumerate domain users", "" },
+       { "enumdomgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domain groups", "" },
+       { "enumalsgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate alias groups",  "" },
+       { "enumdomains",        RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains,          NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domains",  "" },
+
+       { "createdomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain user",      "" },
+       { "createdomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain group",     "" },
+       { "createdomalias",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain alias",     "" },
+       { "samlookupnames",     RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names,          NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
+       { "samlookuprids",      RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids,           NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
+       { "deletedomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain group",     "" },
+       { "deletedomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain user",      "" },
+       { "samquerysecobj",     RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj,         NULL, &ndr_table_samr.syntax_id, NULL, "Query SAMR security object",   "" },
+       { "getdompwinfo",       RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo,        NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve domain password info", "" },
+       { "getusrdompwinfo",    RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo,     NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve user domain password info", "" },
+
+       { "lookupdomain",       RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain,         NULL, &ndr_table_samr.syntax_id, NULL, "Lookup Domain Name", "" },
+       { "chgpasswd",          RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd,             NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
+       { "chgpasswd2",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
+       { "chgpasswd3",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
+       { "getdispinfoidx",     RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx,      NULL, &ndr_table_samr.syntax_id, NULL, "Get Display Information Index", "" },
+       { "setuserinfo",        RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo,           NULL, &ndr_table_samr.syntax_id, NULL, "Set user info", "" },
+       { "setuserinfo2",       RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2,          NULL, &ndr_table_samr.syntax_id, NULL, "Set user info2", "" },
        { NULL }
 };