aboutsummaryrefslogtreecommitdiff
path: root/src/rcfile.c
blob: 988303324dfc9105ae90b77949c5e51e518b0298 (plain)
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
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
/*
   rcfile.c

   This file is part of GNU Anubis.
   Copyright (C) 2001-2014 The Anubis Team.

   GNU Anubis is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   GNU Anubis is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with GNU Anubis.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "headers.h"
#include "extern.h"
#include "rcfile.h"

#define setbool(env, a, b, c)						\
  do {									\
    if (strcasecmp("yes", a) == 0)					\
      (b) |= (c);							\
    else if (strcasecmp("no", a) == 0)					\
      (b) &= ~(c);							\
    else								\
      eval_error (0, env, _("expected `yes' or `no', but found %s"), a); \
  }									\
  while (0)

#define if_empty_set(a, b, c)			\
  do {						\
    if (strlen(a) == 0) {			\
      (b) &= ~(c);				\
    } else {					\
      (b) |= (c);				\
    }						\
  } while (0)

#define MAX_SECTIONS 10

static RC_SECTION *parse_tree;
static time_t global_mtime;
static struct rc_secdef anubis_rc_sections[MAX_SECTIONS];
static int anubis_rc_numsections;

struct rc_secdef *
anubis_add_section (char *name)
{
  int i;

  if (anubis_rc_numsections >= MAX_SECTIONS)
    abort ();
  /*FIXME*/
  for (i = 0; i < anubis_rc_numsections; i++)
    if (strcmp (anubis_rc_sections[i].name, name) == 0)
      return &anubis_rc_sections[i];

  anubis_rc_sections[anubis_rc_numsections].name = name;
  anubis_rc_sections[anubis_rc_numsections].allow_prog = 0;
  anubis_rc_sections[anubis_rc_numsections].prio = prio_user_only;
  anubis_rc_sections[anubis_rc_numsections].child = NULL;
  return &anubis_rc_sections[anubis_rc_numsections++];
}

struct rc_secdef *
anubis_find_section (char *name)
{
  int i;
  for (i = 0; i < anubis_rc_numsections; i++)
    if (strcmp (anubis_rc_sections[i].name, name) == 0)
      return &anubis_rc_sections[i];
  return NULL;
}

void
anubis_section_set_prio (char *name, enum section_prio prio)
{
  struct rc_secdef *p = anubis_find_section (name);
  if (p)
    p->prio = prio;
}

/* A structure uniquely identifying a file */
struct file_id
{
  dev_t dev;			/* Device number */
  ino_t ino;			/* I-node number */
};

/* A list of struct file_id used to prevent duplicate parsing of the
   same file */
static ANUBIS_LIST file_id_list;

/* Comparator for two struct file_id */
static int
cmp_fid (void *a, void *b)
{
  struct file_id *fid_a = a, *fid_b = b;
  return !(fid_a->dev == fid_b->dev && fid_a->ino == fid_b->ino);
}

/* Adds the `filename' to file_id_list.
   Returns 0 if the operation passed successfully, 1 -- if the file
   is already present in the list, and -1 on error */
static int
file_id_add (char *filename)
{
  struct stat st;
  struct file_id *fid;
  if (stat (filename, &st))
    {
      anubis_error (0, errno, _("cannot stat file `%s'"), filename);
      return -1;
    }
  fid = xmalloc (sizeof (*fid));
  fid->dev = st.st_dev;
  fid->ino = st.st_ino;
  if (list_locate (file_id_list, fid, cmp_fid))
    {
      free (fid);
      if (options.termlevel == DEBUG)
	fprintf (stderr, _("File `%s' has already been read.\n"), filename);
      return 1;
    }
  if (!file_id_list)
    file_id_list = list_create ();
  list_append (file_id_list, fid);
  return 0;
}

static void
file_id_destroy ()
{
  list_destroy (&file_id_list, anubis_free_list_item, NULL);
}

void
auth_tunnel (void) /* FIXME: Change to a proper name */
{
  info (NORMAL, _("Welcome user %s !"), session.clientname);
  open_rcfile (CF_CLIENT);
  process_rcfile (CF_CLIENT);
}

