/phpmyadmin/libraries/check_user_privileges.lib.php

https://github.com/amcl/openemr · PHP · 178 lines · 113 code · 17 blank · 48 comment · 43 complexity · 62c09167ae0dc7fd3ec62220f08c8ad9 MD5 · raw file

  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. * ($controllink and $userlink are links to MySQL defined in the "common.inc.php" library)
  6. * Note: if no controluser is defined, $controllink contains $userlink
  7. *
  8. * @version $Id$
  9. */
  10. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. *
  15. */
  16. $is_create_db_priv = false;
  17. $is_process_priv = true;
  18. $is_reload_priv = false;
  19. $db_to_create = '';
  20. $dbs_where_create_table_allowed = array();
  21. // We were trying to find if user if superuser with 'USE mysql'
  22. // but users with the global priv CREATE TEMPORARY TABLES or LOCK TABLES
  23. // can do a 'USE mysql' (even if they cannot see the tables)
  24. $is_superuser = PMA_isSuperuser();
  25. function PMA_analyseShowGrant($rs_usr, &$is_create_db_priv, &$db_to_create, &$is_reload_priv, &$dbs_where_create_table_allowed) {
  26. $re0 = '(^|(\\\\\\\\)+|[^\])'; // non-escaped wildcards
  27. $re1 = '(^|[^\])(\\\)+'; // escaped wildcards
  28. while ($row = PMA_DBI_fetch_row($rs_usr)) {
  29. $show_grants_dbname = substr($row[0], strpos($row[0], ' ON ') + 4, (strpos($row[0], '.', strpos($row[0], ' ON ')) - strpos($row[0], ' ON ') - 4));
  30. $show_grants_dbname = ereg_replace('^`(.*)`', '\\1', $show_grants_dbname);
  31. $show_grants_str = substr($row[0], 6, (strpos($row[0], ' ON ') - 6));
  32. if ($show_grants_str == 'RELOAD') {
  33. $is_reload_priv = true;
  34. }
  35. /**
  36. * @todo if we find CREATE VIEW but not CREATE, do not offer
  37. * the create database dialog box
  38. */
  39. if (($show_grants_str == 'ALL') || ($show_grants_str == 'ALL PRIVILEGES') || ($show_grants_str == 'CREATE') || strpos($show_grants_str, 'CREATE,') !== false) {
  40. if ($show_grants_dbname == '*') {
  41. // a global CREATE privilege
  42. $is_create_db_priv = true;
  43. $is_reload_priv = true;
  44. $db_to_create = '';
  45. $dbs_where_create_table_allowed[] = '*';
  46. break;
  47. } else {
  48. // this array may contain wildcards
  49. $dbs_where_create_table_allowed[] = $show_grants_dbname;
  50. // before MySQL 4.1.0, we cannot use backquotes around a dbname
  51. // for the USE command, so the USE will fail if the dbname contains
  52. // a "-" and we cannot detect if such a db already exists;
  53. // since 4.1.0, we need to use backquotes if the dbname contains a "-"
  54. // in a USE command
  55. if (PMA_MYSQL_INT_VERSION > 40100) {
  56. $dbname_to_test = PMA_backquote($show_grants_dbname);
  57. } else {
  58. $dbname_to_test = $show_grants_dbname;
  59. }
  60. if ((ereg($re0 . '%|_', $show_grants_dbname)
  61. && !ereg('\\\\%|\\\\_', $show_grants_dbname))
  62. // does this db exist?
  63. || (!PMA_DBI_try_query('USE ' . ereg_replace($re1 .'(%|_)', '\\1\\3', $dbname_to_test), null, PMA_DBI_QUERY_STORE)
  64. && substr(PMA_DBI_getError(), 1, 4) != 1044)
  65. ) {
  66. $db_to_create = ereg_replace($re0 . '%', '\\1...', ereg_replace($re0 . '_', '\\1?', $show_grants_dbname));
  67. $db_to_create = ereg_replace($re1 . '(%|_)', '\\1\\3', $db_to_create);
  68. $is_create_db_priv = true;
  69. /**
  70. * @todo collect $db_to_create into an array, to display a
  71. * drop-down in the "Create new database" dialog
  72. */
  73. // we don't break, we want all possible databases
  74. //break;
  75. } // end if
  76. } // end elseif
  77. } // end if
  78. } // end while
  79. } // end function
  80. // Detection for some CREATE privilege.
  81. // Since MySQL 4.1.2, we can easily detect current user's grants
  82. // using $userlink (no control user needed)
  83. // and we don't have to try any other method for detection
  84. if (PMA_MYSQL_INT_VERSION >= 40102) {
  85. $rs_usr = PMA_DBI_try_query('SHOW GRANTS', $userlink, PMA_DBI_QUERY_STORE);
  86. if ($rs_usr) {
  87. PMA_analyseShowGrant($rs_usr, $is_create_db_priv, $db_to_create, $is_reload_priv, $dbs_where_create_table_allowed);
  88. PMA_DBI_free_result($rs_usr);
  89. unset($rs_usr);
  90. }
  91. } else {
  92. // Before MySQL 4.1.2, we first try to find a priv in mysql.user. Hopefuly
  93. // the controluser is correctly defined; but here, $controllink could contain
  94. // $userlink so maybe the SELECT will fail
  95. if (!$is_create_db_priv) {
  96. $res = PMA_DBI_query('SELECT USER();', null, PMA_DBI_QUERY_STORE);
  97. list($mysql_cur_user_and_host) = PMA_DBI_fetch_row($res);
  98. $mysql_cur_user = substr($mysql_cur_user_and_host, 0, strrpos($mysql_cur_user_and_host, '@'));
  99. $local_query = 'SELECT Create_priv, Reload_priv FROM mysql.user WHERE ' . PMA_convert_using('User') . ' = ' . PMA_convert_using(PMA_sqlAddslashes($mysql_cur_user), 'quoted') . ' OR ' . PMA_convert_using('User') . ' = ' . PMA_convert_using('', 'quoted') . ';';
  100. $rs_usr = PMA_DBI_try_query($local_query, $controllink, PMA_DBI_QUERY_STORE); // Debug: or PMA_mysqlDie('', $local_query, false);
  101. if ($rs_usr) {
  102. while ($result_usr = PMA_DBI_fetch_assoc($rs_usr)) {
  103. if (!$is_create_db_priv) {
  104. $is_create_db_priv = ($result_usr['Create_priv'] == 'Y');
  105. }
  106. if (!$is_reload_priv) {
  107. $is_reload_priv = ($result_usr['Reload_priv'] == 'Y');
  108. }
  109. } // end while
  110. PMA_DBI_free_result($rs_usr);
  111. unset($rs_usr, $result_usr);
  112. if ($is_create_db_priv) {
  113. $dbs_where_create_table_allowed[] = '*';
  114. }
  115. } // end if
  116. } // end if
  117. // If the user has Create priv on a inexistant db, show him in the dialog
  118. // the first inexistant db name that we find, in most cases it's probably
  119. // the one he just dropped :)
  120. if (!$is_create_db_priv) {
  121. $local_query = 'SELECT DISTINCT Db FROM mysql.db WHERE ' . PMA_convert_using('Create_priv') . ' = ' . PMA_convert_using('Y', 'quoted') . ' AND (' . PMA_convert_using('User') . ' = ' .PMA_convert_using(PMA_sqlAddslashes($mysql_cur_user), 'quoted') . ' OR ' . PMA_convert_using('User') . ' = ' . PMA_convert_using('', 'quoted') . ');';
  122. $rs_usr = PMA_DBI_try_query($local_query, $controllink, PMA_DBI_QUERY_STORE);
  123. if ($rs_usr) {
  124. $re0 = '(^|(\\\\\\\\)+|[^\])'; // non-escaped wildcards
  125. $re1 = '(^|[^\])(\\\)+'; // escaped wildcards
  126. while ($row = PMA_DBI_fetch_assoc($rs_usr)) {
  127. $dbs_where_create_table_allowed[] = $row['Db'];
  128. if (ereg($re0 . '(%|_)', $row['Db'])
  129. || (!PMA_DBI_try_query('USE ' . ereg_replace($re1 . '(%|_)', '\\1\\3', $row['Db'])) && substr(PMA_DBI_getError(), 1, 4) != 1044)) {
  130. $db_to_create = ereg_replace($re0 . '%', '\\1...', ereg_replace($re0 . '_', '\\1?', $row['Db']));
  131. $db_to_create = ereg_replace($re1 . '(%|_)', '\\1\\3', $db_to_create);
  132. $is_create_db_priv = true;
  133. break;
  134. } // end if
  135. } // end while
  136. PMA_DBI_free_result($rs_usr);
  137. unset($rs_usr, $row, $re0, $re1);
  138. } else {
  139. // Finally, let's try to get the user's privileges by using SHOW
  140. // GRANTS...
  141. // Maybe we'll find a little CREATE priv there :)
  142. $rs_usr = PMA_DBI_try_query('SHOW GRANTS FOR ' . $mysql_cur_user_and_host . ';', $controllink, PMA_DBI_QUERY_STORE);
  143. if (!$rs_usr) {
  144. // OK, now we'd have to guess the user's hostname, but we
  145. // only try out the 'username'@'%' case.
  146. $rs_usr = PMA_DBI_try_query('SHOW GRANTS FOR ' . PMA_convert_using(PMA_sqlAddslashes($mysql_cur_user), 'quoted') . ';', $controllink, PMA_DBI_QUERY_STORE);
  147. }
  148. unset($local_query);
  149. if ($rs_usr) {
  150. PMA_analyseShowGrant($rs_usr, $is_create_db_priv, $db_to_create, $is_reload_priv, $dbs_where_create_table_allowed);
  151. PMA_DBI_free_result($rs_usr);
  152. unset($rs_usr);
  153. } // end if
  154. } // end elseif
  155. } // end if
  156. } // end else (MySQL < 4.1.2)
  157. // If disabled, don't show it
  158. if (!$cfg['SuggestDBName']) {
  159. $db_to_create = '';
  160. }
  161. ?>