Finger server prototype
[mirror/userdir-ldap.git] / ud-fingerserv
1 #!/usr/bin/perl
2 # $Id: ud-fingerserv,v 1.1 1999/10/04 06:51:45 tausq Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib 'web';
7 use strict vars;
8 #use Apache::Registry;
9 use IO::Socket;
10 use Util;
11 use Net::LDAP qw(:all);
12
13 # Global settings...
14 my %config = &Util::ReadConfigFile;
15
16 my %attrs = (
17   'cn' => 'First name',
18   'mn' => 'Middle name',
19   'sn' => 'Last name',
20   'keyfingerprint' => 'Fingerprint'
21 );
22
23 my @summarykeys = ('cn', 'mn', 'sn', 'keyfingerprint');
24
25 my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
26 sub DieHandler {
27   $ldap->unbind if (defined($ldap));
28 }
29
30 $SIG{__DIE__} = \&DieHandler;
31
32 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
33 $mesg;
34 $ldap->bind;
35
36 my $server = IO::Socket::INET->new(Proto => 'tcp', 
37                                    LocalPort => 'finger(79)',
38                                    Listen => SOMAXCONN,
39                                    Reuse => 1);
40
41 die "Cannot listen on finger port" unless $server;
42 print "[Server listening for connections]\n";
43
44 my ($mesg, %entries, $dn, $key);
45
46 my $client;
47 while ($client = $server->accept()) {
48   $client->autoflush(1);
49   my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
50   printf "[Connect from %s]\n", $hostinfo || $client->peerhost;
51   my $query = <$client>;
52   $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
53   my ($uid, $fields) = split(/\//, $query, 2);
54   
55   print "Looking up $uid at $config{basedn}, uid=$uid\n";
56
57   $mesg = $ldap->search(base  => $config{basedn}, filter => "uid=$uid");
58   $mesg->code && die $mesg->error;
59   $entries = $mesg->as_struct;
60
61   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
62     $data = $entries->{$dn};
63
64     if (!$fields) {
65       foreach $key (@summarykeys) {
66         foreach (@{$data->{$key}}) {
67           print $client "$attrs{$key}: ";
68           print $client "$_\n";
69         }
70       }
71     } else {
72       print "$fields\n";
73       foreach $key (split(/,/, $fields)) {
74         foreach (@{$data->{$key}}) {
75           print $client "$attrs{$key}: ";
76           print $client "$_\n";
77         }
78       }
79     }
80   }
81   $client->close;
82 }
83
84 $ldap->unbind;