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 "system/filesys.h"
22 #include "rpc_client/rpc_client.h"
24 #include "utils/net.h"
25 #include "utils/net_registry_util.h"
26 #include "registry/regfio.h"
27 #include "../librpc/gen_ndr/ndr_winreg_c.h"
28 #include "../librpc/gen_ndr/ndr_security.h"
29 #include "registry/reg_format.h"
30 #include "registry/reg_import.h"
32 #include "../libcli/security/display_sec.h"
33 #include "../libcli/registry/util_reg.h"
35 #include "lib/util/smb_strtox.h"
38 /*******************************************************************
39 connect to a registry hive root (open a registry policy)
40 *******************************************************************/
42 static NTSTATUS
dcerpc_winreg_Connect(struct dcerpc_binding_handle
*b
, TALLOC_CTX
*mem_ctx
,
43 uint32_t reg_type
, uint32_t access_mask
,
44 struct policy_handle
*reg_hnd
, WERROR
*werr
)
46 ZERO_STRUCTP(reg_hnd
);
50 case HKEY_CLASSES_ROOT
:
51 return dcerpc_winreg_OpenHKCR(b
, mem_ctx
, NULL
,
52 access_mask
, reg_hnd
, werr
);
54 case HKEY_LOCAL_MACHINE
:
55 return dcerpc_winreg_OpenHKLM(b
, mem_ctx
, NULL
,
56 access_mask
, reg_hnd
, werr
);
59 return dcerpc_winreg_OpenHKU(b
, mem_ctx
, NULL
,
60 access_mask
, reg_hnd
, werr
);
62 case HKEY_CURRENT_USER
:
63 return dcerpc_winreg_OpenHKCU(b
, mem_ctx
, NULL
,
64 access_mask
, reg_hnd
, werr
);
66 case HKEY_PERFORMANCE_DATA
:
67 return dcerpc_winreg_OpenHKPD(b
, mem_ctx
, NULL
,
68 access_mask
, reg_hnd
, werr
);
71 /* fall through to end of function */
75 return NT_STATUS_INVALID_PARAMETER
;
78 static bool reg_hive_key(TALLOC_CTX
*ctx
, const char *fullname
,
79 uint32_t *reg_type
, const char **key_name
)
82 char *hivename
= NULL
;
83 char *tmp_keyname
= NULL
;
85 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
87 werr
= split_hive_key(tmp_ctx
, fullname
, &hivename
, &tmp_keyname
);
88 if (!W_ERROR_IS_OK(werr
)) {
92 *key_name
= talloc_strdup(ctx
, tmp_keyname
);
93 if (*key_name
== NULL
) {
97 if (strequal(hivename
, "HKLM") ||
98 strequal(hivename
, "HKEY_LOCAL_MACHINE"))
100 (*reg_type
) = HKEY_LOCAL_MACHINE
;
101 } else if (strequal(hivename
, "HKCR") ||
102 strequal(hivename
, "HKEY_CLASSES_ROOT"))
104 (*reg_type
) = HKEY_CLASSES_ROOT
;
105 } else if (strequal(hivename
, "HKU") ||
106 strequal(hivename
, "HKEY_USERS"))
108 (*reg_type
) = HKEY_USERS
;
109 } else if (strequal(hivename
, "HKCU") ||
110 strequal(hivename
, "HKEY_CURRENT_USER"))
112 (*reg_type
) = HKEY_CURRENT_USER
;
113 } else if (strequal(hivename
, "HKPD") ||
114 strequal(hivename
, "HKEY_PERFORMANCE_DATA"))
116 (*reg_type
) = HKEY_PERFORMANCE_DATA
;
118 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
126 TALLOC_FREE(tmp_ctx
);
130 static NTSTATUS
registry_openkey(TALLOC_CTX
*mem_ctx
,
131 struct rpc_pipe_client
*pipe_hnd
,
132 const char *name
, uint32_t access_mask
,
133 struct policy_handle
*hive_hnd
,
134 struct policy_handle
*key_hnd
)
139 struct winreg_String key
;
140 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
144 if (!reg_hive_key(mem_ctx
, name
, &hive
, &key
.name
)) {
145 return NT_STATUS_INVALID_PARAMETER
;
148 status
= dcerpc_winreg_Connect(b
, mem_ctx
, hive
, access_mask
,
150 if (!(NT_STATUS_IS_OK(status
))) {
153 if (!W_ERROR_IS_OK(werr
)) {
154 return werror_to_ntstatus(werr
);
157 status
= dcerpc_winreg_OpenKey(b
, mem_ctx
, hive_hnd
, key
, 0,
158 access_mask
, key_hnd
, &werr
);
159 if (!(NT_STATUS_IS_OK(status
))) {
160 dcerpc_winreg_CloseKey(b
, mem_ctx
, hive_hnd
, &werr
);
163 if (!(W_ERROR_IS_OK(werr
))) {
165 dcerpc_winreg_CloseKey(b
, mem_ctx
, hive_hnd
, &_werr
);
166 return werror_to_ntstatus(werr
);
172 static NTSTATUS
registry_enumkeys(TALLOC_CTX
*ctx
,
173 struct rpc_pipe_client
*pipe_hnd
,
174 struct policy_handle
*key_hnd
,
175 uint32_t *pnum_keys
, char ***pnames
,
176 char ***pclasses
, NTTIME
***pmodtimes
)
181 uint32_t num_subkeys
, max_subkeylen
, max_classlen
;
182 uint32_t num_values
, max_valnamelen
, max_valbufsize
;
184 NTTIME last_changed_time
;
185 uint32_t secdescsize
;
186 struct winreg_String classname
;
187 char **names
, **classes
;
189 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
191 if (!(mem_ctx
= talloc_new(ctx
))) {
192 return NT_STATUS_NO_MEMORY
;
195 ZERO_STRUCT(classname
);
196 status
= dcerpc_winreg_QueryInfoKey(
197 b
, mem_ctx
, key_hnd
, &classname
, &num_subkeys
,
198 &max_subkeylen
, &max_classlen
, &num_values
, &max_valnamelen
,
199 &max_valbufsize
, &secdescsize
, &last_changed_time
, &werr
);
201 if (!NT_STATUS_IS_OK(status
)) {
204 if (!W_ERROR_IS_OK(werr
)) {
205 status
= werror_to_ntstatus(werr
);
209 if (num_subkeys
== 0) {
211 TALLOC_FREE(mem_ctx
);
215 if ((!(names
= talloc_zero_array(mem_ctx
, char *, num_subkeys
))) ||
216 (!(classes
= talloc_zero_array(mem_ctx
, char *, num_subkeys
))) ||
217 (!(modtimes
= talloc_zero_array(mem_ctx
, NTTIME
*,
219 status
= NT_STATUS_NO_MEMORY
;
223 for (i
=0; i
<num_subkeys
; i
++) {
225 struct winreg_StringBuf class_buf
;
226 struct winreg_StringBuf name_buf
;
231 class_buf
.size
= max_classlen
+2;
235 name_buf
.size
= max_subkeylen
+2;
237 ZERO_STRUCT(modtime
);
239 status
= dcerpc_winreg_EnumKey(b
, mem_ctx
, key_hnd
,
240 i
, &name_buf
, &class_buf
,
242 if (!NT_STATUS_IS_OK(status
)) {
245 if (W_ERROR_EQUAL(werr
,
246 WERR_NO_MORE_ITEMS
) ) {
247 status
= NT_STATUS_OK
;
250 if (!W_ERROR_IS_OK(werr
)) {
251 status
= werror_to_ntstatus(werr
);
257 if (class_buf
.name
&&
258 (!(classes
[i
] = talloc_strdup(classes
, class_buf
.name
)))) {
259 status
= NT_STATUS_NO_MEMORY
;
263 if (!(names
[i
] = talloc_strdup(names
, name_buf
.name
))) {
264 status
= NT_STATUS_NO_MEMORY
;
268 if ((!(modtimes
[i
] = (NTTIME
*)talloc_memdup(
269 modtimes
, &modtime
, sizeof(modtime
))))) {
270 status
= NT_STATUS_NO_MEMORY
;
275 *pnum_keys
= num_subkeys
;
278 *pnames
= talloc_move(ctx
, &names
);
281 *pclasses
= talloc_move(ctx
, &classes
);
284 *pmodtimes
= talloc_move(ctx
, &modtimes
);
287 status
= NT_STATUS_OK
;
290 TALLOC_FREE(mem_ctx
);
294 static NTSTATUS
registry_enumvalues(TALLOC_CTX
*ctx
,
295 struct rpc_pipe_client
*pipe_hnd
,
296 struct policy_handle
*key_hnd
,
297 uint32_t *pnum_values
, char ***pvalnames
,
298 struct registry_value
***pvalues
)
303 uint32_t num_subkeys
, max_subkeylen
, max_classlen
;
304 uint32_t num_values
, max_valnamelen
, max_valbufsize
;
306 NTTIME last_changed_time
;
307 uint32_t secdescsize
;
308 struct winreg_String classname
;
309 struct registry_value
**values
;
311 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
313 if (!(mem_ctx
= talloc_new(ctx
))) {
314 return NT_STATUS_NO_MEMORY
;
317 ZERO_STRUCT(classname
);
318 status
= dcerpc_winreg_QueryInfoKey(
319 b
, mem_ctx
, key_hnd
, &classname
, &num_subkeys
,
320 &max_subkeylen
, &max_classlen
, &num_values
, &max_valnamelen
,
321 &max_valbufsize
, &secdescsize
, &last_changed_time
, &werr
);
323 if (!NT_STATUS_IS_OK(status
)) {
326 if (!W_ERROR_IS_OK(werr
)) {
327 status
= werror_to_ntstatus(werr
);
331 if (num_values
== 0) {
333 TALLOC_FREE(mem_ctx
);
337 if ((!(names
= talloc_array(mem_ctx
, char *, num_values
))) ||
338 (!(values
= talloc_array(mem_ctx
, struct registry_value
*,
340 status
= NT_STATUS_NO_MEMORY
;
344 for (i
=0; i
<num_values
; i
++) {
345 enum winreg_Type type
= REG_NONE
;
346 uint8_t *data
= NULL
;
348 uint32_t value_length
;
351 struct winreg_ValNameBuf name_buf
;
356 name_buf
.size
= max_valnamelen
+ 2;
358 data_size
= max_valbufsize
;
359 data
= (uint8_t *)TALLOC(mem_ctx
, data_size
);
362 status
= dcerpc_winreg_EnumValue(b
, mem_ctx
, key_hnd
,
365 &value_length
, &err
);
366 if (!(NT_STATUS_IS_OK(status
))) {
370 if ( W_ERROR_EQUAL(err
,
371 WERR_NO_MORE_ITEMS
) ) {
372 status
= NT_STATUS_OK
;
376 if (!W_ERROR_IS_OK(err
)) {
377 status
= werror_to_ntstatus(err
);
381 if (name_buf
.name
== NULL
) {
382 status
= NT_STATUS_INVALID_PARAMETER
;
386 if (!(names
[i
] = talloc_strdup(names
, name_buf
.name
))) {
387 status
= NT_STATUS_NO_MEMORY
;
391 values
[i
] = talloc_zero(values
, struct registry_value
);
392 if (values
[i
] == NULL
) {
393 status
= NT_STATUS_NO_MEMORY
;
397 values
[i
]->type
= type
;
398 values
[i
]->data
= data_blob_talloc(values
[i
], data
, data_size
);
401 *pnum_values
= num_values
;
404 *pvalnames
= talloc_move(ctx
, &names
);
407 *pvalues
= talloc_move(ctx
, &values
);
410 status
= NT_STATUS_OK
;
413 TALLOC_FREE(mem_ctx
);
417 static NTSTATUS
registry_enumvalues2(TALLOC_CTX
*ctx
,
418 struct rpc_pipe_client
*pipe_hnd
,
419 struct policy_handle
*key_hnd
,
420 uint32_t *pnum_values
, char ***pvalnames
,
421 struct regval_blob
***pvalues
)
426 uint32_t num_subkeys
, max_subkeylen
, max_classlen
;
427 uint32_t num_values
, max_valnamelen
, max_valbufsize
;
429 NTTIME last_changed_time
;
430 uint32_t secdescsize
;
431 struct winreg_String classname
;
432 struct regval_blob
**values
;
434 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
436 if (!(mem_ctx
= talloc_new(ctx
))) {
437 return NT_STATUS_NO_MEMORY
;
440 ZERO_STRUCT(classname
);
441 status
= dcerpc_winreg_QueryInfoKey(
442 b
, mem_ctx
, key_hnd
, &classname
, &num_subkeys
,
443 &max_subkeylen
, &max_classlen
, &num_values
, &max_valnamelen
,
444 &max_valbufsize
, &secdescsize
, &last_changed_time
, &werr
);
446 if (!NT_STATUS_IS_OK(status
)) {
449 if (!W_ERROR_IS_OK(werr
)) {
450 status
= werror_to_ntstatus(werr
);
454 if (num_values
== 0) {
456 TALLOC_FREE(mem_ctx
);
460 if ((!(names
= talloc_array(mem_ctx
, char *, num_values
))) ||
461 (!(values
= talloc_array(mem_ctx
, struct regval_blob
*,
463 status
= NT_STATUS_NO_MEMORY
;
467 for (i
=0; i
<num_values
; i
++) {
468 enum winreg_Type type
= REG_NONE
;
469 uint8_t *data
= NULL
;
471 uint32_t value_length
;
474 struct winreg_ValNameBuf name_buf
;
479 name_buf
.size
= max_valnamelen
+ 2;
481 data_size
= max_valbufsize
;
482 data
= (uint8_t *)TALLOC(mem_ctx
, data_size
);
485 status
= dcerpc_winreg_EnumValue(b
, mem_ctx
, key_hnd
,
488 &value_length
, &err
);
489 if (!(NT_STATUS_IS_OK(status
))) {
493 if ( W_ERROR_EQUAL(err
, WERR_NO_MORE_ITEMS
) ) {
494 status
= NT_STATUS_OK
;
498 if (!W_ERROR_IS_OK(err
)) {
499 status
= werror_to_ntstatus(err
);
503 if (name_buf
.name
== NULL
) {
504 status
= NT_STATUS_INVALID_PARAMETER
;
508 if (!(names
[i
] = talloc_strdup(names
, name_buf
.name
))) {
509 status
= NT_STATUS_NO_MEMORY
;
513 assert(value_length
<=data_size
); /*??? */
515 values
[i
] = regval_compose(values
,
520 status
= NT_STATUS_NO_MEMORY
;
525 *pnum_values
= num_values
;
528 *pvalnames
= talloc_move(ctx
, &names
);
531 *pvalues
= talloc_move(ctx
, &values
);
534 status
= NT_STATUS_OK
;
537 TALLOC_FREE(mem_ctx
);
541 static NTSTATUS
registry_getsd(TALLOC_CTX
*mem_ctx
,
542 struct dcerpc_binding_handle
*b
,
543 struct policy_handle
*key_hnd
,
545 struct KeySecurityData
*sd
,
548 return dcerpc_winreg_GetKeySecurity(b
, mem_ctx
, key_hnd
,
553 static NTSTATUS
registry_setvalue(TALLOC_CTX
*mem_ctx
,
554 struct rpc_pipe_client
*pipe_hnd
,
555 struct policy_handle
*key_hnd
,
557 const struct registry_value
*value
)
559 struct winreg_String name_string
;
562 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
564 ZERO_STRUCT(name_string
);
566 name_string
.name
= name
;
567 result
= dcerpc_winreg_SetValue(b
, mem_ctx
, key_hnd
,
568 name_string
, value
->type
,
569 value
->data
.data
, value
->data
.length
, &werr
);
570 if (!NT_STATUS_IS_OK(result
)) {
574 return werror_to_ntstatus(werr
);
577 static NTSTATUS
rpc_registry_setvalue_internal(struct net_context
*c
,
578 const struct dom_sid
*domain_sid
,
579 const char *domain_name
,
580 struct cli_state
*cli
,
581 struct rpc_pipe_client
*pipe_hnd
,
586 struct policy_handle hive_hnd
, key_hnd
;
589 struct registry_value value
;
590 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
592 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
593 SEC_FLAG_MAXIMUM_ALLOWED
,
594 &hive_hnd
, &key_hnd
);
595 if (!NT_STATUS_IS_OK(status
)) {
596 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
601 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
602 d_fprintf(stderr
, _("Too many args for type %s\n"), argv
[2]);
603 return NT_STATUS_NOT_IMPLEMENTED
;
606 if (strequal(argv
[2], "dword")) {
610 v
= smb_strtoul(argv
[3], NULL
, 10, &error
, SMB_STR_STANDARD
);
615 value
.type
= REG_DWORD
;
616 value
.data
= data_blob_talloc(mem_ctx
, NULL
, 4);
617 SIVAL(value
.data
.data
, 0, v
);
619 else if (strequal(argv
[2], "sz")) {
621 if (!push_reg_sz(mem_ctx
, &value
.data
, argv
[3])) {
622 status
= NT_STATUS_NO_MEMORY
;
627 d_fprintf(stderr
, _("type \"%s\" not implemented\n"), argv
[2]);
628 status
= NT_STATUS_NOT_IMPLEMENTED
;
632 status
= registry_setvalue(mem_ctx
, pipe_hnd
, &key_hnd
,
635 if (!NT_STATUS_IS_OK(status
)) {
636 d_fprintf(stderr
, _("registry_setvalue failed: %s\n"),
641 dcerpc_winreg_CloseKey(b
, mem_ctx
, &key_hnd
, &werr
);
642 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &werr
);
647 static int rpc_registry_setvalue(struct net_context
*c
, int argc
,
650 if (argc
< 4 || c
->display_usage
) {
651 d_fprintf(stderr
, "%s\n%s",
653 _("net rpc registry setvalue <key> <valuename> "
654 "<type> [<val>]+\n"));
658 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
659 rpc_registry_setvalue_internal
, argc
, argv
);
662 static NTSTATUS
rpc_registry_deletevalue_internal(struct net_context
*c
,
663 const struct dom_sid
*domain_sid
,
664 const char *domain_name
,
665 struct cli_state
*cli
,
666 struct rpc_pipe_client
*pipe_hnd
,
671 struct policy_handle hive_hnd
, key_hnd
;
674 struct winreg_String valuename
;
675 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
677 ZERO_STRUCT(valuename
);
679 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
680 SEC_FLAG_MAXIMUM_ALLOWED
,
681 &hive_hnd
, &key_hnd
);
682 if (!NT_STATUS_IS_OK(status
)) {
683 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
688 valuename
.name
= argv
[1];
690 status
= dcerpc_winreg_DeleteValue(b
, mem_ctx
, &key_hnd
,
692 if (!NT_STATUS_IS_OK(status
)) {
693 d_fprintf(stderr
, _("registry_deletevalue failed: %s\n"),
696 if (!W_ERROR_IS_OK(werr
)) {
697 status
= werror_to_ntstatus(werr
);
698 d_fprintf(stderr
, _("registry_deletevalue failed: %s\n"),
702 dcerpc_winreg_CloseKey(b
, mem_ctx
, &key_hnd
, &werr
);
703 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &werr
);
708 static int rpc_registry_deletevalue(struct net_context
*c
, int argc
,
711 if (argc
!= 2 || c
->display_usage
) {
712 d_fprintf(stderr
, "%s\n%s",
714 _("net rpc registry deletevalue <key> <valuename>\n"));
718 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
719 rpc_registry_deletevalue_internal
, argc
, argv
);
722 static NTSTATUS
rpc_registry_getvalue_internal(struct net_context
*c
,
723 const struct dom_sid
*domain_sid
,
724 const char *domain_name
,
725 struct cli_state
*cli
,
726 struct rpc_pipe_client
*pipe_hnd
,
732 struct policy_handle hive_hnd
, key_hnd
;
735 struct winreg_String valuename
;
736 struct registry_value
*value
= NULL
;
737 enum winreg_Type type
= REG_NONE
;
738 uint32_t data_size
= 0;
739 uint32_t value_length
= 0;
740 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
741 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
743 ZERO_STRUCT(valuename
);
745 status
= registry_openkey(tmp_ctx
, pipe_hnd
, argv
[0],
746 SEC_FLAG_MAXIMUM_ALLOWED
,
747 &hive_hnd
, &key_hnd
);
748 if (!NT_STATUS_IS_OK(status
)) {
749 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
754 valuename
.name
= argv
[1];
756 value
= talloc_zero(tmp_ctx
, struct registry_value
);
758 return NT_STATUS_NO_MEMORY
;
762 * call QueryValue once with data == NULL to get the
763 * needed memory size to be allocated, then allocate
764 * data buffer and call again.
766 status
= dcerpc_winreg_QueryValue(b
, tmp_ctx
, &key_hnd
,
774 if (!NT_STATUS_IS_OK(status
)) {
775 d_fprintf(stderr
, _("registry_queryvalue failed: %s\n"),
779 if (!W_ERROR_IS_OK(werr
)) {
780 status
= werror_to_ntstatus(werr
);
781 d_fprintf(stderr
, _("registry_queryvalue failed: %s\n"),
786 value
->data
= data_blob_talloc(tmp_ctx
, NULL
, data_size
);
788 status
= dcerpc_winreg_QueryValue(b
, tmp_ctx
, &key_hnd
,
796 if (!NT_STATUS_IS_OK(status
)) {
797 d_fprintf(stderr
, _("registry_queryvalue failed: %s\n"),
801 if (!W_ERROR_IS_OK(werr
)) {
802 status
= werror_to_ntstatus(werr
);
803 d_fprintf(stderr
, _("registry_queryvalue failed: %s\n"),
811 print_registry_value(value
, raw
);
814 dcerpc_winreg_CloseKey(b
, tmp_ctx
, &key_hnd
, &werr
);
815 dcerpc_winreg_CloseKey(b
, tmp_ctx
, &hive_hnd
, &werr
);
817 TALLOC_FREE(tmp_ctx
);
822 static NTSTATUS
rpc_registry_getvalue_full(struct net_context
*c
,
823 const struct dom_sid
*domain_sid
,
824 const char *domain_name
,
825 struct cli_state
*cli
,
826 struct rpc_pipe_client
*pipe_hnd
,
831 return rpc_registry_getvalue_internal(c
, domain_sid
, domain_name
,
832 cli
, pipe_hnd
, mem_ctx
, false,
836 static int rpc_registry_getvalue(struct net_context
*c
, int argc
,
839 if (argc
!= 2 || c
->display_usage
) {
840 d_fprintf(stderr
, "%s\n%s",
842 _("net rpc registry getvalue <key> <valuename>\n"));
846 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
847 rpc_registry_getvalue_full
, argc
, argv
);
850 static NTSTATUS
rpc_registry_getvalue_raw(struct net_context
*c
,
851 const struct dom_sid
*domain_sid
,
852 const char *domain_name
,
853 struct cli_state
*cli
,
854 struct rpc_pipe_client
*pipe_hnd
,
859 return rpc_registry_getvalue_internal(c
, domain_sid
, domain_name
,
860 cli
, pipe_hnd
, mem_ctx
, true,
864 static int rpc_registry_getvalueraw(struct net_context
*c
, int argc
,
867 if (argc
!= 2 || c
->display_usage
) {
868 d_fprintf(stderr
, "%s\n%s",
870 _("net rpc registry getvalue <key> <valuename>\n"));
874 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
875 rpc_registry_getvalue_raw
, argc
, argv
);
878 static NTSTATUS
rpc_registry_createkey_internal(struct net_context
*c
,
879 const struct dom_sid
*domain_sid
,
880 const char *domain_name
,
881 struct cli_state
*cli
,
882 struct rpc_pipe_client
*pipe_hnd
,
888 struct policy_handle hive_hnd
, key_hnd
;
889 struct winreg_String key
, keyclass
;
890 enum winreg_CreateAction action
;
893 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
896 ZERO_STRUCT(keyclass
);
898 if (!reg_hive_key(mem_ctx
, argv
[0], &hive
, &key
.name
)) {
899 return NT_STATUS_INVALID_PARAMETER
;
902 status
= dcerpc_winreg_Connect(b
, mem_ctx
, hive
,
903 SEC_FLAG_MAXIMUM_ALLOWED
,
905 if (!(NT_STATUS_IS_OK(status
))) {
908 if (!W_ERROR_IS_OK(werr
)) {
909 return werror_to_ntstatus(werr
);
912 action
= REG_ACTION_NONE
;
915 status
= dcerpc_winreg_CreateKey(b
, mem_ctx
, &hive_hnd
, key
,
916 keyclass
, 0, REG_KEY_READ
, NULL
,
917 &key_hnd
, &action
, &werr
);
918 if (!NT_STATUS_IS_OK(status
)) {
919 d_fprintf(stderr
, _("createkey returned %s\n"),
921 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &werr
);
924 if (!W_ERROR_IS_OK(werr
)) {
926 d_fprintf(stderr
, _("createkey returned %s\n"),
928 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &_werr
);
929 return werror_to_ntstatus(werr
);
933 case REG_ACTION_NONE
:
934 d_printf(_("createkey did nothing -- huh?\n"));
936 case REG_CREATED_NEW_KEY
:
937 d_printf(_("createkey created %s\n"), argv
[0]);
939 case REG_OPENED_EXISTING_KEY
:
940 d_printf(_("createkey opened existing %s\n"), argv
[0]);
944 dcerpc_winreg_CloseKey(b
, mem_ctx
, &key_hnd
, &werr
);
945 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &werr
);
950 static int rpc_registry_createkey(struct net_context
*c
, int argc
,
953 if (argc
!= 1 || c
->display_usage
) {
954 d_fprintf(stderr
, "%s\n%s",
956 _("net rpc registry createkey <key>\n"));
960 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
961 rpc_registry_createkey_internal
, argc
, argv
);
964 static NTSTATUS
rpc_registry_deletekey_internal(struct net_context
*c
,
965 const struct dom_sid
*domain_sid
,
966 const char *domain_name
,
967 struct cli_state
*cli
,
968 struct rpc_pipe_client
*pipe_hnd
,
974 struct policy_handle hive_hnd
;
975 struct winreg_String key
;
978 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
982 if (!reg_hive_key(mem_ctx
, argv
[0], &hive
, &key
.name
)) {
983 return NT_STATUS_INVALID_PARAMETER
;
986 status
= dcerpc_winreg_Connect(b
, mem_ctx
, hive
,
987 SEC_FLAG_MAXIMUM_ALLOWED
,
989 if (!(NT_STATUS_IS_OK(status
))) {
992 if (!W_ERROR_IS_OK(werr
)) {
993 return werror_to_ntstatus(werr
);
996 status
= dcerpc_winreg_DeleteKey(b
, mem_ctx
, &hive_hnd
, key
, &werr
);
997 if (is_valid_policy_hnd(&hive_hnd
)) {
999 dcerpc_winreg_CloseKey(b
, mem_ctx
, &hive_hnd
, &_werr
);
1002 if (!NT_STATUS_IS_OK(status
)) {
1003 d_fprintf(stderr
, _("deletekey returned %s\n"),
1008 if (!W_ERROR_IS_OK(werr
)) {
1009 d_fprintf(stderr
, _("deletekey returned %s\n"),
1011 return werror_to_ntstatus(werr
);
1017 static int rpc_registry_deletekey(struct net_context
*c
, int argc
, const char **argv
)
1019 if (argc
!= 1 || c
->display_usage
) {
1020 d_fprintf(stderr
, "%s\n%s",
1022 _("net rpc registry deletekey <key>\n"));
1026 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
1027 rpc_registry_deletekey_internal
, argc
, argv
);
1030 /********************************************************************
1031 ********************************************************************/
1033 static NTSTATUS
rpc_registry_enumerate_internal(struct net_context
*c
,
1034 const struct dom_sid
*domain_sid
,
1035 const char *domain_name
,
1036 struct cli_state
*cli
,
1037 struct rpc_pipe_client
*pipe_hnd
,
1038 TALLOC_CTX
*mem_ctx
,
1042 struct policy_handle pol_hive
, pol_key
;
1045 uint32_t num_subkeys
= 0;
1046 uint32_t num_values
= 0;
1047 char **names
= NULL
, **classes
= NULL
;
1048 NTTIME
**modtimes
= NULL
;
1050 struct registry_value
**values
= NULL
;
1051 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1053 if (argc
!= 1 || c
->display_usage
) {
1056 _("net rpc registry enumerate <path>\n"));
1057 d_printf("%s net rpc registry enumerate "
1058 "'HKLM\\Software\\Samba'\n", _("Example:"));
1059 return NT_STATUS_INVALID_PARAMETER
;
1062 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0], REG_KEY_READ
,
1063 &pol_hive
, &pol_key
);
1064 if (!NT_STATUS_IS_OK(status
)) {
1065 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
1070 status
= registry_enumkeys(mem_ctx
, pipe_hnd
, &pol_key
, &num_subkeys
,
1071 &names
, &classes
, &modtimes
);
1072 if (!NT_STATUS_IS_OK(status
)) {
1073 d_fprintf(stderr
, _("enumerating keys failed: %s\n"),
1078 for (i
=0; i
<num_subkeys
; i
++) {
1079 print_registry_key(names
[i
], modtimes
[i
]);
1082 status
= registry_enumvalues(mem_ctx
, pipe_hnd
, &pol_key
, &num_values
,
1084 if (!NT_STATUS_IS_OK(status
)) {
1085 d_fprintf(stderr
, _("enumerating values failed: %s\n"),
1090 for (i
=0; i
<num_values
; i
++) {
1091 print_registry_value_with_name(names
[i
], values
[i
]);
1094 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_key
, &werr
);
1095 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_hive
, &werr
);
1100 /********************************************************************
1101 ********************************************************************/
1103 static int rpc_registry_enumerate(struct net_context
*c
, int argc
,
1106 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
1107 rpc_registry_enumerate_internal
, argc
, argv
);
1110 /********************************************************************
1111 ********************************************************************/
1113 static NTSTATUS
rpc_registry_save_internal(struct net_context
*c
,
1114 const struct dom_sid
*domain_sid
,
1115 const char *domain_name
,
1116 struct cli_state
*cli
,
1117 struct rpc_pipe_client
*pipe_hnd
,
1118 TALLOC_CTX
*mem_ctx
,
1122 WERROR result
= WERR_GEN_FAILURE
;
1123 struct policy_handle pol_hive
, pol_key
;
1124 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1125 struct winreg_String filename
;
1126 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1128 if (argc
!= 2 || c
->display_usage
) {
1131 _("net rpc registry backup <path> <file> \n"));
1132 return NT_STATUS_INVALID_PARAMETER
;
1135 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0], REG_KEY_ALL
,
1136 &pol_hive
, &pol_key
);
1137 if (!NT_STATUS_IS_OK(status
)) {
1138 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
1143 filename
.name
= argv
[1];
1144 status
= dcerpc_winreg_SaveKey(b
, mem_ctx
, &pol_key
, &filename
, NULL
, &result
);
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 d_fprintf(stderr
, _("Unable to save [%s] to %s:%s\n"), argv
[0],
1147 pipe_hnd
->desthost
, argv
[1]);
1149 if (!W_ERROR_IS_OK(result
)) {
1150 status
= werror_to_ntstatus(result
);
1151 d_fprintf(stderr
, _("Unable to save [%s] to %s:%s\n"), argv
[0],
1152 pipe_hnd
->desthost
, argv
[1]);
1157 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_key
, &result
);
1158 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_hive
, &result
);
1163 /********************************************************************
1164 ********************************************************************/
1166 static int rpc_registry_save(struct net_context
*c
, int argc
, const char **argv
)
1168 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
1169 rpc_registry_save_internal
, argc
, argv
);
1173 /********************************************************************
1174 ********************************************************************/
1176 static void dump_values( REGF_NK_REC
*nk
)
1179 const char *data_str
= NULL
;
1180 uint32_t data_size
, data
;
1186 for ( i
=0; i
<nk
->num_values
; i
++ ) {
1187 d_printf( "\"%s\" = ", nk
->values
[i
].valuename
? nk
->values
[i
].valuename
: "(default)" );
1188 d_printf( "(%s) ", str_regtype( nk
->values
[i
].type
) );
1190 data_size
= nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
;
1191 switch ( nk
->values
[i
].type
) {
1193 blob
= data_blob_const(nk
->values
[i
].data
, data_size
);
1194 if (!pull_reg_sz(talloc_tos(), &blob
,
1201 d_printf( "%s", data_str
);
1205 for ( j
=0; j
<data_size
; j
++ ) {
1206 d_printf( "%c", nk
->values
[i
].data
[j
] );
1210 data
= IVAL( nk
->values
[i
].data
, 0 );
1211 d_printf("0x%x", data
);
1214 for ( j
=0; j
<data_size
; j
++ ) {
1215 d_printf( "%x", nk
->values
[i
].data
[j
] );
1219 d_printf(_("unknown"));
1228 /********************************************************************
1229 ********************************************************************/
1231 static bool dump_registry_tree( REGF_FILE
*file
, REGF_NK_REC
*nk
, const char *parent
)
1235 /* depth first dump of the registry tree */
1237 while ( (key
= regfio_fetch_subkey( file
, nk
)) ) {
1239 if (asprintf(®path
, "%s\\%s", parent
, key
->keyname
) < 0) {
1242 d_printf("[%s]\n", regpath
);
1245 dump_registry_tree( file
, key
, regpath
);
1252 /********************************************************************
1253 ********************************************************************/
1255 static bool write_registry_tree( REGF_FILE
*infile
, REGF_NK_REC
*nk
,
1256 REGF_NK_REC
*parent
, REGF_FILE
*outfile
,
1257 const char *parentpath
)
1259 REGF_NK_REC
*key
, *subkey
;
1260 struct regval_ctr
*values
= NULL
;
1261 struct regsubkey_ctr
*subkeys
= NULL
;
1266 werr
= regsubkey_ctr_init(infile
->mem_ctx
, &subkeys
);
1267 if (!W_ERROR_IS_OK(werr
)) {
1268 DEBUG(0, ("write_registry_tree: regsubkey_ctr_init failed: "
1269 "%s\n", win_errstr(werr
)));
1273 werr
= regval_ctr_init(subkeys
, &values
);
1274 if (!W_ERROR_IS_OK(werr
)) {
1275 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
1276 TALLOC_FREE(subkeys
);
1280 /* copy values into the struct regval_ctr */
1282 for ( i
=0; i
<nk
->num_values
; i
++ ) {
1283 regval_ctr_addvalue( values
, nk
->values
[i
].valuename
, nk
->values
[i
].type
,
1284 nk
->values
[i
].data
, (nk
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
) );
1287 /* copy subkeys into the struct regsubkey_ctr */
1289 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
1290 regsubkey_ctr_addkey( subkeys
, subkey
->keyname
);
1293 key
= regfio_write_key( outfile
, nk
->keyname
, values
, subkeys
, nk
->sec_desc
->sec_desc
, parent
);
1295 /* write each one of the subkeys out */
1297 path
= talloc_asprintf(subkeys
,
1303 TALLOC_FREE(subkeys
);
1307 nk
->subkey_index
= 0;
1308 while ( (subkey
= regfio_fetch_subkey( infile
, nk
)) ) {
1309 write_registry_tree( infile
, subkey
, key
, outfile
, path
);
1312 d_printf("[%s]\n", path
);
1313 TALLOC_FREE(subkeys
);
1318 /********************************************************************
1319 ********************************************************************/
1321 static int rpc_registry_dump(struct net_context
*c
, int argc
, const char **argv
)
1323 REGF_FILE
*registry
;
1326 if (argc
!= 1 || c
->display_usage
) {
1329 _("net rpc registry dump <file> \n"));
1333 d_printf(_("Opening %s...."), argv
[0]);
1334 if ( !(registry
= regfio_open( argv
[0], O_RDONLY
, 0)) ) {
1335 d_fprintf(stderr
, _("Failed to open %s for reading\n"),argv
[0]);
1338 d_printf(_("ok\n"));
1340 /* get the root of the registry file */
1342 if ((nk
= regfio_rootkey( registry
)) == NULL
) {
1343 d_fprintf(stderr
, _("Could not get rootkey\n"));
1344 regfio_close( registry
);
1347 d_printf("[%s]\n", nk
->keyname
);
1351 dump_registry_tree( registry
, nk
, nk
->keyname
);
1354 talloc_report_full( registry
->mem_ctx
, stderr
);
1356 d_printf(_("Closing registry..."));
1357 regfio_close( registry
);
1358 d_printf(_("ok\n"));
1363 /********************************************************************
1364 ********************************************************************/
1366 static int rpc_registry_copy(struct net_context
*c
, int argc
, const char **argv
)
1368 REGF_FILE
*infile
= NULL
, *outfile
= NULL
;
1372 if (argc
!= 2 || c
->display_usage
) {
1375 _("net rpc registry copy <srcfile> <newfile>\n"));
1379 d_printf(_("Opening %s...."), argv
[0]);
1380 if ( !(infile
= regfio_open( argv
[0], O_RDONLY
, 0 )) ) {
1381 d_fprintf(stderr
, _("Failed to open %s for reading\n"),argv
[0]);
1384 d_printf(_("ok\n"));
1386 d_printf(_("Opening %s...."), argv
[1]);
1387 if ( !(outfile
= regfio_open( argv
[1], (O_RDWR
|O_CREAT
|O_TRUNC
),
1388 (S_IRUSR
|S_IWUSR
) )) ) {
1389 d_fprintf(stderr
, _("Failed to open %s for writing\n"),argv
[1]);
1392 d_printf(_("ok\n"));
1394 /* get the root of the registry file */
1396 if ((nk
= regfio_rootkey( infile
)) == NULL
) {
1397 d_fprintf(stderr
, _("Could not get rootkey\n"));
1400 d_printf(_("RootKey: [%s]\n"), nk
->keyname
);
1402 write_registry_tree( infile
, nk
, NULL
, outfile
, "" );
1408 d_printf(_("Closing %s..."), argv
[1]);
1410 regfio_close( outfile
);
1412 d_printf(_("ok\n"));
1414 d_printf(_("Closing %s..."), argv
[0]);
1416 regfio_close( infile
);
1418 d_printf(_("ok\n"));
1423 /********************************************************************
1424 ********************************************************************/
1426 static NTSTATUS
rpc_registry_getsd_internal(struct net_context
*c
,
1427 const struct dom_sid
*domain_sid
,
1428 const char *domain_name
,
1429 struct cli_state
*cli
,
1430 struct rpc_pipe_client
*pipe_hnd
,
1431 TALLOC_CTX
*mem_ctx
,
1435 struct policy_handle pol_hive
, pol_key
;
1438 enum ndr_err_code ndr_err
;
1439 struct KeySecurityData
*sd
= NULL
;
1442 struct security_descriptor sec_desc
;
1443 uint32_t access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
|
1444 SEC_FLAG_SYSTEM_SECURITY
;
1445 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1447 if (argc
<1 || argc
> 2 || c
->display_usage
) {
1450 _("net rpc registry getsd <path> <secinfo>\n"));
1451 d_printf("%s net rpc registry getsd "
1452 "'HKLM\\Software\\Samba'\n", _("Example:"));
1453 return NT_STATUS_INVALID_PARAMETER
;
1456 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0],
1458 &pol_hive
, &pol_key
);
1459 if (!NT_STATUS_IS_OK(status
)) {
1460 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
1465 sd
= talloc_zero(mem_ctx
, struct KeySecurityData
);
1467 status
= NT_STATUS_NO_MEMORY
;
1474 sscanf(argv
[1], "%x", &sec_info
);
1476 sec_info
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
;
1479 status
= registry_getsd(mem_ctx
, b
, &pol_key
, sec_info
, sd
, &werr
);
1480 if (!NT_STATUS_IS_OK(status
)) {
1481 d_fprintf(stderr
, _("getting sd failed: %s\n"),
1485 if (!W_ERROR_IS_OK(werr
)) {
1486 status
= werror_to_ntstatus(werr
);
1487 d_fprintf(stderr
, _("getting sd failed: %s\n"),
1492 blob
.data
= sd
->data
;
1493 blob
.length
= sd
->size
;
1495 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &sec_desc
,
1496 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
1497 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1498 status
= ndr_map_error2ntstatus(ndr_err
);
1501 status
= NT_STATUS_OK
;
1503 display_sec_desc(&sec_desc
);
1506 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_key
, &werr
);
1507 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_hive
, &werr
);
1513 static int rpc_registry_getsd(struct net_context
*c
, int argc
, const char **argv
)
1515 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
1516 rpc_registry_getsd_internal
, argc
, argv
);
1519 /********************************************************************
1520 ********************************************************************/
1522 * @defgroup net_rpc_registry net rpc registry
1526 * @defgroup net_rpc_registry_export Export
1527 * @ingroup net_rpc_registry
1531 static NTSTATUS
registry_export(struct rpc_pipe_client
* pipe_hnd
,
1533 struct policy_handle
* key_hnd
,
1534 struct reg_format
* f
,
1535 const char* parentfullname
,
1539 uint32_t num_subkeys
= 0;
1540 uint32_t num_values
= 0;
1541 char **names
= NULL
, **classes
= NULL
;
1542 NTTIME
**modtimes
= NULL
;
1543 struct regval_blob
**values
= NULL
;
1545 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1547 TALLOC_CTX
* mem_ctx
= talloc_new(ctx
);
1550 const char* fullname
= name
1551 ? talloc_asprintf(mem_ctx
, "%s\\%s", parentfullname
, name
)
1553 reg_format_key(f
, &fullname
, 1, false);
1555 status
= registry_enumvalues2(mem_ctx
, pipe_hnd
, key_hnd
, &num_values
,
1557 if (!NT_STATUS_IS_OK(status
)) {
1558 d_fprintf(stderr
, _("enumerating values failed: %s\n"),
1563 for (i
=0; i
<num_values
; i
++) {
1564 reg_format_regval_blob(f
, names
[i
], values
[i
]);
1568 status
= registry_enumkeys(mem_ctx
, pipe_hnd
, key_hnd
, &num_subkeys
,
1569 &names
, &classes
, &modtimes
);
1570 if (!NT_STATUS_IS_OK(status
)) {
1571 d_fprintf(stderr
, _("enumerating keys failed: %s\n"),
1576 for (i
=0; i
<num_subkeys
; i
++) {
1577 struct policy_handle subkey_hnd
;
1578 struct winreg_String key
;
1581 /* key.name = talloc_strdup(mem_ctx, names[i]); ??? */
1582 key
.name
= names
[i
];
1584 status
= dcerpc_winreg_OpenKey(b
, mem_ctx
, key_hnd
, key
,
1586 &subkey_hnd
, &werr
);
1587 if (!NT_STATUS_IS_OK(status
)) {
1589 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1590 names
[i
], nt_errstr(status
));
1593 if (!W_ERROR_IS_OK(werr
)) {
1594 status
= werror_to_ntstatus(werr
);
1596 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1597 names
[i
], win_errstr(werr
));
1601 status
= registry_export(pipe_hnd
, mem_ctx
, &subkey_hnd
,
1602 f
, fullname
, names
[i
]);
1603 if (!(NT_STATUS_IS_OK(status
))) {
1605 _("export key failed: %s %s\n"),
1606 names
[i
], nt_errstr(status
));
1608 dcerpc_winreg_CloseKey(b
, mem_ctx
,
1609 &subkey_hnd
, &werr
);
1612 talloc_free(mem_ctx
);
1616 static NTSTATUS
rpc_registry_export_internal(struct net_context
*c
,
1617 const struct dom_sid
*domain_sid
,
1618 const char *domain_name
,
1619 struct cli_state
*cli
,
1620 struct rpc_pipe_client
*pipe_hnd
,
1621 TALLOC_CTX
*mem_ctx
,
1625 struct policy_handle pol_hive
, pol_key
;
1628 struct reg_format
* f
;
1629 struct dcerpc_binding_handle
*b
= pipe_hnd
->binding_handle
;
1631 if (argc
< 2 || argc
> 3 || c
->display_usage
) {
1634 _("net rpc registry export <path> <file> [opt]\n"));
1635 d_printf("%s net rpc registry export "
1636 "'HKLM\\Software\\Samba' samba.reg\n", _("Example:"));
1637 return NT_STATUS_INVALID_PARAMETER
;
1640 status
= registry_openkey(mem_ctx
, pipe_hnd
, argv
[0], REG_KEY_READ
,
1641 &pol_hive
, &pol_key
);
1642 if (!NT_STATUS_IS_OK(status
)) {
1643 d_fprintf(stderr
, _("registry_openkey failed: %s\n"),
1648 f
= reg_format_file(mem_ctx
, argv
[1], (argc
> 2) ? argv
[2] : NULL
);
1650 d_fprintf(stderr
, _("open file failed: %s\n"), strerror(errno
));
1651 return map_nt_error_from_unix(errno
);
1654 status
= registry_export(pipe_hnd
, mem_ctx
, &pol_key
,
1656 if (!NT_STATUS_IS_OK(status
))
1659 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_key
, &werr
);
1660 dcerpc_winreg_CloseKey(b
, mem_ctx
, &pol_hive
, &werr
);
1664 /********************************************************************
1665 ********************************************************************/
1667 static int rpc_registry_export(struct net_context
*c
, int argc
,
1670 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
1671 rpc_registry_export_internal
, argc
, argv
);
1676 /********************************************************************
1677 ********************************************************************/
1680 * @defgroup net_rpc_registry_import Import
1681 * @ingroup net_rpc_registry
1686 struct rpc_pipe_client
*pipe_hnd
;
1687 TALLOC_CTX
*mem_ctx
;
1690 static WERROR
import_create_key(struct import_ctx
* ctx
,
1691 struct policy_handle
* parent
, const char* name
,
1692 void** pkey
, bool* existing
)
1696 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
1698 struct policy_handle
* key
= NULL
;
1699 struct policy_handle hive
;
1700 struct winreg_String keyclass
, keyname
;
1701 enum winreg_CreateAction action
= REG_ACTION_NONE
;
1702 struct dcerpc_binding_handle
*b
= ctx
->pipe_hnd
->binding_handle
;
1704 ZERO_STRUCT(keyname
);
1705 keyname
.name
= name
;
1707 if (parent
== NULL
) {
1708 uint32_t hive_idx
= 0;
1709 if (!reg_hive_key(mem_ctx
, name
, &hive_idx
, &keyname
.name
)) {
1714 status
= dcerpc_winreg_Connect(b
, mem_ctx
,
1715 hive_idx
, SEC_FLAG_MAXIMUM_ALLOWED
,
1717 if (!NT_STATUS_IS_OK(status
)) {
1718 werr
= ntstatus_to_werror(status
);
1719 d_fprintf(stderr
, _("dcerpc_winreg_Connect returned %s\n"),
1723 if (!W_ERROR_IS_OK(werr
)) {
1724 d_fprintf(stderr
, _("dcerpc_winreg_Connect returned %s\n"),
1732 key
= talloc_zero(mem_ctx
, struct policy_handle
);
1734 werr
= WERR_NOT_ENOUGH_MEMORY
;
1738 ZERO_STRUCT(keyclass
);
1741 status
= dcerpc_winreg_CreateKey(b
, mem_ctx
,
1743 keyclass
, 0, REG_KEY_READ
, NULL
,
1744 key
, &action
, &werr
);
1745 if (!NT_STATUS_IS_OK(status
)) {
1746 werr
= ntstatus_to_werror(status
);
1747 d_fprintf(stderr
, _("dcerpc_winreg_CreateKey returned %s\n"),
1751 if (!W_ERROR_IS_OK(werr
)) {
1752 d_fprintf(stderr
, _("dcerpc_winreg_CreateKey returned %s\n"),
1758 case REG_CREATED_NEW_KEY
:
1759 d_printf(_("createkey created %s\n"), name
);
1760 if (existing
!= NULL
)
1764 case REG_OPENED_EXISTING_KEY
:
1765 d_printf(_("createkey opened existing %s\n"), name
);
1766 if (existing
!= NULL
)
1770 case REG_ACTION_NONE
:
1771 d_printf(_("createkey did nothing -- huh?\n"));
1772 werr
= WERR_CREATE_FAILED
;
1779 if ( parent
== &hive
) {
1781 dcerpc_winreg_CloseKey(b
, mem_ctx
,
1786 *pkey
= talloc_steal(ctx
->mem_ctx
, key
);
1789 talloc_free(mem_ctx
);
1793 static WERROR
import_delete_key(struct import_ctx
* ctx
,
1794 struct policy_handle
* parent
, const char* name
)
1798 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
1799 struct winreg_String keyname
= { 0, };
1800 struct policy_handle hive
;
1801 struct dcerpc_binding_handle
*b
= ctx
->pipe_hnd
->binding_handle
;
1803 keyname
.name
= name
;
1805 if (parent
== NULL
) {
1807 if (!reg_hive_key(mem_ctx
, name
, &hive_idx
, &keyname
.name
)) {
1812 status
= dcerpc_winreg_Connect(b
, mem_ctx
, hive_idx
,
1813 SEC_FLAG_MAXIMUM_ALLOWED
, &hive
,
1815 if (!NT_STATUS_IS_OK(status
)) {
1816 werr
= ntstatus_to_werror(status
);
1817 d_fprintf(stderr
, _("dcerpc_winreg_Connect returned %s\n"),
1821 if (!W_ERROR_IS_OK(werr
)) {
1822 d_fprintf(stderr
, _("dcerpc_winreg_Connect returned %s\n"),
1830 status
= dcerpc_winreg_DeleteKey(b
, mem_ctx
, parent
,
1832 if (!NT_STATUS_IS_OK(status
)) {
1833 werr
= ntstatus_to_werror(status
);
1834 d_fprintf(stderr
, _("dcerpc_winreg_DeleteKey returned %s\n"),
1838 if (!W_ERROR_IS_OK(werr
)) {
1839 d_fprintf(stderr
, _("dcerpc_winreg_DeleteKey returned %s\n"),
1845 if ( parent
== &hive
) {
1847 dcerpc_winreg_CloseKey(b
, mem_ctx
, parent
, &_result
);
1850 talloc_free(mem_ctx
);
1854 static WERROR
import_close_key(struct import_ctx
* ctx
,
1855 struct policy_handle
* key
)
1859 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
1860 struct dcerpc_binding_handle
*b
= ctx
->pipe_hnd
->binding_handle
;
1862 status
= dcerpc_winreg_CloseKey(b
, mem_ctx
, key
, &werr
);
1863 if (!NT_STATUS_IS_OK(status
)) {
1864 werr
= ntstatus_to_werror(status
);
1865 d_fprintf(stderr
, _("dcerpc_winreg_CloseKey returned %s\n"),
1869 if (!W_ERROR_IS_OK(werr
)) {
1870 d_fprintf(stderr
, _("dcerpc_winreg_CloseKey returned %s\n"),
1875 werr
= (talloc_free(key
) == 0) ? WERR_OK
: WERR_GEN_FAILURE
;
1877 talloc_free(mem_ctx
);
1881 static WERROR
import_create_val(struct import_ctx
* ctx
,
1882 struct policy_handle
* parent
, const char* name
,
1883 uint32_t type
, const uint8_t* val
, uint32_t len
)
1887 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
1888 struct winreg_String valuename
;
1889 struct dcerpc_binding_handle
*b
= ctx
->pipe_hnd
->binding_handle
;
1891 if (parent
== NULL
) {
1892 return WERR_INVALID_PARAMETER
;
1895 ZERO_STRUCT(valuename
);
1896 valuename
.name
= name
;
1898 status
= dcerpc_winreg_SetValue(b
, mem_ctx
, parent
,
1900 (uint8_t *)discard_const(val
), len
, &werr
);
1901 if (!NT_STATUS_IS_OK(status
)) {
1902 werr
= ntstatus_to_werror(status
);
1903 d_fprintf(stderr
, _("registry_setvalue failed: %s\n"),
1907 if (!W_ERROR_IS_OK(werr
)) {
1908 d_fprintf(stderr
, _("registry_setvalue failed: %s\n"),
1914 talloc_free(mem_ctx
);
1918 static WERROR
import_delete_val(struct import_ctx
* ctx
,
1919 struct policy_handle
* parent
, const char* name
)
1923 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
1924 struct winreg_String valuename
;
1925 struct dcerpc_binding_handle
*b
= ctx
->pipe_hnd
->binding_handle
;
1927 if (parent
== NULL
) {
1928 return WERR_INVALID_PARAMETER
;
1931 ZERO_STRUCT(valuename
);
1932 valuename
.name
= name
;
1934 status
= dcerpc_winreg_DeleteValue(b
, mem_ctx
,
1935 parent
, valuename
, &werr
);
1937 if (!NT_STATUS_IS_OK(status
)) {
1938 werr
= ntstatus_to_werror(status
);
1939 d_fprintf(stderr
, _("registry_deletevalue failed: %s\n"),
1943 if (!NT_STATUS_IS_OK(status
)) {
1944 d_fprintf(stderr
, _("registry_deletevalue failed: %s\n"),
1950 talloc_free(mem_ctx
);
1956 static NTSTATUS
rpc_registry_import_internal(struct net_context
*c
,
1957 const struct dom_sid
*domain_sid
,
1958 const char *domain_name
,
1959 struct cli_state
*cli
,
1960 struct rpc_pipe_client
*pipe_hnd
,
1961 TALLOC_CTX
*mem_ctx
,
1965 struct import_ctx import_ctx
;
1967 struct reg_import_callback import_callback
= {
1969 .closekey
= (reg_import_callback_closekey_t
)&import_close_key
,
1970 .createkey
= (reg_import_callback_createkey_t
)&import_create_key
,
1971 .deletekey
= (reg_import_callback_deletekey_t
)&import_delete_key
,
1972 .deleteval
= (reg_import_callback_deleteval_t
)&import_delete_val
,
1974 .blob
= (reg_import_callback_setval_blob_t
)&import_create_val
,
1976 .setval_type
= BLOB
,
1981 if (argc
< 1 || argc
> 2 || c
->display_usage
) {
1984 _("net rpc registry import <file> [options]\n"));
1985 d_printf("%s net rpc registry export "
1986 "samba.reg enc=CP1252,flags=0\n", _("Example:"));
1987 return NT_STATUS_INVALID_PARAMETER
;
1989 ZERO_STRUCT(import_ctx
);
1990 import_ctx
.pipe_hnd
= pipe_hnd
;
1991 import_ctx
.mem_ctx
= mem_ctx
;
1992 ret
= reg_parse_file(argv
[0],
1993 reg_import_adapter(import_ctx
.mem_ctx
,
1996 (argc
> 1) ? argv
[1] : NULL
1999 return ret
==0 ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
2002 /********************************************************************
2003 ********************************************************************/
2005 static int rpc_registry_import(struct net_context
*c
, int argc
,
2008 return run_rpc_command(c
, NULL
, &ndr_table_winreg
, 0,
2009 rpc_registry_import_internal
, argc
, argv
);
2013 /********************************************************************
2014 ********************************************************************/
2016 int net_rpc_registry(struct net_context
*c
, int argc
, const char **argv
)
2018 struct functable func
[] = {
2021 rpc_registry_enumerate
,
2023 N_("Enumerate registry keys and values"),
2024 N_("net rpc registry enumerate\n"
2025 " Enumerate registry keys and values")
2029 rpc_registry_createkey
,
2031 N_("Create a new registry key"),
2032 N_("net rpc registry createkey\n"
2033 " Create a new registry key")
2037 rpc_registry_deletekey
,
2039 N_("Delete a registry key"),
2040 N_("net rpc registry deletekey\n"
2041 " Delete a registry key")
2045 rpc_registry_getvalue
,
2047 N_("Print a registry value"),
2048 N_("net rpc registry getvalue\n"
2049 " Print a registry value")
2053 rpc_registry_getvalueraw
,
2055 N_("Print a registry value"),
2056 N_("net rpc registry getvalueraw\n"
2057 " Print a registry value (raw version)")
2061 rpc_registry_setvalue
,
2063 N_("Set a new registry value"),
2064 N_("net rpc registry setvalue\n"
2065 " Set a new registry value")
2069 rpc_registry_deletevalue
,
2071 N_("Delete a registry value"),
2072 N_("net rpc registry deletevalue\n"
2073 " Delete a registry value")
2079 N_("Save a registry file"),
2080 N_("net rpc registry save\n"
2081 " Save a registry file")
2087 N_("Dump a registry file"),
2088 N_("net rpc registry dump\n"
2089 " Dump a registry file")
2095 N_("Copy a registry file"),
2096 N_("net rpc registry copy\n"
2097 " Copy a registry file")
2103 N_("Get security descriptor"),
2104 N_("net rpc registry getsd\n"
2105 " Get security descriptor")
2109 rpc_registry_import
,
2111 N_("Import .reg file"),
2112 N_("net rpc registry import\n"
2113 " Import .reg file")
2117 rpc_registry_export
,
2119 N_("Export .reg file"),
2120 N_("net rpc registry export\n"
2121 " Export .reg file")
2123 {NULL
, NULL
, 0, NULL
, NULL
}
2125 return net_run_function(c
, argc
, argv
, "net rpc registry", func
);