Project

General

Profile

Download (21.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * voucher.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2007-2016 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_expire($vouchers, $syncip, $port, $password, $username) {
33
	global $cpzone;
34
	require_once("xmlrpc_client.inc");
35
	
36
	/* Construct code that is run on remote machine */
37
	$execcmd = <<<EOF
38
	global \$cpzone;
39
	require_once('/etc/inc/captiveportal.inc');
40
	require_once('/etc/inc/voucher.inc');
41
	\$cpzone = "$cpzone";
42
	voucher_expire("$vouchers");
43

    
44
EOF;
45
	$rpc_client = new pfsense_xmlrpc_client();
46
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
47
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
48
	if (empty($resp)) {
49
		return false;
50
	}
51
	return $resp;
52
}
53

    
54
function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $username, $term_cause = 1, $stop_time = null) {
55
	global $cpzone;
56
	require_once("xmlrpc_client.inc");
57
	/* Construct code that is run on remote machine */
58
	$dbent_str = addslashes(serialize($dbent));
59
	$tmp_stop_time = (isset($stop_time)) ? $stop_time : "null";
60
	$execcmd = <<<EOF
61
	global \$cpzone;
62
	require_once('/etc/inc/captiveportal.inc');
63
	require_once('/etc/inc/voucher.inc');
64
	\$cpzone = "$cpzone";
65
	\$radiusservers = captiveportal_get_radius_servers();
66
	\$dbent = unserialize("$dbent_str");
67
	captiveportal_disconnect(\$dbent, \$radiusservers, $term_cause, $tmp_stop_time);
68

    
69
EOF;
70
	$rpc_client = new pfsense_xmlrpc_client();
71
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
72
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
73
	if (empty($resp)) {
74
		return false;
75
	}
76
	return $resp;
77
}
78

    
79
function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
80
	global $config, $cpzone;
81
	require_once("xmlrpc_client.inc");
82

    
83
	/* Construct code that is run on remote machine */
84
	$execcmd = <<<EOF
85
	global \$cpzone;
86
	require_once('/etc/inc/voucher.inc');
87
	\$cpzone = "$cpzone";
88
	\$timeleft = voucher_auth("$voucher_received");
89
	\$toreturn = array();
90
	\$toreturn['timeleft'] = \$timeleft;
91
	\$toreturn['voucher'] = array();
92
	\$toreturn['voucher']['roll'] = \$config['voucher'][\$cpzone]['roll'];
93

    
94
EOF;
95
	$rpc_client = new pfsense_xmlrpc_client();
96
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
97
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
98
	
99
	if (!is_array($config['voucher'])) {
100
		$config['voucher'] = array();
101
	}
102

    
103
	if (is_array($resp['voucher']['roll'])) {
104
		$config['voucher'][$cpzone]['roll'] = $resp['voucher']['roll'];
105
		write_config(sprintf(gettext("Captive Portal Voucher database synchronized with %s:%s"), $syncip, $port));
106
		voucher_configure_zone(true);
107
		unset($resp['voucher']);
108
	} else if (!isset($resp['timeleft'])) {
109
		return null;
110
	}
111

    
112
	return $resp['timeleft'];
113
}
114

    
115
function voucher_expire($voucher_received) {
116
	global $g, $config, $cpzone, $cpzoneid;
117

    
118
	// XMLRPC Call over to the master Voucher node
119
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
120
		$syncip   = $config['voucher'][$cpzone]['vouchersyncdbip'];
121
		$syncport = $config['voucher'][$cpzone]['vouchersyncport'];
122
		$syncpass = $config['voucher'][$cpzone]['vouchersyncpass'];
123
		$vouchersyncusername = $config['voucher'][$cpzone]['vouchersyncusername'];
124
		xmlrpc_sync_voucher_expire($voucher_received, $syncip, $syncport, $syncpass, $vouchersyncusername);
125
	}
126

    
127
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
128

    
129
	// read rolls into assoc array with rollid as key and minutes as value
130
	$tickets_per_roll = array();
131
	$minutes_per_roll = array();
