2 # $Id: ud-fingerserv,v 1.7 1999/10/17 01:42:03 jgg Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
6 use lib '/var/www/userdir-ldap/';
11 use POSIX qw(:sys_wait_h);
14 use Net::LDAP qw(:all);
17 my %config = &Util::ReadConfigFile;
19 getopts("iqhv", \%opts);
20 my $use_inetd = $config{use_inetd} || $opts{i};
25 'mn' => 'Middle name',
27 'keyfingerprint' => 'Fingerprint',
29 'ircnick' => 'IRC nickname'
32 my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key');
34 $SIG{__DIE__} = \&DieHandler;
35 $SIG{INT} = \&DieHandler;
36 $SIG{CHLD} = \&Reaper;
38 &help if (defined($opts{h}));
39 #my $logfh = STDOUT; #TODO
41 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
42 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
46 &log("Binding to port 79") if (defined($opts{v}));
47 my $server = IO::Socket::INET->new(Proto => 'tcp',
48 LocalPort => 'finger(79)',
52 die "Cannot listen on finger port" unless $server;
53 &log("[Server listening for connections]");
55 my ($pid, $client, $hostinfo);
57 while ($client = $server->accept()) {
58 &log("Forking to handle client request") if (defined($opts{v}));
59 next if $pid = fork; # parent
60 die "fork: $!" unless defined $pid;
63 $client->autoflush(1);
64 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
65 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
66 my $query = <$client>;
67 &ProcessQuery($client, $query);
75 my $sockaddr = getpeername(STDIN);
76 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
77 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
79 &ProcessQuery(\*STDOUT, $query);
86 $ldap->unbind if (defined($ldap));
91 1 until (-1 == waitpid(-1, WNOHANG));
92 $SIG{CHLD} = \&Reaper;
99 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
101 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
102 my ($uid, $fields) = split(/\//, $query, 2);
104 &log("Looking up $uid at $config{basedn}, uid=$uid");
106 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
107 $mesg->code && die $mesg->error;
108 $entries = $mesg->as_struct;
110 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
111 $data = $entries->{$dn};
114 foreach (@{$data->{keyfingerprint}}) {
115 push (@{$data->{key}}, "\n".&Util::FetchKey($_));
118 print $client "$dn\n";
120 foreach $key (@summarykeys) {
121 foreach (@{$data->{$key}}) {
122 print $client "$attrs{$key}: ";
123 print $client "$_\n";
128 foreach $key (split(/,/, $fields)) {
129 foreach (@{$data->{$key}}) {
130 print $client "$attrs{$key}: ";
131 print $client "$_\n";
139 print "fingerserv [-i | -q | -v | -h]\n";
140 print "-i = inetd mode; otherwise runs standalone\n";
141 print "-q = quiet mode; no output\n";
142 print "-v = verbose mode\n";
143 print "-h = this help message\n";
149 return if (defined($opts{q}));
151 my $time = localtime;
152 print STDERR "$time $msg\n";