/usr/lib/python3/dist-packages/pyrad/dictionary.py is in python3-pyrad 2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # dictionary.py
#
# Copyright 2002,2005,2007 Wichert Akkerman <wichert@wiggy.net>
"""
RADIUS uses dictionaries to define the attributes that can
be used in packets. The Dictionary class stores the attribute
definitions from one or more dictionary files.
Dictionary files are textfiles with one command per line.
Comments are specified by starting with a # character, and empty
lines are ignored.
The commands supported are::
ATTRIBUTE <attribute> <code> <type> [<vendor>]
specify an attribute and its type
VALUE <attribute> <valuename> <value>
specify a value attribute
VENDOR <name> <id>
specify a vendor ID
BEGIN-VENDOR <vendorname>
begin definition of vendor attributes
END-VENDOR <vendorname>
end definition of vendor attributes
The datatypes currently supported are:
======= ======================
type description
======= ======================
string ASCII string
ipaddr IPv4 address
integer 32 bits signed number
date 32 bits UNIX timestamp
octets arbitrary binary data
======= ======================
These datatypes are parsed but not supported:
+------------+----------------------------------------------+
| type | description |
+============+==============================================+
| abinary | ASCII encoded binary data |
+------------+----------------------------------------------+
| ifid | 8 octets in network byte order |
+------------+----------------------------------------------+
| ipv6addr | 16 octets in network byte order |
+------------+----------------------------------------------+
| ipv6prefix | 18 octets in network byte order |
+------------+----------------------------------------------+
| ether | 6 octets of hh:hh:hh:hh:hh:hh |
| | where 'h' is hex digits, upper or lowercase. |
+------------+----------------------------------------------+
"""
__docformat__ = 'epytext en'
from pyrad import bidict
from pyrad import tools
from pyrad import dictfile
from copy import copy
DATATYPES = frozenset(['string', 'ipaddr', 'integer', 'date',
'octets', 'abinary', 'ipv6addr',
'ipv6prefix', 'ifid', 'ether'])
class ParseError(Exception):
"""Dictionary parser exceptions.
:ivar msg: Error message
:type msg: string
:ivar linenumber: Line number on which the error occured
:type linenumber: integer
"""
def __init__(self, msg=None, **data):
self.msg = msg
self.file = data.get('file', '')
self.line = data.get('line', -1)
def __str__(self):
str = ''
if self.file:
str += self.file
if self.line > -1:
str += '(%d)' % self.line
if self.file or self.line > -1:
str += ': '
str += 'Parse error'
if self.msg:
str += ': %s' % self.msg
return str
class Attribute:
def __init__(self, name, code, datatype, vendor='', values={},
encrypt=0, has_tag=False):
if datatype not in DATATYPES:
raise ValueError('Invalid data type')
self.name = name
self.code = code
self.type = datatype
self.vendor = vendor
self.encrypt = encrypt
self.has_tag = has_tag
self.values = bidict.BiDict()
for (key, value) in values.items():
self.values.Add(key, value)
class Dictionary(object):
"""RADIUS dictionary class.
This class stores all information about vendors, attributes and their
values as defined in RADIUS dictionary files.
:ivar vendors: bidict mapping vendor name to vendor code
:type vendors: bidict
:ivar attrindex: bidict mapping
:type attrindex: bidict
:ivar attributes: bidict mapping attribute name to attribute class
:type attributes: bidict
"""
def __init__(self, dict=None, *dicts):
"""
:param dict: path of dictionary file or file-like object to read
:type dict: string or file
:param dicts: list of dictionaries
:type dicts: sequence of strings or files
"""
self.vendors = bidict.BiDict()
self.vendors.Add('', 0)
self.attrindex = bidict.BiDict()
self.attributes = {}
self.defer_parse = []
if dict:
self.ReadDictionary(dict)
for i in dicts:
self.ReadDictionary(i)
def __len__(self):
return len(self.attributes)
def __getitem__(self, key):
return self.attributes[key]
def __contains__(self, key):
return key in self.attributes
has_key = __contains__
def __ParseAttribute(self, state, tokens):
if not len(tokens) in [4, 5]:
raise ParseError(
'Incorrect number of tokens for attribute definition',
name=state['file'],
line=state['line'])
vendor = state['vendor']
has_tag = False
encrypt = 0
if len(tokens) >= 5:
def keyval(o):
kv = o.split('=')
if len(kv) == 2:
return (kv[0], kv[1])
else:
return (kv[0], None)
options = [keyval(o) for o in tokens[4].split(',')]
for (key, val) in options:
if key == 'has_tag':
has_tag = True
elif key == 'encrypt':
if val not in ['1', '2', '3']:
raise ParseError(
'Illegal attribute encryption: %s' % val,
file=state['file'],
line=state['line'])
encrypt = int(val)
if (not has_tag) and encrypt == 0:
vendor = tokens[4]
if not self.vendors.HasForward(vendor):
raise ParseError('Unknown vendor ' + vendor,
file=state['file'],
line=state['line'])
(attribute, code, datatype) = tokens[1:4]
code = int(code, 0)
if not datatype in DATATYPES:
raise ParseError('Illegal type: ' + datatype,
file=state['file'],
line=state['line'])
if vendor:
key = (self.vendors.GetForward(vendor), code)
else:
key = code
self.attrindex.Add(attribute, key)
self.attributes[attribute] = Attribute(attribute, code, datatype,
vendor, encrypt=encrypt, has_tag=has_tag)
def __ParseValue(self, state, tokens, defer):
if len(tokens) != 4:
raise ParseError('Incorrect number of tokens for value definition',
file=state['file'],
line=state['line'])
(attr, key, value) = tokens[1:]
try:
adef = self.attributes[attr]
except KeyError:
if defer:
self.defer_parse.append((copy(state), copy(tokens)))
return
raise ParseError('Value defined for unknown attribute ' + attr,
file=state['file'],
line=state['line'])
if adef.type == 'integer':
value = int(value, 0)
value = tools.EncodeAttr(adef.type, value)
self.attributes[attr].values.Add(key, value)
def __ParseVendor(self, state, tokens):
if len(tokens) not in [3, 4]:
raise ParseError(
'Incorrect number of tokens for vendor definition',
file=state['file'],
line=state['line'])
# Parse format specification, but do
# nothing about it for now
if len(tokens) == 4:
fmt = tokens[3].split('=')
if fmt[0] != 'format':
raise ParseError(
"Unknown option '%s' for vendor definition" % (fmt[0]),
file=state['file'],
line=state['line'])
try:
(t, l) = tuple(int(a) for a in fmt[1].split(','))
if t not in [1, 2, 4] or l not in [0, 1, 2]:
raise ParseError(
'Unknown vendor format specification %s' % (fmt[1]),
file=state['file'],
line=state['line'])
except ValueError:
raise ParseError(
'Syntax error in vendor specification',
file=state['file'],
line=state['line'])
(vendorname, vendor) = tokens[1:3]
self.vendors.Add(vendorname, int(vendor, 0))
def __ParseBeginVendor(self, state, tokens):
if len(tokens) != 2:
raise ParseError(
'Incorrect number of tokens for begin-vendor statement',
file=state['file'],
line=state['line'])
vendor = tokens[1]
if not self.vendors.HasForward(vendor):
raise ParseError(
'Unknown vendor %s in begin-vendor statement' % vendor,
file=state['file'],
line=state['line'])
state['vendor'] = vendor
def __ParseEndVendor(self, state, tokens):
if len(tokens) != 2:
raise ParseError(
'Incorrect number of tokens for end-vendor statement',
file=state['file'],
line=state['line'])
vendor = tokens[1]
if state['vendor'] != vendor:
raise ParseError(
'Ending non-open vendor' + vendor,
file=state['file'],
line=state['line'])
state['vendor'] = ''
def ReadDictionary(self, file):
"""Parse a dictionary file.
Reads a RADIUS dictionary file and merges its contents into the
class instance.
:param file: Name of dictionary file to parse or a file-like object
:type file: string or file-like object
"""
fil = dictfile.DictFile(file)
state = {}
state['vendor'] = ''
self.defer_parse = []
for line in fil:
state['file'] = fil.File()
state['line'] = fil.Line()
line = line.split('#', 1)[0].strip()
tokens = line.split()
if not tokens:
continue
key = tokens[0].upper()
if key == 'ATTRIBUTE':
self.__ParseAttribute(state, tokens)
elif key == 'VALUE':
self.__ParseValue(state, tokens, True)
elif key == 'VENDOR':
self.__ParseVendor(state, tokens)
elif key == 'BEGIN-VENDOR':
self.__ParseBeginVendor(state, tokens)
elif key == 'END-VENDOR':
self.__ParseEndVendor(state, tokens)
for state, tokens in self.defer_parse:
key = tokens[0].upper()
if key == 'VALUE':
self.__ParseValue(state, tokens, False)
self.defer_parse = []
|