s3:net: registry: add new command enumerate_recursive
[Samba.git] / source3 / utils / net_registry.c
blobe1267c37456c15b3acb570ba29f530b9ac3511be
1 /*
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/>.
22 #include "includes.h"
23 #include "registry.h"
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"
32 #include "registry/reg_format.h"
33 #include <assert.h>
34 #include "../libcli/security/display_sec.h"
35 #include "../libcli/security/sddl.h"
36 #include "../libcli/registry/util_reg.h"
37 #include "passdb/machine_sid.h"
41 * Helper functions
45 /**
46 * split given path into hive and remaining path and open the hive key
48 static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
49 uint32 desired_access,
50 struct registry_key **hive,
51 char **subkeyname)
53 WERROR werr;
54 struct security_token *token = NULL;
55 char *hivename = NULL;
56 char *tmp_subkeyname = NULL;
57 TALLOC_CTX *tmp_ctx = talloc_stackframe();
59 if ((hive == NULL) || (subkeyname == NULL)) {
60 werr = WERR_INVALID_PARAM;
61 goto done;
64 werr = split_hive_key(tmp_ctx, path, &hivename, &tmp_subkeyname);
65 if (!W_ERROR_IS_OK(werr)) {
66 goto done;
68 *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
69 if (*subkeyname == NULL) {
70 werr = WERR_NOMEM;
71 goto done;
74 werr = ntstatus_to_werror(registry_create_admin_token(tmp_ctx, &token));
75 if (!W_ERROR_IS_OK(werr)) {
76 goto done;
79 werr = reg_openhive(ctx, hivename, desired_access, token, hive);
80 if (!W_ERROR_IS_OK(werr)) {
81 goto done;
84 werr = WERR_OK;
86 done:
87 TALLOC_FREE(tmp_ctx);
88 return werr;
91 static WERROR open_key(TALLOC_CTX *ctx, const char *path,
92 uint32 desired_access,
93 struct registry_key **key)
95 WERROR werr;
96 char *subkey_name = NULL;
97 struct registry_key *hive = NULL;
98 TALLOC_CTX *tmp_ctx = talloc_stackframe();
100 if ((path == NULL) || (key == NULL)) {
101 return WERR_INVALID_PARAM;
104 werr = open_hive(tmp_ctx, path, desired_access, &hive, &subkey_name);
105 if (!W_ERROR_IS_OK(werr)) {
106 d_fprintf(stderr, _("open_hive failed: %s\n"),
107 win_errstr(werr));
108 goto done;
111 werr = reg_openkey(ctx, hive, subkey_name, desired_access, key);
112 if (!W_ERROR_IS_OK(werr)) {
113 d_fprintf(stderr, _("reg_openkey failed: %s\n"),
114 win_errstr(werr));
115 goto done;
118 werr = WERR_OK;
120 done:
121 TALLOC_FREE(tmp_ctx);
122 return werr;
127 * the main "net registry" function implementations
131 static int net_registry_enumerate(struct net_context *c, int argc,
132 const char **argv)
134 WERROR werr;
135 struct registry_key *key = NULL;
136 TALLOC_CTX *ctx = talloc_stackframe();
137 char *subkey_name;
138 NTTIME modtime;
139 uint32_t count;
140 char *valname = NULL;
141 struct registry_value *valvalue = NULL;
142 int ret = -1;
144 if (argc != 1 || c->display_usage) {
145 d_printf("%s\n%s",
146 _("Usage:"),
147 _("net registry enumerate <path>\n"));
148 d_printf("%s\n%s",
149 _("Example:"),
150 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
151 goto done;
154 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
155 if (!W_ERROR_IS_OK(werr)) {
156 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
157 goto done;
160 for (count = 0;
161 werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
162 W_ERROR_IS_OK(werr);
163 count++)
165 print_registry_key(subkey_name, &modtime);
167 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
168 goto done;
171 for (count = 0;
172 werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
173 W_ERROR_IS_OK(werr);
174 count++)
176 print_registry_value_with_name(valname, valvalue);
178 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
179 goto done;
182 ret = 0;
183 done:
184 TALLOC_FREE(ctx);
185 return ret;
188 static WERROR registry_enumkey(struct registry_key* parent, const char* keyname, bool recursive)
190 WERROR werr;
191 TALLOC_CTX *ctx = talloc_stackframe();
192 char* subkey_name;
193 NTTIME modtime;
194 uint32_t count;
195 char* valname = NULL;
196 struct registry_value *valvalue = NULL;
197 struct registry_key* key = NULL;
199 werr = reg_openkey(ctx, parent, keyname, REG_KEY_READ, &key);
200 if (!W_ERROR_IS_OK(werr)) {
201 goto done;
204 printf("[%s]\n", key->key->name);
206 for (count = 0;
207 werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
208 W_ERROR_IS_OK(werr);
209 count++)
211 print_registry_key(subkey_name, &modtime);
213 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
214 goto done;
217 for (count = 0;
218 werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
219 W_ERROR_IS_OK(werr);
220 count++)
222 print_registry_value_with_name(valname, valvalue);
224 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
225 goto done;
228 if (!recursive) {
229 werr = WERR_OK;
230 goto done;
233 for (count = 0;
234 werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
235 W_ERROR_IS_OK(werr);
236 count++)
238 werr = registry_enumkey(key, subkey_name, recursive);
239 if (!W_ERROR_IS_OK(werr)) {
240 goto done;
243 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
244 goto done;
246 werr = WERR_OK;
247 done:
248 TALLOC_FREE(ctx);
249 return werr;
252 static int net_registry_enumerate_recursive(struct net_context *c, int argc,
253 const char **argv)
255 WERROR werr;
256 struct registry_key *key = NULL;
257 char* name = NULL;
258 TALLOC_CTX *ctx = talloc_stackframe();
259 int ret = -1;
261 if (argc != 1 || c->display_usage) {
262 d_printf("%s\n%s",
263 _("Usage:"),
264 _("net registry enumerate <path>\n"));
265 d_printf("%s\n%s",
266 _("Example:"),
267 _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
268 goto done;
271 werr = open_hive(ctx, argv[0], REG_KEY_READ, &key, &name);
272 if (!W_ERROR_IS_OK(werr)) {
273 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
274 goto done;
277 werr = registry_enumkey(key, name, true);
278 if (W_ERROR_IS_OK(werr)) {
279 ret = 0;
281 done:
282 TALLOC_FREE(ctx);
283 return ret;
287 static int net_registry_createkey(struct net_context *c, int argc,
288 const char **argv)
290 WERROR werr;
291 enum winreg_CreateAction action;
292 char *subkeyname;
293 struct registry_key *hivekey = NULL;
294 struct registry_key *subkey = NULL;
295 TALLOC_CTX *ctx = talloc_stackframe();
296 int ret = -1;
298 if (argc != 1 || c->display_usage) {
299 d_printf("%s\n%s",
300 _("Usage:"),
301 _("net registry createkey <path>\n"));
302 d_printf("%s\n%s",
303 _("Example:"),
304 _("net registry createkey "
305 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
306 goto done;
308 if (strlen(argv[0]) == 0) {
309 d_fprintf(stderr, _("error: zero length key name given\n"));
310 goto done;
313 werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
314 if (!W_ERROR_IS_OK(werr)) {
315 d_fprintf(stderr, _("open_hive failed: %s\n"),
316 win_errstr(werr));
317 goto done;
320 werr = reg_createkey(ctx, hivekey, subkeyname, REG_KEY_WRITE,
321 &subkey, &action);
322 if (!W_ERROR_IS_OK(werr)) {
323 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
324 win_errstr(werr));
325 goto done;
327 switch (action) {
328 case REG_ACTION_NONE:
329 d_printf(_("createkey did nothing -- huh?\n"));
330 break;
331 case REG_CREATED_NEW_KEY:
332 d_printf(_("createkey created %s\n"), argv[0]);
333 break;
334 case REG_OPENED_EXISTING_KEY:
335 d_printf(_("createkey opened existing %s\n"), argv[0]);
336 break;
339 ret = 0;
341 done:
342 TALLOC_FREE(ctx);
343 return ret;
346 static int net_registry_deletekey_internal(struct net_context *c, int argc,
347 const char **argv,
348 bool recursive)
350 WERROR werr;
351 char *subkeyname;
352 struct registry_key *hivekey = NULL;
353 TALLOC_CTX *ctx = talloc_stackframe();
354 int ret = -1;
356 if (argc != 1 || c->display_usage) {
357 d_printf("%s\n%s",
358 _("Usage:"),
359 _("net registry deletekey <path>\n"));
360 d_printf("%s\n%s",
361 _("Example:"),
362 _("net registry deletekey "
363 "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
364 goto done;
366 if (strlen(argv[0]) == 0) {
367 d_fprintf(stderr, _("error: zero length key name given\n"));
368 goto done;
371 werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
372 if (!W_ERROR_IS_OK(werr)) {
373 d_fprintf(stderr, "open_hive %s: %s\n", _("failed"),
374 win_errstr(werr));
375 goto done;
378 if (recursive) {
379 werr = reg_deletekey_recursive(hivekey, subkeyname);
380 } else {
381 werr = reg_deletekey(hivekey, subkeyname);
383 if (!W_ERROR_IS_OK(werr) &&
384 !(c->opt_force && W_ERROR_EQUAL(werr, WERR_BADFILE)))
386 d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
387 win_errstr(werr));
388 goto done;
391 ret = 0;
393 done:
394 TALLOC_FREE(ctx);
395 return ret;
398 static int net_registry_deletekey(struct net_context *c, int argc,
399 const char **argv)
401 return net_registry_deletekey_internal(c, argc, argv, false);
404 static int net_registry_deletekey_recursive(struct net_context *c, int argc,
405 const char **argv)
407 return net_registry_deletekey_internal(c, argc, argv, true);
410 static int net_registry_getvalue_internal(struct net_context *c, int argc,
411 const char **argv, bool raw)
413 WERROR werr;
414 int ret = -1;
415 struct registry_key *key = NULL;
416 struct registry_value *value = NULL;
417 TALLOC_CTX *ctx = talloc_stackframe();
419 if (argc != 2 || c->display_usage) {
420 d_fprintf(stderr, "%s\n%s",
421 _("Usage:"),
422 _("net registry getvalue <key> <valuename>\n"));
423 goto done;
426 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
427 if (!W_ERROR_IS_OK(werr)) {
428 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
429 goto done;
432 werr = reg_queryvalue(ctx, key, argv[1], &value);
433 if (!W_ERROR_IS_OK(werr)) {
434 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
435 win_errstr(werr));
436 goto done;
439 print_registry_value(value, raw);
441 ret = 0;
443 done:
444 TALLOC_FREE(ctx);
445 return ret;
448 static int net_registry_getvalue(struct net_context *c, int argc,
449 const char **argv)
451 return net_registry_getvalue_internal(c, argc, argv, false);
454 static int net_registry_getvalueraw(struct net_context *c, int argc,
455 const char **argv)
457 return net_registry_getvalue_internal(c, argc, argv, true);
460 static int net_registry_getvaluesraw(struct net_context *c, int argc,
461 const char **argv)
463 WERROR werr;
464 int ret = -1;
465 struct registry_key *key = NULL;
466 TALLOC_CTX *ctx = talloc_stackframe();
467 uint32_t idx;
469 if (argc != 1 || c->display_usage) {
470 d_fprintf(stderr, "usage: net rpc registry getvaluesraw "
471 "<key>\n");
472 goto done;
475 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
476 if (!W_ERROR_IS_OK(werr)) {
477 d_fprintf(stderr, "open_key failed: %s\n", win_errstr(werr));
478 goto done;
481 idx = 0;
482 while (true) {
483 struct registry_value *val;
485 werr = reg_enumvalue(talloc_tos(), key, idx, NULL, &val);
487 if (W_ERROR_EQUAL(werr, WERR_NO_MORE_ITEMS)) {
488 ret = 0;
489 break;
491 if (!W_ERROR_IS_OK(werr)) {
492 break;
494 print_registry_value(val, true);
495 TALLOC_FREE(val);
496 idx += 1;
498 done:
499 TALLOC_FREE(ctx);
500 return ret;
503 static int net_registry_setvalue(struct net_context *c, int argc,
504 const char **argv)
506 WERROR werr;
507 struct registry_value value;
508 struct registry_key *key = NULL;
509 int ret = -1;
510 TALLOC_CTX *ctx = talloc_stackframe();
512 if (argc < 4 || c->display_usage) {
513 d_fprintf(stderr, "%s\n%s",
514 _("Usage:"),
515 _("net registry setvalue <key> <valuename> "
516 "<type> [<val>]+\n"));
517 goto done;
520 if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
521 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
522 goto done;
525 if (strequal(argv[2], "dword")) {
526 uint32_t v = strtoul(argv[3], NULL, 10);
527 value.type = REG_DWORD;
528 value.data = data_blob_talloc(ctx, NULL, 4);
529 SIVAL(value.data.data, 0, v);
530 } else if (strequal(argv[2], "sz")) {
531 value.type = REG_SZ;
532 if (!push_reg_sz(ctx, &value.data, argv[3])) {
533 goto done;
535 } else if (strequal(argv[2], "multi_sz")) {
536 const char **array;
537 int count = argc - 3;
538 int i;
539 value.type = REG_MULTI_SZ;
540 array = talloc_zero_array(ctx, const char *, count + 1);
541 if (array == NULL) {
542 goto done;
544 for (i=0; i < count; i++) {
545 array[i] = talloc_strdup(array, argv[count+i]);
546 if (array[i] == NULL) {
547 goto done;
550 if (!push_reg_multi_sz(ctx, &value.data, array)) {
551 goto done;
553 } else {
554 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
555 goto done;
558 werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
559 if (!W_ERROR_IS_OK(werr)) {
560 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
561 goto done;
564 werr = reg_setvalue(key, argv[1], &value);
565 if (!W_ERROR_IS_OK(werr)) {
566 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
567 win_errstr(werr));
568 goto done;
571 ret = 0;
573 done:
574 TALLOC_FREE(ctx);
575 return ret;
578 struct net_registry_increment_state {
579 const char *keyname;
580 const char *valuename;
581 uint32_t increment;
582 uint32_t newvalue;
583 WERROR werr;
586 static void net_registry_increment_fn(void *private_data)
588 struct net_registry_increment_state *state =
589 (struct net_registry_increment_state *)private_data;
590 struct registry_value *value;
591 struct registry_key *key = NULL;
592 uint32_t v;
594 state->werr = open_key(talloc_tos(), state->keyname,
595 REG_KEY_READ|REG_KEY_WRITE, &key);
596 if (!W_ERROR_IS_OK(state->werr)) {
597 d_fprintf(stderr, _("open_key failed: %s\n"),
598 win_errstr(state->werr));
599 goto done;
602 state->werr = reg_queryvalue(key, key, state->valuename, &value);
603 if (!W_ERROR_IS_OK(state->werr)) {
604 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
605 win_errstr(state->werr));
606 goto done;
609 if (value->type != REG_DWORD) {
610 d_fprintf(stderr, _("value not a DWORD: %s\n"),
611 str_regtype(value->type));
612 goto done;
615 if (value->data.length < 4) {
616 d_fprintf(stderr, _("value too short for regular DWORD\n"));
617 goto done;
620 v = IVAL(value->data.data, 0);
621 v += state->increment;
622 state->newvalue = v;
624 SIVAL(value->data.data, 0, v);
626 state->werr = reg_setvalue(key, state->valuename, value);
627 if (!W_ERROR_IS_OK(state->werr)) {
628 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
629 win_errstr(state->werr));
630 goto done;
633 done:
634 TALLOC_FREE(key);
635 return;
638 static int net_registry_increment(struct net_context *c, int argc,
639 const char **argv)
641 struct net_registry_increment_state state;
642 NTSTATUS status;
643 int ret = -1;
645 if (argc < 2 || c->display_usage) {
646 d_fprintf(stderr, "%s\n%s",
647 _("Usage:"),
648 _("net registry increment <key> <valuename> "
649 "[<increment>]\n"));
650 goto done;
653 state.keyname = argv[0];
654 state.valuename = argv[1];
656 state.increment = 1;
657 if (argc == 3) {
658 state.increment = strtoul(argv[2], NULL, 10);
661 status = g_lock_do("registry_increment_lock", G_LOCK_WRITE,
662 timeval_set(600, 0), procid_self(),
663 net_registry_increment_fn, &state);
664 if (!NT_STATUS_IS_OK(status)) {
665 d_fprintf(stderr, _("g_lock_do failed: %s\n"),
666 nt_errstr(status));
667 goto done;
669 if (!W_ERROR_IS_OK(state.werr)) {
670 d_fprintf(stderr, _("increment failed: %s\n"),
671 win_errstr(state.werr));
672 goto done;
675 d_printf(_("%u\n"), (unsigned)state.newvalue);
677 ret = 0;
679 done:
680 return ret;
683 static int net_registry_deletevalue(struct net_context *c, int argc,
684 const char **argv)
686 WERROR werr;
687 struct registry_key *key = NULL;
688 TALLOC_CTX *ctx = talloc_stackframe();
689 int ret = -1;
691 if (argc != 2 || c->display_usage) {
692 d_fprintf(stderr, "%s\n%s",
693 _("Usage:"),
694 _("net registry deletevalue <key> <valuename>\n"));
695 goto done;
698 werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
699 if (!W_ERROR_IS_OK(werr)) {
700 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
701 goto done;
704 werr = reg_deletevalue(key, argv[1]);
705 if (!W_ERROR_IS_OK(werr)) {
706 d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
707 win_errstr(werr));
708 goto done;
711 ret = 0;
713 done:
714 TALLOC_FREE(ctx);
715 return ret;
718 static WERROR net_registry_getsd_internal(struct net_context *c,
719 TALLOC_CTX *mem_ctx,
720 const char *keyname,
721 struct security_descriptor **sd)
723 WERROR werr;
724 struct registry_key *key = NULL;
725 TALLOC_CTX *ctx = talloc_stackframe();
726 uint32_t access_mask = REG_KEY_READ |
727 SEC_FLAG_MAXIMUM_ALLOWED |
728 SEC_FLAG_SYSTEM_SECURITY;
731 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
732 * is denied with these perms right now...
734 access_mask = REG_KEY_READ;
736 if (sd == NULL) {
737 d_fprintf(stderr, _("internal error: invalid argument\n"));
738 werr = WERR_INVALID_PARAM;
739 goto done;
742 if (strlen(keyname) == 0) {
743 d_fprintf(stderr, _("error: zero length key name given\n"));
744 werr = WERR_INVALID_PARAM;
745 goto done;
748 werr = open_key(ctx, keyname, access_mask, &key);
749 if (!W_ERROR_IS_OK(werr)) {
750 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
751 win_errstr(werr));
752 goto done;
755 werr = reg_getkeysecurity(mem_ctx, key, sd);
756 if (!W_ERROR_IS_OK(werr)) {
757 d_fprintf(stderr, "%s%s\n", _("reg_getkeysecurity failed: "),
758 win_errstr(werr));
759 goto done;
762 werr = WERR_OK;
764 done:
765 TALLOC_FREE(ctx);
766 return werr;
769 static int net_registry_getsd(struct net_context *c, int argc,
770 const char **argv)
772 WERROR werr;
773 int ret = -1;
774 struct security_descriptor *secdesc = NULL;
775 TALLOC_CTX *ctx = talloc_stackframe();
777 if (argc != 1 || c->display_usage) {
778 d_printf("%s\n%s",
779 _("Usage:"),
780 _("net registry getsd <path>\n"));
781 d_printf("%s\n%s",
782 _("Example:"),
783 _("net registry getsd 'HKLM\\Software\\Samba'\n"));
784 goto done;
787 werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
788 if (!W_ERROR_IS_OK(werr)) {
789 goto done;
792 display_sec_desc(secdesc);
794 ret = 0;
796 done:
797 TALLOC_FREE(ctx);
798 return ret;
801 static int net_registry_getsd_sddl(struct net_context *c,
802 int argc, const char **argv)
804 WERROR werr;
805 int ret = -1;
806 struct security_descriptor *secdesc = NULL;
807 TALLOC_CTX *ctx = talloc_stackframe();
809 if (argc != 1 || c->display_usage) {
810 d_printf("%s\n%s",
811 _("Usage:"),
812 _("net registry getsd_sddl <path>\n"));
813 d_printf("%s\n%s",
814 _("Example:"),
815 _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
816 goto done;
819 werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
820 if (!W_ERROR_IS_OK(werr)) {
821 goto done;
824 d_printf("%s\n", sddl_encode(ctx, secdesc, get_global_sam_sid()));
826 ret = 0;
828 done:
829 TALLOC_FREE(ctx);
830 return ret;
833 static WERROR net_registry_setsd_internal(struct net_context *c,
834 TALLOC_CTX *mem_ctx,
835 const char *keyname,
836 struct security_descriptor *sd)
838 WERROR werr;
839 struct registry_key *key = NULL;
840 TALLOC_CTX *ctx = talloc_stackframe();
841 uint32_t access_mask = REG_KEY_WRITE |
842 SEC_FLAG_MAXIMUM_ALLOWED |
843 SEC_FLAG_SYSTEM_SECURITY;
846 * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
847 * is denied with these perms right now...
849 access_mask = REG_KEY_WRITE;
851 if (strlen(keyname) == 0) {
852 d_fprintf(stderr, _("error: zero length key name given\n"));
853 werr = WERR_INVALID_PARAM;
854 goto done;
857 werr = open_key(ctx, keyname, access_mask, &key);
858 if (!W_ERROR_IS_OK(werr)) {
859 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
860 win_errstr(werr));
861 goto done;
864 werr = reg_setkeysecurity(key, sd);
865 if (!W_ERROR_IS_OK(werr)) {
866 d_fprintf(stderr, "%s%s\n", _("reg_setkeysecurity failed: "),
867 win_errstr(werr));
868 goto done;
871 werr = WERR_OK;
873 done:
874 TALLOC_FREE(ctx);
875 return werr;
878 static int net_registry_setsd_sddl(struct net_context *c,
879 int argc, const char **argv)
881 WERROR werr;
882 int ret = -1;
883 struct security_descriptor *secdesc = NULL;
884 TALLOC_CTX *ctx = talloc_stackframe();
886 if (argc != 2 || c->display_usage) {
887 d_printf("%s\n%s",
888 _("Usage:"),
889 _("net registry setsd_sddl <path> <security_descriptor>\n"));
890 d_printf("%s\n%s",
891 _("Example:"),
892 _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
893 goto done;
896 secdesc = sddl_decode(ctx, argv[1], get_global_sam_sid());
897 if (secdesc == NULL) {
898 goto done;
901 werr = net_registry_setsd_internal(c, ctx, argv[0], secdesc);
902 if (!W_ERROR_IS_OK(werr)) {
903 goto done;
906 ret = 0;
908 done:
909 TALLOC_FREE(ctx);
910 return ret;
913 /******************************************************************************/
915 * @defgroup net_registry net registry
919 * @defgroup net_registry_import Import
920 * @ingroup net_registry
921 * @{
924 struct import_ctx {
925 TALLOC_CTX *mem_ctx;
929 static WERROR import_create_key(struct import_ctx* ctx,
930 struct registry_key* parent,
931 const char* name, void** pkey, bool* existing)
933 WERROR werr;
934 void* mem_ctx = talloc_new(ctx->mem_ctx);
936 struct registry_key* key = NULL;
937 enum winreg_CreateAction action;
939 if (parent == NULL) {
940 char* subkeyname = NULL;
941 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
942 &parent, &subkeyname);
943 if (!W_ERROR_IS_OK(werr)) {
944 d_fprintf(stderr, _("open_hive failed: %s\n"),
945 win_errstr(werr));
946 goto done;
948 name = subkeyname;
951 action = REG_ACTION_NONE;
952 werr = reg_createkey(mem_ctx, parent, name, REG_KEY_WRITE,
953 &key, &action);
954 if (!W_ERROR_IS_OK(werr)) {
955 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
956 win_errstr(werr));
957 goto done;
960 if (action == REG_ACTION_NONE) {
961 d_fprintf(stderr, _("createkey did nothing -- huh?\n"));
962 werr = WERR_CREATE_FAILED;
963 goto done;
966 if (existing != NULL) {
967 *existing = (action == REG_OPENED_EXISTING_KEY);
970 if (pkey!=NULL) {
971 *pkey = talloc_steal(ctx->mem_ctx, key);
974 done:
975 talloc_free(mem_ctx);
976 return werr;
979 static WERROR import_close_key(struct import_ctx* ctx,
980 struct registry_key* key)
982 return WERR_OK;
985 static WERROR import_delete_key(struct import_ctx* ctx,
986 struct registry_key* parent, const char* name)
988 WERROR werr;
989 void* mem_ctx = talloc_new(talloc_tos());
991 if (parent == NULL) {
992 char* subkeyname = NULL;
993 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
994 &parent, &subkeyname);
995 if (!W_ERROR_IS_OK(werr)) {
996 d_fprintf(stderr, _("open_hive failed: %s\n"),
997 win_errstr(werr));
998 goto done;
1000 name = subkeyname;
1003 werr = reg_deletekey_recursive(parent, name);
1004 if (!W_ERROR_IS_OK(werr)) {
1005 d_fprintf(stderr, "reg_deletekey_recursive %s: %s\n", _("failed"),
1006 win_errstr(werr));
1007 goto done;
1010 done:
1011 talloc_free(mem_ctx);
1012 return werr;
1015 static WERROR import_create_val (struct import_ctx* ctx,
1016 struct registry_key* parent, const char* name,
1017 const struct registry_value* value)
1019 WERROR werr;
1021 if (parent == NULL) {
1022 return WERR_INVALID_PARAM;
1025 werr = reg_setvalue(parent, name, value);
1026 if (!W_ERROR_IS_OK(werr)) {
1027 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
1028 win_errstr(werr));
1030 return werr;
1033 static WERROR import_delete_val (struct import_ctx* ctx, struct registry_key* parent, const char* name) {
1034 WERROR werr;
1036 if (parent == NULL) {
1037 return WERR_INVALID_PARAM;
1040 werr = reg_deletevalue(parent, name);
1041 if (!W_ERROR_IS_OK(werr)) {
1042 d_fprintf(stderr, _("reg_deletevalue failed: %s\n"),
1043 win_errstr(werr));
1046 return werr;
1050 static int net_registry_import(struct net_context *c, int argc,
1051 const char **argv)
1053 struct import_ctx import_ctx;
1054 struct reg_import_callback import_callback = {
1055 .openkey = NULL,
1056 .closekey = (reg_import_callback_closekey_t)&import_close_key,
1057 .createkey = (reg_import_callback_createkey_t)&import_create_key,
1058 .deletekey = (reg_import_callback_deletekey_t)&import_delete_key,
1059 .deleteval = (reg_import_callback_deleteval_t)&import_delete_val,
1060 .setval = {
1061 .registry_value = (reg_import_callback_setval_registry_value_t)
1062 &import_create_val,
1064 .setval_type = REGISTRY_VALUE,
1065 .data = &import_ctx
1068 int ret;
1070 if (argc < 1 || argc > 2 || c->display_usage) {
1071 d_printf("%s\n%s",
1072 _("Usage:"),
1073 _("net registry import <reg> [options]\n"));
1074 d_printf("%s\n%s",
1075 _("Example:"),
1076 _("net registry import file.reg enc=CP1252\n"));
1077 return -1;
1080 ZERO_STRUCT(import_ctx);
1081 import_ctx.mem_ctx = talloc_stackframe();
1083 regdb_open();
1084 regdb_transaction_start();
1086 ret = reg_parse_file(argv[0],
1087 reg_import_adapter(import_ctx.mem_ctx,
1088 import_callback),
1089 (argc > 1) ? argv[1] : NULL
1091 if (ret < 0) {
1092 d_printf("reg_parse_file failed: transaction canceled\n");
1093 regdb_transaction_cancel();
1094 } else{
1095 regdb_transaction_commit();
1098 regdb_close();
1099 talloc_free(import_ctx.mem_ctx);
1101 return ret;
1103 /**@}*/
1105 /******************************************************************************/
1108 * @defgroup net_registry_export Export
1109 * @ingroup net_registry
1110 * @{
1113 static int registry_export(TALLOC_CTX *ctx, /*const*/ struct registry_key* key,
1114 struct reg_format* f)
1116 int ret=-1;
1117 WERROR werr;
1118 uint32_t count;
1120 struct registry_value *valvalue = NULL;
1121 char *valname = NULL;
1123 struct registry_key* subkey = NULL;
1124 char *subkey_name = NULL;
1125 NTTIME modtime = 0;
1127 reg_format_registry_key(f, key, false);
1129 /* print values */
1130 for (count = 0;
1131 werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
1132 W_ERROR_IS_OK(werr);
1133 count++)
1135 reg_format_registry_value(f, valname, valvalue);
1137 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
1138 d_fprintf(stderr, _("reg_enumvalue failed: %s\n"),
1139 win_errstr(werr));
1140 goto done;
1143 /* recurse on subkeys */
1144 for (count = 0;
1145 werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
1146 W_ERROR_IS_OK(werr);
1147 count++)
1149 werr = reg_openkey(ctx, key, subkey_name, REG_KEY_READ,
1150 &subkey);
1151 if (!W_ERROR_IS_OK(werr)) {
1152 d_fprintf(stderr, _("reg_openkey failed: %s\n"),
1153 win_errstr(werr));
1154 goto done;
1157 registry_export(ctx, subkey, f);
1159 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
1160 d_fprintf(stderr, _("reg_enumkey failed: %s\n"),
1161 win_errstr(werr));
1162 goto done;
1164 ret = 0;
1165 done:
1166 return ret;
1169 static int net_registry_export(struct net_context *c, int argc,
1170 const char **argv)
1172 int ret=-1;
1173 WERROR werr;
1174 struct registry_key *key = NULL;
1175 TALLOC_CTX *ctx = talloc_stackframe();
1176 struct reg_format* f=NULL;
1178 if (argc < 2 || argc > 3 || c->display_usage) {
1179 d_printf("%s\n%s",
1180 _("Usage:"),
1181 _("net registry export <path> <file> [opt]\n"));
1182 d_printf("%s\n%s",
1183 _("Example:"),
1184 _("net registry export 'HKLM\\Software\\Samba' "
1185 "samba.reg regedit5\n"));
1186 goto done;
1189 werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
1190 if (!W_ERROR_IS_OK(werr)) {
1191 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
1192 goto done;
1195 f = reg_format_file(ctx, argv[1], (argc > 2) ? argv[2] : NULL);
1196 if (f == NULL) {
1197 d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
1198 goto done;
1201 ret = registry_export(ctx, key, f);
1203 done:
1204 TALLOC_FREE(ctx);
1205 return ret;
1207 /**@}*/
1209 /******************************************************************************/
1211 * @defgroup net_registry_convert Convert
1212 * @ingroup net_registry
1213 * @{
1216 static int net_registry_convert(struct net_context *c, int argc,
1217 const char **argv)
1219 int ret;
1220 void* mem_ctx;
1221 const char* in_opt = NULL;
1222 const char* out_opt = NULL;
1224 if (argc < 2 || argc > 4|| c->display_usage) {
1225 d_printf("%s\n%s",
1226 _("Usage:"),
1227 _("net registry convert <in> <out> [in_opt] [out_opt]\n"
1228 "net registry convert <in> <out> [out_opt]\n"));
1229 d_printf("%s\n%s",
1230 _("Example:"),
1231 _("net registry convert in.reg out.reg regedit4,enc=CP1252\n"));
1232 return -1;
1235 mem_ctx = talloc_stackframe();
1237 switch (argc ) {
1238 case 2:
1239 break;
1240 case 3:
1241 out_opt = argv[2];
1242 break;
1243 case 4:
1244 out_opt = argv[3];
1245 in_opt = argv[2];
1246 break;
1247 default:
1248 assert(false);
1252 ret = reg_parse_file(argv[0], (struct reg_parse_callback*)
1253 reg_format_file(mem_ctx, argv[1], out_opt),
1254 in_opt);
1256 talloc_free(mem_ctx);
1258 return ret;
1260 /**@}*/
1262 /******************************************************************************/
1264 int net_registry(struct net_context *c, int argc, const char **argv)
1266 int ret = -1;
1268 struct functable func[] = {
1270 "enumerate",
1271 net_registry_enumerate,
1272 NET_TRANSPORT_LOCAL,
1273 N_("Enumerate registry keys and values"),
1274 N_("net registry enumerate\n"
1275 " Enumerate registry keys and values")
1278 "enumerate_recursive",
1279 net_registry_enumerate_recursive,
1280 NET_TRANSPORT_LOCAL,
1281 N_("Enumerate registry keys and values"),
1282 N_("net registry enumerate_recursive\n"
1283 " Enumerate registry keys and values")
1286 "createkey",
1287 net_registry_createkey,
1288 NET_TRANSPORT_LOCAL,
1289 N_("Create a new registry key"),
1290 N_("net registry createkey\n"
1291 " Create a new registry key")
1294 "deletekey",
1295 net_registry_deletekey,
1296 NET_TRANSPORT_LOCAL,
1297 N_("Delete a registry key"),
1298 N_("net registry deletekey\n"
1299 " Delete a registry key")
1302 "deletekey_recursive",
1303 net_registry_deletekey_recursive,
1304 NET_TRANSPORT_LOCAL,
1305 N_("Delete a registry key with subkeys"),
1306 N_("net registry deletekey_recursive\n"
1307 " Delete a registry key with subkeys")
1310 "getvalue",
1311 net_registry_getvalue,
1312 NET_TRANSPORT_LOCAL,
1313 N_("Print a registry value"),
1314 N_("net registry getvalue\n"
1315 " Print a registry value")
1318 "getvalueraw",
1319 net_registry_getvalueraw,
1320 NET_TRANSPORT_LOCAL,
1321 N_("Print a registry value (raw format)"),
1322 N_("net registry getvalueraw\n"
1323 " Print a registry value (raw format)")
1326 "getvaluesraw",
1327 net_registry_getvaluesraw,
1328 NET_TRANSPORT_LOCAL,
1329 "Print all values of a key in raw format",
1330 "net registry getvaluesraw <key>\n"
1331 " Print a registry value (raw format)"
1334 "setvalue",
1335 net_registry_setvalue,
1336 NET_TRANSPORT_LOCAL,
1337 N_("Set a new registry value"),
1338 N_("net registry setvalue\n"
1339 " Set a new registry value")
1342 "increment",
1343 net_registry_increment,
1344 NET_TRANSPORT_LOCAL,
1345 N_("Increment a DWORD registry value under a lock"),
1346 N_("net registry increment\n"
1347 " Increment a DWORD registry value under a lock")
1350 "deletevalue",
1351 net_registry_deletevalue,
1352 NET_TRANSPORT_LOCAL,
1353 N_("Delete a registry value"),
1354 N_("net registry deletevalue\n"
1355 " Delete a registry value")
1358 "getsd",
1359 net_registry_getsd,
1360 NET_TRANSPORT_LOCAL,
1361 N_("Get security descriptor"),
1362 N_("net registry getsd\n"
1363 " Get security descriptor")
1366 "getsd_sddl",
1367 net_registry_getsd_sddl,
1368 NET_TRANSPORT_LOCAL,
1369 N_("Get security descriptor in sddl format"),
1370 N_("net registry getsd_sddl\n"
1371 " Get security descriptor in sddl format")
1374 "setsd_sddl",
1375 net_registry_setsd_sddl,
1376 NET_TRANSPORT_LOCAL,
1377 N_("Set security descriptor from sddl format string"),
1378 N_("net registry setsd_sddl\n"
1379 " Set security descriptor from sddl format string")
1382 "import",
1383 net_registry_import,
1384 NET_TRANSPORT_LOCAL,
1385 N_("Import .reg file"),
1386 N_("net registry import\n"
1387 " Import .reg file")
1390 "export",
1391 net_registry_export,
1392 NET_TRANSPORT_LOCAL,
1393 N_("Export .reg file"),
1394 N_("net registry export\n"
1395 " Export .reg file")
1398 "convert",
1399 net_registry_convert,
1400 NET_TRANSPORT_LOCAL,
1401 N_("Convert .reg file"),
1402 N_("net registry convert\n"
1403 " Convert .reg file")
1405 { NULL, NULL, 0, NULL, NULL }
1408 if (!W_ERROR_IS_OK(registry_init_basic())) {
1409 return -1;
1412 ret = net_run_function(c, argc, argv, "net registry", func);
1414 return ret;