diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2020-06-07 19:46:50 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2020-06-08 00:26:25 +0200 |
commit | deb7c5927fadc71f251bb6e0384dd77d4711e445 (patch) | |
tree | 3760aca1308b1e369eeb9ed9b9ad5936c1ede401 | |
parent | 163b252e9925277c4b3fa240ac6bb0750ffc0592 (diff) |
cmd: add misspelling suggestions for rule commands
# nft add rule foo ber counter
Error: No such file or directory; did you mean chain ‘bar’ in table ip ‘foo’?
add rule foo ber counter
^^^
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r-- | src/cmd.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -6,6 +6,7 @@ #include <iface.h> #include <errno.h> #include <stdlib.h> +#include <cache.h> #include <string.h> static int nft_cmd_enoent_table(struct netlink_ctx *ctx, const struct cmd *cmd, @@ -40,6 +41,40 @@ static int nft_cmd_enoent_chain(struct netlink_ctx *ctx, const struct cmd *cmd, return 1; } +static int nft_cmd_enoent_rule(struct netlink_ctx *ctx, const struct cmd *cmd, + struct location *loc) +{ + unsigned int flags = NFT_CACHE_TABLE | + NFT_CACHE_CHAIN; + const struct table *table; + struct chain *chain; + + if (cache_update(ctx->nft, flags, ctx->msgs) < 0) + return 0; + + table = table_lookup_fuzzy(&cmd->handle, &ctx->nft->cache); + if (table && strcmp(cmd->handle.table.name, table->handle.table.name)) { + netlink_io_error(ctx, loc, "%s; did you mean table ‘%s’ in family %s?", + strerror(ENOENT), table->handle.table.name, + family2str(table->handle.family)); + return 1; + } else if (!table) { + return 0; + } + + chain = chain_lookup_fuzzy(&cmd->handle, &ctx->nft->cache, &table); + if (chain && strcmp(cmd->handle.chain.name, chain->handle.chain.name)) { + netlink_io_error(ctx, loc, "%s; did you mean chain ‘%s’ in table %s ‘%s’?", + strerror(ENOENT), + chain->handle.chain.name, + family2str(table->handle.family), + table->handle.table.name); + return 1; + } + + return 0; +} + static int nft_cmd_enoent_set(struct netlink_ctx *ctx, const struct cmd *cmd, struct location *loc) { @@ -109,6 +144,9 @@ static void nft_cmd_enoent(struct netlink_ctx *ctx, const struct cmd *cmd, case CMD_OBJ_SET: ret = nft_cmd_enoent_set(ctx, cmd, loc); break; + case CMD_OBJ_RULE: + ret = nft_cmd_enoent_rule(ctx, cmd, loc); + break; case CMD_OBJ_COUNTER: case CMD_OBJ_QUOTA: case CMD_OBJ_CT_HELPER: |