I noticed that a login lookup on db.debian.org can take up several
[mirror/userdir-ldap.git] / ud-fingerserv
1 #!/usr/bin/perl
2 # $Id: ud-fingerserv,v 1.17 2004/10/24 18:35:31 joey Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '/var/www/userdir-ldap/';
7 #use lib '/home/randolph/projects/userdir-ldap/web';
8 use strict vars;
9 use IO::Handle;
10 use IO::Socket;
11 use POSIX qw(:sys_wait_h);
12 use Getopt::Std;
13 use Util;
14 use Net::LDAP qw(:all);
15
16 # Global settings...
17 my %config = &Util::ReadConfigFile;
18 my %opts;
19 getopts("iqhv", \%opts);
20 my $use_inetd = $config{use_inetd} || $opts{i}; 
21 $| = 1;
22
23 my %attrs = (
24   'cn' => 'First name',
25   'mn' => 'Middle name',
26   'sn' => 'Last name',
27   'email' => 'Email',
28   'keyfingerprint' => 'Fingerprint',
29   'key' => 'Key block',
30   'ircnick' => 'IRC nickname',
31   'icquin' => 'ICQ UIN',
32   'labeleduri' => 'URL'
33 );
34
35 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'keyfingerprint', 'key');
36
37 $SIG{__DIE__} = \&DieHandler;
38 $SIG{INT} = \&DieHandler;
39 $SIG{CHLD} = \&Reaper;
40
41 &help if (defined($opts{h}));
42 #my $logfh = STDOUT; #TODO
43
44 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
45 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; 
46 $ldap->bind;
47
48 if (!$use_inetd) {
49   &log("Binding to port 79") if (defined($opts{v}));
50   my $server = IO::Socket::INET->new(Proto => 'tcp', 
51                                      LocalPort => 'finger(79)',
52                                      Listen => SOMAXCONN,
53                                      Reuse => 1);
54
55   die "Cannot listen on finger port" unless $server;
56   &log("[Server listening for connections]");
57
58   my ($pid, $client, $hostinfo);
59
60   while ($client = $server->accept()) {
61     &log("Forking to handle client request") if (defined($opts{v}));
62     next if $pid = fork; # parent
63     die "fork: $!" unless defined $pid;
64   
65     # child
66     $client->autoflush(1);
67     my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
68     &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
69     my $query = &readdata($client);
70     &ProcessQuery($client, $query) if (defined($query));
71     $client->close;
72     exit;
73   } continue {
74     $client->close;
75   }
76 } else { # inetd
77   &log("inetd mode");
78   my $sockaddr = getpeername(STDIN);
79   if ($sockaddr) {
80     my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
81     &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
82   } else {
83     &log("[Connect via terminal]");
84   }
85   my $query = &readdata(\*STDIN);
86   &ProcessQuery(\*STDOUT, $query) if (defined($query));
87   exit;
88 }
89
90 $ldap->unbind;
91
92 sub DieHandler {
93   $ldap->unbind if (defined($ldap));
94   exit 0;
95 }
96
97 sub Reaper {
98   1 until (-1 == waitpid(-1, WNOHANG));
99   $SIG{CHLD} = \&Reaper;
100 }
101
102 sub ProcessQuery {
103   my $client = shift;
104   my $query = shift;
105   
106   my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
107
108   $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
109   my ($uid, $fields) = split(/\//, $query, 2);
110   
111   if (($uid eq "") || ($uid =~ /^help$/i)) {
112     &sendhelp($client);
113     return;
114   }
115   
116   &log("Looking up $uid at $config{basedn}, uid=$uid");
117
118   $mesg = $ldap->search(base  => $config{basedn}, filter => "uid=$uid");
119   $mesg->code && die $mesg->error;
120   $entries = $mesg->as_struct;
121
122   if ($mesg->count == 0) {
123     print $client "$uid not found at db.debian.org\n";
124     exit 0;
125   }
126
127   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
128     $data = $entries->{$dn};
129
130     $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
131                                   $data->{mn}->[0], $data->{sn}->[0],
132                                   $data->{uid}->[0]."\@$config{emailappend}");
133                                   
134     $data->{email}->[0] =~ s/\s+/ /g;                             
135  
136     my @keyfingerprint = ();
137     for (my $i=0; $i <= $#{$data->{'keyfingerprint'}}; $i++) {
138       push (@keyfingerprint, $data->{keyfingerprint}->[$i]);
139       $data->{keyfingerprint}->[$i] = &Util::FormatFingerPrint($data->{keyfingerprint}->[$i]);
140       $data->{keyfingerprint}->[$i] =~ s,&nbsp;, ,;
141     }
142     print $client "$dn\n";
143     if (!$fields) {
144       push (@{$data->{key}}, sprintf ("finger %s/key\@db.debian.org", $uid));
145       foreach $key (@summarykeys) {
146         foreach (@{$data->{$key}}) {
147           print $client "$attrs{$key}: ";
148           print $client "$_\n";
149         }
150       }
151     } else {
152   #     print "$fields\n";
153       foreach $key (split(/,/, $fields)) {
154         if ($key eq 'key') {
155           foreach (@keyfingerprint) {
156             push (@{$data->{key}}, "\n".&Util::FetchKey($_));
157           }
158         }
159         foreach (@{$data->{$key}}) {
160           print $client "$attrs{$key}: ";
161           print $client "$_\n";
162         }
163       }
164     }
165   }
166 }  
167
168 sub help {
169   print "fingerserv [-i | -q | -v | -h]\n";
170   print "-i = inetd mode; otherwise runs standalone\n";
171   print "-q = quiet mode; no output\n";
172   print "-v = verbose mode\n";
173   print "-h = this help message\n";
174   exit 0;
175 }
176
177 sub log {
178   my $msg = shift;
179   return if (defined($opts{q}));
180   
181   my $time = localtime;
182   print STDERR "$time $msg\n";
183 }
184
185 sub readdata {
186   my $fh = shift;
187   my $in = undef;
188   my $out = undef;
189   my $bytesread = 0;
190   my $ret;
191
192   my $flags= fcntl($fh, F_GETFL, 0)
193      or die "Can't get flags for socket: $!\n";
194   fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
195      or die "Can't make socket nonblocking: $!\n";
196                                                 
197   while (($bytesread < 1024) && ($out !~ /\n/)) {
198     $ret = sysread($fh, $in, 1024);
199     return undef if (!defined($ret) || ($ret == 0));
200     $bytesread += $ret;
201     $out .= $in;
202   }
203
204   $out =~ /(.*?)\n/;
205   return $1;
206 }
207
208 sub sendhelp {
209   my $client = shift;
210   
211   print $client "userdir-ldap finger daemon\n";
212   print $client "--------------------------\n";
213   print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
214   print $client "  where uid is the user id of the user\n";
215   print $client "  the optional attributes parameter specifies what to return\n";
216   print $client "    if nothing is specified, all attributes are returned.\n";
217   print $client "    The following attributes are currently supported:\n";
218   foreach (@summarykeys) {
219     print $client "      $_ : $attrs{$_}\n";
220   }
221   print $client "    Multiple attributes can be separated by commas, like this:\n";
222   print $client "    finger tux/email,key\@db.debian.org\n";
223 }