CheckUserPrivileges.php 12 KB

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