summaryrefslogtreecommitdiffstats
path: root/usr/local/www/services_captiveportal_vouchers.php
blob: c6cf3e9a0b24af31f5aaf3342421f500a26eb5bf (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php 
/*
	Copyright (C) 2007 Marcel Wiget <mwiget@mac.com>
	All rights reserved. 
	
	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.
*/
/*
	pfSense_BUILDER_BINARIES:	/usr/local/bin/voucher
	pfSense_MODULE:	captiveportal
*/

##|+PRIV
##|*IDENT=page-services-captiveportal-vouchers
##|*NAME=Services: Captive portal Vouchers page
##|*DESCR=Allow access to the 'Services: Captive portal Vouchers' page.
##|*MATCH=services_captiveportal_vouchers.php*
##|-PRIV

$statusurl = "status_captiveportal_vouchers.php";
$logurl = "diag_logs_auth.php";

require("guiconfig.inc");
require("functions.inc");
require("filter.inc");
require("shaper.inc");
require("captiveportal.inc");
require_once("voucher.inc");

$pgtitle = array(gettext("Services"), gettext("Captive portal"), gettext("Vouchers"));

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

if (!is_array($config['voucher']['roll'])) {
    $config['voucher']['roll'] = array();
}
if (!isset($config['voucher']['charset'])) {
    $config['voucher']['charset'] = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
}
if (!isset($config['voucher']['rollbits'])) {
    $config['voucher']['rollbits'] = 16;
}
if (!isset($config['voucher']['ticketbits'])) {
    $config['voucher']['ticketbits'] = 10;
}
if (!isset($config['voucher']['saveinterval'])) {
    $config['voucher']['saveinterval'] = 300;
}
if (!isset($config['voucher']['checksumbits'])) {
    $config['voucher']['checksumbits'] = 5;
}
if (!isset($config['voucher']['magic'])) {
    $config['voucher']['magic'] = rand();   // anything slightly random will do
}
if (!isset($config['voucher']['publickey'])) {
	
	/* generate a random 64 bit RSA key pair using the voucher binary */
	$fd = popen("/usr/local/bin/voucher -g 64", "r");
	if ($fd !== false) {
		$output = fread($fd, 16384);
	    pclose($fd);
		
		list($privkey, $pubkey) = explode("\0", $output);
		
	    $config['voucher']['publickey'] = base64_encode($pubkey);
	    $config['voucher']['privatekey'] = base64_encode($privkey);
	}
}
if (!isset($config['voucher']['msgnoaccess'])) {
    $config['voucher']['msgnoaccess'] = gettext("Voucher invalid");
}
if (!isset($config['voucher']['msgexpired'])) {
    $config['voucher']['msgexpired'] = gettext("Voucher expired");
}

$a_roll = &$config['voucher']['roll'];

if ($_GET['act'] == "del") {
    $id = $_GET['id'];
    if ($a_roll[$id]) {
        $roll = $a_roll[$id]['number']; 
	$voucherlck = lock('voucher');
        unset($a_roll[$id]);
        voucher_unlink_db($roll);
	unlock($voucherlck);
        write_config();
        header("Location: services_captiveportal_vouchers.php");
        exit;
    }
}

/* print all vouchers of the selected roll */
if ($_GET['act'] == "csv") {
    $privkey = base64_decode($config['voucher']['privatekey']);
    if (strstr($privkey,"BEGIN RSA PRIVATE KEY")) {
        $fd = fopen("{$g['varetc_path']}/voucher.private","w");
        if (!$fd) {
            $input_errors[] = gettext("Cannot write private key file") . ".\n";
        } else {
            chmod("{$g['varetc_path']}/voucher.private", 0600);
            fwrite($fd, $privkey);
            fclose($fd);

            $a_voucher = &$config['voucher']['roll'];
            $id = $_GET['id'];
            if (isset($id) && $a_voucher[$id]) {
                $number = $a_voucher[$id]['number'];
                $count = $a_voucher[$id]['count'];

                header("Content-Type: application/octet-stream");
                header("Content-Disposition: attachment; filename=vouchers_roll$number.csv");
                system("/usr/local/bin/voucher -c {$g['varetc_path']}/voucher.cfg -p {$g['varetc_path']}/voucher.private $number $count");
                unlink("{$g['varetc_path']}/voucher.private");
                exit;
            }
        }
    } else {
        $input_errors[] = gettext("Need private RSA key to print vouchers") . "\n";
    }
}

$pconfig['enable'] = isset($config['voucher']['enable']);
$pconfig['charset'] = $config['voucher']['charset'];
$pconfig['rollbits'] = $config['voucher']['rollbits'];
$pconfig['ticketbits'] = $config['voucher']['ticketbits'];
$pconfig['saveinterval'] = $config['voucher']['saveinterval'];
$pconfig['checksumbits'] = $config['voucher']['checksumbits'];
$pconfig['magic'] = $config['voucher']['magic'];
$pconfig['publickey'] = base64_decode($config['voucher']['publickey']);
$pconfig['privatekey'] = base64_decode($config['voucher']['privatekey']);
$pconfig['msgnoaccess'] = $config['voucher']['msgnoaccess'];
$pconfig['msgexpired'] = $config['voucher']['msgexpired'];

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;

	/* input validation */
	if ($_POST['enable']) {
		$reqdfields = explode(" ", "charset rollbits ticketbits checksumbits publickey magic saveinterval");
		$reqdfieldsn = array(gettext("charset"),gettext("rollbits"),gettext("ticketbits"),gettext("checksumbits"),gettext("publickey"),gettext("magic"),gettext("saveinterval"));
		
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
	}
	
	if ($_POST['charset'] && (strlen($_POST['charset'] < 2))) {
		$input_errors[] = gettext("Need at least 2 characters to create vouchers.");
	}
	if ($_POST['charset'] && (strpos($_POST['charset'],"\"")>0)) {
		$input_errors[] = gettext("Double quotes aren't allowed.");
	}
	if ($_POST['charset'] && (strpos($_POST['charset'],",")>0)) {
		$input_errors[] = "',' " . gettext("aren't allowed.");
	}
	if ($_POST['rollbits'] && (!is_numeric($_POST['rollbits']) || ($_POST['rollbits'] < 1) || ($_POST['rollbits'] > 31))) {
		$input_errors[] = gettext("# of Bits to store Roll Id needs to be between 1..31.");
	}
	if ($_POST['ticketbits'] && (!is_numeric($_POST['ticketbits']) || ($_POST['ticketbits'] < 1) || ($_POST['ticketbits'] > 16))) {
		$input_errors[] = gettext("# of Bits to store Ticket Id needs to be between 1..16.");
	}
	if ($_POST['checksumbits'] && (!is_numeric($_POST['checksumbits']) || ($_POST['checksumbits'] < 1) || ($_POST['checksumbits'] > 31))) {
		$input_errors[] = gettext("# of Bits to store checksum needs to be between 1..31.");
	}
	if ($_POST['saveinterval'] && (!is_numeric($_POST['saveinterval']) || ($_POST['saveinterval'] < 1))) {
		$input_errors[] = gettext("Save interval in minutes cant be negative.");
	}
	if ($_POST['publickey'] && (!strstr($_POST['publickey'],"BEGIN PUBLIC KEY"))) {
		$input_errors[] = gettext("This doesn't look like an RSA Public key.");
	}
	if ($_POST['privatekey'] && (!strstr($_POST['privatekey'],"BEGIN RSA PRIVATE KEY"))) {
		$input_errors[] = gettext("This doesn't look like an RSA Private key.");
	}

	if (!$input_errors) {
        	$config['voucher']['enable'] = $_POST['enable'] ? true : false;
        	$config['voucher']['charset'] = $_POST['charset'];
        	$config['voucher']['rollbits'] = $_POST['rollbits'];
        	$config['voucher']['ticketbits'] = $_POST['ticketbits'];
        	$config['voucher']['checksumbits'] = $_POST['checksumbits'];
        	$config['voucher']['magic'] = $_POST['magic'];
        	$config['voucher']['saveinterval'] = $_POST['saveinterval'];
        	$config['voucher']['publickey'] = base64_encode($_POST['publickey']);
        	$config['voucher']['privatekey'] = base64_encode($_POST['privatekey']);
        	$config['voucher']['msgnoaccess'] = $_POST['msgnoaccess'];
        	$config['voucher']['msgexpired'] = $_POST['msgexpired'];

		write_config();
        	voucher_configure();
        	if (isset($config['voucher']['enable']) && !isset($config['captiveportal']['enable'])) {
            		$savemsg = gettext("Don't forget to configure and enable Captive Portal.");
        	}
	}
}
include("head.inc");
?>
<?php include("fbegin.inc"); ?>
<script type="text/javascript">
<!--
function enable_change(enable_change) {
	var endis;
	endis = !(document.iform.enable.checked || enable_change);
	
	document.iform.charset.disabled = endis;
	document.iform.rollbits.disabled = endis;
	document.iform.ticketbits.disabled = endis;
	document.iform.saveinterval.disabled = endis;
	document.iform.checksumbits.disabled = endis;
	document.iform.magic.disabled = endis;
	document.iform.publickey.disabled = endis;
	document.iform.privatekey.disabled = endis;
	document.iform.msgnoaccess.disabled = endis;
	document.iform.msgexpired.disabled = endis;
}
//-->
</script>
<?php if ($input_errors) print_input_errors($input_errors); ?>
<?php if ($savemsg) print_info_box($savemsg); ?>
<form action="services_captiveportal_vouchers.php" method="post" enctype="multipart/form-data" name="iform" id="iform">
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="tab pane">
  <tr><td class="tabnavtbl">
  <ul id="tabnav">
