2020-07-23 16:44:45 +00:00
|
|
|
/*
|
|
|
|
* cmd/ifupdown.c
|
|
|
|
* Purpose: bring interfaces up or down
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
2020-07-29 09:01:49 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2020-07-23 16:44:45 +00:00
|
|
|
#include "libifupdown/libifupdown.h"
|
2020-07-24 09:07:19 +00:00
|
|
|
#include "cmd/multicall.h"
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
struct match_options {
|
|
|
|
bool is_auto;
|
|
|
|
char *exclude_pattern;
|
|
|
|
char *include_pattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool up;
|
2020-07-25 08:16:51 +00:00
|
|
|
static struct lif_execute_opts exec_opts = {
|
|
|
|
.executor_path = EXECUTOR_PATH,
|
2020-07-26 08:50:01 +00:00
|
|
|
.interfaces_file = INTERFACES_FILE,
|
2020-07-29 09:10:55 +00:00
|
|
|
.state_file = STATE_FILE,
|
2020-07-25 08:16:51 +00:00
|
|
|
};
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
void
|
2020-07-24 09:07:19 +00:00
|
|
|
ifupdown_usage(void)
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s [options] <interfaces>\n", argv0);
|
|
|
|
|
|
|
|
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, " -S, --state-file FILE use FILE for state\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, " -n, --no-act do not actually run any commands\n");
|
|
|
|
fprintf(stderr, " -v, --verbose show what commands are being run\n");
|
2020-07-25 08:22:27 +00:00
|
|
|
fprintf(stderr, " -E, --executor-path PATH use PATH for executor directory\n");
|
2020-07-28 14:23:58 +00:00
|
|
|
fprintf(stderr, " -f, --force force (de)configuration\n");
|
2020-07-29 09:01:49 +00:00
|
|
|
fprintf(stderr, " -L, --no-lock do not use a lockfile to serialize state changes\n");
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
is_ifdown()
|
|
|
|
{
|
|
|
|
if (strstr(argv0, "ifdown") != NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
int
|
|
|
|
acquire_state_lock(const char *state_path, const char *lifname)
|
|
|
|
{
|
|
|
|
if (exec_opts.mock || exec_opts.no_lock)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
char lockpath[4096] = {};
|
|
|
|
|
|
|
|
snprintf(lockpath, sizeof lockpath, "%s.%s.lock", state_path, lifname);
|
|
|
|
|
|
|
|
int fd = open(lockpath, O_CREAT | O_WRONLY | O_TRUNC);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "opening lockfile %s: %s\n", lockpath, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fprintf(stderr, "getting flags for lockfile: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (fcntl(fd, F_SETFD, flags) == -1)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fprintf(stderr, "setting lockfile close-on-exec: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct flock fl = {
|
|
|
|
.l_type = F_WRLCK,
|
|
|
|
.l_whence = SEEK_SET
|
|
|
|
};
|
|
|
|
|
|
|
|
if (exec_opts.verbose)
|
|
|
|
fprintf(stderr, "ifupdown: acquiring lock on %s\n", lockpath);
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLKW, &fl) == -1)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fprintf(stderr, "locking lockfile: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
bool
|
2020-07-24 09:42:34 +00:00
|
|
|
change_interface(struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *ifname)
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-29 09:10:55 +00:00
|
|
|
int lockfd = acquire_state_lock(exec_opts.state_file, ifname);
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
{
|
2020-07-24 09:57:52 +00:00
|
|
|
fprintf(stderr, "%s: changing state of interface %s to '%s'\n",
|
2020-07-23 16:57:46 +00:00
|
|
|
argv0, ifname, up ? "up" : "down");
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
if (!lif_lifecycle_run(&exec_opts, iface, collection, state, ifname, up))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-23 16:57:46 +00:00
|
|
|
fprintf(stderr, "%s: failed to change interface %s state to '%s'\n",
|
2020-07-23 16:44:45 +00:00
|
|
|
argv0, ifname, up ? "up" : "down");
|
2020-07-29 09:10:55 +00:00
|
|
|
|
|
|
|
if (lockfd != -1)
|
|
|
|
close(lockfd);
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
if (lockfd != -1)
|
|
|
|
close(lockfd);
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
bool
|
|
|
|
change_auto_interfaces(struct lif_dict *collection, struct lif_dict *state, 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-24 09:42:34 +00:00
|
|
|
if (!change_interface(iface, collection, state, iface->ifname))
|
2020-07-23 16:57:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
int
|
2020-07-24 09:07:19 +00:00
|
|
|
ifupdown_main(int argc, char *argv[])
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
|
|
|
up = !is_ifdown();
|
|
|
|
|
|
|
|
struct lif_dict state = {};
|
|
|
|
struct lif_dict collection = {};
|
|
|
|
struct option long_options[] = {
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
{"interfaces", required_argument, 0, 'i'},
|
|
|
|
{"auto", no_argument, 0, 'a'},
|
|
|
|
{"include", required_argument, 0, 'I'},
|
|
|
|
{"exclude", required_argument, 0, 'X'},
|
|
|
|
{"state-file", required_argument, 0, 'S'},
|
|
|
|
{"no-act", no_argument, 0, 'n'},
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
2020-07-25 08:22:27 +00:00
|
|
|
{"executor-path", required_argument, 0, 'E'},
|
2020-07-28 14:23:58 +00:00
|
|
|
{"force", no_argument, 0, 'f'},
|
2020-07-29 09:01:49 +00:00
|
|
|
{"no-lock", no_argument, 0, 'L'},
|
2020-07-23 16:44:45 +00:00
|
|
|
{NULL, 0, 0, 0}
|
|
|
|
};
|
|
|
|
struct match_options match_opts = {};
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-07-29 09:01:49 +00:00
|
|
|
int c = getopt_long(argc, argv, "hVi:aI:X:S:nvE:fL", long_options, NULL);
|
2020-07-23 16:44:45 +00:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
2020-07-24 09:07:19 +00:00
|
|
|
ifupdown_usage();
|
2020-07-23 16:44:45 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
lif_common_version();
|
|
|
|
break;
|
|
|
|
case 'i':
|
2020-07-26 08:50:01 +00:00
|
|
|
exec_opts.interfaces_file = optarg;
|
2020-07-23 16:44:45 +00:00
|
|
|
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;
|
|
|
|
case 'S':
|
2020-07-29 09:10:55 +00:00
|
|
|
exec_opts.state_file = optarg;
|
2020-07-23 16:44:45 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
exec_opts.mock = true;
|
|
|
|
exec_opts.verbose = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
exec_opts.verbose = true;
|
|
|
|
break;
|
2020-07-25 08:22:27 +00:00
|
|
|
case 'E':
|
|
|
|
exec_opts.executor_path = optarg;
|
|
|
|
break;
|
2020-07-28 14:23:58 +00:00
|
|
|
case 'f':
|
|
|
|
break;
|
2020-07-29 09:01:49 +00:00
|
|
|
case 'L':
|
2020-07-29 09:10:55 +00:00
|
|
|
exec_opts.no_lock = true;
|
2020-07-29 09:01:49 +00:00
|
|
|
break;
|
2020-07-23 16:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
if (!lif_state_read_path(&state, exec_opts.state_file))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-29 09:10:55 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file);
|
2020-07-23 16:44:45 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-26 08:50:01 +00:00
|
|
|
if (!lif_interface_file_parse(&collection, exec_opts.interfaces_file))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-26 08:50:01 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.interfaces_file);
|
2020-07-24 10:48:50 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lif_state_sync(&state, &collection))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not sync state\n", argv0);
|
2020-07-23 16:44:45 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
if (match_opts.is_auto)
|
|
|
|
{
|
|
|
|
if (!change_auto_interfaces(&collection, &state, &match_opts))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
2020-07-24 09:07:19 +00:00
|
|
|
ifupdown_usage();
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
int idx = optind;
|
|
|
|
for (; idx < argc; idx++)
|
|
|
|
{
|
|
|
|
char lifbuf[4096];
|
|
|
|
strlcpy(lifbuf, argv[idx], sizeof lifbuf);
|
|
|
|
|
|
|
|
char *ifname = lifbuf;
|
|
|
|
char *lifname = lifbuf;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((p = strchr(lifbuf, '=')) != NULL)
|
|
|
|
{
|
|
|
|
*p++ = '\0';
|
|
|
|
lifname = p;
|
|
|
|
}
|
|
|
|
|
2020-07-24 06:15:26 +00:00
|
|
|
struct lif_interface *iface = lif_state_lookup(&state, &collection, argv[idx]);
|
|
|
|
if (iface == NULL)
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-24 06:15:26 +00:00
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&collection, lifname);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown interface %s\n", argv0, argv[idx]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
iface = entry->data;
|
2020-07-23 16:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
if (!change_interface(iface, &collection, &state, ifname))
|
2020-07-23 16:44:45 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
if (!exec_opts.mock && !lif_state_write_path(&state, exec_opts.state_file))
|
2020-07-23 17:01:05 +00:00
|
|
|
{
|
2020-07-29 09:10:55 +00:00
|
|
|
fprintf(stderr, "%s: could not update %s\n", argv0, exec_opts.state_file);
|
2020-07-23 17:01:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-07-24 09:07:19 +00:00
|
|
|
|
|
|
|
struct if_applet ifup_applet = {
|
|
|
|
.name = "ifup",
|
|
|
|
.main = ifupdown_main,
|
|
|
|
.usage = ifupdown_usage
|
|
|
|
};
|
|
|
|
|
|
|
|
struct if_applet ifdown_applet = {
|
|
|
|
.name = "ifdown",
|
|
|
|
.main = ifupdown_main,
|
|
|
|
.usage = ifupdown_usage
|
|
|
|
};
|