~ubuntu-branches/ubuntu/maverick/pyca/maverick

« back to all changes in this revision

Viewing changes to pylib/ldapbase.py

  • Committer: Bazaar Package Importer
  • Author(s): Lars Bahner
  • Date: 2003-12-02 19:39:35 UTC
  • Revision ID: james.westby@ubuntu.com-20031202193935-fzzt289mntvy6a8q
Tags: upstream-20031118
ImportĀ upstreamĀ versionĀ 20031118

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
# ldapbase.py Version 0.1.2
 
3
# (c) by Michael Stroeder, michael.stroeder@propack-data.de
 
4
##############################################################################
 
5
 
 
6
import sys, string, re
 
7
 
 
8
dn_pattern   = r'([\w;.]+[\s]*=[^,]+)(,[ ]*[\w;.]+[\s]*=[^,]+)*'
 
9
dn_regex   = re.compile(dn_pattern)
 
10
 
 
11
# returns 1 if s is a LDAP DN
 
12
def is_dn(s):
 
13
  rm = dn_regex.match(s)
 
14
  return rm!=None and rm.group(0)==s
 
15
 
 
16
def normalize_dn(dn):
 
17
  result = string.split(dn,',')
 
18
  result = map(string.strip,result)
 
19
  return string.join(result,',')
 
20
 
 
21
# returns parent-DN of dn
 
22
def ParentDN(dn):
 
23
  return string.join(string.split(dn,',')[1:],',')
 
24
 
 
25
# returns a list of parent-DNs of dn
 
26
def ParentDNList(dn):
 
27
  result = []
 
28
  DNComponentList = string.split(dn,',')
 
29
  for i in range(1,len(DNComponentList)):
 
30
    result.append(string.join(DNComponentList[i:],','))
 
31
  return result
 
32
 
 
33
# parse a LDAP URL and return (host,dn,attributes,scope,filter)
 
34
# host         LDAP host
 
35
# dn           distinguished name
 
36
# attributes   list with attributes
 
37
# scope        search scope string
 
38
# filter       LDAP search filter
 
39
def parse_ldap_url(ldap_url):
 
40
  dummy,rest = string.split(ldap_url,'://',1)
 
41
  try:
 
42
    host,rest = string.split(rest,'/',1)
 
43
  except ValueError:
 
44
    host='' ; dn=rest
 
45
  paramlist=string.split(rest,'?')
 
46
  dn          = paramlist[0]
 
47
  try:
 
48
    attributes  = string.split(paramlist[1],',')
 
49
  except IndexError:
 
50
    attributes  = []
 
51
  try:
 
52
    scope       = paramlist[2]
 
53
  except IndexError:
 
54
    scope       = ''
 
55
  try:
 
56
    filter      = paramlist[3]
 
57
  except IndexError:
 
58
    filter      = ''
 
59
  return (host,dn,attributes,scope,filter)
 
60
 
 
61
 
 
62
class Attribute:
 
63
 
 
64
  def __init__(self):
 
65
    self.name=''
 
66
 
 
67
  def put(self,name,oid='',syntax='',alias=[],notes=''):
 
68
    self.name=name
 
69
    self.oid=oid
 
70
    self.alias=alias
 
71
    self.notes=notes
 
72
 
 
73
  def parse(self,attr_schemastr):
 
74
    pass
 
75
 
 
76
 
 
77
class ObjectClass:
 
78
 
 
79
  def __init__(self):
 
80
    self.name=''
 
81
 
 
82
  def put(self,name,oid='',syntax='',sup='',must=[],may=[],notes=''):
 
83
    self.name=name
 
84
    self.oid=oid
 
85
    self.abstract=abstract
 
86
    self.sup=sup
 
87
    self.must=must
 
88
    self.may=may
 
89
    self.syntax=syntax
 
90
    self.notes=notes
 
91
 
 
92
  def parse(self,oc_schemastr):
 
93
    pass
 
94
 
 
95
 
 
96
class Schema:
 
97
 
 
98
  def __init__(self,host):
 
99
    self.host=host
 
100
    self.oc_def = {}
 
101
    self.oc_list = []
 
102
    self.attr_def = {}
 
103
    self.attr_list = []
 
104
 
 
105
  def AddObjectClass(self,name,oid='',sup='',must=['objectClass'],may=[],syntax='',notes=''):
 
106
    if not name in self.oc_list:
 
107
      self.oc_list.append(name)
 
108
      self.oc_def['name']=ObjectClass()
 
109
    self.oc_def['name'].put(name,oid,sup,must,may,syntax,notes)
 
110
 
 
111
  def AddAttribute(self,name,oid='',syntax='',alias=[],notes=''):
 
112
    if not name in self.attr_list:
 
113
      self.attr_list.append(name)
 
114
      self.attr_def['name']=Attribute()
 
115
    self.attr_def['name'].put(name,oid,syntax,alias,notes)
 
116
 
 
117
  def v3SchemaQuery(self,ldapconn,basedn='cn=schema',searchfilter='objectclass=subschema'):
 
118
    schema = ldapconn.search_s()
 
119
 
 
120
  def ReadOpenLDAPConf(self,slapdconf):
 
121
    f = open(slapdconf,'r')
 
122