Project

General

Profile

Download (22.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * voucher.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2007-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2007 Marcel Wiget <mwiget@mac.com>
8
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
/* include all configuration functions */
28
if (!function_exists('captiveportal_syslog')) {
29
	require_once("captiveportal.inc");
30
}
31

    
32
function xmlrpc_sync_voucher_details(&$syncip, &$port, &$username, &$password) {
33
	global $config;
34

    
35
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
36
		$syncip = $config['voucher'][$cpzone]['vouchersyncdbip'];
37
		$port = $config['voucher'][$cpzone]['vouchersyncport'];
38
		$username = $config['voucher'][$cpzone]['vouchersyncusername'];
39
		$password = $config['voucher'][$cpzone]['vouchersyncpass'];
40
		return true;
41
	}
42

    
43
	if (!is_array($config['hasync']) ||
44
	    empty($config['hasync']['synchronizetoip']) ||
45
	    $config['hasync']['synchronizecaptiveportal'] == "") {
46
		return false;
47
	}
48

    
49
	$syncip = $config['hasync']['synchronizetoip'];
50
	$password = $config['hasync']['password'];
51
	if (empty($config['hasync']['username'])) {
52
		$username = "admin";
53
	} else {
54
		$username = $config['hasync']['username'];
55
	}
56

    
57
	/* if port is empty lets rely on the protocol selection */
58
	$port = $config['system']['webgui']['port'];
59
	if (empty($port)) {
60
		if ($config['system']['webgui']['protocol'] == "http") {
61
			$port = "80";
62
		} else {
63
			$port = "443";
64
		}
65
	}
66

    
67
	return true;
68
}
69

    
70
function xmlrpc_sync_voucher_expire($vouchers, $syncip, $port, $password, $username) {
71
	global $cpzone;
72
	require_once("xmlrpc_client.inc");
73

    
74
	/* Construct code that is run on remote machine */
75
	$execcmd = <<<EOF
76
	global \$cpzone;
77
	require_once('/etc/inc/captiveportal.inc');
78
	require_once('/etc/inc/voucher.inc');
79
	\$cpzone = "$cpzone";
80
	return voucher_expire("$vouchers");
81

    
82
EOF;
83
	$rpc_client = new pfsense_xmlrpc_client();
84
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
85
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
86
	if (empty($resp)) {
87
		return false;
88
	}
89
	return $resp;
90
}
91

    
92
function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $username, $term_cause = 1, $stop_time = null) {
93
	global $cpzone;
94
	require_once("xmlrpc_client.inc");
95
	/* Construct code that is run on remote machine */
96
	$dbent_str = addslashes(serialize($dbent));
97
	$tmp_stop_time = (isset($stop_time)) ? $stop_time : "null";
98
	$execcmd = <<<EOF
99
	global \$cpzone;
100
	require_once('/etc/inc/captiveportal.inc');
101
	require_once('/etc/inc/voucher.inc');
102
	\$cpzone = "$cpzone";
103
	\$radiusservers = captiveportal_get_radius_servers();
104
	\$dbent = unserialize("$dbent_str");
105
	return captiveportal_disconnect(\$dbent, \$radiusservers, $term_cause, $tmp_stop_time);
106

    
107
EOF;
108
	$rpc_client = new pfsense_xmlrpc_client();
109
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
110
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
111
	if (empty($resp)) {
112
		return false;
113
	}
114
	return $resp;
115
}
116

    
117
function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
118
	global $config, $cpzone;
119
	require_once("xmlrpc_client.inc");
120

    
121
	/* Construct code that is run on remote machine */
122
	$execcmd = <<<EOF
123
	global \$cpzone, \$config;
124
	require_once('/etc/inc/voucher.inc');
125
	\$cpzone = "$cpzone";
126
	\$timeleft = voucher_auth("$voucher_received");
127
	\$toreturn = array();
128
	\$toreturn['timeleft'] = \$timeleft;
129
	\$toreturn['voucher'] = array();
130
	if (is_array(\$config['voucher'][\$cpzone]['roll'])) {
131
		\$toreturn['voucher']['roll'] = \$config['voucher'][\$cpzone]['roll'];
132
	}
133

    
134
EOF;
135
	$rpc_client = new pfsense_xmlrpc_client();
136
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
137
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
138

    
139
	if (!is_array($config['voucher'])) {
140
		$config['voucher'] = array();
141
	}
