s3:smbd:smb2: change smbXsrv_session0 to smbXsrv_session in struct user_struct.
[Samba/gebeck_regimport.git] / source3 / registry / reg_parse.c
blobbabf5c015e15688bea35d814141498815edd849b
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2010
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.
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.
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/>.
20 /**
21 * @brief Parser for dot.reg files
22 * @file reg_parse.c
23 * @author Gregor Beck <gb@sernet.de>
24 * @date Jun 2010
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"
36 #include <stdio.h>
37 #include <wchar.h>
38 #include <talloc.h>
39 #include <stdbool.h>
40 #include <string.h>
41 #include <regex.h>
42 #include <assert.h>
43 #include <stdint.h>
45 enum reg_parse_state {
46 STATE_DEFAULT,
47 STATE_KEY_OPEN,
48 STATE_VAL_HEX_CONT,
49 STATE_VAL_SZ_CONT
52 struct reg_parse {
53 struct reg_format_callback reg_format_callback;
54 cbuf* key;
55 cbuf* valname;
56 uint32_t valtype;
57 cbuf* valblob;
58 cbuf* tmp;
59 struct reg_parse_callback call;
60 int ret;
61 int linenum;
62 enum reg_parse_state state;
63 struct reg_parse_options* opt;
64 smb_iconv_t str2UTF16;
65 unsigned flags;
68 /**
69 * @defgroup action Action
70 * @{
72 static bool act_key(struct reg_parse* p, cbuf* keyname, bool del)
74 const char* name = cbuf_gets(keyname, 0);
75 cbuf_swap(p->key, keyname);
77 assert(p->state == STATE_DEFAULT || p->state == STATE_KEY_OPEN);
78 p->state = del ? STATE_DEFAULT : STATE_KEY_OPEN;
80 assert(p->call.key);
81 p->ret = p->call.key(p->call.data, &name, 1, del);
82 return p->ret >= 0;
85 static bool value_callback(struct reg_parse* p)
87 const char* name = cbuf_gets(p->valname,0);
88 const uint8_t* val = (const uint8_t*)cbuf_gets(p->valblob,0);
89 size_t len = cbuf_getpos(p->valblob);
91 assert(p->call.val);
92 p->ret = p->call.val(p->call.data, name, p->valtype, val, len);
93 return p->ret >= 0;
96 static bool act_val_hex(struct reg_parse* p, cbuf* value, bool cont)
98 cbuf_swap(p->valblob, value);
99 assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_HEX_CONT));
101 if (cont) {
102 p->state = STATE_VAL_HEX_CONT;
103 } else {
104 p->state = STATE_KEY_OPEN;
106 switch (p->valtype) {
107 case REG_EXPAND_SZ:
108 case REG_MULTI_SZ:
109 if (p->str2UTF16 != NULL) {
110 char* dst = NULL;
111 const char* src = cbuf_gets(p->valblob, 0);
112 const size_t slen = cbuf_getpos(p->valblob);
113 size_t dlen = iconvert_talloc(p,
114 p->str2UTF16,
115 src, slen,
116 &dst);
117 if (dlen != -1) {
118 cbuf_swapptr(p->valblob, &dst, dlen);
119 } else {
120 DEBUG(0, ("iconvert_talloc failed\n"));
122 talloc_free(dst);
124 default:
125 break;
127 return value_callback(p);
129 return true;
132 static bool act_val_dw(struct reg_parse* p, uint32_t val)
134 assert(p->valtype == REG_DWORD);
135 assert(p->state == STATE_KEY_OPEN);
137 cbuf_clear(p->valblob);
139 if (cbuf_putdw(p->valblob, val) < 0) {
140 return false;
142 return value_callback(p);
145 static bool act_val_sz(struct reg_parse* p, cbuf* value, bool cont)
147 cbuf_swap(p->valblob, value);
149 assert(p->valtype == REG_SZ);
150 assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_SZ_CONT));
152 if (cont) {
153 p->state = STATE_VAL_SZ_CONT;
154 } else {
155 char* dst = NULL;
156 size_t dlen;
157 const char* src = cbuf_gets(p->valblob, 0);
159 p->state = STATE_KEY_OPEN;
162 if (convert_string_talloc(p->valblob, CH_UNIX, CH_UTF16LE,
163 src, strlen(src)+1,
164 &dst, &dlen))
166 cbuf_swapptr(p->valblob, &dst, dlen);
167 } else {
168 DEBUG(0, ("convert_string_talloc failed: >%s<\n"
169 "use it as is\t", src));
171 talloc_free(dst);
173 return value_callback(p);
175 return true;
178 static bool act_val_del(struct reg_parse* p)
180 const char* name = cbuf_gets(p->valname, 0);
182 assert(p->call.val_del);
183 p->ret = p->call.val_del(p->call.data, name);
184 return p->ret >= 0;
187 static bool act_comment (struct reg_parse* p, const char* txt)
189 assert(p->call.comment);
190 p->ret = p->call.comment(p->call.data, txt);
191 return p->ret >= 0;
193 /**@}*/
196 static int nop(void* data)
198 return 0;
202 struct reg_parse* reg_parse_new(const void* ctx,
203 struct reg_parse_callback cb,
204 const char* str_enc, unsigned flags)
206 struct reg_parse* s = talloc_zero(ctx, struct reg_parse);
207 if (s == NULL)
208 return NULL;
209 s->key = cbuf_new(s);
210 s->valname = cbuf_new(s);
211 s->valblob = cbuf_new(s);
212 s->tmp = cbuf_new(s);
213 if ( (s->tmp == NULL) || (s->valblob == NULL)
214 || (s->valname == NULL) || (s->key == NULL) )
216 goto fail;
219 s->reg_format_callback.writeline = (reg_format_callback_writeline_t)&reg_parse_line;
220 s->reg_format_callback.data = s;
222 s->valtype = 0;
223 if (cb.key == NULL) {
224 cb.key = (reg_parse_callback_key_t)&nop;
226 if (cb.val == NULL) {
227 cb.val = (reg_parse_callback_val_t)&nop;
229 if (cb.val_del == NULL) {
230 cb.val_del = (reg_parse_callback_val_del_t)&nop;
232 if (cb.comment == NULL) {
233 cb.comment = (reg_parse_callback_comment_t)&nop;
236 s->call = cb;
237 s->linenum = 0;
238 s->state = STATE_DEFAULT;
239 s->flags = flags;
241 if (str_enc && !set_iconv(&s->str2UTF16, "UTF-16LE", str_enc)) {
242 DEBUG(0, ("reg_parse_new: failed to set encoding: %s",
243 str_enc));
244 goto fail;
247 assert(&s->reg_format_callback == (struct reg_format_callback*)s);
248 return s;
249 fail:
250 talloc_free(s);
251 return NULL;
255 * @defgroup parse Parser Primitive
256 * @ingroup internal
257 * @{
261 static bool srprs_key(const char** ptr, cbuf* key, bool* del)
263 const char* pos = *ptr;
264 const char* closing_bracket_pos = NULL;
265 size_t closing_bracket_idx = 0;
267 if (!srprs_skipws(&pos) || !srprs_char(&pos, '[')) {
268 return false;
271 *del = srprs_char(&pos, '-');
273 cbuf_clear(key);
275 while (true) {
276 while (srprs_charsetinv(&pos, "]\\", key))
279 switch (*pos) {
281 case ']':
282 closing_bracket_idx = cbuf_getpos(key);
283 closing_bracket_pos = pos;
284 cbuf_putc(key, ']');
285 pos++;
286 break;
288 case '\\':
289 cbuf_putc(key, '\\');
290 /* n++; */
291 /* cbuf_puts(subkeyidx, cbuf_getpos(key), sizeof(size_t)) */
292 while (srprs_char(&pos,'\\'))
294 break;
296 case '\0':
297 if (closing_bracket_pos == NULL) {
298 return false;
301 /* remove trailing backslash (if any) */
302 if (*(closing_bracket_pos-1)=='\\') {
303 closing_bracket_idx--;
306 cbuf_setpos(key, closing_bracket_idx);
307 *ptr = closing_bracket_pos+1;
308 return true;
310 default:
311 assert(false);
316 static bool srprs_val_name(const char** ptr, cbuf* name)
318 const char* pos = *ptr;
319 const size_t spos = cbuf_getpos(name);
321 if ( !srprs_skipws(&pos) ) {
322 goto fail;
325 if ( srprs_char(&pos, '@') ) {
326 cbuf_puts(name, "", -1);
328 else if (!srprs_quoted_string(&pos, name, NULL)) {
329 goto fail;
332 if (!srprs_skipws(&pos) || !srprs_char(&pos, '=')) {
333 goto fail;
336 *ptr = pos;
337 return true;
339 fail:
340 cbuf_setpos(name, spos);
341 return false;
344 static bool srprs_val_dword(const char** ptr, uint32_t* type, uint32_t* val)
346 const char* pos = *ptr;
348 if (!srprs_str(&pos, "dword:", -1)) {
349 return false;
352 if (!srprs_hex(&pos, 8, val)) {
353 return false;
356 *type = REG_DWORD;
357 *ptr = pos;
358 return true;
361 static bool srprs_val_sz(const char** ptr, uint32_t* type, cbuf* val, bool* cont)
363 if (!srprs_quoted_string(ptr, val, cont)) {
364 return false;
367 *type = REG_SZ;
368 return true;
372 static bool srprs_nl_no_eos(const char** ptr, cbuf* str, bool eof)
374 const char* pos = *ptr;
375 const size_t spos = cbuf_getpos(str);
377 if( srprs_nl(&pos, str) && (eof || *pos != '\0')) {
378 *ptr = pos;
379 return true;
381 cbuf_setpos(str, spos);
382 return false;
386 static bool srprs_eol_cont(const char** ptr, bool* cont)
388 const char* pos = *ptr;
389 bool bs = srprs_char(&pos, '\\');
391 if (!srprs_eol(&pos, NULL)) {
392 return false;
395 *cont = bs;
396 *ptr = pos;
397 return true;
400 /* matches the empty string, for zero length lists */
401 static bool srprs_val_hex_values(const char** ptr, cbuf* val, bool* cont)
403 const char* pos = *ptr;
404 unsigned u;
406 do {
407 if (!srprs_skipws(&pos) || !srprs_hex(&pos, 2, &u) || !srprs_skipws(&pos)) {
408 break;
410 cbuf_putc(val, (char)u);
411 } while(srprs_char(&pos, ','));
413 *ptr = pos;
415 if (srprs_skipws(&pos) && srprs_eol_cont(&pos, cont)) {
416 *ptr = pos;
419 return true;
422 static bool srprs_val_hex(const char** ptr, uint32_t* ptype, cbuf* val,
423 bool* cont)
425 const char* pos = *ptr;
426 uint32_t type;
428 if (!srprs_str(&pos, "hex", -1)) {
429 return false;
432 if (srprs_char(&pos, ':')) {
433 type = REG_BINARY;
435 else if (!srprs_char(&pos, '(') ||
436 !srprs_hex(&pos, 8, &type) ||
437 !srprs_char(&pos,')') ||
438 !srprs_char(&pos, ':'))
440 return false;
443 if (!srprs_val_hex_values(&pos, val, cont)) {
444 return false;
447 *ptype = type;
448 *ptr = pos;
449 return true;
453 static bool srprs_comment(const char** ptr, cbuf* str)
455 return srprs_char(ptr, ';') && srprs_line(ptr, str);
458 /**@}*/
460 int reg_parse_set_options(struct reg_parse* parser, const char* options)
462 static const char* DEFAULT ="enc=unix,flags=0";
464 int ret = 0;
465 char *key, *val;
466 void* ctx = talloc_new(parser);
468 if (options == NULL) {
469 options = DEFAULT;
472 while (srprs_option(&options, ctx, &key, &val)) {
473 if ((strcmp(key, "enc") == 0) || (strcmp(key, "strenc") == 0)) {
474 } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
475 char* end = NULL;
476 if (val != NULL) {
477 parser->flags = strtol(val, &end, 0);
479 if ((end==NULL) || (*end != '\0')) {
480 DEBUG(0, ("Invalid flags format: %s\n",
481 val ? val : "<NULL>"));
482 ret = -1;
485 /* else if (strcmp(key, "hive") == 0) { */
486 /* if (strcmp(val, "short") == 0) { */
487 /* f->hive_fmt = REG_FMT_SHORT_HIVES; */
488 /* } else if (strcmp(val, "long") == 0) { */
489 /* f->hive_fmt = REG_FMT_LONG_HIVES; */
490 /* } else if (strcmp(val, "preserve") == 0) { */
491 /* f->hive_fmt = REG_FMT_PRESERVE_HIVES; */
492 /* } else { */
493 /* DEBUG(0, ("Invalid hive format: %s\n", val)); */
494 /* ret = -1; */
495 /* } */
496 /* } */
498 talloc_free(ctx);
499 return ret;
503 int reg_parse_line(struct reg_parse* parser, const char* line)
505 const char* pos;
506 bool del=false;
507 cbuf* tmp=cbuf_clear(parser->tmp);
508 bool cb_ok = true;
509 bool cont = true;
511 if (!line) {
512 return -4;
515 parser->linenum++;
516 pos = line;
518 switch (parser->state) {
519 case STATE_VAL_HEX_CONT:
520 if (srprs_val_hex_values(&pos, parser->valblob, &cont)) {
521 cb_ok = act_val_hex(parser, parser->valblob, cont);
523 goto done;
524 case STATE_VAL_SZ_CONT:
525 if (srprs_quoted_string(&pos, parser->valblob, &cont)) {
526 cb_ok = act_val_sz(parser, parser->valblob, cont);
528 goto done;
529 default:
530 cont = false;
533 if ( !srprs_skipws(&pos) ) {
534 return -4;
537 /* empty line ?*/
538 if ( srprs_eol(&pos, NULL) ) {
539 return 0;
542 /* key line ?*/
543 else if (srprs_key(&pos, tmp, &del)) {
544 cb_ok = act_key(parser, tmp, del);
547 /* comment line ? */
548 else if (srprs_comment(&pos, tmp)) {
549 cb_ok = act_comment(parser, cbuf_gets(tmp, 0));
552 /* head line */
553 else if ((parser->linenum == 1) && srprs_line(&pos, tmp) ) {
554 /* cb_ok = act_head(parser, cbuf_gets(tmp, 0)); */
557 /* value line ?*/
558 else if (srprs_val_name(&pos, tmp)) {
559 uint32_t dw;
560 cbuf_swap(parser->valname, tmp);
561 cbuf_clear(tmp);
563 if (parser->state != STATE_KEY_OPEN) {
564 DEBUG(0, ("value \"%s\" without a key at line: %i",
565 cbuf_gets(parser->valname, 0), parser->linenum));
566 return -3;
569 if (!srprs_skipws(&pos)) {
570 return -4;
573 if (srprs_char(&pos, '-')) {
574 cb_ok = act_val_del(parser);
576 else if (srprs_val_dword(&pos, &parser->valtype, &dw)) {
577 cb_ok = act_val_dw(parser, dw);
579 else if (srprs_val_sz(&pos, &parser->valtype, tmp, &cont)) {
580 cb_ok = act_val_sz(parser, tmp, cont);
582 else if (srprs_val_hex(&pos, &parser->valtype, tmp, &cont)){
583 cb_ok = act_val_hex(parser, tmp, cont);
585 else {
586 DEBUG(0, ("value \"%s\" parse error"
587 "at line: %i pos: %li : %s",
588 cbuf_gets(parser->valname, 0), parser->linenum,
589 (long int)(pos-line), pos));
590 return -3;
593 else {
594 DEBUG(0, ("unrecognized line %i : %s\n", parser->linenum, line));
595 return -3;
598 done:
599 if (!cb_ok)
600 return -2;
602 if (!srprs_skipws(&pos) || !srprs_eol(&pos, NULL)) {
603 DEBUG(0, ("trailing garbage at line: %i pos: %li : %s\n",
604 parser->linenum, (long int)(pos-line), pos));
605 return -1;
607 return 0;
610 /******************************************************************************/
612 * @addtogroup misc
613 * @{
615 static bool lookslike_utf16(const char* line, size_t len, bool* little_endian)
617 static const uint16_t M_LE = 0xFF80;
618 static const uint16_t M_BE = 0x80FF;
619 uint16_t mask;
620 bool le;
622 size_t l = MIN(len/2, 64);
623 const uint16_t* u = (const uint16_t*)line;
624 int i;
626 assert(len >= 2);
628 if ( u[0] & M_LE ) {
629 le = true;
630 mask = M_LE;
631 } else if ( u[0] & M_BE ) {
632 le = false;
633 mask = M_BE;
634 } else {
635 return false;
638 for (i=1; i<l; i++) {
639 if ( u[i] & mask ) {
640 return false;
644 *little_endian = le;
645 return true;
648 static bool lookslike_dos(const char* line, size_t len)
650 int i;
651 for (i=0; i<len; i++) {
652 if ( (line[i] == '\0') || (line[i] & 0x80) ) {
653 return false;
655 if ( (line[i] == '\r') && (i+1 < len) && (line[i+1] == '\n') ) {
656 return true;
659 return false;
662 static bool guess_charset(const char** ptr,
663 size_t* len,
664 const char** file_enc,
665 const char** str_enc)
667 const char* charset = NULL;
668 const char* pos = *ptr;
670 if (*len < 4) {
671 return false;
674 if (srprs_bom(&pos, &charset, NULL)) {
675 *len -= (pos - *ptr);
676 *ptr = pos;
677 if (*file_enc == NULL) {
678 *file_enc = charset;
680 else if( strcmp(*file_enc, charset) != 0 ) {
681 DEBUG(0, ("file encoding forced to %s\n",
682 *file_enc));
685 else if (*file_enc == NULL) {
686 bool le;
687 if (lookslike_utf16(*ptr, *len, &le)) {
688 *file_enc = le ? "UTF-16LE" : "UTF-16BE";
690 else if (lookslike_dos(*ptr, *len)) {
691 *file_enc = "dos";
693 else {
694 *file_enc = "unix";
698 if ((str_enc != NULL) && (*str_enc == NULL)) {
699 *str_enc = ( strncmp(*ptr, "REGEDIT4", 8) == 0)
700 ? *file_enc
701 : "UTF-16LE";
704 return true;
706 /**@}*/
708 struct reg_parse_fd_opt {
709 const char* file_enc;
710 const char* str_enc;
711 unsigned flags;
712 int fail_level;
715 static struct reg_parse_fd_opt
716 reg_parse_fd_opt(void* mem_ctx, const char* options)
718 struct reg_parse_fd_opt ret = {
719 .file_enc = NULL,
720 .str_enc = NULL,
721 .flags = 0,
724 void* ctx = talloc_new(mem_ctx);
725 char *key, *val;
727 if (options == NULL) {
728 goto done;
731 while (srprs_option(&options, ctx, &key, &val)) {
732 if (strcmp(key, "enc") == 0) {
733 ret.file_enc = talloc_steal(mem_ctx, val);
734 ret.str_enc = ret.file_enc;
735 } else if (strcmp(key, "strenc") == 0) {
736 ret.str_enc = talloc_steal(mem_ctx, val);
737 } else if (strcmp(key, "fileenc") == 0) {
738 ret.file_enc = talloc_steal(mem_ctx, val);
739 } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
740 char* end = NULL;
741 if (val != NULL) {
742 ret.flags = strtol(val, &end, 0);
744 if ((end==NULL) || (*end != '\0')) {
745 DEBUG(0, ("Invalid format \"%s\": %s\n",
746 key, val ? val : "<NULL>"));
748 } else if ((strcmp(key, "fail") == 0) && (val != NULL)) {
749 char* end = NULL;
750 if (val != NULL) {
751 ret.fail_level = -strtol(val, &end, 0);
753 if ((end==NULL) || (*end != '\0')) {
754 DEBUG(0, ("Invalid format \"%s\": %s\n",
755 key, val ? val : "<NULL>"));
759 done:
760 talloc_free(ctx);
761 return ret;
765 static void
766 handle_iconv_errno(int err, const char* obuf, size_t linenum,
767 smb_iconv_t cd, const char** iptr, size_t* ilen,
768 char** optr, size_t *olen)
770 const char *pos = obuf;
771 const char *ptr = obuf;
772 switch(err) {
773 case EINVAL:
774 /* DEBUG(0, ("Incomplete multibyte sequence\n")); */
775 case E2BIG:
776 return;
777 case EILSEQ:
778 break;
779 default:
780 assert(false);
783 **optr = '\0';
784 while (srprs_line(&ptr, NULL) && srprs_nl(&ptr, NULL)) {
785 pos = ptr;
786 linenum++;
788 if (pos == *optr) {
789 pos = MAX(obuf, *optr-60);
791 DEBUG(0, ("Illegal multibyte sequence at line %lu: %s",
792 (long unsigned)(linenum+1), pos));
794 assert((*ilen) > 0);
795 do {
796 size_t il = 1;
797 DEBUGADD(0, ("<%02x>", (unsigned char)**iptr));
799 if ((*olen) > 0) {
800 *(*optr)++ = '\?';
801 (*iptr)++;
802 /* Todo: parametrize, e.g. skip: *optr++ = *iptr++; */
803 (*ilen)--;
806 if (smb_iconv(cd, iptr, &il, optr, olen) != (size_t)-1 || (errno != EILSEQ)) {
807 if(il == 0)
808 (*ilen)-- ;
809 break;
813 while ((*ilen > 0) && (*olen > 0));
815 DEBUGADD(0, ("\n"));
819 int reg_parse_fd(int fd, const struct reg_parse_callback* cb, const char* opts)
821 void* mem_ctx = talloc_stackframe();
822 cbuf* line = cbuf_new(mem_ctx);
823 smb_iconv_t cd = (smb_iconv_t)-1;
824 struct reg_parse* parser = NULL;
825 char buf_raw[1024];
826 char buf_unix[1025];
828 ssize_t nread;
829 size_t nconv;
830 const char* pos;
831 const char* iptr;
832 char* optr;
833 size_t ilen;
834 size_t olen;
835 int ret = -1;
836 bool eof = false;
837 size_t linenum = 0;
839 struct reg_parse_fd_opt opt = reg_parse_fd_opt(mem_ctx, opts);
841 if (cb == NULL) {
842 DEBUG(0,("reg_parse_fd: NULL callback\n"));
843 goto done;
846 nread = read(fd, buf_raw, sizeof(buf_raw));
847 if (nread < 0) {
848 DEBUG(0, ("reg_parse_fd: read failed: %s\n", strerror(errno)));
849 ret = nread;
850 goto done;
853 iptr = &buf_raw[0];
854 ilen = nread;
856 if (!guess_charset(&iptr, &ilen,
857 &opt.file_enc, &opt.str_enc))
859 DEBUG(0, ("reg_parse_fd: failed to guess encoding\n"));
860 goto done;
863 DEBUG(10, ("reg_parse_fd: encoding file: %s str: %s\n",
864 opt.file_enc, opt.str_enc));
867 if (!set_iconv(&cd, "unix", opt.file_enc)) {
868 DEBUG(0, ("reg_parse_fd: failed to set file encoding %s\n",
869 opt.file_enc));
870 goto done;
873 parser = reg_parse_new(mem_ctx, *cb, opt.str_enc, opt.flags);
875 optr = &buf_unix[0];
876 while (!eof) {
877 olen = sizeof(buf_unix) - (optr - buf_unix) - 1 ;
878 while ( olen > 0 ) {
879 memmove(buf_raw, iptr, ilen);
881 nread = read(fd, buf_raw + ilen, sizeof(buf_raw) - ilen);
882 if (nread < 0) {
883 DEBUG(0, ("reg_parse_fd: read failed: %s\n", strerror(errno)));
884 ret = nread;
885 goto done;
888 iptr = buf_raw;
889 ilen += nread;
891 if (ilen == 0) {
892 smb_iconv(cd, NULL, NULL, &optr, &olen);
893 eof = true;
894 break;
897 nconv = smb_iconv(cd, &iptr, &ilen, &optr, &olen);
899 if (nconv == (size_t)-1) {
900 handle_iconv_errno(errno, buf_unix, linenum,
901 cd, &iptr, &ilen,
902 &optr, &olen);
903 break;
906 /* process_lines: */
907 *optr = '\0';
908 pos = &buf_unix[0];
910 while ( srprs_line(&pos, line) && srprs_nl_no_eos(&pos, line, eof)) {
911 linenum ++;
912 ret = reg_parse_line(parser, cbuf_gets(line, 0));
913 if (ret < opt.fail_level) {
914 goto done;
916 cbuf_clear(line);
918 memmove(buf_unix, pos, optr - pos);
919 optr -= (pos - buf_unix);
922 ret = 0;
923 done:
924 set_iconv(&cd, NULL, NULL);
925 talloc_free(mem_ctx);
926 return ret;
929 int reg_parse_file(const char* fname, const struct reg_parse_callback* cb,
930 const char* opt)
932 int ret = -1;
933 int fd;
935 fd = open(fname, O_RDONLY);
936 if (fd < 0) {
937 DEBUG(0, ("reg_parse_file: open %s failed: %s\n", fname,
938 strerror(errno)));
939 return -1;
942 ret = reg_parse_fd(fd, cb, opt);
944 close(fd);
945 return ret;
948 /* static struct registry_key *find_regkey_by_hnd(pipes_struct *p, */
949 /* struct policy_handle *hnd) */
950 /* { */
951 /* struct registry_key *regkey = NULL; */
953 /* if(!find_policy_by_hnd(p,hnd,(void **)(void *)&regkey)) { */
954 /* DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: ")); */
955 /* return NULL; */
956 /* } */
958 /* return regkey; */
959 /* } */