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