void
open_rcfile (int method)
{
  char homedir[MAXPATHLEN + 1];
  char *rcfile = 0;
  RC_SECTION *sec;

  switch (method) {
  case CF_INIT:
  case CF_SUPERVISOR:
    if (topt & T_ALTRC)
      {
	rcfile = strdup (options.altrc);
      }
    else if (check_superuser ())
      rcfile = strdup (DEFAULT_GLOBAL_RCFILE);
    else
      {
	get_homedir (session.supervisor, homedir, sizeof (homedir));
	rcfile = xmalloc (strlen (homedir) +
			  strlen (DEFAULT_LOCAL_RCFILE) + 2);
	sprintf (rcfile, "%s/%s", homedir, DEFAULT_LOCAL_RCFILE);
      }
    
    if (check_filename (rcfile, &global_mtime) == 0)
      {
	free (rcfile);
	return;
      }
    rc_section_list_destroy (&parse_tree);
    file_id_destroy ();
    info (VERBOSE, _("Reading system configuration file %s..."), rcfile);
    break;
    
  case CF_CLIENT:
    if ((topt & (T_ALTRC | T_NORC)) == (T_ALTRC | T_NORC))
      {
	rcfile = strdup (options.altrc);
      }
    else
      {
	rcfile = user_rcfile_name ();
      }
    info (VERBOSE, _("Reading user configuration file %s..."), rcfile);
  }
  
  if ((topt & T_RELAX_PERM_CHECK) == 0 && check_filemode (rcfile) == 0)
    {				/* Wrong permissions... */
      free (rcfile);
      return;
    }

  if (file_id_add (rcfile) == 0)
    {
      sec = rc_parse (rcfile);
      if (sec)
	rc_section_link (&parse_tree, sec);
    }
  free (rcfile);
}

void
process_rcfile (int method)
{
  rcfile_process_section (method, "CONTROL", NULL, NULL);
#ifdef WITH_GUILE
  rcfile_process_section (method, "GUILE", NULL, NULL);
#endif
#if defined(WITH_GSASL)
  rcfile_process_section (method, "AUTH", NULL, NULL);
#endif
}


/* ************************** The CONTROL Section ************************* */
#define KW_BIND                      0
#define KW_TERMLEVEL                 1
#define KW_LOGLEVEL                  2
#define KW_LOGFILE                   3
#define KW_TRACEFILE                 4
#define KW_REMOTE_MTA                5
#define KW_LOCAL_MTA                 6
#define KW_RULE_PRIORITY             7
#define KW_CONTROL_PRIORITY          8
#define KW_ESMTP_AUTH                9
#define KW_DROP_UNKNOWN_USER        10
#define KW_USER_NOTPRIVILEGED       11
#define KW_SOCKS_PROXY              13
#define KW_SOCKS_V4                 14
#define KW_SOCKS_AUTH               15
#define KW_READ_ENTIRE_BODY         16
#define KW_LOCAL_DOMAIN             17
#define KW_MODE                     18
#define KW_ESMTP_ANONYMOUS_TOKEN    19 
#define KW_ESMTP_AUTH_ID            20
#define KW_ESMTP_AUTHZ_ID           21 
#define KW_ESMTP_PASSWORD           22
#define KW_ESMTP_SERVICE            23
#define KW_ESMTP_HOSTNAME           24
#define KW_ESMTP_GENERIC_SERVICE    25
#define KW_ESMTP_PASSCODE           26
#define KW_ESMTP_REALM              27
#define KW_ESMTP_ALLOWED_MECH       28
#define KW_ESMTP_REQUIRE_ENCRYPTION 29
#define KW_INCOMING_MAIL_RULE       30
#define KW_OUTGOING_MAIL_RULE       31
#define KW_SMTP_COMMAND_RULE        32
#define KW_HANG                     33
#define KW_ALLOW_HANG               34
#define KW_LOG_FACILITY             35
#define KW_LOG_TAG                  36
#define KW_ESMTP_AUTH_DELAYED       37

