| 1 | /** |
| 2 | * @file cpelang.c |
| 3 | * \brief Interface to Common Platform Enumeration (CPE) Language |
| 4 | * |
| 5 | * See more details at http://nvd.nist.gov/cpe.cfm |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * Lukas Kuklinek <[email protected]> |
| 28 | * Maros Barabas <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <libxml/tree.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "public/cpe_lang.h" |
| 39 | #include "cpelang_priv.h" |
| 40 | #include "common/util.h" |
| 41 | #include "common/list.h" |
| 42 | #include "source/public/oscap_source.h" |
| 43 | |
| 44 | #define CPE_LANG_SUPPORTED "2.2" |
| 45 | |
| 46 | void cpe_lang_model_export(const struct cpe_lang_model *spec, const char *file) |
| 47 | { |
| 48 | |
| 49 | __attribute__nonnull__(spec); |
| 50 | __attribute__nonnull__(file); |
| 51 | |
| 52 | if (file == NULL) |
| 53 | return; |
| 54 | |
| 55 | cpe_lang_model_export_xml(spec, file); |
| 56 | } |
| 57 | |
| 58 | const char * cpe_lang_model_supported(void) |
| 59 | { |
| 60 | return CPE_LANG_SUPPORTED; |
| 61 | } |
| 62 | |
| 63 | char *cpe_lang_model_detect_version_priv(xmlTextReader *reader) |
| 64 | { |
| 65 | // FIXME: There is no detection logic in there, since there is only |
| 66 | // one version of CPE language so we just return that. |
| 67 | return oscap_strdup("2.3"); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | bool cpe_language_match_str(const char* cpe, const struct cpe_platform* platform) |
| 72 | { |
| 73 | |
| 74 | __attribute__nonnull__(cpe); |
| 75 | __attribute__nonnull__(platform); |
| 76 | |
| 77 | struct cpe_name* cpe_; |
| 78 | bool ret; |
| 79 | |
| 80 | cpe_ = cpe_new(cpe); |
| 81 | if (cpe_ == NULL) return false; |
| 82 | ret = cpe_language_match_cpe(cpe_, platform); |
| 83 | cpe_free(cpe_); |
| 84 | return ret; |
| 85 | } |
| 86 | */ |
| 87 | |
| 88 | static bool cpe_testexpr_evaluate(const struct cpe_testexpr* expr, cpe_check_fn check_cb, cpe_dict_fn dict_cb, void* usr) |
| 89 | { |
| 90 | bool ret = false; |
| 91 | |
| 92 | switch (cpe_testexpr_get_oper(expr) & CPE_LANG_OPER_MASK) { |
| 93 | case CPE_LANG_OPER_AND: |
| 94 | ret = true; |
| 95 | OSCAP_FOREACH(cpe_testexpr, cur, cpe_testexpr_get_meta_expr(expr), |
| 96 | if (!cpe_testexpr_evaluate(cur, check_cb, dict_cb, usr)) { |
| 97 | ret = false; |
| 98 | break; |
| 99 | } |
| 100 | ) |
| 101 | break; |
| 102 | case CPE_LANG_OPER_OR: |
| 103 | OSCAP_FOREACH(cpe_testexpr, cur, cpe_testexpr_get_meta_expr(expr), |
| 104 | if (cpe_testexpr_evaluate(cur, check_cb, dict_cb, usr)) { |
| 105 | ret = true; |
| 106 | break; |
| 107 | } |
| 108 | ) |
| 109 | break; |
| 110 | case CPE_LANG_OPER_MATCH: |
| 111 | ret = dict_cb(cpe_testexpr_get_meta_cpe(expr), usr); |
| 112 | break; |
| 113 | case CPE_LANG_OPER_CHECK: |
| 114 | ret = check_cb(cpe_testexpr_get_meta_check_system(expr), cpe_testexpr_get_meta_check_href(expr), cpe_testexpr_get_meta_check_id(expr), usr); |
| 115 | break; |
| 116 | default: |
| 117 | assert(false); |
| 118 | } |
| 119 | |
| 120 | return (cpe_testexpr_get_oper(expr) & CPE_LANG_OPER_NOT) ? !ret : ret; |
| 121 | } |
| 122 | |
| 123 | bool cpe_platform_applicable_lang_model(const char* platform, struct cpe_lang_model *lang_model, cpe_check_fn check_cb, cpe_dict_fn dict_fn, void* usr) |
| 124 | { |
| 125 | struct cpe_platform* plat = cpe_lang_model_get_item(lang_model, platform); |
| 126 | |
| 127 | if (plat == NULL) // can't find any matching platform implies not applicable |
| 128 | return false; |
| 129 | |
| 130 | const struct cpe_testexpr* expr = cpe_platform_get_expr(plat); |
| 131 | return cpe_testexpr_evaluate(expr, check_cb, dict_fn, usr); |
| 132 | } |