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