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