2020-07-18 10:49:22 +00:00
|
|
|
/*
|
|
|
|
* cmd/ifquery.c
|
|
|
|
* Purpose: look up information in /etc/network/interfaces
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2020-07-18 23:44:58 +00:00
|
|
|
#define _GNU_SOURCE
|
2020-07-19 01:31:44 +00:00
|
|
|
#include <fnmatch.h>
|
2020-07-18 10:49:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2020-07-18 23:44:58 +00:00
|
|
|
#include <getopt.h>
|
2020-07-18 10:49:22 +00:00
|
|
|
#include "libifupdown/libifupdown.h"
|
2020-07-24 09:07:19 +00:00
|
|
|
#include "cmd/multicall.h"
|
2020-07-18 10:49:22 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
print_interface(struct lif_interface *iface)
|
|
|
|
{
|
2020-07-28 19:46:25 +00:00
|
|
|
if (!lif_lifecycle_query_dependents(&exec_opts, iface, iface->ifname))
|
|
|
|
return;
|
|
|
|
|
2020-07-18 10:49:22 +00:00
|
|
|
if (iface->is_auto)
|
|
|
|
printf("auto %s\n", iface->ifname);
|
|
|
|
|
2020-09-23 17:35:27 +00:00
|
|
|
printf("%s %s\n", iface->is_template ? "template" : "iface", iface->ifname);
|
2020-07-18 10:49:22 +00:00
|
|
|
|
|
|
|
struct lif_node *iter;
|
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
|
|
|
|
if (!strcmp(entry->key, "address"))
|
|
|
|
{
|
|
|
|
struct lif_address *addr = entry->data;
|
|
|
|
char addr_buf[512];
|
|
|
|
|
|
|
|
if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true))
|
|
|
|
{
|
|
|
|
printf(" # warning: failed to unparse address\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" %s %s\n", entry->key, addr_buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf(" %s %s\n", entry->key, (const char *) entry->data);
|
|
|
|
}
|
2020-07-19 01:34:38 +00:00
|
|
|
|
|
|
|
printf("\n");
|
2020-07-18 10:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 10:25:06 +00:00
|
|
|
void
|
|
|
|
print_interface_dot(struct lif_dict *collection, struct lif_interface *iface, struct lif_interface *parent)
|
|
|
|
{
|
2020-08-04 18:36:38 +00:00
|
|
|
if (!lif_lifecycle_query_dependents(&exec_opts, iface, iface->ifname))
|
|
|
|
return;
|
|
|
|
|
2020-07-24 10:25:06 +00:00
|
|
|
if (parent != NULL)
|
2020-09-09 23:14:58 +00:00
|
|
|
printf("\"%s (%zu)\" -> ", parent->ifname, parent->rdepends_count);
|
2020-07-24 10:25:06 +00:00
|
|
|
|
2020-09-09 23:14:58 +00:00
|
|
|
printf("\"%s (%zu)\"", iface->ifname, iface->rdepends_count);
|
2020-07-24 10:25:06 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "requires");
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char require_ifs[4096] = {};
|
|
|
|
strlcpy(require_ifs, entry->data, sizeof require_ifs);
|
|
|
|
char *reqp = require_ifs;
|
|
|
|
|
|
|
|
for (char *tokenp = lif_next_token(&reqp); *tokenp; tokenp = lif_next_token(&reqp))
|
|
|
|
{
|
|
|
|
struct lif_interface *child_if = lif_interface_collection_find(collection, tokenp);
|
|
|
|
|
2020-10-08 08:17:13 +00:00
|
|
|
if (child_if->is_pending)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
child_if->is_pending = true;
|
2020-07-24 10:25:06 +00:00
|
|
|
print_interface_dot(collection, child_if, iface);
|
2020-10-08 08:17:13 +00:00
|
|
|
child_if->is_pending = false;
|
2020-07-24 10:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 08:28:29 +00:00
|
|
|
void
|
|
|
|
print_interface_property(struct lif_interface *iface, const char *property)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
bool printing_address = !strcmp(property, "address");
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, &iface->vars)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
|
|
|
|
if (strcmp(entry->key, property))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (printing_address)
|
|
|
|
{
|
|
|
|
char addr_buf[512];
|
2020-10-13 23:23:23 +00:00
|
|
|
if (!lif_address_format_cidr(iface, entry, addr_buf, sizeof(addr_buf)))
|
2020-07-26 08:28:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
printf("%s\n", addr_buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("%s\n", (const char *) entry->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 01:26:20 +00:00
|
|
|
void
|
|
|
|
list_interfaces(struct lif_dict *collection, struct match_options *opts)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
2020-07-24 10:25:06 +00:00
|
|
|
if (opts->dot)
|
|
|
|
{
|
|
|
|
printf("digraph interfaces {\n");
|
|
|
|
printf("edge [color=blue fontname=Sans fontsize=10]\n");
|
|
|
|
printf("node [fontname=Sans fontsize=10]\n");
|
|
|
|
}
|
|
|
|
|
2020-07-19 01:26:20 +00:00
|
|
|
LIF_DICT_FOREACH(iter, collection)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
struct lif_interface *iface = entry->data;
|
|
|
|
|
|
|
|
if (opts->is_auto && !iface->is_auto)
|
|
|
|
continue;
|
|
|
|
|
2020-07-19 01:31:44 +00:00
|
|
|
if (opts->exclude_pattern != NULL &&
|
|
|
|
!fnmatch(opts->exclude_pattern, iface->ifname, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts->include_pattern != NULL &&
|
|
|
|
fnmatch(opts->include_pattern, iface->ifname, 0))
|
|
|
|
continue;
|
|
|
|
|
2020-07-19 01:34:38 +00:00
|
|
|
if (opts->pretty_print)
|
|
|
|
print_interface(iface);
|
2020-07-24 10:25:06 +00:00
|
|
|
else if (opts->dot)
|
|
|
|
print_interface_dot(collection, iface, NULL);
|
2020-07-19 01:34:38 +00:00
|
|
|
else
|
|
|
|
printf("%s\n", iface->ifname);
|
2020-07-19 01:26:20 +00:00
|
|
|
}
|
2020-07-24 10:25:06 +00:00
|
|
|
|
|
|
|
if (opts->dot)
|
|
|
|
printf("}\n");
|
2020-07-19 01:26:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 08:44:00 +00:00
|
|
|
static bool listing = false, listing_stat = false, listing_running = false;
|
2020-11-04 18:55:36 +00:00
|
|
|
static bool allow_undefined = false;
|
2020-10-14 08:44:00 +00:00
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
void
|
|
|
|
list_state(struct lif_dict *state, struct match_options *opts)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, state)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
|
|
|
|
if (opts->exclude_pattern != NULL &&
|
|
|
|
!fnmatch(opts->exclude_pattern, entry->key, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts->include_pattern != NULL &&
|
|
|
|
fnmatch(opts->include_pattern, entry->key, 0))
|
|
|
|
continue;
|
|
|
|
|
2020-09-08 20:59:58 +00:00
|
|
|
struct lif_state_record *rec = entry->data;
|
2020-10-14 08:44:00 +00:00
|
|
|
|
|
|
|
if (listing_running)
|
|
|
|
printf("%s\n", entry->key);
|
|
|
|
else
|
2020-10-21 14:32:29 +00:00
|
|
|
printf("%s=%s %zu%s\n", entry->key, rec->mapped_if, rec->refcount,
|
|
|
|
rec->is_explicit ? " explicit" : "");
|
2020-07-20 14:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 05:03:55 +00:00
|
|
|
static void
|
2020-08-17 19:59:19 +00:00
|
|
|
set_listing(const char *opt_arg)
|
2020-08-12 05:03:55 +00:00
|
|
|
{
|
2020-08-17 19:59:19 +00:00
|
|
|
(void) opt_arg;
|
|
|
|
listing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_show_state(const char *opt_arg)
|
|
|
|
{
|
|
|
|
(void) opt_arg;
|
|
|
|
listing_stat = true;
|
|
|
|
}
|
|
|
|
|
2020-10-14 08:44:00 +00:00
|
|
|
static void
|
|
|
|
set_show_running(const char *opt_arg)
|
|
|
|
{
|
|
|
|
(void) opt_arg;
|
|
|
|
listing_running = true;
|
|
|
|
}
|
|
|
|
|
2020-08-17 19:59:19 +00:00
|
|
|
static void
|
|
|
|
set_pretty_print(const char *opt_arg)
|
|
|
|
{
|
|
|
|
(void) opt_arg;
|
|
|
|
match_opts.pretty_print = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_output_dot(const char *opt_arg)
|
|
|
|
{
|
|
|
|
(void) opt_arg;
|
|
|
|
match_opts.dot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_property(const char *opt_arg)
|
|
|
|
{
|
|
|
|
match_opts.property = opt_arg;
|
2020-08-12 05:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:55:36 +00:00
|
|
|
static void
|
|
|
|
set_allow_undefined(const char *opt_arg)
|
|
|
|
{
|
|
|
|
(void) opt_arg;
|
|
|
|
allow_undefined = true;
|
|
|
|
}
|
|
|
|
|
2020-08-12 05:03:55 +00:00
|
|
|
static struct if_option local_options[] = {
|
2020-10-14 08:44:00 +00:00
|
|
|
{'r', "running", NULL, "show configured (running) interfaces", false, set_show_running},
|
2020-08-17 19:59:19 +00:00
|
|
|
{'s', "state", NULL, "show configured state", false, set_show_state},
|
|
|
|
{'p', "property", "property PROPERTY", "print values of properties matching PROPERTY", true, set_property},
|
|
|
|
{'D', "dot", NULL, "generate a dependency graph", false, set_output_dot},
|
|
|
|
{'L', "list", NULL, "list matching interfaces", false, set_listing},
|
|
|
|
{'P', "pretty-print", NULL, "pretty print the interfaces instead of just listing", false, set_pretty_print},
|
2020-11-04 18:55:36 +00:00
|
|
|
{'U', "allow-undefined", NULL, "allow querying undefined (virtual) interfaces", false, set_allow_undefined},
|
2020-08-12 05:03:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct if_option_group local_option_group = {
|
|
|
|
.desc = "Program-specific options",
|
|
|
|
.group_size = ARRAY_SIZE(local_options),
|
|
|
|
.group = local_options
|
|
|
|
};
|
|
|
|
|
2020-07-18 10:49:22 +00:00
|
|
|
int
|
2020-07-24 09:07:19 +00:00
|
|
|
ifquery_main(int argc, char *argv[])
|
2020-07-18 10:49:22 +00:00
|
|
|
{
|
2020-07-20 14:19:23 +00:00
|
|
|
struct lif_dict state = {};
|
|
|
|
struct lif_dict collection = {};
|
2020-10-14 09:50:58 +00:00
|
|
|
struct lif_interface_file_parse_state parse_state = {
|
|
|
|
.collection = &collection,
|
|
|
|
};
|
2020-07-18 10:49:22 +00:00
|
|
|
|
2020-08-20 09:45:56 +00:00
|
|
|
lif_interface_collection_init(&collection);
|
|
|
|
|
2020-08-12 04:51:53 +00:00
|
|
|
if (!lif_state_read_path(&state, exec_opts.state_file))
|
2020-07-20 14:19:23 +00:00
|
|
|
{
|
2020-08-12 04:51:53 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file);
|
2020-07-20 14:19:23 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:50:58 +00:00
|
|
|
if (!lif_interface_file_parse(&parse_state, exec_opts.interfaces_file))
|
2020-07-19 00:02:25 +00:00
|
|
|
{
|
2020-07-28 19:46:25 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.interfaces_file);
|
2020-07-19 00:02:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-07-18 10:49:22 +00:00
|
|
|
|
2020-09-25 14:43:26 +00:00
|
|
|
if (match_opts.property == NULL && lif_lifecycle_count_rdepends(&exec_opts, &collection) == -1)
|
2020-09-09 23:39:26 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not validate dependency tree\n", argv0);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-10-18 02:57:24 +00:00
|
|
|
if (!lif_compat_apply(&collection))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: failed to apply compatibility glue\n", argv0);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
/* --list --state is not allowed */
|
2020-10-14 08:44:00 +00:00
|
|
|
if (listing && (listing_stat || listing_running))
|
2020-08-12 05:10:35 +00:00
|
|
|
generic_usage(self_applet, EXIT_FAILURE);
|
2020-07-20 14:19:23 +00:00
|
|
|
|
2020-07-19 01:26:20 +00:00
|
|
|
if (listing)
|
|
|
|
{
|
|
|
|
list_interfaces(&collection, &match_opts);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-10-14 08:44:00 +00:00
|
|
|
else if (listing_stat || listing_running)
|
2020-07-20 14:19:23 +00:00
|
|
|
{
|
|
|
|
list_state(&state, &match_opts);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-07-19 01:26:20 +00:00
|
|
|
|
2020-07-18 23:44:58 +00:00
|
|
|
if (optind >= argc)
|
2020-08-12 05:10:35 +00:00
|
|
|
generic_usage(self_applet, EXIT_FAILURE);
|
2020-07-18 23:44:58 +00:00
|
|
|
|
|
|
|
int idx = optind;
|
|
|
|
for (; idx < argc; idx++)
|
|
|
|
{
|
2020-07-20 14:19:23 +00:00
|
|
|
struct lif_interface *iface = lif_state_lookup(&state, &collection, argv[idx]);
|
2020-07-20 14:24:31 +00:00
|
|
|
|
|
|
|
if (iface == NULL)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&collection, argv[idx]);
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
iface = entry->data;
|
2020-11-04 18:55:36 +00:00
|
|
|
|
|
|
|
if (entry == NULL && allow_undefined)
|
|
|
|
iface = lif_interface_collection_find(&collection, argv[idx]);
|
2020-07-20 14:24:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:19:23 +00:00
|
|
|
if (iface == NULL)
|
2020-07-18 23:44:58 +00:00
|
|
|
{
|
2020-07-24 10:52:15 +00:00
|
|
|
fprintf(stderr, "%s: unknown interface %s\n", argv0, argv[idx]);
|
2020-07-18 23:44:58 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-26 08:28:29 +00:00
|
|
|
if (match_opts.property != NULL)
|
|
|
|
print_interface_property(iface, match_opts.property);
|
|
|
|
else
|
|
|
|
print_interface(iface);
|
2020-07-18 23:44:58 +00:00
|
|
|
}
|
2020-07-18 10:49:22 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-07-24 09:07:19 +00:00
|
|
|
|
|
|
|
struct if_applet ifquery_applet = {
|
|
|
|
.name = "ifquery",
|
2020-08-12 04:37:36 +00:00
|
|
|
.desc = "query interface configuration",
|
2020-07-24 09:07:19 +00:00
|
|
|
.main = ifquery_main,
|
2020-08-12 05:10:35 +00:00
|
|
|
.usage = "ifquery [options] <interfaces>\n ifquery [options] --list",
|
2020-08-18 20:14:33 +00:00
|
|
|
.manpage = "8 ifquery",
|
2020-08-12 05:03:55 +00:00
|
|
|
.groups = { &global_option_group, &match_option_group, &exec_option_group, &local_option_group },
|
2020-07-24 09:07:19 +00:00
|
|
|
};
|