r6942: * merging the registry changes back to the 3.0 tree
[kai/samba.git] / source3 / utils / net_rpc_registry.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) Gerald (Jerry) Carter          2005
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19  
20 #include "includes.h"
21 #include "utils/net.h"
22 #include "regfio.h"
23 #include "reg_objects.h"
24
25
26 /********************************************************************
27 ********************************************************************/
28
29 char* dump_regval_type( uint32 type )
30 {
31         static fstring string;
32         
33         switch (type) {
34         case REG_SZ:
35                 fstrcpy( string, "REG_SZ" );
36                 break;
37         case REG_MULTI_SZ:
38                 fstrcpy( string, "REG_MULTI_SZ" );
39                 break;
40         case REG_EXPAND_SZ:
41                 fstrcpy( string, "REG_EXPAND_SZ" );
42                 break;
43         case REG_DWORD:
44                 fstrcpy( string, "REG_DWORD" );
45                 break;
46         case REG_BINARY:
47                 fstrcpy( string, "REG_BINARY" );
48                 break;
49         default:
50                 fstr_sprintf( string, "UNKNOWN [%d]", type );
51         }
52         
53         return string;
54 }
55 /********************************************************************
56 ********************************************************************/
57
58 void dump_regval_buffer( uint32 type, REGVAL_BUFFER *buffer )
59 {
60         pstring string;
61         uint32 value;
62         
63         switch (type) {
64         case REG_SZ:
65                 rpcstr_pull( string, buffer->buffer, sizeof(string), -1, STR_TERMINATE );
66                 d_printf("%s\n", string);
67                 break;
68         case REG_MULTI_SZ:
69                 d_printf("\n");
70                 break;
71         case REG_DWORD:
72                 value = IVAL( buffer->buffer, 0 );
73                 d_printf( "0x%x\n", value );
74                 break;
75         case REG_BINARY:
76                 d_printf("\n");
77                 break;
78         
79         
80         default:
81                 d_printf( "\tUnknown type [%d]\n", type );
82         }
83 }
84
85 /********************************************************************
86 ********************************************************************/
87
88 static NTSTATUS rpc_registry_enumerate_internal( const DOM_SID *domain_sid, const char *domain_name, 
89                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
90                                            int argc, const char **argv )
91 {
92         WERROR result = WERR_GENERAL_FAILURE;
93         uint32 hive;
94         pstring subpath;
95         POLICY_HND pol_hive, pol_key; 
96         uint32 idx;
97         
98         if (argc != 1 ) {
99                 d_printf("Usage:    net rpc enumerate <path> [recurse]\n");
100                 d_printf("Example:: net rpc enumerate 'HKLM\\Software\\Samba'\n");
101                 return NT_STATUS_OK;
102         }
103         
104         if ( !reg_split_hive( argv[0], &hive, subpath ) ) {
105                 d_printf("invalid registry path\n");
106                 return NT_STATUS_OK;
107         }
108         
109         /* open the top level hive and then the registry key */
110         
111         result = cli_reg_connect( cli, mem_ctx, hive, MAXIMUM_ALLOWED_ACCESS, &pol_hive );
112         if ( !W_ERROR_IS_OK(result) ) {
113                 d_printf("Unable to connect to remote registry\n");
114                 return werror_to_ntstatus(result);
115         }
116         
117         result = cli_reg_open_entry( cli, mem_ctx, &pol_hive, subpath, MAXIMUM_ALLOWED_ACCESS, &pol_key );
118         if ( !W_ERROR_IS_OK(result) ) {
119                 d_printf("Unable to open [%s]\n", argv[0]);
120                 return werror_to_ntstatus(result);
121         }
122         
123         /* get the subkeys */
124         
125         result = WERR_OK;
126         idx = 0;
127         while ( W_ERROR_IS_OK(result) ) {
128                 time_t modtime;
129                 fstring keyname, classname;
130                 
131                 result = cli_reg_enum_key( cli, mem_ctx, &pol_key, idx, 
132                         keyname, classname, &modtime );
133                         
134                 if ( W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
135                         result = WERR_OK;
136                         break;
137                 }
138                         
139                 d_printf("Keyname   = %s\n", keyname );
140                 d_printf("Classname = %s\n", classname );
141                 d_printf("Modtime   = %s\n", http_timestring(modtime) );
142                 d_printf("\n" );
143
144                 idx++;
145         }
146
147         if ( !W_ERROR_IS_OK(result) )
148                 goto out;
149         
150         /* get the values */
151         
152         result = WERR_OK;
153         idx = 0;
154         while ( W_ERROR_IS_OK(result) ) {
155                 uint32 type;
156                 fstring name;
157                 REGVAL_BUFFER value;
158                 
159                 fstrcpy( name, "" );
160                 ZERO_STRUCT( value );
161                 
162                 result = cli_reg_enum_val( cli, mem_ctx, &pol_key, idx, 
163                         name, &type, &value );
164                         
165                 if ( W_ERROR_EQUAL(result, WERR_NO_MORE_ITEMS) ) {
166                         result = WERR_OK;
167                         break;
168                 }
169                         
170                 d_printf("Valuename  = %s\n", name );
171                 d_printf("Type       = %s\n", dump_regval_type(type) );
172                 d_printf("Data       = " );
173                 dump_regval_buffer( type, &value );
174                 d_printf("\n" );
175
176                 idx++;
177         }
178         
179         
180 out:
181         /* cleanup */
182         
183         cli_reg_close( cli, mem_ctx, &pol_key );
184         cli_reg_close( cli, mem_ctx, &pol_hive );
185
186         return werror_to_ntstatus(result);
187 }
188
189 /********************************************************************
190 ********************************************************************/
191
192 static int rpc_registry_enumerate( int argc, const char **argv )
193 {
194         return run_rpc_command( NULL, PI_WINREG, 0, 
195                 rpc_registry_enumerate_internal, argc, argv );
196 }
197
198 /********************************************************************
199 ********************************************************************/
200
201 static NTSTATUS rpc_registry_backup_internal( const DOM_SID *domain_sid, const char *domain_name, 
202                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
203                                            int argc, const char **argv )
204 {
205         WERROR result = WERR_GENERAL_FAILURE;
206         uint32 hive;
207         pstring subpath;
208         POLICY_HND pol_hive, pol_key; 
209         REGF_FILE *regfile;
210         
211         if (argc != 2 ) {
212                 d_printf("Usage:    net rpc backup <path> <file> \n");
213                 return NT_STATUS_OK;
214         }
215         
216         if ( !reg_split_hive( argv[0], &hive, subpath ) ) {
217                 d_printf("invalid registry path\n");
218                 return NT_STATUS_OK;
219         }
220         
221         /* open the top level hive and then the registry key */
222         
223         result = cli_reg_connect( cli, mem_ctx, hive, MAXIMUM_ALLOWED_ACCESS, &pol_hive );
224         if ( !W_ERROR_IS_OK(result) ) {
225                 d_printf("Unable to connect to remote registry\n");
226                 return werror_to_ntstatus(result);
227         }
228         
229         result = cli_reg_open_entry( cli, mem_ctx, &pol_hive, subpath, MAXIMUM_ALLOWED_ACCESS, &pol_key );
230         if ( !W_ERROR_IS_OK(result) ) {
231                 d_printf("Unable to open [%s]\n", argv[0]);
232                 return werror_to_ntstatus(result);
233         }
234         
235         /* open the file */
236         
237         if ( !(regfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC), 0600 )) ) {
238                 d_printf("Unable to open registry file [%s]\n", argv[1]);
239                 return werror_to_ntstatus(WERR_GENERAL_FAILURE);
240         }
241         
242         
243         /* cleanup */
244         
245         regfio_close( regfile );
246         cli_reg_close( cli, mem_ctx, &pol_key );
247         cli_reg_close( cli, mem_ctx, &pol_hive );
248
249         return werror_to_ntstatus(result);
250 }
251
252 /********************************************************************
253 ********************************************************************/
254
255 static int rpc_registry_backup( int argc, const char **argv )
256 {
257         return run_rpc_command( NULL, PI_WINREG, 0, 
258                 rpc_registry_backup_internal, argc, argv );
259 }
260
261
262 /********************************************************************
263 ********************************************************************/
264
265 static void dump_values( REGF_NK_REC *nk )
266 {
267         int i, j;
268         pstring data_str;
269         uint32 data_size, data;
270
271         if ( !nk->values )
272                 return;
273
274         for ( i=0; i<nk->num_values; i++ ) {
275                 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
276                 d_printf( "(%s) ", dump_regval_type( nk->values[i].type ) );
277
278                 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
279                 switch ( nk->values[i].type ) {
280                         case REG_SZ:
281                                 rpcstr_pull( data_str, nk->values[i].data, sizeof(data_str), -1, STR_TERMINATE );
282                                 d_printf( "%s", data_str );
283                                 break;
284                         case REG_MULTI_SZ:
285                         case REG_EXPAND_SZ:
286                                 for ( j=0; j<data_size; j++ ) {
287                                         d_printf( "%c", nk->values[i].data[j] );
288                                 }
289                                 break;
290                         case REG_DWORD:
291                                 data = IVAL( nk->values[i].data, 0 );
292                                 d_printf("0x%x", data );
293                                 break;
294                         case REG_BINARY:
295                                 for ( j=0; j<data_size; j++ ) {
296                                         d_printf( "%x", nk->values[i].data[j] );
297                                 }
298                                 break;
299                         default:
300                                 d_printf("unknown");
301                                 break;
302                 }
303
304                 d_printf( "\n" );
305         }
306
307 }
308
309 /********************************************************************
310 ********************************************************************/
311
312 static BOOL dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
313 {
314         REGF_NK_REC *key;
315         pstring regpath;
316
317         /* depth first dump of the registry tree */
318
319         while ( (key = regfio_fetch_subkey( file, nk )) ) {
320                 pstr_sprintf( regpath, "%s\\%s", parent, key->keyname );
321                 d_printf("[%s]\n", regpath );
322                 dump_values( key );
323                 d_printf("\n");
324                 dump_registry_tree( file, key, regpath );
325         }
326         
327         return True;
328 }
329
330 /********************************************************************
331 ********************************************************************/
332
333 static BOOL write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk, 
334                                  REGF_NK_REC *parent, REGF_FILE *outfile,
335                                  const char *parentpath )
336 {
337         REGF_NK_REC *key, *subkey;
338         REGVAL_CTR values;
339         REGSUBKEY_CTR subkeys;
340         int i;
341         pstring path;
342
343         ZERO_STRUCT( values );
344         ZERO_STRUCT( subkeys );
345
346         regsubkey_ctr_init( &subkeys );
347         regval_ctr_init( &values );
348         
349         /* copy values into the REGVAL_CTR */
350         
351         for ( i=0; i<nk->num_values; i++ ) {
352                 regval_ctr_addvalue( &values, nk->values[i].valuename, nk->values[i].type,
353                         nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
354         }
355
356         /* copy subkeys into the REGSUBKEY_CTR */
357         
358         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
359                 regsubkey_ctr_addkey( &subkeys, subkey->keyname );
360         }
361         
362         key = regfio_write_key( outfile, nk->keyname, &values, &subkeys, nk->sec_desc->sec_desc, parent );
363
364         /* write each one of the subkeys out */
365
366         pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
367         nk->subkey_index = 0;
368         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
369                 write_registry_tree( infile, subkey, key, outfile, path );
370         }
371
372         regval_ctr_destroy( &values );
373         regsubkey_ctr_destroy( &subkeys );
374
375         d_printf("[%s]\n", path );
376         
377         return True;
378 }
379
380 /********************************************************************
381 ********************************************************************/
382
383 static int rpc_registry_dump( int argc, const char **argv )
384 {
385         REGF_FILE   *registry;
386         REGF_NK_REC *nk;
387         
388         if (argc != 1 ) {
389                 d_printf("Usage:    net rpc dump <file> \n");
390                 return 0;
391         }
392         
393         d_printf("Opening %s....", argv[0]);
394         if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
395                 d_printf("Failed to open %s for reading\n", argv[0]);
396                 return 1;
397         }
398         d_printf("ok\n");
399         
400         /* get the root of the registry file */
401         
402         nk = regfio_rootkey( registry );
403         d_printf("[%s]\n", nk->keyname);
404         dump_values( nk );
405         d_printf("\n");
406
407         dump_registry_tree( registry, nk, nk->keyname );
408
409 #if 0
410         talloc_report_full( registry->mem_ctx, stderr );
411 #endif  
412         d_printf("Closing registry...");
413         regfio_close( registry );
414         d_printf("ok\n");
415
416         return 0;
417 }
418
419 /********************************************************************
420 ********************************************************************/
421
422 static int rpc_registry_copy( int argc, const char **argv )
423 {
424         REGF_FILE   *infile, *outfile;
425         REGF_NK_REC *nk;
426         
427         if (argc != 2 ) {
428                 d_printf("Usage:    net rpc copy <srcfile> <newfile>\n");
429                 return 0;
430         }
431         
432         d_printf("Opening %s....", argv[0]);
433         if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
434                 d_printf("Failed to open %s for reading\n", argv[0]);
435                 return 1;
436         }
437         d_printf("ok\n");
438
439         d_printf("Opening %s....", argv[1]);
440         if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
441                 d_printf("Failed to open %s for writing\n", argv[1]);
442                 return 1;
443         }
444         d_printf("ok\n");
445         
446         /* get the root of the registry file */
447         
448         nk = regfio_rootkey( infile );
449         d_printf("RootKey: [%s]\n", nk->keyname);
450
451         write_registry_tree( infile, nk, NULL, outfile, "" );
452
453         d_printf("Closing %s...", argv[1]);
454         regfio_close( outfile );
455         d_printf("ok\n");
456
457         d_printf("Closing %s...", argv[0]);
458         regfio_close( infile );
459         d_printf("ok\n");
460
461         return 0;
462 }
463
464 /********************************************************************
465 ********************************************************************/
466
467 static int net_help_registry( int argc, const char **argv )
468 {
469         d_printf("net rpc registry enumerate <path> [recurse]  Enumerate the subkeya and values for a given registry path\n");
470         d_printf("net rpc registry backup <path> <file>        Backup a registry tree to a local file\n");
471         d_printf("net rpc registry dump <file>                 Dump the contents of a registry file to stdout\n");
472         
473         return -1;
474 }
475
476 /********************************************************************
477 ********************************************************************/
478
479 int net_rpc_registry(int argc, const char **argv) 
480 {
481         struct functable func[] = {
482                 {"enumerate", rpc_registry_enumerate},
483                 {"backup",    rpc_registry_backup},
484                 {"dump",      rpc_registry_dump},
485                 {"copy",      rpc_registry_copy},
486                 {NULL, NULL}
487         };
488         
489         if ( argc )
490                 return net_run_function( argc, argv, func, net_help_registry );
491                 
492         return net_help_registry( argc, argv );
493 }
494
495