Move reg_setvalue_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  * Open a subkey of KEY_SMBCONF (i.e a service)
25  * - variant without error output (q = quiet)-
26  */
27 static WERROR libnet_smbconf_open_path_q(TALLOC_CTX *ctx,
28                                          const char *subkeyname,
29                                          uint32 desired_access,
30                                          struct registry_key **key)
31 {
32         WERROR werr = WERR_OK;
33         char *path = NULL;
34         NT_USER_TOKEN *token;
35
36         if (!(token = registry_create_admin_token(ctx))) {
37                 DEBUG(1, ("Error creating admin token\n"));
38                 goto done;
39         }
40
41         if (subkeyname == NULL) {
42                 path = talloc_strdup(ctx, KEY_SMBCONF);
43         } else {
44                 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
45         }
46
47         werr = reg_open_path(ctx, path, desired_access,
48                              token, key);
49
50 done:
51         TALLOC_FREE(path);
52         return werr;
53 }
54
55 /*
56  * check if a subkey of KEY_SMBCONF of a given name exists
57  */
58 bool libnet_smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
59 {
60         bool ret = False;
61         WERROR werr = WERR_OK;
62         TALLOC_CTX *mem_ctx;
63         struct registry_key *key;
64
65         if (!(mem_ctx = talloc_new(ctx))) {
66                 d_fprintf(stderr, "ERROR: Out of memory...!\n");
67                 goto done;
68         }
69
70         werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
71         if (W_ERROR_IS_OK(werr)) {
72                 ret = True;
73         }
74
75 done:
76         TALLOC_FREE(mem_ctx);
77         return ret;
78 }
79
80 /*
81  * Open a subkey of KEY_SMBCONF (i.e a service)
82  * - variant with error output -
83  */
84 WERROR libnet_smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
85                                 uint32 desired_access,
86                                 struct registry_key **key)
87 {
88         WERROR werr = WERR_OK;
89
90         werr = libnet_smbconf_open_path_q(ctx, subkeyname, desired_access, key);
91         if (!W_ERROR_IS_OK(werr)) {
92                 d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
93                           KEY_SMBCONF,
94                           (subkeyname == NULL) ? "" : subkeyname,
95                           dos_errstr(werr));
96         }
97
98         return werr;
99 }
100
101 /*
102  * open the base key KEY_SMBCONF
103  */
104 WERROR libnet_smbconf_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
105                                     struct registry_key **key)
106 {
107         return libnet_smbconf_open_path(ctx, NULL, desired_access, key);
108 }
109
110 /*
111  * create a subkey of KEY_SMBCONF
112  */
113 WERROR libnet_reg_createkey_internal(TALLOC_CTX *ctx,
114                                      const char * subkeyname,
115                                      struct registry_key **newkey)
116 {
117         WERROR werr = WERR_OK;
118         struct registry_key *create_parent = NULL;
119         TALLOC_CTX *create_ctx;
120         enum winreg_CreateAction action = REG_ACTION_NONE;
121
122         /* create a new talloc ctx for creation. it will hold
123          * the intermediate parent key (SMBCONF) for creation
124          * and will be destroyed when leaving this function... */
125         if (!(create_ctx = talloc_new(ctx))) {
126                 werr = WERR_NOMEM;
127                 goto done;
128         }
129
130         werr = libnet_smbconf_open_basepath(create_ctx, REG_KEY_WRITE, &create_parent);
131         if (!W_ERROR_IS_OK(werr)) {
132                 goto done;
133         }
134
135         werr = reg_createkey(ctx, create_parent, subkeyname,
136                              REG_KEY_WRITE, newkey, &action);
137         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
138                 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
139                 werr = WERR_ALREADY_EXISTS;
140         }
141         if (!W_ERROR_IS_OK(werr)) {
142                 d_fprintf(stderr, "Error creating key %s: %s\n",
143                          subkeyname, dos_errstr(werr));
144         }
145
146 done:
147         TALLOC_FREE(create_ctx);
148         return werr;
149 }
150
151
152 /*
153  * add a value to a key.
154  */
155 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
156                                                    const char *valname,
157                                                    const char *valstr)
158 {
159         struct registry_value val;
160         WERROR werr = WERR_OK;
161         char *subkeyname;
162         const char *canon_valname;
163         const char *canon_valstr;
164
165         if (!lp_canonicalize_parameter_with_value(valname, valstr,
166                                                   &canon_valname,
167                                                   &canon_valstr))
168         {
169                 if (canon_valname == NULL) {
170                         d_fprintf(stderr, "invalid parameter '%s' given\n",
171                                   valname);
172                 } else {
173                         d_fprintf(stderr, "invalid value '%s' given for "
174                                   "parameter '%s'\n", valstr, valname);
175                 }
176                 werr = WERR_INVALID_PARAM;
177                 goto done;
178         }
179
180         ZERO_STRUCT(val);
181
182         val.type = REG_SZ;
183         val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
184         val.v.sz.len = strlen(canon_valstr) + 1;
185
186         if (registry_smbconf_valname_forbidden(canon_valname)) {
187                 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
188                           canon_valname);
189                 werr = WERR_INVALID_PARAM;
190                 goto done;
191         }
192
193         subkeyname = strrchr_m(key->key->name, '\\');
194         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
195                 d_fprintf(stderr, "Invalid registry key '%s' given as "
196                           "smbconf section.\n", key->key->name);
197                 werr = WERR_INVALID_PARAM;
198                 goto done;
199         }
200         subkeyname++;
201         if (!strequal(subkeyname, GLOBAL_NAME) &&
202             lp_parameter_is_global(valname))
203         {
204                 d_fprintf(stderr, "Global paramter '%s' not allowed in "
205                           "service definition ('%s').\n", canon_valname,
206                           subkeyname);
207                 werr = WERR_INVALID_PARAM;
208                 goto done;
209         }
210
211         werr = reg_setvalue(key, canon_valname, &val);
212         if (!W_ERROR_IS_OK(werr)) {
213                 d_fprintf(stderr,
214                           "Error adding value '%s' to "
215                           "key '%s': %s\n",
216                           canon_valname, key->key->name, dos_errstr(werr));
217         }
218
219 done:
220         return werr;
221 }
222
223 static WERROR do_modify_val_config(struct registry_key *key,
224                                    const char *val_name,
225                                    const char *val_data)
226 {
227         struct registry_value val;
228
229         ZERO_STRUCT(val);
230
231         val.type = REG_SZ;
232         val.v.sz.str = CONST_DISCARD(char *, val_data);
233         val.v.sz.len = strlen(val_data) + 1;
234
235         return reg_setvalue(key, val_name, &val);
236 }
237
238 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
239                                        const char *param,
240                                        const char *val)
241 {
242         WERROR werr;
243         struct registry_key *key = NULL;
244
245         if (!lp_include_registry_globals()) {
246                 return WERR_NOT_SUPPORTED;
247         }
248
249         if (!registry_init_regdb()) {
250                 return WERR_REG_IO_FAILURE;
251         }
252
253         if (!libnet_smbconf_key_exists(mem_ctx, GLOBAL_NAME)) {
254                 werr = libnet_reg_createkey_internal(mem_ctx,
255                                                      GLOBAL_NAME, &key);
256         } else {
257                 werr = libnet_smbconf_open_path(mem_ctx,
258                                                 GLOBAL_NAME,
259                                                 REG_KEY_WRITE, &key);
260         }
261
262         if (!W_ERROR_IS_OK(werr)) {
263                 return werr;
264         }
265
266         return do_modify_val_config(key, param, val);
267 }
268
269 static bool libnet_smbconf_value_exists(TALLOC_CTX *ctx,
270                                         struct registry_key *key,
271                                         const char *param)
272 {
273         bool ret = False;
274         WERROR werr = WERR_OK;
275         struct registry_value *value = NULL;
276
277         werr = reg_queryvalue(ctx, key, param, &value);
278         if (W_ERROR_IS_OK(werr)) {
279                 ret = True;
280         }
281
282         TALLOC_FREE(value);
283         return ret;
284 }
285
286 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
287                               const char *service,
288                               const char *param)
289 {
290         struct registry_key *key = NULL;
291         WERROR werr = WERR_OK;
292
293         if (!libnet_smbconf_key_exists(mem_ctx, service)) {
294                 return WERR_NO_SUCH_SERVICE;
295         }
296
297         werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
298         W_ERROR_NOT_OK_RETURN(werr);
299
300         if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
301                 return WERR_INVALID_PARAM;
302         }
303
304         werr = reg_deletevalue(key, param);
305         W_ERROR_NOT_OK_RETURN(werr);
306
307         return WERR_OK;
308 }