1
0
mirror of https://github.com/myvesta/vesta.git synced 2025-03-11 03:52:44 -07:00
myvesta/bin/v-open-fs-file

52 lines
1008 B
Plaintext
Raw Permalink Normal View History

2014-07-30 15:41:06 +03:00
#!/bin/bash
2015-07-16 02:21:22 +03:00
# info: open file
# options: USER FILE
#
# The function opens/reads files on the file system
2014-07-30 15:41:06 +03:00
user=$1
2015-08-20 18:01:27 +03:00
src_file=$2
2014-07-30 15:41:06 +03:00
# Checking arguments
2015-08-20 18:01:27 +03:00
if [ -z "$src_file" ]; then
echo "Usage: USER FILE"
2014-07-30 15:41:06 +03:00
exit 1
fi
2015-07-16 02:21:22 +03:00
# Checking vesta user
2014-07-30 15:41:06 +03:00
if [ ! -e "$VESTA/data/users/$user" ]; then
2015-08-20 18:01:27 +03:00
echo "Error: vesta user $user doesn't exist"
exit 3
2014-07-30 15:41:06 +03:00
fi
2015-07-16 02:21:22 +03:00
# Checking user homedir
2014-07-30 15:41:06 +03:00
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
if [ -z $homedir ]; then
2015-08-20 18:01:27 +03:00
echo "Error: user home directory doesn't exist"
exit 12
2014-07-30 15:41:06 +03:00
fi
# Checking path
2015-08-20 18:01:27 +03:00
if [ ! -z "$src_file" ]; then
rpath=$(readlink -f "$src_file")
2015-07-16 02:21:22 +03:00
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
2015-08-20 18:01:27 +03:00
echo "Error: invalid source path $src_file"
exit 2
2014-07-30 15:41:06 +03:00
fi
2016-06-26 16:18:43 +01:00
if [ ! -f "$src_file" ]; then
echo "Error: file not found $src_file"
exit 2
fi
2014-07-30 15:41:06 +03:00
fi
2015-07-16 02:21:22 +03:00
# Reading file
2015-08-20 18:01:27 +03:00
sudo -u $user cat "$src_file" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: file $src_file was not opened"
exit 3
fi
2014-07-30 15:41:06 +03:00
2015-07-16 02:21:22 +03:00
# Exiting
2015-08-20 18:01:27 +03:00
exit