132
	if (is_array($config['voucher'][$cpzone]['roll'])) {
133
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
134
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
135
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
136
		}
137
	}
138

    
139
	// split into an array. Useful for multiple vouchers given
140
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
141
	$active_dirty = false;
142
	$unsetindexes = array();
143

    
144
	// go through all received vouchers, check their valid and extract
145
	// Roll# and Ticket# using the external readvoucher binary
146
	foreach ($a_vouchers_received as $voucher) {
147
		$v = escapeshellarg($voucher);
148
		if (strlen($voucher) < 5) {
149
			captiveportal_syslog("${voucher} invalid: Too short!");
150
			continue;   // seems too short to be a voucher!
151
		}
152

    
153
		unset($output);
154
		$_gb = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v", $output);
155
		list($status, $roll, $nr) = explode(" ", $output[0]);
156
		if ($status == "OK") {
157
			// check if we have this ticket on a registered roll for this ticket
158
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
159
				// voucher is from a registered roll.
160
				if (!isset($active_vouchers[$roll])) {
161
					$active_vouchers[$roll] = voucher_read_active_db($roll);
162
				}
163
				// valid voucher. Store roll# and ticket#
164
				if (!empty($active_vouchers[$roll][$voucher])) {
165
					$active_dirty = true;
166
					unset($active_vouchers[$roll][$voucher]);
167
				}
168
				// check if voucher already marked as used
169
				if (!isset($bitstring[$roll])) {
170
					$bitstring[$roll] = voucher_read_used_db($roll);
171
				}
172
				$pos = $nr >> 3; // divide by 8 -> octet
173
				$mask = 1 << ($nr % 8);
174
				// mark bit for this voucher as used
175
				if (!(ord($bitstring[$roll][$pos]) & $mask)) {
176
					$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
177
				}
178
				captiveportal_syslog("{$voucher} ({$roll}/{$nr}) forced to expire");
179

    
180
				/* Check if this voucher has any active sessions */
181
				$cpentry = captiveportal_read_db("WHERE username = '{$voucher}'");
182
				if (!empty($cpentry) && !empty($cpentry[0])) {
183
					if (empty($cpzoneid) && !empty($config['captiveportal'][$cpzone])) {
184
						$cpzoneid = $config['captiveportal'][$cpzone]['zoneid'];
185
					}
186
					$cpentry = $cpentry[0];
187
					captiveportal_disconnect($cpentry, null, 13);
188
					captiveportal_logportalauth($cpentry[4], $cpentry[3], $cpentry[2], "FORCLY TERMINATING VOUCHER {$voucher} SESSION");
189
					$unsetindexes[] = $cpentry[5];
190
				}
191
			} else {
192
				captiveportal_syslog(sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr));
193
			}
194
		} else {
195
			// hmm, thats weird ... not what I expected
196
			captiveportal_syslog(sprintf(gettext('%1$s invalid: %2$s!!'), $voucher, $output[0]));
197
		}
198
	}
199

    
200
	// Refresh active DBs
201
	if ($active_dirty == true) {
202
		foreach ($active_vouchers as $roll => $active) {
203
			voucher_write_active_db($roll, $active);
204
		}
205
		unset($active_vouchers);
206

    
207
		/* Trigger a sync of the vouchers on config */
208
		send_event("service sync vouchers");
209
	}
210

    
211
	// Write back the used DB's
212
	if (is_array($bitstring)) {
213
		foreach ($bitstring as $roll => $used) {
214
			if (is_array($used)) {
215
				foreach ($used as $u) {
216
					voucher_write_used_db($roll, base64_encode($u));
217
				}
218
			} else {
219
				voucher_write_used_db($roll, base64_encode($used));
220
			}
221
		}
222
		unset($bitstring);
223
	}
224

    
225
	unlock($voucherlck);
226

    
227
	/* Write database */
228
	if (!empty($unsetindexes)) {
229
		captiveportal_remove_entries($unsetindexes);
230
	}
231

    
232
	return true;
