Skip to content

Commit 723c46f

Browse files
committed
Query YAML files using yaml-path
At this moment the raw returned data is directly used to populate the 'value_of' element.
1 parent 4001616 commit 723c46f

File tree

1 file changed

+102
-4
lines changed

1 file changed

+102
-4
lines changed

src/OVAL/probes/independent/yamlfilecontent_probe.c

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,112 @@
2424
#include <config.h>
2525
#endif
2626

27+
#include <errno.h>
28+
#include <yaml.h>
29+
#include <yaml-path.h>
30+
2731
#include "yamlfilecontent_probe.h"
2832
#include "sexp-manip.h"
2933
#include "debug_priv.h"
3034
#include "oval_fts.h"
3135

36+
37+
static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, unsigned char *output_buffer, size_t output_buffer_size, probe_ctx *ctx)
38+
{
39+
int ret = 0;
40+
FILE *yaml_file = fopen(filepath, "r");
41+
if (yaml_file == NULL) {
42+
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
43+
"Unable to open file '%s': %s", filepath, strerror(errno));
44+
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
45+
SEXP_free(msg);
46+
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
47+
return -1;
48+
}
49+
50+
yaml_path_t *yaml_path = yaml_path_create();
51+
if (yaml_path_parse(yaml_path, (char *) yaml_path_cstr)) {
52+
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
53+
"Invalid YAML path '%s' (%s)\n", yaml_path_cstr,
54+
yaml_path_error_get(yaml_path)->message);
55+
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
56+
SEXP_free(msg);
57+
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
58+
fclose(yaml_file);
59+
return -1;
60+
};
61+
62+
yaml_parser_t parser;
63+
yaml_parser_initialize(&parser);
64+
yaml_parser_set_input_file(&parser, yaml_file);
65+
66+
yaml_emitter_t emitter;
67+
yaml_emitter_initialize(&emitter);
68+
size_t size_written;
69+
yaml_emitter_set_output_string(&emitter,
70+
output_buffer, output_buffer_size, &size_written);
71+
yaml_emitter_set_width(&emitter, -1);
72+
73+
yaml_event_t event;
74+
bool done = false;
75+
76+
do {
77+
if (yaml_parser_parse(&parser, &event)) {
78+
done = (event.type == YAML_STREAM_END_EVENT);
79+
if (yaml_path_filter_event(yaml_path, &parser, &event,
80+
YAML_PATH_FILTER_RETURN_ALL)) {
81+
if (!yaml_emitter_emit(&emitter, &event)) {
82+
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
83+
"YAML emitter error: yaml_emitter_emit returned 0: %s",
84+
emitter.problem);
85+
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
86+
SEXP_free(msg);
87+
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
88+
ret = -1;
89+
goto cleanup;
90+
}
91+
} else {
92+
yaml_event_delete(&event);
93+
}
94+
} else {
95+
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
96+
"YAML parser error: yaml_parse_parse returned 0: %s",
97+
parser.problem);
98+
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
99+
SEXP_free(msg);
100+
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
101+
ret = -1;
102+
goto cleanup;
103+
}
104+
} while (!done);
105+
106+
/* string output_buffer contains '\n' at the end */
107+
output_buffer[size_written - 1] = '\0';
108+
109+
cleanup:
110+
yaml_parser_delete(&parser);
111+
yaml_emitter_delete(&emitter);
112+
113+
yaml_path_destroy(yaml_path);
114+
fclose(yaml_file);
115+
116+
return ret;
117+
}
118+
32119
static int process_yaml_file(const char *path, const char *filename, const char *yamlpath, probe_ctx *ctx)
33120
{
121+
int ret = 0;
34122
char *filepath = oscap_path_join(path, filename);
35123

36-
/* TODO: insert code using libyaml-yamlpath-filter here */
124+
size_t output_buffer_size = 1024;
125+
unsigned char *output_buffer = calloc(output_buffer_size, sizeof(unsigned char));
126+
127+
if (yaml_path_query(filepath, yamlpath, output_buffer, output_buffer_size, ctx)) {
128+
ret = -1;
129+
goto cleanup;
130+
}
131+
132+
/* TODO: type conversion of output_buffer data */
37133

38134
SEXP_t *item = probe_item_create(
39135
OVAL_INDEPENDENT_YAML_FILE_CONTENT,
@@ -42,15 +138,18 @@ static int process_yaml_file(const char *path, const char *filename, const char
42138
"path", OVAL_DATATYPE_STRING, path,
43139
"filename", OVAL_DATATYPE_STRING, filename,
44140
"yamlpath", OVAL_DATATYPE_STRING, yamlpath,
141+
"value_of", OVAL_DATATYPE_STRING, output_buffer,
45142
/*
46-
"value_of",
47143
"windows_view",
48144
*/
49145
NULL
50146
);
51147
probe_item_collect(ctx, item);
148+
149+
cleanup:
150+
free(output_buffer);
52151
free(filepath);
53-
return 0;
152+
return ret;
54153
}
55154

56155
int yamlfilecontent_probe_main(probe_ctx *ctx, void *arg)
@@ -81,7 +180,6 @@ int yamlfilecontent_probe_main(probe_ctx *ctx, void *arg)
81180
oval_fts_close(ofts);
82181
}
83182

84-
cleanup:
85183
free(yamlpath_str);
86184
SEXP_free(yamlpath_val);
87185
SEXP_free(yamlpath_ent);

0 commit comments

Comments
 (0)