From 5fae48753c47bc37b2f38413e391a9da0760998d Mon Sep 17 00:00:00 2001 From: Clemens John Date: Tue, 1 Oct 2013 17:23:45 +0000 Subject: [PATCH] configurator: Check if hostname is valid RFC 1123 (check length of labels, check the whole length and allow . and - where allowed Signed-off-by: Clemens John Rebased: Tim Niemeyer --- .../root_file_system/etc/configurator.sh | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/bsp/default/root_file_system/etc/configurator.sh b/bsp/default/root_file_system/etc/configurator.sh index 8f2b40b..136e835 100644 --- a/bsp/default/root_file_system/etc/configurator.sh +++ b/bsp/default/root_file_system/etc/configurator.sh @@ -52,19 +52,27 @@ sync_hostname() { elif [ "$ret" = "success" ]; then netmon_hostname=${api_return%,*} netmon_hostname=${netmon_hostname#*,} - #use regex to check if hostname is valid (not empty, only characters and numbers) - #http://stackoverflow.com/questions/8696906/check-for-valid-number-in-busybox - if echo -n $netmon_hostname | egrep -q '^[A-Za-z0-9]*$'; then - if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then - err "Setting new hostname: $netmon_hostname" - uci set system.@system[0].hostname=$netmon_hostname - uci commit - echo $netmon_hostname > /proc/sys/kernel/hostname + + #check for valid hostname as specified in rfc 1123 + #see http://stackoverflow.com/a/3824105 + regex='^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])' + regex=$regex'(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$' + if [ ${#netmon_hostname} -le 255 ]; then + if echo -n $netmon_hostname | egrep -q "$regex"; then + if [ "$netmon_hostname" != "`cat /proc/sys/kernel/hostname`" ]; then + err "Setting new hostname: $netmon_hostname" + uci set system.@system[0].hostname=$netmon_hostname + uci commit + echo $netmon_hostname > /proc/sys/kernel/hostname + else + err "Hostname is up to date" + fi else - err "Hostname is up to date" + err "Hostname ist malformed" + exit 0 fi else - err "Hostname ist malformed" + err "Hostname exceeds the maximum length of 255 characters" exit 0 fi fi