#!/usr/bin/perl
use strict;

my @allowed = qw(/usr/local/cpanel/bin/noshell /bin/false /sbin/nologin /usr/local/cpanel/bin/jailshell /bin/rbash);
my $fallback = '/usr/local/cpanel/bin/jailshell';

# Override if rbash found (bash will drop into rbash via profile script)
if (-e '/etc/profile.d/rbash.sh') {
    @allowed = qw(/usr/local/cpanel/bin/noshell /bin/false /sbin/nologin /bin/bash /bin/rbash);
    $fallback = '/bin/bash';
}

my @cp_users;

open(my $fh, '<', '/etc/trueuserdomains');
	while(<$fh>) {
		my ( undef, $user ) = split(/\s+/);
		push(@cp_users, $user);
	}
close($fh);

open($fh, '<', '/etc/passwd');
	while(<$fh>) {
		chomp(my ( $user, undef, $uid, $gid, undef, undef, $shell ) = split(/:/));
		next unless grep(/^$user$/, @cp_users);
		unless ( grep(/^$shell$/, @allowed) ) {
			print "[!] $user has a bad shell! [$shell]\n";
			system('chsh', '-s', $fallback, $user);
		}
	}
close($fh);
