Make libnet_smbconf_value_exists() use talloc_stackframe().
[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_setparm(TALLOC_CTX *mem_ctx,
247                               const char *service,
248                               const char *param,
249                               const char *valstr)
250 {
251         WERROR werr;
252         struct registry_key *key = NULL;
253
254         if (!libnet_smbconf_key_exists(service)) {
255                 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
256                                                              &key);
257         } else {
258                 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
259                                                 &key);
260         }
261         W_ERROR_NOT_OK_RETURN(werr);
262
263         werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
264
265         return werr;
266 }
267
268 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
269                               const char *service,
270                               const char *param,
271                               struct registry_value **value)
272 {
273         WERROR werr;
274         struct registry_key *key = NULL;
275
276         if (!libnet_smbconf_key_exists(service)) {
277                 werr = WERR_NO_SUCH_SERVICE;
278                 goto done;
279         }
280
281         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
282         if (!W_ERROR_IS_OK(werr)) {
283                 goto done;
284         }
285
286         if (!libnet_smbconf_value_exists(key, param)) {
287                 werr = WERR_INVALID_PARAM;
288                 goto done;
289         }
290
291         werr = reg_queryvalue(mem_ctx, key, param, value);
292
293 done:
294         TALLOC_FREE(key);
295         return werr;
296 }
297
298 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
299                               const char *service,
300                               const char *param)
301 {
302         struct registry_key *key = NULL;
303         WERROR werr = WERR_OK;
304
305         if (!libnet_smbconf_key_exists(service)) {
306                 return WERR_NO_SUCH_SERVICE;
307         }
308
309         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
310         W_ERROR_NOT_OK_RETURN(werr);
311
312         if (!libnet_smbconf_value_exists(key, param)) {
313                 return WERR_INVALID_PARAM;
314         }
315
316         werr = reg_deletevalue(key, param);
317
318         return werr;
319 }
320
321
322 /**********************************************************************
323  *
324  * Convenience functions, that are also exportet.
325  *
326  **********************************************************************/
327
328 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
329                                        const char *param,
330                                        const char *val)
331 {
332         return libnet_smbconf_setparm(mem_ctx, GLOBAL_NAME, param, val);
333 }
334