636e966a37d06dd8e851b3c48a3f70486a330bc2
[ira/wip.git] / source3 / libnet / libnet_conf.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libnet smbconf registry Support
4  *  Copyright (C) Michael Adam 2007
5  *  Copyright (C) Guenther Deschner 2007
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 #include "includes.h"
22
23 /**********************************************************************
24  *
25  * Helper functions (mostly registry related)
26  * TODO: These should be eventually static.
27
28  **********************************************************************/
29
30 /**
31  * add a string to a talloced array of strings.
32  */
33 static WERROR libnet_smbconf_add_string_to_array(TALLOC_CTX *mem_ctx,
34                                                 char ***array,
35                                                 uint32_t count,
36                                                 const char *string)
37 {
38         WERROR werr = WERR_OK;
39         char **new_array = NULL;
40
41         if ((array == NULL) || (string == NULL)) {
42                 return WERR_INVALID_PARAM;
43         }
44
45         new_array = TALLOC_REALLOC_ARRAY(mem_ctx, *array, char *, count + 1);
46         if (new_array == NULL) {
47                 return WERR_NOMEM;
48         }
49
50         new_array[count] = talloc_strdup(new_array, string);
51
52         *array = new_array;
53
54         return WERR_OK;
55 }
56
57 /*
58  * Open a subkey of KEY_SMBCONF (i.e a service)
59  */
60 WERROR libnet_smbconf_reg_open_path(TALLOC_CTX *ctx,
61                                     const char *subkeyname,
62                                     uint32 desired_access,
63                                     struct registry_key **key)
64 {
65         WERROR werr = WERR_OK;
66         char *path = NULL;
67         NT_USER_TOKEN *token;
68
69         if (!(token = registry_create_admin_token(ctx))) {
70                 DEBUG(1, ("Error creating admin token\n"));
71                 goto done;
72         }
73
74         if (subkeyname == NULL) {
75                 path = talloc_strdup(ctx, KEY_SMBCONF);
76         } else {
77                 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
78         }
79
80         werr = reg_open_path(ctx, path, desired_access,
81                              token, key);
82
83         if (!W_ERROR_IS_OK(werr)) {
84                 DEBUG(1, ("Error opening registry path '%s': %s\n",
85                           path, dos_errstr(werr)));
86         }
87
88 done:
89         TALLOC_FREE(path);
90         return werr;
91 }
92
93 /*
94  * open the base key KEY_SMBCONF
95  */
96 WERROR libnet_smbconf_reg_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
97                                         struct registry_key **key)
98 {
99         return libnet_smbconf_reg_open_path(ctx, NULL, desired_access, key);
100 }
101
102 /*
103  * check if a subkey of KEY_SMBCONF of a given name exists
104  */
105 bool libnet_smbconf_key_exists(const char *subkeyname)
106 {
107         bool ret = false;
108         WERROR werr = WERR_OK;
109         TALLOC_CTX *mem_ctx = talloc_stackframe();
110         struct registry_key *key = NULL;
111
112         werr = libnet_smbconf_reg_open_path(mem_ctx, subkeyname, REG_KEY_READ,
113                                             &key);
114         if (W_ERROR_IS_OK(werr)) {
115                 ret = true;
116         }
117
118         TALLOC_FREE(mem_ctx);
119         return ret;
120 }
121
122 static bool libnet_smbconf_value_exists(struct registry_key *key,
123                                         const char *param)
124 {
125         bool ret = false;
126         WERROR werr = WERR_OK;
127         TALLOC_CTX *ctx = talloc_stackframe();
128         struct registry_value *value = NULL;
129
130         werr = reg_queryvalue(ctx, key, param, &value);
131         if (W_ERROR_IS_OK(werr)) {
132                 ret = true;
133         }
134
135         TALLOC_FREE(ctx);
136         return ret;
137 }
138
139 /*
140  * create a subkey of KEY_SMBCONF
141  */
142 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
143                                              const char * subkeyname,
144                                              struct registry_key **newkey)
145 {
146         WERROR werr = WERR_OK;
147         struct registry_key *create_parent = NULL;
148         TALLOC_CTX *create_ctx;
149         enum winreg_CreateAction action = REG_ACTION_NONE;
150
151         /* create a new talloc ctx for creation. it will hold
152          * the intermediate parent key (SMBCONF) for creation
153          * and will be destroyed when leaving this function... */
154         if (!(create_ctx = talloc_new(ctx))) {
155                 werr = WERR_NOMEM;
156                 goto done;
157         }
158
159         werr = libnet_smbconf_reg_open_basepath(create_ctx, REG_KEY_WRITE,
160                                                 &create_parent);
161         if (!W_ERROR_IS_OK(werr)) {
162                 goto done;
163         }
164
165         werr = reg_createkey(ctx, create_parent, subkeyname,
166                              REG_KEY_WRITE, newkey, &action);
167         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
168                 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
169                 werr = WERR_ALREADY_EXISTS;
170         }
171         if (!W_ERROR_IS_OK(werr)) {
172                 d_fprintf(stderr, "Error creating key %s: %s\n",
173                          subkeyname, dos_errstr(werr));
174         }
175
176 done:
177         TALLOC_FREE(create_ctx);
178         return werr;
179 }
180
181 /*
182  * add a value to a key.
183  */
184 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
185                                                    const char *valname,
186                                                    const char *valstr)
187 {
188         struct registry_value val;
189         WERROR werr = WERR_OK;
190         char *subkeyname;
191         const char *canon_valname;
192         const char *canon_valstr;
193
194         if (!lp_canonicalize_parameter_with_value(valname, valstr,
195                                                   &canon_valname,
196                                                   &canon_valstr))
197         {
198                 if (canon_valname == NULL) {
199                         d_fprintf(stderr, "invalid parameter '%s' given\n",
200                                   valname);
201                 } else {
202                         d_fprintf(stderr, "invalid value '%s' given for "
203                                   "parameter '%s'\n", valstr, valname);
204                 }
205                 werr = WERR_INVALID_PARAM;
206                 goto done;
207         }
208
209         ZERO_STRUCT(val);
210
211         val.type = REG_SZ;
212         val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
213         val.v.sz.len = strlen(canon_valstr) + 1;
214
215         if (registry_smbconf_valname_forbidden(canon_valname)) {
216                 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
217                           canon_valname);
218                 werr = WERR_INVALID_PARAM;
219                 goto done;
220         }
221
222         subkeyname = strrchr_m(key->key->name, '\\');
223         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
224                 d_fprintf(stderr, "Invalid registry key '%s' given as "
225                           "smbconf section.\n", key->key->name);
226                 werr = WERR_INVALID_PARAM;
227                 goto done;
228         }
229         subkeyname++;
230         if (!strequal(subkeyname, GLOBAL_NAME) &&
231             lp_parameter_is_global(valname))
232         {
233                 d_fprintf(stderr, "Global paramter '%s' not allowed in "
234                           "service definition ('%s').\n", canon_valname,
235                           subkeyname);
236                 werr = WERR_INVALID_PARAM;
237                 goto done;
238         }
239
240         werr = reg_setvalue(key, canon_valname, &val);
241         if (!W_ERROR_IS_OK(werr)) {
242                 d_fprintf(stderr,
243                           "Error adding value '%s' to "
244                           "key '%s': %s\n",
245                           canon_valname, key->key->name, dos_errstr(werr));
246         }
247
248 done:
249         return werr;
250 }
251
252 /**
253  * format a registry_value into a string.
254  *
255  * This is intended to be used for smbconf registry values,
256  * which are ar stored as REG_SZ values, so the incomplete
257  * handling should be ok.
258  */
259 char *libnet_smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
260                                            struct registry_value *value)
261 {
262         char *result = NULL;
263
264         /* alternatively, create a new talloc context? */
265         if (mem_ctx == NULL) {
266                 return result;
267         }
268
269         switch (value->type) {
270         case REG_DWORD:
271                 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
272                 break;
273         case REG_SZ:
274         case REG_EXPAND_SZ:
275                 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
276                 break;
277         case REG_MULTI_SZ: {
278                 uint32 j;
279                 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
280                         result = talloc_asprintf(mem_ctx, "\"%s\" ",
281                                                  value->v.multi_sz.strings[j]);
282                 }
283                 break;
284         }
285         case REG_BINARY:
286                 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
287                                          (int)value->v.binary.length);
288                 break;
289         default:
290                 result = talloc_asprintf(mem_ctx, "<unprintable>");
291                 break;
292         }
293         return result;
294 }
295
296 /**
297  * Get the values of a key as a list of value names
298  * and a list of value strings (ordered)
299  */
300 static WERROR libnet_smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
301                                             struct registry_key *key,
302                                             uint32_t *num_values,
303                                             char ***value_names,
304                                             char ***value_strings)
305 {
306         TALLOC_CTX *tmp_ctx = NULL;
307         WERROR werr = WERR_OK;
308         uint32_t count;
309         struct registry_value *valvalue = NULL;
310         char *valname = NULL;
311         char **tmp_valnames = NULL;
312         char **tmp_valstrings = NULL;
313
314         if ((num_values == NULL) || (value_names == NULL) ||
315             (value_strings == NULL))
316         {
317                 werr = WERR_INVALID_PARAM;
318                 goto done;
319         }
320
321         tmp_ctx = talloc_new(mem_ctx);
322         if (tmp_ctx == NULL) {
323                 werr = WERR_NOMEM;
324                 goto done;
325         }
326
327         for (count = 0;
328              W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
329                                                 &valvalue));
330              count++)
331         {
332                 char *valstring;
333
334                 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
335                                                           &tmp_valnames,
336                                                           count, valname);
337                 if (!W_ERROR_IS_OK(werr)) {
338                         goto done;
339                 }
340
341                 valstring = libnet_smbconf_format_registry_value(tmp_ctx,
342                                                                  valvalue);
343                 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
344                                                           &tmp_valstrings,
345                                                           count,
346                                                           valstring);
347                 if (!W_ERROR_IS_OK(werr)) {
348                         goto done;
349                 }
350         }
351         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
352                 goto done;
353         }
354
355         werr = WERR_OK;
356
357         *num_values = count - 1;
358         if (count > 0) {
359                 *value_names = talloc_move(mem_ctx, &tmp_valnames);
360                 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
361         }
362
363 done:
364         TALLOC_FREE(tmp_ctx);
365         return werr;
366 }
367
368 /**********************************************************************
369  *
370  * The actual net conf api functions, that are exported.
371  *
372  **********************************************************************/
373
374 /**
375  * Drop the whole configuration (restarting empty).
376  */
377 WERROR libnet_smbconf_drop(void)
378 {
379         char *path, *p;
380         WERROR werr = WERR_OK;
381         NT_USER_TOKEN *token;
382         struct registry_key *parent_key = NULL;
383         struct registry_key *new_key = NULL;
384         TALLOC_CTX* mem_ctx = talloc_stackframe();
385         enum winreg_CreateAction action;
386
387         if (!(token = registry_create_admin_token(mem_ctx))) {
388                 /* what is the appropriate error code here? */
389                 werr = WERR_CAN_NOT_COMPLETE;
390                 goto done;
391         }
392
393         path = talloc_strdup(mem_ctx, KEY_SMBCONF);
394         if (path == NULL) {
395                 werr = WERR_NOMEM;
396                 goto done;
397         }
398         p = strrchr(path, '\\');
399         *p = '\0';
400         werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
401
402         if (!W_ERROR_IS_OK(werr)) {
403                 goto done;
404         }
405
406         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
407
408         if (!W_ERROR_IS_OK(werr)) {
409                 goto done;
410         }
411
412         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
413                              &new_key, &action);
414
415 done:
416         TALLOC_FREE(mem_ctx);
417         return werr;
418 }
419
420 /**
421  * get the list of share names defined in the configuration.
422  */
423 WERROR libnet_smbconf_get_share_names(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
424                                       char ***share_names)
425 {
426         uint32_t count;
427         uint32_t added_count = 0;
428         TALLOC_CTX *tmp_ctx = NULL;
429         WERROR werr = WERR_OK;
430         struct registry_key *key = NULL;
431         char *subkey_name = NULL;
432         char **tmp_share_names = NULL;
433
434         if ((num_shares == NULL) || (share_names == NULL)) {
435                 werr = WERR_INVALID_PARAM;
436                 goto done;
437         }
438
439         tmp_ctx = talloc_new(mem_ctx);
440         if (tmp_ctx == NULL) {
441                 werr = WERR_NOMEM;
442                 goto done;
443         }
444
445         /* make sure "global" is always listed first */
446         if (libnet_smbconf_key_exists(GLOBAL_NAME)) {
447                 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
448                                                           &tmp_share_names,
449                                                           0, GLOBAL_NAME);
450                 if (!W_ERROR_IS_OK(werr)) {
451                         goto done;
452                 }
453                 added_count++;
454         }
455
456         werr = libnet_smbconf_reg_open_basepath(tmp_ctx,
457                                                 SEC_RIGHTS_ENUM_SUBKEYS,
458                                                 &key);
459         if (!W_ERROR_IS_OK(werr)) {
460                 goto done;
461         }
462
463         for (count = 0;
464              W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
465                                               &subkey_name, NULL));
466              count++)
467         {
468                 if (strequal(subkey_name, GLOBAL_NAME)) {
469                         continue;
470                 }
471
472                 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
473                                                           &tmp_share_names,
474                                                           added_count,
475                                                           subkey_name);
476                 if (!W_ERROR_IS_OK(werr)) {
477                         goto done;
478                 }
479                 added_count++;
480         }
481         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
482                 goto done;
483         }
484         werr = WERR_OK;
485
486         *num_shares = added_count;
487         if (added_count > 0) {
488                 *share_names = talloc_move(mem_ctx, &tmp_share_names);
489         }
490
491 done:
492         TALLOC_FREE(tmp_ctx);
493         return werr;
494 }
495
496 /**
497  * get a definition of a share (service) from configuration.
498  */
499 WERROR libnet_smbconf_getshare(TALLOC_CTX *mem_ctx, const char *servicename,
500                                uint32_t *num_params, char ***param_names,
501                                char ***param_values)
502 {
503         WERROR werr = WERR_OK;
504         struct registry_key *key = NULL;
505
506         werr = libnet_smbconf_reg_open_path(mem_ctx, servicename, REG_KEY_READ,
507                                             &key);
508         if (!W_ERROR_IS_OK(werr)) {
509                 goto done;
510         }
511
512         werr = libnet_smbconf_reg_get_values(mem_ctx, key, num_params,
513                                              param_names, param_values);
514
515 done:
516         TALLOC_FREE(key);
517         return werr;
518 }
519
520 /**
521  * delete a service from configuration
522  */
523 WERROR libnet_smbconf_delshare(const char *servicename)
524 {
525         WERROR werr = WERR_OK;
526         struct registry_key *key = NULL;
527         TALLOC_CTX *ctx = talloc_stackframe();
528
529         werr = libnet_smbconf_reg_open_basepath(ctx, REG_KEY_WRITE, &key);
530         if (!W_ERROR_IS_OK(werr)) {
531                 goto done;
532         }
533
534         werr = reg_deletekey_recursive(key, key, servicename);
535
536 done:
537         TALLOC_FREE(ctx);
538         return werr;
539 }
540
541 /**
542  * set a configuration parameter to the value provided.
543  */
544 WERROR libnet_smbconf_setparm(const char *service,
545                               const char *param,
546                               const char *valstr)
547 {
548         WERROR werr;
549         struct registry_key *key = NULL;
550         TALLOC_CTX *mem_ctx = talloc_stackframe();
551
552         if (!libnet_smbconf_key_exists(service)) {
553                 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
554                                                              &key);
555         } else {
556                 werr = libnet_smbconf_reg_open_path(mem_ctx, service,
557                                                     REG_KEY_WRITE, &key);
558         }
559         if (!W_ERROR_IS_OK(werr)) {
560                 goto done;
561         }
562
563         werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
564
565 done:
566         TALLOC_FREE(mem_ctx);
567         return werr;
568 }
569
570 /**
571  * get the value of a configuration parameter as a string
572  */
573 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
574                               const char *service,
575                               const char *param,
576                               char **valstr)
577 {
578         WERROR werr = WERR_OK;
579         struct registry_key *key = NULL;
580         struct registry_value *value = NULL;
581
582         if (valstr == NULL) {
583                 werr = WERR_INVALID_PARAM;
584                 goto done;
585         }
586
587         if (!libnet_smbconf_key_exists(service)) {
588                 werr = WERR_NO_SUCH_SERVICE;
589                 goto done;
590         }
591
592         werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_READ,
593                                             &key);
594         if (!W_ERROR_IS_OK(werr)) {
595                 goto done;
596         }
597
598         if (!libnet_smbconf_value_exists(key, param)) {
599                 werr = WERR_INVALID_PARAM;
600                 goto done;
601         }
602
603         werr = reg_queryvalue(mem_ctx, key, param, &value);
604         if (!W_ERROR_IS_OK(werr)) {
605                 goto done;
606         }
607
608         *valstr = libnet_smbconf_format_registry_value(mem_ctx, value);
609
610         if (*valstr == NULL) {
611                 werr = WERR_NOMEM;
612         }
613
614 done:
615         TALLOC_FREE(key);
616         TALLOC_FREE(value);
617         return werr;
618 }
619
620 /**
621  * delete a parameter from configuration
622  */
623 WERROR libnet_smbconf_delparm(const char *service,
624                               const char *param)
625 {
626         struct registry_key *key = NULL;
627         WERROR werr = WERR_OK;
628         TALLOC_CTX *mem_ctx = talloc_stackframe();
629
630         if (!libnet_smbconf_key_exists(service)) {
631                 return WERR_NO_SUCH_SERVICE;
632         }
633
634         werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_ALL, &key);
635         if (!W_ERROR_IS_OK(werr)) {
636                 goto done;
637         }
638
639         if (!libnet_smbconf_value_exists(key, param)) {
640                 werr = WERR_INVALID_PARAM;
641                 goto done;
642         }
643
644         werr = reg_deletevalue(key, param);
645
646 done:
647         TALLOC_FREE(mem_ctx);
648         return werr;
649 }
650
651
652 /**********************************************************************
653  *
654  * Convenience functions that are also exported.
655  *
656  **********************************************************************/
657
658 WERROR libnet_smbconf_set_global_param(const char *param,
659                                        const char *val)
660 {
661         return libnet_smbconf_setparm(GLOBAL_NAME, param, val);
662 }
663