mysqli_auth_pam.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. --TEST--
  2. PAM auth plugin
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('connect.inc');
  8. if (!$link = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
  9. die(sprintf("SKIP Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  10. $host, $user, $db, $port, $socket));
  11. }
  12. if ($link->server_version < 50500)
  13. die(sprintf("SKIP Needs MySQL 5.5 or newer, found MySQL %s\n", $link->server_info));
  14. if (!$res = $link->query("SHOW PLUGINS"))
  15. die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
  16. $have_pam = false;
  17. while ($row = $res->fetch_assoc()) {
  18. if (isset($row['Name']) && in_array($row['Name'], array('pam', 'authentication_pam', 'auth_pam_compat'))) {
  19. $have_pam = $row['Name'];
  20. break;
  21. }
  22. }
  23. $res->close();
  24. if (!$have_pam)
  25. die("SKIP Server PAM plugin not installed");
  26. if ($have_pam == 'pam') {
  27. /* MariaDB - needs system variable pam_use_cleartext_plugin=ON to be set */
  28. if (!$res = mysqli_query($link, 'SHOW GLOBAL VARIABLES LIKE "pam_use_cleartext_plugin"'))
  29. die(sprintf("SKIP MariaDB probe of GLOBAL VARIABLES failed [%d] %s\n",
  30. mysqli_errno($link), mysqli_error($link)));
  31. $pam_use_cleartext_plugin = mysqli_fetch_row($res);
  32. mysqli_free_result($res);
  33. if (!$pam_use_cleartext_plugin or $pam_use_cleartext_plugin[1]!='ON')
  34. die("SKIP Server setting pam_use_cleartext_plugin!=ON");
  35. $pam_service = file_get_contents('/etc/pam.d/mysql');
  36. } elseif ($have_pam == 'authentication_pam') {
  37. /*
  38. required MySQL syntax:
  39. https://dev.mysql.com/doc/refman/8.0/en/pam-pluggable-authentication.html#pam-pluggable-authentication-usage
  40. */
  41. $have_pam .= " AS 'mysql-unix'";
  42. $pam_service = file_get_contents('/etc/pam.d/mysql-unix');
  43. } else {
  44. $pam_service = file_get_contents('/etc/pam.d/mysql');
  45. }
  46. $auth = 0;
  47. $account = 0;
  48. foreach (explode("\n", $pam_service) as $line)
  49. {
  50. if (preg_match('/^auth/', $line)) {
  51. $auth = 1;
  52. } elseif (preg_match('/^account/', $line)) {
  53. $account = 1;
  54. }
  55. }
  56. if (!$auth) {
  57. die("SKIP pam service file missing 'auth' directive");
  58. }
  59. if (!$account) {
  60. die("SKIP pam service file missing 'account' directive");
  61. }
  62. if (!posix_getpwnam('pamtest')) {
  63. die("SKIP no pamtest user");
  64. }
  65. /* Password of user 'pamtest' should be set to 'pamtest' */
  66. mysqli_query($link, 'DROP USER pamtest');
  67. mysqli_query($link, 'DROP USER pamtest@localhost');
  68. if (!mysqli_query($link, "CREATE USER pamtest@'%' IDENTIFIED WITH $have_pam") ||
  69. !mysqli_query($link, "CREATE USER pamtest@'localhost' IDENTIFIED WITH $have_pam")) {
  70. printf("skip Cannot create second DB user [%d] %s", mysqli_errno($link), mysqli_error($link));
  71. mysqli_close($link);
  72. die("skip CREATE USER failed");
  73. }
  74. if (!$link->query("CREATE TABLE test (id INT)") || !$link->query("INSERT INTO test(id) VALUES (1)"))
  75. die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
  76. if (!mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO pamtest@'%%'", $db)) ||
  77. !mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO pamtest@'localhost'", $db))) {
  78. printf("skip Cannot grant SELECT to user [%d] %s", mysqli_errno($link), mysqli_error($link));
  79. mysqli_close($link);
  80. die("skip GRANT failed");
  81. }
  82. ?>
  83. --INI--
  84. max_execution_time=240
  85. --FILE--
  86. <?php
  87. require_once('connect.inc');
  88. require_once('table.inc');
  89. if (!$link = my_mysqli_connect($host, 'pamtest', 'pamtest', $db, $port, $socket)) {
  90. printf("[001] Cannot connect to the server using host=%s, user=pamtest, passwd=pamtest dbname=%s, port=%s, socket=%s\n",
  91. $host, $db, $port, $socket);
  92. } else {
  93. if (!$res = $link->query("SELECT id FROM test WHERE id = 1"))
  94. printf("[002] [%d] %s\n", $link->errno, $link->error);
  95. if (!$row = mysqli_fetch_assoc($res)) {
  96. printf("[003] [%d] %s\n", $link->errno, $link->error);
  97. }
  98. if ($row['id'] != 1) {
  99. printf("[004] Expecting 1 got %s/'%s'", gettype($row['id']), $row['id']);
  100. }
  101. $res->close();
  102. $link->close();
  103. }
  104. print "done!";
  105. ?>
  106. --CLEAN--
  107. <?php
  108. require_once("clean_table.inc");
  109. mysqli_query($link, 'DROP USER pamtest');
  110. mysqli_query($link, 'DROP USER pamtest@localhost');
  111. ?>
  112. --EXPECT--
  113. done!