]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
support ROA export in JSON formatwq
authorreschke <julian.reschke@gmx.de>
Wed, 10 Feb 2021 18:01:44 +0000 (19:01 +0100)
committerreschke <julian.reschke@gmx.de>
Wed, 10 Feb 2021 18:01:44 +0000 (19:01 +0100)
src/config.c
src/config.h
src/output_printer.c

index 2d03935ee3a0393a1f4a863b463fbdedfd698ad3..43cc8ca35d5c8d71deddd22c780a61f6239744ba 100644 (file)
@@ -186,6 +186,8 @@ struct rpki_config {
                char *roa;
                /** File where the validated BGPsec certs will be stored */
                char *bgpsec;
+                /** File where the validated ROAs will be stored in JSON format */
+               char *roa_json;
        } output;
 
        /* ASN1 decoder max stack size allowed */
@@ -697,6 +699,13 @@ static const struct option_field options[] = {
                .offset = offsetof(struct rpki_config, output.bgpsec),
                .doc = "File where BGPsec Router Keys will be stored in CSV format, use '-' to print at console",
                .arg_doc = "<file>",
+       }, {
+               .id = 6002,
+               .name = "output.roa.json",
+               .type = &gt_string,
+                .offset = offsetof(struct rpki_config, output.roa_json),
+               .doc = "File where ROAs will be stored in JSON format, use '-' to print at console",
+               .arg_doc = "<file>",
        },
 
        {
@@ -976,6 +985,7 @@ set_default_values(void)
        }
 
        rpki_config.output.roa = NULL;
+       rpki_config.output.roa_json = NULL;
        rpki_config.output.bgpsec = NULL;
 
        rpki_config.asn1_decode_max_stack = 4096; /* 4kB */
@@ -1437,6 +1447,12 @@ config_get_output_roa(void)
        return rpki_config.output.roa;
 }
 
+char const *
+config_get_output_roa_json(void)
+{
+       return rpki_config.output.roa_json;
+}
+
 char const *
 config_get_output_bgpsec(void)
 {
index 0a8c8d642360ac6d0a49da662b80abc1b31d1296..e77721679ba8d764dc63f84d6ed1afa7476a2bb6 100644 (file)
@@ -48,6 +48,7 @@ unsigned int config_get_http_priority(void);
 unsigned int config_get_http_retry_count(void);
 unsigned int config_get_http_retry_interval(void);
 char const *config_get_output_roa(void);
+char const *config_get_output_roa_json(void);
 char const *config_get_output_bgpsec(void);
 unsigned int config_get_asn1_decode_max_stack(void);
 unsigned int config_get_stale_repository_period(void);
index 8dfd41fa0323099c40724c6ea30fdba9434e61a7..18c98b41d59d7579ba20f2efef7842c6a953e7db 100644 (file)
@@ -62,6 +62,37 @@ print_roa(struct vrp const *vrp, void *arg)
        return 0;
 }
 
+typedef struct json_out { FILE *file; int first; } JSON_OUT;
+
+static int
+print_roa_json(struct vrp const *vrp, void *arg)
+{
+       JSON_OUT *json_out = arg;
+       FILE *out = json_out->file;
+
+       if (!json_out->first)
+               fprintf(out, ",");
+
+
+       switch (vrp->addr_fam) {
+       case AF_INET:
+               fprintf(out, "\n  { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }", vrp->asn,
+                       addr2str4(&vrp->prefix.v4, addr_buf), vrp->prefix_length,
+                       vrp->max_prefix_length);
+               break;
+       case AF_INET6:
+                fprintf(out, "\n  { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }", vrp->asn,
+                       addr2str6(&vrp->prefix.v6, addr_buf), vrp->prefix_length,
+                       vrp->max_prefix_length);
+               break;
+       default:
+               pr_crit("Unknown family type");
+       }
+
+       json_out->first = 0;
+       return 0;
+}
+
 /* Print as base64url strings without trailing pad */
 static int
 print_router_key(struct router_key const *key, void *arg)
@@ -122,6 +153,31 @@ print_roas(struct db_table *db)
                pr_op_err("Error printing ROAs");
 }
 
+static void
+print_roas_json(struct db_table *db)
+{
+       FILE *out;
+       bool fopen;
+       int error;
+
+       out = NULL;
+       error = open_file(config_get_output_roa_json(), &out, &fopen);
+       if (error)
+               return;
+
+       JSON_OUT json_out;
+       json_out.file = out;
+       json_out.first = 1;
+
+       fprintf(out, "{ \"roas\" : [");
+       error = db_table_foreach_roa(db, print_roa_json, &json_out);
+       fprintf(out, "\n]}\n");
+       if (fopen)
+               file_close(out);
+       if (error)
+               pr_op_err("Error printing ROAs");
+}
+
 static void
 print_router_keys(struct db_table *db)
 {
@@ -146,5 +202,6 @@ void
 output_print_data(struct db_table *db)
 {
        print_roas(db);
+       print_roas_json(db);
        print_router_keys(db);
 }