Debian Codegraph @

src/resolve/resolvconf-compat.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "extract-word.h"
8 #include "fileio.h"
9 #include "format-table.h"
10 #include "help-util.h"
11 #include "log.h"
12 #include "options.h"
13 #include "resolvconf-compat.h"
14 #include "resolvectl.h"
15 #include "string-util.h"
16 #include "strv.h"
17
18 typedef enum LookupType {
19 LOOKUP_TYPE_REGULAR,
20 LOOKUP_TYPE_PRIVATE,
21 LOOKUP_TYPE_EXCLUSIVE, /* -x */
22 } LookupType;
23
24 static int resolvconf_help(void) {
25 _cleanup_(table_unrefp) Table *options = NULL;
26 int r;
27
28 r = option_parser_get_help_table_ns("resolvconf", &options);
29 if (r < 0)
30 return r;
31
32 help_cmdline("-a INTERFACE <FILE");
33 help_cmdline("-d INTERFACE");
34 help_abstract("Register DNS server and domain configuration with systemd-resolved.");
35
36 help_section("Options");
37 r = table_print_or_warn(options);
38 if (r < 0)
39 return r;
40
41 printf("\n"
42 "This is a compatibility alias for the resolvectl(1) tool, providing native\n"
43 "command line compatibility with the resolvconf(8) tool of various Linux\n"
44 "distributions and BSD systems. Some options supported by other implementations\n"
45 "are not supported and are ignored: -m, -u. Various options supported by other\n"
46 "implementations are not supported and will cause the invocation to fail:\n"
47 "-I, -i, -l, -R, -r, -v, -V, --enable-updates, --disable-updates,\n"
48 "--updates-are-enabled.\n");
49
50 help_man_page_reference("resolvectl", "1");
51 return 0;
52 }
53
54 static int parse_nameserver(const char *string) {
55 int r;
56
57 assert(string);
58
59 for (;;) {
60 _cleanup_free_ char *word = NULL;
61
62 r = extract_first_word(&string, &word, NULL, 0);
63 if (r < 0)
64 return r;
65 if (r == 0)
66 break;
67
68 if (strv_push(&arg_set_dns, word) < 0)
69 return log_oom();
70
71 word = NULL;
72 }
73
74 return 0;
75 }
76
77 static int parse_search_domain(const char *string) {
78 int r;
79
80 assert(string);
81
82 for (;;) {
83 _cleanup_free_ char *word = NULL;
84
85 r = extract_first_word(&string, &word, NULL, EXTRACT_UNQUOTE);
86 if (r < 0)
87 return r;
88 if (r == 0)
89 break;
90
91 if (strv_push(&arg_set_domain, word) < 0)
92 return log_oom();
93
94 word = NULL;
95 }
96
97 return 0;
98 }
99
100 static int parse_stdin(LookupType lookup_type) {
101 int r;
102
103 for (unsigned n = 0;;) {
104 _cleanup_free_ char *line = NULL;
105 const char *a;
106
107 r = read_stripped_line(stdin, LONG_LINE_MAX, &line);
108 if (r < 0)
109 return log_error_errno(r, "Failed to read from stdin: %m");
110 if (r == 0)
111 break;
112 n++;
113
114 if (IN_SET(*line, '#', ';', 0))
115 continue;
116
117 a = first_word(line, "nameserver");
118 if (a) {
119 (void) parse_nameserver(a);
120 continue;
121 }
122
123 a = first_word(line, "domain");
124 if (!a)
125 a = first_word(line, "search");
126 if (a) {
127 (void) parse_search_domain(a);
128 continue;
129 }
130
131 log_syntax(NULL, LOG_DEBUG, "stdin", n, 0, "Ignoring resolv.conf line: %s", line);
132 }
133
134 switch (lookup_type) {
135 case LOOKUP_TYPE_REGULAR:
136 break;
137
138 case LOOKUP_TYPE_PRIVATE:
139 arg_disable_default_route = true;
140 break;
141
142 case LOOKUP_TYPE_EXCLUSIVE:
143 /* If -x mode is selected, let's preferably route non-suffixed lookups to this interface.
144 * This somewhat matches the original -x behaviour */
145
146 r = strv_extend(&arg_set_domain, "~.");
147 if (r < 0)
148 return log_oom();
149 break;
150
151 default:
152 assert_not_reached();
153 }
154
155 if (strv_isempty(arg_set_dns))
156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
157 "No DNS servers specified, refusing operation.");
158
159 if (strv_isempty(arg_set_domain)) {
160 /* When no domain/search is set, clear the current domains. */
161 r = strv_extend(&arg_set_domain, "");
162 if (r < 0)
163 return log_oom();
164 }
165
166 return 0;
167 }
168
169 int resolvconf_parse_argv(int argc, char *argv[]) {
170 int r;
171
172 assert(argc >= 0);
173 assert(argv);
174
175 /* openresolv checks these environment variables */
176 LookupType lookup_type = LOOKUP_TYPE_REGULAR;
177
178 if (getenv("IF_EXCLUSIVE"))
179 lookup_type = LOOKUP_TYPE_EXCLUSIVE;
180 if (getenv("IF_PRIVATE"))
181 lookup_type = LOOKUP_TYPE_PRIVATE;
182
183 arg_mode = _MODE_INVALID;
184
185 OptionParser opts = { argc, argv, .namespace = "resolvconf" };
186
187 FOREACH_OPTION_OR_RETURN(c, &opts)
188 switch (c) {
189
190 OPTION_NAMESPACE("resolvconf"): {}
191
192 OPTION_COMMON_HELP:
193 return resolvconf_help();
194
195 OPTION_COMMON_VERSION:
196 return version();
197
198 /* -a and -d is what everybody can agree on */
199 OPTION_SHORT('a', NULL, "Register per-interface DNS server and domain data"):
200 arg_mode = MODE_SET_LINK;
201 break;
202
203 OPTION_SHORT('d', NULL, "Unregister per-interface DNS server and domain data"):
204 arg_mode = MODE_REVERT_LINK;
205 break;
206
207 OPTION_SHORT('p', NULL, "Do not use this interface as default route"):
208 lookup_type = LOOKUP_TYPE_PRIVATE;
209 break;
210
211 OPTION_SHORT('f', NULL, "Ignore if specified interface does not exist"):
212 arg_ifindex_permissive = true;
213 break;
214
215 /* The exclusive/private/force stuff is an openresolv invention, we support in some skewed way */
216 OPTION_SHORT('x', NULL, "Send DNS traffic preferably over this interface"):
217 lookup_type = LOOKUP_TYPE_EXCLUSIVE;
218 break;
219
220 /* The metrics stuff is an openresolv invention we ignore (and don't really need) */
221 OPTION_SHORT('m', "ARG", /* help= */ NULL):
222 log_debug("Switch -%c ignored.", opts.opt->short_code);
223 break;
224
225 /* -u supposedly should "update all subscribers". We have no subscribers, hence let's make
226 this a NOP, and exit immediately, cleanly. */
227 OPTION_SHORT('u', NULL, /* help= */ NULL):
228 log_info("Switch -%c ignored.", opts.opt->short_code);
229 return 0;
230
231 /* The following options are openresolv inventions we don't support. */
232 OPTION_SHORT('I', NULL, /* help= */ NULL): {}
233 OPTION_SHORT('i', "ARG", /* help= */ NULL): {}
234 OPTION_SHORT('l', "ARG", /* help= */ NULL): {}
235 OPTION_SHORT('R', NULL, /* help= */ NULL): {}
236 OPTION_SHORT('r', "ARG", /* help= */ NULL): {}
237 OPTION_SHORT('v', NULL, /* help= */ NULL): {}
238 OPTION_SHORT('V', NULL, /* help= */ NULL):
239 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
240 "Switch -%c not supported.", opts.opt->short_code);
241
242 /* The Debian resolvconf commands we don't support. */
243 OPTION_LONG("enable-updates", NULL, /* help= */ NULL):
244 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
245 "Switch --enable-updates not supported.");
246 OPTION_LONG("disable-updates", NULL, /* help= */ NULL):
247 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
248 "Switch --disable-updates not supported.");
249 OPTION_LONG("updates-are-enabled", NULL, /* help= */ NULL):
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
251 "Switch --updates-are-enabled not supported.");
252 }
253
254 if (arg_mode == _MODE_INVALID)
255 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
256 "Expected either -a or -d on the command line.");
257
258 if (option_parser_get_n_args(&opts) != 1)
259 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
260 "Expected interface name as argument.");
261
262 r = ifname_resolvconf_mangle(option_parser_get_arg(&opts, 0));
263 if (r <= 0)
264 return r;
265
266 if (arg_mode == MODE_SET_LINK) {
267 r = parse_stdin(lookup_type);
268 if (r < 0)
269 return r;
270 }
271
272 return 1; /* work to do */
273 }