2018-09-19 19:49:47 -04:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2019-01-01 19:58:10 -05:00
|
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 10:23:50 -05:00
|
|
|
*/
|
2015-06-05 09:58:00 -04:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "subcommands.h"
|
|
|
|
|
|
|
|
const char *PROG_NAME;
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *subcommand;
|
|
|
|
int (*function)(int, char**);
|
|
|
|
const char *description;
|
|
|
|
} subcommands[] = {
|
|
|
|
{ "show", show_main, "Shows the current configuration and device information" },
|
2017-10-15 16:52:38 -04:00
|
|
|
{ "showconf", showconf_main, "Shows the current configuration of a given WireGuard interface, for use with `setconf'" },
|
2015-06-05 09:58:00 -04:00
|
|
|
{ "set", set_main, "Change the current configuration, add peers, remove peers, or change peers" },
|
|
|
|
{ "setconf", setconf_main, "Applies a configuration file to a WireGuard interface" },
|
|
|
|
{ "addconf", setconf_main, "Appends a configuration file to a WireGuard interface" },
|
|
|
|
{ "genkey", genkey_main, "Generates a new private key and writes it to stdout" },
|
2017-04-20 14:21:50 -04:00
|
|
|
{ "genpsk", genkey_main, "Generates a new preshared key and writes it to stdout" },
|
2015-06-05 09:58:00 -04:00
|
|
|
{ "pubkey", pubkey_main, "Reads a private key from stdin and writes a public key to stdout" }
|
|
|
|
};
|
|
|
|
|
2016-07-03 14:06:33 -04:00
|
|
|
static void show_usage(FILE *file)
|
2015-06-05 09:58:00 -04:00
|
|
|
{
|
2016-07-03 14:06:33 -04:00
|
|
|
fprintf(file, "Usage: %s <cmd> [<args>]\n\n", PROG_NAME);
|
|
|
|
fprintf(file, "Available subcommands:\n");
|
2015-06-05 09:58:00 -04:00
|
|
|
for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i)
|
2016-07-03 14:06:33 -04:00
|
|
|
fprintf(file, " %s: %s\n", subcommands[i].subcommand, subcommands[i].description);
|
|
|
|
fprintf(file, "You may pass `--help' to any of these subcommands to view usage.\n");
|
2015-06-05 09:58:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
PROG_NAME = argv[0];
|
|
|
|
|
|
|
|
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help"))) {
|
2016-07-03 14:06:33 -04:00
|
|
|
show_usage(stdout);
|
|
|
|
return 0;
|
2015-06-05 09:58:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2017-12-01 13:42:46 -05:00
|
|
|
static char *new_argv[] = { "show", NULL };
|
2015-06-05 09:58:00 -04:00
|
|
|
return show_main(1, new_argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i) {
|
|
|
|
if (!strcmp(argv[1], subcommands[i].subcommand))
|
|
|
|
return subcommands[i].function(argc - 1, argv + 1);
|
|
|
|
}
|
|
|
|
|
2017-10-15 16:52:38 -04:00
|
|
|
fprintf(stderr, "Invalid subcommand: `%s'\n", argv[1]);
|
2016-07-03 14:06:33 -04:00
|
|
|
show_usage(stderr);
|
2015-06-05 09:58:00 -04:00
|
|
|
return 1;
|
|
|
|
}
|