1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | # $Id: smbldap-userdel 2643 2006-09-15 09:12:49Z erwin $ |
---|
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 | my $del1 = $ldap_master->modify ("cn=AddressAdmins,o=AddressBook,$config{suffix}",delete => { member => "uid=$user,$config{usersdn}" }); |
---|
84 | $del1->code && warn "failed to delete entry: ", $del1->error ; |
---|
85 | |
---|
86 | |
---|
87 | # XXX |
---|
88 | delete_user($user); |
---|
89 | |
---|
90 | # delete dir -- be sure that homeDir is not a strange value |
---|
91 | if ($homedir) { |
---|
92 | my @rmargs = ( '-r' ); |
---|
93 | if (defined($Options{'R'})) { |
---|
94 | push(@rmargs, '-i'); |
---|
95 | } elsif (defined($Options{'r'})) { |
---|
96 | push(@rmargs, '-f'); |
---|
97 | } |
---|
98 | # print "rm @rmargs $homedir\n"; |
---|
99 | system('rm', @rmargs, $homedir); |
---|
100 | } |
---|
101 | |
---|
102 | my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1"; |
---|
103 | |
---|
104 | if ($nscd_status == 0) { |
---|
105 | system "/etc/init.d/nscd restart > /dev/null 2>&1"; |
---|
106 | } |
---|
107 | system "nscd -i passwd; nscd -i group"; |
---|
108 | |
---|
109 | $ldap_master->unbind; # take down session |
---|
110 | |
---|
111 | exit (0); |
---|
112 | |
---|
113 | ############################################################ |
---|
114 | |
---|
115 | =head1 NAME |
---|
116 | |
---|
117 | smbldap-userdel - Delete a user account and related files |
---|
118 | |
---|
119 | =head1 SYNOPSIS |
---|
120 | |
---|
121 | smbldap-userdel [-r] login |
---|
122 | |
---|
123 | =head1 DESCRIPTION |
---|
124 | |
---|
125 | The smbldap-userdel command modifies the system account files, deleting all entries that refer to user defined in "login". The named user must exist. |
---|
126 | |
---|
127 | -r |
---|
128 | 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. |
---|
129 | |
---|
130 | =head1 SEE ALSO |
---|
131 | |
---|
132 | userdel(1) |
---|
133 | |
---|
134 | =cut |
---|
135 | |
---|
136 | #' |
---|