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