1--TEST--
2PAM auth plugin
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
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 < 50500)
15	die(sprintf("SKIP Needs MySQL 5.5 or newer, found MySQL %s\n", $link->server_info));
16
17if (!$res = $link->query("SHOW PLUGINS"))
18	die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
19
20$have_pam = false;
21while ($row = $res->fetch_assoc()) {
22	if (isset($row['Name']) && ('mysql_clear_password' == $row['Name'])) {
23		$have_pam = true;
24		break;
25	}
26}
27$res->close();
28
29if (!$have_pam)
30  die("SKIP Server PAM plugin not installed");
31
32
33mysqli_query($link, 'DROP USER pamtest');
34mysqli_query($link, 'DROP USER pamtest@localhost');
35
36if (!mysqli_query($link, 'CREATE USER pamtest@"%" IDENTIFIED WITH mysql_clear_password') ||
37	!mysqli_query($link, 'CREATE USER pamtest@"localhost" IDENTIFIED WITH mysql_clear_password')) {
38	printf("skip Cannot create second DB user [%d] %s", mysqli_errno($link), mysqli_error($link));
39	mysqli_close($link);
40	die("skip CREATE USER failed");
41}
42
43if (!$link->query("CREATE TABLE test (id INT)") || !$link->query("INSERT INTO test(id) VALUES (1)"))
44	die(sprintf("SKIP [%d] %s\n", $link->errno, $link->error));
45
46
47
48if (!mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO pamtest@'%%'", $db)) ||
49	!mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO pamtest@'localhost'", $db))) {
50	printf("skip Cannot grant SELECT to user [%d] %s", mysqli_errno($link), mysqli_error($link));
51	mysqli_close($link);
52	die("skip GRANT failed");
53}
54?>
55--INI--
56max_execution_time=240
57--FILE--
58<?php
59	require_once('connect.inc');
60	require_once('table.inc');
61
62	if (!$link = my_mysqli_connect($host, 'pamtest', 'pamtest', $db, $port, $socket)) {
63		printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
64			$host, $user, $db, $port, $socket);
65	} else {
66
67	  if (!$res = $link->query("SELECT id FROM test WHERE id = 1"))
68		  printf("[002] [%d] %s\n", $link->errno, $link->error);
69
70	  if (!$row = mysqli_fetch_assoc($res)) {
71		  printf("[003] [%d] %s\n", $link->errno, $link->error);
72	  }
73
74	  if ($row['id'] != 1) {
75		  printf("[004] Expecting 1 got %s/'%s'", gettype($row['id']), $row['id']);
76	  }
77
78	  $res->close();
79	  $link->close();
80	}
81
82	print "done!";
83?>
84--CLEAN--
85<?php
86	require_once("clean_table.inc");
87	mysqli_query($link, 'DROP USER pamtest');
88	mysqli_query($link, 'DROP USER pamtest@localhost');
89?>
90--EXPECTF--
91Warning: mysqli_real_connect(): (28000/1045): Access denied for user %s
92[001] Cannot connect to the server using host=%s
93done!
94