Attempt to fix the patchfile_preg backend for big endian machines.
[ira/wip.git] / source4 / lib / registry / patchfile_dotreg.c
1 /*
2    Unix SMB/CIFS implementation.
3    Reading .REG files
4
5    Copyright (C) Jelmer Vernooij 2004-2007
6    Copyright (C) Wilco Baan Hofman 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /* FIXME Newer .REG files, created by Windows XP and above use unicode UCS-2 */
24
25 #include "includes.h"
26 #include "lib/registry/registry.h"
27 #include "system/filesys.h"
28 #include "param/param.h"
29
30 /**
31  * @file
32  * @brief Registry patch files
33  */
34
35 #define HEADER_STRING "REGEDIT4"
36
37 struct dotreg_data {
38         int fd;
39         struct smb_iconv_convenience *iconv_convenience;
40 };
41
42 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
43 {
44         struct dotreg_data *data = (struct dotreg_data *)_data;
45
46         fdprintf(data->fd, "\n[%s]\n", key_name);
47
48         return WERR_OK;
49 }
50
51 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
52 {
53         struct dotreg_data *data = (struct dotreg_data *)_data;
54
55         fdprintf(data->fd, "\n[-%s]\n", key_name);
56
57         return WERR_OK;
58 }
59
60 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
61                                         const char *value_name,
62                                         uint32_t value_type, DATA_BLOB value)
63 {
64         struct dotreg_data *data = (struct dotreg_data *)_data;
65
66         fdprintf(data->fd, "\"%s\"=%s:%s\n",
67                         value_name, str_regtype(value_type),
68                         reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
69
70         return WERR_OK;
71 }
72
73 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
74                                         const char *value_name)
75 {
76         struct dotreg_data *data = (struct dotreg_data *)_data;
77
78         fdprintf(data->fd, "\"%s\"=-\n", value_name);
79
80         return WERR_OK;
81 }
82
83 static WERROR reg_dotreg_diff_done(void *_data)
84 {
85         struct dotreg_data *data = (struct dotreg_data *)_data;
86
87         close(data->fd);
88         talloc_free(data);
89
90         return WERR_OK;
91 }
92
93 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
94                                              const char *key_name)
95 {
96         return WERR_NOT_SUPPORTED;
97 }
98
99 /**
100  * Save registry diff
101  */
102 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
103                                      struct smb_iconv_convenience *iconv_convenience,
104                                      struct reg_diff_callbacks **callbacks,
105                                      void **callback_data)
106 {
107         struct dotreg_data *data;
108
109         data = talloc_zero(ctx, struct dotreg_data);
110         *callback_data = data;
111
112         data->iconv_convenience = iconv_convenience;
113
114         if (filename) {
115                 data->fd = open(filename, O_CREAT, 0755);
116                 if (data->fd == -1) {
117                         DEBUG(0, ("Unable to open %s\n", filename));
118                         return WERR_BADFILE;
119                 }
120         } else {
121                 data->fd = STDOUT_FILENO;
122         }
123
124         fdprintf(data->fd, "%s\n", HEADER_STRING);
125
126         *callbacks = talloc(ctx, struct reg_diff_callbacks);
127
128         (*callbacks)->add_key = reg_dotreg_diff_add_key;
129         (*callbacks)->del_key = reg_dotreg_diff_del_key;
130         (*callbacks)->set_value = reg_dotreg_diff_set_value;
131         (*callbacks)->del_value = reg_dotreg_diff_del_value;
132         (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
133         (*callbacks)->done = reg_dotreg_diff_done;
134
135         return WERR_OK;
136 }
137
138 /**
139  * Load diff file
140  */
141 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
142                                      struct smb_iconv_convenience *iconv_convenience,
143                                      const struct reg_diff_callbacks *callbacks,
144                                      void *callback_data)
145 {
146         char *line, *p, *q;
147         char *curkey = NULL;
148         TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
149         WERROR error;
150         uint32_t value_type;
151         DATA_BLOB value;
152
153         line = afdgets(fd, mem_ctx, 0);
154         if (!line) {
155                 DEBUG(0, ("Can't read from file.\n"));
156                 talloc_free(mem_ctx);
157                 close(fd);
158                 return WERR_GENERAL_FAILURE;
159         }
160
161         while ((line = afdgets(fd, mem_ctx, 0))) {
162                 /* Ignore comments and empty lines */
163                 if (strlen(line) == 0 || line[0] == ';') {
164                         talloc_free(line);
165
166                         if (curkey) {
167                                 talloc_free(curkey);
168                         }
169                         curkey = NULL;
170                         continue;
171                 }
172
173                 /* Start of key */
174                 if (line[0] == '[') {
175                         p = strchr_m(line, ']');
176                         if (p[strlen(p)-1] != ']') {
177                                 DEBUG(0, ("Missing ']'\n"));
178                                 return WERR_GENERAL_FAILURE;
179                         }
180                         /* Deleting key */
181                         if (line[1] == '-') {
182                                 curkey = talloc_strndup(line, line+2, strlen(line)-3);
183
184                                 error = callbacks->del_key(callback_data,
185                                                            curkey);
186                                 if (!W_ERROR_IS_OK(error)) {
187                                         DEBUG(0,("Error deleting key %s\n",
188                                                 curkey));
189                                         talloc_free(mem_ctx);
190                                         return error;
191                                 }
192
193                                 talloc_free(line);
194                                 curkey = NULL;
195                                 continue;
196                         }
197                         curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
198
199                         error = callbacks->add_key(callback_data, curkey);
200                         if (!W_ERROR_IS_OK(error)) {
201                                 DEBUG(0,("Error adding key %s\n", curkey));
202                                 talloc_free(mem_ctx);
203                                 return error;
204                         }
205
206                         talloc_free(line);
207                         continue;
208                 }
209
210                 /* Deleting/Changing value */
211                 p = strchr_m(line, '=');
212                 if (p == NULL) {
213                         DEBUG(0, ("Malformed line\n"));
214                         talloc_free(line);
215                         continue;
216                 }
217
218                 *p = '\0'; p++;
219
220                 if (curkey == NULL) {
221                         DEBUG(0, ("Value change without key\n"));
222                         talloc_free(line);
223                         continue;
224                 }
225
226                 /* Delete value */
227                 if (strcmp(p, "-") == 0) {
228                         error = callbacks->del_value(callback_data,
229                                                      curkey, line);
230                         if (!W_ERROR_IS_OK(error)) {
231                                 DEBUG(0, ("Error deleting value %s in key %s\n",
232                                         line, curkey));
233                                 talloc_free(mem_ctx);
234                                 return error;
235                         }
236
237                         talloc_free(line);
238                         continue;
239                 }
240
241                 q = strchr_m(p, ':');
242                 if (q) {
243                         *q = '\0';
244                         q++;
245                 }
246
247                 reg_string_to_val(line, iconv_convenience, 
248                                   q?p:"REG_SZ", q?q:p,
249                                   &value_type, &value);
250
251                 error = callbacks->set_value(callback_data, curkey, line,
252                                              value_type, value);
253                 if (!W_ERROR_IS_OK(error)) {
254                         DEBUG(0, ("Error setting value for %s in %s\n",
255                                 line, curkey));
256                         talloc_free(mem_ctx);
257                         return error;
258                 }
259
260                 talloc_free(line);
261         }
262
263         close(fd);
264
265         return WERR_OK;
266 }