X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=ud-fingerserv;h=0a878954f68d8f471928680588041e5ae7cf6b1a;hb=889db862bf2260bd58270ffa2dfa49d8485a9206;hp=67fc24f09218726ad5e5e19817d09700081cc5ee;hpb=5f16f12e574f76e0fd1fe097b8286d06409949ac;p=mirror%2Fuserdir-ldap.git diff --git a/ud-fingerserv b/ud-fingerserv index 67fc24f..0a87895 100755 --- a/ud-fingerserv +++ b/ud-fingerserv @@ -1,18 +1,24 @@ #!/usr/bin/perl -# $Id: ud-fingerserv,v 1.3 1999/10/14 04:28:59 tausq Exp $ +# $Id: ud-fingerserv,v 1.7 1999/10/17 01:42:03 jgg Exp $ # (c) 1999 Randolph Chung. Licensed under the GPL. -use lib 'web'; +use lib '/var/www/userdir-ldap/'; use strict vars; #use Apache::Registry; +use IO::Handle; use IO::Socket; use POSIX qw(:sys_wait_h); +use Getopt::Std; use Util; use Net::LDAP qw(:all); # Global settings... my %config = &Util::ReadConfigFile; +my %opts; +getopts("iqhv", \%opts); +my $use_inetd = $config{use_inetd} || $opts{i}; +$| = 1; my %attrs = ( 'cn' => 'First name', @@ -25,9 +31,60 @@ my %attrs = ( my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key'); -my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary); +$SIG{__DIE__} = \&DieHandler; +$SIG{INT} = \&DieHandler; +$SIG{CHLD} = \&Reaper; + +&help if (defined($opts{h})); +#my $logfh = STDOUT; #TODO + +&log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v})); +my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; +$ldap->bind; + +if (!$use_inetd) { + &log("Binding to port 79") if (defined($opts{v})); + my $server = IO::Socket::INET->new(Proto => 'tcp', + LocalPort => 'finger(79)', + Listen => SOMAXCONN, + Reuse => 1); + + die "Cannot listen on finger port" unless $server; + &log("[Server listening for connections]"); + + my ($pid, $client, $hostinfo); + + while ($client = $server->accept()) { + &log("Forking to handle client request") if (defined($opts{v})); + next if $pid = fork; # parent + die "fork: $!" unless defined $pid; + + # child + $client->autoflush(1); + my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET); + &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost)); + my $query = <$client>; + &ProcessQuery($client, $query); + $client->close; + exit; + } continue { + $client->close; + } +} else { # inetd + &log("inetd mode"); + 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); + exit; +} + +$ldap->unbind; + sub DieHandler { $ldap->unbind if (defined($ldap)); + exit 0; } sub Reaper { @@ -35,42 +92,21 @@ sub Reaper { $SIG{CHLD} = \&Reaper; } -$SIG{__DIE__} = \&DieHandler; -$SIG{CHLD} = \&Reaper; - -$ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!); -$mesg; -$ldap->bind; - -my $server = IO::Socket::INET->new(Proto => 'tcp', - LocalPort => 'finger(79)', - Listen => SOMAXCONN, - Reuse => 1); - -die "Cannot listen on finger port" unless $server; -print "[Server listening for connections]\n"; - -my ($mesg, %entries, $dn, $key, $pid); - -my $client; -while ($client = $server->accept()) { - next if $pid = fork; # parent - die "fork: $!" unless defined $pid; +sub ProcessQuery { + my $client = shift; + my $query = shift; - # child - $client->autoflush(1); - my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET); - printf "[Connect from %s]\n", $hostinfo || $client->peerhost; - my $query = <$client>; + my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data); + $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input my ($uid, $fields) = split(/\//, $query, 2); -# print "Looking up $uid at $config{basedn}, uid=$uid\n"; + &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; - + foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) { $data = $entries->{$dn}; @@ -78,7 +114,7 @@ while ($client = $server->accept()) { foreach (@{$data->{keyfingerprint}}) { push (@{$data->{key}}, "\n".&Util::FetchKey($_)); } - + print $client "$dn\n"; if (!$fields) { foreach $key (@summarykeys) { @@ -88,7 +124,7 @@ while ($client = $server->accept()) { } } } else { -# print "$fields\n"; + # print "$fields\n"; foreach $key (split(/,/, $fields)) { foreach (@{$data->{$key}}) { print $client "$attrs{$key}: "; @@ -97,10 +133,21 @@ while ($client = $server->accept()) { } } } - $client->close; - exit; -} continue { - $client->close; +} + +sub help { + print "fingerserv [-i | -q | -v | -h]\n"; + print "-i = inetd mode; otherwise runs standalone\n"; + print "-q = quiet mode; no output\n"; + print "-v = verbose mode\n"; + print "-h = this help message\n"; + exit 0; } -$ldap->unbind; +sub log { + my $msg = shift; + return if (defined($opts{q})); + + my $time = localtime; + print STDERR "$time $msg\n"; +}