/src/mod_auth_openidc/src/util/file.c
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one |
3 | | * or more contributor license agreements. See the NOTICE file |
4 | | * distributed with this work for additional information |
5 | | * regarding copyright ownership. The ASF licenses this file |
6 | | * to you under the Apache License, Version 2.0 (the |
7 | | * "License"); you may not use this file except in compliance |
8 | | * with the License. You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | /*************************************************************************** |
21 | | * Copyright (C) 2017-2026 ZmartZone Holding BV |
22 | | * All rights reserved. |
23 | | * |
24 | | * DISCLAIMER OF WARRANTIES: |
25 | | * |
26 | | * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT |
27 | | * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING, |
28 | | * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT, |
29 | | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NOR ARE THERE ANY |
30 | | * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE |
31 | | * USAGE. FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET |
32 | | * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE |
33 | | * WILL BE UNINTERRUPTED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
34 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
35 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF |
36 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
37 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
38 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | * |
40 | | * @Author: Hans Zandbelt - hans.zandbelt@openidc.com |
41 | | */ |
42 | | |
43 | | #include "util/util.h" |
44 | | |
45 | | /* |
46 | | * true if path exists and is a regular file; with follow set, a symlink is resolved and it is |
47 | | * the target that must be regular - right for operator-managed files (metadata, templates, |
48 | | * keys), where symlinks are a fact of deployment life (Kubernetes ConfigMap volumes expose |
49 | | * every file as one) - while without it the path itself must be regular, for the module's own |
50 | | * private cache directory where a symlink can only have been planted. finfo->filetype is |
51 | | * always well-defined on return: APR_NOFILE when the path could not be stat-ed at all |
52 | | * (typically: missing, or a dangling symlink when following), the actual (non-regular) type |
53 | | * otherwise, letting callers tell "absent" apart from "present but wrong type" |
54 | | */ |
55 | 0 | apr_byte_t oidc_util_file_is_regular(apr_pool_t *pool, const char *path, apr_byte_t follow, apr_finfo_t *finfo) { |
56 | 0 | finfo->filetype = APR_NOFILE; |
57 | 0 | if (apr_stat(finfo, path, APR_FINFO_TYPE | (follow ? 0 : APR_FINFO_LINK), pool) != APR_SUCCESS) |
58 | 0 | return FALSE; |
59 | 0 | return (finfo->filetype == APR_REG) ? TRUE : FALSE; |
60 | 0 | } |
61 | | |
62 | | typedef enum { |
63 | | OIDC_UTIL_FILE_READ_OK = 0, |
64 | | OIDC_UTIL_FILE_READ_NOT_FOUND, /* missing, or could not be stat-ed/opened */ |
65 | | OIDC_UTIL_FILE_READ_NOT_REGULAR, /* exists but is a symlink or other special file */ |
66 | | OIDC_UTIL_FILE_READ_IO_ERROR, /* stat/open succeeded but a later step failed */ |
67 | | } oidc_util_file_read_rc_t; |
68 | | |
69 | | /* |
70 | | * hardened whole-file read shared by the request- and server-scoped wrappers below: rejects |
71 | | * symlinks/non-regular files and caps the size of what gets allocated into memory; the caller |
72 | | * is responsible for logging, since the request/server logging macros are not interchangeable |
73 | | */ |
74 | | static oidc_util_file_read_rc_t oidc_util_file_read_core(apr_pool_t *pool, const char *path, char **result, char *s_err, |
75 | 0 | apr_size_t s_err_len) { |
76 | 0 | apr_file_t *fd = NULL; |
77 | 0 | apr_status_t rc = APR_SUCCESS; |
78 | 0 | apr_finfo_t finfo; |
79 | | |
80 | | /* follow symlinks: what is read here is operator-managed (metadata, templates, keys) and |
81 | | * commonly symlinked into place; it is the target that must be a regular file */ |
82 | 0 | if (oidc_util_file_is_regular(pool, path, TRUE, &finfo) == FALSE) { |
83 | 0 | if (finfo.filetype == APR_NOFILE) { |
84 | 0 | apr_cpystrn(s_err, "no such file", s_err_len); |
85 | 0 | return OIDC_UTIL_FILE_READ_NOT_FOUND; |
86 | 0 | } |
87 | 0 | apr_cpystrn(s_err, "not a regular file", s_err_len); |
88 | 0 | return OIDC_UTIL_FILE_READ_NOT_REGULAR; |
89 | 0 | } |
90 | | |
91 | | /* open the file */ |
92 | 0 | if ((rc = apr_file_open(&fd, path, APR_FOPEN_READ | APR_FOPEN_BUFFERED, APR_OS_DEFAULT, pool)) != APR_SUCCESS) { |
93 | 0 | apr_strerror(rc, s_err, s_err_len); |
94 | 0 | return OIDC_UTIL_FILE_READ_NOT_FOUND; |
95 | 0 | } |
96 | | |
97 | | /* the file exists, now lock it */ |
98 | 0 | apr_file_lock(fd, APR_FLOCK_EXCLUSIVE); |
99 | | |
100 | | /* move the read pointer to the very start of the file */ |
101 | 0 | apr_off_t begin = 0; |
102 | 0 | apr_file_seek(fd, APR_SET, &begin); |
103 | | |
104 | | /* get the file info so we know its size */ |
105 | 0 | if ((rc = apr_file_info_get(&finfo, APR_FINFO_SIZE, fd)) != APR_SUCCESS) { |
106 | 0 | apr_strerror(rc, s_err, s_err_len); |
107 | 0 | goto error_close; |
108 | 0 | } |
109 | | |
110 | | /* refuse untrusted on-disk sizes before allocating memory for the contents */ |
111 | 0 | if ((finfo.size < 0) || (finfo.size > (apr_off_t)OIDC_UTIL_FILE_SIZE_MAX)) { |
112 | 0 | apr_snprintf(s_err, s_err_len, "file too large (%" APR_OFF_T_FMT " bytes)", finfo.size); |
113 | 0 | goto error_close; |
114 | 0 | } |
115 | | |
116 | | /* now that we have the size of the file, allocate a buffer that can contain its contents */ |
117 | 0 | *result = apr_palloc(pool, finfo.size + 1); |
118 | | |
119 | | /* read the file in to the buffer */ |
120 | 0 | apr_size_t bytes_read = 0; |
121 | 0 | if ((rc = apr_file_read_full(fd, *result, finfo.size, &bytes_read)) != APR_SUCCESS) { |
122 | 0 | apr_strerror(rc, s_err, s_err_len); |
123 | 0 | goto error_close; |
124 | 0 | } |
125 | | |
126 | | /* just to be sure, we set a \0 (we allocated space for it anyway) */ |
127 | 0 | (*result)[bytes_read] = '\0'; |
128 | | |
129 | | /* check that we've got all of it */ |
130 | 0 | if ((apr_off_t)bytes_read != finfo.size) { |
131 | 0 | apr_snprintf(s_err, s_err_len, "read %" APR_SIZE_T_FMT " bytes, expected %" APR_OFF_T_FMT, bytes_read, |
132 | 0 | finfo.size); |
133 | 0 | goto error_close; |
134 | 0 | } |
135 | | |
136 | | /* we're done, unlock and close the file */ |
137 | 0 | apr_file_unlock(fd); |
138 | 0 | apr_file_close(fd); |
139 | |
|
140 | 0 | return OIDC_UTIL_FILE_READ_OK; |
141 | | |
142 | 0 | error_close: |
143 | |
|
144 | 0 | apr_file_unlock(fd); |
145 | 0 | apr_file_close(fd); |
146 | |
|
147 | 0 | return OIDC_UTIL_FILE_READ_IO_ERROR; |
148 | 0 | } |
149 | | |
150 | | /* |
151 | | * read a file from a path on disk, for use at request time |
152 | | */ |
153 | 0 | apr_byte_t oidc_util_file_read(request_rec *r, const char *path, apr_pool_t *pool, char **result) { |
154 | 0 | char s_err[128]; |
155 | 0 | oidc_util_file_read_rc_t rc = oidc_util_file_read_core(pool, path, result, s_err, sizeof(s_err)); |
156 | |
|
157 | 0 | switch (rc) { |
158 | 0 | case OIDC_UTIL_FILE_READ_OK: |
159 | 0 | oidc_debug(r, "file read successfully \"%s\"", path); |
160 | 0 | return TRUE; |
161 | 0 | case OIDC_UTIL_FILE_READ_NOT_FOUND: |
162 | 0 | oidc_warn(r, "no file found at: \"%s\" (%s)", path, s_err); |
163 | 0 | return FALSE; |
164 | 0 | case OIDC_UTIL_FILE_READ_NOT_REGULAR: |
165 | 0 | oidc_warn(r, "refusing to read non-regular file: \"%s\"", path); |
166 | 0 | return FALSE; |
167 | 0 | default: |
168 | 0 | oidc_error(r, "reading \"%s\" failed: %s", path, s_err); |
169 | 0 | return FALSE; |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | /* |
174 | | * read a file from a path on disk, for use at server startup (e.g. config/license checks) |
175 | | * where there is no request_rec to log against or allocate from |
176 | | */ |
177 | 0 | apr_byte_t oidc_util_file_read_server(server_rec *s, const char *path, apr_pool_t *pool, char **result) { |
178 | 0 | char s_err[128]; |
179 | 0 | oidc_util_file_read_rc_t rc = oidc_util_file_read_core(pool, path, result, s_err, sizeof(s_err)); |
180 | |
|
181 | 0 | switch (rc) { |
182 | 0 | case OIDC_UTIL_FILE_READ_OK: |
183 | 0 | oidc_sdebug(s, "file read successfully \"%s\"", path); |
184 | 0 | return TRUE; |
185 | 0 | case OIDC_UTIL_FILE_READ_NOT_FOUND: |
186 | 0 | oidc_swarn(s, "no file found at: \"%s\" (%s)", path, s_err); |
187 | 0 | return FALSE; |
188 | 0 | case OIDC_UTIL_FILE_READ_NOT_REGULAR: |
189 | 0 | oidc_swarn(s, "refusing to read non-regular file: \"%s\"", path); |
190 | 0 | return FALSE; |
191 | 0 | default: |
192 | 0 | oidc_serror(s, "reading \"%s\" failed: %s", path, s_err); |
193 | 0 | return FALSE; |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | /* |
198 | | * write data to a file |
199 | | */ |
200 | 0 | apr_byte_t oidc_util_file_write(request_rec *r, const char *path, const char *data) { |
201 | |
|
202 | 0 | apr_file_t *fd = NULL; |
203 | 0 | apr_status_t rc = APR_SUCCESS; |
204 | 0 | apr_size_t bytes_written = 0; |
205 | 0 | char s_err[128]; |
206 | 0 | char *rnd = NULL; |
207 | 0 | const char *tmp_path = NULL; |
208 | |
|
209 | 0 | if (oidc_util_rand_str(r, &rnd, 12) == FALSE) |
210 | 0 | return FALSE; |
211 | 0 | tmp_path = apr_psprintf(r->pool, "%s.%s.tmp", path, rnd); |
212 | | |
213 | | /* try to open the metadata file for writing, creating it if it does not exist; some of |
214 | | * these files hold secrets (e.g. a dynamically registered client_secret), so restrict |
215 | | * the permissions to the owner only rather than falling back to the process umask */ |
216 | 0 | if ((rc = apr_file_open(&fd, tmp_path, (APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL), |
217 | 0 | (APR_FPROT_UREAD | APR_FPROT_UWRITE), r->pool)) != APR_SUCCESS) { |
218 | 0 | oidc_error(r, "file \"%s\" could not be opened for atomic update (%s)", tmp_path, |
219 | 0 | apr_strerror(rc, s_err, sizeof(s_err))); |
220 | 0 | return FALSE; |
221 | 0 | } |
222 | | |
223 | | /* lock the file and move the write pointer to the start of it */ |
224 | 0 | apr_file_lock(fd, APR_FLOCK_EXCLUSIVE); |
225 | 0 | apr_off_t begin = 0; |
226 | 0 | apr_file_seek(fd, APR_SET, &begin); |
227 | | |
228 | | /* calculate the length of the data, which is a string length */ |
229 | 0 | apr_size_t len = _oidc_strlen(data); |
230 | | |
231 | | /* (blocking) write the number of bytes in the buffer */ |
232 | 0 | rc = apr_file_write_full(fd, data, len, &bytes_written); |
233 | | |
234 | | /* check for a system error */ |
235 | 0 | if (rc != APR_SUCCESS) { |
236 | 0 | oidc_error(r, "could not write to: \"%s\" (%s)", path, apr_strerror(rc, s_err, sizeof(s_err))); |
237 | 0 | apr_file_unlock(fd); |
238 | 0 | apr_file_close(fd); |
239 | 0 | apr_file_remove(tmp_path, r->pool); |
240 | 0 | return FALSE; |
241 | 0 | } |
242 | | |
243 | | /* check that all bytes from the header were written */ |
244 | 0 | if (bytes_written != len) { |
245 | 0 | oidc_error(r, |
246 | 0 | "could not write enough bytes to: \"%s\", bytes_written (%" APR_SIZE_T_FMT |
247 | 0 | ") != len (%" APR_SIZE_T_FMT ")", |
248 | 0 | path, bytes_written, len); |
249 | 0 | apr_file_unlock(fd); |
250 | 0 | apr_file_close(fd); |
251 | 0 | apr_file_remove(tmp_path, r->pool); |
252 | 0 | return FALSE; |
253 | 0 | } |
254 | | |
255 | | /* unlock and close the written file */ |
256 | 0 | apr_file_unlock(fd); |
257 | 0 | apr_file_close(fd); |
258 | 0 | if ((rc = apr_file_rename(tmp_path, path, r->pool)) != APR_SUCCESS) { |
259 | 0 | oidc_error(r, "file \"%s\" could not be atomically renamed to \"%s\" (%s)", tmp_path, path, |
260 | 0 | apr_strerror(rc, s_err, sizeof(s_err))); |
261 | 0 | apr_file_remove(tmp_path, r->pool); |
262 | 0 | return FALSE; |
263 | 0 | } |
264 | | |
265 | 0 | oidc_debug(r, "file \"%s\" written; number of bytes (%" APR_SIZE_T_FMT ")", path, len); |
266 | |
|
267 | 0 | return TRUE; |
268 | 0 | } |