forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.sh
More file actions
203 lines (184 loc) · 5.44 KB
/
ip.sh
File metadata and controls
203 lines (184 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Validationg ip address
is_ip_valid() {
userip=${1-$ip}
check_nat=$(grep -H "^NAT='$userip'" $VESTA/data/ips/* 2>/dev/null)
if [ ! -e "$VESTA/data/ips/$userip" ] && [ -z "$check_nat" ] ; then
echo "Error: IP $userip not exist"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Check if ip availabile for user
is_ip_avalable() {
userip=${1-$ip}
if [ -e "$VESTA/data/ips/$userip" ]; then
ip_data=$(cat $VESTA/data/ips/$userip)
else
nated_ip=$(grep -H "^NAT='$userip'" $VESTA/data/ips/* 2>/dev/null)
nated_ip=$(echo "$nated_ip" | cut -f 1 -d : | cut -f 7 -d /)
ip_data=$(cat $VESTA/data/ips/$nated_ip)
fi
owner=$(echo "$ip_data"|grep OWNER= | cut -f 2 -d \')
status=$(echo "$ip_data"|grep STATUS= | cut -f 2 -d \')
shared=no
if [ 'admin' = "$owner" ] && [ "$status" = 'shared' ]; then
shared='yes'
fi
if [ "$owner" != "$user" ] && [ "$shared" != 'yes' ]; then
echo "Error: User $user don't have permission to use $userip"
log_event "$E_FORBIDEN" "$EVENT"
exit $E_FORBIDEN
fi
}
# Check ip ownership
is_ip_owner() {
# Parsing ip
owner=$(grep 'OWNER=' $VESTA/data/ips/$IP|cut -f 2 -d \')
if [ "$owner" != "$user" ]; then
echo "Error: IP $IP not owned"
log_event "$E_FORBIDEN" "$EVENT"
exit $E_FORBIDEN
fi
}
# Check if ip address is free
is_ip_free() {
if [ -e "$VESTA/data/ips/$ip" ]; then
echo "Error: IP exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
}
# Get full interface name
get_ip_iface() {
i=$(/sbin/ifconfig -a |grep -w "$interface"|cut -f1 -d ' '|\
tail -n 1|cut -f 2 -d :)
if [ "$i" = "$interface" ]; then
n=0
else
n=$((i + 1))
fi
iface="$interface:$n"
}
# Check ip address speciefic value
is_ip_key_empty() {
key="$1"
string=$(cat $VESTA/data/ips/$ip)
eval $string
eval value="$key"
if [ ! -z "$value" ] && [ "$value" != '0' ]; then
echo "Error: $key is not empty = $value"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
}
# Update ip address value
update_ip_value() {
key="$1"
value="$2"
conf="$VESTA/data/ips/$ip"
str=$(cat $conf)
eval $str
c_key=$(echo "${key//$/}")
eval old="${key}"
old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
$conf
}
# Get ip name
get_ip_name() {
grep "NAME=" $VESTA/data/ips/$ip | cut -f 2 -d \'
}
# Increase ip value
increase_ip_value() {
sip=${1-ip}
USER=$user
web_key='U_WEB_DOMAINS'
usr_key='U_SYS_USERS'
current_web=$(grep "$web_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
if [ -z "$current_web" ]; then
echo "Error: Parsing error"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
fi
new_web=$((current_web + 1))
if [ -z "$current_usr" ]; then
new_usr="$USER"
else
check_usr=$(echo -e "${current_usr//,/\n}" |grep -w $USER)
if [ -z "$check_usr" ]; then
new_usr="$current_usr,$USER"
else
new_usr="$current_usr"
fi
fi
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" \
$VESTA/data/ips/$ip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" \
$VESTA/data/ips/$ip
}
# Decrease ip value
decrease_ip_value() {
sip=${1-ip}
USER=$user
web_key='U_WEB_DOMAINS'
usr_key='U_SYS_USERS'
current_web=$(grep "$web_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
if [ -z "$current_web" ]; then
echo "Error: Parsing error"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
fi
new_web=$((current_web - 1))
check_ip=$(grep $sip $USER_DATA/web.conf |wc -l)
if [ "$check_ip" -lt 2 ]; then
new_usr=$(echo "$current_usr" |\
sed -e "s/,/\n/g"|\
sed -e "s/^$user$//g"|\
sed -e "/^$/d"|\
sed -e ':a;N;$!ba;s/\n/,/g')
else
new_usr="$current_usr"
fi
sed -i "s/$web_key='$current_web'/$web_key='$new_web'/g" \
$VESTA/data/ips/$sip
sed -i "s/$usr_key='$current_usr'/$usr_key='$new_usr'/g" \
$VESTA/data/ips/$sip
}
# Get ip address value
get_ip_value() {
key="$1"
string=$( cat $VESTA/data/ips/$ip )
eval $string
eval value="$key"
echo "$value"
}
# Get real ip address
get_real_ip() {
if [ -e "$VESTA/data/ips/$1" ]; then
echo $1
else
nated_ip=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
echo "$nated_ip" | cut -f 1 -d : | cut -f 7 -d /
fi
}
# Get user ip
get_user_ip(){
ip=$(grep -H "OWNER='$1'" $VESTA/data/ips/* 2>/dev/null | head -n1)
ip=$(echo "$ip" | cut -f 7 -d / | cut -f 1 -d :)
if [ -z "$ip" ]; then
admin_ips=$(grep -H "OWNER='admin'" $VESTA/data/ips/* 2>/dev/null)
admin_ips=$(echo "$admin_ips" | cut -f 7 -d / | cut -f 1 -d :)
for admin_ip in $admin_ips; do
if [ -z "$ip" ]; then
shared=$(grep "STATUS='shared'" $VESTA/data/ips/$admin_ip)
if [ ! -z "$shared" ]; then
ip=$admin_ip
fi
fi
done
fi
echo "$ip"
}