Project

General

Profile

Download (22.4 KB) Statistics
| Branch: | Tag: | Revision:
1 336e3c1c Charlie
<?php
2
/*
3 ac24dc24 Renato Botelho
 * voucher.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 4a762cf0 Steve Beaver
 * Copyright (c) 2007-2019 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * Copyright (c) 2007 Marcel Wiget <mwiget@mac.com>
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10
 * originally part of m0n0wall (http://m0n0.ch/wall)
11 c5d81585 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12 ac24dc24 Renato Botelho
 * All rights reserved.
13
 *
14 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
19 ac24dc24 Renato Botelho
 *
20 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
26 523855b0 Scott Ullrich
27 336e3c1c Charlie
/* include all configuration functions */
28 79262830 Phil Davis
if (!function_exists('captiveportal_syslog')) {
29 4e8d55dd Scott Ullrich
	require_once("captiveportal.inc");
30 79262830 Phil Davis
}
31 336e3c1c Charlie
32 b8963db6 Renato Botelho
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 05771a24 Ermal
function xmlrpc_sync_voucher_expire($vouchers, $syncip, $port, $password, $username) {
71 a8620841 PiBa-NL
	global $cpzone;
72
	require_once("xmlrpc_client.inc");
73 179377b0 robjarsen
74 05771a24 Ermal
	/* Construct code that is run on remote machine */
75 6c07db48 Phil Davis
	$execcmd = <<<EOF
76 fac36dfd Ermal
	global \$cpzone;
77 05771a24 Ermal
	require_once('/etc/inc/captiveportal.inc');
78
	require_once('/etc/inc/voucher.inc');
79 a18eeb51 bcyrill
	\$cpzone = "$cpzone";
80 927a988e jim-p
	return voucher_expire("$vouchers");
81 05771a24 Ermal
82
EOF;
83 e3b0eeb2 PiBa-NL
	$rpc_client = new pfsense_xmlrpc_client();
84
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
85 a8620841 PiBa-NL
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
86
	if (empty($resp)) {
87 200728c9 Renato Botelho
		return false;
88
	}
89
	return $resp;
90 05771a24 Ermal
}
91
92 a18eeb51 bcyrill
function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $username, $term_cause = 1, $stop_time = null) {
93 a8620841 PiBa-NL
	global $cpzone;
94
	require_once("xmlrpc_client.inc");
95 d322e3b3 Scott Ullrich
	/* Construct code that is run on remote machine */
96 a8620841 PiBa-NL
	$dbent_str = addslashes(serialize($dbent));
97 bbe29bb1 bcyrill
	$tmp_stop_time = (isset($stop_time)) ? $stop_time : "null";
98 6c07db48 Phil Davis
	$execcmd = <<<EOF
99 fac36dfd Ermal
	global \$cpzone;
100 d322e3b3 Scott Ullrich
	require_once('/etc/inc/captiveportal.inc');
101
	require_once('/etc/inc/voucher.inc');
102 a18eeb51 bcyrill
	\$cpzone = "$cpzone";
103
	\$dbent = unserialize("$dbent_str");
104 eb43c5b1 Augustin FL
	return captiveportal_disconnect(\$dbent, $term_cause, $tmp_stop_time);
105 d322e3b3 Scott Ullrich
106
EOF;
107 e3b0eeb2 PiBa-NL
	$rpc_client = new pfsense_xmlrpc_client();
108
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
109 a8620841 PiBa-NL
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
110
	if (empty($resp)) {
111 200728c9 Renato Botelho
		return false;
112
	}
113
	return $resp;
114 d322e3b3 Scott Ullrich
}
115
116 6c9ca678 Scott Ullrich
function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
117 a8620841 PiBa-NL
	global $config, $cpzone;
118
	require_once("xmlrpc_client.inc");
119 6c9ca678 Scott Ullrich
120
	/* Construct code that is run on remote machine */
121 6c07db48 Phil Davis
	$execcmd = <<<EOF
122 927a988e jim-p
	global \$cpzone, \$config;
123 6c9ca678 Scott Ullrich
	require_once('/etc/inc/voucher.inc');
124 a18eeb51 bcyrill
	\$cpzone = "$cpzone";
125
	\$timeleft = voucher_auth("$voucher_received");
126 6c9ca678 Scott Ullrich
	\$toreturn = array();
127
	\$toreturn['timeleft'] = \$timeleft;
128 90df43c6 Ermal
	\$toreturn['voucher'] = array();
