ip functions refactoring

This commit is contained in:
Serghey Rodin 2016-06-09 17:02:29 +03:00
commit c8d45ed139

View file

@ -1,61 +1,22 @@
# 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 \')
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
check_result $E_FORBIDEN "$ip is not owned by $user"
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
check_result $E_EXISTS "$ip is already exists"
fi
}
# Get full interface name
get_ip_iface() {
i=$(/sbin/ip addr | grep -w $interface |\
awk '{print $NF}' | tail -n 1 | cut -f 2 -d :)
i=$(/sbin/ip addr |grep -w $interface |\
awk '{print $NF}' |tail -n 1 |cut -f 2 -d :)
if [ "$i" = "$interface" ]; then
n=0
else
@ -72,9 +33,7 @@ is_ip_key_empty() {
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
check_result $E_EXISTS "$key is not empty = $value"
fi
}
@ -94,8 +53,11 @@ update_ip_value() {
}
# Get ip name
get_ip_name() {
grep "NAME=" $VESTA/data/ips/$ip | cut -f 2 -d \'
get_ip_alias() {
ip_name=$(grep "NAME=" $VESTA/data/ips/$local_ip |cut -f 2 -d \')
if [ ! -z "$ip_name" ]; then
echo "${1//./-}.$ip_name"
fi
}
# Increase ip value
@ -108,7 +70,7 @@ increase_ip_value() {
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"
log_event "$E_PARSING" "$ARGUMENTS"
exit $E_PARSING
fi
new_web=$((current_web + 1))
@ -140,9 +102,7 @@ decrease_ip_value() {
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
check_result $E_PARSING "Parsing errpr"
fi
new_web=$((current_web - 1))
@ -166,46 +126,28 @@ decrease_ip_value() {
# Get ip address value
get_ip_value() {
key="$1"
string=$( cat $VESTA/data/ips/$ip )
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/*)
if [ ! -z "$nated_ip" ]; then
echo "$nated_ip" | cut -f 1 -d : | cut -f 7 -d /
nat=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
if [ ! -z "$nat" ]; then
echo "$nat" |cut -f 1 -d : |cut -f 7 -d /
else
get_user_ip
fi
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"
}
# Convert CIDR to netmask
convert_cidr() {
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 \
@ -252,3 +194,55 @@ get_broadcast() {
(255 ^ ${N[2]}))).$((${I[3]} |\
(255 ^ ${N[3]})))"
}
# Get user ips
get_user_ips() {
dedicated=$(grep -H "OWNER='$user'" $VESTA/data/ips/*)
dedicated=$(echo "$dedicated" |cut -f 1 -d : |sed 's=.*/==')
shared=$(grep -H -A1 "OWNER='$owner'" $VESTA/data/ips/* |grep shared)
shared=$(echo "$shared" |cut -f 1 -d : |sed 's=.*/==' |cut -f 1 -d \-)
for dedicated_ip in $dedicated; do
shared=$(echo "$shared" |grep -v $dedicated_ip)
done
echo -e "$dedicated\n$shared"
}
# Get user ip
get_user_ip() {
ip=$(get_user_ips |head -n1)
if [ -z "$ip" ]; then
check_result $E_NOTEXIST "no IP is available"
fi
local_ip=$ip
nat=$(grep "^NAT" $VESTA/data/ips/$ip |cut -f 2 -d \')
if [ ! -z "$nat" ]; then
ip=$nat
fi
}
# Validate ip address
is_ip_valid() {
if [ ! -z $1 ]; then
if [ -e "$VESTA/data/ips/$1" ]; then
ip_data=$(cat $VESTA/data/ips/$1)
local_ip="$1"
else
nat=$(grep -H "^NAT='$1'" $VESTA/data/ips/*)
check_result $? "IP $1 doesn't exist" $E_NOTEXIST
nat=$(echo "$nat" |cut -f1 -d: |cut -f7 -d/)
ip_data=$(cat $VESTA/data/ips/$nat)
local_ip=$nat
fi
ip_owner=$(echo "$ip_data" |grep OWNER= |cut -f2 -d \')
ip_status=$(echo "$ip_data" |grep STATUS= |cut -f2 -d \')
if [ "$ip_owner" != "$user" ] && [ "$ip_status" = 'dedicated' ]; then
check_result $E_FORBIDEN "$user user can't use IP $1"
fi
get_user_owner
if [ "$ip_owner" != "$user" ] && [ "$ip_owner" != "$owner" ]; then
check_result $E_FORBIDEN "$user user can't use IP $1"
fi
else
get_user_ip
fi
}