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