File: sieve-error.c

package info (click to toggle)
dovecot 1%3A1.2.15-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 30,252 kB
  • ctags: 19,837
  • sloc: ansic: 191,438; sh: 21,091; makefile: 3,330; cpp: 526; perl: 108; xml: 44
file content (1032 lines) | stat: -rw-r--r-- 26,375 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/* Copyright (c) 2002-2010 Dovecot Sieve authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"
#include "array.h"
#include "ostream.h"
#include "var-expand.h"
#include "eacces-error.h"

#include "sieve-common.h"
#include "sieve-script.h"
#include "sieve-error-private.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

/*
 * Definitions
 */

#define CRITICAL_MSG \
	"internal error occurred: refer to server log for more information."
#define CRITICAL_MSG_STAMP CRITICAL_MSG " [%Y-%m-%d %H:%M:%S]"

/* Logfile error handler will rotate log when it exceeds 10k bytes */
#define LOGFILE_MAX_SIZE (10 * 1024)

/*
 * Utility
 */

const char *sieve_error_script_location
(const struct sieve_script *script, unsigned int source_line)
{
	const char *sname;

	sname = ( script == NULL ? NULL : sieve_script_name(script) );

	if ( sname == NULL || *sname == '\0' )
		return t_strdup_printf("line %d", source_line);

	return t_strdup_printf("%s: line %d", sname, source_line);
}


/*
 * Main error functions
 */

static void sieve_vcopy_master
(const char *location, sieve_error_vfunc_t error_vfunc,
	const char *fmt, va_list args)
{
	va_list args_copy;

	VA_COPY(args_copy, args);

	error_vfunc(_sieve_system_ehandler, location, fmt, args_copy);
}

void sieve_verror
	(struct sieve_error_handler *ehandler, const char *location,
		const char *fmt, va_list args)
{
	if ( ehandler == NULL ) return;

	if ( ehandler->parent == NULL && ehandler->log_master )
		sieve_vcopy_master(location, sieve_verror, fmt, args);

	sieve_direct_verror(ehandler, location, fmt, args);
}

void sieve_vwarning
	(struct sieve_error_handler *ehandler, const char *location,
		const char *fmt, va_list args)
{
	if ( ehandler == NULL ) return;

	if ( ehandler->parent == NULL && ehandler->log_master )
		sieve_vcopy_master(location, sieve_vwarning, fmt, args);

	sieve_direct_vwarning(ehandler, location, fmt, args);
}

void sieve_vinfo
	(struct sieve_error_handler *ehandler, const char *location,
		const char *fmt, va_list args)
{
	if ( ehandler == NULL ) return;

	if ( ehandler->parent == NULL && ehandler->log_master )
		sieve_vcopy_master(location, sieve_vinfo, fmt, args);

	sieve_direct_vinfo(ehandler, location, fmt, args);
}

void sieve_vdebug
	(struct sieve_error_handler *ehandler, const char *location,
		const char *fmt, va_list args)
{
	if ( ehandler == NULL ) return;

	if ( ehandler->parent == NULL && ehandler->log_master )
		sieve_vcopy_master(location, sieve_vdebug, fmt, args);

	sieve_direct_vdebug(ehandler, location, fmt, args);
}

void sieve_vcritical
	(struct sieve_error_handler *ehandler, const char *location,
		const char *fmt, va_list args)
{
	char str[256];
	struct tm *tm;

	tm = localtime(&ioloop_time);

	if ( location == NULL || *location == '\0' )
		sieve_sys_error("%s", t_strdup_vprintf(fmt, args));
	else
		sieve_sys_error("%s: %s", location, t_strdup_vprintf(fmt, args));

	if ( ehandler == NULL ) return;

	sieve_error(ehandler, location, "%s",
		strftime(str, sizeof(str), CRITICAL_MSG_STAMP, tm) > 0 ?
			str : CRITICAL_MSG );
}

void sieve_error
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	T_BEGIN { sieve_verror(ehandler, location, fmt, args); } T_END;

	va_end(args);
}

void sieve_warning
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	T_BEGIN { sieve_vwarning(ehandler, location, fmt, args); } T_END;

	va_end(args);
}

