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/>.
23 #include "utils/net.h"
24 #include "utils/net_registry_util.h"
34 * split given path into hive and remaining path and open the hive key
36 static WERROR
open_hive(TALLOC_CTX
*ctx
, const char *path
,
37 uint32 desired_access
,
38 struct registry_key
**hive
,
42 NT_USER_TOKEN
*token
= NULL
;
43 char *hivename
= NULL
;
44 char *tmp_subkeyname
= NULL
;
45 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
47 if ((hive
== NULL
) || (subkeyname
== NULL
)) {
48 werr
= WERR_INVALID_PARAM
;
52 werr
= split_hive_key(tmp_ctx
, path
, &hivename
, &tmp_subkeyname
);
53 if (!W_ERROR_IS_OK(werr
)) {
56 *subkeyname
= talloc_strdup(ctx
, tmp_subkeyname
);
57 if (*subkeyname
== NULL
) {
62 werr
= ntstatus_to_werror(registry_create_admin_token(tmp_ctx
, &token
));
63 if (!W_ERROR_IS_OK(werr
)) {
67 werr
= reg_openhive(ctx
, hivename
, desired_access
, token
, hive
);
68 if (!W_ERROR_IS_OK(werr
)) {
79 static WERROR
open_key(TALLOC_CTX
*ctx
, const char *path
,
80 uint32 desired_access
,
81 struct registry_key
**key
)
84 char *subkey_name
= NULL
;
85 struct registry_key
*hive
= NULL
;
86 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
88 if ((path
== NULL
) || (key
== NULL
)) {
89 return WERR_INVALID_PARAM
;
92 werr
= open_hive(tmp_ctx
, path
, desired_access
, &hive
, &subkey_name
);
93 if (!W_ERROR_IS_OK(werr
)) {
94 d_fprintf(stderr
, _("open_hive failed: %s\n"),
99 werr
= reg_openkey(ctx
, hive
, subkey_name
, desired_access
, key
);
100 if (!W_ERROR_IS_OK(werr
)) {
101 d_fprintf(stderr
, _("reg_openkey failed: %s\n"),
109 TALLOC_FREE(tmp_ctx
);
115 * the main "net registry" function implementations
119 static int net_registry_enumerate(struct net_context
*c
, int argc
,
123 struct registry_key
*key
= NULL
;
124 TALLOC_CTX
*ctx
= talloc_stackframe();
128 char *valname
= NULL
;
129 struct registry_value
*valvalue
= NULL
;
132 if (argc
!= 1 || c
->display_usage
) {
133 d_printf(_("Usage: net registry enumerate <path>\n"));
134 d_printf(_("Example: net registry enumerate "
135 "'HKLM\\Software\\Samba'\n"));
139 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
140 if (!W_ERROR_IS_OK(werr
)) {
141 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
146 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
150 print_registry_key(subkey_name
, &modtime
);
152 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 static int net_registry_createkey(struct net_context
*c
, int argc
,
177 enum winreg_CreateAction action
;
179 struct registry_key
*hivekey
= NULL
;
180 struct registry_key
*subkey
= NULL
;
181 TALLOC_CTX
*ctx
= talloc_stackframe();
184 if (argc
!= 1 || c
->display_usage
) {
185 d_printf(_("Usage: net registry createkey <path>\n"));
186 d_printf(_("Example: net registry createkey "
187 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
190 if (strlen(argv
[0]) == 0) {
191 d_fprintf(stderr
, _("error: zero length key name given\n"));
195 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
196 if (!W_ERROR_IS_OK(werr
)) {
197 d_fprintf(stderr
, _("open_hive failed: %s\n"),
202 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
204 if (!W_ERROR_IS_OK(werr
)) {
205 d_fprintf(stderr
, _("reg_createkey failed: %s\n"),
210 case REG_ACTION_NONE
:
211 d_printf(_("createkey did nothing -- huh?\n"));
213 case REG_CREATED_NEW_KEY
:
214 d_printf(_("createkey created %s\n"), argv
[0]);
216 case REG_OPENED_EXISTING_KEY
:
217 d_printf(_("createkey opened existing %s\n"), argv
[0]);
228 static int net_registry_deletekey(struct net_context
*c
, int argc
,
233 struct registry_key
*hivekey
= NULL
;
234 TALLOC_CTX
*ctx
= talloc_stackframe();
237 if (argc
!= 1 || c
->display_usage
) {
238 d_printf(_("Usage: net registry deletekey <path>\n"));
239 d_printf(_("Example: net registry deletekey "
240 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
243 if (strlen(argv
[0]) == 0) {
244 d_fprintf(stderr
, _("error: zero length key name given\n"));
248 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
249 if (!W_ERROR_IS_OK(werr
)) {
250 d_fprintf(stderr
, _("open_hive failed: %s\n"),
255 werr
= reg_deletekey(hivekey
, subkeyname
);
256 if (!W_ERROR_IS_OK(werr
)) {
257 d_fprintf(stderr
, _("reg_deletekey failed: %s\n"),
269 static int net_registry_getvalue_internal(struct net_context
*c
, int argc
,
270 const char **argv
, bool raw
)
274 struct registry_key
*key
= NULL
;
275 struct registry_value
*value
= NULL
;
276 TALLOC_CTX
*ctx
= talloc_stackframe();
278 if (argc
!= 2 || c
->display_usage
) {
279 d_fprintf(stderr
, _("usage: net rpc registry getvalue <key> "
284 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
285 if (!W_ERROR_IS_OK(werr
)) {
286 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
290 werr
= reg_queryvalue(ctx
, key
, argv
[1], &value
);
291 if (!W_ERROR_IS_OK(werr
)) {
292 d_fprintf(stderr
, _("reg_queryvalue failed: %s\n"),
297 print_registry_value(value
, raw
);
306 static int net_registry_getvalue(struct net_context
*c
, int argc
,
309 return net_registry_getvalue_internal(c
, argc
, argv
, false);
312 static int net_registry_getvalueraw(struct net_context
*c
, int argc
,
315 return net_registry_getvalue_internal(c
, argc
, argv
, true);
318 static int net_registry_setvalue(struct net_context
*c
, int argc
,
322 struct registry_value value
;
323 struct registry_key
*key
= NULL
;
325 TALLOC_CTX
*ctx
= talloc_stackframe();
327 if (argc
< 4 || c
->display_usage
) {
328 d_fprintf(stderr
, _("usage: net rpc registry setvalue <key> "
329 "<valuename> <type> [<val>]+\n"));
333 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
334 d_fprintf(stderr
, _("Too many args for type %s\n"), argv
[2]);
338 if (strequal(argv
[2], "dword")) {
339 value
.type
= REG_DWORD
;
340 value
.v
.dword
= strtoul(argv
[3], NULL
, 10);
341 } else if (strequal(argv
[2], "sz")) {
343 value
.v
.sz
.len
= strlen(argv
[3])+1;
344 value
.v
.sz
.str
= CONST_DISCARD(char *, argv
[3]);
345 } else if (strequal(argv
[2], "multi_sz")) {
346 value
.type
= REG_MULTI_SZ
;
347 value
.v
.multi_sz
.num_strings
= argc
- 3;
348 value
.v
.multi_sz
.strings
= (char **)(argv
+ 3);
350 d_fprintf(stderr
, _("type \"%s\" not implemented\n"), argv
[2]);
354 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
355 if (!W_ERROR_IS_OK(werr
)) {
356 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
360 werr
= reg_setvalue(key
, argv
[1], &value
);
361 if (!W_ERROR_IS_OK(werr
)) {
362 d_fprintf(stderr
, _("reg_setvalue failed: %s\n"),
374 static int net_registry_deletevalue(struct net_context
*c
, int argc
,
378 struct registry_key
*key
= NULL
;
379 TALLOC_CTX
*ctx
= talloc_stackframe();
382 if (argc
!= 2 || c
->display_usage
) {
383 d_fprintf(stderr
, _("usage: net rpc registry deletevalue <key> "
388 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
389 if (!W_ERROR_IS_OK(werr
)) {
390 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
394 werr
= reg_deletevalue(key
, argv
[1]);
395 if (!W_ERROR_IS_OK(werr
)) {
396 d_fprintf(stderr
, _("reg_deletekey failed: %s\n"),
408 static int net_registry_getsd(struct net_context
*c
, int argc
,
413 struct registry_key
*key
= NULL
;
414 struct security_descriptor
*secdesc
= NULL
;
415 TALLOC_CTX
*ctx
= talloc_stackframe();
416 uint32_t access_mask
= REG_KEY_READ
|
417 SEC_FLAG_MAXIMUM_ALLOWED
|
418 SEC_FLAG_SYSTEM_SECURITY
;
421 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
422 * is denied with these perms right now...
424 access_mask
= REG_KEY_READ
;
426 if (argc
!= 1 || c
->display_usage
) {
427 d_printf(_("Usage: net registry getsd <path>\n"));
428 d_printf(_("Example: net registry getsd "
429 "'HKLM\\Software\\Samba'\n"));
432 if (strlen(argv
[0]) == 0) {
433 d_fprintf(stderr
, "error: zero length key name given\n");
437 werr
= open_key(ctx
, argv
[0], access_mask
, &key
);
438 if (!W_ERROR_IS_OK(werr
)) {
439 d_fprintf(stderr
, _("open_key failed: %s\n"), win_errstr(werr
));
443 werr
= reg_getkeysecurity(ctx
, key
, &secdesc
);
444 if (!W_ERROR_IS_OK(werr
)) {
445 d_fprintf(stderr
, _("reg_getkeysecurity failed: %s\n"),
450 display_sec_desc(secdesc
);
459 int net_registry(struct net_context
*c
, int argc
, const char **argv
)
463 struct functable func
[] = {
466 net_registry_enumerate
,
468 N_("Enumerate registry keys and values"),
469 N_("net registry enumerate\n"
470 " Enumerate registry keys and values")
474 net_registry_createkey
,
476 N_("Create a new registry key"),
477 N_("net registry createkey\n"
478 " Create a new registry key")
482 net_registry_deletekey
,
484 N_("Delete a registry key"),
485 N_("net registry deletekey\n"
486 " Delete a registry key")
490 net_registry_getvalue
,
492 N_("Print a registry value"),
493 N_("net registry getvalue\n"
494 " Print a registry value")
498 net_registry_getvalueraw
,
500 N_("Print a registry value (raw format)"),
501 N_("net registry getvalueraw\n"
502 " Print a registry value (raw format)")
506 net_registry_setvalue
,
508 N_("Set a new registry value"),
509 N_("net registry setvalue\n"
510 " Set a new registry value")
514 net_registry_deletevalue
,
516 N_("Delete a registry value"),
517 N_("net registry deletevalue\n"
518 " Delete a registry value")
524 N_("Get security descriptor"),
525 N_("net registry getsd\n"
526 " Get security descriptor")
528 { NULL
, NULL
, 0, NULL
, NULL
}
531 if (!W_ERROR_IS_OK(registry_init_basic())) {
535 ret
= net_run_function(c
, argc
, argv
, "net registry", func
);