#!/usr/bin/perl -wT my $ID = q$Id: localuserinfo,v 1.6 2007-02-07 13:10:50 jonas Exp $; # # localuserinfo -- List fullname for each user # # Written by Jonas Smedegaard # Copyright 2003-2007 Jonas Smedegaard # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # TODO: Options to print other info than fullname # use strict; use Getopt::Long; use User::pwent; use User::grent; our $verbose = 1; # should we be verbose? my $ignore_badname = 0; # should we ignore bad names? my $include_fullname = 1; my $include_mail = 0; our $custom_string; my $use_custom_string = 0; our @names; # Resolve version number from CVS id my $version = join (' ', (split (' ', $ID))[1..3]); $version =~ s/,v\b//; $version =~ s/(\S+)$/($1)/; our $maildomain_path = '/etc/mailname'; our $maildomain = &get_maildomain; # Parse options, sanity checks unless ( GetOptions ( "quiet|q" => sub { $verbose = 0 }, "fullname|n" => \$include_fullname, "mail|m" => \$include_mail, "custom" => \$use_custom_string, "ignore-badname" => \$ignore_badname, "help|h" => sub { &usage(); exit 0 }, "version|v" => sub { &version(); exit 0 }, "debug" => sub { $verbose = 2 } ) ) { &usage(); exit 1; } while (defined(my $arg = shift(@ARGV))) { push (@names, $arg); } # TODO: Rewrite to batch-resolve userinfo for all users before using any while (my $username = shift @names) { my $string; my ($fullname, @addresses) = &getuserinfo($username); if (defined($custom_string)) { die "Use of custom string not yet implemented!"; } else { my @output; push (@output, $fullname) if ($include_fullname); push (@output, "$username\@$maildomain") if ($include_mail); $string = join(' ', @output); } print "$string\n"; } sub getuserinfo($) { my $username = shift; my $pw = getpwnam($username) || die "Username \"$username\" does not exist."; my ($fullname, $office, $workphone, $homephone, $other) = split /\s*,\s*/, $pw->gecos; my @addresshints; if (defined($other)) { @addresshints = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other; } return ($fullname, @addresshints); } sub get_maildomain { open (MAILDOMAINFILE, "<" . $maildomain_path) || die "can’t open $maildomain_path: $!"; my $string = readline(MAILDOMAINFILE) || die "can’t read $maildomain_path: $!"; close (MAILDOMAINFILE); chomp($string); # FIXME: Do some sanity check of the string - ensure a single word, FQDN syntax etc. return $string; } sub version { printf ("localuserinfo version %s\n\n", $version); print <<'EOF'; List fullname and/or other info for each user. Copyright (C) 2003-2007 Jonas Smedegaard EOF print <<'EOF'; This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, /usr/share/common-licenses/GPL, for more details. EOF } sub usage { printf <<"EOF"; localuserinfo USER [USER...] List fullname and/or other info for each user general options: --quiet | -q don't give process information to stdout --ingore-badname ignore non-existing usernames instead of failing --help | -h usage message --fullname | -n include fullname (enabled by default) --mail | -m include email address --version | -v version number and copyright EOF }