File: verify.in

package info (click to toggle)
freeswan 2.04-11.3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 23,340 kB
  • ctags: 12,260
  • sloc: ansic: 72,499; sh: 14,497; asm: 3,312; perl: 3,153; xml: 2,961; makefile: 2,702; tcl: 620; exp: 612; pascal: 228; sed: 206; awk: 124; lisp: 3
file content (357 lines) | stat: -rwxr-xr-x 9,292 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/perl
# mail OE DNS RR info to relevent administrator
#
# Copyright (C) 2003 Sam Sgro <sam@freeswan.org> 
#
# Based on "verify" from the FreeS/WAN distribution, (C) 2001 Michael 
# Richardson <mcr@freeswan.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

$me="ipsec verify";
$ENV{'PATH'}="/sbin:/usr/bin:/usr/local/sbin:@IPSEC_SBINDIR@:$ENV{'PATH'}";
if($ENV{'IPSEC_CONFS'}) { $conf=$ENV{'IPSEC_CONFS'} } else { $conf= `ipsec --confdir`; chomp($conf); }

# Should we print in colour by default?
$ctype=`/sbin/consoletype`;
if ( $ctype && !($ctype eq "serial"))
{
    $colour="1";
}

sub printfun {
  print sprintf("%-65s",@_);
}

# capture STDOUT as @out, STDERR as @err with no temp files.
sub run {
    $command=shift;

    pipe child_read, parent_write; 
    pipe parent_read, child_write;
    pipe err_read, err_write; 
    
    $mypid=fork;
    if($mypid)
    {
	close child_write; close err_write;
	@out=<parent_read>;
	@err=<err_read>;
    }
    else 
    { 
        close parent_read; close parent_write;
	open STDOUT,">&child_write"; 
        open STDERR,">&err_write";
        exec $command; print STDERR "Cannot execute command \"$command\": $!\n";
    }
}

# Code to print out [OK], [FAILED].
sub errchk {
	if (!shift(@_)) 
	{
	    print "\t[";
	    if($colour) { print "\e[1;31m"; } 
	    if(@_) 
	    { 
		print "@_"; 
	    } 
	    else 
	    { 
		print "FAILED"; 
	    }
	    if($colour) { print "\e[0;39m"; } 
	    print "]\n";
	    if(@err) 
	    { 
		print @err; 
	    }
	}
	else
	{	    
	    print "\t[";
	    if($colour) { print "\e[1;32m"; } 
	    print "OK";
	    if($colour) { print "\e[0;39m"; } 
	    print "]\n";
        }
}

# Code to print out [DEPRECATED] and key restrict message
sub deprecated {

    print "\t[";
    if($colour) { print "\e[1;33m"; } 
    print "DEPRECATED";
    if($colour) { print "\e[0;39m"; } 
    print "]\n
RFC 3445 restricts the use of the KEY RR to DNSSEC applications. The use of 
a KEY record sub-type for Opporunistic Encryption (OE) has been deprecated.
TXT records are used to provide all OE functionality.

For more information on these changes, see:
http://www.freeswan.org/freeswan_snaps/CURRENT-SNAP/doc/upgrading.html\n\n";
}

# Verification routines begin here...
#
# Check DNS Configuration based on a hostname
# $1 = Hostname (string)
# eg: checkdnshost oetest.freeswan.org
sub checkdnshost {
    run "host -t key $_[0]";
    ($keypresent)=grep /(0x4200|16896)/, @out;
    if($keypresent) 
    { 
	printfun "Looking for KEY in forward map: $_[0]";
	deprecated; 
    }


    printfun "Looking for TXT in forward map: $_[0]";
    run "host -t txt $_[0]";
    ($txtpresent)=grep /X-IPsec-Server/,@out;
    errchk "$txtpresent", "MISSING";
}

# Check DNS Configuration based on IP address
# $1 = IP Address (string)
# eg: checkdnsip 127.0.0.2
sub checkdnsip {
    $fortxt=$_[0];
    $revtxt=join('.',reverse(split(/\./, $fortxt))).".in-addr.arpa.";
    printfun "Looking for TXT in reverse map: $revtxt";
    run "host -t txt $revtxt";
    ($txtpresent)=grep /X-IPsec-Server/,@out;
    errchk "$txtpresent", "MISSING";

    if($txtpresent) {
	$txtpresent=~ s/.*X-IPsec-Server\([0-9].*\)=//; $txtpresent=~ s/[\"\ ].*//;
	$gwip=$txtpresent;
	chomp($gwip);
	$gwrev=join('.',reverse(split(/\./, $gwip))).".in-addr.arpa.";
	# Check for a KEY record for the indicated IPSec GW.
	run "host -t key $gwrev";
	($keypresent)=grep /(0x4200|16896)/, @out;
	if($keypresent) 
	{ 
	    printfun "Looking for KEY in reverse map: $gwrev";
	    deprecated; 
	}
	# If the host is its own gateway, then we know we've got a TXT record.
	if($gwip ne $fortxt) {	
	    printfun "Looking for TXT in reverse map: $gwrev";
	    run "host -t txt $gwrev";
	    ($txtpresent)=grep /X-IPsec-Server/,@out;
	    errchk "$txtpresent", "MISSING";
	}

    }
}

