24#include <radcli/radcli.h>
42 if (strlen (namestr) > RC_NAME_LENGTH)
44 rc_log(LOG_ERR,
"rc_dict_addattr: invalid attribute length");
50 rc_log(LOG_ERR,
"rc_dict_addattr: invalid attribute type");
55 if ((attr = malloc(
sizeof (
DICT_ATTR))) == NULL)
57 rc_log(LOG_CRIT,
"rc_dict_addattr: out of memory");
61 strlcpy(attr->
name, namestr,
sizeof(attr->
name));
62 attr->
value = RADCLI_VENDOR_ATTR_SET(value, vendorspec);
66 attr->next = rh->dictionary_attributes;
67 rh->dictionary_attributes = attr;
85 if (strlen(attrstr) > RC_NAME_LENGTH)
87 rc_log(LOG_ERR,
"rc_dict_addval: invalid attribute length");
91 if (strlen(namestr) > RC_NAME_LENGTH)
93 rc_log(LOG_ERR,
"rc_dict_addval: invalid name length");
98 if ((dval = malloc(
sizeof (
DICT_VALUE))) == NULL)
100 rc_log(LOG_CRIT,
"rc_dict_addval: out of memory");
103 strlcpy(dval->attrname, attrstr,
sizeof(dval->attrname));
104 strlcpy(dval->name, namestr,
sizeof(dval->name));
108 dval->next = rh->dictionary_values;
109 rh->dictionary_values = dval;
126 if (strlen(namestr) > RC_NAME_LENGTH)
128 rc_log(LOG_ERR,
"rc_dict_addvend: invalid vendor name length");
136 rc_log(LOG_CRIT,
"rc_dict_init: out of memory");
139 strlcpy(dvend->vendorname, namestr,
sizeof(dvend->vendorname));
140 dvend->vendorpec = vendorspec;
143 dvend->next = rh->dictionary_vendors;
144 rh->dictionary_vendors = dvend;
158static int rc_dict_init(rc_handle *rh, FILE *dictfd,
char const *filename)
160 char dummystr[AUTH_ID_LEN];
161 char namestr[AUTH_ID_LEN];
162 char valstr[AUTH_ID_LEN];
163 char attrstr[AUTH_ID_LEN];
164 char typestr[AUTH_ID_LEN];
165 char optstr[AUTH_ID_LEN];
166 char ifilename[PATH_MAX] = {0};
175 unsigned attr_vendorspec = 0;
176 const char *pfilename = filename;
178 if (pfilename == NULL)
180 pfilename =
"memory";
183 while (fgets (buffer,
sizeof (buffer), dictfd) != NULL)
188 if (*buffer ==
'#' || *buffer ==
'\0' || *buffer ==
'\n' || \
195 cp = strchr(buffer,
'#');
201 if (strncmp (buffer,
"ATTRIBUTE", 9) == 0)
205 if (sscanf (buffer,
"%63s%63s%63s%63s%63s", dummystr, namestr,
206 valstr, typestr, optstr) < 4)
209 "rc_dict_init: invalid attribute on line %d of "
210 "dictionary %s", line_no, pfilename);
217 if (strlen (namestr) > RC_NAME_LENGTH)
220 "rc_dict_init: invalid name length on line %d of "
221 "dictionary %s", line_no, pfilename);
225 if (!isdigit (*valstr))
228 "rc_dict_init: invalid value on line %d of dictionary %s",
232 value = atoi (valstr);
234 if (strcmp (typestr,
"string") == 0)
238 else if (strcmp (typestr,
"integer") == 0)
242 else if (strcmp (typestr,
"ipaddr") == 0)
246 else if (strcmp (typestr,
"ipv4addr") == 0)
250 else if (strcmp (typestr,
"ipv6addr") == 0)
254 else if (strcmp (typestr,
"ipv6prefix") == 0)
258 else if (strcmp (typestr,
"date") == 0)
265 "rc_dict_init: invalid type on line %d of dictionary %s",
271 if (optstr[0] !=
'\0') {
273 for (cp1 = optstr; cp1 != NULL; cp1 = cp) {
274 cp = strchr(cp1,
',');
279 if (strncmp(cp1,
"vendor=", 7) == 0)
284 "rc_dict_init: unknown Vendor-Id %s on line %d of "
285 "dictionary %s", cp1, line_no, pfilename);
292 if ((attr = malloc (
sizeof (
DICT_ATTR))) == NULL)
294 rc_log(LOG_CRIT,
"rc_dict_init: out of memory");
297 strcpy (attr->
name, namestr);
301 attr->
value = RADCLI_VENDOR_ATTR_SET(value, dvend->vendorpec);
303 attr->
value = RADCLI_VENDOR_ATTR_SET(value, attr_vendorspec);
307 attr->next = rh->dictionary_attributes;
308 rh->dictionary_attributes = attr;
310 else if (strncmp (buffer,
"VALUE", 5) == 0)
313 if (sscanf (buffer,
"%63s%63s%63s%63s", dummystr, attrstr,
314 namestr, valstr) != 4)
317 "rc_dict_init: invalid value entry on line %d of "
318 "dictionary %s", line_no, pfilename);
325 if (strlen (attrstr) > RC_NAME_LENGTH)
328 "rc_dict_init: invalid attribute length on line %d of "
329 "dictionary %s", line_no, pfilename);
333 if (strlen (namestr) > RC_NAME_LENGTH)
336 "rc_dict_init: invalid name length on line %d of "
337 "dictionary %s", line_no, pfilename);
341 if (!isdigit (*valstr))
344 "rc_dict_init: invalid value on line %d of dictionary %s",
348 value = atoi (valstr);
351 if ((dval = malloc (
sizeof (
DICT_VALUE))) == NULL)
353 rc_log(LOG_CRIT,
"rc_dict_init: out of memory");
356 strcpy (dval->attrname, attrstr);
357 strcpy (dval->name, namestr);
361 dval->next = rh->dictionary_values;
362 rh->dictionary_values = dval;
364 else if ((filename != NULL) &&
365 (strncmp (buffer,
"$INCLUDE", 8) == 0))
368 if (sscanf (buffer,
"%63s%63s", dummystr, namestr) != 2)
371 "rc_dict_init: invalid include entry on line %d of "
372 "dictionary %s", line_no, pfilename);
375 strncpy(ifilename, namestr,
sizeof(ifilename));
377 if (namestr[0] !=
'/') {
378 cp = strrchr(filename,
'/');
381 snprintf(ifilename,
sizeof(ifilename),
"%s/%s", filename, namestr);
390 else if (strncmp (buffer,
"END-VENDOR", 10) == 0)
394 else if (strncmp (buffer,
"BEGIN-VENDOR", 12) == 0)
398 if (sscanf (buffer+12,
"%63s", dummystr) != 1)
401 "rc_dict_init: invalid Vendor-Id on line %d of "
402 "dictionary %s", line_no, pfilename);
409 "rc_dict_init: unknown Vendor %s on line %d of "
410 "dictionary %s", dummystr, line_no, pfilename);
414 attr_vendorspec = v->vendorpec;
416 else if (strncmp (buffer,
"VENDOR", 6) == 0)
419 if (sscanf (buffer,
"%63s%63s%63s", dummystr, attrstr, valstr) != 3)
422 "rc_dict_init: invalid Vendor-Id on line %d of "
423 "dictionary %s", line_no, pfilename);
428 if (strlen (attrstr) > RC_NAME_LENGTH)
431 "rc_dict_init: invalid attribute length on line %d of "
432 "dictionary %s", line_no, pfilename);
436 if (!isdigit (*valstr))
439 "rc_dict_init: invalid Vendor-Id on line %d of "
440 "dictionary %s", line_no, pfilename);
443 value = atoi (valstr);
449 rc_log(LOG_CRIT,
"rc_dict_init: out of memory");
452 strcpy (dvend->vendorname, attrstr);
453 dvend->vendorpec = value;
456 dvend->next = rh->dictionary_vendors;
457 rh->dictionary_vendors = dvend;
477 if (rh->first_dict_read != NULL && strcmp(filename, rh->first_dict_read) == 0)
480 if ((dictfd = fopen (filename,
"r")) == NULL)
482 rc_log(LOG_ERR,
"rc_read_dictionary couldn't open dictionary %s: %s",
483 filename, strerror(errno));
487 ret_val = rc_dict_init(rh, dictfd, filename);
491 if (rh->first_dict_read == NULL)
492 rh->first_dict_read = strdup(filename);
512 if ((dictfd = fmemopen ((
void *)buf, size,
"r")) == NULL)
514 rc_log(LOG_ERR,
"rc_read_dictionary_from_buffer failed to read "
515 "input buffer %s", strerror(errno));
519 ret_val = rc_dict_init(rh, dictfd, NULL);
536 attr = rh->dictionary_attributes;
539 if (attr->
value == attribute)
559 attr = rh->dictionary_attributes;
562 if (strcasecmp (attr->
name, attrname) == 0)
581 val = rh->dictionary_values;
584 if (strcasecmp (val->name, valname) == 0)
603 for (vend = rh->dictionary_vendors; vend != NULL; vend = vend->next)
604 if (strcasecmp(vend->vendorname, vendorname) == 0)
619 for (vend = rh->dictionary_vendors; vend != NULL; vend = vend->next)
620 if (vend->vendorpec == vendorspec)
636 val = rh->dictionary_values;
639 if (strcmp (val->attrname, attrname) == 0 &&
659 for (attr = rh->dictionary_attributes; attr != NULL; attr = nattr) {
663 for (val = rh->dictionary_values; val != NULL; val = nval) {
667 for (vend = rh->dictionary_vendors; vend != NULL; vend = nvend) {
671 rh->dictionary_attributes = NULL;
672 rh->dictionary_values = NULL;
673 rh->dictionary_vendors = NULL;
DICT_VALUE * rc_dict_getval(rc_handle const *rh, uint32_t value, char const *attrname)
DICT_ATTR * rc_dict_addattr(rc_handle *rh, char const *namestr, uint32_t value, int type, uint32_t vendorspec)
DICT_VENDOR * rc_dict_findvend(rc_handle const *rh, char const *vendorname)
int rc_read_dictionary_from_buffer(rc_handle *rh, char const *buf, size_t size)
DICT_VALUE * rc_dict_findval(rc_handle const *rh, char const *valname)
int rc_read_dictionary(rc_handle *rh, char const *filename)
DICT_VENDOR * rc_dict_getvend(rc_handle const *rh, uint32_t vendorspec)
DICT_ATTR * rc_dict_getattr(rc_handle const *rh, uint64_t attribute)
void rc_dict_free(rc_handle *rh)
DICT_VALUE * rc_dict_addval(rc_handle *rh, char const *attrstr, char const *namestr, uint32_t value)
DICT_VENDOR * rc_dict_addvend(rc_handle *rh, char const *namestr, uint32_t vendorspec)
DICT_ATTR * rc_dict_findattr(rc_handle const *rh, char const *attrname)
@ PW_TYPE_IPADDR
The attribute is an IPv4 address in host-byte order.
@ PW_TYPE_IPV6ADDR
The attribute is an 128-bit IPv6 address.
@ PW_TYPE_MAX
Maximum number of types (last+1)
@ PW_TYPE_IPV6PREFIX
The attribute is an IPv6 prefix; the lvalue will indicate its size.
@ PW_TYPE_INTEGER
The attribute is a 32-bit integer.
@ PW_TYPE_DATE
The attribute contains a 32-bit number indicating the seconds since epoch.
@ PW_TYPE_STRING
The attribute is a printable string.
rc_attr_type type
string, int, etc..
uint64_t value
attribute index and vendor number; use VENDOR() and ATTRID() to separate.
char name[RC_NAME_LENGTH+1]
attribute name.