changed syntax of registry commands so keys can start with HKLM or HKU.
[samba.git] / source3 / rpcclient / cmd_lsarpc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
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 cli_state *smb_cli;
37 extern int smb_tidx;
38
39 extern FILE* out_hnd;
40
41
42 /****************************************************************************
43 nt lsa query
44 ****************************************************************************/
45 void cmd_lsa_query_info(struct client_info *info)
46 {
47         fstring srv_name;
48
49         BOOL res = True;
50
51         fstrcpy(info->dom.level3_dom, "");
52         fstrcpy(info->dom.level5_dom, "");
53         ZERO_STRUCT(info->dom.level3_sid);
54         ZERO_STRUCT(info->dom.level5_sid);
55
56         fstrcpy(srv_name, "\\\\");
57         fstrcat(srv_name, info->myhostname);
58         strupper(srv_name);
59
60         DEBUG(4,("cmd_lsa_query_info: server:%s\n", srv_name));
61
62         DEBUG(5, ("cmd_lsa_query_info: smb_cli->fd:%d\n", smb_cli->fd));
63
64         /* open LSARPC session. */
65         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC) : False;
66
67         /* lookup domain controller; receive a policy handle */
68         res = res ? do_lsa_open_policy(smb_cli,
69                                 srv_name,
70                                 &info->dom.lsa_info_pol, False) : False;
71
72         /* send client info query, level 3.  receive domain name and sid */
73         res = res ? do_lsa_query_info_pol(smb_cli, 
74                                           &info->dom.lsa_info_pol, 0x03,
75                                           info->dom.level3_dom,
76                                           &info->dom.level3_sid) : False;
77
78         /* send client info query, level 5.  receive domain name and sid */
79         res = res ? do_lsa_query_info_pol(smb_cli,
80                                 &info->dom.lsa_info_pol, 0x05,
81                                 info->dom.level5_dom,
82                                 &info->dom.level5_sid) : False;
83
84         res = res ? do_lsa_close(smb_cli, &info->dom.lsa_info_pol) : False;
85
86         /* close the session */
87         cli_nt_session_close(smb_cli);
88
89         if (res)
90         {
91                 BOOL domain_something = False;
92                 fstring sid;
93                 DEBUG(5,("cmd_lsa_query_info: query succeeded\n"));
94
95                 fprintf(out_hnd, "LSA Query Info Policy\n");
96
97                 if (info->dom.level3_dom[0] != 0)
98                 {
99                         sid_to_string(sid, &info->dom.level3_sid);
100                         fprintf(out_hnd, "Domain Member     - Domain: %s SID: %s\n",
101                                 info->dom.level3_dom, sid);
102                         domain_something = True;
103                 }
104                 if (info->dom.level5_dom[0] != 0)
105                 {
106                         sid_to_string(sid, &info->dom.level5_sid);
107                         fprintf(out_hnd, "Domain Controller - Domain: %s SID: %s\n",
108                                 info->dom.level5_dom, sid);
109                         domain_something = True;
110                 }
111                 if (!domain_something)
112                 {
113                         fprintf(out_hnd, "%s is not a Domain Member or Controller\n",
114                             info->dest_host);
115                 }
116         }
117         else
118         {
119                 DEBUG(5,("cmd_lsa_query_info: query failed\n"));
120         }
121 }
122
123 /****************************************************************************
124 nt lsa query
125 ****************************************************************************/
126 void cmd_lsa_lookup_sids(struct client_info *info)
127 {
128         fstring temp;
129         int i;
130         pstring sid_name;
131         fstring srv_name;
132         DOM_SID sid[10];
133         DOM_SID *sids[10];
134         int num_sids = 0;
135         char **names = NULL;
136         int num_names = 0;
137
138         BOOL res = True;
139
140         fstrcpy(srv_name, "\\\\");
141         fstrcat(srv_name, info->myhostname);
142         strupper(srv_name);
143
144         DEBUG(4,("cmd_lsa_lookup_sids: server: %s\n", srv_name));
145
146         while (num_sids < 10 && next_token(NULL, temp, NULL, sizeof(temp)))
147         {
148                 if (strnequal("S-", temp, 2))
149                 {
150                         fstrcpy(sid_name, temp);
151                 }
152                 else
153                 {
154                         sid_to_string(sid_name, &info->dom.level5_sid);
155
156                         if (sid_name[0] == 0)
157                         {
158                                 fprintf(out_hnd, "please use lsaquery first or specify a complete SID\n");
159                                 return;
160                         }
161                                 
162                         fstrcat(sid_name, "-");
163                         fstrcat(sid_name, temp);
164                 }
165                 make_dom_sid(&sid[num_sids], sid_name);
166                 sids[num_sids] = &sid[num_sids];
167                 num_sids++;
168         }
169
170         if (num_sids == 0)
171         {
172                 fprintf(out_hnd, "lookupsid RID or SID\n");
173                 return;
174         }
175
176         /* open LSARPC session. */
177         res = res ? cli_nt_session_open(smb_cli, PIPE_LSARPC) : False;
178
179         /* lookup domain controller; receive a policy handle */
180         res = res ? do_lsa_open_policy(smb_cli,
181                                 srv_name,
182                                 &info->dom.lsa_info_pol, True) : False;
183
184         /* send lsa lookup sids call */
185         res = res ? do_lsa_lookup_sids(smb_cli, 
186                                        &info->dom.lsa_info_pol,
187                                        num_sids, sids,
188                                        &names, &num_names) : False;
189
190         res = res ? do_lsa_close(smb_cli, &info->dom.lsa_info_pol) : False;
191
192         /* close the session */
193         cli_nt_session_close(smb_cli);
194
195         if (res)
196         {
197                 DEBUG(5,("cmd_lsa_lookup_sids: query succeeded\n"));
198         }
199         else
200         {
201                 DEBUG(5,("cmd_lsa_lookup_sids: query failed\n"));
202         }
203         if (names != NULL)
204         {
205                 fprintf(out_hnd,"Lookup SIDS:\n");
206                 for (i = 0; i < num_names; i++)
207                 {
208                         sid_to_string(temp, sids[i]);
209                         fprintf(out_hnd, "SID: %s -> %s\n", temp, names[i]);
210                         if (names[i] != NULL)
211                         {
212                                 free(names[i]);
213                         }
214                 }
215                 free(names);
216         }
217 }
218