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