1 /* 2 Copyright (C) 2015-2021, Dirk Krause 3 SPDX-License-Identifier: BSD-3-Clause 4 */ 5 6 /* 7 WARNING: This file was generated by the dkct program (see 8 http://dktools.sourceforge.net/ for details). 9 Changes you make here will be lost if dkct is run again! 10 You should modify the original source and run dkct on it. 11 Original source: dk4fopc8.ctr 12 */ 13 14 #ifndef DK4FOPC8_H_INCLUDED 15 /** Avoid multiple inclusions. */ 16 #define DK4FOPC8_H_INCLUDED 1 17 18 19 /** @file 20 Open file, apply additional security 21 checks before attempting to open the file. 22 23 Attempts to open a directory as a file are rejected. 24 25 Further optional checks can be applied to avoid: 26 - writing to non-regular files, 27 - writing to files with any symbolic link component in the path, 28 - writing to files via symbolic link, 29 - writing to files via symbolic link if link owner differs 30 from file owner. 31 */ 32 33 #ifndef DK4CONF_H_INCLUDED 34 #if DK4_BUILDING_DKTOOLS4 35 #include "dk4conf.h" 36 #else 37 #include <dktools-4/dk4conf.h> 38 #endif 39 #endif 40 41 #ifndef DK4TYPES_H_INCLUDED 42 #if DK4_BUILDING_DKTOOLS4 43 #include <libdk4base/dk4types.h> 44 #else 45 #include <dktools-4/dk4types.h> 46 #endif 47 #endif 48 49 #ifndef DK4ERROR_H_INCLUDED 50 #if DK4_BUILDING_DKTOOLS4 51 #include <libdk4base/dk4error.h> 52 #else 53 #include <dktools-4/dk4error.h> 54 #endif 55 #endif 56 57 #ifndef DK4NUMCO_H_INCLUDED 58 #if DK4_BUILDING_DKTOOLS4 59 #include <libdk4base/dk4numco.h> 60 #else 61 #include <dktools-4/dk4numco.h> 62 #endif 63 #endif 64 65 #ifndef DK4FOPT_H_INCLUDED 66 #if DK4_BUILDING_DKTOOLS4 67 #include <libdk4c/dk4fopt.h> 68 #else 69 #include <dktools-4/dk4fopt.h> 70 #endif 71 #endif 72 73 #ifndef STDIO_H_INCLUDED 74 #include <stdio.h> 75 #define STDIO_H_INCLUDED 1 76 #endif 77 78 #ifdef __cplusplus 79 extern "C" { 80 #endif 81 82 /** Open a file after doing security checks. 83 CRT on Windows: Required. 84 @param name File name to open. 85 @param mode Opening mode. 86 @param tests Set of tests. 87 @param erp Error report, may be NULL. 88 @return Pointer to open file on success, NULL on error. 89 90 Error codes: 91 - DK4_E_INVALID_ARGUMENTS<br> 92 if name is NULL, 93 - DK4_E_SYNTAX<br> 94 if name does not refer to a regular file, 95 - DK4_E_SEC_CHECK<br> 96 with failed echek id (DK4_FOPEN_SC_WR_PATH_IS_SYMLINK or 97 DK4_FOPEN_SC_WR_SYMLINK_OWNER) in iDetails1 if access is denied by 98 additional security checks, 99 - DK4_E_OPEN_WRITE_FAILED<br> 100 or DK4_E_OPEN_READ_FAILED with errno value in iDetails1 if fopen() 101 failed. 102 103 */ 104 FILE * 105 dk4fopen_c8(const char *name, const char *mode, int tests, dk4_er_t *erp); 106 107 /** Check whether we can allow to open the file. 108 The file must be a regular file. 109 Only POSIX: For root we deny opening for writing via symlink. 110 For all users we deny opening for writing via symlink 111 if the link owner is not the real file owner. 112 113 CRT on Windows: Required. 114 @param name File name to check. 115 @param ww Flag: Write access wanted. 116 @param tests Set of tests. 117 @param erp Error report, may be NULL. 118 @return 1 if opening the file is allowed, 0 otherwise. 119 120 Error codes: 121 - DK4_E_INVALID_ARGUMENTS<br> 122 if name is NULL, 123 - DK4_E_SYNTAX<br> 124 if name is not a regular file, 125 - DK4_E_SEC_CHECK<br> 126 if access to a symlink target is denied symlink owner and target 127 owner differ. 128 129 */ 130 int 131 dk4fopen_check_c8(const char *name, int ww, int tests, dk4_er_t *erp); 132 133 #ifdef __cplusplus 134 } 135 #endif 136 137 138 139 #endif 140