Added more 'const'
[freeradius.git] / src / main / conffile.c
index 7c7d425..b56f5ba 100644 (file)
@@ -99,6 +99,9 @@ static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
                                   int flag);
 
+int cf_log_config = 1;
+int cf_log_modules = 1;
+
 /*
  *     Isolate the scary casts in these tiny provably-safe functions
  */
@@ -444,6 +447,42 @@ static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
        return cs;
 }
 
+/*
+ *     Replace pair in a given section with a new pair,
+ *     of the given value.
+ */
+int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, const char *value)
+{
+       CONF_PAIR *newp;
+       CONF_ITEM *ci, *cn, **last;
+
+       newp = cf_pair_alloc(cp->attr, value, cp->operator, cp->value_type,
+                            cs);
+       if (!newp) return -1;
+
+       ci = cf_pairtoitem(cp);
+       cn = cf_pairtoitem(newp);
+
+       /*
+        *      Find the old one from the linked list, and replace it
+        *      with the new one.
+        */
+       for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
+               if (*last == ci) {
+                       cn->next = (*last)->next;
+                       *last = cn;
+                       ci->next = NULL;
+                       break;
+               }
+       }
+
+       rbtree_deletebydata(cs->pair_tree, ci);
+
+       rbtree_insert(cs->pair_tree, cn);
+
+       return 0;
+}
+
 
 /*
  *     Add an item to a configuration section.
@@ -478,11 +517,13 @@ static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
 
                                if (!cs->section_tree) {
                                        cs->section_tree = rbtree_create(section_cmp, NULL, 0);
-                                       /* ignore any errors */
+                                       if (!cs->section_tree) {
+                                               radlog(L_ERR, "Out of memory");
+                                               _exit(1);
+                                       }
                                }
 
-                               if (cs->section_tree) {
-                                       rbtree_insert(cs->section_tree, cs_new);                                }
+                               rbtree_insert(cs->section_tree, cs_new);
 
                                /*
                                 *      Two names: find the named instance.
@@ -561,6 +602,8 @@ CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
                 *      "foo.bar.baz" means "from the root"
                 */
        } else if (strchr(p, '.') != NULL) {
+               if (!parentcs) goto no_such_item;
+
                cs = parentcs;
        }
 
@@ -631,7 +674,7 @@ CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
        /*
         *      "foo" is "in the current section, OR in main".
         */
-       if ((p == name) && (cs != parentcs)) {
+       if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
                cs = parentcs;
                goto retry;
        }
