LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2003, 03:05 PM   #1
hkerssies
LQ Newbie
 
Registered: Sep 2003
Posts: 2

Rep: Reputation: 0
Question php and iptables


I have a question about php and a batch program that uses iptables.
This is what i want to do:
A web page with a button that add's a iptable rule or delete's a rule.
I have a script but you have to be root to use iptables.
i have tried to set the suid bit and now the file has rxsrxsr-x root:root
but stil has the problem that it is apache and not root who run's it.

What I want is turn the internet on and off in different subnets of the network. If there is an easy'er way for doing this with a webpage please gif me a hand.
Thanks for your time in dealing with this problem
Hans (Holland)
 
Old 09-08-2003, 04:03 PM   #2
KendersPlace
Member
 
Registered: Feb 2003
Location: Phoenix, AZ - USA
Distribution: RedHat 8, Micro$haft
Posts: 33

Rep: Reputation: 15
I played with this same problem a few months back - using php via apache to open and close ports in a remote firewall so I didn't have to hand out root level ssh logins to the "part time" web admins.

It was a challenge, but here's what I did... (RedHat 8, but you should be able to figure it out for whatever distro you use).

Edit your "sudoers" file. /etc/sudoers
Add this entry at the bottom:

apache ALL=NOPASSWD: /sbin/iptables

This will allow apache user to issue commands to iptables.

Then, write your php script like this...

PHP Code:
// Build the rule
$open_rule "iptables -A TABLENAME -s 123.123.123.123 -j ACCEPT";

// Execute the rule on the system
`$open_rule`; 
Each time this script is executed, you will see an entry showing what was executed in your /var/log/secure log so you can keep an eye on what's happening.

As a side note and a tip... I also include in my script a piece that will write each "open rule" to a MySQL table. Then, to reverse the rule, all I have to do is read the entry from the table, replace the "-A" with "-D" and issue the same command again, and it will delete the rule from iptables, so I can easily open, then turn around and close the remote ports easily.

I'm sure you could use the same logic to run "iptables -L" and display the current firewall configuration on a web page for the user to see also for remote management, though I've never actually tried this.

Hope that helps. Took me about 2 or 3 days to figure this out on my own.
 
Old 09-09-2003, 04:51 AM   #3
hkerssies
LQ Newbie
 
Registered: Sep 2003
Posts: 2

Original Poster
Rep: Reputation: 0
I did this:
Edit your "sudoers" file. /etc/sudoers
Add this entry at the bottom:

apache ALL=NOPASSWD: /sbin/iptables

This will allow apache user to issue commands to iptables.

and i added an user hans so that I can test this faster but still I can't execute iptables as user hans.

Did you changed the permissions on the file iptable or is the owner still root?

It's nice that it works this gifs me hope it will work in the nearby future.
 
Old 10-22-2003, 02:14 PM   #4
wytcld
LQ Newbie
 
Registered: Oct 2003
Location: VT
Distribution: Gentoo
Posts: 2

Rep: Reputation: 0
Thumbs up Using sudo with php

Great idea.

The sketch of a php script from Kenders was not fleshed out. A working script looks more like:

Code:
<?php

$command="iptables -t nat -I PREROUTING -p tcp -s " .  $_SERVER['REMOTE_ADDR'] . " -d 123.123.123.123 --dport 22 -j DNAT --to 192.168.1.1";
exec ("sudo $command");
?>
This example is DNAT'ing a remote user to port 22 on an internal machine after they log into the page (which is password protected on Apache/Mod-SSL). Of course, you probably want to do something different.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php apache or php cgi - php learner rblampain Linux - Security 3 12-17-2004 11:10 PM
Iptables - Couldn't load target `ACCPET':/lib/iptables/libipt_ACCPET.so: z00t Linux - Security 3 01-26-2004 02:24 AM
IPtables Log Analyzer from http://www.gege.org/iptables/ brainlego Linux - Software 0 08-11-2003 06:08 AM
iptables book wich one can you pll recomment to be an iptables expert? linuxownt Linux - General 2 06-26-2003 04:38 PM
My iptables script is /etc/sysconfig/iptables. How do i make this baby execute on boo ForumKid Linux - General 3 01-22-2002 07:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration