| 1 | /** |
| 2 | * @file elements.c |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina. |
| 7 | * All Rights Reserved. |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2.1 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | * |
| 23 | * Authors: |
| 24 | * Maros Barabas <[email protected]> |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <fcntl.h> |
| 33 | #ifdef OS_WINDOWS |
| 34 | #include <io.h> |
| 35 | #else |
| 36 | #include <unistd.h> |
| 37 | #endif |
| 38 | #include <sys/stat.h> |
| 39 | |
| 40 | #include "public/oscap.h" |
| 41 | #include "util.h" |
| 42 | #include "list.h" |
| 43 | #include "_error.h" |
| 44 | #include "debug_priv.h" |
| 45 | #include "elements.h" |
| 46 | #include "oscap_helpers.h" |
| 47 | |
| 48 | |
| 49 | const struct oscap_string_map OSCAP_BOOL_MAP[] = { |
| 50 | {true, "true"}, {true, "True"}, {true, "TRUE"}, |
| 51 | {true, "yes"}, {true, "Yes"}, {true, "YES"}, |
| 52 | {true, "1"}, {false, NULL} |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | bool oscap_to_start_element(xmlTextReaderPtr reader, int depth) |
| 57 | { |
| 58 | //int olddepth = xmlTextReaderDepth(reader); |
| 59 | while (xmlTextReaderDepth(reader) >= depth) { |
| 60 | switch (xmlTextReaderNodeType(reader)) { |
| 61 | case XML_READER_TYPE_ELEMENT: |
| 62 | if (xmlTextReaderDepth(reader) == depth) |
| 63 | return true; |
| 64 | default: |
| 65 | break; |
| 66 | } |
| 67 | if (xmlTextReaderRead(reader) != 1) |
| 68 | break; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | void oscap_text_consumer(char *text, void *user) |
| 74 | { |
| 75 | char *platform = *(char **)user; |
| 76 | if (platform == NULL) |
| 77 | platform = oscap_strdup(text); |
| 78 | else { |
| 79 | int size = strlen(platform) + strlen(text) + 1; |
| 80 | char *newtext = (char *) malloc(size * sizeof(char)); |
| 81 | *newtext = 0; |
| 82 | strcat(newtext, platform); |
| 83 | strcat(newtext, text); |
| 84 | free(platform); |
| 85 | platform = newtext; |
| 86 | } |
| 87 | *(char **)user = platform; |
| 88 | } |
| 89 | |
| 90 | /* -1 error; 0 OK */ |
| 91 | int oscap_parser_text_value(xmlTextReaderPtr reader, oscap_xml_value_consumer consumer, void *user) |
| 92 | { |
| 93 | int depth = xmlTextReaderDepth(reader); |
| 94 | bool has_value = false; |
| 95 | int ret = 0; |
| 96 | |
| 97 | if (xmlTextReaderIsEmptyElement(reader)) { |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | xmlTextReaderRead(reader); |
| 102 | while (xmlTextReaderDepth(reader) > depth) { |
| 103 | int nodetype = xmlTextReaderNodeType(reader); |
| 104 | if (nodetype == XML_READER_TYPE_CDATA || nodetype == XML_READER_TYPE_TEXT) { |
| 105 | char *value = (char *)xmlTextReaderValue(reader); |
| 106 | (*consumer) (value, user); |
| 107 | free(value); |
| 108 | has_value = true; |
| 109 | } |
| 110 | if (xmlTextReaderRead(reader) != 1) { |
| 111 | ret = -1; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (!has_value) |
| 117 | (*consumer) ("", user); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | char *oscap_element_string_copy(xmlTextReaderPtr reader) |
| 123 | { |
| 124 | int t; |
| 125 | |
| 126 | if (xmlTextReaderIsEmptyElement(reader)) |
| 127 | return NULL; |
| 128 | |
| 129 | t = xmlTextReaderNodeType(reader); |
| 130 | if (t == XML_ELEMENT_NODE || t == XML_ATTRIBUTE_NODE) |
| 131 | xmlTextReaderRead(reader); |
| 132 | if (xmlTextReaderHasValue(reader)) |
| 133 | return (char *)xmlTextReaderValue(reader); |
| 134 | else |
| 135 | return (char *) calloc(1,1); |
| 136 | } |
| 137 | |
| 138 | const char *oscap_element_string_get(xmlTextReaderPtr reader) |
| 139 | { |
| 140 | if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT || |
| 141 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ATTRIBUTE) |
| 142 | xmlTextReaderRead(reader); |
| 143 | if (xmlTextReaderHasValue(reader)) |
| 144 | return (const char *)xmlTextReaderConstValue(reader); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | int oscap_element_depth(xmlTextReaderPtr reader) |
| 149 | { |
| 150 | int depth = xmlTextReaderDepth(reader); |
| 151 | switch (xmlTextReaderNodeType(reader)) { |
| 152 | case XML_READER_TYPE_ATTRIBUTE: |
| 153 | case XML_READER_TYPE_TEXT: |
| 154 | case XML_READER_TYPE_ENTITY_REFERENCE: |
| 155 | return depth - 1; |
| 156 | default: |
| 157 | return depth; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | char *oscap_get_xml(xmlTextReaderPtr reader) |
| 162 | { |
| 163 | return (char *)xmlTextReaderReadInnerXml(reader); |
| 164 | } |
| 165 | |
| 166 | time_t oscap_get_date(const char *date) |
| 167 | { |
| 168 | if (date) { |
| 169 | struct tm tm; |
| 170 | memset(&tm, 0, sizeof(tm)); |
| 171 | if (sscanf(date, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) { |
| 172 | tm.tm_mon -= 1; |
| 173 | tm.tm_year -= 1900; |
| 174 | return mktime(&tm); |
| 175 | } |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | time_t oscap_get_datetime(const char *date) |
| 181 | { |
| 182 | if (date) { |
| 183 | struct tm tm; |
| 184 | memset(&tm, 0, sizeof(tm)); |
| 185 | if (sscanf |
| 186 | (date, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, |
| 187 | &tm.tm_sec) == 6) { |
| 188 | tm.tm_mon -= 1; |
| 189 | tm.tm_year -= 1900; |
| 190 | return mktime(&tm); |
| 191 | } |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | xmlNode *oscap_xmlstr_to_dom(xmlNode *parent, const char *elname, const char *content) |
| 197 | { |
| 198 | char *str = oscap_sprintf("<x xmlns:xhtml='http://www.w3.org/1999/xhtml'>%s</x>", content); |
| 199 | xmlDoc *doc = xmlReadMemory(str, strlen(str), NULL, NULL, |
| 200 | XML_PARSE_RECOVER | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_NSCLEAN); |
| 201 | xmlNode *text_node = xmlCopyNode(xmlDocGetRootElement(doc), 1); |
| 202 | xmlNodeSetName(text_node, BAD_CAST elname); |
| 203 | xmlAddChild(parent, text_node); |
| 204 | xmlFreeDoc(doc); |
| 205 | free(str); |
| 206 | return text_node; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | const char *oscap_strlist_find_value(char ** const kvalues, const char *key) |
| 211 | { |
| 212 | if (kvalues == NULL || key == NULL) return NULL; |
| 213 | |
| 214 | for (int i = 0; kvalues[i] != NULL && kvalues[i + 1] != NULL; i += 2) |
| 215 | if (strcmp(kvalues[i], key) == 0) |
| 216 | return kvalues[i + 1]; |
| 217 | |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | int oscap_xml_save_filename(const char *filename, xmlDocPtr doc) |
| 222 | { |
| 223 | xmlOutputBufferPtr buff; |
| 224 | int xmlCode; |
| 225 | |
| 226 | if (strcmp(filename, "-") == 0) { |
| 227 | xmlCode = xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1); |
| 228 | } |
| 229 | else { |
| 230 | int fd = oscap_open_writable(filename); |
| 231 | if (fd == -1) |
| 232 | return -1; |
| 233 | |
| 234 | buff = xmlOutputBufferCreateFd(fd, NULL); |
| 235 | if (buff == NULL) { |
| 236 | close(fd); |
| 237 | oscap_setxmlerr(xmlGetLastError()); |
| 238 | dW("xmlOutputBufferCreateFile() failed."); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | xmlCode = xmlSaveFormatFileTo(buff, doc, "UTF-8", 1); |
| 243 | close(fd); |
| 244 | } |
| 245 | if (xmlCode <= 0) { |
| 246 | oscap_setxmlerr(xmlGetLastError()); |
| 247 | dW("No bytes exported: xmlCode: %d.", xmlCode); |
| 248 | } |
| 249 | |
| 250 | return (xmlCode >= 1) ? 1 : -1; |
| 251 | } |
| 252 | |
| 253 | int oscap_xml_save_fd(int fd, xmlDocPtr doc) |
| 254 | { |
| 255 | xmlOutputBufferPtr buff; |
| 256 | int xmlCode; |
| 257 | |
| 258 | buff = xmlOutputBufferCreateFd(fd, NULL); |
| 259 | if (buff == NULL) { |
| 260 | close(fd); |
| 261 | oscap_setxmlerr(xmlGetLastError()); |
| 262 | dW("xmlOutputBufferCreateFile() failed."); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | xmlCode = xmlSaveFormatFileTo(buff, doc, "UTF-8", 1); |
| 267 | if (xmlCode <= 0) { |
| 268 | oscap_setxmlerr(xmlGetLastError()); |
| 269 | dW("No bytes exported: xmlCode: %d.", xmlCode); |
| 270 | } |
| 271 | |
| 272 | return (xmlCode >= 1) ? 1 : -1; |
| 273 | } |
| 274 | |
| 275 | int oscap_xml_save_filename_free(const char *filename, xmlDocPtr doc) |
| 276 | { |
| 277 | int ret = oscap_xml_save_filename(filename, doc); |
| 278 | xmlFreeDoc(doc); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | xmlNs *lookup_xsi_ns(xmlDoc *doc) |
| 283 | { |
| 284 | // Look-up xsi namespace pointer. We can be pretty sure that this namespace |
| 285 | // is defined in root element, because it usually carries xsi:schemaLocation |
| 286 | // attribute. |
| 287 | xmlNsPtr ns_xsi = xmlSearchNsByHref(doc, xmlDocGetRootElement(doc), OSCAP_XMLNS_XSI); |
| 288 | if (ns_xsi == NULL) { |
| 289 | ns_xsi = xmlNewNs(xmlDocGetRootElement(doc), OSCAP_XMLNS_XSI, BAD_CAST "xsi"); |
| 290 | } |
| 291 | return ns_xsi; |
| 292 | } |