From 3636465690769b43a7ba8d3ae47a6ea198fb032e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 2 Jun 2018 21:40:45 +0100 Subject: [PATCH] [Project] Move keypair function to Lua --- lualib/rspamadm/keypair.lua | 89 +++++++++++++++++++++++++++++++++++++ src/rspamadm/CMakeLists.txt | 1 - src/rspamadm/commands.c | 2 - 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 lualib/rspamadm/keypair.lua diff --git a/lualib/rspamadm/keypair.lua b/lualib/rspamadm/keypair.lua new file mode 100644 index 000000000..b5155dfaa --- /dev/null +++ b/lualib/rspamadm/keypair.lua @@ -0,0 +1,89 @@ +--[[ +Copyright (c) 2018, Vsevolod Stakhov + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +]]-- + +local argparse = require "argparse" +local rspamd_keypair = require "rspamd_cryptobox_keypair" +local ucl = require "ucl" + +-- Define command line options +local parser = argparse() + :name "rspamadm keypair" + :description "Manages keypairs for Rspamd" + :help_description_margin(30) + :command_target("command") + :require_command(false) + +local generate = parser:command "generate gen g" + :description "Creates a new keypair" +generate:flag "-s --sign" + :description "Generates a sign keypair instead of the encryption one" +generate:flag "-n --nist" + :description "Uses nist encryption algorithm" +generate:mutex( + generate:flag "-j --json" + :description "Output JSON instead of UCL", + generate:flag "-u --ucl" + :description "Output UCL" + :default(true) +) + +-- Default command is generate, so duplicate options +parser:flag "-s --sign" + :description "Generates a sign keypair instead of the encryption one" +parser:flag "-n --nist" + :description "Uses nist encryption algorithm" +parser:mutex( + parser:flag "-j --json" + :description "Output JSON instead of UCL", + parser:flag "-u --ucl" + :description "Output UCL" + :default(true) +) + +local function handler(args) + local opts = parser:parse(args) + + local command = opts.command or "generate" + + if command == 'generate' then + local mode = 'encryption' + if opts.sign then + mode = 'sign' + end + local alg = 'curve25519' + if opts.nist then + alg = 'nist' + end + -- TODO: probably, do it in a more safe way + local kp = rspamd_keypair.create(mode, alg):totable() + + local format = 'ucl' + + if opts.json then + format = 'json' + end + io.write(ucl.to_format(kp, format)) + else + parser:error('command %s is not yet implemented', command) + end +end + +return { + name = 'keypair', + aliases = {'kp', 'key'}, + handler = handler, + description = parser._description +} \ No newline at end of file diff --git a/src/rspamadm/CMakeLists.txt b/src/rspamadm/CMakeLists.txt index 11603d05d..2105c16b3 100644 --- a/src/rspamadm/CMakeLists.txt +++ b/src/rspamadm/CMakeLists.txt @@ -1,7 +1,6 @@ SET(RSPAMADMSRC rspamadm.c commands.c pw.c - keypair.c configtest.c fuzzy_convert.c fuzzy_merge.c diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c index 0b4e50c48..9216c79b6 100644 --- a/src/rspamadm/commands.c +++ b/src/rspamadm/commands.c @@ -19,7 +19,6 @@ #include "lua/lua_common.h" extern struct rspamadm_command pw_command; -extern struct rspamadm_command keypair_command; extern struct rspamadm_command configtest_command; extern struct rspamadm_command fuzzy_merge_command; extern struct rspamadm_command configdump_command; @@ -34,7 +33,6 @@ extern struct rspamadm_command dkim_keygen_command; const struct rspamadm_command *commands[] = { &help_command, &pw_command, - &keypair_command, &configtest_command, &fuzzy_merge_command, &configdump_command, -- 2.39.5