#!/usr/bin/php -q
<?php 
function cls() 
{
   echo "\033[2J"; // Clear screen
   echo "\033[0;0H"; // Cursor to 0,0
}

function text_red() 
{  // Set text to red
   echo "\033[0;31m";
}

function text_black() 
{  // Set text to black
   echo "\033[0;30m";
}

// Checks if a user exists
function user_exists($name)
{
        // Greps the user from /etc/passwd
        $cmd="grep ".escapeshellarg ($name)." /etc/passwd";
        $res=shell_exec($cmd);
        if (""!=$res)
        {
                return true;
        }
        else
        {
                return false;
        }
} 

// Checks if a directory exists and parses for permissions
function check_dir($dir)
{
        if (is_dir($dir))
        {
                echo ("$dir existsn");
                echo ("Owner: ".fileowner($dir));
                // parse file permissions 
                echo ("\nPermissions: ".substr(sprintf("%o",fileperms($dir)),-4)."\n\n");
                return 0;
        }
        else
        {
                echo ("$dir does not exist \n\n");
                return 1;
        }
}
// Checks if user password matches temp
function check_password($user)
{
        $pw="temp";
        // greps encoded password 
        $cmd="more /etc/shadow | grep $user | cut -d: -f2";
        $curr_pw=trim(shell_exec($cmd));
        // enciphers standard password with salt from encoded version
        $pw_encoded=crypt ($pw,substr($curr_pw,0,2));
        if ($curr_pw===$pw_encoded)
        {
                echo ("Password not changed\n\n");
        }
        else
        {
                echo ("Password changed\n\n");
        }
}
// Checks if user directories still exist 
// and if the password is still temp
function consistency_check($in,$user)
{
        $home="/home/$user";
        $web_dir="/home/$user/public_html";
        $ret=0;
        
        cls();
        check_dir ($home);
        check_dir ($web_dir);
        check_password ($user);
        echo ("Press Enter to continue\n");
        fgets($in,100);
}
        
// Creates a database for the user with the user's name 
// Password is temp
function db_create($in, $user)
{
        cls();
        $cmd="mysql --execute=\"create database $user;\" 2>&1";
        exec($cmd,$output,$ret);
        if (0!=$ret)
        {
                echo "Could not create database\n";
                echo "MySQL reports: \n";
                echo (implode("\n",$output));
        }
        else
        {
                echo "Datenbank angelegt\n";
                $cmd="mysql --execute=\"use $user;grant all privileges on $user to $user@\\\"%\\\" identified by 'temp';\"";
                exec($cmd);
                $cmd="mysql --execute=\"use $user;grant all privileges on $user.* to $user@\\\"%\\\" identified by 'temp';\"";
                exec($cmd);
                echo ("Press Enter to continue");
        }       
        echo " Press Enter to continue";
        fgets($in,100);
}

// Deletes user database 
function db_delete($in,$user)
{
        cls();
        $cmd="mysql --execute=\"drop database $user;\"";
        exec($cmd,$output,$ret);
        if (0!=$ret)
        {
                echo "Could not delete database\n";
                echo "MySQL meldet: \n";
                echo (implode("\n",$output));
        }
        else
        {
                $cmd="mysql --execute=\"use mysql; delete from user where user='$user';\"";
                exec ($cmd);
                $cmd="mysql --execute=\"use mysql; delete from tables_priv where user='$user';\"";
                exec ($cmd);
                echo "DB deleted \n";
        }
        echo "Press Enter to continue";
        fgets($in,100);
}

// Creates directory public_html for user 
function webdir_create($in,$user)
{
        cls();
        $cmd="mkdir /home/$user/public_html 2>&1";
        exec($cmd,$output,$ret);
        if (0!=$ret)
        {
                echo "Could not create directory\n";
                echo "Linux reports: \n";
                echo (implode("\n",$output));
        }
        else
        {
                $cmd="chown $user /home/$user/public_html";
                exec($cmd);
                $cmd="chgrp users /home/$user/public_html";
                exec($cmd);
                echo "Created directory\n";
        }
        echo "Press Enter to continue\n";
        fgets($in,100);
}
                
