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/>. */
22 #include "utils/net.h"
23 #include "utils/net_registry_util.h"
24 #include "registry/regfio.h"
25 #include "../librpc/gen_ndr/ndr_winreg_c.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "registry/reg_format.h"
28 #include "registry/reg_import.h"
30 #include "../libcli/security/display_sec.h"
33 /*******************************************************************
34 connect to a registry hive root (open a registry policy)
35 *******************************************************************/
37 static NTSTATUS dcerpc_winreg_Connect(struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
38 uint32_t reg_type, uint32_t access_mask,
39 struct policy_handle *reg_hnd, WERROR *werr)
41 ZERO_STRUCTP(reg_hnd);
45 case HKEY_CLASSES_ROOT:
46 return dcerpc_winreg_OpenHKCR(b, mem_ctx, NULL,
47 access_mask, reg_hnd, werr);
49 case HKEY_LOCAL_MACHINE:
50 return dcerpc_winreg_OpenHKLM(b, mem_ctx, NULL,
51 access_mask, reg_hnd, werr);
54 return dcerpc_winreg_OpenHKU(b, mem_ctx, NULL,
55 access_mask, reg_hnd, werr);
57 case HKEY_CURRENT_USER:
58 return dcerpc_winreg_OpenHKCU(b, mem_ctx, NULL,
59 access_mask, reg_hnd, werr);
61 case HKEY_PERFORMANCE_DATA:
62 return dcerpc_winreg_OpenHKPD(b, mem_ctx, NULL,
63 access_mask, reg_hnd, werr);
66 /* fall through to end of function */
70 return NT_STATUS_INVALID_PARAMETER;
73 static bool reg_hive_key(TALLOC_CTX *ctx, const char *fullname,
74 uint32 *reg_type, const char **key_name)
77 char *hivename = NULL;
78 char *tmp_keyname = NULL;
80 TALLOC_CTX *tmp_ctx = talloc_stackframe();
82 werr = split_hive_key(tmp_ctx, fullname, &hivename, &tmp_keyname);
83 if (!W_ERROR_IS_OK(werr)) {
87 *key_name = talloc_strdup(ctx, tmp_keyname);
88 if (*key_name == NULL) {
92 if (strequal(hivename, "HKLM") ||
93 strequal(hivename, "HKEY_LOCAL_MACHINE"))
95 (*reg_type) = HKEY_LOCAL_MACHINE;
96 } else if (strequal(hivename, "HKCR") ||
97 strequal(hivename, "HKEY_CLASSES_ROOT"))
99 (*reg_type) = HKEY_CLASSES_ROOT;
100 } else if (strequal(hivename, "HKU") ||
101 strequal(hivename, "HKEY_USERS"))
103 (*reg_type) = HKEY_USERS;
104 } else if (strequal(hivename, "HKCU") ||
105 strequal(hivename, "HKEY_CURRENT_USER"))
107 (*reg_type) = HKEY_CURRENT_USER;
108 } else if (strequal(hivename, "HKPD") ||
109 strequal(hivename, "HKEY_PERFORMANCE_DATA"))
111 (*reg_type) = HKEY_PERFORMANCE_DATA;
113 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
121 TALLOC_FREE(tmp_ctx);
125 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
126 struct rpc_pipe_client *pipe_hnd,
127 const char *name, uint32 access_mask,
128 struct policy_handle *hive_hnd,
129 struct policy_handle *key_hnd)
134 struct winreg_String key;
135 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
139 if (!reg_hive_key(mem_ctx, name, &hive, &key.name)) {
140 return NT_STATUS_INVALID_PARAMETER;
143 status = dcerpc_winreg_Connect(b, mem_ctx, hive, access_mask,
145 if (!(NT_STATUS_IS_OK(status))) {
148 if (!W_ERROR_IS_OK(werr)) {
149 return werror_to_ntstatus(werr);
152 status = dcerpc_winreg_OpenKey(b, mem_ctx, hive_hnd, key, 0,
153 access_mask, key_hnd, &werr);
154 if (!(NT_STATUS_IS_OK(status))) {
155 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &werr);
158 if (!(W_ERROR_IS_OK(werr))) {
160 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &_werr);
161 return werror_to_ntstatus(werr);
167 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
168 struct rpc_pipe_client *pipe_hnd,
169 struct policy_handle *key_hnd,
170 uint32 *pnum_keys, char ***pnames,
171 char ***pclasses, NTTIME ***pmodtimes)
176 uint32 num_subkeys, max_subkeylen, max_classlen;
177 uint32 num_values, max_valnamelen, max_valbufsize;
179 NTTIME last_changed_time;
181 struct winreg_String classname;
182 char **names, **classes;
184 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
186 if (!(mem_ctx = talloc_new(ctx))) {
187 return NT_STATUS_NO_MEMORY;
190 ZERO_STRUCT(classname);
191 status = dcerpc_winreg_QueryInfoKey(
192 b, mem_ctx, key_hnd, &classname, &num_subkeys,
193 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
194 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
196 if (!NT_STATUS_IS_OK(status)) {
199 if (!W_ERROR_IS_OK(werr)) {
200 status = werror_to_ntstatus(werr);
204 if (num_subkeys == 0) {
206 TALLOC_FREE(mem_ctx);
210 if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
211 (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
212 (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
214 status = NT_STATUS_NO_MEMORY;
218 for (i=0; i<num_subkeys; i++) {
220 struct winreg_StringBuf class_buf;
221 struct winreg_StringBuf name_buf;
226 class_buf.size = max_classlen+2;
230 name_buf.size = max_subkeylen+2;
232 ZERO_STRUCT(modtime);
234 status = dcerpc_winreg_EnumKey(b, mem_ctx, key_hnd,
235 i, &name_buf, &class_buf,
237 if (!NT_STATUS_IS_OK(status)) {
240 if (W_ERROR_EQUAL(werr,
241 WERR_NO_MORE_ITEMS) ) {
242 status = NT_STATUS_OK;
245 if (!W_ERROR_IS_OK(werr)) {
246 status = werror_to_ntstatus(werr);
252 if (class_buf.name &&
253 (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
254 status = NT_STATUS_NO_MEMORY;
258 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
259 status = NT_STATUS_NO_MEMORY;
263 if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
264 modtimes, &modtime, sizeof(modtime))))) {
265 status = NT_STATUS_NO_MEMORY;
270 *pnum_keys = num_subkeys;
273 *pnames = talloc_move(ctx, &names);
276 *pclasses = talloc_move(ctx, &classes);
279 *pmodtimes = talloc_move(ctx, &modtimes);
282 status = NT_STATUS_OK;
285 TALLOC_FREE(mem_ctx);
289 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
290 struct rpc_pipe_client *pipe_hnd,
291 struct policy_handle *key_hnd,
292 uint32 *pnum_values, char ***pvalnames,
293 struct registry_value ***pvalues)
298 uint32 num_subkeys, max_subkeylen, max_classlen;
299 uint32 num_values, max_valnamelen, max_valbufsize;
301 NTTIME last_changed_time;
303 struct winreg_String classname;
304 struct registry_value **values;
306 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
308 if (!(mem_ctx = talloc_new(ctx))) {
309 return NT_STATUS_NO_MEMORY;
312 ZERO_STRUCT(classname);
313 status = dcerpc_winreg_QueryInfoKey(
314 b, mem_ctx, key_hnd, &classname, &num_subkeys,
315 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
316 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
318 if (!NT_STATUS_IS_OK(status)) {
321 if (!W_ERROR_IS_OK(werr)) {
322 status = werror_to_ntstatus(werr);
326 if (num_values == 0) {
328 TALLOC_FREE(mem_ctx);
332 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
333 (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
335 status = NT_STATUS_NO_MEMORY;
339 for (i=0; i<num_values; i++) {
340 enum winreg_Type type = REG_NONE;
346 struct winreg_ValNameBuf name_buf;
351 name_buf.size = max_valnamelen + 2;
353 data_size = max_valbufsize;
354 data = (uint8 *)TALLOC(mem_ctx, data_size);
357 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
360 &value_length, &err);
361 if (!(NT_STATUS_IS_OK(status))) {
365 if ( W_ERROR_EQUAL(err,
366 WERR_NO_MORE_ITEMS) ) {
367 status = NT_STATUS_OK;
371 if (!W_ERROR_IS_OK(err)) {
372 status = werror_to_ntstatus(err);
376 if (name_buf.name == NULL) {
377 status = NT_STATUS_INVALID_PARAMETER;
381 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
382 status = NT_STATUS_NO_MEMORY;
386 values[i] = talloc_zero(values, struct registry_value);
387 if (values[i] == NULL) {
388 status = NT_STATUS_NO_MEMORY;
392 values[i]->type = type;
393 values[i]->data = data_blob_talloc(values[i], data, data_size);
396 *pnum_values = num_values;
399 *pvalnames = talloc_move(ctx, &names);
402 *pvalues = talloc_move(ctx, &values);
405 status = NT_STATUS_OK;
408 TALLOC_FREE(mem_ctx);
412 static NTSTATUS registry_enumvalues2(TALLOC_CTX *ctx,
413 struct rpc_pipe_client *pipe_hnd,
414 struct policy_handle *key_hnd,
415 uint32 *pnum_values, char ***pvalnames,
416 struct regval_blob ***pvalues)
421 uint32 num_subkeys, max_subkeylen, max_classlen;
422 uint32 num_values, max_valnamelen, max_valbufsize;
424 NTTIME last_changed_time;
426 struct winreg_String classname;
427 struct regval_blob **values;
429 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
431 if (!(mem_ctx = talloc_new(ctx))) {
432 return NT_STATUS_NO_MEMORY;
435 ZERO_STRUCT(classname);
436 status = dcerpc_winreg_QueryInfoKey(
437 b, mem_ctx, key_hnd, &classname, &num_subkeys,
438 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
439 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
441 if (!NT_STATUS_IS_OK(status)) {
444 if (!W_ERROR_IS_OK(werr)) {
445 status = werror_to_ntstatus(werr);
449 if (num_values == 0) {
451 TALLOC_FREE(mem_ctx);
455 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
456 (!(values = TALLOC_ARRAY(mem_ctx, struct regval_blob *,
458 status = NT_STATUS_NO_MEMORY;
462 for (i=0; i<num_values; i++) {
463 enum winreg_Type type = REG_NONE;
469 struct winreg_ValNameBuf name_buf;
474 name_buf.size = max_valnamelen + 2;
476 data_size = max_valbufsize;
477 data = (uint8 *)TALLOC(mem_ctx, data_size);
480 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
483 &value_length, &err);
484 if (!(NT_STATUS_IS_OK(status))) {
488 if ( W_ERROR_EQUAL(err, WERR_NO_MORE_ITEMS) ) {
489 status = NT_STATUS_OK;
493 if (!W_ERROR_IS_OK(err)) {
494 status = werror_to_ntstatus(err);
498 if (name_buf.name == NULL) {
499 status = NT_STATUS_INVALID_PARAMETER;
503 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
504 status = NT_STATUS_NO_MEMORY;
508 assert(value_length<=data_size); //???
510 values[i] = regval_compose(values,
515 status = NT_STATUS_NO_MEMORY;
520 *pnum_values = num_values;
523 *pvalnames = talloc_move(ctx, &names);
526 *pvalues = talloc_move(ctx, &values);
529 status = NT_STATUS_OK;
532 TALLOC_FREE(mem_ctx);
536 static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
537 struct dcerpc_binding_handle *b,
538 struct policy_handle *key_hnd,
540 struct KeySecurityData *sd,
543 return dcerpc_winreg_GetKeySecurity(b, mem_ctx, key_hnd,
548 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
549 struct rpc_pipe_client *pipe_hnd,
550 struct policy_handle *key_hnd,
552 const struct registry_value *value)
554 struct winreg_String name_string;
557 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
559 ZERO_STRUCT(name_string);
561 name_string.name = name;
562 result = dcerpc_winreg_SetValue(b, mem_ctx, key_hnd,
563 name_string, value->type,
564 value->data.data, value->data.length, &werr);
565 if (!NT_STATUS_IS_OK(result)) {
569 return werror_to_ntstatus(werr);
572 static NTSTATUS rpc_registry_setvalue_internal(struct net_context *c,
573 const struct dom_sid *domain_sid,
574 const char *domain_name,
575 struct cli_state *cli,
576 struct rpc_pipe_client *pipe_hnd,
581 struct policy_handle hive_hnd, key_hnd;
584 struct registry_value value;
585 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
587 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
588 SEC_FLAG_MAXIMUM_ALLOWED,
589 &hive_hnd, &key_hnd);
590 if (!NT_STATUS_IS_OK(status)) {
591 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
596 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
597 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
598 return NT_STATUS_NOT_IMPLEMENTED;
601 if (strequal(argv[2], "dword")) {
602 uint32_t v = strtoul(argv[3], NULL, 10);
603 value.type = REG_DWORD;
604 value.data = data_blob_talloc(mem_ctx, NULL, 4);
605 SIVAL(value.data.data, 0, v);
607 else if (strequal(argv[2], "sz")) {
609 if (!push_reg_sz(mem_ctx, &value.data, argv[3])) {
610 status = NT_STATUS_NO_MEMORY;
615 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
616 status = NT_STATUS_NOT_IMPLEMENTED;
620 status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
623 if (!NT_STATUS_IS_OK(status)) {
624 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
629 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
630 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
635 static int rpc_registry_setvalue(struct net_context *c, int argc,
638 if (argc < 4 || c->display_usage) {
639 d_fprintf(stderr, "%s\n%s",
641 _("net rpc registry setvalue <key> <valuename> "
642 "<type> [<val>]+\n"));
646 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
647 rpc_registry_setvalue_internal, argc, argv );
650 static NTSTATUS rpc_registry_deletevalue_internal(struct net_context *c,
651 const struct dom_sid *domain_sid,
652 const char *domain_name,
653 struct cli_state *cli,
654 struct rpc_pipe_client *pipe_hnd,
659 struct policy_handle hive_hnd, key_hnd;
662 struct winreg_String valuename;
663 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
665 ZERO_STRUCT(valuename);
667 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
668 SEC_FLAG_MAXIMUM_ALLOWED,
669 &hive_hnd, &key_hnd);
670 if (!NT_STATUS_IS_OK(status)) {
671 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
676 valuename.name = argv[1];
678 status = dcerpc_winreg_DeleteValue(b, mem_ctx, &key_hnd,
680 if (!NT_STATUS_IS_OK(status)) {
681 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
684 if (!W_ERROR_IS_OK(werr)) {
685 status = werror_to_ntstatus(werr);
686 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
690 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
691 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
696 static int rpc_registry_deletevalue(struct net_context *c, int argc,
699 if (argc != 2 || c->display_usage) {
700 d_fprintf(stderr, "%s\n%s",
702 _("net rpc registry deletevalue <key> <valuename>\n"));
706 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
707 rpc_registry_deletevalue_internal, argc, argv );
710 static NTSTATUS rpc_registry_getvalue_internal(struct net_context *c,
711 const struct dom_sid *domain_sid,
712 const char *domain_name,
713 struct cli_state *cli,
714 struct rpc_pipe_client *pipe_hnd,
720 struct policy_handle hive_hnd, key_hnd;
723 struct winreg_String valuename;
724 struct registry_value *value = NULL;
725 enum winreg_Type type = REG_NONE;
726 uint32_t data_size = 0;
727 uint32_t value_length = 0;
728 TALLOC_CTX *tmp_ctx = talloc_stackframe();
729 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
731 ZERO_STRUCT(valuename);
733 status = registry_openkey(tmp_ctx, pipe_hnd, argv[0],
734 SEC_FLAG_MAXIMUM_ALLOWED,
735 &hive_hnd, &key_hnd);
736 if (!NT_STATUS_IS_OK(status)) {
737 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
742 valuename.name = argv[1];
744 value = talloc_zero(tmp_ctx, struct registry_value);
746 return NT_STATUS_NO_MEMORY;
750 * call QueryValue once with data == NULL to get the
751 * needed memory size to be allocated, then allocate
752 * data buffer and call again.
754 status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
762 if (!NT_STATUS_IS_OK(status)) {
763 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
767 if (!W_ERROR_IS_OK(werr)) {
768 status = werror_to_ntstatus(werr);
769 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
774 value->data = data_blob_talloc(tmp_ctx, NULL, data_size);
776 status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
784 if (!NT_STATUS_IS_OK(status)) {
785 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
789 if (!W_ERROR_IS_OK(werr)) {
790 status = werror_to_ntstatus(werr);
791 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
799 print_registry_value(value, raw);
802 dcerpc_winreg_CloseKey(b, tmp_ctx, &key_hnd, &werr);
803 dcerpc_winreg_CloseKey(b, tmp_ctx, &hive_hnd, &werr);
805 TALLOC_FREE(tmp_ctx);
810 static NTSTATUS rpc_registry_getvalue_full(struct net_context *c,
811 const struct dom_sid *domain_sid,
812 const char *domain_name,
813 struct cli_state *cli,
814 struct rpc_pipe_client *pipe_hnd,
819 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
820 cli, pipe_hnd, mem_ctx, false,
824 static int rpc_registry_getvalue(struct net_context *c, int argc,
827 if (argc != 2 || c->display_usage) {
828 d_fprintf(stderr, "%s\n%s",
830 _("net rpc registry getvalue <key> <valuename>\n"));
834 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
835 rpc_registry_getvalue_full, argc, argv);
838 static NTSTATUS rpc_registry_getvalue_raw(struct net_context *c,
839 const struct dom_sid *domain_sid,
840 const char *domain_name,
841 struct cli_state *cli,
842 struct rpc_pipe_client *pipe_hnd,
847 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
848 cli, pipe_hnd, mem_ctx, true,
852 static int rpc_registry_getvalueraw(struct net_context *c, int argc,
855 if (argc != 2 || c->display_usage) {
856 d_fprintf(stderr, "%s\n%s",
858 _("net rpc registry getvalue <key> <valuename>\n"));
862 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
863 rpc_registry_getvalue_raw, argc, argv);
866 static NTSTATUS rpc_registry_createkey_internal(struct net_context *c,
867 const struct dom_sid *domain_sid,
868 const char *domain_name,
869 struct cli_state *cli,
870 struct rpc_pipe_client *pipe_hnd,
876 struct policy_handle hive_hnd, key_hnd;
877 struct winreg_String key, keyclass;
878 enum winreg_CreateAction action;
881 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
884 ZERO_STRUCT(keyclass);
886 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
887 return NT_STATUS_INVALID_PARAMETER;
890 status = dcerpc_winreg_Connect(b, mem_ctx, hive,
891 SEC_FLAG_MAXIMUM_ALLOWED,
893 if (!(NT_STATUS_IS_OK(status))) {
896 if (!W_ERROR_IS_OK(werr)) {
897 return werror_to_ntstatus(werr);
900 action = REG_ACTION_NONE;
903 status = dcerpc_winreg_CreateKey(b, mem_ctx, &hive_hnd, key,
904 keyclass, 0, REG_KEY_READ, NULL,
905 &key_hnd, &action, &werr);
906 if (!NT_STATUS_IS_OK(status)) {
907 d_fprintf(stderr, _("createkey returned %s\n"),
909 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
912 if (!W_ERROR_IS_OK(werr)) {
914 d_fprintf(stderr, _("createkey returned %s\n"),
916 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
917 return werror_to_ntstatus(werr);
921 case REG_ACTION_NONE:
922 d_printf(_("createkey did nothing -- huh?\n"));
924 case REG_CREATED_NEW_KEY:
925 d_printf(_("createkey created %s\n"), argv[0]);
927 case REG_OPENED_EXISTING_KEY:
928 d_printf(_("createkey opened existing %s\n"), argv[0]);
932 dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
933 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
938 static int rpc_registry_createkey(struct net_context *c, int argc,
941 if (argc != 1 || c->display_usage) {
942 d_fprintf(stderr, "%s\n%s",
944 _("net rpc registry createkey <key>\n"));
948 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
949 rpc_registry_createkey_internal, argc, argv );
952 static NTSTATUS rpc_registry_deletekey_internal(struct net_context *c,
953 const struct dom_sid *domain_sid,
954 const char *domain_name,
955 struct cli_state *cli,
956 struct rpc_pipe_client *pipe_hnd,
962 struct policy_handle hive_hnd;
963 struct winreg_String key;
966 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
970 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
971 return NT_STATUS_INVALID_PARAMETER;
974 status = dcerpc_winreg_Connect(b, mem_ctx, hive,
975 SEC_FLAG_MAXIMUM_ALLOWED,
977 if (!(NT_STATUS_IS_OK(status))) {
980 if (!W_ERROR_IS_OK(werr)) {
981 return werror_to_ntstatus(werr);
984 status = dcerpc_winreg_DeleteKey(b, mem_ctx, &hive_hnd, key, &werr);
985 if (is_valid_policy_hnd(&hive_hnd)) {
987 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
990 if (!NT_STATUS_IS_OK(status)) {
991 d_fprintf(stderr, _("deletekey returned %s\n"),
996 if (!W_ERROR_IS_OK(werr)) {
997 d_fprintf(stderr, _("deletekey returned %s\n"),
999 return werror_to_ntstatus(werr);
1005 static int rpc_registry_deletekey(struct net_context *c, int argc, const char **argv )
1007 if (argc != 1 || c->display_usage) {
1008 d_fprintf(stderr, "%s\n%s",
1010 _("net rpc registry deletekey <key>\n"));
1014 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1015 rpc_registry_deletekey_internal, argc, argv );
1018 /********************************************************************
1019 ********************************************************************/
1021 static NTSTATUS rpc_registry_enumerate_internal(struct net_context *c,
1022 const struct dom_sid *domain_sid,
1023 const char *domain_name,
1024 struct cli_state *cli,
1025 struct rpc_pipe_client *pipe_hnd,
1026 TALLOC_CTX *mem_ctx,
1030 struct policy_handle pol_hive, pol_key;
1033 uint32 num_subkeys = 0;
1034 uint32 num_values = 0;
1035 char **names = NULL, **classes = NULL;
1036 NTTIME **modtimes = NULL;
1038 struct registry_value **values = NULL;
1039 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1041 if (argc != 1 || c->display_usage) {
1044 _("net rpc registry enumerate <path>\n"));
1045 d_printf("%s net rpc registry enumerate "
1046 "'HKLM\\Software\\Samba'\n", _("Example:"));
1047 return NT_STATUS_INVALID_PARAMETER;
1050 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1051 &pol_hive, &pol_key);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1058 status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
1059 &names, &classes, &modtimes);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1066 for (i=0; i<num_subkeys; i++) {
1067 print_registry_key(names[i], modtimes[i]);
1070 status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
1072 if (!NT_STATUS_IS_OK(status)) {
1073 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1078 for (i=0; i<num_values; i++) {
1079 print_registry_value_with_name(names[i], values[i]);
1082 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1083 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1088 /********************************************************************
1089 ********************************************************************/
1091 static int rpc_registry_enumerate(struct net_context *c, int argc,
1094 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1095 rpc_registry_enumerate_internal, argc, argv );
1098 /********************************************************************
1099 ********************************************************************/
1101 static NTSTATUS rpc_registry_save_internal(struct net_context *c,
1102 const struct dom_sid *domain_sid,
1103 const char *domain_name,
1104 struct cli_state *cli,
1105 struct rpc_pipe_client *pipe_hnd,
1106 TALLOC_CTX *mem_ctx,
1110 WERROR result = WERR_GENERAL_FAILURE;
1111 struct policy_handle pol_hive, pol_key;
1112 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1113 struct winreg_String filename;
1114 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1116 if (argc != 2 || c->display_usage) {
1119 _("net rpc registry backup <path> <file> \n"));
1120 return NT_STATUS_INVALID_PARAMETER;
1123 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
1124 &pol_hive, &pol_key);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1131 filename.name = argv[1];
1132 status = dcerpc_winreg_SaveKey(b, mem_ctx, &pol_key, &filename, NULL, &result);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1135 cli->desthost, argv[1]);
1137 if (!W_ERROR_IS_OK(result)) {
1138 status = werror_to_ntstatus(result);
1139 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1140 cli->desthost, argv[1]);
1145 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &result);
1146 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &result);
1151 /********************************************************************
1152 ********************************************************************/
1154 static int rpc_registry_save(struct net_context *c, int argc, const char **argv )
1156 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1157 rpc_registry_save_internal, argc, argv );
1161 /********************************************************************
1162 ********************************************************************/
1164 static void dump_values( REGF_NK_REC *nk )
1167 const char *data_str = NULL;
1168 uint32 data_size, data;
1174 for ( i=0; i<nk->num_values; i++ ) {
1175 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
1176 d_printf( "(%s) ", str_regtype( nk->values[i].type ) );
1178 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
1179 switch ( nk->values[i].type ) {
1181 blob = data_blob_const(nk->values[i].data, data_size);
1182 pull_reg_sz(talloc_tos(), &blob, &data_str);
1186 d_printf( "%s", data_str );
1190 for ( j=0; j<data_size; j++ ) {
1191 d_printf( "%c", nk->values[i].data[j] );
1195 data = IVAL( nk->values[i].data, 0 );
1196 d_printf("0x%x", data );
1199 for ( j=0; j<data_size; j++ ) {
1200 d_printf( "%x", nk->values[i].data[j] );
1204 d_printf(_("unknown"));
1213 /********************************************************************
1214 ********************************************************************/
1216 static bool dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
1220 /* depth first dump of the registry tree */
1222 while ( (key = regfio_fetch_subkey( file, nk )) ) {
1224 if (asprintf(®path, "%s\\%s", parent, key->keyname) < 0) {
1227 d_printf("[%s]\n", regpath );
1230 dump_registry_tree( file, key, regpath );
1237 /********************************************************************
1238 ********************************************************************/
1240 static bool write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
1241 REGF_NK_REC *parent, REGF_FILE *outfile,
1242 const char *parentpath )
1244 REGF_NK_REC *key, *subkey;
1245 struct regval_ctr *values = NULL;
1246 struct regsubkey_ctr *subkeys = NULL;
1251 werr = regsubkey_ctr_init(infile->mem_ctx, &subkeys);
1252 if (!W_ERROR_IS_OK(werr)) {
1253 DEBUG(0, ("write_registry_tree: regsubkey_ctr_init failed: "
1254 "%s\n", win_errstr(werr)));
1258 werr = regval_ctr_init(subkeys, &values);
1259 if (!W_ERROR_IS_OK(werr)) {
1260 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
1261 TALLOC_FREE(subkeys);
1265 /* copy values into the struct regval_ctr */
1267 for ( i=0; i<nk->num_values; i++ ) {
1268 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
1269 nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
1272 /* copy subkeys into the struct regsubkey_ctr */
1274 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1275 regsubkey_ctr_addkey( subkeys, subkey->keyname );
1278 key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
1280 /* write each one of the subkeys out */
1282 path = talloc_asprintf(subkeys,
1288 TALLOC_FREE(subkeys);
1292 nk->subkey_index = 0;
1293 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1294 write_registry_tree( infile, subkey, key, outfile, path );
1297 d_printf("[%s]\n", path );
1298 TALLOC_FREE(subkeys);
1303 /********************************************************************
1304 ********************************************************************/
1306 static int rpc_registry_dump(struct net_context *c, int argc, const char **argv)
1308 REGF_FILE *registry;
1311 if (argc != 1 || c->display_usage) {
1314 _("net rpc registry dump <file> \n"));
1318 d_printf(_("Opening %s...."), argv[0]);
1319 if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
1320 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1323 d_printf(_("ok\n"));
1325 /* get the root of the registry file */
1327 if ((nk = regfio_rootkey( registry )) == NULL) {
1328 d_fprintf(stderr, _("Could not get rootkey\n"));
1329 regfio_close( registry );
1332 d_printf("[%s]\n", nk->keyname);
1336 dump_registry_tree( registry, nk, nk->keyname );
1339 talloc_report_full( registry->mem_ctx, stderr );
1341 d_printf(_("Closing registry..."));
1342 regfio_close( registry );
1343 d_printf(_("ok\n"));
1348 /********************************************************************
1349 ********************************************************************/
1351 static int rpc_registry_copy(struct net_context *c, int argc, const char **argv )
1353 REGF_FILE *infile = NULL, *outfile = NULL;
1357 if (argc != 2 || c->display_usage) {
1360 _("net rpc registry copy <srcfile> <newfile>\n"));
1364 d_printf(_("Opening %s...."), argv[0]);
1365 if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
1366 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1369 d_printf(_("ok\n"));
1371 d_printf(_("Opening %s...."), argv[1]);
1372 if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC),
1373 (S_IRUSR|S_IWUSR) )) ) {
1374 d_fprintf(stderr, _("Failed to open %s for writing\n"),argv[1]);
1377 d_printf(_("ok\n"));
1379 /* get the root of the registry file */
1381 if ((nk = regfio_rootkey( infile )) == NULL) {
1382 d_fprintf(stderr, _("Could not get rootkey\n"));
1385 d_printf(_("RootKey: [%s]\n"), nk->keyname);
1387 write_registry_tree( infile, nk, NULL, outfile, "" );
1393 d_printf(_("Closing %s..."), argv[1]);
1395 regfio_close( outfile );
1397 d_printf(_("ok\n"));
1399 d_printf(_("Closing %s..."), argv[0]);
1401 regfio_close( infile );
1403 d_printf(_("ok\n"));
1408 /********************************************************************
1409 ********************************************************************/
1411 static NTSTATUS rpc_registry_getsd_internal(struct net_context *c,
1412 const struct dom_sid *domain_sid,
1413 const char *domain_name,
1414 struct cli_state *cli,
1415 struct rpc_pipe_client *pipe_hnd,
1416 TALLOC_CTX *mem_ctx,
1420 struct policy_handle pol_hive, pol_key;
1423 enum ndr_err_code ndr_err;
1424 struct KeySecurityData *sd = NULL;
1427 struct security_descriptor sec_desc;
1428 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED |
1429 SEC_FLAG_SYSTEM_SECURITY;
1430 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1432 if (argc <1 || argc > 2 || c->display_usage) {
1435 _("net rpc registry getsd <path> <secinfo>\n"));
1436 d_printf("%s net rpc registry getsd "
1437 "'HKLM\\Software\\Samba'\n", _("Example:"));
1438 return NT_STATUS_INVALID_PARAMETER;
1441 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
1443 &pol_hive, &pol_key);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1450 sd = TALLOC_ZERO_P(mem_ctx, struct KeySecurityData);
1452 status = NT_STATUS_NO_MEMORY;
1459 sscanf(argv[1], "%x", &sec_info);
1461 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
1464 status = registry_getsd(mem_ctx, b, &pol_key, sec_info, sd, &werr);
1465 if (!NT_STATUS_IS_OK(status)) {
1466 d_fprintf(stderr, _("getting sd failed: %s\n"),
1470 if (!W_ERROR_IS_OK(werr)) {
1471 status = werror_to_ntstatus(werr);
1472 d_fprintf(stderr, _("getting sd failed: %s\n"),
1477 blob.data = sd->data;
1478 blob.length = sd->size;
1480 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &sec_desc,
1481 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1482 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1483 status = ndr_map_error2ntstatus(ndr_err);
1486 status = NT_STATUS_OK;
1488 display_sec_desc(&sec_desc);
1491 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1492 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1498 static int rpc_registry_getsd(struct net_context *c, int argc, const char **argv)
1500 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1501 rpc_registry_getsd_internal, argc, argv);
1504 /********************************************************************
1505 ********************************************************************/
1507 * @defgroup net_rpc_registry net rpc registry
1511 * @defgroup net_rpc_registry_export Export
1512 * @ingroup net_rpc_registry
1516 static NTSTATUS registry_export(struct rpc_pipe_client* pipe_hnd,
1518 struct policy_handle* key_hnd,
1519 struct reg_format* f,
1520 const char* parentfullname,
1524 uint32 num_subkeys = 0;
1525 uint32 num_values = 0;
1526 char **names = NULL, **classes = NULL;
1527 NTTIME **modtimes = NULL;
1528 struct regval_blob **values = NULL;
1530 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1532 TALLOC_CTX* mem_ctx = talloc_new(ctx);
1535 const char* fullname = name
1536 ? talloc_asprintf(mem_ctx, "%s\\%s", parentfullname, name)
1538 reg_format_key(f, &fullname, 1, false);
1540 status = registry_enumvalues2(mem_ctx, pipe_hnd, key_hnd, &num_values,
1542 if (!NT_STATUS_IS_OK(status)) {
1543 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1548 for (i=0; i<num_values; i++) {
1549 reg_format_regval_blob(f, names[i], values[i]);
1553 status = registry_enumkeys(mem_ctx, pipe_hnd, key_hnd, &num_subkeys,
1554 &names, &classes, &modtimes);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1561 for (i=0; i<num_subkeys; i++) {
1562 struct policy_handle subkey_hnd;
1563 struct winreg_String key;
1566 /* key.name = talloc_strdup(mem_ctx, names[i]); ??? */
1567 key.name = names[i];
1569 status = dcerpc_winreg_OpenKey(b, mem_ctx, key_hnd, key,
1571 &subkey_hnd, &werr);
1572 if (!NT_STATUS_IS_OK(status)) {
1574 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1575 names[i], nt_errstr(status));
1578 if (!W_ERROR_IS_OK(werr)) {
1579 status = werror_to_ntstatus(werr);
1581 _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1582 names[i], win_errstr(werr));
1586 status = registry_export(pipe_hnd, mem_ctx, &subkey_hnd,
1587 f, fullname, names[i]);
1588 if (!(NT_STATUS_IS_OK(status))) {
1590 _("export key failed: %s %s\n"),
1591 names[i], nt_errstr(status));
1593 dcerpc_winreg_CloseKey(b, mem_ctx,
1594 &subkey_hnd, &werr);
1597 talloc_free(mem_ctx);
1601 static NTSTATUS rpc_registry_export_internal(struct net_context *c,
1602 const struct dom_sid *domain_sid,
1603 const char *domain_name,
1604 struct cli_state *cli,
1605 struct rpc_pipe_client *pipe_hnd,
1606 TALLOC_CTX *mem_ctx,
1610 struct policy_handle pol_hive, pol_key;
1613 struct reg_format* f;
1614 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1616 if (argc < 2 || argc > 3 || c->display_usage) {
1619 _("net rpc registry export <path> <file> [opt]\n"));
1620 d_printf("%s net rpc registry export "
1621 "'HKLM\\Software\\Samba' samba.reg\n", _("Example:"));
1622 return NT_STATUS_INVALID_PARAMETER;
1625 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1626 &pol_hive, &pol_key);
1627 if (!NT_STATUS_IS_OK(status)) {
1628 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1633 f = reg_format_file(mem_ctx, argv[1], (argc > 2) ? argv[2] : NULL);
1635 d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
1636 return map_nt_error_from_unix(errno);
1639 status = registry_export(pipe_hnd, mem_ctx, &pol_key,
1641 if (!NT_STATUS_IS_OK(status))
1644 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1645 dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1649 /********************************************************************
1650 ********************************************************************/
1652 static int rpc_registry_export(struct net_context *c, int argc,
1655 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1656 rpc_registry_export_internal, argc, argv );
1661 /********************************************************************
1662 ********************************************************************/
1665 * @defgroup net_rpc_registry_import Import
1666 * @ingroup net_rpc_registry
1671 struct rpc_pipe_client *pipe_hnd;
1672 TALLOC_CTX *mem_ctx;
1675 static WERROR import_create_key(struct import_ctx* ctx,
1676 struct policy_handle* parent, const char* name,
1677 void** pkey, bool* existing)
1681 void* mem_ctx = talloc_new(ctx->mem_ctx);
1683 struct policy_handle* key = NULL;
1684 struct policy_handle hive;
1685 struct winreg_String keyclass, keyname;
1686 enum winreg_CreateAction action = REG_ACTION_NONE;
1687 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1689 ZERO_STRUCT(keyname);
1690 keyname.name = name;
1692 if (parent == NULL) {
1693 uint32 hive_idx = 0;
1694 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1699 status = dcerpc_winreg_Connect(b, mem_ctx,
1700 hive_idx, SEC_FLAG_MAXIMUM_ALLOWED,
1702 if (!NT_STATUS_IS_OK(status)) {
1703 werr = ntstatus_to_werror(status);
1704 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1708 if (!W_ERROR_IS_OK(werr)) {
1709 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1717 key = talloc_zero(mem_ctx, struct policy_handle);
1723 ZERO_STRUCT(keyclass);
1726 status = dcerpc_winreg_CreateKey(b, mem_ctx,
1728 keyclass, 0, REG_KEY_READ, NULL,
1729 key, &action, &werr);
1730 if (!NT_STATUS_IS_OK(status)) {
1731 werr = ntstatus_to_werror(status);
1732 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1736 if (!W_ERROR_IS_OK(werr)) {
1737 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1743 case REG_CREATED_NEW_KEY:
1744 d_printf(_("createkey created %s\n"), name);
1745 if (existing != NULL)
1749 case REG_OPENED_EXISTING_KEY:
1750 d_printf(_("createkey opened existing %s\n"), name);
1751 if (existing != NULL)
1755 case REG_ACTION_NONE:
1756 d_printf(_("createkey did nothing -- huh?\n"));
1757 werr = WERR_CREATE_FAILED;
1764 if ( parent == &hive ) {
1766 dcerpc_winreg_CloseKey(b, mem_ctx,
1771 *pkey = talloc_steal(ctx->mem_ctx, key);
1774 talloc_free(mem_ctx);
1778 static WERROR import_delete_key(struct import_ctx* ctx,
1779 struct policy_handle* parent, const char* name)
1783 void* mem_ctx = talloc_new(ctx->mem_ctx);
1784 struct winreg_String keyname = { 0, };
1785 struct policy_handle hive;
1786 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1788 keyname.name = name;
1790 if (parent == NULL) {
1792 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1797 status = dcerpc_winreg_Connect(b, mem_ctx, hive_idx,
1798 SEC_FLAG_MAXIMUM_ALLOWED, &hive,
1800 if (!NT_STATUS_IS_OK(status)) {
1801 werr = ntstatus_to_werror(status);
1802 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1806 if (!W_ERROR_IS_OK(werr)) {
1807 d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1815 status = dcerpc_winreg_DeleteKey(b, mem_ctx, parent,
1817 if (!NT_STATUS_IS_OK(status)) {
1818 werr = ntstatus_to_werror(status);
1819 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1823 if (!W_ERROR_IS_OK(werr)) {
1824 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1830 if ( parent == &hive ) {
1832 dcerpc_winreg_CloseKey(b, mem_ctx, parent, &_result);
1835 talloc_free(mem_ctx);
1839 static WERROR import_close_key(struct import_ctx* ctx,
1840 struct policy_handle* key)
1844 void* mem_ctx = talloc_new(ctx->mem_ctx);
1845 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1847 status = dcerpc_winreg_CloseKey(b, mem_ctx, key, &werr);
1848 if (!NT_STATUS_IS_OK(status)) {
1849 werr = ntstatus_to_werror(status);
1850 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1854 if (!W_ERROR_IS_OK(werr)) {
1855 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1860 werr = (talloc_free(key) == 0) ? WERR_OK : WERR_GENERAL_FAILURE;
1862 talloc_free(mem_ctx);
1866 static WERROR import_create_val(struct import_ctx* ctx,
1867 struct policy_handle* parent, const char* name,
1868 uint32_t type, const uint8_t* val, uint32_t len)
1872 void* mem_ctx = talloc_new(ctx->mem_ctx);
1873 struct winreg_String valuename;
1874 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1876 if (parent == NULL) {
1877 return WERR_INVALID_PARAM;
1880 ZERO_STRUCT(valuename);
1881 valuename.name = name;
1883 status = dcerpc_winreg_SetValue(b, mem_ctx, parent,
1885 (uint8_t *)discard_const(val), len, &werr);
1886 if (!NT_STATUS_IS_OK(status)) {
1887 werr = ntstatus_to_werror(status);
1888 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1892 if (!W_ERROR_IS_OK(werr)) {
1893 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1899 talloc_free(mem_ctx);
1903 static WERROR import_delete_val(struct import_ctx* ctx,
1904 struct policy_handle* parent, const char* name)
1908 void* mem_ctx = talloc_new(ctx->mem_ctx);
1909 struct winreg_String valuename;
1910 struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1912 if (parent == NULL) {
1913 return WERR_INVALID_PARAM;
1916 ZERO_STRUCT(valuename);
1917 valuename.name = name;
1919 status = dcerpc_winreg_DeleteValue(b, mem_ctx,
1920 parent, valuename, &werr);
1922 if (!NT_STATUS_IS_OK(status)) {
1923 werr = ntstatus_to_werror(status);
1924 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1928 if (!NT_STATUS_IS_OK(status)) {
1929 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1935 talloc_free(mem_ctx);
1941 static NTSTATUS rpc_registry_import_internal(struct net_context *c,
1942 const struct dom_sid *domain_sid,
1943 const char *domain_name,
1944 struct cli_state *cli,
1945 struct rpc_pipe_client *pipe_hnd,
1946 TALLOC_CTX *mem_ctx,
1950 struct import_ctx import_ctx;
1952 struct reg_import_callback import_callback = {
1954 .closekey = (reg_import_callback_closekey_t)&import_close_key,
1955 .createkey = (reg_import_callback_createkey_t)&import_create_key,
1956 .deletekey = (reg_import_callback_deletekey_t)&import_delete_key,
1957 .deleteval = (reg_import_callback_deleteval_t)&import_delete_val,
1958 .setval.blob = (reg_import_callback_setval_blob_t)&import_create_val,
1959 .setval_type = BLOB,
1964 if (argc < 1 || argc > 2 || c->display_usage) {
1967 _("net rpc registry import <file> [options]\n"));
1968 d_printf("%s net rpc registry export "
1969 "samba.reg enc=CP1252,flags=0\n", _("Example:"));
1970 return NT_STATUS_INVALID_PARAMETER;
1972 ZERO_STRUCT(import_ctx);
1973 import_ctx.pipe_hnd = pipe_hnd;
1974 import_ctx.mem_ctx = mem_ctx;
1975 ret = reg_parse_file(argv[0],
1976 reg_import_adapter(import_ctx.mem_ctx,
1979 (argc > 1) ? argv[1] : NULL
1982 return ret==0 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1985 /********************************************************************
1986 ********************************************************************/
1988 static int rpc_registry_import(struct net_context *c, int argc,
1991 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1992 rpc_registry_import_internal, argc, argv );
1996 /********************************************************************
1997 ********************************************************************/
1999 int net_rpc_registry(struct net_context *c, int argc, const char **argv)
2001 struct functable func[] = {
2004 rpc_registry_enumerate,
2006 N_("Enumerate registry keys and values"),
2007 N_("net rpc registry enumerate\n"
2008 " Enumerate registry keys and values")
2012 rpc_registry_createkey,
2014 N_("Create a new registry key"),
2015 N_("net rpc registry createkey\n"
2016 " Create a new registry key")
2020 rpc_registry_deletekey,
2022 N_("Delete a registry key"),
2023 N_("net rpc registry deletekey\n"
2024 " Delete a registry key")
2028 rpc_registry_getvalue,
2030 N_("Print a registry value"),
2031 N_("net rpc registry getvalue\n"
2032 " Print a registry value")
2036 rpc_registry_getvalueraw,
2038 N_("Print a registry value"),
2039 N_("net rpc registry getvalueraw\n"
2040 " Print a registry value (raw version)")
2044 rpc_registry_setvalue,
2046 N_("Set a new registry value"),
2047 N_("net rpc registry setvalue\n"
2048 " Set a new registry value")
2052 rpc_registry_deletevalue,
2054 N_("Delete a registry value"),
2055 N_("net rpc registry deletevalue\n"
2056 " Delete a registry value")
2062 N_("Save a registry file"),
2063 N_("net rpc registry save\n"
2064 " Save a registry file")
2070 N_("Dump a registry file"),
2071 N_("net rpc registry dump\n"
2072 " Dump a registry file")
2078 N_("Copy a registry file"),
2079 N_("net rpc registry copy\n"
2080 " Copy a registry file")
2086 N_("Get security descriptor"),
2087 N_("net rpc registry getsd\n"
2088 " Get security descriptior")
2092 rpc_registry_import,
2094 N_("Import .reg file"),
2095 N_("net rpc registry import\n"
2096 " Import .reg file")
2100 rpc_registry_export,
2102 N_("Export .reg file"),
2103 N_("net rpc registry export\n"
2104 " Export .reg file")
2106 {NULL, NULL, 0, NULL, NULL}
2108 return net_run_function(c, argc, argv, "net rpc registry", func);