Gid creation code moved. NT.
[sfa.git] / sfa / senslab / LDAPapi.py
1
2
3
4 import ldap
5 from sfa.util.config import *
6 from sfa.trust.gid import *
7 from sfa.trust.hierarchy import *
8 from sfa.trust.auth import *
9 from sfa.trust.certificate import *
10
11 class LDAPapi :
12         def __init__(self, record_filter = None):
13                 self.ldapserv=ldap.open("192.168.0.251")
14                 self.senslabauth=Hierarchy()
15                 config=Config()
16                 self.authname=config.SFA_REGISTRY_ROOT_AUTH
17                 authinfo=self.senslabauth.get_auth_info(self.authname)
18         
19                 self.auth=Auth()
20                 gid=authinfo.get_gid_object()
21         
22         def ldapFind(self, record_filter = None, columns=None):
23
24                 results = []
25         
26                 if 'authority' in record_filter:
27                 # ask for authority
28                         if record_filter['authority']==self.authname:
29                                 # which is SFA_REGISTRY_ROOT_AUTH
30                                 # request all records which are under our authority, ie all ldap entries
31                                 ldapfilter="cn=*"
32                         else:
33                                 #which is NOT SFA_REGISTRY_ROOT_AUTH
34                                 return []
35                 else :
36                         if not 'hrn' in record_filter:
37                                 print >>sys.stderr,"find : don't know how to handle filter ",record_filter
38                                 return []
39                         else:
40                                 hrns=[]
41                                 h=record_filter['hrn']
42                                 if  isinstance(h,list):
43                                         hrns=h
44                                 else : 
45                                         hrns.append(h)
46         
47                                 ldapfilter="(|"
48                                 for hrn in hrns:
49                                         splited_hrn=hrn.split(".")
50                                         if splited_hrn[0] != self.authname :
51                                                 print >>sys.stderr,"i know nothing about",hrn, " my authname is ", self.authname, " not ", splited_hrn[0]
52                                         else :
53                                                 login=splited_hrn[1]
54                                                 ldapfilter+="(uid="
55                                                 ldapfilter+=login
56                                                 ldapfilter+=")"
57                                 ldapfilter+=")"
58         
59         
60                 rindex=self.ldapserv.search("ou=people,dc=senslab,dc=info",ldap.SCOPE_SUBTREE,ldapfilter, ['mail','givenName', 'sn', 'uid','sshPublicKey'])
61                 ldapresponse=self.ldapserv.result(rindex,1)
62                 for ldapentry in ldapresponse[1]:
63                         hrn=self.authname+"."+ldapentry[1]['uid'][0]
64 #                       uuid=create_uuid() 
65                 
66 #                       RSA_KEY_STRING=ldapentry[1]['sshPublicKey'][0]
67                 
68 #                       pkey=convert_public_key(RSA_KEY_STRING)
69                 
70 #                       gid=self.senslabauth.create_gid("urn:publicid:IDN+"+self.authname+"+user+"+ldapentry[1]['uid'][0], uuid, pkey, CA=False)
71                 
72                         parent_hrn = get_authority(hrn)
73                         parent_auth_info = self.senslabauth.get_auth_info(parent_hrn)
74
75                         results.append(  {      
76                                 'type': 'user',
77                                 'pkey': ldapentry[1]['sshPublicKey'][0].
78 #                               'email': ldapentry[1]['mail'][0],
79 #                               'first_name': ldapentry[1]['givenName'][0],
80 #                               'last_name': ldapentry[1]['sn'][0],
81 #                               'phone': 'none',
82                                 'serial': 'none',
83                                 'authority': self.authname,
84                                 'peer_authority': '',
85                                 'pointer' : -1,
86                                 'hrn': hrn,
87                                 } )
88                 return results