From 881a29f2f7bb84e2c90cd2c573a98404339fff1a Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Wed, 3 Mar 2021 22:53:54 -0700 Subject: [PATCH] Girocco/User.pm: add get_full_list and get_full_list_extended Add equivalent functions of the same name to those provided by Girocco/Project.pm that read directly and only from the file that is the source of all truth for the list of users. Signed-off-by: Kyle J. McKay --- Girocco/User.pm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Girocco/User.pm b/Girocco/User.pm index dc8dd1a..2bbb713 100644 --- a/Girocco/User.pm +++ b/Girocco/User.pm @@ -475,5 +475,27 @@ sub resolve_uid { scalar(getpwnam($name)); } +# returns array of names only taken strictly from the passwd file +# only entries with non-invalid names and uid >= 10000 are returned +# a "non-invalid" name is 1 or more non-whitespace or '#' chars, NOT starting with '_' +sub get_full_list { + open my $fd, '<', jailed_file("/etc/passwd") or die "getting user list failed: $!"; + my @users = map {/^([^:_\s#][^:\s#]*):[^:]*:[1-9]\d{4,}:/ ? $1 : ()} <$fd>; + close $fd; + @users; +} + +# returns array of array refs containing all fields (at least 3) of each entry from passwd file +# only entries with non-invalid names and uid >= 10000 are returned +# a "non-invalid" name is 1 or more non-whitespace or '#' chars, NOT starting with '_' +# using +# join(':',@{}) +# will recover the _exact_ original line from the passwd file for that entry +sub get_full_list_extended { + open my $fd, '<', jailed_file("/etc/passwd") or die "getting user list failed: $!"; + my @users = map {chomp; /^([^:_\s#][^:\s#]*):[^:]*:[1-9]\d{4,}:/ ? [split(/:/,$_,-1)] : ()} <$fd>; + close $fd; + @users; +} 1; -- 2.11.4.GIT