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