From 6a87c26b84e5cab7f4a8ead8abd62e58b7ec7f6c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Sep 2013 22:57:07 -0400 Subject: arch-nspawn: limit the machine_name length to ${HOST_NAME_MAX:-64} chars. The criteria for a valid name are: - is made of of alphanumerics, "_", ".", and "-" - is not emtpy - cannot end with "." - cannot contain ".." - the maximum length is HOST_NAME_MAX, as defined in The existing code takes care of all but the last of these. So, added code after the existing code to check the length of the string, and grab the longest possible substring from the end. I hard-coded our interpretation of HOST_NAME_MAX as 64--on Linux that has been the value since version 1.0 of the kernel... I don't think it will be changing any time soon, and since systemd is Linux-only, we don't have to worry about other kernels. But, just in case, I included a commented out version that uses cpp to get the value. --- arch-nspawn.in | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch-nspawn.in b/arch-nspawn.in index bde77b6..7dcd8ca 100644 --- a/arch-nspawn.in +++ b/arch-nspawn.in @@ -96,6 +96,12 @@ eval $(grep '^CARCH=' "$working_dir/etc/makepkg.conf") machine_name="${working_dir//[![:alnum:]_-]/-}" machine_name="${machine_name#-}" +#HOST_NAME_MAX="$(printf '%s\n' '#include ' 'HOST_NAME_MAX'|cpp -|sed -n '$p')" +HOST_NAME_MAX=64 +if [[ ${#machine_name} -gt "$HOST_NAME_MAX" ]]; then + machine_name="${machine_name:(-${HOST_NAME_MAX})}" + machine_name="${machine_name#-}" +fi exec ${CARCH:+setarch "$CARCH"} systemd-nspawn \ -D "$working_dir" \ -- cgit v1.2.3-70-g09d2