Package ldaptor :: Package protocols :: Module pureldap
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.protocols.pureldap

   1  # Twisted, the Framework of Your Internet 
   2  # Copyright (C) 2001 Matthew W. Lefkowitz 
   3  # 
   4  # This library is free software; you can redistribute it and/or 
   5  # modify it under the terms of version 2.1 of the GNU Lesser General Public 
   6  # License as published by the Free Software Foundation. 
   7  # 
   8  # This library is distributed in the hope that it will be useful, 
   9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  11  # Lesser General Public License for more details. 
  12  # 
  13  # You should have received a copy of the GNU Lesser General Public 
  14  # License along with this library; if not, write to the Free Software 
  15  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  16   
  17  """LDAP protocol message conversion; no application logic here.""" 
  18   
  19  from pureber import * 
  20   
  21  next_ldap_message_id=1 
22 -def alloc_ldap_message_id():
23 global next_ldap_message_id 24 r=next_ldap_message_id 25 next_ldap_message_id=next_ldap_message_id+1 26 return r
27
28 -def escape(s):
29 s = s.replace('\\', r'\5c') 30 s = s.replace('*', r'\2a') 31 s = s.replace('(', r'\28') 32 s = s.replace(')', r'\29') 33 s = s.replace('\0', r'\00') 34 return s
35
36 -class LDAPString(BEROctetString):
37 pass
38
39 -class LDAPAttributeValue(BEROctetString):
40 pass
41
42 -class LDAPMessage(BERSequence):
43 id = None 44 value = None 45
46 - def fromBER(klass, tag, content, berdecoder=None):
47 l = berDecodeMultiple(content, berdecoder) 48 49 id_=l[0].value 50 value=l[1] 51 if l[2:]: 52 controls = [] 53 for c in l[2]: 54 controls.append(( 55 c.controlType, 56 c.criticality, 57 c.controlValue, 58 )) 59 else: 60 controls = None 61 assert not l[3:] 62 63 r = klass(id=id_, 64 value=value, 65 controls=controls, 66 tag=tag) 67 return r
68 fromBER = classmethod(fromBER) 69
70 - def __init__(self, value=None, controls=None, id=None, tag=None):
71 BERSequence.__init__(self, value=[], tag=tag) 72 assert value is not None 73 self.id=id 74 if self.id is None: 75 self.id=alloc_ldap_message_id() 76 self.value=value 77 self.controls = controls
78
79 - def __str__(self):
80 l = [BERInteger(self.id), self.value] 81 if self.controls is not None: 82 l.append(LDAPControls([LDAPControl(*a) for a in self.controls])) 83 return str(BERSequence(l))
84
85 - def __repr__(self):
86 l=[] 87 l.append('id=%r' % self.id) 88 l.append('value=%r' % self.value) 89 if self.tag!=self.__class__.tag: 90 l.append('tag=%d' % self.tag) 91 return self.__class__.__name__+'('+', '.join(l)+')'
92
93 -class LDAPProtocolOp:
94 - def __init__(self):
95 pass
96
97 - def __str__(self):
98 raise NotImplementedError
99
100 -class LDAPProtocolRequest(LDAPProtocolOp):
101 needs_answer=1 102 pass
103
104 -class LDAPProtocolResponse(LDAPProtocolOp):
105 pass
106
107 -class LDAPBERDecoderContext_LDAPBindRequest(BERDecoderContext):
108 Identities = { 109 CLASS_CONTEXT|0x00: BEROctetString, 110 }
111
112 -class LDAPBindRequest(LDAPProtocolRequest, BERSequence):
113 tag=CLASS_APPLICATION|0x00 114
115 - def fromBER(klass, tag, content, berdecoder=None):
116 l = berDecodeMultiple(content, 117 LDAPBERDecoderContext_LDAPBindRequest( 118 fallback=berdecoder)) 119 120 r = klass(version=l[0].value, 121 dn=l[1].value, 122 auth=l[2].value, 123 tag=tag) 124 return r
125 fromBER = classmethod(fromBER) 126
127 - def __init__(self, version=None, dn=None, auth=None, tag=None):
128 LDAPProtocolRequest.__init__(self) 129 BERSequence.__init__(self, [], tag=tag) 130 self.version=version 131 if self.version is None: 132 self.version=3 133 self.dn=dn 134 if self.dn is None: 135 self.dn='' 136 self.auth=auth 137 if self.auth is None: 138 self.auth=''
139
140 - def __str__(self):
141 return str(BERSequence([ 142 BERInteger(self.version), 143 BEROctetString(self.dn), 144 BEROctetString(self.auth, tag=CLASS_CONTEXT|0), 145 ], tag=self.tag))
146
147 - def __repr__(self):
148 l=[] 149 l.append('version=%d' % self.version) 150 l.append('dn=%s' % repr(self.dn)) 151 l.append('auth=%s' % repr(self.auth)) 152 if self.tag!=self.__class__.tag: 153 l.append('tag=%d' % self.tag) 154 return self.__class__.__name__+'('+', '.join(l)+')'
155 156 157
158 -class LDAPReferral(BERSequence):
159 tag = CLASS_CONTEXT | 0x03
160
161 -class LDAPResult(LDAPProtocolResponse, BERSequence):
162 - def fromBER(klass, tag, content, berdecoder=None):
163 l = berDecodeMultiple(content, LDAPBERDecoderContext_LDAPBindRequest( 164 fallback=berdecoder)) 165 166 assert 3<=len(l)<=4 167 168 referral = None 169 #if (l[3:] and isinstance(l[3], LDAPReferral)): 170 #TODO support referrals 171 #self.referral=self.data[0] 172 173 r = klass(resultCode=l[0].value, 174 matchedDN=l[1].value, 175 errorMessage=l[2].value, 176 referral=referral, 177 tag=tag) 178 return r
179 fromBER = classmethod(fromBER) 180
181 - def __init__(self, resultCode=None, matchedDN=None, errorMessage=None, referral=None, serverSaslCreds=None, tag=None):
182 LDAPProtocolResponse.__init__(self) 183 BERSequence.__init__(self, value=[], tag=tag) 184 assert resultCode is not None 185 self.resultCode=resultCode 186 if matchedDN is None: 187 matchedDN='' 188 self.matchedDN=matchedDN 189 if errorMessage is None: 190 errorMessage='' 191 self.errorMessage=errorMessage 192 self.referral=referral 193 self.serverSaslCreds=serverSaslCreds
194
195 - def __str__(self):
196 assert self.referral is None #TODO 197 return str(BERSequence([ 198 BEREnumerated(self.resultCode), 199 BEROctetString(self.matchedDN), 200 BEROctetString(self.errorMessage), 201 #TODO referral [3] Referral OPTIONAL 202 ], tag=self.tag))
203
204 - def __repr__(self):
205 l=[] 206 l.append('resultCode=%r' % self.resultCode) 207 if self.matchedDN: 208 l.append('matchedDN=%r' % str(self.matchedDN)) 209 if self.errorMessage: 210 l.append('errorMessage=%r' % str(self.errorMessage)) 211 if self.referral: 212 l.append('referral=%r' % self.referral) 213 if self.tag!=self.__class__.tag: 214 l.append('tag=%d' % self.tag) 215 return self.__class__.__name__+'('+', '.join(l)+')'
216
217 -class LDAPBindResponse_serverSaslCreds(BERSequence):
218 tag = CLASS_CONTEXT|0x03 219 220 pass
221
222 -class LDAPBERDecoderContext_BindResponse(BERDecoderContext):
223 Identities = { 224 LDAPBindResponse_serverSaslCreds.tag: LDAPBindResponse_serverSaslCreds, 225 }
226
227 -class LDAPBindResponse(LDAPResult):
228 tag=CLASS_APPLICATION|0x01 229 230 resultCode = None 231 matchedDN = None 232 errorMessage = None 233 referral = None 234 serverSaslCreds = None 235
236 - def fromBER(klass, tag, content, berdecoder=None):
237 l = berDecodeMultiple(content, LDAPBERDecoderContext_BindResponse( 238 fallback=berdecoder)) 239 240 assert 3<=len(l)<=4 241 242 try: 243 if isinstance(l[0], LDAPBindResponse_serverSaslCreds): 244 serverSaslCreds=l[0] 245 del l[0] 246 else: 247 serverSaslCreds=None 248 except IndexError: 249 serverSaslCreds=None 250 251 referral = None 252 #if (l[3:] and isinstance(l[3], LDAPReferral)): 253 #TODO support referrals 254 #self.referral=self.data[0] 255 256 r = klass(resultCode=l[0].value, 257 matchedDN=l[1].value, 258 errorMessage=l[2].value, 259 referral=referral, 260 serverSaslCreds=serverSaslCreds, 261 tag=tag) 262 return r
263 fromBER = classmethod(fromBER) 264
265 - def __init__(self, resultCode=None, matchedDN=None, errorMessage=None, referral=None, serverSaslCreds=None, tag=None):
268
269 - def __str__(self):
270 assert self.serverSaslCreds is None #TODO 271 return LDAPResult.__str__(self)
272
273 - def __repr__(self):
274 assert self.serverSaslCreds is None #TODO 275 return LDAPResult.__repr__(self)
276
277 -class LDAPUnbindRequest(LDAPProtocolRequest, BERNull):
278 tag=CLASS_APPLICATION|0x02 279 needs_answer=0 280
281 - def __init__(self, *args, **kwargs):
282 LDAPProtocolRequest.__init__(self) 283 BERNull.__init__(self, *args, **kwargs)
284
285 - def __str__(self):
286 return BERNull.__str__(self)
287
288 -class LDAPAttributeDescription(BEROctetString):
289 pass
290
291 -class LDAPAttributeValueAssertion(BERSequence):
292 - def fromBER(klass, tag, content, berdecoder=None):
293 l = berDecodeMultiple(content, berdecoder) 294 assert len(l) == 2 295 296 r = klass(attributeDesc=l[0], 297 assertionValue=l[1], 298 tag=tag) 299 return r
300 fromBER = classmethod(fromBER) 301
302 - def __init__(self, attributeDesc=None, assertionValue=None, tag=None):
303 BERSequence.__init__(self, value=[], tag=tag) 304 assert attributeDesc is not None 305 self.attributeDesc=attributeDesc 306 self.assertionValue=assertionValue
307
308 - def __str__(self):
309 return str(BERSequence([self.attributeDesc, 310 self.assertionValue], 311 tag=self.tag))
312
313 - def __repr__(self):
314 if self.tag==self.__class__.tag: 315 return self.__class__.__name__+"(attributeDesc=%s, assertionValue=%s)"\ 316 %(repr(self.attributeDesc), repr(self.assertionValue)) 317 else: 318 return self.__class__.__name__+"(attributeDesc=%s, assertionValue=%s, tag=%d)"\ 319 %(repr(self.attributeDesc), repr(self.assertionValue), self.tag)
320 321
322 -class LDAPFilter(BERStructured):
323 - def __init__(self, tag=None):
325
326 -class LDAPFilterSet(BERSet):
327 - def fromBER(klass, tag, content, berdecoder=None):
328 l = berDecodeMultiple(content, LDAPBERDecoderContext_Filter(fallback=berdecoder)) 329 r = klass(l, tag=tag) 330 return r
331 fromBER = classmethod(fromBER)
332
333 -class LDAPFilter_and(LDAPFilterSet):
334 tag = CLASS_CONTEXT|0x00 335
336 - def asText(self):
337 return '(&'+''.join([x.asText() for x in self])+')'
338
339 -class LDAPFilter_or(LDAPFilterSet):
340 tag = CLASS_CONTEXT|0x01 341
342 - def asText(self):
343 return '(|'+''.join([x.asText() for x in self])+')'
344
345 -class LDAPFilter_not(LDAPFilter):
346 tag = CLASS_CONTEXT|0x02 347
348 - def fromBER(klass, tag, content, berdecoder=None):
349 value, bytes = berDecodeObject(LDAPBERDecoderContext_Filter(fallback=berdecoder, inherit=berdecoder), content) 350 assert bytes == len(content) 351 352 r = klass(value=value, 353 tag=tag) 354 return r
355 fromBER = classmethod(fromBER) 356
357 - def __init__(self, value, tag=tag):
358 LDAPFilter.__init__(self, tag=tag) 359 assert value is not None 360 self.value=value
361
362 - def __repr__(self):
363 if self.tag==self.__class__.tag: 364 return self.__class__.__name__\ 365 +"(value=%s)"\ 366 %repr(self.value) 367 else: 368 return self.__class__.__name__\ 369 +"(value=%s, tag=%d)"\ 370 %(repr(self.value), self.tag)
371
372 - def __str__(self):
373 r=str(self.value) 374 return chr(self.identification())+int2berlen(len(r))+r
375
376 - def asText(self):
377 return '(!'+self.value.asText()+')'
378
379 -class LDAPFilter_equalityMatch(LDAPAttributeValueAssertion):
380 tag = CLASS_CONTEXT|0x03 381
382 - def asText(self):
383 return '('+self.attributeDesc.value+'=' \ 384 +escape(self.assertionValue.value)+')'
385
386 -class LDAPFilter_substrings_initial(LDAPString):
387 tag = CLASS_CONTEXT|0x00 388
389 - def asText(self):
390 return escape(self.value)
391 392
393 -class LDAPFilter_substrings_any(LDAPString):
394 tag = CLASS_CONTEXT|0x01 395
396 - def asText(self):
397 return escape(self.value)
398
399 -class LDAPFilter_substrings_final(LDAPString):
400 tag = CLASS_CONTEXT|0x02 401
402 - def asText(self):
403 return escape(self.value)
404
405 -class LDAPBERDecoderContext_Filter_substrings(BERDecoderContext):
406 Identities = { 407 LDAPFilter_substrings_initial.tag: LDAPFilter_substrings_initial, 408 LDAPFilter_substrings_any.tag: LDAPFilter_substrings_any, 409 LDAPFilter_substrings_final.tag: LDAPFilter_substrings_final, 410 }
411
412 -class LDAPFilter_substrings(BERSequence):
413 tag = CLASS_CONTEXT|0x04 414
415 - def fromBER(klass, tag, content, berdecoder=None):
416 l = berDecodeMultiple(content, LDAPBERDecoderContext_Filter_substrings(fallback=berdecoder)) 417 assert len(l) == 2 418 assert len(l[1])>=1 419 420 r = klass(type=l[0].value, 421 substrings=list(l[1]), 422 tag=tag) 423 return r
424 fromBER = classmethod(fromBER) 425
426 - def __init__(self, type=None, substrings=None, tag=None):
427 BERSequence.__init__(self, value=[], tag=tag) 428 assert type is not None 429 assert substrings is not None 430 self.type=type 431 self.substrings=substrings
432
433 - def __str__(self):
434 return str(BERSequence([ 435 LDAPString(self.type), 436 BERSequence(self.substrings)], tag=self.tag))
437
438 - def __repr__(self):
439 if self.tag==self.__class__.tag: 440 return self.__class__.__name__\ 441 +"(type=%s, substrings=%s)"\ 442 %(repr(self.type), repr(self.substrings)) 443 else: 444 return self.__class__.__name__\ 445 +"(type=%s, substrings=%s, tag=%d)"\ 446 %(repr(self.type), repr(self.substrings), self.tag)
447
448 - def asText(self):
449 initial=None 450 final=None 451 any=[] 452 453 for s in self.substrings: 454 assert s is not None 455 if isinstance(s, LDAPFilter_substrings_initial): 456 assert initial is None 457 assert not any 458 assert final is None 459 initial=s.asText() 460 elif isinstance(s, LDAPFilter_substrings_final): 461 assert final is None 462 final=s.asText() 463 elif isinstance(s, LDAPFilter_substrings_any): 464 assert final is None 465 any.append(s.asText()) 466 else: 467 raise 'TODO' 468 469 if initial is None: 470 initial='' 471 if final is None: 472 final='' 473 474 475 return '('+self.type+'=' \ 476 +'*'.join([initial]+any+[final])+')'
477
478 -class LDAPFilter_greaterOrEqual(LDAPAttributeValueAssertion):
479 tag = CLASS_CONTEXT|0x05 480
481 - def asText(self):
482 return '('+self.attributeDesc.value+'>=' \ 483 +escape(self.assertionValue.value)+')'
484
485 -class LDAPFilter_lessOrEqual(LDAPAttributeValueAssertion):
486 tag = CLASS_CONTEXT|0x06 487
488 - def asText(self):
489 return '('+self.attributeDesc.value+'<=' \ 490 +escape(self.assertionValue.value)+')'
491
492 -class LDAPFilter_present(LDAPAttributeDescription):
493 tag = CLASS_CONTEXT|0x07 494
495 - def asText(self):
496 return '(%s=*)' % self.value
497
498 -class LDAPFilter_approxMatch(LDAPAttributeValueAssertion):
499 tag = CLASS_CONTEXT|0x08 500 501
502 - def asText(self):
503 return '('+self.attributeDesc.value+'~=' \ 504 +escape(self.assertionValue.value)+')'
505
506 -class LDAPMatchingRuleId(LDAPString):
507 pass
508
509 -class LDAPAssertionValue(BEROctetString):
510 pass
511
512 -class LDAPMatchingRuleAssertion_matchingRule(LDAPMatchingRuleId):
513 tag = CLASS_CONTEXT|0x01 514 pass
515
516 -class LDAPMatchingRuleAssertion_type(LDAPAttributeDescription):
517 tag = CLASS_CONTEXT|0x02 518 pass
519
520 -class LDAPMatchingRuleAssertion_matchValue(LDAPAssertionValue):
521 tag = CLASS_CONTEXT|0x03 522 pass
523
524 -class LDAPMatchingRuleAssertion_dnAttributes(BERBoolean):
525 tag = CLASS_CONTEXT|0x04 526 pass
527
528 -class LDAPBERDecoderContext_MatchingRuleAssertion(BERDecoderContext):
529 Identities = { 530 LDAPMatchingRuleAssertion_matchingRule.tag: LDAPMatchingRuleAssertion_matchingRule, 531 LDAPMatchingRuleAssertion_type.tag: LDAPMatchingRuleAssertion_type, 532 LDAPMatchingRuleAssertion_matchValue.tag: LDAPMatchingRuleAssertion_matchValue, 533 LDAPMatchingRuleAssertion_dnAttributes.tag: LDAPMatchingRuleAssertion_dnAttributes, 534 }
535
536 -class LDAPMatchingRuleAssertion(BERSequence):
537 matchingRule=None 538 type=None 539 matchValue=None 540 dnAttributes=None 541
542 - def fromBER(klass, tag, content, berdecoder=None):
543 l = berDecodeMultiple(content, LDAPBERDecoderContext_MatchingRuleAssertion(fallback=berdecoder, inherit=berdecoder)) 544 545 assert 1<=len(l)<=4 546 if isinstance(l[0], LDAPMatchingRuleAssertion_matchingRule): 547 matchingRule=l[0] 548 del l[0] 549 if len(l)>1 \ 550 and isinstance(l[0], LDAPMatchingRuleAssertion_type): 551 type=l[0] 552 del l[0] 553 if len(l)>1 \ 554 and isinstance(l[0], LDAPMatchingRuleAssertion_matchValue): 555 matchValue=l[0] 556 del l[0] 557 if len(l)>1 \ 558 and isinstance(l[0], LDAPMatchingRuleAssertion_dnAttributes): 559 dnAttributes=l[0] 560 del l[0] 561 assert matchValue 562 if not dnAttributes: 563 dnAttributes=None 564 565 assert 8<=len(l)<=8 566 r = klass(matchingRule=matchingRule, 567 type=type, 568 matchValue=matchValue, 569 dnAttributes=dnAttributes, 570 tag=tag) 571 return r
572 fromBER = classmethod(fromBER) 573
574 - def __init__(self, matchingRule=None, type=None, 575 matchValue=None, dnAttributes=None, 576 tag=None):
577 BERSequence.__init__(self, value=[], tag=tag) 578 assert matchValue is not None 579 self.matchingRule=matchingRule 580 self.type=type 581 self.matchValue=matchValue 582 self.dnAttributes=dnAttributes 583 if not self.dnAttributes: 584 self.dnAttributes=None
585
586 - def __str__(self):
587 return str(BERSequence( 588 filter(lambda x: x is not None, [self.matchingRule, self.type, self.matchValue, self.dnAttributes]), tag=self.tag))
589
590 - def __repr__(self):
591 l=[] 592 l.append('matchingRule=%s' % repr(self.matchingRule)) 593 l.append('type=%s' % repr(self.type)) 594 l.append('matchValue=%s' % repr(self.matchValue)) 595 l.append('dnAttributes=%s' % repr(self.dnAttributes)) 596 if self.tag!=self.__class__.tag: 597 l.append('tag=%d' % self.tag) 598 return self.__class__.__name__+'('+', '.join(l)+')'
599
600 -class LDAPFilter_extensibleMatch(LDAPMatchingRuleAssertion):
601 tag = CLASS_CONTEXT|0x09 602 pass
603 604
605 -class LDAPBERDecoderContext_Filter(BERDecoderContext):
606 Identities = { 607 LDAPFilter_and.tag: LDAPFilter_and, 608 LDAPFilter_or.tag: LDAPFilter_or, 609 LDAPFilter_not.tag: LDAPFilter_not, 610 LDAPFilter_equalityMatch.tag: LDAPFilter_equalityMatch, 611 LDAPFilter_substrings.tag: LDAPFilter_substrings, 612 LDAPFilter_greaterOrEqual.tag: LDAPFilter_greaterOrEqual, 613 LDAPFilter_lessOrEqual.tag: LDAPFilter_lessOrEqual, 614 LDAPFilter_present.tag: LDAPFilter_present, 615 LDAPFilter_approxMatch.tag: LDAPFilter_approxMatch, 616 LDAPFilter_extensibleMatch.tag: LDAPFilter_extensibleMatch, 617 }
618 619 LDAP_SCOPE_baseObject=0 620 LDAP_SCOPE_singleLevel=1 621 LDAP_SCOPE_wholeSubtree=2 622 623 LDAP_DEREF_neverDerefAliases=0 624 LDAP_DEREF_derefInSearching=1 625 LDAP_DEREF_derefFindingBaseObj=2 626 LDAP_DEREF_derefAlways=3 627 628 LDAPFilterMatchAll = LDAPFilter_present('objectClass') 629
630 -class LDAPSearchRequest(LDAPProtocolRequest, BERSequence):
631 tag=CLASS_APPLICATION|0x03 632 633 baseObject='' 634 scope=LDAP_SCOPE_wholeSubtree 635 derefAliases=LDAP_DEREF_neverDerefAliases 636 sizeLimit=0 637 timeLimit=0 638 typesOnly=0 639 filter=LDAPFilterMatchAll 640 attributes=[] #TODO AttributeDescriptionList 641 642 #TODO decode 643
644 - def fromBER(klass, tag, content, berdecoder=None):
645 l = berDecodeMultiple(content, LDAPBERDecoderContext_Filter(fallback=berdecoder, inherit=berdecoder)) 646 647 assert 8<=len(l)<=8 648 r = klass(baseObject=l[0].value, 649 scope=l[1].value, 650 derefAliases=l[2].value, 651 sizeLimit=l[3].value, 652 timeLimit=l[4].value, 653 typesOnly=l[5].value, 654 filter=l[6], 655 attributes=[x.value for x in l[7]], 656 tag=tag) 657 return r
658 fromBER = classmethod(fromBER) 659
660 - def __init__(self, 661 baseObject=None, 662 scope=None, 663 derefAliases=None, 664 sizeLimit=None, 665 timeLimit=None, 666 typesOnly=None, 667 filter=None, 668 attributes=None, 669 tag=None):
670 LDAPProtocolRequest.__init__(self) 671 BERSequence.__init__(self, [], tag=tag) 672 673 if baseObject is not None: 674 self.baseObject=baseObject 675 if scope is not None: 676 self.scope=scope 677 if derefAliases is not None: 678 self.derefAliases=derefAliases 679 if sizeLimit is not None: 680 self.sizeLimit=sizeLimit 681 if timeLimit is not None: 682 self.timeLimit=timeLimit 683 if typesOnly is not None: 684 self.typesOnly=typesOnly 685 if filter is not None: 686 self.filter=filter 687 if attributes is not None: 688 self.attributes=attributes
689
690 - def __str__(self):
691 return str(BERSequence([ 692 BEROctetString(self.baseObject), 693 BEREnumerated(self.scope), 694 BEREnumerated(self.derefAliases), 695 BERInteger(self.sizeLimit), 696 BERInteger(self.timeLimit), 697 BERBoolean(self.typesOnly), 698 self.filter, 699 BERSequenceOf(map(BEROctetString, self.attributes)), 700 ], tag=self.tag))
701
702 - def __repr__(self):
703 if self.tag==self.__class__.tag: 704 return self.__class__.__name__\ 705 +("(baseObject=%s, scope=%s, derefAliases=%s, " \ 706 +"sizeLimit=%s, timeLimit=%s, typesOnly=%s, " \ 707 "filter=%s, attributes=%s)") \ 708 %(repr(self.baseObject), self.scope, 709 self.derefAliases, self.sizeLimit, 710 self.timeLimit, self.typesOnly, 711 repr(self.filter), self.attributes) 712 713 else: 714 return self.__class__.__name__\ 715 +("(baseObject=%s, scope=%s, derefAliases=%s, " \ 716 +"sizeLimit=%s, timeLimit=%s, typesOnly=%s, " \ 717 "filter=%s, attributes=%s, tag=%d)") \ 718 %(repr(self.baseObject), self.scope, 719 self.derefAliases, self.sizeLimit, 720 self.timeLimit, self.typesOnly, 721 self.filter, self.attributes, self.tag)
722
723 -class LDAPSearchResultEntry(LDAPProtocolResponse, BERSequence):
724 tag=CLASS_APPLICATION|0x04 725
726 - def fromBER(klass, tag, content, berdecoder=None):
727 l = berDecodeMultiple(content, LDAPBERDecoderContext_Filter(fallback=berdecoder, inherit=berdecoder)) 728 729 objectName=l[0].value 730 attributes=[] 731 for attr, li in l[1].data: 732 attributes.append((attr.value, map(lambda x: x.value, li))) 733 r = klass(objectName=objectName, 734 attributes=attributes, 735 tag=tag) 736 return r
737 fromBER = classmethod(fromBER) 738
739 - def __init__(self, objectName, attributes, tag=None):
740 LDAPProtocolResponse.__init__(self) 741 BERSequence.__init__(self, [], tag=tag) 742 assert objectName is not None 743 assert attributes is not None 744 self.objectName=objectName 745 self.attributes=attributes
746
747 - def __str__(self):
748 return str(BERSequence([ 749 BEROctetString(self.objectName), 750 BERSequence(map(lambda (attr,li): 751 BERSequence([BEROctetString(attr), 752 BERSet(map(BEROctetString, 753 li))]), 754 self.attributes)), 755 ], tag=self.tag))
756
757 - def __repr__(self):
758 if self.tag==self.__class__.tag: 759 return self.__class__.__name__\ 760 +"(objectName=%s, attributes=%s"\ 761 %(repr(str(self.objectName)), 762 repr(map(lambda (a,l): 763 (str(a), 764 map(lambda i, l=l: str(i), l)), 765 self.attributes))) 766 else: 767 return self.__class__.__name__\ 768 +"(objectName=%s, attributes=%s, tag=%d"\ 769 %(repr(str(self.objectName)), 770 repr(map(lambda (a,l): 771 (str(a), 772 map(lambda i, l=l: str(i), l)), 773 self.attributes)), 774 self.tag)
775 776
777 -class LDAPSearchResultDone(LDAPResult):
778 tag=CLASS_APPLICATION|0x05 779 780 pass
781
782 -class LDAPControls(BERSequence):
783 tag = CLASS_CONTEXT|0x00 784
785 - def fromBER(klass, tag, content, berdecoder=None):
786 l = berDecodeMultiple(content, LDAPBERDecoderContext_LDAPControls( 787 inherit=berdecoder)) 788 789 r = klass(l, tag=tag) 790 return r
791 fromBER = classmethod(fromBER)
792
793 -class LDAPControl(BERSequence):
794 criticality = None 795 controlValue = None 796
797 - def fromBER(klass, tag, content, berdecoder=None):
798 l = berDecodeMultiple(content, berdecoder) 799 800 kw = {} 801 if l[1:]: 802 kw['criticality'] = l[1].value 803 if l[2:]: 804 kw['controlValue'] = l[2].value 805 # TODO is controlType, controlValue allowed without criticality? 806 assert not l[3:] 807 808 r = klass(controlType=l[0].value, 809 tag=tag, 810 **kw) 811 return r
812 fromBER = classmethod(fromBER) 813
814 - def __init__(self, 815 controlType, criticality=None, controlValue=None, 816 id=None, tag=None):
817 BERSequence.__init__(self, value=[], tag=tag) 818 assert controlType is not None 819 self.controlType = controlType 820 self.criticality = criticality 821 self.controlValue = controlValue
822
823 - def __str__(self):
824 self.data=[LDAPOID(self.controlType)] 825 if self.criticality is not None: 826 self.data.append(BERBoolean(self.criticality)) 827 if self.controlValue is not None: 828 self.data.append(BEROctetString(self.controlValue)) 829 return BERSequence.__str__(self)
830
831 -class LDAPBERDecoderContext_LDAPControls(BERDecoderContext):
832 Identities = { 833 LDAPControl.tag: LDAPControl, 834 }
835
836 -class LDAPBERDecoderContext_LDAPMessage(BERDecoderContext):
837 Identities = { 838 LDAPControls.tag: LDAPControls, 839 }
840
841 -class LDAPBERDecoderContext_TopLevel(BERDecoderContext):
842 Identities = { 843 BERSequence.tag: LDAPMessage, 844 }
845
846 -class LDAPModifyRequest(LDAPProtocolRequest, BERSequence):
847 tag=CLASS_APPLICATION|0x06 848 object = None 849 modification = None 850
851 - def fromBER(klass, tag, content, berdecoder=None):
852 l = berDecodeMultiple(content, berdecoder) 853 854 assert len(l) == 2 855 856 r = klass(object=l[0].value, 857 modification=l[1].data, 858 tag=tag) 859 return r
860 fromBER = classmethod(fromBER) 861
862 - def __init__(self, object=None, modification=None, tag=None):
863 """ 864 Initialize the object 865 866 Example usage:: 867 868 l = LDAPModifyRequest( 869 object='cn=foo,dc=example,dc=com', 870 modification=[ 871 872 BERSequence([ 873 BEREnumerated(0), 874 BERSequence([ 875 LDAPAttributeDescription('attr1'), 876 BERSet([ 877 LDAPString('value1'), 878 LDAPString('value2'), 879 ]), 880 ]), 881 ]), 882 883 BERSequence([ 884 BEREnumerated(1), 885 BERSequence([ 886 LDAPAttributeDescription('attr2'), 887 ]), 888 ]), 889 890 ]) 891 892 But more likely you just want to say:: 893 894 mod = delta.ModifyOp('cn=foo,dc=example,dc=com', 895 [delta.Add('attr1', ['value1', 'value2']), 896 delta.Delete('attr1', ['value1', 'value2'])]) 897 l = mod.asLDAP() 898 """ 899 900 LDAPProtocolRequest.__init__(self) 901 BERSequence.__init__(self, [], tag=tag) 902 self.object=object 903 self.modification=modification
904
905 - def __str__(self):
906 l=[LDAPString(self.object)] 907 if self.modification is not None: 908 l.append(BERSequence(self.modification)) 909 return str(BERSequence(l, tag=self.tag))
910
911 - def __repr__(self):
912 if self.tag==self.__class__.tag: 913 return self.__class__.__name__+"(object=%s, modification=%s)"\ 914 %(repr(self.object), repr(self.modification)) 915 else: 916 return self.__class__.__name__+"(object=%s, modification=%s, tag=%d)" \ 917 %(repr(self.object), repr(self.modification), self.tag)
918 919
920 -class LDAPModifyResponse(LDAPResult):
921 tag = CLASS_APPLICATION|0x07
922
923 -class LDAPAddRequest(LDAPProtocolRequest, BERSequence):
924 tag = CLASS_APPLICATION|0x08 925
926 - def fromBER(klass, tag, content, berdecoder=None):
927 l = berDecodeMultiple(content, berdecoder) 928 929 r = klass(entry=l[0].value, 930 attributes=l[1], 931 tag=tag) 932 return r
933 fromBER = classmethod(fromBER) 934
935 - def __init__(self, entry=None, attributes=None, tag=None):
936 """ 937 Initialize the object 938 939 Example usage:: 940 941 l=LDAPAddRequest(entry='cn=foo,dc=example,dc=com', 942 attributes=[(LDAPAttributeDescription("attrFoo"), 943 BERSet(value=( 944 LDAPAttributeValue("value1"), 945 LDAPAttributeValue("value2"), 946 ))), 947 (LDAPAttributeDescription("attrBar"), 948 BERSet(value=( 949 LDAPAttributeValue("value1"), 950 LDAPAttributeValue("value2"), 951 ))), 952 ]) 953 """ 954 955 LDAPProtocolRequest.__init__(self) 956 BERSequence.__init__(self, [], tag=tag) 957 self.entry=entry 958 self.attributes=attributes
959
960 - def __str__(self):
961 return str(BERSequence([ 962 LDAPString(self.entry), 963 BERSequence(map(BERSequence, self.attributes)), 964 ], tag=self.tag))
965
966 - def __repr__(self):
967 if self.tag==self.__class__.tag: 968 return self.__class__.__name__+"(entry=%s, attributes=%s)"\ 969 %(repr(self.entry), repr(self.attributes)) 970 else: 971 return self.__class__.__name__+"(entry=%s, attributes=%s, tag=%d)" \ 972 %(repr(self.entry), repr(self.attributes), self.tag)
973 974 975
976 -class LDAPAddResponse(LDAPResult):
977 tag = CLASS_APPLICATION|0x09
978
979 -class LDAPDelRequest(LDAPProtocolRequest, LDAPString):
980 tag = CLASS_APPLICATION|0x0a 981
982 - def __init__(self, value=None, entry=None, tag=None):
983 """ 984 Initialize the object 985 986 l=LDAPDelRequest(entry='cn=foo,dc=example,dc=com') 987 """ 988 if entry is None and value is not None: 989 entry = value 990 LDAPProtocolRequest.__init__(self) 991 LDAPString.__init__(self, value=entry, tag=tag)
992
993 - def __str__(self):
994 return LDAPString.__str__(self)
995
996 - def __repr__(self):
997 if self.tag==self.__class__.tag: 998 return self.__class__.__name__+"(entry=%s)" \ 999 %repr(self.value) 1000 else: 1001 return self.__class__.__name__ \ 1002 +"(entry=%s, tag=%d)" \ 1003 %(repr(self.value), self.tag)
1004 1005
1006 -class LDAPDelResponse(LDAPResult):
1007 tag = CLASS_APPLICATION|0x0b 1008 pass
1009 1010
1011 -class LDAPModifyDNResponse_newSuperior(LDAPString):
1012 tag = CLASS_CONTEXT|0x00 1013 1014 pass
1015
1016 -class LDAPBERDecoderContext_ModifyDNRequest(BERDecoderContext):
1017 Identities = { 1018 LDAPModifyDNResponse_newSuperior.tag: LDAPModifyDNResponse_newSuperior, 1019 }
1020
1021 -class LDAPModifyDNRequest(LDAPProtocolRequest, BERSequence):
1022 tag=CLASS_APPLICATION|12 1023 1024 entry=None 1025 newrdn=None 1026 deleteoldrdn=None 1027 newSuperior=None 1028
1029 - def fromBER(klass, tag, content, berdecoder=None):
1030 l = berDecodeMultiple(content, LDAPBERDecoderContext_ModifyDNRequest(fallback=berdecoder)) 1031 1032 kw = {} 1033 try: 1034 kw['newSuperior'] = str(l[3].value) 1035 except IndexError: 1036 pass 1037 1038 r = klass(entry=str(l[0].value), 1039 newrdn=str(l[1].value), 1040 deleteoldrdn=l[2].value, 1041 tag=tag, 1042 **kw) 1043 return r
1044 fromBER = classmethod(fromBER) 1045
1046 - def __init__(self, entry, newrdn, deleteoldrdn, newSuperior=None, 1047 tag=None):
1048 """ 1049 Initialize the object 1050 1051 Example usage:: 1052 1053 l=LDAPModifyDNRequest(entry='cn=foo,dc=example,dc=com', 1054 newrdn='someAttr=value', 1055 deleteoldrdn=0) 1056 """ 1057 1058 LDAPProtocolRequest.__init__(self) 1059 BERSequence.__init__(self, [], tag=tag) 1060 assert entry is not None 1061 assert newrdn is not None 1062 assert deleteoldrdn is not None 1063 self.entry=entry 1064 self.newrdn=newrdn 1065 self.deleteoldrdn=deleteoldrdn 1066 self.newSuperior=newSuperior
1067
1068 - def __str__(self):
1069 l=[ 1070 LDAPString(self.entry), 1071 LDAPString(self.newrdn), 1072 BERBoolean(self.deleteoldrdn), 1073 ] 1074 if self.newSuperior is not None: 1075 l.append(LDAPString(self.newSuperior, tag=CLASS_CONTEXT|0)) 1076 return str(BERSequence(l, tag=self.tag))
1077
1078 - def __repr__(self):
1079 l = [ 1080 "entry=%s" % repr(self.entry), 1081 "newrdn=%s" % repr(self.newrdn), 1082 "deleteoldrdn=%s" % repr(self.deleteoldrdn), 1083 ] 1084 if self.newSuperior is not None: 1085 l.append("newSuperior=%s" % repr(self.newSuperior)) 1086 if self.tag!=self.__class__.tag: 1087 l.append("tag=%d" % self.tag) 1088 return self.__class__.__name__ + "(" + ', '.join(l) + ")"
1089
1090 -class LDAPModifyDNResponse(LDAPResult):
1091 tag=CLASS_APPLICATION|13
1092 1093 #class LDAPCompareResponse(LDAPProtocolResponse): 1094 #class LDAPCompareRequest(LDAPProtocolRequest): 1095 #class LDAPAbandonRequest(LDAPProtocolRequest): 1096 # needs_answer=0 1097 1098
1099 -class LDAPOID(BEROctetString):
1100 pass
1101
1102 -class LDAPResponseName(LDAPOID):
1103 tag = CLASS_CONTEXT|10
1104
1105 -class LDAPResponse(BEROctetString):
1106 tag = CLASS_CONTEXT|11
1107
1108 -class LDAPBERDecoderContext_LDAPExtendedRequest(BERDecoderContext):
1109 Identities = { 1110 CLASS_CONTEXT|0x00: BEROctetString, 1111 CLASS_CONTEXT|0x01: BEROctetString, 1112 }
1113
1114 -class LDAPExtendedRequest(LDAPProtocolRequest, BERSequence):
1115 tag = CLASS_APPLICATION|23 1116 1117 requestName = None 1118 requestValue = None 1119
1120 - def fromBER(klass, tag, content, berdecoder=None):
1121 l = berDecodeMultiple(content, 1122 LDAPBERDecoderContext_LDAPExtendedRequest( 1123 fallback=berdecoder)) 1124 1125 kw = {} 1126 try: 1127 kw['requestValue'] = l[1].value 1128 except IndexError: 1129 pass 1130 1131 r = klass(requestName=l[0].value, 1132 tag=tag, 1133 **kw) 1134 return r
1135 fromBER = classmethod(fromBER) 1136
1137 - def __init__(self, requestName, requestValue=None, 1138 tag=None):
1139 LDAPProtocolRequest.__init__(self) 1140 BERSequence.__init__(self, [], tag=tag) 1141 assert requestName is not None 1142 assert isinstance(requestName, basestring) 1143 self.requestName=requestName 1144 assert isinstance(requestValue, basestring) 1145 self.requestValue=requestValue
1146
1147 - def __str__(self):
1148 l=[LDAPOID(self.requestName, tag=CLASS_CONTEXT|0)] 1149 if self.requestValue is not None: 1150 l.append(BEROctetString(str(self.requestValue), tag=CLASS_CONTEXT|1)) 1151 return str(BERSequence(l, tag=self.tag))
1152
1153 -class LDAPPasswordModifyRequest_userIdentity(BEROctetString):
1154 tag=CLASS_CONTEXT|0
1155 -class LDAPPasswordModifyRequest_oldPasswd(BEROctetString):
1156 tag=CLASS_CONTEXT|1
1157 -class LDAPPasswordModifyRequest_newPasswd(BEROctetString):
1158 tag=CLASS_CONTEXT|2
1159
1160 -class LDAPBERDecoderContext_LDAPPasswordModifyRequest(BERDecoderContext):
1161 Identities = { 1162 LDAPPasswordModifyRequest_userIdentity.tag: 1163 LDAPPasswordModifyRequest_userIdentity, 1164 1165 LDAPPasswordModifyRequest_oldPasswd.tag: 1166 LDAPPasswordModifyRequest_oldPasswd, 1167 1168 LDAPPasswordModifyRequest_newPasswd.tag: 1169 LDAPPasswordModifyRequest_newPasswd, 1170 }
1171
1172 -class LDAPPasswordModifyRequest(LDAPExtendedRequest):
1173 oid = '1.3.6.1.4.1.4203.1.11.1' 1174
1175 - def __init__(self, requestName=None, 1176 userIdentity=None, oldPasswd=None, newPasswd=None, 1177 tag=None):
1178 assert (requestName is None 1179 or requestName == self.oid), \ 1180 '%s requestName was %s instead of %s' \ 1181 % (self.__class__.__name__, requestName, self.oid) 1182 #TODO genPasswd 1183 1184 l=[] 1185 if userIdentity is not None: 1186 l.append(LDAPPasswordModifyRequest_userIdentity(userIdentity)) 1187 if oldPasswd is not None: 1188 l.append(LDAPPasswordModifyRequest_oldPasswd(oldPasswd)) 1189 if newPasswd is not None: 1190 l.append(LDAPPasswordModifyRequest_newPasswd(newPasswd)) 1191 LDAPExtendedRequest.__init__( 1192 self, 1193 requestName=self.oid, 1194 requestValue=str(BERSequence(l)), 1195 tag=tag)
1196
1197 - def __repr__(self):
1198 l=[] 1199 # TODO userIdentity, oldPasswd, newPasswd 1200 if self.tag!=self.__class__.tag: 1201 l.append('tag=%d' % self.tag) 1202 return self.__class__.__name__+'('+', '.join(l)+')'
1203
1204 -class LDAPBERDecoderContext_LDAPExtendedResponse(BERDecoderContext):
1205 Identities = { 1206 LDAPResponseName.tag: LDAPResponseName, 1207 LDAPResponse.tag: LDAPResponse, 1208 }
1209
1210 -class LDAPExtendedResponse(LDAPResult):
1211 tag = CLASS_APPLICATION|0x18 1212 1213 responseName = None 1214 response = None 1215
1216 - def fromBER(klass, tag, content, berdecoder=None):
1217 l = berDecodeMultiple(content, LDAPBERDecoderContext_LDAPExtendedResponse( 1218 fallback=berdecoder)) 1219 1220 assert 3<=len(l)<=4 1221 1222 referral = None 1223 #if (l[3:] and isinstance(l[3], LDAPReferral)): 1224 #TODO support referrals 1225 #self.referral=self.data[0] 1226 1227 r = klass(resultCode=l[0].value, 1228 matchedDN=l[1].value, 1229 errorMessage=l[2].value, 1230 referral=referral, 1231 tag=tag) 1232 return r
1233 fromBER = classmethod(fromBER) 1234
1235 - def __init__(self, resultCode=None, matchedDN=None, errorMessage=None, 1236 referral=None, serverSaslCreds=None, 1237 responseName=None, response=None, 1238 tag=None):
1247
1248 - def __str__(self):
1249 assert self.referral is None #TODO 1250 l=[BEREnumerated(self.resultCode), 1251 BEROctetString(self.matchedDN), 1252 BEROctetString(self.errorMessage), 1253 #TODO referral [3] Referral OPTIONAL 1254 ] 1255 if self.responseName is not None: 1256 l.append(LDAPOID(self.responseName, tag=CLASS_CONTEXT|0x0a)) 1257 if self.response is not None: 1258 l.append(BEROctetString(self.response, tag=CLASS_CONTEXT|0x0b)) 1259 return str(BERSequence(l, tag=self.tag))
1260
1261 -class LDAPStartTLSRequest(LDAPExtendedRequest):
1262 """ 1263 Request to start Transport Layer Security. 1264 1265 See RFC 2830 for details. 1266 """ 1267 oid = '1.3.6.1.4.1.1466.20037' 1268
1269 - def __init__(self, requestName=None, tag=None):
1270 assert (requestName is None 1271 or requestName == self.oid), \ 1272 '%s requestName was %s instead of %s' \ 1273 % (self.__class__.__name__, requestName, self.oid) 1274 1275 LDAPExtendedRequest.__init__( 1276 self, 1277 requestName=self.oid, 1278 tag=tag)
1279
1280 - def __repr__(self):
1281 l=[] 1282 if self.tag!=self.__class__.tag: 1283 l.append('tag=%d' % self.tag) 1284 return self.__class__.__name__+'('+', '.join(l)+')'
1285
1286 -class LDAPBERDecoderContext(BERDecoderContext):
1287 Identities = { 1288 LDAPBindResponse.tag: LDAPBindResponse, 1289 LDAPBindRequest.tag: LDAPBindRequest, 1290 LDAPUnbindRequest.tag: LDAPUnbindRequest, 1291 LDAPSearchRequest.tag: LDAPSearchRequest, 1292 LDAPSearchResultEntry.tag: LDAPSearchResultEntry, 1293 LDAPSearchResultDone.tag: LDAPSearchResultDone, 1294 LDAPReferral.tag: LDAPReferral, 1295 LDAPModifyRequest.tag: LDAPModifyRequest, 1296 LDAPModifyResponse.tag: LDAPModifyResponse, 1297 LDAPAddRequest.tag: LDAPAddRequest, 1298 LDAPAddResponse.tag: LDAPAddResponse, 1299 LDAPDelRequest.tag: LDAPDelRequest, 1300 LDAPDelResponse.tag: LDAPDelResponse, 1301 LDAPExtendedRequest.tag: LDAPExtendedRequest, 1302 LDAPExtendedResponse.tag: LDAPExtendedResponse, 1303 LDAPModifyDNRequest.tag: LDAPModifyDNRequest, 1304 LDAPModifyDNResponse.tag: LDAPModifyDNResponse, 1305 }
1306