6 # l = ldap.open("W92-130-LDAP-2.mit.edu")
7 # # ldap.mit.edu is 1/2 broken right now so we're going to the working backend
8 # l.simple_bind_s("", "")
10 # def getLdapGroups(user):
12 # getLdapGroups(user): returns a generator for the list of LDAP groups containing user
14 # for user_data in l.search_s("ou=affiliates,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uid=" + user, []):
15 # for group_data in l.search_s("ou=groups,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uniqueMember="+user_data[0], ['cn']):
16 # yield group_data[1]['cn'][0]
18 # def checkLdapGroups(user, group):
20 # checkLdapGroups(user, group): returns True if and only if user is in LDAP group group
22 # for result_data in l.search_s("ou=affiliates,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "uid=" + user, []):
23 # if l.search_s("ou=groups,dc=mit,dc=edu", ldap.SCOPE_ONELEVEL, "(&(cn=" + group + ")(uniqueMember="+result_data[0] + "))", []) != []:
27 class AfsProcessError(Exception):
30 def getAfsGroupMembers(group, cell):
31 subprocess.check_call(['aklog', cell], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
32 p = subprocess.Popen(["pts", "membership", "-encrypt", group, '-c', cell],
33 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
35 if err: #Error code doesn't reveal missing groups, but stderr does
36 if err.startswith('pts: Permission denied ; unable to get membership of '):
38 raise AfsProcessError(err)
39 return [line.strip() for line in p.stdout.readlines()[1:]]
41 def getLockerPath(locker):
42 if '/' in locker or locker in ['.', '..']:
43 raise AfsProcessError("Locker '%s' is invalid." % locker)
44 return '/mit/' + locker
47 p = subprocess.Popen(["fs", "whichcell", getLockerPath(locker)],
48 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50 raise AfsProcessError(p.stderr.read())
51 return p.stdout.read().split()[-1][1:-1]
53 def getLockerAcl(locker):
54 p = subprocess.Popen(["fs", "listacl", getLockerPath(locker)],
55 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
57 raise AfsProcessError(p.stderr.read())
58 lines = p.stdout.readlines()
60 for line in lines[1:]:
62 if fields[0] == 'Negative':
65 values.append(fields[0])
68 def notLockerOwner(user, locker):
70 notLockerOwner(user, locker) returns false if and only if user administers locker.
72 If the user does not own the locker, returns the string reason for
76 cell = getCell(locker)
77 values = getLockerAcl(locker)
78 except AfsProcessError, e:
82 if entry == user or (entry[0:6] == "system" and
83 user in getAfsGroupMembers(entry, cell)):
85 return "You don't have admin bits on " + getLockerPath(locker)
88 if __name__ == "__main__":
89 # print list(getldapgroups("tabbott"))
90 print "tabbott" in getAfsGroupMembers("system:debathena", 'athena.mit.edu')
91 print "tabbott" in getAfsGroupMembers("system:debathena", 'sipb.mit.edu')
92 print "tabbott" in getAfsGroupMembers("system:debathena-root", 'athena.mit.edu')
93 print "tabbott" in getAfsGroupMembers("system:hmmt-request", 'athena.mit.edu')
94 print notLockerOwner("tabbott", "tabbott")
95 print notLockerOwner("tabbott", "debathena")
96 print notLockerOwner("tabbott", "sipb")
97 print notLockerOwner("tabbott", "lsc")
98 print notLockerOwner("tabbott", "scripts")
99 print notLockerOwner("ecprice", "hmmt")