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