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
);
38 /****************************************************************
39 ****************************************************************/
41 static bool store_keyval_pair(const char *key
, const char *value
, void *ctx_ptr
)
43 struct gp_inifile_context
*ctx
= (struct gp_inifile_context
*) ctx_ptr
;
44 ctx
->data
= talloc_realloc(ctx
, ctx
->data
, struct keyval_pair
*, ctx
->keyval_count
+1);
45 ctx
->data
[ctx
->keyval_count
] = talloc_zero(ctx
, struct keyval_pair
);
46 ctx
->data
[ctx
->keyval_count
]->key
= talloc_asprintf(ctx
, "%s:%s", ctx
->current_section
, key
);
47 ctx
->data
[ctx
->keyval_count
]->val
= talloc_strdup(ctx
, value
);
52 /****************************************************************
53 ****************************************************************/
55 static NTSTATUS
convert_file_from_ucs2(TALLOC_CTX
*mem_ctx
,
56 const char *filename_in
,
60 uint8_t *data_in
= NULL
;
61 uint8_t *data_out
= NULL
;
62 char *tmp_name
= NULL
;
65 size_t converted_size
;
69 return NT_STATUS_INVALID_PARAMETER
;
72 data_in
= (uint8_t *)file_load(filename_in
, &n
, 0, NULL
);
74 status
= NT_STATUS_NO_SUCH_FILE
;
78 tmp_name
= talloc_asprintf(mem_ctx
, "%s/convert_file_from_ucs2.XXXXXX",
81 status
= NT_STATUS_NO_MEMORY
;
85 mask
= umask(S_IRWXO
| S_IRWXG
);
86 tmp_fd
= mkstemp(tmp_name
);
89 status
= NT_STATUS_ACCESS_DENIED
;
93 if (!convert_string_talloc(mem_ctx
, CH_UTF16LE
, CH_UNIX
, data_in
, n
,
94 (void *)&data_out
, &converted_size
))
96 status
= NT_STATUS_INVALID_BUFFER_SIZE
;
101 DEBUG(11,("convert_file_from_ucs2: "
102 "data_out[0]: 0x%x, data_out[1]: 0x%x, data_out[2]: 0x%x\n",
103 data_out
[0], data_out
[1], data_out
[2]));
105 if ((data_out
[0] == 0xef) && (data_out
[1] == 0xbb) &&
106 (data_out
[2] == 0xbf)) {
107 DEBUG(11,("convert_file_from_ucs2: "
108 "%s skipping utf8 BOM\n", tmp_name
));
113 if (write(tmp_fd
, data_out
, converted_size
) != converted_size
) {
114 status
= map_nt_error_from_unix_common(errno
);
118 *filename_out
= tmp_name
;
120 status
= NT_STATUS_OK
;
127 talloc_free(data_in
);
132 /****************************************************************
133 ****************************************************************/
135 NTSTATUS
gp_inifile_getstring(struct gp_inifile_context
*ctx
, const char *key
, char **ret
)
139 for (i
= 0; i
< ctx
->keyval_count
; i
++) {
140 if (strcmp(ctx
->data
[i
]->key
, key
) == 0) {
141 *ret
= ctx
->data
[i
]->val
;
145 return NT_STATUS_NOT_FOUND
;
148 /****************************************************************
149 ****************************************************************/
151 NTSTATUS
gp_inifile_getint(struct gp_inifile_context
*ctx
, const char *key
, int *ret
)
156 result
= gp_inifile_getstring(ctx
,key
, &value
);
157 if (!NT_STATUS_IS_OK(result
)) {
161 *ret
= (int)strtol(value
, NULL
, 10);
165 /****************************************************************
166 ****************************************************************/
168 NTSTATUS
gp_inifile_init_context(TALLOC_CTX
*mem_ctx
,
170 const char *unix_path
,
172 struct gp_inifile_context
**ctx_ret
)
174 struct gp_inifile_context
*ctx
= NULL
;
177 char *tmp_filename
= NULL
;
178 const char *ini_filename
= NULL
;
180 if (!unix_path
|| !ctx_ret
) {
181 return NT_STATUS_INVALID_PARAMETER
;
184 ctx
= talloc_zero(mem_ctx
, struct gp_inifile_context
);
185 NT_STATUS_HAVE_NO_MEMORY(ctx
);
187 status
= gp_find_file(mem_ctx
, flags
, unix_path
, suffix
,
190 if (!NT_STATUS_IS_OK(status
)) {
194 status
= convert_file_from_ucs2(mem_ctx
, ini_filename
,
196 if (!NT_STATUS_IS_OK(status
)) {
200 rv
= pm_process(tmp_filename
, change_section
, store_keyval_pair
, ctx
);
202 return NT_STATUS_NO_SUCH_FILE
;
206 ctx
->generated_filename
= tmp_filename
;
207 ctx
->mem_ctx
= mem_ctx
;
215 DEBUG(1,("gp_inifile_init_context failed: %s\n",
223 /****************************************************************
224 parse the local gpt.ini file
225 ****************************************************************/
227 #define GPT_INI_SECTION_GENERAL "General"
228 #define GPT_INI_PARAMETER_VERSION "Version"
229 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
231 NTSTATUS
parse_gpt_ini(TALLOC_CTX
*mem_ctx
,
232 const char *filename
,
240 struct gp_inifile_context
*ctx
;
243 return NT_STATUS_INVALID_PARAMETER
;
246 ctx
= talloc_zero(mem_ctx
, struct gp_inifile_context
);
247 NT_STATUS_HAVE_NO_MEMORY(ctx
);
249 rv
= pm_process(filename
, change_section
, store_keyval_pair
, ctx
);
251 return NT_STATUS_NO_SUCH_FILE
;
255 result
= gp_inifile_getstring(ctx
, GPT_INI_SECTION_GENERAL
256 ":"GPT_INI_PARAMETER_DISPLAYNAME
, &name
);
257 if (!NT_STATUS_IS_OK(result
)) {
258 /* the default domain policy and the default domain controller
259 * policy never have a displayname in their gpt.ini file */
260 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename
));
263 if (name
&& display_name
) {
264 *display_name
= talloc_strdup(ctx
, name
);
265 if (*display_name
== NULL
) {
266 return NT_STATUS_NO_MEMORY
;
270 result
= gp_inifile_getint(ctx
, GPT_INI_SECTION_GENERAL
271 ":"GPT_INI_PARAMETER_VERSION
, &v
);
272 if (!NT_STATUS_IS_OK(result
)) {
273 DEBUG(10,("parse_gpt_ini: no version\n"));
274 return NT_STATUS_INTERNAL_DB_CORRUPTION
;