2 # $Id: ud-fingerserv,v 1.4 1999/10/16 21:44:30 tausq Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
6 #use lib '/home/randolph/projects/userdir-ldap/web';
11 use POSIX qw(:sys_wait_h);
14 use Net::LDAP qw(:all);
17 my %config = &Util::ReadConfigFile;
19 getopt("iqh", \%opts);
20 my $use_inetd = $config{use_inetd} || $opts{i};
25 'mn' => 'Middle name',
27 'keyfingerprint' => 'Fingerprint',
29 'ircnick' => 'IRC nickname'
32 my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key');
34 $SIG{__DIE__} = \&DieHandler;
35 $SIG{INT} = \&DieHandler;
36 $SIG{CHLD} = \&Reaper;
38 &help if (defined($opts{h}));
39 #my $logfh = STDOUT; #TODO
41 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
42 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
45 if ($use_inetd == 0) {
46 &log("Binding to port 79") if (defined($opts{v}));
47 my $server = IO::Socket::INET->new(Proto => 'tcp',
48 LocalPort => 'finger(79)',
52 die "Cannot listen on finger port" unless $server;
53 &log("[Server listening for connections]");
55 my ($pid, $client, $hostinfo);
57 while ($client = $server->accept()) {
58 &log("Forking to handle client request") if (defined($opts{v}));
59 next if $pid = fork; # parent
60 die "fork: $!" unless defined $pid;
63 $client->autoflush(1);
64 my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
65 &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
66 my $query = <$client>;
67 &ProcessQuery($client, $query);
74 $opts{q} = 1; # Temp, until i figure out wth tcpd doesn't pass parameters to this program properly
76 my $sockaddr = getpeername(STDIN);
77 my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
78 &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
80 &ProcessQuery(\*STDOUT, $query);
87 $ldap->unbind if (defined($ldap));
92 1 until (-1 == waitpid(-1, WNOHANG));
93 $SIG{CHLD} = \&Reaper;
100 my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
102 $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
103 my ($uid, $fields) = split(/\//, $query, 2);
105 &log("Looking up $uid at $config{basedn}, uid=$uid");
107 $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
108 $mesg->code && die $mesg->error;
109 $entries = $mesg->as_struct;
111 foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
112 $data = $entries->{$dn};
115 foreach (@{$data->{keyfingerprint}}) {
116 push (@{$data->{key}}, "\n".&Util::FetchKey($_));
119 print $client "$dn\n";
121 foreach $key (@summarykeys) {
122 foreach (@{$data->{$key}}) {
123 print $client "$attrs{$key}: ";
124 print $client "$_\n";
129 foreach $key (split(/,/, $fields)) {
130 foreach (@{$data->{$key}}) {
131 print $client "$attrs{$key}: ";
132 print $client "$_\n";
140 print "fingerserv [-i | -q | -v | -h]\n";
141 print "-i = inetd mode; otherwise runs standalone\n";
142 print "-q = quiet mode; no output\n";
143 print "-v = verbose mode\n";
144 print "-h = this help message\n";
150 return if (defined($opts{q}));
152 my $time = localtime;
153 print STDERR "$time $msg\n";