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