File: deluser

package info (click to toggle)
adduser 3.47
  • links: PTS
  • area: main
  • in suites: woody
  • size: 340 kB
  • ctags: 54
  • sloc: perl: 1,498; makefile: 77; sh: 75
file content (381 lines) | stat: -rwxr-xr-x 11,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/perl

# deluser -- a utility to remove users from the system
# delgroup -- a utilty to remove groups from the system
$version = VERSION;

# Copyright (C) 2000 Roland Bauerschmidt <rb@debian.org>
# Based on 'adduser' as pattern by
#     Guy Maor <maor@debian.org>
#     Ted Hajek <tedhajek@boombox.micro.umn.edu>
#     Ian A. Murdock <imurdock@gnu.ai.mit.edu>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# the program can be called as:
#
#  deluser user
#     remove a normal user from the system
#     example: deluser mike
#     $action = "deluser"
#
#     --remove-home             remove the users home directory and mail spool
#     --remove-all-files        remove all files owned by user
#     --backup                  backup files before removing
#
#  delgroup group
#  deluser --group group
#     remove a system group from the system
#     example: deluser --group students
#     $action = "delgroup"
#
#  deluser user group
#     remove the user from a group
#     example: deluser mike students
#     $action = "deluserfromgroup"
#
#  general options:
#
#     --quiet | -q      don't give process information to stdout
#     --help | -h       usage message
#     --version | -v    version number and copyright
#     --conf | -c FILE  use FILE instead of /etc/deluser.conf

$ENV{"PATH"} = "/sbin:/bin:/usr/sbin:/usr/bin";

use warnings;

BEGIN {
    eval 'use File::Find';
    if ($@) {
        $NO_FILE_FIND = 1;
    }
}

use Debian::AdduserCommon;

BEGIN {
    eval 'use Locale::gettext';
    if ($@) {
        *gettext = sub { shift };
        *textdomain = sub { "" };
        *LC_MESSAGES = sub { 5 };
    }
    eval {
        require POSIX;
        import POSIX qw(setlocale);
    };
    if ($@) {
        *setlocale = sub { 1 };
    }
}

setlocale(LC_MESSAGES, "");
textdomain("adduser");

$verbose = 1;
$defaults = "/etc/deluser.conf";

$config{"remove_home"} = 0;
$config{"remove_all_files"} = 0;
$config{"backup"} = 0;

$action = $0 =~ /delgroup$/ ? "delgroup" : "deluser";

while($arg = shift(@ARGV))
{
    die "$0: ",_("No options allowed after names.\n")
	if ($names[0] && $arg =~ /^--/);

    if($arg eq "--quiet" || $arg eq "-q") {
	$verbose = 0;
    } elsif($arg eq "--debug") {
        $verbose = 2;
    } elsif($arg eq "--version" || $arg eq "-v") {
	version();
	exit 0;
    } elsif($arg eq "--help" || $arg eq "-h") {
	usage();
	exit 0;
    } elsif($arg eq "--group" || $arg eq "-g") {
	$action = "delgroup";
    } elsif($arg eq "--conf" || $arg eq "-c") {
        die "$0: ",_("--conf requires an argument.\n")
            if (!($defaults = shift(@ARGV)));
        dief (_("`%s' doesn't exist.\n"),$defaults)
            if (! -f $defaults);
    } elsif($arg eq "--remove-home") {
	$pconfig{"remove_home"} = 1;
    } elsif($arg eq "--remove-all-files") {
	$pconfig{"remove_all_files"} = 1;
    } elsif($arg eq "--backup") {
	$pconfig{"backup"} = 1;
    } elsif($arg =~ /^--/ || $arg =~ /^-/) {
	dief (_("Unknown argument `%s'.\n"),$arg);
    } else {
	push @names, $arg;
    }
}

# read configfile and override with commandline arguments
read_config($defaults);
foreach(keys(%pconfig)) {
    $config{$_} = $pconfig{$_};
}