129 927a988e jim-p
	if (is_array(\$config['voucher'][\$cpzone]['roll'])) {
130
		\$toreturn['voucher']['roll'] = \$config['voucher'][\$cpzone]['roll'];
131
	}
132 6c9ca678 Scott Ullrich
133 fa3ab36a Scott Ullrich
EOF;
134 e3b0eeb2 PiBa-NL
	$rpc_client = new pfsense_xmlrpc_client();
135
	$rpc_client->setConnectionData($syncip, $port, $username, $password);
136 a8620841 PiBa-NL
	$resp = $rpc_client->xmlrpc_exec_php($execcmd);
137 179377b0 robjarsen
138 79262830 Phil Davis
	if (!is_array($config['voucher'])) {
139 90df43c6 Ermal
		$config['voucher'] = array();
140 79262830 Phil Davis
	}
141 62f96568 Ermal
142 200728c9 Renato Botelho
	if (is_array($resp['voucher']['roll'])) {
143
		$config['voucher'][$cpzone]['roll'] = $resp['voucher']['roll'];
144 1579e70f Phil Davis
		write_config(sprintf(gettext('Captive Portal Voucher database synchronized with %1$s:%2$s'), $syncip, $port));
145 b4792bf8 Ermal
		voucher_configure_zone(true);
146 200728c9 Renato Botelho
		unset($resp['voucher']);
147
	} else if (!isset($resp['timeleft'])) {
148 927a988e jim-p
		return 0;
149 79262830 Phil Davis
	}
150 f5c05fcc Ermal
151 200728c9 Renato Botelho
	return $resp['timeleft'];
152 fa3ab36a Scott Ullrich
}
153
154 05771a24 Ermal
function voucher_expire($voucher_received) {
155 abaa7feb Ermal LUÇI
	global $g, $config, $cpzone, $cpzoneid;
156 05771a24 Ermal
157
	// XMLRPC Call over to the master Voucher node
158 b8963db6 Renato Botelho
	if (xmlrpc_sync_voucher_details($syncip, $syncport,
159
	    $vouchersyncusername, $syncpass)) {
160
		xmlrpc_sync_voucher_expire($voucher_received, $syncip,
161
		    $syncport, $syncpass, $vouchersyncusername);
162 05771a24 Ermal
	}
163
164 90df43c6 Ermal
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
165
166 05771a24 Ermal
	// read rolls into assoc array with rollid as key and minutes as value
167
	$tickets_per_roll = array();
168
	$minutes_per_roll = array();
169 b4792bf8 Ermal
	if (is_array($config['voucher'][$cpzone]['roll'])) {
170
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
171 05771a24 Ermal
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
172
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
173
		}
174
	}
175
176
	// split into an array. Useful for multiple vouchers given
177 79262830 Phil Davis
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
178 05771a24 Ermal
	$active_dirty = false;
179 5a6359ae bcyrill
	$unsetindexes = array();
180 2c85b316 Ermal
181 05771a24 Ermal
	// go through all received vouchers, check their valid and extract
182
	// Roll# and Ticket# using the external readvoucher binary
