Typofix in comment.
[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  * - variant without error output (q = quiet)-
33  */
34 static WERROR libnet_smbconf_open_path_q(TALLOC_CTX *ctx,
35                                          const char *subkeyname,
36                                          uint32 desired_access,
37                                          struct registry_key **key)
38 {
39         WERROR werr = WERR_OK;
40         char *path = NULL;
41         NT_USER_TOKEN *token;
42
43         if (!(token = registry_create_admin_token(ctx))) {
44                 DEBUG(1, ("Error creating admin token\n"));
45                 goto done;
46         }
47
48         if (subkeyname == NULL) {
49                 path = talloc_strdup(ctx, KEY_SMBCONF);
50         } else {
51                 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
52         }
53
54         werr = reg_open_path(ctx, path, desired_access,
55                              token, key);
56
57 done:
58         TALLOC_FREE(path);
59         return werr;
60 }
61
62 /*
63  * check if a subkey of KEY_SMBCONF of a given name exists
64  */
65 bool libnet_smbconf_key_exists(const char *subkeyname)
66 {
67         bool ret = false;
68         WERROR werr = WERR_OK;
69         TALLOC_CTX *mem_ctx = talloc_stackframe();
70         struct registry_key *key = NULL;
71
72         werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
73         if (W_ERROR_IS_OK(werr)) {
74                 ret = true;
75         }
76
77         TALLOC_FREE(mem_ctx);
78         return ret;
79 }
80
81 static bool libnet_smbconf_value_exists(struct registry_key *key,
82                                         const char *param)
83 {
84         bool ret = false;
85         WERROR werr = WERR_OK;
86         TALLOC_CTX *ctx = talloc_stackframe();
87         struct registry_value *value = NULL;
88
89         werr = reg_queryvalue(ctx, key, param, &value);
90         if (W_ERROR_IS_OK(werr)) {
91                 ret = true;
92         }
93
94         TALLOC_FREE(ctx);
95         return ret;
96 }
97
98 /*
99  * Open a subkey of KEY_SMBCONF (i.e a service)
100  * - variant with error output -
101  */
102 WERROR libnet_smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
103                                 uint32 desired_access,
104                                 struct registry_key **key)
105 {
106         WERROR werr = WERR_OK;
107
108         werr = libnet_smbconf_open_path_q(ctx, subkeyname, desired_access, key);
109         if (!W_ERROR_IS_OK(werr)) {
110                 d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
111                           KEY_SMBCONF,
112                           (subkeyname == NULL) ? "" : subkeyname,
113                           dos_errstr(werr));
114         }
115
116         return werr;
117 }
118
119 /*
120  * open the base key KEY_SMBCONF
121  */
122 WERROR libnet_smbconf_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
123                                     struct registry_key **key)
124 {
125         return libnet_smbconf_open_path(ctx, NULL, desired_access, key);
126 }
127
128 /*
129  * create a subkey of KEY_SMBCONF
130  */
131 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
132                                              const char * subkeyname,
133                                              struct registry_key **newkey)
134 {
135         WERROR werr = WERR_OK;
136         struct registry_key *create_parent = NULL;
137         TALLOC_CTX *create_ctx;
138         enum winreg_CreateAction action = REG_ACTION_NONE;
139
140         /* create a new talloc ctx for creation. it will hold
141          * the intermediate parent key (SMBCONF) for creation
142          * and will be destroyed when leaving this function... */
143         if (!(create_ctx = talloc_new(ctx))) {
144                 werr = WERR_NOMEM;
145                 goto done;
146         }
147
148         werr = libnet_smbconf_open_basepath(create_ctx, REG_KEY_WRITE, &create_parent);
149         if (!W_ERROR_IS_OK(werr)) {
150                 goto done;
151         }
152
153         werr = reg_createkey(ctx, create_parent, subkeyname,
154                              REG_KEY_WRITE, newkey, &action);
155         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
156                 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
157                 werr = WERR_ALREADY_EXISTS;
158         }
159         if (!W_ERROR_IS_OK(werr)) {
160                 d_fprintf(stderr, "Error creating key %s: %s\n",
161                          subkeyname, dos_errstr(werr));
162         }
163
164 done:
165         TALLOC_FREE(create_ctx);
166         return werr;
167 }
168
169 /*
170  * add a value to a key.
171  */
172 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
173                                                    const char *valname,
174                                                    const char *valstr)
175 {
176         struct registry_value val;
177         WERROR werr = WERR_OK;
178         char *subkeyname;
179         const char *canon_valname;
180         const char *canon_valstr;
181
182         if (!lp_canonicalize_parameter_with_value(valname, valstr,
183                                                   &canon_valname,
184                                                   &canon_valstr))
185         {
186                 if (canon_valname == NULL) {
187                         d_fprintf(stderr, "invalid parameter '%s' given\n",
188                                   valname);
189                 } else {
190                         d_fprintf(stderr, "invalid value '%s' given for "
191                                   "parameter '%s'\n", valstr, valname);
192                 }
193                 werr = WERR_INVALID_PARAM;
194                 goto done;
195         }
196
197         ZERO_STRUCT(val);
198
199         val.type = REG_SZ;
200         val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
201         val.v.sz.len = strlen(canon_valstr) + 1;
202
203         if (registry_smbconf_valname_forbidden(canon_valname)) {
204                 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
205                           canon_valname);
206                 werr = WERR_INVALID_PARAM;
207                 goto done;
208         }
209
210         subkeyname = strrchr_m(key->key->name, '\\');
211         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
212                 d_fprintf(stderr, "Invalid registry key '%s' given as "
213                           "smbconf section.\n", key->key->name);
214                 werr = WERR_INVALID_PARAM;
215                 goto done;
216         }
217         subkeyname++;
218         if (!strequal(subkeyname, GLOBAL_NAME) &&
219             lp_parameter_is_global(valname))
220         {
221                 d_fprintf(stderr, "Global paramter '%s' not allowed in "
222                           "service definition ('%s').\n", canon_valname,
223                           subkeyname);
224                 werr = WERR_INVALID_PARAM;
225                 goto done;
226         }
227
228         werr = reg_setvalue(key, canon_valname, &val);
229         if (!W_ERROR_IS_OK(werr)) {
230                 d_fprintf(stderr,
231                           "Error adding value '%s' to "
232                           "key '%s': %s\n",
233                           canon_valname, key->key->name, dos_errstr(werr));
234         }
235
236 done:
237         return werr;
238 }
239
240 /**********************************************************************
241  *
242  * The actual net conf api functions, that are exported.
243  *
244  **********************************************************************/
245
246 /**
247  * Drop the whole configuration (restarting empty).
248  */
249 WERROR libnet_smbconf_drop(void)
250 {
251         char *path, *p;
252         WERROR werr = WERR_OK;
253         NT_USER_TOKEN *token;
254         struct registry_key *parent_key = NULL;
255         struct registry_key *new_key = NULL;
256         TALLOC_CTX* mem_ctx = talloc_stackframe();
257         enum winreg_CreateAction action;
258
259         if (!(token = registry_create_admin_token(mem_ctx))) {
260                 /* what is the appropriate error code here? */
261                 werr = WERR_CAN_NOT_COMPLETE;
262                 goto done;
263         }
264
265         path = talloc_strdup(mem_ctx, KEY_SMBCONF);
266         if (path == NULL) {
267                 werr = WERR_NOMEM;
268                 goto done;
269         }
270         p = strrchr(path, '\\');
271         *p = '\0';
272         werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
273
274         if (!W_ERROR_IS_OK(werr)) {
275                 goto done;
276         }
277
278         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
279
280         if (!W_ERROR_IS_OK(werr)) {
281                 goto done;
282         }
283
284         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
285                              &new_key, &action);
286
287 done:
288         TALLOC_FREE(mem_ctx);
289         return werr;
290 }
291
292 /**
293  * delete a service from configuration
294  */
295 WERROR libnet_smbconf_delshare(const char *servicename)
296 {
297         WERROR werr = WERR_OK;
298         struct registry_key *key = NULL;
299         TALLOC_CTX *ctx = talloc_stackframe();
300
301         werr = libnet_smbconf_open_basepath(ctx, REG_KEY_WRITE, &key);
302         if (!W_ERROR_IS_OK(werr)) {
303                 goto done;
304         }
305
306         werr = reg_deletekey_recursive(key, key, servicename);
307
308 done:
309         TALLOC_FREE(ctx);
310         return werr;
311 }
312
313 WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
314                               const char *service,
315                               const char *param,
316                               const char *valstr)
317 {
318         WERROR werr;
319         struct registry_key *key = NULL;
320
321         if (!libnet_smbconf_key_exists(service)) {
322                 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
323                                                              &key);
324         } else {
325                 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
326                                                 &key);
327         }
328         if (!W_ERROR_IS_OK(werr)) {
329                 goto done;
330         }
331
332         werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
333
334 done:
335         TALLOC_FREE(key);
336         return werr;
337 }
338
339 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
340                               const char *service,
341                               const char *param,
342                               struct registry_value **value)
343 {
344         WERROR werr;
345         struct registry_key *key = NULL;
346
347         if (!libnet_smbconf_key_exists(service)) {
348                 werr = WERR_NO_SUCH_SERVICE;
349                 goto done;
350         }
351
352         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
353         if (!W_ERROR_IS_OK(werr)) {
354                 goto done;
355         }
356
357         if (!libnet_smbconf_value_exists(key, param)) {
358                 werr = WERR_INVALID_PARAM;
359                 goto done;
360         }
361
362         werr = reg_queryvalue(mem_ctx, key, param, value);
363
364 done:
365         TALLOC_FREE(key);
366         return werr;
367 }
368
369 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
370                               const char *service,
371                               const char *param)
372 {
373         struct registry_key *key = NULL;
374         WERROR werr = WERR_OK;
375
376         if (!libnet_smbconf_key_exists(service)) {
377                 return WERR_NO_SUCH_SERVICE;
378         }
379
380         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
381         if (!W_ERROR_IS_OK(werr)) {
382                 goto done;
383         }
384
385         if (!libnet_smbconf_value_exists(key, param)) {
386                 werr = WERR_INVALID_PARAM;
387                 goto done;
388         }
389
390         werr = reg_deletevalue(key, param);
391
392 done:
393         TALLOC_FREE(key);
394         return werr;
395 }
396
397
398 /**********************************************************************
399  *
400  * Convenience functions that are also exported.
401  *
402  **********************************************************************/
403
404 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
405                                        const char *param,
406                                        const char *val)
407 {
408         return libnet_smbconf_setparm(mem_ctx, GLOBAL_NAME, param, val);
409 }
410