pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.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_t info_level = 21;
320         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
321         union samr_UserInfo *info = NULL;
322         uint32_t 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
389                 if (NT_STATUS_IS_OK(result)) {
390                         if (rids.count != 1) {
391                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
392                                 goto done;
393                         }
394                         if (types.count != 1) {
395                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
396                                 goto done;
397                         }
398
399                         status = dcerpc_samr_OpenUser(b, mem_ctx,
400                                                       &domain_pol,
401                                                       access_mask,
402                                                       rids.ids[0],
403                                                       &user_pol,
404                                                       &result);
405                         if (!NT_STATUS_IS_OK(status)) {
406                                 goto done;
407                         }
408                 }
409         }
410
411
412         if (!NT_STATUS_IS_OK(result)) {
413                 status = result;
414                 goto done;
415         }
416
417         status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
418                                            &user_pol,
419                                            info_level,
420                                            &info,
421                                            &result);
422         if (!NT_STATUS_IS_OK(status)) {
423                 goto done;
424         }
425         if (!NT_STATUS_IS_OK(result)) {
426                 status = result;
427                 goto done;
428         }
429
430         switch (info_level) {
431         case 7:
432                 display_samr_user_info_7(&info->info7);
433                 break;
434         case 9:
435                 display_samr_user_info_9(&info->info9);
436                 break;
437         case 16:
438                 display_samr_user_info_16(&info->info16);
439                 break;
440         case 20:
441                 display_samr_user_info_20(&info->info20);
442                 break;
443         case 21:
444                 display_samr_user_info_21(&info->info21);
445                 break;
446         default:
447                 printf("Unsupported infolevel: %d\n", info_level);
448                 break;
449         }
450
451         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
452         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
453         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
454
455 done:
456         return status;
457 }
458
459 /****************************************************************************
460  display group info
461  ****************************************************************************/
462 static void display_group_info1(struct samr_GroupInfoAll *info1)
463 {
464         printf("\tGroup Name:\t%s\n", info1->name.string);
465         printf("\tDescription:\t%s\n", info1->description.string);
466         printf("\tGroup Attribute:%d\n", info1->attributes);
467         printf("\tNum Members:%d\n", info1->num_members);
468 }
469
470 /****************************************************************************
471  display group info
472  ****************************************************************************/
473 static void display_group_info2(struct lsa_String *info2)
474 {
475         printf("\tGroup Description:%s\n", info2->string);
476 }
477
478
479 /****************************************************************************
480  display group info
481  ****************************************************************************/
482 static void display_group_info3(struct samr_GroupInfoAttributes *info3)
483 {
484         printf("\tGroup Attribute:%d\n", info3->attributes);
485 }
486
487
488 /****************************************************************************
489  display group info
490  ****************************************************************************/
491 static void display_group_info4(struct lsa_String *info4)
492 {
493         printf("\tGroup Description:%s\n", info4->string);
494 }
495
496 /****************************************************************************
497  display group info
498  ****************************************************************************/
499 static void display_group_info5(struct samr_GroupInfoAll *info5)
500 {
501         printf("\tGroup Name:\t%s\n", info5->name.string);
502         printf("\tDescription:\t%s\n", info5->description.string);
503         printf("\tGroup Attribute:%d\n", info5->attributes);
504         printf("\tNum Members:%d\n", info5->num_members);
505 }
506
507 /****************************************************************************
508  display sam sync structure
509  ****************************************************************************/
510 static void display_group_info(union samr_GroupInfo *info,
511                                enum samr_GroupInfoEnum level)
512 {
513         switch (level) {
514                 case 1:
515                         display_group_info1(&info->all);
516                         break;
517                 case 2:
518                         display_group_info2(&info->name);
519                         break;
520                 case 3:
521                         display_group_info3(&info->attributes);
522                         break;
523                 case 4:
524                         display_group_info4(&info->description);
525                         break;
526                 case 5:
527                         display_group_info5(&info->all2);
528                         break;
529         }
530 }
531
532 /***********************************************************************
533  * Query group information
534  */
535 static NTSTATUS cmd_samr_query_group(struct rpc_pipe_client *cli,
536                                      TALLOC_CTX *mem_ctx,
537                                      int argc, const char **argv)
538 {
539         struct policy_handle connect_pol, domain_pol, group_pol;
540         NTSTATUS status, result;
541         enum samr_GroupInfoEnum info_level = GROUPINFOALL;
542         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
543         union samr_GroupInfo *group_info = NULL;
544         uint32_t group_rid;
545         struct dcerpc_binding_handle *b = cli->binding_handle;
546
547         if ((argc < 2) || (argc > 4)) {
548                 printf("Usage: %s rid [info level] [access mask]\n", argv[0]);
549                 return NT_STATUS_OK;
550         }
551
552         sscanf(argv[1], "%i", &group_rid);
553
554         if (argc > 2)
555                 info_level = atoi(argv[2]);
556
557         if (argc > 3)
558                 sscanf(argv[3], "%x", &access_mask);
559
560         status = rpccli_try_samr_connects(cli, mem_ctx,
561                                           MAXIMUM_ALLOWED_ACCESS,
562                                           &connect_pol);
563         if (!NT_STATUS_IS_OK(status)) {
564                 goto done;
565         }
566
567         status = dcerpc_samr_OpenDomain(b, mem_ctx,
568                                         &connect_pol,
569                                         MAXIMUM_ALLOWED_ACCESS,
570                                         &domain_sid,
571                                         &domain_pol,
572                                         &result);
573         if (!NT_STATUS_IS_OK(status)) {
574                 goto done;
575         }
576         if (!NT_STATUS_IS_OK(result)) {
577                 status = result;
578                 goto done;
579         }
580
581         status = dcerpc_samr_OpenGroup(b, mem_ctx,
582                                        &domain_pol,
583                                        access_mask,
584                                        group_rid,
585                                        &group_pol,
586                                        &result);
587         if (!NT_STATUS_IS_OK(status)) {
588                 goto done;
589         }
590         if (!NT_STATUS_IS_OK(result)) {
591                 status = result;
592                 goto done;
593         }
594
595         status = dcerpc_samr_QueryGroupInfo(b, mem_ctx,
596                                             &group_pol,
597                                             info_level,
598                                             &group_info,
599                                             &result);
600         if (!NT_STATUS_IS_OK(status)) {
601                 goto done;
602         }
603         if (!NT_STATUS_IS_OK(result)) {
604                 status = result;
605                 goto done;
606         }
607
608         display_group_info(group_info, info_level);
609
610         dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
611         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
612         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
613 done:
614         return status;
615 }
616
617 /* Query groups a user is a member of */
618
619 static NTSTATUS cmd_samr_query_usergroups(struct rpc_pipe_client *cli,
620                                           TALLOC_CTX *mem_ctx,
621                                           int argc, const char **argv)
622 {
623         struct policy_handle            connect_pol,
624                                 domain_pol,
625                                 user_pol;
626         NTSTATUS status, result;
627         uint32_t                user_rid;
628         uint32_t                access_mask = MAXIMUM_ALLOWED_ACCESS;
629         int                     i;
630         struct samr_RidWithAttributeArray *rid_array = NULL;
631         struct dcerpc_binding_handle *b = cli->binding_handle;
632
633         if ((argc < 2) || (argc > 3)) {
634                 printf("Usage: %s rid [access mask]\n", argv[0]);
635                 return NT_STATUS_OK;
636         }
637
638         sscanf(argv[1], "%i", &user_rid);
639
640         if (argc > 2)
641                 sscanf(argv[2], "%x", &access_mask);
642
643         status = rpccli_try_samr_connects(cli, mem_ctx,
644                                           MAXIMUM_ALLOWED_ACCESS,
645                                           &connect_pol);
646         if (!NT_STATUS_IS_OK(status)) {
647                 goto done;
648         }
649
650         status = dcerpc_samr_OpenDomain(b, mem_ctx,
651                                         &connect_pol,
652                                         MAXIMUM_ALLOWED_ACCESS,
653                                         &domain_sid,
654                                         &domain_pol,
655                                         &result);
656         if (!NT_STATUS_IS_OK(status)) {
657                 goto done;
658         }
659         if (!NT_STATUS_IS_OK(result)) {
660                 status = result;
661                 goto done;
662         }
663
664         status = dcerpc_samr_OpenUser(b, mem_ctx,
665                                       &domain_pol,
666                                       access_mask,
667                                       user_rid,
668                                       &user_pol,
669                                       &result);
670
671         if (!NT_STATUS_IS_OK(status)) {
672                 goto done;
673         }
674         if (!NT_STATUS_IS_OK(result)) {
675                 status = result;
676                 goto done;
677         }
678
679         status = dcerpc_samr_GetGroupsForUser(b, mem_ctx,
680                                               &user_pol,
681                                               &rid_array,
682                                               &result);
683         if (!NT_STATUS_IS_OK(status)) {
684                 goto done;
685         }
686         if (!NT_STATUS_IS_OK(result)) {
687                 status = result;
688                 goto done;
689         }
690
691         for (i = 0; i < rid_array->count; i++) {
692                 printf("\tgroup rid:[0x%x] attr:[0x%x]\n",
693                        rid_array->rids[i].rid,
694                        rid_array->rids[i].attributes);
695         }
696
697         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
698         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
699         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
700  done:
701         return status;
702 }
703
704 /* Query aliases a user is a member of */
705
706 static NTSTATUS cmd_samr_query_useraliases(struct rpc_pipe_client *cli,
707                                            TALLOC_CTX *mem_ctx,
708                                            int argc, const char **argv)
709 {
710         struct policy_handle            connect_pol, domain_pol;
711         NTSTATUS status, result;
712         struct dom_sid                *sids;
713         uint32_t                num_sids;
714         uint32_t                access_mask = MAXIMUM_ALLOWED_ACCESS;
715         int                     i;
716         struct lsa_SidArray sid_array;
717         struct samr_Ids alias_rids;
718         struct dcerpc_binding_handle *b = cli->binding_handle;
719
720         if (argc < 3) {
721                 printf("Usage: %s builtin|domain sid1 sid2 ...\n", argv[0]);
722                 return NT_STATUS_INVALID_PARAMETER;
723         }
724
725         sids = NULL;
726         num_sids = 0;
727
728         for (i=2; i<argc; i++) {
729                 struct dom_sid tmp_sid;
730                 if (!string_to_sid(&tmp_sid, argv[i])) {
731                         printf("%s is not a legal SID\n", argv[i]);
732                         return NT_STATUS_INVALID_PARAMETER;
733                 }
734                 result = add_sid_to_array(mem_ctx, &tmp_sid, &sids, &num_sids);
735                 if (!NT_STATUS_IS_OK(result)) {
736                         return result;
737                 }
738         }
739
740         if (num_sids) {
741                 sid_array.sids = talloc_zero_array(mem_ctx, struct lsa_SidPtr, num_sids);
742                 if (sid_array.sids == NULL)
743                         return NT_STATUS_NO_MEMORY;
744         } else {
745                 sid_array.sids = NULL;
746         }
747
748         for (i=0; i<num_sids; i++) {
749                 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
750                 if (!sid_array.sids[i].sid) {
751                         return NT_STATUS_NO_MEMORY;
752                 }
753         }
754
755         sid_array.num_sids = num_sids;
756
757         status = rpccli_try_samr_connects(cli, mem_ctx,
758                                           MAXIMUM_ALLOWED_ACCESS,
759                                           &connect_pol);
760         if (!NT_STATUS_IS_OK(status)) {
761                 goto done;
762         }
763
764         status = get_domain_handle(cli, mem_ctx, argv[1],
765                                    &connect_pol,
766                                    access_mask,
767                                    &domain_sid,
768                                    &domain_pol);
769         if (!NT_STATUS_IS_OK(status)) {
770                 goto done;
771         }
772
773         status = dcerpc_samr_GetAliasMembership(b, mem_ctx,
774                                                 &domain_pol,
775                                                 &sid_array,
776                                                 &alias_rids,
777                                                 &result);
778         if (!NT_STATUS_IS_OK(status)) {
779                 goto done;
780         }
781         if (!NT_STATUS_IS_OK(result)) {
782                 status = result;
783                 goto done;
784         }
785
786         for (i = 0; i < alias_rids.count; i++) {
787                 printf("\tgroup rid:[0x%x]\n", alias_rids.ids[i]);
788         }
789
790         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
791         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
792  done:
793         return status;
794 }
795
796 /* Query members of a group */
797
798 static NTSTATUS cmd_samr_query_groupmem(struct rpc_pipe_client *cli,
799                                         TALLOC_CTX *mem_ctx,
800                                         int argc, const char **argv)
801 {
802         struct policy_handle connect_pol, domain_pol, group_pol;
803         NTSTATUS status, result;
804         uint32_t group_rid;
805         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
806         int i;
807         unsigned int old_timeout;
808         struct samr_RidAttrArray *rids = NULL;
809         struct dcerpc_binding_handle *b = cli->binding_handle;
810
811         if ((argc < 2) || (argc > 3)) {
812                 printf("Usage: %s rid [access mask]\n", argv[0]);
813                 return NT_STATUS_OK;
814         }
815
816         sscanf(argv[1], "%i", &group_rid);
817
818         if (argc > 2)
819                 sscanf(argv[2], "%x", &access_mask);
820
821         status = rpccli_try_samr_connects(cli, mem_ctx,
822                                           MAXIMUM_ALLOWED_ACCESS,
823                                           &connect_pol);
824         if (!NT_STATUS_IS_OK(status)) {
825                 goto done;
826         }
827
828         status = dcerpc_samr_OpenDomain(b, mem_ctx,
829                                         &connect_pol,
830                                         MAXIMUM_ALLOWED_ACCESS,
831                                         &domain_sid,
832                                         &domain_pol,
833                                         &result);
834         if (!NT_STATUS_IS_OK(status)) {
835                 goto done;
836         }
837         if (!NT_STATUS_IS_OK(result)) {
838                 status = result;
839                 goto done;
840         }
841
842         status = dcerpc_samr_OpenGroup(b, mem_ctx,
843                                        &domain_pol,
844                                        access_mask,
845                                        group_rid,
846                                        &group_pol,
847                                        &result);
848         if (!NT_STATUS_IS_OK(status)) {
849                 goto done;
850         }
851         if (!NT_STATUS_IS_OK(result)) {
852                 status = result;
853                 goto done;
854         }
855
856         /* Make sure to wait for our DC's reply */
857         old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
858         rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
859
860         status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
861                                               &group_pol,
862                                               &rids,
863                                               &result);
864
865         rpccli_set_timeout(cli, old_timeout);
866
867         if (!NT_STATUS_IS_OK(status)) {
868                 goto done;
869         }
870         if (!NT_STATUS_IS_OK(result)) {
871                 status = result;
872                 goto done;
873         }
874
875         for (i = 0; i < rids->count; i++) {
876                 printf("\trid:[0x%x] attr:[0x%x]\n", rids->rids[i],
877                        rids->attributes[i]);
878         }
879
880         dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
881         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
882         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
883  done:
884         return status;
885 }
886
887 /* Enumerate domain users */
888
889 static NTSTATUS cmd_samr_enum_dom_users(struct rpc_pipe_client *cli,
890                                         TALLOC_CTX *mem_ctx,
891                                         int argc, const char **argv)
892 {
893         struct policy_handle connect_pol;
894         struct policy_handle domain_pol = { 0, };
895         NTSTATUS status, result;
896         uint32_t start_idx, num_dom_users, i;
897         struct samr_SamArray *dom_users = NULL;
898         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
899         uint32_t acb_mask = ACB_NORMAL;
900         uint32_t size = 0xffff;
901         struct dcerpc_binding_handle *b = cli->binding_handle;
902
903         if ((argc < 1) || (argc > 4)) {
904                 printf("Usage: %s [access_mask] [acb_mask] [size]\n", argv[0]);
905                 return NT_STATUS_OK;
906         }
907
908         if (argc > 1) {
909                 sscanf(argv[1], "%x", &access_mask);
910         }
911
912         if (argc > 2) {
913                 sscanf(argv[2], "%x", &acb_mask);
914         }
915
916         if (argc > 3) {
917                 sscanf(argv[3], "%x", &size);
918         }
919
920         /* Get sam policy handle */
921
922         status = rpccli_try_samr_connects(cli, mem_ctx,
923                                           MAXIMUM_ALLOWED_ACCESS,
924                                           &connect_pol);
925         if (!NT_STATUS_IS_OK(status)) {
926                 goto done;
927         }
928
929         /* Get domain policy handle */
930
931         status = get_domain_handle(cli, mem_ctx, "domain",
932                                    &connect_pol,
933                                    access_mask,
934                                    &domain_sid,
935                                    &domain_pol);
936         if (!NT_STATUS_IS_OK(status)) {
937                 goto done;
938         }
939
940         /* Enumerate domain users */
941
942         start_idx = 0;
943
944         do {
945                 status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
946                                                      &domain_pol,
947                                                      &start_idx,
948                                                      acb_mask,
949                                                      &dom_users,
950                                                      size,
951                                                      &num_dom_users,
952                                                      &result);
953                 if (!NT_STATUS_IS_OK(status)) {
954                         goto done;
955                 }
956                 if (NT_STATUS_IS_OK(result) ||
957                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
958
959                         for (i = 0; i < num_dom_users; i++)
960                                printf("user:[%s] rid:[0x%x]\n",
961                                        dom_users->entries[i].name.string,
962                                        dom_users->entries[i].idx);
963                 }
964
965         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
966
967  done:
968         if (is_valid_policy_hnd(&domain_pol))
969                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
970
971         if (is_valid_policy_hnd(&connect_pol))
972                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
973
974         return status;
975 }
976
977 /* Enumerate domain groups */
978
979 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
980                                          TALLOC_CTX *mem_ctx,
981                                          int argc, const char **argv)
982 {
983         struct policy_handle connect_pol;
984         struct policy_handle domain_pol = { 0, };
985         NTSTATUS status, result;
986         uint32_t start_idx, num_dom_groups, i;
987         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
988         struct samr_SamArray *dom_groups = NULL;
989         uint32_t size = 0xffff;
990         struct dcerpc_binding_handle *b = cli->binding_handle;
991
992         if ((argc < 1) || (argc > 3)) {
993                 printf("Usage: %s [access_mask] [max_size]\n", argv[0]);
994                 return NT_STATUS_OK;
995         }
996
997         if (argc > 1) {
998                 sscanf(argv[1], "%x", &access_mask);
999         }
1000
1001         if (argc > 2) {
1002                 sscanf(argv[2], "%x", &size);
1003         }
1004
1005         /* Get sam policy handle */
1006
1007         status = rpccli_try_samr_connects(cli, mem_ctx,
1008                                           MAXIMUM_ALLOWED_ACCESS,
1009                                           &connect_pol);
1010         if (!NT_STATUS_IS_OK(status)) {
1011                 goto done;
1012         }
1013
1014         /* Get domain policy handle */
1015
1016         status = get_domain_handle(cli, mem_ctx, "domain",
1017                                    &connect_pol,
1018                                    access_mask,
1019                                    &domain_sid,
1020                                    &domain_pol);
1021         if (!NT_STATUS_IS_OK(status)) {
1022                 goto done;
1023         }
1024
1025         /* Enumerate domain groups */
1026
1027         start_idx = 0;
1028
1029         do {
1030                 status = dcerpc_samr_EnumDomainGroups(b, mem_ctx,
1031                                                       &domain_pol,
1032                                                       &start_idx,
1033                                                       &dom_groups,
1034                                                       size,
1035                                                       &num_dom_groups,
1036                                                       &result);
1037                 if (!NT_STATUS_IS_OK(status)) {
1038                         goto done;
1039                 }
1040                 if (NT_STATUS_IS_OK(result) ||
1041                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1042
1043                         for (i = 0; i < num_dom_groups; i++)
1044                                 printf("group:[%s] rid:[0x%x]\n",
1045                                        dom_groups->entries[i].name.string,
1046                                        dom_groups->entries[i].idx);
1047                 }
1048
1049         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1050
1051  done:
1052         if (is_valid_policy_hnd(&domain_pol))
1053                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1054
1055         if (is_valid_policy_hnd(&connect_pol))
1056                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1057
1058         return status;
1059 }
1060
1061 /* Enumerate alias groups */
1062
1063 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
1064                                          TALLOC_CTX *mem_ctx,
1065                                          int argc, const char **argv)
1066 {
1067         struct policy_handle connect_pol;
1068         struct policy_handle domain_pol = { 0, };
1069         NTSTATUS status, result;
1070         uint32_t start_idx, num_als_groups, i;
1071         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1072         struct samr_SamArray *als_groups = NULL;
1073         uint32_t size = 0xffff;
1074         struct dcerpc_binding_handle *b = cli->binding_handle;
1075
1076         if ((argc < 2) || (argc > 4)) {
1077                 printf("Usage: %s builtin|domain [access mask] [max_size]\n", argv[0]);
1078                 return NT_STATUS_OK;
1079         }
1080
1081         if (argc > 2) {
1082                 sscanf(argv[2], "%x", &access_mask);
1083         }
1084
1085         if (argc > 3) {
1086                 sscanf(argv[3], "%x", &size);
1087         }
1088
1089         /* Get sam policy handle */
1090
1091         status = rpccli_try_samr_connects(cli, mem_ctx,
1092                                           MAXIMUM_ALLOWED_ACCESS,
1093                                           &connect_pol);
1094         if (!NT_STATUS_IS_OK(status)) {
1095                 goto done;
1096         }
1097
1098         /* Get domain policy handle */
1099
1100         status = get_domain_handle(cli, mem_ctx, argv[1],
1101                                    &connect_pol,
1102                                    access_mask,
1103                                    &domain_sid,
1104                                    &domain_pol);
1105         if (!NT_STATUS_IS_OK(status)) {
1106                 goto done;
1107         }
1108
1109         /* Enumerate alias groups */
1110
1111         start_idx = 0;
1112
1113         do {
1114                 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
1115                                                        &domain_pol,
1116                                                        &start_idx,
1117                                                        &als_groups,
1118                                                        size,
1119                                                        &num_als_groups,
1120                                                        &result);
1121                 if (!NT_STATUS_IS_OK(status)) {
1122                         goto done;
1123                 }
1124                 if (NT_STATUS_IS_OK(result) ||
1125                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1126
1127                         for (i = 0; i < num_als_groups; i++)
1128                                 printf("group:[%s] rid:[0x%x]\n",
1129                                        als_groups->entries[i].name.string,
1130                                        als_groups->entries[i].idx);
1131                 }
1132         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1133
1134  done:
1135         if (is_valid_policy_hnd(&domain_pol))
1136                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1137
1138         if (is_valid_policy_hnd(&connect_pol))
1139                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1140
1141         return status;
1142 }
1143
1144 /* Enumerate domains */
1145
1146 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1147                                       TALLOC_CTX *mem_ctx,
1148                                       int argc, const char **argv)
1149 {
1150         struct policy_handle connect_pol;
1151         NTSTATUS status, result;
1152         uint32_t start_idx, size, num_entries, i;
1153         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1154         struct samr_SamArray *sam = NULL;
1155         struct dcerpc_binding_handle *b = cli->binding_handle;
1156
1157         if ((argc < 1) || (argc > 2)) {
1158                 printf("Usage: %s [access mask]\n", argv[0]);
1159                 return NT_STATUS_OK;
1160         }
1161
1162         if (argc > 1) {
1163                 sscanf(argv[1], "%x", &access_mask);
1164         }
1165
1166         /* Get sam policy handle */
1167
1168         status = rpccli_try_samr_connects(cli, mem_ctx,
1169                                           access_mask,
1170                                           &connect_pol);
1171         if (!NT_STATUS_IS_OK(status)) {
1172                 goto done;
1173         }
1174
1175         /* Enumerate alias groups */
1176
1177         start_idx = 0;
1178         size = 0xffff;
1179
1180         do {
1181                 status = dcerpc_samr_EnumDomains(b, mem_ctx,
1182                                                  &connect_pol,
1183                                                  &start_idx,
1184                                                  &sam,
1185                                                  size,
1186                                                  &num_entries,
1187                                                  &result);
1188                 if (!NT_STATUS_IS_OK(status)) {
1189                         goto done;
1190                 }
1191                 if (NT_STATUS_IS_OK(result) ||
1192                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1193
1194                         for (i = 0; i < num_entries; i++)
1195                                 printf("name:[%s] idx:[0x%x]\n",
1196                                        sam->entries[i].name.string,
1197                                        sam->entries[i].idx);
1198                 }
1199         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1200
1201  done:
1202         if (is_valid_policy_hnd(&connect_pol)) {
1203                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1204         }
1205
1206         return status;
1207 }
1208
1209
1210 /* Query alias membership */
1211
1212 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1213                                         TALLOC_CTX *mem_ctx,
1214                                         int argc, const char **argv)
1215 {
1216         struct policy_handle connect_pol, domain_pol, alias_pol;
1217         NTSTATUS status, result;
1218         uint32_t alias_rid, i;
1219         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1220         struct lsa_SidArray sid_array;
1221         struct dcerpc_binding_handle *b = cli->binding_handle;
1222
1223         if ((argc < 3) || (argc > 4)) {
1224                 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1225                 return NT_STATUS_OK;
1226         }
1227
1228         sscanf(argv[2], "%i", &alias_rid);
1229
1230         if (argc > 3)
1231                 sscanf(argv[3], "%x", &access_mask);
1232
1233         /* Open SAMR handle */
1234
1235         status = rpccli_try_samr_connects(cli, mem_ctx,
1236                                           MAXIMUM_ALLOWED_ACCESS,
1237                                           &connect_pol);
1238         if (!NT_STATUS_IS_OK(status)) {
1239                 goto done;
1240         }
1241
1242         /* Open handle on domain */
1243
1244         status = get_domain_handle(cli, mem_ctx, argv[1],
1245                                    &connect_pol,
1246                                    MAXIMUM_ALLOWED_ACCESS,
1247                                    &domain_sid,
1248                                    &domain_pol);
1249         if (!NT_STATUS_IS_OK(status)) {
1250                 goto done;
1251         }
1252
1253         /* Open handle on alias */
1254
1255         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1256                                        &domain_pol,
1257                                        access_mask,
1258                                        alias_rid,
1259                                        &alias_pol,
1260                                        &result);
1261         if (!NT_STATUS_IS_OK(status)) {
1262                 goto done;
1263         }
1264         if (!NT_STATUS_IS_OK(result)) {
1265                 status = result;
1266                 goto done;
1267         }
1268
1269         status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
1270                                                &alias_pol,
1271                                                &sid_array,
1272                                                &result);
1273         if (!NT_STATUS_IS_OK(status)) {
1274                 goto done;
1275         }
1276         if (!NT_STATUS_IS_OK(result)) {
1277                 status = result;
1278                 goto done;
1279         }
1280
1281         for (i = 0; i < sid_array.num_sids; i++) {
1282                 struct dom_sid_buf sid_str;
1283
1284                 printf("\tsid:[%s]\n",
1285                        dom_sid_str_buf(sid_array.sids[i].sid, &sid_str));
1286         }
1287
1288         dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1289         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1290         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1291  done:
1292         return status;
1293 }
1294
1295 /* Query alias info */
1296
1297 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1298                                          TALLOC_CTX *mem_ctx,
1299                                          int argc, const char **argv)
1300 {
1301         struct policy_handle connect_pol, domain_pol, alias_pol;
1302         NTSTATUS status, result;
1303         uint32_t alias_rid;
1304         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1305         union samr_AliasInfo *info = NULL;
1306         enum samr_AliasInfoEnum level = ALIASINFOALL;
1307         struct dcerpc_binding_handle *b = cli->binding_handle;
1308
1309         if ((argc < 3) || (argc > 4)) {
1310                 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1311                         argv[0]);
1312                 return NT_STATUS_OK;
1313         }
1314
1315         sscanf(argv[2], "%i", &alias_rid);
1316
1317         if (argc > 2) {
1318                 level = atoi(argv[3]);
1319         }
1320
1321         if (argc > 3) {
1322                 sscanf(argv[4], "%x", &access_mask);
1323         }
1324
1325         /* Open SAMR handle */
1326
1327         status = rpccli_try_samr_connects(cli, mem_ctx,
1328                                           SEC_FLAG_MAXIMUM_ALLOWED,
1329                                           &connect_pol);
1330         if (!NT_STATUS_IS_OK(status)) {
1331                 goto done;
1332         }
1333
1334         /* Open handle on domain */
1335
1336         status = get_domain_handle(cli, mem_ctx, argv[1],
1337                                    &connect_pol,
1338                                    SEC_FLAG_MAXIMUM_ALLOWED,
1339                                    &domain_sid,
1340                                    &domain_pol);
1341         if (!NT_STATUS_IS_OK(status)) {
1342                 goto done;
1343         }
1344
1345         /* Open handle on alias */
1346
1347         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1348                                        &domain_pol,
1349                                        access_mask,
1350                                        alias_rid,
1351                                        &alias_pol,
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         status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
1362                                             &alias_pol,
1363                                             level,
1364                                             &info,
1365                                             &result);
1366         if (!NT_STATUS_IS_OK(status)) {
1367                 goto done;
1368         }
1369         if (!NT_STATUS_IS_OK(result)) {
1370                 status = result;
1371                 goto done;
1372         }
1373
1374         switch (level) {
1375                 case ALIASINFOALL:
1376                         printf("Name: %s\n", info->all.name.string);
1377                         printf("Description: %s\n", info->all.description.string);
1378                         printf("Num Members: %d\n", info->all.num_members);
1379                         break;
1380                 case ALIASINFONAME:
1381                         printf("Name: %s\n", info->name.string);
1382                         break;
1383                 case ALIASINFODESCRIPTION:
1384                         printf("Description: %s\n", info->description.string);
1385                         break;
1386                 default:
1387                         break;
1388         }
1389
1390         dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
1391         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1392         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1393  done:
1394         return status;
1395 }
1396
1397
1398 /* Query delete an alias membership */
1399
1400 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1401                                       TALLOC_CTX *mem_ctx,
1402                                       int argc, const char **argv)
1403 {
1404         struct policy_handle connect_pol, domain_pol, alias_pol;
1405         NTSTATUS status, result;
1406         uint32_t alias_rid;
1407         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1408         struct dcerpc_binding_handle *b = cli->binding_handle;
1409         int error = 0;
1410
1411         if (argc != 3) {
1412                 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1413                 return NT_STATUS_OK;
1414         }
1415
1416         alias_rid = strtoul_err(argv[2], NULL, 10, &error);
1417         if (error != 0) {
1418                 return NT_STATUS_INVALID_PARAMETER;
1419         }
1420
1421
1422         /* Open SAMR handle */
1423
1424         status = rpccli_try_samr_connects(cli, mem_ctx,
1425                                           MAXIMUM_ALLOWED_ACCESS,
1426                                           &connect_pol);
1427         if (!NT_STATUS_IS_OK(status)) {
1428                 goto done;
1429         }
1430
1431         /* Open handle on domain */
1432
1433         status = get_domain_handle(cli, mem_ctx, argv[1],
1434                                    &connect_pol,
1435                                    MAXIMUM_ALLOWED_ACCESS,
1436                                    &domain_sid,
1437                                    &domain_pol);
1438         if (!NT_STATUS_IS_OK(status)) {
1439                 goto done;
1440         }
1441
1442         /* Open handle on alias */
1443
1444         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1445                                        &domain_pol,
1446                                        access_mask,
1447                                        alias_rid,
1448                                        &alias_pol,
1449                                        &result);
1450         if (!NT_STATUS_IS_OK(status)) {
1451                 goto done;
1452         }
1453         if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1454                 /* Probably this was a user name, try lookupnames */
1455                 struct samr_Ids rids, types;
1456                 struct lsa_String lsa_acct_name;
1457
1458                 init_lsa_String(&lsa_acct_name, argv[2]);
1459
1460                 status = dcerpc_samr_LookupNames(b, mem_ctx,
1461                                                  &domain_pol,
1462                                                  1,
1463                                                  &lsa_acct_name,
1464                                                  &rids,
1465                                                  &types,
1466                                                  &result);
1467                 if (!NT_STATUS_IS_OK(status)) {
1468                         goto done;
1469                 }
1470                 if (NT_STATUS_IS_OK(result)) {
1471                         if (rids.count != 1) {
1472                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1473                                 goto done;
1474                         }
1475                         if (types.count != 1) {
1476                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1477                                 goto done;
1478                         }
1479
1480                         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1481                                                        &domain_pol,
1482                                                        access_mask,
1483                                                        rids.ids[0],
1484                                                        &alias_pol,
1485                                                        &result);
1486                         if (!NT_STATUS_IS_OK(status)) {
1487                                 goto done;
1488                         }
1489                 }
1490         }
1491
1492         status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1493                                             &alias_pol,
1494                                             &result);
1495         if (!NT_STATUS_IS_OK(status)) {
1496                 goto done;
1497         }
1498         if (!NT_STATUS_IS_OK(result)) {
1499                 status = result;
1500                 goto done;
1501         }
1502
1503         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1504         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1505  done:
1506         return status;
1507 }
1508
1509 /* Query display info */
1510
1511 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1512                                                  TALLOC_CTX *mem_ctx,
1513                                                  int argc, const char **argv,
1514                                                  uint32_t opcode)
1515 {
1516         struct policy_handle connect_pol, domain_pol;
1517         NTSTATUS status, result;
1518         uint32_t start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1519         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1520         uint32_t info_level = 1;
1521         union samr_DispInfo info;
1522         int loop_count = 0;
1523         bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1524         uint32_t total_size, returned_size;
1525         struct dcerpc_binding_handle *b = cli->binding_handle;
1526
1527         if (argc > 6) {
1528                 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1529                 return NT_STATUS_OK;
1530         }
1531
1532         if (argc >= 2)
1533                 sscanf(argv[1], "%i", &info_level);
1534
1535         if (argc >= 3)
1536                 sscanf(argv[2], "%i", &start_idx);
1537
1538         if (argc >= 4) {
1539                 sscanf(argv[3], "%i", &max_entries);
1540                 got_params = True;
1541         }
1542
1543         if (argc >= 5) {
1544                 sscanf(argv[4], "%i", &max_size);
1545                 got_params = True;
1546         }
1547
1548         if (argc >= 6)
1549                 sscanf(argv[5], "%x", &access_mask);
1550
1551         /* Get sam policy handle */
1552
1553         status = rpccli_try_samr_connects(cli, mem_ctx,
1554                                           MAXIMUM_ALLOWED_ACCESS,
1555                                           &connect_pol);
1556         if (!NT_STATUS_IS_OK(status)) {
1557                 goto done;
1558         }
1559
1560         /* Get domain policy handle */
1561
1562         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1563                                         &connect_pol,
1564                                         access_mask,
1565                                         &domain_sid,
1566                                         &domain_pol,
1567                                         &result);
1568         if (!NT_STATUS_IS_OK(status)) {
1569                 goto done;
1570         }
1571         if (!NT_STATUS_IS_OK(result)) {
1572                 status = result;
1573                 goto done;
1574         }
1575
1576         /* Query display info */
1577
1578         do {
1579
1580                 if (!got_params)
1581                         dcerpc_get_query_dispinfo_params(
1582                                 loop_count, &max_entries, &max_size);
1583
1584                 switch (opcode) {
1585                 case NDR_SAMR_QUERYDISPLAYINFO:
1586                         status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1587                                                               &domain_pol,
1588                                                               info_level,
1589                                                               start_idx,
1590                                                               max_entries,
1591                                                               max_size,
1592                                                               &total_size,
1593                                                               &returned_size,
1594                                                               &info,
1595                                                               &result);
1596                         break;
1597                 case NDR_SAMR_QUERYDISPLAYINFO2:
1598                         status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1599                                                                &domain_pol,
1600                                                                info_level,
1601                                                                start_idx,
1602                                                                max_entries,
1603                                                                max_size,
1604                                                                &total_size,
1605                                                                &returned_size,
1606                                                                &info,
1607                                                                &result);
1608
1609                         break;
1610                 case NDR_SAMR_QUERYDISPLAYINFO3:
1611                         status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1612                                                                &domain_pol,
1613                                                                info_level,
1614                                                                start_idx,
1615                                                                max_entries,
1616                                                                max_size,
1617                                                                &total_size,
1618                                                                &returned_size,
1619                                                                &info,
1620                                                                &result);
1621
1622                         break;
1623                 default:
1624                         return NT_STATUS_INVALID_PARAMETER;
1625                 }
1626
1627                 if (!NT_STATUS_IS_OK(status)) {
1628                         break;
1629                 }
1630                 status = result;
1631                 if (!NT_STATUS_IS_OK(result) &&
1632                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1633                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1634                         break;
1635                 }
1636
1637                 loop_count++;
1638
1639                 switch (info_level) {
1640                         case 1:
1641                                 num_entries = info.info1.count;
1642                                 break;
1643                         case 2:
1644                                 num_entries = info.info2.count;
1645                                 break;
1646                         case 3:
1647                                 num_entries = info.info3.count;
1648                                 break;
1649                         case 4:
1650                                 num_entries = info.info4.count;
1651                                 break;
1652                         case 5:
1653                                 num_entries = info.info5.count;
1654                                 break;
1655                         default:
1656                                 break;
1657                 }
1658
1659                 start_idx += num_entries;
1660
1661                 if (num_entries == 0)
1662                         break;
1663
1664                 for (i = 0; i < num_entries; i++) {
1665                         switch (info_level) {
1666                         case 1:
1667                                 display_sam_info_1(&info.info1.entries[i]);
1668                                 break;
1669                         case 2:
1670                                 display_sam_info_2(&info.info2.entries[i]);
1671                                 break;
1672                         case 3:
1673                                 display_sam_info_3(&info.info3.entries[i]);
1674                                 break;
1675                         case 4:
1676                                 display_sam_info_4(&info.info4.entries[i]);
1677                                 break;
1678                         case 5:
1679                                 display_sam_info_5(&info.info5.entries[i]);
1680                                 break;
1681                         }
1682                 }
1683         } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1684
1685         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1686         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1687  done:
1688         return status;
1689 }
1690
1691 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1692                                         TALLOC_CTX *mem_ctx,
1693                                         int argc, const char **argv)
1694 {
1695         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1696                                                 NDR_SAMR_QUERYDISPLAYINFO);
1697 }
1698
1699 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1700                                          TALLOC_CTX *mem_ctx,
1701                                          int argc, const char **argv)
1702 {
1703         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1704                                                 NDR_SAMR_QUERYDISPLAYINFO2);
1705 }
1706
1707 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1708                                          TALLOC_CTX *mem_ctx,
1709                                          int argc, const char **argv)
1710 {
1711         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1712                                                 NDR_SAMR_QUERYDISPLAYINFO3);
1713 }
1714
1715 /* Query domain info */
1716
1717 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1718                                        TALLOC_CTX *mem_ctx,
1719                                        int argc, const char **argv)
1720 {
1721         struct policy_handle connect_pol, domain_pol;
1722         NTSTATUS status, result;
1723         uint32_t switch_level = 2;
1724         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1725         union samr_DomainInfo *info = NULL;
1726         struct dcerpc_binding_handle *b = cli->binding_handle;
1727
1728         if (argc > 3) {
1729                 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1730                 return NT_STATUS_OK;
1731         }
1732
1733         if (argc > 1)
1734                 sscanf(argv[1], "%i", &switch_level);
1735
1736         if (argc > 2)
1737                 sscanf(argv[2], "%x", &access_mask);
1738
1739         /* Get sam policy handle */
1740
1741         status = rpccli_try_samr_connects(cli, mem_ctx,
1742                                           MAXIMUM_ALLOWED_ACCESS,
1743                                           &connect_pol);
1744         if (!NT_STATUS_IS_OK(status)) {
1745                 goto done;
1746         }
1747
1748         /* Get domain policy handle */
1749
1750         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1751                                         &connect_pol,
1752                                         access_mask,
1753                                         &domain_sid,
1754                                         &domain_pol,
1755                                         &result);
1756         if (!NT_STATUS_IS_OK(status)) {
1757                 goto done;
1758         }
1759         if (!NT_STATUS_IS_OK(result)) {
1760                 status = result;
1761                 goto done;
1762         }
1763
1764         /* Query domain info */
1765
1766         status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1767                                              &domain_pol,
1768                                              switch_level,
1769                                              &info,
1770                                              &result);
1771         if (!NT_STATUS_IS_OK(status)) {
1772                 goto done;
1773         }
1774         if (!NT_STATUS_IS_OK(result)) {
1775                 status = result;
1776                 goto done;
1777         }
1778
1779         /* Display domain info */
1780
1781         switch (switch_level) {
1782         case 1:
1783                 display_sam_dom_info_1(&info->info1);
1784                 break;
1785         case 2:
1786                 display_sam_dom_info_2(&info->general);
1787                 break;
1788         case 3:
1789                 display_sam_dom_info_3(&info->info3);
1790                 break;
1791         case 4:
1792                 display_sam_dom_info_4(&info->oem);
1793                 break;
1794         case 5:
1795                 display_sam_dom_info_5(&info->info5);
1796                 break;
1797         case 6:
1798                 display_sam_dom_info_6(&info->info6);
1799                 break;
1800         case 7:
1801                 display_sam_dom_info_7(&info->info7);
1802                 break;
1803         case 8:
1804                 display_sam_dom_info_8(&info->info8);
1805                 break;
1806         case 9:
1807                 display_sam_dom_info_9(&info->info9);
1808                 break;
1809         case 12:
1810                 display_sam_dom_info_12(&info->info12);
1811                 break;
1812         case 13:
1813                 display_sam_dom_info_13(&info->info13);
1814                 break;
1815
1816         default:
1817                 printf("cannot display domain info for switch value %d\n",
1818                        switch_level);
1819                 break;
1820         }
1821
1822  done:
1823
1824         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1825         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1826         return status;
1827 }
1828
1829 /* Create domain user */
1830
1831 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1832                                          TALLOC_CTX *mem_ctx,
1833                                          int argc, const char **argv)
1834 {
1835         struct policy_handle connect_pol, domain_pol, user_pol;
1836         NTSTATUS status, result;
1837         struct lsa_String acct_name;
1838         uint32_t acb_info;
1839         uint32_t acct_flags, user_rid;
1840         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1841         uint32_t access_granted = 0;
1842         struct dcerpc_binding_handle *b = cli->binding_handle;
1843
1844         if ((argc < 2) || (argc > 3)) {
1845                 printf("Usage: %s username [access mask]\n", argv[0]);
1846                 return NT_STATUS_OK;
1847         }
1848
1849         init_lsa_String(&acct_name, argv[1]);
1850
1851         if (argc > 2)
1852                 sscanf(argv[2], "%x", &access_mask);
1853
1854         /* Get sam policy handle */
1855
1856         status = rpccli_try_samr_connects(cli, mem_ctx,
1857                                           MAXIMUM_ALLOWED_ACCESS,
1858                                           &connect_pol);
1859         if (!NT_STATUS_IS_OK(status)) {
1860                 goto done;
1861         }
1862
1863         /* Get domain policy handle */
1864
1865         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1866                                         &connect_pol,
1867                                         access_mask,
1868                                         &domain_sid,
1869                                         &domain_pol,
1870                                         &result);
1871         if (!NT_STATUS_IS_OK(status)) {
1872                 goto done;
1873         }
1874         if (!NT_STATUS_IS_OK(result)) {
1875                 status = result;
1876                 goto done;
1877         }
1878
1879         /* Create domain user */
1880
1881         acb_info = ACB_NORMAL;
1882         acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1883                      SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1884                      SAMR_USER_ACCESS_SET_PASSWORD |
1885                      SAMR_USER_ACCESS_GET_ATTRIBUTES |
1886                      SAMR_USER_ACCESS_SET_ATTRIBUTES;
1887
1888         status = dcerpc_samr_CreateUser2(b, mem_ctx,
1889                                          &domain_pol,
1890                                          &acct_name,
1891                                          acb_info,
1892                                          acct_flags,
1893                                          &user_pol,
1894                                          &access_granted,
1895                                          &user_rid,
1896                                          &result);
1897         if (!NT_STATUS_IS_OK(status)) {
1898                 goto done;
1899         }
1900         if (!NT_STATUS_IS_OK(result)) {
1901                 status = result;
1902                 goto done;
1903         }
1904
1905         status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1906         if (!NT_STATUS_IS_OK(status)) goto done;
1907
1908         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1909         if (!NT_STATUS_IS_OK(status)) goto done;
1910
1911         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1912         if (!NT_STATUS_IS_OK(status)) goto done;
1913
1914  done:
1915         return status;
1916 }
1917
1918 /* Create domain group */
1919
1920 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1921                                           TALLOC_CTX *mem_ctx,
1922                                           int argc, const char **argv)
1923 {
1924         struct policy_handle connect_pol, domain_pol, group_pol;
1925         NTSTATUS status, result;
1926         struct lsa_String grp_name;
1927         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1928         uint32_t rid = 0;
1929         struct dcerpc_binding_handle *b = cli->binding_handle;
1930
1931         if ((argc < 2) || (argc > 3)) {
1932                 printf("Usage: %s groupname [access mask]\n", argv[0]);
1933                 return NT_STATUS_OK;
1934         }
1935
1936         init_lsa_String(&grp_name, argv[1]);
1937
1938         if (argc > 2)
1939                 sscanf(argv[2], "%x", &access_mask);
1940
1941         /* Get sam policy handle */
1942
1943         status = rpccli_try_samr_connects(cli, mem_ctx,
1944                                           MAXIMUM_ALLOWED_ACCESS,
1945                                           &connect_pol);
1946         if (!NT_STATUS_IS_OK(status)) {
1947                 goto done;
1948         }
1949
1950         /* Get domain policy handle */
1951
1952         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1953                                         &connect_pol,
1954                                         access_mask,
1955                                         &domain_sid,
1956                                         &domain_pol,
1957                                         &result);
1958         if (!NT_STATUS_IS_OK(status)) {
1959                 goto done;
1960         }
1961         if (!NT_STATUS_IS_OK(result)) {
1962                 status = result;
1963                 goto done;
1964         }
1965
1966         /* Create domain user */
1967         status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1968                                                &domain_pol,
1969                                                &grp_name,
1970                                                MAXIMUM_ALLOWED_ACCESS,
1971                                                &group_pol,
1972                                                &rid,
1973                                                &result);
1974         if (!NT_STATUS_IS_OK(status)) {
1975                 goto done;
1976         }
1977         if (!NT_STATUS_IS_OK(result)) {
1978                 status = result;
1979                 goto done;
1980         }
1981
1982         status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1983         if (!NT_STATUS_IS_OK(status)) goto done;
1984
1985         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1986         if (!NT_STATUS_IS_OK(status)) goto done;
1987
1988         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1989         if (!NT_STATUS_IS_OK(status)) goto done;
1990
1991  done:
1992         return status;
1993 }
1994
1995 /* Create domain alias */
1996
1997 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1998                                           TALLOC_CTX *mem_ctx,
1999                                           int argc, const char **argv)
2000 {
2001         struct policy_handle connect_pol, domain_pol, alias_pol;
2002         NTSTATUS status, result;
2003         struct lsa_String alias_name;
2004         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2005         uint32_t rid = 0;
2006         struct dcerpc_binding_handle *b = cli->binding_handle;
2007
2008         if ((argc < 2) || (argc > 3)) {
2009                 printf("Usage: %s aliasname [access mask]\n", argv[0]);
2010                 return NT_STATUS_OK;
2011         }
2012
2013         init_lsa_String(&alias_name, argv[1]);
2014
2015         if (argc > 2)
2016                 sscanf(argv[2], "%x", &access_mask);
2017
2018         /* Get sam policy handle */
2019
2020         status = rpccli_try_samr_connects(cli, mem_ctx,
2021                                           MAXIMUM_ALLOWED_ACCESS,
2022                                           &connect_pol);
2023         if (!NT_STATUS_IS_OK(status)) {
2024                 goto done;
2025         }
2026
2027         /* Get domain policy handle */
2028
2029         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2030                                         &connect_pol,
2031                                         access_mask,
2032                                         &domain_sid,
2033                                         &domain_pol,
2034                                         &result);
2035         if (!NT_STATUS_IS_OK(status)) {
2036                 goto done;
2037         }
2038         if (!NT_STATUS_IS_OK(result)) {
2039                 status = result;
2040                 goto done;
2041         }
2042
2043         /* Create domain user */
2044
2045         status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2046                                             &domain_pol,
2047                                             &alias_name,
2048                                             MAXIMUM_ALLOWED_ACCESS,
2049                                             &alias_pol,
2050                                             &rid,
2051                                             &result);
2052         if (!NT_STATUS_IS_OK(status)) {
2053                 goto done;
2054         }
2055         if (!NT_STATUS_IS_OK(result)) {
2056                 status = result;
2057                 goto done;
2058         }
2059
2060
2061         status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2062         if (!NT_STATUS_IS_OK(status)) goto done;
2063
2064         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2065         if (!NT_STATUS_IS_OK(status)) goto done;
2066
2067         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2068         if (!NT_STATUS_IS_OK(status)) goto done;
2069
2070  done:
2071         return status;
2072 }
2073
2074 /* Lookup sam names */
2075
2076 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2077                                       TALLOC_CTX *mem_ctx,
2078                                       int argc, const char **argv)
2079 {
2080         NTSTATUS status, result;
2081         struct policy_handle connect_pol, domain_pol;
2082         uint32_t num_names;
2083         struct samr_Ids rids, name_types;
2084         int i;
2085         struct lsa_String *names = NULL;
2086         struct dcerpc_binding_handle *b = cli->binding_handle;
2087
2088         if (argc < 3) {
2089                 printf("Usage: %s  domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2090                 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2091                 printf("or check on the builtin SID: S-1-5-32\n");
2092                 return NT_STATUS_OK;
2093         }
2094
2095         /* Get sam policy and domain handles */
2096
2097         status = rpccli_try_samr_connects(cli, mem_ctx,
2098                                           MAXIMUM_ALLOWED_ACCESS,
2099                                           &connect_pol);
2100         if (!NT_STATUS_IS_OK(status)) {
2101                 goto done;
2102         }
2103
2104         status = get_domain_handle(cli, mem_ctx, argv[1],
2105                                    &connect_pol,
2106                                    MAXIMUM_ALLOWED_ACCESS,
2107                                    &domain_sid,
2108                                    &domain_pol);
2109         if (!NT_STATUS_IS_OK(status)) {
2110                 goto done;
2111         }
2112
2113         /* Look up names */
2114
2115         num_names = argc - 2;
2116
2117         if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2118                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2119                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2120                 status = NT_STATUS_NO_MEMORY;
2121                 goto done;
2122         }
2123
2124         for (i = 0; i < num_names; i++) {
2125                 init_lsa_String(&names[i], argv[i + 2]);
2126         }
2127
2128         status = dcerpc_samr_LookupNames(b, mem_ctx,
2129                                          &domain_pol,
2130                                          num_names,
2131                                          names,
2132                                          &rids,
2133                                          &name_types,
2134                                          &result);
2135         if (!NT_STATUS_IS_OK(status)) {
2136                 goto done;
2137         }
2138         if (!NT_STATUS_IS_OK(result)) {
2139                 status = result;
2140                 goto done;
2141         }
2142         if (rids.count != num_names) {
2143                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2144                 goto done;
2145         }
2146         if (name_types.count != num_names) {
2147                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2148                 goto done;
2149         }
2150
2151         /* Display results */
2152
2153         for (i = 0; i < num_names; i++)
2154                 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2155                        name_types.ids[i]);
2156
2157         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2158         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2159  done:
2160         return status;
2161 }
2162
2163 /* Lookup sam rids */
2164
2165 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2166                                      TALLOC_CTX *mem_ctx,
2167                                      int argc, const char **argv)
2168 {
2169         NTSTATUS status, result;
2170         struct policy_handle connect_pol, domain_pol;
2171         uint32_t num_rids, *rids;
2172         struct lsa_Strings names;
2173         struct samr_Ids types;
2174         struct dcerpc_binding_handle *b = cli->binding_handle;
2175
2176         int i;
2177
2178         if (argc < 3) {
2179                 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2180                 return NT_STATUS_OK;
2181         }
2182
2183         /* Get sam policy and domain handles */
2184
2185         status = rpccli_try_samr_connects(cli, mem_ctx,
2186                                           MAXIMUM_ALLOWED_ACCESS,
2187                                           &connect_pol);
2188         if (!NT_STATUS_IS_OK(status)) {
2189                 goto done;
2190         }
2191
2192         status = get_domain_handle(cli, mem_ctx, argv[1],
2193                                    &connect_pol,
2194                                    MAXIMUM_ALLOWED_ACCESS,
2195                                    &domain_sid,
2196                                    &domain_pol);
2197         if (!NT_STATUS_IS_OK(status)) {
2198                 goto done;
2199         }
2200
2201         /* Look up rids */
2202
2203         num_rids = argc - 2;
2204
2205         if ((rids = talloc_array(mem_ctx, uint32_t, num_rids)) == NULL) {
2206                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2207                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2208                 status = NT_STATUS_NO_MEMORY;
2209                 goto done;
2210         }
2211
2212         for (i = 0; i < argc - 2; i++)
2213                 sscanf(argv[i + 2], "%i", &rids[i]);
2214
2215         status = dcerpc_samr_LookupRids(b, mem_ctx,
2216                                         &domain_pol,
2217                                         num_rids,
2218                                         rids,
2219                                         &names,
2220                                         &types,
2221                                         &result);
2222         if (!NT_STATUS_IS_OK(status)) {
2223                 goto done;
2224         }
2225         status = result;
2226         if (!NT_STATUS_IS_OK(result) &&
2227             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2228                 goto done;
2229
2230         /* Display results */
2231         if (num_rids != names.count) {
2232                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2233                 goto done;
2234         }
2235         if (num_rids != types.count) {
2236                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2237                 goto done;
2238         }
2239
2240         for (i = 0; i < num_rids; i++) {
2241                 printf("rid 0x%x: %s (%d)\n",
2242                         rids[i], names.names[i].string, types.ids[i]);
2243         }
2244
2245         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2246         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2247  done:
2248         return status;
2249 }
2250
2251 /* Delete domain group */
2252
2253 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2254                                          TALLOC_CTX *mem_ctx,
2255                                          int argc, const char **argv)
2256 {
2257         NTSTATUS status, result;
2258         struct policy_handle connect_pol, domain_pol, group_pol;
2259         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2260         struct dcerpc_binding_handle *b = cli->binding_handle;
2261
2262         if ((argc < 2) || (argc > 3)) {
2263                 printf("Usage: %s groupname\n", argv[0]);
2264                 return NT_STATUS_OK;
2265         }
2266
2267         if (argc > 2)
2268                 sscanf(argv[2], "%x", &access_mask);
2269
2270         /* Get sam policy and domain handles */
2271
2272         status = rpccli_try_samr_connects(cli, mem_ctx,
2273                                           MAXIMUM_ALLOWED_ACCESS,
2274                                           &connect_pol);
2275         if (!NT_STATUS_IS_OK(status)) {
2276                 goto done;
2277         }
2278
2279         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2280                                         &connect_pol,
2281                                         MAXIMUM_ALLOWED_ACCESS,
2282                                         &domain_sid,
2283                                         &domain_pol,
2284                                         &result);
2285         if (!NT_STATUS_IS_OK(status)) {
2286                 goto done;
2287         }
2288         if (!NT_STATUS_IS_OK(result)) {
2289                 status = result;
2290                 goto done;
2291         }
2292
2293         /* Get handle on group */
2294
2295         {
2296                 struct samr_Ids group_rids, name_types;
2297                 struct lsa_String lsa_acct_name;
2298
2299                 init_lsa_String(&lsa_acct_name, argv[1]);
2300
2301                 status = dcerpc_samr_LookupNames(b, mem_ctx,
2302                                                  &domain_pol,
2303                                                  1,
2304                                                  &lsa_acct_name,
2305                                                  &group_rids,
2306                                                  &name_types,
2307                                                  &result);
2308                 if (!NT_STATUS_IS_OK(status)) {
2309                         goto done;
2310                 }
2311                 if (!NT_STATUS_IS_OK(result)) {
2312                         status = result;
2313                         goto done;
2314                 }
2315                 if (group_rids.count != 1) {
2316                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2317                         goto done;
2318                 }
2319                 if (name_types.count != 1) {
2320                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2321                         goto done;
2322                 }
2323
2324                 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2325                                                &domain_pol,
2326                                                access_mask,
2327                                                group_rids.ids[0],
2328                                                &group_pol,
2329                                                &result);
2330                 if (!NT_STATUS_IS_OK(status)) {
2331                         goto done;
2332                 }
2333                 if (!NT_STATUS_IS_OK(result)) {
2334                         status = result;
2335                         goto done;
2336                 }
2337         }
2338
2339         /* Delete group */
2340
2341         status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2342                                                &group_pol,
2343                                                &result);
2344         if (!NT_STATUS_IS_OK(status)) {
2345                 goto done;
2346         }
2347         if (!NT_STATUS_IS_OK(result)) {
2348                 status = result;
2349                 goto done;
2350         }
2351
2352         /* Display results */
2353
2354         dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2355         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2356         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2357
2358  done:
2359         return status;
2360 }
2361
2362 /* Delete domain user */
2363
2364 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2365                                          TALLOC_CTX *mem_ctx,
2366                                          int argc, const char **argv)
2367 {
2368         NTSTATUS status, result;
2369         struct policy_handle connect_pol, domain_pol, user_pol;
2370         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2371         struct dcerpc_binding_handle *b = cli->binding_handle;
2372
2373         if ((argc < 2) || (argc > 3)) {
2374                 printf("Usage: %s username\n", argv[0]);
2375                 return NT_STATUS_OK;
2376         }
2377
2378         if (argc > 2)
2379                 sscanf(argv[2], "%x", &access_mask);
2380
2381         /* Get sam policy and domain handles */
2382
2383         status = rpccli_try_samr_connects(cli, mem_ctx,
2384                                           MAXIMUM_ALLOWED_ACCESS,
2385                                           &connect_pol);
2386         if (!NT_STATUS_IS_OK(status)) {
2387                 goto done;
2388         }
2389
2390         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2391                                         &connect_pol,
2392                                         MAXIMUM_ALLOWED_ACCESS,
2393                                         &domain_sid,
2394                                         &domain_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         /* Get handle on user */
2405
2406         {
2407                 struct samr_Ids user_rids, name_types;
2408                 struct lsa_String lsa_acct_name;
2409
2410                 init_lsa_String(&lsa_acct_name, argv[1]);
2411
2412                 status = dcerpc_samr_LookupNames(b, mem_ctx,
2413                                                  &domain_pol,
2414                                                  1,
2415                                                  &lsa_acct_name,
2416                                                  &user_rids,
2417                                                  &name_types,
2418                                                  &result);
2419                 if (!NT_STATUS_IS_OK(status)) {
2420                         goto done;
2421                 }
2422                 if (!NT_STATUS_IS_OK(result)) {
2423                         status = result;
2424                         goto done;
2425                 }
2426                 if (user_rids.count != 1) {
2427                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2428                         goto done;
2429                 }
2430                 if (name_types.count != 1) {
2431                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2432                         goto done;
2433                 }
2434
2435                 status = dcerpc_samr_OpenUser(b, mem_ctx,
2436                                               &domain_pol,
2437                                               access_mask,
2438                                               user_rids.ids[0],
2439                                               &user_pol,
2440                                               &result);
2441                 if (!NT_STATUS_IS_OK(status)) {
2442                         goto done;
2443                 }
2444                 if (!NT_STATUS_IS_OK(result)) {
2445                         status = result;
2446                         goto done;
2447                 }
2448         }
2449
2450         /* Delete user */
2451
2452         status = dcerpc_samr_DeleteUser(b, mem_ctx,
2453                                         &user_pol,
2454                                         &result);
2455         if (!NT_STATUS_IS_OK(status)) {
2456                 goto done;
2457         }
2458         if (!NT_STATUS_IS_OK(result)) {
2459                 status = result;
2460                 goto done;
2461         }
2462
2463         /* Display results */
2464
2465         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2466         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2467         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2468
2469  done:
2470         return status;
2471 }
2472
2473 /**********************************************************************
2474  * Query user security object
2475  */
2476 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2477                                     TALLOC_CTX *mem_ctx,
2478                                     int argc, const char **argv)
2479 {
2480         struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2481         NTSTATUS status, result;
2482         uint32_t sec_info = SECINFO_DACL;
2483         uint32_t user_rid = 0;
2484         TALLOC_CTX *ctx = NULL;
2485         struct sec_desc_buf *sec_desc_buf=NULL;
2486         bool domain = False;
2487         struct dcerpc_binding_handle *b = cli->binding_handle;
2488
2489         ctx=talloc_init("cmd_samr_query_sec_obj");
2490
2491         if ((argc < 1) || (argc > 3)) {
2492                 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2493                 printf("\tSpecify rid for security on user, -d for security on domain\n");
2494                 talloc_destroy(ctx);
2495                 return NT_STATUS_OK;
2496         }
2497
2498         if (argc > 1) {
2499                 if (strcmp(argv[1], "-d") == 0)
2500                         domain = True;
2501                 else
2502                         sscanf(argv[1], "%i", &user_rid);
2503         }
2504
2505         if (argc == 3) {
2506                 sec_info = atoi(argv[2]);
2507         }
2508
2509         status = rpccli_try_samr_connects(cli, mem_ctx,
2510                                           MAXIMUM_ALLOWED_ACCESS,
2511                                           &connect_pol);
2512         if (!NT_STATUS_IS_OK(status)) {
2513                 goto done;
2514         }
2515
2516         if (domain || user_rid) {
2517                 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2518                                                 &connect_pol,
2519                                                 MAXIMUM_ALLOWED_ACCESS,
2520                                                 &domain_sid,
2521                                                 &domain_pol,
2522                                                 &result);
2523                 if (!NT_STATUS_IS_OK(status)) {
2524                         goto done;
2525                 }
2526                 if (!NT_STATUS_IS_OK(result)) {
2527                         status = result;
2528                         goto done;
2529                 }
2530         }
2531
2532         if (user_rid) {
2533                 status = dcerpc_samr_OpenUser(b, mem_ctx,
2534                                               &domain_pol,
2535                                               MAXIMUM_ALLOWED_ACCESS,
2536                                               user_rid,
2537                                               &user_pol,
2538                                               &result);
2539                 if (!NT_STATUS_IS_OK(status)) {
2540                         goto done;
2541                 }
2542                 if (!NT_STATUS_IS_OK(result)) {
2543                         status = result;
2544                         goto done;
2545                 }
2546         }
2547
2548         /* Pick which query pol to use */
2549
2550         pol = &connect_pol;
2551
2552         if (domain)
2553                 pol = &domain_pol;
2554
2555         if (user_rid)
2556                 pol = &user_pol;
2557
2558         /* Query SAM security object */
2559
2560         status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2561                                            pol,
2562                                            sec_info,
2563                                            &sec_desc_buf,
2564                                            &result);
2565         if (!NT_STATUS_IS_OK(status)) {
2566                 goto done;
2567         }
2568         if (!NT_STATUS_IS_OK(result)) {
2569                 status = result;
2570                 goto done;
2571         }
2572
2573         display_sec_desc(sec_desc_buf->sd);
2574
2575         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2576         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2577         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2578 done:
2579         talloc_destroy(ctx);
2580         return status;
2581 }
2582
2583 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2584                                            TALLOC_CTX *mem_ctx,
2585                                            int argc, const char **argv)
2586 {
2587         NTSTATUS status, result;
2588         struct policy_handle connect_pol, domain_pol, user_pol;
2589         struct samr_PwInfo info;
2590         uint32_t rid;
2591         struct dcerpc_binding_handle *b = cli->binding_handle;
2592
2593         if (argc != 2) {
2594                 printf("Usage: %s rid\n", argv[0]);
2595                 return NT_STATUS_OK;
2596         }
2597
2598         sscanf(argv[1], "%i", &rid);
2599
2600         status = rpccli_try_samr_connects(cli, mem_ctx,
2601                                           MAXIMUM_ALLOWED_ACCESS,
2602                                           &connect_pol);
2603         if (!NT_STATUS_IS_OK(status)) {
2604                 goto done;
2605         }
2606
2607         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2608                                         &connect_pol,
2609                                         MAXIMUM_ALLOWED_ACCESS,
2610                                         &domain_sid,
2611                                         &domain_pol,
2612                                         &result);
2613         if (!NT_STATUS_IS_OK(status)) {
2614                 goto done;
2615         }
2616         if (!NT_STATUS_IS_OK(result)) {
2617                 status = result;
2618                 goto done;
2619         }
2620
2621         status = dcerpc_samr_OpenUser(b, mem_ctx,
2622                                       &domain_pol,
2623                                       MAXIMUM_ALLOWED_ACCESS,
2624                                       rid,
2625                                       &user_pol,
2626                                       &result);
2627         if (!NT_STATUS_IS_OK(status)) {
2628                 goto done;
2629         }
2630         if (!NT_STATUS_IS_OK(result)) {
2631                 status = result;
2632                 goto done;
2633         }
2634
2635         status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2636                                            &user_pol,
2637                                            &info,
2638                                            &result);
2639         if (!NT_STATUS_IS_OK(status)) {
2640                 goto done;
2641         }
2642         status = result;
2643         if (NT_STATUS_IS_OK(result)) {
2644                 printf("min_password_length: %d\n", info.min_password_length);
2645                 printf("%s\n",
2646                         NDR_PRINT_STRUCT_STRING(mem_ctx,
2647                                 samr_PasswordProperties, &info.password_properties));
2648         }
2649
2650  done:
2651         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2652         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2653         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2654
2655         return status;
2656 }
2657
2658 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2659                                         TALLOC_CTX *mem_ctx,
2660                                         int argc, const char **argv)
2661 {
2662         NTSTATUS status, result;
2663         struct lsa_String domain_name;
2664         struct samr_PwInfo info;
2665         struct dcerpc_binding_handle *b = cli->binding_handle;
2666
2667         if (argc < 1 || argc > 3) {
2668                 printf("Usage: %s <domain>\n", argv[0]);
2669                 return NT_STATUS_OK;
2670         }
2671
2672         init_lsa_String(&domain_name, argv[1]);
2673
2674         status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2675                                           &domain_name,
2676                                           &info,
2677                                           &result);
2678         if (!NT_STATUS_IS_OK(status)) {
2679                 return status;
2680         }
2681         if (NT_STATUS_IS_OK(result)) {
2682                 printf("min_password_length: %d\n", info.min_password_length);
2683                 display_password_properties(info.password_properties);
2684         }
2685
2686         return result;
2687 }
2688
2689 /* Look up domain name */
2690
2691 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2692                                        TALLOC_CTX *mem_ctx,
2693                                        int argc, const char **argv)
2694 {
2695         struct policy_handle connect_pol, domain_pol;
2696         NTSTATUS status, result;
2697         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2698         struct lsa_String domain_name;
2699         struct dom_sid *sid = NULL;
2700         struct dcerpc_binding_handle *b = cli->binding_handle;
2701
2702         if (argc != 2) {
2703                 printf("Usage: %s domain_name\n", argv[0]);
2704                 return NT_STATUS_OK;
2705         }
2706
2707         init_lsa_String(&domain_name, argv[1]);
2708
2709         status = rpccli_try_samr_connects(cli, mem_ctx,
2710                                           access_mask,
2711                                           &connect_pol);
2712         if (!NT_STATUS_IS_OK(status)) {
2713                 goto done;
2714         }
2715
2716         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2717                                         &connect_pol,
2718                                         access_mask,
2719                                         &domain_sid,
2720                                         &domain_pol,
2721                                         &result);
2722         if (!NT_STATUS_IS_OK(status)) {
2723                 goto done;
2724         }
2725         if (!NT_STATUS_IS_OK(result)) {
2726                 status = result;
2727                 goto done;
2728         }
2729
2730         status = dcerpc_samr_LookupDomain(b, mem_ctx,
2731                                           &connect_pol,
2732                                           &domain_name,
2733                                           &sid,
2734                                           &result);
2735         if (!NT_STATUS_IS_OK(status)) {
2736                 goto done;
2737         }
2738         if (!NT_STATUS_IS_OK(result)) {
2739                 status = result;
2740                 goto done;
2741         }
2742
2743         if (NT_STATUS_IS_OK(result)) {
2744                 struct dom_sid_buf sid_str;
2745                 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2746                        argv[1],
2747                        dom_sid_str_buf(sid, &sid_str));
2748         }
2749
2750         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2751         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2752 done:
2753         return status;
2754 }
2755
2756 /* Change user password */
2757
2758 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2759                                    TALLOC_CTX *mem_ctx,
2760                                    int argc, const char **argv)
2761 {
2762         struct policy_handle connect_pol;
2763         struct policy_handle domain_pol = { 0, };
2764         struct policy_handle user_pol = { 0, };
2765         NTSTATUS status, result;
2766         const char *user, *oldpass, *newpass;
2767         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2768         struct samr_Ids rids, types;
2769         struct lsa_String lsa_acct_name;
2770         struct dcerpc_binding_handle *b = cli->binding_handle;
2771
2772         if (argc < 3) {
2773                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2774                 return NT_STATUS_INVALID_PARAMETER;
2775         }
2776
2777         user = argv[1];
2778         oldpass = argv[2];
2779         newpass = argv[3];
2780
2781         /* Get sam policy handle */
2782
2783         status = rpccli_try_samr_connects(cli, mem_ctx,
2784                                           MAXIMUM_ALLOWED_ACCESS,
2785                                           &connect_pol);
2786         if (!NT_STATUS_IS_OK(status)) {
2787                 goto done;
2788         }
2789
2790         /* Get domain policy handle */
2791
2792         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2793                                         &connect_pol,
2794                                         access_mask,
2795                                         &domain_sid,
2796                                         &domain_pol,
2797                                         &result);
2798         if (!NT_STATUS_IS_OK(status)) {
2799                 goto done;
2800         }
2801         if (!NT_STATUS_IS_OK(result)) {
2802                 status = result;
2803                 goto done;
2804         }
2805
2806         init_lsa_String(&lsa_acct_name, user);
2807
2808         status = dcerpc_samr_LookupNames(b, mem_ctx,
2809                                          &domain_pol,
2810                                          1,
2811                                          &lsa_acct_name,
2812                                          &rids,
2813                                          &types,
2814                                          &result);
2815         if (!NT_STATUS_IS_OK(status)) {
2816                 goto done;
2817         }
2818         if (!NT_STATUS_IS_OK(result)) {
2819                 status = result;
2820                 goto done;
2821         }
2822         if (rids.count != 1) {
2823                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2824                 goto done;
2825         }
2826         if (types.count != 1) {
2827                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2828                 goto done;
2829         }
2830
2831         status = dcerpc_samr_OpenUser(b, mem_ctx,
2832                                       &domain_pol,
2833                                       access_mask,
2834                                       rids.ids[0],
2835                                       &user_pol,
2836                                       &result);
2837         if (!NT_STATUS_IS_OK(status)) {
2838                 goto done;
2839         }
2840         if (!NT_STATUS_IS_OK(result)) {
2841                 status = result;
2842                 goto done;
2843         }
2844
2845         /* Change user password */
2846         status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2847                                             &user_pol,
2848                                             newpass,
2849                                             oldpass);
2850         if (!NT_STATUS_IS_OK(status)) {
2851                 goto done;
2852         }
2853
2854  done:
2855         if (is_valid_policy_hnd(&user_pol)) {
2856                 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2857         }
2858         if (is_valid_policy_hnd(&domain_pol)) {
2859                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2860         }
2861         if (is_valid_policy_hnd(&connect_pol)) {
2862                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2863         }
2864
2865         return status;
2866 }
2867
2868
2869 /* Change user password */
2870
2871 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2872                                     TALLOC_CTX *mem_ctx,
2873                                     int argc, const char **argv)
2874 {
2875         struct policy_handle connect_pol, domain_pol;
2876         NTSTATUS status, result;
2877         const char *user, *oldpass, *newpass;
2878         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2879         struct dcerpc_binding_handle *b = cli->binding_handle;
2880
2881         if (argc < 3) {
2882                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2883                 return NT_STATUS_INVALID_PARAMETER;
2884         }
2885
2886         user = argv[1];
2887         oldpass = argv[2];
2888         newpass = argv[3];
2889
2890         /* Get sam policy handle */
2891
2892         status = rpccli_try_samr_connects(cli, mem_ctx,
2893                                           MAXIMUM_ALLOWED_ACCESS,
2894                                           &connect_pol);
2895         if (!NT_STATUS_IS_OK(status)) {
2896                 goto done;
2897         }
2898
2899         /* Get domain policy handle */
2900
2901         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2902                                         &connect_pol,
2903                                         access_mask,
2904                                         &domain_sid,
2905                                         &domain_pol,
2906                                         &result);
2907         if (!NT_STATUS_IS_OK(status)) {
2908                 goto done;
2909         }
2910         if (!NT_STATUS_IS_OK(result)) {
2911                 status = result;
2912                 goto done;
2913         }
2914
2915         /* Change user password */
2916         status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2917
2918         if (!NT_STATUS_IS_OK(status)) {
2919                 goto done;
2920         }
2921
2922         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2923         if (!NT_STATUS_IS_OK(status)) goto done;
2924
2925         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2926         if (!NT_STATUS_IS_OK(status)) goto done;
2927
2928  done:
2929         return status;
2930 }
2931
2932
2933 /* Change user password */
2934
2935 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2936                                     TALLOC_CTX *mem_ctx,
2937                                     int argc, const char **argv)
2938 {
2939         struct policy_handle connect_pol, domain_pol;
2940         NTSTATUS status, result;
2941         const char *user, *oldpass, *newpass;
2942         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2943         struct samr_DomInfo1 *info = NULL;
2944         struct userPwdChangeFailureInformation *reject = NULL;
2945         struct dcerpc_binding_handle *b = cli->binding_handle;
2946
2947         if (argc < 3) {
2948                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2949                 return NT_STATUS_INVALID_PARAMETER;
2950         }
2951
2952         user = argv[1];
2953         oldpass = argv[2];
2954         newpass = argv[3];
2955
2956         /* Get sam policy handle */
2957
2958         status = rpccli_try_samr_connects(cli, mem_ctx,
2959                                           MAXIMUM_ALLOWED_ACCESS,
2960                                           &connect_pol);
2961         if (!NT_STATUS_IS_OK(status)) {
2962                 goto done;
2963         }
2964
2965         /* Get domain policy handle */
2966
2967         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2968                                         &connect_pol,
2969                                         access_mask,
2970                                         &domain_sid,
2971                                         &domain_pol,
2972                                         &result);
2973         if (!NT_STATUS_IS_OK(status)) {
2974                 goto done;
2975         }
2976         if (!NT_STATUS_IS_OK(result)) {
2977                 status = result;
2978                 goto done;
2979         }
2980
2981         /* Change user password */
2982         status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2983                                              user,
2984                                              newpass,
2985                                              oldpass,
2986                                              &info,
2987                                              &reject);
2988         if (!NT_STATUS_IS_OK(status)) {
2989                 goto done;
2990         }
2991
2992         if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2993
2994                 display_sam_dom_info_1(info);
2995
2996                 switch (reject->extendedFailureReason) {
2997                         case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2998                                 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2999                                 break;
3000                         case SAM_PWD_CHANGE_PWD_IN_HISTORY:
3001                                 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
3002                                 break;
3003                         case SAM_PWD_CHANGE_NOT_COMPLEX:
3004                                 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
3005                                 break;
3006                         default:
3007                                 d_printf("unknown reject reason: %d\n",
3008                                         reject->extendedFailureReason);
3009                                 break;
3010                 }
3011         }
3012
3013         if (!NT_STATUS_IS_OK(result)) {
3014                 status = result;
3015                 goto done;
3016         }
3017
3018         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3019         if (!NT_STATUS_IS_OK(status)) goto done;
3020
3021         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3022         if (!NT_STATUS_IS_OK(status)) goto done;
3023
3024  done:
3025         return status;
3026 }
3027
3028 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
3029                                          TALLOC_CTX *mem_ctx,
3030                                          int argc, const char **argv,
3031                                          int opcode)
3032 {
3033         struct policy_handle connect_pol, domain_pol, user_pol;
3034         NTSTATUS status, result;
3035         const char *user, *param;
3036         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3037         uint32_t level;
3038         uint32_t user_rid;
3039         union samr_UserInfo info;
3040         struct samr_CryptPassword pwd_buf;
3041         struct samr_CryptPasswordEx pwd_buf_ex;
3042         uint8_t nt_hash[16];
3043         uint8_t lm_hash[16];
3044         DATA_BLOB session_key;
3045         uint8_t password_expired = 0;
3046         struct dcerpc_binding_handle *b = cli->binding_handle;
3047
3048         if (argc < 4) {
3049                 printf("Usage: %s username level password [password_expired]\n",
3050                         argv[0]);
3051                 return NT_STATUS_INVALID_PARAMETER;
3052         }
3053
3054         user = argv[1];
3055         level = atoi(argv[2]);
3056         param = argv[3];
3057
3058         if (argc >= 5) {
3059                 password_expired = atoi(argv[4]);
3060         }
3061
3062         status = cli_get_session_key(mem_ctx, cli, &session_key);
3063         if (!NT_STATUS_IS_OK(status)) {
3064                 return status;
3065         }
3066
3067         init_samr_CryptPassword(param, &session_key, &pwd_buf);
3068         init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3069         nt_lm_owf_gen(param, nt_hash, lm_hash);
3070
3071         switch (level) {
3072         case 18:
3073                 {
3074                         DATA_BLOB in,out;
3075                         in = data_blob_const(nt_hash, 16);
3076                         out = data_blob_talloc_zero(mem_ctx, 16);
3077                         sess_crypt_blob(&out, &in, &session_key, true);
3078                         memcpy(nt_hash, out.data, out.length);
3079                 }
3080                 {
3081                         DATA_BLOB in,out;
3082                         in = data_blob_const(lm_hash, 16);
3083                         out = data_blob_talloc_zero(mem_ctx, 16);
3084                         sess_crypt_blob(&out, &in, &session_key, true);
3085                         memcpy(lm_hash, out.data, out.length);
3086                 }
3087
3088                 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3089                 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3090                 info.info18.nt_pwd_active       = true;
3091                 info.info18.lm_pwd_active       = true;
3092                 info.info18.password_expired    = password_expired;
3093
3094                 break;
3095         case 21:
3096                 ZERO_STRUCT(info.info21);
3097
3098                 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3099                                              SAMR_FIELD_LM_PASSWORD_PRESENT;
3100                 if (argc >= 5) {
3101                         info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3102                         info.info21.password_expired = password_expired;
3103                 }
3104
3105                 info.info21.lm_password_set = true;
3106                 info.info21.lm_owf_password.length = 16;
3107                 info.info21.lm_owf_password.size = 16;
3108
3109                 info.info21.nt_password_set = true;
3110                 info.info21.nt_owf_password.length = 16;
3111                 info.info21.nt_owf_password.size = 16;
3112
3113                 {
3114                         DATA_BLOB in,out;
3115                         in = data_blob_const(nt_hash, 16);
3116                         out = data_blob_talloc_zero(mem_ctx, 16);
3117                         sess_crypt_blob(&out, &in, &session_key, true);
3118                         info.info21.nt_owf_password.array =
3119                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3120                 }
3121                 {
3122                         DATA_BLOB in,out;
3123                         in = data_blob_const(lm_hash, 16);
3124                         out = data_blob_talloc_zero(mem_ctx, 16);
3125                         sess_crypt_blob(&out, &in, &session_key, true);
3126                         info.info21.lm_owf_password.array =
3127                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3128                 }
3129
3130                 break;
3131         case 23:
3132                 ZERO_STRUCT(info.info23);
3133
3134                 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3135                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
3136                 if (argc >= 5) {
3137                         info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3138                         info.info23.info.password_expired = password_expired;
3139                 }
3140
3141                 info.info23.password = pwd_buf;
3142
3143                 break;
3144         case 24:
3145                 info.info24.password            = pwd_buf;
3146                 info.info24.password_expired    = password_expired;
3147
3148                 break;
3149         case 25:
3150                 ZERO_STRUCT(info.info25);
3151
3152                 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3153                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
3154                 if (argc >= 5) {
3155                         info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3156                         info.info25.info.password_expired = password_expired;
3157                 }
3158
3159                 info.info25.password = pwd_buf_ex;
3160
3161                 break;
3162         case 26:
3163                 info.info26.password            = pwd_buf_ex;
3164                 info.info26.password_expired    = password_expired;
3165
3166                 break;
3167         default:
3168                 return NT_STATUS_INVALID_INFO_CLASS;
3169         }
3170
3171         /* Get sam policy handle */
3172
3173         status = rpccli_try_samr_connects(cli, mem_ctx,
3174                                           MAXIMUM_ALLOWED_ACCESS,
3175                                           &connect_pol);
3176         if (!NT_STATUS_IS_OK(status)) {
3177                 goto done;
3178         }
3179
3180         /* Get domain policy handle */
3181
3182         status = dcerpc_samr_OpenDomain(b, mem_ctx,
3183                                         &connect_pol,
3184                                         access_mask,
3185                                         &domain_sid,
3186                                         &domain_pol,
3187                                         &result);
3188
3189         if (!NT_STATUS_IS_OK(status))
3190                 goto done;
3191         if (!NT_STATUS_IS_OK(result)) {
3192                 status = result;
3193                 goto done;
3194         }
3195
3196         user_rid = strtol(user, NULL, 0);
3197         if (user_rid) {
3198                 status = dcerpc_samr_OpenUser(b, mem_ctx,
3199                                               &domain_pol,
3200                                               access_mask,
3201                                               user_rid,
3202                                               &user_pol,
3203                                               &result);
3204                 if (!NT_STATUS_IS_OK(status)) {
3205                         goto done;
3206                 }
3207
3208                 status = result;
3209         }
3210
3211         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3212             (user_rid == 0)) {
3213
3214                 /* Probably this was a user name, try lookupnames */
3215                 struct samr_Ids rids, types;
3216                 struct lsa_String lsa_acct_name;
3217
3218                 init_lsa_String(&lsa_acct_name, user);
3219
3220                 status = dcerpc_samr_LookupNames(b, mem_ctx,
3221                                                  &domain_pol,
3222                                                  1,
3223                                                  &lsa_acct_name,
3224                                                  &rids,
3225                                                  &types,
3226                                                  &result);
3227                 if (!NT_STATUS_IS_OK(status)) {
3228                         return status;
3229                 }
3230                 if (!NT_STATUS_IS_OK(result)) {
3231                         return result;
3232                 }
3233                 if (rids.count != 1) {
3234                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
3235                 }
3236                 if (types.count != 1) {
3237                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
3238                 }
3239
3240                 status = dcerpc_samr_OpenUser(b, mem_ctx,
3241                                               &domain_pol,
3242                                               access_mask,
3243                                               rids.ids[0],
3244                                               &user_pol,
3245                                               &result);
3246                 if (!NT_STATUS_IS_OK(status)) {
3247                         return status;
3248                 }
3249                 if (!NT_STATUS_IS_OK(result)) {
3250                         return result;
3251                 }
3252         }
3253
3254         switch (opcode) {
3255         case NDR_SAMR_SETUSERINFO:
3256                 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3257                                                  &user_pol,
3258                                                  level,
3259                                                  &info,
3260                                                  &result);
3261                 break;
3262         case NDR_SAMR_SETUSERINFO2:
3263                 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3264                                                   &user_pol,
3265                                                   level,
3266                                                   &info,
3267                                                   &result);
3268                 break;
3269         default:
3270                 return NT_STATUS_INVALID_PARAMETER;
3271         }
3272         if (!NT_STATUS_IS_OK(status)) {
3273                 DEBUG(0,("status: %s\n", nt_errstr(status)));
3274                 goto done;
3275         }
3276         if (!NT_STATUS_IS_OK(result)) {
3277                 status = result;
3278                 DEBUG(0,("result: %s\n", nt_errstr(status)));
3279                 goto done;
3280         }
3281  done:
3282         return status;
3283 }
3284
3285 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3286                                      TALLOC_CTX *mem_ctx,
3287                                      int argc, const char **argv)
3288 {
3289         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3290                                         NDR_SAMR_SETUSERINFO);
3291 }
3292
3293 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3294                                       TALLOC_CTX *mem_ctx,
3295                                       int argc, const char **argv)
3296 {
3297         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3298                                         NDR_SAMR_SETUSERINFO2);
3299 }
3300
3301 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3302                                           TALLOC_CTX *mem_ctx,
3303                                           int argc, const char **argv)
3304 {
3305         NTSTATUS status, result;
3306         struct policy_handle connect_handle;
3307         struct policy_handle domain_handle = { 0, };
3308         uint16_t level = 1;
3309         struct lsa_String name;
3310         uint32_t idx = 0;
3311         struct dcerpc_binding_handle *b = cli->binding_handle;
3312
3313         if (argc < 2 || argc > 3) {
3314                 printf("Usage: %s name level\n", argv[0]);
3315                 return NT_STATUS_INVALID_PARAMETER;
3316         }
3317
3318         init_lsa_String(&name, argv[1]);
3319
3320         if (argc == 3) {
3321                 level = atoi(argv[2]);
3322         }
3323
3324         status = rpccli_try_samr_connects(cli, mem_ctx,
3325                                           SEC_FLAG_MAXIMUM_ALLOWED,
3326                                           &connect_handle);
3327         if (!NT_STATUS_IS_OK(status)) {
3328                 goto done;
3329         }
3330
3331         status = dcerpc_samr_OpenDomain(b, mem_ctx,
3332                                         &connect_handle,
3333                                         SEC_FLAG_MAXIMUM_ALLOWED,
3334                                         &domain_sid,
3335                                         &domain_handle,
3336                                         &result);
3337         if (!NT_STATUS_IS_OK(status)) {
3338                 goto done;
3339         }
3340         if (!NT_STATUS_IS_OK(result)) {
3341                 status = result;
3342                 goto done;
3343         }
3344
3345         status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3346                                                         &domain_handle,
3347                                                         level,
3348                                                         &name,
3349                                                         &idx,
3350                                                         &result);
3351         if (!NT_STATUS_IS_OK(status)) {
3352                 goto done;
3353         }
3354
3355         status = result;
3356
3357         if (NT_STATUS_IS_OK(status) ||
3358             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3359                 printf("idx: %d (0x%08x)\n", idx, idx);
3360         }
3361  done:
3362
3363         if (is_valid_policy_hnd(&domain_handle)) {
3364                 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3365         }
3366         if (is_valid_policy_hnd(&connect_handle)) {
3367                 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3368         }
3369
3370         return status;
3371
3372 }
3373 /* List of commands exported by this module */
3374
3375 struct cmd_set samr_commands[] = {
3376
3377         {
3378                 .name = "SAMR",
3379         },
3380
3381         {
3382                 .name               = "queryuser",
3383                 .returntype         = RPC_RTYPE_NTSTATUS,
3384                 .ntfn               = cmd_samr_query_user,
3385                 .wfn                = NULL,
3386                 .table              = &ndr_table_samr,
3387                 .rpc_pipe           = NULL,
3388                 .description        = "Query user info",
3389                 .usage              = "",
3390         },
3391         {
3392                 .name               = "querygroup",
3393                 .returntype         = RPC_RTYPE_NTSTATUS,
3394                 .ntfn               = cmd_samr_query_group,
3395                 .wfn                = NULL,
3396                 .table              = &ndr_table_samr,
3397                 .rpc_pipe           = NULL,
3398                 .description        = "Query group info",
3399                 .usage              = "",
3400         },
3401         {
3402                 .name               = "queryusergroups",
3403                 .returntype         = RPC_RTYPE_NTSTATUS,
3404                 .ntfn               = cmd_samr_query_usergroups,
3405                 .wfn                = NULL,
3406                 .table              = &ndr_table_samr,
3407                 .rpc_pipe           = NULL,
3408                 .description        = "Query user groups",
3409                 .usage              = "",
3410         },
3411         {
3412                 .name               = "queryuseraliases",
3413                 .returntype         = RPC_RTYPE_NTSTATUS,
3414                 .ntfn               = cmd_samr_query_useraliases,
3415                 .wfn                = NULL,
3416                 .table              = &ndr_table_samr,
3417                 .rpc_pipe           = NULL,
3418                 .description        = "Query user aliases",
3419                 .usage              = "",
3420         },
3421         {
3422                 .name               = "querygroupmem",
3423                 .returntype         = RPC_RTYPE_NTSTATUS,
3424                 .ntfn               = cmd_samr_query_groupmem,
3425                 .wfn                = NULL,
3426                 .table              = &ndr_table_samr,
3427                 .rpc_pipe           = NULL,
3428                 .description        = "Query group membership",
3429                 .usage              = "",
3430         },
3431         {
3432                 .name               = "queryaliasmem",
3433                 .returntype         = RPC_RTYPE_NTSTATUS,
3434                 .ntfn               = cmd_samr_query_aliasmem,
3435                 .wfn                = NULL,
3436                 .table              = &ndr_table_samr,
3437                 .rpc_pipe           = NULL,
3438                 .description        = "Query alias membership",
3439                 .usage              = "",
3440         },
3441         {
3442                 .name               = "queryaliasinfo",
3443                 .returntype         = RPC_RTYPE_NTSTATUS,
3444                 .ntfn               = cmd_samr_query_aliasinfo,
3445                 .wfn                = NULL,
3446                 .table              = &ndr_table_samr,
3447                 .rpc_pipe           = NULL,
3448                 .description        = "Query alias info",
3449                 .usage              = "",
3450         },
3451         {
3452                 .name               = "deletealias",
3453                 .returntype         = RPC_RTYPE_NTSTATUS,
3454                 .ntfn               = cmd_samr_delete_alias,
3455                 .wfn                = NULL,
3456                 .table              = &ndr_table_samr,
3457                 .rpc_pipe           = NULL,
3458                 .description        = "Delete an alias",
3459                 .usage              = "",
3460         },
3461         {
3462                 .name               = "querydispinfo",
3463                 .returntype         = RPC_RTYPE_NTSTATUS,
3464                 .ntfn               = cmd_samr_query_dispinfo,
3465                 .wfn                = NULL,
3466                 .table              = &ndr_table_samr,
3467                 .rpc_pipe           = NULL,
3468                 .description        = "Query display info",
3469                 .usage              = "",
3470         },
3471         {
3472                 .name               = "querydispinfo2",
3473                 .returntype         = RPC_RTYPE_NTSTATUS,
3474                 .ntfn               = cmd_samr_query_dispinfo2,
3475                 .wfn                = NULL,
3476                 .table              = &ndr_table_samr,
3477                 .rpc_pipe           = NULL,
3478                 .description        = "Query display info",
3479                 .usage              = "",
3480         },
3481         {
3482                 .name               = "querydispinfo3",
3483                 .returntype         = RPC_RTYPE_NTSTATUS,
3484                 .ntfn               = cmd_samr_query_dispinfo3,
3485                 .wfn                = NULL,
3486                 .table              = &ndr_table_samr,
3487                 .rpc_pipe           = NULL,
3488                 .description        = "Query display info",
3489                 .usage              = "",
3490         },
3491         {
3492                 .name               = "querydominfo",
3493                 .returntype         = RPC_RTYPE_NTSTATUS,
3494                 .ntfn               = cmd_samr_query_dominfo,
3495                 .wfn                = NULL,
3496                 .table              = &ndr_table_samr,
3497                 .rpc_pipe           = NULL,
3498                 .description        = "Query domain info",
3499                 .usage              = "",
3500         },
3501         {
3502                 .name               = "enumdomusers",
3503                 .returntype         = RPC_RTYPE_NTSTATUS,
3504                 .ntfn               = cmd_samr_enum_dom_users,
3505                 .wfn                = NULL,
3506                 .table              = &ndr_table_samr,
3507                 .rpc_pipe           = NULL,
3508                 .description        = "Enumerate domain users",
3509                 .usage              = "",
3510         },
3511         {
3512                 .name               = "enumdomgroups",
3513                 .returntype         = RPC_RTYPE_NTSTATUS,
3514                 .ntfn               = cmd_samr_enum_dom_groups,
3515                 .wfn                = NULL,
3516                 .table              = &ndr_table_samr,
3517                 .rpc_pipe           = NULL,
3518                 .description        = "Enumerate domain groups",
3519                 .usage              = "",
3520         },
3521         {
3522                 .name               = "enumalsgroups",
3523                 .returntype         = RPC_RTYPE_NTSTATUS,
3524                 .ntfn               = cmd_samr_enum_als_groups,
3525                 .wfn                = NULL,
3526                 .table              = &ndr_table_samr,
3527                 .rpc_pipe           = NULL,
3528                 .description        = "Enumerate alias groups",
3529                 .usage              = "",
3530         },
3531         {
3532                 .name               = "enumdomains",
3533                 .returntype         = RPC_RTYPE_NTSTATUS,
3534                 .ntfn               = cmd_samr_enum_domains,
3535                 .wfn                = NULL,
3536                 .table              = &ndr_table_samr,
3537                 .rpc_pipe           = NULL,
3538                 .description        = "Enumerate domains",
3539                 .usage              = "",
3540         },
3541
3542         {
3543                 .name               = "createdomuser",
3544                 .returntype         = RPC_RTYPE_NTSTATUS,
3545                 .ntfn               = cmd_samr_create_dom_user,
3546                 .wfn                = NULL,
3547                 .table              = &ndr_table_samr,
3548                 .rpc_pipe           = NULL,
3549                 .description        = "Create domain user",
3550                 .usage              = "",
3551         },
3552         {
3553                 .name               = "createdomgroup",
3554                 .returntype         = RPC_RTYPE_NTSTATUS,
3555                 .ntfn               = cmd_samr_create_dom_group,
3556                 .wfn                = NULL,
3557                 .table              = &ndr_table_samr,
3558                 .rpc_pipe           = NULL,
3559                 .description        = "Create domain group",
3560                 .usage              = "",
3561         },
3562         {
3563                 .name               = "createdomalias",
3564                 .returntype         = RPC_RTYPE_NTSTATUS,
3565                 .ntfn               = cmd_samr_create_dom_alias,
3566                 .wfn                = NULL,
3567                 .table              = &ndr_table_samr,
3568                 .rpc_pipe           = NULL,
3569                 .description        = "Create domain alias",
3570                 .usage              = "",
3571         },
3572         {
3573                 .name               = "samlookupnames",
3574                 .returntype         = RPC_RTYPE_NTSTATUS,
3575                 .ntfn               = cmd_samr_lookup_names,
3576                 .wfn                = NULL,
3577                 .table              = &ndr_table_samr,
3578                 .rpc_pipe           = NULL,
3579                 .description        = "Look up names",
3580                 .usage              = "",
3581         },
3582         {
3583                 .name               = "samlookuprids",
3584                 .returntype         = RPC_RTYPE_NTSTATUS,
3585                 .ntfn               = cmd_samr_lookup_rids,
3586                 .wfn                = NULL,
3587                 .table              = &ndr_table_samr,
3588                 .rpc_pipe           = NULL,
3589                 .description        = "Look up names",
3590                 .usage              = "",
3591         },
3592         {
3593                 .name               = "deletedomgroup",
3594                 .returntype         = RPC_RTYPE_NTSTATUS,
3595                 .ntfn               = cmd_samr_delete_dom_group,
3596                 .wfn                = NULL,
3597                 .table              = &ndr_table_samr,
3598                 .rpc_pipe           = NULL,
3599                 .description        = "Delete domain group",
3600                 .usage              = "",
3601         },
3602         {
3603                 .name               = "deletedomuser",
3604                 .returntype         = RPC_RTYPE_NTSTATUS,
3605                 .ntfn               = cmd_samr_delete_dom_user,
3606                 .wfn                = NULL,
3607                 .table              = &ndr_table_samr,
3608                 .rpc_pipe           = NULL,
3609                 .description        = "Delete domain user",
3610                 .usage              = "",
3611         },
3612         {
3613                 .name               = "samquerysecobj",
3614                 .returntype         = RPC_RTYPE_NTSTATUS,
3615                 .ntfn               = cmd_samr_query_sec_obj,
3616                 .wfn                = NULL,
3617                 .table              = &ndr_table_samr,
3618                 .rpc_pipe           = NULL,
3619                 .description        = "Query SAMR security object",
3620                 .usage              = "",
3621         },
3622         {
3623                 .name               = "getdompwinfo",
3624                 .returntype         = RPC_RTYPE_NTSTATUS,
3625                 .ntfn               = cmd_samr_get_dom_pwinfo,
3626                 .wfn                = NULL,
3627                 .table              = &ndr_table_samr,
3628                 .rpc_pipe           = NULL,
3629                 .description        = "Retrieve domain password info",
3630                 .usage              = "",
3631         },
3632         {
3633                 .name               = "getusrdompwinfo",
3634                 .returntype         = RPC_RTYPE_NTSTATUS,
3635                 .ntfn               = cmd_samr_get_usrdom_pwinfo,
3636                 .wfn                = NULL,
3637                 .table              = &ndr_table_samr,
3638                 .rpc_pipe           = NULL,
3639                 .description        = "Retrieve user domain password info",
3640                 .usage              = "",
3641         },
3642
3643         {
3644                 .name               = "lookupdomain",
3645                 .returntype         = RPC_RTYPE_NTSTATUS,
3646                 .ntfn               = cmd_samr_lookup_domain,
3647                 .wfn                = NULL,
3648                 .table              = &ndr_table_samr,
3649                 .rpc_pipe           = NULL,
3650                 .description        = "Lookup Domain Name",
3651                 .usage              = "",
3652         },
3653         {
3654                 .name               = "chgpasswd",
3655                 .returntype         = RPC_RTYPE_NTSTATUS,
3656                 .ntfn               = cmd_samr_chgpasswd,
3657                 .wfn                = NULL,
3658                 .table              = &ndr_table_samr,
3659                 .rpc_pipe           = NULL,
3660                 .description        = "Change user password",
3661                 .usage              = "",
3662         },
3663         {
3664                 .name               = "chgpasswd2",
3665                 .returntype         = RPC_RTYPE_NTSTATUS,
3666                 .ntfn               = cmd_samr_chgpasswd2,
3667                 .wfn                = NULL,
3668                 .table              = &ndr_table_samr,
3669                 .rpc_pipe           = NULL,
3670                 .description        = "Change user password",
3671                 .usage              = "",
3672         },
3673         {
3674                 .name               = "chgpasswd3",
3675                 .returntype         = RPC_RTYPE_NTSTATUS,
3676                 .ntfn               = cmd_samr_chgpasswd3,
3677                 .wfn                = NULL,
3678                 .table              = &ndr_table_samr,
3679                 .rpc_pipe           = NULL,
3680                 .description        = "Change user password",
3681                 .usage              = "",
3682         },
3683         {
3684                 .name               = "getdispinfoidx",
3685                 .returntype         = RPC_RTYPE_NTSTATUS,
3686                 .ntfn               = cmd_samr_get_dispinfo_idx,
3687                 .wfn                = NULL,
3688                 .table              = &ndr_table_samr,
3689                 .rpc_pipe           = NULL,
3690                 .description        = "Get Display Information Index",
3691                 .usage              = "",
3692         },
3693         {
3694                 .name               = "setuserinfo",
3695                 .returntype         = RPC_RTYPE_NTSTATUS,
3696                 .ntfn               = cmd_samr_setuserinfo,
3697                 .wfn                = NULL,
3698                 .table              = &ndr_table_samr,
3699                 .rpc_pipe           = NULL,
3700                 .description        = "Set user info",
3701                 .usage              = "",
3702         },
3703         {
3704                 .name               = "setuserinfo2",
3705                 .returntype         = RPC_RTYPE_NTSTATUS,
3706                 .ntfn               = cmd_samr_setuserinfo2,
3707                 .wfn                = NULL,
3708                 .table              = &ndr_table_samr,
3709                 .rpc_pipe           = NULL,
3710                 .description        = "Set user info2",
3711                 .usage              = "",
3712         },
3713         {
3714                 .name = NULL,
3715         },
3716 };