syncing examples
[tprouty/samba.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 smbldap_tools;
27
28
29 #####################
30
31 use Getopt::Std;
32 my %Options;
33
34 my $ok = getopts('r?', \%Options);
35
36 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
37         print "Usage: $0 [-r?] username\n";
38         print "  -r     remove home directory\n";
39         exit (1);
40 }
41
42 # Read only first @ARGV
43 my $user = $ARGV[0];
44
45 my $dn;
46 # user must not exist in LDAP
47 if (!defined($dn=get_user_dn($user))) {
48     print "$0: user $user does not exist\n";
49     exit (6);
50 }
51
52 if ($< != 0) {
53     print "You must be root to delete an user\n";
54     exit (1);
55 }
56
57 my $homedir;
58 if (defined($Options{'r'})) {
59     $homedir=get_homedir($user);
60 }
61
62 # remove user from groups
63 my $groups = find_groups_of $user;
64 my @grplines = split(/\n/, $groups);
65
66 my $grp;
67 foreach $grp (@grplines) {
68     my $gname = "";
69     if ( $grp =~ /dn: cn=([^,]+),/) {
70         $gname = $1;
71         #print "xx $gname\n";
72     }
73     if ($gname ne "") {
74         group_remove_member($gname, $user);
75     }
76 }
77
78 # XXX
79 delete_user($user);
80
81 # delete dir -- be sure that homeDir is not a strange value
82 if (defined($Options{'r'})) {
83     if ($homedir !~ /^\/dev/ and $homedir !~ /^\/$/) {
84         system "rm -rf $homedir";
85     }
86 }
87
88 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
89
90 if ($nscd_status == 0) {
91    system "/etc/init.d/nscd restart > /dev/null 2>&1";
92 }
93
94 exit (0);
95
96 ############################################################
97
98 =head1 NAME
99
100        smbldap-userdel.pl - Delete a user account and related files
101
102 =head1 SYNOPSIS
103
104        smbldap-userdel.pl [-r] login
105
106 =head1 DESCRIPTION
107
108        The smbldap-userdel.pl command modifies the system
109        account files, deleting all entries that refer to login.
110        The named user must exist.
111
112        -r     Files in the user's home directory will be removed along with
113               the home directory itself. Files located in other file
114               systems will have to be searched for and deleted manually.
115
116 =head1 SEE ALSO
117
118        userdel(1)
119
120 =cut
121
122 #'