| 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_H |
| 56 | #include <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" |
| 67 | #include <probe-api.h> |
| 68 | #include <probe/entcmp.h> |
| 69 | #include <probe/probe.h> |
| 70 | #include <probe/option.h> |
| 71 | #include <oval_fts.h> |
| 72 | #include "common/debug_priv.h" |
| 73 | #include "common/util.h" |
| 74 | #include "common/oscap_pcre.h" |
| 75 | #include "textfilecontent_probe.h" |
| 76 | |
| 77 | #define FILE_SEPARATOR '/' |
| 78 | |
| 79 | static SEXP_t *create_item(const char *path, const char *filename, char *pattern, |
| 80 | int instance, char **substrs, int substr_cnt, oval_schema_version_t over) |
| 81 | { |
| 82 | int i; |
| 83 | SEXP_t *item; |
| 84 | SEXP_t *r0; |
| 85 | SEXP_t *se_instance, *se_filepath; |
| 86 | char *text; |
| 87 | |
| 88 | if (strlen(path) + strlen(filename) + 1 > PATH_MAX) { |
| 89 | dE("path+filename too long"); |
| 90 | return (NULL); |
| 91 | } |
| 92 | |
| 93 | if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.4)) < 0) { |
| 94 | pattern = text = NULL; |
| 95 | se_instance = NULL; |
| 96 | } else { |
| 97 | text = substrs[0]; |
| 98 | se_instance = SEXP_number_newu_64((int64_t) instance); |
| 99 | } |
| 100 | if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.6)) < 0) { |
| 101 | se_filepath = NULL; |
| 102 | } else { |
| 103 | const size_t path_len = strlen(path); |
| 104 | /* Avoid 2 slashes */ |
| 105 | if (path_len >= 1 && path[path_len - 1] == FILE_SEPARATOR) { |
| 106 | se_filepath = SEXP_string_newf("%s%s", path, filename); |
| 107 | } else { |
| 108 | se_filepath = SEXP_string_newf("%s%c%s", path, FILE_SEPARATOR, filename); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | item = probe_item_create(OVAL_INDEPENDENT_TEXT_FILE_CONTENT, NULL, |
| 113 | "filepath", OVAL_DATATYPE_SEXP, se_filepath, |
| 114 | "path", OVAL_DATATYPE_STRING, path, |
| 115 | "filename", OVAL_DATATYPE_STRING, filename, |
| 116 | "pattern", OVAL_DATATYPE_STRING, pattern, |
| 117 | "instance", OVAL_DATATYPE_SEXP, se_instance, |
| 118 | "line", OVAL_DATATYPE_STRING, pattern, |
| 119 | "text", OVAL_DATATYPE_STRING, substrs[0], |
| 120 | NULL); |
| 121 | |
| 122 | SEXP_string_free(se_filepath); |
| 123 | SEXP_string_free(se_instance); |
| 124 | |
| 125 | for (i = 1; i < substr_cnt; ++i) { |
| 126 | probe_item_ent_add (item, "subexpression", NULL, r0 = SEXP_string_new (substrs[i], strlen (substrs[i]))); |
| 127 | SEXP_free (r0); |
| 128 | } |
| 129 | |
| 130 | return item; |
| 131 | } |
| 132 | |
| 133 | struct pfdata { |
| 134 | char *pattern; |
| 135 | SEXP_t *filename_ent; |
| 136 | probe_ctx *ctx; |
| 137 | }; |
| 138 | |
| 139 | static int process_file(const char *prefix, const char *path, const char *filename, void *arg, oval_schema_version_t over, struct oscap_list *blocked_paths) |
| 140 | { |
| 141 | struct pfdata *pfd = (struct pfdata *) arg; |
| 142 | int ret = 0, path_len, filename_len; |
| 143 | char *whole_path = NULL, *whole_path_with_prefix = NULL; |
| 144 | FILE *fp = NULL; |
| 145 | struct stat st; |
| 146 | char **substrs = NULL; |
| 147 | int substr_cnt = 0; |
| 148 | |
| 149 | // todo: move to probe_main()? |
| 150 | int erroffset = -1; |
| 151 | oscap_pcre_t *re = NULL; |
| 152 | char *error; |
| 153 | |
| 154 | re = oscap_pcre_compile(pfd->pattern, OSCAP_PCRE_OPTS_UTF8, &error, &erroffset); |
| 155 | if (re == NULL) { |
| 156 | oscap_pcre_err_free(error); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | if (filename == NULL) |
| 161 | goto cleanup; |
| 162 | |
| 163 | path_len = strlen(path); |
| 164 | filename_len = strlen(filename); |
| 165 | whole_path = malloc(path_len + filename_len + 2); |
| 166 | memcpy(whole_path, path, path_len); |
| 167 | if (whole_path[path_len - 1] != FILE_SEPARATOR) { |
| 168 | whole_path[path_len] = FILE_SEPARATOR; |
| 169 | ++path_len; |
| 170 | } |
| 171 | memcpy(whole_path + path_len, filename, filename_len + 1); |
| 172 | |
| 173 | if (probe_path_is_blocked(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_join(prefix, whole_path); |
| 187 | if (stat(whole_path_with_prefix, &st) == -1) |
| 188 | goto cleanup; |
| 189 | if (!S_ISREG(st.st_mode)) |
| 190 | goto cleanup; |
| 191 | |
| 192 | fp = fopen(whole_path_with_prefix, "rb"); |
| 193 | if (fp == NULL) { |
| 194 | ret = -2; |
| 195 | goto cleanup; |
| 196 | } |
| 197 | |
| 198 | int cur_inst = 0; |
| 199 | char line[4096]; |
| 200 | int ofs = 0; |
| 201 | |
| 202 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 203 | substr_cnt = oscap_pcre_get_substrings(line, &ofs, re, 1, &substrs); |
| 204 | if (substr_cnt > 0) { |
| 205 | int k; |
| 206 | SEXP_t *item; |
| 207 | |
| 208 | ++cur_inst; |
| 209 | item = create_item(path, filename, pfd->pattern, |
| 210 | cur_inst, substrs, substr_cnt, over); |
| 211 | |
| 212 | probe_item_collect(pfd->ctx, item); |
| 213 | |
| 214 | for (k = 0; k < substr_cnt; ++k) |
| 215 | free(substrs[k]); |
| 216 | free(substrs); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | cleanup: |
| 221 | if (fp != NULL) |
| 222 | fclose(fp); |
| 223 | if (whole_path != NULL) |
| 224 | free(whole_path); |
| 225 | if (re != NULL) |
| 226 | oscap_pcre_free(re); |
| 227 | free(whole_path_with_prefix); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | int textfilecontent_probe_offline_mode_supported() |
| 233 | { |
| 234 | return PROBE_OFFLINE_OWN; |
| 235 | } |
| 236 | |
| 237 | int textfilecontent_probe_main(probe_ctx *ctx, void *arg) |
| 238 | { |
| 239 | SEXP_t *path_ent, *filename_ent, *line_ent, *behaviors_ent, *filepath_ent, *probe_in; |
| 240 | char *pattern; |
| 241 | |
| 242 | OVAL_FTS *ofts; |
| 243 | OVAL_FTSENT *ofts_ent; |
| 244 | |
| 245 | (void)arg; |
| 246 | |
| 247 | probe_in = probe_ctx_getobject(ctx); |
| 248 | |
| 249 | oval_schema_version_t over = probe_obj_get_platform_schema_version(probe_in); |
| 250 | path_ent = probe_obj_getent(probe_in, "path", 1); |
| 251 | filename_ent = probe_obj_getent(probe_in, "filename", 1); |
| 252 | line_ent = probe_obj_getent(probe_in, "line", 1); |
| 253 | filepath_ent = probe_obj_getent(probe_in, "filepath", 1); |
| 254 | behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1); |
| 255 | |
| 256 | if ( ((path_ent == NULL || filename_ent == NULL) && filepath_ent==NULL) || |
| 257 | line_ent==NULL ) { |
| 258 | SEXP_free (path_ent); |
| 259 | SEXP_free (filename_ent); |
| 260 | SEXP_free (line_ent); |
| 261 | SEXP_free (filepath_ent); |
| 262 | SEXP_free (behaviors_ent); |
| 263 | return PROBE_ENOELM; |
| 264 | } |
| 265 | |
| 266 | /* get pattern from SEXP */ |
| 267 | SEXP_t *ent_val; |
| 268 | ent_val = probe_ent_getval(line_ent); |
| 269 | pattern = SEXP_string_cstr(ent_val); |
| 270 | SEXP_free(line_ent); |
| 271 | SEXP_free(ent_val); |
| 272 | if (pattern == NULL) { |
| 273 | SEXP_free(path_ent); |
| 274 | SEXP_free(filename_ent); |
| 275 | SEXP_free(filepath_ent); |
| 276 | SEXP_free(behaviors_ent); |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | /* behaviours are not important if filepath is used */ |
| 281 | if(filepath_ent != NULL && behaviors_ent != NULL) { |
| 282 | SEXP_free (behaviors_ent); |
| 283 | behaviors_ent = NULL; |
| 284 | } |
| 285 | |
| 286 | probe_filebehaviors_canonicalize(&behaviors_ent); |
| 287 | |
| 288 | struct pfdata pfd; |
| 289 | |
| 290 | pfd.pattern = pattern; |
| 291 | pfd.filename_ent = filename_ent; |
| 292 | pfd.ctx = ctx; |
| 293 | |
| 294 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 295 | |
| 296 | if ((ofts = oval_fts_open_prefixed(prefix, path_ent, filename_ent, filepath_ent, behaviors_ent, probe_ctx_getresult(ctx))) != NULL) { |
| 297 | while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 298 | if (ofts_ent->fts_info == FTS_F |
| 299 | || ofts_ent->fts_info == FTS_SL) { |
| 300 | // todo: handle return code |
| 301 | process_file(prefix, ofts_ent->path, ofts_ent->file, &pfd, over, ctx->blocked_paths); |
| 302 | } |
| 303 | oval_ftsent_free(ofts_ent); |
| 304 | } |
| 305 | |
| 306 | oval_fts_close(ofts); |
| 307 | } |
| 308 | |
| 309 | SEXP_free(path_ent); |
| 310 | SEXP_free(filename_ent); |
| 311 | SEXP_free(behaviors_ent); |
| 312 | SEXP_free(filepath_ent); |
| 313 | free(pattern); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 |