2 * We don't use unput, so don't generate code for it.
7 * We don't read from the terminal.
9 %option never-interactive
12 * The language we're scanning is case-insensitive.
17 * Prefix scanner routines with "Radius" rather than "yy", so this scanner
18 * can coexist with other scanners.
20 %option prefix="Radius"
22 %option outfile="radius_dict.c"
27 * RADIUS dictionary parser
31 * Wireshark - Network traffic analyzer
32 * By Gerald Combs <gerald@wireshark.org>
33 * Copyright 1998 Gerald Combs
35 * This program is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU General Public License
37 * as published by the Free Software Foundation; either version 2
38 * of the License, or (at your option) any later version.
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
59 #include <epan/packet.h>
60 #include <epan/dissectors/packet-radius.h>
61 #include "radius_dict_lex.h"
62 #include <wsutil/file_util.h>
65 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated */
66 /* with YY_INPUT code generated by flex versions such as 2.5.35. */
67 #pragma warning (disable:4018)
71 #define MAX_INCLUDE_DEPTH 10
73 static void add_vendor(const gchar* name, guint32 vendor_id, guint vendor_type_octets, guint vendor_length_octets, gboolean vendor_has_flags);
74 static void add_value(const gchar* attrib_name,const gchar* value_repr, long value);
75 static void add_tlv(const gchar* name, const gchar* code, radius_attr_dissector_t type, const gchar* current_attr);
76 static void add_attribute(const gchar*,const gchar*, radius_attr_dissector_t,const gchar*, guint, gboolean, const gchar*);
78 static YY_BUFFER_STATE include_stack[10];
79 static int include_stack_ptr = 0;
81 static radius_dictionary_t* dict = NULL;
82 static GHashTable* value_strings = NULL; /* GArray(value_string) by attribute name */
84 static gchar* attr_name = NULL;
85 static gchar* attr_id = NULL;
86 static radius_attr_dissector_t* attr_type = NULL;
87 static gchar* attr_vendor = NULL;
88 static gchar* vendor_name = NULL;
89 static guint32 vendor_id = 0;
90 static guint vendor_type_octets = 1;
91 static guint vendor_length_octets = 1;
92 static gboolean vendor_has_flags = FALSE;
93 static gchar* value_repr = NULL;
94 static guint encrypted = 0;
95 static gboolean has_tag = FALSE;
96 static gchar* current_vendor = NULL;
97 static gchar* current_attr = NULL;
99 static GString* error = NULL;
100 static gchar* directory = NULL;
101 static int linenums[] = {1,1,1,1,1,1,1,1,1,1};
102 static gchar* fullpaths[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
106 /* Note: FreeRadius allows VENDOR, ATTRIBUTE and VALUE names to contain any non-blank character.
107 * Using a negated "blank character class" pattern below for those names fails for some reason
108 * so for now the patterns for each name type include those characters found for the corresponding
109 * name types in the FreeRadius dictionaries.
112 %START WS_OUT VENDOR VENDOR_W_NAME ATTR ATTR_W_NAME ATTR_W_ID ATTR_W_TYPE ATTR_W_VENDOR VALUE VALUE_W_ATTR VALUE_W_NAME INCLUDE JUNK BEGIN_VENDOR END_VENDOR VENDOR_W_ID VENDOR_W_FORMAT VENDOR_W_TYPE_OCTETS VENDOR_W_LENGTH_OCTETS VENDOR_W_CONTINUATION BEGIN_TLV END_TLV
119 <WS_OUT>VENDOR { BEGIN VENDOR; }
120 <WS_OUT>ATTRIBUTE { BEGIN ATTR; }
121 <WS_OUT>VALUE { BEGIN VALUE; }
122 <WS_OUT>\$INCLUDE { BEGIN INCLUDE; }
123 <WS_OUT>BEGIN-VENDOR { BEGIN BEGIN_VENDOR; }
124 <WS_OUT>END-VENDOR { BEGIN END_VENDOR; }
125 <WS_OUT>BEGIN-TLV { BEGIN BEGIN_TLV; }
126 <WS_OUT>END-TLV { BEGIN END_TLV; }
128 <BEGIN_VENDOR>[0-9a-z_-]+ {
129 if (current_vendor) {
130 g_free(current_vendor);
132 current_vendor = g_strdup(yytext);
136 if (current_vendor) {
137 g_free(current_vendor);
138 current_vendor = NULL;
143 <BEGIN_TLV>[0-9a-z_-]+ {
145 g_free(current_attr);
147 current_attr = g_strdup(yytext);
152 g_free(current_attr);
158 <VENDOR>[0-9a-z_-]+ {
159 vendor_name = g_strdup(yytext);
160 vendor_type_octets = 1;
161 vendor_length_octets = 1;
162 vendor_has_flags = FALSE;
165 <VENDOR_W_NAME>[0-9]+ {
166 vendor_id = strtol(yytext,NULL,10);
169 <VENDOR_W_NAME>0x[0-9a-f]+ {
170 vendor_id = strtol(yytext,NULL,16);
173 <VENDOR_W_ID>format= {
174 BEGIN VENDOR_W_FORMAT;
176 <VENDOR_W_FORMAT>[124] {
177 vendor_type_octets = strtol(yytext,NULL,10);
178 BEGIN VENDOR_W_TYPE_OCTETS;
180 <VENDOR_W_TYPE_OCTETS>,[012] {
181 vendor_length_octets = strtol(yytext+1,NULL,10);
182 BEGIN VENDOR_W_LENGTH_OCTETS;
184 <VENDOR_W_LENGTH_OCTETS>,c {
185 vendor_has_flags = TRUE;
186 BEGIN VENDOR_W_CONTINUATION;
188 <VENDOR_W_FORMAT>\n |
189 <VENDOR_W_TYPE_OCTETS>\n |
190 <VENDOR_W_LENGTH_OCTETS>\n |
191 <VENDOR_W_CONTINUATION>\n |
193 add_vendor(vendor_name, vendor_id, vendor_type_octets, vendor_length_octets, vendor_has_flags);
198 <ATTR>[0-9a-z_/.-]+ { attr_name = g_strdup(yytext); encrypted = 0; has_tag = FALSE; BEGIN ATTR_W_NAME; }
199 <ATTR_W_NAME>[0-9]+ { attr_id = g_strdup(yytext); BEGIN ATTR_W_ID;}
200 <ATTR_W_NAME>0x[0-9a-f]+ { attr_id = g_strdup_printf("%u",(int)strtoul(yytext,NULL,16)); BEGIN ATTR_W_ID;}
201 <ATTR_W_ID>integer { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
202 <ATTR_W_ID>string { attr_type = radius_string; BEGIN ATTR_W_TYPE; }
203 <ATTR_W_ID>octets { attr_type = radius_octets; BEGIN ATTR_W_TYPE; }
204 <ATTR_W_ID>ipaddr { attr_type = radius_ipaddr; BEGIN ATTR_W_TYPE; }
205 <ATTR_W_ID>ipv6addr { attr_type = radius_ipv6addr; BEGIN ATTR_W_TYPE; }
206 <ATTR_W_ID>ipv6prefix { attr_type = radius_ipv6prefix; BEGIN ATTR_W_TYPE; }
207 <ATTR_W_ID>ipxnet { attr_type = radius_ipxnet; BEGIN ATTR_W_TYPE; }
208 <ATTR_W_ID>date { attr_type = radius_date; BEGIN ATTR_W_TYPE; }
209 <ATTR_W_ID>abinary { attr_type = radius_abinary; BEGIN ATTR_W_TYPE; }
210 <ATTR_W_ID>ether { attr_type = radius_ether; BEGIN ATTR_W_TYPE; }
211 <ATTR_W_ID>ifid { attr_type = radius_ifid; BEGIN ATTR_W_TYPE; }
212 <ATTR_W_ID>byte { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
213 <ATTR_W_ID>short { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
214 <ATTR_W_ID>signed { attr_type = radius_signed; BEGIN ATTR_W_TYPE; }
215 <ATTR_W_ID>combo-ip { attr_type = radius_combo_ip; BEGIN ATTR_W_TYPE; }
216 <ATTR_W_ID>tlv { attr_type = radius_tlv; BEGIN ATTR_W_TYPE; }
217 <ATTR_W_ID>[0-9a-z_-]+ { attr_type = radius_octets; BEGIN ATTR_W_TYPE; }
218 <ATTR_W_TYPE>has_tag[,]? { has_tag = TRUE; }
219 <ATTR_W_TYPE>encrypt=[123][,]? { encrypted = strtol(yytext+8,NULL,10); }
220 <ATTR_W_TYPE>[0-9a-z_-]+=([^\n]*) ;
221 <ATTR_W_TYPE>[0-9a-z_-]+ {
222 attr_vendor = g_strdup(yytext);
223 add_attribute(attr_name,attr_id,attr_type,attr_vendor,encrypted,has_tag,current_attr);
233 add_attribute(attr_name,attr_id,attr_type,current_vendor,encrypted,has_tag,current_attr);
236 linenums[include_stack_ptr]++;
242 add_attribute(attr_name,attr_id,attr_type,attr_vendor,encrypted,has_tag,current_attr);
246 linenums[include_stack_ptr]++;
250 <VALUE>[0-9a-z_/-]+ { attr_name = g_strdup(yytext); BEGIN VALUE_W_ATTR; }
251 <VALUE_W_ATTR>[^[:blank:]]+ { value_repr = g_strdup(yytext); BEGIN VALUE_W_NAME; }
252 <VALUE_W_NAME>[0-9]+ { add_value(attr_name,value_repr,strtol(yytext,NULL,10)); g_free(attr_name); g_free(value_repr); BEGIN WS_OUT;}
253 <VALUE_W_NAME>0x[0-9a-f]+ { add_value(attr_name,value_repr,strtol(yytext,NULL,16)); g_free(attr_name); g_free(value_repr); BEGIN WS_OUT;}
255 <INCLUDE>[^[:blank:]\n]+ {
256 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
257 g_string_append_printf(error, "$INCLUDE files nested to deeply\n");
261 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
263 fullpaths[include_stack_ptr] = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
266 yyin = ws_fopen( fullpaths[include_stack_ptr], "r" );
270 g_string_append_printf(error, "Could not open file: '%s', error: %s\n", fullpaths[include_stack_ptr], g_strerror(errno) );
274 linenums[include_stack_ptr] = 1;
275 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
287 if ( --include_stack_ptr < 0 ) {
290 g_free(fullpaths[include_stack_ptr+1]);
291 fullpaths[include_stack_ptr+1] = NULL;
293 yy_delete_buffer( YY_CURRENT_BUFFER );
294 yy_switch_to_buffer(include_stack[include_stack_ptr]);
300 \n { linenums[include_stack_ptr]++; BEGIN WS_OUT; }
305 static void add_vendor(const gchar* name, guint32 vendor_id, guint vendor_type_octets, guint vendor_length_octets, gboolean vendor_has_flags) {
306 radius_vendor_info_t* v;
308 v = g_hash_table_lookup(dict->vendors_by_id, GUINT_TO_POINTER(vendor_id));
311 v = g_malloc(sizeof(radius_vendor_info_t));
312 v->attrs_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
317 /* Assume that the dictionary knows the 'ground truth' about the
318 * type/length/has_flags information and thus allow the dictionary to
319 * overwrite these values even for vendors that have already been loaded.
321 v->type_octets = vendor_type_octets;
322 v->length_octets = vendor_length_octets;
323 v->has_flags = vendor_has_flags;
326 g_free((gpointer) v->name);
327 v->name = g_strdup(name);
329 g_hash_table_insert(dict->vendors_by_id,GUINT_TO_POINTER(v->code),v);
330 g_hash_table_insert(dict->vendors_by_name, (gpointer) v->name, v);
333 static void add_attribute(const gchar* name, const gchar* codestr, radius_attr_dissector_t type, const gchar* vendor_name, guint crypt, gboolean tagged, const gchar* current_attr) {
334 radius_attr_info_t* a;
340 add_tlv(name, codestr, type, current_attr);
346 radius_vendor_info_t* v;
347 v = g_hash_table_lookup(dict->vendors_by_name,vendor_name);
350 g_string_append_printf(error, "Vendor: '%s', does not exist in %s:%i \n", vendor_name, fullpaths[include_stack_ptr], linenums[include_stack_ptr] );
354 by_id = v->attrs_by_id;
357 by_id = dict->attrs_by_id;
360 code=strtol(codestr, NULL, 10);
362 a=g_hash_table_lookup(by_id, GUINT_TO_POINTER(code));
365 a = g_malloc(sizeof(radius_attr_info_t));
380 a->tlvs_by_id = NULL;
383 g_free((gpointer) a->name);
384 a->name = g_strdup(name);
386 g_hash_table_insert(by_id, GUINT_TO_POINTER(code),a);
387 g_hash_table_insert(dict->attrs_by_name,(gpointer) (a->name),a);
390 static void add_tlv(const gchar* name, const gchar* codestr, radius_attr_dissector_t type, const gchar* current_attr) {
391 radius_attr_info_t* a;
392 radius_attr_info_t* s;
395 a = g_hash_table_lookup(dict->attrs_by_name, current_attr);
398 g_string_append_printf(error, "Attr: '%s', does not exist in %s:%i \n", current_attr, fullpaths[include_stack_ptr], linenums[include_stack_ptr]);
403 if (type == radius_tlv) {
404 g_string_append_printf(error, "sub-TLV: '%s', sub-TLV's type is specified as tlv in %s:%i \n", name, fullpaths[include_stack_ptr], linenums[include_stack_ptr]);
410 if (! a->tlvs_by_id) {
411 a->tlvs_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
414 code=strtol(codestr, NULL, 10);
416 s = g_hash_table_lookup(a->tlvs_by_id, GUINT_TO_POINTER(code));
419 s = g_malloc(sizeof(radius_attr_info_t));
435 s->tlvs_by_id = NULL;
438 g_free((gpointer) s->name);
439 s->name = g_strdup(name);
441 g_hash_table_insert(a->tlvs_by_id,GUINT_TO_POINTER(s->code),s);
442 g_hash_table_insert(dict->tlvs_by_name,(gpointer) (s->name),s);
445 void add_value(const gchar* attrib_name, const gchar* value_repr, long value) {
447 GArray* a = g_hash_table_lookup(value_strings,attrib_name);
450 a = g_array_new(TRUE,TRUE,sizeof(value_string));
451 g_hash_table_insert(value_strings,g_strdup(attrib_name),a);
455 v.strptr = g_strdup(value_repr);
457 g_array_append_val(a,v);
460 static void setup_tlvs(gpointer k _U_, gpointer v, gpointer p _U_) {
461 radius_attr_info_t* s = v;
469 if (g_hash_table_lookup_extended(value_strings, s->name, &key, &vs.p)) {
470 s->vs = (value_string*)(void *)vs.a->data;
471 g_array_free(vs.a, FALSE);
472 g_hash_table_remove(value_strings, key);
477 static void setup_attrs(gpointer k _U_, gpointer v, gpointer p _U_) {
478 radius_attr_info_t* a = v;
486 if (g_hash_table_lookup_extended(value_strings,a->name,&key,&vs.p) ) {
487 a->vs = (value_string*)(void *)vs.a->data;
488 g_array_free(vs.a,FALSE);
489 g_hash_table_remove(value_strings,key);
494 g_hash_table_foreach(a->tlvs_by_id, setup_tlvs, p);
498 static void setup_vendors(gpointer k _U_, gpointer v, gpointer p) {
499 radius_vendor_info_t* vnd = v;
501 g_hash_table_foreach(vnd->attrs_by_id,setup_attrs,p);
504 static gboolean destroy_value_strings(gpointer k, gpointer v, gpointer p _U_) {
505 value_string* vs = (value_string*)(void *)(((GArray*)v)->data);
509 for (;vs->strptr;vs++) {
510 g_free((void*)vs->strptr);
513 g_array_free(v,TRUE);
517 static gboolean destroy_tlvs(gpointer k _U_, gpointer v, gpointer p _U_) {
518 radius_attr_info_t* s = v;
521 g_free((gpointer) (s->name));
524 for(i=0; s->vs[i].strptr; i++) {
525 g_free((void *)s->vs[i].strptr);
527 g_free((void *)s->vs);
533 static gboolean destroy_attrs(gpointer k _U_, gpointer v, gpointer p _U_) {
534 radius_attr_info_t* a = v;
537 g_free((gpointer) (a->name));
539 g_hash_table_foreach_remove(a->tlvs_by_id, destroy_tlvs, p);
540 g_hash_table_destroy(a->tlvs_by_id);
544 for(i=0; a->vs[i].strptr; i++) {
545 g_free((void *)a->vs[i].strptr);
547 g_free((void *)a->vs);
553 static gboolean destroy_vendors(gpointer k _U_, gpointer v, gpointer p) {
554 radius_vendor_info_t* vnd = v;
555 g_free( (gpointer) vnd->name);
556 g_hash_table_foreach_remove(vnd->attrs_by_id,destroy_attrs,p);
557 g_hash_table_destroy(vnd->attrs_by_id);
562 static void destroy_dict(radius_dictionary_t* d) {
563 g_hash_table_foreach_remove(d->attrs_by_id,destroy_attrs,NULL);
564 g_hash_table_foreach_remove(d->vendors_by_id,destroy_vendors,NULL);
565 g_hash_table_destroy(d->vendors_by_id);
566 g_hash_table_destroy(d->attrs_by_id);
567 g_hash_table_destroy(d->vendors_by_name);
568 g_hash_table_destroy(d->attrs_by_name);
572 gboolean radius_load_dictionary (radius_dictionary_t* d, gchar* dir, const gchar* filename, gchar** err_str) {
578 fullpaths[include_stack_ptr] = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
581 error = g_string_new("");
583 yyin = ws_fopen(fullpaths[include_stack_ptr],"r");
586 g_string_append_printf(error, "Could not open file: '%s', error: %s\n", fullpaths[include_stack_ptr], g_strerror(errno) );
587 g_free(fullpaths[include_stack_ptr]);
588 *err_str = error->str;
589 g_string_free(error,FALSE);
593 value_strings = g_hash_table_new(g_str_hash,g_str_equal);
599 if (yyin != NULL) fclose(yyin);
602 for (i=0; i < 10; i++) {
603 if (fullpaths[i]) g_free(fullpaths[i]);
606 g_hash_table_foreach(dict->attrs_by_id,setup_attrs,NULL);
607 g_hash_table_foreach(dict->vendors_by_id,setup_vendors,NULL);
608 g_hash_table_foreach_remove(value_strings,destroy_value_strings,NULL);
610 if (error->len > 0) {
611 *err_str = error->str;
612 g_string_free(error,FALSE);
617 g_string_free(error,TRUE);
623 * We want to stop processing when we get to the end of the input.
624 * (%option noyywrap is not used because if used then
625 * some flex versions (eg: 2.5.35) generate code which causes
626 * warnings by the Windows VC compiler).