]> snippets.scripts.mit.edu Git - Scripts/git/.git/blob - django/mit/__init__.py
Properly escape the zwrite command line
[Scripts/git/.git] / django / mit / __init__.py
1 import subprocess
2
3 from django.contrib.auth.middleware import RemoteUserMiddleware
4 from django.contrib.auth.backends import RemoteUserBackend
5 from django.contrib import auth
6 from django.core.exceptions import ObjectDoesNotExist
7
8 def zephyr(msg, clas='remit', instance='log', rcpt='adehnert',):
9     proc = subprocess.Popen(
10         ['zwrite', '-d', '-n', '-c', clas, '-i', instance, rcpt, ],
11         stdin=subprocess.PIPE, stdout=subprocess.PIPE
12     )
13     proc.communicate(msg)
14
15 class ScriptsRemoteUserMiddleware(RemoteUserMiddleware):
16     header = 'SSL_CLIENT_S_DN_Email'
17
18 class ScriptsRemoteUserBackend(RemoteUserBackend):
19     def clean_username(self, username, ):
20         if '@' in username:
21             name, domain = username.split('@')
22             assert domain.upper() == 'MIT.EDU'
23             return name
24         else:
25             return username
26     def configure_user(self, user, ):
27         username = user.username
28         user.password = "ScriptsSSLAuth"
29         import ldap
30         con = ldap.open('ldap.mit.edu')
31         con.simple_bind_s("", "")
32         dn = "dc=mit,dc=edu"
33         fields = ['cn', 'sn', 'givenName', 'mail', ]
34         result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
35         if len(result) == 1:
36             user.first_name = result[0][1]['givenName'][0]
37             user.last_name = result[0][1]['sn'][0]
38             user.email = result[0][1]['mail'][0]
39             try:
40                 user.groups.add(auth.models.Group.objects.get(name='mit'))
41             except ObjectDoesNotExist:
42                 print "Failed to retrieve mit group"
43         try:
44             user.groups.add(auth.models.Group.objects.get(name='autocreated'))
45         except ObjectDoesNotExist:
46             print "Failed to retrieve autocreated group"
47         user.save()
48         return user