classify-users: determine type of Athena accounts master
authorAlex Dehnert <adehnert@mit.edu>
Fri, 8 Nov 2013 06:36:04 +0000 (01:36 -0500)
committerAlex Dehnert <adehnert@mit.edu>
Fri, 8 Nov 2013 06:38:16 +0000 (01:38 -0500)
This script takes a list of usernames (or email addresses, or whatever) and
classifies each as student, staff, etc., or unknown.

Makefile
README
classify-users [new file with mode: 0755]

index 74dfb1a4989bb7eaeb979ef4bba0ded886126fe0..357dbf7a96bc0588446db6bfce58ec6a6a0f39aa 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,7 @@ common=$(archdir)/common/bin/
 
 scripts=\
     bores-me\
+    classify-users\
     diff-memberships\
     get-emails\
     grep-owners\
diff --git a/README b/README
index 1219a51148523fadef039b524dd0cbddc60ffdb3..f3e6573b2e266e8252a3b731ef38fad7f6f5f47f 100644 (file)
--- a/README
+++ b/README
@@ -2,6 +2,7 @@ All scripts in this directory are Copyright (C) 2008--2013 Alex Dehnert
 (and sometimes others)
 
 bores-me    - original
+classify-users  - original
 diff-memberships    - original
 get-emails  - original
 grep-owners - original
diff --git a/classify-users b/classify-users
new file mode 100755 (executable)
index 0000000..657c452
--- /dev/null
@@ -0,0 +1,43 @@
+#!/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)