edca85530bf57baf88db253aed657cc164eff8e9
[ira/wip.git] / source3 / libgpo / gpo_ini.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Support
4  *  Copyright (C) Guenther Deschner 2007
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "gpo_ini.h"
22
23 /****************************************************************
24 ****************************************************************/
25
26 static int gp_inifile_free_context(struct gp_inifile_context *ctx)
27 {
28         if (!ctx) {
29                 return 0;
30         }
31
32         if (ctx->generated_filename) {
33                 unlink(ctx->generated_filename);
34                 ctx->generated_filename = NULL;
35         }
36
37         if (ctx->dict) {
38                 iniparser_freedict(ctx->dict);
39                 ctx->dict = NULL;
40         }
41
42         ctx = NULL;
43
44         return 0;
45 }
46
47 /****************************************************************
48 ****************************************************************/
49
50 static NTSTATUS convert_file_from_ucs2(TALLOC_CTX *mem_ctx,
51                                        const char *filename_in,
52                                        char **filename_out)
53 {
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;
61
62         if (!filename_out) {
63                 return NT_STATUS_INVALID_PARAMETER;
64         }
65
66         data_in = (uint8 *)file_load(filename_in, &n, 0, NULL);
67         if (!data_in) {
68                 status = NT_STATUS_NO_SUCH_FILE;
69                 goto out;
70         }
71
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;
77         }
78
79         tmp_fd = smb_mkstemp(tmp_name);
80         if (tmp_fd == -1) {
81                 status = NT_STATUS_ACCESS_DENIED;
82                 goto out;
83         }
84
85         if (!convert_string_talloc(mem_ctx, CH_UTF16LE, CH_UNIX, data_in, n,
86                                    (void **)&data_out, &converted_size, False))
87         {
88                 status = NT_STATUS_INVALID_BUFFER_SIZE;
89                 goto out;
90         }
91
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]));
96
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;
103         }
104
105         if (sys_write(tmp_fd, data_out, converted_size) != converted_size) {
106                 status = map_nt_error_from_unix(errno);
107                 goto out;
108         }
109
110         *filename_out = tmp_name;
111
112         status = NT_STATUS_OK;
113
114  out:
115         if (tmp_fd != -1) {
116                 close(tmp_fd);
117         }
118
119         TALLOC_FREE(data_in);
120
121         return status;
122 }
123
124 /****************************************************************
125 ****************************************************************/
126
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)
132 {
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;
138
139         if (!unix_path || !ctx_ret) {
140                 return NT_STATUS_INVALID_PARAMETER;
141         }
142
143         ctx = TALLOC_ZERO_P(mem_ctx, struct gp_inifile_context);
144         NT_STATUS_HAVE_NO_MEMORY(ctx);
145
146         talloc_set_destructor(ctx, gp_inifile_free_context);
147
148         status = gp_find_file(mem_ctx, flags, unix_path, suffix,
149                               &ini_filename);
150
151         if (!NT_STATUS_IS_OK(status)) {
152                 goto failed;
153         }
154
155         status = convert_file_from_ucs2(mem_ctx, ini_filename,
156                                         &tmp_filename);
157         if (!NT_STATUS_IS_OK(status)) {
158                 goto failed;
159         }
160
161         dict = iniparser_load(tmp_filename);
162         if (!dict) {
163                 status = NT_STATUS_NO_SUCH_FILE;
164                 goto failed;
165         }
166
167         ctx->generated_filename = tmp_filename;
168         ctx->dict = dict;
169         ctx->mem_ctx = mem_ctx;
170
171         *ctx_ret = ctx;
172
173         return NT_STATUS_OK;
174
175  failed:
176
177         DEBUG(1,("gp_inifile_init_context failed: %s\n",
178                 nt_errstr(status)));
179
180         TALLOC_FREE(ctx);
181
182         return status;
183 }
184
185 /****************************************************************
186  parse the local gpt.ini file
187 ****************************************************************/
188
189 #define GPT_INI_SECTION_GENERAL "General"
190 #define GPT_INI_PARAMETER_VERSION "Version"
191 #define GPT_INI_PARAMETER_DISPLAYNAME "displayName"
192
193 NTSTATUS parse_gpt_ini(TALLOC_CTX *mem_ctx,
194                        const char *filename,
195                        uint32_t *version,
196                        char **display_name)
197 {
198         NTSTATUS result;
199         uint32_t v = 0;
200         char *name = NULL;
201         dictionary *dict = NULL;
202
203         if (!filename) {
204                 return NT_STATUS_INVALID_PARAMETER;
205         }
206
207         dict = iniparser_load(filename);
208         if (!dict) {
209                 return NT_STATUS_NO_SUCH_FILE;
210         }
211
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));
217         }
218
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;
224                 }
225         }
226
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;
232         }
233
234         if (version) {
235                 *version = v;
236         }
237
238         result = NT_STATUS_OK;
239  out:
240         if (dict) {
241                 iniparser_freedict(dict);
242         }
243
244         return result;
245 }