3ea8006d1e140dd5c088201561a98457482b9967
[samba.git] / libgpo / gpo_ini.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Support
4  *  Copyright (C) Guenther Deschner 2007
5  *  Copyright (C) Wilco Baan Hofman 2009
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 #include "gpo.h"
23 #include "gpo_ini.h"
24 #include "system/filesys.h"
25
26
27 static bool change_section(const char *section, void *ctx_ptr)
28 {
29         struct gp_inifile_context *ctx = (struct gp_inifile_context *) ctx_ptr;
30
31         if (ctx->current_section) {
32                 talloc_free(ctx->current_section);
33         }
34         ctx->current_section = talloc_strdup(ctx, section);
35         if (!ctx->current_section) {
36                 return false;
37         }
38         return true;
39 }
40
41 /****************************************************************
42 ****************************************************************/
43
44 static bool store_keyval_pair(const char *key, const char *value, void *ctx_ptr)
45 {
46         struct gp_inifile_context *ctx = (struct gp_inifile_context *) ctx_ptr;
47
48         ctx->data = talloc_realloc(ctx, ctx->data, struct keyval_pair *, ctx->keyval_count+1);
49         if (!ctx->data) {
50                 return false;
51         }
52
53         ctx->data[ctx->keyval_count] = talloc_zero(ctx, struct keyval_pair);
54         if (!ctx->data[ctx->keyval_count]) {
55                 return false;
56         }
57
58         ctx->data[ctx->keyval_count]->key = talloc_asprintf(ctx, "%s:%s", ctx->current_section, key);
59         ctx->data[ctx->keyval_count]->val = talloc_strdup(ctx, value);
60
61         if (!ctx->data[ctx->keyval_count]->key ||
62             !ctx->data[ctx->keyval_count]->val) {
63                 return false;
64         }
65
66         ctx->keyval_count++;
67         return true;
68 }
69
70 /****************************************************************
71 ****************************************************************/
72
73 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
74                                        const char *filename_in,
75                                        char **filename_out)
76 {
77         int tmp_fd = -1;
78         uint8_t *data_in = NULL;
79         uint8_t *data_out = NULL;
80         char *tmp_name = NULL;
81         NTSTATUS status;
82         size_t n = 0;
83         size_t converted_size;
84         mode_t mask;
85
86         if (!filename_out) {
87                 return NT_STATUS_INVALID_PARAMETER;
88         }
89
90         data_in = (uint8_t *)file_load(filename_in, &n, 0, NULL);
91         if (!data_in) {
92                 status = NT_STATUS_NO_SUCH_FILE;
93                 goto out;
94         }
95
96         tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
97                 tmpdir());
98         if (!tmp_name) {
99                 status = NT_STATUS_NO_MEMORY;
100                 goto out;
101         }
102
103         mask = umask(S_IRWXO | S_IRWXG);
104         tmp_fd = mkstemp(tmp_name);
105         umask(mask);
106         if (tmp_fd == -1) {
107                 status = NT_STATUS_ACCESS_DENIED;
108                 goto out;
109         }
110
111         if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
112                                    (void *)&data_out, &converted_size))
113         {
114                 status = NT_STATUS_INVALID_BUFFER_SIZE;
115                 goto out;
116         }
117
118         /* skip utf8 BOM */
119         DEBUG(11,("convert_file_from_ucs2: "
120                "data_out[0]: 0x%x, data_out[1]: 0x%x, data_out[2]: 0x%x\n",
121                 data_out[0], data_out[1], data_out[2]));
122
123         if ((data_out[0] == 0xef) && (data_out[1] == 0xbb) &&
124             (data_out[2] == 0xbf)) {
125                 DEBUG(11,("convert_file_from_ucs2: "
126                          "%s skipping utf8 BOM\n", tmp_name));
127                 data_out += 3;
128                 converted_size -= 3;
129         }
130
131         if (write(tmp_fd, data_out, converted_size) != converted_size) {
132                 status = map_nt_error_from_unix_common(errno);
133                 goto out;
134         }
135
136         *filename_out = tmp_name;
137
138         status = NT_STATUS_OK;
139
140  out:
141         if (tmp_fd != -1) {
142                 close(tmp_fd);
143         }
144
145         talloc_free(data_in);
146
147         return status;
148 }
149
150 /****************************************************************
151 ****************************************************************/
152
153 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
154 {
155         int i;
156
157         for (i = 0; i < ctx->keyval_count; i++) {
158                 if (strcmp(ctx->data[i]->key, key) == 0) {
159                         if (ret) {
160                                 *ret = ctx->data[i]->val;
161                         }
162                         return NT_STATUS_OK;
163                 }
164         }
165         return NT_STATUS_NOT_FOUND;
166 }
167
168 /****************************************************************
169 ****************************************************************/
170
171 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
172 {
173         char *value;
174         NTSTATUS result;
175
176         result = gp_inifile_getstring(ctx,key, &value);
177         if (!NT_STATUS_IS_OK(result)) {
178                 return result;
179         }
180
181         if (ret) {
182                 *ret = (int)strtol(value, NULL, 10);
183         }
184         return NT_STATUS_OK;
185 }
186
187 /****************************************************************
188 ****************************************************************/
189
190 NTSTATUS gp_inifile_getbool(struct gp_inifile_context *ctx, const char *key, bool *ret)
191 {
192         char *value;
193         NTSTATUS result;
194
195         result = gp_inifile_getstring(ctx,key, &value);
196         if (!NT_STATUS_IS_OK(result)) {
197                 return result;
198         }
199
200         if (strequal(value, "Yes") ||
201             strequal(value, "True")) {
202                 if (ret) {
203                         *ret = true;
204                 }
205                 return NT_STATUS_OK;
206         } else if (strequal(value, "No") ||
207                    strequal(value, "False")) {
208                 if (ret) {
209                         *ret = false;
210                 }
211                 return NT_STATUS_OK;
212         }
213
214         return NT_STATUS_NOT_FOUND;
215 }
216
217 /****************************************************************
218 ****************************************************************/
219
220 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
221                                  uint32_t flags,
222                                  const char *unix_path,
223                                  const char *suffix,
224                                  struct gp_inifile_context **ctx_ret)
225 {
226         struct gp_inifile_context *ctx = NULL;
227         NTSTATUS status;
228         int rv;
229         char *tmp_filename = NULL;
230         const char *ini_filename = NULL;
231
232         if (!unix_path || !ctx_ret) {
233                 return NT_STATUS_INVALID_PARAMETER;
234         }
235
236         ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
237         NT_STATUS_HAVE_NO_MEMORY(ctx);
238
239         status = gp_find_file(mem_ctx, flags, unix_path, suffix,
240                               &ini_filename);
241
242         if (!NT_STATUS_IS_OK(status)) {
243                 goto failed;
244         }
245
246         status = convert_file_from_ucs2(mem_ctx, ini_filename,
247                                         &tmp_filename);
248         if (!NT_STATUS_IS_OK(status)) {
249                 goto failed;
250         }
251
252         rv = pm_process(tmp_filename, change_section, store_keyval_pair, ctx);
253         if (!rv) {
254                 return NT_STATUS_NO_SUCH_FILE;
255         }
256
257
258         ctx->generated_filename = tmp_filename;
259         ctx->mem_ctx = mem_ctx;
260
261         *ctx_ret = ctx;
262
263         return NT_STATUS_OK;
264
265  failed:
266
267         DEBUG(1,("gp_inifile_init_context failed: %s\n",
268                 nt_errstr(status)));
269
270         talloc_free(ctx);
271
272         return status;
273 }
274
275 /****************************************************************
276 ****************************************************************/
277
278 NTSTATUS gp_inifile_init_context_direct(TALLOC_CTX *mem_ctx,
279                                         const char *unix_path,
280                                         struct gp_inifile_context **pgp_ctx)
281 {
282         struct gp_inifile_context *gp_ctx = NULL;
283         NTSTATUS status;
284         int rv;
285         char *tmp_filename = NULL;
286
287         if (unix_path == NULL || pgp_ctx == NULL) {
288                 return NT_STATUS_INVALID_PARAMETER;
289         }
290
291         gp_ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
292         if (gp_ctx == NULL) {
293                 return NT_STATUS_NO_MEMORY;
294         }
295
296         status = convert_file_from_ucs2(mem_ctx, unix_path,
297                                         &tmp_filename);
298         if (!NT_STATUS_IS_OK(status)) {
299                 goto failed;
300         }
301
302         rv = pm_process(tmp_filename,
303                         change_section,
304                         store_keyval_pair,
305                         gp_ctx);
306         if (rv != 0) {
307                 return NT_STATUS_NO_SUCH_FILE;
308         }
309
310         gp_ctx->generated_filename = tmp_filename;
311         gp_ctx->mem_ctx = mem_ctx;
312
313         *pgp_ctx = gp_ctx;
314
315         return NT_STATUS_OK;
316
317  failed:
318
319         DEBUG(1,("gp_inifile_init_context_direct failed: %s\n",
320                 nt_errstr(status)));
321
322         talloc_free(gp_ctx);
323
324         return status;
325 }
326
327
328 /****************************************************************
329  parse the local gpt.ini file
330 ****************************************************************/
331
332 #define GPT_INI_SECTION_GENERAL "General"
333 #define GPT_INI_PARAMETER_VERSION "Version"
334 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
335
336 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
337                        const char *filename,
338                        uint32_t *version,
339                        char **display_name)
340 {
341         NTSTATUS result;
342         int rv;
343         int v = 0;
344         char *name = NULL;
345         struct gp_inifile_context *ctx;
346
347         if (!filename) {
348                 return NT_STATUS_INVALID_PARAMETER;
349         }
350
351         ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
352         NT_STATUS_HAVE_NO_MEMORY(ctx);
353
354         rv = pm_process(filename, change_section, store_keyval_pair, ctx);
355         if (!rv) {
356                 return NT_STATUS_NO_SUCH_FILE;
357         }
358
359
360         result = gp_inifile_getstring(ctx, GPT_INI_SECTION_GENERAL
361                         ":"GPT_INI_PARAMETER_DISPLAYNAME, &name);
362         if (!NT_STATUS_IS_OK(result)) {
363                 /* the default domain policy and the default domain controller
364                  * policy never have a displayname in their gpt.ini file */
365                 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
366         }
367
368         if (name && display_name) {
369                 *display_name = talloc_strdup(ctx, name);
370                 if (*display_name == NULL) {
371                         return NT_STATUS_NO_MEMORY;
372                 }
373         }
374
375         result = gp_inifile_getint(ctx, GPT_INI_SECTION_GENERAL
376                         ":"GPT_INI_PARAMETER_VERSION, &v);
377         if (!NT_STATUS_IS_OK(result)) {
378                 DEBUG(10,("parse_gpt_ini: no version\n"));
379                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
380         }
381
382         if (version) {
383                 *version = v;
384         }
385
386         talloc_free(ctx);
387
388         return NT_STATUS_OK;
389 }