1 /****************************************************************************
2 * Copyright 2019-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
34 #include <curses.priv.h>
38 #ifndef USE_ROOT_ACCESS
40 #include <sys/fsuid.h>
46 #if HAVE_GETAUXVAL && HAVE_SYS_AUXV_H && defined(__GLIBC__) && (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 19)
48 #define USE_GETAUXVAL 1
50 #define USE_GETAUXVAL 0
55 MODULE_ID("$Id: access.c,v 1.37 2023/06/24 21:55:09 tom Exp $")
57 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
60 # define ACCESS(FN, MODE) access((FN), (MODE)&(R_OK|W_OK))
62 # define ACCESS access
65 NCURSES_EXPORT(char *)
66 _nc_rootname(char *path)
68 char *result = _nc_basename(path);
69 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
73 if ((temp = strdup(result)) != 0)
75 #if !MIXEDCASE_FILENAMES
76 for (s = result; *s != '\0'; ++s) {
77 *s = (char) LOWERCASE(*s);
81 if ((s = strrchr(result, '.')) != 0) {
82 if (!strcmp(s, PROG_EXT))
91 * Check if a string appears to be an absolute pathname.
94 _nc_is_abs_path(const char *path)
96 #if defined(__EMX__) || defined(__DJGPP__)
97 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
98 || (((s)[0] != 0) && ((s)[1] == ':')))
100 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
102 return is_pathname(path);
106 * Return index of the basename
108 NCURSES_EXPORT(unsigned)
109 _nc_pathlast(const char *path)
111 const char *test = strrchr(path, '/');
114 test = strrchr(path, '\\');
120 return (unsigned) (test - path);
123 NCURSES_EXPORT(char *)
124 _nc_basename(char *path)
126 return path + _nc_pathlast(path);
130 _nc_access(const char *path, int mode)
136 } else if (ACCESS(path, mode) < 0) {
137 if ((mode & W_OK) != 0
139 && strlen(path) < PATH_MAX) {
143 _nc_STRCPY(head, path, sizeof(head));
144 leaf = _nc_basename(head);
149 _nc_STRCPY(head, ".", sizeof(head));
151 result = ACCESS(head, R_OK | W_OK | X_OK);
162 _nc_is_dir_path(const char *path)
167 if (stat(path, &sb) == 0
168 && S_ISDIR(sb.st_mode)) {
175 _nc_is_file_path(const char *path)
180 if (stat(path, &sb) == 0
181 && S_ISREG(sb.st_mode)) {
187 #if HAVE_GETEUID && HAVE_GETEGID
188 #define is_posix_elevated() \
189 (getuid() != geteuid() \
190 || getgid() != getegid())
192 #define is_posix_elevated() FALSE
196 #define is_elevated() issetugid()
197 #elif USE_GETAUXVAL && defined(AT_SECURE)
198 #define is_elevated() \
199 (getauxval(AT_SECURE) \
203 : is_posix_elevated()))
205 #define is_elevated() is_posix_elevated()
209 #define lower_privileges() \
210 int save_err = errno; \
211 setfsuid(getuid()); \
212 setfsgid(getgid()); \
214 #define resume_elevation() \
216 setfsuid(geteuid()); \
217 setfsgid(getegid()); \
220 #define lower_privileges() /* nothing */
221 #define resume_elevation() /* nothing */
225 * Returns true if not running as root or setuid. We use this check to allow
226 * applications to use environment variables that are used for searching lists
227 * of directories, etc.
234 #if HAVE_GETUID && HAVE_GETEUID
235 #if !defined(USE_SETUID_ENVIRON)
240 #if !defined(USE_ROOT_ENVIRON)
241 if ((getuid() == ROOT_UID) || (geteuid() == ROOT_UID)) {
245 #endif /* HAVE_GETUID && HAVE_GETEUID */
249 #ifndef USE_ROOT_ACCESS
251 * Limit privileges if possible; otherwise disallow access for updating files.
253 NCURSES_EXPORT(FILE *)
254 _nc_safe_fopen(const char *path, const char *mode)
259 result = fopen(path, mode);
262 if (!is_elevated() || *mode == 'r') {
263 result = fopen(path, mode);
270 _nc_safe_open3(const char *path, int flags, mode_t mode)
275 result = open(path, flags, mode);
278 if (!is_elevated() || (flags & O_RDONLY)) {
279 result = open(path, flags, mode);
284 #endif /* USE_ROOT_ACCESS */