2 # $Id: ud-fingerserv,v 1.19 2004/11/18 19:10:57 joey Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5 # (c) 2004 Martin Schulze. Licensed under the GPL. <joey@debian.org>
7 use lib '/var/www/userdir-ldap/';
8 #use lib '/home/randolph/projects/userdir-ldap/web';
12 use POSIX qw(:sys_wait_h);
15 use Net::LDAP qw(:all);
18 my %config = &Util::ReadConfigFile;
20 getopts("iqhv", \%opts);
21 my $use_inetd = $config{use_inetd} || $opts{i};
26 'mn' => 'Middle name',
29 'keyfingerprint' => 'Fingerprint',
31 'ircnick' => 'IRC nickname',
32 'icquin' => 'ICQ UIN',
33 'jabberjid' => 'Jabber ID',
37 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'jabberjid', 'keyfingerprint', 'key');
39 $SIG{__DIE__} = \&DieHandler;
40 $SIG{INT} = \&DieHandler;
41 $SIG{CHLD} = \&Reaper;
43 &help if (defined($opts{h}));
44 #my $logfh = STDOUT; #TODO
46 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
47 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
51 &log("Binding to port 79") if (defined($opts{v}));
52 my $server = IO::Socket::INET->new(Proto => 'tcp',
53 LocalPort => 'finger(79)',
57 die "Cannot listen on finger port" unless $server;
58 &log("[Server listening for connections]");
60 my ($pid, $client, $hostinfo);
62 while ($client = $server->accept()) {
63 &log("Forking to handle client request") if (defined($opts{v}));
64 next if $pid = fork; # parent
65 die "fork: $!" unless defined $pid;
68 $client->autoflush(1);
69 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
70 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
71 my $query = &readdata($client);
72 &ProcessQuery($client, $query) if (defined($query));
80 my $sockaddr = getpeername(STDIN);
82 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
83 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
85 &log("[Connect via terminal]");
87 my $query = &readdata(\*STDIN);
88 &ProcessQuery(\*STDOUT, $query) if (defined($query));
95 $ldap->unbind if (defined($ldap));
100 1 until (-1 == waitpid(-1, WNOHANG));
101 $SIG{CHLD} = \&Reaper;
108 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
110 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
111 my ($uid, $fields) = split(/\//, $query, 2);
113 if (($uid eq "") || ($uid =~ /^help$/i)) {
118 &log("Looking up $uid at $config{basedn}, uid=$uid");
120 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
121 $mesg->code && die $mesg->error;
122 $entries = $mesg->as_struct;
124 if ($mesg->count == 0) {
125 print $client "$uid not found at db.debian.org\n";
129 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
130 $data = $entries->{$dn};
132 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
133 $data->{mn}->[0], $data->{sn}->[0],
134 $data->{uid}->[0]."\@$config{emailappend}");
136 $data->{email}->[0] =~ s/\s+/ /g;
138 my @keyfingerprint = ();
139 for (my $i=0; $i <= $#{$data->{'keyfingerprint'}}; $i++) {
140 push (@keyfingerprint, $data->{keyfingerprint}->[$i]);
141 $data->{keyfingerprint}->[$i] = &Util::FormatFingerPrint($data->{keyfingerprint}->[$i]);
142 $data->{keyfingerprint}->[$i] =~ s, , ,;
144 print $client "$dn\n";
146 push (@{$data->{key}}, sprintf ("finger %s/key\@db.debian.org", $uid));
147 foreach $key (@summarykeys) {
148 foreach (@{$data->{$key}}) {
149 print $client "$attrs{$key}: ";
150 print $client "$_\n";
155 foreach $key (split(/,/, $fields)) {
157 foreach (@keyfingerprint) {
158 push (@{$data->{key}}, "\n".&Util::FetchKey($_), 0);
161 foreach (@{$data->{$key}}) {
162 print $client "$attrs{$key}: ";
163 print $client "$_\n";
171 print "fingerserv [-i | -q | -v | -h]\n";
172 print "-i = inetd mode; otherwise runs standalone\n";
173 print "-q = quiet mode; no output\n";
174 print "-v = verbose mode\n";
175 print "-h = this help message\n";
181 return if (defined($opts{q}));
183 my $time = localtime;
184 print STDERR "$time $msg\n";
194 my $flags= fcntl($fh, F_GETFL, 0)
195 or die "Can't get flags for socket: $!\n";
196 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
197 or die "Can't make socket nonblocking: $!\n";
199 while (($bytesread < 1024) && ($out !~ /\n/)) {
200 $ret = sysread($fh, $in, 1024);
201 return undef if (!defined($ret) || ($ret == 0));
213 print $client "userdir-ldap finger daemon\n";
214 print $client "--------------------------\n";
215 print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
216 print $client " where uid is the user id of the user\n";
217 print $client " the optional attributes parameter specifies what to return\n";
218 print $client " if nothing is specified, all attributes are returned.\n";
219 print $client " The following attributes are currently supported:\n";
220 foreach (@summarykeys) {
221 print $client " $_ : $attrs{$_}\n";
223 print $client " Multiple attributes can be separated by commas, like this:\n";
224 print $client " finger tux/email,key\@db.debian.org\n";