142

    
143
	if (is_array($resp['voucher']['roll'])) {
144
		$config['voucher'][$cpzone]['roll'] = $resp['voucher']['roll'];
145
		write_config(sprintf(gettext('Captive Portal Voucher database synchronized with %1$s:%2$s'), $syncip, $port));
146
		voucher_configure_zone(true);
147
		unset($resp['voucher']);
148
	} else if (!isset($resp['timeleft'])) {
149
		return 0;
150
	}
151

    
152
	return $resp['timeleft'];
153
}
154

    
155
function voucher_expire($voucher_received) {
156
	global $g, $config, $cpzone, $cpzoneid;
157

    
158
	// XMLRPC Call over to the master Voucher node
159
	if (xmlrpc_sync_voucher_details($syncip, $syncport,
160
	    $vouchersyncusername, $syncpass)) {
161
		xmlrpc_sync_voucher_expire($voucher_received, $syncip,
162
		    $syncport, $syncpass, $vouchersyncusername);
163
	}
164

    
165
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
166

    
167
	// read rolls into assoc array with rollid as key and minutes as value
168
	$tickets_per_roll = array();
169
	$minutes_per_roll = array();
170
	if (is_array($config['voucher'][$cpzone]['roll'])) {
171
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
172
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
173
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
174
		}
175
	}
176

    
177
	// split into an array. Useful for multiple vouchers given
178
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
179
	$active_dirty = false;
180
	$unsetindexes = array();
181

    
182
	// go through all received vouchers, check their valid and extract
183
	// Roll# and Ticket# using the external readvoucher binary
184
	foreach ($a_vouchers_received as $voucher) {
185
		$v = escapeshellarg($voucher);
186
		if (strlen($voucher) < 5) {
187
			captiveportal_syslog("${voucher} invalid: Too short!");
188
			continue;   // seems too short to be a voucher!
189
		}
190

    
191
		unset($output);
192
		$_gb = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v", $output);
193
		list($status, $roll, $nr) = explode(" ", $output[0]);
194
		if ($status == "OK") {
195
			// check if we have this ticket on a registered roll for this ticket
196
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
197
				// voucher is from a registered roll.
198
				if (!isset($active_vouchers[$roll])) {
199
					$active_vouchers[$roll] = voucher_read_active_db($roll);
200
				}
201
				// valid voucher. Store roll# and ticket#
202
				if (!empty($active_vouchers[$roll][$voucher])) {
203
					$active_dirty = true;
204
					unset($active_vouchers[$roll][$voucher]);
205
				}
206
				// check if voucher already marked as used
207
				if (!isset($bitstring[$roll])) {
208
					$bitstring[$roll] = voucher_read_used_db($roll);
209
				}
210
				$pos = $nr >> 3; // divide by 8 -> octet
211
				$mask = 1 << ($nr % 8);
212
				// mark bit for this voucher as used
213
				if (!(ord($bitstring[$roll][$pos]) & $mask)) {
214
					$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
215
				}
216
				captiveportal_syslog("{$voucher} ({$roll}/{$nr}) forced to expire");
217

    
218
				/* Check if this voucher has any active sessions */
219
				$cpentry = captiveportal_read_db("WHERE username = '{$voucher}'");
220
				if (!empty($cpentry) && !empty($cpentry[0])) {
221
					if (empty($cpzoneid) && !empty($config['captiveportal'][$cpzone])) {
222
						$cpzoneid = $config['captiveportal'][$cpzone]['zoneid'];
223
					}
224
					$cpentry = $cpentry[0];
225
					captiveportal_disconnect($cpentry, null, 13);
226
					captiveportal_logportalauth($cpentry[4], $cpentry[3], $cpentry[2], "FORCLY TERMINATING VOUCHER {$voucher} SESSION");
227
					$unsetindexes[] = $cpentry[5];
228
				}
229
			} else {
230
				captiveportal_syslog(sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr));
231
			}
232
		} else {
233
			// hmm, thats weird ... not what I expected
234
			captiveportal_syslog(sprintf(gettext('%1$s invalid: %2$s!!'), $voucher, $output[0]));
235
		}
236
	}
237

    
238
	// Refresh active DBs
239
	if ($active_dirty == true) {
240
		foreach ($active_vouchers as $roll => $active) {
241
			voucher_write_active_db($roll, $active);
242
		}
243
		unset($active_vouchers);
244

    
245
		/* Trigger a sync of the vouchers on config */
246
		send_event("service sync vouchers");
247
	}
248

    
249
	// Write back the used DB's
250
	if (is_array($bitstring)) {
251
		foreach ($bitstring as $roll => $used) {
252
			if (is_array($used)) {
253
				foreach ($used as $u) {
254
					voucher_write_used_db($roll, base64_encode($u));
255
				}
256
			} else {
257
				voucher_write_used_db($roll, base64_encode($used));
258
			}
259
		}
260
		unset($bitstring);
261
	}
262

    
263
	unlock($voucherlck);
264

    
265
	/* Write database */
266
	if (!empty($unsetindexes)) {
267
		captiveportal_remove_entries($unsetindexes);
268
	}
269

    
270
	return true;
271
}
272

    
273
/*
274
 * Authenticate a voucher and return the remaining time credit in minutes
275
 * if $test is set, don't mark the voucher as used nor add it to the list
276
 * of active vouchers
277
 * If $test is set, simply test the voucher. Don't change anything
278
 * but return a more verbose error and result message back
279
 */
280
function voucher_auth($voucher_received, $test = 0) {
281
	global $g, $config, $cpzone, $dbc;
282

    
283
	if (!isset($config['voucher'][$cpzone]['enable'])) {
284
		return 0;
285
	}
286

    
287
	// XMLRPC Call over to the master Voucher node
288
	if (xmlrpc_sync_voucher_details($syncip, $syncport,
289
	    $vouchersyncusername, $syncpass)) {
290
		$remote_time_used = xmlrpc_sync_used_voucher($voucher_received,
291
		    $syncip, $syncport, $syncpass, $vouchersyncusername);
292
	}
293

    
294
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
295

    
296
	// read rolls into assoc array with rollid as key and minutes as value
297
	$tickets_per_roll = array();
298
	$minutes_per_roll = array();
299
	if (is_array($config['voucher'][$cpzone]['roll'])) {
300
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
301
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
302
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
303
		}
304
	}
305

    
306
	// split into an array. Useful for multiple vouchers given
307
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
308
	$error = 0;
309
	$test_result = array();     // used to display for voucher test option in GUI
310
	$total_minutes = 0;
311
	$first_voucher = "";
312
	$first_voucher_roll = 0;
313

    
314
	// go through all received vouchers, check their valid and extract
315
	// Roll# and Ticket# using the external readvoucher binary
316
	foreach ($a_vouchers_received as $voucher) {
317
		$v = escapeshellarg($voucher);
318
		if (strlen($voucher) < 5) {
319
			$voucher_err_text = sprintf(gettext("%s invalid: Too short!"), $voucher);
320
			$test_result[] = $voucher_err_text;
321
			captiveportal_syslog($voucher_err_text);
322
			$error++;
323
			continue;   // seems too short to be a voucher!
324
		}
325

    
326
		$result = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v");
327
		list($status, $roll, $nr) = explode(" ", $result);
328
		if ($status == "OK") {
329
			if (!$first_voucher) {
330
				// store first voucher. Thats the one we give the timecredit
331
				$first_voucher = $voucher;
332
				$first_voucher_roll = $roll;
333
			}
334
			// check if we have this ticket on a registered roll for this ticket
335
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
336
				// voucher is from a registered roll.
337
				if (!isset($active_vouchers[$roll])) {
338
					$active_vouchers[$roll] = voucher_read_active_db($roll);
339
				}
340
				// valid voucher. Store roll# and ticket#
341
				if (!empty($active_vouchers[$roll][$voucher])) {
342
					list($timestamp, $minutes) = explode(",", $active_vouchers[$roll][$voucher]);
343
					// we have an already active voucher here.
344
					$remaining = intval((($timestamp + (60*$minutes)) - time())/60);
345
					$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) active and good for %4$d Minutes'), $voucher, $roll, $nr, $remaining);
346
					$total_minutes += $remaining;
347
				} else {
348
					// voucher not used. Check if ticket Id is on the roll (not too high)
349
					// and if the ticket is marked used.
350
					// check if voucher already marked as used
351
					if (!isset($bitstring[$roll])) {
352
						$bitstring[$roll] = voucher_read_used_db($roll);
353
					}
354
					$pos = $nr >> 3; // divide by 8 -> octet
355
					$mask = 1 << ($nr % 8);
356
					if (ord($bitstring[$roll][$pos]) & $mask) {
357
						$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s) already used and expired'), $voucher, $roll, $nr);
358
						$test_result[] = $voucher_err_text;
359
						captiveportal_syslog($voucher_err_text);
360
						$total_minutes = -1;    // voucher expired
361
						$error++;
362
					} else {
363
						// mark bit for this voucher as used
364
						$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
365
						$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) good for %4$s Minutes'), $voucher, $roll, $nr, $minutes_per_roll[$roll]);
366
						$total_minutes += $minutes_per_roll[$roll];
367
					}
