aboutsummaryrefslogtreecommitdiffstats
path: root/setup-hostname.in
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2009-08-19 13:20:44 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2009-08-19 13:20:44 +0000
commit8bee519d1aa741ef8cb7677b77b0a717c3395648 (patch)
tree0d5e24440d685262b993529a06628f2e8040867d /setup-hostname.in
parent4be25548bc8e1789453014db90b213bd1756d082 (diff)
downloadalpine-conf-8bee519d1aa741ef8cb7677b77b0a717c3395648.tar.bz2
alpine-conf-8bee519d1aa741ef8cb7677b77b0a717c3395648.tar.xz
setup-hostname: validate the given hostname
Diffstat (limited to 'setup-hostname.in')
-rwxr-xr-xsetup-hostname.in30
1 files changed, 27 insertions, 3 deletions
diff --git a/setup-hostname.in b/setup-hostname.in
index b1c2379..f97f487 100755
--- a/setup-hostname.in
+++ b/setup-hostname.in
@@ -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"