check_user_privileges.lib.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Get user's global privileges and some db-specific privileges
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. *
  13. */
  14. $GLOBALS['is_superuser'] = $GLOBALS['dbi']->isSuperuser();
  15. /**
  16. * sets privilege information extracted from SHOW GRANTS result
  17. *
  18. * Detection for some CREATE privilege.
  19. *
  20. * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
  21. * (no control user needed) and we don't have to try any other method for
  22. * detection
  23. *
  24. * @todo fix to get really all privileges, not only explicitly defined for this user
  25. * from MySQL manual: (http://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
  26. * SHOW GRANTS displays only the privileges granted explicitly to the named
  27. * account. Other privileges might be available to the account, but they are not
  28. * displayed. For example, if an anonymous account exists, the named account
  29. * might be able to use its privileges, but SHOW GRANTS will not display them.
  30. *
  31. * @return void
  32. */
  33. function PMA_analyseShowGrant()
  34. {
  35. if (PMA_Util::cacheExists('is_create_db_priv', null)) {
  36. $GLOBALS['is_create_db_priv'] = PMA_Util::cacheGet(
  37. 'is_create_db_priv', null
  38. );
  39. $GLOBALS['is_process_priv'] = PMA_Util::cacheGet(
  40. 'is_process_priv', null
  41. );
  42. $GLOBALS['is_reload_priv'] = PMA_Util::cacheGet(
  43. 'is_reload_priv', null
  44. );
  45. $GLOBALS['db_to_create'] = PMA_Util::cacheGet(
  46. 'db_to_create', null
  47. );
  48. $GLOBALS['dbs_where_create_table_allowed'] = PMA_Util::cacheGet(
  49. 'dbs_where_create_table_allowed', null
  50. );
  51. return;
  52. }
  53. // defaults
  54. $GLOBALS['is_create_db_priv'] = false;
  55. $GLOBALS['is_process_priv'] = true;
  56. $GLOBALS['is_reload_priv'] = false;
  57. $GLOBALS['db_to_create'] = '';
  58. $GLOBALS['dbs_where_create_table_allowed'] = array();
  59. $rs_usr = $GLOBALS['dbi']->tryQuery('SHOW GRANTS');
  60. if (! $rs_usr) {
  61. return;
  62. }
  63. $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
  64. $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
  65. while ($row = $GLOBALS['dbi']->fetchRow($rs_usr)) {
  66. // extract db from GRANT ... ON *.* or GRANT ... ON db.*
  67. $db_name_offset = strpos($row[0], ' ON ') + 4;
  68. $show_grants_dbname = substr(
  69. $row[0], $db_name_offset,
  70. strpos($row[0], '.', $db_name_offset) - $db_name_offset
  71. );
  72. $show_grants_dbname
  73. = PMA_Util::unQuote($show_grants_dbname, '`');
  74. $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
  75. if ($show_grants_str == 'RELOAD') {
  76. $GLOBALS['is_reload_priv'] = true;
  77. }
  78. /**
  79. * @todo if we find CREATE VIEW but not CREATE, do not offer
  80. * the create database dialog box
  81. */
  82. if ($show_grants_str == 'ALL'
  83. || $show_grants_str == 'ALL PRIVILEGES'
  84. || $show_grants_str == 'CREATE'
  85. || strpos($show_grants_str, 'CREATE,') !== false
  86. ) {
  87. if ($show_grants_dbname == '*') {
  88. // a global CREATE privilege
  89. $GLOBALS['is_create_db_priv'] = true;
  90. $GLOBALS['is_reload_priv'] = true;
  91. $GLOBALS['db_to_create'] = '';
  92. $GLOBALS['dbs_where_create_table_allowed'][] = '*';
  93. // @todo we should not break here, cause GRANT ALL *.*
  94. // could be revoked by a later rule like GRANT SELECT ON db.*
  95. break;
  96. } else {
  97. // this array may contain wildcards
  98. $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
  99. $dbname_to_test = PMA_Util::backquote($show_grants_dbname);
  100. if ($GLOBALS['is_create_db_priv']) {
  101. // no need for any more tests if we already know this
  102. continue;
  103. }
  104. // does this db exist?
  105. if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
  106. && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
  107. || (! $GLOBALS['dbi']->tryQuery(
  108. 'USE ' . preg_replace(
  109. '/' . $re1 . '(%|_)/', '\\1\\3', $dbname_to_test
  110. )
  111. )
  112. && substr($GLOBALS['dbi']->getError(), 1, 4) != 1044)
  113. ) {
  114. /**
  115. * Do not handle the underscore wildcard
  116. * (this case must be rare anyway)
  117. */
  118. $GLOBALS['db_to_create'] = preg_replace(
  119. '/' . $re0 . '%/', '\\1',
  120. $show_grants_dbname
  121. );
  122. $GLOBALS['db_to_create'] = preg_replace(
  123. '/' . $re1 . '(%|_)/', '\\1\\3',
  124. $GLOBALS['db_to_create']
  125. );
  126. $GLOBALS['is_create_db_priv'] = true;
  127. /**
  128. * @todo collect $GLOBALS['db_to_create'] into an array,
  129. * to display a drop-down in the "Create database" dialog
  130. */
  131. // we don't break, we want all possible databases
  132. //break;
  133. } // end if
  134. } // end elseif
  135. } // end if
  136. } // end while
  137. $GLOBALS['dbi']->freeResult($rs_usr);
  138. // must also cacheUnset() them in
  139. // libraries/plugins/auth/AuthenticationCookie.class.php
  140. PMA_Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv'], null);
  141. PMA_Util::cacheSet('is_process_priv', $GLOBALS['is_process_priv'], null);
  142. PMA_Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv'], null);
  143. PMA_Util::cacheSet('db_to_create', $GLOBALS['db_to_create'], null);
  144. PMA_Util::cacheSet(
  145. 'dbs_where_create_table_allowed',
  146. $GLOBALS['dbs_where_create_table_allowed'],
  147. null
  148. );
  149. } // end function
  150. if (!PMA_DRIZZLE) {
  151. PMA_analyseShowGrant();
  152. } else {
  153. // todo: for simple_user_policy only database with user's login can be created
  154. // (unless logged in as root)
  155. $GLOBALS['is_create_db_priv'] = $GLOBALS['is_superuser'];
  156. $GLOBALS['is_process_priv'] = false;
  157. $GLOBALS['is_reload_priv'] = false;
  158. $GLOBALS['db_to_create'] = '';
  159. $GLOBALS['dbs_where_create_table_allowed'] = array('*');
  160. }
  161. ?>