TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (1) Thread Tools Search this Thread Display Modes
Old 12-10-2007, 09:30 PM   1 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
Wildhoney is on a distinguished road
Calculator Building an Apache-like Access Control List (ACL)

I was in two minds as to whether or not to release this as a script or an article, so I'm going to do both just in case people need any assistance with various sections of the code. However, I'm only going to be explaining areas which I feel need to be explained as all the rest can be found elsewhere, either on TalkPHP, or in the common-sense area of your brain.

We're going to be constructing an Apache-like, in fact we're going to be emulating the Apache ACL in its entirety. Although you could quite easily use Apache, this allows you to see how it's all done and even extend onto it to allow temporary bans which expire after a certain amount of time - if you were that way inclined.

The configuration file which we will accept will look like the following:

Code:
Order Deny, Allow
Deny from 127.0.0.1
Allow from All
As I'm only going to cover segments of the code, I will be linking out to articles which deal with that specific part so that you can read further on the subject.

The first article I'm going to link out to is the singleton article. The reasoning behind this is that I can't think of any scenario where you require more than once instance, seeing as how our construct parses the configuration document and then uses a member function to check whether or not the particular user in question is allowed access.

Exceptions will be prevalent throughout the script which you can read more about in the exceptions article. This will allow us to use a try and catch block on the front-end to see whether or not everything went smoothly with the parsing.

We will be using regular expressions to initially parse the configuration file into basic segments. The regex we will be using is as follows:

php Code:
preg_match('~(?P<command>[^\s]+)\s*\w*\s+(?P<options>.*)~', $szLine, $aMatches);

This will essentially parse every line in our configuration file and give us both the command and the options set for that command. All the options will be returned as a whole which we will later split up depending on their content.

Once we've got all the data required, the next thing is to list all the various scenarios - albeit I've picked all the scenarios which would mean a banned user and so if none of those match, then we assume the user is allowed to access the website. I came up with the following:
  1. The user's IP is in deny when order is deny, allow;
  2. Deny is set to ALL and user's IP address is not in allow when order is deny, allow;
  3. User's IP is not in allow and deny is set to ALL when order is allow, deny;
  4. User's IP is in deny and allow is set to ALL when order is allow, deny;
  5. User's IP is in deny and not in allow when order is allow, deny.

That's about all there is to the script. To use the script we need to get the object's instance first which can be done by calling the getInstance function which is defined as a static function:

php Code:
public static function getInstance()
{
    if(empty(self::$m_pInstance))
    {
        self::$m_pInstance = new TalkPHP_ACL();
    }
   
    return self::$m_pInstance;
}

This returns the instance to the variable which we are wanting to hold that particular object, and as we're using exceptions throughout the script the try and catch block is present to attempt to catch and output any exceptions:

php Code:
try
{
    $pACL = TalkPHP_ACL::getInstance();
}
catch(Exception $pEx)
{
    die($pEx->getMessage());
}

If there are no exceptions returned from the class then we can go ahead and check if the current user has accessed based on their IP address:

php Code:
if(!$pACL->hasAccess())
{
    die('You have been banned from this website.');
}

And that's all there is to the ACL script. Without further ado you may download the script from the following location. Hopefully the article has also cleared up any uncertainties you had about the script itself.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
bdm (12-14-2007)
Old 12-14-2007, 12:21 PM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I'm surprised no one has replied yet.

Thank you.
bdm is offline  
Reply With Quote
Old 12-14-2007, 01:03 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,182
Thanks: 88
Wildhoney is on a distinguished road
Default

! I'm not sure whether it warranted an article or not, but every little bit helps! I'm glad you liked it.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1699-building-apache-like-access-control-list-acl.html
Posted By For Type Date
Building an Apache-like Access Control List (ACL) - TalkPHP This thread Refback 01-03-2008 05:08 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:37 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design