r26316: Use contexts for conversion functions.
[jelmer/samba4-debian.git] / source / lib / registry / patchfile_preg.c
1 /*
2    Unix SMB/CIFS implementation.
3    Reading Registry.pol PReg registry files
4
5    Copyright (C) Wilco Baan Hofman 2006
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/registry/registry.h"
24 #include "lib/registry/patchfile.h"
25 #include "system/filesys.h"
26 #include "pstring.h"
27
28 struct preg_data {
29         int fd;
30 };
31
32 static WERROR preg_read_utf16(int fd, char *c)
33 {
34         uint16_t v;
35
36         if (read(fd, &v, 2) < 2) {
37                 return WERR_GENERAL_FAILURE;
38         }
39         push_codepoint(global_smb_iconv_convenience, c, v);
40         return WERR_OK;
41 }
42
43 /* FIXME These functions need to be implemented */
44 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
45 {
46         struct preg_data *data = (struct preg_data *)_data;
47         return WERR_OK;
48 }
49
50 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
51 {
52         struct preg_data *data = (struct preg_data *)_data;
53         return WERR_OK;
54 }
55
56 static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
57                                       const char *value_name,
58                                       uint32_t value_type, DATA_BLOB value_data)
59 {
60         struct preg_data *data = (struct preg_data *)_data;
61         return WERR_OK;
62 }
63
64 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
65                                       const char *value_name)
66 {
67         struct preg_data *data = (struct preg_data *)_data;
68         return WERR_OK;
69 }
70
71 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
72 {
73         struct preg_data *data = (struct preg_data *)_data;
74         return WERR_OK;
75 }
76
77 static WERROR reg_preg_diff_done(void *_data)
78 {
79         struct preg_data *data = (struct preg_data *)_data;
80
81         close(data->fd);
82         talloc_free(data);
83         return WERR_OK;
84 }
85
86 /**
87  * Save registry diff
88  */
89 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
90                                    struct reg_diff_callbacks **callbacks,
91                                    void **callback_data)
92 {
93         struct preg_data *data;
94         struct {
95                 char hdr[4];
96                 uint32_t version;
97         } preg_header;
98
99
100         data = talloc_zero(ctx, struct preg_data);
101         *callback_data = data;
102
103         if (filename) {
104                 data->fd = open(filename, O_CREAT, 0755);
105                 if (data->fd == -1) {
106                         DEBUG(0, ("Unable to open %s\n", filename));
107                         return WERR_BADFILE;
108                 }
109         } else {
110                 data->fd = STDOUT_FILENO;
111         }
112         snprintf(preg_header.hdr, 4, "PReg");
113         SIVAL(&preg_header, 4, 1);
114         write(data->fd, (uint8_t *)&preg_header,8);
115
116         *callbacks = talloc(ctx, struct reg_diff_callbacks);
117
118         (*callbacks)->add_key = reg_preg_diff_add_key;
119         (*callbacks)->del_key = reg_preg_diff_del_key;
120         (*callbacks)->set_value = reg_preg_diff_set_value;
121         (*callbacks)->del_value = reg_preg_diff_del_value;
122         (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
123         (*callbacks)->done = reg_preg_diff_done;
124
125         return WERR_OK;
126 }
127 /**
128  * Load diff file
129  */
130 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
131                                    const struct reg_diff_callbacks *callbacks,
132                                    void *callback_data)
133 {
134         struct {
135                 char hdr[4];
136                 uint32_t version;
137         } preg_header;
138         pstring buf;
139         char *buf_ptr = buf;
140         TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
141
142
143         /* Read first 8 bytes (the header) */
144         if (read(fd, &preg_header, 8) != 8) {
145                 DEBUG(0, ("Could not read PReg file: %s\n",
146                                 strerror(errno)));
147                 close(fd);
148                 return WERR_GENERAL_FAILURE;
149         }
150         if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
151                 DEBUG(0, ("This file is not a valid preg registry file\n"));
152                 close(fd);
153                 return WERR_GENERAL_FAILURE;
154         }
155         if (preg_header.version > 1) {
156                 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
157         }
158
159         /* Read the entries */
160         while(1) {
161                 char *key, *value_name;
162                 uint32_t value_type, length;
163                 DATA_BLOB data;
164
165                 if (!W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr))) {
166                         break;
167                 }
168                 if (*buf_ptr != '[') {
169                         DEBUG(0, ("Error in PReg file.\n"));
170                         close(fd);
171                         return WERR_GENERAL_FAILURE;
172                 }
173
174                 /* Get the path */
175                 buf_ptr = buf;
176                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
177                        *buf_ptr != ';' && buf_ptr-buf < sizeof(buf)) {
178                         buf_ptr++;
179                 }
180                 key = talloc_asprintf(mem_ctx, "\\%s", buf);
181
182                 /* Get the name */
183                 buf_ptr = buf;
184                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
185                        *buf_ptr != ';' && buf_ptr-buf < sizeof(buf)) {
186                         buf_ptr++;
187                 }
188                 value_name = talloc_strdup(mem_ctx, buf);
189
190                 /* Get the type */
191                 if (read(fd, &value_type, 4) < 4) {
192                         DEBUG(0, ("Error while reading PReg\n"));
193                         close(fd);
194                         return WERR_GENERAL_FAILURE;
195                 }
196                 /* Read past delimiter */
197                 buf_ptr = buf;
198                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
199                     *buf_ptr == ';') && buf_ptr-buf < sizeof(buf)) {
200                         DEBUG(0, ("Error in PReg file.\n"));
201                         close(fd);
202                         return WERR_GENERAL_FAILURE;
203                 }
204                 /* Get data length */
205                 if (read(fd, &length, 4) < 4) {
206                         DEBUG(0, ("Error while reading PReg\n"));
207                         close(fd);
208                         return WERR_GENERAL_FAILURE;
209                 }
210                 /* Read past delimiter */
211                 buf_ptr = buf;
212                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
213                     *buf_ptr == ';') && buf_ptr-buf < sizeof(buf)) {
214                         DEBUG(0, ("Error in PReg file.\n"));
215                         close(fd);
216                         return WERR_GENERAL_FAILURE;
217                 }
218                 /* Get the data */
219                 buf_ptr = buf;
220                 if (length < sizeof(buf) &&
221                     read(fd, buf_ptr, length) != length) {
222                         DEBUG(0, ("Error while reading PReg\n"));
223                         close(fd);
224                         return WERR_GENERAL_FAILURE;
225                 }
226                 data = data_blob_talloc(mem_ctx, buf, length);
227
228                 /* Check if delimiter is in place (whine if it isn't) */
229                 buf_ptr = buf;
230                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
231                     *buf_ptr == ']') && buf_ptr-buf < sizeof(buf)) {
232                         DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
233                                 *buf_ptr, *buf_ptr));
234                 }
235
236                 if (strcasecmp(value_name, "**DelVals") == 0) {
237                         callbacks->del_all_values(callback_data, key);
238                 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
239                         char *p = value_name+6;
240
241                         callbacks->del_value(callback_data, key, p);
242                 } else  if (strcasecmp(value_name, "**DeleteValues") == 0) {
243                         char *p, *q;
244
245                         p = (char *) data.data;
246
247                         while ((q = strchr_m(p, ';'))) {
248                                 *q = '\0';
249                                 q++;
250
251                                 callbacks->del_value(callback_data, key, p);
252
253                                 p = q;
254                         }
255                         callbacks->del_value(callback_data, key, p);
256                 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
257                         char *p, *q, *full_key;
258
259                         p = (char *) data.data;
260
261                         while ((q = strchr_m(p, ';'))) {
262                                 *q = '\0';
263                                 q++;
264
265                                 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
266                                                            key, p);
267                                 callbacks->del_key(callback_data, full_key);
268                                 talloc_free(full_key);
269
270                                 p = q;
271                         }
272                         full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
273                         callbacks->del_key(callback_data, full_key);
274                         talloc_free(full_key);
275                 } else {
276                         callbacks->add_key(callback_data, key);
277                         callbacks->set_value(callback_data, key, value_name,
278                                              value_type, data);
279                 }
280                 talloc_free(key);
281                 talloc_free(value_name);
282                 talloc_free(data.data);
283         }
284         close(fd);
285         return WERR_OK;
286 }