@@ -725,6 +768,11 @@ static const char *cf_expand_variables(const char *cf, int *lineno,
                         *  Substitute the value of the variable.
                         */
                        cp = cf_itemtopair(ci);
+                       if (!cp->value) {
+                               radlog(L_ERR, "%s[%d]: Reference \"%s\" has no value",
+                                      cf, *lineno, input);
+                               return NULL;
+                       }
                        strcpy(p, cp->value);
                        p += strlen(p);
                        ptr = end + 1;
@@ -803,10 +851,10 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
        char **q;
        const char *value;
        fr_ipaddr_t ipaddr;
-       const CONF_PAIR *cp;
+       const CONF_PAIR *cp = NULL;
        char ipbuf[128];
 
-       cp = cf_pair_find(cs, name);
+       if (cs) cp = cf_pair_find(cs, name);
        if (cp) {
                value = cp->value;
 
@@ -838,12 +886,12 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                        radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
                        return -1;
                }
-               DEBUG2("\t%s = %s", name, value);
+               cf_log_info(cs, "\t%s = %s", name, value);
                break;
 
        case PW_TYPE_INTEGER:
                *(int *)data = strtol(value, 0, 0);
-               DEBUG2("\t%s = %d", name, *(int *)data);
+               cf_log_info(cs, "\t%s = %d", name, *(int *)data);
                break;
 
        case PW_TYPE_STRING_PTR:
@@ -860,7 +908,9 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                if (value == dflt) {
                        char buffer[8192];
 
-                       int lineno = cs->item.lineno;
+                       int lineno = 0;
+
+                       if (cs) lineno = cs->item.lineno;
 
                        /*
                         *      FIXME: sizeof(buffer)?
@@ -871,7 +921,7 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                        if (!value) return -1;
                }
 
-               DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
+               cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
                *q = value ? strdup(value) : NULL;
                break;
 
@@ -894,7 +944,9 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                if (value == dflt) {
                        char buffer[8192];
 
-                       int lineno = cs->item.lineno;
+                       int lineno = 0;
+
+                       if (cs) lineno = cs->item.lineno;
 
                        /*
                         *      FIXME: sizeof(buffer)?
@@ -905,13 +957,17 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                        if (!value) return -1;
                }
 
-               DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
+               cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
                *q = value ? strdup(value) : NULL;
 
                /*
                 *      And now we "stat" the file.
+                *
+                *      FIXME: This appears to leak memory on exit,
+                *      and we don't use this information.  So it's
+                *      commented out for now.
                 */
-               if (*q) {
+               if (0 && *q) {
                        struct stat buf;
 
                        if (stat(*q, &buf) == 0) {
@@ -932,7 +988,7 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                 */
                if (strcmp(value, "*") == 0) {
                        *(uint32_t *) data = htonl(INADDR_ANY);
-                       DEBUG2("\t%s = *", name);
+                       cf_log_info(cs, "\t%s = *", name);
                        break;
                }
                if (ip_hton(value, AF_INET, &ipaddr) < 0) {
@@ -941,9 +997,9 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                }
                
                if (strspn(value, "0123456789.") == strlen(value)) {
-                       DEBUG2("\t%s = %s", name, value);
+                       cf_log_info(cs, "\t%s = %s", name, value);
                } else {
-                       DEBUG2("\t%s = %s IP address [%s]", name, value,
+                       cf_log_info(cs, "\t%s = %s IP address [%s]", name, value,
                               ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
                }
                *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
@@ -954,7 +1010,7 @@ int cf_item_parse(CONF_SECTION *cs, const char *name,
                        radlog(L_ERR, "Can't find IPv6 address for host %s", value);
                        return -1;
                }
-               DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
+               cf_log_info(cs, "\t%s = %s IPv6 address [%s]", name, value,
                               ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
                memcpy(data, &ipaddr.ipaddr.ip6addr,
                       sizeof(ipaddr.ipaddr.ip6addr));
@@ -983,10 +1039,10 @@ int cf_section_parse(CONF_SECTION *cs, void *base,
        cs->variables = variables; /* this doesn't hurt anything */
 
        if (!cs->name2) {
-               DEBUG2("%.*s%s {", cs->depth, parse_spaces,
+               cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
                       cs->name1);
        } else {
-               DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
+               cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
                       cs->name1, cs->name2);
        }
 
@@ -1040,14 +1096,14 @@ int cf_section_parse(CONF_SECTION *cs, void *base,
                }
        } /* for all variables in the configuration section */
 
-       DEBUG2("%.*s}", cs->depth, parse_spaces);
+       cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
 
        cs->base = base;
 
        return 0;
 
  error:
-       DEBUG2("%.*s}", cs->depth, parse_spaces);
+       cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
        cf_section_parse_free(cs, base);
        return -1;
 }
@@ -1060,11 +1116,11 @@ int cf_section_parse(CONF_SECTION *cs, void *base,
  *     We're not really parsing it here, just checking if it's mostly
  *     well-formed.
  */
-static int condition_looks_ok(char **ptr)
+static int condition_looks_ok(const char **ptr)
 {
        int num_braces = 1;
        int quote = 0;
-       char *p = *ptr;
+       const char *p = *ptr;
 
        while (*p) {
                if (quote) {
@@ -1171,7 +1227,7 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
 {
        CONF_SECTION *this, *css;
        CONF_PAIR *cpn;
-       char *ptr;
+       const char *ptr;
        const char *value;
        char buf[8192];
        char buf1[8192];
@@ -1187,12 +1243,12 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
         *      Read, checking for line continuations ('\\' at EOL)
         */
        for (;;) {
-               int eof;
+               int at_eof;
 
                /*
                 *      Get data, and remember if we are at EOF.
                 */
-               eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
+               at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
                (*lineno)++;
 
                /*
@@ -1214,14 +1270,14 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                 *      conditions.
                 */
                if (cbuf == buf) {
-                       if (eof) break;
+                       if (at_eof) break;
                        
                        ptr = buf;
                        while (*ptr && isspace((int) *ptr)) ptr++;
 
                        if (!*ptr || (*ptr == '#')) continue;
 
-               } else if (eof || (len == 0)) {
+               } else if (at_eof || (len == 0)) {
                        radlog(L_ERR, "%s[%d]: Continuation at EOF is illegal",
                               filename, *lineno);
                        return -1;
@@ -1243,7 +1299,44 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                }
 
                ptr = cbuf = buf;
-               t1 = gettoken(&ptr, buf1, sizeof(buf1));
+
+               /*
+                *      The parser is getting to be evil.
+                */
+               while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
+
+               if (((ptr[0] == '%') && (ptr[1] == '{')) ||
+                   (ptr[0] == '`')) {
+                       int hack;
+
+                       if (ptr[0] == '%') {
+                               hack = rad_copy_variable(buf1, ptr);
+                       } else {
+                               hack = rad_copy_string(buf1, ptr);
+                       }
+                       if (hack < 0) {
+                               radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
+                                      filename, *lineno, ptr);
+                               return -1;
+                       }
+
+                       t1 = T_BARE_WORD;
+                       ptr += hack;
+
+                       t2 = gettoken(&ptr, buf2, sizeof(buf2));
+                       switch (t2) {
+                       case T_EOL:
+                       case T_HASH:
+                               goto do_bare_word;
+                               
+                       default:
+                               radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
+                                      filename, *lineno, ptr);
+                               return -1;
+                       }
+               } else {
+                       t1 = gettoken(&ptr, buf1, sizeof(buf1));
+               }
 
                /*
                 *      The caller eats "name1 name2 {", and calls us
@@ -1369,12 +1462,19 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
 
               if (strcasecmp(buf1, "$template") == 0) {
                       CONF_ITEM *ci;
-                      CONF_SECTION *parentcs;
+                      CONF_SECTION *parentcs, *templatecs;
                       t2 = getword(&ptr, buf2, sizeof(buf2));
 
                       parentcs = cf_top_section(current);
 
-                      ci = cf_reference_item(parentcs, this, buf2);
+                      templatecs = cf_section_sub_find(parentcs, "templates");
+                      if (!templatecs) {
+                               radlog(L_ERR, "%s[%d]: No \"templates\" section for reference \"%s\"",
+                                      filename, *lineno, buf2);
+                               return -1;
+                      }
+
+                      ci = cf_reference_item(parentcs, templatecs, buf2);
                       if (!ci || (ci->type != CONF_ITEM_SECTION)) {
                                radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
                                       filename, *lineno, buf2);
@@ -1408,6 +1508,7 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                switch (t2) {
                case T_EOL:
                case T_HASH:
+               do_bare_word:
                        t2 = T_OP_EQ;
                        value = NULL;
                        goto do_set;
@@ -1417,6 +1518,7 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                case T_OP_SUB:
                case T_OP_LE:
                case T_OP_GE:
+               case T_OP_CMP_FALSE:
                        if (!this || (strcmp(this->name1, "update") != 0)) {
                                radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
                                       filename, *lineno);
@@ -1427,6 +1529,12 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                case T_OP_SET:
                do_set:
                        t3 = getstring(&ptr, buf3, sizeof(buf3));
+                       if (t3 == T_OP_INVALID) {
+                               radlog(L_ERR, "%s[%d]: Parse error: %s",
+                                      filename, *lineno,
+                                      fr_strerror());
+                               return -1;
+                       }
 
                        /*
                         *      Handle variable substitution via ${foo}
@@ -1461,7 +1569,7 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp,
                case T_LBRACE:
                        if ((strcmp(buf1, "if") == 0) ||
                            (strcmp(buf1, "elsif") == 0)) {
-                               char *end = ptr;
+                               const char *end = ptr;
                                CONF_SECTION *server;
 
                                if (!condition_looks_ok(&end)) {
@@ -1598,6 +1706,12 @@ int cf_file_include(const char *filename, CONF_SECTION *cs)
                return -1;
        }
 
+       if (cf_data_find_internal(cs, filename, PW_TYPE_FILENAME)) {
+               radlog(L_ERR, "Cannot include the same file twice: \"%s\"",
+                      filename);
+               return -1;
+       }
+
        /*
         *      Add the filename to the section
         */
@@ -1606,14 +1720,16 @@ int cf_file_include(const char *filename, CONF_SECTION *cs)
 
        if (cf_data_add_internal(cs, filename, mtime, free,
                                 PW_TYPE_FILENAME) < 0) {
-               radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
+               fclose(fp);
+               radlog(L_ERR|L_CONS, "Internal error opening file \"%s\"",
                       filename);
                return -1;
        }
 
        cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
        if (!cd) {
-               radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
+               fclose(fp);
+               radlog(L_ERR|L_CONS, "Internal error opening file \"%s\"",
                       filename);
                return -1;
        }
@@ -1715,10 +1831,6 @@ const char *cf_pair_value(CONF_PAIR *pair)
        return (pair ? pair->value : NULL);
 }
 
-/*
- *     Copied here for error reporting.
- */
-extern void librad_log(const char *, ...);
 
 /*
  * Turn a CONF_PAIR into a VALUE_PAIR
@@ -1726,35 +1838,38 @@ extern void librad_log(const char *, ...);
  */
 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
 {
-       DICT_ATTR *da;
        VALUE_PAIR *vp;
 
        if (!pair) {
-               librad_log("Internal error");
-               return NULL;
-       }
-
-       da = dict_attrbyname(pair->attr);
-       if (!da) {
-               librad_log("Unknown attribute %s", pair->attr);
+               fr_strerror_printf("Internal error");
                return NULL;
        }
 
        if (!pair->value) {
-               librad_log("No value given for attribute %s", pair->attr);
+               fr_strerror_printf("No value given for attribute %s", pair->attr);
                return NULL;
        }
 
-       vp = pairalloc(da);
+       /*
+        *      pairmake handles tags.  pairalloc() doesn't.
+        */
+       vp = pairmake(pair->attr, NULL, pair->operator);
        if (!vp) {
-               librad_log("Out of memory");
                return NULL;
        }
 
-       vp->operator = pair->operator;
-
-       if ((pair->value_type == T_BARE_WORD) ||
-           (pair->value_type == T_SINGLE_QUOTED_STRING)) {
+       if (pair->value_type == T_BARE_WORD) {
+               if ((vp->type == PW_TYPE_STRING) && 
+                   (pair->value[0] == '0') && (pair->value[1] == 'x')) {
+                       vp->type = PW_TYPE_OCTETS;
+               }
+               if (!pairparsevalue(vp, pair->value)) {
+                       pairfree(&vp);
+                       return NULL;
+               }
+               vp->flags.do_xlat = 0;
+         
+       } else if (pair->value_type == T_SINGLE_QUOTED_STRING) {
                if (!pairparsevalue(vp, pair->value)) {
                        pairfree(&vp);
                        return NULL;
@@ -1849,10 +1964,12 @@ CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
 {
        CONF_ITEM *ci;
 
+       if (!name) return NULL; /* can't find an un-named section */
+
        /*
         *      Do the fast lookup if possible.
         */
-       if (name && cs->section_tree) {
+       if (cs->section_tree) {
                CONF_SECTION mycs;
 
                mycs.name1 = name;
@@ -2110,7 +2227,7 @@ int cf_data_add(CONF_SECTION *cs, const char *name,
        return cf_data_add_internal(cs, name, data, data_free, 0);
 }
 
-
+#if 0
 /*
  *     Copy CONF_DATA from src to dst
  */
@@ -2310,6 +2427,7 @@ int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
 
        return 1;               /* rcode means anything? */
 }
+#endif
 
 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
 {
@@ -2324,7 +2442,8 @@ int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
 /*
  *     This is here to make the rest of the code easier to read.  It
  *     ties conffile.c to log.c, but it means we don't have to
- *     pollute the 
+ *     pollute every other function with the knowledge of the
+ *     configuration internals.
  */
 void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
 {
@@ -2339,6 +2458,39 @@ void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
 }
 
 
+void cf_log_info(CONF_SECTION *cs, const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       if (debug_flag > 1 && cf_log_config && cs) vradlog(L_DBG, fmt, ap);
+       va_end(ap);
+}
+
+/*
+ *     Wrapper to simplify the code.
+ */
+void cf_log_module(CONF_SECTION *cs, const char *fmt, ...)
+{
+       va_list ap;
+       char buffer[256];
+
+       va_start(ap, fmt);
+       if (debug_flag > 1 && cf_log_modules && cs) {
+               vsnprintf(buffer, sizeof(buffer), fmt, ap);
+
+               radlog(L_DBG, " Module: %s", buffer);
+       }
+       va_end(ap);
+}
+
+const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
+{
+       if (!cs) return NULL;
+
+       return cs->variables;
+}
+
 #if 0
 /*
  * JMG dump_config tries to dump the config structure in a readable format
@@ -2389,3 +2541,159 @@ int dump_config(CONF_SECTION *cs)
        return dump_config_section(cs, 0);
 }
 #endif
+
+static const char *cf_pair_print_value(const CONF_PAIR *cp,
+                                      char *buffer, size_t buflen)
+{
+       char *p;
+
+       if (!cp->value) return "";
+
+       switch (cp->value_type) {
+       default:
+       case T_BARE_WORD:
+               snprintf(buffer, buflen, "%s", cp->value);
+               break;
+
+       case T_SINGLE_QUOTED_STRING:
+               snprintf(buffer, buflen, "'%s'", cp->value);
+               break;
+
+       case T_DOUBLE_QUOTED_STRING:
+               buffer[0] = '"';
+               fr_print_string(cp->value, strlen(cp->value),
+                               buffer + 1, buflen - 3);
+               p = buffer + strlen(buffer); /* yuck... */
+               p[0] = '"';
+               p[1] = '\0';
+               break;
+       }
+
+       return buffer;
+}
+
+
+int cf_pair2xml(FILE *fp, const CONF_PAIR *cp)
+{
+       fprintf(fp, "<%s>", cp->attr);
+       if (cp->value) {
+               char buffer[2048];
+
+               char *p = buffer;
+               const char *q = cp->value;
+
+               while (*q && (p < (buffer + sizeof(buffer)))) {
+                       if (q[0] == '&') {
+                               memcpy(p, "&amp;", 4);
+                               p += 5;
+
+                       } else if (q[0] == '<') {
+                               memcpy(p, "&lt;", 4);
+                               p += 4;
+
+                       } else if (q[0] == '>') {
+                               memcpy(p, "&gt;", 4);
+                               p += 4;
+
+                       } else {
+                               *(p++) = *q;
+                       }
+                       q++;
+               }
+
+               *p = '\0';
+               fprintf(fp, "%s", buffer);
+       }
+
+       fprintf(fp, "</%s>\n", cp->attr);
+
+       return 1;
+}
+
+int cf_section2xml(FILE *fp, const CONF_SECTION *cs)
+{
+       CONF_ITEM *ci, *next;
+
+       /*
+        *      Section header
+        */
+       fprintf(fp, "<%s>\n", cs->name1);
+       if (cs->name2) {
+               fprintf(fp, "<_name2>%s</_name2>\n", cs->name2);
+       }
+
+       /*
+        *      Loop over contents.
+        */
+       for (ci = cs->children; ci; ci = next) {
+               next = ci->next;
+
+               switch (ci->type) {
+               case CONF_ITEM_PAIR:
+                       if (!cf_pair2xml(fp, (CONF_PAIR *) ci)) return 0;
+                       break;
+
+               case CONF_ITEM_SECTION:
+                       if (!cf_section2xml(fp, (CONF_SECTION *) ci)) return 0;
+                       break;
+
+               default:        /* should really be an error. */
+                       break;
+               
+               }
+       }
+
+       fprintf(fp, "</%s>\n", cs->name1);
+
+       return 1;               /* success */
+}
+
+int cf_pair2file(FILE *fp, const CONF_PAIR *cp)
+{
+       char buffer[2048];
+
+       fprintf(fp, "\t%s = %s\n", cp->attr,
+               cf_pair_print_value(cp, buffer, sizeof(buffer)));
+
+       return 1;
+}
+
+int cf_section2file(FILE *fp, const CONF_SECTION *cs)
+{
+       const CONF_ITEM *ci, *next;
+
+       /*
+        *      Section header
+        */
+       if (!cs->name2) {
+               fprintf(fp, "%s {\n", cs->name1);
+       } else {
+               fprintf(fp, "%s %s {\n",
+                       cs->name1, cs->name2);
+       }
+
+       /*
+        *      Loop over contents.
+        */
+       for (ci = cs->children; ci; ci = next) {
+               next = ci->next;
+
+               switch (ci->type) {
+               case CONF_ITEM_PAIR:
+                       if (!cf_pair2file(fp, (CONF_PAIR *) ci)) return 0;
+                       break;
+
+               case CONF_ITEM_SECTION:
+                       if (!cf_section2file(fp, (CONF_SECTION *) ci)) return 0;
+                       break;
+
+               default:        /* should really be an error. */
+                       break;
+               
+               }
+       }
+
+       fprintf(fp, "}\n");
+
+       return 1;               /* success */
+}