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"
17 #include "terminal-util.h"
19 static int resolvconf_help(void) {
20 _cleanup_free_
char *link
= NULL
;
23 r
= terminal_urlify_man("resolvectl", "1", &link
);
27 printf("%1$s -a INTERFACE < FILE\n"
30 "Register DNS server and domain configuration with systemd-resolved.\n\n"
31 " -h --help Show this help\n"
32 " --version Show package version\n"
33 " -a Register per-interface DNS server and domain data\n"
34 " -d Unregister per-interface DNS server and domain data\n"
35 " -f Ignore if specified interface does not exist\n"
36 " -x Send DNS traffic preferably over this interface\n"
38 "This is a compatibility alias for the resolvectl(1) tool, providing native\n"
39 "command line compatibility with the resolvconf(8) tool of various Linux\n"
40 "distributions and BSD systems. Some options supported by other implementations\n"
41 "are not supported and are ignored: -m, -p. Various options supported by other\n"
42 "implementations are not supported and will cause the invocation to fail: -u,\n"
43 "-I, -i, -l, -R, -r, -v, -V, --enable-updates, --disable-updates,\n"
44 "--updates-are-enabled.\n"
45 "\nSee the %2$s for details.\n"
46 , program_invocation_short_name
53 static int parse_nameserver(const char *string
) {
59 _cleanup_free_
char *word
= NULL
;
61 r
= extract_first_word(&string
, &word
, NULL
, 0);
67 if (strv_push(&arg_set_dns
, word
) < 0)
76 static int parse_search_domain(const char *string
) {
82 _cleanup_free_
char *word
= NULL
;
84 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_QUOTES
);
90 if (strv_push(&arg_set_domain
, word
) < 0)
99 int resolvconf_parse_argv(int argc
, char *argv
[]) {
105 ARG_UPDATES_ARE_ENABLED
,
108 static const struct option options
[] = {
109 { "help", no_argument
, NULL
, 'h' },
110 { "version", no_argument
, NULL
, ARG_VERSION
},
112 /* The following are specific to Debian's original resolvconf */
113 { "enable-updates", no_argument
, NULL
, ARG_ENABLE_UPDATES
},
114 { "disable-updates", no_argument
, NULL
, ARG_DISABLE_UPDATES
},
115 { "updates-are-enabled", no_argument
, NULL
, ARG_UPDATES_ARE_ENABLED
},
121 TYPE_PRIVATE
, /* -p: Not supported, treated identically to TYPE_REGULAR */
122 TYPE_EXCLUSIVE
, /* -x */
123 } type
= TYPE_REGULAR
;
130 /* openresolv checks these environment variables */
131 if (getenv("IF_EXCLUSIVE"))
132 type
= TYPE_EXCLUSIVE
;
133 if (getenv("IF_PRIVATE"))
134 type
= TYPE_PRIVATE
; /* not actually supported */
136 arg_mode
= _MODE_INVALID
;
138 while ((c
= getopt_long(argc
, argv
, "hadxpfm:uIi:l:Rr:vV", options
, NULL
)) >= 0)
142 return resolvconf_help();
147 /* -a and -d is what everybody can agree on */
149 arg_mode
= MODE_SET_LINK
;
153 arg_mode
= MODE_REVERT_LINK
;
156 /* The exclusive/private/force stuff is an openresolv invention, we support in some skewed way */
158 type
= TYPE_EXCLUSIVE
;
162 type
= TYPE_PRIVATE
; /* not actually supported */
166 arg_ifindex_permissive
= true;
169 /* The metrics stuff is an openresolv invention we ignore (and don't really need) */
171 log_debug("Switch -%c ignored.", c
);
174 /* Everybody else can agree on the existance of -u but we don't support it. */
177 /* The following options are openresolv inventions we don't support. */
185 log_error("Switch -%c not supported.", c
);
188 /* The Debian resolvconf commands we don't support. */
189 case ARG_ENABLE_UPDATES
:
190 log_error("Switch --enable-updates not supported.");
192 case ARG_DISABLE_UPDATES
:
193 log_error("Switch --disable-updates not supported.");
195 case ARG_UPDATES_ARE_ENABLED
:
196 log_error("Switch --updates-are-enabled not supported.");
203 assert_not_reached("Unhandled option");
206 if (arg_mode
== _MODE_INVALID
) {
207 log_error("Expected either -a or -d on the command line.");
211 if (optind
+1 != argc
) {
212 log_error("Expected interface name as argument.");
216 r
= ifname_mangle(argv
[optind
], false);
222 if (arg_mode
== MODE_SET_LINK
) {
226 _cleanup_free_
char *line
= NULL
;
229 r
= read_line(stdin
, LONG_LINE_MAX
, &line
);
231 return log_error_errno(r
, "Failed to read from stdin: %m");
238 if (IN_SET(*l
, '#', ';', 0))
241 a
= first_word(l
, "nameserver");
243 (void) parse_nameserver(a
);
247 a
= first_word(l
, "domain");
249 a
= first_word(l
, "search");
251 (void) parse_search_domain(a
);
255 log_syntax(NULL
, LOG_DEBUG
, "stdin", n
, 0, "Ignoring resolv.conf line: %s", l
);
258 if (type
== TYPE_EXCLUSIVE
) {
260 /* If -x mode is selected, let's preferably route non-suffixed lookups to this interface. This
261 * somewhat matches the original -x behaviour */
263 r
= strv_extend(&arg_set_domain
, "~.");
267 } else if (type
== TYPE_PRIVATE
)
268 log_debug("Private DNS server data not supported, ignoring.");
271 log_error("No DNS servers specified, refusing operation.");
276 return 1; /* work to do */