userdel implemented

This commit is contained in:
BlackLight 2011-01-07 18:07:17 +01:00
parent ba0a57c469
commit bc50ada7d9
4 changed files with 133 additions and 7 deletions

45
commands/userdel.json Normal file
View file

@ -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 + "<br/>\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 );
}
}

View file

@ -70,13 +70,97 @@ switch ( $action )
$perms['can_write'] = $username;
$GLOBALS['sudo_cmd'] = true;
print __mkdir ( '/home/'.$username, $perms )."<br/>\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, "<?php\n\n\$files_json = <<<JSON\n".__json_encode ( $json )."\nJSON;\n\n?>");
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, '<?php'."\n\n".'$xmlcontent = <<<XML'."\n" . $xml->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 :

View file

@ -73,6 +73,7 @@
"su",
"touch",
"useradd",
"userdel",
"whoami"
]
}

View file

@ -29,8 +29,10 @@ $files_json = <<<JSON
, {"path": "/contacts", "type": "file", "content": "Contact me at spam@montypython.com"}
, {"path": "/irc", "type": "file", "content": "IRC channel at #thegame@irc.randomstuff.com"}
, {"path": "/root", "type": "directory", "can_write": "root", "can_read": "root"}
, {"path": "/home/blacklight", "type": "directory", "owner": "blacklight", "can_read": "blacklight", "can_write": "blacklight"}
, {"path": "/home/blacklight/.blashrc", "type": "file", "owner": "guest", "can_read": "blacklight", "can_write": "blacklight", "content": "/**<br/> * Sample configuration and contents<br/> */<br/><br/>{<br/>\u0009'banner' : 'Welcome back to blash&lt;br/&gt;&lt;br/&gt;',<br/>\u0009'machine' : 'localhost',<br/><br/>\u0009/**<br/>\u0009 * Macros for promptText:<br/>\u0009 * #{xxx} or #{xxxxxx} - use the specified HTML colour<br/>\u0009 * %n - username<br/>\u0009 * %m - machine name<br/>\u0009 * %W - current working directory<br/>\u0009 */<br/>\u0009'promptText' : '[#{008}%n#{888}@#{008}%m#{888} %W] $ '<br/>}<br/><br/>"}
]
JSON;
?>
?>