2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Local registry interface
6 * Copyright (C) Michael Adam 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "registry/reg_api.h"
25 #include "registry/reg_util_token.h"
26 #include "registry/reg_init_basic.h"
27 #include "utils/net.h"
28 #include "utils/net_registry_util.h"
29 #include "include/g_lock.h"
30 #include "registry/reg_backend_db.h"
31 #include "registry/reg_import.h"
32 #include "registry/reg_format.h"
34 #include "../libcli/security/display_sec.h"
35 #include "../libcli/security/sddl.h"
36 #include "../libcli/registry/util_reg.h"
37 #include "passdb/machine_sid.h"
46 * split given path into hive and remaining path and open the hive key
48 static WERROR
open_hive(TALLOC_CTX
*ctx
, const char *path
,
49 uint32 desired_access
,
50 struct registry_key
**hive
,
54 struct security_token
*token
= NULL
;
55 char *hivename
= NULL
;
56 char *tmp_subkeyname
= NULL
;
57 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
59 if ((hive
== NULL
) || (subkeyname
== NULL
)) {
60 werr
= WERR_INVALID_PARAM
;
64 werr
= split_hive_key(tmp_ctx
, path
, &hivename
, &tmp_subkeyname
);
65 if (!W_ERROR_IS_OK(werr
)) {
68 *subkeyname
= talloc_strdup(ctx
, tmp_subkeyname
);
69 if (*subkeyname
== NULL
) {
74 werr
= ntstatus_to_werror(registry_create_admin_token(tmp_ctx
, &token
));
75 if (!W_ERROR_IS_OK(werr
)) {
79 werr
= reg_openhive(ctx
, hivename
, desired_access
, token
, hive
);
80 if (!W_ERROR_IS_OK(werr
)) {
91 static WERROR
open_key(TALLOC_CTX
*ctx
, const char *path
,
92 uint32 desired_access
,
93 struct registry_key
**key
)
96 char *subkey_name
= NULL
;
97 struct registry_key
*hive
= NULL
;
98 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
100 if ((path
== NULL
) || (key
== NULL
)) {
101 return WERR_INVALID_PARAM
;
104 werr
= open_hive(tmp_ctx
, path
, desired_access
, &hive
, &subkey_name
);
105 if (!W_ERROR_IS_OK(werr
)) {
106 d_fprintf(stderr
, _("open_hive failed: %s\n"),
111 werr
= reg_openkey(ctx
, hive
, subkey_name
, desired_access
, key
);
112 if (!W_ERROR_IS_OK(werr
)) {
113 d_fprintf(stderr
, _("reg_openkey failed: %s\n"),
121 TALLOC_FREE(tmp_ctx
);
127 * the main "net registry" function implementations
131 static int net_registry_enumerate(struct net_context
*c
, int argc
,
135 struct registry_key
*key
= NULL
;
136 TALLOC_CTX
*ctx
= talloc_stackframe();
140 char *valname
= NULL
;
141 struct registry_value
*valvalue
= NULL
;
144 if (argc
!= 1 || c
->display_usage
) {
147 _("net registry enumerate <path>\n"));
150 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
154 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
155 if (!W_ERROR_IS_OK(werr
)) {
156 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
161 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
165 print_registry_key(subkey_name
, &modtime
);
167 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
172 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
176 print_registry_value_with_name(valname
, valvalue
);
178 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
188 static WERROR
registry_enumkey(struct registry_key
* parent
, const char* keyname
, bool recursive
)
191 TALLOC_CTX
*ctx
= talloc_stackframe();
195 char* valname
= NULL
;
196 struct registry_value
*valvalue
= NULL
;
197 struct registry_key
* key
= NULL
;
199 werr
= reg_openkey(ctx
, parent
, keyname
, REG_KEY_READ
, &key
);
200 if (!W_ERROR_IS_OK(werr
)) {
204 printf("[%s]\n", key
->key
->name
);
207 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
211 print_registry_key(subkey_name
, &modtime
);
213 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
218 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
222 print_registry_value_with_name(valname
, valvalue
);
224 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
234 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
238 werr
= registry_enumkey(key
, subkey_name
, recursive
);
239 if (!W_ERROR_IS_OK(werr
)) {
243 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
252 static int net_registry_enumerate_recursive(struct net_context
*c
, int argc
,
256 struct registry_key
*key
= NULL
;
258 TALLOC_CTX
*ctx
= talloc_stackframe();
261 if (argc
!= 1 || c
->display_usage
) {
264 _("net registry enumerate <path>\n"));
267 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
271 werr
= open_hive(ctx
, argv
[0], REG_KEY_READ
, &key
, &name
);
272 if (!W_ERROR_IS_OK(werr
)) {
273 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
277 werr
= registry_enumkey(key
, name
, true);
278 if (W_ERROR_IS_OK(werr
)) {
287 static int net_registry_createkey(struct net_context
*c
, int argc
,
291 enum winreg_CreateAction action
;
293 struct registry_key
*hivekey
= NULL
;
294 struct registry_key
*subkey
= NULL
;
295 TALLOC_CTX
*ctx
= talloc_stackframe();
298 if (argc
!= 1 || c
->display_usage
) {
301 _("net registry createkey <path>\n"));
304 _("net registry createkey "
305 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
308 if (strlen(argv
[0]) == 0) {
309 d_fprintf(stderr
, _("error: zero length key name given\n"));
313 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
314 if (!W_ERROR_IS_OK(werr
)) {
315 d_fprintf(stderr
, _("open_hive failed: %s\n"),
320 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
322 if (!W_ERROR_IS_OK(werr
)) {
323 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
328 case REG_ACTION_NONE
:
329 d_printf(_("createkey did nothing -- huh?\n"));
331 case REG_CREATED_NEW_KEY
:
332 d_printf(_("createkey created %s\n"), argv
[0]);
334 case REG_OPENED_EXISTING_KEY
:
335 d_printf(_("createkey opened existing %s\n"), argv
[0]);
346 static int net_registry_deletekey_internal(struct net_context
*c
, int argc
,
352 struct registry_key
*hivekey
= NULL
;
353 TALLOC_CTX
*ctx
= talloc_stackframe();
356 if (argc
!= 1 || c
->display_usage
) {
359 _("net registry deletekey <path>\n"));
362 _("net registry deletekey "
363 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
366 if (strlen(argv
[0]) == 0) {
367 d_fprintf(stderr
, _("error: zero length key name given\n"));
371 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
372 if (!W_ERROR_IS_OK(werr
)) {
373 d_fprintf(stderr
, "open_hive %s: %s\n", _("failed"),
379 werr
= reg_deletekey_recursive(hivekey
, subkeyname
);
381 werr
= reg_deletekey(hivekey
, subkeyname
);
383 if (!W_ERROR_IS_OK(werr
) &&
384 !(c
->opt_force
&& W_ERROR_EQUAL(werr
, WERR_BADFILE
)))
386 d_fprintf(stderr
, "reg_deletekey %s: %s\n", _("failed"),
398 static int net_registry_deletekey(struct net_context
*c
, int argc
,
401 return net_registry_deletekey_internal(c
, argc
, argv
, false);
404 static int net_registry_deletekey_recursive(struct net_context
*c
, int argc
,
407 return net_registry_deletekey_internal(c
, argc
, argv
, true);
410 static int net_registry_getvalue_internal(struct net_context
*c
, int argc
,
411 const char **argv
, bool raw
)
415 struct registry_key
*key
= NULL
;
416 struct registry_value
*value
= NULL
;
417 TALLOC_CTX
*ctx
= talloc_stackframe();
419 if (argc
!= 2 || c
->display_usage
) {
420 d_fprintf(stderr
, "%s\n%s",
422 _("net registry getvalue <key> <valuename>\n"));
426 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
427 if (!W_ERROR_IS_OK(werr
)) {
428 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
432 werr
= reg_queryvalue(ctx
, key
, argv
[1], &value
);
433 if (!W_ERROR_IS_OK(werr
)) {
434 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
439 print_registry_value(value
, raw
);
448 static int net_registry_getvalue(struct net_context
*c
, int argc
,
451 return net_registry_getvalue_internal(c
, argc
, argv
, false);
454 static int net_registry_getvalueraw(struct net_context
*c
, int argc
,
457 return net_registry_getvalue_internal(c
, argc
, argv
, true);
460 static int net_registry_getvaluesraw(struct net_context
*c
, int argc
,
465 struct registry_key
*key
= NULL
;
466 TALLOC_CTX
*ctx
= talloc_stackframe();
469 if (argc
!= 1 || c
->display_usage
) {
470 d_fprintf(stderr
, "usage: net rpc registry getvaluesraw "
475 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
476 if (!W_ERROR_IS_OK(werr
)) {
477 d_fprintf(stderr
, "open_key failed: %s\n", win_errstr(werr
));
483 struct registry_value
*val
;
485 werr
= reg_enumvalue(talloc_tos(), key
, idx
, NULL
, &val
);
487 if (W_ERROR_EQUAL(werr
, WERR_NO_MORE_ITEMS
)) {
491 if (!W_ERROR_IS_OK(werr
)) {
494 print_registry_value(val
, true);
503 static int net_registry_setvalue(struct net_context
*c
, int argc
,
507 struct registry_value value
;
508 struct registry_key
*key
= NULL
;
510 TALLOC_CTX
*ctx
= talloc_stackframe();
512 if (argc
< 4 || c
->display_usage
) {
513 d_fprintf(stderr
, "%s\n%s",
515 _("net registry setvalue <key> <valuename> "
516 "<type> [<val>]+\n"));
520 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
521 d_fprintf(stderr
, _("Too many args for type %s\n"), argv
[2]);
525 if (strequal(argv
[2], "dword")) {
526 uint32_t v
= strtoul(argv
[3], NULL
, 10);
527 value
.type
= REG_DWORD
;
528 value
.data
= data_blob_talloc(ctx
, NULL
, 4);
529 SIVAL(value
.data
.data
, 0, v
);
530 } else if (strequal(argv
[2], "sz")) {
532 if (!push_reg_sz(ctx
, &value
.data
, argv
[3])) {
535 } else if (strequal(argv
[2], "multi_sz")) {
537 int count
= argc
- 3;
539 value
.type
= REG_MULTI_SZ
;
540 array
= talloc_zero_array(ctx
, const char *, count
+ 1);
544 for (i
=0; i
< count
; i
++) {
545 array
[i
] = talloc_strdup(array
, argv
[count
+i
]);
546 if (array
[i
] == NULL
) {
550 if (!push_reg_multi_sz(ctx
, &value
.data
, array
)) {
554 d_fprintf(stderr
, _("type \"%s\" not implemented\n"), argv
[2]);
558 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
559 if (!W_ERROR_IS_OK(werr
)) {
560 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
564 werr
= reg_setvalue(key
, argv
[1], &value
);
565 if (!W_ERROR_IS_OK(werr
)) {
566 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
578 struct net_registry_increment_state
{
580 const char *valuename
;
586 static void net_registry_increment_fn(void *private_data
)
588 struct net_registry_increment_state
*state
=
589 (struct net_registry_increment_state
*)private_data
;
590 struct registry_value
*value
;
591 struct registry_key
*key
= NULL
;
594 state
->werr
= open_key(talloc_tos(), state
->keyname
,
595 REG_KEY_READ
|REG_KEY_WRITE
, &key
);
596 if (!W_ERROR_IS_OK(state
->werr
)) {
597 d_fprintf(stderr
, _("open_key failed: %s\n"),
598 win_errstr(state
->werr
));
602 state
->werr
= reg_queryvalue(key
, key
, state
->valuename
, &value
);
603 if (!W_ERROR_IS_OK(state
->werr
)) {
604 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
605 win_errstr(state
->werr
));
609 if (value
->type
!= REG_DWORD
) {
610 d_fprintf(stderr
, _("value not a DWORD: %s\n"),
611 str_regtype(value
->type
));
615 if (value
->data
.length
< 4) {
616 d_fprintf(stderr
, _("value too short for regular DWORD\n"));
620 v
= IVAL(value
->data
.data
, 0);
621 v
+= state
->increment
;
624 SIVAL(value
->data
.data
, 0, v
);
626 state
->werr
= reg_setvalue(key
, state
->valuename
, value
);
627 if (!W_ERROR_IS_OK(state
->werr
)) {
628 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
629 win_errstr(state
->werr
));
638 static int net_registry_increment(struct net_context
*c
, int argc
,
641 struct net_registry_increment_state state
;
645 if (argc
< 2 || c
->display_usage
) {
646 d_fprintf(stderr
, "%s\n%s",
648 _("net registry increment <key> <valuename> "
653 state
.keyname
= argv
[0];
654 state
.valuename
= argv
[1];
658 state
.increment
= strtoul(argv
[2], NULL
, 10);
661 status
= g_lock_do("registry_increment_lock", G_LOCK_WRITE
,
662 timeval_set(600, 0), procid_self(),
663 net_registry_increment_fn
, &state
);
664 if (!NT_STATUS_IS_OK(status
)) {
665 d_fprintf(stderr
, _("g_lock_do failed: %s\n"),
669 if (!W_ERROR_IS_OK(state
.werr
)) {
670 d_fprintf(stderr
, _("increment failed: %s\n"),
671 win_errstr(state
.werr
));
675 d_printf(_("%u\n"), (unsigned)state
.newvalue
);
683 static int net_registry_deletevalue(struct net_context
*c
, int argc
,
687 struct registry_key
*key
= NULL
;
688 TALLOC_CTX
*ctx
= talloc_stackframe();
691 if (argc
!= 2 || c
->display_usage
) {
692 d_fprintf(stderr
, "%s\n%s",
694 _("net registry deletevalue <key> <valuename>\n"));
698 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
699 if (!W_ERROR_IS_OK(werr
)) {
700 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
704 werr
= reg_deletevalue(key
, argv
[1]);
705 if (!W_ERROR_IS_OK(werr
)) {
706 d_fprintf(stderr
, _("reg_deletevalue failed: %s\n"),
718 static WERROR
net_registry_getsd_internal(struct net_context
*c
,
721 struct security_descriptor
**sd
)
724 struct registry_key
*key
= NULL
;
725 TALLOC_CTX
*ctx
= talloc_stackframe();
726 uint32_t access_mask
= REG_KEY_READ
|
727 SEC_FLAG_MAXIMUM_ALLOWED
|
728 SEC_FLAG_SYSTEM_SECURITY
;
731 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
732 * is denied with these perms right now...
734 access_mask
= REG_KEY_READ
;
737 d_fprintf(stderr
, _("internal error: invalid argument\n"));
738 werr
= WERR_INVALID_PARAM
;
742 if (strlen(keyname
) == 0) {
743 d_fprintf(stderr
, _("error: zero length key name given\n"));
744 werr
= WERR_INVALID_PARAM
;
748 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
749 if (!W_ERROR_IS_OK(werr
)) {
750 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
755 werr
= reg_getkeysecurity(mem_ctx
, key
, sd
);
756 if (!W_ERROR_IS_OK(werr
)) {
757 d_fprintf(stderr
, "%s%s\n", _("reg_getkeysecurity failed: "),
769 static int net_registry_getsd(struct net_context
*c
, int argc
,
774 struct security_descriptor
*secdesc
= NULL
;
775 TALLOC_CTX
*ctx
= talloc_stackframe();
777 if (argc
!= 1 || c
->display_usage
) {
780 _("net registry getsd <path>\n"));
783 _("net registry getsd 'HKLM\\Software\\Samba'\n"));
787 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
788 if (!W_ERROR_IS_OK(werr
)) {
792 display_sec_desc(secdesc
);
801 static int net_registry_getsd_sddl(struct net_context
*c
,
802 int argc
, const char **argv
)
806 struct security_descriptor
*secdesc
= NULL
;
807 TALLOC_CTX
*ctx
= talloc_stackframe();
809 if (argc
!= 1 || c
->display_usage
) {
812 _("net registry getsd_sddl <path>\n"));
815 _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
819 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
820 if (!W_ERROR_IS_OK(werr
)) {
824 d_printf("%s\n", sddl_encode(ctx
, secdesc
, get_global_sam_sid()));
833 static WERROR
net_registry_setsd_internal(struct net_context
*c
,
836 struct security_descriptor
*sd
)
839 struct registry_key
*key
= NULL
;
840 TALLOC_CTX
*ctx
= talloc_stackframe();
841 uint32_t access_mask
= REG_KEY_WRITE
|
842 SEC_FLAG_MAXIMUM_ALLOWED
|
843 SEC_FLAG_SYSTEM_SECURITY
;
846 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
847 * is denied with these perms right now...
849 access_mask
= REG_KEY_WRITE
;
851 if (strlen(keyname
) == 0) {
852 d_fprintf(stderr
, _("error: zero length key name given\n"));
853 werr
= WERR_INVALID_PARAM
;
857 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
858 if (!W_ERROR_IS_OK(werr
)) {
859 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
864 werr
= reg_setkeysecurity(key
, sd
);
865 if (!W_ERROR_IS_OK(werr
)) {
866 d_fprintf(stderr
, "%s%s\n", _("reg_setkeysecurity failed: "),
878 static int net_registry_setsd_sddl(struct net_context
*c
,
879 int argc
, const char **argv
)
883 struct security_descriptor
*secdesc
= NULL
;
884 TALLOC_CTX
*ctx
= talloc_stackframe();
886 if (argc
!= 2 || c
->display_usage
) {
889 _("net registry setsd_sddl <path> <security_descriptor>\n"));
892 _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
896 secdesc
= sddl_decode(ctx
, argv
[1], get_global_sam_sid());
897 if (secdesc
== NULL
) {
901 werr
= net_registry_setsd_internal(c
, ctx
, argv
[0], secdesc
);
902 if (!W_ERROR_IS_OK(werr
)) {
913 /******************************************************************************/
915 * @defgroup net_registry net registry
919 * @defgroup net_registry_import Import
920 * @ingroup net_registry
929 static WERROR
import_create_key(struct import_ctx
* ctx
,
930 struct registry_key
* parent
,
931 const char* name
, void** pkey
, bool* existing
)
934 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
936 struct registry_key
* key
= NULL
;
937 enum winreg_CreateAction action
;
939 if (parent
== NULL
) {
940 char* subkeyname
= NULL
;
941 werr
= open_hive(mem_ctx
, name
, REG_KEY_WRITE
,
942 &parent
, &subkeyname
);
943 if (!W_ERROR_IS_OK(werr
)) {
944 d_fprintf(stderr
, _("open_hive failed: %s\n"),
951 action
= REG_ACTION_NONE
;
952 werr
= reg_createkey(mem_ctx
, parent
, name
, REG_KEY_WRITE
,
954 if (!W_ERROR_IS_OK(werr
)) {
955 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
960 if (action
== REG_ACTION_NONE
) {
961 d_fprintf(stderr
, _("createkey did nothing -- huh?\n"));
962 werr
= WERR_CREATE_FAILED
;
966 if (existing
!= NULL
) {
967 *existing
= (action
== REG_OPENED_EXISTING_KEY
);
971 *pkey
= talloc_steal(ctx
->mem_ctx
, key
);
975 talloc_free(mem_ctx
);
979 static WERROR
import_close_key(struct import_ctx
* ctx
,
980 struct registry_key
* key
)
985 static WERROR
import_delete_key(struct import_ctx
* ctx
,
986 struct registry_key
* parent
, const char* name
)
989 void* mem_ctx
= talloc_new(talloc_tos());
991 if (parent
== NULL
) {
992 char* subkeyname
= NULL
;
993 werr
= open_hive(mem_ctx
, name
, REG_KEY_WRITE
,
994 &parent
, &subkeyname
);
995 if (!W_ERROR_IS_OK(werr
)) {
996 d_fprintf(stderr
, _("open_hive failed: %s\n"),
1003 werr
= reg_deletekey_recursive(parent
, name
);
1004 if (!W_ERROR_IS_OK(werr
)) {
1005 d_fprintf(stderr
, "reg_deletekey_recursive %s: %s\n", _("failed"),
1011 talloc_free(mem_ctx
);
1015 static WERROR
import_create_val (struct import_ctx
* ctx
,
1016 struct registry_key
* parent
, const char* name
,
1017 const struct registry_value
* value
)
1021 if (parent
== NULL
) {
1022 return WERR_INVALID_PARAM
;
1025 werr
= reg_setvalue(parent
, name
, value
);
1026 if (!W_ERROR_IS_OK(werr
)) {
1027 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
1033 static WERROR
import_delete_val (struct import_ctx
* ctx
, struct registry_key
* parent
, const char* name
) {
1036 if (parent
== NULL
) {
1037 return WERR_INVALID_PARAM
;
1040 werr
= reg_deletevalue(parent
, name
);
1041 if (!W_ERROR_IS_OK(werr
)) {
1042 d_fprintf(stderr
, _("reg_deletevalue failed: %s\n"),
1050 static int net_registry_import(struct net_context
*c
, int argc
,
1053 struct import_ctx import_ctx
;
1054 struct reg_import_callback import_callback
= {
1056 .closekey
= (reg_import_callback_closekey_t
)&import_close_key
,
1057 .createkey
= (reg_import_callback_createkey_t
)&import_create_key
,
1058 .deletekey
= (reg_import_callback_deletekey_t
)&import_delete_key
,
1059 .deleteval
= (reg_import_callback_deleteval_t
)&import_delete_val
,
1061 .registry_value
= (reg_import_callback_setval_registry_value_t
)
1064 .setval_type
= REGISTRY_VALUE
,
1070 if (argc
< 1 || argc
> 2 || c
->display_usage
) {
1073 _("net registry import <reg> [options]\n"));
1076 _("net registry import file.reg enc=CP1252\n"));
1080 ZERO_STRUCT(import_ctx
);
1081 import_ctx
.mem_ctx
= talloc_stackframe();
1084 regdb_transaction_start();
1086 ret
= reg_parse_file(argv
[0],
1087 reg_import_adapter(import_ctx
.mem_ctx
,
1089 (argc
> 1) ? argv
[1] : NULL
1092 d_printf("reg_parse_file failed: transaction canceled\n");
1093 regdb_transaction_cancel();
1095 regdb_transaction_commit();
1099 talloc_free(import_ctx
.mem_ctx
);
1105 /******************************************************************************/
1108 * @defgroup net_registry_export Export
1109 * @ingroup net_registry
1113 static int registry_export(TALLOC_CTX
*ctx
, /*const*/ struct registry_key
* key
,
1114 struct reg_format
* f
)
1120 struct registry_value
*valvalue
= NULL
;
1121 char *valname
= NULL
;
1123 struct registry_key
* subkey
= NULL
;
1124 char *subkey_name
= NULL
;
1127 reg_format_registry_key(f
, key
, false);
1131 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
1132 W_ERROR_IS_OK(werr
);
1135 reg_format_registry_value(f
, valname
, valvalue
);
1137 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
1138 d_fprintf(stderr
, _("reg_enumvalue failed: %s\n"),
1143 /* recurse on subkeys */
1145 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
1146 W_ERROR_IS_OK(werr
);
1149 werr
= reg_openkey(ctx
, key
, subkey_name
, REG_KEY_READ
,
1151 if (!W_ERROR_IS_OK(werr
)) {
1152 d_fprintf(stderr
, _("reg_openkey failed: %s\n"),
1157 registry_export(ctx
, subkey
, f
);
1159 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
1160 d_fprintf(stderr
, _("reg_enumkey failed: %s\n"),
1169 static int net_registry_export(struct net_context
*c
, int argc
,
1174 struct registry_key
*key
= NULL
;
1175 TALLOC_CTX
*ctx
= talloc_stackframe();
1176 struct reg_format
* f
=NULL
;
1178 if (argc
< 2 || argc
> 3 || c
->display_usage
) {
1181 _("net registry export <path> <file> [opt]\n"));
1184 _("net registry export 'HKLM\\Software\\Samba' "
1185 "samba.reg regedit5\n"));
1189 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
1190 if (!W_ERROR_IS_OK(werr
)) {
1191 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
1195 f
= reg_format_file(ctx
, argv
[1], (argc
> 2) ? argv
[2] : NULL
);
1197 d_fprintf(stderr
, _("open file failed: %s\n"), strerror(errno
));
1201 ret
= registry_export(ctx
, key
, f
);
1209 /******************************************************************************/
1211 * @defgroup net_registry_convert Convert
1212 * @ingroup net_registry
1216 static int net_registry_convert(struct net_context
*c
, int argc
,
1221 const char* in_opt
= NULL
;
1222 const char* out_opt
= NULL
;
1224 if (argc
< 2 || argc
> 4|| c
->display_usage
) {
1227 _("net registry convert <in> <out> [in_opt] [out_opt]\n"
1228 "net registry convert <in> <out> [out_opt]\n"));
1231 _("net registry convert in.reg out.reg regedit4,enc=CP1252\n"));
1235 mem_ctx
= talloc_stackframe();
1252 ret
= reg_parse_file(argv
[0], (struct reg_parse_callback
*)
1253 reg_format_file(mem_ctx
, argv
[1], out_opt
),
1256 talloc_free(mem_ctx
);
1262 /******************************************************************************/
1264 int net_registry(struct net_context
*c
, int argc
, const char **argv
)
1268 struct functable func
[] = {
1271 net_registry_enumerate
,
1272 NET_TRANSPORT_LOCAL
,
1273 N_("Enumerate registry keys and values"),
1274 N_("net registry enumerate\n"
1275 " Enumerate registry keys and values")
1278 "enumerate_recursive",
1279 net_registry_enumerate_recursive
,
1280 NET_TRANSPORT_LOCAL
,
1281 N_("Enumerate registry keys and values"),
1282 N_("net registry enumerate_recursive\n"
1283 " Enumerate registry keys and values")
1287 net_registry_createkey
,
1288 NET_TRANSPORT_LOCAL
,
1289 N_("Create a new registry key"),
1290 N_("net registry createkey\n"
1291 " Create a new registry key")
1295 net_registry_deletekey
,
1296 NET_TRANSPORT_LOCAL
,
1297 N_("Delete a registry key"),
1298 N_("net registry deletekey\n"
1299 " Delete a registry key")
1302 "deletekey_recursive",
1303 net_registry_deletekey_recursive
,
1304 NET_TRANSPORT_LOCAL
,
1305 N_("Delete a registry key with subkeys"),
1306 N_("net registry deletekey_recursive\n"
1307 " Delete a registry key with subkeys")
1311 net_registry_getvalue
,
1312 NET_TRANSPORT_LOCAL
,
1313 N_("Print a registry value"),
1314 N_("net registry getvalue\n"
1315 " Print a registry value")
1319 net_registry_getvalueraw
,
1320 NET_TRANSPORT_LOCAL
,
1321 N_("Print a registry value (raw format)"),
1322 N_("net registry getvalueraw\n"
1323 " Print a registry value (raw format)")
1327 net_registry_getvaluesraw
,
1328 NET_TRANSPORT_LOCAL
,
1329 "Print all values of a key in raw format",
1330 "net registry getvaluesraw <key>\n"
1331 " Print a registry value (raw format)"
1335 net_registry_setvalue
,
1336 NET_TRANSPORT_LOCAL
,
1337 N_("Set a new registry value"),
1338 N_("net registry setvalue\n"
1339 " Set a new registry value")
1343 net_registry_increment
,
1344 NET_TRANSPORT_LOCAL
,
1345 N_("Increment a DWORD registry value under a lock"),
1346 N_("net registry increment\n"
1347 " Increment a DWORD registry value under a lock")
1351 net_registry_deletevalue
,
1352 NET_TRANSPORT_LOCAL
,
1353 N_("Delete a registry value"),
1354 N_("net registry deletevalue\n"
1355 " Delete a registry value")
1360 NET_TRANSPORT_LOCAL
,
1361 N_("Get security descriptor"),
1362 N_("net registry getsd\n"
1363 " Get security descriptor")
1367 net_registry_getsd_sddl
,
1368 NET_TRANSPORT_LOCAL
,
1369 N_("Get security descriptor in sddl format"),
1370 N_("net registry getsd_sddl\n"
1371 " Get security descriptor in sddl format")
1375 net_registry_setsd_sddl
,
1376 NET_TRANSPORT_LOCAL
,
1377 N_("Set security descriptor from sddl format string"),
1378 N_("net registry setsd_sddl\n"
1379 " Set security descriptor from sddl format string")
1383 net_registry_import
,
1384 NET_TRANSPORT_LOCAL
,
1385 N_("Import .reg file"),
1386 N_("net registry import\n"
1387 " Import .reg file")
1391 net_registry_export
,
1392 NET_TRANSPORT_LOCAL
,
1393 N_("Export .reg file"),
1394 N_("net registry export\n"
1395 " Export .reg file")
1399 net_registry_convert
,
1400 NET_TRANSPORT_LOCAL
,
1401 N_("Convert .reg file"),
1402 N_("net registry convert\n"
1403 " Convert .reg file")
1405 { NULL
, NULL
, 0, NULL
, NULL
}
1408 if (!W_ERROR_IS_OK(registry_init_basic())) {
1412 ret
= net_run_function(c
, argc
, argv
, "net registry", func
);