<?php 
	$tab_array = array();
        $tab_array[] = array(gettext("Captive portal"), false, "services_captiveportal.php");
        $tab_array[] = array(gettext("Pass-through MAC"), false, "services_captiveportal_mac.php");
        $tab_array[] = array(gettext("Allowed IP addresses"), false, "services_captiveportal_ip.php");
        $tab_array[] = array(gettext("Vouchers"), true, "services_captiveportal_vouchers.php");
        $tab_array[] = array(gettext("File Manager"), false, "services_captiveportal_filemanager.php");
        display_top_tabs($tab_array);
?> 
  </ul>
  </td></tr>
  <tr>
  <td class="tabcont">
  <table width="100%" border="0" cellpadding="6" cellspacing="0" summary="checkbox pane">
	<tr> 
	  <td width="22%" valign="top" class="vtable">&nbsp;</td>
	  <td width="78%" class="vtable">
		<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
		<strong><?=gettext("Enable Vouchers"); ?></strong></td>
	</tr>
	<tr>
	  <td valign="top" class="vncell"><?=gettext("Voucher Rolls"); ?></td>
	  <td class="vtable">

              <table width="100%" border="0" cellpadding="0" cellspacing="0" summary="content pane">
                <tr>
                  <td width="10%" class="listhdrr"><?=gettext("Roll"); ?>#</td>
                  <td width="20%" class="listhdrr"><?=gettext("Minutes/Ticket"); ?></td>
                  <td width="20%" class="listhdrr"># <?=gettext("of Tickets"); ?></td>
                  <td width="35%" class="listhdr"><?=gettext("Comment"); ?></td>
                  <td width="15%" class="list"></td>
		</tr>
	<?php $i = 0; foreach($a_roll as $rollent): ?>
		<tr>
                  <td class="listlr">
                    <?=htmlspecialchars($rollent['number']); ?>&nbsp;
                  </td>
                  <td class="listr">
                    <?=htmlspecialchars($rollent['minutes']);?>&nbsp;
                  </td>
                  <td class="listr">
                    <?=htmlspecialchars($rollent['count']);?>&nbsp;
                  </td>
                  <td class="listr">
                    <?=htmlspecialchars($rollent['comment']); ?>&nbsp;
                  </td>
                  <td valign="middle" nowrap class="list"> 
                  <?php if ($pconfig['enable']): ?> 
                    <a href="services_captiveportal_vouchers_edit.php?id=<?=$i; ?>"><img src="/themes/<?=$g['theme']; ?>/images/icons/icon_e.gif" title="<?=gettext("edit voucher"); ?>" width="17" height="17" border="0" alt="<?=gettext("edit voucher"); ?>"></a>
                    <a href="services_captiveportal_vouchers.php?act=del&amp;id=<?=$i; ?>" onclick="return confirm('<?=gettext("Do you really want to delete this voucher? This makes all vouchers from this roll invalid"); ?>')"><img src="/themes/<?=$g['theme']; ?>/images/icons/icon_x.gif" title="<?=gettext("delete vouchers"); ?>" width="17" height="17" border="0" alt="<?=gettext("delete vouchers"); ?>"></a>
                    <a href="services_captiveportal_vouchers.php?act=csv&amp;id=<?=$i; ?>"><img src="/themes/<?=$g['theme']; ?>/images/icons/icon_log_s.gif" title="<?=gettext("generate vouchers for this roll to CSV file"); ?>" width="11" height="15" border="0" alt="<?=gettext("generate vouchers for this roll to CSV file"); ?>"></a>
                    <?php endif;?>
                  </td>
		</tr>
	<?php $i++; endforeach; ?>
		<tr> 
			  <td class="list" colspan="4"></td>
              <?php
	      if ($pconfig['enable']) {
		echo "<td class=\"list\"> <a href=\"services_captiveportal_vouchers_edit.php\"><img src=\"/themes/{$g['theme']}/images/icons/icon_plus.gif\" title=\"" . gettext("add voucher") . "\" width=\"17\" height=\"17\" border=\"0\" alt=\"" . gettext("add voucher") . "\"></a></td>";
              }
	      ?>
	    </tr>
        </table>     
