| 1 | /** |
| 2 | * @file cpedict.c |
| 3 | * \brief Interface to Common Platform Enumeration (CPE) Dictionary. |
| 4 | * |
| 5 | * See more details at http://nvd.nist.gov/cpe.cfm |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2014 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 | * Maros Barabas <[email protected]> |
| 28 | * Lukas Kuklinek <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "public/cpe_dict.h" |
| 36 | #include "cpedict_priv.h" |
| 37 | |
| 38 | #include "common/list.h" |
| 39 | #include "common/util.h" |
| 40 | #include "common/_error.h" |
| 41 | #include "common/xmlns_priv.h" |
| 42 | #include "common/elements.h" |
| 43 | #include "common/xmltext_priv.h" |
| 44 | #include "source/oscap_source_priv.h" |
| 45 | #include "source/public/oscap_source.h" |
| 46 | #include <string.h> |
| 47 | |
| 48 | #define CPE_DICT_SUPPORTED "2.3" |
| 49 | |
| 50 | struct cpe_dict_model *cpe_dict_model_import_source(struct oscap_source *source) |
| 51 | { |
| 52 | xmlTextReader *reader = oscap_source_get_xmlTextReader(source); |
| 53 | if (reader == NULL) { |
| 54 | return NULL; |
| 55 | } |
| 56 | struct cpe_dict_model *dict = NULL; |
| 57 | struct cpe_parser_ctx *ctx = cpe_parser_ctx_from_reader(reader); |
| 58 | if (ctx) { |
| 59 | xmlTextReaderNextNode(cpe_parser_ctx_get_reader(ctx)); |
| 60 | dict = cpe_dict_model_parse(ctx); |
| 61 | if (dict != NULL) { |
| 62 | dict->origin_file = oscap_strdup(oscap_source_readable_origin(source)); |
| 63 | } |
| 64 | } |
| 65 | cpe_parser_ctx_free(ctx); |
| 66 | xmlFreeTextReader(reader); |
| 67 | return dict; |
| 68 | } |
| 69 | |
| 70 | void cpe_dict_model_export(const struct cpe_dict_model *dict, const char *file) |
| 71 | { |
| 72 | |
| 73 | __attribute__nonnull__(dict); |
| 74 | __attribute__nonnull__(file); |
| 75 | |
| 76 | if (file == NULL) |
| 77 | return; |
| 78 | |
| 79 | cpe_dict_model_export_xml(dict, file); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | bool cpe_name_match_dict(struct cpe_name * cpe, struct cpe_dict_model * dict) |
| 84 | { |
| 85 | // FIXME: We could match faster by matching per component and storing all |
| 86 | // cpe_dict items in a big tree where leaves contain cpe_items and |
| 87 | // inner nodes are the components. |
| 88 | |
| 89 | __attribute__nonnull__(cpe); |
| 90 | __attribute__nonnull__(dict); |
| 91 | |
| 92 | if (cpe == NULL || dict == NULL) |
| 93 | return false; |
| 94 | |
| 95 | struct cpe_item_iterator *items = cpe_dict_model_get_items(dict); |
| 96 | |
| 97 | bool ret = false; |
| 98 | while (cpe_item_iterator_has_more(items)) { |
| 99 | struct cpe_item* item = cpe_item_iterator_next(items); |
| 100 | struct cpe_name* name = cpe_item_get_name(item); |
| 101 | |
| 102 | if (cpe_name_match_one(name, cpe)) { |
| 103 | ret = true; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | cpe_item_iterator_free(items); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | bool cpe_name_applicable_dict(struct cpe_name *cpe, struct cpe_dict_model *dict, cpe_check_fn cb, void* usr) |
| 112 | { |
| 113 | // FIXME: We could match faster by matching per component and storing all |
| 114 | // cpe_dict items in a big tree where leaves contain cpe_items and |
| 115 | // inner nodes are the components. |
| 116 | |
| 117 | __attribute__nonnull__(cpe); |
| 118 | __attribute__nonnull__(dict); |
| 119 | |
| 120 | if (cpe == NULL || dict == NULL) |
| 121 | return false; |
| 122 | |
| 123 | struct cpe_item_iterator *items = cpe_dict_model_get_items(dict); |
| 124 | |
| 125 | // essentially, we want at least one applicable match so as soon as we find |
| 126 | // a match we break and return true |
| 127 | |
| 128 | bool ret = false; |
| 129 | while (cpe_item_iterator_has_more(items)) { |
| 130 | struct cpe_item* item = cpe_item_iterator_next(items); |
| 131 | struct cpe_name* name = cpe_item_get_name(item); |
| 132 | |
| 133 | if (cpe_name_match_one(cpe, name) && cpe_item_is_applicable(item, cb, usr)) { |
| 134 | ret = true; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | cpe_item_iterator_free(items); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static bool cpe_check_evaluate(const struct cpe_check* check, cpe_check_fn cb, void* usr) |
| 143 | { |
| 144 | const char* sys = cpe_check_get_system(check); |
| 145 | const char* href = cpe_check_get_href(check); |
| 146 | const char* name = cpe_check_get_identifier(check); |
| 147 | |
| 148 | return cb(sys, href, name, usr); |
| 149 | } |
| 150 | |
| 151 | bool cpe_item_is_applicable(struct cpe_item* item, cpe_check_fn cb, void* usr) |
| 152 | { |
| 153 | struct cpe_check_iterator* checks = cpe_item_get_checks(item); |
| 154 | |
| 155 | bool ret = false; |
| 156 | while (cpe_check_iterator_has_more(checks)) { |
| 157 | struct cpe_check* check = cpe_check_iterator_next(checks); |
| 158 | if (cpe_check_evaluate(check, cb, usr)) { |
| 159 | ret = true; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | cpe_check_iterator_free(checks); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | const char * cpe_dict_model_supported(void) |
| 169 | { |
| 170 | return CPE_DICT_SUPPORTED; |
| 171 | } |
| 172 | |
| 173 | char *cpe_dict_detect_version_priv(xmlTextReader *reader) |
| 174 | { |
| 175 | char *version = NULL; |
| 176 | /* find root element */ |
| 177 | while (xmlTextReaderRead(reader) == 1 |
| 178 | && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); |
| 179 | |
| 180 | const char* elm_name = (const char *) xmlTextReaderConstLocalName(reader); |
| 181 | if (!elm_name || strcmp("cpe-list", elm_name)) { |
| 182 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Expected root element name 'cpe-list', found '%s'.", elm_name); |
| 183 | return NULL; |
| 184 | } |
| 185 | const char* ns_uri = (const char *) xmlTextReaderConstNamespaceUri(reader); |
| 186 | if (ns_uri == NULL) { |
| 187 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Expected namespace uri for cpe-list, found none."); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /* find generator */ |
| 192 | while (xmlTextReaderRead(reader) == 1 |
| 193 | && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); |
| 194 | elm_name = (const char *) xmlTextReaderConstLocalName(reader); |
| 195 | |
| 196 | // if the element found is a generator, use it to figure out the version |
| 197 | if (elm_name && strcmp(elm_name, "generator") == 0) { |
| 198 | /* find schema_version */ |
| 199 | const int depth = xmlTextReaderDepth(reader); |
| 200 | while (xmlTextReaderRead(reader) == 1 && xmlTextReaderDepth(reader) > depth) { |
| 201 | if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) |
| 202 | continue; |
| 203 | |
| 204 | elm_name = (const char *) xmlTextReaderConstLocalName(reader); |
| 205 | if (!strcmp(elm_name, "schema_version")) { |
| 206 | version = oscap_element_string_copy(reader); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | else { |
| 212 | if (strcmp(ns_uri, XMLNS_CPE2D) == 0) { |
| 213 | // return the newest 2.x version we support |
| 214 | version = oscap_strdup("2.3"); |
| 215 | } |
| 216 | } |
| 217 | return version; |
| 218 | } |