// Create home directory for user 
function homedir_create($in,$user)
{
        cls();
        $cmd="mkdir /home/$user 2>&1";
        exec($cmd,$output,$ret);
        if (0!=$ret)
        {
                echo "Could not create directory\n";
                echo "Linux reports: \n";
                echo (implode("\n",$output));
        }
        else
        {
                $cmd="chown $user /home/$user";
                exec($cmd);
                $cmd="chgrp users /home/$user";
                exec($cmd);
                echo "Created directory\n";
        }
        echo "Press Enter to continue\n";
        fgets($in,100);
}

// Deletes a user including user's database
function del_user($in,$user)
{
        db_delete($in,$user);
        $cmd="userdel -rf $user 2>&1 | grep -v crontab";
        exec ($cmd);
        echo "User $user deleted\n";
        echo "Press Enter to continue\n";
        fgets($in,100);
}

// Resets the user password to temp
function reset_password($in,$user)
{
        cls();
        // greps the original user line from /etc/shadow
        $cmd="grep $user /etc/shadow";
        exec($cmd,$line);
        // Copies the original /etc/shadow without user line 
        $cmd="grep -v $user /etc/shadow > /etc/sha_temp";
        exec($cmd);
        // Encodes password 
        $pwd=crypt("temp","XY");
        $pos1=strpos($line[0],":");
        $pos2=strpos($line[0],":",$pos1+1);
        // Extract user name 
        $newline=substr($line[0],0,$pos1+1);
        // Insert new password 
        $newline.=$pwd;
        // Append rest of old line
        $newline.=substr($line[0],$pos2,strlen($line[0])-$pos2);
        // Append new line to temporary /etc/shadow
        $cmd="echo \"$newline\" >> /etc/sha_temp";
        exec($cmd);
        // Create backup of old file 
        exec ("cp /etc/shadow /etc/sha_backup");
        // overwrite /etc/shadow 
        exec ("mv /etc/sha_temp /etc/shadow");
        echo "Password changed \nPress Enter to continue";
        fgets($in,100); 
}
                
// Function main to prevent exploits with globals
function main($argc, $argv)
{
        $in=fopen("php://stdin","r");
        $err=fopen("php://stderr","w");
        
        // Was a user name passed?
        if (3==$argc && 
            "-u"==$argv[1] && 
            isset($argv[2]))
        {
                $user=$argv[2]; // Parse user name
        } 
        else 
        {   // User name was not passed 
                fputs($err,"Please specify a user name with -u user\n");
                exit(1);
        }
        // Function with /etc/passwd to
        // check if user exists 
        if (false === user_exists($user))
        {
        fputs($err,"User name does not exist\n");
        exit(2);
        }
        
        while (1) // Infinite loop for processing 
        {
                cls();
                text_red();
                echo "Administration for User $user\n";
                text_black();
                echo "1) Consistency check\n";
                echo "2) Create MySQL DB for user\n";
                echo "3) Delete MySQL DB for user\n";
                echo "4) Create web directory for user \n";
                echo "5) Create home directory for user \n";
                echo "6) Reset password\n";
                echo "7) Delete user\n";
                echo "q) Quit Program\n\n";
                echo "Please Choose: ";
                // Trim whitespace from input 
                $input=trim(fgets($in,255));
        
                switch ($input)
                {
                        case "1": consistency_check($in,$user);
                                  break;
                        case "2": db_create($in,$user);
                                  break;
                        case "3": db_delete($in,$user);
                                  break;
                        case "4": webdir_create($in,$user);
                                  break;
                        case "5": homedir_create($in,$user);
                                  break;
                        case "6": reset_password($in,$user);
                                  break;
                        case "7": del_user($in,$user);
                                  break;
                        // Further cases
                        case "q": exit(0);
                
                        // Beep on invalid input
                        default:  echo chr(7); 
                }
        }
} // main

main($argc, $argv);
?>