char **
list_to_argv (ANUBIS_LIST  list)
{
  int i, argc;
  char **argv, *p;
  ITERATOR itr;

  argc = list_count (list);
  argv = xmalloc ((argc + 1) * sizeof (argv[0]));
  itr = iterator_create (list);
  for (i = 0, p = iterator_first (itr); p; i++, p = iterator_next (itr))
    argv[i] = strdup (p);
  iterator_destroy (&itr);
  argv[i] = NULL;
  return argv;
}

#ifndef LOG_AUTHPRIV
# define LOG_AUTHPRIV LOG_AUTH
#endif

static void
parse_log_facility (const char *arg)
{
  unsigned long n;
  char *endp;
  struct anubis_keyword kw[] = {
    { "USER",    LOG_USER },   
    { "DAEMON",  LOG_DAEMON },
    { "AUTH",    LOG_AUTH },
    { "AUTHPRIV",LOG_AUTHPRIV },
    { "MAIL",    LOG_MAIL },
    { "CRON",    LOG_CRON },
    { "LOCAL0",  LOG_LOCAL0 },
    { "LOCAL1",  LOG_LOCAL1 },
    { "LOCAL2",  LOG_LOCAL2 },
    { "LOCAL3",  LOG_LOCAL3 },
    { "LOCAL4",  LOG_LOCAL4 },
    { "LOCAL5",  LOG_LOCAL5 },
    { "LOCAL6",  LOG_LOCAL6 },
    { "LOCAL7",  LOG_LOCAL7 },
    { NULL }
  };
  struct anubis_keyword *p;
  
  if (strlen (arg) > 4 && strncasecmp (arg, "LOG_", 4) == 0)
    arg += 4;
  p = anubis_keyword_lookup_ci (kw, arg);
  if (p)
    log_facility = p->tok;
  else if (((n = strtoul (arg, &endp, 0)), *endp == 0)
	   && (log_facility = n) == n)
    /* nothing */;
  else
    anubis_warning (0,
		    _("%s: invalid syslog facility"), arg);
}
  
/* When HANG=NUMBER is set in CONTROL section, `_anubis_hang' is set and
   Anubis will sleep for one second intervals, decrementing `_anubis_hang'
   until it's zero.  Thus you can force the program to continue by attaching
   a debugger and setting it to 0 yourself.  */
static volatile unsigned long _anubis_hang;

/* List of users who are allowed to use HANG in their profiles */
ANUBIS_LIST allow_hang_users; 

