2 # $Id: ud-fingerserv,v 1.8 1999/10/17 02:01:28 tausq 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',
28 'keyfingerprint' => 'Fingerprint',
30 'ircnick' => 'IRC nickname'
33 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'ircnick', 'keyfingerprint', 'key');
35 $SIG{__DIE__} = \&DieHandler;
36 $SIG{INT} = \&DieHandler;
37 $SIG{CHLD} = \&Reaper;
39 &help if (defined($opts{h}));
40 #my $logfh = STDOUT; #TODO
42 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
43 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
47 &log("Binding to port 79") if (defined($opts{v}));
48 my $server = IO::Socket::INET->new(Proto => 'tcp',
49 LocalPort => 'finger(79)',
53 die "Cannot listen on finger port" unless $server;
54 &log("[Server listening for connections]");
56 my ($pid, $client, $hostinfo);
58 while ($client = $server->accept()) {
59 &log("Forking to handle client request") if (defined($opts{v}));
60 next if $pid = fork; # parent
61 die "fork: $!" unless defined $pid;
64 $client->autoflush(1);
65 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
66 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
67 my $query = <$client>;
68 &ProcessQuery($client, $query);
76 my $sockaddr = getpeername(STDIN);
77 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
78 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
80 &ProcessQuery(\*STDOUT, $query);
87 $ldap->unbind if (defined($ldap));
92 1 until (-1 == waitpid(-1, WNOHANG));
93 $SIG{CHLD} = \&Reaper;
100 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
102 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
103 my ($uid, $fields) = split(/\//, $query, 2);
105 &log("Looking up $uid at $config{basedn}, uid=$uid");
107 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
108 $mesg->code && die $mesg->error;
109 $entries = $mesg->as_struct;
111 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
112 $data = $entries->{$dn};
115 foreach (@{$data->{keyfingerprint}}) {
116 push (@{$data->{key}}, "\n".&Util::FetchKey($_));
119 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
120 $data->{mn}->[0], $data->{sn}->[0],
121 $data->{uid}->[0]."\@$config{emailappend}");
123 $data->{email}->[0] =~ s/\s+/ /g;
125 print $client "$dn\n";
127 foreach $key (@summarykeys) {
128 foreach (@{$data->{$key}}) {
129 print $client "$attrs{$key}: ";
130 print $client "$_\n";
135 foreach $key (split(/,/, $fields)) {
136 foreach (@{$data->{$key}}) {
137 print $client "$attrs{$key}: ";
138 print $client "$_\n";
146 print "fingerserv [-i | -q | -v | -h]\n";
147 print "-i = inetd mode; otherwise runs standalone\n";
148 print "-q = quiet mode; no output\n";
149 print "-v = verbose mode\n";
150 print "-h = this help message\n";
156 return if (defined($opts{q}));
158 my $time = localtime;
159 print STDERR "$time $msg\n";