Przeglądaj źródła

IPV6: Preparation for unified IP check

Common IP format, cidr, netmask, prefix_legth cheks

func/main.sh

func/ip.s

bin/v-add-sys-ip

bin/v-delete-sys-ip
asmcc 3 lat temu
rodzic
commit
bb2bc87429
4 zmienionych plików z 82 dodań i 46 usunięć
  1. 75 11
      bin/v-add-sys-ip
  2. 2 2
      bin/v-delete-sys-ip
  3. 4 33
      func/ip.sh
  4. 1 0
      func/main.sh

+ 75 - 11
bin/v-add-sys-ip

@@ -1,8 +1,12 @@
 #!/bin/bash
 # info: add system ip address
-# options: IP NETMASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
+# options: IP [NETMASK] [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
 #
 # example: v-add-sys-ip 216.239.32.21 255.255.255.0
+# example: v-add-sys-ip 216.239.32.21 /24
+# example: v-add-sys-ip 216.239.32.21/24
+# example: v-add-sys-ip 1234:55:66::1 /64
+# example: v-add-sys-ip 1234:55:66::1/64
 #
 # This function adds ip address into a system. It also creates rc scripts. You
 # can specify ip name which will be used as root domain for temporary aliases.
@@ -15,17 +19,16 @@
 #                Variables & Functions                     #
 #----------------------------------------------------------#
 
-# Get interface name
-iface=$(/bin/ip token | awk -F 'dev ' '{print $2}')
-
 # Argument definition
