r25000: Fix some more C++ compatibility warnings.
[jra/samba/.git] / source4 / 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(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, const char *value_name, uint32_t value_type, DATA_BLOB value_data)
57 {
58         struct preg_data *data = (struct preg_data *)_data;
59         return WERR_OK;
60 }
61
62 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name, const char *value_name)
63 {
64         struct preg_data *data = (struct preg_data *)_data;
65         return WERR_OK;
66 }
67
68 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
69 {
70         struct preg_data *data = (struct preg_data *)_data;
71         return WERR_OK;
72 }
73
74 static WERROR reg_preg_diff_done(void *_data)
75 {
76         struct preg_data *data = (struct preg_data *)_data;
77
78         close(data->fd);
79         talloc_free(data);
80         return WERR_OK;
81 }
82
83 /**
84  * Save registry diff
85  */
86 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename, struct reg_diff_callbacks **callbacks, void **callback_data)
87 {
88         struct preg_data *data;
89         struct {
90                 char hdr[4];
91                 uint32_t version;
92         } preg_header;
93                 
94
95         data = talloc_zero(ctx, struct preg_data);
96         *callback_data = data;
97
98         if (filename) {
99                 data->fd = open(filename, O_CREAT, 0755);
100                 if (data->fd == -1) {
101                         DEBUG(0, ("Unable to open %s\n", filename));
102                         return WERR_BADFILE;
103                 }
104         } else {
105                 data->fd = STDOUT_FILENO;
106         }
107         snprintf(preg_header.hdr, 4, "PReg");
108         SIVAL(&preg_header, 4, 1);
109         write(data->fd, (uint8_t *)&preg_header,8); 
110
111         *callbacks = talloc(ctx, struct reg_diff_callbacks);
112         
113         (*callbacks)->add_key = reg_preg_diff_add_key;
114         (*callbacks)->del_key = reg_preg_diff_del_key;
115         (*callbacks)->set_value = reg_preg_diff_set_value;
116         (*callbacks)->del_value = reg_preg_diff_del_value;
117         (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
118         (*callbacks)->done = reg_preg_diff_done;
119         
120         return WERR_OK;
121 }
122 /**
123  * Load diff file
124  */
125 _PUBLIC_ WERROR reg_preg_diff_load(int fd, const struct reg_diff_callbacks *callbacks, void *callback_data)
126 {
127         struct {
128                 char hdr[4];
129                 uint32_t version;
130         } preg_header;
131         pstring buf;
132         char *buf_ptr = buf;
133         TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
134
135         
136         /* Read first 8 bytes (the header) */
137         if (read(fd, &preg_header, 8) != 8) {
138                 DEBUG(0, ("Could not read PReg file: %s\n",
139                                 strerror(errno)));
140                 close(fd);
141                 return WERR_GENERAL_FAILURE;
142         }
143         if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
144                 DEBUG(0, ("This file is not a valid preg registry file\n"));
145                 close(fd);
146                 return WERR_GENERAL_FAILURE;
147         }
148         if (preg_header.version > 1) {
149                 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
150         }               
151
152         /* Read the entries */
153         while(1) {
154                 char *key, *value_name;
155                 uint32_t value_type, length;
156                 DATA_BLOB data;
157                 
158                 if (!W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr))) {
159                         break;
160                 }
161                 if (*buf_ptr != '[') {
162                         DEBUG(0, ("Error in PReg file.\n"));
163                         close(fd);
164                         return WERR_GENERAL_FAILURE;
165                 }
166         
167                 /* Get the path */
168                 buf_ptr = buf;
169                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) && *buf_ptr != ';' && buf_ptr-buf < sizeof(buf)) { 
170                         buf_ptr++;
171                 }
172                 key = talloc_asprintf(mem_ctx, "\\%s", buf);
173         
174                 /* Get the name */
175                 buf_ptr = buf;
176                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) && *buf_ptr != ';' && buf_ptr-buf < sizeof(buf)) { 
177                         buf_ptr++;
178                 }
179                 value_name = talloc_strdup(mem_ctx, buf);
180
181                 /* Get the type */
182                 if (read(fd, &value_type, 4) < 4) {
183                         DEBUG(0, ("Error while reading PReg\n"));
184                         close(fd);
185                         return WERR_GENERAL_FAILURE;
186                 }
187                 /* Read past delimiter */
188                 buf_ptr = buf;
189                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) && *buf_ptr == ';') && buf_ptr-buf < sizeof(buf)) {
190                         DEBUG(0, ("Error in PReg file.\n"));
191                         close(fd);
192                         return WERR_GENERAL_FAILURE;
193                 }
194                 /* Get data length */
195                 if (read(fd, &length, 4) < 4) {
196                         DEBUG(0, ("Error while reading PReg\n"));
197                         close(fd);
198                         return WERR_GENERAL_FAILURE;
199                 }
200                 /* Read past delimiter */
201                 buf_ptr = buf;
202                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) && *buf_ptr == ';') && buf_ptr-buf < sizeof(buf)) {
203                         DEBUG(0, ("Error in PReg file.\n"));
204                         close(fd);
205                         return WERR_GENERAL_FAILURE;
206                 }       
207                 /* Get the data */
208                 buf_ptr = buf;
209                 if (length < sizeof(buf) && read(fd, buf_ptr, length) != length) {
210                         DEBUG(0, ("Error while reading PReg\n"));
211                         close(fd);
212                         return WERR_GENERAL_FAILURE;
213                 }
214                 data.length = length;
215                 data.data = talloc_memdup(mem_ctx, buf, length);
216                 
217                 /* Check if delimiter is in place (whine if it isn't) */
218                 buf_ptr = buf;
219                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) && *buf_ptr == ']') && buf_ptr-buf < sizeof(buf)) {
220                         DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",*buf_ptr, *buf_ptr));
221                 }
222
223                 if (strcasecmp(value_name, "**DelVals") == 0) {
224                         callbacks->del_all_values(callback_data, key);
225                 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
226                         char *p = value_name+6;
227                         
228                         callbacks->del_value(callback_data, key, p);
229                 } else  if (strcasecmp(value_name, "**DeleteValues") == 0) {
230                         char *p, *q;
231
232                         p = (char *) data.data;
233                         
234                         while ((q = strchr_m(p, ';'))) {
235                                 *q = '\0'; 
236                                 q++;
237
238                                 callbacks->del_value(callback_data, key, p);
239
240                                 p = q;
241                         }
242                         callbacks->del_value(callback_data, key, p);
243                 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
244                         char *p, *q, *full_key;
245
246                         p = (char *) data.data;
247                         
248                         while ((q = strchr_m(p, ';'))) {
249                                 *q = '\0';
250                                 q++;
251         
252                                 full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
253                                 callbacks->del_key(callback_data, full_key);
254                                 talloc_free(full_key);
255
256                                 p = q; 
257                         }
258                         full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
259                         callbacks->del_key(callback_data, full_key);
260                         talloc_free(full_key);
261                 } else {
262                         callbacks->add_key(callback_data, key);
263                         callbacks->set_value(callback_data, key, value_name, value_type, data);
264                 }
265                 talloc_free(key);
266                 talloc_free(value_name);
267                 talloc_free(data.data);
268         }
269         close(fd);      
270         return WERR_OK;
271 }