MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_access.c
1 /* Copyright (C) 2000 MySQL AB
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #include "mysys_priv.h"
17 #include <m_string.h>
18 
19 #ifdef __WIN__
20 
21 /*
22  Check a file or path for accessability.
23 
24  SYNOPSIS
25  file_access()
26  path Path to file
27  amode Access method
28 
29  DESCRIPTION
30  This function wraps the normal access method because the access
31  available in MSVCRT> +reports that filenames such as LPT1 and
32  COM1 are valid (they are but should not be so for us).
33 
34  RETURN VALUES
35  0 ok
36  -1 error (We use -1 as my_access is mapped to access on other platforms)
37 */
38 
39 int my_access(const char *path, int amode)
40 {
41  WIN32_FILE_ATTRIBUTE_DATA fileinfo;
42  BOOL result;
43 
44  result= GetFileAttributesEx(path, GetFileExInfoStandard, &fileinfo);
45  if (! result ||
46  (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) && (amode & W_OK))
47  {
48  my_errno= errno= EACCES;
49  return -1;
50  }
51  return 0;
52 }
53 
54 #endif /* __WIN__ */
55 
56 
57 /*
58  List of file names that causes problem on windows
59 
60  NOTE that one can also not have file names of type CON.TXT
61 
62  NOTE: it is important to keep "CLOCK$" on the first place,
63  we skip it in check_if_legal_tablename.
64 */
65 static const char *reserved_names[]=
66 {
67  "CLOCK$",
68  "CON", "PRN", "AUX", "NUL",
69  "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
70  "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
71  NullS
72 };
73 
74 #define MAX_RESERVED_NAME_LENGTH 6
75 
76 
77 /*
78  Looks up a null-terminated string in a list,
79  case insensitively.
80 
81  SYNOPSIS
82  str_list_find()
83  list list of items
84  str item to find
85 
86  RETURN
87  0 ok
88  1 reserved file name
89 */
90 static int str_list_find(const char **list, const char *str)
91 {
92  const char **name;
93  for (name= list; *name; name++)
94  {
95  if (!my_strcasecmp(&my_charset_latin1, *name, str))
96  return 1;
97  }
98  return 0;
99 }
100 
101 
102 /*
103  A map for faster reserved_names lookup,
104  helps to avoid loops in many cases.
105  1 - can be the first letter
106  2 - can be the second letter
107  4 - can be the third letter
108 */
109 static char reserved_map[256]=
110 {
111  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
112  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
113  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* !"#$%&'()*+,-./ */
114  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0123456789:;<=>? */
115  0,1,0,1,0,0,0,0,0,0,0,0,7,4,5,2, /* @ABCDEFGHIJKLMNO */
116  3,0,2,0,4,2,0,0,4,0,0,0,0,0,0,0, /* PQRSTUVWXYZ[\]^_ */
117  0,1,0,1,0,0,0,0,0,0,0,0,7,4,5,2, /* bcdefghijklmno */
118  3,0,2,0,4,2,0,0,4,0,0,0,0,0,0,0, /* pqrstuvwxyz{|}~. */
119  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
120  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
121  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
122  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
123  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
124  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
125  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ................ */
126  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* ................ */
127 };
128 
129 
130 /*
131  Check if a table name may cause problems
132 
133  SYNOPSIS
134  check_if_legal_tablename
135  name Table name (without any extensions)
136 
137  DESCRIPTION
138  We don't check 'CLOCK$' because dollar sign is encoded as @0024,
139  making table file name 'CLOCK@0024', which is safe.
140  This is why we start lookup from the second element
141  (i.e. &reserver_name[1])
142 
143  RETURN
144  0 ok
145  1 reserved file name
146 */
147 
148 int check_if_legal_tablename(const char *name)
149 {
150  DBUG_ENTER("check_if_legal_tablename");
151  DBUG_RETURN(name[0] != 0 && name[1] != 0 &&
152  (reserved_map[(uchar) name[0]] & 1) &&
153  (reserved_map[(uchar) name[1]] & 2) &&
154  (reserved_map[(uchar) name[2]] & 4) &&
155  str_list_find(&reserved_names[1], name));
156 }
157 
158 
159 #ifdef __WIN__
160 
168 static my_bool does_drive_exists(char drive_letter)
169 {
170  DWORD drive_mask= GetLogicalDrives();
171  drive_letter= toupper(drive_letter);
172 
173  return (drive_letter >= 'A' && drive_letter <= 'Z') &&
174  (drive_mask & (0x1 << (drive_letter - 'A')));
175 }
176 
190 my_bool is_filename_allowed(const char *name __attribute__((unused)),
191  size_t length __attribute__((unused)),
192  my_bool allow_current_dir __attribute__((unused)))
193 {
194  /*
195  For Windows, check if the file name contains : character.
196  Start from end of path and search if the file name contains :
197  */
198  const char* ch = NULL;
199  for (ch= name + length - 1; ch >= name; --ch)
200  {
201  if (FN_LIBCHAR == *ch || '/' == *ch)
202  break;
203  else if (':' == *ch)
204  {
205  /*
206  File names like C:foobar.txt are allowed since the syntax means
207  file foobar.txt in current directory of C drive. However file
208  names likes CC:foobar are not allowed since this syntax means ADS
209  foobar in file CC.
210  */
211  return (allow_current_dir && (ch - name == 1) &&
212  does_drive_exists(*name));
213  }
214  }
215  return TRUE;
216 } /* is_filename_allowed */
217 #endif /* __WIN__ */
218 
219 #if defined(__WIN__) || defined(__EMX__)
220 
221 
222 /*
223  Check if a path will access a reserverd file name that may cause problems
224 
225  SYNOPSIS
226  check_if_legal_filename
227  path Path to file
228 
229  RETURN
230  0 ok
231  1 reserved file name
232 */
233 
234 int check_if_legal_filename(const char *path)
235 {
236  const char *end;
237  const char **reserved_name;
238  DBUG_ENTER("check_if_legal_filename");
239 
240  if (!is_filename_allowed(path, strlen(path), TRUE))
241  DBUG_RETURN(1);
242 
243  path+= dirname_length(path); /* To start of filename */
244  if (!(end= strchr(path, FN_EXTCHAR)))
245  end= strend(path);
246  if (path == end || (uint) (end - path) > MAX_RESERVED_NAME_LENGTH)
247  DBUG_RETURN(0); /* Simplify inner loop */
248 
249  for (reserved_name= reserved_names; *reserved_name; reserved_name++)
250  {
251  const char *reserved= *reserved_name; /* never empty */
252  const char *name= path;
253 
254  do
255  {
256  if (*reserved != my_toupper(&my_charset_latin1, *name))
257  break;
258  if (++name == end && !reserved[1])
259  DBUG_RETURN(1); /* Found wrong path */
260  } while (*++reserved);
261  }
262  DBUG_RETURN(0);
263 }
264 
265 #endif /* defined(__WIN__) || defined(__EMX__) */