Class: Radius::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/radius/dictionary.rb

Overview

This is a simple class that can read RADIUS dictionary files and parse them, allowing conversion between dictionary names and numbers. Vendor-specific attributes are supported in a way consistent with the standards.

This class is patterned after the Net::Radius::Dictionary Perl module written by Christopher Masto ([email protected]) and Luis E. Munoz ([email protected])

Instance Method Summary collapse

Constructor Details

#initialize(dictionary_path = nil) ⇒ Dictionary

Initialize all the instance variables. All variables start out as empty versions of the appropriate type.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/radius/dictionary.rb', line 39

def initialize(dictionary_path = nil)
  @attr = Hash.new(nil)
  @rattr = Array.new
  @val = Array.new
  @rval = Array.new
  @vsattr = Array.new
  @rvsattr = Array.new
  @vsaval = Array.new
  @rvsaval = Array.new
  dictionary_path = File.dirname(__FILE__) + '/../../dictionary' unless dictionary_path
  File.open(dictionary_path, 'r') {|f| read(f)}
end

Instance Method Details

#attr_has_val(attrnum) ⇒ Object

Given an attribute number, return true or false depending on whether or not a value has been given for it.

Parameters

attrnum

Number of the attribute whose definition is known

Return Value

True or false depending on whether some value has been given to the attribute



209
210
211
# File 'lib/radius/dictionary.rb', line 209

def attr_has_val(attrnum)
  return(@val[attrnum] != nil)
end

#attr_name(attrnum) ⇒ Object

Given an attribute number, return the name corresponding to it, based on the dictionary file(s) that have been read, the reverse of the attr_num method.

Parameters

attrname

Name of the attribute whose number is desired.

Return Value

The number corresponding to the appropriate attribute name given.



176
177
178
179
180
181
# File 'lib/radius/dictionary.rb', line 176

def attr_name(attrnum)
  if (@rattr[attrnum] == nil || @rattr[attrnum][0] == nil)
    return(nil)
  end
  return(@rattr[attrnum][0])
end

#attr_num(attrname) ⇒ Object

Given an attribute name, return the number corresponding to it, based on the dictionary file(s) that have been read.

Parameters

attrname

Name of the attribute whose number is desired.

Return Value

The number corresponding to the appropriate attribute name given.



143
144
145
146
147
148
# File 'lib/radius/dictionary.rb', line 143

def attr_num(attrname)
  if (@attr[attrname] == nil || @attr[attrname][0] == nil)
    return(nil)
  end
  return(@attr[attrname][0])
end

#attr_numtype(attrnum) ⇒ Object

Given an attribute number, return the type of the attribute corresponding to it, based on the dictionary file(s) that have been read.

Parameters

attrnum

Number of the attribute whose type is desired.

Return Value

The number corresponding to the appropriate attribute name given.



193
194
195
196
197
198
# File 'lib/radius/dictionary.rb', line 193

def attr_numtype(attrnum)
  if (@rattr[attrnum] == nil || @rattr[attrnum][1] == nil)
    return(nil)
  end
  return(@rattr[attrnum][1])
end

#attr_type(attrname) ⇒ Object

Given an attribute name, return the corresponding type for it, based on the dictionary file(s) that have been read.

Parameters

attrname

Name of the attribute whose type is desired.

Return Value

The type string corresponding to the appropriate attribute name given. This is either string, ipaddr, integer, or date.



159
160
161
162
163
164
# File 'lib/radius/dictionary.rb', line 159

def attr_type(attrname)
  if (@attr[attrname] == nil || @attr[attrname][1] == nil)
    return(nil)
  end
  return(@attr[attrname][1])
end

#read(fp) ⇒ Object

Parse a dictionary file from an IO object and learn the name<->number mappings from it. Only the /first/ definition will apply if multiple definitions are seen. This method may be called multiple times with different IO objects, reading from several files.

Parameters

fp

IO object from which to read the data

Return

None. Any strangeness in the file results in a message being printed to stderr.



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
# File 'lib/radius/dictionary.rb', line 65