void sieve_info
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	T_BEGIN { sieve_vinfo(ehandler, location, fmt, args); } T_END;

	va_end(args);
}

void sieve_debug
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	T_BEGIN { sieve_vdebug(ehandler, location, fmt, args); } T_END;

	va_end(args);
}

void sieve_critical
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	T_BEGIN { sieve_vcritical(ehandler, location, fmt, args); } T_END;

	va_end(args);
}

/*
 * Error statistics
 */

unsigned int sieve_get_errors(struct sieve_error_handler *ehandler)
{
	if ( ehandler == NULL || ehandler->pool == NULL ) return 0;

	return ehandler->errors;
}

unsigned int sieve_get_warnings(struct sieve_error_handler *ehandler)
{
	if ( ehandler == NULL || ehandler->pool == NULL ) return 0;

	return ehandler->errors;
}

bool sieve_errors_more_allowed(struct sieve_error_handler *ehandler)
{
	if ( ehandler == NULL || ehandler->pool == NULL )
		return TRUE;

	return ehandler->max_errors == 0 || ehandler->errors < ehandler->max_errors;
}

/*
 * Error handler configuration
 */

void sieve_error_handler_accept_infolog
(struct sieve_error_handler *ehandler, bool enable)
{
	while ( ehandler != NULL ) {
		ehandler->log_info = enable;
		ehandler = ehandler->parent;
	}
}

void sieve_error_handler_accept_debuglog
(struct sieve_error_handler *ehandler, bool enable)
{
	while ( ehandler != NULL ) {
		ehandler->log_debug = enable;
		ehandler = ehandler->parent;
	}
}

void sieve_error_handler_copy_masterlog
(struct sieve_error_handler *ehandler, bool enable)
{
	while ( ehandler != NULL ) {
		ehandler->log_master = enable;
		ehandler = ehandler->parent;
	}
}

/*
 * Error handler init
 */

void sieve_error_handler_init
(struct sieve_error_handler *ehandler, pool_t pool, unsigned int max_errors)
{
	ehandler->pool = pool;
	ehandler->refcount = 1;
	ehandler->max_errors = max_errors;

	ehandler->errors = 0;
	ehandler->warnings = 0;
}

void sieve_error_handler_init_from_parent
(struct sieve_error_handler *ehandler, pool_t pool, struct sieve_error_handler *parent)
{
	i_assert( parent != NULL );

	sieve_error_handler_init(ehandler, pool, parent->max_errors);

	ehandler->parent = parent;
	sieve_error_handler_ref(parent);

	ehandler->log_master = parent->log_master;
	ehandler->log_info = parent->log_info;
	ehandler->log_debug = parent->log_debug;
}

void sieve_error_handler_ref(struct sieve_error_handler *ehandler)
{
	if ( ehandler == NULL || ehandler->pool == NULL ) return;

	ehandler->refcount++;
}

void sieve_error_handler_unref(struct sieve_error_handler **ehandler)
{
	if ( *ehandler == NULL || (*ehandler)->pool == NULL ) return;

	i_assert((*ehandler)->refcount > 0);

	if (--(*ehandler)->refcount != 0)
		return;

	if ( (*ehandler)->parent != NULL )
		sieve_error_handler_unref(&(*ehandler)->parent);

	if ( (*ehandler)->free != NULL )
		(*ehandler)->free(*ehandler);

	pool_unref(&((*ehandler)->pool));

	*ehandler = NULL;
}

void sieve_error_handler_reset(struct sieve_error_handler *ehandler)
{
	if ( ehandler == NULL || ehandler->pool == NULL ) return;

	ehandler->errors = 0;
	ehandler->warnings = 0;
}

/*
 * Master/System error handler
 *
 * - Output errors directly to Dovecot master log
 */

static void sieve_master_verror
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->log_master ) return;

	if ( location == NULL || *location == '\0' )
		i_error("sieve: %s", t_strdup_vprintf(fmt, args));
	else
		i_error("sieve: %s: %s", location, t_strdup_vprintf(fmt, args));
}

static void sieve_master_vwarning
(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->log_master ) return;

	if ( location == NULL || *location == '\0' )
		i_warning("sieve: %s", t_strdup_vprintf(fmt, args));
	else
		i_warning("sieve: %s: %s", location, t_strdup_vprintf(fmt, args));
}

