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