nermd has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I have a problem with one of my Scripts i code for my group house. Its an DNAT Webgui where a User can put in a port and an ip-adress on the local network and add this as a DNAT Rule to iptables (ie. if my co-inhabitants need a forward for ie. their p2p Client(s)). Befor i will try to explain my Problem, i will post two code snippets. the Form using CGI.pm:
$q->default_dtd('-//W3C//DTD HTML 4.01 Transitional//EN'); print $q->header(), $q->start_html("DNAT"), $q->br(), $q->br(), $q->start_form(), $q->start_table({ -boarder => '0' }), $q->Tr($q->td($q->radio_group(-name => 'proto', -values => ['tcp','udp'], -rows => 2, -columns => 1)), $q->td('Port: ', $q->textfield(-name => 'port')), $q->td('IP-Adresse: ', $q->textfield(-name => 'ip')), $q->td($q->submit(-name => 'add', -value => 'apply'))), $q->end_table(), $q->end_form(); $q->br(), $q->br();
and The "add Rule" Part:
if ($q->param('add')) { # seems that user has requested a new rule my $proto = $q->param('proto'); my $port = $q->param('port'); my $ip = $q->param('ip'); # set up rule hash's for NAT and FILTER Tables my %addrule_nat = ( protocol => "$proto", "destination-port" => "$port", jump => "DNAT", "to-destination" => "$ip", ); my %addrule_for = ( protocol => $proto, "in-interface" => 'eth0', "out-interface" => 'eth1', "destination-port" => $port, jump => "ACCEPT", ); my %addrule_inp = ( protocol => $proto, "destination-port" => $port, jump => "ACCEPT", ); my $erfolg_nat = $nat->append_entry('PREROUTING', \%addrule_nat) || d +ie "NAT: $!"; my $erfolg_for = $filter->insert_entry('FORWARD', \%addrule_for, 0) | +| die "FORWARD: $!"; my $erfolg_inp = $filter->insert_entry('INPUT', \%addrule_inp, 0) || +die "INPUT: $!"; # now commit our work my $commit_nat = $nat->commit(); my $commit_filter = $filter->commit(); # tell the user it is done an refresh in 5sec _cut_ }
In short, the Problem is, everytime i call the script to add a rule, i get an error from the append_entry() methode, telling me that "protocol: Must be passed as integer or string at ..." Ok i know this fact, but i thought i'd alredy do so. So i added a debug output to check the value of $proto ... its a scalar with "tcp" (or "udp") as its value. But why IPTables::IPv4 still tells me that its "not a string"?? Next Test i did was to add "tcp" as a static element of the %addrule Hash ... and woow suddenly append_entry() seems to have no more problems with the protocol, instead now its throws me the same error for the IP-Adress :(. What is wrong with this code?? I'm a realy CGI newb so - imho - i think the problem is somewhere at:
my $proto = $q->param('proto'); my $port = $q->param('port'); my $ip = $q->param('ip');
Can anybody help me with this issue?

Thanks and sorry for my bad english
nermd

Replies are listed 'Best First'.
Re: IPTables::IPv4 and parameters from CGI.pm
by McDarren (Abbot) on Jun 14, 2006 at 15:16 UTC
    I can't see anything obviously wrong with your code, but one suggestion I would make is to grab a hold of Data::Dumper::Simple and add
    print Dumper(%addrule_nat);

    ..just before you call the append_entry() method. This should help to confirm whether or not all values are present in the hash.

    One other comment - you must really trust your users if you're allowing them to modify the firewall via a web-interface ;)

    Cheers,
    Darren :)

      Thanks for the tipp, i tried it and hell ... just as i assumed ... everything looks perfect.
      So, here is the output of Dumper(%addrule_nat);
      %addrule_nat = ( 'protocol' => 'tcp', 'to-destination' => '10.0.1.2', 'destination-port' => '6990', 'jump' => 'DNAT' );
      But still, IPTables::IPv4 reports the error msg:

      "protocol: Must be passed as integer or string at..."

      Hmm maby i will drop the Author of IPTables::IPv4 a mail.
      But still, every tipp is welcome :)!

      And by the way, of course i trust my best friends.

      nermd