368
				}
369
			} else {
370
				$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr);
371
				$test_result[] = $voucher_err_text;
372
				captiveportal_syslog($voucher_err_text);
373
			}
374
		} else {
375
			// hmm, thats weird ... not what I expected
376
			$voucher_err_text = sprintf(gettext('%1$s invalid: %2$s !!'), $voucher, $result);
377
			$test_result[] = $voucher_err_text;
378
			captiveportal_syslog($voucher_err_text);
379
			$error++;
380
		}
381
	}
382

    
383
	// if this was a test call, we're done. Return the result.
384
	if ($test) {
385
		if ($error) {
386
			$test_result[] = gettext("Access denied!");
387
		} else {
388
			$test_result[] = sprintf(gettext("Access granted for %d Minutes in total."), $total_minutes);
389
		}
390
		unlock($voucherlck);
391

    
392
		return $test_result;
393
	}
394

    
395
	// if we had an error (one of the vouchers is invalid), return 0.
396
	// Discussion: we could return the time remaining for good vouchers, but then
397
	// the user wouldn't know that he used at least one invalid voucher.
398
	if ($error) {
399
		unlock($voucherlck);
400
		if ($total_minutes > 0) {   // probably not needed, but want to make sure
401
			$total_minutes = 0;     // we only report -1 (expired) or 0 (no access)
402
		}
403
		return $total_minutes;       // well, at least one voucher had errors. Say NO ACCESS
404
	}
405

    
406
	// If we did a XMLRPC sync earlier check the timeleft
407
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
408
		if (!is_null($remote_time_used)) {
409
			$total_minutes = $remote_time_used;
410
		} else if ($remote_time_used < $total_minutes) {
411
			$total_minutes -= $remote_time_used;
412
		}
413
	}
414

    
415
	// All given vouchers were valid and this isn't simply a test.
416
	// Write back the used DB's
417
	if (is_array($bitstring)) {
418
		foreach ($bitstring as $roll => $used) {
419
			if (is_array($used)) {
420
				foreach ($used as $u) {
421
					voucher_write_used_db($roll, base64_encode($u));
422
				}
423
			} else {
424
				voucher_write_used_db($roll, base64_encode($used));
425
			}
426
		}
427
	}
428

    
429
	// Active DB: we only add the first voucher if multiple given
430
	// and give that one all the time credit. This allows the user to logout and
431
	// log in later using just the first voucher. It also keeps username limited
432
	// to one voucher and that voucher shows the correct time credit in 'active vouchers'
433
	if (!empty($active_vouchers[$first_voucher_roll][$first_voucher])) {
434
		list($timestamp, $minutes) = explode(",", $active_vouchers[$first_voucher_roll][$first_voucher]);
435
	} else {
436
		$timestamp = time();    // new voucher
437
		$minutes = $total_minutes;
438
	}
439

    
440
	$active_vouchers[$first_voucher_roll][$first_voucher] = "$timestamp,$minutes";
441
	voucher_write_active_db($first_voucher_roll, $active_vouchers[$first_voucher_roll]);
442

    
443
	/* Trigger a sync of the vouchers on config */
444
	send_event("service sync vouchers");
445

    
446
	unlock($voucherlck);
447

    
448
	return $total_minutes;
449
}
450

    
451
function voucher_configure($sync = false) {
452
	global $config, $g, $cpzone;
453

    
454
	if (is_array($config['voucher'])) {
455
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
456
			if (platform_booting()) {
457
				echo gettext("Enabling voucher support... ");
458
			}
459
			$cpzone = $voucherzone;
460
			$error = voucher_configure_zone($sync);
461
			if (platform_booting()) {
462
				if ($error) {
463
					echo "error\n";
464
				} else {
465
					echo "done\n";
466
				}
467
			}
468
		}
469
	}
