| 1 | /** |
| 2 | * @file xmlfilecontent_probe.c |
| 3 | * @brief xmlfilecontent probe |
| 4 | * @author "Tomas Heinrich" <[email protected]> |
| 5 | * |
| 6 | * 2010/06/13 [email protected] |
| 7 | * This probe is able to process an xmlfilecontent_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 | /* |
| 34 | * xmlfilecontent probe: |
| 35 | * |
| 36 | * xmlfilecontent_object |
| 37 | * xmlfilecontentbehaviors behaviors |
| 38 | * string path |
| 39 | * string filename |
| 40 | * string xpath |
| 41 | * |
| 42 | * xmlfilecontent_item |
| 43 | * attrs |
| 44 | * id |
| 45 | * status_enum status |
| 46 | * string path |
| 47 | * string filename |
| 48 | * string xpath |
| 49 | * string value_of |
| 50 | */ |
| 51 | |
| 52 | #ifdef HAVE_CONFIG_H |
| 53 | #include <config.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <stdlib.h> |
| 57 | #include <string.h> |
| 58 | #include <limits.h> |
| 59 | |
| 60 | #include <libxml/tree.h> |
| 61 | #include <libxml/parser.h> |
| 62 | #include <libxml/xpath.h> |
| 63 | #include <libxml/xpathInternals.h> |
| 64 | #include <libxslt/xslt.h> |
| 65 | #include <libxslt/xsltInternals.h> |
| 66 | #include <libxslt/transform.h> |
| 67 | #include <libxslt/xsltutils.h> |
| 68 | |
| 69 | #include "_seap.h" |
| 70 | #include <probe-api.h> |
| 71 | #include <probe/probe.h> |
| 72 | #include <probe/option.h> |
| 73 | #include <oval_fts.h> |
| 74 | #include <common/debug_priv.h> |
| 75 | #include "xmlfilecontent_probe.h" |
| 76 | |
| 77 | #define FILE_SEPARATOR '/' |
| 78 | |
| 79 | struct pfdata { |
| 80 | SEXP_t *filename_ent; |
| 81 | char *xpath; |
| 82 | probe_ctx *ctx; |
| 83 | }; |
| 84 | |
| 85 | static void dummy_err_func(void * ctx, const char * msg, ...) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | int xmlfilecontent_probe_offline_mode_supported() |
| 90 | { |
| 91 | return PROBE_OFFLINE_OWN; |
| 92 | } |
| 93 | |
| 94 | void *xmlfilecontent_probe_init(void) |
| 95 | { |
| 96 | /* init libxml */ |
| 97 | //LIBXML_TEST_VERSION; |
| 98 | xmlInitParser(); |
| 99 | xmlSetGenericErrorFunc(NULL, dummy_err_func); |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | void xmlfilecontent_probe_fini(void *arg) |
| 105 | { |
| 106 | (void)arg; |
| 107 | /* deinit libxml */ |
| 108 | xmlCleanupParser(); |
| 109 | } |
| 110 | |
| 111 | static xmlDocPtr strip_ns(xmlDocPtr doc) |
| 112 | { |
| 113 | const char template[] = |
| 114 | "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" |
| 115 | " <xsl:template match=\"*\">" |
| 116 | " <xsl:element name=\"{local-name()}\">" |
| 117 | " <xsl:apply-templates select=\"node()|@*\"/>" |
| 118 | " </xsl:element>" |
| 119 | " </xsl:template>" |
| 120 | " <xsl:template match=\"@*\">" |
| 121 | " <xsl:attribute name=\"{local-name()}\">" |
| 122 | " <xsl:value-of select=\".\"/>" |
| 123 | " </xsl:attribute>" |
| 124 | " </xsl:template>" |
| 125 | "</xsl:stylesheet>"; |
| 126 | xmlDocPtr stylesheet_doc = xmlParseMemory(template, strlen(template)); |
| 127 | if (stylesheet_doc == NULL) { |
| 128 | fprintf(stderr, "Can't parse the XSLT template\n"); |
| 129 | return NULL; |
| 130 | } |
| 131 | xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(stylesheet_doc); |
| 132 | if (stylesheet == NULL) { |
| 133 | fprintf(stderr, "Can't parse the XSLT stylesheet\n"); |
| 134 | xmlFreeDoc(stylesheet_doc); |
| 135 | return NULL; |
| 136 | } |
| 137 | xmlDocPtr result = xsltApplyStylesheet(stylesheet, doc, NULL); |
| 138 | if (result == NULL) { |
| 139 | fprintf(stderr, "Can't apply XSLT on the document\n"); |
| 140 | } |
| 141 | xsltFreeStylesheet(stylesheet); |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | static int process_file(const char *prefix, const char *path, const char *filename, struct pfdata *pfd, struct oscap_list *blocked_paths) |
| 146 | { |
| 147 | int ret = 0, path_len, filename_len; |
| 148 | char *whole_path = NULL; |
| 149 | xmlDoc *doc = NULL; |
| 150 | xmlDoc *doc_no_ns = NULL; |
| 151 | xmlXPathContext *xpath_ctx = NULL; |
| 152 | xmlXPathObject *xpath_obj = NULL; |
| 153 | SEXP_t *item = NULL; |
| 154 | SEXP_t *r0; |
| 155 | char filepath[PATH_MAX+1]; |
| 156 | |
| 157 | if (filename == NULL) |
| 158 | goto cleanup; |
| 159 | |
| 160 | path_len = strlen(path); |
| 161 | filename_len = strlen(filename); |
| 162 | whole_path = malloc(sizeof (char) * (path_len + filename_len + 2)); |
| 163 | |
| 164 | memcpy (whole_path, path, sizeof (char) * path_len); |
| 165 | |
| 166 | if (whole_path[path_len - 1] != FILE_SEPARATOR) { |
| 167 | whole_path[path_len] = FILE_SEPARATOR; |
| 168 | ++path_len; |
| 169 | } |
| 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 | if (prefix == NULL) { |
| 178 | doc = xmlParseFile(whole_path); |
| 179 | } else { |
| 180 | char *path_with_prefix = oscap_path_join(prefix, whole_path); |
| 181 | doc = xmlParseFile(path_with_prefix); |
| 182 | free(path_with_prefix); |
| 183 | } |
| 184 | |
| 185 | if (doc == NULL) { |
| 186 | SEXP_t *msg; |
| 187 | msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "Can't parse '%s'.", whole_path); |
| 188 | probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); |
| 189 | SEXP_free(msg); |
| 190 | probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); |
| 191 | |
| 192 | ret = -1; |
| 193 | goto cleanup; |
| 194 | } |
| 195 | |
| 196 | /* Remove the namespace from the examined document. The XPath expressions |
| 197 | * will be evaluated as if the namespace is ignored. Even though the |
| 198 | * xmlfilecontent should use standardized XPath, existing content expects |
| 199 | * this behavior. |
| 200 | */ |
| 201 | doc_no_ns = strip_ns(doc); |
| 202 | if (doc_no_ns == NULL) { |
| 203 | SEXP_t *msg; |
| 204 | msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, |
| 205 | "Can't remove namespaces from '%s'.", whole_path); |
| 206 | probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); |
| 207 | SEXP_free(msg); |
| 208 | probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); |
| 209 | ret = -1; |
| 210 | goto cleanup; |
| 211 | } |
| 212 | |
| 213 | /* evaluate xpath */ |
| 214 | xpath_ctx = xmlXPathNewContext(doc_no_ns); |
| 215 | if (xpath_ctx == NULL) { |
| 216 | SEXP_t *msg; |
| 217 | msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "xmlXPathNewContext() error."); |
| 218 | probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); |
| 219 | SEXP_free(msg); |
| 220 | probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); |
| 221 | |
| 222 | ret = -2; |
| 223 | goto cleanup; |
| 224 | } |
| 225 | |
| 226 | xpath_obj = xmlXPathEvalExpression(BAD_CAST pfd->xpath, xpath_ctx); |
| 227 | if (xpath_obj == NULL) { |
| 228 | SEXP_t *msg; |
| 229 | msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "xmlXPathEvalExpression() error"); |
| 230 | probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); |
| 231 | SEXP_free(msg); |
| 232 | probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); |
| 233 | |
| 234 | ret = -3; |
| 235 | goto cleanup; |
| 236 | } |
| 237 | |
| 238 | /* Avoid 2 slashes */ |
| 239 | if (path_len >= 1 && path[path_len - 1] == FILE_SEPARATOR) { |
| 240 | snprintf(filepath, PATH_MAX, "%s%s", path, filename); |
| 241 | } else { |
| 242 | snprintf(filepath, PATH_MAX, "%s%c%s", path, FILE_SEPARATOR, filename); |
| 243 | } |
| 244 | |
| 245 | item = probe_item_create(OVAL_INDEPENDENT_XML_FILE_CONTENT, NULL, |
| 246 | "filepath", OVAL_DATATYPE_STRING, filepath, |
| 247 | "path", OVAL_DATATYPE_STRING, path, |
| 248 | "filename", OVAL_DATATYPE_STRING, filename, |
| 249 | "xpath", OVAL_DATATYPE_STRING, pfd->xpath, |
| 250 | NULL); |
| 251 | |
| 252 | dD("xpath obj type: %d.", xpath_obj->type); |
| 253 | switch(xpath_obj->type) { |
| 254 | case XPATH_BOOLEAN: |
| 255 | { |
| 256 | SEXP_t *val; |
| 257 | int b; |
| 258 | |
| 259 | b = xmlXPathCastToBoolean(xpath_obj); |
| 260 | val = SEXP_number_newb(b); |
| 261 | probe_item_ent_add(item, "value_of", NULL, val); |
| 262 | SEXP_free(val); |
| 263 | break; |
| 264 | } |
| 265 | case XPATH_NUMBER: |
| 266 | { |
| 267 | SEXP_t *val; |
| 268 | double d; |
| 269 | |
| 270 | d = xmlXPathCastToNumber(xpath_obj); |
| 271 | val = SEXP_number_newi_32(d); |
| 272 | probe_item_ent_add(item, "value_of", NULL, val); |
| 273 | SEXP_free(val); |
| 274 | break; |
| 275 | } |
| 276 | case XPATH_STRING: |
| 277 | { |
| 278 | SEXP_t *val; |
| 279 | char *s; |
| 280 | |
| 281 | s = (char *) xmlXPathCastToString(xpath_obj); |
| 282 | val = SEXP_string_newf("%s", s); |
| 283 | xmlFree(s); |
| 284 | probe_item_ent_add(item, "value_of", NULL, val); |
| 285 | SEXP_free(val); |
| 286 | break; |
| 287 | } |
| 288 | case XPATH_NODESET: |
| 289 | { |
| 290 | int node_cnt, i; |
| 291 | xmlNodeSet *nodes; |
| 292 | xmlNode *cur_node, **node_tab; |
| 293 | |
| 294 | nodes = xpath_obj->nodesetval; |
| 295 | if (nodes == NULL) { |
| 296 | ret = -4; |
| 297 | goto cleanup; |
| 298 | } |
| 299 | |
| 300 | node_cnt = nodes->nodeNr; |
| 301 | dD("node_cnt: %d.", node_cnt); |
| 302 | if (node_cnt <= 0) { |
| 303 | ret = -5; |
| 304 | goto cleanup; |
| 305 | } else { |
| 306 | node_tab = nodes->nodeTab; |
| 307 | for (i = 0; i < node_cnt; ++i) { |
| 308 | cur_node = node_tab[i]; |
| 309 | dD("node[%d] line: %ld, name: '%s', type: %d.", |
| 310 | i, XML_GET_LINE(cur_node), cur_node->name, cur_node->type); |
| 311 | if (cur_node->type == XML_ATTRIBUTE_NODE |
| 312 | || cur_node->type == XML_TEXT_NODE) { |
| 313 | xmlChar *value; |
| 314 | |
| 315 | value = xmlNodeGetContent(cur_node); |
| 316 | probe_item_ent_add(item, "value_of", NULL, |
| 317 | r0 = SEXP_string_newf ("%s", (char *) value)); |
| 318 | xmlFree(value); |
| 319 | SEXP_free (r0); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | break; |
| 324 | } |
| 325 | default: |
| 326 | probe_item_setstatus(item, SYSCHAR_STATUS_DOES_NOT_EXIST); |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | probe_item_collect(pfd->ctx, item); |
| 331 | item = NULL; |
| 332 | cleanup: |
| 333 | if (item != NULL) |
| 334 | SEXP_free(item); |
| 335 | if (xpath_obj != NULL) |
| 336 | xmlXPathFreeObject(xpath_obj); |
| 337 | if (xpath_ctx != NULL) |
| 338 | xmlXPathFreeContext(xpath_ctx); |
| 339 | if (doc != NULL) |
| 340 | xmlFreeDoc(doc); |
| 341 | if (doc_no_ns != NULL) |
| 342 | xmlFreeDoc(doc_no_ns); |
| 343 | if (whole_path != NULL) |
| 344 | free(whole_path); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | int xmlfilecontent_probe_main(probe_ctx *ctx, void *arg) |
| 350 | { |
| 351 | SEXP_t *path_ent, *filename_ent, *xpath_ent, *behaviors_ent, *filepath_ent, *probe_in; |
| 352 | SEXP_t *r0; |
| 353 | |
| 354 | OVAL_FTS *ofts; |
| 355 | OVAL_FTSENT *ofts_ent; |
| 356 | |
| 357 | (void)arg; |
| 358 | |
| 359 | probe_in = probe_ctx_getobject(ctx); |
| 360 | |
| 361 | path_ent = probe_obj_getent(probe_in, "path", 1); |
| 362 | filename_ent = probe_obj_getent(probe_in, "filename", 1); |
| 363 | xpath_ent = probe_obj_getent(probe_in, "xpath", 1); |
| 364 | filepath_ent = probe_obj_getent(probe_in, "filepath", 1); |
| 365 | behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1); |
| 366 | |
| 367 | /* we want (path+filename or filepath) + xpath */ |
| 368 | if ( ((path_ent == NULL || filename_ent == NULL) && filepath_ent==NULL ) || |
| 369 | xpath_ent==NULL) { |
| 370 | SEXP_free (path_ent); |
| 371 | SEXP_free (filename_ent); |
| 372 | SEXP_free (filepath_ent); |
| 373 | SEXP_free (xpath_ent); |
| 374 | SEXP_free (behaviors_ent); |
| 375 | |
| 376 | return PROBE_ENOELM; |
| 377 | } |
| 378 | |
| 379 | /* behaviours are not important if filepath is used */ |
| 380 | if(filepath_ent != NULL && behaviors_ent != NULL) { |
| 381 | SEXP_free (behaviors_ent); |
| 382 | behaviors_ent = NULL; |
| 383 | } |
| 384 | |
| 385 | probe_filebehaviors_canonicalize(&behaviors_ent); |
| 386 | |
| 387 | struct pfdata pfd; |
| 388 | |
| 389 | pfd.xpath = SEXP_string_cstr(r0 = probe_ent_getval(xpath_ent)); |
| 390 | SEXP_free (r0); |
| 391 | |
| 392 | pfd.filename_ent = filename_ent; |
| 393 | pfd.ctx = ctx; |
| 394 | |
| 395 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 396 | |
| 397 | if ((ofts = oval_fts_open_prefixed(prefix, path_ent, filename_ent, filepath_ent, behaviors_ent, probe_ctx_getresult(ctx))) != NULL) { |
| 398 | while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 399 | process_file(prefix, ofts_ent->path, ofts_ent->file, &pfd, ctx->blocked_paths); |
| 400 | oval_ftsent_free(ofts_ent); |
| 401 | } |
| 402 | |
| 403 | oval_fts_close(ofts); |
| 404 | } |
| 405 | |
| 406 | free(pfd.xpath); |
| 407 | SEXP_free (path_ent); |
| 408 | SEXP_free (filename_ent); |
| 409 | SEXP_free (xpath_ent); |
| 410 | SEXP_free (behaviors_ent); |
| 411 | SEXP_free (filepath_ent); |
| 412 | |
| 413 | return 0; |
| 414 | } |