]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Add doveadm_cmd_param_uint64/uint32/int32()
authorMarco Bettini <marco.bettini@open-xchange.com>
Fri, 22 Apr 2022 13:03:51 +0000 (13:03 +0000)
committerMarco Bettini <marco.bettini@open-xchange.com>
Wed, 1 Jun 2022 10:02:11 +0000 (10:02 +0000)
src/doveadm/doveadm-cmd-parse.c
src/doveadm/doveadm-cmd-parse.h
src/doveadm/doveadm-dict.c
src/doveadm/doveadm-director.c
src/doveadm/doveadm-fs.c
src/doveadm/doveadm-log.c
src/doveadm/doveadm-pw.c
src/doveadm/doveadm-zlib.c

index 3d5adedb1dfa90ebe702f637cec49903edcd6c86..8077d025b49bc52ebf39bda2a71b6f0434b5d734 100644 (file)
@@ -40,6 +40,23 @@ bool doveadm_cmd_param_bool(const struct doveadm_cmd_context *cctx,
        return TRUE;
 }
 
+#define doveadm_cmd_param_int64_cast(type, check) \
+bool doveadm_cmd_param_##type(const struct doveadm_cmd_context *cctx, \
+                             const char *name, type##_t *value_r) \
+{ \
+       int64_t value; \
+       bool ret = doveadm_cmd_param_int64(cctx, name, &value); \
+       if (ret) { \
+               i_assert(check); \
+               *value_r = (type##_t) value; \
+       } \
+       return ret; \
+}
+
+doveadm_cmd_param_int64_cast(uint64, value >= 0)
+doveadm_cmd_param_int64_cast(uint32, value >= 0 && value <= UINT32_MAX)
+doveadm_cmd_param_int64_cast(int32, value >= INT32_MIN && value <= INT32_MAX)
+
 bool doveadm_cmd_param_int64(const struct doveadm_cmd_context *cctx,
                             const char *name, int64_t *value_r)
 {
@@ -171,6 +188,14 @@ doveadm_fill_param(struct doveadm_cmd_param *param,
                                        "Invalid number: %s", value);
                        return -1;
                }
+               if ((param->flags & CMD_PARAM_FLAG_UNSIGNED) != 0 &&
+                   param->value.v_int64 < 0) {
+                       param->value_set = FALSE;
+                       *error_r = t_strdup_printf(
+                                       "Cannot be negative: %s", value);
+                       param->value_set = FALSE;
+                       return -1;
+               }
                break;
        case CMD_PARAM_IP:
                if (net_addr2ip(value, &param->value.v_ip) != 0) {
index 94d1cadef9eea3fea85648c8687022b35c06ec5f..960db0167eec710dd23da797f456e7854d0f3614 100644 (file)
@@ -23,6 +23,7 @@ typedef enum {
        CMD_PARAM_FLAG_NONE             = 0x0,
        CMD_PARAM_FLAG_POSITIONAL       = 0x1,
        CMD_PARAM_FLAG_DO_NOT_EXPOSE    = 0x2,
+       CMD_PARAM_FLAG_UNSIGNED         = 0x4,  /* int64 checked to be non negative */
 } doveadm_cmd_param_flag_t;
 
 typedef enum {
@@ -104,6 +105,12 @@ doveadm_cmd_param_flag(const struct doveadm_cmd_context *cctx, const char *name)
 
 bool doveadm_cmd_param_int64(const struct doveadm_cmd_context *cctx,
                             const char *name, int64_t *value_r);
+bool doveadm_cmd_param_uint64(const struct doveadm_cmd_context *cctx,
+                             const char *name, uint64_t *value_r);
+bool doveadm_cmd_param_int32(const struct doveadm_cmd_context *cctx,
+                            const char *name, int32_t *value_r);
+bool doveadm_cmd_param_uint32(const struct doveadm_cmd_context *cctx,
+                             const char *name, uint32_t *value_r);
 bool doveadm_cmd_param_str(const struct doveadm_cmd_context *cctx,
                           const char *name, const char **value_r);
 bool doveadm_cmd_param_ip(const struct doveadm_cmd_context *cctx,
index a99272338bb9e51aa39e4be9ad4884f16329de5f..05589d2c9857054ea1afb1399ebd059a43224039 100644 (file)
@@ -295,7 +295,7 @@ DOVEADM_CMD_PARAMS_END
        .usage = "[-u <user>] [-t <timestamp-nsecs>] <dict uri> <key> <value>",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('u', "user", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "value", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
@@ -307,7 +307,7 @@ DOVEADM_CMD_PARAMS_END
        .usage = "[-u <user>] [-t <timestamp-nsecs>] <dict uri> <key>",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('u', "user", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
@@ -318,7 +318,7 @@ DOVEADM_CMD_PARAMS_END
        .usage = "[-u <user>] [-t <timestamp-nsecs>] <dict uri> <key> <diff>",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('u', "user", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('t', "timestamp", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "dict-uri", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "key", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "difference", CMD_PARAM_INT64, CMD_PARAM_FLAG_POSITIONAL)
index 89089a6983060fab8874786816ce50a8cb0c1383..1bbf5c4d57aef56286c8bf8eac42039eb502b4bf 100644 (file)
@@ -1022,7 +1022,7 @@ DOVEADM_CMD_PARAMS_END
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('a', "socket-path", CMD_PARAM_STR, 0)
 DOVEADM_CMD_PARAM('F', "force-flush", CMD_PARAM_BOOL, 0)
-DOVEADM_CMD_PARAM('\0', "max-parallel", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('\0', "max-parallel", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "host", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAMS_END
 },
index 17f6129927dbf4a69d3ea819b50eb45c2a19d980..f2f60e0887b4645e19af82571173ab9cd6b10268 100644 (file)
@@ -558,7 +558,7 @@ DOVEADM_CMD_PARAMS_END
        .usage = "[-R] [-n <count>] <fs-driver> <fs-args> <path> [<path> ...]",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('R', "recursive", CMD_PARAM_BOOL, 0)
-DOVEADM_CMD_PARAM('n', "max-parallel", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('n', "max-parallel", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('\0', "fs-driver", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "fs-args", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
 DOVEADM_CMD_PARAM('\0', "path", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
index f63ad4d1d9b880b8dd1ca178df5819d9ee07e5b8..af06ebb7019557b1a14ced18703916743f7190ad 100644 (file)
@@ -392,7 +392,7 @@ DOVEADM_CMD_PARAMS_END
        .usage = "[-s <min_timestamp>]",
        .cmd = cmd_log_errors,
 DOVEADM_CMD_PARAMS_START
-DOVEADM_CMD_PARAM('s', "since", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('s', "since", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAMS_END
 }
 };
index a4f852c81a9c8ecab5aba69040a0dae05bad3601..bdc983269c70a810bf00fc7d7d4642d1eb847286 100644 (file)
@@ -23,8 +23,7 @@ static void cmd_pw(struct doveadm_cmd_context *cctx)
        const char *scheme = NULL;
        const char *plaintext = NULL;
        const char *test_hash = NULL;
-       bool list_schemes = FALSE, reverse_verify = FALSE;
-       int64_t rounds_int64;
+       bool list_schemes = FALSE;
        struct module_dir_load_settings mod_set;
        struct password_generate_params gen_params;
        i_zero(&gen_params);
@@ -43,16 +42,12 @@ static void cmd_pw(struct doveadm_cmd_context *cctx)
 
        (void)doveadm_cmd_param_bool(cctx, "list", &list_schemes);
        (void)doveadm_cmd_param_str(cctx, "plaintext", &plaintext);
-       if (doveadm_cmd_param_int64(cctx, "rounds", &rounds_int64)) {
-               if (rounds_int64 > UINT_MAX)
-                       i_fatal("Invalid number of rounds: %"PRId64, rounds_int64);
-               gen_params.rounds = rounds_int64;
-       }
+       (void)doveadm_cmd_param_uint32(cctx, "rounds", &gen_params.rounds);
        (void)doveadm_cmd_param_str(cctx, "scheme", &scheme);
-       if (doveadm_cmd_param_str(cctx, "test-hash", &test_hash))
-               reverse_verify = TRUE;
        (void)doveadm_cmd_param_str(cctx, "user", &gen_params.user);
-       (void)doveadm_cmd_param_bool(cctx, "reverse-verify", &reverse_verify);
+       bool reverse_verify =
+               doveadm_cmd_param_flag(cctx, "reverse-verify") ||
+               doveadm_cmd_param_str(cctx, "test-hash", &test_hash);
 
        if (list_schemes) {
                ARRAY_TYPE(password_scheme_p) arr;
@@ -127,7 +122,7 @@ struct doveadm_cmd_ver2 doveadm_cmd_pw = {
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('l', "list", CMD_PARAM_BOOL, 0)
 DOVEADM_CMD_PARAM('p', "plaintext", CMD_PARAM_STR, 0)
-DOVEADM_CMD_PARAM('r', "rounds", CMD_PARAM_INT64, 0)
+DOVEADM_CMD_PARAM('r', "rounds", CMD_PARAM_INT64, CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAM('s', "scheme", CMD_PARAM_STR, 0)
 DOVEADM_CMD_PARAM('t', "test-hash", CMD_PARAM_STR, 0)
 DOVEADM_CMD_PARAM('u', "user", CMD_PARAM_STR, 0)
index 0987de92e37e831182617fe745e16a4118fcc52e..d205f417b74ebc3186a075219b7017c16d329d16 100644 (file)
@@ -294,6 +294,6 @@ struct doveadm_cmd_ver2 doveadm_cmd_zlibconnect = {
        .usage = "<host> [<port>]",
 DOVEADM_CMD_PARAMS_START
 DOVEADM_CMD_PARAM('\0', "host", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
-DOVEADM_CMD_PARAM('\0', "port", CMD_PARAM_INT64, CMD_PARAM_FLAG_POSITIONAL)
+DOVEADM_CMD_PARAM('\0', "port", CMD_PARAM_INT64, CMD_PARAM_FLAG_POSITIONAL|CMD_PARAM_FLAG_UNSIGNED)
 DOVEADM_CMD_PARAMS_END
 };