root/source3/rpcclient/cmd_samr.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. display_samr_user_info_7
  2. display_samr_user_info_9
  3. display_samr_user_info_16
  4. display_samr_user_info_20
  5. display_samr_user_info_21
  6. display_password_properties
  7. display_sam_dom_info_1
  8. display_sam_dom_info_2
  9. display_sam_dom_info_3
  10. display_sam_dom_info_4
  11. display_sam_dom_info_5
  12. display_sam_dom_info_6
  13. display_sam_dom_info_7
  14. display_sam_dom_info_8
  15. display_sam_dom_info_9
  16. display_sam_dom_info_12
  17. display_sam_dom_info_13
  18. display_sam_info_1
  19. display_sam_info_2
  20. display_sam_info_3
  21. display_sam_info_4
  22. display_sam_info_5
  23. get_domain_handle
  24. cmd_samr_query_user
  25. display_group_info1
  26. display_group_info2
  27. display_group_info3
  28. display_group_info4
  29. display_group_info5
  30. display_group_info
  31. cmd_samr_query_group
  32. cmd_samr_query_usergroups
  33. cmd_samr_query_useraliases
  34. cmd_samr_query_groupmem
  35. cmd_samr_enum_dom_users
  36. cmd_samr_enum_dom_groups
  37. cmd_samr_enum_als_groups
  38. cmd_samr_enum_domains
  39. cmd_samr_query_aliasmem
  40. cmd_samr_query_aliasinfo
  41. cmd_samr_delete_alias
  42. cmd_samr_query_dispinfo_internal
  43. cmd_samr_query_dispinfo
  44. cmd_samr_query_dispinfo2
  45. cmd_samr_query_dispinfo3
  46. cmd_samr_query_dominfo
  47. cmd_samr_create_dom_user
  48. cmd_samr_create_dom_group
  49. cmd_samr_create_dom_alias
  50. cmd_samr_lookup_names
  51. cmd_samr_lookup_rids
  52. cmd_samr_delete_dom_group
  53. cmd_samr_delete_dom_user
  54. cmd_samr_query_sec_obj
  55. cmd_samr_get_usrdom_pwinfo
  56. cmd_samr_get_dom_pwinfo
  57. cmd_samr_lookup_domain
  58. cmd_samr_chgpasswd
  59. cmd_samr_chgpasswd2
  60. cmd_samr_chgpasswd3
  61. cmd_samr_setuserinfo_int
  62. cmd_samr_setuserinfo
  63. cmd_samr_setuserinfo2
  64. cmd_samr_get_dispinfo_idx

   1 /*
   2    Unix SMB/CIFS implementation.
   3    RPC pipe client
   4 
   5    Copyright (C) Andrew Tridgell              1992-2000,
   6    Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
   7    Copyright (C) Elrond                            2000,
   8    Copyright (C) Tim Potter                        2000
   9    Copyright (C) Guenther Deschner                 2008
  10 
  11    This program is free software; you can redistribute it and/or modify
  12    it under the terms of the GNU General Public License as published by
  13    the Free Software Foundation; either version 3 of the License, or
  14    (at your option) any later version.
  15 
  16    This program is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20 
  21    You should have received a copy of the GNU General Public License
  22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23 */
  24 
  25 #include "includes.h"
  26 #include "rpcclient.h"
  27 
  28 extern DOM_SID domain_sid;
  29 
  30 /****************************************************************************
  31  display samr_user_info_7 structure
  32  ****************************************************************************/
  33 static void display_samr_user_info_7(struct samr_UserInfo7 *r)
     /* [<][>][^][v][top][bottom][index][help] */
  34 {
  35         printf("\tUser Name   :\t%s\n", r->account_name.string);
  36 }
  37 
  38 /****************************************************************************
  39  display samr_user_info_9 structure
  40  ****************************************************************************/
  41 static void display_samr_user_info_9(struct samr_UserInfo9 *r)
     /* [<][>][^][v][top][bottom][index][help] */
  42 {
  43         printf("\tPrimary group RID   :\tox%x\n", r->primary_gid);
  44 }
  45 
  46 /****************************************************************************
  47  display samr_user_info_16 structure
  48  ****************************************************************************/
  49 static void display_samr_user_info_16(struct samr_UserInfo16 *r)
     /* [<][>][^][v][top][bottom][index][help] */
  50 {
  51         printf("\tAcct Flags   :\tox%x\n", r->acct_flags);
  52 }
  53 
  54 /****************************************************************************
  55  display samr_user_info_20 structure
  56  ****************************************************************************/
  57 static void display_samr_user_info_20(struct samr_UserInfo20 *r)
     /* [<][>][^][v][top][bottom][index][help] */
  58 {
  59         printf("\tRemote Dial :\n");
  60         dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
  61 }
  62 
  63 
  64 /****************************************************************************
  65  display samr_user_info_21 structure
  66  ****************************************************************************/
  67 static void display_samr_user_info_21(struct samr_UserInfo21 *r)
     /* [<][>][^][v][top][bottom][index][help] */
  68 {
  69         printf("\tUser Name   :\t%s\n", r->account_name.string);
  70         printf("\tFull Name   :\t%s\n", r->full_name.string);
  71         printf("\tHome Drive  :\t%s\n", r->home_directory.string);
  72         printf("\tDir Drive   :\t%s\n", r->home_drive.string);
  73         printf("\tProfile Path:\t%s\n", r->profile_path.string);
  74         printf("\tLogon Script:\t%s\n", r->logon_script.string);
  75         printf("\tDescription :\t%s\n", r->description.string);
  76         printf("\tWorkstations:\t%s\n", r->workstations.string);
  77         printf("\tComment     :\t%s\n", r->comment.string);
  78         printf("\tRemote Dial :\n");
  79         dump_data(0, (uint8_t *)r->parameters.array, r->parameters.length*2);
  80 
  81         printf("\tLogon Time               :\t%s\n",
  82                http_timestring(talloc_tos(), nt_time_to_unix(r->last_logon)));
  83         printf("\tLogoff Time              :\t%s\n",
  84                http_timestring(talloc_tos(), nt_time_to_unix(r->last_logoff)));
  85         printf("\tKickoff Time             :\t%s\n",
  86                http_timestring(talloc_tos(), nt_time_to_unix(r->acct_expiry)));
  87         printf("\tPassword last set Time   :\t%s\n",
  88                http_timestring(talloc_tos(), nt_time_to_unix(r->last_password_change)));
  89         printf("\tPassword can change Time :\t%s\n",
  90                http_timestring(talloc_tos(), nt_time_to_unix(r->allow_password_change)));
  91         printf("\tPassword must change Time:\t%s\n",
  92                http_timestring(talloc_tos(), nt_time_to_unix(r->force_password_change)));
  93 
  94         printf("\tunknown_2[0..31]...\n"); /* user passwords? */
  95 
  96         printf("\tuser_rid :\t0x%x\n"  , r->rid); /* User ID */
  97         printf("\tgroup_rid:\t0x%x\n"  , r->primary_gid); /* Group ID */
  98         printf("\tacb_info :\t0x%08x\n", r->acct_flags); /* Account Control Info */
  99 
 100         printf("\tfields_present:\t0x%08x\n", r->fields_present); /* 0x00ff ffff */
 101         printf("\tlogon_divs:\t%d\n", r->logon_hours.units_per_week); /* 0x0000 00a8 which is 168 which is num hrs in a week */
 102         printf("\tbad_password_count:\t0x%08x\n", r->bad_password_count);
 103         printf("\tlogon_count:\t0x%08x\n", r->logon_count);
 104 
 105         printf("\tpadding1[0..7]...\n");
 106 
 107         if (r->logon_hours.bits) {
 108                 printf("\tlogon_hrs[0..%d]...\n", r->logon_hours.units_per_week/8);
 109         }
 110 }
 111 
 112 
 113 static void display_password_properties(uint32_t password_properties)
     /* [<][>][^][v][top][bottom][index][help] */
 114 {
 115         printf("password_properties: 0x%08x\n", password_properties);
 116 
 117         if (password_properties & DOMAIN_PASSWORD_COMPLEX)
 118                 printf("\tDOMAIN_PASSWORD_COMPLEX\n");
 119 
 120         if (password_properties & DOMAIN_PASSWORD_NO_ANON_CHANGE)
 121                 printf("\tDOMAIN_PASSWORD_NO_ANON_CHANGE\n");
 122 
 123         if (password_properties & DOMAIN_PASSWORD_NO_CLEAR_CHANGE)
 124                 printf("\tDOMAIN_PASSWORD_NO_CLEAR_CHANGE\n");
 125 
 126         if (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)
 127                 printf("\tDOMAIN_PASSWORD_LOCKOUT_ADMINS\n");
 128 
 129         if (password_properties & DOMAIN_PASSWORD_STORE_CLEARTEXT)
 130                 printf("\tDOMAIN_PASSWORD_STORE_CLEARTEXT\n");
 131 
 132         if (password_properties & DOMAIN_REFUSE_PASSWORD_CHANGE)
 133                 printf("\tDOMAIN_REFUSE_PASSWORD_CHANGE\n");
 134 }
 135 
 136 static void display_sam_dom_info_1(struct samr_DomInfo1 *info1)
     /* [<][>][^][v][top][bottom][index][help] */
 137 {
 138         printf("Minimum password length:\t\t\t%d\n",
 139                 info1->min_password_length);
 140         printf("Password uniqueness (remember x passwords):\t%d\n",
 141                 info1->password_history_length);
 142         display_password_properties(info1->password_properties);
 143         printf("password expire in:\t\t\t\t%s\n",
 144                 display_time(info1->max_password_age));
 145         printf("Min password age (allow changing in x days):\t%s\n",
 146                 display_time(info1->min_password_age));
 147 }
 148 
 149 static void display_sam_dom_info_2(struct samr_DomGeneralInformation *general)
     /* [<][>][^][v][top][bottom][index][help] */
 150 {
 151         printf("Domain:\t\t%s\n", general->domain_name.string);
 152         printf("Server:\t\t%s\n", general->primary.string);
 153         printf("Comment:\t%s\n", general->oem_information.string);
 154 
 155         printf("Total Users:\t%d\n", general->num_users);
 156         printf("Total Groups:\t%d\n", general->num_groups);
 157         printf("Total Aliases:\t%d\n", general->num_aliases);
 158 
 159         printf("Sequence No:\t%llu\n", (unsigned long long)general->sequence_num);
 160 
 161         printf("Force Logoff:\t%d\n",
 162                 (int)nt_time_to_unix_abs(&general->force_logoff_time));
 163 
 164         printf("Domain Server State:\t0x%x\n", general->domain_server_state);
 165         printf("Server Role:\t%s\n", server_role_str(general->role));
 166         printf("Unknown 3:\t0x%x\n", general->unknown3);
 167 }
 168 
 169 static void display_sam_dom_info_3(struct samr_DomInfo3 *info3)
     /* [<][>][^][v][top][bottom][index][help] */
 170 {
 171         printf("Force Logoff:\t%d\n",
 172                 (int)nt_time_to_unix_abs(&info3->force_logoff_time));
 173 }
 174 
 175 static void display_sam_dom_info_4(struct samr_DomOEMInformation *oem)
     /* [<][>][^][v][top][bottom][index][help] */
 176 {
 177         printf("Comment:\t%s\n", oem->oem_information.string);
 178 }
 179 
 180 static void display_sam_dom_info_5(struct samr_DomInfo5 *info5)
     /* [<][>][^][v][top][bottom][index][help] */
 181 {
 182         printf("Domain:\t\t%s\n", info5->domain_name.string);
 183 }
 184 
 185 static void display_sam_dom_info_6(struct samr_DomInfo6 *info6)
     /* [<][>][^][v][top][bottom][index][help] */
 186 {
 187         printf("Server:\t\t%s\n", info6->primary.string);
 188 }
 189 
 190 static void display_sam_dom_info_7(struct samr_DomInfo7 *info7)
     /* [<][>][^][v][top][bottom][index][help] */
 191 {
 192         printf("Server Role:\t%s\n", server_role_str(info7->role));
 193 }
 194 
 195 static void display_sam_dom_info_8(struct samr_DomInfo8 *info8)
     /* [<][>][^][v][top][bottom][index][help] */
 196 {
 197         printf("Sequence No:\t%llu\n", (unsigned long long)info8->sequence_num);
 198         printf("Domain Create Time:\t%s\n",
 199                 http_timestring(talloc_tos(), nt_time_to_unix(info8->domain_create_time)));
 200 }
 201 
 202 static void display_sam_dom_info_9(struct samr_DomInfo9 *info9)
     /* [<][>][^][v][top][bottom][index][help] */
 203 {
 204         printf("Domain Server State:\t0x%x\n", info9->domain_server_state);
 205 }
 206 
 207 static void display_sam_dom_info_12(struct samr_DomInfo12 *info12)
     /* [<][>][^][v][top][bottom][index][help] */
 208 {
 209         printf("Bad password lockout duration:               %s\n",
 210                 display_time(info12->lockout_duration));
 211         printf("Reset Lockout after:                         %s\n",
 212                 display_time(info12->lockout_window));
 213         printf("Lockout after bad attempts:                  %d\n",
 214                 info12->lockout_threshold);
 215 }
 216 
 217 static void display_sam_dom_info_13(struct samr_DomInfo13 *info13)
     /* [<][>][^][v][top][bottom][index][help] */
 218 {
 219         printf("Sequence No:\t%llu\n", (unsigned long long)info13->sequence_num);
 220         printf("Domain Create Time:\t%s\n",
 221                 http_timestring(talloc_tos(), nt_time_to_unix(info13->domain_create_time)));
 222         printf("Sequence No at last promotion:\t%llu\n",
 223                 (unsigned long long)info13->modified_count_at_last_promotion);
 224 }
 225 
 226 static void display_sam_info_1(struct samr_DispEntryGeneral *r)
     /* [<][>][^][v][top][bottom][index][help] */
 227 {
 228         printf("index: 0x%x ", r->idx);
 229         printf("RID: 0x%x ", r->rid);
 230         printf("acb: 0x%08x ", r->acct_flags);
 231         printf("Account: %s\t", r->account_name.string);
 232         printf("Name: %s\t", r->full_name.string);
 233         printf("Desc: %s\n", r->description.string);
 234 }
 235 
 236 static void display_sam_info_2(struct samr_DispEntryFull *r)
     /* [<][>][^][v][top][bottom][index][help] */
 237 {
 238         printf("index: 0x%x ", r->idx);
 239         printf("RID: 0x%x ", r->rid);
 240         printf("acb: 0x%08x ", r->acct_flags);
 241         printf("Account: %s\t", r->account_name.string);
 242         printf("Desc: %s\n", r->description.string);
 243 }
 244 
 245 static void display_sam_info_3(struct samr_DispEntryFullGroup *r)
     /* [<][>][^][v][top][bottom][index][help] */
 246 {
 247         printf("index: 0x%x ", r->idx);
 248         printf("RID: 0x%x ", r->rid);
 249         printf("acb: 0x%08x ", r->acct_flags);
 250         printf("Account: %s\t", r->account_name.string);
 251         printf("Desc: %s\n", r->description.string);
 252 }
 253 
 254 static void display_sam_info_4(struct samr_DispEntryAscii *r)
     /* [<][>][^][v][top][bottom][index][help] */
 255 {
 256         printf("index: 0x%x ", r->idx);
 257         printf("Account: %s\n", r->account_name.string);
 258 }
 259 
 260 static void display_sam_info_5(struct samr_DispEntryAscii *r)
     /* [<][>][^][v][top][bottom][index][help] */
 261 {
 262         printf("index: 0x%x ", r->idx);
 263         printf("Account: %s\n", r->account_name.string);
 264 }
 265 
 266 /****************************************************************************
 267  ****************************************************************************/
 268 
 269 static NTSTATUS get_domain_handle(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 270                                   TALLOC_CTX *mem_ctx,
 271                                   const char *sam,
 272                                   struct policy_handle *connect_pol,
 273                                   uint32_t access_mask,
 274                                   struct dom_sid *_domain_sid,
 275                                   struct policy_handle *domain_pol)
 276 {
 277 
 278         if (StrCaseCmp(sam, "domain") == 0) {
 279                 return rpccli_samr_OpenDomain(cli, mem_ctx,
 280                                               connect_pol,
 281                                               access_mask,
 282                                               _domain_sid,
 283                                               domain_pol);
 284         } else if (StrCaseCmp(sam, "builtin") == 0) {
 285                 return rpccli_samr_OpenDomain(cli, mem_ctx,
 286                                               connect_pol,
 287                                               access_mask,
 288                                               CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
 289                                               domain_pol);
 290         }
 291 
 292         return NT_STATUS_INVALID_PARAMETER;
 293 }
 294 
 295 /**********************************************************************
 296  * Query user information
 297  */
 298 static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 299                                     TALLOC_CTX *mem_ctx,
 300                                     int argc, const char **argv)
 301 {
 302         struct policy_handle connect_pol, domain_pol, user_pol;
 303         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 304         uint32 info_level = 21;
 305         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 306         union samr_UserInfo *info = NULL;
 307         uint32 user_rid = 0;
 308 
 309         if ((argc < 2) || (argc > 4)) {
 310                 printf("Usage: %s rid [info level] [access mask] \n", argv[0]);
 311                 return NT_STATUS_OK;
 312         }
 313 
 314         sscanf(argv[1], "%i", &user_rid);
 315 
 316         if (argc > 2)
 317                 sscanf(argv[2], "%i", &info_level);
 318 
 319         if (argc > 3)
 320                 sscanf(argv[3], "%x", &access_mask);
 321 
 322 
 323         result = rpccli_try_samr_connects(cli, mem_ctx,
 324                                           MAXIMUM_ALLOWED_ACCESS,
 325                                           &connect_pol);
 326 
 327         if (!NT_STATUS_IS_OK(result))
 328                 goto done;
 329 
 330         result = rpccli_samr_OpenDomain(cli, mem_ctx,
 331                                         &connect_pol,
 332                                         MAXIMUM_ALLOWED_ACCESS,
 333                                         &domain_sid,
 334                                         &domain_pol);
 335         if (!NT_STATUS_IS_OK(result))
 336                 goto done;
 337 
 338         result = rpccli_samr_OpenUser(cli, mem_ctx,
 339                                       &domain_pol,
 340                                       access_mask,
 341                                       user_rid,
 342                                       &user_pol);
 343 
 344         if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) &&
 345             (user_rid == 0)) {
 346 
 347                 /* Probably this was a user name, try lookupnames */
 348                 struct samr_Ids rids, types;
 349                 struct lsa_String lsa_acct_name;
 350 
 351                 init_lsa_String(&lsa_acct_name, argv[1]);
 352 
 353                 result = rpccli_samr_LookupNames(cli, mem_ctx,
 354                                                  &domain_pol,
 355                                                  1,
 356                                                  &lsa_acct_name,
 357                                                  &rids,
 358                                                  &types);
 359 
 360                 if (NT_STATUS_IS_OK(result)) {
 361                         result = rpccli_samr_OpenUser(cli, mem_ctx,
 362                                                       &domain_pol,
 363                                                       access_mask,
 364                                                       rids.ids[0],
 365                                                       &user_pol);
 366                 }
 367         }
 368 
 369 
 370         if (!NT_STATUS_IS_OK(result))
 371                 goto done;
 372 
 373         result = rpccli_samr_QueryUserInfo(cli, mem_ctx,
 374                                            &user_pol,
 375                                            info_level,
 376                                            &info);
 377 
 378         if (!NT_STATUS_IS_OK(result))
 379                 goto done;
 380 
 381         switch (info_level) {
 382         case 7:
 383                 display_samr_user_info_7(&info->info7);
 384                 break;
 385         case 9:
 386                 display_samr_user_info_9(&info->info9);
 387                 break;
 388         case 16:
 389                 display_samr_user_info_16(&info->info16);
 390                 break;
 391         case 20:
 392                 display_samr_user_info_20(&info->info20);
 393                 break;
 394         case 21:
 395                 display_samr_user_info_21(&info->info21);
 396                 break;
 397         default:
 398                 printf("Unsupported infolevel: %d\n", info_level);
 399                 break;
 400         }
 401 
 402         rpccli_samr_Close(cli, mem_ctx, &user_pol);
 403         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 404         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 405 
 406 done:
 407         return result;
 408 }
 409 
 410 /****************************************************************************
 411  display group info
 412  ****************************************************************************/
 413 static void display_group_info1(struct samr_GroupInfoAll *info1)
     /* [<][>][^][v][top][bottom][index][help] */
 414 {
 415         printf("\tGroup Name:\t%s\n", info1->name.string);
 416         printf("\tDescription:\t%s\n", info1->description.string);
 417         printf("\tGroup Attribute:%d\n", info1->attributes);
 418         printf("\tNum Members:%d\n", info1->num_members);
 419 }
 420 
 421 /****************************************************************************
 422  display group info
 423  ****************************************************************************/
 424 static void display_group_info2(struct lsa_String *info2)
     /* [<][>][^][v][top][bottom][index][help] */
 425 {
 426         printf("\tGroup Description:%s\n", info2->string);
 427 }
 428 
 429 
 430 /****************************************************************************
 431  display group info
 432  ****************************************************************************/
 433 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
     /* [<][>][^][v][top][bottom][index][help] */
 434 {
 435         printf("\tGroup Attribute:%d\n", info3->attributes);
 436 }
 437 
 438 
 439 /****************************************************************************
 440  display group info
 441  ****************************************************************************/
 442 static void display_group_info4(struct lsa_String *info4)
     /* [<][>][^][v][top][bottom][index][help] */
 443 {
 444         printf("\tGroup Description:%s\n", info4->string);
 445 }
 446 
 447 /****************************************************************************
 448  display group info
 449  ****************************************************************************/
 450 static void display_group_info5(struct samr_GroupInfoAll *info5)
     /* [<][>][^][v][top][bottom][index][help] */
 451 {
 452         printf("\tGroup Name:\t%s\n", info5->name.string);
 453         printf("\tDescription:\t%s\n", info5->description.string);
 454         printf("\tGroup Attribute:%d\n", info5->attributes);
 455         printf("\tNum Members:%d\n", info5->num_members);
 456 }
 457 
 458 /****************************************************************************
 459  display sam sync structure
 460  ****************************************************************************/
 461 static void display_group_info(union samr_GroupInfo *info,
     /* [<][>][^][v][top][bottom][index][help] */
 462                                enum samr_GroupInfoEnum level)
 463 {
 464         switch (level) {
 465                 case 1:
 466                         display_group_info1(&info->all);
 467                         break;
 468                 case 2:
 469                         display_group_info2(&info->name);
 470                         break;
 471                 case 3:
 472                         display_group_info3(&info->attributes);
 473                         break;
 474                 case 4:
 475                         display_group_info4(&info->description);
 476                         break;
 477                 case 5:
 478                         display_group_info5(&info->all2);
 479                         break;
 480         }
 481 }
 482 
 483 /***********************************************************************
 484  * Query group information
 485  */
 486 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 487                                      TALLOC_CTX *mem_ctx,
 488                                      int argc, const char **argv)
 489 {
 490         struct policy_handle connect_pol, domain_pol, group_pol;
 491         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 492         enum samr_GroupInfoEnum info_level = GROUPINFOALL;
 493         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 494         union samr_GroupInfo *group_info = NULL;
 495         uint32 group_rid;
 496 
 497         if ((argc < 2) || (argc > 4)) {
 498                 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
 499                 return NT_STATUS_OK;
 500         }
 501 
 502         sscanf(argv[1], "%i", &group_rid);
 503 
 504         if (argc > 2)
 505                 info_level = atoi(argv[2]);
 506 
 507         if (argc > 3)
 508                 sscanf(argv[3], "%x", &access_mask);
 509 
 510         result = rpccli_try_samr_connects(cli, mem_ctx,
 511                                           MAXIMUM_ALLOWED_ACCESS,
 512                                           &connect_pol);
 513 
 514         if (!NT_STATUS_IS_OK(result))
 515                 goto done;
 516 
 517         result = rpccli_samr_OpenDomain(cli, mem_ctx,
 518                                         &connect_pol,
 519                                         MAXIMUM_ALLOWED_ACCESS,
 520                                         &domain_sid,
 521                                         &domain_pol);
 522 
 523         if (!NT_STATUS_IS_OK(result))
 524                 goto done;
 525 
 526         result = rpccli_samr_OpenGroup(cli, mem_ctx,
 527                                        &domain_pol,
 528                                        access_mask,
 529                                        group_rid,
 530                                        &group_pol);
 531 
 532         if (!NT_STATUS_IS_OK(result))
 533                 goto done;
 534 
 535         result = rpccli_samr_QueryGroupInfo(cli, mem_ctx,
 536                                             &group_pol,
 537                                             info_level,
 538                                             &group_info);
 539         if (!NT_STATUS_IS_OK(result)) {
 540                 goto done;
 541         }
 542 
 543         display_group_info(group_info, info_level);
 544 
 545         rpccli_samr_Close(cli, mem_ctx, &group_pol);
 546         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 547         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 548 done:
 549         return result;
 550 }
 551 
 552 /* Query groups a user is a member of */
 553 
 554 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 555                                           TALLOC_CTX *mem_ctx,
 556                                           int argc, const char **argv)
 557 {
 558         struct policy_handle            connect_pol,
 559                                 domain_pol,
 560                                 user_pol;
 561         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
 562         uint32                  user_rid;
 563         uint32                  access_mask = MAXIMUM_ALLOWED_ACCESS;
 564         int                     i;
 565         struct samr_RidWithAttributeArray *rid_array = NULL;
 566 
 567         if ((argc < 2) || (argc > 3)) {
 568                 printf("Usage: %s rid [access mask]\n", argv[0]);
 569                 return NT_STATUS_OK;
 570         }
 571 
 572         sscanf(argv[1], "%i", &user_rid);
 573 
 574         if (argc > 2)
 575                 sscanf(argv[2], "%x", &access_mask);
 576 
 577         result = rpccli_try_samr_connects(cli, mem_ctx,
 578                                           MAXIMUM_ALLOWED_ACCESS,
 579                                           &connect_pol);
 580 
 581         if (!NT_STATUS_IS_OK(result))
 582                 goto done;
 583 
 584         result = rpccli_samr_OpenDomain(cli, mem_ctx,
 585                                         &connect_pol,
 586                                         MAXIMUM_ALLOWED_ACCESS,
 587                                         &domain_sid, &domain_pol);
 588 
 589         if (!NT_STATUS_IS_OK(result))
 590                 goto done;
 591 
 592         result = rpccli_samr_OpenUser(cli, mem_ctx,
 593                                       &domain_pol,
 594                                       access_mask,
 595                                       user_rid,
 596                                       &user_pol);
 597 
 598         if (!NT_STATUS_IS_OK(result))
 599                 goto done;
 600 
 601         result = rpccli_samr_GetGroupsForUser(cli, mem_ctx,
 602                                               &user_pol,
 603                                               &rid_array);
 604 
 605         if (!NT_STATUS_IS_OK(result))
 606                 goto done;
 607 
 608         for (i = 0; i < rid_array->count; i++) {
 609                 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
 610                        rid_array->rids[i].rid,
 611                        rid_array->rids[i].attributes);
 612         }
 613 
 614         rpccli_samr_Close(cli, mem_ctx, &user_pol);
 615         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 616         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 617  done:
 618         return result;
 619 }
 620 
 621 /* Query aliases a user is a member of */
 622 
 623 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 624                                            TALLOC_CTX *mem_ctx,
 625                                            int argc, const char **argv)
 626 {
 627         struct policy_handle            connect_pol, domain_pol;
 628         NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
 629         DOM_SID                *sids;
 630         size_t                     num_sids;
 631         uint32                  access_mask = MAXIMUM_ALLOWED_ACCESS;
 632         int                     i;
 633         struct lsa_SidArray sid_array;
 634         struct samr_Ids alias_rids;
 635 
 636         if (argc < 3) {
 637                 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
 638                 return NT_STATUS_INVALID_PARAMETER;
 639         }
 640 
 641         sids = NULL;
 642         num_sids = 0;
 643 
 644         for (i=2; i<argc; i++) {
 645                 DOM_SID tmp_sid;
 646                 if (!string_to_sid(&tmp_sid, argv[i])) {
 647                         printf("%s is not a legal SID\n", argv[i]);
 648                         return NT_STATUS_INVALID_PARAMETER;
 649                 }
 650                 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
 651                 if (!NT_STATUS_IS_OK(result)) {
 652                         return result;
 653                 }
 654         }
 655 
 656         if (num_sids) {
 657                 sid_array.sids = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
 658                 if (sid_array.sids == NULL)
 659                         return NT_STATUS_NO_MEMORY;
 660         } else {
 661                 sid_array.sids = NULL;
 662         }
 663 
 664         for (i=0; i<num_sids; i++) {
 665                 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
 666                 if (!sid_array.sids[i].sid) {
 667                         return NT_STATUS_NO_MEMORY;
 668                 }
 669         }
 670 
 671         sid_array.num_sids = num_sids;
 672 
 673         result = rpccli_try_samr_connects(cli, mem_ctx,
 674                                           MAXIMUM_ALLOWED_ACCESS,
 675                                           &connect_pol);
 676 
 677         if (!NT_STATUS_IS_OK(result))
 678                 goto done;
 679 
 680         result = get_domain_handle(cli, mem_ctx, argv[1],
 681                                    &connect_pol,
 682                                    access_mask,
 683                                    &domain_sid,
 684                                    &domain_pol);
 685 
 686         if (!NT_STATUS_IS_OK(result))
 687                 goto done;
 688 
 689         result = rpccli_samr_GetAliasMembership(cli, mem_ctx,
 690                                                 &domain_pol,
 691                                                 &sid_array,
 692                                                 &alias_rids);
 693         if (!NT_STATUS_IS_OK(result))
 694                 goto done;
 695 
 696         for (i = 0; i < alias_rids.count; i++) {
 697                 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
 698         }
 699 
 700         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 701         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 702  done:
 703         return result;
 704 }
 705 
 706 /* Query members of a group */
 707 
 708 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 709                                         TALLOC_CTX *mem_ctx,
 710                                         int argc, const char **argv)
 711 {
 712         struct policy_handle connect_pol, domain_pol, group_pol;
 713         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 714         uint32 group_rid;
 715         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 716         int i;
 717         unsigned int old_timeout;
 718         struct samr_RidTypeArray *rids = NULL;
 719 
 720         if ((argc < 2) || (argc > 3)) {
 721                 printf("Usage: %s rid [access mask]\n", argv[0]);
 722                 return NT_STATUS_OK;
 723         }
 724 
 725         sscanf(argv[1], "%i", &group_rid);
 726 
 727         if (argc > 2)
 728                 sscanf(argv[2], "%x", &access_mask);
 729 
 730         result = rpccli_try_samr_connects(cli, mem_ctx,
 731                                           MAXIMUM_ALLOWED_ACCESS,
 732                                           &connect_pol);
 733 
 734         if (!NT_STATUS_IS_OK(result))
 735                 goto done;
 736 
 737         result = rpccli_samr_OpenDomain(cli, mem_ctx,
 738                                         &connect_pol,
 739                                         MAXIMUM_ALLOWED_ACCESS,
 740                                         &domain_sid,
 741                                         &domain_pol);
 742 
 743         if (!NT_STATUS_IS_OK(result))
 744                 goto done;
 745 
 746         result = rpccli_samr_OpenGroup(cli, mem_ctx,
 747                                        &domain_pol,
 748                                        access_mask,
 749                                        group_rid,
 750                                        &group_pol);
 751 
 752         if (!NT_STATUS_IS_OK(result))
 753                 goto done;
 754 
 755         /* Make sure to wait for our DC's reply */
 756         old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
 757         rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
 758 
 759         result = rpccli_samr_QueryGroupMember(cli, mem_ctx,
 760                                               &group_pol,
 761                                               &rids);
 762 
 763         rpccli_set_timeout(cli, old_timeout);
 764 
 765         if (!NT_STATUS_IS_OK(result))
 766                 goto done;
 767 
 768         for (i = 0; i < rids->count; i++) {
 769                 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
 770                        rids->types[i]);
 771         }
 772 
 773         rpccli_samr_Close(cli, mem_ctx, &group_pol);
 774         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 775         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 776  done:
 777         return result;
 778 }
 779 
 780 /* Enumerate domain users */
 781 
 782 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 783                                         TALLOC_CTX *mem_ctx,
 784                                         int argc, const char **argv)
 785 {
 786         struct policy_handle connect_pol, domain_pol;
 787         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 788         uint32 start_idx, size, num_dom_users, i;
 789         struct samr_SamArray *dom_users = NULL;
 790         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 791         uint32 acb_mask = ACB_NORMAL;
 792 
 793         if ((argc < 1) || (argc > 3)) {
 794                 printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
 795                 return NT_STATUS_OK;
 796         }
 797 
 798         if (argc > 1)
 799                 sscanf(argv[1], "%x", &access_mask);
 800 
 801         if (argc > 2)
 802                 sscanf(argv[2], "%x", &acb_mask);
 803 
 804         /* Get sam policy handle */
 805 
 806         result = rpccli_try_samr_connects(cli, mem_ctx,
 807                                           MAXIMUM_ALLOWED_ACCESS,
 808                                           &connect_pol);
 809 
 810         if (!NT_STATUS_IS_OK(result))
 811                 goto done;
 812 
 813         /* Get domain policy handle */
 814 
 815         result = get_domain_handle(cli, mem_ctx, "domain",
 816                                    &connect_pol,
 817                                    access_mask,
 818                                    &domain_sid,
 819                                    &domain_pol);
 820         if (!NT_STATUS_IS_OK(result))
 821                 goto done;
 822 
 823         /* Enumerate domain users */
 824 
 825         start_idx = 0;
 826         size = 0xffff;
 827 
 828         do {
 829                 result = rpccli_samr_EnumDomainUsers(cli, mem_ctx,
 830                                                      &domain_pol,
 831                                                      &start_idx,
 832                                                      acb_mask,
 833                                                      &dom_users,
 834                                                      size,
 835                                                      &num_dom_users);
 836 
 837                 if (NT_STATUS_IS_OK(result) ||
 838                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 839 
 840                         for (i = 0; i < num_dom_users; i++)
 841                                printf("user:[%s] rid:[0x%x]\n",
 842                                        dom_users->entries[i].name.string,
 843                                        dom_users->entries[i].idx);
 844                 }
 845 
 846         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 847 
 848  done:
 849         if (is_valid_policy_hnd(&domain_pol))
 850                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 851 
 852         if (is_valid_policy_hnd(&connect_pol))
 853                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 854 
 855         return result;
 856 }
 857 
 858 /* Enumerate domain groups */
 859 
 860 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 861                                          TALLOC_CTX *mem_ctx,
 862                                          int argc, const char **argv)
 863 {
 864         struct policy_handle connect_pol, domain_pol;
 865         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 866         uint32 start_idx, size, num_dom_groups, i;
 867         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 868         struct samr_SamArray *dom_groups = NULL;
 869 
 870         if ((argc < 1) || (argc > 2)) {
 871                 printf("Usage: %s [access_mask]\n", argv[0]);
 872                 return NT_STATUS_OK;
 873         }
 874 
 875         if (argc > 1)
 876                 sscanf(argv[1], "%x", &access_mask);
 877 
 878         /* Get sam policy handle */
 879 
 880         result = rpccli_try_samr_connects(cli, mem_ctx,
 881                                           MAXIMUM_ALLOWED_ACCESS,
 882                                           &connect_pol);
 883 
 884         if (!NT_STATUS_IS_OK(result))
 885                 goto done;
 886 
 887         /* Get domain policy handle */
 888 
 889         result = get_domain_handle(cli, mem_ctx, "domain",
 890                                    &connect_pol,
 891                                    access_mask,
 892                                    &domain_sid,
 893                                    &domain_pol);
 894         if (!NT_STATUS_IS_OK(result))
 895                 goto done;
 896 
 897         /* Enumerate domain groups */
 898 
 899         start_idx = 0;
 900         size = 0xffff;
 901 
 902         do {
 903                 result = rpccli_samr_EnumDomainGroups(cli, mem_ctx,
 904                                                       &domain_pol,
 905                                                       &start_idx,
 906                                                       &dom_groups,
 907                                                       size,
 908                                                       &num_dom_groups);
 909                 if (NT_STATUS_IS_OK(result) ||
 910                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 911 
 912                         for (i = 0; i < num_dom_groups; i++)
 913                                 printf("group:[%s] rid:[0x%x]\n",
 914                                        dom_groups->entries[i].name.string,
 915                                        dom_groups->entries[i].idx);
 916                 }
 917 
 918         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 919 
 920  done:
 921         if (is_valid_policy_hnd(&domain_pol))
 922                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 923 
 924         if (is_valid_policy_hnd(&connect_pol))
 925                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 926 
 927         return result;
 928 }
 929 
 930 /* Enumerate alias groups */
 931 
 932 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
 933                                          TALLOC_CTX *mem_ctx,
 934                                          int argc, const char **argv)
 935 {
 936         struct policy_handle connect_pol, domain_pol;
 937         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 938         uint32 start_idx, size, num_als_groups, i;
 939         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
 940         struct samr_SamArray *als_groups = NULL;
 941 
 942         if ((argc < 2) || (argc > 3)) {
 943                 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
 944                 return NT_STATUS_OK;
 945         }
 946 
 947         if (argc > 2)
 948                 sscanf(argv[2], "%x", &access_mask);
 949 
 950         /* Get sam policy handle */
 951 
 952         result = rpccli_try_samr_connects(cli, mem_ctx,
 953                                           MAXIMUM_ALLOWED_ACCESS,
 954                                           &connect_pol);
 955 
 956         if (!NT_STATUS_IS_OK(result))
 957                 goto done;
 958 
 959         /* Get domain policy handle */
 960 
 961         result = get_domain_handle(cli, mem_ctx, argv[1],
 962                                    &connect_pol,
 963                                    access_mask,
 964                                    &domain_sid,
 965                                    &domain_pol);
 966 
 967         if (!NT_STATUS_IS_OK(result))
 968                 goto done;
 969 
 970         /* Enumerate alias groups */
 971 
 972         start_idx = 0;
 973         size = 0xffff;          /* Number of groups to retrieve */
 974 
 975         do {
 976                 result = rpccli_samr_EnumDomainAliases(cli, mem_ctx,
 977                                                        &domain_pol,
 978                                                        &start_idx,
 979                                                        &als_groups,
 980                                                        size,
 981                                                        &num_als_groups);
 982 
 983                 if (NT_STATUS_IS_OK(result) ||
 984                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
 985 
 986                         for (i = 0; i < num_als_groups; i++)
 987                                 printf("group:[%s] rid:[0x%x]\n",
 988                                        als_groups->entries[i].name.string,
 989                                        als_groups->entries[i].idx);
 990                 }
 991         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
 992 
 993  done:
 994         if (is_valid_policy_hnd(&domain_pol))
 995                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
 996 
 997         if (is_valid_policy_hnd(&connect_pol))
 998                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
 999 
1000         return result;
1001 }
1002 
1003 /* Enumerate domains */
1004 
1005 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1006                                       TALLOC_CTX *mem_ctx,
1007                                       int argc, const char **argv)
1008 {
1009         struct policy_handle connect_pol;
1010         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1011         uint32 start_idx, size, num_entries, i;
1012         uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1013         struct samr_SamArray *sam = NULL;
1014 
1015         if ((argc < 1) || (argc > 2)) {
1016                 printf("Usage: %s [access mask]\n", argv[0]);
1017                 return NT_STATUS_OK;
1018         }
1019 
1020         if (argc > 1) {
1021                 sscanf(argv[1], "%x", &access_mask);
1022         }
1023 
1024         /* Get sam policy handle */
1025 
1026         result = rpccli_try_samr_connects(cli, mem_ctx,
1027                                           access_mask,
1028                                           &connect_pol);
1029 
1030         if (!NT_STATUS_IS_OK(result)) {
1031                 goto done;
1032         }
1033 
1034         /* Enumerate alias groups */
1035 
1036         start_idx = 0;
1037         size = 0xffff;
1038 
1039         do {
1040                 result = rpccli_samr_EnumDomains(cli, mem_ctx,
1041                                                  &connect_pol,
1042                                                  &start_idx,
1043                                                  &sam,
1044                                                  size,
1045                                                  &num_entries);
1046 
1047                 if (NT_STATUS_IS_OK(result) ||
1048                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1049 
1050                         for (i = 0; i < num_entries; i++)
1051                                 printf("name:[%s] idx:[0x%x]\n",
1052                                        sam->entries[i].name.string,
1053                                        sam->entries[i].idx);
1054                 }
1055         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1056 
1057  done:
1058         if (is_valid_policy_hnd(&connect_pol)) {
1059                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1060         }
1061 
1062         return result;
1063 }
1064 
1065 
1066 /* Query alias membership */
1067 
1068 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1069                                         TALLOC_CTX *mem_ctx,
1070                                         int argc, const char **argv)
1071 {
1072         struct policy_handle connect_pol, domain_pol, alias_pol;
1073         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1074         uint32 alias_rid, i;
1075         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1076         struct lsa_SidArray sid_array;
1077 
1078         if ((argc < 3) || (argc > 4)) {
1079                 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1080                 return NT_STATUS_OK;
1081         }
1082 
1083         sscanf(argv[2], "%i", &alias_rid);
1084 
1085         if (argc > 3)
1086                 sscanf(argv[3], "%x", &access_mask);
1087 
1088         /* Open SAMR handle */
1089 
1090         result = rpccli_try_samr_connects(cli, mem_ctx,
1091                                           MAXIMUM_ALLOWED_ACCESS,
1092                                           &connect_pol);
1093 
1094         if (!NT_STATUS_IS_OK(result))
1095                 goto done;
1096 
1097         /* Open handle on domain */
1098 
1099         result = get_domain_handle(cli, mem_ctx, argv[1],
1100                                    &connect_pol,
1101                                    MAXIMUM_ALLOWED_ACCESS,
1102                                    &domain_sid,
1103                                    &domain_pol);
1104 
1105         if (!NT_STATUS_IS_OK(result))
1106                 goto done;
1107 
1108         /* Open handle on alias */
1109 
1110         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1111                                        &domain_pol,
1112                                        access_mask,
1113                                        alias_rid,
1114                                        &alias_pol);
1115         if (!NT_STATUS_IS_OK(result))
1116                 goto done;
1117 
1118         result = rpccli_samr_GetMembersInAlias(cli, mem_ctx,
1119                                                &alias_pol,
1120                                                &sid_array);
1121 
1122         if (!NT_STATUS_IS_OK(result))
1123                 goto done;
1124 
1125         for (i = 0; i < sid_array.num_sids; i++) {
1126                 fstring sid_str;
1127 
1128                 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1129                 printf("\tsid:[%s]\n", sid_str);
1130         }
1131 
1132         rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1133         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1134         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1135  done:
1136         return result;
1137 }
1138 
1139 /* Query alias info */
1140 
1141 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1142                                          TALLOC_CTX *mem_ctx,
1143                                          int argc, const char **argv)
1144 {
1145         struct policy_handle connect_pol, domain_pol, alias_pol;
1146         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1147         uint32_t alias_rid;
1148         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1149         union samr_AliasInfo *info = NULL;
1150         enum samr_AliasInfoEnum level = ALIASINFOALL;
1151 
1152         if ((argc < 3) || (argc > 4)) {
1153                 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1154                         argv[0]);
1155                 return NT_STATUS_OK;
1156         }
1157 
1158         sscanf(argv[2], "%i", &alias_rid);
1159 
1160         if (argc > 2) {
1161                 level = atoi(argv[3]);
1162         }
1163 
1164         if (argc > 3) {
1165                 sscanf(argv[4], "%x", &access_mask);
1166         }
1167 
1168         /* Open SAMR handle */
1169 
1170         result = rpccli_try_samr_connects(cli, mem_ctx,
1171                                           SEC_FLAG_MAXIMUM_ALLOWED,
1172                                           &connect_pol);
1173 
1174         if (!NT_STATUS_IS_OK(result)) {
1175                 goto done;
1176         }
1177 
1178         /* Open handle on domain */
1179 
1180         result = get_domain_handle(cli, mem_ctx, argv[1],
1181                                    &connect_pol,
1182                                    SEC_FLAG_MAXIMUM_ALLOWED,
1183                                    &domain_sid,
1184                                    &domain_pol);
1185 
1186         if (!NT_STATUS_IS_OK(result)) {
1187                 goto done;
1188         }
1189 
1190         /* Open handle on alias */
1191 
1192         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1193                                        &domain_pol,
1194                                        access_mask,
1195                                        alias_rid,
1196                                        &alias_pol);
1197         if (!NT_STATUS_IS_OK(result)) {
1198                 goto done;
1199         }
1200 
1201         result = rpccli_samr_QueryAliasInfo(cli, mem_ctx,
1202                                             &alias_pol,
1203                                             level,
1204                                             &info);
1205 
1206         if (!NT_STATUS_IS_OK(result)) {
1207                 goto done;
1208         }
1209 
1210         switch (level) {
1211                 case ALIASINFOALL:
1212                         printf("Name: %s\n", info->all.name.string);
1213                         printf("Description: %s\n", info->all.description.string);
1214                         printf("Num Members: %d\n", info->all.num_members);
1215                         break;
1216                 case ALIASINFONAME:
1217                         printf("Name: %s\n", info->name.string);
1218                         break;
1219                 case ALIASINFODESCRIPTION:
1220                         printf("Description: %s\n", info->description.string);
1221                         break;
1222                 default:
1223                         break;
1224         }
1225 
1226         rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1227         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1228         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1229  done:
1230         return result;
1231 }
1232 
1233 
1234 /* Query delete an alias membership */
1235 
1236 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1237                                       TALLOC_CTX *mem_ctx,
1238                                       int argc, const char **argv)
1239 {
1240         struct policy_handle connect_pol, domain_pol, alias_pol;
1241         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1242         uint32 alias_rid;
1243         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1244 
1245         if (argc != 3) {
1246                 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1247                 return NT_STATUS_OK;
1248         }
1249 
1250         alias_rid = strtoul(argv[2], NULL, 10);
1251 
1252         /* Open SAMR handle */
1253 
1254         result = rpccli_try_samr_connects(cli, mem_ctx,
1255                                           MAXIMUM_ALLOWED_ACCESS,
1256                                           &connect_pol);
1257 
1258         if (!NT_STATUS_IS_OK(result))
1259                 goto done;
1260 
1261         /* Open handle on domain */
1262 
1263         result = get_domain_handle(cli, mem_ctx, argv[1],
1264                                    &connect_pol,
1265                                    MAXIMUM_ALLOWED_ACCESS,
1266                                    &domain_sid,
1267                                    &domain_pol);
1268 
1269         if (!NT_STATUS_IS_OK(result))
1270                 goto done;
1271 
1272         /* Open handle on alias */
1273 
1274         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1275                                        &domain_pol,
1276                                        access_mask,
1277                                        alias_rid,
1278                                        &alias_pol);
1279         if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1280                 /* Probably this was a user name, try lookupnames */
1281                 struct samr_Ids rids, types;
1282                 struct lsa_String lsa_acct_name;
1283 
1284                 init_lsa_String(&lsa_acct_name, argv[2]);
1285 
1286                 result = rpccli_samr_LookupNames(cli, mem_ctx,
1287                                                  &domain_pol,
1288                                                  1,
1289                                                  &lsa_acct_name,
1290                                                  &rids,
1291                                                  &types);
1292 
1293                 if (NT_STATUS_IS_OK(result)) {
1294                         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1295                                                        &domain_pol,
1296                                                        access_mask,
1297                                                        rids.ids[0],
1298                                                        &alias_pol);
1299                 }
1300         }
1301 
1302         result = rpccli_samr_DeleteDomAlias(cli, mem_ctx,
1303                                             &alias_pol);
1304 
1305         if (!NT_STATUS_IS_OK(result))
1306                 goto done;
1307 
1308         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1309         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1310  done:
1311         return result;
1312 }
1313 
1314 /* Query display info */
1315 
1316 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1317                                                  TALLOC_CTX *mem_ctx,
1318                                                  int argc, const char **argv,
1319                                                  uint32_t opcode)
1320 {
1321         struct policy_handle connect_pol, domain_pol;
1322         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1323         uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1324         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1325         uint32 info_level = 1;
1326         union samr_DispInfo info;
1327         int loop_count = 0;
1328         bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1329         uint32_t total_size, returned_size;
1330 
1331         if (argc > 6) {
1332                 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1333                 return NT_STATUS_OK;
1334         }
1335 
1336         if (argc >= 2)
1337                 sscanf(argv[1], "%i", &info_level);
1338 
1339         if (argc >= 3)
1340                 sscanf(argv[2], "%i", &start_idx);
1341 
1342         if (argc >= 4) {
1343                 sscanf(argv[3], "%i", &max_entries);
1344                 got_params = True;
1345         }
1346 
1347         if (argc >= 5) {
1348                 sscanf(argv[4], "%i", &max_size);
1349                 got_params = True;
1350         }
1351 
1352         if (argc >= 6)
1353                 sscanf(argv[5], "%x", &access_mask);
1354 
1355         /* Get sam policy handle */
1356 
1357         result = rpccli_try_samr_connects(cli, mem_ctx,
1358                                           MAXIMUM_ALLOWED_ACCESS,
1359                                           &connect_pol);
1360 
1361         if (!NT_STATUS_IS_OK(result))
1362                 goto done;
1363 
1364         /* Get domain policy handle */
1365 
1366         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1367                                         &connect_pol,
1368                                         access_mask,
1369                                         &domain_sid,
1370                                         &domain_pol);
1371 
1372         if (!NT_STATUS_IS_OK(result))
1373                 goto done;
1374 
1375         /* Query display info */
1376 
1377         do {
1378 
1379                 if (!got_params)
1380                         get_query_dispinfo_params(
1381                                 loop_count, &max_entries, &max_size);
1382 
1383                 switch (opcode) {
1384                 case NDR_SAMR_QUERYDISPLAYINFO:
1385                         result = rpccli_samr_QueryDisplayInfo(cli, mem_ctx,
1386                                                               &domain_pol,
1387                                                               info_level,
1388                                                               start_idx,
1389                                                               max_entries,
1390                                                               max_size,
1391                                                               &total_size,
1392                                                               &returned_size,
1393                                                               &info);
1394                         break;
1395                 case NDR_SAMR_QUERYDISPLAYINFO2:
1396                         result = rpccli_samr_QueryDisplayInfo2(cli, mem_ctx,
1397                                                                &domain_pol,
1398                                                                info_level,
1399                                                                start_idx,
1400                                                                max_entries,
1401                                                                max_size,
1402                                                                &total_size,
1403                                                                &returned_size,
1404                                                                &info);
1405 
1406                         break;
1407                 case NDR_SAMR_QUERYDISPLAYINFO3:
1408                         result = rpccli_samr_QueryDisplayInfo3(cli, mem_ctx,
1409                                                                &domain_pol,
1410                                                                info_level,
1411                                                                start_idx,
1412                                                                max_entries,
1413                                                                max_size,
1414                                                                &total_size,
1415                                                                &returned_size,
1416                                                                &info);
1417 
1418                         break;
1419                 default:
1420                         return NT_STATUS_INVALID_PARAMETER;
1421                 }
1422 
1423                 if (!NT_STATUS_IS_OK(result) &&
1424                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1425                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1426                         break;
1427                 }
1428 
1429                 loop_count++;
1430 
1431                 switch (info_level) {
1432                         case 1:
1433                                 num_entries = info.info1.count;
1434                                 break;
1435                         case 2:
1436                                 num_entries = info.info2.count;
1437                                 break;
1438                         case 3:
1439                                 num_entries = info.info3.count;
1440                                 break;
1441                         case 4:
1442                                 num_entries = info.info4.count;
1443                                 break;
1444                         case 5:
1445                                 num_entries = info.info5.count;
1446                                 break;
1447                         default:
1448                                 break;
1449                 }
1450 
1451                 start_idx += num_entries;
1452 
1453                 if (num_entries == 0)
1454                         break;
1455 
1456                 for (i = 0; i < num_entries; i++) {
1457                         switch (info_level) {
1458                         case 1:
1459                                 display_sam_info_1(&info.info1.entries[i]);
1460                                 break;
1461                         case 2:
1462                                 display_sam_info_2(&info.info2.entries[i]);
1463                                 break;
1464                         case 3:
1465                                 display_sam_info_3(&info.info3.entries[i]);
1466                                 break;
1467                         case 4:
1468                                 display_sam_info_4(&info.info4.entries[i]);
1469                                 break;
1470                         case 5:
1471                                 display_sam_info_5(&info.info5.entries[i]);
1472                                 break;
1473                         }
1474                 }
1475         } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1476 
1477         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1478         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1479  done:
1480         return result;
1481 }
1482 
1483 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1484                                         TALLOC_CTX *mem_ctx,
1485                                         int argc, const char **argv)
1486 {
1487         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1488                                                 NDR_SAMR_QUERYDISPLAYINFO);
1489 }
1490 
1491 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1492                                          TALLOC_CTX *mem_ctx,
1493                                          int argc, const char **argv)
1494 {
1495         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1496                                                 NDR_SAMR_QUERYDISPLAYINFO2);
1497 }
1498 
1499 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1500                                          TALLOC_CTX *mem_ctx,
1501                                          int argc, const char **argv)
1502 {
1503         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1504                                                 NDR_SAMR_QUERYDISPLAYINFO3);
1505 }
1506 
1507 /* Query domain info */
1508 
1509 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1510                                        TALLOC_CTX *mem_ctx,
1511                                        int argc, const char **argv)
1512 {
1513         struct policy_handle connect_pol, domain_pol;
1514         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1515         uint32 switch_level = 2;
1516         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1517         union samr_DomainInfo *info = NULL;
1518 
1519         if (argc > 3) {
1520                 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1521                 return NT_STATUS_OK;
1522         }
1523 
1524         if (argc > 1)
1525                 sscanf(argv[1], "%i", &switch_level);
1526 
1527         if (argc > 2)
1528                 sscanf(argv[2], "%x", &access_mask);
1529 
1530         /* Get sam policy handle */
1531 
1532         result = rpccli_try_samr_connects(cli, mem_ctx,
1533                                           MAXIMUM_ALLOWED_ACCESS,
1534                                           &connect_pol);
1535 
1536         if (!NT_STATUS_IS_OK(result))
1537                 goto done;
1538 
1539         /* Get domain policy handle */
1540 
1541         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1542                                         &connect_pol,
1543                                         access_mask,
1544                                         &domain_sid,
1545                                         &domain_pol);
1546 
1547         if (!NT_STATUS_IS_OK(result))
1548                 goto done;
1549 
1550         /* Query domain info */
1551 
1552         result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
1553                                              &domain_pol,
1554                                              switch_level,
1555                                              &info);
1556 
1557         if (!NT_STATUS_IS_OK(result))
1558                 goto done;
1559 
1560         /* Display domain info */
1561 
1562         switch (switch_level) {
1563         case 1:
1564                 display_sam_dom_info_1(&info->info1);
1565                 break;
1566         case 2:
1567                 display_sam_dom_info_2(&info->general);
1568                 break;
1569         case 3:
1570                 display_sam_dom_info_3(&info->info3);
1571                 break;
1572         case 4:
1573                 display_sam_dom_info_4(&info->oem);
1574                 break;
1575         case 5:
1576                 display_sam_dom_info_5(&info->info5);
1577                 break;
1578         case 6:
1579                 display_sam_dom_info_6(&info->info6);
1580                 break;
1581         case 7:
1582                 display_sam_dom_info_7(&info->info7);
1583                 break;
1584         case 8:
1585                 display_sam_dom_info_8(&info->info8);
1586                 break;
1587         case 9:
1588                 display_sam_dom_info_9(&info->info9);
1589                 break;
1590         case 12:
1591                 display_sam_dom_info_12(&info->info12);
1592                 break;
1593         case 13:
1594                 display_sam_dom_info_13(&info->info13);
1595                 break;
1596 
1597         default:
1598                 printf("cannot display domain info for switch value %d\n",
1599                        switch_level);
1600                 break;
1601         }
1602 
1603  done:
1604 
1605         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1606         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1607         return result;
1608 }
1609 
1610 /* Create domain user */
1611 
1612 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1613                                          TALLOC_CTX *mem_ctx,
1614                                          int argc, const char **argv)
1615 {
1616         struct policy_handle connect_pol, domain_pol, user_pol;
1617         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1618         struct lsa_String acct_name;
1619         uint32 acb_info;
1620         uint32 acct_flags, user_rid;
1621         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1622         uint32_t access_granted = 0;
1623 
1624         if ((argc < 2) || (argc > 3)) {
1625                 printf("Usage: %s username [access mask]\n", argv[0]);
1626                 return NT_STATUS_OK;
1627         }
1628 
1629         init_lsa_String(&acct_name, argv[1]);
1630 
1631         if (argc > 2)
1632                 sscanf(argv[2], "%x", &access_mask);
1633 
1634         /* Get sam policy handle */
1635 
1636         result = rpccli_try_samr_connects(cli, mem_ctx,
1637                                           MAXIMUM_ALLOWED_ACCESS,
1638                                           &connect_pol);
1639 
1640         if (!NT_STATUS_IS_OK(result))
1641                 goto done;
1642 
1643         /* Get domain policy handle */
1644 
1645         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1646                                         &connect_pol,
1647                                         access_mask,
1648                                         &domain_sid,
1649                                         &domain_pol);
1650 
1651         if (!NT_STATUS_IS_OK(result))
1652                 goto done;
1653 
1654         /* Create domain user */
1655 
1656         acb_info = ACB_NORMAL;
1657         acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1658                      SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1659                      SAMR_USER_ACCESS_SET_PASSWORD |
1660                      SAMR_USER_ACCESS_GET_ATTRIBUTES |
1661                      SAMR_USER_ACCESS_SET_ATTRIBUTES;
1662 
1663         result = rpccli_samr_CreateUser2(cli, mem_ctx,
1664                                          &domain_pol,
1665                                          &acct_name,
1666                                          acb_info,
1667                                          acct_flags,
1668                                          &user_pol,
1669                                          &access_granted,
1670                                          &user_rid);
1671 
1672         if (!NT_STATUS_IS_OK(result))
1673                 goto done;
1674 
1675         result = rpccli_samr_Close(cli, mem_ctx, &user_pol);
1676         if (!NT_STATUS_IS_OK(result)) goto done;
1677 
1678         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1679         if (!NT_STATUS_IS_OK(result)) goto done;
1680 
1681         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1682         if (!NT_STATUS_IS_OK(result)) goto done;
1683 
1684  done:
1685         return result;
1686 }
1687 
1688 /* Create domain group */
1689 
1690 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1691                                           TALLOC_CTX *mem_ctx,
1692                                           int argc, const char **argv)
1693 {
1694         struct policy_handle connect_pol, domain_pol, group_pol;
1695         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1696         struct lsa_String grp_name;
1697         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1698         uint32_t rid = 0;
1699 
1700         if ((argc < 2) || (argc > 3)) {
1701                 printf("Usage: %s groupname [access mask]\n", argv[0]);
1702                 return NT_STATUS_OK;
1703         }
1704 
1705         init_lsa_String(&grp_name, argv[1]);
1706 
1707         if (argc > 2)
1708                 sscanf(argv[2], "%x", &access_mask);
1709 
1710         /* Get sam policy handle */
1711 
1712         result = rpccli_try_samr_connects(cli, mem_ctx,
1713                                           MAXIMUM_ALLOWED_ACCESS,
1714                                           &connect_pol);
1715 
1716         if (!NT_STATUS_IS_OK(result))
1717                 goto done;
1718 
1719         /* Get domain policy handle */
1720 
1721         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1722                                         &connect_pol,
1723                                         access_mask,
1724                                         &domain_sid,
1725                                         &domain_pol);
1726 
1727         if (!NT_STATUS_IS_OK(result))
1728                 goto done;
1729 
1730         /* Create domain user */
1731         result = rpccli_samr_CreateDomainGroup(cli, mem_ctx,
1732                                                &domain_pol,
1733                                                &grp_name,
1734                                                MAXIMUM_ALLOWED_ACCESS,
1735                                                &group_pol,
1736                                                &rid);
1737 
1738         if (!NT_STATUS_IS_OK(result))
1739                 goto done;
1740 
1741         result = rpccli_samr_Close(cli, mem_ctx, &group_pol);
1742         if (!NT_STATUS_IS_OK(result)) goto done;
1743 
1744         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1745         if (!NT_STATUS_IS_OK(result)) goto done;
1746 
1747         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1748         if (!NT_STATUS_IS_OK(result)) goto done;
1749 
1750  done:
1751         return result;
1752 }
1753 
1754 /* Create domain alias */
1755 
1756 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1757                                           TALLOC_CTX *mem_ctx,
1758                                           int argc, const char **argv)
1759 {
1760         struct policy_handle connect_pol, domain_pol, alias_pol;
1761         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1762         struct lsa_String alias_name;
1763         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1764         uint32_t rid = 0;
1765 
1766         if ((argc < 2) || (argc > 3)) {
1767                 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1768                 return NT_STATUS_OK;
1769         }
1770 
1771         init_lsa_String(&alias_name, argv[1]);
1772 
1773         if (argc > 2)
1774                 sscanf(argv[2], "%x", &access_mask);
1775 
1776         /* Get sam policy handle */
1777 
1778         result = rpccli_try_samr_connects(cli, mem_ctx,
1779                                           MAXIMUM_ALLOWED_ACCESS,
1780                                           &connect_pol);
1781 
1782         if (!NT_STATUS_IS_OK(result))
1783                 goto done;
1784 
1785         /* Get domain policy handle */
1786 
1787         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1788                                         &connect_pol,
1789                                         access_mask,
1790                                         &domain_sid,
1791                                         &domain_pol);
1792 
1793         if (!NT_STATUS_IS_OK(result))
1794                 goto done;
1795 
1796         /* Create domain user */
1797 
1798         result = rpccli_samr_CreateDomAlias(cli, mem_ctx,
1799                                             &domain_pol,
1800                                             &alias_name,
1801                                             MAXIMUM_ALLOWED_ACCESS,
1802                                             &alias_pol,
1803                                             &rid);
1804 
1805         if (!NT_STATUS_IS_OK(result))
1806                 goto done;
1807 
1808         result = rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1809         if (!NT_STATUS_IS_OK(result)) goto done;
1810 
1811         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1812         if (!NT_STATUS_IS_OK(result)) goto done;
1813 
1814         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1815         if (!NT_STATUS_IS_OK(result)) goto done;
1816 
1817  done:
1818         return result;
1819 }
1820 
1821 /* Lookup sam names */
1822 
1823 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1824                                       TALLOC_CTX *mem_ctx,
1825                                       int argc, const char **argv)
1826 {
1827         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1828         struct policy_handle connect_pol, domain_pol;
1829         uint32 num_names;
1830         struct samr_Ids rids, name_types;
1831         int i;
1832         struct lsa_String *names = NULL;;
1833 
1834         if (argc < 3) {
1835                 printf("Usage: %s  domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1836                 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1837                 printf("or check on the builtin SID: S-1-5-32\n");
1838                 return NT_STATUS_OK;
1839         }
1840 
1841         /* Get sam policy and domain handles */
1842 
1843         result = rpccli_try_samr_connects(cli, mem_ctx,
1844                                           MAXIMUM_ALLOWED_ACCESS,
1845                                           &connect_pol);
1846 
1847         if (!NT_STATUS_IS_OK(result))
1848                 goto done;
1849 
1850         result = get_domain_handle(cli, mem_ctx, argv[1],
1851                                    &connect_pol,
1852                                    MAXIMUM_ALLOWED_ACCESS,
1853                                    &domain_sid,
1854                                    &domain_pol);
1855 
1856         if (!NT_STATUS_IS_OK(result))
1857                 goto done;
1858 
1859         /* Look up names */
1860 
1861         num_names = argc - 2;
1862 
1863         if ((names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names)) == NULL) {
1864                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1865                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1866                 result = NT_STATUS_NO_MEMORY;
1867                 goto done;
1868         }
1869 
1870         for (i = 0; i < num_names; i++) {
1871                 init_lsa_String(&names[i], argv[i + 2]);
1872         }
1873 
1874         result = rpccli_samr_LookupNames(cli, mem_ctx,
1875                                          &domain_pol,
1876                                          num_names,
1877                                          names,
1878                                          &rids,
1879                                          &name_types);
1880 
1881         if (!NT_STATUS_IS_OK(result))
1882                 goto done;
1883 
1884         /* Display results */
1885 
1886         for (i = 0; i < num_names; i++)
1887                 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
1888                        name_types.ids[i]);
1889 
1890         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1891         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1892  done:
1893         return result;
1894 }
1895 
1896 /* Lookup sam rids */
1897 
1898 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1899                                      TALLOC_CTX *mem_ctx,
1900                                      int argc, const char **argv)
1901 {
1902         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1903         struct policy_handle connect_pol, domain_pol;
1904         uint32_t num_rids, *rids;
1905         struct lsa_Strings names;
1906         struct samr_Ids types;
1907 
1908         int i;
1909 
1910         if (argc < 3) {
1911                 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
1912                 return NT_STATUS_OK;
1913         }
1914 
1915         /* Get sam policy and domain handles */
1916 
1917         result = rpccli_try_samr_connects(cli, mem_ctx,
1918                                           MAXIMUM_ALLOWED_ACCESS,
1919                                           &connect_pol);
1920 
1921         if (!NT_STATUS_IS_OK(result))
1922                 goto done;
1923 
1924         result = get_domain_handle(cli, mem_ctx, argv[1],
1925                                    &connect_pol,
1926                                    MAXIMUM_ALLOWED_ACCESS,
1927                                    &domain_sid,
1928                                    &domain_pol);
1929 
1930         if (!NT_STATUS_IS_OK(result))
1931                 goto done;
1932 
1933         /* Look up rids */
1934 
1935         num_rids = argc - 2;
1936 
1937         if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1938                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1939                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1940                 result = NT_STATUS_NO_MEMORY;
1941                 goto done;
1942         }
1943 
1944         for (i = 0; i < argc - 2; i++)
1945                 sscanf(argv[i + 2], "%i", &rids[i]);
1946 
1947         result = rpccli_samr_LookupRids(cli, mem_ctx,
1948                                         &domain_pol,
1949                                         num_rids,
1950                                         rids,
1951                                         &names,
1952                                         &types);
1953 
1954         if (!NT_STATUS_IS_OK(result) &&
1955             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1956                 goto done;
1957 
1958         /* Display results */
1959 
1960         for (i = 0; i < num_rids; i++) {
1961                 printf("rid 0x%x: %s (%d)\n",
1962                         rids[i], names.names[i].string, types.ids[i]);
1963         }
1964 
1965         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1966         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1967  done:
1968         return result;
1969 }
1970 
1971 /* Delete domain group */
1972 
1973 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
1974                                          TALLOC_CTX *mem_ctx,
1975                                          int argc, const char **argv)
1976 {
1977         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1978         struct policy_handle connect_pol, domain_pol, group_pol;
1979         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1980 
1981         if ((argc < 2) || (argc > 3)) {
1982                 printf("Usage: %s groupname\n", argv[0]);
1983                 return NT_STATUS_OK;
1984         }
1985 
1986         if (argc > 2)
1987                 sscanf(argv[2], "%x", &access_mask);
1988 
1989         /* Get sam policy and domain handles */
1990 
1991         result = rpccli_try_samr_connects(cli, mem_ctx,
1992                                           MAXIMUM_ALLOWED_ACCESS,
1993                                           &connect_pol);
1994 
1995         if (!NT_STATUS_IS_OK(result))
1996                 goto done;
1997 
1998         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1999                                         &connect_pol,
2000                                         MAXIMUM_ALLOWED_ACCESS,
2001                                         &domain_sid,
2002                                         &domain_pol);
2003 
2004         if (!NT_STATUS_IS_OK(result))
2005                 goto done;
2006 
2007         /* Get handle on group */
2008 
2009         {
2010                 struct samr_Ids group_rids, name_types;
2011                 struct lsa_String lsa_acct_name;
2012 
2013                 init_lsa_String(&lsa_acct_name, argv[1]);
2014 
2015                 result = rpccli_samr_LookupNames(cli, mem_ctx,
2016                                                  &domain_pol,
2017                                                  1,
2018                                                  &lsa_acct_name,
2019                                                  &group_rids,
2020                                                  &name_types);
2021                 if (!NT_STATUS_IS_OK(result))
2022                         goto done;
2023 
2024                 result = rpccli_samr_OpenGroup(cli, mem_ctx,
2025                                                &domain_pol,
2026                                                access_mask,
2027                                                group_rids.ids[0],
2028                                                &group_pol);
2029 
2030                 if (!NT_STATUS_IS_OK(result))
2031                         goto done;
2032         }
2033 
2034         /* Delete group */
2035 
2036         result = rpccli_samr_DeleteDomainGroup(cli, mem_ctx,
2037                                                &group_pol);
2038 
2039         if (!NT_STATUS_IS_OK(result))
2040                 goto done;
2041 
2042         /* Display results */
2043 
2044         rpccli_samr_Close(cli, mem_ctx, &group_pol);
2045         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2046         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2047 
2048  done:
2049         return result;
2050 }
2051 
2052 /* Delete domain user */
2053 
2054 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2055                                          TALLOC_CTX *mem_ctx,
2056                                          int argc, const char **argv)
2057 {
2058         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2059         struct policy_handle connect_pol, domain_pol, user_pol;
2060         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2061 
2062         if ((argc < 2) || (argc > 3)) {
2063                 printf("Usage: %s username\n", argv[0]);
2064                 return NT_STATUS_OK;
2065         }
2066 
2067         if (argc > 2)
2068                 sscanf(argv[2], "%x", &access_mask);
2069 
2070         /* Get sam policy and domain handles */
2071 
2072         result = rpccli_try_samr_connects(cli, mem_ctx,
2073                                           MAXIMUM_ALLOWED_ACCESS,
2074                                           &connect_pol);
2075 
2076         if (!NT_STATUS_IS_OK(result))
2077                 goto done;
2078 
2079         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2080                                         &connect_pol,
2081                                         MAXIMUM_ALLOWED_ACCESS,
2082                                         &domain_sid,
2083                                         &domain_pol);
2084 
2085         if (!NT_STATUS_IS_OK(result))
2086                 goto done;
2087 
2088         /* Get handle on user */
2089 
2090         {
2091                 struct samr_Ids user_rids, name_types;
2092                 struct lsa_String lsa_acct_name;
2093 
2094                 init_lsa_String(&lsa_acct_name, argv[1]);
2095 
2096                 result = rpccli_samr_LookupNames(cli, mem_ctx,
2097                                                  &domain_pol,
2098                                                  1,
2099                                                  &lsa_acct_name,
2100                                                  &user_rids,
2101                                                  &name_types);
2102 
2103                 if (!NT_STATUS_IS_OK(result))
2104                         goto done;
2105 
2106                 result = rpccli_samr_OpenUser(cli, mem_ctx,
2107                                               &domain_pol,
2108                                               access_mask,
2109                                               user_rids.ids[0],
2110                                               &user_pol);
2111 
2112                 if (!NT_STATUS_IS_OK(result))
2113                         goto done;
2114         }
2115 
2116         /* Delete user */
2117 
2118         result = rpccli_samr_DeleteUser(cli, mem_ctx,
2119                                         &user_pol);
2120 
2121         if (!NT_STATUS_IS_OK(result))
2122                 goto done;
2123 
2124         /* Display results */
2125 
2126         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2127         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2128         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2129 
2130  done:
2131         return result;
2132 }
2133 
2134 /**********************************************************************
2135  * Query user security object
2136  */
2137 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2138                                     TALLOC_CTX *mem_ctx,
2139                                     int argc, const char **argv)
2140 {
2141         struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2142         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2143         uint32 sec_info = DACL_SECURITY_INFORMATION;
2144         uint32 user_rid = 0;
2145         TALLOC_CTX *ctx = NULL;
2146         SEC_DESC_BUF *sec_desc_buf=NULL;
2147         bool domain = False;
2148 
2149         ctx=talloc_init("cmd_samr_query_sec_obj");
2150 
2151         if ((argc < 1) || (argc > 3)) {
2152                 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2153                 printf("\tSpecify rid for security on user, -d for security on domain\n");
2154                 talloc_destroy(ctx);
2155                 return NT_STATUS_OK;
2156         }
2157 
2158         if (argc > 1) {
2159                 if (strcmp(argv[1], "-d") == 0)
2160                         domain = True;
2161                 else
2162                         sscanf(argv[1], "%i", &user_rid);
2163         }
2164 
2165         if (argc == 3) {
2166                 sec_info = atoi(argv[2]);
2167         }
2168 
2169         result = rpccli_try_samr_connects(cli, mem_ctx,
2170                                           MAXIMUM_ALLOWED_ACCESS,
2171                                           &connect_pol);
2172 
2173         if (!NT_STATUS_IS_OK(result))
2174                 goto done;
2175 
2176         if (domain || user_rid)
2177                 result = rpccli_samr_OpenDomain(cli, mem_ctx,
2178                                                 &connect_pol,
2179                                                 MAXIMUM_ALLOWED_ACCESS,
2180                                                 &domain_sid,
2181                                                 &domain_pol);
2182 
2183         if (!NT_STATUS_IS_OK(result))
2184                 goto done;
2185 
2186         if (user_rid)
2187                 result = rpccli_samr_OpenUser(cli, mem_ctx,
2188                                               &domain_pol,
2189                                               MAXIMUM_ALLOWED_ACCESS,
2190                                               user_rid,
2191                                               &user_pol);
2192 
2193         if (!NT_STATUS_IS_OK(result))
2194                 goto done;
2195 
2196         /* Pick which query pol to use */
2197 
2198         pol = &connect_pol;
2199 
2200         if (domain)
2201                 pol = &domain_pol;
2202 
2203         if (user_rid)
2204                 pol = &user_pol;
2205 
2206         /* Query SAM security object */
2207 
2208         result = rpccli_samr_QuerySecurity(cli, mem_ctx,
2209                                            pol,
2210                                            sec_info,
2211                                            &sec_desc_buf);
2212 
2213         if (!NT_STATUS_IS_OK(result))
2214                 goto done;
2215 
2216         display_sec_desc(sec_desc_buf->sd);
2217 
2218         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2219         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2220         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2221 done:
2222         talloc_destroy(ctx);
2223         return result;
2224 }
2225 
2226 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2227                                            TALLOC_CTX *mem_ctx,
2228                                            int argc, const char **argv)
2229 {
2230         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2231         struct policy_handle connect_pol, domain_pol, user_pol;
2232         struct samr_PwInfo info;
2233         uint32_t rid;
2234 
2235         if (argc != 2) {
2236                 printf("Usage: %s rid\n", argv[0]);
2237                 return NT_STATUS_OK;
2238         }
2239 
2240         sscanf(argv[1], "%i", &rid);
2241 
2242         result = rpccli_try_samr_connects(cli, mem_ctx,
2243                                           MAXIMUM_ALLOWED_ACCESS,
2244                                           &connect_pol);
2245 
2246         if (!NT_STATUS_IS_OK(result)) {
2247                 goto done;
2248         }
2249 
2250         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2251                                         &connect_pol,
2252                                         MAXIMUM_ALLOWED_ACCESS,
2253                                         &domain_sid,
2254                                         &domain_pol);
2255 
2256         if (!NT_STATUS_IS_OK(result)) {
2257                 goto done;
2258         }
2259 
2260         result = rpccli_samr_OpenUser(cli, mem_ctx,
2261                                       &domain_pol,
2262                                       MAXIMUM_ALLOWED_ACCESS,
2263                                       rid,
2264                                       &user_pol);
2265 
2266         if (!NT_STATUS_IS_OK(result)) {
2267                 goto done;
2268         }
2269 
2270         result = rpccli_samr_GetUserPwInfo(cli, mem_ctx, &user_pol, &info);
2271         if (NT_STATUS_IS_OK(result)) {
2272                 printf("min_password_length: %d\n", info.min_password_length);
2273                 printf("%s\n",
2274                         NDR_PRINT_STRUCT_STRING(mem_ctx,
2275                                 samr_PasswordProperties, &info.password_properties));
2276         }
2277 
2278  done:
2279         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2280         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2281         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2282 
2283         return result;
2284 }
2285 
2286 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2287                                         TALLOC_CTX *mem_ctx,
2288                                         int argc, const char **argv)
2289 {
2290         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2291         struct lsa_String domain_name;
2292         struct samr_PwInfo info;
2293 
2294         if (argc < 1 || argc > 3) {
2295                 printf("Usage: %s <domain>\n", argv[0]);
2296                 return NT_STATUS_OK;
2297         }
2298 
2299         init_lsa_String(&domain_name, argv[1]);
2300 
2301         result = rpccli_samr_GetDomPwInfo(cli, mem_ctx, &domain_name, &info);
2302 
2303         if (NT_STATUS_IS_OK(result)) {
2304                 printf("min_password_length: %d\n", info.min_password_length);
2305                 display_password_properties(info.password_properties);
2306         }
2307 
2308         return result;
2309 }
2310 
2311 /* Look up domain name */
2312 
2313 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2314                                        TALLOC_CTX *mem_ctx,
2315                                        int argc, const char **argv)
2316 {
2317         struct policy_handle connect_pol, domain_pol;
2318         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2319         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2320         fstring sid_string;
2321         struct lsa_String domain_name;
2322         DOM_SID *sid = NULL;
2323 
2324         if (argc != 2) {
2325                 printf("Usage: %s domain_name\n", argv[0]);
2326                 return NT_STATUS_OK;
2327         }
2328 
2329         init_lsa_String(&domain_name, argv[1]);
2330 
2331         result = rpccli_try_samr_connects(cli, mem_ctx,
2332                                           access_mask,
2333                                           &connect_pol);
2334 
2335         if (!NT_STATUS_IS_OK(result))
2336                 goto done;
2337 
2338         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2339                                         &connect_pol,
2340                                         access_mask,
2341                                         &domain_sid,
2342                                         &domain_pol);
2343 
2344         if (!NT_STATUS_IS_OK(result))
2345                 goto done;
2346 
2347         result = rpccli_samr_LookupDomain(cli, mem_ctx,
2348                                           &connect_pol,
2349                                           &domain_name,
2350                                           &sid);
2351 
2352         if (NT_STATUS_IS_OK(result)) {
2353                 sid_to_fstring(sid_string, sid);
2354                 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2355                        argv[1], sid_string);
2356         }
2357 
2358         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2359         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2360 done:
2361         return result;
2362 }
2363 
2364 /* Change user password */
2365 
2366 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2367                                    TALLOC_CTX *mem_ctx,
2368                                    int argc, const char **argv)
2369 {
2370         struct policy_handle connect_pol, domain_pol, user_pol;
2371         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2372         const char *user, *oldpass, *newpass;
2373         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2374         struct samr_Ids rids, types;
2375         struct lsa_String lsa_acct_name;
2376 
2377         if (argc < 3) {
2378                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2379                 return NT_STATUS_INVALID_PARAMETER;
2380         }
2381 
2382         user = argv[1];
2383         oldpass = argv[2];
2384         newpass = argv[3];
2385 
2386         /* Get sam policy handle */
2387 
2388         result = rpccli_try_samr_connects(cli, mem_ctx,
2389                                           MAXIMUM_ALLOWED_ACCESS,
2390                                           &connect_pol);
2391 
2392         if (!NT_STATUS_IS_OK(result)) {
2393                 goto done;
2394         }
2395 
2396         /* Get domain policy handle */
2397 
2398         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2399                                         &connect_pol,
2400                                         access_mask,
2401                                         &domain_sid,
2402                                         &domain_pol);
2403 
2404         if (!NT_STATUS_IS_OK(result)) {
2405                 goto done;
2406         }
2407 
2408         init_lsa_String(&lsa_acct_name, user);
2409 
2410         result = rpccli_samr_LookupNames(cli, mem_ctx,
2411                                          &domain_pol,
2412                                          1,
2413                                          &lsa_acct_name,
2414                                          &rids,
2415                                          &types);
2416 
2417         if (!NT_STATUS_IS_OK(result)) {
2418                 goto done;
2419         }
2420 
2421         result = rpccli_samr_OpenUser(cli, mem_ctx,
2422                                       &domain_pol,
2423                                       access_mask,
2424                                       rids.ids[0],
2425                                       &user_pol);
2426 
2427         if (!NT_STATUS_IS_OK(result)) {
2428                 goto done;
2429         }
2430 
2431         /* Change user password */
2432         result = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2433                                             &user_pol,
2434                                             newpass,
2435                                             oldpass);
2436 
2437         if (!NT_STATUS_IS_OK(result)) {
2438                 goto done;
2439         }
2440 
2441  done:
2442         if (is_valid_policy_hnd(&user_pol)) {
2443                 rpccli_samr_Close(cli, mem_ctx, &user_pol);
2444         }
2445         if (is_valid_policy_hnd(&domain_pol)) {
2446                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2447         }
2448         if (is_valid_policy_hnd(&connect_pol)) {
2449                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2450         }
2451 
2452         return result;
2453 }
2454 
2455 
2456 /* Change user password */
2457 
2458 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2459                                     TALLOC_CTX *mem_ctx,
2460                                     int argc, const char **argv)
2461 {
2462         struct policy_handle connect_pol, domain_pol;
2463         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2464         const char *user, *oldpass, *newpass;
2465         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2466 
2467         if (argc < 3) {
2468                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2469                 return NT_STATUS_INVALID_PARAMETER;
2470         }
2471 
2472         user = argv[1];
2473         oldpass = argv[2];
2474         newpass = argv[3];
2475 
2476         /* Get sam policy handle */
2477 
2478         result = rpccli_try_samr_connects(cli, mem_ctx,
2479                                           MAXIMUM_ALLOWED_ACCESS,
2480                                           &connect_pol);
2481 
2482         if (!NT_STATUS_IS_OK(result))
2483                 goto done;
2484 
2485         /* Get domain policy handle */
2486 
2487         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2488                                         &connect_pol,
2489                                         access_mask,
2490                                         &domain_sid,
2491                                         &domain_pol);
2492 
2493         if (!NT_STATUS_IS_OK(result))
2494                 goto done;
2495 
2496         /* Change user password */
2497         result = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2498 
2499         if (!NT_STATUS_IS_OK(result))
2500                 goto done;
2501 
2502         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2503         if (!NT_STATUS_IS_OK(result)) goto done;
2504 
2505         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2506         if (!NT_STATUS_IS_OK(result)) goto done;
2507 
2508  done:
2509         return result;
2510 }
2511 
2512 
2513 /* Change user password */
2514 
2515 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2516                                     TALLOC_CTX *mem_ctx,
2517                                     int argc, const char **argv)
2518 {
2519         struct policy_handle connect_pol, domain_pol;
2520         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2521         const char *user, *oldpass, *newpass;
2522         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2523         struct samr_DomInfo1 *info = NULL;
2524         struct samr_ChangeReject *reject = NULL;
2525 
2526         if (argc < 3) {
2527                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2528                 return NT_STATUS_INVALID_PARAMETER;
2529         }
2530 
2531         user = argv[1];
2532         oldpass = argv[2];
2533         newpass = argv[3];
2534 
2535         /* Get sam policy handle */
2536 
2537         result = rpccli_try_samr_connects(cli, mem_ctx,
2538                                           MAXIMUM_ALLOWED_ACCESS,
2539                                           &connect_pol);
2540 
2541         if (!NT_STATUS_IS_OK(result))
2542                 goto done;
2543 
2544         /* Get domain policy handle */
2545 
2546         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2547                                         &connect_pol,
2548                                         access_mask,
2549                                         &domain_sid,
2550                                         &domain_pol);
2551 
2552         if (!NT_STATUS_IS_OK(result))
2553                 goto done;
2554 
2555         /* Change user password */
2556         result = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2557                                              user,
2558                                              newpass,
2559                                              oldpass,
2560                                              &info,
2561                                              &reject);
2562 
2563         if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2564 
2565                 display_sam_dom_info_1(info);
2566 
2567                 switch (reject->reason) {
2568                         case SAMR_REJECT_TOO_SHORT:
2569                                 d_printf("SAMR_REJECT_TOO_SHORT\n");
2570                                 break;
2571                         case SAMR_REJECT_IN_HISTORY:
2572                                 d_printf("SAMR_REJECT_IN_HISTORY\n");
2573                                 break;
2574                         case SAMR_REJECT_COMPLEXITY:
2575                                 d_printf("SAMR_REJECT_COMPLEXITY\n");
2576                                 break;
2577                         case SAMR_REJECT_OTHER:
2578                                 d_printf("SAMR_REJECT_OTHER\n");
2579                                 break;
2580                         default:
2581                                 d_printf("unknown reject reason: %d\n",
2582                                         reject->reason);
2583                                 break;
2584                 }
2585         }
2586 
2587         if (!NT_STATUS_IS_OK(result))
2588                 goto done;
2589 
2590         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2591         if (!NT_STATUS_IS_OK(result)) goto done;
2592 
2593         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2594         if (!NT_STATUS_IS_OK(result)) goto done;
2595 
2596  done:
2597         return result;
2598 }
2599 
2600 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2601                                          TALLOC_CTX *mem_ctx,
2602                                          int argc, const char **argv,
2603                                          int opcode)
2604 {
2605         struct policy_handle connect_pol, domain_pol, user_pol;
2606         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2607         const char *user, *param;
2608         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2609         uint32_t level;
2610         uint32_t user_rid;
2611         union samr_UserInfo info;
2612         struct samr_CryptPassword pwd_buf;
2613         struct samr_CryptPasswordEx pwd_buf_ex;
2614         uint8_t nt_hash[16];
2615         uint8_t lm_hash[16];
2616         DATA_BLOB session_key;
2617         uint8_t password_expired = 0;
2618 
2619         if (argc < 4) {
2620                 printf("Usage: %s username level password [password_expired]\n",
2621                         argv[0]);
2622                 return NT_STATUS_INVALID_PARAMETER;
2623         }
2624 
2625         user = argv[1];
2626         level = atoi(argv[2]);
2627         param = argv[3];
2628 
2629         if (argc >= 5) {
2630                 password_expired = atoi(argv[4]);
2631         }
2632 
2633         status = cli_get_session_key(mem_ctx, cli, &session_key);
2634         if (!NT_STATUS_IS_OK(status)) {
2635                 return status;
2636         }
2637 
2638         init_samr_CryptPassword(param, &session_key, &pwd_buf);
2639         init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
2640         nt_lm_owf_gen(param, nt_hash, lm_hash);
2641 
2642         switch (level) {
2643         case 18:
2644                 {
2645                         DATA_BLOB in,out;
2646                         in = data_blob_const(nt_hash, 16);
2647                         out = data_blob_talloc_zero(mem_ctx, 16);
2648                         sess_crypt_blob(&out, &in, &session_key, true);
2649                         memcpy(nt_hash, out.data, out.length);
2650                 }
2651                 {
2652                         DATA_BLOB in,out;
2653                         in = data_blob_const(lm_hash, 16);
2654                         out = data_blob_talloc_zero(mem_ctx, 16);
2655                         sess_crypt_blob(&out, &in, &session_key, true);
2656                         memcpy(lm_hash, out.data, out.length);
2657                 }
2658 
2659                 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
2660                 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
2661                 info.info18.nt_pwd_active       = true;
2662                 info.info18.lm_pwd_active       = true;
2663                 info.info18.password_expired    = password_expired;
2664 
2665                 break;
2666         case 21:
2667                 ZERO_STRUCT(info.info21);
2668 
2669                 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2670                                              SAMR_FIELD_LM_PASSWORD_PRESENT;
2671                 if (argc >= 5) {
2672                         info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2673                         info.info21.password_expired = password_expired;
2674                 }
2675 
2676                 info.info21.lm_password_set = true;
2677                 info.info21.lm_owf_password.length = 16;
2678                 info.info21.lm_owf_password.size = 16;
2679 
2680                 info.info21.nt_password_set = true;
2681                 info.info21.nt_owf_password.length = 16;
2682                 info.info21.nt_owf_password.size = 16;
2683 
2684                 {
2685                         DATA_BLOB in,out;
2686                         in = data_blob_const(nt_hash, 16);
2687                         out = data_blob_talloc_zero(mem_ctx, 16);
2688                         sess_crypt_blob(&out, &in, &session_key, true);
2689                         info.info21.nt_owf_password.array =
2690                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
2691                 }
2692                 {
2693                         DATA_BLOB in,out;
2694                         in = data_blob_const(lm_hash, 16);
2695                         out = data_blob_talloc_zero(mem_ctx, 16);
2696                         sess_crypt_blob(&out, &in, &session_key, true);
2697                         info.info21.lm_owf_password.array =
2698                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
2699                 }
2700 
2701                 break;
2702         case 23:
2703                 ZERO_STRUCT(info.info23);
2704 
2705                 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2706                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
2707                 if (argc >= 5) {
2708                         info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2709                         info.info23.info.password_expired = password_expired;
2710                 }
2711 
2712                 info.info23.password = pwd_buf;
2713 
2714                 break;
2715         case 24:
2716                 info.info24.password            = pwd_buf;
2717                 info.info24.password_expired    = password_expired;
2718 
2719                 break;
2720         case 25:
2721                 ZERO_STRUCT(info.info25);
2722 
2723                 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2724                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
2725                 if (argc >= 5) {
2726                         info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2727                         info.info25.info.password_expired = password_expired;
2728                 }
2729 
2730                 info.info25.password = pwd_buf_ex;
2731 
2732                 break;
2733         case 26:
2734                 info.info26.password            = pwd_buf_ex;
2735                 info.info26.password_expired    = password_expired;
2736 
2737                 break;
2738         default:
2739                 return NT_STATUS_INVALID_INFO_CLASS;
2740         }
2741 
2742         /* Get sam policy handle */
2743 
2744         status = rpccli_try_samr_connects(cli, mem_ctx,
2745                                           MAXIMUM_ALLOWED_ACCESS,
2746                                           &connect_pol);
2747 
2748         if (!NT_STATUS_IS_OK(status))
2749                 goto done;
2750 
2751         /* Get domain policy handle */
2752 
2753         status = rpccli_samr_OpenDomain(cli, mem_ctx,
2754                                         &connect_pol,
2755                                         access_mask,
2756                                         &domain_sid,
2757                                         &domain_pol);
2758 
2759         if (!NT_STATUS_IS_OK(status))
2760                 goto done;
2761 
2762         user_rid = strtol(user, NULL, 0);
2763         if (user_rid) {
2764                 status = rpccli_samr_OpenUser(cli, mem_ctx,
2765                                               &domain_pol,
2766                                               access_mask,
2767                                               user_rid,
2768                                               &user_pol);
2769         }
2770 
2771         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
2772             (user_rid == 0)) {
2773 
2774                 /* Probably this was a user name, try lookupnames */
2775                 struct samr_Ids rids, types;
2776                 struct lsa_String lsa_acct_name;
2777 
2778                 init_lsa_String(&lsa_acct_name, user);
2779 
2780                 status = rpccli_samr_LookupNames(cli, mem_ctx,
2781                                                  &domain_pol,
2782                                                  1,
2783                                                  &lsa_acct_name,
2784                                                  &rids,
2785                                                  &types);
2786                 if (!NT_STATUS_IS_OK(status)) {
2787                         return status;
2788                 }
2789 
2790                 status = rpccli_samr_OpenUser(cli, mem_ctx,
2791                                               &domain_pol,
2792                                               access_mask,
2793                                               rids.ids[0],
2794                                               &user_pol);
2795                 if (!NT_STATUS_IS_OK(status)) {
2796                         return status;
2797                 }
2798         }
2799 
2800         switch (opcode) {
2801         case NDR_SAMR_SETUSERINFO:
2802                 status = rpccli_samr_SetUserInfo(cli, mem_ctx,
2803                                                  &user_pol,
2804                                                  level,
2805                                                  &info);
2806                 break;
2807         case NDR_SAMR_SETUSERINFO2:
2808                 status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
2809                                                   &user_pol,
2810                                                   level,
2811                                                   &info);
2812                 break;
2813         default:
2814                 return NT_STATUS_INVALID_PARAMETER;
2815         }
2816 
2817  done:
2818         return status;
2819 }
2820 
2821 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2822                                      TALLOC_CTX *mem_ctx,
2823                                      int argc, const char **argv)
2824 {
2825         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
2826                                         NDR_SAMR_SETUSERINFO);
2827 }
2828 
2829 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2830                                       TALLOC_CTX *mem_ctx,
2831                                       int argc, const char **argv)
2832 {
2833         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
2834                                         NDR_SAMR_SETUSERINFO2);
2835 }
2836 
2837 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
     /* [<][>][^][v][top][bottom][index][help] */
