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_util_token.h"
25 #include "utils/net.h"
26 #include "utils/net_registry_util.h"
27 #include "include/g_lock.h"
36 * split given path into hive and remaining path and open the hive key
38 static WERROR
open_hive(TALLOC_CTX
*ctx
, const char *path
,
39 uint32 desired_access
,
40 struct registry_key
**hive
,
44 NT_USER_TOKEN
*token
= NULL
;
45 char *hivename
= NULL
;
46 char *tmp_subkeyname
= NULL
;
47 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
49 if ((hive
== NULL
) || (subkeyname
== NULL
)) {
50 werr
= WERR_INVALID_PARAM
;
54 werr
= split_hive_key(tmp_ctx
, path
, &hivename
, &tmp_subkeyname
);
55 if (!W_ERROR_IS_OK(werr
)) {
58 *subkeyname
= talloc_strdup(ctx
, tmp_subkeyname
);
59 if (*subkeyname
== NULL
) {
64 werr
= ntstatus_to_werror(registry_create_admin_token(tmp_ctx
, &token
));
65 if (!W_ERROR_IS_OK(werr
)) {
69 werr
= reg_openhive(ctx
, hivename
, desired_access
, token
, hive
);
70 if (!W_ERROR_IS_OK(werr
)) {
81 static WERROR
open_key(TALLOC_CTX
*ctx
, const char *path
,
82 uint32 desired_access
,
83 struct registry_key
**key
)
86 char *subkey_name
= NULL
;
87 struct registry_key
*hive
= NULL
;
88 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
90 if ((path
== NULL
) || (key
== NULL
)) {
91 return WERR_INVALID_PARAM
;
94 werr
= open_hive(tmp_ctx
, path
, desired_access
, &hive
, &subkey_name
);
95 if (!W_ERROR_IS_OK(werr
)) {
96 d_fprintf(stderr
, _("open_hive failed: %s\n"),
101 werr
= reg_openkey(ctx
, hive
, subkey_name
, desired_access
, key
);
102 if (!W_ERROR_IS_OK(werr
)) {
103 d_fprintf(stderr
, _("reg_openkey failed: %s\n"),
111 TALLOC_FREE(tmp_ctx
);
117 * the main "net registry" function implementations
121 static int net_registry_enumerate(struct net_context
*c
, int argc
,
125 struct registry_key
*key
= NULL
;
126 TALLOC_CTX
*ctx
= talloc_stackframe();
130 char *valname
= NULL
;
131 struct registry_value
*valvalue
= NULL
;
134 if (argc
!= 1 || c
->display_usage
) {
137 _("net registry enumerate <path>\n"));
140 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
144 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
145 if (!W_ERROR_IS_OK(werr
)) {
146 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
151 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
155 print_registry_key(subkey_name
, &modtime
);
157 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
162 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
166 print_registry_value_with_name(valname
, valvalue
);
168 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
178 static int net_registry_createkey(struct net_context
*c
, int argc
,
182 enum winreg_CreateAction action
;
184 struct registry_key
*hivekey
= NULL
;
185 struct registry_key
*subkey
= NULL
;
186 TALLOC_CTX
*ctx
= talloc_stackframe();
189 if (argc
!= 1 || c
->display_usage
) {
192 _("net registry createkey <path>\n"));
195 _("net registry createkey "
196 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
199 if (strlen(argv
[0]) == 0) {
200 d_fprintf(stderr
, _("error: zero length key name given\n"));
204 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
205 if (!W_ERROR_IS_OK(werr
)) {
206 d_fprintf(stderr
, _("open_hive failed: %s\n"),
211 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
213 if (!W_ERROR_IS_OK(werr
)) {
214 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
219 case REG_ACTION_NONE
:
220 d_printf(_("createkey did nothing -- huh?\n"));
222 case REG_CREATED_NEW_KEY
:
223 d_printf(_("createkey created %s\n"), argv
[0]);
225 case REG_OPENED_EXISTING_KEY
:
226 d_printf(_("createkey opened existing %s\n"), argv
[0]);
237 static int net_registry_deletekey(struct net_context
*c
, int argc
,
242 struct registry_key
*hivekey
= NULL
;
243 TALLOC_CTX
*ctx
= talloc_stackframe();
246 if (argc
!= 1 || c
->display_usage
) {
249 _("net registry deletekey <path>\n"));
252 _("net registry deletekey "
253 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
256 if (strlen(argv
[0]) == 0) {
257 d_fprintf(stderr
, _("error: zero length key name given\n"));
261 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
262 if (!W_ERROR_IS_OK(werr
)) {
263 d_fprintf(stderr
, "open_hive %s: %s\n", _("failed"),
268 werr
= reg_deletekey(hivekey
, subkeyname
);
269 if (!W_ERROR_IS_OK(werr
)) {
270 d_fprintf(stderr
, "reg_deletekey %s: %s\n", _("failed"),
282 static int net_registry_getvalue_internal(struct net_context
*c
, int argc
,
283 const char **argv
, bool raw
)
287 struct registry_key
*key
= NULL
;
288 struct registry_value
*value
= NULL
;
289 TALLOC_CTX
*ctx
= talloc_stackframe();
291 if (argc
!= 2 || c
->display_usage
) {
292 d_fprintf(stderr
, "%s\n%s",
294 _("net registry getvalue <key> <valuename>\n"));
298 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
299 if (!W_ERROR_IS_OK(werr
)) {
300 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
304 werr
= reg_queryvalue(ctx
, key
, argv
[1], &value
);
305 if (!W_ERROR_IS_OK(werr
)) {
306 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
311 print_registry_value(value
, raw
);
320 static int net_registry_getvalue(struct net_context
*c
, int argc
,
323 return net_registry_getvalue_internal(c
, argc
, argv
, false);
326 static int net_registry_getvalueraw(struct net_context
*c
, int argc
,
329 return net_registry_getvalue_internal(c
, argc
, argv
, true);
332 static int net_registry_setvalue(struct net_context
*c
, int argc
,
336 struct registry_value value
;
337 struct registry_key
*key
= NULL
;
339 TALLOC_CTX
*ctx
= talloc_stackframe();
341 if (argc
< 4 || c
->display_usage
) {
342 d_fprintf(stderr
, "%s\n%s",
344 _("net registry setvalue <key> <valuename> "
345 "<type> [<val>]+\n"));
349 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
350 d_fprintf(stderr
, _("Too many args for type %s\n"), argv
[2]);
354 if (strequal(argv
[2], "dword")) {
355 uint32_t v
= strtoul(argv
[3], NULL
, 10);
356 value
.type
= REG_DWORD
;
357 value
.data
= data_blob_talloc(ctx
, NULL
, 4);
358 SIVAL(value
.data
.data
, 0, v
);
359 } else if (strequal(argv
[2], "sz")) {
361 if (!push_reg_sz(ctx
, &value
.data
, argv
[3])) {
364 } else if (strequal(argv
[2], "multi_sz")) {
366 int count
= argc
- 3;
368 value
.type
= REG_MULTI_SZ
;
369 array
= talloc_zero_array(ctx
, const char *, count
+ 1);
373 for (i
=0; i
< count
; i
++) {
374 array
[i
] = talloc_strdup(array
, argv
[count
+i
]);
375 if (array
[i
] == NULL
) {
379 if (!push_reg_multi_sz(ctx
, &value
.data
, array
)) {
383 d_fprintf(stderr
, _("type \"%s\" not implemented\n"), argv
[2]);
387 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
388 if (!W_ERROR_IS_OK(werr
)) {
389 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
393 werr
= reg_setvalue(key
, argv
[1], &value
);
394 if (!W_ERROR_IS_OK(werr
)) {
395 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
407 struct net_registry_increment_state
{
409 const char *valuename
;
415 static void net_registry_increment_fn(void *private_data
)
417 struct net_registry_increment_state
*state
=
418 (struct net_registry_increment_state
*)private_data
;
419 struct registry_value
*value
;
420 struct registry_key
*key
= NULL
;
423 state
->werr
= open_key(talloc_tos(), state
->keyname
,
424 REG_KEY_READ
|REG_KEY_WRITE
, &key
);
425 if (!W_ERROR_IS_OK(state
->werr
)) {
426 d_fprintf(stderr
, _("open_key failed: %s\n"),
427 win_errstr(state
->werr
));
431 state
->werr
= reg_queryvalue(key
, key
, state
->valuename
, &value
);
432 if (!W_ERROR_IS_OK(state
->werr
)) {
433 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
434 win_errstr(state
->werr
));
438 if (value
->type
!= REG_DWORD
) {
439 d_fprintf(stderr
, _("value not a DWORD: %s\n"),
440 str_regtype(value
->type
));
444 if (value
->data
.length
< 4) {
445 d_fprintf(stderr
, _("value too short for regular DWORD\n"));
449 v
= IVAL(value
->data
.data
, 0);
450 v
+= state
->increment
;
453 SIVAL(value
->data
.data
, 0, v
);
455 state
->werr
= reg_setvalue(key
, state
->valuename
, value
);
456 if (!W_ERROR_IS_OK(state
->werr
)) {
457 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
458 win_errstr(state
->werr
));
467 static int net_registry_increment(struct net_context
*c
, int argc
,
470 struct net_registry_increment_state state
;
474 if (argc
< 2 || c
->display_usage
) {
475 d_fprintf(stderr
, "%s\n%s",
477 _("net registry increment <key> <valuename> "
482 state
.keyname
= argv
[0];
483 state
.valuename
= argv
[1];
487 state
.increment
= strtoul(argv
[2], NULL
, 10);
490 status
= g_lock_do("registry_increment_lock", G_LOCK_WRITE
,
491 timeval_set(600, 0), procid_self(),
492 net_registry_increment_fn
, &state
);
493 if (!NT_STATUS_IS_OK(status
)) {
494 d_fprintf(stderr
, _("g_lock_do failed: %s\n"),
498 if (!W_ERROR_IS_OK(state
.werr
)) {
499 d_fprintf(stderr
, _("increment failed: %s\n"),
500 win_errstr(state
.werr
));
504 d_printf(_("%u\n"), (unsigned)state
.newvalue
);
512 static int net_registry_deletevalue(struct net_context
*c
, int argc
,
516 struct registry_key
*key
= NULL
;
517 TALLOC_CTX
*ctx
= talloc_stackframe();
520 if (argc
!= 2 || c
->display_usage
) {
521 d_fprintf(stderr
, "%s\n%s",
523 _("net registry deletevalue <key> <valuename>\n"));
527 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
528 if (!W_ERROR_IS_OK(werr
)) {
529 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
533 werr
= reg_deletevalue(key
, argv
[1]);
534 if (!W_ERROR_IS_OK(werr
)) {
535 d_fprintf(stderr
, _("reg_deletekey failed: %s\n"),
547 static WERROR
net_registry_getsd_internal(struct net_context
*c
,
550 struct security_descriptor
**sd
)
553 struct registry_key
*key
= NULL
;
554 TALLOC_CTX
*ctx
= talloc_stackframe();
555 uint32_t access_mask
= REG_KEY_READ
|
556 SEC_FLAG_MAXIMUM_ALLOWED
|
557 SEC_FLAG_SYSTEM_SECURITY
;
560 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
561 * is denied with these perms right now...
563 access_mask
= REG_KEY_READ
;
566 d_fprintf(stderr
, _("internal error: invalid argument\n"));
567 werr
= WERR_INVALID_PARAM
;
571 if (strlen(keyname
) == 0) {
572 d_fprintf(stderr
, _("error: zero length key name given\n"));
573 werr
= WERR_INVALID_PARAM
;
577 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
578 if (!W_ERROR_IS_OK(werr
)) {
579 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
584 werr
= reg_getkeysecurity(mem_ctx
, key
, sd
);
585 if (!W_ERROR_IS_OK(werr
)) {
586 d_fprintf(stderr
, "%s%s\n", _("reg_getkeysecurity failed: "),
598 static int net_registry_getsd(struct net_context
*c
, int argc
,
603 struct security_descriptor
*secdesc
= NULL
;
604 TALLOC_CTX
*ctx
= talloc_stackframe();
606 if (argc
!= 1 || c
->display_usage
) {
609 _("net registry getsd <path>\n"));
612 _("net registry getsd 'HKLM\\Software\\Samba'\n"));
616 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
617 if (!W_ERROR_IS_OK(werr
)) {
621 display_sec_desc(secdesc
);
630 static int net_registry_getsd_sddl(struct net_context
*c
,
631 int argc
, const char **argv
)
635 struct security_descriptor
*secdesc
= NULL
;
636 TALLOC_CTX
*ctx
= talloc_stackframe();
638 if (argc
!= 1 || c
->display_usage
) {
641 _("net registry getsd_sddl <path>\n"));
644 _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
648 werr
= net_registry_getsd_internal(c
, ctx
, argv
[0], &secdesc
);
649 if (!W_ERROR_IS_OK(werr
)) {
653 d_printf("%s\n", sddl_encode(ctx
, secdesc
, get_global_sam_sid()));
662 static WERROR
net_registry_setsd_internal(struct net_context
*c
,
665 struct security_descriptor
*sd
)
668 struct registry_key
*key
= NULL
;
669 TALLOC_CTX
*ctx
= talloc_stackframe();
670 uint32_t access_mask
= REG_KEY_WRITE
|
671 SEC_FLAG_MAXIMUM_ALLOWED
|
672 SEC_FLAG_SYSTEM_SECURITY
;
675 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
676 * is denied with these perms right now...
678 access_mask
= REG_KEY_WRITE
;
680 if (strlen(keyname
) == 0) {
681 d_fprintf(stderr
, _("error: zero length key name given\n"));
682 werr
= WERR_INVALID_PARAM
;
686 werr
= open_key(ctx
, keyname
, access_mask
, &key
);
687 if (!W_ERROR_IS_OK(werr
)) {
688 d_fprintf(stderr
, "%s%s\n", _("open_key failed: "),
693 werr
= reg_setkeysecurity(key
, sd
);
694 if (!W_ERROR_IS_OK(werr
)) {
695 d_fprintf(stderr
, "%s%s\n", _("reg_setkeysecurity failed: "),
707 static int net_registry_setsd_sddl(struct net_context
*c
,
708 int argc
, const char **argv
)
712 struct security_descriptor
*secdesc
= NULL
;
713 TALLOC_CTX
*ctx
= talloc_stackframe();
715 if (argc
!= 2 || c
->display_usage
) {
718 _("net registry setsd_sddl <path> <security_descriptor>\n"));
721 _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
725 secdesc
= sddl_decode(ctx
, argv
[1], get_global_sam_sid());
726 if (secdesc
== NULL
) {
730 werr
= net_registry_setsd_internal(c
, ctx
, argv
[0], secdesc
);
731 if (!W_ERROR_IS_OK(werr
)) {
742 int net_registry(struct net_context
*c
, int argc
, const char **argv
)
746 struct functable func
[] = {
749 net_registry_enumerate
,
751 N_("Enumerate registry keys and values"),
752 N_("net registry enumerate\n"
753 " Enumerate registry keys and values")
757 net_registry_createkey
,
759 N_("Create a new registry key"),
760 N_("net registry createkey\n"
761 " Create a new registry key")
765 net_registry_deletekey
,
767 N_("Delete a registry key"),
768 N_("net registry deletekey\n"
769 " Delete a registry key")
773 net_registry_getvalue
,
775 N_("Print a registry value"),
776 N_("net registry getvalue\n"
777 " Print a registry value")
781 net_registry_getvalueraw
,
783 N_("Print a registry value (raw format)"),
784 N_("net registry getvalueraw\n"
785 " Print a registry value (raw format)")
789 net_registry_setvalue
,
791 N_("Set a new registry value"),
792 N_("net registry setvalue\n"
793 " Set a new registry value")
797 net_registry_increment
,
799 N_("Increment a DWORD registry value under a lock"),
800 N_("net registry increment\n"
801 " Increment a DWORD registry value under a lock")
805 net_registry_deletevalue
,
807 N_("Delete a registry value"),
808 N_("net registry deletevalue\n"
809 " Delete a registry value")
815 N_("Get security descriptor"),
816 N_("net registry getsd\n"
817 " Get security descriptor")
821 net_registry_getsd_sddl
,
823 N_("Get security descriptor in sddl format"),
824 N_("net registry getsd_sddl\n"
825 " Get security descriptor in sddl format")
829 net_registry_setsd_sddl
,
831 N_("Set security descriptor from sddl format string"),
832 N_("net registry setsd_sddl\n"
833 " Set security descriptor from sddl format string")
835 { NULL
, NULL
, 0, NULL
, NULL
}
838 if (!W_ERROR_IS_OK(registry_init_basic())) {
842 ret
= net_run_function(c
, argc
, argv
, "net registry", func
);