rpcclient: Use dom_sid_str_buf
[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
1410         if (argc != 3) {
1411                 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1412                 return NT_STATUS_OK;
1413         }
1414
1415         alias_rid = strtoul(argv[2], NULL, 10);
1416
1417         /* Open SAMR handle */
1418
1419         status = rpccli_try_samr_connects(cli, mem_ctx,
1420                                           MAXIMUM_ALLOWED_ACCESS,
1421                                           &connect_pol);
1422         if (!NT_STATUS_IS_OK(status)) {
1423                 goto done;
1424         }
1425
1426         /* Open handle on domain */
1427
1428         status = get_domain_handle(cli, mem_ctx, argv[1],
1429                                    &connect_pol,
1430                                    MAXIMUM_ALLOWED_ACCESS,
1431                                    &domain_sid,
1432                                    &domain_pol);
1433         if (!NT_STATUS_IS_OK(status)) {
1434                 goto done;
1435         }
1436
1437         /* Open handle on alias */
1438
1439         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1440                                        &domain_pol,
1441                                        access_mask,
1442                                        alias_rid,
1443                                        &alias_pol,
1444                                        &result);
1445         if (!NT_STATUS_IS_OK(status)) {
1446                 goto done;
1447         }
1448         if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1449                 /* Probably this was a user name, try lookupnames */
1450                 struct samr_Ids rids, types;
1451                 struct lsa_String lsa_acct_name;
1452
1453                 init_lsa_String(&lsa_acct_name, argv[2]);
1454
1455                 status = dcerpc_samr_LookupNames(b, mem_ctx,
1456                                                  &domain_pol,
1457                                                  1,
1458                                                  &lsa_acct_name,
1459                                                  &rids,
1460                                                  &types,
1461                                                  &result);
1462                 if (!NT_STATUS_IS_OK(status)) {
1463                         goto done;
1464                 }
1465                 if (NT_STATUS_IS_OK(result)) {
1466                         if (rids.count != 1) {
1467                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1468                                 goto done;
1469                         }
1470                         if (types.count != 1) {
1471                                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1472                                 goto done;
1473                         }
1474
1475                         status = dcerpc_samr_OpenAlias(b, mem_ctx,
1476                                                        &domain_pol,
1477                                                        access_mask,
1478                                                        rids.ids[0],
1479                                                        &alias_pol,
1480                                                        &result);
1481                         if (!NT_STATUS_IS_OK(status)) {
1482                                 goto done;
1483                         }
1484                 }
1485         }
1486
1487         status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1488                                             &alias_pol,
1489                                             &result);
1490         if (!NT_STATUS_IS_OK(status)) {
1491                 goto done;
1492         }
1493         if (!NT_STATUS_IS_OK(result)) {
1494                 status = result;
1495                 goto done;
1496         }
1497
1498         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1499         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1500  done:
1501         return status;
1502 }
1503
1504 /* Query display info */
1505
1506 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1507                                                  TALLOC_CTX *mem_ctx,
1508                                                  int argc, const char **argv,
1509                                                  uint32_t opcode)
1510 {
1511         struct policy_handle connect_pol, domain_pol;
1512         NTSTATUS status, result;
1513         uint32_t start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1514         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1515         uint32_t info_level = 1;
1516         union samr_DispInfo info;
1517         int loop_count = 0;
1518         bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1519         uint32_t total_size, returned_size;
1520         struct dcerpc_binding_handle *b = cli->binding_handle;
1521
1522         if (argc > 6) {
1523                 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1524                 return NT_STATUS_OK;
1525         }
1526
1527         if (argc >= 2)
1528                 sscanf(argv[1], "%i", &info_level);
1529
1530         if (argc >= 3)
1531                 sscanf(argv[2], "%i", &start_idx);
1532
1533         if (argc >= 4) {
1534                 sscanf(argv[3], "%i", &max_entries);
1535                 got_params = True;
1536         }
1537
1538         if (argc >= 5) {
1539                 sscanf(argv[4], "%i", &max_size);
1540                 got_params = True;
1541         }
1542
1543         if (argc >= 6)
1544                 sscanf(argv[5], "%x", &access_mask);
1545
1546         /* Get sam policy handle */
1547
1548         status = rpccli_try_samr_connects(cli, mem_ctx,
1549                                           MAXIMUM_ALLOWED_ACCESS,
1550                                           &connect_pol);
1551         if (!NT_STATUS_IS_OK(status)) {
1552                 goto done;
1553         }
1554
1555         /* Get domain policy handle */
1556
1557         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1558                                         &connect_pol,
1559                                         access_mask,
1560                                         &domain_sid,
1561                                         &domain_pol,
1562                                         &result);
1563         if (!NT_STATUS_IS_OK(status)) {
1564                 goto done;
1565         }
1566         if (!NT_STATUS_IS_OK(result)) {
1567                 status = result;
1568                 goto done;
1569         }
1570
1571         /* Query display info */
1572
1573         do {
1574
1575                 if (!got_params)
1576                         dcerpc_get_query_dispinfo_params(
1577                                 loop_count, &max_entries, &max_size);
1578
1579                 switch (opcode) {
1580                 case NDR_SAMR_QUERYDISPLAYINFO:
1581                         status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
1582                                                               &domain_pol,
1583                                                               info_level,
1584                                                               start_idx,
1585                                                               max_entries,
1586                                                               max_size,
1587                                                               &total_size,
1588                                                               &returned_size,
1589                                                               &info,
1590                                                               &result);
1591                         break;
1592                 case NDR_SAMR_QUERYDISPLAYINFO2:
1593                         status = dcerpc_samr_QueryDisplayInfo2(b, mem_ctx,
1594                                                                &domain_pol,
1595                                                                info_level,
1596                                                                start_idx,
1597                                                                max_entries,
1598                                                                max_size,
1599                                                                &total_size,
1600                                                                &returned_size,
1601                                                                &info,
1602                                                                &result);
1603
1604                         break;
1605                 case NDR_SAMR_QUERYDISPLAYINFO3:
1606                         status = dcerpc_samr_QueryDisplayInfo3(b, mem_ctx,
1607                                                                &domain_pol,
1608                                                                info_level,
1609                                                                start_idx,
1610                                                                max_entries,
1611                                                                max_size,
1612                                                                &total_size,
1613                                                                &returned_size,
1614                                                                &info,
1615                                                                &result);
1616
1617                         break;
1618                 default:
1619                         return NT_STATUS_INVALID_PARAMETER;
1620                 }
1621
1622                 if (!NT_STATUS_IS_OK(status)) {
1623                         break;
1624                 }
1625                 status = result;
1626                 if (!NT_STATUS_IS_OK(result) &&
1627                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1628                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1629                         break;
1630                 }
1631
1632                 loop_count++;
1633
1634                 switch (info_level) {
1635                         case 1:
1636                                 num_entries = info.info1.count;
1637                                 break;
1638                         case 2:
1639                                 num_entries = info.info2.count;
1640                                 break;
1641                         case 3:
1642                                 num_entries = info.info3.count;
1643                                 break;
1644                         case 4:
1645                                 num_entries = info.info4.count;
1646                                 break;
1647                         case 5:
1648                                 num_entries = info.info5.count;
1649                                 break;
1650                         default:
1651                                 break;
1652                 }
1653
1654                 start_idx += num_entries;
1655
1656                 if (num_entries == 0)
1657                         break;
1658
1659                 for (i = 0; i < num_entries; i++) {
1660                         switch (info_level) {
1661                         case 1:
1662                                 display_sam_info_1(&info.info1.entries[i]);
1663                                 break;
1664                         case 2:
1665                                 display_sam_info_2(&info.info2.entries[i]);
1666                                 break;
1667                         case 3:
1668                                 display_sam_info_3(&info.info3.entries[i]);
1669                                 break;
1670                         case 4:
1671                                 display_sam_info_4(&info.info4.entries[i]);
1672                                 break;
1673                         case 5:
1674                                 display_sam_info_5(&info.info5.entries[i]);
1675                                 break;
1676                         }
1677                 }
1678         } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1679
1680         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1681         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1682  done:
1683         return status;
1684 }
1685
1686 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1687                                         TALLOC_CTX *mem_ctx,
1688                                         int argc, const char **argv)
1689 {
1690         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1691                                                 NDR_SAMR_QUERYDISPLAYINFO);
1692 }
1693
1694 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1695                                          TALLOC_CTX *mem_ctx,
1696                                          int argc, const char **argv)
1697 {
1698         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1699                                                 NDR_SAMR_QUERYDISPLAYINFO2);
1700 }
1701
1702 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1703                                          TALLOC_CTX *mem_ctx,
1704                                          int argc, const char **argv)
1705 {
1706         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1707                                                 NDR_SAMR_QUERYDISPLAYINFO3);
1708 }
1709
1710 /* Query domain info */
1711
1712 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1713                                        TALLOC_CTX *mem_ctx,
1714                                        int argc, const char **argv)
1715 {
1716         struct policy_handle connect_pol, domain_pol;
1717         NTSTATUS status, result;
1718         uint32_t switch_level = 2;
1719         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1720         union samr_DomainInfo *info = NULL;
1721         struct dcerpc_binding_handle *b = cli->binding_handle;
1722
1723         if (argc > 3) {
1724                 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1725                 return NT_STATUS_OK;
1726         }
1727
1728         if (argc > 1)
1729                 sscanf(argv[1], "%i", &switch_level);
1730
1731         if (argc > 2)
1732                 sscanf(argv[2], "%x", &access_mask);
1733
1734         /* Get sam policy handle */
1735
1736         status = rpccli_try_samr_connects(cli, mem_ctx,
1737                                           MAXIMUM_ALLOWED_ACCESS,
1738                                           &connect_pol);
1739         if (!NT_STATUS_IS_OK(status)) {
1740                 goto done;
1741         }
1742
1743         /* Get domain policy handle */
1744
1745         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1746                                         &connect_pol,
1747                                         access_mask,
1748                                         &domain_sid,
1749                                         &domain_pol,
1750                                         &result);
1751         if (!NT_STATUS_IS_OK(status)) {
1752                 goto done;
1753         }
1754         if (!NT_STATUS_IS_OK(result)) {
1755                 status = result;
1756                 goto done;
1757         }
1758
1759         /* Query domain info */
1760
1761         status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
1762                                              &domain_pol,
1763                                              switch_level,
1764                                              &info,
1765                                              &result);
1766         if (!NT_STATUS_IS_OK(status)) {
1767                 goto done;
1768         }
1769         if (!NT_STATUS_IS_OK(result)) {
1770                 status = result;
1771                 goto done;
1772         }
1773
1774         /* Display domain info */
1775
1776         switch (switch_level) {
1777         case 1:
1778                 display_sam_dom_info_1(&info->info1);
1779                 break;
1780         case 2:
1781                 display_sam_dom_info_2(&info->general);
1782                 break;
1783         case 3:
1784                 display_sam_dom_info_3(&info->info3);
1785                 break;
1786         case 4:
1787                 display_sam_dom_info_4(&info->oem);
1788                 break;
1789         case 5:
1790                 display_sam_dom_info_5(&info->info5);
1791                 break;
1792         case 6:
1793                 display_sam_dom_info_6(&info->info6);
1794                 break;
1795         case 7:
1796                 display_sam_dom_info_7(&info->info7);
1797                 break;
1798         case 8:
1799                 display_sam_dom_info_8(&info->info8);
1800                 break;
1801         case 9:
1802                 display_sam_dom_info_9(&info->info9);
1803                 break;
1804         case 12:
1805                 display_sam_dom_info_12(&info->info12);
1806                 break;
1807         case 13:
1808                 display_sam_dom_info_13(&info->info13);
1809                 break;
1810
1811         default:
1812                 printf("cannot display domain info for switch value %d\n",
1813                        switch_level);
1814                 break;
1815         }
1816
1817  done:
1818
1819         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1820         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1821         return status;
1822 }
1823
1824 /* Create domain user */
1825
1826 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1827                                          TALLOC_CTX *mem_ctx,
1828                                          int argc, const char **argv)
1829 {
1830         struct policy_handle connect_pol, domain_pol, user_pol;
1831         NTSTATUS status, result;
1832         struct lsa_String acct_name;
1833         uint32_t acb_info;
1834         uint32_t acct_flags, user_rid;
1835         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1836         uint32_t access_granted = 0;
1837         struct dcerpc_binding_handle *b = cli->binding_handle;
1838
1839         if ((argc < 2) || (argc > 3)) {
1840                 printf("Usage: %s username [access mask]\n", argv[0]);
1841                 return NT_STATUS_OK;
1842         }
1843
1844         init_lsa_String(&acct_name, argv[1]);
1845
1846         if (argc > 2)
1847                 sscanf(argv[2], "%x", &access_mask);
1848
1849         /* Get sam policy handle */
1850
1851         status = rpccli_try_samr_connects(cli, mem_ctx,
1852                                           MAXIMUM_ALLOWED_ACCESS,
1853                                           &connect_pol);
1854         if (!NT_STATUS_IS_OK(status)) {
1855                 goto done;
1856         }
1857
1858         /* Get domain policy handle */
1859
1860         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1861                                         &connect_pol,
1862                                         access_mask,
1863                                         &domain_sid,
1864                                         &domain_pol,
1865                                         &result);
1866         if (!NT_STATUS_IS_OK(status)) {
1867                 goto done;
1868         }
1869         if (!NT_STATUS_IS_OK(result)) {
1870                 status = result;
1871                 goto done;
1872         }
1873
1874         /* Create domain user */
1875
1876         acb_info = ACB_NORMAL;
1877         acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1878                      SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1879                      SAMR_USER_ACCESS_SET_PASSWORD |
1880                      SAMR_USER_ACCESS_GET_ATTRIBUTES |
1881                      SAMR_USER_ACCESS_SET_ATTRIBUTES;
1882
1883         status = dcerpc_samr_CreateUser2(b, mem_ctx,
1884                                          &domain_pol,
1885                                          &acct_name,
1886                                          acb_info,
1887                                          acct_flags,
1888                                          &user_pol,
1889                                          &access_granted,
1890                                          &user_rid,
1891                                          &result);
1892         if (!NT_STATUS_IS_OK(status)) {
1893                 goto done;
1894         }
1895         if (!NT_STATUS_IS_OK(result)) {
1896                 status = result;
1897                 goto done;
1898         }
1899
1900         status = dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1901         if (!NT_STATUS_IS_OK(status)) goto done;
1902
1903         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1904         if (!NT_STATUS_IS_OK(status)) goto done;
1905
1906         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1907         if (!NT_STATUS_IS_OK(status)) goto done;
1908
1909  done:
1910         return status;
1911 }
1912
1913 /* Create domain group */
1914
1915 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1916                                           TALLOC_CTX *mem_ctx,
1917                                           int argc, const char **argv)
1918 {
1919         struct policy_handle connect_pol, domain_pol, group_pol;
1920         NTSTATUS status, result;
1921         struct lsa_String grp_name;
1922         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
1923         uint32_t rid = 0;
1924         struct dcerpc_binding_handle *b = cli->binding_handle;
1925
1926         if ((argc < 2) || (argc > 3)) {
1927                 printf("Usage: %s groupname [access mask]\n", argv[0]);
1928                 return NT_STATUS_OK;
1929         }
1930
1931         init_lsa_String(&grp_name, argv[1]);
1932
1933         if (argc > 2)
1934                 sscanf(argv[2], "%x", &access_mask);
1935
1936         /* Get sam policy handle */
1937
1938         status = rpccli_try_samr_connects(cli, mem_ctx,
1939                                           MAXIMUM_ALLOWED_ACCESS,
1940                                           &connect_pol);
1941         if (!NT_STATUS_IS_OK(status)) {
1942                 goto done;
1943         }
1944
1945         /* Get domain policy handle */
1946
1947         status = dcerpc_samr_OpenDomain(b, mem_ctx,
1948                                         &connect_pol,
1949                                         access_mask,
1950                                         &domain_sid,
1951                                         &domain_pol,
1952                                         &result);
1953         if (!NT_STATUS_IS_OK(status)) {
1954                 goto done;
1955         }
1956         if (!NT_STATUS_IS_OK(result)) {
1957                 status = result;
1958                 goto done;
1959         }
1960
1961         /* Create domain user */
1962         status = dcerpc_samr_CreateDomainGroup(b, mem_ctx,
1963                                                &domain_pol,
1964                                                &grp_name,
1965                                                MAXIMUM_ALLOWED_ACCESS,
1966                                                &group_pol,
1967                                                &rid,
1968                                                &result);
1969         if (!NT_STATUS_IS_OK(status)) {
1970                 goto done;
1971         }
1972         if (!NT_STATUS_IS_OK(result)) {
1973                 status = result;
1974                 goto done;
1975         }
1976
1977         status = dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
1978         if (!NT_STATUS_IS_OK(status)) goto done;
1979
1980         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1981         if (!NT_STATUS_IS_OK(status)) goto done;
1982
1983         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1984         if (!NT_STATUS_IS_OK(status)) goto done;
1985
1986  done:
1987         return status;
1988 }
1989
1990 /* Create domain alias */
1991
1992 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1993                                           TALLOC_CTX *mem_ctx,
1994                                           int argc, const char **argv)
1995 {
1996         struct policy_handle connect_pol, domain_pol, alias_pol;
1997         NTSTATUS status, result;
1998         struct lsa_String alias_name;
1999         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2000         uint32_t rid = 0;
2001         struct dcerpc_binding_handle *b = cli->binding_handle;
2002
2003         if ((argc < 2) || (argc > 3)) {
2004                 printf("Usage: %s aliasname [access mask]\n", argv[0]);
2005                 return NT_STATUS_OK;
2006         }
2007
2008         init_lsa_String(&alias_name, argv[1]);
2009
2010         if (argc > 2)
2011                 sscanf(argv[2], "%x", &access_mask);
2012
2013         /* Get sam policy handle */
2014
2015         status = rpccli_try_samr_connects(cli, mem_ctx,
2016                                           MAXIMUM_ALLOWED_ACCESS,
2017                                           &connect_pol);
2018         if (!NT_STATUS_IS_OK(status)) {
2019                 goto done;
2020         }
2021
2022         /* Get domain policy handle */
2023
2024         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2025                                         &connect_pol,
2026                                         access_mask,
2027                                         &domain_sid,
2028                                         &domain_pol,
2029                                         &result);
2030         if (!NT_STATUS_IS_OK(status)) {
2031                 goto done;
2032         }
2033         if (!NT_STATUS_IS_OK(result)) {
2034                 status = result;
2035                 goto done;
2036         }
2037
2038         /* Create domain user */
2039
2040         status = dcerpc_samr_CreateDomAlias(b, mem_ctx,
2041                                             &domain_pol,
2042                                             &alias_name,
2043                                             MAXIMUM_ALLOWED_ACCESS,
2044                                             &alias_pol,
2045                                             &rid,
2046                                             &result);
2047         if (!NT_STATUS_IS_OK(status)) {
2048                 goto done;
2049         }
2050         if (!NT_STATUS_IS_OK(result)) {
2051                 status = result;
2052                 goto done;
2053         }
2054
2055
2056         status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &result);
2057         if (!NT_STATUS_IS_OK(status)) goto done;
2058
2059         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2060         if (!NT_STATUS_IS_OK(status)) goto done;
2061
2062         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2063         if (!NT_STATUS_IS_OK(status)) goto done;
2064
2065  done:
2066         return status;
2067 }
2068
2069 /* Lookup sam names */
2070
2071 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
2072                                       TALLOC_CTX *mem_ctx,
2073                                       int argc, const char **argv)
2074 {
2075         NTSTATUS status, result;
2076         struct policy_handle connect_pol, domain_pol;
2077         uint32_t num_names;
2078         struct samr_Ids rids, name_types;
2079         int i;
2080         struct lsa_String *names = NULL;
2081         struct dcerpc_binding_handle *b = cli->binding_handle;
2082
2083         if (argc < 3) {
2084                 printf("Usage: %s  domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
2085                 printf("check on the domain SID: S-1-5-21-x-y-z\n");
2086                 printf("or check on the builtin SID: S-1-5-32\n");
2087                 return NT_STATUS_OK;
2088         }
2089
2090         /* Get sam policy and domain handles */
2091
2092         status = rpccli_try_samr_connects(cli, mem_ctx,
2093                                           MAXIMUM_ALLOWED_ACCESS,
2094                                           &connect_pol);
2095         if (!NT_STATUS_IS_OK(status)) {
2096                 goto done;
2097         }
2098
2099         status = get_domain_handle(cli, mem_ctx, argv[1],
2100                                    &connect_pol,
2101                                    MAXIMUM_ALLOWED_ACCESS,
2102                                    &domain_sid,
2103                                    &domain_pol);
2104         if (!NT_STATUS_IS_OK(status)) {
2105                 goto done;
2106         }
2107
2108         /* Look up names */
2109
2110         num_names = argc - 2;
2111
2112         if ((names = talloc_array(mem_ctx, struct lsa_String, num_names)) == NULL) {
2113                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2114                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2115                 status = NT_STATUS_NO_MEMORY;
2116                 goto done;
2117         }
2118
2119         for (i = 0; i < num_names; i++) {
2120                 init_lsa_String(&names[i], argv[i + 2]);
2121         }
2122
2123         status = dcerpc_samr_LookupNames(b, mem_ctx,
2124                                          &domain_pol,
2125                                          num_names,
2126                                          names,
2127                                          &rids,
2128                                          &name_types,
2129                                          &result);
2130         if (!NT_STATUS_IS_OK(status)) {
2131                 goto done;
2132         }
2133         if (!NT_STATUS_IS_OK(result)) {
2134                 status = result;
2135                 goto done;
2136         }
2137         if (rids.count != num_names) {
2138                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2139                 goto done;
2140         }
2141         if (name_types.count != num_names) {
2142                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2143                 goto done;
2144         }
2145
2146         /* Display results */
2147
2148         for (i = 0; i < num_names; i++)
2149                 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
2150                        name_types.ids[i]);
2151
2152         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2153         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2154  done:
2155         return status;
2156 }
2157
2158 /* Lookup sam rids */
2159
2160 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
2161                                      TALLOC_CTX *mem_ctx,
2162                                      int argc, const char **argv)
2163 {
2164         NTSTATUS status, result;
2165         struct policy_handle connect_pol, domain_pol;
2166         uint32_t num_rids, *rids;
2167         struct lsa_Strings names;
2168         struct samr_Ids types;
2169         struct dcerpc_binding_handle *b = cli->binding_handle;
2170
2171         int i;
2172
2173         if (argc < 3) {
2174                 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
2175                 return NT_STATUS_OK;
2176         }
2177
2178         /* Get sam policy and domain handles */
2179
2180         status = rpccli_try_samr_connects(cli, mem_ctx,
2181                                           MAXIMUM_ALLOWED_ACCESS,
2182                                           &connect_pol);
2183         if (!NT_STATUS_IS_OK(status)) {
2184                 goto done;
2185         }
2186
2187         status = get_domain_handle(cli, mem_ctx, argv[1],
2188                                    &connect_pol,
2189                                    MAXIMUM_ALLOWED_ACCESS,
2190                                    &domain_sid,
2191                                    &domain_pol);
2192         if (!NT_STATUS_IS_OK(status)) {
2193                 goto done;
2194         }
2195
2196         /* Look up rids */
2197
2198         num_rids = argc - 2;
2199
2200         if ((rids = talloc_array(mem_ctx, uint32_t, num_rids)) == NULL) {
2201                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2202                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2203                 status = NT_STATUS_NO_MEMORY;
2204                 goto done;
2205         }
2206
2207         for (i = 0; i < argc - 2; i++)
2208                 sscanf(argv[i + 2], "%i", &rids[i]);
2209
2210         status = dcerpc_samr_LookupRids(b, mem_ctx,
2211                                         &domain_pol,
2212                                         num_rids,
2213                                         rids,
2214                                         &names,
2215                                         &types,
2216                                         &result);
2217         if (!NT_STATUS_IS_OK(status)) {
2218                 goto done;
2219         }
2220         status = result;
2221         if (!NT_STATUS_IS_OK(result) &&
2222             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
2223                 goto done;
2224
2225         /* Display results */
2226         if (num_rids != names.count) {
2227                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2228                 goto done;
2229         }
2230         if (num_rids != types.count) {
2231                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2232                 goto done;
2233         }
2234
2235         for (i = 0; i < num_rids; i++) {
2236                 printf("rid 0x%x: %s (%d)\n",
2237                         rids[i], names.names[i].string, types.ids[i]);
2238         }
2239
2240         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2241         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2242  done:
2243         return status;
2244 }
2245
2246 /* Delete domain group */
2247
2248 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
2249                                          TALLOC_CTX *mem_ctx,
2250                                          int argc, const char **argv)
2251 {
2252         NTSTATUS status, result;
2253         struct policy_handle connect_pol, domain_pol, group_pol;
2254         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2255         struct dcerpc_binding_handle *b = cli->binding_handle;
2256
2257         if ((argc < 2) || (argc > 3)) {
2258                 printf("Usage: %s groupname\n", argv[0]);
2259                 return NT_STATUS_OK;
2260         }
2261
2262         if (argc > 2)
2263                 sscanf(argv[2], "%x", &access_mask);
2264
2265         /* Get sam policy and domain handles */
2266
2267         status = rpccli_try_samr_connects(cli, mem_ctx,
2268                                           MAXIMUM_ALLOWED_ACCESS,
2269                                           &connect_pol);
2270         if (!NT_STATUS_IS_OK(status)) {
2271                 goto done;
2272         }
2273
2274         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2275                                         &connect_pol,
2276                                         MAXIMUM_ALLOWED_ACCESS,
2277                                         &domain_sid,
2278                                         &domain_pol,
2279                                         &result);
2280         if (!NT_STATUS_IS_OK(status)) {
2281                 goto done;
2282         }
2283         if (!NT_STATUS_IS_OK(result)) {
2284                 status = result;
2285                 goto done;
2286         }
2287
2288         /* Get handle on group */
2289
2290         {
2291                 struct samr_Ids group_rids, name_types;
2292                 struct lsa_String lsa_acct_name;
2293
2294                 init_lsa_String(&lsa_acct_name, argv[1]);
2295
2296                 status = dcerpc_samr_LookupNames(b, mem_ctx,
2297                                                  &domain_pol,
2298                                                  1,
2299                                                  &lsa_acct_name,
2300                                                  &group_rids,
2301                                                  &name_types,
2302                                                  &result);
2303                 if (!NT_STATUS_IS_OK(status)) {
2304                         goto done;
2305                 }
2306                 if (!NT_STATUS_IS_OK(result)) {
2307                         status = result;
2308                         goto done;
2309                 }
2310                 if (group_rids.count != 1) {
2311                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2312                         goto done;
2313                 }
2314                 if (name_types.count != 1) {
2315                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2316                         goto done;
2317                 }
2318
2319                 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2320                                                &domain_pol,
2321                                                access_mask,
2322                                                group_rids.ids[0],
2323                                                &group_pol,
2324                                                &result);
2325                 if (!NT_STATUS_IS_OK(status)) {
2326                         goto done;
2327                 }
2328                 if (!NT_STATUS_IS_OK(result)) {
2329                         status = result;
2330                         goto done;
2331                 }
2332         }
2333
2334         /* Delete group */
2335
2336         status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
2337                                                &group_pol,
2338                                                &result);
2339         if (!NT_STATUS_IS_OK(status)) {
2340                 goto done;
2341         }
2342         if (!NT_STATUS_IS_OK(result)) {
2343                 status = result;
2344                 goto done;
2345         }
2346
2347         /* Display results */
2348
2349         dcerpc_samr_Close(b, mem_ctx, &group_pol, &result);
2350         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2351         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2352
2353  done:
2354         return status;
2355 }
2356
2357 /* Delete domain user */
2358
2359 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2360                                          TALLOC_CTX *mem_ctx,
2361                                          int argc, const char **argv)
2362 {
2363         NTSTATUS status, result;
2364         struct policy_handle connect_pol, domain_pol, user_pol;
2365         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2366         struct dcerpc_binding_handle *b = cli->binding_handle;
2367
2368         if ((argc < 2) || (argc > 3)) {
2369                 printf("Usage: %s username\n", argv[0]);
2370                 return NT_STATUS_OK;
2371         }
2372
2373         if (argc > 2)
2374                 sscanf(argv[2], "%x", &access_mask);
2375
2376         /* Get sam policy and domain handles */
2377
2378         status = rpccli_try_samr_connects(cli, mem_ctx,
2379                                           MAXIMUM_ALLOWED_ACCESS,
2380                                           &connect_pol);
2381         if (!NT_STATUS_IS_OK(status)) {
2382                 goto done;
2383         }
2384
2385         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2386                                         &connect_pol,
2387                                         MAXIMUM_ALLOWED_ACCESS,
2388                                         &domain_sid,
2389                                         &domain_pol,
2390                                         &result);
2391         if (!NT_STATUS_IS_OK(status)) {
2392                 goto done;
2393         }
2394         if (!NT_STATUS_IS_OK(result)) {
2395                 status = result;
2396                 goto done;
2397         }
2398
2399         /* Get handle on user */
2400
2401         {
2402                 struct samr_Ids user_rids, name_types;
2403                 struct lsa_String lsa_acct_name;
2404
2405                 init_lsa_String(&lsa_acct_name, argv[1]);
2406
2407                 status = dcerpc_samr_LookupNames(b, mem_ctx,
2408                                                  &domain_pol,
2409                                                  1,
2410                                                  &lsa_acct_name,
2411                                                  &user_rids,
2412                                                  &name_types,
2413                                                  &result);
2414                 if (!NT_STATUS_IS_OK(status)) {
2415                         goto done;
2416                 }
2417                 if (!NT_STATUS_IS_OK(result)) {
2418                         status = result;
2419                         goto done;
2420                 }
2421                 if (user_rids.count != 1) {
2422                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2423                         goto done;
2424                 }
2425                 if (name_types.count != 1) {
2426                         status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2427                         goto done;
2428                 }
2429
2430                 status = dcerpc_samr_OpenUser(b, mem_ctx,
2431                                               &domain_pol,
2432                                               access_mask,
2433                                               user_rids.ids[0],
2434                                               &user_pol,
2435                                               &result);
2436                 if (!NT_STATUS_IS_OK(status)) {
2437                         goto done;
2438                 }
2439                 if (!NT_STATUS_IS_OK(result)) {
2440                         status = result;
2441                         goto done;
2442                 }
2443         }
2444
2445         /* Delete user */
2446
2447         status = dcerpc_samr_DeleteUser(b, mem_ctx,
2448                                         &user_pol,
2449                                         &result);
2450         if (!NT_STATUS_IS_OK(status)) {
2451                 goto done;
2452         }
2453         if (!NT_STATUS_IS_OK(result)) {
2454                 status = result;
2455                 goto done;
2456         }
2457
2458         /* Display results */
2459
2460         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2461         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2462         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2463
2464  done:
2465         return status;
2466 }
2467
2468 /**********************************************************************
2469  * Query user security object
2470  */
2471 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2472                                     TALLOC_CTX *mem_ctx,
2473                                     int argc, const char **argv)
2474 {
2475         struct policy_handle connect_pol, domain_pol, user_pol, *pol;
2476         NTSTATUS status, result;
2477         uint32_t sec_info = SECINFO_DACL;
2478         uint32_t user_rid = 0;
2479         TALLOC_CTX *ctx = NULL;
2480         struct sec_desc_buf *sec_desc_buf=NULL;
2481         bool domain = False;
2482         struct dcerpc_binding_handle *b = cli->binding_handle;
2483
2484         ctx=talloc_init("cmd_samr_query_sec_obj");
2485
2486         if ((argc < 1) || (argc > 3)) {
2487                 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2488                 printf("\tSpecify rid for security on user, -d for security on domain\n");
2489                 talloc_destroy(ctx);
2490                 return NT_STATUS_OK;
2491         }
2492
2493         if (argc > 1) {
2494                 if (strcmp(argv[1], "-d") == 0)
2495                         domain = True;
2496                 else
2497                         sscanf(argv[1], "%i", &user_rid);
2498         }
2499
2500         if (argc == 3) {
2501                 sec_info = atoi(argv[2]);
2502         }
2503
2504         status = rpccli_try_samr_connects(cli, mem_ctx,
2505                                           MAXIMUM_ALLOWED_ACCESS,
2506                                           &connect_pol);
2507         if (!NT_STATUS_IS_OK(status)) {
2508                 goto done;
2509         }
2510
2511         if (domain || user_rid) {
2512                 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2513                                                 &connect_pol,
2514                                                 MAXIMUM_ALLOWED_ACCESS,
2515                                                 &domain_sid,
2516                                                 &domain_pol,
2517                                                 &result);
2518                 if (!NT_STATUS_IS_OK(status)) {
2519                         goto done;
2520                 }
2521                 if (!NT_STATUS_IS_OK(result)) {
2522                         status = result;
2523                         goto done;
2524                 }
2525         }
2526
2527         if (user_rid) {
2528                 status = dcerpc_samr_OpenUser(b, mem_ctx,
2529                                               &domain_pol,
2530                                               MAXIMUM_ALLOWED_ACCESS,
2531                                               user_rid,
2532                                               &user_pol,
2533                                               &result);
2534                 if (!NT_STATUS_IS_OK(status)) {
2535                         goto done;
2536                 }
2537                 if (!NT_STATUS_IS_OK(result)) {
2538                         status = result;
2539                         goto done;
2540                 }
2541         }
2542
2543         /* Pick which query pol to use */
2544
2545         pol = &connect_pol;
2546
2547         if (domain)
2548                 pol = &domain_pol;
2549
2550         if (user_rid)
2551                 pol = &user_pol;
2552
2553         /* Query SAM security object */
2554
2555         status = dcerpc_samr_QuerySecurity(b, mem_ctx,
2556                                            pol,
2557                                            sec_info,
2558                                            &sec_desc_buf,
2559                                            &result);
2560         if (!NT_STATUS_IS_OK(status)) {
2561                 goto done;
2562         }
2563         if (!NT_STATUS_IS_OK(result)) {
2564                 status = result;
2565                 goto done;
2566         }
2567
2568         display_sec_desc(sec_desc_buf->sd);
2569
2570         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2571         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2572         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2573 done:
2574         talloc_destroy(ctx);
2575         return status;
2576 }
2577
2578 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2579                                            TALLOC_CTX *mem_ctx,
2580                                            int argc, const char **argv)
2581 {
2582         NTSTATUS status, result;
2583         struct policy_handle connect_pol, domain_pol, user_pol;
2584         struct samr_PwInfo info;
2585         uint32_t rid;
2586         struct dcerpc_binding_handle *b = cli->binding_handle;
2587
2588         if (argc != 2) {
2589                 printf("Usage: %s rid\n", argv[0]);
2590                 return NT_STATUS_OK;
2591         }
2592
2593         sscanf(argv[1], "%i", &rid);
2594
2595         status = rpccli_try_samr_connects(cli, mem_ctx,
2596                                           MAXIMUM_ALLOWED_ACCESS,
2597                                           &connect_pol);
2598         if (!NT_STATUS_IS_OK(status)) {
2599                 goto done;
2600         }
2601
2602         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2603                                         &connect_pol,
2604                                         MAXIMUM_ALLOWED_ACCESS,
2605                                         &domain_sid,
2606                                         &domain_pol,
2607                                         &result);
2608         if (!NT_STATUS_IS_OK(status)) {
2609                 goto done;
2610         }
2611         if (!NT_STATUS_IS_OK(result)) {
2612                 status = result;
2613                 goto done;
2614         }
2615
2616         status = dcerpc_samr_OpenUser(b, mem_ctx,
2617                                       &domain_pol,
2618                                       MAXIMUM_ALLOWED_ACCESS,
2619                                       rid,
2620                                       &user_pol,
2621                                       &result);
2622         if (!NT_STATUS_IS_OK(status)) {
2623                 goto done;
2624         }
2625         if (!NT_STATUS_IS_OK(result)) {
2626                 status = result;
2627                 goto done;
2628         }
2629
2630         status = dcerpc_samr_GetUserPwInfo(b, mem_ctx,
2631                                            &user_pol,
2632                                            &info,
2633                                            &result);
2634         if (!NT_STATUS_IS_OK(status)) {
2635                 goto done;
2636         }
2637         status = result;
2638         if (NT_STATUS_IS_OK(result)) {
2639                 printf("min_password_length: %d\n", info.min_password_length);
2640                 printf("%s\n",
2641                         NDR_PRINT_STRUCT_STRING(mem_ctx,
2642                                 samr_PasswordProperties, &info.password_properties));
2643         }
2644
2645  done:
2646         dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2647         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2648         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2649
2650         return status;
2651 }
2652
2653 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2654                                         TALLOC_CTX *mem_ctx,
2655                                         int argc, const char **argv)
2656 {
2657         NTSTATUS status, result;
2658         struct lsa_String domain_name;
2659         struct samr_PwInfo info;
2660         struct dcerpc_binding_handle *b = cli->binding_handle;
2661
2662         if (argc < 1 || argc > 3) {
2663                 printf("Usage: %s <domain>\n", argv[0]);
2664                 return NT_STATUS_OK;
2665         }
2666
2667         init_lsa_String(&domain_name, argv[1]);
2668
2669         status = dcerpc_samr_GetDomPwInfo(b, mem_ctx,
2670                                           &domain_name,
2671                                           &info,
2672                                           &result);
2673         if (!NT_STATUS_IS_OK(status)) {
2674                 return status;
2675         }
2676         if (NT_STATUS_IS_OK(result)) {
2677                 printf("min_password_length: %d\n", info.min_password_length);
2678                 display_password_properties(info.password_properties);
2679         }
2680
2681         return result;
2682 }
2683
2684 /* Look up domain name */
2685
2686 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2687                                        TALLOC_CTX *mem_ctx,
2688                                        int argc, const char **argv)
2689 {
2690         struct policy_handle connect_pol, domain_pol;
2691         NTSTATUS status, result;
2692         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2693         struct lsa_String domain_name;
2694         struct dom_sid *sid = NULL;
2695         struct dcerpc_binding_handle *b = cli->binding_handle;
2696
2697         if (argc != 2) {
2698                 printf("Usage: %s domain_name\n", argv[0]);
2699                 return NT_STATUS_OK;
2700         }
2701
2702         init_lsa_String(&domain_name, argv[1]);
2703
2704         status = rpccli_try_samr_connects(cli, mem_ctx,
2705                                           access_mask,
2706                                           &connect_pol);
2707         if (!NT_STATUS_IS_OK(status)) {
2708                 goto done;
2709         }
2710
2711         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2712                                         &connect_pol,
2713                                         access_mask,
2714                                         &domain_sid,
2715                                         &domain_pol,
2716                                         &result);
2717         if (!NT_STATUS_IS_OK(status)) {
2718                 goto done;
2719         }
2720         if (!NT_STATUS_IS_OK(result)) {
2721                 status = result;
2722                 goto done;
2723         }
2724
2725         status = dcerpc_samr_LookupDomain(b, mem_ctx,
2726                                           &connect_pol,
2727                                           &domain_name,
2728                                           &sid,
2729                                           &result);
2730         if (!NT_STATUS_IS_OK(status)) {
2731                 goto done;
2732         }
2733         if (!NT_STATUS_IS_OK(result)) {
2734                 status = result;
2735                 goto done;
2736         }
2737
2738         if (NT_STATUS_IS_OK(result)) {
2739                 struct dom_sid_buf sid_str;
2740                 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2741                        argv[1],
2742                        dom_sid_str_buf(sid, &sid_str));
2743         }
2744
2745         dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2746         dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2747 done:
2748         return status;
2749 }
2750
2751 /* Change user password */
2752
2753 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2754                                    TALLOC_CTX *mem_ctx,
2755                                    int argc, const char **argv)
2756 {
2757         struct policy_handle connect_pol;
2758         struct policy_handle domain_pol = { 0, };
2759         struct policy_handle user_pol = { 0, };
2760         NTSTATUS status, result;
2761         const char *user, *oldpass, *newpass;
2762         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2763         struct samr_Ids rids, types;
2764         struct lsa_String lsa_acct_name;
2765         struct dcerpc_binding_handle *b = cli->binding_handle;
2766
2767         if (argc < 3) {
2768                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2769                 return NT_STATUS_INVALID_PARAMETER;
2770         }
2771
2772         user = argv[1];
2773         oldpass = argv[2];
2774         newpass = argv[3];
2775
2776         /* Get sam policy handle */
2777
2778         status = rpccli_try_samr_connects(cli, mem_ctx,
2779                                           MAXIMUM_ALLOWED_ACCESS,
2780                                           &connect_pol);
2781         if (!NT_STATUS_IS_OK(status)) {
2782                 goto done;
2783         }
2784
2785         /* Get domain policy handle */
2786
2787         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2788                                         &connect_pol,
2789                                         access_mask,
2790                                         &domain_sid,
2791                                         &domain_pol,
2792                                         &result);
2793         if (!NT_STATUS_IS_OK(status)) {
2794                 goto done;
2795         }
2796         if (!NT_STATUS_IS_OK(result)) {
2797                 status = result;
2798                 goto done;
2799         }
2800
2801         init_lsa_String(&lsa_acct_name, user);
2802
2803         status = dcerpc_samr_LookupNames(b, mem_ctx,
2804                                          &domain_pol,
2805                                          1,
2806                                          &lsa_acct_name,
2807                                          &rids,
2808                                          &types,
2809                                          &result);
2810         if (!NT_STATUS_IS_OK(status)) {
2811                 goto done;
2812         }
2813         if (!NT_STATUS_IS_OK(result)) {
2814                 status = result;
2815                 goto done;
2816         }
2817         if (rids.count != 1) {
2818                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2819                 goto done;
2820         }
2821         if (types.count != 1) {
2822                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2823                 goto done;
2824         }
2825
2826         status = dcerpc_samr_OpenUser(b, mem_ctx,
2827                                       &domain_pol,
2828                                       access_mask,
2829                                       rids.ids[0],
2830                                       &user_pol,
2831                                       &result);
2832         if (!NT_STATUS_IS_OK(status)) {
2833                 goto done;
2834         }
2835         if (!NT_STATUS_IS_OK(result)) {
2836                 status = result;
2837                 goto done;
2838         }
2839
2840         /* Change user password */
2841         status = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2842                                             &user_pol,
2843                                             newpass,
2844                                             oldpass);
2845         if (!NT_STATUS_IS_OK(status)) {
2846                 goto done;
2847         }
2848
2849  done:
2850         if (is_valid_policy_hnd(&user_pol)) {
2851                 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
2852         }
2853         if (is_valid_policy_hnd(&domain_pol)) {
2854                 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2855         }
2856         if (is_valid_policy_hnd(&connect_pol)) {
2857                 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2858         }
2859
2860         return status;
2861 }
2862
2863
2864 /* Change user password */
2865
2866 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2867                                     TALLOC_CTX *mem_ctx,
2868                                     int argc, const char **argv)
2869 {
2870         struct policy_handle connect_pol, domain_pol;
2871         NTSTATUS status, result;
2872         const char *user, *oldpass, *newpass;
2873         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2874         struct dcerpc_binding_handle *b = cli->binding_handle;
2875
2876         if (argc < 3) {
2877                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2878                 return NT_STATUS_INVALID_PARAMETER;
2879         }
2880
2881         user = argv[1];
2882         oldpass = argv[2];
2883         newpass = argv[3];
2884
2885         /* Get sam policy handle */
2886
2887         status = rpccli_try_samr_connects(cli, mem_ctx,
2888                                           MAXIMUM_ALLOWED_ACCESS,
2889                                           &connect_pol);
2890         if (!NT_STATUS_IS_OK(status)) {
2891                 goto done;
2892         }
2893
2894         /* Get domain policy handle */
2895
2896         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2897                                         &connect_pol,
2898                                         access_mask,
2899                                         &domain_sid,
2900                                         &domain_pol,
2901                                         &result);
2902         if (!NT_STATUS_IS_OK(status)) {
2903                 goto done;
2904         }
2905         if (!NT_STATUS_IS_OK(result)) {
2906                 status = result;
2907                 goto done;
2908         }
2909
2910         /* Change user password */
2911         status = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2912
2913         if (!NT_STATUS_IS_OK(status)) {
2914                 goto done;
2915         }
2916
2917         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2918         if (!NT_STATUS_IS_OK(status)) goto done;
2919
2920         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2921         if (!NT_STATUS_IS_OK(status)) goto done;
2922
2923  done:
2924         return status;
2925 }
2926
2927
2928 /* Change user password */
2929
2930 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2931                                     TALLOC_CTX *mem_ctx,
2932                                     int argc, const char **argv)
2933 {
2934         struct policy_handle connect_pol, domain_pol;
2935         NTSTATUS status, result;
2936         const char *user, *oldpass, *newpass;
2937         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2938         struct samr_DomInfo1 *info = NULL;
2939         struct userPwdChangeFailureInformation *reject = NULL;
2940         struct dcerpc_binding_handle *b = cli->binding_handle;
2941
2942         if (argc < 3) {
2943                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2944                 return NT_STATUS_INVALID_PARAMETER;
2945         }
2946
2947         user = argv[1];
2948         oldpass = argv[2];
2949         newpass = argv[3];
2950
2951         /* Get sam policy handle */
2952
2953         status = rpccli_try_samr_connects(cli, mem_ctx,
2954                                           MAXIMUM_ALLOWED_ACCESS,
2955                                           &connect_pol);
2956         if (!NT_STATUS_IS_OK(status)) {
2957                 goto done;
2958         }
2959
2960         /* Get domain policy handle */
2961
2962         status = dcerpc_samr_OpenDomain(b, mem_ctx,
2963                                         &connect_pol,
2964                                         access_mask,
2965                                         &domain_sid,
2966                                         &domain_pol,
2967                                         &result);
2968         if (!NT_STATUS_IS_OK(status)) {
2969                 goto done;
2970         }
2971         if (!NT_STATUS_IS_OK(result)) {
2972                 status = result;
2973                 goto done;
2974         }
2975
2976         /* Change user password */
2977         status = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2978                                              user,
2979                                              newpass,
2980                                              oldpass,
2981                                              &info,
2982                                              &reject);
2983         if (!NT_STATUS_IS_OK(status)) {
2984                 goto done;
2985         }
2986
2987         if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2988
2989                 display_sam_dom_info_1(info);
2990
2991                 switch (reject->extendedFailureReason) {
2992                         case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
2993                                 d_printf("SAM_PWD_CHANGE_PASSWORD_TOO_SHORT\n");
2994                                 break;
2995                         case SAM_PWD_CHANGE_PWD_IN_HISTORY:
2996                                 d_printf("SAM_PWD_CHANGE_PWD_IN_HISTORY\n");
2997                                 break;
2998                         case SAM_PWD_CHANGE_NOT_COMPLEX:
2999                                 d_printf("SAM_PWD_CHANGE_NOT_COMPLEX\n");
3000                                 break;
3001                         default:
3002                                 d_printf("unknown reject reason: %d\n",
3003                                         reject->extendedFailureReason);
3004                                 break;
3005                 }
3006         }
3007
3008         if (!NT_STATUS_IS_OK(result)) {
3009                 status = result;
3010                 goto done;
3011         }
3012
3013         status = dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3014         if (!NT_STATUS_IS_OK(status)) goto done;
3015
3016         status = dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
3017         if (!NT_STATUS_IS_OK(status)) goto done;
3018
3019  done:
3020         return status;
3021 }
3022
3023 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
3024                                          TALLOC_CTX *mem_ctx,
3025                                          int argc, const char **argv,
3026                                          int opcode)
3027 {
3028         struct policy_handle connect_pol, domain_pol, user_pol;
3029         NTSTATUS status, result;
3030         const char *user, *param;
3031         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
3032         uint32_t level;
3033         uint32_t user_rid;
3034         union samr_UserInfo info;
3035         struct samr_CryptPassword pwd_buf;
3036         struct samr_CryptPasswordEx pwd_buf_ex;
3037         uint8_t nt_hash[16];
3038         uint8_t lm_hash[16];
3039         DATA_BLOB session_key;
3040         uint8_t password_expired = 0;
3041         struct dcerpc_binding_handle *b = cli->binding_handle;
3042
3043         if (argc < 4) {
3044                 printf("Usage: %s username level password [password_expired]\n",
3045                         argv[0]);
3046                 return NT_STATUS_INVALID_PARAMETER;
3047         }
3048
3049         user = argv[1];
3050         level = atoi(argv[2]);
3051         param = argv[3];
3052
3053         if (argc >= 5) {
3054                 password_expired = atoi(argv[4]);
3055         }
3056
3057         status = cli_get_session_key(mem_ctx, cli, &session_key);
3058         if (!NT_STATUS_IS_OK(status)) {
3059                 return status;
3060         }
3061
3062         init_samr_CryptPassword(param, &session_key, &pwd_buf);
3063         init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
3064         nt_lm_owf_gen(param, nt_hash, lm_hash);
3065
3066         switch (level) {
3067         case 18:
3068                 {
3069                         DATA_BLOB in,out;
3070                         in = data_blob_const(nt_hash, 16);
3071                         out = data_blob_talloc_zero(mem_ctx, 16);
3072                         sess_crypt_blob(&out, &in, &session_key, true);
3073                         memcpy(nt_hash, out.data, out.length);
3074                 }
3075                 {
3076                         DATA_BLOB in,out;
3077                         in = data_blob_const(lm_hash, 16);
3078                         out = data_blob_talloc_zero(mem_ctx, 16);
3079                         sess_crypt_blob(&out, &in, &session_key, true);
3080                         memcpy(lm_hash, out.data, out.length);
3081                 }
3082
3083                 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
3084                 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
3085                 info.info18.nt_pwd_active       = true;
3086                 info.info18.lm_pwd_active       = true;
3087                 info.info18.password_expired    = password_expired;
3088
3089                 break;
3090         case 21:
3091                 ZERO_STRUCT(info.info21);
3092
3093                 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3094                                              SAMR_FIELD_LM_PASSWORD_PRESENT;
3095                 if (argc >= 5) {
3096                         info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3097                         info.info21.password_expired = password_expired;
3098                 }
3099
3100                 info.info21.lm_password_set = true;
3101                 info.info21.lm_owf_password.length = 16;
3102                 info.info21.lm_owf_password.size = 16;
3103
3104                 info.info21.nt_password_set = true;
3105                 info.info21.nt_owf_password.length = 16;
3106                 info.info21.nt_owf_password.size = 16;
3107
3108                 {
3109                         DATA_BLOB in,out;
3110                         in = data_blob_const(nt_hash, 16);
3111                         out = data_blob_talloc_zero(mem_ctx, 16);
3112                         sess_crypt_blob(&out, &in, &session_key, true);
3113                         info.info21.nt_owf_password.array =
3114                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3115                 }
3116                 {
3117                         DATA_BLOB in,out;
3118                         in = data_blob_const(lm_hash, 16);
3119                         out = data_blob_talloc_zero(mem_ctx, 16);
3120                         sess_crypt_blob(&out, &in, &session_key, true);
3121                         info.info21.lm_owf_password.array =
3122                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
3123                 }
3124
3125                 break;
3126         case 23:
3127                 ZERO_STRUCT(info.info23);
3128
3129                 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3130                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
3131                 if (argc >= 5) {
3132                         info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3133                         info.info23.info.password_expired = password_expired;
3134                 }
3135
3136                 info.info23.password = pwd_buf;
3137
3138                 break;
3139         case 24:
3140                 info.info24.password            = pwd_buf;
3141                 info.info24.password_expired    = password_expired;
3142
3143                 break;
3144         case 25:
3145                 ZERO_STRUCT(info.info25);
3146
3147                 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
3148                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
3149                 if (argc >= 5) {
3150                         info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
3151                         info.info25.info.password_expired = password_expired;
3152                 }
3153
3154                 info.info25.password = pwd_buf_ex;
3155
3156                 break;
3157         case 26:
3158                 info.info26.password            = pwd_buf_ex;
3159                 info.info26.password_expired    = password_expired;
3160
3161                 break;
3162         default:
3163                 return NT_STATUS_INVALID_INFO_CLASS;
3164         }
3165
3166         /* Get sam policy handle */
3167
3168         status = rpccli_try_samr_connects(cli, mem_ctx,
3169                                           MAXIMUM_ALLOWED_ACCESS,
3170                                           &connect_pol);
3171         if (!NT_STATUS_IS_OK(status)) {
3172                 goto done;
3173         }
3174
3175         /* Get domain policy handle */
3176
3177         status = dcerpc_samr_OpenDomain(b, mem_ctx,
3178                                         &connect_pol,
3179                                         access_mask,
3180                                         &domain_sid,
3181                                         &domain_pol,
3182                                         &result);
3183
3184         if (!NT_STATUS_IS_OK(status))
3185                 goto done;
3186         if (!NT_STATUS_IS_OK(result)) {
3187                 status = result;
3188                 goto done;
3189         }
3190
3191         user_rid = strtol(user, NULL, 0);
3192         if (user_rid) {
3193                 status = dcerpc_samr_OpenUser(b, mem_ctx,
3194                                               &domain_pol,
3195                                               access_mask,
3196                                               user_rid,
3197                                               &user_pol,
3198                                               &result);
3199                 if (!NT_STATUS_IS_OK(status)) {
3200                         goto done;
3201                 }
3202
3203                 status = result;
3204         }
3205
3206         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
3207             (user_rid == 0)) {
3208
3209                 /* Probably this was a user name, try lookupnames */
3210                 struct samr_Ids rids, types;
3211                 struct lsa_String lsa_acct_name;
3212
3213                 init_lsa_String(&lsa_acct_name, user);
3214
3215                 status = dcerpc_samr_LookupNames(b, mem_ctx,
3216                                                  &domain_pol,
3217                                                  1,
3218                                                  &lsa_acct_name,
3219                                                  &rids,
3220                                                  &types,
3221                                                  &result);
3222                 if (!NT_STATUS_IS_OK(status)) {
3223                         return status;
3224                 }
3225                 if (!NT_STATUS_IS_OK(result)) {
3226                         return result;
3227                 }
3228                 if (rids.count != 1) {
3229                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
3230                 }
3231                 if (types.count != 1) {
3232                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
3233                 }
3234
3235                 status = dcerpc_samr_OpenUser(b, mem_ctx,
3236                                               &domain_pol,
3237                                               access_mask,
3238                                               rids.ids[0],
3239                                               &user_pol,
3240                                               &result);
3241                 if (!NT_STATUS_IS_OK(status)) {
3242                         return status;
3243                 }
3244                 if (!NT_STATUS_IS_OK(result)) {
3245                         return result;
3246                 }
3247         }
3248
3249         switch (opcode) {
3250         case NDR_SAMR_SETUSERINFO:
3251                 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
3252                                                  &user_pol,
3253                                                  level,
3254                                                  &info,
3255                                                  &result);
3256                 break;
3257         case NDR_SAMR_SETUSERINFO2:
3258                 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
3259                                                   &user_pol,
3260                                                   level,
3261                                                   &info,
3262                                                   &result);
3263                 break;
3264         default:
3265                 return NT_STATUS_INVALID_PARAMETER;
3266         }
3267         if (!NT_STATUS_IS_OK(status)) {
3268                 DEBUG(0,("status: %s\n", nt_errstr(status)));
3269                 goto done;
3270         }
3271         if (!NT_STATUS_IS_OK(result)) {
3272                 status = result;
3273                 DEBUG(0,("result: %s\n", nt_errstr(status)));
3274                 goto done;
3275         }
3276  done:
3277         return status;
3278 }
3279
3280 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
3281                                      TALLOC_CTX *mem_ctx,
3282                                      int argc, const char **argv)
3283 {
3284         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3285                                         NDR_SAMR_SETUSERINFO);
3286 }
3287
3288 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
3289                                       TALLOC_CTX *mem_ctx,
3290                                       int argc, const char **argv)
3291 {
3292         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
3293                                         NDR_SAMR_SETUSERINFO2);
3294 }
3295
3296 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
3297                                           TALLOC_CTX *mem_ctx,
3298                                           int argc, const char **argv)
3299 {
3300         NTSTATUS status, result;
3301         struct policy_handle connect_handle;
3302         struct policy_handle domain_handle = { 0, };
3303         uint16_t level = 1;
3304         struct lsa_String name;
3305         uint32_t idx = 0;
3306         struct dcerpc_binding_handle *b = cli->binding_handle;
3307
3308         if (argc < 2 || argc > 3) {
3309                 printf("Usage: %s name level\n", argv[0]);
3310                 return NT_STATUS_INVALID_PARAMETER;
3311         }
3312
3313         init_lsa_String(&name, argv[1]);
3314
3315         if (argc == 3) {
3316                 level = atoi(argv[2]);
3317         }
3318
3319         status = rpccli_try_samr_connects(cli, mem_ctx,
3320                                           SEC_FLAG_MAXIMUM_ALLOWED,
3321                                           &connect_handle);
3322         if (!NT_STATUS_IS_OK(status)) {
3323                 goto done;
3324         }
3325
3326         status = dcerpc_samr_OpenDomain(b, mem_ctx,
3327                                         &connect_handle,
3328                                         SEC_FLAG_MAXIMUM_ALLOWED,
3329                                         &domain_sid,
3330                                         &domain_handle,
3331                                         &result);
3332         if (!NT_STATUS_IS_OK(status)) {
3333                 goto done;
3334         }
3335         if (!NT_STATUS_IS_OK(result)) {
3336                 status = result;
3337                 goto done;
3338         }
3339
3340         status = dcerpc_samr_GetDisplayEnumerationIndex(b, mem_ctx,
3341                                                         &domain_handle,
3342                                                         level,
3343                                                         &name,
3344                                                         &idx,
3345                                                         &result);
3346         if (!NT_STATUS_IS_OK(status)) {
3347                 goto done;
3348         }
3349
3350         status = result;
3351
3352         if (NT_STATUS_IS_OK(status) ||
3353             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
3354                 printf("idx: %d (0x%08x)\n", idx, idx);
3355         }
3356  done:
3357
3358         if (is_valid_policy_hnd(&domain_handle)) {
3359                 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
3360         }
3361         if (is_valid_policy_hnd(&connect_handle)) {
3362                 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
3363         }
3364
3365         return status;
3366
3367 }
3368 /* List of commands exported by this module */
3369
3370 struct cmd_set samr_commands[] = {
3371
3372         { "SAMR" },
3373
3374         { "queryuser",  RPC_RTYPE_NTSTATUS, cmd_samr_query_user,                NULL, &ndr_table_samr, NULL,    "Query user info",         "" },
3375         { "querygroup",         RPC_RTYPE_NTSTATUS, cmd_samr_query_group,               NULL, &ndr_table_samr, NULL,    "Query group info",        "" },
3376         { "queryusergroups",    RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups,  NULL, &ndr_table_samr, NULL,    "Query user groups",       "" },
3377         { "queryuseraliases",   RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases,         NULL, &ndr_table_samr, NULL,    "Query user aliases",      "" },
3378         { "querygroupmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem,    NULL, &ndr_table_samr, NULL,    "Query group membership",  "" },
3379         { "queryaliasmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem,    NULL, &ndr_table_samr, NULL,    "Query alias membership",  "" },
3380         { "queryaliasinfo",     RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo,   NULL, &ndr_table_samr, NULL,    "Query alias info",       "" },
3381         { "deletealias",        RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias,      NULL, &ndr_table_samr, NULL,    "Delete an alias",  "" },
3382         { "querydispinfo",      RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo,    NULL, &ndr_table_samr, NULL,    "Query display info",      "" },
3383         { "querydispinfo2",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2,   NULL, &ndr_table_samr, NULL,    "Query display info",      "" },
3384         { "querydispinfo3",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3,   NULL, &ndr_table_samr, NULL,    "Query display info",      "" },
3385         { "querydominfo",       RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo,     NULL, &ndr_table_samr, NULL,    "Query domain info",       "" },
3386         { "enumdomusers",       RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users,       NULL, &ndr_table_samr, NULL, "Enumerate domain users", "" },
3387         { "enumdomgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups,       NULL, &ndr_table_samr, NULL,        "Enumerate domain groups", "" },
3388         { "enumalsgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups,       NULL, &ndr_table_samr, NULL,        "Enumerate alias groups",  "" },
3389         { "enumdomains",        RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains,          NULL, &ndr_table_samr, NULL,        "Enumerate domains",  "" },
3390
3391         { "createdomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user,       NULL, &ndr_table_samr, NULL,        "Create domain user",      "" },
3392         { "createdomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group,      NULL, &ndr_table_samr, NULL,        "Create domain group",     "" },
3393         { "createdomalias",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias,      NULL, &ndr_table_samr, NULL,        "Create domain alias",     "" },
3394         { "samlookupnames",     RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names,          NULL, &ndr_table_samr, NULL,        "Look up names",           "" },
3395         { "samlookuprids",      RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids,           NULL, &ndr_table_samr, NULL,        "Look up names",           "" },
3396         { "deletedomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group,      NULL, &ndr_table_samr, NULL,        "Delete domain group",     "" },
3397         { "deletedomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user,       NULL, &ndr_table_samr, NULL,        "Delete domain user",      "" },
3398         { "samquerysecobj",     RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj,         NULL, &ndr_table_samr, NULL, "Query SAMR security object",   "" },
3399         { "getdompwinfo",       RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo,        NULL, &ndr_table_samr, NULL, "Retrieve domain password info", "" },
3400         { "getusrdompwinfo",    RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo,     NULL, &ndr_table_samr, NULL, "Retrieve user domain password info", "" },
3401
3402         { "lookupdomain",       RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain,         NULL, &ndr_table_samr, NULL, "Lookup Domain Name", "" },
3403         { "chgpasswd",          RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd,             NULL, &ndr_table_samr, NULL, "Change user password", "" },
3404         { "chgpasswd2",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2,            NULL, &ndr_table_samr, NULL, "Change user password", "" },
3405         { "chgpasswd3",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3,            NULL, &ndr_table_samr, NULL, "Change user password", "" },
3406         { "getdispinfoidx",     RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx,      NULL, &ndr_table_samr, NULL, "Get Display Information Index", "" },
3407         { "setuserinfo",        RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo,           NULL, &ndr_table_samr, NULL, "Set user info", "" },
3408         { "setuserinfo2",       RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2,          NULL, &ndr_table_samr, NULL, "Set user info2", "" },
3409         { NULL }
3410 };