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