From: pcarana Date: Tue, 16 Apr 2019 22:38:23 +0000 (-0500) Subject: Rename 'is_new' funcs, use ENOENT err at SLURM loading, allow ASN 0 X-Git-Tag: v0.0.2~49^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c1a09c8deee3abb2de6e3cb6b52afa8afe86ed6e;p=thirdparty%2FFORT-validator.git Rename 'is_new' funcs, use ENOENT err at SLURM loading, allow ASN 0 --- diff --git a/src/json_parser.c b/src/json_parser.c index 40241c4b..60977401 100644 --- a/src/json_parser.c +++ b/src/json_parser.c @@ -3,6 +3,13 @@ #include #include +/* + * 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); diff --git a/src/slurm_db.c b/src/slurm_db.c index 274b2f91..1279732c 100644 --- a/src/slurm_db.c +++ b/src/slurm_db.c @@ -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 diff --git a/src/slurm_parser.c b/src/slurm_parser.c index a0cb451a..af285c37 100644 --- a/src/slurm_parser.c +++ b/src/slurm_parser.c @@ -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; }