1 | /*
|
---|
2 | * Samba Unix/Linux SMB client library
|
---|
3 | *
|
---|
4 | * Copyright (C) Gregor Beck 2010
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @brief Format dot.reg files
|
---|
22 | * @file reg_format.c
|
---|
23 | * @author Gregor Beck <gb@sernet.de>
|
---|
24 | * @date Sep 2010
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "includes.h"
|
---|
28 | #include "reg_format.h"
|
---|
29 | #include "reg_parse.h"
|
---|
30 | #include "reg_parse_internal.h"
|
---|
31 | #include "cbuf.h"
|
---|
32 | #include "srprs.h"
|
---|
33 | #include "registry.h"
|
---|
34 | #include "registry/reg_objects.h"
|
---|
35 | #include <assert.h>
|
---|
36 |
|
---|
37 | static void cstr_unescape(char* val)
|
---|
38 | {
|
---|
39 | all_string_sub(val, "\\r", "\r", 0);
|
---|
40 | all_string_sub(val, "\\n", "\n", 0);
|
---|
41 | all_string_sub(val, "\\t", "\t", 0);
|
---|
42 | all_string_sub(val, "\\\\", "\\", 0);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /******************************************************************************/
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Print value assign to stream.
|
---|
49 | *
|
---|
50 | * @param[out] ost outstream
|
---|
51 | * @param[in] name string
|
---|
52 | *
|
---|
53 | * @return numner of bytes written, -1 on error
|
---|
54 | * @see srprs_val_name
|
---|
55 | */
|
---|
56 | static int cbuf_print_value_assign(cbuf* ost, const char* name) {
|
---|
57 | int ret, n;
|
---|
58 | if (*name == '\0') {
|
---|
59 | ret = cbuf_putc(ost, '@');
|
---|
60 | } else {
|
---|
61 | ret = cbuf_print_quoted_string(ost, name);
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (ret<0) {
|
---|
65 | return ret;
|
---|
66 | }
|
---|
67 |
|
---|
68 | n = cbuf_putc(ost, '=');
|
---|
69 | if (n < 0) {
|
---|
70 | return n;
|
---|
71 | }
|
---|
72 | ret += n;
|
---|
73 |
|
---|
74 | return ret;
|
---|
75 | }
|
---|
76 |
|
---|
77 | enum fmt_hive {
|
---|
78 | FMT_HIVE_PRESERVE=0,
|
---|
79 | FMT_HIVE_SHORT,
|
---|
80 | FMT_HIVE_LONG
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | struct fmt_key {
|
---|
85 | enum fmt_hive hive_fmt;
|
---|
86 | enum fmt_case hive_case;
|
---|
87 | enum fmt_case key_case;
|
---|
88 | const char* sep;
|
---|
89 | };
|
---|
90 |
|
---|
91 |
|
---|
92 | static int
|
---|
93 | cbuf_print_hive(cbuf* ost, const char* hive, int len, const struct fmt_key* fmt)
|
---|
94 | {
|
---|
95 | if (fmt->hive_fmt != FMT_HIVE_PRESERVE) {
|
---|
96 | const struct hive_info* hinfo = hive_info(hive, len);
|
---|
97 | if (hinfo == NULL) {
|
---|
98 | DEBUG(0, ("Unknown hive %*s", len, hive));
|
---|
99 | } else {
|
---|
100 | switch(fmt->hive_fmt) {
|
---|
101 | case FMT_HIVE_SHORT:
|
---|
102 | hive = hinfo->short_name;
|
---|
103 | len = hinfo->short_name_len;
|
---|
104 | break;
|
---|
105 | case FMT_HIVE_LONG:
|
---|
106 | hive = hinfo->long_name;
|
---|
107 | len = hinfo->long_name_len;
|
---|
108 | break;
|
---|
109 | default:
|
---|
110 | DEBUG(0, ("Unsupported hive format %d",
|
---|
111 | (int)fmt->hive_fmt));
|
---|
112 | return -1;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return cbuf_puts_case(ost, hive, len, fmt->hive_case);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int
|
---|
121 | cbuf_print_keyname(cbuf* ost, const char* key[], int n, const struct fmt_key* fmt)
|
---|
122 | {
|
---|
123 | int r, ret=0;
|
---|
124 | size_t pos = cbuf_getpos(ost);
|
---|
125 | bool hive = true;
|
---|
126 |
|
---|
127 | for (; n>0; key++, n--) {
|
---|
128 | const char* start = *key;
|
---|
129 | while(*start != '\0') {
|
---|
130 | const char* end = start;
|
---|
131 | while(*end != '\\' && *end != '\0') {
|
---|
132 | end++;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (hive) {
|
---|
136 | r = cbuf_print_hive(ost, start, end-start, fmt);
|
---|
137 | if (r < 0) {
|
---|
138 | goto fail;
|
---|
139 | }
|
---|
140 |
|
---|
141 | ret += r;
|
---|
142 | hive = false;
|
---|
143 | } else {
|
---|
144 | r = cbuf_puts(ost, fmt->sep, -1);
|
---|
145 | if (r < 0) {
|
---|
146 | goto fail;
|
---|
147 | }
|
---|
148 | ret += r;
|
---|
149 |
|
---|
150 | r = cbuf_puts_case(ost, start, end-start, fmt->key_case);
|
---|
151 | if (r < 0) {
|
---|
152 | goto fail;
|
---|
153 | }
|
---|
154 | ret += r;
|
---|
155 | }
|
---|
156 |
|
---|
157 | while(*end == '\\') {
|
---|
158 | end++;
|
---|
159 | }
|
---|
160 | start = end;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return ret;
|
---|
164 | fail:
|
---|
165 | cbuf_setpos(ost, pos);
|
---|
166 | return r;
|
---|
167 | }
|
---|
168 | /**@}*/
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * @defgroup reg_format Format dot.reg file.
|
---|
172 | * @{
|
---|
173 | */
|
---|
174 |
|
---|
175 | struct reg_format
|
---|
176 | {
|
---|
177 | struct reg_parse_callback reg_parse_callback;
|
---|
178 | struct reg_format_callback call;
|
---|
179 | unsigned flags;
|
---|
180 | smb_iconv_t fromUTF16;
|
---|
181 | const char* sep;
|
---|
182 | };
|
---|
183 |
|
---|
184 | int reg_format_value_delete(struct reg_format* f, const char* name)
|
---|
185 | {
|
---|
186 | int ret;
|
---|
187 | cbuf* line = cbuf_new(f);
|
---|
188 |
|
---|
189 | ret = cbuf_print_value_assign(line, name);
|
---|
190 | if (ret < 0) {
|
---|
191 | goto done;
|
---|
192 | }
|
---|
193 |
|
---|
194 | ret = cbuf_putc(line, '-');
|
---|
195 | if (ret < 0 ) {
|
---|
196 | goto done;
|
---|
197 | }
|
---|
198 |
|
---|
199 | ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
|
---|
200 | done:
|
---|
201 | talloc_free(line);
|
---|
202 | return ret;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Todo: write hex if str contains CR or LF */
|
---|
206 | static int
|
---|
207 | reg_format_value_sz(struct reg_format* f, const char* name, const char* str)
|
---|
208 | {
|
---|
209 | int ret;
|
---|
210 | cbuf* line = cbuf_new(f);
|
---|
211 |
|
---|
212 | ret = cbuf_print_value_assign(line, name);
|
---|
213 | if (ret < 0) {
|
---|
214 | goto done;
|
---|
215 | }
|
---|
216 |
|
---|
217 | ret = cbuf_print_quoted_string(line, str);
|
---|
218 | if (ret < 0) {
|
---|
219 | goto done;
|
---|
220 | }
|
---|
221 |
|
---|
222 | ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
|
---|
223 |
|
---|
224 | done:
|
---|
225 | talloc_free(line);
|
---|
226 | return ret;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int reg_format_value_dw(struct reg_format* f, const char* name, uint32_t dw)
|
---|
230 | {
|
---|
231 | int ret;
|
---|
232 | cbuf* line = cbuf_new(f);
|
---|
233 |
|
---|
234 | ret = cbuf_print_value_assign(line, name);
|
---|
235 | if (ret < 0) {
|
---|
236 | goto done;
|
---|
237 | }
|
---|
238 |
|
---|
239 | ret = cbuf_printf(line, "dword:%08x", dw);
|
---|
240 | if (ret < 0) {
|
---|
241 | goto done;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
|
---|
245 | done:
|
---|
246 | talloc_free(line);
|
---|
247 | return ret;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static int reg_format_value_hex(struct reg_format* f, const char* name, uint32_t type,
|
---|
251 | const void* data, size_t len)
|
---|
252 | {
|
---|
253 | int n;
|
---|
254 | int cpl=0;
|
---|
255 | int ret=0;
|
---|
256 | const unsigned char* ptr;
|
---|
257 |
|
---|
258 | cbuf* line = cbuf_new(f);
|
---|
259 |
|
---|
260 | n = cbuf_print_value_assign(line, name);
|
---|
261 | if (n < 0) {
|
---|
262 | ret = n;
|
---|
263 | goto done;
|
---|
264 | }
|
---|
265 |
|
---|
266 | cpl += n;
|
---|
267 |
|
---|
268 | if (type==REG_BINARY && !(f->flags & REG_FMT_HEX_BIN)) {
|
---|
269 | n=cbuf_puts(line, "hex:", -1);
|
---|
270 | } else {
|
---|
271 | n=cbuf_printf(line, "hex(%x):", type);
|
---|
272 | }
|
---|
273 | if (n < 0) {
|
---|
274 | ret = n;
|
---|
275 | goto done;
|
---|
276 | }
|
---|
277 |
|
---|
278 | cpl += n;
|
---|
279 |
|
---|
280 | for (ptr=(const unsigned char *)data; len>1; len--,ptr++) {
|
---|
281 | n = cbuf_printf(line, "%02x,", (unsigned)(*ptr));
|
---|
282 | if (n < 0) {
|
---|
283 | return n;
|
---|
284 | }
|
---|
285 | cpl += n;
|
---|
286 |
|
---|
287 | if ( cpl > 76 ) {
|
---|
288 | n = cbuf_putc(line, '\\');
|
---|
289 | if (n< 0) {
|
---|
290 | return n;
|
---|
291 | }
|
---|
292 |
|
---|
293 | n = f->call.writeline(f->call.data, cbuf_gets(line,0));
|
---|
294 | if (n < 0) {
|
---|
295 | ret = n;
|
---|
296 | goto done;
|
---|
297 | }
|
---|
298 | ret += n;
|
---|
299 |
|
---|
300 | cbuf_clear(line);
|
---|
301 | cpl = cbuf_puts(line, " ", -1);
|
---|
302 | if (cpl < 0) {
|
---|
303 | ret = cpl;
|
---|
304 | goto done;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | if ( len > 0 ) {
|
---|
310 | n = cbuf_printf(line, "%02x", (unsigned)(*ptr));
|
---|
311 | if (n < 0) {
|
---|
312 | ret = n;
|
---|
313 | goto done;
|
---|
314 | }
|
---|
315 | cpl += n;
|
---|
316 | }
|
---|
317 |
|
---|
318 | n = f->call.writeline(f->call.data, cbuf_gets(line,0));
|
---|
319 | if (n < 0) {
|
---|
320 | ret = n;
|
---|
321 | goto done;
|
---|
322 | }
|
---|
323 | ret += n;
|
---|
324 | done:
|
---|
325 | talloc_free(line);
|
---|
326 | return ret;
|
---|
327 | }
|
---|
328 |
|
---|
329 | static bool is_zero_terminated_ucs2(const uint8_t* data, size_t len) {
|
---|
330 | const size_t idx = len/sizeof(smb_ucs2_t);
|
---|
331 | const smb_ucs2_t *str = (const smb_ucs2_t*)data;
|
---|
332 |
|
---|
333 | if ((len % sizeof(smb_ucs2_t)) != 0) {
|
---|
334 | return false;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (idx == 0) {
|
---|
338 | return false;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return (str[idx-1] == 0);
|
---|
342 | }
|
---|
343 |
|
---|
344 | int reg_format_value(struct reg_format* f, const char* name, uint32_t type,
|
---|
345 | const uint8_t* data, size_t len)
|
---|
346 | {
|
---|
347 | int ret = 0;
|
---|
348 | void* mem_ctx = talloc_new(f);
|
---|
349 |
|
---|
350 | switch (type) {
|
---|
351 | case REG_SZ:
|
---|
352 | if (!(f->flags & REG_FMT_HEX_SZ)
|
---|
353 | && is_zero_terminated_ucs2(data, len))
|
---|
354 | {
|
---|
355 | char* str = NULL;
|
---|
356 | size_t dlen;
|
---|
357 | if (pull_ucs2_talloc(mem_ctx, &str, (const smb_ucs2_t*)data, &dlen)) {
|
---|
358 | ret = reg_format_value_sz(f, name, str);
|
---|
359 | goto done;
|
---|
360 | } else {
|
---|
361 | DEBUG(0, ("reg_format_value %s: "
|
---|
362 | "pull_ucs2_talloc failed"
|
---|
363 | ", try to write hex\n", name));
|
---|
364 | }
|
---|
365 | }
|
---|
366 | break;
|
---|
367 |
|
---|
368 | case REG_DWORD:
|
---|
369 | if (!(f->flags & REG_FMT_HEX_SZ) && (len == sizeof(uint32_t))) {
|
---|
370 | uint32_t dw = IVAL(data,0);
|
---|
371 | ret = reg_format_value_dw(f, name, dw);
|
---|
372 | goto done;
|
---|
373 | }
|
---|
374 | break;
|
---|
375 |
|
---|
376 | case REG_MULTI_SZ:
|
---|
377 | case REG_EXPAND_SZ:
|
---|
378 | if (f->fromUTF16 && (f->fromUTF16 != ((smb_iconv_t)-1))) {
|
---|
379 | char* str = NULL;
|
---|
380 | size_t dlen = iconvert_talloc(mem_ctx, f->fromUTF16,
|
---|
381 | (const char*)data, len, &str);
|
---|
382 | if (dlen != -1) {
|
---|
383 | ret = reg_format_value_hex(f, name, type, str, dlen);
|
---|
384 | goto done;
|
---|
385 | } else {
|
---|
386 | DEBUG(0, ("reg_format_value %s: "
|
---|
387 | "iconvert_talloc failed"
|
---|
388 | ", try to write hex\n", name));
|
---|
389 | }
|
---|
390 | }
|
---|
391 | break;
|
---|
392 | default:
|
---|
393 | break;
|
---|
394 | }
|
---|
395 |
|
---|
396 | ret = reg_format_value_hex(f, name, type, data, len);
|
---|
397 | done:
|
---|
398 | talloc_free(mem_ctx);
|
---|
399 | return ret;
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | int reg_format_comment(struct reg_format* f, const char* txt)
|
---|
404 | {
|
---|
405 | int ret;
|
---|
406 | cbuf* line = cbuf_new(f);
|
---|
407 |
|
---|
408 | ret = cbuf_putc(line,';');
|
---|
409 | if (ret<0) {
|
---|
410 | goto done;
|
---|
411 | }
|
---|
412 |
|
---|
413 | ret = cbuf_puts(line, txt, -1);
|
---|
414 | if (ret < 0) {
|
---|
415 | goto done;
|
---|
416 | }
|
---|
417 |
|
---|
418 | ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
|
---|
419 | done:
|
---|
420 | talloc_free(line);
|
---|
421 | return ret;
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | /******************************************************************************/
|
---|
426 |
|
---|
427 |
|
---|
428 |
|
---|
429 | struct reg_format* reg_format_new(const void* talloc_ctx,
|
---|
430 | struct reg_format_callback cb,
|
---|
431 | const char* str_enc, unsigned flags,
|
---|
432 | const char* sep)
|
---|
433 | {
|
---|
434 | static const struct reg_parse_callback reg_parse_callback_default = {
|
---|
435 | .key = (reg_parse_callback_key_t)®_format_key,
|
---|
436 | .val = (reg_parse_callback_val_t)®_format_value,
|
---|
437 | .val_del = (reg_parse_callback_val_del_t)®_format_value_delete,
|
---|
438 | .comment = (reg_parse_callback_comment_t)®_format_comment,
|
---|
439 | };
|
---|
440 |
|
---|
441 | struct reg_format* f = talloc_zero(talloc_ctx, struct reg_format);
|
---|
442 | if (f == NULL) {
|
---|
443 | return NULL;
|
---|
444 | }
|
---|
445 |
|
---|
446 | f->reg_parse_callback = reg_parse_callback_default;
|
---|
447 | f->reg_parse_callback.data = f;
|
---|
448 |
|
---|
449 | f->call = cb;
|
---|
450 | f->flags = flags;
|
---|
451 | f->sep = sep;
|
---|
452 |
|
---|
453 | if (str_enc && !set_iconv(&f->fromUTF16, str_enc, "UTF-16LE")) {
|
---|
454 | DEBUG(0, ("reg_format_new: failed to set encoding: %s\n",
|
---|
455 | str_enc));
|
---|
456 | goto fail;
|
---|
457 | }
|
---|
458 |
|
---|
459 | assert(&f->reg_parse_callback == (struct reg_parse_callback*)f);
|
---|
460 | return f;
|
---|
461 | fail:
|
---|
462 | talloc_free(f);
|
---|
463 | return NULL;
|
---|
464 | }
|
---|
465 |
|
---|
466 | int reg_format_set_options(struct reg_format* fmt, const char* options)
|
---|
467 | {
|
---|
468 | static const char* DEFAULT ="enc=unix,flags=0,sep=\\";
|
---|
469 |
|
---|
470 | int ret = 0;
|
---|
471 | char *key, *val;
|
---|
472 | void* ctx = talloc_new(fmt);
|
---|
473 |
|
---|
474 | if (options == NULL) {
|
---|
475 | options = DEFAULT;
|
---|
476 | }
|
---|
477 |
|
---|
478 | while (srprs_option(&options, ctx, &key, &val)) {
|
---|
479 | if ((strcmp(key, "enc") == 0) || (strcmp(key, "strenc") == 0)) {
|
---|
480 | if (!set_iconv(&fmt->fromUTF16, val, "UTF-16LE")) {
|
---|
481 | DEBUG(0, ("Failed to set encoding: %s\n", val));
|
---|
482 | ret = -1;
|
---|
483 | }
|
---|
484 | } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
|
---|
485 | char* end = NULL;
|
---|
486 | if (val != NULL) {
|
---|
487 | fmt->flags = strtol(val, &end, 0);
|
---|
488 | }
|
---|
489 | if ((end==NULL) || (*end != '\0')) {
|
---|
490 | DEBUG(0, ("Invalid flags format: %s\n",
|
---|
491 | val ? val : "<NULL>"));
|
---|
492 | ret = -1;
|
---|
493 | }
|
---|
494 | } else if ((strcmp(key, "sep") == 0) && (val != NULL)) {
|
---|
495 | cstr_unescape(val);
|
---|
496 | fmt->sep = talloc_steal(fmt, val);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /* else if (strcmp(key, "hive") == 0) { */
|
---|
500 | /* if (strcmp(val, "short") == 0) { */
|
---|
501 | /* f->hive_fmt = REG_FMT_SHORT_HIVES; */
|
---|
502 | /* } else if (strcmp(val, "long") == 0) { */
|
---|
503 | /* f->hive_fmt = REG_FMT_LONG_HIVES; */
|
---|
504 | /* } else if (strcmp(val, "preserve") == 0) { */
|
---|
505 | /* f->hive_fmt = REG_FMT_PRESERVE_HIVES; */
|
---|
506 | /* } else { */
|
---|
507 | /* DEBUG(0, ("Invalid hive format: %s\n", val)); */
|
---|
508 | /* ret = -1; */
|
---|
509 | /* } */
|
---|
510 | /* } */
|
---|
511 | }
|
---|
512 | talloc_free(ctx);
|
---|
513 | return ret;
|
---|
514 | }
|
---|
515 |
|
---|
516 | int reg_format_key(struct reg_format* f, const char* key[], size_t n, bool del)
|
---|
517 | {
|
---|
518 | int ret, r;
|
---|
519 | cbuf* line = cbuf_new(f);
|
---|
520 | struct fmt_key key_fmt = {
|
---|
521 | .key_case = (f->flags >> 4) & 0x0F,
|
---|
522 | .hive_case = (f->flags >> 8) & 0x0F,
|
---|
523 | .hive_fmt = (f->flags >> 12) & 0x0F,
|
---|
524 | .sep = f->sep,
|
---|
525 | };
|
---|
526 |
|
---|
527 | ret = cbuf_putc(line, '[');
|
---|
528 | if (ret < 0) {
|
---|
529 | goto done;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (del) {
|
---|
533 | ret = cbuf_putc(line, '-');
|
---|
534 | if (ret < 0) {
|
---|
535 | goto done;
|
---|
536 | }
|
---|
537 | }
|
---|
538 |
|
---|
539 | ret = cbuf_print_keyname(line, key, n, &key_fmt);
|
---|
540 | if (ret < 0) {
|
---|
541 | goto done;
|
---|
542 | }
|
---|
543 |
|
---|
544 | ret = cbuf_putc(line, ']');
|
---|
545 | if (ret < 0) {
|
---|
546 | goto done;
|
---|
547 | }
|
---|
548 |
|
---|
549 | ret = f->call.writeline(f->call.data, "");
|
---|
550 | if (ret < 0) {
|
---|
551 | goto done;
|
---|
552 | }
|
---|
553 |
|
---|
554 | r = f->call.writeline(f->call.data, cbuf_gets(line, 0));
|
---|
555 | if (r < 0) {
|
---|
556 | ret = r;
|
---|
557 | goto done;
|
---|
558 | }
|
---|
559 | ret += r;
|
---|
560 |
|
---|
561 | done:
|
---|
562 | talloc_free(line);
|
---|
563 | return ret;
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | int reg_format_registry_key(struct reg_format* f, struct registry_key* key,
|
---|
568 | bool del)
|
---|
569 | {
|
---|
570 | return reg_format_key(f, (const char**)&key->key->name, 1, del);
|
---|
571 | }
|
---|
572 |
|
---|
573 | int reg_format_registry_value(struct reg_format* f, const char* name,
|
---|
574 | struct registry_value* val)
|
---|
575 | {
|
---|
576 | return reg_format_value(f, name, val->type,
|
---|
577 | val->data.data, val->data.length);
|
---|
578 | }
|
---|
579 |
|
---|
580 | int reg_format_regval_blob(struct reg_format* f, const char* name,
|
---|
581 | struct regval_blob* val)
|
---|
582 | {
|
---|
583 |
|
---|
584 | return reg_format_value(f,
|
---|
585 | name ? name : regval_name(val),
|
---|
586 | regval_type(val),
|
---|
587 | regval_data_p(val),
|
---|
588 | regval_size(val));
|
---|
589 | }
|
---|
590 |
|
---|
591 | /**@}*/
|
---|
592 |
|
---|
593 |
|
---|
594 | struct reg_format_file
|
---|
595 | {
|
---|
596 | FILE* file;
|
---|
597 | const char* encoding;
|
---|
598 | smb_iconv_t fromUnix;
|
---|
599 | char* nl;
|
---|
600 | size_t nl_len;
|
---|
601 | };
|
---|
602 |
|
---|
603 |
|
---|
604 | static int reg_format_file_close(struct reg_format* fmt)
|
---|
605 | {
|
---|
606 | struct reg_format_file* fmt_ctx
|
---|
607 | = (struct reg_format_file*) fmt->call.data;
|
---|
608 | int ret = 0;
|
---|
609 | FILE* file = fmt_ctx->file;
|
---|
610 |
|
---|
611 | if (fmt_ctx->encoding) {
|
---|
612 | char buf[32];
|
---|
613 | snprintf(buf, sizeof(buf), "coding: %s", fmt_ctx->encoding);
|
---|
614 | reg_format_comment(fmt, "Local Variables:");
|
---|
615 | reg_format_comment(fmt, buf);
|
---|
616 | reg_format_comment(fmt, "End:");
|
---|
617 | }
|
---|
618 |
|
---|
619 | if (file != NULL) {
|
---|
620 | ret = fclose(file);
|
---|
621 | }
|
---|
622 |
|
---|
623 | return ret;
|
---|
624 | }
|
---|
625 |
|
---|
626 | static int reg_format_file_writeline(void* ptr, const char* line)
|
---|
627 | {
|
---|
628 | size_t size;
|
---|
629 | char* dst=NULL;
|
---|
630 | struct reg_format_file* fmt_ctx = (struct reg_format_file*)ptr;
|
---|
631 | int ret, r;
|
---|
632 |
|
---|
633 | size = iconvert_talloc(ptr, fmt_ctx->fromUnix, line, strlen(line), &dst);
|
---|
634 | if (size == -1 ) {
|
---|
635 | DEBUG(0, ("reg_format_file_writeline: iconvert_talloc failed >%s<\n", line));
|
---|
636 | return -1;
|
---|
637 | }
|
---|
638 |
|
---|
639 | ret = fwrite(dst, 1, size, fmt_ctx->file);
|
---|
640 | if (ret < 0) {
|
---|
641 | goto done;
|
---|
642 | }
|
---|
643 |
|
---|
644 | r = fwrite(fmt_ctx->nl, 1, fmt_ctx->nl_len, fmt_ctx->file);
|
---|
645 | ret = (r < 0) ? r : ret + r;
|
---|
646 |
|
---|
647 | done:
|
---|
648 | talloc_free(dst);
|
---|
649 | return ret;
|
---|
650 | }
|
---|
651 |
|
---|
652 | struct reg_format_file_opt {
|
---|
653 | const char* head;
|
---|
654 | const char* nl;
|
---|
655 | const char* enc;
|
---|
656 | bool bom;
|
---|
657 | const char* str_enc;
|
---|
658 | unsigned flags;
|
---|
659 | const char* sep;
|
---|
660 | };
|
---|
661 |
|
---|
662 | static struct reg_format_file_opt reg_format_file_opt(void* mem_ctx, const char* opt)
|
---|
663 | {
|
---|
664 | static const struct reg_format_file_opt REG4 = {
|
---|
665 | .head = "REGEDIT4",
|
---|
666 | .nl = "\r\n",
|
---|
667 | .enc = "dos",
|
---|
668 | .str_enc = "dos",
|
---|
669 | .bom = false,
|
---|
670 | .flags = (FMT_HIVE_LONG << 12),
|
---|
671 | .sep = "\\",
|
---|
672 | };
|
---|
673 |
|
---|
674 | static const struct reg_format_file_opt REG5 = {
|
---|
675 | .head = "Windows Registry Editor Version 5.00",
|
---|
676 | .nl = "\r\n",
|
---|
677 | .enc = "UTF-16LE",
|
---|
678 | .str_enc = "UTF-16LE",
|
---|
679 | .bom = true,
|
---|
680 | .flags = (FMT_HIVE_LONG << 12),
|
---|
681 | .sep = "\\",
|
---|
682 | };
|
---|
683 |
|
---|
684 | struct reg_format_file_opt ret = {
|
---|
685 | .head = REG5.head,
|
---|
686 | .nl = "\n",
|
---|
687 | .enc = "unix",
|
---|
688 | .bom = false,
|
---|
689 | .str_enc = "UTF-16LE",
|
---|
690 | .flags = 0,
|
---|
691 | .sep = "\\",
|
---|
692 | };
|
---|
693 |
|
---|
694 | void* tmp_ctx = talloc_new(mem_ctx);
|
---|
695 |
|
---|
696 | char *key, *val;
|
---|
697 |
|
---|
698 | if (opt == NULL) {
|
---|
699 | goto done;
|
---|
700 | }
|
---|
701 |
|
---|
702 | while(srprs_option(&opt, tmp_ctx, &key, &val)) {
|
---|
703 | if (strcmp(key, "enc") == 0) {
|
---|
704 | ret.enc = talloc_steal(mem_ctx, val);
|
---|
705 | ret.str_enc = ret.enc;
|
---|
706 | } else if (strcmp(key, "strenc") == 0) {
|
---|
707 | ret.str_enc = talloc_steal(mem_ctx, val);
|
---|
708 | } else if (strcmp(key, "fileenc") == 0) {
|
---|
709 | ret.enc = talloc_steal(mem_ctx, val);
|
---|
710 | } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
|
---|
711 | char* end = NULL;
|
---|
712 | if (val != NULL) {
|
---|
713 | ret.flags = strtol(val, &end, 0);
|
---|
714 | }
|
---|
715 | if ((end==NULL) || (*end != '\0')) {
|
---|
716 | DEBUG(0, ("Invalid flags format: %s\n",
|
---|
717 | val ? val : "<NULL>"));
|
---|
718 | }
|
---|
719 | } else if ((strcmp(key, "sep") == 0) && (val != NULL)) {
|
---|
720 | cstr_unescape(val);
|
---|
721 | ret.sep = talloc_steal(mem_ctx, val);
|
---|
722 | } else if (strcmp(key, "head") == 0) {
|
---|
723 | cstr_unescape(val);
|
---|
724 | ret.head = talloc_steal(mem_ctx, val);
|
---|
725 | } else if (strcmp(key, "nl") == 0) {
|
---|
726 | cstr_unescape(val);
|
---|
727 | ret.nl = talloc_steal(mem_ctx, val);
|
---|
728 | } else if (strcmp(key, "bom") == 0) {
|
---|
729 | if (val == NULL) {
|
---|
730 | ret.bom = true;
|
---|
731 | } else {
|
---|
732 | ret.bom = atoi(val);
|
---|
733 | }
|
---|
734 | } else if (strcmp(key, "regedit4") == 0) {
|
---|
735 | ret = REG4;
|
---|
736 | } else if (strcmp(key, "regedit5") == 0) {
|
---|
737 | ret = REG5;
|
---|
738 | }
|
---|
739 | }
|
---|
740 | done:
|
---|
741 | talloc_free(tmp_ctx);
|
---|
742 | return ret;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | struct reg_format* reg_format_file(const void* talloc_ctx,
|
---|
747 | const char* filename,
|
---|
748 | const char* options)
|
---|
749 | {
|
---|
750 | struct reg_format_file* fmt_ctx;
|
---|
751 | struct reg_format* fmt;
|
---|
752 | int ret;
|
---|
753 | struct reg_format_file_opt opt;
|
---|
754 |
|
---|
755 | struct reg_format_callback reg_format_cb = {
|
---|
756 | .writeline = ®_format_file_writeline
|
---|
757 | };
|
---|
758 |
|
---|
759 | fmt_ctx = talloc_zero(talloc_ctx, struct reg_format_file);
|
---|
760 | if (fmt_ctx == NULL) {
|
---|
761 | errno = ENOMEM;
|
---|
762 | return NULL;
|
---|
763 | }
|
---|
764 |
|
---|
765 | opt = reg_format_file_opt(fmt_ctx, options);
|
---|
766 |
|
---|
767 | reg_format_cb.data = fmt_ctx;
|
---|
768 |
|
---|
769 | fmt = reg_format_new(talloc_ctx, reg_format_cb,
|
---|
770 | opt.str_enc, opt.flags, opt.sep);
|
---|
771 | if (fmt == NULL) {
|
---|
772 | errno = ENOMEM;
|
---|
773 | talloc_free(fmt_ctx);
|
---|
774 | return NULL;
|
---|
775 | }
|
---|
776 |
|
---|
777 | talloc_steal(fmt, fmt_ctx);
|
---|
778 |
|
---|
779 | if (!set_iconv(&fmt->fromUTF16, opt.str_enc, "UTF-16LE")) { /* HACK */
|
---|
780 | DEBUG(0, ("reg_format_file: failed to set string encoding %s",
|
---|
781 | opt.str_enc));
|
---|
782 | goto fail;
|
---|
783 | }
|
---|
784 |
|
---|
785 | if (!set_iconv(&fmt_ctx->fromUnix, opt.enc, "unix")) {
|
---|
786 | DEBUG(0, ("reg_format_file: failed to set file encoding %s",
|
---|
787 | opt.enc));
|
---|
788 | goto fail;
|
---|
789 | }
|
---|
790 | fmt_ctx->encoding = talloc_strdup(fmt_ctx, get_charset(opt.enc));
|
---|
791 |
|
---|
792 | fmt_ctx->file = fopen(filename, "w");
|
---|
793 | if (fmt_ctx->file == NULL) {
|
---|
794 | DEBUG(0, ("reg_format_file: fopen failed: %s\n", strerror(errno)));
|
---|
795 | goto fail;
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (setvbuf(fmt_ctx->file, NULL, _IOFBF, 64000) < 0) {
|
---|
799 | DEBUG(0, ("reg_format_file: setvbuf failed: %s\n", strerror(errno)));
|
---|
800 | }
|
---|
801 |
|
---|
802 | talloc_set_destructor(fmt, reg_format_file_close);
|
---|
803 |
|
---|
804 | fmt_ctx->nl_len = iconvert_talloc(fmt, fmt_ctx->fromUnix, opt.nl, strlen(opt.nl), &fmt_ctx->nl);
|
---|
805 | if (fmt_ctx->nl_len == -1) {
|
---|
806 | DEBUG(0, ("iconvert_talloc failed\n"));
|
---|
807 | goto fail;
|
---|
808 | }
|
---|
809 |
|
---|
810 | if (opt.bom) {
|
---|
811 | ret = write_bom(fmt_ctx->file, opt.enc, -1);
|
---|
812 | if (ret < 0) {
|
---|
813 | goto fail;
|
---|
814 | }
|
---|
815 | }
|
---|
816 |
|
---|
817 | ret = fmt->call.writeline(fmt->call.data, opt.head);
|
---|
818 | if (ret < 0) {
|
---|
819 | goto fail;
|
---|
820 | }
|
---|
821 |
|
---|
822 | return fmt;
|
---|
823 | fail:
|
---|
824 | talloc_free(fmt);
|
---|
825 | return NULL;
|
---|
826 | }
|
---|