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