2 * Copyright (C) 2002 Roaring Penguin Software Inc.
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
11 * See the file COPYRIGHT for the respective terms and conditions.
12 * If the file is missing contact me at lf@elemental.net
13 * and I'll send you a copy.
18 #include <radiusclient.h>
20 static DICT_ATTR *dictionary_attributes = NULL;
21 static DICT_VALUE *dictionary_values = NULL;
22 static VENDOR_DICT *vendor_dictionaries = NULL;
25 * Function: rc_read_dictionary
27 * Purpose: Initialize the dictionary. Read all ATTRIBUTES into
28 * the dictionary_attributes list. Read all VALUES into
29 * the dictionary_values list. Construct VENDOR dictionaries
34 int rc_read_dictionary (char *filename)
37 char dummystr[AUTH_ID_LEN];
38 char namestr[AUTH_ID_LEN];
39 char valstr[AUTH_ID_LEN];
40 char attrstr[AUTH_ID_LEN];
41 char typestr[AUTH_ID_LEN];
42 char vendorstr[AUTH_ID_LEN];
52 if ((dictfd = fopen (filename, "r")) == (FILE *) NULL)
54 error( "rc_read_dictionary: couldn't open dictionary %s: %s",
55 filename, strerror(errno));
61 while (fgets (buffer, sizeof (buffer), dictfd) != (char *) NULL)
65 /* Skip empty space */
66 if (*buffer == '#' || *buffer == '\0' || *buffer == '\n')
71 if (strncmp (buffer, "VENDOR", 6) == 0) {
72 /* Read the VENDOR line */
73 if (sscanf(buffer, "%s%s%d", dummystr, namestr, &value) != 3) {
74 error("rc_read_dictionary: invalid vendor on line %d of dictionary %s",
80 if (strlen (namestr) > NAME_LENGTH) {
81 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
86 /* Create new vendor entry */
87 vdict = (VENDOR_DICT *) malloc (sizeof (VENDOR_DICT));
89 novm("rc_read_dictionary");
93 strcpy(vdict->vendorname, namestr);
94 vdict->vendorcode = value;
95 vdict->attributes = NULL;
96 vdict->next = vendor_dictionaries;
97 vendor_dictionaries = vdict;
99 else if (strncmp (buffer, "ATTRIBUTE", 9) == 0)
102 /* Read the ATTRIBUTE line. It is one of:
103 * ATTRIBUTE attr_name attr_val type OR
104 * ATTRIBUTE attr_name attr_val type vendor */
106 n = sscanf(buffer, "%s%s%s%s%s", dummystr, namestr, valstr, typestr, vendorstr);
107 if (n != 4 && n != 5)
109 error("rc_read_dictionary: invalid attribute on line %d of dictionary %s",
116 * Validate all entries
118 if (strlen (namestr) > NAME_LENGTH)
120 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
126 if (strlen (vendorstr) > NAME_LENGTH)
128 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
134 if (!isdigit (*valstr))
136 error("rc_read_dictionary: invalid value on line %d of dictionary %s",
141 value = atoi (valstr);
143 if (strcmp (typestr, "string") == 0)
145 type = PW_TYPE_STRING;
147 else if (strcmp (typestr, "integer") == 0)
149 type = PW_TYPE_INTEGER;
151 else if (strcmp (typestr, "ipaddr") == 0 || strcmp (typestr, "ipv4addr") == 0)
153 type = PW_TYPE_IPADDR;
155 else if (strcmp (typestr, "date") == 0)
159 else if (strcmp (typestr, "ifid") == 0)
163 else if (strcmp (typestr, "ipv6addr") == 0)
165 type = PW_TYPE_IPV6ADDR;
167 else if (strcmp (typestr, "ipv6prefix") == 0)
169 type = PW_TYPE_IPV6PREFIX;
173 error("rc_read_dictionary: invalid type on line %d of dictionary %s",
179 /* Search for vendor if supplied */
181 vdict = rc_dict_findvendor(vendorstr);
183 error("rc_read_dictionary: unknown vendor on line %d of dictionary %s",
191 /* Create a new attribute for the list */
193 (DICT_ATTR *) malloc (sizeof (DICT_ATTR)))
194 == (DICT_ATTR *) NULL)
196 novm("rc_read_dictionary");
200 strcpy (attr->name, namestr);
202 attr->vendorcode = vdict->vendorcode;
204 attr->vendorcode = VENDOR_NONE;
209 /* Insert it into the list */
211 attr->next = vdict->attributes;
212 vdict->attributes = attr;
214 attr->next = dictionary_attributes;
215 dictionary_attributes = attr;
218 else if (strncmp (buffer, "VALUE", 5) == 0)
220 /* Read the VALUE line */
221 if (sscanf (buffer, "%s%s%s%s", dummystr, attrstr,
222 namestr, valstr) != 4)
224 error("rc_read_dictionary: invalid value entry on line %d of dictionary %s",
231 * Validate all entries
233 if (strlen (attrstr) > NAME_LENGTH)
235 error("rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
241 if (strlen (namestr) > NAME_LENGTH)
243 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
249 if (!isdigit (*valstr))
251 error("rc_read_dictionary: invalid value on line %d of dictionary %s",
256 value = atoi (valstr);
258 /* Create a new VALUE entry for the list */
260 (DICT_VALUE *) malloc (sizeof (DICT_VALUE)))
261 == (DICT_VALUE *) NULL)
263 novm("rc_read_dictionary");
267 strcpy (dval->attrname, attrstr);
268 strcpy (dval->name, namestr);
271 /* Insert it into the list */
272 dval->next = dictionary_values;
273 dictionary_values = dval;
275 else if (strncmp (buffer, "INCLUDE", 7) == 0)
277 /* Read the INCLUDE line */
278 if (sscanf (buffer, "%s%s", dummystr, namestr) != 2)
280 error("rc_read_dictionary: invalid include entry on line %d of dictionary %s",
285 if (rc_read_dictionary(namestr) == -1)
297 * Function: rc_dict_getattr
299 * Purpose: Return the full attribute structure based on the
300 * attribute id number and vendor code. If vendor code is VENDOR_NONE,
301 * non-vendor-specific attributes are used
305 DICT_ATTR *rc_dict_getattr (int attribute, int vendor)
310 if (vendor == VENDOR_NONE) {
311 attr = dictionary_attributes;
312 while (attr != (DICT_ATTR *) NULL) {
313 if (attr->value == attribute) {
319 dict = rc_dict_getvendor(vendor);
323 attr = dict->attributes;
325 if (attr->value == attribute) {
335 * Function: rc_dict_findattr
337 * Purpose: Return the full attribute structure based on the
342 DICT_ATTR *rc_dict_findattr (char *attrname)
347 attr = dictionary_attributes;
348 while (attr != (DICT_ATTR *) NULL)
350 if (strcasecmp (attr->name, attrname) == 0)
357 /* Search vendor-specific dictionaries */
358 dict = vendor_dictionaries;
360 attr = dict->attributes;
362 if (strcasecmp (attr->name, attrname) == 0) {
369 return ((DICT_ATTR *) NULL);
374 * Function: rc_dict_findval
376 * Purpose: Return the full value structure based on the
381 DICT_VALUE *rc_dict_findval (char *valname)
385 val = dictionary_values;
386 while (val != (DICT_VALUE *) NULL)
388 if (strcasecmp (val->name, valname) == 0)
394 return ((DICT_VALUE *) NULL);
398 * Function: dict_getval
400 * Purpose: Return the full value structure based on the
401 * actual value and the associated attribute name.
405 DICT_VALUE * rc_dict_getval (UINT4 value, char *attrname)
409 val = dictionary_values;
410 while (val != (DICT_VALUE *) NULL)
412 if (strcmp (val->attrname, attrname) == 0 &&
419 return ((DICT_VALUE *) NULL);
423 * Function: rc_dict_findvendor
425 * Purpose: Return the vendor's dictionary given the vendor name.
428 VENDOR_DICT * rc_dict_findvendor (char *vendorname)
432 dict = vendor_dictionaries;
434 if (!strcmp(vendorname, dict->vendorname)) {
443 * Function: rc_dict_getvendor
445 * Purpose: Return the vendor's dictionary given the vendor ID
448 VENDOR_DICT * rc_dict_getvendor (int id)
452 dict = vendor_dictionaries;
454 if (id == dict->vendorcode) {