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