From 452f87ace48b6cc710e2b50a749c818825455493 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 23 Apr 2022 03:17:26 -0500 Subject: hostname: Fix file reading for setting node name This isn't exactly compatible with net-tools, but it is close. net-tools will buffer a line longer than 1024 characters (up to the next 63 characters). This implementation will break at the 1024th character and start a new block of 63 char names there. I sincerely hope nobody is actually using this weird, undefined behaviour for anything serious. --- hostname/hostname.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'hostname') diff --git a/hostname/hostname.c b/hostname/hostname.c index d19e4b8..b994a07 100644 --- a/hostname/hostname.c +++ b/hostname/hostname.c @@ -132,7 +132,7 @@ int do_set_nodename(bool file, const char *value) /* This is insane. There is no validation whatsoever. * However, strace on net-tools hostname(1) shows this to be * the exact algorithm used, so... */ - char buf[1024]; + char buf[1025]; int fildes = open(value, O_RDONLY); ssize_t result = 0; @@ -145,7 +145,8 @@ int do_set_nodename(bool file, const char *value) while(true) { char *curr = buf; - result = read(fildes, buf, sizeof buf); + memset(buf, 0, sizeof buf); + result = read(fildes, buf, sizeof buf - 1); switch(result) { case -1: @@ -158,14 +159,21 @@ int do_set_nodename(bool file, const char *value) } do { - len = result; + len = strnlen(curr, result); char *pos = strchr(curr, '\n'); if(pos) { len = pos - curr; } - sethostname(curr, (len > 63) ? 63 : len); - } while((curr = strchr(curr, '\n'))); + if(len == 0 || *curr == '#') continue; + while(len > 63) + { + sethostname(curr, 63); + len -= 63; + curr += 63; + } + sethostname(curr, len); + } while(curr = strchr(curr, '\n') + 1, curr != 0x1); } } -- cgit v1.2.3-70-g09d2