2 * $Id: dict.c,v 1.1 2002/01/22 16:03:02 dfs 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.
21 #include <radiusclient.h>
23 static DICT_ATTR *dictionary_attributes = NULL;
24 static DICT_VALUE *dictionary_values = NULL;
25 static VENDOR_DICT *vendor_dictionaries = NULL;
28 * Function: rc_read_dictionary
30 * Purpose: Initialize the dictionary. Read all ATTRIBUTES into
31 * the dictionary_attributes list. Read all VALUES into
32 * the dictionary_values list. Construct VENDOR dictionaries
37 int rc_read_dictionary (char *filename)
40 char dummystr[AUTH_ID_LEN];
41 char namestr[AUTH_ID_LEN];
42 char valstr[AUTH_ID_LEN];
43 char attrstr[AUTH_ID_LEN];
44 char typestr[AUTH_ID_LEN];
45 char vendorstr[AUTH_ID_LEN];
55 if ((dictfd = fopen (filename, "r")) == (FILE *) NULL)
57 rc_log(LOG_ERR, "rc_read_dictionary: couldn't open dictionary %s: %s",
58 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 rc_log(LOG_ERR, "rc_read_dictionary: invalid vendor on line %d of dictionary %s",
81 if (strlen (namestr) > NAME_LENGTH) {
82 rc_log(LOG_ERR, "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 rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
92 strcpy(vdict->vendorname, namestr);
93 vdict->vendorcode = value;
94 vdict->attributes = NULL;
95 vdict->next = vendor_dictionaries;
96 vendor_dictionaries = vdict;
98 else if (strncmp (buffer, "ATTRIBUTE", 9) == 0)
101 /* Read the ATTRIBUTE line. It is one of:
102 * ATTRIBUTE attr_name attr_val type OR
103 * ATTRIBUTE attr_name attr_val type vendor */
105 n = sscanf(buffer, "%s%s%s%s%s", dummystr, namestr, valstr, typestr, vendorstr);
106 if (n != 4 && n != 5)
108 rc_log(LOG_ERR, "rc_read_dictionary: invalid attribute on line %d of dictionary %s",
114 * Validate all entries
116 if (strlen (namestr) > NAME_LENGTH)
118 rc_log(LOG_ERR, "rc_read_dictionary: invalid name length on line %d of dictionary %s",
123 if (strlen (vendorstr) > NAME_LENGTH)
125 rc_log(LOG_ERR, "rc_read_dictionary: invalid name length on line %d of dictionary %s",
130 if (!isdigit (*valstr))
133 "rc_read_dictionary: invalid value on line %d of dictionary %s",
137 value = atoi (valstr);
139 if (strcmp (typestr, "string") == 0)
141 type = PW_TYPE_STRING;
143 else if (strcmp (typestr, "integer") == 0)
145 type = PW_TYPE_INTEGER;
147 else if (strcmp (typestr, "ipaddr") == 0)
149 type = PW_TYPE_IPADDR;
151 else if (strcmp (typestr, "date") == 0)
158 "rc_read_dictionary: invalid type on line %d of dictionary %s",
163 /* Search for vendor if supplied */
165 vdict = rc_dict_findvendor(vendorstr);
168 "rc_read_dictionary: unknown vendor on line %d of dictionary %s",
175 /* Create a new attribute for the list */
177 (DICT_ATTR *) malloc (sizeof (DICT_ATTR)))
178 == (DICT_ATTR *) NULL)
180 rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
183 strcpy (attr->name, namestr);
185 attr->vendorcode = vdict->vendorcode;
187 attr->vendorcode = VENDOR_NONE;
192 /* Insert it into the list */
194 attr->next = vdict->attributes;
195 vdict->attributes = attr;
197 attr->next = dictionary_attributes;
198 dictionary_attributes = attr;
201 else if (strncmp (buffer, "VALUE", 5) == 0)
203 /* Read the VALUE line */
204 if (sscanf (buffer, "%s%s%s%s", dummystr, attrstr,
205 namestr, valstr) != 4)
208 "rc_read_dictionary: invalid value entry on line %d of dictionary %s",
214 * Validate all entries
216 if (strlen (attrstr) > NAME_LENGTH)
219 "rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
224 if (strlen (namestr) > NAME_LENGTH)
227 "rc_read_dictionary: invalid name length on line %d of dictionary %s",
232 if (!isdigit (*valstr))
235 "rc_read_dictionary: invalid value on line %d of dictionary %s",
239 value = atoi (valstr);
241 /* Create a new VALUE entry for the list */
243 (DICT_VALUE *) malloc (sizeof (DICT_VALUE)))
244 == (DICT_VALUE *) NULL)
246 rc_log(LOG_CRIT, "rc_read_dictionary: out of memory");
249 strcpy (dval->attrname, attrstr);
250 strcpy (dval->name, namestr);
253 /* Insert it into the list */
254 dval->next = dictionary_values;
255 dictionary_values = dval;
263 * Function: rc_dict_getattr
265 * Purpose: Return the full attribute structure based on the
266 * attribute id number and vendor code. If vendor code is VENDOR_NONE,
267 * non-vendor-specific attributes are used
271 DICT_ATTR *rc_dict_getattr (int attribute, int vendor)
276 if (vendor == VENDOR_NONE) {
277 attr = dictionary_attributes;
278 while (attr != (DICT_ATTR *) NULL) {
279 if (attr->value == attribute) {
285 dict = rc_dict_getvendor(vendor);
289 attr = dict->attributes;
291 if (attr->value == attribute) {
301 * Function: rc_dict_findattr
303 * Purpose: Return the full attribute structure based on the
308 DICT_ATTR *rc_dict_findattr (char *attrname)
313 attr = dictionary_attributes;
314 while (attr != (DICT_ATTR *) NULL)
316 if (strcasecmp (attr->name, attrname) == 0)
323 /* Search vendor-specific dictionaries */
324 dict = vendor_dictionaries;
326 attr = dict->attributes;
328 if (strcasecmp (attr->name, attrname) == 0) {
335 return ((DICT_ATTR *) NULL);
340 * Function: rc_dict_findval
342 * Purpose: Return the full value structure based on the
347 DICT_VALUE *rc_dict_findval (char *valname)
351 val = dictionary_values;
352 while (val != (DICT_VALUE *) NULL)
354 if (strcasecmp (val->name, valname) == 0)
360 return ((DICT_VALUE *) NULL);
364 * Function: dict_getval
366 * Purpose: Return the full value structure based on the
367 * actual value and the associated attribute name.
371 DICT_VALUE * rc_dict_getval (UINT4 value, char *attrname)
375 val = dictionary_values;
376 while (val != (DICT_VALUE *) NULL)
378 if (strcmp (val->attrname, attrname) == 0 &&
385 return ((DICT_VALUE *) NULL);
389 * Function: rc_dict_findvendor
391 * Purpose: Return the vendor's dictionary given the vendor name.
394 VENDOR_DICT * rc_dict_findvendor (char *vendorname)
398 dict = vendor_dictionaries;
400 if (!strcmp(vendorname, dict->vendorname)) {
409 * Function: rc_dict_getvendor
411 * Purpose: Return the vendor's dictionary given the vendor ID
414 VENDOR_DICT * rc_dict_getvendor (int id)
418 dict = vendor_dictionaries;
420 if (id == dict->vendorcode) {