Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package Radius::Dictionary;
use strict;
use warnings;
use vars qw($VERSION);
# $Id: Dictionary.pm,v 1.11 2007/04/26 20:20:02 lem Exp $
$VERSION = '1.55';
sub new {
my $class = shift;
my $self = {
rvsattr => {},
vsattr => {},
vsaval => {},
rvsaval => {},
attr => {},
rattr => {},
val => {},
rval => {},
vendors => {},
packet => undef, # Fall back to default
rpacket => undef, # Fall back to default
};
bless $self, $class;
$self->readfile($_) for @_; # Read all given dictionaries
return $self;
}
sub readfile {
my ($self, $filename) = @_;
open(DICT, "< $filename") or return undef;
while (defined(my $l = <DICT>)) {
next if $l =~ /^\#/;
next unless my @l = split /\s+/, $l;
if ($l[0] =~ m/^vendor$/i)
{
if (defined $l[1] and defined $l[2] and $l[2] =~ /^[xo0-9]+$/)
{
if (substr($l[2],0,1) eq "0") { #allow hex or octal
my $num = lc($l[2]);
$num =~ s/^0b//;
$l[2] = oct($num);
}
$self->{vendors}->{$l[1]} = $l[2];
}
else
{
warn "Garbled VENDOR line $l\n";
}
}
elsif ($l[0] =~ m/^attribute$/i)
{
if (@l == 4)
{
$self->{attr}->{$l[1]} = [@l[2,3]];
$self->{rattr}->{$l[2]} = [@l[1,3]];
}
elsif (@l == 5) # VENDORATTR
{
if (substr($l[2],0,1) eq "0") { #allow hex or octal
my $num = lc($l[2]);
$num =~ s/^0b//;
$l[2] = oct($num);
}
if (exists $self->{vendors}->{$l[4]})
{
$self->{vsattr}->{$self->{vendors}->{$l[4]}}->{$l[1]}
= [@l[2, 3]];
$self->{rvsattr}->{$self->{vendors}->{$l[4]}}->{$l[2]}
= [@l[1, 3]];
}
elsif ($l[4] =~ m/^\d+$/)
{
$self->{vsattr}->{$l[4]}->{$l[1]} = [@l[2, 3]];
$self->{rvsattr}->{$l[4]}->{$l[2]} = [@l[1, 3]];
}
else
{
warn "Warning: Unknown vendor $l[4]\n";
}
}
}
elsif ($l[0] =~ m/^value$/i) {
if (exists $self->{attr}->{$l[1]}) {
$self->{val}->{$self->{attr}->{$l[1]}->[0]}->{$l[2]} = $l[3];
$self->{rval}->{$self->{attr}->{$l[1]}->[0]}->{$l[3]} = $l[2];
}
else {
for my $v (keys %{$self->{vsattr}})
{
if (defined $self->{vsattr}->{$v}->{$l[1]})
{
$self->{vsaval}->{$v}->{$self->{vsattr}->{$v}
->{$l[1]}->[0]}->{$l[2]}
= $l[3];
$self->{rvsaval}->{$v}->{$self->{vsattr}->{$v}
->{$l[1]}->[0]}->{$l[3]}
= $l[2];
}
}
}
}
elsif ($l[0] =~ m/^vendorattr$/i) {
if (substr($l[3],0,1) eq "0") { #allow hex or octal
my $num = lc($l[3]);
$num =~ s/^0b//;
$l[3] = oct($num);
}
if (exists $self->{vendors}->{$l[1]})
{
$self->{vsattr}->{$self->{vendors}->{$l[1]}}->{$l[2]}
= [@l[3, 4]];
$self->{rvsattr}->{$self->{vendors}->{$l[1]}}->{$l[3]}
= [@l[2, 4]];
}
elsif ($l[1] =~ m/^\d+$/)
{
$self->{vsattr}->{$l[1]}->{$l[2]} = [@l[3, 4]];
$self->{rvsattr}->{$l[1]}->{$l[3]} = [@l[2, 4]];
}
else
{
warn "Warning: Unknown vendor $l[1]\n";
}
}
elsif ($l[0] =~ m/^vendorvalue$/i) {
if (substr($l[4],0,1) eq "0")
{ #allow hex or octal
my $num = lc($l[4]);
$num =~ s/^0b//;
$l[4] = oct($num);
}
if (exists $self->{vendors}->{$l[1]})
{
$self->{vsaval}->{$self->{vendors}->{$l[1]}}
->{$self->{vsattr}->{$self->{vendors}->{$l[1]}}
->{$l[2]}->[0]}->{$l[3]} = $l[4];
$self->{rvsaval}->{$self->{vendors}->{$l[1]}}
->{$self->{vsattr}->{$self->{vendors}->{$l[1]}}
->{$l[2]}->[0]}->{$l[4]} = $l[3];
}
elsif ($l[1] =~ m/^\d+$/)
{
$self->{vsaval}->{$l[1]}->{$self->{vsattr}->{$l[1]}->{$l[2]}
->[0]}->{$l[3]} = $l[4];
$self->{rvsaval}->{$l[1]}->{$self->{vsattr}->{$l[1]}->{$l[2]}
->[0]}->{$l[4]} = $l[3];
}
else {
warn "Warning: $filename contains vendor value for ",
"unknown vendor attribute - ignored ",
"\"$l[1]\"\n $l";
}
}
elsif (lc($l[0]) eq 'packet') {
my ($name, $value) = @l[1,2];
$self->{packet}{$name} = $value;
$self->{rpacket}{$value} = $name;
}
else {
warn "Warning: Weird dictionary line: $l\n";
}
}
close DICT;
}
# Accessors for standard attributes
sub vendor_num { $_[0]->{vendors}->{$_[1]}; }
sub attr_num { $_[0]->{attr}->{$_[1]}->[0]; }
sub attr_type { $_[0]->{attr}->{$_[1]}->[1]; }
sub attr_name { $_[0]->{rattr}->{$_[1]}->[0]; }
sub attr_numtype { $_[0]->{rattr}->{$_[1]}->[1]; }
sub attr_has_val { $_[0]->{val}->{$_[1]}; }
sub val_has_name { $_[0]->{rval}->{$_[1]}; }
sub val_num { $_[0]->{val}->{$_[1]}->{$_[2]}; }
sub val_name { $_[0]->{rval}->{$_[1]}->{$_[2]}; }
sub val_tag { $_[0]->{val}->{$_[1]}->{$_[3]}; }
# Accessors for Vendor-Specific Attributes
sub vsattr_num { $_[0]->{vsattr}->{$_[1]}->{$_[2]}->[0]; }
sub vsattr_type { $_[0]->{vsattr}->{$_[1]}->{$_[2]}->[1]; }
sub vsattr_name { $_[0]->{rvsattr}->{$_[1]}->{$_[2]}->[0]; }
sub vsattr_numtype { $_[0]->{rvsattr}->{$_[1]}->{$_[2]}->[1]; }
sub vsattr_has_val { $_[0]->{vsaval}->{$_[1]}->{$_[2]}; }
sub vsaval_has_name { $_[0]->{rvsaval}->{$_[1]}->{$_[2]}; }
sub vsaval_has_tval { $_[0]->{vsaval}->{$_[1]}->{$_[2]}->[0]; }
sub vsaval_has_tag { $_[0]->{vsaval}->{$_[1]}->{$_[2]}->[1]; }
sub vsaval_num { $_[0]->{vsaval}->{$_[1]}->{$_[2]}->{$_[3]}; }
sub vsaval_name { $_[0]->{rvsaval}->{$_[1]}->{$_[2]}->{$_[3]}; }
# Accessors for packet types. Fall-back to defaults if the case.
# Defaults taken from http://www.iana.org/assignments/radius-types
# as of Oct 21, 2006
my %default_packets = (
'Access-Request' => 1, # [RFC2865]
'Access-Accept' => 2, # [RFC2865]
'Access-Reject' => 3, # [RFC2865]
'Accounting-Request' => 4, # [RFC2865]
'Accounting-Response' => 5, # [RFC2865]
'Accounting-Status' => 6, # [RFC2882] (now Interim Accounting)
'Interim-Accounting' => 6, # see previous note
'Password-Request' => 7, # [RFC2882]
'Password-Ack' => 8, # [RFC2882]
'Password-Reject' => 9, # [RFC2882]
'Accounting-Message' => 10, # [RFC2882]
'Access-Challenge' => 11, # [RFC2865]
'Status-Server' => 12, # (experimental) [RFC2865]
'Status-Client' => 13, # (experimental) [RFC2865]
'Resource-Free-Request' => 21, # [RFC2882]
'Resource-Free-Response' => 22, # [RFC2882]
'Resource-Query-Request' => 23, # [RFC2882]
'Resource-Query-Response' => 24, # [RFC2882]
'Alternate-Resource-Reclaim-Request' => 25, # [RFC2882]
'NAS-Reboot-Request' => 26, # [RFC2882]
'NAS-Reboot-Response' => 27, # [RFC2882]
# 28 Reserved
'Next-Passcode' => 29, # [RFC2882]
'New-Pin' => 30, # [RFC2882]
'Terminate-Session' => 31, # [RFC2882]
'Password-Expired' => 32, # [RFC2882]
'Event-Request' => 33, # [RFC2882]
'Event-Response' => 34, # [RFC2882]
'Disconnect-Request' => 40, # [RFC3575]
'Disconnect-ACK' => 41, # [RFC3575]
'Disconnect-NAK' => 42, # [RFC3575]
'CoA-Request' => 43, # [RFC3575]
'CoA-ACK' => 44, # [RFC3575]
'CoA-NAK' => 45, # [RFC3575]
'IP-Address-Allocate' => 50, # [RFC2882]
'IP-Address-Release' => 51, # [RFC2882]
# 250-253 Experimental Use
# 254 Reserved
# 255 Reserved [RFC2865]
);
# Reverse defaults. Remember that code #6 has a double mapping, force
# to Interim-Accouting
my %default_rpackets
= map { $default_packets{$_} => $_ } keys %default_packets;
$default_rpackets{6} = 'Interim-Accounting';
# Get full hashes
sub packet_numbers { %{ $_[0]->{packet} || \%default_packets } }
sub packet_names { %{ $_[0]->{rpacket} || \%default_rpackets }; }
# Single resolution, I'm taking care of avoiding auto-vivification
sub packet_hasname {
my $href = $_[0]->{packet} || \%default_packets;
my $ok = exists $href->{$_[1]};
return $ok unless wantarray;
# return both answer and the resolution
return ($ok, $ok ? $href->{$_[1]} : undef);
}
sub packet_hasnum {
my $href = $_[0]->{rpacket} || \%default_rpackets;
my $ok = exists $href->{$_[1]};
return $ok unless wantarray;
# return both answer and the resolution
return ($ok, $ok ? $href->{$_[1]} : undef);
}
# Note: crossed, as it might not be immediately evident
sub packet_num { ($_[0]->packet_hasname($_[1]))[1]; }
sub packet_name { ($_[0]->packet_hasnum($_[1]))[1]; }
1;
__END__
=head1 NAME
Net::Radius::Dictionary - RADIUS dictionary parser
=head1 SYNOPSIS
use Net::Radius::Dictionary;
my $dict = new Net::Radius::Dictionary "/etc/radius/dictionary";
$dict->readfile("/some/other/file");
my $num = $dict->attr_num('User-Name');
my $name = $dict->attr_name(1);
my $vsa_num = $dict->vsattr_num(9, 'cisco-avpair');
my $vsa_name = $dict->vsattr_name(9, 1);
=head1 DESCRIPTION
This is a simple module that reads a RADIUS dictionary file and
parses it, allowing conversion between dictionary names and numbers.
Vendor-Specific attributes are supported in a way consistent to the
standards.
A few earlier versions of this module attempted to make dictionaries
case-insensitive. This proved to be a very bad decision. From this
version on, this tendency is reverted: Dictionaries and its contents
are to be case-sensitive to prevent random, hard to debug failures in
production code.
=head2 METHODS
=over
=item B<new($dict_file, ...)>
Returns a new instance of a Net::Radius::Dictionary object. This
object will have no attributes defined, as expected.
If given an (optional) list of filenames, it calls I<readfile> for you
for all of them, in the given order.
=item B<-E<gt>readfile($dict_file)>
Parses a dictionary file and learns the mappings to use. It can be
called multiple times for the same object. The result will be that new
entries will override older ones, thus you could load a default
dictionary and then have a smaller dictionary that override specific
entries.
=item B<-E<gt>vendor_num($vendorname)>
Return the vendor number for the given vendor name.
=item B<-E<gt>attr_num($attrname)>
Returns the number of the named attribute.
=item B<-E<gt>attr_type($attrname)>
Returns the type (I<string>, I<integer>, I<ipaddr>, or I<time>) of the
named attribute.
=item B<-E<gt>attr_name($attrnum)>
Returns the name of the attribute with the given number.
=item B<-E<gt>attr_numtype($attrnum)>
Returns the type of the attribute with the given number.
=item B<-E<gt>attr_has_val($attrnum)>
Returns a true or false value, depending on whether or not the numbered
attribute has any known value constants.
=item B<-E<gt>val_has_name($attrnum)>
Alternate (bad) name for I<attr_has_val>.
=item B<-E<gt>val_num($attrnum, $valname)>
Returns the number of the named value for the attribute number supplied.
=item B<-E<gt>val_name($attrnum, $valnumber)>
Returns the name of the numbered value for the attribute number supplied.
=back
There is an equivalent family of accessor methods for Vendor-Specific
attributes and its values. Those methods are identical to their standard
attributes counterparts with two exceptions. Their names have a
I<vsa> prepended to the accessor name and the first argument to each one
is the vendor code on which they apply.
=head1 CAVEATS
This module is mostly for the internal use of Net::Radius::Packet, and
may otherwise cause insanity and/or blindness if studied.
=head1 AUTHOR
Christopher Masto <chris@netmonger.net>,
Luis E. Muñoz <luismunoz@cpan.org> contributed the VSA code.
=head1 SEE ALSO
Net::Radius::Packet
=cut