check in Idealx tools after talking with Olivier Lemaire at idealx dot org.
[Samba.git] / examples / LDAP / smbldap-tools / smbldap-userdel.pl
blobec9cc9a5937863417b0f68187dd5887483403ad3
1 #!/usr/bin/perl
3 # $Id: smbldap-userdel.pl,v 1.1.2.1 2002/06/04 22:25:39 jerry 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 smbldap_tools;
31 #####################
33 use Getopt::Std;
34 my %Options;
36 my $ok = getopts('r?', \%Options);
38 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
39 print "Usage: $0 [-r?] username\n";
40 print " -r remove home directory\n";
41 exit (1);
44 # Read only first @ARGV
45 my $user = $ARGV[0];
47 my $dn;
48 # user must not exist in LDAP
49 if (!defined($dn=get_user_dn($user))) {
50 print "$0: user $user does not exist\n";
51 exit (6);
54 if ($< != 0) {
55 print "You must be root to delete an user\n";
56 exit (1);
59 my $homedir;
60 if (defined($Options{'r'})) {
61 $homedir=get_homedir($user);
64 # remove user from groups
65 my $groups = find_groups_of $user;
66 my @grplines = split(/\n/, $groups);
68 my $grp;
69 foreach $grp (@grplines) {
70 my $gname = "";
71 if ( $grp =~ /dn: cn=([^,]+),/) {
72 $gname = $1;
73 #print "xx $gname\n";
75 if ($gname ne "") {
76 group_remove_member($gname, $user);
80 # XXX
81 delete_user($user);
83 # delete dir -- be sure that homeDir is not a strange value
84 if (defined($Options{'r'})) {
85 if ($homedir !~ /^\/dev/ and $homedir !~ /^\/$/) {
86 system "rm -rf $homedir";
90 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
92 if ($nscd_status == 0) {
93 system "/etc/init.d/nscd restart > /dev/null 2>&1";
96 exit (0);
98 ############################################################
100 =head1 NAME
102 smbldap-userdel.pl - Delete a user account and related files
104 =head1 SYNOPSIS
106 smbldap-userdel.pl [-r] login
108 =head1 DESCRIPTION
110 The smbldap-userdel.pl command modifies the system
111 account files, deleting all entries that refer to login.
112 The named user must exist.
114 -r Files in the user's home directory will be removed along with
115 the home directory itself. Files located in other file
116 systems will have to be searched for and deleted manually.
118 =head1 SEE ALSO
120 userdel(1)
122 =cut