def _sync_children(self): children = [] try: filenames = os.listdir(self.path) except OSError as e: if e.errno == errno.ENOENT: pass else: raise else: seen = set() for fn in filenames: base, ext = os.path.splitext(fn) if ext not in [u'.dir', u'.ldif']: continue if base in seen: continue seen.add(base) dn = distinguishedname.DistinguishedName(listOfRDNs=( (distinguishedname.RelativeDistinguishedName(base), ) + self.dn.split())) e = self.__class__(os.path.join(self.path, base + u'.dir'), dn) children.append(e) return children
def handle_LDAPModifyDNRequest(self, request, controls, reply): self.checkControls(controls) dn = distinguishedname.DistinguishedName(request.entry) newrdn = distinguishedname.RelativeDistinguishedName(request.newrdn) deleteoldrdn = bool(request.deleteoldrdn) if not deleteoldrdn: raise ldaperrors.LDAPUnwillingToPerform( "Cannot handle preserving old RDN yet.") newSuperior = request.newSuperior if newSuperior is None: newSuperior = dn.up() else: newSuperior = distinguishedname.DistinguishedName(newSuperior) newdn = distinguishedname.DistinguishedName( listOfRDNs=(newrdn,)+newSuperior.split()) root = interfaces.IConnectedLDAPEntry(self.factory) d = root.lookup(dn) def _gotEntry(entry): d = entry.move(newdn) return d def _report(entry): return pureldap.LDAPModifyDNResponse(resultCode=0) d.addCallback(_gotEntry) d.addCallback(_report) return d
def _deleteChild(self, rdn): if not isinstance(rdn, distinguishedname.RelativeDistinguishedName): rdn = distinguishedname.RelativeDistinguishedName(stringValue=rdn) for c in self._sync_children(): if c.dn.split()[0] == rdn: return c.delete() raise ldaperrors.LDAPNoSuchObject(rdn.getText())
def testKnownValues(self): for s, l in self.knownValues: fromString = dn.DistinguishedName(s) listOfRDNs = [] for av in l: listOfAttributeTypesAndValues = [] for a,v in av: listOfAttributeTypesAndValues.append(dn.LDAPAttributeTypeAndValue(attributeType=a, value=v)) r=dn.RelativeDistinguishedName(listOfAttributeTypesAndValues) listOfRDNs.append(r) fromList = dn.DistinguishedName(listOfRDNs) self.assertEqual(fromString, fromList) fromStringToText = fromString.getText() fromListToText = fromList.getText() assert fromStringToText == fromListToText canon = fromStringToText # DNs equal their byte string representation. Note this does # not mean they equal all the possible string # representations -- just the canonical one. self.assertEqual(fromString, canon) self.assertEqual(fromList, canon) self.assertEqual(canon, fromString) self.assertEqual(canon, fromList) # DNs can be used interchangeably with their canonical # string representation as hash keys. self.assertEqual(hash(fromString), hash(canon)) self.assertEqual(hash(fromList), hash(canon)) self.assertEqual(hash(canon), hash(fromString)) self.assertEqual(hash(canon), hash(fromList))
def addChild(self, rdn, attributes): self._checkState() a = [] if attributes.get('objectClass', None): a.append(('objectClass', attributes['objectClass'])) del attributes['objectClass'] attributes = a + sorted(attributes.items()) del a rdn = distinguishedname.RelativeDistinguishedName(rdn) dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) ldapAttrs = [] for attrType, values in attributes: ldapAttrType = pureldap.LDAPAttributeDescription(attrType) lst = [] for value in values: if (isinstance(value, six.text_type)): value = value.encode('utf-8') lst.append(pureldap.LDAPAttributeValue(value)) ldapValues = pureber.BERSet(lst) ldapAttrs.append((ldapAttrType, ldapValues)) op = pureldap.LDAPAddRequest(entry=dn.getText(), attributes=ldapAttrs) d = self.client.send(op) d.addCallback(self._cbAddDone, dn) return d
def testOpenLDAPEqualsEscape(self): """Slapd wants = to be escaped in RDN attributeValues.""" got = dn.DistinguishedName(listOfRDNs=[ dn.RelativeDistinguishedName(attributeTypesAndValues=[ dn.LDAPAttributeTypeAndValue(attributeType='cn', value=r'test'), dn.LDAPAttributeTypeAndValue(attributeType='owner', value=r'uid=foo,ou=depart' + r'ment,dc=example,dc=com'), ]), dn.RelativeDistinguishedName('dc=example'), dn.RelativeDistinguishedName('dc=com'), ]) got = str(got) self.assertEqual( got, r'cn=test+owner=uid\=foo\,ou\=depar' + r'tment\,dc\=example\,dc\=com,dc=ex' + r'ample,dc=com')
def _deleteChild(self, rdn): if not isinstance(rdn, distinguishedname.RelativeDistinguishedName): rdn = distinguishedname.RelativeDistinguishedName(stringValue=rdn) for c in self._children: if c.dn.split()[0] == rdn: self._children.remove(c) return c raise ldaperrors.LDAPNoSuchObject, rdn
def _deleteChild(self, rdn): if not isinstance(rdn, distinguishedname.RelativeDistinguishedName): rdn = distinguishedname.RelativeDistinguishedName(stringValue=rdn) rdn_str = rdn.toWire() try: return self._children.pop(rdn_str) except KeyError: raise ldaperrors.LDAPNoSuchObject(rdn)
def add(self, context, **kw): cfg = context.locate(interfaces.ILDAPConfig) dnAttr = self._getDNAttr() assert kw.has_key( 'add_' + dnAttr), 'Must have attribute dn %s points to.' % dnAttr assert kw['add_' + dnAttr], 'Attribute %s must have value.' % 'add_' + dnAttr # TODO ugly rdn = distinguishedname.RelativeDistinguishedName( attributeTypesAndValues=[ distinguishedname.LDAPAttributeTypeAndValue( attributeType=dnAttr, value=kw['add_' + dnAttr]), ]) #TODO verify changes = [] for k, v in kw.items(): if hasattr(self, "nonUserEditableAttributeType_" + k): raise "Can't set attribute %s when adding." % k elif k[:len("add_")] == "add_": if not v: continue attrtype = self._get_attrtype(k[len("add_"):]) assert attrtype if attrtype.single_value or attrtype.uiHint_multiline: v = [v] else: v = self._textarea_to_list(v) if v and [1 for x in v if x]: attr = k[len("add_"):] changes.append(defer.succeed((attr, v))) #TODO for attributeType in self.nonUserEditableAttributes: thing = getattr(self, 'nonUserEditableAttributeType_' + attributeType) if callable(thing): changes.append(thing(attributeType, context)) else: changes.append(defer.succeed((attributeType, thing))) dl = defer.DeferredList(changes, fireOnOneErrback=1) #dl.addErrback(lambda x: x[0]) # throw away index def _pruneSuccessFlags(l): r = [] for succeeded, result in l: assert succeeded r.append(result) return r dl.addCallback(_pruneSuccessFlags) dl.addCallback(self._process2, context, rdn, kw) return dl
def addChild(self, rdn, attributes): """TODO ugly API. Returns the created entry.""" rdn = distinguishedname.RelativeDistinguishedName(rdn) rdn_str = rdn.toWire() if rdn_str in self._children: raise ldaperrors.LDAPEntryAlreadyExists(self._children[rdn_str].dn) dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) e = self.__class__(dn, attributes) e._parent = self self._children[rdn_str] = e return e
def addChild(self, rdn, attributes): """TODO ugly API. Returns the created entry.""" rdn = distinguishedname.RelativeDistinguishedName(rdn) for c in self._children: if c.dn.split()[0] == rdn: raise ldaperrors.LDAPEntryAlreadyExists, c.dn dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) e = ReadOnlyInMemoryLDAPEntry(dn, attributes) e._parent = self self._children.append(e) return e
def _addChild(self, rdn, attributes): rdn = distinguishedname.RelativeDistinguishedName(rdn) for c in self._sync_children(): if c.dn.split()[0] == rdn: raise ldaperrors.LDAPEntryAlreadyExists, c.dn dn = distinguishedname.DistinguishedName( listOfRDNs=(rdn,) + self.dn.split()) e = entry.BaseLDAPEntry(dn, attributes) if not os.path.exists(self.path): os.mkdir(self.path) fileName = os.path.join(self.path, '%s' % rdn) tmp = fileName + '.' + tempName() + '.tmp' f = file(tmp, 'w') f.write(str(e)) f.close() os.rename(tmp, fileName+'.ldif') dirName = os.path.join(self.path, '%s.dir' % rdn) e = self.__class__(dirName, dn) return e
def addChild(self, rdn, attributes): self._checkState() rdn = distinguishedname.RelativeDistinguishedName(rdn) dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) ldapAttrs = [] for attrType, values in attributes.items(): ldapAttrType = pureldap.LDAPAttributeDescription(attrType) l = [] for value in values: l.append(pureldap.LDAPAttributeValue(value)) ldapValues = pureber.BERSet(l) ldapAttrs.append((ldapAttrType, ldapValues)) op = pureldap.LDAPAddRequest(entry=str(dn), attributes=ldapAttrs) d = self.client.send(op) d.addCallback(self._cbAddDone, dn) return d
def _addChild(self, rdn, attributes): rdn = distinguishedname.RelativeDistinguishedName(rdn) for c in self._sync_children(): if c.dn.split()[0] == rdn: raise ldaperrors.LDAPEntryAlreadyExists(c.dn.getText()) dn = distinguishedname.DistinguishedName(listOfRDNs=(rdn, ) + self.dn.split()) e = entry.BaseLDAPEntry(dn, attributes) if not os.path.exists(self.path): os.mkdir(self.path) fileName = os.path.join(self.path, u'%s' % rdn.getText()) tmp = u'%s.%s.tmp' % (fileName, str(uuid.uuid4())) f = open(tmp, 'wb') f.write(e.toWire()) f.close() os.rename(tmp, fileName + u'.ldif') dirName = os.path.join(self.path, u'%s.dir' % rdn.getText()) e = self.__class__(dirName, dn) return e
def _cbSetPassword(self, ctx, newPassword, serviceName): e = getEntry(ctx, self.dn) rdn = distinguishedname.RelativeDistinguishedName( attributeTypesAndValues=[ distinguishedname.LDAPAttributeTypeAndValue(attributeType='cn', value=serviceName), distinguishedname.LDAPAttributeTypeAndValue( attributeType='owner', value=str(self.dn)) ]) d = e.addChild( rdn, { 'objectClass': ['serviceSecurityObject'], 'cn': [serviceName], 'owner': [str(self.dn)], 'userPassword': ['{crypt}!'], }) def _setPass(e, newPassword): d = e.setPassword(newPassword) return d d.addCallback(_setPass, newPassword) return d
def testToWire(self): rdn = dn.RelativeDistinguishedName('dc=example') self.assertEqual(rdn.toWire(), b'dc=example')
if e.errno == errno.ENOENT: pass else: raise else: seen = set() for fn in filenames: base, ext = os.path.splitext(fn) if ext not in ['.dir', '.ldif']: continue if base in seen: continue seen.add(base) dn = distinguishedname.DistinguishedName( listOfRDNs=((distinguishedname.RelativeDistinguishedName(base),) + self.dn.split())) e = self.__class__(os.path.join(self.path, base + '.dir'), dn) children.append(e) return children def _children(self, callback=None): children = self._sync_children() if callback is None: return children else: for c in children: callback(c) return None def children(self, callback=None):
if e.errno == errno.ENOENT: pass else: raise else: seen = sets.Set() for fn in filenames: base, ext = os.path.splitext(fn) if ext not in ['.dir', '.ldif']: continue if base in seen: continue seen.add(base) dn = distinguishedname.DistinguishedName(listOfRDNs=( (distinguishedname.RelativeDistinguishedName(base), ) + self.dn.split())) e = self.__class__(os.path.join(self.path, base + '.dir'), dn) children.append(e) return children def _children(self, callback=None): children = self._sync_children() if callback is None: return children else: for c in children: callback(c) return None def children(self, callback=None):
def testString(self): rdn = dn.RelativeDistinguishedName('dc=example') self.assertEqual(str(rdn), 'dc=example')
def testRDN(self): proto = dn.RelativeDistinguishedName('dc=example') rdn = dn.RelativeDistinguishedName(proto) self.assertEqual(str(rdn), 'dc=example')
def testRDN(self): proto = dn.RelativeDistinguishedName('dc=example') rdn = dn.RelativeDistinguishedName(proto) self.assertEqual(rdn.toWire(), b'dc=example')
def testRDN(self): proto=dn.RelativeDistinguishedName('dc=example') rdn=dn.RelativeDistinguishedName(proto) self.assertEqual(rdn.getText(), u'dc=example')
def testGetText(self): rdn=dn.RelativeDistinguishedName('dc=example') self.assertEqual(rdn.getText(), u'dc=example')