Fix condition for attribute not found
[freeradius.git] / src / main / evaluate.c
index 467a6d6..77df822 100644 (file)
@@ -162,7 +162,7 @@ static FR_TOKEN getregex(const char **ptr, char *buffer, size_t buflen,
 
                        default:
                                if ((p[1] >= '0') && (p[1] <= '9') &&
-                                   (sscanf(p, "%3o", &x) == 1)) {
+                                   (sscanf(p + 1, "%3o", &x) == 1)) {
                                        *q++ = x;
                                        p += 2;
                                } else {
@@ -199,8 +199,7 @@ static const FR_NAME_NUMBER modreturn_table[] = {
 };
 
 
-static int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p)
-                       
+int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p)
 {
        const char *vp_name = name;
        REQUEST *myrequest = request;
@@ -290,7 +289,7 @@ static int radius_get_vp(REQUEST *request, const char *name, VALUE_PAIR **vp_p)
        /*
         *      May not may not be found, but it *is* a known name.
         */
-       *vp_p = pairfind(vps, da->attr);
+       *vp_p = pairfind(vps, da->attr, da->vendor);
        return TRUE;
 }
 
@@ -304,7 +303,7 @@ static int radius_do_cmp(REQUEST *request, int *presult,
                         int cflags, int modreturn)
 {
        int result;
-       int lint, rint;
+       uint32_t lint, rint;
        VALUE_PAIR *vp = NULL;
 #ifdef HAVE_REGEX_H
        char buffer[1024];
@@ -356,17 +355,19 @@ static int radius_do_cmp(REQUEST *request, int *presult,
                                 *      If so, try looking for it.
                                 */
                                da = dict_attrbyname(pleft);
-                               if (da && radius_find_compare(da->attr)) {
+                               if (da && (da->vendor == 0) && radius_find_compare(da->attr)) {
                                        VALUE_PAIR *check = pairmake(pleft, pright, token);
                                        *presult = (radius_callback_compare(request, NULL, check, NULL, NULL) == 0);
+                                       RDEBUG3("  Callback returns %d",
+                                               *presult);
                                        pairfree(&check);
-                                       if (*presult)  return TRUE;
-                                       return FALSE;
+                                       return TRUE;
                                }
                                
                                RDEBUG2("    (Attribute %s was not found)",
                                       pleft);
-                               return FALSE;
+                               *presult = 0;
+                               return TRUE;
                        }
 
 #ifdef HAVE_REGEX_H
@@ -391,6 +392,7 @@ static int radius_do_cmp(REQUEST *request, int *presult,
 
                        myvp.operator = token;
                        *presult = paircmp(&myvp, vp);
+                       RDEBUG3("  paircmp -> %d", *presult);
                        return TRUE;
                } /* else it's not a VP in a list */
        }
@@ -407,12 +409,12 @@ static int radius_do_cmp(REQUEST *request, int *presult,
                        RDEBUG2("    (Right field is not a number at: %s)", pright);
                        return FALSE;
                }
-               rint = atoi(pright);
+               rint = strtoul(pright, NULL, 0);
                if (!all_digits(pleft)) {
                        RDEBUG2("    (Left field is not a number at: %s)", pleft);
                        return FALSE;
                }
-               lint = atoi(pleft);
+               lint = strtoul(pleft, NULL, 0);
                break;
                
        default:
@@ -426,7 +428,7 @@ static int radius_do_cmp(REQUEST *request, int *presult,
                 *      Check for truth or falsehood.
                 */
                if (all_digits(pleft)) {
-                       lint = atoi(pleft);
+                       lint = strtoul(pleft, NULL, 0);
                        result = (lint != 0);
                        
                } else {
@@ -586,8 +588,14 @@ int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
                 *      ! EXPR
                 */
                if (!found_condition && (*p == '!')) {
-                       RDEBUG4(">>> INVERT");
-                       invert = TRUE;
+                       /*
+                        *      Don't change the results if we're not
+                        *      evaluating the condition.
+                        */
+                       if (evaluate_next_condition) {
+                               RDEBUG4(">>> INVERT");
+                               invert = TRUE;
+                       }
                        p++;
 
                        while ((*p == ' ') || (*p == '\t')) p++;
@@ -612,13 +620,13 @@ int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
                        }
 
                        if (invert) {
-                               if (evaluate_next_condition)
-                               RDEBUG2("%.*s Converting !%s -> %s",
-                                      depth, filler,
-                                      (result != FALSE) ? "TRUE" : "FALSE",
-                                      (result == FALSE) ? "TRUE" : "FALSE");
-
-                               result = (result == FALSE);
+                               if (evaluate_next_condition) {
+                                       RDEBUG2("%.*s Converting !%s -> %s",
+                                               depth, filler,
+                                               (result != FALSE) ? "TRUE" : "FALSE",
+                                               (result == FALSE) ? "TRUE" : "FALSE");
+                                       result = (result == FALSE);
+                               }
                                invert = FALSE;
                        }
 
@@ -767,8 +775,7 @@ int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
                 */
                token = gettoken(&p, comp, sizeof(comp));
                if ((token < T_OP_NE) || (token > T_OP_CMP_EQ) ||
-                   (token == T_OP_CMP_TRUE) ||
-                   (token == T_OP_CMP_FALSE)) {
+                   (token == T_OP_CMP_TRUE)) {
                        radlog(L_ERR, "Expected comparison at: %s", comp);
                        return FALSE;
                }
@@ -827,6 +834,7 @@ int radius_evaluate_condition(REQUEST *request, int modreturn, int depth,
                                           rt, pright, cflags, modreturn)) {
                                return FALSE;
                        }
+                       RDEBUG4(">>> Comparison returned %d", result);
 
                        if (invert) {
                                RDEBUG4(">>> INVERTING result");
@@ -972,7 +980,7 @@ void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from)
 
                found = FALSE;
                for (j = 0; j < to_count; j++) {
-                       if (edited[j] || !to_list[j]) continue;
+                       if (edited[j] || !to_list[j] || !from_list[i]) continue;
 
                        /*
                         *      Attributes aren't the same, skip them.
@@ -1014,6 +1022,14 @@ void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from)
                        }
 
                        /*
+                        *      Delete every attribute, independent
+                        *      of its value.
+                        */
+                       if (from_list[i]->operator == T_OP_CMP_FALSE) {
+                               goto delete;
+                       }
+
+                       /*
                         *      Delete all matching attributes from
                         *      "to"
                         */
@@ -1147,6 +1163,9 @@ void radius_pairmove(REQUEST *request, VALUE_PAIR **to, VALUE_PAIR *from)
                last = &(*last)->next;
        }
 
+       rad_assert(request != NULL);
+       rad_assert(request->packet != NULL);
+
        /*
         *      Fix dumb cache issues
         */
@@ -1298,6 +1317,12 @@ int radius_update_attrlist(REQUEST *request, CONF_SECTION *cs,
 
                cp = cf_itemtopair(ci);
 
+#ifndef NDEBUG
+               if (debug_flag && radius_find_compare(vp->attribute)) {
+                       DEBUG("WARNING: You are modifying the value of virtual attribute %s.  This is not supported.", vp->name);
+               }
+#endif
+
                /*
                 *      The VP && CF lists should be in sync.  If they're
                 *      not, panic.