diff --git a/commands/userdel.json b/commands/userdel.json new file mode 100644 index 0000000..7dfc21d --- /dev/null +++ b/commands/userdel.json @@ -0,0 +1,45 @@ +{ + "name" : "userdel", + + "info" : { + "syntax" : "userdel <username>", + "brief" : "Remove a username from the system", + }, + + "action" : function ( arg ) + { + if ( !arg || arg.length == 0 ) + { + return "Usage: " + this.info.syntax + "
\n"; + } + + shell.auto_prompt_refresh = false; + + var users_php = window.location.href; + users_php = users_php.replace ( /\/([a-zA-Z\.]+)$/, '/modules/users/users.php' ); + params = 'action=del&user=' + escape ( arg ); + + var http = new XMLHttpRequest(); + http.open ( "POST", users_php, true ); + http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + http.setRequestHeader("Content-length", params.length); + http.setRequestHeader("Connection", "close"); + + http.onreadystatechange = function () + { + if ( http.readyState == 4 && http.status == 200 ) + { + if ( http.responseText.length > 0 ) + { + shell.cmdOut.innerHTML = http.responseText; + } + + shell.auto_prompt_refresh = true; + shell.refreshPrompt ( false, false ); + } + } + + http.send ( params ); + } +} + diff --git a/modules/users/users.php b/modules/users/users.php index 6879a44..8ed8479 100644 --- a/modules/users/users.php +++ b/modules/users/users.php @@ -70,13 +70,97 @@ switch ( $action ) $perms['can_write'] = $username; $GLOBALS['sudo_cmd'] = true; + print __mkdir ( '/home/'.$username, $perms )."
\n"; set_content ( '/home/'.$username.'/.blashrc', file_get_contents ( '../../system/default_blashrc.json' )); + + include "../../system/files_json.php"; + + if ( !$files_json || strlen ( $files_json ) == 0 ) + { + return 'Error: Empty JSON file container'; + } + + $json = json_decode ( $files_json, true ); + + if ( !$json ) + { + return 'Error: Empty JSON file container'; + } + + for ( $i=0; $i < count ( $json ); $i++ ) + { + if ( $json[$i]['path'] == '/home/'.$username.'/.blashrc' ) + { + $json[$i]['can_read'] = $username; + $json[$i]['can_write'] = $username; + + if ( !( $fp = fopen ( "../../system/files_json.php", "w" ))) + { + return "Unable to write on directories file\n"; + } + + fwrite ( $fp, ""); + fclose ( $fp ); + + break; + } + } + $GLOBALS['sudo_cmd'] = false; print 'User "'.$username.'" successfully added, home directory set to "/home/'.$username."\"\n"; break; + case 'del': + $user= $_REQUEST['user']; + + if ( $user == null ) + { + return false; + } + + $cur_user = getUser(); + + if ( $cur_user != 'root' && $cur_user != $user && !$GLOBALS['sudo_cmd'] ) + { + print "You cannot remove the specified user: Permission denied\n"; + return false; + } + + if ( !( $xml = new SimpleXMLElement ( $xmlcontent ))) + { + print "Unable to open the users XML file\n"; + return false; + } + + $user_found = false; + + for ( $i = 0; $i < count ( $xml->user ) && !$user_found; $i++ ) + { + if ( !strcmp ( $xml->user[$i]['name'], $user )) + { + unset ( $xml->user[$i] ); + $user_found = true; + } + } + + if ( !$user_found ) + { + print "Username not found\n"; + return false; + } + + if ( !( $fp = fopen ( 'userlist.php', 'w' ))) + { + print "Unable to add the specified user, unknown error\n"; + return false; + } + + fwrite ( $fp, 'asXML() . "\nXML;\n\n?>\n" ); + fclose ( $fp ); + break; + case 'login': $username = $_REQUEST['user']; $password = $_REQUEST['pass']; @@ -265,12 +349,6 @@ switch ( $action ) } print set_content ( $file, $content ); - - // If this was a sudo command, for example for creating .blashrc file, - // revoke sudo permissions now - if ( $GLOBALS['sudo_cmd'] == true ) - $GLOBALS['sudo_cmd'] = false; - break; default : diff --git a/system/blash.json b/system/blash.json index 3aec8c6..815d0ee 100644 --- a/system/blash.json +++ b/system/blash.json @@ -73,6 +73,7 @@ "su", "touch", "useradd", + "userdel", "whoami" ] } diff --git a/system/files_json.php b/system/files_json.php index d46a403..b7b5445 100644 --- a/system/files_json.php +++ b/system/files_json.php @@ -29,8 +29,10 @@ $files_json = << * Sample configuration and contents
*/

{
\u0009'banner' : 'Welcome back to blash<br/><br/>',
\u0009'machine' : 'localhost',

\u0009/**
\u0009 * Macros for promptText:
\u0009 * #{xxx} or #{xxxxxx} - use the specified HTML colour
\u0009 * %n - username
\u0009 * %m - machine name
\u0009 * %W - current working directory
\u0009 */
\u0009'promptText' : '[#{008}%n#{888}@#{008}%m#{888} %W] $ '
}

"} ] JSON; -?> +?> \ No newline at end of file