183
	foreach ($a_vouchers_received as $voucher) {
184
		$v = escapeshellarg($voucher);
185 9fcbc9ff Luiz Otavio O Souza
		if (strlen($voucher) < 5) {
186
			captiveportal_syslog("${voucher} invalid: Too short!");
187 05771a24 Ermal
			continue;   // seems too short to be a voucher!
188 79262830 Phil Davis
		}
189 05771a24 Ermal
190 15bec718 Ermal
		unset($output);
191
		$_gb = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v", $output);
192
		list($status, $roll, $nr) = explode(" ", $output[0]);
193 05771a24 Ermal
		if ($status == "OK") {
194 79262830 Phil Davis
			// check if we have this ticket on a registered roll for this ticket
195 05771a24 Ermal
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
196 79262830 Phil Davis
				// voucher is from a registered roll.
197
				if (!isset($active_vouchers[$roll])) {
198 05771a24 Ermal
					$active_vouchers[$roll] = voucher_read_active_db($roll);
199 79262830 Phil Davis
				}
200 05771a24 Ermal
				// valid voucher. Store roll# and ticket#
201
				if (!empty($active_vouchers[$roll][$voucher])) {
202
					$active_dirty = true;
203
					unset($active_vouchers[$roll][$voucher]);
204
				}
205
				// check if voucher already marked as used
206 79262830 Phil Davis
				if (!isset($bitstring[$roll])) {
207 05771a24 Ermal
					$bitstring[$roll] = voucher_read_used_db($roll);
208 79262830 Phil Davis
				}
209 05771a24 Ermal
				$pos = $nr >> 3; // divide by 8 -> octet
210
				$mask = 1 << ($nr % 8);
211
				// mark bit for this voucher as used
212 79262830 Phil Davis
				if (!(ord($bitstring[$roll][$pos]) & $mask)) {
213 05771a24 Ermal
					$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
214 79262830 Phil Davis
				}
215 05771a24 Ermal
				captiveportal_syslog("{$voucher} ({$roll}/{$nr}) forced to expire");
216 2c85b316 Ermal
217
				/* Check if this voucher has any active sessions */
218 26ee5aaf Ermal
				$cpentry = captiveportal_read_db("WHERE username = '{$voucher}'");
219 ea6cbc39 Gertjan
				if (!empty($cpentry) && !empty($cpentry[0])) {
220 086cf944 Phil Davis
					if (empty($cpzoneid) && !empty($config['captiveportal'][$cpzone])) {
221 abaa7feb Ermal LUÇI
						$cpzoneid = $config['captiveportal'][$cpzone]['zoneid'];
222 086cf944 Phil Davis
					}
223 ea6cbc39 Gertjan
					$cpentry = $cpentry[0];
224 eb43c5b1 Augustin FL
					captiveportal_disconnect($cpentry, 13);
225 086cf944 Phil Davis
					captiveportal_logportalauth($cpentry[4], $cpentry[3], $cpentry[2], "FORCLY TERMINATING VOUCHER {$voucher} SESSION");
226 5705c60a Renato Botelho
					$unsetindexes[] = $cpentry[5];
227 2c85b316 Ermal
				}
228 79262830 Phil Davis
			} else {
229 51a14c58 Phil Davis
				captiveportal_syslog(sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr));
230 79262830 Phil Davis
			}
231
		} else {
232 05771a24 Ermal
			// hmm, thats weird ... not what I expected
233 51a14c58 Phil Davis
			captiveportal_syslog(sprintf(gettext('%1$s invalid: %2$s!!'), $voucher, $output[0]));
234 79262830 Phil Davis
		}
235 05771a24 Ermal
	}
236
237
	// Refresh active DBs
238
	if ($active_dirty == true) {
239 79262830 Phil Davis
		foreach ($active_vouchers as $roll => $active) {
240 05771a24 Ermal
			voucher_write_active_db($roll, $active);
241 79262830 Phil Davis
		}
242 15bec718 Ermal
		unset($active_vouchers);
243 5ebe85e9 Ermal
244 f416763b Phil Davis
		/* Trigger a sync of the vouchers on config */
245 5ebe85e9 Ermal
		send_event("service sync vouchers");
246 05771a24 Ermal
	}
247
248
	// Write back the used DB's
249
	if (is_array($bitstring)) {
250
		foreach ($bitstring as $roll => $used) {
251 79262830 Phil Davis
			if (is_array($used)) {
252
				foreach ($used as $u) {
253 05771a24 Ermal
					voucher_write_used_db($roll, base64_encode($u));
254 79262830 Phil Davis
				}
255 05771a24 Ermal
			} else {
256
				voucher_write_used_db($roll, base64_encode($used));
257
			}
258
		}
259 15bec718 Ermal
		unset($bitstring);
260 05771a24 Ermal
	}
261
262
	unlock($voucherlck);
263
264 2c85b316 Ermal
	/* Write database */
265 79262830 Phil Davis
	if (!empty($unsetindexes)) {
266 26ee5aaf Ermal
		captiveportal_remove_entries($unsetindexes);
267 79262830 Phil Davis
	}
268 2c85b316 Ermal
269 05771a24 Ermal
	return true;
270
}
271
272 79262830 Phil Davis
/*
273 666bc4d1 Ermal
 * Authenticate a voucher and return the remaining time credit in minutes
274 336e3c1c Charlie
 * if $test is set, don't mark the voucher as used nor add it to the list
275
 * of active vouchers
276 666bc4d1 Ermal
 * If $test is set, simply test the voucher. Don't change anything
277
 * but return a more verbose error and result message back
278 336e3c1c Charlie
 */