470
}
471

    
472
function voucher_configure_zone($sync = false) {
473
	global $config, $g, $cpzone;
474

    
475
	if (!isset($config['voucher'][$cpzone]['enable'])) {
476
		return 0;
477
	}
478

    
479
	if ($sync == true) {
480
		captiveportal_syslog("Writing voucher db from sync data...");
481
	}
482

    
483
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
484

    
485
	/* write public key used to verify vouchers */
486
	$pubkey = base64_decode($config['voucher'][$cpzone]['publickey']);
487
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.public", "w");
488
	if (!$fd) {
489
		captiveportal_syslog("Voucher error: cannot write voucher.public\n");
490
		unlock($voucherlck);
491
		return 1;
492
	}
493
	fwrite($fd, $pubkey);
494
	fclose($fd);
495
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.public", 0600);
496

    
497
	/* write config file used by voucher binary to decode vouchers */
498
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.cfg", "w");
499
	if (!$fd) {
500
		printf(gettext("Error: cannot write voucher.cfg") . "\n");
501
		unlock($voucherlck);
502
		return 1;
503
	}
504
	fwrite($fd, "{$config['voucher'][$cpzone]['rollbits']},{$config['voucher'][$cpzone]['ticketbits']},{$config['voucher'][$cpzone]['checksumbits']},{$config['voucher'][$cpzone]['magic']},{$config['voucher'][$cpzone]['charset']}\n");
505
	fclose($fd);
506
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.cfg", 0600);
507
	unlock($voucherlck);
508

    
509
	if ((platform_booting() || $sync == true) && is_array($config['voucher'][$cpzone]['roll'])) {
510

    
511
		$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
512

    
513
		// create active and used DB per roll on ramdisk from config
514
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
515

    
516
			$roll = $rollent['number'];
517
			$len = ($rollent['count'] >> 3) + 1;
518
			if (strlen(base64_decode($rollent['used'])) != $len) {
519
				$rollent['used'] = base64_encode(str_repeat("\000", $len));
520
			}
521
			voucher_write_used_db($roll, $rollent['used']);
522
			$minutes = $rollent['minutes'];
523
			$active_vouchers = array();
524
			$a_active = &$rollent['active'];
525
			if (is_array($a_active)) {
526
				foreach ($a_active as $activent) {
527
					$voucher = $activent['voucher'];
528
					$timestamp = $activent['timestamp'];
529
					$minutes = $activent['minutes'];
530
					// its tempting to check for expired timestamps, but during
531
					// bootup, we most likely don't have the correct time.
532
					$active_vouchers[$voucher] = "$timestamp,$minutes";
533
				}
534
			}
535
			voucher_write_active_db($roll, $active_vouchers);
536
		}
537

    
538
		unlock($voucherlck);
539
	}
540

    
541
	return 0;
542
}
543

    
544
/* write bitstring of used vouchers to ramdisk.
545
 * Bitstring must already be base64_encoded!
546
 */