if (($config{remove_home} || $config{remove_all_files} || $config{backup}) &&
	defined($NO_FILE_FIND)) {
    die _("In order to use the --remove-home, --remove-all-files, and --backup features,\nyou need to install the `perl-modules' package. To accomplish that, run\napt-get install perl-modules\n");
}

die "$0: ",_("Only root may remove a user or group from the system.\n") if ($> != 0);
 
if(@names == 0) {
    if($action eq "delgroup") {
	print _("Enter a groupname to remove: ");
    } else {
	print _("Enter a username to remove: ");
    }

    chomp($answer=<STDIN>);
    push(@names, $answer);
}
die "$0: ",_("I need a name to remove.\n") if (length($names[0]) == 0);
die "$0: ",_("No more than two names.\n") if (@names > 2);

if(@names == 2) {      # must be addusertogroup
    die "$0: ",_("Specify only one name in this mode.\n")
        if ($action eq "delgroup");
    $action = "deluserfromgroup";
    $user = shift(@names);
    $group = shift(@names);
} else {
    if($action eq "delgroup") {
	$group = shift(@names);
    } else {
	$user = shift(@names);
    }
}

if($user) {
    #($pw_name,$pw_passwd,$pw_uid,$pw_gid,$pw_quota,$pw_comment,
    # $pw_gecos,$pw_homedir,$pw_shell,$pw_expire) = getpwnam($user);
    my @passwd = getpwnam($user);
    $pw_uid = $passwd[2];
    $pw_gid = $passwd[3];
    $pw_homedir = $passwd[7];
    
    $maingroup = $pw_gid ? getgrgid($pw_gid) : "";
}
if($group) {
    #($gr_name,$gr_passwd,$gr_gid,$gr_members) = getgrnam($group);
    my @group = getgrnam($group);
    $gr_gid = $group[2];
}

# arguments are processed:
#
#  $action = "deluser"
#     $user          name of the user to remove
#
#  $action = "delgroup"
#     $group         name of the group to remove
#
#  $action = "deluserfromgroup"
#     $user          the user to be remove
#     $group         the group to remove him/her from


