190ab05819c9699e210a638b202c740dc7558606
[samba.git] / source3 / utils / net_rpc_registry.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4
5    Copyright (C) Gerald (Jerry) Carter          2005-2006
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19  
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "registry.h"
23 #include "utils/net.h"
24 #include "utils/net_registry_util.h"
25 #include "registry/regfio.h"
26 #include "../librpc/gen_ndr/ndr_winreg_c.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "registry/reg_format.h"
29 #include "registry/reg_import.h"
30 #include <assert.h>
31 #include "../libcli/security/display_sec.h"
32 #include "../libcli/registry/util_reg.h"
33
34
35 /*******************************************************************
36  connect to a registry hive root (open a registry policy)
37 *******************************************************************/
38
39 static NTSTATUS dcerpc_winreg_Connect(struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
40                                       uint32_t reg_type, uint32_t access_mask,
41                                       struct policy_handle *reg_hnd, WERROR *werr)
42 {
43         ZERO_STRUCTP(reg_hnd);
44
45         switch (reg_type)
46         {
47         case HKEY_CLASSES_ROOT:
48                 return dcerpc_winreg_OpenHKCR(b, mem_ctx, NULL,
49                         access_mask, reg_hnd, werr);
50
51         case HKEY_LOCAL_MACHINE:
52                 return dcerpc_winreg_OpenHKLM(b, mem_ctx, NULL,
53                         access_mask, reg_hnd, werr);
54
55         case HKEY_USERS:
56                 return dcerpc_winreg_OpenHKU(b, mem_ctx, NULL,
57                         access_mask, reg_hnd, werr);
58
59         case HKEY_CURRENT_USER:
60                 return dcerpc_winreg_OpenHKCU(b, mem_ctx, NULL,
61                         access_mask, reg_hnd, werr);
62
63         case HKEY_PERFORMANCE_DATA:
64                 return dcerpc_winreg_OpenHKPD(b, mem_ctx, NULL,
65                         access_mask, reg_hnd, werr);
66
67         default:
68                 /* fall through to end of function */
69                 break;
70         }
71
72         return NT_STATUS_INVALID_PARAMETER;
73 }
74
75 static bool reg_hive_key(TALLOC_CTX *ctx, const char *fullname,
76                          uint32 *reg_type, const char **key_name)
77 {
78         WERROR werr;
79         char *hivename = NULL;
80         char *tmp_keyname = NULL;
81         bool ret = false;
82         TALLOC_CTX *tmp_ctx = talloc_stackframe();
83
84         werr = split_hive_key(tmp_ctx, fullname, &hivename, &tmp_keyname);
85         if (!W_ERROR_IS_OK(werr)) {
86                 goto done;
87         }
88
89         *key_name = talloc_strdup(ctx, tmp_keyname);
90         if (*key_name == NULL) {
91                 goto done;
92         }
93
94         if (strequal(hivename, "HKLM") ||
95             strequal(hivename, "HKEY_LOCAL_MACHINE"))
96         {
97                 (*reg_type) = HKEY_LOCAL_MACHINE;
98         } else if (strequal(hivename, "HKCR") ||
99                    strequal(hivename, "HKEY_CLASSES_ROOT"))
100         {
101                 (*reg_type) = HKEY_CLASSES_ROOT;
102         } else if (strequal(hivename, "HKU") ||
103                    strequal(hivename, "HKEY_USERS"))
104         {
105                 (*reg_type) = HKEY_USERS;
106         } else if (strequal(hivename, "HKCU") ||
107                    strequal(hivename, "HKEY_CURRENT_USER"))
108         {
109                 (*reg_type) = HKEY_CURRENT_USER;
110         } else if (strequal(hivename, "HKPD") ||
111                    strequal(hivename, "HKEY_PERFORMANCE_DATA"))
112         {
113                 (*reg_type) = HKEY_PERFORMANCE_DATA;
114         } else {
115                 DEBUG(10,("reg_hive_key: unrecognised hive key %s\n",
116                           fullname));
117                 goto done;
118         }
119
120         ret = true;
121
122 done:
123         TALLOC_FREE(tmp_ctx);
124         return ret;
125 }
126
127 static NTSTATUS registry_openkey(TALLOC_CTX *mem_ctx,
128                                  struct rpc_pipe_client *pipe_hnd,
129                                  const char *name, uint32 access_mask,
130                                  struct policy_handle *hive_hnd,
131                                  struct policy_handle *key_hnd)
132 {
133         uint32 hive;
134         NTSTATUS status;
135         WERROR werr;
136         struct winreg_String key;
137         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
138
139         ZERO_STRUCT(key);
140
141         if (!reg_hive_key(mem_ctx, name, &hive, &key.name)) {
142                 return NT_STATUS_INVALID_PARAMETER;
143         }
144
145         status = dcerpc_winreg_Connect(b, mem_ctx, hive, access_mask,
146                                        hive_hnd, &werr);
147         if (!(NT_STATUS_IS_OK(status))) {
148                 return status;
149         }
150         if (!W_ERROR_IS_OK(werr)) {
151                 return werror_to_ntstatus(werr);
152         }
153
154         status = dcerpc_winreg_OpenKey(b, mem_ctx, hive_hnd, key, 0,
155                                        access_mask, key_hnd, &werr);
156         if (!(NT_STATUS_IS_OK(status))) {
157                 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &werr);
158                 return status;
159         }
160         if (!(W_ERROR_IS_OK(werr))) {
161                 WERROR _werr;
162                 dcerpc_winreg_CloseKey(b, mem_ctx, hive_hnd, &_werr);
163                 return werror_to_ntstatus(werr);
164         }
165
166         return NT_STATUS_OK;
167 }
168
169 static NTSTATUS registry_enumkeys(TALLOC_CTX *ctx,
170                                   struct rpc_pipe_client *pipe_hnd,
171                                   struct policy_handle *key_hnd,
172                                   uint32 *pnum_keys, char ***pnames,
173                                   char ***pclasses, NTTIME ***pmodtimes)
174 {
175         TALLOC_CTX *mem_ctx;
176         NTSTATUS status;
177         WERROR werr;
178         uint32 num_subkeys, max_subkeylen, max_classlen;
179         uint32 num_values, max_valnamelen, max_valbufsize;
180         uint32 i;
181         NTTIME last_changed_time;
182         uint32 secdescsize;
183         struct winreg_String classname;
184         char **names, **classes;
185         NTTIME **modtimes;
186         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
187
188         if (!(mem_ctx = talloc_new(ctx))) {
189                 return NT_STATUS_NO_MEMORY;
190         }
191
192         ZERO_STRUCT(classname);
193         status = dcerpc_winreg_QueryInfoKey(
194                 b, mem_ctx, key_hnd, &classname, &num_subkeys,
195                 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
196                 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
197
198         if (!NT_STATUS_IS_OK(status)) {
199                 goto error;
200         }
201         if (!W_ERROR_IS_OK(werr)) {
202                 status = werror_to_ntstatus(werr);
203                 goto error;
204         }
205
206         if (num_subkeys == 0) {
207                 *pnum_keys = 0;
208                 TALLOC_FREE(mem_ctx);
209                 return NT_STATUS_OK;
210         }
211
212         if ((!(names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
213             (!(classes = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_subkeys))) ||
214             (!(modtimes = TALLOC_ZERO_ARRAY(mem_ctx, NTTIME *,
215                                             num_subkeys)))) {
216                 status = NT_STATUS_NO_MEMORY;
217                 goto error;
218         }
219
220         for (i=0; i<num_subkeys; i++) {
221                 char c, n;
222                 struct winreg_StringBuf class_buf;
223                 struct winreg_StringBuf name_buf;
224                 NTTIME modtime;
225
226                 c = '\0';
227                 class_buf.name = &c;
228                 class_buf.size = max_classlen+2;
229
230                 n = '\0';
231                 name_buf.name = &n;
232                 name_buf.size = max_subkeylen+2;
233
234                 ZERO_STRUCT(modtime);
235
236                 status = dcerpc_winreg_EnumKey(b, mem_ctx, key_hnd,
237                                                i, &name_buf, &class_buf,
238                                                &modtime, &werr);
239                 if (!NT_STATUS_IS_OK(status)) {
240                         goto error;
241                 }
242                 if (W_ERROR_EQUAL(werr,
243                                   WERR_NO_MORE_ITEMS) ) {
244                         status = NT_STATUS_OK;
245                         break;
246                 }
247                 if (!W_ERROR_IS_OK(werr)) {
248                         status = werror_to_ntstatus(werr);
249                         goto error;
250                 }
251
252                 classes[i] = NULL;
253
254                 if (class_buf.name &&
255                     (!(classes[i] = talloc_strdup(classes, class_buf.name)))) {
256                         status = NT_STATUS_NO_MEMORY;
257                         goto error;
258                 }
259
260                 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
261                         status = NT_STATUS_NO_MEMORY;
262                         goto error;
263                 }
264
265                 if ((!(modtimes[i] = (NTTIME *)talloc_memdup(
266                                modtimes, &modtime, sizeof(modtime))))) {
267                         status = NT_STATUS_NO_MEMORY;
268                         goto error;
269                 }
270         }
271
272         *pnum_keys = num_subkeys;
273
274         if (pnames) {
275                 *pnames = talloc_move(ctx, &names);
276         }
277         if (pclasses) {
278                 *pclasses = talloc_move(ctx, &classes);
279         }
280         if (pmodtimes) {
281                 *pmodtimes = talloc_move(ctx, &modtimes);
282         }
283
284         status = NT_STATUS_OK;
285
286  error:
287         TALLOC_FREE(mem_ctx);
288         return status;
289 }
290
291 static NTSTATUS registry_enumvalues(TALLOC_CTX *ctx,
292                                     struct rpc_pipe_client *pipe_hnd,
293                                     struct policy_handle *key_hnd,
294                                     uint32 *pnum_values, char ***pvalnames,
295                                     struct registry_value ***pvalues)
296 {
297         TALLOC_CTX *mem_ctx;
298         NTSTATUS status;
299         WERROR werr;
300         uint32 num_subkeys, max_subkeylen, max_classlen;
301         uint32 num_values, max_valnamelen, max_valbufsize;
302         uint32 i;
303         NTTIME last_changed_time;
304         uint32 secdescsize;
305         struct winreg_String classname;
306         struct registry_value **values;
307         char **names;
308         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
309
310         if (!(mem_ctx = talloc_new(ctx))) {
311                 return NT_STATUS_NO_MEMORY;
312         }
313
314         ZERO_STRUCT(classname);
315         status = dcerpc_winreg_QueryInfoKey(
316                 b, mem_ctx, key_hnd, &classname, &num_subkeys,
317                 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
318                 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
319
320         if (!NT_STATUS_IS_OK(status)) {
321                 goto error;
322         }
323         if (!W_ERROR_IS_OK(werr)) {
324                 status = werror_to_ntstatus(werr);
325                 goto error;
326         }
327
328         if (num_values == 0) {
329                 *pnum_values = 0;
330                 TALLOC_FREE(mem_ctx);
331                 return NT_STATUS_OK;
332         }
333
334         if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
335             (!(values = TALLOC_ARRAY(mem_ctx, struct registry_value *,
336                                      num_values)))) {
337                 status = NT_STATUS_NO_MEMORY;
338                 goto error;
339         }
340
341         for (i=0; i<num_values; i++) {
342                 enum winreg_Type type = REG_NONE;
343                 uint8 *data = NULL;
344                 uint32 data_size;
345                 uint32 value_length;
346
347                 char n;
348                 struct winreg_ValNameBuf name_buf;
349                 WERROR err;
350
351                 n = '\0';
352                 name_buf.name = &n;
353                 name_buf.size = max_valnamelen + 2;
354
355                 data_size = max_valbufsize;
356                 data = (uint8 *)TALLOC(mem_ctx, data_size);
357                 value_length = 0;
358
359                 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
360                                                  i, &name_buf, &type,
361                                                  data, &data_size,
362                                                  &value_length, &err);
363                 if (!(NT_STATUS_IS_OK(status))) {
364                         goto error;
365                 }
366
367                 if ( W_ERROR_EQUAL(err,
368                                    WERR_NO_MORE_ITEMS) ) {
369                         status = NT_STATUS_OK;
370                         break;
371                 }
372
373                 if (!W_ERROR_IS_OK(err)) {
374                         status = werror_to_ntstatus(err);
375                         goto error;
376                 }
377
378                 if (name_buf.name == NULL) {
379                         status = NT_STATUS_INVALID_PARAMETER;
380                         goto error;
381                 }
382
383                 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
384                         status = NT_STATUS_NO_MEMORY;
385                         goto error;
386                 }
387
388                 values[i] = talloc_zero(values, struct registry_value);
389                 if (values[i] == NULL) {
390                         status = NT_STATUS_NO_MEMORY;
391                         goto error;
392                 }
393
394                 values[i]->type = type;
395                 values[i]->data = data_blob_talloc(values[i], data, data_size);
396         }
397
398         *pnum_values = num_values;
399
400         if (pvalnames) {
401                 *pvalnames = talloc_move(ctx, &names);
402         }
403         if (pvalues) {
404                 *pvalues = talloc_move(ctx, &values);
405         }
406
407         status = NT_STATUS_OK;
408
409  error:
410         TALLOC_FREE(mem_ctx);
411         return status;
412 }
413
414 static NTSTATUS registry_enumvalues2(TALLOC_CTX *ctx,
415                                      struct rpc_pipe_client *pipe_hnd,
416                                      struct policy_handle *key_hnd,
417                                      uint32 *pnum_values, char ***pvalnames,
418                                      struct regval_blob ***pvalues)
419 {
420         TALLOC_CTX *mem_ctx;
421         NTSTATUS status;
422         WERROR werr;
423         uint32 num_subkeys, max_subkeylen, max_classlen;
424         uint32 num_values, max_valnamelen, max_valbufsize;
425         uint32 i;
426         NTTIME last_changed_time;
427         uint32 secdescsize;
428         struct winreg_String classname;
429         struct regval_blob **values;
430         char **names;
431         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
432
433         if (!(mem_ctx = talloc_new(ctx))) {
434                 return NT_STATUS_NO_MEMORY;
435         }
436
437         ZERO_STRUCT(classname);
438         status = dcerpc_winreg_QueryInfoKey(
439                 b, mem_ctx, key_hnd, &classname, &num_subkeys,
440                 &max_subkeylen, &max_classlen, &num_values, &max_valnamelen,
441                 &max_valbufsize, &secdescsize, &last_changed_time, &werr);
442
443         if (!NT_STATUS_IS_OK(status)) {
444                 goto error;
445         }
446         if (!W_ERROR_IS_OK(werr)) {
447                 status = werror_to_ntstatus(werr);
448                 goto error;
449         }
450
451         if (num_values == 0) {
452                 *pnum_values = 0;
453                 TALLOC_FREE(mem_ctx);
454                 return NT_STATUS_OK;
455         }
456
457         if ((!(names = TALLOC_ARRAY(mem_ctx, char *, num_values))) ||
458             (!(values = TALLOC_ARRAY(mem_ctx, struct regval_blob *,
459                                      num_values)))) {
460                 status = NT_STATUS_NO_MEMORY;
461                 goto error;
462         }
463
464         for (i=0; i<num_values; i++) {
465                 enum winreg_Type type = REG_NONE;
466                 uint8 *data = NULL;
467                 uint32 data_size;
468                 uint32 value_length;
469
470                 char n;
471                 struct winreg_ValNameBuf name_buf;
472                 WERROR err;
473
474                 n = '\0';
475                 name_buf.name = &n;
476                 name_buf.size = max_valnamelen + 2;
477
478                 data_size = max_valbufsize;
479                 data = (uint8 *)TALLOC(mem_ctx, data_size);
480                 value_length = 0;
481
482                 status = dcerpc_winreg_EnumValue(b, mem_ctx, key_hnd,
483                                                  i, &name_buf, &type,
484                                                  data, &data_size,
485                                                  &value_length, &err);
486                 if (!(NT_STATUS_IS_OK(status))) {
487                         goto error;
488                 }
489
490                 if ( W_ERROR_EQUAL(err, WERR_NO_MORE_ITEMS) ) {
491                         status = NT_STATUS_OK;
492                         break;
493                 }
494
495                 if (!W_ERROR_IS_OK(err)) {
496                         status = werror_to_ntstatus(err);
497                         goto error;
498                 }
499
500                 if (name_buf.name == NULL) {
501                         status = NT_STATUS_INVALID_PARAMETER;
502                         goto error;
503                 }
504
505                 if (!(names[i] = talloc_strdup(names, name_buf.name))) {
506                         status = NT_STATUS_NO_MEMORY;
507                         goto error;
508                 }
509
510                 assert(value_length<=data_size); //???
511
512                 values[i] = regval_compose(values,
513                                            name_buf.name,
514                                            type,
515                                            data, value_length);
516                 if (!values[i]) {
517                         status = NT_STATUS_NO_MEMORY;
518                         goto error;
519                 }
520         }
521
522         *pnum_values = num_values;
523
524         if (pvalnames) {
525                 *pvalnames = talloc_move(ctx, &names);
526         }
527         if (pvalues) {
528                 *pvalues = talloc_move(ctx, &values);
529         }
530
531         status = NT_STATUS_OK;
532
533  error:
534         TALLOC_FREE(mem_ctx);
535         return status;
536 }
537
538 static NTSTATUS registry_getsd(TALLOC_CTX *mem_ctx,
539                                struct dcerpc_binding_handle *b,
540                                struct policy_handle *key_hnd,
541                                uint32_t sec_info,
542                                struct KeySecurityData *sd,
543                                WERROR *werr)
544 {
545         return dcerpc_winreg_GetKeySecurity(b, mem_ctx, key_hnd,
546                                             sec_info, sd, werr);
547 }
548
549
550 static NTSTATUS registry_setvalue(TALLOC_CTX *mem_ctx,
551                                   struct rpc_pipe_client *pipe_hnd,
552                                   struct policy_handle *key_hnd,
553                                   const char *name,
554                                   const struct registry_value *value)
555 {
556         struct winreg_String name_string;
557         NTSTATUS result;
558         WERROR werr;
559         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
560
561         ZERO_STRUCT(name_string);
562
563         name_string.name = name;
564         result = dcerpc_winreg_SetValue(b, mem_ctx, key_hnd,
565                                         name_string, value->type,
566                                         value->data.data, value->data.length, &werr);
567         if (!NT_STATUS_IS_OK(result)) {
568                 return result;
569         }
570
571         return werror_to_ntstatus(werr);
572 }
573
574 static NTSTATUS rpc_registry_setvalue_internal(struct net_context *c,
575                                                const struct dom_sid *domain_sid,
576                                                const char *domain_name,
577                                                struct cli_state *cli,
578                                                struct rpc_pipe_client *pipe_hnd,
579                                                TALLOC_CTX *mem_ctx,
580                                                int argc,
581                                                const char **argv )
582 {
583         struct policy_handle hive_hnd, key_hnd;
584         NTSTATUS status;
585         WERROR werr;
586         struct registry_value value;
587         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
588
589         status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
590                                   SEC_FLAG_MAXIMUM_ALLOWED,
591                                   &hive_hnd, &key_hnd);
592         if (!NT_STATUS_IS_OK(status)) {
593                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
594                           nt_errstr(status));
595                 return status;
596         }
597
598         if (!strequal(argv[2], "multi_sz") && (argc != 4)) {
599                 d_fprintf(stderr, _("Too many args for type %s\n"), argv[2]);
600                 return NT_STATUS_NOT_IMPLEMENTED;
601         }
602
603         if (strequal(argv[2], "dword")) {
604                 uint32_t v = strtoul(argv[3], NULL, 10);
605                 value.type = REG_DWORD;
606                 value.data = data_blob_talloc(mem_ctx, NULL, 4);
607                 SIVAL(value.data.data, 0, v);
608         }
609         else if (strequal(argv[2], "sz")) {
610                 value.type = REG_SZ;
611                 if (!push_reg_sz(mem_ctx, &value.data, argv[3])) {
612                         status = NT_STATUS_NO_MEMORY;
613                         goto error;
614                 }
615         }
616         else {
617                 d_fprintf(stderr, _("type \"%s\" not implemented\n"), argv[2]);
618                 status = NT_STATUS_NOT_IMPLEMENTED;
619                 goto error;
620         }
621
622         status = registry_setvalue(mem_ctx, pipe_hnd, &key_hnd,
623                                    argv[1], &value);
624
625         if (!NT_STATUS_IS_OK(status)) {
626                 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
627                           nt_errstr(status));
628         }
629
630  error:
631         dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
632         dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
633
634         return NT_STATUS_OK;
635 }
636
637 static int rpc_registry_setvalue(struct net_context *c, int argc,
638                                  const char **argv )
639 {
640         if (argc < 4 || c->display_usage) {
641                 d_fprintf(stderr, "%s\n%s",
642                           _("Usage:"),
643                           _("net rpc registry setvalue <key> <valuename> "
644                             "<type> [<val>]+\n"));
645                 return -1;
646         }
647
648         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
649                 rpc_registry_setvalue_internal, argc, argv );
650 }
651
652 static NTSTATUS rpc_registry_deletevalue_internal(struct net_context *c,
653                                                   const struct dom_sid *domain_sid,
654                                                   const char *domain_name,
655                                                   struct cli_state *cli,
656                                                   struct rpc_pipe_client *pipe_hnd,
657                                                   TALLOC_CTX *mem_ctx,
658                                                   int argc,
659                                                   const char **argv )
660 {
661         struct policy_handle hive_hnd, key_hnd;
662         NTSTATUS status;
663         WERROR werr;
664         struct winreg_String valuename;
665         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
666
667         ZERO_STRUCT(valuename);
668
669         status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
670                                   SEC_FLAG_MAXIMUM_ALLOWED,
671                                   &hive_hnd, &key_hnd);
672         if (!NT_STATUS_IS_OK(status)) {
673                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
674                           nt_errstr(status));
675                 return status;
676         }
677
678         valuename.name = argv[1];
679
680         status = dcerpc_winreg_DeleteValue(b, mem_ctx, &key_hnd,
681                                            valuename, &werr);
682         if (!NT_STATUS_IS_OK(status)) {
683                 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
684                           nt_errstr(status));
685         }
686         if (!W_ERROR_IS_OK(werr)) {
687                 status = werror_to_ntstatus(werr);
688                 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
689                           win_errstr(werr));
690         }
691
692         dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
693         dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
694
695         return status;
696 }
697
698 static int rpc_registry_deletevalue(struct net_context *c, int argc,
699                                     const char **argv )
700 {
701         if (argc != 2 || c->display_usage) {
702                 d_fprintf(stderr, "%s\n%s",
703                           _("Usage:"),
704                           _("net rpc registry deletevalue <key> <valuename>\n"));
705                 return -1;
706         }
707
708         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
709                 rpc_registry_deletevalue_internal, argc, argv );
710 }
711
712 static NTSTATUS rpc_registry_getvalue_internal(struct net_context *c,
713                                                const struct dom_sid *domain_sid,
714                                                const char *domain_name,
715                                                struct cli_state *cli,
716                                                struct rpc_pipe_client *pipe_hnd,
717                                                TALLOC_CTX *mem_ctx,
718                                                bool raw,
719                                                int argc,
720                                                const char **argv)
721 {
722         struct policy_handle hive_hnd, key_hnd;
723         NTSTATUS status;
724         WERROR werr;
725         struct winreg_String valuename;
726         struct registry_value *value = NULL;
727         enum winreg_Type type = REG_NONE;
728         uint32_t data_size = 0;
729         uint32_t value_length = 0;
730         TALLOC_CTX *tmp_ctx = talloc_stackframe();
731         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
732
733         ZERO_STRUCT(valuename);
734
735         status = registry_openkey(tmp_ctx, pipe_hnd, argv[0],
736                                   SEC_FLAG_MAXIMUM_ALLOWED,
737                                   &hive_hnd, &key_hnd);
738         if (!NT_STATUS_IS_OK(status)) {
739                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
740                           nt_errstr(status));
741                 return status;
742         }
743
744         valuename.name = argv[1];
745
746         value = talloc_zero(tmp_ctx, struct registry_value);
747         if (value == NULL) {
748                 return NT_STATUS_NO_MEMORY;
749         }
750
751         /*
752          * call QueryValue once with data == NULL to get the
753          * needed memory size to be allocated, then allocate
754          * data buffer and call again.
755          */
756         status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
757                                           &valuename,
758                                           &type,
759                                           NULL,
760                                           &data_size,
761                                           &value_length,
762                                           &werr);
763
764         if (!NT_STATUS_IS_OK(status)) {
765                 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
766                           nt_errstr(status));
767                 goto done;
768         }
769         if (!W_ERROR_IS_OK(werr)) {
770                 status = werror_to_ntstatus(werr);
771                 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
772                           nt_errstr(status));
773                 goto done;
774         }
775
776         value->data = data_blob_talloc(tmp_ctx, NULL, data_size);
777
778         status = dcerpc_winreg_QueryValue(b, tmp_ctx, &key_hnd,
779                                           &valuename,
780                                           &type,
781                                           value->data.data,
782                                           &data_size,
783                                           &value_length,
784                                           &werr);
785
786         if (!NT_STATUS_IS_OK(status)) {
787                 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
788                           nt_errstr(status));
789                 goto done;
790         }
791         if (!W_ERROR_IS_OK(werr)) {
792                 status = werror_to_ntstatus(werr);
793                 d_fprintf(stderr, _("registry_queryvalue failed: %s\n"),
794                           win_errstr(werr));
795                 goto done;
796         }
797
798
799         value->type = type;
800
801         print_registry_value(value, raw);
802
803 done:
804         dcerpc_winreg_CloseKey(b, tmp_ctx, &key_hnd, &werr);
805         dcerpc_winreg_CloseKey(b, tmp_ctx, &hive_hnd, &werr);
806
807         TALLOC_FREE(tmp_ctx);
808
809         return status;
810 }
811
812 static NTSTATUS rpc_registry_getvalue_full(struct net_context *c,
813                                            const struct dom_sid *domain_sid,
814                                            const char *domain_name,
815                                            struct cli_state *cli,
816                                            struct rpc_pipe_client *pipe_hnd,
817                                            TALLOC_CTX *mem_ctx,
818                                            int argc,
819                                            const char **argv)
820 {
821         return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
822                                               cli, pipe_hnd, mem_ctx, false,
823                                               argc, argv);
824 }
825
826 static int rpc_registry_getvalue(struct net_context *c, int argc,
827                                  const char **argv)
828 {
829         if (argc != 2 || c->display_usage) {
830                 d_fprintf(stderr, "%s\n%s",
831                           _("Usage:"),
832                           _("net rpc registry getvalue <key> <valuename>\n"));
833                 return -1;
834         }
835
836         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
837                 rpc_registry_getvalue_full, argc, argv);
838 }
839
840 static NTSTATUS rpc_registry_getvalue_raw(struct net_context *c,
841                                           const struct dom_sid *domain_sid,
842                                           const char *domain_name,
843                                           struct cli_state *cli,
844                                           struct rpc_pipe_client *pipe_hnd,
845                                           TALLOC_CTX *mem_ctx,
846                                           int argc,
847                                           const char **argv)
848 {
849         return rpc_registry_getvalue_internal(c, domain_sid, domain_name,
850                                               cli, pipe_hnd, mem_ctx, true,
851                                               argc, argv);
852 }
853
854 static int rpc_registry_getvalueraw(struct net_context *c, int argc,
855                                     const char **argv)
856 {
857         if (argc != 2 || c->display_usage) {
858                 d_fprintf(stderr, "%s\n%s",
859                           _("Usage:"),
860                           _("net rpc registry getvalue <key> <valuename>\n"));
861                 return -1;
862         }
863
864         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
865                 rpc_registry_getvalue_raw, argc, argv);
866 }
867
868 static NTSTATUS rpc_registry_createkey_internal(struct net_context *c,
869                                                 const struct dom_sid *domain_sid,
870                                                 const char *domain_name,
871                                                 struct cli_state *cli,
872                                                 struct rpc_pipe_client *pipe_hnd,
873                                                 TALLOC_CTX *mem_ctx,
874                                                 int argc,
875                                                 const char **argv )
876 {
877         uint32 hive;
878         struct policy_handle hive_hnd, key_hnd;
879         struct winreg_String key, keyclass;
880         enum winreg_CreateAction action;
881         NTSTATUS status;
882         WERROR werr;
883         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
884
885         ZERO_STRUCT(key);
886         ZERO_STRUCT(keyclass);
887
888         if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
889                 return NT_STATUS_INVALID_PARAMETER;
890         }
891
892         status = dcerpc_winreg_Connect(b, mem_ctx, hive,
893                                        SEC_FLAG_MAXIMUM_ALLOWED,
894                                        &hive_hnd, &werr);
895         if (!(NT_STATUS_IS_OK(status))) {
896                 return status;
897         }
898         if (!W_ERROR_IS_OK(werr)) {
899                 return werror_to_ntstatus(werr);
900         }
901
902         action = REG_ACTION_NONE;
903         keyclass.name = "";
904
905         status = dcerpc_winreg_CreateKey(b, mem_ctx, &hive_hnd, key,
906                                          keyclass, 0, REG_KEY_READ, NULL,
907                                          &key_hnd, &action, &werr);
908         if (!NT_STATUS_IS_OK(status)) {
909                 d_fprintf(stderr, _("createkey returned %s\n"),
910                           nt_errstr(status));
911                 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
912                 return status;
913         }
914         if (!W_ERROR_IS_OK(werr)) {
915                 WERROR _werr;
916                 d_fprintf(stderr, _("createkey returned %s\n"),
917                           win_errstr(werr));
918                 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
919                 return werror_to_ntstatus(werr);
920         }
921
922         switch (action) {
923                 case REG_ACTION_NONE:
924                         d_printf(_("createkey did nothing -- huh?\n"));
925                         break;
926                 case REG_CREATED_NEW_KEY:
927                         d_printf(_("createkey created %s\n"), argv[0]);
928                         break;
929                 case REG_OPENED_EXISTING_KEY:
930                         d_printf(_("createkey opened existing %s\n"), argv[0]);
931                         break;
932         }
933
934         dcerpc_winreg_CloseKey(b, mem_ctx, &key_hnd, &werr);
935         dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &werr);
936
937         return status;
938 }
939
940 static int rpc_registry_createkey(struct net_context *c, int argc,
941                                   const char **argv )
942 {
943         if (argc != 1 || c->display_usage) {
944                 d_fprintf(stderr, "%s\n%s",
945                           _("Usage:"),
946                           _("net rpc registry createkey <key>\n"));
947                 return -1;
948         }
949
950         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
951                 rpc_registry_createkey_internal, argc, argv );
952 }
953
954 static NTSTATUS rpc_registry_deletekey_internal(struct net_context *c,
955                                                 const struct dom_sid *domain_sid,
956                                                 const char *domain_name,
957                                                 struct cli_state *cli,
958                                                 struct rpc_pipe_client *pipe_hnd,
959                                                 TALLOC_CTX *mem_ctx,
960                                                 int argc,
961                                                 const char **argv )
962 {
963         uint32 hive;
964         struct policy_handle hive_hnd;
965         struct winreg_String key;
966         NTSTATUS status;
967         WERROR werr;
968         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
969
970         ZERO_STRUCT(key);
971
972         if (!reg_hive_key(mem_ctx, argv[0], &hive, &key.name)) {
973                 return NT_STATUS_INVALID_PARAMETER;
974         }
975
976         status = dcerpc_winreg_Connect(b, mem_ctx, hive,
977                                        SEC_FLAG_MAXIMUM_ALLOWED,
978                                        &hive_hnd, &werr);
979         if (!(NT_STATUS_IS_OK(status))) {
980                 return status;
981         }
982         if (!W_ERROR_IS_OK(werr)) {
983                 return werror_to_ntstatus(werr);
984         }
985
986         status = dcerpc_winreg_DeleteKey(b, mem_ctx, &hive_hnd, key, &werr);
987         if (is_valid_policy_hnd(&hive_hnd)) {
988                 WERROR _werr;
989                 dcerpc_winreg_CloseKey(b, mem_ctx, &hive_hnd, &_werr);
990         }
991
992         if (!NT_STATUS_IS_OK(status)) {
993                 d_fprintf(stderr, _("deletekey returned %s\n"),
994                           nt_errstr(status));
995                 return status;
996         }
997
998         if (!W_ERROR_IS_OK(werr)) {
999                 d_fprintf(stderr, _("deletekey returned %s\n"),
1000                           win_errstr(werr));
1001                 return werror_to_ntstatus(werr);
1002         }
1003
1004         return status;
1005 }
1006
1007 static int rpc_registry_deletekey(struct net_context *c, int argc, const char **argv )
1008 {
1009         if (argc != 1 || c->display_usage) {
1010                 d_fprintf(stderr, "%s\n%s",
1011                           _("Usage:"),
1012                           _("net rpc registry deletekey <key>\n"));
1013                 return -1;
1014         }
1015
1016         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1017                 rpc_registry_deletekey_internal, argc, argv );
1018 }
1019
1020 /********************************************************************
1021 ********************************************************************/
1022
1023 static NTSTATUS rpc_registry_enumerate_internal(struct net_context *c,
1024                                                 const struct dom_sid *domain_sid,
1025                                                 const char *domain_name,
1026                                                 struct cli_state *cli,
1027                                                 struct rpc_pipe_client *pipe_hnd,
1028                                                 TALLOC_CTX *mem_ctx,
1029                                                 int argc,
1030                                                 const char **argv )
1031 {
1032         struct policy_handle pol_hive, pol_key;
1033         NTSTATUS status;
1034         WERROR werr;
1035         uint32 num_subkeys = 0;
1036         uint32 num_values = 0;
1037         char **names = NULL, **classes = NULL;
1038         NTTIME **modtimes = NULL;
1039         uint32 i;
1040         struct registry_value **values = NULL;
1041         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1042
1043         if (argc != 1 || c->display_usage) {
1044                 d_printf("%s\n%s",
1045                          _("Usage:"),
1046                          _("net rpc registry enumerate <path>\n"));
1047                 d_printf("%s  net rpc registry enumerate "
1048                          "'HKLM\\Software\\Samba'\n", _("Example:"));
1049                 return NT_STATUS_INVALID_PARAMETER;
1050         }
1051
1052         status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1053                                   &pol_hive, &pol_key);
1054         if (!NT_STATUS_IS_OK(status)) {
1055                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1056                           nt_errstr(status));
1057                 return status;
1058         }
1059
1060         status = registry_enumkeys(mem_ctx, pipe_hnd, &pol_key, &num_subkeys,
1061                                    &names, &classes, &modtimes);
1062         if (!NT_STATUS_IS_OK(status)) {
1063                 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1064                           nt_errstr(status));
1065                 return status;
1066         }
1067
1068         for (i=0; i<num_subkeys; i++) {
1069                 print_registry_key(names[i], modtimes[i]);
1070         }
1071
1072         status = registry_enumvalues(mem_ctx, pipe_hnd, &pol_key, &num_values,
1073                                      &names, &values);
1074         if (!NT_STATUS_IS_OK(status)) {
1075                 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1076                           nt_errstr(status));
1077                 return status;
1078         }
1079
1080         for (i=0; i<num_values; i++) {
1081                 print_registry_value_with_name(names[i], values[i]);
1082         }
1083
1084         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1085         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1086
1087         return status;
1088 }
1089
1090 /********************************************************************
1091 ********************************************************************/
1092
1093 static int rpc_registry_enumerate(struct net_context *c, int argc,
1094                                   const char **argv )
1095 {
1096         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1097                 rpc_registry_enumerate_internal, argc, argv );
1098 }
1099
1100 /********************************************************************
1101 ********************************************************************/
1102
1103 static NTSTATUS rpc_registry_save_internal(struct net_context *c,
1104                                         const struct dom_sid *domain_sid,
1105                                         const char *domain_name,
1106                                         struct cli_state *cli,
1107                                         struct rpc_pipe_client *pipe_hnd,
1108                                         TALLOC_CTX *mem_ctx,
1109                                         int argc,
1110                                         const char **argv )
1111 {
1112         WERROR result = WERR_GENERAL_FAILURE;
1113         struct policy_handle pol_hive, pol_key;
1114         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1115         struct winreg_String filename;
1116         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1117
1118         if (argc != 2 || c->display_usage) {
1119                 d_printf("%s\n%s",
1120                          _("Usage:"),
1121                          _("net rpc registry backup <path> <file> \n"));
1122                 return NT_STATUS_INVALID_PARAMETER;
1123         }
1124
1125         status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_ALL,
1126                                   &pol_hive, &pol_key);
1127         if (!NT_STATUS_IS_OK(status)) {
1128                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1129                           nt_errstr(status));
1130                 return status;
1131         }
1132
1133         filename.name = argv[1];
1134         status = dcerpc_winreg_SaveKey(b, mem_ctx, &pol_key, &filename, NULL, &result);
1135         if (!NT_STATUS_IS_OK(status)) {
1136                 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1137                           cli->desthost, argv[1]);
1138         }
1139         if (!W_ERROR_IS_OK(result)) {
1140                 status = werror_to_ntstatus(result);
1141                 d_fprintf(stderr, _("Unable to save [%s] to %s:%s\n"), argv[0],
1142                           cli->desthost, argv[1]);
1143         }
1144
1145         /* cleanup */
1146
1147         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &result);
1148         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &result);
1149
1150         return status;
1151 }
1152
1153 /********************************************************************
1154 ********************************************************************/
1155
1156 static int rpc_registry_save(struct net_context *c, int argc, const char **argv )
1157 {
1158         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1159                 rpc_registry_save_internal, argc, argv );
1160 }
1161
1162
1163 /********************************************************************
1164 ********************************************************************/
1165
1166 static void dump_values( REGF_NK_REC *nk )
1167 {
1168         int i, j;
1169         const char *data_str = NULL;
1170         uint32 data_size, data;
1171         DATA_BLOB blob;
1172
1173         if ( !nk->values )
1174                 return;
1175
1176         for ( i=0; i<nk->num_values; i++ ) {
1177                 d_printf( "\"%s\" = ", nk->values[i].valuename ? nk->values[i].valuename : "(default)" );
1178                 d_printf( "(%s) ", str_regtype( nk->values[i].type ) );
1179
1180                 data_size = nk->values[i].data_size & ~VK_DATA_IN_OFFSET;
1181                 switch ( nk->values[i].type ) {
1182                         case REG_SZ:
1183                                 blob = data_blob_const(nk->values[i].data, data_size);
1184                                 pull_reg_sz(talloc_tos(), &blob, &data_str);
1185                                 if (!data_str) {
1186                                         break;
1187                                 }
1188                                 d_printf( "%s", data_str );
1189                                 break;
1190                         case REG_MULTI_SZ:
1191                         case REG_EXPAND_SZ:
1192                                 for ( j=0; j<data_size; j++ ) {
1193                                         d_printf( "%c", nk->values[i].data[j] );
1194                                 }
1195                                 break;
1196                         case REG_DWORD:
1197                                 data = IVAL( nk->values[i].data, 0 );
1198                                 d_printf("0x%x", data );
1199                                 break;
1200                         case REG_BINARY:
1201                                 for ( j=0; j<data_size; j++ ) {
1202                                         d_printf( "%x", nk->values[i].data[j] );
1203                                 }
1204                                 break;
1205                         default:
1206                                 d_printf(_("unknown"));
1207                                 break;
1208                 }
1209
1210                 d_printf( "\n" );
1211         }
1212
1213 }
1214
1215 /********************************************************************
1216 ********************************************************************/
1217
1218 static bool dump_registry_tree( REGF_FILE *file, REGF_NK_REC *nk, const char *parent )
1219 {
1220         REGF_NK_REC *key;
1221
1222         /* depth first dump of the registry tree */
1223
1224         while ( (key = regfio_fetch_subkey( file, nk )) ) {
1225                 char *regpath;
1226                 if (asprintf(&regpath, "%s\\%s", parent, key->keyname) < 0) {
1227                         break;
1228                 }
1229                 d_printf("[%s]\n", regpath );
1230                 dump_values( key );
1231                 d_printf("\n");
1232                 dump_registry_tree( file, key, regpath );
1233                 SAFE_FREE(regpath);
1234         }
1235
1236         return true;
1237 }
1238
1239 /********************************************************************
1240 ********************************************************************/
1241
1242 static bool write_registry_tree( REGF_FILE *infile, REGF_NK_REC *nk,
1243                                  REGF_NK_REC *parent, REGF_FILE *outfile,
1244                                  const char *parentpath )
1245 {
1246         REGF_NK_REC *key, *subkey;
1247         struct regval_ctr *values = NULL;
1248         struct regsubkey_ctr *subkeys = NULL;
1249         int i;
1250         char *path = NULL;
1251         WERROR werr;
1252
1253         werr = regsubkey_ctr_init(infile->mem_ctx, &subkeys);
1254         if (!W_ERROR_IS_OK(werr)) {
1255                 DEBUG(0, ("write_registry_tree: regsubkey_ctr_init failed: "
1256                           "%s\n", win_errstr(werr)));
1257                 return false;
1258         }
1259
1260         werr = regval_ctr_init(subkeys, &values);
1261         if (!W_ERROR_IS_OK(werr)) {
1262                 DEBUG(0,("write_registry_tree: talloc() failed!\n"));
1263                 TALLOC_FREE(subkeys);
1264                 return false;
1265         }
1266
1267         /* copy values into the struct regval_ctr */
1268
1269         for ( i=0; i<nk->num_values; i++ ) {
1270                 regval_ctr_addvalue( values, nk->values[i].valuename, nk->values[i].type,
1271                         nk->values[i].data, (nk->values[i].data_size & ~VK_DATA_IN_OFFSET) );
1272         }
1273
1274         /* copy subkeys into the struct regsubkey_ctr */
1275
1276         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1277                 regsubkey_ctr_addkey( subkeys, subkey->keyname );
1278         }
1279
1280         key = regfio_write_key( outfile, nk->keyname, values, subkeys, nk->sec_desc->sec_desc, parent );
1281
1282         /* write each one of the subkeys out */
1283
1284         path = talloc_asprintf(subkeys,
1285                         "%s%s%s",
1286                         parentpath,
1287                         parent ? "\\" : "",
1288                         nk->keyname);
1289         if (!path) {
1290                 TALLOC_FREE(subkeys);
1291                 return false;
1292         }
1293
1294         nk->subkey_index = 0;
1295         while ( (subkey = regfio_fetch_subkey( infile, nk )) ) {
1296                 write_registry_tree( infile, subkey, key, outfile, path );
1297         }
1298
1299         d_printf("[%s]\n", path );
1300         TALLOC_FREE(subkeys);
1301
1302         return true;
1303 }
1304
1305 /********************************************************************
1306 ********************************************************************/
1307
1308 static int rpc_registry_dump(struct net_context *c, int argc, const char **argv)
1309 {
1310         REGF_FILE   *registry;
1311         REGF_NK_REC *nk;
1312
1313         if (argc != 1 || c->display_usage) {
1314                 d_printf("%s\n%s",
1315                          _("Usage:"),
1316                          _("net rpc registry dump <file> \n"));
1317                 return -1;
1318         }
1319
1320         d_printf(_("Opening %s...."), argv[0]);
1321         if ( !(registry = regfio_open( argv[0], O_RDONLY, 0)) ) {
1322                 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1323                 return 1;
1324         }
1325         d_printf(_("ok\n"));
1326
1327         /* get the root of the registry file */
1328
1329         if ((nk = regfio_rootkey( registry )) == NULL) {
1330                 d_fprintf(stderr, _("Could not get rootkey\n"));
1331                 regfio_close( registry );
1332                 return 1;
1333         }
1334         d_printf("[%s]\n", nk->keyname);
1335         dump_values( nk );
1336         d_printf("\n");
1337
1338         dump_registry_tree( registry, nk, nk->keyname );
1339
1340 #if 0
1341         talloc_report_full( registry->mem_ctx, stderr );
1342 #endif
1343         d_printf(_("Closing registry..."));
1344         regfio_close( registry );
1345         d_printf(_("ok\n"));
1346
1347         return 0;
1348 }
1349
1350 /********************************************************************
1351 ********************************************************************/
1352
1353 static int rpc_registry_copy(struct net_context *c, int argc, const char **argv )
1354 {
1355         REGF_FILE   *infile = NULL, *outfile = NULL;
1356         REGF_NK_REC *nk;
1357         int result = 1;
1358
1359         if (argc != 2 || c->display_usage) {
1360                 d_printf("%s\n%s",
1361                          _("Usage:"),
1362                          _("net rpc registry copy <srcfile> <newfile>\n"));
1363                 return -1;
1364         }
1365
1366         d_printf(_("Opening %s...."), argv[0]);
1367         if ( !(infile = regfio_open( argv[0], O_RDONLY, 0 )) ) {
1368                 d_fprintf(stderr, _("Failed to open %s for reading\n"),argv[0]);
1369                 return 1;
1370         }
1371         d_printf(_("ok\n"));
1372
1373         d_printf(_("Opening %s...."), argv[1]);
1374         if ( !(outfile = regfio_open( argv[1], (O_RDWR|O_CREAT|O_TRUNC),
1375                                       (S_IRUSR|S_IWUSR) )) ) {
1376                 d_fprintf(stderr, _("Failed to open %s for writing\n"),argv[1]);
1377                 goto out;
1378         }
1379         d_printf(_("ok\n"));
1380
1381         /* get the root of the registry file */
1382
1383         if ((nk = regfio_rootkey( infile )) == NULL) {
1384                 d_fprintf(stderr, _("Could not get rootkey\n"));
1385                 goto out;
1386         }
1387         d_printf(_("RootKey: [%s]\n"), nk->keyname);
1388
1389         write_registry_tree( infile, nk, NULL, outfile, "" );
1390
1391         result = 0;
1392
1393 out:
1394
1395         d_printf(_("Closing %s..."), argv[1]);
1396         if (outfile) {
1397                 regfio_close( outfile );
1398         }
1399         d_printf(_("ok\n"));
1400
1401         d_printf(_("Closing %s..."), argv[0]);
1402         if (infile) {
1403                 regfio_close( infile );
1404         }
1405         d_printf(_("ok\n"));
1406
1407         return( result);
1408 }
1409
1410 /********************************************************************
1411 ********************************************************************/
1412
1413 static NTSTATUS rpc_registry_getsd_internal(struct net_context *c,
1414                                             const struct dom_sid *domain_sid,
1415                                             const char *domain_name,
1416                                             struct cli_state *cli,
1417                                             struct rpc_pipe_client *pipe_hnd,
1418                                             TALLOC_CTX *mem_ctx,
1419                                             int argc,
1420                                             const char **argv)
1421 {
1422         struct policy_handle pol_hive, pol_key;
1423         NTSTATUS status;
1424         WERROR werr;
1425         enum ndr_err_code ndr_err;
1426         struct KeySecurityData *sd = NULL;
1427         uint32_t sec_info;
1428         DATA_BLOB blob;
1429         struct security_descriptor sec_desc;
1430         uint32_t access_mask = SEC_FLAG_MAXIMUM_ALLOWED |
1431                                SEC_FLAG_SYSTEM_SECURITY;
1432         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1433
1434         if (argc <1 || argc > 2 || c->display_usage) {
1435                 d_printf("%s\n%s",
1436                          _("Usage:"),
1437                          _("net rpc registry getsd <path> <secinfo>\n"));
1438                 d_printf("%s  net rpc registry getsd "
1439                            "'HKLM\\Software\\Samba'\n", _("Example:"));
1440                 return NT_STATUS_INVALID_PARAMETER;
1441         }
1442
1443         status = registry_openkey(mem_ctx, pipe_hnd, argv[0],
1444                                   access_mask,
1445                                   &pol_hive, &pol_key);
1446         if (!NT_STATUS_IS_OK(status)) {
1447                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1448                           nt_errstr(status));
1449                 return status;
1450         }
1451
1452         sd = TALLOC_ZERO_P(mem_ctx, struct KeySecurityData);
1453         if (!sd) {
1454                 status = NT_STATUS_NO_MEMORY;
1455                 goto out;
1456         }
1457
1458         sd->size = 0x1000;
1459
1460         if (argc >= 2) {
1461                 sscanf(argv[1], "%x", &sec_info);
1462         } else {
1463                 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
1464         }
1465
1466         status = registry_getsd(mem_ctx, b, &pol_key, sec_info, sd, &werr);
1467         if (!NT_STATUS_IS_OK(status)) {
1468                 d_fprintf(stderr, _("getting sd failed: %s\n"),
1469                           nt_errstr(status));
1470                 goto out;
1471         }
1472         if (!W_ERROR_IS_OK(werr)) {
1473                 status = werror_to_ntstatus(werr);
1474                 d_fprintf(stderr, _("getting sd failed: %s\n"),
1475                           win_errstr(werr));
1476                 goto out;
1477         }
1478
1479         blob.data = sd->data;
1480         blob.length = sd->size;
1481
1482         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &sec_desc,
1483                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
1484         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1485                 status = ndr_map_error2ntstatus(ndr_err);
1486                 goto out;
1487         }
1488         status = NT_STATUS_OK;
1489
1490         display_sec_desc(&sec_desc);
1491
1492  out:
1493         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1494         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1495
1496         return status;
1497 }
1498
1499
1500 static int rpc_registry_getsd(struct net_context *c, int argc, const char **argv)
1501 {
1502         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1503                 rpc_registry_getsd_internal, argc, argv);
1504 }
1505
1506 /********************************************************************
1507  ********************************************************************/
1508 /**
1509  * @defgroup net_rpc_registry net rpc registry
1510  */
1511
1512 /**
1513  * @defgroup net_rpc_registry_export Export
1514  * @ingroup net_rpc_registry
1515  * @{
1516  */
1517
1518 static NTSTATUS registry_export(struct rpc_pipe_client* pipe_hnd,
1519                                 TALLOC_CTX* ctx,
1520                                 struct policy_handle* key_hnd,
1521                                 struct reg_format* f,
1522                                 const char* parentfullname,
1523                                 const char* name)
1524 {
1525         NTSTATUS status;
1526         uint32 num_subkeys = 0;
1527         uint32 num_values = 0;
1528         char **names = NULL, **classes = NULL;
1529         NTTIME **modtimes = NULL;
1530         struct regval_blob **values = NULL;
1531         uint32 i;
1532         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1533
1534         TALLOC_CTX* mem_ctx = talloc_new(ctx);
1535
1536
1537         const char* fullname = name
1538                 ? talloc_asprintf(mem_ctx, "%s\\%s", parentfullname, name)
1539                 : parentfullname;
1540         reg_format_key(f, &fullname, 1, false);
1541
1542         status = registry_enumvalues2(mem_ctx, pipe_hnd, key_hnd, &num_values,
1543                                       &names, &values);
1544         if (!NT_STATUS_IS_OK(status)) {
1545                 d_fprintf(stderr, _("enumerating values failed: %s\n"),
1546                           nt_errstr(status));
1547                 goto done;
1548         }
1549
1550         for (i=0; i<num_values; i++) {
1551                 reg_format_regval_blob(f, names[i], values[i]);
1552         }
1553
1554
1555         status = registry_enumkeys(mem_ctx, pipe_hnd, key_hnd, &num_subkeys,
1556                                    &names, &classes, &modtimes);
1557         if (!NT_STATUS_IS_OK(status)) {
1558                 d_fprintf(stderr, _("enumerating keys failed: %s\n"),
1559                           nt_errstr(status));
1560                 goto done;
1561         }
1562
1563         for (i=0; i<num_subkeys; i++) {
1564                 struct policy_handle subkey_hnd;
1565                 struct winreg_String key;
1566                 WERROR werr;
1567                 ZERO_STRUCT(key);
1568                 /* key.name = talloc_strdup(mem_ctx, names[i]); ??? */
1569                 key.name = names[i];
1570
1571                 status = dcerpc_winreg_OpenKey(b, mem_ctx, key_hnd, key,
1572                                                0, REG_KEY_READ,
1573                                                &subkey_hnd, &werr);
1574                 if (!NT_STATUS_IS_OK(status)) {
1575                         d_fprintf(stderr,
1576                                   _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1577                                   names[i], nt_errstr(status));
1578                         continue;
1579                 }
1580                 if (!W_ERROR_IS_OK(werr)) {
1581                         status = werror_to_ntstatus(werr);
1582                         d_fprintf(stderr,
1583                                   _("dcerpc_winreg_OpenKey failed: %s %s\n"),
1584                                   names[i], win_errstr(werr));
1585                         continue;
1586                 }
1587
1588                 status = registry_export(pipe_hnd, mem_ctx, &subkey_hnd,
1589                                          f, fullname, names[i]);
1590                 if (!(NT_STATUS_IS_OK(status))) {
1591                         d_fprintf(stderr,
1592                                   _("export key failed: %s %s\n"),
1593                                   names[i], nt_errstr(status));
1594                 }
1595                 dcerpc_winreg_CloseKey(b, mem_ctx,
1596                                        &subkey_hnd, &werr);
1597         }
1598 done:
1599         talloc_free(mem_ctx);
1600         return status;
1601 }
1602
1603 static NTSTATUS rpc_registry_export_internal(struct net_context *c,
1604                                              const struct dom_sid *domain_sid,
1605                                              const char *domain_name,
1606                                              struct cli_state *cli,
1607                                              struct rpc_pipe_client *pipe_hnd,
1608                                              TALLOC_CTX *mem_ctx,
1609                                              int argc,
1610                                              const char **argv )
1611 {
1612         struct policy_handle pol_hive, pol_key;
1613         NTSTATUS status;
1614         WERROR werr;
1615         struct reg_format* f;
1616         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1617
1618         if (argc < 2 || argc > 3 || c->display_usage) {
1619                 d_printf("%s\n%s",
1620                          _("Usage:"),
1621                          _("net rpc registry export <path> <file> [opt]\n"));
1622                 d_printf("%s  net rpc registry export "
1623                          "'HKLM\\Software\\Samba' samba.reg\n", _("Example:"));
1624                 return NT_STATUS_INVALID_PARAMETER;
1625         }
1626
1627         status = registry_openkey(mem_ctx, pipe_hnd, argv[0], REG_KEY_READ,
1628                                   &pol_hive, &pol_key);
1629         if (!NT_STATUS_IS_OK(status)) {
1630                 d_fprintf(stderr, _("registry_openkey failed: %s\n"),
1631                           nt_errstr(status));
1632                 return status;
1633         }
1634
1635         f = reg_format_file(mem_ctx, argv[1], (argc > 2) ? argv[2] : NULL);
1636         if (f == NULL) {
1637                 d_fprintf(stderr, _("open file failed: %s\n"), strerror(errno));
1638                 return map_nt_error_from_unix(errno);
1639         }
1640
1641         status = registry_export(pipe_hnd, mem_ctx, &pol_key,
1642                                  f, argv[0], NULL );
1643         if (!NT_STATUS_IS_OK(status))
1644                 return status;
1645
1646         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_key, &werr);
1647         dcerpc_winreg_CloseKey(b, mem_ctx, &pol_hive, &werr);
1648
1649         return status;
1650 }
1651 /********************************************************************
1652  ********************************************************************/
1653
1654 static int rpc_registry_export(struct net_context *c, int argc,
1655                                const char **argv )
1656 {
1657         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1658                                rpc_registry_export_internal, argc, argv );
1659 }
1660
1661 /**@}*/
1662
1663 /********************************************************************
1664  ********************************************************************/
1665
1666 /**
1667  * @defgroup net_rpc_registry_import Import
1668  * @ingroup net_rpc_registry
1669  * @{
1670  */
1671
1672 struct import_ctx {
1673         struct rpc_pipe_client *pipe_hnd;
1674         TALLOC_CTX *mem_ctx;
1675 };
1676
1677 static WERROR import_create_key(struct import_ctx* ctx,
1678                                 struct policy_handle* parent, const char* name,
1679                                 void** pkey, bool* existing)
1680 {
1681         WERROR werr;
1682         NTSTATUS status;
1683         void* mem_ctx = talloc_new(ctx->mem_ctx);
1684
1685         struct policy_handle* key = NULL;
1686         struct policy_handle  hive;
1687         struct winreg_String  keyclass, keyname;
1688         enum winreg_CreateAction action = REG_ACTION_NONE;
1689         struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1690
1691         ZERO_STRUCT(keyname);
1692         keyname.name = name;
1693
1694         if (parent == NULL) {
1695                 uint32 hive_idx = 0;
1696                 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1697                         werr = WERR_FOOBAR;
1698                         goto done;
1699                 }
1700
1701                 status = dcerpc_winreg_Connect(b, mem_ctx,
1702                                                hive_idx, SEC_FLAG_MAXIMUM_ALLOWED,
1703                                                &hive, &werr);
1704                 if (!NT_STATUS_IS_OK(status)) {
1705                         werr = ntstatus_to_werror(status);
1706                         d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1707                                   nt_errstr(status));
1708                         goto done;
1709                 }
1710                 if (!W_ERROR_IS_OK(werr)) {
1711                         d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1712                                   win_errstr(werr));
1713                         goto done;
1714                 }
1715
1716                 parent = &hive;
1717         }
1718
1719         key = talloc_zero(mem_ctx, struct policy_handle);
1720         if (key == NULL) {
1721                 werr = WERR_NOMEM;
1722                 goto done;
1723         }
1724
1725         ZERO_STRUCT(keyclass);
1726         keyclass.name = "";
1727
1728         status = dcerpc_winreg_CreateKey(b, mem_ctx,
1729                                          parent, keyname,
1730                                          keyclass, 0, REG_KEY_READ, NULL,
1731                                          key, &action, &werr);
1732         if (!NT_STATUS_IS_OK(status)) {
1733                 werr = ntstatus_to_werror(status);
1734                 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1735                           nt_errstr(status));
1736                 goto done;
1737         }
1738         if (!W_ERROR_IS_OK(werr)) {
1739                 d_fprintf(stderr, _("dcerpc_winreg_CreateKey returned %s\n"),
1740                           win_errstr(werr));
1741                 goto done;
1742         }
1743
1744         switch (action) {
1745         case REG_CREATED_NEW_KEY:
1746                 d_printf(_("createkey created %s\n"), name);
1747                 if (existing != NULL)
1748                         *existing = false;
1749                 break;
1750
1751         case REG_OPENED_EXISTING_KEY:
1752                 d_printf(_("createkey opened existing %s\n"), name);
1753                 if (existing != NULL)
1754                         *existing = true;
1755                 break;
1756
1757         case REG_ACTION_NONE:
1758                 d_printf(_("createkey did nothing -- huh?\n"));
1759                 werr = WERR_CREATE_FAILED;
1760                 break;
1761         default:
1762                 assert(false);
1763         }
1764
1765 done:
1766         if ( parent == &hive ) {
1767                 WERROR _result;
1768                 dcerpc_winreg_CloseKey(b, mem_ctx,
1769                                        parent, &_result);
1770         }
1771
1772         if (pkey!=NULL) {
1773                 *pkey = talloc_steal(ctx->mem_ctx, key);
1774         }
1775
1776         talloc_free(mem_ctx);
1777         return werr;
1778 }
1779
1780 static WERROR import_delete_key(struct import_ctx* ctx,
1781                                 struct policy_handle* parent, const char* name)
1782 {
1783         WERROR werr;
1784         NTSTATUS status;
1785         void* mem_ctx = talloc_new(ctx->mem_ctx);
1786         struct winreg_String  keyname = { 0, };
1787         struct policy_handle  hive;
1788         struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1789
1790         keyname.name = name;
1791
1792         if (parent == NULL) {
1793                 uint32 hive_idx;
1794                 if (!reg_hive_key(mem_ctx, name, &hive_idx, &keyname.name)) {
1795                         werr = WERR_FOOBAR;
1796                         goto done;
1797                 }
1798
1799                 status = dcerpc_winreg_Connect(b, mem_ctx, hive_idx,
1800                                                SEC_FLAG_MAXIMUM_ALLOWED, &hive,
1801                                                &werr);
1802                 if (!NT_STATUS_IS_OK(status)) {
1803                         werr = ntstatus_to_werror(status);
1804                         d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1805                                   nt_errstr(status));
1806                         goto done;
1807                 }
1808                 if (!W_ERROR_IS_OK(werr)) {
1809                         d_fprintf(stderr, _("dcerpc_winreg_Connect returned %s\n"),
1810                                   win_errstr(werr));
1811                         goto done;
1812                 }
1813
1814                 parent = &hive;
1815         }
1816
1817         status = dcerpc_winreg_DeleteKey(b, mem_ctx, parent,
1818                                          keyname, &werr);
1819         if (!NT_STATUS_IS_OK(status)) {
1820                 werr = ntstatus_to_werror(status);
1821                 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1822                           nt_errstr(status));
1823                 goto done;
1824         }
1825         if (!W_ERROR_IS_OK(werr)) {
1826                 d_fprintf(stderr, _("dcerpc_winreg_DeleteKey returned %s\n"),
1827                           win_errstr(werr));
1828                 goto done;
1829         }
1830
1831 done:
1832         if ( parent == &hive ) {
1833                 WERROR _result;
1834                 dcerpc_winreg_CloseKey(b, mem_ctx, parent, &_result);
1835         }
1836
1837         talloc_free(mem_ctx);
1838         return werr;
1839 }
1840
1841 static WERROR import_close_key(struct import_ctx* ctx,
1842                                struct policy_handle* key)
1843 {
1844         WERROR werr;
1845         NTSTATUS status;
1846         void* mem_ctx = talloc_new(ctx->mem_ctx);
1847         struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1848
1849         status = dcerpc_winreg_CloseKey(b, mem_ctx, key, &werr);
1850         if (!NT_STATUS_IS_OK(status)) {
1851                 werr = ntstatus_to_werror(status);
1852                 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1853                           nt_errstr(status));
1854                 goto done;
1855         }
1856         if (!W_ERROR_IS_OK(werr)) {
1857                 d_fprintf(stderr, _("dcerpc_winreg_CloseKey returned %s\n"),
1858                           win_errstr(werr));
1859                 goto done;
1860         }
1861
1862         werr = (talloc_free(key) == 0) ? WERR_OK : WERR_GENERAL_FAILURE;
1863 done:
1864         talloc_free(mem_ctx);
1865         return werr;
1866 }
1867
1868 static WERROR import_create_val(struct import_ctx* ctx,
1869                                 struct policy_handle* parent, const char* name,
1870                                 uint32_t type, const uint8_t* val, uint32_t len)
1871 {
1872         WERROR werr;
1873         NTSTATUS status;
1874         void* mem_ctx = talloc_new(ctx->mem_ctx);
1875         struct winreg_String valuename;
1876         struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1877
1878         if (parent == NULL) {
1879                 return WERR_INVALID_PARAM;
1880         }
1881
1882         ZERO_STRUCT(valuename);
1883         valuename.name = name;
1884
1885         status = dcerpc_winreg_SetValue(b, mem_ctx, parent,
1886                                         valuename, type,
1887                                         (uint8_t *)discard_const(val), len, &werr);
1888         if (!NT_STATUS_IS_OK(status)) {
1889                 werr = ntstatus_to_werror(status);
1890                 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1891                           nt_errstr(status));
1892                 goto done;
1893         }
1894         if (!W_ERROR_IS_OK(werr)) {
1895                 d_fprintf(stderr, _("registry_setvalue failed: %s\n"),
1896                           win_errstr(werr));
1897                 goto done;
1898         }
1899
1900 done:
1901         talloc_free(mem_ctx);
1902         return werr;
1903 }
1904
1905 static WERROR import_delete_val(struct import_ctx* ctx,
1906                                 struct policy_handle* parent, const char* name)
1907 {
1908         WERROR werr;
1909         NTSTATUS status;
1910         void* mem_ctx = talloc_new(ctx->mem_ctx);
1911         struct winreg_String valuename;
1912         struct dcerpc_binding_handle *b = ctx->pipe_hnd->binding_handle;
1913
1914         if (parent == NULL) {
1915                 return WERR_INVALID_PARAM;
1916         }
1917
1918         ZERO_STRUCT(valuename);
1919         valuename.name = name;
1920
1921         status = dcerpc_winreg_DeleteValue(b, mem_ctx,
1922                                            parent, valuename, &werr);
1923
1924         if (!NT_STATUS_IS_OK(status)) {
1925                 werr = ntstatus_to_werror(status);
1926                 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1927                           nt_errstr(status));
1928                 goto done;
1929         }
1930         if (!NT_STATUS_IS_OK(status)) {
1931                 d_fprintf(stderr, _("registry_deletevalue failed: %s\n"),
1932                           win_errstr(werr));
1933                 goto done;
1934         }
1935
1936 done:
1937         talloc_free(mem_ctx);
1938         return werr;
1939 }
1940
1941
1942
1943 static NTSTATUS rpc_registry_import_internal(struct net_context *c,
1944                                              const struct dom_sid *domain_sid,
1945                                              const char *domain_name,
1946                                              struct cli_state *cli,
1947                                              struct rpc_pipe_client *pipe_hnd,
1948                                              TALLOC_CTX *mem_ctx,
1949                                              int argc,
1950                                              const char **argv )
1951 {
1952         struct import_ctx import_ctx;
1953
1954         struct reg_import_callback import_callback = {
1955                 .openkey     = NULL,
1956                 .closekey    = (reg_import_callback_closekey_t)&import_close_key,
1957                 .createkey   = (reg_import_callback_createkey_t)&import_create_key,
1958                 .deletekey   = (reg_import_callback_deletekey_t)&import_delete_key,
1959                 .deleteval   = (reg_import_callback_deleteval_t)&import_delete_val,
1960                 .setval.blob = (reg_import_callback_setval_blob_t)&import_create_val,
1961                 .setval_type = BLOB,
1962                 .data = &import_ctx
1963         };
1964
1965         int ret;
1966         if (argc < 1 || argc > 2 || c->display_usage) {
1967                 d_printf("%s\n%s",
1968                          _("Usage:"),
1969                          _("net rpc registry import <file> [options]\n"));
1970                 d_printf("%s  net rpc registry export "
1971                          "samba.reg enc=CP1252,flags=0\n", _("Example:"));
1972                 return NT_STATUS_INVALID_PARAMETER;
1973         }
1974         ZERO_STRUCT(import_ctx);
1975         import_ctx.pipe_hnd = pipe_hnd;
1976         import_ctx.mem_ctx  = mem_ctx;
1977         ret = reg_parse_file(argv[0],
1978                              reg_import_adapter(import_ctx.mem_ctx,
1979                                                 import_callback
1980                                      ),
1981                              (argc > 1) ? argv[1] : NULL
1982                 );
1983
1984         return ret==0 ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1985 }
1986
1987 /********************************************************************
1988  ********************************************************************/
1989
1990 static int rpc_registry_import(struct net_context *c, int argc,
1991                                const char **argv )
1992 {
1993         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1994                                rpc_registry_import_internal, argc, argv );
1995 }
1996
1997 /**@}*/
1998 /********************************************************************
1999  ********************************************************************/
2000
2001 int net_rpc_registry(struct net_context *c, int argc, const char **argv)
2002 {
2003         struct functable func[] = {
2004                 {
2005                         "enumerate",
2006                         rpc_registry_enumerate,
2007                         NET_TRANSPORT_RPC,
2008                         N_("Enumerate registry keys and values"),
2009                         N_("net rpc registry enumerate\n"
2010                            "    Enumerate registry keys and values")
2011                 },
2012                 {
2013                         "createkey",
2014                         rpc_registry_createkey,
2015                         NET_TRANSPORT_RPC,
2016                         N_("Create a new registry key"),
2017                         N_("net rpc registry createkey\n"
2018                            "    Create a new registry key")
2019                 },
2020                 {
2021                         "deletekey",
2022                         rpc_registry_deletekey,
2023                         NET_TRANSPORT_RPC,
2024                         N_("Delete a registry key"),
2025                         N_("net rpc registry deletekey\n"
2026                            "    Delete a registry key")
2027                 },
2028                 {
2029                         "getvalue",
2030                         rpc_registry_getvalue,
2031                         NET_TRANSPORT_RPC,
2032                         N_("Print a registry value"),
2033                         N_("net rpc registry getvalue\n"
2034                            "    Print a registry value")
2035                 },
2036                 {
2037                         "getvalueraw",
2038                         rpc_registry_getvalueraw,
2039                         NET_TRANSPORT_RPC,
2040                         N_("Print a registry value"),
2041                         N_("net rpc registry getvalueraw\n"
2042                            "    Print a registry value (raw version)")
2043                 },
2044                 {
2045                         "setvalue",
2046                         rpc_registry_setvalue,
2047                         NET_TRANSPORT_RPC,
2048                         N_("Set a new registry value"),
2049                         N_("net rpc registry setvalue\n"
2050                            "    Set a new registry value")
2051                 },
2052                 {
2053                         "deletevalue",
2054                         rpc_registry_deletevalue,
2055                         NET_TRANSPORT_RPC,
2056                         N_("Delete a registry value"),
2057                         N_("net rpc registry deletevalue\n"
2058                            "    Delete a registry value")
2059                 },
2060                 {
2061                         "save",
2062                         rpc_registry_save,
2063                         NET_TRANSPORT_RPC,
2064                         N_("Save a registry file"),
2065                         N_("net rpc registry save\n"
2066                            "    Save a registry file")
2067                 },
2068                 {
2069                         "dump",
2070                         rpc_registry_dump,
2071                         NET_TRANSPORT_RPC,
2072                         N_("Dump a registry file"),
2073                         N_("net rpc registry dump\n"
2074                            "    Dump a registry file")
2075                 },
2076                 {
2077                         "copy",
2078                         rpc_registry_copy,
2079                         NET_TRANSPORT_RPC,
2080                         N_("Copy a registry file"),
2081                         N_("net rpc registry copy\n"
2082                            "    Copy a registry file")
2083                 },
2084                 {
2085                         "getsd",
2086                         rpc_registry_getsd,
2087                         NET_TRANSPORT_RPC,
2088                         N_("Get security descriptor"),
2089                         N_("net rpc registry getsd\n"
2090                            "    Get security descriptior")
2091                 },
2092                 {
2093                         "import",
2094                         rpc_registry_import,
2095                         NET_TRANSPORT_RPC,
2096                         N_("Import .reg file"),
2097                         N_("net rpc registry import\n"
2098                            "    Import .reg file")
2099                 },
2100                 {
2101                         "export",
2102                         rpc_registry_export,
2103                         NET_TRANSPORT_RPC,
2104                         N_("Export .reg file"),
2105                         N_("net rpc registry export\n"
2106                            "    Export .reg file")
2107                 },
2108                 {NULL, NULL, 0, NULL, NULL}
2109         };
2110         return net_run_function(c, argc, argv, "net rpc registry", func);
2111 }