Debian Codegraph @

src/OVAL/probes/independent/textfilecontent_probe.c

raw symbols SCIP
1 /**
2 * @file textfilecontent_probe.c
3 * @brief textfilecontent probe
4 * @author "Tomas Heinrich" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a textfilecontent_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2010 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 /*
34 * textfilecontent probe:
35 *
36 * textfilecontent_object
37 * textfilecontentbehaviors behaviors
38 * string path
39 * string filename
40 * string line
41 *
42 * textfilecontent_item
43 * attrs
44 * id
45 * status_enum status
46 * string path
47 * string filename
48 * string pattern
49 * int instance
50 * string line
51 * string text
52 * [0..*] anytype subexpression
53 */
54
55 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
56 #include <config.h>cxx . . $ `<file>/build/config.h`/
57 #endif
58
59 #include <stdio.h>
60 #include <string.h>
61 #include <sys/stat.h>
62 #include <sys/types.h>
63 #include <fcntl.h>
64 #include <limits.h>
65
66 #include "_seap.h"cxx . . $ `<file>/src/OVAL/probes/SEAP/_seap.h`/
67 #include <probe-api.h>cxx . . $ `<file>/src/OVAL/probes/public/probe-api.h`/
68 #include <probe/entcmp.h>cxx . . $ `<file>/src/OVAL/probes/probe/entcmp.h`/
69 #include <probe/probe.h>cxx . . $ `<file>/src/OVAL/probes/probe/probe.h`/
70 #include <probe/option.h>cxx . . $ `<file>/src/OVAL/probes/probe/option.h`/
71 #include <oval_fts.h>cxx . . $ `<file>/src/OVAL/probes/oval_fts.h`/
72 #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
73 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
74 #include "common/oscap_pcre.h"cxx . . $ `<file>/src/common/oscap_pcre.h`/
75 #include "textfilecontent_probe.h"cxx . . $ `<file>/src/OVAL/probes/independent/textfilecontent_probe.h`/
76
77 #define FILE_SEPARATORcxx . . $ `src/OVAL/probes/independent/textfilecontent_probe.c:77:9`! '/'
78
79 static SEXP_tcxx . . $ SEXP_t# *create_itemcxx . . $ create_item(91a654989eed1720).(const char *path, const char *filename, char *pattern,
80 int instance, char **substrs, int substr_cnt, oval_schema_version_tcxx . . $ oval_schema_version_t# over)
81 {
82 int i;
83 SEXP_tcxx . . $ SEXP_t# *item;
84 SEXP_tcxx . . $ SEXP_t# *r0;
85 SEXP_tcxx . . $ SEXP_t# *se_instance, *se_filepath;
86 char *text;
87
88 if (strlencxx . . $ strlen(751346f8406fd082).(path) + strlencxx . . $ strlen(751346f8406fd082).(filename) + 1 > PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!) {
89 dEcxx . . $ `src/common/debug_priv.h:63:9`!("path+filename too long");
90 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
91 }
92
93 if (oval_schema_version_cmpcxx . . $ oval_schema_version_cmp(b4d0c2bfb97f274f).(over, OVAL_SCHEMA_VERSIONcxx . . $ `src/OVAL/public/oval_schema_version.h:39:9`!(5.4)) < 0) {
94 pattern = text = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
95 se_instance = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
96 } else {
97 text = substrs[0];
98 se_instance = SEXP_number_newu_64cxx . . $ SEXP_number_newu_64(6d23cce3a40f89fa).((int64_tcxx . . $ int64_t#) instance);
99 }
100 if (oval_schema_version_cmpcxx . . $ oval_schema_version_cmp(b4d0c2bfb97f274f).(over, OVAL_SCHEMA_VERSIONcxx . . $ `src/OVAL/public/oval_schema_version.h:39:9`!(5.6)) < 0) {
101 se_filepath = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
102 } else {
103 const size_t path_len = strlencxx . . $ strlen(751346f8406fd082).(path);
104 /* Avoid 2 slashes */
105 if (path_len >= 1 && path[path_len - 1] == FILE_SEPARATORcxx . . $ `src/OVAL/probes/independent/textfilecontent_probe.c:77:9`!) {
106 se_filepath = SEXP_string_newfcxx . . $ SEXP_string_newf(b2137ed863725962).("%s%s", path, filename);
107 } else {
108 se_filepath = SEXP_string_newfcxx . . $ SEXP_string_newf(b2137ed863725962).("%s%c%s", path, FILE_SEPARATORcxx . . $ `src/OVAL/probes/independent/textfilecontent_probe.c:77:9`!, filename);
109 }
110 }
111
112 item = probe_item_createcxx . . $ probe_item_create(36b16a6622050a9c).(OVAL_INDEPENDENT_TEXT_FILE_CONTENTcxx . . $ OVAL_INDEPENDENT_TEXT_FILE_CONTENT., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
113 "filepath", OVAL_DATATYPE_SEXPcxx . . $ OVAL_DATATYPE_SEXP., se_filepath,
114 "path", OVAL_DATATYPE_STRINGcxx . . $ OVAL_DATATYPE_STRING., path,
115 "filename", OVAL_DATATYPE_STRINGcxx . . $ OVAL_DATATYPE_STRING., filename,
116 "pattern", OVAL_DATATYPE_STRINGcxx . . $ OVAL_DATATYPE_STRING., pattern,
117 "instance", OVAL_DATATYPE_SEXPcxx . . $ OVAL_DATATYPE_SEXP., se_instance,
118 "line", OVAL_DATATYPE_STRINGcxx . . $ OVAL_DATATYPE_STRING., pattern,
119 "text", OVAL_DATATYPE_STRINGcxx . . $ OVAL_DATATYPE_STRING., substrs[0],
120 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
121
122 SEXP_string_freecxx . . $ SEXP_string_free(c6b28547479798f4).(se_filepath);
123 SEXP_string_freecxx . . $ SEXP_string_free(c6b28547479798f4).(se_instance);
124
125 for (i = 1; i < substr_cnt; ++i) {
126 probe_item_ent_addcxx . . $ probe_item_ent_add(9f488d3b029fc9d1). (item, "subexpression", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, r0 = SEXP_string_newcxx . . $ SEXP_string_new(a588a58d7514eee2). (substrs[i], strlencxx . . $ strlen(751346f8406fd082). (substrs[i])));
127 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (r0);
128 }
129
130 return item;
131 }
132
133 struct pfdatacxx . . $ pfdata# {
134 char *patterncxx . . $ pfdata#pattern.;
135 SEXP_tcxx . . $ SEXP_t# *filename_entcxx . . $ pfdata#filename_ent.;
136 probe_ctxcxx . . $ probe_ctx# *ctxcxx . . $ pfdata#ctx.;
137 };
138
139 static int process_filecxx . . $ process_file(5eb55a77b3b9ca58).(const char *prefix, const char *path, const char *filename, void *arg, oval_schema_version_tcxx . . $ oval_schema_version_t# over, struct oscap_listcxx . . $ oscap_list# *blocked_paths)
140 {
141 struct pfdatacxx . . $ pfdata# *pfd = (struct pfdatacxx . . $ pfdata# *) arg;
142 int ret = 0, path_len, filename_len;
143 char *whole_path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, *whole_path_with_prefix = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
144 FILEcxx . . $ FILE# *fp = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
145 struct statcxx . . $ stat# st;
146 char **substrs = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
147 int substr_cnt = 0;
148
149 // todo: move to probe_main()?
150 int erroffset = -1;
151 oscap_pcre_tcxx . . $ oscap_pcre_t# *re = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
152 char *error;
153
154 re = oscap_pcre_compilecxx . . $ oscap_pcre_compile(29ca11f2d36a957a).(pfd->patterncxx . . $ pfdata#pattern., OSCAP_PCRE_OPTS_UTF8cxx . . $ OSCAP_PCRE_OPTS_UTF8., &error, &erroffset);
155 if (re == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
156 oscap_pcre_err_freecxx . . $ oscap_pcre_err_free(fda57c8427c60377).(error);
157 return -1;
158 }
159
160 if (filename == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
161 goto cleanup;
162
163 path_len = strlencxx . . $ strlen(751346f8406fd082).(path);
164 filename_len = strlencxx . . $ strlen(751346f8406fd082).(filename);
165 whole_path = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(path_len + filename_len + 2);
166 memcpycxx . . $ memcpy(cc817cb175f282d).(whole_path, path, path_len);
167 if (whole_path[path_len - 1] != FILE_SEPARATORcxx . . $ `src/OVAL/probes/independent/textfilecontent_probe.c:77:9`!) {
168 whole_path[path_len] = FILE_SEPARATORcxx . . $ `src/OVAL/probes/independent/textfilecontent_probe.c:77:9`!;
169 ++path_len;
170 }
171 memcpycxx . . $ memcpy(cc817cb175f282d).(whole_path + path_len, filename, filename_len + 1);
172
173 if (probe_path_is_blockedcxx . . $ probe_path_is_blocked(6f786adec44f4a13).(whole_path, blocked_paths)) {
174 goto cleanup;
175 }
176
177 /*
178 * If stat() fails, don't report an error and just skip the file.
179 * This is an expected situation, because the fts_*() functions
180 * are called with the 'FTS_PHYSICAL' option. Normally, stumbling
181 * upon a symlink without a target would cause fts_read() to return
182 * the 'FTS_SLNONE' flag, but the 'FTS_PHYSICAL' option causes it
183 * to return 'FTS_SL' and the presence of a valid target has to
184 * be determined with stat().
185 */
186 whole_path_with_prefix = oscap_path_joincxx . . $ oscap_path_join(a713fe7b9a57cf96).(prefix, whole_path);
187 if (statcxx . . $ stat(a9cd1e23e96f423c).(whole_path_with_prefix, &st) == -1)
188 goto cleanup;
189 if (!S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`!(st.st_mode))
190 goto cleanup;
191
192 fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(whole_path_with_prefix, "rb");
193 if (fp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
194 ret = -2;
195 goto cleanup;
196 }
197
198 int cur_inst = 0;
199 char line[4096];
200 int ofs = 0;
201
202 while (fgetscxx . . $ fgets(efb0aaba7ac866ae).(line, sizeof(line), fp) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
203 substr_cnt = oscap_pcre_get_substringscxx . . $ oscap_pcre_get_substrings(60fafe1d399fec14).(line, &ofs, re, 1, &substrs);
204 if (substr_cnt > 0) {
205 int k;
206 SEXP_tcxx . . $ SEXP_t# *item;
207
208 ++cur_inst;
209 item = create_itemcxx . . $ create_item(91a654989eed1720).(path, filename, pfd->patterncxx . . $ pfdata#pattern.,
210 cur_inst, substrs, substr_cnt, over);
211
212 probe_item_collectcxx . . $ probe_item_collect(11bb80e0c30650d7).(pfd->ctxcxx . . $ pfdata#ctx., item);
213
214 for (k = 0; k < substr_cnt; ++k)
215 freecxx . . $ free(5d22bdacc48458e8).(substrs[k]);
216 freecxx . . $ free(5d22bdacc48458e8).(substrs);
217 }
218 }
219
220 cleanup:
221 if (fp != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
222 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
223 if (whole_path != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
224 freecxx . . $ free(5d22bdacc48458e8).(whole_path);
225 if (re != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
226 oscap_pcre_freecxx . . $ oscap_pcre_free(804bb72472b0846e).(re);
227 freecxx . . $ free(5d22bdacc48458e8).(whole_path_with_prefix);
228
229 return ret;
230 }
231
232 int textfilecontent_probe_offline_mode_supportedcxx . . $ textfilecontent_probe_offline_mode_supported(b126dc7c1de90089).()
233 {
234 return PROBE_OFFLINE_OWNcxx . . $ PROBE_OFFLINE_OWN.;
235 }
236
237 int textfilecontent_probe_maincxx . . $ textfilecontent_probe_main(470536d3ffacaf10).(probe_ctxcxx . . $ probe_ctx# *ctx, void *arg)
238 {
239 SEXP_tcxx . . $ SEXP_t# *path_ent, *filename_ent, *line_ent, *behaviors_ent, *filepath_ent, *probe_in;
240 char *pattern;
241
242 OVAL_FTScxx . . $ OVAL_FTS# *ofts;
243 OVAL_FTSENTcxx . . $ OVAL_FTSENT# *ofts_ent;
244
245 (void)arg;
246
247 probe_in = probe_ctx_getobjectcxx . . $ probe_ctx_getobject(4299b6b53fa804a5).(ctx);
248
249 oval_schema_version_tcxx . . $ oval_schema_version_t# over = probe_obj_get_platform_schema_versioncxx . . $ probe_obj_get_platform_schema_version(5bacca80194debea).(probe_in);
250 path_ent = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "path", 1);
251 filename_ent = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "filename", 1);
252 line_ent = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "line", 1);
253 filepath_ent = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "filepath", 1);
254 behaviors_ent = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "behaviors", 1);
255
256 if ( ((path_ent == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || filename_ent == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) && filepath_ent==NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) ||
257 line_ent==NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ) {
258 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (path_ent);
259 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (filename_ent);
260 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (line_ent);
261 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (filepath_ent);
262 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (behaviors_ent);
263 return PROBE_ENOELMcxx . . $ `src/OVAL/probes/public/probe-common.h:9:9`!;
264 }
265
266 /* get pattern from SEXP */
267 SEXP_tcxx . . $ SEXP_t# *ent_val;
268 ent_val = probe_ent_getvalcxx . . $ probe_ent_getval(2f7f980afaaae0f0).(line_ent);
269 pattern = SEXP_string_cstrcxx . . $ SEXP_string_cstr(c831a2fa12818dfe).(ent_val);
270 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(line_ent);
271 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(ent_val);
272 if (pattern == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
273 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(path_ent);
274 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(filename_ent);
275 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(filepath_ent);
276 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(behaviors_ent);
277 return -1;
278 }
279
280 /* behaviours are not important if filepath is used */
281 if(filepath_ent != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && behaviors_ent != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
282 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4). (behaviors_ent);
283 behaviors_ent = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
284 }
285
286 probe_filebehaviors_canonicalizecxx . . $ probe_filebehaviors_canonicalize(e661f441b24a5de6).(&behaviors_ent);
287
288 struct pfdatacxx . . $ pfdata# pfd;
289
290 pfd.patterncxx . . $ pfdata#pattern. = pattern;
291 pfd.filename_entcxx . . $ pfdata#filename_ent. = filename_ent;
292 pfd.ctxcxx . . $ pfdata#ctx. = ctx;
293
294 const char *prefix = getenvcxx . . $ getenv(a8b8e32347182212).("OSCAP_PROBE_ROOT");
295
296 if ((ofts = oval_fts_open_prefixedcxx . . $ oval_fts_open_prefixed(d2d91f0fe545c5d7).(prefix, path_ent, filename_ent, filepath_ent, behaviors_ent, probe_ctx_getresultcxx . . $ probe_ctx_getresult(4299b6b53fa804a5).(ctx))) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
297 while ((ofts_ent = oval_fts_readcxx . . $ oval_fts_read(592d87f37bbdbe51).(ofts)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
298 if (ofts_ent->fts_infocxx . . $ $anonymous_type_ebdee0feb58b991e_1#fts_info. == FTS_Fcxx . . $ `<external>/345209348829246610/fts.h:126:9`!
299 || ofts_ent->fts_infocxx . . $ $anonymous_type_ebdee0feb58b991e_1#fts_info. == FTS_SLcxx . . $ `<external>/345209348829246610/fts.h:130:9`!) {
300 // todo: handle return code
301 process_filecxx . . $ process_file(5eb55a77b3b9ca58).(prefix, ofts_ent->pathcxx . . $ $anonymous_type_ebdee0feb58b991e_1#path., ofts_ent->filecxx . . $ $anonymous_type_ebdee0feb58b991e_1#file., &pfd, over, ctx->blocked_pathscxx . . $ probe_ctx#blocked_paths.);
302 }
303 oval_ftsent_freecxx . . $ oval_ftsent_free(7e09326da17d8ab0).(ofts_ent);
304 }
305
306 oval_fts_closecxx . . $ oval_fts_close(cd898f79c195c42c).(ofts);
307 }
308
309 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(path_ent);
310 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(filename_ent);
311 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(behaviors_ent);
312 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(filepath_ent);
313 freecxx . . $ free(5d22bdacc48458e8).(pattern);
314
315 return 0;
316 }
317