<?php if ($pconfig['enable']): ?> 
<?=gettext("Create, generate and activate Rolls with Vouchers that allow access through the " .
"captive portal for the configured time. Once a voucher is activated, " .
"its clock is started and runs uninterrupted until it expires. During that " .
"time, the voucher can be re-used from the same or a different computer. If the voucher " .
"is used again from another computer, the previous session is stopped"); ?>.
<?php else: ?>
<?=gettext("Enable Voucher support first using the checkbox above and hit Save at the bottom"); ?>.</td>
<?php endif;?>
	</tr>
	<tr>
      <td valign="top" class="vncellreq"><?=gettext("Voucher public key"); ?></td>
      <td class="vtable">
        <textarea name="publickey" cols="65" rows="4" id="publickey" class="formpre"><?=htmlspecialchars($pconfig['publickey']);?></textarea>
        <br>
    <?=gettext("Paste an RSA public key (64 Bit or smaller) in PEM format here. This key is used to decrypt vouchers"); ?>.</td>
	</tr>
	<tr>
      <td valign="top" class="vncell"><?=gettext("Voucher private key"); ?></td>
      <td class="vtable">
        <textarea name="privatekey" cols="65" rows="5" id="privatekey" class="formpre"><?=htmlspecialchars($pconfig['privatekey']);?></textarea>
        <br>
    <?=gettext("Paste an RSA private key (64 Bit or smaller) in PEM format here. This key is only used to generate encrypted vouchers and doesn't need to be available if the vouchers have been generated offline"); ?>.</td>
	</tr>
	<tr> 
       <td width="22%" valign="top" class="vncellreq"><?=gettext("Character set"); ?></td>
       <td width="78%" class="vtable">
         <input name="charset" type="text" class="formfld" id="charset" size="80" value="<?=htmlspecialchars($pconfig['charset']);?>">
         <br>
         <?=gettext("Tickets are generated with the specified character set. It should contain printable characters (numbers, lower case and upper case letters) that are hard to confuse with others. Avoid e.g. 0/O and l/1"); ?>.</td>
    </tr>
	<tr> 
       <td width="22%" valign="top" class="vncellreq"># <?=gettext("of Roll Bits"); ?></td>
       <td width="78%" class="vtable">
         <input name="rollbits" type="text" class="formfld" id="rollbits" size="2" value="<?=htmlspecialchars($pconfig['rollbits']);?>">
         <br>
         <?=gettext("Reserves a range in each voucher to store the Roll# it belongs to. Allowed range: 1..31. Sum of Roll+Ticket+Checksum bits must be one Bit less than the RSA key size"); ?>.</td>
    </tr>
	<tr> 
       <td width="22%" valign="top" class="vncellreq"># <?=gettext("of Ticket Bits"); ?></td>
       <td width="78%" class="vtable">
         <input name="ticketbits" type="text" class="formfld" id="ticketbits" size="2" value="<?=htmlspecialchars($pconfig['ticketbits']);?>">
         <br>
         <?=gettext("Reserves a range in each voucher to store the Ticket# it belongs to. Allowed range: 1..16. Using 16 bits allows a roll to have up to 65535 vouchers. A bit array, stored in RAM and in the config, is used to mark if a voucher has been used. A bit array for 65535 vouchers requires 8 KB of storage"); ?>.</td>
    </tr>
	<tr> 
       <td width="22%" valign="top" class="vncellreq"># <?=gettext("of Checksum Bits"); ?></td>
       <td width="78%" class="vtable">
         <input name="checksumbits" type="text" class="formfld" id="checksumbits" size="2" value="<?=htmlspecialchars($pconfig['checksumbits']);?>">
         <br>
         <?=gettext("Reserves a range in each voucher to store a simple checksum over Roll# and Ticket#. Allowed range is 0..31"); ?>.</td>
    </tr>
	<tr> 
       <td width="22%" valign="top" class="vncellreq"><?=gettext("Magic Number"); ?></td>
       <td width="78%" class="vtable">
         <input name="magic" type="text" class="formfld" id="magic" size="20" value="<?=htmlspecialchars($pconfig['magic']);?>">
         <br>
         <?=gettext("Magic number stored in every voucher. Verified during voucher check. Size depends on how many bits are left by Roll+Ticket+Checksum bits. If all bits are used, no magic number will be used and checked"); ?>.</td>
    </tr>
    <tr> 
       <td width="22%" valign="top" class="vncellreq"><?=gettext("Save Interval"); ?></td>
       <td width="78%" class="vtable">
         <input name="saveinterval" type="text" class="formfld" id="saveinterval" size="4" value="<?=htmlspecialchars($pconfig['saveinterval']);?>">
         <?=gettext("Minutes"); ?><br>
         <?=gettext("The list of active and used vouchers can be stored in the system's configuration file once every x minutes to survive power outages. No save is done if no new vouchers have been activated.  Enter 0 to never write runtime state to XML config"); ?>.</td>
    </tr>
    <tr> 
       <td width="22%" valign="top" class="vncellreq"><?=gettext("Invalid Voucher Message"); ?></td>
       <td width="78%" class="vtable">
         <input name="msgnoaccess" type="text" class="formfld" id="msgnoaccess" size="80" value="<?=htmlspecialchars($pconfig['msgnoaccess']);?>">
         <br><?=gettext("Error message displayed for invalid vouchers on captive portal error page"); ?> ($PORTAL_MESSAGE$).</td>
    </tr>
    <tr> 
       <td width="22%" valign="top" class="vncellreq"><?=gettext("Expired Voucher Message"); ?></td>
       <td width="78%" class="vtable">
         <input name="msgexpired" type="text" class="formfld" id="msgexpired" size="80" value="<?=htmlspecialchars($pconfig['msgexpired']);?>">
         <br><?=gettext("Error message displayed for expired vouchers on captive portal error page"); ?> ($PORTAL_MESSAGE$).</td>
    </tr>
    <tr>
       <td width="22%" valign="top">&nbsp;</td>
       <td width="78%">
        <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" onClick="enable_change(true)">
       </td>
    </tr>
    <tr>
     <td colspan="2" class="list"><p class="vexpl">
     <span class="red"><strong> <?=gettext("Note"); ?>:<br>   </strong></span>
      <?=gettext("Changing any Voucher parameter (apart from managing the list of Rolls) on this page will render existing vouchers useless if they were generated with different settings"); ?>.
      </p>
     </td>
    </tr>
  </table>
  </td>
  </tr>
  </table>
</form>
<script type="text/javascript">
<!--
enable_change(false);
//-->
</script>
<?php include("fend.inc"); ?>
OpenPOWER on IntegriCloud