31977e955467c8db6b2bb9b6a985b829e486c3c8
[tprouty/samba.git] / source3 / rpcclient / cmd_samr.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Andrew Tridgell              1992-2000,
6    Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7    Copyright (C) Elrond                            2000,
8    Copyright (C) Tim Potter                        2000
9    Copyright (C) Guenther Deschner                 2008
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "rpcclient.h"
27
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         POLICY_HND 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         POLICY_HND 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         POLICY_HND              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         POLICY_HND              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         POLICY_HND 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         POLICY_HND 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         bool got_connect_pol = False, got_domain_pol = False;
793
794         if ((argc < 1) || (argc > 3)) {
795                 printf("Usage: %s [access_mask] [acb_mask]\n", argv[0]);
796                 return NT_STATUS_OK;
797         }
798
799         if (argc > 1)
800                 sscanf(argv[1], "%x", &access_mask);
801
802         if (argc > 2)
803                 sscanf(argv[2], "%x", &acb_mask);
804
805         /* Get sam policy handle */
806
807         result = rpccli_try_samr_connects(cli, mem_ctx,
808                                           MAXIMUM_ALLOWED_ACCESS,
809                                           &connect_pol);
810
811         if (!NT_STATUS_IS_OK(result))
812                 goto done;
813
814         got_connect_pol = True;
815
816         /* Get domain policy handle */
817
818         result = rpccli_samr_OpenDomain(cli, mem_ctx,
819                                         &connect_pol,
820                                         access_mask,
821                                         &domain_sid,
822                                         &domain_pol);
823
824         if (!NT_STATUS_IS_OK(result))
825                 goto done;
826
827         got_domain_pol = True;
828
829         /* Enumerate domain users */
830
831         start_idx = 0;
832         size = 0xffff;
833
834         do {
835                 result = rpccli_samr_EnumDomainUsers(cli, mem_ctx,
836                                                      &domain_pol,
837                                                      &start_idx,
838                                                      acb_mask,
839                                                      &dom_users,
840                                                      size,
841                                                      &num_dom_users);
842
843                 if (NT_STATUS_IS_OK(result) ||
844                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
845
846                         for (i = 0; i < num_dom_users; i++)
847                                printf("user:[%s] rid:[0x%x]\n",
848                                        dom_users->entries[i].name.string,
849                                        dom_users->entries[i].idx);
850                 }
851
852         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
853
854  done:
855         if (got_domain_pol)
856                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
857
858         if (got_connect_pol)
859                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
860
861         return result;
862 }
863
864 /* Enumerate domain groups */
865
866 static NTSTATUS cmd_samr_enum_dom_groups(struct rpc_pipe_client *cli,
867                                          TALLOC_CTX *mem_ctx,
868                                          int argc, const char **argv)
869 {
870         POLICY_HND connect_pol, domain_pol;
871         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
872         uint32 start_idx, size, num_dom_groups, i;
873         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
874         struct samr_SamArray *dom_groups = NULL;
875         bool got_connect_pol = False, got_domain_pol = False;
876
877         if ((argc < 1) || (argc > 2)) {
878                 printf("Usage: %s [access_mask]\n", argv[0]);
879                 return NT_STATUS_OK;
880         }
881
882         if (argc > 1)
883                 sscanf(argv[1], "%x", &access_mask);
884
885         /* Get sam policy handle */
886
887         result = rpccli_try_samr_connects(cli, mem_ctx,
888                                           MAXIMUM_ALLOWED_ACCESS,
889                                           &connect_pol);
890
891         if (!NT_STATUS_IS_OK(result))
892                 goto done;
893
894         got_connect_pol = True;
895
896         /* Get domain policy handle */
897
898         result = rpccli_samr_OpenDomain(cli, mem_ctx,
899                                         &connect_pol,
900                                         access_mask,
901                                         &domain_sid,
902                                         &domain_pol);
903
904         if (!NT_STATUS_IS_OK(result))
905                 goto done;
906
907         got_domain_pol = True;
908
909         /* Enumerate domain groups */
910
911         start_idx = 0;
912         size = 0xffff;
913
914         do {
915                 result = rpccli_samr_EnumDomainGroups(cli, mem_ctx,
916                                                       &domain_pol,
917                                                       &start_idx,
918                                                       &dom_groups,
919                                                       size,
920                                                       &num_dom_groups);
921                 if (NT_STATUS_IS_OK(result) ||
922                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
923
924                         for (i = 0; i < num_dom_groups; i++)
925                                 printf("group:[%s] rid:[0x%x]\n",
926                                        dom_groups->entries[i].name.string,
927                                        dom_groups->entries[i].idx);
928                 }
929
930         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
931
932  done:
933         if (got_domain_pol)
934                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
935
936         if (got_connect_pol)
937                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
938
939         return result;
940 }
941
942 /* Enumerate alias groups */
943
944 static NTSTATUS cmd_samr_enum_als_groups(struct rpc_pipe_client *cli,
945                                          TALLOC_CTX *mem_ctx,
946                                          int argc, const char **argv)
947 {
948         POLICY_HND connect_pol, domain_pol;
949         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
950         uint32 start_idx, size, num_als_groups, i;
951         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
952         struct samr_SamArray *als_groups = NULL;
953         bool got_connect_pol = False, got_domain_pol = False;
954
955         if ((argc < 2) || (argc > 3)) {
956                 printf("Usage: %s builtin|domain [access mask]\n", argv[0]);
957                 return NT_STATUS_OK;
958         }
959
960         if (argc > 2)
961                 sscanf(argv[2], "%x", &access_mask);
962
963         /* Get sam policy handle */
964
965         result = rpccli_try_samr_connects(cli, mem_ctx,
966                                           MAXIMUM_ALLOWED_ACCESS,
967                                           &connect_pol);
968
969         if (!NT_STATUS_IS_OK(result))
970                 goto done;
971
972         got_connect_pol = True;
973
974         /* Get domain policy handle */
975
976         result = get_domain_handle(cli, mem_ctx, argv[1],
977                                    &connect_pol,
978                                    access_mask,
979                                    &domain_sid,
980                                    &domain_pol);
981
982         if (!NT_STATUS_IS_OK(result))
983                 goto done;
984
985         got_domain_pol = True;
986
987         /* Enumerate alias groups */
988
989         start_idx = 0;
990         size = 0xffff;          /* Number of groups to retrieve */
991
992         do {
993                 result = rpccli_samr_EnumDomainAliases(cli, mem_ctx,
994                                                        &domain_pol,
995                                                        &start_idx,
996                                                        &als_groups,
997                                                        size,
998                                                        &num_als_groups);
999
1000                 if (NT_STATUS_IS_OK(result) ||
1001                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1002
1003                         for (i = 0; i < num_als_groups; i++)
1004                                 printf("group:[%s] rid:[0x%x]\n",
1005                                        als_groups->entries[i].name.string,
1006                                        als_groups->entries[i].idx);
1007                 }
1008         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1009
1010  done:
1011         if (got_domain_pol)
1012                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1013
1014         if (got_connect_pol)
1015                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1016
1017         return result;
1018 }
1019
1020 /* Enumerate domains */
1021
1022 static NTSTATUS cmd_samr_enum_domains(struct rpc_pipe_client *cli,
1023                                       TALLOC_CTX *mem_ctx,
1024                                       int argc, const char **argv)
1025 {
1026         POLICY_HND connect_pol;
1027         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1028         uint32 start_idx, size, num_entries, i;
1029         uint32 access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1030         bool got_connect_pol = false;
1031         struct samr_SamArray *sam = NULL;
1032
1033         if ((argc < 1) || (argc > 2)) {
1034                 printf("Usage: %s [access mask]\n", argv[0]);
1035                 return NT_STATUS_OK;
1036         }
1037
1038         if (argc > 1) {
1039                 sscanf(argv[1], "%x", &access_mask);
1040         }
1041
1042         /* Get sam policy handle */
1043
1044         result = rpccli_try_samr_connects(cli, mem_ctx,
1045                                           access_mask,
1046                                           &connect_pol);
1047
1048         if (!NT_STATUS_IS_OK(result)) {
1049                 goto done;
1050         }
1051
1052         got_connect_pol = true;
1053
1054         /* Enumerate alias groups */
1055
1056         start_idx = 0;
1057         size = 0xffff;
1058
1059         do {
1060                 result = rpccli_samr_EnumDomains(cli, mem_ctx,
1061                                                  &connect_pol,
1062                                                  &start_idx,
1063                                                  &sam,
1064                                                  size,
1065                                                  &num_entries);
1066
1067                 if (NT_STATUS_IS_OK(result) ||
1068                     NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES)) {
1069
1070                         for (i = 0; i < num_entries; i++)
1071                                 printf("name:[%s] idx:[0x%x]\n",
1072                                        sam->entries[i].name.string,
1073                                        sam->entries[i].idx);
1074                 }
1075         } while (NT_STATUS_V(result) == NT_STATUS_V(STATUS_MORE_ENTRIES));
1076
1077  done:
1078         if (got_connect_pol) {
1079                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1080         }
1081
1082         return result;
1083 }
1084
1085
1086 /* Query alias membership */
1087
1088 static NTSTATUS cmd_samr_query_aliasmem(struct rpc_pipe_client *cli,
1089                                         TALLOC_CTX *mem_ctx,
1090                                         int argc, const char **argv)
1091 {
1092         POLICY_HND connect_pol, domain_pol, alias_pol;
1093         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1094         uint32 alias_rid, i;
1095         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1096         struct lsa_SidArray sid_array;
1097
1098         if ((argc < 3) || (argc > 4)) {
1099                 printf("Usage: %s builtin|domain rid [access mask]\n", argv[0]);
1100                 return NT_STATUS_OK;
1101         }
1102
1103         sscanf(argv[2], "%i", &alias_rid);
1104
1105         if (argc > 3)
1106                 sscanf(argv[3], "%x", &access_mask);
1107
1108         /* Open SAMR handle */
1109
1110         result = rpccli_try_samr_connects(cli, mem_ctx,
1111                                           MAXIMUM_ALLOWED_ACCESS,
1112                                           &connect_pol);
1113
1114         if (!NT_STATUS_IS_OK(result))
1115                 goto done;
1116
1117         /* Open handle on domain */
1118
1119         result = get_domain_handle(cli, mem_ctx, argv[1],
1120                                    &connect_pol,
1121                                    MAXIMUM_ALLOWED_ACCESS,
1122                                    &domain_sid,
1123                                    &domain_pol);
1124
1125         if (!NT_STATUS_IS_OK(result))
1126                 goto done;
1127
1128         /* Open handle on alias */
1129
1130         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1131                                        &domain_pol,
1132                                        access_mask,
1133                                        alias_rid,
1134                                        &alias_pol);
1135         if (!NT_STATUS_IS_OK(result))
1136                 goto done;
1137
1138         result = rpccli_samr_GetMembersInAlias(cli, mem_ctx,
1139                                                &alias_pol,
1140                                                &sid_array);
1141
1142         if (!NT_STATUS_IS_OK(result))
1143                 goto done;
1144
1145         for (i = 0; i < sid_array.num_sids; i++) {
1146                 fstring sid_str;
1147
1148                 sid_to_fstring(sid_str, sid_array.sids[i].sid);
1149                 printf("\tsid:[%s]\n", sid_str);
1150         }
1151
1152         rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1153         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1154         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1155  done:
1156         return result;
1157 }
1158
1159 /* Query alias info */
1160
1161 static NTSTATUS cmd_samr_query_aliasinfo(struct rpc_pipe_client *cli,
1162                                          TALLOC_CTX *mem_ctx,
1163                                          int argc, const char **argv)
1164 {
1165         POLICY_HND connect_pol, domain_pol, alias_pol;
1166         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1167         uint32_t alias_rid;
1168         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1169         union samr_AliasInfo *info = NULL;
1170         enum samr_AliasInfoEnum level = ALIASINFOALL;
1171
1172         if ((argc < 3) || (argc > 4)) {
1173                 printf("Usage: %s builtin|domain rid [level] [access mask]\n",
1174                         argv[0]);
1175                 return NT_STATUS_OK;
1176         }
1177
1178         sscanf(argv[2], "%i", &alias_rid);
1179
1180         if (argc > 2) {
1181                 level = atoi(argv[3]);
1182         }
1183
1184         if (argc > 3) {
1185                 sscanf(argv[4], "%x", &access_mask);
1186         }
1187
1188         /* Open SAMR handle */
1189
1190         result = rpccli_try_samr_connects(cli, mem_ctx,
1191                                           SEC_FLAG_MAXIMUM_ALLOWED,
1192                                           &connect_pol);
1193
1194         if (!NT_STATUS_IS_OK(result)) {
1195                 goto done;
1196         }
1197
1198         /* Open handle on domain */
1199
1200         result = get_domain_handle(cli, mem_ctx, argv[1],
1201                                    &connect_pol,
1202                                    SEC_FLAG_MAXIMUM_ALLOWED,
1203                                    &domain_sid,
1204                                    &domain_pol);
1205
1206         if (!NT_STATUS_IS_OK(result)) {
1207                 goto done;
1208         }
1209
1210         /* Open handle on alias */
1211
1212         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1213                                        &domain_pol,
1214                                        access_mask,
1215                                        alias_rid,
1216                                        &alias_pol);
1217         if (!NT_STATUS_IS_OK(result)) {
1218                 goto done;
1219         }
1220
1221         result = rpccli_samr_QueryAliasInfo(cli, mem_ctx,
1222                                             &alias_pol,
1223                                             level,
1224                                             &info);
1225
1226         if (!NT_STATUS_IS_OK(result)) {
1227                 goto done;
1228         }
1229
1230         switch (level) {
1231                 case ALIASINFOALL:
1232                         printf("Name: %s\n", info->all.name.string);
1233                         printf("Description: %s\n", info->all.description.string);
1234                         printf("Num Members: %d\n", info->all.num_members);
1235                         break;
1236                 case ALIASINFONAME:
1237                         printf("Name: %s\n", info->name.string);
1238                         break;
1239                 case ALIASINFODESCRIPTION:
1240                         printf("Description: %s\n", info->description.string);
1241                         break;
1242                 default:
1243                         break;
1244         }
1245
1246         rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1247         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1248         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1249  done:
1250         return result;
1251 }
1252
1253
1254 /* Query delete an alias membership */
1255
1256 static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
1257                                       TALLOC_CTX *mem_ctx,
1258                                       int argc, const char **argv)
1259 {
1260         POLICY_HND connect_pol, domain_pol, alias_pol;
1261         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1262         uint32 alias_rid;
1263         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1264
1265         if (argc != 3) {
1266                 printf("Usage: %s builtin|domain [rid|name]\n", argv[0]);
1267                 return NT_STATUS_OK;
1268         }
1269
1270         alias_rid = strtoul(argv[2], NULL, 10);
1271
1272         /* Open SAMR handle */
1273
1274         result = rpccli_try_samr_connects(cli, mem_ctx,
1275                                           MAXIMUM_ALLOWED_ACCESS,
1276                                           &connect_pol);
1277
1278         if (!NT_STATUS_IS_OK(result))
1279                 goto done;
1280
1281         /* Open handle on domain */
1282
1283         result = get_domain_handle(cli, mem_ctx, argv[1],
1284                                    &connect_pol,
1285                                    MAXIMUM_ALLOWED_ACCESS,
1286                                    &domain_sid,
1287                                    &domain_pol);
1288
1289         if (!NT_STATUS_IS_OK(result))
1290                 goto done;
1291
1292         /* Open handle on alias */
1293
1294         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1295                                        &domain_pol,
1296                                        access_mask,
1297                                        alias_rid,
1298                                        &alias_pol);
1299         if (!NT_STATUS_IS_OK(result) && (alias_rid == 0)) {
1300                 /* Probably this was a user name, try lookupnames */
1301                 struct samr_Ids rids, types;
1302                 struct lsa_String lsa_acct_name;
1303
1304                 init_lsa_String(&lsa_acct_name, argv[2]);
1305
1306                 result = rpccli_samr_LookupNames(cli, mem_ctx,
1307                                                  &domain_pol,
1308                                                  1,
1309                                                  &lsa_acct_name,
1310                                                  &rids,
1311                                                  &types);
1312
1313                 if (NT_STATUS_IS_OK(result)) {
1314                         result = rpccli_samr_OpenAlias(cli, mem_ctx,
1315                                                        &domain_pol,
1316                                                        access_mask,
1317                                                        rids.ids[0],
1318                                                        &alias_pol);
1319                 }
1320         }
1321
1322         result = rpccli_samr_DeleteDomAlias(cli, mem_ctx,
1323                                             &alias_pol);
1324
1325         if (!NT_STATUS_IS_OK(result))
1326                 goto done;
1327
1328         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1329         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1330  done:
1331         return result;
1332 }
1333
1334 /* Query display info */
1335
1336 static NTSTATUS cmd_samr_query_dispinfo_internal(struct rpc_pipe_client *cli,
1337                                                  TALLOC_CTX *mem_ctx,
1338                                                  int argc, const char **argv,
1339                                                  uint32_t opcode)
1340 {
1341         POLICY_HND connect_pol, domain_pol;
1342         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1343         uint32 start_idx=0, max_entries=250, max_size = 0xffff, num_entries = 0, i;
1344         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1345         uint32 info_level = 1;
1346         union samr_DispInfo info;
1347         int loop_count = 0;
1348         bool got_params = False; /* Use get_query_dispinfo_params() or not? */
1349         uint32_t total_size, returned_size;
1350
1351         if (argc > 6) {
1352                 printf("Usage: %s [info level] [start index] [max entries] [max size] [access mask]\n", argv[0]);
1353                 return NT_STATUS_OK;
1354         }
1355
1356         if (argc >= 2)
1357                 sscanf(argv[1], "%i", &info_level);
1358
1359         if (argc >= 3)
1360                 sscanf(argv[2], "%i", &start_idx);
1361
1362         if (argc >= 4) {
1363                 sscanf(argv[3], "%i", &max_entries);
1364                 got_params = True;
1365         }
1366
1367         if (argc >= 5) {
1368                 sscanf(argv[4], "%i", &max_size);
1369                 got_params = True;
1370         }
1371
1372         if (argc >= 6)
1373                 sscanf(argv[5], "%x", &access_mask);
1374
1375         /* Get sam policy handle */
1376
1377         result = rpccli_try_samr_connects(cli, mem_ctx,
1378                                           MAXIMUM_ALLOWED_ACCESS,
1379                                           &connect_pol);
1380
1381         if (!NT_STATUS_IS_OK(result))
1382                 goto done;
1383
1384         /* Get domain policy handle */
1385
1386         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1387                                         &connect_pol,
1388                                         access_mask,
1389                                         &domain_sid,
1390                                         &domain_pol);
1391
1392         if (!NT_STATUS_IS_OK(result))
1393                 goto done;
1394
1395         /* Query display info */
1396
1397         do {
1398
1399                 if (!got_params)
1400                         get_query_dispinfo_params(
1401                                 loop_count, &max_entries, &max_size);
1402
1403                 switch (opcode) {
1404                 case NDR_SAMR_QUERYDISPLAYINFO:
1405                         result = rpccli_samr_QueryDisplayInfo(cli, mem_ctx,
1406                                                               &domain_pol,
1407                                                               info_level,
1408                                                               start_idx,
1409                                                               max_entries,
1410                                                               max_size,
1411                                                               &total_size,
1412                                                               &returned_size,
1413                                                               &info);
1414                         break;
1415                 case NDR_SAMR_QUERYDISPLAYINFO2:
1416                         result = rpccli_samr_QueryDisplayInfo2(cli, mem_ctx,
1417                                                                &domain_pol,
1418                                                                info_level,
1419                                                                start_idx,
1420                                                                max_entries,
1421                                                                max_size,
1422                                                                &total_size,
1423                                                                &returned_size,
1424                                                                &info);
1425
1426                         break;
1427                 case NDR_SAMR_QUERYDISPLAYINFO3:
1428                         result = rpccli_samr_QueryDisplayInfo3(cli, mem_ctx,
1429                                                                &domain_pol,
1430                                                                info_level,
1431                                                                start_idx,
1432                                                                max_entries,
1433                                                                max_size,
1434                                                                &total_size,
1435                                                                &returned_size,
1436                                                                &info);
1437
1438                         break;
1439                 default:
1440                         return NT_STATUS_INVALID_PARAMETER;
1441                 }
1442
1443                 if (!NT_STATUS_IS_OK(result) &&
1444                     !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
1445                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
1446                         break;
1447                 }
1448
1449                 loop_count++;
1450
1451                 switch (info_level) {
1452                         case 1:
1453                                 num_entries = info.info1.count;
1454                                 break;
1455                         case 2:
1456                                 num_entries = info.info2.count;
1457                                 break;
1458                         case 3:
1459                                 num_entries = info.info3.count;
1460                                 break;
1461                         case 4:
1462                                 num_entries = info.info4.count;
1463                                 break;
1464                         case 5:
1465                                 num_entries = info.info5.count;
1466                                 break;
1467                         default:
1468                                 break;
1469                 }
1470
1471                 start_idx += num_entries;
1472
1473                 if (num_entries == 0)
1474                         break;
1475
1476                 for (i = 0; i < num_entries; i++) {
1477                         switch (info_level) {
1478                         case 1:
1479                                 display_sam_info_1(&info.info1.entries[i]);
1480                                 break;
1481                         case 2:
1482                                 display_sam_info_2(&info.info2.entries[i]);
1483                                 break;
1484                         case 3:
1485                                 display_sam_info_3(&info.info3.entries[i]);
1486                                 break;
1487                         case 4:
1488                                 display_sam_info_4(&info.info4.entries[i]);
1489                                 break;
1490                         case 5:
1491                                 display_sam_info_5(&info.info5.entries[i]);
1492                                 break;
1493                         }
1494                 }
1495         } while ( NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1496
1497         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1498         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1499  done:
1500         return result;
1501 }
1502
1503 static NTSTATUS cmd_samr_query_dispinfo(struct rpc_pipe_client *cli,
1504                                         TALLOC_CTX *mem_ctx,
1505                                         int argc, const char **argv)
1506 {
1507         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1508                                                 NDR_SAMR_QUERYDISPLAYINFO);
1509 }
1510
1511 static NTSTATUS cmd_samr_query_dispinfo2(struct rpc_pipe_client *cli,
1512                                          TALLOC_CTX *mem_ctx,
1513                                          int argc, const char **argv)
1514 {
1515         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1516                                                 NDR_SAMR_QUERYDISPLAYINFO2);
1517 }
1518
1519 static NTSTATUS cmd_samr_query_dispinfo3(struct rpc_pipe_client *cli,
1520                                          TALLOC_CTX *mem_ctx,
1521                                          int argc, const char **argv)
1522 {
1523         return cmd_samr_query_dispinfo_internal(cli, mem_ctx, argc, argv,
1524                                                 NDR_SAMR_QUERYDISPLAYINFO3);
1525 }
1526
1527 /* Query domain info */
1528
1529 static NTSTATUS cmd_samr_query_dominfo(struct rpc_pipe_client *cli,
1530                                        TALLOC_CTX *mem_ctx,
1531                                        int argc, const char **argv)
1532 {
1533         POLICY_HND connect_pol, domain_pol;
1534         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1535         uint32 switch_level = 2;
1536         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1537         union samr_DomainInfo *info = NULL;
1538
1539         if (argc > 3) {
1540                 printf("Usage: %s [info level] [access mask]\n", argv[0]);
1541                 return NT_STATUS_OK;
1542         }
1543
1544         if (argc > 1)
1545                 sscanf(argv[1], "%i", &switch_level);
1546
1547         if (argc > 2)
1548                 sscanf(argv[2], "%x", &access_mask);
1549
1550         /* Get sam policy handle */
1551
1552         result = rpccli_try_samr_connects(cli, mem_ctx,
1553                                           MAXIMUM_ALLOWED_ACCESS,
1554                                           &connect_pol);
1555
1556         if (!NT_STATUS_IS_OK(result))
1557                 goto done;
1558
1559         /* Get domain policy handle */
1560
1561         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1562                                         &connect_pol,
1563                                         access_mask,
1564                                         &domain_sid,
1565                                         &domain_pol);
1566
1567         if (!NT_STATUS_IS_OK(result))
1568                 goto done;
1569
1570         /* Query domain info */
1571
1572         result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
1573                                              &domain_pol,
1574                                              switch_level,
1575                                              &info);
1576
1577         if (!NT_STATUS_IS_OK(result))
1578                 goto done;
1579
1580         /* Display domain info */
1581
1582         switch (switch_level) {
1583         case 1:
1584                 display_sam_dom_info_1(&info->info1);
1585                 break;
1586         case 2:
1587                 display_sam_dom_info_2(&info->general);
1588                 break;
1589         case 3:
1590                 display_sam_dom_info_3(&info->info3);
1591                 break;
1592         case 4:
1593                 display_sam_dom_info_4(&info->oem);
1594                 break;
1595         case 5:
1596                 display_sam_dom_info_5(&info->info5);
1597                 break;
1598         case 6:
1599                 display_sam_dom_info_6(&info->info6);
1600                 break;
1601         case 7:
1602                 display_sam_dom_info_7(&info->info7);
1603                 break;
1604         case 8:
1605                 display_sam_dom_info_8(&info->info8);
1606                 break;
1607         case 9:
1608                 display_sam_dom_info_9(&info->info9);
1609                 break;
1610         case 12:
1611                 display_sam_dom_info_12(&info->info12);
1612                 break;
1613         case 13:
1614                 display_sam_dom_info_13(&info->info13);
1615                 break;
1616
1617         default:
1618                 printf("cannot display domain info for switch value %d\n",
1619                        switch_level);
1620                 break;
1621         }
1622
1623  done:
1624
1625         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1626         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1627         return result;
1628 }
1629
1630 /* Create domain user */
1631
1632 static NTSTATUS cmd_samr_create_dom_user(struct rpc_pipe_client *cli,
1633                                          TALLOC_CTX *mem_ctx,
1634                                          int argc, const char **argv)
1635 {
1636         POLICY_HND connect_pol, domain_pol, user_pol;
1637         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1638         struct lsa_String acct_name;
1639         uint32 acb_info;
1640         uint32 acct_flags, user_rid;
1641         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1642         uint32_t access_granted = 0;
1643
1644         if ((argc < 2) || (argc > 3)) {
1645                 printf("Usage: %s username [access mask]\n", argv[0]);
1646                 return NT_STATUS_OK;
1647         }
1648
1649         init_lsa_String(&acct_name, argv[1]);
1650
1651         if (argc > 2)
1652                 sscanf(argv[2], "%x", &access_mask);
1653
1654         /* Get sam policy handle */
1655
1656         result = rpccli_try_samr_connects(cli, mem_ctx,
1657                                           MAXIMUM_ALLOWED_ACCESS,
1658                                           &connect_pol);
1659
1660         if (!NT_STATUS_IS_OK(result))
1661                 goto done;
1662
1663         /* Get domain policy handle */
1664
1665         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1666                                         &connect_pol,
1667                                         access_mask,
1668                                         &domain_sid,
1669                                         &domain_pol);
1670
1671         if (!NT_STATUS_IS_OK(result))
1672                 goto done;
1673
1674         /* Create domain user */
1675
1676         acb_info = ACB_NORMAL;
1677         acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
1678                      SEC_STD_WRITE_DAC | SEC_STD_DELETE |
1679                      SAMR_USER_ACCESS_SET_PASSWORD |
1680                      SAMR_USER_ACCESS_GET_ATTRIBUTES |
1681                      SAMR_USER_ACCESS_SET_ATTRIBUTES;
1682
1683         result = rpccli_samr_CreateUser2(cli, mem_ctx,
1684                                          &domain_pol,
1685                                          &acct_name,
1686                                          acb_info,
1687                                          acct_flags,
1688                                          &user_pol,
1689                                          &access_granted,
1690                                          &user_rid);
1691
1692         if (!NT_STATUS_IS_OK(result))
1693                 goto done;
1694
1695         result = rpccli_samr_Close(cli, mem_ctx, &user_pol);
1696         if (!NT_STATUS_IS_OK(result)) goto done;
1697
1698         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1699         if (!NT_STATUS_IS_OK(result)) goto done;
1700
1701         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1702         if (!NT_STATUS_IS_OK(result)) goto done;
1703
1704  done:
1705         return result;
1706 }
1707
1708 /* Create domain group */
1709
1710 static NTSTATUS cmd_samr_create_dom_group(struct rpc_pipe_client *cli,
1711                                           TALLOC_CTX *mem_ctx,
1712                                           int argc, const char **argv)
1713 {
1714         POLICY_HND connect_pol, domain_pol, group_pol;
1715         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1716         struct lsa_String grp_name;
1717         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1718         uint32_t rid = 0;
1719
1720         if ((argc < 2) || (argc > 3)) {
1721                 printf("Usage: %s groupname [access mask]\n", argv[0]);
1722                 return NT_STATUS_OK;
1723         }
1724
1725         init_lsa_String(&grp_name, argv[1]);
1726
1727         if (argc > 2)
1728                 sscanf(argv[2], "%x", &access_mask);
1729
1730         /* Get sam policy handle */
1731
1732         result = rpccli_try_samr_connects(cli, mem_ctx,
1733                                           MAXIMUM_ALLOWED_ACCESS,
1734                                           &connect_pol);
1735
1736         if (!NT_STATUS_IS_OK(result))
1737                 goto done;
1738
1739         /* Get domain policy handle */
1740
1741         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1742                                         &connect_pol,
1743                                         access_mask,
1744                                         &domain_sid,
1745                                         &domain_pol);
1746
1747         if (!NT_STATUS_IS_OK(result))
1748                 goto done;
1749
1750         /* Create domain user */
1751         result = rpccli_samr_CreateDomainGroup(cli, mem_ctx,
1752                                                &domain_pol,
1753                                                &grp_name,
1754                                                MAXIMUM_ALLOWED_ACCESS,
1755                                                &group_pol,
1756                                                &rid);
1757
1758         if (!NT_STATUS_IS_OK(result))
1759                 goto done;
1760
1761         result = rpccli_samr_Close(cli, mem_ctx, &group_pol);
1762         if (!NT_STATUS_IS_OK(result)) goto done;
1763
1764         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1765         if (!NT_STATUS_IS_OK(result)) goto done;
1766
1767         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1768         if (!NT_STATUS_IS_OK(result)) goto done;
1769
1770  done:
1771         return result;
1772 }
1773
1774 /* Create domain alias */
1775
1776 static NTSTATUS cmd_samr_create_dom_alias(struct rpc_pipe_client *cli,
1777                                           TALLOC_CTX *mem_ctx,
1778                                           int argc, const char **argv)
1779 {
1780         POLICY_HND connect_pol, domain_pol, alias_pol;
1781         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1782         struct lsa_String alias_name;
1783         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
1784         uint32_t rid = 0;
1785
1786         if ((argc < 2) || (argc > 3)) {
1787                 printf("Usage: %s aliasname [access mask]\n", argv[0]);
1788                 return NT_STATUS_OK;
1789         }
1790
1791         init_lsa_String(&alias_name, argv[1]);
1792
1793         if (argc > 2)
1794                 sscanf(argv[2], "%x", &access_mask);
1795
1796         /* Get sam policy handle */
1797
1798         result = rpccli_try_samr_connects(cli, mem_ctx,
1799                                           MAXIMUM_ALLOWED_ACCESS,
1800                                           &connect_pol);
1801
1802         if (!NT_STATUS_IS_OK(result))
1803                 goto done;
1804
1805         /* Get domain policy handle */
1806
1807         result = rpccli_samr_OpenDomain(cli, mem_ctx,
1808                                         &connect_pol,
1809                                         access_mask,
1810                                         &domain_sid,
1811                                         &domain_pol);
1812
1813         if (!NT_STATUS_IS_OK(result))
1814                 goto done;
1815
1816         /* Create domain user */
1817
1818         result = rpccli_samr_CreateDomAlias(cli, mem_ctx,
1819                                             &domain_pol,
1820                                             &alias_name,
1821                                             MAXIMUM_ALLOWED_ACCESS,
1822                                             &alias_pol,
1823                                             &rid);
1824
1825         if (!NT_STATUS_IS_OK(result))
1826                 goto done;
1827
1828         result = rpccli_samr_Close(cli, mem_ctx, &alias_pol);
1829         if (!NT_STATUS_IS_OK(result)) goto done;
1830
1831         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1832         if (!NT_STATUS_IS_OK(result)) goto done;
1833
1834         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1835         if (!NT_STATUS_IS_OK(result)) goto done;
1836
1837  done:
1838         return result;
1839 }
1840
1841 /* Lookup sam names */
1842
1843 static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
1844                                       TALLOC_CTX *mem_ctx,
1845                                       int argc, const char **argv)
1846 {
1847         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1848         POLICY_HND connect_pol, domain_pol;
1849         uint32 num_names;
1850         struct samr_Ids rids, name_types;
1851         int i;
1852         struct lsa_String *names = NULL;;
1853
1854         if (argc < 3) {
1855                 printf("Usage: %s  domain|builtin name1 [name2 [name3] [...]]\n", argv[0]);
1856                 printf("check on the domain SID: S-1-5-21-x-y-z\n");
1857                 printf("or check on the builtin SID: S-1-5-32\n");
1858                 return NT_STATUS_OK;
1859         }
1860
1861         /* Get sam policy and domain handles */
1862
1863         result = rpccli_try_samr_connects(cli, mem_ctx,
1864                                           MAXIMUM_ALLOWED_ACCESS,
1865                                           &connect_pol);
1866
1867         if (!NT_STATUS_IS_OK(result))
1868                 goto done;
1869
1870         result = get_domain_handle(cli, mem_ctx, argv[1],
1871                                    &connect_pol,
1872                                    MAXIMUM_ALLOWED_ACCESS,
1873                                    &domain_sid,
1874                                    &domain_pol);
1875
1876         if (!NT_STATUS_IS_OK(result))
1877                 goto done;
1878
1879         /* Look up names */
1880
1881         num_names = argc - 2;
1882
1883         if ((names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names)) == NULL) {
1884                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1885                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1886                 result = NT_STATUS_NO_MEMORY;
1887                 goto done;
1888         }
1889
1890         for (i = 0; i < num_names; i++) {
1891                 init_lsa_String(&names[i], argv[i + 2]);
1892         }
1893
1894         result = rpccli_samr_LookupNames(cli, mem_ctx,
1895                                          &domain_pol,
1896                                          num_names,
1897                                          names,
1898                                          &rids,
1899                                          &name_types);
1900
1901         if (!NT_STATUS_IS_OK(result))
1902                 goto done;
1903
1904         /* Display results */
1905
1906         for (i = 0; i < num_names; i++)
1907                 printf("name %s: 0x%x (%d)\n", names[i].string, rids.ids[i],
1908                        name_types.ids[i]);
1909
1910         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1911         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1912  done:
1913         return result;
1914 }
1915
1916 /* Lookup sam rids */
1917
1918 static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
1919                                      TALLOC_CTX *mem_ctx,
1920                                      int argc, const char **argv)
1921 {
1922         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1923         POLICY_HND connect_pol, domain_pol;
1924         uint32_t num_rids, *rids;
1925         struct lsa_Strings names;
1926         struct samr_Ids types;
1927
1928         int i;
1929
1930         if (argc < 3) {
1931                 printf("Usage: %s domain|builtin rid1 [rid2 [rid3] [...]]\n", argv[0]);
1932                 return NT_STATUS_OK;
1933         }
1934
1935         /* Get sam policy and domain handles */
1936
1937         result = rpccli_try_samr_connects(cli, mem_ctx,
1938                                           MAXIMUM_ALLOWED_ACCESS,
1939                                           &connect_pol);
1940
1941         if (!NT_STATUS_IS_OK(result))
1942                 goto done;
1943
1944         result = get_domain_handle(cli, mem_ctx, argv[1],
1945                                    &connect_pol,
1946                                    MAXIMUM_ALLOWED_ACCESS,
1947                                    &domain_sid,
1948                                    &domain_pol);
1949
1950         if (!NT_STATUS_IS_OK(result))
1951                 goto done;
1952
1953         /* Look up rids */
1954
1955         num_rids = argc - 2;
1956
1957         if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1958                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1959                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1960                 result = NT_STATUS_NO_MEMORY;
1961                 goto done;
1962         }
1963
1964         for (i = 0; i < argc - 2; i++)
1965                 sscanf(argv[i + 2], "%i", &rids[i]);
1966
1967         result = rpccli_samr_LookupRids(cli, mem_ctx,
1968                                         &domain_pol,
1969                                         num_rids,
1970                                         rids,
1971                                         &names,
1972                                         &types);
1973
1974         if (!NT_STATUS_IS_OK(result) &&
1975             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
1976                 goto done;
1977
1978         /* Display results */
1979
1980         for (i = 0; i < num_rids; i++) {
1981                 printf("rid 0x%x: %s (%d)\n",
1982                         rids[i], names.names[i].string, types.ids[i]);
1983         }
1984
1985         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
1986         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
1987  done:
1988         return result;
1989 }
1990
1991 /* Delete domain group */
1992
1993 static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
1994                                          TALLOC_CTX *mem_ctx,
1995                                          int argc, const char **argv)
1996 {
1997         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1998         POLICY_HND connect_pol, domain_pol, group_pol;
1999         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2000
2001         if ((argc < 2) || (argc > 3)) {
2002                 printf("Usage: %s groupname\n", argv[0]);
2003                 return NT_STATUS_OK;
2004         }
2005
2006         if (argc > 2)
2007                 sscanf(argv[2], "%x", &access_mask);
2008
2009         /* Get sam policy and domain handles */
2010
2011         result = rpccli_try_samr_connects(cli, mem_ctx,
2012                                           MAXIMUM_ALLOWED_ACCESS,
2013                                           &connect_pol);
2014
2015         if (!NT_STATUS_IS_OK(result))
2016                 goto done;
2017
2018         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2019                                         &connect_pol,
2020                                         MAXIMUM_ALLOWED_ACCESS,
2021                                         &domain_sid,
2022                                         &domain_pol);
2023
2024         if (!NT_STATUS_IS_OK(result))
2025                 goto done;
2026
2027         /* Get handle on group */
2028
2029         {
2030                 struct samr_Ids group_rids, name_types;
2031                 struct lsa_String lsa_acct_name;
2032
2033                 init_lsa_String(&lsa_acct_name, argv[1]);
2034
2035                 result = rpccli_samr_LookupNames(cli, mem_ctx,
2036                                                  &domain_pol,
2037                                                  1,
2038                                                  &lsa_acct_name,
2039                                                  &group_rids,
2040                                                  &name_types);
2041                 if (!NT_STATUS_IS_OK(result))
2042                         goto done;
2043
2044                 result = rpccli_samr_OpenGroup(cli, mem_ctx,
2045                                                &domain_pol,
2046                                                access_mask,
2047                                                group_rids.ids[0],
2048                                                &group_pol);
2049
2050                 if (!NT_STATUS_IS_OK(result))
2051                         goto done;
2052         }
2053
2054         /* Delete group */
2055
2056         result = rpccli_samr_DeleteDomainGroup(cli, mem_ctx,
2057                                                &group_pol);
2058
2059         if (!NT_STATUS_IS_OK(result))
2060                 goto done;
2061
2062         /* Display results */
2063
2064         rpccli_samr_Close(cli, mem_ctx, &group_pol);
2065         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2066         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2067
2068  done:
2069         return result;
2070 }
2071
2072 /* Delete domain user */
2073
2074 static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
2075                                          TALLOC_CTX *mem_ctx,
2076                                          int argc, const char **argv)
2077 {
2078         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2079         POLICY_HND connect_pol, domain_pol, user_pol;
2080         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2081
2082         if ((argc < 2) || (argc > 3)) {
2083                 printf("Usage: %s username\n", argv[0]);
2084                 return NT_STATUS_OK;
2085         }
2086
2087         if (argc > 2)
2088                 sscanf(argv[2], "%x", &access_mask);
2089
2090         /* Get sam policy and domain handles */
2091
2092         result = rpccli_try_samr_connects(cli, mem_ctx,
2093                                           MAXIMUM_ALLOWED_ACCESS,
2094                                           &connect_pol);
2095
2096         if (!NT_STATUS_IS_OK(result))
2097                 goto done;
2098
2099         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2100                                         &connect_pol,
2101                                         MAXIMUM_ALLOWED_ACCESS,
2102                                         &domain_sid,
2103                                         &domain_pol);
2104
2105         if (!NT_STATUS_IS_OK(result))
2106                 goto done;
2107
2108         /* Get handle on user */
2109
2110         {
2111                 struct samr_Ids user_rids, name_types;
2112                 struct lsa_String lsa_acct_name;
2113
2114                 init_lsa_String(&lsa_acct_name, argv[1]);
2115
2116                 result = rpccli_samr_LookupNames(cli, mem_ctx,
2117                                                  &domain_pol,
2118                                                  1,
2119                                                  &lsa_acct_name,
2120                                                  &user_rids,
2121                                                  &name_types);
2122
2123                 if (!NT_STATUS_IS_OK(result))
2124                         goto done;
2125
2126                 result = rpccli_samr_OpenUser(cli, mem_ctx,
2127                                               &domain_pol,
2128                                               access_mask,
2129                                               user_rids.ids[0],
2130                                               &user_pol);
2131
2132                 if (!NT_STATUS_IS_OK(result))
2133                         goto done;
2134         }
2135
2136         /* Delete user */
2137
2138         result = rpccli_samr_DeleteUser(cli, mem_ctx,
2139                                         &user_pol);
2140
2141         if (!NT_STATUS_IS_OK(result))
2142                 goto done;
2143
2144         /* Display results */
2145
2146         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2147         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2148         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2149
2150  done:
2151         return result;
2152 }
2153
2154 /**********************************************************************
2155  * Query user security object
2156  */
2157 static NTSTATUS cmd_samr_query_sec_obj(struct rpc_pipe_client *cli,
2158                                     TALLOC_CTX *mem_ctx,
2159                                     int argc, const char **argv)
2160 {
2161         POLICY_HND connect_pol, domain_pol, user_pol, *pol;
2162         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2163         uint32 sec_info = DACL_SECURITY_INFORMATION;
2164         uint32 user_rid = 0;
2165         TALLOC_CTX *ctx = NULL;
2166         SEC_DESC_BUF *sec_desc_buf=NULL;
2167         bool domain = False;
2168
2169         ctx=talloc_init("cmd_samr_query_sec_obj");
2170
2171         if ((argc < 1) || (argc > 3)) {
2172                 printf("Usage: %s [rid|-d] [sec_info]\n", argv[0]);
2173                 printf("\tSpecify rid for security on user, -d for security on domain\n");
2174                 talloc_destroy(ctx);
2175                 return NT_STATUS_OK;
2176         }
2177
2178         if (argc > 1) {
2179                 if (strcmp(argv[1], "-d") == 0)
2180                         domain = True;
2181                 else
2182                         sscanf(argv[1], "%i", &user_rid);
2183         }
2184
2185         if (argc == 3) {
2186                 sec_info = atoi(argv[2]);
2187         }
2188
2189         result = rpccli_try_samr_connects(cli, mem_ctx,
2190                                           MAXIMUM_ALLOWED_ACCESS,
2191                                           &connect_pol);
2192
2193         if (!NT_STATUS_IS_OK(result))
2194                 goto done;
2195
2196         if (domain || user_rid)
2197                 result = rpccli_samr_OpenDomain(cli, mem_ctx,
2198                                                 &connect_pol,
2199                                                 MAXIMUM_ALLOWED_ACCESS,
2200                                                 &domain_sid,
2201                                                 &domain_pol);
2202
2203         if (!NT_STATUS_IS_OK(result))
2204                 goto done;
2205
2206         if (user_rid)
2207                 result = rpccli_samr_OpenUser(cli, mem_ctx,
2208                                               &domain_pol,
2209                                               MAXIMUM_ALLOWED_ACCESS,
2210                                               user_rid,
2211                                               &user_pol);
2212
2213         if (!NT_STATUS_IS_OK(result))
2214                 goto done;
2215
2216         /* Pick which query pol to use */
2217
2218         pol = &connect_pol;
2219
2220         if (domain)
2221                 pol = &domain_pol;
2222
2223         if (user_rid)
2224                 pol = &user_pol;
2225
2226         /* Query SAM security object */
2227
2228         result = rpccli_samr_QuerySecurity(cli, mem_ctx,
2229                                            pol,
2230                                            sec_info,
2231                                            &sec_desc_buf);
2232
2233         if (!NT_STATUS_IS_OK(result))
2234                 goto done;
2235
2236         display_sec_desc(sec_desc_buf->sd);
2237
2238         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2239         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2240         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2241 done:
2242         talloc_destroy(ctx);
2243         return result;
2244 }
2245
2246 static NTSTATUS cmd_samr_get_usrdom_pwinfo(struct rpc_pipe_client *cli,
2247                                            TALLOC_CTX *mem_ctx,
2248                                            int argc, const char **argv)
2249 {
2250         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2251         POLICY_HND connect_pol, domain_pol, user_pol;
2252         struct samr_PwInfo info;
2253         uint32_t rid;
2254
2255         if (argc != 2) {
2256                 printf("Usage: %s rid\n", argv[0]);
2257                 return NT_STATUS_OK;
2258         }
2259
2260         sscanf(argv[1], "%i", &rid);
2261
2262         result = rpccli_try_samr_connects(cli, mem_ctx,
2263                                           MAXIMUM_ALLOWED_ACCESS,
2264                                           &connect_pol);
2265
2266         if (!NT_STATUS_IS_OK(result)) {
2267                 goto done;
2268         }
2269
2270         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2271                                         &connect_pol,
2272                                         MAXIMUM_ALLOWED_ACCESS,
2273                                         &domain_sid,
2274                                         &domain_pol);
2275
2276         if (!NT_STATUS_IS_OK(result)) {
2277                 goto done;
2278         }
2279
2280         result = rpccli_samr_OpenUser(cli, mem_ctx,
2281                                       &domain_pol,
2282                                       MAXIMUM_ALLOWED_ACCESS,
2283                                       rid,
2284                                       &user_pol);
2285
2286         if (!NT_STATUS_IS_OK(result)) {
2287                 goto done;
2288         }
2289
2290         result = rpccli_samr_GetUserPwInfo(cli, mem_ctx, &user_pol, &info);
2291         if (NT_STATUS_IS_OK(result)) {
2292                 printf("min_password_length: %d\n", info.min_password_length);
2293                 printf("%s\n",
2294                         NDR_PRINT_STRUCT_STRING(mem_ctx,
2295                                 samr_PasswordProperties, &info.password_properties));
2296         }
2297
2298  done:
2299         rpccli_samr_Close(cli, mem_ctx, &user_pol);
2300         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2301         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2302
2303         return result;
2304 }
2305
2306 static NTSTATUS cmd_samr_get_dom_pwinfo(struct rpc_pipe_client *cli,
2307                                         TALLOC_CTX *mem_ctx,
2308                                         int argc, const char **argv)
2309 {
2310         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2311         struct lsa_String domain_name;
2312         struct samr_PwInfo info;
2313
2314         if (argc < 1 || argc > 3) {
2315                 printf("Usage: %s <domain>\n", argv[0]);
2316                 return NT_STATUS_OK;
2317         }
2318
2319         init_lsa_String(&domain_name, argv[1]);
2320
2321         result = rpccli_samr_GetDomPwInfo(cli, mem_ctx, &domain_name, &info);
2322
2323         if (NT_STATUS_IS_OK(result)) {
2324                 printf("min_password_length: %d\n", info.min_password_length);
2325                 display_password_properties(info.password_properties);
2326         }
2327
2328         return result;
2329 }
2330
2331 /* Look up domain name */
2332
2333 static NTSTATUS cmd_samr_lookup_domain(struct rpc_pipe_client *cli,
2334                                        TALLOC_CTX *mem_ctx,
2335                                        int argc, const char **argv)
2336 {
2337         POLICY_HND connect_pol, domain_pol;
2338         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2339         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2340         fstring sid_string;
2341         struct lsa_String domain_name;
2342         DOM_SID *sid = NULL;
2343
2344         if (argc != 2) {
2345                 printf("Usage: %s domain_name\n", argv[0]);
2346                 return NT_STATUS_OK;
2347         }
2348
2349         init_lsa_String(&domain_name, argv[1]);
2350
2351         result = rpccli_try_samr_connects(cli, mem_ctx,
2352                                           access_mask,
2353                                           &connect_pol);
2354
2355         if (!NT_STATUS_IS_OK(result))
2356                 goto done;
2357
2358         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2359                                         &connect_pol,
2360                                         access_mask,
2361                                         &domain_sid,
2362                                         &domain_pol);
2363
2364         if (!NT_STATUS_IS_OK(result))
2365                 goto done;
2366
2367         result = rpccli_samr_LookupDomain(cli, mem_ctx,
2368                                           &connect_pol,
2369                                           &domain_name,
2370                                           &sid);
2371
2372         if (NT_STATUS_IS_OK(result)) {
2373                 sid_to_fstring(sid_string, sid);
2374                 printf("SAMR_LOOKUP_DOMAIN: Domain Name: %s Domain SID: %s\n",
2375                        argv[1], sid_string);
2376         }
2377
2378         rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2379         rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2380 done:
2381         return result;
2382 }
2383
2384 /* Change user password */
2385
2386 static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
2387                                    TALLOC_CTX *mem_ctx,
2388                                    int argc, const char **argv)
2389 {
2390         POLICY_HND connect_pol, domain_pol, user_pol;
2391         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2392         const char *user, *oldpass, *newpass;
2393         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2394         struct samr_Ids rids, types;
2395         struct lsa_String lsa_acct_name;
2396
2397         if (argc < 3) {
2398                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2399                 return NT_STATUS_INVALID_PARAMETER;
2400         }
2401
2402         user = argv[1];
2403         oldpass = argv[2];
2404         newpass = argv[3];
2405
2406         /* Get sam policy handle */
2407
2408         result = rpccli_try_samr_connects(cli, mem_ctx,
2409                                           MAXIMUM_ALLOWED_ACCESS,
2410                                           &connect_pol);
2411
2412         if (!NT_STATUS_IS_OK(result)) {
2413                 goto done;
2414         }
2415
2416         /* Get domain policy handle */
2417
2418         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2419                                         &connect_pol,
2420                                         access_mask,
2421                                         &domain_sid,
2422                                         &domain_pol);
2423
2424         if (!NT_STATUS_IS_OK(result)) {
2425                 goto done;
2426         }
2427
2428         init_lsa_String(&lsa_acct_name, user);
2429
2430         result = rpccli_samr_LookupNames(cli, mem_ctx,
2431                                          &domain_pol,
2432                                          1,
2433                                          &lsa_acct_name,
2434                                          &rids,
2435                                          &types);
2436
2437         if (!NT_STATUS_IS_OK(result)) {
2438                 goto done;
2439         }
2440
2441         result = rpccli_samr_OpenUser(cli, mem_ctx,
2442                                       &domain_pol,
2443                                       access_mask,
2444                                       rids.ids[0],
2445                                       &user_pol);
2446
2447         if (!NT_STATUS_IS_OK(result)) {
2448                 goto done;
2449         }
2450
2451         /* Change user password */
2452         result = rpccli_samr_chgpasswd_user(cli, mem_ctx,
2453                                             &user_pol,
2454                                             newpass,
2455                                             oldpass);
2456
2457         if (!NT_STATUS_IS_OK(result)) {
2458                 goto done;
2459         }
2460
2461  done:
2462         if (is_valid_policy_hnd(&user_pol)) {
2463                 rpccli_samr_Close(cli, mem_ctx, &user_pol);
2464         }
2465         if (is_valid_policy_hnd(&domain_pol)) {
2466                 rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2467         }
2468         if (is_valid_policy_hnd(&connect_pol)) {
2469                 rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2470         }
2471
2472         return result;
2473 }
2474
2475
2476 /* Change user password */
2477
2478 static NTSTATUS cmd_samr_chgpasswd2(struct rpc_pipe_client *cli,
2479                                     TALLOC_CTX *mem_ctx,
2480                                     int argc, const char **argv)
2481 {
2482         POLICY_HND connect_pol, domain_pol;
2483         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2484         const char *user, *oldpass, *newpass;
2485         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2486
2487         if (argc < 3) {
2488                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2489                 return NT_STATUS_INVALID_PARAMETER;
2490         }
2491
2492         user = argv[1];
2493         oldpass = argv[2];
2494         newpass = argv[3];
2495
2496         /* Get sam policy handle */
2497
2498         result = rpccli_try_samr_connects(cli, mem_ctx,
2499                                           MAXIMUM_ALLOWED_ACCESS,
2500                                           &connect_pol);
2501
2502         if (!NT_STATUS_IS_OK(result))
2503                 goto done;
2504
2505         /* Get domain policy handle */
2506
2507         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2508                                         &connect_pol,
2509                                         access_mask,
2510                                         &domain_sid,
2511                                         &domain_pol);
2512
2513         if (!NT_STATUS_IS_OK(result))
2514                 goto done;
2515
2516         /* Change user password */
2517         result = rpccli_samr_chgpasswd_user2(cli, mem_ctx, user, newpass, oldpass);
2518
2519         if (!NT_STATUS_IS_OK(result))
2520                 goto done;
2521
2522         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2523         if (!NT_STATUS_IS_OK(result)) goto done;
2524
2525         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2526         if (!NT_STATUS_IS_OK(result)) goto done;
2527
2528  done:
2529         return result;
2530 }
2531
2532
2533 /* Change user password */
2534
2535 static NTSTATUS cmd_samr_chgpasswd3(struct rpc_pipe_client *cli,
2536                                     TALLOC_CTX *mem_ctx,
2537                                     int argc, const char **argv)
2538 {
2539         POLICY_HND connect_pol, domain_pol;
2540         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2541         const char *user, *oldpass, *newpass;
2542         uint32 access_mask = MAXIMUM_ALLOWED_ACCESS;
2543         struct samr_DomInfo1 *info = NULL;
2544         struct samr_ChangeReject *reject = NULL;
2545
2546         if (argc < 3) {
2547                 printf("Usage: %s username oldpass newpass\n", argv[0]);
2548                 return NT_STATUS_INVALID_PARAMETER;
2549         }
2550
2551         user = argv[1];
2552         oldpass = argv[2];
2553         newpass = argv[3];
2554
2555         /* Get sam policy handle */
2556
2557         result = rpccli_try_samr_connects(cli, mem_ctx,
2558                                           MAXIMUM_ALLOWED_ACCESS,
2559                                           &connect_pol);
2560
2561         if (!NT_STATUS_IS_OK(result))
2562                 goto done;
2563
2564         /* Get domain policy handle */
2565
2566         result = rpccli_samr_OpenDomain(cli, mem_ctx,
2567                                         &connect_pol,
2568                                         access_mask,
2569                                         &domain_sid,
2570                                         &domain_pol);
2571
2572         if (!NT_STATUS_IS_OK(result))
2573                 goto done;
2574
2575         /* Change user password */
2576         result = rpccli_samr_chgpasswd_user3(cli, mem_ctx,
2577                                              user,
2578                                              newpass,
2579                                              oldpass,
2580                                              &info,
2581                                              &reject);
2582
2583         if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {
2584
2585                 display_sam_dom_info_1(info);
2586
2587                 switch (reject->reason) {
2588                         case SAMR_REJECT_TOO_SHORT:
2589                                 d_printf("SAMR_REJECT_TOO_SHORT\n");
2590                                 break;
2591                         case SAMR_REJECT_IN_HISTORY:
2592                                 d_printf("SAMR_REJECT_IN_HISTORY\n");
2593                                 break;
2594                         case SAMR_REJECT_COMPLEXITY:
2595                                 d_printf("SAMR_REJECT_COMPLEXITY\n");
2596                                 break;
2597                         case SAMR_REJECT_OTHER:
2598                                 d_printf("SAMR_REJECT_OTHER\n");
2599                                 break;
2600                         default:
2601                                 d_printf("unknown reject reason: %d\n",
2602                                         reject->reason);
2603                                 break;
2604                 }
2605         }
2606
2607         if (!NT_STATUS_IS_OK(result))
2608                 goto done;
2609
2610         result = rpccli_samr_Close(cli, mem_ctx, &domain_pol);
2611         if (!NT_STATUS_IS_OK(result)) goto done;
2612
2613         result = rpccli_samr_Close(cli, mem_ctx, &connect_pol);
2614         if (!NT_STATUS_IS_OK(result)) goto done;
2615
2616  done:
2617         return result;
2618 }
2619
2620 static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
2621                                          TALLOC_CTX *mem_ctx,
2622                                          int argc, const char **argv,
2623                                          int opcode)
2624 {
2625         POLICY_HND connect_pol, domain_pol, user_pol;
2626         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2627         const char *user, *param;
2628         uint32_t access_mask = MAXIMUM_ALLOWED_ACCESS;
2629         uint32_t level;
2630         uint32_t user_rid;
2631         union samr_UserInfo info;
2632         struct samr_CryptPassword pwd_buf;
2633         struct samr_CryptPasswordEx pwd_buf_ex;
2634         uint8_t nt_hash[16];
2635         uint8_t lm_hash[16];
2636         DATA_BLOB session_key;
2637         uint8_t password_expired = 0;
2638
2639         if (argc < 4) {
2640                 printf("Usage: %s username level password [password_expired]\n",
2641                         argv[0]);
2642                 return NT_STATUS_INVALID_PARAMETER;
2643         }
2644
2645         user = argv[1];
2646         level = atoi(argv[2]);
2647         param = argv[3];
2648
2649         if (argc >= 5) {
2650                 password_expired = atoi(argv[4]);
2651         }
2652
2653         status = cli_get_session_key(mem_ctx, cli, &session_key);
2654         if (!NT_STATUS_IS_OK(status)) {
2655                 return status;
2656         }
2657
2658         init_samr_CryptPassword(param, &session_key, &pwd_buf);
2659         init_samr_CryptPasswordEx(param, &session_key, &pwd_buf_ex);
2660         nt_lm_owf_gen(param, nt_hash, lm_hash);
2661
2662         switch (level) {
2663         case 18:
2664                 {
2665                         DATA_BLOB in,out;
2666                         in = data_blob_const(nt_hash, 16);
2667                         out = data_blob_talloc_zero(mem_ctx, 16);
2668                         sess_crypt_blob(&out, &in, &session_key, true);
2669                         memcpy(nt_hash, out.data, out.length);
2670                 }
2671                 {
2672                         DATA_BLOB in,out;
2673                         in = data_blob_const(lm_hash, 16);
2674                         out = data_blob_talloc_zero(mem_ctx, 16);
2675                         sess_crypt_blob(&out, &in, &session_key, true);
2676                         memcpy(lm_hash, out.data, out.length);
2677                 }
2678
2679                 memcpy(info.info18.nt_pwd.hash, nt_hash, 16);
2680                 memcpy(info.info18.lm_pwd.hash, lm_hash, 16);
2681                 info.info18.nt_pwd_active       = true;
2682                 info.info18.lm_pwd_active       = true;
2683                 info.info18.password_expired    = password_expired;
2684
2685                 break;
2686         case 21:
2687                 ZERO_STRUCT(info.info21);
2688
2689                 info.info21.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2690                                              SAMR_FIELD_LM_PASSWORD_PRESENT;
2691                 if (argc >= 5) {
2692                         info.info21.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2693                         info.info21.password_expired = password_expired;
2694                 }
2695
2696                 info.info21.lm_password_set = true;
2697                 info.info21.lm_owf_password.length = 16;
2698                 info.info21.lm_owf_password.size = 16;
2699
2700                 info.info21.nt_password_set = true;
2701                 info.info21.nt_owf_password.length = 16;
2702                 info.info21.nt_owf_password.size = 16;
2703
2704                 {
2705                         DATA_BLOB in,out;
2706                         in = data_blob_const(nt_hash, 16);
2707                         out = data_blob_talloc_zero(mem_ctx, 16);
2708                         sess_crypt_blob(&out, &in, &session_key, true);
2709                         info.info21.nt_owf_password.array =
2710                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
2711                 }
2712                 {
2713                         DATA_BLOB in,out;
2714                         in = data_blob_const(lm_hash, 16);
2715                         out = data_blob_talloc_zero(mem_ctx, 16);
2716                         sess_crypt_blob(&out, &in, &session_key, true);
2717                         info.info21.lm_owf_password.array =
2718                                 (uint16_t *)talloc_memdup(mem_ctx, out.data, 16);
2719                 }
2720
2721                 break;
2722         case 23:
2723                 ZERO_STRUCT(info.info23);
2724
2725                 info.info23.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2726                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
2727                 if (argc >= 5) {
2728                         info.info23.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2729                         info.info23.info.password_expired = password_expired;
2730                 }
2731
2732                 info.info23.password = pwd_buf;
2733
2734                 break;
2735         case 24:
2736                 info.info24.password            = pwd_buf;
2737                 info.info24.password_expired    = password_expired;
2738
2739                 break;
2740         case 25:
2741                 ZERO_STRUCT(info.info25);
2742
2743                 info.info25.info.fields_present = SAMR_FIELD_NT_PASSWORD_PRESENT |
2744                                                   SAMR_FIELD_LM_PASSWORD_PRESENT;
2745                 if (argc >= 5) {
2746                         info.info25.info.fields_present |= SAMR_FIELD_EXPIRED_FLAG;
2747                         info.info25.info.password_expired = password_expired;
2748                 }
2749
2750                 info.info25.password = pwd_buf_ex;
2751
2752                 break;
2753         case 26:
2754                 info.info26.password            = pwd_buf_ex;
2755                 info.info26.password_expired    = password_expired;
2756
2757                 break;
2758         default:
2759                 return NT_STATUS_INVALID_INFO_CLASS;
2760         }
2761
2762         /* Get sam policy handle */
2763
2764         status = rpccli_try_samr_connects(cli, mem_ctx,
2765                                           MAXIMUM_ALLOWED_ACCESS,
2766                                           &connect_pol);
2767
2768         if (!NT_STATUS_IS_OK(status))
2769                 goto done;
2770
2771         /* Get domain policy handle */
2772
2773         status = rpccli_samr_OpenDomain(cli, mem_ctx,
2774                                         &connect_pol,
2775                                         access_mask,
2776                                         &domain_sid,
2777                                         &domain_pol);
2778
2779         if (!NT_STATUS_IS_OK(status))
2780                 goto done;
2781
2782         user_rid = strtol(user, NULL, 0);
2783         if (user_rid) {
2784                 status = rpccli_samr_OpenUser(cli, mem_ctx,
2785                                               &domain_pol,
2786                                               access_mask,
2787                                               user_rid,
2788                                               &user_pol);
2789         }
2790
2791         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER) ||
2792             (user_rid == 0)) {
2793
2794                 /* Probably this was a user name, try lookupnames */
2795                 struct samr_Ids rids, types;
2796                 struct lsa_String lsa_acct_name;
2797
2798                 init_lsa_String(&lsa_acct_name, user);
2799
2800                 status = rpccli_samr_LookupNames(cli, mem_ctx,
2801                                                  &domain_pol,
2802                                                  1,
2803                                                  &lsa_acct_name,
2804                                                  &rids,
2805                                                  &types);
2806                 if (!NT_STATUS_IS_OK(status)) {
2807                         return status;
2808                 }
2809
2810                 status = rpccli_samr_OpenUser(cli, mem_ctx,
2811                                               &domain_pol,
2812                                               access_mask,
2813                                               rids.ids[0],
2814                                               &user_pol);
2815                 if (!NT_STATUS_IS_OK(status)) {
2816                         return status;
2817                 }
2818         }
2819
2820         switch (opcode) {
2821         case NDR_SAMR_SETUSERINFO:
2822                 status = rpccli_samr_SetUserInfo(cli, mem_ctx,
2823                                                  &user_pol,
2824                                                  level,
2825                                                  &info);
2826                 break;
2827         case NDR_SAMR_SETUSERINFO2:
2828                 status = rpccli_samr_SetUserInfo2(cli, mem_ctx,
2829                                                   &user_pol,
2830                                                   level,
2831                                                   &info);
2832                 break;
2833         default:
2834                 return NT_STATUS_INVALID_PARAMETER;
2835         }
2836
2837  done:
2838         return status;
2839 }
2840
2841 static NTSTATUS cmd_samr_setuserinfo(struct rpc_pipe_client *cli,
2842                                      TALLOC_CTX *mem_ctx,
2843                                      int argc, const char **argv)
2844 {
2845         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
2846                                         NDR_SAMR_SETUSERINFO);
2847 }
2848
2849 static NTSTATUS cmd_samr_setuserinfo2(struct rpc_pipe_client *cli,
2850                                       TALLOC_CTX *mem_ctx,
2851                                       int argc, const char **argv)
2852 {
2853         return cmd_samr_setuserinfo_int(cli, mem_ctx, argc, argv,
2854                                         NDR_SAMR_SETUSERINFO2);
2855 }
2856
2857 static NTSTATUS cmd_samr_get_dispinfo_idx(struct rpc_pipe_client *cli,
2858                                           TALLOC_CTX *mem_ctx,
2859                                           int argc, const char **argv)
2860 {
2861         NTSTATUS status;
2862         struct policy_handle connect_handle;
2863         struct policy_handle domain_handle;
2864         uint16_t level = 1;
2865         struct lsa_String name;
2866         uint32_t idx = 0;
2867
2868         if (argc < 2 || argc > 3) {
2869                 printf("Usage: %s name level\n", argv[0]);
2870                 return NT_STATUS_INVALID_PARAMETER;
2871         }
2872
2873         init_lsa_String(&name, argv[1]);
2874
2875         if (argc == 3) {
2876                 level = atoi(argv[2]);
2877         }
2878
2879         status = rpccli_try_samr_connects(cli, mem_ctx,
2880                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
2881                                           &connect_handle);
2882
2883         if (!NT_STATUS_IS_OK(status)) {
2884                 goto done;
2885         }
2886
2887         status = rpccli_samr_OpenDomain(cli, mem_ctx,
2888                                         &connect_handle,
2889                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
2890                                         &domain_sid,
2891                                         &domain_handle);
2892
2893         if (!NT_STATUS_IS_OK(status))
2894                 goto done;
2895
2896
2897         status = rpccli_samr_GetDisplayEnumerationIndex(cli, mem_ctx,
2898                                                         &domain_handle,
2899                                                         level,
2900                                                         &name,
2901                                                         &idx);
2902
2903         if (NT_STATUS_IS_OK(status) ||
2904             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
2905                 printf("idx: %d (0x%08x)\n", idx, idx);
2906         }
2907  done:
2908
2909         if (is_valid_policy_hnd(&domain_handle)) {
2910                 rpccli_samr_Close(cli, mem_ctx, &domain_handle);
2911         }
2912         if (is_valid_policy_hnd(&connect_handle)) {
2913                 rpccli_samr_Close(cli, mem_ctx, &connect_handle);
2914         }
2915
2916         return status;
2917
2918 }
2919 /* List of commands exported by this module */
2920
2921 struct cmd_set samr_commands[] = {
2922
2923         { "SAMR" },
2924
2925         { "queryuser",  RPC_RTYPE_NTSTATUS, cmd_samr_query_user,                NULL, &ndr_table_samr.syntax_id, NULL,  "Query user info",         "" },
2926         { "querygroup",         RPC_RTYPE_NTSTATUS, cmd_samr_query_group,               NULL, &ndr_table_samr.syntax_id, NULL,  "Query group info",        "" },
2927         { "queryusergroups",    RPC_RTYPE_NTSTATUS, cmd_samr_query_usergroups,  NULL, &ndr_table_samr.syntax_id, NULL,  "Query user groups",       "" },
2928         { "queryuseraliases",   RPC_RTYPE_NTSTATUS, cmd_samr_query_useraliases,         NULL, &ndr_table_samr.syntax_id, NULL,  "Query user aliases",      "" },
2929         { "querygroupmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_groupmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query group membership",  "" },
2930         { "queryaliasmem",      RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasmem,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias membership",  "" },
2931         { "queryaliasinfo",     RPC_RTYPE_NTSTATUS, cmd_samr_query_aliasinfo,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query alias info",       "" },
2932         { "deletealias",        RPC_RTYPE_NTSTATUS, cmd_samr_delete_alias,      NULL, &ndr_table_samr.syntax_id, NULL,  "Delete an alias",  "" },
2933         { "querydispinfo",      RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo,    NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2934         { "querydispinfo2",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo2,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2935         { "querydispinfo3",     RPC_RTYPE_NTSTATUS, cmd_samr_query_dispinfo3,   NULL, &ndr_table_samr.syntax_id, NULL,  "Query display info",      "" },
2936         { "querydominfo",       RPC_RTYPE_NTSTATUS, cmd_samr_query_dominfo,     NULL, &ndr_table_samr.syntax_id, NULL,  "Query domain info",       "" },
2937         { "enumdomusers",       RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_users,       NULL, &ndr_table_samr.syntax_id, NULL,       "Enumerate domain users", "" },
2938         { "enumdomgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_dom_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domain groups", "" },
2939         { "enumalsgroups",      RPC_RTYPE_NTSTATUS, cmd_samr_enum_als_groups,       NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate alias groups",  "" },
2940         { "enumdomains",        RPC_RTYPE_NTSTATUS, cmd_samr_enum_domains,          NULL, &ndr_table_samr.syntax_id, NULL,      "Enumerate domains",  "" },
2941
2942         { "createdomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain user",      "" },
2943         { "createdomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain group",     "" },
2944         { "createdomalias",     RPC_RTYPE_NTSTATUS, cmd_samr_create_dom_alias,      NULL, &ndr_table_samr.syntax_id, NULL,      "Create domain alias",     "" },
2945         { "samlookupnames",     RPC_RTYPE_NTSTATUS, cmd_samr_lookup_names,          NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
2946         { "samlookuprids",      RPC_RTYPE_NTSTATUS, cmd_samr_lookup_rids,           NULL, &ndr_table_samr.syntax_id, NULL,      "Look up names",           "" },
2947         { "deletedomgroup",     RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_group,      NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain group",     "" },
2948         { "deletedomuser",      RPC_RTYPE_NTSTATUS, cmd_samr_delete_dom_user,       NULL, &ndr_table_samr.syntax_id, NULL,      "Delete domain user",      "" },
2949         { "samquerysecobj",     RPC_RTYPE_NTSTATUS, cmd_samr_query_sec_obj,         NULL, &ndr_table_samr.syntax_id, NULL, "Query SAMR security object",   "" },
2950         { "getdompwinfo",       RPC_RTYPE_NTSTATUS, cmd_samr_get_dom_pwinfo,        NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve domain password info", "" },
2951         { "getusrdompwinfo",    RPC_RTYPE_NTSTATUS, cmd_samr_get_usrdom_pwinfo,     NULL, &ndr_table_samr.syntax_id, NULL, "Retrieve user domain password info", "" },
2952
2953         { "lookupdomain",       RPC_RTYPE_NTSTATUS, cmd_samr_lookup_domain,         NULL, &ndr_table_samr.syntax_id, NULL, "Lookup Domain Name", "" },
2954         { "chgpasswd",          RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd,             NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2955         { "chgpasswd2",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd2,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2956         { "chgpasswd3",         RPC_RTYPE_NTSTATUS, cmd_samr_chgpasswd3,            NULL, &ndr_table_samr.syntax_id, NULL, "Change user password", "" },
2957         { "getdispinfoidx",     RPC_RTYPE_NTSTATUS, cmd_samr_get_dispinfo_idx,      NULL, &ndr_table_samr.syntax_id, NULL, "Get Display Information Index", "" },
2958         { "setuserinfo",        RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo,           NULL, &ndr_table_samr.syntax_id, NULL, "Set user info", "" },
2959         { "setuserinfo2",       RPC_RTYPE_NTSTATUS, cmd_samr_setuserinfo2,          NULL, &ndr_table_samr.syntax_id, NULL, "Set user info2", "" },
2960         { NULL }
2961 };