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