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"
41 * split given path into hive and remaining path and open the hive key
43 static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
44 uint32 desired_access,
45 struct registry_key **hive,
49 struct security_token *token = NULL;
50 char *hivename = NULL;
51 char *tmp_subkeyname = NULL;
52 TALLOC_CTX *tmp_ctx = talloc_stackframe();
54 if ((hive == NULL) || (subkeyname == NULL)) {
55 werr = WERR_INVALID_PARAM;
59 werr = split_hive_key(tmp_ctx, path, &hivename, &tmp_subkeyname);
60 if (!W_ERROR_IS_OK(werr)) {
63 *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
64 if (*subkeyname == NULL) {
69 werr = ntstatus_to_werror(registry_create_admin_token(tmp_ctx, &token));
70 if (!W_ERROR_IS_OK(werr)) {
74 werr = reg_openhive(ctx, hivename, desired_access, token, hive);
75 if (!W_ERROR_IS_OK(werr)) {
86 static WERROR open_key(TALLOC_CTX *ctx, const char *path,
87 uint32 desired_access,
88 struct registry_key **key)
91 char *subkey_name = NULL;
92 struct registry_key *hive = NULL;
93 TALLOC_CTX *tmp_ctx = talloc_stackframe();
95 if ((path == NULL) || (key == NULL)) {
96 return WERR_INVALID_PARAM;
99 werr = open_hive(tmp_ctx, path, desired_access, &hive, &subkey_name);
100 if (!W_ERROR_IS_OK(werr)) {
101 d_fprintf(stderr, _("open_hive failed: %s\n"),
106 werr = reg_openkey(ctx, hive, subkey_name, desired_access, key);
107 if (!W_ERROR_IS_OK(werr)) {
108 d_fprintf(stderr, _("reg_openkey failed: %s\n"),
116 TALLOC_FREE(tmp_ctx);
122 * the main "net registry" function implementations
126 static int net_registry_enumerate(struct net_context *c, int argc,
130 struct registry_key *key = NULL;
131 TALLOC_CTX *ctx = talloc_stackframe();
135 char *valname = NULL;
136 struct registry_value *valvalue = NULL;
139 if (argc != 1 || c->display_usage) {
142 _("net registry enumerate <path>\n"));
145 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
149 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
150 if (!W_ERROR_IS_OK(werr)) {
151 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
156 werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
160 print_registry_key(subkey_name, &modtime);
162 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
167 werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
171 print_registry_value_with_name(valname, valvalue);
173 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
183 static int net_registry_createkey(struct net_context *c, int argc,
187 enum winreg_CreateAction action;
189 struct registry_key *hivekey = NULL;
190 struct registry_key *subkey = NULL;
191 TALLOC_CTX *ctx = talloc_stackframe();
194 if (argc != 1 || c->display_usage) {
197 _("net registry createkey <path>\n"));
200 _("net registry createkey "
201 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
204 if (strlen(argv[0]) == 0) {
205 d_fprintf(stderr, _("error: zero length key name given\n"));
209 werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
210 if (!W_ERROR_IS_OK(werr)) {
211 d_fprintf(stderr, _("open_hive failed: %s\n"),
216 werr = reg_createkey(ctx, hivekey, subkeyname, REG_KEY_WRITE,
218 if (!W_ERROR_IS_OK(werr)) {
219 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
224 case REG_ACTION_NONE:
225 d_printf(_("createkey did nothing -- huh?\n"));
227 case REG_CREATED_NEW_KEY:
228 d_printf(_("createkey created %s\n"), argv[0]);
230 case REG_OPENED_EXISTING_KEY:
231 d_printf(_("createkey opened existing %s\n"), argv[0]);
242 static int net_registry_deletekey(struct net_context *c, int argc,
247 struct registry_key *hivekey = NULL;
248 TALLOC_CTX *ctx = talloc_stackframe();
251 if (argc != 1 || c->display_usage) {
254 _("net registry deletekey <path>\n"));
257 _("net registry deletekey "
258 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
261 if (strlen(argv[0]) == 0) {
262 d_fprintf(stderr, _("error: zero length key name given\n"));
266 werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
267 if (!W_ERROR_IS_OK(werr)) {
268 d_fprintf(stderr, "open_hive %s: %s\n", _("failed"),
273 werr = reg_deletekey(hivekey, subkeyname);
274 if (!W_ERROR_IS_OK(werr)) {
275 d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
287 static int net_registry_getvalue_internal(struct net_context *c, int argc,
288 const char **argv, bool raw)
292 struct registry_key *key = NULL;
293 struct registry_value *value = NULL;
294 TALLOC_CTX *ctx = talloc_stackframe();
296 if (argc != 2 || c->display_usage) {
297 d_fprintf(stderr, "%s\n%s",
299 _("net registry getvalue <key> <valuename>\n"));
303 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
304 if (!W_ERROR_IS_OK(werr)) {
305 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
309 werr = reg_queryvalue(ctx, key, argv[1], &value);
310 if (!W_ERROR_IS_OK(werr)) {
311 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
316 print_registry_value(value, raw);
325 static int net_registry_getvalue(struct net_context *c, int argc,
328 return net_registry_getvalue_internal(c, argc, argv, false);
331 static int net_registry_getvalueraw(struct net_context *c, int argc,
334 return net_registry_getvalue_internal(c, argc, argv, true);
337 static int net_registry_setvalue(struct net_context *c, int argc,
341 struct registry_value value;
342 struct registry_key *key = NULL;
344 TALLOC_CTX *ctx = talloc_stackframe();
346 if (argc < 4 || c->display_usage) {
347 d_fprintf(stderr, "%s\n%s",
349 _("net registry setvalue <key> <valuename> "
350 "<type> [<val>]+\n"));
354 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
355 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
359 if (strequal(argv[2], "dword")) {
360 uint32_t v = strtoul(argv[3], NULL, 10);
361 value.type = REG_DWORD;
362 value.data = data_blob_talloc(ctx, NULL, 4);
363 SIVAL(value.data.data, 0, v);
364 } else if (strequal(argv[2], "sz")) {
366 if (!push_reg_sz(ctx, &value.data, argv[3])) {
369 } else if (strequal(argv[2], "multi_sz")) {
371 int count = argc - 3;
373 value.type = REG_MULTI_SZ;
374 array = talloc_zero_array(ctx, const char *, count + 1);
378 for (i=0; i < count; i++) {
379 array[i] = talloc_strdup(array, argv[count+i]);
380 if (array[i] == NULL) {
384 if (!push_reg_multi_sz(ctx, &value.data, array)) {
388 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
392 werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
393 if (!W_ERROR_IS_OK(werr)) {
394 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
398 werr = reg_setvalue(key, argv[1], &value);
399 if (!W_ERROR_IS_OK(werr)) {
400 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
412 struct net_registry_increment_state {
414 const char *valuename;
420 static void net_registry_increment_fn(void *private_data)
422 struct net_registry_increment_state *state =
423 (struct net_registry_increment_state *)private_data;
424 struct registry_value *value;
425 struct registry_key *key = NULL;
428 state->werr = open_key(talloc_tos(), state->keyname,
429 REG_KEY_READ|REG_KEY_WRITE, &key);
430 if (!W_ERROR_IS_OK(state->werr)) {
431 d_fprintf(stderr, _("open_key failed: %s\n"),
432 win_errstr(state->werr));
436 state->werr = reg_queryvalue(key, key, state->valuename, &value);
437 if (!W_ERROR_IS_OK(state->werr)) {
438 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
439 win_errstr(state->werr));
443 if (value->type != REG_DWORD) {
444 d_fprintf(stderr, _("value not a DWORD: %s\n"),
445 str_regtype(value->type));
449 if (value->data.length < 4) {
450 d_fprintf(stderr, _("value too short for regular DWORD\n"));
454 v = IVAL(value->data.data, 0);
455 v += state->increment;
458 SIVAL(value->data.data, 0, v);
460 state->werr = reg_setvalue(key, state->valuename, value);
461 if (!W_ERROR_IS_OK(state->werr)) {
462 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
463 win_errstr(state->werr));
472 static int net_registry_increment(struct net_context *c, int argc,
475 struct net_registry_increment_state state;
479 if (argc < 2 || c->display_usage) {
480 d_fprintf(stderr, "%s\n%s",
482 _("net registry increment <key> <valuename> "
487 state.keyname = argv[0];
488 state.valuename = argv[1];
492 state.increment = strtoul(argv[2], NULL, 10);
495 status = g_lock_do("registry_increment_lock", G_LOCK_WRITE,
496 timeval_set(600, 0), procid_self(),
497 net_registry_increment_fn, &state);
498 if (!NT_STATUS_IS_OK(status)) {
499 d_fprintf(stderr, _("g_lock_do failed: %s\n"),
503 if (!W_ERROR_IS_OK(state.werr)) {
504 d_fprintf(stderr, _("increment failed: %s\n"),
505 win_errstr(state.werr));
509 d_printf(_("%u\n"), (unsigned)state.newvalue);
517 static int net_registry_deletevalue(struct net_context *c, int argc,
521 struct registry_key *key = NULL;
522 TALLOC_CTX *ctx = talloc_stackframe();
525 if (argc != 2 || c->display_usage) {
526 d_fprintf(stderr, "%s\n%s",
528 _("net registry deletevalue <key> <valuename>\n"));
532 werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
533 if (!W_ERROR_IS_OK(werr)) {
534 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
538 werr = reg_deletevalue(key, argv[1]);
539 if (!W_ERROR_IS_OK(werr)) {
540 d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
552 static WERROR net_registry_getsd_internal(struct net_context *c,
555 struct security_descriptor **sd)
558 struct registry_key *key = NULL;
559 TALLOC_CTX *ctx = talloc_stackframe();
560 uint32_t access_mask = REG_KEY_READ |
561 SEC_FLAG_MAXIMUM_ALLOWED |
562 SEC_FLAG_SYSTEM_SECURITY;
565 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
566 * is denied with these perms right now...
568 access_mask = REG_KEY_READ;
571 d_fprintf(stderr, _("internal error: invalid argument\n"));
572 werr = WERR_INVALID_PARAM;
576 if (strlen(keyname) == 0) {
577 d_fprintf(stderr, _("error: zero length key name given\n"));
578 werr = WERR_INVALID_PARAM;
582 werr = open_key(ctx, keyname, access_mask, &key);
583 if (!W_ERROR_IS_OK(werr)) {
584 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
589 werr = reg_getkeysecurity(mem_ctx, key, sd);
590 if (!W_ERROR_IS_OK(werr)) {
591 d_fprintf(stderr, "%s%s\n", _("reg_getkeysecurity failed: "),
603 static int net_registry_getsd(struct net_context *c, int argc,
608 struct security_descriptor *secdesc = NULL;
609 TALLOC_CTX *ctx = talloc_stackframe();
611 if (argc != 1 || c->display_usage) {
614 _("net registry getsd <path>\n"));
617 _("net registry getsd 'HKLM\\Software\\Samba'\n"));
621 werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
622 if (!W_ERROR_IS_OK(werr)) {
626 display_sec_desc(secdesc);
635 static int net_registry_getsd_sddl(struct net_context *c,
636 int argc, const char **argv)
640 struct security_descriptor *secdesc = NULL;
641 TALLOC_CTX *ctx = talloc_stackframe();
643 if (argc != 1 || c->display_usage) {
646 _("net registry getsd_sddl <path>\n"));
649 _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
653 werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
654 if (!W_ERROR_IS_OK(werr)) {
658 d_printf("%s\n", sddl_encode(ctx, secdesc, get_global_sam_sid()));
667 static WERROR net_registry_setsd_internal(struct net_context *c,
670 struct security_descriptor *sd)
673 struct registry_key *key = NULL;
674 TALLOC_CTX *ctx = talloc_stackframe();
675 uint32_t access_mask = REG_KEY_WRITE |
676 SEC_FLAG_MAXIMUM_ALLOWED |
677 SEC_FLAG_SYSTEM_SECURITY;
680 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
681 * is denied with these perms right now...
683 access_mask = REG_KEY_WRITE;
685 if (strlen(keyname) == 0) {
686 d_fprintf(stderr, _("error: zero length key name given\n"));
687 werr = WERR_INVALID_PARAM;
691 werr = open_key(ctx, keyname, access_mask, &key);
692 if (!W_ERROR_IS_OK(werr)) {
693 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
698 werr = reg_setkeysecurity(key, sd);
699 if (!W_ERROR_IS_OK(werr)) {
700 d_fprintf(stderr, "%s%s\n", _("reg_setkeysecurity failed: "),
712 static int net_registry_setsd_sddl(struct net_context *c,
713 int argc, const char **argv)
717 struct security_descriptor *secdesc = NULL;
718 TALLOC_CTX *ctx = talloc_stackframe();
720 if (argc != 2 || c->display_usage) {
723 _("net registry setsd_sddl <path> <security_descriptor>\n"));
726 _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
730 secdesc = sddl_decode(ctx, argv[1], get_global_sam_sid());
731 if (secdesc == NULL) {
735 werr = net_registry_setsd_internal(c, ctx, argv[0], secdesc);
736 if (!W_ERROR_IS_OK(werr)) {
747 /******************************************************************************/
749 * @defgroup net_registry net registry
753 * @defgroup net_registry_import Import
754 * @ingroup net_registry
763 static WERROR import_create_key(struct import_ctx* ctx,
764 struct registry_key* parent,
765 const char* name, void** pkey, bool* existing)
768 void* mem_ctx = talloc_new(ctx->mem_ctx);
770 struct registry_key* key = NULL;
771 enum winreg_CreateAction action;
773 if (parent == NULL) {
774 char* subkeyname = NULL;
775 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
776 &parent, &subkeyname);
777 if (!W_ERROR_IS_OK(werr)) {
778 d_fprintf(stderr, _("open_hive failed: %s\n"),
785 action = REG_ACTION_NONE;
786 werr = reg_createkey(mem_ctx, parent, name, REG_KEY_WRITE,
788 if (!W_ERROR_IS_OK(werr)) {
789 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
794 if (action == REG_ACTION_NONE) {
795 d_fprintf(stderr, _("createkey did nothing -- huh?\n"));
796 werr = WERR_CREATE_FAILED;
800 if (existing != NULL) {
801 *existing = (action == REG_OPENED_EXISTING_KEY);
805 *pkey = talloc_steal(ctx->mem_ctx, key);
809 talloc_free(mem_ctx);
813 static WERROR import_close_key(struct import_ctx* ctx,
814 struct registry_key* key)
819 static WERROR import_delete_key(struct import_ctx* ctx,
820 struct registry_key* parent, const char* name)
823 void* mem_ctx = talloc_new(talloc_tos());
825 if (parent == NULL) {
826 char* subkeyname = NULL;
827 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
828 &parent, &subkeyname);
829 if (!W_ERROR_IS_OK(werr)) {
830 d_fprintf(stderr, _("open_hive failed: %s\n"),
837 werr = reg_deletekey_recursive(mem_ctx, parent, name);
838 if (!W_ERROR_IS_OK(werr)) {
839 d_fprintf(stderr, "reg_deletekey_recursive %s: %s\n", _("failed"),
845 talloc_free(mem_ctx);
849 static WERROR import_create_val (struct import_ctx* ctx,
850 struct registry_key* parent, const char* name,
851 const struct registry_value* value)
855 if (parent == NULL) {
856 return WERR_INVALID_PARAM;
859 werr = reg_setvalue(parent, name, value);
860 if (!W_ERROR_IS_OK(werr)) {
861 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
867 static WERROR import_delete_val (struct import_ctx* ctx, struct registry_key* parent, const char* name) {
870 if (parent == NULL) {
871 return WERR_INVALID_PARAM;
874 werr = reg_deletevalue(parent, name);
875 if (!W_ERROR_IS_OK(werr)) {
876 d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
884 static int net_registry_import(struct net_context *c, int argc,
887 struct import_ctx import_ctx;
888 struct reg_import_callback import_callback = {
890 .closekey = (reg_import_callback_closekey_t)&import_close_key,
891 .createkey = (reg_import_callback_createkey_t)&import_create_key,
892 .deletekey = (reg_import_callback_deletekey_t)&import_delete_key,
893 .deleteval = (reg_import_callback_deleteval_t)&import_delete_val,
894 .setval.registry_value = (reg_import_callback_setval_registry_value_t)
896 .setval_type = REGISTRY_VALUE,
902 if (argc < 1 || argc > 2 || c->display_usage) {
905 _("net registry import <reg> [options]\n"));
908 _("net registry import file.reg enc=CP1252\n"));
912 ZERO_STRUCT(import_ctx);
913 import_ctx.mem_ctx = talloc_stackframe();
916 regdb_transaction_start();
918 ret = reg_parse_file(argv[0],
919 reg_import_adapter(import_ctx.mem_ctx,
921 (argc > 1) ? argv[1] : NULL
924 d_printf("reg_parse_file failed: transaction canceled\n");
925 regdb_transaction_cancel();
927 regdb_transaction_commit();
931 talloc_free(import_ctx.mem_ctx);
938 /******************************************************************************/
939 int net_registry(struct net_context *c, int argc, const char **argv)
943 struct functable func[] = {
946 net_registry_enumerate,
948 N_("Enumerate registry keys and values"),
949 N_("net registry enumerate\n"
950 " Enumerate registry keys and values")
954 net_registry_createkey,
956 N_("Create a new registry key"),
957 N_("net registry createkey\n"
958 " Create a new registry key")
962 net_registry_deletekey,
964 N_("Delete a registry key"),
965 N_("net registry deletekey\n"
966 " Delete a registry key")
970 net_registry_getvalue,
972 N_("Print a registry value"),
973 N_("net registry getvalue\n"
974 " Print a registry value")
978 net_registry_getvalueraw,
980 N_("Print a registry value (raw format)"),
981 N_("net registry getvalueraw\n"
982 " Print a registry value (raw format)")
986 net_registry_setvalue,
988 N_("Set a new registry value"),
989 N_("net registry setvalue\n"
990 " Set a new registry value")
994 net_registry_increment,
996 N_("Increment a DWORD registry value under a lock"),
997 N_("net registry increment\n"
998 " Increment a DWORD registry value under a lock")
1002 net_registry_deletevalue,
1003 NET_TRANSPORT_LOCAL,
1004 N_("Delete a registry value"),
1005 N_("net registry deletevalue\n"
1006 " Delete a registry value")
1011 NET_TRANSPORT_LOCAL,
1012 N_("Get security descriptor"),
1013 N_("net registry getsd\n"
1014 " Get security descriptor")
1018 net_registry_getsd_sddl,
1019 NET_TRANSPORT_LOCAL,
1020 N_("Get security descriptor in sddl format"),
1021 N_("net registry getsd_sddl\n"
1022 " Get security descriptor in sddl format")
1026 net_registry_setsd_sddl,
1027 NET_TRANSPORT_LOCAL,
1028 N_("Set security descriptor from sddl format string"),
1029 N_("net registry setsd_sddl\n"
1030 " Set security descriptor from sddl format string")
1034 net_registry_import,
1035 NET_TRANSPORT_LOCAL,
1036 N_("Import .reg file"),
1037 N_("net registry import\n"
1038 " Import .reg file")
1040 { NULL, NULL, 0, NULL, NULL }
1043 if (!W_ERROR_IS_OK(registry_init_basic())) {
1047 ret = net_run_function(c, argc, argv, "net registry", func);