2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) Gerald (Jerry) Carter 2005-2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "utils/net.h"
22 #include "utils/net_registry_util.h"
24 #include "reg_objects.h"
25 #include "registry/reg_format.h"
26 #include "registry/reg_import.h"
29 static bool reg_hive_key(TALLOC_CTX *ctx, const char *fullname,
30 uint32 *reg_type, const char **key_name)
33 char *hivename = NULL;
34 char *tmp_keyname = NULL;
36 TALLOC_CTX *tmp_ctx = talloc_stackframe();
38 werr = split_hive_key(tmp_ctx, fullname, &hivename, &tmp_keyname);
39 if (!W_ERROR_IS_OK(werr)) {
43 *key_name = talloc_strdup(ctx, tmp_keyname);
44 if (*key_name == NULL) {
48 if (strequal(hivename, "HKLM") ||
49 strequal(hivename, "HKEY_LOCAL_MACHINE"))
51 (*reg_type) = HKEY_LOCAL_MACHINE;
52 } else if (strequal(hivename, "HKCR") ||
53 strequal(hivename, "HKEY_CLASSES_ROOT"))
55 (*reg_type) = HKEY_CLASSES_ROOT;
56 } else if (strequal(hivename, "HKU") ||
57 strequal(hivename, "HKEY_USERS"))
59 (*reg_type) = HKEY_USERS;
60 } else if (strequal(hivename, "HKCU") ||
61 strequal(hivename, "HKEY_CURRENT_USER"))
63 (*reg_type) = HKEY_CURRENT_USER;
64 } else if (strequal(hivename, "HKPD") ||
65 strequal(hivename, "HKEY_PERFORMANCE_DATA"))
67 (*reg_type) = HKEY_PERFORMANCE_DATA;
69 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
81 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
82 struct rpc_pipe_client *pipe_hnd,
83 const char *name, uint32 access_mask,
84 struct policy_handle *hive_hnd,
85 struct policy_handle *key_hnd)
89 struct winreg_String key;
93 if (!reg_hive_key(mem_ctx, name, &hive, &key.name)) {
94 return NT_STATUS_INVALID_PARAMETER;
97 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive, access_mask,
99 if (!(NT_STATUS_IS_OK(status))) {
103 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, hive_hnd, key, 0,
104 access_mask, key_hnd, NULL);
105 if (!(NT_STATUS_IS_OK(status))) {
106 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, hive_hnd, NULL);
113 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
114 struct rpc_pipe_client *pipe_hnd,
115 struct policy_handle *key_hnd,
116 uint32 *pnum_keys, char ***pnames,
117 char ***pclasses, NTTIME ***pmodtimes)
121 uint32 num_subkeys, max_subkeylen, max_classlen;
122 uint32 num_values, max_valnamelen, max_valbufsize;
124 NTTIME last_changed_time;
126 struct winreg_String classname;
127 char **names, **classes;
130 if (!(mem_ctx = talloc_new(ctx))) {
131 return NT_STATUS_NO_MEMORY;
134 ZERO_STRUCT(classname);
135 status = rpccli_winreg_QueryInfoKey(
136 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
137 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
138 &max_valbufsize, &secdescsize, &last_changed_time, NULL );
140 if (!NT_STATUS_IS_OK(status)) {
144 if (num_subkeys == 0) {
146 TALLOC_FREE(mem_ctx);
150 if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
151 (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
152 (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
154 status = NT_STATUS_NO_MEMORY;
158 for (i=0; i<num_subkeys; i++) {
160 struct winreg_StringBuf class_buf;
161 struct winreg_StringBuf name_buf;
167 class_buf.size = max_classlen+2;
171 name_buf.size = max_subkeylen+2;
173 ZERO_STRUCT(modtime);
175 status = rpccli_winreg_EnumKey(pipe_hnd, mem_ctx, key_hnd,
176 i, &name_buf, &class_buf,
179 if (W_ERROR_EQUAL(werr,
180 WERR_NO_MORE_ITEMS) ) {
181 status = NT_STATUS_OK;
184 if (!NT_STATUS_IS_OK(status)) {
190 if (class_buf.name &&
191 (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
192 status = NT_STATUS_NO_MEMORY;
196 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
197 status = NT_STATUS_NO_MEMORY;
201 if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
202 modtimes, &modtime, sizeof(modtime))))) {
203 status = NT_STATUS_NO_MEMORY;
208 *pnum_keys = num_subkeys;
211 *pnames = talloc_move(ctx, &names);
214 *pclasses = talloc_move(ctx, &classes);
217 *pmodtimes = talloc_move(ctx, &modtimes);
220 status = NT_STATUS_OK;
223 TALLOC_FREE(mem_ctx);
227 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
228 struct rpc_pipe_client *pipe_hnd,
229 struct policy_handle *key_hnd,
230 uint32 *pnum_values, char ***pvalnames,
231 struct registry_value ***pvalues)
235 uint32 num_subkeys, max_subkeylen, max_classlen;
236 uint32 num_values, max_valnamelen, max_valbufsize;
238 NTTIME last_changed_time;
240 struct winreg_String classname;
241 struct registry_value **values;
244 if (!(mem_ctx = talloc_new(ctx))) {
245 return NT_STATUS_NO_MEMORY;
248 ZERO_STRUCT(classname);
249 status = rpccli_winreg_QueryInfoKey(
250 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
251 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
252 &max_valbufsize, &secdescsize, &last_changed_time, NULL );
254 if (!NT_STATUS_IS_OK(status)) {
258 if (num_values == 0) {
260 TALLOC_FREE(mem_ctx);
264 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
265 (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
267 status = NT_STATUS_NO_MEMORY;
271 for (i=0; i<num_values; i++) {
272 enum winreg_Type type = REG_NONE;
278 struct winreg_ValNameBuf name_buf;
283 name_buf.size = max_valnamelen + 2;
285 data_size = max_valbufsize;
286 data = (uint8 *)TALLOC(mem_ctx, data_size);
289 status = rpccli_winreg_EnumValue(pipe_hnd, mem_ctx, key_hnd,
292 &value_length, &err);
294 if ( W_ERROR_EQUAL(err,
295 WERR_NO_MORE_ITEMS) ) {
296 status = NT_STATUS_OK;
300 if (!(NT_STATUS_IS_OK(status))) {
304 if (name_buf.name == NULL) {
305 status = NT_STATUS_INVALID_PARAMETER;
309 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
310 status = NT_STATUS_NO_MEMORY;
314 err = registry_pull_value(values, &values[i], type, data,
315 data_size, value_length);
316 if (!W_ERROR_IS_OK(err)) {
317 status = werror_to_ntstatus(err);
322 *pnum_values = num_values;
325 *pvalnames = talloc_move(ctx, &names);
328 *pvalues = talloc_move(ctx, &values);
331 status = NT_STATUS_OK;
334 TALLOC_FREE(mem_ctx);
338 static NTSTATUS registry_enumvalues2(TALLOC_CTX *ctx,
339 struct rpc_pipe_client *pipe_hnd,
340 struct policy_handle *key_hnd,
341 uint32 *pnum_values, char ***pvalnames,
342 struct regval_blob ***pvalues)
346 uint32 num_subkeys, max_subkeylen, max_classlen;
347 uint32 num_values, max_valnamelen, max_valbufsize;
349 NTTIME last_changed_time;
351 struct winreg_String classname;
352 struct regval_blob **values;
355 if (!(mem_ctx = talloc_new(ctx))) {
356 return NT_STATUS_NO_MEMORY;
359 ZERO_STRUCT(classname);
360 status = rpccli_winreg_QueryInfoKey(
361 pipe_hnd, mem_ctx, key_hnd, &classname, &num_subkeys,
362 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
363 &max_valbufsize, &secdescsize, &last_changed_time, NULL );
365 if (!NT_STATUS_IS_OK(status)) {
369 if (num_values == 0) {
371 TALLOC_FREE(mem_ctx);
375 if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
376 (!(values = TALLOC_ARRAY(mem_ctx, struct regval_blob *,
378 status = NT_STATUS_NO_MEMORY;
382 for (i=0; i<num_values; i++) {
383 enum winreg_Type type = REG_NONE;
389 struct winreg_ValNameBuf name_buf;
394 name_buf.size = max_valnamelen + 2;
396 data_size = max_valbufsize;
397 data = (uint8 *)TALLOC(mem_ctx, data_size);
400 status = rpccli_winreg_EnumValue(pipe_hnd, mem_ctx, key_hnd,
403 &value_length, &err);
405 if ( W_ERROR_EQUAL(err, WERR_NO_MORE_ITEMS) ) {
406 status = NT_STATUS_OK;
410 if (!(NT_STATUS_IS_OK(status))) {
414 if (name_buf.name == NULL) {
415 status = NT_STATUS_INVALID_PARAMETER;
419 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
420 status = NT_STATUS_NO_MEMORY;
424 values[i] = regval_compose(values,
427 (char*)data, value_length);
429 status = NT_STATUS_NO_MEMORY;
434 *pnum_values = num_values;
437 *pvalnames = talloc_move(ctx, &names);
440 *pvalues = talloc_move(ctx, &values);
443 status = NT_STATUS_OK;
446 TALLOC_FREE(mem_ctx);
450 static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
451 struct rpc_pipe_client *pipe_hnd,
452 struct policy_handle *key_hnd,
454 struct KeySecurityData *sd)
456 return rpccli_winreg_GetKeySecurity(pipe_hnd, mem_ctx, key_hnd,
461 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
462 struct rpc_pipe_client *pipe_hnd,
463 struct policy_handle *key_hnd,
465 const struct registry_value *value)
467 struct winreg_String name_string;
472 err = registry_push_value(mem_ctx, value, &blob);
473 if (!W_ERROR_IS_OK(err)) {
474 return werror_to_ntstatus(err);
477 ZERO_STRUCT(name_string);
479 name_string.name = name;
480 result = rpccli_winreg_SetValue(pipe_hnd, blob.data, key_hnd,
481 name_string, value->type,
482 blob.data, blob.length, NULL);
483 TALLOC_FREE(blob.data);
487 static NTSTATUS rpc_registry_setvalue_internal(struct net_context *c,
488 const DOM_SID *domain_sid,
489 const char *domain_name,
490 struct cli_state *cli,
491 struct rpc_pipe_client *pipe_hnd,
496 struct policy_handle hive_hnd, key_hnd;
498 struct registry_value value;
500 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
501 SEC_FLAG_MAXIMUM_ALLOWED,
502 &hive_hnd, &key_hnd);
503 if (!NT_STATUS_IS_OK(status)) {
504 d_fprintf(stderr, "registry_openkey failed: %s\n",
509 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
510 d_fprintf(stderr, "Too many args for type %s\n", argv[2]);
511 return NT_STATUS_NOT_IMPLEMENTED;
514 if (strequal(argv[2], "dword")) {
515 value.type = REG_DWORD;
516 value.v.dword = strtoul(argv[3], NULL, 10);
518 else if (strequal(argv[2], "sz")) {
520 value.v.sz.len = strlen(argv[3])+1;
521 value.v.sz.str = CONST_DISCARD(char *, argv[3]);
524 d_fprintf(stderr, "type \"%s\" not implemented\n", argv[2]);
525 status = NT_STATUS_NOT_IMPLEMENTED;
529 status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
532 if (!NT_STATUS_IS_OK(status)) {
533 d_fprintf(stderr, "registry_setvalue failed: %s\n",
538 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
539 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
544 static int rpc_registry_setvalue(struct net_context *c, int argc,
547 if (argc < 4 || c->display_usage) {
548 d_fprintf(stderr, "usage: net rpc registry setvalue <key> "
549 "<valuename> <type> [<val>]+\n");
553 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
554 rpc_registry_setvalue_internal, argc, argv );
557 static NTSTATUS rpc_registry_deletevalue_internal(struct net_context *c,
558 const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
566 struct policy_handle hive_hnd, key_hnd;
568 struct winreg_String valuename;
570 ZERO_STRUCT(valuename);
572 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
573 SEC_FLAG_MAXIMUM_ALLOWED,
574 &hive_hnd, &key_hnd);
575 if (!NT_STATUS_IS_OK(status)) {
576 d_fprintf(stderr, "registry_openkey failed: %s\n",
581 valuename.name = argv[1];
583 status = rpccli_winreg_DeleteValue(pipe_hnd, mem_ctx, &key_hnd,
586 if (!NT_STATUS_IS_OK(status)) {
587 d_fprintf(stderr, "registry_deletevalue failed: %s\n",
591 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
592 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
597 static int rpc_registry_deletevalue(struct net_context *c, int argc,
600 if (argc != 2 || c->display_usage) {
601 d_fprintf(stderr, "usage: net rpc registry deletevalue <key> "
606 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
607 rpc_registry_deletevalue_internal, argc, argv );
610 static NTSTATUS rpc_registry_getvalue_internal(struct net_context *c,
611 const DOM_SID *domain_sid,
612 const char *domain_name,
613 struct cli_state *cli,
614 struct rpc_pipe_client *pipe_hnd,
620 struct policy_handle hive_hnd, key_hnd;
623 struct winreg_String valuename;
624 struct registry_value *value = NULL;
625 enum winreg_Type type = REG_NONE;
626 uint8_t *data = NULL;
627 uint32_t data_size = 0;
628 uint32_t value_length = 0;
629 TALLOC_CTX *tmp_ctx = talloc_stackframe();
631 ZERO_STRUCT(valuename);
633 status = registry_openkey(tmp_ctx, pipe_hnd, argv[0],
634 SEC_FLAG_MAXIMUM_ALLOWED,
635 &hive_hnd, &key_hnd);
636 if (!NT_STATUS_IS_OK(status)) {
637 d_fprintf(stderr, "registry_openkey failed: %s\n",
642 valuename.name = argv[1];
645 * call QueryValue once with data == NULL to get the
646 * needed memory size to be allocated, then allocate
647 * data buffer and call again.
649 status = rpccli_winreg_QueryValue(pipe_hnd, tmp_ctx, &key_hnd,
657 if (!NT_STATUS_IS_OK(status)) {
658 d_fprintf(stderr, "registry_queryvalue failed: %s\n",
663 data = (uint8 *)TALLOC(tmp_ctx, data_size);
666 status = rpccli_winreg_QueryValue(pipe_hnd, tmp_ctx, &key_hnd,
674 if (!NT_STATUS_IS_OK(status)) {
675 d_fprintf(stderr, "registry_queryvalue failed: %s\n",
680 werr = registry_pull_value(tmp_ctx, &value, type, data,
681 data_size, value_length);
682 if (!W_ERROR_IS_OK(werr)) {
683 status = werror_to_ntstatus(werr);
687 print_registry_value(value, raw);
690 rpccli_winreg_CloseKey(pipe_hnd, tmp_ctx, &key_hnd, NULL);
691 rpccli_winreg_CloseKey(pipe_hnd, tmp_ctx, &hive_hnd, NULL);
693 TALLOC_FREE(tmp_ctx);
698 static NTSTATUS rpc_registry_getvalue_full(struct net_context *c,
699 const DOM_SID *domain_sid,
700 const char *domain_name,
701 struct cli_state *cli,
702 struct rpc_pipe_client *pipe_hnd,
707 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
708 cli, pipe_hnd, mem_ctx, false,
712 static int rpc_registry_getvalue(struct net_context *c, int argc,
715 if (argc != 2 || c->display_usage) {
716 d_fprintf(stderr, "usage: net rpc registry getvalue <key> "
721 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
722 rpc_registry_getvalue_full, argc, argv);
725 static NTSTATUS rpc_registry_getvalue_raw(struct net_context *c,
726 const DOM_SID *domain_sid,
727 const char *domain_name,
728 struct cli_state *cli,
729 struct rpc_pipe_client *pipe_hnd,
734 return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
735 cli, pipe_hnd, mem_ctx, true,
739 static int rpc_registry_getvalueraw(struct net_context *c, int argc,
742 if (argc != 2 || c->display_usage) {
743 d_fprintf(stderr, "usage: net rpc registry getvalue <key> "
748 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
749 rpc_registry_getvalue_raw, argc, argv);
752 static NTSTATUS rpc_registry_createkey_internal(struct net_context *c,
753 const DOM_SID *domain_sid,
754 const char *domain_name,
755 struct cli_state *cli,
756 struct rpc_pipe_client *pipe_hnd,
762 struct policy_handle hive_hnd, key_hnd;
763 struct winreg_String key, keyclass;
764 enum winreg_CreateAction action;
768 ZERO_STRUCT(keyclass);
770 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
771 return NT_STATUS_INVALID_PARAMETER;
774 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
775 SEC_FLAG_MAXIMUM_ALLOWED,
777 if (!(NT_STATUS_IS_OK(status))) {
781 action = REG_ACTION_NONE;
784 status = rpccli_winreg_CreateKey(pipe_hnd, mem_ctx, &hive_hnd, key,
785 keyclass, 0, REG_KEY_READ, NULL,
786 &key_hnd, &action, NULL);
787 if (!NT_STATUS_IS_OK(status)) {
788 d_fprintf(stderr, "createkey returned %s\n",
790 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
795 case REG_ACTION_NONE:
796 d_printf("createkey did nothing -- huh?\n");
798 case REG_CREATED_NEW_KEY:
799 d_printf("createkey created %s\n", argv[0]);
801 case REG_OPENED_EXISTING_KEY:
802 d_printf("createkey opened existing %s\n", argv[0]);
806 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &key_hnd, NULL);
807 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
812 static int rpc_registry_createkey(struct net_context *c, int argc,
815 if (argc != 1 || c->display_usage) {
816 d_fprintf(stderr, "usage: net rpc registry createkey <key>\n");
820 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
821 rpc_registry_createkey_internal, argc, argv );
824 static NTSTATUS rpc_registry_deletekey_internal(struct net_context *c,
825 const DOM_SID *domain_sid,
826 const char *domain_name,
827 struct cli_state *cli,
828 struct rpc_pipe_client *pipe_hnd,
834 struct policy_handle hive_hnd;
835 struct winreg_String key;
840 if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
841 return NT_STATUS_INVALID_PARAMETER;
844 status = rpccli_winreg_Connect(pipe_hnd, mem_ctx, hive,
845 SEC_FLAG_MAXIMUM_ALLOWED,
847 if (!(NT_STATUS_IS_OK(status))) {
851 status = rpccli_winreg_DeleteKey(pipe_hnd, mem_ctx, &hive_hnd, key, NULL);
852 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &hive_hnd, NULL);
854 if (!NT_STATUS_IS_OK(status)) {
855 d_fprintf(stderr, "deletekey returned %s\n",
862 static int rpc_registry_deletekey(struct net_context *c, int argc, const char **argv )
864 if (argc != 1 || c->display_usage) {
865 d_fprintf(stderr, "usage: net rpc registry deletekey <key>\n");
869 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
870 rpc_registry_deletekey_internal, argc, argv );
873 /********************************************************************
874 ********************************************************************/
876 static NTSTATUS rpc_registry_enumerate_internal(struct net_context *c,
877 const DOM_SID *domain_sid,
878 const char *domain_name,
879 struct cli_state *cli,
880 struct rpc_pipe_client *pipe_hnd,
885 struct policy_handle pol_hive, pol_key;
887 uint32 num_subkeys = 0;
888 uint32 num_values = 0;
889 char **names = NULL, **classes = NULL;
890 NTTIME **modtimes = NULL;
892 struct registry_value **values = NULL;
894 if (argc != 1 || c->display_usage) {
895 d_printf("Usage: net rpc registry enumerate <path>\n");
896 d_printf("Example: net rpc registry enumerate 'HKLM\\Software\\Samba'\n");
897 return NT_STATUS_INVALID_PARAMETER;
900 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
901 &pol_hive, &pol_key);
902 if (!NT_STATUS_IS_OK(status)) {
903 d_fprintf(stderr, "registry_openkey failed: %s\n",
908 status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
909 &names, &classes, &modtimes);
910 if (!NT_STATUS_IS_OK(status)) {
911 d_fprintf(stderr, "enumerating keys failed: %s\n",
916 for (i=0; i<num_subkeys; i++) {
917 print_registry_key(names[i], modtimes[i]);
920 status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
922 if (!NT_STATUS_IS_OK(status)) {
923 d_fprintf(stderr, "enumerating values failed: %s\n",
928 for (i=0; i<num_values; i++) {
929 print_registry_value_with_name(names[i], values[i]);
932 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key, NULL);
933 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive, NULL);
938 /********************************************************************
939 ********************************************************************/
941 static int rpc_registry_enumerate(struct net_context *c, int argc,
944 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
945 rpc_registry_enumerate_internal, argc, argv );
948 /********************************************************************
949 ********************************************************************/
951 static NTSTATUS rpc_registry_save_internal(struct net_context *c,
952 const DOM_SID *domain_sid,
953 const char *domain_name,
954 struct cli_state *cli,
955 struct rpc_pipe_client *pipe_hnd,
960 WERROR result = WERR_GENERAL_FAILURE;
961 struct policy_handle pol_hive, pol_key;
962 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
963 struct winreg_String filename;
965 if (argc != 2 || c->display_usage) {
966 d_printf("Usage: net rpc registry backup <path> <file> \n");
967 return NT_STATUS_INVALID_PARAMETER;
970 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
971 &pol_hive, &pol_key);
972 if (!NT_STATUS_IS_OK(status)) {
973 d_fprintf(stderr, "registry_openkey failed: %s\n",
978 filename.name = argv[1];
979 status = rpccli_winreg_SaveKey( pipe_hnd, mem_ctx, &pol_key, &filename, NULL, NULL);
980 if ( !W_ERROR_IS_OK(result) ) {
981 d_fprintf(stderr, "Unable to save [%s] to %s:%s\n", argv[0], cli->desthost, argv[1]);
986 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key, NULL);
987 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive, NULL);
992 /********************************************************************
993 ********************************************************************/
995 static int rpc_registry_save(struct net_context *c, int argc, const char **argv )
997 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
998 rpc_registry_save_internal, argc, argv );
1002 /********************************************************************
1003 ********************************************************************/
1005 static void dump_values( REGF_NK_REC *nk )
1008 char *data_str = NULL;
1009 uint32 data_size, data;
1014 for ( i=0; i<nk->num_values; i++ ) {
1015 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
1016 d_printf( "(%s) ", reg_type_lookup( nk->values[i].type ) );
1018 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
1019 switch ( nk->values[i].type ) {
1021 rpcstr_pull_talloc(talloc_tos(),
1029 d_printf( "%s", data_str );
1033 for ( j=0; j<data_size; j++ ) {
1034 d_printf( "%c", nk->values[i].data[j] );
1038 data = IVAL( nk->values[i].data, 0 );
1039 d_printf("0x%x", data );
1042 for ( j=0; j<data_size; j++ ) {
1043 d_printf( "%x", nk->values[i].data[j] );
1047 d_printf("unknown");
1056 /********************************************************************
1057 ********************************************************************/
1059 static bool dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
1063 /* depth first dump of the registry tree */
1065 while ( (key = regfio_fetch_subkey( file, nk )) ) {
1067 if (asprintf(®path, "%s\\%s", parent, key->keyname) < 0) {
1070 d_printf("[%s]\n", regpath );
1073 dump_registry_tree( file, key, regpath );
1080 /********************************************************************
1081 ********************************************************************/
1083 static bool write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
1084 REGF_NK_REC *parent, REGF_FILE *outfile,
1085 const char *parentpath )
1087 REGF_NK_REC *key, *subkey;
1088 struct regval_ctr *values = NULL;
1089 struct regsubkey_ctr *subkeys = NULL;
1094 werr = regsubkey_ctr_init(infile->mem_ctx, &subkeys);
1095 if (!W_ERROR_IS_OK(werr)) {
1096 DEBUG(0, ("write_registry_tree: regsubkey_ctr_init failed: "
1097 "%s\n", win_errstr(werr)));
1101 if ( !(values = TALLOC_ZERO_P( subkeys, struct regval_ctr )) ) {
1102 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
1103 TALLOC_FREE(subkeys);
1107 /* copy values into the struct regval_ctr */
1109 for ( i=0; i<nk->num_values; i++ ) {
1110 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
1111 (const char *)nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
1114 /* copy subkeys into the struct regsubkey_ctr */
1116 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1117 regsubkey_ctr_addkey( subkeys, subkey->keyname );
1120 key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
1122 /* write each one of the subkeys out */
1124 path = talloc_asprintf(subkeys,
1130 TALLOC_FREE(subkeys);
1134 nk->subkey_index = 0;
1135 while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1136 write_registry_tree( infile, subkey, key, outfile, path );
1139 d_printf("[%s]\n", path );
1140 TALLOC_FREE(subkeys);
1145 /********************************************************************
1146 ********************************************************************/
1148 static int rpc_registry_dump(struct net_context *c, int argc, const char **argv)
1150 REGF_FILE *registry;
1153 if (argc != 1 || c->display_usage) {
1154 d_printf("Usage: net rpc registry dump <file> \n");
1158 d_printf("Opening %s....", argv[0]);
1159 if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
1160 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
1165 /* get the root of the registry file */
1167 if ((nk = regfio_rootkey( registry )) == NULL) {
1168 d_fprintf(stderr, "Could not get rootkey\n");
1169 regfio_close( registry );
1172 d_printf("[%s]\n", nk->keyname);
1176 dump_registry_tree( registry, nk, nk->keyname );
1179 talloc_report_full( registry->mem_ctx, stderr );
1181 d_printf("Closing registry...");
1182 regfio_close( registry );
1188 /********************************************************************
1189 ********************************************************************/
1191 static int rpc_registry_copy(struct net_context *c, int argc, const char **argv )
1193 REGF_FILE *infile = NULL, *outfile = NULL;
1197 if (argc != 2 || c->display_usage) {
1198 d_printf("Usage: net rpc registry copy <srcfile> <newfile>\n");
1202 d_printf("Opening %s....", argv[0]);
1203 if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
1204 d_fprintf(stderr, "Failed to open %s for reading\n", argv[0]);
1209 d_printf("Opening %s....", argv[1]);
1210 if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC), (S_IREAD|S_IWRITE) )) ) {
1211 d_fprintf(stderr, "Failed to open %s for writing\n", argv[1]);
1216 /* get the root of the registry file */
1218 if ((nk = regfio_rootkey( infile )) == NULL) {
1219 d_fprintf(stderr, "Could not get rootkey\n");
1222 d_printf("RootKey: [%s]\n", nk->keyname);
1224 write_registry_tree( infile, nk, NULL, outfile, "" );
1230 d_printf("Closing %s...", argv[1]);
1232 regfio_close( outfile );
1236 d_printf("Closing %s...", argv[0]);
1238 regfio_close( infile );
1245 /********************************************************************
1246 ********************************************************************/
1248 static NTSTATUS rpc_registry_getsd_internal(struct net_context *c,
1249 const DOM_SID *domain_sid,
1250 const char *domain_name,
1251 struct cli_state *cli,
1252 struct rpc_pipe_client *pipe_hnd,
1253 TALLOC_CTX *mem_ctx,
1257 struct policy_handle pol_hive, pol_key;
1259 enum ndr_err_code ndr_err;
1260 struct KeySecurityData *sd = NULL;
1263 struct security_descriptor sec_desc;
1264 uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED |
1265 SEC_FLAG_SYSTEM_SECURITY;
1268 if (argc <1 || argc > 2 || c->display_usage) {
1269 d_printf("Usage: net rpc registry getsd <path> <secinfo>\n");
1270 d_printf("Example: net rpc registry getsd 'HKLM\\Software\\Samba'\n");
1271 return NT_STATUS_INVALID_PARAMETER;
1274 status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
1276 &pol_hive, &pol_key);
1277 if (!NT_STATUS_IS_OK(status)) {
1278 d_fprintf(stderr, "registry_openkey failed: %s\n",
1283 sd = TALLOC_ZERO_P(mem_ctx, struct KeySecurityData);
1285 status = NT_STATUS_NO_MEMORY;
1292 sscanf(argv[1], "%x", &sec_info);
1294 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
1297 status = registry_getsd(mem_ctx, pipe_hnd, &pol_key, sec_info, sd);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 d_fprintf(stderr, "getting sd failed: %s\n",
1304 blob.data = sd->data;
1305 blob.length = sd->size;
1307 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, &sec_desc,
1308 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1309 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1310 status = ndr_map_error2ntstatus(ndr_err);
1313 status = NT_STATUS_OK;
1315 display_sec_desc(&sec_desc);
1318 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key, NULL);
1319 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive, NULL);
1325 static int rpc_registry_getsd(struct net_context *c, int argc, const char **argv)
1327 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1328 rpc_registry_getsd_internal, argc, argv);
1331 /********************************************************************
1332 ********************************************************************/
1334 * @defgroup net_rpc_registry net rpc registry
1338 * @defgroup net_rpc_registry_export Export
1339 * @ingroup net_rpc_registry
1343 static NTSTATUS registry_export(struct rpc_pipe_client* pipe_hnd,
1345 struct policy_handle* key_hnd,
1346 struct reg_format* f,
1347 const char* parentfullname,
1351 uint32 num_subkeys = 0;
1352 uint32 num_values = 0;
1353 char **names = NULL, **classes = NULL;
1354 NTTIME **modtimes = NULL;
1355 struct regval_blob **values = NULL;
1358 TALLOC_CTX* mem_ctx = talloc_new(ctx);
1361 const char* fullname = name
1362 ? talloc_asprintf(mem_ctx, "%s\\%s", parentfullname, name)
1364 reg_format_key(f, &fullname, 1, false);
1366 status = registry_enumvalues2(mem_ctx, pipe_hnd, key_hnd, &num_values,
1368 if (!NT_STATUS_IS_OK(status)) {
1369 d_fprintf(stderr, "enumerating values failed: %s\n",
1374 for (i=0; i<num_values; i++) {
1375 reg_format_regval_blob(f, names[i], values[i]);
1379 status = registry_enumkeys(mem_ctx, pipe_hnd, key_hnd, &num_subkeys,
1380 &names, &classes, &modtimes);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 d_fprintf(stderr, "enumerating keys failed: %s\n",
1387 for (i=0; i<num_subkeys; i++) {
1388 struct policy_handle subkey_hnd;
1389 struct winreg_String key;
1391 /* key.name = talloc_strdup(mem_ctx, names[i]); ??? */
1392 key.name = names[i];
1394 status = rpccli_winreg_OpenKey(pipe_hnd, mem_ctx, key_hnd, key,
1397 if (NT_STATUS_IS_OK(status)) {
1398 status = registry_export(pipe_hnd, mem_ctx, &subkey_hnd,
1399 f, fullname, names[i]);
1400 if (!(NT_STATUS_IS_OK(status)))
1402 "export key failed: %s %s\n",
1403 names[i], nt_errstr(status));
1405 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx,
1409 "rpccli_winreg_OpenKey failed: %s %s\n",
1410 names[i], nt_errstr(status));
1414 talloc_free(mem_ctx);
1418 static NTSTATUS rpc_registry_export_internal(struct net_context *c,
1419 const DOM_SID *domain_sid,
1420 const char *domain_name,
1421 struct cli_state *cli,
1422 struct rpc_pipe_client *pipe_hnd,
1423 TALLOC_CTX *mem_ctx,
1427 struct policy_handle pol_hive, pol_key;
1429 struct reg_format* f;
1431 if (argc != 2 || c->display_usage) {
1434 "net rpc registry export <path> <file> [opt]\n");
1435 d_printf("%s net rpc registry export "
1436 "'HKLM\\Software\\Samba' samba.reg\n", "Example:");
1437 return NT_STATUS_INVALID_PARAMETER;
1440 status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1441 &pol_hive, &pol_key);
1442 if (!NT_STATUS_IS_OK(status)) {
1443 d_fprintf(stderr, "registry_openkey failed: %s\n",
1448 f = reg_format_file(mem_ctx, argv[1], (argc > 2) ? argv[2] : NULL);
1450 d_fprintf(stderr, "open file failed: %s\n", strerror(errno));
1451 return map_nt_error_from_unix(errno);
1454 status = registry_export(pipe_hnd, mem_ctx, &pol_key,
1456 if (!NT_STATUS_IS_OK(status))
1459 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_key, NULL);
1460 rpccli_winreg_CloseKey(pipe_hnd, mem_ctx, &pol_hive, NULL);
1465 static int rpc_registry_export(struct net_context *c, int argc,
1468 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1469 rpc_registry_export_internal, argc, argv );
1474 /********************************************************************
1475 ********************************************************************/
1478 * @defgroup net_rpc_registry_import Import
1479 * @ingroup net_rpc_registry
1484 struct rpc_pipe_client *pipe_hnd;
1485 TALLOC_CTX *mem_ctx;
1488 static WERROR import_create_key(struct import_ctx* ctx,
1489 struct policy_handle* parent, const char* name,
1490 void** pkey, bool* existing)
1492 WERROR werr=WERR_OK;
1494 void* mem_ctx = talloc_new(ctx->mem_ctx);
1496 struct policy_handle* key = NULL;
1497 struct policy_handle hive;
1498 struct winreg_String keyclass, keyname;
1499 enum winreg_CreateAction action = REG_ACTION_NONE;
1501 ZERO_STRUCT(keyname);
1502 keyname.name = name;
1504 if (parent == NULL) {
1505 uint32 hive_idx = 0;
1506 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1511 status = rpccli_winreg_Connect(ctx->pipe_hnd, mem_ctx,
1512 hive_idx, SEC_FLAG_MAXIMUM_ALLOWED,
1514 werr = ntstatus_to_werror(status);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 d_fprintf(stderr, "rpccli_winreg_Connect returned %s\n",
1525 key = talloc_zero(mem_ctx, struct policy_handle);
1531 ZERO_STRUCT(keyclass);
1534 status = rpccli_winreg_CreateKey(ctx->pipe_hnd, mem_ctx,
1536 keyclass, 0, REG_KEY_READ, NULL,
1537 key, &action, &werr);
1538 if (!NT_STATUS_IS_OK(status)) {
1539 d_fprintf(stderr, "rpccli_winreg_CreateKey returned %s\n",
1545 case REG_CREATED_NEW_KEY:
1546 d_printf("createkey created %s\n", name);
1547 if (existing != NULL)
1551 case REG_OPENED_EXISTING_KEY:
1552 d_printf("createkey opened existing %s\n", name);
1553 if (existing != NULL)
1557 case REG_ACTION_NONE:
1558 d_printf("createkey did nothing -- huh?\n");
1566 if ( parent == &hive ) {
1567 rpccli_winreg_CloseKey(ctx->pipe_hnd, mem_ctx,
1572 *pkey = talloc_steal(ctx->mem_ctx, key);
1575 talloc_free(mem_ctx);
1579 static WERROR import_delete_key(struct import_ctx* ctx,
1580 struct policy_handle* parent, const char* name)
1584 void* mem_ctx = talloc_new(ctx->mem_ctx);
1585 struct winreg_String keyname;
1586 struct policy_handle hive;
1588 keyname.name = name;
1590 if (parent == NULL) {
1592 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1597 status = rpccli_winreg_Connect(ctx->pipe_hnd, mem_ctx, hive_idx,
1598 SEC_FLAG_MAXIMUM_ALLOWED, &hive);
1599 werr = ntstatus_to_werror(status);
1600 if (!NT_STATUS_IS_OK(status)) {
1601 d_fprintf(stderr, "rpccli_winreg_Connect returned %s\n",
1609 status = rpccli_winreg_DeleteKey(ctx->pipe_hnd, mem_ctx, parent,
1611 if (!NT_STATUS_IS_OK(status)) {
1612 d_fprintf(stderr, "rpccli_winreg_DeleteKey returned %s\n",
1618 if ( parent == &hive ) {
1619 rpccli_winreg_CloseKey(ctx->pipe_hnd, mem_ctx, parent, NULL);
1622 talloc_free(mem_ctx);
1626 static WERROR import_close_key(struct import_ctx* ctx,
1627 struct policy_handle* key)
1631 void* mem_ctx = talloc_new(ctx->mem_ctx);
1633 status = rpccli_winreg_CloseKey(ctx->pipe_hnd, mem_ctx, key, &werr);
1634 if (!NT_STATUS_IS_OK(status)) {
1635 d_fprintf(stderr, "rpccli_winreg_CloseKey returned %s\n",
1640 werr = (talloc_free(key) == 0) ? WERR_OK : WERR_GENERAL_FAILURE;
1642 talloc_free(mem_ctx);
1646 static WERROR import_create_val(struct import_ctx* ctx,
1647 struct policy_handle* parent, const char* name,
1648 uint32_t type, const uint8_t* val, uint32_t len)
1652 void* mem_ctx = talloc_new(ctx->mem_ctx);
1653 struct winreg_String valuename;
1655 if (parent == NULL) {
1656 return WERR_INVALID_PARAM;
1659 ZERO_STRUCT(valuename);
1660 valuename.name = name;
1662 status = rpccli_winreg_SetValue(ctx->pipe_hnd, mem_ctx, parent,
1664 discard_const(val), len, &werr);
1665 if (!NT_STATUS_IS_OK(status)) {
1666 d_fprintf(stderr, "registry_setvalue failed: %s\n",
1672 talloc_free(mem_ctx);
1676 static WERROR import_delete_val(struct import_ctx* ctx,
1677 struct policy_handle* parent, const char* name)
1681 void* mem_ctx = talloc_new(ctx->mem_ctx);
1682 struct winreg_String valuename;
1684 if (parent == NULL) {
1685 return WERR_INVALID_PARAM;
1688 ZERO_STRUCT(valuename);
1689 valuename.name = name;
1691 status = rpccli_winreg_DeleteValue(ctx->pipe_hnd, mem_ctx,
1692 parent, valuename, &werr);
1694 if (!NT_STATUS_IS_OK(status)) {
1695 d_fprintf(stderr, "registry_deletevalue failed: %s\n",
1700 talloc_free(mem_ctx);
1706 static NTSTATUS rpc_registry_import_internal(struct net_context *c,
1707 const DOM_SID *domain_sid,
1708 const char *domain_name,
1709 struct cli_state *cli,
1710 struct rpc_pipe_client *pipe_hnd,
1711 TALLOC_CTX *mem_ctx,
1715 struct import_ctx import_ctx;
1717 struct reg_import_callback import_callback = {
1719 .closekey = (reg_import_callback_closekey_t)&import_close_key,
1720 .createkey = (reg_import_callback_createkey_t)&import_create_key,
1721 .deletekey = (reg_import_callback_deletekey_t)&import_delete_key,
1722 .deleteval = (reg_import_callback_deleteval_t)&import_delete_val,
1723 .setval.blob = (reg_import_callback_setval_blob_t)&import_create_val,
1724 .setval_type = BLOB,
1729 if (argc < 1 || argc > 2 || c->display_usage) {
1732 "net rpc registry import <file> [options]\n");
1733 d_printf("%s net rpc registry export "
1734 "samba.reg enc=CP1252,flags=0\n", "Example:");
1735 return NT_STATUS_INVALID_PARAMETER;
1737 ZERO_STRUCT(import_ctx);
1738 import_ctx.pipe_hnd = pipe_hnd;
1739 import_ctx.mem_ctx = mem_ctx;
1740 ret = reg_parse_file(argv[0],
1741 reg_import_adapter(import_ctx.mem_ctx,
1744 (argc > 1) ? argv[1] : NULL
1747 return ret==0 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1751 static int rpc_registry_import(struct net_context *c, int argc,
1754 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1755 rpc_registry_import_internal, argc, argv );
1759 /********************************************************************
1760 ********************************************************************/
1762 int net_rpc_registry(struct net_context *c, int argc, const char **argv)
1764 struct functable func[] = {
1767 rpc_registry_enumerate,
1769 "Enumerate registry keys and values",
1770 "net rpc registry enumerate\n"
1771 " Enumerate registry keys and values"
1775 rpc_registry_createkey,
1777 "Create a new registry key",
1778 "net rpc registry createkey\n"
1779 " Create a new registry key"
1783 rpc_registry_deletekey,
1785 "Delete a registry key",
1786 "net rpc registry deletekey\n"
1787 " Delete a registry key"
1791 rpc_registry_getvalue,
1793 "Print a registry value",
1794 "net rpc registry getvalue\n"
1795 " Print a registry value"
1799 rpc_registry_getvalueraw,
1801 "Print a registry value",
1802 "net rpc registry getvalueraw\n"
1803 " Print a registry value (raw version)"
1807 rpc_registry_setvalue,
1809 "Set a new registry value",
1810 "net rpc registry setvalue\n"
1811 " Set a new registry value"
1815 rpc_registry_deletevalue,
1817 "Delete a registry value",
1818 "net rpc registry deletevalue\n"
1819 " Delete a registry value"
1825 "Save a registry file",
1826 "net rpc registry save\n"
1827 " Save a registry file"
1833 "Dump a registry file",
1834 "net rpc registry dump\n"
1835 " Dump a registry file"
1841 "Copy a registry file",
1842 "net rpc registry copy\n"
1843 " Copy a registry file"
1849 "Get security descriptor",
1850 "net rpc registry getsd\n"
1851 " Get security descriptior"
1855 rpc_registry_export,
1857 "net registry export\n"
1862 rpc_registry_import,
1865 "net rpc registry import\n"
1868 {NULL, NULL, 0, NULL, NULL}
1871 return net_run_function(c, argc, argv, "net rpc registry", func);