diff options
author | Phil Sutter <phil@nwl.cc> | 2018-08-29 16:23:28 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-08-30 12:19:36 +0200 |
commit | 49e0f1dc6e52e791f5e0ba21097aea17d5950d38 (patch) | |
tree | 00215046781f409c62974cd57bf75f96ad265a51 /src/parser_json.c | |
parent | 90d4ee087171e75d5313359ad6b6f1341e51ddc5 (diff) |
JSON: Add metainfo object to all output
Right now this object merely contains the nftables version and release
name as well as a JSON schema version, but it could be extended
arbitrarily. In the future, this will also allow for non-compatible
schema changes should the need for this arise.
Adjust the parser to accept metainfo objects and make it verify
json_schema_version to be less than or equal to the one hard-coded in
the library.
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/parser_json.c')
-rw-r--r-- | src/parser_json.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/parser_json.c b/src/parser_json.c index 6870434e..6af9d075 100644 --- a/src/parser_json.c +++ b/src/parser_json.c @@ -3118,6 +3118,22 @@ static struct cmd *json_parse_cmd(struct json_ctx *ctx, json_t *root) return NULL; } +static int json_verify_metainfo(struct json_ctx *ctx, json_t *root) +{ + int schema_version; + + if (!json_unpack(root, "{s:i}", "json_schema_version", &schema_version)) + return 0; + + if (schema_version > JSON_SCHEMA_VERSION) { + json_error(ctx, "Schema version %d not supported, maximum supported version is %d\n", + schema_version, JSON_SCHEMA_VERSION); + return 1; + } + + return 0; +} + static int __json_parse(struct json_ctx *ctx, json_t *root) { struct eval_ctx ectx = { @@ -3142,11 +3158,22 @@ static int __json_parse(struct json_ctx *ctx, json_t *root) /* this is more or less from parser_bison.y:716 */ LIST_HEAD(list); struct cmd *cmd; + json_t *tmp2; if (!json_is_object(value)) { json_error(ctx, "Unexpected command array element of type %s, expected object.", json_typename(value)); return -1; } + + tmp2 = json_object_get(value, "metainfo"); + if (tmp2) { + if (json_verify_metainfo(ctx, tmp2)) { + json_error(ctx, "Metainfo verification failed."); + return -1; + } + continue; + } + cmd = json_parse_cmd(ctx, value); if (!cmd) { |