static void sieve_master_vinfo
(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->log_master ) return;

	if ( location == NULL || *location == '\0' )
		i_info("sieve: %s", t_strdup_vprintf(fmt, args));
	else
		i_info("sieve: %s: %s", location, t_strdup_vprintf(fmt, args));
}

struct sieve_error_handler *sieve_master_ehandler_create
(unsigned int max_errors)
{
	pool_t pool;
	struct sieve_error_handler *ehandler;

	/* Pool is not strictly necessary, but other handler types will need
	 * a pool, so this one will have one too.
	 */
	pool = pool_alloconly_create
		("master_error_handler", sizeof(struct sieve_error_handler));
	ehandler = p_new(pool, struct sieve_error_handler, 1);
	sieve_error_handler_init(ehandler, pool, max_errors);

	ehandler->verror = sieve_master_verror;
	ehandler->vwarning = sieve_master_vwarning;
	ehandler->vinfo = sieve_master_vinfo;
	ehandler->vdebug = sieve_master_vinfo;

	return ehandler;
}

struct sieve_error_handler _sieve_system_ehandler_object = {
	NULL, 0, NULL, 0, 0, 0,
	FALSE,
	TRUE,
	TRUE,
	sieve_master_verror,
	sieve_master_vwarning,
	sieve_master_vinfo,
	sieve_master_vinfo,
	NULL
};

struct sieve_error_handler *_sieve_system_ehandler = &_sieve_system_ehandler_object;

void sieve_system_ehandler_set(struct sieve_error_handler *ehandler)
{
	sieve_error_handler_unref(&_sieve_system_ehandler);
	_sieve_system_ehandler = ehandler;
	sieve_error_handler_ref(_sieve_system_ehandler);
}

void sieve_system_ehandler_reset(void)
{
	sieve_error_handler_unref(&_sieve_system_ehandler);
	_sieve_system_ehandler = &_sieve_system_ehandler_object;
}

/*
 * STDERR error handler
 *
 * - Output errors directly to stderror
 */

static void sieve_stderr_vmessage
(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *prefix,
	const char *location, const char *fmt, va_list args)
{
	if ( location == NULL || *location == '\0' )
		fprintf(stderr, "%s: %s.\n", prefix, t_strdup_vprintf(fmt, args));
	else
		fprintf(stderr, "%s: %s: %s.\n", location, prefix, t_strdup_vprintf(fmt, args));
}

static void sieve_stderr_verror
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	sieve_stderr_vmessage(ehandler, "error", location, fmt, args);
}

static void sieve_stderr_vwarning
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	sieve_stderr_vmessage(ehandler, "warning", location, fmt, args);
}

static void sieve_stderr_vinfo
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	sieve_stderr_vmessage(ehandler, "info", location, fmt, args);
}

static void sieve_stderr_vdebug
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	sieve_stderr_vmessage(ehandler, "debug", location, fmt, args);
}

struct sieve_error_handler *sieve_stderr_ehandler_create
(unsigned int max_errors)
{
	pool_t pool;
	struct sieve_error_handler *ehandler;

	/* Pool is not strictly necessary, but other handler types will need
	 * a pool, so this one will have one too.
	 */
	pool = pool_alloconly_create
		("stderr_error_handler", sizeof(struct sieve_error_handler));
	ehandler = p_new(pool, struct sieve_error_handler, 1);
	sieve_error_handler_init(ehandler, pool, max_errors);

	ehandler->verror = sieve_stderr_verror;
	ehandler->vwarning = sieve_stderr_vwarning;
	ehandler->vinfo = sieve_stderr_vinfo;
	ehandler->vdebug = sieve_stderr_vdebug;

	return ehandler;
}

/* String buffer error handler
 *
 * - Output errors to a string buffer
 */

struct sieve_strbuf_ehandler {
	struct sieve_error_handler handler;

	string_t *errors;
	bool crlf;
};

