Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[bbaumbach/samba-autobuild/.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 2 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 UTF-16 */
24
25 #include "includes.h"
26 #include "lib/registry/patchfile.h"
27 #include "lib/registry/registry.h"
28 #include "system/filesys.h"
29 #include "param/param.h"
30
31 /**
32  * @file
33  * @brief Registry patch files
34  */
35
36 #define HEADER_STRING "REGEDIT4"
37
38 struct dotreg_data {
39         int fd;
40         struct smb_iconv_convenience *iconv_convenience;
41 };
42
43 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
44 {
45         struct dotreg_data *data = (struct dotreg_data *)_data;
46
47         fdprintf(data->fd, "\n[%s]\n", key_name);
48
49         return WERR_OK;
50 }
51
52 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
53 {
54         struct dotreg_data *data = (struct dotreg_data *)_data;
55
56         fdprintf(data->fd, "\n[-%s]\n", key_name);
57
58         return WERR_OK;
59 }
60
61 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
62                                         const char *value_name,
63                                         uint32_t value_type, DATA_BLOB value)
64 {
65         struct dotreg_data *data = (struct dotreg_data *)_data;
66
67         fdprintf(data->fd, "\"%s\"=%s:%s\n",
68                         value_name, str_regtype(value_type),
69                         reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
70
71         return WERR_OK;
72 }
73
74 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
75                                         const char *value_name)
76 {
77         struct dotreg_data *data = (struct dotreg_data *)_data;
78
79         fdprintf(data->fd, "\"%s\"=-\n", value_name);
80
81         return WERR_OK;
82 }
83
84 static WERROR reg_dotreg_diff_done(void *_data)
85 {
86         struct dotreg_data *data = (struct dotreg_data *)_data;
87
88         close(data->fd);
89         talloc_free(data);
90
91         return WERR_OK;
92 }
93
94 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
95                                              const char *key_name)
96 {
97         return WERR_NOT_SUPPORTED;
98 }
99
100 /**
101  * Save registry diff
102  */
103 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
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 = lp_iconv_convenience(global_loadparm);
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 }