Commit f29bd833 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Check if Windows ComputerName is valid

The ComputerName in Windows has strong restrictions:
    https://technet.microsoft.com/en-us/library/ff715676.aspx
If the name we set in the unattend.xml is not valid, then sysprep
will fail during the first boot.

This resolves #81
parent 942cc07c
......@@ -973,6 +973,48 @@ networking_opts() {
fi
}
check_windows_computer_name() {
local name length msg matched pipefail
name="${1}"
length=$(wc -c <<< "$name")
# ComputerName restrictions are documented here:
# https://technet.microsoft.com/en-us/library/ff715676.aspx
msg="Invalid Windows ComputerName: \`$name'."
# Check if the length of the string exceeds the 15 bytes
if [ $length -gt 15 ]; then
log_error "$msg It's size is bigger (=$length) than 15 bytes."
fi
pipefail=$(set -o | grep pipefail | awk '{ print $2 }')
set -o pipefail
set +e
# We only check for invalid ascii characters but this is OK. The name
# derives from the instance's name and Ganeti seems to only allows ascii
# characters for instance names.
invalid_char="$(grep -o -P \
'[\x00-\x2c\x2e-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7e]' <<< "$name" \
| head -1)"
# If grep fails, the pipefail option will make $matched host the return
# code of grep
matched=$?
set -e
if [ "$pipefail" = off ]; then
set +o pipefail
fi
if [ $matched -eq 0 ]; then
log_error "$msg Contains invalid character: \`$(printf "%q" "$invalid_char")'."
fi
if [[ "$name" =~ ^([0-9])+$ ]]; then
log_error "$msg Contains only numbers (0-9)."
fi
}
trap cleanup EXIT
set -o pipefail
......
......@@ -35,9 +35,11 @@ check_if_excluded
check_if_mounted_excluded
windows_hostname() {
local target password unattend tmp_unattend namespace
local target hostname unattend tmp_unattend namespace
target="$1"
password="$2"
hostname="$2"
check_windows_computer_name "$hostname"
tmp_unattend=$(mktemp)
add_cleanup rm "$tmp_unattend"
......@@ -51,7 +53,7 @@ windows_hostname() {
log_error "Unattend.xml is missing."
fi
"$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$unattend" > "$tmp_unattend"
"$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$hostname" "$unattend" > "$tmp_unattend"
cat "$tmp_unattend" > "$unattend"
echo done
......
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