static void sieve_strbuf_vmessage
(struct sieve_error_handler *ehandler, const char *prefix,
	const char *location, const char *fmt, va_list args)
{
	struct sieve_strbuf_ehandler *handler =
		(struct sieve_strbuf_ehandler *) ehandler;

	if ( location != NULL && *location != '\0' )
		str_printfa(handler->errors, "%s: ", location);
	str_printfa(handler->errors, "%s: ", prefix);
	str_vprintfa(handler->errors, fmt, args);

	if ( !handler->crlf )
		str_append(handler->errors, ".\n");
	else
		str_append(handler->errors, ".\r\n");
}

static void sieve_strbuf_verror
(struct sieve_error_handler *ehandler, const char *location,
    const char *fmt, va_list args)
{
	sieve_strbuf_vmessage(ehandler, "error", location, fmt, args);
}

static void sieve_strbuf_vwarning
(struct sieve_error_handler *ehandler, const char *location,
    const char *fmt, va_list args)
{
	sieve_strbuf_vmessage(ehandler, "warning", location, fmt, args);
}

static void sieve_strbuf_vinfo
(struct sieve_error_handler *ehandler, const char *location,
    const char *fmt, va_list args)
{
	sieve_strbuf_vmessage(ehandler, "info", location, fmt, args);
}

static void sieve_strbuf_vdebug
(struct sieve_error_handler *ehandler, const char *location,
    const char *fmt, va_list args)
{
	sieve_strbuf_vmessage(ehandler, "debug", location, fmt, args);
}

struct sieve_error_handler *sieve_strbuf_ehandler_create
(string_t *strbuf, bool crlf, unsigned int max_errors)
{
	pool_t pool;
	struct sieve_strbuf_ehandler *ehandler;

	pool = pool_alloconly_create("strbuf_error_handler", 256);
	ehandler = p_new(pool, struct sieve_strbuf_ehandler, 1);
	ehandler->errors = strbuf;

	sieve_error_handler_init(&ehandler->handler, pool, max_errors);

	ehandler->handler.verror = sieve_strbuf_verror;
	ehandler->handler.vwarning = sieve_strbuf_vwarning;
	ehandler->handler.vinfo = sieve_strbuf_vinfo;
	ehandler->handler.vdebug = sieve_strbuf_vdebug;

	ehandler->crlf = crlf;

	return &(ehandler->handler);
}

/*
 * Logfile error handler
 *
 * - Output errors to a log file
 */

struct sieve_logfile_ehandler {
	struct sieve_error_handler handler;

	const char *logfile;
	bool started;
	int fd;
	struct ostream *stream;
};

static void sieve_logfile_vprintf
(struct sieve_logfile_ehandler *ehandler, const char *location,
	const char *prefix, const char *fmt, va_list args)
{
	string_t *outbuf;
	ssize_t ret = 0, remain;
	const char *data;

	if ( ehandler->stream == NULL ) return;

	T_BEGIN {
		outbuf = t_str_new(256);
		if ( location != NULL && *location != '\0' )
			str_printfa(outbuf, "%s: ", location);
		str_printfa(outbuf, "%s: ", prefix);
		str_vprintfa(outbuf, fmt, args);
		str_append(outbuf, ".\n");

		remain = str_len(outbuf);
		data = (const char *) str_data(outbuf);

		while ( remain > 0 ) {
			if ( (ret=o_stream_send(ehandler->stream, data, remain)) < 0 )
				break;

			remain -= ret;
			data += ret;
		}
	} T_END;

	if ( ret < 0 ) {
		sieve_sys_error(
			"o_stream_send() failed on logfile %s: %m", ehandler->logfile);
	}
}

inline static void sieve_logfile_printf
(struct sieve_logfile_ehandler *ehandler, const char *location, const char *prefix,
	const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	sieve_logfile_vprintf(ehandler, location, prefix, fmt, args);

	va_end(args);
}

