]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Purge early output files writability validations
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 6 May 2024 19:24:22 +0000 (13:24 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 6 May 2024 21:52:02 +0000 (15:52 -0600)
Fort used to clear the --output.roa and --output.bgpsec files to make
sure they were writable, during early validations.

So this is why the files spent so much time being empty! This was not
acceptable. It didn't even guarantee the files would still remain
writable by the time Fort needed to properly populate them.

Adjacent progress for #124.

src/config.c
src/file.c
src/file.h
src/output_printer.c

index 0843a679f48fd346335a2769b31eb2e85342146e..3bf9fcd9464a5be9668b731051bcac777dff4421 100644 (file)
@@ -989,12 +989,6 @@ set_default_values(void)
        rpki_config.thread_pool.validation.max = 5;
 }
 
-static bool
-valid_output_file(char const *path)
-{
-       return strcmp(path, "-") == 0 || file_valid(path);
-}
-
 static int
 validate_config(void)
 {
@@ -1024,14 +1018,6 @@ validate_config(void)
            rpki_config.server.interval.retry)
                return pr_op_err("Expire interval must be greater than refresh and retry intervals");
 
-       if (rpki_config.output.roa != NULL &&
-           !valid_output_file(rpki_config.output.roa))
-               return pr_op_err("Invalid output.roa file.");
-
-       if (rpki_config.output.bgpsec != NULL &&
-           !valid_output_file(rpki_config.output.bgpsec))
-               return pr_op_err("Invalid output.bgpsec file.");
-
        if (rpki_config.slurm != NULL && !valid_file_or_dir(rpki_config.slurm, true))
                return pr_op_err("Invalid slurm location.");
 
index d31c81349e209f6e3a08516721eb3e609da0f4c8..1de8fc95d13024231d74943e160deff3d5494063 100644 (file)
@@ -127,27 +127,6 @@ file_exists(char const *path)
        return (stat(path, &meta) == 0) ? 0 : errno;
 }
 
