2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2005
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.
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.
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. */
21 #include "utils/net.h"
23 #include "reg_objects.h"
25 /********************************************************************
26 ********************************************************************/
28 char* dump_regval_type( uint32 type
)
30 static fstring string
;
34 fstrcpy( string
, "REG_SZ" );
37 fstrcpy( string
, "REG_MULTI_SZ" );
40 fstrcpy( string
, "REG_EXPAND_SZ" );
43 fstrcpy( string
, "REG_DWORD" );
46 fstrcpy( string
, "REG_BINARY" );
49 fstr_sprintf( string
, "UNKNOWN [%d]", type
);
54 /********************************************************************
55 ********************************************************************/
57 void dump_regval_buffer( uint32 type
, REGVAL_BUFFER
*buffer
)
64 rpcstr_pull( string
, buffer
->buffer
, sizeof(string
), -1, STR_TERMINATE
);
65 d_printf("%s\n", string
);
71 value
= IVAL( buffer
->buffer
, 0 );
72 d_printf( "0x%x\n", value
);
80 d_printf( "\tUnknown type [%d]\n", type
);
84 /********************************************************************
85 ********************************************************************/
87 static NTSTATUS
rpc_registry_enumerate_internal(const DOM_SID
*domain_sid
,
88 const char *domain_name
,
89 struct cli_state
*cli
,
90 struct rpc_pipe_client
*pipe_hnd
,
95 WERROR result
= WERR_GENERAL_FAILURE
;
98 POLICY_HND pol_hive
, pol_key
;
102 d_printf("Usage: net rpc enumerate <path> [recurse]\n");
103 d_printf("Example: net rpc enumerate 'HKLM\\Software\\Samba'\n");
107 if ( !reg_split_hive( argv
[0], &hive
, subpath
) ) {
108 d_fprintf(stderr
, "invalid registry path\n");
112 /* open the top level hive and then the registry key */
114 result
= rpccli_reg_connect(pipe_hnd
, mem_ctx
, hive
, MAXIMUM_ALLOWED_ACCESS
, &pol_hive
);
115 if ( !W_ERROR_IS_OK(result
) ) {
116 d_fprintf(stderr
, "Unable to connect to remote registry\n");
117 return werror_to_ntstatus(result
);
120 if ( strlen( subpath
) != 0 ) {
121 result
= rpccli_reg_open_entry(pipe_hnd
, mem_ctx
, &pol_hive
, subpath
, MAXIMUM_ALLOWED_ACCESS
, &pol_key
);
122 if ( !W_ERROR_IS_OK(result
) ) {
123 d_fprintf(stderr
, "Unable to open [%s]\n", argv
[0]);
124 return werror_to_ntstatus(result
);
128 /* get the subkeys */
132 while ( W_ERROR_IS_OK(result
) ) {
134 fstring keyname
, classname
;
136 result
= rpccli_reg_enum_key(pipe_hnd
, mem_ctx
, &pol_key
, idx
,
137 keyname
, classname
, &modtime
);
139 if ( W_ERROR_EQUAL(result
, WERR_NO_MORE_ITEMS
) ) {
144 d_printf("Keyname = %s\n", keyname
);
145 d_printf("Classname = %s\n", classname
);
146 d_printf("Modtime = %s\n", http_timestring(modtime
) );
152 if ( !W_ERROR_IS_OK(result
) )
159 while ( W_ERROR_IS_OK(result
) ) {
165 ZERO_STRUCT( value
);
167 result
= rpccli_reg_enum_val(pipe_hnd
, mem_ctx
, &pol_key
, idx
,
168 name
, &type
, &value
);
170 if ( W_ERROR_EQUAL(result
, WERR_NO_MORE_ITEMS
) ) {
175 d_printf("Valuename = %s\n", name
);
176 d_printf("Type = %s\n", dump_regval_type(type
) );
177 d_printf("Data = " );
178 dump_regval_buffer( type
, &value
);
188 if ( strlen( subpath
) != 0 )
189 rpccli_reg_close(pipe_hnd
, mem_ctx
, &pol_key
);
190 rpccli_reg_close(pipe_hnd
, mem_ctx
, &pol_hive
);
192 return werror_to_ntstatus(result
);
195 /********************************************************************
196 ********************************************************************/
198 static int rpc_registry_enumerate( int argc
, const char **argv
)
200 return run_rpc_command( NULL
, PI_WINREG
, 0,
201 rpc_registry_enumerate_internal
, argc
, argv
);
204 /********************************************************************
205 ********************************************************************/
207 static NTSTATUS
rpc_registry_save_internal(const DOM_SID
*domain_sid
,
208 const char *domain_name
,
209 struct cli_state
*cli
,
210 struct rpc_pipe_client
*pipe_hnd
,
215 WERROR result
= WERR_GENERAL_FAILURE
;
218 POLICY_HND pol_hive
, pol_key
;
221 d_printf("Usage: net rpc backup <path> <file> \n");
225 if ( !reg_split_hive( argv
[0], &hive
, subpath
) ) {
226 d_fprintf(stderr
, "invalid registry path\n");
230 /* open the top level hive and then the registry key */
232 result
= rpccli_reg_connect(pipe_hnd
, mem_ctx
, hive
, MAXIMUM_ALLOWED_ACCESS
, &pol_hive
);
233 if ( !W_ERROR_IS_OK(result
) ) {
234 d_fprintf(stderr
, "Unable to connect to remote registry\n");
235 return werror_to_ntstatus(result
);
238 result
= rpccli_reg_open_entry(pipe_hnd
, mem_ctx
, &pol_hive
, subpath
, MAXIMUM_ALLOWED_ACCESS
, &pol_key
);
239 if ( !W_ERROR_IS_OK(result
) ) {
240 d_fprintf(stderr
, "Unable to open [%s]\n", argv
[0]);
241 return werror_to_ntstatus(result
);
244 result
= rpccli_reg_save_key(pipe_hnd
, mem_ctx
, &pol_key
, argv
[1] );
245 if ( !W_ERROR_IS_OK(result
) ) {
246 d_fprintf(stderr
, "Unable to save [%s] to %s:%s\n", argv
[0], cli
->desthost
, argv
[1]);
252 rpccli_reg_close(pipe_hnd
, mem_ctx
, &pol_key
);
253 rpccli_reg_close(pipe_hnd
, mem_ctx
, &pol_hive
);
255 return werror_to_ntstatus(result
);
258 /********************************************************************
259 ********************************************************************/
261 static int rpc_registry_save( int argc
, const char **argv
)
263 return run_rpc_command( NULL
, PI_WINREG
, 0,
264 rpc_registry_save_internal
, argc
, argv
);
268 /********************************************************************
269 ********************************************************************/
271 static void dump_values( REGF_NK_REC
*nk
)
275 uint32 data_size
, data
;
280 for ( i
=0; i
<nk
->num_values
; i
++ ) {
281 d_printf( "\"%s\" = ", nk
->values
[i
].valuename
? nk
->values
[i
].valuename
: "(default)" );
282 d_printf( "(%s) ", dump_regval_type( nk
->values
[i
].type
) );
284 data_size
= nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
;
285 switch ( nk
->values
[i
].type
) {
287 rpcstr_pull( data_str
, nk
->values
[i
].data
, sizeof(data_str
), -1, STR_TERMINATE
);
288 d_printf( "%s", data_str
);
292 for ( j
=0; j
<data_size
; j
++ ) {
293 d_printf( "%c", nk
->values
[i
].data
[j
] );
297 data
= IVAL( nk
->values
[i
].data
, 0 );
298 d_printf("0x%x", data
);
301 for ( j
=0; j
<data_size
; j
++ ) {
302 d_printf( "%x", nk
->values
[i
].data
[j
] );
315 /********************************************************************
316 ********************************************************************/
318 static BOOL
dump_registry_tree( REGF_FILE
*file
, REGF_NK_REC
*nk
, const char *parent
)
323 /* depth first dump of the registry tree */
325 while ( (key
= regfio_fetch_subkey( file
, nk
)) ) {
326 pstr_sprintf( regpath
, "%s\\%s", parent
, key
->keyname
);
327 d_printf("[%s]\n", regpath
);
330 dump_registry_tree( file
, key
, regpath
);
336 /********************************************************************
337 ********************************************************************/
339 static BOOL
write_registry_tree( REGF_FILE
*infile
, REGF_NK_REC
*nk
,
340 REGF_NK_REC
*parent
, REGF_FILE
*outfile
,
341 const char *parentpath
)
343 REGF_NK_REC
*key
, *subkey
;
345 REGSUBKEY_CTR
*subkeys
;
349 if ( !( subkeys
= TALLOC_ZERO_P( infile
->mem_ctx
, REGSUBKEY_CTR
)) ) {
350 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
354 if ( !(values
= TALLOC_ZERO_P( subkeys
, REGVAL_CTR
)) ) {
355 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
359 /* copy values into the REGVAL_CTR */
361 for ( i
=0; i
<nk
->num_values
; i
++ ) {
362 regval_ctr_addvalue( values
, nk
->values
[i
].valuename
, nk
->values
[i
].type
,
363 (const char *)nk
->values
[i
].data
, (nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
) );
366 /* copy subkeys into the REGSUBKEY_CTR */
368 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
369 regsubkey_ctr_addkey( subkeys
, subkey
->keyname
);
372 key
= regfio_write_key( outfile
, nk
->keyname
, values
, subkeys
, nk
->sec_desc
->sec_desc
, parent
);
374 /* write each one of the subkeys out */
376 pstr_sprintf( path
, "%s%s%s", parentpath
, parent
? "\\" : "", nk
->keyname
);
377 nk
->subkey_index
= 0;
378 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
379 write_registry_tree( infile
, subkey
, key
, outfile
, path
);
382 TALLOC_FREE( subkeys
);
384 d_printf("[%s]\n", path
);
389 /********************************************************************
390 ********************************************************************/
392 static int rpc_registry_dump( int argc
, const char **argv
)
398 d_printf("Usage: net rpc dump <file> \n");
402 d_printf("Opening %s....", argv
[0]);
403 if ( !(registry
= regfio_open( argv
[0], O_RDONLY
, 0)) ) {
404 d_fprintf(stderr
, "Failed to open %s for reading\n", argv
[0]);
409 /* get the root of the registry file */
411 nk
= regfio_rootkey( registry
);
412 d_printf("[%s]\n", nk
->keyname
);
416 dump_registry_tree( registry
, nk
, nk
->keyname
);
419 talloc_report_full( registry
->mem_ctx
, stderr
);
421 d_printf("Closing registry...");
422 regfio_close( registry
);
428 /********************************************************************
429 ********************************************************************/
431 static int rpc_registry_copy( int argc
, const char **argv
)
433 REGF_FILE
*infile
, *outfile
;
438 d_printf("Usage: net rpc copy <srcfile> <newfile>\n");
442 d_printf("Opening %s....", argv
[0]);
443 if ( !(infile
= regfio_open( argv
[0], O_RDONLY
, 0 )) ) {
444 d_fprintf(stderr
, "Failed to open %s for reading\n", argv
[0]);
449 d_printf("Opening %s....", argv
[1]);
450 if ( !(outfile
= regfio_open( argv
[1], (O_RDWR
|O_CREAT
|O_TRUNC
), (S_IREAD
|S_IWRITE
) )) ) {
451 d_fprintf(stderr
, "Failed to open %s for writing\n", argv
[1]);
452 goto out_close_infile
;
456 /* get the root of the registry file */
458 nk
= regfio_rootkey( infile
);
459 d_printf("RootKey: [%s]\n", nk
->keyname
);
461 write_registry_tree( infile
, nk
, NULL
, outfile
, "" );
465 d_printf("Closing %s...", argv
[1]);
466 regfio_close( outfile
);
470 d_printf("Closing %s...", argv
[0]);
471 regfio_close( infile
);
477 /********************************************************************
478 ********************************************************************/
480 static int net_help_registry( int argc
, const char **argv
)
482 d_printf("net rpc registry enumerate <path> [recurse] Enumerate the subkeya and values for a given registry path\n");
483 d_printf("net rpc registry save <path> <file> Backup a registry tree to a file on the server\n");
484 d_printf("net rpc registry dump <file> Dump the contents of a registry file to stdout\n");
489 /********************************************************************
490 ********************************************************************/
492 int net_rpc_registry(int argc
, const char **argv
)
494 struct functable func
[] = {
495 {"enumerate", rpc_registry_enumerate
},
496 {"save", rpc_registry_save
},
497 {"dump", rpc_registry_dump
},
498 {"copy", rpc_registry_copy
},
503 return net_run_function( argc
, argv
, func
, net_help_registry
);
505 return net_help_registry( argc
, argv
);