2 # $Id: ud-fingerserv,v 1.9 1999/10/17 02:19:07 tausq Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
6 #use lib '/var/www/userdir-ldap/';
7 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'
34 my @summarykeys = ('cn', 'mn', 'sn', 'email', 'ircnick', 'keyfingerprint', 'key');
36 $SIG{__DIE__} = \&DieHandler;
37 $SIG{INT} = \&DieHandler;
38 $SIG{CHLD} = \&Reaper;
40 &help if (defined($opts{h}));
41 #my $logfh = STDOUT; #TODO
43 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
44 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
48 &log("Binding to port 79") if (defined($opts{v}));
49 my $server = IO::Socket::INET->new(Proto => 'tcp',
50 LocalPort => 'finger(79)',
54 die "Cannot listen on finger port" unless $server;
55 &log("[Server listening for connections]");
57 my ($pid, $client, $hostinfo);
59 while ($client = $server->accept()) {
60 &log("Forking to handle client request") if (defined($opts{v}));
61 next if $pid = fork; # parent
62 die "fork: $!" unless defined $pid;
65 $client->autoflush(1);
66 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
67 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
68 my $query = <$client>;
69 &ProcessQuery($client, $query);
77 my $sockaddr = getpeername(STDIN);
78 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
79 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
81 &ProcessQuery(\*STDOUT, $query);
88 $ldap->unbind if (defined($ldap));
93 1 until (-1 == waitpid(-1, WNOHANG));
94 $SIG{CHLD} = \&Reaper;
101 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
103 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
104 my ($uid, $fields) = split(/\//, $query, 2);
106 if ($uid =~ /^help$/i) {
111 &log("Looking up $uid at $config{basedn}, uid=$uid");
113 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
114 $mesg->code && die $mesg->error;
115 $entries = $mesg->as_struct;
117 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
118 $data = $entries->{$dn};
121 foreach (@{$data->{keyfingerprint}}) {
122 push (@{$data->{key}}, "\n".&Util::FetchKey($_));
125 $data->{email}->[0] = sprintf("%s %s %s <%s>", $data->{cn}->[0],
126 $data->{mn}->[0], $data->{sn}->[0],
127 $data->{uid}->[0]."\@$config{emailappend}");
129 $data->{email}->[0] =~ s/\s+/ /g;
131 print $client "$dn\n";
133 foreach $key (@summarykeys) {
134 foreach (@{$data->{$key}}) {
135 print $client "$attrs{$key}: ";
136 print $client "$_\n";
141 foreach $key (split(/,/, $fields)) {
142 foreach (@{$data->{$key}}) {
143 print $client "$attrs{$key}: ";
144 print $client "$_\n";
152 print "fingerserv [-i | -q | -v | -h]\n";
153 print "-i = inetd mode; otherwise runs standalone\n";
154 print "-q = quiet mode; no output\n";
155 print "-v = verbose mode\n";
156 print "-h = this help message\n";
162 return if (defined($opts{q}));
164 my $time = localtime;
165 print STDERR "$time $msg\n";
171 print $client "userdir-ldap finger daemon\n";
172 print $client "--------------------------\n";
173 print $client "finger <uid>[/<attributes>]\@db.debian.org\n";
174 print $client " where uid is the user id of the user\n";
175 print $client " the optional attributes parameter specifies what to return\n";
176 print $client " if nothing is specified, all attributes are returned.\n";
177 print $client " The following attributes are current supported:\n";
178 foreach (@summarykeys) {
179 print $client " $_ : $attrs{$_}\n";
181 print $client " Multiple attributes can be separated by commas, like this:\n";
182 print $client " finger tux/email,key\@db.debian.org\n";