Programação Especialista

Artigos de Programação em Geral

Autenticar Usuário no Linux utiliando PHP + (passwd ou shadow)

, , , , , ,

Hoje surgiu um desafio aqui onde eu trabalho:

Fazer autenticação de um usuário utilizando o passwd ou o shadow pois evitariamos ter uma senha de root para cada sistema

Então pensei, pesquisei e cheguei no seguinte código que eu espero que seja útil para alguem algum dia

<?php

class LinuxAuth {
    /** @author Marcus Brasizza
     *
     * @version 1.0.0
     */
    private $pathShadow = '/etc/shadow';
    private $pathPasswd = '/etc/passwd';
    private $authConfig = '/etc/sysconfig/authconfig';
    private $authConfigTMP = '/tmp/authconfig';
    private $content = null;
    private $useType = null;
    
    
    /**
     *  Constructor
     */
    function __construct() {
        exec('cat  ' . $this->authConfig . ' > '. $this->authConfigTMP );               
        $authCon = file_get_contents($this->authConfigTMP);
        $strFind = 'USESHADOW';
        $tamFind = strlen($strFind);
        $posShadow = strpos($authCon, $strFind);
        if ($posShadow !== false) {
            $useFind = trim(substr($authCon, ($posShadow + $tamFind + 1), 3));

            if ($useFind == 'yes') {
                $this->useType = 'shadow';
                exec('cat /etc/shadow > /tmp/shadow');                
                $file = file('/tmp/shadow');
                $this->content = $file;
                unlink('/tmp/shadow');                   
            } else {
                $this->useType = 'passwd';
                exec('cat /etc/passwd  /tmp/passwd');                
                $file = file('/tmp/passwd');
                $this->content = $file;
                unlink('/tmp/passwd');
            }
        }
    }
    
    
    
    final private function getUserInfo($userInfo) {
        if (isset($this->content)) {
            foreach ($this->content as $line) {               
                if (strpos($line, $userInfo) !== false) {
                    $l = explode(':', $line);
                    $user = trim($l[0]);
                    $pass = trim($l[1]); 
                    $info = new stdClass();
                    $info->username = $user;
                    $info->password = $pass;
                    return $info;
                }              
                
            }
            return false;
        }
    }
    
    /**
     * Username Access
     * @param Username $username
     * @param Password $password
     * @return boolean true if the user is in the password file system 
     * @access public
     * @uses $myauth->authUser('myUsername','mypasswordBased64');
     */
    public function authUser($username,$password){
               
        $myInfo = $this->getUserInfo($username);
        if($myInfo != false){
            
            $password = base64_decode($password);
           
            $passHashed = crypt($password,$myInfo->password);
            if( trim($passHashed) ===  trim($myInfo->password)){
                return true;
            }else{
             return false;  
            }
        }
        return false;
        
    }

}
?>





A utilização é simples

<?php

$lin = new LinuxAuth();
$myPass = base64_encode('myP4ssw0rd');
$myUser = 'root';
if($lin->authUser($myUser,$myPass)){    
    
    echo " AUTENTICADO";
}else{
    echo " NAO AUTENTICADO";
}

?>

Como utilizar complexTypes no nuSoapArray de ComplexTypes no Nusoap

Comments

Unregistered user Thursday, January 19, 2012 12:15:53 PM

andre writes: muito boa a sua solução! obrigado era o que eu estava precisando para meu problema. fiz uma pequena modificaçao, pois utilizo um sistema ubuntu, entao, parti direto para o arquivo shadow na funcao construct. ficou mais ou menos assim: function __construct() { $this->useType = 'shadow'; exec('cat /etc/shadow > /tmp/shadow'); $file = file('/tmp/shadow'); $this->content = $file; unlink('/tmp/shadow'); }

Write a comment

New comments have been disabled for this post.