damn, that took a while. nt login password was being stored incorrectly
[nivanova/samba-autobuild/.git] / source3 / rpcclient / cmd_samr.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24
25 #ifdef SYSLOG
26 #undef SYSLOG
27 #endif
28
29 #include "includes.h"
30 #include "nterr.h"
31
32 extern int DEBUGLEVEL;
33
34 #define DEBUG_TESTING
35
36 extern struct user_credentials *usr_creds;
37
38 extern FILE* out_hnd;
39
40 static void sam_display_domain(const char *domain)
41 {
42         report(out_hnd, "Domain Name: %s\n", domain);
43 }
44
45 static void sam_display_alias_info(const char *domain, const DOM_SID *sid,
46                                 uint32 alias_rid, 
47                                 ALIAS_INFO_CTR *const ctr)
48 {
49         display_alias_info_ctr(out_hnd, ACTION_HEADER   , ctr);
50         display_alias_info_ctr(out_hnd, ACTION_ENUMERATE, ctr);
51         display_alias_info_ctr(out_hnd, ACTION_FOOTER   , ctr);
52 }
53
54 static void sam_display_alias(const char *domain, const DOM_SID *sid,
55                                 uint32 alias_rid, const char *alias_name)
56 {
57         report(out_hnd, "Alias RID: %8x  Alias Name: %s\n",
58                           alias_rid, alias_name);
59 }
60
61 static void sam_display_alias_members(const char *domain, const DOM_SID *sid,
62                                 uint32 alias_rid, const char *alias_name,
63                                 uint32 num_names,
64                                 DOM_SID *const *const sids,
65                                 char *const *const name,
66                                 uint8 *const type)
67 {
68         display_alias_members(out_hnd, ACTION_HEADER   , num_names, name, type);
69         display_alias_members(out_hnd, ACTION_ENUMERATE, num_names, name, type);
70         display_alias_members(out_hnd, ACTION_FOOTER   , num_names, name, type);
71 }
72
73 static void sam_display_group_info(const char *domain, const DOM_SID *sid,
74                                 uint32 group_rid, 
75                                 GROUP_INFO_CTR *const ctr)
76 {
77         display_group_info_ctr(out_hnd, ACTION_HEADER   , ctr);
78         display_group_info_ctr(out_hnd, ACTION_ENUMERATE, ctr);
79         display_group_info_ctr(out_hnd, ACTION_FOOTER   , ctr);
80 }
81
82 static void sam_display_group(const char *domain, const DOM_SID *sid,
83                                 uint32 group_rid, const char *group_name)
84 {
85         report(out_hnd, "Group RID: %8x  Group Name: %s\n",
86                           group_rid, group_name);
87 }
88
89 static void sam_display_group_members(const char *domain, const DOM_SID *sid,
90                                 uint32 group_rid, const char *group_name,
91                                 uint32 num_names,
92                                 const uint32 *rid_mem,
93                                 char *const *const name,
94                                 uint32 *const type)
95 {
96         display_group_members(out_hnd, ACTION_HEADER   , num_names, name, type);
97         display_group_members(out_hnd, ACTION_ENUMERATE, num_names, name, type);
98         display_group_members(out_hnd, ACTION_FOOTER   , num_names, name, type);
99 }
100
101 static void sam_display_user_info(const char *domain, const DOM_SID *sid,
102                                 uint32 user_rid, 
103                                 SAM_USER_INFO_21 *const usr)
104 {
105         display_sam_user_info_21(out_hnd, ACTION_HEADER   , usr);
106         display_sam_user_info_21(out_hnd, ACTION_ENUMERATE, usr);
107         display_sam_user_info_21(out_hnd, ACTION_FOOTER   , usr);
108 }
109
110 static void sam_display_user(const char *domain, const DOM_SID *sid,
111                                 uint32 user_rid, const char *user_name)
112 {
113         report(out_hnd, "User RID: %8x  User Name: %s\n",
114                         user_rid, user_name);
115 }
116
117
118 /****************************************************************************
119 SAM password change
120 ****************************************************************************/
121 void cmd_sam_ntchange_pwd(struct client_info *info, int argc, char *argv[])
122 {
123         fstring srv_name;
124         fstring domain;
125         fstring sid;
126         char *new_passwd;
127         BOOL res = True;
128         char nt_newpass[516];
129         uchar nt_hshhash[16];
130         uchar nt_newhash[16];
131         uchar nt_oldhash[16];
132         char lm_newpass[516];
133         uchar lm_newhash[16];
134         uchar lm_hshhash[16];
135         uchar lm_oldhash[16];
136
137         struct cli_connection *con = NULL;
138
139         sid_to_string(sid, &info->dom.level5_sid);
140         fstrcpy(domain, info->dom.level5_dom);
141
142         fstrcpy(srv_name, "\\\\");
143         fstrcat(srv_name, info->dest_host);
144         strupper(srv_name);
145
146         report(out_hnd, "SAM NT Password Change\n");
147
148 #if 0
149         struct pwd_info new_pwd;
150         pwd_read(&new_pwd, "New Password (ONCE: this is test code!):", True);
151 #endif
152         new_passwd = (char*)getpass("New Password (ONCE ONLY - get it right :-)");
153
154         nt_lm_owf_gen(new_passwd, lm_newhash, nt_newhash);
155         pwd_get_lm_nt_16(&(usr_creds->pwd), lm_oldhash, nt_oldhash );
156         make_oem_passwd_hash(nt_newpass, new_passwd, nt_oldhash, True);
157         make_oem_passwd_hash(lm_newpass, new_passwd, lm_oldhash, True);
158         E_old_pw_hash(lm_newhash, lm_oldhash, lm_hshhash);
159         E_old_pw_hash(lm_newhash, nt_oldhash, nt_hshhash);
160
161         usr_creds->ntlmssp_flags = NTLMSSP_NEGOTIATE_UNICODE |
162                                     NTLMSSP_NEGOTIATE_OEM |
163                                     NTLMSSP_NEGOTIATE_SIGN |
164                                     NTLMSSP_NEGOTIATE_SEAL |
165                                     NTLMSSP_NEGOTIATE_LM_KEY |
166                                     NTLMSSP_NEGOTIATE_NTLM |
167                                     NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
168                                     NTLMSSP_NEGOTIATE_00001000 |
169                                     NTLMSSP_NEGOTIATE_00002000;
170
171         /* open SAMR session.  */
172         res = res ? cli_connection_init(srv_name, PIPE_SAMR, &con) : False;
173
174         /* establish a connection. */
175         res = res ? samr_unknown_38(con, srv_name) : False;
176
177         /* establish a connection. */
178         res = res ? samr_chgpasswd_user(con,
179                                            srv_name, usr_creds->user_name,
180                                            nt_newpass, nt_hshhash,
181                                            lm_newpass, lm_hshhash) : False;
182         /* close the session */
183         cli_connection_unlink(con);
184
185         if (res)
186         {
187                 report(out_hnd, "NT Password changed OK\n");
188         }
189         else
190         {
191                 report(out_hnd, "NT Password change FAILED\n");
192         }
193 }
194
195
196 /****************************************************************************
197 experimental SAM encryted rpc test connection
198 ****************************************************************************/
199 void cmd_sam_test(struct client_info *info, int argc, char *argv[])
200 {
201         struct cli_connection *con = NULL;
202         fstring srv_name;
203         fstring domain;
204         fstring sid;
205         BOOL res = True;
206
207         sid_to_string(sid, &info->dom.level5_sid);
208         fstrcpy(domain, info->dom.level5_dom);
209
210 /*
211         if (sid1.num_auths == 0)
212         {
213                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
214                 return;
215         }
216 */
217         fstrcpy(srv_name, "\\\\");
218         fstrcat(srv_name, info->dest_host);
219         strupper(srv_name);
220
221         report(out_hnd, "SAM Encryption Test\n");
222
223         usr_creds->ntlmssp_flags = NTLMSSP_NEGOTIATE_UNICODE |
224                                     NTLMSSP_NEGOTIATE_OEM |
225                                     NTLMSSP_NEGOTIATE_SIGN |
226                                     NTLMSSP_NEGOTIATE_SEAL |
227                                     NTLMSSP_NEGOTIATE_LM_KEY |
228                                     NTLMSSP_NEGOTIATE_NTLM |
229                                     NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
230                                     NTLMSSP_NEGOTIATE_00001000 |
231                                     NTLMSSP_NEGOTIATE_00002000;
232
233         /* open SAMR session.  */
234         res = res ? cli_connection_init(srv_name, PIPE_SAMR, &con) : False;
235
236         /* close the session */
237         cli_connection_unlink(con);
238
239         if (res)
240         {
241                 DEBUG(5,("cmd_sam_test: succeeded\n"));
242         }
243         else
244         {
245                 DEBUG(5,("cmd_sam_test: failed\n"));
246         }
247 }
248
249 /****************************************************************************
250 Lookup domain in SAM server.
251 ****************************************************************************/
252 void cmd_sam_lookup_domain(struct client_info *info, int argc, char *argv[])
253 {
254         fstring srv_name;
255         char *domain;
256         fstring str_sid;
257         DOM_SID dom_sid;
258         BOOL res = True;
259         POLICY_HND sam_pol;
260
261         fstrcpy(srv_name, "\\\\");
262         fstrcat(srv_name, info->dest_host);
263         strupper(srv_name);
264
265         if (argc < 2)
266         {
267                 report(out_hnd, "lookupdomain: <name>\n");
268                 return;
269         }
270
271         domain = argv[1];
272
273         report(out_hnd, "Lookup Domain in SAM Server\n");
274
275         /* establish a connection. */
276         res = res ? samr_connect( srv_name, 0x02000000,
277                                 &sam_pol) : False;
278
279         /* connect to the domain */
280         res = res ? samr_query_lookup_domain( &sam_pol, domain, &dom_sid) : False;
281
282         res = res ? samr_close(&sam_pol) : False;
283
284         if (res)
285         {
286                 DEBUG(5,("cmd_sam_lookup_domain: succeeded\n"));
287
288                 sid_to_string(str_sid, &dom_sid);
289                 report(out_hnd, "%s SID: %s\n", domain, str_sid);
290                 report(out_hnd, "Lookup Domain: OK\n");
291         }
292         else
293         {
294                 DEBUG(5,("cmd_sam_lookup_domain: failed\n"));
295                 report(out_hnd, "Lookup Domain: FAILED\n");
296         }
297 }
298
299 /****************************************************************************
300 SAM delete alias member.
301 ****************************************************************************/
302 void cmd_sam_del_aliasmem(struct client_info *info, int argc, char *argv[])
303 {
304         fstring srv_name;
305         fstring domain;
306         fstring sid;
307         DOM_SID sid1;
308         POLICY_HND alias_pol;
309         BOOL res = True;
310         BOOL res1 = True;
311         BOOL res2 = True;
312         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
313         DOM_SID member_sid; 
314         uint32 alias_rid;
315         POLICY_HND sam_pol;
316         POLICY_HND pol_dom;
317
318         sid_copy(&sid1, &info->dom.level5_sid);
319         sid_to_string(sid, &sid1);
320         fstrcpy(domain, info->dom.level5_dom);
321
322         if (sid1.num_auths == 0)
323         {
324                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
325                 return;
326         }
327
328         fstrcpy(srv_name, "\\\\");
329         fstrcat(srv_name, info->dest_host);
330         strupper(srv_name);
331
332         if (argc < 2)
333         {
334                 report(out_hnd, "delaliasmem: <alias rid> [member sid1] [member sid2] ...\n");
335                 return;
336         }
337
338         argc--;
339         argv++;
340
341         alias_rid = get_number(argv[0]);
342
343         report(out_hnd, "SAM Domain Alias Member\n");
344
345         /* establish a connection. */
346         res = res ? samr_connect( srv_name, 0x02000000,
347                                 &sam_pol) : False;
348
349         /* connect to the domain */
350         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
351                     &pol_dom) : False;
352
353         /* connect to the domain */
354         res1 = res ? samr_open_alias( &pol_dom,
355                     0x000f001f, alias_rid, &alias_pol) : False;
356
357         while (argc > 0 && res2 && res1)
358         {
359                 argc--;
360                 argv++;
361                 /* get a sid, delete a member from the alias */
362                 res2 = res2 ? string_to_sid(&member_sid, argv[0]) : False;
363                 res2 = res2 ? samr_del_aliasmem(&alias_pol, &member_sid) : False;
364
365                 if (res2)
366                 {
367                         report(out_hnd, "SID deleted from Alias 0x%x: %s\n", alias_rid, argv[0]);
368                 }
369         }
370
371         res1 = res1 ? samr_close(&alias_pol) : False;
372         res  = res  ? samr_close(&pol_dom) : False;
373         res  = res  ? samr_close(&sam_pol) : False;
374
375         if (res && res1 && res2)
376         {
377                 DEBUG(5,("cmd_sam_del_aliasmem: succeeded\n"));
378                 report(out_hnd, "Delete Domain Alias Member: OK\n");
379         }
380         else
381         {
382                 DEBUG(5,("cmd_sam_del_aliasmem: failed\n"));
383                 report(out_hnd, "Delete Domain Alias Member: FAILED\n");
384         }
385 }
386
387 /****************************************************************************
388 SAM delete alias.
389 ****************************************************************************/
390 void cmd_sam_delete_dom_alias(struct client_info *info, int argc, char *argv[])
391 {
392         fstring srv_name;
393         fstring domain;
394         char *name;
395         fstring sid;
396         DOM_SID sid1;
397         POLICY_HND alias_pol;
398         BOOL res = True;
399         BOOL res1 = True;
400         BOOL res2 = True;
401         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
402         uint32 alias_rid = 0;
403         char *names[1];
404         uint32 rid [MAX_LOOKUP_SIDS];
405         uint32 type[MAX_LOOKUP_SIDS];
406         uint32 num_rids;
407         POLICY_HND sam_pol;
408         POLICY_HND pol_dom;
409
410         sid_copy(&sid1, &info->dom.level5_sid);
411         sid_to_string(sid, &sid1);
412         fstrcpy(domain, info->dom.level5_dom);
413
414         if (sid1.num_auths == 0)
415         {
416                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
417                 return;
418         }
419
420         fstrcpy(srv_name, "\\\\");
421         fstrcat(srv_name, info->dest_host);
422         strupper(srv_name);
423
424         if (argc < 2)
425         {
426                 report(out_hnd, "delalias <alias name>\n");
427                 return;
428         }
429
430         name = argv[1];
431
432         report(out_hnd, "SAM Delete Domain Alias\n");
433
434         /* establish a connection. */
435         res = res ? samr_connect( srv_name, 0x02000000,
436                                 &sam_pol) : False;
437
438         /* connect to the domain */
439         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
440                     &pol_dom) : False;
441
442         names[0] = name;
443
444         res1 = res ? samr_query_lookup_names( &pol_dom, 0x000003e8,
445                     1, names,
446                     &num_rids, rid, type) : False;
447
448         if (res1 && num_rids == 1)
449         {
450                 alias_rid = rid[0];
451         }
452
453         /* connect to the domain */
454         res1 = res1 ? samr_open_alias( &pol_dom,
455                     0x000f001f, alias_rid, &alias_pol) : False;
456
457         res2 = res1 ? samr_delete_dom_alias(&alias_pol) : False;
458
459         res1 = res1 ? samr_close(&alias_pol) : False;
460         res  = res  ? samr_close(&pol_dom) : False;
461         res  = res  ? samr_close(&sam_pol) : False;
462
463         if (res && res1 && res2)
464         {
465                 DEBUG(5,("cmd_sam_delete_dom_alias: succeeded\n"));
466                 report(out_hnd, "Delete Domain Alias: OK\n");
467         }
468         else
469         {
470                 DEBUG(5,("cmd_sam_delete_dom_alias: failed\n"));
471                 report(out_hnd, "Delete Domain Alias: FAILED\n");
472         }
473 }
474
475 /****************************************************************************
476 SAM add alias member.
477 ****************************************************************************/
478 void cmd_sam_add_aliasmem(struct client_info *info, int argc, char *argv[])
479 {
480         fstring srv_name;
481         fstring domain;
482         fstring tmp;
483         fstring sid;
484         DOM_SID sid1;
485         POLICY_HND alias_pol;
486         BOOL res = True;
487         BOOL res1 = True;
488         BOOL res2 = True;
489         BOOL res3 = True;
490         BOOL res4 = True;
491         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
492         uint32 alias_rid;
493         char **names = NULL;
494         int num_names = 0;
495         DOM_SID *sids = NULL; 
496         int num_sids = 0;
497         int i;
498         POLICY_HND sam_pol;
499         POLICY_HND pol_dom;
500         POLICY_HND lsa_pol;
501
502         sid_copy(&sid1, &info->dom.level5_sid);
503         sid_to_string(sid, &sid1);
504         fstrcpy(domain, info->dom.level5_dom);
505
506         if (sid1.num_auths == 0)
507         {
508                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
509                 return;
510         }
511
512         fstrcpy(srv_name, "\\\\");
513         fstrcat(srv_name, info->dest_host);
514         strupper(srv_name);
515
516         if (argc < 2)
517         {
518                 report(out_hnd, "addaliasmem <group name> [member name1] [member name2] ...\n");
519                 return;
520         }
521         
522         num_names = argc+1;
523         names = argv+1;
524
525         report(out_hnd, "SAM Domain Alias Member\n");
526
527         /* lookup domain controller; receive a policy handle */
528         res3 = res3 ? lsa_open_policy(srv_name,
529                                 &lsa_pol, True) : False;
530
531         /* send lsa lookup sids call */
532         res4 = res3 ? lsa_lookup_names(&lsa_pol,
533                                        num_names, names, 
534                                        &sids, NULL, &num_sids) : False;
535
536         res3 = res3 ? lsa_close(&lsa_pol) : False;
537
538         res4 = num_sids < 2 ? False : res4;
539
540         if (res4)
541         {
542                 /*
543                  * accept domain sid or builtin sid
544                  */
545
546                 DOM_SID sid_1_5_20;
547                 string_to_sid(&sid_1_5_20, "S-1-5-32");
548                 sid_split_rid(&sids[0], &alias_rid);
549
550                 if (sid_equal(&sids[0], &sid_1_5_20))
551                 {
552                         sid_copy(&sid1, &sid_1_5_20);
553                 }
554                 else if (!sid_equal(&sids[0], &sid1))
555                 {       
556                         res4 = False;
557                 }
558         }
559
560         /* establish a connection. */
561         res = res ? samr_connect( srv_name, 0x02000000,
562                                 &sam_pol) : False;
563
564         /* connect to the domain */
565         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
566                     &pol_dom) : False;
567
568         /* connect to the domain */
569         res1 = res ? samr_open_alias( &pol_dom,
570                     0x000f001f, alias_rid, &alias_pol) : False;
571
572         for (i = 1; i < num_sids && res2 && res1; i++)
573         {
574                 /* add a member to the alias */
575                 res2 = res2 ? samr_add_aliasmem(&alias_pol, &sids[i]) : False;
576
577                 if (res2)
578                 {
579                         sid_to_string(tmp, &sids[i]);
580                         report(out_hnd, "SID added to Alias 0x%x: %s\n", alias_rid, tmp);
581                 }
582         }
583
584         res1 = res1 ? samr_close(&alias_pol) : False;
585         res  = res  ? samr_close(&pol_dom) : False;
586         res  = res  ? samr_close(&sam_pol) : False;
587
588         if (sids != NULL)
589         {
590                 free(sids);
591         }
592         
593         free_char_array(num_names, names);
594
595         if (res && res1 && res2)
596         {
597                 DEBUG(5,("cmd_sam_add_aliasmem: succeeded\n"));
598                 report(out_hnd, "Add Domain Alias Member: OK\n");
599         }
600         else
601         {
602                 DEBUG(5,("cmd_sam_add_aliasmem: failed\n"));
603                 report(out_hnd, "Add Domain Alias Member: FAILED\n");
604         }
605 }
606
607
608 #if 0
609 /****************************************************************************
610 SAM create domain user.
611 ****************************************************************************/
612 void cmd_sam_create_dom_trusting(struct client_info *info, int argc, char *argv[])
613 {
614         fstring local_domain;
615         fstring local_pdc;
616
617         char *trusting_domain;
618         char *trusting_pdc;
619         fstring password;
620
621         fstring sid;
622         DOM_SID sid1;
623         uint32 user_rid; 
624
625         sid_copy(&sid1, &info->dom.level5_sid);
626         sid_to_string(sid, &sid1);
627         fstrcpy(domain, info->dom.level5_dom);
628
629         if (sid1.num_auths == 0)
630         {
631                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
632                 return;
633         }
634
635         if (argc < 3)
636         {
637                 report(out_hnd, "createtrusting: <Domain Name> <PDC Name> [password]\n");
638                 return;
639         }
640
641         argc--;
642         argv++;
643
644         trusting_domain = argv[0];
645
646         argc--;
647         argv++;
648
649         trusting_pdc = argv[0];
650
651         argc--;
652         argv++;
653
654         if (argc > 0)
655         {
656                 safe_strcpy(password, argv[0], sizeof(password)-1);
657         }
658         else
659         {
660                 fstring pass_str;
661                 char *pass;
662                 slprintf(pass_str, sizeof(pass_str)-1, "Enter %s's Password:",
663                          user_name);
664                 pass = (char*)getpass(pass_str);
665
666                 if (pass != NULL)
667                 {
668                         safe_strcpy(password, pass, sizeof(password)-1);
669                         set_passwd = True;
670                 }
671         }
672         report(out_hnd, "SAM Create Domain Trusting Account\n");
673
674         if (msrpc_sam_create_dom_user(srv_name,
675                                       acct_name, ACB_WSTRUST, &user_rid))
676         {
677                 report(out_hnd, "Create Domain User: OK\n");
678         }
679         else
680         {
681                 report(out_hnd, "Create Domain User: FAILED\n");
682         }
683 }
684 #endif
685
686 /****************************************************************************
687 SAM create domain user.
688 ****************************************************************************/
689 void cmd_sam_create_dom_user(struct client_info *info, int argc, char *argv[])
690 {
691         fstring domain;
692         fstring acct_name;
693         fstring name;
694         fstring sid;
695         DOM_SID sid1;
696         uint32 user_rid; 
697         uint16 acb_info = ACB_NORMAL;
698         BOOL join_domain = False;
699         int opt;
700         char *password = NULL;
701         int plen = 0;
702         int len = 0;
703         UNISTR2 upw;
704
705         fstring srv_name;
706         fstrcpy(srv_name, "\\\\");
707         fstrcat(srv_name, info->dest_host);
708         strupper(srv_name);
709
710
711         sid_copy(&sid1, &info->dom.level5_sid);
712         sid_to_string(sid, &sid1);
713         fstrcpy(domain, info->dom.level5_dom);
714
715         if (sid1.num_auths == 0)
716         {
717                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
718                 return;
719         }
720
721         if (argc < 2)
722         {
723                 report(out_hnd, "createuser: <acct name> [-i] [-s] [-j]\n");
724                 return;
725         }
726
727         argc--;
728         argv++;
729
730         safe_strcpy(acct_name, argv[0], sizeof(acct_name));
731         len = strlen(acct_name)-1;
732         if (acct_name[len] == '$')
733         {
734                 safe_strcpy(name, argv[0], sizeof(name));
735                 name[len] = 0;
736                 acb_info = ACB_WSTRUST;
737         }
738
739         while ((opt = getopt(argc, argv,"isj")) != EOF)
740         {
741                 switch (opt)
742                 {
743                         case 'i':
744                         {
745                                 acb_info = ACB_DOMTRUST;
746                                 break;
747                         }
748                         case 's':
749                         {
750                                 acb_info = ACB_SVRTRUST;
751                                 break;
752                         }
753                         case 'j':
754                         {
755                                 join_domain = True;
756                         }
757                 }
758         }
759
760         if (join_domain && acb_info == ACB_NORMAL)
761         {
762                 report(out_hnd, "can only join trust accounts to a domain\n");
763                 return;
764         }
765
766         report(out_hnd, "SAM Create Domain User\n");
767         report(out_hnd, "Domain: %s Name: %s ACB: %s\n",
768                           domain, acct_name,
769             pwdb_encode_acct_ctrl(acb_info, NEW_PW_FORMAT_SPACE_PADDED_LEN));
770
771         if (acb_info == ACB_WSTRUST || acb_info == ACB_SVRTRUST)
772         {
773                 upw.uni_str_len = 12;
774                 upw.uni_max_len = 12;
775                 generate_random_buffer((uchar*)upw.buffer,
776                                        upw.uni_str_len*2, True);
777                 password = (char*)upw.buffer;
778                 plen = upw.uni_str_len * 2;
779         }
780
781         if (msrpc_sam_create_dom_user(srv_name, &sid1,
782                                       acct_name, acb_info, password, plen,
783                                       &user_rid))
784         {
785                 report(out_hnd, "Create Domain User: OK\n");
786
787                 if (join_domain)
788                 {
789                         uchar ntpw[16];
790                         
791                         nt_owf_genW(&upw, ntpw);
792
793                         report(out_hnd, "Join %s to Domain %s", name, domain);
794                         if (create_trust_account_file(domain, name, ntpw))
795                         {
796                                 report(out_hnd, ": OK\n");
797                         }
798                         else
799                         {
800                                 report(out_hnd, ": FAILED\n");
801                         }
802                 }
803         }
804         else
805         {
806                 report(out_hnd, "Create Domain User: FAILED\n");
807         }
808 }
809
810
811 /****************************************************************************
812 SAM create domain alias.
813 ****************************************************************************/
814 void cmd_sam_create_dom_alias(struct client_info *info, int argc, char *argv[])
815 {
816         fstring srv_name;
817         fstring domain;
818         char *acct_name;
819         fstring acct_desc;
820         fstring sid;
821         DOM_SID sid1;
822         BOOL res = True;
823         BOOL res1 = True;
824         uint32 ace_perms = 0x02000000; /* permissions */
825         uint32 alias_rid; 
826         POLICY_HND sam_pol;
827         POLICY_HND pol_dom;
828
829         sid_copy(&sid1, &info->dom.level5_sid);
830         sid_to_string(sid, &sid1);
831         fstrcpy(domain, info->dom.level5_dom);
832
833         if (sid1.num_auths == 0)
834         {
835                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
836                 return;
837         }
838
839
840         fstrcpy(srv_name, "\\\\");
841         fstrcat(srv_name, info->dest_host);
842         strupper(srv_name);
843
844         if (argc < 2)
845         {
846                 report(out_hnd, "createalias: <acct name> [acct description]\n");
847         }
848
849         acct_name = argv[1];
850
851         if (argc < 3)
852         {
853                 acct_desc[0] = 0;
854         }
855         else
856         {
857                 safe_strcpy(acct_desc, argv[2], sizeof(acct_desc)-1);
858         }
859
860         report(out_hnd, "SAM Create Domain Alias\n");
861         report(out_hnd, "Domain: %s Name: %s Description: %s\n",
862                           domain, acct_name, acct_desc);
863
864         /* establish a connection. */
865         res = res ? samr_connect( srv_name, 0x02000000,
866                                 &sam_pol) : False;
867
868         /* connect to the domain */
869         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
870                     &pol_dom) : False;
871
872         /* create a domain alias */
873         res1 = res ? create_samr_domain_alias( &pol_dom,
874                                 acct_name, acct_desc, &alias_rid) : False;
875
876         res = res ? samr_close( &pol_dom) : False;
877
878         res = res ? samr_close( &sam_pol) : False;
879
880         if (res && res1)
881         {
882                 DEBUG(5,("cmd_sam_create_dom_alias: succeeded\n"));
883                 report(out_hnd, "Create Domain Alias: OK\n");
884         }
885         else
886         {
887                 DEBUG(5,("cmd_sam_create_dom_alias: failed\n"));
888                 report(out_hnd, "Create Domain Alias: FAILED\n");
889         }
890 }
891
892
893 /****************************************************************************
894 SAM delete group member.
895 ****************************************************************************/
896 void cmd_sam_del_groupmem(struct client_info *info, int argc, char *argv[])
897 {
898         fstring srv_name;
899         fstring domain;
900         fstring sid;
901         DOM_SID sid1;
902         POLICY_HND pol_grp;
903         BOOL res = True;
904         BOOL res1 = True;
905         BOOL res2 = True;
906         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
907         uint32 member_rid; 
908         uint32 group_rid;
909         POLICY_HND sam_pol;
910         POLICY_HND pol_dom;
911
912         sid_copy(&sid1, &info->dom.level5_sid);
913         sid_to_string(sid, &sid1);
914         fstrcpy(domain, info->dom.level5_dom);
915
916         if (sid1.num_auths == 0)
917         {
918                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
919                 return;
920         }
921
922         fstrcpy(srv_name, "\\\\");
923         fstrcat(srv_name, info->dest_host);
924         strupper(srv_name);
925
926         if (argc < 2)
927         {
928                 report(out_hnd, "delgroupmem: <group rid> [member rid1] [member rid2] ...\n");
929                 return;
930         }
931
932         argc--;
933         argv++;
934
935         group_rid = get_number(argv[0]);
936
937         report(out_hnd, "SAM Add Domain Group member\n");
938
939         /* establish a connection. */
940         res = res ? samr_connect( srv_name, 0x02000000,
941                                 &sam_pol) : False;
942
943         /* connect to the domain */
944         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
945                     &pol_dom) : False;
946
947         /* connect to the domain */
948         res1 = res ? samr_open_group( &pol_dom,
949                     0x0000001f, group_rid, &pol_grp) : False;
950
951         while (argc > 0 && res2 && res1)
952         {
953                 argc--;
954                 argv++;
955
956                 /* get a rid, delete a member from the group */
957                 member_rid = get_number(argv[0]);
958                 res2 = res2 ? samr_del_groupmem(&pol_grp, member_rid) : False;
959
960                 if (res2)
961                 {
962                         report(out_hnd, "RID deleted from Group 0x%x: 0x%x\n", group_rid, member_rid);
963                 }
964         }
965
966         res1 = res1 ? samr_close(&pol_grp) : False;
967         res  = res  ? samr_close(&pol_dom) : False;
968         res  = res  ? samr_close(&sam_pol) : False;
969
970         if (res && res1 && res2)
971         {
972                 DEBUG(5,("cmd_sam_del_groupmem: succeeded\n"));
973                 report(out_hnd, "Add Domain Group Member: OK\n");
974         }
975         else
976         {
977                 DEBUG(5,("cmd_sam_del_groupmem: failed\n"));
978                 report(out_hnd, "Add Domain Group Member: FAILED\n");
979         }
980 }
981
982
983 /****************************************************************************
984 SAM delete group.
985 ****************************************************************************/
986 void cmd_sam_delete_dom_group(struct client_info *info, int argc, char *argv[])
987 {
988         fstring srv_name;
989         fstring domain;
990         char *name;
991         fstring sid;
992         DOM_SID sid1;
993         POLICY_HND pol_grp;
994         BOOL res = True;
995         BOOL res1 = True;
996         BOOL res2 = True;
997         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
998         uint32 group_rid = 0;
999         char *names[1];
1000         uint32 rid [MAX_LOOKUP_SIDS];
1001         uint32 type[MAX_LOOKUP_SIDS];
1002         uint32 num_rids;
1003         POLICY_HND sam_pol;
1004         POLICY_HND pol_dom;
1005
1006         sid_copy(&sid1, &info->dom.level5_sid);
1007         sid_to_string(sid, &sid1);
1008         fstrcpy(domain, info->dom.level5_dom);
1009
1010         if (sid1.num_auths == 0)
1011         {
1012                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1013                 return;
1014         }
1015
1016         fstrcpy(srv_name, "\\\\");
1017         fstrcat(srv_name, info->dest_host);
1018         strupper(srv_name);
1019
1020         if (argc < 2)
1021         {
1022                 report(out_hnd, "delgroup <group name>\n");
1023                 return;
1024         }
1025
1026         name = argv[1];
1027
1028         report(out_hnd, "SAM Delete Domain Group\n");
1029
1030         /* establish a connection. */
1031         res = res ? samr_connect( srv_name, 0x02000000,
1032                                 &sam_pol) : False;
1033
1034         /* connect to the domain */
1035         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
1036                     &pol_dom) : False;
1037
1038         names[0] = name;
1039
1040         res1 = res ? samr_query_lookup_names( &pol_dom, 0x000003e8,
1041                     1, names,
1042                     &num_rids, rid, type) : False;
1043
1044         if (res1 && num_rids == 1)
1045         {
1046                 group_rid = rid[0];
1047         }
1048
1049         /* connect to the domain */
1050         res1 = res1 ? samr_open_group( &pol_dom,
1051                     0x0000001f, group_rid, &pol_grp) : False;
1052
1053         res2 = res1 ? samr_delete_dom_group(&pol_grp) : False;
1054
1055         res1 = res1 ? samr_close(&pol_grp) : False;
1056         res  = res  ? samr_close(&pol_dom) : False;
1057         res  = res  ? samr_close(&sam_pol) : False;
1058
1059         if (res && res1 && res2)
1060         {
1061                 DEBUG(5,("cmd_sam_delete_dom_group: succeeded\n"));
1062                 report(out_hnd, "Delete Domain Group: OK\n");
1063         }
1064         else
1065         {
1066                 DEBUG(5,("cmd_sam_delete_dom_group: failed\n"));
1067                 report(out_hnd, "Delete Domain Group: FAILED\n");
1068         }
1069 }
1070
1071
1072 /****************************************************************************
1073 SAM add group member.
1074 ****************************************************************************/
1075 void cmd_sam_add_groupmem(struct client_info *info, int argc, char *argv[])
1076 {
1077         fstring srv_name;
1078         fstring domain;
1079         fstring sid;
1080         DOM_SID sid1;
1081         POLICY_HND pol_grp;
1082         BOOL res = True;
1083         BOOL res1 = True;
1084         BOOL res2 = True;
1085         BOOL res3 = True;
1086         BOOL res4 = True;
1087         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
1088         uint32 group_rid[1];
1089         uint32 group_type[1];
1090         char **names = NULL;
1091         uint32 num_names = 0;
1092         fstring group_name;
1093         char *group_names[1];
1094         uint32 rid [MAX_LOOKUP_SIDS];
1095         uint32 type[MAX_LOOKUP_SIDS];
1096         uint32 num_rids;
1097         uint32 num_group_rids;
1098         uint32 i;
1099         DOM_SID sid_1_5_20;
1100         POLICY_HND sam_pol;
1101         POLICY_HND pol_dom;
1102         POLICY_HND pol_blt;
1103
1104         string_to_sid(&sid_1_5_20, "S-1-5-32");
1105
1106         sid_copy(&sid1, &info->dom.level5_sid);
1107         sid_to_string(sid, &sid1);
1108         fstrcpy(domain, info->dom.level5_dom);
1109
1110         if (sid1.num_auths == 0)
1111         {
1112                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1113                 return;
1114         }
1115
1116         fstrcpy(srv_name, "\\\\");
1117         fstrcat(srv_name, info->dest_host);
1118         strupper(srv_name);
1119
1120         if (argc < 3)
1121         {
1122                 report(out_hnd, "addgroupmem <group name> [member name1] [member name2] ...\n");
1123                 return;
1124         }
1125         
1126         argc--;
1127         argv++;
1128
1129         group_names[0] = argv[0];
1130
1131         argc--;
1132         argv++;
1133
1134         num_names = argc;
1135         names = argv;
1136
1137         report(out_hnd, "SAM Add Domain Group member\n");
1138
1139         /* establish a connection. */
1140         res = res ? samr_connect( srv_name, 0x02000000,
1141                                 &sam_pol) : False;
1142
1143         /* connect to the domain */
1144         res4 = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
1145                     &pol_dom) : False;
1146
1147         /* connect to the domain */
1148         res3 = res ? samr_open_domain( &sam_pol, ace_perms, &sid_1_5_20,
1149                     &pol_blt) : False;
1150
1151         res2 = res4 ? samr_query_lookup_names( &pol_dom, 0x000003e8,
1152                     1, group_names,
1153                     &num_group_rids, group_rid, group_type) : False;
1154
1155         /* open the group */
1156         res2 = res2 ? samr_open_group( &pol_dom,
1157                     0x0000001f, group_rid[0], &pol_grp) : False;
1158
1159         if (!res2 || (group_type != NULL && group_type[0] == SID_NAME_UNKNOWN))
1160         {
1161                 res2 = res3 ? samr_query_lookup_names( &pol_blt, 0x000003e8,
1162                             1, group_names, 
1163                             &num_group_rids, group_rid, group_type) : False;
1164
1165                 /* open the group */
1166                 res2 = res2 ? samr_open_group( &pol_blt,
1167                             0x0000001f, group_rid[0], &pol_grp) : False;
1168         }
1169
1170         if (res2 && group_type[0] == SID_NAME_ALIAS)
1171         {
1172                 report(out_hnd, "%s is a local alias, not a group.  Use addaliasmem command instead\n",
1173                         group_name);
1174                 return;
1175         }
1176         res1 = res2 ? samr_query_lookup_names( &pol_dom, 0x000003e8,
1177                     num_names, names,
1178                     &num_rids, rid, type) : False;
1179
1180         if (num_rids == 0)
1181         {
1182                 report(out_hnd, "Member names not known\n");
1183         }
1184         for (i = 0; i < num_rids && res2 && res1; i++)
1185         {
1186                 if (type[i] == SID_NAME_UNKNOWN)
1187                 {
1188                         report(out_hnd, "Name %s unknown\n", names[i]);
1189                 }
1190                 else
1191                 {
1192                         if (samr_add_groupmem(&pol_grp, rid[i]))
1193                         {
1194                                 report(out_hnd, "RID added to Group 0x%x: 0x%x\n",
1195                                                  group_rid[0], rid[i]);
1196                         }
1197                 }
1198         }
1199
1200         res1 = res ? samr_close(&pol_grp) : False;
1201         res1 = res3 ? samr_close(&pol_blt) : False;
1202         res1 = res4 ? samr_close(&pol_dom) : False;
1203         res  = res ? samr_close(&sam_pol) : False;
1204
1205         free_char_array(num_names, names);
1206         
1207         if (res && res1 && res2)
1208         {
1209                 DEBUG(5,("cmd_sam_add_groupmem: succeeded\n"));
1210                 report(out_hnd, "Add Domain Group Member: OK\n");
1211         }
1212         else
1213         {
1214                 DEBUG(5,("cmd_sam_add_groupmem: failed\n"));
1215                 report(out_hnd, "Add Domain Group Member: FAILED\n");
1216         }
1217 #if 0
1218         if (group_rid != NULL)
1219         {
1220                 free(group_rid);
1221         }
1222         if (group_type != NULL)
1223         {
1224                 free(group_type);
1225         }
1226 #endif
1227 }
1228
1229
1230 /****************************************************************************
1231 SAM create domain group.
1232 ****************************************************************************/
1233 void cmd_sam_create_dom_group(struct client_info *info, int argc, char *argv[])
1234 {
1235         fstring srv_name;
1236         fstring domain;
1237         char *acct_name;
1238         fstring acct_desc;
1239         fstring sid;
1240         DOM_SID sid1;
1241         BOOL res = True;
1242         BOOL res1 = True;
1243         uint32 ace_perms = 0x02000000; /* absolutely no idea. */
1244         uint32 group_rid; 
1245         POLICY_HND sam_pol;
1246         POLICY_HND pol_dom;
1247
1248         sid_copy(&sid1, &info->dom.level5_sid);
1249         sid_to_string(sid, &sid1);
1250         fstrcpy(domain, info->dom.level5_dom);
1251
1252         if (sid1.num_auths == 0)
1253         {
1254                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1255                 return;
1256         }
1257
1258
1259         fstrcpy(srv_name, "\\\\");
1260         fstrcat(srv_name, info->dest_host);
1261         strupper(srv_name);
1262
1263         if (argc < 2)
1264         {
1265                 report(out_hnd, "creategroup: <acct name> [acct description]\n");
1266         }
1267
1268         acct_name = argv[1];
1269
1270         if (argc < 3)
1271         {
1272                 acct_desc[0] = 0;
1273         }
1274         else
1275         {
1276                 safe_strcpy(acct_desc, argv[2], sizeof(acct_desc)-1);
1277         }
1278
1279
1280         report(out_hnd, "SAM Create Domain Group\n");
1281         report(out_hnd, "Domain: %s Name: %s Description: %s\n",
1282                           domain, acct_name, acct_desc);
1283
1284         /* establish a connection. */
1285         res = res ? samr_connect( srv_name, 0x02000000,
1286                                 &sam_pol) : False;
1287
1288         /* connect to the domain */
1289         res = res ? samr_open_domain( &sam_pol, ace_perms, &sid1,
1290                     &pol_dom) : False;
1291
1292         /* read some users */
1293         res1 = res ? create_samr_domain_group( &pol_dom,
1294                                 acct_name, acct_desc, &group_rid) : False;
1295
1296         res = res ? samr_close( &pol_dom) : False;
1297
1298         res = res ? samr_close( &sam_pol) : False;
1299
1300         if (res && res1)
1301         {
1302                 DEBUG(5,("cmd_sam_create_dom_group: succeeded\n"));
1303                 report(out_hnd, "Create Domain Group: OK\n");
1304         }
1305         else
1306         {
1307                 DEBUG(5,("cmd_sam_create_dom_group: failed\n"));
1308                 report(out_hnd, "Create Domain Group: FAILED\n");
1309         }
1310 }
1311
1312 /****************************************************************************
1313 experimental SAM users enum.
1314 ****************************************************************************/
1315 void cmd_sam_enum_users(struct client_info *info, int argc, char *argv[])
1316 {
1317         BOOL request_user_info  = False;
1318         BOOL request_group_info = False;
1319         BOOL request_alias_info = False;
1320         struct acct_info *sam = NULL;
1321         uint32 num_sam_entries = 0;
1322         int opt;
1323
1324         fstring srv_name;
1325         fstring domain;
1326         fstring sid;
1327         DOM_SID sid1;
1328         sid_copy(&sid1, &info->dom.level5_sid);
1329         sid_to_string(sid, &sid1);
1330         fstrcpy(domain, info->dom.level5_dom);
1331
1332         if (sid1.num_auths == 0)
1333         {
1334                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1335                 return;
1336         }
1337
1338         fstrcpy(srv_name, "\\\\");
1339         fstrcat(srv_name, info->dest_host);
1340         strupper(srv_name);
1341
1342         while ((opt = getopt(argc, argv, "uga")) != EOF)
1343         {
1344                 switch (opt)
1345                 {
1346                         case 'u':
1347                         {
1348                                 request_user_info  = True;
1349                                 break;
1350                         }
1351                         case 'g':
1352                         {
1353                                 request_group_info = True;
1354                                 break;
1355                         }
1356                         case 'a':
1357                         {
1358                                 request_alias_info = True;
1359                                 break;
1360                         }
1361                 }
1362         }
1363
1364         report(out_hnd, "SAM Enumerate Users\n");
1365
1366         msrpc_sam_enum_users(srv_name, domain, &sid1,
1367                     &sam, &num_sam_entries,
1368                     sam_display_user,
1369                     request_user_info  ? sam_display_user_info     : NULL,
1370                     request_group_info ? sam_display_group_members : NULL,
1371                     request_alias_info ? sam_display_group_members : NULL);
1372
1373         if (sam != NULL)
1374         {
1375                 free(sam);
1376         }
1377 }
1378
1379
1380 /****************************************************************************
1381 experimental SAM group query members.
1382 ****************************************************************************/
1383 void cmd_sam_query_groupmem(struct client_info *info, int argc, char *argv[])
1384 {
1385         fstring srv_name;
1386         fstring domain;
1387         fstring sid_str;
1388         DOM_SID sid;
1389         BOOL res = True;
1390         BOOL res1 = True;
1391
1392         char *group_name;
1393         char *names[1];
1394         uint32 num_rids;
1395         uint32 rid[MAX_LOOKUP_SIDS];
1396         uint32 type[MAX_LOOKUP_SIDS];
1397         POLICY_HND sam_pol;
1398         POLICY_HND pol_dom;
1399
1400         fstrcpy(domain, info->dom.level5_dom);
1401         sid_copy(&sid, &info->dom.level5_sid);
1402
1403         if (sid.num_auths == 0)
1404         {
1405                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1406                 return;
1407         }
1408
1409         if (argc < 2)
1410         {
1411                 report(out_hnd, "samgroupmem <name>\n");
1412                 return;
1413         }
1414
1415         group_name = argv[1];
1416
1417         fstrcpy(srv_name, "\\\\");
1418         fstrcat(srv_name, info->dest_host);
1419         strupper(srv_name);
1420
1421         sid_to_string(sid_str, &sid);
1422
1423         report(out_hnd, "SAM Query Group: %s\n", group_name);
1424         report(out_hnd, "From: %s To: %s Domain: %s SID: %s\n",
1425                           info->myhostname, srv_name, domain, sid_str);
1426
1427         /* establish a connection. */
1428         res = res ? samr_connect( srv_name, 0x02000000,
1429                                 &sam_pol) : False;
1430
1431         /* connect to the domain */
1432         res = res ? samr_open_domain( &sam_pol, 0x304, &sid,
1433                     &pol_dom) : False;
1434
1435         /* look up group rid */
1436         names[0] = group_name;
1437         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
1438                                         1, names,
1439                                         &num_rids, rid, type) : False;
1440
1441         if (res1 && num_rids == 1)
1442         {
1443                 res1 = req_groupmem_info( &pol_dom,
1444                                 domain,
1445                                 &sid,
1446                                 rid[0],
1447                                 names[0],
1448                                 sam_display_group_members);
1449         }
1450
1451         res = res ? samr_close( &sam_pol) : False;
1452
1453         res = res ? samr_close( &pol_dom) : False;
1454
1455         if (res1)
1456         {
1457                 DEBUG(5,("cmd_sam_query_group: succeeded\n"));
1458         }
1459         else
1460         {
1461                 DEBUG(5,("cmd_sam_query_group: failed\n"));
1462         }
1463 }
1464
1465
1466 /****************************************************************************
1467 experimental SAM group query.
1468 ****************************************************************************/
1469 void cmd_sam_query_group(struct client_info *info, int argc, char *argv[])
1470 {
1471         fstring srv_name;
1472         fstring domain;
1473         fstring sid_str;
1474         DOM_SID sid;
1475         BOOL res = True;
1476         BOOL res1 = True;
1477
1478         char *group_name;
1479         char *names[1];
1480         uint32 num_rids;
1481         uint32 rid[MAX_LOOKUP_SIDS];
1482         uint32 type[MAX_LOOKUP_SIDS];
1483         POLICY_HND sam_pol;
1484         POLICY_HND pol_dom;
1485
1486         fstrcpy(domain, info->dom.level5_dom);
1487         sid_copy(&sid, &info->dom.level5_sid);
1488
1489         if (sid.num_auths == 0)
1490         {
1491                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1492                 return;
1493         }
1494
1495         if (argc < 2)
1496         {
1497                 report(out_hnd, "samgroup <name>\n");
1498                 return;
1499         }
1500
1501         group_name = argv[1];
1502
1503         fstrcpy(srv_name, "\\\\");
1504         fstrcat(srv_name, info->dest_host);
1505         strupper(srv_name);
1506
1507         sid_to_string(sid_str, &sid);
1508
1509         report(out_hnd, "SAM Query Group: %s\n", group_name);
1510         report(out_hnd, "From: %s To: %s Domain: %s SID: %s\n",
1511                           info->myhostname, srv_name, domain, sid_str);
1512
1513         /* establish a connection. */
1514         res = res ? samr_connect( srv_name, 0x02000000,
1515                                 &sam_pol) : False;
1516
1517         /* connect to the domain */
1518         res = res ? samr_open_domain( &sam_pol, 0x304, &sid,
1519                     &pol_dom) : False;
1520
1521         /* look up group rid */
1522         names[0] = group_name;
1523         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
1524                                         1, names,
1525                                         &num_rids, rid, type) : False;
1526
1527         if (res1 && num_rids == 1)
1528         {
1529                 res1 = query_groupinfo( &pol_dom,
1530                                 domain,
1531                                 &sid,
1532                                 rid[0],
1533                                 sam_display_group_info);
1534         }
1535
1536         res = res ? samr_close( &sam_pol) : False;
1537
1538         res = res ? samr_close( &pol_dom) : False;
1539
1540         if (res1)
1541         {
1542                 DEBUG(5,("cmd_sam_query_group: succeeded\n"));
1543         }
1544         else
1545         {
1546                 DEBUG(5,("cmd_sam_query_group: failed\n"));
1547         }
1548 }
1549
1550
1551 /****************************************************************************
1552 experimental SAM user query.
1553 ****************************************************************************/
1554 void cmd_sam_query_user(struct client_info *info, int argc, char *argv[])
1555 {
1556         fstring srv_name;
1557         fstring domain;
1558         fstring sid_str;
1559         DOM_SID sid;
1560         BOOL res = True;
1561         BOOL res1 = True;
1562         int opt;
1563
1564         char *user_name;
1565         char *names[1];
1566         uint32 num_rids;
1567         uint32 rid[MAX_LOOKUP_SIDS];
1568         uint32 type[MAX_LOOKUP_SIDS];
1569         POLICY_HND sam_pol;
1570         POLICY_HND pol_dom;
1571
1572         BOOL request_user_info  = False;
1573         BOOL request_group_info = False;
1574         BOOL request_alias_info = False;
1575
1576         fstrcpy(domain, info->dom.level5_dom);
1577         sid_copy(&sid, &info->dom.level5_sid);
1578
1579         if (sid.num_auths == 0)
1580         {
1581                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1582                 return;
1583         }
1584
1585         if (argc < 2)
1586         {
1587                 report(out_hnd, "samuser <name> [-u] [-g] [-a]\n");
1588                 return;
1589         }
1590
1591         user_name = argv[1];
1592
1593         argc--;
1594         argv++;
1595
1596         while ((opt = getopt(argc, argv, "uga")) != EOF)
1597         {
1598                 switch (opt)
1599                 {
1600                         case 'u':
1601                         {
1602                                 request_user_info  = True;
1603                                 break;
1604                         }
1605                         case 'g':
1606                         {
1607                                 request_group_info = True;
1608                                 break;
1609                         }
1610                         case 'a':
1611                         {
1612                                 request_alias_info = True;
1613                                 break;
1614                         }
1615                 }
1616         }
1617
1618         fstrcpy(srv_name, "\\\\");
1619         fstrcat(srv_name, info->dest_host);
1620         strupper(srv_name);
1621
1622         sid_to_string(sid_str, &sid);
1623
1624         report(out_hnd, "SAM Query User: %s\n", user_name);
1625         report(out_hnd, "From: %s To: %s Domain: %s SID: %s\n",
1626                           info->myhostname, srv_name, domain, sid_str);
1627
1628         /* establish a connection. */
1629         res = res ? samr_connect( srv_name, 0x02000000,
1630                                 &sam_pol) : False;
1631
1632         /* connect to the domain */
1633         res = res ? samr_open_domain( &sam_pol, 0x304, &sid,
1634                     &pol_dom) : False;
1635
1636         /* look up user rid */
1637         names[0] = user_name;
1638         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
1639                                         1, names,
1640                                         &num_rids, rid, type) : False;
1641
1642         /* send user info query */
1643         if (res1 && num_rids == 1)
1644         {
1645                 msrpc_sam_user( &pol_dom, NULL,
1646                                 domain,
1647                                 &sid, NULL,
1648                                 rid[0], names[0],
1649                     sam_display_user,
1650                     request_user_info  ? sam_display_user_info     : NULL,
1651                     request_group_info ? sam_display_group_members : NULL,
1652                     request_alias_info ? sam_display_group_members : NULL);
1653         }
1654         else
1655         {
1656                 res1 = False;
1657         }
1658
1659         res = res ? samr_close( &sam_pol) : False;
1660         res = res ? samr_close( &pol_dom) : False;
1661
1662         if (res1)
1663         {
1664                 DEBUG(5,("cmd_sam_query_user: succeeded\n"));
1665         }
1666         else
1667         {
1668                 DEBUG(5,("cmd_sam_query_user: failed\n"));
1669         }
1670 }
1671
1672
1673 /****************************************************************************
1674 experimental SAM user set.
1675 ****************************************************************************/
1676 void cmd_sam_set_userinfo2(struct client_info *info, int argc, char *argv[])
1677 {
1678         fstring srv_name;
1679         fstring domain;
1680         fstring sid_str;
1681         DOM_SID sid;
1682         BOOL res = True;
1683         BOOL res1 = True;
1684         int opt;
1685         BOOL set_acb_bits = False;
1686
1687         fstring user_name;
1688
1689         char *names[1];
1690         uint32 num_rids;
1691         uint32 rid[MAX_LOOKUP_SIDS];
1692         uint32 type[MAX_LOOKUP_SIDS];
1693         POLICY_HND sam_pol;
1694         POLICY_HND pol_dom;
1695         SAM_USER_INFO_16 usr16;
1696         uint16 acb_set = 0x0;
1697
1698         fstrcpy(domain, info->dom.level5_dom);
1699         sid_copy(&sid, &info->dom.level5_sid);
1700
1701         if (sid.num_auths == 0)
1702         {
1703                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1704                 return;
1705         }
1706
1707         if (argc < 2)
1708         {
1709                 report(out_hnd, "samuserset2 <name> [-s <acb_bits>]\n");
1710                 return;
1711         }
1712
1713         argc--;
1714         argv++;
1715
1716         safe_strcpy(user_name, argv[0], sizeof(user_name));
1717
1718         while ((opt = getopt(argc, argv,"s:")) != EOF)
1719         {
1720                 switch (opt)
1721                 {
1722                         case 's':
1723                         {
1724                                 set_acb_bits = True;
1725                                 acb_set = get_number(optarg);
1726                                 break;
1727                         }
1728                 }
1729         }
1730
1731         fstrcpy(srv_name, "\\\\");
1732         fstrcat(srv_name, info->dest_host);
1733         strupper(srv_name);
1734
1735         sid_to_string(sid_str, &sid);
1736
1737         report(out_hnd, "SAM Set User Info: %s\n", user_name);
1738
1739         /* establish a connection. */
1740         res = res ? samr_connect( srv_name, 0x02000000,
1741                                 &sam_pol) : False;
1742
1743         /* connect to the domain */
1744         res = res ? samr_open_domain( &sam_pol, 0x02000000, &sid,
1745                     &pol_dom) : False;
1746
1747         /* look up user rid */
1748         names[0] = user_name;
1749         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
1750                                         1, names,
1751                                         &num_rids, rid, type) : False;
1752
1753         /* send set user info */
1754         if (res1 && num_rids == 1 && get_samr_query_userinfo( &pol_dom,
1755                                                     0x10, rid[0],
1756                                                     (void*)&usr16))
1757         {
1758                 void *usr = NULL;
1759                 uint32 switch_value = 0;
1760
1761                 if (set_acb_bits)
1762                 {
1763                         usr16.acb_info |= acb_set;
1764                 }
1765
1766                 if (True)
1767                 {
1768                         SAM_USER_INFO_16 *p = (SAM_USER_INFO_16 *)malloc(sizeof(SAM_USER_INFO_16));
1769                         p->acb_info = usr16.acb_info;
1770
1771                         usr = (void*)p;
1772                         switch_value = 16;
1773                 }
1774                 
1775                 if (usr != NULL)
1776                 {
1777                         res1 = set_samr_set_userinfo2( &pol_dom,
1778                                             switch_value, rid[0], usr);
1779                 }
1780         }
1781         res = res ? samr_close( &sam_pol) : False;
1782
1783         res = res ? samr_close( &pol_dom) : False;
1784
1785         if (res1)
1786         {
1787                 report(out_hnd, "Set User Info: OK\n");
1788                 DEBUG(5,("cmd_sam_query_user: succeeded\n"));
1789         }
1790         else
1791         {
1792                 report(out_hnd, "Set User Info: Failed\n");
1793                 DEBUG(5,("cmd_sam_query_user: failed\n"));
1794         }
1795 }
1796
1797 /****************************************************************************
1798 experimental SAM user set.
1799 ****************************************************************************/
1800 void cmd_sam_set_userinfo(struct client_info *info, int argc, char *argv[])
1801 {
1802         fstring srv_name;
1803         fstring domain;
1804         fstring sid_str;
1805         DOM_SID sid;
1806         BOOL res = True;
1807         BOOL res1 = True;
1808         int opt;
1809         BOOL set_passwd = False;
1810
1811         fstring user_name;
1812         fstring password;
1813
1814         char *names[1];
1815         uint32 num_rids;
1816         uint32 rid[MAX_LOOKUP_SIDS];
1817         uint32 type[MAX_LOOKUP_SIDS];
1818         POLICY_HND sam_pol;
1819         POLICY_HND pol_dom;
1820         SAM_USER_INFO_21 usr21;
1821
1822         fstrcpy(domain, info->dom.level5_dom);
1823         sid_copy(&sid, &info->dom.level5_sid);
1824
1825         if (sid.num_auths == 0)
1826         {
1827                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
1828                 return;
1829         }
1830
1831         argc--;
1832         argv++;
1833
1834         if (argc == 0)
1835         {
1836                 report(out_hnd, "samuserset <name> [-p password]\n");
1837                 return;
1838         }
1839
1840         safe_strcpy(user_name, argv[0], sizeof(user_name));
1841
1842         if (argc == 1)
1843         {
1844                 fstring pass_str;
1845                 char *pass;
1846                 slprintf(pass_str, sizeof(pass_str)-1, "Enter %s's Password:",
1847                          user_name);
1848                 pass = (char*)getpass(pass_str);
1849
1850                 if (pass != NULL)
1851                 {
1852                         safe_strcpy(password, pass,
1853                                     sizeof(password)-1);
1854                         set_passwd = True;
1855                 }
1856         }
1857         else
1858         {
1859                 while ((opt = getopt(argc, argv,"p:")) != EOF)
1860                 {
1861                         switch (opt)
1862                         {
1863                                 case 'p':
1864                                 {
1865                                         set_passwd = True;
1866                                         safe_strcpy(password, optarg,
1867                                                     sizeof(password)-1);
1868                                         break;
1869                                 }
1870                         }
1871                 }
1872         }
1873
1874         fstrcpy(srv_name, "\\\\");
1875         fstrcat(srv_name, info->dest_host);
1876         strupper(srv_name);
1877
1878         sid_to_string(sid_str, &sid);
1879
1880         report(out_hnd, "SAM Set User Info: %s\n", user_name);
1881         report(out_hnd, "Password: %s\n", password);
1882
1883         /* establish a connection. */
1884         res = res ? samr_connect( srv_name, 0x02000000,
1885                                 &sam_pol) : False;
1886
1887         /* connect to the domain */
1888         res = res ? samr_open_domain( &sam_pol, 0x02000000, &sid,
1889                     &pol_dom) : False;
1890
1891         /* look up user rid */
1892         names[0] = user_name;
1893         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
1894                                         1, names,
1895                                         &num_rids, rid, type) : False;
1896
1897         /* send set user info */
1898         if (res1 && num_rids == 1 && get_samr_query_userinfo( &pol_dom,
1899                                                     0x15, rid[0], &usr21))
1900         {
1901                 void *usr = NULL;
1902                 uint32 switch_value = 0;
1903                 char pwbuf[516];
1904
1905                 if (set_passwd)
1906                 {
1907                         encode_pw_buffer(pwbuf, password,
1908                                        strlen(password), True);
1909                 }
1910
1911                 if (True)
1912                 {
1913                         SAM_USER_INFO_24 *p = (SAM_USER_INFO_24*)malloc(sizeof(SAM_USER_INFO_24));
1914                         make_sam_user_info24(p, pwbuf, strlen(password));
1915
1916                         usr = p;
1917                         switch_value = 24;
1918                 }
1919                 
1920                 if (False)
1921                 {
1922                         SAM_USER_INFO_23 *p = (SAM_USER_INFO_23*)malloc(sizeof(SAM_USER_INFO_23));
1923                         /* send user info query, level 0x15 */
1924                         make_sam_user_info23W(p,
1925                                 &usr21.logon_time, 
1926                                 &usr21.logoff_time, 
1927                                 &usr21.kickoff_time, 
1928                                 &usr21.pass_last_set_time, 
1929                                 &usr21.pass_can_change_time, 
1930                                 &usr21.pass_must_change_time, 
1931
1932                                 &usr21.uni_user_name, 
1933                                 &usr21.uni_full_name,
1934                                 &usr21.uni_home_dir,
1935                                 &usr21.uni_dir_drive,
1936                                 &usr21.uni_logon_script,
1937                                 &usr21.uni_profile_path,
1938                                 &usr21.uni_acct_desc,
1939                                 &usr21.uni_workstations,
1940                                 &usr21.uni_unknown_str,
1941                                 &usr21.uni_munged_dial,
1942
1943                                 0x0, 
1944                                 usr21.group_rid,
1945                                 usr21.acb_info, 
1946
1947                                 0x09f827fa,
1948                                 usr21.logon_divs,
1949                                 &usr21.logon_hrs,
1950                                 usr21.unknown_5,
1951                                 pwbuf,
1952                                 usr21.unknown_6);
1953
1954                         usr = p;
1955                         switch_value = 23;
1956                 }
1957                 if (usr != NULL)
1958                 {
1959                         res1 = set_samr_set_userinfo( &pol_dom,
1960                                             switch_value, rid[0], usr);
1961                 }
1962         }
1963         res = res ? samr_close( &sam_pol) : False;
1964
1965         res = res ? samr_close( &pol_dom) : False;
1966
1967         if (res1)
1968         {
1969                 report(out_hnd, "Set User Info: OK\n");
1970                 DEBUG(5,("cmd_sam_query_user: succeeded\n"));
1971         }
1972         else
1973         {
1974                 report(out_hnd, "Set User Info: Failed\n");
1975                 DEBUG(5,("cmd_sam_query_user: failed\n"));
1976         }
1977 }
1978
1979 static void sam_display_disp_info(const char* domain, const DOM_SID *sid,
1980                                 uint16 info, uint32 num,
1981                                 SAM_DISPINFO_CTR *ctr)
1982                                 
1983 {
1984         report(out_hnd, "SAM Display Info for Domain %s\n", domain);
1985
1986         display_sam_disp_info_ctr(out_hnd, ACTION_HEADER   , info, num, ctr);
1987         display_sam_disp_info_ctr(out_hnd, ACTION_ENUMERATE, info, num, ctr);
1988         display_sam_disp_info_ctr(out_hnd, ACTION_FOOTER   , info, num, ctr);
1989 }
1990
1991 /****************************************************************************
1992 experimental SAM query display info.
1993 ****************************************************************************/
1994 void cmd_sam_query_dispinfo(struct client_info *info, int argc, char *argv[])
1995 {
1996         fstring srv_name;
1997         fstring domain;
1998         fstring sid;
1999         DOM_SID sid1;
2000         uint16 switch_value = 1;
2001         SAM_DISPINFO_CTR ctr;
2002         SAM_DISPINFO_1 inf1;
2003         uint32 num_entries;
2004
2005         sid_to_string(sid, &info->dom.level5_sid);
2006         fstrcpy(domain, info->dom.level5_dom);
2007
2008         if (sid1.num_auths == 0)
2009         {
2010                 fprintf(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2011                 return;
2012         }
2013
2014         string_to_sid(&sid1, sid);
2015
2016         fstrcpy(srv_name, "\\\\");
2017         fstrcat(srv_name, info->dest_host);
2018         strupper(srv_name);
2019
2020         if (argc > 1)
2021         {
2022                 switch_value = strtoul(argv[1], (char**)NULL, 10);
2023         }
2024
2025         ctr.sam.info1 = &inf1;
2026
2027         if (msrpc_sam_query_dispinfo( srv_name, domain, &sid1,
2028                     switch_value, 
2029                     &num_entries, &ctr, sam_display_disp_info))
2030         {
2031
2032                 DEBUG(5,("cmd_sam_query_dispinfo: succeeded\n"));
2033         }
2034         else
2035         {
2036                 DEBUG(5,("cmd_sam_query_dispinfo: failed\n"));
2037         }
2038 }
2039
2040 /****************************************************************************
2041 experimental SAM domain info query.
2042 ****************************************************************************/
2043 void cmd_sam_query_dominfo(struct client_info *info, int argc, char *argv[])
2044 {
2045         fstring domain;
2046         fstring sid;
2047         DOM_SID sid1;
2048         uint32 switch_value = 2;
2049         SAM_UNK_CTR ctr;
2050         fstring srv_name;
2051         fstrcpy(srv_name, "\\\\");
2052         fstrcat(srv_name, info->dest_host);
2053         strupper(srv_name);
2054
2055
2056         sid_to_string(sid, &info->dom.level5_sid);
2057         fstrcpy(domain, info->dom.level5_dom);
2058
2059         if (sid1.num_auths == 0)
2060         {
2061                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2062                 return;
2063         }
2064
2065         string_to_sid(&sid1, sid);
2066
2067         if (argc > 1)
2068         {
2069                 switch_value = strtoul(argv[1], (char**)NULL, 10);
2070         }
2071
2072         report(out_hnd, "SAM Query Domain Info: info level %d\n", switch_value);
2073         report(out_hnd, "From: %s Domain: %s SID: %s\n",
2074                           info->myhostname, domain, sid);
2075
2076         if (sam_query_dominfo(srv_name, &sid1, switch_value, &ctr))
2077         {
2078                 DEBUG(5,("cmd_sam_query_dominfo: succeeded\n"));
2079                 display_sam_unk_ctr(out_hnd, ACTION_HEADER   , switch_value, &ctr);
2080                 display_sam_unk_ctr(out_hnd, ACTION_ENUMERATE, switch_value, &ctr);
2081                 display_sam_unk_ctr(out_hnd, ACTION_FOOTER   , switch_value, &ctr);
2082         }
2083         else
2084         {
2085                 DEBUG(5,("cmd_sam_query_dominfo: failed\n"));
2086         }
2087 }
2088
2089 /****************************************************************************
2090 experimental SAM alias query members.
2091 ****************************************************************************/
2092 void cmd_sam_query_aliasmem(struct client_info *info, int argc, char *argv[])
2093 {
2094         fstring srv_name;
2095         fstring domain;
2096         fstring sid_str;
2097         DOM_SID sid;
2098         BOOL res = True;
2099         BOOL res1 = True;
2100
2101         char *alias_name;
2102         char *names[1];
2103         uint32 num_rids;
2104         uint32 rid[MAX_LOOKUP_SIDS];
2105         uint32 type[MAX_LOOKUP_SIDS];
2106         POLICY_HND sam_pol;
2107         POLICY_HND pol_dom;
2108
2109         fstrcpy(domain, info->dom.level5_dom);
2110         sid_copy(&sid, &info->dom.level5_sid);
2111
2112         if (sid.num_auths == 0)
2113         {
2114                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2115                 return;
2116         }
2117
2118         if (argc < 2)
2119         {
2120                 report(out_hnd, "samaliasmem <name>\n");
2121                 return;
2122         }
2123
2124         alias_name = argv[1];
2125
2126         fstrcpy(srv_name, "\\\\");
2127         fstrcat(srv_name, info->dest_host);
2128         strupper(srv_name);
2129
2130         sid_to_string(sid_str, &sid);
2131
2132         report(out_hnd, "SAM Query Alias: %s\n", alias_name);
2133         report(out_hnd, "From: %s To: %s Domain: %s SID: %s\n",
2134                           info->myhostname, srv_name, domain, sid_str);
2135
2136         /* establish a connection. */
2137         res = res ? samr_connect( srv_name, 0x02000000,
2138                                 &sam_pol) : False;
2139
2140         /* connect to the domain */
2141         res = res ? samr_open_domain( &sam_pol, 0x304, &sid,
2142                     &pol_dom) : False;
2143
2144         /* look up alias rid */
2145         names[0] = alias_name;
2146         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
2147                                         1, names,
2148                                         &num_rids, rid, type) : False;
2149
2150         if (res1 && num_rids == 1)
2151         {
2152                 res1 = req_aliasmem_info(srv_name,
2153                                 &pol_dom,
2154                                 domain,
2155                                 &sid,
2156                                 rid[0],
2157                                 names[0],
2158                                 sam_display_alias_members);
2159         }
2160
2161         res = res ? samr_close( &sam_pol) : False;
2162
2163         res = res ? samr_close( &pol_dom) : False;
2164
2165         if (res1)
2166         {
2167                 DEBUG(5,("cmd_sam_query_alias: succeeded\n"));
2168         }
2169         else
2170         {
2171                 DEBUG(5,("cmd_sam_query_alias: failed\n"));
2172         }
2173 }
2174
2175
2176 /****************************************************************************
2177 experimental SAM alias query.
2178 ****************************************************************************/
2179 void cmd_sam_query_alias(struct client_info *info, int argc, char *argv[])
2180 {
2181         fstring srv_name;
2182         fstring domain;
2183         fstring sid_str;
2184         DOM_SID sid;
2185         BOOL res = True;
2186         BOOL res1 = True;
2187
2188         char *alias_name;
2189         char *names[1];
2190         uint32 num_rids;
2191         uint32 rid[MAX_LOOKUP_SIDS];
2192         uint32 type[MAX_LOOKUP_SIDS];
2193         POLICY_HND sam_pol;
2194         POLICY_HND pol_dom;
2195
2196         fstrcpy(domain, info->dom.level5_dom);
2197         sid_copy(&sid, &info->dom.level5_sid);
2198
2199         if (sid.num_auths == 0)
2200         {
2201                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2202                 return;
2203         }
2204
2205         if (argc < 2)
2206         {
2207                 report(out_hnd, "samalias <name>\n");
2208                 return;
2209         }
2210
2211         alias_name = argv[1];
2212
2213         fstrcpy(srv_name, "\\\\");
2214         fstrcat(srv_name, info->dest_host);
2215         strupper(srv_name);
2216
2217         sid_to_string(sid_str, &sid);
2218
2219         report(out_hnd, "SAM Query Alias: %s\n", alias_name);
2220         report(out_hnd, "From: %s To: %s Domain: %s SID: %s\n",
2221                           info->myhostname, srv_name, domain, sid_str);
2222
2223         /* establish a connection. */
2224         res = res ? samr_connect( srv_name, 0x02000000,
2225                                 &sam_pol) : False;
2226
2227         /* connect to the domain */
2228         res = res ? samr_open_domain( &sam_pol, 0x304, &sid,
2229                     &pol_dom) : False;
2230
2231         /* look up alias rid */
2232         names[0] = alias_name;
2233         res1 = res ? samr_query_lookup_names( &pol_dom, 0x3e8,
2234                                         1, names,
2235                                         &num_rids, rid, type) : False;
2236
2237         if (res1 && num_rids == 1)
2238         {
2239                 res1 = query_aliasinfo( &pol_dom,
2240                                 domain,
2241                                 &sid,
2242                                 rid[0],
2243                                 sam_display_alias_info);
2244         }
2245
2246         res = res ? samr_close( &sam_pol) : False;
2247
2248         res = res ? samr_close( &pol_dom) : False;
2249
2250         if (res1)
2251         {
2252                 DEBUG(5,("cmd_sam_query_alias: succeeded\n"));
2253         }
2254         else
2255         {
2256                 DEBUG(5,("cmd_sam_query_alias: failed\n"));
2257         }
2258 }
2259
2260
2261 /****************************************************************************
2262 SAM aliases query.
2263 ****************************************************************************/
2264 void cmd_sam_enum_aliases(struct client_info *info, int argc, char *argv[])
2265 {
2266         BOOL request_member_info = False;
2267         BOOL request_alias_info = False;
2268         struct acct_info *sam = NULL;
2269         uint32 num_sam_entries = 0;
2270         int opt;
2271
2272         fstring domain;
2273         fstring srv_name;
2274         fstring sid;
2275         DOM_SID sid1;
2276         sid_copy(&sid1, &info->dom.level5_sid);
2277         sid_to_string(sid, &sid1);
2278         fstrcpy(domain, info->dom.level5_dom);
2279
2280         if (sid1.num_auths == 0)
2281         {
2282                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2283                 return;
2284         }
2285
2286         fstrcpy(srv_name, "\\\\");
2287         fstrcat(srv_name, info->dest_host);
2288         strupper(srv_name);
2289
2290         while ((opt = getopt(argc, argv, "ma")) != EOF)
2291         {
2292                 switch (opt)
2293                 {
2294                         case 'm':
2295                         {
2296                                 request_member_info  = True;
2297                                 break;
2298                         }
2299                         case 'a':
2300                         {
2301                                 request_alias_info = True;
2302                                 break;
2303                         }
2304                 }
2305         }
2306
2307         report(out_hnd, "SAM Enumerate Aliases\n");
2308
2309         msrpc_sam_enum_aliases(srv_name, domain, &sid1, 
2310                     &sam, &num_sam_entries,
2311                     sam_display_alias,
2312                     request_alias_info  ? sam_display_alias_info    : NULL,
2313                     request_member_info ? sam_display_alias_members : NULL);
2314
2315         if (sam != NULL)
2316         {
2317                 free(sam);
2318         }
2319 }
2320
2321 /****************************************************************************
2322 experimental SAM groups enum.
2323 ****************************************************************************/
2324 void cmd_sam_enum_groups(struct client_info *info, int argc, char *argv[])
2325 {
2326         BOOL request_member_info = False;
2327         BOOL request_group_info = False;
2328         struct acct_info *sam = NULL;
2329         uint32 num_sam_entries = 0;
2330         int opt;
2331
2332         fstring srv_name;
2333         fstring domain;
2334         fstring sid;
2335         DOM_SID sid1;
2336         sid_copy(&sid1, &info->dom.level5_sid);
2337         sid_to_string(sid, &sid1);
2338         fstrcpy(domain, info->dom.level5_dom);
2339
2340         if (sid1.num_auths == 0)
2341         {
2342                 report(out_hnd, "please use 'lsaquery' first, to ascertain the SID\n");
2343                 return;
2344         }
2345
2346         fstrcpy(srv_name, "\\\\");
2347         fstrcat(srv_name, info->dest_host);
2348         strupper(srv_name);
2349
2350         while ((opt = getopt(argc, argv, "mg")) != EOF)
2351         {
2352                 switch (opt)
2353                 {
2354                         case 'm':
2355                         {
2356                                 request_member_info  = True;
2357                                 break;
2358                         }
2359                         case 'g':
2360                         {
2361                                 request_group_info = True;
2362                                 break;
2363                         }
2364                 }
2365         }
2366
2367         report(out_hnd, "SAM Enumerate Groups\n");
2368
2369         msrpc_sam_enum_groups(srv_name, domain, &sid1, 
2370                     &sam, &num_sam_entries,
2371                     sam_display_group,
2372                     request_group_info  ? sam_display_group_info    : NULL,
2373                     request_member_info ? sam_display_group_members : NULL);
2374
2375         if (sam != NULL)
2376         {
2377                 free(sam);
2378         }
2379 }
2380
2381 /****************************************************************************
2382 experimental SAM domains enum.
2383 ****************************************************************************/
2384 void cmd_sam_enum_domains(struct client_info *info, int argc, char *argv[])
2385 {
2386         BOOL request_domain_info = False;
2387         struct acct_info *sam = NULL;
2388         uint32 num_sam_entries = 0;
2389         int opt;
2390
2391         fstring srv_name;
2392
2393         fstrcpy(srv_name, "\\\\");
2394         fstrcat(srv_name, info->dest_host);
2395         strupper(srv_name);
2396
2397         while ((opt = getopt(argc, argv, "i")) != EOF)
2398         {
2399                 switch (opt)
2400                 {
2401                         case 'i':
2402                         {
2403                                 request_domain_info= True;
2404                                 break;
2405                         }
2406                 }
2407         }
2408
2409         report(out_hnd, "SAM Enumerate Domains\n");
2410
2411         msrpc_sam_enum_domains(srv_name,
2412                     &sam, &num_sam_entries,
2413                     sam_display_domain);
2414
2415         if (sam != NULL)
2416         {
2417                 free(sam);
2418         }
2419 }
2420