]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/access.c
ncurses 6.3
[ncurses.git] / ncurses / tinfo / access.c
1 /****************************************************************************
2  * Copyright 2019-2020,2021 Thomas E. Dickey                                *
3  * Copyright 1998-2011,2012 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                                                *
32  ****************************************************************************/
33
34 #include <curses.priv.h>
35
36 #include <ctype.h>
37
38 #ifndef USE_ROOT_ACCESS
39 #if HAVE_SETFSUID
40 #include <sys/fsuid.h>
41 #else
42 #include <sys/stat.h>
43 #endif
44 #endif
45
46 #include <tic.h>
47
48 MODULE_ID("$Id: access.c,v 1.31 2021/08/29 10:35:17 tom Exp $")
49
50 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
51
52 #ifdef _NC_MSC
53 # define ACCESS(FN, MODE) access((FN), (MODE)&(R_OK|W_OK))
54 #else
55 # define ACCESS access
56 #endif
57
58 NCURSES_EXPORT(char *)
59 _nc_rootname(char *path)
60 {
61     char *result = _nc_basename(path);
62 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
63     static char *temp;
64     char *s;
65
66     temp = strdup(result);
67     result = temp;
68 #if !MIXEDCASE_FILENAMES
69     for (s = result; *s != '\0'; ++s) {
70         *s = (char) LOWERCASE(*s);
71     }
72 #endif
73 #if defined(PROG_EXT)
74     if ((s = strrchr(result, '.')) != 0) {
75         if (!strcmp(s, PROG_EXT))
76             *s = '\0';
77     }
78 #endif
79 #endif
80     return result;
81 }
82
83 /*
84  * Check if a string appears to be an absolute pathname.
85  */
86 NCURSES_EXPORT(bool)
87 _nc_is_abs_path(const char *path)
88 {
89 #if defined(__EMX__) || defined(__DJGPP__)
90 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
91                   || (((s)[0] != 0) && ((s)[1] == ':')))
92 #else
93 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
94 #endif
95     return is_pathname(path);
96 }
97
98 /*
99  * Return index of the basename
100  */
101 NCURSES_EXPORT(unsigned)
102 _nc_pathlast(const char *path)
103 {
104     const char *test = strrchr(path, '/');
105 #ifdef __EMX__
106     if (test == 0)
107         test = strrchr(path, '\\');
108 #endif
109     if (test == 0)
110         test = path;
111     else
112         test++;
113     return (unsigned) (test - path);
114 }
115
116 NCURSES_EXPORT(char *)
117 _nc_basename(char *path)
118 {
119     return path + _nc_pathlast(path);
120 }
121
122 NCURSES_EXPORT(int)
123 _nc_access(const char *path, int mode)
124 {
125     int result;
126
127     if (path == 0) {
128         result = -1;
129     } else if (ACCESS(path, mode) < 0) {
130         if ((mode & W_OK) != 0
131             && errno == ENOENT
132             && strlen(path) < PATH_MAX) {
133             char head[PATH_MAX];
134             char *leaf;
135
136             _nc_STRCPY(head, path, sizeof(head));
137             leaf = _nc_basename(head);
138             if (leaf == 0)
139                 leaf = head;
140             *leaf = '\0';
141             if (head == leaf)
142                 _nc_STRCPY(head, ".", sizeof(head));
143
144             result = ACCESS(head, R_OK | W_OK | X_OK);
145         } else {
146             result = -1;
147         }
148     } else {
149         result = 0;
150     }
151     return result;
152 }
153
154 NCURSES_EXPORT(bool)
155 _nc_is_dir_path(const char *path)
156 {
157     bool result = FALSE;
158     struct stat sb;
159
160     if (stat(path, &sb) == 0
161         && S_ISDIR(sb.st_mode)) {
162         result = TRUE;
163     }
164     return result;
165 }
166
167 NCURSES_EXPORT(bool)
168 _nc_is_file_path(const char *path)
169 {
170     bool result = FALSE;
171     struct stat sb;
172
173     if (stat(path, &sb) == 0
174         && S_ISREG(sb.st_mode)) {
175         result = TRUE;
176     }
177     return result;
178 }
179
180 #if HAVE_ISSETUGID
181 #define is_elevated() issetugid()
182 #elif HAVE_GETEUID && HAVE_GETEGID
183 #define is_elevated() \
184         (getuid() != geteuid() \
185          || getgid() != getegid())
186 #else
187 #define is_elevated() FALSE
188 #endif
189
190 #if HAVE_SETFSUID
191 #define lower_privileges() \
192             int save_err = errno; \
193             setfsuid(getuid()); \
194             setfsgid(getgid()); \
195             errno = save_err
196 #define resume_elevation() \
197             save_err = errno; \
198             setfsuid(geteuid()); \
199             setfsgid(getegid()); \
200             errno = save_err
201 #else
202 #define lower_privileges()      /* nothing */
203 #define resume_elevation()      /* nothing */
204 #endif
205
206 #ifndef USE_ROOT_ENVIRON
207 /*
208  * Returns true if we allow application to use environment variables that are
209  * used for searching lists of directories, etc.
210  */
211 NCURSES_EXPORT(int)
212 _nc_env_access(void)
213 {
214     int result = TRUE;
215
216     if (is_elevated()) {
217         result = FALSE;
218     } else if ((getuid() == ROOT_UID) || (geteuid() == ROOT_UID)) {
219         result = FALSE;
220     }
221     return result;
222 }
223 #endif /* USE_ROOT_ENVIRON */
224
225 #ifndef USE_ROOT_ACCESS
226 /*
227  * Limit privileges if possible; otherwise disallow access for updating files.
228  */
229 NCURSES_EXPORT(FILE *)
230 _nc_safe_fopen(const char *path, const char *mode)
231 {
232     FILE *result = NULL;
233 #if HAVE_SETFSUID
234     lower_privileges();
235     result = fopen(path, mode);
236     resume_elevation();
237 #else
238     if (!is_elevated() || *mode == 'r') {
239         result = fopen(path, mode);
240     }
241 #endif
242     return result;
243 }
244
245 NCURSES_EXPORT(int)
246 _nc_safe_open3(const char *path, int flags, mode_t mode)
247 {
248     int result = -1;
249 #if HAVE_SETFSUID
250     lower_privileges();
251     result = open(path, flags, mode);
252     resume_elevation();
253 #else
254     if (!is_elevated() || (flags & O_RDONLY)) {
255         result = open(path, flags, mode);
256     }
257 #endif
258     return result;
259 }
260 #endif /* USE_ROOT_ENVIRON */