2 * We don't use input, so don't generate code for it.
7 * We don't use unput, so don't generate code for it.
12 * We don't read from the terminal.
14 %option never-interactive
17 * The language we're scanning is case-insensitive.
22 * Prefix scanner routines with "Radius" rather than "yy", so this scanner
23 * can coexist with other scanners.
25 %option prefix="Radius"
27 %option outfile="radius_dict.c"
32 * RADIUS dictionary parser
36 * Wireshark - Network traffic analyzer
37 * By Gerald Combs <gerald@wireshark.org>
38 * Copyright 1998 Gerald Combs
40 * This program is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU General Public License
42 * as published by the Free Software Foundation; either version 2
43 * of the License, or (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
62 #include <epan/packet.h>
63 #include <epan/dissectors/packet-radius.h>
64 #include "radius_dict_lex.h"
65 #include <wsutil/file_util.h>
68 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated */
69 /* with YY_INPUT code generated by flex versions such as 2.5.35. */
70 #pragma warning (disable:4018)
74 #define MAX_INCLUDE_DEPTH 10
76 static void add_vendor(const gchar* name, guint32 id, guint type_octets, guint length_octets, gboolean has_flags);
77 static void add_value(const gchar* attrib_name,const gchar* repr, long value);
78 static void add_tlv(const gchar* name, const gchar* code, radius_attr_dissector_t type, const gchar* attr);
79 static void add_attribute(const gchar*,const gchar*, radius_attr_dissector_t,const gchar*, guint, gboolean, const gchar*);
81 static YY_BUFFER_STATE include_stack[10];
82 static int include_stack_ptr = 0;
84 static radius_dictionary_t* dict = NULL;
85 static GHashTable* value_strings = NULL; /* GArray(value_string) by attribute name */
87 static gchar* attr_name = NULL;
88 static gchar* attr_id = NULL;
89 static radius_attr_dissector_t* attr_type = NULL;
90 static gchar* attr_vendor = NULL;
91 static gchar* vendor_name = NULL;
92 static guint32 vendor_id = 0;
93 static guint vendor_type_octets = 1;
94 static guint vendor_length_octets = 1;
95 static gboolean vendor_has_flags = FALSE;
96 static gchar* value_repr = NULL;
97 static guint encrypted = 0;
98 static gboolean has_tag = FALSE;
99 static gchar* current_vendor = NULL;
100 static gchar* current_attr = NULL;
102 static GString* error = NULL;
103 static gchar* directory = NULL;
104 static int linenums[] = {1,1,1,1,1,1,1,1,1,1};
105 static gchar* fullpaths[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
109 /* Note: FreeRadius allows VENDOR, ATTRIBUTE and VALUE names to contain any non-blank character.
110 * Using a negated "blank character class" pattern below for those names fails for some reason
111 * so for now the patterns for each name type include those characters found for the corresponding
112 * name types in the FreeRadius dictionaries.
115 %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
122 <WS_OUT>VENDOR { BEGIN VENDOR; }
123 <WS_OUT>ATTRIBUTE { BEGIN ATTR; }
124 <WS_OUT>VALUE { BEGIN VALUE; }
125 <WS_OUT>\$INCLUDE { BEGIN INCLUDE; }
126 <WS_OUT>BEGIN-VENDOR { BEGIN BEGIN_VENDOR; }
127 <WS_OUT>END-VENDOR { BEGIN END_VENDOR; }
128 <WS_OUT>BEGIN-TLV { BEGIN BEGIN_TLV; }
129 <WS_OUT>END-TLV { BEGIN END_TLV; }
131 <BEGIN_VENDOR>[0-9a-z_-]+ {
132 if (current_vendor) {
133 g_free(current_vendor);
135 current_vendor = g_strdup(yytext);
139 if (current_vendor) {
140 g_free(current_vendor);
141 current_vendor = NULL;
146 <BEGIN_TLV>[0-9a-z_-]+ {
148 g_free(current_attr);
150 current_attr = g_strdup(yytext);
155 g_free(current_attr);
161 <VENDOR>[0-9a-z_-]+ {
162 vendor_name = g_strdup(yytext);
163 vendor_type_octets = 1;
164 vendor_length_octets = 1;
165 vendor_has_flags = FALSE;
168 <VENDOR_W_NAME>[0-9]+ {
169 vendor_id = strtol(yytext,NULL,10);
172 <VENDOR_W_NAME>0x[0-9a-f]+ {
173 vendor_id = strtol(yytext,NULL,16);
176 <VENDOR_W_ID>format= {
177 BEGIN VENDOR_W_FORMAT;
179 <VENDOR_W_FORMAT>[124] {
180 vendor_type_octets = strtol(yytext,NULL,10);
181 BEGIN VENDOR_W_TYPE_OCTETS;
183 <VENDOR_W_TYPE_OCTETS>,[012] {
184 vendor_length_octets = strtol(yytext+1,NULL,10);
185 BEGIN VENDOR_W_LENGTH_OCTETS;
187 <VENDOR_W_LENGTH_OCTETS>,c {
188 vendor_has_flags = TRUE;
189 BEGIN VENDOR_W_CONTINUATION;
191 <VENDOR_W_FORMAT>\n |
192 <VENDOR_W_TYPE_OCTETS>\n |
193 <VENDOR_W_LENGTH_OCTETS>\n |
194 <VENDOR_W_CONTINUATION>\n |
196 add_vendor(vendor_name, vendor_id, vendor_type_octets, vendor_length_octets, vendor_has_flags);
201 <ATTR>[0-9a-z_/.-]+ { attr_name = g_strdup(yytext); encrypted = 0; has_tag = FALSE; BEGIN ATTR_W_NAME; }
202 <ATTR_W_NAME>[0-9]+ { attr_id = g_strdup(yytext); BEGIN ATTR_W_ID;}
203 <ATTR_W_NAME>0x[0-9a-f]+ { attr_id = g_strdup_printf("%u",(int)strtoul(yytext,NULL,16)); BEGIN ATTR_W_ID;}
204 <ATTR_W_ID>integer { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
205 <ATTR_W_ID>string { attr_type = radius_string; BEGIN ATTR_W_TYPE; }
206 <ATTR_W_ID>octets { attr_type = radius_octets; BEGIN ATTR_W_TYPE; }
207 <ATTR_W_ID>ipaddr { attr_type = radius_ipaddr; BEGIN ATTR_W_TYPE; }
208 <ATTR_W_ID>ipv6addr { attr_type = radius_ipv6addr; BEGIN ATTR_W_TYPE; }
209 <ATTR_W_ID>ipv6prefix { attr_type = radius_ipv6prefix; BEGIN ATTR_W_TYPE; }
210 <ATTR_W_ID>ipxnet { attr_type = radius_ipxnet; BEGIN ATTR_W_TYPE; }
211 <ATTR_W_ID>date { attr_type = radius_date; BEGIN ATTR_W_TYPE; }
212 <ATTR_W_ID>abinary { attr_type = radius_abinary; BEGIN ATTR_W_TYPE; }
213 <ATTR_W_ID>ether { attr_type = radius_ether; BEGIN ATTR_W_TYPE; }
214 <ATTR_W_ID>ifid { attr_type = radius_ifid; BEGIN ATTR_W_TYPE; }
215 <ATTR_W_ID>byte { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
216 <ATTR_W_ID>short { attr_type = radius_integer; BEGIN ATTR_W_TYPE; }
217 <ATTR_W_ID>signed { attr_type = radius_signed; BEGIN ATTR_W_TYPE; }
218 <ATTR_W_ID>combo-ip { attr_type = radius_combo_ip; BEGIN ATTR_W_TYPE; }
219 <ATTR_W_ID>tlv { attr_type = radius_tlv; BEGIN ATTR_W_TYPE; }
220 <ATTR_W_ID>[0-9a-z_-]+ { attr_type = radius_octets; BEGIN ATTR_W_TYPE; }
221 <ATTR_W_TYPE>has_tag[,]? { has_tag = TRUE; }
222 <ATTR_W_TYPE>encrypt=[123][,]? { encrypted = strtol(yytext+8,NULL,10); }
223 <ATTR_W_TYPE>[0-9a-z_-]+=([^\n]*) ;
224 <ATTR_W_TYPE>[0-9a-z_-]+ {
225 attr_vendor = g_strdup(yytext);
226 add_attribute(attr_name,attr_id,attr_type,attr_vendor,encrypted,has_tag,current_attr);
236 add_attribute(attr_name,attr_id,attr_type,current_vendor,encrypted,has_tag,current_attr);
239 linenums[include_stack_ptr]++;
245 add_attribute(attr_name,attr_id,attr_type,attr_vendor,encrypted,has_tag,current_attr);
249 linenums[include_stack_ptr]++;
253 <VALUE>[0-9a-z_/-]+ { attr_name = g_strdup(yytext); BEGIN VALUE_W_ATTR; }
254 <VALUE_W_ATTR>[^[:blank:]]+ { value_repr = g_strdup(yytext); BEGIN VALUE_W_NAME; }
255 <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;}
256 <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;}
258 <INCLUDE>[^[:blank:]\n]+ {
259 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
260 g_string_append_printf(error, "$INCLUDE files nested to deeply\n");
264 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
266 fullpaths[include_stack_ptr] = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
269 yyin = ws_fopen( fullpaths[include_stack_ptr], "r" );
273 g_string_append_printf(error,
274 "Could not open file: '%s', error: %s\n",
275 fullpaths[include_stack_ptr],
278 g_string_append_printf(error,
279 "Could not open file: '%s', no errno\n",
280 fullpaths[include_stack_ptr]);
282 g_free(fullpaths[include_stack_ptr]);
283 fullpaths[include_stack_ptr] = NULL;
286 linenums[include_stack_ptr] = 1;
287 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
299 if ( --include_stack_ptr < 0 ) {
302 g_free(fullpaths[include_stack_ptr+1]);
303 fullpaths[include_stack_ptr+1] = NULL;
305 yy_delete_buffer( YY_CURRENT_BUFFER );
306 yy_switch_to_buffer(include_stack[include_stack_ptr]);
312 \n { linenums[include_stack_ptr]++; BEGIN WS_OUT; }
317 static void add_vendor(const gchar* name, guint32 id, guint type_octets, guint length_octets, gboolean has_flags) {
318 radius_vendor_info_t* v;
320 v = (radius_vendor_info_t *)g_hash_table_lookup(dict->vendors_by_id, GUINT_TO_POINTER(id));
323 v = g_new(radius_vendor_info_t,1);
324 v->attrs_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
329 /* Assume that the dictionary knows the 'ground truth' about the
330 * type/length/has_flags information and thus allow the dictionary to
331 * overwrite these values even for vendors that have already been loaded.
333 v->type_octets = type_octets;
334 v->length_octets = length_octets;
335 v->has_flags = has_flags;
338 g_free((gpointer) v->name);
339 v->name = g_strdup(name);
341 g_hash_table_insert(dict->vendors_by_id,GUINT_TO_POINTER(v->code),v);
342 g_hash_table_insert(dict->vendors_by_name, (gpointer) v->name, v);
345 static void add_attribute(const gchar* name, const gchar* codestr, radius_attr_dissector_t type, const gchar* vendor, guint encrypted_flag, gboolean tagged, const gchar* attr) {
346 radius_attr_info_t* a;
349 const gchar *tmpName = NULL;
353 add_tlv(name, codestr, type, attr);
359 radius_vendor_info_t* v;
360 v = (radius_vendor_info_t *)g_hash_table_lookup(dict->vendors_by_name,vendor);
363 g_string_append_printf(error, "Vendor: '%s', does not exist in %s:%i \n", vendor, fullpaths[include_stack_ptr], linenums[include_stack_ptr] );
367 by_id = v->attrs_by_id;
370 by_id = dict->attrs_by_id;
373 code=strtol(codestr, NULL, 10);
375 a=(radius_attr_info_t*)g_hash_table_lookup(by_id, GUINT_TO_POINTER(code));
378 a = g_new(radius_attr_info_t,1);
384 a->encrypt = encrypted_flag;
393 a->tlvs_by_id = NULL;
398 a->name = g_strdup(name);
400 g_hash_table_insert(by_id, GUINT_TO_POINTER(code),a);
401 g_hash_table_insert(dict->attrs_by_name,(gpointer) (a->name),a);
403 /* Don't free the old name until after the hash_table ops, since it
404 seems to end up being used in there somewhere, causing valgrind
405 errors. https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=7803 */
407 g_free((gpointer) tmpName);
411 static void add_tlv(const gchar* name, const gchar* codestr, radius_attr_dissector_t type, const gchar* attr) {
412 radius_attr_info_t* a;
413 radius_attr_info_t* s;
416 a = (radius_attr_info_t*)g_hash_table_lookup(dict->attrs_by_name, attr);
419 g_string_append_printf(error, "Attr: '%s', does not exist in %s:%i \n", attr, fullpaths[include_stack_ptr], linenums[include_stack_ptr]);
424 if (type == radius_tlv) {
425 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]);
431 if (! a->tlvs_by_id) {
432 a->tlvs_by_id = g_hash_table_new(g_direct_hash,g_direct_equal);
435 code=strtol(codestr, NULL, 10);
437 s = (radius_attr_info_t*)g_hash_table_lookup(a->tlvs_by_id, GUINT_TO_POINTER(code));
440 s = g_new(radius_attr_info_t,1);
456 s->tlvs_by_id = NULL;
459 g_free((gpointer) s->name);
460 s->name = g_strdup(name);
462 g_hash_table_insert(a->tlvs_by_id,GUINT_TO_POINTER(s->code),s);
463 g_hash_table_insert(dict->tlvs_by_name,(gpointer) (s->name),s);
466 void add_value(const gchar* attrib_name, const gchar* repr, long value) {
468 GArray* a = (GArray*)g_hash_table_lookup(value_strings,attrib_name);
471 a = g_array_new(TRUE,TRUE,sizeof(value_string));
472 g_hash_table_insert(value_strings,g_strdup(attrib_name),a);
476 v.strptr = g_strdup(repr);
478 g_array_append_val(a,v);
481 static void setup_tlvs(gpointer k _U_, gpointer v, gpointer p _U_) {
482 radius_attr_info_t* s = (radius_attr_info_t*)v;
490 if (g_hash_table_lookup_extended(value_strings, s->name, &key, &vs.p)) {
491 s->vs = (value_string*)(void *)vs.a->data;
492 g_array_free(vs.a, FALSE);
493 g_hash_table_remove(value_strings, key);
498 static void setup_attrs(gpointer k _U_, gpointer v, gpointer p _U_) {
499 radius_attr_info_t* a = (radius_attr_info_t*)v;
507 if (g_hash_table_lookup_extended(value_strings,a->name,&key,&vs.p) ) {
508 a->vs = (value_string*)(void *)vs.a->data;
509 g_array_free(vs.a,FALSE);
510 g_hash_table_remove(value_strings,key);
515 g_hash_table_foreach(a->tlvs_by_id, setup_tlvs, p);
519 static void setup_vendors(gpointer k _U_, gpointer v, gpointer p) {
520 radius_vendor_info_t* vnd = (radius_vendor_info_t*)v;
522 g_hash_table_foreach(vnd->attrs_by_id,setup_attrs,p);
525 static gboolean destroy_value_strings(gpointer k, gpointer v, gpointer p _U_) {
526 value_string* vs = (value_string*)(void *)(((GArray*)v)->data);
530 for (;vs->strptr;vs++) {
531 g_free((void*)vs->strptr);
534 g_array_free((GArray*)v,TRUE);
538 gboolean radius_load_dictionary (radius_dictionary_t* d, gchar* dir, const gchar* filename, gchar** err_str) {
544 fullpaths[include_stack_ptr] = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
547 error = g_string_new("");
549 yyin = ws_fopen(fullpaths[include_stack_ptr],"r");
552 g_string_append_printf(error, "Could not open file: '%s', error: %s\n", fullpaths[include_stack_ptr], g_strerror(errno) );
553 g_free(fullpaths[include_stack_ptr]);
554 *err_str = error->str;
555 g_string_free(error,FALSE);
559 value_strings = g_hash_table_new(g_str_hash,g_str_equal);
565 if (yyin != NULL) fclose(yyin);
568 for (i=0; i < 10; i++) {
569 if (fullpaths[i]) g_free(fullpaths[i]);
572 g_hash_table_foreach(dict->attrs_by_id,setup_attrs,NULL);
573 g_hash_table_foreach(dict->vendors_by_id,setup_vendors,NULL);
574 g_hash_table_foreach_remove(value_strings,destroy_value_strings,NULL);
576 if (error->len > 0) {
577 *err_str = error->str;
578 g_string_free(error,FALSE);
582 g_string_free(error,TRUE);
588 * We want to stop processing when we get to the end of the input.
589 * (%option noyywrap is not used because if used then
590 * some flex versions (eg: 2.5.35) generate code which causes
591 * warnings by the Windows VC compiler).
599 * Editor modelines - http://www.wireshark.org/tools/modelines.html
604 * indent-tabs-mode: t
607 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
608 * :indentSize=8:tabSize=8:noTabs=false: