/* * Process the VALUE-ALIAS command * * This allows VALUE mappings to be shared among multiple * attributes. */ static int process_value_alias(const char* fn, const int line, char **argv, int argc) { DICT_ATTR *my_da, *da; DICT_VALUE *dval; if (argc != 2) { fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line", fn, line); return -1; } my_da = dict_attrbyname(argv[0]); if (!my_da) { fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist", fn, line, argv[1]); return -1; } if (my_da->flags.has_value) { fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE", fn, line, argv[0]); return -1; } if (my_da->flags.has_value_alias) { fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS", fn, line, argv[0]); return -1; } da = dict_attrbyname(argv[1]); if (!da) { fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias", fn, line, argv[1]); return -1; } if (!da->flags.has_value) { fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values", fn, line, argv[1]); return -1; } if (da->flags.has_value_alias) { fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS", fn, line, argv[1]); return -1; } if (my_da->type != da->type) { fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type", fn, line); return -1; } if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) { fr_strerror_printf("dict_addvalue: out of memory"); return -1; } dval->name[0] = '\0'; /* empty name */ dval->attr = my_da->attr; dval->value = da->attr; if (!fr_hash_table_insert(values_byname, dval)) { fr_strerror_printf("dict_init: %s[%d]: Error create alias", fn, line); fr_pool_free(dval); return -1; } return 0; }
/* * Add an attribute to the dictionary. */ int dict_addattr(const char *name, int vendor, int type, int value, ATTR_FLAGS flags) { size_t namelen; static int max_attr = 0; const char *p; DICT_ATTR *attr; namelen = strlen(name); if (namelen >= DICT_ATTR_MAX_NAME_LEN) { fr_strerror_printf("dict_addattr: attribute name too long"); return -1; } for (p = name; *p != '\0'; p++) { if (*p < ' ') { fr_strerror_printf("dict_addattr: attribute name cannot contain control characters"); return -1; } if ((*p == '"') || (*p == '\\')) { fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash"); return -1; } if ((*p == '<') || (*p == '>') || (*p == '&')) { fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters"); return -1; } } /* * If the value is '-1', that means use a pre-existing * one (if it already exists). If one does NOT already exist, * then create a new attribute, with a non-conflicting value, * and use that. */ if (value == -1) { if (dict_attrbyname(name)) { return 0; /* exists, don't add it again */ } value = ++max_attr; } else if (vendor == 0) { /* * Update 'max_attr' */ if (value > max_attr) { max_attr = value; } } if (value < 0) { fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)"); return -1; } if (value >= 65536) { fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 65535)."); return -1; } if (vendor) { DICT_VENDOR *dv; static DICT_VENDOR *last_vendor = NULL; if (flags.is_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) { fr_strerror_printf("Sub-TLV's cannot be encrypted"); return -1; } if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) { fr_strerror_printf("TLV's cannot be encrypted"); return -1; } if (flags.is_tlv && flags.has_tag) { fr_strerror_printf("Sub-TLV's cannot have a tag"); return -1; } if (flags.has_tlv && flags.has_tag) { fr_strerror_printf("TLV's cannot have a tag"); return -1; } /* * Most ATTRIBUTEs are bunched together by * VENDOR. We can save a lot of lookups on * dictionary initialization by caching the last * vendor. */ if (last_vendor && (vendor == last_vendor->vendorpec)) { dv = last_vendor; } else { dv = dict_vendorbyvalue(vendor); last_vendor = dv; } /* * If the vendor isn't defined, die. */ if (!dv) { fr_strerror_printf("dict_addattr: Unknown vendor"); return -1; } /* * FIXME: Switch over dv->type, and limit things * properly. */ if ((dv->type == 1) && (value >= 256) && !flags.is_tlv) { fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255)."); return -1; } /* else 256..65535 are allowed */ } /* * Create a new attribute for the list */ if ((attr = fr_pool_alloc(sizeof(*attr) + namelen)) == NULL) { fr_strerror_printf("dict_addattr: out of memory"); return -1; } memcpy(attr->name, name, namelen); attr->name[namelen] = '\0'; attr->attr = value; attr->attr |= (vendor << 16); /* FIXME: hack */ attr->vendor = vendor; attr->type = type; attr->flags = flags; attr->vendor = vendor; /* * Insert the attribute, only if it's not a duplicate. */ if (!fr_hash_table_insert(attributes_byname, attr)) { DICT_ATTR *a; /* * If the attribute has identical number, then * ignore the duplicate. */ a = fr_hash_table_finddata(attributes_byname, attr); if (a && (strcasecmp(a->name, attr->name) == 0)) { if (a->attr != attr->attr) { fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name); fr_pool_free(attr); return -1; } /* * Same name, same vendor, same attr, * maybe the flags and/or type is * different. Let the new value * over-ride the old one. */ } fr_hash_table_delete(attributes_byvalue, a); if (!fr_hash_table_replace(attributes_byname, attr)) { fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name); fr_pool_free(attr); return -1; } } /* * Insert the SAME pointer (not free'd when this entry is * deleted), into another table. * * We want this behaviour because we want OLD names for * the attributes to be read from the configuration * files, but when we're printing them, (and looking up * by value) we want to use the NEW name. */ if (!fr_hash_table_replace(attributes_byvalue, attr)) { fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name); return -1; } if (!vendor && (value > 0) && (value < 256)) { dict_base_attrs[value] = attr; } return 0; }
/* * Add a value for an attribute to the dictionary. */ int dict_addvalue(const char *namestr, const char *attrstr, int value) { size_t length; DICT_ATTR *dattr; DICT_VALUE *dval; static DICT_ATTR *last_attr = NULL; if (!*namestr) { fr_strerror_printf("dict_addvalue: empty names are not permitted"); return -1; } if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) { fr_strerror_printf("dict_addvalue: value name too long"); return -1; } if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) { fr_strerror_printf("dict_addvalue: out of memory"); return -1; } memset(dval, 0, sizeof(*dval)); strcpy(dval->name, namestr); dval->value = value; /* * Most VALUEs are bunched together by ATTRIBUTE. We can * save a lot of lookups on dictionary initialization by * caching the last attribute. */ if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) { dattr = last_attr; } else { dattr = dict_attrbyname(attrstr); last_attr = dattr; } /* * Remember which attribute is associated with this * value, if possible. */ if (dattr) { if (dattr->flags.has_value_alias) { fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr); return -1; } dval->attr = dattr->attr; /* * Enforce valid values * * Don't worry about fixups... */ switch (dattr->type) { case PW_TYPE_BYTE: if (value > 255) { fr_pool_free(dval); fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255"); return -1; } break; case PW_TYPE_SHORT: if (value > 65535) { fr_pool_free(dval); fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535"); return -1; } break; /* * Allow octets for now, because * of dictionary.cablelabs */ case PW_TYPE_OCTETS: case PW_TYPE_INTEGER: break; default: fr_pool_free(dval); fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'", fr_int2str(dict_attr_types, dattr->type, "?Unknown?")); return -1; } dattr->flags.has_value = 1; } else { value_fixup_t *fixup; fixup = (value_fixup_t *) malloc(sizeof(*fixup)); if (!fixup) { fr_pool_free(dval); fr_strerror_printf("dict_addvalue: out of memory"); return -1; } memset(fixup, 0, sizeof(*fixup)); strncpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr) - 1); fixup->dval = dval; /* * Insert to the head of the list. */ fixup->next = value_fixup; value_fixup = fixup; return 0; } /* * Add the value into the dictionary. */ if (!fr_hash_table_insert(values_byname, dval)) { if (dattr) { DICT_VALUE *old; /* * Suppress duplicates with the same * name and value. There are lots in * dictionary.ascend. */ old = dict_valbyname(dattr->attr, namestr); if (old && (old->value == dval->value)) { fr_pool_free(dval); return 0; } } fr_pool_free(dval); fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr); return -1; } /* * There are multiple VALUE's, keyed by attribute, so we * take care of that here. */ if (!fr_hash_table_replace(values_byvalue, dval)) { fr_strerror_printf("dict_addvalue: Failed inserting value %s", namestr); return -1; } return 0; }
/* * Add vendor to the list. */ 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"); return -1; } if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) { fr_strerror_printf("dict_addvendor: vendor name too long"); return -1; } if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) { fr_strerror_printf("dict_addvendor: out of memory"); return -1; } strcpy(dv->name, name); dv->vendorpec = value; dv->type = dv->length = 1; /* defaults */ if (!fr_hash_table_insert(vendors_byname, dv)) { DICT_VENDOR *old_dv; old_dv = fr_hash_table_finddata(vendors_byname, dv); if (!old_dv) { fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name); return -1; } if (old_dv->vendorpec != dv->vendorpec) { fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name); return -1; } /* * Already inserted. Discard the duplicate entry. */ fr_pool_free(dv); return 0; } /* * Insert the SAME pointer (not free'd when this table is * deleted), into another table. * * We want this behaviour because we want OLD names for * the attributes to be read from the configuration * files, but when we're printing them, (and looking up * by value) we want to use the NEW name. */ if (!fr_hash_table_replace(vendors_byvalue, dv)) { fr_strerror_printf("dict_addvendor: Failed inserting vendor %s", name); return -1; } return 0; }