-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadduser-copy-in-chroot
executable file
·71 lines (56 loc) · 1.88 KB
/
adduser-copy-in-chroot
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
#!/usr/bin/perl -w
# Tue 2 Jun 11:04:46 BST 2020
(my $email='ch%christianjaeger,ch')=~ tr/%,/@./;
use strict; use warnings FATAL => 'uninitialized';
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname CHROOTBASE username(s)
Recreate given username(s) in chroot at CHROOTBASE with their own
groups of the same name, with both group and user having the same
uid and gid as outside the chroot.
NOTE: does *not* currently recreate secondary group memberships.
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose=0;
#our $opt_dry;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
#"dry-run"=> \$opt_dry,
) or exit 1;
usage unless @ARGV;
#my @pw= getpwnam $username
#ah I *did* do this:
use Chj::Unix::Object::User;
#use lib "/opt/functional-perl/lib/"; use FP::Repl::Trap; use FP::Repl;
use Chj::Unix::User;
use Chj::xperlfunc ":all";
use Chj::singlequote ":all";
my $chrootbase= shift @ARGV;
my @username= @ARGV;
sub copy {
my ($username)=@_;
my $user= Chj::Unix::Object::User->get_by_nam($username)
or die "no such user: $username";
my $cmd= do {
my @addgroup= ("addgroup", "--gid", $user->gid, $username);
# ^ XX blindly assumes username == groupname, or/also, that
# $user->gid is to the group with the same name as $username.
my @adduser= ("adduser", "--uid", $user->uid, "--gid", $user->gid,
"--disabled-password", $username);
join(";"
,"set -euo pipefail"
,"IFS="
,singlequote_sh_many(@addgroup)
,singlequote_sh_many(@adduser))
};
xxsystem "chrootlogin", $chrootbase, "root", "--", "bash", "-c", $cmd;
print "Done with '$username'.\n";
}
copy $_ for @username;
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;