2 * Unix SMB/CIFS implementation.
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/>.
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) {
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);
53 ctx->data[ctx->keyval_count] = talloc_zero(ctx, struct keyval_pair);
54 if (!ctx->data[ctx->keyval_count]) {
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);
61 if (!ctx->data[ctx->keyval_count]->key ||
62 !ctx->data[ctx->keyval_count]->val) {
70 /****************************************************************
71 ****************************************************************/
73 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
74 const char *filename_in,
78 uint8_t *data_in = NULL;
79 uint8_t *data_out = NULL;
80 char *tmp_name = NULL;
83 size_t converted_size;
87 return NT_STATUS_INVALID_PARAMETER;
90 data_in = (uint8_t *)file_load(filename_in, &n, 0, NULL);
92 status = NT_STATUS_NO_SUCH_FILE;
96 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
99 status = NT_STATUS_NO_MEMORY;
103 mask = umask(S_IRWXO | S_IRWXG);
104 tmp_fd = mkstemp(tmp_name);
107 status = NT_STATUS_ACCESS_DENIED;
111 if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
112 (void *)&data_out, &converted_size))
114 status = NT_STATUS_INVALID_BUFFER_SIZE;
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]));
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));
131 if (write(tmp_fd, data_out, converted_size) != converted_size) {
132 status = map_nt_error_from_unix_common(errno);
136 *filename_out = tmp_name;
138 status = NT_STATUS_OK;
145 talloc_free(data_in);
150 /****************************************************************
151 ****************************************************************/
153 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
157 for (i = 0; i < ctx->keyval_count; i++) {
158 if (strcmp(ctx->data[i]->key, key) == 0) {
160 *ret = ctx->data[i]->val;
165 return NT_STATUS_NOT_FOUND;
168 /****************************************************************
169 ****************************************************************/
171 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
176 result = gp_inifile_getstring(ctx,key, &value);
177 if (!NT_STATUS_IS_OK(result)) {
182 *ret = (int)strtol(value, NULL, 10);
187 /****************************************************************
188 ****************************************************************/
190 NTSTATUS gp_inifile_getbool(struct gp_inifile_context *ctx, const char *key, bool *ret)
195 result = gp_inifile_getstring(ctx,key, &value);
196 if (!NT_STATUS_IS_OK(result)) {
200 if (strequal(value, "Yes") ||
201 strequal(value, "True")) {
206 } else if (strequal(value, "No") ||
207 strequal(value, "False")) {
214 return NT_STATUS_NOT_FOUND;
217 /****************************************************************
218 ****************************************************************/
220 NTSTATUS gp_inifile_enum_section(struct gp_inifile_context *ctx,
222 size_t *num_ini_keys,
223 const char ***ini_keys,
224 const char ***ini_values)
228 size_t num_keys = 0, num_vals = 0;
229 const char **keys = NULL;
230 const char **values = NULL;
232 if (section == NULL || num_ini_keys == NULL ||
233 ini_keys == NULL || ini_values == NULL) {
234 return NT_STATUS_INVALID_PARAMETER;
237 for (i = 0; i < ctx->keyval_count; i++) {
243 * KEYNAME:value matches
244 * KEYNAME_OEM:value not
247 if (strlen(section)+1 > strlen(ctx->data[i]->key)) {
251 if (!strnequal(section, ctx->data[i]->key, strlen(section))) {
255 if (ctx->data[i]->key[strlen(section)] != ':') {
259 ok = add_string_to_array(ctx, ctx->data[i]->key, &keys, &num_keys);
261 status = NT_STATUS_NO_MEMORY;
265 ok = add_string_to_array(ctx, ctx->data[i]->val, &values, &num_vals);
267 status = NT_STATUS_NO_MEMORY;
271 if (num_keys != num_vals) {
272 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
277 *num_ini_keys = num_keys;
279 *ini_values = values;
291 /****************************************************************
292 ****************************************************************/
294 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
296 const char *unix_path,
298 struct gp_inifile_context **ctx_ret)
300 struct gp_inifile_context *ctx = NULL;
303 char *tmp_filename = NULL;
304 const char *ini_filename = NULL;
306 if (!unix_path || !ctx_ret) {
307 return NT_STATUS_INVALID_PARAMETER;
310 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
311 NT_STATUS_HAVE_NO_MEMORY(ctx);
313 status = gp_find_file(mem_ctx, flags, unix_path, suffix,
316 if (!NT_STATUS_IS_OK(status)) {
320 status = convert_file_from_ucs2(mem_ctx, ini_filename,
322 if (!NT_STATUS_IS_OK(status)) {
326 rv = pm_process(tmp_filename, change_section, store_keyval_pair, ctx);
328 return NT_STATUS_NO_SUCH_FILE;
332 ctx->generated_filename = tmp_filename;
333 ctx->mem_ctx = mem_ctx;
341 DEBUG(1,("gp_inifile_init_context failed: %s\n",
349 /****************************************************************
350 ****************************************************************/
352 NTSTATUS gp_inifile_init_context_direct(TALLOC_CTX *mem_ctx,
353 const char *unix_path,
354 struct gp_inifile_context **pgp_ctx)
356 struct gp_inifile_context *gp_ctx = NULL;
359 char *tmp_filename = NULL;
361 if (unix_path == NULL || pgp_ctx == NULL) {
362 return NT_STATUS_INVALID_PARAMETER;
365 gp_ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
366 if (gp_ctx == NULL) {
367 return NT_STATUS_NO_MEMORY;
370 status = convert_file_from_ucs2(mem_ctx, unix_path,
372 if (!NT_STATUS_IS_OK(status)) {
376 rv = pm_process(tmp_filename,
381 return NT_STATUS_NO_SUCH_FILE;
384 gp_ctx->generated_filename = tmp_filename;
385 gp_ctx->mem_ctx = mem_ctx;
393 DEBUG(1,("gp_inifile_init_context_direct failed: %s\n",
402 /****************************************************************
403 parse the local gpt.ini file
404 ****************************************************************/
406 #define GPT_INI_SECTION_GENERAL "General"
407 #define GPT_INI_PARAMETER_VERSION "Version"
408 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
410 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
411 const char *filename,
419 struct gp_inifile_context *ctx;
422 return NT_STATUS_INVALID_PARAMETER;
425 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
426 NT_STATUS_HAVE_NO_MEMORY(ctx);
428 rv = pm_process(filename, change_section, store_keyval_pair, ctx);
430 return NT_STATUS_NO_SUCH_FILE;
434 result = gp_inifile_getstring(ctx, GPT_INI_SECTION_GENERAL
435 ":"GPT_INI_PARAMETER_DISPLAYNAME, &name);
436 if (!NT_STATUS_IS_OK(result)) {
437 /* the default domain policy and the default domain controller
438 * policy never have a displayname in their gpt.ini file */
439 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
442 if (name && display_name) {
443 *display_name = talloc_strdup(ctx, name);
444 if (*display_name == NULL) {
445 return NT_STATUS_NO_MEMORY;
449 result = gp_inifile_getint(ctx, GPT_INI_SECTION_GENERAL
450 ":"GPT_INI_PARAMETER_VERSION, &v);
451 if (!NT_STATUS_IS_OK(result)) {
452 DEBUG(10,("parse_gpt_ini: no version\n"));
453 return NT_STATUS_INTERNAL_DB_CORRUPTION;