Add urlunquote
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 8 Jun 2014 08:19:34 +0000 (09:19 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 8 Jun 2014 08:29:48 +0000 (09:29 +0100)
src/modules/rlm_expr/rlm_expr.c
src/tests/keywords/urlquote

index b147966..2ef158b 100644 (file)
@@ -481,6 +481,43 @@ static ssize_t urlquote_xlat(UNUSED void *instance, UNUSED REQUEST *request,
        return outlen - freespace;
 }
 
+/** URLencode special characters
+ *
+ * Example: "%{urlunquote:http%3A%47%47example.org%47}" == "http://example.org/"
+ */
+static ssize_t urlunquote_xlat(UNUSED void *instance, UNUSED REQUEST *request,
+                              char const *fmt, char *out, size_t outlen)
+{
+       static char const *hextab = "0123456789abcdef";
+
+       char const *p;
+       char *c1, *c2;
+       size_t  freespace = outlen;
+
+       if (outlen <= 1) return 0;
+
+       p = fmt;
+       while (*p && (--freespace > 0)) {
+               if (*p != '%') {
+                       *out++ = *p++;
+                       continue;
+               }
+               /* Is a % char */
+
+               if (!*p || !(c1 = memchr(hextab, tolower(*++p), 16)) ||
+                   !*p || !(c2 = memchr(hextab, tolower(*++p), 16))) {
+                       REMARKER(fmt, p - fmt, "None hex char in % sequence");
+                       return -1;
+               }
+               p++;
+               *out++ = ((c1 - hextab) << 4) + (c2 - hextab);
+       }
+
+       *out = '\0';
+
+       return outlen - freespace;
+}
+
 /** Equivalent to the old safe_characters functionality in rlm_sql
  *
  * @verbatim Example: "%{escape:<img>foo.jpg</img>}" == "=60img=62foo.jpg=60/img=62" @endverbatim
@@ -890,6 +927,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        xlat_register("rand", rand_xlat, NULL, inst);
        xlat_register("randstr", randstr_xlat, NULL, inst);
        xlat_register("urlquote", urlquote_xlat, NULL, inst);
+       xlat_register("urlunquote", urlunquote_xlat, NULL, inst);
        xlat_register("escape", escape_xlat, NULL, inst);
        xlat_register("tolower", lc_xlat, NULL, inst);
        xlat_register("toupper", uc_xlat, NULL, inst);
index 4a14723..678e5fe 100644 (file)
@@ -6,6 +6,8 @@ update {
        request:Tmp-String-0 := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~'
        request:Tmp-String-1 := '±§!@#$%^&*()+={[}]:;"'|\<,>?/`'
        request:Tmp-String-2 := '™œ¥¤'
+       request:Tmp-String-3 := '%C2%B1%C2%A7%21%40%23%24%25%5E%26%2A%28%29%2B%3D%7B%5B%7D%5D%3A%3B%5C%22'
+       request:Tmp-String-4 := '%E2%84%A2%C5%93%C2%A5%C2%A4'
        reply:Filter-ID := 'filter'
 }
 
@@ -16,14 +18,32 @@ if ("%{urlquote:%{request:Tmp-String-0}}" != '0123456789ABCDEFGHIJKLMNOPQRSTUVWX
        }
 }
 
-if ("%{urlquote:%{request:Tmp-String-1}}" != '%C2%B1%C2%A7%21%40%23%24%25%5E%26%2A%28%29%2B%3D%7B%5B%7D%5D%3A%3B%5C%22') {
+if (<string>"%{urlquote:%{request:Tmp-String-1}}" != &Tmp-String-3) {
        update reply {
                Filter-Id += 'Fail 2'
        }
 }
 
-if ("%{urlquote:%{request:Tmp-String-2}}" != '%E2%84%A2%C5%93%C2%A5%C2%A4') {
+if (<string>"%{urlquote:%{request:Tmp-String-2}}" != &Tmp-String-4) {
        update reply {
                Filter-Id += 'Fail 3'
        }
 }
+
+if (<string>"%{urlunquote:%{request:Tmp-String-0}}" != &Tmp-String-0) {
+       update reply {
+               Filter-Id += 'Fail 4'
+       }
+}
+
+if (<string>"%{urlunquote:%{request:Tmp-String-3}}" != &Tmp-String-1) {
+       update reply {
+               Filter-Id += 'Fail 5'
+       }
+}
+
+if (<string>"%{urlunquote:%{request:Tmp-String-4}}" != &Tmp-String-2) {
+       update reply {
+               Filter-Id += 'Fail 6'
+       }
+}