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 POSIX qw(:sys_wait_h);
19 my %config = &Util::ReadConfigFile;
21 getopts("fiqhvl:", \%opts);
22 my $use_inetd = $config{use_inetd} || $opts{i};
27 'mn' => 'Middle name',
30 'keyfingerprint' => 'Fingerprint',
32 'ircnick' => 'IRC nickname',
33 'icquin' => 'ICQ UIN',
34 'jabberjid' => 'Jabber ID',
38 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'jabberjid', 'keyfingerprint', 'key');
40 $SIG{__DIE__} = \&DieHandler;
41 $SIG{INT} = \&DieHandler;
42 $SIG{CHLD} = \&Reaper;
44 &help if (defined($opts{h}));
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";
54 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
55 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
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: $!";
66 defined($pid = fork) or die "Can't fork: $!";
68 setsid or die "Can't start a new session: $!";
69 defined($pid = fork) or die "Can't fork: $!";
71 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
74 &log("Binding to port 79") if (defined($opts{v}));
75 my $server = IO::Socket::INET->new(Proto => 'tcp',
76 LocalPort => 'finger(79)',
80 mydie("Cannot listen on finger port") unless $server;
81 &log("[Server listening for connections]");
83 my ($pid, $client, $hostinfo);
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;
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));
103 my $sockaddr = getpeername(STDIN);
105 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
106 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
108 &log("[Connect via terminal]");
110 my $query = &readdata(\*STDIN);
111 &ProcessQuery(\*STDOUT, $query) if (defined($query));
118 $ldap->unbind if (defined($ldap));
123 1 until (-1 == waitpid(-1, WNOHANG));
124 $SIG{CHLD} = \&Reaper;
131 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
133 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
134 my ($uid, $fields) = split(/\//, $query, 2);
136 if (($uid eq "") || ($uid =~ /^help$/i)) {
141 &log("Looking up $uid at $config{basedn}, uid=$uid");
143 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
144 $mesg->code && mydie $mesg->error;
145 $entries = $mesg->as_struct;
147 if ($mesg->count == 0) {
148 print $client "$uid not found at db.debian.org\n";
152 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
153 $data = $entries->{$dn};
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}");
159 $data->{email}->[0] =~ s/\s+/ /g;
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, , ,;
167 print $client "$dn\n";
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";
178 foreach $key (split(/,/, $fields)) {
180 foreach (@keyfingerprint) {
181 push (@{$data->{key}}, "\n".&Util::FetchKey($_), 0);
184 foreach (@{$data->{$key}}) {
185 print $client "$attrs{$key}: ";
186 print $client "$_\n";
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";
206 return if (defined($opts{q}));
208 my $time = localtime;
209 print $logfh "$time $msg\n";
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";
230 while (($bytesread < 1024) && ($out !~ /\n/)) {
231 $ret = sysread($fh, $in, 1024);
232 return undef if (!defined($ret) || ($ret == 0));
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";
254 print $client " Multiple attributes can be separated by commas, like this:\n";
255 print $client " finger tux/email,key\@db.debian.org\n";