Skip to content

Instantly share code, notes, and snippets.

@tbielawa
Created April 7, 2010 00:53
Show Gist options
  • Save tbielawa/358329 to your computer and use it in GitHub Desktop.
Save tbielawa/358329 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import ldap
import re
from accounts import user
DEBUG = 1
def dn2uid(dn):
if "cn=" in dn or "uid=" in dn:
return user.dn2uid(dn)
else:
return ""
def gen_people_proxy(person, fileserver):
"""Generate a proxy line for apache from a username and fileserver"""
print "ProxyPass /~%s http://%s:8000/~%s" % (person, fileserver, person)
print "ProxyPassReverse /~%s http://%s:8000/~%s" % (person, fileserver, person)
def gen_project_proxy(project, fileserver):
"""Generate a proxy line for apache from a project and fileserver"""
print "ProxyPass /%s http://%s:8000/%s" % (project, fileserver, project)
print "ProxyPassReverse /%s http://%s:8000/%s" % (project, fileserver, project)
LDAPURI = "ldap://ldap.csee.wvu.edu"
LDAPBASE = "dc=csee,dc=wvu,dc=edu"
PROJECTS = "ou=auto.projects," + LDAPBASE
PEOPLE = "ou=People," + LDAPBASE
HOME = "ou=auto.home," + LDAPBASE
"""Collect People"""
people_list = {}
db = ldap.initialize(LDAPURI)
results = db.search_s(PEOPLE, ldap.SCOPE_SUBTREE, '(objectClass=*)', ['registeredAddress'])
legit_people_list = [ dn2uid(entry[0]) for entry in results if len(entry[1]) == 1 ]
# Get the fileserver from the automount information string
for bro in legit_people_list:
filter = "(cn=%s)" % bro
res = db.search_s(HOME, ldap.SCOPE_SUBTREE, filter, ['automountInformation'])
if len(res) == 1:
# This line is nasty but there is no other way about it
fsinfo = res[0][1]['automountInformation'][0]
match = re.search("fileserver[0-9]{3}", fsinfo)
# Add legit entries to the list
if match:
people_list[bro] = match.group(0)
for k,v in people_list.iteritems():
if DEBUG == 1:
gen_people_proxy(k, v)
else:
pass
""" Collect Projects"""
project_list = {}
db = ldap.initialize(LDAPURI)
results = db.search_s(PROJECTS, ldap.SCOPE_SUBTREE, '(objectClass=automount)', ['automountInformation'])
legit_projects_list = [ entry for entry in results if len(entry[1]) == 1 ]
# Get the fileserver from the automount information string
for dn, autoInfo in legit_projects_list:
cn = dn2uid(dn)
fsinfo = autoInfo['automountInformation'][0]
match = re.search("fileserver[0-9]{3}", fsinfo)
# Add legit entries to the list
if match:
project_list[cn] = match.group(0)
for k, v in project_list.iteritems():
if DEBUG == 1:
gen_project_proxy(k, v)
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment