1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
<?php
/**
*
* MySQL authentication module. Uses MySQL capability to store or
* retrieve user credentials. Called from API when authentication
* functions are requested.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author Filippo Callegari (callegari.filippo@gmail.com)
* @version $Id:1.0 MySQL.php 2015-03-10 23:49:11 Tio Igor
* @package phpVirtualBox
*
*/
/**
struct of db:
CREATE TABLE users(
username VARCHAR(20) PRIMARY KEY,
password VARCHAR(40),
admin ENUM('0','1') DEFAULT '0'
)ENGINE=InnoDB;
INSERT INTO users(username,password,admin)
VALUES("admin","$1$65CMtT1M$GSDBTEZ6o5Web.4cDL6rz1",'1'); /*user:admin, pass:admin*/
*/
class phpvbAuthMySQL implements phpvbAuth
{
var $capabilities = array(
'canChangePassword' => true,
'canModifyUsers' => true,
'canLogout' => true
);
/**
*
* Connect to MySQL DB.
* return PDOconnection.
*/
function newPDO()
{
$host="127.0.0.1";
$port=3306;
$user="MySQLuser";
$pass="MySQLpassword";
$db="vboxDB";
try{
return new PDO("mysql:host=$host;port=$port;dbname=$db;charset=utf8",$user,$pass);
}catch (PDOException $e){throw new Exception("Can't connect to MySQL db!",vboxconnector::PHPVB_ERRNO_CONNECT);}
}
/**
*
* Select row from username
* @param $username the user we search
* return row
*/
function PDO_selectUser($username)
{
try{
$statement=$this->newPDO()->prepare("SELECT username, password, admin FROM users WHERE username=:username");
$statement->bindValue(":username",$username, PDO::PARAM_STR);
$statement->execute();
}catch(PDOException $e){throw new Exception("Can't execute requested query!",vboxconnector::PHPVB_ERRNO_FATAL);}
return $statement->fetch(PDO::FETCH_ASSOC);
}
/**
*
* Generate a random salt.
* @param $lenght the lenght of the salt, default is 8.
*
* On Linux (in particular Ubuntu), the password is generate with this command:
* "echo "${username}:${password}" | chpasswd -S -c $crypt_method | cut -d: -f2".
* in this particoular implementation I use MD5.
*
* Max length is 20 char!
*/
function generateRandomSalt($length = 8)
{
return substr(sha1(rand().time()), rand(0,20-$length), $length);
}
/**
*
* Revalidate login info and set authCheckHeartbeat session variable.
* @param vboxconnector $vbox vboxconnector object instance, THIS VARIABLE WILL NOT USED.
*/
function heartbeat($vbox)
{
global $_SESSION;
$q=$this->PDO_selectUser(@$_SESSION['user']);
$p=isset($q['password'])?$q['password']:0;
if($p && $p!=@$_SESSION[uHash])
{
$_SESSION['valid']=false;
session_destroy();
}
else
{
$_SESSION['admin']=intval(q['admin']);
$_SESSION['authCheckHeartbeat']=time();
}
if(!isset($_SESSION['valid']) || !$_SESSION['valid'])
throw new Exception(trans('Not logged in.','UIUsers'), vboxconnector::PHPVB_ERRNO_FATAL);
}
/**
*
* Log in function. Populates $_SESSION
* @param string $username user name
* @param string $password password
*/
function login($username, $password)
{
global $_SESSION;
$q=$this->PDO_selectUser($username);
$p=isset($q['password'])?$q['password']:0;
if($p && password_verify($password,$p))
{
$_SESSION['valid'] = true;
$_SESSION['user'] = $username;
$_SESSION['admin'] = intval($q['admin']);
$_SESSION['authCheckHeartbeat'] = time();
$_SESSION['uHash'] = $p;
}
}
/**
*
* Log out user present in $_SESSION
* @param array $response response passed byref by API and populated within function
*/
function logout(&$response)
{
global $_SESSION;
if(function_exists('session_destroy')) session_destroy();
else unset($_SESSION['valid']);
$response['data']['result'] = 1;
}
/**
*
* Change password function.
* @param string $old old password
* @param string $new new password
* @return boolean true on success
*/
function changePassword($old, $new)
{
global $_SESSION;
$p=$this->PDO_selectUser($_SESSION['user']);
$p=isset($p['password'])?$p['password']:0; //along the time is changed?
if($p && password_verify($old, $p))
{
$np=crypt($new, '$1$'.$this->generateRandomSalt().'$'); //look the MD5 format!!
//look here for more info: http://php.net/manual/en/faq.passwords.php
try{
$sth=$this->newPDO()->prepare("UPDATE users SET password=:password WHERE username=:username");
$sth->bindValue(":password",$np,PDO::PARAM_STR);
$sth->bindValue(":username",$_SESSION['user'],PDO::PARAM_STR);
$sth->execute();
}catch(PDOException $e){throw new Exception("Can't execute requested query!",vboxconnector::PHPVB_ERRNO_FATAL);}
return true;
}
return false;
}
/**
*
* Return a list of users
* @return array list of users
*/
function listUsers()
{
$response = array();
try{
$sth=$this->newPDO()->prepare("SELECT * FROM users");
$sth->execute();
}catch(PDOException $e){throw new Exception("Can't display users list!",vboxconnector::PHPVB_ERRNO_FATAL);}
while(($row=$sth->fetch(PDO::FETCH_ASSOC))!==FALSE)
{
$response[$row['username']]=array('username'=> $row['username'], 'admin'=> intval($row['admin']));
}
return $response;
}
/**
*
* Update user information such as password and admin status
* @param array $vboxRequest request passed from API representing the ajax request. Contains user, password and administration level.
* @param boolean $skipExistCheck Do not check that the user exists first. Essentially, if this is set and the user does not exist, it is added.
*/
function updateUser($vboxRequest, $skipExistCheck)
{
global $_SESSION;
if(!$_SESSION['admin']) return;
$q=$this->PDO_selectUser($vboxRequest['u']);
if(!$skipExistCheck && $q) return;
$np=($vboxRequest['p'])?crypt($vboxRequest['p'], '$1$'.$this->generateRandomSalt().'$'):0;
$query="INSERT INTO `users`(`username`, `password`,`admin`)
VALUES (:username, :password, :admin)
ON DUPLICATE KEY UPDATE `password`=:password, `admin`=:admin";
$sth=$this->newPDO()->prepare($query);
try{
$sth->bindValue(":username",$vboxRequest['u'],PDO::PARAM_STR);
$sth->bindValue(":password",($vboxRequest['p']?$np:$q['password']),PDO::PARAM_STR);
$sth->bindValue(":admin",($vboxRequest['a']?"1":"0"),PDO::PARAM_STR);
$sth->execute();
}catch(PDOException $e){throw new Exception("Can't execute requested query!",vboxconnector::PHPVB_ERRNO_FATAL);}
}
/**
*
* Remove the user $user
* @param string $user Username to remove
*/
function deleteUser($user)
{
$sth=$this->newPDO()->prepare("DELETE FROM users WHERE username=:username");
try{
$sth->bindValue(":username",$user,PDO::PARAM_STR);
$sth->execute();
}catch(PDOException $e){throw new Exception("Can't execute requested query!",vboxconnector::PHPVB_ERRNO_FATAL);}
}
}
|