233
}
234

    
235
/*
236
 * Authenticate a voucher and return the remaining time credit in minutes
237
 * if $test is set, don't mark the voucher as used nor add it to the list
238
 * of active vouchers
239
 * If $test is set, simply test the voucher. Don't change anything
240
 * but return a more verbose error and result message back
241
 */
242
function voucher_auth($voucher_received, $test = 0) {
243
	global $g, $config, $cpzone, $dbc;
244

    
245
	if (!isset($config['voucher'][$cpzone]['enable'])) {
246
		return 0;
247
	}
248

    
249
	// XMLRPC Call over to the master Voucher node
250
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
251
		$syncip   = $config['voucher'][$cpzone]['vouchersyncdbip'];
252
		$syncport = $config['voucher'][$cpzone]['vouchersyncport'];
253
		$syncpass = $config['voucher'][$cpzone]['vouchersyncpass'];
254
		$vouchersyncusername = $config['voucher'][$cpzone]['vouchersyncusername'];
255
		$remote_time_used = xmlrpc_sync_used_voucher($voucher_received, $syncip, $syncport, $syncpass, $vouchersyncusername);
256
	}
257

    
258
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
259

    
260
	// read rolls into assoc array with rollid as key and minutes as value
261
	$tickets_per_roll = array();
262
	$minutes_per_roll = array();
263
	if (is_array($config['voucher'][$cpzone]['roll'])) {
264
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
265
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
266
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
267
		}
268
	}
269

    
270
	// split into an array. Useful for multiple vouchers given
271
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
272
	$error = 0;
273
	$test_result = array();     // used to display for voucher test option in GUI
274
	$total_minutes = 0;
275
	$first_voucher = "";
276
	$first_voucher_roll = 0;
277

    
278
	// go through all received vouchers, check their valid and extract
279
	// Roll# and Ticket# using the external readvoucher binary
280
	foreach ($a_vouchers_received as $voucher) {
281
		$v = escapeshellarg($voucher);
282
		if (strlen($voucher) < 5) {
283
			$voucher_err_text = sprintf(gettext("%s invalid: Too short!"), $voucher);
284
			$test_result[] = $voucher_err_text;
285
			captiveportal_syslog($voucher_err_text);
286
			$error++;
287
			continue;   // seems too short to be a voucher!
288
		}
289

    
290
		$result = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v");
291
		list($status, $roll, $nr) = explode(" ", $result);
292
		if ($status == "OK") {
293
			if (!$first_voucher) {
294
				// store first voucher. Thats the one we give the timecredit
295
				$first_voucher = $voucher;
296
				$first_voucher_roll = $roll;
297
			}
298
			// check if we have this ticket on a registered roll for this ticket
299
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
300
				// voucher is from a registered roll.
301
				if (!isset($active_vouchers[$roll])) {
302
					$active_vouchers[$roll] = voucher_read_active_db($roll);
303
				}
304
				// valid voucher. Store roll# and ticket#
305
				if (!empty($active_vouchers[$roll][$voucher])) {
306
					list($timestamp, $minutes) = explode(",", $active_vouchers[$roll][$voucher]);
307
					// we have an already active voucher here.
308
					$remaining = intval((($timestamp + (60*$minutes)) - time())/60);
309
					$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) active and good for %4$d Minutes'), $voucher, $roll, $nr, $remaining);
310
					$total_minutes += $remaining;
311
				} else {
312
					// voucher not used. Check if ticket Id is on the roll (not too high)
313
					// and if the ticket is marked used.
314
					// check if voucher already marked as used
315
					if (!isset($bitstring[$roll])) {
316
						$bitstring[$roll] = voucher_read_used_db($roll);
317
					}
318
					$pos = $nr >> 3; // divide by 8 -> octet
319
					$mask = 1 << ($nr % 8);
320
					if (ord($bitstring[$roll][$pos]) & $mask) {
321
						$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s) already used and expired'), $voucher, $roll, $nr);
322
						$test_result[] = $voucher_err_text;
323
						captiveportal_syslog($voucher_err_text);
324
						$total_minutes = -1;    // voucher expired
325
						$error++;
326
					} else {
327
						// mark bit for this voucher as used
328
						$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
329
						$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) good for %4$s Minutes'), $voucher, $roll, $nr, $minutes_per_roll[$roll]);
330
						$total_minutes += $minutes_per_roll[$roll];
331
					}
