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/>.
22 #include "lib/util/util_file.h"
25 #include "system/filesys.h"
28 static bool change_section(const char *section
, void *ctx_ptr
)
30 struct gp_inifile_context
*ctx
= (struct gp_inifile_context
*) ctx_ptr
;
32 if (ctx
->current_section
) {
33 talloc_free(ctx
->current_section
);
35 ctx
->current_section
= talloc_strdup(ctx
, section
);
36 if (!ctx
->current_section
) {
42 /****************************************************************
43 ****************************************************************/
45 static bool store_keyval_pair(const char *key
, const char *value
, void *ctx_ptr
)
47 struct gp_inifile_context
*ctx
= (struct gp_inifile_context
*) ctx_ptr
;
49 ctx
->data
= talloc_realloc(ctx
, ctx
->data
, struct keyval_pair
*, ctx
->keyval_count
+1);
54 ctx
->data
[ctx
->keyval_count
] = talloc_zero(ctx
, struct keyval_pair
);
55 if (!ctx
->data
[ctx
->keyval_count
]) {
59 ctx
->data
[ctx
->keyval_count
]->key
= talloc_asprintf(ctx
, "%s:%s", ctx
->current_section
, key
);
60 ctx
->data
[ctx
->keyval_count
]->val
= talloc_strdup(ctx
, value
? value
: "");
62 if (!ctx
->data
[ctx
->keyval_count
]->key
||
63 !ctx
->data
[ctx
->keyval_count
]->val
) {
71 /****************************************************************
72 ****************************************************************/
74 static NTSTATUS
convert_file_from_ucs2(TALLOC_CTX
*mem_ctx
,
75 const char *filename_in
,
79 uint8_t *data_in
= NULL
;
80 uint8_t *data_out
= NULL
;
81 char *tmp_name
= NULL
;
84 size_t converted_size
;
88 return NT_STATUS_INVALID_PARAMETER
;
91 data_in
= (uint8_t *)file_load(filename_in
, &n
, 0, mem_ctx
);
93 status
= NT_STATUS_NO_SUCH_FILE
;
97 DEBUG(11,("convert_file_from_ucs2: "
98 "data_in[0]: 0x%x, data_in[1]: 0x%x, data_in[2]: 0x%x\n",
99 data_in
[0], data_in
[1], data_in
[2]));
101 if ((data_in
[0] != 0xff) || (data_in
[1] != 0xfe) || (data_in
[2] != 0x0d)) {
102 *filename_out
= NULL
;
103 status
= NT_STATUS_OK
;
107 tmp_name
= talloc_asprintf(mem_ctx
, "%s/convert_file_from_ucs2.XXXXXX",
110 status
= NT_STATUS_NO_MEMORY
;
114 mask
= umask(S_IRWXO
| S_IRWXG
);
115 tmp_fd
= mkstemp(tmp_name
);
118 status
= NT_STATUS_ACCESS_DENIED
;
122 if (!convert_string_talloc(mem_ctx
, CH_UTF16LE
, CH_UNIX
, data_in
, n
,
123 &data_out
, &converted_size
))
125 status
= NT_STATUS_INVALID_BUFFER_SIZE
;
129 DEBUG(11,("convert_file_from_ucs2: "
130 "%s skipping utf16-le BOM\n", tmp_name
));
134 if (write(tmp_fd
, data_out
+ 3, converted_size
) != converted_size
) {
135 status
= map_nt_error_from_unix_common(errno
);
139 *filename_out
= tmp_name
;
141 status
= NT_STATUS_OK
;
148 talloc_free(data_in
);
149 talloc_free(data_out
);
154 /****************************************************************
155 ****************************************************************/
157 NTSTATUS
gp_inifile_getstring(struct gp_inifile_context
*ctx
, const char *key
, const char **ret
)
161 for (i
= 0; i
< ctx
->keyval_count
; i
++) {
162 if (strcmp(ctx
->data
[i
]->key
, key
) == 0) {
164 *ret
= ctx
->data
[i
]->val
;
169 return NT_STATUS_NOT_FOUND
;
172 /****************************************************************
173 ****************************************************************/
175 NTSTATUS
gp_inifile_getint(struct gp_inifile_context
*ctx
, const char *key
, int *ret
)
180 result
= gp_inifile_getstring(ctx
,key
, &value
);
181 if (!NT_STATUS_IS_OK(result
)) {
186 *ret
= (int)strtol(value
, NULL
, 10);
191 /****************************************************************
192 ****************************************************************/
194 NTSTATUS
gp_inifile_getbool(struct gp_inifile_context
*ctx
, const char *key
, bool *ret
)
199 result
= gp_inifile_getstring(ctx
,key
, &value
);
200 if (!NT_STATUS_IS_OK(result
)) {
204 if (strequal(value
, "Yes") ||
205 strequal(value
, "True")) {
210 } else if (strequal(value
, "No") ||
211 strequal(value
, "False")) {
218 return NT_STATUS_NOT_FOUND
;
221 /****************************************************************
222 ****************************************************************/
224 NTSTATUS
gp_inifile_enum_section(struct gp_inifile_context
*ctx
,
226 size_t *num_ini_keys
,
227 const char ***ini_keys
,
228 const char ***ini_values
)
232 size_t num_keys
= 0, num_vals
= 0;
233 const char **keys
= NULL
;
234 const char **values
= NULL
;
236 if (section
== NULL
|| num_ini_keys
== NULL
||
237 ini_keys
== NULL
|| ini_values
== NULL
) {
238 return NT_STATUS_INVALID_PARAMETER
;
241 for (i
= 0; i
< ctx
->keyval_count
; i
++) {
247 * KEYNAME:value matches
248 * KEYNAME_OEM:value not
251 if (strlen(section
)+1 > strlen(ctx
->data
[i
]->key
)) {
255 if (!strnequal(section
, ctx
->data
[i
]->key
, strlen(section
))) {
259 if (ctx
->data
[i
]->key
[strlen(section
)] != ':') {
263 ok
= add_string_to_array(ctx
, ctx
->data
[i
]->key
, &keys
, &num_keys
);
265 status
= NT_STATUS_NO_MEMORY
;
269 ok
= add_string_to_array(ctx
, ctx
->data
[i
]->val
, &values
, &num_vals
);
271 status
= NT_STATUS_NO_MEMORY
;
275 if (num_keys
!= num_vals
) {
276 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
281 *num_ini_keys
= num_keys
;
283 *ini_values
= values
;
295 /****************************************************************
296 ****************************************************************/
298 NTSTATUS
gp_inifile_init_context(TALLOC_CTX
*mem_ctx
,
300 const char *unix_path
,
302 struct gp_inifile_context
**ctx_ret
)
304 struct gp_inifile_context
*ctx
= NULL
;
307 char *tmp_filename
= NULL
;
308 const char *ini_filename
= NULL
;
310 if (!unix_path
|| !ctx_ret
) {
311 return NT_STATUS_INVALID_PARAMETER
;
314 ctx
= talloc_zero(mem_ctx
, struct gp_inifile_context
);
315 NT_STATUS_HAVE_NO_MEMORY(ctx
);
317 status
= gp_find_file(mem_ctx
, flags
, unix_path
, suffix
,
320 if (!NT_STATUS_IS_OK(status
)) {
324 status
= convert_file_from_ucs2(mem_ctx
, ini_filename
,
326 if (!NT_STATUS_IS_OK(status
)) {
330 rv
= pm_process(tmp_filename
!= NULL
? tmp_filename
: ini_filename
,
331 change_section
, store_keyval_pair
, ctx
);
333 return NT_STATUS_NO_SUCH_FILE
;
337 ctx
->generated_filename
= tmp_filename
;
338 ctx
->mem_ctx
= mem_ctx
;
346 DEBUG(1,("gp_inifile_init_context failed: %s\n",
354 /****************************************************************
355 ****************************************************************/
357 NTSTATUS
gp_inifile_init_context_direct(TALLOC_CTX
*mem_ctx
,
358 const char *unix_path
,
359 struct gp_inifile_context
**pgp_ctx
)
361 struct gp_inifile_context
*gp_ctx
= NULL
;
364 char *tmp_filename
= NULL
;
366 if (unix_path
== NULL
|| pgp_ctx
== NULL
) {
367 return NT_STATUS_INVALID_PARAMETER
;
370 gp_ctx
= talloc_zero(mem_ctx
, struct gp_inifile_context
);
371 if (gp_ctx
== NULL
) {
372 return NT_STATUS_NO_MEMORY
;
375 status
= convert_file_from_ucs2(mem_ctx
, unix_path
,
377 if (!NT_STATUS_IS_OK(status
)) {
381 rv
= pm_process_with_flags(tmp_filename
!= NULL
? tmp_filename
: unix_path
,
387 return NT_STATUS_NO_SUCH_FILE
;
390 gp_ctx
->generated_filename
= tmp_filename
;
391 gp_ctx
->mem_ctx
= mem_ctx
;
399 DEBUG(1,("gp_inifile_init_context_direct failed: %s\n",
408 /****************************************************************
409 parse the local gpt.ini file
410 ****************************************************************/
412 #define GPT_INI_SECTION_GENERAL "General"
413 #define GPT_INI_PARAMETER_VERSION "Version"
414 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
416 NTSTATUS
parse_gpt_ini(TALLOC_CTX
*mem_ctx
,
417 const char *filename
,
424 const char *name
= NULL
;
425 struct gp_inifile_context
*ctx
;
428 return NT_STATUS_INVALID_PARAMETER
;
431 ctx
= talloc_zero(mem_ctx
, struct gp_inifile_context
);
432 NT_STATUS_HAVE_NO_MEMORY(ctx
);
434 rv
= pm_process(filename
, change_section
, store_keyval_pair
, ctx
);
436 return NT_STATUS_NO_SUCH_FILE
;
440 result
= gp_inifile_getstring(ctx
, GPT_INI_SECTION_GENERAL
441 ":"GPT_INI_PARAMETER_DISPLAYNAME
, &name
);
442 if (!NT_STATUS_IS_OK(result
)) {
443 /* the default domain policy and the default domain controller
444 * policy never have a displayname in their gpt.ini file */
445 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename
));
448 if (name
&& display_name
) {
449 *display_name
= talloc_strdup(ctx
, name
);
450 if (*display_name
== NULL
) {
451 return NT_STATUS_NO_MEMORY
;
455 result
= gp_inifile_getint(ctx
, GPT_INI_SECTION_GENERAL
456 ":"GPT_INI_PARAMETER_VERSION
, &v
);
457 if (!NT_STATUS_IS_OK(result
)) {
458 DEBUG(10,("parse_gpt_ini: no version\n"));
459 return NT_STATUS_INTERNAL_DB_CORRUPTION
;