| 1 | /* |
| 2 | * Copyright 2012--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 | */ |
| 20 | |
| 21 | #ifndef OSCAP_ACQUIRE_H_ |
| 22 | #define OSCAP_ACQUIRE_H_ |
| 23 | |
| 24 | #include "util.h" |
| 25 | #include <stdbool.h> |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Create an oscap temp dir. (While ideally all the operations are being |
| 30 | * made on unliked files using file descriptors, this is bordeline impossible |
| 31 | * in short term given the library interfaces. |
| 32 | * This function emits a message on stderr in case of error. |
| 33 | * @return filename of the temporary directory or NULL on error. Returned value |
| 34 | * shall be freed by oscap_acquire_cleanup_dir() call. |
| 35 | */ |
| 36 | char *oscap_acquire_temp_dir(void); |
| 37 | |
| 38 | /** |
| 39 | * Unlink given directory recursivelly. |
| 40 | * @param dir_path path to the directory for removal. |
| 41 | */ |
| 42 | void oscap_acquire_cleanup_dir(char **dir_path); |
| 43 | |
| 44 | /** |
| 45 | * Create new temp file within given directory. With filename |
| 46 | * corresponding with the template. |
| 47 | * @param dir directory to create file in |
| 48 | * @param template scheme for newly created file name |
| 49 | * @param filename name of newly created temp_file. The caller |
| 50 | * is responsible for disposing it. |
| 51 | * @returns file descriptors or a negative number to indicate failure. |
| 52 | */ |
| 53 | int oscap_acquire_temp_file(const char *dir, const char *template, char **filename); |
| 54 | |
| 55 | /** |
| 56 | * Is the given url supported by OpenSCAP? |
| 57 | * @param url Requested url |
| 58 | * @return true if the given string reminds supported url. |
| 59 | */ |
| 60 | bool oscap_acquire_url_is_supported(const char *url); |
| 61 | |
| 62 | /** |
| 63 | * Escape characters in the url so it can be used as POSIX filename. |
| 64 | * @param url Requested url |
| 65 | * @return escaped url or NULL |
| 66 | */ |
| 67 | char *oscap_acquire_url_to_filename(const char *url); |
| 68 | |
| 69 | /** |
| 70 | * Download the given url to memory |
| 71 | * @param url The url to acquire |
| 72 | * @param memory_size Size of memory. If NULL is returned, variable is not modified. |
| 73 | * @return the pointer to memory with downloaded data or NULL on error |
| 74 | */ |
| 75 | char * |
| 76 | oscap_acquire_url_download(const char *url, size_t* memory_size); |
| 77 | |
| 78 | /** |
| 79 | * Guess how the realpath of given file may look like. Do your best! |
| 80 | * Unlike realpath() this works for non-existent files. |
| 81 | * @param filepath |
| 82 | * @returns the normalized filepath |
| 83 | */ |
| 84 | char *oscap_acquire_guess_realpath(const char *filepath); |
| 85 | |
| 86 | /** |
| 87 | * Creates the directory, if it does not already exist. Further it makes parent |
| 88 | * directories as needed. |
| 89 | * @param path filepath to the target directory |
| 90 | * @returns the zero on success |
| 91 | */ |
| 92 | int oscap_acquire_mkdir_p(const char* path); |
| 93 | |
| 94 | /** |
| 95 | * Ensures the parent directory of the filepath, if it does not already exists. |
| 96 | * Further it makes parent directories as needed. |
| 97 | * @param filepath filepath to the descendant file or dir |
| 98 | * @returns the zero on success |
| 99 | */ |
| 100 | int oscap_acquire_ensure_parent_dir(const char *filepath); |
| 101 | |
| 102 | /** |
| 103 | * CURL WRITEFUNCTION callback |
| 104 | * @param ptr The pointer to received data |
| 105 | * @param size_t |
| 106 | * @param nmemb |
| 107 | * @param userdata Pointer to storage of received data |
| 108 | * @return Number of stored bytes |
| 109 | */ |
| 110 | size_t |
| 111 | write_to_memory_callback(char *ptr, size_t size, size_t nmemb, void *userdata); |
| 112 | |
| 113 | // FIXME: SCE engine uses this particular function |
| 114 | |
| 115 | /** |
| 116 | * Read characters from pipe, escape '&' and store result |
| 117 | * in the C string buffer. |
| 118 | * @param fd File descriptor to read. It will be closed afterwards |
| 119 | * @returns buffer allocated on heap. |
| 120 | */ |
| 121 | char *oscap_acquire_pipe_to_string(int fd); |
| 122 | |
| 123 | |
| 124 | |
| 125 | #endif |