2 # $Id: ud-fingerserv,v 1.16 2004/10/24 18:34:12 joey Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
6 use lib '/var/www/userdir-ldap/';
7 #use lib '/home/randolph/projects/userdir-ldap/web';
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',
31 'icquin' => 'ICQ UIN',
35 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'keyfingerprint', 'key');
37 $SIG{__DIE__} = \&DieHandler;
38 $SIG{INT} = \&DieHandler;
39 $SIG{CHLD} = \&Reaper;
41 &help if (defined($opts{h}));
42 #my $logfh = STDOUT; #TODO
44 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
45 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
49 &log("Binding to port 79") if (defined($opts{v}));
50 my $server = IO::Socket::INET->new(Proto => 'tcp',
51 LocalPort => 'finger(79)',
55 die "Cannot listen on finger port" unless $server;
56 &log("[Server listening for connections]");
58 my ($pid, $client, $hostinfo);
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;
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));
78 my $sockaddr = getpeername(STDIN);
79 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
80 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
81 my $query = &readdata(\*STDIN);
82 &ProcessQuery(\*STDOUT, $query) if (defined($query));
89 $ldap->unbind if (defined($ldap));
94 1 until (-1 == waitpid(-1, WNOHANG));
95 $SIG{CHLD} = \&Reaper;
102 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
104 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
105 my ($uid, $fields) = split(/\//, $query, 2);
107 if (($uid eq "") || ($uid =~ /^help$/i)) {
112 &log("Looking up $uid at $config{basedn}, uid=$uid");
114 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
115 $mesg->code && die $mesg->error;
116 $entries = $mesg->as_struct;
118 if ($mesg->count == 0) {
119 print $client "$uid not found at db.debian.org\n";
123 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
124 $data = $entries->{$dn};
127 foreach (@{$data->{keyfingerprint}}) {
128 push (@{$data->{key}}, "\n".&Util::FetchKey($_));
131 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
132 $data->{mn}->[0], $data->{sn}->[0],
133 $data->{uid}->[0]."\@$config{emailappend}");
135 $data->{email}->[0] =~ s/\s+/ /g;
137 for (my $i=0; $i <= $#{$data->{'keyfingerprint'}}; $i++) {
138 $data->{keyfingerprint}->[$i] = &Util::FormatFingerPrint($data->{keyfingerprint}->[$i]);
139 $data->{keyfingerprint}->[$i] =~ s, , ,;
141 print $client "$dn\n";
143 foreach $key (@summarykeys) {
144 foreach (@{$data->{$key}}) {
145 print $client "$attrs{$key}: ";
146 print $client "$_\n";
151 foreach $key (split(/,/, $fields)) {
152 foreach (@{$data->{$key}}) {
153 print $client "$attrs{$key}: ";
154 print $client "$_\n";
162 print "fingerserv [-i | -q | -v | -h]\n";
163 print "-i = inetd mode; otherwise runs standalone\n";
164 print "-q = quiet mode; no output\n";
165 print "-v = verbose mode\n";
166 print "-h = this help message\n";
172 return if (defined($opts{q}));
174 my $time = localtime;
175 print STDERR "$time $msg\n";
185 my $flags= fcntl($fh, F_GETFL, 0)
186 or die "Can't get flags for socket: $!\n";
187 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
188 or die "Can't make socket nonblocking: $!\n";
190 while (($bytesread < 1024) && ($out !~ /\n/)) {
191 $ret = sysread($fh, $in, 1024);
192 return undef if (!defined($ret) || ($ret == 0));
204 print $client "userdir-ldap finger daemon\n";
205 print $client "--------------------------\n";
206 print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
207 print $client " where uid is the user id of the user\n";
208 print $client " the optional attributes parameter specifies what to return\n";
209 print $client " if nothing is specified, all attributes are returned.\n";
210 print $client " The following attributes are currently supported:\n";
211 foreach (@summarykeys) {
212 print $client " $_ : $attrs{$_}\n";
214 print $client " Multiple attributes can be separated by commas, like this:\n";
215 print $client " finger tux/email,key\@db.debian.org\n";