Guest User

Untitled

a guest
Apr 9th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2.  
  3. class LinuxAuth {
  4.     /** @author Marcus Brasizza
  5.      *
  6.      * @version 1.0.0
  7.      */
  8.     private $pathShadow = '/etc/shadow';
  9.     private $pathPasswd = '/etc/passwd';
  10.     private $authConfig = '/etc/sysconfig/authconfig';
  11.     private $authConfigTMP = '/tmp/authconfig';
  12.     private $content = null;
  13.     private $useType = null;
  14.    
  15.    
  16.     /**
  17.      *  Constructor
  18.      */
  19.     function __construct() {
  20.         exec('sudo cp  ' . $this->authConfig . ' /tmp/');
  21.         exec('chmod 777 -R  ' . $this->authConfigTMP);
  22.         $authCon = file_get_contents($this->authConfigTMP);
  23.         $strFind = 'USESHADOW';
  24.         $tamFind = strlen($strFind);
  25.         $posShadow = strpos($authCon, $strFind);
  26.         if ($posShadow !== false) {
  27.             $useFind = trim(substr($authCon, ($posShadow + $tamFind + 1), 3));
  28.  
  29.             if ($useFind == 'yes') {
  30.                 $this->useType = 'shadow';
  31.                 exec('sudo cp /etc/shadow  /tmp/');
  32.                 exec('sudo chmod 777 -R /tmp/shadow');
  33.                 $file = file('/tmp/shadow');
  34.                 $this->content = $file;
  35.                 unlink('/tmp/shadow');                  
  36.             } else {
  37.                 $this->useType = 'passwd';
  38.                 exec('sudo cp /etc/passwd  /tmp/');
  39.                 exec('sudo chmod 777 -R /tmp/passwd');
  40.                 $file = file('/tmp/passwd');
  41.                 $this->content = $file;
  42.                 unlink('/tmp/passwd');
  43.             }
  44.         }
  45.     }
  46.    
  47.    
  48.    
  49.     final private function getUserInfo($username) {
  50.         if (isset($this->content)) {
  51.             foreach ($this->content as $line) {
  52.                 $data = explode(':', $line);
  53.                 $user = trim($data[0]);
  54.                 if ($user == $username) {
  55.                     $pass = trim($data[1]);
  56.                     $info = new stdClass();
  57.                     $info->username = $user;
  58.                     $info->password = $pass;
  59.                     return $info;
  60.                 }
  61.             }
  62.         }
  63.         return false;
  64.     }
  65.    
  66.     /**
  67.      * Username Access
  68.      * @param Username $username
  69.      * @param Password $password
  70.      * @return boolean true if the user is in the password file system
  71.      * @access public
  72.      * @uses $myauth->authUser('myUsername','mypasswordBased64');
  73.      */
  74.     public function authUser($username,$password){              
  75.         $myInfo = $this->getUserInfo($username);
  76.         if($myInfo != false){
  77.             $password = base64_decode($password);
  78.             $passHashed = crypt($password, $myInfo->password);
  79.             return trim($passHashed) ===  trim($myInfo->password);
  80.         }
  81.         return false;
  82.        
  83.     }
  84.  
  85. }
  86. ?>
  87.  
  88.  
  89.  
  90.  
  91.  
  92. <?php
  93.  
  94. //Como utilizar
  95.  
  96. $lin = new LinuxAuth();
  97. $myPass = base64_encode('myPassword');
  98. $myUser = 'root';
  99. if($lin->authUser($myUser,$myPass)){
  100.     echo " AUTENTICADO";
  101. }else{
  102.     echo " NAO AUTENTICADO";
  103. }
  104.  
  105. ?>
Add Comment
Please, Sign In to add comment