ifupdown-ng/cmd/ifquery.c

257 lines
6 KiB
C
Raw Normal View History

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
#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"
void
print_interface(struct lif_interface *iface)
{
if (iface->is_auto)
printf("auto %s\n", iface->ifname);
printf("iface %s", iface->ifname);
if (iface->is_loopback)
printf(" inet loopback\n");
else if (iface->is_dhcp)
printf(" inet dhcp\n");
else
printf("\n");
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-18 23:44:58 +00:00
void
usage()
{
2020-07-19 06:05:12 +00:00
fprintf(stderr, "usage: ifquery [options] <interfaces>\n");
fprintf(stderr, " ifquery [options] --list\n");
2020-07-18 23:44:58 +00:00
2020-07-19 06:05:12 +00:00
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " -h, --help this help\n");
fprintf(stderr, " -V, --version show this program's version\n");
fprintf(stderr, " -i, --interfaces FILE use FILE for interface definitions\n");
fprintf(stderr, " -L, --list list matching interfaces\n");
fprintf(stderr, " -a, --auto only match against interfaces hinted as 'auto'\n");
fprintf(stderr, " -I, --include PATTERN only match against interfaces matching PATTERN\n");
fprintf(stderr, " -X, --exclude PATTERN never match against interfaces matching PATTERN\n");
fprintf(stderr, " -P, --pretty-print pretty print the interfaces instead of just listing\n");
2020-07-20 14:19:23 +00:00
fprintf(stderr, " -S, --state-file FILE use FILE for state\n");
fprintf(stderr, " -s, --state show configured state\n");
2020-07-18 23:44:58 +00:00
exit(1);
}
2020-07-18 10:49:22 +00:00
2020-07-19 01:26:20 +00:00
struct match_options {
bool is_auto;
char *exclude_pattern;
char *include_pattern;
2020-07-19 01:34:38 +00:00
bool pretty_print;
2020-07-19 01:26:20 +00:00
};
void
list_interfaces(struct lif_dict *collection, struct match_options *opts)
{
struct lif_node *iter;
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;
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);
else
printf("%s\n", iface->ifname);
2020-07-19 01:26:20 +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;
printf("%s=%s\n", entry->key, (const char *) entry->data);
}
}
2020-07-18 10:49:22 +00:00
int
main(int argc, char *argv[])
{
2020-07-20 14:19:23 +00:00
struct lif_dict state = {};
struct lif_dict collection = {};
2020-07-18 23:44:58 +00:00
struct option long_options[] = {
2020-07-19 01:09:08 +00:00
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
2020-07-18 23:44:58 +00:00
{"interfaces", required_argument, 0, 'i'},
2020-07-19 01:26:20 +00:00
{"list", no_argument, 0, 'L'},
{"auto", no_argument, 0, 'a'},
{"include", required_argument, 0, 'I'},
{"exclude", required_argument, 0, 'X'},
2020-07-19 01:34:38 +00:00
{"pretty-print", no_argument, 0, 'P'},
2020-07-20 14:19:23 +00:00
{"state-file", required_argument, 0, 'S'},
{"state", no_argument, 0, 's'},
2020-07-18 23:44:58 +00:00
{NULL, 0, 0, 0}
};
2020-07-19 01:26:20 +00:00
struct match_options match_opts = {};
2020-07-20 14:19:23 +00:00
bool listing = false, listing_stat = false;
2020-07-18 23:44:58 +00:00
char *interfaces_file = INTERFACES_FILE;
2020-07-20 14:19:23 +00:00
char *state_file = STATE_FILE;
2020-07-18 23:44:58 +00:00
for (;;)
{
2020-07-20 14:19:23 +00:00
int c = getopt_long(argc, argv, "hVi:LaI:X:PS:s", long_options, NULL);
2020-07-18 23:44:58 +00:00
if (c == -1)
break;
switch (c) {
2020-07-19 01:09:08 +00:00
case 'h':
usage();
break;
case 'V':
lif_common_version();
break;
2020-07-18 23:44:58 +00:00
case 'i':
interfaces_file = optarg;
break;
2020-07-19 01:26:20 +00:00
case 'L':
listing = true;
break;
case 'a':
match_opts.is_auto = true;
break;
case 'I':
match_opts.include_pattern = optarg;
break;
case 'X':
match_opts.exclude_pattern = optarg;
break;
2020-07-19 01:34:38 +00:00
case 'P':
match_opts.pretty_print = true;
break;
2020-07-20 14:19:23 +00:00
case 'S':
state_file = optarg;
break;
case 's':
listing_stat = true;
break;
2020-07-18 23:44:58 +00:00
}
}
2020-07-18 10:49:22 +00:00
2020-07-20 14:19:23 +00:00
if (!lif_state_read_path(&state, state_file))
{
fprintf(stderr, "ifquery: could not parse %s\n", state_file);
return EXIT_FAILURE;
}
2020-07-19 00:02:25 +00:00
if (!lif_interface_file_parse(&collection, interfaces_file))
{
fprintf(stderr, "ifquery: could not parse %s\n", interfaces_file);
return EXIT_FAILURE;
}
2020-07-18 10:49:22 +00:00
2020-07-20 14:19:23 +00:00
/* --list --state is not allowed */
if (listing && listing_stat)
usage();
2020-07-19 01:26:20 +00:00
if (listing)
{
list_interfaces(&collection, &match_opts);
return EXIT_SUCCESS;
}
2020-07-20 14:19:23 +00:00
else if (listing_stat)
{
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)
usage();
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-07-20 14:19:23 +00:00
if (iface == NULL)
2020-07-18 23:44:58 +00:00
{
2020-07-19 00:02:25 +00:00
fprintf(stderr, "ifquery: unknown interface %s\n", argv[idx]);
2020-07-18 23:44:58 +00:00
return EXIT_FAILURE;
}
2020-07-20 14:19:23 +00:00
print_interface(iface);
2020-07-18 23:44:58 +00:00
}
2020-07-18 10:49:22 +00:00
return EXIT_SUCCESS;
}