-ip=${1// /}
-netmask=$2
-iface="${3-$iface}"
+first_parameter=${1// /}										# conventional delete of spaces
+ip46=${1%/*}													# clean ip address without cidr/prefix_length
+[ -n "$ip46" ] && [ "$ip46" = "${1}" ] || ip_cidr=${1#$ip46}	# extract cidr/prefix from first parameter
+second_parameter=${2}											# second parameter can be netmask, cidr or prefix_length
+iface="${3}"
 user="${4-admin}"
 ip_status="${5-shared}"
-ip_name=$6
-nat_ip=$7
+ip_name="${6}"
+nat_ip="${7}"
 
 # Includes
 # shellcheck source=/etc/hestiacp/hestia.conf
@@ -43,8 +46,66 @@ source_conf "$HESTIA/conf/hestia.conf"
 #                    Verifications                         #
 #----------------------------------------------------------#
 
-check_args '2' "$#" 'IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NATED_IP]'
-is_format_valid 'ip' 'netmask' 'iface' 'user' 'ip_status'
+check_args '1' "$#" 'IP [NETMASK] [INTERFACE] [USER] [STATUS] [NAME] [NATED_IP]'
+
+echo "ip46=$ip46"
+echo "ip_cidr=$ip_cidr"
+
+ip_format="$(get_ip_format ${ip46})"	#	ip verification and format identification
+if [ -n "$second_parameter" -a -n "$ip_format" ]; then
+	[ -n "$ip_cidr" ] && check_result 1 "cidr / prefix length double defined as IP address suffix and as separate argument!"	# wrong parameters
+	netmask="$(echo ${second_parameter} | sed -nr ''/$REGEX_IPV4/p'')"				# extract netmask from second parameter if available
+	cidr_prefixlen="$(echo ${second_parameter} | sed -ne '/^\/[0-9]\{1,3\}$/p')"	# extract cidr/prefix_length from second parameter if available
+	[ -z "$netmask" -a -z "$cidr_prefixlen" ] && check_result 2 "Wrong netmask / cidr / prefix length definition!"	# wrong parameters
+	[ -n "$netmask" -a $ip_format -ne 4 ] && check_result 3 "Netmask definition for a not IPV4 address! Define a prefix lenght instead of netmask!"	# wrong parameters
+fi
+ip_check_string=''ip46''
+if [ -n "$ip_format" ]; then
+	if [ $ip_format -eq 4 ]; then
+		ip=${ip46}
+		ipv6=''
+		ip_check_string=''ip''
+		if [ -n "$netmask" ]; then
+			is_ip_format_valid "${netmask}" 'netmask'	# check for correct netmask
+			cidr="$(convert_netmask $netmask)"			# convert netmask to cidr
+		fi
+		if [ -n "$cidr_prefixlen" ]; then
+			cidr=${cidr_prefixlen}
+		fi
+		if [ -n "$ip_cidr" ]; then
+			cidr=${ip_cidr}
+		else
+			[ -z "$cidr" ] && cidr="/31"
+		fi
+		if [ -z "$netmask" ]; then 
+			is_ip_format_valid "${cidr}" 'cidr'	# check for correct cidr
+			netmask=$(convert_cidr ${cidr})	# convert cidr to netmask
+		fi
+	fi
+	if [ $ip_format -eq 6 ]; then
+		ip=''
+		ipv6=${ip46}
+		ip_check_string=''ipv6''
+		[ -n "$cidr_prefixlen" ] && prefix_length=${cidr_prefixlen}
+		if [ -n "$ip_cidr" ]; then
+			prefix_length="${ip_cidr}"
+		else
+			[ -z "$prefix_length" ] && prefix_length="/64"
+		fi
+	fi
+fi
+
+[ -z "$iface" ] && iface="$($BIN/v-list-sys-interfaces plain | head -n 1)" 			# Get first available system interface, if none defined
+[ -z "$iface" ] && iface='eth0' # eth0, if still not defined
+
+echo "ip_format=$ip_format"
+echo "netmask=$netmask"
+echo "cidr_prefixlen=$cidr_prefixlen"
+echo "cidr=$cidr"
+echo "prefix_length=$prefix_length"
+echo "iface=$iface"
+
+is_format_valid ${ip_check_string} 'netmask' 'prefix_length' 'iface' 'user' 'ip_status'
 is_ip_free
 is_object_valid 'user' 'USER' "$user"
 is_object_unsuspended 'user' 'USER' "$user"
@@ -68,6 +129,9 @@ check_hestia_demo_mode
 cidr=$(convert_netmask $netmask)
 broadcast=$(get_broadcast $ip $netmask)
 
+echo "debug action exit"
+exit
+
 sys_ip_check=$(/sbin/ip addr | grep "$ip")
 if [ -z "$sys_ip_check" ]; then
 	# Adding sys ip

+ 2 - 2
bin/v-delete-sys-ip

@@ -49,7 +49,7 @@ cidr=$(convert_netmask "$NETMASK")
 main_ip=$(hostname -i)
 
 # Checking main ip on the interface
-interface=$(/sbin/ip addr | grep "$ip/$cidr" | awk '{print $NF}')
+interface=$(/sbin/ip addr | grep "$ip$cidr" | awk '{print $NF}')
 if [ -n "$interface" ] && [ "$ip" = "$main_ip" ]; then
 	echo "Error: can't delete main IP address"
 	log_event "$E_FORBIDEN" "$ARGUMENTS"
@@ -58,7 +58,7 @@ fi
 
 # Deleting system ip
 if [ -n "$interface" ]; then
-	/sbin/ip addr del "$ip/$cidr" dev "$INTERFACE"
+	/sbin/ip addr del "$ip$cidr" dev "$INTERFACE"
 	if [ "$?" -ne 0 ]; then
 		echo "Error: can't delete system ip"
 		log_event "$E_FORBIDEN" "$ARGUMENTS"

+ 4 - 33
func/ip.sh

@@ -189,6 +189,9 @@ get_real_ip() {
 
 # Convert CIDR to netmask
 convert_cidr() {
+	# CIDR can be defined as /32 (with leading /) or as 32 (number without leading /)
+	# please check the value range of cidr before converting!
+	set ${1#/}	# allow to use cidr format with leading /
 	set -- $((5 - ($1 / 8))) 255 255 255 255 \
 		$(((255 << (8 - ($1 % 8))) & 255)) 0 0 0
 	if [[ $1 -gt 1 ]]; then
@@ -216,7 +219,7 @@ convert_netmask() {
 			0) ;;
 		esac
 	done
-	echo "$nbits"
+	echo "/$nbits"
 }
 
 # Calculate broadcast address
@@ -439,38 +442,6 @@ get_real_ipv6() {
     fi
 }
 
-# Convert ipv6 CIDR to netmask
-convert_cidrv6() {
-    set -- $(( 5 - ($1 / 8) )) 255 255 255 255 \
-        $(((255 << (8 - ($1 % 8))) & 255 )) 0 0 0
-    if [[ $1 -gt 1 ]]; then
-        shift $1
-    else
-        shift
-    fi
-    echo ${1-0}.${2-0}.${3-0}.${4-0}
-}
-
-# Convert ipv6 netmask to CIDR
-convert_netmaskv6() {
-    nbits=0
-    IFS=.
-    for dec in $1 ; do
-        case $dec in
-            255) let nbits+=8;;
-            254) let nbits+=7;;
-            252) let nbits+=6;;
-            248) let nbits+=5;;
-            240) let nbits+=4;;
-            224) let nbits+=3;;
-            192) let nbits+=2;;
-            128) let nbits+=1;;
-            0);;
-        esac
-    done
-    echo "$nbits"
-}
-
 # Get user ip6s
 get_user_ip6s() {
     dedicated=$(grep -H -A10 "OWNER='$user'" $HESTIA/data/ips/* |grep "VERSION='6'")

+ 1 - 0
func/main.sh

@@ -1220,6 +1220,7 @@ is_format_valid() {
 				charset) is_object_format_valid "$arg" "$arg_name" ;;
 				charsets) is_common_format_valid "$arg" 'charsets' ;;
 				chain) is_object_format_valid "$arg" 'chain' ;;
+				cidr) is_ip_format_valid "$arg" 'cidr' ;;
 				comment) is_object_format_valid "$arg" 'comment' ;;
 				database) is_database_format_valid "$arg" 'database' ;;
 				day) is_cron_format_valid "$arg" $arg_name ;;