]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
examples: replace grep command in NM dispatcher script
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 23 Mar 2022 14:17:03 +0000 (15:17 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 23 Mar 2022 14:36:17 +0000 (15:36 +0100)
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

index 4454f0371c941c4c1d6ad2dd0889f1a6de84dc6f..547ce83f5bd113201f7ed65471d4a5f2cd892d2d 100644 (file)
@@ -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