From 56a9c03a7a53e11240e6a7128be90b28a1be3c79 Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 11 Apr 2017 10:40:14 +0000 Subject: [PATCH] Fold priv_write_file() into its only remaining user priv_write_resolv_conf() and move the latter into kroute.c with all its priv_ friends. No intentional functional change. --- sbin/dhclient/dhclient.c | 52 +--------------------------------------- sbin/dhclient/dhcpd.h | 3 ++- sbin/dhclient/kroute.c | 45 +++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index a2189c2f66d..beda16591f4 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.412 2017/04/10 21:47:44 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.413 2017/04/11 10:40:14 krw Exp $ */ /* * Copyright 2004 Henning Brauer @@ -2547,34 +2547,6 @@ apply_ignore_list(char *ignore_list) memcpy(config->ignored_options, list, sizeof(config->ignored_options)); } -void -priv_write_file(char *path, int flags, mode_t mode, - u_int8_t *contents, size_t sz) -{ - ssize_t n; - int fd; - - fd = open(path, flags, mode); - if (fd == -1) { - 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); - else if ((size_t)n < sz) - log_warnx("Short contents write to '%s' (%zd vs %zu)", path, - n, sz); - - if (fchown(fd, 0, 0) == -1) - log_warn("fchown(fd, %d, %d) of '%s' failed", 0, 0, path); - if (fchmod(fd, mode) == -1) - log_warn("fchmod(fd, 0x%x) of '%s' failed", mode, path); - - close(fd); -} - void set_lease_times(struct client_lease *lease) { @@ -2742,28 +2714,6 @@ write_resolv_conf(u_int8_t *contents, size_t sz) flush_unpriv_ibuf("write_resolv_conf"); } -void -priv_write_resolv_conf(struct interface_info *ifi, struct imsg *imsg) -{ - u_int8_t *contents; - size_t sz; - - 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; - - priv_write_file("/etc/resolv.conf", - O_WRONLY | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, contents, sz); -} - /* * add_direct_route is the equivalent of * diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index 9f905209af5..ea88b3dfdc0 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.169 2017/04/08 20:16:04 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.170 2017/04/11 10:40:14 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -172,6 +172,7 @@ struct dhcp_timeout { void *arg; }; +#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 5686aea5a99..543f26b9f2d 100644 --- a/sbin/dhclient/kroute.c +++ b/sbin/dhclient/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.88 2017/04/09 20:44:13 krw Exp $ */ +/* $OpenBSD: kroute.c,v 1.89 2017/04/11 10:40:14 krw Exp $ */ /* * Copyright 2012 Kenneth R Westerback @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -30,6 +31,7 @@ #include #include +#include #include #include #include @@ -531,6 +533,47 @@ priv_cleanup(struct interface_info *ifi, struct imsg_hup *imsg) priv_delete_address(ifi, &dimsg); } +/* + * priv_write_resolv_conf writes out a new resolv.conf. + */ +void +priv_write_resolv_conf(struct interface_info *ifi, struct imsg *imsg) +{ + u_int8_t *contents; + 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, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + if (fd == -1) { + log_warn("Couldn't open '%s'", _PATH_RESOLV_CONF); + return; + } + + n = write(fd, contents, sz); + if (n == -1) + log_warn("Couldn't write contents to '%s'", _PATH_RESOLV_CONF); + else if ((size_t)n < sz) + log_warnx("Short contents write to '%s' (%zd vs %zu)", + _PATH_RESOLV_CONF, n, sz); + + close(fd); +} + int resolv_conf_priority(struct interface_info *ifi) { -- 2.20.1