How to build squid authentication helpers
Build your own authentication helper using the language of your choice
Download the whole article as PDF
Short URL: http://fsmsh.com/2144
- 2007-07-09
- Server side | Intermediate
-
Write a full post in response to this!
This content was sponsored by:
Have you ever tried to figure out how to make Squid authenticate users according to your own exotic rules? Users are in a DB? Are you using an ActiveDirectory? Users/passwords are authenticated by a java class? Everything is possible. Here I intend to explain how to make your own custom authentication helpers so you can develop your own routines for your own requirements.
Squid
Squid is such a wonderful HTTP cache server. It’s stable, fast, highly customizable, and you barely notice it when it’s working (oh and did I say it’s free as in freedom?).
It comes with a number of authentication helpers, but there are times when these helpers are not enough. Sometimes you have authentication requirements exotic enough that make those default helpers useless.
The need
Suppose you have to do a little checking: you have users/passwords in a MySQL DB table. To make it a little more exotic, passwords are not directly stored, but MD5s instead. Suppose that you also want the allowed users to be listed in a text file in your file system and, finally, make an LDAP request to see if there’s an item in the directory that matches usernames by the field named “thisCrazyField”. If all that fails, the user/password can be the pair “foo/bar” (a backdoor… just in case you want to see some things that are better left anonymous in the Squid logs!). I am sure no default helper will be able to pull it off.
Before you waste more brainpower trying to figure out when your boss is going to fire you because you couldn’t find a way to make this authentication scheme work with Squid (or any other HTTP cache solution for that matter), you should know that you can make a stand-alone program that can tell if a user is permitted to go through or not. Easy!
Authentication helpers
What a helper does (even a default one) is very simple: it reads username/password pairs from Standard Input one pair at a time in a single line of text, and writes a single line of text to Standard Output that either says “OK” (for a user that can go through) or “ERR” (in case of problems)—that’s it. The helper has to repeat this action in an endless cycle. Username and passwords are encoded using the character encoding described in RFC 1738 (section 2.2) and are separated by a white space.
What a helper does (even default ones) is very simple
Say I want to make a helper in PHP that will check if the user/password is one of the following pairs:
- hello/world
- foo/bar
Here’s the PHP code:
<?
if (! defined(STDIN)) {
define("STDIN", fopen("php://stdin", "r"));
}
while (!feof(STDIN)) {
$line = trim(fgets(STDIN));
$fields = explode(' ', $line);
$username = rawurldecode($fields[0]); //1738
$password = rawurldecode($fields[1]); //1738
if ($username == 'hello'
and $password == 'world') {
fwrite(STDOUT, "OK\n");
} else if ($username == 'fo'
and $password == 'bar') {
fwrite(STDOUT, "OK\n");
} else {
// failed miserably
fwrite(STDOUT, "ERR\n");
}
}
?>
That’s it! I’ve just created a PHP-based Squid helper. Feel free to use any tool you want, be it bash, python, Perl or any other language you like. The only requirement is that the language is able to read from the standard input and write to the standard output (if you want to use bash, be careful to avoid making passwords visible with a ps ax).
Testing your masterpiece
Now comes the testing part. You have to act the same way Squid would have to: start the script and interact with it passing username/password pairs. If it outputs “OK” or “ERR” as wanted, then your helper is done. Here’s a demonstration of the helper I just made:
$ php squid_helper.php hello world OK foo bar ERR
Oops! “foo/bar” is not okay. Go to the source code of the helper. See what’s going on? I wrote == '**fo**' instead of == '**foo**'. Correct it in the source code and try all over again:
Write a full post in response to this!
Download the files attached with this article.
Do you like this post?
Vote for it!
Copyright information
This article is made available under the "Attribution" Creative Commons License 3.0 available from http://creativecommons.org/licenses/by/3.0/.
Biography
Edmundo Carmona: Edmundo is a Venezuelan Computer Engineer. He is working as a Freelance Java Developer in Colombia since very recently. He has also been a GNU/Linux user and consultant for several years. After years of being retired from music, he's working right now to regain his classical flute skills.
- Login or register to post comments
- 27510 reads
- Printer friendly version (unavailable!)




