24
24
#include <config.h>
25
25
#endif
26
26
27
+ #include <errno.h>
28
+ #include <yaml.h>
29
+ #include <yaml-path.h>
30
+
27
31
#include "yamlfilecontent_probe.h"
28
32
#include "sexp-manip.h"
29
33
#include "debug_priv.h"
30
34
#include "oval_fts.h"
31
35
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
+
32
119
static int process_yaml_file (const char * path , const char * filename , const char * yamlpath , probe_ctx * ctx )
33
120
{
121
+ int ret = 0 ;
34
122
char * filepath = oscap_path_join (path , filename );
35
123
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 */
37
133
38
134
SEXP_t * item = probe_item_create (
39
135
OVAL_INDEPENDENT_YAML_FILE_CONTENT ,
@@ -42,15 +138,18 @@ static int process_yaml_file(const char *path, const char *filename, const char
42
138
"path" , OVAL_DATATYPE_STRING , path ,
43
139
"filename" , OVAL_DATATYPE_STRING , filename ,
44
140
"yamlpath" , OVAL_DATATYPE_STRING , yamlpath ,
141
+ "value_of" , OVAL_DATATYPE_STRING , output_buffer ,
45
142
/*
46
- "value_of",
47
143
"windows_view",
48
144
*/
49
145
NULL
50
146
);
51
147
probe_item_collect (ctx , item );
148
+
149
+ cleanup :
150
+ free (output_buffer );
52
151
free (filepath );
53
- return 0 ;
152
+ return ret ;
54
153
}
55
154
56
155
int yamlfilecontent_probe_main (probe_ctx * ctx , void * arg )
@@ -81,7 +180,6 @@ int yamlfilecontent_probe_main(probe_ctx *ctx, void *arg)
81
180
oval_fts_close (ofts );
82
181
}
83
182
84
- cleanup :
85
183
free (yamlpath_str );
86
184
SEXP_free (yamlpath_val );
87
185
SEXP_free (yamlpath_ent );
0 commit comments