4 from webcommon import InvalidInput
7 # l = ldap.open("W92-130-LDAP-2.mit.edu")
8 # # ldap.mit.edu is 1/2 broken right now so we're going to the working backend
9 # l.simple_bind_s("", "")
11 # def getLdapGroups(user):
13 # getLdapGroups(user): returns a generator for the list of LDAP groups containing user
15 # for user_data in l.search_s("ou=affiliates,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uid=" + user, []):
16 # for group_data in l.search_s("ou=groups,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uniqueMember="+user_data[0], ['cn']):
17 # yield group_data[1]['cn'][0]
19 # def checkLdapGroups(user, group):
21 # checkLdapGroups(user, group): returns True if and only if user is in LDAP group group
23 # for result_data in l.search_s("ou=affiliates,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uid=" + user, []):
24 # if l.search_s("ou=groups,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "(&(cn=" + group + ")(uniqueMember="+result_data[0] + "))", []) != []:
28 class MyException(Exception):
31 def getAfsGroupMembers(group, cell):
32 p = subprocess.Popen(["pts", "membership", group, '-c', cell],
33 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
36 return [line.strip() for line in p.stdout.readlines()[1:]]
38 def getLockerPath(locker):
39 if '/' in locker or locker in ['.', '..']:
40 raise InvalidInput('owner', locker, 'Locker name is invalid.')
41 return '/mit/' + locker
43 def checkAfsGroup(user, group, cell):
45 checkAfsGroup(user, group) returns True if and only if user is in AFS group group in cell cell
47 return user in getAfsGroupMembers(group, cell)
50 p = subprocess.Popen(["fs", "whichcell", getLockerPath(locker)],
51 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
53 raise MyException(p.stderr.read())
54 return p.stdout.read().split()[-1][1:-1]
56 def getLockerAcl(locker):
57 p = subprocess.Popen(["fs", "listacl", getLockerPath(locker)],
58 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60 raise MyException(p.stderr.read())
61 lines = p.stdout.readlines()
63 for line in lines[1:]:
65 if fields[0] == 'Negative':
68 values.append(fields[0])
71 def notLockerOwner(user, locker):
73 notLockerOwner(user, locker) returns false if and only if user administers locker.
75 If the user does not own the locker, returns the string reason for
79 cell = getCell(locker)
80 values = getLockerAcl(locker)
81 except MyException, e:
85 if entry == user or (entry[0:6] == "system" and
86 checkAfsGroup(user, entry, cell)):
88 return "You don't have admin bits on " + getLockerPath(locker)
91 if __name__ == "__main__":
92 # print list(getldapgroups("tabbott"))
93 print checkAfsGroup("tabbott", "system:debathena", 'athena.mit.edu')
94 print checkAfsGroup("tabbott", "system:debathena", 'sipb.mit.edu')
95 print checkAfsGroup("tabbott", "system:debathena-root", 'athena.mit.edu')
96 print checkAfsGroup("tabbott", "system:hmmt-request", 'athena.mit.edu')
97 print notLockerOwner("tabbott", "tabbott")
98 print notLockerOwner("tabbott", "debathena")
99 print notLockerOwner("tabbott", "sipb")
100 print notLockerOwner("tabbott", "lsc")
101 print notLockerOwner("tabbott", "scripts")
102 print notLockerOwner("ecprice", "hmmt")