static void sieve_logfile_start(struct sieve_logfile_ehandler *ehandler)
{
	int fd;
	struct ostream *ostream = NULL;
	struct stat st;
	struct tm *tm;
	char buf[256];
	time_t now;

	/* Open the logfile */

	fd = open(ehandler->logfile, O_CREAT | O_APPEND | O_WRONLY, 0600);
	if (fd == -1) {
		if ( errno == EACCES ) {
			sieve_sys_error("failed to open logfile (LOGGING TO STDERR): %s",
				eacces_error_get_creating("open", ehandler->logfile));
		} else {
			sieve_sys_error("failed to open logfile (LOGGING TO STDERR): "
				"open(%s) failed: %m", ehandler->logfile);
		}
		fd = STDERR_FILENO;
	} else {
		/* fd_close_on_exec(fd, TRUE); Necessary? */

		/* Stat the log file to obtain size information */
		if ( fstat(fd, &st) != 0 ) {
			sieve_sys_error("failed to stat logfile (logging to STDERR): "
				"fstat(fd=%s) failed: %m", ehandler->logfile);

			if ( close(fd) < 0 ) {
				sieve_sys_error("failed to close logfile after error: "
					"close(fd=%s) failed: %m", ehandler->logfile);
			}

			fd = STDERR_FILENO;
		}

		/* Rotate log when it has grown too large */
		if ( st.st_size >= LOGFILE_MAX_SIZE ) {
			const char *rotated;

			/* Close open file */
			if ( close(fd) < 0 ) {
				sieve_sys_error("failed to close logfile: close(fd=%s) failed: %m",
					ehandler->logfile);
			}

			/* Rotate logfile */
			rotated = t_strconcat(ehandler->logfile, ".0", NULL);
			if ( rename(ehandler->logfile, rotated) < 0 ) {
				sieve_sys_error("failed to rotate logfile: rename(%s, %s) failed: %m",
					ehandler->logfile, rotated);
			}

			/* Open clean logfile (overwrites existing if rename() failed earlier) */
			fd = open(ehandler->logfile, O_CREAT | O_WRONLY | O_TRUNC, 0600);
			if (fd == -1) {
				if ( errno == EACCES ) {
					sieve_sys_error("failed to open logfile (LOGGING TO STDERR): %s",
						eacces_error_get_creating("open", ehandler->logfile));
				} else {
					sieve_sys_error("failed to open logfile (LOGGING TO STDERR): "
						"open(%s) failed: %m", ehandler->logfile);
				}
				fd = STDERR_FILENO;
			}
		}
	}

	ostream = o_stream_create_fd(fd, 0, FALSE);
	if ( ostream == NULL ) {
		/* Can't we do anything else in this most awkward situation? */
		sieve_sys_error("failed to open log stream on open file: "
			"o_stream_create_fd(fd=%s) failed "
			"(non-critical messages are not logged!)", ehandler->logfile);
	}

	ehandler->fd = fd;
	ehandler->stream = ostream;
	ehandler->started = TRUE;

	if ( ostream != NULL ) {
		now = time(NULL);
		tm = localtime(&now);

		if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", tm) > 0) {
			sieve_logfile_printf(ehandler, "sieve", "info",
				"started log at %s", buf);
		}
	}
}

static void sieve_logfile_verror
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	struct sieve_logfile_ehandler *handler =
		(struct sieve_logfile_ehandler *) ehandler;

	if ( !handler->started ) sieve_logfile_start(handler);

	sieve_logfile_vprintf(handler, location, "error", fmt, args);
}

static void sieve_logfile_vwarning
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	struct sieve_logfile_ehandler *handler =
		(struct sieve_logfile_ehandler *) ehandler;

	if ( !handler->started ) sieve_logfile_start(handler);

	sieve_logfile_vprintf(handler, location, "warning", fmt, args);
}

static void sieve_logfile_vinfo
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	struct sieve_logfile_ehandler *handler =
		(struct sieve_logfile_ehandler *) ehandler;

	if ( !handler->started ) sieve_logfile_start(handler);

	sieve_logfile_vprintf(handler, location, "info", fmt, args);
}

static void sieve_logfile_vdebug
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	struct sieve_logfile_ehandler *handler =
		(struct sieve_logfile_ehandler *) ehandler;

	if ( !handler->started ) sieve_logfile_start(handler);

	sieve_logfile_vprintf(handler, location, "debug", fmt, args);
}

