2022-12-01 18:22:46 +09:00
#!/usr/bin/env bash
# disable a user by removing them from the sshallow/sshforward group
# and move them to the sshreject group
2022-12-02 09:23:35 +09:00
# Note that call is ./lock_user.sh -t <user 1> <user 2> ...
# if the -t is not in the first position it will be ignored
2022-12-01 18:22:46 +09:00
# SET TO 1 to TEST [will not move user in groups]
2022-12-02 09:23:35 +09:00
TEST = 0; # no delete, just print
while getopts ":t" opt; do
2022-12-01 18:22:46 +09:00
case " ${ opt } " in
2024-09-27 18:49:36 +09:00
t) # test
2022-12-01 18:22:46 +09:00
TEST = 1;
; ;
2024-09-27 18:49:36 +09:00
\? )
echo "" ;
echo "-t test run, do not lock users" ;
; ;
2022-12-01 18:22:46 +09:00
esac ;
done ;
2022-12-02 09:23:35 +09:00
shift " $(( OPTIND-1)) "
2024-09-27 18:49:36 +09:00
if [ " $( whoami) " != "root" ] ; then
2022-12-02 09:23:35 +09:00
if [ ${ TEST } -eq 0 ] ; then
echo "Script must be run as root user" ;
exit;
else
echo "!!!! Script must be run as root user !!!!" ;
fi ;
fi ;
2022-12-01 18:22:46 +09:00
if [ $# -eq 0 ] ; then
echo "Must give at least one user name" ;
exit;
fi ;
2022-12-02 09:23:35 +09:00
# ignore users (root and admin users)
ignore_users = ( 'root' 'ec2-user' 'ubuntu' 'admin' ) ;
2022-12-01 18:22:46 +09:00
# ssh reject group
ssh_reject_group = "sshreject" ;
2024-09-27 18:49:36 +09:00
if ! grep -q " ${ ssh_reject_group } : " /etc/group; then
2022-12-01 18:22:46 +09:00
echo " Missing ssh reject group: ${ ssh_reject_group } " ;
exit;
fi ;
ssh_allow_group = "sshallow" ;
2023-12-21 16:11:31 +09:00
ssh_forward_group = "sshforward" ;
2022-12-02 09:23:35 +09:00
user_group_tpl = "gpasswd -d %s %s\ngpasswd -a %s %s\n" ;
2022-12-01 18:22:46 +09:00
2024-12-16 15:44:09 +09:00
# base folder for all data
BASE_FOLDER = $( dirname " $( readlink -f " $0 " ) " ) "/" ;
2024-12-09 11:37:37 +09:00
LOG = " ${ BASE_FOLDER } /../log/user_management.log " ;
function write_log( )
{
text = " ${ 1 } " ;
do_echo = " ${ 2 } " ;
log_prefix = "" ;
# log prefix
if [ ${ TEST } -eq 1 ] ; then
log_prefix = "TEST" ;
fi ;
if [ -n " ${ log_prefix } " ] ; then
log_prefix = " [ ${ log_prefix } ] " ;
fi ;
echo " [ $( date +"%F %T" ) ] [ $0 ] ${ log_prefix } ${ text } " >> " ${ LOG } " ;
if [ " ${ do_echo } " = "1" ] ; then
echo " ${ text } " ;
fi ;
}
write_log "START SCRIPT RUN" ;
2022-12-01 18:22:46 +09:00
echo "--------------------->"
# $1 ... $n
for username in " $@ " ; do
2022-12-02 09:23:35 +09:00
# skip if there is an option hidden
2024-12-09 11:37:37 +09:00
# shellcheck disable=SC2154
2022-12-02 09:23:35 +09:00
if [ [ ${ _arg : 0 : 1 } = "-" ] ] ; then
continue ;
fi ;
# skip ignore users, note that if a user is not in the sshallow list anyway
# we skip them too, this is just in case check
2024-09-27 18:49:36 +09:00
if [ [ " ${ ignore_users [*] } " = ~ [ [ :space:] ] ${ username } [ [ :space:] ] ] ] ; then
2024-12-09 11:37:37 +09:00
write_log " [ERROR] User ${ username } is in the ignore user list " "1" ;
2022-12-02 09:23:35 +09:00
continue ;
fi ;
2022-12-01 18:22:46 +09:00
# check that user exists in passwd
2022-12-02 09:23:35 +09:00
if ! id " ${ username } " & >/dev/null; then
2024-12-09 11:37:37 +09:00
write_log " [ERROR] User ${ username } does not exists in /etc/passwd file " "1" ;
2022-12-01 18:22:46 +09:00
continue ;
fi ;
# if not check if in reject list
2022-12-02 09:23:35 +09:00
if id -nGz " ${ username } " | grep -qzxF " ${ ssh_reject_group } " ; then
2024-12-09 11:37:37 +09:00
write_log " [.] User ${ username } already in the ${ ssh_reject_group } list " ;
2022-12-01 18:22:46 +09:00
continue ;
fi ;
# check if user is in sshallow/forward list
ssh_remove_group = '' ;
if id -nGz " ${ username } " | grep -qzxF " ${ ssh_allow_group } " ; then
ssh_remove_group = " ${ ssh_allow_group } " ;
fi ;
# if user is in ssh allow group and ALSO in ssh forward group -> bad
if id -nGz " ${ username } " | grep -qzxF " ${ ssh_forward_group } " ; then
2024-09-27 18:49:36 +09:00
if [ -n " ${ ssh_remove_group } " ] ; then
2024-12-09 11:37:37 +09:00
write_log " [!!!! ERROR !!!!] User ${ username } exists in both ${ ssh_allow_group } and ${ ssh_forward_group } group which should not be allowed. Remove user from one group and run script again. " "1" ;
2022-12-01 18:22:46 +09:00
break;
fi ;
ssh_remove_group = " ${ ssh_forward_group } " ;
fi ;
2024-09-27 18:49:36 +09:00
if [ -n " ${ ssh_remove_group } " ] ; then
2022-12-01 18:22:46 +09:00
# remove user from ssh group and add to reject groups
2024-12-09 11:37:37 +09:00
write_log " [*] User ${ username } will be removed from ${ ssh_remove_group } " "1" ;
2022-12-02 09:23:35 +09:00
if [ ${ TEST } -eq 1 ] ; then
2024-09-27 18:49:36 +09:00
# shellcheck disable=SC2059
2022-12-02 09:23:35 +09:00
printf " ${ user_group_tpl } " " ${ username } " " ${ ssh_remove_group } " " ${ username } " " ${ ssh_reject_group } " ;
else
gpasswd -d " ${ username } " " ${ ssh_remove_group } " ;
gpasswd -a " ${ username } " " ${ ssh_reject_group } " ;
fi ;
2022-12-01 18:22:46 +09:00
else
# skip not ssh user
2024-12-09 11:37:37 +09:00
write_log " [?] User ${ username } not in any ssh allow/foward groups " "1" ;
2022-12-01 18:22:46 +09:00
fi ;
done ;
# __END__