2 ###################################################################
3 # identd.perl5 : An implementation of RFC 1413 Ident Server
4 # using Vic Abell's lsof.
6 # - Started from inetd with 'nowait' option. This entry in
7 # /etc/inetd.conf will suffice :
9 # ident stream tcp nowait root /usr/local/bin/identd.perl5 -t200
11 # - Multiple instances of the server are not a performance penalty
12 # since they shall use lsof's cacheing mechanism. (compare with
13 # Peter Eriksson's pidentd)
14 # - assumes 'lsof' binary in /usr/local/sbin
15 # - Command line arguments :
16 # -t TIMEOUT Number of seconds to wait for a query before aborting.
19 # Kapil Chowksey <kchowksey@hss.hns.com>
20 ###################################################################
27 if (($LSOF = &isexec("../lsof")) eq "") { # Try .. first
28 if (($LSOF = &isexec("lsof")) eq "") { # Then try . and $PATH
29 print "can't execute $LSOF\n"; exit 1
33 # redirect lsof's warnings/errors to /dev/null
35 open(STDERR, ">/dev/null");
44 ($port, $iaddr) = sockaddr_in(getpeername(STDIN));
45 $peer_addr = inet_ntoa($iaddr);
47 # read ident-query from socket (STDIN) with a timeout.
48 $timeout = int($Timeout);
50 local $SIG{ALRM} = sub { die "alarm\n" };
55 die if $@ && $@ ne "alarm\n";
61 # remove all white-spaces from query
66 ($serv_port,$cli_port) = split(/,/,$query);
68 if ($serv_port =~ /^[0-9]+$/) {
69 if (int($serv_port) < 1 || int($serv_port) > 65535) {
70 print $query." : ERROR : INVALID-PORT"."\n";
74 print $query." : ERROR : INVALID-PORT"."\n";
78 if ($cli_port =~ /^[0-9]+$/) {
79 if (int($cli_port) < 1 || int($cli_port) > 65535) {
80 print $query." : ERROR : INVALID-PORT"."\n";
84 print $query." : ERROR : INVALID-PORT"."\n";
88 open(LSOFP,"$LSOF -nPDi -T -FLn -iTCP@".$peer_addr.":".$cli_port."|");
91 while ($a_line = <LSOFP>) {
93 if ($a_line =~ /^L.*/) {
94 ($user) = ($a_line =~ /^L(.*)/);
97 # make sure local port matches.
98 if ($a_line =~ /^n.*:\Q$serv_port->/) {
99 print $serv_port.", ".$cli_port." : USERID : UNIX :".$user."\n";
104 print $serv_port.", ".$cli_port." : ERROR : NO-USER"."\n";
107 ## isexec($path) -- is $path executable
109 # $path = absolute or relative path to file to test for executabiity.
110 # Paths that begin with neither '/' nor '.' that arent't found as
111 # simple references are also tested with the path prefixes of the
112 # PATH environment variable.
119 $path =~ s/^\s+|\s+$//g;
120 if ($path eq "") { return(""); }
121 if (($path =~ m#^[\/\.]#)) {
122 if (-x $path) { return($path); }
126 @P = split(":", $PATH);
127 for ($i = 0; $i <= $#P; $i++) {
128 if (-x "$P[$i]/$path") { return("$P[$i]/$path"); }