Skip to content

Commit 7ae58ba

Browse files
committed
Collect only scalars
Instead of emitted YAML document we will gather the scalars that match the YAML patch expression. We don't produce OVAL items directly but we use a temporary list to avoid incomplete results if a list contains non-scalar values. We don't need the yaml_emitter anymore.
1 parent 821e63a commit 7ae58ba

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

src/OVAL/probes/independent/yamlfilecontent_probe.c

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
#include "sexp-manip.h"
3333
#include "debug_priv.h"
3434
#include "oval_fts.h"
35+
#include "list.h"
3536

3637

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+
static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, struct oscap_list *values, probe_ctx *ctx)
3839
{
3940
int ret = 0;
4041
FILE *yaml_file = fopen(filepath, "r");
@@ -63,17 +64,9 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, uns
6364
yaml_parser_initialize(&parser);
6465
yaml_parser_set_input_file(&parser, yaml_file);
6566

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-
7367
yaml_event_t event;
74-
bool done = false;
75-
7668
bool sequence = false;
69+
7770
do {
7871
if (!yaml_parser_parse(&parser, &event)) {
7972
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
@@ -85,7 +78,6 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, uns
8578
ret = -1;
8679
goto cleanup;
8780
}
88-
done = (event.type == YAML_STREAM_END_EVENT);
8981
if (!yaml_path_filter_event(yaml_path, &parser, &event,
9082
YAML_PATH_FILTER_RETURN_ALL)) {
9183
yaml_event_delete(&event);
@@ -120,27 +112,13 @@ static int yaml_path_query(const char *filepath, const char *yaml_path_cstr, uns
120112
goto cleanup;
121113
}
122114
}
123-
124-
if (!yaml_emitter_emit(&emitter, &event)) {
125-
SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
126-
"YAML emitter error: yaml_emitter_emit returned 0: %s",
127-
emitter.problem);
128-
probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
129-
SEXP_free(msg);
130-
probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR);
131-
ret = -1;
132-
goto cleanup;
133-
}
134-
135-
} while (!done);
136-
137-
/* string output_buffer contains '\n' at the end */
138-
output_buffer[size_written - 1] = '\0';
115+
if (event.type == YAML_SCALAR_EVENT) {
116+
oscap_list_add(values, (void *) strdup((const char *) event.data.scalar.value));
117+
}
118+
} while (event.type != YAML_STREAM_END_EVENT);
139119

140120
cleanup:
141121
yaml_parser_delete(&parser);
142-
yaml_emitter_delete(&emitter);
143-
144122
yaml_path_destroy(yaml_path);
145123
fclose(yaml_file);
146124

@@ -151,34 +129,37 @@ static int process_yaml_file(const char *path, const char *filename, const char
151129
{
152130
int ret = 0;
153131
char *filepath = oscap_path_join(path, filename);
132+
struct oscap_list *values = oscap_list_new();
154133

155-
size_t output_buffer_size = 1024;
156-
unsigned char *output_buffer = calloc(output_buffer_size, sizeof(unsigned char));
157-
158-
if (yaml_path_query(filepath, yamlpath, output_buffer, output_buffer_size, ctx)) {
134+
if (yaml_path_query(filepath, yamlpath, values, ctx)) {
159135
ret = -1;
160136
goto cleanup;
161137
}
162138

163-
/* TODO: type conversion of output_buffer data */
164-
165-
SEXP_t *item = probe_item_create(
166-
OVAL_INDEPENDENT_YAML_FILE_CONTENT,
167-
NULL,
168-
"filepath", OVAL_DATATYPE_STRING, filepath,
169-
"path", OVAL_DATATYPE_STRING, path,
170-
"filename", OVAL_DATATYPE_STRING, filename,
171-
"yamlpath", OVAL_DATATYPE_STRING, yamlpath,
172-
"value_of", OVAL_DATATYPE_STRING, output_buffer,
173-
/*
174-
"windows_view",
175-
*/
176-
NULL
177-
);
178-
probe_item_collect(ctx, item);
139+
struct oscap_iterator *values_it = oscap_iterator_new(values);
140+
while (oscap_iterator_has_more(values_it)) {
141+
char *value = oscap_iterator_next(values_it);
142+
/* TODO: type conversion of 'value' data */
143+
144+
SEXP_t *item = probe_item_create(
145+
OVAL_INDEPENDENT_YAML_FILE_CONTENT,
146+
NULL,
147+
"filepath", OVAL_DATATYPE_STRING, filepath,
148+
"path", OVAL_DATATYPE_STRING, path,
149+
"filename", OVAL_DATATYPE_STRING, filename,
150+
"yamlpath", OVAL_DATATYPE_STRING, yamlpath,
151+
"value_of", OVAL_DATATYPE_STRING, value,
152+
/*
153+
"windows_view",
154+
*/
155+
NULL
156+
);
157+
probe_item_collect(ctx, item);
158+
}
159+
oscap_iterator_free(values_it);
179160

180161
cleanup:
181-
free(output_buffer);
162+
oscap_list_free(values, free);
182163
free(filepath);
183164
return ret;
184165
}

0 commit comments

Comments
 (0)