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