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: dk4fopwc.ctr
12 */
13 
14 #ifndef DK4FOPWC_H_INCLUDED
15 /** Avoid multiple inclusions. */
16 #define DK4FOPWC_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_NOT_SUPPORTED<br>
96 	  if no functions for wchar_t file names are available,
97 	- DK4_E_SEC_CHECK<br>
98 	  with failed check id in iDetails1 if access is denied by additional
99 	  security checks,
100 	- DK4_E_OPEN_WRITE_FAILED<br>
101 	  or DK4_E_OPEN_READ_FAILED with errno value in iDetails1 if fopen()
102 	  failed.
103 
104 */
105 FILE *
106 dk4fopen_wc(const wchar_t *name, const wchar_t *mode, int tests, dk4_er_t *erp);
107 
108 /**	Check whether we can allow to open the file.
109 	The file must be a regular file.
110 	Only POSIX: For root we deny opening for writing via symlink.
111 	For all users we deny opening for writing via symlink
112 	if the link owner is not the real file owner.
113 
114 	CRT on Windows: Required.
115 	@param	name	File name to check.
116 	@param	ww	Flag: Write access wanted.
117 	@param	tests	Set of tests.
118 	@param	erp	Error report, may be NULL.
119 	@return	1 if opening the file is allowed, 0 otherwise.
120 
121 	Error codes:
122 	- DK4_E_INVALID_ARGUMENTS<br>
123 	  if name is NULL,
124 	- DK4_E_SYNTAX<br>
125 	  if name is not a regular file,
126 	- DK4_E_SEC_CHECK<br>
127 	  if access to a symlink target is denied symlink owner and target
128 	  owner differ.
129 
130 */
131 int
132 dk4fopen_check_wc(const wchar_t *name, int ww, int tests, dk4_er_t *erp);
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 
139 
140 
141 #endif
142