r19817: Implement net rpc registry setvalue (only dword and sz so far)
[Samba.git] / source / utils / net_rpc_registry.c
blob10848d0afd6537f9bff48209d831715a9b27f720
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) Gerald (Jerry) Carter 2005-2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "regfio.h"
24 #include "reg_objects.h"
26 static BOOL reg_hive_key(const char *fullname, uint32 *reg_type,
27 const char **key_name)
29 const char *sep;
30 ptrdiff_t len;
32 sep = strchr_m(fullname, '\\');
34 if (sep != NULL) {
35 len = sep - fullname;
36 *key_name = sep+1;
38 else {
39 len = strlen(fullname);
40 *key_name = "";
43 if (strnequal(fullname, "HKLM", len) ||
44 strnequal(fullname, "HKEY_LOCAL_MACHINE", len))
45 (*reg_type) = HKEY_LOCAL_MACHINE;
46 else if (strnequal(fullname, "HKCR", len) ||
47 strnequal(fullname, "HKEY_CLASSES_ROOT", len))
48 (*reg_type) = HKEY_CLASSES_ROOT;
49 else if (strnequal(fullname, "HKU", len) ||
50 strnequal(fullname, "HKEY_USERS", len))
51 (*reg_type) = HKEY_USERS;
52 else if (strnequal(fullname, "HKPD", len) ||
53 strnequal(fullname, "HKEY_PERFORMANCE_DATA", len))
54 (*reg_type) = HKEY_PERFORMANCE_DATA;
55 else {
56 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
57 fullname));
58 return False;
61 return True;
64 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
65 struct rpc_pipe_client *pipe_hnd,
66 const char *name, uint32 access_mask,
67 struct policy_handle *hive_hnd,
68 struct policy_handle *key_hnd)
70 uint32 hive;
71 NTSTATUS status;
72 struct winreg_String key;
74 if (!reg_hive_key(name, &hive, &key.name)) {
75 return NT_STATUS_INVALID_PARAMETER;
78 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, access_mask,
79 hive_hnd);
80 if (!(NT_STATUS_IS_OK(status))) {
81 return status;
84 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, hive_hnd, key, 0,
85 access_mask, key_hnd);
86 if (!(NT_STATUS_IS_OK(status))) {
87 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, hive_hnd);
88 return status;
91 return NT_STATUS_OK;
94 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
95 struct rpc_pipe_client *pipe_hnd,
96 struct policy_handle *key_hnd,
97 uint32 *pnum_keys, char ***pnames,
98 char ***pclasses, NTTIME ***pmodtimes)
100 TALLOC_CTX *mem_ctx;
101 NTSTATUS status;
102 uint32 num_subkeys, max_subkeylen, max_classlen;
103 uint32 num_values, max_valnamelen, max_valbufsize;
104 uint32 i;
105 NTTIME last_changed_time;
106 uint32 secdescsize;
107 struct winreg_String classname;
108 char **names, **classes;
109 NTTIME **modtimes;
111 if (!(mem_ctx = talloc_new(ctx))) {
112 return NT_STATUS_NO_MEMORY;
115 ZERO_STRUCT(classname);
116 status = rpccli_winreg_QueryInfoKey(
117 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
118 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
119 &max_valbufsize, &secdescsize, &last_changed_time );
121 if (!NT_STATUS_IS_OK(status)) {
122 goto error;
125 if (num_subkeys == 0) {
126 *pnum_keys = 0;
127 TALLOC_FREE(mem_ctx);
128 return NT_STATUS_OK;
131 if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
132 (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
133 (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
134 num_subkeys)))) {
135 status = NT_STATUS_NO_MEMORY;
136 goto error;
139 for (i=0; i<num_subkeys; i++) {
140 char c, n;
141 struct winreg_StringBuf class_buf;
142 struct winreg_StringBuf *pclass_buf = &class_buf;
143 struct winreg_StringBuf name_buf;
144 NTTIME modtime;
145 NTTIME *pmodtime = &modtime;
147 c = '\0';
148 class_buf.name = &c;
149 class_buf.size = max_classlen+2;
151 n = '\0';
152 name_buf.name = &n;
153 name_buf.size = max_subkeylen+2;
155 ZERO_STRUCT(modtime);
157 status = rpccli_winreg_EnumKey(pipe_hnd, mem_ctx, key_hnd,
158 i, &name_buf, &pclass_buf,
159 &pmodtime);
161 if (W_ERROR_EQUAL(ntstatus_to_werror(status),
162 WERR_NO_MORE_ITEMS) ) {
163 status = NT_STATUS_OK;
164 break;
166 if (!NT_STATUS_IS_OK(status)) {
167 goto error;
170 classes[i] = NULL;
172 if ((pclass_buf) &&
173 (!(classes[i] = talloc_strdup(classes,
174 pclass_buf->name)))) {
175 status = NT_STATUS_NO_MEMORY;
176 goto error;
179 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
180 status = NT_STATUS_NO_MEMORY;
181 goto error;
184 if ((pmodtime) &&
185 (!(modtimes[i] = (NTTIME *)talloc_memdup(
186 modtimes, pmodtime, sizeof(*pmodtime))))) {
187 status = NT_STATUS_NO_MEMORY;
188 goto error;
192 *pnum_keys = num_subkeys;
194 if (pnames) {
195 *pnames = talloc_move(ctx, &names);
197 if (pclasses) {
198 *pclasses = talloc_move(ctx, &classes);
200 if (pmodtimes) {
201 *pmodtimes = talloc_move(ctx, &modtimes);
204 status = NT_STATUS_OK;
206 error:
207 TALLOC_FREE(mem_ctx);
208 return status;
211 static NTSTATUS registry_pull_value(TALLOC_CTX *mem_ctx,
212 struct registry_value **pvalue,
213 enum winreg_Type type, uint8 *data,
214 uint32 size, uint32 length)
216 struct registry_value *value;
217 NTSTATUS status;
219 if (!(value = TALLOC_ZERO_P(mem_ctx, struct registry_value))) {
220 return NT_STATUS_NO_MEMORY;
223 value->type = type;
225 switch (type) {
226 case REG_DWORD:
227 if ((size != 4) || (length != 4)) {
228 status = NT_STATUS_INVALID_PARAMETER;
229 goto error;
231 value->v.dword = IVAL(data, 0);
232 break;
233 case REG_SZ:
234 case REG_EXPAND_SZ:
237 * Make sure we get a NULL terminated string for
238 * convert_string_talloc().
241 smb_ucs2_t *tmp;
242 uint32 num_ucs2 = length / 2;
244 if ((length % 2) != 0) {
245 status = NT_STATUS_INVALID_PARAMETER;
246 goto error;
249 if (!(tmp = SMB_MALLOC_ARRAY(smb_ucs2_t, num_ucs2+1))) {
250 status = NT_STATUS_NO_MEMORY;
251 goto error;
254 memcpy((void *)tmp, (const void *)data, length);
255 tmp[num_ucs2] = 0;
257 value->v.sz.len = convert_string_talloc(
258 value, CH_UTF16LE, CH_UNIX, tmp, length+2,
259 &value->v.sz.str, False);
261 SAFE_FREE(tmp);
263 if (value->v.sz.len == (size_t)-1) {
264 status = NT_STATUS_INVALID_PARAMETER;
265 goto error;
267 break;
269 case REG_MULTI_SZ:
270 status = reg_pull_multi_sz(value, (void *)data, length,
271 &value->v.multi_sz.num_strings,
272 &value->v.multi_sz.strings);
273 if (!(NT_STATUS_IS_OK(status))) {
274 goto error;
276 break;
277 case REG_BINARY:
278 value->v.binary.data = talloc_move(value, &data);
279 value->v.binary.length = length;
280 break;
281 default:
282 status = NT_STATUS_INVALID_PARAMETER;
283 goto error;
286 *pvalue = value;
287 return NT_STATUS_OK;
289 error:
290 TALLOC_FREE(value);
291 return status;
294 static NTSTATUS registry_push_value(TALLOC_CTX *mem_ctx,
295 const struct registry_value *value,
296 DATA_BLOB *presult)
298 switch (value->type) {
299 case REG_DWORD: {
300 char buf[4];
301 SIVAL(buf, 0, value->v.dword);
302 *presult = data_blob_talloc(mem_ctx, (void *)buf, 4);
303 if (presult->data == NULL) {
304 return NT_STATUS_NO_MEMORY;
306 break;
308 case REG_SZ:
309 case REG_EXPAND_SZ: {
310 presult->length = convert_string_talloc(
311 mem_ctx, CH_UNIX, CH_UTF16LE, value->v.sz.str,
312 MIN(value->v.sz.len, strlen(value->v.sz.str)+1),
313 (void *)&(presult->data), False);
314 if (presult->length == (size_t)-1) {
315 return NT_STATUS_NO_MEMORY;
317 break;
319 default:
320 return NT_STATUS_INVALID_PARAMETER;
323 return NT_STATUS_OK;
326 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
327 struct rpc_pipe_client *pipe_hnd,
328 struct policy_handle *key_hnd,
329 uint32 *pnum_values, char ***pvalnames,
330 struct registry_value ***pvalues)
332 TALLOC_CTX *mem_ctx;
333 NTSTATUS status;
334 uint32 num_subkeys, max_subkeylen, max_classlen;
335 uint32 num_values, max_valnamelen, max_valbufsize;
336 uint32 i;
337 NTTIME last_changed_time;
338 uint32 secdescsize;
339 struct winreg_String classname;
340 struct registry_value **values;
341 char **names;
343 if (!(mem_ctx = talloc_new(ctx))) {
344 return NT_STATUS_NO_MEMORY;
347 ZERO_STRUCT(classname);
348 status = rpccli_winreg_QueryInfoKey(
349 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
350 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
351 &max_valbufsize, &secdescsize, &last_changed_time );
353 if (!NT_STATUS_IS_OK(status)) {
354 goto error;
357 if (num_values == 0) {
358 *pnum_values = 0;
359 TALLOC_FREE(mem_ctx);
360 return NT_STATUS_OK;
363 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
364 (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
365 num_values)))) {
366 status = NT_STATUS_NO_MEMORY;
367 goto error;
370 for (i=0; i<num_values; i++) {
371 enum winreg_Type type = REG_NONE;
372 enum winreg_Type *ptype = &type;
373 uint8 d = 0;
374 uint8 *data = &d;
376 uint32 data_size;
377 uint32 *pdata_size = &data_size;
379 uint32 value_length;
380 uint32 *pvalue_length = &value_length;
382 char n;
383 struct winreg_StringBuf name_buf;
385 n = '\0';
386 name_buf.name = &n;
387 name_buf.size = max_valnamelen + 2;
389 data_size = max_valbufsize;
390 value_length = 0;
392 status = rpccli_winreg_EnumValue(pipe_hnd, mem_ctx, key_hnd,
393 i, &name_buf, &ptype,
394 &data, &pdata_size,
395 &pvalue_length );
397 if ( W_ERROR_EQUAL(ntstatus_to_werror(status),
398 WERR_NO_MORE_ITEMS) ) {
399 status = NT_STATUS_OK;
400 break;
403 if (!(NT_STATUS_IS_OK(status))) {
404 goto error;
407 if ((name_buf.name == NULL) || (ptype == NULL) ||
408 (data == NULL) || (pdata_size == 0) ||
409 (pvalue_length == NULL)) {
410 status = NT_STATUS_INVALID_PARAMETER;
411 goto error;
414 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
415 status = NT_STATUS_NO_MEMORY;
416 goto error;
419 status = registry_pull_value(values, &values[i], *ptype, data,
420 *pdata_size, *pvalue_length);
421 if (!(NT_STATUS_IS_OK(status))) {
422 goto error;
426 *pnum_values = num_values;
428 if (pvalnames) {
429 *pvalnames = talloc_move(ctx, &names);
431 if (pvalues) {
432 *pvalues = talloc_move(ctx, &values);
435 status = NT_STATUS_OK;
437 error:
438 TALLOC_FREE(mem_ctx);
439 return status;
442 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
443 struct rpc_pipe_client *pipe_hnd,
444 struct policy_handle *key_hnd,
445 const char *name,
446 const struct registry_value *value)
448 struct winreg_String name_string;
449 DATA_BLOB blob;
450 NTSTATUS result;
452 result = registry_push_value(mem_ctx, value, &blob);
453 if (!NT_STATUS_IS_OK(result)) {
454 return result;
457 name_string.name = name;
458 result = rpccli_winreg_SetValue(pipe_hnd, blob.data, key_hnd,
459 name_string, value->type,
460 blob.data, blob.length);
461 TALLOC_FREE(blob.data);
462 return result;
465 static NTSTATUS rpc_registry_setvalue_internal(const DOM_SID *domain_sid,
466 const char *domain_name,
467 struct cli_state *cli,
468 struct rpc_pipe_client *pipe_hnd,
469 TALLOC_CTX *mem_ctx,
470 int argc,
471 const char **argv )
473 struct policy_handle hive_hnd, key_hnd;
474 NTSTATUS status;
475 struct registry_value value;
477 if (argc < 4) {
478 d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
479 "<valuename> <type> [<val>]+\n");
480 return NT_STATUS_INVALID_PARAMETER;
483 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_WRITE,
484 &hive_hnd, &key_hnd);
485 if (!NT_STATUS_IS_OK(status)) {
486 d_fprintf(stderr, "registry_openkey failed: %s\n",
487 nt_errstr(status));
488 return status;
491 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
492 d_fprintf(stderr, "Too many args for type %s\n", argv[2]);
493 return NT_STATUS_NOT_IMPLEMENTED;
496 if (strequal(argv[2], "dword")) {
497 value.type = REG_DWORD;
498 value.v.dword = strtoul(argv[3], NULL, 10);
500 else if (strequal(argv[2], "sz")) {
501 value.type = REG_SZ;
502 value.v.sz.len = strlen(argv[3])+1;
503 value.v.sz.str = CONST_DISCARD(char *, argv[3]);
505 else {
506 d_fprintf(stderr, "type \"%s\" not implemented\n", argv[3]);
507 status = NT_STATUS_NOT_IMPLEMENTED;
508 goto error;
511 status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
512 argv[1], &value);
514 if (!NT_STATUS_IS_OK(status)) {
515 d_fprintf(stderr, "registry_setvalue failed: %s\n",
516 nt_errstr(status));
519 error:
520 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd);
521 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd);
523 return NT_STATUS_OK;
526 static int rpc_registry_setvalue( int argc, const char **argv )
528 return run_rpc_command( NULL, PI_WINREG, 0,
529 rpc_registry_setvalue_internal, argc, argv );
532 /********************************************************************
533 ********************************************************************/
535 static NTSTATUS rpc_registry_enumerate_internal(const DOM_SID *domain_sid,
536 const char *domain_name,
537 struct cli_state *cli,
538 struct rpc_pipe_client *pipe_hnd,
539 TALLOC_CTX *mem_ctx,
540 int argc,
541 const char **argv )
543 WERROR result = WERR_GENERAL_FAILURE;
544 uint32 hive;
545 pstring subpath;
546 POLICY_HND pol_hive, pol_key;
547 NTSTATUS status;
548 struct winreg_String subkeyname;
549 uint32 num_subkeys;
550 uint32 num_values;
551 char **names, **classes;
552 NTTIME **modtimes;
553 uint32 i;
554 struct registry_value **values;
556 if (argc != 1 ) {
557 d_printf("Usage: net rpc enumerate <path> [recurse]\n");
558 d_printf("Example: net rpc enumerate 'HKLM\\Software\\Samba'\n");
559 return NT_STATUS_OK;
562 if ( !reg_split_hive( argv[0], &hive, subpath ) ) {
563 d_fprintf(stderr, "invalid registry path\n");
564 return NT_STATUS_OK;
567 /* open the top level hive and then the registry key */
569 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, MAXIMUM_ALLOWED_ACCESS, &pol_hive );
570 if ( !NT_STATUS_IS_OK(status) ) {
571 d_fprintf(stderr, "Unable to connect to remote registry: "
572 "%s\n", nt_errstr(status));
573 return status;
576 subkeyname.name = subpath;
577 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, &pol_hive, subkeyname,
578 0, MAXIMUM_ALLOWED_ACCESS, &pol_key );
579 if ( !NT_STATUS_IS_OK(status) ) {
580 d_fprintf(stderr, "Unable to open [%s]: %s\n", argv[0],
581 nt_errstr(status));
582 return werror_to_ntstatus(result);
585 status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
586 &names, &classes, &modtimes);
587 if (!NT_STATUS_IS_OK(status)) {
588 d_fprintf(stderr, "enumerating keys failed: %s\n",
589 nt_errstr(status));
590 return status;
593 for (i=0; i<num_subkeys; i++) {
594 d_printf("Keyname = %s\n", names[i]);
595 d_printf("Modtime = %s\n", modtimes[i]
596 ? http_timestring(nt_time_to_unix(*modtimes[i]))
597 : "None");
598 d_printf("\n" );
601 status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
602 &names, &values);
603 if (!NT_STATUS_IS_OK(status)) {
604 d_fprintf(stderr, "enumerating values failed: %s\n",
605 nt_errstr(status));
606 return status;
609 for (i=0; i<num_values; i++) {
610 struct registry_value *v = values[i];
611 d_printf("Valuename = %s\n", names[i]);
612 d_printf("Type = %s\n",
613 reg_type_lookup(v->type));
614 switch(v->type) {
615 case REG_DWORD:
616 d_printf("Value = %d\n", v->v.dword);
617 break;
618 case REG_SZ:
619 case REG_EXPAND_SZ:
620 d_printf("Value = \"%s\"\n", v->v.sz.str);
621 break;
622 case REG_MULTI_SZ: {
623 uint32 j;
624 for (j = 0; j < v->v.multi_sz.num_strings; j++) {
625 d_printf("Value[%3.3d] = \"%s\"\n", j,
626 v->v.multi_sz.strings[j]);
628 break;
630 case REG_BINARY:
631 d_printf("Value = %d bytes\n",
632 v->v.binary.length);
633 break;
634 default:
635 d_printf("Value = <unprintable>\n");
636 break;
639 d_printf("\n");
642 if ( strlen( subpath ) != 0 )
643 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key );
644 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive );
646 return status;
649 /********************************************************************
650 ********************************************************************/
652 static int rpc_registry_enumerate( int argc, const char **argv )
654 return run_rpc_command( NULL, PI_WINREG, 0,
655 rpc_registry_enumerate_internal, argc, argv );
658 /********************************************************************
659 ********************************************************************/
661 static NTSTATUS rpc_registry_save_internal(const DOM_SID *domain_sid,
662 const char *domain_name,
663 struct cli_state *cli,
664 struct rpc_pipe_client *pipe_hnd,
665 TALLOC_CTX *mem_ctx,
666 int argc,
667 const char **argv )
669 WERROR result = WERR_GENERAL_FAILURE;
670 uint32 hive;
671 pstring subpath;
672 POLICY_HND pol_hive, pol_key;
673 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
674 struct winreg_String subkeyname;
675 struct winreg_String filename;
677 if (argc != 2 ) {
678 d_printf("Usage: net rpc backup <path> <file> \n");
679 return NT_STATUS_OK;
682 if ( !reg_split_hive( argv[0], &hive, subpath ) ) {
683 d_fprintf(stderr, "invalid registry path\n");
684 return NT_STATUS_OK;
687 /* open the top level hive and then the registry key */
689 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, MAXIMUM_ALLOWED_ACCESS, &pol_hive );
690 if ( !NT_STATUS_IS_OK(status) ) {
691 d_fprintf(stderr, "Unable to connect to remote registry\n");
692 return status;
695 subkeyname.name = subpath;
696 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, &pol_hive, subkeyname,
697 0, MAXIMUM_ALLOWED_ACCESS, &pol_key );
698 if ( !NT_STATUS_IS_OK(status) ) {
699 d_fprintf(stderr, "Unable to open [%s]\n", argv[0]);
700 return werror_to_ntstatus(result);
703 filename.name = argv[1];
704 status = rpccli_winreg_SaveKey( pipe_hnd, mem_ctx, &pol_key, &filename, NULL );
705 if ( !W_ERROR_IS_OK(result) ) {
706 d_fprintf(stderr, "Unable to save [%s] to %s:%s\n", argv[0], cli->desthost, argv[1]);
709 /* cleanup */
711 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key );
712 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive );
714 return status;
717 /********************************************************************
718 ********************************************************************/
720 static int rpc_registry_save( int argc, const char **argv )
722 return run_rpc_command( NULL, PI_WINREG, 0,
723 rpc_registry_save_internal, argc, argv );
727 /********************************************************************
728 ********************************************************************/
730 static void dump_values( REGF_NK_REC *nk )
732 int i, j;
733 pstring data_str;
734 uint32 data_size, data;
736 if ( !nk->values )
737 return;
739 for ( i=0; i<nk->num_values; i++ ) {
740 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
741 d_printf( "(%s) ", reg_type_lookup( nk->values[i].type ) );
743 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
744 switch ( nk->values[i].type ) {
745 case REG_SZ:
746 rpcstr_pull( data_str, nk->values[i].data, sizeof(data_str), -1, STR_TERMINATE );
747 d_printf( "%s", data_str );
748 break;
749 case REG_MULTI_SZ:
750 case REG_EXPAND_SZ:
751 for ( j=0; j<data_size; j++ ) {
752 d_printf( "%c", nk->values[i].data[j] );
754 break;
755 case REG_DWORD:
756 data = IVAL( nk->values[i].data, 0 );
757 d_printf("0x%x", data );
758 break;
759 case REG_BINARY:
760 for ( j=0; j<data_size; j++ ) {
761 d_printf( "%x", nk->values[i].data[j] );
763 break;
764 default:
765 d_printf("unknown");
766 break;
769 d_printf( "\n" );
774 /********************************************************************
775 ********************************************************************/
777 static BOOL dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
779 REGF_NK_REC *key;
780 pstring regpath;
782 /* depth first dump of the registry tree */
784 while ( (key = regfio_fetch_subkey( file, nk )) ) {
785 pstr_sprintf( regpath, "%s\\%s", parent, key->keyname );
786 d_printf("[%s]\n", regpath );
787 dump_values( key );
788 d_printf("\n");
789 dump_registry_tree( file, key, regpath );
792 return True;
795 /********************************************************************
796 ********************************************************************/
798 static BOOL write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
799 REGF_NK_REC *parent, REGF_FILE *outfile,
800 const char *parentpath )
802 REGF_NK_REC *key, *subkey;
803 REGVAL_CTR *values;
804 REGSUBKEY_CTR *subkeys;
805 int i;
806 pstring path;
808 if ( !( subkeys = TALLOC_ZERO_P( infile->mem_ctx, REGSUBKEY_CTR )) ) {
809 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
810 return False;
813 if ( !(values = TALLOC_ZERO_P( subkeys, REGVAL_CTR )) ) {
814 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
815 return False;
818 /* copy values into the REGVAL_CTR */
820 for ( i=0; i<nk->num_values; i++ ) {
821 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
822 (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
825 /* copy subkeys into the REGSUBKEY_CTR */
827 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
828 regsubkey_ctr_addkey( subkeys, subkey->keyname );
831 key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
833 /* write each one of the subkeys out */
835 pstr_sprintf( path, "%s%s%s", parentpath, parent ? "\\" : "", nk->keyname );
836 nk->subkey_index = 0;
837 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
838 write_registry_tree( infile, subkey, key, outfile, path );
841 TALLOC_FREE( subkeys );
843 d_printf("[%s]\n", path );
845 return True;
848 /********************************************************************
849 ********************************************************************/
851 static int rpc_registry_dump( int argc, const char **argv )
853 REGF_FILE *registry;
854 REGF_NK_REC *nk;
856 if (argc != 1 ) {
857 d_printf("Usage: net rpc dump <file> \n");
858 return 0;
861 d_printf("Opening %s....", argv[0]);
862 if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
863 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
864 return 1;
866 d_printf("ok\n");
868 /* get the root of the registry file */
870 if ((nk = regfio_rootkey( registry )) == NULL) {
871 d_fprintf(stderr, "Could not get rootkey\n");
872 regfio_close( registry );
873 return 1;
875 d_printf("[%s]\n", nk->keyname);
876 dump_values( nk );
877 d_printf("\n");
879 dump_registry_tree( registry, nk, nk->keyname );
881 #if 0
882 talloc_report_full( registry->mem_ctx, stderr );
883 #endif
884 d_printf("Closing registry...");
885 regfio_close( registry );
886 d_printf("ok\n");
888 return 0;
891 /********************************************************************
892 ********************************************************************/
894 static int rpc_registry_copy( int argc, const char **argv )
896 REGF_FILE *infile = NULL, *outfile = NULL;
897 REGF_NK_REC *nk;
898 int result = 1;
900 if (argc != 2 ) {
901 d_printf("Usage: net rpc copy <srcfile> <newfile>\n");
902 return 0;
905 d_printf("Opening %s....", argv[0]);
906 if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
907 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
908 return 1;
910 d_printf("ok\n");
912 d_printf("Opening %s....", argv[1]);
913 if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
914 d_fprintf(stderr, "Failed to open %s for writing\n", argv[1]);
915 goto out;
917 d_printf("ok\n");
919 /* get the root of the registry file */
921 if ((nk = regfio_rootkey( infile )) == NULL) {
922 d_fprintf(stderr, "Could not get rootkey\n");
923 goto out;
925 d_printf("RootKey: [%s]\n", nk->keyname);
927 write_registry_tree( infile, nk, NULL, outfile, "" );
929 result = 0;
931 out:
933 d_printf("Closing %s...", argv[1]);
934 if (outfile) {
935 regfio_close( outfile );
937 d_printf("ok\n");
939 d_printf("Closing %s...", argv[0]);
940 if (infile) {
941 regfio_close( infile );
943 d_printf("ok\n");
945 return( result);
948 /********************************************************************
949 ********************************************************************/
951 static int net_help_registry( int argc, const char **argv )
953 d_printf("net rpc registry enumerate <path> [recurse] Enumerate the subkeya and values for a given registry path\n");
954 d_printf("net rpc registry save <path> <file> Backup a registry tree to a file on the server\n");
955 d_printf("net rpc registry dump <file> Dump the contents of a registry file to stdout\n");
957 return -1;
960 /********************************************************************
961 ********************************************************************/
963 int net_rpc_registry(int argc, const char **argv)
965 struct functable func[] = {
966 {"enumerate", rpc_registry_enumerate},
967 {"setvalue", rpc_registry_setvalue},
968 {"save", rpc_registry_save},
969 {"dump", rpc_registry_dump},
970 {"copy", rpc_registry_copy},
971 {NULL, NULL}
974 if ( argc )
975 return net_run_function( argc, argv, func, net_help_registry );
977 return net_help_registry( argc, argv );