]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Rename 'is_new' funcs, use ENOENT err at SLURM loading, allow ASN 0
authorpcarana <pc.moreno2099@gmail.com>
Tue, 16 Apr 2019 22:38:23 +0000 (17:38 -0500)
committerpcarana <pc.moreno2099@gmail.com>
Tue, 16 Apr 2019 22:38:23 +0000 (17:38 -0500)
src/json_parser.c
src/slurm_db.c
src/slurm_parser.c

index 40241c4b1a15807fb1f784bffbed2ca3076c5116..60977401d202eaeb82d36734908aa41d34b1aaaf 100644 (file)
@@ -3,6 +3,13 @@
 #include <err.h>
 #include <errno.h>
 
+/*
+ * Try to get member @name from @parent as a char const *. On success, set
+ * @result with the members value.
+ *
+ * Returns 0 on success, -ENOENT if the @name doesn't exists, -EINVAL if the
+ * member isn't a JSON integer.
+ */
 int
 json_get_string(json_t *parent, char const *name, char const **result)
 {
@@ -11,11 +18,12 @@ json_get_string(json_t *parent, char const *name, char const **result)
        child = json_object_get(parent, name);
        if (child == NULL) {
                *result = NULL;
-               return 0;
+               return -ENOENT;
        }
 
        if (!json_is_string(child)) {
                warnx("The '%s' element is not a JSON string.", name);
+               *result = NULL;
                return -EINVAL;
        }
 
@@ -23,16 +31,21 @@ json_get_string(json_t *parent, char const *name, char const **result)
        return 0;
 }
 
