Changeset 2943


Ignore:
Timestamp:
05/18/11 22:26:12 (2 years ago)
Author:
fufroma
Message:

Séparation des bases de données utilisateurs de la base
de donnée systéme.

Attention ! Bug ! $db et $dbu ne sont pas étanche !
Il faut se pencher sur la class db_mysql.php pour
finir l'isolation

Location:
alternc/trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • alternc/trunk/bureau/class/m_bro.php

    r2863 r2943  
    268268    // Now seek the extension 
    269269    if (!$bro_type[$ext]) { 
    270         return "File"; 
    271     } else { 
    272         return $bro_type[$ext]; 
     270            return "File"; 
     271    } else { 
     272            return $bro_type[$ext]; 
    273273    } 
    274274  } 
     
    307307        $nextpath = $dir . '/' . $file; 
    308308 
    309         if ($file != '.' && $file != '..' && !is_link($nextpath)) { 
     309              if ($file != '.' && $file != '..' && !is_link($nextpath)) { 
    310310          if (is_dir($nextpath)) { 
    311311            $totalsize += $this->dirsize($nextpath); 
     
    406406      $new[$i]=ssla($new[$i]); 
    407407      if (!strpos($old[$i],"/") && !strpos($new[$i],"/")) {  // caractère / interdit dans old ET dans new... 
    408         @rename($absolute."/".$old[$i],$absolute."/".$old[$i].$alea); 
     408              @rename($absolute."/".$old[$i],$absolute."/".$old[$i].$alea); 
    409409      } 
    410410    } 
    411411    for ($i=0;$i<count($old);$i++) { 
    412412      if (!strpos($old[$i],"/") && !strpos($new[$i],"/")) {  // caractère / interdit dans old ET dans new... 
    413         @rename($absolute."/".$old[$i].$alea,$absolute."/".$new[$i]); 
     413        @rename($absolute."/".$old[$i].$alea,$absolute."/".$new[$i]); 
    414414      } 
    415415    } 
     
    477477      $d[$i]=ssla($d[$i]); // strip slashes if needed 
    478478      if (!strpos($d[$i],"/")) {  // caractère / interdit dans le nom du fichier 
    479         // @rename($absolute."/".$old[$i],$absolute."/".$old[$i].$alea); 
    480         $m = fileperms($absolute."/". $d[$i]); 
    481  
    482         // pour l'instant on se limite a "write" pour owner, puisque c'est le seul 
    483         // cas interessant compte tenu de la conf de Apache pour AlternC.. 
    484         if ($perm[$i]['w']) { 
    485           $m = $m | 128; 
    486         } else { 
    487           $m = $m ^ 128; 
    488         } 
    489         $m = $m | ($perm[$i]['w'] ? 128 : 0); // 0600 
    490         chmod($absolute."/".$d[$i], $m); 
    491         echo "chmod " . sprintf('%o', $m) . " file, was " . sprintf('%o', fileperms($absolute."/". $d[$i])). " -- " . $perm[$i]['w']; 
     479        // @rename($absolute."/".$old[$i],$absolute."/".$old[$i].$alea); 
     480        $m = fileperms($absolute."/". $d[$i]); 
     481 
     482        // pour l'instant on se limite a "write" pour owner, puisque c'est le seul 
     483        // cas interessant compte tenu de la conf de Apache pour AlternC.. 
     484        if ($perm[$i]['w']) { 
     485          $m = $m | 128; 
     486        } else { 
     487                $m = $m ^ 128; 
     488        } 
     489        $m = $m | ($perm[$i]['w'] ? 128 : 0); // 0600 
     490        chmod($absolute."/".$d[$i], $m); 
     491        echo "chmod " . sprintf('%o', $m) . " file, was " . sprintf('%o', fileperms($absolute."/". $d[$i])). " -- " . $perm[$i]['w']; 
    492492      } 
    493493    } 
     
    756756      $end="";  $beg=$dir;      $tofind=true; 
    757757      while ($tofind) { 
    758         $db->query("SELECT sub,domaine FROM sub_domaines WHERE compte='$cuid' 
    759                         AND type=0 AND (valeur='/$beg/' or valeur='/$beg');"); 
    760         $db->next_record(); 
    761         if ($db->num_rows()) { 
    762           $tofind=false; 
    763           $this->cacheurl["d".$dir]="http://".$db->f("sub").ife($db->f("sub"),".").$db->f("domaine").$end; 
    764         } 
    765         if (!$beg && $tofind) { 
    766           $tofind=false; 
    767           $this->cacheurl["d".$dir]="-"; 
    768                                 // We did not find it ;( 
    769         } 
    770         if (($tt=strrpos($beg,"/"))!==false) { 
    771           $end=substr($beg,$tt).$end; // = /topdir$end so $end starts AND ends with / 
    772           $beg=substr($beg,0,$tt); 
    773         } else { 
    774           $end="/".$beg.$end; 
    775           $beg="/"; 
    776         } 
     758        $db->query("SELECT sub,domaine FROM sub_domaines WHERE compte='$cuid' 
     759            AND type=0 AND (valeur='/$beg/' or valeur='/$beg');"); 
     760        $db->next_record(); 
     761        if ($db->num_rows()) { 
     762          $tofind=false; 
     763          $this->cacheurl["d".$dir]="http://".$db->f("sub").ife($db->f("sub"),".").$db->f("domaine").$end; 
     764        } 
     765        if (!$beg && $tofind) { 
     766          $tofind=false; 
     767          $this->cacheurl["d".$dir]="-"; 
     768              // We did not find it ;( 
     769        } 
     770        if (($tt=strrpos($beg,"/"))!==false) { 
     771          $end=substr($beg,$tt).$end; // = /topdir$end so $end starts AND ends with / 
     772          $beg=substr($beg,0,$tt); 
     773        } else { 
     774          $end="/".$beg.$end; 
     775          $beg="/"; 
     776        } 
    777777      } 
    778778    } 
     
    794794      case "bz": 
    795795      case "bz2": 
    796         $ext = array_pop($parts) . $ext; 
    797         /* FALLTHROUGH */ 
     796            $ext = array_pop($parts) . $ext; 
     797            /* FALLTHROUGH */ 
    798798      case "tar.gz": 
    799799      case "tar.bz": 
     
    817817      $absolute.="/".$file; 
    818818      if (file_exists($absolute)) { 
    819         $content = @file($absolute); 
    820         for($i=0;$i<count($content);$i++) { 
    821           echo stripslashes($content[$i]); 
    822         } 
     819              $content = @file($absolute); 
     820              for($i=0;$i<count($content);$i++) { 
     821                echo stripslashes($content[$i]); 
     822        } 
    823823      } 
    824824    } else { 
     
    844844      $absolute.="/".$file; 
    845845      if (file_exists($absolute)) { 
    846         $f=@fopen($absolute,"wb"); 
    847         if ($f) { 
    848           fputs($f,$texte,strlen($texte)); 
    849           fclose($f); 
    850         } 
     846              $f=@fopen($absolute,"wb"); 
     847        if ($f) { 
     848                fputs($f,$texte,strlen($texte)); 
     849                fclose($f); 
     850              } 
    851851      } 
    852852    } else { 
     
    940940      $handle = opendir($file); 
    941941      while($filename = readdir($handle)) { 
    942         if ($filename != "." && $filename != "..") { 
    943           $this->_delete($file."/".$filename); 
    944         } 
     942              if ($filename != "." && $filename != "..") { 
     943                $this->_delete($file."/".$filename); 
     944              } 
    945945      } 
    946946      closedir($handle); 
  • alternc/trunk/bureau/class/m_mysql.php

    r2926 r2943  
    3434 * @copyright    AlternC-Team 2002-2005 http://alternc.org/ 
    3535 */ 
     36 
     37class DBU_mysql extends DB_Sql { 
     38  var $Host,$HumanHostname,$User,$Password; 
     39 
     40  /** 
     41  * Creator 
     42  */ 
     43  function DBU_mysql() { 
     44 
     45    # Use the dbusers file if exist, else use default alternc configuration 
     46    if ( is_readable("/etc/alternc/dbusers.cnf") ) { 
     47      $mysqlconf=file_get_contents("/etc/alternc/dbusers.cnf"); 
     48    } else { 
     49      $mysqlconf=file_get_contents("/etc/alternc/my.cnf"); 
     50    } 
     51    $mysqlconf=explode("\n",$mysqlconf); 
     52 
     53    # Read the configuration 
     54    foreach ($mysqlconf as $line) { 
     55      # First, read the "standard" configuration 
     56      if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $regs)) { 
     57          switch ($regs[1]) { 
     58          case "user": 
     59              $user = $regs[2]; 
     60              break; 
     61          case "password": 
     62              $password = $regs[2]; 
     63              break; 
     64          case "host": 
     65              $host = $regs[2]; 
     66              break; 
     67          } 
     68      } 
     69      # Then, read specific alternc configuration 
     70      if (preg_match('/^#alternc_var ([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $regs)) { 
     71        $$regs[1]=$regs[2]; 
     72      } 
     73    } 
     74 
     75    # Set value of human_host if unset 
     76    if (! isset($human_hostname) || empty($human_hostname)) { 
     77      if ( checkip($host) || checkipv6($host) ) { 
     78        $human_hostname = gethostbyaddr($host); 
     79      } else { 
     80        $human_hostname = $host; 
     81      } 
     82    } 
     83 
     84 
     85    # Create the object 
     86    $this->Host     = $host; 
     87    $this->User     = $user; 
     88    $this->Password = $password; 
     89// TODO BUG BUG BUG 
     90// c'est pas étanche : $db se retrouve avec Database de $sql->dbu . Danger, faut comprendre pourquoi 
     91    $this->Database = "alternc"; 
     92    $this->HumanHostname = $human_hostname; 
     93 
     94  } 
     95} 
     96 
     97 
    3698class m_mysql { 
    37  
    38   var $server; 
    39   var $client; 
    40  
     99  var $dbu; 
    41100 
    42101  /*---------------------------------------------------------------------------*/ 
     
    45104  */ 
    46105  function m_mysql() { 
    47       $this->server = $GLOBALS['L_MYSQL_HOST']; 
    48       $this->client = $GLOBALS['L_MYSQL_CLIENT']; 
     106    $this->dbu = new DBU_mysql(); 
    49107  } 
    50108 
     
    160218      $pa=addslashes($db->f("pass")); 
    161219    } 
    162     if ($db->query("CREATE DATABASE `$dbname`;")) { 
     220    if ($this->dbu->query("CREATE DATABASE `$dbname`;")) { 
    163221      // Ok, database does not exist, quota is ok and dbname is compliant. Let's proceed 
    164222      $db->query("INSERT INTO db (uid,login,pass,db,bck_mode) VALUES ('$cuid','$lo','$pa','$dbname',0);"); 
    165223      // give everything but GRANT on db.* 
    166224      // we assume there's already a user 
    167       $db->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$lo."'@'$this->client'"); 
     225      $this->dbu->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$lo."'@'$this->client'"); 
    168226      return true; 
    169227    } else { 
     
    195253    // Ok, database exists and dbname is compliant. Let's proceed 
    196254    $db->query("DELETE FROM db WHERE uid='$cuid' AND db='$dbname';"); 
    197     $db->query("DROP DATABASE `$dbname`;"); 
     255    $this->dbu->query("DROP DATABASE `$dbname`;"); 
    198256    $db->query("SELECT COUNT(*) AS cnt FROM db WHERE uid='$cuid';"); 
    199257    $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;"); 
     258    $this->dbu->query("REVOKE ALL PRIVILEGES ON `".$dbname."`.* FROM '".$login."'@'$this->client'"); 
     259    if ($this->dbu->f("cnt")==0) { 
     260      $this->dbu->query("DELETE FROM mysql.user WHERE User='".$login."';"); 
     261      $this->dbu->query("FLUSH PRIVILEGES;"); 
    204262    } 
    205263    return true; 
     
    279337    if (is_callable(array($admin,"checkPolicy"))) { 
    280338      if (!$admin->checkPolicy("mysql",$login,$password)) { 
    281         return false; // The error has been raised by checkPolicy() 
     339              return false; // The error has been raised by checkPolicy() 
    282340      } 
    283341    } 
     
    285343    // Update all the "pass" fields for this user :  
    286344    $db->query("UPDATE db SET pass='$password' WHERE uid='$cuid';"); 
    287     $db->query("SET PASSWORD FOR '$login'@'$this->client' = PASSWORD('$password')"); 
     345    $this->dbu->query("SET PASSWORD FOR '$login'@'$this->client' = PASSWORD('$password')"); 
    288346    return true; 
    289347  } 
     
    323381    if (is_callable(array($admin,"checkPolicy"))) { 
    324382      if (!$admin->checkPolicy("mysql",$login,$password)) { 
    325         return false; // The error has been raised by checkPolicy() 
     383        return false; // The error has been raised by checkPolicy() 
    326384      } 
    327385    }     
     
    330388    $db->query("INSERT INTO db (uid,login,pass,db) VALUES ('$cuid','".$login."','$password','".$dbname."');"); 
    331389    // give everything but GRANT on $user.* 
    332     $db->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$login."'@'$this->client' IDENTIFIED BY '".addslashes($password)."'"); 
    333     $db->query("CREATE DATABASE `".$dbname."`;"); 
     390    $this->dbu->query("GRANT ALL PRIVILEGES ON `".$dbname."`.* TO '".$login."'@'$this->client' IDENTIFIED BY '".addslashes($password)."'"); 
     391    $this->dbu->query("CREATE DATABASE `".$dbname."`;"); 
    334392    return true; 
    335393  } 
     
    344402   */ 
    345403  function restore($file,$stdout,$id) {  
     404// TODO don't work with the separated sql serveur for dbusers 
    346405    global $err,$bro,$mem,$L_MYSQL_HOST; 
    347406    if (!$r=$this->get_mysql_details($id)) {  
     
    385444    global $db,$err; 
    386445     
    387     $db->query("SHOW TABLE STATUS FROM `$dbname`;"); 
     446    $this->dbu->query("SHOW TABLE STATUS FROM `$dbname`;"); 
    388447    $size = 0; 
    389448    while ($db->next_record()) { 
    390       $size += $db->f('Data_length') + $db->f('Index_length') 
    391         + $db->f('Data_free'); 
     449      $size += $db->f('Data_length') + $db->f('Index_length')   + $db->f('Data_free'); 
    392450    } 
    393451    return $size; 
     
    467525    if (is_callable(array($admin,"checkPolicy"))) { 
    468526      if (!$admin->checkPolicy("mysql",$user,$password)) { 
    469         return false; // The error has been raised by checkPolicy() 
     527              return false; // The error has been raised by checkPolicy() 
    470528      } 
    471529    } 
    472530 
    473531    // 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';"); 
     532    $this->dbu->query("GRANT file ON *.* TO '$user'@'$this->client' IDENTIFIED BY '$pass';"); 
    475533    // We add him to the user table  
    476534    $db->query("INSERT INTO dbusers (uid,name) VALUES($cuid,'$user');"); 
     
    505563    } 
    506564 
    507     $db->query("SET PASSWORD FOR '$user'@'$this->client' = PASSWORD('$pass')"); 
     565    $this->dbu->query("SET PASSWORD FOR '$user'@'$this->client' = PASSWORD('$pass')"); 
    508566    return true; 
    509567  } 
     
    533591 
    534592    // Ok, database exists and dbname is compliant. Let's proceed 
    535     $db->query("REVOKE ALL PRIVILEGES ON *.* FROM '".$mem->user["login"]."_$user'@'$this->client';"); 
    536     $db->query("DELETE FROM mysql.db WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); 
    537     $db->query("DELETE FROM mysql.user WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); 
    538     $db->query("FLUSH PRIVILEGES"); 
    539     $db->query("DELETE FROM dbusers WHERE uid='$cuid' AND name='".$mem->user["login"]."_$user';"); 
     593    $this->dbu->query("REVOKE ALL PRIVILEGES ON *.* FROM '".$mem->user["login"]."_$user'@'$this->client';"); 
     594    $this->dbu->query("DELETE FROM mysql.db WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); 
     595    $this->dbu->query("DELETE FROM mysql.user WHERE User='".$mem->user["login"]."_$user' AND Host='$this->client';"); 
     596    $this->dbu->query("FLUSH PRIVILEGES"); 
     597    $this->dbu->query("DELETE FROM dbusers WHERE uid='$cuid' AND name='".$mem->user["login"]."_$user';"); 
    540598    return true; 
    541599  } 
     
    556614 
    557615    for ( $i=0 ; $i<count($dblist) ; $i++ ) { 
    558       $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"]."';"); 
    559       if ($db->next_record()) 
    560         $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")); 
     616      $this->dbu->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"]."';"); 
     617      if ($this->dbu->next_record()) 
     618        $r[]=array("db"=>$dblist[$i]["name"], "select"=>$this->dbu->f("Select_priv"), "insert"=>$this->dbu->f("Insert_priv"),   "update"=>$this->dbu->f("Update_priv"), "delete"=>$this->dbu->f("Delete_priv"), "create"=>$this->dbu->f("Create_priv"), "drop"=>$this->dbu->f("Drop_priv"), "references"=>$this->dbu->f("References_priv"), "index"=>$this->dbu->f("Index_priv"), "alter"=>$this->dbu->f("Alter_priv"), "create_tmp"=>$this->dbu->f("Create_tmp_table_priv"), "lock"=>$this->dbu->f("Lock_tables_priv")); 
    561619      else 
    562620        $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" ); 
     
    620678 
    621679    // We reset all user rights on this DB :  
    622     $db->query("SELECT * FROM mysql.db WHERE User = '$usern' AND Db = '$dbname';"); 
    623     if($db->num_rows()) 
    624       $db->query("REVOKE ALL PRIVILEGES ON $dbname.* FROM '$usern'@'$this->client';"); 
     680    $this->dbu->query("SELECT * FROM mysql.db WHERE User = '$usern' AND Db = '$dbname';"); 
     681    if($this->dbu->num_rows()) 
     682      $this->dbu->query("REVOKE ALL PRIVILEGES ON $dbname.* FROM '$usern'@'$this->client';"); 
    625683    if( $strrights ){ 
    626684      $strrights=substr($strrights,0,strlen($strrights)-1); 
    627       $db->query("GRANT $strrights ON $dbname.* TO '$usern'@'$this->client';");       
    628     } 
    629     $db->query("FLUSH PRIVILEGES"); 
     685      $this->dbu->query("GRANT $strrights ON $dbname.* TO '$usern'@'$this->client';");       
     686    } 
     687    $this->dbu->query("FLUSH PRIVILEGES"); 
    630688    return TRUE; 
    631689  } 
     
    702760   */ 
    703761  function alternc_export($tmpdir) { 
     762//TODO don't work with separated sql server for dbusers 
    704763    global $db,$err,$cuid; 
    705764    $err->log("mysql","export"); 
Note: See TracChangeset for help on using the changeset viewer.