1 /****************************************************************************
2 * Copyright 2019,2020 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 #include <tic.h>
39
40 MODULE_ID("$Id: access.c,v 1.25 2020/02/02 23:34:34 tom Exp $")
41
42 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
43
NCURSES_EXPORT(char *)44 NCURSES_EXPORT(char *)
45 _nc_rootname(char *path)
46 {
47 char *result = _nc_basename(path);
48 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
49 static char *temp;
50 char *s;
51
52 temp = strdup(result);
53 result = temp;
54 #if !MIXEDCASE_FILENAMES
55 for (s = result; *s != '\0'; ++s) {
56 *s = (char) LOWERCASE(*s);
57 }
58 #endif
59 #if defined(PROG_EXT)
60 if ((s = strrchr(result, '.')) != 0) {
61 if (!strcmp(s, PROG_EXT))
62 *s = '\0';
63 }
64 #endif
65 #endif
66 return result;
67 }
68
69 /*
70 * Check if a string appears to be an absolute pathname.
71 */
72 NCURSES_EXPORT(bool)
_nc_is_abs_path(const char * path)73 _nc_is_abs_path(const char *path)
74 {
75 #if defined(__EMX__) || defined(__DJGPP__)
76 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
77 || (((s)[0] != 0) && ((s)[1] == ':')))
78 #else
79 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
80 #endif
81 return is_pathname(path);
82 }
83
84 /*
85 * Return index of the basename
86 */
87 NCURSES_EXPORT(unsigned)
_nc_pathlast(const char * path)88 _nc_pathlast(const char *path)
89 {
90 const char *test = strrchr(path, '/');
91 #ifdef __EMX__
92 if (test == 0)
93 test = strrchr(path, '\\');
94 #endif
95 if (test == 0)
96 test = path;
97 else
98 test++;
99 return (unsigned) (test - path);
100 }
101
102 NCURSES_EXPORT(char *)
_nc_basename(char * path)103 _nc_basename(char *path)
104 {
105 return path + _nc_pathlast(path);
106 }
107
108 NCURSES_EXPORT(int)
_nc_access(const char * path,int mode)109 _nc_access(const char *path, int mode)
110 {
111 int result;
112
113 if (path == 0) {
114 result = -1;
115 } else if (access(path, mode) < 0) {
116 if ((mode & W_OK) != 0
117 && errno == ENOENT
118 && strlen(path) < PATH_MAX) {
119 char head[PATH_MAX];
120 char *leaf;
121
122 _nc_STRCPY(head, path, sizeof(head));
123 leaf = _nc_basename(head);
124 if (leaf == 0)
125 leaf = head;
126 *leaf = '\0';
127 if (head == leaf)
128 _nc_STRCPY(head, ".", sizeof(head));
129
130 result = access(head, R_OK | W_OK | X_OK);
131 } else {
132 result = -1;
133 }
134 } else {
135 result = 0;
136 }
137 return result;
138 }
139
140 NCURSES_EXPORT(bool)
_nc_is_dir_path(const char * path)141 _nc_is_dir_path(const char *path)
142 {
143 bool result = FALSE;
144 struct stat sb;
145
146 if (stat(path, &sb) == 0
147 && S_ISDIR(sb.st_mode)) {
148 result = TRUE;
149 }
150 return result;
151 }
152
153 NCURSES_EXPORT(bool)
_nc_is_file_path(const char * path)154 _nc_is_file_path(const char *path)
155 {
156 bool result = FALSE;
157 struct stat sb;
158
159 if (stat(path, &sb) == 0
160 && S_ISREG(sb.st_mode)) {
161 result = TRUE;
162 }
163 return result;
164 }
165
166 #ifndef USE_ROOT_ENVIRON
167 /*
168 * Returns true if we allow application to use environment variables that are
169 * used for searching lists of directories, etc.
170 */
171 NCURSES_EXPORT(int)
_nc_env_access(void)172 _nc_env_access(void)
173 {
174 #if HAVE_ISSETUGID
175 if (issetugid())
176 return FALSE;
177 #elif HAVE_GETEUID && HAVE_GETEGID
178 if (getuid() != geteuid()
179 || getgid() != getegid())
180 return FALSE;
181 #endif
182 /* ...finally, disallow root */
183 return (getuid() != ROOT_UID) && (geteuid() != ROOT_UID);
184 }
185 #endif
186