Convert some more files to GPLv3.
[samba.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 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 smb_iconv_convenience *iconv_convenience,
105                                      struct reg_diff_callbacks **callbacks,
106                                      void **callback_data)
107 {
108         struct dotreg_data *data;
109
110         data = talloc_zero(ctx, struct dotreg_data);
111         *callback_data = data;
112
113         data->iconv_convenience = iconv_convenience;
114
115         if (filename) {
116                 data->fd = open(filename, O_CREAT, 0755);
117                 if (data->fd == -1) {
118                         DEBUG(0, ("Unable to open %s\n", filename));
119                         return WERR_BADFILE;
120                 }
121         } else {
122                 data->fd = STDOUT_FILENO;
123         }
124
125         fdprintf(data->fd, "%s\n", HEADER_STRING);
126
127         *callbacks = talloc(ctx, struct reg_diff_callbacks);
128
129         (*callbacks)->add_key = reg_dotreg_diff_add_key;
130         (*callbacks)->del_key = reg_dotreg_diff_del_key;
131         (*callbacks)->set_value = reg_dotreg_diff_set_value;
132         (*callbacks)->del_value = reg_dotreg_diff_del_value;
133         (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
134         (*callbacks)->done = reg_dotreg_diff_done;
135
136         return WERR_OK;
137 }
138
139 /**
140  * Load diff file
141  */
142 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
143                                      struct smb_iconv_convenience *iconv_convenience,
144                                      const struct reg_diff_callbacks *callbacks,
145                                      void *callback_data)
146 {
147         char *line, *p, *q;
148         char *curkey = NULL;
149         TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
150         WERROR error;
151         uint32_t value_type;
152         DATA_BLOB value;
153
154         line = afdgets(fd, mem_ctx, 0);
155         if (!line) {
156                 DEBUG(0, ("Can't read from file.\n"));
157                 talloc_free(mem_ctx);
158                 close(fd);
159                 return WERR_GENERAL_FAILURE;
160         }
161
162         while ((line = afdgets(fd, mem_ctx, 0))) {
163                 /* Ignore comments and empty lines */
164                 if (strlen(line) == 0 || line[0] == ';') {
165                         talloc_free(line);
166
167                         if (curkey) {
168                                 talloc_free(curkey);
169                         }
170                         curkey = NULL;
171                         continue;
172                 }
173
174                 /* Start of key */
175                 if (line[0] == '[') {
176                         p = strchr_m(line, ']');
177                         if (p[strlen(p)-1] != ']') {
178                                 DEBUG(0, ("Missing ']'\n"));
179                                 return WERR_GENERAL_FAILURE;
180                         }
181                         /* Deleting key */
182                         if (line[1] == '-') {
183                                 curkey = talloc_strndup(line, line+2, strlen(line)-3);
184
185                                 error = callbacks->del_key(callback_data,
186                                                            curkey);
187                                 if (!W_ERROR_IS_OK(error)) {
188                                         DEBUG(0,("Error deleting key %s\n",
189                                                 curkey));
190                                         talloc_free(mem_ctx);
191                                         return error;
192                                 }
193
194                                 talloc_free(line);
195                                 curkey = NULL;
196                                 continue;
197                         }
198                         curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
199
200                         error = callbacks->add_key(callback_data, curkey);
201                         if (!W_ERROR_IS_OK(error)) {
202                                 DEBUG(0,("Error adding key %s\n", curkey));
203                                 talloc_free(mem_ctx);
204                                 return error;
205                         }
206
207                         talloc_free(line);
208                         continue;
209                 }
210
211                 /* Deleting/Changing value */
212                 p = strchr_m(line, '=');
213                 if (p == NULL) {
214                         DEBUG(0, ("Malformed line\n"));
215                         talloc_free(line);
216                         continue;
217                 }
218
219                 *p = '\0'; p++;
220
221                 if (curkey == NULL) {
222                         DEBUG(0, ("Value change without key\n"));
223                         talloc_free(line);
224                         continue;
225                 }
226
227                 /* Delete value */
228                 if (strcmp(p, "-") == 0) {
229                         error = callbacks->del_value(callback_data,
230                                                      curkey, line);
231                         if (!W_ERROR_IS_OK(error)) {
232                                 DEBUG(0, ("Error deleting value %s in key %s\n",
233                                         line, curkey));
234                                 talloc_free(mem_ctx);
235                                 return error;
236                         }
237
238                         talloc_free(line);
239                         continue;
240                 }
241
242                 q = strchr_m(p, ':');
243                 if (q) {
244                         *q = '\0';
245                         q++;
246                 }
247
248                 reg_string_to_val(line, iconv_convenience, 
249                                   q?p:"REG_SZ", q?q:p,
250                                   &value_type, &value);
251
252                 error = callbacks->set_value(callback_data, curkey, line,
253                                              value_type, value);
254                 if (!W_ERROR_IS_OK(error)) {
255                         DEBUG(0, ("Error setting value for %s in %s\n",
256                                 line, curkey));
257                         talloc_free(mem_ctx);
258                         return error;
259                 }
260
261                 talloc_free(line);
262         }
263
264         close(fd);
265
266         return WERR_OK;
267 }