blash/modules/users/users.php

424 lines
8.2 KiB
PHP
Raw Permalink Normal View History

2010-12-25 13:27:38 +01:00
<?php
include 'userlist.php';
include 'user_utils.php';
2010-12-26 18:08:43 +01:00
2010-12-30 23:05:42 +01:00
global $sudo_cmd;
2010-12-25 13:27:38 +01:00
$action = $_REQUEST['action'];
if ( $action == null )
{
die ("");
}
switch ( $action )
{
case 'add':
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
if ( !( $username != null && $password != null ))
{
die ("");
}
if ( preg_match ( '/[^a-zA-Z0-9_]/', $username ))
{
print "The username can only contain characters in the charset '[a-zA-Z0-9_]'\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 13:27:38 +01:00
}
2010-12-30 23:05:42 +01:00
if ( preg_match ( '/[^a-fA-F0-9]/', $password ) || strlen ( $password ) != 32 )
2010-12-25 13:27:38 +01:00
{
2010-12-25 21:52:37 +01:00
print "The provided password is not a valid hash\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 13:27:38 +01:00
}
if ( !( $xml = new SimpleXMLElement ( $xmlcontent )))
{
print "Unable to open the users XML file\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 13:27:38 +01:00
}
for ( $i = 0; $i < count ( $xml->user ); $i++ )
{
if ( !strcasecmp ( $xml->user[$i]['name'], $username ))
{
print "The specified user already exists\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 13:27:38 +01:00
}
}
$newuser = $xml->addChild ( 'user' );
$newuser->addAttribute ( 'name', $username );
$newuser->addAttribute ( 'pass', $password );
$newuser->addAttribute ( 'home', '/home/' . $username );
if ( !( $fp = fopen ( 'userlist.php', 'w' )))
{
print "Unable to add the specified user, unknown error\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 13:27:38 +01:00
}
fwrite ( $fp, '<?php'."\n\n".'$xmlcontent = <<<XML'."\n" . $xml->asXML() . "\nXML;\n\n?>\n" );
2010-12-25 13:27:38 +01:00
fclose ( $fp );
2010-12-30 23:05:42 +01:00
$perms = array();
$perms['owner'] = $username;
$perms['can_read'] = $username;
$perms['can_write'] = $username;
$GLOBALS['sudo_cmd'] = true;
2011-01-07 18:07:17 +01:00
2010-12-30 23:05:42 +01:00
print __mkdir ( '/home/'.$username, $perms )."<br/>\n";
2011-01-07 11:42:38 +01:00
set_content ( '/home/'.$username.'/.blashrc', file_get_contents ( '../../system/default_blashrc.json' ));
2011-01-07 22:54:07 +01:00
set_content ( '/home/'.$username.'/.stylerc', file_get_contents ( '../../system/default_stylerc.css' ));
2011-01-07 18:07:17 +01:00
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++ )
{
2011-01-07 22:54:07 +01:00
if ( $json[$i]['path'] == '/home/'.$username.'/.blashrc'
|| $json[$i]['path'] == '/home/'.$username.'/.stylerc' )
2011-01-07 18:07:17 +01:00
{
$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 );
}
}
2010-12-30 23:05:42 +01:00
$GLOBALS['sudo_cmd'] = false;
print 'User "'.$username.'" successfully added, home directory set to "/home/'.$username."\"\n";
2010-12-25 13:27:38 +01:00
break;
2010-12-25 21:52:37 +01:00
2011-01-07 18:07:17 +01:00
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;
2010-12-25 21:52:37 +01:00
case 'login':
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
if ( !( $username != null && $password != null ))
{
die ("");
}
if ( preg_match ( '/[^a-zA-Z0-9_]/', $username ))
{
print "The username can only contain characters in the charset '[a-zA-Z0-9_]'\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 21:52:37 +01:00
}
if ( !( $xml = new SimpleXMLElement ( $xmlcontent )))
{
print "Unable to open the users XML file\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 21:52:37 +01:00
}
for ( $i = 0; $i < count ( $xml->user ) && !$found; $i++ )
{
if ( !strcasecmp ( $xml->user[$i]['name'], $username ))
{
if ( strcasecmp ( $xml->user[$i]['pass'], $password ))
{
print "Wrong password provided for user '$username'\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 21:52:37 +01:00
} else {
$auth = md5 ( $xml->user[$i]['name'] . $xml->user[$i]['pass'] );
setcookie ( 'username', $xml->user[$i]['name'], 0, "/" );
setcookie ( 'auth', $auth, 0, "/" );
print "Successfully logged in as '$username'\n";
2010-12-25 21:52:37 +01:00
return 0;
}
}
}
print "Username not found: '$username'\n";
break;
case 'getuser':
2010-12-26 18:08:43 +01:00
print getUser();
2010-12-31 03:00:35 +01:00
break;
case 'gethome':
print getHome();
2010-12-26 18:08:43 +01:00
break;
case 'logout':
setcookie ( 'username', '', 0, "/" );
setcookie ( 'auth', '', 0, "/" );
break;
case 'changepwd':
$old_pass = $_REQUEST['oldpass'];
$new_pass = $_REQUEST['newpass'];
$user = $_REQUEST['user'];
$cur_user = getUser();
// If the current user is not root and he's trying to change someone else's password, STOP HIM!
if ( $cur_user != 'root' && $cur_user != $user )
2010-12-25 21:52:37 +01:00
{
2010-12-26 18:08:43 +01:00
print "You cannot change the password for the user '$user'\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-26 18:08:43 +01:00
}
2010-12-25 21:52:37 +01:00
2010-12-26 18:08:43 +01:00
if ( !( $xml = new SimpleXMLElement ( $xmlcontent )))
{
print "Unable to open the users XML file\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-26 18:08:43 +01:00
}
for ( $i = 0; $i < count ( $xml->user ); $i++ )
{
// If we've found the user whose password should be changed...
if ( !strcasecmp ( $xml->user[$i]['name'], $user ))
2010-12-25 21:52:37 +01:00
{
2010-12-26 18:08:43 +01:00
$found = true;
2010-12-25 21:52:37 +01:00
2010-12-26 18:08:43 +01:00
// If the current user is not root, check his own inserted current password
if ( $cur_user != 'root' )
{
if ( $xml->user[$i]['pass'] != $old_pass )
2010-12-25 21:52:37 +01:00
{
2010-12-26 18:08:43 +01:00
print "The provided current password is wrong\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-25 21:52:37 +01:00
}
}
2010-12-26 18:08:43 +01:00
$xml->user[$i]['pass'] = $new_pass;
if ( !( $fp = fopen ( 'userlist.php', 'w' )))
{
print "Unable to change the password for the specified user, unknown error\n";
2010-12-30 23:05:42 +01:00
return '';
2010-12-26 18:08:43 +01:00
}
fwrite ( $fp, "<?php\n\n\$xmlcontent = <<<XML\n" . $xml->asXML() . "\nXML;\n\n?>\n" );
fclose ( $fp );
print 'Password successfully changed for the user '.$user."\n";
return 0;
}
2010-12-25 21:52:37 +01:00
}
break;
case 'getperms':
$res = $_REQUEST['resource'];
if ( !$res )
{
return false;
}
print getPerms ( $res );
break;
2010-12-30 23:05:42 +01:00
case 'mkdir':
$dir = $_REQUEST['dir'];
if ( !$dir )
{
return false;
}
print __mkdir ( $dir, null );
break;
case 'rmdir':
$dir = $_REQUEST['dir'];
if ( !$dir )
{
return false;
}
print __rmdir ( $dir );
break;
2010-12-31 03:00:35 +01:00
2010-12-31 03:24:38 +01:00
case 'touch':
$file = $_REQUEST['file'];
if ( !$file )
{
return false;
}
print __touch ( $file, null );
break;
case 'rm':
$file = $_REQUEST['file'];
if ( !$file )
{
return false;
}
print __rm ( $file );
break;
2011-01-07 11:42:38 +01:00
case 'chmod':
$resource = $_REQUEST['resource'];
$perms = $_REQUEST['perms'];
$userlist = $_REQUEST['userlist'];
if ( !( $resource && $perms ))
{
return false;
}
print __chmod ( $resource, (( $userlist ) ? $userlist : null ), $perms );
break;
2011-01-01 17:25:28 +01:00
case 'set_content':
$file = $_REQUEST['file'];
$content = $_REQUEST['content'];
if ( !( $file && $content ))
{
return false;
}
print set_content ( $file, $content );
break;
case 'list':
if ( !( $xml = new SimpleXMLElement ( $xmlcontent )))
{
print "Unable to open the users XML file\n";
return false;
}
$users = array();
for ( $i = 0; $i < count ( $xml->user ); $i++ )
{
array_push ( $users, $xml->user[$i]['name'] );
}
sort ( $users, SORT_STRING );
foreach ( $users as $i => $user )
{
print $user;
if ( $i < count ( $users ) - 1 )
{
print "<br/>";
}
print "\n";
}
break;
2011-01-10 22:31:56 +01:00
case 'link':
$resource = $_REQUEST['resource'];
$link = $_REQUEST['link'];
$type = $_REQUEST['type'];
if ( !( $resource && $link && $type ))
{
return false;
}
print __link ( $resource, $link, $type );
break;
2011-01-11 12:18:45 +01:00
case 'cp':
case 'mv':
$src = $_REQUEST['src'];
$dest = $_REQUEST['dest'];
if ( !( $src && $dest ))
{
return false;
}
print __cp ( $src, $dest );
if ( $action == 'mv' )
{
print __rm ( $src );
}
break;
2010-12-31 03:00:35 +01:00
default :
print "Unallowed action\n";
break;
2010-12-25 13:27:38 +01:00
}
2010-12-31 03:00:35 +01:00
return "";
2010-12-25 13:27:38 +01:00
?>