validate CIDR

This isn't meant to be precise but supposed to guard against silly
mistakes.

Closes #116.
This commit is contained in:
Matthias Radestock committed 2014-10-25 22:04:36 +01:00
1 parent 007b68c206
commit d8bea832d3
1 file changed
+16
+16
View File
@@ -55,6 +55,15 @@ check_command_and_run() {
fi fi
} }
validate_cidr() {
# The regexp here is far from precise, but good enough.
if ! echo "$1" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$' >/dev/null ; then
echo "Invalid CIDR: $1" >&2
echo "CIDR must of be of form <ip_address>/<routing_prefix_length>, e.g. 10.0.1.1/24" >&2
exit 1
fi
}
run_iptables() { run_iptables() {
# -w is recent addition to iptables # -w is recent addition to iptables
if [ -z "$CHECKED_IPTABLES_W" ] ; then if [ -z "$CHECKED_IPTABLES_W" ] ; then
@@ -223,6 +232,7 @@ detach() {
case "$COMMAND" in case "$COMMAND" in
launch) launch)
[ $# -gt 0 ] || usage [ $# -gt 0 ] || usage
validate_cidr $1
CIDR=$1 CIDR=$1
shift 1 shift 1
case $(docker inspect --format '{{ .State.Running }} {{ .Config.Image }}' $CONTAINER_NAME 2>/dev/null) in case $(docker inspect --format '{{ .State.Running }} {{ .Config.Image }}' $CONTAINER_NAME 2>/dev/null) in
@@ -291,6 +301,7 @@ case "$COMMAND" in
;; ;;
run) run)
[ $# -gt 0 ] || usage [ $# -gt 0 ] || usage
validate_cidr $1
CIDR=$1 CIDR=$1
shift 1 shift 1
create_bridge create_bridge
@@ -300,6 +311,7 @@ case "$COMMAND" in
;; ;;
start) start)
[ $# -eq 2 ] || usage [ $# -eq 2 ] || usage
validate_cidr $1
create_bridge create_bridge
CONTAINER=$(docker start $2) CONTAINER=$(docker start $2)
with_container_netns $CONTAINER attach $1 $CONTAINER_IFNAME with_container_netns $CONTAINER attach $1 $CONTAINER_IFNAME
@@ -307,15 +319,18 @@ case "$COMMAND" in
;; ;;
attach) attach)
[ $# -eq 2 ] || usage [ $# -eq 2 ] || usage
validate_cidr $1
create_bridge create_bridge
with_container_netns $2 attach $1 $CONTAINER_IFNAME with_container_netns $2 attach $1 $CONTAINER_IFNAME
;; ;;
detach) detach)
[ $# -eq 2 ] || usage [ $# -eq 2 ] || usage
validate_cidr $1
with_container_netns $2 detach $1 with_container_netns $2 detach $1
;; ;;
expose) expose)
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
validate_cidr $1
create_bridge create_bridge
if ! ip addr show dev $BRIDGE | grep -qF $1 if ! ip addr show dev $BRIDGE | grep -qF $1
then then
@@ -324,6 +339,7 @@ case "$COMMAND" in
;; ;;
hide) hide)
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
validate_cidr $1
create_bridge create_bridge
if ip addr show dev $BRIDGE | grep -qF $1 if ip addr show dev $BRIDGE | grep -qF $1
then then