From: William Lallemand Date: Tue, 28 Nov 2023 16:57:21 +0000 (+0100) Subject: MINOR: mworker/cli: implements the customized payload pattern for master CLI X-Git-Tag: v2.9-dev12~44 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08f1e2bea2fd84f2e3b725e51a86503d09c89efa;p=thirdparty%2Fhaproxy.git MINOR: mworker/cli: implements the customized payload pattern for master CLI Implements the customized payload pattern for the master CLI. The pattern is stored in the stream in char pcli_payload_pat[8]. The principle is basically the same as the CLI one, it looks for '<<' then stores what's between '<<' and '\n', and look for it to exit the payload mode. --- diff --git a/include/haproxy/stream-t.h b/include/haproxy/stream-t.h index aa84ebf716..7e79b96e26 100644 --- a/include/haproxy/stream-t.h +++ b/include/haproxy/stream-t.h @@ -266,6 +266,7 @@ struct stream { int pcli_next_pid; /* next target PID to use for the CLI proxy */ int pcli_flags; /* flags for CLI proxy */ + char pcli_payload_pat[8]; /* payload pattern for the CLI proxy */ struct ist unique_id; /* custom unique ID */ diff --git a/src/cli.c b/src/cli.c index 94f2297451..98e261e082 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2555,7 +2555,6 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int int argl; /* number of args */ char *p; char *trim = NULL; - char *payload = NULL; int wtrim = 0; /* number of words to trim */ int reql = 0; int ret; @@ -2609,9 +2608,14 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int goto end; } + /* in payload mode, skip the whole parsing/exec and just look for a pattern */ if (s->pcli_flags & PCLI_F_PAYLOAD) { - if (reql == 1) /* last line of the payload */ - s->pcli_flags &= ~PCLI_F_PAYLOAD; + if (reql-1 == strlen(s->pcli_payload_pat)) { + /* the custom pattern len can be 0 (empty line) */ + if (strncmp(str, s->pcli_payload_pat, strlen(s->pcli_payload_pat)) == 0) { + s->pcli_flags &= ~PCLI_F_PAYLOAD; + } + } ret = reql; goto end; } @@ -2644,6 +2648,22 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int argl = i; + /* first look for '<<' at the beginning of the last argument */ + if (strncmp(args[argl-1], PAYLOAD_PATTERN, strlen(PAYLOAD_PATTERN)) == 0) { + size_t pat_len = strlen(args[argl-1] + strlen(PAYLOAD_PATTERN)); + + /* + * A customized pattern can't be more than 7 characters + * if it's more, don't make it a payload + */ + if (pat_len < sizeof(s->pcli_payload_pat)) { + s->pcli_flags |= PCLI_F_PAYLOAD; + /* copy the customized pattern, don't store the << */ + strncpy(s->pcli_payload_pat, args[argl-1] + strlen(PAYLOAD_PATTERN), sizeof(s->pcli_payload_pat)-1); + s->pcli_payload_pat[sizeof(s->pcli_payload_pat)-1] = '\0'; + } + } + for (; i < MAX_CLI_ARGS + 1; i++) args[i] = NULL; @@ -2658,12 +2678,6 @@ int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int p++; } - payload = strstr(str, PAYLOAD_PATTERN); - if ((end - 1) == (payload + strlen(PAYLOAD_PATTERN))) { - /* if the payload pattern is at the end */ - s->pcli_flags |= PCLI_F_PAYLOAD; - } - *(end-1) = '\n'; if (wtrim > 0) {