def read(fp)
  fp.each_line {
    |line|
    next if line =~ /^\#/	# discard comments
    next if (sl = line.split(/\s+/)) == []
    case sl[0].upcase
    when "ATTRIBUTE"
      @attr[sl[1]] = [sl[2].to_i, sl[3]] if (@attr[sl[1]] == nil)
      @rattr[sl[2].to_i] = [sl[1], sl[3]] if (@rattr[sl[2].to_i] == nil)
    when "VALUE"
      if (@attr[sl[1]] == nil)
        $stderr.print("Warning: value given for unknown attribute #{sl[1]}");
      else
        if (@val[@attr[sl[1]][0]] == nil)
          @val[@attr[sl[1]][0]] = {}
        end
        if (@rval[@attr[sl[1]][0]] == nil)
          @rval[@attr[sl[1]][0]] = []
        end
        if (@val[@attr[sl[1]][0]][sl[2]] == nil)
          @val[@attr[sl[1]][0]][sl[2]] = sl[3].to_i
        end
        if (@rval[@attr[sl[1]][0]][sl[3].to_i] == nil)
          @rval[@attr[sl[1]][0]][sl[3].to_i] = sl[2]
        end
      end
    when "VENDORATTR"
      sl[3] = Kernel::Integer(sl[3]) # this gets hex and octal
      # values correctly
      @vsattr[sl[1].to_i] = {} if (@vsattr[sl[1].to_i] == nil)
      @rvsattr[sl[1].to_i] = {} if (@rvsattr[sl[1].to_i] == nil)

      if (@vsattr[sl[1].to_i][sl[2]] == nil)
        @vsattr[sl[1].to_i][sl[2]] = sl[3..4]
      end

      if (@rvsattr[sl[1].to_i][sl[3]] == nil)
        @rvsattr[sl[1].to_i][sl[3]] = [sl[2], sl[4]]
      end
    when "VENDORVALUE"
      sl[4] = Kernel::Integer(sl[4])
      if (@vsattr[sl[1].to_i][sl[2]] == nil)
        $stderr.print "Warning: vendor value for unknown vendor attribute #{sl[1]} found - ignored\n"
      else
        sl[1] = sl[1].to_i
        @vsaval[sl[1]] = {} if @vsaval[sl[1].to_i] == nil
        @rvsaval[sl[1]] = {} if @rvsaval[sl[1].to_i] == nil
        if @vsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]] == nil
          @vsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]] = {}
        end

        if @rvsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]] == nil
          @rvsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]] = []
        end

        if @vsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]][sl[3]] == nil
          @vsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]][sl[3]] = sl[4]
        end

        if @rvsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]][sl[4]] == nil
          @rvsaval[sl[1]][@vsattr[sl[1]][sl[2]][0]][sl[4]] = sl[3]
        end
      end
    else
      $stderr.print "Warning: Weird dictionary line: #{line}\n"
    end
  }
end

#val_has_name(attrnum) ⇒ Object

Alias for attr_has_val. Don’t use this; it’s confusing.

Parameters

attrname

Name of the attribute whose number is desired.

Return Value

The number corresponding to the appropriate attribute name given.



221
222
223
# File 'lib/radius/dictionary.rb', line 221

def val_has_name(attrnum)
  return(@rval[attrnum] != nil)
end

#val_name(attrnum, valnum) ⇒ Object

Returns the name of the numbered value for the attribute value supplied. The reverse of val_num.

Parameters

attrnum

the attribute number

valname

the name of the value

Return

The name of the numbered value and attribute.



245
246
247
# File 'lib/radius/dictionary.rb', line 245

def val_name(attrnum, valnum)
  return(@rval[attrnum][valnum])
end

#val_num(attrnum, valname) ⇒ Object

Give the number of the named value for the attribute number supplied.

Parameters

attrnum

the attribute number

valname

the name of the value

Return

The number of the named value and attribute.



233
234
235
# File 'lib/radius/dictionary.rb', line 233

