s3:smbd: Fix off-by 4 error in wrap protection code in create_outbuf()
[Samba.git] / libgpo / gpo_ini.c
bloba2cb106d73c9ad335852262604aea0805cd23ef6
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 return true;
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);
48 ctx->keyval_count++;
49 return true;
52 /****************************************************************
53 ****************************************************************/
55 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
56 const char *filename_in,
57 char **filename_out)
59 int tmp_fd = -1;
60 uint8_t *data_in = NULL;
61 uint8_t *data_out = NULL;
62 char *tmp_name = NULL;
63 NTSTATUS status;
64 size_t n = 0;
65 size_t converted_size;
66 mode_t mask;
68 if (!filename_out) {
69 return NT_STATUS_INVALID_PARAMETER;
72 data_in = (uint8_t *)file_load(filename_in, &n, 0, NULL);
73 if (!data_in) {
74 status = NT_STATUS_NO_SUCH_FILE;
75 goto out;
78 tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
79 tmpdir());
80 if (!tmp_name) {
81 status = NT_STATUS_NO_MEMORY;
82 goto out;
85 mask = umask(S_IRWXO | S_IRWXG);
86 tmp_fd = mkstemp(tmp_name);
87 umask(mask);
88 if (tmp_fd == -1) {
89 status = NT_STATUS_ACCESS_DENIED;
90 goto out;
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;
97 goto out;
100 /* skip utf8 BOM */
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));
109 data_out += 3;
110 converted_size -= 3;
113 if (write(tmp_fd, data_out, converted_size) != converted_size) {
114 status = map_nt_error_from_unix_common(errno);
115 goto out;
118 *filename_out = tmp_name;
120 status = NT_STATUS_OK;
122 out:
123 if (tmp_fd != -1) {
124 close(tmp_fd);
127 talloc_free(data_in);
129 return status;
132 /****************************************************************
133 ****************************************************************/
135 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
137 int i;
139 for (i = 0; i < ctx->keyval_count; i++) {
140 if (strcmp(ctx->data[i]->key, key) == 0) {
141 *ret = ctx->data[i]->val;
142 return NT_STATUS_OK;
145 return NT_STATUS_NOT_FOUND;
148 /****************************************************************
149 ****************************************************************/
151 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
153 char *value;
154 NTSTATUS result;
156 result = gp_inifile_getstring(ctx,key, &value);
157 if (!NT_STATUS_IS_OK(result)) {
158 return result;
161 *ret = (int)strtol(value, NULL, 10);
162 return NT_STATUS_OK;
165 /****************************************************************
166 ****************************************************************/
168 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
169 uint32_t flags,
170 const char *unix_path,
171 const char *suffix,
172 struct gp_inifile_context **ctx_ret)
174 struct gp_inifile_context *ctx = NULL;
175 NTSTATUS status;
176 int rv;
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,
188 &ini_filename);
190 if (!NT_STATUS_IS_OK(status)) {
191 goto failed;
194 status = convert_file_from_ucs2(mem_ctx, ini_filename,
195 &tmp_filename);
196 if (!NT_STATUS_IS_OK(status)) {
197 goto failed;
200 rv = pm_process(tmp_filename, change_section, store_keyval_pair, ctx);
201 if (!rv) {
202 return NT_STATUS_NO_SUCH_FILE;
206 ctx->generated_filename = tmp_filename;
207 ctx->mem_ctx = mem_ctx;
209 *ctx_ret = ctx;
211 return NT_STATUS_OK;
213 failed:
215 DEBUG(1,("gp_inifile_init_context failed: %s\n",
216 nt_errstr(status)));
218 talloc_free(ctx);
220 return status;
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,
233 uint32_t *version,
234 char **display_name)
236 NTSTATUS result;
237 int rv;
238 int v = 0;
239 char *name = NULL;
240 struct gp_inifile_context *ctx;
242 if (!filename) {
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);
250 if (!rv) {
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;
277 if (version) {
278 *version = v;
281 talloc_free(ctx);
283 return NT_STATUS_OK;