File: identd.perl5

package info (click to toggle)
lsof 4.91+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,984 kB
  • sloc: ansic: 68,893; sh: 6,862; makefile: 1,682; perl: 1,099; awk: 214
file content (130 lines) | stat: -rwxr-xr-x 3,304 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl
###################################################################
# identd.perl5 : An implementation of RFC 1413 Ident Server
#                using Vic Abell's lsof.
#
# - Started from inetd with 'nowait' option. This entry in 
#   /etc/inetd.conf will suffice :
#
#   ident   stream  tcp     nowait  root    /usr/local/bin/identd.perl5 -t200
#
# - Multiple instances of the server are not a performance penalty
#   since they shall use lsof's cacheing mechanism. (compare with
#   Peter Eriksson's pidentd)
# - assumes 'lsof' binary in $PATH
# - Command line arguments :
#   -t TIMEOUT Number of seconds to wait for a query before aborting.
#              Default is 120.
#
# Kapil Chowksey <kchowksey@hss.hns.com>
###################################################################
use strict;
use Socket;
use Getopt::Long;

# Set path to lsof.

my $LSOF;
if (($LSOF = &isexec("/usr/bin/lsof")) eq "") {	# Try .. first
    if (($LSOF = &isexec("lsof")) eq "") {	# Then try . and $PATH
	print "can't execute $LSOF\n"; exit 1
    }
}

# redirect lsof's warnings/errors to /dev/null
close(STDERR);
open(STDERR, ">/dev/null");

my $Timeout = "120";

GetOptions('timeout=i'=>\$Timeout);

my ($port, $iaddr) = sockaddr_in(getpeername(STDIN));
my $peer_addr = inet_ntoa($iaddr);
my $query;

# read ident-query from socket (STDIN) with a timeout.
my $timeout = int($Timeout);
eval {
    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm $timeout;
    $query = <STDIN>;
    alarm 0;
};
die if $@ && $@ ne "alarm\n";
if ($@) {
    # timed out
    exit;
}

# remove all white-spaces from query
$query =~ s/\s//g;

my $serv_port = "";
my $cli_port = "";
($serv_port,$cli_port) = split(/,/,$query);

if ($serv_port =~ /^[0-9]+$/) {
    if (int($serv_port) < 1 || int($serv_port) > 65535) {
        print $query." : ERROR : INVALID-PORT"."\n";
        exit;
    }
} else {
    print $query." : ERROR : INVALID-PORT"."\n";
    exit;
}

if ($cli_port =~ /^[0-9]+$/) {
    if (int($cli_port) < 1 || int($cli_port) > 65535) {
        print $query." : ERROR : INVALID-PORT"."\n";
        exit;
    }
} else {
    print $query." : ERROR : INVALID-PORT"."\n";
    exit;
}

open(LSOFP,"$LSOF -nPDi -T -FLn -iTCP@".$peer_addr.":".$cli_port."|");

my $user = "UNKNOWN";
while (my $a_line = <LSOFP>) {
    # extract user name.
    if ($a_line =~ /^L.*/) {
        ($user) = ($a_line =~ /^L(.*)/);
    }

    # make sure local port matches.
    if ($a_line =~ /^n.*:\Q$serv_port->/) {
        print $serv_port.", ".$cli_port." : USERID : UNIX :".$user."\n";
        exit;
    }
}

print $serv_port.", ".$cli_port." : ERROR : NO-USER"."\n";


## isexec($path) -- is $path executable
#
# $path   = absolute or relative path to file to test for executabiity.
#	    Paths that begin with neither '/' nor '.' that arent't found as
#	    simple references are also tested with the path prefixes of the
#	    PATH environment variable.  

sub
isexec {
    my ($path) = @_;
    my ($i, @P, $PATH);

    $path =~ s/^\s+|\s+$//g;
    if ($path eq "") { return(""); }
    if (($path =~ m#^[\/\.]#)) {
	if (-x $path) { return($path); }
	return("");
    }
    $PATH = $ENV{PATH};
    @P = split(":", $PATH);
    for ($i = 0; $i <= $#P; $i++) {
	if (-x "$P[$i]/$path") { return("$P[$i]/$path"); }
    }
    return("");
}