Initial import
[samba] / examples / LDAP / smbldap-tools-0.9.1 / smbldap-userdel
1 #!/usr/bin/perl
2
3 # $Id: smbldap-userdel,v 1.11 2005/01/08 12:04:45 jtournier Exp $
4 #
5 #  This code was developped by IDEALX (http://IDEALX.org/) and
6 #  contributors (their names can be found in the CONTRIBUTORS file).
7 #
8 #                 Copyright (C) 2001-2002 IDEALX
9 #
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.
14 #
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.
19 #
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.
24
25 # Purpose of smbldap-userdel : user (posix,shadow,samba) deletion
26
27 use strict;
28 use FindBin;
29 use FindBin qw($RealBin);
30 use lib "$RealBin/";
31 use smbldap_tools;
32
33
34 #####################
35
36 use Getopt::Std;
37 my %Options;
38
39 my $ok = getopts('rR?', \%Options);
40
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);
47 }
48
49 # Read only first @ARGV
50 my $user = $ARGV[0];
51
52 my $ldap_master=connect_ldap_master();
53
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);
59 }
60
61 if ($< != 0) {
62   print "You must be root to delete an user\n";
63   exit (1);
64 }
65
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);
72   }
73 }
74
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);
80   }
81 }
82
83 # XXX
84 delete_user($user);
85
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');
93   }
94   # print "rm @rmargs $homedir\n";
95   system('rm', @rmargs, $homedir);
96 }
97
98 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
99
100 if ($nscd_status == 0) {
101   system "/etc/init.d/nscd restart > /dev/null 2>&1";
102 }
103
104 $ldap_master->unbind;           # take down session
105
106 exit (0);
107
108 ############################################################
109
110 =head1 NAME
111
112 smbldap-userdel - Delete a user account and related files
113
114 =head1 SYNOPSIS
115
116 smbldap-userdel [-r] login
117
118 =head1 DESCRIPTION
119
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.
121
122 -r
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.
124
125 =head1 SEE ALSO
126
127        userdel(1)
128
129 =cut
130
131 #'