Best voted contents
-
Linux performance: is Linux becoming just too slow and bloated?
Mitch Meyran, 2010-01-26 -
The Bizarre Cathedral - 66
Ryan Cartwright, 2010-02-01 -
Save "Sita Sings the Blues" from the Flash format: can you convert FLA?
Terry Hancock, 2010-01-29 -
The Morevna Project: Anime with Synfig and Blender
Terry Hancock, 2010-02-08
Buzz authors
Free Software news
- You should be limited only by your hardware, not by crap software or financial burden. via @psyphen #truedat #freesoftware #goodgnus
- Discovered http://nakedcomputers.org/ - listing retailers of PCs without an OS. Enabling you to put your own on. #freesoftware #linux
- Just added myself to the http://wefollow.com twitter directory under: #chengdu_china #freesoftware #opensource #b... http://bit.ly/aQUevU
- Just added myself to the http://wefollow.com twitter directory under: #chengdu_china #freesoftware #opensource #browser #mobile
- New blog post: Gestire il parco hardware/... http://bit.ly/cmRG76 #freesoftware #review
Similar entries
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 60 days
-
Open Science and climategate: The IPCC/CRU needs to take a leaf out of CERN's Book
Gary Richmond, 2009-12-16 -
Linux performance: is Linux becoming just too slow and bloated?
Mitch Meyran, 2010-01-26 -
Web code is already open - why not make it free as well
Ryan Cartwright, 2010-01-20 -
Save "Sita Sings the Blues" from the Flash format: can you convert FLA?
Terry Hancock, 2010-01-29 -
Mastering a DVD using QDVDAuthor
Terry Hancock, 2010-01-08
Hot topics - last 21 days
-
Linux performance: is Linux becoming just too slow and bloated?
Mitch Meyran, 2010-01-26 -
Web code is already open - why not make it free as well
Ryan Cartwright, 2010-01-20 -
Save "Sita Sings the Blues" from the Flash format: can you convert FLA?
Terry Hancock, 2010-01-29 -
The Open-PC: one step closer to open-hardware
Ryan Cartwright, 2010-01-20
Odiogo
Free Software Magazine uses Apollo, project management and CRM for its everyday activities!

