Skip to content

Commit 99e73de

Browse files
committed
http auth api feature
1 parent 45426d5 commit 99e73de

File tree

6 files changed

+282
-2
lines changed

6 files changed

+282
-2
lines changed

bin/v-add-web-domain-httpauth

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
# info: add password protection for web domain
3+
# options: USER DOMAIN AUTH_USER AUTH_PASSWORD [RESTART]
4+
#
5+
# The call is used for securing web domain with http auth
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
domain=$2
15+
auth_user=$3
16+
password=$4
17+
restart=${5-yes}
18+
19+
# Includes
20+
source $VESTA/func/main.sh
21+
source $VESTA/func/domain.sh
22+
source $VESTA/conf/vesta.conf
23+
24+
# Hiding password
25+
A4='******'
26+
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
27+
28+
# Definining htpasswd file
29+
htaccess="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.conf_htaccess"
30+
htpasswd="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.htpasswd"
31+
docroot="$HOMEDIR/$user/web/$domain/public_html"
32+
33+
34+
#----------------------------------------------------------#
35+
# Verifications #
36+
#----------------------------------------------------------#
37+
38+
check_args '4' "$#" 'USER DOMAIN AUTH_USER AUTH_PASSWORD [RESTART]'
39+
validate_format 'user' 'domain'
40+
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
41+
is_object_valid 'user' 'USER' "$user"
42+
is_object_unsuspended 'user' 'USER' "$user"
43+
is_object_valid 'web' 'DOMAIN' "$domain"
44+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
45+
is_password_valid
46+
get_domain_values 'web'
47+
if [ ! -z "$(echo "$AUTH_USER" |tr : '\n' |grep ^$auth_user$)" ]; then
48+
echo "Error: auth user $auth_user already exists"
49+
log_event "$E_EXISTS" "$EVENT"
50+
exit $E_EXISTS
51+
fi
52+
53+
54+
#----------------------------------------------------------#
55+
# Action #
56+
#----------------------------------------------------------#
57+
58+
# Adding htaccess password protection
59+
if [ ! -e "$htaccess" ]; then
60+
if [ "$WEB_SYSTEM" != 'nginx' ]; then
61+
echo "<Directory $docroot>" > $htaccess
62+
echo " AuthUserFile $htpasswd" >> $htaccess
63+
echo " AuthName \"$domain access\"" >> $htaccess
64+
echo " AuthType Basic" >> $htaccess
65+
echo " Require valid-user" >> $htaccess
66+
echo "</Directory>" >> $htaccess
67+
else
68+
echo "auth_basic \"$domain password access\";" > $htaccess
69+
echo "auth_basic_user_file $htpasswd;" >> $htaccess
70+
fi
71+
restart_required='yes'
72+
fi
73+
74+
# Adding httpasswd user
75+
auth_hash=$($BIN/v-generate-password-hash htpasswd htpasswd $password)
76+
touch $htpasswd
77+
sed -i "/^$auth_user:/d" $htpasswd
78+
echo "$auth_user:$auth_hash" >> $htpasswd
79+
80+
# Restarting web server
81+
if [ "$restart" != 'no' ] && [ "$restart_required" = 'yes' ]; then
82+
$BIN/v-restart-web
83+
fi
84+
85+
86+
#----------------------------------------------------------#
87+
# Vesta #
88+
#----------------------------------------------------------#
89+
90+
# Preparing web.conf keys
91+
if [ ! -z "$AUTH_USER" ]; then
92+
auth_user="$AUTH_USER:$auth_user"
93+
auth_hash="$AUTH_HASH:$auth_hash"
94+
else
95+
# Adding new key into web.conf
96+
add_object_key "web" 'DOMAIN' "$domain" 'AUTH_USER' 'U_DISK'
97+
add_object_key "web" 'DOMAIN' "$domain" 'AUTH_HASH' 'U_DISK'
98+
fi
99+
100+
# Updating config
101+
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_USER' "$auth_user"
102+
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_HASH' "$auth_hash"
103+
104+
# Logging
105+
log_history "added http auth user $httpauth_user on $domain"
106+
log_event "$OK" "$EVENT"
107+
108+
exit

