Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Configuration condition preprocessor |
| 3 | * |
| 4 | * Copyright 2000-2021 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <haproxy/api.h> |
| 14 | #include <haproxy/arg.h> |
| 15 | #include <haproxy/cfgcond.h> |
| 16 | #include <haproxy/global.h> |
| 17 | #include <haproxy/tools.h> |
| 18 | |
| 19 | /* supported condition predicates */ |
| 20 | const struct cond_pred_kw cond_predicates[] = { |
Remi Tricot-Le Breton | b01179a | 2021-10-11 15:34:12 +0200 | [diff] [blame] | 21 | { "defined", CFG_PRED_DEFINED, ARG1(1, STR) }, |
| 22 | { "feature", CFG_PRED_FEATURE, ARG1(1, STR) }, |
| 23 | { "streq", CFG_PRED_STREQ, ARG2(2, STR, STR) }, |
| 24 | { "strneq", CFG_PRED_STRNEQ, ARG2(2, STR, STR) }, |
| 25 | { "version_atleast", CFG_PRED_VERSION_ATLEAST, ARG1(1, STR) }, |
| 26 | { "version_before", CFG_PRED_VERSION_BEFORE, ARG1(1, STR) }, |
| 27 | { "openssl_version_atleast", CFG_PRED_OSSL_VERSION_ATLEAST, ARG1(1, STR) }, |
| 28 | { "openssl_version_before", CFG_PRED_OSSL_VERSION_BEFORE, ARG1(1, STR) }, |
| 29 | { "ssllib_name_startswith", CFG_PRED_SSLLIB_NAME_STARTSWITH, ARG1(1, STR) }, |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 30 | { NULL, CFG_PRED_NONE, 0 } |
| 31 | }; |
| 32 | |
| 33 | /* looks up a cond predicate matching the keyword in <str>, possibly followed |
| 34 | * by a parenthesis. Returns a pointer to it or NULL if not found. |
| 35 | */ |
| 36 | const struct cond_pred_kw *cfg_lookup_cond_pred(const char *str) |
| 37 | { |
| 38 | const struct cond_pred_kw *ret; |
| 39 | int len = strcspn(str, " ("); |
| 40 | |
| 41 | for (ret = &cond_predicates[0]; ret->word; ret++) { |
| 42 | if (len != strlen(ret->word)) |
| 43 | continue; |
| 44 | if (strncmp(str, ret->word, len) != 0) |
| 45 | continue; |
| 46 | return ret; |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 51 | /* Frees <term> and its args. NULL is supported and does nothing. */ |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 52 | void cfg_free_cond_term(struct cfg_cond_term *term) |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 53 | { |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 54 | if (!term) |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 55 | return; |
| 56 | |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 57 | if (term->type == CCTT_PAREN) { |
| 58 | cfg_free_cond_expr(term->expr); |
| 59 | term->expr = NULL; |
| 60 | } |
Willy Tarreau | 316ea7e | 2021-07-16 14:56:59 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 62 | free_args(term->args); |
| 63 | free(term->args); |
| 64 | free(term); |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 67 | /* Parse an indirect input text as a possible config condition term. |
| 68 | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 69 | * success. <term> is allocated and filled with the parsed info, and <text> |
| 70 | * is updated on success to point to the first unparsed character, or is left |
| 71 | * untouched on failure. On success, the caller must free <term> using |
| 72 | * cfg_free_cond_term(). An error will be set in <err> on error, and only |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 73 | * in this case. In this case the first bad character will be reported in |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 74 | * <errptr>. <maxdepth> corresponds to the maximum recursion depth permitted, |
| 75 | * it is decremented on each recursive call and the parsing will fail one |
| 76 | * reaching <= 0. |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 77 | */ |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 78 | int cfg_parse_cond_term(const char **text, struct cfg_cond_term **term, char **err, const char **errptr, int maxdepth) |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 79 | { |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 80 | struct cfg_cond_term *t; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 81 | const char *in = *text; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 82 | const char *end_ptr; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 83 | int err_arg; |
| 84 | int nbargs; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 85 | char *end; |
| 86 | long val; |
| 87 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 88 | while (*in == ' ' || *in == '\t') |
| 89 | in++; |
| 90 | |
| 91 | if (!*in) /* empty term does not parse */ |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 92 | return 0; |
| 93 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 94 | *term = NULL; |
| 95 | if (maxdepth <= 0) |
| 96 | goto fail0; |
| 97 | |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 98 | t = *term = calloc(1, sizeof(**term)); |
| 99 | if (!t) { |
| 100 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
| 101 | goto fail1; |
| 102 | } |
| 103 | |
| 104 | t->type = CCTT_NONE; |
| 105 | t->args = NULL; |
| 106 | t->neg = 0; |
| 107 | |
Willy Tarreau | ca56d3d | 2021-07-16 13:56:54 +0200 | [diff] [blame] | 108 | /* !<term> negates the term. White spaces permitted */ |
| 109 | while (*in == '!') { |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 110 | t->neg = !t->neg; |
Willy Tarreau | ca56d3d | 2021-07-16 13:56:54 +0200 | [diff] [blame] | 111 | do { in++; } while (*in == ' ' || *in == '\t'); |
| 112 | } |
| 113 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 114 | val = strtol(in, &end, 0); |
| 115 | if (end != in) { |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 116 | t->type = val ? CCTT_TRUE : CCTT_FALSE; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 117 | *text = end; |
| 118 | return 1; |
| 119 | } |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | 316ea7e | 2021-07-16 14:56:59 +0200 | [diff] [blame] | 121 | /* Try to parse '(' EXPR ')' */ |
| 122 | if (*in == '(') { |
| 123 | int ret; |
| 124 | |
| 125 | t->type = CCTT_PAREN; |
| 126 | t->args = NULL; |
| 127 | |
| 128 | do { in++; } while (*in == ' ' || *in == '\t'); |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 129 | ret = cfg_parse_cond_expr(&in, &t->expr, err, errptr, maxdepth - 1); |
Willy Tarreau | 316ea7e | 2021-07-16 14:56:59 +0200 | [diff] [blame] | 130 | if (ret == -1) |
| 131 | goto fail2; |
| 132 | if (ret == 0) |
| 133 | goto fail0; |
| 134 | |
| 135 | /* find the closing ')' */ |
| 136 | while (*in == ' ' || *in == '\t') |
| 137 | in++; |
| 138 | if (*in != ')') { |
| 139 | memprintf(err, "expected ')' after conditional expression '%s'", *text); |
| 140 | goto fail1; |
| 141 | } |
| 142 | do { in++; } while (*in == ' ' || *in == '\t'); |
| 143 | *text = in; |
| 144 | return 1; |
| 145 | } |
| 146 | |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 147 | /* below we'll likely all make_arg_list() so we must return only via |
| 148 | * the <done> label which frees the arg list. |
| 149 | */ |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 150 | t->pred = cfg_lookup_cond_pred(in); |
| 151 | if (t->pred) { |
| 152 | t->type = CCTT_PRED; |
| 153 | nbargs = make_arg_list(in + strlen(t->pred->word), -1, |
| 154 | t->pred->arg_mask, &t->args, err, |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 155 | &end_ptr, &err_arg, NULL); |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 156 | if (nbargs < 0) { |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 157 | memprintf(err, "%s in argument %d of predicate '%s' used in conditional expression", *err, err_arg, t->pred->word); |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 158 | if (errptr) |
| 159 | *errptr = end_ptr; |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 160 | goto fail2; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 161 | } |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 162 | *text = end_ptr; |
| 163 | return 1; |
| 164 | } |
| 165 | |
Willy Tarreau | 316ea7e | 2021-07-16 14:56:59 +0200 | [diff] [blame] | 166 | fail0: |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 167 | memprintf(err, "unparsable conditional expression '%s'", *text); |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 168 | fail1: |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 169 | if (errptr) |
| 170 | *errptr = *text; |
Willy Tarreau | 087b2d0 | 2021-07-16 14:27:20 +0200 | [diff] [blame] | 171 | fail2: |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 172 | cfg_free_cond_term(*term); |
| 173 | *term = NULL; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | /* evaluate a condition term on a .if/.elif line. The condition was already |
| 178 | * parsed in <term>. Returns -1 on error (in which case err is filled with a |
| 179 | * message, and only in this case), 0 if the condition is false, 1 if it's |
| 180 | * true. |
| 181 | */ |
| 182 | int cfg_eval_cond_term(const struct cfg_cond_term *term, char **err) |
| 183 | { |
| 184 | int ret = -1; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 186 | if (term->type == CCTT_FALSE) |
| 187 | ret = 0; |
| 188 | else if (term->type == CCTT_TRUE) |
| 189 | ret = 1; |
| 190 | else if (term->type == CCTT_PRED) { |
| 191 | /* here we know we have a valid predicate with valid arguments |
| 192 | * placed in term->args (which the caller will free). |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 193 | */ |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 194 | switch (term->pred->prd) { |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 195 | case CFG_PRED_DEFINED: // checks if arg exists as an environment variable |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 196 | ret = getenv(term->args[0].data.str.area) != NULL; |
| 197 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 198 | |
| 199 | case CFG_PRED_FEATURE: { // checks if the arg matches an enabled feature |
| 200 | const char *p; |
| 201 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 202 | ret = 0; // assume feature not found |
| 203 | for (p = build_features; (p = strstr(p, term->args[0].data.str.area)); p++) { |
| 204 | if (p > build_features && |
| 205 | (p[term->args[0].data.str.data] == ' ' || |
| 206 | p[term->args[0].data.str.data] == 0)) { |
| 207 | if (*(p-1) == '+') { // e.g. "+OPENSSL" |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 208 | ret = 1; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 209 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 210 | } |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 211 | else if (*(p-1) == '-') { // e.g. "-OPENSSL" |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 212 | ret = 0; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 213 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 214 | } |
| 215 | /* it was a sub-word, let's restart from next place */ |
| 216 | } |
| 217 | } |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 218 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 219 | } |
| 220 | case CFG_PRED_STREQ: // checks if the two arg are equal |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 221 | ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) == 0; |
| 222 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 223 | |
| 224 | case CFG_PRED_STRNEQ: // checks if the two arg are different |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 225 | ret = strcmp(term->args[0].data.str.area, term->args[1].data.str.area) != 0; |
| 226 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 227 | |
| 228 | case CFG_PRED_VERSION_ATLEAST: // checks if the current version is at least this one |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 229 | ret = compare_current_version(term->args[0].data.str.area) <= 0; |
| 230 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 231 | |
| 232 | case CFG_PRED_VERSION_BEFORE: // checks if the current version is older than this one |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 233 | ret = compare_current_version(term->args[0].data.str.area) > 0; |
| 234 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 235 | |
William Lallemand | 3aeb3f9 | 2021-08-21 23:59:56 +0200 | [diff] [blame] | 236 | case CFG_PRED_OSSL_VERSION_ATLEAST: { // checks if the current openssl version is at least this one |
| 237 | int opensslret = openssl_compare_current_version(term->args[0].data.str.area); |
| 238 | |
| 239 | if (opensslret < -1) /* can't parse the string or no openssl available */ |
| 240 | ret = -1; |
| 241 | else |
| 242 | ret = opensslret <= 0; |
| 243 | break; |
| 244 | } |
| 245 | case CFG_PRED_OSSL_VERSION_BEFORE: { // checks if the current openssl version is older than this one |
| 246 | int opensslret = openssl_compare_current_version(term->args[0].data.str.area); |
| 247 | |
| 248 | if (opensslret < -1) /* can't parse the string or no openssl available */ |
| 249 | ret = -1; |
| 250 | else |
| 251 | ret = opensslret > 0; |
| 252 | break; |
| 253 | } |
Remi Tricot-Le Breton | b01179a | 2021-10-11 15:34:12 +0200 | [diff] [blame] | 254 | case CFG_PRED_SSLLIB_NAME_STARTSWITH: { // checks if the current SSL library's name starts with a specified string (can be used to distinguish OpenSSL from LibreSSL or BoringSSL) |
| 255 | ret = openssl_compare_current_name(term->args[0].data.str.area) == 0; |
| 256 | break; |
| 257 | } |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 258 | default: |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 259 | memprintf(err, "internal error: unhandled conditional expression predicate '%s'", term->pred->word); |
| 260 | break; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
Willy Tarreau | 316ea7e | 2021-07-16 14:56:59 +0200 | [diff] [blame] | 263 | else if (term->type == CCTT_PAREN) { |
| 264 | ret = cfg_eval_cond_expr(term->expr, err); |
| 265 | } |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 266 | else { |
| 267 | memprintf(err, "internal error: unhandled condition term type %d", (int)term->type); |
| 268 | } |
Willy Tarreau | ca56d3d | 2021-07-16 13:56:54 +0200 | [diff] [blame] | 269 | |
| 270 | if (ret >= 0 && term->neg) |
| 271 | ret = !ret; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 276 | /* Frees <expr> and its terms and args. NULL is supported and does nothing. */ |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 277 | void cfg_free_cond_and(struct cfg_cond_and *expr) |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 278 | { |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 279 | struct cfg_cond_and *prev; |
| 280 | |
| 281 | while (expr) { |
| 282 | cfg_free_cond_term(expr->left); |
| 283 | prev = expr; |
| 284 | expr = expr->right; |
| 285 | free(prev); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /* Frees <expr> and its terms and args. NULL is supported and does nothing. */ |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 290 | void cfg_free_cond_expr(struct cfg_cond_expr *expr) |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 291 | { |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 292 | struct cfg_cond_expr *prev; |
| 293 | |
| 294 | while (expr) { |
| 295 | cfg_free_cond_and(expr->left); |
| 296 | prev = expr; |
| 297 | expr = expr->right; |
| 298 | free(prev); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Parse an indirect input text as a possible config condition sub-expr. |
| 303 | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
| 304 | * success. <expr> is filled with the parsed info, and <text> is updated on |
| 305 | * success to point to the first unparsed character, or is left untouched |
| 306 | * on failure. On success, the caller will have to free all lower-level |
| 307 | * allocated structs using cfg_free_cond_expr(). An error will be set in |
| 308 | * <err> on error, and only in this case. In this case the first bad |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 309 | * character will be reported in <errptr>. <maxdepth> corresponds to the |
| 310 | * maximum recursion depth permitted, it is decremented on each recursive |
| 311 | * call and the parsing will fail one reaching <= 0. |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 312 | */ |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 313 | int cfg_parse_cond_and(const char **text, struct cfg_cond_and **expr, char **err, const char **errptr, int maxdepth) |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 314 | { |
| 315 | struct cfg_cond_and *e; |
| 316 | const char *in = *text; |
| 317 | int ret = -1; |
| 318 | |
| 319 | if (!*in) /* empty expr does not parse */ |
| 320 | return 0; |
| 321 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 322 | *expr = NULL; |
| 323 | if (maxdepth <= 0) { |
| 324 | memprintf(err, "unparsable conditional sub-expression '%s'", in); |
| 325 | if (errptr) |
| 326 | *errptr = in; |
| 327 | goto done; |
| 328 | } |
| 329 | |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 330 | e = *expr = calloc(1, sizeof(**expr)); |
| 331 | if (!e) { |
| 332 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
| 333 | goto done; |
| 334 | } |
| 335 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 336 | ret = cfg_parse_cond_term(&in, &e->left, err, errptr, maxdepth - 1); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 337 | if (ret == -1) // parse error, error already reported |
| 338 | goto done; |
| 339 | |
| 340 | if (ret == 0) { |
| 341 | /* ret == 0, no other way to parse this */ |
| 342 | memprintf(err, "unparsable conditional sub-expression '%s'", in); |
| 343 | if (errptr) |
| 344 | *errptr = in; |
| 345 | ret = -1; |
| 346 | goto done; |
| 347 | } |
| 348 | |
| 349 | /* ret=1, we have a term in the left hand set */ |
| 350 | |
Ilya Shipitsin | 0188108 | 2021-08-07 14:41:56 +0500 | [diff] [blame] | 351 | /* find an optional '&&' */ |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 352 | while (*in == ' ' || *in == '\t') |
| 353 | in++; |
| 354 | |
| 355 | *text = in; |
| 356 | if (in[0] != '&' || in[1] != '&') |
| 357 | goto done; |
| 358 | |
| 359 | /* we have a '&&', let's parse the right handset's subexp */ |
| 360 | in += 2; |
| 361 | while (*in == ' ' || *in == '\t') |
| 362 | in++; |
| 363 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 364 | ret = cfg_parse_cond_and(&in, &e->right, err, errptr, maxdepth - 1); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 365 | if (ret > 0) |
| 366 | *text = in; |
| 367 | done: |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 368 | if (ret < 0) { |
| 369 | cfg_free_cond_and(*expr); |
| 370 | *expr = NULL; |
| 371 | } |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | /* Parse an indirect input text as a possible config condition term. |
| 376 | * Returns <0 on parsing error, 0 if the parser is desynchronized, or >0 on |
| 377 | * success. <expr> is filled with the parsed info, and <text> is updated on |
| 378 | * success to point to the first unparsed character, or is left untouched |
| 379 | * on failure. On success, the caller will have to free all lower-level |
| 380 | * allocated structs using cfg_free_cond_expr(). An error will be set in |
| 381 | * <err> on error, and only in this case. In this case the first bad |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 382 | * character will be reported in <errptr>. <maxdepth> corresponds to the |
| 383 | * maximum recursion depth permitted, it is decremented on each recursive call |
| 384 | * and the parsing will fail one reaching <= 0. |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 385 | */ |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 386 | int cfg_parse_cond_expr(const char **text, struct cfg_cond_expr **expr, char **err, const char **errptr, int maxdepth) |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 387 | { |
| 388 | struct cfg_cond_expr *e; |
| 389 | const char *in = *text; |
| 390 | int ret = -1; |
| 391 | |
| 392 | if (!*in) /* empty expr does not parse */ |
| 393 | return 0; |
| 394 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 395 | *expr = NULL; |
| 396 | if (maxdepth <= 0) { |
| 397 | memprintf(err, "unparsable conditional expression '%s'", in); |
| 398 | if (errptr) |
| 399 | *errptr = in; |
| 400 | goto done; |
| 401 | } |
| 402 | |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 403 | e = *expr = calloc(1, sizeof(**expr)); |
| 404 | if (!e) { |
| 405 | memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text); |
| 406 | goto done; |
| 407 | } |
| 408 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 409 | ret = cfg_parse_cond_and(&in, &e->left, err, errptr, maxdepth - 1); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 410 | if (ret == -1) // parse error, error already reported |
| 411 | goto done; |
| 412 | |
| 413 | if (ret == 0) { |
| 414 | /* ret == 0, no other way to parse this */ |
| 415 | memprintf(err, "unparsable conditional expression '%s'", in); |
| 416 | if (errptr) |
| 417 | *errptr = in; |
| 418 | ret = -1; |
| 419 | goto done; |
| 420 | } |
| 421 | |
| 422 | /* ret=1, we have a sub-expr in the left hand set */ |
| 423 | |
Ilya Shipitsin | 0188108 | 2021-08-07 14:41:56 +0500 | [diff] [blame] | 424 | /* find an optional '||' */ |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 425 | while (*in == ' ' || *in == '\t') |
| 426 | in++; |
| 427 | |
| 428 | *text = in; |
| 429 | if (in[0] != '|' || in[1] != '|') |
| 430 | goto done; |
| 431 | |
| 432 | /* we have a '||', let's parse the right handset's subexp */ |
| 433 | in += 2; |
| 434 | while (*in == ' ' || *in == '\t') |
| 435 | in++; |
| 436 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 437 | ret = cfg_parse_cond_expr(&in, &e->right, err, errptr, maxdepth - 1); |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 438 | if (ret > 0) |
| 439 | *text = in; |
| 440 | done: |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 441 | if (ret < 0) { |
| 442 | cfg_free_cond_expr(*expr); |
| 443 | *expr = NULL; |
| 444 | } |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | /* evaluate an sub-expression on a .if/.elif line. The expression is valid and |
| 449 | * was already parsed in <expr>. Returns -1 on error (in which case err is |
| 450 | * filled with a message, and only in this case), 0 if the condition is false, |
| 451 | * 1 if it's true. |
| 452 | */ |
| 453 | int cfg_eval_cond_and(struct cfg_cond_and *expr, char **err) |
| 454 | { |
| 455 | int ret; |
| 456 | |
| 457 | /* AND: loop on terms and sub-exp's terms as long as they're TRUE |
| 458 | * (stop on FALSE and ERROR). |
| 459 | */ |
| 460 | while ((ret = cfg_eval_cond_term(expr->left, err)) > 0 && expr->right) |
| 461 | expr = expr->right; |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | /* evaluate an expression on a .if/.elif line. The expression is valid and was |
| 466 | * already parsed in <expr>. Returns -1 on error (in which case err is filled |
| 467 | * with a message, and only in this case), 0 if the condition is false, 1 if |
| 468 | * it's true. |
| 469 | */ |
| 470 | int cfg_eval_cond_expr(struct cfg_cond_expr *expr, char **err) |
| 471 | { |
| 472 | int ret; |
| 473 | |
| 474 | /* OR: loop on sub-exps as long as they're FALSE (stop on TRUE and ERROR) */ |
| 475 | while ((ret = cfg_eval_cond_and(expr->left, err)) == 0 && expr->right) |
| 476 | expr = expr->right; |
| 477 | return ret; |
| 478 | } |
| 479 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 480 | /* evaluate a condition on a .if/.elif line. The condition is already tokenized |
| 481 | * in <err>. Returns -1 on error (in which case err is filled with a message, |
| 482 | * and only in this case), 0 if the condition is false, 1 if it's true. If |
| 483 | * <errptr> is not NULL, it's set to the first invalid character on error. |
| 484 | */ |
| 485 | int cfg_eval_condition(char **args, char **err, const char **errptr) |
| 486 | { |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 487 | struct cfg_cond_expr *expr = NULL; |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 488 | const char *text = args[0]; |
| 489 | int ret = -1; |
| 490 | |
| 491 | if (!*text) /* note: empty = false */ |
| 492 | return 0; |
| 493 | |
Willy Tarreau | dc70c18 | 2021-07-20 17:58:34 +0200 | [diff] [blame] | 494 | ret = cfg_parse_cond_expr(&text, &expr, err, errptr, MAX_CFG_RECURSION); |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 495 | if (ret != 0) { |
| 496 | if (ret == -1) // parse error, error already reported |
| 497 | goto done; |
Willy Tarreau | 379ceea | 2021-07-16 16:18:03 +0200 | [diff] [blame] | 498 | while (*text == ' ' || *text == '\t') |
| 499 | text++; |
| 500 | |
| 501 | if (*text) { |
| 502 | ret = -1; |
| 503 | memprintf(err, "unexpected character '%c' at the end of conditional expression '%s'", |
| 504 | *text, args[0]); |
| 505 | goto fail; |
| 506 | } |
| 507 | |
Willy Tarreau | ca81887 | 2021-07-16 14:46:09 +0200 | [diff] [blame] | 508 | ret = cfg_eval_cond_expr(expr, err); |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 509 | goto done; |
| 510 | } |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 511 | |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 512 | /* ret == 0, no other way to parse this */ |
| 513 | ret = -1; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 514 | memprintf(err, "unparsable conditional expression '%s'", args[0]); |
Willy Tarreau | 379ceea | 2021-07-16 16:18:03 +0200 | [diff] [blame] | 515 | fail: |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 516 | if (errptr) |
Willy Tarreau | f869095 | 2021-07-16 12:12:00 +0200 | [diff] [blame] | 517 | *errptr = text; |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 518 | done: |
Willy Tarreau | f1db20c | 2021-07-17 18:46:30 +0200 | [diff] [blame] | 519 | cfg_free_cond_expr(expr); |
Willy Tarreau | 66243b4 | 2021-07-16 15:39:28 +0200 | [diff] [blame] | 520 | return ret; |
| 521 | } |