| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2018 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _UTIL_SELINUX_H_ |
| 22 | #define _UTIL_SELINUX_H_ |
| 23 | |
| 24 | #include "common/errno_t.h" |
| 25 | |
| 26 | /** |
| 27 | * Create temporary file created on @filepath.XXXXXX with security context |
| 28 | * set to default security context of @filepath. |
| 29 | * |
| 30 | * @param filepath File for which a temporary file should be created. |
| 31 | * @param mode Temporary file mode. |
| 32 | * @param _tmpfile Created temporary file. |
| 33 | * |
| 34 | * @return EOK on success, other errno code on failure. |
| 35 | */ |
| 36 | errno_t |
| 37 | selinux_mkstemp_for(const char *filepath, |
| 38 | mode_t mode, |
| 39 | char **_tmpfile); |
| 40 | |
| 41 | /** |
| 42 | * Copy file to destination. Directory is created if it does not exist. |
| 43 | * The original owner, permissions and selinux context of the source |
| 44 | * file are kept. |
| 45 | * |
| 46 | * @param source Source file name. |
| 47 | * @param destdir Destination directory. |
| 48 | * @param destname Destination file name. |
| 49 | * @param dir_mode Access mode of destination directory if it is created. |
| 50 | * |
| 51 | * @return EOK on success, other errno code on error. |
| 52 | */ |
| 53 | errno_t |
| 54 | selinux_file_copy(const char *source, |
| 55 | const char *destdir, |
| 56 | const char *destname, |
| 57 | mode_t dir_mode); |
| 58 | |
| 59 | /** |
| 60 | * Make copy of a file @source and store it in temporary file |
| 61 | * @destdir/@destname.XXXXXX, owner and permissions. |
| 62 | * |
| 63 | * @param source Source file name. |
| 64 | * @param destdir Destination directory. |
| 65 | * @param destname Destination file name. |
| 66 | * @param dir_mode Access mode of destination directory if it is created. |
| 67 | * @param keep_secontext If true, selinux context of the source file is kept. |
| 68 | * @param _tmpfile Path to created temporary file. |
| 69 | * |
| 70 | * @return EOK on success, other errno code on error. |
| 71 | */ |
| 72 | errno_t |
| 73 | selinux_mkstemp_copy(const char *source, |
| 74 | const char *destdir, |
| 75 | const char *destname, |
| 76 | mode_t dir_mode, |
| 77 | bool keep_secontext, |
| 78 | char **_tmpfile); |
| 79 | |
| 80 | struct selinux_safe_copy { |
| 81 | /* Source file name. */ |
| 82 | const char *source; |
| 83 | |
| 84 | /* Destination file name. */ |
| 85 | const char *destination; |
| 86 | |
| 87 | /* Unlink destination if source file does not exist. */ |
| 88 | bool can_unlink; |
| 89 | |
| 90 | /* Write an empty file if the source is missing. */ |
| 91 | bool write_empty_if_missing; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Copy multiple files to their new destination, keeping their ownership, |
| 96 | * permissions. It will first copy the files into temporary files in their |
| 97 | * new destination to ensure that the destination is writable and there is |
| 98 | * enough space to hold the file. Then it will rename it to their desired name, |
| 99 | * overwriting existing file. |
| 100 | * |
| 101 | * @param table File definitions. |
| 102 | * @param dir_mode Access mode of destination directory if it is created. |
| 103 | * @param keep_secontext If true, selinux context of the source file is kept. |
| 104 | * |
| 105 | * @return EOK on success, other errno code on error. |
| 106 | */ |
| 107 | errno_t |
| 108 | selinux_copy_files_safely(struct selinux_safe_copy *table, |
| 109 | mode_t dir_mode, |
| 110 | bool keep_secontext); |
| 111 | |
| 112 | #endif /* _UTIL_SELINUX_H_ */ |