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
);
125 static WERROR
registry_enumkey(struct registry_key
* parent
, const char* keyname
, bool recursive
)
128 TALLOC_CTX
*ctx
= talloc_stackframe();
132 char* valname
= NULL
;
133 struct registry_value
*valvalue
= NULL
;
134 struct registry_key
* key
= NULL
;
136 werr
= reg_openkey(ctx
, parent
, keyname
, REG_KEY_READ
, &key
);
137 if (!W_ERROR_IS_OK(werr
)) {
142 printf("[%s]\n\n", key
->key
->name
);
145 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
149 print_registry_key(subkey_name
, &modtime
);
151 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
157 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
161 print_registry_value_with_name(valname
, valvalue
);
163 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
173 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
177 werr
= registry_enumkey(key
, subkey_name
, recursive
);
178 if (!W_ERROR_IS_OK(werr
)) {
182 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
197 * the main "net registry" function implementations
200 static int net_registry_enumerate(struct net_context
*c
, int argc
,
204 struct registry_key
*key
= NULL
;
206 TALLOC_CTX
*ctx
= talloc_stackframe();
209 if (argc
!= 1 || c
->display_usage
) {
212 _("net registry enumerate <path>\n"));
215 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
219 werr
= open_hive(ctx
, argv
[0], REG_KEY_READ
, &key
, &name
);
220 if (!W_ERROR_IS_OK(werr
)) {
221 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
225 werr
= registry_enumkey(key
, name
, c
->opt_reboot
);
226 if (W_ERROR_IS_OK(werr
)) {
234 static int net_registry_enumerate_recursive(struct net_context
*c
, int argc
,
238 struct registry_key
*key
= NULL
;
240 TALLOC_CTX
*ctx
= talloc_stackframe();
243 if (argc
!= 1 || c
->display_usage
) {
246 _("net registry enumerate <path>\n"));
249 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
253 werr
= open_hive(ctx
, argv
[0], REG_KEY_READ
, &key
, &name
);
254 if (!W_ERROR_IS_OK(werr
)) {
255 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
259 werr
= registry_enumkey(key
, name
, true);
260 if (W_ERROR_IS_OK(werr
)) {
269 static int net_registry_createkey(struct net_context
*c
, int argc
,
273 enum winreg_CreateAction action
;
275 struct registry_key
*hivekey
= NULL
;
276 struct registry_key
*subkey
= NULL
;
277 TALLOC_CTX
*ctx
= talloc_stackframe();
280 if (argc
!= 1 || c
->display_usage
) {
283 _("net registry createkey <path>\n"));
286 _("net registry createkey "
287 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
290 if (strlen(argv
[0]) == 0) {
291 d_fprintf(stderr
, _("error: zero length key name given\n"));
295 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
296 if (!W_ERROR_IS_OK(werr
)) {
297 d_fprintf(stderr
, _("open_hive failed: %s\n"),
302 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
304 if (!W_ERROR_IS_OK(werr
)) {
305 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
310 case REG_ACTION_NONE
:
311 d_printf(_("createkey did nothing -- huh?\n"));
313 case REG_CREATED_NEW_KEY
:
314 d_printf(_("createkey created %s\n"), argv
[0]);
316 case REG_OPENED_EXISTING_KEY
:
317 d_printf(_("createkey opened existing %s\n"), argv
[0]);
328 static int net_registry_deletekey_internal(struct net_context
*c
, int argc
,
334 struct registry_key
*hivekey
= NULL
;
335 TALLOC_CTX
*ctx
= talloc_stackframe();
338 if (argc
!= 1 || c
->display_usage
) {
341 _("net registry deletekey <path>\n"));
344 _("net registry deletekey "
345 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
348 if (strlen(argv
[0]) == 0) {
349 d_fprintf(stderr
, _("error: zero length key name given\n"));
353 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
354 if (!W_ERROR_IS_OK(werr
)) {
355 d_fprintf(stderr
, "open_hive %s: %s\n", _("failed"),
361 werr
= reg_deletekey_recursive(hivekey
, subkeyname
);
363 werr
= reg_deletekey(hivekey
, subkeyname
);
365 if (!W_ERROR_IS_OK(werr
) &&
366 !(c
->opt_force
&& W_ERROR_EQUAL(werr
, WERR_BADFILE
)))
368 d_fprintf(stderr
, "reg_deletekey %s: %s\n", _("failed"),
380 static int net_registry_deletekey(struct net_context
*c
, int argc
,
383 return net_registry_deletekey_internal(c
, argc
, argv
, false);
386 static int net_registry_deletekey_recursive(struct net_context
*c
, int argc
,
389 return net_registry_deletekey_internal(c
, argc
, argv
, true);
392 static int net_registry_getvalue_internal(struct net_context
*c
, int argc
,
393 const char **argv
, bool raw
)
397 struct registry_key
*key
= NULL
;
398 struct registry_value
*value
= NULL
;
399 TALLOC_CTX
*ctx
= talloc_stackframe();
401 if (argc
!= 2 || c
->display_usage
) {
402 d_fprintf(stderr
, "%s\n%s",
404 _("net registry getvalue <key> <valuename>\n"));
408 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
409 if (!W_ERROR_IS_OK(werr
)) {
410 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
414 werr
= reg_queryvalue(ctx
, key
, argv
[1], &value
);
415 if (!W_ERROR_IS_OK(werr
)) {
416 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
421 print_registry_value(value
, raw
);
430 static int net_registry_getvalue(struct net_context
*c
, int argc
,
433 return net_registry_getvalue_internal(c
, argc
, argv
, false);
436 static int net_registry_getvalueraw(struct net_context
*c
, int argc
,
439 return net_registry_getvalue_internal(c
, argc
, argv
, true);
442 static int net_registry_getvaluesraw(struct net_context
*c
, int argc
,
447 struct registry_key
*key
= NULL
;
448 TALLOC_CTX
*ctx
= talloc_stackframe();
451 if (argc
!= 1 || c
->display_usage
) {
452 d_fprintf(stderr
, "usage: net rpc registry getvaluesraw "
457 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
458 if (!W_ERROR_IS_OK(werr
)) {
459 d_fprintf(stderr
, "open_key failed: %s\n", win_errstr(werr
));
465 struct registry_value
*val
;
467 werr
= reg_enumvalue(talloc_tos(), key
, idx
, NULL
, &val
);
469 if (W_ERROR_EQUAL(werr
, WERR_NO_MORE_ITEMS
)) {
473 if (!W_ERROR_IS_OK(werr
)) {
476 print_registry_value(val
, true);
485 static int net_registry_setvalue(struct net_context
*c
, int argc
,
489 struct registry_value value
;
490 struct registry_key
*key
= NULL
;
492 TALLOC_CTX
*ctx
= talloc_stackframe();
494 if (argc
< 4 || c
->display_usage
) {
495 d_fprintf(stderr
, "%s\n%s",
497 _("net registry setvalue <key> <valuename> "
498 "<type> [<val>]+\n"));
502 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
503 d_fprintf(stderr
, _("Too many args for type %s\n"), argv
[2]);
507 if (strequal(argv
[2], "dword")) {
508 uint32_t v
= strtoul(argv
[3], NULL
, 10);
509 value
.type
= REG_DWORD
;
510 value
.data
= data_blob_talloc(ctx
, NULL
, 4);
511 SIVAL(value
.data
.data
, 0, v
);
512 } else if (strequal(argv
[2], "sz")) {
514 if (!push_reg_sz(ctx
, &value
.data
, argv
[3])) {
517 } else if (strequal(argv
[2], "multi_sz")) {
519 int count
= argc
- 3;
521 value
.type
= REG_MULTI_SZ
;
522 array
= talloc_zero_array(ctx
, const char *, count
+ 1);
526 for (i
=0; i
< count
; i
++) {
527 array
[i
] = talloc_strdup(array
, argv
[count
+i
]);
528 if (array
[i
] == NULL
) {
532 if (!push_reg_multi_sz(ctx
, &value
.data
, array
)) {
536 d_fprintf(stderr
, _("type \"%s\" not implemented\n"), argv
[2]);
540 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
541 if (!W_ERROR_IS_OK(werr
)) {
542 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
546 werr
= reg_setvalue(key
, argv
[1], &value
);
547 if (!W_ERROR_IS_OK(werr
)) {
548 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
560 struct net_registry_increment_state
{
562 const char *valuename
;
568 static void net_registry_increment_fn(void *private_data
)
570 struct net_registry_increment_state
*state
=
571 (struct net_registry_increment_state
*)private_data
;
572 struct registry_value
*value
;
573 struct registry_key
*key
= NULL
;
576 state
->werr
= open_key(talloc_tos(), state
->keyname
,
577 REG_KEY_READ
|REG_KEY_WRITE
, &key
);
578 if (!W_ERROR_IS_OK(state
->werr
)) {
579 d_fprintf(stderr
, _("open_key failed: %s\n"),
580 win_errstr(state
->werr
));
584 state
->werr
= reg_queryvalue(key
, key
, state
->valuename
, &value
);
585 if (!W_ERROR_IS_OK(state
->werr
)) {
586 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
587 win_errstr(state
->werr
));
591 if (value
->type
!= REG_DWORD
) {
592 d_fprintf(stderr
, _("value not a DWORD: %s\n"),
593 str_regtype(value
->type
));
597 if (value
->data
.length
< 4) {
598 d_fprintf(stderr
, _("value too short for regular DWORD\n"));
602 v
= IVAL(value
->data
.data
, 0);
603 v
+= state
->increment
;
606 SIVAL(value
->data
.data
, 0, v
);
608 state
->werr
= reg_setvalue(key
, state
->valuename
, value
);
609 if (!W_ERROR_IS_OK(state
->werr
)) {
610 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
611 win_errstr(state
->werr
));
620 static int net_registry_increment(struct net_context
*c
, int argc
,
623 struct net_registry_increment_state state
;
627 if (argc
< 2 || c
->display_usage
) {
628 d_fprintf(stderr
, "%s\n%s",
630 _("net registry increment <key> <valuename> "
635 state
.keyname
= argv
[0];
636 state
.valuename
= argv
[1];
640 state
.increment
= strtoul(argv
[2], NULL
, 10);
643 status
= g_lock_do("registry_increment_lock", G_LOCK_WRITE
,
644 timeval_set(600, 0), procid_self(),
645 net_registry_increment_fn
, &state
);
646 if (!NT_STATUS_IS_OK(status
)) {
647 d_fprintf(stderr
, _("g_lock_do failed: %s\n"),
651 if (!W_ERROR_IS_OK(state
.werr
)) {
652 d_fprintf(stderr
, _("increment failed: %s\n"),
653 win_errstr(state
.werr
));
657 d_printf(_("%u\n"), (unsigned)state
.newvalue
);
665 static int net_registry_deletevalue(struct net_context
*c
, int argc
,
669 struct registry_key
*key
= NULL
;
670 TALLOC_CTX
*ctx
= talloc_stackframe();
673 if (argc
!= 2 || c
->display_usage
) {
674 d_fprintf(stderr
, "%s\n%s",
676 _("net registry deletevalue <key> <valuename>\n"));
680 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
681 if (!W_ERROR_IS_OK(werr
)) {
682 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
686 werr
= reg_deletevalue(key
, argv
[1]);
687 if (!W_ERROR_IS_OK(werr
)) {
688 d_fprintf(stderr
, _("reg_deletevalue failed: %s\n"),
700 static WERROR
net_registry_getsd_internal(struct net_context
*c
,
703 struct security_descriptor
**sd
)
706 struct registry_key
*key
= NULL
;
707 TALLOC_CTX
*ctx
= talloc_stackframe();
708 uint32_t access_mask
= REG_KEY_READ
|
709 SEC_FLAG_MAXIMUM_ALLOWED
|
710 SEC_FLAG_SYSTEM_SECURITY
;
713 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
714 * is denied with these perms right now...
716 access_mask
= REG_KEY_READ
;
719 d_fprintf(stderr
, _("internal error: invalid argument\n"));
720 werr
= WERR_INVALID_PARAM
;
724 if (strlen(keyname
) == 0) {
725 d_fprintf(stderr
, _("error: zero length key name given\n"));
726 werr
= WERR_INVALID_PARAM
;
730 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
731 if (!W_ERROR_IS_OK(werr
)) {
732 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
737 werr
= reg_getkeysecurity(mem_ctx
, key
, sd
);
738 if (!W_ERROR_IS_OK(werr
)) {
739 d_fprintf(stderr
, "%s%s\n", _("reg_getkeysecurity failed: "),
751 static int net_registry_getsd(struct net_context
*c
, int argc
,
756 struct security_descriptor
*secdesc
= NULL
;
757 TALLOC_CTX
*ctx
= talloc_stackframe();
759 if (argc
!= 1 || c
->display_usage
) {
762 _("net registry getsd <path>\n"));
765 _("net registry getsd 'HKLM\\Software\\Samba'\n"));
769 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
770 if (!W_ERROR_IS_OK(werr
)) {
774 display_sec_desc(secdesc
);
783 static int net_registry_getsd_sddl(struct net_context
*c
,
784 int argc
, const char **argv
)
788 struct security_descriptor
*secdesc
= NULL
;
789 TALLOC_CTX
*ctx
= talloc_stackframe();
791 if (argc
!= 1 || c
->display_usage
) {
794 _("net registry getsd_sddl <path>\n"));
797 _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
801 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
802 if (!W_ERROR_IS_OK(werr
)) {
806 d_printf("%s\n", sddl_encode(ctx
, secdesc
, get_global_sam_sid()));
815 static WERROR
net_registry_setsd_internal(struct net_context
*c
,
818 struct security_descriptor
*sd
)
821 struct registry_key
*key
= NULL
;
822 TALLOC_CTX
*ctx
= talloc_stackframe();
823 uint32_t access_mask
= REG_KEY_WRITE
|
824 SEC_FLAG_MAXIMUM_ALLOWED
|
825 SEC_FLAG_SYSTEM_SECURITY
;
828 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
829 * is denied with these perms right now...
831 access_mask
= REG_KEY_WRITE
;
833 if (strlen(keyname
) == 0) {
834 d_fprintf(stderr
, _("error: zero length key name given\n"));
835 werr
= WERR_INVALID_PARAM
;
839 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
840 if (!W_ERROR_IS_OK(werr
)) {
841 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
846 werr
= reg_setkeysecurity(key
, sd
);
847 if (!W_ERROR_IS_OK(werr
)) {
848 d_fprintf(stderr
, "%s%s\n", _("reg_setkeysecurity failed: "),
860 static int net_registry_setsd_sddl(struct net_context
*c
,
861 int argc
, const char **argv
)
865 struct security_descriptor
*secdesc
= NULL
;
866 TALLOC_CTX
*ctx
= talloc_stackframe();
868 if (argc
!= 2 || c
->display_usage
) {
871 _("net registry setsd_sddl <path> <security_descriptor>\n"));
874 _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
878 secdesc
= sddl_decode(ctx
, argv
[1], get_global_sam_sid());
879 if (secdesc
== NULL
) {
883 werr
= net_registry_setsd_internal(c
, ctx
, argv
[0], secdesc
);
884 if (!W_ERROR_IS_OK(werr
)) {
895 /******************************************************************************/
897 * @defgroup net_registry net registry
901 * @defgroup net_registry_import Import
902 * @ingroup net_registry
911 static WERROR
import_create_key(struct import_ctx
* ctx
,
912 struct registry_key
* parent
,
913 const char* name
, void** pkey
, bool* existing
)
916 void* mem_ctx
= talloc_new(ctx
->mem_ctx
);
918 struct registry_key
* key
= NULL
;
919 enum winreg_CreateAction action
;
921 if (parent
== NULL
) {
922 char* subkeyname
= NULL
;
923 werr
= open_hive(mem_ctx
, name
, REG_KEY_WRITE
,
924 &parent
, &subkeyname
);
925 if (!W_ERROR_IS_OK(werr
)) {
926 d_fprintf(stderr
, _("open_hive failed: %s\n"),
933 action
= REG_ACTION_NONE
;
934 werr
= reg_createkey(mem_ctx
, parent
, name
, REG_KEY_WRITE
,
936 if (!W_ERROR_IS_OK(werr
)) {
937 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
942 if (action
== REG_ACTION_NONE
) {
943 d_fprintf(stderr
, _("createkey did nothing -- huh?\n"));
944 werr
= WERR_CREATE_FAILED
;
948 if (existing
!= NULL
) {
949 *existing
= (action
== REG_OPENED_EXISTING_KEY
);
953 *pkey
= talloc_steal(ctx
->mem_ctx
, key
);
957 talloc_free(mem_ctx
);
961 static WERROR
import_close_key(struct import_ctx
* ctx
,
962 struct registry_key
* key
)
967 static WERROR
import_delete_key(struct import_ctx
* ctx
,
968 struct registry_key
* parent
, const char* name
)
971 void* mem_ctx
= talloc_new(talloc_tos());
973 if (parent
== NULL
) {
974 char* subkeyname
= NULL
;
975 werr
= open_hive(mem_ctx
, name
, REG_KEY_WRITE
,
976 &parent
, &subkeyname
);
977 if (!W_ERROR_IS_OK(werr
)) {
978 d_fprintf(stderr
, _("open_hive failed: %s\n"),
985 werr
= reg_deletekey_recursive(parent
, name
);
986 if (!W_ERROR_IS_OK(werr
)) {
987 d_fprintf(stderr
, "reg_deletekey_recursive %s: %s\n", _("failed"),
993 talloc_free(mem_ctx
);
997 static WERROR
import_create_val (struct import_ctx
* ctx
,
998 struct registry_key
* parent
, const char* name
,
999 const struct registry_value
* value
)
1003 if (parent
== NULL
) {
1004 return WERR_INVALID_PARAM
;
1007 werr
= reg_setvalue(parent
, name
, value
);
1008 if (!W_ERROR_IS_OK(werr
)) {
1009 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
1015 static WERROR
import_delete_val (struct import_ctx
* ctx
, struct registry_key
* parent
, const char* name
) {
1018 if (parent
== NULL
) {
1019 return WERR_INVALID_PARAM
;
1022 werr
= reg_deletevalue(parent
, name
);
1023 if (!W_ERROR_IS_OK(werr
)) {
1024 d_fprintf(stderr
, _("reg_deletevalue failed: %s\n"),
1032 static int net_registry_import(struct net_context
*c
, int argc
,
1035 struct import_ctx import_ctx
;
1036 struct reg_import_callback import_callback
= {
1038 .closekey
= (reg_import_callback_closekey_t
)&import_close_key
,
1039 .createkey
= (reg_import_callback_createkey_t
)&import_create_key
,
1040 .deletekey
= (reg_import_callback_deletekey_t
)&import_delete_key
,
1041 .deleteval
= (reg_import_callback_deleteval_t
)&import_delete_val
,
1043 .registry_value
= (reg_import_callback_setval_registry_value_t
)
1046 .setval_type
= REGISTRY_VALUE
,
1052 if (argc
< 1 || argc
> 2 || c
->display_usage
) {
1055 _("net registry import <reg> [options]\n"));
1058 _("net registry import file.reg enc=CP1252\n"));
1062 ZERO_STRUCT(import_ctx
);
1063 import_ctx
.mem_ctx
= talloc_stackframe();
1066 regdb_transaction_start();
1068 ret
= reg_parse_file(argv
[0],
1069 reg_import_adapter(import_ctx
.mem_ctx
,
1071 (argc
> 1) ? argv
[1] : NULL
1074 d_printf("reg_parse_file failed: transaction canceled\n");
1075 regdb_transaction_cancel();
1077 regdb_transaction_commit();
1081 talloc_free(import_ctx
.mem_ctx
);
1087 /******************************************************************************/
1090 * @defgroup net_registry_export Export
1091 * @ingroup net_registry
1095 static int registry_export(TALLOC_CTX
*ctx
, /*const*/ struct registry_key
* key
,
1096 struct reg_format
* f
)
1102 struct registry_value
*valvalue
= NULL
;
1103 char *valname
= NULL
;
1105 char *subkey_name
= NULL
;
1108 reg_format_registry_key(f
, key
, false);
1112 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
1113 W_ERROR_IS_OK(werr
);
1116 reg_format_registry_value(f
, valname
, valvalue
);
1118 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
1119 d_fprintf(stderr
, _("reg_enumvalue failed: %s\n"),
1124 /* recurse on subkeys */
1126 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
1127 W_ERROR_IS_OK(werr
);
1130 struct registry_key
* subkey
= NULL
;
1132 werr
= reg_openkey(ctx
, key
, subkey_name
, REG_KEY_READ
,
1134 if (!W_ERROR_IS_OK(werr
)) {
1135 d_fprintf(stderr
, _("reg_openkey failed: %s\n"),
1140 registry_export(ctx
, subkey
, f
);
1141 TALLOC_FREE(subkey
);
1143 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
1144 d_fprintf(stderr
, _("reg_enumkey failed: %s\n"),
1153 static int net_registry_export(struct net_context
*c
, int argc
,
1158 struct registry_key
*key
= NULL
;
1159 TALLOC_CTX
*ctx
= talloc_stackframe();
1160 struct reg_format
* f
=NULL
;
1162 if (argc
< 2 || argc
> 3 || c
->display_usage
) {
1165 _("net registry export <path> <file> [opt]\n"));
1168 _("net registry export 'HKLM\\Software\\Samba' "
1169 "samba.reg regedit5\n"));
1173 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
1174 if (!W_ERROR_IS_OK(werr
)) {
1175 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
1179 f
= reg_format_file(ctx
, argv
[1], (argc
> 2) ? argv
[2] : NULL
);
1181 d_fprintf(stderr
, _("open file failed: %s\n"), strerror(errno
));
1185 ret
= registry_export(ctx
, key
, f
);
1193 /******************************************************************************/
1195 * @defgroup net_registry_convert Convert
1196 * @ingroup net_registry
1200 static int net_registry_convert(struct net_context
*c
, int argc
,
1205 const char* in_opt
= NULL
;
1206 const char* out_opt
= NULL
;
1208 if (argc
< 2 || argc
> 4|| c
->display_usage
) {
1211 _("net registry convert <in> <out> [in_opt] [out_opt]\n"
1212 "net registry convert <in> <out> [out_opt]\n"));
1215 _("net registry convert in.reg out.reg regedit4,enc=CP1252\n"));
1219 mem_ctx
= talloc_stackframe();
1236 ret
= reg_parse_file(argv
[0], (struct reg_parse_callback
*)
1237 reg_format_file(mem_ctx
, argv
[1], out_opt
),
1240 talloc_free(mem_ctx
);
1246 /******************************************************************************/
1248 int net_registry(struct net_context
*c
, int argc
, const char **argv
)
1252 struct functable func
[] = {
1255 net_registry_enumerate
,
1256 NET_TRANSPORT_LOCAL
,
1257 N_("Enumerate registry keys and values"),
1258 N_("net registry enumerate\n"
1259 " Enumerate registry keys and values")
1262 "enumerate_recursive",
1263 net_registry_enumerate_recursive
,
1264 NET_TRANSPORT_LOCAL
,
1265 N_("Enumerate registry keys and values"),
1266 N_("net registry enumerate_recursive\n"
1267 " Enumerate registry keys and values")
1271 net_registry_createkey
,
1272 NET_TRANSPORT_LOCAL
,
1273 N_("Create a new registry key"),
1274 N_("net registry createkey\n"
1275 " Create a new registry key")
1279 net_registry_deletekey
,
1280 NET_TRANSPORT_LOCAL
,
1281 N_("Delete a registry key"),
1282 N_("net registry deletekey\n"
1283 " Delete a registry key")
1286 "deletekey_recursive",
1287 net_registry_deletekey_recursive
,
1288 NET_TRANSPORT_LOCAL
,
1289 N_("Delete a registry key with subkeys"),
1290 N_("net registry deletekey_recursive\n"
1291 " Delete a registry key with subkeys")
1295 net_registry_getvalue
,
1296 NET_TRANSPORT_LOCAL
,
1297 N_("Print a registry value"),
1298 N_("net registry getvalue\n"
1299 " Print a registry value")
1303 net_registry_getvalueraw
,
1304 NET_TRANSPORT_LOCAL
,
1305 N_("Print a registry value (raw format)"),
1306 N_("net registry getvalueraw\n"
1307 " Print a registry value (raw format)")
1311 net_registry_getvaluesraw
,
1312 NET_TRANSPORT_LOCAL
,
1313 "Print all values of a key in raw format",
1314 "net registry getvaluesraw <key>\n"
1315 " Print a registry value (raw format)"
1319 net_registry_setvalue
,
1320 NET_TRANSPORT_LOCAL
,
1321 N_("Set a new registry value"),
1322 N_("net registry setvalue\n"
1323 " Set a new registry value")
1327 net_registry_increment
,
1328 NET_TRANSPORT_LOCAL
,
1329 N_("Increment a DWORD registry value under a lock"),
1330 N_("net registry increment\n"
1331 " Increment a DWORD registry value under a lock")
1335 net_registry_deletevalue
,
1336 NET_TRANSPORT_LOCAL
,
1337 N_("Delete a registry value"),
1338 N_("net registry deletevalue\n"
1339 " Delete a registry value")
1344 NET_TRANSPORT_LOCAL
,
1345 N_("Get security descriptor"),
1346 N_("net registry getsd\n"
1347 " Get security descriptor")
1351 net_registry_getsd_sddl
,
1352 NET_TRANSPORT_LOCAL
,
1353 N_("Get security descriptor in sddl format"),
1354 N_("net registry getsd_sddl\n"
1355 " Get security descriptor in sddl format")
1359 net_registry_setsd_sddl
,
1360 NET_TRANSPORT_LOCAL
,
1361 N_("Set security descriptor from sddl format string"),
1362 N_("net registry setsd_sddl\n"
1363 " Set security descriptor from sddl format string")
1367 net_registry_import
,
1368 NET_TRANSPORT_LOCAL
,
1369 N_("Import .reg file"),
1370 N_("net registry import\n"
1371 " Import .reg file")
1375 net_registry_export
,
1376 NET_TRANSPORT_LOCAL
,
1377 N_("Export .reg file"),
1378 N_("net registry export\n"
1379 " Export .reg file")
1383 net_registry_convert
,
1384 NET_TRANSPORT_LOCAL
,
1385 N_("Convert .reg file"),
1386 N_("net registry convert\n"
1387 " Convert .reg file")
1389 { NULL
, NULL
, 0, NULL
, NULL
}
1392 if (!W_ERROR_IS_OK(registry_init_basic())) {
1396 ret
= net_run_function(c
, argc
, argv
, "net registry", func
);