X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=ud-fingerserv;h=17c64a7d7fde94f81282cf3c3e2f77f3ece1c563;hb=db36cbb61b95f316e9f9bc0d89aea2d3bef3dc54;hp=8822d7052c5ac8f7593a69355eaecba0a5a68d0b;hpb=bfbe92408ab9c1b23aa44c0d314896afe1b0fd19;p=mirror%2Fuserdir-ldap.git diff --git a/ud-fingerserv b/ud-fingerserv index 8822d70..17c64a7 100755 --- a/ud-fingerserv +++ b/ud-fingerserv @@ -1,11 +1,11 @@ #!/usr/bin/perl -# $Id: ud-fingerserv,v 1.5 1999/10/16 21:50:52 tausq Exp $ +# $Id: ud-fingerserv,v 1.14 2000/02/21 06:08:32 tausq Exp $ # (c) 1999 Randolph Chung. Licensed under the GPL. -use lib '/home/randolph/projects/userdir-ldap/web'; +use lib '/var/www/userdir-ldap/'; +#use lib '/home/randolph/projects/userdir-ldap/web'; use strict vars; -#use Apache::Registry; use IO::Handle; use IO::Socket; use POSIX qw(:sys_wait_h); @@ -16,7 +16,7 @@ use Net::LDAP qw(:all); # Global settings... my %config = &Util::ReadConfigFile; my %opts; -getopts("iqh", \%opts); +getopts("iqhv", \%opts); my $use_inetd = $config{use_inetd} || $opts{i}; $| = 1; @@ -24,12 +24,14 @@ my %attrs = ( 'cn' => 'First name', 'mn' => 'Middle name', 'sn' => 'Last name', + 'email' => 'Email', 'keyfingerprint' => 'Fingerprint', 'key' => 'Key block', - 'ircnick' => 'IRC nickname' + 'ircnick' => 'IRC nickname', + 'labeledurl' => 'URL' ); -my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key'); +my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeledurl', 'ircnick', 'keyfingerprint', 'key'); $SIG{__DIE__} = \&DieHandler; $SIG{INT} = \&DieHandler; @@ -42,7 +44,7 @@ $SIG{CHLD} = \&Reaper; my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; $ldap->bind; -if ($use_inetd == 0) { +if (!$use_inetd) { &log("Binding to port 79") if (defined($opts{v})); my $server = IO::Socket::INET->new(Proto => 'tcp', LocalPort => 'finger(79)', @@ -63,8 +65,8 @@ if ($use_inetd == 0) { $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET); &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost)); - my $query = <$client>; - &ProcessQuery($client, $query); + my $query = &readdata($client); + &ProcessQuery($client, $query) if (defined($query)); $client->close; exit; } continue { @@ -75,8 +77,8 @@ if ($use_inetd == 0) { my $sockaddr = getpeername(STDIN); my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN)); &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr))); - my $query = ; - &ProcessQuery(\*STDOUT, $query); + my $query = &readdata(\*STDIN); + &ProcessQuery(\*STDOUT, $query) if (defined($query)); exit; } @@ -101,12 +103,22 @@ sub ProcessQuery { $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input my ($uid, $fields) = split(/\//, $query, 2); + if (($uid eq "") || ($uid =~ /^help$/i)) { + &sendhelp($client); + return; + } + &log("Looking up $uid at $config{basedn}, uid=$uid"); $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid"); $mesg->code && die $mesg->error; $entries = $mesg->as_struct; - + + if ($mesg->count == 0) { + print $client "$uid not found at db.debian.org\n"; + exit 0; + } + foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) { $data = $entries->{$dn}; @@ -114,6 +126,12 @@ sub ProcessQuery { foreach (@{$data->{keyfingerprint}}) { push (@{$data->{key}}, "\n".&Util::FetchKey($_)); } + + $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0], + $data->{mn}->[0], $data->{sn}->[0], + $data->{uid}->[0]."\@$config{emailappend}"); + + $data->{email}->[0] =~ s/\s+/ /g; print $client "$dn\n"; if (!$fields) { @@ -151,3 +169,43 @@ sub log { my $time = localtime; print STDERR "$time $msg\n"; } + +sub readdata { + my $fh = shift; + my $in = undef; + my $out = undef; + my $bytesread = 0; + my $ret; + + my $flags= fcntl($fh, F_GETFL, 0) + or die "Can't get flags for socket: $!\n"; + fcntl($fh, F_SETFL, $flags | O_NONBLOCK) + or die "Can't make socket nonblocking: $!\n"; + + while (($bytesread < 1024) && ($out !~ /\n/)) { + $ret = sysread($fh, $in, 1024); + return undef if (!defined($ret) || ($ret == 0)); + $bytesread += $ret; + $out .= $in; + } + + $out =~ /(.*?)\n/; + return $1; +} + +sub sendhelp { + my $client = shift; + + print $client "userdir-ldap finger daemon\n"; + print $client "--------------------------\n"; + print $client "finger [/]\@db.debian.org\n"; + print $client " where uid is the user id of the user\n"; + print $client " the optional attributes parameter specifies what to return\n"; + print $client " if nothing is specified, all attributes are returned.\n"; + print $client " The following attributes are currently supported:\n"; + foreach (@summarykeys) { + print $client " $_ : $attrs{$_}\n"; + } + print $client " Multiple attributes can be separated by commas, like this:\n"; + print $client " finger tux/email,key\@db.debian.org\n"; +}