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