2 * parser.c Parse various things
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2013 Alan DeKok <aland@freeradius.org>
25 #include <freeradius-devel/radiusd.h>
26 #include <freeradius-devel/parser.h>
27 #include <freeradius-devel/rad_assert.h>
31 #define PW_CAST_BASE (1850)
33 static const FR_NAME_NUMBER allowed_return_codes[] = {
47 * This file shouldn't use any functions from the server core.
50 size_t fr_cond_sprint(char *buffer, size_t bufsize, fr_cond_t const *c)
54 char *end = buffer + bufsize - 1;
58 *(p++) = '!'; /* FIXME: only allow for child? */
62 case COND_TYPE_EXISTS:
63 rad_assert(c->data.vpt != NULL);
65 len = snprintf(p, end - p, "<%s>", fr_int2str(dict_attr_types,
66 c->cast->type, "??"));
70 len = radius_tmpl2str(p, end - p, c->data.vpt);
75 rad_assert(c->data.map != NULL);
77 *(p++) = '['; /* for extra-clear debugging */
80 len = snprintf(p, end - p, "<%s>", fr_int2str(dict_attr_types,
81 c->cast->type, "??"));
85 len = radius_map2str(p, end - p, c->data.map);
93 rad_assert(c->data.child != NULL);
95 len = fr_cond_sprint(p, end - p, c->data.child);
101 strlcpy(buffer, "true", bufsize);
102 return strlen(buffer);
104 case COND_TYPE_FALSE:
105 strlcpy(buffer, "false", bufsize);
106 return strlen(buffer);
113 if (c->next_op == COND_NONE) {
114 rad_assert(c->next == NULL);
119 if (c->next_op == COND_AND) {
120 strlcpy(p, " && ", end - p);
123 } else if (c->next_op == COND_OR) {
124 strlcpy(p, " || ", end - p);
136 static ssize_t condition_tokenize_string(TALLOC_CTX *ctx, char const *start, char **out,
137 FR_TOKEN *op, char const **error)
139 char const *p = start;
147 *op = T_DOUBLE_QUOTED_STRING;
151 *op = T_SINGLE_QUOTED_STRING;
155 *op = T_BACK_QUOTED_STRING;
159 *op = T_OP_REG_EQ; /* a bit of a hack. */
164 *out = talloc_array(ctx, char, strlen(start) - 1); /* + 2 - 1 */
165 if (!*out) return -1;
179 *error = "End of string after escape";
204 *error = "Unterminated string";
208 static ssize_t condition_tokenize_word(TALLOC_CTX *ctx, char const *start, char **out,
209 FR_TOKEN *op, char const **error)
212 char const *p = start;
214 if ((*p == '"') || (*p == '\'') || (*p == '`') || (*p == '/')) {
215 return condition_tokenize_string(ctx, start, out, op, error);
219 if (*p == '&') p++; /* special-case &User-Name */
223 * The LHS should really be limited to only a few
224 * things. For now, we allow pretty much anything.
227 *error = "Unexpected escape";
239 * Spaces or special characters delineate the word
241 if (isspace((int) *p) || (*p == '&') || (*p == '|') ||
242 (*p == '!') || (*p == '=') || (*p == '<') || (*p == '>')) {
246 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
247 *error = "Unexpected start of string";
256 *error = "Empty string is invalid";
260 *out = talloc_array(ctx, char, len + 1);
261 memcpy(*out, start, len);
267 static ssize_t condition_tokenize_cast(char const *start, DICT_ATTR const **pda, char const **error)
269 char const *p = start;
273 while (isspace((int) *p)) p++; /* skip spaces before condition */
275 if (*p != '<') return 0;
279 while (*q && *q != '>') q++;
281 cast = fr_substr2int(dict_attr_types, p, PW_TYPE_INVALID, q - p);
282 if (cast == PW_TYPE_INVALID) {
283 *error = "Invalid data type in cast";
287 *pda = dict_attrbyvalue(PW_CAST_BASE + cast, 0);
289 *error = "Cannot cast to this data type";
295 while (isspace((int) *q)) q++; /* skip spaces after cast */
301 * Less code means less bugs
303 #define return_P(_x) *error = _x;goto return_p
304 #define return_0(_x) *error = _x;goto return_0
305 #define return_lhs(_x) *error = _x;goto return_lhs
306 #define return_rhs(_x) *error = _x;goto return_rhs
307 #define return_SLEN goto return_slen
310 /** Tokenize a conditional check
312 * @param[in] ctx for talloc
313 * @param[in] ci for CONF_ITEM
314 * @param[in] start the start of the string to process. Should be "(..."
315 * @param[in] brace look for a closing brace
316 * @param[in] flags do one/two pass
317 * @param[out] pcond pointer to the returned condition structure
318 * @param[out] error the parse error (if any)
319 * @return length of the string skipped, or when negative, the offset to the offending error
321 static ssize_t condition_tokenize(TALLOC_CTX *ctx, CONF_ITEM *ci, char const *start, int brace, fr_cond_t **pcond, char const **error, int flags)
324 char const *p = start;
325 char const *lhs_p, *rhs_p;
328 FR_TOKEN op, lhs_type, rhs_type;
330 c = talloc_zero(ctx, fr_cond_t);
332 rad_assert(c != NULL);
334 lhs_type = rhs_type = T_OP_INVALID;
336 while (isspace((int) *p)) p++; /* skip spaces before condition */
339 return_P("Empty condition is invalid");
348 while (isspace((int) *p)) p++; /* skip spaces after negation */
354 return_P("Double negation is invalid");
365 * We've already eaten one layer of
366 * brackets. Go recurse to get more.
368 c->type = COND_TYPE_CHILD;
370 slen = condition_tokenize(c, ci, p, true, &c->data.child, error, flags);
375 if (!c->data.child) {
376 return_P("Empty condition is invalid");
380 while (isspace((int) *p)) p++; /* skip spaces after (COND)*/
382 } else { /* it's a bare FOO==BAR */
384 * We didn't see anything special. The condition must be one of
394 return_P("Conditional check cannot begin with a regular expression");
397 slen = condition_tokenize_cast(p, &c->cast, error);
404 slen = condition_tokenize_word(c, p, &lhs, &lhs_type, error);
410 while (isspace((int)*p)) p++; /* skip spaces after LHS */
413 * We may (or not) have an operator
422 * don't skip the brace. We'll look for it later.
431 return_P("No closing brace at end of string");
439 } else if (((p[0] == '&') && (p[1] == '&')) ||
440 ((p[0] == '|') && (p[1] == '|'))) {
444 return_0("Cannot do cast for existence check");
447 c->type = COND_TYPE_EXISTS;
450 c->data.vpt = radius_str2tmpl(c, lhs, lhs_type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
452 return_P("Failed creating exists");
455 rad_assert(c->data.vpt->type != VPT_TYPE_REGEX);
457 } else { /* it's an operator */
461 * The next thing should now be a comparison operator.
464 c->type = COND_TYPE_MAP;
469 return_P("Invalid text. Expected comparison operator");
476 } else if (p[1] == '~') {
482 } else if (p[1] == '*') {
483 if (lhs_type != T_BARE_WORD) {
484 return_P("Cannot use !* on a string");
491 goto invalid_operator;
500 } else if (p[1] == '~') {
506 } else if (p[1] == '*') {
507 if (lhs_type != T_BARE_WORD) {
508 return_P("Cannot use =* on a string");
516 return_P("Invalid operator");
544 while (isspace((int) *p)) p++; /* skip spaces after operator */
547 return_P("Expected text after operator");
551 * Cannot have a cast on the RHS.
552 * But produce good errors, too.
555 DICT_ATTR const *cast_da;
557 slen = condition_tokenize_cast(p, &cast_da, error);
563 return_P("Unexpected cast");
566 if (c->cast != cast_da) {
567 return_P("Cannot cast to a different data type");
570 return_P("Unnecessary cast");
577 slen = condition_tokenize_word(c, p, &rhs, &rhs_type, error);
583 * Sanity checks for regexes.
587 return_P("Expected regular expression");
593 if (p[slen] == 'i') {
598 } else if (!regex && (*p == '/')) {
599 return_P("Unexpected regular expression");
602 c->data.map = radius_str2map(c, lhs, lhs_type, op, rhs, rhs_type,
603 REQUEST_CURRENT, PAIR_LIST_REQUEST,
604 REQUEST_CURRENT, PAIR_LIST_REQUEST);
608 * FIXME: In the future,
610 * know whether this is
612 * it's pass2, then an
613 * unknown attribute is a
616 return_0("Unknown attribute");
618 return_0("Syntax error");
622 * Could have been a reference to an attribute which is registered later.
623 * Mark it as being checked in pass2.
625 if ((lhs_type == T_BARE_WORD) &&
626 (c->data.map->dst->type == VPT_TYPE_LITERAL)) {
627 c->pass2_fixup = PASS2_FIXUP_ATTR;
631 * Save the CONF_ITEM for later.
633 c->data.map->ci = ci;
636 * @todo: check LHS and RHS separately, to
639 if ((c->data.map->src->type == VPT_TYPE_LIST) ||
640 (c->data.map->dst->type == VPT_TYPE_LIST)) {
641 return_0("Cannot use list references in condition");
645 * Check cast type. We can have the RHS
646 * a string if the LHS has a cast. But
647 * if the RHS is an attr, it MUST be the
648 * same type as the LHS.
651 if ((c->data.map->src->type == VPT_TYPE_ATTR) &&
652 (c->cast->type != c->data.map->src->da->type)) {
656 if (c->data.map->src->type == VPT_TYPE_REGEX) {
657 return_0("Cannot use cast with regex comparison");
661 * The LHS is a literal which has been cast to a data type.
662 * Cast it to the appropriate data type.
664 if ((c->data.map->dst->type == VPT_TYPE_LITERAL) &&
665 !radius_cast_tmpl(c->data.map->dst, c->cast)) {
666 *error = "Failed to parse field";
667 if (lhs) talloc_free(lhs);
668 if (rhs) talloc_free(rhs);
670 return -(lhs_p - start);
674 * The RHS is a literal, and the LHS has been cast to a data
677 if ((c->data.map->dst->type == VPT_TYPE_DATA) &&
678 (c->data.map->src->type == VPT_TYPE_LITERAL) &&
679 !radius_cast_tmpl(c->data.map->src, c->data.map->dst->da)) {
680 return_rhs("Failed to parse field");
684 * Casting to a redundant type means we don't need the cast.
686 * Do this LAST, as the rest of the code above assumes c->cast
689 if ((c->data.map->dst->type == VPT_TYPE_ATTR) &&
690 (c->cast->type == c->data.map->dst->da->type)) {
696 * Two attributes? They must be of the same type
698 if ((c->data.map->src->type == VPT_TYPE_ATTR) &&
699 (c->data.map->dst->type == VPT_TYPE_ATTR) &&
700 (c->data.map->dst->da->type != c->data.map->src->da->type)) {
702 return_0("Attribute comparisons must be of the same data type");
706 * Without a cast, we can't compare "foo" to User-Name,
707 * it has to be done the other way around.
709 if ((c->data.map->src->type == VPT_TYPE_ATTR) &&
710 (c->data.map->dst->type != VPT_TYPE_ATTR)) {
711 *error = "Cannot use attribute reference on right side of condition";
713 if (lhs) talloc_free(lhs);
714 if (rhs) talloc_free(rhs);
720 * Invalid: User-Name == bob
721 * Valid: User-Name == "bob"
723 * There's no real reason for
724 * this, other than consistency.
726 if ((c->data.map->dst->type == VPT_TYPE_ATTR) &&
727 (c->data.map->src->type != VPT_TYPE_ATTR) &&
728 (c->data.map->dst->da->type == PW_TYPE_STRING) &&
729 (c->data.map->op != T_OP_CMP_TRUE) &&
730 (c->data.map->op != T_OP_CMP_FALSE) &&
731 (rhs_type == T_BARE_WORD)) {
732 return_rhs("Must have string as value for attribute");
736 * Quotes around non-string
737 * attributes mean that it's
738 * either xlat, or an exec.
740 if ((c->data.map->dst->type == VPT_TYPE_ATTR) &&
741 (c->data.map->src->type != VPT_TYPE_ATTR) &&
742 (c->data.map->dst->da->type != PW_TYPE_STRING) &&
743 (c->data.map->dst->da->type != PW_TYPE_OCTETS) &&
744 (c->data.map->dst->da->type != PW_TYPE_DATE) &&
745 (rhs_type == T_SINGLE_QUOTED_STRING)) {
746 *error = "Value must be an unquoted string";
748 if (lhs) talloc_free(lhs);
749 if (rhs) talloc_free(rhs);
751 return -(rhs_p - start);
755 * The LHS has been cast to a data type, and the RHS is a
756 * literal. Cast the RHS to the type of the cast.
758 if (c->cast && (c->data.map->src->type == VPT_TYPE_LITERAL) &&
759 !radius_cast_tmpl(c->data.map->src, c->cast)) {
760 return_rhs("Failed to parse field");
764 * The LHS is an attribute, and the RHS is a literal. Cast the
765 * RHS to the data type of the LHS.
767 if ((c->data.map->dst->type == VPT_TYPE_ATTR) &&
768 (c->data.map->src->type == VPT_TYPE_LITERAL) &&
769 !radius_cast_tmpl(c->data.map->src, c->data.map->dst->da)) {
770 DICT_ATTR const *da = c->data.map->dst->da;
772 if ((da->vendor == 0) &&
773 ((da->attr == PW_AUTH_TYPE) ||
774 (da->attr == PW_AUTZ_TYPE) ||
775 (da->attr == PW_ACCT_TYPE) ||
776 (da->attr == PW_SESSION_TYPE) ||
777 (da->attr == PW_POST_AUTH_TYPE) ||
778 (da->attr == PW_PRE_PROXY_TYPE) ||
779 (da->attr == PW_POST_PROXY_TYPE) ||
780 (da->attr == PW_PRE_ACCT_TYPE) ||
781 (da->attr == PW_RECV_COA_TYPE) ||
782 (da->attr == PW_SEND_COA_TYPE))) {
784 * The types for these attributes are dynamically allocated
785 * by modules.c, so we can't enforce strictness here.
787 c->pass2_fixup = PASS2_FIXUP_TYPE;
790 return_rhs("Failed to parse value for attribute");
797 while (isspace((int) *p)) p++; /* skip spaces after RHS */
799 } /* parse a condition (COND) or FOO OP BAR*/
806 return_P("Unexpected closing brace");
810 while (isspace((int) *p)) p++; /* skip spaces after closing brace */
815 * End of string is now allowed.
819 return_P("No closing brace at end of string");
825 if (!(((p[0] == '&') && (p[1] == '&')) ||
826 ((p[0] == '|') && (p[1] == '|')))) {
827 *error = "Unexpected text after condition";
829 if (lhs) talloc_free(lhs);
830 if (rhs) talloc_free(rhs);
836 * Recurse to parse the next condition.
842 * May still be looking for a closing brace.
844 slen = condition_tokenize(c, ci, p, brace, &c->next, error, flags);
847 if (lhs) talloc_free(lhs);
848 if (rhs) talloc_free(rhs);
850 return slen - (p - start);
856 * Normalize the condition before returning.
858 * We collapse multiple levels of braces to one. Then
859 * convert maps to literals. Then literals to true/false
860 * statements. Then true/false ||/&& followed by other
861 * conditions to just conditions.
863 * Order is important. The more complex cases are
864 * converted to simpler ones, from the most complex cases
865 * to the simplest ones.
870 * (FOO) ... --> FOO ...
872 if ((c->type == COND_TYPE_CHILD) && !c->data.child->next) {
875 child = talloc_steal(ctx, c->data.child);
876 c->data.child = NULL;
878 child->next = talloc_steal(child, c->next);
881 child->next_op = c->next_op;
884 * Set the negation properly
886 if ((c->negate && !child->negate) ||
887 (!c->negate && child->negate)) {
888 child->negate = true;
890 child->negate = false;
899 * (FOO ...) --> FOO ...
901 * But don't do !(FOO || BAR) --> !FOO || BAR
902 * Because that's different.
904 if ((c->type == COND_TYPE_CHILD) &&
905 !c->next && !c->negate) {
908 child = talloc_steal(ctx, c->data.child);
909 c->data.child = NULL;
917 * Convert maps to literals. Convert one form of map to
918 * a standardized form. This doesn't make any
919 * theoretical difference, but it does mean that the
920 * run-time evaluation has fewer cases to check.
922 if (c->type == COND_TYPE_MAP) do {
924 * !FOO !~ BAR --> FOO =~ BAR
926 if (c->negate && (c->data.map->op == T_OP_REG_NE)) {
928 c->data.map->op = T_OP_REG_EQ;
932 * FOO !~ BAR --> !FOO =~ BAR
934 if (!c->negate && (c->data.map->op == T_OP_REG_NE)) {
936 c->data.map->op = T_OP_REG_EQ;
940 * !FOO != BAR --> FOO == BAR
942 if (c->negate && (c->data.map->op == T_OP_NE)) {
944 c->data.map->op = T_OP_CMP_EQ;
948 * This next one catches "LDAP-Group != foo",
949 * which doesn't work as-is, but this hack fixes
952 * FOO != BAR --> !FOO == BAR
954 if (!c->negate && (c->data.map->op == T_OP_NE)) {
956 c->data.map->op = T_OP_CMP_EQ;
961 * FOO !* BAR --> !FOO
963 if ((c->data.map->op == T_OP_CMP_TRUE) ||
964 (c->data.map->op == T_OP_CMP_FALSE)) {
965 value_pair_tmpl_t *vpt;
967 vpt = talloc_steal(c, c->data.map->dst);
968 c->data.map->dst = NULL;
971 * Invert the negation bit.
973 if (c->data.map->op == T_OP_CMP_FALSE) {
974 c->negate = !c->negate;
977 TALLOC_FREE(c->data.map);
979 c->type = COND_TYPE_EXISTS;
981 break; /* it's no longer a map */
985 * Both are data (IP address, integer, etc.)
987 * We can do the evaluation here, so that it
988 * doesn't need to be done at run time
990 if ((c->data.map->dst->type == VPT_TYPE_DATA) &&
991 (c->data.map->src->type == VPT_TYPE_DATA)) {
994 rad_assert(c->cast != NULL);
996 rcode = radius_evaluate_map(NULL, 0, 0, c);
997 TALLOC_FREE(c->data.map);
1001 c->type = COND_TYPE_TRUE;
1003 c->type = COND_TYPE_FALSE;
1006 break; /* it's no longer a map */
1010 * Both are literal strings. They're not parsed
1011 * as VPT_TYPE_DATA because there's no cast to an
1014 * We can do the evaluation here, so that it
1015 * doesn't need to be done at run time
1017 if ((c->data.map->src->type == VPT_TYPE_LITERAL) &&
1018 (c->data.map->dst->type == VPT_TYPE_LITERAL)) {
1021 rad_assert(c->cast == NULL);
1022 rad_assert(c->regex_i == false);
1024 rcode = radius_evaluate_map(NULL, 0, 0, c);
1026 c->type = COND_TYPE_TRUE;
1028 c->type = COND_TYPE_FALSE;
1032 * Free map after using it above.
1034 TALLOC_FREE(c->data.map);
1040 * Existence checks. We short-circuit static strings,
1043 if (c->type == COND_TYPE_EXISTS) {
1044 switch (c->data.vpt->type) {
1052 * 'true' and 'false' are special strings
1053 * which mean themselves.
1055 * For integers, 0 is false, all other
1056 * integers are true.
1058 * For strings, '' and "" are false.
1059 * 'foo' and "foo" are true.
1061 * The str2tmpl function takes care of
1062 * marking "%{foo}" as VPT_TYPE_XLAT, so
1063 * the strings here are fixed at compile
1066 * `exec` and "%{...}" are left alone.
1068 * Bare words must be module return
1071 case VPT_TYPE_LITERAL:
1072 if ((strcmp(c->data.vpt->name, "true") == 0) ||
1073 (strcmp(c->data.vpt->name, "1") == 0)) {
1074 c->type = COND_TYPE_TRUE;
1075 TALLOC_FREE(c->data.vpt);
1077 } else if ((strcmp(c->data.vpt->name, "false") == 0) ||
1078 (strcmp(c->data.vpt->name, "0") == 0)) {
1079 c->type = COND_TYPE_FALSE;
1080 TALLOC_FREE(c->data.vpt);
1082 } else if (!*c->data.vpt->name) {
1083 c->type = COND_TYPE_FALSE;
1084 TALLOC_FREE(c->data.vpt);
1086 } else if ((lhs_type == T_SINGLE_QUOTED_STRING) ||
1087 (lhs_type == T_DOUBLE_QUOTED_STRING)) {
1088 c->type = COND_TYPE_TRUE;
1089 TALLOC_FREE(c->data.vpt);
1091 } else if (lhs_type == T_BARE_WORD) {
1095 for (q = c->data.vpt->name;
1098 if (!isdigit((int) *q)) {
1104 * It's all digits, and therefore
1108 c->type = COND_TYPE_TRUE;
1109 TALLOC_FREE(c->data.vpt);
1113 rcode = fr_str2int(allowed_return_codes,
1114 c->data.vpt->name, 0);
1116 return_0("Expected a module return code");
1121 * Else lhs_type==T_OP_INVALID, and this
1122 * node was made by promoting a child
1123 * which had already been normalized.
1128 return_0("Cannot use data here");
1131 return_0("Internal sanity check failed");
1138 if (c->type == COND_TYPE_TRUE) {
1141 c->type = COND_TYPE_FALSE;
1148 if (c->type == COND_TYPE_FALSE) {
1151 c->type = COND_TYPE_TRUE;
1156 * true && FOO --> FOO
1158 if ((c->type == COND_TYPE_TRUE) &&
1159 (c->next_op == COND_AND)) {
1162 next = talloc_steal(ctx, c->next);
1171 * false && FOO --> false
1173 if ((c->type == COND_TYPE_FALSE) &&
1174 (c->next_op == COND_AND)) {
1175 talloc_free(c->next);
1177 c->next_op = COND_NONE;
1181 * false || FOO --> FOO
1183 if ((c->type == COND_TYPE_FALSE) &&
1184 (c->next_op == COND_OR)) {
1187 next = talloc_steal(ctx, c->next);
1196 * true || FOO --> true
1198 if ((c->type == COND_TYPE_TRUE) &&
1199 (c->next_op == COND_OR)) {
1200 talloc_free(c->next);
1202 c->next_op = COND_NONE;
1205 if (lhs) talloc_free(lhs);
1206 if (rhs) talloc_free(rhs);
1212 /** Tokenize a conditional check
1214 * @param[in] ctx for talloc
1215 * @param[in] ci for CONF_ITEM
1216 * @param[in] start the start of the string to process. Should be "(..."
1217 * @param[out] head the parsed condition structure
1218 * @param[out] error the parse error (if any)
1219 * @param[in] flags do one/two pass
1220 * @return length of the string skipped, or when negative, the offset to the offending error
1222 ssize_t fr_condition_tokenize(TALLOC_CTX *ctx, CONF_ITEM *ci, char const *start, fr_cond_t **head, char const **error, int flags)
1224 return condition_tokenize(ctx, ci, start, false, head, error, flags);
1230 bool fr_condition_walk(fr_cond_t *c, bool (*callback)(void *, fr_cond_t *), void *ctx)
1234 * Process this one, exit on error.
1236 if (!callback(ctx, c)) return false;
1239 case COND_TYPE_INVALID:
1242 case COND_TYPE_EXISTS:
1244 case COND_TYPE_TRUE:
1245 case COND_TYPE_FALSE:
1248 case COND_TYPE_CHILD:
1250 * Walk over the child.
1252 if (!fr_condition_walk(c->data.child, callback, ctx)) {
1260 if (c->next_op == COND_NONE) break;
1263 * process the next sibling