2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 /****************************************************************
24 ****************************************************************/
26 static int gp_inifile_free_context(struct gp_inifile_context *ctx)
32 if (ctx->generated_filename) {
33 unlink(ctx->generated_filename);
34 ctx->generated_filename = NULL;
38 iniparser_freedict(ctx->dict);
47 /****************************************************************
48 ****************************************************************/
50 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
51 const char *filename_in,
55 uint8 *data_in = NULL;
56 uint8 *data_out = NULL;
57 char *tmp_name = NULL;
62 return NT_STATUS_INVALID_PARAMETER;
65 data_in = (uint8 *)file_load(filename_in, &n, 0);
67 status = NT_STATUS_NO_SUCH_FILE;
71 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
74 status = NT_STATUS_NO_MEMORY;
78 tmp_fd = smb_mkstemp(tmp_name);
80 status = NT_STATUS_ACCESS_DENIED;
84 n = convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX,
85 data_in, n, &data_out, False);
88 status = NT_STATUS_INVALID_BUFFER_SIZE;
93 DEBUG(11,("convert_file_from_ucs2: "
94 "data_out[0]: 0x%x, data_out[1]: 0x%x, data_out[2]: 0x%x\n",
95 data_out[0], data_out[1], data_out[2]));
97 if ((data_out[0] == 0xef) && (data_out[1] == 0xbb) &&
98 (data_out[2] == 0xbf)) {
99 DEBUG(11,("convert_file_from_ucs2: "
100 "%s skipping utf8 BOM\n", tmp_name));
105 if (sys_write(tmp_fd, data_out, n) != n) {
106 status = map_nt_error_from_unix(errno);
110 *filename_out = tmp_name;
112 status = NT_STATUS_OK;
124 /****************************************************************
125 ****************************************************************/
127 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
129 const char *unix_path,
131 struct gp_inifile_context **ctx_ret)
133 struct gp_inifile_context *ctx = NULL;
135 dictionary *dict = NULL;
136 char *tmp_filename = NULL;
137 const char *ini_filename = NULL;
139 if (!unix_path || !ctx_ret) {
140 return NT_STATUS_INVALID_PARAMETER;
143 ctx = TALLOC_ZERO_P(mem_ctx, struct gp_inifile_context);
144 NT_STATUS_HAVE_NO_MEMORY(ctx);
146 talloc_set_destructor(ctx, gp_inifile_free_context);
148 status = gp_find_file(mem_ctx, flags, unix_path, suffix,
151 if (!NT_STATUS_IS_OK(status)) {
155 status = convert_file_from_ucs2(mem_ctx, ini_filename,
157 if (!NT_STATUS_IS_OK(status)) {
161 dict = iniparser_load(tmp_filename);
163 status = NT_STATUS_NO_SUCH_FILE;
167 ctx->generated_filename = tmp_filename;
169 ctx->mem_ctx = mem_ctx;
177 DEBUG(1,("gp_inifile_init_context failed: %s\n",
185 /****************************************************************
186 parse the local gpt.ini file
187 ****************************************************************/
189 #define GPT_INI_SECTION_GENERAL "General"
190 #define GPT_INI_PARAMETER_VERSION "Version"
191 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
193 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
194 const char *filename,
201 dictionary *dict = NULL;
204 return NT_STATUS_INVALID_PARAMETER;
207 dict = iniparser_load(filename);
209 return NT_STATUS_NO_SUCH_FILE;
212 if ((name = iniparser_getstring(dict, GPT_INI_SECTION_GENERAL
213 ":"GPT_INI_PARAMETER_DISPLAYNAME, NULL)) == NULL) {
214 /* the default domain policy and the default domain controller
215 * policy never have a displayname in their gpt.ini file */
216 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
219 if (name && display_name) {
220 *display_name = talloc_strdup(mem_ctx, name);
221 if (*display_name == NULL) {
222 result = NT_STATUS_NO_MEMORY;
227 if ((v = iniparser_getint(dict, GPT_INI_SECTION_GENERAL
228 ":"GPT_INI_PARAMETER_VERSION, Undefined)) == Undefined) {
229 DEBUG(10,("parse_gpt_ini: no version\n"));
230 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
238 result = NT_STATUS_OK;
241 iniparser_freedict(dict);