2018-09-20 01:49:47 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2020-01-02 19:52:25 +01:00
|
|
|
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 16:23:50 +01:00
|
|
|
*/
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "subcommands.h"
|
2019-12-26 12:30:12 +01:00
|
|
|
#include "version.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
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 22:52:38 +02:00
|
|
|
{ "showconf", showconf_main, "Shows the current configuration of a given WireGuard interface, for use with `setconf'" },
|
2015-06-05 15:58:00 +02: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" },
|
2019-06-11 19:22:52 +02:00
|
|
|
{ "syncconf", setconf_main, "Synchronizes a configuration file to a WireGuard interface" },
|
2015-06-05 15:58:00 +02:00
|
|
|
{ "genkey", genkey_main, "Generates a new private key and writes it to stdout" },
|
2017-04-20 20:21:50 +02:00
|
|
|
{ "genpsk", genkey_main, "Generates a new preshared key and writes it to stdout" },
|
2015-06-05 15:58:00 +02:00
|
|
|
{ "pubkey", pubkey_main, "Reads a private key from stdin and writes a public key to stdout" }
|
|
|
|
};
|
|
|
|
|
2016-07-03 20:06:33 +02:00
|
|
|
static void show_usage(FILE *file)
|
2015-06-05 15:58:00 +02:00
|
|
|
{
|
2016-07-03 20:06:33 +02:00
|
|
|
fprintf(file, "Usage: %s <cmd> [<args>]\n\n", PROG_NAME);
|
|
|
|
fprintf(file, "Available subcommands:\n");
|
2015-06-05 15:58:00 +02:00
|
|
|
for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i)
|
2016-07-03 20:06:33 +02: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 15:58:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
PROG_NAME = argv[0];
|
|
|
|
|
2019-12-26 12:30:12 +01:00
|
|
|
if (argc == 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version") || !strcmp(argv[1], "version"))) {
|
|
|
|
printf("wireguard-tools v%s - https://git.zx2c4.com/wireguard-tools/\n", WIREGUARD_TOOLS_VERSION);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help"))) {
|
2016-07-03 20:06:33 +02:00
|
|
|
show_usage(stdout);
|
|
|
|
return 0;
|
2015-06-05 15:58:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2017-12-01 19:42:46 +01:00
|
|
|
static char *new_argv[] = { "show", NULL };
|
2015-06-05 15:58:00 +02: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 22:52:38 +02:00
|
|
|
fprintf(stderr, "Invalid subcommand: `%s'\n", argv[1]);
|
2016-07-03 20:06:33 +02:00
|
|
|
show_usage(stderr);
|
2015-06-05 15:58:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|