home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20110725.etc.tar.gz / bradford.20110725.etc.tar / etc / sysconfig / scripts / SuSEfirewall2-rpcinfo < prev    next >
Text File  |  2006-04-22  |  4KB  |  149 lines

  1. #!/usr/bin/perl -w
  2. # SuSEfirewall2-rpcinfo - helper script for SuSEfirewall2
  3. # Copyright (C) 2004 SUSE Linux AG
  4. # Copyright (C) 2005 SUSE Linux Products GmbH
  5. #
  6. # Author: Ludwig Nussel
  7. # Please send feedback via http://www.suse.de/feedback
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # version 2 as published by the Free Software Foundation.
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. # determine ports of RPC services specified on the command line and print them
  21. # as iptables command line parameters. Only services that are actually running
  22. # and are running as root are printed.
  23.  
  24. use strict;
  25. #use Data::Dumper;
  26.  
  27. if ($#ARGV < 0)
  28. {
  29.     print STDERR "Usage: $0 <service ...>\n\n";
  30.     exit 1;
  31. }
  32.  
  33. # {
  34. #           'ypbind' => [
  35. #                        {
  36. #                          'udp' => [
  37. #                                     979
  38. #                                   ],
  39. #                          'tcp' => [
  40. #                                     982
  41. #                                   ],
  42. #                          'sport' => '666:777',
  43. #                          'net' => '10.10.0.1/32'
  44. #                        },
  45. # }
  46. my %services;
  47. foreach my $service (@ARGV)
  48. {
  49.     my @a = split(/,/,$service);
  50.     if( $#a == 0)
  51.     {
  52.     push @{$services{$service}}, {};
  53.     }
  54.     elsif ($#a >= 2 && $a[1] eq '_rpc_')
  55.     {
  56.     my %h = ();
  57.     $h{'net'} = $a[0] if($a[0] && length($a[0]));
  58.     $h{'sport'} = $a[3] if($a[3] && length($a[3]));
  59.     push @{$services{$a[2]}}, \%h;
  60.     }
  61. }
  62.  
  63. my %udpports = ();
  64. my %tcpports = ();
  65.  
  66. # collect registered rpc services
  67. open (RPCINFO, '/usr/sbin/rpcinfo -p localhost|') or die;
  68. <RPCINFO>; # header line
  69. while(<RPCINFO>)
  70. {
  71.     chomp;
  72.     my @line = split;
  73.     next if($#line < 4);
  74.     next unless (exists $services{$line[4]});
  75.     if($line[2] eq 'udp')
  76.     {
  77.     $udpports{$line[3]} = $line[4];
  78.     }
  79.     elsif($line[2] eq 'tcp')
  80.     {
  81.     $tcpports{$line[3]} = $line[4];
  82.     }
  83. }
  84. close RPCINFO;
  85.  
  86. # @param file
  87. # @param hashref
  88. sub getportsfor($$)
  89. {
  90.     my ($proto, $href) = @_;
  91.     # check if the registered ports are actually used and whether they are
  92.     # owned by a process running as root
  93.     open (FILE, '<', "/proc/net/$proto") or die;
  94.     <FILE>; # header line
  95.     my $ret = 0;
  96.     while(<FILE>)
  97.     {
  98.     chomp;
  99.     my @line = split;
  100.     next if($line[7] != 0); # only root allowed
  101.     my ($addr, $port) = split(/:/, $line[1], 2);
  102.     $port = pack('H*', $port); # "007B" => "\x00\x7B"
  103.     $port = unpack('n', $port); # "\x00\x7B" => 0x007B
  104.     if(exists $href->{$port})
  105.     {
  106.         ++$ret;
  107.         foreach my $h (@{$services{$href->{$port}}})
  108.         {
  109.         push @{$h->{$proto}}, $port;
  110.         }
  111.     }
  112.     }
  113.     close FILE;
  114.  
  115.     # always also add portmapper
  116.     if($ret && !exists $services{'portmapper'})
  117.     {
  118.     push @{$services{'portmapper'}}, { tcp => [111], udp => [111] };
  119.     }
  120. }
  121.  
  122. getportsfor('udp', \%udpports);
  123. getportsfor('tcp', \%tcpports);
  124.  
  125. #print Dumper(\%services);
  126.  
  127. foreach my $l (values %services)
  128. {
  129.     foreach my $h (@$l)
  130.     {
  131.     foreach my $proto (('udp', 'tcp'))
  132.     {
  133.         if(exists($h->{$proto}))
  134.         {
  135.         foreach my $port (@{$h->{$proto}})
  136.         {
  137.             print "-p $proto --dport $port";
  138.             print " --sport ".$h->{'sport'} if exists $h->{'sport'};
  139.             print " -s ".$h->{'net'} if exists $h->{'net'};
  140.             print "\n";
  141.         }
  142.         }
  143.     }
  144.     }
  145. }
  146.