279
function voucher_auth($voucher_received, $test = 0) {
280 b4792bf8 Ermal
	global $g, $config, $cpzone, $dbc;
281 336e3c1c Charlie
282 79262830 Phil Davis
	if (!isset($config['voucher'][$cpzone]['enable'])) {
283 1a863be2 Ermal
		return 0;
284 79262830 Phil Davis
	}
285 1a863be2 Ermal
286 6c9ca678 Scott Ullrich
	// XMLRPC Call over to the master Voucher node
287 b8963db6 Renato Botelho
	if (xmlrpc_sync_voucher_details($syncip, $syncport,
288
	    $vouchersyncusername, $syncpass)) {
289
		$remote_time_used = xmlrpc_sync_used_voucher($voucher_received,
290
		    $syncip, $syncport, $syncpass, $vouchersyncusername);
291 6c9ca678 Scott Ullrich
	}
292 fa3ab36a Scott Ullrich
293 90df43c6 Ermal
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
294
295 7afb7ea9 Ermal
	// read rolls into assoc array with rollid as key and minutes as value
296
	$tickets_per_roll = array();
297
	$minutes_per_roll = array();
298 b4792bf8 Ermal
	if (is_array($config['voucher'][$cpzone]['roll'])) {
299
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
300 7afb7ea9 Ermal
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
301
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
302
		}
303
	}
304 336e3c1c Charlie
305 05771a24 Ermal
	// split into an array. Useful for multiple vouchers given
306 79262830 Phil Davis
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
307 05771a24 Ermal
	$error = 0;
308
	$test_result = array();     // used to display for voucher test option in GUI
309
	$total_minutes = 0;
310
	$first_voucher = "";
311
	$first_voucher_roll = 0;
312
313
	// go through all received vouchers, check their valid and extract
314
	// Roll# and Ticket# using the external readvoucher binary
315
	foreach ($a_vouchers_received as $voucher) {
316
		$v = escapeshellarg($voucher);
317 9fcbc9ff Luiz Otavio O Souza
		if (strlen($voucher) < 5) {
318 51a14c58 Phil Davis
			$voucher_err_text = sprintf(gettext("%s invalid: Too short!"), $voucher);
319
			$test_result[] = $voucher_err_text;
320
			captiveportal_syslog($voucher_err_text);
321 b08758c3 jim-p
			$error++;
322 05771a24 Ermal
			continue;   // seems too short to be a voucher!
323 79262830 Phil Davis
		}
324 05771a24 Ermal
325 b4792bf8 Ermal
		$result = exec("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher_{$cpzone}.cfg -k {$g['varetc_path']}/voucher_{$cpzone}.public -- $v");
326 05771a24 Ermal
		list($status, $roll, $nr) = explode(" ", $result);
327
		if ($status == "OK") {
328
			if (!$first_voucher) {
329
				// store first voucher. Thats the one we give the timecredit
330
				$first_voucher = $voucher;
331
				$first_voucher_roll = $roll;
332
			}
333 79262830 Phil Davis
			// check if we have this ticket on a registered roll for this ticket
334 05771a24 Ermal
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
335 79262830 Phil Davis
				// voucher is from a registered roll.
336
				if (!isset($active_vouchers[$roll])) {
337 05771a24 Ermal
					$active_vouchers[$roll] = voucher_read_active_db($roll);
338 79262830 Phil Davis
				}
339 05771a24 Ermal
				// valid voucher. Store roll# and ticket#
340
				if (!empty($active_vouchers[$roll][$voucher])) {
341 086cf944 Phil Davis
					list($timestamp, $minutes) = explode(",", $active_vouchers[$roll][$voucher]);
342 05771a24 Ermal
					// we have an already active voucher here.
343
					$remaining = intval((($timestamp + (60*$minutes)) - time())/60);
344 d8012adb Vinicius Coque
					$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) active and good for %4$d Minutes'), $voucher, $roll, $nr, $remaining);
345 05771a24 Ermal
					$total_minutes += $remaining;
346
				} else {
347
					// voucher not used. Check if ticket Id is on the roll (not too high)
348
					// and if the ticket is marked used.
349
					// check if voucher already marked as used
350 79262830 Phil Davis
					if (!isset($bitstring[$roll])) {
351 05771a24 Ermal
						$bitstring[$roll] = voucher_read_used_db($roll);
352 79262830 Phil Davis
					}
353 05771a24 Ermal
					$pos = $nr >> 3; // divide by 8 -> octet
354
					$mask = 1 << ($nr % 8);
355
					if (ord($bitstring[$roll][$pos]) & $mask) {
356 51a14c58 Phil Davis
						$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s) already used and expired'), $voucher, $roll, $nr);
357
						$test_result[] = $voucher_err_text;
358
						captiveportal_syslog($voucher_err_text);
359 05771a24 Ermal
						$total_minutes = -1;    // voucher expired
360
						$error++;
361
					} else {
362
						// mark bit for this voucher as used
363
						$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
364 51a14c58 Phil Davis
						$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) good for %4$s Minutes'), $voucher, $roll, $nr, $minutes_per_roll[$roll]);
365 05771a24 Ermal
						$total_minutes += $minutes_per_roll[$roll];
366
					}
