forked from jpardobl/pfsense_vouchers_rest
/
cp.php
329 lines (253 loc) · 10.1 KB
/
cp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
cp.php
Copyright (C) 2012 Javier Pardo Blasco
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require('globals.inc');
require("captiveportal.inc");
require_once("voucher.inc");
define("BASE_URL", '/cp.php');
define("VOUCHER_BIN", '/usr/local/bin/voucher');
global $cpzone, $g;
define("SECRET_KEY", 'michorizo');
class RollNotFoundException extends Exception{}
class CantWritePrivateKeyException extends Exception{}
function auth() {
//file_put_contents('/tmp/trace', $_SERVER);
if(
$_SERVER['auth'] == SECRET_KEY ||
$_SERVER['Auth'] == SECRET_KEY ||
$_SERVER['AUTH'] == SECRET_KEY ||
$_SERVER['HTTP_AUTH'] == SECRET_KEY
) return true;
return false;
}
if(!auth()){
header("Not authorized", false, 403);
return;
}
/**
* Given the roll number, returns the the roll index in the persistence array.
* @param Array $db The roll database as it arrives from the persistence
* @param int $number The roll number
*/
function get_rol_index_by_number($db, $number){
//print "Get rol index by number:";
//print_r($db);
//print "Number: ".$number;
foreach($db as $key => $rol)
if($rol["number"] == $number)return $key;
throw new RollNotFoundException("Number:".$number);
}
/**
* Given a roll number, returns the vouchers for that roll
* @param integer $roll_number
* @param integer $count The number of vouchers to return
*/
function retrieve_vouchers($roll_number, $count){
global $g, $config, $cpzone;
$privkey = base64_decode($config['voucher'][$cpzone]['privatekey']);
if (!strstr($privkey,"BEGIN RSA PRIVATE KEY")) throw new CantWritePrivateKeyException("There is no RSA key info");
$fd = fopen("{$g['varetc_path']}/voucher.private","w");
if (!$fd) throw new CantWritePrivateKeyException("Cant write RSA key to temp disk");
chmod("{$g['varetc_path']}/voucher.private", 0600);
fwrite($fd, $privkey);
fclose($fd);
$cmd = VOUCHER_BIN." -c {$g['varetc_path']}/voucher_$cpzone.cfg -p {$g['varetc_path']}/voucher.private $roll_number $count";
#print "cmd: $cmd";
exec($cmd, $out);
unlink("{$g['varetc_path']}/voucher.private");
$vs = Array();
foreach( $out as $line){
if(!preg_match('/^\"\s(.*)\"$/', $line, $voucher)) continue;
$vs[] = $voucher[1];
}
return $vs;
}
/**
* parses a roll to json
* @param Array $roll The roll info as it arrives from the pfSense persistence
*/
function to_json($roll){
$format = '{"number": "%s", "minutes": "%s", "comment": "%s", "count": "%s"';
$data = sprintf($format, $roll["number"], $roll["minutes"], $roll["comment"], $roll["count"]);
#print_r($roll);
if(isset($roll["active"]) && sizeof($roll["active"]) && is_array($roll["active"])){
$acts = $roll["active"];
$data .= ', "active": {';
foreach($acts as $key => $vo){
$data .= sprintf('"%s": ', $key);
$data .= sprintf('{"voucher": "%s", "timestamp": "%s"},', $vo["voucher"], $vo["timestamp"]);
}
$data = str_replace("},", "}}", $data);
}
$data .= sprintf(', "used": "%s"', voucher_used_count($roll["number"]));
$vouchers = retrieve_vouchers($roll["number"], $roll["count"]);
$tdata = "";
foreach( $vouchers as $voucher)
$tdata .= sprintf('"%s", ', $voucher);
//print_r($vouchers);
if(sizeof($vouchers) > 0){
$tdata = preg_replace( "/,\s$/", "", $tdata );
$data .= ', "vouchers": ['.$tdata."]";
}
$data .= "}";
retrieve_vouchers($roll["number"], $roll["count"]);
return $data;
}
/**
* Validates the user input for creating new posts
*/
function valid_post(){
global $a_roll, $config, $cpzone;
// Look for duplicate roll #
foreach($a_roll as $re) {
if($re['number'] == $_POST['number']) {
header("Resource exists", fall, 409);
print "Roll number %s already exists." % $_POST['number'];
return false;
}
}
$maxnumber = (1<<$config['voucher'][$cpzone]['rollbits']) -1; // Highest Roll#
$maxcount = (1<<$config['voucher'][$cpzone]['ticketbits']) -1; // Highest Ticket#
if(!isset($_POST['number']))
$input_errors[] = "Roll number is missing";
if(!isset($_POST['count']))
$input_errors[] = "Roll vouchers count is missing";
if(!isset($_POST['minutes']))
$input_errors[] = "Roll minutes is missing";
if (!is_numeric($_POST['number']) || $_POST['number'] >= $maxnumber)
$input_errors[] = sprintf(gettext("Roll number must be numeric and less than %s"), $maxnumber);
if (!is_numeric($_POST['count']) || $_POST['count'] < 1 || $_POST['count'] > $maxcount)
$input_errors[] = sprintf(gettext("A roll has at least one voucher and less than %s."), $maxcount);
if (!is_numeric($_POST['minutes']) || $_POST['minutes'] < 1)
$input_errors[] = gettext("Each voucher must be good for at least 1 minute.");
if($input_errors){
header("Errors found", fall, 400);
foreach($input_errors as $error){
$out .= $error."\n";
}
return false;
}
return true;
}
/*
* Checks if the config is correctly initialized
*/
function load_cfg(){
global $config, $cpzone;
//print_r( $config["voucher"]);
if (!is_array($config['voucher'])) {
print "No existe config de voucher";
$config['voucher'] = array();
}
if (!is_array($config['voucher'][$cpzone]['roll'])) {
print "no existe info de roles";
$config['voucher'][$cpzone]['roll'] = array();
}
$a_roll = &$config['voucher'][$cpzone]['roll'];
return $a_roll;
}
#print( "has hecho:".$_SERVER['REQUEST_METHOD']."--->");
switch($_SERVER['REQUEST_METHOD']){
case GET:
if(preg_match('/^\/roll\/(\w+)\/(\d+)\/?$/', $_SERVER['PATH_INFO'], $matches)){
$cpzone = strtolower($matches[1]);
$number = $matches[2];
}else{
header("Bad URI", false, 400);
print "Bad URI";
return;
}
$a_roll = load_cfg();
#print_r($a_roll);
try{
$id = get_rol_index_by_number($a_roll, $number);
}catch(RollNotFoundException $ex){
header("Not found", false, 404);
print "Not found";
return;
}
$rol = $a_roll[$id];
header("Content-Type: application/json");
$data = to_json($rol);
print $data;
break;
case DELETE:
$id = -1;
if(preg_match('/^\/roll\/(\w+)\/(\d+)\/?$/', $_SERVER['PATH_INFO'], $matches)){
$cpzone = strtolower($matches[1]);
$number = $matches[2];
}else{
header("Bad URI", false, 400);
print "Bad URI";
return;
}
$a_roll = load_cfg();
#END load_cfg
try{
$id = get_rol_index_by_number($a_roll, $number);
}catch(RollNotFoundException $ex){
header("Not found", false, 404);
print "Not found";
return;
}
$roll = $a_roll[$id]['number'];
$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
unset($config['voucher'][$cpzone]['roll'][$id]);
voucher_unlink_db($roll);
unlock($voucherlck);
write_config();
header("Roll $id deleted", false, 204);
break;
case POST:
# print "POST";
// print $_SERVER['PATH_INFO'];
if(preg_match('/^\/roll\/(\w+)\/?$/', $_SERVER['PATH_INFO'], $matches)){
$cpzone = strtolower($matches[1]);
}else{
header("Not found", false, 404);
print "Not found";
return;
}
$a_roll = load_cfg();
if(!valid_post())return;
$voucherlck = lock("voucher{$cpzone}");
$rollent['zone'] = $cpzone;
$rollent['number'] = $_POST['number'];
$rollent['minutes'] = $_POST['minutes'];
$rollent['comment'] = $_POST['comment'];
$rollent['count'] = $_POST['count'];
$len = ($rollent['count']>>3) + 1; // count / 8 +1
$rollent['used'] = base64_encode(str_repeat("\000",$len)); // 4 bitmask
$rollent['active'] = array();
voucher_write_used_db($rollent['number'], $rollent['used']);
voucher_write_active_db($rollent['number'], array()); // create empty DB
voucher_log(LOG_INFO,sprintf(gettext('All %1$s vouchers from Roll %2$s marked unused'), $rollent['count'], $rollent['number']));
unlock($voucherlck);
$config['voucher'][$cpzone]['roll'][] = $rollent;
//print_r($a_roll);
//print($g['conf_path']);
write_config();
header("Location: ".BASE_URL."/roll/".$cpzone."/".$_POST["number"], false, 303);
print BASE_URL."/roll/".$cpzone."/".$_POST["number"];
break;
}
?>