Skip to content

Commit

Permalink
cli: Use macro for flags check
Browse files Browse the repository at this point in the history
  • Loading branch information
walid-git committed Dec 26, 2023
1 parent 1825eb2 commit 1bd61cf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
12 changes: 5 additions & 7 deletions bin/varnishd/cache/cache_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ cli_cb_before(const struct cli *cli, struct cli_proto *clp, const char * const *
(void)av;

if (clp != NULL &&
!DO_DEBUG(DBG_CLI_SHOW_SENSITIVE) &&
(clp->desc->flags & CLI_F_SENSITIVE)) {
VCLS_IsSensitive(clp->desc, DO_DEBUG(DBG_CLI_SHOW_SENSITIVE))) {
VSB_clear(cli->cmd);
if (clp->logfunc != NULL)
clp->logfunc(cli, av, cli->cmd);
Expand All @@ -95,14 +94,13 @@ cli_cb_after(const struct cli *cli, struct cli_proto *clp, const char * const *a
(void)av;
const char *h = "(hidden)";

if (clp == NULL ||
DO_DEBUG(DBG_CLI_SHOW_SENSITIVE) ||
!(clp->desc->flags & CLI_F_SENSITIVE)) {
if (clp != NULL &&
VCLS_IsSensitive(clp->desc, DO_DEBUG(DBG_CLI_SHOW_SENSITIVE))) {
VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
cli->result, strlen(h), h);
} else {
VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
cli->result, strlen(h), h);
cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
}
}

Expand Down
16 changes: 6 additions & 10 deletions bin/varnishd/mgt/mgt_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mcf_askchild(struct cli *cli, const char * const *av, void *priv)
}

