netapi: implement NetFileClose_r.
[Samba.git] / source / libgpo / gpo_ini.c
blob54aaffa4776be872e18328bbb6338c7fe1f8d284
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
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/>.
20 #include "includes.h"
21 #include "gpo_ini.h"
23 /****************************************************************
24 ****************************************************************/
26 static int gp_inifile_free_context(struct gp_inifile_context *ctx)
28 if (!ctx) {
29 return 0;
32 if (ctx->generated_filename) {
33 unlink(ctx->generated_filename);
34 ctx->generated_filename = NULL;
37 if (ctx->dict) {
38 iniparser_freedict(ctx->dict);
39 ctx->dict = NULL;
42 ctx = NULL;
44 return 0;
47 /****************************************************************
48 ****************************************************************/
50 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
51 const char *filename_in,
52 char **filename_out)
54 int tmp_fd = -1;
55 uint8 *data_in = NULL;
56 uint8 *data_out = NULL;
57 char *tmp_name = NULL;
58 NTSTATUS status;
59 size_t n = 0;
60 size_t converted_size;
62 if (!filename_out) {
63 return NT_STATUS_INVALID_PARAMETER;
66 data_in = (uint8 *)file_load(filename_in, &n, 0);
67 if (!data_in) {
68 status = NT_STATUS_NO_SUCH_FILE;
69 goto out;
72 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
73 tmpdir());
74 if (!tmp_name) {
75 status = NT_STATUS_NO_MEMORY;
76 goto out;
79 tmp_fd = smb_mkstemp(tmp_name);
80 if (tmp_fd == -1) {
81 status = NT_STATUS_ACCESS_DENIED;
82 goto out;
85 if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
86 &data_out, &converted_size, False))
88 status = NT_STATUS_INVALID_BUFFER_SIZE;
89 goto out;
92 /* skip utf8 BOM */
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));
101 data_out += 3;
102 converted_size -= 3;
105 if (sys_write(tmp_fd, data_out, converted_size) != converted_size) {
106 status = map_nt_error_from_unix(errno);
107 goto out;
110 *filename_out = tmp_name;
112 status = NT_STATUS_OK;
114 out:
115 if (tmp_fd != -1) {
116 close(tmp_fd);
119 SAFE_FREE(data_in);
121 return status;
124 /****************************************************************
125 ****************************************************************/
127 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
128 uint32_t flags,
129 const char *unix_path,
130 const char *suffix,
131 struct gp_inifile_context **ctx_ret)
133 struct gp_inifile_context *ctx = NULL;
134 NTSTATUS status;
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,
149 &ini_filename);
151 if (!NT_STATUS_IS_OK(status)) {
152 goto failed;
155 status = convert_file_from_ucs2(mem_ctx, ini_filename,
156 &tmp_filename);
157 if (!NT_STATUS_IS_OK(status)) {
158 goto failed;
161 dict = iniparser_load(tmp_filename);
162 if (!dict) {
163 status = NT_STATUS_NO_SUCH_FILE;
164 goto failed;
167 ctx->generated_filename = tmp_filename;
168 ctx->dict = dict;
169 ctx->mem_ctx = mem_ctx;
171 *ctx_ret = ctx;
173 return NT_STATUS_OK;
175 failed:
177 DEBUG(1,("gp_inifile_init_context failed: %s\n",
178 nt_errstr(status)));
180 TALLOC_FREE(ctx);
182 return status;
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,
195 uint32_t *version,
196 char **display_name)
198 NTSTATUS result;
199 uint32_t v = 0;
200 char *name = NULL;
201 dictionary *dict = NULL;
203 if (!filename) {
204 return NT_STATUS_INVALID_PARAMETER;
207 dict = iniparser_load(filename);
208 if (!dict) {
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;
223 goto out;
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;
231 goto out;
234 if (version) {
235 *version = v;
238 result = NT_STATUS_OK;
239 out:
240 if (dict) {
241 iniparser_freedict(dict);
244 return result;