mysqli_pam_sha256.phpt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --TEST--
  2. PAM: SHA-256
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. ob_start();
  9. phpinfo(INFO_MODULES);
  10. $tmp = ob_get_contents();
  11. ob_end_clean();
  12. if (!stristr($tmp, "auth_plugin_sha256_password"))
  13. die("skip SHA256 auth plugin not built-in to mysqlnd");
  14. require_once('connect.inc');
  15. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  16. die(printf("skip: [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
  17. if (mysqli_get_server_version($link) < 50606)
  18. die("skip: SHA-256 requires MySQL 5.6.6+");
  19. if (!($res = $link->query("SHOW PLUGINS"))) {
  20. die(sprintf("skip [%d] %s\n", $link->errno, $link->error));
  21. }
  22. $found = false;
  23. while ($row = $res->fetch_assoc()) {
  24. if (($row['Name'] == 'sha256_password') && ($row['Status'] == 'ACTIVE')) {
  25. $found = true;
  26. break;
  27. }
  28. }
  29. if (!$found)
  30. die("skip SHA-256 server plugin unavailable");
  31. if (!($res = $link->query("SHOW STATUS LIKE 'Rsa_public_key'"))) {
  32. die(sprintf("skip [%d] %s\n", $link->errno, $link->error));
  33. }
  34. if (!($row = $res->fetch_assoc())) {
  35. die(sprintf("skip Failed to check RSA pub key, [%d] %s\n", $link->errno, $link->error));
  36. }
  37. if (strlen($row['Value']) < 100) {
  38. die(sprintf("skip Server misconfiguration? RSA pub key is suspicious, [%d] %s\n", $link->errno, $link->error));
  39. }
  40. // Ignore errors because this variable exists only in MySQL 5.6 and 5.7
  41. $link->query("SET @@session.old_passwords=2");
  42. $link->query('DROP USER shatest');
  43. $link->query("DROP USER shatest@localhost");
  44. if (!$link->query('CREATE USER shatest@"%" IDENTIFIED WITH sha256_password') ||
  45. !$link->query('CREATE USER shatest@"localhost" IDENTIFIED WITH sha256_password')) {
  46. die(sprintf("skip CREATE USER failed [%d] %s", $link->errno, $link->error));
  47. }
  48. if (!$link->query('SET PASSWORD FOR shatest@"%" = "shatest"') ||
  49. !$link->query('SET PASSWORD FOR shatest@"localhost" = "shatest"')) {
  50. die(sprintf("skip SET PASSWORD failed [%d] %s", $link->errno, $link->error));
  51. }
  52. if (!$link->query("DROP TABLE IF EXISTS test") ||
  53. !$link->query("CREATE TABLE test (id INT)") ||
  54. !$link->query("INSERT INTO test(id) VALUES (1), (2), (3)"))
  55. die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
  56. if (!$link->query(sprintf("GRANT SELECT ON TABLE %s.test TO shatest@'%%'", $db)) ||
  57. !$link->query(sprintf("GRANT SELECT ON TABLE %s.test TO shatest@'localhost'", $db))) {
  58. die(sprintf("skip Cannot grant SELECT to user [%d] %s", mysqli_errno($link), mysqli_error($link)));
  59. }
  60. $link->close();
  61. echo "nocache";
  62. ?>
  63. --FILE--
  64. <?php
  65. require_once("connect.inc");
  66. if (!$link = my_mysqli_connect($host, 'shatest', 'shatest', $db, $port, $socket)) {
  67. printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  68. $host, "shatest", $db, $port, $socket);
  69. } else {
  70. if (!$res = $link->query("SELECT id FROM test WHERE id = 1"))
  71. printf("[002] [%d] %s\n", $link->errno, $link->error);
  72. if (!$row = mysqli_fetch_assoc($res)) {
  73. printf("[003] [%d] %s\n", $link->errno, $link->error);
  74. }
  75. if ($row['id'] != 1) {
  76. printf("[004] Expecting 1 got %s/'%s'", gettype($row['id']), $row['id']);
  77. }
  78. $res->close();
  79. $link->close();
  80. }
  81. print "done!";
  82. ?>
  83. --CLEAN--
  84. <?php
  85. require_once("clean_table.inc");
  86. $link->query('DROP USER shatest');
  87. $link->query('DROP USER shatest@localhost');
  88. ?>
  89. --EXPECT--
  90. done!