From fca8966adaaf8376536af86ba2afe02501463588 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 23 Mar 2022 15:17:03 +0100 Subject: [PATCH] examples: replace grep command in NM dispatcher script Some grep implementations detect binary data and return success without matching whole line. This might be an issue for the DHCPv6 NTP FQDN check. The GNU grep in the C locale seems to check only for the NUL character, which cannot be passed in an environment variable, but other implementations might behave differently and there doesn't seem to be a portable way to force matching the whole line. Instead of the grep command, check for invalid characters by comparing the length of the input passed through "tr -d -c". --- examples/chrony.nm-dispatcher.dhcp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/chrony.nm-dispatcher.dhcp b/examples/chrony.nm-dispatcher.dhcp index 4454f03..547ce83 100644 --- a/examples/chrony.nm-dispatcher.dhcp +++ b/examples/chrony.nm-dispatcher.dhcp @@ -19,7 +19,11 @@ add_servers_from_dhcp() { rm -f "$dhcp_server_file" for server in $dhcp_ntp_servers; do # Check for invalid characters (from the DHCPv6 NTP FQDN suboption) - printf '%s\n' "$server" | grep -E -q '^[-A-Za-z0-9:.]{1,255}$' || continue + len1=$(printf '%s' "$server" | wc -c) + len2=$(printf '%s' "$server" | tr -d -c 'A-Za-z0-9:.-' | wc -c) + if [ "$len1" -ne "$len2" ] || [ "$len2" -lt 1 ] || [ "$len2" -gt 255 ]; then + continue + fi printf 'server %s %s\n' "$server" "$server_options" >> "$dhcp_server_file" done