Commit a3a87c05 authored by Yuanle Song's avatar Yuanle Song
Browse files

check IP is a valid public IP. bugfix: literal dot in regexp should be \.

parent c529ef37
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ Time-stamp: <2012-05-27>
    - do not update when fatal error is not cleared by user.
    - use IP from local router.
    - only update when IP is different from last successful update.
    - send email to EMAIL and FATAL_ERROR_EMAIL
    - send email to EMAIL and FATAL_ERROR_EMAIL.
    - check IP is a valid public IP.
* v0.1.0
  - init version. was used to update sylecn01.dnsd.me.
+22 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ UPDATE_REQUEST_FAILED=3
UPDATE_REJECTED=4
UPDATE_NOCHG=5
FATAL_ERROR_EXISTS=6
BAD_IP_ADDRESS=7
NO_CONF=50

# stores responses from remote DDNS provider's server.  usually it will be
@@ -77,7 +78,7 @@ get_myip() {
		exit $GET_MYIP_FAILED
	fi

	if echo "$MYIP" | egrep -q -e '^[0-9]{1,3}(.[0-9]{1,3}){3}$'; then
	if echo "$MYIP" | egrep -q -e '^[0-9]{1,3}(\.[0-9]{1,3}){3}$'; then
		return 0
	else
		err "Error: IP address $MYIP is not well-formed."
@@ -108,6 +109,19 @@ require_update() {
	fi
}

# Usage: is_public_ip IP
# returns true if IP is not in private address space or some special IP address.
is_public_ip() {
	IP="$1"
	if [ -z "$IP" ]; then
		return 1
	fi
	if `echo $IP | egrep -q -e '^(10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.|0\.0\.0\.0|127\.0\.0\.1|255\.255\.255\.255|224\.0\.0\.1|169\.254\.)'`; then
		return 1
	fi
	return 0
}

do_update() {
	log "update $HOSTNAME using IP $MYIP"
	if [ -f "$FATAL_ERROR_FILE" ]; then
@@ -115,6 +129,13 @@ do_update() {
Delete $FATAL_ERROR_FILE when you cleared the issue."
		exit $FATAL_ERROR_EXISTS
	fi

	# check IP is a public IP.
	if ! is_public_ip "$MYIP"; then
		err "Error: bad IP address: $MYIP."
		exit $BAD_IP_ADDRESS
	fi

	URL=`printf $UPDATE_URL $MYIP`
	curl -s -o "$HTTP_RESPONSE" "$URL"
	# check curl EXIT CODES