Skip to content

Commit c8d45ed

Browse files
committed
ip functions refactoring
1 parent e05dca6 commit c8d45ed

File tree

1 file changed

+71
-77
lines changed

1 file changed

+71
-77
lines changed

func/ip.sh

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,22 @@
1-
# Validationg ip address
2-
is_ip_valid() {
3-
userip=${1-$ip}
4-
check_nat=$(grep -H "^NAT='$userip'" $VESTA/data/ips/* 2>/dev/null)
5-
if [ ! -e "$VESTA/data/ips/$userip" ] && [ -z "$check_nat" ] ; then
6-
echo "Error: IP $userip not exist"
7-
log_event "$E_NOTEXIST" "$EVENT"
8-
exit $E_NOTEXIST
9-
fi
10-
}
11-
12-
# Check if ip availabile for user
13-
is_ip_avalable() {
14-
userip=${1-$ip}
15-
if [ -e "$VESTA/data/ips/$userip" ]; then
16-
ip_data=$(cat $VESTA/data/ips/$userip)
17-
else
18-
nated_ip=$(grep -H "^NAT='$userip'" $VESTA/data/ips/* 2>/dev/null)
19-
nated_ip=$(echo "$nated_ip" | cut -f 1 -d : | cut -f 7 -d /)
20-
ip_data=$(cat $VESTA/data/ips/$nated_ip)
21-
fi
22-
owner=$(echo "$ip_data"|grep OWNER= | cut -f 2 -d \')
23-
status=$(echo "$ip_data"|grep STATUS= | cut -f 2 -d \')
24-
shared=no
25-
if [ 'admin' = "$owner" ] && [ "$status" = 'shared' ]; then
26-
shared='yes'
27-
fi
28-
if [ "$owner" != "$user" ] && [ "$shared" != 'yes' ]; then
29-
echo "Error: User $user don't have permission to use $userip"
30-
log_event "$E_FORBIDEN" "$EVENT"
31-
exit $E_FORBIDEN
32-
fi
33-
}
34-
351
# Check ip ownership
362
is_ip_owner() {
37-
# Parsing ip
38-
owner=$(grep 'OWNER=' $VESTA/data/ips/$IP|cut -f 2 -d \')
3+
owner=$(grep 'OWNER=' $VESTA/data/ips/$ip |cut -f 2 -d \')
394
if [ "$owner" != "$user" ]; then
40-
echo "Error: IP $IP not owned"
41-
log_event "$E_FORBIDEN" "$EVENT"
42-
exit $E_FORBIDEN
5+
check_result $E_FORBIDEN "$ip is not owned by $user"
436
fi
447
}
458

469
# Check if ip address is free
4710
is_ip_free() {
4811
if [ -e "$VESTA/data/ips/$ip" ]; then
49-
echo "Error: IP exist"
50-
log_event "$E_EXISTS" "$EVENT"
51-
exit $E_EXISTS
12+
check_result $E_EXISTS "$ip is already exists"
5213
fi
5314
}
5415

5516
# Get full interface name
5617
get_ip_iface() {
57-
i=$(/sbin/ip addr | grep -w $interface |\
58-
awk '{print $NF}' | tail -n 1 | cut -f 2 -d :)
18+
i=$(/sbin/ip addr |grep -w $interface |\
19+
awk '{print $NF}' |tail -n 1 |cut -f 2 -d :)
5920
if [ "$i" = "$interface" ]; then
6021
n=0
6122
else
@@ -72,9 +33,7 @@ is_ip_key_empty() {
7233
eval $string
7334
eval value="$key"
7435
if [ ! -z "$value" ] && [ "$value" != '0' ]; then
75-
echo "Error: $key is not empty = $value"
76-
log_event "$E_EXISTS" "$EVENT"
77-
exit $E_EXISTS
36+
check_result $E_EXISTS "$key is not empty = $value"
7837
fi
7938
}
8039

@@ -94,8 +53,11 @@ update_ip_value() {
9453
}
9554

9655
# Get ip name
97-
get_ip_name() {
98-
grep "NAME=" $VESTA/data/ips/$ip | cut -f 2 -d \'
56+
get_ip_alias() {
57+
ip_name=$(grep "NAME=" $VESTA/data/ips/$local_ip |cut -f 2 -d \')
58+
if [ ! -z "$ip_name" ]; then
59+
echo "${1//./-}.$ip_name"
60+
fi
9961
}
10062

10163
# Increase ip value
@@ -108,7 +70,7 @@ increase_ip_value() {
10870
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
10971
if [ -z "$current_web" ]; then
11072
echo "Error: Parsing error"
111-
log_event "$E_PARSING" "$EVENT"
73+
log_event "$E_PARSING" "$ARGUMENTS"
11274
exit $E_PARSING
11375
fi
11476
new_web=$((current_web + 1))
@@ -140,9 +102,7 @@ decrease_ip_value() {
140102
current_usr=$(grep "$usr_key=" $VESTA/data/ips/$sip |cut -f 2 -d \')
141103

142104
if [ -z "$current_web" ]; then
143-
echo "Error: Parsing error"
144-
log_event "$E_PARSING" "$EVENT"
145-
exit $E_PARSING
105+
check_result $E_PARSING "Parsing errpr"
146106
fi
147107

148108
new_web=$((current_web - 1))
@@ -166,46 +126,28 @@ decrease_ip_value() {
166126
# Get ip address value
167127
get_ip_value() {
168128
key="$1"
169-
string=$( cat $VESTA/data/ips/$ip )
129+
string=$(cat $VESTA/data/ips/$ip)
170130
eval $string
171131
eval value="$key"
172132
echo "$value"
173133
}
174134

135+
136+
175137
# Get real ip address
176138
get_real_ip() {
177139
if [ -e "$VESTA/data/ips/$1" ]; then
178140
echo $1
179141
else
180-
nated_ip=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
181-
if [ ! -z "$nated_ip" ]; then
182-
echo "$nated_ip" | cut -f 1 -d : | cut -f 7 -d /
142+
nat=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
143+
if [ ! -z "$nat" ]; then
144+
echo "$nat" |cut -f 1 -d : |cut -f 7 -d /
183145
else
184146
get_user_ip
185147
fi
186148
fi
187149
}
188150

189-
# Get user ip
190-
get_user_ip(){
191-
ip=$(grep -H "OWNER='$1'" $VESTA/data/ips/* 2>/dev/null | head -n1)
192-
ip=$(echo "$ip" | cut -f 7 -d / | cut -f 1 -d :)
193-
194-
if [ -z "$ip" ]; then
195-
admin_ips=$(grep -H "OWNER='admin'" $VESTA/data/ips/* 2>/dev/null)
196-
admin_ips=$(echo "$admin_ips" | cut -f 7 -d / | cut -f 1 -d :)
197-
for admin_ip in $admin_ips; do
198-
if [ -z "$ip" ]; then
199-
shared=$(grep "STATUS='shared'" $VESTA/data/ips/$admin_ip)
200-
if [ ! -z "$shared" ]; then
201-
ip=$admin_ip
202-
fi
203-
fi
204-
done
205-
fi
206-
echo "$ip"
207-
}
208-
209151
# Convert CIDR to netmask
210152
convert_cidr() {
211153
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 \
@@ -252,3 +194,55 @@ get_broadcast() {
252194
(255 ^ ${N[2]}))).$((${I[3]} |\
253195
(255 ^ ${N[3]})))"
254196
}
197+
198+
# Get user ips
199+
get_user_ips() {
200+
dedicated=$(grep -H "OWNER='$user'" $VESTA/data/ips/*)
201+
dedicated=$(echo "$dedicated" |cut -f 1 -d : |sed 's=.*/==')
202+
shared=$(grep -H -A1 "OWNER='$owner'" $VESTA/data/ips/* |grep shared)
203+
shared=$(echo "$shared" |cut -f 1 -d : |sed 's=.*/==' |cut -f 1 -d \-)
204+
for dedicated_ip in $dedicated; do
205+
shared=$(echo "$shared" |grep -v $dedicated_ip)
206+
done
207+
echo -e "$dedicated\n$shared"
208+
}
209+
210+
# Get user ip
211+
get_user_ip() {
212+
ip=$(get_user_ips |head -n1)
213+
if [ -z "$ip" ]; then
214+
check_result $E_NOTEXIST "no IP is available"
215+
fi
216+
local_ip=$ip
217+
nat=$(grep "^NAT" $VESTA/data/ips/$ip |cut -f 2 -d \')
218+
if [ ! -z "$nat" ]; then
219+
ip=$nat
220+
fi
221+
}
222+
223+
# Validate ip address
224+
is_ip_valid() {
225+
if [ ! -z $1 ]; then
226+
if [ -e "$VESTA/data/ips/$1" ]; then
227+
ip_data=$(cat $VESTA/data/ips/$1)
228+
local_ip="$1"
229+
else
230+
nat=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
231+
check_result $? "IP $1 doesn't exist" $E_NOTEXIST
232+
nat=$(echo "$nat" |cut -f1 -d: |cut -f7 -d/)
233+
ip_data=$(cat $VESTA/data/ips/$nat)
234+
local_ip=$nat
235+
fi
236+
ip_owner=$(echo "$ip_data" |grep OWNER= |cut -f2 -d \')
237+
ip_status=$(echo "$ip_data" |grep STATUS= |cut -f2 -d \')
238+
if [ "$ip_owner" != "$user" ] && [ "$ip_status" = 'dedicated' ]; then
239+
check_result $E_FORBIDEN "$user user can't use IP $1"
240+
fi
241+
get_user_owner
242+
if [ "$ip_owner" != "$user" ] && [ "$ip_owner" != "$owner" ]; then
243+
check_result $E_FORBIDEN "$user user can't use IP $1"
244+
fi
245+
else
246+
get_user_ip
247+
fi
248+
}

0 commit comments

Comments
 (0)