#!/usr/bin/perl use File::Basename; #the command you want to use in the chroot environment my @command = qw(bash cat chmod cp id ls mkdir mv nslookup perl ping pwd rm rmdir rsync scp sh ssh ssh-keygen vi); my ($homedir, $shell); print "Username: "; my $username = ; chomp $username; #Check if the specified user already exists my $user_status; my @etc_passwd = `cat /etc/passwd`; foreach (@etc_passwd){ chomp $_; my($etc_username, $etc_homedir, $etc_shell) = (split(/:/, $_))[0,5,6]; chomp($etc_username, $etc_homedir, $etc_shell); if( $etc_username eq $username ){ $user_status = 1; $homedir = $etc_homedir; $shell = $etc_shell; } } #For existing user if($user_status){ my $mod_user; until($mod_user =~ /(Y|N)/){ print "$username already exists. Set chroot for this user? (Y/N): "; $mod_user = ; chomp $mod_user; } if($mod_user eq 'Y'){ print "usermod -d $homedir/\./ $username\n"; `usermod -d $homedir/\./\ $username`; } exit if($mod_user eq 'N'); } #For new user else{ print "Shell (/bin/bash): "; $shell = ; chomp $shell; print "Home Directory (/home/USRENAME): "; $homedir = ; chomp $homedir; if(!$homedir){ $homedir = '/home' . "/$username"; } my $chroot_homedir = "$homedir" . '/./'; #Create user's home directory if(!-d "$homedir"){ print "making homedirectory..."; `mkdir $homedir`; print "done\n"; } #Add user print "Adding User..."; if($shell){ `useradd -s $shell -d $chroot_homedir $username`; print "done\n"; } else{ `useradd -d $chroot_homedir $username`; print "done\n"; } #Change Password `passwd $username`; #Change the directory permission `chown $username:$username $homedir`; } #change directory chdir($homedir); #Remove the dot file `ls -A |xargs rm -f`; #Create the top directory `mkdir bin`; `mkdir etc`; `mkdir dev`; `mkdir -p usr/bin`; `mkdir -p usr/libexec`; `mkdir -p usr/local/bin`; #set ~/usr/bin/groups open(OUT, "> usr/bin/groups"); print OUT "#!/bin/bash\n\n"; print OUT "id -Gn\n"; close(OUT); `chmod 755 usr/bin/groups`; #create passwd and group files `grep /etc/passwd -e "^root" -e "^$username" > etc/passwd`; `grep /etc/group -e "^root" -e "^$username" > etc/group`; #Copy specified commands and required libraries foreach (@command){ chomp $_; my $command = `which $_`; chomp $command; #Copy specified commands `cp $command \.$command`; #check the associated libs my @libs = `ldd $command`; foreach my $lib (@libs){ chomp $lib; $lib =~ s/^\s+(.*)\(.*\)/$1/g; if($lib =~ /=> /){ $lib =~ s/=> //g; $lib = (split(/ /, $lib))[1]; } #check if the parent directory exists my ($filename, $path) = (fileparse($lib))[0,1]; chomp $filename; chomp $path; if(!-d "\.$path"){ print "mkdir -p \.$path\n"; `mkdir -p \.$path`; } #Copy the required libraries if(!-f "\.$lib"){ print "cp $lib \.$lib\n"; `cp $lib \.$lib`; } } } #Copy the static libraries print "Copy /lib/libnss_*\n"; `cp /lib/libnss_* lib/`; #copy sftp-server program if (-f "/usr/libexec/sftp-server") { print "cp /usr/libexec/sftp-server usr/libexec/sftp-server\n"; `cp /usr/libexec/sftp-server usr/libexec/sftp-server`; } else{ print "/usr/libexec/sftp-server not found!!\n"; } #copy /etc/resolv.conf print "Copy /etc/resolv.conf\n"; `cp /etc/resolv.conf ./etc`; #make special file #make dev/zero my($zero1, $zero2) = ((split/,/, `ls -alh /dev/zero |awk '{ print \$5 \$6 }'`)); print "mknod -m 666 dev/zero c $zero1 $zero2\n"; `mknod -m 666 dev/zero c $zero1 $zero2`; #make dev/null my($null1, $null2) = ((split/,/, `ls -alh /dev/null |awk '{ print \$5 \$6 }'`)); print "mknod -m 666 dev/null c $null1 $null2\n"; `mknod -m 666 dev/null c $null1 $null2`;