def val_num(attrnum, valname)
  return(@val[attrnum][valname])
end

#vsattr_has_val(vendorid, code) ⇒ Object

Determines whether the vendor-specific attibute with the Vendor-Id and code given has a value.

Parameters
vendorid

the Vendor-Id

code

the code of the vendor-specific attribute to query

Return Value

True or false on whether or not the vendor-specific attribute has a value



307
308
309
# File 'lib/radius/dictionary.rb', line 307

def vsattr_has_val(vendorid, code)
  return(@vsaval[vendorid][code] != nil)
end

#vsattr_name(vendorid, code) ⇒ Object

Obtains the name of a vendor-specific attribute given the Vendor-Id and the code of the vendor-specific attribute. The inverse of vsattr_num.

Parameters
vendorid

the Vendor-Id

code

the code of the vendor-specific attribute to query

Return Value

The name of the vendor-specific attribute



283
284
285
# File 'lib/radius/dictionary.rb', line 283

def vsattr_name(vendorid, code)
  return(@rvsattr[vendorid][code][0])
end

#vsattr_num(vendorid, name) ⇒ Object

Obtains the code of a vendor-specific attribute given the Vendor-Id and the name of the vendor-specific attribute (e.g. 9 for Cisco and ‘cisco-avpair’).

Parameters
vendorid

the Vendor-Id

name

the name of the vendor-specific attribute to query

Return Value

The code for the vendor-specific attribute



258
259
260
# File 'lib/radius/dictionary.rb', line 258

def vsattr_num(vendorid, name)
  return(@vsattr[vendorid][name][0])
end

#vsattr_numtype(vendorid, code) ⇒ Object

Obtains the type of a vendor-specific attribute given the Vendor-Id and the code of the vendor-specific attribute.

Parameters
vendorid

the Vendor-Id

code

the code of the vendor-specific attribute to query

Return Value

The type of the vendor-specific attribute



295
296
297
# File 'lib/radius/dictionary.rb', line 295

def vsattr_numtype(vendorid, code)
  return(@rvsattr[vendorid][code][1]) rescue nil
end

#vsattr_type(vendorid, name) ⇒ Object

Obtains the type of a vendor-specific attribute given the Vendor-Id and the name of the vendor-specific attribute.

Parameters
vendorid

the Vendor-Id

name

the name of the vendor-specific attribute to query

Return Value

The type for the vendor-specific attribute



270
271
272
# File 'lib/radius/dictionary.rb', line 270

def vsattr_type(vendorid, name)
  return(@vsattr[vendorid][name][1])
end

#vsaval_has_name(vendorid, attrnum) ⇒ Object

Alias for vsattr_has_val. Don’t use this; it’s confusing.

Parameters

vendorid

the Vendor-Id

attrnum

Name of the attribute whose number is desired.

Return Value

The number corresponding to the appropriate attribute name given.



320
321
322
# File 'lib/radius/dictionary.rb', line 320

def vsaval_has_name(vendorid, attrnum)
  return(@rvsaval[vendorid][attrnum] != nil)
end

#vsaval_name(vendorid, attrnum, valnum) ⇒ Object

Returns the name of the numbered value for the vendor-specific attribute value supplied. The reverse of val_num.

Parameters

vendorid

the vendor ID

attrnum

the attribute number

valname

the name of the value

Return

The name of the numbered value and attribute.



346
347
348
# File 'lib/radius/dictionary.rb', line 346

def vsaval_name(vendorid, attrnum, valnum)
  return(@rvsaval[vendorid][attrnum][valnum])
end

#vsaval_num(vendorid, attrnum, valname) ⇒ Object

Give the number of the named value for the vendor-specific attribute number supplied.

Parameters

vendorid

the Vendor-Id

attrnum

the attribute number

valname

the name of the value

Return

The number of the named value and attribute.



333
334
335
# File 'lib/radius/dictionary.rb', line 333

def vsaval_num(vendorid, attrnum, valname)
  return(@vsaval[vendorid][attrnum][valname])
end