| 1 | /* |
| 2 | * Copyright 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 | * Author: |
| 20 | * Šimon Lukašík |
| 21 | */ |
| 22 | #ifndef OSCAP_SOURCE_H |
| 23 | #define OSCAP_SOURCE_H |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include "oscap.h" |
| 32 | #include "oscap_export.h" |
| 33 | |
| 34 | /** |
| 35 | * The oscap_source is low-level structure. The oscap_source shall hold |
| 36 | * information regarding SCAP file/node needed for further processing by |
| 37 | * OpenSCAP library. |
| 38 | * |
| 39 | * The oscap_source may refer to a DataStream, a component of datastream, |
| 40 | * an XCCDF or OVAL file, or any other SCAP file. In rare cases it can |
| 41 | * also refer to a non-XML file, even binary data! |
| 42 | * |
| 43 | * The oscap_source is primarily focused to solve problems regarding parsing |
| 44 | * and serialization of SCAP files. The structure shall not know much about |
| 45 | * relation to other sources. |
| 46 | * |
| 47 | * For handling relations between oscap_sources users are adviced to use |
| 48 | * ds_sds_session. For example, if the oscap_source originated from DataStream |
| 49 | * file there may be other oscap_sources that represent the components of |
| 50 | * DataStream. These oscap_sources would be managed by ds_sds_session. |
| 51 | * |
| 52 | * The oscap_source keeps track of its origin (i.e. XCCDF file may originate |
| 53 | * by openning an XCCDF file, extracting XCCDF section from DataStream or |
| 54 | * by evaluation. |
| 55 | * |
| 56 | * The oscap_source is lazy structure, it only does its job when it has to. |
| 57 | * The structure caches data whenever it is feasible. |
| 58 | */ |
| 59 | struct oscap_source; |
| 60 | |
| 61 | /** |
| 62 | * Create new oscap_source from filename. In the future this should be |
| 63 | * the only function in the OpenSCAP library which will take in a path |
| 64 | * to filename. |
| 65 | * @param filepath Path to the file on a disk |
| 66 | * @returns newly created oscap_source structure |
| 67 | */ |
| 68 | OSCAP_API struct oscap_source *oscap_source_new_from_file(const char *filepath); |
| 69 | |
| 70 | /** |
| 71 | * Create new oscap_source from raw memory. The memory can contain \0 bytes |
| 72 | * and they are not considered NULL-terminations! Always pass the correct |
| 73 | * size. This constructor is meant as a last resort when no other constructor |
| 74 | * will work for your use case. If at all possible you should use the more |
| 75 | * specialized constructors. |
| 76 | * oscap_source will always allocate its own memory buffer and copy the data. |
| 77 | * You are responsible for freeing the original passed buffer. |
| 78 | * @param buffer Memory buffer with raw data |
| 79 | * @param size size of the memory buffer |
| 80 | * @param filepath Suggested filename for the file or NULL |
| 81 | * @returns newly created oscap_source_structure |
| 82 | */ |
| 83 | OSCAP_API struct oscap_source *oscap_source_new_from_memory(const char *buffer, size_t size, const char *filepath); |
| 84 | |
| 85 | /** |
| 86 | * Clone oscap_source structure. |
| 87 | * @param old Resource to clone |
| 88 | * @returns A clone of the given oscap_source. |
| 89 | */ |
| 90 | OSCAP_API struct oscap_source *oscap_source_clone(struct oscap_source *old); |
| 91 | |
| 92 | /** |
| 93 | * Dispose oscap_source structure. |
| 94 | * @param source Resource to dispose |
| 95 | */ |
| 96 | OSCAP_API void oscap_source_free(struct oscap_source *source); |
| 97 | |
| 98 | /** |
| 99 | * Dispose oscap_source xmlDoc structure. |
| 100 | * @param source Resource to dispose xmlDoc structure from |
| 101 | */ |
| 102 | OSCAP_API void oscap_source_free_xmlDoc(struct oscap_source *source); |
| 103 | |
| 104 | /** |
| 105 | * Get filepath of the given resource |
| 106 | * @memberof oscap_source |
| 107 | * @param source |
| 108 | * @returns filepath of the original source or NULL |
| 109 | */ |
| 110 | OSCAP_API const char *oscap_source_get_filepath(struct oscap_source *source); |
| 111 | |
| 112 | /** |
| 113 | * Get SCAP document type of the given resource |
| 114 | * @memberof oscap_source |
| 115 | * @param source |
| 116 | * @returns determined document type |
| 117 | * |
| 118 | * @note |
| 119 | * This function returns OSCAP_DOCUMENT_UNKNOWN to signal an error. Not being |
| 120 | * able to determine a valid documnent type is treated as an error. |
| 121 | */ |
| 122 | OSCAP_API oscap_document_type_t oscap_source_get_scap_type(struct oscap_source *source); |
| 123 | |
| 124 | /** |
| 125 | * Get the version of the schema for the particular document type |
| 126 | * @memberof oscap_source |
| 127 | * @param source The oscap_source to get the schema version from. |
| 128 | * @returns the schema version |
| 129 | */ |
| 130 | OSCAP_API const char *oscap_source_get_schema_version(struct oscap_source *source); |
| 131 | |
| 132 | /** |
| 133 | * Validate the SCAP document against particular XML schema definition. |
| 134 | * @memberof oscap_source |
| 135 | * @param source The oscap_source to validate |
| 136 | * @note The held resource has to be XML for this function to work. |
| 137 | * @returns 0 on pass; 1 on fail, and -1 on internal error |
| 138 | */ |
| 139 | OSCAP_API int oscap_source_validate(struct oscap_source *source, xml_reporter reporter, void *user); |
| 140 | |
| 141 | /** |
| 142 | * Validate the SCAP document against schematron assertions |
| 143 | * @memberof oscap_source |
| 144 | * @param source The oscap_source to validate |
| 145 | * @note The held resource has to be XML for this function to work. |
| 146 | * @returns 0 on pass; 1 on fail, and -1 on internal error |
| 147 | */ |
| 148 | OSCAP_API int oscap_source_validate_schematron(struct oscap_source *source); |
| 149 | |
| 150 | /** |
| 151 | * Returns human readable description of oscap_source origin |
| 152 | * @memberof oscap_source |
| 153 | * @param source The oscap_source to get readable source from. |
| 154 | * @returns human readable description |
| 155 | */ |
| 156 | OSCAP_API const char *oscap_source_readable_origin(const struct oscap_source *source); |
| 157 | |
| 158 | /** |
| 159 | * Store the resource represented by oscap_source to the file. |
| 160 | * @memberof oscap_source |
| 161 | * @param source The oscap_source to save |
| 162 | * @param filename The filename or NULL, the previously supplied name will |
| 163 | * be used if filename is NULL. |
| 164 | * @returns 0 on success, 1 or -1 to indicate error |
| 165 | */ |
| 166 | OSCAP_API int oscap_source_save_as(struct oscap_source *source, const char *filename); |
| 167 | |
| 168 | /** |
| 169 | * Store the resource represented by oscap_source to the file descriptor. |
| 170 | * @memberof oscap_source |
| 171 | * @param source The oscap_source to save |
| 172 | * @param fd file descriptor |
| 173 | * @returns 0 on success, 1 or -1 to indicate error |
| 174 | */ |
| 175 | OSCAP_API int oscap_source_to_fd(struct oscap_source *source, int fd); |
| 176 | |
| 177 | /** |
| 178 | * Retrieve contents refered to by oscap_source as raw memory. |
| 179 | * The memory is always copied. If the origin of oscap_source is raw memory, |
| 180 | * this function will simply duplicate it and the operation is relatively cheap. |
| 181 | * If however the origin is xmlDoc or an XML file this function has to serialize |
| 182 | * it and then copy the results to given buffer. Keep in mind that this may be |
| 183 | * performance intensive. |
| 184 | * You are responsible for freeing the buffer. |
| 185 | * @param buffer Will be filled with a pointer to a newly allocated buffer |
| 186 | * @param size Will be filled with size of the buffer |
| 187 | * @returns 0 on success, 1 otherwise |
| 188 | */ |
| 189 | OSCAP_API int oscap_source_get_raw_memory(struct oscap_source *source, char **buffer, size_t *size); |
| 190 | |
| 191 | #endif |