diff options
author | Tom Gundersen <teg@jklm.no> | 2014-01-05 23:01:10 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-01-12 15:37:21 +0100 |
commit | 3bef724f7e7f7eaca69881548b06e221b77d7031 (patch) | |
tree | 0b17e5a43d7e5c5d07dc696d23d4c829f9ab7200 /src/network/networkd-manager.c | |
parent | 924fe4304af981ffd849346b4a1d415f11e9dd79 (diff) |
networkd: generate resolv.conf
This adds support to generate a basic resolv.conf in /run/systemd/network.
This file will not take any effect unless a symlink is created from
/etc/resolv.conf.
Nameservers received over DHCP takes precedence over statically configured ones.
Note: /etc/resolv.conf is severely limited, so in the future we will likely
rather provide a much more powerfull nss plugin (or something to that effect),
but this should allow current users to function without any loss of
functionality.
Diffstat (limited to 'src/network/networkd-manager.c')
-rw-r--r-- | src/network/networkd-manager.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index c9ce1d6a13..f02eed1a59 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -19,10 +19,13 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ +#include <resolv.h> + #include "path-util.h" #include "networkd.h" #include "libudev-private.h" #include "udev-util.h" +#include "mkdir.h" const char* const network_dirs[] = { "/etc/systemd/network", @@ -276,3 +279,75 @@ int manager_rtnl_listen(Manager *m) { return 0; } + +static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) { + char buf[INET6_ADDRSTRLEN]; + const char *address; + + address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN); + if (!address) { + log_warning("Invalid DNS address. Ignoring."); + return; + } + + if (*count == MAXNS) + fputs("# Too many dynamic name servers configured, the " + "following entries will be ignored\n", f); + + fprintf(f, "nameserver %s\n", address); + + (*count) ++; +} + +int manager_update_resolv_conf(Manager *m) { + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + Link *link; + Iterator i; + unsigned count = 0; + int r; + + assert(m); + + r = mkdir_safe_label("/run/systemd/network", 0755, 0, 0); + if (r < 0) + return r; + + r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path); + if (r < 0) + return r; + + fchmod(fileno(f), 0644); + + fputs("# This file is managed by systemd-networkd(8). Do not edit.\n", f); + + HASHMAP_FOREACH(link, m->links, i) { + if (link->dhcp) { + struct in_addr **nameservers; + + r = sd_dhcp_client_get_dns(link->dhcp, &nameservers); + if (r >= 0) { + unsigned j; + + for (j = 0; nameservers[j]; j++) + append_dns(f, nameservers[j], AF_INET, &count); + } + } + } + + HASHMAP_FOREACH(link, m->links, i) + if (link->network && link->network->dns) + append_dns(f, &link->network->dns->in_addr.in, + link->network->dns->family, &count); + + fflush(f); + + if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) { + r = -errno; + unlink("/run/systemd/network/resolv.conf"); + unlink(temp_path); + return r; + } + + return 0; +} |