if($action eq "deluser") {
    &invalidate_nscd();
    unless(exist_user($user)) {
	dief (_("`%s' does not exist.\n"),$user);
    }
    
    if($config{"remove_home"} || $config{"remove_all_files"}) {
	s_print(_("Looking for files to backup/remove...\n"));
	my(@files,@dirs);
	if($config{"remove_home"} && ! $config{"remove_all_files"}) {
	  sub home_match {
	    push(@files, $File::Find::name) 
	      if(-f $File::Find::name);
	    push(@dirs, $File::Find::name)
	      if(-d $File::Find::name);
	  }
	  File::Find::find({wanted => \&home_match, untaint => 1, no_chdir => 1}, $pw_homedir);
	  push(@files, "/var/mail/$user")
	    if(-e "/var/mail/$user");
	} else {
	  sub find_match {
	    no warnings;
	    my ($dev,$ino,$mode,$nlink,$uid,$gid);
	    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
	      ($uid == $pw_uid) &&
		(
		 ($File::Find::name =~ /^\/proc\// && ($File::Find::prune = 1)) ||
		 (-f $File::Find::name && push(@files, $File::Find::name)) ||
		 (-d $File::Find::name && push(@dirs, $File::Find::name))
		);
	  }
	  File::Find::find({wanted => \&find_match, untaint => 1, no_chdir => 1}, '/');
	}

	if($config{"backup"}) {
	    s_print(_("Backuping files to be removed...\n"));
	    systemcall("/bin/tar", "-cf", "$user.tar", "--", @files);
	    if(-e "/usr/bin/bzip2") {
	    	systemcall("/usr/bin/bzip2", "$user.tar");
	    } elsif(-e "/bin/gzip") {
	    	systemcall("/bin/gzip", "$user.tar");
	    }
	}

	if(@files || @dirs) {
	    s_print(_("Removing files...\n"));
	    unlink(@files) if(@files);
	    foreach(reverse(sort(@dirs))) {
		rmdir($_);
	    }
	}
    }

    s_printf(_("Removing user %s...\n"),$user);
    systemcall("userdel", $user);
    &invalidate_nscd();

    systemcall('/usr/local/sbin/deluser.local', $user, $pw_uid,
                $pw_gid, $pw_homedir) if (-x "/usr/local/sbin/deluser.local");

    s_print(_("done.\n"));
} elsif($action eq "delgroup") {
    &invalidate_nscd();
    unless(exist_group($group)) {
	dief (_("`%s' does not exist.\n"),$group);
    }
    # This need to be fixed
    if(system("grep", "-q", "^.*:.*:.*:$gr_gid:.*:.*:.*\$", "/etc/passwd") == 0) {
	dief (_("There are users having `%s' as primary group!\n"),$group);
    }

    s_printf(_("Removing group %s...\n"),$group);
    systemcall("groupdel",$group);
    &invalidate_nscd();
    s_print(_("done.\n"));
}
elsif($action eq "deluserfromgroup")
{
    &invalidate_nscd();
    unless(exist_user($user)) {
	dief (_("`%s' does not exist.\n"),$user);
    }
    unless(exist_group($group)) {
	dief (_("`%s' does not exist.\n"),$group);
    }
    if($maingroup eq $group) {
	die "$0: ",_("You may not remove the user from his/her primary group.\n");
    }

    my @members = get_group_members($group);
    my $ismember = 0;

    for($i = 0; $i <= $#members; $i++) {
	if($members[$i] eq $user) {
	    $ismember = 1;
	    splice(@members,$i,1);
	}
    }

    unless($ismember) {
	dief(_("%s is not a member of group %s.\n"),$user,$group);
    }

    s_printf(_("Removing user %s from group %s...\n"),$user,$group);
    #systemcall("usermod","-G", join(",",@groups), $user );
    systemcall('gpasswd','-M', join(',',@members), $group);
    &invalidate_nscd();
    s_print(_("done.\n"));
}


######

sub version {
    printf("deluser: %s $version\n\n",_("removing user and groups from the system. Version:"));

    print("Copyright (C) 2000 Roland Bauerschmidt <roland\@copyleft.de>\n\n");

    print("deluser is based on adduser by Guy Maor <maor\@debian.org>, Ian Murdock\n",
	  "<imurdock\@gnu.ai.mit.edu> and Ted Hajek <tedhajek\@boombox.micro.umn.edu>\n");

    print("\nThis program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License, /usr/share/common-licenses/GPL, for more details.\n");
}

sub usage {
    printf("deluser: %s $version\n\n",_("removing user and groups from the system. Version:"));

    print("deluser user
  remove a normal user from the system
  example: deluser mike

  --remove-home             remove the users home directory and mail spool
  --remove-all-files        remove all files owned by user
  --backup                  backup files before removing

delgroup group
deluser --group group
  remove a system group from the system
  example: deluser --group students

deluser user group
  remove the user from a group
  example: deluser mike students

general options:
  --quiet | -q      don't give process information to stdout
  --help | -h       usage message
  --version | -v    version number and copyright
  --conf | -c FILE  use FILE instead of $defaults\n\n");

    printf(_("Global configuration is in the file %s.\n"), $defaults);    
}

sub exist_user {
    my $exist_user = shift;
    return(defined getpwnam($exist_user));
}

sub exist_group {
    my $exist_group = shift;
    return(defined getgrnam($exist_group));
}

sub systemcall {
    my $c = join(' ', @_);
    #print "$c\n" if $debugging;
    if (system(@_)) {
        die("$0: `$c' returned error code " . ($?>>8) . ".  Aborting.\n")
	  if ($?>>8);
	die("$0: `$c' exited from signal " . ($?&255) . ".  Aborting.\n");
    }
}