AWSOME
Submitted by dorgan on Tue, 2007-06-05 15:46.
Vote!This article rocks and will allow me to have one less things to integrate. Thank you soo much.
-k reconfigure issue / question
Submitted by amit_shogun on Tue, 2009-10-27 05:25.
Vote!First, Great article, helped me a lot. Thanks.
When using 'squid -k reconfigure' seems like my auth program (written in C++) is duplicating it self, i,e: If i have 32 children - i get 64, 96 etc...
What am i missing. I need '-k reconfigure' to always keep 32 (in my case) children.
Thanks in advance, Amit
I forgot to mention...
Submitted by Edmundo Carmona on Tue, 2007-06-05 22:26.
Vote!...that in the downloads of the article is a helper to authenticate against ActiveDirectory as a real example.
And thanks for that comment!
Glad to be helpful (even more if there are some ISA servers to be replaced as a side effect of the article :-D).
This is alternative to NoCat, I suppose
Submitted by valen_willie on Wed, 2007-06-06 01:17.
Vote!Excellent article. My company is working on providing Internet pre-paid system to miners who are in the middle of nowhere. The only backhaul we have is Satellite, so we have to treasure our bandwidth. We are tinkling with NoCat at the moment. This article opens up a new possibility of a whole new system all together. Of course, we have to think of how to block all traffics that squid cannot proxy, or else there will be holes in this system (`iptables' to the rescue!). Also, how to tunnel other traffics via HTTP.
Problem with your Script
Submitted by omid mohajerani (not verified) on Sat, 2007-07-14 09:18.
Vote!hi
first thank you for your nice article . I have a problem with the script
when i want to run it ( /bin/php /etc/squid/script.php)
it says :
Constant STDIN already defined in /etc/squid/script.php on line 3
please help me.
best regards
That's a PHP problem
Submitted by Edmundo Carmona on Mon, 2007-07-16 01:20.
Vote!I thought the if (! defined()) would avoid that (at least, it does avoid that problem over here).
Make sure you included the "!" in the conditional on line 2. If you did, that'd be weird... but anyway it seems that your "PHP machine" already has STDIN defined, so you can comment line 3, where it's "redefined"... or remove that whole checking section altogether (the conditional and definition of STDIN).
Hope that works.
problem
Submitted by Anonymous visitor (not verified) on Tue, 2007-07-17 10:12.
Vote!i have problem when using this document. this is the error :
The basicauthenticator helpers are crashing too rapidly, need help!
sounds like...
Submitted by Edmundo Carmona on Tue, 2007-07-17 17:12.
Vote!sounds like your program is crashing right after squid starts it (or them, if you have many children). Did you test it as a standalone console application before using it with squid to see that it receives the username/password pair and replies with OK/ERR in an infinite cycle?
Undefined Offset
Submitted by omid mohajerani (not verified) on Wed, 2007-07-18 06:52.
Vote!first tanx for your answer ; I have delete that line and now my script( ofcourse yours ) is like this
<?
while (!feof(STDIN)) {
$line = trim(fgets(STDIN));
$fields = explode(' ', $line);
$username = rawurldecode($fields[0]); //1738
$password = rawurldecode($fields[1]); //1738
if ($username == 'hello'
and $password == 'world') {
fwrite(STDOUT, "OK\n");
} else if ($username == 'fo'
and $password == 'bar') {
fwrite(STDOUT, "OK\n");
} else {
// failed miserably
fwrite(STDOUT, "ERR\n");
}
}
?>
and now result of excuting /bin/php script.php is :(of course when i enter a string)
PHP Notice: Undefined offset: 1 in /omid/omid.php on line 10
ERR
any suggestion ?
email me
Submitted by Edmundo Carmona on Thu, 2007-07-19 13:47.
Vote!Why don't you email me? I just copied (verbatim) your script and tried it over here and it worked:
$ php prueba.php
hello world
OK
foo
ERR
foo bar
ERR
fo bar
OK
it's ok
ERR
Maybe the problem is here:
$ php -version
PHP 5.2.3-1ubuntu2 (cli) (built: Jul 4 2007 16:13:07)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
Anyway, email me so we can take a closer look at what's going on. eantoranz at google's public mail service, you know, right? :-D
Solved Omid's problem
Submitted by Edmundo Carmona on Fri, 2007-07-20 20:20.
Vote!The problem is the define call. I have the fopen() right in the define, and it fails that way. If you do the fopen and save the result in a temporary variable, it works.
Try with this:
if (! defined(STDIN)) {
$temp = fopen("php://stdin", "r");
define("STDIN", $temp);
}
It worked with version 5.1.2 that way.
It worked with 5.2.3 the original way... I guess it's a bug in 5.1.2
Problem with script on Windows 2003 AD
Submitted by AHMED.S (not verified) on Fri, 2007-09-14 09:16.
Vote!Thanks for your wonderful article. I am having problems with your script. I believe it is due to the fact that my windows 2003 AD requires authentication to do any LDAP binds. I have another php LDAP application that connects to my AD successfully as it has a variable to configure a user account to connect to the AD with.
Can you please tell me how I can edit your script to bind to ldap using a username and password?
thanks
Did you see the attachments?
Submitted by Edmundo Carmona on Sun, 2007-09-16 16:47.
Vote!Ahmed, in the article's attachments, there's a PHP script that authenticates against AD through LDAP, and it binds to AD using the username/password provided by squid. Are you having problems with that script? Email me if you have any problems: eantoranz at gmail dot com.
Transmitting username and password as cleartext?
Submitted by Tobias K. (not verified) on Tue, 2007-09-25 18:44.
Vote!Thanks a lot for your nice article and for writing for the freesoftwaremagazine.
In many environments the transmission of usernames and passwords as cleartext is not acceptable.
Do you haven an idea of how to solve this problem an transmit the authorization data between client and proxy encrypted?
Regards.
Tobias
There are some other methods
Submitted by Edmundo Carmona on Wed, 2007-09-26 13:02.
Vote!If you check my example (and the attached script), I always used the "basic" method. However, there are some other methods: digest, ntlm, negociate, so don't stick with basic if you want to do it some other way.
As I'm reading squid.conf's details of each method, helpers will interact with squid in different ways, so take a close look at that.
A comment received by email...
Submitted by admin on Sat, 2008-06-14 01:16.
Vote!Contacted Mr. Carmona today concerning the PHP script that authenticates against AD through LDAP. It no longer appears to be in the downloadable section of this article. I made mention of this in my email and requested help if he could provide.
I barely got the email sent before he responded. Not only did he respond he sent me the code after a very short time. Plus he said if I needed more help just email him.
Very impressed.
Tis my opinion the world needs more like him. Thanks for sharing this and thanks for all the help
Authentication Helpers
Submitted by Danbo on Thu, 2008-12-25 07:57.
Vote!Hi everyone,
I am a web designer and I have limited linux knowledge but I have set up two linux boxes.
One that runs a Voip system and another running Squid.
I have managed to get NCSA authentication working on the Squid proxy server but what bothers me is, that, the NCSA authentication method allows the same username and password to make simulataneous multiple logins which I don't want.
I need to write an authentication helper to restrict logins to one machine per session. Can anyone help me with this?
Kind Regards and Seasons Greetings!
Danbo
Squid on Windows 2003
Submitted by rosdi on Fri, 2009-02-27 03:37.
Vote!If you install Squid on Windows Server (2000/2003) and your requirement is simple, it is easier to use mswin_auth.exe provided in libexec.
Then you can login to Squid using your windows login id and password.
Check out mswin_auth.exe in libexec folder.
Thanks for the article. For
Submitted by jensen on Thu, 2009-05-28 16:33.
Vote!Thanks for the article. For a squid rookie like me the perfect entry point. As I intend to use squid mainly for elaborate authentication and logging purposes could you (or someone) tell me
-if there's a way to define a chain of authentication helpers and
-if there's a way to define a logging helper?
thanks a lot jens
Multiple authentication helpers?
Submitted by jensen on Thu, 2009-05-28 16:58.
Vote!First of all thanks for your article. For me as a rookie it was a good entry point.
As i want to use squid mainly for authentication and logging there are two questions left: Is there a way to define a chain of authentication helpers? Can i define a logging helper in the same way as a authentication helper? Thanks for any clue,
jens