332
				}
333
			} else {
334
				$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr);
335
				$test_result[] = $voucher_err_text;
336
				captiveportal_syslog($voucher_err_text);
337
			}
338
		} else {
339
			// hmm, thats weird ... not what I expected
340
			$voucher_err_text = sprintf(gettext('%1$s invalid: %2$s !!'), $voucher, $result);
341
			$test_result[] = $voucher_err_text;
342
			captiveportal_syslog($voucher_err_text);
343
			$error++;
344
		}
345
	}
346

    
347
	// if this was a test call, we're done. Return the result.
348
	if ($test) {
349
		if ($error) {
350
			$test_result[] = gettext("Access denied!");
351
		} else {
352
			$test_result[] = sprintf(gettext("Access granted for %d Minutes in total."), $total_minutes);
353
		}
354
		unlock($voucherlck);
355

    
356
		return $test_result;
357
	}
358

    
359
	// if we had an error (one of the vouchers is invalid), return 0.
360
	// Discussion: we could return the time remaining for good vouchers, but then
361
	// the user wouldn't know that he used at least one invalid voucher.
362
	if ($error) {
363
		unlock($voucherlck);
364
		if ($total_minutes > 0) {   // probably not needed, but want to make sure
365
			$total_minutes = 0;     // we only report -1 (expired) or 0 (no access)
366
		}
367
		return $total_minutes;       // well, at least one voucher had errors. Say NO ACCESS
368
	}
369

    
370
	// If we did a XMLRPC sync earlier check the timeleft
371
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
372
		if (!is_null($remote_time_used)) {
373
			$total_minutes = $remote_time_used;
374
		} else if ($remote_time_used < $total_minutes) {
375
			$total_minutes -= $remote_time_used;
376
		}
377
	}
378

    
379
	// All given vouchers were valid and this isn't simply a test.
380
	// Write back the used DB's
381
	if (is_array($bitstring)) {
382
		foreach ($bitstring as $roll => $used) {
383
			if (is_array($used)) {
384
				foreach ($used as $u) {
385
					voucher_write_used_db($roll, base64_encode($u));
386
				}
387
			} else {
388
				voucher_write_used_db($roll, base64_encode($used));
389
			}
390
		}
391
	}
392

    
393
	// Active DB: we only add the first voucher if multiple given
394
	// and give that one all the time credit. This allows the user to logout and
395
	// log in later using just the first voucher. It also keeps username limited
396
	// to one voucher and that voucher shows the correct time credit in 'active vouchers'
397
	if (!empty($active_vouchers[$first_voucher_roll][$first_voucher])) {
398
		list($timestamp, $minutes) = explode(",", $active_vouchers[$first_voucher_roll][$first_voucher]);
399
	} else {
400
		$timestamp = time();    // new voucher
401
		$minutes = $total_minutes;
402
	}
403

    
404
	$active_vouchers[$first_voucher_roll][$first_voucher] = "$timestamp,$minutes";
405
	voucher_write_active_db($first_voucher_roll, $active_vouchers[$first_voucher_roll]);
406

    
407
	/* Trigger a sync of the vouchers on config */
408
	send_event("service sync vouchers");
409

    
410
	unlock($voucherlck);
411

    
412
	return $total_minutes;
413
}
414

    
415
function voucher_configure($sync = false) {
416
	global $config, $g, $cpzone;
417

    
418
	if (is_array($config['voucher'])) {
419
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
420
			if (platform_booting()) {
421
				echo gettext("Enabling voucher support... ");
422
			}
423
			$cpzone = $voucherzone;
424
			$error = voucher_configure_zone($sync);
425
			if (platform_booting()) {
426
				if ($error) {
427
					echo "error\n";
428
				} else {
429
					echo "done\n";
430
				}
431
			}
432
		}
433
	}
