Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / user_password.php
blobdf8cd8b3eb45ca8daded3430c8a087810fe16daf
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * displays and handles the form where the user can change his password
5 * linked from index.php
7 * @package PhpMyAdmin
8 */
10 /**
11 * Gets some core libraries
13 require_once './libraries/common.inc.php';
15 /**
16 * Libraries needed for some functions
18 require_once './libraries/server_privileges.lib.php';
20 $response = PMA\libraries\Response::getInstance();
21 $header = $response->getHeader();
22 $scripts = $header->getScripts();
23 $scripts->addFile('server_privileges.js');
25 /**
26 * Displays an error message and exits if the user isn't allowed to use this
27 * script
29 if (! $GLOBALS['cfg']['ShowChgPassword']) {
30 $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
32 if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
33 PMA\libraries\Message::error(
34 __('You don\'t have sufficient privileges to be here right now!')
35 )->display();
36 exit;
37 } // end if
39 /**
40 * If the "change password" form has been submitted, checks for valid values
41 * and submit the query or logout
43 if (isset($_REQUEST['nopass'])) {
44 if ($_REQUEST['nopass'] == '1') {
45 $password = '';
46 } else {
47 $password = $_REQUEST['pma_pw'];
49 $change_password_message = PMA_setChangePasswordMsg();
50 $msg = $change_password_message['msg'];
51 if (! $change_password_message['error']) {
52 PMA_changePassword($password, $msg, $change_password_message);
53 } else {
54 PMA_getChangePassMessage($change_password_message);
58 /**
59 * If the "change password" form hasn't been submitted or the values submitted
60 * aren't valid -> displays the form
63 // Displays an error message if required
64 if (isset($msg)) {
65 $msg->display();
66 unset($msg);
69 require_once './libraries/display_change_password.lib.php';
71 echo PMA_getHtmlForChangePassword('change_pw', $username, $hostname);
72 exit;
74 /**
75 * Send the message as an ajax request
77 * @param array $change_password_message Message to display
78 * @param string $sql_query SQL query executed
80 * @return void
82 function PMA_getChangePassMessage($change_password_message, $sql_query = '')
84 if ($GLOBALS['is_ajax_request'] == true) {
85 /**
86 * If in an Ajax request, we don't need to show the rest of the page
88 $response = PMA\libraries\Response::getInstance();
89 if ($change_password_message['error']) {
90 $response->addJSON('message', $change_password_message['msg']);
91 $response->setRequestStatus(false);
92 } else {
93 $sql_query = PMA\libraries\Util::getMessage(
94 $change_password_message['msg'],
95 $sql_query,
96 'success'
98 $response->addJSON('message', $sql_query);
100 exit;
105 * Generate the message
107 * @return array error value and message
109 function PMA_setChangePasswordMsg()
111 $error = false;
112 $message = PMA\libraries\Message::success(__('The profile has been updated.'));
114 if (($_REQUEST['nopass'] != '1')) {
115 if (empty($_REQUEST['pma_pw']) || empty($_REQUEST['pma_pw2'])) {
116 $message = PMA\libraries\Message::error(__('The password is empty!'));
117 $error = true;
118 } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
119 $message = PMA\libraries\Message::error(
120 __('The passwords aren\'t the same!')
122 $error = true;
125 return array('error' => $error, 'msg' => $message);
129 * Change the password
131 * @param string $password New password
132 * @param string $message Message
133 * @param array $change_password_message Message to show
135 * @return void
137 function PMA_changePassword($password, $message, $change_password_message)
139 global $auth_plugin;
141 $hashing_function = PMA_changePassHashingFunction();
143 $row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user');
144 $curr_user = $row['user'];
145 list($username, $hostname) = explode('@', $curr_user);
147 $serverType = PMA\libraries\Util::getServerType();
149 if (isset($_REQUEST['authentication_plugin'])
150 && ! empty($_REQUEST['authentication_plugin'])
152 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
153 } else {
154 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
155 'change', $username, $hostname
159 $sql_query = 'SET password = '
160 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
162 if ($serverType == 'MySQL'
163 && PMA_MYSQL_INT_VERSION >= 50706
165 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
166 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
167 . (($password == '') ? '\'\'' : '\'***\'');
168 } else if (($serverType == 'MySQL'
169 && PMA_MYSQL_INT_VERSION >= 50507)
170 || ($serverType == 'MariaDB'
171 && PMA_MYSQL_INT_VERSION >= 50200)
173 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
174 // explicitly set value of `old_passwords` so that
175 // it does not give an error while using
176 // the PASSWORD() function
177 if ($orig_auth_plugin == 'sha256_password') {
178 $value = 2;
179 } else {
180 $value = 0;
182 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
185 PMA_changePassUrlParamsAndSubmitQuery(
186 $username, $hostname, $password,
187 $sql_query, $hashing_function, $orig_auth_plugin
190 $auth_plugin->handlePasswordChange($password);
191 PMA_getChangePassMessage($change_password_message, $sql_query);
192 PMA_changePassDisplayPage($message, $sql_query);
196 * Generate the hashing function
198 * @return string $hashing_function
200 function PMA_changePassHashingFunction()
202 if (PMA_isValid(
203 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
204 )) {
205 $hashing_function = 'OLD_PASSWORD';
206 } else {
207 $hashing_function = 'PASSWORD';
209 return $hashing_function;
213 * Generate the error url and submit the query
215 * @param string $username Username
216 * @param string $hostname Hostname
217 * @param string $password Password
218 * @param string $sql_query SQL query
219 * @param string $hashing_function Hashing function
220 * @param string $orig_auth_plugin Original Authentication Plugin
222 * @return void
224 function PMA_changePassUrlParamsAndSubmitQuery(
225 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
227 $err_url = 'user_password.php' . PMA_URL_getCommon();
229 $serverType = PMA\libraries\Util::getServerType();
231 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
232 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
233 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
234 . (($password == '')
235 ? '\'\''
236 : '\'' . PMA\libraries\Util::sqlAddSlashes($password) . '\'');
237 } else if ($serverType == 'MariaDB'
238 && PMA_MYSQL_INT_VERSION >= 50200
240 if ($orig_auth_plugin == 'mysql_native_password') {
241 // Set the hashing method used by PASSWORD()
242 // to be 'mysql_native_password' type
243 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
244 } else if ($orig_auth_plugin == 'sha256_password') {
245 // Set the hashing method used by PASSWORD()
246 // to be 'sha256_password' type
247 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
250 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
252 $local_query = "UPDATE `mysql`.`user` SET"
253 . " `authentication_string` = '" . $hashedPassword
254 . "', `Password` = '', "
255 . " `plugin` = '" . $orig_auth_plugin . "'"
256 . " WHERE `User` = '" . $username . "' AND Host = '"
257 . $hostname . "';";
259 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
260 } else {
261 $local_query = 'SET password = ' . (($password == '')
262 ? '\'\''
263 : $hashing_function . '(\''
264 . PMA\libraries\Util::sqlAddSlashes($password) . '\')');
266 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
267 PMA\libraries\Util::mysqlDie(
268 $GLOBALS['dbi']->getError(),
269 $sql_query,
270 false,
271 $err_url
277 * Display the page
279 * @param string $message Message
280 * @param string $sql_query SQL query
282 * @return void
284 function PMA_changePassDisplayPage($message, $sql_query)
286 echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
287 echo PMA\libraries\Util::getMessage(
288 $message, $sql_query, 'success'
290 echo '<a href="index.php' , PMA_URL_getCommon()
291 , ' target="_parent">' , "\n"
292 , '<strong>' , __('Back') , '</strong></a>';
293 exit;