import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / examples / LDAP / smbldap-tools / smbldap-userdel.pl
1 #!/usr/bin/perl
2
3 #  This code was developped by IDEALX (http://IDEALX.org/) and
4 #  contributors (their names can be found in the CONTRIBUTORS file).
5 #
6 #                 Copyright (C) 2001-2002 IDEALX
7 #
8 #  This program is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU General Public License
10 #  as published by the Free Software Foundation; either version 2
11 #  of the License, or (at your option) any later version.
12 #
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17 #
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #  USA.
22
23 # Purpose of smbldap-userdel : user (posix,shadow,samba) deletion
24
25 use strict;
26 use FindBin;
27 use FindBin qw($RealBin);
28 use lib "$RealBin/";
29 use smbldap_tools;
30
31
32 #####################
33
34 use Getopt::Std;
35 my %Options;
36
37 my $ok = getopts('r?', \%Options);
38
39 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
40   print "Usage: $0 [-r?] username\n";
41   print "  -r   remove home directory\n";
42   exit (1);
43 }
44
45 # Read only first @ARGV
46 my $user = $ARGV[0];
47
48 my $dn;
49 # user must not exist in LDAP
50 if (!defined($dn=get_user_dn($user))) {
51   print "$0: user $user does not exist\n";
52   exit (6);
53 }
54
55 if ($< != 0) {
56   print "You must be root to delete an user\n";
57   exit (1);
58 }
59
60 my $homedir;
61 if (defined($Options{'r'})) {
62   $homedir=get_homedir($user);
63 }
64
65 # remove user from groups
66 my $groups = find_groups_of $user;
67 my @grplines = split(/\n/,$groups);
68
69 my $grp;
70 foreach $grp (@grplines) {
71   my $gname = "";
72   if ( $grp =~ /dn: cn=([^,]+),/) {
73         $gname = $1;
74         #print "xx $gname\n";
75   }
76   if ($gname ne "") {
77         group_remove_member($gname, $user);
78   }
79 }
80
81 # XXX
82 delete_user($user);
83
84 # delete dir -- be sure that homeDir is not a strange value
85 if (defined($Options{'r'})) {
86   if ($homedir !~ /^\/dev/ and $homedir !~ /^\/$/) {
87         system "rm -rf $homedir";
88   }
89 }
90
91 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
92
93 if ($nscd_status == 0) {
94    system "/etc/init.d/nscd restart > /dev/null 2>&1";
95 }
96
97 exit (0);
98
99 ############################################################
100
101 =head1 NAME
102
103        smbldap-userdel.pl - Delete a user account and related files
104
105 =head1 SYNOPSIS
106
107        smbldap-userdel.pl [-r] login
108
109 =head1 DESCRIPTION
110
111        The smbldap-userdel.pl command modifies the system
112        account files, deleting all entries that refer to login.
113        The named user must exist.
114
115        -r     Files in the user's home directory will be removed along with
116               the home directory itself. Files located in other file
117               systems will have to be searched for and deleted manually.
118
119 =head1 SEE ALSO
120
121        userdel(1)
122
123 =cut
124
125 #'