added inetd handling code; still needs work
[mirror/userdir-ldap.git] / ud-fingerserv
1 #!/usr/bin/perl
2 # $Id: ud-fingerserv,v 1.4 1999/10/16 21:44:30 tausq Exp $
3
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 #use lib '/home/randolph/projects/userdir-ldap/web';
7 use strict vars;
8 #use Apache::Registry;
9 use IO::Handle;
10 use IO::Socket;
11 use POSIX qw(:sys_wait_h);
12 use Getopt::Std;
13 use Util;
14 use Net::LDAP qw(:all);
15
16 # Global settings...
17 my %config = &Util::ReadConfigFile;
18 my %opts;
19 getopt("iqh", \%opts);
20 my $use_inetd = $config{use_inetd} || $opts{i}; 
21 $| = 1;
22
23 my %attrs = (
24   'cn' => 'First name',
25   'mn' => 'Middle name',
26   'sn' => 'Last name',
27   'keyfingerprint' => 'Fingerprint',
28   'key' => 'Key block',
29   'ircnick' => 'IRC nickname'
30 );
31
32 my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key');
33
34 $SIG{__DIE__} = \&DieHandler;
35 $SIG{INT} = \&DieHandler;
36 $SIG{CHLD} = \&Reaper;
37
38 &help if (defined($opts{h}));
39 #my $logfh = STDOUT; #TODO
40
41 &log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
42 my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; 
43 $ldap->bind;
44
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)',
49                                      Listen => SOMAXCONN,
50                                      Reuse => 1);
51
52   die "Cannot listen on finger port" unless $server;
53   &log("[Server listening for connections]");
54
55   my ($pid, $client, $hostinfo);
56
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;
61   
62     # child
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);
68     $client->close;
69     exit;
70   } continue {
71     $client->close;
72   }
73 } else { # inetd
74   $opts{q} = 1; # Temp, until i figure out wth tcpd doesn't pass parameters to this program properly
75   &log("inetd mode");
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)));
79   my $query = <STDIN>;
80   &ProcessQuery(\*STDOUT, $query);
81   exit;
82 }
83
84 $ldap->unbind;
85
86 sub DieHandler {
87   $ldap->unbind if (defined($ldap));
88   exit 0;
89 }
90
91 sub Reaper {
92   1 until (-1 == waitpid(-1, WNOHANG));
93   $SIG{CHLD} = \&Reaper;
94 }
95
96 sub ProcessQuery {
97   my $client = shift;
98   my $query = shift;
99   
100   my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
101
102   $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
103   my ($uid, $fields) = split(/\//, $query, 2);
104   
105   &log("Looking up $uid at $config{basedn}, uid=$uid");
106
107   $mesg = $ldap->search(base  => $config{basedn}, filter => "uid=$uid");
108   $mesg->code && die $mesg->error;
109   $entries = $mesg->as_struct;
110   
111   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
112     $data = $entries->{$dn};
113
114     $data->{key} = [];    
115     foreach (@{$data->{keyfingerprint}}) {
116       push (@{$data->{key}}, "\n".&Util::FetchKey($_));
117     }
118  
119     print $client "$dn\n";
120     if (!$fields) {
121       foreach $key (@summarykeys) {
122         foreach (@{$data->{$key}}) {
123           print $client "$attrs{$key}: ";
124           print $client "$_\n";
125         }
126       }
127     } else {
128   #     print "$fields\n";
129       foreach $key (split(/,/, $fields)) {
130         foreach (@{$data->{$key}}) {
131           print $client "$attrs{$key}: ";
132           print $client "$_\n";
133         }
134       }
135     }
136   }
137 }  
138
139 sub help {
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";
145   exit 0;
146 }
147
148 sub log {
149   my $msg = shift;
150   return if (defined($opts{q}));
151   
152   my $time = localtime;
153   print STDERR "$time $msg\n";
154 }