+/*
+ * Try to get member @name from @parent as a json_int_t. On success, set
+ * @result with the members value.
+ *
+ * Returns 0 on success, -ENOENT if the @name doesn't exists, -EINVAL if the
+ * member isn't a JSON integer.
+ */
 int
 json_get_int(json_t *parent, char const *name, json_int_t *result)
 {
        json_t *child;
 
        child = json_object_get(parent, name);
-       if (child == NULL) {
-               *result = 0;
-               return 0;
-       }
+       if (child == NULL)
+               return -ENOENT;
 
        if (!json_is_integer(child)) {
                warnx("The '%s' element is not a JSON integer.", name);
index 274b2f91bce91766cc7759fc1384aeab61fb021b..1279732c9392404efc21bf52e6404810afbcbc85 100644 (file)
@@ -16,23 +16,23 @@ struct arraylist_db {
        struct al_assertion_bgpsec assertion_bgps_al;
 } array_lists_db;
 
-#define LOCATE_FUNCS(name, type, array_list, equal_cb, filter)                 \
+#define LOCATE_FUNCS(name, type, array_list, equal_cb, filter)         \
        static type *                                                   \
        name##_locate(array_list *base, type *obj)                      \
        {                                                               \
                type *cursor;                                           \
                                                                        \
                ARRAYLIST_FOREACH(base, cursor)                         \
-                       if (equal_cb(cursor, obj, filter))                      \
+                       if (equal_cb(cursor, obj, filter))              \
                                return cursor;                          \
                                                                        \
                return NULL;                                            \
        }                                                               \
                                                                        \
        static bool                                                     \
-       name##_is_new(array_list *base, type *obj)                      \
+       name##_exists(array_list *base, type *obj)                      \
        {                                                               \
-               return name##_locate(base, obj) == NULL;                \
+               return name##_locate(base, obj) != NULL;                \
        }
 
 int
@@ -165,11 +165,10 @@ LOCATE_FUNCS(bgpsec_assertion, struct slurm_bgpsec, struct al_assertion_bgpsec,
 int
 slurm_db_add_prefix_filter(struct slurm_prefix *prefix)
 {
-       if (prefix_filter_is_new(&array_lists_db.filter_pfx_al, prefix))
-               return al_filter_prefix_add(&array_lists_db.filter_pfx_al,
-                   prefix);
+       if (prefix_filter_exists(&array_lists_db.filter_pfx_al, prefix))
+               return -EEXIST;
 
-       return -EEXIST;
+       return al_filter_prefix_add(&array_lists_db.filter_pfx_al, prefix);
 }
 
 /*
@@ -180,11 +179,11 @@ slurm_db_add_prefix_filter(struct slurm_prefix *prefix)
 int
 slurm_db_add_prefix_assertion(struct slurm_prefix *prefix)
 {
-       if (prefix_assertion_is_new(&array_lists_db.assertion_pfx_al, prefix))
-               return al_assertion_prefix_add(
-                   &array_lists_db.assertion_pfx_al, prefix);
+       if (prefix_assertion_exists(&array_lists_db.assertion_pfx_al, prefix))
+               return -EEXIST;
 
-       return -EEXIST;
+       return al_assertion_prefix_add(&array_lists_db.assertion_pfx_al,
+           prefix);
 }
 
 /*
@@ -195,11 +194,10 @@ slurm_db_add_prefix_assertion(struct slurm_prefix *prefix)
 int
 slurm_db_add_bgpsec_filter(struct slurm_bgpsec *bgpsec)
 {
-       if (bgpsec_filter_is_new(&array_lists_db.filter_bgps_al, bgpsec))
-               return al_filter_bgpsec_add(&array_lists_db.filter_bgps_al,
-                   bgpsec);
+       if (bgpsec_filter_exists(&array_lists_db.filter_bgps_al, bgpsec))
+               return -EEXIST;
 
-       return -EEXIST;
+       return al_filter_bgpsec_add(&array_lists_db.filter_bgps_al, bgpsec);
 }
 
 /*
@@ -210,11 +208,11 @@ slurm_db_add_bgpsec_filter(struct slurm_bgpsec *bgpsec)
 int
 slurm_db_add_bgpsec_assertion(struct slurm_bgpsec *bgpsec)
 {
-       if (bgpsec_assertion_is_new(&array_lists_db.assertion_bgps_al, bgpsec))
-               return al_assertion_bgpsec_add(
-                   &array_lists_db.assertion_bgps_al, bgpsec);
+       if (bgpsec_assertion_exists(&array_lists_db.assertion_bgps_al, bgpsec))
+               return -EEXIST;
 
-       return -EEXIST;
+       return al_assertion_bgpsec_add(&array_lists_db.assertion_bgps_al,
+           bgpsec);
 }
 
 static void
index a0cb451aacf7e4ebddbfcd6d902faf0ce46d58f5..af285c378424e4c84175f33a745a03e49d784484 100644 (file)
@@ -106,21 +106,18 @@ set_asn(json_t *object, bool is_assertion, u_int32_t *result,
        int error;
 
        error = json_get_int(object, ASN, &int_tmp);
-       if (error)
-               return error;
-
-       if (int_tmp == 0) {
-               /* Optional for filters */
-               if(is_assertion) {
+       if (error == -ENOENT) {
+               if (is_assertion) {
                        warnx("ASN is required");
                        return -EINVAL;
                } else
-                       return 0;
-       }
+                       return 0; /* Optional for filters */
+       } else if (error)
+               return error;
 
        /* An underflow or overflow will be considered here */
-       if (int_tmp <= 0 || UINT32_MAX < int_tmp) {
-               warnx("ASN (%lld) is out of range [1 - %u].", int_tmp,
+       if (int_tmp < 0 || UINT32_MAX < int_tmp) {
+               warnx("ASN (%lld) is out of range [0 - %u].", int_tmp,
                    UINT32_MAX);
                return -EINVAL;
        }
@@ -138,12 +135,11 @@ set_comment(json_t *object, char const **comment, u_int8_t *flag,
        int error;
 
        error = json_get_string(object, COMMENT, &tmp);
-       if (error)
+       if (error && error == -ENOENT)
+               return 0; /* Optional member */
+       else if (error)
                return error;
 
-       if (tmp == NULL)
-               return 0;
-
        *comment = strdup(tmp);
        *flag = *flag | SLURM_COM_FLAG_COMMENT;
        (*members_loaded)++;
@@ -164,17 +160,14 @@ set_prefix(json_t *object, bool is_assertion, struct slurm_prefix *result,
 
        /* First part: Prefix in string format */
        error = json_get_string(object, PREFIX, &str_prefix);
-       if (error)
-               return error;
-
-       if (str_prefix == NULL) {
-               /* Optional for filters */
-               if(is_assertion) {
+       if (error && error == -ENOENT) {
+               if (is_assertion) {
                        warnx("SLURM assertion prefix is required");
                        return -EINVAL;
                } else
-                       return 0;
-       }
+                       return 0; /* Optional for filters */
+       } else if (error)
+               return error;
 
        clone = strdup(str_prefix);
        if (clone == NULL) {
@@ -230,17 +223,18 @@ set_max_prefix_length(json_t *object, bool is_assertion, u_int8_t addr_fam,
        json_int_t int_tmp;
        int error;
 
-       /* Handle error for filters */
-       if (!is_assertion)
-               return 0;
-
        error = json_get_int(object, MAX_PREFIX_LENGTH, &int_tmp);
-       if (error)
+       if (error == -ENOENT)
+               return 0; /* Optional for assertions, unsupported by filters */
+
+       if (error && is_assertion)
                return error;
 
-       /* Optional for assertions */
-       if (int_tmp == 0)
-               return 0;
+       /* Unsupported by filters */
+       if (!is_assertion) {
+               warnx("Prefix filter can't have a max prefix length");
+               return -EINVAL;
+       }
 
        /* An underflow or overflow will be considered here */
        if (int_tmp <= 0 || (addr_fam == AF_INET ? 32 : 128) < int_tmp) {
@@ -293,17 +287,14 @@ set_ski(json_t *object, bool is_assertion, struct slurm_bgpsec *result,
        int error;
 
        error = json_get_string(object, SKI, &str_encoded);
-       if (error)
-               return error;
-
-       if (str_encoded == NULL) {
-               /* Optional for filters */
-               if(is_assertion) {
+       if (error && error == -ENOENT) {
+               if (is_assertion) {
                        warnx("SLURM assertion %s is required", SKI);
                        return -EINVAL;
                } else
-                       return 0;
-       }
+                       return 0; /* Optional for filters */
+       } else if (error)
+               return error;
 
        error = validate_base64url_encoded(str_encoded);
        if (error)
@@ -332,17 +323,22 @@ set_router_pub_key(json_t *object, bool is_assertion,
        char const *str_encoded;
        int error;
 
-       /* Handle error for filters */
-       if (!is_assertion)
-               return 0;
-
        error = json_get_string(object, ROUTER_PUBLIC_KEY, &str_encoded);
-       if (error)
+       if (error == -ENOENT && !is_assertion)
+               return 0; /* OK for filters */
+
+       /* Required by assertions */
+       if (error && is_assertion) {
+               if (error == -ENOENT) {
+                       warnx("SLURM assertion %s is required", ROUTER_PUBLIC_KEY);
+                       return -EINVAL;
+               }
                return error;
+       }
 
-       /* Required for assertions */
-       if (str_encoded == NULL) {
-               warnx("SLURM assertion %s is required", ROUTER_PUBLIC_KEY);
+       /* Unsupported by filters */
+       if (!is_assertion) {
+               warnx("BGPsec filter can't have a router public key");
                return -EINVAL;
        }
 
@@ -439,16 +435,10 @@ load_single_prefix(json_t *object, bool is_assertion)
                        error = -EINVAL;
                        goto release_comment;
                }
-               /* and can't have the max prefix length */
-               if ((result.data_flag & SLURM_PFX_FLAG_MAX_LENGTH) > 0) {
-                       warnx("Prefix filter can't have a max prefix length");
-                       error = -EINVAL;
-                       goto release_comment;
-               }
 
                /* Validate expected members */
                if (!valid_members_count(object, member_count)) {
-                       warnx("Prefix filter has unknown members (see RFC 8416 section 3.3.1");
+                       warnx("Prefix filter has unknown members (see RFC 8416 section 3.3.1)");
                        error = -EINVAL;
                        goto release_comment;
                }
@@ -475,7 +465,7 @@ load_single_prefix(json_t *object, bool is_assertion)
 
        /* Validate expected members */
        if (!valid_members_count(object, member_count)) {
-               warnx("Prefix assertion has unknown members (see RFC 8416 section 3.4.1");
+               warnx("Prefix assertion has unknown members (see RFC 8416 section 3.4.1)");
                error = -EINVAL;
                goto release_comment;
        }
@@ -580,16 +570,10 @@ load_single_bgpsec(json_t *object, bool is_assertion)
                        error = -EINVAL;
                        goto release_comment;
                }
-               /* and can't have the router public key */
-               if ((result.data_flag & SLURM_BGPS_FLAG_ROUTER_KEY) > 0) {
-                       warnx("BGPsec filter can't have a router public key");
-                       error = -EINVAL;
-                       goto release_comment;
-               }
 
                /* Validate expected members */
                if (!valid_members_count(object, member_count)) {
-                       warnx("BGPsec filter has unknown members (see RFC 8416 section 3.3.2");
+                       warnx("BGPsec filter has unknown members (see RFC 8416 section 3.3.2)");
                        error = -EINVAL;
                        goto release_comment;
                }
@@ -603,7 +587,7 @@ load_single_bgpsec(json_t *object, bool is_assertion)
 
        /* Validate expected members */
        if (!valid_members_count(object, member_count)) {
-               warnx("BGPsec assertion has unknown members (see RFC 8416 section 3.4.2");
+               warnx("BGPsec assertion has unknown members (see RFC 8416 section 3.4.2)");
                error = -EINVAL;
                goto release_comment;
        }