From a53cd78ee9f122ade507aaeb4efc027b055b22f2 Mon Sep 17 00:00:00 2001 From: tausq <> Date: Wed, 20 Oct 1999 02:33:43 +0000 Subject: [PATCH] non-block reads, limits input to 1k. fixed typo in help output shows "not found" message if user is not found makes finger @ show help text --- ud-fingerserv | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/ud-fingerserv b/ud-fingerserv index 07466de..67fa6a3 100755 --- a/ud-fingerserv +++ b/ud-fingerserv @@ -1,10 +1,10 @@ #!/usr/bin/perl -# $Id: ud-fingerserv,v 1.10 1999/10/17 02:21:48 tausq Exp $ +# $Id: ud-fingerserv,v 1.11 1999/10/20 04:33:43 tausq Exp $ # (c) 1999 Randolph Chung. Licensed under the GPL. -use lib '/var/www/userdir-ldap/'; -#use lib '/home/randolph/projects/userdir-ldap/web'; +#use lib '/var/www/userdir-ldap/'; +use lib '/home/randolph/projects/userdir-ldap/web'; use strict vars; #use Apache::Registry; use IO::Handle; @@ -65,7 +65,7 @@ if (!$use_inetd) { $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr, AF_INET); &log(sprintf("[Connect from %s]", $hostinfo || $client->peerhost)); - my $query = <$client>; + my $query = &readdata($client); &ProcessQuery($client, $query); $client->close; exit; @@ -77,7 +77,7 @@ if (!$use_inetd) { 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 = ; + my $query = &readdata(\*STDIN); &ProcessQuery(\*STDOUT, $query); exit; } @@ -103,7 +103,7 @@ sub ProcessQuery { $query =~ s/[^\/,0-9a-z]//gi; # be paranoid about input my ($uid, $fields) = split(/\//, $query, 2); - if ($uid =~ /^help$/i) { + if (($uid eq "") || ($uid =~ /^help$/i)) { &sendhelp($client); return; } @@ -113,7 +113,12 @@ sub ProcessQuery { $mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid"); $mesg->code && die $mesg->error; $entries = $mesg->as_struct; - + + if ($mesg->count == 0) { + print $client "$uid not found at db.debian.org\n"; + exit 0; + } + foreach $dn (sort {$entries->{$a}->{sn}->[0] <=> $entries->{$b}->{sn}->[0]} keys(%$entries)) { $data = $entries->{$dn}; @@ -165,6 +170,26 @@ sub log { print STDERR "$time $msg\n"; } +sub readdata { + my $fh = shift; + my $in = undef; + my $out = undef; + my $bytesread = 0; + + my $flags= fcntl($fh, F_GETFL, 0) + or die "Can't get flags for socket: $!\n"; + fcntl($fh, F_SETFL, $flags | O_NONBLOCK) + or die "Can't make socket nonblocking: $!\n"; + + while (($bytesread < 1024) && ($out !~ /\n/)) { + $bytesread += sysread($fh, $in, 1024); + $out .= $in; + } + + $out =~ /(.*?)\n/; + return $1; +} + sub sendhelp { my $client = shift; @@ -174,7 +199,7 @@ sub sendhelp { print $client " where uid is the user id of the user\n"; print $client " the optional attributes parameter specifies what to return\n"; print $client " if nothing is specified, all attributes are returned.\n"; - print $client " The following attributes are current supported:\n"; + print $client " The following attributes are currently supported:\n"; foreach (@summarykeys) { print $client " $_ : $attrs{$_}\n"; } -- 2.20.1