[RADIATOR] Persistent database connection

Hugh Irvine hugh at open.com.au
Wed Aug 19 18:21:17 CDT 2009


Hello Neil -

It is much easier to use a normal AuthBy SQL clause with an Identifier  
in your configuration file, then simply refer to it in your hook.

Radiator will deal with the database connection for you and keep it  
open.

There is an example that is similar in "goodies/hooks.txt".

Something like this:

.....

# configuration file

<AuthBy SQL>
	Identifier SQL_database
	.....
</AuthBy>

.....

# hook

.....

sub
{
     my $p = ${$_[0]};   # proxy reply packet
     my $rp = ${$_[1]};  # reply packet to NAS
     my $op = ${$_[2]};  # original request packet
     my $sp = ${$_[3]};  # packet sent to proxy

     # Find the AuthBy clause
     my $authby = Radius::AuthGeneric::find('SQL_database');
     &main::log($main::LOG_DEBUG, "Found AuthBy with Identifier  
$identifier");

     # Get the request code from the proxy reply.
     my $code = $p->code;

     if ($code eq 'Access-Accept')
     {
         # Set the correct reply code in the reply packet
         # or if the AuthBy is undefined set to Access-Reject.

         if (defined $authby)
         {
             # Call handle_request for this AuthBy
             my ($rc, $reason) = $authby->handle_request($op, $rp);


             ......


hope that helps

regards

Hugh



On 20 Aug 2009, at 03:02, Johnson, Neil M wrote:

>
> Question from a PERL/Radiator newbie.
>
> I would like to make a database query  inside of a ReplyHook. I can  
> successfully open the database connection in the StartupHook, but  
> how to I pass the connection to the ReplyHook to peform the query ?  
> Or do I have to open the database connection every time I call the  
> ReplyHook.
>
> Code snippets below:
>
> Thanks.
>
>
> Code in startuphook.pl
>
> # -*- mode: Perl -*-
> use DBI;
>
> sub
> {
> #    my $rp = ${$_[1]};
> #    my $p = ${$_[2]};
>
> 	# Constants
> 	my $DHCP_DB_DSN 	= "DBI:ODBC:dhcp";
> 	my $DHCP_DB_USER 	= "poolupdater";
> 	my $DHCP_DB_PASSWORD = "secretsauce";
> 	
> 	# Variables
> 	my $dhcp_dbh;
> 	my $dhcp_query_sth;
>
>    &main::log($main::LOG_DEBUG, "StartupHook called.");
> 	
> 	$dhcp_dbh = DBI->connect($DHCP_DB_DSN, $DHCP_DB_USER,  
> $DHCP_DB_PASSWORD)
> 		|| die "$DBI::errstr";
> 		
> 	&main::log($main::LOG_DEBUG, "StartupHook - DatbaseConnected");
> 	
> 	$dhcp_query_sth = $dhcp_dbh->prepare(
> 	  qq{ SELECT * from pools; }
> 	);
> 	
> return;
> }
>
> Code in ReplyHook.pl
> sub
> {
>    my $rp = ${$_[1]};
>    my $p = ${$_[2]};
>
>    my $ASCIIvlan;
>    my $binaryvlan = 830;
> 	
>    &main::log($main::LOG_DEBUG, "ReplyHook VLAN rewriter called.");
> 	$ASCIIvlan = $rp->get_attr('Tunnel-Private-Group-ID');
> 	# check for attribute tag
> 	if ($ASCIIvlan =~ /^(d+):(.*)/)
> 	{
> 			# tagged attribute
> 			&main::log($main::LOG_DEBUG, "Found tagged ASCII VLAN attribute  
> of $ASCIIvlan");
> 	}
> 	else
> 	{
> 			# untagged attribute
> 			&main::log($main::LOG_DEBUG, "Found untagged ASCII VLAN attribute  
> of $ASCIIvlan");
> 	}
>
> 	# Replace attribute
> 	
>               # Query DHCP database to determine pool with most  
> available leases
>
> 	$dhcp_query_sth = $execute();
> 	
> 	&main::log($main::LOG_DEBUG, "Replacing ASCII vlan tag with  
> $binaryvlan");
> 	$rp->change_attr('Tunnel-Private-Group-ID', $binaryvlan);
> return;
> }
>
> Thanks.
>
> -- 
> Neil Johnson
> Network Engineer
> Information Technology Services
> The University of Iowa
> Work: 319 384-0938
> Mobile: 319 540-2081
> Fax: 319 355-2618
> E-mail: neil-johnson at uiowa.edu
>
>
> _______________________________________________
> radiator mailing list
> radiator at open.com.au
> http://www.open.com.au/mailman/listinfo/radiator



NB:

Have you read the reference manual ("doc/ref.html")?
Have you searched the mailing list archive (www.open.com.au/archives/radiator)?
Have you had a quick look on Google (www.google.com)?
Have you included a copy of your configuration file (no secrets),
together with a trace 4 debug showing what is happening?
Have you checked the RadiusExpert wiki:
http://www.open.com.au/wiki/index.php/Main_Page

-- 
Radiator: the most portable, flexible and configurable RADIUS server
anywhere. Available on *NIX, *BSD, Windows, MacOS X.
Includes support for reliable RADIUS transport (RadSec),
and DIAMETER translation agent.
-
Nets: internetwork inventory and management - graphical, extensible,
flexible with hardware, software, platform and database independence.
-
CATool: Private Certificate Authority for Unix and Unix-like systems.




More information about the radiator mailing list