2 * evaluate.c Evaluate complex conditions
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 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/parser.h>
29 #include <freeradius-devel/rad_assert.h>
34 #ifdef WITH_EVAL_DEBUG
35 # define EVAL_DEBUG(fmt, ...) printf("EVAL: ");printf(fmt, ## __VA_ARGS__);printf("\n");fflush(stdout)
37 # define EVAL_DEBUG(...)
40 FR_NAME_NUMBER const modreturn_table[] = {
41 { "reject", RLM_MODULE_REJECT },
42 { "fail", RLM_MODULE_FAIL },
43 { "ok", RLM_MODULE_OK },
44 { "handled", RLM_MODULE_HANDLED },
45 { "invalid", RLM_MODULE_INVALID },
46 { "userlock", RLM_MODULE_USERLOCK },
47 { "notfound", RLM_MODULE_NOTFOUND },
48 { "noop", RLM_MODULE_NOOP },
49 { "updated", RLM_MODULE_UPDATED },
54 static bool all_digits(char const *string)
56 char const *p = string;
58 rad_assert(p != NULL);
60 if (*p == '\0') return false;
64 while (isdigit((int) *p)) p++;
69 /** Evaluate a template
71 * Converts a vp_tmpl_t to a boolean value.
73 * @param[in] request the REQUEST
74 * @param[in] modreturn the previous module return code
75 * @param[in] depth of the recursion (only used for debugging)
76 * @param[in] vpt the template to evaluate
77 * @return -1 on error, 0 for "no match", 1 for "match".
79 int radius_evaluate_tmpl(REQUEST *request, int modreturn, UNUSED int depth, vp_tmpl_t const *vpt)
86 case TMPL_TYPE_LITERAL:
87 modcode = fr_str2int(modreturn_table, vpt->name, RLM_MODULE_UNKNOWN);
88 if (modcode != RLM_MODULE_UNKNOWN) {
89 rcode = (modcode == modreturn);
94 * Else it's a literal string. Empty string is
95 * false, non-empty string is true.
97 * @todo: Maybe also check for digits?
99 * The VPT *doesn't* have a "bare word" type,
100 * which arguably it should.
102 rcode = (*vpt->name != '\0');
107 if (tmpl_find_vp(NULL, request, vpt) == 0) {
114 case TMPL_TYPE_XLAT_STRUCT:
120 if (!*vpt->name) return false;
121 rcode = tmpl_aexpand(request, &p, request, vpt, NULL, NULL);
123 EVAL_DEBUG("FAIL %d", __LINE__);
127 rcode = (data.strvalue && (*data.strvalue != '\0'));
128 talloc_free(data.ptr);
133 * Can't have a bare ... (/foo/) ...
135 case TMPL_TYPE_REGEX:
136 case TMPL_TYPE_REGEX_STRUCT:
141 EVAL_DEBUG("FAIL %d", __LINE__);
150 /** Perform a regular expressions comparison between two operands
152 * @return -1 on error, 0 for "no match", 1 for "match".
154 static int cond_do_regex(REQUEST *request, fr_cond_t const *c,
155 PW_TYPE lhs_type, value_data_t const *lhs, size_t lhs_len,
156 PW_TYPE rhs_type, value_data_t const *rhs, size_t rhs_len)
158 vp_map_t const *map = c->data.map;
163 regex_t *preg, *rreg = NULL;
164 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1]; /* +1 for %{0} (whole match) capture group */
165 size_t nmatch = sizeof(rxmatch) / sizeof(regmatch_t);
167 if (!lhs || (lhs_type != PW_TYPE_STRING)) return -1;
169 EVAL_DEBUG("CMP WITH REGEX %s %s",
170 map->rhs->tmpl_iflag ? "CASE INSENSITIVE" : "CASE SENSITIVE",
171 map->rhs->tmpl_mflag ? "MULTILINE" : "SINGLELINE");
173 switch (map->rhs->type) {
174 case TMPL_TYPE_REGEX_STRUCT: /* pre-compiled to a regex */
175 preg = map->rhs->tmpl_preg;
179 rad_assert(rhs_type == PW_TYPE_STRING);
180 rad_assert(rhs->strvalue);
181 slen = regex_compile(request, &rreg, rhs->strvalue, rhs_len,
182 map->rhs->tmpl_iflag, map->rhs->tmpl_mflag, true, true);
184 REMARKER(rhs->strvalue, -slen, fr_strerror());
185 EVAL_DEBUG("FAIL %d", __LINE__);
193 ret = regex_exec(preg, lhs->strvalue, lhs_len, rxmatch, &nmatch);
196 EVAL_DEBUG("CLEARING SUBCAPTURES");
197 regex_sub_to_request(request, NULL, NULL, 0, NULL, 0); /* clear out old entries */
201 EVAL_DEBUG("SETTING SUBCAPTURES");
202 regex_sub_to_request(request, &preg, lhs->strvalue, lhs_len, rxmatch, nmatch);
206 EVAL_DEBUG("REGEX ERROR");
207 REDEBUG("regex failed: %s", fr_strerror());
214 if (preg) talloc_free(rreg);
220 #ifdef WITH_EVAL_DEBUG
221 static void cond_print_operands(REQUEST *request,
222 PW_TYPE lhs_type, value_data_t const *lhs, size_t lhs_len,
223 PW_TYPE rhs_type, value_data_t const *rhs, size_t rhs_len)
226 if (lhs_type == PW_TYPE_STRING) {
227 EVAL_DEBUG("LHS: \"%s\" (%zu)" , lhs->strvalue, lhs_len);
231 lhs_hex = talloc_array(request, char, (lhs_len * 2) + 1);
233 if (lhs_type == PW_TYPE_OCTETS) {
234 fr_bin2hex(lhs_hex, lhs->octets, lhs_len);
236 fr_bin2hex(lhs_hex, (uint8_t const *)lhs, lhs_len);
239 EVAL_DEBUG("LHS: 0x%s (%zu)", lhs_hex, lhs_len);
241 talloc_free(lhs_hex);
244 EVAL_DEBUG("LHS: VIRTUAL");
248 if (rhs_type == PW_TYPE_STRING) {
249 EVAL_DEBUG("RHS: \"%s\" (%zu)" , rhs->strvalue, rhs_len);
253 rhs_hex = talloc_array(request, char, (rhs_len * 2) + 1);
255 if (rhs_type == PW_TYPE_OCTETS) {
256 fr_bin2hex(rhs_hex, rhs->octets, rhs_len);
258 fr_bin2hex(rhs_hex, (uint8_t const *)rhs, rhs_len);
261 EVAL_DEBUG("RHS: 0x%s (%zu)", rhs_hex, rhs_len);
263 talloc_free(rhs_hex);
266 EVAL_DEBUG("RHS: COMPILED");
271 /** Call the correct data comparison function for the condition
273 * Deals with regular expression comparisons, virtual attribute
274 * comparisons, and data comparisons.
276 * @return -1 on error, 0 for "no match", 1 for "match".
278 static int cond_cmp_values(REQUEST *request, fr_cond_t const *c,
279 PW_TYPE lhs_type, value_data_t const *lhs, size_t lhs_len,
280 PW_TYPE rhs_type, value_data_t const *rhs, size_t rhs_len)
282 vp_map_t const *map = c->data.map;
285 #ifdef WITH_EVAL_DEBUG
286 EVAL_DEBUG("CMP OPERANDS");
287 cond_print_operands(request, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
294 if (map->op == T_OP_REG_EQ) {
295 rcode = cond_do_regex(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
300 * Virtual attribute comparison.
302 if (c->pass2_fixup == PASS2_PAIRCOMPARE) {
305 EVAL_DEBUG("CMP WITH PAIRCOMPARE");
306 rad_assert(map->lhs->type == TMPL_TYPE_ATTR);
308 vp = fr_pair_afrom_da(request, map->lhs->tmpl_da);
309 vp->op = c->data.map->op;
311 value_data_copy(vp, &vp->data, rhs_type, rhs, rhs_len);
312 vp->vp_length = rhs_len;
314 rcode = paircompare(request, request->packet->vps, vp, NULL);
315 rcode = (rcode == 0) ? 1 : 0;
321 * At this point both operands should have been normalised
322 * to the same type, and there's no special comparisons
325 rad_assert(lhs_type == rhs_type);
327 EVAL_DEBUG("CMP WITH VALUE DATA");
328 rcode = value_data_cmp_op(map->op, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
340 EVAL_DEBUG("ERROR %i", rcode);
348 static size_t regex_escape(UNUSED REQUEST *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
352 while (*in && (outlen > 2)) {
362 case '[': /* we don't list close braces */
381 /** Convert both operands to the same type
383 * If casting is successful, we call cond_cmp_values to do the comparison
385 * @return -1 on error, 0 for "no match", 1 for "match".
387 static int cond_normalise_and_cmp(REQUEST *request, fr_cond_t const *c,
388 PW_TYPE lhs_type, DICT_ATTR const *lhs_enumv,
389 value_data_t const *lhs, size_t lhs_len)
391 vp_map_t const *map = c->data.map;
393 DICT_ATTR const *cast = NULL;
394 PW_TYPE cast_type = PW_TYPE_INVALID;
398 PW_TYPE rhs_type = PW_TYPE_INVALID;
399 DICT_ATTR const *rhs_enumv = NULL;
400 value_data_t const *rhs = NULL;
403 value_data_t lhs_cast, rhs_cast;
404 void *lhs_cast_buff = NULL, *rhs_cast_buff = NULL;
406 xlat_escape_t escape = NULL;
409 * Cast operand to correct type.
411 * With hack for strings that look like integers, to cast them
412 * to 64 bit unsigned integers.
414 * @fixme For things like this it'd be useful to have a 64bit signed type.
418 if ((cast_type != PW_TYPE_INVALID) && (_s ## _type != PW_TYPE_INVALID) && (cast_type != _s ## _type)) {\
420 EVAL_DEBUG("CASTING " #_s " FROM %s TO %s",\
421 fr_int2str(dict_attr_types, _s ## _type, "<INVALID>"),\
422 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));\
423 r = value_data_cast(request, &_s ## _cast, cast_type, cast, _s ## _type, _s ## _enumv, _s, _s ## _len);\
425 REDEBUG("Failed casting " #_s " operand: %s", fr_strerror());\
429 if (cast && cast->flags.is_pointer) _s ## _cast_buff = _s ## _cast.ptr;\
430 _s ## _type = cast_type;\
431 _s ## _len = (size_t)r;\
436 #define CHECK_INT_CAST(_l, _r) \
438 if ((cast_type == PW_TYPE_INVALID) &&\
439 _l && (_l ## _type == PW_TYPE_STRING) &&\
440 _r && (_r ## _type == PW_TYPE_STRING) &&\
441 all_digits(lhs->strvalue) && all_digits(rhs->strvalue)) {\
442 cast_type = PW_TYPE_INTEGER64;\
443 EVAL_DEBUG("OPERANDS ARE NUMBER STRINGS, SETTING CAST TO integer64");\
448 * Regular expressions need both operands to be strings
451 if (map->op == T_OP_REG_EQ) {
452 cast_type = PW_TYPE_STRING;
454 if (map->rhs->type == TMPL_TYPE_XLAT_STRUCT) escape = regex_escape;
459 * If it's a pair comparison, data gets cast to the
460 * type of the pair comparison attribute.
462 * Magic attribute is always the LHS.
464 if (c->pass2_fixup == PASS2_PAIRCOMPARE) {
465 rad_assert(!c->cast);
466 rad_assert(map->lhs->type == TMPL_TYPE_ATTR);
468 /* expensive assert */
469 rad_assert((map->rhs->type != TMPL_TYPE_ATTR) || !radius_find_compare(map->rhs->tmpl_da));
471 cast = map->lhs->tmpl_da;
472 cast_type = cast->type;
474 EVAL_DEBUG("NORMALISATION TYPE %s (PAIRCMP TYPE)",
475 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
477 * Otherwise we use the explicit cast, or implicit
478 * cast (from an attribute reference).
479 * We already have the data for the lhs, so we convert
482 } else if (c->cast) {
484 EVAL_DEBUG("NORMALISATION TYPE %s (EXPLICIT CAST)",
485 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
486 } else if (map->lhs->type == TMPL_TYPE_ATTR) {
487 cast = map->lhs->tmpl_da;
488 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM LHS REF)",
489 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
490 } else if (map->rhs->type == TMPL_TYPE_ATTR) {
491 cast = map->rhs->tmpl_da;
492 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM RHS REF)",
493 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
494 } else if (map->lhs->type == TMPL_TYPE_DATA) {
495 cast_type = map->lhs->tmpl_data_type;
496 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM LHS DATA)",
497 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));
498 } else if (map->rhs->type == TMPL_TYPE_DATA) {
499 cast_type = map->rhs->tmpl_data_type;
500 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM RHS DATA)",
501 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));
504 if (cast) cast_type = cast->type;
506 switch (map->rhs->type) {
512 for (vp = tmpl_cursor_init(&rcode, &cursor, request, map->rhs);
514 vp = tmpl_cursor_next(&cursor, map->rhs)) {
515 rhs_type = vp->da->type;
518 rhs_len = vp->vp_length;
520 CHECK_INT_CAST(lhs, rhs);
524 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
525 if (rcode != 0) break;
527 TALLOC_FREE(rhs_cast_buff);
533 rhs_type = map->rhs->tmpl_data_type;
534 rhs = &map->rhs->tmpl_data_value;
535 rhs_len = map->rhs->tmpl_data_length;
537 CHECK_INT_CAST(lhs, rhs);
541 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
545 * Expanded types start as strings, then get converted
546 * to the type of the attribute or the explicit cast.
548 case TMPL_TYPE_LITERAL:
551 case TMPL_TYPE_XLAT_STRUCT:
556 if (map->rhs->type != TMPL_TYPE_LITERAL) {
559 ret = tmpl_aexpand(request, &p, request, map->rhs, escape, NULL);
561 EVAL_DEBUG("FAIL [%i]", __LINE__);
569 data.strvalue = map->rhs->name;
570 rhs_len = map->rhs->len;
572 rad_assert(data.strvalue);
574 rhs_type = PW_TYPE_STRING;
577 CHECK_INT_CAST(lhs, rhs);
581 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
582 if (map->rhs->type != TMPL_TYPE_LITERAL)talloc_free(data.ptr);
588 * RHS is a compiled regex, we don't need to do anything with it.
590 case TMPL_TYPE_REGEX_STRUCT:
592 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, PW_TYPE_INVALID, NULL, 0);
595 * Unsupported types (should have been parse errors)
599 case TMPL_TYPE_UNKNOWN:
600 case TMPL_TYPE_ATTR_UNDEFINED:
601 case TMPL_TYPE_REGEX: /* Should now be a TMPL_TYPE_REGEX_STRUCT or TMPL_TYPE_XLAT_STRUCT */
608 talloc_free(lhs_cast_buff);
609 talloc_free(rhs_cast_buff);
617 * @param[in] request the REQUEST
618 * @param[in] modreturn the previous module return code
619 * @param[in] depth of the recursion (only used for debugging)
620 * @param[in] c the condition to evaluate
621 * @return -1 on error, 0 for "no match", 1 for "match".
623 int radius_evaluate_map(REQUEST *request, UNUSED int modreturn, UNUSED int depth, fr_cond_t const *c)
627 vp_map_t const *map = c->data.map;
629 EVAL_DEBUG(">>> MAP TYPES LHS: %s, RHS: %s",
630 fr_int2str(tmpl_names, map->lhs->type, "???"),
631 fr_int2str(tmpl_names, map->rhs->type, "???"));
633 switch (map->lhs->type) {
635 * LHS is an attribute or list
643 * Legacy paircompare call, skip processing the magic attribute
644 * if it's the LHS and cast RHS to the same type.
646 if ((c->pass2_fixup == PASS2_PAIRCOMPARE) && (map->op != T_OP_REG_EQ)) {
648 rad_assert(radius_find_compare(map->lhs->tmpl_da)); /* expensive assert */
650 rcode = cond_normalise_and_cmp(request, c, PW_TYPE_INVALID, NULL, NULL, 0);
653 for (vp = tmpl_cursor_init(&rcode, &cursor, request, map->lhs);
655 vp = tmpl_cursor_next(&cursor, map->lhs)) {
657 * Evaluate all LHS values, condition evaluates to true
658 * if we get at least one set of operands that
661 rcode = cond_normalise_and_cmp(request, c, vp->da->type, vp->da, &vp->data, vp->vp_length);
662 if (rcode != 0) break;
668 rcode = cond_normalise_and_cmp(request, c,
669 map->lhs->tmpl_data_type, NULL, &map->lhs->tmpl_data_value,
670 map->lhs->tmpl_data_length);
673 case TMPL_TYPE_LITERAL:
676 case TMPL_TYPE_XLAT_STRUCT:
681 if (map->lhs->type != TMPL_TYPE_LITERAL) {
684 ret = tmpl_aexpand(request, &p, request, map->lhs, NULL, NULL);
686 EVAL_DEBUG("FAIL [%i]", __LINE__);
691 data.strvalue = map->lhs->name;
694 rad_assert(data.strvalue);
696 rcode = cond_normalise_and_cmp(request, c, PW_TYPE_STRING, NULL, &data, ret);
697 if (map->lhs->type != TMPL_TYPE_LITERAL) talloc_free(data.ptr);
702 * Unsupported types (should have been parse errors)
705 case TMPL_TYPE_ATTR_UNDEFINED:
706 case TMPL_TYPE_UNKNOWN:
707 case TMPL_TYPE_REGEX: /* should now be a TMPL_TYPE_REGEX_STRUCT or TMPL_TYPE_XLAT_STRUCT */
708 case TMPL_TYPE_REGEX_STRUCT: /* not allowed as LHS */
719 /** Evaluate a fr_cond_t;
721 * @param[in] request the REQUEST
722 * @param[in] modreturn the previous module return code
723 * @param[in] depth of the recursion (only used for debugging)
724 * @param[in] c the condition to evaluate
725 * @return -1 on failure, -2 on attribute not found, 0 for "no match", 1 for "match".
727 int radius_evaluate_cond(REQUEST *request, int modreturn, int depth, fr_cond_t const *c)
730 #ifdef WITH_EVAL_DEBUG
733 fr_cond_sprint(buffer, sizeof(buffer), c);
734 EVAL_DEBUG("%s", buffer);
739 case COND_TYPE_EXISTS:
740 rcode = radius_evaluate_tmpl(request, modreturn, depth, c->data.vpt);
741 /* Existence checks are special, because we expect them to fail */
742 if (rcode < 0) rcode = 0;
746 rcode = radius_evaluate_map(request, modreturn, depth, c);
749 case COND_TYPE_CHILD:
750 rcode = radius_evaluate_cond(request, modreturn, depth + 1, c->data.child);
757 case COND_TYPE_FALSE:
761 EVAL_DEBUG("FAIL %d", __LINE__);
765 if (rcode < 0) return rcode;
767 if (c->negate) rcode = !rcode;
772 * FALSE && ... = FALSE
774 if (!rcode && (c->next_op == COND_AND)) return false;
779 if (rcode && (c->next_op == COND_OR)) return true;
785 EVAL_DEBUG("FAIL %d", __LINE__);
793 * The fr_pair_list_move() function in src/lib/valuepair.c does all sorts of
794 * extra magic that we don't want here.
796 * FIXME: integrate this with the code calling it, so that we
797 * only fr_pair_list_copy() those attributes that we're really going to
800 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from, bool do_xlat)
802 int i, j, count, from_count, to_count, tailto;
804 VALUE_PAIR *vp, *next, **last;
805 VALUE_PAIR **from_list, **to_list;
806 VALUE_PAIR *append, **append_tail;
809 REQUEST *fixup = NULL;
813 * Set up arrays for editing, to remove some of the
814 * O(N^2) dependencies. This also makes it easier to
815 * insert and remove attributes.
817 * It also means that the operators apply ONLY to the
818 * attributes in the original list. With the previous
819 * implementation of fr_pair_list_move(), adding two attributes
820 * via "+=" and then "=" would mean that the second one
821 * wasn't added, because of the existence of the first
822 * one in the "to" list. This implementation doesn't
825 * Also, the previous implementation did NOT implement
826 * "-=" correctly. If two of the same attributes existed
827 * in the "to" list, and you tried to subtract something
828 * matching the *second* value, then the fr_pair_delete_by_num()
829 * function was called, and the *all* attributes of that
830 * number were deleted. With this implementation, only
831 * the matching attributes are deleted.
834 for (vp = fr_cursor_init(&cursor, &from); vp; vp = fr_cursor_next(&cursor)) count++;
835 from_list = talloc_array(request, VALUE_PAIR *, count);
837 for (vp = fr_cursor_init(&cursor, to); vp; vp = fr_cursor_next(&cursor)) count++;
838 to_list = talloc_array(request, VALUE_PAIR *, count);
841 append_tail = &append;
844 * Move the lists to the arrays, and break the list
848 for (vp = from; vp != NULL; vp = next) {
850 from_list[from_count++] = vp;
855 ctx = talloc_parent(*to);
856 to_copy = fr_pair_list_copy(ctx, *to);
857 for (vp = to_copy; vp != NULL; vp = next) {
859 to_list[to_count++] = vp;
863 edited = talloc_zero_array(request, bool, to_count);
865 RDEBUG4("::: FROM %d TO %d MAX %d", from_count, to_count, count);
868 * Now that we have the lists initialized, start working
871 for (i = 0; i < from_count; i++) {
874 RDEBUG4("::: Examining %s", from_list[i]->da->name);
876 if (do_xlat) radius_xlat_do(request, from_list[i]);
879 * Attribute should be appended, OR the "to" list
880 * is empty, and we're supposed to replace or
881 * "add if not existing".
883 if (from_list[i]->op == T_OP_ADD) goto do_append;
886 for (j = 0; j < to_count; j++) {
887 if (edited[j] || !to_list[j] || !from_list[i]) continue;
890 * Attributes aren't the same, skip them.
892 if (from_list[i]->da != to_list[j]->da) {
897 * We don't use a "switch" statement here
898 * because we want to break out of the
899 * "for" loop over 'j' in most cases.
903 * Over-write the FIRST instance of the
904 * matching attribute name. We free the
905 * one in the "to" list, and move over
906 * the one in the "from" list.
908 if (from_list[i]->op == T_OP_SET) {
909 RDEBUG4("::: OVERWRITING %s FROM %d TO %d",
910 to_list[j]->da->name, i, j);
911 fr_pair_list_free(&to_list[j]);
912 to_list[j] = from_list[i];
919 * Add the attribute only if it does not
920 * exist... but it exists, so we stop
923 if (from_list[i]->op == T_OP_EQ) {
929 * Delete every attribute, independent
932 if (from_list[i]->op == T_OP_CMP_FALSE) {
937 * Delete all matching attributes from
940 if ((from_list[i]->op == T_OP_SUB) ||
941 (from_list[i]->op == T_OP_CMP_EQ) ||
942 (from_list[i]->op == T_OP_LE) ||
943 (from_list[i]->op == T_OP_GE)) {
945 int old_op = from_list[i]->op;
948 * Check for equality.
950 from_list[i]->op = T_OP_CMP_EQ;
953 * If equal, delete the one in
956 rcode = radius_compare_vps(NULL, from_list[i],
959 * We may want to do more
960 * subtractions, so we re-set the
961 * operator back to it's original
964 from_list[i]->op = old_op;
968 if (rcode != 0) goto delete;
974 RDEBUG4("::: DELETING %s FROM %d TO %d",
975 from_list[i]->da->name, i, j);
976 fr_pair_list_free(&to_list[j]);
982 * Enforce <=. If it's
987 RDEBUG4("::: REPLACING %s FROM %d TO %d",
988 from_list[i]->da->name, i, j);
989 fr_pair_list_free(&to_list[j]);
990 to_list[j] = from_list[i];
998 RDEBUG4("::: REPLACING %s FROM %d TO %d",
999 from_list[i]->da->name, i, j);
1000 fr_pair_list_free(&to_list[j]);
1001 to_list[j] = from_list[i];
1002 from_list[i] = NULL;
1011 rad_assert(0 == 1); /* panic! */
1015 * We were asked to add it if it didn't exist,
1016 * and it doesn't exist. Move it over to the
1017 * tail of the "to" list, UNLESS it was already
1018 * moved by another operator.
1020 if (!found && from_list[i]) {
1021 if ((from_list[i]->op == T_OP_EQ) ||
1022 (from_list[i]->op == T_OP_LE) ||
1023 (from_list[i]->op == T_OP_GE) ||
1024 (from_list[i]->op == T_OP_SET)) {
1026 RDEBUG4("::: APPENDING %s FROM %d TO %d",
1027 from_list[i]->da->name, i, tailto);
1028 *append_tail = from_list[i];
1029 from_list[i]->op = T_OP_EQ;
1030 from_list[i] = NULL;
1031 append_tail = &(*append_tail)->next;
1037 * Delete attributes in the "from" list.
1039 for (i = 0; i < from_count; i++) {
1040 if (!from_list[i]) continue;
1042 fr_pair_list_free(&from_list[i]);
1044 talloc_free(from_list);
1046 RDEBUG4("::: TO in %d out %d", to_count, tailto);
1049 * Re-chain the "to" list.
1051 fr_pair_list_free(to);
1054 if (to == &request->packet->vps) {
1056 } else if (request->parent && (to == &request->parent->packet->vps)) {
1057 fixup = request->parent;
1060 for (i = 0; i < tailto; i++) {
1061 if (!to_list[i]) continue;
1064 RDEBUG4("::: to[%d] = %s", i, vp->da->name);
1067 * Mash the operator to a simple '='. The
1068 * operators in the "to" list aren't used for
1069 * anything. BUT they're used in the "detail"
1070 * file and debug output, where we don't want to
1071 * see the operators.
1076 last = &(*last)->next;
1080 * And finally add in the attributes we're appending to
1081 * the tail of the "to" list.
1086 * Fix dumb cache issues
1089 fixup->username = NULL;
1090 fixup->password = NULL;
1092 for (vp = fixup->packet->vps; vp != NULL; vp = vp->next) {
1093 if (vp->da->vendor) continue;
1095 if ((vp->da->attr == PW_USER_NAME) && !fixup->username) {
1096 fixup->username = vp;
1098 } else if (vp->da->attr == PW_STRIPPED_USER_NAME) {
1099 fixup->username = vp;
1101 } else if (vp->da->attr == PW_USER_PASSWORD) {
1102 fixup->password = vp;
1107 rad_assert(request->packet != NULL);
1109 talloc_free(to_list);
1110 talloc_free(edited);