1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "dns-domain.h"
9 #include "extract-word.h"
11 #include "parse-util.h"
12 #include "resolvconf-compat.h"
13 #include "resolvectl.h"
14 #include "resolved-def.h"
15 #include "string-util.h"
18 static void resolvconf_help(void) {
19 printf("%1$s -a INTERFACE < FILE\n"
22 "Register DNS server and domain configuration with systemd-resolved.\n\n"
23 " -h --help Show this help\n"
24 " --version Show package version\n"
25 " -a Register per-interface DNS server and domain data\n"
26 " -d Unregister per-interface DNS server and domain data\n"
27 " -f Ignore if specified interface does not exist\n"
28 " -x Send DNS traffic preferably over this interface\n"
30 "This is a compatibility alias for the resolvectl(1) tool, providing native\n"
31 "command line compatibility with the resolvconf(8) tool of various Linux\n"
32 "distributions and BSD systems. Some options supported by other implementations\n"
33 "are not supported and are ignored: -m, -p. Various options supported by other\n"
34 "implementations are not supported and will cause the invocation to fail: -u,\n"
35 "-I, -i, -l, -R, -r, -v, -V, --enable-updates, --disable-updates,\n"
36 "--updates-are-enabled.\n"
37 , program_invocation_short_name
);
40 static int parse_nameserver(const char *string
) {
46 _cleanup_free_
char *word
= NULL
;
48 r
= extract_first_word(&string
, &word
, NULL
, 0);
54 if (strv_push(&arg_set_dns
, word
) < 0)
63 static int parse_search_domain(const char *string
) {
69 _cleanup_free_
char *word
= NULL
;
71 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_QUOTES
);
77 if (strv_push(&arg_set_domain
, word
) < 0)
86 int resolvconf_parse_argv(int argc
, char *argv
[]) {
92 ARG_UPDATES_ARE_ENABLED
,
95 static const struct option options
[] = {
96 { "help", no_argument
, NULL
, 'h' },
97 { "version", no_argument
, NULL
, ARG_VERSION
},
99 /* The following are specific to Debian's original resolvconf */
100 { "enable-updates", no_argument
, NULL
, ARG_ENABLE_UPDATES
},
101 { "disable-updates", no_argument
, NULL
, ARG_DISABLE_UPDATES
},
102 { "updates-are-enabled", no_argument
, NULL
, ARG_UPDATES_ARE_ENABLED
},
108 TYPE_PRIVATE
, /* -p: Not supported, treated identically to TYPE_REGULAR */
109 TYPE_EXCLUSIVE
, /* -x */
110 } type
= TYPE_REGULAR
;
117 /* openresolv checks these environment variables */
118 if (getenv("IF_EXCLUSIVE"))
119 type
= TYPE_EXCLUSIVE
;
120 if (getenv("IF_PRIVATE"))
121 type
= TYPE_PRIVATE
; /* not actually supported */
123 arg_mode
= _MODE_INVALID
;
125 while ((c
= getopt_long(argc
, argv
, "hadxpfm:uIi:l:Rr:vV", options
, NULL
)) >= 0)
130 return 0; /* done */;
135 /* -a and -d is what everybody can agree on */
137 arg_mode
= MODE_SET_LINK
;
141 arg_mode
= MODE_REVERT_LINK
;
144 /* The exclusive/private/force stuff is an openresolv invention, we support in some skewed way */
146 type
= TYPE_EXCLUSIVE
;
150 type
= TYPE_PRIVATE
; /* not actually supported */
154 arg_ifindex_permissive
= true;
157 /* The metrics stuff is an openresolv invention we ignore (and don't really need) */
159 log_debug("Switch -%c ignored.", c
);
162 /* Everybody else can agree on the existance of -u but we don't support it. */
165 /* The following options are openresolv inventions we don't support. */
173 log_error("Switch -%c not supported.", c
);
176 /* The Debian resolvconf commands we don't support. */
177 case ARG_ENABLE_UPDATES
:
178 log_error("Switch --enable-updates not supported.");
180 case ARG_DISABLE_UPDATES
:
181 log_error("Switch --disable-updates not supported.");
183 case ARG_UPDATES_ARE_ENABLED
:
184 log_error("Switch --updates-are-enabled not supported.");
191 assert_not_reached("Unhandled option");
194 if (arg_mode
== _MODE_INVALID
) {
195 log_error("Expected either -a or -d on the command line.");
199 if (optind
+1 != argc
) {
200 log_error("Expected interface name as argument.");
204 r
= ifname_mangle(argv
[optind
], false);
210 if (arg_mode
== MODE_SET_LINK
) {
214 _cleanup_free_
char *line
= NULL
;
217 r
= read_line(stdin
, LONG_LINE_MAX
, &line
);
219 return log_error_errno(r
, "Failed to read from stdin: %m");
226 if (IN_SET(*l
, '#', ';', 0))
229 a
= first_word(l
, "nameserver");
231 (void) parse_nameserver(a
);
235 a
= first_word(l
, "domain");
237 a
= first_word(l
, "search");
239 (void) parse_search_domain(a
);
243 log_syntax(NULL
, LOG_DEBUG
, "stdin", n
, 0, "Ignoring resolv.conf line: %s", l
);
246 if (type
== TYPE_EXCLUSIVE
) {
248 /* If -x mode is selected, let's preferably route non-suffixed lookups to this interface. This
249 * somewhat matches the original -x behaviour */
251 r
= strv_extend(&arg_set_domain
, "~.");
255 } else if (type
== TYPE_PRIVATE
)
256 log_debug("Private DNS server data not supported, ignoring.");
259 log_error("No DNS servers specified, refusing operation.");
264 return 1; /* work to do */