sub udp500check {
    run "netstat -an";
    # Flaw: Only taking the first IP address found listening...
    ($listen)=grep /:500/, @out;
    if(!$listen)
    { 
	printfun "Pluto not listening on port udp 500. Check interfaces defintion in ipsec.conf.";	
    }
    else
    {
	( $rest, $rest, $rest, $defaultsrcip, $rest)=split(' ',$listen); 
	$defaultsrcip=~ s/:500//;
	run "/bin/ls /proc/sys/net/ipv4/conf";
	foreach $net (grep !/(lo|all|default|ipsec|vmnet)/, @check=@out)
	{
	    if ( -e "/proc/net/ip_fwchains" )
	    {
		chomp($net);
		printfun "Checking IPchains port 500 hole ($defaultsrcip on $net)";
		run "ipchains --check input -p udp --src $defaultsrcip 500 --dst 2.3.4.5 500 -i $net";
		chomp($out[0]);
		if( $out[0] eq "accepted" ) { errchk "$out[0]"; } else { errchk "","BLOCKED"; }
	    }
	    elsif( -e "/proc/net/ip_tables_names" )
	    {
		# iptables --check will never happen, hack it outselves
		# use tcpdump? nmap? custom rule? at least check outgoing 500? ping outside activeOE box?
		# print "Cannot check if IPtables has port 500 hole ($defaultsrcip on $net)";
	    }
	}
    }
}

sub checktunnel {
    $csource=$_[0]; $cdest=$_[1]; $ctun=$_[2]; $all="0.0.0.0/0";

    printfun "Checking $ctun from $csource to $cdest";
    run "iptables -t nat -L POSTROUTING -n";
    @out=grep !/(Chain POSTROUTING|target)/, @out;
    foreach (@out) {
	( $target, $prot, $opt, $source, $dest ) = split(' ',$_);
	if(((($source eq $csource) || ($source eq $all)) && (($dest eq $cdest) || ($dest = $all))) && $target eq "ACCEPT")
	{ 
	    errchk "@out";
	}
	else
	{
	    @err="$target from $source to $dest kills tunnel $source -> $cdest\n";
	    errchk "","FAILED";
	}
    }
}

sub installstartcheck {
	print "Checking your system to see if IPsec got installed and started correctly:\n";

	printfun "Version check and ipsec on-path";
	run "ipsec --version";
	errchk "@out";
	print grep /Linux/, @out;

        printfun "Checking for KLIPS support in kernel";
	if ( -e "/proc/net/ipsec_eroute" ) { $test="1" }
	errchk "$test";

	printfun "Checking for RSA private key ($conf/ipsec.secrets)";
	run "ipsec showhostkey --txt 1.1.1.1";
	errchk @out;
	
	# Wouldn't this test fail if your mucked up your interface definition?
	printfun "Checking that pluto is running";
	run "ipsec whack --status";
	errchk "@out";
	if (grep /interface/, @out)
	{
	    udp500check;
	}
}

sub tunnelchecks {
    open("dev", "/proc/net/dev");
    if((grep !/(ipsec|lo:|Inter|packets)/, <dev>) > 1) 
    {
	printfun "Two or more interfaces found, checking IP forwarding";
        open("cat", "/proc/sys/net/ipv4/ip_forward");
	if(<cat> == "0") 
	{ 
	    errchk ""; 
	} 
	else 
	{
	    errchk "@out";
	    printfun "Checking NAT and MASQUERADEing";
	    if( -e "/proc/net/ip_conntrack" )
	    {
		run "iptables -t nat -L -n";
		if(grep /(NAT|MASQ)/, @out)
		{
		    printf "\n";
		    open("cat", "/proc/net/ipsec_eroute");
		    foreach(grep /tun0x/, <cat>)
		    {      
			@eroute=split(' ',$_);
			checktunnel $eroute[1], $eroute[3], $eroute[5];
		    }
		}
		else
		{
		    errchk "1";
		}
	    }
	    else
	    { 
		errchk "","N/A";
	    }
	}
    }
}

sub dnschecks {
    # Check the running hostname.
    printf "\nOpportunistic Encryption DNS checks:\n";
    run "hostname"; 
    ($hostname)=@out; chomp $hostname;
    checkdnshost $hostname;
    
    # Check all the public IP addresses...
    run "/sbin/ifconfig -a";
    foreach (grep /inet addr/,@out)
    {
	$_=~ s/^\s*//;
        @temp=split(/[:\ ]+/, $_);
	push(@address,$temp[2]);
    }
    # Purge all non-routeable IPs...
    @address=grep !/(127.*|10.*|172.1[6789]+.*.*|172.2+.*.*|172.3[01]+.*.*|192.168.*.*|169.254.*.*)/,@address;
    printfun "Does the machine have at least one non-private address?";
    errchk @address;
    foreach(@address=grep !$check{$_}++,@address)
    {
	checkdnsip $_;
    }
}

# Main function begins here!
# Harvest options, ensure they're valid.
use Getopt::Long;
%optctl = ("host" => \$hostname,"ip" => \$ip, "colour" => \$colour);
GetOptions(\%optctl, "host=s" ,"ip=s", "colour!");

# Exit if we get passed an option we don't recognize.
if($Getopt::Long::error) { exit; }


# If you've passed --host or --ip, do only those checks.
if($hostname || $ip)
{
# Check this --host for OE.
    if($hostname)
    {
	printf "Checking $hostname for Opportunistic Encryption:\n";
	checkdnshost $hostname;
	run "host -t A $hostname";
	if(($ipaddr) = grep (/address/i, @out))
	{
	    $ipaddr=~ s/.*address\ //;
	    chomp $ipaddr;
	    checkdnsip $ipaddr;
	}
	else
	{
	    printf "$hostname does not resolve to an IP, no reverse lookup tests possible.\n";
	}
    }
# Check this IP for OE.
    if($ip)
    {
	printf "Checking IP $ip for Opportunistic Encryption:\n";
	checkdnsip $ip;
    }
}
else
{
    # Call the default routines...
    # Root check...
    if($> != "0") 
    {
	print "To check this machine, you need to run \"$me\" as root.\n"; exit;
    }
    else
    {
	installstartcheck;
	tunnelchecks;
	dnschecks;
    }
}