s3-net: Added net rpc conf addshare command to net rpc conf
[samba.git] / source3 / utils / net_rpc_conf.c
1 /*
2  *  Samba Unix/Linux SMB client library
3  *  Distributed SMB/CIFS Server Management Utility
4  *  Local configuration interface
5  *  Copyright (C) Vicentiu Ciorbaru 2011
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
21 /*
22  * This is an interface to Samba's configuration.
23  *
24  * This tool supports local as well as remote interaction via rpc
25  * with the configuration stored in the registry.
26  */
27
28
29 #include "includes.h"
30 #include "utils/net.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "../librpc/gen_ndr/ndr_samr_c.h"
33 #include "rpc_client/init_samr.h"
34 #include "../librpc/gen_ndr/ndr_winreg_c.h"
35 #include "../libcli/registry/util_reg.h"
36 #include "rpc_client/cli_winreg.h"
37 #include "../lib/smbconf/smbconf.h"
38
39 /* internal functions */
40 /**********************************************************
41  *
42  * usage functions
43  *
44  **********************************************************/
45 const char confpath[100] = "Software\\Samba\\smbconf";
46
47 static int rpc_conf_list_usage(struct net_context *c, int argc,
48                                const char **argv)
49 {
50         d_printf("%s net rpc conf list\n", _("Usage:"));
51         return -1;
52 }
53
54 static int rpc_conf_listshares_usage(struct net_context *c, int argc,
55                                      const char **argv)
56 {
57         d_printf("%s net rpc conf listshares\n", _("Usage:"));
58         return -1;
59 }
60
61 static int rpc_conf_delshare_usage(struct net_context *c, int argc,
62                                    const char **argv)
63 {
64         d_printf("%s\n%s",
65                  _("Usage:"),
66                  _("net rpc conf delshare <sharename>\n"));
67         return -1;
68 }
69
70 static int rpc_conf_showshare_usage(struct net_context *c, int argc,
71                                     const char **argv)
72 {
73         d_printf("%s\n%s",
74                  _("Usage:"),
75                  _("net rpc conf showshare <sharename>\n"));
76         return -1;
77 }
78
79 static int rpc_conf_drop_usage(struct net_context *c, int argc,
80                                const char **argv)
81 {
82         d_printf("%s\nnet rpc conf drop\n", _("Usage:"));
83         return -1;
84 }
85
86 static int rpc_conf_getparm_usage(struct net_context *c, int argc,
87                                const char **argv)
88 {
89         d_printf("%s\nnet rpc conf getparm <sharename> <parameter>\n",
90                         _("Usage:"));
91         return -1;
92 }
93
94 static int rpc_conf_setparm_usage(struct net_context *c, int argc,
95                                   const char **argv)
96 {
97         d_printf("%s\n%s",
98                  _("Usage:"),
99                  _(" net rpc conf setparm <section> <param> <value>\n"));
100         return -1;
101 }
102
103 static int rpc_conf_delparm_usage(struct net_context *c, int argc,
104                                 const char **argv)
105 {
106         d_printf("%s\nnet rpc conf delparm <sharename> <parameter>\n",
107                         _("Usage:"));
108         return -1;
109 }
110
111 static int rpc_conf_getincludes_usage(struct net_context *c, int argc,
112                                 const char **argv)
113 {
114         d_printf("%s\nnet rpc conf getincludes <sharename>\n",
115                         _("Usage:"));
116         return -1;
117 }
118
119 static int rpc_conf_setincludes_usage(struct net_context *c, int argc,
120                                 const char **argv)
121 {
122         d_printf("%s\nnet rpc conf setincludes <sharename> [<filename>]*\n",
123                         _("Usage:"));
124         return -1;
125 }
126
127 static int rpc_conf_delincludes_usage(struct net_context *c, int argc,
128                                 const char **argv)
129 {
130         d_printf("%s\nnet rpc conf delincludes <sharename>\n",
131                         _("Usage:"));
132         return -1;
133 }
134
135 static bool rpc_conf_reg_valname_forbidden(const char * valname)
136 {
137         const char *forbidden_valnames[] = {
138                 "lock directory",
139                 "lock dir",
140                 "config backend",
141                 "include",
142                 "includes", /* this has a special meaning internally */
143                 NULL
144         };
145         const char **forbidden = NULL;
146
147         for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
148                 if (strwicmp(valname, *forbidden) == 0) {
149                         return true;
150                 }
151         }
152         return false;
153
154 }
155 static NTSTATUS rpc_conf_del_value(TALLOC_CTX *mem_ctx,
156                                    struct dcerpc_binding_handle *b,
157                                    struct policy_handle *parent_hnd,
158                                    const char *share_name,
159                                    const char *value,
160                                    WERROR *werr)
161 {
162
163         TALLOC_CTX *frame = talloc_stackframe();
164         NTSTATUS status = NT_STATUS_OK;
165         WERROR result = WERR_OK;
166         WERROR _werr;
167
168         struct winreg_String keyname, valuename;
169         struct policy_handle child_hnd;
170
171         ZERO_STRUCT(child_hnd);
172         ZERO_STRUCT(keyname);
173         ZERO_STRUCT(valuename);
174
175         keyname.name = share_name;
176         valuename.name = value;
177
178         status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, keyname, 0,
179                                        REG_KEY_WRITE, &child_hnd, &result);
180
181         if (!(NT_STATUS_IS_OK(status))) {
182                 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
183                                 keyname.name, nt_errstr(status));
184                 goto error;
185         }
186
187         if (!(W_ERROR_IS_OK(result))) {
188                 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
189                                 keyname.name, win_errstr(result));
190                 goto error;
191         }
192
193         status = dcerpc_winreg_DeleteValue(b,
194                                            frame,
195                                            &child_hnd,
196                                            valuename,
197                                            &result);
198
199         if (!(NT_STATUS_IS_OK(status))) {
200                 d_fprintf(stderr, _("Failed to delete value %s\n"),
201                                 nt_errstr(status));
202                 goto error;
203         }
204
205         if (!(W_ERROR_IS_OK(result))) {
206                 if (W_ERROR_EQUAL(result, WERR_BADFILE)){
207                         result = WERR_OK;
208                         goto error;
209                 }
210
211                 d_fprintf(stderr, _("Failed to delete value  %s\n"),
212                                 win_errstr(result));
213                 goto error;
214         }
215
216 error:
217         *werr = result;
218
219         dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
220
221         TALLOC_FREE(frame);
222         return status;;
223
224 }
225
226 static NTSTATUS rpc_conf_get_share(TALLOC_CTX *mem_ctx,
227                                    struct dcerpc_binding_handle *b,
228                                    struct policy_handle *parent_hnd,
229                                    const char *share_name,
230                                    struct smbconf_service *share,
231                                    WERROR *werr)
232 {
233         TALLOC_CTX *frame = talloc_stackframe();
234
235         NTSTATUS status = NT_STATUS_OK;
236         WERROR result = WERR_OK;
237         WERROR _werr;
238         struct policy_handle child_hnd;
239         int32_t includes_cnt, includes_idx = -1;
240         uint32_t num_vals, i, param_cnt = 0;
241         const char **val_names;
242         enum winreg_Type *types;
243         DATA_BLOB *data;
244         struct winreg_String key;
245         const char **multi_s = NULL;
246         const char *s = NULL;
247         struct smbconf_service tmp_share;
248
249         ZERO_STRUCT(tmp_share);
250
251         key.name = share_name;
252         status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, key, 0,
253                                REG_KEY_READ, &child_hnd, &result);
254
255         if (!(NT_STATUS_IS_OK(status))) {
256                 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
257                                 nt_errstr(status));
258                 goto error;
259         }
260         if (!(W_ERROR_IS_OK(result))) {
261                 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
262                                 win_errstr(result));
263                 goto error;
264         }
265         /* get all the info from the share key */
266         status = dcerpc_winreg_enumvals(frame,
267                         b,
268                         &child_hnd,
269                         &num_vals,
270                         &val_names,
271                         &types,
272                         &data,
273                         &result);
274
275         if (!(NT_STATUS_IS_OK(status))) {
276                 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
277                                 nt_errstr(status));
278                 goto error;
279         }
280         if (!(W_ERROR_IS_OK(result))) {
281                 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
282                                 win_errstr(result));
283                 goto error;
284         }
285         /* check for includes */
286         for (i = 0; i < num_vals; i++) {
287                 if (strcmp(val_names[i], "includes") == 0){
288                         if (!pull_reg_multi_sz(frame,
289                                                &data[i],
290                                                &multi_s))
291                         {
292                                 result = WERR_NOMEM;
293                                 d_fprintf(stderr,
294                                           _("Failed to enumerate values: %s\n"),
295                                           win_errstr(result));
296                                 goto error;
297                         }
298                         includes_idx = i;
299                 }
300         }
301         /* count the number of includes */
302         includes_cnt = 0;
303         if (includes_idx != -1) {
304                 for (includes_cnt = 0;
305                      multi_s[includes_cnt] != NULL;
306                      includes_cnt ++);
307         }
308         /* place the name of the share in the smbconf_service struct */
309         tmp_share.name = talloc_strdup(frame, share_name);
310         if (tmp_share.name == NULL) {
311                 result = WERR_NOMEM;
312                 d_fprintf(stderr, _("Failed to create share: %s\n"),
313                                 win_errstr(result));
314                 goto error;
315         }
316         /* place the number of parameters in the smbconf_service struct */
317         tmp_share.num_params = num_vals;
318         if (includes_idx != -1) {
319                 tmp_share.num_params = num_vals + includes_cnt - 1;
320         }
321         /* allocate memory for the param_names and param_values lists */
322         tmp_share.param_names = talloc_zero_array(frame, char *, tmp_share.num_params);
323         if (tmp_share.param_names == NULL) {
324                 result = WERR_NOMEM;
325                 d_fprintf(stderr, _("Failed to create share: %s\n"),
326                                 win_errstr(result));
327                 goto error;
328         }
329         tmp_share.param_values = talloc_zero_array(frame, char *, tmp_share.num_params);
330         if (tmp_share.param_values == NULL) {
331                 result = WERR_NOMEM;
332                 d_fprintf(stderr, _("Failed to create share: %s\n"),
333                                 win_errstr(result));
334                 goto error;
335         }
336         /* place all params except includes */
337         for (i = 0; i < num_vals; i++) {
338                 if (strcmp(val_names[i], "includes") != 0) {
339                         if (!pull_reg_sz(frame, &data[i], &s)) {
340                                 result = WERR_NOMEM;
341                                 d_fprintf(stderr,
342                                           _("Failed to enumerate values: %s\n"),
343                                           win_errstr(result));
344                                 goto error;
345                         }
346                         /* place param_names */
347                         tmp_share.param_names[param_cnt] = talloc_strdup(frame, val_names[i]);
348                         if (tmp_share.param_names[param_cnt] == NULL) {
349                                 result = WERR_NOMEM;
350                                 d_fprintf(stderr, _("Failed to create share: %s\n"),
351                                                 win_errstr(result));
352                                 goto error;
353                         }
354
355                         /* place param_values */
356                         tmp_share.param_values[param_cnt++] = talloc_strdup(frame, s);
357                         if (tmp_share.param_values[param_cnt - 1] == NULL) {
358                                 result = WERR_NOMEM;
359                                 d_fprintf(stderr, _("Failed to create share: %s\n"),
360                                                 win_errstr(result));
361                                 goto error;
362                         }
363                 }
364         }
365         /* place the includes last */
366         for (i = 0; i < includes_cnt; i++) {
367                 tmp_share.param_names[param_cnt] = talloc_strdup(frame, "include");
368                 if (tmp_share.param_names[param_cnt] == NULL) {
369                                 result = WERR_NOMEM;
370                                 d_fprintf(stderr, _("Failed to create share: %s\n"),
371                                                 win_errstr(result));
372                                 goto error;
373                 }
374
375                 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, multi_s[i]);
376                 if (tmp_share.param_values[param_cnt - 1] == NULL) {
377                                 result = WERR_NOMEM;
378                                 d_fprintf(stderr, _("Failed to create share: %s\n"),
379                                                 win_errstr(result));
380                                 goto error;
381                 }
382         }
383
384         /* move everything to the main memory ctx */
385         for (i = 0; i < param_cnt; i++) {
386                 tmp_share.param_names[i] = talloc_move(mem_ctx, &tmp_share.param_names[i]);
387                 tmp_share.param_values[i] = talloc_move(mem_ctx, &tmp_share.param_values[i]);
388         }
389
390         tmp_share.name = talloc_move(mem_ctx, &tmp_share.name);
391         tmp_share.param_names = talloc_move(mem_ctx, &tmp_share.param_names);
392         tmp_share.param_values = talloc_move(mem_ctx, &tmp_share.param_values);
393         /* out parameter */
394         *share = tmp_share;
395 error:
396         /* close child */
397         dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
398         *werr = result;
399         TALLOC_FREE(frame);
400         return status;
401 }
402
403 static int rpc_conf_print_shares(uint32_t num_shares,
404                                  struct smbconf_service *shares)
405 {
406
407         uint32_t share_count, param_count;
408         const char *indent = "\t";
409
410         if (num_shares == 0) {
411                 return 0;
412         }
413
414         for (share_count = 0; share_count < num_shares; share_count++) {
415                 d_printf("\n");
416                 if (shares[share_count].name != NULL) {
417                 d_printf("[%s]\n", shares[share_count].name);
418                 }
419
420                 for (param_count = 0;
421                      param_count < shares[share_count].num_params;
422                      param_count++)
423                 {
424                         d_printf("%s%s = %s\n",
425                                  indent,
426                                  shares[share_count].param_names[param_count],
427                                  shares[share_count].param_values[param_count]);
428                 }
429         }
430         d_printf("\n");
431
432         return 0;
433
434 }
435 static NTSTATUS rpc_conf_open_conf(TALLOC_CTX *mem_ctx,
436                                    struct dcerpc_binding_handle *b,
437                                    uint32_t access_mask,
438                                    struct policy_handle *hive_hnd,
439                                    struct policy_handle *key_hnd,
440                                    WERROR *werr)
441 {
442         TALLOC_CTX *frame = talloc_stackframe();
443         NTSTATUS status = NT_STATUS_OK;
444         WERROR result = WERR_OK;
445         WERROR _werr;
446         struct policy_handle tmp_hive_hnd, tmp_key_hnd;
447         struct winreg_String key;
448
449         ZERO_STRUCT(key);
450
451         status = dcerpc_winreg_OpenHKLM(b, frame, NULL,
452                         access_mask, &tmp_hive_hnd, &result);
453
454         /*
455          * print no error messages if it is a read only open
456          * and key does not exist
457          * error still gets returned
458          */
459
460         if (access_mask == REG_KEY_READ &&
461             W_ERROR_EQUAL(result, WERR_BADFILE))
462         {
463                 goto error;
464         }
465
466         if (!(NT_STATUS_IS_OK(status))) {
467                 d_fprintf(stderr, _("Failed to open hive: %s\n"),
468                                 nt_errstr(status));
469                 goto error;
470         }
471         if (!W_ERROR_IS_OK(result)) {
472                 d_fprintf(stderr, _("Failed to open hive: %s\n"),
473                                 win_errstr(result));
474                 goto error;
475         }
476
477         key.name = confpath;
478         status = dcerpc_winreg_OpenKey(b, frame, &tmp_hive_hnd, key, 0,
479                                        access_mask, &tmp_key_hnd, &result);
480
481         /*
482          * print no error messages if it is a read only open
483          * and key does not exist
484          * error still gets returned
485          */
486
487         if (access_mask == REG_KEY_READ &&
488             W_ERROR_EQUAL(result, WERR_BADFILE))
489         {
490                 goto error;
491         }
492
493         if (!(NT_STATUS_IS_OK(status))) {
494                 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
495                                 nt_errstr(status));
496                 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
497                 goto error;
498         }
499         if (!(W_ERROR_IS_OK(result))) {
500                 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
501                         win_errstr(result));
502                 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
503                 goto error;
504         }
505
506         *hive_hnd = tmp_hive_hnd;
507         *key_hnd = tmp_key_hnd;
508
509 error:
510         TALLOC_FREE(frame);
511         *werr = result;
512
513         return status;
514 }
515
516 static NTSTATUS rpc_conf_listshares_internal(struct net_context *c,
517                                              const struct dom_sid *domain_sid,
518                                              const char *domain_name,
519                                              struct cli_state *cli,
520                                              struct rpc_pipe_client *pipe_hnd,
521                                              TALLOC_CTX *mem_ctx,
522                                              int argc,
523                                              const char **argv )
524 {
525
526         TALLOC_CTX *frame = talloc_stackframe();
527         NTSTATUS status = NT_STATUS_OK;
528         WERROR werr = WERR_OK;
529         WERROR _werr;
530
531         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
532
533         /* key info */
534         struct policy_handle hive_hnd, key_hnd;
535         uint32_t num_subkeys;
536         uint32_t i;
537         const char **subkeys = NULL;
538
539
540         ZERO_STRUCT(hive_hnd);
541         ZERO_STRUCT(key_hnd);
542
543
544         if (argc != 0 || c->display_usage) {
545                 rpc_conf_listshares_usage(c, argc, argv);
546                 status = NT_STATUS_INVALID_PARAMETER;
547                 goto error;
548         }
549
550
551         status = rpc_conf_open_conf(frame,
552                                     b,
553                                     REG_KEY_READ,
554                                     &hive_hnd,
555                                     &key_hnd,
556                                     &werr);
557
558         if (!(NT_STATUS_IS_OK(status))) {
559                 goto error;
560         }
561
562         if (!(W_ERROR_IS_OK(werr))) {
563                 goto error;
564         }
565
566         status = dcerpc_winreg_enum_keys(frame,
567                                          b,
568                                          &key_hnd,
569                                          &num_subkeys,
570                                          &subkeys,
571                                          &werr);
572
573         if (!(NT_STATUS_IS_OK(status))) {
574                 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
575                                 nt_errstr(status));
576                 goto error;
577         }
578
579         if (!(W_ERROR_IS_OK(werr))) {
580                 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
581                                 win_errstr(werr));
582                 goto error;
583         }
584
585         for (i = 0; i < num_subkeys; i++) {
586                 d_printf("%s\n", subkeys[i]);
587         }
588
589 error:
590         if (!(W_ERROR_IS_OK(werr))) {
591                 status =  werror_to_ntstatus(werr);
592         }
593
594         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
595         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
596
597         TALLOC_FREE(frame);
598         return status;;
599 }
600
601 static NTSTATUS rpc_conf_delshare_internal(struct net_context *c,
602                                            const struct dom_sid *domain_sid,
603                                            const char *domain_name,
604                                            struct cli_state *cli,
605                                            struct rpc_pipe_client *pipe_hnd,
606                                            TALLOC_CTX *mem_ctx,
607                                            int argc,
608                                            const char **argv )
609 {
610
611         TALLOC_CTX *frame = talloc_stackframe();
612         NTSTATUS status = NT_STATUS_OK;
613         WERROR werr = WERR_OK;
614         WERROR _werr;
615
616         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
617
618         /* key info */
619         struct policy_handle hive_hnd, key_hnd;
620
621         ZERO_STRUCT(hive_hnd);
622         ZERO_STRUCT(key_hnd);
623
624
625         if (argc != 1 || c->display_usage) {
626                 rpc_conf_delshare_usage(c, argc, argv);
627                 status = NT_STATUS_INVALID_PARAMETER;
628                 goto error;
629         }
630
631         status = rpc_conf_open_conf(frame,
632                                     b,
633                                     REG_KEY_ALL,
634                                     &hive_hnd,
635                                     &key_hnd,
636                                     &werr);
637
638         if (!(NT_STATUS_IS_OK(status))) {
639                 goto error;
640         }
641
642         if (!(W_ERROR_IS_OK(werr))) {
643                 goto error;
644         }
645
646         status = dcerpc_winreg_delete_subkeys_recursive(frame,
647                                                         b,
648                                                         &key_hnd,
649                                                         REG_KEY_ALL,
650                                                         argv[0],
651                                                         &werr);
652
653         if (!NT_STATUS_IS_OK(status)) {
654                 d_fprintf(stderr,
655                           "winreg_delete_subkeys: Could not delete key %s: %s\n",
656                           argv[0], nt_errstr(status));
657                 goto error;
658         }
659
660         if (W_ERROR_EQUAL(werr, WERR_BADFILE)){
661                 d_fprintf(stderr, _("ERROR: Key does not exist\n"));
662         }
663
664
665         if (!W_ERROR_IS_OK(werr)) {
666                 d_fprintf(stderr,
667                           "winreg_delete_subkeys: Could not delete key %s: %s\n",
668                           argv[0], win_errstr(werr));
669                 goto error;
670         }
671
672 error:
673         if (!(W_ERROR_IS_OK(werr))) {
674                 status =  werror_to_ntstatus(werr);
675         }
676
677         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
678         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
679
680         TALLOC_FREE(frame);
681
682         return status;
683 }
684
685 static NTSTATUS rpc_conf_list_internal(struct net_context *c,
686                                        const struct dom_sid *domain_sid,
687                                        const char *domain_name,
688                                        struct cli_state *cli,
689                                        struct rpc_pipe_client *pipe_hnd,
690                                        TALLOC_CTX *mem_ctx,
691                                        int argc,
692                                        const char **argv )
693 {
694
695         TALLOC_CTX *frame = talloc_stackframe();
696         NTSTATUS status = NT_STATUS_OK;
697         WERROR werr = WERR_OK;
698         WERROR _werr;
699
700         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
701
702         /* key info */
703         struct policy_handle hive_hnd, key_hnd;
704         uint32_t num_subkeys;
705         uint32_t i;
706         struct smbconf_service *shares;
707         const char **subkeys = NULL;
708
709
710         ZERO_STRUCT(hive_hnd);
711         ZERO_STRUCT(key_hnd);
712
713
714         if (argc != 0 || c->display_usage) {
715                 rpc_conf_list_usage(c, argc, argv);
716                 status = NT_STATUS_INVALID_PARAMETER;
717                 goto error;
718         }
719
720         status = rpc_conf_open_conf(frame,
721                                     b,
722                                     REG_KEY_READ,
723                                     &hive_hnd,
724                                     &key_hnd,
725                                     &werr);
726
727         if (!(NT_STATUS_IS_OK(status))) {
728                 goto error;
729         }
730
731         if (!(W_ERROR_IS_OK(werr))) {
732                 goto error;
733         }
734
735         status = dcerpc_winreg_enum_keys(frame,
736                                          b,
737                                          &key_hnd,
738                                          &num_subkeys,
739                                          &subkeys,
740                                          &werr);
741
742         if (!(NT_STATUS_IS_OK(status))) {
743                 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
744                                 nt_errstr(status));
745                 goto error;
746         }
747
748         if (!(W_ERROR_IS_OK(werr))) {
749                 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
750                                 win_errstr(werr));
751                 goto error;
752         }
753
754         if (num_subkeys == 0) {
755                 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
756                 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
757                 TALLOC_FREE(frame);
758                 return NT_STATUS_OK;
759         }
760
761         /* get info from each subkey */
762         shares = talloc_zero_array(frame, struct smbconf_service, num_subkeys);
763         if (shares == NULL) {
764                 werr = WERR_NOMEM;
765                 d_fprintf(stderr, _("Failed to create shares: %s\n"),
766                                 win_errstr(werr));
767                 goto error;
768
769         }
770
771         for (i = 0; i < num_subkeys; i++) {
772                 /* get each share and place it in the shares array */
773                 status = rpc_conf_get_share(frame,
774                                 b,
775                                 &key_hnd,
776                                 subkeys[i],
777                                 &shares[i],
778                                 &werr);
779                 if (!(NT_STATUS_IS_OK(status))) {
780                         goto error;
781                 }
782                 if (!(W_ERROR_IS_OK(werr))) {
783                         goto error;
784                 }
785
786         }
787         /* print the shares array */
788         rpc_conf_print_shares(num_subkeys, shares);
789
790 error:
791         if (!(W_ERROR_IS_OK(werr))) {
792                 status =  werror_to_ntstatus(werr);
793         }
794
795         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
796         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
797
798         TALLOC_FREE(frame);
799         return status;
800
801 }
802
803 static NTSTATUS rpc_conf_drop_internal(struct net_context *c,
804                                        const struct dom_sid *domain_sid,
805                                        const char *domain_name,
806                                        struct cli_state *cli,
807                                        struct rpc_pipe_client *pipe_hnd,
808                                        TALLOC_CTX *mem_ctx,
809                                        int argc,
810                                        const char **argv )
811 {
812         TALLOC_CTX *frame = talloc_stackframe();
813         NTSTATUS status = NT_STATUS_OK;
814         WERROR werr = WERR_OK;
815         WERROR _werr;
816
817         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
818
819         /* key info */
820         struct policy_handle hive_hnd, key_hnd;
821         const char *keyname = confpath;
822         struct winreg_String wkey, wkeyclass;
823         enum winreg_CreateAction action = REG_ACTION_NONE;
824
825
826         ZERO_STRUCT(hive_hnd);
827         ZERO_STRUCT(key_hnd);
828
829
830         if (argc != 0 || c->display_usage) {
831                 rpc_conf_drop_usage(c, argc, argv);
832                 status = NT_STATUS_INVALID_PARAMETER;
833                 goto error;
834         }
835
836         status = rpc_conf_open_conf(frame,
837                                     b,
838                                     REG_KEY_ALL,
839                                     &hive_hnd,
840                                     &key_hnd,
841                                     &werr);
842
843         if (!(NT_STATUS_IS_OK(status))) {
844                 goto error;
845         }
846
847         if (!(W_ERROR_IS_OK(werr))) {
848                 goto error;
849         }
850
851         status = dcerpc_winreg_delete_subkeys_recursive(frame,
852                                                         b,
853                                                         &hive_hnd,
854                                                         REG_KEY_ALL,
855                                                         keyname,
856                                                         &werr);
857
858         if (!NT_STATUS_IS_OK(status)) {
859                 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
860                           keyname, nt_errstr(status));
861                 goto error;
862         }
863
864         if (!W_ERROR_IS_OK(werr)) {
865                 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
866                           keyname, win_errstr(werr));
867                 goto error;
868         }
869
870         wkey.name = keyname;
871         ZERO_STRUCT(wkeyclass);
872         wkeyclass.name = "";
873         action = REG_ACTION_NONE;
874
875         status = dcerpc_winreg_CreateKey(b,
876                                          frame,
877                                          &hive_hnd,
878                                          wkey,
879                                          wkeyclass,
880                                          0,
881                                          REG_KEY_ALL,
882                                          NULL,
883                                          &key_hnd,
884                                          &action,
885                                          &werr);
886
887         if (!NT_STATUS_IS_OK(status)) {
888                 d_printf("winreg_CreateKey: Could not create smbconf key\n");
889                 goto error;
890         }
891
892         if (!W_ERROR_IS_OK(werr)) {
893                 d_printf("winreg_CreateKey: Could not create smbconf key\n");
894                 goto error;
895         }
896
897
898 error:
899         if (!(W_ERROR_IS_OK(werr))) {
900                 status =  werror_to_ntstatus(werr);
901         }
902
903         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
904         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
905
906         TALLOC_FREE(frame);
907         return status;
908 }
909
910 static NTSTATUS rpc_conf_showshare_internal(struct net_context *c,
911                                             const struct dom_sid *domain_sid,
912                                             const char *domain_name,
913                                             struct cli_state *cli,
914                                             struct rpc_pipe_client *pipe_hnd,
915                                             TALLOC_CTX *mem_ctx,
916                                             int argc,
917                                             const char **argv )
918 {
919         TALLOC_CTX *frame = talloc_stackframe();
920         NTSTATUS status = NT_STATUS_OK;
921         WERROR werr = WERR_OK;
922         WERROR _werr;
923
924         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
925
926         /* key info */
927         struct policy_handle hive_hnd, key_hnd;
928         struct smbconf_service *service = NULL;
929         const char *sharename = NULL;
930
931
932         ZERO_STRUCT(hive_hnd);
933         ZERO_STRUCT(key_hnd);
934
935
936         if (argc != 1 || c->display_usage) {
937                 rpc_conf_showshare_usage(c, argc, argv);
938                 status = NT_STATUS_INVALID_PARAMETER;
939                 goto error;
940         }
941
942         status = rpc_conf_open_conf(frame,
943                                     b,
944                                     REG_KEY_READ,
945                                     &hive_hnd,
946                                     &key_hnd,
947                                     &werr);
948
949         if (!(NT_STATUS_IS_OK(status))) {
950                 goto error;
951         }
952
953         if (!(W_ERROR_IS_OK(werr))) {
954                 goto error;
955         }
956
957         sharename = talloc_strdup(frame, argv[0]);
958         if (sharename == NULL) {
959                 werr = WERR_NOMEM;
960                 d_fprintf(stderr, _("Failed to create share: %s\n"),
961                                 win_errstr(werr));
962                 goto error;
963         }
964
965         service = talloc(frame, struct smbconf_service);
966         if (service == NULL) {
967                 werr = WERR_NOMEM;
968                 d_fprintf(stderr, _("Failed to create share: %s\n"),
969                                 win_errstr(werr));
970                 goto error;
971         }
972
973         status = rpc_conf_get_share(frame,
974                         b,
975                         &key_hnd,
976                         sharename,
977                         service,
978                         &werr);
979
980         if (!(NT_STATUS_IS_OK(status))) {
981                 goto error;
982         }
983         if (!(W_ERROR_IS_OK(werr))) {
984                 goto error;
985         }
986
987         rpc_conf_print_shares(1, service);
988
989 error:
990         if (!(W_ERROR_IS_OK(werr))) {
991                 status =  werror_to_ntstatus(werr);
992         }
993
994         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
995         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
996
997         TALLOC_FREE(frame);
998         return status;
999 }
1000
1001 static NTSTATUS rpc_conf_getparm_internal(struct net_context *c,
1002                                           const struct dom_sid *domain_sid,
1003                                           const char *domain_name,
1004                                           struct cli_state *cli,
1005                                           struct rpc_pipe_client *pipe_hnd,
1006                                           TALLOC_CTX *mem_ctx,
1007                                           int argc,
1008                                           const char **argv )
1009 {
1010         TALLOC_CTX *frame = talloc_stackframe();
1011         NTSTATUS status = NT_STATUS_OK;
1012         WERROR werr = WERR_OK;
1013         WERROR _werr;
1014
1015         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1016
1017         /* key info */
1018         struct policy_handle hive_hnd, key_hnd;
1019         struct smbconf_service *service = NULL;
1020
1021         bool param_is_set = false;
1022         uint32_t param_count;
1023
1024         ZERO_STRUCT(hive_hnd);
1025         ZERO_STRUCT(key_hnd);
1026
1027
1028         if (argc != 2 || c->display_usage) {
1029                 rpc_conf_getparm_usage(c, argc, argv);
1030                 status = NT_STATUS_INVALID_PARAMETER;
1031                 goto error;
1032         }
1033
1034         status = rpc_conf_open_conf(frame,
1035                                     b,
1036                                     REG_KEY_READ,
1037                                     &hive_hnd,
1038                                     &key_hnd,
1039                                     &werr);
1040
1041         if (!(NT_STATUS_IS_OK(status))) {
1042                 goto error;
1043         }
1044
1045         if (!(W_ERROR_IS_OK(werr))) {
1046                 goto error;
1047         }
1048
1049
1050         service = talloc(frame, struct smbconf_service);
1051
1052         status = rpc_conf_get_share(frame,
1053                                     b,
1054                                     &key_hnd,
1055                                     argv[0],
1056                                     service,
1057                                     &werr);
1058
1059         if (!(NT_STATUS_IS_OK(status))) {
1060                         goto error;
1061         }
1062
1063         if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
1064                 d_fprintf(stderr, _("ERROR: Share %s does not exist\n"),
1065                                 argv[0]);
1066                 goto error;
1067         }
1068
1069         if (!(W_ERROR_IS_OK(werr))) {
1070                         goto error;
1071         }
1072
1073         for (param_count = 0;
1074              param_count < service->num_params;
1075              param_count++)
1076         {
1077                 /* should includes also be printed? */
1078                 if (strcmp(service->param_names[param_count], argv[1]) == 0) {
1079                         d_printf(_("%s\n"),
1080                                 service->param_values[param_count]);
1081                         param_is_set = true;
1082                 }
1083         }
1084
1085         if (!param_is_set) {
1086                 d_fprintf(stderr, _("ERROR: Given parameter '%s' has not been set\n"),
1087                                 argv[1]);
1088                 werr = WERR_BADFILE;
1089                 goto error;
1090         }
1091
1092 error:
1093
1094         if (!(W_ERROR_IS_OK(werr))) {
1095                 status =  werror_to_ntstatus(werr);
1096         }
1097
1098         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1099         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1100
1101         TALLOC_FREE(frame);
1102         return status;
1103
1104 }
1105
1106 static NTSTATUS rpc_conf_setparm_internal(struct net_context *c,
1107                                           const struct dom_sid *domain_sid,
1108                                           const char *domain_name,
1109                                           struct cli_state *cli,
1110                                           struct rpc_pipe_client *pipe_hnd,
1111                                           TALLOC_CTX *mem_ctx,
1112                                           int argc,
1113                                           const char **argv )
1114 {
1115         TALLOC_CTX *frame = talloc_stackframe();
1116         NTSTATUS status = NT_STATUS_OK;
1117         WERROR werr = WERR_OK;
1118         WERROR _werr;
1119
1120         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1121
1122         /* key info */
1123         struct policy_handle hive_hnd, key_hnd, share_hnd;
1124
1125         struct winreg_String key, keyclass;
1126         enum winreg_CreateAction action = 0;
1127
1128         ZERO_STRUCT(hive_hnd);
1129         ZERO_STRUCT(key_hnd);
1130         ZERO_STRUCT(share_hnd);
1131
1132         ZERO_STRUCT(key);
1133         ZERO_STRUCT(keyclass);
1134
1135         if (argc != 3 || c->display_usage) {
1136                 rpc_conf_setparm_usage(c, argc, argv);
1137                 status = NT_STATUS_INVALID_PARAMETER;
1138                 goto error;
1139         }
1140
1141         status = rpc_conf_open_conf(frame,
1142                                     b,
1143                                     REG_KEY_READ,
1144                                     &hive_hnd,
1145                                     &key_hnd,
1146                                     &werr);
1147
1148         if (!(NT_STATUS_IS_OK(status))) {
1149                 goto error;
1150         }
1151
1152         if (!(W_ERROR_IS_OK(werr))) {
1153                 goto error;
1154         }
1155
1156         key.name = argv[0];
1157         keyclass.name = "";
1158
1159         status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1160                         0, REG_KEY_READ, NULL, &share_hnd,
1161                         &action, &werr);
1162
1163         if (!(NT_STATUS_IS_OK(status))) {
1164                 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1165                                 argv[0], nt_errstr(status));
1166                 goto error;
1167         }
1168
1169         if (!W_ERROR_IS_OK(werr)) {
1170                 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1171                                 argv[0], win_errstr(werr));
1172                 goto error;
1173         }
1174
1175         switch (action) {
1176                 case REG_ACTION_NONE:
1177                         werr = WERR_CREATE_FAILED;
1178                         d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1179                                 argv[0], win_errstr(werr));
1180                         goto error;
1181                 case REG_CREATED_NEW_KEY:
1182                         DEBUG(5, ("net rpc conf setparm:"
1183                                         "createkey created %s\n", argv[0]));
1184                         break;
1185                 case REG_OPENED_EXISTING_KEY:
1186                         DEBUG(5, ("net rpc conf setparm:"
1187                                         "createkey opened existing %s\n", argv[0]));
1188
1189                         /* delete posibly existing value */
1190                         status = rpc_conf_del_value(frame,
1191                                                     b,
1192                                                     &key_hnd,
1193                                                     argv[0],
1194                                                     argv[1],
1195                                                     &werr);
1196
1197                         if (!(NT_STATUS_IS_OK(status))) {
1198                                 goto error;
1199                         }
1200
1201                         if (!(W_ERROR_IS_OK(werr))) {
1202                                 goto error;
1203                         }
1204
1205                         break;
1206         }
1207
1208
1209         const char *canon_valname;
1210         const char *canon_valstr;
1211         /* check if parameter is valid for writing */
1212         if (!lp_canonicalize_parameter_with_value(argv[1], argv[2],
1213                                                   &canon_valname,
1214                                                   &canon_valstr))
1215         {
1216                 if (canon_valname == NULL) {
1217                         d_fprintf(stderr, "invalid parameter '%s' given\n",
1218                                   argv[1]);
1219                 } else {
1220                         d_fprintf(stderr, "invalid value '%s' given for "
1221                                   "parameter '%s'\n", argv[1], argv[2]);
1222                 }
1223                 werr = WERR_INVALID_PARAM;
1224                 goto error;
1225         }
1226
1227         if (rpc_conf_reg_valname_forbidden(canon_valname)) {
1228                 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
1229                           canon_valname);
1230                 werr = WERR_INVALID_PARAM;
1231                 goto error;
1232         }
1233
1234         if (!strequal(argv[0], "global") &&
1235             lp_parameter_is_global(argv[1]))
1236         {
1237                 d_fprintf(stderr, "Global parameter '%s' not allowed in "
1238                           "service definition ('%s').\n", canon_valname,
1239                           argv[0]);
1240                 werr = WERR_INVALID_PARAM;
1241                 goto error;
1242         }
1243
1244         /* set the parameter */
1245         status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1246                                         argv[1], argv[2], &werr);
1247
1248         if (!(NT_STATUS_IS_OK(status))) {
1249                 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1250                                 " with value %s\n %s\n",
1251                                 argv[1], argv[2], nt_errstr(status));
1252                 goto error;
1253         }
1254
1255         if (!(W_ERROR_IS_OK(werr))) {
1256                 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1257                                 " with value %s\n %s\n",
1258                                 argv[1], argv[2], win_errstr(werr));
1259                 goto error;
1260         }
1261
1262 error:
1263
1264         if (!(W_ERROR_IS_OK(werr))) {
1265                 status =  werror_to_ntstatus(werr);
1266         }
1267
1268         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1269         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1270         dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1271
1272         TALLOC_FREE(frame);
1273         return status;
1274 }
1275
1276 static NTSTATUS rpc_conf_delparm_internal(struct net_context *c,
1277                                           const struct dom_sid *domain_sid,
1278                                           const char *domain_name,
1279                                           struct cli_state *cli,
1280                                           struct rpc_pipe_client *pipe_hnd,
1281                                           TALLOC_CTX *mem_ctx,
1282                                           int argc,
1283                                           const char **argv )
1284 {
1285         TALLOC_CTX *frame = talloc_stackframe();
1286         NTSTATUS status = NT_STATUS_OK;
1287         WERROR werr = WERR_OK;
1288         WERROR _werr;
1289
1290         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1291
1292         /* key info */
1293         struct policy_handle hive_hnd, key_hnd;
1294
1295
1296         ZERO_STRUCT(hive_hnd);
1297         ZERO_STRUCT(key_hnd);
1298
1299
1300         if (argc != 2 || c->display_usage) {
1301                 rpc_conf_delparm_usage(c, argc, argv);
1302                 status = NT_STATUS_INVALID_PARAMETER;
1303                 goto error;
1304         }
1305
1306         status = rpc_conf_open_conf(frame,
1307                                     b,
1308                                     REG_KEY_READ,
1309                                     &hive_hnd,
1310                                     &key_hnd,
1311                                     &werr);
1312
1313         if (!(NT_STATUS_IS_OK(status))) {
1314                 goto error;
1315         }
1316
1317         if (!(W_ERROR_IS_OK(werr))) {
1318                 goto error;
1319         }
1320
1321         status = rpc_conf_del_value(frame,
1322                                     b,
1323                                     &key_hnd,
1324                                     argv[0],
1325                                     argv[1],
1326                                     &werr);
1327
1328 error:
1329
1330         if (!(W_ERROR_IS_OK(werr))) {
1331                 status =  werror_to_ntstatus(werr);
1332         }
1333
1334         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1335         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1336
1337         TALLOC_FREE(frame);
1338         return status;
1339
1340 }
1341
1342 static NTSTATUS rpc_conf_getincludes_internal(struct net_context *c,
1343                                               const struct dom_sid *domain_sid,
1344                                               const char *domain_name,
1345                                               struct cli_state *cli,
1346                                               struct rpc_pipe_client *pipe_hnd,
1347                                               TALLOC_CTX *mem_ctx,
1348                                               int argc,
1349                                               const char **argv )
1350 {
1351         TALLOC_CTX *frame = talloc_stackframe();
1352         NTSTATUS status = NT_STATUS_OK;
1353         WERROR werr = WERR_OK;
1354         WERROR _werr;
1355
1356         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1357
1358         /* key info */
1359         struct policy_handle hive_hnd, key_hnd;
1360         struct smbconf_service *service = NULL;
1361
1362         uint32_t param_count;
1363
1364
1365         ZERO_STRUCT(hive_hnd);
1366         ZERO_STRUCT(key_hnd);
1367
1368
1369         if (argc != 1 || c->display_usage) {
1370                 rpc_conf_getincludes_usage(c, argc, argv);
1371                 status = NT_STATUS_INVALID_PARAMETER;
1372                 goto error;
1373         }
1374
1375         status = rpc_conf_open_conf(frame,
1376                                     b,
1377                                     REG_KEY_READ,
1378                                     &hive_hnd,
1379                                     &key_hnd,
1380                                     &werr);
1381
1382         if (!(NT_STATUS_IS_OK(status))) {
1383                 goto error;
1384         }
1385
1386         if (!(W_ERROR_IS_OK(werr))) {
1387                 goto error;
1388         }
1389
1390         service = talloc(frame, struct smbconf_service);
1391
1392         status = rpc_conf_get_share(frame,
1393                                     b,
1394                                     &key_hnd,
1395                                     argv[0],
1396                                     service,
1397                                     &werr);
1398
1399         if (!(NT_STATUS_IS_OK(status))) {
1400                         goto error;
1401         }
1402
1403         if (!(W_ERROR_IS_OK(werr))) {
1404                         goto error;
1405         }
1406
1407         for (param_count = 0;
1408              param_count < service->num_params;
1409              param_count++)
1410         {
1411                 if (strcmp(service->param_names[param_count], "include") == 0) {
1412                         d_printf(_("%s = %s\n"),
1413                                 service->param_names[param_count],
1414                                 service->param_values[param_count]);
1415                 }
1416         }
1417
1418 error:
1419
1420         if (!(W_ERROR_IS_OK(werr))) {
1421                 status =  werror_to_ntstatus(werr);
1422         }
1423
1424         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1425         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1426
1427         TALLOC_FREE(frame);
1428         return status;
1429
1430 }
1431
1432 static NTSTATUS rpc_conf_setincludes_internal(struct net_context *c,
1433                                               const struct dom_sid *domain_sid,
1434                                               const char *domain_name,
1435                                               struct cli_state *cli,
1436                                               struct rpc_pipe_client *pipe_hnd,
1437                                               TALLOC_CTX *mem_ctx,
1438                                               int argc,
1439                                               const char **argv )
1440 {
1441         TALLOC_CTX *frame = talloc_stackframe();
1442         NTSTATUS status = NT_STATUS_OK;
1443         WERROR werr = WERR_OK;
1444         WERROR _werr;
1445
1446         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1447
1448         /* key info */
1449         struct policy_handle hive_hnd, key_hnd, share_hnd;
1450
1451         struct winreg_String key, keyclass;
1452         enum winreg_CreateAction action = 0;
1453
1454         ZERO_STRUCT(hive_hnd);
1455         ZERO_STRUCT(key_hnd);
1456         ZERO_STRUCT(share_hnd);
1457
1458         ZERO_STRUCT(key);
1459         ZERO_STRUCT(keyclass);
1460
1461         if (argc < 1 || c->display_usage) {
1462                 rpc_conf_setincludes_usage(c, argc, argv);
1463                 status = NT_STATUS_INVALID_PARAMETER;
1464                 goto error;
1465         }
1466
1467         status = rpc_conf_open_conf(frame,
1468                                     b,
1469                                     REG_KEY_READ,
1470                                     &hive_hnd,
1471                                     &key_hnd,
1472                                     &werr);
1473
1474         if (!(NT_STATUS_IS_OK(status))) {
1475                 goto error;
1476         }
1477
1478         if (!(W_ERROR_IS_OK(werr))) {
1479                 goto error;
1480         }
1481
1482         key.name = argv[0];
1483         keyclass.name = "";
1484
1485         status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1486                         0, REG_KEY_READ, NULL, &share_hnd,
1487                         &action, &werr);
1488
1489         if (!(NT_STATUS_IS_OK(status))) {
1490                 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1491                                 argv[0], nt_errstr(status));
1492                 goto error;
1493         }
1494
1495         if (!W_ERROR_IS_OK(werr)) {
1496                 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1497                                 argv[0], win_errstr(werr));
1498                 goto error;
1499         }
1500
1501         switch (action) {
1502                 case REG_ACTION_NONE:
1503                         /* Is there any other way to treat this? */
1504                         werr = WERR_CREATE_FAILED;
1505                         d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1506                                 argv[0], win_errstr(werr));
1507                         goto error;
1508                 case REG_CREATED_NEW_KEY:
1509                         DEBUG(5, ("net rpc conf setincludes:"
1510                                         "createkey created %s\n", argv[0]));
1511                         break;
1512                 case REG_OPENED_EXISTING_KEY:
1513                         DEBUG(5, ("net rpc conf setincludes:"
1514                                         "createkey opened existing %s\n", argv[0]));
1515
1516                         /* delete posibly existing value */
1517                         status = rpc_conf_del_value(frame,
1518                                                     b,
1519                                                     &key_hnd,
1520                                                     argv[0],
1521                                                     "includes",
1522                                                     &werr);
1523
1524                         if (!(NT_STATUS_IS_OK(status))) {
1525                                 goto error;
1526                         }
1527
1528                         if (!(W_ERROR_IS_OK(werr))) {
1529                                 goto error;
1530                         }
1531                         break;
1532         }
1533
1534         /* set the 'includes' values */
1535         status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
1536                                             "includes", argv + 1, &werr);
1537         if (!(NT_STATUS_IS_OK(status))) {
1538                 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
1539                                 nt_errstr(status));
1540                 goto error;
1541         }
1542
1543         if (!(W_ERROR_IS_OK(werr))) {
1544                 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
1545                                 win_errstr(werr));
1546                 goto error;
1547         }
1548
1549 error:
1550
1551         if (!(W_ERROR_IS_OK(werr))) {
1552                 status =  werror_to_ntstatus(werr);
1553         }
1554
1555         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1556         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1557         dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1558
1559         TALLOC_FREE(frame);
1560         return status;
1561 }
1562
1563 static NTSTATUS rpc_conf_delincludes_internal(struct net_context *c,
1564                                               const struct dom_sid *domain_sid,
1565                                               const char *domain_name,
1566                                               struct cli_state *cli,
1567                                               struct rpc_pipe_client *pipe_hnd,
1568                                               TALLOC_CTX *mem_ctx,
1569                                               int argc,
1570                                               const char **argv )
1571 {
1572         TALLOC_CTX *frame = talloc_stackframe();
1573         NTSTATUS status = NT_STATUS_OK;
1574         WERROR werr = WERR_OK;
1575         WERROR _werr;
1576
1577         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1578
1579         /* key info */
1580         struct policy_handle hive_hnd, key_hnd;
1581
1582
1583         ZERO_STRUCT(hive_hnd);
1584         ZERO_STRUCT(key_hnd);
1585
1586
1587         if (argc != 1 || c->display_usage) {
1588                 rpc_conf_delincludes_usage(c, argc, argv);
1589                 status = NT_STATUS_INVALID_PARAMETER;
1590                 goto error;
1591         }
1592 /* try REG_KEY_WRITE */
1593         status = rpc_conf_open_conf(frame,
1594                                     b,
1595                                     REG_KEY_READ,
1596                                     &hive_hnd,
1597                                     &key_hnd,
1598                                     &werr);
1599
1600         if (!(NT_STATUS_IS_OK(status))) {
1601                 goto error;
1602         }
1603
1604         if (!(W_ERROR_IS_OK(werr))) {
1605                 goto error;
1606         }
1607
1608         status = rpc_conf_del_value(frame,
1609                                     b,
1610                                     &key_hnd,
1611                                     argv[0],
1612                                     "includes",
1613                                     &werr);
1614
1615 error:
1616
1617         if (!(W_ERROR_IS_OK(werr))) {
1618                 status =  werror_to_ntstatus(werr);
1619         }
1620
1621         dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1622         dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1623
1624         TALLOC_FREE(frame);
1625         return status;
1626
1627 }
1628
1629 static int rpc_conf_drop(struct net_context *c, int argc,
1630                                 const char **argv)
1631 {
1632         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1633                 rpc_conf_drop_internal, argc, argv );
1634
1635 }
1636
1637 static int rpc_conf_showshare(struct net_context *c, int argc,
1638                                 const char **argv)
1639 {
1640         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1641                 rpc_conf_showshare_internal, argc, argv );
1642 }
1643
1644 static int rpc_conf_addshare(struct net_context *c, int argc,
1645                                 const char **argv)
1646 {
1647         d_printf("Function not yet implemented\n");
1648         return 0;
1649 }
1650
1651 static int rpc_conf_listshares(struct net_context *c, int argc,
1652                                 const char **argv)
1653 {
1654         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1655                 rpc_conf_listshares_internal, argc, argv );
1656 }
1657
1658 static int rpc_conf_list(struct net_context *c, int argc,
1659                              const char **argv)
1660 {
1661         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1662                 rpc_conf_list_internal, argc, argv );
1663 }
1664
1665 static int rpc_conf_delshare(struct net_context *c, int argc,
1666                              const char **argv)
1667 {
1668         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1669                 rpc_conf_delshare_internal, argc, argv );
1670 }
1671
1672 static int rpc_conf_getparm(struct net_context *c, int argc,
1673                              const char **argv)
1674 {
1675         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1676                 rpc_conf_getparm_internal, argc, argv );
1677 }
1678
1679 static int rpc_conf_setparm(struct net_context *c, int argc,
1680                                 const char **argv)
1681 {
1682         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1683                 rpc_conf_setparm_internal, argc, argv );
1684 }
1685 static int rpc_conf_delparm(struct net_context *c, int argc,
1686                                 const char **argv)
1687 {
1688         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1689                 rpc_conf_delparm_internal, argc, argv );
1690 }
1691
1692 static int rpc_conf_getincludes(struct net_context *c, int argc,
1693                              const char **argv)
1694 {
1695         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1696                 rpc_conf_getincludes_internal, argc, argv );
1697 }
1698
1699 static int rpc_conf_setincludes(struct net_context *c, int argc,
1700                                 const char **argv)
1701 {
1702         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1703                 rpc_conf_setincludes_internal, argc, argv );
1704 }
1705
1706 static int rpc_conf_delincludes(struct net_context *c, int argc,
1707                                 const char **argv)
1708 {
1709         return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
1710                 rpc_conf_delincludes_internal, argc, argv );
1711 }
1712
1713 /* function calls */
1714 int net_rpc_conf(struct net_context *c, int argc,
1715                  const char **argv)
1716 {
1717         struct functable func_table[] = {
1718                 {
1719                         "list",
1720                         rpc_conf_list,
1721                         NET_TRANSPORT_RPC,
1722                         N_("Dump the complete remote configuration in smb.conf like "
1723                            "format."),
1724                         N_("net rpc conf list\n"
1725                            "    Dump the complete remote configuration in smb.conf "
1726                            "like format.")
1727
1728                 },
1729                 {
1730                         "listshares",
1731                         rpc_conf_listshares,
1732                         NET_TRANSPORT_RPC,
1733                         N_("List the remote share names."),
1734                         N_("net rpc conf list\n"
1735                            "    List the remote share names.")
1736
1737                 },
1738                 {
1739                         "drop",
1740                         rpc_conf_drop,
1741                         NET_TRANSPORT_RPC,
1742                         N_("Delete the complete remote configuration."),
1743                         N_("net rpc conf drop\n"
1744                            "    Delete the complete remote configuration.")
1745
1746                 },
1747                 {
1748                         "showshare",
1749                         rpc_conf_showshare,
1750                         NET_TRANSPORT_RPC,
1751                         N_("Show the definition of a remote share."),
1752                         N_("net rpc conf showshare\n"
1753                            "    Show the definition of a remote share.")
1754
1755                 },
1756                 {
1757                         "addshare",
1758                         rpc_conf_addshare,
1759                         NET_TRANSPORT_RPC,
1760                         N_("Create a new remote share."),
1761                         N_("net rpc conf addshare\n"
1762                            "    Create a new remote share.")
1763                 },
1764                 {
1765                         "delshare",
1766                         rpc_conf_delshare,
1767                         NET_TRANSPORT_RPC,
1768                         N_("Delete a remote share."),
1769                         N_("net rpc conf delshare\n"
1770                            "    Delete a remote share.")
1771                 },
1772                 {
1773                         "getparm",
1774                         rpc_conf_getparm,
1775                         NET_TRANSPORT_RPC,
1776                         N_("Retrieve the value of a parameter."),
1777                         N_("net rpc conf getparm\n"
1778                            "    Retrieve the value of a parameter.")
1779                 },
1780                 {
1781                         "setparm",
1782                         rpc_conf_setparm,
1783                         NET_TRANSPORT_RPC,
1784                         N_("Store a parameter."),
1785                         N_("net rpc conf setparm\n"
1786                            "    Store a parameter.")
1787                 },
1788                 {
1789                         "delparm",
1790                         rpc_conf_delparm,
1791                         NET_TRANSPORT_RPC,
1792                         N_("Delete a parameter."),
1793                         N_("net rpc conf delparm\n"
1794                            "    Delete a parameter.")
1795                 },
1796                 {
1797                         "getincludes",
1798                         rpc_conf_getincludes,
1799                         NET_TRANSPORT_RPC,
1800                         N_("Show the includes of a share definition."),
1801                         N_("net rpc conf getincludes\n"
1802                            "    Show the includes of a share definition.")
1803                 },
1804                 {
1805                         "setincludes",
1806                         rpc_conf_setincludes,
1807                         NET_TRANSPORT_RPC,
1808                         N_("Set includes for a share."),
1809                         N_("net rpc conf setincludes\n"
1810                            "    Set includes for a share.")
1811                 },
1812                 {
1813                         "delincludes",
1814                         rpc_conf_delincludes,
1815                         NET_TRANSPORT_RPC,
1816                         N_("Delete includes from a share definition."),
1817                         N_("net rpc conf delincludes\n"
1818                            "    Delete includes from a share definition.")
1819                 },
1820                 {NULL, NULL, 0, NULL, NULL}
1821         };
1822
1823         return net_run_function(c, argc, argv, "net rpc conf", func_table);
1824
1825 }