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