s3-net: add command registry import
[gd/samba-autobuild/.git] / source3 / utils / net_registry.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Distributed SMB/CIFS Server Management Utility
4  * Local registry interface
5  *
6  * Copyright (C) Michael Adam 2008
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
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 <assert.h>
33
34 /*
35  *
36  * Helper functions
37  *
38  */
39
40 /**
41  * split given path into hive and remaining path and open the hive key
42  */
43 static WERROR open_hive(TALLOC_CTX *ctx, const char *path,
44                         uint32 desired_access,
45                         struct registry_key **hive,
46                         char **subkeyname)
47 {
48         WERROR werr;
49         struct security_token *token = NULL;
50         char *hivename = NULL;
51         char *tmp_subkeyname = NULL;
52         TALLOC_CTX *tmp_ctx = talloc_stackframe();
53
54         if ((hive == NULL) || (subkeyname == NULL)) {
55                 werr = WERR_INVALID_PARAM;
56                 goto done;
57         }
58
59         werr = split_hive_key(tmp_ctx, path, &hivename, &tmp_subkeyname);
60         if (!W_ERROR_IS_OK(werr)) {
61                 goto done;
62         }
63         *subkeyname = talloc_strdup(ctx, tmp_subkeyname);
64         if (*subkeyname == NULL) {
65                 werr = WERR_NOMEM;
66                 goto done;
67         }
68
69         werr = ntstatus_to_werror(registry_create_admin_token(tmp_ctx, &token));
70         if (!W_ERROR_IS_OK(werr)) {
71                 goto done;
72         }
73
74         werr = reg_openhive(ctx, hivename, desired_access, token, hive);
75         if (!W_ERROR_IS_OK(werr)) {
76                 goto done;
77         }
78
79         werr = WERR_OK;
80
81 done:
82         TALLOC_FREE(tmp_ctx);
83         return werr;
84 }
85
86 static WERROR open_key(TALLOC_CTX *ctx, const char *path,
87                        uint32 desired_access,
88                        struct registry_key **key)
89 {
90         WERROR werr;
91         char *subkey_name = NULL;
92         struct registry_key *hive = NULL;
93         TALLOC_CTX *tmp_ctx = talloc_stackframe();
94
95         if ((path == NULL) || (key == NULL)) {
96                 return WERR_INVALID_PARAM;
97         }
98
99         werr = open_hive(tmp_ctx, path, desired_access, &hive, &subkey_name);
100         if (!W_ERROR_IS_OK(werr)) {
101                 d_fprintf(stderr, _("open_hive failed: %s\n"),
102                           win_errstr(werr));
103                 goto done;
104         }
105
106         werr = reg_openkey(ctx, hive, subkey_name, desired_access, key);
107         if (!W_ERROR_IS_OK(werr)) {
108                 d_fprintf(stderr, _("reg_openkey failed: %s\n"),
109                           win_errstr(werr));
110                 goto done;
111         }
112
113         werr = WERR_OK;
114
115 done:
116         TALLOC_FREE(tmp_ctx);
117         return werr;
118 }
119
120 /*
121  *
122  * the main "net registry" function implementations
123  *
124  */
125
126 static int net_registry_enumerate(struct net_context *c, int argc,
127                                   const char **argv)
128 {
129         WERROR werr;
130         struct registry_key *key = NULL;
131         TALLOC_CTX *ctx = talloc_stackframe();
132         char *subkey_name;
133         NTTIME modtime;
134         uint32_t count;
135         char *valname = NULL;
136         struct registry_value *valvalue = NULL;
137         int ret = -1;
138
139         if (argc != 1 || c->display_usage) {
140                 d_printf("%s\n%s",
141                          _("Usage:"),
142                          _("net registry enumerate <path>\n"));
143                 d_printf("%s\n%s",
144                          _("Example:"),
145                          _("net registry enumerate 'HKLM\\Software\\Samba'\n"));
146                 goto done;
147         }
148
149         werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
150         if (!W_ERROR_IS_OK(werr)) {
151                 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
152                 goto done;
153         }
154
155         for (count = 0;
156              werr = reg_enumkey(ctx, key, count, &subkey_name, &modtime),
157              W_ERROR_IS_OK(werr);
158              count++)
159         {
160                 print_registry_key(subkey_name, &modtime);
161         }
162         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
163                 goto done;
164         }
165
166         for (count = 0;
167              werr = reg_enumvalue(ctx, key, count, &valname, &valvalue),
168              W_ERROR_IS_OK(werr);
169              count++)
170         {
171                 print_registry_value_with_name(valname, valvalue);
172         }
173         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
174                 goto done;
175         }
176
177         ret = 0;
178 done:
179         TALLOC_FREE(ctx);
180         return ret;
181 }
182
183 static int net_registry_createkey(struct net_context *c, int argc,
184                                   const char **argv)
185 {
186         WERROR werr;
187         enum winreg_CreateAction action;
188         char *subkeyname;
189         struct registry_key *hivekey = NULL;
190         struct registry_key *subkey = NULL;
191         TALLOC_CTX *ctx = talloc_stackframe();
192         int ret = -1;
193
194         if (argc != 1 || c->display_usage) {
195                 d_printf("%s\n%s",
196                          _("Usage:"),
197                          _("net registry createkey <path>\n"));
198                 d_printf("%s\n%s",
199                          _("Example:"),
200                          _("net registry createkey "
201                            "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
202                 goto done;
203         }
204         if (strlen(argv[0]) == 0) {
205                 d_fprintf(stderr, _("error: zero length key name given\n"));
206                 goto done;
207         }
208
209         werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
210         if (!W_ERROR_IS_OK(werr)) {
211                 d_fprintf(stderr, _("open_hive failed: %s\n"),
212                           win_errstr(werr));
213                 goto done;
214         }
215
216         werr = reg_createkey(ctx, hivekey, subkeyname, REG_KEY_WRITE,
217                              &subkey, &action);
218         if (!W_ERROR_IS_OK(werr)) {
219                 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
220                           win_errstr(werr));
221                 goto done;
222         }
223         switch (action) {
224                 case REG_ACTION_NONE:
225                         d_printf(_("createkey did nothing -- huh?\n"));
226                         break;
227                 case REG_CREATED_NEW_KEY:
228                         d_printf(_("createkey created %s\n"), argv[0]);
229                         break;
230                 case REG_OPENED_EXISTING_KEY:
231                         d_printf(_("createkey opened existing %s\n"), argv[0]);
232                         break;
233         }
234
235         ret = 0;
236
237 done:
238         TALLOC_FREE(ctx);
239         return ret;
240 }
241
242 static int net_registry_deletekey(struct net_context *c, int argc,
243                                   const char **argv)
244 {
245         WERROR werr;
246         char *subkeyname;
247         struct registry_key *hivekey = NULL;
248         TALLOC_CTX *ctx = talloc_stackframe();
249         int ret = -1;
250
251         if (argc != 1 || c->display_usage) {
252                 d_printf("%s\n%s",
253                          _("Usage:"),
254                          _("net registry deletekey <path>\n"));
255                 d_printf("%s\n%s",
256                          _("Example:"),
257                          _("net registry deletekey "
258                            "'HKLM\\Software\\Samba\\smbconf.127.0.0.1'\n"));
259                 goto done;
260         }
261         if (strlen(argv[0]) == 0) {
262                 d_fprintf(stderr, _("error: zero length key name given\n"));
263                 goto done;
264         }
265
266         werr = open_hive(ctx, argv[0], REG_KEY_WRITE, &hivekey, &subkeyname);
267         if (!W_ERROR_IS_OK(werr)) {
268                 d_fprintf(stderr, "open_hive %s: %s\n", _("failed"),
269                           win_errstr(werr));
270                 goto done;
271         }
272
273         werr = reg_deletekey(hivekey, subkeyname);
274         if (!W_ERROR_IS_OK(werr)) {
275                 d_fprintf(stderr, "reg_deletekey %s: %s\n", _("failed"),
276                           win_errstr(werr));
277                 goto done;
278         }
279
280         ret = 0;
281
282 done:
283         TALLOC_FREE(ctx);
284         return ret;
285 }
286
287 static int net_registry_getvalue_internal(struct net_context *c, int argc,
288                                           const char **argv, bool raw)
289 {
290         WERROR werr;
291         int ret = -1;
292         struct registry_key *key = NULL;
293         struct registry_value *value = NULL;
294         TALLOC_CTX *ctx = talloc_stackframe();
295
296         if (argc != 2 || c->display_usage) {
297                 d_fprintf(stderr, "%s\n%s",
298                           _("Usage:"),
299                           _("net registry getvalue <key> <valuename>\n"));
300                 goto done;
301         }
302
303         werr = open_key(ctx, argv[0], REG_KEY_READ, &key);
304         if (!W_ERROR_IS_OK(werr)) {
305                 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
306                 goto done;
307         }
308
309         werr = reg_queryvalue(ctx, key, argv[1], &value);
310         if (!W_ERROR_IS_OK(werr)) {
311                 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
312                           win_errstr(werr));
313                 goto done;
314         }
315
316         print_registry_value(value, raw);
317
318         ret = 0;
319
320 done:
321         TALLOC_FREE(ctx);
322         return ret;
323 }
324
325 static int net_registry_getvalue(struct net_context *c, int argc,
326                                  const char **argv)
327 {
328         return net_registry_getvalue_internal(c, argc, argv, false);
329 }
330
331 static int net_registry_getvalueraw(struct net_context *c, int argc,
332                                     const char **argv)
333 {
334         return net_registry_getvalue_internal(c, argc, argv, true);
335 }
336
337 static int net_registry_setvalue(struct net_context *c, int argc,
338                                  const char **argv)
339 {
340         WERROR werr;
341         struct registry_value value;
342         struct registry_key *key = NULL;
343         int ret = -1;
344         TALLOC_CTX *ctx = talloc_stackframe();
345
346         if (argc < 4 || c->display_usage) {
347                 d_fprintf(stderr, "%s\n%s",
348                           _("Usage:"),
349                           _("net registry setvalue <key> <valuename> "
350                             "<type> [<val>]+\n"));
351                 goto done;
352         }
353
354         if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
355                 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
356                 goto done;
357         }
358
359         if (strequal(argv[2], "dword")) {
360                 uint32_t v = strtoul(argv[3], NULL, 10);
361                 value.type = REG_DWORD;
362                 value.data = data_blob_talloc(ctx, NULL, 4);
363                 SIVAL(value.data.data, 0, v);
364         } else if (strequal(argv[2], "sz")) {
365                 value.type = REG_SZ;
366                 if (!push_reg_sz(ctx, &value.data, argv[3])) {
367                         goto done;
368                 }
369         } else if (strequal(argv[2], "multi_sz")) {
370                 const char **array;
371                 int count = argc - 3;
372                 int i;
373                 value.type = REG_MULTI_SZ;
374                 array = talloc_zero_array(ctx, const char *, count + 1);
375                 if (array == NULL) {
376                         goto done;
377                 }
378                 for (i=0; i < count; i++) {
379                         array[i] = talloc_strdup(array, argv[count+i]);
380                         if (array[i] == NULL) {
381                                 goto done;
382                         }
383                 }
384                 if (!push_reg_multi_sz(ctx, &value.data, array)) {
385                         goto done;
386                 }
387         } else {
388                 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
389                 goto done;
390         }
391
392         werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
393         if (!W_ERROR_IS_OK(werr)) {
394                 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
395                 goto done;
396         }
397
398         werr = reg_setvalue(key, argv[1], &value);
399         if (!W_ERROR_IS_OK(werr)) {
400                 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
401                           win_errstr(werr));
402                 goto done;
403         }
404
405         ret = 0;
406
407 done:
408         TALLOC_FREE(ctx);
409         return ret;
410 }
411
412 struct net_registry_increment_state {
413         const char *keyname;
414         const char *valuename;
415         uint32_t increment;
416         uint32_t newvalue;
417         WERROR werr;
418 };
419
420 static void net_registry_increment_fn(void *private_data)
421 {
422         struct net_registry_increment_state *state =
423                 (struct net_registry_increment_state *)private_data;
424         struct registry_value *value;
425         struct registry_key *key = NULL;
426         uint32_t v;
427
428         state->werr = open_key(talloc_tos(), state->keyname,
429                                REG_KEY_READ|REG_KEY_WRITE, &key);
430         if (!W_ERROR_IS_OK(state->werr)) {
431                 d_fprintf(stderr, _("open_key failed: %s\n"),
432                           win_errstr(state->werr));
433                 goto done;
434         }
435
436         state->werr = reg_queryvalue(key, key, state->valuename, &value);
437         if (!W_ERROR_IS_OK(state->werr)) {
438                 d_fprintf(stderr, _("reg_queryvalue failed: %s\n"),
439                           win_errstr(state->werr));
440                 goto done;
441         }
442
443         if (value->type != REG_DWORD) {
444                 d_fprintf(stderr, _("value not a DWORD: %s\n"),
445                           str_regtype(value->type));
446                 goto done;
447         }
448
449         if (value->data.length < 4) {
450                 d_fprintf(stderr, _("value too short for regular DWORD\n"));
451                 goto done;
452         }
453
454         v = IVAL(value->data.data, 0);
455         v += state->increment;
456         state->newvalue = v;
457
458         SIVAL(value->data.data, 0, v);
459
460         state->werr = reg_setvalue(key, state->valuename, value);
461         if (!W_ERROR_IS_OK(state->werr)) {
462                 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
463                           win_errstr(state->werr));
464                 goto done;
465         }
466
467 done:
468         TALLOC_FREE(key);
469         return;
470 }
471
472 static int net_registry_increment(struct net_context *c, int argc,
473                                   const char **argv)
474 {
475         struct net_registry_increment_state state;
476         NTSTATUS status;
477         int ret = -1;
478
479         if (argc < 2 || c->display_usage) {
480                 d_fprintf(stderr, "%s\n%s",
481                           _("Usage:"),
482                           _("net registry increment <key> <valuename> "
483                             "[<increment>]\n"));
484                 goto done;
485         }
486
487         state.keyname = argv[0];
488         state.valuename = argv[1];
489
490         state.increment = 1;
491         if (argc == 3) {
492                 state.increment = strtoul(argv[2], NULL, 10);
493         }
494
495         status = g_lock_do("registry_increment_lock", G_LOCK_WRITE,
496                            timeval_set(600, 0), procid_self(),
497                            net_registry_increment_fn, &state);
498         if (!NT_STATUS_IS_OK(status)) {
499                 d_fprintf(stderr, _("g_lock_do failed: %s\n"),
500                           nt_errstr(status));
501                 goto done;
502         }
503         if (!W_ERROR_IS_OK(state.werr)) {
504                 d_fprintf(stderr, _("increment failed: %s\n"),
505                           win_errstr(state.werr));
506                 goto done;
507         }
508
509         d_printf(_("%u\n"), (unsigned)state.newvalue);
510
511         ret = 0;
512
513 done:
514         return ret;
515 }
516
517 static int net_registry_deletevalue(struct net_context *c, int argc,
518                                     const char **argv)
519 {
520         WERROR werr;
521         struct registry_key *key = NULL;
522         TALLOC_CTX *ctx = talloc_stackframe();
523         int ret = -1;
524
525         if (argc != 2 || c->display_usage) {
526                 d_fprintf(stderr, "%s\n%s",
527                           _("Usage:"),
528                           _("net registry deletevalue <key> <valuename>\n"));
529                 goto done;
530         }
531
532         werr = open_key(ctx, argv[0], REG_KEY_WRITE, &key);
533         if (!W_ERROR_IS_OK(werr)) {
534                 d_fprintf(stderr, _("open_key failed: %s\n"), win_errstr(werr));
535                 goto done;
536         }
537
538         werr = reg_deletevalue(key, argv[1]);
539         if (!W_ERROR_IS_OK(werr)) {
540                 d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
541                           win_errstr(werr));
542                 goto done;
543         }
544
545         ret = 0;
546
547 done:
548         TALLOC_FREE(ctx);
549         return ret;
550 }
551
552 static WERROR net_registry_getsd_internal(struct net_context *c,
553                                           TALLOC_CTX *mem_ctx,
554                                           const char *keyname,
555                                           struct security_descriptor **sd)
556 {
557         WERROR werr;
558         struct registry_key *key = NULL;
559         TALLOC_CTX *ctx = talloc_stackframe();
560         uint32_t access_mask = REG_KEY_READ |
561                                SEC_FLAG_MAXIMUM_ALLOWED |
562                                SEC_FLAG_SYSTEM_SECURITY;
563
564         /*
565          * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
566          * is denied with these perms right now...
567          */
568         access_mask = REG_KEY_READ;
569
570         if (sd == NULL) {
571                 d_fprintf(stderr, _("internal error: invalid argument\n"));
572                 werr = WERR_INVALID_PARAM;
573                 goto done;
574         }
575
576         if (strlen(keyname) == 0) {
577                 d_fprintf(stderr, _("error: zero length key name given\n"));
578                 werr = WERR_INVALID_PARAM;
579                 goto done;
580         }
581
582         werr = open_key(ctx, keyname, access_mask, &key);
583         if (!W_ERROR_IS_OK(werr)) {
584                 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
585                           win_errstr(werr));
586                 goto done;
587         }
588
589         werr = reg_getkeysecurity(mem_ctx, key, sd);
590         if (!W_ERROR_IS_OK(werr)) {
591                 d_fprintf(stderr, "%s%s\n", _("reg_getkeysecurity failed: "),
592                           win_errstr(werr));
593                 goto done;
594         }
595
596         werr = WERR_OK;
597
598 done:
599         TALLOC_FREE(ctx);
600         return werr;
601 }
602
603 static int net_registry_getsd(struct net_context *c, int argc,
604                               const char **argv)
605 {
606         WERROR werr;
607         int ret = -1;
608         struct security_descriptor *secdesc = NULL;
609         TALLOC_CTX *ctx = talloc_stackframe();
610
611         if (argc != 1 || c->display_usage) {
612                 d_printf("%s\n%s",
613                          _("Usage:"),
614                          _("net registry getsd <path>\n"));
615                 d_printf("%s\n%s",
616                          _("Example:"),
617                          _("net registry getsd 'HKLM\\Software\\Samba'\n"));
618                 goto done;
619         }
620
621         werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
622         if (!W_ERROR_IS_OK(werr)) {
623                 goto done;
624         }
625
626         display_sec_desc(secdesc);
627
628         ret = 0;
629
630 done:
631         TALLOC_FREE(ctx);
632         return ret;
633 }
634
635 static int net_registry_getsd_sddl(struct net_context *c,
636                                    int argc, const char **argv)
637 {
638         WERROR werr;
639         int ret = -1;
640         struct security_descriptor *secdesc = NULL;
641         TALLOC_CTX *ctx = talloc_stackframe();
642
643         if (argc != 1 || c->display_usage) {
644                 d_printf("%s\n%s",
645                          _("Usage:"),
646                          _("net registry getsd_sddl <path>\n"));
647                 d_printf("%s\n%s",
648                          _("Example:"),
649                          _("net registry getsd_sddl 'HKLM\\Software\\Samba'\n"));
650                 goto done;
651         }
652
653         werr = net_registry_getsd_internal(c, ctx, argv[0], &secdesc);
654         if (!W_ERROR_IS_OK(werr)) {
655                 goto done;
656         }
657
658         d_printf("%s\n", sddl_encode(ctx, secdesc, get_global_sam_sid()));
659
660         ret = 0;
661
662 done:
663         TALLOC_FREE(ctx);
664         return ret;
665 }
666
667 static WERROR net_registry_setsd_internal(struct net_context *c,
668                                           TALLOC_CTX *mem_ctx,
669                                           const char *keyname,
670                                           struct security_descriptor *sd)
671 {
672         WERROR werr;
673         struct registry_key *key = NULL;
674         TALLOC_CTX *ctx = talloc_stackframe();
675         uint32_t access_mask = REG_KEY_WRITE |
676                                SEC_FLAG_MAXIMUM_ALLOWED |
677                                SEC_FLAG_SYSTEM_SECURITY;
678
679         /*
680          * net_rpc_regsitry uses SEC_FLAG_SYSTEM_SECURITY, but access
681          * is denied with these perms right now...
682          */
683         access_mask = REG_KEY_WRITE;
684
685         if (strlen(keyname) == 0) {
686                 d_fprintf(stderr, _("error: zero length key name given\n"));
687                 werr = WERR_INVALID_PARAM;
688                 goto done;
689         }
690
691         werr = open_key(ctx, keyname, access_mask, &key);
692         if (!W_ERROR_IS_OK(werr)) {
693                 d_fprintf(stderr, "%s%s\n", _("open_key failed: "),
694                           win_errstr(werr));
695                 goto done;
696         }
697
698         werr = reg_setkeysecurity(key, sd);
699         if (!W_ERROR_IS_OK(werr)) {
700                 d_fprintf(stderr, "%s%s\n", _("reg_setkeysecurity failed: "),
701                           win_errstr(werr));
702                 goto done;
703         }
704
705         werr = WERR_OK;
706
707 done:
708         TALLOC_FREE(ctx);
709         return werr;
710 }
711
712 static int net_registry_setsd_sddl(struct net_context *c,
713                                    int argc, const char **argv)
714 {
715         WERROR werr;
716         int ret = -1;
717         struct security_descriptor *secdesc = NULL;
718         TALLOC_CTX *ctx = talloc_stackframe();
719
720         if (argc != 2 || c->display_usage) {
721                 d_printf("%s\n%s",
722                          _("Usage:"),
723                          _("net registry setsd_sddl <path> <security_descriptor>\n"));
724                 d_printf("%s\n%s",
725                          _("Example:"),
726                          _("net registry setsd_sddl 'HKLM\\Software\\Samba'\n"));
727                 goto done;
728         }
729
730         secdesc = sddl_decode(ctx, argv[1], get_global_sam_sid());
731         if (secdesc == NULL) {
732                 goto done;
733         }
734
735         werr = net_registry_setsd_internal(c, ctx, argv[0], secdesc);
736         if (!W_ERROR_IS_OK(werr)) {
737                 goto done;
738         }
739
740         ret = 0;
741
742 done:
743         TALLOC_FREE(ctx);
744         return ret;
745 }
746
747 /******************************************************************************/
748 /**
749  * @defgroup net_registry net registry
750  */
751
752 /**
753  * @defgroup net_registry_import Import
754  * @ingroup net_registry
755  * @{
756  */
757
758 struct import_ctx {
759         TALLOC_CTX *mem_ctx;
760 };
761
762
763 static WERROR import_create_key(struct import_ctx* ctx,
764                                 struct registry_key* parent,
765                                 const char* name, void** pkey, bool* existing)
766 {
767         WERROR werr;
768         void* mem_ctx = talloc_new(ctx->mem_ctx);
769
770         struct registry_key* key = NULL;
771         enum winreg_CreateAction action;
772
773         if (parent == NULL) {
774                 char* subkeyname = NULL;
775                 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
776                          &parent, &subkeyname);
777                 if (!W_ERROR_IS_OK(werr)) {
778                         d_fprintf(stderr, _("open_hive failed: %s\n"),
779                                   win_errstr(werr));
780                         goto done;
781                 }
782                 name = subkeyname;
783         }
784
785         action = REG_ACTION_NONE;
786         werr = reg_createkey(mem_ctx, parent, name, REG_KEY_WRITE,
787                              &key, &action);
788         if (!W_ERROR_IS_OK(werr)) {
789                 d_fprintf(stderr, _("reg_createkey failed: %s\n"),
790                           win_errstr(werr));
791                 goto done;
792         }
793
794         if (action == REG_ACTION_NONE) {
795                 d_fprintf(stderr, _("createkey did nothing -- huh?\n"));
796                 werr = WERR_CREATE_FAILED;
797                 goto done;
798         }
799
800         if (existing != NULL) {
801                 *existing = (action == REG_OPENED_EXISTING_KEY);
802         }
803
804         if (pkey!=NULL) {
805                 *pkey = talloc_steal(ctx->mem_ctx, key);
806         }
807
808 done:
809         talloc_free(mem_ctx);
810         return werr;
811 }
812
813 static WERROR import_close_key(struct import_ctx* ctx,
814                                struct registry_key* key)
815 {
816         return WERR_OK;
817 }
818
819 static WERROR import_delete_key(struct import_ctx* ctx,
820                                 struct registry_key* parent, const char* name)
821 {
822         WERROR werr;
823         void* mem_ctx = talloc_new(talloc_tos());
824
825         if (parent == NULL) {
826                 char* subkeyname = NULL;
827                 werr = open_hive(mem_ctx, name, REG_KEY_WRITE,
828                          &parent, &subkeyname);
829                 if (!W_ERROR_IS_OK(werr)) {
830                         d_fprintf(stderr, _("open_hive failed: %s\n"),
831                                   win_errstr(werr));
832                         goto done;
833                 }
834                 name = subkeyname;
835         }
836
837         werr = reg_deletekey_recursive(mem_ctx, parent, name);
838         if (!W_ERROR_IS_OK(werr)) {
839                 d_fprintf(stderr, "reg_deletekey_recursive %s: %s\n", _("failed"),
840                           win_errstr(werr));
841                 goto done;
842         }
843
844 done:
845         talloc_free(mem_ctx);
846         return werr;
847 }
848
849 static WERROR import_create_val (struct import_ctx* ctx,
850                                  struct registry_key* parent, const char* name,
851                                  const struct registry_value* value)
852 {
853         WERROR werr;
854
855         if (parent == NULL) {
856                 return WERR_INVALID_PARAM;
857         }
858
859         werr = reg_setvalue(parent, name, value);
860         if (!W_ERROR_IS_OK(werr)) {
861                 d_fprintf(stderr, _("reg_setvalue failed: %s\n"),
862                           win_errstr(werr));
863         }
864         return werr;
865 }
866
867 static WERROR import_delete_val (struct import_ctx* ctx, struct registry_key* parent, const char* name) {
868         WERROR werr;
869
870         if (parent == NULL) {
871                 return WERR_INVALID_PARAM;
872         }
873
874         werr = reg_deletevalue(parent, name);
875         if (!W_ERROR_IS_OK(werr)) {
876                 d_fprintf(stderr, _("reg_deletekey failed: %s\n"),
877                           win_errstr(werr));
878         }
879
880         return werr;
881 }
882
883
884 static int net_registry_import(struct net_context *c, int argc,
885                                const char **argv)
886 {
887         struct import_ctx import_ctx;
888         struct reg_import_callback import_callback = {
889                 .openkey     = NULL,
890                 .closekey    = (reg_import_callback_closekey_t)&import_close_key,
891                 .createkey   = (reg_import_callback_createkey_t)&import_create_key,
892                 .deletekey   = (reg_import_callback_deletekey_t)&import_delete_key,
893                 .deleteval   = (reg_import_callback_deleteval_t)&import_delete_val,
894                 .setval.registry_value = (reg_import_callback_setval_registry_value_t)
895                 &import_create_val,
896                 .setval_type           = REGISTRY_VALUE,
897                 .data        = &import_ctx
898         };
899
900         int ret;
901
902         if (argc < 1 || argc > 2 || c->display_usage) {
903                 d_printf("%s\n%s",
904                          _("Usage:"),
905                          _("net registry import <reg> [options]\n"));
906                 d_printf("%s\n%s",
907                          _("Example:"),
908                          _("net registry import file.reg enc=CP1252\n"));
909                 return -1;
910         }
911
912         ZERO_STRUCT(import_ctx);
913         import_ctx.mem_ctx = talloc_stackframe();
914
915         regdb_open();
916         regdb_transaction_start();
917
918         ret = reg_parse_file(argv[0],
919                              reg_import_adapter(import_ctx.mem_ctx,
920                                                 import_callback),
921                              (argc > 1) ? argv[1] : NULL
922                 );
923         if (ret < 0) {
924                 d_printf("reg_parse_file failed: transaction canceled\n");
925                 regdb_transaction_cancel();
926         } else{
927                 regdb_transaction_commit();
928         }
929
930         regdb_close();
931         talloc_free(import_ctx.mem_ctx);
932
933         return ret;
934 }
935 /**@}*/
936
937
938 /******************************************************************************/
939 int net_registry(struct net_context *c, int argc, const char **argv)
940 {
941         int ret = -1;
942
943         struct functable func[] = {
944                 {
945                         "enumerate",
946                         net_registry_enumerate,
947                         NET_TRANSPORT_LOCAL,
948                         N_("Enumerate registry keys and values"),
949                         N_("net registry enumerate\n"
950                            "    Enumerate registry keys and values")
951                 },
952                 {
953                         "createkey",
954                         net_registry_createkey,
955                         NET_TRANSPORT_LOCAL,
956                         N_("Create a new registry key"),
957                         N_("net registry createkey\n"
958                            "    Create a new registry key")
959                 },
960                 {
961                         "deletekey",
962                         net_registry_deletekey,
963                         NET_TRANSPORT_LOCAL,
964                         N_("Delete a registry key"),
965                         N_("net registry deletekey\n"
966                            "    Delete a registry key")
967                 },
968                 {
969                         "getvalue",
970                         net_registry_getvalue,
971                         NET_TRANSPORT_LOCAL,
972                         N_("Print a registry value"),
973                         N_("net registry getvalue\n"
974                            "    Print a registry value")
975                 },
976                 {
977                         "getvalueraw",
978                         net_registry_getvalueraw,
979                         NET_TRANSPORT_LOCAL,
980                         N_("Print a registry value (raw format)"),
981                         N_("net registry getvalueraw\n"
982                            "    Print a registry value (raw format)")
983                 },
984                 {
985                         "setvalue",
986                         net_registry_setvalue,
987                         NET_TRANSPORT_LOCAL,
988                         N_("Set a new registry value"),
989                         N_("net registry setvalue\n"
990                            "    Set a new registry value")
991                 },
992                 {
993                         "increment",
994                         net_registry_increment,
995                         NET_TRANSPORT_LOCAL,
996                         N_("Increment a DWORD registry value under a lock"),
997                         N_("net registry increment\n"
998                            "    Increment a DWORD registry value under a lock")
999                 },
1000                 {
1001                         "deletevalue",
1002                         net_registry_deletevalue,
1003                         NET_TRANSPORT_LOCAL,
1004                         N_("Delete a registry value"),
1005                         N_("net registry deletevalue\n"
1006                            "    Delete a registry value")
1007                 },
1008                 {
1009                         "getsd",
1010                         net_registry_getsd,
1011                         NET_TRANSPORT_LOCAL,
1012                         N_("Get security descriptor"),
1013                         N_("net registry getsd\n"
1014                            "    Get security descriptor")
1015                 },
1016                 {
1017                         "getsd_sddl",
1018                         net_registry_getsd_sddl,
1019                         NET_TRANSPORT_LOCAL,
1020                         N_("Get security descriptor in sddl format"),
1021                         N_("net registry getsd_sddl\n"
1022                            "    Get security descriptor in sddl format")
1023                 },
1024                 {
1025                         "setsd_sddl",
1026                         net_registry_setsd_sddl,
1027                         NET_TRANSPORT_LOCAL,
1028                         N_("Set security descriptor from sddl format string"),
1029                         N_("net registry setsd_sddl\n"
1030                            "    Set security descriptor from sddl format string")
1031                 },
1032                 {
1033                         "import",
1034                         net_registry_import,
1035                         NET_TRANSPORT_LOCAL,
1036                         N_("Import .reg file"),
1037                         N_("net registry import\n"
1038                            "    Import .reg file")
1039                 },
1040         { NULL, NULL, 0, NULL, NULL }
1041         };
1042
1043         if (!W_ERROR_IS_OK(registry_init_basic())) {
1044                 return -1;
1045         }
1046
1047         ret = net_run_function(c, argc, argv, "net registry", func);
1048
1049         return ret;
1050 }