s4-waf: enable the pc_files in the build rules
[nivanova/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-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         char *data_string = reg_val_data_string(NULL, data->iconv_convenience,
65                                                 value_type, value);
66         W_ERROR_HAVE_NO_MEMORY(data_string);
67         fdprintf(data->fd, "\"%s\"=%s:%s\n",
68                  value_name, str_regtype(value_type), data_string);
69         talloc_free(data_string);
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|O_WRONLY, 0755);
117                 if (data->fd < 0) {
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\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                                 W_ERROR_HAVE_NO_MEMORY(curkey);
185
186                                 error = callbacks->del_key(callback_data,
187                                                            curkey);
188
189                                 if (!W_ERROR_IS_OK(error)) {
190                                         DEBUG(0,("Error deleting key %s\n",
191                                                 curkey));
192                                         talloc_free(mem_ctx);
193                                         return error;
194                                 }
195
196                                 talloc_free(line);
197                                 curkey = NULL;
198                                 continue;
199                         }
200                         curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
201                         W_ERROR_HAVE_NO_MEMORY(curkey);
202
203                         error = callbacks->add_key(callback_data, curkey);
204                         if (!W_ERROR_IS_OK(error)) {
205                                 DEBUG(0,("Error adding key %s\n", curkey));
206                                 talloc_free(mem_ctx);
207                                 return error;
208                         }
209
210                         talloc_free(line);
211                         continue;
212                 }
213
214                 /* Deleting/Changing value */
215                 p = strchr_m(line, '=');
216                 if (p == NULL) {
217                         DEBUG(0, ("Malformed line\n"));
218                         talloc_free(line);
219                         continue;
220                 }
221
222                 *p = '\0'; p++;
223
224                 if (curkey == NULL) {
225                         DEBUG(0, ("Value change without key\n"));
226                         talloc_free(line);
227                         continue;
228                 }
229
230                 /* Delete value */
231                 if (strcmp(p, "-") == 0) {
232                         error = callbacks->del_value(callback_data,
233                                                      curkey, line);
234                         if (!W_ERROR_IS_OK(error)) {
235                                 DEBUG(0, ("Error deleting value %s in key %s\n",
236                                         line, curkey));
237                                 talloc_free(mem_ctx);
238                                 return error;
239                         }
240
241                         talloc_free(line);
242                         continue;
243                 }
244
245                 q = strchr_m(p, ':');
246                 if (q) {
247                         *q = '\0';
248                         q++;
249                 }
250
251                 reg_string_to_val(line, iconv_convenience, 
252                                   q?p:"REG_SZ", q?q:p,
253                                   &value_type, &value);
254
255                 error = callbacks->set_value(callback_data, curkey, line,
256                                              value_type, value);
257                 if (!W_ERROR_IS_OK(error)) {
258                         DEBUG(0, ("Error setting value for %s in %s\n",
259                                 line, curkey));
260                         talloc_free(mem_ctx);
261                         return error;
262                 }
263
264                 talloc_free(line);
265         }
266
267         close(fd);
268
269         return WERR_OK;
270 }