-/*
- Parse a configuration file and put the results in the configuration tree
- starting at *base.
-*/
-int read_config_file(avl_tree_t *config_tree, const char *fname)
-{
- int err = -2; /* Parse error */
- FILE *fp;
- char *buffer, *line;
- char *variable, *value;
- int lineno = 0, ignore = 0;
- config_t *cfg;
- size_t bufsize;
-
- cp();
-
- fp = fopen(fname, "r");
-
- if(!fp) {
- logger(LOG_ERR, _("Cannot open config file %s: %s"), fname,
- strerror(errno));
- return -3;
- }
-
- bufsize = 100;
- buffer = xmalloc(bufsize);
-
- for(;;) {
- line = readline(fp, &buffer, &bufsize);
-
- if(!line) {
- err = -1;
- break;
- }
-
- if(feof(fp)) {
- err = 0;
- break;
+/// Write a configuration file to a FILE handle.
+bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config) {
+ if(mesh->config_key) {
+ uint8_t buf[config->len + 16];
+ size_t len = sizeof(buf);
+ uint8_t seqbuf[12];
+ randomize(&seqbuf, sizeof(seqbuf));
+ chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
+ chacha_poly1305_set_key(ctx, mesh->config_key);
+ bool success = false;
+
+ if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
+ success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
+ } else {
+ logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
+ meshlink_errno = MESHLINK_ESTORAGE;