Allow IP addresses with /32 suffixes
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 15 Feb 2014 19:37:42 +0000 (19:37 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 15 Feb 2014 19:37:42 +0000 (19:37 +0000)
src/lib/valuepair.c
src/modules/rlm_sql/sql.c
src/tests/unit/condition.txt

index 8310956..3874415 100644 (file)
@@ -1285,10 +1285,9 @@ bool pairparsevalue(VALUE_PAIR *vp, char const *value)
                 *      cannot be resolved, or resolve later!
                 */
                p = NULL;
-               cs = value;
-
                {
                        fr_ipaddr_t ipaddr;
+                       char ipv4[16];
 
                        /*
                         *      Convert things which are obviously integers to IP addresses
@@ -1301,6 +1300,25 @@ bool pairparsevalue(VALUE_PAIR *vp, char const *value)
                                break;
                        }
 
+                       /*
+                        *      Certain applications/databases print IPv4 addresses with a
+                        *      /32 suffix. Strip it off if the mask is 32, else error out.
+                        */
+                       p = strchr(value, '/');
+                       if (p) {
+                               if ((p[1] != '3') || (p[2] != '2')) {
+                                       fr_strerror_printf("Invalid IP address suffix \"%s\".  Only '/32' permitted "
+                                                          "for non-prefix types", p);
+                                       return false;
+                               }
+
+                               strlcpy(ipv4, value, sizeof(ipv4));
+                               ipv4[p - value] = '\0';
+                               cs = ipv4;
+                       } else {
+                               cs = value;
+                       }
+
                        if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
                                fr_strerror_printf("Failed to find IP address for %s", cs);
                                return false;
index be654eb..cede609 100644 (file)
@@ -255,7 +255,7 @@ int sql_userparse(TALLOC_CTX *ctx, VALUE_PAIR **head, rlm_sql_row_t row)
                }
        } else {
                if (!pairparsevalue(vp, value)) {
-                       ERROR("rlm_sql: Error parsing value");
+                       ERROR("rlm_sql: Error parsing value: %s", fr_strerror());
 
                        talloc_free(vp);
                        return -1;
@@ -422,7 +422,7 @@ int sql_getvpdata(rlm_sql_t * inst, rlm_sql_handle_t **handle,
                if (!row)
                        break;
                if (sql_userparse(ctx, pair, row) != 0) {
-                       ERROR("rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
+                       ERROR("rlm_sql (%s): Error parsing user data from database result", inst->config->xlat_name);
 
                        (inst->module->sql_finish_select_query)(*handle, inst->config);
 
index f80957e..675ebf8 100644 (file)
@@ -334,3 +334,7 @@ data &User-Name == "%{sql: blah}"
 
 condition <ipaddr>127.0.0.1 == 2130706433
 data true
+
+# /32 suffix should be trimmed for this type
+condition <ipaddr>127.0.0.1/32 == 127.0.0.1
+data true