forked from extern/egroupware
8afe9094b7
- read rights are not checks in each traversed directory (via sql in a single query to locate the path) - diropen additionally checks for execute rights - fopen checks for read or write depending on the mode - chmod, chgrp, chown methods in sqlfs and egw_vfs/vfs plus an egw_vfs::$is_root var used to grant root rights (no access controll and chown or chgrp without being the owner of a file) - find method (some more params to come) to recursivly search and optionaly execute some callback - egw_vfs::remove doing a "rm -r" / recursive remove or dirs and files - new files or dirs inherit the perms and ownership from the parent directory (no umask) - files/dirs the user has no read rights, in a directory where he has no write rights, get hidden (eg. not showing all the other users / groups home dirs - many new cli commands (chmod, chgrp, chown, find), recursive option for most commands and the ability to use it with root rights, see the usage message if called without options - "cp -r -p" to copy a whole tree incl. ownership and perms, eg. backing up /home to /backup
648 lines
15 KiB
PHP
Executable File
648 lines
15 KiB
PHP
Executable File
#!/usr/bin/php -qC
|
|
<?php
|
|
/**
|
|
* Filemanager - Command line interface: ls
|
|
*
|
|
* @link http://www.egroupware.org
|
|
* @package admin
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
* @copyright (c) 2006 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
* @version $Id$
|
|
*/
|
|
|
|
chdir(dirname(__FILE__)); // to enable our relative pathes to work
|
|
|
|
error_reporting(error_reporting() & ~E_NOTICE);
|
|
|
|
if (isset($_SERVER['HTTP_HOST'])) // security precaution: forbit calling ls as web-page
|
|
{
|
|
die('<h1>'.basename(__FILE__).' must NOT be called as web-page --> exiting !!!</h1>');
|
|
}
|
|
|
|
/**
|
|
* callback if the session-check fails, redirects via xajax to login.php
|
|
*
|
|
* @param array &$account account_info with keys 'login', 'passwd' and optional 'passwd_type'
|
|
* @return boolean/string true if we allow the access and account is set, a sessionid or false otherwise
|
|
*/
|
|
function user_pass_from_argv(&$account)
|
|
{
|
|
$account = $GLOBALS['egw_login_data'];
|
|
//print_r($account);
|
|
if (!($sessionid = $GLOBALS['egw']->session->create($account)))
|
|
{
|
|
echo "Wrong user-account or -password !!!\n\n";
|
|
usage('',1);
|
|
}
|
|
return $sessionid;
|
|
}
|
|
|
|
/**
|
|
* Give a usage message and exit
|
|
*
|
|
* @param string $action=null
|
|
* @param int $ret=0 exit-code
|
|
*/
|
|
function usage($action=null,$ret=0)
|
|
{
|
|
$cmd = basename(__FILE__);
|
|
echo "Usage:\t$cmd [-r|--recursive] URL [URL2 ...]\n";
|
|
echo "\t$cmd --cat URL [URL2 ...]\n";
|
|
echo "\t$cmd --cp [-r|--recursive] [-p|--perms] URL-from URL-to\n";
|
|
echo "\t$cmd --cp [-r|--recursive] [-p|--perms] URL-from [URL-from2 ...] URL-to-directory\n";
|
|
echo "\t$cmd --rm [-r|--recursive] URL [URL2 ...]\n";
|
|
echo "\t$cmd --mkdir [-p|--parents] URL [URL2 ...]\n";
|
|
echo "\t$cmd --rmdir URL [URL2 ...]\n";
|
|
echo "\t$cmd --touch [-r|--recursive] [-d|--date time] URL [URL2 ...]\n";
|
|
echo "\t$cmd --chmod [-r|--recursive] mode=[ugoa]*[+-=][rwx]+,... URL [URL2 ...]\n";
|
|
echo "\t$cmd --chown [-r|--recursive] user URL [URL2 ...]\n";
|
|
echo "\t$cmd --chgrp [-r|--recursive] group URL [URL2 ...]\n";
|
|
echo "\t$cmd --find URL [URL2 ...] [options to come]\n";
|
|
|
|
echo "\nURL: {vfs|sqlfs|oldvfs}://user:password@domain/home/user/file, /dir/file, ...\n";
|
|
|
|
echo "\nUse root_{header-admin|config-user} as user and according password for root access (no user specific access control and chown).\n\n";
|
|
|
|
exit;
|
|
}
|
|
$long = $numeric = $recursive = $perms = false;
|
|
$argv = $_SERVER['argv'];
|
|
$cmd = basename(array_shift($argv),'.php');
|
|
|
|
if (!$argv) $argv = array('-h');
|
|
|
|
foreach($argv as $key => $option)
|
|
{
|
|
if ($option[0] != '-') continue;
|
|
|
|
unset($argv[$key]);
|
|
|
|
switch($option)
|
|
{
|
|
default:
|
|
case '-h': case '--help':
|
|
usage();
|
|
|
|
case '-l': case '--long':
|
|
$long = true;
|
|
continue 2; // switch is counting too!
|
|
|
|
case '-n': case '--numeric':
|
|
$numeric = true;
|
|
continue 2; // switch is counting too!
|
|
|
|
case '-r': case '--recursive':
|
|
$recursive = true;
|
|
continue 2; // switch is counting too!
|
|
|
|
case '-p': case '--parents': case '--perms':
|
|
if ($cmd == 'cp')
|
|
{
|
|
$perms = true;
|
|
}
|
|
else
|
|
{
|
|
$recursive = true;
|
|
}
|
|
continue 2; // switch is counting too!
|
|
|
|
case '-d': case '--date':
|
|
$time = strtotime($argv[$key+1]);
|
|
unset($argv[$key+1]);
|
|
break;
|
|
|
|
case '--cat': // cat files (!) to stdout
|
|
case '--cp': // copy files
|
|
case '--rm': // remove files
|
|
case '--ls': // list files
|
|
case '--rmdir': // remove dirs
|
|
case '--mkdir': // make directories
|
|
case '--rename':// rename
|
|
case '--touch': // touch
|
|
case '--chmod': // chmod
|
|
case '--chown': // chown (requires root)
|
|
case '--chgrp': // chgrp
|
|
case '--find':
|
|
$cmd = substr($option,2);
|
|
continue 2; // switch is counting too!
|
|
}
|
|
}
|
|
$argv = array_values($argv);
|
|
$argc = count($argv);
|
|
|
|
switch($cmd)
|
|
{
|
|
case 'find':
|
|
do_find($argv);
|
|
break;
|
|
|
|
case 'cp':
|
|
do_cp($argv,$recursive,$perms);
|
|
break;
|
|
|
|
case 'rename':
|
|
if (count($argv) != 2) usage(null,3);
|
|
load_wrapper($argv[0]);
|
|
load_wrapper($argv[1]);
|
|
rename($argv[0],$argv[1]);
|
|
break;
|
|
|
|
default:
|
|
while($argv)
|
|
{
|
|
$url = array_shift($argv);
|
|
|
|
load_wrapper($url);
|
|
//echo "$cmd $url (long=".(int)$long.", numeric=".(int)$numeric.")\n";
|
|
|
|
switch($cmd)
|
|
{
|
|
case 'rm':
|
|
if ($recursive && class_exists('egw_vfs'))
|
|
{
|
|
array_unshift($argv,$url);
|
|
egw_vfs::remove($argv);
|
|
$argv = array();
|
|
}
|
|
else
|
|
{
|
|
unlink($url);
|
|
}
|
|
break;
|
|
|
|
case 'rmdir':
|
|
rmdir($url);
|
|
break;
|
|
|
|
case 'mkdir':
|
|
mkdir($url,null,$recursive);
|
|
break;
|
|
|
|
case 'touch':
|
|
case 'chmod':
|
|
case 'chown':
|
|
case 'chgrp':
|
|
switch($cmd)
|
|
{
|
|
case 'touch':
|
|
$params = array($url,$time);
|
|
break;
|
|
case 'chmod':
|
|
if (!isset($mode))
|
|
{
|
|
$mode = $url; // first param is mode
|
|
$url = array_shift($argv);
|
|
}
|
|
if (parse_url($url,PHP_URL_SCHEME)) load_wrapper($url); // cant use stat or egw_vfs::mode2int otherwise!
|
|
|
|
if (strpos($mode,'+') !== false || strpos($mode,'-') !== false)
|
|
{
|
|
$stat = stat($url);
|
|
$set = $stat['mode'];
|
|
}
|
|
else
|
|
{
|
|
$set = 0;
|
|
}
|
|
if (!class_exists('egw_vfs'))
|
|
{
|
|
die("chmod only implemented for eGW streams!"); // dont want to repeat the code here
|
|
}
|
|
$set = egw_vfs::mode2int($mode,$set);
|
|
$params = array($url,$set);
|
|
break;
|
|
case 'chown':
|
|
case 'chgrp':
|
|
$type = $cmd == 'chgrp' ? 'group' : 'user';
|
|
if (!isset($owner))
|
|
{
|
|
$owner = $url; // first param is owner/group
|
|
$url = array_shift($argv);
|
|
if (parse_url($url,PHP_URL_SCHEME)) load_wrapper($url); // we need the header loaded
|
|
if ($owner == 'root')
|
|
{
|
|
$owner = 0;
|
|
}
|
|
elseif (!is_numeric($owner))
|
|
{
|
|
if (!is_object($GLOBALS['egw']))
|
|
{
|
|
die("only numeric user/group-id's allowed for non eGW streams!");
|
|
}
|
|
if (!($owner = $GLOBALS['egw']->accounts->name2id($owner_was=$owner,'account_lid',$type[0])) ||
|
|
($owner < 0) != ($cmd == 'chgrp'))
|
|
{
|
|
die("Unknown $type '$owner_was'!");
|
|
}
|
|
}
|
|
elseif($owner && is_object($GLOBALS['egw']) && (!$GLOBALS['egw']->accounts->id2name($owner) ||
|
|
($owner < 0) != ($cmd == 'chgrp')))
|
|
{
|
|
die("Unknown $type '$owner_was'!");
|
|
}
|
|
}
|
|
$params = array($url,$owner);
|
|
break;
|
|
}
|
|
if (($scheme = parse_url($url,PHP_URL_SCHEME)))
|
|
{
|
|
load_wrapper($url);
|
|
if (class_exists($class = $scheme.'_stream_wrapper') && method_exists($class,'touch'))
|
|
{
|
|
$cmd = array($scheme.'_stream_wrapper',$cmd);
|
|
}
|
|
else
|
|
{
|
|
die("Can't $cmd for scheme $scheme!\n");
|
|
}
|
|
}
|
|
if ($recursive && class_exists('egw_vfs'))
|
|
{
|
|
array_unshift($argv,$url);
|
|
$params = array($argv,null,$cmd,$params[1]);
|
|
$cmd = array('egw_vfs','find');
|
|
$argv = array(); // we processed all url's
|
|
}
|
|
//echo "calling cmd=".print_r($cmd,true).", params=".print_r($params,true)."\n";
|
|
call_user_func_array($cmd,$params);
|
|
break;
|
|
|
|
case 'cat':
|
|
case 'ls':
|
|
default:
|
|
// recursive ls atm only for vfs://
|
|
if ($cmd != 'cat' && $recursive && class_exists('egw_vfs'))
|
|
{
|
|
load_wrapper($url);
|
|
array_unshift($argv,$url);
|
|
egw_vfs::find($argv,array('dirs_last'=>true),'do_stat',array($long,$numeric,true));
|
|
$argv = array();
|
|
}
|
|
elseif (is_dir($url) && ($dir = opendir($url)))
|
|
{
|
|
if ($argc)
|
|
{
|
|
echo "\n".basename(parse_url($url,PHP_URL_PATH)).":\n";
|
|
}
|
|
while(($file = readdir($dir)) !== false)
|
|
{
|
|
do_stat($url.'/'.$file,$long,$numeric);
|
|
}
|
|
closedir($dir);
|
|
}
|
|
elseif ($cmd == 'cat')
|
|
{
|
|
if (!($f = fopen($url,'r')))
|
|
{
|
|
echo "File $url not found !!!\n\n";
|
|
}
|
|
else
|
|
{
|
|
if ($argc)
|
|
{
|
|
echo "\n".basename(parse_url($url,PHP_URL_PATH)).":\n";
|
|
}
|
|
fpassthru($f);
|
|
fclose($f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
do_stat($url,$long,$numeric);
|
|
}
|
|
if (!$long && $cmd == 'ls') echo "\n";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load the necessary wrapper for an url
|
|
*
|
|
* @param string $url
|
|
*/
|
|
function load_wrapper($url)
|
|
{
|
|
switch($scheme = parse_url($url,PHP_URL_SCHEME))
|
|
{
|
|
case 'webdav':
|
|
require_once('HTTP/WebDAV/Client.php');
|
|
break;
|
|
case 'oldvfs':
|
|
case 'vfs':
|
|
case 'sqlfs':
|
|
if (!isset($GLOBALS['egw_info']))
|
|
{
|
|
$_GET['domain'] = parse_url($url,PHP_URL_HOST);
|
|
$GLOBALS['egw_login_data'] = array(
|
|
'login' => parse_url($url,PHP_URL_USER),
|
|
'passwd' => parse_url($url,PHP_URL_PASS),
|
|
'passwd_type' => 'text',
|
|
);
|
|
|
|
if (is_dir('/tmp')) ini_set('session.save_path','/tmp'); // regular users may have no rights to apache's session dir
|
|
|
|
$GLOBALS['egw_info'] = array(
|
|
'flags' => array(
|
|
'currentapp' => 'filemanager',
|
|
'noheader' => true,
|
|
'autocreate_session_callback' => 'user_pass_from_argv',
|
|
)
|
|
);
|
|
|
|
if (substr($GLOBALS['egw_login_data']['login'],0,5) != 'root_')
|
|
{
|
|
include('../header.inc.php');
|
|
}
|
|
else
|
|
{
|
|
$GLOBALS['egw_info']['flags']['currentapp'] = 'login';
|
|
include('../header.inc.php');
|
|
|
|
if ($GLOBALS['egw_login_data']['login'] == 'root_'.$GLOBALS['egw_info']['server']['header_admin_user'] &&
|
|
_check_pw($GLOBALS['egw_info']['server']['header_admin_password'],$GLOBALS['egw_login_data']['passwd']) ||
|
|
$GLOBALS['egw_login_data']['login'] == 'root_'.$GLOBALS['egw_domain'][$_GET['domain']]['config_user'] &&
|
|
_check_pw($GLOBALS['egw_domain'][$_GET['domain']]['config_passwd'],$GLOBALS['egw_login_data']['passwd']))
|
|
{
|
|
echo "\nRoot access granted!\n";
|
|
egw_vfs::$is_root = true;
|
|
}
|
|
else
|
|
{
|
|
die("Unknown user or password!\n");
|
|
}
|
|
set_exception_handler('cli_exception_handler'); // otherwise we get html!
|
|
}
|
|
}
|
|
require_once(EGW_API_INC.'/class.'.$scheme.'_stream_wrapper.inc.php');
|
|
break;
|
|
case '': // default scheme is file and alsways available
|
|
break;
|
|
default:
|
|
if (!in_array($scheme,stream_get_wrappers()))
|
|
{
|
|
die("Unknown scheme '$scheme' in $url !!!\n\n");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check password against a md5 hash or cleartext password
|
|
*
|
|
* @param string $hash_or_cleartext
|
|
* @param string $pw
|
|
* @return boolean
|
|
*/
|
|
function _check_pw($hash_or_cleartext,$pw)
|
|
{
|
|
//echo "_check_pw($hash_or_cleartext,$pw) md5=".md5($pw)."\n";
|
|
if (preg_match('/^[0-9a-f]{32}$/',$hash_or_cleartext))
|
|
{
|
|
return $hash_or_cleartext == md5($pw);
|
|
}
|
|
return $hash_or_cleartext == $pw;
|
|
}
|
|
|
|
/**
|
|
* Give the stats for one file
|
|
*
|
|
* @param string $url
|
|
* @param boolean $long=false true=long listing with owner,group,size,perms, default false only filename
|
|
* @param boolean $numeric=false true=give numeric uid&gid, else resolve the id to a name
|
|
*/
|
|
function do_stat($url,$long=false,$numeric=false,$full_path=false)
|
|
{
|
|
//echo "do_stat($url,$long,$numeric,$full_path)\n";
|
|
$bname = parse_url($url,PHP_URL_PATH);
|
|
|
|
if (!$full_path)
|
|
{
|
|
$bname = basename($bname);
|
|
}
|
|
if ($long && ($stat = stat($url)))
|
|
{
|
|
//echo $url; print_r($stat);
|
|
|
|
if (class_exists('egw_vfs'))
|
|
{
|
|
$perms = egw_vfs::int2mode($stat['mode']);
|
|
}
|
|
else
|
|
{
|
|
$perms = int2mode($stat['mode']);
|
|
}
|
|
if ($numeric)
|
|
{
|
|
$uid = $stat['uid'];
|
|
$gid = $stat['gid'];
|
|
}
|
|
else
|
|
{
|
|
if ($stat['uid'])
|
|
{
|
|
$uid = isset($GLOBALS['egw']) ? $GLOBALS['egw']->accounts->id2name($stat['uid']) :
|
|
(function_exists('posix_getpwuid') ? posix_getpwuid($stat['uid']) : $stat['uid']);
|
|
if (is_array($uid)) $uid = $uid['name'];
|
|
if (empty($uid)) $uid = $stat['uid'];
|
|
}
|
|
if (!isset($uid)) $uid = 'root';
|
|
if ($stat['gid'])
|
|
{
|
|
$gid = isset($GLOBALS['egw']) ? $GLOBALS['egw']->accounts->id2name(-abs($stat['gid'])) :
|
|
(function_exists('posix_getgrgid') ? posix_getgrgid($stat['gid']) : $stat['gid']);
|
|
if (is_array($gid)) $gid = $gid['name'];
|
|
if (empty($gid)) $gid = $stat['gid'];
|
|
}
|
|
if (!isset($gid)) $gid = 'root';
|
|
}
|
|
$size = hsize($stat['size']);
|
|
$mtime = date('Y-m-d H:i:s',$stat['mtime']);
|
|
$nlink = $stat['nlink'];
|
|
|
|
echo "$perms $nlink\t$uid\t$gid\t$size\t$mtime\t$bname\n";
|
|
}
|
|
else
|
|
{
|
|
echo "$bname\t";
|
|
}
|
|
}
|
|
|
|
function hsize($size)
|
|
{
|
|
if ($size < 1024) return $size;
|
|
if ($size < 1024*1024) return sprintf('%3.1lfk',(float)$size/1024);
|
|
return sprintf('%3.1lfM',(float)$size/(1024*1024));
|
|
}
|
|
|
|
|
|
function do_cp($argv,$recursive=false,$perms=false)
|
|
{
|
|
$to = array_pop($argv);
|
|
load_wrapper($to);
|
|
|
|
$to_exists = file_exists($to);
|
|
|
|
if (count($argv) > 1 && $to_exists && !is_dir($to))
|
|
{
|
|
usage(null,4);
|
|
}
|
|
foreach($argv as $from)
|
|
{
|
|
if (is_dir($from) && (!file_exists($to) || is_dir($to)) && $recursive && class_exists('egw_vfs'))
|
|
{
|
|
foreach(egw_vfs::find($from) as $f)
|
|
{
|
|
$t = $to.substr($f,strlen($from));
|
|
if (is_dir($f))
|
|
{
|
|
++$anz_dirs;
|
|
mkdir($t);
|
|
}
|
|
else
|
|
{
|
|
++$anz_files;
|
|
_cp($f,$t);
|
|
}
|
|
if ($perms) _cp_perms($f,$t);
|
|
}
|
|
echo ($anz_dirs?"$anz_dirs dir(s) created and ":'')."$anz_files file(s) copied.\n";
|
|
}
|
|
else
|
|
{
|
|
_cp($from,$to,true);
|
|
if ($perms) _cp_perms($from,$to);
|
|
}
|
|
}
|
|
}
|
|
|
|
function _cp($from,$to,$verbose=false,$perms=false)
|
|
{
|
|
load_wrapper($from);
|
|
|
|
if (is_dir($to) || !file_exists($to) && is_dir($from) && $mkdir)
|
|
{
|
|
$path = parse_url($from,PHP_URL_PATH);
|
|
if (is_dir($to)) $to .= '/'.basename($path);
|
|
}
|
|
if (!($from_fp = fopen($from,'r')))
|
|
{
|
|
die("File $from not found!\n");
|
|
}
|
|
if (!($to_fp = fopen($to,'w')))
|
|
{
|
|
die("Can't open $to for writing!\n");
|
|
}
|
|
$count = stream_copy_to_stream($from_fp,$to_fp);
|
|
|
|
if ($verbose) echo hsize($count)." bytes written to $to\n";
|
|
|
|
fclose($from_fp);
|
|
|
|
if (!fclose($to_fp))
|
|
{
|
|
die("Error closing $to!\n");
|
|
}
|
|
}
|
|
|
|
|
|
function _cp_perms($from,$to)
|
|
{
|
|
if (($from_stat = stat($from)) && ($to_stat = stat($to)))
|
|
{
|
|
foreach(array(
|
|
'mode' => 'chmod',
|
|
'uid' => 'chown',
|
|
'gid' => 'chgrp',
|
|
) as $perm => $cmd)
|
|
{
|
|
if ($from_stat[$perm] != $to_stat[$perm])
|
|
{
|
|
//echo "egw_vfs::$cmd($to,{$from_stat[$perm]}\n";
|
|
call_user_func(array('egw_vfs',$cmd),$to,$from_stat[$perm]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function do_find($bases)
|
|
{
|
|
foreach($bases as $url)
|
|
{
|
|
load_wrapper($url);
|
|
}
|
|
foreach(egw_vfs::find($bases) as $path)
|
|
{
|
|
echo "$path\n";
|
|
}
|
|
}
|
|
|
|
function cli_exception_handler(Exception $e)
|
|
{
|
|
echo $e->getMessage()."\n";
|
|
exit($e->getCode());
|
|
}
|
|
|
|
/**
|
|
* Convert a numerical mode to a symbolic mode-string
|
|
*
|
|
* @param int $mode
|
|
* @return string
|
|
*/
|
|
function int2mode( $mode )
|
|
{
|
|
if($mode & 0x1000) // FIFO pipe
|
|
{
|
|
$sP = 'p';
|
|
}
|
|
elseif($mode & 0x2000) // Character special
|
|
{
|
|
$sP = 'c';
|
|
}
|
|
elseif($mode & 0x4000) // Directory
|
|
{
|
|
$sP = 'd';
|
|
}
|
|
elseif($mode & 0x6000) // Block special
|
|
{
|
|
$sP = 'b';
|
|
}
|
|
elseif($mode & 0x8000) // Regular
|
|
{
|
|
$sP = '-';
|
|
}
|
|
elseif($mode & 0xA000) // Symbolic Link
|
|
{
|
|
$sP = 'l';
|
|
}
|
|
elseif($mode & 0xC000) // Socket
|
|
{
|
|
$sP = 's';
|
|
}
|
|
else // UNKNOWN
|
|
{
|
|
$sP = 'u';
|
|
}
|
|
|
|
// owner
|
|
$sP .= (($mode & 0x0100) ? 'r' : '-') .
|
|
(($mode & 0x0080) ? 'w' : '-') .
|
|
(($mode & 0x0040) ? (($mode & 0x0800) ? 's' : 'x' ) :
|
|
(($mode & 0x0800) ? 'S' : '-'));
|
|
|
|
// group
|
|
$sP .= (($mode & 0x0020) ? 'r' : '-') .
|
|
(($mode & 0x0010) ? 'w' : '-') .
|
|
(($mode & 0x0008) ? (($mode & 0x0400) ? 's' : 'x' ) :
|
|
(($mode & 0x0400) ? 'S' : '-'));
|
|
|
|
// world
|
|
$sP .= (($mode & 0x0004) ? 'r' : '-') .
|
|
(($mode & 0x0002) ? 'w' : '-') .
|
|
(($mode & 0x0001) ? (($mode & 0x0200) ? 't' : 'x' ) :
|
|
(($mode & 0x0200) ? 'T' : '-'));
|
|
|
|
return $sP;
|
|
}
|