static struct rc_kwdef esmtp_kw[] = {
  { "esmtp-auth",   KW_ESMTP_AUTH, KWF_HIDDEN },
  { "esmtp-anonymous-token", KW_ESMTP_ANONYMOUS_TOKEN, KWF_HIDDEN },
  { "esmtp-auth-id", KW_ESMTP_AUTH_ID, KWF_HIDDEN },
  { "esmtp-authz-id", KW_ESMTP_AUTHZ_ID, KWF_HIDDEN },
  { "esmtp-password", KW_ESMTP_PASSWORD, KWF_HIDDEN },
  { "esmtp-service",  KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-hostname", KW_ESMTP_HOSTNAME, KWF_HIDDEN },
  { "esmtp-generic-service", KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-passcode", KW_ESMTP_PASSCODE, KWF_HIDDEN },
  { "esmtp-realm", KW_ESMTP_REALM, KWF_HIDDEN },
  { "esmtp-allowed-mech", KW_ESMTP_ALLOWED_MECH },
  { "esmtp-require-encryption", KW_ESMTP_REQUIRE_ENCRYPTION },
  { NULL }
};

static int
parse_esmtp_kv (int key, ANUBIS_LIST arglist)
{
  char *arg = list_item (arglist, 0);
  switch (key)
    {
#if defined (WITH_GSASL)
    case KW_ESMTP_AUTH:
      {
	char *p = strchr (arg, ':');
	if (p)
	  {
	    *p++ = 0;
	    auth_password = strdup (p);
	    authentication_id = strdup (arg);
	    authorization_id = strdup (arg);
	    topt |= T_ESMTP_AUTH;
	  }
	else
	  topt &= ~T_ESMTP_AUTH;
      }
      break;
      
    case KW_ESMTP_ANONYMOUS_TOKEN:
      anon_token = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_AUTH_ID:
      authentication_id = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_AUTHZ_ID:
      authorization_id = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_PASSWORD:
      auth_password = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_SERVICE:
      auth_service = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_HOSTNAME:
      auth_hostname = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_GENERIC_SERVICE:
      generic_service_name = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_PASSCODE:
      auth_passcode = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_REALM:
      auth_realm = strdup (arg);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_ALLOWED_MECH:
      anubis_set_client_mech_list (arglist);
      topt |= T_ESMTP_AUTH;
      break;
      
    case KW_ESMTP_REQUIRE_ENCRYPTION:
      anubis_set_encryption_mech_list (arglist);
      break;

    default:
      return 1;
#endif 
    }
  return 0;
}

void
control_parser (EVAL_ENV env, int key, ANUBIS_LIST arglist, void *inv_data)
{
  char *arg = list_item (arglist, 0);
  int method = eval_env_method (env);
  
  switch (key)
    {
    case KW_BIND:
      parse_mtahost (arg, &session.anubis, &session.anubis_port);
      if (session.anubis && strlen (session.anubis) != 0)
	topt |= T_NAMES;
      break;
      
    case KW_RULE_PRIORITY:
      if (strcasecmp (arg, "user") == 0)
	anubis_section_set_prio ("RULE", prio_user);
      else if (strcasecmp (arg, "user-only") == 0)
	anubis_section_set_prio ("RULE", prio_user_only);
      else if (strcasecmp (arg, "system") == 0)
	anubis_section_set_prio ("RULE", prio_system);
      else if (strcasecmp (arg, "system-only") == 0)
	anubis_section_set_prio ("RULE", prio_system_only);
      else
	eval_error (0, env, _("invalid rule priority"));
      break;
      
    case KW_CONTROL_PRIORITY:
      if (strcasecmp (arg, "user") == 0)
	anubis_section_set_prio ("CONTROL", prio_user);
      else if (strcasecmp (arg, "system") == 0)
	anubis_section_set_prio ("CONTROL", prio_system);
      else
	eval_error (0, env, _("invalid control priority"));
      break;
      
    case KW_TERMLEVEL:
      if (strcasecmp ("silent", arg) == 0)
	options.termlevel = SILENT;
      else if (strcasecmp ("normal", arg) == 0)
	options.termlevel = NORMAL;
      else if (strcasecmp ("verbose", arg) == 0)
	options.termlevel = VERBOSE;
      else if (strcasecmp ("debug", arg) == 0)
	options.termlevel = DEBUG;
      else
	eval_error (0, env, _("invalid termlevel"));
      break;
      
    case KW_USER_NOTPRIVILEGED:
      assign_string (&session.notprivileged, arg);
      topt |= T_USER_NOTPRIVIL;
      break;
      
    case KW_LOGFILE:
      if (method == CF_CLIENT)
	{
	  xfree (options.ulogfile);
	  options.ulogfile = xstrdup (arg);
	}
      else if (getpid () == 0)
	eval_warning (env,
		      _("`logfile' directive is ignored in main configuration file"));
      else
	{
	  topt |= T_DISABLE_SYSLOG;
	  xfree (options.ulogfile);
	  options.ulogfile = xstrdup (arg);
	}
      break;
      
    case KW_LOGLEVEL:
      if (strcasecmp ("none", arg) == 0)
	options.uloglevel = NONE;
      else if (strcasecmp ("all", arg) == 0)
	options.uloglevel = ALL;
      else if (strcasecmp ("fails", arg) == 0)
	options.uloglevel = FAILS;
      else
	eval_error (0, env, _("invalid loglevel"));
      break;
      
    case KW_TRACEFILE:
      if (method & (CF_SUPERVISOR | CF_INIT))
	setbool (env, arg, topt, T_TRACEFILE_SYS);
      else if (method == CF_CLIENT)
	{
	  if (strcasecmp ("no", arg) == 0)
	    topt &= ~T_TRACEFILE_USR;
	  else
	    {
	      xfree (options.tracefile);
	      if (strcasecmp ("yes", arg) == 0)
		{
		  if (options.ulogfile)
		    {
		      options.tracefile = strdup (options.ulogfile);
		      topt |= T_TRACEFILE_USR;
		    }
		  else
		    topt &= ~T_TRACEFILE_USR;
		}
	      else
		{
		  options.tracefile = xstrdup (arg);
		  topt |= T_TRACEFILE_USR;
		}
	    }
	}
      break;
      
    case KW_REMOTE_MTA:
      parse_mtaport (arg, &session.mta, &session.mta_port);
      break;
      
    case KW_LOCAL_MTA:
      xfree (session.execpath);
      argcv_free (-1, session.execargs);
      session.execpath = strdup (arg);
      session.execargs = list_to_argv (arglist);
      topt |= T_LOCAL_MTA;
      break;
      
    case KW_LOCAL_DOMAIN:
      anubis_domain = strdup (arg);
      break;

#ifdef USE_SOCKS_PROXY
    case KW_SOCKS_PROXY:
      parse_mtaport (arg, &session.socks, &session.socks_port);
      if_empty_set (session.socks, topt, T_SOCKS);
      break;
      
    case KW_SOCKS_V4:
      setbool (env, arg, topt, T_SOCKS_V4);
      break;
      
    case KW_SOCKS_AUTH:
      {
	char *p = 0;
	p = strchr (arg, ':');
	if (p)
	  {
	    *p++ = 0;
	    assign_string (&session.socks_password, p);
	    assign_string (session.socks_username, arg);
	    topt |= T_SOCKS_AUTH;
	  }
	break;
      }
#endif /* USE_SOCKS_PROXY */

    case KW_READ_ENTIRE_BODY:
      setbool (env, arg, topt, T_ENTIRE_BODY);
      break;
      
    case KW_DROP_UNKNOWN_USER:
      setbool (env, arg, topt, T_DROP_UNKNOWN_USER);
      break;
      
    case KW_MODE:
      if (anubis_mode != anubis_mda) /* Special case. See comment to
					KW_LOCAL_MAILER directive, though */
	{
	  if (list_count (arglist) != 1)
	    eval_error (1, env, _("not enough arguments"));
	  else if (anubis_set_mode (arg))
	    eval_error (0, env, _("invalid mode: %s"), arg);
	}
      break;
      
    case KW_INCOMING_MAIL_RULE:
      incoming_mail_rule = strdup (arg);
      break;
      
    case KW_OUTGOING_MAIL_RULE:
      outgoing_mail_rule = strdup (arg);
      break;

    case KW_SMTP_COMMAND_RULE:
      smtp_command_rule = strdup (arg);
      break;
	
    case KW_LOG_FACILITY:
      parse_log_facility (arg);
      break;
      
    case KW_LOG_TAG:
      log_tag = strdup (arg);
      break;
      
    case KW_ALLOW_HANG:
      {
	char *p;
	ITERATOR itr = iterator_create (arglist);
	
	allow_hang_users = list_create ();
	for (p = iterator_first (itr); p; p = iterator_next (itr))
	  list_append (allow_hang_users, strdup (p));
      }
      break;
      
    case KW_HANG:
      if (list_locate (allow_hang_users, session.clientname, anubis_name_cmp))
	{
	  int keep_termlevel = options.termlevel;
	  
	  _anubis_hang = atoi (arg ? arg : "3600");
	  options.termlevel = DEBUG;
	  eval_warning (env,
			ngettext ("Child process suspended for %lu second",
				  "Child process suspended for %lu seconds",
				  _anubis_hang),
			_anubis_hang);
	  options.termlevel = keep_termlevel;
	  
	  while (_anubis_hang-- > 0)
	    sleep (1);
	}
      else
	anubis_warning (0,
			_("Command HANG is not allowed for user `%s'"),
			session.clientname);
      break;
#if defined (WITH_GSASL)
    case KW_ESMTP_AUTH_DELAYED:
      topt |= T_ESMTP_AUTH;
      setbool (env, arg, topt, T_ESMTP_AUTH_DELAYED);
      break;
#endif
    default:
      if (parse_esmtp_kv (key, arglist))
	eval_error (2, env,
		    _("INTERNAL ERROR at %s:%d: unhandled key %d; "
		      "please report"),
		    __FILE__, __LINE__,
		    key);
    }
}

static struct rc_kwdef init_kw[] = {
  { "bind",         KW_BIND },
  { "local-domain", KW_LOCAL_DOMAIN },
  { "mode",         KW_MODE },
  { "incoming-mail-rule", KW_INCOMING_MAIL_RULE },
  { "outgoing-mail-rule", KW_OUTGOING_MAIL_RULE },
  { "smtp-command-rule", KW_SMTP_COMMAND_RULE },
  { "log-facility", KW_LOG_FACILITY },
  { "log-tag", KW_LOG_TAG },
  { "ALLOW-HANG",   KW_ALLOW_HANG },
  { NULL },
};

static struct rc_secdef_child init_sect_child = {
  NULL,
  CF_INIT,
  init_kw,
  control_parser,
  NULL
};

static struct rc_kwdef init_supervisor_kw[] = {
  { "termlevel",          KW_TERMLEVEL },
  { "user-notprivileged", KW_USER_NOTPRIVILEGED },
  { "drop-unknown-user",  KW_DROP_UNKNOWN_USER },
  { "rule-priority",      KW_RULE_PRIORITY },
  { "control-priority",   KW_CONTROL_PRIORITY },
  { "logfile",            KW_LOGFILE },
  { "loglevel",           KW_LOGLEVEL },
  { NULL }
};

static struct rc_secdef_child init_supervisor_sect_child = {
  NULL,
  CF_INIT | CF_SUPERVISOR,
  init_supervisor_kw,
  control_parser,
  NULL
};

struct rc_kwdef client_kw[] = {
  { "logfile",  KW_LOGFILE },
  { "loglevel", KW_LOGLEVEL },
  { "HANG",     KW_HANG },
  { NULL },
};

static struct rc_secdef_child client_sect_child = {
  NULL,
  CF_CLIENT,
  client_kw,
  control_parser,
  NULL
};

struct rc_kwdef control_kw[] = {
  { "remote-mta",   KW_REMOTE_MTA },
  { "local-mta",    KW_LOCAL_MTA },
  { "tracefile",    KW_TRACEFILE },
  { "esmtp-auth",   KW_ESMTP_AUTH, KWF_HIDDEN },
  { "esmtp-anonymous-token", KW_ESMTP_ANONYMOUS_TOKEN, KWF_HIDDEN },
  { "esmtp-auth-id", KW_ESMTP_AUTH_ID, KWF_HIDDEN },
  { "esmtp-authz-id", KW_ESMTP_AUTHZ_ID, KWF_HIDDEN },
  { "esmtp-password", KW_ESMTP_PASSWORD, KWF_HIDDEN },
  { "esmtp-service",  KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-hostname", KW_ESMTP_HOSTNAME, KWF_HIDDEN },
  { "esmtp-generic-service", KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-passcode", KW_ESMTP_PASSCODE, KWF_HIDDEN },
  { "esmtp-realm", KW_ESMTP_REALM, KWF_HIDDEN },
  { "esmtp-allowed-mech", KW_ESMTP_ALLOWED_MECH },
  { "esmtp-require-encryption", KW_ESMTP_REQUIRE_ENCRYPTION },
  { "esmtp-auth-delayed", KW_ESMTP_AUTH_DELAYED },
#ifdef USE_SOCKS_PROXY
  { "socks-proxy",  KW_SOCKS_PROXY },
  { "socks-v4",     KW_SOCKS_V4 },
  { "socks-auth",   KW_SOCKS_AUTH },
#endif /* USE_SOCKS_PROXY */
  { "read-entire-body", KW_READ_ENTIRE_BODY },
  { NULL },
};

static struct rc_secdef_child control_sect_child = {
  NULL,
  CF_ALL,
  control_kw,
  control_parser,
  NULL
};

/* FIXME: This belongs to another file */
#if defined(USE_GNUTLS)
#define KW_SSL                 1
#define KW_SSL_ONEWAY          2
#define KW_SSL_CERT            3
#define KW_SSL_KEY             4
#define KW_SSL_CAFILE          5

void
tls_parser (EVAL_ENV env, int key, ANUBIS_LIST arglist, void *inv_data)
{
  char *arg = list_item (arglist, 0);
  switch (key)
    {
    case KW_SSL:
      setbool (env, arg, topt, T_SSL);
      break;
      
    case KW_SSL_ONEWAY:
      setbool (env, arg, topt, T_SSL_ONEWAY);
      break;
      
    case KW_SSL_CERT:
      xfree (secure.cert);
      secure.cert = xstrdup (arg);
      break;
      
    case KW_SSL_KEY:
      xfree (secure.key);
      secure.key = xstrdup (arg);
      if (eval_env_method (env) == CF_CLIENT)
	topt |= T_SSL_CKCLIENT;
      break;
      
    case KW_SSL_CAFILE:
      xfree (secure.cafile);
      secure.cafile = xstrdup (arg);
      break;
      
    default:
      eval_error (2, env,
		  _("INTERNAL ERROR at %s:%d: unhandled key %d; "
		    "please report"),
		  __FILE__, __LINE__,
		  key);
  }
}

static struct rc_kwdef tls_kw[] = {
  { "ssl",        KW_SSL },
  { "ssl-oneway", KW_SSL_ONEWAY },
  { "ssl-cert",   KW_SSL_CERT },
  { "ssl-key",    KW_SSL_KEY },
  { "ssl-cafile", KW_SSL_CAFILE },
  { NULL }
};

static struct rc_secdef_child tls_sect_child = {
  NULL,
  CF_ALL,
  tls_kw,
  tls_parser,
  NULL
};
#endif /* USE_GNUTLS */

void
control_section_init (void)
{
  struct rc_secdef *sp = anubis_add_section ("CONTROL");
  sp->prio = prio_system;
  rc_secdef_add_child (sp, &init_sect_child);
  rc_secdef_add_child (sp, &init_supervisor_sect_child);
  rc_secdef_add_child (sp, &client_sect_child);
  rc_secdef_add_child (sp, &control_sect_child);
#if defined(USE_GNUTLS)
  rc_secdef_add_child (sp, &tls_sect_child);
#endif
}

/* ************************** The RULE Section *************************** */
#define KW_SIGNATURE_FILE_APPEND    1
#define KW_BODY_APPEND              2
#define KW_BODY_CLEAR_APPEND        3
#define KW_EXTERNAL_BODY_PROCESSOR  4
#define KW_BODY_CLEAR               5

void
rule_parser (EVAL_ENV env, int key, ANUBIS_LIST arglist, void *inv_data)
{
  MESSAGE msg = eval_env_message (env);
  char *arg = list_item (arglist, 0);
  char **argv;

  switch (key)
    {
    case KW_SIGNATURE_FILE_APPEND:
      if (strcasecmp ("no", arg))
	message_append_signature_file (msg);
      break;
      
    case KW_BODY_APPEND:
      message_append_text_file (msg, arg, NULL);
      break;
      
    case KW_BODY_CLEAR:
      message_replace_body (msg, xstrdup (""));
      break;
      
    case KW_BODY_CLEAR_APPEND:
      message_replace_body (msg, xstrdup (""));
      message_append_text_file (msg, arg, NULL);
      break;
      
    case KW_EXTERNAL_BODY_PROCESSOR:
      argv = list_to_argv (arglist);
      message_external_proc (msg, argv);
      argcv_free (-1, argv);
      break;
      
    default:
      eval_error (2, env,
		  _("INTERNAL ERROR at %s:%d: unhandled key %d; "
		    "please report"),
		  __FILE__, __LINE__,
		  key);
    }
}

struct rc_kwdef rule_kw[] = {
  { "signature-file-append",   KW_SIGNATURE_FILE_APPEND },
  { "body-append",             KW_BODY_APPEND },
  { "body-clear-append",       KW_BODY_CLEAR_APPEND },
  { "body-clear",              KW_BODY_CLEAR },
  { "external-body-processor", KW_EXTERNAL_BODY_PROCESSOR },
  { NULL }
};

static struct rc_secdef_child rule_sect_child = {
  NULL,
  CF_CLIENT,
  rule_kw,
  rule_parser,
  NULL
};

void
rule_section_init (void)
{
  struct rc_secdef *sp = anubis_add_section ("RULE");
  sp->allow_prog = 1;
  sp->prio = prio_system;
  rc_secdef_add_child (sp, &rule_sect_child);
}


void
smtp_rule_parser (EVAL_ENV env, int key, ANUBIS_LIST arglist, void *inv_data)
{
  MESSAGE msg = eval_env_message (env);
  char *arg = list_item (arglist, 0);
  char **argv;

  switch (key)
    {
    case KW_SIGNATURE_FILE_APPEND:
      if (strcasecmp ("no", arg))
	message_append_signature_file (msg);
      break;
      
    case KW_BODY_APPEND:
      message_append_text_file (msg, arg, NULL);
      break;
      
    case KW_BODY_CLEAR:
      message_replace_body (msg, xstrdup (""));
      break;
      
    case KW_BODY_CLEAR_APPEND:
      message_replace_body (msg, xstrdup (""));
      message_append_text_file (msg, arg, NULL);
      break;
      
    case KW_EXTERNAL_BODY_PROCESSOR:
      argv = list_to_argv (arglist);
      message_external_proc (msg, argv);
      argcv_free (-1, argv);
      break;
      
    default:
      if (parse_esmtp_kv (key, arglist))
	eval_error (2, env,
		    _("INTERNAL ERROR at %s:%d: unhandled key %d; "
		      "please report"),
		    __FILE__, __LINE__,
		    key);
    }
}

struct rc_kwdef smtp_rule_kw[] = {
  { "signature-file-append",   KW_SIGNATURE_FILE_APPEND },
  { "body-append",             KW_BODY_APPEND },
  { "body-clear-append",       KW_BODY_CLEAR_APPEND },
  { "body-clear",              KW_BODY_CLEAR },
  { "external-body-processor", KW_EXTERNAL_BODY_PROCESSOR },
  /* FIXME: It is supposed that none of the KW_ESMTP defines coincides
     with any of the above */
  { "esmtp-auth",              KW_ESMTP_AUTH, KWF_HIDDEN },
  { "esmtp-anonymous-token",   KW_ESMTP_ANONYMOUS_TOKEN, KWF_HIDDEN },
  { "esmtp-auth-id",           KW_ESMTP_AUTH_ID, KWF_HIDDEN },
  { "esmtp-authz-id",          KW_ESMTP_AUTHZ_ID, KWF_HIDDEN },
  { "esmtp-password",          KW_ESMTP_PASSWORD, KWF_HIDDEN },
  { "esmtp-service",           KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-hostname",          KW_ESMTP_HOSTNAME, KWF_HIDDEN },
  { "esmtp-generic-service",   KW_ESMTP_SERVICE, KWF_HIDDEN },
  { "esmtp-passcode",          KW_ESMTP_PASSCODE, KWF_HIDDEN },
  { "esmtp-realm",             KW_ESMTP_REALM, KWF_HIDDEN },
  { "esmtp-allowed-mech",      KW_ESMTP_ALLOWED_MECH },
  { NULL }
};

static struct rc_secdef_child smtp_rule_sect_child = {
  NULL,
  CF_CLIENT,
  smtp_rule_kw,
  smtp_rule_parser,
  NULL
};

void
smtp_rule_section_init (void)
{
  struct rc_secdef *sp = anubis_add_section ("SMTP");
  sp->allow_prog = 1;
  sp->prio = prio_system;
  rc_secdef_add_child (sp, &smtp_rule_sect_child);
}

void
rc_system_init (void)
{
  control_section_init ();
  translate_section_init ();
  rule_section_init ();
  smtp_rule_section_init ();
#ifdef WITH_GUILE
  guile_section_init ();
#endif
#ifdef HAVE_GPG
  gpg_section_init ();
#endif
#ifdef WITH_GSASL
  authmode_section_init ();
#endif
}

void
rcfile_process_se