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