| 1 |
|
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 16 |
# |
| 17 |
# Copyright 2002 The FreeRADIUS server project |
| 18 |
# Copyright 2002 Boian Jordanov <[email protected]> |
| 19 |
# |
| 20 |
|
| 21 |
# |
| 22 |
# Example code for use with rlm_perl |
| 23 |
# |
| 24 |
# You can use every module that comes with your perl distribution! |
| 25 |
# |
| 26 |
# If you are using DBI and do some queries to DB, please be sure to |
| 27 |
# use the CLONE function to initialize the DBI connection to DB. |
| 28 |
# |
| 29 |
|
| 30 |
use strict; |
| 31 |
use warnings; |
| 32 |
|
| 33 |
# use ... |
| 34 |
use Data::Dumper; |
| 35 |
|
| 36 |
# Bring the global hashes into the package scope |
| 37 |
our (%RAD_REQUEST, %RAD_REPLY, %RAD_CHECK, %RAD_STATE, %RAD_PERLCONF); |
| 38 |
|
| 39 |
# This is hash wich hold original request from radius |
| 40 |
#my %RAD_REQUEST; |
| 41 |
# In this hash you add values that will be returned to NAS. |
| 42 |
#my %RAD_REPLY; |
| 43 |
#This is for check items |
| 44 |
#my %RAD_CHECK; |
| 45 |
# This is the session-sate |
| 46 |
#my %RAD_STATE; |
| 47 |
# This is configuration items from "config" perl module configuration section |
| 48 |
#my %RAD_PERLCONF; |
| 49 |
|
| 50 |
# Multi-value attributes are mapped to perl arrayrefs. |
| 51 |
# |
| 52 |
# update request { |
| 53 |
# Filter-Id := 'foo' |
| 54 |
# Filter-Id += 'bar' |
| 55 |
# } |
| 56 |
# |
| 57 |
# This results to the following entry in %RAD_REQUEST: |
| 58 |
# |
| 59 |
# $RAD_REQUEST{'Filter-Id'} = [ 'foo', 'bar' ]; |
| 60 |
# |
| 61 |
# Likewise, you can assign an arrayref to return multi-value attributes |
| 62 |
|
| 63 |
# |
| 64 |
# This the remapping of return values |
| 65 |
# |
| 66 |
use constant { |
| 67 |
RLM_MODULE_REJECT => 0, # immediately reject the request |
| 68 |
RLM_MODULE_OK => 2, # the module is OK, continue |
| 69 |
RLM_MODULE_HANDLED => 3, # the module handled the request, so stop |
| 70 |
RLM_MODULE_INVALID => 4, # the module considers the request invalid |
| 71 |
RLM_MODULE_USERLOCK => 5, # reject the request (user is locked out) |
| 72 |
RLM_MODULE_NOTFOUND => 6, # user not found |
| 73 |
RLM_MODULE_NOOP => 7, # module succeeded without doing anything |
| 74 |
RLM_MODULE_UPDATED => 8, # OK (pairs modified) |
| 75 |
RLM_MODULE_NUMCODES => 9 # How many return codes there are |
| 76 |
}; |
| 77 |
|
| 78 |
# Same as src/include/log.h |
| 79 |
use constant { |
| 80 |
L_AUTH => 2, # Authentication message |
| 81 |
L_INFO => 3, # Informational message |
| 82 |
L_ERR => 4, # Error message |
| 83 |
L_WARN => 5, # Warning |
| 84 |
L_PROXY => 6, # Proxy messages |
| 85 |
L_ACCT => 7, # Accounting messages |
| 86 |
L_DBG => 16, # Only displayed when debugging is enabled |
| 87 |
L_DBG_WARN => 17, # Warning only displayed when debugging is enabled |
| 88 |
L_DBG_ERR => 18, # Error only displayed when debugging is enabled |
| 89 |
L_DBG_WARN_REQ => 19, # Less severe warning only displayed when debugging is enabled |
| 90 |
L_DBG_ERR_REQ => 20, # Less severe error only displayed when debugging is enabled |
| 91 |
}; |
| 92 |
|
| 93 |
# Global variables can persist across different calls to the module. |
| 94 |
# |
| 95 |
# |
| 96 |
# { |
| 97 |
# my %static_global_hash = (); |
| 98 |
# |
| 99 |
# sub post_auth { |
| 100 |
# ... |
| 101 |
# } |
| 102 |
# ... |
| 103 |
# } |
| 104 |
|
| 105 |
|
| 106 |
# Function to handle authorize |
| 107 |
sub authorize { |
| 108 |
# For debugging purposes only |
| 109 |
# &log_request_attributes; |
| 110 |
|
| 111 |
# Here's where your authorization code comes |
| 112 |
# You can call another function from here: |
| 113 |
&test_call; |
| 114 |
|
| 115 |
return RLM_MODULE_OK; |
| 116 |
} |
| 117 |
|
| 118 |
# Function to handle authenticate |
| 119 |
sub authenticate { |
| 120 |
# For debugging purposes only |
| 121 |
# &log_request_attributes; |
| 122 |
|
| 123 |
if ($RAD_REQUEST{'User-Name'} =~ /^baduser/i) { |
| 124 |
# Reject user and tell him why |
| 125 |
$RAD_REPLY{'Reply-Message'} = "Denied access by rlm_perl function"; |
| 126 |
return RLM_MODULE_REJECT; |
| 127 |
} else { |
| 128 |
# Accept user and set some attribute |
| 129 |
if (&radiusd::xlat("%{client:group}") eq 'UltraAllInclusive') { |
| 130 |
# User called from NAS with unlim plan set, set higher limits |
| 131 |
$RAD_REPLY{'h323-credit-amount'} = "1000000"; |
| 132 |
} else { |
| 133 |
$RAD_REPLY{'h323-credit-amount'} = "100"; |
| 134 |
} |
| 135 |
return RLM_MODULE_OK; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
# Function to handle preacct |
| 140 |
sub preacct { |
| 141 |
# For debugging purposes only |
| 142 |
# &log_request_attributes; |
| 143 |
|
| 144 |
return RLM_MODULE_OK; |
| 145 |
} |
| 146 |
|
| 147 |
# Function to handle accounting |
| 148 |
sub accounting { |
| 149 |
# For debugging purposes only |
| 150 |
# &log_request_attributes; |
| 151 |
|
| 152 |
# You can call another subroutine from here |
| 153 |
&test_call; |
| 154 |
|
| 155 |
return RLM_MODULE_OK; |
| 156 |
} |
| 157 |
|
| 158 |
# Function to handle checksimul |
| 159 |
sub checksimul { |
| 160 |
# For debugging purposes only |
| 161 |
# &log_request_attributes; |
| 162 |
|
| 163 |
return RLM_MODULE_OK; |
| 164 |
} |
| 165 |
|
| 166 |
# Function to handle pre_proxy |
| 167 |
sub pre_proxy { |
| 168 |
# For debugging purposes only |
| 169 |
# &log_request_attributes; |
| 170 |
|
| 171 |
return RLM_MODULE_OK; |
| 172 |
} |
| 173 |
|
| 174 |
# Function to handle post_proxy |
| 175 |
sub post_proxy { |
| 176 |
# For debugging purposes only |
| 177 |
# &log_request_attributes; |
| 178 |
|
| 179 |
return RLM_MODULE_OK; |
| 180 |
} |
| 181 |
|
| 182 |
# Function to handle post_auth |
| 183 |
sub post_auth { |
| 184 |
# For debugging purposes only |
| 185 |
# &log_request_attributes; |
| 186 |
|
| 187 |
return RLM_MODULE_OK; |
| 188 |
} |
| 189 |
|
| 190 |
# Function to handle xlat |
| 191 |
sub xlat { |
| 192 |
# For debugging purposes only |
| 193 |
# &log_request_attributes; |
| 194 |
|
| 195 |
# Loads some external perl and evaluate it |
| 196 |
my ($filename,$a,$b,$c,$d) = @_; |
| 197 |
&radiusd::radlog(L_DBG, "From xlat $filename "); |
| 198 |
&radiusd::radlog(L_DBG,"From xlat $a $b $c $d "); |
| 199 |
local *FH; |
| 200 |
open FH, $filename or die "open '$filename' $!"; |
| 201 |
local($/) = undef; |
| 202 |
my $sub = <FH>; |
| 203 |
close FH; |
| 204 |
my $eval = qq{ sub handler{ $sub;} }; |
| 205 |
eval $eval; |
| 206 |
eval {main->handler;}; |
| 207 |
} |
| 208 |
|
| 209 |
# Function to handle detach |
| 210 |
sub detach { |
| 211 |
# For debugging purposes only |
| 212 |
# &log_request_attributes; |
| 213 |
} |
| 214 |
|
| 215 |
# |
| 216 |
# Some functions that can be called from other functions |
| 217 |
# |
| 218 |
|
| 219 |
sub test_call { |
| 220 |
# Some code goes here |
| 221 |
} |
| 222 |
|
| 223 |
sub log_request_attributes { |
| 224 |
# This shouldn't be done in production environments! |
| 225 |
# This is only meant for debugging! |
| 226 |
for (keys %RAD_REQUEST) { |
| 227 |
&radiusd::radlog(L_DBG, "RAD_REQUEST: $_ = $RAD_REQUEST{$_}"); |
| 228 |
} |
| 229 |
} |
| 230 |
|