/usr/share/pyshared/ldapurl.py is in python-ldap 2.4.10-1.
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 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | """
ldapurl - handling of LDAP URLs as described in RFC 4516
See http://www.python-ldap.org/ for details.
\$Id: ldapurl.py,v 1.58 2012/06/07 18:40:59 stroeder Exp $
Python compability note:
This module only works with Python 2.0+ since
1. string methods are used instead of module string and
2. list comprehensions are used.
"""
__version__ = '2.4.10'
__all__ = [
# constants
'SEARCH_SCOPE','SEARCH_SCOPE_STR',
'LDAP_SCOPE_BASE','LDAP_SCOPE_ONELEVEL','LDAP_SCOPE_SUBTREE',
# functions
'isLDAPUrl',
# classes
'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl'
]
import UserDict
from urllib import quote,unquote
LDAP_SCOPE_BASE = 0
LDAP_SCOPE_ONELEVEL = 1
LDAP_SCOPE_SUBTREE = 2
SEARCH_SCOPE_STR = {None:'',0:'base',1:'one',2:'sub'}
SEARCH_SCOPE = {
'':None,
# the search scope strings defined in RFC2255
'base':LDAP_SCOPE_BASE,
'one':LDAP_SCOPE_ONELEVEL,
'sub':LDAP_SCOPE_SUBTREE,
}
# Some widely used types
StringType = type('')
TupleType=type(())
def isLDAPUrl(s):
"""
Returns 1 if s is a LDAP URL, 0 else
"""
s_lower = s.lower()
return \
s_lower.startswith('ldap://') or \
s_lower.startswith('ldaps://') or \
s_lower.startswith('ldapi://')
def ldapUrlEscape(s):
"""Returns URL encoding of string s"""
return quote(s).replace(',','%2C').replace('/','%2F')
class LDAPUrlExtension:
"""
Class for parsing and unparsing LDAP URL extensions
as described in RFC 4516.
Usable class attributes:
critical
Boolean integer marking the extension as critical
extype
Type of extension
exvalue
Value of extension
"""
def __init__(self,extensionStr=None,critical=0,extype=None,exvalue=None):
self.critical = critical
self.extype = extype
self.exvalue = exvalue
if extensionStr:
self._parse(extensionStr)
def _parse(self,extension):
extension = extension.strip()
if not extension:
# Don't parse empty strings
self.extype,self.exvalue = None,None
return
self.critical = extension[0]=='!'
if extension[0]=='!':
extension = extension[1:].strip()
try:
self.extype,self.exvalue = extension.split('=',1)
except ValueError:
# No value, just the extype
self.extype,self.exvalue = extension,None
else:
self.exvalue = unquote(self.exvalue.strip())
self.extype = self.extype.strip()
def unparse(self):
if self.exvalue is None:
return '%s%s' % ('!'*(self.critical>0),self.extype)
else:
return '%s%s=%s' % (
'!'*(self.critical>0),
self.extype,quote(self.exvalue or '')
)
def __str__(self):
return self.unparse()
def __repr__(self):
return '<%s.%s instance at %s: %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self)),
self.__dict__
)
def __eq__(self,other):
return \
(self.critical==other.critical) and \
(self.extype==other.extype) and \
(self.exvalue==other.exvalue)
def __ne__(self,other):
return not self.__eq__(other)
class LDAPUrlExtensions(UserDict.UserDict):
"""
Models a collection of LDAP URL extensions as
dictionary type
"""
def __init__(self,default=None):
UserDict.UserDict.__init__(self)
for k,v in (default or {}).items():
self[k]=v
def __setitem__(self,name,value):
"""
value
Either LDAPUrlExtension instance, (critical,exvalue)
or string'ed exvalue
"""
assert isinstance(value,LDAPUrlExtension)
assert name==value.extype
self.data[name] = value
def values(self):
return [
self[k]
for k in self.keys()
]
def __str__(self):
return ','.join(map(str,self.values()))
def __repr__(self):
return '<%s.%s instance at %s: %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self)),
self.data
)
def __eq__(self,other):
assert isinstance(other,self.__class__),TypeError(
"other has to be instance of %s" % (self.__class__)
)
return self.data==other.data
def parse(self,extListStr):
for extension_str in extListStr.strip().split(','):
if extension_str:
e = LDAPUrlExtension(extension_str)
self[e.extype] = e
def unparse(self):
return ','.join([ v.unparse() for v in self.values() ])
class LDAPUrl:
"""
Class for parsing and unparsing LDAP URLs
as described in RFC 4516.
Usable class attributes:
urlscheme
URL scheme (either ldap, ldaps or ldapi)
hostport
LDAP host (default '')
dn
String holding distinguished name (default '')
attrs
list of attribute types (default None)
scope
integer search scope for ldap-module
filterstr
String representation of LDAP Search Filters
(see RFC 4515)
extensions
Dictionary used as extensions store
who
Maps automagically to bindname LDAP URL extension
cred
Maps automagically to X-BINDPW LDAP URL extension
"""
attr2extype = {'who':'bindname','cred':'X-BINDPW'}
def __init__(
self,
ldapUrl=None,
urlscheme='ldap',
hostport='',dn='',attrs=None,scope=None,filterstr=None,
extensions=None,
who=None,cred=None
):
self.urlscheme=urlscheme
self.hostport=hostport
self.dn=dn
self.attrs=attrs
self.scope=scope
self.filterstr=filterstr
self.extensions=(extensions or LDAPUrlExtensions({}))
if ldapUrl!=None:
self._parse(ldapUrl)
if who!=None:
self.who = who
if cred!=None:
self.cred = cred
def __eq__(self,other):
return \
self.urlscheme==other.urlscheme and \
self.hostport==other.hostport and \
self.dn==other.dn and \
self.attrs==other.attrs and \
self.scope==other.scope and \
self.filterstr==other.filterstr and \
self.extensions==other.extensions
def __ne__(self,other):
return not self.__eq__(other)
def _parse(self,ldap_url):
"""
parse a LDAP URL and set the class attributes
urlscheme,host,dn,attrs,scope,filterstr,extensions
"""
if not isLDAPUrl(ldap_url):
raise ValueError,'Parameter ldap_url does not seem to be a LDAP URL.'
scheme,rest = ldap_url.split('://',1)
self.urlscheme = scheme.strip()
if not self.urlscheme in ['ldap','ldaps','ldapi']:
raise ValueError,'LDAP URL contains unsupported URL scheme %s.' % (self.urlscheme)
slash_pos = rest.find('/')
qemark_pos = rest.find('?')
if (slash_pos==-1) and (qemark_pos==-1):
# No / and ? found at all
self.hostport = unquote(rest)
self.dn = ''
return
else:
if slash_pos!=-1 and (qemark_pos==-1 or (slash_pos<qemark_pos)):
# Slash separates DN from hostport
self.hostport = unquote(rest[:slash_pos])
# Eat the slash from rest
rest = rest[slash_pos+1:]
elif qemark_pos!=1 and (slash_pos==-1 or (slash_pos>qemark_pos)):
# Question mark separates hostport from rest, DN is assumed to be empty
self.hostport = unquote(rest[:qemark_pos])
# Do not eat question mark
rest = rest[qemark_pos:]
else:
raise ValueError,'Something completely weird happened!'
paramlist=rest.split('?',4)
paramlist_len = len(paramlist)
if paramlist_len>=1:
self.dn = unquote(paramlist[0]).strip()
if (paramlist_len>=2) and (paramlist[1]):
self.attrs = unquote(paramlist[1].strip()).split(',')
if paramlist_len>=3:
scope = paramlist[2].strip()
try:
self.scope = SEARCH_SCOPE[scope]
except KeyError:
raise ValueError,"Search scope must be either one of base, one or sub. LDAP URL contained %s" % (repr(scope))
if paramlist_len>=4:
filterstr = paramlist[3].strip()
if not filterstr:
self.filterstr = None
else:
self.filterstr = unquote(filterstr)
if paramlist_len>=5:
if paramlist[4]:
self.extensions = LDAPUrlExtensions()
self.extensions.parse(paramlist[4])
else:
self.extensions = None
return
def applyDefaults(self,defaults):
"""
Apply defaults to all class attributes which are None.
defaults
Dictionary containing a mapping from class attributes
to default values
"""
for k in defaults.keys():
if getattr(self,k) is None:
setattr(self,k,defaults[k])
def initializeUrl(self):
"""
Returns LDAP URL suitable to be passed to ldap.initialize()
"""
if self.urlscheme=='ldapi':
# hostport part might contain slashes when ldapi:// is used
hostport = ldapUrlEscape(self.hostport)
else:
hostport = self.hostport
return '%s://%s' % (self.urlscheme,hostport)
def unparse(self):
"""
Returns LDAP URL depending on class attributes set.
"""
if self.attrs is None:
attrs_str = ''
else:
attrs_str = ','.join(self.attrs)
scope_str = SEARCH_SCOPE_STR[self.scope]
if self.filterstr is None:
filterstr = ''
else:
filterstr = ldapUrlEscape(self.filterstr)
dn = ldapUrlEscape(self.dn)
if self.urlscheme=='ldapi':
# hostport part might contain slashes when ldapi:// is used
hostport = ldapUrlEscape(self.hostport)
else:
hostport = self.hostport
ldap_url = '%s://%s/%s?%s?%s?%s' % (
self.urlscheme,
hostport,dn,attrs_str,scope_str,filterstr
)
if self.extensions:
ldap_url = ldap_url+'?'+self.extensions.unparse()
return ldap_url
def htmlHREF(self,urlPrefix='',hrefText=None,hrefTarget=None):
"""
Returns a string with HTML link for this LDAP URL.
urlPrefix
Prefix before LDAP URL (e.g. for addressing another web-based client)
hrefText
link text/description
hrefTarget
string added as link target attribute
"""
assert type(urlPrefix)==StringType, "urlPrefix must be StringType"
if hrefText is None:
hrefText = self.unparse()
assert type(hrefText)==StringType, "hrefText must be StringType"
if hrefTarget is None:
target = ''
else:
assert type(hrefTarget)==StringType, "hrefTarget must be StringType"
target = ' target="%s"' % hrefTarget
return '<a%s href="%s%s">%s</a>' % (
target,urlPrefix,self.unparse(),hrefText
)
def __str__(self):
return self.unparse()
def __repr__(self):
return '<%s.%s instance at %s: %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self)),
self.__dict__
)
def __getattr__(self,name):
if self.attr2extype.has_key(name):
extype = self.attr2extype[name]
if self.extensions and \
self.extensions.has_key(extype) and \
not self.extensions[extype].exvalue is None:
result = unquote(self.extensions[extype].exvalue)
else:
return None
else:
raise AttributeError,"%s has no attribute %s" % (
self.__class__.__name__,name
)
return result # __getattr__()
def __setattr__(self,name,value):
if self.attr2extype.has_key(name):
extype = self.attr2extype[name]
if value is None:
# A value of None means that extension is deleted
delattr(self,name)
elif value!=None:
# Add appropriate extension
self.extensions[extype] = LDAPUrlExtension(
extype=extype,exvalue=unquote(value)
)
else:
self.__dict__[name] = value
def __delattr__(self,name):
if self.attr2extype.has_key(name):
extype = self.attr2extype[name]
if self.extensions:
try:
del self.extensions[extype]
except KeyError:
pass
else:
del self.__dict__[name]
|