added inetd handling code; still needs work
authortausq <>
Sat, 16 Oct 1999 19:44:30 +0000 (19:44 +0000)
committertausq <>
Sat, 16 Oct 1999 19:44:30 +0000 (19:44 +0000)
ud-fingerserv
userdir-ldap.conf

index 67fc24f..7060fa4 100755 (executable)
@@ -1,18 +1,24 @@
 #!/usr/bin/perl
-# $Id: ud-fingerserv,v 1.3 1999/10/14 04:28:59 tausq Exp $
+# $Id: ud-fingerserv,v 1.4 1999/10/16 21:44:30 tausq Exp $
 
 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
 
-use lib 'web';
+#use lib '/home/randolph/projects/userdir-ldap/web';
 use strict vars;
 #use Apache::Registry;
+use IO::Handle;
 use IO::Socket;
 use POSIX qw(:sys_wait_h);
+use Getopt::Std;
 use Util;
 use Net::LDAP qw(:all);
 
 # Global settings...
 my %config = &Util::ReadConfigFile;
+my %opts;
+getopt("iqh", \%opts);
+my $use_inetd = $config{use_inetd} || $opts{i}; 
+$| = 1;
 
 my %attrs = (
   'cn' => 'First name',
@@ -25,9 +31,61 @@ my %attrs = (
 
 my @summarykeys = ('cn', 'mn', 'sn', 'ircnick', 'keyfingerprint', 'key');
 
-my ($ldap, $mesg, $dn, $entries, $data, %output, $key, $hostlist, $hostdetails, $selected, %summary);
+$SIG{__DIE__} = \&DieHandler;
+$SIG{INT} = \&DieHandler;
+$SIG{CHLD} = \&Reaper;
+
+&help if (defined($opts{h}));
+#my $logfh = STDOUT; #TODO
+
+&log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
+my $ldap = Net::LDAP->new($config{ldaphost}) || die $1; 
+$ldap->bind;
+
+if ($use_inetd == 0) {
+  &log("Binding to port 79") if (defined($opts{v}));
+  my $server = IO::Socket::INET->new(Proto => 'tcp', 
+                                     LocalPort => 'finger(79)',
+                                    Listen => SOMAXCONN,
+                                    Reuse => 1);
+
+  die "Cannot listen on finger port" unless $server;
+  &log("[Server listening for connections]");
+
+  my ($pid, $client, $hostinfo);
+
+  while ($client = $server->accept()) {
+    &log("Forking to handle client request") if (defined($opts{v}));
+    next if $pid = fork; # parent
+    die "fork: $!" unless defined $pid;
+  
+    # child
+    $client->autoflush(1);
+    my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
+    &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost));
+    my $query = <$client>;
+    &ProcessQuery($client, $query);
+    $client->close;
+    exit;
+  } continue {
+    $client->close;
+  }
+} else { # inetd
+  $opts{q} = 1; # Temp, until i figure out wth tcpd doesn't pass parameters to this program properly
+  &log("inetd mode");
+  my $sockaddr = getpeername(STDIN);
+  my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
+  &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
+  my $query = <STDIN>;
+  &ProcessQuery(\*STDOUT, $query);
+  exit;
+}
+
+$ldap->unbind;
+
 sub DieHandler {
   $ldap->unbind if (defined($ldap));
+  exit 0;
 }
 
 sub Reaper {
@@ -35,42 +93,21 @@ sub Reaper {
   $SIG{CHLD} = \&Reaper;
 }
 
-$SIG{__DIE__} = \&DieHandler;
-$SIG{CHLD} = \&Reaper;
-
-$ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
-$mesg;
-$ldap->bind;
-
-my $server = IO::Socket::INET->new(Proto => 'tcp', 
-                                   LocalPort => 'finger(79)',
-                                  Listen => SOMAXCONN,
-                                  Reuse => 1);
-
-die "Cannot listen on finger port" unless $server;
-print "[Server listening for connections]\n";
-
-my ($mesg, %entries, $dn, $key, $pid);
-
-my $client;
-while ($client = $server->accept()) {
-  next if $pid = fork; # parent
-  die "fork: $!" unless defined $pid;
+sub ProcessQuery {
+  my $client = shift;
+  my $query = shift;
   
-  # child
-  $client->autoflush(1);
-  my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET);
-  printf "[Connect from %s]\n", $hostinfo || $client->peerhost;
-  my $query = <$client>;
+  my ($uid, $fields, $mesg, $entries, $dn, $key, $pid, $data);
+
   $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input
   my ($uid, $fields) = split(/\//, $query, 2);
   
-#  print "Looking up $uid at $config{basedn}, uid=$uid\n";
+  &log("Looking up $uid at $config{basedn}, uid=$uid");
 
   $mesg = $ldap->search(base  => $config{basedn}, filter => "uid=$uid");
   $mesg->code && die $mesg->error;
   $entries = $mesg->as_struct;
-
+  
   foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) {
     $data = $entries->{$dn};
 
@@ -78,7 +115,7 @@ while ($client = $server->accept()) {
     foreach (@{$data->{keyfingerprint}}) {
       push (@{$data->{key}}, "\n".&Util::FetchKey($_));
     }
-
     print $client "$dn\n";
     if (!$fields) {
       foreach $key (@summarykeys) {
@@ -88,7 +125,7 @@ while ($client = $server->accept()) {
         }
       }
     } else {
-     print "$fields\n";
+  #     print "$fields\n";
       foreach $key (split(/,/, $fields)) {
         foreach (@{$data->{$key}}) {
           print $client "$attrs{$key}: ";
@@ -97,10 +134,21 @@ while ($client = $server->accept()) {
       }
     }
   }
-  $client->close;
-  exit;
-} continue {
-  $client->close;
+}  
+
+sub help {
+  print "fingerserv [-i | -q | -v | -h]\n";
+  print "-i = inetd mode; otherwise runs standalone\n";
+  print "-q = quiet mode; no output\n";
+  print "-v = verbose mode\n";
+  print "-h = this help message\n";
+  exit 0;
 }
 
-$ldap->unbind;
+sub log {
+  my $msg = shift;
+  return if (defined($opts{q}));
+  
+  my $time = localtime;
+  print STDERR "$time $msg\n";
+}
index 8b1cc52..8ef572d 100644 (file)
@@ -52,3 +52,6 @@ blowfishkeylen = 10;
 # Change this!
 authtokenpath = "/var/cache/userdir-ldap/web-cookies";
 countrylist = "/var/www/userdir-ldap/domains.tab";
+
+# Finger daemon settings
+use_inetd = 1;