| 1 | /* |
| 2 | * Copyright 2009--2014 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 | * Lukas Kuklinek <[email protected]> |
| 21 | * Peter Vrabec <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <libxml/parser.h> |
| 29 | #include <libxml/xmlschemas.h> |
| 30 | #include <libxslt/xslt.h> |
| 31 | #include <libxslt/xsltInternals.h> |
| 32 | #include <libxslt/transform.h> |
| 33 | #include <libxslt/xsltutils.h> |
| 34 | #include <libexslt/exslt.h> |
| 35 | #include <string.h> |
| 36 | #ifndef OS_WINDOWS |
| 37 | #include <unistd.h> |
| 38 | #else |
| 39 | #include <io.h> |
| 40 | #include <windows.h> |
| 41 | #endif |
| 42 | #include <fcntl.h> |
| 43 | |
| 44 | #include "public/oscap.h" |
| 45 | #include "_error.h" |
| 46 | #include "util.h" |
| 47 | #include "list.h" |
| 48 | #include "elements.h" |
| 49 | #include "debug_priv.h" |
| 50 | #include "oscap_source.h" |
| 51 | #include "oscapxml.h" |
| 52 | #include "source/schematron_priv.h" |
| 53 | #include "source/validate_priv.h" |
| 54 | #include "source/xslt_priv.h" |
| 55 | #include "oscap_helpers.h" |
| 56 | |
| 57 | const char *const OSCAP_SCHEMA_PATH = OSCAP_DEFAULT_SCHEMA_PATH; |
| 58 | const char *const OSCAP_XSLT_PATH = OSCAP_DEFAULT_XSLT_PATH; |
| 59 | const char *const OSCAP_CPE_PATH = OSCAP_DEFAULT_CPE_PATH; |
| 60 | |
| 61 | #ifdef OS_WINDOWS |
| 62 | static const char *_get_default_path(const char *defpath) |
| 63 | { |
| 64 | /* On Windows, default paths are directory names of directories that are |
| 65 | * located in the same directory as oscap.exe. We will make a full path |
| 66 | * based on oscap.exe location. |
| 67 | */ |
| 68 | char oscap_path[PATH_MAX]; |
| 69 | GetModuleFileName(NULL, oscap_path, PATH_MAX); |
| 70 | char *oscap_path_dirname = oscap_dirname(oscap_path); |
| 71 | const char *path = oscap_sprintf("%s\\%s", oscap_path_dirname, defpath); |
| 72 | free(oscap_path_dirname); |
| 73 | return path; |
| 74 | } |
| 75 | #else |
| 76 | static const char *_get_default_path(const char *defpath) |
| 77 | { |
| 78 | return defpath; |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | /* |
| 83 | * If environment variable specified by 'pathvar' is defined, returns value of this environment variable. |
| 84 | * If this environment variable is not defined, returns default path, provided by 'defpath' argument. |
| 85 | */ |
| 86 | static const char * oscap_path_to(const char *pathvar, const char *defpath) { |
| 87 | const char *path = NULL; |
| 88 | |
| 89 | if (pathvar != NULL) |
| 90 | path = getenv(pathvar); |
| 91 | |
| 92 | if (path == NULL || oscap_streq(path, "")) { |
| 93 | path = _get_default_path(defpath); |
| 94 | } |
| 95 | |
| 96 | return path; |
| 97 | } |
| 98 | |
| 99 | const char * oscap_path_to_schemas() { |
| 100 | return oscap_path_to("OSCAP_SCHEMA_PATH", OSCAP_SCHEMA_PATH); |
| 101 | } |
| 102 | |
| 103 | const char *oscap_path_to_xslt(void) |
| 104 | { |
| 105 | return oscap_path_to("OSCAP_XSLT_PATH", OSCAP_XSLT_PATH); |
| 106 | } |
| 107 | |
| 108 | const char * oscap_path_to_cpe() { |
| 109 | return oscap_path_to("OSCAP_CPE_PATH", OSCAP_CPE_PATH); |
| 110 | } |
| 111 | |
| 112 | void oscap_init(void) |
| 113 | { |
| 114 | xmlInitParser(); |
| 115 | xsltInit(); |
| 116 | exsltRegisterAll(); |
| 117 | } |
| 118 | |
| 119 | void oscap_cleanup(void) |
| 120 | { |
| 121 | oscap_clearerr(); |
| 122 | xsltCleanupGlobals(); |
| 123 | xmlCleanupParser(); |
| 124 | } |
| 125 | |
| 126 | const char *oscap_get_version(void) { return OPENSCAP_VERSION; } |
| 127 | |
| 128 | int oscap_apply_xslt(const char *xmlfile, const char *xsltfile, const char *outfile, const char **params) |
| 129 | { |
| 130 | struct oscap_source *source = oscap_source_new_from_file(xmlfile); |
| 131 | int ret = oscap_source_apply_xslt_path(source, xsltfile, outfile, params, oscap_path_to_xslt()); |
| 132 | oscap_source_free(source); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | const char *oscap_document_type_to_string(oscap_document_type_t type) |
| 137 | { |
| 138 | switch (type) { |
| 139 | case OSCAP_DOCUMENT_OVAL_DEFINITIONS: |
| 140 | return "OVAL Definition"; |
| 141 | case OSCAP_DOCUMENT_OVAL_DIRECTIVES: |
| 142 | return "OVAL Directives"; |
| 143 | case OSCAP_DOCUMENT_OVAL_RESULTS: |
| 144 | return "OVAL Results"; |
| 145 | case OSCAP_DOCUMENT_OVAL_SYSCHAR: |
| 146 | return "OVAL System Characteristics"; |
| 147 | case OSCAP_DOCUMENT_OVAL_VARIABLES: |
| 148 | return "OVAL Variables"; |
| 149 | case OSCAP_DOCUMENT_SDS: |
| 150 | return "SCAP Source Datastream"; |
| 151 | case OSCAP_DOCUMENT_XCCDF: |
| 152 | return "XCCDF Checklist"; |
| 153 | case OSCAP_DOCUMENT_XCCDF_TAILORING: |
| 154 | return "XCCDF Tailoring"; |
| 155 | case OSCAP_DOCUMENT_SCE_RESULT: |
| 156 | return "SCE Results"; |
| 157 | case OSCAP_DOCUMENT_CPE_DICTIONARY: |
| 158 | return "CPE Dictionary"; |
| 159 | case OSCAP_DOCUMENT_CPE_LANGUAGE: |
| 160 | return "CPE Language"; |
| 161 | case OSCAP_DOCUMENT_ARF: |
| 162 | return "ARF Result Datastream"; |
| 163 | case OSCAP_DOCUMENT_OCIL: |
| 164 | return "OCIL Questionnaire"; |
| 165 | default: |
| 166 | return NULL; |
| 167 | } |
| 168 | } |