1 /**************************************************************************** 2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Thomas E. Dickey * 31 ****************************************************************************/ 32 33 #include <curses.priv.h> 34 35 #include <ctype.h> 36 #include <sys/stat.h> 37 38 #include <tic.h> 39 40 MODULE_ID("$Id: access.c,v 1.16 2010/01/23 17:57:43 tom Exp $") 41 42 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c)) 43 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 = 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) 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) 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 *) 103 _nc_basename(char *path) 104 { 105 return path + _nc_pathlast(path); 106 } 107 108 NCURSES_EXPORT(int) 109 _nc_access(const char *path, int mode) 110 { 111 if (access(path, mode) < 0) { 112 if ((mode & W_OK) != 0 113 && errno == ENOENT 114 && strlen(path) < PATH_MAX) { 115 char head[PATH_MAX]; 116 char *leaf = _nc_basename(strcpy(head, path)); 117 118 if (leaf == 0) 119 leaf = head; 120 *leaf = '\0'; 121 if (head == leaf) 122 (void) strcpy(head, "."); 123 124 return access(head, R_OK | W_OK | X_OK); 125 } 126 return -1; 127 } 128 return 0; 129 } 130 131 NCURSES_EXPORT(bool) 132 _nc_is_dir_path(const char *path) 133 { 134 bool result = FALSE; 135 struct stat sb; 136 137 if (stat(path, &sb) == 0 138 && (sb.st_mode & S_IFMT) == S_IFDIR) { 139 result = TRUE; 140 } 141 return result; 142 } 143 144 NCURSES_EXPORT(bool) 145 _nc_is_file_path(const char *path) 146 { 147 bool result = FALSE; 148 struct stat sb; 149 150 if (stat(path, &sb) == 0 151 && (sb.st_mode & S_IFMT) == S_IFREG) { 152 result = TRUE; 153 } 154 return result; 155 } 156 157 #ifndef USE_ROOT_ENVIRON 158 /* 159 * Returns true if we allow application to use environment variables that are 160 * used for searching lists of directories, etc. 161 */ 162 NCURSES_EXPORT(int) 163 _nc_env_access(void) 164 { 165 #if HAVE_ISSETUGID 166 if (issetugid()) 167 return FALSE; 168 #elif HAVE_GETEUID && HAVE_GETEGID 169 if (getuid() != geteuid() 170 || getgid() != getegid()) 171 return FALSE; 172 #endif 173 return getuid() != 0 && geteuid() != 0; /* ...finally, disallow root */ 174 } 175 #endif 176