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