[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Deleting home dirs of deleted users



Hi cipux-devel,
hi debian edu,

we were recently asked to provide a method to automatically remove user 
directories of deleted users.

To accomplish that task we wrote a python script which can be called by cron 
an which reads all users in the ldap database. That user list is compared with 
the list of directories in /skole/tjener/home0, and all directories which do 
not fit to a user name and which are not owned by root are removed.

Use that script at your own risk - it can severly damage your system!

Regards,

Roland
-- 
Roland Wolters
Tel.: +49 (0) 21 61 / 46 43-181
Mobil: +49 (0) 160 / 74 52 624
pgp fingerprint: D2DE 4612 9C7D 7B7A 420D 84A8 E900 29F9 C16C 2283

credativ GmbH, HRB Mönchengladbach 12080 
Hohenzollernstr. 133, 41061 Mönchengladbach 
Geschäftsführung: Dr. Michael Meskes, Jörg Folz
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
### Author:
# Roland Wolters (roland.wolters@credativ.de)
# Timo Wedemeier
#
### Description:
# This script checks for all active users in the ldap directory
# and compares that list against the existing user directories of
# of a SkoleLinux installation. User directories which are not
# named after an existing user and do not belong to root are
# removed.
#
### Version
# v0.1, 2009-08-26
#
### Usage:
# The script doesn't take any arguments. Just call it and watch.
#
### Licence and Responsibility:
#
# Copyright (c) 2009, credativ GmbH
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
#   copyright notice, this list of conditions and the following 
#   disclaimer in the documentation and/or other materials provided
#   with the distribution.
# * Neither the name of the credativ GmbH nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

import os, ldap, sys
import shutil
from sets import Set

## Our global variables
homedir ='/skole/tjener/home0'

### Our functions.

def list_home_dirs(homedir):  
    dir_list = os.listdir(homedir)
    return dir_list                 

def list_ldap_users():
    ldap_server=ldap.initialize("ldap://tjener.intern:389";)
    ldap_search = ldap_server.search_s('ou=Group,dc=skole,dc=skolelinux,dc=no', ldap.SCOPE_SUBTREE, '(cn=*)', ['sn'])
    user_list = []
    for i in range(len(ldap_search)):
      user_list.append(ldap_search[i][0].split(',',1)[0].split('=')[1])
    return user_list

def compare(list,list2):
    set1 = set(list2) 
    set2 = set(list)
    set_diff = set2-set1
    return set_diff

def del_home(homedir,set_diff):

    for i in set_diff:
      if os.lstat(homedir+'/'+i)[4]!=0:
        shutil.rmtree(homedir+'/'+i)
#        print 'Directory deleted: '+i
    
### Main part

home_dirs = list_home_dirs(homedir)
real_users = list_ldap_users()
set_diff = compare(home_dirs,real_users)

del_home(homedir,set_diff)

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: