lib/param: set default for -valid parameter
[Samba.git] / libgpo / gpo_ini.c
bloba91bb9217ca0ffd407e6c0c9882c2b8f5d801cc3
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
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/>.
21 #include "includes.h"
22 #include "gpo.h"
23 #include "gpo_ini.h"
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) {
36 return false;
38 return true;
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);
49 if (!ctx->data) {
50 return false;
53 ctx->data[ctx->keyval_count] = talloc_zero(ctx, struct keyval_pair);
54 if (!ctx->data[ctx->keyval_count]) {
55 return false;
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) {
63 return false;
66 ctx->keyval_count++;
67 return true;
70 /****************************************************************
71 ****************************************************************/
73 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
74 const char *filename_in,
75 char **filename_out)
77 int tmp_fd = -1;
78 uint8_t *data_in = NULL;
79 uint8_t *data_out = NULL;
80 char *tmp_name = NULL;
81 NTSTATUS status;
82 size_t n = 0;
83 size_t converted_size;
84 mode_t mask;
86 if (!filename_out) {
87 return NT_STATUS_INVALID_PARAMETER;
90 data_in = (uint8_t *)file_load(filename_in, &n, 0, NULL);
91 if (!data_in) {
92 status = NT_STATUS_NO_SUCH_FILE;
93 goto out;
96 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
97 tmpdir());
98 if (!tmp_name) {
99 status = NT_STATUS_NO_MEMORY;
100 goto out;
103 mask = umask(S_IRWXO | S_IRWXG);
104 tmp_fd = mkstemp(tmp_name);
105 umask(mask);
106 if (tmp_fd == -1) {
107 status = NT_STATUS_ACCESS_DENIED;
108 goto out;
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;
115 goto out;
118 /* skip utf8 BOM */
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));
127 data_out += 3;
128 converted_size -= 3;
131 if (write(tmp_fd, data_out, converted_size) != converted_size) {
132 status = map_nt_error_from_unix_common(errno);
133 goto out;
136 *filename_out = tmp_name;
138 status = NT_STATUS_OK;
140 out:
141 if (tmp_fd != -1) {
142 close(tmp_fd);
145 talloc_free(data_in);
147 return status;
150 /****************************************************************
151 ****************************************************************/
153 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
155 int i;
157 for (i = 0; i < ctx->keyval_count; i++) {
158 if (strcmp(ctx->data[i]->key, key) == 0) {
159 if (ret) {
160 *ret = ctx->data[i]->val;
162 return NT_STATUS_OK;
165 return NT_STATUS_NOT_FOUND;
168 /****************************************************************
169 ****************************************************************/
171 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
173 char *value;
174 NTSTATUS result;
176 result = gp_inifile_getstring(ctx,key, &value);
177 if (!NT_STATUS_IS_OK(result)) {
178 return result;
181 if (ret) {
182 *ret = (int)strtol(value, NULL, 10);
184 return NT_STATUS_OK;
187 /****************************************************************
188 ****************************************************************/
190 NTSTATUS gp_inifile_getbool(struct gp_inifile_context *ctx, const char *key, bool *ret)
192 char *value;
193 NTSTATUS result;
195 result = gp_inifile_getstring(ctx,key, &value);
196 if (!NT_STATUS_IS_OK(result)) {
197 return result;
200 if (strequal(value, "Yes")) {
201 if (ret) {
202 *ret = true;
204 return NT_STATUS_OK;
205 } else if (strequal(value, "No")) {
206 if (ret) {
207 *ret = false;
209 return NT_STATUS_OK;
212 return NT_STATUS_NOT_FOUND;
215 /****************************************************************
216 ****************************************************************/
218 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
219 uint32_t flags,
220 const char *unix_path,
221 const char *suffix,
222 struct gp_inifile_context **ctx_ret)
224 struct gp_inifile_context *ctx = NULL;
225 NTSTATUS status;
226 int rv;
227 char *tmp_filename = NULL;
228 const char *ini_filename = NULL;
230 if (!unix_path || !ctx_ret) {
231 return NT_STATUS_INVALID_PARAMETER;
234 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
235 NT_STATUS_HAVE_NO_MEMORY(ctx);
237 status = gp_find_file(mem_ctx, flags, unix_path, suffix,
238 &ini_filename);
240 if (!NT_STATUS_IS_OK(status)) {
241 goto failed;
244 status = convert_file_from_ucs2(mem_ctx, ini_filename,
245 &tmp_filename);
246 if (!NT_STATUS_IS_OK(status)) {
247 goto failed;
250 rv = pm_process(tmp_filename, change_section, store_keyval_pair, ctx);
251 if (!rv) {
252 return NT_STATUS_NO_SUCH_FILE;
256 ctx->generated_filename = tmp_filename;
257 ctx->mem_ctx = mem_ctx;
259 *ctx_ret = ctx;
261 return NT_STATUS_OK;
263 failed:
265 DEBUG(1,("gp_inifile_init_context failed: %s\n",
266 nt_errstr(status)));
268 talloc_free(ctx);
270 return status;
273 /****************************************************************
274 parse the local gpt.ini file
275 ****************************************************************/
277 #define GPT_INI_SECTION_GENERAL "General"
278 #define GPT_INI_PARAMETER_VERSION "Version"
279 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
281 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
282 const char *filename,
283 uint32_t *version,
284 char **display_name)
286 NTSTATUS result;
287 int rv;
288 int v = 0;
289 char *name = NULL;
290 struct gp_inifile_context *ctx;
292 if (!filename) {
293 return NT_STATUS_INVALID_PARAMETER;
296 ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
297 NT_STATUS_HAVE_NO_MEMORY(ctx);
299 rv = pm_process(filename, change_section, store_keyval_pair, ctx);
300 if (!rv) {
301 return NT_STATUS_NO_SUCH_FILE;
305 result = gp_inifile_getstring(ctx, GPT_INI_SECTION_GENERAL
306 ":"GPT_INI_PARAMETER_DISPLAYNAME, &name);
307 if (!NT_STATUS_IS_OK(result)) {
308 /* the default domain policy and the default domain controller
309 * policy never have a displayname in their gpt.ini file */
310 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
313 if (name && display_name) {
314 *display_name = talloc_strdup(ctx, name);
315 if (*display_name == NULL) {
316 return NT_STATUS_NO_MEMORY;
320 result = gp_inifile_getint(ctx, GPT_INI_SECTION_GENERAL
321 ":"GPT_INI_PARAMETER_VERSION, &v);
322 if (!NT_STATUS_IS_OK(result)) {
323 DEBUG(10,("parse_gpt_ini: no version\n"));
324 return NT_STATUS_INTERNAL_DB_CORRUPTION;
327 if (version) {
328 *version = v;
331 talloc_free(ctx);
333 return NT_STATUS_OK;