Debian Codegraph @

src/OVAL/probes/independent/textfilecontent54_probe.c

raw symbols SCIP
1 /**
2 * @file textfilecontent54_probe.c
3 * @brief textfilecontent54 probe
4 * @author "Tomas Heinrich" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a textfilecontent54_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * "Tomas Heinrich" <[email protected]>
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <limits.h>
46
47 #include "_seap.h"
48 #include <probe-api.h>
49 #include <probe/entcmp.h>
50 #include <probe/probe.h>
51 #include <probe/option.h>
52 #include <oval_fts.h>
53 #include "common/debug_priv.h"
54 #include "common/util.h"
55 #include "common/oscap_pcre.h"
56 #include "common/list.h"
57
58 #include "textfilecontent54_probe.h"
59
60 #define FILE_SEPARATOR '/'
61
62 static SEXP_t *create_item(const char *path, const char *filename, char *pattern,
63 int instance, char **substrs, int substr_cnt, oval_schema_version_t over)
64 {
65 int i;
66 SEXP_t *item;
67 SEXP_t *r0;
68 SEXP_t *se_instance, *se_filepath;
69 char *text;
70
71 if (strlen(path) + strlen(filename) + 1 > PATH_MAX) {
72 dE("path+filename too long");
73 return (NULL);
74 }
75
76 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.4)) < 0) {
77 pattern = text = NULL;
78 se_instance = NULL;
79 } else {
80 text = substrs[0];
81 se_instance = SEXP_number_newu_64((int64_t) instance);
82 }
83 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.6)) < 0) {
84 se_filepath = NULL;
85 } else {
86 const size_t path_len = strlen(path);
87 /* Avoid 2 slashes */
88 if (path_len >= 1 && path[path_len - 1] == FILE_SEPARATOR) {
89 se_filepath = SEXP_string_newf("%s%s", path, filename);
90 } else {
91 se_filepath = SEXP_string_newf("%s%c%s", path, FILE_SEPARATOR, filename);
92 }
93 }
94
95 item = probe_item_create(OVAL_INDEPENDENT_TEXT_FILE_CONTENT, NULL,
96 "filepath", OVAL_DATATYPE_SEXP, se_filepath,
97 "path", OVAL_DATATYPE_STRING, path,
98 "filename", OVAL_DATATYPE_STRING, filename,
99 "pattern", OVAL_DATATYPE_STRING, pattern,
100 "instance", OVAL_DATATYPE_SEXP, se_instance,
101 "line", OVAL_DATATYPE_STRING, pattern,
102 "text", OVAL_DATATYPE_STRING, substrs[0],
103 NULL);
104
105 for (i = 1; i < substr_cnt; ++i) {
106 probe_item_ent_add (item, "subexpression", NULL, r0 = SEXP_string_new (substrs[i], strlen (substrs[i])));
107 SEXP_free (r0);
108 }
109
110 SEXP_free(se_filepath);
111 SEXP_free(se_instance);
112 return item;
113 }
114
115 struct pfdata {
116 char *pattern;
117 oscap_pcre_options_t re_opts;
118 SEXP_t *instance_ent;
119 probe_ctx *ctx;
120 oscap_pcre_t *compiled_regex;
121 };
122
123 static int process_file(const char *prefix, const char *path, const char *file, struct pfdata *pfd, oval_schema_version_t over, struct oscap_list *blocked_paths)
124 {
125 int ret = 0, path_len, file_len, cur_inst = 0, fd = -1, substr_cnt,
126 buf_size = 0, buf_used = 0, ofs = 0, buf_inc = 4096, instance_count = 0,
127 want_instance = 1, negative_instance_value = 0;
128 char **substrs = NULL;
129 char *whole_path = NULL, *whole_path_with_prefix = NULL, *buf = NULL;
130 SEXP_t *next_inst = NULL, *items = SEXP_list_new(NULL), *instance_value_list = NULL,
131 *instance_value = NULL;
132 struct stat st;
133
134
135 if (file == NULL)
136 goto cleanup;
137
138 path_len = strlen(path);
139 file_len = strlen(file);
140 whole_path = malloc(path_len + file_len + 2);
141
142 memcpy(whole_path, path, path_len);
143
144 if (whole_path[path_len - 1] != FILE_SEPARATOR) {
145 whole_path[path_len] = FILE_SEPARATOR;
146 ++path_len;
147 }
148
149 memcpy(whole_path + path_len, file, file_len + 1);
150
151 if (probe_path_is_blocked(whole_path, blocked_paths)) {
152 goto cleanup;
153 }
154 /*
155 * If stat() fails, don't report an error and just skip the file.
156 * This is an expected situation, because the fts_*() functions
157 * are called with the 'FTS_PHYSICAL' option. Normally, stumbling
158 * upon a symlink without a target would cause fts_read() to return
159 * the 'FTS_SLNONE' flag, but the 'FTS_PHYSICAL' option causes it
160 * to return 'FTS_SL' and the presence of a valid target has to
161 * be determined with stat().
162 */
163 whole_path_with_prefix = oscap_path_join(prefix, whole_path);
164 if (stat(whole_path_with_prefix, &st) == -1)
165 goto cleanup;
166 if (!S_ISREG(st.st_mode))
167 goto cleanup;
168
169 fd = open(whole_path_with_prefix, O_RDONLY);
170 if (fd == -1) {
171 SEXP_t *msg;
172
173 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "open(): '%s' %s.", whole_path, strerror(errno));
174 probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg);
175 SEXP_free(msg);
176 probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR);
177 ret = -1;
178 goto cleanup;
179 }
180
181 do {
182 buf_size += buf_inc;
183 void *new_buf = realloc(buf, buf_size);
184 if (new_buf == NULL) {
185 dE("Can't re-allocate memory for file-processing buffer");
186 ret = PROBE_ENOMEM;
187 goto cleanup;
188 }
189 buf = new_buf;
190 ret = read(fd, buf + buf_used, buf_inc);
191 if (ret == -1) {
192 SEXP_t *msg;
193
194 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "read(): '%s' %s.", whole_path, strerror(errno));
195 probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg);
196 SEXP_free(msg);
197 probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR);
198 ret = -2;
199 goto cleanup;
200 }
201 buf_used += ret;
202 } while (ret == buf_inc);
203
204 if (buf_used == buf_size) {
205 void *new_buf = realloc(buf, ++buf_size);
206 if (new_buf == NULL) {
207 dE("Can't re-allocate memory");
208 ret = PROBE_ENOMEM;
209 goto cleanup;
210 }
211 buf = new_buf;
212 }
213 buf[buf_used++] = '\0';
214
215 do {
216 substr_cnt = oscap_pcre_get_substrings(buf, &ofs, pfd->compiled_regex, want_instance, &substrs);
217
218 if (substr_cnt < 0) {
219 SEXP_t *msg;
220 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
221 "Regular expression pattern match failed in file %s with error %d.",
222 whole_path, substr_cnt);
223 probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg);
224 SEXP_free(msg);
225 probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR);
226 ret = -3;
227 goto cleanup;
228 }
229
230 if (substr_cnt > 0) {
231 int k;
232 instance_count++;
233
234 SEXP_list_add(items, create_item(path, file, pfd->pattern,
235 instance_count, substrs, substr_cnt, over));
236
237 for (k = 0; k < substr_cnt; ++k)
238 free(substrs[k]);
239 free(substrs);
240 }
241 } while (substr_cnt > 0 && ofs < buf_used);
242
243 probe_ent_getvals(pfd->instance_ent, &instance_value_list);
244 instance_value = SEXP_list_first(instance_value_list);
245 negative_instance_value = SEXP_number_geti_64(instance_value) < 0;
246 SEXP_free(instance_value_list);
247 SEXP_free(instance_value);
248
249 for(cur_inst = 0; cur_inst < instance_count; cur_inst++){
250 if (negative_instance_value)
251 next_inst = SEXP_number_newi_32(cur_inst - instance_count);
252
253 else
254 next_inst = SEXP_number_newi_32(cur_inst + 1);
255
256 if (probe_entobj_cmp(pfd->instance_ent, next_inst) == OVAL_RESULT_TRUE)
257 want_instance = 1;
258 else
259 want_instance = 0;
260
261 SEXP_free(next_inst);
262
263 if (want_instance) {
264 int pic_ret = probe_item_collect(pfd->ctx, SEXP_list_nth(items, cur_inst + 1));
265 if (pic_ret == 2 || pic_ret == -1) {
266 ret = -4;
267 break;
268 }
269 }
270 else
271 SEXP_free(SEXP_list_nth(items, cur_inst + 1));
272 }
273
274 cleanup:
275 if (fd != -1)
276 close(fd);
277 free(buf);
278 if (whole_path != NULL)
279 free(whole_path);
280 free(whole_path_with_prefix);
281
282 /* coverity[leaked_storage] - substrs is not leaked */
283 return ret;
284 }
285
286 int textfilecontent54_probe_offline_mode_supported()
287 {
288 return PROBE_OFFLINE_OWN;
289 }
290
291 int textfilecontent54_probe_main(probe_ctx *ctx, void *arg)
292 {
293 SEXP_t *path_ent, *file_ent, *inst_ent, *bh_ent, *patt_ent, *filepath_ent, *probe_in;
294 SEXP_t *r0;
295 bool val;
296 struct pfdata pfd;
297 int ret = 0;
298 int errorffset = -1;
299 char *error;
300 OVAL_FTS *ofts;
301 OVAL_FTSENT *ofts_ent;
302
303 (void)arg;
304
305 memset(&pfd, 0, sizeof(pfd));
306
307 probe_in = probe_ctx_getobject(ctx);
308
309 oval_schema_version_t over = probe_obj_get_platform_schema_version(probe_in);
310 path_ent = probe_obj_getent(probe_in, "path", 1);
311 file_ent = probe_obj_getent(probe_in, "filename", 1);
312 inst_ent = probe_obj_getent(probe_in, "instance", 1);
313 patt_ent = probe_obj_getent(probe_in, "pattern", 1);
314 filepath_ent = probe_obj_getent(probe_in, "filepath", 1);
315 bh_ent = probe_obj_getent(probe_in, "behaviors", 1);
316
317 /* we want (path+filename or filepath) + instance + pattern*/
318 if ( ((path_ent == NULL || file_ent == NULL) && filepath_ent==NULL) ||
319 inst_ent==NULL ||
320 patt_ent==NULL) {
321 SEXP_free (patt_ent);
322 ret = PROBE_ENOELM;
323 goto cleanup;
324 }
325
326 /* get pattern from SEXP */
327 SEXP_t *ent_val;
328 ent_val = probe_ent_getval(patt_ent);
329 pfd.pattern = SEXP_string_cstr(ent_val);
330 SEXP_free(patt_ent);
331 SEXP_free(ent_val);
332 if (pfd.pattern == NULL) {
333 ret = -1;
334 goto cleanup;
335 }
336
337 probe_tfc54behaviors_canonicalize(&bh_ent);
338
339 pfd.instance_ent = inst_ent;
340 pfd.ctx = ctx;
341 pfd.re_opts = OSCAP_PCRE_OPTS_UTF8;
342 r0 = probe_ent_getattrval(bh_ent, "ignore_case");
343 if (r0) {
344 val = SEXP_string_getb(r0);
345 SEXP_free(r0);
346 if (val)
347 pfd.re_opts |= OSCAP_PCRE_OPTS_CASELESS;
348 }
349 r0 = probe_ent_getattrval(bh_ent, "multiline");
350 if (r0) {
351 val = SEXP_string_getb(r0);
352 SEXP_free(r0);
353 if (val)
354 pfd.re_opts |= OSCAP_PCRE_OPTS_MULTILINE;
355 }
356 r0 = probe_ent_getattrval(bh_ent, "singleline");
357 if (r0) {
358 val = SEXP_string_getb(r0);
359 SEXP_free(r0);
360 if (val)
361 pfd.re_opts |= OSCAP_PCRE_OPTS_DOTALL;
362 }
363
364 pfd.compiled_regex = oscap_pcre_compile(pfd.pattern, pfd.re_opts, &error, &errorffset);
365 if (pfd.compiled_regex == NULL) {
366 SEXP_t *msg;
367
368 msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "oscap_pcre_compile() '%s' %s.", pfd.pattern, error);
369 probe_cobj_add_msg(probe_ctx_getresult(pfd.ctx), msg);
370 SEXP_free(msg);
371 probe_cobj_set_flag(probe_ctx_getresult(pfd.ctx), SYSCHAR_FLAG_ERROR);
372 oscap_pcre_err_free(error);
373 goto cleanup;
374 }
375
376 const char *prefix = getenv("OSCAP_PROBE_ROOT");
377
378 if ((ofts = oval_fts_open_prefixed(prefix, path_ent, file_ent, filepath_ent, bh_ent, probe_ctx_getresult(ctx))) != NULL) {
379 while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
380 if (ofts_ent->fts_info == FTS_F
381 || ofts_ent->fts_info == FTS_SL) {
382 // todo: handle return code
383 process_file(prefix, ofts_ent->path, ofts_ent->file, &pfd, over, ctx->blocked_paths);
384 }
385 oval_ftsent_free(ofts_ent);
386 }
387
388 oval_fts_close(ofts);
389 }
390
391 cleanup:
392 SEXP_free(file_ent);
393 SEXP_free(path_ent);
394 SEXP_free(inst_ent);
395 SEXP_free(bh_ent);
396 SEXP_free(filepath_ent);
397 if (pfd.pattern != NULL)
398 free(pfd.pattern);
399 if (pfd.compiled_regex != NULL)
400 oscap_pcre_free(pfd.compiled_regex);
401 return ret;
402 }