#!/usr/bin/python import ldap import ldap.filter import sys class LdapDirectory(object): def __init__(self, ): self.con = ldap.open('ldap-too.mit.edu') self.con.simple_bind_s("", "") def ldap_lookup(self, username): dn = "ou=users,ou=moira,dc=mit,dc=edu" fields = ['eduPersonAffiliation', 'mitDirStudentYear'] userfilter = ldap.filter.filter_format('uid=%s', [username]) result = self.con.search_s(dn, ldap.SCOPE_SUBTREE, userfilter, fields) if len(result) > 0: if len(result) > 1: print >>sys.stderr, "Unexpectedly received multiple results: %s" % (result, ) return result[0][1] else: return False def classify(ldap_result): secondary = "-" if ldap_result == False: primary = "unknown" elif ldap_result == {}: primary = "secret" else: primary = ldap_result['eduPersonAffiliation'][0] secondary = ldap_result.get('mitDirStudentYear', ['-'])[0] return primary, secondary if __name__ == '__main__': print >>sys.stderr, "Connecting to LDAP..." directory = LdapDirectory() print >>sys.stderr, "Starting lookups..." for name in sys.stdin: name = name.strip() result = directory.ldap_lookup(name) primary, secondary = classify(result) print "%s\t%s\t%s" % (primary, secondary, name)