From c4d471e41817a7e0935988be20161a406ca26741 Mon Sep 17 00:00:00 2001 From: krw Date: Fri, 23 Jun 2017 15:40:56 +0000 Subject: [PATCH] Take reyk's imsg resolv.conf improvements of a while ago to their logical conclusion. Nuke _PATH_RESOLV_CONF since the value is only meant to be known inside priv_write_resolv_conf(). Just use a local const char *. Bring priv_write_resolv_conf() into line with other priv_ functions invoked from the dispatch loop. i.e. don't pass it the imsg, just pass a pointer to the data and a size after ensuring there is data to pass. --- sbin/dhclient/dhcpd.h | 3 +-- sbin/dhclient/kroute.c | 24 +++++++----------------- sbin/dhclient/privsep.c | 9 +++++++-- sbin/dhclient/privsep.h | 4 ++-- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index 2e4d70f2a99..a3d279fe751 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.191 2017/06/22 15:08:53 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.192 2017/06/23 15:40:56 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -157,7 +157,6 @@ struct interface_info { TAILQ_HEAD(_leases, client_lease) leases; }; -#define _PATH_RESOLV_CONF "/etc/resolv.conf" #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf" #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases" diff --git a/sbin/dhclient/kroute.c b/sbin/dhclient/kroute.c index 5ac34c37880..878496f8293 100644 --- a/sbin/dhclient/kroute.c +++ b/sbin/dhclient/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.91 2017/04/12 12:22:25 krw Exp $ */ +/* $OpenBSD: kroute.c,v 1.92 2017/06/23 15:40:56 krw Exp $ */ /* * Copyright 2012 Kenneth R Westerback @@ -782,39 +782,29 @@ write_resolv_conf(u_int8_t *contents, size_t sz) } void -priv_write_resolv_conf(struct interface_info *ifi, struct imsg *imsg) +priv_write_resolv_conf(struct interface_info *ifi, u_int8_t *contents, size_t sz) { - u_int8_t *contents; + const char *path = "/etc/resolv.conf"; ssize_t n; - size_t sz; int fd; - - if (imsg->hdr.len < IMSG_HEADER_SIZE) { - log_warnx("short IMSG_WRITE_RESOLV_CONF"); - return; - } - if (!resolv_conf_priority(ifi)) return; - contents = imsg->data; - sz = imsg->hdr.len - IMSG_HEADER_SIZE; - - fd = open(_PATH_RESOLV_CONF, O_WRONLY | O_CREAT | O_TRUNC, + fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { - log_warn("Couldn't open '%s'", _PATH_RESOLV_CONF); + log_warn("Couldn't open '%s'", path); return; } n = write(fd, contents, sz); if (n == -1) - log_warn("Couldn't write contents to '%s'", _PATH_RESOLV_CONF); + log_warn("Couldn't write contents to '%s'", path); else if ((size_t)n < sz) log_warnx("Short contents write to '%s' (%zd vs %zu)", - _PATH_RESOLV_CONF, n, sz); + path, n, sz); close(fd); } diff --git a/sbin/dhclient/privsep.c b/sbin/dhclient/privsep.c index 64898a8e9e4..23e4ae96f4b 100644 --- a/sbin/dhclient/privsep.c +++ b/sbin/dhclient/privsep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.c,v 1.46 2017/04/10 21:47:44 krw Exp $ */ +/* $OpenBSD: privsep.c,v 1.47 2017/06/23 15:40:56 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -90,7 +90,12 @@ dispatch_imsg(struct interface_info *ifi, struct imsgbuf *ibuf) break; case IMSG_WRITE_RESOLV_CONF: - priv_write_resolv_conf(ifi, &imsg); + if (imsg.hdr.len <= IMSG_HEADER_SIZE) { + log_warnx("short IMSG_WRITE_RESOLV_CONF"); + return; + } else + priv_write_resolv_conf(ifi, imsg.data, + imsg.hdr.len - IMSG_HEADER_SIZE); break; case IMSG_HUP: diff --git a/sbin/dhclient/privsep.h b/sbin/dhclient/privsep.h index ab94db67c31..3d367634bac 100644 --- a/sbin/dhclient/privsep.h +++ b/sbin/dhclient/privsep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.h,v 1.35 2017/04/11 13:59:27 krw Exp $ */ +/* $OpenBSD: privsep.h,v 1.36 2017/06/23 15:40:56 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -69,7 +69,7 @@ void priv_flush_routes(struct interface_info *, struct imsg_flush_routes *); char *resolv_conf_contents(struct interface_info *ifi, struct option_data *, struct option_data *, struct option_data *); void write_resolv_conf(u_int8_t *, size_t); -void priv_write_resolv_conf(struct interface_info *, struct imsg *); +void priv_write_resolv_conf(struct interface_info *, u_int8_t *, size_t); void priv_delete_address(struct interface_info *, struct imsg_delete_address *); -- 2.20.1