bin/v-change-web-domain-httpauth

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
# info: change password for http auth user
3+
# options: USER DOMAIN AUTH_USER AUTH_PASSWORD
4+
#
5+
# The call is used for chaning http auth user password
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
domain=$2
15+
auth_user=$3
16+
password=$4
17+
18+
# Includes
19+
source $VESTA/func/main.sh
20+
source $VESTA/func/domain.sh
21+
source $VESTA/conf/vesta.conf
22+
23+
# Hiding password
24+
A4='******'
25+
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
26+
27+
# Definining htpasswd file
28+
htpasswd="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.htpasswd"
29+
30+
31+
#----------------------------------------------------------#
32+
# Verifications #
33+
#----------------------------------------------------------#
34+
35+
check_args '4' "$#" 'USER DOMAIN AUTH_USER AUTH_PASSWORD [RESTART]'
36+
validate_format 'user' 'domain'
37+
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
38+
is_object_valid 'user' 'USER' "$user"
39+
is_object_unsuspended 'user' 'USER' "$user"
40+
is_object_valid 'web' 'DOMAIN' "$domain"
41+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
42+
is_password_valid
43+
get_domain_values 'web'
44+
if [ -z "$(echo "$AUTH_USER" |tr : '\n' |grep ^$auth_user$)" ]; then
45+
echo "Error: auth user $auth_user doesn't exist"
46+
log_event "$E_NOTEXIST" "$EVENT"
47+
exit $E_NOTEXIST
48+
fi
49+
50+
51+
#----------------------------------------------------------#
52+
# Action #
53+
#----------------------------------------------------------#
54+
55+
# Adding httpasswd user
56+
auth_hash=$($BIN/v-generate-password-hash htpasswd htpasswd $password)
57+
touch $htpasswd
58+
sed -i "/^$auth_user:/d" $htpasswd
59+
echo "$auth_user:$auth_hash" >> $htpasswd
60+
61+
62+
#----------------------------------------------------------#
63+
# Vesta #
64+
#----------------------------------------------------------#
65+
66+
# Rebuilding AUTH_HASH variable
67+
position=$(echo $AUTH_USER |tr ':' '\n' |grep -n '' |grep ":$auth_user$" |\
68+
cut -f 1 -d:)
69+
auth_hash=$(echo $AUTH_HASH |tr ':' '\n' |grep -n '' |\
70+
sed -e "s%^$position:.*%$position:$auth_hash%" |\
71+
cut -f 2 -d :| sed -e "/^$/d"| sed -e ':a;N;$!ba;s/\n/:/g')
72+
73+
# Updating config
74+
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_HASH' "$auth_hash"
75+
76+
# Logging
77+
log_history "changed auth user $httpauth_user password on $domain"
78+
log_event "$OK" "$EVENT"
79+
80+
exit

