Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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('sudo cp ' . $this->authConfig . ' /tmp/');
- exec('chmod 777 -R ' . $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('sudo cp /etc/shadow /tmp/');
- exec('sudo chmod 777 -R /tmp/shadow');
- $file = file('/tmp/shadow');
- $this->content = $file;
- unlink('/tmp/shadow');
- } else {
- $this->useType = 'passwd';
- exec('sudo cp /etc/passwd /tmp/');
- exec('sudo chmod 777 -R /tmp/passwd');
- $file = file('/tmp/passwd');
- $this->content = $file;
- unlink('/tmp/passwd');
- }
- }
- }
- final private function getUserInfo($username) {
- if (isset($this->content)) {
- foreach ($this->content as $line) {
- $data = explode(':', $line);
- $user = trim($data[0]);
- if ($user == $username) {
- $pass = trim($data[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);
- return trim($passHashed) === trim($myInfo->password);
- }
- return false;
- }
- }
- ?>
- <?php
- //Como utilizar
- $lin = new LinuxAuth();
- $myPass = base64_encode('myPassword');
- $myUser = 'root';
- if($lin->authUser($myUser,$myPass)){
- echo " AUTENTICADO";
- }else{
- echo " NAO AUTENTICADO";
- }
- ?>
Add Comment
Please, Sign In to add comment