434
}
435

    
436
function voucher_configure_zone($sync = false) {
437
	global $config, $g, $cpzone;
438

    
439
	if (!isset($config['voucher'][$cpzone]['enable'])) {
440
		return 0;
441
	}
442

    
443
	if ($sync == true) {
444
		captiveportal_syslog("Writing voucher db from sync data...");
445
	}
446

    
447
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
448

    
449
	/* write public key used to verify vouchers */
450
	$pubkey = base64_decode($config['voucher'][$cpzone]['publickey']);
451
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.public", "w");
452
	if (!$fd) {
453
		captiveportal_syslog("Voucher error: cannot write voucher.public\n");
454
		unlock($voucherlck);
455
		return 1;
456
	}
457
	fwrite($fd, $pubkey);
458
	fclose($fd);
459
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.public", 0600);
460

    
461
	/* write config file used by voucher binary to decode vouchers */
462
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.cfg", "w");
463
	if (!$fd) {
464
		printf(gettext("Error: cannot write voucher.cfg") . "\n");
465
		unlock($voucherlck);
466
		return 1;
467
	}
468
	fwrite($fd, "{$config['voucher'][$cpzone]['rollbits']},{$config['voucher'][$cpzone]['ticketbits']},{$config['voucher'][$cpzone]['checksumbits']},{$config['voucher'][$cpzone]['magic']},{$config['voucher'][$cpzone]['charset']}\n");
469
	fclose($fd);
470
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.cfg", 0600);
471
	unlock($voucherlck);
472

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

    
475
		$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
476

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

    
480
			$roll = $rollent['number'];
481
			$len = ($rollent['count'] >> 3) + 1;
482
			if (strlen(base64_decode($rollent['used'])) != $len) {
483
				$rollent['used'] = base64_encode(str_repeat("\000", $len));
484
			}
485
			voucher_write_used_db($roll, $rollent['used']);
486
			$minutes = $rollent['minutes'];
487
			$active_vouchers = array();
488
			$a_active = &$rollent['active'];
489
			if (is_array($a_active)) {
490
				foreach ($a_active as $activent) {
491
					$voucher = $activent['voucher'];
492
					$timestamp = $activent['timestamp'];
493
					$minutes = $activent['minutes'];
494
					// its tempting to check for expired timestamps, but during
495
					// bootup, we most likely don't have the correct time.
496
					$active_vouchers[$voucher] = "$timestamp,$minutes";
497
				}
498
			}
499
			voucher_write_active_db($roll, $active_vouchers);
500
		}
501

    
502
		unlock($voucherlck);
503
	}
504

    
505
	return 0;
506
}
507

    
508
/* write bitstring of used vouchers to ramdisk.
509
 * Bitstring must already be base64_encoded!
510
 */
