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(int argc
, const char **argv
)
121 struct registry_key
*key
= NULL
;
122 TALLOC_CTX
*ctx
= talloc_stackframe();
126 char *valname
= NULL
;
127 struct registry_value
*valvalue
= NULL
;
131 d_printf("Usage: net registry enumerate <path>\n");
132 d_printf("Example: net registry enumerate "
133 "'HKLM\\Software\\Samba'\n");
137 werr
= open_key(ctx
, argv
[0], REG_KEY_READ
, &key
);
138 if (!W_ERROR_IS_OK(werr
)) {
139 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
144 werr
= reg_enumkey(ctx
, key
, count
, &subkey_name
, &modtime
),
148 print_registry_key(subkey_name
, &modtime
);
150 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
155 werr
= reg_enumvalue(ctx
, key
, count
, &valname
, &valvalue
),
159 print_registry_value(valname
, valvalue
);
161 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
171 static int net_registry_createkey(int argc
, const char **argv
)
174 enum winreg_CreateAction action
;
176 struct registry_key
*hivekey
= NULL
;
177 struct registry_key
*subkey
= NULL
;
178 TALLOC_CTX
*ctx
= talloc_stackframe();
182 d_printf("Usage: net registry createkey <path>\n");
183 d_printf("Example: net registry createkey "
184 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
187 if (strlen(argv
[0]) == 0) {
188 d_fprintf(stderr
, "error: zero length key name given\n");
192 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
193 if (!W_ERROR_IS_OK(werr
)) {
194 d_fprintf(stderr
, "open_hive failed: %s\n", dos_errstr(werr
));
198 werr
= reg_createkey(ctx
, hivekey
, subkeyname
, REG_KEY_WRITE
,
200 if (!W_ERROR_IS_OK(werr
)) {
201 d_fprintf(stderr
, "reg_createkey failed: %s\n",
206 case REG_ACTION_NONE
:
207 d_printf("createkey did nothing -- huh?\n");
209 case REG_CREATED_NEW_KEY
:
210 d_printf("createkey created %s\n", argv
[0]);
212 case REG_OPENED_EXISTING_KEY
:
213 d_printf("createkey opened existing %s\n", argv
[0]);
224 static int net_registry_deletekey(int argc
, const char **argv
)
228 struct registry_key
*hivekey
= NULL
;
229 TALLOC_CTX
*ctx
= talloc_stackframe();
233 d_printf("Usage: net registry deletekey <path>\n");
234 d_printf("Example: net registry deletekey "
235 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n");
238 if (strlen(argv
[0]) == 0) {
239 d_fprintf(stderr
, "error: zero length key name given\n");
243 werr
= open_hive(ctx
, argv
[0], REG_KEY_WRITE
, &hivekey
, &subkeyname
);
244 if (!W_ERROR_IS_OK(werr
)) {
245 d_fprintf(stderr
, "open_hive failed: %s\n", dos_errstr(werr
));
249 werr
= reg_deletekey(hivekey
, subkeyname
);
250 if (!W_ERROR_IS_OK(werr
)) {
251 d_fprintf(stderr
, "reg_deletekey failed: %s\n",
263 static int net_registry_setvalue(int argc
, const char **argv
)
266 struct registry_value value
;
267 struct registry_key
*key
= NULL
;
269 TALLOC_CTX
*ctx
= talloc_stackframe();
272 d_fprintf(stderr
, "usage: net rpc registry setvalue <key> "
273 "<valuename> <type> [<val>]+\n");
277 if (!strequal(argv
[2], "multi_sz") && (argc
!= 4)) {
278 d_fprintf(stderr
, "Too many args for type %s\n", argv
[2]);
282 if (strequal(argv
[2], "dword")) {
283 value
.type
= REG_DWORD
;
284 value
.v
.dword
= strtoul(argv
[3], NULL
, 10);
285 } else if (strequal(argv
[2], "sz")) {
287 value
.v
.sz
.len
= strlen(argv
[3])+1;
288 value
.v
.sz
.str
= CONST_DISCARD(char *, argv
[3]);
290 d_fprintf(stderr
, "type \"%s\" not implemented\n", argv
[2]);
294 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
295 if (!W_ERROR_IS_OK(werr
)) {
296 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
300 werr
= reg_setvalue(key
, argv
[1], &value
);
301 if (!W_ERROR_IS_OK(werr
)) {
302 d_fprintf(stderr
, "reg_setvalue failed: %s\n",
314 static int net_registry_deletevalue(int argc
, const char **argv
)
317 struct registry_key
*key
= NULL
;
318 TALLOC_CTX
*ctx
= talloc_stackframe();
322 d_fprintf(stderr
, "usage: net rpc registry deletevalue <key> "
327 werr
= open_key(ctx
, argv
[0], REG_KEY_WRITE
, &key
);
328 if (!W_ERROR_IS_OK(werr
)) {
329 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
333 werr
= reg_deletevalue(key
, argv
[1]);
334 if (!W_ERROR_IS_OK(werr
)) {
335 d_fprintf(stderr
, "reg_deletekey failed: %s\n",
347 static int net_registry_getsd(int argc
, const char **argv
)
351 struct registry_key
*key
= NULL
;
352 struct security_descriptor
*secdesc
= NULL
;
353 TALLOC_CTX
*ctx
= talloc_stackframe();
354 uint32_t access_mask
= REG_KEY_READ
|
355 SEC_RIGHT_MAXIMUM_ALLOWED
|
356 SEC_RIGHT_SYSTEM_SECURITY
;
359 * net_rpc_regsitry uses SEC_RIGHT_SYSTEM_SECURITY, but access
360 * is denied with these perms right now...
362 access_mask
= REG_KEY_READ
;
365 d_printf("Usage: net registry getsd <path>\n");
366 d_printf("Example: net registry getsd "
367 "'HKLM\\Software\\Samba'\n");
370 if (strlen(argv
[0]) == 0) {
371 d_fprintf(stderr
, "error: zero length key name given\n");
375 werr
= open_key(ctx
, argv
[0], access_mask
, &key
);
376 if (!W_ERROR_IS_OK(werr
)) {
377 d_fprintf(stderr
, "open_key failed: %s\n", dos_errstr(werr
));
381 werr
= reg_getkeysecurity(ctx
, key
, &secdesc
);
382 if (!W_ERROR_IS_OK(werr
)) {
383 d_fprintf(stderr
, "reg_getkeysecurity failed: %s\n",
388 display_sec_desc(secdesc
);
397 int net_registry(int argc
, const char **argv
)
401 struct functable2 func
[] = {
404 net_registry_enumerate
,
405 "Enumerate registry keys and values"
409 net_registry_createkey
,
410 "Create a new registry key"
414 net_registry_deletekey
,
415 "Delete a registry key"
419 net_registry_setvalue
,
420 "Set a new registry value"
424 net_registry_deletevalue
,
425 "Delete a registry value"
430 "Get security descriptor"
435 if (!registry_init_basic()) {
439 ret
= net_run_function2(argc
, argv
, "net registry", func
);