chiark / gitweb /
Import release 0.1.10
[secnet.git] / conffile.c
index 9e10efa2e3881c36db72afd9223074e72f3351d8..f33354e6e075b36b38e2d20ad21c3c6ab9f2d5e1 100644 (file)
 #include "conffile.h"
 #include "conffile_internal.h"
 #include "util.h"
-#include "modules.h"
+#include "ipaddr.h"
+
+/* from modules.c */
+extern void init_builtin_modules(dict_t *dict);
 
 static struct cloc no_loc={"none",0};
 
@@ -332,6 +335,9 @@ static list_t *process_invocation(dict_t *context, struct p_node *i)
     if (cl->type != t_closure) {
        cfgfatal(i->l->loc,"conffile","only closures can be invoked\n");
     }
+    if (!cl->data.closure->apply) {
+       cfgfatal(i->l->loc,"conffile","this closure cannot be invoked\n");
+    }
     args=process_ilist(context, i->r);
     return cl->data.closure->apply(cl->data.closure, i->loc, context, args);
 }
@@ -551,10 +557,36 @@ list_t *list_new(void)
     return NULL;
 }
 
+uint32_t list_length(list_t *a)
+{
+    uint32_t l=0;
+    list_t *i;
+    for (i=a; i; i=i->next) l++;
+    return l;
+}
+
+list_t *list_copy(list_t *a)
+{
+    list_t *r, *i, *b, *l;
+
+    if (!a) return NULL;
+    l=NULL;
+    r=NULL;
+    for (i=a; i; i=i->next) {
+       b=safe_malloc(sizeof(*b),"list_copy");
+       if (l) l->next=b; else r=b;
+       l=b;
+       b->item=i->item;
+       b->next=NULL;
+    }
+    return r;
+}
+
 list_t *list_append_list(list_t *a, list_t *b)
 {
     list_t *i;
 
+    b=list_copy(b);
     if (!a) return b;
     for (i=a; i->next; i=i->next);
     i->next=b;
@@ -696,6 +728,13 @@ static struct subnet string_to_subnet(item_t *i, string_t desc)
        cfgfatal(i->loc,desc,"expecting a string (subnet specification)\n");
     }
 
+    if (strcmp(i->data.string,"default")==0) {
+       s.prefix=0;
+       s.mask=0;
+       s.len=0;
+       return s;
+    }
+
     /* We expect strings of the form "a.b.c.d[/n]", i.e. the dots are
        NOT optional. The subnet mask is optional; if missing it is assumed
        to be /32. */
@@ -711,7 +750,7 @@ static struct subnet string_to_subnet(item_t *i, string_t desc)
        cfgfatal(i->loc,desc,"\"%s\": range error\n",i->data.string);
     }
     s.prefix=(a<<24)|(b<<16)|(c<<8)|(d);
-    s.mask=(~0UL << (32-n));
+    s.mask=n?(~0UL << (32-n)):0;
     s.len=n;
     if (s.prefix & ~s.mask) {
        cfgfatal(i->loc,desc,"\"%s\": prefix not fully contained "
@@ -757,7 +796,7 @@ void dict_read_subnet_list(dict_t *dict, string_t key, bool_t required,
        cfgfatal(loc,desc,"required parameter \"%s\" not found\n",key);
     }
     /* Count the items in the list */
-    for (li=l; li; li=li->next) e++;
+    e=list_length(l);
     if (e==0) return;
     sl->entries=e;
     sl->list=safe_malloc(sizeof(struct subnet)*e, "dict_read_subnet_list");
@@ -773,6 +812,17 @@ void dict_read_subnet_list(dict_t *dict, string_t key, bool_t required,
     }
 }
 
+uint32_t string_to_word(string_t s, struct cloc loc,
+                       struct flagstr *f, string_t desc)
+{
+    struct flagstr *j;
+    for (j=f; j->name; j++)
+       if (strcmp(s,j->name)==0)
+           return j->value;
+    cfgfatal(loc,desc,"option \"%s\" not known\n",s);
+    return 0;
+}
+
 uint32_t string_list_to_word(list_t *l, struct flagstr *f, string_t desc)
 {
     list_t *i;
@@ -785,8 +835,7 @@ uint32_t string_list_to_word(list_t *l, struct flagstr *f, string_t desc)
                     "strings\n");
        }
        for (j=f; j->name; j++)
-           if (strcmp(i->item->data.string,j->name)==0)
-               r|=j->value;
+           r|=string_to_word(i->item->data.string,i->item->loc,f,desc);
     }
     return r;
 }