From 75374e264a043c3144eaaf9659babb9a77982049 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Thu, 16 Jul 2015 02:21:22 +0300 Subject: [PATCH] API backend for Web File Manager --- bin/v-add-fs-directory | 37 ++++++++++++ bin/v-add-fs-file | 37 ++++++++++++ bin/v-change-fs-file-permission | 43 ++++++++++++++ bin/v-copy-fs-directory | 49 ++++++++++++++++ bin/v-copy-fs-file | 43 ++++++-------- bin/v-delete-fs-dir | 38 ++++++++++++ bin/v-delete-fs-file | 38 ++++++++++++ bin/v-extract-fs-archive | 100 ++++++++++++++++++++++++++++++++ bin/v-list-fs-directory | 19 +++--- bin/v-move-fs-file | 50 ++++++++++++++++ bin/v-open-fs-file | 19 +++--- 11 files changed, 431 insertions(+), 42 deletions(-) create mode 100755 bin/v-add-fs-directory create mode 100755 bin/v-add-fs-file create mode 100755 bin/v-change-fs-file-permission create mode 100755 bin/v-copy-fs-directory create mode 100755 bin/v-delete-fs-dir create mode 100755 bin/v-delete-fs-file create mode 100755 bin/v-extract-fs-archive create mode 100755 bin/v-move-fs-file diff --git a/bin/v-add-fs-directory b/bin/v-add-fs-directory new file mode 100755 index 00000000..ead1640f --- /dev/null +++ b/bin/v-add-fs-directory @@ -0,0 +1,37 @@ +#!/bin/bash +# info: add directory +# options: USER DIRECTORY +# +# The function creates new directory on the file system + +user=$1 +dst_dir=$2 + +# Checking arguments +if [ -z "$dst_dir" ]; then + echo "Usage: USER DIRECTORY" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_dir") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Adding directory +sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + +# Extiging +exit $? diff --git a/bin/v-add-fs-file b/bin/v-add-fs-file new file mode 100755 index 00000000..d173cda7 --- /dev/null +++ b/bin/v-add-fs-file @@ -0,0 +1,37 @@ +#!/bin/bash +# info: add file +# options: USER FILE +# +# The function creates new files on file system + +user=$1 +dst_file=$2 + +# Checking arguments +if [ -z "$dst_file" ]; then + echo "Usage: USER FILE" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Creating file +sudo -u $user touch $dst_file >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-change-fs-file-permission b/bin/v-change-fs-file-permission new file mode 100755 index 00000000..ce5c923c --- /dev/null +++ b/bin/v-change-fs-file-permission @@ -0,0 +1,43 @@ +#!/bin/bash +# info: change file permission +# options: USER FILE PERMISSIONS +# +# The function changes file access permissions on the file system + +user=$1 +src_file=$2 +permissions=$3 + +# Checking arguments +if [ -z "$permissions" ]; then + echo "Usage: USER FILE PERMISSIONS" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking source file +if [ ! -f "$src_file" ]; then + exit 1 +fi + +# Checking source path +rpath=$(readlink -f "$src_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Changing file permissions +sudo -u $user chmod $permisions $src_file >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-copy-fs-directory b/bin/v-copy-fs-directory new file mode 100755 index 00000000..fd531ef1 --- /dev/null +++ b/bin/v-copy-fs-directory @@ -0,0 +1,49 @@ +#!/bin/bash +# info: copy directory +# options: USER SRC_DIRECTORY DST_DIRECTORY +# +# The function copies directory on the file system + +user=$1 +src_dir=$2 +dst_dir=$3 + +# Checking arguments +if [ -z "$dst_dir" ]; then + echo "Usage: USER SRC_DIRECTORY DST_DIRECTORY" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking source dir +if [ ! -e "$src_dir" ]; then + exit 1 +fi + +# Checking source path +rpath=$(readlink -f "$src_dir") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_dir") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Copying directory +sudo -u $user cp -r $src_dir $dst_dir >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-copy-fs-file b/bin/v-copy-fs-file index a3004654..4f971ec9 100755 --- a/bin/v-copy-fs-file +++ b/bin/v-copy-fs-file @@ -1,58 +1,49 @@ #!/bin/bash -# File copier +# info: copy file +# options: USER SRC_FILE DST_FLE +# +# The function copies file on the file system user=$1 -file_src=$2 -file_dst=$3 +src_file=$2 +dst_file=$3 # Checking arguments -if [ -z "$file_dst" ]; then +if [ -z "$dst_file" ]; then echo "Usage: USER SRC_FILE DST_FILE" exit 1 fi -# Checking users +# Checking vesta user if [ ! -e "$VESTA/data/users/$user" ]; then exit 1 fi -# Checking homedir +# Checking user homedir homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) if [ -z $homedir ]; then exit 1 fi # Checking source file -if [ ! -e "$file_src" ]; then +if [ ! -f "$src_file" ]; then exit 1 fi # Checking source path -rpath=$(readlink -f "$file_src") -if [ -z "$(echo $rpath |grep ^/tmp)" ]; then +rpath=$(readlink -f "$src_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then exit 1 fi # Checking destination path -rpath=$(readlink -f "$file_dst") -if [ -z "$(echo $rpath |grep ^$homedir)" ]; then +rpath=$(readlink -f "$dst_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then exit 1 fi -# Checking dst file permission -if [ -e "$file_dst" ]; then - perms=$(stat --format '%a' $file_dst) -fi - # Copying file -cp $file_src $file_dst +sudo -u $user cp $src_file $dst_file >/dev/null 2>&1 -# Changing ownership -chown $user:$user $file_dst - -# Changin permissions -if [ ! -z "$perms" ]; then - chmod $perms $file_dst -fi - -exit +# Exiting +exit $? diff --git a/bin/v-delete-fs-dir b/bin/v-delete-fs-dir new file mode 100755 index 00000000..258c5e9c --- /dev/null +++ b/bin/v-delete-fs-dir @@ -0,0 +1,38 @@ +#!/bin/bash +# info: delete directory +# options: USER DIRECTORY +# +# The function deletes directory on the file system + + +user=$1 +dst_dir=$2 + +# Checking arguments +if [ -z "$dst_dir" ]; then + echo "Usage: USER DIRECTORY" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_dir") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Deleting directory +sudo -u $user rm -rf $dst_dir >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-delete-fs-file b/bin/v-delete-fs-file new file mode 100755 index 00000000..785ed98b --- /dev/null +++ b/bin/v-delete-fs-file @@ -0,0 +1,38 @@ +#!/bin/bash +# info: delete file +# options: USER FILE +# +# The function deletes file on the file system + + +user=$1 +dst_file=$2 + +# Checking arguments +if [ -z "$dst_file" ]; then + echo "Usage: USER FILE" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Deleting file +sudo -u $user rm -f $dst_file >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-extract-fs-archive b/bin/v-extract-fs-archive new file mode 100755 index 00000000..1c78c1da --- /dev/null +++ b/bin/v-extract-fs-archive @@ -0,0 +1,100 @@ +#!/bin/bash +# info: archive to directory +# options: USER ARCHIVE DIRECTORY +# +# The function extracts archive into directory on the file system + +user=$1 +src_file=$2 +dst_dir=$3 + +# Checking arguments +if [ -z "$dst_dir" ]; then + echo "Usage: USER ARCHIVE DIRECTORY" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking source dir +if [ ! -e "$src_file" ]; then + exit 1 +fi + +# Checking source path +rpath=$(readlink -f "$src_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_dir") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Extracting gziped archive +if [ ! -z "$(echo $src_file |egrep -i '.tgz|.tar.gz')" ]; then + x='yes' + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user tar -xzf $src_file -C $dst_dir >/dev/null 2>&1 + rc=$? +fi + +# Extracting bziped archive +if [ ! -z "$(echo $src_file |egrep -i '.tbz|.tar.bz')" ]; then + x='yes' + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user tar -xjf $src_file -C $dst_dir >/dev/null 2>&1 + rc=$? +fi + +# Extracting gziped file +if [ ! -z "$(echo $src_file |grep -i '.gz')" ] && [ -z "$x" ]; then + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user mv $src_file $dst_dir >/dev/null 2>&1 + sudo -u $user gzip -d $dst_dir/$(basename $src_file) >/dev/null 2>&1 + rc=$? +fi + +# Extracting bziped file +if [ ! -z "$(echo $src_file |grep -i '.bz')" ] && [ -z "$x" ]; then + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user mv $src_file $dst_dir >/dev/null 2>&1 + sudo -u $user bzip2 -d $dst_dir/$(basename $src_file) >/dev/null 2>&1 + rc=$? +fi + +# Extracting ziped archive +if [ ! -z "$(echo $src_file |grep -i '.zip')" ]; then + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user unzip $src_file -d $dst_dir >/dev/null 2>&1 + rc=$? +fi + +# Extracting tared archive +if [ ! -z "$(echo $src_file |grep -i '.tar')" ] && [ -z "$x" ]; then + x='yes' + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user tar -xf $src_file -C $dst_dir >/dev/null 2>&1 + rc=$? +fi + +# Extracting rared archive +if [ ! -z "$(echo $src_file |grep -i '.rar')" ]; then + sudo -u $user mkdir -p $dst_dir >/dev/null 2>&1 + sudo -u $user unrar $src_file $dst_dir >/dev/null 2>&1 + rc=$? +fi + +# Exiting +exit $rc diff --git a/bin/v-list-fs-directory b/bin/v-list-fs-directory index f183d6a8..bf4777e8 100755 --- a/bin/v-list-fs-directory +++ b/bin/v-list-fs-directory @@ -1,5 +1,8 @@ #!/bin/bash -# File list wrapper +# info: list directory +# options: USER DIRECTORY +# +# The function lists directory on the file system user=$1 path=$2 @@ -10,12 +13,12 @@ if [ -z "$user" ]; then exit 1 fi -# Checking users +# Checking vesta user if [ ! -e "$VESTA/data/users/$user" ]; then exit 1 fi -# Checking homedir +# Checking user homedir homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) if [ -z $homedir ]; then exit 1 @@ -23,7 +26,6 @@ fi # Checking path if [ ! -z "$path" ]; then - # Validating absolute path rpath=$(readlink -f "$path") if [ -z "$(echo $rpath |grep $homedir)" ]; then exit 1 @@ -32,8 +34,9 @@ else path=$homedir fi -# Listing files -find "$path" -maxdepth 1 -printf "%y|%m|%TY-%Tm-%Td|%TH:%TM:%TS|%u|%g|%s|%P\n" +# Listing directory +sudo -u $user find "$path" -maxdepth 1 \ + -printf "%y|%m|%TY-%Tm-%Td|%TH:%TM:%TS|%u|%g|%s|%P\n" 2>/dev/null - -exit +# Exiting +exit $? diff --git a/bin/v-move-fs-file b/bin/v-move-fs-file new file mode 100755 index 00000000..ff9fb7d9 --- /dev/null +++ b/bin/v-move-fs-file @@ -0,0 +1,50 @@ +#!/bin/bash +# info: move file +# options: USER SRC_FILE DST_FLE +# +# The function moved file or directory on the file system. This function +# can also be used to rename files just like normal mv command. + +user=$1 +src_file=$2 +dst_file=$3 + +# Checking arguments +if [ -z "$dst_file" ]; then + echo "Usage: USER SRC_FILE DST_FILE" + exit 1 +fi + +# Checking vesta user +if [ ! -e "$VESTA/data/users/$user" ]; then + exit 1 +fi + +# Checking user homedir +homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) +if [ -z $homedir ]; then + exit 1 +fi + +# Checking source file +if [ ! -f "$src_file" ]; then + exit 1 +fi + +# Checking source path +rpath=$(readlink -f "$src_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Checking destination path +rpath=$(readlink -f "$dst_file") +if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then + exit 1 +fi + +# Copying file +sudo -u $user mv $src_file $dst_file >/dev/null 2>&1 + +# Exiting +exit $? diff --git a/bin/v-open-fs-file b/bin/v-open-fs-file index d5c8ea3a..68eeb6a9 100755 --- a/bin/v-open-fs-file +++ b/bin/v-open-fs-file @@ -1,5 +1,8 @@ #!/bin/bash -# File reader +# info: open file +# options: USER FILE +# +# The function opens/reads files on the file system user=$1 path=$2 @@ -10,12 +13,12 @@ if [ -z "$path" ]; then exit 1 fi -# Checking users +# Checking vesta user if [ ! -e "$VESTA/data/users/$user" ]; then exit 1 fi -# Checking homedir +# Checking user homedir homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :) if [ -z $homedir ]; then exit 1 @@ -23,14 +26,14 @@ fi # Checking path if [ ! -z "$path" ]; then - # Validating absolute path rpath=$(readlink -f "$path") - if [ -z "$(echo $rpath |grep $homedir)" ]; then + if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then exit 1 fi fi -cat "$path" - -exit +# Reading file +sudo -u $user cat "$path" +# Exiting +exit $?