bin/v-delete-web-domain-httpauth

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# info: delete http auth user
3+
# options: USER DOMAIN AUTH_USER [RESTART]
4+
#
5+
# The call is used for deleting http auth user
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
user=$1
14+
domain=$2
15+
auth_user=$3
16+
restart=${4-yes}
17+
18+
# Includes
19+
source $VESTA/func/main.sh
20+
source $VESTA/func/domain.sh
21+
source $VESTA/conf/vesta.conf
22+
23+
# Definining htpasswd file
24+
htaccess="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.conf_htaccess"
25+
htpasswd="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.htpasswd"
26+
27+
28+
#----------------------------------------------------------#
29+
# Verifications #
30+
#----------------------------------------------------------#
31+
32+
check_args '3' "$#" 'USER DOMAIN AUTH_USER [RESTART]'
33+
validate_format 'user' 'domain'
34+
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
35+
is_object_valid 'user' 'USER' "$user"
36+
is_object_unsuspended 'user' 'USER' "$user"
37+
is_object_valid 'web' 'DOMAIN' "$domain"
38+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
39+
is_password_valid
40+
get_domain_values 'web'
41+
if [ -z "$(echo "$AUTH_USER" |tr : '\n' |grep ^$auth_user$)" ]; then
42+
echo "Error: auth user $auth_user doesn't exist"
43+
log_event "$E_NOTEXIST" "$EVENT"
44+
exit $E_NOTEXIST
45+
fi
46+
47+
48+
#----------------------------------------------------------#
49+
# Action #
50+
#----------------------------------------------------------#
51+
52+
# Deleting auth user
53+
sed -i "/^$auth_user:/d" $htpasswd
54+
55+
# Deleting password protection
56+
if [ "$(echo "$AUTH_USER" |tr : '\n' |wc -l)" -le 1 ]; then
57+
rm -f $htaccess
58+
restart_required='yes'
59+
fi
60+
61+
# Restarting web server
62+
if [ "$restart" != 'no' ] && [ "$restart_required" = 'yes' ]; then
63+
$BIN/v-restart-web
64+
fi
65+
66+
67+
#----------------------------------------------------------#
68+
# Vesta #
69+
#----------------------------------------------------------#
70+
71+
# Rebuilding FTP variables
72+
position=$(echo $AUTH_USER |tr ':' '\n' |grep -n '' |grep ":$auth_user$" |\
73+
cut -f 1 -d:)
74+
auth_user=$(echo $AUTH_USER |tr ':' '\n' |grep -n '' |grep -v "^$position:" |\
75+
cut -f 2 -d :| sed -e "/^$/d"| sed -e ':a;N;$!ba;s/\n/:/g')
76+
auth_hash=$(echo $AUTH_HASH |tr ':' '\n' |grep -n '' |grep -v "^$position:" |\
77+
cut -f 2 -d :| sed -e ':a;N;$!ba;s/\n/:/g')
78+
79+
# Update config
80+
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_USER' "$auth_user"
81+
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_HASH' "$auth_hash"
82+
83+
# Logging
84+
log_history "changed auth user $httpauth_user password on $domain"
85+
log_event "$OK" "$EVENT"
86+
87+
exit

bin/v-generate-password-hash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ if ($crypt == 'sha-512' ) {
3232
$hash = str_replace('$rounds=5000','',$hash);
3333
}
3434

35+
// Generating base64 hash
36+
if ($crypt == 'htpasswd' ) {
37+
$hash = crypt($password, base64_encode($password));
38+
}
39+
3540
// Printing result
3641
echo $hash . "\n";

bin/v-list-web-domain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ conf=$USER_DATA/web.conf
7676

7777
# Defining fileds to select
7878
fields='$DOMAIN $IP $IP6 $U_DISK $U_BANDWIDTH $TPL $ALIAS $STATS $STATS_USER
79-
$SSL $SSL_HOME $FTP_USER $FTP_PATH $BACKEND $PROXY $PROXY_EXT
79+
$SSL $SSL_HOME $FTP_USER $FTP_PATH $BACKEND $PROXY $PROXY_EXT $AUTH_USER
8080
$DOCUMENT_ROOT $SUSPENDED $TIME $DATE'
8181

8282
# Defining document root

bin/v-list-web-domains

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ conf=$USER_DATA/web.conf
3535

3636
# Defining fileds to select
3737
fields="\$DOMAIN \$IP \$IP6 \$U_DISK \$U_BANDWIDTH \$TPL \$ALIAS \$STATS"
38-
fields="$fields \$STATS_USER \$SSL \$SSL_HOME \$FTP_USER \$FTP_PATH"
38+
fields="$fields \$STATS_USER \$SSL \$SSL_HOME \$FTP_USER \$FTP_PATH \$AUTH_USER"
3939
fields="$fields \$BACKEND \$PROXY \$PROXY_EXT \$SUSPENDED \$TIME \$DATE"
4040

4141
# Listing domains

0 commit comments

Comments
 (0)