From c589917e7152dd2ed6eb4e63bfdd5c98ee94e00b Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 24 Jun 2022 12:17:30 +1000 Subject: Parse resolv.conf with new lineread implementation Switch the resolv.conf parsing in conf.c to use the new lineread implementation. This means that it can now handle a resolv.conf file which contains blank lines. There are quite a few other fragilities with the resolv.conf parsing, but that's out of scope for this patch. Signed-off-by: David Gibson --- conf.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'conf.c') diff --git a/conf.c b/conf.c index 3a5d650..22949fd 100644 --- a/conf.c +++ b/conf.c @@ -38,6 +38,7 @@ #include "udp.h" #include "tcp.h" #include "pasta.h" +#include "lineread.h" /** * get_bound_ports() - Get maps of ports with bound sockets @@ -320,7 +321,9 @@ static void get_dns(struct ctx *c) struct in6_addr *dns6 = &c->dns6[0]; struct fqdn *s = c->dns_search; uint32_t *dns4 = &c->dns4[0]; - char buf[BUFSIZ], *p, *end; + struct lineread resolvconf; + int line_len; + char *line, *p, *end; dns4_set = !c->v4 || !!*dns4; dns6_set = !c->v6 || !IN6_IS_ADDR_UNSPECIFIED(dns6); @@ -333,13 +336,14 @@ static void get_dns(struct ctx *c) if ((fd = open("/etc/resolv.conf", O_RDONLY | O_CLOEXEC)) < 0) goto out; - for (*buf = 0; line_read(buf, BUFSIZ, fd); *buf = 0) { - if (!dns_set && strstr(buf, "nameserver ") == buf) { - p = strrchr(buf, ' '); + lineread_init(&resolvconf, fd); + while ((line_len = lineread_get(&resolvconf, &line)) > 0) { + if (!dns_set && strstr(line, "nameserver ") == line) { + p = strrchr(line, ' '); if (!p) continue; - end = strpbrk(buf, "%\n"); + end = strpbrk(line, "%\n"); if (end) *end = 0; @@ -356,13 +360,13 @@ static void get_dns(struct ctx *c) dns6++; memset(dns6, 0, sizeof(*dns6)); } - } else if (!dnss_set && strstr(buf, "search ") == buf && + } else if (!dnss_set && strstr(line, "search ") == line && s == c->dns_search) { - end = strpbrk(buf, "\n"); + end = strpbrk(line, "\n"); if (end) *end = 0; - if (!strtok(buf, " \t")) + if (!strtok(line, " \t")) continue; while (s - c->dns_search < ARRAY_SIZE(c->dns_search) - 1 @@ -374,6 +378,8 @@ static void get_dns(struct ctx *c) } } + if (line_len < 0) + warn("Error reading /etc/resolv.conf: %s", strerror(errno)); close(fd); out: -- cgit v1.2.3