]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-conf.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / resolve / resolved-conf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "conf-parser.h"
23 #include "parse-util.h"
24 #include "resolved-conf.h"
25 #include "extract-word.h"
26 #include "string-util.h"
27
28 int manager_parse_dns_server(Manager *m, DnsServerType type, const char *string) {
29 DnsServer *first;
30 int r;
31
32 assert(m);
33 assert(string);
34
35 first = type == DNS_SERVER_FALLBACK ? m->fallback_dns_servers : m->dns_servers;
36
37 for(;;) {
38 _cleanup_free_ char *word;
39 union in_addr_union addr;
40 bool found = false;
41 DnsServer *s;
42 int family;
43
44 r = extract_first_word(&string, &word, NULL, 0);
45 if (r < 0)
46 return log_error_errno(r, "Failed to parse resolved dns server syntax \"%s\": %m", string);
47
48 if (r == 0)
49 break;
50
51 r = in_addr_from_string_auto(word, &family, &addr);
52 if (r < 0) {
53 log_warning("Ignoring invalid DNS address '%s'", word);
54 continue;
55 }
56
57 /* Filter out duplicates */
58 LIST_FOREACH(servers, s, first)
59 if (s->family == family && in_addr_equal(family, &s->address, &addr)) {
60 found = true;
61 break;
62 }
63
64 if (found)
65 continue;
66
67 r = dns_server_new(m, NULL, type, NULL, family, &addr);
68 if (r < 0)
69 return r;
70 }
71
72 return 0;
73 }
74
75 int config_parse_dnsv(
76 const char *unit,
77 const char *filename,
78 unsigned line,
79 const char *section,
80 unsigned section_line,
81 const char *lvalue,
82 int ltype,
83 const char *rvalue,
84 void *data,
85 void *userdata) {
86
87 Manager *m = userdata;
88 int r;
89
90 assert(filename);
91 assert(lvalue);
92 assert(rvalue);
93 assert(m);
94
95 if (isempty(rvalue))
96 /* Empty assignment means clear the list */
97 manager_flush_dns_servers(m, ltype);
98 else {
99 /* Otherwise add to the list */
100 r = manager_parse_dns_server(m, ltype, rvalue);
101 if (r < 0) {
102 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
103 return 0;
104 }
105 }
106
107 /* If we have a manual setting, then we stop reading
108 * /etc/resolv.conf */
109 if (ltype == DNS_SERVER_SYSTEM)
110 m->read_resolv_conf = false;
111
112 return 0;
113 }
114
115 int config_parse_support(
116 const char *unit,
117 const char *filename,
118 unsigned line,
119 const char *section,
120 unsigned section_line,
121 const char *lvalue,
122 int ltype,
123 const char *rvalue,
124 void *data,
125 void *userdata) {
126
127 Support support, *v = data;
128 int r;
129
130 assert(filename);
131 assert(lvalue);
132 assert(rvalue);
133
134 support = support_from_string(rvalue);
135 if (support < 0) {
136 r = parse_boolean(rvalue);
137 if (r < 0) {
138 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse support level '%s'. Ignoring.", rvalue);
139 return 0;
140 }
141
142 support = r ? SUPPORT_YES : SUPPORT_NO;
143 }
144
145 *v = support;
146 return 0;
147 }
148
149 int manager_parse_config_file(Manager *m) {
150 assert(m);
151
152 return config_parse_many("/etc/systemd/resolved.conf",
153 CONF_DIRS_NULSTR("systemd/resolved.conf"),
154 "Resolve\0",
155 config_item_perf_lookup, resolved_gperf_lookup,
156 false, m);
157 }