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 3 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, see <http://www.gnu.org/licenses/>. */
21 #include "utils/net.h"
22 #include "utils/net_registry_util.h"
24 #include "reg_objects.h"
26 static bool reg_hive_key(TALLOC_CTX
*ctx
, const char *fullname
,
27 uint32
*reg_type
, const char **key_name
)
30 char *hivename
= NULL
;
31 char *tmp_keyname
= NULL
;
33 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
35 werr
= split_hive_key(tmp_ctx
, fullname
, &hivename
, &tmp_keyname
);
36 if (!W_ERROR_IS_OK(werr
)) {
40 *key_name
= talloc_strdup(ctx
, tmp_keyname
);
41 if (*key_name
== NULL
) {
45 if (strequal(hivename
, "HKLM") ||
46 strequal(hivename
, "HKEY_LOCAL_MACHINE"))
48 (*reg_type
) = HKEY_LOCAL_MACHINE
;
49 } else if (strequal(hivename
, "HKCR") ||
50 strequal(hivename
, "HKEY_CLASSES_ROOT"))
52 (*reg_type
) = HKEY_CLASSES_ROOT
;
53 } else if (strequal(hivename
, "HKU") ||
54 strequal(hivename
, "HKEY_USERS"))
56 (*reg_type
) = HKEY_USERS
;
57 } else if (strequal(hivename
, "HKCU") ||
58 strequal(hivename
, "HKEY_CURRENT_USER"))
60 (*reg_type
) = HKEY_CURRENT_USER
;
61 } else if (strequal(hivename
, "HKPD") ||
62 strequal(hivename
, "HKEY_PERFORMANCE_DATA"))
64 (*reg_type
) = HKEY_PERFORMANCE_DATA
;
66 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
78 static NTSTATUS
registry_openkey(TALLOC_CTX
*mem_ctx
,
79 struct rpc_pipe_client
*pipe_hnd
,
80 const char *name
, uint32 access_mask
,
81 struct policy_handle
*hive_hnd
,
82 struct policy_handle
*key_hnd
)
86 struct winreg_String key
;
90 if (!reg_hive_key(mem_ctx
, name
, &hive
, &key
.name
)) {
91 return NT_STATUS_INVALID_PARAMETER
;
94 status
= rpccli_winreg_Connect(pipe_hnd
, mem_ctx
, hive
, access_mask
,
96 if (!(NT_STATUS_IS_OK(status
))) {
100 status
= rpccli_winreg_OpenKey(pipe_hnd
, mem_ctx
, hive_hnd
, key
, 0,
101 access_mask
, key_hnd
, NULL
);
102 if (!(NT_STATUS_IS_OK(status
))) {
103 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, hive_hnd
, NULL
);
110 static NTSTATUS
registry_enumkeys(TALLOC_CTX
*ctx
,
111 struct rpc_pipe_client
*pipe_hnd
,
112 struct policy_handle
*key_hnd
,
113 uint32
*pnum_keys
, char ***pnames
,
114 char ***pclasses
, NTTIME
***pmodtimes
)
118 uint32 num_subkeys
, max_subkeylen
, max_classlen
;
119 uint32 num_values
, max_valnamelen
, max_valbufsize
;
121 NTTIME last_changed_time
;
123 struct winreg_String classname
;
124 char **names
, **classes
;
127 if (!(mem_ctx
= talloc_new(ctx
))) {
128 return NT_STATUS_NO_MEMORY
;
131 ZERO_STRUCT(classname
);
132 status
= rpccli_winreg_QueryInfoKey(
133 pipe_hnd
, mem_ctx
, key_hnd
, &classname
, &num_subkeys
,
134 &max_subkeylen
, &max_classlen
, &num_values
, &max_valnamelen
,
135 &max_valbufsize
, &secdescsize
, &last_changed_time
, NULL
);
137 if (!NT_STATUS_IS_OK(status
)) {
141 if (num_subkeys
== 0) {
143 TALLOC_FREE(mem_ctx
);
147 if ((!(names
= TALLOC_ZERO_ARRAY(mem_ctx
, char *, num_subkeys
))) ||
148 (!(classes
= TALLOC_ZERO_ARRAY(mem_ctx
, char *, num_subkeys
))) ||
149 (!(modtimes
= TALLOC_ZERO_ARRAY(mem_ctx
, NTTIME
*,
151 status
= NT_STATUS_NO_MEMORY
;
155 for (i
=0; i
<num_subkeys
; i
++) {
157 struct winreg_StringBuf class_buf
;
158 struct winreg_StringBuf name_buf
;
164 class_buf
.size
= max_classlen
+2;
168 name_buf
.size
= max_subkeylen
+2;
170 ZERO_STRUCT(modtime
);
172 status
= rpccli_winreg_EnumKey(pipe_hnd
, mem_ctx
, key_hnd
,
173 i
, &name_buf
, &class_buf
,
176 if (W_ERROR_EQUAL(werr
,
177 WERR_NO_MORE_ITEMS
) ) {
178 status
= NT_STATUS_OK
;
181 if (!NT_STATUS_IS_OK(status
)) {
187 if (class_buf
.name
&&
188 (!(classes
[i
] = talloc_strdup(classes
, class_buf
.name
)))) {
189 status
= NT_STATUS_NO_MEMORY
;
193 if (!(names
[i
] = talloc_strdup(names
, name_buf
.name
))) {
194 status
= NT_STATUS_NO_MEMORY
;
198 if ((!(modtimes
[i
] = (NTTIME
*)talloc_memdup(
199 modtimes
, &modtime
, sizeof(modtime
))))) {
200 status
= NT_STATUS_NO_MEMORY
;
205 *pnum_keys
= num_subkeys
;
208 *pnames
= talloc_move(ctx
, &names
);
211 *pclasses
= talloc_move(ctx
, &classes
);
214 *pmodtimes
= talloc_move(ctx
, &modtimes
);
217 status
= NT_STATUS_OK
;
220 TALLOC_FREE(mem_ctx
);
224 static NTSTATUS
registry_enumvalues(TALLOC_CTX
*ctx
,
225 struct rpc_pipe_client
*pipe_hnd
,
226 struct policy_handle
*key_hnd
,
227 uint32
*pnum_values
, char ***pvalnames
,
228 struct registry_value
***pvalues
)
232 uint32 num_subkeys
, max_subkeylen
, max_classlen
;
233 uint32 num_values
, max_valnamelen
, max_valbufsize
;
235 NTTIME last_changed_time
;
237 struct winreg_String classname
;
238 struct registry_value
**values
;
241 if (!(mem_ctx
= talloc_new(ctx
))) {
242 return NT_STATUS_NO_MEMORY
;
245 ZERO_STRUCT(classname
);
246 status
= rpccli_winreg_QueryInfoKey(
247 pipe_hnd
, mem_ctx
, key_hnd
, &classname
, &num_subkeys
,
248 &max_subkeylen
, &max_classlen
, &num_values
, &max_valnamelen
,
249 &max_valbufsize
, &secdescsize
, &last_changed_time
, NULL
);
251 if (!NT_STATUS_IS_OK(status
)) {
255 if (num_values
== 0) {
257 TALLOC_FREE(mem_ctx
);
261 if ((!(names
= TALLOC_ARRAY(mem_ctx
, char *, num_values
))) ||
262 (!(values
= TALLOC_ARRAY(mem_ctx
, struct registry_value
*,
264 status
= NT_STATUS_NO_MEMORY
;
268 for (i
=0; i
<num_values
; i
++) {
269 enum winreg_Type type
= REG_NONE
;
275 struct winreg_ValNameBuf name_buf
;
280 name_buf
.size
= max_valnamelen
+ 2;
282 data_size
= max_valbufsize
;
283 data
= (uint8
*)TALLOC(mem_ctx
, data_size
);
286 status
= rpccli_winreg_EnumValue(pipe_hnd
, mem_ctx
, key_hnd
,
289 &value_length
, &err
);
291 if ( W_ERROR_EQUAL(err
,
292 WERR_NO_MORE_ITEMS
) ) {
293 status
= NT_STATUS_OK
;
297 if (!(NT_STATUS_IS_OK(status
))) {
301 if (name_buf
.name
== NULL
) {
302 status
= NT_STATUS_INVALID_PARAMETER
;
306 if (!(names
[i
] = talloc_strdup(names
, name_buf
.name
))) {
307 status
= NT_STATUS_NO_MEMORY
;
311 err
= registry_pull_value(values
, &values
[i
], type
, data
,
312 data_size
, value_length
);
313 if (!W_ERROR_IS_OK(err
)) {
314 status
= werror_to_ntstatus(err
);
319 *pnum_values
= num_values
;
322 *pvalnames
= talloc_move(ctx
, &names
);
325 *pvalues
= talloc_move(ctx
, &values
);
328 status
= NT_STATUS_OK
;
331 TALLOC_FREE(mem_ctx
);
335 static NTSTATUS
registry_getsd(TALLOC_CTX
*mem_ctx
,
336 struct rpc_pipe_client
*pipe_hnd
,
337 struct policy_handle
*key_hnd
,
339 struct KeySecurityData
*sd
)
341 return rpccli_winreg_GetKeySecurity(pipe_hnd
, mem_ctx
, key_hnd
,
346 static NTSTATUS
registry_setvalue(TALLOC_CTX
*mem_ctx
,
347 struct rpc_pipe_client
*pipe_hnd
,
348 struct policy_handle
*key_hnd
,
350 const struct registry_value
*value
)
352 struct winreg_String name_string
;
357 err
= registry_push_value(mem_ctx
, value
, &blob
);
358 if (!W_ERROR_IS_OK(err
)) {
359 return werror_to_ntstatus(err
);
362 ZERO_STRUCT(name_string
);
364 name_string
.name
= name
;
365 result
= rpccli_winreg_SetValue(pipe_hnd
, blob
.data
, key_hnd
,
366 name_string
, value
->type
,
367 blob
.data
, blob
.length
, NULL
);
368 TALLOC_FREE(blob
.data
);
372 static NTSTATUS
rpc_registry_setvalue_internal(const DOM_SID
*domain_sid
,
373 const char *domain_name
,
374 struct cli_state
*cli
,
375 struct rpc_pipe_client
*pipe_hnd
,
380 struct policy_handle hive_hnd
, key_hnd
;
382 struct registry_value value
;
384 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
385 SEC_RIGHTS_MAXIMUM_ALLOWED
,
386 &hive_hnd
, &key_hnd
);
387 if (!NT_STATUS_IS_OK(status
)) {
388 d_fprintf(stderr
, "registry_openkey failed: %s\n",
393 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
394 d_fprintf(stderr
, "Too many args for type %s\n", argv
[2]);
395 return NT_STATUS_NOT_IMPLEMENTED
;
398 if (strequal(argv
[2], "dword")) {
399 value
.type
= REG_DWORD
;
400 value
.v
.dword
= strtoul(argv
[3], NULL
, 10);
402 else if (strequal(argv
[2], "sz")) {
404 value
.v
.sz
.len
= strlen(argv
[3])+1;
405 value
.v
.sz
.str
= CONST_DISCARD(char *, argv
[3]);
408 d_fprintf(stderr
, "type \"%s\" not implemented\n", argv
[2]);
409 status
= NT_STATUS_NOT_IMPLEMENTED
;
413 status
= registry_setvalue(mem_ctx
, pipe_hnd
, &key_hnd
,
416 if (!NT_STATUS_IS_OK(status
)) {
417 d_fprintf(stderr
, "registry_setvalue failed: %s\n",
422 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &key_hnd
, NULL
);
423 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &hive_hnd
, NULL
);
428 static int rpc_registry_setvalue( int argc
, const char **argv
)
431 d_fprintf(stderr
, "usage: net rpc registry setvalue <key> "
432 "<valuename> <type> [<val>]+\n");
436 return run_rpc_command( NULL
, PI_WINREG
, 0,
437 rpc_registry_setvalue_internal
, argc
, argv
);
440 static NTSTATUS
rpc_registry_deletevalue_internal(const DOM_SID
*domain_sid
,
441 const char *domain_name
,
442 struct cli_state
*cli
,
443 struct rpc_pipe_client
*pipe_hnd
,
448 struct policy_handle hive_hnd
, key_hnd
;
450 struct winreg_String valuename
;
452 ZERO_STRUCT(valuename
);
454 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
455 SEC_RIGHTS_MAXIMUM_ALLOWED
,
456 &hive_hnd
, &key_hnd
);
457 if (!NT_STATUS_IS_OK(status
)) {
458 d_fprintf(stderr
, "registry_openkey failed: %s\n",
463 valuename
.name
= argv
[1];
465 status
= rpccli_winreg_DeleteValue(pipe_hnd
, mem_ctx
, &key_hnd
,
468 if (!NT_STATUS_IS_OK(status
)) {
469 d_fprintf(stderr
, "registry_deletevalue failed: %s\n",
473 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &key_hnd
, NULL
);
474 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &hive_hnd
, NULL
);
479 static int rpc_registry_deletevalue( int argc
, const char **argv
)
482 d_fprintf(stderr
, "usage: net rpc registry deletevalue <key> "
487 return run_rpc_command( NULL
, PI_WINREG
, 0,
488 rpc_registry_deletevalue_internal
, argc
, argv
);
491 static NTSTATUS
rpc_registry_getvalue_internal(const DOM_SID
*domain_sid
,
492 const char *domain_name
,
493 struct cli_state
*cli
,
494 struct rpc_pipe_client
*pipe_hnd
,
499 struct policy_handle hive_hnd
, key_hnd
;
502 struct winreg_String valuename
;
503 struct registry_value
*value
= NULL
;
504 enum winreg_Type type
= REG_NONE
;
505 uint8_t *data
= NULL
;
506 uint32_t data_size
= 0;
507 uint32_t value_length
= 0;
508 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
510 ZERO_STRUCT(valuename
);
512 status
= registry_openkey(tmp_ctx
, pipe_hnd
, argv
[0],
513 SEC_RIGHTS_MAXIMUM_ALLOWED
,
514 &hive_hnd
, &key_hnd
);
515 if (!NT_STATUS_IS_OK(status
)) {
516 d_fprintf(stderr
, "registry_openkey failed: %s\n",
521 valuename
.name
= argv
[1];
524 * call QueryValue once with data == NULL to get the
525 * needed memory size to be allocated, then allocate
526 * data buffer and call again.
528 status
= rpccli_winreg_QueryValue(pipe_hnd
, tmp_ctx
, &key_hnd
,
536 if (!NT_STATUS_IS_OK(status
)) {
537 d_fprintf(stderr
, "registry_queryvalue failed: %s\n",
542 data
= (uint8
*)TALLOC(tmp_ctx
, data_size
);
545 status
= rpccli_winreg_QueryValue(pipe_hnd
, tmp_ctx
, &key_hnd
,
553 if (!NT_STATUS_IS_OK(status
)) {
554 d_fprintf(stderr
, "registry_queryvalue failed: %s\n",
559 werr
= registry_pull_value(tmp_ctx
, &value
, type
, data
,
560 data_size
, value_length
);
561 if (!W_ERROR_IS_OK(werr
)) {
562 status
= werror_to_ntstatus(werr
);
566 print_registry_value(value
);
569 rpccli_winreg_CloseKey(pipe_hnd
, tmp_ctx
, &key_hnd
, NULL
);
570 rpccli_winreg_CloseKey(pipe_hnd
, tmp_ctx
, &hive_hnd
, NULL
);
572 TALLOC_FREE(tmp_ctx
);
577 static int rpc_registry_getvalue(int argc
, const char **argv
)
580 d_fprintf(stderr
, "usage: net rpc registry deletevalue <key> "
585 return run_rpc_command(NULL
, PI_WINREG
, 0,
586 rpc_registry_getvalue_internal
, argc
, argv
);
589 static NTSTATUS
rpc_registry_createkey_internal(const DOM_SID
*domain_sid
,
590 const char *domain_name
,
591 struct cli_state
*cli
,
592 struct rpc_pipe_client
*pipe_hnd
,
598 struct policy_handle hive_hnd
, key_hnd
;
599 struct winreg_String key
, keyclass
;
600 enum winreg_CreateAction action
;
604 ZERO_STRUCT(keyclass
);
606 if (!reg_hive_key(mem_ctx
, argv
[0], &hive
, &key
.name
)) {
607 return NT_STATUS_INVALID_PARAMETER
;
610 status
= rpccli_winreg_Connect(pipe_hnd
, mem_ctx
, hive
,
611 SEC_RIGHTS_MAXIMUM_ALLOWED
,
613 if (!(NT_STATUS_IS_OK(status
))) {
617 action
= REG_ACTION_NONE
;
620 status
= rpccli_winreg_CreateKey(pipe_hnd
, mem_ctx
, &hive_hnd
, key
,
621 keyclass
, 0, REG_KEY_READ
, NULL
,
622 &key_hnd
, &action
, NULL
);
623 if (!NT_STATUS_IS_OK(status
)) {
624 d_fprintf(stderr
, "createkey returned %s\n",
626 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &hive_hnd
, NULL
);
631 case REG_ACTION_NONE
:
632 d_printf("createkey did nothing -- huh?\n");
634 case REG_CREATED_NEW_KEY
:
635 d_printf("createkey created %s\n", argv
[0]);
637 case REG_OPENED_EXISTING_KEY
:
638 d_printf("createkey opened existing %s\n", argv
[0]);
642 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &key_hnd
, NULL
);
643 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &hive_hnd
, NULL
);
648 static int rpc_registry_createkey( int argc
, const char **argv
)
651 d_fprintf(stderr
, "usage: net rpc registry createkey <key>\n");
655 return run_rpc_command( NULL
, PI_WINREG
, 0,
656 rpc_registry_createkey_internal
, argc
, argv
);
659 static NTSTATUS
rpc_registry_deletekey_internal(const DOM_SID
*domain_sid
,
660 const char *domain_name
,
661 struct cli_state
*cli
,
662 struct rpc_pipe_client
*pipe_hnd
,
668 struct policy_handle hive_hnd
;
669 struct winreg_String key
;
674 if (!reg_hive_key(mem_ctx
, argv
[0], &hive
, &key
.name
)) {
675 return NT_STATUS_INVALID_PARAMETER
;
678 status
= rpccli_winreg_Connect(pipe_hnd
, mem_ctx
, hive
,
679 SEC_RIGHTS_MAXIMUM_ALLOWED
,
681 if (!(NT_STATUS_IS_OK(status
))) {
685 status
= rpccli_winreg_DeleteKey(pipe_hnd
, mem_ctx
, &hive_hnd
, key
, NULL
);
686 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &hive_hnd
, NULL
);
688 if (!NT_STATUS_IS_OK(status
)) {
689 d_fprintf(stderr
, "deletekey returned %s\n",
696 static int rpc_registry_deletekey( int argc
, const char **argv
)
699 d_fprintf(stderr
, "usage: net rpc registry deletekey <key>\n");
703 return run_rpc_command( NULL
, PI_WINREG
, 0,
704 rpc_registry_deletekey_internal
, argc
, argv
);
707 /********************************************************************
708 ********************************************************************/
710 static NTSTATUS
rpc_registry_enumerate_internal(const DOM_SID
*domain_sid
,
711 const char *domain_name
,
712 struct cli_state
*cli
,
713 struct rpc_pipe_client
*pipe_hnd
,
718 POLICY_HND pol_hive
, pol_key
;
720 uint32 num_subkeys
= 0;
721 uint32 num_values
= 0;
722 char **names
= NULL
, **classes
= NULL
;
723 NTTIME
**modtimes
= NULL
;
725 struct registry_value
**values
= NULL
;
728 d_printf("Usage: net rpc registry enumerate <path> [recurse]\n");
729 d_printf("Example: net rpc registry enumerate 'HKLM\\Software\\Samba'\n");
730 return NT_STATUS_INVALID_PARAMETER
;
733 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0], REG_KEY_READ
,
734 &pol_hive
, &pol_key
);
735 if (!NT_STATUS_IS_OK(status
)) {
736 d_fprintf(stderr
, "registry_openkey failed: %s\n",
741 status
= registry_enumkeys(mem_ctx
, pipe_hnd
, &pol_key
, &num_subkeys
,
742 &names
, &classes
, &modtimes
);
743 if (!NT_STATUS_IS_OK(status
)) {
744 d_fprintf(stderr
, "enumerating keys failed: %s\n",
749 for (i
=0; i
<num_subkeys
; i
++) {
750 print_registry_key(names
[i
], modtimes
[i
]);
753 status
= registry_enumvalues(mem_ctx
, pipe_hnd
, &pol_key
, &num_values
,
755 if (!NT_STATUS_IS_OK(status
)) {
756 d_fprintf(stderr
, "enumerating values failed: %s\n",
761 for (i
=0; i
<num_values
; i
++) {
762 print_registry_value_with_name(names
[i
], values
[i
]);
765 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_key
, NULL
);
766 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_hive
, NULL
);
771 /********************************************************************
772 ********************************************************************/
774 static int rpc_registry_enumerate( int argc
, const char **argv
)
776 return run_rpc_command( NULL
, PI_WINREG
, 0,
777 rpc_registry_enumerate_internal
, argc
, argv
);
780 /********************************************************************
781 ********************************************************************/
783 static NTSTATUS
rpc_registry_save_internal(const DOM_SID
*domain_sid
,
784 const char *domain_name
,
785 struct cli_state
*cli
,
786 struct rpc_pipe_client
*pipe_hnd
,
791 WERROR result
= WERR_GENERAL_FAILURE
;
792 POLICY_HND pol_hive
, pol_key
;
793 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
794 struct winreg_String filename
;
797 d_printf("Usage: net rpc registry backup <path> <file> \n");
798 return NT_STATUS_INVALID_PARAMETER
;
801 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0], REG_KEY_ALL
,
802 &pol_hive
, &pol_key
);
803 if (!NT_STATUS_IS_OK(status
)) {
804 d_fprintf(stderr
, "registry_openkey failed: %s\n",
809 filename
.name
= argv
[1];
810 status
= rpccli_winreg_SaveKey( pipe_hnd
, mem_ctx
, &pol_key
, &filename
, NULL
, NULL
);
811 if ( !W_ERROR_IS_OK(result
) ) {
812 d_fprintf(stderr
, "Unable to save [%s] to %s:%s\n", argv
[0], cli
->desthost
, argv
[1]);
817 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_key
, NULL
);
818 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_hive
, NULL
);
823 /********************************************************************
824 ********************************************************************/
826 static int rpc_registry_save( int argc
, const char **argv
)
828 return run_rpc_command( NULL
, PI_WINREG
, 0,
829 rpc_registry_save_internal
, argc
, argv
);
833 /********************************************************************
834 ********************************************************************/
836 static void dump_values( REGF_NK_REC
*nk
)
839 char *data_str
= NULL
;
840 uint32 data_size
, data
;
845 for ( i
=0; i
<nk
->num_values
; i
++ ) {
846 d_printf( "\"%s\" = ", nk
->values
[i
].valuename
? nk
->values
[i
].valuename
: "(default)" );
847 d_printf( "(%s) ", reg_type_lookup( nk
->values
[i
].type
) );
849 data_size
= nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
;
850 switch ( nk
->values
[i
].type
) {
852 rpcstr_pull_talloc(talloc_tos(),
860 d_printf( "%s", data_str
);
864 for ( j
=0; j
<data_size
; j
++ ) {
865 d_printf( "%c", nk
->values
[i
].data
[j
] );
869 data
= IVAL( nk
->values
[i
].data
, 0 );
870 d_printf("0x%x", data
);
873 for ( j
=0; j
<data_size
; j
++ ) {
874 d_printf( "%x", nk
->values
[i
].data
[j
] );
887 /********************************************************************
888 ********************************************************************/
890 static bool dump_registry_tree( REGF_FILE
*file
, REGF_NK_REC
*nk
, const char *parent
)
894 /* depth first dump of the registry tree */
896 while ( (key
= regfio_fetch_subkey( file
, nk
)) ) {
898 if (asprintf(®path
, "%s\\%s", parent
, key
->keyname
) < 0) {
901 d_printf("[%s]\n", regpath
);
904 dump_registry_tree( file
, key
, regpath
);
911 /********************************************************************
912 ********************************************************************/
914 static bool write_registry_tree( REGF_FILE
*infile
, REGF_NK_REC
*nk
,
915 REGF_NK_REC
*parent
, REGF_FILE
*outfile
,
916 const char *parentpath
)
918 REGF_NK_REC
*key
, *subkey
;
919 REGVAL_CTR
*values
= NULL
;
920 REGSUBKEY_CTR
*subkeys
= NULL
;
924 if ( !( subkeys
= TALLOC_ZERO_P( infile
->mem_ctx
, REGSUBKEY_CTR
)) ) {
925 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
929 if ( !(values
= TALLOC_ZERO_P( subkeys
, REGVAL_CTR
)) ) {
930 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
931 TALLOC_FREE(subkeys
);
935 /* copy values into the REGVAL_CTR */
937 for ( i
=0; i
<nk
->num_values
; i
++ ) {
938 regval_ctr_addvalue( values
, nk
->values
[i
].valuename
, nk
->values
[i
].type
,
939 (const char *)nk
->values
[i
].data
, (nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
) );
942 /* copy subkeys into the REGSUBKEY_CTR */
944 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
945 regsubkey_ctr_addkey( subkeys
, subkey
->keyname
);
948 key
= regfio_write_key( outfile
, nk
->keyname
, values
, subkeys
, nk
->sec_desc
->sec_desc
, parent
);
950 /* write each one of the subkeys out */
952 path
= talloc_asprintf(subkeys
,
958 TALLOC_FREE(subkeys
);
962 nk
->subkey_index
= 0;
963 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
964 write_registry_tree( infile
, subkey
, key
, outfile
, path
);
967 d_printf("[%s]\n", path
);
968 TALLOC_FREE(subkeys
);
973 /********************************************************************
974 ********************************************************************/
976 static int rpc_registry_dump( int argc
, const char **argv
)
982 d_printf("Usage: net rpc registry dump <file> \n");
986 d_printf("Opening %s....", argv
[0]);
987 if ( !(registry
= regfio_open( argv
[0], O_RDONLY
, 0)) ) {
988 d_fprintf(stderr
, "Failed to open %s for reading\n", argv
[0]);
993 /* get the root of the registry file */
995 if ((nk
= regfio_rootkey( registry
)) == NULL
) {
996 d_fprintf(stderr
, "Could not get rootkey\n");
997 regfio_close( registry
);
1000 d_printf("[%s]\n", nk
->keyname
);
1004 dump_registry_tree( registry
, nk
, nk
->keyname
);
1007 talloc_report_full( registry
->mem_ctx
, stderr
);
1009 d_printf("Closing registry...");
1010 regfio_close( registry
);
1016 /********************************************************************
1017 ********************************************************************/
1019 static int rpc_registry_copy( int argc
, const char **argv
)
1021 REGF_FILE
*infile
= NULL
, *outfile
= NULL
;
1026 d_printf("Usage: net rpc registry copy <srcfile> <newfile>\n");
1030 d_printf("Opening %s....", argv
[0]);
1031 if ( !(infile
= regfio_open( argv
[0], O_RDONLY
, 0 )) ) {
1032 d_fprintf(stderr
, "Failed to open %s for reading\n", argv
[0]);
1037 d_printf("Opening %s....", argv
[1]);
1038 if ( !(outfile
= regfio_open( argv
[1], (O_RDWR
|O_CREAT
|O_TRUNC
), (S_IREAD
|S_IWRITE
) )) ) {
1039 d_fprintf(stderr
, "Failed to open %s for writing\n", argv
[1]);
1044 /* get the root of the registry file */
1046 if ((nk
= regfio_rootkey( infile
)) == NULL
) {
1047 d_fprintf(stderr
, "Could not get rootkey\n");
1050 d_printf("RootKey: [%s]\n", nk
->keyname
);
1052 write_registry_tree( infile
, nk
, NULL
, outfile
, "" );
1058 d_printf("Closing %s...", argv
[1]);
1060 regfio_close( outfile
);
1064 d_printf("Closing %s...", argv
[0]);
1066 regfio_close( infile
);
1073 /********************************************************************
1074 ********************************************************************/
1076 static NTSTATUS
rpc_registry_getsd_internal(const DOM_SID
*domain_sid
,
1077 const char *domain_name
,
1078 struct cli_state
*cli
,
1079 struct rpc_pipe_client
*pipe_hnd
,
1080 TALLOC_CTX
*mem_ctx
,
1084 POLICY_HND pol_hive
, pol_key
;
1086 enum ndr_err_code ndr_err
;
1087 struct KeySecurityData
*sd
= NULL
;
1090 struct security_descriptor sec_desc
;
1091 uint32_t access_mask
= REG_KEY_READ
|
1092 SEC_RIGHT_MAXIMUM_ALLOWED
|
1093 SEC_RIGHT_SYSTEM_SECURITY
;
1095 if (argc
<1 || argc
> 2) {
1096 d_printf("Usage: net rpc registry getsd <path> <secinfo>\n");
1097 d_printf("Example: net rpc registry getsd 'HKLM\\Software\\Samba'\n");
1098 return NT_STATUS_INVALID_PARAMETER
;
1101 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
1103 &pol_hive
, &pol_key
);
1104 if (!NT_STATUS_IS_OK(status
)) {
1105 d_fprintf(stderr
, "registry_openkey failed: %s\n",
1110 sd
= TALLOC_ZERO_P(mem_ctx
, struct KeySecurityData
);
1112 status
= NT_STATUS_NO_MEMORY
;
1119 sscanf(argv
[1], "%x", &sec_info
);
1121 sec_info
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
;
1124 status
= registry_getsd(mem_ctx
, pipe_hnd
, &pol_key
, sec_info
, sd
);
1125 if (!NT_STATUS_IS_OK(status
)) {
1126 d_fprintf(stderr
, "getting sd failed: %s\n",
1131 blob
.data
= sd
->data
;
1132 blob
.length
= sd
->size
;
1134 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &sec_desc
,
1135 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
1136 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1137 status
= ndr_map_error2ntstatus(ndr_err
);
1140 status
= NT_STATUS_OK
;
1142 display_sec_desc(&sec_desc
);
1145 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_key
, NULL
);
1146 rpccli_winreg_CloseKey(pipe_hnd
, mem_ctx
, &pol_hive
, NULL
);
1152 static int rpc_registry_getsd(int argc
, const char **argv
)
1154 return run_rpc_command(NULL
, PI_WINREG
, 0,
1155 rpc_registry_getsd_internal
, argc
, argv
);
1158 /********************************************************************
1159 ********************************************************************/
1161 int net_rpc_registry(int argc
, const char **argv
)
1163 struct functable2 func
[] = {
1164 { "enumerate", rpc_registry_enumerate
,
1165 "Enumerate registry keys and values" },
1166 { "createkey", rpc_registry_createkey
,
1167 "Create a new registry key" },
1168 { "deletekey", rpc_registry_deletekey
,
1169 "Delete a registry key" },
1170 { "getvalue", rpc_registry_getvalue
,
1171 "Print a registry value" },
1172 { "setvalue", rpc_registry_setvalue
,
1173 "Set a new registry value" },
1174 { "deletevalue", rpc_registry_deletevalue
,
1175 "Delete a registry value" },
1176 { "save", rpc_registry_save
,
1177 "Save a registry file" },
1178 { "dump", rpc_registry_dump
,
1179 "Dump a registry file" },
1180 { "copy", rpc_registry_copy
,
1181 "Copy a registry file" },
1182 { "getsd", rpc_registry_getsd
,
1183 "Get security descriptor" },
1187 return net_run_function2(argc
, argv
, "net rpc registry", func
);