dhcpd server invalid mac detection

On my dhcp configuration, i have on commit hook to save device information. My problem is some of the mac addresses becomes invalid:

8:7c:39:cf:b6:3f - this should start with zero

8:d0:b7:52:f9:68 - also this

my dhcpd.conf

set clientmac = binary-to-ascii(16,8,":",substring(hardware,1,6));


Solution 1:

It depends on what you use to parse it, one can argue that it is completely valid to leave out prefix zeroes, and that is what we do most of the time because there is no definition of how many digits there should be.

However if we skip over the part about if this is being invalid and why or not and instead ask "How do I get this in desired format" we can provide a answer.

In this case isc has a KB article about it

This is not a bug. The problem is that the binary-to-ascii function doesn't "know" anything about the intended use of the converted binary digits and it's unusual to include leading zeroes when printing numeric values.

However, with a bit of additional manipulation it's still possible to get the desired result:

set foo = concat (
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,1,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,2,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,3,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,4,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,5,1))),2), ":",
suffix (concat ("0", binary-to-ascii (16, 8, "", substring(hardware,6,1))),2)
);

(It operates by converting each "component" separately, adding a leading zero to it (in case one is needed); taking the last two hex characters, and then concatenating them all together again.)