summaryrefslogtreecommitdiff
path: root/hostname
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2022-04-23 03:17:26 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2022-04-23 03:17:26 -0500
commit452f87ace48b6cc710e2b50a749c818825455493 (patch)
treed19cc8f6ba966d0a0f88f00b1ed5091a4263c77e /hostname
parent2a708d9795c2474be8af2b4920c3806fc7b8f5cf (diff)
downloadshimmy-452f87ace48b6cc710e2b50a749c818825455493.tar.gz
shimmy-452f87ace48b6cc710e2b50a749c818825455493.tar.bz2
shimmy-452f87ace48b6cc710e2b50a749c818825455493.tar.xz
shimmy-452f87ace48b6cc710e2b50a749c818825455493.zip
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.
Diffstat (limited to 'hostname')
-rw-r--r--hostname/hostname.c18
1 files changed, 13 insertions, 5 deletions
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);
}
}