79bf5fd8d83fc90aa44cd175b8463f7707ec24f7
[samba.git] / libgpo / gpo_ini.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Support
4  *  Copyright (C) Guenther Deschner 2007
5  *  Copyright (C) Wilco Baan Hofman 2009
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "includes.h"
22 #include "gpo.h"
23 #include "gpo_ini.h"
24 #include "system/filesys.h"
25
26
27 static bool change_section(const char *section, void *ctx_ptr)
28 {
29         struct gp_inifile_context *ctx = (struct gp_inifile_context *) ctx_ptr;
30
31         if (ctx->current_section) {
32                 talloc_free(ctx->current_section);
33         }
34         ctx->current_section = talloc_strdup(ctx, section);
35         return true;
36 }
37
38 /****************************************************************
39 ****************************************************************/
40
41 static bool store_keyval_pair(const char *key, const char *value, void *ctx_ptr)
42 {
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]->key = talloc_asprintf(ctx, "%s:%s", ctx->current_section, key);
46         ctx->data[ctx->keyval_count]->val = talloc_strdup(ctx, value);
47         ctx->keyval_count++;
48         return true;
49 }
50
51 /****************************************************************
52 ****************************************************************/
53
54 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
55                                        const char *filename_in,
56                                        char **filename_out)
57 {
58         int tmp_fd = -1;
59         uint8_t *data_in = NULL;
60         uint8_t *data_out = NULL;
61         char *tmp_name = NULL;
62         NTSTATUS status;
63         size_t n = 0;
64         size_t converted_size;
65
66         if (!filename_out) {
67                 return NT_STATUS_INVALID_PARAMETER;
68         }
69
70         data_in = (uint8_t *)file_load(filename_in, &n, 0, NULL);
71         if (!data_in) {
72                 status = NT_STATUS_NO_SUCH_FILE;
73                 goto out;
74         }
75
76         tmp_name = talloc_asprintf(mem_ctx, "%s/convert_file_from_ucs2.XXXXXX",
77                 tmpdir());
78         if (!tmp_name) {
79                 status = NT_STATUS_NO_MEMORY;
80                 goto out;
81         }
82
83         tmp_fd = mkstemp(tmp_name);
84         if (tmp_fd == -1) {
85                 status = NT_STATUS_ACCESS_DENIED;
86                 goto out;
87         }
88
89         if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
90                                    (void **)&data_out, &converted_size, false))
91         {
92                 status = NT_STATUS_INVALID_BUFFER_SIZE;
93                 goto out;
94         }
95
96         /* skip utf8 BOM */
97         DEBUG(11,("convert_file_from_ucs2: "
98                "data_out[0]: 0x%x, data_out[1]: 0x%x, data_out[2]: 0x%x\n",
99                 data_out[0], data_out[1], data_out[2]));
100
101         if ((data_out[0] == 0xef) && (data_out[1] == 0xbb) &&
102             (data_out[2] == 0xbf)) {
103                 DEBUG(11,("convert_file_from_ucs2: "
104                          "%s skipping utf8 BOM\n", tmp_name));
105                 data_out += 3;
106                 converted_size -= 3;
107         }
108
109         if (write(tmp_fd, data_out, converted_size) != converted_size) {
110                 status = map_nt_error_from_unix(errno);
111                 goto out;
112         }
113
114         *filename_out = tmp_name;
115
116         status = NT_STATUS_OK;
117
118  out:
119         if (tmp_fd != -1) {
120                 close(tmp_fd);
121         }
122
123         talloc_free(data_in);
124
125         return status;
126 }
127
128 /****************************************************************
129 ****************************************************************/
130
131 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
132 {
133         int i;
134
135         for (i = 0; i < ctx->keyval_count; i++) {
136                 if (strcmp(ctx->data[i]->key, key) == 0) {
137                         *ret = ctx->data[i]->val;
138                         return NT_STATUS_OK;
139                 }
140         }
141         return NT_STATUS_NOT_FOUND;
142 }
143
144 /****************************************************************
145 ****************************************************************/
146
147 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
148 {
149         char *value;
150         NTSTATUS result;
151
152         result = gp_inifile_getstring(ctx,key, &value);
153         if (!NT_STATUS_IS_OK(result)) {
154                 return result;
155         }
156
157         *ret = (int)strtol(value, NULL, 10);
158         return NT_STATUS_OK;
159 }
160
161 /****************************************************************
162 ****************************************************************/
163
164 NTSTATUS gp_inifile_init_context(TALLOC_CTX *mem_ctx,
165                                  uint32_t flags,
166                                  const char *unix_path,
167                                  const char *suffix,
168                                  struct gp_inifile_context **ctx_ret)
169 {
170         struct gp_inifile_context *ctx = NULL;
171         NTSTATUS status;
172         char *tmp_filename = NULL;
173         const char *ini_filename = NULL;
174
175         if (!unix_path || !ctx_ret) {
176                 return NT_STATUS_INVALID_PARAMETER;
177         }
178
179         ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
180         NT_STATUS_HAVE_NO_MEMORY(ctx);
181
182         status = gp_find_file(mem_ctx, flags, unix_path, suffix,
183                               &ini_filename);
184
185         if (!NT_STATUS_IS_OK(status)) {
186                 goto failed;
187         }
188
189         status = convert_file_from_ucs2(mem_ctx, ini_filename,
190                                         &tmp_filename);
191         if (!NT_STATUS_IS_OK(status)) {
192                 goto failed;
193         }
194
195         ctx->generated_filename = tmp_filename;
196         ctx->mem_ctx = mem_ctx;
197
198         *ctx_ret = ctx;
199
200         return NT_STATUS_OK;
201
202  failed:
203
204         DEBUG(1,("gp_inifile_init_context failed: %s\n",
205                 nt_errstr(status)));
206
207         talloc_free(ctx);
208
209         return status;
210 }
211
212 /****************************************************************
213  parse the local gpt.ini file
214 ****************************************************************/
215
216 #define GPT_INI_SECTION_GENERAL "General"
217 #define GPT_INI_PARAMETER_VERSION "Version"
218 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
219
220 NTSTATUS parse_gpt_ini(struct gp_inifile_context *ctx,
221                        const char *filename,
222                        uint32_t *version,
223                        char **display_name)
224 {
225         NTSTATUS result;
226         int rv;
227         int v = 0;
228         char *name = NULL;
229
230         if (!filename) {
231                 return NT_STATUS_INVALID_PARAMETER;
232         }
233
234         rv = pm_process(filename, change_section, store_keyval_pair, NULL);
235         if (!rv) {
236                 return NT_STATUS_NO_SUCH_FILE;
237         }
238
239
240         result = gp_inifile_getstring(ctx, GPT_INI_SECTION_GENERAL
241                         ":"GPT_INI_PARAMETER_DISPLAYNAME, &name);
242         if (!NT_STATUS_IS_OK(result)) {
243                 /* the default domain policy and the default domain controller
244                  * policy never have a displayname in their gpt.ini file */
245                 DEBUG(10,("parse_gpt_ini: no name in %s\n", filename));
246         }
247
248         if (name && display_name) {
249                 *display_name = talloc_strdup(ctx, name);
250                 if (*display_name == NULL) {
251                         return NT_STATUS_NO_MEMORY;
252                 }
253         }
254
255         result = gp_inifile_getint(ctx, GPT_INI_SECTION_GENERAL
256                         ":"GPT_INI_PARAMETER_VERSION, &v);
257         if (!NT_STATUS_IS_OK(result)) {
258                 DEBUG(10,("parse_gpt_ini: no version\n"));
259                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
260         }
261
262         if (version) {
263                 *version = v;
264         }
265
266         result = NT_STATUS_OK;
267
268         return result;
269 }