511
function voucher_write_used_db($roll, $vdb) {
512
	global $g, $cpzone;
513

    
514
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db", "w");
515
	if ($fd) {
516
		fwrite($fd, $vdb . "\n");
517
		fclose($fd);
518
	} else {
519
		voucher_log(LOG_ERR, sprintf(gettext('cant write %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
520
	}
521
}
522

    
523
/* return assoc array of active vouchers with activation timestamp
524
 * voucher is index.
525
 */
526
function voucher_read_active_db($roll) {
527
	global $g, $cpzone;
528

    
529
	$active = array();
530
	$dirty = 0;
531
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db";
532
	if (file_exists($file)) {
533
		$fd = fopen($file, "r");
534
		if ($fd) {
535
			while (!feof($fd)) {
536
				$line = trim(fgets($fd));
537
				if ($line) {
538
					list($voucher, $timestamp, $minutes) = explode(",", $line); // voucher,timestamp
539
					if ((($timestamp + (60*$minutes)) - time()) > 0) {
540
						$active[$voucher] = "$timestamp,$minutes";
541
					} else {
542
						$dirty=1;
543
					}
544
				}
545
			}
546
			fclose($fd);
547
			if ($dirty) { // if we found expired entries, lets save our snapshot
548
				voucher_write_active_db($roll, $active);
549

    
550
				/* Trigger a sync of the vouchers on config */
551
				send_event("service sync vouchers");
552
			}
553
		}
554
	}
555
	return $active;
556
}
557

    
558
/* store array of active vouchers back to DB */
559
function voucher_write_active_db($roll, $active) {
560
	global $g, $cpzone;
561

    
562
	if (!is_array($active)) {
563
		return;
564
	}
565
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db", "w");
566
	if ($fd) {
567
		foreach ($active as $voucher => $value) {
568
			fwrite($fd, "$voucher,$value\n");
569
		}
570
		fclose($fd);
571
	}
572
}
573

    
574
/* return how many vouchers are marked used on a roll */
575
function voucher_used_count($roll) {
576
	global $g, $cpzone;
577

    
578
	$bitstring = voucher_read_used_db($roll);
579
	$max = strlen($bitstring) * 8;
580
	$used = 0;
581
	for ($i = 1; $i <= $max; $i++) {
582
		// check if ticket already used or not.
583
		$pos = $i >> 3;            // divide by 8 -> octet
584
		$mask = 1 << ($i % 8);  // mask to test bit in octet
585
		if (ord($bitstring[$pos]) & $mask) {
586
			$used++;
587
		}
588
	}
589
	unset($bitstring);
590

    
591
	return $used;
592
}
593

    
594
function voucher_read_used_db($roll) {
595
	global $g, $cpzone;
596

    
597
	$vdb = "";
598
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db";
599
	if (file_exists($file)) {
600
		$fd = fopen($file, "r");
601
		if ($fd) {
602
			$vdb = trim(fgets($fd));
603
			fclose($fd);
604
		} else {
605
			voucher_log(LOG_ERR, sprintf(gettext('cant read %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
606
		}
607
	}
608
	return base64_decode($vdb);
609
}
610

    
611
function voucher_unlink_db($roll) {
612
	global $g, $cpzone;
613
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db");
614
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db");
615
}
616

    
617
/* we share the log with captiveportal for now */
618
function voucher_log($priority, $message) {
619

    
620
	$message = trim($message);
621
	openlog("logportalauth", LOG_PID, LOG_LOCAL4);
622
	syslog($priority, sprintf(gettext("Voucher: %s"), $message));
623
	closelog();
624
}
625

    
626
/* Save active and used voucher DB into XML config and write it to flash
627
 * Called during reboot -> system_reboot_cleanup() and every active voucher change
628
 */
629
function voucher_save_db_to_config() {
630
	global $config, $g, $cpzone;
631

    
632
	if (is_array($config['voucher'])) {
633
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
634
			$cpzone = $voucherzone;
635
			voucher_save_db_to_config_zone();
636
		}
637
	}
638
}
639

    
640
function voucher_save_db_to_config_zone() {
641
	global $config, $g, $cpzone;
642

    
643
	if (!isset($config['voucher'][$cpzone]['enable'])) {
644
		return;   // no vouchers or don't want to save DB's
645
	}
646

    
647
	if (!is_array($config['voucher'][$cpzone]['roll'])) {
648
		return;
649
	}
650

    
651
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
652

    
653
	// walk all active rolls and save runtime DB's to flash
654
	$a_roll = &$config['voucher'][$cpzone]['roll'];
655
	while (list($key, $value) = each($a_roll)) {
656
		$rollent = &$a_roll[$key];
657
		$roll = $rollent['number'];
658
		$bitmask = voucher_read_used_db($roll);
659
		$rollent['used'] = base64_encode($bitmask);
660
		$active_vouchers = voucher_read_active_db($roll);
661
		$db = array();
662
		$dbi = 1;
663
		foreach ($active_vouchers as $voucher => $line) {
664
			list($timestamp, $minutes) = explode(",", $line);
665
			$activent['voucher'] = $voucher;
666
			$activent['timestamp'] = $timestamp;
667
			$activent['minutes'] = $minutes;
668
			$db["v{$dbi}"] = $activent;
669
			$dbi++;
670
		}
671
		$rollent['active'] = $db;
672
		unset($active_vouchers);
673
	}
674

    
675
	unlock($voucherlck);
676

    
677
	write_config(gettext("Syncing vouchers"));
678
	return;
679
}
680

    
681
?>
(44-44/51)