cmd = mgt_cmd_lookup(av[1]);
if (cmd != NULL && cmd->flags & CLI_F_INTERNAL) {
if (cmd != NULL && VCLS_CMD_IS(cmd, INTERNAL)) {
VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
VCLI_SetResult(cli, CLIS_UNKNOWN);
return;
Expand Down Expand Up @@ -371,9 +371,7 @@ mgt_cli_cb_before(const struct cli *cli, struct cli_proto *clp, const char * con
if (cli->priv == stderr)
fprintf(stderr, "> %s\n", VSB_data(cli->cmd));

if (cmd != NULL &&
!MGT_DO_DEBUG(DBG_CLI_SHOW_SENSITIVE) &&
(cmd->flags & CLI_F_SENSITIVE)) {
if (VCLS_IsSensitive(cmd, MGT_DO_DEBUG(DBG_CLI_SHOW_SENSITIVE))) {
d = (*VSB_data(cli->cmd) == '-');
VSB_clear(cli->cmd);
VSB_printf(cli->cmd, "%s", d ? "-" : "");
Expand All @@ -394,14 +392,12 @@ mgt_cli_cb_after(const struct cli *cli, struct cli_proto *clp, const char * cons

if (av) {
cmd = (clp == NULL ? mgt_cmd_lookup(av[1]) : clp->desc);
if (cmd == NULL ||
MGT_DO_DEBUG(DBG_CLI_SHOW_SENSITIVE) ||
!(cmd->flags & CLI_F_SENSITIVE)) {
if (VCLS_IsSensitive(cmd, MGT_DO_DEBUG(DBG_CLI_SHOW_SENSITIVE))) {
MGT_Complain(C_CLI, "CLI %s Wr %03u %s",
cli->ident, cli->result, VSB_data(cli->sb));
cli->ident, cli->result, "(hidden)");
} else {
MGT_Complain(C_CLI, "CLI %s Wr %03u %s",
cli->ident, cli->result, "(hidden)");
cli->ident, cli->result, VSB_data(cli->sb));
}
}
if (cli->priv != stderr)
Expand Down Expand Up @@ -756,7 +752,7 @@ mgt_DumpRstCli(void)
cp = cmds[z];
if (!strncmp(cp->request, "debug.", 6))
continue;
if (cp->flags & CLI_F_INTERNAL)
if (VCLS_CMD_IS(cp, INTERNAL))
continue;
printf(".. _ref_cli_");
for (p = cp->request; *p; p++)
Expand Down
4 changes: 4 additions & 0 deletions include/vcli_serve.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ enum cli_flags {
#include "tbl/cli_flags.h"
};

#define VCLS_CMD_IS(cmd, FLAG) ((cmd)->flags & CLI_F_##FLAG)
#define VCLS_PROTO_IS(clp, FLAG) VCLS_CMD_IS((clp)->desc, FLAG)

struct cli_cmd_desc {
/* Must match CLI_CMD macro in include/tbl/cli_cmds.h */
const char *request;
Expand Down Expand Up @@ -105,6 +108,7 @@ typedef void cls_cbc_f(const struct cli*, struct cli_proto *, const char * const
struct VCLS *VCLS_New(struct VCLS *);
void VCLS_SetHooks(struct VCLS *, cls_cbc_f *, cls_cbc_f *);
void VCLS_SetLimit(struct VCLS *, volatile unsigned *);
unsigned VCLS_IsSensitive(const struct cli_cmd_desc *, unsigned);
struct cli *VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc,
void *priv);
void VCLS_AddFunc(struct VCLS *cs, struct cli_proto *clp);
Expand Down
23 changes: 16 additions & 7 deletions lib/libvarnish/vcli_serve.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ VCLS_func_help(struct cli *cli, const char * const *av, void *priv)
}
}
VTAILQ_FOREACH(clp, &cs->funcs, list) {
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (VCLS_PROTO_IS(clp, AUTH) && !cli->auth)
continue;
if (clp->desc->flags & CLI_F_INTERNAL)
if (VCLS_PROTO_IS(clp, INTERNAL))
continue;
if (av[0] != NULL && !strcmp(clp->desc->request, av[0])) {
help_helper(cli, clp, av);
return;
} else if (av[0] == NULL) {
d = clp->desc->flags & CLI_F_DEBUG ? 2 : 1;
d = VCLS_PROTO_IS(clp, DEBUG) ? 2 : 1;
if (filter & d)
help_helper(cli, clp, av);
}
Expand All @@ -213,9 +213,9 @@ VCLS_func_help_json(struct cli *cli, const char * const *av, void *priv)
VCLI_JSON_begin(cli, 2, av);
VTAILQ_FOREACH(clp, &cs->funcs, list) {
sep = "";
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (VCLS_PROTO_IS(clp, AUTH) && !cli->auth)
continue;
if (clp->desc->flags & CLI_F_INTERNAL)
if (VCLS_PROTO_IS(clp, INTERNAL))
continue;
VCLI_Out(cli, ",\n {\n");
VSB_indent(cli->sb, 2);
Expand Down Expand Up @@ -325,7 +325,7 @@ cls_lookup(char * const *av, struct cli *cli, struct VCLS *cs, enum cmd_error_e


VTAILQ_FOREACH(clp, &cs->funcs, list) {
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (VCLS_PROTO_IS(clp, AUTH) && !cli->auth)
continue;
if (!strcmp(clp->desc->request, av[1]))
break;
Expand Down Expand Up @@ -389,7 +389,7 @@ cls_exec(struct VCLS_fd *cfd, char * const *av)
case (CMD_ERR_UNKNOWN):
break;
default:
if (cs->wildcard && (!(cs->wildcard->desc->flags & CLI_F_AUTH) || cli->auth))
if (cs->wildcard && (!VCLS_PROTO_IS(cs->wildcard, AUTH) || cli->auth))
cls_dispatch(cli, cs->wildcard, av, na);
break;
}
Expand Down Expand Up @@ -716,6 +716,15 @@ VCLS_Destroy(struct VCLS **csp)
FREE_OBJ(cs);
}

unsigned
VCLS_IsSensitive(const struct cli_cmd_desc *cmd, unsigned show) {

if (cmd == NULL || show)
return 0;

return VCLS_CMD_IS(cmd, SENSITIVE);
}

/**********************************************************************
* Utility functions for implementing CLI commands
*/
Expand Down

0 comments on commit 1bd61cf

Please sign in to comment.