CheckUserPrivileges.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Util;
  12. /**
  13. * PhpMyAdmin\CheckUserPrivileges class
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class CheckUserPrivileges
  18. {
  19. /**
  20. * @var DatabaseInterface
  21. */
  22. private $dbi;
  23. /**
  24. * Constructor
  25. *
  26. * @param DatabaseInterface $dbi DatabaseInterface object
  27. */
  28. public function __construct(DatabaseInterface $dbi)
  29. {
  30. $this->dbi = $dbi;
  31. }
  32. /**
  33. * Extracts details from a result row of a SHOW GRANT query
  34. *
  35. * @param string $row grant row
  36. *
  37. * @return array
  38. */
  39. public function getItemsFromShowGrantsRow(string $row): array
  40. {
  41. $db_name_offset = mb_strpos($row, ' ON ') + 4;
  42. $tblname_end_offset = mb_strpos($row, ' TO ');
  43. $tblname_start_offset = false;
  44. if (($__tblname_start_offset = mb_strpos($row, '`.', $db_name_offset))
  45. && $__tblname_start_offset
  46. < $tblname_end_offset) {
  47. $tblname_start_offset = $__tblname_start_offset + 1;
  48. }
  49. if (! $tblname_start_offset) {
  50. $tblname_start_offset = mb_strpos($row, '.', $db_name_offset);
  51. }
  52. $show_grants_dbname = mb_substr(
  53. $row,
  54. $db_name_offset,
  55. $tblname_start_offset - $db_name_offset
  56. );
  57. $show_grants_dbname = Util::unQuote($show_grants_dbname, '`');
  58. $show_grants_str = mb_substr(
  59. $row,
  60. 6,
  61. mb_strpos($row, ' ON ') - 6
  62. );
  63. $show_grants_tblname = mb_substr(
  64. $row,
  65. $tblname_start_offset + 1,
  66. $tblname_end_offset - $tblname_start_offset - 1
  67. );
  68. $show_grants_tblname = Util::unQuote($show_grants_tblname, '`');
  69. return [
  70. $show_grants_str,
  71. $show_grants_dbname,
  72. $show_grants_tblname,
  73. ];
  74. }
  75. /**
  76. * Check if user has required privileges for
  77. * performing 'Adjust privileges' operations
  78. *
  79. * @param string $show_grants_str string containing grants for user
  80. * @param string $show_grants_dbname name of db extracted from grant string
  81. * @param string $show_grants_tblname name of table extracted from grant string
  82. *
  83. * @return void
  84. */
  85. public function checkRequiredPrivilegesForAdjust(
  86. string $show_grants_str,
  87. string $show_grants_dbname,
  88. string $show_grants_tblname
  89. ): void {
  90. // '... ALL PRIVILEGES ON *.* ...' OR '... ALL PRIVILEGES ON `mysql`.* ..'
  91. // OR
  92. // SELECT, INSERT, UPDATE, DELETE .... ON *.* OR `mysql`.*
  93. if ($show_grants_str == 'ALL'
  94. || $show_grants_str == 'ALL PRIVILEGES'
  95. || (mb_strpos(
  96. $show_grants_str,
  97. 'SELECT, INSERT, UPDATE, DELETE'
  98. ) !== false)
  99. ) {
  100. if ($show_grants_dbname == '*'
  101. && $show_grants_tblname == '*'
  102. ) {
  103. $GLOBALS['col_priv'] = true;
  104. $GLOBALS['db_priv'] = true;
  105. $GLOBALS['proc_priv'] = true;
  106. $GLOBALS['table_priv'] = true;
  107. if ($show_grants_str == 'ALL PRIVILEGES'
  108. || $show_grants_str == 'ALL'
  109. ) {
  110. $GLOBALS['is_reload_priv'] = true;
  111. }
  112. }
  113. // check for specific tables in `mysql` db
  114. // Ex. '... ALL PRIVILEGES on `mysql`.`columns_priv` .. '
  115. if ($show_grants_dbname == 'mysql') {
  116. switch ($show_grants_tblname) {
  117. case "columns_priv":
  118. $GLOBALS['col_priv'] = true;
  119. break;
  120. case "db":
  121. $GLOBALS['db_priv'] = true;
  122. break;
  123. case "procs_priv":
  124. $GLOBALS['proc_priv'] = true;
  125. break;
  126. case "tables_priv":
  127. $GLOBALS['table_priv'] = true;
  128. break;
  129. case "*":
  130. $GLOBALS['col_priv'] = true;
  131. $GLOBALS['db_priv'] = true;
  132. $GLOBALS['proc_priv'] = true;
  133. $GLOBALS['table_priv'] = true;
  134. break;
  135. default:
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * sets privilege information extracted from SHOW GRANTS result
  142. *
  143. * Detection for some CREATE privilege.
  144. *
  145. * Since MySQL 4.1.2, we can easily detect current user's grants using $userlink
  146. * (no control user needed) and we don't have to try any other method for
  147. * detection
  148. *
  149. * @todo fix to get really all privileges, not only explicitly defined for this user
  150. * from MySQL manual: (https://dev.mysql.com/doc/refman/5.0/en/show-grants.html)
  151. * SHOW GRANTS displays only the privileges granted explicitly to the named
  152. * account. Other privileges might be available to the account, but they are not
  153. * displayed. For example, if an anonymous account exists, the named account
  154. * might be able to use its privileges, but SHOW GRANTS will not display them.
  155. *
  156. * @return void
  157. */
  158. private function analyseShowGrant(): void
  159. {
  160. if (Util::cacheExists('is_create_db_priv')) {
  161. $GLOBALS['is_create_db_priv'] = Util::cacheGet(
  162. 'is_create_db_priv'
  163. );
  164. $GLOBALS['is_reload_priv'] = Util::cacheGet(
  165. 'is_reload_priv'
  166. );
  167. $GLOBALS['db_to_create'] = Util::cacheGet(
  168. 'db_to_create'
  169. );
  170. $GLOBALS['dbs_where_create_table_allowed'] = Util::cacheGet(
  171. 'dbs_where_create_table_allowed'
  172. );
  173. $GLOBALS['dbs_to_test'] = Util::cacheGet(
  174. 'dbs_to_test'
  175. );
  176. $GLOBALS['db_priv'] = Util::cacheGet(
  177. 'db_priv'
  178. );
  179. $GLOBALS['col_priv'] = Util::cacheGet(
  180. 'col_priv'
  181. );
  182. $GLOBALS['table_priv'] = Util::cacheGet(
  183. 'table_priv'
  184. );
  185. $GLOBALS['proc_priv'] = Util::cacheGet(
  186. 'proc_priv'
  187. );
  188. return;
  189. }
  190. // defaults
  191. $GLOBALS['is_create_db_priv'] = false;
  192. $GLOBALS['is_reload_priv'] = false;
  193. $GLOBALS['db_to_create'] = '';
  194. $GLOBALS['dbs_where_create_table_allowed'] = [];
  195. $GLOBALS['dbs_to_test'] = $this->dbi->getSystemSchemas();
  196. $GLOBALS['proc_priv'] = false;
  197. $GLOBALS['db_priv'] = false;
  198. $GLOBALS['col_priv'] = false;
  199. $GLOBALS['table_priv'] = false;
  200. $rs_usr = $this->dbi->tryQuery('SHOW GRANTS');
  201. if (! $rs_usr) {
  202. return;
  203. }
  204. $re0 = '(^|(\\\\\\\\)+|[^\\\\])'; // non-escaped wildcards
  205. $re1 = '(^|[^\\\\])(\\\)+'; // escaped wildcards
  206. while ($row = $this->dbi->fetchRow($rs_usr)) {
  207. list(
  208. $show_grants_str,
  209. $show_grants_dbname,
  210. $show_grants_tblname
  211. ) = $this->getItemsFromShowGrantsRow($row[0]);
  212. if ($show_grants_dbname == '*') {
  213. if ($show_grants_str != 'USAGE') {
  214. $GLOBALS['dbs_to_test'] = false;
  215. }
  216. } elseif ($GLOBALS['dbs_to_test'] !== false) {
  217. $GLOBALS['dbs_to_test'][] = $show_grants_dbname;
  218. }
  219. if (mb_strpos($show_grants_str, 'RELOAD') !== false) {
  220. $GLOBALS['is_reload_priv'] = true;
  221. }
  222. // check for the required privileges for adjust
  223. $this->checkRequiredPrivilegesForAdjust(
  224. $show_grants_str,
  225. $show_grants_dbname,
  226. $show_grants_tblname
  227. );
  228. /**
  229. * @todo if we find CREATE VIEW but not CREATE, do not offer
  230. * the create database dialog box
  231. */
  232. if ($show_grants_str == 'ALL'
  233. || $show_grants_str == 'ALL PRIVILEGES'
  234. || $show_grants_str == 'CREATE'
  235. || strpos($show_grants_str, 'CREATE,') !== false
  236. ) {
  237. if ($show_grants_dbname == '*') {
  238. // a global CREATE privilege
  239. $GLOBALS['is_create_db_priv'] = true;
  240. $GLOBALS['is_reload_priv'] = true;
  241. $GLOBALS['db_to_create'] = '';
  242. $GLOBALS['dbs_where_create_table_allowed'][] = '*';
  243. // @todo we should not break here, cause GRANT ALL *.*
  244. // could be revoked by a later rule like GRANT SELECT ON db.*
  245. break;
  246. } else {
  247. // this array may contain wildcards
  248. $GLOBALS['dbs_where_create_table_allowed'][] = $show_grants_dbname;
  249. $dbname_to_test = Util::backquote($show_grants_dbname);
  250. if ($GLOBALS['is_create_db_priv']) {
  251. // no need for any more tests if we already know this
  252. continue;
  253. }
  254. // does this db exist?
  255. if ((preg_match('/' . $re0 . '%|_/', $show_grants_dbname)
  256. && ! preg_match('/\\\\%|\\\\_/', $show_grants_dbname))
  257. || (! $this->dbi->tryQuery(
  258. 'USE ' . preg_replace(
  259. '/' . $re1 . '(%|_)/',
  260. '\\1\\3',
  261. $dbname_to_test
  262. )
  263. )
  264. && mb_substr($this->dbi->getError(), 1, 4) != 1044)
  265. ) {
  266. /**
  267. * Do not handle the underscore wildcard
  268. * (this case must be rare anyway)
  269. */
  270. $GLOBALS['db_to_create'] = preg_replace(
  271. '/' . $re0 . '%/',
  272. '\\1',
  273. $show_grants_dbname
  274. );
  275. $GLOBALS['db_to_create'] = preg_replace(
  276. '/' . $re1 . '(%|_)/',
  277. '\\1\\3',
  278. $GLOBALS['db_to_create']
  279. );
  280. $GLOBALS['is_create_db_priv'] = true;
  281. /**
  282. * @todo collect $GLOBALS['db_to_create'] into an array,
  283. * to display a drop-down in the "Create database" dialog
  284. */
  285. // we don't break, we want all possible databases
  286. //break;
  287. } // end if
  288. } // end elseif
  289. } // end if
  290. } // end while
  291. $this->dbi->freeResult($rs_usr);
  292. // must also cacheUnset() them in
  293. // PhpMyAdmin\Plugins\Auth\AuthenticationCookie
  294. Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv']);
  295. Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv']);
  296. Util::cacheSet('db_to_create', $GLOBALS['db_to_create']);
  297. Util::cacheSet(
  298. 'dbs_where_create_table_allowed',
  299. $GLOBALS['dbs_where_create_table_allowed']
  300. );
  301. Util::cacheSet('dbs_to_test', $GLOBALS['dbs_to_test']);
  302. Util::cacheSet('proc_priv', $GLOBALS['proc_priv']);
  303. Util::cacheSet('table_priv', $GLOBALS['table_priv']);
  304. Util::cacheSet('col_priv', $GLOBALS['col_priv']);
  305. Util::cacheSet('db_priv', $GLOBALS['db_priv']);
  306. }
  307. /**
  308. * Get user's global privileges and some db-specific privileges
  309. *
  310. * @return void
  311. */
  312. public function getPrivileges(): void
  313. {
  314. $username = '';
  315. $current = $this->dbi->getCurrentUserAndHost();
  316. if (! empty($current)) {
  317. list($username, ) = $current;
  318. }
  319. // If MySQL is started with --skip-grant-tables
  320. if ($username === '') {
  321. $GLOBALS['is_create_db_priv'] = true;
  322. $GLOBALS['is_reload_priv'] = true;
  323. $GLOBALS['db_to_create'] = '';
  324. $GLOBALS['dbs_where_create_table_allowed'] = ['*'];
  325. $GLOBALS['dbs_to_test'] = false;
  326. $GLOBALS['db_priv'] = true;
  327. $GLOBALS['col_priv'] = true;
  328. $GLOBALS['table_priv'] = true;
  329. $GLOBALS['proc_priv'] = true;
  330. } else {
  331. $this->analyseShowGrant();
  332. }
  333. }
  334. }