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 rad_assert(lhs_type == PW_TYPE_STRING);
168 rad_assert(lhs != NULL);
170 EVAL_DEBUG("CMP WITH REGEX %s %s",
171 map->rhs->tmpl_iflag ? "CASE INSENSITIVE" : "CASE SENSITIVE",
172 map->rhs->tmpl_mflag ? "MULTILINE" : "SINGLELINE");
174 switch (map->rhs->type) {
175 case TMPL_TYPE_REGEX_STRUCT: /* pre-compiled to a regex */
176 preg = map->rhs->tmpl_preg;
180 rad_assert(rhs_type == PW_TYPE_STRING);
181 rad_assert(rhs->strvalue);
182 slen = regex_compile(request, &rreg, rhs->strvalue, rhs_len,
183 map->rhs->tmpl_iflag, map->rhs->tmpl_mflag, true, true);
185 REMARKER(rhs->strvalue, -slen, fr_strerror());
186 EVAL_DEBUG("FAIL %d", __LINE__);
194 ret = regex_exec(preg, lhs->strvalue, lhs_len, rxmatch, &nmatch);
197 EVAL_DEBUG("CLEARING SUBCAPTURES");
198 regex_sub_to_request(request, NULL, NULL, 0, NULL, 0); /* clear out old entries */
202 EVAL_DEBUG("SETTING SUBCAPTURES");
203 regex_sub_to_request(request, &preg, lhs->strvalue, lhs_len, rxmatch, nmatch);
207 EVAL_DEBUG("REGEX ERROR");
208 REDEBUG("regex failed: %s", fr_strerror());
215 if (preg) talloc_free(rreg);
221 #ifdef WITH_EVAL_DEBUG
222 static void cond_print_operands(REQUEST *request,
223 PW_TYPE lhs_type, value_data_t const *lhs, size_t lhs_len,
224 PW_TYPE rhs_type, value_data_t const *rhs, size_t rhs_len)
227 if (lhs_type == PW_TYPE_STRING) {
228 EVAL_DEBUG("LHS: \"%s\" (%zu)" , lhs->strvalue, lhs_len);
232 lhs_hex = talloc_array(request, char, (lhs_len * 2) + 1);
234 if (lhs_type == PW_TYPE_OCTETS) {
235 fr_bin2hex(lhs_hex, lhs->octets, lhs_len);
237 fr_bin2hex(lhs_hex, (uint8_t const *)lhs, lhs_len);
240 EVAL_DEBUG("LHS: 0x%s (%zu)", lhs_hex, lhs_len);
242 talloc_free(lhs_hex);
245 EVAL_DEBUG("LHS: VIRTUAL");
249 if (rhs_type == PW_TYPE_STRING) {
250 EVAL_DEBUG("RHS: \"%s\" (%zu)" , rhs->strvalue, rhs_len);
254 rhs_hex = talloc_array(request, char, (rhs_len * 2) + 1);
256 if (rhs_type == PW_TYPE_OCTETS) {
257 fr_bin2hex(rhs_hex, rhs->octets, rhs_len);
259 fr_bin2hex(rhs_hex, (uint8_t const *)rhs, rhs_len);
262 EVAL_DEBUG("RHS: 0x%s (%zu)", rhs_hex, rhs_len);
264 talloc_free(rhs_hex);
267 EVAL_DEBUG("RHS: COMPILED");
272 /** Call the correct data comparison function for the condition
274 * Deals with regular expression comparisons, virtual attribute
275 * comparisons, and data comparisons.
277 * @return -1 on error, 0 for "no match", 1 for "match".
279 static int cond_cmp_values(REQUEST *request, fr_cond_t const *c,
280 PW_TYPE lhs_type, value_data_t const *lhs, size_t lhs_len,
281 PW_TYPE rhs_type, value_data_t const *rhs, size_t rhs_len)
283 vp_map_t const *map = c->data.map;
286 #ifdef WITH_EVAL_DEBUG
287 EVAL_DEBUG("CMP OPERANDS");
288 cond_print_operands(request, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
295 if (map->op == T_OP_REG_EQ) {
296 rcode = cond_do_regex(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
301 * Virtual attribute comparison.
303 if (c->pass2_fixup == PASS2_PAIRCOMPARE) {
306 EVAL_DEBUG("CMP WITH PAIRCOMPARE");
307 rad_assert(map->lhs->type == TMPL_TYPE_ATTR);
309 vp = fr_pair_afrom_da(request, map->lhs->tmpl_da);
310 vp->op = c->data.map->op;
312 value_data_copy(vp, &vp->data, rhs_type, rhs, rhs_len);
313 vp->vp_length = rhs_len;
315 rcode = paircompare(request, request->packet->vps, vp, NULL);
316 rcode = (rcode == 0) ? 1 : 0;
322 * At this point both operands should have been normalised
323 * to the same type, and there's no special comparisons
326 rad_assert(lhs_type == rhs_type);
328 EVAL_DEBUG("CMP WITH VALUE DATA");
329 rcode = value_data_cmp_op(map->op, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
341 EVAL_DEBUG("ERROR %i", rcode);
350 /** Convert both operands to the same type
352 * If casting is successful, we call cond_cmp_values to do the comparison
354 * @return -1 on error, 0 for "no match", 1 for "match".
356 static int cond_normalise_and_cmp(REQUEST *request, fr_cond_t const *c,
357 PW_TYPE lhs_type, DICT_ATTR const *lhs_enumv,
358 value_data_t const *lhs, size_t lhs_len)
360 vp_map_t const *map = c->data.map;
362 DICT_ATTR const *cast = NULL;
363 PW_TYPE cast_type = PW_TYPE_INVALID;
367 PW_TYPE rhs_type = PW_TYPE_INVALID;
368 DICT_ATTR const *rhs_enumv = NULL;
369 value_data_t const *rhs = NULL;
372 value_data_t lhs_cast, rhs_cast;
373 void *lhs_cast_buff = NULL, *rhs_cast_buff = NULL;
376 * Cast operand to correct type.
378 * With hack for strings that look like integers, to cast them
379 * to 64 bit unsigned integers.
381 * @fixme For things like this it'd be useful to have a 64bit signed type.
385 if ((cast_type != PW_TYPE_INVALID) && (_s ## _type != PW_TYPE_INVALID) && (cast_type != _s ## _type)) {\
387 EVAL_DEBUG("CASTING " #_s " FROM %s TO %s",\
388 fr_int2str(dict_attr_types, _s ## _type, "<INVALID>"),\
389 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));\
390 r = value_data_cast(request, &_s ## _cast, cast_type, cast, _s ## _type, _s ## _enumv, _s, _s ## _len);\
392 REDEBUG("Failed casting " #_s " operand: %s", fr_strerror());\
396 if (cast && cast->flags.is_pointer) _s ## _cast_buff = _s ## _cast.ptr;\
397 _s ## _type = cast_type;\
398 _s ## _len = (size_t)r;\
403 #define CHECK_INT_CAST(_l, _r) \
405 if ((cast_type == PW_TYPE_INVALID) &&\
406 _l && (_l ## _type == PW_TYPE_STRING) &&\
407 _r && (_r ## _type == PW_TYPE_STRING) &&\
408 all_digits(lhs->strvalue) && all_digits(rhs->strvalue)) {\
409 cast_type = PW_TYPE_INTEGER64;\
410 EVAL_DEBUG("OPERANDS ARE NUMBER STRINGS, SETTING CAST TO integer64");\
415 * Regular expressions need both operands to be strings
418 if (map->op == T_OP_REG_EQ) cast_type = PW_TYPE_STRING;
422 * If it's a pair comparison, data gets cast to the
423 * type of the pair comparison attribute.
425 * Magic attribute is always the LHS.
427 if (c->pass2_fixup == PASS2_PAIRCOMPARE) {
428 rad_assert(!c->cast);
429 rad_assert(map->lhs->type == TMPL_TYPE_ATTR);
431 /* expensive assert */
432 rad_assert((map->rhs->type != TMPL_TYPE_ATTR) || !radius_find_compare(map->rhs->tmpl_da));
434 cast = map->lhs->tmpl_da;
435 cast_type = cast->type;
437 EVAL_DEBUG("NORMALISATION TYPE %s (PAIRCMP TYPE)",
438 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
440 * Otherwise we use the explicit cast, or implicit
441 * cast (from an attribute reference).
442 * We already have the data for the lhs, so we convert
445 } else if (c->cast) {
447 EVAL_DEBUG("NORMALISATION TYPE %s (EXPLICIT CAST)",
448 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
449 } else if (map->lhs->type == TMPL_TYPE_ATTR) {
450 cast = map->lhs->tmpl_da;
451 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM LHS REF)",
452 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
453 } else if (map->rhs->type == TMPL_TYPE_ATTR) {
454 cast = map->rhs->tmpl_da;
455 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM RHS REF)",
456 fr_int2str(dict_attr_types, cast->type, "<INVALID>"));
457 } else if (map->lhs->type == TMPL_TYPE_DATA) {
458 cast_type = map->lhs->tmpl_data_type;
459 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM LHS DATA)",
460 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));
461 } else if (map->rhs->type == TMPL_TYPE_DATA) {
462 cast_type = map->rhs->tmpl_data_type;
463 EVAL_DEBUG("NORMALISATION TYPE %s (IMPLICIT FROM RHS DATA)",
464 fr_int2str(dict_attr_types, cast_type, "<INVALID>"));
467 if (cast) cast_type = cast->type;
469 switch (map->rhs->type) {
475 for (vp = tmpl_cursor_init(&rcode, &cursor, request, map->rhs);
477 vp = tmpl_cursor_next(&cursor, map->rhs)) {
478 rhs_type = vp->da->type;
481 rhs_len = vp->vp_length;
483 CHECK_INT_CAST(lhs, rhs);
487 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
488 if (rcode != 0) break;
490 TALLOC_FREE(rhs_cast_buff);
496 rhs_type = map->rhs->tmpl_data_type;
497 rhs = &map->rhs->tmpl_data_value;
498 rhs_len = map->rhs->tmpl_data_length;
500 CHECK_INT_CAST(lhs, rhs);
504 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
508 * Expanded types start as strings, then get converted
509 * to the type of the attribute or the explicit cast.
511 case TMPL_TYPE_LITERAL:
514 case TMPL_TYPE_XLAT_STRUCT:
519 if (map->rhs->type != TMPL_TYPE_LITERAL) {
522 ret = tmpl_aexpand(request, &p, request, map->rhs, NULL, NULL);
524 EVAL_DEBUG("FAIL [%i]", __LINE__);
532 data.strvalue = map->rhs->name;
533 rhs_len = map->rhs->len;
535 rad_assert(data.strvalue);
537 rhs_type = PW_TYPE_STRING;
540 CHECK_INT_CAST(lhs, rhs);
544 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, rhs_type, rhs, rhs_len);
545 if (map->rhs->type != TMPL_TYPE_LITERAL)talloc_free(data.ptr);
551 * RHS is a compiled regex, we don't need to do anything with it.
553 case TMPL_TYPE_REGEX_STRUCT:
555 rcode = cond_cmp_values(request, c, lhs_type, lhs, lhs_len, PW_TYPE_INVALID, NULL, 0);
558 * Unsupported types (should have been parse errors)
562 case TMPL_TYPE_UNKNOWN:
563 case TMPL_TYPE_ATTR_UNDEFINED:
564 case TMPL_TYPE_REGEX: /* Should now be a TMPL_TYPE_REGEX_STRUCT or TMPL_TYPE_XLAT_STRUCT */
571 talloc_free(lhs_cast_buff);
572 talloc_free(rhs_cast_buff);
579 * @param[in] request the REQUEST
580 * @param[in] modreturn the previous module return code
581 * @param[in] depth of the recursion (only used for debugging)
582 * @param[in] c the condition to evaluate
583 * @return -1 on error, 0 for "no match", 1 for "match".
585 int radius_evaluate_map(REQUEST *request, UNUSED int modreturn, UNUSED int depth, fr_cond_t const *c)
589 vp_map_t const *map = c->data.map;
591 EVAL_DEBUG(">>> MAP TYPES LHS: %s, RHS: %s",
592 fr_int2str(tmpl_names, map->lhs->type, "???"),
593 fr_int2str(tmpl_names, map->rhs->type, "???"));
595 switch (map->lhs->type) {
597 * LHS is an attribute or list
605 * Legacy paircompare call, skip processing the magic attribute
606 * if it's the LHS and cast RHS to the same type.
608 if ((c->pass2_fixup == PASS2_PAIRCOMPARE) && (map->op != T_OP_REG_EQ)) {
610 rad_assert(radius_find_compare(map->lhs->tmpl_da)); /* expensive assert */
612 rcode = cond_normalise_and_cmp(request, c, PW_TYPE_INVALID, NULL, NULL, 0);
615 for (vp = tmpl_cursor_init(&rcode, &cursor, request, map->lhs);
617 vp = tmpl_cursor_next(&cursor, map->lhs)) {
619 * Evaluate all LHS values, condition evaluates to true
620 * if we get at least one set of operands that
623 rcode = cond_normalise_and_cmp(request, c, vp->da->type, vp->da, &vp->data, vp->vp_length);
624 if (rcode != 0) break;
630 rcode = cond_normalise_and_cmp(request, c,
631 map->lhs->tmpl_data_type, NULL, &map->lhs->tmpl_data_value,
632 map->lhs->tmpl_data_length);
635 case TMPL_TYPE_LITERAL:
638 case TMPL_TYPE_XLAT_STRUCT:
643 if (map->lhs->type != TMPL_TYPE_LITERAL) {
646 ret = tmpl_aexpand(request, &p, request, map->lhs, NULL, NULL);
648 EVAL_DEBUG("FAIL [%i]", __LINE__);
653 data.strvalue = map->lhs->name;
656 rad_assert(data.strvalue);
658 rcode = cond_normalise_and_cmp(request, c, PW_TYPE_STRING, NULL, &data, ret);
659 if (map->lhs->type != TMPL_TYPE_LITERAL) talloc_free(data.ptr);
664 * Unsupported types (should have been parse errors)
667 case TMPL_TYPE_ATTR_UNDEFINED:
668 case TMPL_TYPE_UNKNOWN:
669 case TMPL_TYPE_REGEX: /* should now be a TMPL_TYPE_REGEX_STRUCT or TMPL_TYPE_XLAT_STRUCT */
670 case TMPL_TYPE_REGEX_STRUCT: /* not allowed as LHS */
681 /** Evaluate a fr_cond_t;
683 * @param[in] request the REQUEST
684 * @param[in] modreturn the previous module return code
685 * @param[in] depth of the recursion (only used for debugging)
686 * @param[in] c the condition to evaluate
687 * @return -1 on failure, -2 on attribute not found, 0 for "no match", 1 for "match".
689 int radius_evaluate_cond(REQUEST *request, int modreturn, int depth, fr_cond_t const *c)
692 #ifdef WITH_EVAL_DEBUG
695 fr_cond_sprint(buffer, sizeof(buffer), c);
696 EVAL_DEBUG("%s", buffer);
701 case COND_TYPE_EXISTS:
702 rcode = radius_evaluate_tmpl(request, modreturn, depth, c->data.vpt);
703 /* Existence checks are special, because we expect them to fail */
704 if (rcode < 0) rcode = 0;
708 rcode = radius_evaluate_map(request, modreturn, depth, c);
711 case COND_TYPE_CHILD:
712 rcode = radius_evaluate_cond(request, modreturn, depth + 1, c->data.child);
719 case COND_TYPE_FALSE:
723 EVAL_DEBUG("FAIL %d", __LINE__);
727 if (rcode < 0) return rcode;
729 if (c->negate) rcode = !rcode;
734 * FALSE && ... = FALSE
736 if (!rcode && (c->next_op == COND_AND)) return false;
741 if (rcode && (c->next_op == COND_OR)) return true;
747 EVAL_DEBUG("FAIL %d", __LINE__);
755 * The fr_pair_list_move() function in src/lib/valuepair.c does all sorts of
756 * extra magic that we don't want here.
758 * FIXME: integrate this with the code calling it, so that we
759 * only fr_pair_list_copy() those attributes that we're really going to
762 void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from, bool do_xlat)
764 int i, j, count, from_count, to_count, tailto;
766 VALUE_PAIR *vp, *next, **last;
767 VALUE_PAIR **from_list, **to_list;
768 VALUE_PAIR *append, **append_tail;
771 REQUEST *fixup = NULL;
775 * Set up arrays for editing, to remove some of the
776 * O(N^2) dependencies. This also makes it easier to
777 * insert and remove attributes.
779 * It also means that the operators apply ONLY to the
780 * attributes in the original list. With the previous
781 * implementation of fr_pair_list_move(), adding two attributes
782 * via "+=" and then "=" would mean that the second one
783 * wasn't added, because of the existence of the first
784 * one in the "to" list. This implementation doesn't
787 * Also, the previous implementation did NOT implement
788 * "-=" correctly. If two of the same attributes existed
789 * in the "to" list, and you tried to subtract something
790 * matching the *second* value, then the fr_pair_delete_by_num()
791 * function was called, and the *all* attributes of that
792 * number were deleted. With this implementation, only
793 * the matching attributes are deleted.
796 for (vp = fr_cursor_init(&cursor, &from); vp; vp = fr_cursor_next(&cursor)) count++;
797 from_list = talloc_array(request, VALUE_PAIR *, count);
799 for (vp = fr_cursor_init(&cursor, to); vp; vp = fr_cursor_next(&cursor)) count++;
800 to_list = talloc_array(request, VALUE_PAIR *, count);
803 append_tail = &append;
806 * Move the lists to the arrays, and break the list
810 for (vp = from; vp != NULL; vp = next) {
812 from_list[from_count++] = vp;
817 ctx = talloc_parent(*to);
818 to_copy = fr_pair_list_copy(ctx, *to);
819 for (vp = to_copy; vp != NULL; vp = next) {
821 to_list[to_count++] = vp;
825 edited = talloc_zero_array(request, bool, to_count);
827 RDEBUG4("::: FROM %d TO %d MAX %d", from_count, to_count, count);
830 * Now that we have the lists initialized, start working
833 for (i = 0; i < from_count; i++) {
836 RDEBUG4("::: Examining %s", from_list[i]->da->name);
838 if (do_xlat) radius_xlat_do(request, from_list[i]);
841 * Attribute should be appended, OR the "to" list
842 * is empty, and we're supposed to replace or
843 * "add if not existing".
845 if (from_list[i]->op == T_OP_ADD) goto do_append;
848 for (j = 0; j < to_count; j++) {
849 if (edited[j] || !to_list[j] || !from_list[i]) continue;
852 * Attributes aren't the same, skip them.
854 if (from_list[i]->da != to_list[j]->da) {
859 * We don't use a "switch" statement here
860 * because we want to break out of the
861 * "for" loop over 'j' in most cases.
865 * Over-write the FIRST instance of the
866 * matching attribute name. We free the
867 * one in the "to" list, and move over
868 * the one in the "from" list.
870 if (from_list[i]->op == T_OP_SET) {
871 RDEBUG4("::: OVERWRITING %s FROM %d TO %d",
872 to_list[j]->da->name, i, j);
873 fr_pair_list_free(&to_list[j]);
874 to_list[j] = from_list[i];
881 * Add the attribute only if it does not
882 * exist... but it exists, so we stop
885 if (from_list[i]->op == T_OP_EQ) {
891 * Delete every attribute, independent
894 if (from_list[i]->op == T_OP_CMP_FALSE) {
899 * Delete all matching attributes from
902 if ((from_list[i]->op == T_OP_SUB) ||
903 (from_list[i]->op == T_OP_CMP_EQ) ||
904 (from_list[i]->op == T_OP_LE) ||
905 (from_list[i]->op == T_OP_GE)) {
907 int old_op = from_list[i]->op;
910 * Check for equality.
912 from_list[i]->op = T_OP_CMP_EQ;
915 * If equal, delete the one in
918 rcode = radius_compare_vps(NULL, from_list[i],
921 * We may want to do more
922 * subtractions, so we re-set the
923 * operator back to it's original
926 from_list[i]->op = old_op;
930 if (rcode != 0) goto delete;
936 RDEBUG4("::: DELETING %s FROM %d TO %d",
937 from_list[i]->da->name, i, j);
938 fr_pair_list_free(&to_list[j]);
944 * Enforce <=. If it's
949 RDEBUG4("::: REPLACING %s FROM %d TO %d",
950 from_list[i]->da->name, i, j);
951 fr_pair_list_free(&to_list[j]);
952 to_list[j] = from_list[i];
960 RDEBUG4("::: REPLACING %s FROM %d TO %d",
961 from_list[i]->da->name, i, j);
962 fr_pair_list_free(&to_list[j]);
963 to_list[j] = from_list[i];
973 rad_assert(0 == 1); /* panic! */
977 * We were asked to add it if it didn't exist,
978 * and it doesn't exist. Move it over to the
979 * tail of the "to" list, UNLESS it was already
980 * moved by another operator.
982 if (!found && from_list[i]) {
983 if ((from_list[i]->op == T_OP_EQ) ||
984 (from_list[i]->op == T_OP_LE) ||
985 (from_list[i]->op == T_OP_GE) ||
986 (from_list[i]->op == T_OP_SET)) {
988 RDEBUG4("::: APPENDING %s FROM %d TO %d",
989 from_list[i]->da->name, i, tailto);
990 *append_tail = from_list[i];
991 from_list[i]->op = T_OP_EQ;
993 append_tail = &(*append_tail)->next;
999 * Delete attributes in the "from" list.
1001 for (i = 0; i < from_count; i++) {
1002 if (!from_list[i]) continue;
1004 fr_pair_list_free(&from_list[i]);
1006 talloc_free(from_list);
1008 RDEBUG4("::: TO in %d out %d", to_count, tailto);
1011 * Re-chain the "to" list.
1013 fr_pair_list_free(to);
1016 if (to == &request->packet->vps) {
1018 } else if (request->parent && (to == &request->parent->packet->vps)) {
1019 fixup = request->parent;
1022 for (i = 0; i < tailto; i++) {
1023 if (!to_list[i]) continue;
1026 RDEBUG4("::: to[%d] = %s", i, vp->da->name);
1029 * Mash the operator to a simple '='. The
1030 * operators in the "to" list aren't used for
1031 * anything. BUT they're used in the "detail"
1032 * file and debug output, where we don't want to
1033 * see the operators.
1038 last = &(*last)->next;
1042 * And finally add in the attributes we're appending to
1043 * the tail of the "to" list.
1048 * Fix dumb cache issues
1051 fixup->username = NULL;
1052 fixup->password = NULL;
1054 for (vp = fixup->packet->vps; vp != NULL; vp = vp->next) {
1055 if (vp->da->vendor) continue;
1057 if ((vp->da->attr == PW_USER_NAME) && !fixup->username) {
1058 fixup->username = vp;
1060 } else if (vp->da->attr == PW_STRIPPED_USER_NAME) {
1061 fixup->username = vp;
1063 } else if (vp->da->attr == PW_USER_PASSWORD) {
1064 fixup->password = vp;
1069 rad_assert(request->packet != NULL);
1071 talloc_free(to_list);
1072 talloc_free(edited);