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("fiqhvl:", \%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}));
46 unless ($opt{i} || $opt{f}) {
47 die "Need logfile unless running foreground\n" unless (defined($opt{l}));
48 open ($logfh, $opt{l}) or die "Can't open logfile: $!\n";
53 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
54 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
61 chdir '/' or die "Can't chdir to /: $!";
62 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
63 open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
64 defined(my $pid = fork) or die "Can't fork: $!";
66 setsid or die "Can't start a new session: $!";
67 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
70 &log("Binding to port 79") if (defined($opts{v}));
71 my $server = IO::Socket::INET->new(Proto => 'tcp',
72 LocalPort => 'finger(79)',
76 mydie "Cannot listen on finger port" unless $server;
77 &log("[Server listening for connections]");
79 my ($pid, $client, $hostinfo);
81 while ($client = $server->accept()) {
82 &log("Forking to handle client request") if (defined($opts{v}));
83 next if $pid = fork; # parent
84 mydie "fork: $!" unless defined $pid;
87 $client->autoflush(1);
88 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
89 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
90 my $query = &readdata($client);
91 &ProcessQuery($client, $query) if (defined($query));
99 my $sockaddr = getpeername(STDIN);
101 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
102 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
104 &log("[Connect via terminal]");
106 my $query = &readdata(\*STDIN);
107 &ProcessQuery(\*STDOUT, $query) if (defined($query));
114 $ldap->unbind if (defined($ldap));
119 1 until (-1 == waitpid(-1, WNOHANG));
120 $SIG{CHLD} = \&Reaper;
127 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
129 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
130 my ($uid, $fields) = split(/\//, $query, 2);
132 if (($uid eq "") || ($uid =~ /^help$/i)) {
137 &log("Looking up $uid at $config{basedn}, uid=$uid");
139 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
140 $mesg->code && mydie $mesg->error;
141 $entries = $mesg->as_struct;
143 if ($mesg->count == 0) {
144 print $client "$uid not found at db.debian.org\n";
148 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
149 $data = $entries->{$dn};
151 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
152 $data->{mn}->[0], $data->{sn}->[0],
153 $data->{uid}->[0]."\@$config{emailappend}");
155 $data->{email}->[0] =~ s/\s+/ /g;
157 my @keyfingerprint = ();
158 for (my $i=0; $i <= $#{$data->{'keyfingerprint'}}; $i++) {
159 push (@keyfingerprint, $data->{keyfingerprint}->[$i]);
160 $data->{keyfingerprint}->[$i] = &Util::FormatFingerPrint($data->{keyfingerprint}->[$i]);
161 $data->{keyfingerprint}->[$i] =~ s, , ,;
163 print $client "$dn\n";
165 push (@{$data->{key}}, sprintf ("finger %s/key\@db.debian.org", $uid));
166 foreach $key (@summarykeys) {
167 foreach (@{$data->{$key}}) {
168 print $client "$attrs{$key}: ";
169 print $client "$_\n";
174 foreach $key (split(/,/, $fields)) {
176 foreach (@keyfingerprint) {
177 push (@{$data->{key}}, "\n".&Util::FetchKey($_), 0);
180 foreach (@{$data->{$key}}) {
181 print $client "$attrs{$key}: ";
182 print $client "$_\n";
190 print "fingerserv [-f | -l | -i | -q | -v | -h]\n";
191 print "-f = foreground; do not detach from tty\n";
192 print "-i = inetd mode; otherwise runs standalone\n";
193 print "-q = quiet mode; no output\n";
194 print "-v = verbose mode\n";
195 print "-h = this help message\n";
196 print "-l = log file. Necessary if not using -f or -i\n";
202 return if (defined($opts{q}));
204 my $time = localtime;
205 print $logfh "$time $msg\n";
221 my $flags= fcntl($fh, F_GETFL, 0)
222 or mydie "Can't get flags for socket: $!\n";
223 fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
224 or mydie "Can't make socket nonblocking: $!\n";
226 while (($bytesread < 1024) && ($out !~ /\n/)) {
227 $ret = sysread($fh, $in, 1024);
228 return undef if (!defined($ret) || ($ret == 0));
240 print $client "userdir-ldap finger daemon\n";
241 print $client "--------------------------\n";
242 print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
243 print $client " where uid is the user id of the user\n";
244 print $client " the optional attributes parameter specifies what to return\n";
245 print $client " if nothing is specified, all attributes are returned.\n";
246 print $client " The following attributes are currently supported:\n";
247 foreach (@summarykeys) {
248 print $client " $_ : $attrs{$_}\n";
250 print $client " Multiple attributes can be separated by commas, like this:\n";
251 print $client " finger tux/email,key\@db.debian.org\n";