+my @summarykeys = ('cn', 'mn', 'sn', 'email', 'labeleduri', 'ircnick', 'icquin', 'jabberjid', 'keyfingerprint', 'key');
+
+$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) {
+ &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 = &readdata($client);
+ &ProcessQuery($client, $query) if (defined($query));
+ $client->close;
+ exit;
+ } continue {
+ $client->close;
+ }
+} else { # inetd
+ &log("inetd mode");
+ my $sockaddr = getpeername(STDIN);
+ if ($sockaddr) {
+ my ($port, $addr) = unpack_sockaddr_in(getpeername(STDIN));
+ &log(sprintf("[Connect from %s (%s)]", gethostbyaddr($addr, AF_INET), inet_ntoa($addr)));
+ } else {
+ &log("[Connect via terminal]");
+ }
+ my $query = &readdata(\*STDIN);
+ &ProcessQuery(\*STDOUT, $query) if (defined($query));
+ exit;
+}
+
+$ldap->unbind;