367
				}
368
			} else {
369 51a14c58 Phil Davis
				$voucher_err_text = sprintf(gettext('%1$s (%2$s/%3$s): not found on any registered Roll'), $voucher, $roll, $nr);
370
				$test_result[] = $voucher_err_text;
371
				captiveportal_syslog($voucher_err_text);
372 05771a24 Ermal
			}
373
		} else {
374
			// hmm, thats weird ... not what I expected
375 51a14c58 Phil Davis
			$voucher_err_text = sprintf(gettext('%1$s invalid: %2$s !!'), $voucher, $result);
376
			$test_result[] = $voucher_err_text;
377
			captiveportal_syslog($voucher_err_text);
378 05771a24 Ermal
			$error++;
379
		}
380
	}
381 336e3c1c Charlie
382 05771a24 Ermal
	// if this was a test call, we're done. Return the result.
383
	if ($test) {
384
		if ($error) {
385 d8012adb Vinicius Coque
			$test_result[] = gettext("Access denied!");
386 05771a24 Ermal
		} else {
387 086cf944 Phil Davis
			$test_result[] = sprintf(gettext("Access granted for %d Minutes in total."), $total_minutes);
388 05771a24 Ermal
		}
389
		unlock($voucherlck);
390 336e3c1c Charlie
391 05771a24 Ermal
		return $test_result;
392
	}
393 336e3c1c Charlie
394 05771a24 Ermal
	// if we had an error (one of the vouchers is invalid), return 0.
395
	// Discussion: we could return the time remaining for good vouchers, but then
396
	// the user wouldn't know that he used at least one invalid voucher.
397
	if ($error) {
398 830c33be Scott Ullrich
		unlock($voucherlck);
399 79262830 Phil Davis
		if ($total_minutes > 0) {   // probably not needed, but want to make sure
400 05771a24 Ermal
			$total_minutes = 0;     // we only report -1 (expired) or 0 (no access)
401 79262830 Phil Davis
		}
402 05771a24 Ermal
		return $total_minutes;       // well, at least one voucher had errors. Say NO ACCESS
403
	}
404 336e3c1c Charlie
405 6c9ca678 Scott Ullrich
	// If we did a XMLRPC sync earlier check the timeleft
406 62f96568 Ermal
	if (!empty($config['voucher'][$cpzone]['vouchersyncdbip'])) {
407 79262830 Phil Davis
		if (!is_null($remote_time_used)) {
408 62f96568 Ermal
			$total_minutes = $remote_time_used;
409 79262830 Phil Davis
		} else if ($remote_time_used < $total_minutes) {
410 f3512fca jim-p
			$total_minutes -= $remote_time_used;
411 79262830 Phil Davis
		}
412 62f96568 Ermal
	}
413 830c33be Scott Ullrich
414 05771a24 Ermal
	// All given vouchers were valid and this isn't simply a test.
415
	// Write back the used DB's
416 c14d9967 Scott Ullrich
	if (is_array($bitstring)) {
417
		foreach ($bitstring as $roll => $used) {
418 79262830 Phil Davis
			if (is_array($used)) {
419
				foreach ($used as $u) {
420 c14d9967 Scott Ullrich
					voucher_write_used_db($roll, base64_encode($u));
421 79262830 Phil Davis
				}
422 c14d9967 Scott Ullrich
			} else {
423
				voucher_write_used_db($roll, base64_encode($used));
424
			}
425
		}
426
	}
427 336e3c1c Charlie
428 05771a24 Ermal
	// Active DB: we only add the first voucher if multiple given
429
	// and give that one all the time credit. This allows the user to logout and
430
	// log in later using just the first voucher. It also keeps username limited
431
	// to one voucher and that voucher shows the correct time credit in 'active vouchers'
