Move do_xlat=1 code to pairmake_xlat()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 29 Nov 2011 10:56:25 +0000 (11:56 +0100)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 29 Nov 2011 11:13:38 +0000 (12:13 +0100)
This abstracts the xlat code (i.e. integer type needs string)
so that it's easier to fix it later.

src/include/libradius.h
src/lib/valuepair.c
src/main/conffile.c
src/modules/rlm_ldap/rlm_ldap.c
src/modules/rlm_sql/sql.c

index 2a5033d..b3fa8da 100644 (file)
@@ -417,6 +417,7 @@ void                pairmove(VALUE_PAIR **to, VALUE_PAIR **from);
 void           pairmove2(VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor);
 VALUE_PAIR     *pairparsevalue(VALUE_PAIR *vp, const char *value);
 VALUE_PAIR     *pairmake(const char *attribute, const char *value, int operator);
+VALUE_PAIR *pairmake_xlat(const char *attribute, const char *value, int operator);
 VALUE_PAIR     *pairread(const char **ptr, FR_TOKEN *eol);
 FR_TOKEN       userparse(const char *buffer, VALUE_PAIR **first_pair);
 VALUE_PAIR     *readvp2(FILE *fp, int *pfiledone, const char *errprefix);
index b3eebc8..a2f1f00 100644 (file)
@@ -1435,8 +1435,6 @@ static VALUE_PAIR *pairmake_any(const char *attribute, const char *value,
         *      extended attribute of the "evs" data type.
         */
        if (*p == '.') {
-               DICT_ATTR *da;
-
                da = dict_attrbyvalue(attr, 0);
                if (!da) {
                        fr_strerror_printf("Cannot parse attributes without dictionaries");
@@ -1687,15 +1685,9 @@ VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator)
                        pairbasicfree(vp);
                        return NULL;
                }
-         
-               strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
-               vp->length = strlen(vp->vp_strvalue);
-               /*
-                *      If anything goes wrong, this is a run-time error,
-                *      not a compile-time error.
-                */
-               return vp;
 
+               pairbasicfree(vp);
+               return pairmake_xlat(attribute, value, operator);
        }
 
        /*
@@ -1714,6 +1706,24 @@ VALUE_PAIR *pairmake(const char *attribute, const char *value, int operator)
        return vp;
 }
 
+VALUE_PAIR *pairmake_xlat(const char *attribute, const char *value, int operator)
+{
+       VALUE_PAIR *vp;
+
+       if (!value) {
+               fr_strerror_printf("Empty value passed to pairmake_xlat()");
+               return NULL;
+       }
+
+       vp = pairmake(attribute, NULL, operator);
+       if (!vp) return vp;
+
+       strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
+       vp->flags.do_xlat = 1;
+       vp->length = 0;
+
+       return vp;
+}
 
 /*
  *     [a-zA-Z0-9_-:.]+
@@ -1842,15 +1852,12 @@ VALUE_PAIR *pairread(const char **ptr, FR_TOKEN *eol)
                                fr_strerror_printf("Value too long");
                                return NULL;
                        }
-                       vp = pairmake(attr, NULL, token);
+                       vp = pairmake_xlat(attr, value, token);
                        if (!vp) {
                                *eol = T_OP_INVALID;
                                return NULL;
                        }
 
-                       strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
-                       vp->flags.do_xlat = 1;
-                       vp->length = 0;
                } else {
                        /*
                         *      Parse && escape it, as defined by the
@@ -1900,15 +1907,11 @@ VALUE_PAIR *pairread(const char **ptr, FR_TOKEN *eol)
                        return NULL;
                }
 
-               vp = pairmake(attr, NULL, token);
+               vp = pairmake_xlat(attr, value, token);
                if (!vp) {
                        *eol = T_OP_INVALID;
                        return NULL;
                }
-
-               vp->flags.do_xlat = 1;
-               strlcpy(vp->vp_strvalue, value, sizeof(vp->vp_strvalue));
-               vp->length = 0;
                break;
        }
 
index 531ea9f..48960cd 100644 (file)
@@ -1989,8 +1989,6 @@ extern void fr_strerror_printf(const char *, ...);
  */
 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
 {
-       VALUE_PAIR *vp;
-
        if (!pair) {
                fr_strerror_printf("Internal error");
                return NULL;
@@ -2002,40 +2000,16 @@ VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
        }
 
        /*
-        *      pairmake handles tags.  pairalloc() doesn't.
-        */
-       vp = pairmake(pair->attr, NULL, pair->operator);
-       if (!vp) {
-               return NULL;
-       }
-
-       /*
-        *      Ignore the value if it's a false comparison.
+        *      FALSE comparisons never match.  BUT if it's a "string"
+        *      or `string`, then remember to expand it later.
         */
-       if (pair->operator == T_OP_CMP_FALSE) return vp;
-
-       if (pair->value_type == T_BARE_WORD) {
-               if ((vp->type == PW_TYPE_STRING) && 
-                   (pair->value[0] == '0') && (pair->value[1] == 'x')) {
-                       vp->type = PW_TYPE_OCTETS;
-               }
-               if (!pairparsevalue(vp, pair->value)) {
-                       pairfree(&vp);
-                       return NULL;
-               }
-               vp->flags.do_xlat = 0;
-         
-       } else if (pair->value_type == T_SINGLE_QUOTED_STRING) {
-               if (!pairparsevalue(vp, pair->value)) {
-                       pairfree(&vp);
-                       return NULL;
-               }
-               vp->flags.do_xlat = 0;
-       } else {
-               vp->flags.do_xlat = 1;
+       if ((pair->operator != T_OP_CMP_FALSE) &&
+           ((pair->value_type == T_DOUBLE_QUOTED_STRING) ||
+            (pair->value_type == T_BACK_QUOTED_STRING))) {
+               return pairmake_xlat(pair->attr, pair->value, pair->operator);
        }
 
-       return vp;
+       return pairmake(pair->attr, pair->value, pair->operator);
 }
 
 /*
index 3caa97c..6af1762 100644 (file)
@@ -2781,20 +2781,20 @@ static VALUE_PAIR *ldap_pairget(LDAP *ld, LDAPMessage *entry,
                                /*
                                 *      Create the pair.
                                 */
-                               newpair = pairmake(element->radius_attr,
-                                                  do_xlat ? NULL : value,
-                                                  operator);
+                               if (do_xlat) {
+                                       newpair = pairmake_xlat(element->radius_attr,
+                                                               value,
+                                                               operator);
+                               } else {
+                                       newpair = pairmake(element->radius_attr,
+                                                          value,
+                                                          operator);
+                               }
                                if (newpair == NULL) {
                                        radlog(L_ERR, "  [%s] Failed to create the pair: %s", inst->xlat_name, fr_strerror());
                                        continue;
                                }
 
-                               if (do_xlat) {
-                                       newpair->flags.do_xlat = 1;
-                                       strlcpy(newpair->vp_strvalue, buf,
-                                               sizeof(newpair->vp_strvalue));
-                                       newpair->length = 0;
-                               }
                                vp_prints(print_buffer, sizeof(print_buffer),
                                          newpair);
                                DEBUG("  [%s] %s -> %s", inst->xlat_name,
index 9943deb..5bfa662 100644 (file)
@@ -222,16 +222,15 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        /*
         *      Create the pair
         */
-       pair = pairmake(row[2], value, operator);
+       if (do_xlat) {
+               pair = pairmake_xlat(row[2], value, operator);
+       } else {
+               pair = pairmake(row[2], value, operator);
+       }
        if (pair == NULL) {
                radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", fr_strerror());
                return -1;
        }
-       if (do_xlat) {
-               pair->flags.do_xlat = 1;
-               strlcpy(pair->vp_strvalue, buf, sizeof(pair->vp_strvalue));
-               pair->length = 0;
-       }
 
        /*
         *      Add the pair into the packet