-
Notifications
You must be signed in to change notification settings - Fork 4
/
radius.class.php
849 lines (749 loc) · 33.7 KB
/
radius.class.php
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
/*********************************************************************
*
* Pure PHP radius class
*
* This Radius class is a radius client implementation in pure PHP
* following the RFC 2865 rules (http://www.ietf.org/rfc/rfc2865.txt)
*
* This class works with at least the following RADIUS servers:
* - Authenex Strong Authentication System (ASAS) with two-factor authentication
* - FreeRADIUS, a free Radius server implementation for Linux and *nix environments
* - Microsoft Radius server IAS
* - Mideye RADIUS server (http://www.mideye.com)
* - Radl, a free Radius server for Windows
* - RSA SecurID
* - VASCO Middleware 3.0 server
* - WinRadius, Windows Radius server (free for 5 users)
* - ZyXEL ZyWALL OTP (Authenex ASAS branded by ZyXEL, cheaper)
*
*
* LICENCE
*
* Copyright (c) 2008, SysCo systemes de communication sa
* SysCo (tm) is a trademark of SysCo systemes de communication sa
* (http://www.sysco.ch/)
* All rights reserved.
*
* This file is part of the Pure PHP radius class
*
* Pure PHP radius class is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Pure PHP radius class 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Pure PHP radius class.
* If not, see <http://www.gnu.org/licenses/>.
*
*
* @author: SysCo/al
* @since CreationDate: 2008-01-04
* @copyright (c) 2008 by SysCo systemes de communication sa
* @version $LastChangedRevision: 1.2.4 $
* @version $LastChangedDate: 2018-11-03 $
* @version $LastChangedBy: SysCo/al $
* @link $HeadURL: radius.class.php $
* @link http://developer.sysco.ch/php/
* @link developer@sysco.ch
* Language: PHP 4.0.7 or higher
*
*
* Usage
*
* require_once('radius.class.php');
* $radius = new Radius($ip_radius_server = 'radius_server_ip_address', $shared_secret = 'radius_shared_secret'[, $radius_suffix = 'optional_radius_suffix'[, $udp_timeout = udp_timeout_in_seconds[, $authentication_port = 1812]]]);
* $result = $radius->Access_Request($username = 'username', $password = 'password'[, $udp_timeout = udp_timeout_in_seconds]);
*
*
* Examples
*
* Example 1
* <?php
* require_once('radius.class.php');
* $radius = new Radius('127.0.0.1', 'secret');
* $radius->SetNasIpAddress('1.2.3.4'); // Needed for some devices, and not auto_detected if PHP not runned through a web server
* if ($radius->AccessRequest('user', 'pass'))
* {
* echo "Authentication accepted.";
* }
* else
* {
* echo "Authentication rejected.";
* }
* ?>
*
* Example 2
* <?php
* require_once('radius.class.php');
* $radius = new Radius('127.0.0.1', 'secret');
* $radius->SetNasPort(0);
* $radius->SetNasIpAddress('1.2.3.4'); // Needed for some devices, and not auto_detected if PHP not runned through a web server
* if ($radius->AccessRequest('user', 'pass'))
* {
* echo "Authentication accepted.";
* echo "<br />";
* }
* else
* {
* echo "Authentication rejected.";
* echo "<br />";
* }
* echo $radius->GetReadableReceivedAttributes();
* ?>
*
*
* External file needed
*
* none.
*
*
* External file created
*
* none.
*
*
* Special issues
*
* - Sockets support must be enabled.
* * In Linux and *nix environments, the extension is enabled at
* compile time using the --enable-sockets configure option
* * In Windows, PHP Sockets can be activated by un-commenting
* extension=php_sockets.dll in php.ini
*
*
* Other related ressources
*
* FreeRADIUS, a free Radius server implementation for Linux and *nix environments:
* http://www.freeradius.org/
*
* WinRadius, Windows Radius server (free for 5 users):
* http://www.itconsult2000.com/en/product/WinRadius.zip
*
* Radl, a free Radius server for Windows:
* http://www.loriotpro.com/Products/RadiusServer/FreeRadiusServer_EN.php
*
* DOS command line Radius client:
* http://www.itconsult2000.com/en/product/WinRadiusClient.zip
*
*
* Users feedbacks and comments
*
* 2008-07-02 Pim Koeman/Parantion
*
* When using a radius connection behind a linux iptables firewall
* allow port 1812 and 1813 with udp protocol
*
* IPTABLES EXAMPLE (command line):
* iptables -A AlwaysACCEPT -p udp --dport 1812 -j ACCEPT
* iptables -A AlwaysACCEPT -p udp --dport 1813 -j ACCEPT
*
* or put the lines in /etc/sysconfig/iptables (red-hat type systems (fedora, centos, rhel etc.)
* -A AlwaysACCEPT -p udp --dport 1812 -j ACCEPT
* -A AlwaysACCEPT -p udp --dport 1813 -j ACCEPT
*
*
* Change Log
*
* 2018-11-03 1.2.4 SysCo/al __construct instead of the name of the class
* 2018-11-03 1.2.3 SysCo/al Added gplaza correction, Fix parsing IP v6 value
* 2009-01-05 1.2.2 SysCo/al Added Robert Svensson feedback, Mideye RADIUS server is supported
* 2008-11-11 1.2.1 SysCo/al Added Carlo Ferrari resolution in examples (add NAS IP Address for a VASCO Middleware server)
* 2008-07-07 1.2 SysCo/al Added Pim Koeman (Parantion) contribution
* - comments concerning using radius behind a linux iptables firewall
* Added Jon Bright (tick Trading Software AG) contribution
* - false octal encoding with 0xx indexes (indexes are now rewritten in xx only)
* - challenge/response support for the RSA SecurID New-PIN mode
* Added GetRadiusPacketInfo() method
* Added GetAttributesInfo() method
* Added DecodeVendorSpecificContent() (to answer Raul Carvalho's question)
* Added Decoded Vendor Specific Content in debug messages
* 2008-02-04 1.1 SysCo/al Typo error for the udp_timeout parameter (line 256 in the version 1.0)
* 2008-01-07 1.0 SysCo/al Initial release
*
*********************************************************************/
/*********************************************************************
*
* Radius
* Pure PHP radius class
*
* Creation 2018-11-03
* Update 2009-01-05
* @package radius
* @version v.1.2.4
* @author SysCo/al
*
*********************************************************************/
class Radius
{
var $_ip_radius_server; // Radius server IP address
var $_shared_secret; // Shared secret with the radius server
var $_radius_suffix; // Radius suffix (default is '');
var $_udp_timeout; // Timeout of the UDP connection in seconds (default value is 5)
var $_authentication_port; // Authentication port (default value is 1812)
var $_accounting_port; // Accouting port (default value is 1813)
var $_nas_ip_address; // NAS IP address
var $_nas_port; // NAS port
var $_encrypted_password; // Encrypted password, as described in the RFC 2865
var $_user_ip_address; // Remote IP address of the user
var $_request_authenticator; // Request-Authenticator, 16 octets random number
var $_response_authenticator; // Request-Authenticator, 16 octets random number
var $_username; // Username to sent to the Radius server
var $_password; // Password to sent to the Radius server (clear password, must be encrypted)
var $_identifier_to_send; // Identifier field for the packet to be sent
var $_identifier_received; // Identifier field for the received packet
var $_radius_packet_to_send; // Radius packet code (1=Access-Request, 2=Access-Accept, 3=Access-Reject, 4=Accounting-Request, 5=Accounting-Response, 11=Access-Challenge, 12=Status-Server (experimental), 13=Status-Client (experimental), 255=Reserved
var $_radius_packet_received; // Radius packet code (1=Access-Request, 2=Access-Accept, 3=Access-Reject, 4=Accounting-Request, 5=Accounting-Response, 11=Access-Challenge, 12=Status-Server (experimental), 13=Status-Client (experimental), 255=Reserved
var $_attributes_to_send; // Radius attributes to send
var $_attributes_received; // Radius attributes received
var $_socket_to_server; // Socket connection
var $_debug_mode; // Debug mode flag
var $_attributes_info; // Attributes info array
var $_radius_packet_info; // Radius packet codes info array
var $_last_error_code; // Last error code
var $_last_error_message; // Last error message
/*********************************************************************
*
* Name: __construct
* short description: Radius class constructor
*
* Creation 2008-01-04
* Update 2018-11-03
* @version v.1.2.4
* @author SysCo/al
* @param string ip address of the radius server
* @param string shared secret with the radius server
* @param string radius domain name suffix (default is empty)
* @param integer UDP timeout (default is 5)
* @param integer authentication port
* @param integer accounting port
* @return NULL
*********************************************************************/
public function __construct(
$ip_radius_server = '127.0.0.1',
$shared_secret = '',
$radius_suffix = '',
$udp_timeout = 5,
$authentication_port = 1812,
$accounting_port = 1813
) {
$this->_radius_packet_info[1] = 'Access-Request';
$this->_radius_packet_info[2] = 'Access-Accept';
$this->_radius_packet_info[3] = 'Access-Reject';
$this->_radius_packet_info[4] = 'Accounting-Request';
$this->_radius_packet_info[5] = 'Accounting-Response';
$this->_radius_packet_info[11] = 'Access-Challenge';
$this->_radius_packet_info[12] = 'Status-Server (experimental)';
$this->_radius_packet_info[13] = 'Status-Client (experimental)';
$this->_radius_packet_info[255] = 'Reserved';
$this->_attributes_info[1] = array('User-Name', 'S');
$this->_attributes_info[2] = array('User-Password', 'S');
$this->_attributes_info[3] = array('CHAP-Password', 'S'); // Type (1) / Length (1) / CHAP Ident (1) / String
$this->_attributes_info[4] = array('NAS-IP-Address', 'A');
$this->_attributes_info[5] = array('NAS-Port', 'I');
$this->_attributes_info[6] = array('Service-Type', 'I');
$this->_attributes_info[7] = array('Framed-Protocol', 'I');
$this->_attributes_info[8] = array('Framed-IP-Address', 'A');
$this->_attributes_info[9] = array('Framed-IP-Netmask', 'A');
$this->_attributes_info[10] = array('Framed-Routing', 'I');
$this->_attributes_info[11] = array('Filter-Id', 'T');
$this->_attributes_info[12] = array('Framed-MTU', 'I');
$this->_attributes_info[13] = array('Framed-Compression', 'I');
$this->_attributes_info[14] = array( 'Login-IP-Host', 'A');
$this->_attributes_info[15] = array('Login-service', 'I');
$this->_attributes_info[16] = array('Login-TCP-Port', 'I');
$this->_attributes_info[17] = array('(unassigned)', '');
$this->_attributes_info[18] = array('Reply-Message', 'T');
$this->_attributes_info[19] = array('Callback-Number', 'S');
$this->_attributes_info[20] = array('Callback-Id', 'S');
$this->_attributes_info[21] = array('(unassigned)', '');
$this->_attributes_info[22] = array('Framed-Route', 'T');
$this->_attributes_info[23] = array('Framed-IPX-Network', 'I');
$this->_attributes_info[24] = array('State', 'S');
$this->_attributes_info[25] = array('Class', 'S');
$this->_attributes_info[26] = array('Vendor-Specific', 'S'); // Type (1) / Length (1) / Vendor-Id (4) / Vendor type (1) / Vendor length (1) / Attribute-Specific...
$this->_attributes_info[27] = array('Session-Timeout', 'I');
$this->_attributes_info[28] = array('Idle-Timeout', 'I');
$this->_attributes_info[29] = array('Termination-Action', 'I');
$this->_attributes_info[30] = array('Called-Station-Id', 'S');
$this->_attributes_info[31] = array('Calling-Station-Id', 'S');
$this->_attributes_info[32] = array('NAS-Identifier', 'S');
$this->_attributes_info[33] = array('Proxy-State', 'S');
$this->_attributes_info[34] = array('Login-LAT-Service', 'S');
$this->_attributes_info[35] = array('Login-LAT-Node', 'S');
$this->_attributes_info[36] = array('Login-LAT-Group', 'S');
$this->_attributes_info[37] = array('Framed-AppleTalk-Link', 'I');
$this->_attributes_info[38] = array('Framed-AppleTalk-Network', 'I');
$this->_attributes_info[39] = array('Framed-AppleTalk-Zone', 'S');
$this->_attributes_info[60] = array('CHAP-Challenge', 'S');
$this->_attributes_info[61] = array('NAS-Port-Type', 'I');
$this->_attributes_info[62] = array('Port-Limit', 'I');
$this->_attributes_info[63] = array('Login-LAT-Port', 'S');
$this->_attributes_info[76] = array('Prompt', 'I');
$this->_identifier_to_send = 0;
$this->_user_ip_address = (isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'0.0.0.0');
$this->GenerateRequestAuthenticator();
$this->SetIpRadiusServer($ip_radius_server);
$this->SetSharedSecret($shared_secret);
$this->SetAuthenticationPort($authentication_port);
$this->SetAccountingPort($accounting_port);
$this->SetRadiusSuffix($radius_suffix);
$this->SetUdpTimeout($udp_timeout);
$this->SetUsername();
$this->SetPassword();
$this->SetNasIpAddress();
$this->SetNasPort();
$this->ClearLastError();
$this->ClearDataToSend();
$this->ClearDataReceived();
}
function GetNextIdentifier()
{
$this->_identifier_to_send = (($this->_identifier_to_send + 1) % 256);
return $this->_identifier_to_send;
}
function GenerateRequestAuthenticator()
{
$this->_request_authenticator = '';
for ($ra_loop = 0; $ra_loop <= 15; $ra_loop++)
{
$this->_request_authenticator .= chr(rand(1, 255));
}
}
function GetRequestAuthenticator()
{
return $this->_request_authenticator;
}
function GetLastError()
{
if (0 < $this->_last_error_code)
{
return $this->_last_error_message.' ('.$this->_last_error_code.')';
}
else
{
return '';
}
}
function ClearDataToSend()
{
$this->_radius_packet_to_send = 0;
$this->_attributes_to_send = NULL;
}
function ClearDataReceived()
{
$this->_radius_packet_received = 0;
$this->_attributes_received = NULL;
}
function SetPacketCodeToSend($packet_code)
{
$this->_radius_packet_to_send = $packet_code;
}
function SetDebugMode($debug_mode)
{
$this->_debug_mode = (TRUE === $debug_mode);
}
function SetIpRadiusServer($ip_radius_server)
{
$this->_ip_radius_server = gethostbyname($ip_radius_server);
}
function SetSharedSecret($shared_secret)
{
$this->_shared_secret = $shared_secret;
}
function SetRadiusSuffix($radius_suffix)
{
$this->_radius_suffix = $radius_suffix;
}
function SetUsername($username = '')
{
$temp_username = $username;
if (false === strpos($temp_username, '@'))
{
$temp_username .= $this->_radius_suffix;
}
$this->_username = $temp_username;
$this->SetAttribute(1, $this->_username);
}
function SetPassword($password = '')
{
$this->_password = $password;
$encrypted_password = '';
$padded_password = $password;
if (0 != (strlen($password)%16))
{
$padded_password .= str_repeat(chr(0),(16-strlen($password)%16));
}
$previous_result = $this->_request_authenticator;
for ($full_loop = 0; $full_loop < (strlen($padded_password)/16); $full_loop++)
{
$xor_value = md5($this->_shared_secret.$previous_result);
$previous_result = '';
for ($xor_loop = 0; $xor_loop <= 15; $xor_loop++)
{
$value1 = ord(substr($padded_password, ($full_loop * 16) + $xor_loop, 1));
$value2 = hexdec(substr($xor_value, 2*$xor_loop, 2));
$xor_result = $value1 ^ $value2;
$previous_result .= chr($xor_result);
}
$encrypted_password .= $previous_result;
}
$this->_encrypted_password = $encrypted_password;
$this->SetAttribute(2, $this->_encrypted_password);
}
function SetNasIPAddress($nas_ip_address = '')
{
if (0 < strlen($nas_ip_address))
{
$this->_nas_ip_address = gethostbyname($nas_ip_address);
}
else
{
$this->_nas_ip_address = gethostbyname(isset($_SERVER['SERVER_ADDR'])?$_SERVER['SERVER_ADDR']:'0.0.0.0');
}
$this->SetAttribute(4, $this->_nas_ip_address);
}
function SetNasPort($nas_port = 0)
{
$this->_nas_port = intval($nas_port);
$this->SetAttribute(5, $this->_nas_port);
}
function SetUdpTimeout($udp_timeout = 5)
{
if (intval($udp_timeout) > 0)
{
$this->_udp_timeout = intval($udp_timeout);
}
}
function ClearLastError()
{
$this->_last_error_code = 0;
$this->_last_error_message = '';
}
function SetAuthenticationPort($authentication_port)
{
if ((intval($authentication_port) > 0) && (intval($authentication_port) < 65536))
{
$this->_authentication_port = intval($authentication_port);
}
}
function SetAccountingPort($accounting_port)
{
if ((intval($accounting_port) > 0) && (intval($accounting_port) < 65536))
{
$this->_accounting_port = intval($accounting_port);
}
}
function GetReceivedPacket()
{
return $this->_radius_packet_received;
}
function GetReceivedAttributes()
{
return $this->_attributes_received;
}
function GetReadableReceivedAttributes()
{
$readable_attributes = '';
if (isset($this->_attributes_received))
{
foreach($this->_attributes_received as $one_received_attribute)
{
$attributes_info = $this->GetAttributesInfo($one_received_attribute[0]);
$readable_attributes .= $attributes_info[0].": ";
if (26 == $one_received_attribute[0])
{
$vendor_array = $this->DecodeVendorSpecificContent($one_received_attribute[1]);
foreach($vendor_array as $vendor_one)
{
$readable_attributes .= 'Vendor-Id: '.$vendor_one[0].", Vendor-type: ".$vendor_one[1].", Attribute-specific: ".$vendor_one[2];
}
}
else
{
$readable_attributes .= $one_received_attribute[1];
}
$readable_attributes .= "<br />\n";
}
}
return $readable_attributes;
}
function GetAttribute($attribute_type)
{
$attribute_value = NULL;
foreach($this->_attributes_received as $one_received_attribute)
{
if (intval($attribute_type) == $one_received_attribute[0])
{
$attribute_value = $one_received_attribute[1];
break;
}
}
return $attribute_value;
}
function GetRadiusPacketInfo($info_index)
{
if (isset($this->_radius_packet_info[intval($info_index)]))
{
return $this->_radius_packet_info[intval($info_index)];
}
else
{
return '';
}
}
function GetAttributesInfo($info_index)
{
if (isset($this->_attributes_info[intval($info_index)]))
{
return $this->_attributes_info[intval($info_index)];
}
else
{
return array('','');
}
}
function DebugInfo($debug_info)
{
if ($this->_debug_mode)
{
echo date('Y-m-d H:i:s').' DEBUG: ';
echo $debug_info;
echo '<br />';
flush();
}
}
function SetAttribute($type, $value)
{
$attribute_index = -1;
for ($attributes_loop = 0; $attributes_loop < count($this->_attributes_to_send); $attributes_loop++)
{
if ($type == ord(substr($this->_attributes_to_send[$attributes_loop], 0, 1)))
{
$attribute_index = $attributes_loop;
break;
}
}
$temp_attribute = NULL;
if (isset($this->_attributes_info[$type]))
{
switch ($this->_attributes_info[$type][1])
{
case 'T': // Text, 1-253 octets containing UTF-8 encoded ISO 10646 characters (RFC 2279).
$temp_attribute = chr($type).chr(2+strlen($value)).$value;
break;
case 'S': // String, 1-253 octets containing binary data (values 0 through 255 decimal, inclusive).
$temp_attribute = chr($type).chr(2+strlen($value)).$value;
break;
case 'A': // Address, 32 bit value, most significant octet first.
$ip_array = explode(".", $value);
$ip_array = (count($ip_array) < 4) ? array('0', '0', '0', '0') : $ip_array;
$temp_attribute = chr($type).chr(6).chr($ip_array[0]).chr($ip_array[1]).chr($ip_array[2]).chr($ip_array[3]);
break;
case 'I': // Integer, 32 bit unsigned value, most significant octet first.
$temp_attribute = chr($type).chr(6).chr(($value/(256*256*256))%256).chr(($value/(256*256))%256).chr(($value/(256))%256).chr($value%256);
break;
case 'D': // Time, 32 bit unsigned value, most significant octet first -- seconds since 00:00:00 UTC, January 1, 1970. (not used in this RFC)
$temp_attribute = NULL;
break;
default:
$temp_attribute = NULL;
}
}
if ($attribute_index > -1)
{
$this->_attributes_to_send[$attribute_index] = $temp_attribute;
$additional_debug = 'Modified';
}
else
{
$this->_attributes_to_send[] = $temp_attribute;
$additional_debug = 'Added';
}
$attribute_info = $this->GetAttributesInfo($type);
$this->DebugInfo($additional_debug.' Attribute '.$type.' ('.$attribute_info[0].'), format '.$attribute_info[1].', value <em>'.$value.'</em>');
}
function DecodeAttribute($attribute_raw_value, $attribute_format)
{
$attribute_value = NULL;
if (isset($this->_attributes_info[$attribute_format]))
{
switch ($this->_attributes_info[$attribute_format][1])
{
case 'T': // Text, 1-253 octets containing UTF-8 encoded ISO 10646 characters (RFC 2279).
$attribute_value = $attribute_raw_value;
break;
case 'S': // String, 1-253 octets containing binary data (values 0 through 255 decimal, inclusive).
$attribute_value = $attribute_raw_value;
break;
case 'A': // Address, 32 bit value, most significant octet first.
$attribute_value = ord(substr($attribute_raw_value, 0, 1)).'.'.ord(substr($attribute_raw_value, 1, 1)).'.'.ord(substr($attribute_raw_value, 2, 1)).'.'.ord(substr($attribute_raw_value, 3, 1));
break;
case 'I': // Integer, 32 bit unsigned value, most significant octet first.
$attribute_value = (ord(substr($attribute_raw_value, 0, 1))*256*256*256)+(ord(substr($attribute_raw_value, 1, 1))*256*256)+(ord(substr($attribute_raw_value, 2, 1))*256)+ord(substr($attribute_raw_value, 3, 1));
break;
case 'D': // Time, 32 bit unsigned value, most significant octet first -- seconds since 00:00:00 UTC, January 1, 1970. (not used in this RFC)
$attribute_value = NULL;
break;
default:
$attribute_value = NULL;
}
}
return $attribute_value;
}
/*********************************************************************
* Array returned: array(array(Vendor-Id1, Vendor type1, Attribute-Specific1), ..., array(Vendor-IdN, Vendor typeN, Attribute-SpecificN)
*********************************************************************/
function DecodeVendorSpecificContent($vendor_specific_raw_value)
{
$result = array();
$offset_in_raw = 0;
$vendor_id = (ord(substr($vendor_specific_raw_value, 0, 1))*256*256*256)+(ord(substr($vendor_specific_raw_value, 1, 1))*256*256)+(ord(substr($vendor_specific_raw_value, 2, 1))*256)+ord(substr($vendor_specific_raw_value, 3, 1));
$offset_in_raw += 4;
while ($offset_in_raw < strlen($vendor_specific_raw_value))
{
$vendor_type = (ord(substr($vendor_specific_raw_value, 0+$offset_in_raw, 1)));
$vendor_length = (ord(substr($vendor_specific_raw_value, 1+$offset_in_raw, 1)));
$attribute_specific = substr($vendor_specific_raw_value, 2+$offset_in_raw, $vendor_length);
$result[] = array($vendor_id, $vendor_type, $attribute_specific);
$offset_in_raw += ($vendor_length);
}
return $result;
}
/*
* Function : AccessRequest
*
* Return TRUE if Access-Request is accepted, FALSE otherwise
*/
function AccessRequest($username = '', $password = '', $udp_timeout = 0, $state = NULL)
{
$this->ClearDataReceived();
$this->ClearLastError();
$this->SetPacketCodeToSend(1); // Access-Request
if (0 < strlen($username))
{
$this->SetUsername($username);
}
if (0 < strlen($password))
{
$this->SetPassword($password);
}
if ($state!==NULL)
{
$this->SetAttribute(24, $state);
}
else
{
$this->SetAttribute(6, 1); // 1=Login
}
if (intval($udp_timeout) > 0)
{
$this->SetUdpTimeout($udp_timeout);
}
$attributes_content = '';
for ($attributes_loop = 0; $attributes_loop < count($this->_attributes_to_send); $attributes_loop++)
{
$attributes_content .= $this->_attributes_to_send[$attributes_loop];
}
$packet_length = 4; // Radius packet code + Identifier + Length high + Length low
$packet_length += strlen($this->_request_authenticator); // Request-Authenticator
$packet_length += strlen($attributes_content); // Attributes
$packet_data = chr($this->_radius_packet_to_send);
$packet_data .= chr($this->GetNextIdentifier());
$packet_data .= chr(intval($packet_length/256));
$packet_data .= chr(intval($packet_length%256));
$packet_data .= $this->_request_authenticator;
$packet_data .= $attributes_content;
$_socket_to_server = socket_create(AF_INET, SOCK_DGRAM, 17); // UDP packet = 17
if ($_socket_to_server === FALSE)
{
$this->_last_error_code = socket_last_error();
$this->_last_error_message = socket_strerror($this->_last_error_code);
}
elseif (FALSE === socket_connect($_socket_to_server, $this->_ip_radius_server, $this->_authentication_port))
{
$this->_last_error_code = socket_last_error();
$this->_last_error_message = socket_strerror($this->_last_error_code);
}
elseif (FALSE === socket_write($_socket_to_server, $packet_data, $packet_length))
{
$this->_last_error_code = socket_last_error();
$this->_last_error_message = socket_strerror($this->_last_error_code);
}
else
{
$this->DebugInfo('<b>Packet type '.$this->_radius_packet_to_send.' ('.$this->GetRadiusPacketInfo($this->_radius_packet_to_send).')'.' sent</b>');
if ($this->_debug_mode)
{
$readable_attributes = '';
foreach($this->_attributes_to_send as $one_attribute_to_send)
{
$attribute_info = $this->GetAttributesInfo(ord(substr($one_attribute_to_send,0,1)));
$this->DebugInfo('Attribute '.ord(substr($one_attribute_to_send,0,1)).' ('.$attribute_info[0].'), length '.(ord(substr($one_attribute_to_send,1,1))-2).', format '.$attribute_info[1].', value <em>'.$this->DecodeAttribute(substr($one_attribute_to_send,2), ord(substr($one_attribute_to_send,0,1))).'</em>');
}
}
$read_socket_array = array($_socket_to_server);
$write_socket_array = NULL;
$except_socket_array = NULL;
$received_packet = chr(0);
if (!(FALSE === socket_select($read_socket_array, $write_socket_array, $except_socket_array, $this->_udp_timeout)))
{
if (in_array($_socket_to_server, $read_socket_array))
{
if (FALSE === ($received_packet = @socket_read($_socket_to_server, 1024))) // @ used, than no error is displayed if the connection is closed by the remote host
{
$received_packet = chr(0);
$this->_last_error_code = socket_last_error();
$this->_last_error_message = socket_strerror($this->_last_error_code);
}
else
{
socket_close($_socket_to_server);
}
}
}
else
{
socket_close($_socket_to_server);
}
}
$this->_radius_packet_received = intval(ord(substr($received_packet, 0, 1)));
$this->DebugInfo('<b>Packet type '.$this->_radius_packet_received.' ('.$this->GetRadiusPacketInfo($this->_radius_packet_received).')'.' received</b>');
if ($this->_radius_packet_received > 0)
{
$this->_identifier_received = intval(ord(substr($received_packet, 1, 1)));
$packet_length = (intval(ord(substr($received_packet, 2, 1))) * 256) + (intval(ord(substr($received_packet, 3, 1))));
$this->_response_authenticator = substr($received_packet, 4, 16);
$attributes_content = substr($received_packet, 20, ($packet_length - 4 - 16));
while (strlen($attributes_content) > 2)
{
$attribute_type = intval(ord(substr($attributes_content,0,1)));
$attribute_length = intval(ord(substr($attributes_content,1,1)));
$attribute_raw_value = substr($attributes_content,2,$attribute_length-2);
$attributes_content = substr($attributes_content, $attribute_length);
$attribute_value = $this->DecodeAttribute($attribute_raw_value, $attribute_type);
$attribute_info = $this->GetAttributesInfo($attribute_type);
if (26 == $attribute_type)
{
$vendor_array = $this->DecodeVendorSpecificContent($attribute_value);
foreach($vendor_array as $vendor_one)
{
$this->DebugInfo('Attribute '.$attribute_type.' ('.$attribute_info[0].'), length '.($attribute_length-2).', format '.$attribute_info[1].', Vendor-Id: '.$vendor_one[0].", Vendor-type: ".$vendor_one[1].", Attribute-specific: ".$vendor_one[2]);
}
}
else
{
$this->DebugInfo('Attribute '.$attribute_type.' ('.$attribute_info[0].'), length '.($attribute_length-2).', format '.$attribute_info[1].', value <em>'.$attribute_value.'</em>');
}
$this->_attributes_received[] = array($attribute_type, $attribute_value);
}
}
return (2 == ($this->_radius_packet_received));
}
}
?>