432
	if (!empty($active_vouchers[$first_voucher_roll][$first_voucher])) {
433
		list($timestamp, $minutes) = explode(",", $active_vouchers[$first_voucher_roll][$first_voucher]);
434
	} else {
435
		$timestamp = time();    // new voucher
436
		$minutes = $total_minutes;
437
	}
438 336e3c1c Charlie
439 05771a24 Ermal
	$active_vouchers[$first_voucher_roll][$first_voucher] = "$timestamp,$minutes";
440 80b86de5 Ermal
	voucher_write_active_db($first_voucher_roll, $active_vouchers[$first_voucher_roll]);
441 336e3c1c Charlie
442 f416763b Phil Davis
	/* Trigger a sync of the vouchers on config */
443 5ebe85e9 Ermal
	send_event("service sync vouchers");
444
445 05771a24 Ermal
	unlock($voucherlck);
446 336e3c1c Charlie
447 05771a24 Ermal
	return $total_minutes;
448 336e3c1c Charlie
}
449
450 351b6990 Ermal
function voucher_configure($sync = false) {
451 b4792bf8 Ermal
	global $config, $g, $cpzone;
452 336e3c1c Charlie
453 b4792bf8 Ermal
	if (is_array($config['voucher'])) {
454
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
455 79262830 Phil Davis
			if (platform_booting()) {
456
				echo gettext("Enabling voucher support... ");
457
			}
458 b4792bf8 Ermal
			$cpzone = $voucherzone;
459 abf421ce Ermal
			$error = voucher_configure_zone($sync);
460 285ef132 Ermal LUÇI
			if (platform_booting()) {
461 79262830 Phil Davis
				if ($error) {
462 abf421ce Ermal
					echo "error\n";
463 79262830 Phil Davis
				} else {
464 abf421ce Ermal
					echo "done\n";
465 79262830 Phil Davis
				}
466 abf421ce Ermal
			}
467 b4792bf8 Ermal
		}
468
	}
469
}
470
471
function voucher_configure_zone($sync = false) {
472
	global $config, $g, $cpzone;
473
474 79262830 Phil Davis
	if (!isset($config['voucher'][$cpzone]['enable'])) {
475 eaca40df Ermal
		return 0;
476 79262830 Phil Davis
	}
477 336e3c1c Charlie
478 79262830 Phil Davis
	if ($sync == true) {
479
		captiveportal_syslog("Writing voucher db from sync data...");
480
	}
481 336e3c1c Charlie
482 b4792bf8 Ermal
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
483 eaca40df Ermal
484 79262830 Phil Davis
	/* write public key used to verify vouchers */
485
	$pubkey = base64_decode($config['voucher'][$cpzone]['publickey']);
486
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.public", "w");
487
	if (!$fd) {
488
		captiveportal_syslog("Voucher error: cannot write voucher.public\n");
489
		unlock($voucherlck);
490
		return 1;
491
	}
492
	fwrite($fd, $pubkey);
493
	fclose($fd);
494
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.public", 0600);
495
496
	/* write config file used by voucher binary to decode vouchers */
497
	$fd = fopen("{$g['varetc_path']}/voucher_{$cpzone}.cfg", "w");
498
	if (!$fd) {
499
		printf(gettext("Error: cannot write voucher.cfg") . "\n");
500
		unlock($voucherlck);
501
		return 1;
502
	}
503
	fwrite($fd, "{$config['voucher'][$cpzone]['rollbits']},{$config['voucher'][$cpzone]['ticketbits']},{$config['voucher'][$cpzone]['checksumbits']},{$config['voucher'][$cpzone]['magic']},{$config['voucher'][$cpzone]['charset']}\n");
504
	fclose($fd);
505
	@chmod("{$g['varetc_path']}/voucher_{$cpzone}.cfg", 0600);
506 c4ea3691 Ermal
	unlock($voucherlck);
507 336e3c1c Charlie
508 79262830 Phil Davis
	if ((platform_booting() || $sync == true) && is_array($config['voucher'][$cpzone]['roll'])) {
509 336e3c1c Charlie
510 b4792bf8 Ermal
		$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
511 336e3c1c Charlie
512 79262830 Phil Davis
		// create active and used DB per roll on ramdisk from config
513
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
514
515
			$roll = $rollent['number'];
516 351b8519 Luiz Otavio O Souza
			$len = ($rollent['count'] >> 3) + 1;
517 4e322e2c Phil Davis
			if (strlen(base64_decode($rollent['used'])) != $len) {
518 351b8519 Luiz Otavio O Souza
				$rollent['used'] = base64_encode(str_repeat("\000", $len));
519 4e322e2c Phil Davis
			}
520 79262830 Phil Davis
			voucher_write_used_db($roll, $rollent['used']);
521
			$minutes = $rollent['minutes'];
522
			$active_vouchers = array();
523
			$a_active = &$rollent['active'];
524
			if (is_array($a_active)) {
525
				foreach ($a_active as $activent) {
526
					$voucher = $activent['voucher'];
527
					$timestamp = $activent['timestamp'];
528
					$minutes = $activent['minutes'];
529
					// its tempting to check for expired timestamps, but during
530
					// bootup, we most likely don't have the correct time.
531
					$active_vouchers[$voucher] = "$timestamp,$minutes";
532
				}
533
			}
534
			voucher_write_active_db($roll, $active_vouchers);
535
		}
536 c4ea3691 Ermal
537
		unlock($voucherlck);
538 79262830 Phil Davis
	}
