File: scan_rhosts.pl

package info (click to toggle)
tiger 2.2.4-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,188 kB
  • ctags: 240
  • sloc: sh: 8,388; ansic: 2,109; makefile: 141; perl: 106
file content (114 lines) | stat: -rwxr-xr-x 2,792 bytes parent folder | download
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
#!/usr/local/bin/perl -- # -*-Perl -*-
#
#  Scan .rhosts files for various things, including checks for
#  rhosting of other users.
#
#  Usage:  scan.rhosts
#
#----
# Where is the finger program?
$FINGER="/usr/ucb/finger";
#----
# How long do we give hosts to respond? (in seconds)
$ALARMTIME=30;

$SIG{'ALRM'} = 'sigalrm';

sub sigalrm {
    $timeout = 1;
    kill 9, $pid;
}

setpwent;

while(($user, $pwd, $uid, $gid, $quota, $comment, $gcos, $home) = getpwent){
    if(-f "$home/.rhosts"){
	$header = "$user ($gcos):";
	if((($d,$i,$mode) = stat(_))){
	    if($mode & 0066){
		$perms="";
		if($mode & 0060){
		    $perms .= " group";
		    $perms .= " readable" if($mode & 0040);
		    $perms .= " writable" if($mode & 0020);
		    $perms .= "," if($mode & 0006);
		}
		if($mode & 0006){
		    $perms .= " world";
		    $perms .= " readable" if($mode & 0004);
		    $perms .= " writable" if($mode & 0002);
		}
		print "$header .rhosts is$perms.\n";
	    }
	}
	else {
	    warn "stat($home/.rhosts): $!.\n";
	}
	
	open(RHOSTS, "$home/.rhosts") || warn "Can't open $home/.rhosts: $!\n";
	while(<RHOSTS>){
	    if(!/^\s*$/){
		($rhost, $ruser) = split;
		if($rhost eq "+" || $ruser eq "+"){
		    print "$header has + in ~/.rhosts\n";
		}
		elsif($rhost !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ &&
		      !(($s) = gethostbyname($rhost))){
		    print "$header $ruser@$rhost ([host unknown]).\n";
		}
		elsif($ruser ne "" && $ruser ne $user){
		    alarm($ALARMTIME);
		    $timeout = 0;
		    $foundit = 0;
		    $pid = open(FINGER, "$FINGER $ruser@$rhost 2>&1 |") || 
			warn "Can't execute $FINGER\n";
		    while(<FINGER>){
			alarm(0);
			if(/connect: *([^\r\n]*)\s$/){
			    $foundit = 1;
			    if($rhost =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/){
				print "$header (INVALID ENTRY) $ruser@$rhost ([$1]).\n";
			    }
			    else {
				print "$header $ruser@$rhost ([$1]).\n";
			    }
			}
			elsif(/^[Ll]og[oi]n.*:[ \t]+(\w+).+: *([^\r\n]*)\s*$/){
			    if($1 eq $ruser){
				$rname = $2;
				$foundit=1;
				if($rhost =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/){
				    print "$header (INVALID ENTRY) $ruser@$rhost ($rname).\n";
				}
				else {
				    print "$header $ruser@$rhost ($rname)\n";
				}
				last;
			    }
			}
		    }
		    alarm(0);
		    close(FINGER);
		    if($timeout){
			if($rhost =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/){
			    print "$header (INVALID ENTRY) $ruser@$rhost ($rname).\n";
			}
			else {
			    print "$header $ruser@$rhost ([timeout]).\n";
			}
		    }
		    elsif(!$foundit){
			if($rhost =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/){
			    print "$header (INVALID ENTRY) $ruser@$rhost ([no answer]).\n";
			}
			else {
			    print "$header $ruser@$rhost ([no answer]).\n";
			}
		    }
		}
	    }
	}
	close(RHOSTS);
    }
}
endpwent;