Login   Register  
PHP Classes
elePHPant
Icontem

File: LinuxAuth.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marcus Brasizza  >  Login with Linux passwd or shadow  >  LinuxAuth.php  >  Download  
File: LinuxAuth.php
Role: Class source
Content type: text/plain
Description: Class to auth
Class: Login with Linux passwd or shadow
Authenticate Linux users with passwd or shadow
Author: By
Last change: I made a change.
Now i don't use sudo or cp anymore.
I'm using now the cat command do get the content and then saving in a text file instead of use a cp to copy the file that need some privilege.
Date: 2 years ago
Size: 2,855 bytes
 

Contents

Class file image Download
<?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;
        
    }

}
?>