From 21e110cc8a0dfc5bfd63c296666d1d70b036c442 Mon Sep 17 00:00:00 2001 From: Ilia Ross Date: Sun, 20 Oct 2024 15:59:41 +0300 Subject: [PATCH] Fix to bring back support for limit in `last` command https://forum.virtualmin.com/t/is-this-process-normal-or-should-i-worry/128835/10?u=ilia --- useradmin/freebsd-lib.pl | 9 +++++---- useradmin/linux-lib.pl | 9 +++++---- useradmin/macos-lib.pl | 7 ++++--- useradmin/user-lib.pl | 8 +++++--- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/useradmin/freebsd-lib.pl b/useradmin/freebsd-lib.pl index 1eed14733..3b0b80573 100755 --- a/useradmin/freebsd-lib.pl +++ b/useradmin/freebsd-lib.pl @@ -17,12 +17,13 @@ sub groupfiles_type return 0; } -# open_last_command(handle, user) +# open_last_command(handle, user, [max]) sub open_last_command { -local ($fh, $user) = @_; -local $quser = quotemeta($user); -open($fh, "(last -w $quser || last $quser) |"); +my ($fh, $user, $max) = @_; +my $quser = quotemeta($user); +$max = " -n $max" if ($max); +open($fh, "(last -w$max $quser || last$max $quser) |"); } # read_last_line(handle) diff --git a/useradmin/linux-lib.pl b/useradmin/linux-lib.pl index f5d848344..678cb1a30 100755 --- a/useradmin/linux-lib.pl +++ b/useradmin/linux-lib.pl @@ -17,12 +17,13 @@ sub groupfiles_type return &password_file($config{'gshadow_file'}) ? 2 : 0; } -# open_last_command(handle, user) +# open_last_command(handle, user, [max]) sub open_last_command { -local ($fh, $user) = @_; -local $quser = quotemeta($user); -open($fh, "(last -F -w $quser || last -w $quser) |"); +my ($fh, $user, $max) = @_; +my $quser = quotemeta($user); +$max = " -n $max" if ($max); +open($fh, "(last -F -w$max $quser || last -w$max $quser) |"); } # read_last_line(handle) diff --git a/useradmin/macos-lib.pl b/useradmin/macos-lib.pl index 39bed222a..622aa2437 100755 --- a/useradmin/macos-lib.pl +++ b/useradmin/macos-lib.pl @@ -54,11 +54,12 @@ else { } } -# open_last_command(handle, user) +# open_last_command(handle, user, [max]) sub open_last_command { -local ($fh, $user) = @_; -open($fh, "last $user |"); +my ($fh, $user, $max) = @_; +$max = " -n $max" if ($max); +open($fh, "last$max $user |"); } # read_last_line(handle) diff --git a/useradmin/user-lib.pl b/useradmin/user-lib.pl index 1cddcc7d0..9cd2700a7 100755 --- a/useradmin/user-lib.pl +++ b/useradmin/user-lib.pl @@ -2552,11 +2552,13 @@ Returns a list of array references, each containing the details of a login. =cut sub list_last_logins { -local @rv; -&open_last_command(LAST, $_[0]); +my ($user, $max) = @_; +my @rv; +undef($max) if (defined($max) && $max <= 0); # sanity check +&open_last_command(LAST, $user, $max); while(@last = &read_last_line(LAST)) { push(@rv, [ @last ]); - if ($_[1] && scalar(@rv) >= $_[1]) { + if ($max && scalar(@rv) >= $max) { last; # reached max } }