Commit 8bee519d authored by Natanael Copa's avatar Natanael Copa

setup-hostname: validate the given hostname

parent 4be25548
......@@ -5,9 +5,33 @@ PREFIX=
. $PREFIX/lib/libalpine.sh
HOST=`hostname`
echon "Enter system hostname (short form, e.g. 'foo') [$HOST]: "
default_read HOST `hostname`
# http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
valid_hostname() {
# check length
if [ $(echo "$1" | wc -c) -gt 63 ]; then
echo "Hostname '$1' is too long."
return 1
fi
# check that it only contains valid chars
if ! [ -z "$(echo $1 | sed 's/[0-9a-z-]//g')" ]; then
echo "Hostname must only contain letters (a-z), digits (0-9) or -"
return 1
fi
# must not start with -
case "$1" in
-*) echo "Hostname must not start with a '-'"; return 1;;
esac
return 0
}
while true; do
HOST=`hostname`
echon "Enter system hostname (short form, e.g. 'foo') [$HOST]: "
default_read HOST `hostname`
if valid_hostname "$HOST"; then
break
fi
done
mkdir -p "$ROOT/etc"
echo "$HOST" > "$ROOT/etc/hostname"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment