399fb30603470c00a801ae126c63705de76f7951
[samba.git] / source3 / rpcclient / cmd_reg.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 extern struct cli_state *smb_cli;
35 extern int smb_tidx;
36
37 extern FILE* out_hnd;
38
39
40 /****************************************************************************
41 nt registry enum
42 ****************************************************************************/
43 void cmd_reg_enum(struct client_info *info)
44 {
45         fstring type;
46         uint32 unk_0;
47         uint32 unk_1;
48         BOOL res = True;
49         BOOL res1 = True;
50         BOOL res2 = True;
51         int i;
52
53         POLICY_HND key_pol;
54         fstring key_name;
55
56         /*
57          * query key info
58          */
59
60         uint32 unknown_0; 
61         uint32 unknown_1;
62         uint32 num_subkeys;
63         uint32 max_subkeylen;
64         uint32 unknown_4; 
65         uint32 num_values;
66         uint32 max_valnamelen;
67         uint32 max_valbufsize;
68         uint32 unknown_8;
69         NTTIME mod_time;
70
71         /*
72          * unknown 0x1a request
73          */
74
75         uint32 unk_1a_response;
76
77         DEBUG(5, ("cmd_reg_enum: smb_cli->fd:%d\n", smb_cli->fd));
78
79         if (!next_token(NULL, key_name, NULL, sizeof(key_name)))
80         {
81                 fprintf(out_hnd, "regenum key_name\n");
82                 return;
83         }
84
85         /* open WINREG session. */
86         res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False;
87
88         /* open registry receive a policy handle */
89         res = res ? do_reg_open_policy(smb_cli,
90                                 0x84E0, 0x02000000,
91                                 &info->dom.reg_pol_connect) : False;
92
93         /* open an entry */
94         res1 = res  ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect,
95                                  key_name, 0x02000000, &key_pol) : False;
96
97         /* query it */
98         res1 = res1 ? do_reg_query_info(smb_cli, &key_pol,
99                                 type, &unk_0, &unk_1) : False;
100
101         res1 = res1 ? do_reg_query_unk_10(smb_cli,
102                                 &key_pol,
103                                 &unknown_0, &unknown_1,
104                                 &num_subkeys, &max_subkeylen,
105                                 &unknown_4, &num_values,
106                                 &max_valnamelen, &max_valbufsize,
107                                 &unknown_8, &mod_time) : False;
108
109         if (res1)
110         {
111                 fprintf(out_hnd,"Registry Query Info Key\n");
112                 fprintf(out_hnd,"unk_0,1 : 0x%x 0x%x\n", unknown_0, unknown_1);
113                 fprintf(out_hnd,"subkeys, max_len: %d %d\n", num_subkeys, max_subkeylen);
114                 fprintf(out_hnd,"unk_4 : 0x%x\n", unknown_4);
115                 fprintf(out_hnd,"vals, max_len, max_size: 0x%x 0x%x 0x%x\n", num_values, max_valnamelen, max_valbufsize);
116                 fprintf(out_hnd,"unk_8: 0x%x\n", unknown_8);
117                 fprintf(out_hnd,"mod time: %s\n", http_timestring(nt_time_to_unix(&mod_time)));
118         }
119
120         for (i = 0; i < num_subkeys; i++)
121         {
122                 /*
123                  * enumerate key
124                  */
125
126                 fstring enum_name;
127                 uint32 enum_unk1;
128                 uint32 enum_unk2;
129                 time_t key_mod_time;
130
131                 /* unknown 1a it */
132                 res2 = res1 ? do_reg_unknown_1a(smb_cli, &key_pol,
133                                         &unk_1a_response) : False;
134
135                 if (res2 && unk_1a_response != 5)
136                 {
137                         fprintf(out_hnd,"Unknown 1a response: %x\n", unk_1a_response);
138                 }
139
140                 /* enum key */
141                 res2 = res2 ? do_reg_enum_key(smb_cli, &key_pol,
142                                         i, enum_name,
143                                         &enum_unk1, &enum_unk2,
144                                         &key_mod_time) : False;
145                 
146                 if (res2)
147                 {
148                         display_reg_key_info(out_hnd, ACTION_HEADER   , enum_name, key_mod_time);
149                         display_reg_key_info(out_hnd, ACTION_ENUMERATE, enum_name, key_mod_time);
150                         display_reg_key_info(out_hnd, ACTION_FOOTER   , enum_name, key_mod_time);
151                 }
152
153         }
154
155         for (i = 0; i < num_values; i++)
156         {
157                 /*
158                  * enumerate key
159                  */
160
161                 uint32 val_type;
162                 BUFFER2 value;
163                 fstring val_name;
164
165                 /* unknown 1a it */
166                 res2 = res1 ? do_reg_unknown_1a(smb_cli, &key_pol,
167                                         &unk_1a_response) : False;
168
169                 if (res2 && unk_1a_response != 5)
170                 {
171                         fprintf(out_hnd,"Unknown 1a response: %x\n", unk_1a_response);
172                 }
173
174                 /* enum key */
175                 res2 = res2 ? do_reg_enum_val(smb_cli, &key_pol,
176                                         i, max_valnamelen, max_valbufsize,
177                                         val_name, &val_type, &value) : False;
178                 
179                 if (res2)
180                 {
181                         display_reg_value_info(out_hnd, ACTION_HEADER   , val_name, val_type, &value);
182                         display_reg_value_info(out_hnd, ACTION_ENUMERATE, val_name, val_type, &value);
183                         display_reg_value_info(out_hnd, ACTION_FOOTER   , val_name, val_type, &value);
184                 }
185         }
186
187         /* close the handles */
188         res1 = res1 ? do_reg_close(smb_cli, &key_pol) : False;
189         res  = res  ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False;
190
191         /* close the session */
192         cli_nt_session_close(smb_cli);
193
194         if (res && res1 && res2)
195         {
196                 DEBUG(5,("cmd_reg_enum: query succeeded\n"));
197                 fprintf(out_hnd,"Registry Enumeration\n");
198                 fprintf(out_hnd,"Type: %s unk_0:%x unk_1:%x\n", type, unk_0, unk_1);
199         }
200         else
201         {
202                 DEBUG(5,("cmd_reg_enum: query failed\n"));
203         }
204 }
205
206 /****************************************************************************
207 nt registry test
208 ****************************************************************************/
209 void cmd_reg_test2(struct client_info *info)
210 {
211         BOOL res = True;
212         BOOL res1 = True;
213         BOOL res2 = True;
214         int i;
215
216         /*
217          * query key info
218          */
219
220         uint32 unknown_0; 
221         uint32 unknown_1;
222         uint32 num_subkeys;
223         uint32 max_subkeylen;
224         uint32 unknown_4; 
225         uint32 num_values;
226         uint32 max_valnamelen;
227         uint32 unknown_7;
228         uint32 unknown_8;
229         NTTIME mod_time;
230
231         /*
232          * unknown 0x1a request
233          */
234
235         uint32 unk_1a_response;
236
237         /*
238          * enumerate key
239          */
240
241         fstring enum_name;
242         uint32 enum_unk1;
243         uint32 enum_unk2;
244         time_t key_mod_time;
245
246         DEBUG(5, ("cmd_reg_test: smb_cli->fd:%d\n", smb_cli->fd));
247
248         /* open WINREG session. */
249         res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False;
250
251         /* open registry receive a policy handle */
252         res  = res ? do_reg_open_policy(smb_cli,
253                                 0x84E0, 0x02000000,
254                                 &info->dom.reg_pol_connect) : False;
255
256         res1 = res ? do_reg_open_unk_4(smb_cli,
257                                 0x84E0, 0x02000000,
258                                 &info->dom.reg_pol_unk_4  ) : False;
259
260         res2 = res1 ? do_reg_query_unk_10(smb_cli,
261                                 &info->dom.reg_pol_connect,
262                                 &unknown_0, &unknown_1,
263                                 &num_subkeys, &max_subkeylen,
264                                 &unknown_4, &num_values,
265                                 &max_valnamelen, &unknown_7,
266                                 &unknown_8, &mod_time) : False;
267
268         if (res2)
269         {
270                 fprintf(out_hnd,"Registry Query Info Key\n");
271                 fprintf(out_hnd,"unk_0,1 : 0x%x 0x%x\n", unknown_0, unknown_1);
272                 fprintf(out_hnd,"subkeys, max_len: %d %d\n", num_subkeys, max_subkeylen);
273                 fprintf(out_hnd,"unk_4 : 0x%x\n", unknown_4);
274                 fprintf(out_hnd,"vals, max_len : 0x%x 0x%x\n", num_values, max_valnamelen);
275                 fprintf(out_hnd,"unk_7, 8: 0x%x 0x%x\n", unknown_7, unknown_8);
276                 fprintf(out_hnd,"mod time: %s\n", http_timestring(nt_time_to_unix(&mod_time)));
277         }
278
279         for (i = 0; i < num_subkeys; i++)
280         {
281                 /* unknown 1a it */
282                 res2 = res1 ? do_reg_unknown_1a(smb_cli, &info->dom.reg_pol_connect,
283                                         &unk_1a_response) : False;
284
285                 if (res2)
286                 {
287                         fprintf(out_hnd,"Unknown 1a response: %x\n", unk_1a_response);
288                 }
289
290                 /* enum key */
291                 res2 = res2 ? do_reg_enum_key(smb_cli, &info->dom.reg_pol_connect,
292                                         i, enum_name,
293                                         &enum_unk1, &enum_unk2,
294                                         &key_mod_time) : False;
295                 
296                 if (res2)
297                 {
298                         fprintf(out_hnd,"Enum Key: %s  ", enum_name);
299                         fprintf(out_hnd,"unk (%08x %08x)  ", enum_unk1, enum_unk2);
300                         fprintf(out_hnd,"mod time: %s\n", http_timestring(key_mod_time));
301                 }
302         }
303
304         /* close the handles */
305         res1 = res1 ? do_reg_close(smb_cli, &info->dom.reg_pol_unk_4  ) : False;
306         res  = res  ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False;
307
308         /* close the session */
309         cli_nt_session_close(smb_cli);
310
311         if (res && res1 && res2)
312         {
313                 DEBUG(5,("cmd_reg_test2: query succeeded\n"));
314                 fprintf(out_hnd,"Registry Test2\n");
315         }
316         else
317         {
318                 DEBUG(5,("cmd_reg_test2: query failed\n"));
319         }
320 }
321
322 /****************************************************************************
323 nt registry security info
324 ****************************************************************************/
325 void cmd_reg_get_key_sec(struct client_info *info)
326 {
327         BOOL res = True;
328         BOOL res3 = True;
329         BOOL res4 = True;
330
331         POLICY_HND key_pol;
332         fstring key_name;
333
334         /*
335          * security info
336          */
337
338         uint32 sec_buf_size;
339         SEC_DESC_BUF sec_buf;
340
341         DEBUG(5, ("cmd_reg_get_key_sec: smb_cli->fd:%d\n", smb_cli->fd));
342
343         if (!next_token(NULL, key_name, NULL, sizeof(key_name)))
344         {
345                 fprintf(out_hnd, "regtest key_name\n");
346                 return;
347         }
348
349         /* open WINREG session. */
350         res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False;
351
352         /* open registry receive a policy handle */
353         res  = res ? do_reg_open_policy(smb_cli,
354                                 0x84E0, 0x02000000,
355                                 &info->dom.reg_pol_connect) : False;
356
357         /* open an entry */
358         res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect,
359                                  key_name, 0x02000000, &key_pol) : False;
360
361         /* query key sec info.  first call sets sec_buf_size. */
362         sec_buf_size = 0;
363         res4 = res3 ? do_reg_get_key_sec(smb_cli, &key_pol,
364                                 &sec_buf_size, &sec_buf) : False;
365         
366         res4 = res4 ? do_reg_get_key_sec(smb_cli, &key_pol,
367                                 &sec_buf_size, &sec_buf) : False;
368
369         if (res4 && sec_buf.len > 0)
370         {
371                 fprintf(out_hnd, "Security Info for %s: (%d)\n",
372                                  key_name, sec_buf_size);
373                 display_sec_desc(out_hnd, ACTION_HEADER   , &sec_buf.sec);
374                 display_sec_desc(out_hnd, ACTION_ENUMERATE, &sec_buf.sec);
375                 display_sec_desc(out_hnd, ACTION_FOOTER   , &sec_buf.sec);
376         }
377
378         /* close the key handle */
379         res3 = res3 ? do_reg_close(smb_cli, &key_pol) : False;
380
381         /* close the registry handles */
382         res  = res  ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False;
383
384         /* close the session */
385         cli_nt_session_close(smb_cli);
386
387         if (res && res3 && res4)
388         {
389                 DEBUG(5,("cmd_reg_test2: query succeeded\n"));
390                 fprintf(out_hnd,"Registry Test2\n");
391         }
392         else
393         {
394                 DEBUG(5,("cmd_reg_test2: query failed\n"));
395         }
396 }
397