source: trunk/packages/invirt-web/code/getafsgroups.py @ 1955

Last change on this file since 1955 was 1955, checked in by broder, 15 years ago

Ignore errors from an AFS group not being listable.

File size: 3.7 KB
RevLine 
[161]1#!/usr/bin/python
2import pprint
3import subprocess
4
5# import ldap
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("", "")
9
10# def getLdapGroups(user):
11#     """
12#     getLdapGroups(user): returns a generator for the list of LDAP groups containing user
13#     """
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]
17
18# def checkLdapGroups(user, group):
19#     """
20#     checkLdapGroups(user, group): returns True if and only if user is in LDAP group group
21#     """
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] + "))", []) != []:
24#             return True
25#     return False
26
[409]27class AfsProcessError(Exception):
[234]28    pass
29
30def getAfsGroupMembers(group, cell):
[1947]31    p = subprocess.Popen(["pts", "membership", "-encrypt", group, '-c', cell],
[177]32                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
[413]33    err = p.stderr.read()
34    if err: #Error code doesn't reveal missing groups, but stderr does
[1955]35        if err.startswith('pts: Permission denied ; unable to get membership of '):
36            return []
[413]37        raise AfsProcessError(err)
[234]38    return [line.strip() for line in p.stdout.readlines()[1:]]
[161]39
[408]40def getLockerPath(locker):
41    if '/' in locker or locker in ['.', '..']:
[412]42        raise AfsProcessError("Locker '%s' is invalid." % locker)
[408]43    return '/mit/' + locker
44
[234]45def getCell(locker):
[408]46    p = subprocess.Popen(["fs", "whichcell", getLockerPath(locker)], 
[177]47                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48    if p.wait():
[409]49        raise AfsProcessError(p.stderr.read())
[234]50    return p.stdout.read().split()[-1][1:-1]
51
52def getLockerAcl(locker):
[1155]53    p = subprocess.Popen(["fs", "listacl", getLockerPath(locker)], 
54                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55    if p.wait():
56        raise AfsProcessError(p.stderr.read())
[234]57    lines = p.stdout.readlines()
58    values = []
59    for line in lines[1:]:
60        fields = line.split()
61        if fields[0] == 'Negative':
[161]62            break
[408]63        if 'a' in fields[1]:
[234]64            values.append(fields[0])
65    return values
[161]66
[234]67def notLockerOwner(user, locker):
68    """
69    notLockerOwner(user, locker) returns false if and only if user administers locker.
[161]70
[234]71    If the user does not own the locker, returns the string reason for
72    the failure.
73    """
74    try:
75        cell = getCell(locker)
76        values = getLockerAcl(locker)
[409]77    except AfsProcessError, e:
[234]78        return str(e)
79
80    for entry in values:
[413]81        if entry == user or (entry[0:6] == "system" and
82                                user in getAfsGroupMembers(entry, cell)):
[234]83            return False
[408]84    return "You don't have admin bits on " + getLockerPath(locker)
[234]85
86
[161]87if __name__ == "__main__":
88#    print list(getldapgroups("tabbott"))
[413]89    print "tabbott" in getAfsGroupMembers("system:debathena", 'athena.mit.edu')
90    print "tabbott" in getAfsGroupMembers("system:debathena", 'sipb.mit.edu')
91    print "tabbott" in getAfsGroupMembers("system:debathena-root", 'athena.mit.edu')
92    print "tabbott" in getAfsGroupMembers("system:hmmt-request", 'athena.mit.edu')
[234]93    print notLockerOwner("tabbott", "tabbott")
94    print notLockerOwner("tabbott", "debathena")
95    print notLockerOwner("tabbott", "sipb")
96    print notLockerOwner("tabbott", "lsc")
97    print notLockerOwner("tabbott", "scripts")
98    print notLockerOwner("ecprice", "hmmt")
Note: See TracBrowser for help on using the repository browser.