Remove a d_fprintf() from libnet_smbconf_drop().
[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 WERROR libnet_smbconf_drop(void)
247 {
248         char *path, *p;
249         WERROR werr = WERR_OK;
250         NT_USER_TOKEN *token;
251         struct registry_key *parent_key = NULL;
252         struct registry_key *new_key = NULL;
253         TALLOC_CTX* mem_ctx = talloc_stackframe();
254         enum winreg_CreateAction action;
255
256         if (!(token = registry_create_admin_token(mem_ctx))) {
257                 /* what is the appropriate error code here? */
258                 werr = WERR_CAN_NOT_COMPLETE;
259                 goto done;
260         }
261
262         path = talloc_strdup(mem_ctx, KEY_SMBCONF);
263         if (path == NULL) {
264                 werr = WERR_NOMEM;
265                 goto done;
266         }
267         p = strrchr(path, '\\');
268         *p = '\0';
269         werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
270
271         if (!W_ERROR_IS_OK(werr)) {
272                 goto done;
273         }
274
275         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
276
277         if (!W_ERROR_IS_OK(werr)) {
278                 goto done;
279         }
280
281         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
282                              &new_key, &action);
283
284 done:
285         TALLOC_FREE(mem_ctx);
286         return werr;
287 }
288
289 /**
290  * delete a service from configuration
291  */
292 WERROR libnet_smbconf_delshare(const char *servicename)
293 {
294         WERROR werr = WERR_OK;
295         struct registry_key *key = NULL;
296         TALLOC_CTX *ctx = talloc_stackframe();
297
298         werr = libnet_smbconf_open_basepath(ctx, REG_KEY_WRITE, &key);
299         if (!W_ERROR_IS_OK(werr)) {
300                 goto done;
301         }
302
303         werr = reg_deletekey_recursive(key, key, servicename);
304
305 done:
306         TALLOC_FREE(ctx);
307         return werr;
308 }
309
310 WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
311                               const char *service,
312                               const char *param,
313                               const char *valstr)
314 {
315         WERROR werr;
316         struct registry_key *key = NULL;
317
318         if (!libnet_smbconf_key_exists(service)) {
319                 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
320                                                              &key);
321         } else {
322                 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
323                                                 &key);
324         }
325         if (!W_ERROR_IS_OK(werr)) {
326                 goto done;
327         }
328
329         werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
330
331 done:
332         TALLOC_FREE(key);
333         return werr;
334 }
335
336 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
337                               const char *service,
338                               const char *param,
339                               struct registry_value **value)
340 {
341         WERROR werr;
342         struct registry_key *key = NULL;
343
344         if (!libnet_smbconf_key_exists(service)) {
345                 werr = WERR_NO_SUCH_SERVICE;
346                 goto done;
347         }
348
349         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
350         if (!W_ERROR_IS_OK(werr)) {
351                 goto done;
352         }
353
354         if (!libnet_smbconf_value_exists(key, param)) {
355                 werr = WERR_INVALID_PARAM;
356                 goto done;
357         }
358
359         werr = reg_queryvalue(mem_ctx, key, param, value);
360
361 done:
362         TALLOC_FREE(key);
363         return werr;
364 }
365
366 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
367                               const char *service,
368                               const char *param)
369 {
370         struct registry_key *key = NULL;
371         WERROR werr = WERR_OK;
372
373         if (!libnet_smbconf_key_exists(service)) {
374                 return WERR_NO_SUCH_SERVICE;
375         }
376
377         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
378         if (!W_ERROR_IS_OK(werr)) {
379                 goto done;
380         }
381
382         if (!libnet_smbconf_value_exists(key, param)) {
383                 werr = WERR_INVALID_PARAM;
384                 goto done;
385         }
386
387         werr = reg_deletevalue(key, param);
388
389 done:
390         TALLOC_FREE(key);
391         return werr;
392 }
393
394
395 /**********************************************************************
396  *
397  * Convenience functions, that are also exportet.
398  *
399  **********************************************************************/
400
401 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
402                                        const char *param,
403                                        const char *val)
404 {
405         return libnet_smbconf_setparm(mem_ctx, GLOBAL_NAME, param, val);
406 }
407