Newer
Older
#!/usr/bin/python
import ldap
import traceback
import os
import stat
class ldapSearchConfig:
def __init__(self):
self.ldapserver=""
self.binddn=""
self.bindpw=""
self.baseDN=""
self.searchFilter=""
self.cacertfile=''
class genericUser:
def __init__(self):
self.dn=""
self.cn=""
self.entry=""
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def get_users(server):
# ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,server.cacertfile)
ldap.set_option( ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER )
l=ldap.initialize(server.ldapserver)
l.simple_bind_s(server.binddn,server.bindpw)
retrieveAttributes = ["*"]
searchScope=ldap.SCOPE_SUBTREE
try:
ldap_result_id = l.search(server.baseDN,searchScope,server.searchFilter,retrieveAttributes)
except ldap.LDAPError, e:
pass
rtype,rdata = l.result(ldap_result_id,1)
allusers={}
for user in rdata:
dn=user[0]
attrs=user[1]
allusers[dn]=genericUser()
allusers[dn].dn=dn
allusers[dn].entry=attrs
return allusers
def mk_homedir(path,uidNumber,gidNumber):
try:
statinfo = os.stat(path)
except OSError as e:
if 'No such file or directory' in e:
os.mkdir(path,0700)
statinfo = os.stat(path)
if stat.S_ISDIR(statinfo.st_mode):
if statinfo.st_gid != gidNumber or statinfo.st_uid!=uidNumber:
os.chown(path,uidNumber,gidNumber)
else:
raise Exception("users homedirectory is not a directory %s"%path)
# adapted from http://stackoverflow.com/questions/5994840/how-to-change-the-user-and-group-permissions-for-a-directory-by-name
def recursive_chown(path,uidNumber,gidNumber):
si = os.stat(path)
# just a precaution before we recursively apply uid/gid to a path
if si.st_uid != uidNumber or si.st_gid != gidNumber:
raise Exception("user home %s uid %d != %d, gid %d != %d mismatch" % (path,si.st_uid,uidNumber,si.st_gid,gidNumber))
for root, dirs, files in os.walk(path):
for dname in dirs:
os.chown(os.path.join(root, dname), uidNumber, gidNumber)
for fname in files:
os.chown(os.path.join(root, fname), uidNumber, gidNumber)
def cp_skel(skelroot, path, uidNumber, gidNumber):
if skelroot is None:
# assumes NO trailing / on the home path or dead meat
skelroot = path.rsplit("/", 1)[0]
# fix this later if your common/skel is located elsewhere
skelpath = os.path.join(skelroot, 'common', 'skel')
if os.path.isdir(skelpath):
# copy the skel into the user $HOME
# os.system("/bin/cp -r %s/* %s" % (skelpath, path))
subprocess.call(['/bin/cp', '-r', "%s/*" % skelpath, path])
# os.system("/bin/cp %s/\.* %s" % (skelpath, path))
subprocess.call(['/bin/cp', "%s/.*" % skelpath, path])
# ideally use this, but it assumes 'path' does not exist
# shutil.copytree(skelpath, path)
# chown to user ownership
recursive_chown(path, uidNumber, gidNumber)
else:
raise Exception("skel path is missing %s" % skelpath)
s=ldapSearchConfig()
s.ldapserver="{{ ldapURI }}"
s.binddn="{{ ldapBindDN }}"
s.bindpw="{{ ldapBindDNPassword }}"
s.baseDN="{{ ldapBase }}"
s.searchFilter = "{{ search_filter }}"
homeDirEntry= "{{ homeDirEntry }}"
Chris Hines
committed
mnthome = "{{ mnthome }}"
if mnthome != "":
path=mnthome+"/"+users[user].entry[homeDirEntry][0].rsplit("/",1)[1]
else:
path=users[user].entry[homeDirEntry][0]
mk_homedir(None, path,int(users[user].entry['uidNumber'][0]),int(users[user].entry['gidNumber'][0]))
cp_skel(None, path,int(users[user].entry['uidNumber'][0]),int(users[user].entry['gidNumber'][0]))