-/*
- * Validate @file_name, if it doesn't exist, this function will create it and
- * close it.
- */
-bool
-file_valid(char const *file_name)
-{
-       FILE *tmp;
-       int error;
-
-       if (file_name == NULL)
-               return false;
-
-       error = file_write(file_name, "w", &tmp);
-       if (error)
-               return false;
-
-       file_close(tmp);
-       return true;
-}
-
 static int
 rm(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
 {
index d38290e8c2e0bad6dd3f98fd37545648805037f4..a8218fc2c77eaa0dbd82776b8a662af0f8b142ca 100644 (file)
@@ -31,7 +31,6 @@ int file_load(char const *, struct file_contents *);
 void file_free(struct file_contents *);
 
 int file_exists(char const *);
-bool file_valid(char const *);
 
 int file_rm_rf(char const *);
 
index e321ebdc65f9b82634343f53e9a5fad4b4fa872e..946dbd146da0655d101960d5a5fd080dba136331 100644 (file)
@@ -7,50 +7,34 @@
 #include "crypto/base64.h"
 #include "types/vrp.h"
 
-static char addr_buf[INET6_ADDRSTRLEN];
-
 typedef struct json_out {
        FILE *file;
        bool first;
 } JSON_OUT;
 
-static int
-load_output_file(char const *output, FILE **result, bool *fopen)
+static FILE *
+load_output_file(char const *filename)
 {
-       FILE *tmp;
-       int error;
-
-       if (output == NULL) {
-               *result = NULL;
-               return 0;
-       }
+       FILE *out;
 
-       *fopen = false;
-       if (strcmp(output, "-") == 0) {
-               *result = stdout;
-               return 0;
-       }
+       if (filename == NULL)
+               return NULL;
 
-       error = file_write(output, "w", &tmp);
-       if (error) {
-               *result = NULL;
-               return error;
-       }
+       if (strcmp(filename, "-") == 0)
+               return stdout;
 
-       *fopen = true;
-       *result = tmp;
-       return 0;
+       return (file_write(filename, "w", &out) == 0) ? out : NULL;
 }
 
 static int
 print_roa_csv(struct vrp const *vrp, void *arg)
 {
-       FILE *out = arg;
+       char addr_buf[INET6_ADDRSTRLEN];
 
        if (vrp->addr_fam != AF_INET && vrp->addr_fam != AF_INET6)
                pr_crit("Unknown family type");
 
-       fprintf(out, "AS%u,%s/%u,%u\n", vrp->asn,
+       fprintf(arg, "AS%u,%s/%u,%u\n", vrp->asn,
            inet_ntop(vrp->addr_fam, &vrp->prefix, addr_buf, INET6_ADDRSTRLEN),
            vrp->prefix_length, vrp->max_prefix_length);
 
@@ -62,6 +46,7 @@ print_roa_json(struct vrp const *vrp, void *arg)
 {
        JSON_OUT *json_out = arg;
        FILE *out;
+       char addr_buf[INET6_ADDRSTRLEN];
 
        out = json_out->file;
        if (!json_out->first)
@@ -71,7 +56,7 @@ print_roa_json(struct vrp const *vrp, void *arg)
                pr_crit("Unknown family type");
 
        fprintf(out,
-           "\n  { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }",
+           "\n  { \"asn\": \"AS%u\", \"prefix\": \"%s/%u\", \"maxLength\": %u }",
            vrp->asn,
            inet_ntop(vrp->addr_fam, &vrp->prefix, addr_buf, INET6_ADDRSTRLEN),
            vrp->prefix_length,
@@ -85,7 +70,6 @@ print_roa_json(struct vrp const *vrp, void *arg)
 static int
 print_router_key_csv(struct router_key const *key, void *arg)
 {
-       FILE *out = arg;
        char *buf1, *buf2;
 
        if (!base64url_encode(key->ski, RK_SKI_LEN, &buf1)) {
@@ -99,7 +83,7 @@ print_router_key_csv(struct router_key const *key, void *arg)
                return 0; /* Skip it, I guess */
        }
 
-       fprintf(out, "AS%u,%s,%s\n", key->as, buf1, buf2);
+       fprintf(arg, "AS%u,%s,%s\n", key->as, buf1, buf2);
 
        free(buf2);
        free(buf1);
@@ -130,7 +114,7 @@ print_router_key_json(struct router_key const *key, void *arg)
                fprintf(out, ",");
 
        fprintf(out,
-           "\n  { \"asn\" : \"AS%u\", \"ski\" : \"%s\", \"spki\" : \"%s\" }",
+           "\n  { \"asn\": \"AS%u\", \"ski\": \"%s\", \"spki\": \"%s\" }",
            key->as,
            buf1,
            buf2);
@@ -141,38 +125,21 @@ print_router_key_json(struct router_key const *key, void *arg)
        return 0;
 }
 
-static int
-open_file(char const *loc, FILE **out, bool *fopen)
-{
-       int error;
-
-       error = load_output_file(loc, out, fopen);
-       if (error)
-               return pr_op_err("Error getting file '%s'", loc);
-
-       /* No output configured */
-       if (*out == NULL)
-               return -ENOENT;
-
-       return 0;
-}
-
 static void
 print_roas(struct db_table const *db)
 {
        FILE *out;
        JSON_OUT json_out;
-       bool fopen;
        int error;
 
-       out = NULL;
-       error = open_file(config_get_output_roa(), &out, &fopen);
-       if (error)
+       out = load_output_file(config_get_output_roa());
+       if (out == NULL)
                return;
 
        if (config_get_output_format() == OFM_CSV) {
                fprintf(out, "ASN,Prefix,Max prefix length\n");
                error = db_table_foreach_roa(db, print_roa_csv, out);
+
        } else {
                json_out.file = out;
                json_out.first = true;
@@ -182,10 +149,10 @@ print_roas(struct db_table const *db)
                fprintf(out, "\n]}\n");
        }
 
-       if (fopen)
-               file_close(out);
        if (error)
-               pr_op_err("Error printing ROAs");
+               pr_op_err("Error printing ROAs: %s", strerror(error));
+       if (out != stdout)
+               file_close(out);
 }
 
 static void
@@ -193,33 +160,29 @@ print_router_keys(struct db_table const *db)
 {
        FILE *out;
        JSON_OUT json_out;
-       bool fopen;
        int error;
 
-       out = NULL;
-       error = open_file(config_get_output_bgpsec(), &out, &fopen);
-       if (error)
+       out = load_output_file(config_get_output_bgpsec());
+       if (out == NULL)
                return;
 
        if (config_get_output_format() == OFM_CSV) {
-               fprintf(out,
-                   "ASN,Subject Key Identifier,Subject Public Key Info\n");
-               error = db_table_foreach_router_key(db, print_router_key_csv,
-                   out);
+               fprintf(out, "ASN,Subject Key Identifier,Subject Public Key Info\n");
+               error = db_table_foreach_router_key(db, print_router_key_csv, out);
+
        } else {
                json_out.file = out;
                json_out.first = true;
 
                fprintf(out, "{ \"router-keys\" : [");
-               error = db_table_foreach_router_key(db, print_router_key_json,
-                   &json_out);
+               error = db_table_foreach_router_key(db, print_router_key_json, &json_out);
                fprintf(out, "\n]}\n");
        }
 
-       if (fopen)
-               file_close(out);
        if (error)
-               pr_op_err("Error printing Router Keys");
+               pr_op_err("Error printing Router Keys: %s", strerror(error));
+       if (out != stdout)
+               file_close(out);
 }
 
 void