547
function voucher_write_used_db($roll, $vdb) {
548
	global $g, $cpzone;
549

    
550
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db", "w");
551
	if ($fd) {
552
		fwrite($fd, $vdb . "\n");
553
		fclose($fd);
554
	} else {
555
		voucher_log(LOG_ERR, sprintf(gettext('cant write %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
556
	}
557
}
558

    
559
/* return assoc array of active vouchers with activation timestamp
560
 * voucher is index.
561
 */
562
function voucher_read_active_db($roll) {
563
	global $g, $cpzone;
564

    
565
	$active = array();
566
	$dirty = 0;
567
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db";
568
	if (file_exists($file)) {
569
		$fd = fopen($file, "r");
570
		if ($fd) {
571
			while (!feof($fd)) {
572
				$line = trim(fgets($fd));
573
				if ($line) {
574
					list($voucher, $timestamp, $minutes) = explode(",", $line); // voucher,timestamp
575
					if ((($timestamp + (60*$minutes)) - time()) > 0) {
576
						$active[$voucher] = "$timestamp,$minutes";
577
					} else {
578
						$dirty=1;
579
					}
580
				}
581
			}
582
			fclose($fd);
583
			if ($dirty) { // if we found expired entries, lets save our snapshot
584
				voucher_write_active_db($roll, $active);
585

    
586
				/* Trigger a sync of the vouchers on config */
587
				send_event("service sync vouchers");
588
			}
589
		}
590
	}
591
	return $active;
592
}
593

    
594
/* store array of active vouchers back to DB */
595
function voucher_write_active_db($roll, $active) {
596
	global $g, $cpzone;
597

    
598
	if (!is_array($active)) {
599
		return;
600
	}
601
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db", "w");
602
	if ($fd) {
603
		foreach ($active as $voucher => $value) {
604
			fwrite($fd, "$voucher,$value\n");
605
		}
606
		fclose($fd);
607
	}
608
}
609

    
610
/* return how many vouchers are marked used on a roll */
611
function voucher_used_count($roll) {
612
	global $g, $cpzone;
613

    
614
	$bitstring = voucher_read_used_db($roll);
615
	$max = strlen($bitstring) * 8;
616
	$used = 0;
617
	for ($i = 1; $i <= $max; $i++) {
618
		// check if ticket already used or not.
619
		$pos = $i >> 3;            // divide by 8 -> octet
620
		$mask = 1 << ($i % 8);  // mask to test bit in octet
621
		if (ord($bitstring[$pos]) & $mask) {
622
			$used++;
623
		}
624
	}
625
	unset($bitstring);
626

    
627
	return $used;
628
}
629

    
630
function voucher_read_used_db($roll) {
631
	global $g, $cpzone;
632

    
633
	$vdb = "";
634
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db";
635
	if (file_exists($file)) {
636
		$fd = fopen($file, "r");
637
		if ($fd) {
638
			$vdb = trim(fgets($fd));
639
			fclose($fd);
640
		} else {
641
			voucher_log(LOG_ERR, sprintf(gettext('cant read %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
642
		}
643
	}
644
	return base64_decode($vdb);
645
}
646

    
647
function voucher_unlink_db($roll) {
648
	global $g, $cpzone;
649
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db");
650
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db");
651
}
652

    
653
/* we share the log with captiveportal for now */
654
function voucher_log($priority, $message) {
655

    
656
	$message = trim($message);
657
	openlog("logportalauth", LOG_PID, LOG_LOCAL4);
658
	syslog($priority, sprintf(gettext("Voucher: %s"), $message));
659
	closelog();
660
}
661

    
662
/* Save active and used voucher DB into XML config and write it to flash
663
 * Called during reboot -> system_reboot_cleanup() and every active voucher change
664
 */
665
function voucher_save_db_to_config() {
666
	global $config, $g, $cpzone;
667

    
668
	if (is_array($config['voucher'])) {
669
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
670
			$cpzone = $voucherzone;
671
			voucher_save_db_to_config_zone();
672
		}
673
	}
674
}
675

    
676
function voucher_save_db_to_config_zone() {
677
	global $config, $g, $cpzone;
678

    
679
	if (!isset($config['voucher'][$cpzone]['enable'])) {
680
		return;   // no vouchers or don't want to save DB's
681
	}
682

    
683
	if (!is_array($config['voucher'][$cpzone]['roll'])) {
684
		return;
685
	}
686

    
687
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
688

    
689
	// walk all active rolls and save runtime DB's to flash
690
	$a_roll = &$config['voucher'][$cpzone]['roll'];
691
	while (list($key, $value) = each($a_roll)) {
692
		$rollent = &$a_roll[$key];
693
		$roll = $rollent['number'];
694
		$bitmask = voucher_read_used_db($roll);
695
		$rollent['used'] = base64_encode($bitmask);
696
		$active_vouchers = voucher_read_active_db($roll);
697
		$db = array();
698
		$dbi = 1;
699
		foreach ($active_vouchers as $voucher => $line) {
700
			list($timestamp, $minutes) = explode(",", $line);
701
			$activent['voucher'] = $voucher;
702
			$activent['timestamp'] = $timestamp;
703
			$activent['minutes'] = $minutes;
704
			$db["v{$dbi}"] = $activent;
705
			$dbi++;
706
		}
707
		$rollent['active'] = $db;
708
		unset($active_vouchers);
709
	}
710

    
711
	unlock($voucherlck);
712

    
713
	write_config(gettext("Syncing vouchers"));
714
	return;
715
}
716

    
717
?>
(53-53/60)