Add support for extended attributes: draft-dekok-radext-radius-extensions
[freeradius.git] / src / lib / dict.c
index dfa7fb2..ff77e21 100644 (file)
@@ -99,6 +99,28 @@ static const FR_NAME_NUMBER type_table[] = {
 
 
 /*
+ *     WiMAX craziness.
+ */
+#define MAX_TLV_NEST (4)
+/*
+ *     Bit packing:
+ *     8 bits of base VSA
+ *     8 bits for nested TLV 1
+ *     8 bits for nested TLV 2
+ *     5 bits for nested TLV 3
+ *     3 bits for nested TLV 4
+ */
+const int fr_wimax_max_tlv = MAX_TLV_NEST;
+const int fr_wimax_shift[MAX_TLV_NEST + 1] = {
+  0, 8, 16, 24, 29
+};
+
+const int fr_wimax_mask[MAX_TLV_NEST + 1] = {
+  0, 0xff, 0xff, 0x1f, 0x07
+};
+
+
+/*
  *     Create the hash of the name.
  *
  *     We copy the hash function here because it's substantially faster.
@@ -429,8 +451,8 @@ int dict_addvendor(const char *name, int value)
        size_t length;
        DICT_VENDOR *dv;
 
-       if (value > 65535) {
-               fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
+       if (value > FR_MAX_VENDOR) {
+               fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
                return -1;
        }
 
@@ -524,12 +546,33 @@ int dict_addattr(const char *name, int attr, int vendor, int type,
                }
        }
 
+       /*
+        *      Additional checks for extended attributes.
+        */
+       if (flags.extended || flags.extended_flags) {
+               if (vendor != 0) {
+                       fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" attribute format.");
+                       return -1;
+               }
+               vendor = VENDORPEC_EXTENDED;
+
+               if ((attr < 256) && (type != PW_TYPE_OCTETS)) {
+                       fr_strerror_printf("dict_addattr: The base \"extended\" attribute definition MUST be of type \"octets\".");
+                       return -1;
+               }
+
+               if (flags.has_tag || flags.array || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
+                       fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
+                       return -1;
+               }
+       }
+
        if (attr < 0) {
                fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
                return -1;
        }
 
