Make sure to set umask() before calling mkstemp().
[nivanova/samba-autobuild/.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] = 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;
50 }
51
52 /****************************************************************
53 ****************************************************************/
54
55 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
56                                        const char *filename_in,
57                                        char **filename_out)
58 {
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;
67
68         if (!filename_out) {
69                 return NT_STATUS_INVALID_PARAMETER;
70         }
71
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;
76         }
77
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;
83         }
84
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;
91         }
92
93         if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
94                                    (void *)&data_out, &converted_size))
95         {
96                 status = NT_STATUS_INVALID_BUFFER_SIZE;
97                 goto out;
98         }
99
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]));
104
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;
111         }
112
113         if (write(tmp_fd, data_out, converted_size) != converted_size) {
114                 status = map_nt_error_from_unix_common(errno);
115                 goto out;
116         }
117
118         *filename_out = tmp_name;
119
120         status = NT_STATUS_OK;
121
122  out:
123         if (tmp_fd != -1) {
124                 close(tmp_fd);
125         }
126
127         talloc_free(data_in);
128
129         return status;
130 }
131
132 /****************************************************************
133 ****************************************************************/
134
135 NTSTATUS gp_inifile_getstring(struct gp_inifile_context *ctx, const char *key, char **ret)
136 {
137         int i;
138
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;
143                 }
144         }
145         return NT_STATUS_NOT_FOUND;
146 }
147
148 /****************************************************************
149 ****************************************************************/
150
151 NTSTATUS gp_inifile_getint(struct gp_inifile_context *ctx, const char *key, int *ret)
152 {
153         char *value;
154         NTSTATUS result;
155
156         result = gp_inifile_getstring(ctx,key, &value);
157         if (!NT_STATUS_IS_OK(result)) {
158                 return result;
159         }
160
161         *ret = (int)strtol(value, NULL, 10);
162         return NT_STATUS_OK;
163 }
164
165 /****************************************************************
166 ****************************************************************/
167
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)
173 {
174         struct gp_inifile_context *ctx = NULL;
175         NTSTATUS status;
176         int rv;
177         char *tmp_filename = NULL;
178         const char *ini_filename = NULL;
179
180         if (!unix_path || !ctx_ret) {
181                 return NT_STATUS_INVALID_PARAMETER;
182         }
183
184         ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
185         NT_STATUS_HAVE_NO_MEMORY(ctx);
186
187         status = gp_find_file(mem_ctx, flags, unix_path, suffix,
188                               &ini_filename);
189
190         if (!NT_STATUS_IS_OK(status)) {
191                 goto failed;
192         }
193
194         status = convert_file_from_ucs2(mem_ctx, ini_filename,
195                                         &tmp_filename);
196         if (!NT_STATUS_IS_OK(status)) {
197                 goto failed;
198         }
199
200         rv = pm_process(tmp_filename, change_section, store_keyval_pair, ctx);
201         if (!rv) {
202                 return NT_STATUS_NO_SUCH_FILE;
203         }
204
205
206         ctx->generated_filename = tmp_filename;
207         ctx->mem_ctx = mem_ctx;
208
209         *ctx_ret = ctx;
210
211         return NT_STATUS_OK;
212
213  failed:
214
215         DEBUG(1,("gp_inifile_init_context failed: %s\n",
216                 nt_errstr(status)));
217
218         talloc_free(ctx);
219
220         return status;
221 }
222
223 /****************************************************************
224  parse the local gpt.ini file
225 ****************************************************************/
226
227 #define GPT_INI_SECTION_GENERAL "General"
228 #define GPT_INI_PARAMETER_VERSION "Version"
229 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
230
231 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
232                        const char *filename,
233                        uint32_t *version,
234                        char **display_name)
235 {
236         NTSTATUS result;
237         int rv;
238         int v = 0;
239         char *name = NULL;
240         struct gp_inifile_context *ctx;
241
242         if (!filename) {
243                 return NT_STATUS_INVALID_PARAMETER;
244         }
245
246         ctx = talloc_zero(mem_ctx, struct gp_inifile_context);
247         NT_STATUS_HAVE_NO_MEMORY(ctx);
248
249         rv = pm_process(filename, change_section, store_keyval_pair, ctx);
250         if (!rv) {
251                 return NT_STATUS_NO_SUCH_FILE;
252         }
253
254
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));
261         }
262
263         if (name && display_name) {
264                 *display_name = talloc_strdup(ctx, name);
265                 if (*display_name == NULL) {
266                         return NT_STATUS_NO_MEMORY;
267                 }
268         }
269
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;
275         }
276
277         if (version) {
278                 *version = v;
279         }
280
281         talloc_free(ctx);
282
283         return NT_STATUS_OK;
284 }