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 "popt_common.h"
23 #include "utils/net.h"
24 #include "utils/net_registry_util.h"
25 #include "registry/regfio.h"
26 #include "../librpc/gen_ndr/ndr_winreg_c.h"
27 #include "registry/reg_objects.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"
35 /*******************************************************************
36 connect to a registry hive root (open a registry policy)
37 *******************************************************************/
39 static NTSTATUS dcerpc_winreg_Connect(struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
40 uint32_t reg_type, uint32_t access_mask,
41 struct policy_handle *reg_hnd, WERROR *werr)
43 ZERO_STRUCTP(reg_hnd);
47 case HKEY_CLASSES_ROOT:
48 return dcerpc_winreg_OpenHKCR(b, mem_ctx, NULL,
49 access_mask, reg_hnd, werr);
51 case HKEY_LOCAL_MACHINE:
52 return dcerpc_winreg_OpenHKLM(b, mem_ctx, NULL,
53 access_mask, reg_hnd, werr);
56 return dcerpc_winreg_OpenHKU(b, mem_ctx, NULL,
57 access_mask, reg_hnd, werr);
59 case HKEY_CURRENT_USER:
60 return dcerpc_winreg_OpenHKCU(b, mem_ctx, NULL,
61 access_mask, reg_hnd, werr);
63 case HKEY_PERFORMANCE_DATA:
64 return dcerpc_winreg_OpenHKPD(b, mem_ctx, NULL,
65 access_mask, reg_hnd, werr);
68 /* fall through to end of function */
72 return NT_STATUS_INVALID_PARAMETER;
75 static bool reg_hive_key(TALLOC_CTX *ctx, const char *fullname,
76 uint32 *reg_type, const char **key_name)
79 char *hivename = NULL;
80 char *tmp_keyname = NULL;
82 TALLOC_CTX *tmp_ctx = talloc_stackframe();
84 werr = split_hive_key(tmp_ctx, fullname, &hivename, &tmp_keyname);
85 if (!W_ERROR_IS_OK(werr)) {
89 *key_name = talloc_strdup(ctx, tmp_keyname);
90 if (*key_name == NULL) {
94 if (strequal(hivename, "HKLM") ||
95 strequal(hivename, "HKEY_LOCAL_MACHINE"))
97 (*reg_type) = HKEY_LOCAL_MACHINE;
98 } else if (strequal(hivename, "HKCR") ||
99 strequal(hivename, "HKEY_CLASSES_ROOT"))
101 (*reg_type) = HKEY_CLASSES_ROOT;
102 } else if (strequal(hivename, "HKU") ||
103 strequal(hivename, "HKEY_USERS"))
105 (*reg_type) = HKEY_USERS;
106 } else if (strequal(hivename, "HKCU") ||
107 strequal(hivename, "HKEY_CURRENT_USER"))
109 (*reg_type) = HKEY_CURRENT_USER;
110 } else if (strequal(hivename, "HKPD") ||
111 strequal(hivename, "HKEY_PERFORMANCE_DATA"))
113 (*reg_type) = HKEY_PERFORMANCE_DATA;
115 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
123 TALLOC_FREE(tmp_ctx);
127 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
128 struct rpc_pipe_client *pipe_hnd,
129 const char *name, uint32 access_mask,
130 struct policy_handle *hive_hnd,
131 struct policy_handle *key_hnd)
136 struct winreg_String key;
137 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
141 if (!reg_hive_key(mem_ctx, name, &hive, &key.name)) {
142 return NT_STATUS_INVALID_PARAMETER;
145 status = dcerpc_winreg_Connect(b, mem_ctx, hive, access_mask,
147 if (!(NT_STATUS_IS_OK(status))) {
150 if (!W_ERROR_IS_OK(werr)) {
151 return werror_to_ntstatus(werr);
154 status = dcerpc_winreg_OpenKey(b, mem_ctx, hive_hnd, key, 0,
155 access_mask, key_hnd, &werr);
156 if (!(NT_STATUS_IS_OK(status))) {
157 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &werr);
160 if (!(W_ERROR_IS_OK(werr))) {
162 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &_werr);
163 return werror_to_ntstatus(werr);
169 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
170 struct rpc_pipe_client *pipe_hnd,
171 struct policy_handle *key_hnd,
172 uint32 *pnum_keys, char ***pnames,
173 char ***pclasses, NTTIME ***pmodtimes)
178 uint32 num_subkeys, max_subkeylen, max_classlen;
179 uint32 num_values, max_valnamelen, max_valbufsize;
181 NTTIME last_changed_time;
183 struct winreg_String classname;
184 char **names, **classes;
186 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
188 if (!(mem_ctx = talloc_new(ctx))) {
189 return NT_STATUS_NO_MEMORY;
192 ZERO_STRUCT(classname);
193 status = dcerpc_winreg_QueryInfoKey(
194 b, mem_ctx, key_hnd, &classname, &num_subkeys,
195 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
196 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
198 if (!NT_STATUS_IS_OK(status)) {
201 if (!W_ERROR_IS_OK(werr)) {
202 status = werror_to_ntstatus(werr);
206 if (num_subkeys == 0) {
208 TALLOC_FREE(mem_ctx);
212 if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
213 (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
214 (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
216 status = NT_STATUS_NO_MEMORY;
220 for (i=0; i<num_subkeys; i++) {
222 struct winreg_StringBuf class_buf;
223 struct winreg_StringBuf name_buf;
228 class_buf.size = max_classlen+2;
232 name_buf.size = max_subkeylen+2;
234 ZERO_STRUCT(modtime);
236 status = dcerpc_winreg_EnumKey(b, mem_ctx, key_hnd,
237 i, &name_buf, &class_buf,
239 if (!NT_STATUS_IS_OK(status)) {
242 if (W_ERROR_EQUAL(werr,
243 WERR_NO_MORE_ITEMS) ) {
244 status = NT_STATUS_OK;
247 if (!W_ERROR_IS_OK(werr)) {
248 status = werror_to_ntstatus(werr);
254 if (class_buf.name &&
255 (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
256 status = NT_STATUS_NO_MEMORY;
260 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
261 status = NT_STATUS_NO_MEMORY;
265 if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
266 modtimes, &modtime, sizeof(modtime))))) {
267 status = NT_STATUS_NO_MEMORY;
272 *pnum_keys = num_subkeys;
275 *pnames = talloc_move(ctx, &names);
278 *pclasses = talloc_move(ctx, &classes);
281 *pmodtimes = talloc_move(ctx, &modtimes);
284 status = NT_STATUS_OK;
287 TALLOC_FREE(mem_ctx);
291 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
292 struct rpc_pipe_client *pipe_hnd,
293 struct policy_handle *key_hnd,
294 uint32 *pnum_values, char ***pvalnames,
295 struct registry_value ***pvalues)
300 uint32 num_subkeys, max_subkeylen, max_classlen;
301 uint32 num_values, max_valnamelen, max_valbufsize;
303 NTTIME last_changed_time;
305 struct winreg_String classname;
306 struct registry_value **values;
308 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
310 if (!(mem_ctx = talloc_new(ctx))) {
311 return NT_STATUS_NO_MEMORY;
314 ZERO_STRUCT(classname);
315 status = dcerpc_winreg_QueryInfoKey(
316 b, mem_ctx, key_hnd, &classname, &num_subkeys,
317 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
318 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
320 if (!NT_STATUS_IS_OK(status)) {
323 if (!W_ERROR_IS_OK(werr)) {
324 status = werror_to_ntstatus(werr);
328 if (num_values == 0) {
330 TALLOC_FREE(mem_ctx);
334 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
335 (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
337 status = NT_STATUS_NO_MEMORY;
341 for (i=0; i<num_values; i++) {
342 enum winreg_Type type = REG_NONE;
348 struct winreg_ValNameBuf name_buf;
353 name_buf.size = max_valnamelen + 2;
355 data_size = max_valbufsize;
356 data = (uint8 *)TALLOC(mem_ctx, data_size);
359 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
362 &value_length, &err);
363 if (!(NT_STATUS_IS_OK(status))) {
367 if ( W_ERROR_EQUAL(err,
368 WERR_NO_MORE_ITEMS) ) {
369 status = NT_STATUS_OK;
373 if (!W_ERROR_IS_OK(err)) {
374 status = werror_to_ntstatus(err);
378 if (name_buf.name == NULL) {
379 status = NT_STATUS_INVALID_PARAMETER;
383 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
384 status = NT_STATUS_NO_MEMORY;
388 values[i] = talloc_zero(values, struct registry_value);
389 if (values[i] == NULL) {
390 status = NT_STATUS_NO_MEMORY;
394 values[i]->type = type;
395 values[i]->data = data_blob_talloc(values[i], data, data_size);
398 *pnum_values = num_values;
401 *pvalnames = talloc_move(ctx, &names);
404 *pvalues = talloc_move(ctx, &values);
407 status = NT_STATUS_OK;
410 TALLOC_FREE(mem_ctx);
414 static NTSTATUS registry_enumvalues2(TALLOC_CTX *ctx,
415 struct rpc_pipe_client *pipe_hnd,
416 struct policy_handle *key_hnd,
417 uint32 *pnum_values, char ***pvalnames,
418 struct regval_blob ***pvalues)
423 uint32 num_subkeys, max_subkeylen, max_classlen;
424 uint32 num_values, max_valnamelen, max_valbufsize;
426 NTTIME last_changed_time;
428 struct winreg_String classname;
429 struct regval_blob **values;
431 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
433 if (!(mem_ctx = talloc_new(ctx))) {
434 return NT_STATUS_NO_MEMORY;
437 ZERO_STRUCT(classname);
438 status = dcerpc_winreg_QueryInfoKey(
439 b, mem_ctx, key_hnd, &classname, &num_subkeys,
440 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
441 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
443 if (!NT_STATUS_IS_OK(status)) {
446 if (!W_ERROR_IS_OK(werr)) {
447 status = werror_to_ntstatus(werr);
451 if (num_values == 0) {
453 TALLOC_FREE(mem_ctx);
457 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
458 (!(values = TALLOC_ARRAY(mem_ctx, struct regval_blob *,
460 status = NT_STATUS_NO_MEMORY;
464 for (i=0; i<num_values; i++) {
465 enum winreg_Type type = REG_NONE;
471 struct winreg_ValNameBuf name_buf;
476 name_buf.size = max_valnamelen + 2;
478 data_size = max_valbufsize;
479 data = (uint8 *)TALLOC(mem_ctx, data_size);
482 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
485 &value_length, &err);
486 if (!(NT_STATUS_IS_OK(status))) {
490 if ( W_ERROR_EQUAL(err, WERR_NO_MORE_ITEMS) ) {
491 status = NT_STATUS_OK;
495 if (!W_ERROR_IS_OK(err)) {
496 status = werror_to_ntstatus(err);
500 if (name_buf.name == NULL) {
501 status = NT_STATUS_INVALID_PARAMETER;
505 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
506 status = NT_STATUS_NO_MEMORY;
510 assert(value_length<=data_size); //???
512 values[i] = regval_compose(values,
517 status = NT_STATUS_NO_MEMORY;
522 *pnum_values = num_values;
525 *pvalnames = talloc_move(ctx, &names);
528 *pvalues = talloc_move(ctx, &values);
531 status = NT_STATUS_OK;
534 TALLOC_FREE(mem_ctx);
538 static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
539 struct dcerpc_binding_handle *b,
540 struct policy_handle *key_hnd,
542 struct KeySecurityData *sd,
545 return dcerpc_winreg_GetKeySecurity(b, mem_ctx, key_hnd,
550 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
551 struct rpc_pipe_client *pipe_hnd,
552 struct policy_handle *key_hnd,
554 const struct registry_value *value)
556 struct winreg_String name_string;
559 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
561 ZERO_STRUCT(name_string);
563 name_string.name = name;
564 result = dcerpc_winreg_SetValue(b, mem_ctx, key_hnd,
565 name_string, value->type,
566 value->data.data, value->data.length, &werr);
567 if (!NT_STATUS_IS_OK(result)) {
571 return werror_to_ntstatus(werr);
574 static NTSTATUS rpc_registry_setvalue_internal(struct net_context *c,
575 const struct dom_sid *domain_sid,
576 const char *domain_name,
577 struct cli_state *cli,
578 struct rpc_pipe_client *pipe_hnd,
583 struct policy_handle hive_hnd, key_hnd;
586 struct registry_value value;
587 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
589 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
590 SEC_FLAG_MAXIMUM_ALLOWED,
591 &hive_hnd, &key_hnd);
592 if (!NT_STATUS_IS_OK(status)) {
593 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
598 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
599 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
600 return NT_STATUS_NOT_IMPLEMENTED;
603 if (strequal(argv[2], "dword")) {
604 uint32_t v = strtoul(argv[3], NULL, 10);
605 value.type = REG_DWORD;
606 value.data = data_blob_talloc(mem_ctx, NULL, 4);
607 SIVAL(value.data.data, 0, v);
609 else if (strequal(argv[2], "sz")) {
611 if (!push_reg_sz(mem_ctx, &value.data, argv[3])) {
612 status = NT_STATUS_NO_MEMORY;
617 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
618 status = NT_STATUS_NOT_IMPLEMENTED;
622 status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
625 if (!NT_STATUS_IS_OK(status)) {
626 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
631 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
632 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
637 static int rpc_registry_setvalue(struct net_context *c, int argc,
640 if (argc < 4 || c->display_usage) {
641 d_fprintf(stderr, "%s\n%s",
643 _("net rpc registry setvalue <key> <valuename> "
644 "<type> [<val>]+\n"));
648 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
649 rpc_registry_setvalue_internal, argc, argv );
652 static NTSTATUS rpc_registry_deletevalue_internal(struct net_context *c,
653 const struct dom_sid *domain_sid,
654 const char *domain_name,
655 struct cli_state *cli,
656 struct rpc_pipe_client *pipe_hnd,
661 struct policy_handle hive_hnd, key_hnd;
664 struct winreg_String valuename;
665 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
667 ZERO_STRUCT(valuename);
669 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
670 SEC_FLAG_MAXIMUM_ALLOWED,
671 &hive_hnd, &key_hnd);
672 if (!NT_STATUS_IS_OK(status)) {
673 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
678 valuename.name = argv[1];
680 status = dcerpc_winreg_DeleteValue(b, mem_ctx, &key_hnd,
682 if (!NT_STATUS_IS_OK(status)) {
683 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
686 if (!W_ERROR_IS_OK(werr)) {
687 status = werror_to_ntstatus(werr);
688 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
692 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
693 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
698 static int rpc_registry_deletevalue(struct net_context *c, int argc,
701 if (argc != 2 || c->display_usage) {
702 d_fprintf(stderr, "%s\n%s",
704 _("net rpc registry deletevalue <key> <valuename>\n"));
708 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
709 rpc_registry_deletevalue_internal, argc, argv );
712 static NTSTATUS rpc_registry_getvalue_internal(struct net_context *c,
713 const struct dom_sid *domain_sid,
714 const char *domain_name,
715 struct cli_state *cli,
716 struct rpc_pipe_client *pipe_hnd,
722 struct policy_handle hive_hnd, key_hnd;
725 struct winreg_String valuename;
726 struct registry_value *value = NULL;
727 enum winreg_Type type = REG_NONE;
728 uint32_t data_size = 0;
729 uint32_t value_length = 0;
730 TALLOC_CTX *tmp_ctx = talloc_stackframe();
731 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
733 ZERO_STRUCT(valuename);
735 status = registry_openkey(tmp_ctx, pipe_hnd, argv[0],
736 SEC_FLAG_MAXIMUM_ALLOWED,
737 &hive_hnd, &key_hnd);
738 if (!NT_STATUS_IS_OK(status)) {
739 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
744 valuename.name = argv[1];
746 value = talloc_zero(tmp_ctx, struct registry_value);
748 return NT_STATUS_NO_MEMORY;
752 * call QueryValue once with data == NULL to get the
753 * needed memory size to be allocated, then allocate
754 * data buffer and call again.
756 status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
764 if (!NT_STATUS_IS_OK(status)) {
765 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
769 if (!W_ERROR_IS_OK(werr)) {
770 status = werror_to_ntstatus(werr);
771 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
776 value->data = data_blob_talloc(tmp_ctx, NULL, data_size);
778 status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
786 if (!NT_STATUS_IS_OK(status)) {
787 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
791 if (!W_ERROR_IS_OK(werr)) {
792 status = werror_to_ntstatus(werr);
793 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
801 print_registry_value(value, raw);
804 dcerpc_winreg_CloseKey(b, tmp_ctx, &key_hnd, &werr);
805 dcerpc_winreg_CloseKey(b, tmp_ctx, &hive_hnd, &werr);
807 TALLOC_FREE(tmp_ctx);
812 static NTSTATUS rpc_registry_getvalue_full(struct net_context *c,
813 const struct dom_sid *domain_sid,
814 const char *domain_name,
815 struct cli_state *cli,
816 struct rpc_pipe_client *pipe_hnd,
821 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
822 cli, pipe_hnd, mem_ctx, false,
826 static int rpc_registry_getvalue(struct net_context *c, int argc,
829 if (argc != 2 || c->display_usage) {
830 d_fprintf(stderr, "%s\n%s",
832 _("net rpc registry getvalue <key> <valuename>\n"));
836 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
837 rpc_registry_getvalue_full, argc, argv);
840 static NTSTATUS rpc_registry_getvalue_raw(struct net_context *c,
841 const struct dom_sid *domain_sid,
842 const char *domain_name,
843 struct cli_state *cli,
844 struct rpc_pipe_client *pipe_hnd,
849 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
850 cli, pipe_hnd, mem_ctx, true,
854 static int rpc_registry_getvalueraw(struct net_context *c, int argc,
857 if (argc != 2 || c->display_usage) {
858 d_fprintf(stderr, "%s\n%s",
860 _("net rpc registry getvalue <key> <valuename>\n"));
864 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
865 rpc_registry_getvalue_raw, argc, argv);
868 static NTSTATUS rpc_registry_createkey_internal(struct net_context *c,
869 const struct dom_sid *domain_sid,
870 const char *domain_name,
871 struct cli_state *cli,
872 struct rpc_pipe_client *pipe_hnd,
878 struct policy_handle hive_hnd, key_hnd;
879 struct winreg_String key, keyclass;
880 enum winreg_CreateAction action;
883 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
886 ZERO_STRUCT(keyclass);
888 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
889 return NT_STATUS_INVALID_PARAMETER;
892 status = dcerpc_winreg_Connect(b, mem_ctx, hive,
893 SEC_FLAG_MAXIMUM_ALLOWED,
895 if (!(NT_STATUS_IS_OK(status))) {
898 if (!W_ERROR_IS_OK(werr)) {
899 return werror_to_ntstatus(werr);
902 action = REG_ACTION_NONE;
905 status = dcerpc_winreg_CreateKey(b, mem_ctx, &hive_hnd, key,
906 keyclass, 0, REG_KEY_READ, NULL,
907 &key_hnd, &action, &werr);
908 if (!NT_STATUS_IS_OK(status)) {
909 d_fprintf(stderr, _("createkey returned %s\n"),
911 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
914 if (!W_ERROR_IS_OK(werr)) {
916 d_fprintf(stderr, _("createkey returned %s\n"),
918 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
919 return werror_to_ntstatus(werr);
923 case REG_ACTION_NONE:
924 d_printf(_("createkey did nothing -- huh?\n"));
926 case REG_CREATED_NEW_KEY:
927 d_printf(_("createkey created %s\n"), argv[0]);
929 case REG_OPENED_EXISTING_KEY:
930 d_printf(_("createkey opened existing %s\n"), argv[0]);
934 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
935 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
940 static int rpc_registry_createkey(struct net_context *c, int argc,
943 if (argc != 1 || c->display_usage) {
944 d_fprintf(stderr, "%s\n%s",
946 _("net rpc registry createkey <key>\n"));
950 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
951 rpc_registry_createkey_internal, argc, argv );
954 static NTSTATUS rpc_registry_deletekey_internal(struct net_context *c,
955 const struct dom_sid *domain_sid,
956 const char *domain_name,
957 struct cli_state *cli,
958 struct rpc_pipe_client *pipe_hnd,
964 struct policy_handle hive_hnd;
965 struct winreg_String key;
968 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
972 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
973 return NT_STATUS_INVALID_PARAMETER;
976 status = dcerpc_winreg_Connect(b, mem_ctx, hive,
977 SEC_FLAG_MAXIMUM_ALLOWED,
979 if (!(NT_STATUS_IS_OK(status))) {
982 if (!W_ERROR_IS_OK(werr)) {
983 return werror_to_ntstatus(werr);
986 status = dcerpc_winreg_DeleteKey(b, mem_ctx, &hive_hnd, key, &werr);
987 if (is_valid_policy_hnd(&hive_hnd)) {
989 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
992 if (!NT_STATUS_IS_OK(status)) {
993 d_fprintf(stderr, _("deletekey returned %s\n"),
998 if (!W_ERROR_IS_OK(werr)) {
999 d_fprintf(stderr, _("deletekey returned %s\n"),
1001 return werror_to_ntstatus(werr);
1007 static int rpc_registry_deletekey(struct net_context *c, int argc, const char **argv )
1009 if (argc != 1 || c->display_usage) {
1010 d_fprintf(stderr, "%s\n%s",
1012 _("net rpc registry deletekey <key>\n"));
1016 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1017 rpc_registry_deletekey_internal, argc, argv );
1020 /********************************************************************
1021 ********************************************************************/
1023 static NTSTATUS rpc_registry_enumerate_internal(struct net_context *c,
1024 const struct dom_sid *domain_sid,
1025 const char *domain_name,
1026 struct cli_state *cli,
1027 struct rpc_pipe_client *pipe_hnd,
1028 TALLOC_CTX *mem_ctx,
1032 struct policy_handle pol_hive, pol_key;
1035 uint32 num_subkeys = 0;
1036 uint32 num_values = 0;
1037 char **names = NULL, **classes = NULL;
1038 NTTIME **modtimes = NULL;
1040 struct registry_value **values = NULL;
1041 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1043 if (argc != 1 || c->display_usage) {
1046 _("net rpc registry enumerate <path>\n"));
1047 d_printf("%s net rpc registry enumerate "
1048 "'HKLM\\Software\\Samba'\n", _("Example:"));
1049 return NT_STATUS_INVALID_PARAMETER;
1052 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1053 &pol_hive, &pol_key);
1054 if (!NT_STATUS_IS_OK(status)) {
1055 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1060 status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
1061 &names, &classes, &modtimes);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1068 for (i=0; i<num_subkeys; i++) {
1069 print_registry_key(names[i], modtimes[i]);
1072 status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
1074 if (!NT_STATUS_IS_OK(status)) {
1075 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1080 for (i=0; i<num_values; i++) {
1081 print_registry_value_with_name(names[i], values[i]);
1084 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1085 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1090 /********************************************************************
1091 ********************************************************************/
1093 static int rpc_registry_enumerate(struct net_context *c, int argc,
1096 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1097 rpc_registry_enumerate_internal, argc, argv );
1100 /********************************************************************
1101 ********************************************************************/
1103 static NTSTATUS rpc_registry_save_internal(struct net_context *c,
1104 const struct dom_sid *domain_sid,
1105 const char *domain_name,
1106 struct cli_state *cli,
1107 struct rpc_pipe_client *pipe_hnd,
1108 TALLOC_CTX *mem_ctx,
1112 WERROR result = WERR_GENERAL_FAILURE;
1113 struct policy_handle pol_hive, pol_key;
1114 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1115 struct winreg_String filename;
1116 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1118 if (argc != 2 || c->display_usage) {
1121 _("net rpc registry backup <path> <file> \n"));
1122 return NT_STATUS_INVALID_PARAMETER;
1125 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
1126 &pol_hive, &pol_key);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1133 filename.name = argv[1];
1134 status = dcerpc_winreg_SaveKey(b, mem_ctx, &pol_key, &filename, NULL, &result);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1137 cli->desthost, argv[1]);
1139 if (!W_ERROR_IS_OK(result)) {
1140 status = werror_to_ntstatus(result);
1141 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1142 cli->desthost, argv[1]);
1147 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &result);
1148 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &result);
1153 /********************************************************************
1154 ********************************************************************/
1156 static int rpc_registry_save(struct net_context *c, int argc, const char **argv )
1158 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1159 rpc_registry_save_internal, argc, argv );
1163 /********************************************************************
1164 ********************************************************************/
1166 static void dump_values( REGF_NK_REC *nk )
1169 const char *data_str = NULL;
1170 uint32 data_size, data;
1176 for ( i=0; i<nk->num_values; i++ ) {
1177 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
1178 d_printf( "(%s) ", str_regtype( nk->values[i].type ) );
1180 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
1181 switch ( nk->values[i].type ) {
1183 blob = data_blob_const(nk->values[i].data, data_size);
1184 pull_reg_sz(talloc_tos(), &blob, &data_str);
1188 d_printf( "%s", data_str );
1192 for ( j=0; j<data_size; j++ ) {
1193 d_printf( "%c", nk->values[i].data[j] );
1197 data = IVAL( nk->values[i].data, 0 );
1198 d_printf("0x%x", data );
1201 for ( j=0; j<data_size; j++ ) {
1202 d_printf( "%x", nk->values[i].data[j] );
1206 d_printf(_("unknown"));
1215 /********************************************************************
1216 ********************************************************************/
1218 static bool dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
1222 /* depth first dump of the registry tree */
1224 while ( (key = regfio_fetch_subkey( file, nk )) ) {
1226 if (asprintf(®path, "%s\\%s", parent, key->keyname) < 0) {
1229 d_printf("[%s]\n", regpath );
1232 dump_registry_tree( file, key, regpath );
1239 /********************************************************************
1240 ********************************************************************/
1242 static bool write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
1243 REGF_NK_REC *parent, REGF_FILE *outfile,
1244 const char *parentpath )
1246 REGF_NK_REC *key, *subkey;
1247 struct regval_ctr *values = NULL;
1248 struct regsubkey_ctr *subkeys = NULL;
1253 werr = regsubkey_ctr_init(infile->mem_ctx, &subkeys);
1254 if (!W_ERROR_IS_OK(werr)) {
1255 DEBUG(0, ("write_registry_tree: regsubkey_ctr_init failed: "
1256 "%s\n", win_errstr(werr)));
1260 werr = regval_ctr_init(subkeys, &values);
1261 if (!W_ERROR_IS_OK(werr)) {
1262 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
1263 TALLOC_FREE(subkeys);
1267 /* copy values into the struct regval_ctr */
1269 for ( i=0; i<nk->num_values; i++ ) {
1270 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
1271 nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
1274 /* copy subkeys into the struct regsubkey_ctr */
1276 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1277 regsubkey_ctr_addkey( subkeys, subkey->keyname );
1280 key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
1282 /* write each one of the subkeys out */
1284 path = talloc_asprintf(subkeys,
1290 TALLOC_FREE(subkeys);
1294 nk->subkey_index = 0;
1295 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1296 write_registry_tree( infile, subkey, key, outfile, path );
1299 d_printf("[%s]\n", path );
1300 TALLOC_FREE(subkeys);
1305 /********************************************************************
1306 ********************************************************************/
1308 static int rpc_registry_dump(struct net_context *c, int argc, const char **argv)
1310 REGF_FILE *registry;
1313 if (argc != 1 || c->display_usage) {
1316 _("net rpc registry dump <file> \n"));
1320 d_printf(_("Opening %s...."), argv[0]);
1321 if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
1322 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1325 d_printf(_("ok\n"));
1327 /* get the root of the registry file */
1329 if ((nk = regfio_rootkey( registry )) == NULL) {
1330 d_fprintf(stderr, _("Could not get rootkey\n"));
1331 regfio_close( registry );
1334 d_printf("[%s]\n", nk->keyname);
1338 dump_registry_tree( registry, nk, nk->keyname );
1341 talloc_report_full( registry->mem_ctx, stderr );
1343 d_printf(_("Closing registry..."));
1344 regfio_close( registry );
1345 d_printf(_("ok\n"));
1350 /********************************************************************
1351 ********************************************************************/
1353 static int rpc_registry_copy(struct net_context *c, int argc, const char **argv )
1355 REGF_FILE *infile = NULL, *outfile = NULL;
1359 if (argc != 2 || c->display_usage) {
1362 _("net rpc registry copy <srcfile> <newfile>\n"));
1366 d_printf(_("Opening %s...."), argv[0]);
1367 if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
1368 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1371 d_printf(_("ok\n"));
1373 d_printf(_("Opening %s...."), argv[1]);
1374 if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC),
1375 (S_IRUSR|S_IWUSR) )) ) {
1376 d_fprintf(stderr, _("Failed to open %s for writing\n"),argv[1]);
1379 d_printf(_("ok\n"));
1381 /* get the root of the registry file */
1383 if ((nk = regfio_rootkey( infile )) == NULL) {
1384 d_fprintf(stderr, _("Could not get rootkey\n"));
1387 d_printf(_("RootKey: [%s]\n"), nk->keyname);
1389 write_registry_tree( infile, nk, NULL, outfile, "" );
1395 d_printf(_("Closing %s..."), argv[1]);
1397 regfio_close( outfile );
1399 d_printf(_("ok\n"));
1401 d_printf(_("Closing %s..."), argv[0]);
1403 regfio_close( infile );
1405 d_printf(_("ok\n"));
1410 /********************************************************************
1411 ********************************************************************/
1413 static NTSTATUS rpc_registry_getsd_internal(struct net_context *c,
1414 const struct dom_sid *domain_sid,
1415 const char *domain_name,
1416 struct cli_state *cli,
1417 struct rpc_pipe_client *pipe_hnd,
1418 TALLOC_CTX *mem_ctx,
1422 struct policy_handle pol_hive, pol_key;
1425 enum ndr_err_code ndr_err;
1426 struct KeySecurityData *sd = NULL;
1429 struct security_descriptor sec_desc;
1430 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED |
1431 SEC_FLAG_SYSTEM_SECURITY;
1432 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1434 if (argc <1 || argc > 2 || c->display_usage) {
1437 _("net rpc registry getsd <path> <secinfo>\n"));
1438 d_printf("%s net rpc registry getsd "
1439 "'HKLM\\Software\\Samba'\n", _("Example:"));
1440 return NT_STATUS_INVALID_PARAMETER;
1443 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
1445 &pol_hive, &pol_key);
1446 if (!NT_STATUS_IS_OK(status)) {
1447 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1452 sd = TALLOC_ZERO_P(mem_ctx, struct KeySecurityData);
1454 status = NT_STATUS_NO_MEMORY;
1461 sscanf(argv[1], "%x", &sec_info);
1463 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
1466 status = registry_getsd(mem_ctx, b, &pol_key, sec_info, sd, &werr);
1467 if (!NT_STATUS_IS_OK(status)) {
1468 d_fprintf(stderr, _("getting sd failed: %s\n"),
1472 if (!W_ERROR_IS_OK(werr)) {
1473 status = werror_to_ntstatus(werr);
1474 d_fprintf(stderr, _("getting sd failed: %s\n"),
1479 blob.data = sd->data;
1480 blob.length = sd->size;
1482 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &sec_desc,
1483 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1484 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1485 status = ndr_map_error2ntstatus(ndr_err);
1488 status = NT_STATUS_OK;
1490 display_sec_desc(&sec_desc);
1493 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1494 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1500 static int rpc_registry_getsd(struct net_context *c, int argc, const char **argv)
1502 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1503 rpc_registry_getsd_internal, argc, argv);
1506 /********************************************************************
1507 ********************************************************************/
1509 * @defgroup net_rpc_registry net rpc registry
1513 * @defgroup net_rpc_registry_export Export
1514 * @ingroup net_rpc_registry
1518 static NTSTATUS registry_export(struct rpc_pipe_client* pipe_hnd,
1520 struct policy_handle* key_hnd,
1521 struct reg_format* f,
1522 const char* parentfullname,
1526 uint32 num_subkeys = 0;
1527 uint32 num_values = 0;
1528 char **names = NULL, **classes = NULL;
1529 NTTIME **modtimes = NULL;
1530 struct regval_blob **values = NULL;
1532 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1534 TALLOC_CTX* mem_ctx = talloc_new(ctx);
1537 const char* fullname = name
1538 ? talloc_asprintf(mem_ctx, "%s\\%s", parentfullname, name)
1540 reg_format_key(f, &fullname, 1, false);
1542 status = registry_enumvalues2(mem_ctx, pipe_hnd, key_hnd, &num_values,
1544 if (!NT_STATUS_IS_OK(status)) {
1545 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1550 for (i=0; i<num_values; i++) {
1551 reg_format_regval_blob(f, names[i], values[i]);
1555 status = registry_enumkeys(mem_ctx, pipe_hnd, key_hnd, &num_subkeys,
1556 &names, &classes, &modtimes);
1557 if (!NT_STATUS_IS_OK(status)) {
1558 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1563 for (i=0; i<num_subkeys; i++) {
1564 struct policy_handle subkey_hnd;
1565 struct winreg_String key;
1568 /* key.name = talloc_strdup(mem_ctx, names[i]); ??? */
1569 key.name = names[i];
1571 status = dcerpc_winreg_OpenKey(b, mem_ctx, key_hnd, key,
1573 &subkey_hnd, &werr);
1574 if (!NT_STATUS_IS_OK(status)) {
1576 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1577 names[i], nt_errstr(status));
1580 if (!W_ERROR_IS_OK(werr)) {
1581 status = werror_to_ntstatus(werr);
1583 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1584 names[i], win_errstr(werr));
1588 status = registry_export(pipe_hnd, mem_ctx, &subkey_hnd,
1589 f, fullname, names[i]);
1590 if (!(NT_STATUS_IS_OK(status))) {
1592 _("export key failed: %s %s\n"),
1593 names[i], nt_errstr(status));
1595 dcerpc_winreg_CloseKey(b, mem_ctx,
1596 &subkey_hnd, &werr);
1599 talloc_free(mem_ctx);
1603 static NTSTATUS rpc_registry_export_internal(struct net_context *c,
1604 const struct dom_sid *domain_sid,
1605 const char *domain_name,
1606 struct cli_state *cli,
1607 struct rpc_pipe_client *pipe_hnd,
1608 TALLOC_CTX *mem_ctx,
1612 struct policy_handle pol_hive, pol_key;
1615 struct reg_format* f;
1616 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1618 if (argc < 2 || argc > 3 || c->display_usage) {
1621 _("net rpc registry export <path> <file> [opt]\n"));
1622 d_printf("%s net rpc registry export "
1623 "'HKLM\\Software\\Samba' samba.reg\n", _("Example:"));
1624 return NT_STATUS_INVALID_PARAMETER;
1627 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1628 &pol_hive, &pol_key);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1635 f = reg_format_file(mem_ctx, argv[1], (argc > 2) ? argv[2] : NULL);
1637 d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
1638 return map_nt_error_from_unix(errno);
1641 status = registry_export(pipe_hnd, mem_ctx, &pol_key,
1643 if (!NT_STATUS_IS_OK(status))
1646 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1647 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1651 /********************************************************************
1652 ********************************************************************/
1654 static int rpc_registry_export(struct net_context *c, int argc,
1657 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1658 rpc_registry_export_internal, argc, argv );
1663 /********************************************************************
1664 ********************************************************************/
1667 * @defgroup net_rpc_registry_import Import
1668 * @ingroup net_rpc_registry
1673 struct rpc_pipe_client *pipe_hnd;
1674 TALLOC_CTX *mem_ctx;
1677 static WERROR import_create_key(struct import_ctx* ctx,
1678 struct policy_handle* parent, const char* name,
1679 void** pkey, bool* existing)
1683 void* mem_ctx = talloc_new(ctx->mem_ctx);
1685 struct policy_handle* key = NULL;
1686 struct policy_handle hive;
1687 struct winreg_String keyclass, keyname;
1688 enum winreg_CreateAction action = REG_ACTION_NONE;
1689 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1691 ZERO_STRUCT(keyname);
1692 keyname.name = name;
1694 if (parent == NULL) {
1695 uint32 hive_idx = 0;
1696 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1701 status = dcerpc_winreg_Connect(b, mem_ctx,
1702 hive_idx, SEC_FLAG_MAXIMUM_ALLOWED,
1704 if (!NT_STATUS_IS_OK(status)) {
1705 werr = ntstatus_to_werror(status);
1706 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1710 if (!W_ERROR_IS_OK(werr)) {
1711 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1719 key = talloc_zero(mem_ctx, struct policy_handle);
1725 ZERO_STRUCT(keyclass);
1728 status = dcerpc_winreg_CreateKey(b, mem_ctx,
1730 keyclass, 0, REG_KEY_READ, NULL,
1731 key, &action, &werr);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 werr = ntstatus_to_werror(status);
1734 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1738 if (!W_ERROR_IS_OK(werr)) {
1739 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1745 case REG_CREATED_NEW_KEY:
1746 d_printf(_("createkey created %s\n"), name);
1747 if (existing != NULL)
1751 case REG_OPENED_EXISTING_KEY:
1752 d_printf(_("createkey opened existing %s\n"), name);
1753 if (existing != NULL)
1757 case REG_ACTION_NONE:
1758 d_printf(_("createkey did nothing -- huh?\n"));
1759 werr = WERR_CREATE_FAILED;
1766 if ( parent == &hive ) {
1768 dcerpc_winreg_CloseKey(b, mem_ctx,
1773 *pkey = talloc_steal(ctx->mem_ctx, key);
1776 talloc_free(mem_ctx);
1780 static WERROR import_delete_key(struct import_ctx* ctx,
1781 struct policy_handle* parent, const char* name)
1785 void* mem_ctx = talloc_new(ctx->mem_ctx);
1786 struct winreg_String keyname = { 0, };
1787 struct policy_handle hive;
1788 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1790 keyname.name = name;
1792 if (parent == NULL) {
1794 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1799 status = dcerpc_winreg_Connect(b, mem_ctx, hive_idx,
1800 SEC_FLAG_MAXIMUM_ALLOWED, &hive,
1802 if (!NT_STATUS_IS_OK(status)) {
1803 werr = ntstatus_to_werror(status);
1804 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1808 if (!W_ERROR_IS_OK(werr)) {
1809 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1817 status = dcerpc_winreg_DeleteKey(b, mem_ctx, parent,
1819 if (!NT_STATUS_IS_OK(status)) {
1820 werr = ntstatus_to_werror(status);
1821 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1825 if (!W_ERROR_IS_OK(werr)) {
1826 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1832 if ( parent == &hive ) {
1834 dcerpc_winreg_CloseKey(b, mem_ctx, parent, &_result);
1837 talloc_free(mem_ctx);
1841 static WERROR import_close_key(struct import_ctx* ctx,
1842 struct policy_handle* key)
1846 void* mem_ctx = talloc_new(ctx->mem_ctx);
1847 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1849 status = dcerpc_winreg_CloseKey(b, mem_ctx, key, &werr);
1850 if (!NT_STATUS_IS_OK(status)) {
1851 werr = ntstatus_to_werror(status);
1852 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1856 if (!W_ERROR_IS_OK(werr)) {
1857 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1862 werr = (talloc_free(key) == 0) ? WERR_OK : WERR_GENERAL_FAILURE;
1864 talloc_free(mem_ctx);
1868 static WERROR import_create_val(struct import_ctx* ctx,
1869 struct policy_handle* parent, const char* name,
1870 uint32_t type, const uint8_t* val, uint32_t len)
1874 void* mem_ctx = talloc_new(ctx->mem_ctx);
1875 struct winreg_String valuename;
1876 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1878 if (parent == NULL) {
1879 return WERR_INVALID_PARAM;
1882 ZERO_STRUCT(valuename);
1883 valuename.name = name;
1885 status = dcerpc_winreg_SetValue(b, mem_ctx, parent,
1887 (uint8_t *)discard_const(val), len, &werr);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 werr = ntstatus_to_werror(status);
1890 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1894 if (!W_ERROR_IS_OK(werr)) {
1895 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1901 talloc_free(mem_ctx);
1905 static WERROR import_delete_val(struct import_ctx* ctx,
1906 struct policy_handle* parent, const char* name)
1910 void* mem_ctx = talloc_new(ctx->mem_ctx);
1911 struct winreg_String valuename;
1912 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1914 if (parent == NULL) {
1915 return WERR_INVALID_PARAM;
1918 ZERO_STRUCT(valuename);
1919 valuename.name = name;
1921 status = dcerpc_winreg_DeleteValue(b, mem_ctx,
1922 parent, valuename, &werr);
1924 if (!NT_STATUS_IS_OK(status)) {
1925 werr = ntstatus_to_werror(status);
1926 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1930 if (!NT_STATUS_IS_OK(status)) {
1931 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1937 talloc_free(mem_ctx);
1943 static NTSTATUS rpc_registry_import_internal(struct net_context *c,
1944 const struct dom_sid *domain_sid,
1945 const char *domain_name,
1946 struct cli_state *cli,
1947 struct rpc_pipe_client *pipe_hnd,
1948 TALLOC_CTX *mem_ctx,
1952 struct import_ctx import_ctx;
1954 struct reg_import_callback import_callback = {
1956 .closekey = (reg_import_callback_closekey_t)&import_close_key,
1957 .createkey = (reg_import_callback_createkey_t)&import_create_key,
1958 .deletekey = (reg_import_callback_deletekey_t)&import_delete_key,
1959 .deleteval = (reg_import_callback_deleteval_t)&import_delete_val,
1960 .setval.blob = (reg_import_callback_setval_blob_t)&import_create_val,
1961 .setval_type = BLOB,
1966 if (argc < 1 || argc > 2 || c->display_usage) {
1969 _("net rpc registry import <file> [options]\n"));
1970 d_printf("%s net rpc registry export "
1971 "samba.reg enc=CP1252,flags=0\n", _("Example:"));
1972 return NT_STATUS_INVALID_PARAMETER;
1974 ZERO_STRUCT(import_ctx);
1975 import_ctx.pipe_hnd = pipe_hnd;
1976 import_ctx.mem_ctx = mem_ctx;
1977 ret = reg_parse_file(argv[0],
1978 reg_import_adapter(import_ctx.mem_ctx,
1981 (argc > 1) ? argv[1] : NULL
1984 return ret==0 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1987 /********************************************************************
1988 ********************************************************************/
1990 static int rpc_registry_import(struct net_context *c, int argc,
1993 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1994 rpc_registry_import_internal, argc, argv );
1998 /********************************************************************
1999 ********************************************************************/
2001 int net_rpc_registry(struct net_context *c, int argc, const char **argv)
2003 struct functable func[] = {
2006 rpc_registry_enumerate,
2008 N_("Enumerate registry keys and values"),
2009 N_("net rpc registry enumerate\n"
2010 " Enumerate registry keys and values")
2014 rpc_registry_createkey,
2016 N_("Create a new registry key"),
2017 N_("net rpc registry createkey\n"
2018 " Create a new registry key")
2022 rpc_registry_deletekey,
2024 N_("Delete a registry key"),
2025 N_("net rpc registry deletekey\n"
2026 " Delete a registry key")
2030 rpc_registry_getvalue,
2032 N_("Print a registry value"),
2033 N_("net rpc registry getvalue\n"
2034 " Print a registry value")
2038 rpc_registry_getvalueraw,
2040 N_("Print a registry value"),
2041 N_("net rpc registry getvalueraw\n"
2042 " Print a registry value (raw version)")
2046 rpc_registry_setvalue,
2048 N_("Set a new registry value"),
2049 N_("net rpc registry setvalue\n"
2050 " Set a new registry value")
2054 rpc_registry_deletevalue,
2056 N_("Delete a registry value"),
2057 N_("net rpc registry deletevalue\n"
2058 " Delete a registry value")
2064 N_("Save a registry file"),
2065 N_("net rpc registry save\n"
2066 " Save a registry file")
2072 N_("Dump a registry file"),
2073 N_("net rpc registry dump\n"
2074 " Dump a registry file")
2080 N_("Copy a registry file"),
2081 N_("net rpc registry copy\n"
2082 " Copy a registry file")
2088 N_("Get security descriptor"),
2089 N_("net rpc registry getsd\n"
2090 " Get security descriptior")
2094 rpc_registry_import,
2096 N_("Import .reg file"),
2097 N_("net rpc registry import\n"
2098 " Import .reg file")
2102 rpc_registry_export,
2104 N_("Export .reg file"),
2105 N_("net rpc registry export\n"
2106 " Export .reg file")
2108 {NULL, NULL, 0, NULL, NULL}
2110 return net_run_function(c, argc, argv, "net rpc registry", func);