2009-09-10 10:31:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "command.h"
|
2009-09-15 09:49:14 +00:00
|
|
|
#include "pki.h"
|
2009-09-10 10:31:40 +00:00
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
2009-09-10 10:31:40 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
#include <library.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <utils/optionsfrom.h>
|
|
|
|
|
2009-09-10 10:31:40 +00:00
|
|
|
/**
|
|
|
|
* Registered commands.
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
command_t cmds[MAX_COMMANDS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* active command.
|
|
|
|
*/
|
|
|
|
static int active = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* number of registered commands
|
|
|
|
*/
|
|
|
|
static int registered = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* help command index
|
|
|
|
*/
|
|
|
|
static int help_idx;
|
2009-09-10 10:31:40 +00:00
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
static int argc;
|
|
|
|
|
|
|
|
static char **argv;
|
|
|
|
|
|
|
|
static options_t *options;
|
|
|
|
|
2009-09-10 10:31:40 +00:00
|
|
|
/**
|
|
|
|
* Global options used by all subcommands
|
|
|
|
*/
|
2009-09-15 09:49:14 +00:00
|
|
|
static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
|
2009-09-10 10:31:40 +00:00
|
|
|
|
|
|
|
/**
|
2009-09-15 08:20:22 +00:00
|
|
|
* Global optstring used by all subcommands
|
|
|
|
*/
|
2009-09-15 09:49:14 +00:00
|
|
|
static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
|
2009-09-15 08:20:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build command_opts/command_optstr for the active command
|
2009-09-10 10:31:40 +00:00
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
static void build_opts()
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-15 08:20:22 +00:00
|
|
|
int i, pos = 0;
|
2009-09-10 10:31:40 +00:00
|
|
|
|
|
|
|
memset(command_opts, 0, sizeof(command_opts));
|
2009-09-15 08:20:22 +00:00
|
|
|
memset(command_optstring, 0, sizeof(command_optstring));
|
2009-09-10 10:44:06 +00:00
|
|
|
if (active == help_idx)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
command_opts[i].name = cmds[i].cmd;
|
|
|
|
command_opts[i].val = cmds[i].op;
|
2009-09-15 08:20:22 +00:00
|
|
|
command_optstring[i] = cmds[i].op;
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].options[i].name; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
command_opts[i].name = cmds[active].options[i].name;
|
|
|
|
command_opts[i].has_arg = cmds[active].options[i].arg;
|
|
|
|
command_opts[i].val = cmds[active].options[i].op;
|
2009-09-15 08:20:22 +00:00
|
|
|
command_optstring[pos++] = cmds[active].options[i].op;
|
|
|
|
switch (cmds[active].options[i].arg)
|
|
|
|
{
|
|
|
|
case optional_argument:
|
|
|
|
command_optstring[pos++] = ':';
|
|
|
|
/* FALL */
|
|
|
|
case required_argument:
|
|
|
|
command_optstring[pos++] = ':';
|
|
|
|
/* FALL */
|
|
|
|
case no_argument:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
/**
|
|
|
|
* getopt_long wrapper
|
|
|
|
*/
|
|
|
|
int command_getopt(char **arg)
|
|
|
|
{
|
|
|
|
int op;
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
if (!options->from(options, optarg, &argc, &argv, optind))
|
|
|
|
{
|
|
|
|
/* a error value */
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case 'v':
|
2009-09-16 11:16:00 +00:00
|
|
|
dbg_default_set_level(atoi(optarg));
|
2009-09-15 09:49:14 +00:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
*arg = optarg;
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 10:31:40 +00:00
|
|
|
/**
|
|
|
|
* Register a command
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
void command_register(command_t command)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-15 09:49:14 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
cmds[registered] = command;
|
|
|
|
/* append default options, but not to --help */
|
|
|
|
if (!active)
|
|
|
|
{
|
|
|
|
for (i = 0; i < countof(cmds[registered].options); i++)
|
|
|
|
{
|
|
|
|
if (cmds[registered].options[i].name)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cmds[registered].options[i++] = (command_option_t) {
|
|
|
|
"debug", 'v', 1, "set debug level, default: 1"
|
|
|
|
};
|
|
|
|
cmds[registered].options[i++] = (command_option_t) {
|
|
|
|
"options", '+', 1, "read command line options from file"
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
registered++;
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print usage text, with an optional error
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
int command_usage(char *error)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
FILE *out = stdout;
|
2009-09-15 08:20:22 +00:00
|
|
|
char buf[64];
|
2009-09-10 10:31:40 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
out = stderr;
|
|
|
|
fprintf(out, "Error: %s\n", error);
|
|
|
|
}
|
|
|
|
fprintf(out, "strongSwan %s PKI tool\n", VERSION);
|
|
|
|
fprintf(out, "usage:\n");
|
2009-09-10 10:44:06 +00:00
|
|
|
if (active == help_idx)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-15 08:20:22 +00:00
|
|
|
snprintf(buf, sizeof(buf), "--%s (-%c)", cmds[i].cmd, cmds[i].op);
|
|
|
|
fprintf(out, " pki %-14s %s\n", buf, cmds[i].description);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].line[i]; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
fprintf(out, " pki --%s %s\n",
|
|
|
|
cmds[active].cmd, cmds[active].line[i]);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
fprintf(out, " %s\n", cmds[active].line[i]);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].options[i].name; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-15 08:20:22 +00:00
|
|
|
snprintf(buf, sizeof(buf), "--%s (-%c)",
|
|
|
|
cmds[active].options[i].name, cmds[active].options[i].op);
|
|
|
|
fprintf(out, " %-15s %s\n",
|
|
|
|
buf, cmds[active].options[i].desc);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return error != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show usage information
|
|
|
|
*/
|
|
|
|
static int help(int argc, char *argv[])
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
return command_usage(NULL);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
/**
|
|
|
|
* Dispatch cleanup hook
|
|
|
|
*/
|
|
|
|
static void cleanup()
|
|
|
|
{
|
|
|
|
options->destroy(options);
|
|
|
|
}
|
|
|
|
|
2009-09-10 10:31:40 +00:00
|
|
|
/**
|
|
|
|
* Dispatch commands.
|
|
|
|
*/
|
2009-09-15 09:49:14 +00:00
|
|
|
int command_dispatch(int c, char *v[])
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
int op, i;
|
|
|
|
|
2009-09-15 09:49:14 +00:00
|
|
|
options = options_create();
|
|
|
|
atexit(cleanup);
|
2009-09-10 10:44:06 +00:00
|
|
|
active = help_idx = registered;
|
2009-09-15 09:49:14 +00:00
|
|
|
argc = c;
|
|
|
|
argv = v;
|
2009-09-10 10:44:06 +00:00
|
|
|
command_register((command_t){help, 'h', "help", "show usage information"});
|
|
|
|
|
|
|
|
build_opts();
|
2009-09-15 09:49:14 +00:00
|
|
|
op = getopt_long(c, v, command_optstring, command_opts, NULL);
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
if (cmds[i].op == op)
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
active = i;
|
|
|
|
build_opts();
|
2009-09-15 09:49:14 +00:00
|
|
|
return cmds[i].call();
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 10:44:06 +00:00
|
|
|
return command_usage("invalid operation");
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|