| 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Martin Preisler <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "ds_common.h" |
| 28 | #include "common/_error.h" |
| 29 | #include "common/list.h" |
| 30 | #include "common/oscap_acquire.h" |
| 31 | #include "source/public/oscap_source.h" |
| 32 | #include "oscap_helpers.h" |
| 33 | |
| 34 | #include <libxml/tree.h> |
| 35 | |
| 36 | xmlDoc *ds_doc_from_foreign_node(xmlNode *node, xmlDoc *parent) |
| 37 | { |
| 38 | xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt(); |
| 39 | xmlDocPtr new_doc = xmlNewDoc(BAD_CAST "1.0"); |
| 40 | xmlNodePtr res_node = NULL; |
| 41 | if (xmlDOMWrapCloneNode(wrap_ctxt, parent, node, &res_node, new_doc, NULL, 1, 0) != 0) |
| 42 | { |
| 43 | oscap_seterr(OSCAP_EFAMILY_XML, "Error when cloning node '%s' while dumping component " |
| 44 | "from DataStream", node->name); |
| 45 | xmlFreeDoc(new_doc); |
| 46 | xmlDOMWrapFreeCtxt(wrap_ctxt); |
| 47 | return NULL; |
| 48 | } |
| 49 | xmlDocSetRootElement(new_doc, res_node); |
| 50 | if (xmlDOMWrapReconcileNamespaces(wrap_ctxt, res_node, 0) != 0) |
| 51 | { |
| 52 | oscap_seterr(OSCAP_EFAMILY_XML, "Internal libxml error when reconciling namespaces " |
| 53 | "for node '%s' while dumping component.", node->name); |
| 54 | xmlFreeDoc(new_doc); |
| 55 | xmlDOMWrapFreeCtxt(wrap_ctxt); |
| 56 | return NULL; |
| 57 | } |
| 58 | xmlDOMWrapFreeCtxt(wrap_ctxt); |
| 59 | return new_doc; |
| 60 | } |
| 61 | |
| 62 | int ds_dump_component_sources(struct oscap_htable *component_sources, const char *target_dir) |
| 63 | { |
| 64 | struct oscap_htable_iterator *hit = oscap_htable_iterator_new(component_sources); |
| 65 | while (oscap_htable_iterator_has_more(hit)) { |
| 66 | struct oscap_source *s = oscap_htable_iterator_next_value(hit); |
| 67 | char *filename = target_dir == NULL ? oscap_strdup(oscap_source_readable_origin(s)) |
| 68 | : oscap_sprintf("%s/%s", target_dir, oscap_source_readable_origin(s)); |
| 69 | int ret = oscap_acquire_ensure_parent_dir(filename); |
| 70 | if (ret != 0) { |
| 71 | free(filename); |
| 72 | oscap_htable_iterator_free(hit); |
| 73 | return ret; |
| 74 | } |
| 75 | ret = oscap_source_save_as(s, filename); |
| 76 | free(filename); |
| 77 | if (ret != 0) { |
| 78 | oscap_htable_iterator_free(hit); |
| 79 | return ret; |
| 80 | } |
| 81 | } |
| 82 | oscap_htable_iterator_free(hit); |
| 83 | return 0; |
| 84 | } |