| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Id: m_mysql.php,v 1.35 2005/12/18 09:51:32 benjamin Exp $ |
|---|
| 4 | ---------------------------------------------------------------------- |
|---|
| 5 | AlternC - Web Hosting System |
|---|
| 6 | Copyright (C) 2002 by the AlternC Development Team. |
|---|
| 7 | http://alternc.org/ |
|---|
| 8 | ---------------------------------------------------------------------- |
|---|
| 9 | Based on: |
|---|
| 10 | Valentin Lacambre's web hosting softwares: http://altern.org/ |
|---|
| 11 | ---------------------------------------------------------------------- |
|---|
| 12 | LICENSE |
|---|
| 13 | |
|---|
| 14 | This program is free software; you can redistribute it and/or |
|---|
| 15 | modify it under the terms of the GNU General Public License (GPL) |
|---|
| 16 | as published by the Free Software Foundation; either version 2 |
|---|
| 17 | of the License, or (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | This program is distributed in the hope that it will be useful, |
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | GNU General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | To read the license please visit http://www.gnu.org/copyleft/gpl.html |
|---|
| 25 | ---------------------------------------------------------------------- |
|---|
| 26 | Original Author of file: Benjamin Sonntag |
|---|
| 27 | Purpose of file: Manage mysql database for users. |
|---|
| 28 | ---------------------------------------------------------------------- |
|---|
| 29 | */ |
|---|
| 30 | /** |
|---|
| 31 | * MySQL user database management for AlternC. |
|---|
| 32 | * This class manage user's databases in MySQL, and user's MySQL accounts. |
|---|
| 33 | * |
|---|
| 34 | * @copyright AlternC-Team 2002-2005 http://alternc.org/ |
|---|
| 35 | */ |
|---|
| 36 | class m_mysql { |
|---|
| 37 | |
|---|
| 38 | var $server; |
|---|
| 39 | var $client; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /*---------------------------------------------------------------------------*/ |
|---|
| 43 | /** Constructor |
|---|
| 44 | * m_mysql([$mid]) Constructeur de la classe m_mysql, initialise le membre concerne |
|---|
| 45 | */ |
|---|
| 46 | function m_mysql() { |
|---|
| 47 | $this->server = $GLOBALS['L_MYSQL_HOST']; |
|---|
| 48 | $this->client = $GLOBALS['L_MYSQL_CLIENT']; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /* ----------------------------------------------------------------- */ |
|---|
| 53 | /** Hook called by m_quota to obtain the quota managed by this class. |
|---|
| 54 | * Quota name |
|---|
| 55 | */ |
|---|
| 56 | function alternc_quota_names() { |
|---|
| 57 | return array("mysql","mysql_users"); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /* ----------------------------------------------------------------- */ |
|---|
| 62 | /** |
|---|
| 63 | * Password kind used in this class (hook for admin class) |
|---|
| 64 | */ |
|---|
| 65 | function alternc_password_policy() { |
|---|
| 66 | return array("mysql"=>"MySQL users"); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | /*---------------------------------------------------------------------------*/ |
|---|
| 71 | /** Get the list of the database for the current user. |
|---|
| 72 | * @return array returns an associative array as follow : <br> |
|---|
| 73 | * "db" => database name "bck" => backup mode for this db |
|---|
| 74 | * "dir" => Backup folder. |
|---|
| 75 | * Returns FALSE if the user has no database. |
|---|
| 76 | */ |
|---|
| 77 | function get_dblist() { |
|---|
| 78 | global $db,$err,$bro,$cuid; |
|---|
| 79 | $err->log("mysql","get_dblist"); |
|---|
| 80 | $db->query("SELECT login,pass,db, bck_mode, bck_dir FROM db WHERE uid='$cuid' ORDER BY db;"); |
|---|
| 81 | if (!$db->num_rows()) { |
|---|
| 82 | $err->raise("mysql",11); |
|---|
| 83 | return false; |
|---|
| 84 | } |
|---|
| 85 | $c=array(); |
|---|
| 86 | while ($db->next_record()) { |
|---|
| 87 | list($dbu,$dbn)=split_mysql_database_name($db->f("db")); |
|---|
| 88 | $c[]=array("db"=>$db->f("db"), "name"=>$dbn,"bck"=>$db->f("bck_mode"), "dir"=>$db->f("bck_dir"), "login"=>$db->f("login"), "pass"=>$db->f("pass")); |
|---|
| 89 | } |
|---|
| 90 | return $c; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /*---------------------------------------------------------------------------*/ |
|---|
| 95 | /** Returns the details of a user's database. |
|---|
| 96 | * $dbn is the name of the database (after the _) or nothing for the database "$user" |
|---|
| 97 | * @return array returns an associative array as follow : |
|---|
| 98 | * "db" => Name of the database |
|---|
| 99 | * "bck" => Current bckup mode |
|---|
| 100 | * "dir" => Backup directory |
|---|
| 101 | * "size" => Size of the database (in bytes) |
|---|
| 102 | * "pass" => Password of the user |
|---|
| 103 | * "history" => Number of backup we keep |
|---|
| 104 | * "gzip" => Does we compress the dumps ? |
|---|
| 105 | * Returns FALSE if the user has no database of if the database does not exist. |
|---|
| 106 | */ |
|---|
| 107 | function get_mysql_details($dbn) { |
|---|
| 108 | global $db,$err,$bro,$mem,$cuid; |
|---|
| 109 | $root="/var/alternc/html/".substr($mem->user["login"],0,1)."/".$mem->user["login"]; |
|---|
| 110 | $err->log("mysql","get_mysql_details"); |
|---|
| 111 | $dbname=$mem->user["login"].($dbn?"_":"").$dbn; |
|---|
| 112 | $size=$this->get_db_size($dbname); |
|---|
| 113 | $db->query("SELECT login,pass,db, bck_mode, bck_gzip, bck_dir, bck_history FROM db WHERE uid='$cuid' AND db='$dbname';"); |
|---|
| 114 | if (!$db->num_rows()) { |
|---|
| 115 | $err->raise("mysql",4); |
|---|
| 116 | return array("enabled"=>false); |
|---|
| 117 | } |
|---|
| 118 | $c=array(); |
|---|
| 119 | $db->next_record(); |
|---|
| 120 | list($dbu,$dbn)=split_mysql_database_name($db->f("db")); |
|---|
| 121 | return array("enabled"=>true,"login"=>$db->f("login"),"db"=>$db->f("db"), "name"=>$dbn,"bck"=>$db->f("bck_mode"), "dir"=>substr($db->f("bck_dir"),strlen($root)), "size"=>$size, "pass"=>$db->f("pass"), "history"=>$db->f("bck_history"), "gzip"=>$db->f("bck_gzip")); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /*---------------------------------------------------------------------------*/ |
|---|
| 126 | /** Create a new database for the current user. |
|---|
| 127 | * @param $dbn string Database name ($user_$dbn is the mysql db name) |
|---|
| 128 | * @return TRUE if the database $user_$db has been successfully created, or FALSE if |
|---|
| 129 | * an error occured, such as over quota user. |
|---|
| 130 | */ |
|---|
| 131 | function add_db($dbn) { |
|---|
| 132 | global $db,$err,$quota,$mem,$cuid; |
|---|
| 133 | $err->log("mysql","add_db",$dbn); |
|---|
| 134 | if (!$quota->cancreate("mysql")) { |
|---|
| 135 | $err->raise("mysql",1); |
|---|
| 136 | return false; |
|---|
| 137 | } |
|---|
| 138 | if (!ereg("^[0-9a-z]*$",$dbn)) { |
|---|
| 139 | $err->raise("mysql",2); |
|---|
| 140 | return false; |
|---|
| 141 | } |
|---|
| 142 | $dbname=$mem->user["login"].($dbn?"_":"").$dbn; |
|---|
| 143 | if (strlen($dbname) > 64) { |
|---|
| 144 | $err->raise("mysql",12); |
|---|
| 145 | return false; |
|---|
| 146 | } |
|---|
| 147 | $db->query("SELECT * FROM db WHERE db='$dbname';"); |
|---|
| 148 | if ($db->num_rows()) { |
|---|
| 149 | $err->raise("mysql",3); |
|---|
| 150 | return false; |
|---|
| 151 | } |
|---|
| 152 | // find the login/pass for this user : |
|---|
| 153 | $db->query("SELECT login,pass FROM db WHERE uid='$cuid' LIMIT 0,1;"); |
|---|
| 154 | if (!$db->num_rows()) { |
|---|
| 155 | $lo=$mem->user["login"]; |
|---|
| 156 | $pa=""; |
|---|
| 157 | } else { |
|---|
| 158 | $db->next_record(); |
|---|
| 159 | $lo=addslashes($db->f("login")); |
|---|
| 160 | $pa=addslashes($db->f("pass")); |
|---|
| 161 | } |
|---|
| 162 | if ($db->query("CREATE DATABASE `$dbname`;")) { |
|---|
| 163 | // Ok, database does not exist, quota is ok and dbname is compliant. Let's proceed |
|---|
| 164 | $db->query("INSERT INTO db (uid,login,pass,db,bck_mode) VALUES ('$cuid','$lo','$pa','$dbname',0);"); |
|---|
| 165 | // give everything but GRANT on db.* |
|---|
| 166 | // we assume there's already a user |
|---|
| 167 | $db->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$lo."'@'$this->client'"); |
|---|
| 168 | return true; |
|---|
| 169 | } else { |
|---|
| 170 | $err->raise("mysql",3); |
|---|
| 171 | return false; |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | /*---------------------------------------------------------------------------*/ |
|---|
| 177 | /** Delete a database for the current user. |
|---|
| 178 | * @param $dbn string Name of the database to delete. The db name is $user_$dbn |
|---|
| 179 | * @return TRUE if the database $user_$db has been successfully deleted, or FALSE if |
|---|
| 180 | * an error occured, such as db does not exist. |
|---|
| 181 | */ |
|---|
| 182 | function del_db($dbn) { |
|---|
| 183 | global $db,$err,$mem,$cuid; |
|---|
| 184 | $err->log("mysql","del_db",$dbn); |
|---|
| 185 | |
|---|
| 186 | $dbname=addslashes($mem->user["login"].($dbn?"_":"").$dbn); |
|---|
| 187 | $db->query("SELECT login FROM db WHERE db='$dbname';"); |
|---|
| 188 | if (!$db->num_rows()) { |
|---|
| 189 | $err->raise("mysql",4); |
|---|
| 190 | return false; |
|---|
| 191 | } |
|---|
| 192 | $db->next_record(); |
|---|
| 193 | $login=$db->f("login"); |
|---|
| 194 | |
|---|
| 195 | // Ok, database exists and dbname is compliant. Let's proceed |
|---|
| 196 | $db->query("DELETE FROM db WHERE uid='$cuid' AND db='$dbname';"); |
|---|
| 197 | $db->query("DROP DATABASE `$dbname`;"); |
|---|
| 198 | $db->query("SELECT COUNT(*) AS cnt FROM db WHERE uid='$cuid';"); |
|---|
| 199 | $db->next_record(); |
|---|
| 200 | $db->query("REVOKE ALL PRIVILEGES ON `".$dbname."`.* FROM '".$login."'@'$this->client'"); |
|---|
| 201 | if ($db->f("cnt")==0) { |
|---|
| 202 | $db->query("DELETE FROM mysql.user WHERE User='".$login."';"); |
|---|
| 203 | $db->query("FLUSH PRIVILEGES;"); |
|---|
| 204 | } |
|---|
| 205 | return true; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | /*---------------------------------------------------------------------------*/ |
|---|
| 210 | /** Set the backup parameters for the database $db |
|---|
| 211 | * @param $db string database name |
|---|
| 212 | * @param $bck_mode integer Backup mode (0 = none 1 = daily 2 = weekly) |
|---|
| 213 | * @param $bck_history integer How many backup should we keep ? |
|---|
| 214 | * @param $bck_gzip boolean shall we compress the backup ? |
|---|
| 215 | * @param $bck_dir string Directory relative to the user account where the backup will be stored |
|---|
| 216 | * @return boolean true if the backup parameters has been successfully changed, false if not. |
|---|
| 217 | */ |
|---|
| 218 | function put_mysql_backup($dbn,$bck_mode,$bck_history,$bck_gzip,$bck_dir) { |
|---|
| 219 | global $db,$err,$mem,$bro,$cuid; |
|---|
| 220 | $err->log("mysql","put_mysql_backup"); |
|---|
| 221 | if (!ereg("^[0-9a-z]*$",$dbn)) { |
|---|
| 222 | $err->raise("mysql",2); |
|---|
| 223 | return false; |
|---|
| 224 | } |
|---|
| 225 | $dbname=$mem->user["login"].($dbn?"_":"").$dbn; |
|---|
| 226 | $db->query("SELECT * FROM db WHERE uid='$cuid' AND db='$dbname';"); |
|---|
| 227 | if (!$db->num_rows()) { |
|---|
| 228 | $err->raise("mysql",4); |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | $db->next_record(); |
|---|
| 232 | $bck_mode=intval($bck_mode); |
|---|
| 233 | $bck_history=intval($bck_history); |
|---|
| 234 | if ($bck_gzip) |
|---|
| 235 | $bck_gzip="1"; |
|---|
| 236 | else |
|---|
| 237 | $bck_gzip="0"; |
|---|
| 238 | if (!$bck_mode) |
|---|
| 239 | $bck_mode="0"; |
|---|
| 240 | if (!$bck_history) { |
|---|
| 241 | $err->raise("mysql",5); |
|---|
| 242 | return false; |
|---|
| 243 | } |
|---|
| 244 | if (($bck_dir=$bro->convertabsolute($bck_dir,0))===false) { // return a full path or FALSE |
|---|
| 245 | $err->raise("mysql",6); |
|---|
| 246 | return false; |
|---|
| 247 | } |
|---|
| 248 | $db->query("UPDATE db SET bck_mode='$bck_mode', bck_history='$bck_history', bck_gzip='$bck_gzip', bck_dir='$bck_dir' WHERE uid='$cuid' AND db='$dbname';"); |
|---|
| 249 | return true; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | /*---------------------------------------------------------------------------*/ |
|---|
| 254 | /** Change the password of the user in MySQL |
|---|
| 255 | * @param $password string new password (cleartext) |
|---|
| 256 | * @return boolean TRUE if the password has been successfully changed, FALSE else. |
|---|
| 257 | */ |
|---|
| 258 | function put_mysql_details($password) { |
|---|
| 259 | global $db,$err,$mem,$cuid,$admin; |
|---|
| 260 | $err->log("mysql","put_mysql_details"); |
|---|
| 261 | $db->query("SELECT * FROM db WHERE uid='$cuid';"); |
|---|
| 262 | if (!$db->num_rows()) { |
|---|
| 263 | $err->raise("mysql",7); |
|---|
| 264 | return false; |
|---|
| 265 | } |
|---|
| 266 | $db->next_record(); |
|---|
| 267 | $login=$db->f("login"); |
|---|
| 268 | |
|---|
| 269 | if (strlen($password)>16) { |
|---|
| 270 | $err->raise("mysql",8); |
|---|
| 271 | return false; |
|---|
| 272 | } |
|---|
| 273 | if (!$password) { |
|---|
| 274 | $err->raise("mysql",20); |
|---|
| 275 | return false; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | // Check this password against the password policy using common API : |
|---|
| 279 | if (is_callable(array($admin,"checkPolicy"))) { |
|---|
| 280 | if (!$admin->checkPolicy("mysql",$login,$password)) { |
|---|
| 281 | return false; // The error has been raised by checkPolicy() |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // Update all the "pass" fields for this user : |
|---|
| 286 | $db->query("UPDATE db SET pass='$password' WHERE uid='$cuid';"); |
|---|
| 287 | $db->query("SET PASSWORD FOR '$login'@'$this->client' = PASSWORD('$password')"); |
|---|
| 288 | return true; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | /* ----------------------------------------------------------------- */ |
|---|
| 293 | /** Create a new mysql account for this user |
|---|
| 294 | * @param string cleartext password for the new account |
|---|
| 295 | * It also create the first database. |
|---|
| 296 | */ |
|---|
| 297 | function new_mysql($password) { |
|---|
| 298 | global $db,$err,$mem,$cuid,$admin; |
|---|
| 299 | $err->log("mysql","new_mysql"); |
|---|
| 300 | if (strlen($password)>16) { |
|---|
| 301 | $err->raise("mysql",8); |
|---|
| 302 | return false; |
|---|
| 303 | } |
|---|
| 304 | if (!$password) { |
|---|
| 305 | $err->raise("mysql",20); |
|---|
| 306 | return false; |
|---|
| 307 | } |
|---|
| 308 | $db->query("SELECT * FROM db WHERE uid='$cuid';"); |
|---|
| 309 | if ($db->num_rows()) { |
|---|
| 310 | $err->raise("mysql",10); |
|---|
| 311 | return false; |
|---|
| 312 | } |
|---|
| 313 | $login=$mem->user["login"]; |
|---|
| 314 | $dbname=$mem->user["login"]; |
|---|
| 315 | |
|---|
| 316 | // Username cannot be longer than 16 characters |
|---|
| 317 | if (strlen($login)>16) { |
|---|
| 318 | $err->raise("mysql",15); |
|---|
| 319 | return false; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | // Check this password against the password policy using common API : |
|---|
| 323 | if (is_callable(array($admin,"checkPolicy"))) { |
|---|
| 324 | if (!$admin->checkPolicy("mysql",$login,$password)) { |
|---|
| 325 | return false; // The error has been raised by checkPolicy() |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | // OK, creation now... |
|---|
| 330 | $db->query("INSERT INTO db (uid,login,pass,db) VALUES ('$cuid','".$login."','$password','".$dbname."');"); |
|---|
| 331 | // give everything but GRANT on $user.* |
|---|
| 332 | $db->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$login."'@'$this->client' IDENTIFIED BY '".$password."'"); |
|---|
| 333 | $db->query("CREATE DATABASE `".$dbname."`;"); |
|---|
| 334 | return true; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | /* ----------------------------------------------------------------- */ |
|---|
| 339 | /** Restore a sql database. |
|---|
| 340 | * @param $file string The filename, relative to the user root dir, which contains a sql dump |
|---|
| 341 | * @param $stdout boolean shall-we dump the error to stdout ? |
|---|
| 342 | * @param $id integer The ID of the database to dump to. |
|---|
| 343 | * @return boolean TRUE if the database has been restored, or FALSE if an error occurred |
|---|
| 344 | */ |
|---|
| 345 | function restore($file,$stdout,$id) { |
|---|
| 346 | global $err,$bro,$mem,$L_MYSQL_HOST; |
|---|
| 347 | if (!$r=$this->get_mysql_details($id)) { |
|---|
| 348 | return false; |
|---|
| 349 | } |
|---|
| 350 | if (!($fi=$bro->convertabsolute($file,0))) { |
|---|
| 351 | $err->raise("mysql",9); |
|---|
| 352 | return false; |
|---|
| 353 | } |
|---|
| 354 | if (substr($fi,-3)==".gz") { |
|---|
| 355 | $exe="/bin/gzip -d -c <".escapeshellarg($fi)." | /usr/bin/mysql -h".escapeshellarg($L_MYSQL_HOST)." -u".escapeshellarg($r["login"])." -p".escapeshellarg($r["pass"])." ".escapeshellarg($r["db"]); |
|---|
| 356 | } elseif (substr($fi,-4)==".bz2") { |
|---|
| 357 | $exe="/usr/bin/bunzip2 -d -c <".escapeshellarg($fi)." | /usr/bin/mysql -h".escapeshellarg($L_MYSQL_HOST)." -u".escapeshellarg($r["login"])." -p".escapeshellarg($r["pass"])." ".escapeshellarg($r["db"]); |
|---|
| 358 | } else { |
|---|
| 359 | $exe="/usr/bin/mysql -h".escapeshellarg($L_MYSQL_HOST)." -u".escapeshellarg($r["login"])." -p".escapeshellarg($r["pass"])." ".escapeshellarg($r["db"])." <".escapeshellarg($fi); |
|---|
| 360 | } |
|---|
| 361 | $exe .= " 2>&1"; |
|---|
| 362 | |
|---|
| 363 | echo "<code><pre>" ; |
|---|
| 364 | if ($stdout) { |
|---|
| 365 | passthru($exe,$ret); |
|---|
| 366 | } else { |
|---|
| 367 | exec ($exe,$ret); |
|---|
| 368 | } |
|---|
| 369 | echo "</pre></code>" ; |
|---|
| 370 | if ($ret != 0) { |
|---|
| 371 | return false ; |
|---|
| 372 | } else { |
|---|
| 373 | return true ; |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | /* ----------------------------------------------------------------- */ |
|---|
| 379 | /** Get the size of a database |
|---|
| 380 | * @param $dbname name of the database |
|---|
| 381 | * @return integer database size |
|---|
| 382 | * @access private |
|---|
| 383 | */ |
|---|
| 384 | function get_db_size($dbname) { |
|---|
| 385 | global $db,$err; |
|---|
| 386 | |
|---|
| 387 | $db->query("SHOW TABLE STATUS FROM `$dbname`;"); |
|---|
| 388 | $size = 0; |
|---|
| 389 | while ($db->next_record()) { |
|---|
| 390 | $size += $db->f('Data_length') + $db->f('Index_length') |
|---|
| 391 | + $db->f('Data_free'); |
|---|
| 392 | } |
|---|
| 393 | return $size; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | /* ------------------------------------------------------------ */ |
|---|
| 398 | /** |
|---|
| 399 | * Returns the list of database users of an account |
|---|
| 400 | **/ |
|---|
| 401 | function get_userslist() { |
|---|
| 402 | global $db,$err,$bro,$cuid; |
|---|
| 403 | $err->log("mysql","get_userslist"); |
|---|
| 404 | $db->query("SELECT name FROM dbusers WHERE uid='$cuid';"); |
|---|
| 405 | if (!$db->num_rows()) { |
|---|
| 406 | $err->raise("mysql",19); |
|---|
| 407 | return false; |
|---|
| 408 | } |
|---|
| 409 | $c=array(); |
|---|
| 410 | while ($db->next_record()) { |
|---|
| 411 | $c[]=array("name"=>substr($db->f("name"),strpos($db->f("name"),"_")+1)); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | return $c; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | /* ------------------------------------------------------------ */ |
|---|
| 419 | /** |
|---|
| 420 | * Create a new user in MySQL rights tables |
|---|
| 421 | * @param $usern the username (we will add _[alternc-account] to it) |
|---|
| 422 | * @param $password The password for this username |
|---|
| 423 | * @param $passconf The password confirmation |
|---|
| 424 | * @return TRUE if the user has been created in MySQL or FALSE if an error occurred |
|---|
| 425 | **/ |
|---|
| 426 | function add_user($usern,$password,$passconf) { |
|---|
| 427 | global $db,$err,$quota,$mem,$cuid,$admin; |
|---|
| 428 | $err->log("mysql","add_user",$usern); |
|---|
| 429 | |
|---|
| 430 | $usern=trim($usern); |
|---|
| 431 | $user=addslashes($mem->user["login"]."_".$usern); |
|---|
| 432 | $pass=addslashes($password); |
|---|
| 433 | |
|---|
| 434 | if (!$usern) { |
|---|
| 435 | $err->raise("mysql",21); |
|---|
| 436 | return false; |
|---|
| 437 | } |
|---|
| 438 | if (!$pass) { |
|---|
| 439 | $err->raise("mysql",20); |
|---|
| 440 | return false; |
|---|
| 441 | } |
|---|
| 442 | if (!$quota->cancreate("mysql_users")) { |
|---|
| 443 | $err->raise("mysql",13); |
|---|
| 444 | return false; |
|---|
| 445 | } |
|---|
| 446 | if (!ereg("^[0-9a-z]",$usern)) { |
|---|
| 447 | $err->raise("mysql",14); |
|---|
| 448 | return false; |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | // We check the length of the COMPLETE username, not only the part after _ |
|---|
| 452 | if (strlen($user) > 16) { |
|---|
| 453 | $err->raise("mysql",15); |
|---|
| 454 | return false; |
|---|
| 455 | } |
|---|
| 456 | $db->query("SELECT * FROM dbusers WHERE name='$user';"); |
|---|
| 457 | if ($db->num_rows()) { |
|---|
| 458 | $err->raise("mysql",16); |
|---|
| 459 | return false; |
|---|
| 460 | } |
|---|
| 461 | if ($password != $passconf || !$password) { |
|---|
| 462 | $err->raise("mysql",17); |
|---|
| 463 | return false; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | // Check this password against the password policy using common API : |
|---|
| 467 | if (is_callable(array($admin,"checkPolicy"))) { |
|---|
| 468 | if (!$admin->checkPolicy("mysql",$user,$password)) { |
|---|
| 469 | return false; // The error has been raised by checkPolicy() |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | // We create the user account (the "file" right is the only one we need globally to be able to use load data into outfile) |
|---|
| 474 | $db->query("GRANT file ON *.* TO '$user'@'$this->client' IDENTIFIED BY '$pass';"); |
|---|
| 475 | // We add him to the user table |
|---|
| 476 | $db->query("INSERT INTO dbusers (uid,name) VALUES($cuid,'$user');"); |
|---|
| 477 | return true; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | /* ------------------------------------------------------------ */ |
|---|
| 482 | /** |
|---|
| 483 | * Delete a user in MySQL rights tables |
|---|
| 484 | * @param $user the username (we will add "[alternc-account]_" to it) to delete |
|---|
| 485 | * @return TRUE if the user has been deleted in MySQL or FALSE if an error occurred |
|---|
| 486 | **/ |
|---|
| 487 | function del_user($user) { |
|---|
| 488 | global $db,$err,$mem,$cuid,$L_MYSQL_DATABASE; |
|---|
| 489 | $err->log("mysql","del_user",$user); |
|---|
| 490 | if (!ereg("^[0-9a-z]",$user)) { |
|---|
| 491 | $err->raise("mysql",14); |
|---|
| 492 | return false; |
|---|
| 493 | } |
|---|
| 494 | $db->query("SELECT name FROM dbusers WHERE name='".$mem->user["login"]."_$user';"); |
|---|
| 495 | if (!$db->num_rows()) { |
|---|
| 496 | $err->raise("mysql",18); |
|---|
| 497 | return false; |
|---|
| 498 | } |
|---|
| 499 | $db->next_record(); |
|---|
| 500 | $login=$db->f("name"); |
|---|
| 501 | |
|---|
| 502 | // Ok, database exists and dbname is compliant. Let's proceed |
|---|
| 503 | $db->query("REVOKE ALL PRIVILEGES ON *.* FROM '".$mem->user["login"]."_$user'@'$this->client';"); |
|---|
| 504 | $db->query("DELETE FROM mysql.db WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); |
|---|
| 505 | $db->query("DELETE FROM mysql.user WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); |
|---|
| 506 | $db->query("FLUSH PRIVILEGES"); |
|---|
| 507 | $db->query("DELETE FROM dbusers WHERE uid='$cuid' AND name='".$mem->user["login"]."_$user';"); |
|---|
| 508 | return true; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | /* ------------------------------------------------------------ */ |
|---|
| 513 | /** |
|---|
| 514 | * Return the list of the database rights of user $user |
|---|
| 515 | * @param $user the username |
|---|
| 516 | * @return array An array of database name and rights |
|---|
| 517 | **/ |
|---|
| 518 | function get_user_dblist($user) { |
|---|
| 519 | global $db,$err,$mem,$cuid,$L_MYSQL_DATABASE; |
|---|
| 520 | $err->log("mysql","get_user_dblist"); |
|---|
| 521 | |
|---|
| 522 | $r=array(); |
|---|
| 523 | $dblist=$this->get_dblist(); |
|---|
| 524 | |
|---|
| 525 | for ( $i=0 ; $i<count($dblist) ; $i++ ) { |
|---|
| 526 | $db->query("SELECT Db, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, References_priv, Index_priv, Alter_priv, Create_tmp_table_priv, Lock_tables_priv FROM mysql.db WHERE User='".$mem->user["login"].($user?"_":"").$user."' AND Host='$this->client' AND Db='".$dblist[$i]["db"]."';"); |
|---|
| 527 | if ($db->next_record()) |
|---|
| 528 | $r[]=array("db"=>$dblist[$i]["name"], "select"=>$db->f("Select_priv"), "insert"=>$db->f("Insert_priv"), "update"=>$db->f("Update_priv"), "delete"=>$db->f("Delete_priv"), "create"=>$db->f("Create_priv"), "drop"=>$db->f("Drop_priv"), "references"=>$db->f("References_priv"), "index"=>$db->f("Index_priv"), "alter"=>$db->f("Alter_priv"), "create_tmp"=>$db->f("Create_tmp_table_priv"), "lock"=>$db->f("Lock_tables_priv")); |
|---|
| 529 | else |
|---|
| 530 | $r[]=array("db"=>$dblist[$i]["name"], "select"=>"N", "insert"=>"N", "update"=>"N", "delete"=>"N", "create"=>"N", "drop"=>"N", "references"=>"N", "index"=>"N", "alter"=>"N", "Create_tmp"=>"N", "lock"=>"N" ); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | return $r; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | /* ------------------------------------------------------------ */ |
|---|
| 538 | /** |
|---|
| 539 | * Set the access rights of user $user to database $dbn to be rights $rights |
|---|
| 540 | * @param $user the username to give rights to |
|---|
| 541 | * @param $dbn The database to give rights to |
|---|
| 542 | * @param $rights The rights as an array of MySQL keywords (insert, select ...) |
|---|
| 543 | * @return boolean TRUE if the rights has been applied or FALSE if an error occurred |
|---|
| 544 | * |
|---|
| 545 | **/ |
|---|
| 546 | function set_user_rights($user,$dbn,$rights) { |
|---|
| 547 | global $mem, $db; |
|---|
| 548 | |
|---|
| 549 | $usern=addslashes($mem->user["login"].($user?"_":"").$user); |
|---|
| 550 | $dbname=addslashes($mem->user["login"].($dbn?"_":"").$dbn); |
|---|
| 551 | // On génère les droits en fonction du tableau de droits |
|---|
| 552 | for( $i=0 ; $i<count($rights) ; $i++ ) { |
|---|
| 553 | switch ($rights[$i]) { |
|---|
| 554 | case "select": |
|---|
| 555 | $strrights.="SELECT,"; |
|---|
| 556 | break; |
|---|
| 557 | case "insert": |
|---|
| 558 | $strrights.="INSERT,"; |
|---|
| 559 | break; |
|---|
| 560 | case "update": |
|---|
| 561 | $strrights.="UPDATE,"; |
|---|
| 562 | break; |
|---|
| 563 | case "delete": |
|---|
| 564 | $strrights.="DELETE,"; |
|---|
| 565 | break; |
|---|
| 566 | case "create": |
|---|
| 567 | $strrights.="CREATE,"; |
|---|
| 568 | break; |
|---|
| 569 | case "drop": |
|---|
| 570 | $strrights.="DROP,"; |
|---|
| 571 | break; |
|---|
| 572 | case "references": |
|---|
| 573 | $strrights.="REFERENCES,"; |
|---|
| 574 | break; |
|---|
| 575 | case "index": |
|---|
| 576 | $strrights.="INDEX,"; |
|---|
| 577 | break; |
|---|
| 578 | case "alter": |
|---|
| 579 | $strrights.="ALTER,"; |
|---|
| 580 | break; |
|---|
| 581 | case "create_tmp": |
|---|
| 582 | $strrights.="CREATE TEMPORARY TABLES,"; |
|---|
| 583 | break; |
|---|
| 584 | case "lock": |
|---|
| 585 | $strrights.="LOCK TABLES,"; |
|---|
| 586 | break; |
|---|
| 587 | } |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | // We reset all user rights on this DB : |
|---|
| 591 | $db->query("SELECT * FROM mysql.db WHERE User = '$usern' AND Db = '$dbname';"); |
|---|
| 592 | if($db->num_rows()) |
|---|
| 593 | $db->query("REVOKE ALL PRIVILEGES ON $dbname.* FROM '$usern'@'$this->client';"); |
|---|
| 594 | if( $strrights ){ |
|---|
| 595 | $strrights=substr($strrights,0,strlen($strrights)-1); |
|---|
| 596 | $db->query("GRANT $strrights ON $dbname.* TO '$usern'@'$this->client';"); |
|---|
| 597 | } |
|---|
| 598 | $db->query("FLUSH PRIVILEGES"); |
|---|
| 599 | return TRUE; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | /* ----------------------------------------------------------------- */ |
|---|
| 604 | /** Hook function called by the quota class to compute user used quota |
|---|
| 605 | * Returns the used quota for the $name service for the current user. |
|---|
| 606 | * @param $name string name of the quota |
|---|
| 607 | * @return integer the number of service used or false if an error occured |
|---|
| 608 | * @access private |
|---|
| 609 | */ |
|---|
| 610 | function alternc_get_quota($name) { |
|---|
| 611 | global $err,$db,$cuid; |
|---|
| 612 | if ($name=="mysql") { |
|---|
| 613 | $err->log("mysql","alternc_get_quota"); |
|---|
| 614 | $c=$this->get_dblist(); |
|---|
| 615 | if (is_array($c)) { |
|---|
| 616 | return count($c); |
|---|
| 617 | } else { |
|---|
| 618 | return 0; |
|---|
| 619 | } |
|---|
| 620 | } elseif ($name=="mysql_users") { |
|---|
| 621 | $err->log("mysql","alternc_get_quota"); |
|---|
| 622 | $c=$this->get_userslist(); |
|---|
| 623 | if(is_array($c)) |
|---|
| 624 | return count($c); |
|---|
| 625 | else |
|---|
| 626 | return 0; |
|---|
| 627 | } else return false; |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | /* ----------------------------------------------------------------- */ |
|---|
| 632 | /** Hook function called when a user is deleted. |
|---|
| 633 | * AlternC's standard function that delete a member |
|---|
| 634 | * @access private |
|---|
| 635 | */ |
|---|
| 636 | function alternc_del_member() { |
|---|
| 637 | global $db,$err,$cuid; |
|---|
| 638 | $err->log("mysql","alternc_del_member"); |
|---|
| 639 | $c=$this->get_dblist(); |
|---|
| 640 | if (is_array($c)) { |
|---|
| 641 | for($i=0;$i<count($c);$i++) { |
|---|
| 642 | $this->del_db($c[$i]["name"]); |
|---|
| 643 | } |
|---|
| 644 | } |
|---|
| 645 | return true; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | /* ----------------------------------------------------------------- */ |
|---|
| 650 | /** Hook function called when a user is logged out. |
|---|
| 651 | * We just remove the cookie created in admin/sql_admin.php |
|---|
| 652 | * @access private |
|---|
| 653 | */ |
|---|
| 654 | function alternc_del_session() { |
|---|
| 655 | setcookie("REMOTE_USER",""); |
|---|
| 656 | setcookie("REMOTE_PASSWORD",""); |
|---|
| 657 | } |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | /* ----------------------------------------------------------------- */ |
|---|
| 661 | /** |
|---|
| 662 | * Exporte all the mysql information of an account |
|---|
| 663 | * @access private |
|---|
| 664 | * EXPERIMENTAL 'sid' function ;) |
|---|
| 665 | */ |
|---|
| 666 | function alternc_export($tmpdir) { |
|---|
| 667 | global $db,$err,$cuid; |
|---|
| 668 | $err->log("mysql","export"); |
|---|
| 669 | $db->query("SELECT login, pass, db, bck_mode, bck_dir, bck_history, bck_gzip FROM db WHERE uid='$cuid';"); |
|---|
| 670 | if ($db->next_record()) { |
|---|
| 671 | $str="<mysql>\n"; |
|---|
| 672 | $str.=" <login>".xml_entities($db->Record["login"])."</login>"; |
|---|
| 673 | $str.=" <pass>".xml_entities($db->Record["pass"])."</pass>"; |
|---|
| 674 | do { |
|---|
| 675 | // Do the dump : |
|---|
| 676 | $filename=$tmpdir."/mysql.".$db->Record["db"].".sql.gz"; |
|---|
| 677 | exec("/usr/bin/mysqldump --add-drop-table --allow-keywords -Q -f -q -a -e -u".escapeshellarg($db->Record["login"])." -p".escapeshellarg($db->Record["pass"])." ".escapeshellarg($db->Record["db"])." |/bin/gzip >".escapeshellarg($filename)); |
|---|
| 678 | $str.=" <db>\n"; |
|---|
| 679 | $str.=" <name>".xml_entities($db->Record["db"])."</name>\n"; |
|---|
| 680 | if ($s["bck_mode"]!=0) { |
|---|
| 681 | $str.=" <backup>\n"; |
|---|
| 682 | $str.=" <mode>".xml_entities($db->Record["bck_mode"])."</mode>\n"; |
|---|
| 683 | $str.=" <dir>".xml_entities($db->Record["bck_dir"])."</dir>\n"; |
|---|
| 684 | $str.=" <history>".xml_entities($db->Record["bck_history"])."</history>\n"; |
|---|
| 685 | $str.=" <gzip>".xml_entities($db->Record["bck_gzip"])."</gzip>\n"; |
|---|
| 686 | $str.=" </backup>\n"; |
|---|
| 687 | } |
|---|
| 688 | $str.=" </db>\n"; |
|---|
| 689 | } while ($db->next_record()); |
|---|
| 690 | $str.="</mysql>\n"; |
|---|
| 691 | } |
|---|
| 692 | return $str; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | } /* Class m_mysql */ |
|---|
| 697 | |
|---|
| 698 | ?> |
|---|