static void sieve_logfile_free
(struct sieve_error_handler *ehandler)
{
	struct sieve_logfile_ehandler *handler =
		(struct sieve_logfile_ehandler *) ehandler;

	if ( handler->stream != NULL ) {
		o_stream_destroy(&(handler->stream));
		if ( handler->fd != STDERR_FILENO ){
			if ( close(handler->fd) < 0 ) {
				sieve_sys_error("failed to close logfile: "
					"close(fd=%s) failed: %m", handler->logfile);
			}
		}
	}
}

struct sieve_error_handler *sieve_logfile_ehandler_create
(const char *logfile, unsigned int max_errors)
{
	pool_t pool;
	struct sieve_logfile_ehandler *ehandler;

	pool = pool_alloconly_create("logfile_error_handler", 256);
	ehandler = p_new(pool, struct sieve_logfile_ehandler, 1);
	sieve_error_handler_init(&ehandler->handler, pool, max_errors);

	ehandler->handler.verror = sieve_logfile_verror;
	ehandler->handler.vwarning = sieve_logfile_vwarning;
	ehandler->handler.vinfo = sieve_logfile_vinfo;
	ehandler->handler.vdebug = sieve_logfile_vdebug;
	ehandler->handler.free = sieve_logfile_free;

	/* Don't open logfile until something is actually logged.
	 * Let's not pullute the sieve directory with useless logfiles.
	 */
	ehandler->logfile = p_strdup(pool, logfile);
	ehandler->started = FALSE;
	ehandler->stream = NULL;
	ehandler->fd = -1;

	return &(ehandler->handler);
}

/*
 * Prefix error handler
 *
 *   Encapsulates an existing error handler and prefixes all messages with
 *   the given prefix.
 */

struct sieve_prefix_ehandler {
	struct sieve_error_handler handler;

	struct sieve_error_handler *parent;

	const char *location;
	const char *prefix;
};

static const char *_prefix_message
(struct sieve_prefix_ehandler *ehandler,
	const char *location, const char *fmt, va_list args)
{
	string_t *str = t_str_new(256);

	if ( ehandler->prefix != NULL)
		str_printfa(str, "%s: ", ehandler->prefix);
	if ( location != NULL)
		str_printfa(str, "%s: ", location);
	str_vprintfa(str, fmt, args);

	return str_c(str);
}

static void sieve_prefix_verror
(struct sieve_error_handler *_ehandler, const char *location,
	const char *fmt, va_list args)
{
    struct sieve_prefix_ehandler *ehandler =
        (struct sieve_prefix_ehandler *) _ehandler;

	if ( _ehandler->parent == NULL ) return;

	sieve_error(_ehandler->parent, ehandler->location, "%s",
		_prefix_message(ehandler, location, fmt, args));
}

static void sieve_prefix_vwarning
(struct sieve_error_handler *_ehandler, const char *location,
	const char *fmt, va_list args)
{
    struct sieve_prefix_ehandler *ehandler =
        (struct sieve_prefix_ehandler *) _ehandler;

	if ( _ehandler->parent == NULL ) return;

	sieve_warning(_ehandler->parent, ehandler->location, "%s",
		_prefix_message(ehandler, location, fmt, args));
}

static void sieve_prefix_vinfo
(struct sieve_error_handler *_ehandler, const char *location,
	const char *fmt, va_list args)
{
    struct sieve_prefix_ehandler *ehandler =
        (struct sieve_prefix_ehandler *) _ehandler;

	if ( _ehandler->parent == NULL ) return;

	sieve_direct_info(_ehandler->parent, ehandler->location, "%s",
		_prefix_message(ehandler, location, fmt, args));
}

static void sieve_prefix_vdebug
(struct sieve_error_handler *_ehandler, const char *location,
	const char *fmt, va_list args)
{
    struct sieve_prefix_ehandler *ehandler =
        (struct sieve_prefix_ehandler *) _ehandler;

	if ( _ehandler->parent == NULL ) return;

	sieve_direct_debug(_ehandler->parent, ehandler->location, "%s",
		_prefix_message(ehandler, location, fmt, args));
}

