Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / LDAP / smbldap-tools-0.9.2 / smbldap-userdel
blob4064316b21f4fceeb3b332cfb7112171fe9f4279
1 #!/usr/bin/perl
3 # $Id: smbldap-userdel,v 1.11 2005/01/08 12:04:45 jtournier Exp $
5 # This code was developped by IDEALX (http://IDEALX.org/) and
6 # contributors (their names can be found in the CONTRIBUTORS file).
8 # Copyright (C) 2001-2002 IDEALX
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 # USA.
25 # Purpose of smbldap-userdel : user (posix,shadow,samba) deletion
27 use strict;
28 use FindBin;
29 use FindBin qw($RealBin);
30 use lib "$RealBin/";
31 use smbldap_tools;
34 #####################
36 use Getopt::Std;
37 my %Options;
39 my $ok = getopts('rR?', \%Options);
41 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
42 print_banner;
43 print "Usage: $0 [-r?] username\n";
44 print " -r remove home directory\n";
45 print " -R remove home directory interactively\n";
46 exit (1);
49 # Read only first @ARGV
50 my $user = $ARGV[0];
52 my $ldap_master=connect_ldap_master();
54 my $dn;
55 # user must not exist in LDAP
56 if (!defined($dn=get_user_dn($user))) {
57 print "$0: user $user does not exist\n";
58 exit (6);
61 if ($< != 0) {
62 print "You must be root to delete an user\n";
63 exit (1);
66 my $homedir;
67 if (defined($Options{'r'}) || defined($Options{'R'})) {
68 $homedir=get_homedir($user);
69 if ($homedir !~ /^\/.+\/(.*)$user/) {
70 print "Refusing to delete this home directory: $homedir\n";
71 exit (1);
75 # remove user from groups
76 my @groups = &find_groups_of($user);
77 foreach my $gname (@groups) {
78 if ($gname ne "") {
79 group_remove_member($gname, $user);
83 # XXX
84 delete_user($user);
86 # delete dir -- be sure that homeDir is not a strange value
87 if ($homedir) {
88 my @rmargs = ( '-r' );
89 if (defined($Options{'R'})) {
90 push(@rmargs, '-i');
91 } elsif (defined($Options{'r'})) {
92 push(@rmargs, '-f');
94 # print "rm @rmargs $homedir\n";
95 system('rm', @rmargs, $homedir);
98 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
100 if ($nscd_status == 0) {
101 system "/etc/init.d/nscd restart > /dev/null 2>&1";
104 $ldap_master->unbind; # take down session
106 exit (0);
108 ############################################################
110 =head1 NAME
112 smbldap-userdel - Delete a user account and related files
114 =head1 SYNOPSIS
116 smbldap-userdel [-r] login
118 =head1 DESCRIPTION
120 The smbldap-userdel command modifies the system account files, deleting all entries that refer to user defined in "login". The named user must exist.
123 Files in the user's home directory will be removed along with the home directory itself. Files located in other file systems will have to be searched for and deleted manually.
125 =head1 SEE ALSO
127 userdel(1)
129 =cut