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)
155 type = PW_TYPE_IPADDR;
157 else if (strcmp (typestr, "date") == 0)
163 error("rc_read_dictionary: invalid type on line %d of dictionary %s",
169 /* Search for vendor if supplied */
171 vdict = rc_dict_findvendor(vendorstr);
173 error("rc_read_dictionary: unknown vendor on line %d of dictionary %s",
181 /* Create a new attribute for the list */
183 (DICT_ATTR *) malloc (sizeof (DICT_ATTR)))
184 == (DICT_ATTR *) NULL)
186 novm("rc_read_dictionary");
190 strcpy (attr->name, namestr);
192 attr->vendorcode = vdict->vendorcode;
194 attr->vendorcode = VENDOR_NONE;
199 /* Insert it into the list */
201 attr->next = vdict->attributes;
202 vdict->attributes = attr;
204 attr->next = dictionary_attributes;
205 dictionary_attributes = attr;
208 else if (strncmp (buffer, "VALUE", 5) == 0)
210 /* Read the VALUE line */
211 if (sscanf (buffer, "%s%s%s%s", dummystr, attrstr,
212 namestr, valstr) != 4)
214 error("rc_read_dictionary: invalid value entry on line %d of dictionary %s",
221 * Validate all entries
223 if (strlen (attrstr) > NAME_LENGTH)
225 error("rc_read_dictionary: invalid attribute length on line %d of dictionary %s",
231 if (strlen (namestr) > NAME_LENGTH)
233 error("rc_read_dictionary: invalid name length on line %d of dictionary %s",
239 if (!isdigit (*valstr))
241 error("rc_read_dictionary: invalid value on line %d of dictionary %s",
246 value = atoi (valstr);
248 /* Create a new VALUE entry for the list */
250 (DICT_VALUE *) malloc (sizeof (DICT_VALUE)))
251 == (DICT_VALUE *) NULL)
253 novm("rc_read_dictionary");
257 strcpy (dval->attrname, attrstr);
258 strcpy (dval->name, namestr);
261 /* Insert it into the list */
262 dval->next = dictionary_values;
263 dictionary_values = dval;
265 else if (strncmp (buffer, "INCLUDE", 7) == 0)
267 /* Read the INCLUDE line */
268 if (sscanf (buffer, "%s%s", dummystr, namestr) != 2)
270 error("rc_read_dictionary: invalid include entry on line %d of dictionary %s",
275 if (rc_read_dictionary(namestr) == -1)
287 * Function: rc_dict_getattr
289 * Purpose: Return the full attribute structure based on the
290 * attribute id number and vendor code. If vendor code is VENDOR_NONE,
291 * non-vendor-specific attributes are used
295 DICT_ATTR *rc_dict_getattr (int attribute, int vendor)
300 if (vendor == VENDOR_NONE) {
301 attr = dictionary_attributes;
302 while (attr != (DICT_ATTR *) NULL) {
303 if (attr->value == attribute) {
309 dict = rc_dict_getvendor(vendor);
313 attr = dict->attributes;
315 if (attr->value == attribute) {
325 * Function: rc_dict_findattr
327 * Purpose: Return the full attribute structure based on the
332 DICT_ATTR *rc_dict_findattr (char *attrname)
337 attr = dictionary_attributes;
338 while (attr != (DICT_ATTR *) NULL)
340 if (strcasecmp (attr->name, attrname) == 0)
347 /* Search vendor-specific dictionaries */
348 dict = vendor_dictionaries;
350 attr = dict->attributes;
352 if (strcasecmp (attr->name, attrname) == 0) {
359 return ((DICT_ATTR *) NULL);
364 * Function: rc_dict_findval
366 * Purpose: Return the full value structure based on the
371 DICT_VALUE *rc_dict_findval (char *valname)
375 val = dictionary_values;
376 while (val != (DICT_VALUE *) NULL)
378 if (strcasecmp (val->name, valname) == 0)
384 return ((DICT_VALUE *) NULL);
388 * Function: dict_getval
390 * Purpose: Return the full value structure based on the
391 * actual value and the associated attribute name.
395 DICT_VALUE * rc_dict_getval (UINT4 value, char *attrname)
399 val = dictionary_values;
400 while (val != (DICT_VALUE *) NULL)
402 if (strcmp (val->attrname, attrname) == 0 &&
409 return ((DICT_VALUE *) NULL);
413 * Function: rc_dict_findvendor
415 * Purpose: Return the vendor's dictionary given the vendor name.
418 VENDOR_DICT * rc_dict_findvendor (char *vendorname)
422 dict = vendor_dictionaries;
424 if (!strcmp(vendorname, dict->vendorname)) {
433 * Function: rc_dict_getvendor
435 * Purpose: Return the vendor's dictionary given the vendor ID
438 VENDOR_DICT * rc_dict_getvendor (int id)
442 dict = vendor_dictionaries;
444 if (id == dict->vendorcode) {