1--TEST--
2MySQL 5.6 / MariaDB 10.4.3 EXPIRE PASSWORD protocol change
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 < 50610)
14    die(sprintf("SKIP Needs MySQL 5.6.10 or newer, found MySQL %s\n", $link->server_info));
15
16if ($link->server_version >= 100000) {
17    if ($link->server_version < 100403)
18        die(sprintf("SKIP Needs MariaDB 10.4.3 or newer, found MariaDB %s\n", $link->server_info));
19    $result = $link->query("select @@disconnect_on_expired_password");
20    if (!$result)
21        die("SKIP Failed to query MariaDB @@disconnect_on_expired_password value");
22    $row = mysqli_fetch_row($result);
23    if ($row[0] == 0)
24        die("SKIP Cannot run in MariaDB @@disconnect_on_expired_password=OFF state");
25
26}
27
28if  (!$IS_MYSQLND && (mysqli_get_client_version() < 50610)) {
29    die(sprintf("SKIP Needs libmysql 5.6.10 or newer, found  %s\n", mysqli_get_client_version()));
30}
31
32mysqli_query($link, 'DROP USER expiretest');
33mysqli_query($link, 'DROP USER expiretest@localhost');
34
35if (!mysqli_query($link, 'CREATE USER expiretest IDENTIFIED BY \'expiredpassword\'') ||
36    !mysqli_query($link, 'CREATE USER expiretest@localhost IDENTIFIED BY \'expiredpassword\'')) {
37    printf("skip Cannot create second DB user [%d] %s", mysqli_errno($link), mysqli_error($link));
38    mysqli_close($link);
39    die("skip CREATE USER failed");
40}
41
42if (!mysqli_query($link, 'ALTER USER expiretest PASSWORD EXPIRE') ||
43    !mysqli_query($link, 'ALTER USER expiretest@localhost PASSWORD EXPIRE')) {
44    printf("skip Cannot modify second DB user [%d] %s", mysqli_errno($link), mysqli_error($link));
45    mysqli_close($link);
46    die("skip ALTER USER failed");
47}
48
49if (!$link->query("DROP TABLE IF EXISTS test") ||
50    !$link->query("CREATE TABLE test (id INT)") || !$link->query("INSERT INTO test(id) VALUES (1)"))
51    die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
52
53
54
55if (!mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO expiretest@'%%'", $db)) ||
56    !mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO expiretest@'localhost'", $db))) {
57    printf("skip Cannot grant SELECT to user [%d] %s", mysqli_errno($link), mysqli_error($link));
58    mysqli_close($link);
59    die("skip GRANT failed");
60}
61?>
62--FILE--
63<?php
64    require_once('connect.inc');
65    require_once('table.inc');
66
67    /* default */
68    if (!$link = my_mysqli_connect($host, 'expiretest', 'expiredpassword', $db, $port, $socket)) {
69        printf("[001] Cannot connect [%d] %s\n",
70            mysqli_connect_errno(), mysqli_connect_error());
71    } else {
72        $link->query("SELECT id FROM test WHERE id = 1");
73        printf("[002] Connect should fail, [%d] %s\n", $link->errno, $link->error);
74    }
75
76    /* explicitly requesting default */
77    $link = mysqli_init();
78    $link->options(MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 0);
79    if (!my_mysqli_real_connect($link, $host, 'expiretest', 'expiredpassword', $db, $port, $socket)) {
80        printf("[003] Cannot connect [%d] %s\n",
81            mysqli_connect_errno(), mysqli_connect_error());
82    } else {
83        $link->query("SELECT id FROM test WHERE id = 1");
84        printf("[004] Connect should fail, [%d] %s\n", $link->errno, $link->error);
85    }
86
87    /* allow connect */
88    $link = mysqli_init();
89    $link->options(MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 1);
90    if (!my_mysqli_real_connect($link, $host, 'expiretest', 'expiredpassword', $db, $port, $socket)) {
91        printf("[005] Cannot connect [%d] %s\n",
92            mysqli_connect_errno(), mysqli_connect_error());
93    } else {
94        $link->query("SELECT id FROM test WHERE id = 1");
95        printf("[006] Connect allowed, query fail, [%d] %s\n", $link->errno, $link->error);
96        $link->close();
97    }
98
99    /* allow connect, fix pw */
100    $link = mysqli_init();
101    $link->options(MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 1);
102    if (!my_mysqli_real_connect($link, $host, 'expiretest', 'expiredpassword', $db, $port, $socket)) {
103        printf("[007] Cannot connect [%d] %s\n",
104            mysqli_connect_errno(), mysqli_connect_error());
105    } else {
106        if (!$link->query("SET PASSWORD='expiretest'")) {
107            $link->query("SET PASSWORD=PASSWORD('expiretest')");
108        }
109        printf("[008] Connect allowed, pw set, [%d] %s\n", $link->errno, $link->error);
110        if ($res = $link->query("SELECT id FROM test WHERE id = 1"))
111            var_dump($res->fetch_assoc());
112        $link->close();
113    }
114
115
116    /* check login */
117    if (!$link = my_mysqli_connect($host, 'expiretest', "expiretest", $db, $port, $socket)) {
118        printf("[001] Cannot connect [%d] %s\n",
119            mysqli_connect_errno(), mysqli_connect_error());
120    } else {
121        $link->query("SELECT id FROM test WHERE id = 1");
122        if ($res = $link->query("SELECT id FROM test WHERE id = 1"))
123            var_dump($res->fetch_assoc());
124        $link->close();
125    }
126
127
128
129    print "done!";
130?>
131--CLEAN--
132<?php
133    require_once("clean_table.inc");
134    mysqli_query($link, 'DROP USER expiretest');
135    mysqli_query($link, 'DROP USER expiretest@localhost');
136?>
137--EXPECTF--
138Warning: mysqli%sconnect(): (HY000/%d): %s in %s on line %d
139[001] Cannot connect [%d] %s
140
141Warning: mysqli%sconnect(): (HY000/%d): %s in %s on line %d
142[003] Cannot connect [%d] %s
143[006] Connect allowed, query fail, [1820] %s
144[008] Connect allowed, pw set, [0%A
145array(1) {
146  ["id"]=>
147  string(1) "1"
148}
149array(1) {
150  ["id"]=>
151  string(1) "1"
152}
153done!
154