-
Notifications
You must be signed in to change notification settings - Fork 17
/
privacyidea_radius.pm
730 lines (619 loc) · 25.4 KB
/
privacyidea_radius.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#
# privacyIDEA FreeRADIUS plugin
# 2021-07-23 Cornelius Kölbel <[email protected]>
# URL encode parameters
# 2020-09-09 Cornelius Kölbel <[email protected]>
# Add Packet-Src-IP-Address as fallback for client IP.
# 2020-03-21 Cornelius Kölbel <[email protected]>
# Add ADD_EMPTY_PASS to send an empty password to
# privacyIDEA in case no password is given.
# Allow config section to have different modules
# with different config files.
# 2019-03-17 Cornelius Kölbel <[email protected]>
# Add password splitting
# 2018-01-12 Cornelius Kölbel <[email protected]>
# Substrings of multivalue user attributes can be added
# to the RADIUS response.
# 2017-10-16 Cornelius Kölbel <[email protected]>
# Add Calling-Station-Id
# 2016-09-30 Cornelius Kölbel <[email protected]>
# Add attribute mapping
# 2016-08-13 Cornelius Kölbel <[email protected]>
# Add user-agent to be displayed in
# privacyIDEA Client Applicaton Type
# 2015-10-10 Cornelius Kölbel <[email protected]>
# Add privacyIDEA-Serial to the response.
# 2015-10-09 Cornelius Kölbel <[email protected]>
# Improve the reading of the config file.
# 2015-09-25 Cornelius Kölbel <[email protected]>
# Add the possibility to read config from
# /etc/privacyidea/rlm_perl.ini
# 2015-06-10 Cornelius Kölbel <[email protected]>
# Add using of Stripped-User-Name and Realm from the
# RAD_REQUEST
# 2015-04-10 Cornelius Kölbel <[email protected]>
# fix typo in log
# 2015-02-25 cornelius kölbel <[email protected]>
# remove the usage of simplecheck and use /validate/check
# 2014-06-25 Cornelius Kölbel
# changed the used modules from Config::Files to Config::IniFile
# to make it easily run on CentOS with EPEL, without CPAN
#
# Copyright (C) 2010 - 2014 LSE Leading Security Experts GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Copyright 2002 The FreeRADIUS server project
# Copyright 2002 Boian Jordanov <[email protected]>
# Copyright 2011 LSE Leading Security Experts GmbH
#
# E-mail: [email protected]
# Contact: www.linotp.org
# Support: www.lsexperts.de
#
# Based on the Example code for use with rlm_perl
#
#
=head1 NAME
freeradius_perl - Perl module for use with FreeRADIUS rlm_perl, to authenticate against
LinOTP http://www.linotp.org
privacyIDEA http://www.privacyidea.org
=head1 SYNOPSIS
use with freeradius:
Configure rlm_perl to work with privacyIDEA:
in /etc/freeradius/users
set:
DEFAULT Auth-type := perl
in /etc/freeradius/modules/perl
point
perl {
module =
to this file
in /etc/freeradius/sites-enabled/<yoursite>
set
authenticate{
perl
[....]
=head1 DESCRIPTION
This module enables freeradius to authenticate using privacyIDEA or LinOTP.
TODO:
* checking of server certificate
=head2 Methods
* authenticate
=head1 CONFIGURATION
The authentication request with its URL and default LinOTP/privacyIDEA Realm
could be defined in a dedicated configuration file, which is expected to be:
/opt/privacyIDEA/rlm_perl.ini
This configuration file could contain default definition for URL and REALM like
[Default]
URL = http://192.168.56.1:5001/validate/check
REALM =
But as well could contain "Access-Type" specific configurations, e.g. for the
Access-Type 'scope1', this would look like:
[Default]
URL = https://localhost/validate/check
REALM =
CLIENTATTRIBUTE = Calling-Station-Id
[scope1]
URL = http://192.168.56.1:5001/validate/check
REALM = mydefault
=head1 AUTHOR
Cornelius Koelbel ([email protected])
Cornelius Koelbel ([email protected])
=head1 COPYRIGHT
Copyright 2013, 2014
This library is free software; you can redistribute it
under the GPLv2.
=head1 SEE ALSO
perl(1).
=cut
use strict;
use LWP 6;
use Config::IniFiles;
use Data::Dump;
use Try::Tiny;
use JSON;
use Time::HiRes qw( gettimeofday tv_interval );
use URI::Encode;
use Encode::Guess;
# use ...
# This is very important ! Without this script will not get the filled hashes from main.
use vars qw(%RAD_REQUEST %RAD_REPLY %RAD_CHECK %RAD_CONFIG %RAD_PERLCONF);
# This is hash wich hold original request from radius
#my %RAD_REQUEST;
# In this hash you add values that will be returned to NAS.
#my %RAD_REPLY;
#This is for check items
#my %RAD_CHECK;
# constant definition for the remapping of return values
use constant RLM_MODULE_REJECT => 0; # /* immediately reject the request */
use constant RLM_MODULE_FAIL => 1; # /* module failed, don't reply */
use constant RLM_MODULE_OK => 2; # /* the module is OK, continue */
use constant RLM_MODULE_HANDLED => 3; # /* the module handled the request, so stop. */
use constant RLM_MODULE_INVALID => 4; # /* the module considers the request invalid. */
use constant RLM_MODULE_USERLOCK => 5; # /* reject the request (user is locked out) */
use constant RLM_MODULE_NOTFOUND => 6; # /* user not found */
use constant RLM_MODULE_NOOP => 7; # /* module succeeded without doing anything */
use constant RLM_MODULE_UPDATED => 8; # /* OK (pairs modified) */
use constant RLM_MODULE_NUMCODES => 9; # /* How many return codes there are */
our $ret_hash = {
0 => "RLM_MODULE_REJECT",
1 => "RLM_MODULE_FAIL",
2 => "RLM_MODULE_OK",
3 => "RLM_MODULE_HANDLED",
4 => "RLM_MODULE_INVALID",
5 => "RLM_MODULE_USERLOCK",
6 => "RLM_MODULE_NOTFOUND",
7 => "RLM_MODULE_NOOP",
8 => "RLM_MODULE_UPDATED",
9 => "RLM_MODULE_NUMCODES"
};
## constant definition for comparison
use constant false => 0;
use constant true => 1;
## constant definitions for logging
use constant Debug => 1;
use constant Auth => 2;
use constant Info => 3;
use constant Error => 4;
use constant Proxy => 5;
use constant Acct => 6;
# You can configure, which config file to use in the perl module definition:
# perl privacyIDEA-A {
# filename = /usr/share/privacyidea/freeradius/privacyidea_radius.pm
# config {
# configfile = /etc/privacyidea/rlm_perl-A.ini
# }
# }
our $CONFIG_FILE = $RAD_PERLCONF{'configfile'};
our @CONFIG_FILES = ("/etc/privacyidea/rlm_perl.ini", "/etc/freeradius/rlm_perl.ini", "/opt/privacyIDEA/rlm_perl.ini");
our $Config = {};
our $Mapping = {};
our $cfg_file;
$Config->{FSTAT} = "not found!";
$Config->{URL} = 'https://127.0.0.1/validate/check';
$Config->{REALM} = '';
$Config->{CLIENTATTRIBUTE} = '';
$Config->{RESCONF} = "";
$Config->{Debug} = "FALSE";
$Config->{SSL_CHECK} = "FALSE";
$Config->{TIMEOUT} = 10;
$Config->{SPLIT_NULL_BYTE} = "FALSE";
$Config->{ADD_EMPTY_PASS} = "FALSE";
if ($CONFIG_FILE) {
@CONFIG_FILES = ($CONFIG_FILE);
}
foreach my $file (@CONFIG_FILES) {
if (( -e $file )) {
$cfg_file = Config::IniFiles->new( -file => $file);
$CONFIG_FILE = $file;
$Config->{FSTAT} = "found!";
$Config->{URL} = $cfg_file->val("Default", "URL");
$Config->{REALM} = $cfg_file->val("Default", "REALM");
$Config->{RESCONF} = $cfg_file->val("Default", "RESCONF");
$Config->{Debug} = $cfg_file->val("Default", "DEBUG");
$Config->{SPLIT_NULL_BYTE} = $cfg_file->val("Default", "SPLIT_NULL_BYTE");
$Config->{ADD_EMPTY_PASS} = $cfg_file->val("Default", "ADD_EMPTY_PASS");
$Config->{SSL_CHECK} = $cfg_file->val("Default", "SSL_CHECK");
$Config->{SSL_CA_PATH} = $cfg_file->val("Default", "SSL_CA_PATH");
$Config->{TIMEOUT} = $cfg_file->val("Default", "TIMEOUT", 10);
$Config->{CLIENTATTRIBUTE} = $cfg_file->val("Default", "CLIENTATTRIBUTE");
}
}
sub add_reply_attibute {
my $radReply = shift;
my $newValue = shift;
if (ref($radReply) eq "ARRAY") {
# This is an array, there is already a value to this replyAttribute
#&radiusd::radlog( Info, "Adding $newValue to the Reply Attribute, being an array.\n");
push @$radReply, $newValue;
} else {
# This is an empty replyValue, we add the first value
#&radiusd::radlog( Info, "Adding $newValue to the Reply Attribute, being a string.\n");
$radReply = [$newValue];
}
return $radReply;
}
sub mapResponse {
# This function maps the Mapping sections in rlm_perl.ini
# to RADIUS Attributes.
my $decoded = shift;
my %radReply;
my $topnode;
if ($cfg_file) {
foreach my $group ($cfg_file->Groups) {
&radiusd::radlog( Info, "++++ Parsing group: $group\n");
foreach my $member ($cfg_file->GroupMembers($group)) {
&radiusd::radlog(Info, "+++++ Found member '$member'");
$member =~/(.*)\ (.*)/;
$topnode = $2;
if ($group eq "Mapping") {
foreach my $key ($cfg_file->Parameters($member)){
my $radiusAttribute = $cfg_file->val($member, $key);
&radiusd::radlog( Info, "++++++ Map: $topnode : $key -> $radiusAttribute");
my $newValue = $decoded->{detail}{$topnode}{$key};
$radReply{$radiusAttribute} = add_reply_attibute($radReply{$radiusAttribute}, $newValue);
};
}
if ($group eq "Attribute") {
my $radiusAttribute = $topnode;
# opional overwrite radiusAttribute
my $ra = $cfg_file->val($member, "radiusAttribute");
if ($ra ne "") {
$radiusAttribute = $ra;
}
my $userAttribute = $cfg_file->val($member, "userAttribute");
my $regex = $cfg_file->val($member, "regex");
my $directory = $cfg_file->val($member, "dir");
my $prefix = $cfg_file->val($member, "prefix");
my $suffix = $cfg_file->val($member, "suffix");
&radiusd::radlog( Info, "++++++ Attribute: IF '$directory'->'$userAttribute' == '$regex' THEN '$radiusAttribute'");
my $attributevalue="";
if ($directory eq "") {
$attributevalue = $decoded->{detail}{$userAttribute};
&radiusd::radlog( Info, "++++++ no directory");
} else {
$attributevalue = $decoded->{detail}{$directory}{$userAttribute};
&radiusd::radlog( Info, "++++++ searching in directory $directory");
}
my @values = ();
if (ref($attributevalue) eq "") {
&radiusd::radlog(Info, "+++++++ User attribute is a string: $attributevalue");
push(@values, $attributevalue);
}
if (ref($attributevalue) eq "ARRAY") {
&radiusd::radlog(Info, "+++++++ User attribute is a list: $attributevalue");
@values = @$attributevalue;
}
foreach my $value (@values) {
&radiusd::radlog(Info, "+++++++ trying to match $value");
if ($value =~ /$regex/) {
my $result = $1;
$radReply{$radiusAttribute} = add_reply_attibute($radReply{$radiusAttribute}, "$prefix$result$suffix");
&radiusd::radlog(Info, "++++++++ Result: Add RADIUS attribute $radiusAttribute = $prefix$result$suffix");
} else {
&radiusd::radlog(Info, "++++++++ Result: No match, no RADIUS attribute $radiusAttribute added.");
}
}
}
}
}
foreach my $key ($cfg_file->Parameters("Mapping")) {
my $radiusAttribute = $cfg_file->val("Mapping", $key);
&radiusd::radlog( Info, "+++ Map: $key -> $radiusAttribute");
$radReply{$radiusAttribute} = add_reply_attibute($radReply{$radiusAttribute}, $decoded->{detail}{$key});
}
}
return %radReply;
}
# Function to handle authenticate
sub authenticate {
## show where the config comes from -
# in the module init we can't print this out, so it starts here
&radiusd::radlog( Info, "Config File $CONFIG_FILE ".$Config->{FSTAT} );
# we inherrit the defaults
my $URL = $Config->{URL};
my $REALM = $Config->{REALM};
my $RESCONF = $Config->{RESCONF};
my $SSL_CA_PATH = $Config->{SSL_CA_PATH};
my $debug = false;
if ( $Config->{Debug} =~ /true/i ) {
$debug = true;
}
&radiusd::radlog( Info, "Debugging config: ". $Config->{Debug});
my $check_ssl = false;
if ( $Config->{SSL_CHECK} =~ /true/i ) {
$check_ssl = true;
}
&radiusd::radlog( Info, "Verifying SSL certificate: ". $Config->{SSL_CHECK} );
my $timeout = $Config->{TIMEOUT};
&radiusd::radlog( Info, "Default URL $URL " );
# if there exists an auth-type config may overwrite this
my $auth_type = $RAD_CONFIG{"Auth-Type"};
try {
&radiusd::radlog( Info, "Looking for config for auth-type $auth_type");
if ( ( $cfg_file->val( $auth_type, "URL") )) {
$URL = $cfg_file->val( $auth_type, "URL" );
}
if ( ( $cfg_file->val( $auth_type, "REALM") )) {
$REALM = $cfg_file->val( $auth_type, "REALM" );
}
if ( ( $cfg_file->val( $auth_type, "RESCONF") )) {
$RESCONF = $cfg_file->val( $auth_type, "RESCONF" );
}
}
catch {
&radiusd::radlog( Info, "Warning: $@" );
};
if ( $debug == true ) {
&log_request_attributes;
}
my %params = ();
# put RAD_REQUEST members in the privacyIDEA request
if ( exists( $RAD_REQUEST{'State'} ) ) {
my $hexState = $RAD_REQUEST{'State'};
if ( substr( $hexState, 0, 2 ) eq "0x" ) {
$hexState = substr( $hexState, 2 );
}
$params{'state'} = pack 'H*', $hexState;
}
if ( exists( $RAD_REQUEST{'User-Name'} ) ) {
$params{"user"} = $RAD_REQUEST{'User-Name'};
}
if ( exists( $RAD_REQUEST{'Stripped-User-Name'} )) {
$params{"user"} = $RAD_REQUEST{'Stripped-User-Name'};
}
if ( exists( $RAD_REQUEST{'User-Password'} ) ) {
my $password = $RAD_REQUEST{'User-Password'};
if ( $Config->{SPLIT_NULL_BYTE} =~ /true/i ) {
my @p = split(/\0/, $password);
$password = @p[0];
}
# Decode password (from <https://perldoc.perl.org/Encode::Guess#Encode::Guess-%3Eguess($data)>)
my $decoder = Encode::Guess->guess($password);
if ( ! ref($decoder) ) {
radiusd::radlog( Info, "Could not find valid password encoding. Sending password as-is." );
radiusd::radlog( Debug, $decoder );
} else {
&radiusd::radlog( Info, "Password encoding guessed: " . $decoder->name);
$password = $decoder->decode($password);
}
$params{"pass"} = $password;
} elsif ( $Config->{ADD_EMPTY_PASS} =~ /true/i ) {
$params{"pass"} = "";
}
# We need to decode the username as well since it might contain special chars
if ( exists( $params{"user"} ) ) {
my $decoder = Encode::Guess->guess($params{"user"});
if ( ! ref($decoder) ) {
radiusd::radlog( Info, "Could not find valid username encoding. Sending username as-is." );
radiusd::radlog( Debug, $decoder );
} else {
&radiusd::radlog( Info, "Username encoding guessed: " . $decoder->name);
$params{"user"} = $decoder->decode($params{"user"});
}
}
# Security enhancement sned Message-Authenticator back
if ( exists( $RAD_REQUEST{'Message-Authenticator'} )) {
$RAD_REPLY{'Message-Authenticator'} = $RAD_REQUEST{'Message-Authenticator'};
}
# URL encode username and password
my $uri = URI::Encode->new( { encode_reserved => 0 } );
$params{"user"} = $uri->encode($params{"user"});
$params{"pass"} = $uri->encode($params{"pass"});
if ( exists( $RAD_REQUEST{'NAS-IP-Address'} ) ) {
$params{"client"} = $RAD_REQUEST{'NAS-IP-Address'};
&radiusd::radlog( Info, "Setting client IP to $params{'client'}." );
} elsif ( exists( $RAD_REQUEST{'Packet-Src-IP-Address'} ) ) {
$params{"client"} = $RAD_REQUEST{'Packet-Src-IP-Address'};
&radiusd::radlog( Info, "Setting client IP to $params{'client'}." );
}
if (exists ( $Config->{CLIENTATTRIBUTE} ) ) {
if ( exists( $RAD_REQUEST{$Config->{CLIENTATTRIBUTE}} ) ) {
$params{"client"} = $RAD_REQUEST{$Config->{CLIENTATTRIBUTE}};
&radiusd::radlog( Info, "Setting client IP to $params{'client'}." );
}
}
if ( length($REALM) > 0 ) {
$params{"realm"} = $REALM;
} elsif ( length($RAD_REQUEST{'Realm'}) > 0 ) {
$params{"realm"} = $RAD_REQUEST{'Realm'};
}
if ( length($RESCONF) > 0 ) {
$params{"resConf"} = $RESCONF;
}
&radiusd::radlog( Info, "Auth-Type: $auth_type" );
&radiusd::radlog( Info, "url: $URL" );
&radiusd::radlog( Info, "user sent to privacyidea: $params{'user'}" );
&radiusd::radlog( Info, "realm sent to privacyidea: $params{'realm'}" );
&radiusd::radlog( Info, "resolver sent to privacyidea: $params{'resConf'}" );
&radiusd::radlog( Info, "client sent to privacyidea: $params{'client'}" );
&radiusd::radlog( Info, "state sent to privacyidea: $params{'state'}" );
if ( $debug == true ) {
&radiusd::radlog( Debug, "urlparam $_ = $params{$_}\n" )
for ( keys %params );
}
else {
&radiusd::radlog( Info, "urlparam $_ \n" ) for ( keys %params );
}
my $ua = LWP::UserAgent->new();
$ua->env_proxy;
$ua->timeout($timeout);
&radiusd::radlog( Info, "Request timeout: $timeout " );
# Set the user-agent to be fetched in privacyIDEA Client Application Type
$ua->agent("FreeRADIUS");
if ($check_ssl == false) {
try {
# This is only availble with LWP version 6
&radiusd::radlog( Info, "Not verifying SSL certificate!" );
$ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00 );
} catch {
&radiusd::radlog( Error, "ssl_opts only supported with LWP 6. error: $_" );
}
} else {
try {
&radiusd::radlog( Info, "Verifying SSL certificate!" );
if ( exists( $Config->{SSL_CA_PATH} ) ) {
if ( length $SSL_CA_PATH ) {
&radiusd::radlog( Info, "SSL_CA_PATH: $SSL_CA_PATH" );
$ua->ssl_opts(
SSL_ca_path => $SSL_CA_PATH,
verify_hostname => 1
);
}
else {
&radiusd::radlog( Info,
"Verifying SSL certificate against system wide CAs!" );
$ua->ssl_opts( verify_hostname => 1 );
}
}
}
catch {
&radiusd::radlog( Error,
"Something went wrong setting up SSL certificate verification: $_" );
}
}
my $starttime = [gettimeofday];
my $response = $ua->post( $URL, \%params );
my $content = $response->decoded_content();
my $elapsedtime = tv_interval($starttime);
&radiusd::radlog( Info, "elapsed time for privacyidea call: $elapsedtime" );
if ( $debug == true ) {
&radiusd::radlog( Debug, "Content $content" );
}
$RAD_REPLY{'Reply-Message'} = "privacyIDEA server denied access!";
my $g_return = RLM_MODULE_REJECT;
if ( !$response->is_success ) {
# This was NO OK 200 response
my $status = $response->status_line;
&radiusd::radlog( Info, "privacyIDEA request failed: $status" );
$RAD_REPLY{'Reply-Message'} = "privacyIDEA request failed: $status";
$g_return = RLM_MODULE_FAIL;
}
try {
my $coder = JSON->new->ascii->pretty->allow_nonref;
my $decoded = $coder->decode($content);
my $message = $decoded->{detail}{message};
if ( $decoded->{result}{value} ) {
&radiusd::radlog( Info, "privacyIDEA access granted for $params{'user'} realm='$params{'realm'}'" );
$RAD_REPLY{'Reply-Message'} = "privacyIDEA access granted";
# Add the response hash to the Radius Reply
%RAD_REPLY = ( %RAD_REPLY, mapResponse($decoded));
$g_return = RLM_MODULE_OK;
}
elsif ( $decoded->{result}{status} ) {
&radiusd::radlog( Info, "privacyIDEA Result status is true!" );
$RAD_REPLY{'Reply-Message'} = $decoded->{detail}{message};
if ( $decoded->{detail}{transaction_id} ) {
## we are in challenge response mode:
## 1. split the response in fail, state and challenge
## 2. show the client the challenge and the state
## 3. get the response and
## 4. submit the response and the state to linotp and
## 5. reply ok or reject
$RAD_REPLY{'State'} = $decoded->{detail}{transaction_id};
$RAD_CHECK{'Response-Packet-Type'} = "Access-Challenge";
# Add the response hash to the Radius Reply
%RAD_REPLY = ( %RAD_REPLY, mapResponse($decoded));
$g_return = RLM_MODULE_HANDLED;
} else {
&radiusd::radlog( Info, "privacyIDEA access denied for $params{'user'} realm='$params{'realm'}'" );
#$RAD_REPLY{'Reply-Message'} = "privacyIDEA access denied";
$g_return = RLM_MODULE_REJECT;
}
}
elsif ( !$decoded->{result}{status}) {
# An internal error occurred. We use the original return value RLM_MODULE_FAIL
&radiusd::radlog( Info, "privacyIDEA Result status is false!" );
$RAD_REPLY{'Reply-Message'} = $decoded->{result}{error}{message};
&radiusd::radlog( Info, $decoded->{result}{error}{message});
my $errorcode = $decoded->{result}{error}{code};
if ($errorcode == 904) {
$g_return = RLM_MODULE_NOTFOUND;
} else {
$g_return = RLM_MODULE_FAIL;
}
&radiusd::radlog( Info, "privacyIDEA failed to handle the request" );
}
} catch {
my $e = shift;
&radiusd::radlog( Info, "$e");
&radiusd::radlog( Info, "Can not parse response from privacyIDEA." );
};
&radiusd::radlog( Info, "return $ret_hash->{$g_return}" );
return $g_return;
}
sub log_request_attributes {
# This shouldn't be done in production environments!
# This is only meant for debugging!
for ( keys %RAD_REQUEST ) {
&radiusd::radlog( Debug, "RAD_REQUEST: $_ = $RAD_REQUEST{$_}" );
}
}
# Function to handle authorize
sub authorize {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle preacct
sub preacct {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle accounting
sub accounting {
# For debugging purposes only
# &log_request_attributes;
# You can call another subroutine from here
&test_call;
return RLM_MODULE_OK;
}
# Function to handle checksimul
sub checksimul {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle pre_proxy
sub pre_proxy {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle post_proxy
sub post_proxy {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle post_auth
sub post_auth {
# For debugging purposes only
# &log_request_attributes;
return RLM_MODULE_OK;
}
# Function to handle xlat
sub xlat {
# For debugging purposes only
# &log_request_attributes;
# Loads some external perl and evaluate it
my ( $filename, $a, $b, $c, $d ) = @_;
&radiusd::radlog( 1, "From xlat $filename " );
&radiusd::radlog( 1, "From xlat $a $b $c $d " );
local *FH;
open FH, $filename or die "open '$filename' $!";
local ($/) = undef;
my $sub = <FH>;
close FH;
my $eval = qq{ sub handler{ $sub;} };
eval $eval;
eval { main->handler; };
}
# Function to handle detach
sub detach {
# For debugging purposes only
# &log_request_attributes;
# Do some logging.
&radiusd::radlog( 0, "rlm_perl::Detaching. Reloading. Done." );
}
#
# Some functions that can be called from other functions
#
sub test_call {
# Some code goes here
}
1;