util: Add cmocka unit test for directory_create_or_exists
[Samba.git] / libgpo / gpo_ini.c
blobc1b1698b184d82c9ad08739980c94fba737443b3
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
4 * Copyright (C) Guenther Deschner 2007
5 * Copyright (C) Wilco Baan Hofman 2009
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.
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.
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/>.
21 #include "includes.h"
22 #include "gpo.h"
23 #include "gpo_ini.h"
24 #include "system/filesys.h"
27 static bool change_section(const char *section, void *ctx_ptr)
29 struct gp_inifile_context *ctx = (struct gp_inifile_context *) ctx_ptr;
31 if (ctx->current_section) {
32 talloc_free(ctx->current_section);
34 ctx->current_section = talloc_strdup(ctx, section);
35 if (!ctx->current_section) {
36 return false;
38 return true;
41 /****************************************************************
42 ****************************************************************/
44 static bool store_keyval_pair(const char *key, const char *value, void *ctx_ptr)
46 struct gp_inifile_context *ctx = (struct gp_inifile_context *) ctx_ptr;
48 ctx->data = talloc_realloc(ctx, ctx->data, struct keyval_pair *, ctx->keyval_count+1);
49 if (!ctx->data) {
50 return false;
53 ctx->data[ctx->keyval_count] = talloc_zero(ctx, struct keyval_pair);
54 if (!ctx->data[ctx->keyval_count]) {
55 return false;
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 ? value : "");
61 if (!ctx->data[ctx->keyval_count]->key ||
62 !ctx->data[ctx->keyval_count]->val) {
63 return false;
66 ctx->keyval_count++;
67 return true;
70 /****************************************************************
71 ****************************************************************/
73 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
74 const char *filename_in,
75 char **filename_out)
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;
86 if (!filename_out) {
87 return NT_STATUS_INVALID_PARAMETER;
90 data_in = (uint8_t *)file_load(filename_in, &n, 0, mem_ctx);
91 if (!data_in) {
92 status = NT_STATUS_NO_SUCH_FILE;
93 goto out;
96 DEBUG(11,("convert_file_from_ucs2: "
97 "data_in[0]: 0x%x, data_in[1]: 0x%x, data_in[2]: 0x%x\n",
98 data_in[0], data_in[1], data_in[2]));
100 if ((data_in[0] != 0xff) || (data_in[1] != 0xfe) || (data_in[2] != 0x0d)) {
101 *filename_out = NULL;
102 status = NT_STATUS_OK;
103 goto out;
106 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
107 tmpdir());
108 if (!tmp_name) {
109 status = NT_STATUS_NO_MEMORY;
110 goto out;
113 mask = umask(S_IRWXO | S_IRWXG);
114 tmp_fd = mkstemp(tmp_name);
115 umask(mask);
116 if (tmp_fd == -1) {
117 status = NT_STATUS_ACCESS_DENIED;
118 goto out;
121 if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
122 (void *)&data_out, &converted_size))
124 status = NT_STATUS_INVALID_BUFFER_SIZE;
125 goto out;
128 DEBUG(11,("convert_file_from_ucs2: "
129 "%s skipping utf16-le BOM\n", tmp_name));
131 converted_size -= 3;
133 if (write(tmp_fd, data_out + 3, converted_size) != converted_size) {
134 status = map_nt_error_from_unix_common(errno);
135 goto out;
138 *filename_out = tmp_name;
140 status = NT_STATUS_OK;
142 out:
143 if (tmp_fd != -1) {
144 close(tmp_fd);
147 talloc_free(data_in);
148 talloc_free(data_out);
150 return status;
153 /****************************************************************
154 ****************************************************************/
156 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, const char **ret)
158 int i;
160 for (i = 0; i < ctx->keyval_count; i++) {
161 if (strcmp(ctx->data[i]->key, key) == 0) {
162 if (ret) {
163 *ret = ctx->data[i]->val;
165 return NT_STATUS_OK;
168 return NT_STATUS_NOT_FOUND;
171 /****************************************************************
172 ****************************************************************/
174 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
176 const char *value;
177 NTSTATUS result;
179 result = gp_inifile_getstring(ctx,key, &value);
180 if (!NT_STATUS_IS_OK(result)) {
181 return result;
184 if (ret) {
185 *ret = (int)strtol(value, NULL, 10);
187 return NT_STATUS_OK;
190 /****************************************************************
191 ****************************************************************/
193 NTSTATUS gp_inifile_getbool(struct gp_inifile_context *ctx, const char *key, bool *ret)
195 const char *value;
196 NTSTATUS result;
198 result = gp_inifile_getstring(ctx,key, &value);
199 if (!NT_STATUS_IS_OK(result)) {
200 return result;
203 if (strequal(value, "Yes") ||
204 strequal(value, "True")) {
205 if (ret) {
206 *ret = true;
208 return NT_STATUS_OK;
209 } else if (strequal(value, "No") ||
210 strequal(value, "False")) {
211 if (ret) {
212 *ret = false;
214 return NT_STATUS_OK;
217 return NT_STATUS_NOT_FOUND;
220 /****************************************************************
221 ****************************************************************/
223 NTSTATUS gp_inifile_enum_section(struct gp_inifile_context *ctx,
224 const char *section,
225 size_t *num_ini_keys,
226 const char ***ini_keys,
227 const char ***ini_values)
229 NTSTATUS status;
230 int i;
231 size_t num_keys = 0, num_vals = 0;
232 const char **keys = NULL;
233 const char **values = NULL;
235 if (section == NULL || num_ini_keys == NULL ||
236 ini_keys == NULL || ini_values == NULL) {
237 return NT_STATUS_INVALID_PARAMETER;
240 for (i = 0; i < ctx->keyval_count; i++) {
242 bool ok;
245 * section: KEYNAME
246 * KEYNAME:value matches
247 * KEYNAME_OEM:value not
250 if (strlen(section)+1 > strlen(ctx->data[i]->key)) {
251 continue;
254 if (!strnequal(section, ctx->data[i]->key, strlen(section))) {
255 continue;
258 if (ctx->data[i]->key[strlen(section)] != ':') {
259 continue;
262 ok = add_string_to_array(ctx, ctx->data[i]->key, &keys, &num_keys);
263 if (!ok) {
264 status = NT_STATUS_NO_MEMORY;
265 goto failed;
268 ok = add_string_to_array(ctx, ctx->data[i]->val, &values, &num_vals);
269 if (!ok) {
270 status = NT_STATUS_NO_MEMORY;
271 goto failed;
274 if (num_keys != num_vals) {
275 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
276 goto failed;
280 *num_ini_keys = num_keys;
281 *ini_keys = keys;
282 *ini_values = values;
284 return NT_STATUS_OK;
286 failed:
287 talloc_free(keys);
288 talloc_free(values);
290 return status;
294 /****************************************************************
295 ****************************************************************/
297 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
298 uint32_t flags,
299 const char *unix_path,
300 const char *suffix,
301 struct gp_inifile_context **ctx_ret)
303 struct gp_inifile_context *ctx = NULL;
304 NTSTATUS status;
305 int rv;
306 char *tmp_filename = NULL;
307 const char *ini_filename = NULL;
309 if (!unix_path || !ctx_ret) {
310 return NT_STATUS_INVALID_PARAMETER;
313 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
314 NT_STATUS_HAVE_NO_MEMORY(ctx);
316 status = gp_find_file(mem_ctx, flags, unix_path, suffix,
317 &ini_filename);
319 if (!NT_STATUS_IS_OK(status)) {
320 goto failed;
323 status = convert_file_from_ucs2(mem_ctx, ini_filename,
324 &tmp_filename);
325 if (!NT_STATUS_IS_OK(status)) {
326 goto failed;
329 rv = pm_process(tmp_filename != NULL ? tmp_filename : ini_filename,
330 change_section, store_keyval_pair, ctx);
331 if (!rv) {
332 return NT_STATUS_NO_SUCH_FILE;
336 ctx->generated_filename = tmp_filename;
337 ctx->mem_ctx = mem_ctx;
339 *ctx_ret = ctx;
341 return NT_STATUS_OK;
343 failed:
345 DEBUG(1,("gp_inifile_init_context failed: %s\n",
346 nt_errstr(status)));
348 talloc_free(ctx);
350 return status;
353 /****************************************************************
354 ****************************************************************/
356 NTSTATUS gp_inifile_init_context_direct(TALLOC_CTX *mem_ctx,
357 const char *unix_path,
358 struct gp_inifile_context **pgp_ctx)
360 struct gp_inifile_context *gp_ctx = NULL;
361 NTSTATUS status;
362 bool rv;
363 char *tmp_filename = NULL;
365 if (unix_path == NULL || pgp_ctx == NULL) {
366 return NT_STATUS_INVALID_PARAMETER;
369 gp_ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
370 if (gp_ctx == NULL) {
371 return NT_STATUS_NO_MEMORY;
374 status = convert_file_from_ucs2(mem_ctx, unix_path,
375 &tmp_filename);
376 if (!NT_STATUS_IS_OK(status)) {
377 goto failed;
380 rv = pm_process_with_flags(tmp_filename != NULL ? tmp_filename : unix_path,
381 true,
382 change_section,
383 store_keyval_pair,
384 gp_ctx);
385 if (!rv) {
386 return NT_STATUS_NO_SUCH_FILE;
389 gp_ctx->generated_filename = tmp_filename;
390 gp_ctx->mem_ctx = mem_ctx;
392 *pgp_ctx = gp_ctx;
394 return NT_STATUS_OK;
396 failed:
398 DEBUG(1,("gp_inifile_init_context_direct failed: %s\n",
399 nt_errstr(status)));
401 talloc_free(gp_ctx);
403 return status;
407 /****************************************************************
408 parse the local gpt.ini file
409 ****************************************************************/
411 #define GPT_INI_SECTION_GENERAL "General"
412 #define GPT_INI_PARAMETER_VERSION "Version"
413 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
415 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
416 const char *filename,
417 uint32_t *version,
418 char **display_name)
420 NTSTATUS result;
421 int rv;
422 int v = 0;
423 const char *name = NULL;
424 struct gp_inifile_context *ctx;
426 if (!filename) {
427 return NT_STATUS_INVALID_PARAMETER;
430 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
431 NT_STATUS_HAVE_NO_MEMORY(ctx);
433 rv = pm_process(filename, change_section, store_keyval_pair, ctx);
434 if (!rv) {
435 return NT_STATUS_NO_SUCH_FILE;
439 result = gp_inifile_getstring(ctx, GPT_INI_SECTION_GENERAL
440 ":"GPT_INI_PARAMETER_DISPLAYNAME, &name);
441 if (!NT_STATUS_IS_OK(result)) {
442 /* the default domain policy and the default domain controller
443 * policy never have a displayname in their gpt.ini file */
444 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
447 if (name && display_name) {
448 *display_name = talloc_strdup(ctx, name);
449 if (*display_name == NULL) {
450 return NT_STATUS_NO_MEMORY;
454 result = gp_inifile_getint(ctx, GPT_INI_SECTION_GENERAL
455 ":"GPT_INI_PARAMETER_VERSION, &v);
456 if (!NT_STATUS_IS_OK(result)) {
457 DEBUG(10,("parse_gpt_ini: no version\n"));
458 return NT_STATUS_INTERNAL_DB_CORRUPTION;
461 if (version) {
462 *version = v;
465 talloc_free(ctx);
467 return NT_STATUS_OK;