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>
6 # Copyright (c) 2008 Peter Palfrader <peter@palfrader.org>
8 use lib '/var/www/userdir-ldap/';
9 #use lib '/home/randolph/projects/userdir-ldap/web';
13 use Socket qw(:addrinfo);
14 use POSIX qw(:sys_wait_h);
20 my %config = &Util::ReadConfigFile;
22 getopts("fiqhvl:", \%opts);
23 my $use_inetd = $config{use_inetd} || $opts{i};
28 'mn' => 'Middle name',
31 'keyfingerprint' => 'Fingerprint',
33 'ircnick' => 'IRC nickname',
34 'icquin' => 'ICQ UIN',
35 'jabberjid' => 'Jabber ID',
39 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'jabberjid', 'keyfingerprint', 'key');
41 $SIG{__DIE__} = \&DieHandler;
42 $SIG{INT} = \&DieHandler;
43 $SIG{CHLD} = \&Reaper;
45 &help if (defined($opts{h}));
48 unless ($opts{i} || $opts{f}) {
49 die "Need logfile unless running foreground\n" unless (defined($opts{l}));
50 open ($logfh, $opts{l}) or die "Can't open logfile: $!\n";
55 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
56 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
63 chdir '/' or die "Can't chdir to /: $!";
64 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
65 open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
67 defined($pid = fork) or die "Can't fork: $!";
69 setsid or die "Can't start a new session: $!";
70 defined($pid = fork) or die "Can't fork: $!";
72 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
75 &log("Binding to port 79") if (defined($opts{v}));
76 my $server = IO::Socket::INET->new(Proto => 'tcp',
77 LocalPort => 'finger(79)',
81 mydie("Cannot listen on finger port") unless $server;
82 &log("[Server listening for connections]");
84 my ($pid, $client, $hostinfo);
86 while ($client = $server->accept()) {
87 &log("Forking to handle client request") if (defined($opts{v}));
88 next if $pid = fork; # parent
89 mydie("fork: $!") unless defined $pid;
92 $client->autoflush(1);
93 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
94 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
95 my $query = &readdata($client);
96 &ProcessQuery($client, $query) if (defined($query));
104 my $sockaddr = getpeername(STDIN);
106 my ($err, $hostname, $servicename) = getnameinfo($sockaddr, NI_NUMERICHOST|NI_NUMERICSERV);
107 &log(sprintf("[Connect from %s:%s]", $hostname, $servicename));
109 &log("[Connect via terminal]");
111 my $query = &readdata(\*STDIN);
112 &ProcessQuery(\*STDOUT, $query) if (defined($query));
119 $ldap->unbind if (defined($ldap));
124 1 until (-1 == waitpid(-1, WNOHANG));
125 $SIG{CHLD} = \&Reaper;
132 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
134 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
135 my ($uid, $fields) = split(/\//, $query, 2);
137 if (($uid eq "") || ($uid =~ /^help$/i)) {
142 &log("Looking up $uid at $config{basedn}, uid=$uid");
144 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
145 $mesg->code && mydie $mesg->error;
146 $entries = $mesg->as_struct;
148 if ($mesg->count == 0) {
149 print $client "$uid not found at db.debian.org\n";
153 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
154 $data = $entries->{$dn};
156 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
157 $data->{mn}->[0], $data->{sn}->[0],
158 $data->{uid}->[0]."\@$config{emailappend}");
160 $data->{email}->[0] =~ s/\s+/ /g;
162 my @keyfingerprint = ();
163 for (my $i=0; $i <= $#{$data->{'keyfingerprint'}}; $i++) {
164 push (@keyfingerprint, $data->{keyfingerprint}->[$i]);
165 $data->{keyfingerprint}->[$i] = &Util::FormatFingerPrint($data->{keyfingerprint}->[$i]);
166 $data->{keyfingerprint}->[$i] =~ s, , ,;
168 print $client "$dn\n";
170 push (@{$data->{key}}, sprintf ("finger %s/key\@db.debian.org", $uid));
171 foreach $key (@summarykeys) {
172 foreach (@{$data->{$key}}) {
173 print $client "$attrs{$key}: ";
174 print $client "$_\n";
179 foreach $key (split(/,/, $fields)) {
181 foreach (@keyfingerprint) {
182 push (@{$data->{key}}, "\n".&Util::FetchKey($_), 0);
185 foreach (@{$data->{$key}}) {
186 print $client "$attrs{$key}: ";
187 print $client "$_\n";
195 print "fingerserv [-f | -l | -i | -q | -v | -h]\n";
196 print "-f = foreground; do not detach from tty\n";
197 print "-i = inetd mode; otherwise runs standalone\n";
198 print "-q = quiet mode; no output\n";
199 print "-v = verbose mode\n";
200 print "-h = this help message\n";
201 print "-l = log file. Necessary if not using -f or -i\n";
207 return if (defined($opts{q}));
209 my $time = localtime;
210 print $logfh "$time $msg\n";
226 my $flags= fcntl($fh, F_GETFL, 0)
227 or mydie "Can't get flags for socket: $!\n";
228 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
229 or mydie "Can't make socket nonblocking: $!\n";
231 while (($bytesread < 1024) && ($out !~ /\n/)) {
232 $ret = sysread($fh, $in, 1024);
233 return undef if (!defined($ret) || ($ret == 0));
245 print $client "userdir-ldap finger daemon\n";
246 print $client "--------------------------\n";
247 print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
248 print $client " where uid is the user id of the user\n";
249 print $client " the optional attributes parameter specifies what to return\n";
250 print $client " if nothing is specified, all attributes are returned.\n";
251 print $client " The following attributes are currently supported:\n";
252 foreach (@summarykeys) {
253 print $client " $_ : $attrs{$_}\n";
255 print $client " Multiple attributes can be separated by commas, like this:\n";
256 print $client " finger tux/email,key\@db.debian.org\n";