Remove unused include param/param.h.
[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-2008
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
29 /**
30  * @file
31  * @brief Registry patch files
32  */
33
34 #define HEADER_STRING "REGEDIT4"
35
36 struct dotreg_data {
37         int fd;
38         struct smb_iconv_convenience *iconv_convenience;
39 };
40
41 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
42 {
43         struct dotreg_data *data = (struct dotreg_data *)_data;
44
45         fdprintf(data->fd, "\n[%s]\n", key_name);
46
47         return WERR_OK;
48 }
49
50 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
51 {
52         struct dotreg_data *data = (struct dotreg_data *)_data;
53
54         fdprintf(data->fd, "\n[-%s]\n", key_name);
55
56         return WERR_OK;
57 }
58
59 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
60                                         const char *value_name,
61                                         uint32_t value_type, DATA_BLOB value)
62 {
63         struct dotreg_data *data = (struct dotreg_data *)_data;
64
65         fdprintf(data->fd, "\"%s\"=%s:%s\n",
66                         value_name, str_regtype(value_type),
67                         reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
68
69         return WERR_OK;
70 }
71
72 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
73                                         const char *value_name)
74 {
75         struct dotreg_data *data = (struct dotreg_data *)_data;
76
77         fdprintf(data->fd, "\"%s\"=-\n", value_name);
78
79         return WERR_OK;
80 }
81
82 static WERROR reg_dotreg_diff_done(void *_data)
83 {
84         struct dotreg_data *data = (struct dotreg_data *)_data;
85
86         close(data->fd);
87         talloc_free(data);
88
89         return WERR_OK;
90 }
91
92 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
93                                              const char *key_name)
94 {
95         return WERR_NOT_SUPPORTED;
96 }
97
98 /**
99  * Save registry diff
100  */
101 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
102                                      struct smb_iconv_convenience *iconv_convenience,
103                                      struct reg_diff_callbacks **callbacks,
104                                      void **callback_data)
105 {
106         struct dotreg_data *data;
107
108         data = talloc_zero(ctx, struct dotreg_data);
109         *callback_data = data;
110
111         data->iconv_convenience = iconv_convenience;
112
113         if (filename) {
114                 data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
115                 if (data->fd < 0) {
116                         DEBUG(0, ("Unable to open %s\n", filename));
117                         return WERR_BADFILE;
118                 }
119         } else {
120                 data->fd = STDOUT_FILENO;
121         }
122
123         fdprintf(data->fd, "%s\n\n", HEADER_STRING);
124
125         *callbacks = talloc(ctx, struct reg_diff_callbacks);
126
127         (*callbacks)->add_key = reg_dotreg_diff_add_key;
128         (*callbacks)->del_key = reg_dotreg_diff_del_key;
129         (*callbacks)->set_value = reg_dotreg_diff_set_value;
130         (*callbacks)->del_value = reg_dotreg_diff_del_value;
131         (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
132         (*callbacks)->done = reg_dotreg_diff_done;
133
134         return WERR_OK;
135 }
136
137 /**
138  * Load diff file
139  */
140 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
141                                      struct smb_iconv_convenience *iconv_convenience,
142                                      const struct reg_diff_callbacks *callbacks,
143                                      void *callback_data)
144 {
145         char *line, *p, *q;
146         char *curkey = NULL;
147         TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
148         WERROR error;
149         uint32_t value_type;
150         DATA_BLOB value;
151
152         line = afdgets(fd, mem_ctx, 0);
153         if (!line) {
154                 DEBUG(0, ("Can't read from file.\n"));
155                 talloc_free(mem_ctx);
156                 close(fd);
157                 return WERR_GENERAL_FAILURE;
158         }
159
160         while ((line = afdgets(fd, mem_ctx, 0))) {
161                 /* Ignore comments and empty lines */
162                 if (strlen(line) == 0 || line[0] == ';') {
163                         talloc_free(line);
164
165                         if (curkey) {
166                                 talloc_free(curkey);
167                         }
168                         curkey = NULL;
169                         continue;
170                 }
171
172                 /* Start of key */
173                 if (line[0] == '[') {
174                         p = strchr_m(line, ']');
175                         if (p[strlen(p)-1] != ']') {
176                                 DEBUG(0, ("Missing ']'\n"));
177                                 return WERR_GENERAL_FAILURE;
178                         }
179                         /* Deleting key */
180                         if (line[1] == '-') {
181                                 curkey = talloc_strndup(line, line+2, strlen(line)-3);
182
183                                 error = callbacks->del_key(callback_data,
184                                                            curkey);
185                                 if (!W_ERROR_IS_OK(error)) {
186                                         DEBUG(0,("Error deleting key %s\n",
187                                                 curkey));
188                                         talloc_free(mem_ctx);
189                                         return error;
190                                 }
191
192                                 talloc_free(line);
193                                 curkey = NULL;
194                                 continue;
195                         }
196                         curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
197
198                         error = callbacks->add_key(callback_data, curkey);
199                         if (!W_ERROR_IS_OK(error)) {
200                                 DEBUG(0,("Error adding key %s\n", curkey));
201                                 talloc_free(mem_ctx);
202                                 return error;
203                         }
204
205                         talloc_free(line);
206                         continue;
207                 }
208
209                 /* Deleting/Changing value */
210                 p = strchr_m(line, '=');
211                 if (p == NULL) {
212                         DEBUG(0, ("Malformed line\n"));
213                         talloc_free(line);
214                         continue;
215                 }
216
217                 *p = '\0'; p++;
218
219                 if (curkey == NULL) {
220                         DEBUG(0, ("Value change without key\n"));
221                         talloc_free(line);
222                         continue;
223                 }
224
225                 /* Delete value */
226                 if (strcmp(p, "-") == 0) {
227                         error = callbacks->del_value(callback_data,
228                                                      curkey, line);
229                         if (!W_ERROR_IS_OK(error)) {
230                                 DEBUG(0, ("Error deleting value %s in key %s\n",
231                                         line, curkey));
232                                 talloc_free(mem_ctx);
233                                 return error;
234                         }
235
236                         talloc_free(line);
237                         continue;
238                 }
239
240                 q = strchr_m(p, ':');
241                 if (q) {
242                         *q = '\0';
243                         q++;
244                 }
245
246                 reg_string_to_val(line, iconv_convenience, 
247                                   q?p:"REG_SZ", q?q:p,
248                                   &value_type, &value);
249
250                 error = callbacks->set_value(callback_data, curkey, line,
251                                              value_type, value);
252                 if (!W_ERROR_IS_OK(error)) {
253                         DEBUG(0, ("Error setting value for %s in %s\n",
254                                 line, curkey));
255                         talloc_free(mem_ctx);
256                         return error;
257                 }
258
259                 talloc_free(line);
260         }
261
262         close(fd);
263
264         return WERR_OK;
265 }