Translated using Weblate (Armenian)
[phpmyadmin.git] / libraries / check_user_privileges.lib.php
blobf3e9888339d475cbbb17e6154fe50d3597d9a56d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Get user's global privileges and some db-specific privileges
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 $GLOBALS['is_superuser'] = $GLOBALS['dbi']->isSuperuser();
17 /**
18 * Check if user has required privileges for
19 * performing 'FLUSH PRIVILEGES' operation
21 * @return void
23 function PMA_checkRequiredPrivilegesForFlushing()
26 $res = $GLOBALS['dbi']->tryQuery(
27 'FLUSH PRIVILEGES'
30 // Save the value
31 $GLOBALS['flush_priv'] = $res;
34 /**
35 * Check if user has required privileges for
36 * performing 'Adjust Privileges' operations
38 * @return void
40 function PMA_checkRequiredPrivilgesForAdjust()
42 $privs_available = true;
43 // FOR DB PRIVS
44 $select_privs_available = $GLOBALS['dbi']->tryQuery(
45 'SELECT * FROM `mysql`.`db` LIMIT 1'
48 $privs_available = $select_privs_available && $privs_available;
50 if ($privs_available) {
51 $delete_privs_available = $GLOBALS['dbi']->tryQuery(
52 'DELETE FROM `mysql`.`db` WHERE `host` = "" AND '
53 . '`Db` = "" AND `User` = "" LIMIT 1'
55 $privs_available = $delete_privs_available && $privs_available;
58 if ($privs_available) {
59 $insert_privs_available = $GLOBALS['dbi']->tryQuery(
60 'INSERT INTO `mysql`.`db`(`host`, `Db`, `User`) VALUES("pma_test_host", '
61 . '"mysql", "pma_test_user")'
63 // If successful test insert, delete the test row
64 if ($insert_privs_available) {
65 $GLOBALS['dbi']->tryQuery(
66 'DELETE FROM `mysql`.`db` WHERE host = "pma_test_host" AND '
67 . 'Db = "mysql" AND User = "pma_test_user" LIMIT 1'
70 $privs_available = $insert_privs_available && $privs_available;
73 if ($privs_available) {
74 $update_privs_available = $GLOBALS['dbi']->tryQuery(
75 'UPDATE `mysql`.`db` SET `host` = "" WHERE `host` = "" AND '
76 . '`Db` = "" AND `User` = "" LIMIT 1'
78 $privs_available = $update_privs_available && $privs_available;
80 // save the value
81 $GLOBALS['db_priv'] = $privs_available;
82 // reset the value
83 $privs_available = true;
85 // FOR COLUMNS_PRIV
86 $select_privs_available = $GLOBALS['dbi']->tryQuery(
87 'SELECT * FROM `mysql`.`columns_priv` LIMIT 1'
90 $privs_available = $select_privs_available && $privs_available;
92 if ($privs_available) {
93 $delete_privs_available = $GLOBALS['dbi']->tryQuery(
94 'DELETE FROM `mysql`.`columns_priv` WHERE `host` = "" AND '
95 . '`Db` = "" AND `User` = "" LIMIT 1'
97 $privs_available = $delete_privs_available && $privs_available;
100 if ($privs_available) {
101 $insert_privs_available = $GLOBALS['dbi']->tryQuery(
102 'INSERT INTO `mysql`.`columns_priv`(`host`, `Db`, `User`, `Table_name`,'
103 . ' `Column_name`) VALUES("pma_test_host", '
104 . '"mysql", "pma_test_user", "", "")'
106 // If successful test insert, delete the test row
107 if ($insert_privs_available) {
108 $GLOBALS['dbi']->tryQuery(
109 'DELETE FROM `mysql`.`columns_priv` WHERE host = "pma_test_host" AND '
110 . 'Db = "mysql" AND User = "pma_test_user" AND Table_name = ""'
111 . ' AND Column_name = "" LIMIT 1'
114 $privs_available = $insert_privs_available && $privs_available;
117 if ($privs_available) {
118 $update_privs_available = $GLOBALS['dbi']->tryQuery(
119 'UPDATE `mysql`.`columns_priv` SET `host` = "" WHERE `host` = "" AND '
120 . '`Db` = "" AND `User` = "" AND Column_name = "" AND Table_name = "" LIMIT 1'
122 $privs_available = $update_privs_available && $privs_available;
125 // Save the value
126 $GLOBALS['col_priv'] = $privs_available;
127 // Reset the value
128 $privs_available = true;
130 // FOR TABLES_PRIV
131 $select_privs_available = $GLOBALS['dbi']->tryQuery(
132 'SELECT * FROM `mysql`.`tables_priv` LIMIT 1'
135 $privs_available = $select_privs_available && $privs_available;
137 if ($privs_available) {
138 $delete_privs_available = $GLOBALS['dbi']->tryQuery(
139 'DELETE FROM `mysql`.`tables_priv` WHERE `host` = "" AND '
140 . '`Db` = "" AND `User` = "" AND Table_name = "" LIMIT 1'
142 $privs_available = $delete_privs_available && $privs_available;
145 if ($privs_available) {
146 $insert_privs_available = $GLOBALS['dbi']->tryQuery(
147 'INSERT INTO `mysql`.`tables_priv`(`host`, `Db`, `User`, `Table_name`'
148 . ') VALUES("pma_test_host", '
149 . '"mysql", "pma_test_user", "")'
151 // If successful test insert, delete the test row
152 if ($insert_privs_available) {
153 $GLOBALS['dbi']->tryQuery(
154 'DELETE FROM `mysql`.`tables_priv` WHERE host = "pma_test_host" AND '
155 . 'Db = "mysql" AND User = "pma_test_user" AND Table_name = "" LIMIT 1'
158 $privs_available = $insert_privs_available && $privs_available;
161 if ($privs_available) {
162 $update_privs_available = $GLOBALS['dbi']->tryQuery(
163 'UPDATE `mysql`.`tables_priv` SET `host` = "" WHERE `host` = "" AND '
164 . '`Db` = "" AND `User` = "" AND Table_name = "" LIMIT 1'
166 $privs_available = $update_privs_available && $privs_available;
169 // Save the value
170 $GLOBALS['table_priv'] = $privs_available;
171 // Reset the value
172 $privs_available = true;
174 // FOR PROCS_PRIV
175 $select_privs_available = $GLOBALS['dbi']->tryQuery(
176 'SELECT * FROM `mysql`.`procs_priv` LIMIT 1'
179 $privs_available = $select_privs_available && $privs_available;
181 if ($privs_available) {
182 $delete_privs_available = $GLOBALS['dbi']->tryQuery(
183 'DELETE FROM `mysql`.`procs_priv` WHERE `host` = "" AND '
184 . '`Db` = "" AND `User` = "" AND `Routine_name` = ""'
185 . ' AND `Routine_type` = "" LIMIT 1'
187 $privs_available = $delete_privs_available && $privs_available;
190 if ($privs_available) {
191 $insert_privs_available = $GLOBALS['dbi']->tryQuery(
192 'INSERT INTO `mysql`.`procs_priv`(`host`, `Db`, `User`, `Routine_name`,'
193 . ' `Routine_type`) VALUES("pma_test_host", '
194 . '"mysql", "pma_test_user", "", "PROCEDURE")'
196 // If successful test insert, delete the test row
197 if ($insert_privs_available) {
198 $GLOBALS['dbi']->tryQuery(
199 'DELETE FROM `mysql`.`procs_priv` WHERE `host` = "pma_test_host" AND '
200 . '`Db` = "mysql" AND `User` = "pma_test_user" AND `Routine_name` = ""'
201 . ' AND `Routine_type` = "PROCEDURE" LIMIT 1'
204 $privs_available = $insert_privs_available && $privs_available;
207 if ($privs_available) {
208 $update_privs_available = $GLOBALS['dbi']->tryQuery(
209 'UPDATE `mysql`.`procs_priv` SET `host` = "" WHERE `host` = "" AND '
210 . '`Db` = "" AND `User` = "" AND `Routine_name` = "" LIMIT 1'
212 $privs_available = $update_privs_available && $privs_available;
214 // Save the value
215 $GLOBALS['proc_priv'] = $privs_available;
220 * sets privilege information extracted from SHOW GRANTS result
222 * Detection for some CREATE privilege.
224 * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
225 * (no control user needed) and we don't have to try any other method for
226 * detection
228 * @todo fix to get really all privileges, not only explicitly defined for this user
229 * from MySQL manual: (http://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
230 * SHOW GRANTS displays only the privileges granted explicitly to the named
231 * account. Other privileges might be available to the account, but they are not
232 * displayed. For example, if an anonymous account exists, the named account
233 * might be able to use its privileges, but SHOW GRANTS will not display them.
235 * @return void
237 function PMA_analyseShowGrant()
239 if (PMA_Util::cacheExists('is_create_db_priv')) {
240 $GLOBALS['is_create_db_priv'] = PMA_Util::cacheGet(
241 'is_create_db_priv'
243 $GLOBALS['is_reload_priv'] = PMA_Util::cacheGet(
244 'is_reload_priv'
246 $GLOBALS['db_to_create'] = PMA_Util::cacheGet(
247 'db_to_create'
249 $GLOBALS['dbs_where_create_table_allowed'] = PMA_Util::cacheGet(
250 'dbs_where_create_table_allowed'
252 $GLOBALS['dbs_to_test'] = PMA_Util::cacheGet(
253 'dbs_to_test'
255 return;
258 // defaults
259 $GLOBALS['is_create_db_priv'] = false;
260 $GLOBALS['is_reload_priv'] = false;
261 $GLOBALS['db_to_create'] = '';
262 $GLOBALS['dbs_where_create_table_allowed'] = array();
263 $GLOBALS['dbs_to_test'] = $GLOBALS['dbi']->getSystemSchemas();
265 $rs_usr = $GLOBALS['dbi']->tryQuery('SHOW GRANTS');
267 if (! $rs_usr) {
268 return;
271 $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
272 $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
274 while ($row = $GLOBALS['dbi']->fetchRow($rs_usr)) {
275 // extract db from GRANT ... ON *.* or GRANT ... ON db.*
276 $db_name_offset = /*overload*/mb_strpos($row[0], ' ON ') + 4;
277 $show_grants_dbname = /*overload*/mb_substr(
278 $row[0], $db_name_offset,
279 /*overload*/mb_strpos($row[0], '.', $db_name_offset) - $db_name_offset
281 $show_grants_dbname = PMA_Util::unQuote($show_grants_dbname, '`');
283 $show_grants_str = /*overload*/mb_substr(
284 $row[0],
286 (/*overload*/mb_strpos($row[0], ' ON ') - 6)
289 if ($show_grants_dbname == '*') {
290 if ($show_grants_str != 'USAGE') {
291 $GLOBALS['dbs_to_test'] = false;
293 } elseif ($GLOBALS['dbs_to_test'] !== false) {
294 $GLOBALS['dbs_to_test'][] = $show_grants_dbname;
297 if ($show_grants_str == 'RELOAD') {
298 $GLOBALS['is_reload_priv'] = true;
302 * @todo if we find CREATE VIEW but not CREATE, do not offer
303 * the create database dialog box
305 if ($show_grants_str == 'ALL'
306 || $show_grants_str == 'ALL PRIVILEGES'
307 || $show_grants_str == 'CREATE'
308 || strpos($show_grants_str, 'CREATE,') !== false
310 if ($show_grants_dbname == '*') {
311 // a global CREATE privilege
312 $GLOBALS['is_create_db_priv'] = true;
313 $GLOBALS['is_reload_priv'] = true;
314 $GLOBALS['db_to_create'] = '';
315 $GLOBALS['dbs_where_create_table_allowed'][] = '*';
316 // @todo we should not break here, cause GRANT ALL *.*
317 // could be revoked by a later rule like GRANT SELECT ON db.*
318 break;
319 } else {
320 // this array may contain wildcards
321 $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
323 $dbname_to_test = PMA_Util::backquote($show_grants_dbname);
325 if ($GLOBALS['is_create_db_priv']) {
326 // no need for any more tests if we already know this
327 continue;
330 // does this db exist?
331 if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
332 && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
333 || (! $GLOBALS['dbi']->tryQuery(
334 'USE ' . preg_replace(
335 '/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test
338 && /*overload*/mb_substr($GLOBALS['dbi']->getError(), 1, 4) != 1044)
341 * Do not handle the underscore wildcard
342 * (this case must be rare anyway)
344 $GLOBALS['db_to_create'] = preg_replace(
345 '/' . $re0 . '%/', '\\1',
346 $show_grants_dbname
348 $GLOBALS['db_to_create'] = preg_replace(
349 '/' . $re1 . '(%|_)/', '\\1\\3',
350 $GLOBALS['db_to_create']
352 $GLOBALS['is_create_db_priv'] = true;
355 * @todo collect $GLOBALS['db_to_create'] into an array,
356 * to display a drop-down in the "Create database" dialog
358 // we don't break, we want all possible databases
359 //break;
360 } // end if
361 } // end elseif
362 } // end if
363 } // end while
365 $GLOBALS['dbi']->freeResult($rs_usr);
367 // must also cacheUnset() them in
368 // libraries/plugins/auth/AuthenticationCookie.class.php
369 PMA_Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv']);
370 PMA_Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv']);
371 PMA_Util::cacheSet('db_to_create', $GLOBALS['db_to_create']);
372 PMA_Util::cacheSet(
373 'dbs_where_create_table_allowed',
374 $GLOBALS['dbs_where_create_table_allowed']
376 PMA_Util::cacheSet('dbs_to_test', $GLOBALS['dbs_to_test']);
377 } // end function
379 if (!PMA_DRIZZLE) {
380 $user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();");
381 if ($user == '@') { // MySQL is started with --skip-grant-tables
382 $GLOBALS['is_create_db_priv'] = true;
383 $GLOBALS['is_reload_priv'] = true;
384 $GLOBALS['db_to_create'] = '';
385 $GLOBALS['dbs_where_create_table_allowed'] = array('*');
386 $GLOBALS['dbs_to_test'] = false;
387 } else {
388 PMA_analyseShowGrant();
391 // Check if privileges to 'mysql'.col_privs, 'mysql'.db,
392 // 'mysql'.table_privs, 'mysql'.proc_privs and privileges for
393 // flushing the privileges are available
394 PMA_checkRequiredPrivilegesForFlushing();
395 PMA_checkRequiredPrivilgesForAdjust();
397 } else {
398 // todo: for simple_user_policy only database with user's login can be created
399 // (unless logged in as root)
400 $GLOBALS['is_create_db_priv'] = $GLOBALS['is_superuser'];
401 $GLOBALS['is_reload_priv'] = false;
402 $GLOBALS['db_to_create'] = '';
403 $GLOBALS['dbs_where_create_table_allowed'] = array('*');
404 $GLOBALS['dbs_to_test'] = false;