539 eaca40df Ermal
540
	return 0;
541 336e3c1c Charlie
}
542
543 79262830 Phil Davis
/* write bitstring of used vouchers to ramdisk.
544 336e3c1c Charlie
 * Bitstring must already be base64_encoded!
545
 */
546
function voucher_write_used_db($roll, $vdb) {
547 b4792bf8 Ermal
	global $g, $cpzone;
548 666bc4d1 Ermal
549 b4792bf8 Ermal
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db", "w");
550 666bc4d1 Ermal
	if ($fd) {
551
		fwrite($fd, $vdb . "\n");
552
		fclose($fd);
553 79262830 Phil Davis
	} else {
554 51a14c58 Phil Davis
		voucher_log(LOG_ERR, sprintf(gettext('cant write %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
555 79262830 Phil Davis
	}
556 336e3c1c Charlie
}
557
558
/* return assoc array of active vouchers with activation timestamp
559 79262830 Phil Davis
 * voucher is index.
560 336e3c1c Charlie
 */
561
function voucher_read_active_db($roll) {
562 b4792bf8 Ermal
	global $g, $cpzone;
563 666bc4d1 Ermal
564
	$active = array();
565
	$dirty = 0;
566 b4792bf8 Ermal
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db";
567 666bc4d1 Ermal
	if (file_exists($file)) {
568
		$fd = fopen($file, "r");
569
		if ($fd) {
570
			while (!feof($fd)) {
571
				$line = trim(fgets($fd));
572
				if ($line) {
573 086cf944 Phil Davis
					list($voucher, $timestamp, $minutes) = explode(",", $line); // voucher,timestamp
574 79262830 Phil Davis
					if ((($timestamp + (60*$minutes)) - time()) > 0) {
575 666bc4d1 Ermal
						$active[$voucher] = "$timestamp,$minutes";
576 79262830 Phil Davis
					} else {
577 666bc4d1 Ermal
						$dirty=1;
578 79262830 Phil Davis
					}
579 666bc4d1 Ermal
				}
580
			}
581
			fclose($fd);
582 5ebe85e9 Ermal
			if ($dirty) { // if we found expired entries, lets save our snapshot
583 666bc4d1 Ermal
				voucher_write_active_db($roll, $active);
584 5ebe85e9 Ermal
585 f416763b Phil Davis
				/* Trigger a sync of the vouchers on config */
586 5ebe85e9 Ermal
				send_event("service sync vouchers");
587
			}
588 666bc4d1 Ermal
		}
589
	}
590
	return $active;
591 336e3c1c Charlie
}
592
593
/* store array of active vouchers back to DB */
594
function voucher_write_active_db($roll, $active) {
595 79262830 Phil Davis
	global $g, $cpzone;
596 336e3c1c Charlie
597 79262830 Phil Davis
	if (!is_array($active)) {
598 05771a24 Ermal
		return;
599 79262830 Phil Davis
	}
600
	$fd = fopen("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db", "w");
601
	if ($fd) {
602
		foreach ($active as $voucher => $value) {
603
			fwrite($fd, "$voucher,$value\n");
604
		}
605
		fclose($fd);
606
	}
607 336e3c1c Charlie
}
608
609
/* return how many vouchers are marked used on a roll */
610
function voucher_used_count($roll) {
611 79262830 Phil Davis
	global $g, $cpzone;
612
613
	$bitstring = voucher_read_used_db($roll);
614
	$max = strlen($bitstring) * 8;
615
	$used = 0;
616
	for ($i = 1; $i <= $max; $i++) {
617
		// check if ticket already used or not.
618
		$pos = $i >> 3;            // divide by 8 -> octet
619
		$mask = 1 << ($i % 8);  // mask to test bit in octet
620
		if (ord($bitstring[$pos]) & $mask) {
621
			$used++;
622
		}
623
	}
624
	unset($bitstring);
625
626
	return $used;
627 336e3c1c Charlie
}
628
629
function voucher_read_used_db($roll) {
630 79262830 Phil Davis
	global $g, $cpzone;
631
632
	$vdb = "";
633
	$file = "{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db";
634
	if (file_exists($file)) {
635
		$fd = fopen($file, "r");
636
		if ($fd) {
637
			$vdb = trim(fgets($fd));
638
			fclose($fd);
639
		} else {
640 51a14c58 Phil Davis
			voucher_log(LOG_ERR, sprintf(gettext('cant read %1$s/voucher_%2$s_used_%3$s.db'), $g['vardb_path'], $cpzone, $roll));
641 79262830 Phil Davis
		}
642
	}
643
	return base64_decode($vdb);
644 336e3c1c Charlie
}
645
646
function voucher_unlink_db($roll) {
647 79262830 Phil Davis
	global $g, $cpzone;
648
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_used_$roll.db");
649
	@unlink("{$g['vardb_path']}/voucher_{$cpzone}_active_$roll.db");
650 336e3c1c Charlie
}
651
652
/* we share the log with captiveportal for now */
653
function voucher_log($priority, $message) {
654
655 79262830 Phil Davis
	$message = trim($message);
656
	openlog("logportalauth", LOG_PID, LOG_LOCAL4);
657 086cf944 Phil Davis
	syslog($priority, sprintf(gettext("Voucher: %s"), $message));
658 79262830 Phil Davis
	closelog();
659 336e3c1c Charlie
}
660
661
/* Save active and used voucher DB into XML config and write it to flash
662 5ebe85e9 Ermal
 * Called during reboot -> system_reboot_cleanup() and every active voucher change
663 336e3c1c Charlie
 */
664
function voucher_save_db_to_config() {
665 79262830 Phil Davis
	global $config, $g, $cpzone;
666 b4792bf8 Ermal
667
	if (is_array($config['voucher'])) {
668 573ae2d1 Chris Buechler
		foreach ($config['voucher'] as $voucherzone => $vcfg) {
669 b4792bf8 Ermal
			$cpzone = $voucherzone;
670
			voucher_save_db_to_config_zone();
671
		}
672
	}
673
}
674
675 52bb3619 Cyrill Bannwart
function voucher_save_db_to_config_zone() {
676 79262830 Phil Davis
	global $config, $g, $cpzone;
677 336e3c1c Charlie
678 79262830 Phil Davis
	if (!isset($config['voucher'][$cpzone]['enable'])) {
679
		return;   // no vouchers or don't want to save DB's
680
	}
681
682
	if (!is_array($config['voucher'][$cpzone]['roll'])) {
683
		return;
684
	}
685
686
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
687 c65c3a5d Ermal
688 79262830 Phil Davis
	// walk all active rolls and save runtime DB's to flash
689
	$a_roll = &$config['voucher'][$cpzone]['roll'];
690
	while (list($key, $value) = each($a_roll)) {
691
		$rollent = &$a_roll[$key];
692
		$roll = $rollent['number'];
693
		$bitmask = voucher_read_used_db($roll);
694
		$rollent['used'] = base64_encode($bitmask);
695
		$active_vouchers = voucher_read_active_db($roll);
696
		$db = array();
697 4d5bbdfb Scott Ullrich
		$dbi = 1;
698 79262830 Phil Davis
		foreach ($active_vouchers as $voucher => $line) {
699 086cf944 Phil Davis
			list($timestamp, $minutes) = explode(",", $line);
700 79262830 Phil Davis
			$activent['voucher'] = $voucher;
701
			$activent['timestamp'] = $timestamp;
702
			$activent['minutes'] = $minutes;
703
			$db["v{$dbi}"] = $activent;
704
			$dbi++;
705
		}
706
		$rollent['active'] = $db;
707
		unset($active_vouchers);
708
	}
709
710
	unlock($voucherlck);
711
712 51a14c58 Phil Davis
	write_config(gettext("Syncing vouchers"));
713 79262830 Phil Davis
	return;
714 336e3c1c Charlie
}
715
716 2ee5fe9b Ermal Lu?i
?>