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