Add a couple of comment headers to the main libnet_conf functions.
[ira/wip.git] / source / 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 /**********************************************************************
271  *
272  * The actual net conf api functions, that are exported.
273  *
274  **********************************************************************/
275
276 /**
277  * Drop the whole configuration (restarting empty).
278  */
279 WERROR libnet_smbconf_drop(void)
280 {
281         char *path, *p;
282         WERROR werr = WERR_OK;
283         NT_USER_TOKEN *token;
284         struct registry_key *parent_key = NULL;
285         struct registry_key *new_key = NULL;
286         TALLOC_CTX* mem_ctx = talloc_stackframe();
287         enum winreg_CreateAction action;
288
289         if (!(token = registry_create_admin_token(mem_ctx))) {
290                 /* what is the appropriate error code here? */
291                 werr = WERR_CAN_NOT_COMPLETE;
292                 goto done;
293         }
294
295         path = talloc_strdup(mem_ctx, KEY_SMBCONF);
296         if (path == NULL) {
297                 werr = WERR_NOMEM;
298                 goto done;
299         }
300         p = strrchr(path, '\\');
301         *p = '\0';
302         werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
303
304         if (!W_ERROR_IS_OK(werr)) {
305                 goto done;
306         }
307
308         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
309
310         if (!W_ERROR_IS_OK(werr)) {
311                 goto done;
312         }
313
314         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
315                              &new_key, &action);
316
317 done:
318         TALLOC_FREE(mem_ctx);
319         return werr;
320 }
321
322 /**
323  * delete a service from configuration
324  */
325 WERROR libnet_smbconf_delshare(const char *servicename)
326 {
327         WERROR werr = WERR_OK;
328         struct registry_key *key = NULL;
329         TALLOC_CTX *ctx = talloc_stackframe();
330
331         werr = libnet_smbconf_reg_open_basepath(ctx, REG_KEY_WRITE, &key);
332         if (!W_ERROR_IS_OK(werr)) {
333                 goto done;
334         }
335
336         werr = reg_deletekey_recursive(key, key, servicename);
337
338 done:
339         TALLOC_FREE(ctx);
340         return werr;
341 }
342
343 /**
344  * set a configuration parameter to the value provided.
345  */
346 WERROR libnet_smbconf_setparm(const char *service,
347                               const char *param,
348                               const char *valstr)
349 {
350         WERROR werr;
351         struct registry_key *key = NULL;
352         TALLOC_CTX *mem_ctx = talloc_stackframe();
353
354         if (!libnet_smbconf_key_exists(service)) {
355                 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
356                                                              &key);
357         } else {
358                 werr = libnet_smbconf_reg_open_path(mem_ctx, service,
359                                                     REG_KEY_WRITE, &key);
360         }
361         if (!W_ERROR_IS_OK(werr)) {
362                 goto done;
363         }
364
365         werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
366
367 done:
368         TALLOC_FREE(mem_ctx);
369         return werr;
370 }
371
372 /**
373  * get the value of a configuration parameter as a string
374  */
375 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
376                               const char *service,
377                               const char *param,
378                               char **valstr)
379 {
380         WERROR werr = WERR_OK;
381         struct registry_key *key = NULL;
382         struct registry_value *value = NULL;
383
384         if (valstr == NULL) {
385                 werr = WERR_INVALID_PARAM;
386                 goto done;
387         }
388
389         if (!libnet_smbconf_key_exists(service)) {
390                 werr = WERR_NO_SUCH_SERVICE;
391                 goto done;
392         }
393
394         werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_READ,
395                                             &key);
396         if (!W_ERROR_IS_OK(werr)) {
397                 goto done;
398         }
399
400         if (!libnet_smbconf_value_exists(key, param)) {
401                 werr = WERR_INVALID_PARAM;
402                 goto done;
403         }
404
405         werr = reg_queryvalue(mem_ctx, key, param, &value);
406         if (!W_ERROR_IS_OK(werr)) {
407                 goto done;
408         }
409
410         *valstr = libnet_smbconf_format_registry_value(mem_ctx, value);
411
412         if (*valstr == NULL) {
413                 werr = WERR_NOMEM;
414         }
415
416 done:
417         TALLOC_FREE(key);
418         TALLOC_FREE(value);
419         return werr;
420 }
421
422 /**
423  * delete a parameter from configuration
424  */
425 WERROR libnet_smbconf_delparm(const char *service,
426                               const char *param)
427 {
428         struct registry_key *key = NULL;
429         WERROR werr = WERR_OK;
430         TALLOC_CTX *mem_ctx = talloc_stackframe();
431
432         if (!libnet_smbconf_key_exists(service)) {
433                 return WERR_NO_SUCH_SERVICE;
434         }
435
436         werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_ALL, &key);
437         if (!W_ERROR_IS_OK(werr)) {
438                 goto done;
439         }
440
441         if (!libnet_smbconf_value_exists(key, param)) {
442                 werr = WERR_INVALID_PARAM;
443                 goto done;
444         }
445
446         werr = reg_deletevalue(key, param);
447
448 done:
449         TALLOC_FREE(mem_ctx);
450         return werr;
451 }
452
453
454 /**********************************************************************
455  *
456  * Convenience functions that are also exported.
457  *
458  **********************************************************************/
459
460 WERROR libnet_smbconf_set_global_param(const char *param,
461                                        const char *val)
462 {
463         return libnet_smbconf_setparm(GLOBAL_NAME, param, val);
464 }
465