]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/resolve/resolved-etc-hosts.c
resolve: add option to toggle reading /etc/hosts
[thirdparty/systemd.git] / src / resolve / resolved-etc-hosts.c
index 6ccbdca20ebdbbfd7720455326aa1ecb54a9effa..5d394a863939f7c9a2f46b535d1659eab5af58df 100644 (file)
@@ -1,21 +1,4 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2016 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "fd-util.h"
 #include "fileio.h"
@@ -96,7 +79,7 @@ static const struct hash_ops etc_hosts_item_ops = {
 };
 
 static int add_item(Manager *m, int family, const union in_addr_union *address, char **names) {
-
+        _cleanup_strv_free_ char **dummy = names;
         EtcHostsItem key = {
                 .family = family,
                 .address = *address,
@@ -129,19 +112,23 @@ static int add_item(Manager *m, int family, const union in_addr_union *address,
                         if (r < 0)
                                 return log_oom();
 
-                        item = new0(EtcHostsItem, 1);
+                        item = new(EtcHostsItem, 1);
                         if (!item)
                                 return log_oom();
 
-                        item->family = family;
-                        item->address = *address;
-                        item->names = names;
+                        *item = (EtcHostsItem) {
+                                .family = family,
+                                .address = *address,
+                                .names = names,
+                        };
 
                         r = set_put(m->etc_hosts_by_address, item);
                         if (r < 0) {
                                 free(item);
                                 return log_oom();
                         }
+
+                        dummy = NULL;
                 }
         }
 
@@ -249,7 +236,7 @@ static int parse_line(Manager *m, unsigned nr, const char *line) {
         return r;
 }
 
-int manager_etc_hosts_read(Manager *m) {
+static int manager_etc_hosts_read(Manager *m) {
         _cleanup_fclose_ FILE *f = NULL;
         char line[LINE_MAX];
         struct stat st;
@@ -301,7 +288,7 @@ int manager_etc_hosts_read(Manager *m) {
         FOREACH_LINE(line, f, return log_error_errno(errno, "Failed to read /etc/hosts: %m")) {
                 char *l;
 
-                nr ++;
+                nr++;
 
                 l = strstrip(line);
                 if (isempty(l))
@@ -337,6 +324,9 @@ int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
         assert(q);
         assert(answer);
 
+        if (!m->read_etc_hosts)
+                return 0;
+
         r = manager_etc_hosts_read(m);
         if (r < 0)
                 return r;
@@ -431,8 +421,8 @@ int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
         for (i = 0; i < bn->n_items; i++) {
                 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
-                if ((found_a && bn->items[i]->family != AF_INET) &&
-                    (found_aaaa && bn->items[i]->family != AF_INET6))
+                if ((!found_a && bn->items[i]->family == AF_INET) ||
+                    (!found_aaaa && bn->items[i]->family == AF_INET6))
                         continue;
 
                 r = dns_resource_record_new_address(&rr, bn->items[i]->family, &bn->items[i]->address, bn->name);
@@ -444,5 +434,5 @@ int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
                         return r;
         }
 
-        return 1;
+        return found_a || found_aaaa;
 }