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 Parser for dot.reg files
|
---|
22 | * @file reg_parse.c
|
---|
23 | * @author Gregor Beck <gb@sernet.de>
|
---|
24 | * @date Jun 2010
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "includes.h"
|
---|
29 | #include "system/filesys.h"
|
---|
30 | #include "cbuf.h"
|
---|
31 | #include "srprs.h"
|
---|
32 | #include "reg_parse_internal.h"
|
---|
33 | #include "reg_parse.h"
|
---|
34 | #include "reg_format.h"
|
---|
35 |
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <talloc.h>
|
---|
38 | #include <stdbool.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <regex.h>
|
---|
41 | #include <assert.h>
|
---|
42 | #include <stdint.h>
|
---|
43 |
|
---|
44 | enum reg_parse_state {
|
---|
45 | STATE_DEFAULT,
|
---|
46 | STATE_KEY_OPEN,
|
---|
47 | STATE_VAL_HEX_CONT,
|
---|
48 | STATE_VAL_SZ_CONT
|
---|
49 | };
|
---|
50 |
|
---|
51 | struct reg_parse {
|
---|
52 | struct reg_format_callback reg_format_callback;
|
---|
53 | cbuf* key;
|
---|
54 | cbuf* valname;
|
---|
55 | uint32_t valtype;
|
---|
56 | cbuf* valblob;
|
---|
57 | cbuf* tmp;
|
---|
58 | struct reg_parse_callback call;
|
---|
59 | int ret;
|
---|
60 | int linenum;
|
---|
61 | enum reg_parse_state state;
|
---|
62 | struct reg_parse_options* opt;
|
---|
63 | smb_iconv_t str2UTF16;
|
---|
64 | unsigned flags;
|
---|
65 | };
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @defgroup action Action
|
---|
69 | * @{
|
---|
70 | */
|
---|
71 | static bool act_key(struct reg_parse* p, cbuf* keyname, bool del)
|
---|
72 | {
|
---|
73 | const char* name = cbuf_gets(keyname, 0);
|
---|
74 | cbuf_swap(p->key, keyname);
|
---|
75 |
|
---|
76 | assert(p->state == STATE_DEFAULT || p->state == STATE_KEY_OPEN);
|
---|
77 | p->state = del ? STATE_DEFAULT : STATE_KEY_OPEN;
|
---|
78 |
|
---|
79 | assert(p->call.key);
|
---|
80 | p->ret = p->call.key(p->call.data, &name, 1, del);
|
---|
81 | return p->ret >= 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static bool value_callback(struct reg_parse* p)
|
---|
85 | {
|
---|
86 | const char* name = cbuf_gets(p->valname,0);
|
---|
87 | const uint8_t* val = (const uint8_t*)cbuf_gets(p->valblob,0);
|
---|
88 | size_t len = cbuf_getpos(p->valblob);
|
---|
89 |
|
---|
90 | assert(p->call.val);
|
---|
91 | p->ret = p->call.val(p->call.data, name, p->valtype, val, len);
|
---|
92 | return p->ret >= 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static bool act_val_hex(struct reg_parse* p, cbuf* value, bool cont)
|
---|
96 | {
|
---|
97 | cbuf_swap(p->valblob, value);
|
---|
98 | assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_HEX_CONT));
|
---|
99 |
|
---|
100 | if (cont) {
|
---|
101 | p->state = STATE_VAL_HEX_CONT;
|
---|
102 | } else {
|
---|
103 | p->state = STATE_KEY_OPEN;
|
---|
104 |
|
---|
105 | switch (p->valtype) {
|
---|
106 | case REG_EXPAND_SZ:
|
---|
107 | case REG_MULTI_SZ:
|
---|
108 | if (p->str2UTF16 != NULL) {
|
---|
109 | char* dst = NULL;
|
---|
110 | const char* src = cbuf_gets(p->valblob, 0);
|
---|
111 | const size_t slen = cbuf_getpos(p->valblob);
|
---|
112 | size_t dlen = iconvert_talloc(p,
|
---|
113 | p->str2UTF16,
|
---|
114 | src, slen,
|
---|
115 | &dst);
|
---|
116 | if (dlen != -1) {
|
---|
117 | cbuf_swapptr(p->valblob, &dst, dlen);
|
---|
118 | } else {
|
---|
119 | DEBUG(0, ("iconvert_talloc failed\n"));
|
---|
120 | }
|
---|
121 | talloc_free(dst);
|
---|
122 | }
|
---|
123 | default:
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | return value_callback(p);
|
---|
127 | }
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static bool act_val_dw(struct reg_parse* p, uint32_t val)
|
---|
132 | {
|
---|
133 | assert(p->valtype == REG_DWORD);
|
---|
134 | assert(p->state == STATE_KEY_OPEN);
|
---|
135 |
|
---|
136 | cbuf_clear(p->valblob);
|
---|
137 |
|
---|
138 | if (cbuf_putdw(p->valblob, val) < 0) {
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 | return value_callback(p);
|
---|
142 | }
|
---|
143 |
|
---|
144 | static bool act_val_sz(struct reg_parse* p, cbuf* value, bool cont)
|
---|
145 | {
|
---|
146 | cbuf_swap(p->valblob, value);
|
---|
147 |
|
---|
148 | assert(p->valtype == REG_SZ);
|
---|
149 | assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_SZ_CONT));
|
---|
150 |
|
---|
151 | if (cont) {
|
---|
152 | p->state = STATE_VAL_SZ_CONT;
|
---|
153 | } else {
|
---|
154 | char* dst = NULL;
|
---|
155 | size_t dlen;
|
---|
156 | const char* src = cbuf_gets(p->valblob, 0);
|
---|
157 |
|
---|
158 | p->state = STATE_KEY_OPEN;
|
---|
159 |
|
---|
160 |
|
---|
161 | if (convert_string_talloc(p->valblob, CH_UNIX, CH_UTF16LE,
|
---|
162 | src, strlen(src)+1,
|
---|
163 | &dst, &dlen))
|
---|
164 | {
|
---|
165 | cbuf_swapptr(p->valblob, &dst, dlen);
|
---|
166 | } else {
|
---|
167 | DEBUG(0, ("convert_string_talloc failed: >%s<\n"
|
---|
168 | "use it as is\t", src));
|
---|
169 | }
|
---|
170 | talloc_free(dst);
|
---|
171 |
|
---|
172 | return value_callback(p);
|
---|
173 | }
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static bool act_val_del(struct reg_parse* p)
|
---|
178 | {
|
---|
179 | const char* name = cbuf_gets(p->valname, 0);
|
---|
180 |
|
---|
181 | assert(p->call.val_del);
|
---|
182 | p->ret = p->call.val_del(p->call.data, name);
|
---|
183 | return p->ret >= 0;
|
---|
184 | }
|
---|
185 |
|
---|
186 | static bool act_comment (struct reg_parse* p, const char* txt)
|
---|
187 | {
|
---|
188 | assert(p->call.comment);
|
---|
189 | p->ret = p->call.comment(p->call.data, txt);
|
---|
190 | return p->ret >= 0;
|
---|
191 | }
|
---|
192 | /**@}*/
|
---|
193 |
|
---|
194 |
|
---|
195 | static int nop(void* data)
|
---|
196 | {
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | struct reg_parse* reg_parse_new(const void* ctx,
|
---|
202 | struct reg_parse_callback cb,
|
---|
203 | const char* str_enc, unsigned flags)
|
---|
204 | {
|
---|
205 | struct reg_parse* s = talloc_zero(ctx, struct reg_parse);
|
---|
206 | if (s == NULL)
|
---|
207 | return NULL;
|
---|
208 | s->key = cbuf_new(s);
|
---|
209 | s->valname = cbuf_new(s);
|
---|
210 | s->valblob = cbuf_new(s);
|
---|
211 | s->tmp = cbuf_new(s);
|
---|
212 | if ( (s->tmp == NULL) || (s->valblob == NULL)
|
---|
213 | || (s->valname == NULL) || (s->key == NULL) )
|
---|
214 | {
|
---|
215 | goto fail;
|
---|
216 | }
|
---|
217 |
|
---|
218 | s->reg_format_callback.writeline = (reg_format_callback_writeline_t)®_parse_line;
|
---|
219 | s->reg_format_callback.data = s;
|
---|
220 |
|
---|
221 | s->valtype = 0;
|
---|
222 | if (cb.key == NULL) {
|
---|
223 | cb.key = (reg_parse_callback_key_t)&nop;
|
---|
224 | }
|
---|
225 | if (cb.val == NULL) {
|
---|
226 | cb.val = (reg_parse_callback_val_t)&nop;
|
---|
227 | }
|
---|
228 | if (cb.val_del == NULL) {
|
---|
229 | cb.val_del = (reg_parse_callback_val_del_t)&nop;
|
---|
230 | }
|
---|
231 | if (cb.comment == NULL) {
|
---|
232 | cb.comment = (reg_parse_callback_comment_t)&nop;
|
---|
233 | }
|
---|
234 |
|
---|
235 | s->call = cb;
|
---|
236 | s->linenum = 0;
|
---|
237 | s->state = STATE_DEFAULT;
|
---|
238 | s->flags = flags;
|
---|
239 |
|
---|
240 | if (str_enc && !set_iconv(&s->str2UTF16, "UTF-16LE", str_enc)) {
|
---|
241 | DEBUG(0, ("reg_parse_new: failed to set encoding: %s",
|
---|
242 | str_enc));
|
---|
243 | goto fail;
|
---|
244 | }
|
---|
245 |
|
---|
246 | assert(&s->reg_format_callback == (struct reg_format_callback*)s);
|
---|
247 | return s;
|
---|
248 | fail:
|
---|
249 | talloc_free(s);
|
---|
250 | return NULL;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * @defgroup parse Parser Primitive
|
---|
255 | * @ingroup internal
|
---|
256 | * @{
|
---|
257 | */
|
---|
258 |
|
---|
259 |
|
---|
260 | static bool srprs_key(const char** ptr, cbuf* key, bool* del)
|
---|
261 | {
|
---|
262 | const char* pos = *ptr;
|
---|
263 | const char* closing_bracket_pos = NULL;
|
---|
264 | size_t closing_bracket_idx = 0;
|
---|
265 |
|
---|
266 | if (!srprs_skipws(&pos) || !srprs_char(&pos, '[')) {
|
---|
267 | return false;
|
---|
268 | }
|
---|
269 |
|
---|
270 | *del = srprs_char(&pos, '-');
|
---|
271 |
|
---|
272 | cbuf_clear(key);
|
---|
273 |
|
---|
274 | while (true) {
|
---|
275 | while (srprs_charsetinv(&pos, "]\\", key))
|
---|
276 | ;
|
---|
277 |
|
---|
278 | switch (*pos) {
|
---|
279 |
|
---|
280 | case ']':
|
---|
281 | closing_bracket_idx = cbuf_getpos(key);
|
---|
282 | closing_bracket_pos = pos;
|
---|
283 | cbuf_putc(key, ']');
|
---|
284 | pos++;
|
---|
285 | break;
|
---|
286 |
|
---|
287 | case '\\':
|
---|
288 | cbuf_putc(key, '\\');
|
---|
289 | /* n++; */
|
---|
290 | /* cbuf_puts(subkeyidx, cbuf_getpos(key), sizeof(size_t)) */
|
---|
291 | while (srprs_char(&pos,'\\'))
|
---|
292 | ;
|
---|
293 | break;
|
---|
294 |
|
---|
295 | case '\0':
|
---|
296 | if (closing_bracket_pos == NULL) {
|
---|
297 | return false;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /* remove trailing backslash (if any) */
|
---|
301 | if (*(closing_bracket_pos-1)=='\\') {
|
---|
302 | closing_bracket_idx--;
|
---|
303 | }
|
---|
304 |
|
---|
305 | cbuf_setpos(key, closing_bracket_idx);
|
---|
306 | *ptr = closing_bracket_pos+1;
|
---|
307 | return true;
|
---|
308 |
|
---|
309 | default:
|
---|
310 | assert(false);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | static bool srprs_val_name(const char** ptr, cbuf* name)
|
---|
316 | {
|
---|
317 | const char* pos = *ptr;
|
---|
318 | const size_t spos = cbuf_getpos(name);
|
---|
319 |
|
---|
320 | if ( !srprs_skipws(&pos) ) {
|
---|
321 | goto fail;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if ( srprs_char(&pos, '@') ) {
|
---|
325 | cbuf_puts(name, "", -1);
|
---|
326 | }
|
---|
327 | else if (!srprs_quoted_string(&pos, name, NULL)) {
|
---|
328 | goto fail;
|
---|
329 | }
|
---|
330 |
|
---|
331 | if (!srprs_skipws(&pos) || !srprs_char(&pos, '=')) {
|
---|
332 | goto fail;
|
---|
333 | }
|
---|
334 |
|
---|
335 | *ptr = pos;
|
---|
336 | return true;
|
---|
337 |
|
---|
338 | fail:
|
---|
339 | cbuf_setpos(name, spos);
|
---|
340 | return false;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static bool srprs_val_dword(const char** ptr, uint32_t* type, uint32_t* val)
|
---|
344 | {
|
---|
345 | const char* pos = *ptr;
|
---|
346 |
|
---|
347 | if (!srprs_str(&pos, "dword:", -1)) {
|
---|
348 | return false;
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (!srprs_hex(&pos, 8, val)) {
|
---|
352 | return false;
|
---|
353 | }
|
---|
354 |
|
---|
355 | *type = REG_DWORD;
|
---|
356 | *ptr = pos;
|
---|
357 | return true;
|
---|
358 | }
|
---|
359 |
|
---|
360 | static bool srprs_val_sz(const char** ptr, uint32_t* type, cbuf* val, bool* cont)
|
---|
361 | {
|
---|
362 | if (!srprs_quoted_string(ptr, val, cont)) {
|
---|
363 | return false;
|
---|
364 | }
|
---|
365 |
|
---|
366 | *type = REG_SZ;
|
---|
367 | return true;
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | static bool srprs_nl_no_eos(const char** ptr, cbuf* str, bool eof)
|
---|
372 | {
|
---|
373 | const char* pos = *ptr;
|
---|
374 | const size_t spos = cbuf_getpos(str);
|
---|
375 |
|
---|
376 | if( srprs_nl(&pos, str) && (eof || *pos != '\0')) {
|
---|
377 | *ptr = pos;
|
---|
378 | return true;
|
---|
379 | }
|
---|
380 | cbuf_setpos(str, spos);
|
---|
381 | return false;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | static bool srprs_eol_cont(const char** ptr, bool* cont)
|
---|
386 | {
|
---|
387 | const char* pos = *ptr;
|
---|
388 | bool bs = srprs_char(&pos, '\\');
|
---|
389 |
|
---|
390 | if (!srprs_eol(&pos, NULL)) {
|
---|
391 | return false;
|
---|
392 | }
|
---|
393 |
|
---|
394 | *cont = bs;
|
---|
395 | *ptr = pos;
|
---|
396 | return true;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* matches the empty string, for zero length lists */
|
---|
400 | static bool srprs_val_hex_values(const char** ptr, cbuf* val, bool* cont)
|
---|
401 | {
|
---|
402 | const char* pos = *ptr;
|
---|
403 | unsigned u;
|
---|
404 |
|
---|
405 | do {
|
---|
406 | if (!srprs_skipws(&pos) || !srprs_hex(&pos, 2, &u) || !srprs_skipws(&pos)) {
|
---|
407 | break;
|
---|
408 | }
|
---|
409 | cbuf_putc(val, (char)u);
|
---|
410 | } while(srprs_char(&pos, ','));
|
---|
411 |
|
---|
412 | *ptr = pos;
|
---|
413 |
|
---|
414 | if (srprs_skipws(&pos) && srprs_eol_cont(&pos, cont)) {
|
---|
415 | *ptr = pos;
|
---|
416 | }
|
---|
417 |
|
---|
418 | return true;
|
---|
419 | }
|
---|
420 |
|
---|
421 | static bool srprs_val_hex(const char** ptr, uint32_t* ptype, cbuf* val,
|
---|
422 | bool* cont)
|
---|
423 | {
|
---|
424 | const char* pos = *ptr;
|
---|
425 | uint32_t type;
|
---|
426 |
|
---|
427 | if (!srprs_str(&pos, "hex", -1)) {
|
---|
428 | return false;
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (srprs_char(&pos, ':')) {
|
---|
432 | type = REG_BINARY;
|
---|
433 | }
|
---|
434 | else if (!srprs_char(&pos, '(') ||
|
---|
435 | !srprs_hex(&pos, 8, &type) ||
|
---|
436 | !srprs_char(&pos,')') ||
|
---|
437 | !srprs_char(&pos, ':'))
|
---|
438 | {
|
---|
439 | return false;
|
---|
440 | }
|
---|
441 |
|
---|
442 | if (!srprs_val_hex_values(&pos, val, cont)) {
|
---|
443 | return false;
|
---|
444 | }
|
---|
445 |
|
---|
446 | *ptype = type;
|
---|
447 | *ptr = pos;
|
---|
448 | return true;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | static bool srprs_comment(const char** ptr, cbuf* str)
|
---|
453 | {
|
---|
454 | return srprs_char(ptr, ';') && srprs_line(ptr, str);
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**@}*/
|
---|
458 |
|
---|
459 | int reg_parse_set_options(struct reg_parse* parser, const char* options)
|
---|
460 | {
|
---|
461 | static const char* DEFAULT ="enc=unix,flags=0";
|
---|
462 |
|
---|
463 | int ret = 0;
|
---|
464 | char *key, *val;
|
---|
465 | void* ctx = talloc_new(parser);
|
---|
466 |
|
---|
467 | if (options == NULL) {
|
---|
468 | options = DEFAULT;
|
---|
469 | }
|
---|
470 |
|
---|
471 | while (srprs_option(&options, ctx, &key, &val)) {
|
---|
472 | if ((strcmp(key, "enc") == 0) || (strcmp(key, "strenc") == 0)) {
|
---|
473 | } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
|
---|
474 | char* end = NULL;
|
---|
475 | if (val != NULL) {
|
---|
476 | parser->flags = strtol(val, &end, 0);
|
---|
477 | }
|
---|
478 | if ((end==NULL) || (*end != '\0')) {
|
---|
479 | DEBUG(0, ("Invalid flags format: %s\n",
|
---|
480 | val ? val : "<NULL>"));
|
---|
481 | ret = -1;
|
---|
482 | }
|
---|
483 | }
|
---|
484 | /* else if (strcmp(key, "hive") == 0) { */
|
---|
485 | /* if (strcmp(val, "short") == 0) { */
|
---|
486 | /* f->hive_fmt = REG_FMT_SHORT_HIVES; */
|
---|
487 | /* } else if (strcmp(val, "long") == 0) { */
|
---|
488 | /* f->hive_fmt = REG_FMT_LONG_HIVES; */
|
---|
489 | /* } else if (strcmp(val, "preserve") == 0) { */
|
---|
490 | /* f->hive_fmt = REG_FMT_PRESERVE_HIVES; */
|
---|
491 | /* } else { */
|
---|
492 | /* DEBUG(0, ("Invalid hive format: %s\n", val)); */
|
---|
493 | /* ret = -1; */
|
---|
494 | /* } */
|
---|
495 | /* } */
|
---|
496 | }
|
---|
497 | talloc_free(ctx);
|
---|
498 | return ret;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | int reg_parse_line(struct reg_parse* parser, const char* line)
|
---|
503 | {
|
---|
504 | const char* pos;
|
---|
505 | bool del=false;
|
---|
506 | cbuf* tmp=cbuf_clear(parser->tmp);
|
---|
507 | bool cb_ok = true;
|
---|
508 | bool cont = true;
|
---|
509 |
|
---|
510 | if (!line) {
|
---|
511 | return -4;
|
---|
512 | }
|
---|
513 |
|
---|
514 | parser->linenum++;
|
---|
515 | pos = line;
|
---|
516 |
|
---|
517 | switch (parser->state) {
|
---|
518 | case STATE_VAL_HEX_CONT:
|
---|
519 | if (srprs_val_hex_values(&pos, parser->valblob, &cont)) {
|
---|
520 | cb_ok = act_val_hex(parser, parser->valblob, cont);
|
---|
521 | }
|
---|
522 | goto done;
|
---|
523 | case STATE_VAL_SZ_CONT:
|
---|
524 | if (srprs_quoted_string(&pos, parser->valblob, &cont)) {
|
---|
525 | cb_ok = act_val_sz(parser, parser->valblob, cont);
|
---|
526 | }
|
---|
527 | goto done;
|
---|
528 | default:
|
---|
529 | cont = false;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if ( !srprs_skipws(&pos) ) {
|
---|
533 | return -4;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /* empty line ?*/
|
---|
537 | if ( srprs_eol(&pos, NULL) ) {
|
---|
538 | return 0;
|
---|
539 | }
|
---|
540 |
|
---|
541 | /* key line ?*/
|
---|
542 | else if (srprs_key(&pos, tmp, &del)) {
|
---|
543 | cb_ok = act_key(parser, tmp, del);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /* comment line ? */
|
---|
547 | else if (srprs_comment(&pos, tmp)) {
|
---|
548 | cb_ok = act_comment(parser, cbuf_gets(tmp, 0));
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* head line */
|
---|
552 | else if ((parser->linenum == 1) && srprs_line(&pos, tmp) ) {
|
---|
553 | /* cb_ok = act_head(parser, cbuf_gets(tmp, 0)); */
|
---|
554 | }
|
---|
555 |
|
---|
556 | /* value line ?*/
|
---|
557 | else if (srprs_val_name(&pos, tmp)) {
|
---|
558 | uint32_t dw;
|
---|
559 | cbuf_swap(parser->valname, tmp);
|
---|
560 | cbuf_clear(tmp);
|
---|
561 |
|
---|
562 | if (parser->state != STATE_KEY_OPEN) {
|
---|
563 | DEBUG(0, ("value \"%s\" without a key at line: %i",
|
---|
564 | cbuf_gets(parser->valname, 0), parser->linenum));
|
---|
565 | return -3;
|
---|
566 | }
|
---|
567 |
|
---|
568 | if (!srprs_skipws(&pos)) {
|
---|
569 | return -4;
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (srprs_char(&pos, '-')) {
|
---|
573 | cb_ok = act_val_del(parser);
|
---|
574 | }
|
---|
575 | else if (srprs_val_dword(&pos, &parser->valtype, &dw)) {
|
---|
576 | cb_ok = act_val_dw(parser, dw);
|
---|
577 | }
|
---|
578 | else if (srprs_val_sz(&pos, &parser->valtype, tmp, &cont)) {
|
---|
579 | cb_ok = act_val_sz(parser, tmp, cont);
|
---|
580 | }
|
---|
581 | else if (srprs_val_hex(&pos, &parser->valtype, tmp, &cont)){
|
---|
582 | cb_ok = act_val_hex(parser, tmp, cont);
|
---|
583 | }
|
---|
584 | else {
|
---|
585 | DEBUG(0, ("value \"%s\" parse error"
|
---|
586 | "at line: %i pos: %li : %s",
|
---|
587 | cbuf_gets(parser->valname, 0), parser->linenum,
|
---|
588 | (long int)(pos-line), pos));
|
---|
589 | return -3;
|
---|
590 | }
|
---|
591 | }
|
---|
592 | else {
|
---|
593 | DEBUG(0, ("unrecognized line %i : %s\n", parser->linenum, line));
|
---|
594 | return -3;
|
---|
595 | }
|
---|
596 |
|
---|
597 | done:
|
---|
598 | if (!cb_ok)
|
---|
599 | return -2;
|
---|
600 |
|
---|
601 | if (!srprs_skipws(&pos) || !srprs_eol(&pos, NULL)) {
|
---|
602 | DEBUG(0, ("trailing garbage at line: %i pos: %li : %s\n",
|
---|
603 | parser->linenum, (long int)(pos-line), pos));
|
---|
604 | return -1;
|
---|
605 | }
|
---|
606 | return 0;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /******************************************************************************/
|
---|
610 | /**
|
---|
611 | * @addtogroup misc
|
---|
612 | * @{
|
---|
613 | */
|
---|
614 | static bool lookslike_utf16(const char* line, size_t len, bool* little_endian)
|
---|
615 | {
|
---|
616 | static const uint16_t M_LE = 0xFF80;
|
---|
617 | static const uint16_t M_BE = 0x80FF;
|
---|
618 | uint16_t mask;
|
---|
619 | bool le;
|
---|
620 |
|
---|
621 | size_t l = MIN(len/2, 64);
|
---|
622 | const uint16_t* u = (const uint16_t*)line;
|
---|
623 | int i;
|
---|
624 |
|
---|
625 | assert(len >= 2);
|
---|
626 |
|
---|
627 | if ( u[0] & M_LE ) {
|
---|
628 | le = true;
|
---|
629 | mask = M_LE;
|
---|
630 | } else if ( u[0] & M_BE ) {
|
---|
631 | le = false;
|
---|
632 | mask = M_BE;
|
---|
633 | } else {
|
---|
634 | return false;
|
---|
635 | }
|
---|
636 |
|
---|
637 | for (i=1; i<l; i++) {
|
---|
638 | if ( u[i] & mask ) {
|
---|
639 | return false;
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | *little_endian = le;
|
---|
644 | return true;
|
---|
645 | }
|
---|
646 |
|
---|
647 | static bool lookslike_dos(const char* line, size_t len)
|
---|
648 | {
|
---|
649 | int i;
|
---|
650 | for (i=0; i<len; i++) {
|
---|
651 | if ( (line[i] == '\0') || (line[i] & 0x80) ) {
|
---|
652 | return false;
|
---|
653 | }
|
---|
654 | if ( (line[i] == '\r') && (i+1 < len) && (line[i+1] == '\n') ) {
|
---|
655 | return true;
|
---|
656 | }
|
---|
657 | }
|
---|
658 | return false;
|
---|
659 | }
|
---|
660 |
|
---|
661 | static bool guess_charset(const char** ptr,
|
---|
662 | size_t* len,
|
---|
663 | const char** file_enc,
|
---|
664 | const char** str_enc)
|
---|
665 | {
|
---|
666 | const char* charset = NULL;
|
---|
667 | const char* pos = *ptr;
|
---|
668 |
|
---|
669 | if (*len < 4) {
|
---|
670 | return false;
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (srprs_bom(&pos, &charset, NULL)) {
|
---|
674 | *len -= (pos - *ptr);
|
---|
675 | *ptr = pos;
|
---|
676 | if (*file_enc == NULL) {
|
---|
677 | *file_enc = charset;
|
---|
678 | }
|
---|
679 | else if( strcmp(*file_enc, charset) != 0 ) {
|
---|
680 | DEBUG(0, ("file encoding forced to %s\n",
|
---|
681 | *file_enc));
|
---|
682 | }
|
---|
683 | }
|
---|
684 | else if (*file_enc == NULL) {
|
---|
685 | bool le;
|
---|
686 | if (lookslike_utf16(*ptr, *len, &le)) {
|
---|
687 | *file_enc = le ? "UTF-16LE" : "UTF-16BE";
|
---|
688 | }
|
---|
689 | else if (lookslike_dos(*ptr, *len)) {
|
---|
690 | *file_enc = "dos";
|
---|
691 | }
|
---|
692 | else {
|
---|
693 | *file_enc = "unix";
|
---|
694 | }
|
---|
695 | }
|
---|
696 |
|
---|
697 | if ((str_enc != NULL) && (*str_enc == NULL)) {
|
---|
698 | *str_enc = ( strncmp(*ptr, "REGEDIT4", 8) == 0)
|
---|
699 | ? *file_enc
|
---|
700 | : "UTF-16LE";
|
---|
701 | }
|
---|
702 |
|
---|
703 | return true;
|
---|
704 | }
|
---|
705 | /**@}*/
|
---|
706 |
|
---|
707 | struct reg_parse_fd_opt {
|
---|
708 | const char* file_enc;
|
---|
709 | const char* str_enc;
|
---|
710 | unsigned flags;
|
---|
711 | int fail_level;
|
---|
712 | };
|
---|
713 |
|
---|
714 | static struct reg_parse_fd_opt
|
---|
715 | reg_parse_fd_opt(void* mem_ctx, const char* options)
|
---|
716 | {
|
---|
717 | struct reg_parse_fd_opt ret = {
|
---|
718 | .file_enc = NULL,
|
---|
719 | .str_enc = NULL,
|
---|
720 | .flags = 0,
|
---|
721 | };
|
---|
722 |
|
---|
723 | void* ctx = talloc_new(mem_ctx);
|
---|
724 | char *key, *val;
|
---|
725 |
|
---|
726 | if (options == NULL) {
|
---|
727 | goto done;
|
---|
728 | }
|
---|
729 |
|
---|
730 | while (srprs_option(&options, ctx, &key, &val)) {
|
---|
731 | if (strcmp(key, "enc") == 0) {
|
---|
732 | ret.file_enc = talloc_steal(mem_ctx, val);
|
---|
733 | ret.str_enc = ret.file_enc;
|
---|
734 | } else if (strcmp(key, "strenc") == 0) {
|
---|
735 | ret.str_enc = talloc_steal(mem_ctx, val);
|
---|
736 | } else if (strcmp(key, "fileenc") == 0) {
|
---|
737 | ret.file_enc = talloc_steal(mem_ctx, val);
|
---|
738 | } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
|
---|
739 | char* end = NULL;
|
---|
740 | if (val != NULL) {
|
---|
741 | ret.flags = strtol(val, &end, 0);
|
---|
742 | }
|
---|
743 | if ((end==NULL) || (*end != '\0')) {
|
---|
744 | DEBUG(0, ("Invalid format \"%s\": %s\n",
|
---|
745 | key, val ? val : "<NULL>"));
|
---|
746 | }
|
---|
747 | } else if ((strcmp(key, "fail") == 0) && (val != NULL)) {
|
---|
748 | char* end = NULL;
|
---|
749 | if (val != NULL) {
|
---|
750 | ret.fail_level = -strtol(val, &end, 0);
|
---|
751 | }
|
---|
752 | if ((end==NULL) || (*end != '\0')) {
|
---|
753 | DEBUG(0, ("Invalid format \"%s\": %s\n",
|
---|
754 | key, val ? val : "<NULL>"));
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 | done:
|
---|
759 | talloc_free(ctx);
|
---|
760 | return ret;
|
---|
761 | }
|
---|
762 |
|
---|
763 |
|
---|
764 | static void
|
---|
765 | handle_iconv_errno(int err, const char* obuf, size_t linenum,
|
---|
766 | smb_iconv_t cd, const char** iptr, size_t* ilen,
|
---|
767 | char** optr, size_t *olen)
|
---|
768 | {
|
---|
769 | const char *pos = obuf;
|
---|
770 | const char *ptr = obuf;
|
---|
771 | switch(err) {
|
---|
772 | case EINVAL:
|
---|
773 | /* DEBUG(0, ("Incomplete multibyte sequence\n")); */
|
---|
774 | case E2BIG:
|
---|
775 | return;
|
---|
776 | case EILSEQ:
|
---|
777 | break;
|
---|
778 | default:
|
---|
779 | assert(false);
|
---|
780 | }
|
---|
781 |
|
---|
782 | **optr = '\0';
|
---|
783 | while (srprs_line(&ptr, NULL) && srprs_nl(&ptr, NULL)) {
|
---|
784 | pos = ptr;
|
---|
785 | linenum++;
|
---|
786 | }
|
---|
787 | if (pos == *optr) {
|
---|
788 | pos = MAX(obuf, *optr-60);
|
---|
789 | }
|
---|
790 | DEBUG(0, ("Illegal multibyte sequence at line %lu: %s",
|
---|
791 | (long unsigned)(linenum+1), pos));
|
---|
792 |
|
---|
793 | assert((*ilen) > 0);
|
---|
794 | do {
|
---|
795 | size_t il = 1;
|
---|
796 | DEBUGADD(0, ("<%02x>", (unsigned char)**iptr));
|
---|
797 |
|
---|
798 | if ((*olen) > 0) {
|
---|
799 | *(*optr)++ = '\?';
|
---|
800 | (*iptr)++;
|
---|
801 | /* Todo: parametrize, e.g. skip: *optr++ = *iptr++; */
|
---|
802 | (*ilen)--;
|
---|
803 | }
|
---|
804 |
|
---|
805 | if (smb_iconv(cd, iptr, &il, optr, olen) != (size_t)-1 || (errno != EILSEQ)) {
|
---|
806 | if(il == 0)
|
---|
807 | (*ilen)-- ;
|
---|
808 | break;
|
---|
809 | }
|
---|
810 |
|
---|
811 | }
|
---|
812 | while ((*ilen > 0) && (*olen > 0));
|
---|
813 |
|
---|
814 | DEBUGADD(0, ("\n"));
|
---|
815 |
|
---|
816 | }
|
---|
817 |
|
---|
818 | int reg_parse_fd(int fd, const struct reg_parse_callback* cb, const char* opts)
|
---|
819 | {
|
---|
820 | void* mem_ctx = talloc_stackframe();
|
---|
821 | cbuf* line = cbuf_new(mem_ctx);
|
---|
822 | smb_iconv_t cd = (smb_iconv_t)-1;
|
---|
823 | struct reg_parse* parser = NULL;
|
---|
824 | char buf_raw[1024];
|
---|
825 | char buf_unix[1025];
|
---|
826 |
|
---|
827 | ssize_t nread;
|
---|
828 | size_t nconv;
|
---|
829 | const char* pos;
|
---|
830 | const char* iptr;
|
---|
831 | char* optr;
|
---|
832 | size_t ilen;
|
---|
833 | size_t olen;
|
---|
834 | int ret = -1;
|
---|
835 | bool eof = false;
|
---|
836 | size_t linenum = 0;
|
---|
837 |
|
---|
838 | struct reg_parse_fd_opt opt = reg_parse_fd_opt(mem_ctx, opts);
|
---|
839 |
|
---|
840 | if (cb == NULL) {
|
---|
841 | DEBUG(0,("reg_parse_fd: NULL callback\n"));
|
---|
842 | goto done;
|
---|
843 | }
|
---|
844 |
|
---|
845 | nread = read(fd, buf_raw, sizeof(buf_raw));
|
---|
846 | if (nread < 0) {
|
---|
847 | DEBUG(0, ("reg_parse_fd: read failed: %s\n", strerror(errno)));
|
---|
848 | ret = nread;
|
---|
849 | goto done;
|
---|
850 | }
|
---|
851 |
|
---|
852 | iptr = &buf_raw[0];
|
---|
853 | ilen = nread;
|
---|
854 |
|
---|
855 | if (!guess_charset(&iptr, &ilen,
|
---|
856 | &opt.file_enc, &opt.str_enc))
|
---|
857 | {
|
---|
858 | DEBUG(0, ("reg_parse_fd: failed to guess encoding\n"));
|
---|
859 | goto done;
|
---|
860 | }
|
---|
861 |
|
---|
862 | DEBUG(10, ("reg_parse_fd: encoding file: %s str: %s\n",
|
---|
863 | opt.file_enc, opt.str_enc));
|
---|
864 |
|
---|
865 |
|
---|
866 | if (!set_iconv(&cd, "unix", opt.file_enc)) {
|
---|
867 | DEBUG(0, ("reg_parse_fd: failed to set file encoding %s\n",
|
---|
868 | opt.file_enc));
|
---|
869 | goto done;
|
---|
870 | }
|
---|
871 |
|
---|
872 | parser = reg_parse_new(mem_ctx, *cb, opt.str_enc, opt.flags);
|
---|
873 |
|
---|
874 | optr = &buf_unix[0];
|
---|
875 | while (!eof) {
|
---|
876 | olen = sizeof(buf_unix) - (optr - buf_unix) - 1 ;
|
---|
877 | while ( olen > 0 ) {
|
---|
878 | memmove(buf_raw, iptr, ilen);
|
---|
879 |
|
---|
880 | nread = read(fd, buf_raw + ilen, sizeof(buf_raw) - ilen);
|
---|
881 | if (nread < 0) {
|
---|
882 | DEBUG(0, ("reg_parse_fd: read failed: %s\n", strerror(errno)));
|
---|
883 | ret = nread;
|
---|
884 | goto done;
|
---|
885 | }
|
---|
886 |
|
---|
887 | iptr = buf_raw;
|
---|
888 | ilen += nread;
|
---|
889 |
|
---|
890 | if (ilen == 0) {
|
---|
891 | smb_iconv(cd, NULL, NULL, &optr, &olen);
|
---|
892 | eof = true;
|
---|
893 | break;
|
---|
894 | }
|
---|
895 |
|
---|
896 | nconv = smb_iconv(cd, &iptr, &ilen, &optr, &olen);
|
---|
897 |
|
---|
898 | if (nconv == (size_t)-1) {
|
---|
899 | handle_iconv_errno(errno, buf_unix, linenum,
|
---|
900 | cd, &iptr, &ilen,
|
---|
901 | &optr, &olen);
|
---|
902 | break;
|
---|
903 | }
|
---|
904 | }
|
---|
905 | /* process_lines: */
|
---|
906 | *optr = '\0';
|
---|
907 | pos = &buf_unix[0];
|
---|
908 |
|
---|
909 | while ( srprs_line(&pos, line) && srprs_nl_no_eos(&pos, line, eof)) {
|
---|
910 | linenum ++;
|
---|
911 | ret = reg_parse_line(parser, cbuf_gets(line, 0));
|
---|
912 | if (ret < opt.fail_level) {
|
---|
913 | goto done;
|
---|
914 | }
|
---|
915 | cbuf_clear(line);
|
---|
916 | }
|
---|
917 | memmove(buf_unix, pos, optr - pos);
|
---|
918 | optr -= (pos - buf_unix);
|
---|
919 | }
|
---|
920 |
|
---|
921 | ret = 0;
|
---|
922 | done:
|
---|
923 | set_iconv(&cd, NULL, NULL);
|
---|
924 | talloc_free(mem_ctx);
|
---|
925 | return ret;
|
---|
926 | }
|
---|
927 |
|
---|
928 | int reg_parse_file(const char* fname, const struct reg_parse_callback* cb,
|
---|
929 | const char* opt)
|
---|
930 | {
|
---|
931 | int ret = -1;
|
---|
932 | int fd;
|
---|
933 |
|
---|
934 | fd = open(fname, O_RDONLY);
|
---|
935 | if (fd < 0) {
|
---|
936 | DEBUG(0, ("reg_parse_file: open %s failed: %s\n", fname,
|
---|
937 | strerror(errno)));
|
---|
938 | return -1;
|
---|
939 | }
|
---|
940 |
|
---|
941 | ret = reg_parse_fd(fd, cb, opt);
|
---|
942 |
|
---|
943 | close(fd);
|
---|
944 | return ret;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* static struct registry_key *find_regkey_by_hnd(pipes_struct *p, */
|
---|
948 | /* struct policy_handle *hnd) */
|
---|
949 | /* { */
|
---|
950 | /* struct registry_key *regkey = NULL; */
|
---|
951 |
|
---|
952 | /* if(!find_policy_by_hnd(p,hnd,(void **)(void *)®key)) { */
|
---|
953 | /* DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: ")); */
|
---|
954 | /* return NULL; */
|
---|
955 | /* } */
|
---|
956 |
|
---|
957 | /* return regkey; */
|
---|
958 | /* } */
|
---|