File: ldapbase.py

package info (click to toggle)
pyca 20031119-0
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 644 kB
  • ctags: 933
  • sloc: python: 4,998; sh: 809; makefile: 41
file content (122 lines) | stat: -rw-r--r-- 3,110 bytes parent folder | download | duplicates (5)
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
##############################################################################
# ldapbase.py Version 0.1.2
# (c) by Michael Stroeder, michael.stroeder@propack-data.de
##############################################################################

import sys, string, re

dn_pattern   = r'([\w;.]+[\s]*=[^,]+)(,[ ]*[\w;.]+[\s]*=[^,]+)*'
dn_regex   = re.compile(dn_pattern)

# returns 1 if s is a LDAP DN
def is_dn(s):
  rm = dn_regex.match(s)
  return rm!=None and rm.group(0)==s

def normalize_dn(dn):
  result = string.split(dn,',')
  result = map(string.strip,result)
  return string.join(result,',')

# returns parent-DN of dn
def ParentDN(dn):
  return string.join(string.split(dn,',')[1:],',')

# returns a list of parent-DNs of dn
def ParentDNList(dn):
  result = []
  DNComponentList = string.split(dn,',')
  for i in range(1,len(DNComponentList)):
    result.append(string.join(DNComponentList[i:],','))
  return result

# parse a LDAP URL and return (host,dn,attributes,scope,filter)
# host         LDAP host
# dn           distinguished name
# attributes   list with attributes
# scope        search scope string
# filter       LDAP search filter
def parse_ldap_url(ldap_url):
  dummy,rest = string.split(ldap_url,'://',1)
  try:
    host,rest = string.split(rest,'/',1)
  except ValueError:
    host='' ; dn=rest
  paramlist=string.split(rest,'?')
  dn          = paramlist[0]
  try:
    attributes  = string.split(paramlist[1],',')
  except IndexError:
    attributes  = []
  try:
    scope       = paramlist[2]
  except IndexError:
    scope       = ''
  try:
    filter      = paramlist[3]
  except IndexError:
    filter      = ''
  return (host,dn,attributes,scope,filter)


class Attribute:

  def __init__(self):
    self.name=''

  def put(self,name,oid='',syntax='',alias=[],notes=''):
    self.name=name
    self.oid=oid
    self.alias=alias
    self.notes=notes

  def parse(self,attr_schemastr):
    pass


class ObjectClass:

  def __init__(self):
    self.name=''

  def put(self,name,oid='',syntax='',sup='',must=[],may=[],notes=''):
    self.name=name
    self.oid=oid
    self.abstract=abstract
    self.sup=sup
    self.must=must
    self.may=may
    self.syntax=syntax
    self.notes=notes

  def parse(self,oc_schemastr):
    pass


class Schema:

  def __init__(self,host):
    self.host=host
    self.oc_def = {}
    self.oc_list = []
    self.attr_def = {}
    self.attr_list = []

  def AddObjectClass(self,name,oid='',sup='',must=['objectClass'],may=[],syntax='',notes=''):
    if not name in self.oc_list:
      self.oc_list.append(name)
      self.oc_def['name']=ObjectClass()
    self.oc_def['name'].put(name,oid,sup,must,may,syntax,notes)

  def AddAttribute(self,name,oid='',syntax='',alias=[],notes=''):
    if not name in self.attr_list:
      self.attr_list.append(name)
      self.attr_def['name']=Attribute()
    self.attr_def['name'].put(name,oid,syntax,alias,notes)

  def v3SchemaQuery(self,ldapconn,basedn='cn=schema',searchfilter='objectclass=subschema'):
    schema = ldapconn.search_s()

  def ReadOpenLDAPConf(self,slapdconf):
    f = open(slapdconf,'r')