2 * $Id: dict.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
4 * Copyright (C) 2002 Roaring Penguin Software Inc.
6 * Copyright (C) 1995,1996,1997 Lars Fenneberg
8 * Copyright 1992 Livingston Enterprises, Inc.
10 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
11 * and Merit Network, Inc. All Rights Reserved
13 * See the file COPYRIGHT for the respective terms and conditions.
14 * If the file is missing contact me at lf@elemental.net
15 * and I'll send you a copy.
20 #include <radiusclient.h>
22 static DICT_ATTR *dictionary_attributes = NULL;
23 static DICT_VALUE *dictionary_values = NULL;
24 static VENDOR_DICT *vendor_dictionaries = NULL;
27 * Function: rc_read_dictionary
29 * Purpose: Initialize the dictionary. Read all ATTRIBUTES into
30 * the dictionary_attributes list. Read all VALUES into
31 * the dictionary_values list. Construct VENDOR dictionaries
36 int rc_read_dictionary (char *filename)
39 char dummystr[AUTH_ID_LEN];
40 char namestr[AUTH_ID_LEN];
41 char valstr[AUTH_ID_LEN];
42 char attrstr[AUTH_ID_LEN];
43 char typestr[AUTH_ID_LEN];
44 char vendorstr[AUTH_ID_LEN];
54 if ((dictfd = fopen (filename, "r")) == (FILE *) NULL)
56 error( "rc_read_dictionary: couldn't open dictionary %s: %s",
57 filename, strerror(errno));
63 while (fgets (buffer, sizeof (buffer), dictfd) != (char *) NULL)
67 /* Skip empty space */
68 if (*buffer == '#' || *buffer == '\0' || *buffer == '\n')
73 if (strncmp (buffer, "VENDOR", 6) == 0) {
74 /* Read the VENDOR line */
75 if (sscanf(buffer, "%s%s%d", dummystr, namestr, &value) != 3) {
76 error("rc_read_dictionary: invalid vendor on line %d of dictionary %s",
82 if (strlen (namestr) > NAME_LENGTH) {
83 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
88 /* Create new vendor entry */
89 vdict = (VENDOR_DICT *) malloc (sizeof (VENDOR_DICT));
91 novm("rc_read_dictionary");
95 strcpy(vdict->vendorname, namestr);
96 vdict->vendorcode = value;
97 vdict->attributes = NULL;
98 vdict->next = vendor_dictionaries;
99 vendor_dictionaries = vdict;
101 else if (strncmp (buffer, "ATTRIBUTE", 9) == 0)
104 /* Read the ATTRIBUTE line. It is one of:
105 * ATTRIBUTE attr_name attr_val type OR
106 * ATTRIBUTE attr_name attr_val type vendor */
108 n = sscanf(buffer, "%s%s%s%s%s", dummystr, namestr, valstr, typestr, vendorstr);
109 if (n != 4 && n != 5)
111 error("rc_read_dictionary: invalid attribute on line %d of dictionary %s",
118 * Validate all entries
120 if (strlen (namestr) > NAME_LENGTH)
122 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
128 if (strlen (vendorstr) > NAME_LENGTH)
130 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
136 if (!isdigit (*valstr))
138 error("rc_read_dictionary: invalid value on line %d of dictionary %s",
143 value = atoi (valstr);
145 if (strcmp (typestr, "string") == 0)
147 type = PW_TYPE_STRING;
149 else if (strcmp (typestr, "integer") == 0)
151 type = PW_TYPE_INTEGER;
153 else if (strcmp (typestr, "ipaddr") == 0 || strcmp (typestr, "ipv4addr") == 0)
155 type = PW_TYPE_IPADDR;
157 else if (strcmp (typestr, "date") == 0)
161 else if (strcmp (typestr, "ifid") == 0)
165 else if (strcmp (typestr, "ipv6addr") == 0)
167 type = PW_TYPE_IPV6ADDR;
169 else if (strcmp (typestr, "ipv6prefix") == 0)
171 type = PW_TYPE_IPV6PREFIX;
175 error("rc_read_dictionary: invalid type on line %d of dictionary %s",
181 /* Search for vendor if supplied */
183 vdict = rc_dict_findvendor(vendorstr);
185 error("rc_read_dictionary: unknown vendor on line %d of dictionary %s",
193 /* Create a new attribute for the list */
195 (DICT_ATTR *) malloc (sizeof (DICT_ATTR)))
196 == (DICT_ATTR *) NULL)
198 novm("rc_read_dictionary");
202 strcpy (attr->name, namestr);
204 attr->vendorcode = vdict->vendorcode;
206 attr->vendorcode = VENDOR_NONE;
211 /* Insert it into the list */
213 attr->next = vdict->attributes;
214 vdict->attributes = attr;
216 attr->next = dictionary_attributes;
217 dictionary_attributes = attr;
220 else if (strncmp (buffer, "VALUE", 5) == 0)
222 /* Read the VALUE line */
223 if (sscanf (buffer, "%s%s%s%s", dummystr, attrstr,
224 namestr, valstr) != 4)
226 error("rc_read_dictionary: invalid value entry on line %d of dictionary %s",
233 * Validate all entries
235 if (strlen (attrstr) > NAME_LENGTH)
237 error("rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
243 if (strlen (namestr) > NAME_LENGTH)
245 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
251 if (!isdigit (*valstr))
253 error("rc_read_dictionary: invalid value on line %d of dictionary %s",
258 value = atoi (valstr);
260 /* Create a new VALUE entry for the list */
262 (DICT_VALUE *) malloc (sizeof (DICT_VALUE)))
263 == (DICT_VALUE *) NULL)
265 novm("rc_read_dictionary");
269 strcpy (dval->attrname, attrstr);
270 strcpy (dval->name, namestr);
273 /* Insert it into the list */
274 dval->next = dictionary_values;
275 dictionary_values = dval;
277 else if (strncmp (buffer, "INCLUDE", 7) == 0)
279 /* Read the INCLUDE line */
280 if (sscanf (buffer, "%s%s", dummystr, namestr) != 2)
282 error("rc_read_dictionary: invalid include entry on line %d of dictionary %s",
287 if (rc_read_dictionary(namestr) == -1)
299 * Function: rc_dict_getattr
301 * Purpose: Return the full attribute structure based on the
302 * attribute id number and vendor code. If vendor code is VENDOR_NONE,
303 * non-vendor-specific attributes are used
307 DICT_ATTR *rc_dict_getattr (int attribute, int vendor)
312 if (vendor == VENDOR_NONE) {
313 attr = dictionary_attributes;
314 while (attr != (DICT_ATTR *) NULL) {
315 if (attr->value == attribute) {
321 dict = rc_dict_getvendor(vendor);
325 attr = dict->attributes;
327 if (attr->value == attribute) {
337 * Function: rc_dict_findattr
339 * Purpose: Return the full attribute structure based on the
344 DICT_ATTR *rc_dict_findattr (char *attrname)
349 attr = dictionary_attributes;
350 while (attr != (DICT_ATTR *) NULL)
352 if (strcasecmp (attr->name, attrname) == 0)
359 /* Search vendor-specific dictionaries */
360 dict = vendor_dictionaries;
362 attr = dict->attributes;
364 if (strcasecmp (attr->name, attrname) == 0) {
371 return ((DICT_ATTR *) NULL);
376 * Function: rc_dict_findval
378 * Purpose: Return the full value structure based on the
383 DICT_VALUE *rc_dict_findval (char *valname)
387 val = dictionary_values;
388 while (val != (DICT_VALUE *) NULL)
390 if (strcasecmp (val->name, valname) == 0)
396 return ((DICT_VALUE *) NULL);
400 * Function: dict_getval
402 * Purpose: Return the full value structure based on the
403 * actual value and the associated attribute name.
407 DICT_VALUE * rc_dict_getval (UINT4 value, char *attrname)
411 val = dictionary_values;
412 while (val != (DICT_VALUE *) NULL)
414 if (strcmp (val->attrname, attrname) == 0 &&
421 return ((DICT_VALUE *) NULL);
425 * Function: rc_dict_findvendor
427 * Purpose: Return the vendor's dictionary given the vendor name.
430 VENDOR_DICT * rc_dict_findvendor (char *vendorname)
434 dict = vendor_dictionaries;
436 if (!strcmp(vendorname, dict->vendorname)) {
445 * Function: rc_dict_getvendor
447 * Purpose: Return the vendor's dictionary given the vendor ID
450 VENDOR_DICT * rc_dict_getvendor (int id)
454 dict = vendor_dictionaries;
456 if (id == dict->vendorcode) {