From 130ce2915743952a8b40e8c3bab4d8bc5848564e Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Mon, 17 Nov 2014 17:24:31 +0000 Subject: Add an is_real_group API This is similar to the is_real_user() API added in the previous commit and will be useful in higher level code which might need to filter the display appropriately. Here we use the heuristic that if the GID is in the range 500-999 then we check to see whether the group is the primary group of a user with the same name (and the user is considered a 'real' user) or that the group has any member who is considered 'real'. mga#14346 --- lib/MDK/Common/System.pm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/MDK/Common/System.pm b/lib/MDK/Common/System.pm index 851a4f1..4319593 100644 --- a/lib/MDK/Common/System.pm +++ b/lib/MDK/Common/System.pm @@ -59,6 +59,10 @@ return the list of users as given by C (see perlfunc) checks whether or not the user is a system user or a real user +=item is_real_group() + +checks whether or not the group is a system group or a real group + =item list_home() return the list of home (eg: /home/foo, /home/pixel, ...) @@ -203,7 +207,7 @@ use MDK::Common::DataStructure; use Exporter; our @ISA = qw(Exporter); -our @EXPORT_OK = qw(%compat_arch $printable_chars $sizeof_int $bitof_int arch distrib typeFromMagic list_passwd is_real_user list_home list_skels list_users syscall_ psizeof availableMemory availableRamMB gettimeofday unix2dos whereis_binary getVarsFromSh setVarsInSh setVarsInShMode addVarsInSh addVarsInShMode setExportedVarsInSh setExportedVarsInCsh template2file template2userfile read_gnomekderc update_gnomekderc fuzzy_pidofs); #); +our @EXPORT_OK = qw(%compat_arch $printable_chars $sizeof_int $bitof_int arch distrib typeFromMagic list_passwd is_real_user is_real_group list_home list_skels list_users syscall_ psizeof availableMemory availableRamMB gettimeofday unix2dos whereis_binary getVarsFromSh setVarsInSh setVarsInShMode addVarsInSh addVarsInShMode setExportedVarsInSh setExportedVarsInCsh template2file template2userfile read_gnomekderc update_gnomekderc fuzzy_pidofs); #); our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); @@ -304,6 +308,28 @@ sub is_real_user { ($uid >= 1000 || ($uid >= 500 && $homedir !~ /^\/($|var\/|run\/)/ && $shell !~ /(nologin|false)$/)) && $username ne "nobody"; } +sub is_real_group { + my ($groupname, $gid) = @_; + + return 0 if $groupname eq "nogroup" || $gid < 500; + return 1 if $gid >= 1000; + + # We are in the range 500-1000, so we need some heuristic. + # We consider ourselves a "real" group if this is the primary group of a user + # with the same name, or we have any member users who are "real" + + my ($username,undef,$uid,$egid,undef,undef,undef,$homedir,$shell,undef) = getpwnam($groupname); + return 1 if $username == $groupname && $egid == $gid; + + my (undef,undef,$egid,$members) = getgrnam($groupname); + return 0 if $egid != $gid; + + foreach (split(' ', $members)) { + my ($username,undef,$uid,undef,undef,undef,undef,$homedir,$shell,undef) = getpwnam($_); + return 1 if is_real_user($username, $uid, $homedir, $shell); + } + return 0; +} sub list_home() { MDK::Common::DataStructure::uniq(map { $_->[7] } grep { is_real_user($_->[0], $_->[2], $_->[7], $_->[8]) } list_passwd()); } -- cgit v1.2.1