From aebe14a6e9646416b082bc76baf8417cced5d83f Mon Sep 17 00:00:00 2001 From: Ondrej Kozina Date: Wed, 15 Jul 2020 12:17:33 +0200 Subject: [PATCH] Introduce new function to parse command line arguments. To be used later in all tools. --- src/Makemodule.am | 1 + src/cryptsetup.h | 35 +++++++++++++ src/utils_args.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 src/utils_args.c diff --git a/src/Makemodule.am b/src/Makemodule.am index ece70fb1..5c5c675b 100644 --- a/src/Makemodule.am +++ b/src/Makemodule.am @@ -6,6 +6,7 @@ cryptsetup_SOURCES = \ lib/utils_loop.c \ lib/utils_io.c \ lib/utils_blkid.c \ + src/utils_args.c \ src/utils_tools.c \ src/utils_password.c \ src/utils_luks2.c \ diff --git a/src/cryptsetup.h b/src/cryptsetup.h index 85214309..7c6cf5a0 100644 --- a/src/cryptsetup.h +++ b/src/cryptsetup.h @@ -24,6 +24,7 @@ #ifndef CRYPTSETUP_H #define CRYPTSETUP_H +#include #include #include #include @@ -55,6 +56,11 @@ #define ROUND_SECTOR(x) (((x) + SECTOR_SIZE - 1) / SECTOR_SIZE) #define DEFAULT_WIPE_BLOCK 1048576 /* 1 MiB */ +#define MAX_ACTIONS 16 + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#endif extern int opt_debug; extern int opt_debug_json; @@ -127,4 +133,33 @@ void tools_cleanup(void); #define log_verbose(x...) clogger(NULL, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x) #define log_err(x...) clogger(NULL, CRYPT_LOG_ERROR, __FILE__, __LINE__, x) +typedef enum { + CRYPT_ARG_BOOL = 0, + CRYPT_ARG_STRING, + CRYPT_ARG_INT32, + CRYPT_ARG_UINT32, + CRYPT_ARG_INT64, + CRYPT_ARG_UINT64 +} crypt_arg_type_info; + +struct tools_arg { + const char *name; + bool set; + crypt_arg_type_info type; + union { + char *str_value; + uint64_t u64_value; + uint32_t u32_value; + int32_t i32_value; + int64_t i64_value; + } u; + const char *actions_array[MAX_ACTIONS]; +}; + +void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, struct tools_arg *arg, const char *popt_arg, int popt_val, bool(*needs_size_conv_fn)(unsigned arg_id)); + +void tools_args_free(struct tools_arg *args, size_t args_count); + +void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context); + #endif /* CRYPTSETUP_H */ diff --git a/src/utils_args.c b/src/utils_args.c new file mode 100644 index 00000000..3f1b223e --- /dev/null +++ b/src/utils_args.c @@ -0,0 +1,127 @@ +/* + * Cryptsetup command line arguments list + * + * Copyright (C) 2020 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020 Ondrej Kozina + * + * 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. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "cryptsetup.h" + +void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, struct tools_arg *arg, const char *popt_arg, int popt_val, bool(*needs_size_conv_fn)(unsigned arg_id)) +{ + char *end, msg[128]; + long int l; + long long int ll; + long unsigned int ul; + long long unsigned int ull; + + errno = 0; + + switch (type) { + case CRYPT_ARG_BOOL: + break; + case CRYPT_ARG_STRING: + if (arg->set) + free(arg->u.str_value); + arg->u.str_value = poptGetOptArg(popt_context); + break; + case CRYPT_ARG_INT32: + l = strtol(popt_arg, &end, 10); + if (*end || !*popt_arg || l > INT32_MAX || l < INT32_MIN || errno == ERANGE) + usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER), + poptGetInvocationName(popt_context)); + arg->u.i32_value = l; + break; + case CRYPT_ARG_UINT32: + ul = strtoul(popt_arg, &end, 10); + if (*end || !*popt_arg || ul > UINT32_MAX || errno == ERANGE) + usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER), + poptGetInvocationName(popt_context)); + arg->u.u32_value = ul; + break; + case CRYPT_ARG_INT64: + ll = strtoll(popt_arg, &end, 10); + if (*end || !*popt_arg || errno == ERANGE) + usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER), + poptGetInvocationName(popt_context)); + arg->u.i64_value = ll; + break; + case CRYPT_ARG_UINT64: + /* special size strings with units converted to integers */ + if (needs_size_conv_fn && needs_size_conv_fn(popt_val)) { + if (tools_string_to_size(NULL, popt_arg, &arg->u.u64_value)) { + snprintf(msg, sizeof(msg), _("Invalid size specification in parameter --%s."), arg->name); + usage(popt_context, EXIT_FAILURE, msg, + poptGetInvocationName(popt_context)); + } + } else { + ull = strtoull(popt_arg, &end, 10); + if (*end || !*popt_arg || errno == ERANGE) + usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER), + poptGetInvocationName(popt_context)); + arg->u.u64_value = ull; + } + break; + default: + /* this signals internal tools coding mistake */ + abort(); + } + + arg->set = true; +} + +void tools_args_free(struct tools_arg *args, size_t args_size) +{ + size_t i; + + for (i = 0; i < args_size; i++) { + if (args[i].set && args[i].type == CRYPT_ARG_STRING) + free(args[i].u.str_value); + } +} + +static bool action_allowed(const char *action, const char * const* list, size_t list_size) +{ + size_t i; + + if (!list[0]) + return true; + + for (i = 0; i < list_size && list[i]; i++) { + if (!strcmp(action, list[i])) + return true; + } + + return false; +} + +void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context) +{ + size_t i; + char msg[256]; + + for (i = 1; i < args_size; i++) { + if (args[i].set) { + if (action_allowed(action, args[i].actions_array, MAX_ACTIONS)) { + continue; + } else { + (void)snprintf(msg, sizeof(msg), _("Option --%s is not allowed with %s action."), args[i].name, action); + usage(popt_context, EXIT_FAILURE, msg, poptGetInvocationName(popt_context)); + } + } + } +}