-       if (vendor) {
+       if (vendor && (vendor != VENDORPEC_EXTENDED)) {
                DICT_VENDOR *dv;
                static DICT_VENDOR *last_vendor = NULL;
 
@@ -565,7 +608,8 @@ int dict_addattr(const char *name, int attr, int vendor, int type,
                 *      If the vendor isn't defined, die.
                 */
                if (!dv) {
-                       fr_strerror_printf("dict_addattr: Unknown vendor");
+                       fr_strerror_printf("dict_addattr: Unknown vendor %d",
+                                          vendor);
                        return -1;
                }
 
@@ -593,7 +637,6 @@ int dict_addattr(const char *name, int attr, int vendor, int type,
        da->vendor = vendor;
        da->type = type;
        da->flags = flags;
-       da->vendor = vendor;
 
        /*
         *      Insert the attribute, only if it's not a duplicate.
@@ -810,7 +853,7 @@ static int sscanf_i(const char *str, int *pvalue)
 {
        int rcode = 0;
        int base = 10;
-       const char *tab = "0123456789";
+       static const char *tab = "0123456789";
 
        if ((str[0] == '0') &&
            ((str[1] == 'x') || (str[1] == 'X'))) {
@@ -846,7 +889,9 @@ static int process_attribute(const char* fn, const int line,
        int             vendor = 0;
        int             value;
        int             type;
+       int             length = 0;
        ATTR_FLAGS      flags;
+       char            *p;
 
        if ((argc < 3) || (argc > 4)) {
                fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
@@ -854,6 +899,14 @@ static int process_attribute(const char* fn, const int line,
                return -1;
        }
 
+       memset(&flags, 0, sizeof(flags));
+
+       /*
+        *      Look for extended attributes before doing anything else.
+        */
+       p = strchr(argv[1], '.');
+       if (p) *p = '\0';
+
        /*
         *      Validate all entries
         */
@@ -863,23 +916,182 @@ static int process_attribute(const char* fn, const int line,
        }
 
        /*
-        *      find the type of the attribute.
+        *      Parse extended attributes.
         */
-       type = fr_str2int(type_table, argv[2], -1);
-       if (type < 0) {
-               fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
-                       fn, line, argv[2]);
-               return -1;
+       if (p) {
+               int sub;
+               char *q;
+               DICT_ATTR *da;
+
+               *p = '.';       /* reset forlater printing */
+
+               /*
+                *      Does the parent attribute exist?
+                */
+               da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
+               if (!da) {
+                       fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
+                       return -1;
+               }
+
+               /*
+                *      241.1 means 241 is of type "extended".
+                *      Otherwise, die.
+                */
+               if (!da->flags.extended && !da->flags.extended_flags) {
+                       fr_strerror_printf("dict_init: %s[%d]: Entry refers to a non-extended attribute %d", fn, line, value);
+                       return -1;
+               }
+
+               /*
+                *      Look for sub-TLVs
+                */
+               q = strchr(p + 1, '.');
+               if (q) *q = '\0';
+
+               /*
+                *      Parse error.
+                */
+               if (!sscanf_i(p + 1, &sub)) {
+                       fr_strerror_printf("dict_init: %s[%d]: Parse error in value \"%s\"", fn, line, argv[1]);
+                       return -1;
+               }
+
+               /*
+                *      Value is out of bounds.
+                */
+               if ((sub == 0) || (sub > 255)) {
+                       fr_strerror_printf("dict_init: %s[%d]: Entry has value out of range 0..255: %d", fn, line, sub);
+                       return -1;
+               }
+
+               value |= (sub << fr_wimax_shift[1]);
+
+               /*
+                *      If this is defining the contents of a TLV,
+                *      look for the parent, and check it.
+                */
+               if (q) {
+                       DICT_ATTR *tlv;
+
+                       tlv = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
+                       if (!tlv || !tlv->flags.has_tlv ||
+                           (!tlv->flags.extended && !tlv->flags.extended_flags)) {
+                               fr_strerror_printf("dict_init: %s[%d]: Entry refers to Attribute \"%s\", which is not an extended attribute TLV", fn, line, argv[1]);
+                               return -1;
+
+                       }
+
+                       flags.is_tlv = 1;
+                       
+                       /*
+                        *      Parse error.
+                        */
+                       if (!sscanf_i(q + 1, &sub)) {
+                               fr_strerror_printf("dict_init: %s[%d]: Parse error in value \"%s\"", fn, line, argv[1]);
+                               return -1;
+                       }
+
+                       /*
+                        *      Value is out of bounds.
+                        */
+                       if ((sub == 0) || (sub > 255)) {
+                               fr_strerror_printf("dict_init: %s[%d]: Entry has value out of range 0..255: %d", fn, line, sub);
+                               return -1;
+                       }
+
+                       value |= (sub << fr_wimax_shift[2]);
+               }
+
+               /*
+                *      Set which type of attribute this is.
+                */
+               flags.extended = da->flags.extended;
+               flags.extended_flags = da->flags.extended_flags;
+       }
+
+       if (strncmp(argv[2], "octets[", 7) != 0) {
+               /*
+                *      find the type of the attribute.
+                */
+               type = fr_str2int(type_table, argv[2], -1);
+               if (type < 0) {
+                       fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
+                                          fn, line, argv[2]);
+                       return -1;
+               }
+       } else {
+               type = PW_TYPE_OCTETS;
+               
+               p = strchr(argv[2] + 7, ']');
+               if (!p) {
+                       fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
+                       return -1;
+               }
+
+               *p = 0;
+
+               if (!sscanf_i(argv[1], &length)) {
+                       fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
+                       return -1;
+               }
+
+               if ((length == 0) || (length > 253)) {
+                       fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
+                       return -1;
+               }
        }
 
        /*
         *      Only look up the vendor if the string
         *      is non-empty.
         */
-       memset(&flags, 0, sizeof(flags));
-       if (argc == 4) {
+       if (argc < 4) {
+               /*
+                *      Force "length" for data types of fixed length;
+                */
+               switch (type) {
+               case PW_TYPE_BYTE:
+                       length = 1;
+                       break;
+
+               case PW_TYPE_SHORT:
+                       length = 2;
+                       break;
+
+               case PW_TYPE_DATE:
+               case PW_TYPE_IPADDR:
+               case PW_TYPE_INTEGER:
+               case PW_TYPE_SIGNED:
+                       length = 4;
+                       break;
+
+               case PW_TYPE_ETHERNET:
+                       length = 6;
+                       break;
+
+               case PW_TYPE_IFID:
+                       length = 8;
+                       break;
+
+               case PW_TYPE_IPV6ADDR:
+                       length = 16;
+                       break;
+
+               default:
+                       break;
+               }
+
+               flags.length = length;
+
+       } else {                /* argc == 4: we have options */
                char *key, *next, *last;
 
+               if (length != 0) {
+                       fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
+                       return -1;
+               }
+
                key = argv[3];
                do {
                        next = strchr(key, ',');
@@ -903,7 +1115,7 @@ static int process_attribute(const char* fn, const int line,
                                        return -1;
                                }
                                
-                       } else if (strncmp(key, "array", 8) == 0) {
+                       } else if (strncmp(key, "array", 6) == 0) {
                                flags.array = 1;
                                
                                switch (type) {
@@ -928,6 +1140,23 @@ static int process_attribute(const char* fn, const int line,
                                   ((vendor = dict_vendorbyname(key)) !=0)) {
                                break;
 
+                       } else if (strncmp(key, "extended-flags", 15) == 0) {
+                               if (flags.extended) {
+                                       fr_strerror_printf( "dict_init: %s[%d] You cannot set two  \"extended\" flags.",
+                                                           fn, line);
+                                       return -1;
+                               }
+
+                               flags.extended_flags = 1;
+
+                       } else if (strncmp(key, "extended", 9) == 0) {
+                               if (flags.extended_flags) {
+                                       fr_strerror_printf( "dict_init: %s[%d] You cannot set two  \"extended\" flags.",
+                                                           fn, line);
+                                       return -1;
+                               }
+                               flags.extended = 1;
+
                        } else {
                                fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
                                            fn, line, key);
@@ -959,7 +1188,6 @@ static int process_attribute(const char* fn, const int line,
                                   fn, line,
                                   fr_int2str(type_table, type, "?Unknown?"));
                        return -1;
-
                }
        }
 
@@ -971,8 +1199,8 @@ static int process_attribute(const char* fn, const int line,
                /*
                 *      TLV's can be only one octet.
                 */
-               if ((value <= 0) || (value > 255)) {
-                       fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot have value > 255",
+         if ((value <= 0) || ((value & ~fr_wimax_mask[tlv_depth]) != 0)) {
+                       fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
                                    fn, line);
                        return -1;
                }
@@ -980,7 +1208,7 @@ static int process_attribute(const char* fn, const int line,
                /*
                 *      
                 */
-               value <<= (8 * tlv_depth);
+               value <<= fr_wimax_shift[tlv_depth];
                value |= block_tlv->attr;
                flags.is_tlv = 1;
        }
@@ -1259,7 +1487,7 @@ static int str2argv(char *str, char **argv, int max_argc)
        int argc = 0;
 
        while (*str) {
-               if (argc >= max_argc) return argc;
+               if (argc >= max_argc) break;
 
                /*
                 *      Chop out comments early.
@@ -1274,7 +1502,7 @@ static int str2argv(char *str, char **argv, int max_argc)
                       (*str == '\r') ||
                       (*str == '\n')) *(str++) = '\0';
 
-               if (!*str) return argc;
+               if (!*str) break;
 
                argv[argc] = str;
                argc++;
@@ -1290,7 +1518,6 @@ static int str2argv(char *str, char **argv, int max_argc)
 }
 
 #define MAX_ARGV (16)
-#define MAX_TLV_NEST (3)
 
 /*
  *     Initialize the dictionary.
@@ -1490,6 +1717,7 @@ static int my_dict_init(const char *dir, const char *fn,
                                return -1;
                        }
 
+
                        block_tlv[++which_block_tlv] = da;
                        continue;
                } /* BEGIN-TLV */