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", dos_errstr(werr
));
98 werr
= reg_openkey(ctx
, hive
, subkey_name
, desired_access
, key
);
99 if (!W_ERROR_IS_OK(werr
)) {
100 d_fprintf(stderr
, "reg_openkey failed: %s\n",
108 TALLOC_FREE(tmp_ctx
);
114 * the main "net registry" function implementations
118 static int net_registry_enumerate(struct net_context
*c
, int argc
,
122 struct registry_key
*key
= NULL
;
123 TALLOC_CTX
*ctx
= talloc_stackframe();
127 char *valname
= NULL
;
128 struct registry_value
*valvalue
= NULL
;
131 if (argc
!= 1 || c
->display_usage
) {
132 d_printf("Usage: net registry enumerate <path>\n");
133 d_printf("Example: net registry enumerate "
134 "'HKLM\\Software\\Samba'\n");
138 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
139 if (!W_ERROR_IS_OK(werr
)) {
140 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
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
)) {
156 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
160 print_registry_value_with_name(valname
, valvalue
);
162 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
172 static int net_registry_createkey(struct net_context
*c
, int argc
,
176 enum winreg_CreateAction action
;
178 struct registry_key
*hivekey
= NULL
;
179 struct registry_key
*subkey
= NULL
;
180 TALLOC_CTX
*ctx
= talloc_stackframe();
183 if (argc
!= 1 || c
->display_usage
) {
184 d_printf("Usage: net registry createkey <path>\n");
185 d_printf("Example: net registry createkey "
186 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
189 if (strlen(argv
[0]) == 0) {
190 d_fprintf(stderr
, "error: zero length key name given\n");
194 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
195 if (!W_ERROR_IS_OK(werr
)) {
196 d_fprintf(stderr
, "open_hive failed: %s\n", dos_errstr(werr
));
200 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
202 if (!W_ERROR_IS_OK(werr
)) {
203 d_fprintf(stderr
, "reg_createkey failed: %s\n",
208 case REG_ACTION_NONE
:
209 d_printf("createkey did nothing -- huh?\n");
211 case REG_CREATED_NEW_KEY
:
212 d_printf("createkey created %s\n", argv
[0]);
214 case REG_OPENED_EXISTING_KEY
:
215 d_printf("createkey opened existing %s\n", argv
[0]);
226 static int net_registry_deletekey(struct net_context
*c
, int argc
,
231 struct registry_key
*hivekey
= NULL
;
232 TALLOC_CTX
*ctx
= talloc_stackframe();
235 if (argc
!= 1 || c
->display_usage
) {
236 d_printf("Usage: net registry deletekey <path>\n");
237 d_printf("Example: net registry deletekey "
238 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
241 if (strlen(argv
[0]) == 0) {
242 d_fprintf(stderr
, "error: zero length key name given\n");
246 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
247 if (!W_ERROR_IS_OK(werr
)) {
248 d_fprintf(stderr
, "open_hive failed: %s\n", dos_errstr(werr
));
252 werr
= reg_deletekey(hivekey
, subkeyname
);
253 if (!W_ERROR_IS_OK(werr
)) {
254 d_fprintf(stderr
, "reg_deletekey failed: %s\n",
266 static int net_registry_getvalue_internal(struct net_context
*c
, int argc
,
267 const char **argv
, bool raw
)
271 struct registry_key
*key
= NULL
;
272 struct registry_value
*value
= NULL
;
273 TALLOC_CTX
*ctx
= talloc_stackframe();
275 if (argc
!= 2 || c
->display_usage
) {
276 d_fprintf(stderr
, "usage: net rpc registry getvalue <key> "
281 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
282 if (!W_ERROR_IS_OK(werr
)) {
283 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
287 werr
= reg_queryvalue(ctx
, key
, argv
[1], &value
);
288 if (!W_ERROR_IS_OK(werr
)) {
289 d_fprintf(stderr
, "reg_queryvalue failed: %s\n",
294 print_registry_value(value
, raw
);
303 static int net_registry_getvalue(struct net_context
*c
, int argc
,
306 return net_registry_getvalue_internal(c
, argc
, argv
, false);
309 static int net_registry_getvalueraw(struct net_context
*c
, int argc
,
312 return net_registry_getvalue_internal(c
, argc
, argv
, true);
315 static int net_registry_setvalue(struct net_context
*c
, int argc
,
319 struct registry_value value
;
320 struct registry_key
*key
= NULL
;
322 TALLOC_CTX
*ctx
= talloc_stackframe();
324 if (argc
< 4 || c
->display_usage
) {
325 d_fprintf(stderr
, "usage: net rpc registry setvalue <key> "
326 "<valuename> <type> [<val>]+\n");
330 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
331 d_fprintf(stderr
, "Too many args for type %s\n", argv
[2]);
335 if (strequal(argv
[2], "dword")) {
336 value
.type
= REG_DWORD
;
337 value
.v
.dword
= strtoul(argv
[3], NULL
, 10);
338 } else if (strequal(argv
[2], "sz")) {
340 value
.v
.sz
.len
= strlen(argv
[3])+1;
341 value
.v
.sz
.str
= CONST_DISCARD(char *, argv
[3]);
343 d_fprintf(stderr
, "type \"%s\" not implemented\n", argv
[2]);
347 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
348 if (!W_ERROR_IS_OK(werr
)) {
349 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
353 werr
= reg_setvalue(key
, argv
[1], &value
);
354 if (!W_ERROR_IS_OK(werr
)) {
355 d_fprintf(stderr
, "reg_setvalue failed: %s\n",
367 static int net_registry_deletevalue(struct net_context
*c
, int argc
,
371 struct registry_key
*key
= NULL
;
372 TALLOC_CTX
*ctx
= talloc_stackframe();
375 if (argc
!= 2 || c
->display_usage
) {
376 d_fprintf(stderr
, "usage: net rpc registry deletevalue <key> "
381 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
382 if (!W_ERROR_IS_OK(werr
)) {
383 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
387 werr
= reg_deletevalue(key
, argv
[1]);
388 if (!W_ERROR_IS_OK(werr
)) {
389 d_fprintf(stderr
, "reg_deletekey failed: %s\n",
401 static int net_registry_getsd(struct net_context
*c
, int argc
,
406 struct registry_key
*key
= NULL
;
407 struct security_descriptor
*secdesc
= NULL
;
408 TALLOC_CTX
*ctx
= talloc_stackframe();
409 uint32_t access_mask
= REG_KEY_READ
|
410 SEC_RIGHT_MAXIMUM_ALLOWED
|
411 SEC_RIGHT_SYSTEM_SECURITY
;
414 * net_rpc_regsitry uses SEC_RIGHT_SYSTEM_SECURITY, but access
415 * is denied with these perms right now...
417 access_mask
= REG_KEY_READ
;
419 if (argc
!= 1 || c
->display_usage
) {
420 d_printf("Usage: net registry getsd <path>\n");
421 d_printf("Example: net registry getsd "
422 "'HKLM\\Software\\Samba'\n");
425 if (strlen(argv
[0]) == 0) {
426 d_fprintf(stderr
, "error: zero length key name given\n");
430 werr
= open_key(ctx
, argv
[0], access_mask
, &key
);
431 if (!W_ERROR_IS_OK(werr
)) {
432 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
436 werr
= reg_getkeysecurity(ctx
, key
, &secdesc
);
437 if (!W_ERROR_IS_OK(werr
)) {
438 d_fprintf(stderr
, "reg_getkeysecurity failed: %s\n",
443 display_sec_desc(secdesc
);
452 int net_registry(struct net_context
*c
, int argc
, const char **argv
)
456 struct functable func
[] = {
459 net_registry_enumerate
,
461 "Enumerate registry keys and values",
462 "net registry enumerate\n"
463 " Enumerate registry keys and values"
467 net_registry_createkey
,
469 "Create a new registry key",
470 "net registry createkey\n"
471 " Create a new registry key"
475 net_registry_deletekey
,
477 "Delete a registry key",
478 "net registry deletekey\n"
479 " Delete a registry key"
483 net_registry_getvalue
,
485 "Print a registry value",
486 "net registry getvalue\n"
487 " Print a registry value"
491 net_registry_getvalueraw
,
493 "Print a registry value (raw format)",
494 "net registry getvalueraw\n"
495 " Print a registry value (raw format)"
499 net_registry_setvalue
,
501 "Set a new registry value",
502 "net registry setvalue\n"
503 " Set a new registry value"
507 net_registry_deletevalue
,
509 "Delete a registry value",
510 "net registry deletevalue\n"
511 " Delete a registry value"
517 "Get security descriptor",
518 "net registry getsd\n"
519 " Get security descriptor"
521 { NULL
, NULL
, 0, NULL
, NULL
}
524 if (!W_ERROR_IS_OK(registry_init_basic())) {
528 ret
= net_run_function(c
, argc
, argv
, "net registry", func
);