2838                                           TALLOC_CTX *mem_ctx,
2839                                           int argc, const char **argv)
2840 {
2841         NTSTATUS status;
2842         struct policy_handle connect_handle;
2843         struct policy_handle domain_handle;
2844         uint16_t level = 1;
2845         struct lsa_String name;
2846         uint32_t idx = 0;
2847 
2848         if (argc < 2 || argc > 3) {
2849                 printf("Usage: %s name level\n", argv[0]);
2850                 return NT_STATUS_INVALID_PARAMETER;
2851         }
2852 
2853         init_lsa_String(&name, argv[1]);
2854 
2855         if (argc == 3) {
2856                 level = atoi(argv[2]);
2857         }
2858 
2859         status = rpccli_try_samr_connects(cli, mem_ctx,
2860                                           SEC_FLAG_MAXIMUM_ALLOWED,
2861                                           &connect_handle);
2862 
2863         if (!NT_STATUS_IS_OK(status)) {
2864                 goto done;
2865         }
2866 
2867         status = rpccli_samr_OpenDomain(cli, mem_ctx,
2868                                         &connect_handle,
2869                                         SEC_FLAG_MAXIMUM_ALLOWED,
2870                                         &domain_sid,
2871                                         &domain_handle);
2872 
2873         if (!NT_STATUS_IS_OK(status))
2874                 goto done;
2875 
2876 
2877         status = rpccli_samr_GetDisplayEnumerationIndex(cli, mem_ctx,
2878                                                         &domain_handle,
2879                                                         level,
2880                                                         &name,
2881                                                         &idx);
2882 
2883         if (NT_STATUS_IS_OK(status) ||
2884             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
2885                 printf("idx: %d (0x%08x)\n", idx, idx);
2886         }
2887  done:
2888 
2889         if (is_valid_policy_hnd(&domain_handle)) {
2890                 rpccli_samr_Close(cli, mem_ctx, &domain_handle);
2891         }
2892         if (is_valid_policy_hnd(&connect_handle)) {
2893                 rpccli_samr_Close(cli, mem_ctx, &connect_handle);
2894         }
2895 
2896         return status;
2897 
2898 }
2899 /* List of commands exported by this module */
2900 
2901 struct cmd_set samr_commands[] = {
2902 
2903         { "SAMR" },
2904 
2905         { "queryuser",  RPC_RTYPE_NTSTATUS, cmd_samr_query_user,                NULL, &ndr_table_samr.syntax_id, NULL,  "Query user info",         "" },
2906         { "querygroup",         RPC_RTYPE_NTSTATUS, cmd_samr_query_group,               NULL, &ndr_table_samr.syntax_id, NULL,  "Query group info",        "" },
2907         { "queryusergroups",    RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups,  NULL, &ndr_table_samr.syntax_id, NULL,  "Query user groups",       "" },
2908         { "queryuseraliases",   RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases,         NULL, &ndr_table_samr.syntax_id, NULL,  "Query user aliases",      "" },
2909         { "querygroupmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query group membership",  "" },
2910         { "queryaliasmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias membership",  "" },
2911         { "queryaliasinfo",     RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias info",       "" },
2912         { "deletealias",        RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias,      NULL, &ndr_table_samr.syntax_id, NULL,  "Delete an alias",  "" },
2913         { "querydispinfo",      RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2914         { "querydispinfo2",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2915         { "querydispinfo3",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2916         { "querydominfo",       RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo,     NULL, &ndr_table_samr.syntax_id, NULL,  "Query domain info",       "" },
2917         { "enumdomusers",       RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users,       NULL, &ndr_table_samr.syntax_id, NULL,       "Enumerate domain users", "" },
2918         { "enumdomgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domain groups", "" },
2919         { "enumalsgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate alias groups",  "" },
2920         { "enumdomains",        RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains,          NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domains",  "" },
2921 
2922         { "createdomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain user",      "" },
2923         { "createdomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain group",     "" },
2924         { "createdomalias",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain alias",     "" },
2925         { "samlookupnames",     RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names,          NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
2926         { "samlookuprids",      RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids,           NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
2927         { "deletedomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain group",     "" },
2928         { "deletedomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain user",      "" },
2929         { "samquerysecobj",     RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj,         NULL, &ndr_table_samr.syntax_id, NULL, "Query SAMR security object",   "" },
2930         { "getdompwinfo",       RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo,        NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve domain password info", "" },
2931         { "getusrdompwinfo",    RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo,     NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve user domain password info", "" },
2932 
2933         { "lookupdomain",       RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain,         NULL, &ndr_table_samr.syntax_id, NULL, "Lookup Domain Name", "" },
2934         { "chgpasswd",          RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd,             NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2935         { "chgpasswd2",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2936         { "chgpasswd3",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2937         { "getdispinfoidx",     RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx,      NULL, &ndr_table_samr.syntax_id, NULL, "Get Display Information Index", "" },
2938         { "setuserinfo",        RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo,           NULL, &ndr_table_samr.syntax_id, NULL, "Set user info", "" },
2939         { "setuserinfo2",       RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2,          NULL, &ndr_table_samr.syntax_id, NULL, "Set user info2", "" },
2940         { NULL }
2941 };

/* [<][>][^][v][top][bottom][index][help] */