struct sieve_error_handler *sieve_prefix_ehandler_create
(struct sieve_error_handler *parent, const char *location, const char *prefix)
{
	pool_t pool;
	struct sieve_prefix_ehandler *ehandler;

	if ( parent == NULL )
		return NULL;

	pool = pool_alloconly_create("sieve_prefix_error_handler", 256);
	ehandler = p_new(pool, struct sieve_prefix_ehandler, 1);
	sieve_error_handler_init_from_parent(&ehandler->handler, pool, parent);

	ehandler->location = p_strdup(pool, location);
	ehandler->prefix = p_strdup(pool, prefix);

	ehandler->handler.verror = sieve_prefix_verror;
	ehandler->handler.vwarning = sieve_prefix_vwarning;
	ehandler->handler.vinfo = sieve_prefix_vinfo;
	ehandler->handler.vdebug = sieve_prefix_vdebug;

	return &(ehandler->handler);
}

/*
 * Varexpand error handler
 *
 *   Encapsulates an existing error handler and formats all messages using the
 *   provided format string and variables;
 */

struct sieve_varexpand_ehandler {
	struct sieve_error_handler handler;

	const char *format;
	ARRAY_DEFINE(table, struct var_expand_table);
};

static const char *_expand_message
(struct sieve_error_handler *_ehandler,
	const char *location, const char *fmt, va_list args)
{
    struct sieve_varexpand_ehandler *ehandler =
        (struct sieve_varexpand_ehandler *) _ehandler;
	unsigned int count;
	struct var_expand_table *table = array_get_modifiable(&ehandler->table, &count);
	string_t *str = t_str_new(256);

	/* Fill in substitution items */
	table[0].value = t_strdup_vprintf(fmt, args);
	table[1].value = location;

	/* Expand variables */
	var_expand(str, ehandler->format, table);

	return str_c(str);
}

static void sieve_varexpand_verror
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->parent == NULL ) return;

	sieve_error(ehandler->parent, location, "%s",
		_expand_message(ehandler, location, fmt, args));
}

static void sieve_varexpand_vwarning
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->parent == NULL ) return;

	sieve_warning(ehandler->parent, location, "%s",
		_expand_message(ehandler, location, fmt, args));
}

static void sieve_varexpand_vinfo
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->parent == NULL ) return;

	sieve_info(ehandler->parent, location, "%s",
		_expand_message(ehandler, location, fmt, args));
}

static void sieve_varexpand_vdebug
(struct sieve_error_handler *ehandler, const char *location,
	const char *fmt, va_list args)
{
	if ( ehandler->parent == NULL ) return;

	sieve_direct_debug(ehandler->parent, location, "%s",
		_expand_message(ehandler, location, fmt, args));
}

struct sieve_error_handler *sieve_varexpand_ehandler_create
(struct sieve_error_handler *parent, const char *format,
	struct var_expand_table *table)
{
	pool_t pool;
	struct sieve_varexpand_ehandler *ehandler;
	struct var_expand_table *entry;
	int i;

	if ( parent == NULL )
		return NULL;

	if ( format == NULL ) {
		sieve_error_handler_ref(parent);
		return parent;
	}

	pool = pool_alloconly_create("sieve_varexpand_error_handler", 256);
	ehandler = p_new(pool, struct sieve_varexpand_ehandler, 1);
	sieve_error_handler_init_from_parent(&ehandler->handler, pool, parent);

	ehandler->format = format;
	p_array_init(&ehandler->table, pool, 10);

	entry = array_append_space(&ehandler->table);
	entry->key = '$';
	entry = array_append_space(&ehandler->table);
	entry->key = 'l';
	entry->long_key = "location";

	for (i = 0; table[i].key != '\0'; i++) {
		entry = array_append_space(&ehandler->table);

		/* Sanitize substitution items */
		entry->key = table[i].key;

		if ( table[i].value != NULL )
			entry->value = p_strdup(pool, table[i].value);
		if ( table[i].long_key != NULL )
			entry->long_key = p_strdup(pool, table[i].long_key);
	}

	(void)array_append_space(&ehandler->table);

	ehandler->handler.verror = sieve_varexpand_verror;
	ehandler->handler.vwarning = sieve_varexpand_vwarning;
	ehandler->handler.vinfo = sieve_varexpand_vinfo;
	ehandler->handler.vdebug = sieve_varexpand_vdebug;

	return &(ehandler->handler);
}