Skip to content

Commit 62201cf

Browse files
committed
Fix input validation on file system cli scripts
1 parent 4df2132 commit 62201cf

16 files changed

+183
-200
lines changed

bin/v-add-fs-archive

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
user=$1
88
archive=$2
9-
src0=$3
109

11-
# Checking arguments
12-
if [ -z "$src0" ]; then
13-
echo "Usage: USER ARCHIVE FILE [FILE_2] [FILE_3] [FILE ...]"
14-
exit 1
15-
fi
10+
# Includes
11+
source $HESTIA/func/main.sh
1612

17-
# Checking hestia user
18-
if [ ! -d "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
13+
#----------------------------------------------------------#
14+
# Verifications #
15+
#----------------------------------------------------------#
16+
17+
check_args '3' "$#" 'USER ARCHIVE FILE [FILE_2] [FILE_3] [FILE ...]'
18+
is_format_valid 'user'
19+
is_object_valid 'user' 'USER' "$user"
2220

2321
# Checking user homedir
2422
homedir=$(grep "^$user:" /etc/passwd |cut -f 6 -d :)
@@ -54,7 +52,7 @@ for src in $*; do
5452
src=$(echo "$src"| sed -e "s|/home/$user/||")
5553

5654
# Creating tar.gz archive
57-
sudo -u $user tar -rf "${archive/.gz/}" -C /home/$user $src >\
55+
sudo -u $user -- tar -rf "${archive/.gz/}" -C /home/$user $src >\
5856
/dev/null 2>&1
5957
if [ "$?" -ne 0 ]; then
6058
echo "Error: archive $archive was not created"
@@ -66,7 +64,7 @@ done
6664

6765
# Checking gzip
6866
if [[ "$archive" =~ \.gz$ ]]; then
69-
sudo -u $user gzip "${archive/.gz/}" >/dev/null 2>&1
67+
sudo -u $user -- gzip "${archive/.gz/}" >/dev/null 2>&1
7068
if [ "$?" -ne 0 ]; then
7169
echo "Error: archive $archive was not gziped"
7270
exit 3

bin/v-add-fs-directory

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
user=$1
88
dst_dir=$2
99

10-
# Checking arguments
11-
if [ -z "$dst_dir" ]; then
12-
echo "Usage: USER DIRECTORY"
13-
exit 1
14-
fi
10+
# Includes
11+
source $HESTIA/func/main.sh
1512

16-
# Checking hestia user
17-
if [ ! -d "$HESTIA/data/users/$user" ]; then
18-
echo "Error: hestia user $user doesn't exist"
19-
exit 3
20-
fi
13+
#----------------------------------------------------------#
14+
# Verifications #
15+
#----------------------------------------------------------#
16+
17+
check_args '2' "$#" 'USER DIR'
18+
is_format_valid 'user'
19+
is_object_valid 'user' 'USER' "$user"
2120

2221
# Checking user homedir
2322
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -34,11 +33,11 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
3433
fi
3534

3635
# Adding directory
37-
sudo -u $user mkdir -p "$dst_dir" >/dev/null 2>&1
36+
sudo -u $user -- mkdir -p "$dst_dir" >/dev/null 2>&1
3837
if [ $? -ne 0 ]; then
3938
echo "Error: directory $dst_dir was not created"
4039
exit 3
4140
fi
4241

43-
# Extiging
42+
# Exiting
4443
exit

bin/v-add-fs-file

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
user=$1
88
dst_file=$2
99

10-
# Checking arguments
11-
if [ -z "$dst_file" ]; then
12-
echo "Usage: USER FILE"
13-
exit 1
14-
fi
10+
# Includes
11+
source $HESTIA/func/main.sh
1512

16-
# Checking hestia user
17-
if [ ! -d "$HESTIA/data/users/$user" ]; then
18-
echo "Error: hestia user $user doesn't exist"
19-
exit 3
20-
fi
13+
#----------------------------------------------------------#
14+
# Verifications #
15+
#----------------------------------------------------------#
16+
17+
check_args '2' "$#" 'USER FILE'
18+
is_format_valid 'user'
19+
is_object_valid 'user' 'USER' "$user"
2120

2221
# Checking user homedir
2322
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -34,7 +33,7 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
3433
fi
3534

3635
# Creating file
37-
sudo -u $user touch "$dst_file" >/dev/null 2>&1
36+
sudo -u $user -- touch "$dst_file" >/dev/null 2>&1
3837
if [ $? -ne 0 ]; then
3938
echo "Error: file $dst_file was not created"
4039
exit 3

bin/v-change-fs-file-permission

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ user=$1
88
src_file=$2
99
permissions=$3
1010

11-
# Checking arguments
12-
if [ -z "$permissions" ]; then
13-
echo "Usage: USER FILE PERMISSIONS"
14-
exit 1
15-
fi
11+
# Includes
12+
source $HESTIA/func/main.sh
1613

17-
# Checking hestia user
18-
if [ ! -d "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
14+
#----------------------------------------------------------#
15+
# Verifications #
16+
#----------------------------------------------------------#
17+
18+
check_args '3' "$#" 'USER FILE PERMISSIONS'
19+
is_format_valid 'user'
20+
is_object_valid 'user' 'USER' "$user"
2221

2322
# Checking user homedir
2423
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -41,7 +40,7 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
4140
fi
4241

4342
# Changing file permissions
44-
sudo -u $user chmod -R $permissions "$src_file" >/dev/null 2>&1
43+
sudo -u $user -- chmod -R $permissions "$src_file" >/dev/null 2>&1
4544
if [ $? -ne 0 ]; then
4645
echo "Error: access permission on $src_file was not changed"
4746
exit 3

bin/v-check-fs-permission

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
user=$1
88
src=$2
99

10-
# Checking arguments
11-
if [ -z "$src" ]; then
12-
echo "Usage: USER FILE"
13-
exit 1
14-
fi
10+
# Includes
11+
source $HESTIA/func/main.sh
1512

16-
# Checking hestia user
17-
if [ ! -d "$HESTIA/data/users/$user" ]; then
18-
echo "Error: hestia user $user doesn't exist"
19-
exit 3
20-
fi
13+
#----------------------------------------------------------#
14+
# Verifications #
15+
#----------------------------------------------------------#
16+
17+
check_args '2' "$#" 'USER FILE'
18+
is_format_valid 'user'
19+
is_object_valid 'user' 'USER' "$user"
2120

2221
# Checking user homedir
2322
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -36,7 +35,7 @@ if [ ! -z "$src" ]; then
3635
fi
3736

3837
# Checking if file has readable permission
39-
sudo -u $user ls "$src" > /dev/null 2>&1
38+
sudo -u $user -- ls "$src" > /dev/null 2>&1
4039
if [ $? -ne 0 ]; then
4140
echo "Error: can't read $src"
4241
exit 1

bin/v-copy-fs-directory

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ user=$1
88
src_dir=$2
99
dst_dir=$3
1010

11-
# Checking arguments
12-
if [ -z "$dst_dir" ]; then
13-
echo "Usage: USER SRC_DIRECTORY DST_DIRECTORY"
14-
exit 1
15-
fi
11+
# Includes
12+
source $HESTIA/func/main.sh
1613

17-
# Checking hestia user
18-
if [ ! -d "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
14+
#----------------------------------------------------------#
15+
# Verifications #
16+
#----------------------------------------------------------#
17+
18+
check_args '3' "$#" 'USER SRC_DIRECTORY DST_DIRECTORY'
19+
is_format_valid 'user'
20+
is_object_valid 'user' 'USER' "$user"
2221

2322
# Checking user homedir
2423
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -48,7 +47,7 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
4847
fi
4948

5049
# Copying directory
51-
sudo -u $user cp -r "$src_dir" "$dst_dir" >/dev/null 2>&1
50+
sudo -u $user -- cp -rf "$src_dir" "$dst_dir" >/dev/null 2>&1
5251
if [ $? -ne 0 ]; then
5352
echo "Error: directory $src_dir was not copied"
5453
exit 3

bin/v-copy-fs-file

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#!/bin/bash
22
# info: copy file
3-
# options: USER SRC_FILE DST_FLE
3+
# options: USER SRC_FILE DST_FILE
44
#
55
# The function copies file on the file system
66

77
user=$1
88
src_file=$2
99
dst_file=$3
1010

11-
# Checking arguments
12-
if [ -z "$dst_file" ]; then
13-
echo "Usage: USER SRC_FILE DST_FILE"
14-
exit 1
15-
fi
11+
# Includes
12+
source $HESTIA/func/main.sh
1613

17-
# Checking hestia user
18-
if [ ! -e "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
14+
#----------------------------------------------------------#
15+
# Verifications #
16+
#----------------------------------------------------------#
17+
18+
check_args '3' "$#" 'USER SRC_FILE DST_FILE'
19+
is_format_valid 'user'
20+
is_object_valid 'user' 'USER' "$user"
2221

2322
# Checking user homedir
2423
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -43,12 +42,12 @@ fi
4342
# Checking destination path
4443
rpath=$(readlink -f "$dst_file")
4544
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
46-
echo "Error: ivalid destination path $dst_file"
45+
echo "Error: invalid destination path $dst_file"
4746
exit 2
4847
fi
4948

5049
# Copying file
51-
sudo -u $user cp "$src_file" "$dst_file" >/dev/null 2>&1
50+
sudo -u $user -- cp "$src_file" "$dst_file" >/dev/null 2>&1
5251
if [ $? -ne 0 ]; then
5352
echo "Error: file $src_file was not copied"
5453
exit 3

bin/v-delete-fs-directory

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
user=$1
99
dst_dir=$2
1010

11-
# Checking arguments
12-
if [ -z "$dst_dir" ]; then
13-
echo "Usage: USER DIRECTORY"
14-
exit 1
15-
fi
11+
# Includes
12+
source $HESTIA/func/main.sh
1613

17-
# Checking hestia user
18-
if [ ! -d "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
14+
#----------------------------------------------------------#
15+
# Verifications #
16+
#----------------------------------------------------------#
17+
18+
check_args '2' "$#" 'USER DIRECTORY'
19+
is_format_valid 'user'
20+
is_object_valid 'user' 'USER' "$user"
2221

2322
# Checking user homedir
2423
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -35,7 +34,7 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
3534
fi
3635

3736
# Deleting directory
38-
sudo -u $user rm -rf "$dst_dir" # >/dev/null 2>&1
37+
sudo -u $user -- rm -rf "$dst_dir" # >/dev/null 2>&1
3938
if [ $? -ne 0 ]; then
4039
echo "Error: directory $dst_dir was not deleted"
4140
exit 3

bin/v-delete-fs-file

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
user=$1
99
dst_file=$2
1010

11-
# Checking arguments
12-
if [ -z "$dst_file" ]; then
13-
echo "Usage: USER FILE"
14-
exit 1
15-
fi
11+
# Includes
12+
source $HESTIA/func/main.sh
1613

17-
# Checking hestia user
18-
if [ ! -d "$HESTIA/data/users/$user" ]; then
19-
echo "Error: hestia user $user doesn't exist"
20-
exit 3
21-
fi
14+
#----------------------------------------------------------#
15+
# Verifications #
16+
#----------------------------------------------------------#
17+
18+
check_args '2' "$#" 'USER FILE'
19+
is_format_valid 'user'
20+
is_object_valid 'user' 'USER' "$user"
2221

2322
# Checking user homedir
2423
homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
@@ -35,7 +34,7 @@ if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
3534
fi
3635

3736
# Deleting file
38-
sudo -u $user rm -f "$dst_file" >/dev/null 2>&1
37+
sudo -u $user -- rm -f "$dst_file" >/dev/null 2>&1
3938
if [ $? -ne 0 ]; then
4039
echo "Error: file $dst_file was not deleted"
4140
exit 3

0 commit comments

Comments
 (0)