Merge "upstream" fixes from SIPB's snippets
authorAlex Dehnert <adehnert@mit.edu>
Mon, 6 Jun 2011 07:10:15 +0000 (03:10 -0400)
committerAlex Dehnert <adehnert@mit.edu>
Mon, 6 Jun 2011 07:10:15 +0000 (03:10 -0400)
__init__.py

index 8f785ae9b8b6331debfaacc5edf9ea92b115bc5e..7958bebec91626471e4a88d2295cbd61109010ac 100644 (file)
@@ -1,3 +1,7 @@
+import subprocess
+import ldap
+import ldap.filter
+
 from django.contrib.auth.middleware import RemoteUserMiddleware
 from django.contrib.auth.backends import RemoteUserBackend
 from django.contrib.auth.views import login
@@ -7,9 +11,12 @@ from django.contrib import auth
 from django.core.exceptions import ObjectDoesNotExist
 import settings
 
-def zephyr(msg, clas='remit', instance='log', rcpt='adehnert',):
-    import os
-    os.system("zwrite -d -c '%s' -i '%s' '%s' -m '%s'" % (clas, instance, rcpt, msg, ))
+def zephyr(msg, clas='message', instance='log', rcpt='nobody',):
+    proc = subprocess.Popen(
+        ['zwrite', '-d', '-n', '-c', clas, '-i', instance, rcpt, ],
+        stdin=subprocess.PIPE, stdout=subprocess.PIPE
+    )
+    proc.communicate(msg)
 
 class ScriptsRemoteUserMiddleware(RemoteUserMiddleware):
     header = 'SSL_CLIENT_S_DN_Email'
@@ -24,12 +31,13 @@ class ScriptsRemoteUserBackend(RemoteUserBackend):
             return username
     def configure_user(self, user, ):
         username = user.username
-        import ldap
+        user.password = "ScriptsSSLAuth"
         con = ldap.open('ldap.mit.edu')
         con.simple_bind_s("", "")
         dn = "dc=mit,dc=edu"
         fields = ['cn', 'sn', 'givenName', 'mail', ]
-        result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
+        userfilter = ldap.filter.filter_format('uid=%s', [username])
+        result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, userfilter, fields)
         if len(result) == 1:
             user.first_name = result[0][1]['givenName'][0]
             user.last_name = result[0][1]['sn'][0]
@@ -38,11 +46,13 @@ class ScriptsRemoteUserBackend(RemoteUserBackend):
                 user.groups.add(auth.models.Group.objects.get(name='mit'))
             except ObjectDoesNotExist:
                 print "Failed to retrieve mit group"
-            user.save()
+        else:
+            raise ValueError, ("Could not find user with username '%s' (filter '%s')"%(username, userfilter))
         try:
             user.groups.add(auth.models.Group.objects.get(name='autocreated'))
         except ObjectDoesNotExist:
             print "Failed to retrieve autocreated group"
+        user.save()
         return user
 
 def scripts_login(request, **kwargs):