Skip to content
Snippets Groups Projects
Commit 6f3536ef authored by Jakob Olsson's avatar Jakob Olsson
Browse files

fix compilation warnings

parent ac72687a
Branches
No related tags found
No related merge requests found
...@@ -65,7 +65,7 @@ INTERFACE ...@@ -65,7 +65,7 @@ INTERFACE
add_library(json-validator add_library(json-validator
SHARED SHARED
src/json-validator.cpp src/json-validator.cpp
src/schema.c src/schema.cpp
) )
set_target_properties(json-validator set_target_properties(json-validator
...@@ -104,6 +104,10 @@ target_link_libraries(json-validator ...@@ -104,6 +104,10 @@ target_link_libraries(json-validator
PUBLIC PUBLIC
json-hpp) json-hpp)
target_link_libraries(json-validator
PUBLIC
${LIBUBOX_LIBRARIES})
# regex with boost if gcc < 4.9 - default is std::regex # regex with boost if gcc < 4.9 - default is std::regex
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0")
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <json-c/json.h> #include <json-c/json.h>
#include <json-schema.hpp> #include <json-schema.hpp>
#include <regex.h>
#include "schema.h" #include "schema.h"
#include "json-validator.h" #include "json-validator.h"
...@@ -36,6 +37,46 @@ int schema_validator_init(void) ...@@ -36,6 +37,46 @@ int schema_validator_init(void)
return 0; return 0;
} }
bool schema_validator_validate(struct json_object *j_object, char *object, char *method, enum schema_call_t type)
{
struct json_object *definitions, *schema;
struct schema_object *s_object;
struct schema_method *s_method;
int rv = 1;
s_object = schema_get_object_schema(object);
if (!s_object)
return rv;
rv = 0;
s_method = schema_get_method_schema(object, method);
if (!s_method)
goto out;
if (type == SCHEMA_INPUT_CALL)
schema = json_tokener_parse(s_method->input);
else if (type == SCHEMA_OUTPUT_CALL)
schema = json_tokener_parse(s_method->output);
else
goto out;
if (!schema)
goto out;
if (s_object->definitions) {
definitions = json_tokener_parse(s_object->definitions);
if (definitions)
json_object_object_add(schema, "definitions", definitions);
}
rv = json_object_validate_schema(j_object, schema);
json_object_put(schema);
out:
return rv;
}
bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema) bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema)
{ {
json_object_object_add(j_object, "definitions", definitions); json_object_object_add(j_object, "definitions", definitions);
......
...@@ -4,11 +4,18 @@ ...@@ -4,11 +4,18 @@
extern "C" extern "C"
{ {
#endif #endif
enum schema_call_t {
SCHEMA_INPUT_CALL,
SCHEMA_OUTPUT_CALL
};
int schema_validator_destroy(void); int schema_validator_destroy(void);
int schema_validator_init(void); int schema_validator_init(void);
bool schema_validator_validate(struct json_object *j_object, char *object, char *method, enum schema_call_t type);
bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema); bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema);
bool json_object_validate_schema(struct json_object *j_obj, struct json_object *j_schema); bool json_object_validate_schema(struct json_object *j_obj, struct json_object *j_schema);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif
\ No newline at end of file
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <glob.h> #include <glob.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <regex.h>
#include "common.h"
#include "schema.h" #include "schema.h"
#define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas" #define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas"
...@@ -17,64 +17,64 @@ struct schema_context s_ctx; ...@@ -17,64 +17,64 @@ struct schema_context s_ctx;
static void schema_flush_method(struct schema_method *s_method) static void schema_flush_method(struct schema_method *s_method)
{ {
lwsl_notice("cleaning method %s\n", (char *)s_method->avl.key); fprintf(stderr, "cleaning method %s\n", (char *)s_method->avl.key);
if (s_method->input) if (s_method->input)
free(s_method->input); free(s_method->input);
if (s_method->output) if (s_method->output)
free(s_method->output); free(s_method->output);
if (s_method->avl.key) if (s_method->avl.key)
free((void *) s_method->avl.key); free((void *) s_method->avl.key);
free(s_method); free(s_method);
return; return;
} }
static void schema_flush_methods(struct schema_object *s_object) static void schema_flush_methods(struct schema_object *s_object)
{ {
struct schema_method *s_method, *tmp; struct schema_method *s_method, *tmp;
lwsl_notice("flushing all methods of object %s\n", (char *)s_object->avl.key); fprintf(stderr, "flushing all methods of object %s\n", (char *)s_object->avl.key);
avl_for_each_element_safe(&s_object->schema_methods, s_method, avl, tmp) { avl_for_each_element_safe(&s_object->schema_methods, s_method, avl, tmp) {
avl_delete(&s_object->schema_methods, &s_method->avl); avl_delete(&s_object->schema_methods, &s_method->avl);
schema_flush_method(s_method); schema_flush_method(s_method);
} }
return; return;
} }
static void schema_flush_object(struct schema_object *s_object) static void schema_flush_object(struct schema_object *s_object)
{ {
lwsl_notice("cleaning object %s\n", (char *)s_object->avl.key); fprintf(stderr, "cleaning object %s\n", (char *)s_object->avl.key);
schema_flush_methods(s_object); schema_flush_methods(s_object);
free((void *)s_object->avl.key); free((void *)s_object->avl.key);
if (s_object->definitions) if (s_object->definitions)
free(s_object->definitions); free(s_object->definitions);
if (s_object->regex) if (s_object->regex)
regfree(&s_object->regex_exp); regfree(&s_object->regex_exp);
free(s_object); free(s_object);
return; return;
} }
void schema_flush_objects(void) void schema_flush_objects(void)
{ {
struct schema_object *s_object, *tmp; struct schema_object *s_object, *tmp;
lwsl_notice("cleaning all schema objects\n"); fprintf(stderr, "cleaning all schema objects\n");
avl_for_each_element_safe(&s_ctx->schema_objects, s_object, avl, tmp) { avl_for_each_element_safe(&s_ctx.schema_objects, s_object, avl, tmp) {
avl_delete(&s_ctx->schema_objects, &s_object->avl); avl_delete(&s_ctx.schema_objects, &s_object->avl);
schema_flush_object(s_object); schema_flush_object(s_object);
} }
return; return;
} }
static void schema_parse_object(struct schema_object *schema_object, struct blob_buf *acl, struct blob_attr *object) static void schema_parse_object(struct schema_object *schema_object, struct blob_attr *object)
{ {
struct blob_attr *method, *properties; struct blob_attr *method, *properties;
int rem, rem2; int rem, rem2;
...@@ -84,8 +84,8 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -84,8 +84,8 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
if (strncmp(blobmsg_name(properties), "properties", 11)) if (strncmp(blobmsg_name(properties), "properties", 11))
continue; continue;
//lwsl_notice("%s %d method name = %s\n", __func__, __LINE__, blobmsg_name(object)); //fprintf(stderr, "%s %d method name = %s\n", __func__, __LINE__, blobmsg_name(object));
s_method = calloc(1, sizeof(*s_method)); s_method = (struct schema_method *)calloc(1, sizeof(*s_method));
if (!s_method) if (!s_method)
continue; continue;
...@@ -93,14 +93,14 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -93,14 +93,14 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
avl_insert(&schema_object->schema_methods, &s_method->avl); avl_insert(&schema_object->schema_methods, &s_method->avl);
blobmsg_for_each_attr(method, properties, rem2) { blobmsg_for_each_attr(method, properties, rem2) {
//lwsl_notice("%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method)); //fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method));
if (!strncmp(blobmsg_name(method), "input", 6)) { if (!strncmp(blobmsg_name(method), "input", 6)) {
s_method->input = blobmsg_format_json(method, true); s_method->input = blobmsg_format_json(method, true);
//lwsl_notice("input = %s\n", s_method->input); //fprintf(stderr, "input = %s\n", s_method->input);
} }
else if (!strncmp(blobmsg_name(method), "output", 7)) { else if (!strncmp(blobmsg_name(method), "output", 7)) {
s_method->output = blobmsg_format_json(method, true); s_method->output = blobmsg_format_json(method, true);
//lwsl_notice("output = %s\n", s_method->output); //fprintf(stderr, "output = %s\n", s_method->output);
} }
} }
} }
...@@ -109,12 +109,13 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -109,12 +109,13 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
static void static void
schema_setup_file(const char *path) schema_setup_file(const char *path)
{ {
struct blob_buf acl = { 0 }; struct blob_buf acl;
struct blob_attr *root, *object; struct blob_attr *root, *object;
int rem, rem2; int rem, rem2;
struct schema_object *schema_object; struct schema_object *schema_object;
char *definitions = NULL; char *definitions = NULL;
memset(&acl, 0, sizeof(acl));
blob_buf_init(&acl, 0); blob_buf_init(&acl, 0);
if (!blobmsg_add_json_from_file(&acl, path)) { if (!blobmsg_add_json_from_file(&acl, path)) {
...@@ -122,7 +123,7 @@ schema_setup_file(const char *path) ...@@ -122,7 +123,7 @@ schema_setup_file(const char *path)
goto out; goto out;
} }
schema_object = calloc(1, sizeof(*schema_object)); schema_object = (struct schema_object *) calloc(1, sizeof(*schema_object));
if (!schema_object) if (!schema_object)
goto out; goto out;
...@@ -132,15 +133,15 @@ schema_setup_file(const char *path) ...@@ -132,15 +133,15 @@ schema_setup_file(const char *path)
blob_for_each_attr(root, acl.head, rem) { blob_for_each_attr(root, acl.head, rem) {
if (!strncmp(blobmsg_name(root), "regex", 6)) { if (!strncmp(blobmsg_name(root), "regex", 6)) {
schema_object->regex = blobmsg_get_bool(root); schema_object->regex = blobmsg_get_bool(root);
lwsl_notice("%s: regex enabled %d\n", __func__, schema_object->regex); fprintf(stderr, "%s: regex enabled %d\n", __func__, schema_object->regex);
} }
if (!strncmp(blobmsg_name(root), "object", 7)) { if (!strncmp(blobmsg_name(root), "object", 7)) {
schema_object->avl.key = strdup(blobmsg_data(root)); schema_object->avl.key = strdup(blobmsg_get_string(root));
if (!schema_object->avl.key) if (!schema_object->avl.key)
goto out_flush; goto out_flush;
} }
if (!strncmp(blobmsg_name(root), "definitions", 12)) if (!strncmp(blobmsg_name(root), "definitions", 12))
definitions = blobmsg_format_json(root, true); definitions = blobmsg_format_json(root, true);
...@@ -153,16 +154,16 @@ schema_setup_file(const char *path) ...@@ -153,16 +154,16 @@ schema_setup_file(const char *path)
if (blobmsg_type(object) != BLOBMSG_TYPE_TABLE) if (blobmsg_type(object) != BLOBMSG_TYPE_TABLE)
continue; continue;
schema_parse_object(prog, schema_object, &acl, object); schema_parse_object(schema_object, object);
} }
} }
if (schema_object->regex) { if (schema_object->regex) {
int rv; int rv;
lwsl_notice("parsing regex for %s!\n", schema_object->avl.key ); fprintf(stderr, "parsing regex for %s!\n", (char *)schema_object->avl.key );
rv = regcomp(&schema_object->regex_exp, (char *) schema_object->avl.key, 0); rv = regcomp(&schema_object->regex_exp, (char *) schema_object->avl.key, 0);
if (rv) { if (rv) {
lwsl_notice("%s: invalid regex: %s, flushing validation schema!\n", __func__, (char *) schema_object->avl.key); fprintf(stderr, "%s: invalid regex: %s, flushing validation schema!\n", __func__, (char *) schema_object->avl.key);
goto out_flush; goto out_flush;
} }
} }
...@@ -171,10 +172,10 @@ schema_setup_file(const char *path) ...@@ -171,10 +172,10 @@ schema_setup_file(const char *path)
goto out_flush; goto out_flush;
schema_object->definitions = definitions; schema_object->definitions = definitions;
avl_insert(&s_ctx->schema_objects, &schema_object->avl); avl_insert(&s_ctx.schema_objects, &schema_object->avl);
/*avl_for_each_element(&s_ctx->schema_objects, schema_object, avl) { /*avl_for_each_element(&s_ctx.schema_objects, schema_object, avl) {
lwsl_notice("%s %d key = %s\n", __func__, __LINE__, (char *) schema_object->avl.key); fprintf(stderr, "%s %d key = %s\n", __func__, __LINE__, (char *) schema_object->avl.key);
}*/ }*/
out: out:
blob_buf_free(&acl); blob_buf_free(&acl);
return; return;
...@@ -184,42 +185,42 @@ out_flush: ...@@ -184,42 +185,42 @@ out_flush:
struct schema_method *schema_get_method_schema(const char *object, const char *method) struct schema_method *schema_get_method_schema(const char *object, const char *method)
{ {
struct schema_object *s_object; struct schema_object *s_object;
struct schema_method *s_method; struct schema_method *s_method;
lwsl_notice("%s: object = %s method = %s\n", __func__, object, method); fprintf(stderr, "%s: object = %s method = %s\n", __func__, object, method);
s_object = schema_get_object_schema(prog, object); s_object = schema_get_object_schema(object);
if (!s_object) if (!s_object)
return NULL; return NULL;
avl_for_each_element(&s_object->schema_methods, s_method, avl) { avl_for_each_element(&s_object->schema_methods, s_method, avl) {
lwsl_notice("%s: method = %s, avl.key = %s\n", __func__, method, (char *)s_method->avl.key); fprintf(stderr, "%s: method = %s, avl.key = %s\n", __func__, method, (char *)s_method->avl.key);
if (!strncmp(method, (char *) s_method->avl.key, METHOD_NAME_MAX_LEN)) if (!strncmp(method, (char *) s_method->avl.key, METHOD_NAME_MAX_LEN))
return s_method; return s_method;
} }
return NULL; return NULL;
} }
const char *schema_get_definitions_schema(const char *object) const char *schema_get_definitions_schema(const char *object)
{ {
struct schema_object *s_object; struct schema_object *s_object;
s_object = schema_get_object_schema(prog, object); s_object = schema_get_object_schema(object);
if (!s_object) if (!s_object)
return NULL; return NULL;
return s_object->definitions; return s_object->definitions;
} }
struct schema_object *schema_get_object_schema(const char *object) struct schema_object *schema_get_object_schema(const char *object)
{ {
struct schema_object *s_object; struct schema_object *s_object;
lwsl_notice("%s: object = %s\n", __func__, object); fprintf(stderr, "%s: object = %s\n", __func__, object);
avl_for_each_element(&s_ctx->schema_objects, s_object, avl) { avl_for_each_element(&s_ctx.schema_objects, s_object, avl) {
lwsl_notice("%s: object = %s, avl.key = %s, regex = %d\n", __func__, object, (char *)s_object->avl.key, s_object->regex); fprintf(stderr, "%s: object = %s, avl.key = %s, regex = %d\n", __func__, object, (char *)s_object->avl.key, s_object->regex);
if (s_object->regex) { if (s_object->regex) {
if (!regexec(&s_object->regex_exp, object, 0, NULL, 0)) if (!regexec(&s_object->regex_exp, object, 0, NULL, 0))
return s_object; return s_object;
...@@ -227,9 +228,9 @@ struct schema_object *schema_get_object_schema(const char *object) ...@@ -227,9 +228,9 @@ struct schema_object *schema_get_object_schema(const char *object)
if (!strncmp(object, (char *) s_object->avl.key, OBJECT_NAME_MAX_LEN)) if (!strncmp(object, (char *) s_object->avl.key, OBJECT_NAME_MAX_LEN))
return s_object; return s_object;
} }
} }
return NULL; return NULL;
} }
void void
...@@ -238,15 +239,15 @@ schema_setup_json(void) ...@@ -238,15 +239,15 @@ schema_setup_json(void)
int i; int i;
glob_t gl; glob_t gl;
avl_init(&s_ctx->schema_objects, avl_strcmp, false, NULL); avl_init(&s_ctx.schema_objects, avl_strcmp, false, NULL);
if (glob(JSON_SCHEMA_DIR "/*.json", 0, NULL, &gl)) if (glob(JSON_SCHEMA_DIR "/*.json", 0, NULL, &gl))
return; return;
for (i = 0; i < gl.gl_pathc; i++) { for (i = 0; i < gl.gl_pathc; i++) {
lwsl_notice("path = %s\n", gl.gl_pathv[i]); fprintf(stderr, "path = %s\n", gl.gl_pathv[i]);
schema_setup_file(prog, gl.gl_pathv[i]); schema_setup_file(gl.gl_pathv[i]);
} }
globfree(&gl); globfree(&gl);
} }
#ifndef SCHEMA_H #ifndef SCHEMA_H
#define SCHEMA_H #define SCHEMA_H
#ifdef __cplusplus
extern "C"
{
#endif
#define METHOD_NAME_MAX_LEN 64 #define METHOD_NAME_MAX_LEN 64
#define OBJECT_NAME_MAX_LEN 64 #define OBJECT_NAME_MAX_LEN 64
...@@ -25,14 +30,21 @@ struct schema_object { ...@@ -25,14 +30,21 @@ struct schema_object {
char *definitions; char *definitions;
bool regex;
regex_t regex_exp;
struct avl_tree schema_methods; struct avl_tree schema_methods;
struct avl_node avl; struct avl_node avl;
}; };
struct schema_method *schema_get_method_schema(const char *object, const char *method); extern struct schema_method *schema_get_method_schema(const char *object, const char *method);
const char *schema_get_definitions_schema(const char *object); extern const char *schema_get_definitions_schema(const char *object);
struct schema_object *schema_get_object_schema(const char *object); extern struct schema_object *schema_get_object_schema(const char *object);
void schema_setup_json(); extern void schema_setup_json();
void schema_flush_objects(); extern void schema_flush_objects();
#ifdef __cplusplus
}
#endif
#endif #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment