@@ -731,7 +731,7 @@ function captiveportal_delete_rules($pipes_to_remove = array()) {
731
731
/*
732
732
* Remove clients that have been around for longer than the specified amount of time
733
733
* db file structure:
734
- * timestamp,ipfw_rule_no,clientip,clientmac,username,sessionid,password,session_timeout,idle_timeout,session_terminate_time,interim_interval
734
+ * timestamp,ipfw_rule_no,clientip,clientmac,username,sessionid,password,session_timeout,idle_timeout,session_terminate_time,interim_interval,traffic_quota,radiusctx
735
735
* (password is in Base64 and only saved when reauthentication is enabled)
736
736
*/
737
737
function captiveportal_prune_old () {
@@ -755,9 +755,16 @@ function captiveportal_prune_old() {
755
755
$ idletimeout = $ cpcfg ['idletimeout ' ] * 60 ;
756
756
}
757
757
758
+ /* check for entries exceeding their traffic quota */
759
+ $ trafficquota = 0 ;
760
+ if (!empty ($ cpcfg ['trafficquota ' ]) && is_numeric ($ cpcfg ['trafficquota ' ])) {
761
+ $ trafficquota = $ cpcfg ['trafficquota ' ] * 1048576 ;
762
+ }
763
+
758
764
/* Is there any job to do? */
759
- if (!$ timeout && !$ idletimeout && !isset ($ cpcfg ['reauthenticate ' ]) &&
760
- !isset ($ cpcfg ['radiussession_timeout ' ]) && !isset ($ vcpcfg ['enable ' ])) {
765
+ if (!$ timeout && !$ idletimeout && !$ trafficquota && !isset ($ cpcfg ['reauthenticate ' ]) &&
766
+ !isset ($ cpcfg ['radiussession_timeout ' ]) && !isset ($ cpcfg ['radiustraffic_quota ' ]) &&
767
+ !isset ($ vcpcfg ['enable ' ])) {
761
768
return ;
762
769
}
763
770
@@ -779,16 +786,21 @@ function captiveportal_prune_old() {
779
786
780
787
$ timedout = false ;
781
788
$ term_cause = 1 ;
782
- if (empty ($ cpentry [11 ])) {
783
- $ cpentry [11 ] = 'first ' ;
789
+ $ logout_cause = 'TIMEOUT ' ;
790
+ if (empty ($ cpentry [12 ])) {
791
+ $ cpentry [12 ] = 'first ' ;
784
792
}
785
- $ radiusservers = $ radiussrvs [$ cpentry [11 ]];
793
+ $ radiusservers = $ radiussrvs [$ cpentry [12 ]];
786
794
787
- /* hard timeout? */
795
+ /* hard timeout or session_timeout from radius if enabled */
796
+ if (isset ($ cpcfg ['radiussession_timeout ' ])) {
797
+ $ timeout = (is_numeric ($ cpentry [7 ])) ? $ cpentry [7 ] : $ timeout ;
798
+ }
788
799
if ($ timeout ) {
789
800
if (($ pruning_time - $ cpentry [0 ]) >= $ timeout ) {
790
801
$ timedout = true ;
791
802
$ term_cause = 5 ; // Session-Timeout
803
+ $ logout_cause = 'SESSION TIMEOUT ' ;
792
804
}
793
805
}
794
806
@@ -797,6 +809,7 @@ function captiveportal_prune_old() {
797
809
if ($ pruning_time >= $ cpentry [9 ]) {
798
810
$ timedout = true ;
799
811
$ term_cause = 5 ; // Session-Timeout
812
+ $ logout_cause = 'SESSION TIMEOUT ' ;
800
813
}
801
814
}
802
815
@@ -812,6 +825,7 @@ function captiveportal_prune_old() {
812
825
if ($ lastact && (($ pruning_time - $ lastact ) >= $ uidletimeout )) {
813
826
$ timedout = true ;
814
827
$ term_cause = 4 ; // Idle-Timeout
828
+ $ logout_cause = 'IDLE TIMEOUT ' ;
815
829
if (!isset ($ config ['captiveportal ' ][$ cpzone ]['includeidletime ' ])) {
816
830
$ stop_time = $ lastact ;
817
831
}
@@ -823,21 +837,27 @@ function captiveportal_prune_old() {
823
837
if ($ pruning_time >= ($ cpentry [0 ] + $ cpentry [7 ])) {
824
838
$ timedout = true ;
825
839
$ term_cause = 5 ; // Session-Timeout
840
+ $ logout_cause = 'SESSION TIMEOUT ' ;
826
841
$ voucher_needs_sync = true ;
827
842
}
828
843
}
829
844
830
- /* if radius session_timeout is enabled and the session_timeout is not null, then check if the user should be logged out */
831
- if (!$ timedout && isset ($ cpcfg ['radiussession_timeout ' ]) && !empty ($ cpentry [7 ])) {
832
- if ($ pruning_time >= ($ cpentry [0 ] + $ cpentry [7 ])) {
845
+ /* traffic quota, value retrieved from the radius attribute if the option is enabled */
846
+ if (isset ($ cpcfg ['radiustraffic_quota ' ])) {
847
+ $ trafficquota = (is_numeric ($ cpentry [11 ])) ? $ cpentry [11 ] : $ trafficquota ;
848
+ }
849
+ if (!$ timedout && $ trafficquota > 0 ) {
850
+ $ volume = getVolume ($ cpentry [2 ], $ cpentry [3 ]);
851
+ if (($ volume ['input_bytes ' ] + $ volume ['output_bytes ' ]) > $ trafficquota ) {
833
852
$ timedout = true ;
834
- $ term_cause = 5 ; // Session-Timeout
853
+ $ term_cause = 10 ; // NAS-Request
854
+ $ logout_cause = 'QUOTA EXCEEDED ' ;
835
855
}
836
856
}
837
857
838
858
if ($ timedout ) {
839
859
captiveportal_disconnect ($ cpentry , $ radiusservers , $ term_cause , $ stop_time );
840
- captiveportal_logportalauth ($ cpentry [4 ], $ cpentry [3 ], $ cpentry [2 ], " TIMEOUT " );
860
+ captiveportal_logportalauth ($ cpentry [4 ], $ cpentry [3 ], $ cpentry [2 ], $ logout_cause );
841
861
$ unsetindexes [] = $ cpentry [5 ];
842
862
}
843
863
@@ -1098,10 +1118,10 @@ function captiveportal_disconnect_client($sessionid, $term_cause = 1, $logoutRea
1098
1118
captiveportal_write_db ("DELETE FROM captiveportal WHERE sessionid = ' {$ sessionid }' " );
1099
1119
1100
1120
foreach ($ result as $ cpentry ) {
1101
- if (empty ($ cpentry [11 ])) {
1102
- $ cpentry [11 ] = 'first ' ;
1121
+ if (empty ($ cpentry [12 ])) {
1122
+ $ cpentry [12 ] = 'first ' ;
1103
1123
}
1104
- captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [11 ]], $ term_cause );
1124
+ captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [12 ]], $ term_cause );
1105
1125
captiveportal_logportalauth ($ cpentry [4 ], $ cpentry [3 ], $ cpentry [2 ], "DISCONNECT " );
1106
1126
}
1107
1127
unset($ result );
@@ -1155,15 +1175,15 @@ function captiveportal_radius_stop_all($term_cause = 6, $logoutReason = "DISCONN
1155
1175
foreach ($ cpdb as $ cpentry ) {
1156
1176
if ($ radacct ) {
1157
1177
if (!empty ($ radiusservers )) {
1158
- if (empty ($ cpentry [11 ])) {
1159
- $ cpentry [11 ] = 'first ' ;
1178
+ if (empty ($ cpentry [12 ])) {
1179
+ $ cpentry [12 ] = 'first ' ;
1160
1180
}
1161
- if (!empty ($ radiusservers [$ cpentry [11 ]])) {
1181
+ if (!empty ($ radiusservers [$ cpentry [12 ]])) {
1162
1182
RADIUS_ACCOUNTING_STOP ($ cpentry [1 ], // ruleno
1163
1183
$ cpentry [4 ], // username
1164
1184
$ cpentry [5 ], // sessionid
1165
1185
$ cpentry [0 ], // start time
1166
- $ radiusservers [$ cpentry [11 ]],
1186
+ $ radiusservers [$ cpentry [12 ]],
1167
1187
$ cpentry [2 ], // clientip
1168
1188
$ cpentry [3 ], // clientmac
1169
1189
$ term_cause );
@@ -1613,7 +1633,8 @@ function captiveportal_opendb() {
1613
1633
$ createquery = "CREATE TABLE IF NOT EXISTS captiveportal ( " .
1614
1634
"allow_time INTEGER, pipeno INTEGER, ip TEXT, mac TEXT, username TEXT, " .
1615
1635
"sessionid TEXT, bpassword TEXT, session_timeout INTEGER, idle_timeout INTEGER, " .
1616
- "session_terminate_time INTEGER, interim_interval INTEGER, radiusctx TEXT); " .
1636
+ "session_terminate_time INTEGER, interim_interval INTEGER, traffic_quota INTEGER, " .
1637
+ "radiusctx TEXT); " .
1617
1638
"CREATE UNIQUE INDEX IF NOT EXISTS idx_active ON captiveportal (sessionid, username); " .
1618
1639
"CREATE INDEX IF NOT EXISTS user ON captiveportal (username); " .
1619
1640
"CREATE INDEX IF NOT EXISTS ip ON captiveportal (ip); " .
@@ -2147,8 +2168,14 @@ function captiveportal_reapply_attributes($cpentry, $attributes) {
2147
2168
$ dwfaultbw_up = $ dwfaultbw_down = 0 ;
2148
2169
}
2149
2170
/* pipe throughputs must always be an integer, enforce that restriction again here. */
2150
- $ bw_up = round (!empty ($ attributes ['bw_up ' ]) ? intval ($ attributes ['bw_up ' ])/1000 : $ dwfaultbw_up , 0 );
2151
- $ bw_down = round (!empty ($ attributes ['bw_down ' ]) ? intval ($ attributes ['bw_down ' ])/1000 : $ dwfaultbw_down , 0 );
2171
+ if (isset ($ config ['captiveportal ' ][$ cpzone ]['radiusperuserbw ' ])) {
2172
+ $ bw_up = round (!empty ($ attributes ['bw_up ' ]) ? intval ($ attributes ['bw_up ' ])/1000 : $ dwfaultbw_up , 0 );
2173
+ $ bw_down = round (!empty ($ attributes ['bw_down ' ]) ? intval ($ attributes ['bw_down ' ])/1000 : $ dwfaultbw_down , 0 );
2174
+ } else {
2175
+ $ bw_up = round ($ dwfaultbw_up ,0 );
2176
+ $ bw_down = round ($ dwfaultbw_down ,0 );
2177
+ }
2178
+
2152
2179
$ bw_up_pipeno = $ cpentry [1 ];
2153
2180
$ bw_down_pipeno = $ cpentry [1 ]+1 ;
2154
2181
@@ -2236,8 +2263,8 @@ function portal_allow($clientip, $clientmac, $username, $password = null, $attri
2236
2263
}
2237
2264
2238
2265
foreach ($ cpdb as $ cpentry ) {
2239
- if (empty ($ cpentry [11 ])) {
2240
- $ cpentry [11 ] = 'first ' ;
2266
+ if (empty ($ cpentry [12 ])) {
2267
+ $ cpentry [12 ] = 'first ' ;
2241
2268
}
2242
2269
/* on the same ip */
2243
2270
if ($ cpentry [2 ] == $ clientip ) {
@@ -2257,15 +2284,15 @@ function portal_allow($clientip, $clientmac, $username, $password = null, $attri
2257
2284
}
2258
2285
2259
2286
/* This user was already logged in so we disconnect the old one */
2260
- captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [11 ]], 13 );
2287
+ captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [12 ]], 13 );
2261
2288
captiveportal_logportalauth ($ cpentry [4 ], $ cpentry [3 ], $ cpentry [2 ], "CONCURRENT LOGIN - TERMINATING OLD SESSION " );
2262
2289
$ unsetindexes [] = $ cpentry [5 ];
2263
2290
break ;
2264
2291
} elseif ((isset ($ config ['captiveportal ' ][$ cpzone ]['noconcurrentlogins ' ])) && ($ username != 'unauthenticated ' )) {
2265
2292
/* on the same username */
2266
2293
if (strcasecmp ($ cpentry [4 ], $ username ) == 0 ) {
2267
2294
/* This user was already logged in so we disconnect the old one */
2268
- captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [11 ]], 13 );
2295
+ captiveportal_disconnect ($ cpentry , $ radiusservers [$ cpentry [12 ]], 13 );
2269
2296
captiveportal_logportalauth ($ cpentry [4 ], $ cpentry [3 ], $ cpentry [2 ], "CONCURRENT LOGIN - TERMINATING OLD SESSION " );
2270
2297
$ unsetindexes [] = $ cpentry [5 ];
2271
2298
break ;
@@ -2294,8 +2321,13 @@ function portal_allow($clientip, $clientmac, $username, $password = null, $attri
2294
2321
$ dwfaultbw_up = $ dwfaultbw_down = 0 ;
2295
2322
}
2296
2323
/* pipe throughputs must always be an integer, enforce that restriction again here. */
2297
- $ bw_up = round (!empty ($ attributes ['bw_up ' ]) ? intval ($ attributes ['bw_up ' ])/1000 : $ dwfaultbw_up , 0 );
2298
- $ bw_down = round (!empty ($ attributes ['bw_down ' ]) ? intval ($ attributes ['bw_down ' ])/1000 : $ dwfaultbw_down , 0 );
2324
+ if (isset ($ config ['captiveportal ' ][$ cpzone ]['radiusperuserbw ' ])) {
2325
+ $ bw_up = round (!empty ($ attributes ['bw_up ' ]) ? intval ($ attributes ['bw_up ' ])/1000 : $ dwfaultbw_up , 0 );
2326
+ $ bw_down = round (!empty ($ attributes ['bw_down ' ]) ? intval ($ attributes ['bw_down ' ])/1000 : $ dwfaultbw_down , 0 );
2327
+ } else {
2328
+ $ bw_up = round ($ dwfaultbw_up ,0 );
2329
+ $ bw_down = round ($ dwfaultbw_down ,0 );
2330
+ }
2299
2331
2300
2332
if ($ passthrumac ) {
2301
2333
@@ -2374,15 +2406,16 @@ function portal_allow($clientip, $clientmac, $username, $password = null, $attri
2374
2406
$ idle_timeout = (!empty ($ attributes ['idle_timeout ' ])) ? $ attributes ['idle_timeout ' ] : 'NULL ' ;
2375
2407
$ session_terminate_time = (!empty ($ attributes ['session_terminate_time ' ])) ? $ attributes ['session_terminate_time ' ] : 'NULL ' ;
2376
2408
$ interim_interval = (!empty ($ attributes ['interim_interval ' ])) ? $ attributes ['interim_interval ' ] : 'NULL ' ;
2409
+ $ traffic_quota = (!empty ($ attributes ['maxbytes ' ])) ? $ attributes ['maxbytes ' ] : 'NULL ' ;
2377
2410
2378
2411
/* escape username */
2379
2412
$ safe_username = SQLite3::escapeString ($ username );
2380
2413
2381
2414
/* encode password in Base64 just in case it contains commas */
2382
2415
$ bpassword = (isset ($ config ['captiveportal ' ][$ cpzone ]['reauthenticate ' ])) ? base64_encode ($ password ) : '' ;
2383
- $ insertquery = "INSERT INTO captiveportal (allow_time, pipeno, ip, mac, username, sessionid, bpassword, session_timeout, idle_timeout, session_terminate_time, interim_interval, radiusctx) " ;
2416
+ $ insertquery = "INSERT INTO captiveportal (allow_time, pipeno, ip, mac, username, sessionid, bpassword, session_timeout, idle_timeout, session_terminate_time, interim_interval, traffic_quota, radiusctx) " ;
2384
2417
$ insertquery .= "VALUES ( {$ allow_time }, {$ pipeno }, ' {$ clientip }', ' {$ clientmac }', ' {$ safe_username }', ' {$ sessionid }', ' {$ bpassword }', " ;
2385
- $ insertquery .= "{$ session_timeout }, {$ idle_timeout }, {$ session_terminate_time }, {$ interim_interval }, ' {$ radiusctx }') " ;
2418
+ $ insertquery .= "{$ session_timeout }, {$ idle_timeout }, {$ session_terminate_time }, {$ interim_interval }, { $ traffic_quota } , ' {$ radiusctx }') " ;
2386
2419
2387
2420
/* store information to database */
2388
2421
captiveportal_write_db ($ insertquery );
0 commit comments