From 15e81c84bef76af267bae139b5cda666cb3f95b2 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 16 Sep 2010 20:30:14 +0200 Subject: Support group, members, groups and @-appended grouphints. --- localuserinfo | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/localuserinfo b/localuserinfo index 40fec4f..72a7efc 100755 --- a/localuserinfo +++ b/localuserinfo @@ -40,6 +40,10 @@ my $include_homephone = 1; my $include_other = 0; my $include_addresshints = 1; my $include_mail = 0; +my $include_groupname = 0; +my $include_members = 0; +my $include_groups = 0; +my $include_grouphints = 1; our $custom_template; our @names; @@ -65,6 +69,10 @@ unless ( "other" => \$include_other, "addresshints" => \$include_addresshints, "mail|m" => \$include_mail, + "groupname" => \$include_groupname, + "members" => \$include_members, + "groups" => \$include_groups, + "grouphints" => \$include_grouphints, "custom=s" => \$custom_template, "ignore-badname" => \$ignore_badname, "help|h" => sub { &usage(); exit 0 }, @@ -91,14 +99,26 @@ push (@infochunks, 'homephone') if ($include_homephone); push (@infochunks, 'other') if ($include_other); push (@infochunks, 'addresshints') if ($include_addresshints); push (@infochunks, 'mailaddress') if ($include_mail); +push (@infochunks, 'groupname') if ($include_groupname); +push (@infochunks, 'members') if ($include_members); +push (@infochunks, 'groups') if ($include_groups); +push (@infochunks, 'grouphints') if ($include_grouphints); my $template = $custom_template ? $custom_template : '%' . join('% %', @infochunks) . '%'; +my %groups; +while (my $gr = getgrent()) { + + foreach my $member (@{$gr->members}) { + push @{$groups{$member}}, $gr->name unless ($member eq $gr->name); + } +} + # TODO: Rewrite to batch-resolve userinfo for all users before using any while (my $username = shift @names) { my $string; - my ($fullname, $office, $workphone, $homephone, $other, @officehints, @addresshints) = &getuserinfo($username); + my ($fullname, $office, $workphone, $homephone, $other, $groupname, $members, $groups, @officehints, @addresshints, @grouphints) = &getuserinfo($username); my $mailaddress = "$username\@$maildomain"; $string = $template; @@ -111,8 +131,10 @@ while (my $username = shift @names) { $string =~ s/\%officehints\%/@officehints/g; $string =~ s/\%addresshints\%/@addresshints/g; $string =~ s/\%mailaddress\%/$mailaddress/g; -# $string =~ s/\%groups\%/$groups/g; -# $string =~ s/\%grouphints\%/$grouphints/g; + $string =~ s/\%groupname\%/$groupname/g; + $string =~ s/\%members\%/$members/g; + $string =~ s/\%groups\%/$groups/g; + $string =~ s/\%grouphints\%/@grouphints/g; print "$string\n"; } @@ -120,7 +142,8 @@ while (my $username = shift @names) { sub getuserinfo($) { my $username = shift; - my $pw = getpwnam($username) || die "Username \"$username\" does not exist."; + my $pw = getpwnam($username) || die "Failed locating user \"$username\"."; + my $gr = getgrgid($pw->gid) || die "Failed locating primary group of user \"$username\"."; my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos; my @officehints = grep {s/^([\.[:alnum:]_-]+)$/%$1/} split /\s+/, $office; @@ -130,8 +153,12 @@ sub getuserinfo($) { } else { $other = ""; } + my $groupname = $gr->name; + my $members = $gr->members; + my $groups = join ' ', defined($groups{$username}) ? @{$groups{$username}} : ''; + my @grouphints = grep {s/^([\.[:alnum:]_-]+)$/\@$1/} split /\s+/, $groups; - return ($fullname, $office, $workphone, $homephone, $other, @officehints, @addresshints); + return ($fullname, $office, $workphone, $homephone, $other, $groupname, $members, $groups, @officehints, @addresshints, @grouphints); } sub get_maildomain { @@ -187,6 +214,11 @@ general options: --addresshints include address hints: words in "other" field containing "@" --mail | -m include email address: USERNAME\@MAILDOMAIN + --groupname include primary group + --members include members of primary group + --groups include secondary groups + --grouphints include group hints: secondary groups with "@" + appended --custom=TEMPLATE custom template, e.g. '%username% (%fullname%)' available infochunks: * username @@ -198,6 +230,10 @@ general options: other * addresshints * mailaddress + groupname + members + groups + * grouphint (all marked infosnippets are included by default, in the order listed) --version | -v version number and copyright -- cgit v1.2.3